perfused.c revision 1.14 1 /* $NetBSD: perfused.c,v 1.14 2011/08/30 20:17:01 joerg 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
312 DWARNX("unknown debug flag \"%s\"", opt);
313 }
314
315 return retval;
316 }
317
318 /* ARGSUSED0 */
319 static void
320 siginfo_handler(int sig)
321 {
322 static int old_flags = 0;
323 int swap;
324
325 swap = perfuse_diagflags;
326 perfuse_diagflags = old_flags;
327 old_flags = swap;
328
329 DWARNX("debug %sabled", old_flags == 0 ? "en" : "dis");
330
331 return;
332 }
333
334 static int
335 parse_options(int argc, char **argv)
336 {
337 int ch;
338 int foreground = 0;
339 int retval = -1;
340
341 perfuse_diagflags = PDF_FOREGROUND | PDF_SYSLOG;
342
343 while ((ch = getopt(argc, argv, "d:fsi:")) != -1) {
344 switch (ch) {
345 case 'd':
346 perfuse_diagflags |= parse_debug(optarg);
347 break;
348 case 's':
349 if (signal(SIGINFO, siginfo_handler) != 0)
350 DERR(EX_OSERR, "signal failed");
351 break;
352 case 'f':
353 foreground = 1;
354 perfuse_diagflags |= PDF_MISC;
355 break;
356 case 'i':
357 retval = atoi(optarg);
358 foreground = 1;
359 break;
360 default:
361 DERRX(EX_USAGE, "%s [-fs] [-d classes] [-i fd]", argv[0]);
362 break;
363 }
364 }
365
366 if (!foreground)
367 perfuse_diagflags &= ~PDF_FOREGROUND;
368
369 return retval;
370 }
371
372 int
373 main(int argc, char **argv)
374 {
375 int s;
376 int sock_type;
377 socklen_t len;
378
379 s = parse_options(argc, argv);
380
381 if (perfuse_diagflags & PDF_SYSLOG)
382 openlog("perfused", 0, LOG_DAEMON);
383
384 if (!(perfuse_diagflags & PDF_FOREGROUND))
385 if (daemon(0, 0) != 0)
386 DERR(EX_OSERR, "daemon failed");
387
388 if (s != -1) {
389 new_mount(s, PMNT_SOCKPAIR);
390 exit(0);
391 }
392
393 s = perfuse_open_sock();
394
395 #ifdef PERFUSE_DEBUG
396 if (perfuse_diagflags & PDF_MISC)
397 DPRINTF("perfused ready\n");
398 #endif
399 len = sizeof(sock_type);
400 if (getsockopt(s, SOL_SOCKET, SO_TYPE, &sock_type, &len) != 0)
401 DERR(EX_OSERR, "getsockopt SO_TYPE failed");
402
403 switch(sock_type) {
404 case SOCK_DGRAM:
405 new_mount(s, PMNT_DEVFUSE|PMNT_DGRAM);
406 exit(0);
407 break;
408 case SOCK_SEQPACKET:
409 if (listen(s, 0) != 0)
410 DERR(EX_OSERR, "listen failed");
411
412 do {
413 int fd;
414 struct sockaddr_un sun;
415 struct sockaddr *sa;
416
417 len = sizeof(sun);
418 sa = (struct sockaddr *)(void *)&sun;
419 if ((fd = accept(s, sa, &len)) == -1)
420 DERR(EX_OSERR, "accept failed");
421
422 new_mount(fd, PMNT_DEVFUSE);
423 } while (1 /* CONSTCOND */);
424 break;
425 default:
426 DERRX(EX_SOFTWARE, "unexpected so_type %d", sock_type);
427 break;
428 }
429
430 /* NOTREACHED */
431 return 0;
432 }
433