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