186d7f5d3SJohn Marino /* $OpenBSD: privsep_fdpass.c,v 1.2 2004/08/13 02:51:48 djm Exp $ */
286d7f5d3SJohn Marino
386d7f5d3SJohn Marino /*
486d7f5d3SJohn Marino * Copyright 2001 Niels Provos <provos@citi.umich.edu>
586d7f5d3SJohn Marino * All rights reserved.
686d7f5d3SJohn Marino *
786d7f5d3SJohn Marino * Copyright (c) 2002 Matthieu Herrb
886d7f5d3SJohn Marino * All rights reserved.
986d7f5d3SJohn Marino *
1086d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
1186d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
1286d7f5d3SJohn Marino * are met:
1386d7f5d3SJohn Marino *
1486d7f5d3SJohn Marino * - Redistributions of source code must retain the above copyright
1586d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
1686d7f5d3SJohn Marino * - Redistributions in binary form must reproduce the above
1786d7f5d3SJohn Marino * copyright notice, this list of conditions and the following
1886d7f5d3SJohn Marino * disclaimer in the documentation and/or other materials provided
1986d7f5d3SJohn Marino * with the distribution.
2086d7f5d3SJohn Marino *
2186d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2286d7f5d3SJohn Marino * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2386d7f5d3SJohn Marino * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2486d7f5d3SJohn Marino * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2586d7f5d3SJohn Marino * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2686d7f5d3SJohn Marino * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2786d7f5d3SJohn Marino * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2886d7f5d3SJohn Marino * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
2986d7f5d3SJohn Marino * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3086d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
3186d7f5d3SJohn Marino * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3286d7f5d3SJohn Marino * POSSIBILITY OF SUCH DAMAGE.
3386d7f5d3SJohn Marino */
3486d7f5d3SJohn Marino #include <sys/param.h>
3586d7f5d3SJohn Marino #include <sys/uio.h>
3686d7f5d3SJohn Marino #include <sys/types.h>
3786d7f5d3SJohn Marino #include <sys/socket.h>
3886d7f5d3SJohn Marino #include <sys/stat.h>
3986d7f5d3SJohn Marino #include <err.h>
4086d7f5d3SJohn Marino #include <errno.h>
4186d7f5d3SJohn Marino #include <fcntl.h>
4286d7f5d3SJohn Marino #include <signal.h>
4386d7f5d3SJohn Marino #include <stdio.h>
4486d7f5d3SJohn Marino #include <stdlib.h>
4586d7f5d3SJohn Marino #include <string.h>
4686d7f5d3SJohn Marino #include <unistd.h>
4786d7f5d3SJohn Marino #include "pflogd.h"
4886d7f5d3SJohn Marino
4986d7f5d3SJohn Marino void
send_fd(int sock,int fd)5086d7f5d3SJohn Marino send_fd(int sock, int fd)
5186d7f5d3SJohn Marino {
5286d7f5d3SJohn Marino struct msghdr msg;
5386d7f5d3SJohn Marino char tmp[CMSG_SPACE(sizeof(int))];
5486d7f5d3SJohn Marino struct cmsghdr *cmsg;
5586d7f5d3SJohn Marino struct iovec vec;
5686d7f5d3SJohn Marino int result = 0;
5786d7f5d3SJohn Marino ssize_t n;
5886d7f5d3SJohn Marino
5986d7f5d3SJohn Marino memset(&msg, 0, sizeof(msg));
6086d7f5d3SJohn Marino
6186d7f5d3SJohn Marino if (fd >= 0) {
6286d7f5d3SJohn Marino msg.msg_control = (caddr_t)tmp;
6386d7f5d3SJohn Marino msg.msg_controllen = CMSG_LEN(sizeof(int));
6486d7f5d3SJohn Marino cmsg = CMSG_FIRSTHDR(&msg);
6586d7f5d3SJohn Marino cmsg->cmsg_len = CMSG_LEN(sizeof(int));
6686d7f5d3SJohn Marino cmsg->cmsg_level = SOL_SOCKET;
6786d7f5d3SJohn Marino cmsg->cmsg_type = SCM_RIGHTS;
6886d7f5d3SJohn Marino *(int *)CMSG_DATA(cmsg) = fd;
6986d7f5d3SJohn Marino } else {
7086d7f5d3SJohn Marino result = errno;
7186d7f5d3SJohn Marino }
7286d7f5d3SJohn Marino
7386d7f5d3SJohn Marino vec.iov_base = (caddr_t)&result;
7486d7f5d3SJohn Marino vec.iov_len = sizeof(int);
7586d7f5d3SJohn Marino msg.msg_iov = &vec;
7686d7f5d3SJohn Marino msg.msg_iovlen = 1;
7786d7f5d3SJohn Marino
7886d7f5d3SJohn Marino if ((n = sendmsg(sock, &msg, 0)) == -1)
7986d7f5d3SJohn Marino warn("%s: sendmsg(%d)", __func__, sock);
8086d7f5d3SJohn Marino if (n != sizeof(int))
8186d7f5d3SJohn Marino warnx("%s: sendmsg: expected sent 1 got %ld",
8286d7f5d3SJohn Marino __func__, (long)n);
8386d7f5d3SJohn Marino }
8486d7f5d3SJohn Marino
8586d7f5d3SJohn Marino int
receive_fd(int sock)8686d7f5d3SJohn Marino receive_fd(int sock)
8786d7f5d3SJohn Marino {
8886d7f5d3SJohn Marino struct msghdr msg;
8986d7f5d3SJohn Marino char tmp[CMSG_SPACE(sizeof(int))];
9086d7f5d3SJohn Marino struct cmsghdr *cmsg;
9186d7f5d3SJohn Marino struct iovec vec;
9286d7f5d3SJohn Marino ssize_t n;
9386d7f5d3SJohn Marino int result;
9486d7f5d3SJohn Marino int fd;
9586d7f5d3SJohn Marino
9686d7f5d3SJohn Marino memset(&msg, 0, sizeof(msg));
9786d7f5d3SJohn Marino vec.iov_base = (caddr_t)&result;
9886d7f5d3SJohn Marino vec.iov_len = sizeof(int);
9986d7f5d3SJohn Marino msg.msg_iov = &vec;
10086d7f5d3SJohn Marino msg.msg_iovlen = 1;
10186d7f5d3SJohn Marino msg.msg_control = tmp;
10286d7f5d3SJohn Marino msg.msg_controllen = sizeof(tmp);
10386d7f5d3SJohn Marino
10486d7f5d3SJohn Marino if ((n = recvmsg(sock, &msg, 0)) == -1)
10586d7f5d3SJohn Marino warn("%s: recvmsg", __func__);
10686d7f5d3SJohn Marino if (n != sizeof(int))
10786d7f5d3SJohn Marino warnx("%s: recvmsg: expected received 1 got %ld",
10886d7f5d3SJohn Marino __func__, (long)n);
10986d7f5d3SJohn Marino if (result == 0) {
11086d7f5d3SJohn Marino cmsg = CMSG_FIRSTHDR(&msg);
11186d7f5d3SJohn Marino if (cmsg == NULL) {
11286d7f5d3SJohn Marino warnx("%s: no message header", __func__);
11386d7f5d3SJohn Marino return -1;
11486d7f5d3SJohn Marino }
11586d7f5d3SJohn Marino if (cmsg->cmsg_type != SCM_RIGHTS)
11686d7f5d3SJohn Marino warnx("%s: expected type %d got %d", __func__,
11786d7f5d3SJohn Marino SCM_RIGHTS, cmsg->cmsg_type);
11886d7f5d3SJohn Marino fd = (*(int *)CMSG_DATA(cmsg));
11986d7f5d3SJohn Marino return fd;
12086d7f5d3SJohn Marino } else {
12186d7f5d3SJohn Marino errno = result;
12286d7f5d3SJohn Marino return -1;
12386d7f5d3SJohn Marino }
12486d7f5d3SJohn Marino }
125