]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/mount.c
mount: Recognize zero as a valid value for the port= option
[nfs-utils.git] / utils / mount / mount.c
1 /*
2  * mount.c -- Linux NFS mount
3  *
4  * Copyright (C) 2006 Amit Gud <agud@redhat.com>
5  *
6  * - Basic code and wrapper around mount and umount code of NFS.
7  *   Based on util-linux/mount/mount.c.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <sys/mount.h>
33 #include <getopt.h>
34 #include <mntent.h>
35 #include <pwd.h>
36
37 #include "fstab.h"
38 #include "xcommon.h"
39 #include "nls.h"
40 #include "mount_constants.h"
41 #include "mount_config.h"
42 #include "nfs_paths.h"
43 #include "nfs_mntent.h"
44
45 #include "nfs_mount.h"
46 #include "nfs4_mount.h"
47 #include "mount.h"
48 #include "error.h"
49 #include "stropts.h"
50 #include "version.h"
51
52 char *progname;
53 int nfs_mount_data_version;
54 int nomtab;
55 int verbose;
56 int sloppy;
57 int string;
58
59 #define FOREGROUND      (0)
60 #define BACKGROUND      (1)
61
62 static struct option longopts[] = {
63   { "fake", 0, 0, 'f' },
64   { "help", 0, 0, 'h' },
65   { "no-mtab", 0, 0, 'n' },
66   { "read-only", 0, 0, 'r' },
67   { "ro", 0, 0, 'r' },
68   { "verbose", 0, 0, 'v' },
69   { "version", 0, 0, 'V' },
70   { "read-write", 0, 0, 'w' },
71   { "rw", 0, 0, 'w' },
72   { "options", 1, 0, 'o' },
73   { NULL, 0, 0, 0 }
74 };
75
76 /*
77  * Map from -o and fstab option strings to the flag argument to mount(2).
78  */
79 struct opt_map {
80         const char *opt;        /* option name */
81         int skip;               /* skip in mtab option string */
82         int inv;                /* true if flag value should be inverted */
83         int mask;               /* flag mask value */
84 };
85
86 static const struct opt_map opt_map[] = {
87   { "defaults", 0, 0, 0         },      /* default options */
88   { "ro",       1, 0, MS_RDONLY },      /* read-only */
89   { "rw",       1, 1, MS_RDONLY },      /* read-write */
90   { "exec",     0, 1, MS_NOEXEC },      /* permit execution of binaries */
91   { "noexec",   0, 0, MS_NOEXEC },      /* don't execute binaries */
92   { "suid",     0, 1, MS_NOSUID },      /* honor suid executables */
93   { "nosuid",   0, 0, MS_NOSUID },      /* don't honor suid executables */
94   { "dev",      0, 1, MS_NODEV  },      /* interpret device files  */
95   { "nodev",    0, 0, MS_NODEV  },      /* don't interpret devices */
96   { "sync",     0, 0, MS_SYNCHRONOUS},  /* synchronous I/O */
97   { "async",    0, 1, MS_SYNCHRONOUS},  /* asynchronous I/O */
98   { "dirsync",  0, 0, MS_DIRSYNC},      /* synchronous directory modifications */
99   { "remount",  0, 0, MS_REMOUNT},      /* Alter flags of mounted FS */
100   { "bind",     0, 0, MS_BIND   },      /* Remount part of tree elsewhere */
101   { "rbind",    0, 0, MS_BIND|MS_REC }, /* Idem, plus mounted subtrees */
102   { "auto",     0, 0, MS_DUMMY },       /* Can be mounted using -a */
103   { "noauto",   0, 0, MS_DUMMY },       /* Can  only be mounted explicitly */
104   { "users",    1, 0, MS_USERS },       /* Allow ordinary user to mount */
105   { "nousers",  0, 1, MS_DUMMY  },      /* Forbid ordinary user to mount */
106   { "user",     1, 0, MS_USER  },       /* Allow ordinary user to mount */
107   { "nouser",   0, 1, MS_DUMMY   },     /* Forbid ordinary user to mount */
108   { "owner",    0, 0, MS_DUMMY  },      /* Let the owner of the device mount */
109   { "noowner",  0, 0, MS_DUMMY  },      /* Device owner has no special privs */
110   { "group",    0, 0, MS_DUMMY  },      /* Let the group of the device mount */
111   { "nogroup",  0, 0, MS_DUMMY  },      /* Device group has no special privs */
112   { "_netdev",  0, 0, MS_DUMMY},        /* Device requires network */
113   { "comment",  0, 0, MS_DUMMY},        /* fstab comment only (kudzu,_netdev)*/
114
115   /* add new options here */
116 #ifdef MS_NOSUB
117   { "sub",      0, 1, MS_NOSUB  },      /* allow submounts */
118   { "nosub",    0, 0, MS_NOSUB  },      /* don't allow submounts */
119 #endif
120 #ifdef MS_SILENT
121   { "quiet",    0, 0, MS_SILENT    },   /* be quiet  */
122   { "loud",     0, 1, MS_SILENT    },   /* print out messages. */
123 #endif
124 #ifdef MS_MANDLOCK
125   { "mand",     0, 0, MS_MANDLOCK },    /* Allow mandatory locks on this FS */
126   { "nomand",   0, 1, MS_MANDLOCK },    /* Forbid mandatory locks on this FS */
127 #endif
128   { "loop",     1, 0, MS_DUMMY   },      /* use a loop device */
129 #ifdef MS_NOATIME
130   { "atime",    0, 1, MS_NOATIME },     /* Update access time */
131   { "noatime",  0, 0, MS_NOATIME },     /* Do not update access time */
132 #endif
133 #ifdef MS_NODIRATIME
134   { "diratime", 0, 1, MS_NODIRATIME },  /* Update dir access times */
135   { "nodiratime", 0, 0, MS_NODIRATIME },/* Do not update dir access times */
136 #endif
137 #ifdef MS_RELATIME
138   { "relatime", 0, 0, MS_RELATIME },   /* Update access times relative to
139                       mtime/ctime */
140   { "norelatime", 0, 1, MS_RELATIME }, /* Update access time without regard
141                       to mtime/ctime */
142 #endif
143   { "noquota", 0, 0, MS_DUMMY },        /* Don't enforce quota */
144   { "quota", 0, 0, MS_DUMMY },          /* Enforce user quota */
145   { "usrquota", 0, 0, MS_DUMMY },       /* Enforce user quota */
146   { "grpquota", 0, 0, MS_DUMMY },       /* Enforce group quota */
147   { NULL,       0, 0, 0         }
148 };
149
150 static void parse_opts(const char *options, int *flags, char **extra_opts);
151
152 /*
153  * Choose the version of the nfs_mount_data structure that is appropriate
154  * for the kernel that is doing the mount.
155  *
156  * NFS_MOUNT_VERSION:           maximum version supported by these sources
157  * nfs_mount_data_version:      maximum version supported by the running kernel
158  */
159 static void discover_nfs_mount_data_version(void)
160 {
161         unsigned int kernel_version = linux_version_code();
162
163         if (kernel_version) {
164                 if (kernel_version < MAKE_VERSION(2, 1, 32))
165                         nfs_mount_data_version = 1;
166                 else if (kernel_version < MAKE_VERSION(2, 2, 18))
167                         nfs_mount_data_version = 3;
168                 else if (kernel_version < MAKE_VERSION(2, 3, 0))
169                         nfs_mount_data_version = 4;
170                 else if (kernel_version < MAKE_VERSION(2, 3, 99))
171                         nfs_mount_data_version = 3;
172                 else if (kernel_version < MAKE_VERSION(2, 6, 3))
173                         nfs_mount_data_version = 4;
174                 else
175                         nfs_mount_data_version = 6;
176         }
177         if (nfs_mount_data_version > NFS_MOUNT_VERSION)
178                 nfs_mount_data_version = NFS_MOUNT_VERSION;
179         else
180                 if (kernel_version > MAKE_VERSION(2, 6, 22))
181                         string++;
182 }
183
184 static void print_one(char *spec, char *node, char *type, char *opts)
185 {
186         if (!verbose)
187                 return;
188
189         if (opts)
190                 printf(_("%s on %s type %s (%s)\n"), spec, node, type, opts);
191         else
192                 printf(_("%s on %s type %s\n"), spec, node, type);
193 }
194
195 /*
196  * Build a canonical mount option string for /etc/mtab.
197  */
198 static char *fix_opts_string(int flags, const char *extra_opts)
199 {
200         const struct opt_map *om;
201         char *new_opts;
202
203         new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");
204         if (flags & MS_USER) {
205                 /* record who mounted this so they can unmount */
206                 struct passwd *pw = getpwuid(getuid());
207                 if(pw)
208                         new_opts = xstrconcat3(new_opts, ",user=", pw->pw_name);
209         }
210         if (flags & MS_USERS)
211                 new_opts = xstrconcat3(new_opts, ",users", "");
212
213         for (om = opt_map; om->opt != NULL; om++) {
214                 if (om->skip)
215                         continue;
216                 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
217                         continue;
218                 new_opts = xstrconcat3(new_opts, ",", om->opt);
219                 flags &= ~om->mask;
220         }
221         if (extra_opts && *extra_opts) {
222                 new_opts = xstrconcat3(new_opts, ",", extra_opts);
223         }
224         return new_opts;
225 }
226
227 static void
228 init_mntent(struct mntent *mnt, char *fsname, char *dir, char *type,
229                 int flags, char *opts)
230 {
231         mnt->mnt_fsname = fsname;
232         mnt->mnt_dir    = dir;
233         mnt->mnt_type   = type;
234         mnt->mnt_opts   = fix_opts_string(flags & ~MS_NOMTAB, opts);
235
236         /* these are always zero for NFS */
237         mnt->mnt_freq   = 0;
238         mnt->mnt_passno = 0;
239 }
240
241 /* Create mtab with a root entry.  */
242 static void
243 create_mtab (void) {
244         struct mntentchn *fstab;
245         struct mntent mnt;
246         int flags;
247         mntFILE *mfp;
248
249         lock_mtab();
250
251         mfp = nfs_setmntent (MOUNTED, "a+");
252         if (mfp == NULL || mfp->mntent_fp == NULL) {
253                 int errsv = errno;
254                 die (EX_FILEIO, _("mount: can't open %s for writing: %s"),
255                      MOUNTED, strerror (errsv));
256         }
257
258         /* Find the root entry by looking it up in fstab */
259         if ((fstab = getfsfile ("/")) || (fstab = getfsfile ("root"))) {
260                 char *extra_opts;
261                 parse_opts (fstab->m.mnt_opts, &flags, &extra_opts);
262                 init_mntent(&mnt, xstrdup(fstab->m.mnt_fsname), "/",
263                                 fstab->m.mnt_type, flags, extra_opts);
264                 free(extra_opts);
265
266                 if (nfs_addmntent (mfp, &mnt) == 1) {
267                         int errsv = errno;
268                         die (EX_FILEIO, _("mount: error writing %s: %s"),
269                              _PATH_MOUNTED, strerror (errsv));
270                 }
271         }
272         if (fchmod (fileno (mfp->mntent_fp), 0644) < 0)
273                 if (errno != EROFS) {
274                         int errsv = errno;
275                         die (EX_FILEIO,
276                              _("mount: error changing mode of %s: %s"),
277                              _PATH_MOUNTED, strerror (errsv));
278                 }
279         nfs_endmntent (mfp);
280
281         unlock_mtab();
282
283         reset_mtab_info();
284 }
285
286 static int add_mtab(char *spec, char *mount_point, char *fstype,
287                         int flags, char *opts)
288 {
289         struct mntent ment;
290         int result = EX_SUCCESS;
291
292         init_mntent(&ment, spec, mount_point, fstype, flags, opts);
293
294         if (!nomtab && mtab_does_not_exist()) {
295                 if (verbose > 1)
296                         printf(_("mount: no %s found - creating it..\n"),
297                                MOUNTED);
298                 create_mtab ();
299         }
300
301         if (!nomtab && mtab_is_writable()) {
302                 if (flags & MS_REMOUNT)
303                         update_mtab(ment.mnt_dir, &ment);
304                 else {
305                         mntFILE *mtab;
306                         
307                         lock_mtab();
308                         mtab = nfs_setmntent(MOUNTED, "a+");
309                         if (mtab == NULL || mtab->mntent_fp == NULL) {
310                                 nfs_error(_("Can't open mtab: %s"),
311                                                 strerror(errno));
312                                 result = EX_FILEIO;
313                         } else {
314                                 if (nfs_addmntent(mtab, &ment) == 1) {
315                                         nfs_error(_("Can't write mount entry to mtab: %s"),
316                                                         strerror(errno));
317                                         result = EX_FILEIO;
318                                 }
319                         }
320                         nfs_endmntent(mtab);
321                         unlock_mtab();
322                 }
323         }
324
325         free(ment.mnt_opts);
326
327         return result;
328 }
329
330 static void mount_usage(void)
331 {
332         printf(_("usage: %s remotetarget dir [-rvVwfnsih] [-o nfsoptions]\n"),
333                 progname);
334         printf(_("options:\n"));
335         printf(_("\t-r\t\tMount file system readonly\n"));
336         printf(_("\t-v\t\tVerbose\n"));
337         printf(_("\t-V\t\tPrint version\n"));
338         printf(_("\t-w\t\tMount file system read-write\n"));
339         printf(_("\t-f\t\tFake mount, do not actually mount\n"));
340         printf(_("\t-n\t\tDo not update /etc/mtab\n"));
341         printf(_("\t-s\t\tTolerate sloppy mount options rather than fail\n"));
342         printf(_("\t-h\t\tPrint this help\n"));
343         printf(_("\tnfsoptions\tRefer to mount.nfs(8) or nfs(5)\n\n"));
344 }
345
346 static void parse_opt(const char *opt, int *mask, char *extra_opts, size_t len)
347 {
348         const struct opt_map *om;
349
350         for (om = opt_map; om->opt != NULL; om++) {
351                 if (!strcmp (opt, om->opt)) {
352                         if (om->inv)
353                                 *mask &= ~om->mask;
354                         else
355                                 *mask |= om->mask;
356                         return;
357                 }
358         }
359
360         len -= strlen(extra_opts);
361
362         if (*extra_opts && --len > 0)
363                 strcat(extra_opts, ",");
364
365         if ((len -= strlen(opt)) > 0)
366                 strcat(extra_opts, opt);
367 }
368
369 /*
370  * Convert the provided mount command-line options into the 4th &
371  * 5th arguments to mount(2).  Output parameter "@flags" gets the
372  * standard options (indicated by MS_ bits), and output parameter
373  * "@extra_opts" gets all the filesystem-specific options.
374  */
375 static void parse_opts(const char *options, int *flags, char **extra_opts)
376 {
377         if (options != NULL) {
378                 char *opts = xstrdup(options);
379                 char *opt, *p;
380                 size_t len = strlen(opts) + 1;  /* include room for a null */
381                 int open_quote = 0;
382
383                 *extra_opts = xmalloc(len);
384                 **extra_opts = '\0';
385
386                 for (p = opts, opt = NULL; p && *p; p++) {
387                         if (!opt)
388                                 opt = p;        /* begin of the option item */
389                         if (*p == '"')
390                                 open_quote ^= 1; /* reverse the status */
391                         if (open_quote)
392                                 continue;       /* still in a quoted block */
393                         if (*p == ',')
394                                 *p = '\0';      /* terminate the option item */
395
396                         /* end of option item or last item */
397                         if (*p == '\0' || *(p + 1) == '\0') {
398                                 parse_opt(opt, flags, *extra_opts, len);
399                                 opt = NULL;
400                         }
401                 }
402                 free(opts);
403         }
404 }
405
406 static int chk_mountpoint(char *mount_point)
407 {
408         struct stat sb;
409
410         if (stat(mount_point, &sb) < 0){
411                 mount_error(NULL, mount_point, errno);
412                 return 1;
413         }
414         if (S_ISDIR(sb.st_mode) == 0){
415                 mount_error(NULL, mount_point, ENOTDIR);
416                 return 1;
417         }
418         if (access(mount_point, X_OK) < 0) {
419                 mount_error(NULL, mount_point, errno);
420                 return 1;
421         }
422
423         return 0;
424 }
425
426 static int try_mount(char *spec, char *mount_point, int flags,
427                         char *fs_type, char **extra_opts, char *mount_opts,
428                         int fake, int bg)
429 {
430         int ret;
431
432         if (string)
433                 ret = nfsmount_string(spec, mount_point, fs_type, flags,
434                                         extra_opts, fake, bg);
435         else {
436                 if (strcmp(fs_type, "nfs4") == 0)
437                         ret = nfs4mount(spec, mount_point, flags,
438                                         extra_opts, fake, bg);
439                 else
440                         ret = nfsmount(spec, mount_point, flags,
441                                         extra_opts, fake, bg);
442         }
443
444         if (ret)
445                 return ret;
446
447         if (!fake)
448                 print_one(spec, mount_point, fs_type, mount_opts);
449
450         return add_mtab(spec, mount_point, fs_type, flags, *extra_opts);
451 }
452
453 int main(int argc, char *argv[])
454 {
455         int c, flags = 0, mnt_err = 1, fake = 0;
456         char *spec, *mount_point, *fs_type = "nfs";
457         char *extra_opts = NULL, *mount_opts = NULL;
458         uid_t uid = getuid();
459
460         progname = basename(argv[0]);
461
462         discover_nfs_mount_data_version();
463
464         if(!strncmp(progname, "umount", strlen("umount")))
465                 exit(nfsumount(argc, argv));
466
467         if (argv[1] && argv[1][0] == '-') {
468                 if(argv[1][1] == 'V')
469                         printf("%s ("PACKAGE_STRING")\n", progname);
470                 else
471                         mount_usage();
472                 exit(EX_SUCCESS);
473         }
474
475         if ((argc < 3)) {
476                 mount_usage();
477                 exit(EX_USAGE);
478         }
479
480         spec = argv[1];
481         mount_point = argv[2];
482
483         mount_config_init(progname);
484
485         argv[2] = argv[0]; /* so that getopt error messages are correct */
486         while ((c = getopt_long(argc - 2, argv + 2, "rvVwfno:hs",
487                                 longopts, NULL)) != -1) {
488                 switch (c) {
489                 case 'r':
490                         flags |= MS_RDONLY;
491                         break;
492                 case 'v':
493                         ++verbose;
494                         break;
495                 case 'V':
496                         printf("%s: ("PACKAGE_STRING")\n", progname);
497                         exit(EX_SUCCESS);
498                 case 'w':
499                         flags &= ~MS_RDONLY;
500                         break;
501                 case 'f':
502                         ++fake;
503                         break;
504                 case 'n':
505                         ++nomtab;
506                         break;
507                 case 'o':              /* specify mount options */
508                         if (mount_opts)
509                                 mount_opts = xstrconcat3(mount_opts, ",", optarg);
510                         else
511                                 mount_opts = xstrdup(optarg);
512                         break;
513                 case 's':
514                         ++sloppy;
515                         break;
516                 case 'h':
517                 default:
518                         mount_usage();
519                         goto out_usage;
520                 }
521         }
522
523         /*
524          * Extra non-option words at the end are bogus...
525          */
526         if (optind != argc - 2) {
527                 mount_usage();
528                 goto out_usage;
529         }
530
531         if (strcmp(progname, "mount.nfs4") == 0)
532                 fs_type = "nfs4";
533
534         /*
535          * If a non-root user is attempting to mount, make sure the
536          * user's requested options match the options specified in
537          * /etc/fstab; otherwise, don't allow the mount.
538          */
539         if (uid != 0) {
540                 struct mntentchn *mc;
541
542                 if ((mc = getfsfile(mount_point)) == NULL ||
543                     strcmp(mc->m.mnt_fsname, spec) != 0 ||
544                     strcmp(mc->m.mnt_type, fs_type) != 0) {
545                         nfs_error(_("%s: permission denied: no match for %s "
546                                 "found in /etc/fstab"), progname, mount_point);
547                         goto out_usage;
548                 }
549
550                 /*
551                  * 'mount' munges the options from fstab before passing them
552                  * to us, so it is non-trivial to test that we have the correct
553                  * set of options and we don't want to trust what the user
554                  * gave us, so just take whatever is in /etc/fstab.
555                  */
556                 mount_opts = strdup(mc->m.mnt_opts);
557         }
558
559         mount_point = canonicalize(mount_point);
560         if (!mount_point) {
561                 nfs_error(_("%s: no mount point provided"), progname);
562                 goto out_usage;
563         }
564         if (mount_point[0] != '/') {
565                 nfs_error(_("%s: unrecognized mount point %s"),
566                         progname, mount_point);
567                 mnt_err = EX_USAGE;
568                 goto out;
569         }
570         /*
571          * Concatenate mount options from the configuration file
572          */
573         mount_opts = mount_config_opts(spec, mount_point, mount_opts);
574
575         parse_opts(mount_opts, &flags, &extra_opts);
576
577         if (uid != 0) {
578                 if (!(flags & (MS_USERS|MS_USER))) {
579                         nfs_error(_("%s: permission denied"), progname);
580                         mnt_err = EX_USAGE;
581                         goto out;
582                 }
583
584                 if (geteuid() != 0) {
585                         nfs_error(_("%s: not installed setuid - "
586                                     "\"user\" NFS mounts not supported."), progname);
587                         exit(EX_FAIL);
588                 }
589         }
590
591         if (chk_mountpoint(mount_point)) {
592                 mnt_err = EX_USAGE;
593                 goto out;
594         }
595
596         mnt_err = try_mount(spec, mount_point, flags, fs_type, &extra_opts,
597                                 mount_opts, fake, FOREGROUND);
598         if (mnt_err == EX_BG) {
599                 printf(_("%s: backgrounding \"%s\"\n"),
600                         progname, spec);
601                 printf(_("%s: mount options: \"%s\"\n"),
602                         progname, extra_opts);
603
604                 fflush(stdout);
605
606                 /*
607                  * Parent exits immediately with success.
608                  */
609                 if (daemon(0, 0)) {
610                         nfs_error(_("%s: failed to start "
611                                         "background process: %s\n"),
612                                                 progname, strerror(errno));
613                         exit(EX_FAIL);
614                 }
615
616                 mnt_err = try_mount(spec, mount_point, flags, fs_type,
617                                         &extra_opts, mount_opts, fake,
618                                         BACKGROUND);
619                 if (verbose && mnt_err)
620                         printf(_("%s: giving up \"%s\"\n"),
621                                 progname, spec);
622         }
623
624 out:
625         free(mount_opts);
626         free(extra_opts);
627         free(mount_point);
628         exit(mnt_err);
629
630 out_usage:
631         free(mount_opts);
632         exit(EX_USAGE);
633 }