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