blob: 641ce721ef514b0fe85b900df5944258977a2d67 [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 Lindstromd5bf46e2002-06-27 00:21:03 +000027RCSID("$OpenBSD: monitor_fdpass.c,v 1.4 2002/06/26 14:50:04 deraadt 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';
Ben Lindstromd5bf46e2002-06-27 00:21:03 +000041 ssize_t n;
Tim Rice28bbb0c2002-05-27 17:37:32 -070042#ifndef HAVE_ACCRIGHTS_IN_MSGHDR
Kevin Steves1adb1202002-03-22 19:32:53 +000043 char tmp[CMSG_SPACE(sizeof(int))];
44 struct cmsghdr *cmsg;
45#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000046
47 memset(&msg, 0, sizeof(msg));
Tim Rice28bbb0c2002-05-27 17:37:32 -070048#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
Kevin Steves1adb1202002-03-22 19:32:53 +000049 msg.msg_accrights = (caddr_t)&fd;
50 msg.msg_accrightslen = sizeof(fd);
51#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000052 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 Steves1adb1202002-03-22 19:32:53 +000059#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000060
61 vec.iov_base = &ch;
62 vec.iov_len = 1;
63 msg.msg_iov = &vec;
64 msg.msg_iovlen = 1;
65
Ben Lindstrom31ee7ae2002-03-26 02:36:29 +000066 if ((n = sendmsg(socket, &msg, 0)) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000067 fatal("%s: sendmsg(%d): %s", __func__, fd,
Ben Lindstrom31ee7ae2002-03-26 02:36:29 +000068 strerror(errno));
69 if (n != 1)
Ben Lindstromd5bf46e2002-06-27 00:21:03 +000070 fatal("%s: sendmsg: expected sent 1 got %ld",
71 __func__, (long)n);
Kevin Stevesc3c82552002-04-07 16:39:12 +000072#else
73 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +000074 __func__);
Kevin Stevesc3c82552002-04-07 16:39:12 +000075#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000076}
77
78int
79mm_receive_fd(int socket)
80{
Tim Rice66480f12002-04-15 21:10:09 -070081#if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000082 struct msghdr msg;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000083 struct iovec vec;
Ben Lindstromd5bf46e2002-06-27 00:21:03 +000084 ssize_t n;
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000085 char ch;
Ben Lindstromd5bf46e2002-06-27 00:21:03 +000086 int fd;
Tim Rice28bbb0c2002-05-27 17:37:32 -070087#ifndef HAVE_ACCRIGHTS_IN_MSGHDR
Kevin Steves1adb1202002-03-22 19:32:53 +000088 char tmp[CMSG_SPACE(sizeof(int))];
89 struct cmsghdr *cmsg;
Kevin Steves1adb1202002-03-22 19:32:53 +000090#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +000091
92 memset(&msg, 0, sizeof(msg));
93 vec.iov_base = &ch;
94 vec.iov_len = 1;
95 msg.msg_iov = &vec;
96 msg.msg_iovlen = 1;
Tim Rice28bbb0c2002-05-27 17:37:32 -070097#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
Kevin Steves1adb1202002-03-22 19:32:53 +000098 msg.msg_accrights = (caddr_t)&fd;
99 msg.msg_accrightslen = sizeof(fd);
100#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000101 msg.msg_control = tmp;
102 msg.msg_controllen = sizeof(tmp);
Kevin Steves1adb1202002-03-22 19:32:53 +0000103#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000104
Ben Lindstrom31ee7ae2002-03-26 02:36:29 +0000105 if ((n = recvmsg(socket, &msg, 0)) == -1)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000106 fatal("%s: recvmsg: %s", __func__, strerror(errno));
Ben Lindstrom31ee7ae2002-03-26 02:36:29 +0000107 if (n != 1)
Ben Lindstromd5bf46e2002-06-27 00:21:03 +0000108 fatal("%s: recvmsg: expected received 1 got %ld",
109 __func__, (long)n);
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000110
Tim Rice28bbb0c2002-05-27 17:37:32 -0700111#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
Kevin Steves1adb1202002-03-22 19:32:53 +0000112 if (msg.msg_accrightslen != sizeof(fd))
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000113 fatal("%s: no fd", __func__);
Kevin Steves1adb1202002-03-22 19:32:53 +0000114#else
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000115 cmsg = CMSG_FIRSTHDR(&msg);
116 if (cmsg->cmsg_type != SCM_RIGHTS)
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000117 fatal("%s: expected type %d got %d", __func__,
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000118 SCM_RIGHTS, cmsg->cmsg_type);
Ben Lindstrom31ee7ae2002-03-26 02:36:29 +0000119 fd = (*(int *)CMSG_DATA(cmsg));
Kevin Steves1adb1202002-03-22 19:32:53 +0000120#endif
Ben Lindstrom31ee7ae2002-03-26 02:36:29 +0000121 return fd;
Kevin Stevesc3c82552002-04-07 16:39:12 +0000122#else
123 fatal("%s: UsePrivilegeSeparation=yes not supported",
Ben Lindstrom7d9c38f2002-06-06 21:40:51 +0000124 __func__);
Kevin Stevesc3c82552002-04-07 16:39:12 +0000125#endif
Ben Lindstrom7a2073c2002-03-22 02:30:41 +0000126}