]> andersk Git - openssh.git/blame - monitor_fdpass.c
- deraadt@cvs.openbsd.org 2008/03/15 16:19:02
[openssh.git] / monitor_fdpass.c
CommitLineData
93cdda57 1/* $OpenBSD: monitor_fdpass.c,v 1.16 2008/03/15 16:19:02 deraadt 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>
8153fef1 32#ifdef HAVE_SYS_UN_H
33#include <sys/un.h>
34#endif
1853d1ef 35
028094f4 36#include <errno.h>
00146caa 37#include <string.h>
31652869 38#include <stdarg.h>
028094f4 39
1853d1ef 40#include "log.h"
41#include "monitor_fdpass.h"
42
8de7aaab 43int
ca75d7de 44mm_send_fd(int sock, int fd)
1853d1ef 45{
1b06e75e 46#if defined(HAVE_SENDMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
1853d1ef 47 struct msghdr msg;
1853d1ef 48 struct iovec vec;
d4355079 49 char ch = '\0';
63b5f1a1 50 ssize_t n;
637f9177 51#ifndef HAVE_ACCRIGHTS_IN_MSGHDR
0c9a63f1 52 union {
53 struct cmsghdr hdr;
54 char tmp[CMSG_SPACE(sizeof(int))];
d1978bb4 55 char buf[CMSG_SPACE(sizeof(int))];
56 } cmsgbuf;
dc90b259 57 struct cmsghdr *cmsg;
58#endif
1853d1ef 59
60 memset(&msg, 0, sizeof(msg));
637f9177 61#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
dc90b259 62 msg.msg_accrights = (caddr_t)&fd;
63 msg.msg_accrightslen = sizeof(fd);
64#else
d1978bb4 65 msg.msg_control = (caddr_t)&cmsgbuf.buf;
93cdda57 66 msg.msg_controllen = CMSG_LEN(sizeof(int));
1853d1ef 67 cmsg = CMSG_FIRSTHDR(&msg);
68 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
69 cmsg->cmsg_level = SOL_SOCKET;
70 cmsg->cmsg_type = SCM_RIGHTS;
71 *(int *)CMSG_DATA(cmsg) = fd;
dc90b259 72#endif
1853d1ef 73
74 vec.iov_base = &ch;
75 vec.iov_len = 1;
76 msg.msg_iov = &vec;
77 msg.msg_iovlen = 1;
78
8de7aaab 79 if ((n = sendmsg(sock, &msg, 0)) == -1) {
80 error("%s: sendmsg(%d): %s", __func__, fd,
d4355079 81 strerror(errno));
8de7aaab 82 return -1;
83 }
84
85 if (n != 1) {
86 error("%s: sendmsg: expected sent 1 got %ld",
63b5f1a1 87 __func__, (long)n);
8de7aaab 88 return -1;
89 }
90 return 0;
84071420 91#else
8de7aaab 92 error("%s: file descriptor passing not supported", __func__);
93 return -1;
84071420 94#endif
1853d1ef 95}
96
97int
ca75d7de 98mm_receive_fd(int sock)
1853d1ef 99{
1b06e75e 100#if defined(HAVE_RECVMSG) && (defined(HAVE_ACCRIGHTS_IN_MSGHDR) || defined(HAVE_CONTROL_IN_MSGHDR))
1853d1ef 101 struct msghdr msg;
1853d1ef 102 struct iovec vec;
63b5f1a1 103 ssize_t n;
1853d1ef 104 char ch;
63b5f1a1 105 int fd;
637f9177 106#ifndef HAVE_ACCRIGHTS_IN_MSGHDR
0c9a63f1 107 union {
0c9a63f1 108 struct cmsghdr hdr;
d1978bb4 109 char buf[CMSG_SPACE(sizeof(int))];
110 } cmsgbuf;
dc90b259 111 struct cmsghdr *cmsg;
dc90b259 112#endif
1853d1ef 113
114 memset(&msg, 0, sizeof(msg));
115 vec.iov_base = &ch;
116 vec.iov_len = 1;
117 msg.msg_iov = &vec;
118 msg.msg_iovlen = 1;
637f9177 119#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
dc90b259 120 msg.msg_accrights = (caddr_t)&fd;
121 msg.msg_accrightslen = sizeof(fd);
122#else
d1978bb4 123 msg.msg_control = &cmsgbuf.buf;
93cdda57 124 msg.msg_controllen = CMSG_LEN(sizeof(int));
dc90b259 125#endif
1853d1ef 126
8de7aaab 127 if ((n = recvmsg(sock, &msg, 0)) == -1) {
128 error("%s: recvmsg: %s", __func__, strerror(errno));
129 return -1;
130 }
131 if (n != 1) {
132 error("%s: recvmsg: expected received 1 got %ld",
63b5f1a1 133 __func__, (long)n);
8de7aaab 134 return -1;
135 }
1853d1ef 136
637f9177 137#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
8de7aaab 138 if (msg.msg_accrightslen != sizeof(fd)) {
139 error("%s: no fd", __func__);
140 return -1;
141 }
dc90b259 142#else
1853d1ef 143 cmsg = CMSG_FIRSTHDR(&msg);
8de7aaab 144 if (cmsg == NULL) {
145 error("%s: no message header", __func__);
146 return -1;
147 }
bf7c1e6c 148#ifndef BROKEN_CMSG_TYPE
8de7aaab 149 if (cmsg->cmsg_type != SCM_RIGHTS) {
150 error("%s: expected type %d got %d", __func__,
1853d1ef 151 SCM_RIGHTS, cmsg->cmsg_type);
8de7aaab 152 return -1;
153 }
bf7c1e6c 154#endif
d4355079 155 fd = (*(int *)CMSG_DATA(cmsg));
dc90b259 156#endif
d4355079 157 return fd;
84071420 158#else
8de7aaab 159 error("%s: file descriptor passing not supported", __func__);
160 return -1;
84071420 161#endif
1853d1ef 162}
This page took 0.204408 seconds and 5 git commands to generate.