]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/mount_portalfs/activate.c
This commit was generated by cvs2svn to compensate for changes in r98503,
[FreeBSD/FreeBSD.git] / sbin / mount_portalfs / 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
40 #ifndef lint
41 static const char rcsid[] =
42   "$FreeBSD$";
43 #endif /* not lint */
44
45 #include <errno.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/socket.h>
51 #include <sys/syslog.h>
52 #include <sys/uio.h>
53
54 #include "portald.h"
55
56 /*
57  * Scan the providers list and call the
58  * appropriate function.
59  */
60 static int activate_argv(pcr, key, v, so, fdp)
61 struct portal_cred *pcr;
62 char *key;
63 char **v;
64 int so;
65 int *fdp;
66 {
67         provider *pr;
68
69         for (pr = providers; pr->pr_match; pr++)
70                 if (strcmp(v[0], pr->pr_match) == 0)
71                         return ((*pr->pr_func)(pcr, key, v, so, fdp));
72
73         return (ENOENT);
74 }
75
76 static int get_request(so, pcr, key, klen)
77 int so;
78 struct portal_cred *pcr;
79 char *key;
80 int klen;
81 {
82         struct iovec iov[2];
83         struct msghdr msg;
84         int n;
85
86         iov[0].iov_base = (caddr_t) pcr;
87         iov[0].iov_len = sizeof(*pcr);
88         iov[1].iov_base = key;
89         iov[1].iov_len = klen;
90
91         memset(&msg, 0, sizeof(msg));
92         msg.msg_iov = iov;
93         msg.msg_iovlen = 2;
94
95         n = recvmsg(so, &msg, 0);
96         if (n < 0)
97                 return (errno);
98
99         if (n <= sizeof(*pcr))
100                 return (EINVAL);
101
102         n -= sizeof(*pcr);
103         key[n] = '\0';
104
105         return (0);
106 }
107
108 static void send_reply(so, fd, error)
109 int so;
110 int fd;
111 int error;
112 {
113         int n;
114         struct iovec iov;
115         struct msghdr msg;
116         union {
117                 struct cmsghdr cmsg;
118                 char control[CMSG_SPACE(sizeof(int))];
119         } ctl;
120
121         /*
122          * Line up error code.  Don't worry about byte ordering
123          * because we must be sending to the local machine.
124          */
125         iov.iov_base = (caddr_t) &error;
126         iov.iov_len = sizeof(error);
127
128         /*
129          * Build a msghdr
130          */
131         memset(&msg, 0, sizeof(msg));
132         msg.msg_iov = &iov;
133         msg.msg_iovlen = 1;
134
135         /*
136          * If there is a file descriptor to send then
137          * construct a suitable rights control message.
138          */
139         if (fd >= 0) {
140                 ctl.cmsg.cmsg_len = CMSG_LEN(sizeof(int));
141                 ctl.cmsg.cmsg_level = SOL_SOCKET;
142                 ctl.cmsg.cmsg_type = SCM_RIGHTS;
143                 *((int *)CMSG_DATA(&ctl.cmsg)) = fd;
144                 msg.msg_control = (caddr_t) &ctl;
145                 msg.msg_controllen = ctl.cmsg.cmsg_len;
146         }
147
148         /*
149          * Send to kernel...
150          */
151         if ((n = sendmsg(so, &msg, 0)) < 0)
152                 syslog(LOG_ERR, "send: %s", strerror(errno));
153 #ifdef DEBUG
154         fprintf(stderr, "sent %d bytes\n", n);
155 #endif
156         sleep(1);       /*XXX*/
157 #ifdef notdef
158         if (shutdown(so, 2) < 0)
159                 syslog(LOG_ERR, "shutdown: %s", strerror(errno));
160 #endif
161         /*
162          * Throw away the open file descriptor
163          */
164         (void) close(fd);
165 }
166
167 void activate(q, so)
168 qelem *q;
169 int so;
170 {
171         struct portal_cred pcred;
172         char key[MAXPATHLEN+1];
173         int error;
174         char **v;
175         int fd = -1;
176
177         /*
178          * Read the key from the socket
179          */
180         error = get_request(so, &pcred, key, sizeof(key));
181         if (error) {
182                 syslog(LOG_ERR, "activate: recvmsg: %s", strerror(error));
183                 goto drop;
184         }
185
186 #ifdef DEBUG
187         fprintf(stderr, "lookup key %s\n", key);
188 #endif
189
190         /*
191          * Find a match in the configuration file
192          */
193         v = conf_match(q, key);
194
195         /*
196          * If a match existed, then find an appropriate portal
197          * otherwise simply return ENOENT.
198          */
199         if (v) {
200                 error = activate_argv(&pcred, key, v, so, &fd);
201                 if (error)
202                         fd = -1;
203                 else if (fd < 0)
204                         error = -1;
205         } else {
206                 error = ENOENT;
207         }
208
209         if (error >= 0)
210                 send_reply(so, fd, error);
211
212 drop:;
213         close(so);
214 }