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