activate.c revision 1.13 1 1.13 xtraeme /* $NetBSD: activate.c,v 1.13 2005/02/09 13:57:57 xtraeme 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.12 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd *
34 1.2 mycroft * from: Id: activate.c,v 1.2 1992/05/27 07:09:27 jsp Exp
35 1.7 lukem * @(#)activate.c 8.3 (Berkeley) 4/28/95
36 1.1 cgd */
37 1.1 cgd
38 1.6 lukem #include <sys/cdefs.h>
39 1.6 lukem #ifndef lint
40 1.13 xtraeme __RCSID("$NetBSD: activate.c,v 1.13 2005/02/09 13:57:57 xtraeme Exp $");
41 1.6 lukem #endif /* not lint */
42 1.6 lukem
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.13 xtraeme static int activate_argv(struct portal_cred *, char *, char **,
59 1.13 xtraeme int, int *);
60 1.13 xtraeme static int get_request(int, struct portal_cred *, char *, int);
61 1.13 xtraeme static void send_reply(int, int, int);
62 1.6 lukem
63 1.1 cgd /*
64 1.1 cgd * Scan the providers list and call the
65 1.1 cgd * appropriate function.
66 1.1 cgd */
67 1.6 lukem static int
68 1.13 xtraeme activate_argv(struct portal_cred *pcr, char *key, char **v, int so, int *fdp)
69 1.1 cgd {
70 1.1 cgd provider *pr;
71 1.1 cgd
72 1.1 cgd for (pr = providers; pr->pr_match; pr++)
73 1.1 cgd if (strcmp(v[0], pr->pr_match) == 0)
74 1.1 cgd return ((*pr->pr_func)(pcr, key, v, so, fdp));
75 1.1 cgd
76 1.1 cgd return (ENOENT);
77 1.1 cgd }
78 1.1 cgd
79 1.6 lukem static int
80 1.13 xtraeme get_request(int so, struct portal_cred *pcr, char *key, int klen)
81 1.1 cgd {
82 1.1 cgd struct iovec iov[2];
83 1.1 cgd struct msghdr msg;
84 1.1 cgd int n;
85 1.1 cgd
86 1.1 cgd iov[0].iov_base = (caddr_t) pcr;
87 1.1 cgd iov[0].iov_len = sizeof(*pcr);
88 1.1 cgd iov[1].iov_base = key;
89 1.1 cgd iov[1].iov_len = klen;
90 1.1 cgd
91 1.3 mycroft memset(&msg, 0, sizeof(msg));
92 1.1 cgd msg.msg_iov = iov;
93 1.1 cgd msg.msg_iovlen = 2;
94 1.1 cgd
95 1.1 cgd n = recvmsg(so, &msg, 0);
96 1.1 cgd if (n < 0)
97 1.1 cgd return (errno);
98 1.1 cgd
99 1.1 cgd if (n <= sizeof(*pcr))
100 1.1 cgd return (EINVAL);
101 1.1 cgd
102 1.1 cgd n -= sizeof(*pcr);
103 1.1 cgd key[n] = '\0';
104 1.1 cgd
105 1.1 cgd return (0);
106 1.1 cgd }
107 1.1 cgd
108 1.6 lukem static void
109 1.13 xtraeme send_reply(int so, int fd, int error)
110 1.1 cgd {
111 1.1 cgd int n;
112 1.1 cgd struct iovec iov;
113 1.1 cgd struct msghdr msg;
114 1.9 thorpej void *ctl = NULL;
115 1.9 thorpej struct cmsghdr *cmsg;
116 1.9 thorpej int *files;
117 1.9 thorpej socklen_t cmsgsize;
118 1.1 cgd
119 1.1 cgd /*
120 1.1 cgd * Line up error code. Don't worry about byte ordering
121 1.1 cgd * because we must be sending to the local machine.
122 1.1 cgd */
123 1.1 cgd iov.iov_base = (caddr_t) &error;
124 1.1 cgd iov.iov_len = sizeof(error);
125 1.1 cgd
126 1.1 cgd /*
127 1.1 cgd * Build a msghdr
128 1.1 cgd */
129 1.3 mycroft memset(&msg, 0, sizeof(msg));
130 1.1 cgd msg.msg_iov = &iov;
131 1.1 cgd msg.msg_iovlen = 1;
132 1.1 cgd
133 1.1 cgd /*
134 1.1 cgd * If there is a file descriptor to send then
135 1.1 cgd * construct a suitable rights control message.
136 1.1 cgd */
137 1.1 cgd if (fd >= 0) {
138 1.11 atatat cmsgsize = CMSG_LEN(sizeof(*files));
139 1.9 thorpej
140 1.9 thorpej ctl = malloc(cmsgsize);
141 1.9 thorpej if (ctl == NULL) {
142 1.10 lukem syslog(LOG_WARNING, "malloc control message: %m");
143 1.9 thorpej return;
144 1.9 thorpej }
145 1.9 thorpej memset(ctl, 0, cmsgsize);
146 1.9 thorpej
147 1.9 thorpej cmsg = (struct cmsghdr *) ctl;
148 1.9 thorpej cmsg->cmsg_len = CMSG_LEN(sizeof(int));
149 1.9 thorpej cmsg->cmsg_level = SOL_SOCKET;
150 1.9 thorpej cmsg->cmsg_type = SCM_RIGHTS;
151 1.9 thorpej
152 1.9 thorpej files = (int *)CMSG_DATA(cmsg);
153 1.9 thorpej files[0] = fd;
154 1.9 thorpej
155 1.9 thorpej msg.msg_control = ctl;
156 1.9 thorpej msg.msg_controllen = cmsgsize;
157 1.1 cgd }
158 1.1 cgd
159 1.1 cgd /*
160 1.1 cgd * Send to kernel...
161 1.1 cgd */
162 1.1 cgd if ((n = sendmsg(so, &msg, MSG_EOR)) < 0)
163 1.10 lukem syslog(LOG_WARNING, "send: %m");
164 1.1 cgd #ifdef DEBUG
165 1.1 cgd fprintf(stderr, "sent %d bytes\n", n);
166 1.1 cgd #endif
167 1.1 cgd sleep(1); /*XXX*/
168 1.1 cgd #ifdef notdef
169 1.1 cgd if (shutdown(so, 2) < 0)
170 1.10 lukem syslog(LOG_WARNING, "shutdown: %m");
171 1.1 cgd #endif
172 1.1 cgd /*
173 1.9 thorpej * Throw away the open file descriptor and control
174 1.9 thorpej * message buffer.
175 1.1 cgd */
176 1.9 thorpej if (fd >= 0)
177 1.9 thorpej (void) close(fd);
178 1.9 thorpej if (ctl != NULL)
179 1.9 thorpej free(ctl);
180 1.1 cgd }
181 1.1 cgd
182 1.6 lukem void
183 1.13 xtraeme activate(qelem *q, int so)
184 1.1 cgd {
185 1.1 cgd struct portal_cred pcred;
186 1.1 cgd char key[MAXPATHLEN+1];
187 1.1 cgd int error;
188 1.1 cgd char **v;
189 1.1 cgd int fd = -1;
190 1.1 cgd
191 1.1 cgd /*
192 1.1 cgd * Read the key from the socket
193 1.1 cgd */
194 1.1 cgd error = get_request(so, &pcred, key, sizeof(key));
195 1.1 cgd if (error) {
196 1.10 lukem syslog(LOG_WARNING, "activate: recvmsg: %m");
197 1.1 cgd goto drop;
198 1.1 cgd }
199 1.1 cgd
200 1.1 cgd #ifdef DEBUG
201 1.1 cgd fprintf(stderr, "lookup key %s\n", key);
202 1.1 cgd #endif
203 1.1 cgd
204 1.1 cgd /*
205 1.1 cgd * Find a match in the configuration file
206 1.1 cgd */
207 1.1 cgd v = conf_match(q, key);
208 1.1 cgd
209 1.1 cgd /*
210 1.1 cgd * If a match existed, then find an appropriate portal
211 1.1 cgd * otherwise simply return ENOENT.
212 1.1 cgd */
213 1.1 cgd if (v) {
214 1.1 cgd error = activate_argv(&pcred, key, v, so, &fd);
215 1.1 cgd if (error)
216 1.1 cgd fd = -1;
217 1.1 cgd else if (fd < 0)
218 1.1 cgd error = -1;
219 1.8 enami } else
220 1.1 cgd error = ENOENT;
221 1.1 cgd
222 1.1 cgd if (error >= 0)
223 1.1 cgd send_reply(so, fd, error);
224 1.1 cgd
225 1.1 cgd drop:;
226 1.1 cgd close(so);
227 1.1 cgd }
228