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