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()
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)
509 return fuse_mnt_umount(progname, mnt, mnt, lazy);
513 static void strip_line(
char *line)
515 char *s = strchr(line,
'#');
518 for (s = line + strlen(line) - 1;
519 s >= line && isspace((
unsigned char) *s); s--);
521 for (s = line; isspace((
unsigned char) *s); s++);
523 memmove(line, s, strlen(s)+1);
526 static void parse_line(
char *line,
int linenum)
529 if (strcmp(line,
"user_allow_other") == 0)
530 user_allow_other = 1;
531 else if (sscanf(line,
"mount_max = %i", &tmp) == 1)
535 "%s: unknown parameter in %s at line %i: '%s'\n",
536 progname, FUSE_CONF, linenum, line);
539 static void read_conf(
void)
541 FILE *fp = fopen(FUSE_CONF,
"r");
546 while (fgets(line,
sizeof(line), fp) != NULL) {
548 if (line[strlen(line)-1] ==
'\n') {
550 parse_line(line, linenum);
554 }
else if(line[strlen(line)-1] ==
'\n') {
555 fprintf(stderr,
"%s: reading %s: line %i too long\n", progname, FUSE_CONF, linenum);
563 fprintf(stderr,
"%s: reading %s: missing newline at end of file\n", progname, FUSE_CONF);
567 }
else if (errno != ENOENT) {
568 fprintf(stderr,
"%s: failed to open %s: %s\n",
569 progname, FUSE_CONF, strerror(errno));
573 static int begins_with(
const char *s,
const char *beg)
575 if (strncmp(s, beg, strlen(beg)) == 0)
588 static struct mount_flags mount_flags[] = {
589 {
"rw", MS_RDONLY, 0, 1},
590 {
"ro", MS_RDONLY, 1, 1},
591 {
"suid", MS_NOSUID, 0, 0},
592 {
"nosuid", MS_NOSUID, 1, 1},
593 {
"dev", MS_NODEV, 0, 0},
594 {
"nodev", MS_NODEV, 1, 1},
595 {
"exec", MS_NOEXEC, 0, 1},
596 {
"noexec", MS_NOEXEC, 1, 1},
597 {
"async", MS_SYNCHRONOUS, 0, 1},
598 {
"sync", MS_SYNCHRONOUS, 1, 1},
599 {
"atime", MS_NOATIME, 0, 1},
600 {
"noatime", MS_NOATIME, 1, 1},
601 {
"dirsync", MS_DIRSYNC, 1, 1},
605 static int find_mount_flag(
const char *s,
unsigned len,
int *on,
int *flag)
609 for (i = 0; mount_flags[i].opt != NULL; i++) {
610 const char *opt = mount_flags[i].opt;
611 if (strlen(opt) == len && strncmp(opt, s, len) == 0) {
612 *on = mount_flags[i].on;
613 *flag = mount_flags[i].flag;
614 if (!mount_flags[i].safe && getuid() != 0) {
617 "%s: unsafe option %s ignored\n",
626 static int add_option(
char **optsp,
const char *opt,
unsigned expand)
630 newopts = strdup(opt);
632 unsigned oldsize = strlen(*optsp);
633 unsigned newsize = oldsize + 1 + strlen(opt) + expand + 1;
634 newopts = (
char *) realloc(*optsp, newsize);
636 sprintf(newopts + oldsize,
",%s", opt);
638 if (newopts == NULL) {
639 fprintf(stderr,
"%s: failed to allocate memory\n", progname);
646 static int get_mnt_opts(
int flags,
char *opts,
char **mnt_optsp)
651 if (!(flags & MS_RDONLY) && add_option(mnt_optsp,
"rw", 0) == -1)
654 for (i = 0; mount_flags[i].opt != NULL; i++) {
655 if (mount_flags[i].on && (flags & mount_flags[i].flag) &&
656 add_option(mnt_optsp, mount_flags[i].opt, 0) == -1)
660 if (add_option(mnt_optsp, opts, 0) == -1)
663 l = strlen(*mnt_optsp);
664 if ((*mnt_optsp)[l-1] ==
',')
665 (*mnt_optsp)[l-1] =
'\0';
667 const char *user = get_user_name();
671 if (add_option(mnt_optsp,
"user=", strlen(user)) == -1)
673 strcat(*mnt_optsp, user);
678 static int opt_eq(
const char *s,
unsigned len,
const char *opt)
680 if(strlen(opt) == len && strncmp(s, opt, len) == 0)
686 static int get_string_opt(
const char *s,
unsigned len,
const char *opt,
690 unsigned opt_len = strlen(opt);
695 *val = (
char *) malloc(len - opt_len + 1);
697 fprintf(stderr,
"%s: failed to allocate memory\n", progname);
704 for (i = 0; i < len; i++) {
705 if (s[i] ==
'\\' && i + 1 < len)
713 static int do_mount(
const char *mnt,
char **typep, mode_t rootmode,
714 int fd,
const char *opts,
const char *dev,
char **sourcep,
718 int flags = MS_NOSUID | MS_NODEV;
720 char *mnt_opts = NULL;
724 char *subtype = NULL;
729 optbuf = (
char *) malloc(strlen(opts) + 128);
731 fprintf(stderr,
"%s: failed to allocate memory\n", progname);
735 for (s = opts, d = optbuf; *s;) {
737 const char *fsname_str =
"fsname=";
738 const char *subtype_str =
"subtype=";
739 for (len = 0; s[len]; len++) {
740 if (s[len] ==
'\\' && s[len + 1])
742 else if (s[len] ==
',')
745 if (begins_with(s, fsname_str)) {
746 if (!get_string_opt(s, len, fsname_str, &fsname))
748 }
else if (begins_with(s, subtype_str)) {
749 if (!get_string_opt(s, len, subtype_str, &subtype))
751 }
else if (opt_eq(s, len,
"blkdev")) {
754 "%s: option blkdev is privileged\n",
759 }
else if (opt_eq(s, len,
"auto_unmount")) {
761 }
else if (!begins_with(s,
"fd=") &&
762 !begins_with(s,
"rootmode=") &&
763 !begins_with(s,
"user_id=") &&
764 !begins_with(s,
"group_id=")) {
768 if (opt_eq(s, len,
"large_read")) {
769 struct utsname utsname;
771 res = uname(&utsname);
773 sscanf(utsname.release,
"%u.%u",
774 &kmaj, &kmin) == 2 &&
775 (kmaj > 2 || (kmaj == 2 && kmin > 4))) {
776 fprintf(stderr,
"%s: note: 'large_read' mount option is deprecated for %i.%i kernels\n", progname, kmaj, kmin);
780 if (getuid() != 0 && !user_allow_other &&
781 (opt_eq(s, len,
"allow_other") ||
782 opt_eq(s, len,
"allow_root"))) {
783 fprintf(stderr,
"%s: option %.*s only allowed if 'user_allow_other' is set in %s\n", progname, len, s, FUSE_CONF);
787 if (find_mount_flag(s, len, &on, &flag)) {
804 res = get_mnt_opts(flags, optbuf, &mnt_opts);
808 sprintf(d,
"fd=%i,rootmode=%o,user_id=%u,group_id=%u",
809 fd, rootmode, getuid(), getgid());
811 source = malloc((fsname ? strlen(fsname) : 0) +
812 (subtype ? strlen(subtype) : 0) + strlen(dev) + 32);
814 type = malloc((subtype ? strlen(subtype) : 0) + 32);
815 if (!type || !source) {
816 fprintf(stderr,
"%s: failed to allocate memory\n", progname);
821 sprintf(type,
"%s.%s", blkdev ?
"fuseblk" :
"fuse", subtype);
823 strcpy(type, blkdev ?
"fuseblk" :
"fuse");
826 strcpy(source, fsname);
828 strcpy(source, subtype ? subtype : dev);
830 res = mount(source, mnt, type, flags, optbuf);
831 if (res == -1 && errno == ENODEV && subtype) {
833 strcpy(type, blkdev ?
"fuseblk" :
"fuse");
836 sprintf(source,
"%s#%s", subtype, fsname);
838 strcpy(source, type);
841 res = mount(source, mnt, type, flags, optbuf);
843 if (res == -1 && errno == EINVAL) {
845 sprintf(d,
"fd=%i,rootmode=%o,user_id=%u",
846 fd, rootmode, getuid());
847 res = mount(source, mnt, type, flags, optbuf);
850 int errno_save = errno;
851 if (blkdev && errno == ENODEV && !fuse_mnt_check_fuseblk())
852 fprintf(stderr,
"%s: 'fuseblk' support missing\n",
855 fprintf(stderr,
"%s: mount failed: %s\n", progname,
856 strerror(errno_save));
861 *mnt_optsp = mnt_opts;
877 static int check_perm(
const char **mntp,
struct stat *stbuf,
int *mountpoint_fd)
880 const char *mnt = *mntp;
881 const char *origmnt = mnt;
883 res = lstat(mnt, stbuf);
885 fprintf(stderr,
"%s: failed to access mountpoint %s: %s\n",
886 progname, mnt, strerror(errno));
894 if (S_ISDIR(stbuf->st_mode)) {
898 "%s: failed to chdir to mountpoint: %s\n",
899 progname, strerror(errno));
903 res = lstat(mnt, stbuf);
906 "%s: failed to access mountpoint %s: %s\n",
907 progname, origmnt, strerror(errno));
911 if ((stbuf->st_mode & S_ISVTX) && stbuf->st_uid != getuid()) {
912 fprintf(stderr,
"%s: mountpoint %s not owned by user\n",
917 res = access(mnt, W_OK);
919 fprintf(stderr,
"%s: user has no write access to mountpoint %s\n",
923 }
else if (S_ISREG(stbuf->st_mode)) {
924 static char procfile[256];
925 *mountpoint_fd = open(mnt, O_WRONLY);
926 if (*mountpoint_fd == -1) {
927 fprintf(stderr,
"%s: failed to open %s: %s\n",
928 progname, mnt, strerror(errno));
931 res = fstat(*mountpoint_fd, stbuf);
934 "%s: failed to access mountpoint %s: %s\n",
935 progname, mnt, strerror(errno));
938 if (!S_ISREG(stbuf->st_mode)) {
940 "%s: mountpoint %s is no longer a regular file\n",
945 sprintf(procfile,
"/proc/self/fd/%i", *mountpoint_fd);
949 "%s: mountpoint %s is not a directory or a regular file\n",
958 static int try_open(
const char *dev,
char **devp,
int silent)
960 int fd = open(dev, O_RDWR);
964 fprintf(stderr,
"%s: failed to allocate memory\n",
969 }
else if (errno == ENODEV ||
973 fprintf(stderr,
"%s: failed to open %s: %s\n", progname, dev,
979 static int try_open_fuse_device(
char **devp)
984 fd = try_open(FUSE_DEV, devp, 0);
989 static int open_fuse_device(
char **devp)
991 int fd = try_open_fuse_device(devp);
996 "%s: fuse device not found, try 'modprobe fuse' first\n",
1003 static int mount_fuse(
const char *mnt,
const char *opts)
1010 char *source = NULL;
1011 char *mnt_opts = NULL;
1012 const char *real_mnt = mnt;
1013 int mountpoint_fd = -1;
1015 fd = open_fuse_device(&dev);
1022 if (getuid() != 0 && mount_max != -1) {
1023 int mount_count = count_fuse_fs();
1024 if (mount_count >= mount_max) {
1025 fprintf(stderr,
"%s: too many FUSE filesystems mounted; mount_max=N can be set in %s\n", progname, FUSE_CONF);
1030 res = check_perm(&real_mnt, &stbuf, &mountpoint_fd);
1033 res = do_mount(real_mnt, &type, stbuf.st_mode & S_IFMT,
1034 fd, opts, dev, &source, &mnt_opts);
1036 if (mountpoint_fd != -1)
1037 close(mountpoint_fd);
1044 fprintf(stderr,
"%s: failed to chdir to '/'\n", progname);
1048 if (geteuid() == 0) {
1049 res = add_mount(source, mnt, type, mnt_opts);
1070 static int send_fd(
int sock_fd,
int fd)
1074 struct cmsghdr *p_cmsg;
1076 size_t cmsgbuf[CMSG_SPACE(
sizeof(fd)) /
sizeof(size_t)];
1080 msg.msg_control = cmsgbuf;
1081 msg.msg_controllen =
sizeof(cmsgbuf);
1082 p_cmsg = CMSG_FIRSTHDR(&msg);
1083 p_cmsg->cmsg_level = SOL_SOCKET;
1084 p_cmsg->cmsg_type = SCM_RIGHTS;
1085 p_cmsg->cmsg_len = CMSG_LEN(
sizeof(fd));
1086 p_fds = (
int *) CMSG_DATA(p_cmsg);
1088 msg.msg_controllen = p_cmsg->cmsg_len;
1089 msg.msg_name = NULL;
1090 msg.msg_namelen = 0;
1096 vec.iov_base = &sendchar;
1097 vec.iov_len =
sizeof(sendchar);
1098 while ((retval = sendmsg(sock_fd, &msg, 0)) == -1 && errno == EINTR);
1100 perror(
"sending file descriptor");
1106 static void usage(
void)
1108 printf(
"%s: [options] mountpoint\n" 1111 " -V print version\n" 1112 " -o opt[,opt...] mount options\n" 1115 " -z lazy unmount\n",
1120 static void show_version(
void)
1122 printf(
"fusermount3 version: %s\n", PACKAGE_VERSION);
1126 int main(
int argc,
char *argv[])
1134 static int unmount = 0;
1135 static int lazy = 0;
1136 static int quiet = 0;
1139 const char *opts =
"";
1141 static const struct option long_opts[] = {
1142 {
"unmount", no_argument, NULL,
'u'},
1143 {
"lazy", no_argument, NULL,
'z'},
1144 {
"quiet", no_argument, NULL,
'q'},
1145 {
"help", no_argument, NULL,
'h'},
1146 {
"version", no_argument, NULL,
'V'},
1149 progname = strdup(argv[0]);
1150 if (progname == NULL) {
1151 fprintf(stderr,
"%s: failed to allocate memory\n", argv[0]);
1155 while ((ch = getopt_long(argc, argv,
"hVo:uzq", long_opts,
1187 if (lazy && !unmount) {
1188 fprintf(stderr,
"%s: -z can only be used with -u\n", progname);
1192 if (optind >= argc) {
1193 fprintf(stderr,
"%s: missing mountpoint argument\n", progname);
1195 }
else if (argc > optind + 1) {
1196 fprintf(stderr,
"%s: extra arguments after the mountpoint\n",
1201 origmnt = argv[optind];
1204 mnt = fuse_mnt_resolve_path(progname, origmnt);
1208 fprintf(stderr,
"%s: failed to chdir to '/'\n", progname);
1220 commfd = getenv(FUSE_COMMFD_ENV);
1221 if (commfd == NULL) {
1222 fprintf(stderr,
"%s: old style mounting not supported\n",
1227 fd = mount_fuse(mnt, opts);
1232 res = send_fd(cfd, fd);
1237 if (!auto_unmount) {
1249 fprintf(stderr,
"%s: failed to chdir to '/'\n", progname);
1253 sigfillset(&sigset);
1254 sigprocmask(SIG_BLOCK, &sigset, NULL);
1260 unsigned char buf[16];
1261 int n = recv(cfd, buf,
sizeof(buf), 0);
1274 res = unmount_fuse(mnt, quiet, lazy);
1276 res = umount2(mnt, lazy ? UMOUNT_DETACH : 0);
1277 if (res == -1 && !quiet)
1279 "%s: failed to unmount %s: %s\n",
1280 progname, mnt, strerror(errno));