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