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