Home | History | Annotate | Line # | Download | only in unfdpass
unfdpass.c revision 1.2
      1  1.2  thorpej /*	$NetBSD: unfdpass.c,v 1.2 1998/01/07 23:38:54 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.1  thorpej #include <err.h>
     50  1.2  thorpej #include <errno.h>
     51  1.1  thorpej #include <fcntl.h>
     52  1.2  thorpej #include <signal.h>
     53  1.1  thorpej #include <stdio.h>
     54  1.1  thorpej #include <string.h>
     55  1.1  thorpej #include <unistd.h>
     56  1.1  thorpej 
     57  1.2  thorpej #define	SOCK_NAME	"test-sock"
     58  1.1  thorpej 
     59  1.1  thorpej int	main __P((int, char *[]));
     60  1.1  thorpej void	child __P((void));
     61  1.2  thorpej void	catch_sigchld __P((int));
     62  1.1  thorpej 
     63  1.2  thorpej struct fdcmessage {
     64  1.1  thorpej 	struct cmsghdr cm;
     65  1.1  thorpej 	int files[2];
     66  1.1  thorpej };
     67  1.1  thorpej 
     68  1.2  thorpej struct crcmessage {
     69  1.2  thorpej 	struct cmsghdr cm;
     70  1.2  thorpej 	char creds[SOCKCREDSIZE(NGROUPS)];
     71  1.2  thorpej };
     72  1.2  thorpej 
     73  1.1  thorpej /* ARGSUSED */
     74  1.1  thorpej int
     75  1.1  thorpej main(argc, argv)
     76  1.1  thorpej 	int argc;
     77  1.1  thorpej 	char *argv[];
     78  1.1  thorpej {
     79  1.1  thorpej 	struct msghdr msg;
     80  1.2  thorpej 	int listensock, sock, fd, i, status;
     81  1.1  thorpej 	char fname[16], buf[64];
     82  1.2  thorpej 	struct cmsghdr *cmp;
     83  1.2  thorpej 	struct {
     84  1.2  thorpej 		struct fdcmessage fdcm;
     85  1.2  thorpej 		struct crcmessage crcm;
     86  1.2  thorpej 	} message;
     87  1.2  thorpej 	int *files = NULL;
     88  1.2  thorpej 	struct sockcred *sc = NULL;
     89  1.2  thorpej 	struct sockaddr_un sun, csun;
     90  1.2  thorpej 	int csunlen;
     91  1.2  thorpej 	fd_set oob;
     92  1.1  thorpej 	pid_t pid;
     93  1.1  thorpej 
     94  1.1  thorpej 	/*
     95  1.1  thorpej 	 * Create the test files.
     96  1.1  thorpej 	 */
     97  1.1  thorpej 	for (i = 0; i < 2; i++) {
     98  1.1  thorpej 		(void) sprintf(fname, "file%d", i + 1);
     99  1.1  thorpej 		if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
    100  1.1  thorpej 			err(1, "open %s", fname);
    101  1.1  thorpej 		(void) sprintf(buf, "This is file %d.\n", i + 1);
    102  1.1  thorpej 		if (write(fd, buf, strlen(buf)) != strlen(buf))
    103  1.1  thorpej 			err(1, "write %s", fname);
    104  1.1  thorpej 		(void) close(fd);
    105  1.1  thorpej 	}
    106  1.1  thorpej 
    107  1.1  thorpej 	/*
    108  1.2  thorpej 	 * Create the listen socket.
    109  1.1  thorpej 	 */
    110  1.2  thorpej 	if ((listensock = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1)
    111  1.2  thorpej 		err(1, "socket");
    112  1.2  thorpej 
    113  1.2  thorpej 	(void) unlink(SOCK_NAME);
    114  1.2  thorpej 	(void) memset(&sun, 0, sizeof(sun));
    115  1.2  thorpej 	sun.sun_family = AF_LOCAL;
    116  1.2  thorpej 	(void) strcpy(sun.sun_path, SOCK_NAME);
    117  1.2  thorpej 	sun.sun_len = SUN_LEN(&sun);
    118  1.2  thorpej 
    119  1.2  thorpej 	i = 1;
    120  1.2  thorpej 	if (setsockopt(listensock, 0, LOCAL_CREDS, &i, sizeof(i)) == -1)
    121  1.2  thorpej 		err(1, "setsockopt");
    122  1.2  thorpej 
    123  1.2  thorpej 	if (bind(listensock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
    124  1.2  thorpej 		err(1, "bind");
    125  1.2  thorpej 
    126  1.2  thorpej 	if (listen(listensock, 1) == -1)
    127  1.2  thorpej 		err(1, "listen");
    128  1.1  thorpej 
    129  1.2  thorpej 	/*
    130  1.2  thorpej 	 * Create the sender.
    131  1.2  thorpej 	 */
    132  1.2  thorpej 	(void) signal(SIGCHLD, catch_sigchld);
    133  1.1  thorpej 	pid = fork();
    134  1.1  thorpej 	switch (pid) {
    135  1.1  thorpej 	case -1:
    136  1.1  thorpej 		err(1, "fork");
    137  1.1  thorpej 		/* NOTREACHED */
    138  1.1  thorpej 
    139  1.1  thorpej 	case 0:
    140  1.1  thorpej 		child();
    141  1.1  thorpej 		/* NOTREACHED */
    142  1.1  thorpej 	}
    143  1.1  thorpej 
    144  1.2  thorpej 	/*
    145  1.2  thorpej 	 * Wait for the sender to connect.
    146  1.2  thorpej 	 */
    147  1.2  thorpej 	if ((sock = accept(listensock, (struct sockaddr *)&csun,
    148  1.2  thorpej 	    &csunlen)) == -1)
    149  1.2  thorpej 		err(1, "accept");
    150  1.1  thorpej 
    151  1.1  thorpej 	/*
    152  1.2  thorpej 	 * Give sender a chance to run.  We will get going again
    153  1.2  thorpej 	 * once the SIGCHLD arrives.
    154  1.1  thorpej 	 */
    155  1.2  thorpej 	(void) sleep(10);
    156  1.1  thorpej 
    157  1.2  thorpej 	/*
    158  1.2  thorpej 	 * Grab the descriptors and credentials passed to us.
    159  1.2  thorpej 	 */
    160  1.1  thorpej 	(void) memset(&msg, 0, sizeof(msg));
    161  1.2  thorpej 	msg.msg_control = (caddr_t) &message;
    162  1.2  thorpej 	msg.msg_controllen = sizeof(message);
    163  1.2  thorpej 
    164  1.2  thorpej 	if (recvmsg(sock, &msg, 0) < 0)
    165  1.2  thorpej 		err(1, "recvmsg");
    166  1.2  thorpej 
    167  1.2  thorpej 	(void) close(sock);
    168  1.2  thorpej 
    169  1.2  thorpej 	if (msg.msg_controllen == 0)
    170  1.2  thorpej 		errx(1, "no control messages received");
    171  1.2  thorpej 
    172  1.2  thorpej 	if (msg.msg_flags & MSG_CTRUNC)
    173  1.2  thorpej 		errx(1, "lost control message data");
    174  1.1  thorpej 
    175  1.1  thorpej 	cmp = CMSG_FIRSTHDR(&msg);
    176  1.2  thorpej 	for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL;
    177  1.2  thorpej 	    cmp = CMSG_NXTHDR(&msg, cmp)) {
    178  1.2  thorpej 		if (cmp->cmsg_level != SOL_SOCKET)
    179  1.2  thorpej 			errx(1, "bad control message level %d",
    180  1.2  thorpej 			    cmp->cmsg_level);
    181  1.2  thorpej 
    182  1.2  thorpej 		switch (cmp->cmsg_type) {
    183  1.2  thorpej 		case SCM_RIGHTS:
    184  1.2  thorpej 			if (cmp->cmsg_len != sizeof(message.fdcm))
    185  1.2  thorpej 				errx(1, "bad fd control message length");
    186  1.2  thorpej 
    187  1.2  thorpej 			files = (int *)CMSG_DATA(cmp);
    188  1.2  thorpej 			break;
    189  1.2  thorpej 
    190  1.2  thorpej 		case SCM_CREDS:
    191  1.2  thorpej 			if (cmp->cmsg_len < sizeof(struct sockcred))
    192  1.2  thorpej 				errx(1, "bad cred control message length");
    193  1.2  thorpej 
    194  1.2  thorpej 			sc = (struct sockcred *)CMSG_DATA(cmp);
    195  1.2  thorpej 			break;
    196  1.2  thorpej 
    197  1.2  thorpej 		default:
    198  1.2  thorpej 			errx(1, "unexpected control message");
    199  1.2  thorpej 			/* NOTREACHED */
    200  1.2  thorpej 		}
    201  1.2  thorpej 	}
    202  1.1  thorpej 
    203  1.2  thorpej 	/*
    204  1.2  thorpej 	 * Read the files and print their contents.
    205  1.2  thorpej 	 */
    206  1.2  thorpej 	if (files == NULL)
    207  1.2  thorpej 		warnx("didn't get fd control message");
    208  1.2  thorpej 	else {
    209  1.2  thorpej 		for (i = 0; i < 2; i++) {
    210  1.2  thorpej 			(void) memset(buf, 0, sizeof(buf));
    211  1.2  thorpej 			if (read(files[i], buf, sizeof(buf)) <= 0)
    212  1.2  thorpej 				err(1, "read file %d", i + 1);
    213  1.2  thorpej 			printf("%s", buf);
    214  1.2  thorpej 		}
    215  1.1  thorpej 	}
    216  1.1  thorpej 
    217  1.2  thorpej 	/*
    218  1.2  thorpej 	 * Double-check credentials.
    219  1.2  thorpej 	 */
    220  1.2  thorpej 	if (sc == NULL)
    221  1.2  thorpej 		warnx("didn't get cred control message");
    222  1.2  thorpej 	else {
    223  1.2  thorpej 		if (sc->sc_uid == getuid() &&
    224  1.2  thorpej 		    sc->sc_euid == geteuid() &&
    225  1.2  thorpej 		    sc->sc_gid == getgid() &&
    226  1.2  thorpej 		    sc->sc_egid == getegid())
    227  1.2  thorpej 			printf("Credentials match.\n");
    228  1.2  thorpej 		else
    229  1.2  thorpej 			printf("Credentials do NOT match.\n");
    230  1.2  thorpej 	}
    231  1.1  thorpej 
    232  1.2  thorpej 	/*
    233  1.2  thorpej 	 * All done!
    234  1.2  thorpej 	 */
    235  1.1  thorpej 	exit(0);
    236  1.1  thorpej }
    237  1.1  thorpej 
    238  1.1  thorpej void
    239  1.2  thorpej catch_sigchld(sig)
    240  1.2  thorpej 	int sig;
    241  1.2  thorpej {
    242  1.2  thorpej 	int status;
    243  1.2  thorpej 
    244  1.2  thorpej 	(void) wait(&status);
    245  1.2  thorpej }
    246  1.2  thorpej 
    247  1.2  thorpej void
    248  1.1  thorpej child()
    249  1.1  thorpej {
    250  1.1  thorpej 	struct msghdr msg;
    251  1.2  thorpej 	char fname[16], buf[64];
    252  1.1  thorpej 	struct cmsghdr *cmp;
    253  1.2  thorpej 	struct fdcmessage fdcm;
    254  1.2  thorpej 	int i, fd, sock;
    255  1.2  thorpej 	struct sockaddr_un sun;
    256  1.1  thorpej 
    257  1.1  thorpej 	/*
    258  1.2  thorpej 	 * Create socket and connect to the receiver.
    259  1.1  thorpej 	 */
    260  1.2  thorpej 	if ((sock = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1)
    261  1.2  thorpej 		errx(1, "child socket");
    262  1.2  thorpej 
    263  1.2  thorpej 	(void) memset(&sun, 0, sizeof(sun));
    264  1.2  thorpej 	sun.sun_family = AF_LOCAL;
    265  1.2  thorpej 	(void) strcpy(sun.sun_path, SOCK_NAME);
    266  1.2  thorpej 	sun.sun_len = SUN_LEN(&sun);
    267  1.2  thorpej 
    268  1.2  thorpej 	if (connect(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
    269  1.2  thorpej 		err(1, "child connect");
    270  1.1  thorpej 
    271  1.1  thorpej 	/*
    272  1.2  thorpej 	 * Open the files again, and pass them to the child over the socket.
    273  1.1  thorpej 	 */
    274  1.2  thorpej 	for (i = 0; i < 2; i++) {
    275  1.2  thorpej 		(void) sprintf(fname, "file%d", i + 1);
    276  1.2  thorpej 		if ((fd = open(fname, O_RDONLY, 0666)) == -1)
    277  1.2  thorpej 			err(1, "child open %s", fname);
    278  1.2  thorpej 		fdcm.files[i] = fd;
    279  1.2  thorpej 	}
    280  1.2  thorpej 
    281  1.1  thorpej 	(void) memset(&msg, 0, sizeof(msg));
    282  1.2  thorpej 	msg.msg_control = (caddr_t) &fdcm;
    283  1.2  thorpej 	msg.msg_controllen = sizeof(fdcm);
    284  1.1  thorpej 
    285  1.1  thorpej 	cmp = CMSG_FIRSTHDR(&msg);
    286  1.2  thorpej 	cmp->cmsg_len = sizeof(fdcm);
    287  1.2  thorpej 	cmp->cmsg_level = SOL_SOCKET;
    288  1.2  thorpej 	cmp->cmsg_type = SCM_RIGHTS;
    289  1.1  thorpej 
    290  1.2  thorpej 	if (sendmsg(sock, &msg, 0))
    291  1.2  thorpej 		err(1, "child sendmsg");
    292  1.1  thorpej 
    293  1.1  thorpej 	/*
    294  1.1  thorpej 	 * All done!
    295  1.1  thorpej 	 */
    296  1.1  thorpej 	exit(0);
    297  1.1  thorpej }
    298