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