perfused.c revision 1.2 1 1.2 manu /* $NetBSD: perfused.c,v 1.2 2010/08/26 13:29:02 manu Exp $ */
2 1.1 manu
3 1.1 manu /*-
4 1.1 manu * Copyright (c) 2010 Emmanuel Dreyfus. All rights reserved.
5 1.1 manu *
6 1.1 manu * Redistribution and use in source and binary forms, with or without
7 1.1 manu * modification, are permitted provided that the following conditions
8 1.1 manu * are met:
9 1.1 manu * 1. Redistributions of source code must retain the above copyright
10 1.1 manu * notice, this list of conditions and the following disclaimer.
11 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 manu * notice, this list of conditions and the following disclaimer in the
13 1.1 manu * documentation and/or other materials provided with the distribution.
14 1.1 manu *
15 1.1 manu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1 manu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1 manu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1 manu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1 manu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1 manu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1 manu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1 manu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1 manu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1 manu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 manu * POSSIBILITY OF SUCH DAMAGE.
26 1.1 manu */
27 1.1 manu
28 1.1 manu #include <stdio.h>
29 1.1 manu #include <unistd.h>
30 1.1 manu #include <stdlib.h>
31 1.1 manu #include <fcntl.h>
32 1.1 manu #include <syslog.h>
33 1.1 manu #include <ctype.h>
34 1.1 manu #include <paths.h>
35 1.1 manu #include <stdarg.h>
36 1.1 manu #include <err.h>
37 1.1 manu #include <errno.h>
38 1.1 manu #include <string.h>
39 1.1 manu #include <sysexits.h>
40 1.1 manu #include <signal.h>
41 1.1 manu #include <puffs.h>
42 1.1 manu #include <sys/wait.h>
43 1.1 manu #include <sys/param.h>
44 1.1 manu #include <sys/queue.h>
45 1.1 manu #include <sys/uio.h>
46 1.1 manu #include <sys/socket.h>
47 1.1 manu #include <sys/un.h>
48 1.1 manu #include <machine/vmparam.h>
49 1.1 manu
50 1.1 manu #include "../../lib/libperfuse/perfuse_if.h"
51 1.1 manu #include "perfused.h"
52 1.1 manu
53 1.1 manu static int getpeerid(int, pid_t *, uid_t *, gid_t *);
54 1.1 manu static int access_mount(const char *, uid_t, int);
55 1.1 manu static int accept_new_mount(int);
56 1.1 manu static int parse_debug(char *);
57 1.1 manu static void siginfo_handler(int);
58 1.1 manu static void parse_options(int, char **);
59 1.1 manu static void get_mount_info(int, struct perfuse_mount_info *);
60 1.1 manu int main(int, char **);
61 1.1 manu
62 1.1 manu
63 1.1 manu static int
64 1.1 manu getpeerid(s, pidp, uidp, gidp)
65 1.1 manu int s;
66 1.1 manu pid_t *pidp;
67 1.1 manu uid_t *uidp;
68 1.1 manu gid_t *gidp;
69 1.1 manu {
70 1.1 manu struct unpcbid unp;
71 1.1 manu socklen_t len;
72 1.1 manu int error;
73 1.1 manu
74 1.1 manu len = sizeof(unp);
75 1.1 manu error = getsockopt(s, 0, LOCAL_PEEREID, &unp, &len);
76 1.1 manu if (error != 0)
77 1.1 manu return error;
78 1.1 manu
79 1.1 manu if (pidp != NULL)
80 1.1 manu *pidp = unp.unp_pid;
81 1.1 manu
82 1.1 manu if (uidp != NULL)
83 1.1 manu *uidp = unp.unp_euid;
84 1.1 manu
85 1.1 manu if (gidp != NULL)
86 1.1 manu *gidp = unp.unp_egid;
87 1.1 manu
88 1.1 manu return 0;
89 1.1 manu }
90 1.1 manu
91 1.1 manu static int
92 1.1 manu access_mount(mnt, uid, ro)
93 1.1 manu const char *mnt;
94 1.1 manu uid_t uid;
95 1.1 manu int ro;
96 1.1 manu {
97 1.1 manu struct stat st;
98 1.1 manu mode_t mode;
99 1.1 manu
100 1.1 manu if (uid == 0)
101 1.1 manu return 0;
102 1.1 manu
103 1.1 manu if (stat(mnt, &st) == -1)
104 1.1 manu return -1;
105 1.1 manu
106 1.1 manu if (st.st_uid != uid)
107 1.1 manu return -1;
108 1.1 manu
109 1.1 manu mode = S_IRUSR;
110 1.1 manu if (!ro)
111 1.1 manu mode |= S_IWUSR;
112 1.1 manu
113 1.1 manu if ((st.st_mode & mode) == mode)
114 1.1 manu return 0;
115 1.1 manu
116 1.1 manu return -1;
117 1.1 manu }
118 1.1 manu
119 1.1 manu static void
120 1.1 manu get_mount_info(fd, pmi)
121 1.1 manu int fd;
122 1.1 manu struct perfuse_mount_info *pmi;
123 1.1 manu {
124 1.1 manu struct perfuse_mount_out *pmo;
125 1.1 manu char *source = NULL;
126 1.1 manu char *target = NULL;
127 1.1 manu char *filesystemtype = NULL;
128 1.1 manu long mountflags = 0;
129 1.1 manu void *data;
130 1.1 manu size_t len;
131 1.1 manu
132 1.1 manu pmo = (struct perfuse_mount_out *)perfuse_recv_early(fd, sizeof(*pmo));
133 1.1 manu if (pmo == NULL) {
134 1.1 manu if (shutdown(fd, SHUT_RDWR) != 0)
135 1.1 manu DERR(EX_OSERR, "shutdown failed");
136 1.1 manu exit(EX_PROTOCOL);
137 1.1 manu }
138 1.1 manu
139 1.1 manu #ifdef PERFUSE_DEBUG
140 1.1 manu DPRINTF("perfuse lengths: source = %d, target = %d, "
141 1.1 manu "filesystemtype = %d, data = %d\n",
142 1.1 manu pmo->pmo_source_len,
143 1.1 manu pmo->pmo_target_len,
144 1.1 manu pmo->pmo_filesystemtype_len,
145 1.1 manu pmo->pmo_data_len);
146 1.1 manu #endif
147 1.1 manu len = pmo->pmo_source_len;
148 1.1 manu source = perfuse_recv_early(fd, len);
149 1.1 manu
150 1.1 manu len = pmo->pmo_target_len;
151 1.1 manu target = perfuse_recv_early(fd, len);
152 1.1 manu
153 1.1 manu len = pmo->pmo_filesystemtype_len;
154 1.1 manu filesystemtype = perfuse_recv_early(fd, len);
155 1.1 manu
156 1.1 manu mountflags = pmo->pmo_mountflags;
157 1.1 manu
158 1.1 manu len = pmo->pmo_data_len;
159 1.1 manu data = perfuse_recv_early(fd, len);
160 1.1 manu
161 1.1 manu #ifdef PERFUSE_DEBUG
162 1.1 manu DPRINTF("%s(\"%s\", \"%s\", \"%s\", 0x%lx, \"%s\")\n", __func__,
163 1.1 manu source, target, filesystemtype, mountflags, (const char *)data);
164 1.1 manu #endif
165 1.1 manu pmi->pmi_source = source;
166 1.1 manu pmi->pmi_target = target;
167 1.1 manu pmi->pmi_filesystemtype = filesystemtype;
168 1.1 manu pmi->pmi_mountflags = mountflags;
169 1.1 manu pmi->pmi_data = data;
170 1.1 manu
171 1.1 manu return;
172 1.1 manu }
173 1.1 manu
174 1.1 manu static int
175 1.1 manu accept_new_mount(s)
176 1.1 manu int s;
177 1.1 manu {
178 1.1 manu struct puffs_usermount *pu;
179 1.1 manu struct sockaddr_storage ss;
180 1.1 manu socklen_t ss_len;
181 1.1 manu struct sockaddr *sa;
182 1.1 manu struct perfuse_mount_info pmi;
183 1.1 manu struct perfuse_callbacks pc;
184 1.1 manu int ro_flag;
185 1.1 manu pid_t pid;
186 1.1 manu int fd;
187 1.1 manu int flags;
188 1.1 manu
189 1.1 manu #ifdef PERFUSE_DEBUG
190 1.1 manu DPRINTF("waiting connexion\n");
191 1.1 manu #endif
192 1.1 manu sa = (struct sockaddr *)(void *)&ss;
193 1.1 manu ss_len = sizeof(ss);
194 1.1 manu if ((fd = accept(s, sa, &ss_len)) == -1)
195 1.1 manu DERR(EX_OSERR, "accept failed");
196 1.1 manu
197 1.1 manu #ifdef PERFUSE_DEBUG
198 1.1 manu DPRINTF("connexion accepted\n");
199 1.1 manu #endif
200 1.1 manu
201 1.1 manu pid = (perfuse_diagflags & PDF_FOREGROUND) ? 0 : fork();
202 1.1 manu switch(pid) {
203 1.1 manu case -1:
204 1.1 manu DERR(EX_OSERR, "cannot fork");
205 1.1 manu break;
206 1.1 manu case 0:
207 1.1 manu break;
208 1.1 manu default:
209 1.1 manu return fd;
210 1.1 manu /* NOTREACHED */
211 1.1 manu break;
212 1.1 manu }
213 1.1 manu
214 1.1 manu /*
215 1.1 manu * Mount information (source, target, mount flags...)
216 1.1 manu */
217 1.1 manu get_mount_info(fd, &pmi);
218 1.1 manu
219 1.1 manu /*
220 1.1 manu * Get peer identity
221 1.1 manu */
222 1.1 manu if (getpeerid(fd, NULL, &pmi.pmi_uid, NULL) != 0)
223 1.1 manu DWARNX("Unable to retreive peer identity");
224 1.1 manu
225 1.1 manu /*
226 1.1 manu * Check that peer owns mountpoint and read (and write) on it?
227 1.1 manu */
228 1.1 manu ro_flag = pmi.pmi_mountflags & MNT_RDONLY;
229 1.1 manu if (access_mount(pmi.pmi_target, pmi.pmi_uid, ro_flag) != 0)
230 1.1 manu DERRX(EX_NOPERM, "insuficient privvileges to mount %s",
231 1.1 manu pmi.pmi_target);
232 1.1 manu
233 1.1 manu
234 1.1 manu /*
235 1.1 manu * Initialize libperfuse, which will initialize libpuffs
236 1.1 manu */
237 1.1 manu pc.pc_new_msg = perfuse_new_pb;
238 1.1 manu pc.pc_xchg_msg = perfuse_xchg_pb;
239 1.1 manu pc.pc_destroy_msg = (perfuse_destroy_msg_fn)puffs_framebuf_destroy;
240 1.1 manu pc.pc_get_inhdr = perfuse_get_inhdr;
241 1.1 manu pc.pc_get_inpayload = perfuse_get_inpayload;
242 1.1 manu pc.pc_get_outhdr = perfuse_get_outhdr;
243 1.1 manu pc.pc_get_outpayload = perfuse_get_outpayload;
244 1.1 manu
245 1.1 manu pu = perfuse_init(&pc, &pmi);
246 1.1 manu
247 1.1 manu puffs_framev_init(pu, perfuse_readframe, perfuse_writeframe,
248 1.1 manu perfuse_cmpframe, perfuse_gotframe, NULL);
249 1.1 manu
250 1.1 manu if (puffs_framev_addfd(pu, fd, PUFFS_FBIO_READ|PUFFS_FBIO_WRITE) == -1)
251 1.1 manu DERR(EX_SOFTWARE, "puffs_framev_addfd failed");
252 1.1 manu
253 1.1 manu perfuse_setspecific(pu, (void *)fd);
254 1.1 manu
255 1.1 manu setproctitle("perfused %s", pmi.pmi_target);
256 1.1 manu (void)kill(getpid(), SIGINFO); /* This is for -s option */
257 1.1 manu
258 1.1 manu perfuse_fs_init(pu);
259 1.1 manu
260 1.1 manu /*
261 1.1 manu * Non blocking I/O on /dev/fuse
262 1.1 manu * This must be done after perfuse_fs_init
263 1.1 manu */
264 1.1 manu if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
265 1.1 manu DERR(EX_OSERR, "fcntl failed");
266 1.1 manu if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) != 0)
267 1.1 manu DERR(EX_OSERR, "fcntl failed");
268 1.1 manu
269 1.1 manu /*
270 1.1 manu * Hand over control to puffs main loop.
271 1.1 manu */
272 1.1 manu return perfuse_mainloop(pu);
273 1.1 manu }
274 1.1 manu
275 1.1 manu static int
276 1.1 manu parse_debug(optstr)
277 1.1 manu char *optstr;
278 1.1 manu {
279 1.1 manu int retval = PDF_SYSLOG;
280 1.1 manu char *opt;
281 1.1 manu char *lastp;
282 1.1 manu
283 1.1 manu for (opt = strtok_r(optstr, ",", &lastp);
284 1.1 manu opt;
285 1.1 manu opt = strtok_r(NULL, ",", &lastp)) {
286 1.1 manu if (strcmp(opt, "fuse") == 0)
287 1.1 manu retval |= PDF_FUSE;
288 1.1 manu else if (strcmp(opt, "puffs") == 0)
289 1.1 manu retval |= PDF_PUFFS;
290 1.1 manu else if (strcmp(opt, "dump") == 0)
291 1.1 manu retval |= PDF_DUMP;
292 1.1 manu else if (strcmp(opt, "fh") == 0)
293 1.1 manu retval |= PDF_FH;
294 1.1 manu else if (strcmp(opt, "readdir") == 0)
295 1.1 manu retval |= PDF_READDIR;
296 1.1 manu else if (strcmp(opt, "reclaim") == 0)
297 1.1 manu retval |= PDF_RECLAIM;
298 1.1 manu else if (strcmp(opt, "requeue") == 0)
299 1.1 manu retval |= PDF_REQUEUE;
300 1.2 manu else if (strcmp(opt, "sync") == 0)
301 1.2 manu retval |= PDF_SYNC;
302 1.1 manu else if (strcmp(opt, "misc") == 0)
303 1.1 manu retval |= PDF_MISC;
304 1.1 manu else
305 1.1 manu DERRX(EX_USAGE, "unknown debug flag \"%s\"", opt);
306 1.1 manu }
307 1.1 manu
308 1.1 manu return retval;
309 1.1 manu }
310 1.1 manu
311 1.1 manu /* ARGSUSED0 */
312 1.1 manu static void
313 1.1 manu siginfo_handler(sig)
314 1.1 manu int sig;
315 1.1 manu {
316 1.1 manu static int old_flags = 0;
317 1.1 manu int swap;
318 1.1 manu
319 1.1 manu swap = perfuse_diagflags;
320 1.1 manu perfuse_diagflags = old_flags;
321 1.1 manu old_flags = swap;
322 1.1 manu
323 1.1 manu DWARNX("debug %sabled", old_flags == 0 ? "en" : "dis");
324 1.1 manu
325 1.1 manu return;
326 1.1 manu }
327 1.1 manu
328 1.1 manu static void
329 1.1 manu parse_options(argc, argv)
330 1.1 manu int argc;
331 1.1 manu char **argv;
332 1.1 manu {
333 1.1 manu int ch;
334 1.1 manu int foreground = 0;
335 1.1 manu
336 1.1 manu perfuse_diagflags = PDF_FOREGROUND | PDF_SYSLOG;
337 1.1 manu
338 1.1 manu while ((ch = getopt(argc, argv, "d:fs")) != -1) {
339 1.1 manu switch (ch) {
340 1.1 manu case 'd':
341 1.1 manu perfuse_diagflags |= parse_debug(optarg);
342 1.1 manu break;
343 1.1 manu case 's':
344 1.1 manu if (signal(SIGINFO, siginfo_handler) != 0)
345 1.1 manu DERR(EX_OSERR, "signal failed");
346 1.1 manu break;
347 1.1 manu case 'f':
348 1.1 manu foreground = 1;
349 1.1 manu break;
350 1.1 manu default:
351 1.1 manu DERR(EX_USAGE, "%s [-d level] [-s] [-f]", argv[0]);
352 1.1 manu break;
353 1.1 manu }
354 1.1 manu }
355 1.1 manu
356 1.1 manu if (!foreground)
357 1.1 manu perfuse_diagflags &= ~PDF_FOREGROUND;
358 1.1 manu
359 1.1 manu return;
360 1.1 manu }
361 1.1 manu
362 1.1 manu int
363 1.1 manu main(argc, argv)
364 1.1 manu int argc;
365 1.1 manu char **argv;
366 1.1 manu {
367 1.1 manu int s;
368 1.1 manu
369 1.1 manu parse_options(argc, argv);
370 1.1 manu
371 1.1 manu if (perfuse_diagflags & PDF_SYSLOG)
372 1.1 manu openlog("perfused", 0, LOG_DAEMON);
373 1.1 manu
374 1.1 manu if (!(perfuse_diagflags & PDF_FOREGROUND))
375 1.1 manu if (daemon(0, 0) != 0)
376 1.1 manu DERR(EX_OSERR, "daemon failed");
377 1.1 manu
378 1.1 manu s = perfuse_open_sock();
379 1.1 manu
380 1.1 manu do {
381 1.1 manu (void)accept_new_mount(s);
382 1.1 manu } while (1 /* CONSTCOND */);
383 1.1 manu
384 1.1 manu /* NOTREACHED */
385 1.1 manu return 0;
386 1.1 manu }
387