]> andersk Git - openssh.git/blame - monitor_fdpass.c
- (djm) Add back radix.o (used by AFS support), after it went missing from
[openssh.git] / monitor_fdpass.c
CommitLineData
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"
63b5f1a1 27RCSID("$OpenBSD: monitor_fdpass.c,v 1.4 2002/06/26 14:50:04 deraadt Exp $");
1853d1ef 28
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{
1b06e75e 37#if defined(HAVE_SENDMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
1853d1ef 38 struct msghdr msg;
1853d1ef 39 struct iovec vec;
d4355079 40 char ch = '\0';
63b5f1a1 41 ssize_t n;
637f9177 42#ifndef HAVE_ACCRIGHTS_IN_MSGHDR
dc90b259 43 char tmp[CMSG_SPACE(sizeof(int))];
44 struct cmsghdr *cmsg;
45#endif
1853d1ef 46
47 memset(&msg, 0, sizeof(msg));
637f9177 48#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
dc90b259 49 msg.msg_accrights = (caddr_t)&fd;
50 msg.msg_accrightslen = sizeof(fd);
51#else
1853d1ef 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;
dc90b259 59#endif
1853d1ef 60
61 vec.iov_base = &ch;
62 vec.iov_len = 1;
63 msg.msg_iov = &vec;
64 msg.msg_iovlen = 1;
65
d4355079 66 if ((n = sendmsg(socket, &msg, 0)) == -1)
1588c277 67 fatal("%s: sendmsg(%d): %s", __func__, fd,
d4355079 68 strerror(errno));
69 if (n != 1)
63b5f1a1 70 fatal("%s: sendmsg: expected sent 1 got %ld",
71 __func__, (long)n);
84071420 72#else
73 fatal("%s: UsePrivilegeSeparation=yes not supported",
1588c277 74 __func__);
84071420 75#endif
1853d1ef 76}
77
78int
79mm_receive_fd(int socket)
80{
1b06e75e 81#if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
1853d1ef 82 struct msghdr msg;
1853d1ef 83 struct iovec vec;
63b5f1a1 84 ssize_t n;
1853d1ef 85 char ch;
63b5f1a1 86 int fd;
637f9177 87#ifndef HAVE_ACCRIGHTS_IN_MSGHDR
dc90b259 88 char tmp[CMSG_SPACE(sizeof(int))];
89 struct cmsghdr *cmsg;
dc90b259 90#endif
1853d1ef 91
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;
637f9177 97#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
dc90b259 98 msg.msg_accrights = (caddr_t)&fd;
99 msg.msg_accrightslen = sizeof(fd);
100#else
1853d1ef 101 msg.msg_control = tmp;
102 msg.msg_controllen = sizeof(tmp);
dc90b259 103#endif
1853d1ef 104
d4355079 105 if ((n = recvmsg(socket, &msg, 0)) == -1)
1588c277 106 fatal("%s: recvmsg: %s", __func__, strerror(errno));
d4355079 107 if (n != 1)
63b5f1a1 108 fatal("%s: recvmsg: expected received 1 got %ld",
109 __func__, (long)n);
1853d1ef 110
637f9177 111#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
dc90b259 112 if (msg.msg_accrightslen != sizeof(fd))
1588c277 113 fatal("%s: no fd", __func__);
dc90b259 114#else
1853d1ef 115 cmsg = CMSG_FIRSTHDR(&msg);
116 if (cmsg->cmsg_type != SCM_RIGHTS)
1588c277 117 fatal("%s: expected type %d got %d", __func__,
1853d1ef 118 SCM_RIGHTS, cmsg->cmsg_type);
d4355079 119 fd = (*(int *)CMSG_DATA(cmsg));
dc90b259 120#endif
d4355079 121 return fd;
84071420 122#else
123 fatal("%s: UsePrivilegeSeparation=yes not supported",
1588c277 124 __func__);
84071420 125#endif
1853d1ef 126}
This page took 0.258112 seconds and 5 git commands to generate.