]>
Commit | Line | Data |
---|---|---|
1853d1ef | 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" | |
1853d1ef | 27 | |
28 | #include <sys/uio.h> | |
29 | ||
30 | #include "log.h" | |
31 | #include "monitor_fdpass.h" | |
32 | ||
33 | void | |
ca75d7de | 34 | mm_send_fd(int sock, int fd) |
1853d1ef | 35 | { |
1b06e75e | 36 | #if defined(HAVE_SENDMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR)) |
1853d1ef | 37 | struct msghdr msg; |
1853d1ef | 38 | struct iovec vec; |
d4355079 | 39 | char ch = '\0'; |
63b5f1a1 | 40 | ssize_t n; |
637f9177 | 41 | #ifndef HAVE_ACCRIGHTS_IN_MSGHDR |
dc90b259 | 42 | char tmp[CMSG_SPACE(sizeof(int))]; |
43 | struct cmsghdr *cmsg; | |
44 | #endif | |
1853d1ef | 45 | |
46 | memset(&msg, 0, sizeof(msg)); | |
637f9177 | 47 | #ifdef HAVE_ACCRIGHTS_IN_MSGHDR |
dc90b259 | 48 | msg.msg_accrights = (caddr_t)&fd; |
49 | msg.msg_accrightslen = sizeof(fd); | |
50 | #else | |
1853d1ef | 51 | msg.msg_control = (caddr_t)tmp; |
52 | msg.msg_controllen = CMSG_LEN(sizeof(int)); | |
53 | cmsg = CMSG_FIRSTHDR(&msg); | |
54 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); | |
55 | cmsg->cmsg_level = SOL_SOCKET; | |
56 | cmsg->cmsg_type = SCM_RIGHTS; | |
57 | *(int *)CMSG_DATA(cmsg) = fd; | |
dc90b259 | 58 | #endif |
1853d1ef | 59 | |
60 | vec.iov_base = &ch; | |
61 | vec.iov_len = 1; | |
62 | msg.msg_iov = &vec; | |
63 | msg.msg_iovlen = 1; | |
64 | ||
ca75d7de | 65 | if ((n = sendmsg(sock, &msg, 0)) == -1) |
1588c277 | 66 | fatal("%s: sendmsg(%d): %s", __func__, fd, |
d4355079 | 67 | strerror(errno)); |
68 | if (n != 1) | |
63b5f1a1 | 69 | fatal("%s: sendmsg: expected sent 1 got %ld", |
70 | __func__, (long)n); | |
84071420 | 71 | #else |
72 | fatal("%s: UsePrivilegeSeparation=yes not supported", | |
1588c277 | 73 | __func__); |
84071420 | 74 | #endif |
1853d1ef | 75 | } |
76 | ||
77 | int | |
ca75d7de | 78 | mm_receive_fd(int sock) |
1853d1ef | 79 | { |
1b06e75e | 80 | #if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR)) |
1853d1ef | 81 | struct msghdr msg; |
1853d1ef | 82 | struct iovec vec; |
63b5f1a1 | 83 | ssize_t n; |
1853d1ef | 84 | char ch; |
63b5f1a1 | 85 | int fd; |
637f9177 | 86 | #ifndef HAVE_ACCRIGHTS_IN_MSGHDR |
dc90b259 | 87 | char tmp[CMSG_SPACE(sizeof(int))]; |
88 | struct cmsghdr *cmsg; | |
dc90b259 | 89 | #endif |
1853d1ef | 90 | |
91 | memset(&msg, 0, sizeof(msg)); | |
92 | vec.iov_base = &ch; | |
93 | vec.iov_len = 1; | |
94 | msg.msg_iov = &vec; | |
95 | msg.msg_iovlen = 1; | |
637f9177 | 96 | #ifdef HAVE_ACCRIGHTS_IN_MSGHDR |
dc90b259 | 97 | msg.msg_accrights = (caddr_t)&fd; |
98 | msg.msg_accrightslen = sizeof(fd); | |
99 | #else | |
1853d1ef | 100 | msg.msg_control = tmp; |
101 | msg.msg_controllen = sizeof(tmp); | |
dc90b259 | 102 | #endif |
1853d1ef | 103 | |
ca75d7de | 104 | if ((n = recvmsg(sock, &msg, 0)) == -1) |
1588c277 | 105 | fatal("%s: recvmsg: %s", __func__, strerror(errno)); |
d4355079 | 106 | if (n != 1) |
63b5f1a1 | 107 | fatal("%s: recvmsg: expected received 1 got %ld", |
108 | __func__, (long)n); | |
1853d1ef | 109 | |
637f9177 | 110 | #ifdef HAVE_ACCRIGHTS_IN_MSGHDR |
dc90b259 | 111 | if (msg.msg_accrightslen != sizeof(fd)) |
1588c277 | 112 | fatal("%s: no fd", __func__); |
dc90b259 | 113 | #else |
1853d1ef | 114 | cmsg = CMSG_FIRSTHDR(&msg); |
fb3d6bd2 | 115 | if (cmsg == NULL) |
116 | fatal("%s: no message header", __func__); | |
bf7c1e6c | 117 | #ifndef BROKEN_CMSG_TYPE |
1853d1ef | 118 | if (cmsg->cmsg_type != SCM_RIGHTS) |
1588c277 | 119 | fatal("%s: expected type %d got %d", __func__, |
1853d1ef | 120 | SCM_RIGHTS, cmsg->cmsg_type); |
bf7c1e6c | 121 | #endif |
d4355079 | 122 | fd = (*(int *)CMSG_DATA(cmsg)); |
dc90b259 | 123 | #endif |
d4355079 | 124 | return fd; |
84071420 | 125 | #else |
126 | fatal("%s: UsePrivilegeSeparation=yes not supported", | |
1588c277 | 127 | __func__); |
84071420 | 128 | #endif |
1853d1ef | 129 | } |