1 /* $OpenBSD: monitor_fdpass.c,v 1.18 2008/11/30 11:59:26 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 <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/uio.h>
30
31 #include <errno.h>
32 #include <string.h>
33 #include <stdarg.h>
34
35 #include "log.h"
36 #include "monitor_fdpass.h"
37
38 __RCSID("$MirOS: src/usr.bin/ssh/monitor_fdpass.c,v 1.4 2009/10/04 16:35:25 tg Exp $");
39
40 int
mm_send_fd(int sock,int fd)41 mm_send_fd(int sock, int fd)
42 {
43 struct msghdr msg;
44 union {
45 struct cmsghdr hdr;
46 char buf[CMSG_SPACE(sizeof(int))];
47 } cmsgbuf;
48 struct cmsghdr *cmsg;
49 struct iovec vec;
50 char ch = '\0';
51 ssize_t n;
52
53 memset(&msg, 0, sizeof(msg));
54 msg.msg_control = (caddr_t)&cmsgbuf.buf;
55 msg.msg_controllen = CMSG_LEN(sizeof(int));
56 cmsg = CMSG_FIRSTHDR(&msg);
57 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
58 cmsg->cmsg_level = SOL_SOCKET;
59 cmsg->cmsg_type = SCM_RIGHTS;
60 memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
61
62 vec.iov_base = &ch;
63 vec.iov_len = 1;
64 msg.msg_iov = &vec;
65 msg.msg_iovlen = 1;
66
67 while ((n = sendmsg(sock, &msg, 0)) == -1 && (errno == EAGAIN ||
68 errno == EINTR))
69 debug3("%s: sendmsg(%d): %s", __func__, fd, strerror(errno));
70 if (n == -1) {
71 error("%s: sendmsg(%d): %s", __func__, fd,
72 strerror(errno));
73 return -1;
74 }
75
76 if (n != 1) {
77 error("%s: sendmsg: expected sent 1 got %ld",
78 __func__, (long)n);
79 return -1;
80 }
81 return 0;
82 }
83
84 int
mm_receive_fd(int sock)85 mm_receive_fd(int sock)
86 {
87 struct msghdr msg;
88 union {
89 struct cmsghdr hdr;
90 char buf[CMSG_SPACE(sizeof(int))];
91 } cmsgbuf;
92 struct cmsghdr *cmsg;
93 struct iovec vec;
94 ssize_t n;
95 char ch;
96 int fd;
97
98 memset(&msg, 0, sizeof(msg));
99 vec.iov_base = &ch;
100 vec.iov_len = 1;
101 msg.msg_iov = &vec;
102 msg.msg_iovlen = 1;
103 msg.msg_control = &cmsgbuf.buf;
104 msg.msg_controllen = CMSG_LEN(sizeof(int));
105
106 while ((n = recvmsg(sock, &msg, 0)) == -1 && (errno == EAGAIN ||
107 errno == EINTR))
108 debug3("%s: recvmsg: %s", __func__, strerror(errno));
109 if (n == -1) {
110 error("%s: recvmsg: %s", __func__, strerror(errno));
111 return -1;
112 }
113
114 if (n != 1) {
115 error("%s: recvmsg: expected received 1 got %ld",
116 __func__, (long)n);
117 return -1;
118 }
119
120 cmsg = CMSG_FIRSTHDR(&msg);
121 if (cmsg == NULL) {
122 error("%s: no message header", __func__);
123 return -1;
124 }
125
126 if (cmsg->cmsg_type != SCM_RIGHTS) {
127 error("%s: expected type %d got %d", __func__,
128 SCM_RIGHTS, cmsg->cmsg_type);
129 return -1;
130 }
131 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
132 return fd;
133 }
134