unfdpass.c revision 1.6 1 1.6 thorpej /* $NetBSD: unfdpass.c,v 1.6 2000/06/05 06:01:42 thorpej 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.6 thorpej #include <stdlib.h>
58 1.1 thorpej #include <unistd.h>
59 1.1 thorpej
60 1.2 thorpej #define SOCK_NAME "test-sock"
61 1.1 thorpej
62 1.1 thorpej int main __P((int, char *[]));
63 1.1 thorpej void child __P((void));
64 1.2 thorpej void catch_sigchld __P((int));
65 1.5 sommerfe void usage __P((char *progname));
66 1.1 thorpej
67 1.4 mycroft #define FILE_SIZE 128
68 1.4 mycroft #define MSG_SIZE -1
69 1.4 mycroft #define NFILES 24
70 1.4 mycroft
71 1.6 thorpej #define FDCM_DATASIZE (sizeof(int) * NFILES)
72 1.6 thorpej #define CRCM_DATASIZE (SOCKCREDSIZE(NGROUPS))
73 1.6 thorpej
74 1.6 thorpej #define MESSAGE_SIZE (CMSG_SPACE(FDCM_DATASIZE) + \
75 1.6 thorpej CMSG_SPACE(CRCM_DATASIZE))
76 1.2 thorpej
77 1.5 sommerfe int chroot_rcvr = 0;
78 1.5 sommerfe int pass_dir = 0;
79 1.5 sommerfe int pass_root_dir = 0;
80 1.5 sommerfe int exit_early = 0;
81 1.5 sommerfe int exit_later = 0;
82 1.5 sommerfe int pass_sock = 0;
83 1.5 sommerfe int make_pretzel = 0;
84 1.5 sommerfe
85 1.1 thorpej /* ARGSUSED */
86 1.1 thorpej int
87 1.1 thorpej main(argc, argv)
88 1.1 thorpej int argc;
89 1.1 thorpej char *argv[];
90 1.1 thorpej {
91 1.4 mycroft #if MSG_SIZE >= 0
92 1.4 mycroft struct iovec iov;
93 1.4 mycroft #endif
94 1.5 sommerfe char *progname=argv[0];
95 1.1 thorpej struct msghdr msg;
96 1.6 thorpej int listensock, sock, fd, i;
97 1.4 mycroft char fname[16], buf[FILE_SIZE];
98 1.2 thorpej struct cmsghdr *cmp;
99 1.6 thorpej void *message;
100 1.2 thorpej int *files = NULL;
101 1.2 thorpej struct sockcred *sc = NULL;
102 1.2 thorpej struct sockaddr_un sun, csun;
103 1.2 thorpej int csunlen;
104 1.1 thorpej pid_t pid;
105 1.5 sommerfe int ch;
106 1.6 thorpej
107 1.6 thorpej message = malloc(CMSG_SPACE(MESSAGE_SIZE));
108 1.6 thorpej if (message == NULL)
109 1.6 thorpej err(1, "unable to malloc message buffer");
110 1.6 thorpej memset(message, 0, CMSG_SPACE(MESSAGE_SIZE));
111 1.5 sommerfe
112 1.5 sommerfe while ((ch = getopt(argc, argv, "DESdepr")) != -1) {
113 1.5 sommerfe switch(ch) {
114 1.5 sommerfe
115 1.5 sommerfe case 'e':
116 1.5 sommerfe exit_early++; /* test early GC */
117 1.5 sommerfe break;
118 1.5 sommerfe
119 1.5 sommerfe case 'E':
120 1.5 sommerfe exit_later++; /* test later GC */
121 1.5 sommerfe break;
122 1.5 sommerfe
123 1.5 sommerfe case 'd':
124 1.5 sommerfe pass_dir++;
125 1.5 sommerfe break;
126 1.5 sommerfe
127 1.5 sommerfe case 'D':
128 1.5 sommerfe pass_dir++;
129 1.5 sommerfe pass_root_dir++;
130 1.5 sommerfe break;
131 1.5 sommerfe
132 1.5 sommerfe case 'S':
133 1.5 sommerfe pass_sock++;
134 1.5 sommerfe break;
135 1.5 sommerfe
136 1.5 sommerfe case 'r':
137 1.5 sommerfe chroot_rcvr++;
138 1.5 sommerfe break;
139 1.5 sommerfe
140 1.5 sommerfe case 'p':
141 1.5 sommerfe make_pretzel++;
142 1.5 sommerfe break;
143 1.5 sommerfe
144 1.5 sommerfe case '?':
145 1.5 sommerfe default:
146 1.5 sommerfe usage(progname);
147 1.5 sommerfe }
148 1.5 sommerfe }
149 1.5 sommerfe
150 1.1 thorpej
151 1.1 thorpej /*
152 1.1 thorpej * Create the test files.
153 1.1 thorpej */
154 1.4 mycroft for (i = 0; i < NFILES; i++) {
155 1.1 thorpej (void) sprintf(fname, "file%d", i + 1);
156 1.1 thorpej if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
157 1.1 thorpej err(1, "open %s", fname);
158 1.1 thorpej (void) sprintf(buf, "This is file %d.\n", i + 1);
159 1.1 thorpej if (write(fd, buf, strlen(buf)) != strlen(buf))
160 1.1 thorpej err(1, "write %s", fname);
161 1.1 thorpej (void) close(fd);
162 1.1 thorpej }
163 1.1 thorpej
164 1.1 thorpej /*
165 1.2 thorpej * Create the listen socket.
166 1.1 thorpej */
167 1.3 thorpej if ((listensock = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1)
168 1.2 thorpej err(1, "socket");
169 1.2 thorpej
170 1.2 thorpej (void) unlink(SOCK_NAME);
171 1.2 thorpej (void) memset(&sun, 0, sizeof(sun));
172 1.2 thorpej sun.sun_family = AF_LOCAL;
173 1.2 thorpej (void) strcpy(sun.sun_path, SOCK_NAME);
174 1.2 thorpej sun.sun_len = SUN_LEN(&sun);
175 1.2 thorpej
176 1.2 thorpej i = 1;
177 1.2 thorpej if (setsockopt(listensock, 0, LOCAL_CREDS, &i, sizeof(i)) == -1)
178 1.2 thorpej err(1, "setsockopt");
179 1.2 thorpej
180 1.2 thorpej if (bind(listensock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
181 1.2 thorpej err(1, "bind");
182 1.2 thorpej
183 1.2 thorpej if (listen(listensock, 1) == -1)
184 1.2 thorpej err(1, "listen");
185 1.1 thorpej
186 1.2 thorpej /*
187 1.2 thorpej * Create the sender.
188 1.2 thorpej */
189 1.2 thorpej (void) signal(SIGCHLD, catch_sigchld);
190 1.1 thorpej pid = fork();
191 1.1 thorpej switch (pid) {
192 1.1 thorpej case -1:
193 1.1 thorpej err(1, "fork");
194 1.1 thorpej /* NOTREACHED */
195 1.1 thorpej
196 1.1 thorpej case 0:
197 1.1 thorpej child();
198 1.1 thorpej /* NOTREACHED */
199 1.1 thorpej }
200 1.1 thorpej
201 1.5 sommerfe if (exit_early)
202 1.5 sommerfe exit(0);
203 1.5 sommerfe
204 1.5 sommerfe if (chroot_rcvr &&
205 1.5 sommerfe ((chroot(".") < 0)))
206 1.5 sommerfe err(1, "chroot");
207 1.5 sommerfe
208 1.2 thorpej /*
209 1.2 thorpej * Wait for the sender to connect.
210 1.2 thorpej */
211 1.2 thorpej if ((sock = accept(listensock, (struct sockaddr *)&csun,
212 1.2 thorpej &csunlen)) == -1)
213 1.2 thorpej err(1, "accept");
214 1.1 thorpej
215 1.1 thorpej /*
216 1.2 thorpej * Give sender a chance to run. We will get going again
217 1.2 thorpej * once the SIGCHLD arrives.
218 1.1 thorpej */
219 1.2 thorpej (void) sleep(10);
220 1.1 thorpej
221 1.5 sommerfe if (exit_later)
222 1.5 sommerfe exit(0);
223 1.5 sommerfe
224 1.2 thorpej /*
225 1.2 thorpej * Grab the descriptors and credentials passed to us.
226 1.2 thorpej */
227 1.4 mycroft
228 1.6 thorpej /* Expect 2 messages; descriptors and creds. */
229 1.5 sommerfe do {
230 1.5 sommerfe (void) memset(&msg, 0, sizeof(msg));
231 1.6 thorpej msg.msg_control = message;
232 1.6 thorpej msg.msg_controllen = MESSAGE_SIZE;
233 1.4 mycroft #if MSG_SIZE >= 0
234 1.5 sommerfe iov.iov_base = buf;
235 1.5 sommerfe iov.iov_len = MSG_SIZE;
236 1.5 sommerfe msg.msg_iov = &iov;
237 1.5 sommerfe msg.msg_iovlen = 1;
238 1.4 mycroft #endif
239 1.2 thorpej
240 1.5 sommerfe if (recvmsg(sock, &msg, 0) == -1)
241 1.5 sommerfe err(1, "recvmsg");
242 1.5 sommerfe
243 1.5 sommerfe (void) close(sock);
244 1.5 sommerfe sock = -1;
245 1.2 thorpej
246 1.5 sommerfe if (msg.msg_controllen == 0)
247 1.5 sommerfe errx(1, "no control messages received");
248 1.2 thorpej
249 1.5 sommerfe if (msg.msg_flags & MSG_CTRUNC)
250 1.5 sommerfe errx(1, "lost control message data");
251 1.2 thorpej
252 1.5 sommerfe for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL;
253 1.5 sommerfe cmp = CMSG_NXTHDR(&msg, cmp)) {
254 1.5 sommerfe if (cmp->cmsg_level != SOL_SOCKET)
255 1.5 sommerfe errx(1, "bad control message level %d",
256 1.5 sommerfe cmp->cmsg_level);
257 1.1 thorpej
258 1.5 sommerfe switch (cmp->cmsg_type) {
259 1.5 sommerfe case SCM_RIGHTS:
260 1.6 thorpej if (cmp->cmsg_len != CMSG_LEN(FDCM_DATASIZE))
261 1.6 thorpej errx(1, "bad fd control message "
262 1.6 thorpej "length %d", cmp->cmsg_len);
263 1.2 thorpej
264 1.5 sommerfe files = (int *)CMSG_DATA(cmp);
265 1.5 sommerfe break;
266 1.2 thorpej
267 1.5 sommerfe case SCM_CREDS:
268 1.6 thorpej if (cmp->cmsg_len < CMSG_LEN(SOCKCREDSIZE(1)))
269 1.6 thorpej errx(1, "bad cred control message "
270 1.6 thorpej "length %d", cmp->cmsg_len);
271 1.2 thorpej
272 1.5 sommerfe sc = (struct sockcred *)CMSG_DATA(cmp);
273 1.5 sommerfe break;
274 1.2 thorpej
275 1.5 sommerfe default:
276 1.5 sommerfe errx(1, "unexpected control message");
277 1.5 sommerfe /* NOTREACHED */
278 1.5 sommerfe }
279 1.2 thorpej }
280 1.1 thorpej
281 1.5 sommerfe /*
282 1.5 sommerfe * Read the files and print their contents.
283 1.5 sommerfe */
284 1.5 sommerfe if (files == NULL)
285 1.5 sommerfe warnx("didn't get fd control message");
286 1.5 sommerfe else {
287 1.5 sommerfe for (i = 0; i < NFILES; i++) {
288 1.5 sommerfe struct stat st;
289 1.5 sommerfe (void) memset(buf, 0, sizeof(buf));
290 1.5 sommerfe fstat(files[i], &st);
291 1.5 sommerfe if (S_ISDIR(st.st_mode)) {
292 1.5 sommerfe printf("file %d is a directory\n", i+1);
293 1.5 sommerfe } else if (S_ISSOCK(st.st_mode)) {
294 1.5 sommerfe printf("file %d is a socket\n", i+1);
295 1.5 sommerfe sock = files[i];
296 1.5 sommerfe } else {
297 1.5 sommerfe int c;
298 1.5 sommerfe c = read (files[i], buf, sizeof(buf));
299 1.5 sommerfe if (c < 0)
300 1.5 sommerfe err(1, "read file %d", i + 1);
301 1.5 sommerfe else if (c == 0)
302 1.5 sommerfe printf("[eof on %d]\n", i + 1);
303 1.5 sommerfe else
304 1.5 sommerfe printf("%s", buf);
305 1.5 sommerfe }
306 1.5 sommerfe }
307 1.5 sommerfe }
308 1.5 sommerfe /*
309 1.5 sommerfe * Double-check credentials.
310 1.5 sommerfe */
311 1.5 sommerfe if (sc == NULL)
312 1.5 sommerfe warnx("didn't get cred control message");
313 1.5 sommerfe else {
314 1.5 sommerfe if (sc->sc_uid == getuid() &&
315 1.5 sommerfe sc->sc_euid == geteuid() &&
316 1.5 sommerfe sc->sc_gid == getgid() &&
317 1.5 sommerfe sc->sc_egid == getegid())
318 1.5 sommerfe printf("Credentials match.\n");
319 1.5 sommerfe else
320 1.5 sommerfe printf("Credentials do NOT match.\n");
321 1.2 thorpej }
322 1.5 sommerfe } while (sock != -1);
323 1.1 thorpej
324 1.2 thorpej /*
325 1.2 thorpej * All done!
326 1.2 thorpej */
327 1.1 thorpej exit(0);
328 1.1 thorpej }
329 1.1 thorpej
330 1.1 thorpej void
331 1.5 sommerfe usage(progname)
332 1.5 sommerfe char *progname;
333 1.5 sommerfe {
334 1.5 sommerfe fprintf(stderr, "usage: %s [-derDES]\n", progname);
335 1.5 sommerfe exit(1);
336 1.5 sommerfe }
337 1.5 sommerfe
338 1.5 sommerfe void
339 1.2 thorpej catch_sigchld(sig)
340 1.2 thorpej int sig;
341 1.2 thorpej {
342 1.2 thorpej int status;
343 1.2 thorpej
344 1.2 thorpej (void) wait(&status);
345 1.2 thorpej }
346 1.2 thorpej
347 1.2 thorpej void
348 1.1 thorpej child()
349 1.1 thorpej {
350 1.4 mycroft #if MSG_SIZE >= 0
351 1.4 mycroft struct iovec iov;
352 1.4 mycroft #endif
353 1.1 thorpej struct msghdr msg;
354 1.6 thorpej char fname[16];
355 1.1 thorpej struct cmsghdr *cmp;
356 1.6 thorpej void *fdcm;
357 1.6 thorpej int i, fd, sock, nfd, *files;
358 1.2 thorpej struct sockaddr_un sun;
359 1.5 sommerfe int spair[2];
360 1.6 thorpej
361 1.6 thorpej fdcm = malloc(CMSG_SPACE(FDCM_DATASIZE));
362 1.6 thorpej if (fdcm == NULL)
363 1.6 thorpej err(1, "unable to malloc fd control message");
364 1.6 thorpej memset(fdcm, 0, CMSG_SPACE(FDCM_DATASIZE));
365 1.6 thorpej
366 1.6 thorpej cmp = fdcm;
367 1.6 thorpej files = (int *)CMSG_DATA(fdcm);
368 1.6 thorpej
369 1.1 thorpej /*
370 1.2 thorpej * Create socket and connect to the receiver.
371 1.1 thorpej */
372 1.3 thorpej if ((sock = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1)
373 1.2 thorpej errx(1, "child socket");
374 1.2 thorpej
375 1.2 thorpej (void) memset(&sun, 0, sizeof(sun));
376 1.2 thorpej sun.sun_family = AF_LOCAL;
377 1.2 thorpej (void) strcpy(sun.sun_path, SOCK_NAME);
378 1.2 thorpej sun.sun_len = SUN_LEN(&sun);
379 1.2 thorpej
380 1.2 thorpej if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
381 1.2 thorpej err(1, "child connect");
382 1.1 thorpej
383 1.5 sommerfe nfd = NFILES;
384 1.5 sommerfe i = 0;
385 1.5 sommerfe
386 1.5 sommerfe if (pass_sock) {
387 1.6 thorpej files[i++] = sock;
388 1.5 sommerfe }
389 1.5 sommerfe
390 1.5 sommerfe if (pass_dir)
391 1.5 sommerfe nfd--;
392 1.5 sommerfe
393 1.1 thorpej /*
394 1.5 sommerfe * Open the files again, and pass them to the child
395 1.5 sommerfe * over the socket.
396 1.1 thorpej */
397 1.5 sommerfe
398 1.5 sommerfe for (; i < nfd; i++) {
399 1.2 thorpej (void) sprintf(fname, "file%d", i + 1);
400 1.2 thorpej if ((fd = open(fname, O_RDONLY, 0666)) == -1)
401 1.2 thorpej err(1, "child open %s", fname);
402 1.6 thorpej files[i] = fd;
403 1.2 thorpej }
404 1.5 sommerfe
405 1.5 sommerfe if (pass_dir) {
406 1.5 sommerfe char *dirname = pass_root_dir ? "/" : ".";
407 1.5 sommerfe
408 1.5 sommerfe
409 1.5 sommerfe if ((fd = open(dirname, O_RDONLY, 0)) == -1) {
410 1.5 sommerfe err(1, "child open directory %s", dirname);
411 1.5 sommerfe }
412 1.6 thorpej files[i] = fd;
413 1.5 sommerfe }
414 1.5 sommerfe
415 1.1 thorpej (void) memset(&msg, 0, sizeof(msg));
416 1.6 thorpej msg.msg_control = fdcm;
417 1.6 thorpej msg.msg_controllen = CMSG_LEN(FDCM_DATASIZE);
418 1.4 mycroft #if MSG_SIZE >= 0
419 1.4 mycroft iov.iov_base = buf;
420 1.4 mycroft iov.iov_len = MSG_SIZE;
421 1.4 mycroft msg.msg_iov = &iov;
422 1.4 mycroft msg.msg_iovlen = 1;
423 1.4 mycroft #endif
424 1.1 thorpej
425 1.1 thorpej cmp = CMSG_FIRSTHDR(&msg);
426 1.6 thorpej cmp->cmsg_len = CMSG_LEN(FDCM_DATASIZE);
427 1.2 thorpej cmp->cmsg_level = SOL_SOCKET;
428 1.2 thorpej cmp->cmsg_type = SCM_RIGHTS;
429 1.5 sommerfe
430 1.5 sommerfe while (make_pretzel > 0) {
431 1.5 sommerfe if (socketpair(PF_LOCAL, SOCK_STREAM, 0, spair) < 0)
432 1.5 sommerfe err(1, "socketpair");
433 1.5 sommerfe
434 1.5 sommerfe printf("send pretzel\n");
435 1.5 sommerfe if (sendmsg(spair[0], &msg, 0) < 0)
436 1.5 sommerfe err(1, "child prezel sendmsg");
437 1.5 sommerfe
438 1.6 thorpej close(files[0]);
439 1.6 thorpej close(files[1]);
440 1.6 thorpej files[0] = spair[0];
441 1.6 thorpej files[1] = spair[1];
442 1.5 sommerfe make_pretzel--;
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