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