privsep_fdpass.c revision 145838
1130561Sobrien/* $OpenBSD: privsep_fdpass.c,v 1.2 2004/08/13 02:51:48 djm Exp $ */ 2130561Sobrien 3130561Sobrien/* 4130561Sobrien * Copyright 2001 Niels Provos <provos@citi.umich.edu> 5130561Sobrien * All rights reserved. 6130561Sobrien * 7130561Sobrien * Copyright (c) 2002 Matthieu Herrb 8130561Sobrien * All rights reserved. 9130561Sobrien * 10130561Sobrien * Redistribution and use in source and binary forms, with or without 11130561Sobrien * modification, are permitted provided that the following conditions 12130561Sobrien * are met: 13130561Sobrien * 14130561Sobrien * - Redistributions of source code must retain the above copyright 15130561Sobrien * notice, this list of conditions and the following disclaimer. 16130561Sobrien * - Redistributions in binary form must reproduce the above 17130561Sobrien * copyright notice, this list of conditions and the following 18130561Sobrien * disclaimer in the documentation and/or other materials provided 19130561Sobrien * with the distribution. 20130561Sobrien * 21130561Sobrien * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22130561Sobrien * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23130561Sobrien * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24130561Sobrien * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25130561Sobrien * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26130561Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27130561Sobrien * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28130561Sobrien * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29130561Sobrien * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30130561Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31130561Sobrien * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32130561Sobrien * POSSIBILITY OF SUCH DAMAGE. 33130561Sobrien */ 34130561Sobrien#include <sys/param.h> 35130561Sobrien#include <sys/uio.h> 36130561Sobrien#include <sys/types.h> 37130561Sobrien#include <sys/socket.h> 38130561Sobrien#include <sys/stat.h> 39130561Sobrien#include <err.h> 40130561Sobrien#include <errno.h> 41130561Sobrien#include <fcntl.h> 42130561Sobrien#include <signal.h> 43130561Sobrien#include <stdio.h> 44130561Sobrien#include <stdlib.h> 45130561Sobrien#include <string.h> 46130561Sobrien#include <unistd.h> 47130561Sobrien#include "pflogd.h" 48130561Sobrien 49130561Sobrienvoid 50130561Sobriensend_fd(int sock, int fd) 51130561Sobrien{ 52130561Sobrien struct msghdr msg; 53130561Sobrien char tmp[CMSG_SPACE(sizeof(int))]; 54130561Sobrien struct cmsghdr *cmsg; 55130561Sobrien struct iovec vec; 56130561Sobrien int result = 0; 57130561Sobrien ssize_t n; 58130561Sobrien 59130561Sobrien memset(&msg, 0, sizeof(msg)); 60130561Sobrien 61130561Sobrien if (fd >= 0) { 62130561Sobrien msg.msg_control = (caddr_t)tmp; 63130561Sobrien msg.msg_controllen = CMSG_LEN(sizeof(int)); 64130561Sobrien cmsg = CMSG_FIRSTHDR(&msg); 65130561Sobrien cmsg->cmsg_len = CMSG_LEN(sizeof(int)); 66130561Sobrien cmsg->cmsg_level = SOL_SOCKET; 67130561Sobrien cmsg->cmsg_type = SCM_RIGHTS; 68130561Sobrien *(int *)CMSG_DATA(cmsg) = fd; 69130561Sobrien } else { 70130561Sobrien result = errno; 71130561Sobrien } 72130561Sobrien 73130561Sobrien vec.iov_base = &result; 74130561Sobrien vec.iov_len = sizeof(int); 75130561Sobrien msg.msg_iov = &vec; 76130561Sobrien msg.msg_iovlen = 1; 77130561Sobrien 78130561Sobrien if ((n = sendmsg(sock, &msg, 0)) == -1) 79130561Sobrien warn("%s: sendmsg(%d)", __func__, sock); 80130561Sobrien if (n != sizeof(int)) 81130561Sobrien warnx("%s: sendmsg: expected sent 1 got %ld", 82130561Sobrien __func__, (long)n); 83130561Sobrien} 84130561Sobrien 85130561Sobrienint 86130561Sobrienreceive_fd(int sock) 87130561Sobrien{ 88130561Sobrien struct msghdr msg; 89130561Sobrien char tmp[CMSG_SPACE(sizeof(int))]; 90130561Sobrien struct cmsghdr *cmsg; 91130561Sobrien struct iovec vec; 92130561Sobrien ssize_t n; 93130561Sobrien int result; 94130561Sobrien int fd; 95130561Sobrien 96130561Sobrien memset(&msg, 0, sizeof(msg)); 97130561Sobrien vec.iov_base = &result; 98130561Sobrien vec.iov_len = sizeof(int); 99130561Sobrien msg.msg_iov = &vec; 100130561Sobrien msg.msg_iovlen = 1; 101130561Sobrien msg.msg_control = tmp; 102130561Sobrien msg.msg_controllen = sizeof(tmp); 103130561Sobrien 104130561Sobrien if ((n = recvmsg(sock, &msg, 0)) == -1) 105130561Sobrien warn("%s: recvmsg", __func__); 106130561Sobrien if (n != sizeof(int)) 107130561Sobrien warnx("%s: recvmsg: expected received 1 got %ld", 108130561Sobrien __func__, (long)n); 109130561Sobrien if (result == 0) { 110130561Sobrien cmsg = CMSG_FIRSTHDR(&msg); 111130561Sobrien if (cmsg == NULL) { 112130561Sobrien warnx("%s: no message header", __func__); 113130561Sobrien return -1; 114130561Sobrien } 115130561Sobrien if (cmsg->cmsg_type != SCM_RIGHTS) 116130561Sobrien warnx("%s: expected type %d got %d", __func__, 117130561Sobrien SCM_RIGHTS, cmsg->cmsg_type); 118130561Sobrien fd = (*(int *)CMSG_DATA(cmsg)); 119130561Sobrien return fd; 120130561Sobrien } else { 121130561Sobrien errno = result; 122130561Sobrien return -1; 123130561Sobrien } 124130561Sobrien} 125130561Sobrien