1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1980, 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
35 The Regents of the University of California. All rights reserved.\n";
36 #if 0
37 static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 5/8/95";
38 #endif
39 #endif /* not lint */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <sys/stat.h>
47 #include <sys/wait.h>
48
49 #include <ctype.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fstab.h>
53 #include <paths.h>
54 #include <pwd.h>
55 #include <signal.h>
56 #include <stdint.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <libutil.h>
62
63 #include "extern.h"
64 #include "mntopts.h"
65 #include "pathnames.h"
66
67 /* `meta' options */
68 #define MOUNT_META_OPTION_FSTAB "fstab"
69 #define MOUNT_META_OPTION_CURRENT "current"
70
71 static int debug, fstab_style, verbose;
72
73 struct cpa {
74 char **a;
75 ssize_t sz;
76 int c;
77 };
78
79 char *catopt(char *, const char *);
80 struct statfs *getmntpt(const char *);
81 int hasopt(const char *, const char *);
82 int ismounted(struct fstab *, struct statfs *, int);
83 int isremountable(const char *);
84 void mangle(char *, struct cpa *);
85 char *update_options(char *, char *, int);
86 int mountfs(const char *, const char *, const char *,
87 int, const char *, const char *);
88 void remopt(char *, const char *);
89 void prmount(struct statfs *);
90 void putfsent(struct statfs *);
91 void usage(void);
92 char *flags2opts(int);
93
94 /* Map from mount options to printable formats. */
95 static struct opt {
96 uint64_t o_opt;
97 const char *o_name;
98 } optnames[] = {
99 { MNT_ASYNC, "asynchronous" },
100 { MNT_EXPORTED, "NFS exported" },
101 { MNT_LOCAL, "local" },
102 { MNT_NOATIME, "noatime" },
103 { MNT_NOEXEC, "noexec" },
104 { MNT_NOSUID, "nosuid" },
105 { MNT_NOSYMFOLLOW, "nosymfollow" },
106 { MNT_QUOTA, "with quotas" },
107 { MNT_RDONLY, "read-only" },
108 { MNT_SYNCHRONOUS, "synchronous" },
109 { MNT_UNION, "union" },
110 { MNT_NOCLUSTERR, "noclusterr" },
111 { MNT_NOCLUSTERW, "noclusterw" },
112 { MNT_SUIDDIR, "suiddir" },
113 { MNT_SOFTDEP, "soft-updates" },
114 { MNT_SUJ, "journaled soft-updates" },
115 { MNT_MULTILABEL, "multilabel" },
116 { MNT_ACLS, "acls" },
117 { MNT_NFS4ACLS, "nfsv4acls" },
118 { MNT_GJOURNAL, "gjournal" },
119 { MNT_AUTOMOUNTED, "automounted" },
120 { MNT_VERIFIED, "verified" },
121 { MNT_UNTRUSTED, "untrusted" },
122 { 0, NULL }
123 };
124
125 /*
126 * List of VFS types that can be remounted without becoming mounted on top
127 * of each other.
128 * XXX Is this list correct?
129 */
130 static const char *
131 remountable_fs_names[] = {
132 "ufs", "ffs", "ext2fs",
133 0
134 };
135
136 static const char userquotaeq[] = "userquota=";
137 static const char groupquotaeq[] = "groupquota=";
138
139 static char *mountprog = NULL;
140
141 static int
use_mountprog(const char * vfstype)142 use_mountprog(const char *vfstype)
143 {
144 /* XXX: We need to get away from implementing external mount
145 * programs for every filesystem, and move towards having
146 * each filesystem properly implement the nmount() system call.
147 */
148 unsigned int i;
149 const char *fs[] = {
150 "cd9660", "mfs", "msdosfs", "nfs",
151 "nullfs", "smbfs", "udf", "unionfs",
152 NULL
153 };
154
155 if (mountprog != NULL)
156 return (1);
157
158 for (i = 0; fs[i] != NULL; ++i) {
159 if (strcmp(vfstype, fs[i]) == 0)
160 return (1);
161 }
162
163 return (0);
164 }
165
166 static int
exec_mountprog(const char * name,const char * execname,char * const argv[])167 exec_mountprog(const char *name, const char *execname, char *const argv[])
168 {
169 pid_t pid;
170 int status;
171
172 switch (pid = fork()) {
173 case -1: /* Error. */
174 warn("fork");
175 exit (1);
176 case 0: /* Child. */
177 /* Go find an executable. */
178 execvP(execname, _PATH_SYSPATH, argv);
179 if (errno == ENOENT) {
180 warn("exec %s not found", execname);
181 if (execname[0] != '/') {
182 warnx("in path: %s", _PATH_SYSPATH);
183 }
184 }
185 exit(1);
186 default: /* Parent. */
187 if (waitpid(pid, &status, 0) < 0) {
188 warn("waitpid");
189 return (1);
190 }
191
192 if (WIFEXITED(status)) {
193 if (WEXITSTATUS(status) != 0)
194 return (WEXITSTATUS(status));
195 } else if (WIFSIGNALED(status)) {
196 warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
197 return (1);
198 }
199 break;
200 }
201
202 return (0);
203 }
204
205 static int
specified_ro(const char * arg)206 specified_ro(const char *arg)
207 {
208 char *optbuf, *opt;
209 int ret = 0;
210
211 optbuf = strdup(arg);
212 if (optbuf == NULL)
213 err(1, NULL);
214
215 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
216 if (strcmp(opt, "ro") == 0) {
217 ret = 1;
218 break;
219 }
220 }
221 free(optbuf);
222 return (ret);
223 }
224
225 static void
restart_mountd(void)226 restart_mountd(void)
227 {
228 struct pidfh *pfh;
229 pid_t mountdpid;
230
231 mountdpid = 0;
232 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
233 if (pfh != NULL) {
234 /* Mountd is not running. */
235 pidfile_remove(pfh);
236 return;
237 }
238 if (errno != EEXIST) {
239 /* Cannot open pidfile for some reason. */
240 return;
241 }
242
243 /*
244 * Refuse to send broadcast or group signals, this has
245 * happened due to the bugs in pidfile(3).
246 */
247 if (mountdpid <= 0) {
248 warnx("mountd pid %d, refusing to send SIGHUP", mountdpid);
249 return;
250 }
251
252 /* We have mountd(8) PID in mountdpid varible, let's signal it. */
253 if (kill(mountdpid, SIGHUP) == -1)
254 err(1, "signal mountd");
255 }
256
257 int
main(int argc,char * argv[])258 main(int argc, char *argv[])
259 {
260 const char *mntfromname, **vfslist, *vfstype;
261 struct fstab *fs;
262 struct statfs *mntbuf;
263 int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
264 int onlylate;
265 char *cp, *ep, *options;
266
267 all = init_flags = late = onlylate = 0;
268 ro = 0;
269 options = NULL;
270 vfslist = NULL;
271 vfstype = "ufs";
272 while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
273 switch (ch) {
274 case 'a':
275 all = 1;
276 break;
277 case 'd':
278 debug = 1;
279 break;
280 case 'F':
281 setfstab(optarg);
282 break;
283 case 'f':
284 init_flags |= MNT_FORCE;
285 break;
286 case 'L':
287 onlylate = 1;
288 late = 1;
289 break;
290 case 'l':
291 late = 1;
292 break;
293 case 'n':
294 /* For compatibility with the Linux version of mount. */
295 break;
296 case 'o':
297 if (*optarg) {
298 options = catopt(options, optarg);
299 if (specified_ro(optarg))
300 ro = 1;
301 }
302 break;
303 case 'p':
304 fstab_style = 1;
305 verbose = 1;
306 break;
307 case 'r':
308 options = catopt(options, "ro");
309 ro = 1;
310 break;
311 case 't':
312 if (vfslist != NULL)
313 errx(1, "only one -t option may be specified");
314 vfslist = makevfslist(optarg);
315 vfstype = optarg;
316 break;
317 case 'u':
318 init_flags |= MNT_UPDATE;
319 break;
320 case 'v':
321 verbose = 1;
322 break;
323 case 'w':
324 options = catopt(options, "noro");
325 break;
326 case '?':
327 default:
328 usage();
329 /* NOTREACHED */
330 }
331 argc -= optind;
332 argv += optind;
333
334 #define BADTYPE(type) \
335 (strcmp(type, FSTAB_RO) && \
336 strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
337
338 if ((init_flags & MNT_UPDATE) && (ro == 0))
339 options = catopt(options, "noro");
340
341 rval = 0;
342 switch (argc) {
343 case 0:
344 if ((mntsize = getmntinfo(&mntbuf,
345 verbose ? MNT_WAIT : MNT_NOWAIT)) == 0)
346 err(1, "getmntinfo");
347 if (all) {
348 while ((fs = getfsent()) != NULL) {
349 if (BADTYPE(fs->fs_type))
350 continue;
351 if (checkvfsname(fs->fs_vfstype, vfslist))
352 continue;
353 if (hasopt(fs->fs_mntops, "noauto"))
354 continue;
355 if (!hasopt(fs->fs_mntops, "late") && onlylate)
356 continue;
357 if (hasopt(fs->fs_mntops, "late") && !late)
358 continue;
359 if (hasopt(fs->fs_mntops, "failok"))
360 failok = 1;
361 else
362 failok = 0;
363 if (!(init_flags & MNT_UPDATE) &&
364 ismounted(fs, mntbuf, mntsize))
365 continue;
366 options = update_options(options, fs->fs_mntops,
367 mntbuf->f_flags);
368 if (mountfs(fs->fs_vfstype, fs->fs_spec,
369 fs->fs_file, init_flags, options,
370 fs->fs_mntops) && !failok)
371 rval = 1;
372 }
373 } else if (fstab_style) {
374 for (i = 0; i < mntsize; i++) {
375 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
376 continue;
377 putfsent(&mntbuf[i]);
378 }
379 } else {
380 for (i = 0; i < mntsize; i++) {
381 if (checkvfsname(mntbuf[i].f_fstypename,
382 vfslist))
383 continue;
384 if (!verbose &&
385 (mntbuf[i].f_flags & MNT_IGNORE) != 0)
386 continue;
387 prmount(&mntbuf[i]);
388 }
389 }
390 exit(rval);
391 case 1:
392 if (vfslist != NULL)
393 usage();
394
395 rmslashes(*argv, *argv);
396 if (init_flags & MNT_UPDATE) {
397 mntfromname = NULL;
398 have_fstab = 0;
399 if ((mntbuf = getmntpt(*argv)) == NULL)
400 errx(1, "not currently mounted %s", *argv);
401 /*
402 * Only get the mntflags from fstab if both mntpoint
403 * and mntspec are identical. Also handle the special
404 * case where just '/' is mounted and 'spec' is not
405 * identical with the one from fstab ('/dev' is missing
406 * in the spec-string at boot-time).
407 */
408 if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
409 if (strcmp(fs->fs_spec,
410 mntbuf->f_mntfromname) == 0 &&
411 strcmp(fs->fs_file,
412 mntbuf->f_mntonname) == 0) {
413 have_fstab = 1;
414 mntfromname = mntbuf->f_mntfromname;
415 } else if (argv[0][0] == '/' &&
416 argv[0][1] == '\0' &&
417 strcmp(fs->fs_vfstype,
418 mntbuf->f_fstypename) == 0) {
419 fs = getfsfile("/");
420 have_fstab = 1;
421 mntfromname = fs->fs_spec;
422 }
423 }
424 if (have_fstab) {
425 options = update_options(options, fs->fs_mntops,
426 mntbuf->f_flags);
427 } else {
428 mntfromname = mntbuf->f_mntfromname;
429 options = update_options(options, NULL,
430 mntbuf->f_flags);
431 }
432 rval = mountfs(mntbuf->f_fstypename, mntfromname,
433 mntbuf->f_mntonname, init_flags, options, 0);
434 break;
435 }
436 if ((fs = getfsfile(*argv)) == NULL &&
437 (fs = getfsspec(*argv)) == NULL)
438 errx(1, "%s: unknown special file or file system",
439 *argv);
440 if (BADTYPE(fs->fs_type))
441 errx(1, "%s has unknown file system type",
442 *argv);
443 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
444 init_flags, options, fs->fs_mntops);
445 break;
446 case 2:
447 /*
448 * If -t flag has not been specified, the path cannot be
449 * found, spec contains either a ':' or a '@', then assume
450 * that an NFS file system is being specified ala Sun.
451 * Check if the hostname contains only allowed characters
452 * to reduce false positives. IPv6 addresses containing
453 * ':' will be correctly parsed only if the separator is '@'.
454 * The definition of a valid hostname is taken from RFC 1034.
455 */
456 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
457 (ep = strchr(argv[0], ':')) != NULL)) {
458 if (*ep == '@') {
459 cp = ep + 1;
460 ep = cp + strlen(cp);
461 } else
462 cp = argv[0];
463 while (cp != ep) {
464 if (!isdigit(*cp) && !isalpha(*cp) &&
465 *cp != '.' && *cp != '-' && *cp != ':')
466 break;
467 cp++;
468 }
469 if (cp == ep)
470 vfstype = "nfs";
471 }
472 rval = mountfs(vfstype,
473 argv[0], argv[1], init_flags, options, NULL);
474 break;
475 default:
476 usage();
477 /* NOTREACHED */
478 }
479
480 /*
481 * If the mount was successfully, and done by root, tell mountd the
482 * good news.
483 */
484 if (rval == 0 && getuid() == 0)
485 restart_mountd();
486
487 exit(rval);
488 }
489
490 int
ismounted(struct fstab * fs,struct statfs * mntbuf,int mntsize)491 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
492 {
493 char realfsfile[PATH_MAX];
494 int i;
495
496 if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
497 /* the root file system can always be remounted */
498 return (0);
499
500 /* The user may have specified a symlink in fstab, resolve the path */
501 if (realpath(fs->fs_file, realfsfile) == NULL) {
502 /* Cannot resolve the path, use original one */
503 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
504 }
505
506 /*
507 * Consider the filesystem to be mounted if:
508 * It has the same mountpoint as a mounted filesytem, and
509 * It has the same type as that same mounted filesystem, and
510 * It has the same device name as that same mounted filesystem, OR
511 * It is a nonremountable filesystem
512 */
513 for (i = mntsize - 1; i >= 0; --i)
514 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
515 strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 &&
516 (!isremountable(fs->fs_vfstype) ||
517 (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
518 return (1);
519 return (0);
520 }
521
522 int
isremountable(const char * vfsname)523 isremountable(const char *vfsname)
524 {
525 const char **cp;
526
527 for (cp = remountable_fs_names; *cp; cp++)
528 if (strcmp(*cp, vfsname) == 0)
529 return (1);
530 return (0);
531 }
532
533 int
hasopt(const char * mntopts,const char * option)534 hasopt(const char *mntopts, const char *option)
535 {
536 int negative, found;
537 char *opt, *optbuf;
538
539 if (option[0] == 'n' && option[1] == 'o') {
540 negative = 1;
541 option += 2;
542 } else
543 negative = 0;
544 optbuf = strdup(mntopts);
545 found = 0;
546 for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
547 if (opt[0] == 'n' && opt[1] == 'o') {
548 if (!strcasecmp(opt + 2, option))
549 found = negative;
550 } else if (!strcasecmp(opt, option))
551 found = !negative;
552 }
553 free(optbuf);
554 return (found);
555 }
556
557 static void
append_arg(struct cpa * sa,char * arg)558 append_arg(struct cpa *sa, char *arg)
559 {
560 if (sa->c + 1 == sa->sz) {
561 sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
562 sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
563 if (sa->a == NULL)
564 errx(1, "realloc failed");
565 }
566 sa->a[++sa->c] = arg;
567 }
568
569 int
mountfs(const char * vfstype,const char * spec,const char * name,int flags,const char * options,const char * mntopts)570 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
571 const char *options, const char *mntopts)
572 {
573 struct statfs sf;
574 int i, ret;
575 char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
576 static struct cpa mnt_argv;
577
578 /* resolve the mountpoint with realpath(3) */
579 if (checkpath(name, mntpath) != 0) {
580 warn("%s", mntpath);
581 return (1);
582 }
583 name = mntpath;
584
585 if (mntopts == NULL)
586 mntopts = "";
587 optbuf = catopt(strdup(mntopts), options);
588
589 if (strcmp(name, "/") == 0)
590 flags |= MNT_UPDATE;
591 if (flags & MNT_FORCE)
592 optbuf = catopt(optbuf, "force");
593 if (flags & MNT_RDONLY)
594 optbuf = catopt(optbuf, "ro");
595 /*
596 * XXX
597 * The mount_mfs (newfs) command uses -o to select the
598 * optimization mode. We don't pass the default "-o rw"
599 * for that reason.
600 */
601 if (flags & MNT_UPDATE)
602 optbuf = catopt(optbuf, "update");
603
604 /* Compatibility glue. */
605 if (strcmp(vfstype, "msdos") == 0)
606 vfstype = "msdosfs";
607
608 /* Construct the name of the appropriate mount command */
609 (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
610
611 mnt_argv.c = -1;
612 append_arg(&mnt_argv, execname);
613 mangle(optbuf, &mnt_argv);
614 if (mountprog != NULL)
615 strlcpy(execname, mountprog, sizeof(execname));
616
617 append_arg(&mnt_argv, strdup(spec));
618 append_arg(&mnt_argv, strdup(name));
619 append_arg(&mnt_argv, NULL);
620
621 if (debug) {
622 if (use_mountprog(vfstype))
623 printf("exec: %s", execname);
624 else
625 printf("mount -t %s", vfstype);
626 for (i = 1; i < mnt_argv.c; i++)
627 (void)printf(" %s", mnt_argv.a[i]);
628 (void)printf("\n");
629 free(optbuf);
630 free(mountprog);
631 mountprog = NULL;
632 return (0);
633 }
634
635 if (use_mountprog(vfstype)) {
636 ret = exec_mountprog(name, execname, mnt_argv.a);
637 } else {
638 ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
639 }
640
641 free(optbuf);
642 free(mountprog);
643 mountprog = NULL;
644
645 if (verbose) {
646 if (statfs(name, &sf) < 0) {
647 warn("statfs %s", name);
648 return (1);
649 }
650 if (fstab_style)
651 putfsent(&sf);
652 else
653 prmount(&sf);
654 }
655
656 return (ret);
657 }
658
659 void
prmount(struct statfs * sfp)660 prmount(struct statfs *sfp)
661 {
662 uint64_t flags;
663 unsigned int i;
664 struct opt *o;
665 struct passwd *pw;
666
667 (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
668 sfp->f_fstypename);
669
670 flags = sfp->f_flags & MNT_VISFLAGMASK;
671 for (o = optnames; flags != 0 && o->o_opt != 0; o++)
672 if (flags & o->o_opt) {
673 (void)printf(", %s", o->o_name);
674 flags &= ~o->o_opt;
675 }
676 /*
677 * Inform when file system is mounted by an unprivileged user
678 * or privileged non-root user.
679 */
680 if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
681 (void)printf(", mounted by ");
682 if ((pw = getpwuid(sfp->f_owner)) != NULL)
683 (void)printf("%s", pw->pw_name);
684 else
685 (void)printf("%d", sfp->f_owner);
686 }
687 if (verbose) {
688 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
689 (void)printf(", writes: sync %ju async %ju",
690 (uintmax_t)sfp->f_syncwrites,
691 (uintmax_t)sfp->f_asyncwrites);
692 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
693 (void)printf(", reads: sync %ju async %ju",
694 (uintmax_t)sfp->f_syncreads,
695 (uintmax_t)sfp->f_asyncreads);
696 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
697 printf(", fsid ");
698 for (i = 0; i < sizeof(sfp->f_fsid); i++)
699 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
700 }
701 }
702 (void)printf(")\n");
703 }
704
705 struct statfs *
getmntpt(const char * name)706 getmntpt(const char *name)
707 {
708 struct statfs *mntbuf;
709 int i, mntsize;
710
711 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
712 for (i = mntsize - 1; i >= 0; i--) {
713 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
714 strcmp(mntbuf[i].f_mntonname, name) == 0)
715 return (&mntbuf[i]);
716 }
717 return (NULL);
718 }
719
720 char *
catopt(char * s0,const char * s1)721 catopt(char *s0, const char *s1)
722 {
723 char *cp;
724
725 if (s1 == NULL || *s1 == '\0')
726 return (s0);
727
728 if (s0 && *s0) {
729 if (asprintf(&cp, "%s,%s", s0, s1) == -1)
730 errx(1, "asprintf failed");
731 } else
732 cp = strdup(s1);
733
734 if (s0)
735 free(s0);
736 return (cp);
737 }
738
739 void
mangle(char * options,struct cpa * a)740 mangle(char *options, struct cpa *a)
741 {
742 char *p, *s, *val;
743
744 for (s = options; (p = strsep(&s, ",")) != NULL;)
745 if (*p != '\0') {
746 if (strcmp(p, "noauto") == 0) {
747 /*
748 * Do not pass noauto option to nmount().
749 * or external mount program. noauto is
750 * only used to prevent mounting a filesystem
751 * when 'mount -a' is specified, and is
752 * not a real mount option.
753 */
754 continue;
755 } else if (strcmp(p, "late") == 0) {
756 /*
757 * "late" is used to prevent certain file
758 * systems from being mounted before late
759 * in the boot cycle; for instance,
760 * loopback NFS mounts can't be mounted
761 * before mountd starts.
762 */
763 continue;
764 } else if (strcmp(p, "failok") == 0) {
765 /*
766 * "failok" is used to prevent certain file
767 * systems from being causing the system to
768 * drop into single user mode in the boot
769 * cycle, and is not a real mount option.
770 */
771 continue;
772 } else if (strncmp(p, "mountprog", 9) == 0) {
773 /*
774 * "mountprog" is used to force the use of
775 * userland mount programs.
776 */
777 val = strchr(p, '=');
778 if (val != NULL) {
779 ++val;
780 if (*val != '\0')
781 mountprog = strdup(val);
782 }
783
784 if (mountprog == NULL) {
785 errx(1, "Need value for -o mountprog");
786 }
787 continue;
788 } else if (strcmp(p, "userquota") == 0) {
789 continue;
790 } else if (strncmp(p, userquotaeq,
791 sizeof(userquotaeq) - 1) == 0) {
792 continue;
793 } else if (strcmp(p, "groupquota") == 0) {
794 continue;
795 } else if (strncmp(p, groupquotaeq,
796 sizeof(groupquotaeq) - 1) == 0) {
797 continue;
798 } else if (*p == '-') {
799 append_arg(a, p);
800 p = strchr(p, '=');
801 if (p != NULL) {
802 *p = '\0';
803 append_arg(a, p + 1);
804 }
805 } else {
806 append_arg(a, strdup("-o"));
807 append_arg(a, p);
808 }
809 }
810 }
811
812
813 char *
update_options(char * opts,char * fstab,int curflags)814 update_options(char *opts, char *fstab, int curflags)
815 {
816 char *o, *p;
817 char *cur;
818 char *expopt, *newopt, *tmpopt;
819
820 if (opts == NULL)
821 return (strdup(""));
822
823 /* remove meta options from list */
824 remopt(fstab, MOUNT_META_OPTION_FSTAB);
825 remopt(fstab, MOUNT_META_OPTION_CURRENT);
826 cur = flags2opts(curflags);
827
828 /*
829 * Expand all meta-options passed to us first.
830 */
831 expopt = NULL;
832 for (p = opts; (o = strsep(&p, ",")) != NULL;) {
833 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
834 expopt = catopt(expopt, fstab);
835 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
836 expopt = catopt(expopt, cur);
837 else
838 expopt = catopt(expopt, o);
839 }
840 free(cur);
841 free(opts);
842
843 /*
844 * Remove previous contradictory arguments. Given option "foo" we
845 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
846 * and "foo" - so we can deal with possible options like "notice".
847 */
848 newopt = NULL;
849 for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
850 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
851 errx(1, "malloc failed");
852
853 strcpy(tmpopt, "no");
854 strcat(tmpopt, o);
855 remopt(newopt, tmpopt);
856 free(tmpopt);
857
858 if (strncmp("no", o, 2) == 0)
859 remopt(newopt, o+2);
860
861 newopt = catopt(newopt, o);
862 }
863 free(expopt);
864
865 return (newopt);
866 }
867
868 void
remopt(char * string,const char * opt)869 remopt(char *string, const char *opt)
870 {
871 char *o, *p, *r;
872
873 if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
874 return;
875
876 r = string;
877
878 for (p = string; (o = strsep(&p, ",")) != NULL;) {
879 if (strcmp(opt, o) != 0) {
880 if (*r == ',' && *o != '\0')
881 r++;
882 while ((*r++ = *o++) != '\0')
883 ;
884 *--r = ',';
885 }
886 }
887 *r = '\0';
888 }
889
890 void
usage(void)891 usage(void)
892 {
893
894 (void)fprintf(stderr, "%s\n%s\n%s\n",
895 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
896 " mount [-dfpruvw] special | node",
897 " mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
898 exit(1);
899 }
900
901 void
putfsent(struct statfs * ent)902 putfsent(struct statfs *ent)
903 {
904 struct fstab *fst;
905 char *opts, *rw;
906 int l;
907
908 opts = NULL;
909 /* flags2opts() doesn't return the "rw" option. */
910 if ((ent->f_flags & MNT_RDONLY) != 0)
911 rw = NULL;
912 else
913 rw = catopt(NULL, "rw");
914
915 opts = flags2opts(ent->f_flags);
916 opts = catopt(rw, opts);
917
918 if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
919 strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
920 strlcpy(ent->f_mntfromname,
921 (strnstr(ent->f_mntfromname, ":", 8) +1),
922 sizeof(ent->f_mntfromname));
923 }
924
925 l = strlen(ent->f_mntfromname);
926 printf("%s%s%s%s", ent->f_mntfromname,
927 l < 8 ? "\t" : "",
928 l < 16 ? "\t" : "",
929 l < 24 ? "\t" : " ");
930 l = strlen(ent->f_mntonname);
931 printf("%s%s%s%s", ent->f_mntonname,
932 l < 8 ? "\t" : "",
933 l < 16 ? "\t" : "",
934 l < 24 ? "\t" : " ");
935 printf("%s\t", ent->f_fstypename);
936 l = strlen(opts);
937 printf("%s%s", opts,
938 l < 8 ? "\t" : " ");
939 free(opts);
940
941 if ((fst = getfsspec(ent->f_mntfromname)))
942 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
943 else if ((fst = getfsfile(ent->f_mntonname)))
944 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
945 else if (strcmp(ent->f_fstypename, "ufs") == 0) {
946 if (strcmp(ent->f_mntonname, "/") == 0)
947 printf("\t1 1\n");
948 else
949 printf("\t2 2\n");
950 } else
951 printf("\t0 0\n");
952 }
953
954
955 char *
flags2opts(int flags)956 flags2opts(int flags)
957 {
958 char *res;
959
960 res = NULL;
961
962 if (flags & MNT_RDONLY) res = catopt(res, "ro");
963 if (flags & MNT_SYNCHRONOUS) res = catopt(res, "sync");
964 if (flags & MNT_NOEXEC) res = catopt(res, "noexec");
965 if (flags & MNT_NOSUID) res = catopt(res, "nosuid");
966 if (flags & MNT_UNION) res = catopt(res, "union");
967 if (flags & MNT_ASYNC) res = catopt(res, "async");
968 if (flags & MNT_NOATIME) res = catopt(res, "noatime");
969 if (flags & MNT_NOCLUSTERR) res = catopt(res, "noclusterr");
970 if (flags & MNT_NOCLUSTERW) res = catopt(res, "noclusterw");
971 if (flags & MNT_NOSYMFOLLOW) res = catopt(res, "nosymfollow");
972 if (flags & MNT_SUIDDIR) res = catopt(res, "suiddir");
973 if (flags & MNT_MULTILABEL) res = catopt(res, "multilabel");
974 if (flags & MNT_ACLS) res = catopt(res, "acls");
975 if (flags & MNT_NFS4ACLS) res = catopt(res, "nfsv4acls");
976 if (flags & MNT_UNTRUSTED) res = catopt(res, "untrusted");
977
978 return (res);
979 }
980