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