Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 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" |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 27 | RCSID("$OpenBSD: monitor_fdpass.c,v 1.3 2002/06/04 23:05:49 markus Exp $"); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 28 | |
| 29 | #include <sys/uio.h> |
| 30 | |
| 31 | #include "log.h" |
| 32 | #include "monitor_fdpass.h" |
| 33 | |
| 34 | void |
| 35 | mm_send_fd(int socket, int fd) |
| 36 | { |
Tim Rice | 66480f1 | 2002-04-15 21:10:09 -0700 | [diff] [blame] | 37 | #if defined(HAVE_SENDMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR)) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 38 | struct msghdr msg; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 39 | struct iovec vec; |
Ben Lindstrom | 31ee7ae | 2002-03-26 02:36:29 +0000 | [diff] [blame] | 40 | char ch = '\0'; |
| 41 | int n; |
Tim Rice | 28bbb0c | 2002-05-27 17:37:32 -0700 | [diff] [blame] | 42 | #ifndef HAVE_ACCRIGHTS_IN_MSGHDR |
Kevin Steves | 1adb120 | 2002-03-22 19:32:53 +0000 | [diff] [blame] | 43 | char tmp[CMSG_SPACE(sizeof(int))]; |
| 44 | struct cmsghdr *cmsg; |
| 45 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 46 | |
| 47 | memset(&msg, 0, sizeof(msg)); |
Tim Rice | 28bbb0c | 2002-05-27 17:37:32 -0700 | [diff] [blame] | 48 | #ifdef HAVE_ACCRIGHTS_IN_MSGHDR |
Kevin Steves | 1adb120 | 2002-03-22 19:32:53 +0000 | [diff] [blame] | 49 | msg.msg_accrights = (caddr_t)&fd; |
| 50 | msg.msg_accrightslen = sizeof(fd); |
| 51 | #else |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 52 | msg.msg_control = (caddr_t)tmp; |
| 53 | msg.msg_controllen = CMSG_LEN(sizeof(int)); |
| 54 | cmsg = CMSG_FIRSTHDR(&msg); |
| 55 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 56 | cmsg->cmsg_level = SOL_SOCKET; |
| 57 | cmsg->cmsg_type = SCM_RIGHTS; |
| 58 | *(int *)CMSG_DATA(cmsg) = fd; |
Kevin Steves | 1adb120 | 2002-03-22 19:32:53 +0000 | [diff] [blame] | 59 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 60 | |
| 61 | vec.iov_base = &ch; |
| 62 | vec.iov_len = 1; |
| 63 | msg.msg_iov = &vec; |
| 64 | msg.msg_iovlen = 1; |
| 65 | |
Ben Lindstrom | 31ee7ae | 2002-03-26 02:36:29 +0000 | [diff] [blame] | 66 | if ((n = sendmsg(socket, &msg, 0)) == -1) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 67 | fatal("%s: sendmsg(%d): %s", __func__, fd, |
Ben Lindstrom | 31ee7ae | 2002-03-26 02:36:29 +0000 | [diff] [blame] | 68 | strerror(errno)); |
| 69 | if (n != 1) |
| 70 | fatal("%s: sendmsg: expected sent 1 got %d", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 71 | __func__, n); |
Kevin Steves | c3c8255 | 2002-04-07 16:39:12 +0000 | [diff] [blame] | 72 | #else |
| 73 | fatal("%s: UsePrivilegeSeparation=yes not supported", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 74 | __func__); |
Kevin Steves | c3c8255 | 2002-04-07 16:39:12 +0000 | [diff] [blame] | 75 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int |
| 79 | mm_receive_fd(int socket) |
| 80 | { |
Tim Rice | 66480f1 | 2002-04-15 21:10:09 -0700 | [diff] [blame] | 81 | #if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR)) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 82 | struct msghdr msg; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 83 | struct iovec vec; |
| 84 | char ch; |
Ben Lindstrom | 31ee7ae | 2002-03-26 02:36:29 +0000 | [diff] [blame] | 85 | int fd, n; |
Tim Rice | 28bbb0c | 2002-05-27 17:37:32 -0700 | [diff] [blame] | 86 | #ifndef HAVE_ACCRIGHTS_IN_MSGHDR |
Kevin Steves | 1adb120 | 2002-03-22 19:32:53 +0000 | [diff] [blame] | 87 | char tmp[CMSG_SPACE(sizeof(int))]; |
| 88 | struct cmsghdr *cmsg; |
Kevin Steves | 1adb120 | 2002-03-22 19:32:53 +0000 | [diff] [blame] | 89 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 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; |
Tim Rice | 28bbb0c | 2002-05-27 17:37:32 -0700 | [diff] [blame] | 96 | #ifdef HAVE_ACCRIGHTS_IN_MSGHDR |
Kevin Steves | 1adb120 | 2002-03-22 19:32:53 +0000 | [diff] [blame] | 97 | msg.msg_accrights = (caddr_t)&fd; |
| 98 | msg.msg_accrightslen = sizeof(fd); |
| 99 | #else |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 100 | msg.msg_control = tmp; |
| 101 | msg.msg_controllen = sizeof(tmp); |
Kevin Steves | 1adb120 | 2002-03-22 19:32:53 +0000 | [diff] [blame] | 102 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 103 | |
Ben Lindstrom | 31ee7ae | 2002-03-26 02:36:29 +0000 | [diff] [blame] | 104 | if ((n = recvmsg(socket, &msg, 0)) == -1) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 105 | fatal("%s: recvmsg: %s", __func__, strerror(errno)); |
Ben Lindstrom | 31ee7ae | 2002-03-26 02:36:29 +0000 | [diff] [blame] | 106 | if (n != 1) |
| 107 | fatal("%s: recvmsg: expected received 1 got %d", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 108 | __func__, n); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 109 | |
Tim Rice | 28bbb0c | 2002-05-27 17:37:32 -0700 | [diff] [blame] | 110 | #ifdef HAVE_ACCRIGHTS_IN_MSGHDR |
Kevin Steves | 1adb120 | 2002-03-22 19:32:53 +0000 | [diff] [blame] | 111 | if (msg.msg_accrightslen != sizeof(fd)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 112 | fatal("%s: no fd", __func__); |
Kevin Steves | 1adb120 | 2002-03-22 19:32:53 +0000 | [diff] [blame] | 113 | #else |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 114 | cmsg = CMSG_FIRSTHDR(&msg); |
| 115 | if (cmsg->cmsg_type != SCM_RIGHTS) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 116 | fatal("%s: expected type %d got %d", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 117 | SCM_RIGHTS, cmsg->cmsg_type); |
Ben Lindstrom | 31ee7ae | 2002-03-26 02:36:29 +0000 | [diff] [blame] | 118 | fd = (*(int *)CMSG_DATA(cmsg)); |
Kevin Steves | 1adb120 | 2002-03-22 19:32:53 +0000 | [diff] [blame] | 119 | #endif |
Ben Lindstrom | 31ee7ae | 2002-03-26 02:36:29 +0000 | [diff] [blame] | 120 | return fd; |
Kevin Steves | c3c8255 | 2002-04-07 16:39:12 +0000 | [diff] [blame] | 121 | #else |
| 122 | fatal("%s: UsePrivilegeSeparation=yes not supported", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 123 | __func__); |
Kevin Steves | c3c8255 | 2002-04-07 16:39:12 +0000 | [diff] [blame] | 124 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 125 | } |