13 #include "mount_util.h" 27 #include <sys/mount.h> 28 #include <sys/fsuid.h> 29 #include <sys/socket.h> 30 #include <sys/utsname.h> 33 #define FUSE_COMMFD_ENV "_FUSE_COMMFD" 35 #define FUSE_DEV "/dev/fuse" 36 #define FUSE_CONF "/etc/fuse.conf" 39 #define MS_DIRSYNC 128 45 #define MS_PRIVATE (1<<18) 49 #define UMOUNT_DETACH 0x00000002 51 #ifndef UMOUNT_NOFOLLOW 52 #define UMOUNT_NOFOLLOW 0x00000008 55 #define UMOUNT_UNUSED 0x80000000 58 static const char *progname;
60 static int user_allow_other = 0;
61 static int mount_max = 1000;
63 static int auto_unmount = 0;
65 static const char *get_user_name(
void)
67 struct passwd *pw = getpwuid(getuid());
68 if (pw != NULL && pw->pw_name != NULL)
71 fprintf(stderr,
"%s: could not determine username\n", progname);
76 static uid_t oldfsuid;
77 static gid_t oldfsgid;
79 static void drop_privs(
void)
82 oldfsuid = setfsuid(getuid());
83 oldfsgid = setfsgid(getgid());
87 static void restore_privs(
void)
99 static int lock_umount(
void)
101 const char *mtab_lock = _PATH_MOUNTED
".fuselock";
104 struct stat mtab_stat;
107 if (lstat(_PATH_MOUNTED, &mtab_stat) == 0 && S_ISLNK(mtab_stat.st_mode))
110 mtablock = open(mtab_lock, O_RDWR | O_CREAT, 0600);
111 if (mtablock == -1) {
112 fprintf(stderr,
"%s: unable to open fuse lock file: %s\n",
113 progname, strerror(errno));
116 res = lockf(mtablock, F_LOCK, 0);
118 fprintf(stderr,
"%s: error getting lock: %s\n", progname,
127 static void unlock_umount(
int mtablock)
132 res = lockf(mtablock, F_ULOCK, 0);
134 fprintf(stderr,
"%s: error releasing lock: %s\n",
135 progname, strerror(errno));
141 static int add_mount(
const char *source,
const char *mnt,
const char *type,
144 return fuse_mnt_add_mount(progname, source, mnt, type, opts);
147 static int may_unmount(
const char *mnt,
int quiet)
151 const char *user = NULL;
155 const char *mtab = _PATH_MOUNTED;
157 user = get_user_name();
161 fp = setmntent(mtab,
"r");
163 fprintf(stderr,
"%s: failed to open %s: %s\n", progname, mtab,
168 uidlen = sprintf(uidstr,
"%u", getuid());
171 while ((entp = getmntent(fp)) != NULL) {
172 if (!found && strcmp(entp->mnt_dir, mnt) == 0 &&
173 (strcmp(entp->mnt_type,
"fuse") == 0 ||
174 strcmp(entp->mnt_type,
"fuseblk") == 0 ||
175 strncmp(entp->mnt_type,
"fuse.", 5) == 0 ||
176 strncmp(entp->mnt_type,
"fuseblk.", 8) == 0)) {
177 char *p = strstr(entp->mnt_opts,
"user=");
179 (p == entp->mnt_opts || *(p-1) ==
',') &&
180 strcmp(p + 5, user) == 0) {
187 strstr(entp->mnt_opts,
"user_id=")) &&
188 (p == entp->mnt_opts ||
190 strncmp(p + 8, uidstr, uidlen) == 0 &&
191 (*(p+8+uidlen) ==
',' ||
192 *(p+8+uidlen) ==
'\0')) {
203 "%s: entry for %s not found in %s\n",
204 progname, mnt, mtab);
234 static int check_is_mount_child(
void *p)
237 const char *last = a[0];
238 const char *mnt = a[1];
240 const char *procmounts =
"/proc/mounts";
246 res = mount(
"",
"/",
"", MS_PRIVATE | MS_REC, NULL);
248 fprintf(stderr,
"%s: failed to mark mounts private: %s\n",
249 progname, strerror(errno));
253 fp = setmntent(procmounts,
"r");
255 fprintf(stderr,
"%s: failed to open %s: %s\n", progname,
256 procmounts, strerror(errno));
261 while (getmntent(fp) != NULL)
265 fp = setmntent(procmounts,
"r");
267 fprintf(stderr,
"%s: failed to open %s: %s\n", progname,
268 procmounts, strerror(errno));
272 res = mount(
".",
"/",
"", MS_BIND | MS_REC, NULL);
274 fprintf(stderr,
"%s: failed to bind parent to /: %s\n",
275 progname, strerror(errno));
280 while ((entp = getmntent(fp)) != NULL) {
285 if (entp->mnt_dir[0] ==
'/' &&
286 strcmp(entp->mnt_dir + 1, last) == 0) {
294 fprintf(stderr,
"%s: %s not mounted\n", progname, mnt);
301 static pid_t clone_newns(
void *a)
304 char *stack = buf + (
sizeof(buf) / 2 - ((
size_t) buf & 15));
307 extern int __clone2(
int (*fn)(
void *),
308 void *child_stack_base,
size_t stack_size,
309 int flags,
void *arg, pid_t *ptid,
310 void *tls, pid_t *ctid);
312 return __clone2(check_is_mount_child, stack,
sizeof(buf) / 2,
313 CLONE_NEWNS, a, NULL, NULL, NULL);
315 return clone(check_is_mount_child, stack, CLONE_NEWNS, a);
319 static int check_is_mount(
const char *last,
const char *mnt)
323 const char *a[2] = { last, mnt };
325 pid = clone_newns((
void *) a);
326 if (pid == (pid_t) -1) {
327 fprintf(stderr,
"%s: failed to clone namespace: %s\n",
328 progname, strerror(errno));
331 p = waitpid(pid, &status, __WCLONE);
332 if (p == (pid_t) -1) {
333 fprintf(stderr,
"%s: waitpid failed: %s\n",
334 progname, strerror(errno));
337 if (!WIFEXITED(status)) {
338 fprintf(stderr,
"%s: child terminated abnormally (status %i)\n",
342 if (WEXITSTATUS(status) != 0)
348 static int chdir_to_parent(
char *copy,
const char **lastp)
355 tmp = strrchr(copy,
'/');
356 if (tmp == NULL || tmp[1] ==
'\0') {
357 fprintf(stderr,
"%s: internal error: invalid abs path: <%s>\n",
365 }
else if (tmp[1] !=
'\0') {
375 fprintf(stderr,
"%s: failed to chdir to %s: %s\n",
376 progname, parent, strerror(errno));
380 if (getcwd(buf,
sizeof(buf)) == NULL) {
381 fprintf(stderr,
"%s: failed to obtain current directory: %s\n",
382 progname, strerror(errno));
385 if (strcmp(buf, parent) != 0) {
386 fprintf(stderr,
"%s: mountpoint moved (%s -> %s)\n", progname,
396 static int umount_nofollow_support(
void)
398 int res = umount2(
"", UMOUNT_UNUSED);
399 if (res != -1 || errno != EINVAL)
402 res = umount2(
"", UMOUNT_NOFOLLOW);
403 if (res != -1 || errno != ENOENT)
409 static int unmount_fuse_locked(
const char *mnt,
int quiet,
int lazy)
414 int umount_flags = lazy ? UMOUNT_DETACH : 0;
417 res = may_unmount(mnt, quiet);
424 fprintf(stderr,
"%s: failed to allocate memory\n", progname);
428 res = chdir_to_parent(copy, &last);
432 if (umount_nofollow_support()) {
433 umount_flags |= UMOUNT_NOFOLLOW;
435 res = check_is_mount(last, mnt);
440 res = umount2(last, umount_flags);
441 if (res == -1 && !quiet) {
442 fprintf(stderr,
"%s: failed to unmount %s: %s\n",
443 progname, mnt, strerror(errno));
452 fprintf(stderr,
"%s: failed to chdir to '/'\n", progname);
456 return fuse_mnt_remove_mount(progname, mnt);
459 static int unmount_fuse(
const char *mnt,
int quiet,
int lazy)
462 int mtablock = lock_umount();
464 res = unmount_fuse_locked(mnt, quiet, lazy);
465 unlock_umount(mtablock);
470 static int count_fuse_fs(
void)
474 const char *mtab = _PATH_MOUNTED;
475 FILE *fp = setmntent(mtab,
"r");
477 fprintf(stderr,
"%s: failed to open %s: %s\n", progname, mtab,
481 while ((entp = getmntent(fp)) != NULL) {
482 if (strcmp(entp->mnt_type,
"fuse") == 0 ||
483 strncmp(entp->mnt_type,
"fuse.", 5) == 0)
492 static int count_fuse_fs(
void)
497 static int add_mount(
const char *source,
const char *mnt,
const char *type,
507 static int unmount_fuse(
const char *mnt,
int quiet,
int lazy)
510 return fuse_mnt_umount(progname, mnt, mnt, lazy);
514 static void strip_line(
char *line)
516 char *s = strchr(line,
'#');
519 for (s = line + strlen(line) - 1;
520 s >= line && isspace((
unsigned char) *s); s--);
522 for (s = line; isspace((
unsigned char) *s); s++);
524 memmove(line, s, strlen(s)+1);
527 static void parse_line(
char *line,
int linenum)
530 if (strcmp(line,
"user_allow_other") == 0)
531 user_allow_other = 1;
532 else if (sscanf(line,
"mount_max = %i", &tmp) == 1)
536 "%s: unknown parameter in %s at line %i: '%s'\n",
537 progname, FUSE_CONF, linenum, line);
540 static void read_conf(
void)
542 FILE *fp = fopen(FUSE_CONF,
"r");
547 while (fgets(line,
sizeof(line), fp) != NULL) {
549 if (line[strlen(line)-1] ==
'\n') {
551 parse_line(line, linenum);
555 }
else if(line[strlen(line)-1] ==
'\n') {
556 fprintf(stderr,
"%s: reading %s: line %i too long\n", progname, FUSE_CONF, linenum);
564 fprintf(stderr,
"%s: reading %s: missing newline at end of file\n", progname, FUSE_CONF);
568 }
else if (errno != ENOENT) {
569 fprintf(stderr,
"%s: failed to open %s: %s\n",
570 progname, FUSE_CONF, strerror(errno));
574 static int begins_with(
const char *s,
const char *beg)
576 if (strncmp(s, beg, strlen(beg)) == 0)
589 static struct mount_flags mount_flags[] = {
590 {
"rw", MS_RDONLY, 0, 1},
591 {
"ro", MS_RDONLY, 1, 1},
592 {
"suid", MS_NOSUID, 0, 0},
593 {
"nosuid", MS_NOSUID, 1, 1},
594 {
"dev", MS_NODEV, 0, 0},
595 {
"nodev", MS_NODEV, 1, 1},
596 {
"exec", MS_NOEXEC, 0, 1},
597 {
"noexec", MS_NOEXEC, 1, 1},
598 {
"async", MS_SYNCHRONOUS, 0, 1},
599 {
"sync", MS_SYNCHRONOUS, 1, 1},
600 {
"atime", MS_NOATIME, 0, 1},
601 {
"noatime", MS_NOATIME, 1, 1},
602 {
"dirsync", MS_DIRSYNC, 1, 1},
606 static int find_mount_flag(
const char *s,
unsigned len,
int *on,
int *flag)
610 for (i = 0; mount_flags[i].opt != NULL; i++) {
611 const char *opt = mount_flags[i].opt;
612 if (strlen(opt) == len && strncmp(opt, s, len) == 0) {
613 *on = mount_flags[i].on;
614 *flag = mount_flags[i].flag;
615 if (!mount_flags[i].safe && getuid() != 0) {
618 "%s: unsafe option %s ignored\n",
627 static int add_option(
char **optsp,
const char *opt,
unsigned expand)
631 newopts = strdup(opt);
633 unsigned oldsize = strlen(*optsp);
634 unsigned newsize = oldsize + 1 + strlen(opt) + expand + 1;
635 newopts = (
char *) realloc(*optsp, newsize);
637 sprintf(newopts + oldsize,
",%s", opt);
639 if (newopts == NULL) {
640 fprintf(stderr,
"%s: failed to allocate memory\n", progname);
647 static int get_mnt_opts(
int flags,
char *opts,
char **mnt_optsp)
652 if (!(flags & MS_RDONLY) && add_option(mnt_optsp,
"rw", 0) == -1)
655 for (i = 0; mount_flags[i].opt != NULL; i++) {
656 if (mount_flags[i].on && (flags & mount_flags[i].flag) &&
657 add_option(mnt_optsp, mount_flags[i].opt, 0) == -1)
661 if (add_option(mnt_optsp, opts, 0) == -1)
664 l = strlen(*mnt_optsp);
665 if ((*mnt_optsp)[l-1] ==
',')
666 (*mnt_optsp)[l-1] =
'\0';
668 const char *user = get_user_name();
672 if (add_option(mnt_optsp,
"user=", strlen(user)) == -1)
674 strcat(*mnt_optsp, user);
679 static int opt_eq(
const char *s,
unsigned len,
const char *opt)
681 if(strlen(opt) == len && strncmp(s, opt, len) == 0)
687 static int get_string_opt(
const char *s,
unsigned len,
const char *opt,
691 unsigned opt_len = strlen(opt);
696 *val = (
char *) malloc(len - opt_len + 1);
698 fprintf(stderr,
"%s: failed to allocate memory\n", progname);
705 for (i = 0; i < len; i++) {
706 if (s[i] ==
'\\' && i + 1 < len)
714 static int do_mount(
const char *mnt,
char **typep, mode_t rootmode,
715 int fd,
const char *opts,
const char *dev,
char **sourcep,
719 int flags = MS_NOSUID | MS_NODEV;
721 char *mnt_opts = NULL;
725 char *subtype = NULL;
730 optbuf = (
char *) malloc(strlen(opts) + 128);
732 fprintf(stderr,
"%s: failed to allocate memory\n", progname);
736 for (s = opts, d = optbuf; *s;) {
738 const char *fsname_str =
"fsname=";
739 const char *subtype_str =
"subtype=";
740 for (len = 0; s[len]; len++) {
741 if (s[len] ==
'\\' && s[len + 1])
743 else if (s[len] ==
',')
746 if (begins_with(s, fsname_str)) {
747 if (!get_string_opt(s, len, fsname_str, &fsname))
749 }
else if (begins_with(s, subtype_str)) {
750 if (!get_string_opt(s, len, subtype_str, &subtype))
752 }
else if (opt_eq(s, len,
"blkdev")) {
755 "%s: option blkdev is privileged\n",
760 }
else if (opt_eq(s, len,
"auto_unmount")) {
762 }
else if (!begins_with(s,
"fd=") &&
763 !begins_with(s,
"rootmode=") &&
764 !begins_with(s,
"user_id=") &&
765 !begins_with(s,
"group_id=")) {
769 if (opt_eq(s, len,
"large_read")) {
770 struct utsname utsname;
772 res = uname(&utsname);
774 sscanf(utsname.release,
"%u.%u",
775 &kmaj, &kmin) == 2 &&
776 (kmaj > 2 || (kmaj == 2 && kmin > 4))) {
777 fprintf(stderr,
"%s: note: 'large_read' mount option is deprecated for %i.%i kernels\n", progname, kmaj, kmin);
781 if (getuid() != 0 && !user_allow_other &&
782 (opt_eq(s, len,
"allow_other") ||
783 opt_eq(s, len,
"allow_root"))) {
784 fprintf(stderr,
"%s: option %.*s only allowed if 'user_allow_other' is set in %s\n", progname, len, s, FUSE_CONF);
788 if (find_mount_flag(s, len, &on, &flag)) {
805 res = get_mnt_opts(flags, optbuf, &mnt_opts);
809 sprintf(d,
"fd=%i,rootmode=%o,user_id=%u,group_id=%u",
810 fd, rootmode, getuid(), getgid());
812 source = malloc((fsname ? strlen(fsname) : 0) +
813 (subtype ? strlen(subtype) : 0) + strlen(dev) + 32);
815 type = malloc((subtype ? strlen(subtype) : 0) + 32);
816 if (!type || !source) {
817 fprintf(stderr,
"%s: failed to allocate memory\n", progname);
822 sprintf(type,
"%s.%s", blkdev ?
"fuseblk" :
"fuse", subtype);
824 strcpy(type, blkdev ?
"fuseblk" :
"fuse");
827 strcpy(source, fsname);
829 strcpy(source, subtype ? subtype : dev);
831 res = mount(source, mnt, type, flags, optbuf);
832 if (res == -1 && errno == ENODEV && subtype) {
834 strcpy(type, blkdev ?
"fuseblk" :
"fuse");
837 sprintf(source,
"%s#%s", subtype, fsname);
839 strcpy(source, type);
842 res = mount(source, mnt, type, flags, optbuf);
844 if (res == -1 && errno == EINVAL) {
846 sprintf(d,
"fd=%i,rootmode=%o,user_id=%u",
847 fd, rootmode, getuid());
848 res = mount(source, mnt, type, flags, optbuf);
851 int errno_save = errno;
852 if (blkdev && errno == ENODEV && !fuse_mnt_check_fuseblk())
853 fprintf(stderr,
"%s: 'fuseblk' support missing\n",
856 fprintf(stderr,
"%s: mount failed: %s\n", progname,
857 strerror(errno_save));
862 *mnt_optsp = mnt_opts;
878 static int check_perm(
const char **mntp,
struct stat *stbuf,
int *mountpoint_fd)
881 const char *mnt = *mntp;
882 const char *origmnt = mnt;
884 res = lstat(mnt, stbuf);
886 fprintf(stderr,
"%s: failed to access mountpoint %s: %s\n",
887 progname, mnt, strerror(errno));
895 if (S_ISDIR(stbuf->st_mode)) {
899 "%s: failed to chdir to mountpoint: %s\n",
900 progname, strerror(errno));
904 res = lstat(mnt, stbuf);
907 "%s: failed to access mountpoint %s: %s\n",
908 progname, origmnt, strerror(errno));
912 if ((stbuf->st_mode & S_ISVTX) && stbuf->st_uid != getuid()) {
913 fprintf(stderr,
"%s: mountpoint %s not owned by user\n",
918 res = access(mnt, W_OK);
920 fprintf(stderr,
"%s: user has no write access to mountpoint %s\n",
924 }
else if (S_ISREG(stbuf->st_mode)) {
925 static char procfile[256];
926 *mountpoint_fd = open(mnt, O_WRONLY);
927 if (*mountpoint_fd == -1) {
928 fprintf(stderr,
"%s: failed to open %s: %s\n",
929 progname, mnt, strerror(errno));
932 res = fstat(*mountpoint_fd, stbuf);
935 "%s: failed to access mountpoint %s: %s\n",
936 progname, mnt, strerror(errno));
939 if (!S_ISREG(stbuf->st_mode)) {
941 "%s: mountpoint %s is no longer a regular file\n",
946 sprintf(procfile,
"/proc/self/fd/%i", *mountpoint_fd);
950 "%s: mountpoint %s is not a directory or a regular file\n",
959 static int try_open(
const char *dev,
char **devp,
int silent)
961 int fd = open(dev, O_RDWR);
965 fprintf(stderr,
"%s: failed to allocate memory\n",
970 }
else if (errno == ENODEV ||
974 fprintf(stderr,
"%s: failed to open %s: %s\n", progname, dev,
980 static int try_open_fuse_device(
char **devp)
985 fd = try_open(FUSE_DEV, devp, 0);
990 static int open_fuse_device(
char **devp)
992 int fd = try_open_fuse_device(devp);
997 "%s: fuse device not found, try 'modprobe fuse' first\n",
1004 static int mount_fuse(
const char *mnt,
const char *opts)
1011 char *source = NULL;
1012 char *mnt_opts = NULL;
1013 const char *real_mnt = mnt;
1014 int mountpoint_fd = -1;
1016 fd = open_fuse_device(&dev);
1023 if (getuid() != 0 && mount_max != -1) {
1024 int mount_count = count_fuse_fs();
1025 if (mount_count >= mount_max) {
1026 fprintf(stderr,
"%s: too many FUSE filesystems mounted; mount_max=N can be set in %s\n", progname, FUSE_CONF);
1031 res = check_perm(&real_mnt, &stbuf, &mountpoint_fd);
1034 res = do_mount(real_mnt, &type, stbuf.st_mode & S_IFMT,
1035 fd, opts, dev, &source, &mnt_opts);
1037 if (mountpoint_fd != -1)
1038 close(mountpoint_fd);
1045 fprintf(stderr,
"%s: failed to chdir to '/'\n", progname);
1049 if (geteuid() == 0) {
1050 res = add_mount(source, mnt, type, mnt_opts);
1071 static int send_fd(
int sock_fd,
int fd)
1075 struct cmsghdr *p_cmsg;
1077 size_t cmsgbuf[CMSG_SPACE(
sizeof(fd)) /
sizeof(size_t)];
1081 msg.msg_control = cmsgbuf;
1082 msg.msg_controllen =
sizeof(cmsgbuf);
1083 p_cmsg = CMSG_FIRSTHDR(&msg);
1084 p_cmsg->cmsg_level = SOL_SOCKET;
1085 p_cmsg->cmsg_type = SCM_RIGHTS;
1086 p_cmsg->cmsg_len = CMSG_LEN(
sizeof(fd));
1087 p_fds = (
int *) CMSG_DATA(p_cmsg);
1089 msg.msg_controllen = p_cmsg->cmsg_len;
1090 msg.msg_name = NULL;
1091 msg.msg_namelen = 0;
1097 vec.iov_base = &sendchar;
1098 vec.iov_len =
sizeof(sendchar);
1099 while ((retval = sendmsg(sock_fd, &msg, 0)) == -1 && errno == EINTR);
1101 perror(
"sending file descriptor");
1107 static void usage(
void)
1109 printf(
"%s: [options] mountpoint\n" 1112 " -V print version\n" 1113 " -o opt[,opt...] mount options\n" 1116 " -z lazy unmount\n",
1121 static void show_version(
void)
1123 printf(
"fusermount3 version: %s\n", PACKAGE_VERSION);
1127 int main(
int argc,
char *argv[])
1135 static int unmount = 0;
1136 static int lazy = 0;
1137 static int quiet = 0;
1140 const char *opts =
"";
1142 static const struct option long_opts[] = {
1143 {
"unmount", no_argument, NULL,
'u'},
1144 {
"lazy", no_argument, NULL,
'z'},
1145 {
"quiet", no_argument, NULL,
'q'},
1146 {
"help", no_argument, NULL,
'h'},
1147 {
"version", no_argument, NULL,
'V'},
1150 progname = strdup(argv[0]);
1151 if (progname == NULL) {
1152 fprintf(stderr,
"%s: failed to allocate memory\n", argv[0]);
1156 while ((ch = getopt_long(argc, argv,
"hVo:uzq", long_opts,
1188 if (lazy && !unmount) {
1189 fprintf(stderr,
"%s: -z can only be used with -u\n", progname);
1193 if (optind >= argc) {
1194 fprintf(stderr,
"%s: missing mountpoint argument\n", progname);
1196 }
else if (argc > optind + 1) {
1197 fprintf(stderr,
"%s: extra arguments after the mountpoint\n",
1202 origmnt = argv[optind];
1205 mnt = fuse_mnt_resolve_path(progname, origmnt);
1209 fprintf(stderr,
"%s: failed to chdir to '/'\n", progname);
1221 commfd = getenv(FUSE_COMMFD_ENV);
1222 if (commfd == NULL) {
1223 fprintf(stderr,
"%s: old style mounting not supported\n",
1228 fd = mount_fuse(mnt, opts);
1233 res = send_fd(cfd, fd);
1238 if (!auto_unmount) {
1250 fprintf(stderr,
"%s: failed to chdir to '/'\n", progname);
1254 sigfillset(&sigset);
1255 sigprocmask(SIG_BLOCK, &sigset, NULL);
1261 unsigned char buf[16];
1262 int n = recv(cfd, buf,
sizeof(buf), 0);
1275 res = unmount_fuse(mnt, quiet, lazy);
1277 res = umount2(mnt, lazy ? UMOUNT_DETACH : 0);
1278 if (res == -1 && !quiet)
1280 "%s: failed to unmount %s: %s\n",
1281 progname, mnt, strerror(errno));