1 /* $NetBSD: activate.c,v 1.14 2007/07/02 18:07:44 pooka Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software donated to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
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
38 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: activate.c,v 1.14 2007/07/02 18:07:44 pooka Exp $");
49 #include <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/socket.h>
53 #include <sys/syslog.h>
58 static int get_request(int, struct portal_cred
*, char *, size_t);
59 static void send_reply(int, int, int);
62 * Scan the providers list and call the
63 * appropriate function.
66 activate_argv(struct portal_cred
*pcr
, char *key
, char **v
, int *fdp
)
70 for (pr
= providers
; pr
->pr_match
; pr
++)
71 if (strcmp(v
[0], pr
->pr_match
) == 0)
72 return ((*pr
->pr_func
)(pcr
, key
, v
, fdp
));
78 get_request(int so
, struct portal_cred
*pcr
, char *key
, size_t klen
)
84 iov
[0].iov_base
= (caddr_t
) pcr
;
85 iov
[0].iov_len
= sizeof(*pcr
);
86 iov
[1].iov_base
= key
;
87 iov
[1].iov_len
= klen
;
89 memset(&msg
, 0, sizeof(msg
));
93 n
= recvmsg(so
, &msg
, 0);
97 if (n
<= (ssize_t
)sizeof(*pcr
))
107 send_reply(int so
, int fd
, int error
)
113 struct cmsghdr
*cmsg
;
118 * Line up error code. Don't worry about byte ordering
119 * because we must be sending to the local machine.
121 iov
.iov_base
= (caddr_t
) &error
;
122 iov
.iov_len
= sizeof(error
);
127 memset(&msg
, 0, sizeof(msg
));
132 * If there is a file descriptor to send then
133 * construct a suitable rights control message.
136 cmsgsize
= CMSG_LEN(sizeof(*files
));
138 ctl
= malloc(cmsgsize
);
140 syslog(LOG_WARNING
, "malloc control message: %m");
143 memset(ctl
, 0, cmsgsize
);
145 cmsg
= (struct cmsghdr
*) ctl
;
146 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
147 cmsg
->cmsg_level
= SOL_SOCKET
;
148 cmsg
->cmsg_type
= SCM_RIGHTS
;
150 files
= (int *)CMSG_DATA(cmsg
);
153 msg
.msg_control
= ctl
;
154 msg
.msg_controllen
= cmsgsize
;
160 if ((n
= sendmsg(so
, &msg
, MSG_EOR
)) < 0)
161 syslog(LOG_WARNING
, "send: %m");
163 fprintf(stderr
, "sent %d bytes\n", n
);
167 if (shutdown(so
, 2) < 0)
168 syslog(LOG_WARNING
, "shutdown: %m");
171 * Throw away the open file descriptor and control
181 activate(qelem
*q
, int so
)
183 struct portal_cred pcred
;
184 char key
[MAXPATHLEN
+1];
190 * Read the key from the socket
192 error
= get_request(so
, &pcred
, key
, sizeof(key
));
194 syslog(LOG_WARNING
, "activate: recvmsg: %m");
199 fprintf(stderr
, "lookup key %s\n", key
);
203 * Find a match in the configuration file
205 v
= conf_match(q
, key
);
208 * If a match existed, then find an appropriate portal
209 * otherwise simply return ENOENT.
212 error
= activate_argv(&pcred
, key
, v
, &fd
);
221 send_reply(so
, fd
, error
);