You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

644 lines
14 KiB

  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU LGPLv2.
  5. See the file COPYING.LIB.
  6. */
  7. #include "config.h"
  8. #include "fuse_i.h"
  9. #include "fuse_misc.h"
  10. #include "fuse_opt.h"
  11. #include "fuse_common_compat.h"
  12. #include "mount_util.h"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <stddef.h>
  17. #include <string.h>
  18. #include <fcntl.h>
  19. #include <errno.h>
  20. #include <sys/poll.h>
  21. #include <sys/socket.h>
  22. #include <sys/un.h>
  23. #include <sys/wait.h>
  24. #include <sys/mount.h>
  25. #ifdef __NetBSD__
  26. #include <perfuse.h>
  27. #define MS_RDONLY MNT_RDONLY
  28. #define MS_NOSUID MNT_NOSUID
  29. #define MS_NODEV MNT_NODEV
  30. #define MS_NOEXEC MNT_NOEXEC
  31. #define MS_SYNCHRONOUS MNT_SYNCHRONOUS
  32. #define MS_NOATIME MNT_NOATIME
  33. #define umount2(mnt, flags) unmount(mnt, (flags == 2) ? MNT_FORCE : 0)
  34. #endif
  35. #define FUSERMOUNT_PROG "fusermount"
  36. #define FUSE_COMMFD_ENV "_FUSE_COMMFD"
  37. #ifndef HAVE_FORK
  38. #define fork() vfork()
  39. #endif
  40. #ifndef MS_DIRSYNC
  41. #define MS_DIRSYNC 128
  42. #endif
  43. enum {
  44. KEY_KERN_FLAG,
  45. KEY_KERN_OPT,
  46. KEY_FUSERMOUNT_OPT,
  47. KEY_SUBTYPE_OPT,
  48. KEY_MTAB_OPT,
  49. KEY_ALLOW_ROOT,
  50. KEY_RO,
  51. KEY_HELP,
  52. KEY_VERSION,
  53. };
  54. struct mount_opts {
  55. int allow_other;
  56. int allow_root;
  57. int ishelp;
  58. int flags;
  59. int nonempty;
  60. int auto_unmount;
  61. int blkdev;
  62. char *fsname;
  63. char *subtype;
  64. char *subtype_opt;
  65. char *mtab_opts;
  66. char *fusermount_opts;
  67. char *kernel_opts;
  68. };
  69. #define FUSE_MOUNT_OPT(t, p) { t, offsetof(struct mount_opts, p), 1 }
  70. static const struct fuse_opt fuse_mount_opts[] = {
  71. FUSE_MOUNT_OPT("allow_other", allow_other),
  72. FUSE_MOUNT_OPT("allow_root", allow_root),
  73. FUSE_MOUNT_OPT("nonempty", nonempty),
  74. FUSE_MOUNT_OPT("blkdev", blkdev),
  75. FUSE_MOUNT_OPT("auto_unmount", auto_unmount),
  76. FUSE_MOUNT_OPT("fsname=%s", fsname),
  77. FUSE_MOUNT_OPT("subtype=%s", subtype),
  78. FUSE_OPT_KEY("allow_other", KEY_KERN_OPT),
  79. FUSE_OPT_KEY("allow_root", KEY_ALLOW_ROOT),
  80. FUSE_OPT_KEY("nonempty", KEY_FUSERMOUNT_OPT),
  81. FUSE_OPT_KEY("auto_unmount", KEY_FUSERMOUNT_OPT),
  82. FUSE_OPT_KEY("blkdev", KEY_FUSERMOUNT_OPT),
  83. FUSE_OPT_KEY("fsname=", KEY_FUSERMOUNT_OPT),
  84. FUSE_OPT_KEY("subtype=", KEY_SUBTYPE_OPT),
  85. FUSE_OPT_KEY("large_read", KEY_KERN_OPT),
  86. FUSE_OPT_KEY("blksize=", KEY_KERN_OPT),
  87. FUSE_OPT_KEY("default_permissions", KEY_KERN_OPT),
  88. FUSE_OPT_KEY("context=", KEY_KERN_OPT),
  89. FUSE_OPT_KEY("fscontext=", KEY_KERN_OPT),
  90. FUSE_OPT_KEY("defcontext=", KEY_KERN_OPT),
  91. FUSE_OPT_KEY("rootcontext=", KEY_KERN_OPT),
  92. FUSE_OPT_KEY("max_read=", KEY_KERN_OPT),
  93. FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP),
  94. FUSE_OPT_KEY("user=", KEY_MTAB_OPT),
  95. FUSE_OPT_KEY("-r", KEY_RO),
  96. FUSE_OPT_KEY("ro", KEY_KERN_FLAG),
  97. FUSE_OPT_KEY("rw", KEY_KERN_FLAG),
  98. FUSE_OPT_KEY("suid", KEY_KERN_FLAG),
  99. FUSE_OPT_KEY("nosuid", KEY_KERN_FLAG),
  100. FUSE_OPT_KEY("dev", KEY_KERN_FLAG),
  101. FUSE_OPT_KEY("nodev", KEY_KERN_FLAG),
  102. FUSE_OPT_KEY("exec", KEY_KERN_FLAG),
  103. FUSE_OPT_KEY("noexec", KEY_KERN_FLAG),
  104. FUSE_OPT_KEY("async", KEY_KERN_FLAG),
  105. FUSE_OPT_KEY("sync", KEY_KERN_FLAG),
  106. FUSE_OPT_KEY("dirsync", KEY_KERN_FLAG),
  107. FUSE_OPT_KEY("atime", KEY_KERN_FLAG),
  108. FUSE_OPT_KEY("noatime", KEY_KERN_FLAG),
  109. FUSE_OPT_KEY("-h", KEY_HELP),
  110. FUSE_OPT_KEY("--help", KEY_HELP),
  111. FUSE_OPT_KEY("-V", KEY_VERSION),
  112. FUSE_OPT_KEY("--version", KEY_VERSION),
  113. FUSE_OPT_END
  114. };
  115. static void mount_help(void)
  116. {
  117. fprintf(stderr,
  118. " -o allow_other allow access to other users\n"
  119. " -o allow_root allow access to root\n"
  120. " -o auto_unmount auto unmount on process termination\n"
  121. " -o nonempty allow mounts over non-empty file/dir\n"
  122. " -o default_permissions enable permission checking by kernel\n"
  123. " -o fsname=NAME set filesystem name\n"
  124. " -o subtype=NAME set filesystem type\n"
  125. " -o large_read issue large read requests (2.4 only)\n"
  126. " -o max_read=N set maximum size of read requests\n"
  127. "\n");
  128. }
  129. static void exec_fusermount(const char *argv[])
  130. {
  131. execv(FUSERMOUNT_DIR "/" FUSERMOUNT_PROG, (char **) argv);
  132. execvp(FUSERMOUNT_PROG, (char **) argv);
  133. }
  134. static void mount_version(void)
  135. {
  136. int pid = fork();
  137. if (!pid) {
  138. const char *argv[] = { FUSERMOUNT_PROG, "--version", NULL };
  139. exec_fusermount(argv);
  140. _exit(1);
  141. } else if (pid != -1)
  142. waitpid(pid, NULL, 0);
  143. }
  144. struct mount_flags {
  145. const char *opt;
  146. unsigned long flag;
  147. int on;
  148. };
  149. static const struct mount_flags mount_flags[] = {
  150. {"rw", MS_RDONLY, 0},
  151. {"ro", MS_RDONLY, 1},
  152. {"suid", MS_NOSUID, 0},
  153. {"nosuid", MS_NOSUID, 1},
  154. {"dev", MS_NODEV, 0},
  155. {"nodev", MS_NODEV, 1},
  156. {"exec", MS_NOEXEC, 0},
  157. {"noexec", MS_NOEXEC, 1},
  158. {"async", MS_SYNCHRONOUS, 0},
  159. {"sync", MS_SYNCHRONOUS, 1},
  160. {"atime", MS_NOATIME, 0},
  161. {"noatime", MS_NOATIME, 1},
  162. #ifndef __NetBSD__
  163. {"dirsync", MS_DIRSYNC, 1},
  164. #endif
  165. {NULL, 0, 0}
  166. };
  167. static void set_mount_flag(const char *s, int *flags)
  168. {
  169. int i;
  170. for (i = 0; mount_flags[i].opt != NULL; i++) {
  171. const char *opt = mount_flags[i].opt;
  172. if (strcmp(opt, s) == 0) {
  173. if (mount_flags[i].on)
  174. *flags |= mount_flags[i].flag;
  175. else
  176. *flags &= ~mount_flags[i].flag;
  177. return;
  178. }
  179. }
  180. fprintf(stderr, "fuse: internal error, can't find mount flag\n");
  181. abort();
  182. }
  183. static int fuse_mount_opt_proc(void *data, const char *arg, int key,
  184. struct fuse_args *outargs)
  185. {
  186. struct mount_opts *mo = data;
  187. switch (key) {
  188. case KEY_ALLOW_ROOT:
  189. if (fuse_opt_add_opt(&mo->kernel_opts, "allow_other") == -1 ||
  190. fuse_opt_add_arg(outargs, "-oallow_root") == -1)
  191. return -1;
  192. return 0;
  193. case KEY_RO:
  194. arg = "ro";
  195. /* fall through */
  196. case KEY_KERN_FLAG:
  197. set_mount_flag(arg, &mo->flags);
  198. return 0;
  199. case KEY_KERN_OPT:
  200. return fuse_opt_add_opt(&mo->kernel_opts, arg);
  201. case KEY_FUSERMOUNT_OPT:
  202. return fuse_opt_add_opt_escaped(&mo->fusermount_opts, arg);
  203. case KEY_SUBTYPE_OPT:
  204. return fuse_opt_add_opt(&mo->subtype_opt, arg);
  205. case KEY_MTAB_OPT:
  206. return fuse_opt_add_opt(&mo->mtab_opts, arg);
  207. case KEY_HELP:
  208. mount_help();
  209. mo->ishelp = 1;
  210. break;
  211. case KEY_VERSION:
  212. mount_version();
  213. mo->ishelp = 1;
  214. break;
  215. }
  216. return 1;
  217. }
  218. /* return value:
  219. * >= 0 => fd
  220. * -1 => error
  221. */
  222. static int receive_fd(int fd)
  223. {
  224. struct msghdr msg;
  225. struct iovec iov;
  226. char buf[1];
  227. int rv;
  228. size_t ccmsg[CMSG_SPACE(sizeof(int)) / sizeof(size_t)];
  229. struct cmsghdr *cmsg;
  230. iov.iov_base = buf;
  231. iov.iov_len = 1;
  232. memset(&msg, 0, sizeof(msg));
  233. msg.msg_name = 0;
  234. msg.msg_namelen = 0;
  235. msg.msg_iov = &iov;
  236. msg.msg_iovlen = 1;
  237. /* old BSD implementations should use msg_accrights instead of
  238. * msg_control; the interface is different. */
  239. msg.msg_control = ccmsg;
  240. msg.msg_controllen = sizeof(ccmsg);
  241. while(((rv = recvmsg(fd, &msg, 0)) == -1) && errno == EINTR);
  242. if (rv == -1) {
  243. perror("recvmsg");
  244. return -1;
  245. }
  246. if(!rv) {
  247. /* EOF */
  248. return -1;
  249. }
  250. cmsg = CMSG_FIRSTHDR(&msg);
  251. if (cmsg->cmsg_type != SCM_RIGHTS) {
  252. fprintf(stderr, "got control message of unknown type %d\n",
  253. cmsg->cmsg_type);
  254. return -1;
  255. }
  256. return *(int*)CMSG_DATA(cmsg);
  257. }
  258. void fuse_kern_unmount(const char *mountpoint, int fd)
  259. {
  260. int res;
  261. int pid;
  262. if (!mountpoint)
  263. return;
  264. if (fd != -1) {
  265. struct pollfd pfd;
  266. pfd.fd = fd;
  267. pfd.events = 0;
  268. res = poll(&pfd, 1, 0);
  269. /* Need to close file descriptor, otherwise synchronous umount
  270. would recurse into filesystem, and deadlock.
  271. Caller expects fuse_kern_unmount to close the fd, so close it
  272. anyway. */
  273. close(fd);
  274. /* If file poll returns POLLERR on the device file descriptor,
  275. then the filesystem is already unmounted */
  276. if (res == 1 && (pfd.revents & POLLERR))
  277. return;
  278. }
  279. if (geteuid() == 0) {
  280. fuse_mnt_umount("fuse", mountpoint, mountpoint, 1);
  281. return;
  282. }
  283. res = umount2(mountpoint, 2);
  284. if (res == 0)
  285. return;
  286. pid = fork();
  287. if(pid == -1)
  288. return;
  289. if(pid == 0) {
  290. const char *argv[] = { FUSERMOUNT_PROG, "-u", "-q", "-z",
  291. "--", mountpoint, NULL };
  292. exec_fusermount(argv);
  293. _exit(1);
  294. }
  295. waitpid(pid, NULL, 0);
  296. }
  297. void fuse_unmount_compat22(const char *mountpoint)
  298. {
  299. fuse_kern_unmount(mountpoint, -1);
  300. }
  301. static int fuse_mount_fusermount(const char *mountpoint, struct mount_opts *mo,
  302. const char *opts, int quiet)
  303. {
  304. int fds[2], pid;
  305. int res;
  306. int rv;
  307. if (!mountpoint) {
  308. fprintf(stderr, "fuse: missing mountpoint parameter\n");
  309. return -1;
  310. }
  311. res = socketpair(PF_UNIX, SOCK_STREAM, 0, fds);
  312. if(res == -1) {
  313. perror("fuse: socketpair() failed");
  314. return -1;
  315. }
  316. pid = fork();
  317. if(pid == -1) {
  318. perror("fuse: fork() failed");
  319. close(fds[0]);
  320. close(fds[1]);
  321. return -1;
  322. }
  323. if(pid == 0) {
  324. char env[10];
  325. const char *argv[32];
  326. int a = 0;
  327. if (quiet) {
  328. int fd = open("/dev/null", O_RDONLY);
  329. if (fd != -1) {
  330. dup2(fd, 1);
  331. dup2(fd, 2);
  332. }
  333. }
  334. argv[a++] = FUSERMOUNT_PROG;
  335. if (opts) {
  336. argv[a++] = "-o";
  337. argv[a++] = opts;
  338. }
  339. argv[a++] = "--";
  340. argv[a++] = mountpoint;
  341. argv[a++] = NULL;
  342. close(fds[1]);
  343. fcntl(fds[0], F_SETFD, 0);
  344. snprintf(env, sizeof(env), "%i", fds[0]);
  345. setenv(FUSE_COMMFD_ENV, env, 1);
  346. exec_fusermount(argv);
  347. perror("fuse: failed to exec fusermount");
  348. _exit(1);
  349. }
  350. close(fds[0]);
  351. rv = receive_fd(fds[1]);
  352. if (!mo->auto_unmount) {
  353. /* with auto_unmount option fusermount will not exit until
  354. this socket is closed */
  355. close(fds[1]);
  356. waitpid(pid, NULL, 0); /* bury zombie */
  357. }
  358. return rv;
  359. }
  360. int fuse_mount_compat22(const char *mountpoint, const char *opts)
  361. {
  362. struct mount_opts mo;
  363. memset(&mo, 0, sizeof(mo));
  364. mo.flags = MS_NOSUID | MS_NODEV;
  365. return fuse_mount_fusermount(mountpoint, &mo, opts, 0);
  366. }
  367. static int fuse_mount_sys(const char *mnt, struct mount_opts *mo,
  368. const char *mnt_opts)
  369. {
  370. char tmp[128];
  371. const char *devname = "/dev/fuse";
  372. char *source = NULL;
  373. char *type = NULL;
  374. struct stat stbuf;
  375. int fd;
  376. int res;
  377. if (!mnt) {
  378. fprintf(stderr, "fuse: missing mountpoint parameter\n");
  379. return -1;
  380. }
  381. res = stat(mnt, &stbuf);
  382. if (res == -1) {
  383. fprintf(stderr ,"fuse: failed to access mountpoint %s: %s\n",
  384. mnt, strerror(errno));
  385. return -1;
  386. }
  387. if (!mo->nonempty) {
  388. res = fuse_mnt_check_empty("fuse", mnt, stbuf.st_mode,
  389. stbuf.st_size);
  390. if (res == -1)
  391. return -1;
  392. }
  393. if (mo->auto_unmount) {
  394. /* Tell the caller to fallback to fusermount because
  395. auto-unmount does not work otherwise. */
  396. return -2;
  397. }
  398. fd = open(devname, O_RDWR);
  399. if (fd == -1) {
  400. if (errno == ENODEV || errno == ENOENT)
  401. fprintf(stderr, "fuse: device not found, try 'modprobe fuse' first\n");
  402. else
  403. fprintf(stderr, "fuse: failed to open %s: %s\n",
  404. devname, strerror(errno));
  405. return -1;
  406. }
  407. snprintf(tmp, sizeof(tmp), "fd=%i,rootmode=%o,user_id=%u,group_id=%u",
  408. fd, stbuf.st_mode & S_IFMT, getuid(), getgid());
  409. res = fuse_opt_add_opt(&mo->kernel_opts, tmp);
  410. if (res == -1)
  411. goto out_close;
  412. source = malloc((mo->fsname ? strlen(mo->fsname) : 0) +
  413. (mo->subtype ? strlen(mo->subtype) : 0) +
  414. strlen(devname) + 32);
  415. type = malloc((mo->subtype ? strlen(mo->subtype) : 0) + 32);
  416. if (!type || !source) {
  417. fprintf(stderr, "fuse: failed to allocate memory\n");
  418. goto out_close;
  419. }
  420. strcpy(type, mo->blkdev ? "fuseblk" : "fuse");
  421. if (mo->subtype) {
  422. strcat(type, ".");
  423. strcat(type, mo->subtype);
  424. }
  425. strcpy(source,
  426. mo->fsname ? mo->fsname : (mo->subtype ? mo->subtype : devname));
  427. res = mount(source, mnt, type, mo->flags, mo->kernel_opts);
  428. if (res == -1 && errno == ENODEV && mo->subtype) {
  429. /* Probably missing subtype support */
  430. strcpy(type, mo->blkdev ? "fuseblk" : "fuse");
  431. if (mo->fsname) {
  432. if (!mo->blkdev)
  433. sprintf(source, "%s#%s", mo->subtype,
  434. mo->fsname);
  435. } else {
  436. strcpy(source, type);
  437. }
  438. res = mount(source, mnt, type, mo->flags, mo->kernel_opts);
  439. }
  440. if (res == -1) {
  441. /*
  442. * Maybe kernel doesn't support unprivileged mounts, in this
  443. * case try falling back to fusermount
  444. */
  445. if (errno == EPERM) {
  446. res = -2;
  447. } else {
  448. int errno_save = errno;
  449. if (mo->blkdev && errno == ENODEV &&
  450. !fuse_mnt_check_fuseblk())
  451. fprintf(stderr,
  452. "fuse: 'fuseblk' support missing\n");
  453. else
  454. fprintf(stderr, "fuse: mount failed: %s\n",
  455. strerror(errno_save));
  456. }
  457. goto out_close;
  458. }
  459. #ifndef __NetBSD__
  460. #ifndef IGNORE_MTAB
  461. if (geteuid() == 0) {
  462. char *newmnt = fuse_mnt_resolve_path("fuse", mnt);
  463. res = -1;
  464. if (!newmnt)
  465. goto out_umount;
  466. res = fuse_mnt_add_mount("fuse", source, newmnt, type,
  467. mnt_opts);
  468. free(newmnt);
  469. if (res == -1)
  470. goto out_umount;
  471. }
  472. #endif /* IGNORE_MTAB */
  473. #endif /* __NetBSD__ */
  474. free(type);
  475. free(source);
  476. return fd;
  477. out_umount:
  478. umount2(mnt, 2); /* lazy umount */
  479. out_close:
  480. free(type);
  481. free(source);
  482. close(fd);
  483. return res;
  484. }
  485. static int get_mnt_flag_opts(char **mnt_optsp, int flags)
  486. {
  487. int i;
  488. if (!(flags & MS_RDONLY) && fuse_opt_add_opt(mnt_optsp, "rw") == -1)
  489. return -1;
  490. for (i = 0; mount_flags[i].opt != NULL; i++) {
  491. if (mount_flags[i].on && (flags & mount_flags[i].flag) &&
  492. fuse_opt_add_opt(mnt_optsp, mount_flags[i].opt) == -1)
  493. return -1;
  494. }
  495. return 0;
  496. }
  497. int fuse_kern_mount(const char *mountpoint, struct fuse_args *args)
  498. {
  499. struct mount_opts mo;
  500. int res = -1;
  501. char *mnt_opts = NULL;
  502. memset(&mo, 0, sizeof(mo));
  503. mo.flags = MS_NOSUID | MS_NODEV;
  504. if (args &&
  505. fuse_opt_parse(args, &mo, fuse_mount_opts, fuse_mount_opt_proc) == -1)
  506. return -1;
  507. if (mo.allow_other && mo.allow_root) {
  508. fprintf(stderr, "fuse: 'allow_other' and 'allow_root' options are mutually exclusive\n");
  509. goto out;
  510. }
  511. res = 0;
  512. if (mo.ishelp)
  513. goto out;
  514. res = -1;
  515. if (get_mnt_flag_opts(&mnt_opts, mo.flags) == -1)
  516. goto out;
  517. if (mo.kernel_opts && fuse_opt_add_opt(&mnt_opts, mo.kernel_opts) == -1)
  518. goto out;
  519. if (mo.mtab_opts && fuse_opt_add_opt(&mnt_opts, mo.mtab_opts) == -1)
  520. goto out;
  521. res = fuse_mount_sys(mountpoint, &mo, mnt_opts);
  522. if (res == -2) {
  523. if (mo.fusermount_opts &&
  524. fuse_opt_add_opt(&mnt_opts, mo.fusermount_opts) == -1)
  525. goto out;
  526. if (mo.subtype) {
  527. char *tmp_opts = NULL;
  528. res = -1;
  529. if (fuse_opt_add_opt(&tmp_opts, mnt_opts) == -1 ||
  530. fuse_opt_add_opt(&tmp_opts, mo.subtype_opt) == -1) {
  531. free(tmp_opts);
  532. goto out;
  533. }
  534. res = fuse_mount_fusermount(mountpoint, &mo, tmp_opts, 1);
  535. free(tmp_opts);
  536. if (res == -1)
  537. res = fuse_mount_fusermount(mountpoint, &mo,
  538. mnt_opts, 0);
  539. } else {
  540. res = fuse_mount_fusermount(mountpoint, &mo, mnt_opts, 0);
  541. }
  542. }
  543. out:
  544. free(mnt_opts);
  545. free(mo.fsname);
  546. free(mo.subtype);
  547. free(mo.fusermount_opts);
  548. free(mo.subtype_opt);
  549. free(mo.kernel_opts);
  550. free(mo.mtab_opts);
  551. return res;
  552. }
  553. FUSE_SYMVER(".symver fuse_mount_compat22,fuse_mount@FUSE_2.2");
  554. FUSE_SYMVER(".symver fuse_unmount_compat22,fuse_unmount@FUSE_2.2");