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