# HG changeset patch # User Rob Landley # Date 1408941983 18000 # Node ID 487716951287b16ff6e42b45eabc90b19890c19a # Parent f46ccbcf3f13c4a23a51494bc82b046c8cf6669c Work in progress snapshot of mount, with fallout to umount. (Not done yet.) diff -r f46ccbcf3f13 -r 487716951287 lib/getmountlist.c --- a/lib/getmountlist.c Sun Aug 24 22:42:47 2014 -0500 +++ b/lib/getmountlist.c Sun Aug 24 23:46:23 2014 -0500 @@ -6,6 +6,129 @@ #include "toys.h" #include +// Realloc *old with oldstring,newstring + +void comma_collate(char **old, char *new) +{ + char *temp, *atold = *old; + + // Only add a comma if old string didn't end with one + if (atold && *atold) { + char *comma = ","; + + if (atold[strlen(atold)-1] == ',') comma = ""; + temp = xmprintf("%s%s%s", atold, comma, new); + } else temp = xstrdup(new); + free (atold); + *old = temp; +} + +// iterate through strings in a comma separated list. +// returns start of next entry or NULL if none +// sets *len to length of entry (not including comma) +// advances *list to start of next entry +char *comma_iterate(char **list, int *len) +{ + char *start = *list, *end; + + if (!*list) return 0; + + if (!(end = strchr(*list, ','))) { + *len = strlen(*list); + *list = 0; + } else *list += (*len = end-start)+1; + + return start; +} + +static void deslash(char *s) +{ + char *o = s; + + while (*s) { + if (*s == '\\') { + int i, oct = 0; + + for (i = 1; i < 4; i++) { + if (!isdigit(s[i])) break; + oct = (oct<<3)+s[i]-'0'; + } + if (i == 4) { + *o++ = oct; + s += i; + continue; + } + } + *o++ = *s++; + } + + *o = 0; +} + +// check all instances of opt and "no"opt in optlist, return true if opt +// found and last instance wasn't no. If clean, remove each instance from list. +int comma_scan(char *optlist, char *opt, int clean) +{ + int optlen = strlen(opt), len, no, got = 0; + + if (optlist) for (;;) { + char *s = comma_iterate(&optlist, &len); + + if (!s) break; + no = 2*(*s == 'n' && s[1] == 'o'); + if (optlen == len+no && !strcmp(opt, s+no)) got = !no; + if (clean) memmove(s, optlist, strlen(optlist)+1); + } + + return got; +} + +// return true if all scanlist options enabled in optlist +int comma_scanall(char *optlist, char *scanlist) +{ + int i = 1; + + for (;;) { + char *opt = comma_iterate(&scanlist, &i), *s = xstrndup(opt, i); + + i = comma_scan(optlist, s, 0); + free(s); + if (!i) break; + } + + return i; +} + +// Check if this type matches list. +// Odd syntax: typelist all yes = if any, typelist all no = if none. + +int mountlist_istype(struct mtab_list *ml, char *typelist) +{ + int len, skip; + char *t; + + if (!typelist) return 1; + + skip = strncmp(typelist, "no", 2); + + for (;;) { + if (!(t = comma_iterate(&typelist, &len))) break; + if (!skip) { + // If one -t starts with "no", the rest must too + if (strncmp(t, "no", 2)) error_exit("bad typelist"); + if (!strncmp(t+2, ml->type, len-2)) { + skip = 1; + break; + } + } else if (!strncmp(t, ml->type, len) && !ml->type[len]) { + skip = 0; + break; + } + } + + return !skip; +} + // Get list of mounted filesystems, including stat and statvfs info. // Returns a reversed list, which is good for finding overmounts and such. @@ -39,6 +162,9 @@ mt->device = stpcpy(mt->dir, me->mnt_dir)+1; mt->opts = stpcpy(mt->device, me->mnt_fsname)+1; strcpy(mt->opts, me->mnt_opts); + + deslash(mt->dir); + deslash(mt->device); } endmntent(fp); diff -r f46ccbcf3f13 -r 487716951287 lib/lib.h --- a/lib/lib.h Sun Aug 24 22:42:47 2014 -0500 +++ b/lib/lib.h Sun Aug 24 23:46:23 2014 -0500 @@ -182,6 +182,11 @@ char type[0]; }; +void comma_collate(char **old, char *new); +char *comma_iterate(char **list, int *len); +int comma_scan(char *optlist, char *opt, int clean); +int comma_scanall(char *optlist, char *scanlist); +int mountlist_istype(struct mtab_list *ml, char *typelist); struct mtab_list *xgetmountlist(char *path); // signal diff -r f46ccbcf3f13 -r 487716951287 toys/lsb/umount.c --- a/toys/lsb/umount.c Sun Aug 24 22:42:47 2014 -0500 +++ b/toys/lsb/umount.c Sun Aug 24 23:46:23 2014 -0500 @@ -37,53 +37,44 @@ char *types; ) -// todo +// todo (done?) // borrow df code to identify filesystem? // umount -a from fstab // umount when getpid() not 0, according to fstab // lookup mount: losetup -d, bind, file, block +// loopback delete +// fstab -o user // TODO -// loopback delete -// fstab -o user - -// Realloc *old with oldstring,newstring - -void comma_collate(char **old, char *new) -{ - char *temp, *atold = *old; - - // Only add a comma if old string didn't end with one - if (atold && *atold) { - char *comma = ","; - - if (atold[strlen(atold)-1] == ',') comma = ""; - temp = xmprintf("%s%s%s", atold, comma, new); - } else temp = xstrdup(new); - free (atold); - *old = temp; -} - -// iterate through strings in a comma separated list. -// returns start of next entry or NULL if none -// sets *len to length of entry (not including comma) -// advances *list to start of next entry -char *comma_iterate(char **list, int *len) -{ - char *start = *list, *end; - - if (!*list) return 0; - - if (!(end = strchr(*list, ','))) { - *len = strlen(*list); - *list = 0; - } else *list += (*len = end-start)+1; - - return start; -} +// swapon, swapoff static void do_umount(char *dir, char *dev, int flags) { + // is it ok for this user to umount this mount? + if (CFG_TOYBOX_SUID && getuid()) { + struct mtab_list *mt = dlist_terminate(xgetmountlist("/etc/fstab")); + int len, user = 0; + + while (mt) { + struct mtab_list *mtemp = mt; + char *s; + + if (!strcmp(mt->dir, dir)) while ((s = comma_iterate(&mt->opts, &len))) { + if (len == 4 && strncmp(s, "user", 4)) user = 1; + else if (len == 6 && strncmp(s, "nouser", 6)) user = 0; + } + + mt = mt->next; + free(mtemp); + } + + if (!user) { + error_msg("not root"); + + return; + } + } + if (!umount2(dir, flags)) { if (toys.optflags & FLAG_v) xprintf("%s unmounted\n", dir); @@ -136,30 +127,8 @@ struct arg_list *tal; for (tal = TT.t; tal; tal = tal->next) comma_collate(&typestr, tal->arg); - for (ml = mlrev; ml; ml = ml->prev) { - if (typestr) { - char *type, *types = typestr; - int len, skip = strncmp(types, "no", 2); - - for (;;) { - if (!(type = comma_iterate(&types, &len))) break; - if (!skip) { - // If one -t starts with "no", the rest must too - if (strncmp(type, "no", 2)) error_exit("bad -t"); - if (!strncmp(type+2, ml->type, len-2)) { - skip = 1; - break; - } - } else if (!strncmp(type, ml->type, len) && !ml->type[len]) { - skip = 0; - break; - } - } - if (skip) continue; - } - - do_umount(ml->dir, ml->device, flags); - } + for (ml = mlrev; ml; ml = ml->prev) + if (mountlist_istype(ml, typestr)) do_umount(ml->dir, ml->device, flags); if (CFG_TOYBOX_FREE) { free(typestr); llist_traverse(mlsave, free); diff -r f46ccbcf3f13 -r 487716951287 toys/pending/mount.c --- a/toys/pending/mount.c Sun Aug 24 22:42:47 2014 -0500 +++ b/toys/pending/mount.c Sun Aug 24 23:46:23 2014 -0500 @@ -6,7 +6,7 @@ * Note: -hV is bad spec, haven't implemented -FsLU yet * no mtab (/proc/mounts does it) so -n is NOP. -USE_MOUNT(NEWTOY(mount, "?>2afnrvwt:o*[-rw]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT)) +USE_MOUNT(NEWTOY(mount, "?O:afnrvwt:o*[-rw]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT)) config MOUNT bool "mount" @@ -18,6 +18,7 @@ mounts. -a mount all entries in /etc/fstab (with -t, only entries of that TYPE) + -O only mount -a entries that have this option -f fake it (don't actually mount) -r read only (same as -o ro) -w read/write (default, same as -o rw) @@ -29,7 +30,8 @@ This mount autodetects loopback mounts (a file on a directory) and bind mounts (file on file, directory on directory), so you don't need - to say --bind or --loop. + to say --bind or --loop. You can also "mount -a /path" to mount everything + in /etc/fstab under /path, even if it's noauto. */ #define FOR_mount @@ -38,14 +40,27 @@ GLOBALS( struct arg_list *optlist; char *type; + char *bigO; unsigned long flags; char *opts; + int okuser; ) -// Strip flags out of comma separated list of options. -// Return flags, -static long parse_opts(char *new, long flags, char **more) +// TODO detect existing identical mount (procfs with different dev name?) +// TODO user, users, owner, group, nofail +// TODO -p (passfd) +// TODO -a -t notype,type2 +// TODO --subtree +// TODO --rbind, -R +// TODO make "mount --bind,ro old new" work (implicit -o remount) +// TODO mount -a +// TODO mount -o remount +// TODO fstab: lookup default options for mount +// TODO implement -v + +// Strip flags out of comma separated list of options, return flags,. +static long flag_opts(char *new, long flags, char **more) { struct { char *name; @@ -53,6 +68,7 @@ } opts[] = { // NOPs (we autodetect --loop and --bind) {"loop", 0}, {"bind", 0}, {"defaults", 0}, {"quiet", 0}, + {"user", 0}, {"nouser", 0}, // checked in fstab, ignored in -o // {"noauto", 0}, {"swap", 0}, {"ro", MS_RDONLY}, {"rw", ~MS_RDONLY}, {"nosuid", MS_NOSUID}, {"suid", ~MS_NOSUID}, @@ -70,7 +86,7 @@ // mand dirsync rec iversion strictatime }; - for (;;) { + if (new) for (;;) { char *comma = strchr(new, ','); int i; @@ -110,17 +126,28 @@ if (toys.optflags & FLAG_f) return; + if (getuid()) { + if (TT.okuser) TT.okuser = 0; + else { + error_msg("'%s' not user mountable in fstab"); + return; + } + } + // Autodetect bind mount or filesystem type - if (!type) { + if (!type || !strcmp(type, "auto")) { struct stat stdev, stdir; + // file on file or dir on dir is a --bind mount. if (!stat(dev, &stdev) && !stat(dir, &stdir) && ((S_ISREG(stdev.st_mode) && S_ISREG(stdir.st_mode)) || (S_ISDIR(stdev.st_mode) && S_ISDIR(stdir.st_mode)))) { flags |= MS_BIND; } else fp = xfopen("/proc/filesystems", "r"); - } + } else if (!strcmp(type, "ignore")) return; + else if (!strcmp(type, "swap")) + toys.exitval |= xpclose(xpopen((char *[]){"swapon", "--", dev, 0}, 0), 0); for (;;) { char *buf = 0; @@ -145,15 +172,17 @@ printf("try '%s' type '%s' on '%s'\n", dev, type, dir); rc = mount(dev, dir, type, flags, opts); - // Looking for bind mounts in autodetect above isn't good enough because - // "mount -t ext2 fs.img dir" is valid, but if you _do_ accept bind mounts - // with -t how do you tell "-t cifs" isn't looking for a block device if - // it's not in /proc/filesystems yet because the module that won't be - // loaded until you try the mount, and if you can't then DEVICE - // existing as a file would cause a false positive loopback mount. + // Trying to autodetect loop mounts like bind mounts above (file on dir) + // isn't good enough because "mount -t ext2 fs.img dir" is valid, but if + // you _do_ accept loop mounts with -t how do you tell "-t cifs" isn't + // looking for a block device if it's not in /proc/filesystems yet + // because the module that won't be loaded until you try the mount, and + // if you can't then DEVICE existing as a file would cause a false + // positive loopback mount (so "touch servername" becomes a potential + // denial of service attack...) // - // Solution: try mount, let the kernel tell us it wanted a block device, - // do the loopback setup and retry the mount. + // Solution: try the mount, let the kernel tell us it wanted a block device, + // then do the loopback setup and retry the mount. if (rc && errno == ENOTBLK) { char *losetup[] = {"losetup", "-fs", dev, 0}; int pipes[2], len; @@ -181,39 +210,71 @@ void mount_main(void) { + char *opts = 0, *dev = 0, *dir = 0, **ss; long flags = MS_SILENT; struct arg_list *o; - char *opts = 0; + struct mtab_list *mtl, *mm; - if (toys.optflags & FLAG_a) { - fprintf(stderr, "not yet\n"); - return; - } - +// TODO what do mount -aw and -ar do? + for (o = TT.optlist; o; o = o->next) flags = flag_opts(o->arg, flags, &opts); if (toys.optflags & FLAG_r) flags |= MS_RDONLY; if (toys.optflags & FLAG_w) flags &= ~MS_RDONLY; - for (o = TT.optlist; o; o = o->next) - flags = parse_opts(o->arg, flags, &opts); + + // Treat each --option as -o option + for (ss = toys.optargs; *ss; ss++) { + if ((*ss)[0] && (*ss)[1]) flags = flag_opts(2+*ss, flags, &opts); + else if (!dev) dev = *ss; + else if (!dir) dir = *ss; + // same message as lib/args.c ">2" which we can't use because --opts count + else error_exit("Max 2 arguments\n"); + } + + if ((toys.optflags & FLAG_a) && dir) error_exit("-a with DIR"); + + // Do we need to do an /etc/fstab trawl? + if (toys.optflags & FLAG_a || !dir || getpid()) { + for (mtl = xgetmountlist("/etc/fstab"); mtl && (mm = dlist_pop(&mtl)); + free(mm)) + { + char *aopts = opts ? xstrdup(opts) : 0; + int aflags; + + if (toys.optflags & FLAG_a) { + if (!mountlist_istype(mtl,TT.type) || !comma_scanall(mtl->opts,TT.bigO)) + continue; + + } else { + if (dir && strcmp(dir, mtl->dir)) continue; + if (dev && strcmp(dev, mtl->device) && (dir || strcmp(dev, mtl->dir))) + continue; + } + + // user only counts from fstab, not opts. + if (comma_scan(mtl->opts, "user", 1)) TT.okuser = 1; + aflags = flag_opts(mtl->opts, flags, &aopts); + + mount_filesystem(mtl->device, mtl->dir, mtl->type, aflags, aopts); + + free(aopts); + } + } // show mounts - if (!toys.optc) { - struct mtab_list *mtl = xgetmountlist(0), *m; - - for (mtl = xgetmountlist(0); mtl && (m = dlist_pop(&mtl)); free(m)) { + if (!dir) { + for (mtl = xgetmountlist(0); mtl && (mm = dlist_pop(&mtl)); free(mm)) { char *s = 0; - if (TT.type && strcmp(TT.type, m->type)) continue; - if (*m->device == '/') s = xabspath(m->device, 0); + if (TT.type && strcmp(TT.type, mm->type)) continue; + if (*mm->device == '/') s = xabspath(mm->device, 0); xprintf("%s on %s type %s (%s)\n", - s ? s : m->device, m->dir, m->type, m->opts); + s ? s : mm->device, mm->dir, mm->type, mm->opts); free(s); } // one argument: from fstab, remount, subtree - } else if (toys.optc == 1) { - fprintf(stderr, "not yet\n"); + } else if (!dev) { + fprintf(stderr, "not yet\n"); // TODO return; // two arguments - } else mount_filesystem(toys.optargs[0], toys.optargs[1], TT.type, - flags, opts ? opts : ""); + } else mount_filesystem(dev, dir, TT.type, flags, opts); }