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