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/types.h>
28 #include <sys/socket.h>
29 #include <sys/un.h>
30 #include <inttypes.h>
31 #include <stdarg.h>
32 #include <stdbool.h>
33 #include <stdlib.h>
34
35 #include "uc_common.h"
36 #include "t_generic.h"
37 #include "t_sockcred.h"
38
39 static int
t_sockcred_client(int type,int fd)40 t_sockcred_client(int type, int fd)
41 {
42 struct msghdr msghdr;
43 struct iovec iov[1];
44 int rv;
45
46 if (uc_sync_recv() < 0)
47 return (-2);
48
49 rv = -2;
50
51 uc_msghdr_init_client(&msghdr, iov, NULL, 0, 0, 0);
52
53 if (uc_socket_connect(fd) < 0)
54 goto done;
55
56 if (type == 2)
57 if (uc_sync_recv() < 0)
58 goto done;
59
60 if (uc_message_sendn(fd, &msghdr) < 0)
61 goto done;
62
63 rv = 0;
64 done:
65 return (rv);
66 }
67
68 static int
t_sockcred_server(int type,int fd1)69 t_sockcred_server(int type, int fd1)
70 {
71 struct msghdr msghdr;
72 struct iovec iov[1];
73 struct cmsghdr *cmsghdr;
74 void *cmsg_data;
75 size_t cmsg_size;
76 u_int i;
77 int fd2, rv, val;
78
79 fd2 = -1;
80 rv = -2;
81
82 cmsg_size = CMSG_SPACE(SOCKCREDSIZE(uc_cfg.proc_cred.gid_num));
83 cmsg_data = malloc(cmsg_size);
84 if (cmsg_data == NULL) {
85 uc_logmsg("malloc");
86 goto done;
87 }
88
89 if (type == 1) {
90 uc_dbgmsg("setting LOCAL_CREDS");
91 val = 1;
92 if (setsockopt(fd1, 0, LOCAL_CREDS, &val, sizeof(val)) < 0) {
93 uc_logmsg("setsockopt(LOCAL_CREDS)");
94 goto done;
95 }
96 }
97
98 if (uc_sync_send() < 0)
99 goto done;
100
101 if (uc_cfg.sock_type == SOCK_STREAM) {
102 fd2 = uc_socket_accept(fd1);
103 if (fd2 < 0)
104 goto done;
105 } else
106 fd2 = fd1;
107
108 if (type == 2) {
109 uc_dbgmsg("setting LOCAL_CREDS");
110 val = 1;
111 if (setsockopt(fd2, 0, LOCAL_CREDS, &val, sizeof(val)) < 0) {
112 uc_logmsg("setsockopt(LOCAL_CREDS)");
113 goto done;
114 }
115 if (uc_sync_send() < 0)
116 goto done;
117 }
118
119 rv = -1;
120 for (i = 1; i <= uc_cfg.ipc_msg.msg_num; ++i) {
121 uc_dbgmsg("message #%u", i);
122
123 uc_msghdr_init_server(&msghdr, iov, cmsg_data, cmsg_size);
124 if (uc_message_recv(fd2, &msghdr) < 0) {
125 rv = -2;
126 break;
127 }
128
129 if (i > 1 && uc_cfg.sock_type == SOCK_STREAM) {
130 if (uc_check_msghdr(&msghdr, 0) < 0)
131 break;
132 } else {
133 if (uc_check_msghdr(&msghdr, sizeof(*cmsghdr)) < 0)
134 break;
135
136 cmsghdr = CMSG_FIRSTHDR(&msghdr);
137 if (uc_check_scm_creds_sockcred(cmsghdr) < 0)
138 break;
139 }
140 }
141 if (i > uc_cfg.ipc_msg.msg_num)
142 rv = 0;
143 done:
144 free(cmsg_data);
145 if (uc_cfg.sock_type == SOCK_STREAM && fd2 >= 0)
146 if (uc_socket_close(fd2) < 0)
147 rv = -2;
148 return (rv);
149 }
150
151 int
t_sockcred_1(void)152 t_sockcred_1(void)
153 {
154 u_int i;
155 int fd, rv, rv_client;
156
157 switch (uc_client_fork()) {
158 case 0:
159 for (i = 1; i <= 2; ++i) {
160 uc_dbgmsg("client #%u", i);
161 fd = uc_socket_create();
162 if (fd < 0)
163 rv = -2;
164 else {
165 rv = t_sockcred_client(1, fd);
166 if (uc_socket_close(fd) < 0)
167 rv = -2;
168 }
169 if (rv != 0)
170 break;
171 }
172 uc_client_exit(rv);
173 break;
174 case 1:
175 fd = uc_socket_create();
176 if (fd < 0)
177 rv = -2;
178 else {
179 rv = t_sockcred_server(1, fd);
180 if (rv == 0)
181 rv = t_sockcred_server(3, fd);
182 rv_client = uc_client_wait();
183 if (rv == 0 || (rv == -2 && rv_client != 0))
184 rv = rv_client;
185 if (uc_socket_close(fd) < 0)
186 rv = -2;
187 }
188 break;
189 default:
190 rv = -2;
191 }
192
193 return (rv);
194 }
195
196 static int
t_sockcred_2_client(int fd)197 t_sockcred_2_client(int fd)
198 {
199 return (t_sockcred_client(2, fd));
200 }
201
202 static int
t_sockcred_2_server(int fd)203 t_sockcred_2_server(int fd)
204 {
205 return (t_sockcred_server(2, fd));
206 }
207
208 int
t_sockcred_2(void)209 t_sockcred_2(void)
210 {
211 return (t_generic(t_sockcred_2_client, t_sockcred_2_server));
212 }
213