1 /*- 2 * Copyright (c) 2005 Jean-Sebastien Pedron 3 * Copyright (c) 2005 Csaba Henk 4 * 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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/mount.h> 34 #include <sys/uio.h> 35 #include <sys/stat.h> 36 #include <sys/sysctl.h> 37 38 #include <err.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <sysexits.h> 43 #include <unistd.h> 44 #include <fcntl.h> 45 #include <signal.h> 46 #include <getopt.h> 47 #include <libgen.h> 48 #include <limits.h> 49 #include <osreldate.h> 50 #include <paths.h> 51 52 #include "mntopts.h" 53 54 #ifndef FUSE4BSD_VERSION 55 #define FUSE4BSD_VERSION "0.3.9-pre1" 56 #endif 57 58 void __usage_short(void); 59 void usage(void); 60 void helpmsg(void); 61 void showversion(void); 62 int init_backgrounded(void); 63 64 struct mntopt mopts[] = { 65 #define ALTF_PRIVATE 0x01 66 { "private", 0, ALTF_PRIVATE, 1 }, 67 { "neglect_shares", 0, 0x02, 1 }, 68 { "push_symlinks_in", 0, 0x04, 1 }, 69 { "allow_other", 0, 0x08, 1 }, 70 { "default_permissions", 0, 0x10, 1 }, 71 #define ALTF_MAXREAD 0x20 72 { "max_read=", 0, ALTF_MAXREAD, 1 }, 73 #define ALTF_SUBTYPE 0x40 74 { "subtype=", 0, ALTF_SUBTYPE, 1 }, 75 #define ALTF_SYNC_UNMOUNT 0x80 76 { "sync_unmount", 0, ALTF_SYNC_UNMOUNT, 1 }, 77 /* Linux specific options, we silently ignore them */ 78 { "fsname=", 0, 0x00, 1 }, 79 { "fd=", 0, 0x00, 1 }, 80 { "rootmode=", 0, 0x00, 1 }, 81 { "user_id=", 0, 0x00, 1 }, 82 { "group_id=", 0, 0x00, 1 }, 83 { "large_read", 0, 0x00, 1 }, 84 /* "nonempty", just the first two chars are stripped off during parsing */ 85 { "nempty", 0, 0x00, 1 }, 86 MOPT_STDOPTS, 87 MOPT_END 88 }; 89 90 struct mntval { 91 int mv_flag; 92 void *mv_value; 93 int mv_len; 94 }; 95 96 struct mntval mvals[] = { 97 { ALTF_MAXREAD, NULL, 0 }, 98 { ALTF_SUBTYPE, NULL, 0 }, 99 { 0, NULL, 0 } 100 }; 101 102 char *progname; 103 104 #define DEFAULT_MOUNT_FLAGS ALTF_PRIVATE | ALTF_SYNC_UNMOUNT 105 106 int 107 main(int argc, char *argv[]) 108 { 109 struct iovec *iov; 110 int mntflags, iovlen, verbose = 0; 111 char *dev = NULL, *dir = NULL, mntpath[MAXPATHLEN]; 112 char *devo = NULL, *diro = NULL; 113 char ndev[128], fdstr[15]; 114 int i, done = 0, reject_allow_other = 0, safe_level = 0; 115 int altflags = DEFAULT_MOUNT_FLAGS; 116 int __altflags = DEFAULT_MOUNT_FLAGS; 117 int ch = 0; 118 struct mntopt *mo; 119 struct mntval *mv; 120 static struct option longopts[] = { 121 {"reject-allow_other", no_argument, NULL, 'A'}, 122 {"safe", no_argument, NULL, 'S'}, 123 {"daemon", required_argument, NULL, 'D'}, 124 {"daemon_opts", required_argument, NULL, 'O'}, 125 {"special", required_argument, NULL, 's'}, 126 {"mountpath", required_argument, NULL, 'm'}, 127 {"version", no_argument, NULL, 'V'}, 128 {"help", no_argument, NULL, 'h'}, 129 {0,0,0,0} 130 }; 131 int pid = 0; 132 int fd = -1, fdx; 133 char *ep; 134 char *daemon_str = NULL, *daemon_opts = NULL; 135 136 progname = argv[0]; 137 138 /* 139 * We want a parsing routine which is not sensitive to 140 * the position of args/opts; it should extract the 141 * first two args and stop at the beginning of the rest. 142 * (This makes it easier to call mount_fusefs from external 143 * utils than it is with a strict "util flags args" syntax.) 144 */ 145 146 iov = NULL; 147 iovlen = 0; 148 mntflags = 0; 149 /* All in all, I feel it more robust this way... */ 150 unsetenv("POSIXLY_CORRECT"); 151 if (getenv("MOUNT_FUSEFS_IGNORE_UNKNOWN")) 152 getmnt_silent = 1; 153 if (getenv("MOUNT_FUSEFS_VERBOSE")) 154 verbose = 1; 155 156 do { 157 for (i = 0; i < 3; i++) { 158 if (optind < argc && argv[optind][0] != '-') { 159 if (dir) { 160 done = 1; 161 break; 162 } 163 if (dev) 164 dir = argv[optind]; 165 else 166 dev = argv[optind]; 167 optind++; 168 } 169 } 170 switch(ch) { 171 case 'A': 172 reject_allow_other = 1; 173 break; 174 case 'S': 175 safe_level = 1; 176 break; 177 case 'D': 178 if (daemon_str) 179 errx(1, "daemon specified inconsistently"); 180 daemon_str = optarg; 181 break; 182 case 'O': 183 if (daemon_opts) 184 errx(1, "daemon opts specified inconsistently"); 185 daemon_opts = optarg; 186 break; 187 case 'o': 188 getmntopts(optarg, mopts, &mntflags, &altflags); 189 for (mv = mvals; mv->mv_flag; ++mv) { 190 if (! (altflags & mv->mv_flag)) 191 continue; 192 for (mo = mopts; mo->m_flag; ++mo) { 193 char *p, *q; 194 195 if (mo->m_flag != mv->mv_flag) 196 continue; 197 p = strstr(optarg, mo->m_option); 198 if (p) { 199 p += strlen(mo->m_option); 200 q = p; 201 while (*q != '\0' && *q != ',') 202 q++; 203 mv->mv_len = q - p + 1; 204 mv->mv_value = malloc(mv->mv_len); 205 memcpy(mv->mv_value, p, mv->mv_len - 1); 206 ((char *)mv->mv_value)[mv->mv_len - 1] = '\0'; 207 break; 208 } 209 } 210 } 211 break; 212 case 's': 213 if (devo) 214 errx(1, "special specified inconsistently"); 215 devo = optarg; 216 break; 217 case 'm': 218 if (diro) 219 errx(1, "mount path specified inconsistently"); 220 diro = optarg; 221 break; 222 case 'v': 223 verbose = 1; 224 break; 225 case 'h': 226 helpmsg(); 227 break; 228 case 'V': 229 showversion(); 230 break; 231 case '\0': 232 break; 233 case '?': 234 default: 235 usage(); 236 } 237 if (done) 238 break; 239 } while ((ch = getopt_long(argc, argv, "AvVho:SD:O:s:m:", longopts, NULL)) != -1); 240 241 argc -= optind; 242 argv += optind; 243 244 if (devo) { 245 if (dev) 246 errx(1, "special specified inconsistently"); 247 dev = devo; 248 } else if (diro) 249 errx(1, "if mountpoint is given via an option, special should also be given via an option"); 250 251 if (diro) { 252 if (dir) 253 errx(1, "mount path specified inconsistently"); 254 dir = diro; 255 } 256 257 if ((! dev) && argc > 0) { 258 dev = *argv++; 259 argc--; 260 } 261 262 if ((! dir) && argc > 0) { 263 dir = *argv++; 264 argc--; 265 } 266 267 if (! (dev && dir)) 268 errx(1, "missing special and/or mountpoint"); 269 270 for (mo = mopts; mo->m_flag; ++mo) { 271 if (altflags & mo->m_flag) { 272 int iov_done = 0; 273 274 if (reject_allow_other && 275 strcmp(mo->m_option, "allow_other") == 0) 276 /* 277 * reject_allow_other is stronger than a 278 * negative of allow_other: if this is set, 279 * allow_other is blocked, period. 280 */ 281 errx(1, "\"allow_other\" usage is banned by respective option"); 282 283 for (mv = mvals; mv->mv_flag; ++mv) { 284 if (mo->m_flag != mv->mv_flag) 285 continue; 286 if (mv->mv_value) { 287 build_iovec(&iov, &iovlen, mo->m_option, mv->mv_value, mv->mv_len); 288 iov_done = 1; 289 break; 290 } 291 } 292 if (! iov_done) 293 build_iovec(&iov, &iovlen, mo->m_option, 294 __DECONST(void *, ""), -1); 295 } 296 if (__altflags & mo->m_flag) { 297 char *uscore_opt; 298 299 if (asprintf(&uscore_opt, "__%s", mo->m_option) == -1) 300 err(1, "failed to allocate memory"); 301 build_iovec(&iov, &iovlen, uscore_opt, 302 __DECONST(void *, ""), -1); 303 free(uscore_opt); 304 } 305 } 306 307 if (getenv("MOUNT_FUSEFS_SAFE")) 308 safe_level = 1; 309 310 if (safe_level > 0 && (argc > 0 || daemon_str || daemon_opts)) 311 errx(1, "safe mode, spawning daemon not allowed"); 312 313 if ((argc > 0 && (daemon_str || daemon_opts)) || 314 (daemon_opts && ! daemon_str)) 315 errx(1, "daemon specified inconsistently"); 316 317 /* 318 * Resolve the mountpoint with realpath(3) and remove unnecessary 319 * slashes from the devicename if there are any. 320 */ 321 if (checkpath(dir, mntpath) != 0) 322 err(1, "%s", mntpath); 323 (void)rmslashes(dev, dev); 324 325 if (strcmp(dev, "auto") == 0) 326 dev = __DECONST(char *, "/dev/fuse"); 327 328 if (strcmp(dev, "/dev/fuse") == 0) { 329 if (! (argc > 0 || daemon_str)) { 330 fprintf(stderr, "Please also specify the fuse daemon to run when mounting via the multiplexer!\n"); 331 usage(); 332 } 333 if ((fd = open(dev, O_RDWR)) < 0) 334 err(1, "failed to open fuse device"); 335 } else { 336 fdx = strtol(dev, &ep, 10); 337 if (*ep == '\0') 338 fd = fdx; 339 } 340 341 /* Identifying device */ 342 if (fd >= 0) { 343 struct stat sbuf; 344 char *ndevbas, *lep; 345 346 if (fstat(fd, &sbuf) == -1) 347 err(1, "cannot stat device file descriptor"); 348 349 strcpy(ndev, _PATH_DEV); 350 ndevbas = ndev + strlen(_PATH_DEV); 351 devname_r(sbuf.st_rdev, S_IFCHR, ndevbas, 352 sizeof(ndev) - strlen(_PATH_DEV)); 353 354 if (strncmp(ndevbas, "fuse", 4)) 355 errx(1, "mounting inappropriate device"); 356 357 strtol(ndevbas + 4, &lep, 10); 358 if (*lep != '\0') 359 errx(1, "mounting inappropriate device"); 360 361 dev = ndev; 362 } 363 364 if (argc > 0 || daemon_str) { 365 char *fds; 366 367 if (fd < 0 && (fd = open(dev, O_RDWR)) < 0) 368 err(1, "failed to open fuse device"); 369 370 if (asprintf(&fds, "%d", fd) == -1) 371 err(1, "failed to allocate memory"); 372 setenv("FUSE_DEV_FD", fds, 1); 373 free(fds); 374 setenv("FUSE_NO_MOUNT", "1", 1); 375 376 if (daemon_str) { 377 char *bgdaemon; 378 int len; 379 380 if (! daemon_opts) 381 daemon_opts = __DECONST(char *, ""); 382 383 len = strlen(daemon_str) + 1 + strlen(daemon_opts) + 384 2 + 1; 385 bgdaemon = calloc(1, len); 386 387 if (! bgdaemon) 388 err(1, "failed to allocate memory"); 389 390 strlcpy(bgdaemon, daemon_str, len); 391 strlcat(bgdaemon, " ", len); 392 strlcat(bgdaemon, daemon_opts, len); 393 strlcat(bgdaemon, " &", len); 394 395 if (system(bgdaemon)) 396 err(1, "failed to call fuse daemon"); 397 } else { 398 if ((pid = fork()) < 0) 399 err(1, "failed to fork for fuse daemon"); 400 401 if (pid == 0) { 402 execvp(argv[0], argv); 403 err(1, "failed to exec fuse daemon"); 404 } 405 } 406 } 407 408 if (fd >= 0 && ! init_backgrounded() && close(fd) < 0) { 409 if (pid) 410 kill(pid, SIGKILL); 411 err(1, "failed to close fuse device"); 412 } 413 414 /* Prepare the options vector for nmount(). build_iovec() is declared 415 * in mntopts.h. */ 416 sprintf(fdstr, "%d", fd); 417 build_iovec(&iov, &iovlen, "fstype", __DECONST(void *, "fusefs"), -1); 418 build_iovec(&iov, &iovlen, "fspath", mntpath, -1); 419 build_iovec(&iov, &iovlen, "from", dev, -1); 420 build_iovec(&iov, &iovlen, "fd", fdstr, -1); 421 422 if (verbose) 423 fprintf(stderr, "mounting fuse daemon on device %s\n", dev); 424 425 if (nmount(iov, iovlen, mntflags) < 0) 426 err(EX_OSERR, "%s on %s", dev, mntpath); 427 428 exit(0); 429 } 430 431 void 432 __usage_short(void) { 433 fprintf(stderr, 434 "usage:\n%s [-A|-S|-v|-V|-h|-D daemon|-O args|-s special|-m node|-o option...] special node [daemon args...]\n\n", 435 basename(progname)); 436 } 437 438 void 439 usage(void) 440 { 441 struct mntopt *mo; 442 443 __usage_short(); 444 445 fprintf(stderr, "known options:\n"); 446 for (mo = mopts; mo->m_flag; ++mo) 447 fprintf(stderr, "\t%s\n", mo->m_option); 448 449 fprintf(stderr, "\n(use -h for a detailed description of these options)\n"); 450 exit(EX_USAGE); 451 } 452 453 void 454 helpmsg(void) 455 { 456 if (! getenv("MOUNT_FUSEFS_CALL_BY_LIB")) { 457 __usage_short(); 458 fprintf(stderr, "description of options:\n"); 459 } 460 461 /* 462 * The main use case of this function is giving info embedded in general 463 * FUSE lib help output. Therefore the style and the content of the output 464 * tries to fit there as much as possible. 465 */ 466 fprintf(stderr, 467 " -o allow_other allow access to other users\n" 468 /* " -o nonempty allow mounts over non-empty file/dir\n" */ 469 " -o default_permissions enable permission checking by kernel\n" 470 /* 471 " -o fsname=NAME set filesystem name\n" 472 " -o large_read issue large read requests (2.4 only)\n" 473 */ 474 " -o subtype=NAME set filesystem type\n" 475 " -o max_read=N set maximum size of read requests\n" 476 " -o noprivate allow secondary mounting of the filesystem\n" 477 " -o neglect_shares don't report EBUSY when unmount attempted\n" 478 " in presence of secondary mounts\n" 479 " -o push_symlinks_in prefix absolute symlinks with mountpoint\n" 480 " -o sync_unmount do unmount synchronously\n" 481 ); 482 exit(EX_USAGE); 483 } 484 485 void 486 showversion(void) 487 { 488 puts("mount_fusefs [fuse4bsd] version: " FUSE4BSD_VERSION); 489 exit(EX_USAGE); 490 } 491 492 int 493 init_backgrounded(void) 494 { 495 int ibg; 496 size_t len; 497 498 len = sizeof(ibg); 499 500 if (sysctlbyname("vfs.fuse.init_backgrounded", &ibg, &len, NULL, 0)) 501 return (0); 502 503 return (ibg); 504 } 505