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