]> andersk Git - openssh.git/blob - monitor_fdpass.c
fix for systems that have both HAVE_ACCRIGHTS_IN_MSGHDR and
[openssh.git] / monitor_fdpass.c
1 /*
2  * Copyright 2001 Niels Provos <provos@citi.umich.edu>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "includes.h"
27 RCSID("$OpenBSD: monitor_fdpass.c,v 1.2 2002/03/24 17:53:16 stevesk Exp $");
28
29 #include <sys/uio.h>
30
31 #include "log.h"
32 #include "monitor_fdpass.h"
33
34 void
35 mm_send_fd(int socket, int fd)
36 {
37 #if defined(HAVE_SENDMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
38         struct msghdr msg;
39         struct iovec vec;
40         char ch = '\0';
41         int n;
42 #if !defined(HAVE_ACCRIGHTS_IN_MSGHDR) || \
43         (defined(HAVE_ACCRIGHTS_IN_MSGHDR) && defined(HAVE_CONTROL_IN_MSGHDR))
44         char tmp[CMSG_SPACE(sizeof(int))];
45         struct cmsghdr *cmsg;
46 #endif
47
48         memset(&msg, 0, sizeof(msg));
49 #if defined(HAVE_ACCRIGHTS_IN_MSGHDR) && !defined(HAVE_CONTROL_IN_MSGHDR)
50         msg.msg_accrights = (caddr_t)&fd;
51         msg.msg_accrightslen = sizeof(fd);
52 #else
53         msg.msg_control = (caddr_t)tmp;
54         msg.msg_controllen = CMSG_LEN(sizeof(int));
55         cmsg = CMSG_FIRSTHDR(&msg);
56         cmsg->cmsg_len = CMSG_LEN(sizeof(int));
57         cmsg->cmsg_level = SOL_SOCKET;
58         cmsg->cmsg_type = SCM_RIGHTS;
59         *(int *)CMSG_DATA(cmsg) = fd;
60 #endif
61
62         vec.iov_base = &ch;
63         vec.iov_len = 1;
64         msg.msg_iov = &vec;
65         msg.msg_iovlen = 1;
66
67         if ((n = sendmsg(socket, &msg, 0)) == -1)
68                 fatal("%s: sendmsg(%d): %s", __FUNCTION__, fd,
69                     strerror(errno));
70         if (n != 1)
71                 fatal("%s: sendmsg: expected sent 1 got %d",
72                     __FUNCTION__, n);
73 #else
74         fatal("%s: UsePrivilegeSeparation=yes not supported",
75             __FUNCTION__);
76 #endif
77 }
78
79 int
80 mm_receive_fd(int socket)
81 {
82 #if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
83         struct msghdr msg;
84         struct iovec vec;
85         char ch;
86         int fd, n;
87 #if !defined(HAVE_ACCRIGHTS_IN_MSGHDR) || \
88         (defined(HAVE_ACCRIGHTS_IN_MSGHDR) && defined(HAVE_CONTROL_IN_MSGHDR))
89         char tmp[CMSG_SPACE(sizeof(int))];
90         struct cmsghdr *cmsg;
91 #endif
92
93         memset(&msg, 0, sizeof(msg));
94         vec.iov_base = &ch;
95         vec.iov_len = 1;
96         msg.msg_iov = &vec;
97         msg.msg_iovlen = 1;
98 #if defined(HAVE_ACCRIGHTS_IN_MSGHDR) && !defined(HAVE_CONTROL_IN_MSGHDR)
99         msg.msg_accrights = (caddr_t)&fd;
100         msg.msg_accrightslen = sizeof(fd);
101 #else
102         msg.msg_control = tmp;
103         msg.msg_controllen = sizeof(tmp);
104 #endif
105
106         if ((n = recvmsg(socket, &msg, 0)) == -1)
107                 fatal("%s: recvmsg: %s", __FUNCTION__, strerror(errno));
108         if (n != 1)
109                 fatal("%s: recvmsg: expected received 1 got %d",
110                     __FUNCTION__, n);
111
112 #if defined(HAVE_ACCRIGHTS_IN_MSGHDR) && !defined(HAVE_CONTROL_IN_MSGHDR)
113         if (msg.msg_accrightslen != sizeof(fd))
114                 fatal("%s: no fd", __FUNCTION__);
115 #else
116         cmsg = CMSG_FIRSTHDR(&msg);
117         if (cmsg->cmsg_type != SCM_RIGHTS)
118                 fatal("%s: expected type %d got %d", __FUNCTION__,
119                     SCM_RIGHTS, cmsg->cmsg_type);
120         fd = (*(int *)CMSG_DATA(cmsg));
121 #endif
122         return fd;
123 #else
124         fatal("%s: UsePrivilegeSeparation=yes not supported",
125             __FUNCTION__);
126 #endif
127 }
This page took 0.32086 seconds and 5 git commands to generate.