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