Home | History | Annotate | Line # | Download | only in perfused
perfused.c revision 1.7
      1 /*  $NetBSD: perfused.c,v 1.7 2010/09/07 02:11:04 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 getpeerid(int, pid_t *, uid_t *, gid_t *);
     54 static int access_mount(const char *, uid_t, int);
     55 static void new_mount(int, int);
     56 static int parse_debug(char *);
     57 static void siginfo_handler(int);
     58 static int parse_options(int, char **);
     59 static void get_mount_info(int, struct perfuse_mount_info *);
     60 int main(int, char **);
     61 
     62 /*
     63  * Flags for new_mount()
     64  */
     65 #define  PMNT_DEVFUSE	0x0	/* We use /dev/fuse */
     66 #define  PMNT_SOCKPAIR	0x1	/* We use socketpair */
     67 
     68 
     69 static int
     70 getpeerid(s, pidp, uidp, gidp)
     71 	int s;
     72 	pid_t *pidp;
     73 	uid_t *uidp;
     74 	gid_t *gidp;
     75 {
     76 	struct unpcbid unp;
     77 	socklen_t len;
     78 	int error;
     79 
     80 	len = sizeof(unp);
     81 	error = getsockopt(s, 0, LOCAL_PEEREID, &unp, &len);
     82 	if (error != 0)
     83 		return error;
     84 
     85 	if (pidp != NULL)
     86 		*pidp = unp.unp_pid;
     87 
     88 	if (uidp != NULL)
     89 		*uidp = unp.unp_euid;
     90 
     91 	if (gidp != NULL)
     92 		*gidp = unp.unp_egid;
     93 
     94 	return 0;
     95 }
     96 
     97 static int
     98 access_mount(mnt, uid, ro)
     99 	const char *mnt;
    100 	uid_t uid;
    101 	int ro;
    102 {
    103 	struct stat st;
    104 	mode_t mode;
    105 
    106 	if (uid == 0)
    107 		return 0;
    108 
    109 	if (stat(mnt, &st) == -1)
    110 		return -1;
    111 
    112 	if (st.st_uid != uid)
    113 		return -1;
    114 
    115 	mode = S_IRUSR;
    116 	if (!ro)
    117 		mode |= S_IWUSR;
    118 
    119 	if ((st.st_mode & mode) == mode)
    120 		return 0;
    121 
    122 	return -1;
    123 }
    124 
    125 static void
    126 get_mount_info(fd, pmi)
    127 	int fd;
    128 	struct perfuse_mount_info *pmi;
    129 {
    130 	struct perfuse_mount_out *pmo;
    131 	char *source = NULL;
    132 	char *target = NULL;
    133 	char *filesystemtype = NULL;
    134 	long mountflags = 0;
    135 	void *data;
    136 	size_t len;
    137 
    138 	pmo = (struct perfuse_mount_out *)perfuse_recv_early(fd, sizeof(*pmo));
    139 	if (pmo == NULL) {
    140 		if (shutdown(fd, SHUT_RDWR) != 0)
    141 			DERR(EX_OSERR, "shutdown failed");
    142 		exit(EX_PROTOCOL);
    143 	}
    144 
    145 #ifdef PERFUSE_DEBUG
    146 	if (perfuse_diagflags & PDF_MISC)
    147 		DPRINTF("perfuse lengths: source = %"PRId32", "
    148 			"target = %"PRId32", filesystemtype = %"PRId32", "
    149 			"data = %"PRId32"\n", pmo->pmo_source_len,
    150 			pmo->pmo_target_len, pmo->pmo_filesystemtype_len,
    151 			pmo->pmo_data_len);
    152 #endif
    153 	len = pmo->pmo_source_len;
    154 	source = perfuse_recv_early(fd, len);
    155 
    156 	len = pmo->pmo_target_len;
    157 	target = perfuse_recv_early(fd, len);
    158 
    159 	len = pmo->pmo_filesystemtype_len;
    160 	filesystemtype = perfuse_recv_early(fd, len);
    161 
    162 	mountflags = pmo->pmo_mountflags;
    163 
    164 	len = pmo->pmo_data_len;
    165 	data = perfuse_recv_early(fd, len);
    166 
    167 #ifdef PERFUSE_DEBUG
    168 	if (perfuse_diagflags & PDF_MISC)
    169 		DPRINTF("%s(\"%s\", \"%s\", \"%s\", 0x%lx, \"%s\")\n",
    170 		__func__, source, target, filesystemtype,
    171 		mountflags, (const char *)data);
    172 #endif
    173 	pmi->pmi_source = source;
    174 	pmi->pmi_target = target;
    175 	pmi->pmi_filesystemtype = filesystemtype;
    176 	pmi->pmi_mountflags = (int)mountflags;
    177 	pmi->pmi_data = data;
    178 
    179 	return;
    180 }
    181 
    182 static void
    183 new_mount(fd, pmnt_flags)
    184 	int fd;
    185 	int pmnt_flags;
    186 {
    187 	struct puffs_usermount *pu;
    188 	struct perfuse_mount_info pmi;
    189 	struct perfuse_callbacks pc;
    190 	int ro_flag;
    191 	pid_t pid;
    192 	int flags;
    193 
    194 
    195 	pid = (perfuse_diagflags & PDF_FOREGROUND) ? 0 : fork();
    196 	switch(pid) {
    197 	case -1:
    198 		DERR(EX_OSERR, "cannot fork");
    199 		break;
    200 	case 0:
    201 		break;
    202 	default:
    203 		return;
    204 		/* NOTREACHED */
    205 		break;
    206 	}
    207 
    208 	/*
    209 	 * Mount information (source, target, mount flags...)
    210 	 */
    211 	get_mount_info(fd, &pmi);
    212 
    213 	/*
    214 	 * Get peer identity. If we use socketpair (-i option),
    215 	 * peer identity if the same as us.
    216 	 */
    217 	if (pmnt_flags & PMNT_SOCKPAIR) {
    218 		pmi.pmi_uid = getuid();
    219 	} else {
    220 		if (getpeerid(fd, NULL, &pmi.pmi_uid, NULL) != 0) {
    221 			DWARNX("Unable to retreive peer identity");
    222 			pmi.pmi_uid = (uid_t)-1;
    223 		}
    224 	}
    225 
    226 	/*
    227 	 * Check that peer owns mountpoint and read (and write) on it?
    228 	 */
    229 	ro_flag = pmi.pmi_mountflags & MNT_RDONLY;
    230 	if (access_mount(pmi.pmi_target, pmi.pmi_uid, ro_flag) != 0)
    231 		DERRX(EX_NOPERM, "insuficient privileges to mount on %s",
    232 		      pmi.pmi_target);
    233 
    234 
    235 	/*
    236 	 * Initialize libperfuse, which will initialize libpuffs
    237 	 */
    238 	pc.pc_new_msg = perfuse_new_pb;
    239 	pc.pc_xchg_msg = perfuse_xchg_pb;
    240 	pc.pc_destroy_msg = (perfuse_destroy_msg_fn)puffs_framebuf_destroy;
    241 	pc.pc_get_inhdr = perfuse_get_inhdr;
    242 	pc.pc_get_inpayload = perfuse_get_inpayload;
    243 	pc.pc_get_outhdr = perfuse_get_outhdr;
    244 	pc.pc_get_outpayload = perfuse_get_outpayload;
    245 
    246 	pu = perfuse_init(&pc, &pmi);
    247 
    248 	puffs_framev_init(pu, perfuse_readframe, perfuse_writeframe,
    249 			  perfuse_cmpframe, perfuse_gotframe, perfuse_fdnotify);
    250 
    251 	if (puffs_framev_addfd(pu, fd, PUFFS_FBIO_READ|PUFFS_FBIO_WRITE) == -1)
    252 		DERR(EX_SOFTWARE, "puffs_framev_addfd failed");
    253 
    254 	perfuse_setspecific(pu, (void *)(long)fd);
    255 
    256 	setproctitle("perfused %s", pmi.pmi_target);
    257 	(void)kill(getpid(), SIGINFO);		/* This is for -s option */
    258 
    259 	perfuse_fs_init(pu);
    260 
    261 	/*
    262 	 * Non blocking I/O on /dev/fuse
    263 	 * This must be done after perfuse_fs_init
    264 	 */
    265 	if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
    266 		DERR(EX_OSERR, "fcntl failed");
    267 	if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) != 0)
    268 		DERR(EX_OSERR, "fcntl failed");
    269 
    270 	/*
    271 	 * Hand over control to puffs main loop.
    272 	 */
    273 	(void)perfuse_mainloop(pu);
    274 
    275 	DERRX(EX_SOFTWARE, "perfuse_mainloop exit");
    276 
    277 	/* NOTREACHED */
    278 	return;
    279 }
    280 
    281 static int
    282 parse_debug(optstr)
    283 	char *optstr;
    284 {
    285 	int retval = PDF_SYSLOG;
    286 	char *opt;
    287 	char *lastp;
    288 
    289 	for (opt = strtok_r(optstr, ",", &lastp);
    290 	     opt;
    291 	     opt = strtok_r(NULL, ",", &lastp)) {
    292 		if (strcmp(opt, "fuse") == 0)
    293 			retval |= PDF_FUSE;
    294 		else if (strcmp(opt, "puffs") == 0)
    295 			retval |= PDF_PUFFS;
    296 		else if (strcmp(opt, "dump") == 0)
    297 			retval |= PDF_DUMP;
    298 		else if (strcmp(opt, "fh") == 0)
    299 			retval |= PDF_FH;
    300 		else if (strcmp(opt, "readdir") == 0)
    301 			retval |= PDF_READDIR;
    302 		else if (strcmp(opt, "reclaim") == 0)
    303 			retval |= PDF_RECLAIM;
    304 		else if (strcmp(opt, "requeue") == 0)
    305 			retval |= PDF_REQUEUE;
    306 		else if (strcmp(opt, "sync") == 0)
    307 			retval |= PDF_SYNC;
    308 		else if (strcmp(opt, "misc") == 0)
    309 			retval |= PDF_MISC;
    310 		else
    311 			DERRX(EX_USAGE, "unknown debug flag \"%s\"", opt);
    312 	}
    313 
    314 	return retval;
    315 }
    316 
    317 /* ARGSUSED0 */
    318 static void
    319 siginfo_handler(sig)
    320 	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(argc, argv)
    336 	int argc;
    337 	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 			DERR(EX_USAGE, "%s [-fs] [-d level] [-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(argc, argv)
    376 	int argc;
    377 	char **argv;
    378 {
    379 	int s;
    380 
    381 	s = parse_options(argc, argv);
    382 
    383 	if (perfuse_diagflags & PDF_SYSLOG)
    384 		openlog("perfused", 0, 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 		DERRX(EX_SOFTWARE, "new_mount exit while -i is used");
    393 	}
    394 
    395 	s = perfuse_open_sock();
    396 
    397 	do {
    398 		struct sockaddr *sa;
    399 		struct sockaddr_storage ss;
    400 		socklen_t ss_len;
    401 		int fd;
    402 
    403 #ifdef PERFUSE_DEBUG
    404 		if (perfuse_diagflags & PDF_MISC)
    405 			DPRINTF("waiting connexion\n");
    406 #endif
    407 		sa = (struct sockaddr *)(void *)&ss;
    408 		ss_len = sizeof(ss);
    409 		if ((fd = accept(s, sa, &ss_len)) == -1)
    410 			DERR(EX_OSERR,  "accept failed");
    411 #ifdef PERFUSE_DEBUG
    412 		if (perfuse_diagflags & PDF_MISC)
    413 			DPRINTF("connexion accepted\n");
    414 #endif
    415 		new_mount(fd, PMNT_DEVFUSE);
    416 	} while (1 /* CONSTCOND */);
    417 
    418 	/* NOTREACHED */
    419 	return 0;
    420 }
    421