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