]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/mount.c
68b57eb36d7e26cf3cfd0ff33d10a171b486e4d1
[thirdparty/util-linux.git] / sys-utils / mount.c
1 /*
2 * mount(8) -- mount a filesystem
3 *
4 * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
5 * Written by Karel Zak <kzak@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it would be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <getopt.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <stdarg.h>
32 #include <libmount.h>
33 #include <ctype.h>
34
35 #include "nls.h"
36 #include "c.h"
37 #include "env.h"
38 #include "strutils.h"
39 #include "closestream.h"
40 #include "canonicalize.h"
41 #include "pathnames.h"
42
43 #define XALLOC_EXIT_CODE MNT_EX_SYSERR
44 #include "xalloc.h"
45
46 #define OPTUTILS_EXIT_CODE MNT_EX_USAGE
47 #include "optutils.h"
48
49 static struct ul_env_list *envs_removed;
50
51 static int mk_exit_code(struct libmnt_context *cxt, int rc);
52
53 static void suid_drop(struct libmnt_context *cxt)
54 {
55 const uid_t ruid = getuid();
56 const uid_t euid = geteuid();
57
58 if (ruid != 0 && euid == 0 && drop_permissions() != 0)
59 err(MNT_EX_FAIL, _("drop permissions failed"));
60
61 /* be paranoid and check it, setuid(0) has to fail */
62 if (ruid != 0 && setuid(0) == 0)
63 errx(MNT_EX_FAIL, _("drop permissions failed."));
64
65 mnt_context_force_unrestricted(cxt);
66
67 /* restore "bad" environment variables */
68 if (envs_removed) {
69 env_list_setenv(envs_removed);
70 env_list_free(envs_removed);
71 envs_removed = NULL;
72 }
73 }
74
75 static void __attribute__((__noreturn__)) mount_print_version(void)
76 {
77 const char *ver = NULL;
78 const char **features = NULL, **p;
79
80 mnt_get_library_version(&ver);
81 mnt_get_library_features(&features);
82
83 printf(_("%s from %s (libmount %s"),
84 program_invocation_short_name,
85 PACKAGE_STRING,
86 ver);
87 p = features;
88 while (p && *p) {
89 fputs(p == features ? ": " : ", ", stdout);
90 fputs(*p++, stdout);
91 }
92 fputs(")\n", stdout);
93 exit(MNT_EX_SUCCESS);
94 }
95
96 static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)),
97 const char *filename, int line)
98 {
99 if (filename)
100 warnx(_("%s: parse error at line %d -- ignored"), filename, line);
101 return 1;
102 }
103
104 /*
105 * Replace control chars with '?' to be compatible with coreutils. For more
106 * robust solution use findmnt(1) where we use \x?? hex encoding.
107 */
108 static void safe_fputs(const char *data)
109 {
110 const char *p;
111
112 for (p = data; p && *p; p++) {
113 if (iscntrl((unsigned char) *p))
114 fputc('?', stdout);
115 else
116 fputc(*p, stdout);
117 }
118 }
119
120 static void print_all(struct libmnt_context *cxt, char *pattern, int show_label)
121 {
122 struct libmnt_table *tb;
123 struct libmnt_iter *itr = NULL;
124 struct libmnt_fs *fs;
125 struct libmnt_cache *cache = NULL;
126
127 mnt_context_enable_noautofs(cxt, 1);
128
129 if (mnt_context_get_mtab(cxt, &tb))
130 err(MNT_EX_SYSERR, _("failed to read mtab"));
131
132 itr = mnt_new_iter(MNT_ITER_FORWARD);
133 if (!itr)
134 err(MNT_EX_SYSERR, _("failed to initialize libmount iterator"));
135 if (show_label)
136 cache = mnt_new_cache();
137
138 while (mnt_table_next_fs(tb, itr, &fs) == 0) {
139 const char *type = mnt_fs_get_fstype(fs);
140 const char *src = mnt_fs_get_source(fs);
141 const char *optstr = mnt_fs_get_options(fs);
142 char *xsrc = NULL;
143
144 if (type && pattern && !mnt_match_fstype(type, pattern))
145 continue;
146
147 if (mnt_fs_is_regularfs(fs))
148 xsrc = mnt_pretty_path(src, cache);
149 printf ("%s on ", xsrc ? xsrc : src);
150 safe_fputs(mnt_fs_get_target(fs));
151
152 if (type)
153 printf (" type %s", type);
154 if (optstr)
155 printf (" (%s)", optstr);
156 if (show_label && src) {
157 char *lb = mnt_cache_find_tag_value(cache, src, "LABEL");
158 if (lb)
159 printf (" [%s]", lb);
160 }
161 fputc('\n', stdout);
162 free(xsrc);
163 }
164
165 mnt_unref_cache(cache);
166 mnt_free_iter(itr);
167 }
168
169 /*
170 * mount -a [-F]
171 */
172 static int mount_all(struct libmnt_context *cxt)
173 {
174 struct libmnt_iter *itr;
175 struct libmnt_fs *fs;
176 int mntrc, ignored, rc = MNT_EX_SUCCESS;
177
178 int nsucc = 0, nerrs = 0;
179
180 itr = mnt_new_iter(MNT_ITER_FORWARD);
181 if (!itr) {
182 warn(_("failed to initialize libmount iterator"));
183 return MNT_EX_SYSERR;
184 }
185
186 while (mnt_context_next_mount(cxt, itr, &fs, &mntrc, &ignored) == 0) {
187
188 const char *tgt = mnt_fs_get_target(fs);
189
190 if (ignored) {
191 if (mnt_context_is_verbose(cxt))
192 printf(ignored == 1 ? _("%-25s: ignored\n") :
193 _("%-25s: already mounted\n"),
194 tgt);
195 } else if (mnt_context_is_fork(cxt)) {
196 if (mnt_context_is_verbose(cxt))
197 printf("%-25s: mount successfully forked\n", tgt);
198 } else {
199 if (mk_exit_code(cxt, mntrc) == MNT_EX_SUCCESS) {
200 nsucc++;
201
202 /* Note that MNT_EX_SUCCESS return code does
203 * not mean that FS has been really mounted
204 * (e.g. nofail option) */
205 if (mnt_context_get_status(cxt)
206 && mnt_context_is_verbose(cxt))
207 printf("%-25s: successfully mounted\n", tgt);
208 } else
209 nerrs++;
210 }
211 }
212
213 if (mnt_context_is_parent(cxt)) {
214 /* wait for mount --fork children */
215 int nchildren = 0;
216
217 nerrs = 0, nsucc = 0;
218
219 rc = mnt_context_wait_for_children(cxt, &nchildren, &nerrs);
220 if (!rc && nchildren)
221 nsucc = nchildren - nerrs;
222 }
223
224 if (nerrs == 0)
225 rc = MNT_EX_SUCCESS; /* all success */
226 else if (nsucc == 0)
227 rc = MNT_EX_FAIL; /* all failed */
228 else
229 rc = MNT_EX_SOMEOK; /* some success, some failed */
230
231 mnt_free_iter(itr);
232 return rc;
233 }
234
235
236 /*
237 * mount -a -o remount
238 */
239 static int remount_all(struct libmnt_context *cxt)
240 {
241 struct libmnt_iter *itr;
242 struct libmnt_fs *fs;
243 int mntrc, ignored, rc = MNT_EX_SUCCESS;
244
245 int nsucc = 0, nerrs = 0;
246
247 itr = mnt_new_iter(MNT_ITER_FORWARD);
248 if (!itr) {
249 warn(_("failed to initialize libmount iterator"));
250 return MNT_EX_SYSERR;
251 }
252
253 while (mnt_context_next_remount(cxt, itr, &fs, &mntrc, &ignored) == 0) {
254
255 const char *tgt = mnt_fs_get_target(fs);
256
257 if (ignored) {
258 if (mnt_context_is_verbose(cxt))
259 printf(_("%-25s: ignored\n"), tgt);
260 } else {
261 if (mk_exit_code(cxt, mntrc) == MNT_EX_SUCCESS) {
262 nsucc++;
263
264 /* Note that MNT_EX_SUCCESS return code does
265 * not mean that FS has been really mounted
266 * (e.g. nofail option) */
267 if (mnt_context_get_status(cxt)
268 && mnt_context_is_verbose(cxt))
269 printf("%-25s: successfully remounted\n", tgt);
270 } else
271 nerrs++;
272 }
273 }
274
275 if (nerrs == 0)
276 rc = MNT_EX_SUCCESS; /* all success */
277 else if (nsucc == 0)
278 rc = MNT_EX_FAIL; /* all failed */
279 else
280 rc = MNT_EX_SOMEOK; /* some success, some failed */
281
282 mnt_free_iter(itr);
283 return rc;
284 }
285
286 static void success_message(struct libmnt_context *cxt)
287 {
288 unsigned long mflags = 0;
289 const char *tgt, *src, *pr = program_invocation_short_name;
290
291 if (mnt_context_helper_executed(cxt)
292 || mnt_context_get_status(cxt) != 1)
293 return;
294
295 mnt_context_get_mflags(cxt, &mflags);
296 tgt = mnt_context_get_target(cxt);
297 src = mnt_context_get_source(cxt);
298
299 if (mflags & MS_MOVE)
300 printf(_("%s: %s moved to %s.\n"), pr, src, tgt);
301 else if (mflags & MS_BIND)
302 printf(_("%s: %s bound on %s.\n"), pr, src, tgt);
303 else if (mflags & MS_PROPAGATION) {
304 if (src && strcmp(src, "none") != 0 && tgt)
305 printf(_("%s: %s mounted on %s.\n"), pr, src, tgt);
306
307 printf(_("%s: %s propagation flags changed.\n"), pr, tgt);
308 } else
309 printf(_("%s: %s mounted on %s.\n"), pr, src, tgt);
310 }
311
312 #if defined(HAVE_LIBSELINUX) && defined(HAVE_SECURITY_GET_INITIAL_CONTEXT)
313 # include <selinux/selinux.h>
314 # include <selinux/context.h>
315
316 static void selinux_warning(struct libmnt_context *cxt, const char *tgt)
317 {
318
319 if (tgt && mnt_context_is_verbose(cxt) && is_selinux_enabled() > 0) {
320 char *raw = NULL, *def = NULL;
321
322 if (getfilecon(tgt, &raw) > 0
323 && security_get_initial_context("file", &def) == 0) {
324
325 if (!selinux_file_context_cmp(raw, def))
326 printf(_(
327 "mount: %s does not contain SELinux labels.\n"
328 " You just mounted a file system that supports labels which does not\n"
329 " contain labels, onto an SELinux box. It is likely that confined\n"
330 " applications will generate AVC messages and not be allowed access to\n"
331 " this file system. For more details see restorecon(8) and mount(8).\n"),
332 tgt);
333 }
334 freecon(raw);
335 freecon(def);
336 }
337 }
338 #else
339 # define selinux_warning(_x, _y)
340 #endif
341
342
343 #ifdef USE_SYSTEMD
344 static void systemd_hint(void)
345 {
346 static int fstab_check_done = 0;
347
348 if (fstab_check_done == 0) {
349 struct stat a, b;
350
351 if (isatty(STDERR_FILENO) &&
352 stat(_PATH_SD_UNITSLOAD, &a) == 0 &&
353 stat(_PATH_MNTTAB, &b) == 0 &&
354 cmp_stat_mtime(&a, &b, <))
355 printf(_(
356 "mount: (hint) your fstab has been modified, but systemd still uses\n"
357 " the old version; use 'systemctl daemon-reload' to reload.\n"));
358
359 fstab_check_done = 1;
360 }
361 }
362 #else
363 # define systemd_hint()
364 #endif
365
366
367 /*
368 * Returns exit status (MNT_EX_*) and/or prints error message.
369 */
370 static int mk_exit_code(struct libmnt_context *cxt, int rc)
371 {
372 const char *tgt;
373 char buf[BUFSIZ] = { 0 };
374
375 rc = mnt_context_get_excode(cxt, rc, buf, sizeof(buf));
376 tgt = mnt_context_get_target(cxt);
377
378 if (*buf) {
379 const char *spec = tgt;
380 if (!spec)
381 spec = mnt_context_get_source(cxt);
382 if (!spec)
383 spec = "???";
384 warnx("%s: %s.", spec, buf);
385
386 if (mnt_context_syscall_called(cxt) &&
387 mnt_context_get_syscall_errno(cxt) != 0)
388 fprintf(stderr, _(" dmesg(1) may have more information after failed mount system call.\n"));
389 }
390
391 if (rc == MNT_EX_SUCCESS && mnt_context_get_status(cxt) == 1) {
392 selinux_warning(cxt, tgt);
393 }
394
395 systemd_hint();
396
397 return rc;
398 }
399
400 static struct libmnt_table *append_fstab(struct libmnt_context *cxt,
401 struct libmnt_table *fstab,
402 const char *path)
403 {
404
405 if (!fstab) {
406 fstab = mnt_new_table();
407 if (!fstab)
408 err(MNT_EX_SYSERR, _("failed to initialize libmount table"));
409
410 mnt_table_set_parser_errcb(fstab, table_parser_errcb);
411 mnt_context_set_fstab(cxt, fstab);
412
413 mnt_unref_table(fstab); /* reference is handled by @cxt now */
414 }
415
416 if (mnt_table_parse_fstab(fstab, path))
417 errx(MNT_EX_USAGE,_("%s: failed to parse"), path);
418
419 return fstab;
420 }
421
422 /*
423 * Check source and target paths -- non-root user should not be able to
424 * resolve paths which are unreadable for them.
425 */
426 static int sanitize_paths(struct libmnt_context *cxt)
427 {
428 const char *p;
429 struct libmnt_fs *fs = mnt_context_get_fs(cxt);
430
431 if (!fs)
432 return 0;
433
434 p = mnt_fs_get_target(fs);
435 if (p) {
436 char *np = canonicalize_path_restricted(p);
437 if (!np)
438 return -EPERM;
439 mnt_fs_set_target(fs, np);
440 free(np);
441 }
442
443 p = mnt_fs_get_srcpath(fs);
444 if (p) {
445 char *np = canonicalize_path_restricted(p);
446 if (!np)
447 return -EPERM;
448 mnt_fs_set_source(fs, np);
449 free(np);
450 }
451 return 0;
452 }
453
454 static void append_option(struct libmnt_context *cxt, const char *opt, const char *arg)
455 {
456 char *o = NULL;
457
458 if (opt && (*opt == '=' || *opt == '\'' || *opt == '\"' || isblank(*opt)))
459 errx(MNT_EX_USAGE, _("unsupported option format: %s"), opt);
460
461 if (arg && *arg)
462 xasprintf(&o, "%s=\"%s\"", opt, arg);
463
464 if (mnt_context_append_options(cxt, o ? : opt))
465 err(MNT_EX_SYSERR, _("failed to append option '%s'"), o ? : opt);
466
467 free(o);
468 }
469
470 static int has_remount_flag(struct libmnt_context *cxt)
471 {
472 unsigned long mflags = 0;
473
474 if (mnt_context_get_mflags(cxt, &mflags))
475 return 0;
476
477 return mflags & MS_REMOUNT;
478 }
479
480 static void __attribute__((__noreturn__)) usage(void)
481 {
482 FILE *out = stdout;
483
484 fputs(USAGE_HEADER, out);
485 fprintf(out, _(
486 " %1$s [-lhV]\n"
487 " %1$s -a [options]\n"
488 " %1$s [options] [--source] <source> | [--target] <directory>\n"
489 " %1$s [options] <source> <directory>\n"
490 " %1$s <operation> <mountpoint> [<target>]\n"),
491 program_invocation_short_name);
492
493 fputs(USAGE_SEPARATOR, out);
494 fputs(_("Mount a filesystem.\n"), out);
495
496 fputs(USAGE_OPTIONS, out);
497 fputs(_(" -a, --all mount all filesystems mentioned in fstab\n"), out);
498 fputs(_(" -c, --no-canonicalize don't canonicalize paths\n"), out);
499 fputs(_(" -f, --fake dry run; skip the mount(2) syscall\n"), out);
500 fputs(_(" -F, --fork fork off for each device (use with -a)\n"), out);
501 fputs(_(" -T, --fstab <path> alternative file to /etc/fstab\n"), out);
502 fputs(_(" -i, --internal-only don't call the mount.<type> helpers\n"), out);
503 fputs(_(" -l, --show-labels show also filesystem labels\n"), out);
504 fputs(_(" --map-groups <inner>:<outer>:<count>\n"
505 " add the specified GID map to an ID-mapped mount\n"), out);
506 fputs(_(" --map-users <inner>:<outer>:<count>\n"
507 " add the specified UID map to an ID-mapped mount\n"), out);
508 fputs(_(" --map-users /proc/<pid>/ns/user\n"
509 " specify the user namespace for an ID-mapped mount\n"), out);
510 fputs(_(" -m, --mkdir[=<mode>] alias to '-o X-mount.mkdir[=<mode>]'\n"), out);
511 fputs(_(" -n, --no-mtab don't write to /etc/mtab\n"), out);
512 fputs(_(" --options-mode <mode>\n"
513 " what to do with options loaded from fstab\n"), out);
514 fputs(_(" --options-source <source>\n"
515 " mount options source\n"), out);
516 fputs(_(" --options-source-force\n"
517 " force use of options from fstab/mtab\n"), out);
518 fputs(_(" --onlyonce check if filesystem is already mounted\n"), out);
519 fputs(_(" -o, --options <list> comma-separated list of mount options\n"), out);
520 fputs(_(" -O, --test-opts <list> limit the set of filesystems (use with -a)\n"), out);
521 fputs(_(" -r, --read-only mount the filesystem read-only (same as -o ro)\n"), out);
522 fputs(_(" -t, --types <list> limit the set of filesystem types\n"), out);
523 fputs(_(" --source <src> explicitly specifies source (path, label, uuid)\n"), out);
524 fputs(_(" --target <target> explicitly specifies mountpoint\n"), out);
525 fputs(_(" --target-prefix <path>\n"
526 " specifies path used for all mountpoints\n"), out);
527 fputs(_(" -v, --verbose say what is being done\n"), out);
528 fputs(_(" -w, --rw, --read-write mount the filesystem read-write (default)\n"), out);
529 fputs(_(" -N, --namespace <ns> perform mount in another namespace\n"), out);
530
531 fputs(USAGE_SEPARATOR, out);
532 fprintf(out, USAGE_HELP_OPTIONS(25));
533
534 fputs(USAGE_SEPARATOR, out);
535 fputs(_("Source:\n"), out);
536 fputs(_(" -L, --label <label> synonym for LABEL=<label>\n"), out);
537 fputs(_(" -U, --uuid <uuid> synonym for UUID=<uuid>\n"), out);
538 fputs(_(" LABEL=<label> specifies device by filesystem label\n"), out);
539 fputs(_(" UUID=<uuid> specifies device by filesystem UUID\n"), out);
540 fputs(_(" PARTLABEL=<label> specifies device by partition label\n"), out);
541 fputs(_(" PARTUUID=<uuid> specifies device by partition UUID\n"), out);
542 fputs(_(" ID=<id> specifies device by udev hardware ID\n"), out);
543 fputs(_(" <device> specifies device by path\n"), out);
544 fputs(_(" <directory> mountpoint for bind mounts (see --bind/rbind)\n"), out);
545 fputs(_(" <file> regular file for loopdev setup\n"), out);
546
547 fputs(USAGE_SEPARATOR, out);
548 fputs(_("Operations:\n"), out);
549 fputs(_(" -B, --bind mount a subtree somewhere else (same as -o bind)\n"), out);
550 fputs(_(" -M, --move move a subtree to some other place\n"), out);
551 fputs(_(" -R, --rbind mount a subtree and all submounts somewhere else\n"), out);
552 fputs(_(" --make-shared mark a subtree as shared\n"), out);
553 fputs(_(" --make-slave mark a subtree as slave\n"), out);
554 fputs(_(" --make-private mark a subtree as private\n"), out);
555 fputs(_(" --make-unbindable mark a subtree as unbindable\n"), out);
556 fputs(_(" --make-rshared recursively mark a whole subtree as shared\n"), out);
557 fputs(_(" --make-rslave recursively mark a whole subtree as slave\n"), out);
558 fputs(_(" --make-rprivate recursively mark a whole subtree as private\n"), out);
559 fputs(_(" --make-runbindable recursively mark a whole subtree as unbindable\n"), out);
560
561 fprintf(out, USAGE_MAN_TAIL("mount(8)"));
562
563 exit(MNT_EX_SUCCESS);
564 }
565
566 struct flag_str {
567 int value;
568 char *str;
569 };
570
571 static int omode2mask(const char *str)
572 {
573 size_t i;
574
575 static const struct flag_str flags[] = {
576 { MNT_OMODE_IGNORE, "ignore" },
577 { MNT_OMODE_APPEND, "append" },
578 { MNT_OMODE_PREPEND, "prepend" },
579 { MNT_OMODE_REPLACE, "replace" },
580 };
581
582 for (i = 0; i < ARRAY_SIZE(flags); i++) {
583 if (!strcmp(str, flags[i].str))
584 return flags[i].value;
585 }
586 return -EINVAL;
587 }
588
589 static long osrc2mask(const char *str, size_t len)
590 {
591 size_t i;
592
593 static const struct flag_str flags[] = {
594 { MNT_OMODE_FSTAB, "fstab" },
595 { MNT_OMODE_MTAB, "mtab" },
596 { MNT_OMODE_NOTAB, "disable" },
597 };
598
599 for (i = 0; i < ARRAY_SIZE(flags); i++) {
600 if (!strncmp(str, flags[i].str, len) && !flags[i].str[len])
601 return flags[i].value;
602 }
603 return -EINVAL;
604 }
605
606 static pid_t parse_pid(const char *str)
607 {
608 char *end;
609 pid_t ret;
610
611 errno = 0;
612 ret = strtoul(str, &end, 10);
613
614 if (ret < 0 || errno || end == str || (end && *end))
615 return 0;
616 return ret;
617 }
618
619 int main(int argc, char **argv)
620 {
621 int c, rc = MNT_EX_SUCCESS, all = 0, show_labels = 0;
622 struct libmnt_context *cxt;
623 struct libmnt_table *fstab = NULL;
624 char *idmap = NULL;
625 char *srcbuf = NULL;
626 char *types = NULL;
627 int oper = 0, is_move = 0;
628 int propa = 0;
629 int optmode = 0, optmode_mode = 0, optmode_src = 0;
630
631 enum {
632 MOUNT_OPT_SHARED = CHAR_MAX + 1,
633 MOUNT_OPT_SLAVE,
634 MOUNT_OPT_PRIVATE,
635 MOUNT_OPT_UNBINDABLE,
636 MOUNT_OPT_RSHARED,
637 MOUNT_OPT_RSLAVE,
638 MOUNT_OPT_RPRIVATE,
639 MOUNT_OPT_RUNBINDABLE,
640 MOUNT_OPT_MAP_GROUPS,
641 MOUNT_OPT_MAP_USERS,
642 MOUNT_OPT_TARGET,
643 MOUNT_OPT_TARGET_PREFIX,
644 MOUNT_OPT_SOURCE,
645 MOUNT_OPT_OPTMODE,
646 MOUNT_OPT_OPTSRC,
647 MOUNT_OPT_OPTSRC_FORCE,
648 MOUNT_OPT_ONLYONCE
649 };
650
651 static const struct option longopts[] = {
652 { "all", no_argument, NULL, 'a' },
653 { "fake", no_argument, NULL, 'f' },
654 { "fstab", required_argument, NULL, 'T' },
655 { "fork", no_argument, NULL, 'F' },
656 { "help", no_argument, NULL, 'h' },
657 { "no-mtab", no_argument, NULL, 'n' },
658 { "read-only", no_argument, NULL, 'r' },
659 { "ro", no_argument, NULL, 'r' },
660 { "verbose", no_argument, NULL, 'v' },
661 { "version", no_argument, NULL, 'V' },
662 { "read-write", no_argument, NULL, 'w' },
663 { "rw", no_argument, NULL, 'w' },
664 { "options", required_argument, NULL, 'o' },
665 { "test-opts", required_argument, NULL, 'O' },
666 { "types", required_argument, NULL, 't' },
667 { "uuid", required_argument, NULL, 'U' },
668 { "label", required_argument, NULL, 'L' },
669 { "bind", no_argument, NULL, 'B' },
670 { "move", no_argument, NULL, 'M' },
671 { "rbind", no_argument, NULL, 'R' },
672 { "make-shared", no_argument, NULL, MOUNT_OPT_SHARED },
673 { "make-slave", no_argument, NULL, MOUNT_OPT_SLAVE },
674 { "make-private", no_argument, NULL, MOUNT_OPT_PRIVATE },
675 { "make-unbindable", no_argument, NULL, MOUNT_OPT_UNBINDABLE },
676 { "make-rshared", no_argument, NULL, MOUNT_OPT_RSHARED },
677 { "make-rslave", no_argument, NULL, MOUNT_OPT_RSLAVE },
678 { "make-rprivate", no_argument, NULL, MOUNT_OPT_RPRIVATE },
679 { "make-runbindable", no_argument, NULL, MOUNT_OPT_RUNBINDABLE },
680 { "map-groups", required_argument, NULL, MOUNT_OPT_MAP_GROUPS },
681 { "map-users", required_argument, NULL, MOUNT_OPT_MAP_USERS },
682 { "mkdir", optional_argument, NULL, 'm' },
683 { "no-canonicalize", no_argument, NULL, 'c' },
684 { "internal-only", no_argument, NULL, 'i' },
685 { "show-labels", no_argument, NULL, 'l' },
686 { "target", required_argument, NULL, MOUNT_OPT_TARGET },
687 { "target-prefix", required_argument, NULL, MOUNT_OPT_TARGET_PREFIX },
688 { "source", required_argument, NULL, MOUNT_OPT_SOURCE },
689 { "onlyonce", no_argument, NULL, MOUNT_OPT_ONLYONCE },
690 { "options-mode", required_argument, NULL, MOUNT_OPT_OPTMODE },
691 { "options-source", required_argument, NULL, MOUNT_OPT_OPTSRC },
692 { "options-source-force", no_argument, NULL, MOUNT_OPT_OPTSRC_FORCE},
693 { "namespace", required_argument, NULL, 'N' },
694 { NULL, 0, NULL, 0 }
695 };
696
697 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
698 { 'B','M','R' }, /* bind,move,rbind */
699 { 'L','U', MOUNT_OPT_SOURCE }, /* label,uuid,source */
700 { 0 }
701 };
702 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
703
704 __sanitize_env(&envs_removed);
705 setlocale(LC_ALL, "");
706 bindtextdomain(PACKAGE, LOCALEDIR);
707 textdomain(PACKAGE);
708 close_stdout_atexit();
709
710 strutils_set_exitcode(MNT_EX_USAGE);
711
712 mnt_init_debug(0);
713 cxt = mnt_new_context();
714 if (!cxt)
715 err(MNT_EX_SYSERR, _("libmount context allocation failed"));
716
717 mnt_context_set_tables_errcb(cxt, table_parser_errcb);
718
719 while ((c = getopt_long(argc, argv, "aBcfFhilL:m::Mno:O:rRsU:vVwt:T:N:",
720 longopts, NULL)) != -1) {
721
722 /* only few options are allowed for non-root users */
723 if (mnt_context_is_restricted(cxt) &&
724 !strchr("hlLUVvrist", c) &&
725 c != MOUNT_OPT_TARGET &&
726 c != MOUNT_OPT_SOURCE)
727 suid_drop(cxt);
728
729 err_exclusive_options(c, longopts, excl, excl_st);
730
731 switch(c) {
732 case 'a':
733 all = 1;
734 break;
735 case 'c':
736 mnt_context_disable_canonicalize(cxt, TRUE);
737 break;
738 case 'f':
739 mnt_context_enable_fake(cxt, TRUE);
740 break;
741 case 'F':
742 mnt_context_enable_fork(cxt, TRUE);
743 break;
744 case 'i':
745 mnt_context_disable_helpers(cxt, TRUE);
746 break;
747 case 'n':
748 mnt_context_disable_mtab(cxt, TRUE);
749 break;
750 case 'r':
751 append_option(cxt, "ro", NULL);
752 mnt_context_enable_rwonly_mount(cxt, FALSE);
753 break;
754 case 'v':
755 mnt_context_enable_verbose(cxt, TRUE);
756 break;
757 case 'w':
758 append_option(cxt, "rw", NULL);
759 mnt_context_enable_rwonly_mount(cxt, TRUE);
760 break;
761 case 'o':
762 /* "move" is not supported as option string in libmount
763 * to avoid use in fstab */
764 if (mnt_optstr_get_option(optarg, "move", NULL, 0) == 0) {
765 char *o = xstrdup(optarg);
766
767 mnt_optstr_remove_option(&o, "move");
768 if (o && *o)
769 append_option(cxt, o, NULL);
770 oper = is_move = 1;
771 free(o);
772 } else
773 append_option(cxt, optarg, NULL);
774 break;
775 case 'O':
776 if (mnt_context_set_options_pattern(cxt, optarg))
777 err(MNT_EX_SYSERR, _("failed to set options pattern"));
778 break;
779 case 'L':
780 xasprintf(&srcbuf, "LABEL=\"%s\"", optarg);
781 mnt_context_disable_swapmatch(cxt, 1);
782 mnt_context_set_source(cxt, srcbuf);
783 free(srcbuf);
784 break;
785 case 'U':
786 xasprintf(&srcbuf, "UUID=\"%s\"", optarg);
787 mnt_context_disable_swapmatch(cxt, 1);
788 mnt_context_set_source(cxt, srcbuf);
789 free(srcbuf);
790 break;
791 case 'l':
792 show_labels = 1;
793 break;
794 case 't':
795 types = optarg;
796 break;
797 case 'T':
798 fstab = append_fstab(cxt, fstab, optarg);
799 break;
800 case 's':
801 mnt_context_enable_sloppy(cxt, TRUE);
802 break;
803 case 'B':
804 oper = 1;
805 append_option(cxt, "bind", NULL);
806 break;
807 case 'M':
808 oper = 1;
809 is_move = 1;
810 break;
811 case 'm':
812 if (optarg && *optarg == '=')
813 optarg++;
814 append_option(cxt, "X-mount.mkdir", optarg);
815 break;
816 case 'R':
817 oper = 1;
818 append_option(cxt, "rbind", NULL);
819 break;
820 case 'N':
821 {
822 char path[PATH_MAX];
823 pid_t pid = parse_pid(optarg);
824
825 if (pid)
826 snprintf(path, sizeof(path), "/proc/%i/ns/mnt", pid);
827
828 if (mnt_context_set_target_ns(cxt, pid ? path : optarg))
829 err(MNT_EX_SYSERR, _("failed to set target namespace to %s"), pid ? path : optarg);
830 break;
831 }
832 case MOUNT_OPT_SHARED:
833 append_option(cxt, "shared", NULL);
834 propa = 1;
835 break;
836 case MOUNT_OPT_SLAVE:
837 append_option(cxt, "slave", NULL);
838 propa = 1;
839 break;
840 case MOUNT_OPT_PRIVATE:
841 append_option(cxt, "private", NULL);
842 propa = 1;
843 break;
844 case MOUNT_OPT_UNBINDABLE:
845 append_option(cxt, "unbindable", NULL);
846 propa = 1;
847 break;
848 case MOUNT_OPT_RSHARED:
849 append_option(cxt, "rshared", NULL);
850 propa = 1;
851 break;
852 case MOUNT_OPT_RSLAVE:
853 append_option(cxt, "rslave", NULL);
854 propa = 1;
855 break;
856 case MOUNT_OPT_RPRIVATE:
857 append_option(cxt, "rprivate", NULL);
858 propa = 1;
859 break;
860 case MOUNT_OPT_RUNBINDABLE:
861 append_option(cxt, "runbindable", NULL);
862 propa = 1;
863 break;
864 case MOUNT_OPT_MAP_GROUPS:
865 case MOUNT_OPT_MAP_USERS:
866 if (*optarg == '=')
867 optarg++;
868 if (idmap && (*idmap == '/' || *optarg == '/')) {
869 warnx(_("bad usage"));
870 errtryhelp(MNT_EX_USAGE);
871 } else if (*optarg == '/') {
872 idmap = xstrdup(optarg);
873 } else {
874 char *tmp;
875 xasprintf(&tmp, "%s%s%s%s", idmap ? idmap : "", idmap ? " " : "",
876 c == MOUNT_OPT_MAP_GROUPS ? "g:" : "u:", optarg);
877 free(idmap);
878 idmap = tmp;
879 }
880 break;
881 case MOUNT_OPT_TARGET:
882 mnt_context_disable_swapmatch(cxt, 1);
883 mnt_context_set_target(cxt, optarg);
884 break;
885 case MOUNT_OPT_TARGET_PREFIX:
886 mnt_context_set_target_prefix(cxt, optarg);
887 break;
888 case MOUNT_OPT_SOURCE:
889 mnt_context_disable_swapmatch(cxt, 1);
890 mnt_context_set_source(cxt, optarg);
891 break;
892 case MOUNT_OPT_OPTMODE:
893 optmode_mode = omode2mask(optarg);
894 if (optmode_mode == -EINVAL) {
895 warnx(_("bad usage"));
896 errtryhelp(MNT_EX_USAGE);
897 }
898 break;
899 case MOUNT_OPT_OPTSRC:
900 {
901 unsigned long tmp = 0;
902 if (string_to_bitmask(optarg, &tmp, osrc2mask)) {
903 warnx(_("bad usage"));
904 errtryhelp(MNT_EX_USAGE);
905 }
906 optmode_src = tmp;
907 break;
908 }
909 case MOUNT_OPT_OPTSRC_FORCE:
910 optmode |= MNT_OMODE_FORCE;
911 break;
912 case MOUNT_OPT_ONLYONCE:
913 mnt_context_enable_onlyonce(cxt, 1);
914 break;
915 case 'h':
916 mnt_free_context(cxt);
917 usage();
918 case 'V':
919 mnt_free_context(cxt);
920 mount_print_version();
921 default:
922 errtryhelp(MNT_EX_USAGE);
923 }
924 }
925
926 argc -= optind;
927 argv += optind;
928
929 if (idmap)
930 append_option(cxt, "X-mount.idmap", idmap);
931
932 optmode |= optmode_mode | optmode_src;
933 if (optmode) {
934 if (!optmode_mode)
935 optmode |= MNT_OMODE_PREPEND;
936 if (!optmode_src)
937 optmode |= MNT_OMODE_FSTAB | MNT_OMODE_MTAB;
938 mnt_context_set_optsmode(cxt, optmode);
939 }
940
941 if (fstab && !mnt_context_is_nocanonicalize(cxt)) {
942 /*
943 * We have external (context independent) fstab instance, let's
944 * make a connection between the fstab and the canonicalization
945 * cache.
946 */
947 mnt_table_set_cache(fstab, mnt_context_get_cache(cxt));
948 }
949
950 if (!mnt_context_get_source(cxt) &&
951 !mnt_context_get_target(cxt) &&
952 !argc &&
953 !all) {
954 if (oper || mnt_context_get_options(cxt)) {
955 warnx(_("bad usage"));
956 errtryhelp(MNT_EX_USAGE);
957 }
958 print_all(cxt, types, show_labels);
959 goto done;
960 }
961
962 /* Non-root users are allowed to use -t to print_all(),
963 but not to mount */
964 if (mnt_context_is_restricted(cxt) && types)
965 suid_drop(cxt);
966
967 if (oper && (types || all || mnt_context_get_source(cxt))) {
968 warnx(_("bad usage"));
969 errtryhelp(MNT_EX_USAGE);
970 }
971
972 if (types && (all || strchr(types, ',') ||
973 strncmp(types, "no", 2) == 0))
974 mnt_context_set_fstype_pattern(cxt, types);
975 else if (types)
976 mnt_context_set_fstype(cxt, types);
977
978 if (all) {
979 /*
980 * A) Mount all
981 */
982 if (has_remount_flag(cxt))
983 rc = remount_all(cxt);
984 else
985 rc = mount_all(cxt);
986 goto done;
987
988 } else if (argc == 0 && (mnt_context_get_source(cxt) ||
989 mnt_context_get_target(cxt))) {
990 /*
991 * B) mount -L|-U|--source|--target
992 *
993 * non-root may specify source *or* target, but not both
994 */
995 if (mnt_context_is_restricted(cxt) &&
996 mnt_context_get_source(cxt) &&
997 mnt_context_get_target(cxt))
998 suid_drop(cxt);
999
1000 } else if (argc == 1 && (!mnt_context_get_source(cxt) ||
1001 !mnt_context_get_target(cxt))) {
1002 /*
1003 * C) mount [-L|-U|--source] <target>
1004 * mount [--target <dir>] <source>
1005 * mount <source|target>
1006 *
1007 * non-root may specify source *or* target, but not both
1008 *
1009 * It does not matter for libmount if we set source or target
1010 * here (the library is able to swap it), but it matters for
1011 * sanitize_paths().
1012 */
1013 int istag = mnt_tag_is_valid(argv[0]);
1014
1015 if (istag && mnt_context_get_source(cxt))
1016 /* -L, -U or --source together with LABEL= or UUID= */
1017 errx(MNT_EX_USAGE, _("source specified more than once"));
1018 else if (istag || mnt_context_get_target(cxt))
1019 mnt_context_set_source(cxt, argv[0]);
1020 else
1021 mnt_context_set_target(cxt, argv[0]);
1022
1023 if (mnt_context_is_restricted(cxt) &&
1024 mnt_context_get_source(cxt) &&
1025 mnt_context_get_target(cxt))
1026 suid_drop(cxt);
1027
1028 } else if (argc == 2 && !mnt_context_get_source(cxt)
1029 && !mnt_context_get_target(cxt)) {
1030 /*
1031 * D) mount <source> <target>
1032 */
1033 if (mnt_context_is_restricted(cxt))
1034 suid_drop(cxt);
1035
1036 mnt_context_set_source(cxt, argv[0]);
1037 mnt_context_set_target(cxt, argv[1]);
1038
1039 } else {
1040 warnx(_("bad usage"));
1041 errtryhelp(MNT_EX_USAGE);
1042 }
1043
1044 if (mnt_context_is_restricted(cxt) && sanitize_paths(cxt) != 0)
1045 suid_drop(cxt);
1046
1047 if (is_move)
1048 /* "move" as option string is not supported by libmount */
1049 mnt_context_set_mflags(cxt, MS_MOVE);
1050
1051 if ((oper && !has_remount_flag(cxt)) || propa)
1052 /* For --make-* or --bind is fstab/mtab unnecessary */
1053 mnt_context_set_optsmode(cxt, MNT_OMODE_NOTAB);
1054
1055 rc = mnt_context_mount(cxt);
1056
1057 if (rc == -EPERM
1058 && mnt_context_is_restricted(cxt)
1059 && !mnt_context_syscall_called(cxt)) {
1060 /* Try it again without permissions */
1061 suid_drop(cxt);
1062 rc = mnt_context_mount(cxt);
1063 }
1064 rc = mk_exit_code(cxt, rc);
1065
1066 if (rc == MNT_EX_SUCCESS && mnt_context_is_verbose(cxt))
1067 success_message(cxt);
1068 done:
1069 mnt_free_context(cxt);
1070 env_list_free(envs_removed);
1071 return rc;
1072 }