Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:27:33 dillon 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(pcr, key, v, so, fdp)
59 struct portal_cred *pcr;
60 char *key;
61 char **v;
62 int so;
63 int *fdp;
64 {
65         provider *pr;
66
67         for (pr = providers; pr->pr_match; pr++)
68                 if (strcmp(v[0], pr->pr_match) == 0)
69                         return ((*pr->pr_func)(pcr, key, v, so, fdp));
70
71         return (ENOENT);
72 }
73
74 static int get_request(so, pcr, key, klen)
75 int so;
76 struct portal_cred *pcr;
77 char *key;
78 int klen;
79 {
80         struct iovec iov[2];
81         struct msghdr msg;
82         int n;
83
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;
88
89         memset(&msg, 0, sizeof(msg));
90         msg.msg_iov = iov;
91         msg.msg_iovlen = 2;
92
93         n = recvmsg(so, &msg, 0);
94         if (n < 0)
95                 return (errno);
96
97         if (n <= sizeof(*pcr))
98                 return (EINVAL);
99
100         n -= sizeof(*pcr);
101         key[n] = '\0';
102
103         return (0);
104 }
105
106 static void send_reply(so, fd, error)
107 int so;
108 int fd;
109 int error;
110 {
111         int n;
112         struct iovec iov;
113         struct msghdr msg;
114         struct {
115                 struct cmsghdr cmsg;
116                 int fd;
117         } ctl;
118
119         /*
120          * Line up error code.  Don't worry about byte ordering
121          * because we must be sending to the local machine.
122          */
123         iov.iov_base = (caddr_t) &error;
124         iov.iov_len = sizeof(error);
125
126         /*
127          * Build a msghdr
128          */
129         memset(&msg, 0, sizeof(msg));
130         msg.msg_iov = &iov;
131         msg.msg_iovlen = 1;
132
133         /*
134          * If there is a file descriptor to send then
135          * construct a suitable rights control message.
136          */
137         if (fd >= 0) {
138                 ctl.fd = fd;
139                 ctl.cmsg.cmsg_len = sizeof(ctl);
140                 ctl.cmsg.cmsg_level = SOL_SOCKET;
141                 ctl.cmsg.cmsg_type = SCM_RIGHTS;
142                 msg.msg_control = (caddr_t) &ctl;
143                 msg.msg_controllen = ctl.cmsg.cmsg_len;
144         }
145
146         /*
147          * Send to kernel...
148          */
149         if ((n = sendmsg(so, &msg, 0)) < 0)
150                 syslog(LOG_ERR, "send: %s", strerror(errno));
151 #ifdef DEBUG
152         fprintf(stderr, "sent %d bytes\n", n);
153 #endif
154         sleep(1);       /*XXX*/
155 #ifdef notdef
156         if (shutdown(so, 2) < 0)
157                 syslog(LOG_ERR, "shutdown: %s", strerror(errno));
158 #endif
159         /*
160          * Throw away the open file descriptor
161          */
162         (void) close(fd);
163 }
164
165 void activate(q, so)
166 qelem *q;
167 int so;
168 {
169         struct portal_cred pcred;
170         char key[MAXPATHLEN+1];
171         int error;
172         char **v;
173         int fd = -1;
174
175         /*
176          * Read the key from the socket
177          */
178         error = get_request(so, &pcred, key, sizeof(key));
179         if (error) {
180                 syslog(LOG_ERR, "activate: recvmsg: %s", strerror(error));
181                 goto drop;
182         }
183
184 #ifdef DEBUG
185         fprintf(stderr, "lookup key %s\n", key);
186 #endif
187
188         /*
189          * Find a match in the configuration file
190          */
191         v = conf_match(q, key);
192
193         /*
194          * If a match existed, then find an appropriate portal
195          * otherwise simply return ENOENT.
196          */
197         if (v) {
198                 error = activate_argv(&pcred, key, v, so, &fd);
199                 if (error)
200                         fd = -1;
201                 else if (fd < 0)
202                         error = -1;
203         } else {
204                 error = ENOENT;
205         }
206
207         if (error >= 0)
208                 send_reply(so, fd, error);
209
210 drop:;
211         close(so);
212 }