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