unfdpass.c revision 1.8 1 /* $NetBSD: unfdpass.c,v 1.8 2006/05/10 19:10:09 mrg Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
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 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Test passing of file descriptors and credentials over Unix domain sockets.
42 */
43
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <sys/time.h>
47 #include <sys/wait.h>
48 #include <sys/un.h>
49 #include <sys/uio.h>
50
51 #include <err.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <signal.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <stdlib.h>
58 #include <unistd.h>
59
60 #define SOCK_NAME "test-sock"
61
62 int main(int, char *[]);
63 void child(void);
64 void catch_sigchld(int);
65 void usage(char *progname);
66
67 #define FILE_SIZE 128
68 #define MSG_SIZE -1
69 #define NFILES 24
70
71 #define FDCM_DATASIZE (sizeof(int) * NFILES)
72 #define CRCM_DATASIZE (SOCKCREDSIZE(NGROUPS))
73
74 #define MESSAGE_SIZE (CMSG_SPACE(FDCM_DATASIZE) + \
75 CMSG_SPACE(CRCM_DATASIZE))
76
77 int chroot_rcvr = 0;
78 int pass_dir = 0;
79 int pass_root_dir = 0;
80 int exit_early = 0;
81 int exit_later = 0;
82 int pass_sock = 0;
83 int make_pretzel = 0;
84
85 /* ARGSUSED */
86 int
87 main(argc, argv)
88 int argc;
89 char *argv[];
90 {
91 #if MSG_SIZE >= 0
92 struct iovec iov;
93 #endif
94 char *progname=argv[0];
95 struct msghdr msg;
96 int listensock, sock, fd, i;
97 char fname[16], buf[FILE_SIZE];
98 struct cmsghdr *cmp;
99 void *message;
100 int *files = NULL;
101 struct sockcred *sc = NULL;
102 struct sockaddr_un sun, csun;
103 socklen_t csunlen;
104 pid_t pid;
105 int ch;
106
107 message = malloc(CMSG_SPACE(MESSAGE_SIZE));
108 if (message == NULL)
109 err(1, "unable to malloc message buffer");
110 memset(message, 0, CMSG_SPACE(MESSAGE_SIZE));
111
112 while ((ch = getopt(argc, argv, "DESdepr")) != -1) {
113 switch(ch) {
114
115 case 'e':
116 exit_early++; /* test early GC */
117 break;
118
119 case 'E':
120 exit_later++; /* test later GC */
121 break;
122
123 case 'd':
124 pass_dir++;
125 break;
126
127 case 'D':
128 pass_dir++;
129 pass_root_dir++;
130 break;
131
132 case 'S':
133 pass_sock++;
134 break;
135
136 case 'r':
137 chroot_rcvr++;
138 break;
139
140 case 'p':
141 make_pretzel++;
142 break;
143
144 case '?':
145 default:
146 usage(progname);
147 }
148 }
149
150
151 /*
152 * Create the test files.
153 */
154 for (i = 0; i < NFILES; i++) {
155 (void) sprintf(fname, "file%d", i + 1);
156 if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
157 err(1, "open %s", fname);
158 (void) sprintf(buf, "This is file %d.\n", i + 1);
159 if (write(fd, buf, strlen(buf)) != strlen(buf))
160 err(1, "write %s", fname);
161 (void) close(fd);
162 }
163
164 /*
165 * Create the listen socket.
166 */
167 if ((listensock = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1)
168 err(1, "socket");
169
170 (void) unlink(SOCK_NAME);
171 (void) memset(&sun, 0, sizeof(sun));
172 sun.sun_family = AF_LOCAL;
173 (void) strcpy(sun.sun_path, SOCK_NAME);
174 sun.sun_len = SUN_LEN(&sun);
175
176 i = 1;
177 if (setsockopt(listensock, 0, LOCAL_CREDS, &i, sizeof(i)) == -1)
178 err(1, "setsockopt");
179
180 if (bind(listensock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
181 err(1, "bind");
182
183 if (listen(listensock, 1) == -1)
184 err(1, "listen");
185
186 /*
187 * Create the sender.
188 */
189 (void) signal(SIGCHLD, catch_sigchld);
190 pid = fork();
191 switch (pid) {
192 case -1:
193 err(1, "fork");
194 /* NOTREACHED */
195
196 case 0:
197 child();
198 /* NOTREACHED */
199 }
200
201 if (exit_early)
202 exit(0);
203
204 if (chroot_rcvr &&
205 ((chroot(".") < 0)))
206 err(1, "chroot");
207
208 /*
209 * Wait for the sender to connect.
210 */
211 if ((sock = accept(listensock, (struct sockaddr *)&csun,
212 &csunlen)) == -1)
213 err(1, "accept");
214
215 /*
216 * Give sender a chance to run. We will get going again
217 * once the SIGCHLD arrives.
218 */
219 (void) sleep(10);
220
221 if (exit_later)
222 exit(0);
223
224 /*
225 * Grab the descriptors and credentials passed to us.
226 */
227
228 /* Expect 2 messages; descriptors and creds. */
229 do {
230 (void) memset(&msg, 0, sizeof(msg));
231 msg.msg_control = message;
232 msg.msg_controllen = MESSAGE_SIZE;
233 #if MSG_SIZE >= 0
234 iov.iov_base = buf;
235 iov.iov_len = MSG_SIZE;
236 msg.msg_iov = &iov;
237 msg.msg_iovlen = 1;
238 #endif
239
240 if (recvmsg(sock, &msg, 0) == -1)
241 err(1, "recvmsg");
242
243 (void) close(sock);
244 sock = -1;
245
246 if (msg.msg_controllen == 0)
247 errx(1, "no control messages received");
248
249 if (msg.msg_flags & MSG_CTRUNC)
250 errx(1, "lost control message data");
251
252 for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL;
253 cmp = CMSG_NXTHDR(&msg, cmp)) {
254 if (cmp->cmsg_level != SOL_SOCKET)
255 errx(1, "bad control message level %d",
256 cmp->cmsg_level);
257
258 switch (cmp->cmsg_type) {
259 case SCM_RIGHTS:
260 if (cmp->cmsg_len != CMSG_LEN(FDCM_DATASIZE))
261 errx(1, "bad fd control message "
262 "length %d", cmp->cmsg_len);
263
264 files = (int *)CMSG_DATA(cmp);
265 break;
266
267 case SCM_CREDS:
268 if (cmp->cmsg_len < CMSG_LEN(SOCKCREDSIZE(1)))
269 errx(1, "bad cred control message "
270 "length %d", cmp->cmsg_len);
271
272 sc = (struct sockcred *)CMSG_DATA(cmp);
273 break;
274
275 default:
276 errx(1, "unexpected control message");
277 /* NOTREACHED */
278 }
279 }
280
281 /*
282 * Read the files and print their contents.
283 */
284 if (files == NULL)
285 warnx("didn't get fd control message");
286 else {
287 for (i = 0; i < NFILES; i++) {
288 struct stat st;
289 (void) memset(buf, 0, sizeof(buf));
290 fstat(files[i], &st);
291 if (S_ISDIR(st.st_mode)) {
292 printf("file %d is a directory\n", i+1);
293 } else if (S_ISSOCK(st.st_mode)) {
294 printf("file %d is a socket\n", i+1);
295 sock = files[i];
296 } else {
297 int c;
298 c = read (files[i], buf, sizeof(buf));
299 if (c < 0)
300 err(1, "read file %d", i + 1);
301 else if (c == 0)
302 printf("[eof on %d]\n", i + 1);
303 else
304 printf("%s", buf);
305 }
306 }
307 }
308 /*
309 * Double-check credentials.
310 */
311 if (sc == NULL)
312 warnx("didn't get cred control message");
313 else {
314 if (sc->sc_uid == getuid() &&
315 sc->sc_euid == geteuid() &&
316 sc->sc_gid == getgid() &&
317 sc->sc_egid == getegid())
318 printf("Credentials match.\n");
319 else
320 printf("Credentials do NOT match.\n");
321 }
322 } while (sock != -1);
323
324 /*
325 * All done!
326 */
327 exit(0);
328 }
329
330 void
331 usage(progname)
332 char *progname;
333 {
334 fprintf(stderr, "usage: %s [-derDES]\n", progname);
335 exit(1);
336 }
337
338 void
339 catch_sigchld(sig)
340 int sig;
341 {
342 int status;
343
344 (void) wait(&status);
345 }
346
347 void
348 child()
349 {
350 #if MSG_SIZE >= 0
351 struct iovec iov;
352 #endif
353 struct msghdr msg;
354 char fname[16];
355 struct cmsghdr *cmp;
356 void *fdcm;
357 int i, fd, sock, nfd, *files;
358 struct sockaddr_un sun;
359 int spair[2];
360
361 fdcm = malloc(CMSG_SPACE(FDCM_DATASIZE));
362 if (fdcm == NULL)
363 err(1, "unable to malloc fd control message");
364 memset(fdcm, 0, CMSG_SPACE(FDCM_DATASIZE));
365
366 cmp = fdcm;
367 files = (int *)CMSG_DATA(fdcm);
368
369 /*
370 * Create socket and connect to the receiver.
371 */
372 if ((sock = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1)
373 errx(1, "child socket");
374
375 (void) memset(&sun, 0, sizeof(sun));
376 sun.sun_family = AF_LOCAL;
377 (void) strcpy(sun.sun_path, SOCK_NAME);
378 sun.sun_len = SUN_LEN(&sun);
379
380 if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
381 err(1, "child connect");
382
383 nfd = NFILES;
384 i = 0;
385
386 if (pass_sock) {
387 files[i++] = sock;
388 }
389
390 if (pass_dir)
391 nfd--;
392
393 /*
394 * Open the files again, and pass them to the child
395 * over the socket.
396 */
397
398 for (; i < nfd; i++) {
399 (void) sprintf(fname, "file%d", i + 1);
400 if ((fd = open(fname, O_RDONLY, 0666)) == -1)
401 err(1, "child open %s", fname);
402 files[i] = fd;
403 }
404
405 if (pass_dir) {
406 char *dirname = pass_root_dir ? "/" : ".";
407
408
409 if ((fd = open(dirname, O_RDONLY, 0)) == -1) {
410 err(1, "child open directory %s", dirname);
411 }
412 files[i] = fd;
413 }
414
415 (void) memset(&msg, 0, sizeof(msg));
416 msg.msg_control = fdcm;
417 msg.msg_controllen = CMSG_LEN(FDCM_DATASIZE);
418 #if MSG_SIZE >= 0
419 iov.iov_base = buf;
420 iov.iov_len = MSG_SIZE;
421 msg.msg_iov = &iov;
422 msg.msg_iovlen = 1;
423 #endif
424
425 cmp = CMSG_FIRSTHDR(&msg);
426 cmp->cmsg_len = CMSG_LEN(FDCM_DATASIZE);
427 cmp->cmsg_level = SOL_SOCKET;
428 cmp->cmsg_type = SCM_RIGHTS;
429
430 while (make_pretzel > 0) {
431 if (socketpair(PF_LOCAL, SOCK_STREAM, 0, spair) < 0)
432 err(1, "socketpair");
433
434 printf("send pretzel\n");
435 if (sendmsg(spair[0], &msg, 0) < 0)
436 err(1, "child prezel sendmsg");
437
438 close(files[0]);
439 close(files[1]);
440 files[0] = spair[0];
441 files[1] = spair[1];
442 make_pretzel--;
443 }
444
445 if (sendmsg(sock, &msg, 0) == -1)
446 err(1, "child sendmsg");
447
448 /*
449 * All done!
450 */
451 exit(0);
452 }
453