mount_portal(8): Raise WARNS to 6.
[dragonfly.git] / sbin / mount_portal / activate.c
CommitLineData
984263bc
MD
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
1de703da
MD
38 *
39 * $FreeBSD: src/sbin/mount_portal/activate.c,v 1.7 1999/08/28 00:13:35 peter Exp $
e5d03018 40 * $DragonFly: src/sbin/mount_portal/activate.c,v 1.6 2007/05/18 17:05:12 dillon Exp $
984263bc
MD
41 */
42
984263bc
MD
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 */
e929265d
SW
58static int
59activate_argv(struct portal_cred *pcr, char *key, char **v, int so, int *fdp)
984263bc
MD
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
e929265d
SW
70static int
71get_request(int so, struct portal_cred *pcr, char *key, int klen)
984263bc
MD
72{
73 struct iovec iov[2];
74 struct msghdr msg;
75 int n;
76
77 iov[0].iov_base = (caddr_t) pcr;
78 iov[0].iov_len = sizeof(*pcr);
79 iov[1].iov_base = key;
80 iov[1].iov_len = klen;
81
82 memset(&msg, 0, sizeof(msg));
83 msg.msg_iov = iov;
84 msg.msg_iovlen = 2;
85
86 n = recvmsg(so, &msg, 0);
87 if (n < 0)
88 return (errno);
89
92f258c4 90 if ((size_t)n <= sizeof(*pcr))
984263bc
MD
91 return (EINVAL);
92
93 n -= sizeof(*pcr);
94 key[n] = '\0';
95
96 return (0);
97}
98
e929265d
SW
99static void
100send_reply(int so, int fd, int error)
984263bc
MD
101{
102 int n;
103 struct iovec iov;
104 struct msghdr msg;
105 struct {
106 struct cmsghdr cmsg;
107 int fd;
108 } ctl;
109
110 /*
111 * Line up error code. Don't worry about byte ordering
112 * because we must be sending to the local machine.
113 */
114 iov.iov_base = (caddr_t) &error;
115 iov.iov_len = sizeof(error);
116
117 /*
118 * Build a msghdr
119 */
120 memset(&msg, 0, sizeof(msg));
121 msg.msg_iov = &iov;
122 msg.msg_iovlen = 1;
123
124 /*
125 * If there is a file descriptor to send then
126 * construct a suitable rights control message.
127 */
128 if (fd >= 0) {
129 ctl.fd = fd;
130 ctl.cmsg.cmsg_len = sizeof(ctl);
131 ctl.cmsg.cmsg_level = SOL_SOCKET;
132 ctl.cmsg.cmsg_type = SCM_RIGHTS;
133 msg.msg_control = (caddr_t) &ctl;
134 msg.msg_controllen = ctl.cmsg.cmsg_len;
135 }
136
137 /*
138 * Send to kernel...
139 */
140 if ((n = sendmsg(so, &msg, 0)) < 0)
141 syslog(LOG_ERR, "send: %s", strerror(errno));
142#ifdef DEBUG
143 fprintf(stderr, "sent %d bytes\n", n);
144#endif
145 sleep(1); /*XXX*/
146#ifdef notdef
e5d03018 147 if (shutdown(so, SHUT_RDWR) < 0)
984263bc
MD
148 syslog(LOG_ERR, "shutdown: %s", strerror(errno));
149#endif
150 /*
151 * Throw away the open file descriptor
152 */
7cee7052 153 close(fd);
984263bc
MD
154}
155
e929265d
SW
156void
157activate(qelem *q, int so)
984263bc
MD
158{
159 struct portal_cred pcred;
160 char key[MAXPATHLEN+1];
161 int error;
162 char **v;
163 int fd = -1;
164
165 /*
166 * Read the key from the socket
167 */
168 error = get_request(so, &pcred, key, sizeof(key));
169 if (error) {
170 syslog(LOG_ERR, "activate: recvmsg: %s", strerror(error));
171 goto drop;
172 }
173
174#ifdef DEBUG
175 fprintf(stderr, "lookup key %s\n", key);
176#endif
177
178 /*
179 * Find a match in the configuration file
180 */
181 v = conf_match(q, key);
182
183 /*
184 * If a match existed, then find an appropriate portal
185 * otherwise simply return ENOENT.
186 */
187 if (v) {
188 error = activate_argv(&pcred, key, v, so, &fd);
189 if (error)
190 fd = -1;
191 else if (fd < 0)
192 error = -1;
193 } else {
194 error = ENOENT;
195 }
196
197 if (error >= 0)
198 send_reply(so, fd, error);
199
200drop:;
201 close(so);
202}