blob: fb97171ff5f5647d2478d0c5daf20dcaa5bf5a32 [file] [log] [blame]
Ben Lindstrom7a2073c2002-03-22 02:30:41 +00001/*
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 Lindstrom31ee7ae2002-03-26 02:36:29 +000027RCSID("$OpenBSD: monitor_fdpass.c,v 1.2 2002/03/24 17:53:16 stevesk Exp $");
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000028
29#include <sys/uio.h>
30
31#include "log.h"
32#include "monitor_fdpass.h"
33
34void
35mm_send_fd(int socket, int fd)
36{
Tim Rice66480f12002-04-15 21:10:09 -070037#if defined(HAVE_SENDMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000038 struct msghdr msg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000039 struct iovec vec;
Ben Lindstrom31ee7ae2002-03-26 02:36:29 +000040 char ch = '\0';
41 int n;
Tim Rice802b9562002-05-11 15:30:04 -070042#if !defined(HAVE_ACCRIGHTS_IN_MSGHDR) || \
43 (defined(HAVE_ACCRIGHTS_IN_MSGHDR) && defined(HAVE_CONTROL_IN_MSGHDR))
Kevin Steves1adb1202002-03-22 19:32:53 +000044 char tmp[CMSG_SPACE(sizeof(int))];
45 struct cmsghdr *cmsg;
46#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000047
48 memset(&msg, 0, sizeof(msg));
Tim Rice802b9562002-05-11 15:30:04 -070049#if defined(HAVE_ACCRIGHTS_IN_MSGHDR) && !defined(HAVE_CONTROL_IN_MSGHDR)
Kevin Steves1adb1202002-03-22 19:32:53 +000050 msg.msg_accrights = (caddr_t)&fd;
51 msg.msg_accrightslen = sizeof(fd);
52#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000053 msg.msg_control = (caddr_t)tmp;
54 msg.msg_controllen = CMSG_LEN(sizeof(int));
55 cmsg = CMSG_FIRSTHDR(&msg);
56 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
57 cmsg->cmsg_level = SOL_SOCKET;
58 cmsg->cmsg_type = SCM_RIGHTS;
59 *(int *)CMSG_DATA(cmsg) = fd;
Kevin Steves1adb1202002-03-22 19:32:53 +000060#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000061
62 vec.iov_base = &ch;
63 vec.iov_len = 1;
64 msg.msg_iov = &vec;
65 msg.msg_iovlen = 1;
66
Ben Lindstrom31ee7ae2002-03-26 02:36:29 +000067 if ((n = sendmsg(socket, &msg, 0)) == -1)
68 fatal("%s: sendmsg(%d): %s", __FUNCTION__, fd,
69 strerror(errno));
70 if (n != 1)
71 fatal("%s: sendmsg: expected sent 1 got %d",
72 __FUNCTION__, n);
Kevin Stevesc3c82552002-04-07 16:39:12 +000073#else
74 fatal("%s: UsePrivilegeSeparation=yes not supported",
75 __FUNCTION__);
76#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000077}
78
79int
80mm_receive_fd(int socket)
81{
Tim Rice66480f12002-04-15 21:10:09 -070082#if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000083 struct msghdr msg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000084 struct iovec vec;
85 char ch;
Ben Lindstrom31ee7ae2002-03-26 02:36:29 +000086 int fd, n;
Tim Rice802b9562002-05-11 15:30:04 -070087#if !defined(HAVE_ACCRIGHTS_IN_MSGHDR) || \
88 (defined(HAVE_ACCRIGHTS_IN_MSGHDR) && defined(HAVE_CONTROL_IN_MSGHDR))
Kevin Steves1adb1202002-03-22 19:32:53 +000089 char tmp[CMSG_SPACE(sizeof(int))];
90 struct cmsghdr *cmsg;
Kevin Steves1adb1202002-03-22 19:32:53 +000091#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000092
93 memset(&msg, 0, sizeof(msg));
94 vec.iov_base = &ch;
95 vec.iov_len = 1;
96 msg.msg_iov = &vec;
97 msg.msg_iovlen = 1;
Tim Rice802b9562002-05-11 15:30:04 -070098#if defined(HAVE_ACCRIGHTS_IN_MSGHDR) && !defined(HAVE_CONTROL_IN_MSGHDR)
Kevin Steves1adb1202002-03-22 19:32:53 +000099 msg.msg_accrights = (caddr_t)&fd;
100 msg.msg_accrightslen = sizeof(fd);
101#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000102 msg.msg_control = tmp;
103 msg.msg_controllen = sizeof(tmp);
Kevin Steves1adb1202002-03-22 19:32:53 +0000104#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000105
Ben Lindstrom31ee7ae2002-03-26 02:36:29 +0000106 if ((n = recvmsg(socket, &msg, 0)) == -1)
107 fatal("%s: recvmsg: %s", __FUNCTION__, strerror(errno));
108 if (n != 1)
109 fatal("%s: recvmsg: expected received 1 got %d",
110 __FUNCTION__, n);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000111
Tim Rice802b9562002-05-11 15:30:04 -0700112#if defined(HAVE_ACCRIGHTS_IN_MSGHDR) && !defined(HAVE_CONTROL_IN_MSGHDR)
Kevin Steves1adb1202002-03-22 19:32:53 +0000113 if (msg.msg_accrightslen != sizeof(fd))
114 fatal("%s: no fd", __FUNCTION__);
Kevin Steves1adb1202002-03-22 19:32:53 +0000115#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000116 cmsg = CMSG_FIRSTHDR(&msg);
117 if (cmsg->cmsg_type != SCM_RIGHTS)
118 fatal("%s: expected type %d got %d", __FUNCTION__,
119 SCM_RIGHTS, cmsg->cmsg_type);
Ben Lindstrom31ee7ae2002-03-26 02:36:29 +0000120 fd = (*(int *)CMSG_DATA(cmsg));
Kevin Steves1adb1202002-03-22 19:32:53 +0000121#endif
Ben Lindstrom31ee7ae2002-03-26 02:36:29 +0000122 return fd;
Kevin Stevesc3c82552002-04-07 16:39:12 +0000123#else
124 fatal("%s: UsePrivilegeSeparation=yes not supported",
125 __FUNCTION__);
126#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000127}