Annotation of src/usr.bin/ssh/monitor_fdpass.c, Revision 1.7
1.1 provos 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:
28: #include <sys/uio.h>
29:
30: #include "log.h"
31: #include "monitor_fdpass.h"
32:
33: void
1.5 avsm 34: mm_send_fd(int sock, int fd)
1.1 provos 35: {
36: struct msghdr msg;
37: char tmp[CMSG_SPACE(sizeof(int))];
38: struct cmsghdr *cmsg;
39: struct iovec vec;
1.2 stevesk 40: char ch = '\0';
1.4 deraadt 41: ssize_t n;
1.1 provos 42:
43: memset(&msg, 0, sizeof(msg));
44: msg.msg_control = (caddr_t)tmp;
45: msg.msg_controllen = CMSG_LEN(sizeof(int));
46: cmsg = CMSG_FIRSTHDR(&msg);
47: cmsg->cmsg_len = CMSG_LEN(sizeof(int));
48: cmsg->cmsg_level = SOL_SOCKET;
49: cmsg->cmsg_type = SCM_RIGHTS;
50: *(int *)CMSG_DATA(cmsg) = fd;
51:
52: vec.iov_base = &ch;
53: vec.iov_len = 1;
54: msg.msg_iov = &vec;
55: msg.msg_iovlen = 1;
56:
1.5 avsm 57: if ((n = sendmsg(sock, &msg, 0)) == -1)
1.3 markus 58: fatal("%s: sendmsg(%d): %s", __func__, fd,
1.2 stevesk 59: strerror(errno));
60: if (n != 1)
1.4 deraadt 61: fatal("%s: sendmsg: expected sent 1 got %ld",
62: __func__, (long)n);
1.1 provos 63: }
64:
65: int
1.5 avsm 66: mm_receive_fd(int sock)
1.1 provos 67: {
68: struct msghdr msg;
69: char tmp[CMSG_SPACE(sizeof(int))];
70: struct cmsghdr *cmsg;
71: struct iovec vec;
1.4 deraadt 72: ssize_t n;
1.1 provos 73: char ch;
1.4 deraadt 74: int fd;
1.1 provos 75:
76: memset(&msg, 0, sizeof(msg));
77: vec.iov_base = &ch;
78: vec.iov_len = 1;
79: msg.msg_iov = &vec;
80: msg.msg_iovlen = 1;
81: msg.msg_control = tmp;
82: msg.msg_controllen = sizeof(tmp);
83:
1.5 avsm 84: if ((n = recvmsg(sock, &msg, 0)) == -1)
1.3 markus 85: fatal("%s: recvmsg: %s", __func__, strerror(errno));
1.2 stevesk 86: if (n != 1)
1.4 deraadt 87: fatal("%s: recvmsg: expected received 1 got %ld",
88: __func__, (long)n);
1.1 provos 89:
90: cmsg = CMSG_FIRSTHDR(&msg);
1.6 djm 91: if (cmsg == NULL)
92: fatal("%s: no message header", __func__);
1.1 provos 93: if (cmsg->cmsg_type != SCM_RIGHTS)
1.3 markus 94: fatal("%s: expected type %d got %d", __func__,
1.1 provos 95: SCM_RIGHTS, cmsg->cmsg_type);
1.2 stevesk 96: fd = (*(int *)CMSG_DATA(cmsg));
97: return fd;
1.1 provos 98: }