Home | History | Annotate | Line # | Download | only in mount_portal
activate.c revision 1.8.10.1
      1  1.8.10.1  minoura /*	$NetBSD: activate.c,v 1.8.10.1 2000/06/22 16:05:39 minoura Exp $	*/
      2       1.4      cgd 
      3       1.1      cgd /*
      4       1.1      cgd  * Copyright (c) 1992, 1993
      5       1.1      cgd  *	The Regents of the University of California.  All rights reserved.
      6       1.1      cgd  *
      7       1.1      cgd  * This code is derived from software donated to Berkeley by
      8       1.1      cgd  * Jan-Simon Pendry.
      9       1.1      cgd  *
     10       1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1      cgd  * modification, are permitted provided that the following conditions
     12       1.1      cgd  * are met:
     13       1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18       1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19       1.1      cgd  *    must display the following acknowledgement:
     20       1.1      cgd  *	This product includes software developed by the University of
     21       1.1      cgd  *	California, Berkeley and its contributors.
     22       1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23       1.1      cgd  *    may be used to endorse or promote products derived from this software
     24       1.1      cgd  *    without specific prior written permission.
     25       1.1      cgd  *
     26       1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27       1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28       1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29       1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30       1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31       1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32       1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33       1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34       1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35       1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36       1.1      cgd  * SUCH DAMAGE.
     37       1.1      cgd  *
     38       1.2  mycroft  *	from: Id: activate.c,v 1.2 1992/05/27 07:09:27 jsp Exp
     39       1.7    lukem  *	@(#)activate.c	8.3 (Berkeley) 4/28/95
     40       1.1      cgd  */
     41       1.1      cgd 
     42       1.6    lukem #include <sys/cdefs.h>
     43       1.6    lukem #ifndef lint
     44  1.8.10.1  minoura __RCSID("$NetBSD: activate.c,v 1.8.10.1 2000/06/22 16:05:39 minoura Exp $");
     45       1.6    lukem #endif /* not lint */
     46       1.6    lukem 
     47       1.1      cgd #include <stdio.h>
     48       1.1      cgd #include <stdlib.h>
     49       1.1      cgd #include <unistd.h>
     50       1.1      cgd #include <string.h>
     51       1.1      cgd #include <errno.h>
     52       1.1      cgd #include <signal.h>
     53       1.1      cgd #include <sys/types.h>
     54       1.1      cgd #include <sys/param.h>
     55       1.1      cgd #include <sys/socket.h>
     56       1.1      cgd #include <sys/un.h>
     57       1.1      cgd #include <sys/syslog.h>
     58       1.1      cgd #include <sys/uio.h>
     59       1.1      cgd 
     60       1.1      cgd #include "portald.h"
     61       1.1      cgd 
     62       1.6    lukem static	int	activate_argv __P((struct portal_cred *, char *, char **,
     63       1.6    lukem 				    int, int *));
     64       1.6    lukem static	int	get_request __P((int, struct portal_cred *, char *, int));
     65       1.6    lukem static	void	send_reply __P((int, int, int));
     66       1.6    lukem 
     67       1.1      cgd /*
     68       1.1      cgd  * Scan the providers list and call the
     69       1.1      cgd  * appropriate function.
     70       1.1      cgd  */
     71       1.6    lukem static int
     72       1.6    lukem activate_argv(pcr, key, v, so, fdp)
     73       1.6    lukem 	struct portal_cred *pcr;
     74       1.6    lukem 	char *key;
     75       1.6    lukem 	char **v;
     76       1.6    lukem 	int so;
     77       1.6    lukem 	int *fdp;
     78       1.1      cgd {
     79       1.1      cgd 	provider *pr;
     80       1.1      cgd 
     81       1.1      cgd 	for (pr = providers; pr->pr_match; pr++)
     82       1.1      cgd 		if (strcmp(v[0], pr->pr_match) == 0)
     83       1.1      cgd 			return ((*pr->pr_func)(pcr, key, v, so, fdp));
     84       1.1      cgd 
     85       1.1      cgd 	return (ENOENT);
     86       1.1      cgd }
     87       1.1      cgd 
     88       1.6    lukem static int
     89       1.6    lukem get_request(so, pcr, key, klen)
     90       1.6    lukem 	int so;
     91       1.6    lukem 	struct portal_cred *pcr;
     92       1.6    lukem 	char *key;
     93       1.6    lukem 	int klen;
     94       1.1      cgd {
     95       1.1      cgd 	struct iovec iov[2];
     96       1.1      cgd 	struct msghdr msg;
     97       1.1      cgd 	int n;
     98       1.1      cgd 
     99       1.1      cgd 	iov[0].iov_base = (caddr_t) pcr;
    100       1.1      cgd 	iov[0].iov_len = sizeof(*pcr);
    101       1.1      cgd 	iov[1].iov_base = key;
    102       1.1      cgd 	iov[1].iov_len = klen;
    103       1.1      cgd 
    104       1.3  mycroft 	memset(&msg, 0, sizeof(msg));
    105       1.1      cgd 	msg.msg_iov = iov;
    106       1.1      cgd 	msg.msg_iovlen = 2;
    107       1.1      cgd 
    108       1.1      cgd 	n = recvmsg(so, &msg, 0);
    109       1.1      cgd 	if (n < 0)
    110       1.1      cgd 		return (errno);
    111       1.1      cgd 
    112       1.1      cgd 	if (n <= sizeof(*pcr))
    113       1.1      cgd 		return (EINVAL);
    114       1.1      cgd 
    115       1.1      cgd 	n -= sizeof(*pcr);
    116       1.1      cgd 	key[n] = '\0';
    117       1.1      cgd 
    118       1.1      cgd 	return (0);
    119       1.1      cgd }
    120       1.1      cgd 
    121       1.6    lukem static void
    122       1.6    lukem send_reply(so, fd, error)
    123       1.6    lukem 	int so;
    124       1.6    lukem 	int fd;
    125       1.6    lukem 	int error;
    126       1.1      cgd {
    127       1.1      cgd 	int n;
    128       1.1      cgd 	struct iovec iov;
    129       1.1      cgd 	struct msghdr msg;
    130  1.8.10.1  minoura 	void *ctl = NULL;
    131  1.8.10.1  minoura 	struct cmsghdr *cmsg;
    132  1.8.10.1  minoura 	int *files;
    133  1.8.10.1  minoura 	socklen_t cmsgsize;
    134       1.1      cgd 
    135       1.1      cgd 	/*
    136       1.1      cgd 	 * Line up error code.  Don't worry about byte ordering
    137       1.1      cgd 	 * because we must be sending to the local machine.
    138       1.1      cgd 	 */
    139       1.1      cgd 	iov.iov_base = (caddr_t) &error;
    140       1.1      cgd 	iov.iov_len = sizeof(error);
    141       1.1      cgd 
    142       1.1      cgd 	/*
    143       1.1      cgd 	 * Build a msghdr
    144       1.1      cgd 	 */
    145       1.3  mycroft 	memset(&msg, 0, sizeof(msg));
    146       1.1      cgd 	msg.msg_iov = &iov;
    147       1.1      cgd 	msg.msg_iovlen = 1;
    148       1.1      cgd 
    149       1.1      cgd 	/*
    150       1.1      cgd 	 * If there is a file descriptor to send then
    151       1.1      cgd 	 * construct a suitable rights control message.
    152       1.1      cgd 	 */
    153       1.1      cgd 	if (fd >= 0) {
    154  1.8.10.1  minoura 		cmsgsize = CMSG_SPACE(sizeof(*files));
    155  1.8.10.1  minoura 
    156  1.8.10.1  minoura 		ctl = malloc(cmsgsize);
    157  1.8.10.1  minoura 		if (ctl == NULL) {
    158  1.8.10.1  minoura 			syslog(LOG_ERR, "malloc control message: %m");
    159  1.8.10.1  minoura 			return;
    160  1.8.10.1  minoura 		}
    161  1.8.10.1  minoura 		memset(ctl, 0, cmsgsize);
    162  1.8.10.1  minoura 
    163  1.8.10.1  minoura 		cmsg = (struct cmsghdr *) ctl;
    164  1.8.10.1  minoura 		cmsg->cmsg_len = CMSG_LEN(sizeof(int));
    165  1.8.10.1  minoura 		cmsg->cmsg_level = SOL_SOCKET;
    166  1.8.10.1  minoura 		cmsg->cmsg_type = SCM_RIGHTS;
    167  1.8.10.1  minoura 
    168  1.8.10.1  minoura 		files = (int *)CMSG_DATA(cmsg);
    169  1.8.10.1  minoura 		files[0] = fd;
    170  1.8.10.1  minoura 
    171  1.8.10.1  minoura 		msg.msg_control = ctl;
    172  1.8.10.1  minoura 		msg.msg_controllen = cmsgsize;
    173       1.1      cgd 	}
    174       1.1      cgd 
    175       1.1      cgd 	/*
    176       1.1      cgd 	 * Send to kernel...
    177       1.1      cgd 	 */
    178       1.1      cgd 	if ((n = sendmsg(so, &msg, MSG_EOR)) < 0)
    179       1.8    enami 		syslog(LOG_ERR, "send: %m");
    180       1.1      cgd #ifdef DEBUG
    181       1.1      cgd 	fprintf(stderr, "sent %d bytes\n", n);
    182       1.1      cgd #endif
    183       1.1      cgd 	sleep(1);	/*XXX*/
    184       1.1      cgd #ifdef notdef
    185       1.1      cgd 	if (shutdown(so, 2) < 0)
    186       1.8    enami 		syslog(LOG_ERR, "shutdown: %m");
    187       1.1      cgd #endif
    188       1.1      cgd 	/*
    189  1.8.10.1  minoura 	 * Throw away the open file descriptor and control
    190  1.8.10.1  minoura 	 * message buffer.
    191       1.1      cgd 	 */
    192  1.8.10.1  minoura 	if (fd >= 0)
    193  1.8.10.1  minoura 		(void) close(fd);
    194  1.8.10.1  minoura 	if (ctl != NULL)
    195  1.8.10.1  minoura 		free(ctl);
    196       1.1      cgd }
    197       1.1      cgd 
    198       1.6    lukem void
    199       1.6    lukem activate(q, so)
    200       1.6    lukem 	qelem *q;
    201       1.6    lukem 	int so;
    202       1.1      cgd {
    203       1.1      cgd 	struct portal_cred pcred;
    204       1.1      cgd 	char key[MAXPATHLEN+1];
    205       1.1      cgd 	int error;
    206       1.1      cgd 	char **v;
    207       1.1      cgd 	int fd = -1;
    208       1.1      cgd 
    209       1.1      cgd 	/*
    210       1.1      cgd 	 * Read the key from the socket
    211       1.1      cgd 	 */
    212       1.1      cgd 	error = get_request(so, &pcred, key, sizeof(key));
    213       1.1      cgd 	if (error) {
    214       1.8    enami 		syslog(LOG_ERR, "activate: recvmsg: %m");
    215       1.1      cgd 		goto drop;
    216       1.1      cgd 	}
    217       1.1      cgd 
    218       1.1      cgd #ifdef DEBUG
    219       1.1      cgd 	fprintf(stderr, "lookup key %s\n", key);
    220       1.1      cgd #endif
    221       1.1      cgd 
    222       1.1      cgd 	/*
    223       1.1      cgd 	 * Find a match in the configuration file
    224       1.1      cgd 	 */
    225       1.1      cgd 	v = conf_match(q, key);
    226       1.1      cgd 
    227       1.1      cgd 	/*
    228       1.1      cgd 	 * If a match existed, then find an appropriate portal
    229       1.1      cgd 	 * otherwise simply return ENOENT.
    230       1.1      cgd 	 */
    231       1.1      cgd 	if (v) {
    232       1.1      cgd 		error = activate_argv(&pcred, key, v, so, &fd);
    233       1.1      cgd 		if (error)
    234       1.1      cgd 			fd = -1;
    235       1.1      cgd 		else if (fd < 0)
    236       1.1      cgd 			error = -1;
    237       1.8    enami 	} else
    238       1.1      cgd 		error = ENOENT;
    239       1.1      cgd 
    240       1.1      cgd 	if (error >= 0)
    241       1.1      cgd 		send_reply(so, fd, error);
    242       1.1      cgd 
    243       1.1      cgd drop:;
    244       1.1      cgd 	close(so);
    245       1.1      cgd }
    246