activate.c revision 1.14 1 1.14 pooka /* $NetBSD: activate.c,v 1.14 2007/07/02 18:07:44 pooka 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.14 pooka __RCSID("$NetBSD: activate.c,v 1.14 2007/07/02 18:07:44 pooka 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 get_request(int, struct portal_cred *, char *, int);
59 1.13 xtraeme static void send_reply(int, int, int);
60 1.6 lukem
61 1.1 cgd /*
62 1.1 cgd * Scan the providers list and call the
63 1.1 cgd * appropriate function.
64 1.1 cgd */
65 1.14 pooka int
66 1.14 pooka activate_argv(struct portal_cred *pcr, char *key, char **v, int *fdp)
67 1.1 cgd {
68 1.1 cgd provider *pr;
69 1.1 cgd
70 1.1 cgd for (pr = providers; pr->pr_match; pr++)
71 1.1 cgd if (strcmp(v[0], pr->pr_match) == 0)
72 1.14 pooka return ((*pr->pr_func)(pcr, key, v, fdp));
73 1.1 cgd
74 1.1 cgd return (ENOENT);
75 1.1 cgd }
76 1.1 cgd
77 1.6 lukem static int
78 1.13 xtraeme get_request(int so, struct portal_cred *pcr, char *key, int klen)
79 1.1 cgd {
80 1.1 cgd struct iovec iov[2];
81 1.1 cgd struct msghdr msg;
82 1.1 cgd int n;
83 1.1 cgd
84 1.1 cgd iov[0].iov_base = (caddr_t) pcr;
85 1.1 cgd iov[0].iov_len = sizeof(*pcr);
86 1.1 cgd iov[1].iov_base = key;
87 1.1 cgd iov[1].iov_len = klen;
88 1.1 cgd
89 1.3 mycroft memset(&msg, 0, sizeof(msg));
90 1.1 cgd msg.msg_iov = iov;
91 1.1 cgd msg.msg_iovlen = 2;
92 1.1 cgd
93 1.1 cgd n = recvmsg(so, &msg, 0);
94 1.1 cgd if (n < 0)
95 1.1 cgd return (errno);
96 1.1 cgd
97 1.1 cgd if (n <= sizeof(*pcr))
98 1.1 cgd return (EINVAL);
99 1.1 cgd
100 1.1 cgd n -= sizeof(*pcr);
101 1.1 cgd key[n] = '\0';
102 1.1 cgd
103 1.1 cgd return (0);
104 1.1 cgd }
105 1.1 cgd
106 1.6 lukem static void
107 1.13 xtraeme send_reply(int so, int fd, int error)
108 1.1 cgd {
109 1.1 cgd int n;
110 1.1 cgd struct iovec iov;
111 1.1 cgd struct msghdr msg;
112 1.9 thorpej void *ctl = NULL;
113 1.9 thorpej struct cmsghdr *cmsg;
114 1.9 thorpej int *files;
115 1.9 thorpej socklen_t cmsgsize;
116 1.1 cgd
117 1.1 cgd /*
118 1.1 cgd * Line up error code. Don't worry about byte ordering
119 1.1 cgd * because we must be sending to the local machine.
120 1.1 cgd */
121 1.1 cgd iov.iov_base = (caddr_t) &error;
122 1.1 cgd iov.iov_len = sizeof(error);
123 1.1 cgd
124 1.1 cgd /*
125 1.1 cgd * Build a msghdr
126 1.1 cgd */
127 1.3 mycroft memset(&msg, 0, sizeof(msg));
128 1.1 cgd msg.msg_iov = &iov;
129 1.1 cgd msg.msg_iovlen = 1;
130 1.1 cgd
131 1.1 cgd /*
132 1.1 cgd * If there is a file descriptor to send then
133 1.1 cgd * construct a suitable rights control message.
134 1.1 cgd */
135 1.1 cgd if (fd >= 0) {
136 1.11 atatat cmsgsize = CMSG_LEN(sizeof(*files));
137 1.9 thorpej
138 1.9 thorpej ctl = malloc(cmsgsize);
139 1.9 thorpej if (ctl == NULL) {
140 1.10 lukem syslog(LOG_WARNING, "malloc control message: %m");
141 1.9 thorpej return;
142 1.9 thorpej }
143 1.9 thorpej memset(ctl, 0, cmsgsize);
144 1.9 thorpej
145 1.9 thorpej cmsg = (struct cmsghdr *) ctl;
146 1.9 thorpej cmsg->cmsg_len = CMSG_LEN(sizeof(int));
147 1.9 thorpej cmsg->cmsg_level = SOL_SOCKET;
148 1.9 thorpej cmsg->cmsg_type = SCM_RIGHTS;
149 1.9 thorpej
150 1.9 thorpej files = (int *)CMSG_DATA(cmsg);
151 1.9 thorpej files[0] = fd;
152 1.9 thorpej
153 1.9 thorpej msg.msg_control = ctl;
154 1.9 thorpej msg.msg_controllen = cmsgsize;
155 1.1 cgd }
156 1.1 cgd
157 1.1 cgd /*
158 1.1 cgd * Send to kernel...
159 1.1 cgd */
160 1.1 cgd if ((n = sendmsg(so, &msg, MSG_EOR)) < 0)
161 1.10 lukem syslog(LOG_WARNING, "send: %m");
162 1.1 cgd #ifdef DEBUG
163 1.1 cgd fprintf(stderr, "sent %d bytes\n", n);
164 1.1 cgd #endif
165 1.1 cgd sleep(1); /*XXX*/
166 1.1 cgd #ifdef notdef
167 1.1 cgd if (shutdown(so, 2) < 0)
168 1.10 lukem syslog(LOG_WARNING, "shutdown: %m");
169 1.1 cgd #endif
170 1.1 cgd /*
171 1.9 thorpej * Throw away the open file descriptor and control
172 1.9 thorpej * message buffer.
173 1.1 cgd */
174 1.9 thorpej if (fd >= 0)
175 1.9 thorpej (void) close(fd);
176 1.9 thorpej if (ctl != NULL)
177 1.9 thorpej free(ctl);
178 1.1 cgd }
179 1.1 cgd
180 1.6 lukem void
181 1.13 xtraeme activate(qelem *q, int so)
182 1.1 cgd {
183 1.1 cgd struct portal_cred pcred;
184 1.1 cgd char key[MAXPATHLEN+1];
185 1.1 cgd int error;
186 1.1 cgd char **v;
187 1.1 cgd int fd = -1;
188 1.1 cgd
189 1.1 cgd /*
190 1.1 cgd * Read the key from the socket
191 1.1 cgd */
192 1.1 cgd error = get_request(so, &pcred, key, sizeof(key));
193 1.1 cgd if (error) {
194 1.10 lukem syslog(LOG_WARNING, "activate: recvmsg: %m");
195 1.1 cgd goto drop;
196 1.1 cgd }
197 1.1 cgd
198 1.1 cgd #ifdef DEBUG
199 1.1 cgd fprintf(stderr, "lookup key %s\n", key);
200 1.1 cgd #endif
201 1.1 cgd
202 1.1 cgd /*
203 1.1 cgd * Find a match in the configuration file
204 1.1 cgd */
205 1.1 cgd v = conf_match(q, key);
206 1.1 cgd
207 1.1 cgd /*
208 1.1 cgd * If a match existed, then find an appropriate portal
209 1.1 cgd * otherwise simply return ENOENT.
210 1.1 cgd */
211 1.1 cgd if (v) {
212 1.14 pooka error = activate_argv(&pcred, key, v, &fd);
213 1.1 cgd if (error)
214 1.1 cgd fd = -1;
215 1.1 cgd else if (fd < 0)
216 1.1 cgd error = -1;
217 1.8 enami } else
218 1.1 cgd error = ENOENT;
219 1.1 cgd
220 1.1 cgd if (error >= 0)
221 1.1 cgd send_reply(so, fd, error);
222 1.1 cgd
223 1.1 cgd drop:;
224 1.1 cgd close(so);
225 1.1 cgd }
226