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