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