1 /* $OpenBSD: monitor_fdpass.c,v 1.23 2026/02/08 19:54:31 dtucker Exp $ */
2 /*
3 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "includes.h"
28
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/uio.h>
32 #include <sys/un.h>
33
34 #include <errno.h>
35 #include <poll.h>
36 #include <string.h>
37 #include <stdarg.h>
38
39 #include "log.h"
40 #include "monitor_fdpass.h"
41
42 int
mm_send_fd(int sock,int fd)43 mm_send_fd(int sock, int fd)
44 {
45 #if defined(HAVE_SENDMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
46 struct msghdr msg;
47 #ifndef HAVE_ACCRIGHTS_IN_MSGHDR
48 union {
49 struct cmsghdr hdr;
50 char buf[CMSG_SPACE(sizeof(int))];
51 } cmsgbuf;
52 struct cmsghdr *cmsg;
53 #endif
54 struct iovec vec;
55 char ch = '\0';
56 ssize_t n;
57 struct pollfd pfd;
58
59 memset(&msg, 0, sizeof(msg));
60 #ifdef HAVE_ACCRIGHTS_IN_MSGHDR
61 msg.msg_accrights = (caddr_t)&fd;
62 msg.msg_accrightslen = sizeof(fd);
63 #else
64 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
65 msg.msg_control = (caddr_t)&cmsgbuf.buf;
66 msg.msg_controllen = sizeof(cmsgbuf.buf);
67 cmsg = CMSG_FIRSTHDR(&msg);
68 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
69 cmsg->cmsg_level = SOL_SOCKET;
70 cmsg->cmsg_type = SCM_RIGHTS;
71 *(int *)CMSG_DATA(cmsg) = fd;
72 #endif
73
74 vec.iov_base = &ch;
75 vec.iov_len = 1;
76 msg.msg_iov = &vec;
77 msg.msg_iovlen = 1;
78
79 pfd.fd = sock;
80 pfd.events = POLLOUT;
81 while ((n = sendmsg(sock, &msg, 0)) == -1 &&
82 (errno == EAGAIN || errno == EINTR)) {
83 debug3_f("sendmsg(%d): %s", fd, strerror(errno));
84 (void)poll(&pfd, 1, -1);
85 }
86 if (n == -1) {
87 error_f("sendmsg(%d): %s", fd, strerror(errno));
88 return -1;
89 }
90
91 if (n != 1) {
92 error_f("sendmsg: expected sent 1 got %zd", n);
93 return -1;
94 }
95 return 0;
96 #else
97 error_f("file descriptor passing not supported");
98 return -1;
99 #endif
100 }
101
102 int
mm_receive_fd(int sock)103 mm_receive_fd(int sock)
104 {
105 #if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
106 struct msghdr msg;
107 #ifndef HAVE_ACCRIGHTS_IN_MSGHDR
108 union {
109 struct cmsghdr hdr;
110 char buf[CMSG_SPACE(sizeof(int))];
111 } cmsgbuf;
112 struct cmsghdr *cmsg;
113 #endif
114 struct iovec vec;
115 ssize_t n;
116 char ch;
117 int fd;
118 struct pollfd pfd;
119
120 memset(&msg, 0, sizeof(msg));
121 vec.iov_base = &ch;
122 vec.iov_len = 1;
123 msg.msg_iov = &vec;
124 msg.msg_iovlen = 1;
125 #ifdef HAVE_ACCRIGHTS_IN_MSGHDR
126 msg.msg_accrights = (caddr_t)&fd;
127 msg.msg_accrightslen = sizeof(fd);
128 #else
129 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
130 msg.msg_control = &cmsgbuf.buf;
131 msg.msg_controllen = sizeof(cmsgbuf.buf);
132 #endif
133
134 pfd.fd = sock;
135 pfd.events = POLLIN;
136 while ((n = recvmsg(sock, &msg, 0)) == -1 &&
137 (errno == EAGAIN || errno == EINTR)) {
138 debug3_f("recvmsg: %s", strerror(errno));
139 (void)poll(&pfd, 1, -1);
140 }
141 if (n == -1) {
142 error_f("recvmsg: %s", strerror(errno));
143 return -1;
144 }
145
146 if (n != 1) {
147 error_f("recvmsg: expected received 1 got %zd", n);
148 return -1;
149 }
150
151 #ifdef HAVE_ACCRIGHTS_IN_MSGHDR
152 if (msg.msg_accrightslen != sizeof(fd)) {
153 error_f("no fd");
154 return -1;
155 }
156 #else
157 cmsg = CMSG_FIRSTHDR(&msg);
158 if (cmsg == NULL) {
159 error_f("no message header");
160 return -1;
161 }
162
163 #ifndef BROKEN_CMSG_TYPE
164 if (cmsg->cmsg_type != SCM_RIGHTS) {
165 error_f("expected %d got %d", SCM_RIGHTS, cmsg->cmsg_type);
166 return -1;
167 }
168 #endif
169 fd = (*(int *)CMSG_DATA(cmsg));
170 #endif
171 return fd;
172 #else
173 error_f("file descriptor passing not supported");
174 return -1;
175 #endif
176 }
177