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