1*86d7f5d3SJohn Marino /* $OpenBSD: privsep_fdpass.c,v 1.2 2004/08/13 02:51:48 djm Exp $ */
2*86d7f5d3SJohn Marino
3*86d7f5d3SJohn Marino /*
4*86d7f5d3SJohn Marino * Copyright 2001 Niels Provos <provos@citi.umich.edu>
5*86d7f5d3SJohn Marino * All rights reserved.
6*86d7f5d3SJohn Marino *
7*86d7f5d3SJohn Marino * Copyright (c) 2002 Matthieu Herrb
8*86d7f5d3SJohn Marino * All rights reserved.
9*86d7f5d3SJohn Marino *
10*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
11*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
12*86d7f5d3SJohn Marino * are met:
13*86d7f5d3SJohn Marino *
14*86d7f5d3SJohn Marino * - Redistributions of source code must retain the above copyright
15*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
16*86d7f5d3SJohn Marino * - Redistributions in binary form must reproduce the above
17*86d7f5d3SJohn Marino * copyright notice, this list of conditions and the following
18*86d7f5d3SJohn Marino * disclaimer in the documentation and/or other materials provided
19*86d7f5d3SJohn Marino * with the distribution.
20*86d7f5d3SJohn Marino *
21*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*86d7f5d3SJohn Marino * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*86d7f5d3SJohn Marino * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*86d7f5d3SJohn Marino * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*86d7f5d3SJohn Marino * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*86d7f5d3SJohn Marino * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*86d7f5d3SJohn Marino * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28*86d7f5d3SJohn Marino * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29*86d7f5d3SJohn Marino * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*86d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31*86d7f5d3SJohn Marino * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32*86d7f5d3SJohn Marino * POSSIBILITY OF SUCH DAMAGE.
33*86d7f5d3SJohn Marino */
34*86d7f5d3SJohn Marino #include <sys/param.h>
35*86d7f5d3SJohn Marino #include <sys/uio.h>
36*86d7f5d3SJohn Marino #include <sys/types.h>
37*86d7f5d3SJohn Marino #include <sys/socket.h>
38*86d7f5d3SJohn Marino #include <sys/stat.h>
39*86d7f5d3SJohn Marino #include <err.h>
40*86d7f5d3SJohn Marino #include <errno.h>
41*86d7f5d3SJohn Marino #include <fcntl.h>
42*86d7f5d3SJohn Marino #include <signal.h>
43*86d7f5d3SJohn Marino #include <stdio.h>
44*86d7f5d3SJohn Marino #include <stdlib.h>
45*86d7f5d3SJohn Marino #include <string.h>
46*86d7f5d3SJohn Marino #include <unistd.h>
47*86d7f5d3SJohn Marino #include "pflogd.h"
48*86d7f5d3SJohn Marino
49*86d7f5d3SJohn Marino void
send_fd(int sock,int fd)50*86d7f5d3SJohn Marino send_fd(int sock, int fd)
51*86d7f5d3SJohn Marino {
52*86d7f5d3SJohn Marino struct msghdr msg;
53*86d7f5d3SJohn Marino char tmp[CMSG_SPACE(sizeof(int))];
54*86d7f5d3SJohn Marino struct cmsghdr *cmsg;
55*86d7f5d3SJohn Marino struct iovec vec;
56*86d7f5d3SJohn Marino int result = 0;
57*86d7f5d3SJohn Marino ssize_t n;
58*86d7f5d3SJohn Marino
59*86d7f5d3SJohn Marino memset(&msg, 0, sizeof(msg));
60*86d7f5d3SJohn Marino
61*86d7f5d3SJohn Marino if (fd >= 0) {
62*86d7f5d3SJohn Marino msg.msg_control = (caddr_t)tmp;
63*86d7f5d3SJohn Marino msg.msg_controllen = CMSG_LEN(sizeof(int));
64*86d7f5d3SJohn Marino cmsg = CMSG_FIRSTHDR(&msg);
65*86d7f5d3SJohn Marino cmsg->cmsg_len = CMSG_LEN(sizeof(int));
66*86d7f5d3SJohn Marino cmsg->cmsg_level = SOL_SOCKET;
67*86d7f5d3SJohn Marino cmsg->cmsg_type = SCM_RIGHTS;
68*86d7f5d3SJohn Marino *(int *)CMSG_DATA(cmsg) = fd;
69*86d7f5d3SJohn Marino } else {
70*86d7f5d3SJohn Marino result = errno;
71*86d7f5d3SJohn Marino }
72*86d7f5d3SJohn Marino
73*86d7f5d3SJohn Marino vec.iov_base = (caddr_t)&result;
74*86d7f5d3SJohn Marino vec.iov_len = sizeof(int);
75*86d7f5d3SJohn Marino msg.msg_iov = &vec;
76*86d7f5d3SJohn Marino msg.msg_iovlen = 1;
77*86d7f5d3SJohn Marino
78*86d7f5d3SJohn Marino if ((n = sendmsg(sock, &msg, 0)) == -1)
79*86d7f5d3SJohn Marino warn("%s: sendmsg(%d)", __func__, sock);
80*86d7f5d3SJohn Marino if (n != sizeof(int))
81*86d7f5d3SJohn Marino warnx("%s: sendmsg: expected sent 1 got %ld",
82*86d7f5d3SJohn Marino __func__, (long)n);
83*86d7f5d3SJohn Marino }
84*86d7f5d3SJohn Marino
85*86d7f5d3SJohn Marino int
receive_fd(int sock)86*86d7f5d3SJohn Marino receive_fd(int sock)
87*86d7f5d3SJohn Marino {
88*86d7f5d3SJohn Marino struct msghdr msg;
89*86d7f5d3SJohn Marino char tmp[CMSG_SPACE(sizeof(int))];
90*86d7f5d3SJohn Marino struct cmsghdr *cmsg;
91*86d7f5d3SJohn Marino struct iovec vec;
92*86d7f5d3SJohn Marino ssize_t n;
93*86d7f5d3SJohn Marino int result;
94*86d7f5d3SJohn Marino int fd;
95*86d7f5d3SJohn Marino
96*86d7f5d3SJohn Marino memset(&msg, 0, sizeof(msg));
97*86d7f5d3SJohn Marino vec.iov_base = (caddr_t)&result;
98*86d7f5d3SJohn Marino vec.iov_len = sizeof(int);
99*86d7f5d3SJohn Marino msg.msg_iov = &vec;
100*86d7f5d3SJohn Marino msg.msg_iovlen = 1;
101*86d7f5d3SJohn Marino msg.msg_control = tmp;
102*86d7f5d3SJohn Marino msg.msg_controllen = sizeof(tmp);
103*86d7f5d3SJohn Marino
104*86d7f5d3SJohn Marino if ((n = recvmsg(sock, &msg, 0)) == -1)
105*86d7f5d3SJohn Marino warn("%s: recvmsg", __func__);
106*86d7f5d3SJohn Marino if (n != sizeof(int))
107*86d7f5d3SJohn Marino warnx("%s: recvmsg: expected received 1 got %ld",
108*86d7f5d3SJohn Marino __func__, (long)n);
109*86d7f5d3SJohn Marino if (result == 0) {
110*86d7f5d3SJohn Marino cmsg = CMSG_FIRSTHDR(&msg);
111*86d7f5d3SJohn Marino if (cmsg == NULL) {
112*86d7f5d3SJohn Marino warnx("%s: no message header", __func__);
113*86d7f5d3SJohn Marino return -1;
114*86d7f5d3SJohn Marino }
115*86d7f5d3SJohn Marino if (cmsg->cmsg_type != SCM_RIGHTS)
116*86d7f5d3SJohn Marino warnx("%s: expected type %d got %d", __func__,
117*86d7f5d3SJohn Marino SCM_RIGHTS, cmsg->cmsg_type);
118*86d7f5d3SJohn Marino fd = (*(int *)CMSG_DATA(cmsg));
119*86d7f5d3SJohn Marino return fd;
120*86d7f5d3SJohn Marino } else {
121*86d7f5d3SJohn Marino errno = result;
122*86d7f5d3SJohn Marino return -1;
123*86d7f5d3SJohn Marino }
124*86d7f5d3SJohn Marino }
125