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