1 /* $OpenBSD: privsep_fdpass.c,v 1.5 2008/03/24 16:11:08 deraadt Exp $ */
4 * Copyright 2001 Niels Provos <provos@citi.umich.edu>
7 * Copyright (c) 2002 Matthieu Herrb
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
50 send_fd(int sock, int fd)
55 char buf[CMSG_SPACE(sizeof(int))];
62 memset(&msg, 0, sizeof(msg));
65 msg.msg_control = (caddr_t)&cmsgbuf.buf;
66 msg.msg_controllen = sizeof(cmsgbuf.buf);
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;
76 vec.iov_base = &result;
77 vec.iov_len = sizeof(int);
81 if ((n = sendmsg(sock, &msg, 0)) == -1)
82 warn("%s: sendmsg(%d)", __func__, sock);
84 warnx("%s: sendmsg: expected sent 1 got %ld",
94 char buf[CMSG_SPACE(sizeof(int))];
102 memset(&msg, 0, sizeof(msg));
103 vec.iov_base = &result;
104 vec.iov_len = sizeof(int);
107 msg.msg_control = &cmsgbuf.buf;
108 msg.msg_controllen = sizeof(cmsgbuf.buf);
110 if ((n = recvmsg(sock, &msg, 0)) == -1)
111 warn("%s: recvmsg", __func__);
112 if (n != sizeof(int))
113 warnx("%s: recvmsg: expected received 1 got %ld",
116 cmsg = CMSG_FIRSTHDR(&msg);
118 warnx("%s: no message header", __func__);
121 if (cmsg->cmsg_type != SCM_RIGHTS)
122 warnx("%s: expected type %d got %d", __func__,
123 SCM_RIGHTS, cmsg->cmsg_type);
124 fd = (*(int *)CMSG_DATA(cmsg));