activate.c revision 1.6 1 1.6 lukem /* $NetBSD: activate.c,v 1.6 1997/09/15 05:58:22 lukem 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.4 cgd * @(#)activate.c 8.2 (Berkeley) 3/27/94
40 1.1 cgd */
41 1.1 cgd
42 1.6 lukem #include <sys/cdefs.h>
43 1.6 lukem #ifndef lint
44 1.6 lukem __RCSID("$NetBSD: activate.c,v 1.6 1997/09/15 05:58:22 lukem 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.1 cgd struct {
131 1.1 cgd struct cmsghdr cmsg;
132 1.1 cgd int fd;
133 1.1 cgd } ctl;
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.1 cgd ctl.fd = fd;
155 1.1 cgd ctl.cmsg.cmsg_len = sizeof(ctl);
156 1.1 cgd ctl.cmsg.cmsg_level = SOL_SOCKET;
157 1.1 cgd ctl.cmsg.cmsg_type = SCM_RIGHTS;
158 1.1 cgd msg.msg_control = (caddr_t) &ctl;
159 1.1 cgd msg.msg_controllen = ctl.cmsg.cmsg_len;
160 1.1 cgd }
161 1.1 cgd
162 1.1 cgd /*
163 1.1 cgd * Send to kernel...
164 1.1 cgd */
165 1.1 cgd if ((n = sendmsg(so, &msg, MSG_EOR)) < 0)
166 1.1 cgd syslog(LOG_ERR, "send: %s", strerror(errno));
167 1.1 cgd #ifdef DEBUG
168 1.1 cgd fprintf(stderr, "sent %d bytes\n", n);
169 1.1 cgd #endif
170 1.1 cgd sleep(1); /*XXX*/
171 1.1 cgd #ifdef notdef
172 1.1 cgd if (shutdown(so, 2) < 0)
173 1.1 cgd syslog(LOG_ERR, "shutdown: %s", strerror(errno));
174 1.1 cgd #endif
175 1.1 cgd /*
176 1.1 cgd * Throw away the open file descriptor
177 1.1 cgd */
178 1.1 cgd (void) close(fd);
179 1.1 cgd }
180 1.1 cgd
181 1.6 lukem void
182 1.6 lukem activate(q, so)
183 1.6 lukem qelem *q;
184 1.6 lukem int so;
185 1.1 cgd {
186 1.1 cgd struct portal_cred pcred;
187 1.1 cgd char key[MAXPATHLEN+1];
188 1.1 cgd int error;
189 1.1 cgd char **v;
190 1.1 cgd int fd = -1;
191 1.1 cgd
192 1.1 cgd /*
193 1.1 cgd * Read the key from the socket
194 1.1 cgd */
195 1.1 cgd error = get_request(so, &pcred, key, sizeof(key));
196 1.1 cgd if (error) {
197 1.1 cgd syslog(LOG_ERR, "activate: recvmsg: %s", strerror(error));
198 1.1 cgd goto drop;
199 1.1 cgd }
200 1.1 cgd
201 1.1 cgd #ifdef DEBUG
202 1.1 cgd fprintf(stderr, "lookup key %s\n", key);
203 1.1 cgd #endif
204 1.1 cgd
205 1.1 cgd /*
206 1.1 cgd * Find a match in the configuration file
207 1.1 cgd */
208 1.1 cgd v = conf_match(q, key);
209 1.1 cgd
210 1.1 cgd /*
211 1.1 cgd * If a match existed, then find an appropriate portal
212 1.1 cgd * otherwise simply return ENOENT.
213 1.1 cgd */
214 1.1 cgd if (v) {
215 1.1 cgd error = activate_argv(&pcred, key, v, so, &fd);
216 1.1 cgd if (error)
217 1.1 cgd fd = -1;
218 1.1 cgd else if (fd < 0)
219 1.1 cgd error = -1;
220 1.1 cgd } else {
221 1.1 cgd error = ENOENT;
222 1.1 cgd }
223 1.1 cgd
224 1.1 cgd if (error >= 0)
225 1.1 cgd send_reply(so, fd, error);
226 1.1 cgd
227 1.1 cgd drop:;
228 1.1 cgd close(so);
229 1.1 cgd }
230