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