1 /*- 2 * Copyright (c) 2005 Andrey Simonenko 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 #include <sys/un.h> 31 #include <inttypes.h> 32 #include <stdarg.h> 33 #include <stdbool.h> 34 #include <stdlib.h> 35 36 #include "uc_common.h" 37 #include "t_generic.h" 38 #include "t_sockcred.h" 39 40 static int 41 t_sockcred_client(int type, int fd) 42 { 43 struct msghdr msghdr; 44 struct iovec iov[1]; 45 int rv; 46 47 if (uc_sync_recv() < 0) 48 return (-2); 49 50 rv = -2; 51 52 uc_msghdr_init_client(&msghdr, iov, NULL, 0, 0, 0); 53 54 if (uc_socket_connect(fd) < 0) 55 goto done; 56 57 if (type == 2) 58 if (uc_sync_recv() < 0) 59 goto done; 60 61 if (uc_message_sendn(fd, &msghdr) < 0) 62 goto done; 63 64 rv = 0; 65 done: 66 return (rv); 67 } 68 69 static int 70 t_sockcred_server(int type, int fd1) 71 { 72 struct msghdr msghdr; 73 struct iovec iov[1]; 74 struct cmsghdr *cmsghdr; 75 void *cmsg_data; 76 size_t cmsg_size; 77 u_int i; 78 int fd2, rv, val; 79 80 fd2 = -1; 81 rv = -2; 82 83 cmsg_size = CMSG_SPACE(SOCKCREDSIZE(uc_cfg.proc_cred.gid_num)); 84 cmsg_data = malloc(cmsg_size); 85 if (cmsg_data == NULL) { 86 uc_logmsg("malloc"); 87 goto done; 88 } 89 90 if (type == 1) { 91 uc_dbgmsg("setting LOCAL_CREDS"); 92 val = 1; 93 if (setsockopt(fd1, 0, LOCAL_CREDS, &val, sizeof(val)) < 0) { 94 uc_logmsg("setsockopt(LOCAL_CREDS)"); 95 goto done; 96 } 97 } 98 99 if (uc_sync_send() < 0) 100 goto done; 101 102 if (uc_cfg.sock_type == SOCK_STREAM) { 103 fd2 = uc_socket_accept(fd1); 104 if (fd2 < 0) 105 goto done; 106 } else 107 fd2 = fd1; 108 109 if (type == 2) { 110 uc_dbgmsg("setting LOCAL_CREDS"); 111 val = 1; 112 if (setsockopt(fd2, 0, LOCAL_CREDS, &val, sizeof(val)) < 0) { 113 uc_logmsg("setsockopt(LOCAL_CREDS)"); 114 goto done; 115 } 116 if (uc_sync_send() < 0) 117 goto done; 118 } 119 120 rv = -1; 121 for (i = 1; i <= uc_cfg.ipc_msg.msg_num; ++i) { 122 uc_dbgmsg("message #%u", i); 123 124 uc_msghdr_init_server(&msghdr, iov, cmsg_data, cmsg_size); 125 if (uc_message_recv(fd2, &msghdr) < 0) { 126 rv = -2; 127 break; 128 } 129 130 if (i > 1 && uc_cfg.sock_type == SOCK_STREAM) { 131 if (uc_check_msghdr(&msghdr, 0) < 0) 132 break; 133 } else { 134 if (uc_check_msghdr(&msghdr, sizeof(*cmsghdr)) < 0) 135 break; 136 137 cmsghdr = CMSG_FIRSTHDR(&msghdr); 138 if (uc_check_scm_creds_sockcred(cmsghdr) < 0) 139 break; 140 } 141 } 142 if (i > uc_cfg.ipc_msg.msg_num) 143 rv = 0; 144 done: 145 free(cmsg_data); 146 if (uc_cfg.sock_type == SOCK_STREAM && fd2 >= 0) 147 if (uc_socket_close(fd2) < 0) 148 rv = -2; 149 return (rv); 150 } 151 152 int 153 t_sockcred_1(void) 154 { 155 u_int i; 156 int fd, rv, rv_client; 157 158 switch (uc_client_fork()) { 159 case 0: 160 for (i = 1; i <= 2; ++i) { 161 uc_dbgmsg("client #%u", i); 162 fd = uc_socket_create(); 163 if (fd < 0) 164 rv = -2; 165 else { 166 rv = t_sockcred_client(1, fd); 167 if (uc_socket_close(fd) < 0) 168 rv = -2; 169 } 170 if (rv != 0) 171 break; 172 } 173 uc_client_exit(rv); 174 break; 175 case 1: 176 fd = uc_socket_create(); 177 if (fd < 0) 178 rv = -2; 179 else { 180 rv = t_sockcred_server(1, fd); 181 if (rv == 0) 182 rv = t_sockcred_server(3, fd); 183 rv_client = uc_client_wait(); 184 if (rv == 0 || (rv == -2 && rv_client != 0)) 185 rv = rv_client; 186 if (uc_socket_close(fd) < 0) 187 rv = -2; 188 } 189 break; 190 default: 191 rv = -2; 192 } 193 194 return (rv); 195 } 196 197 static int 198 t_sockcred_2_client(int fd) 199 { 200 return (t_sockcred_client(2, fd)); 201 } 202 203 static int 204 t_sockcred_2_server(int fd) 205 { 206 return (t_sockcred_server(2, fd)); 207 } 208 209 int 210 t_sockcred_2(void) 211 { 212 return (t_generic(t_sockcred_2_client, t_sockcred_2_server)); 213 } 214