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