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