]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/mount.c
cc078eba336db7f47ad7bae1a5e9f6826ec0a973
[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 "utils.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  * Build a canonical mount option string for /etc/mtab.
154  */
155 static char *fix_opts_string(int flags, const char *extra_opts)
156 {
157         const struct opt_map *om;
158         char *new_opts;
159
160         new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");
161         if (flags & MS_USER) {
162                 /* record who mounted this so they can unmount */
163                 struct passwd *pw = getpwuid(getuid());
164                 if(pw)
165                         new_opts = xstrconcat3(new_opts, ",user=", pw->pw_name);
166         }
167         if (flags & MS_USERS)
168                 new_opts = xstrconcat3(new_opts, ",users", "");
169
170         for (om = opt_map; om->opt != NULL; om++) {
171                 if (om->skip)
172                         continue;
173                 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
174                         continue;
175                 new_opts = xstrconcat3(new_opts, ",", om->opt);
176                 flags &= ~om->mask;
177         }
178         if (extra_opts && *extra_opts) {
179                 new_opts = xstrconcat3(new_opts, ",", extra_opts);
180         }
181         return new_opts;
182 }
183
184 static void
185 init_mntent(struct mntent *mnt, char *fsname, char *dir, char *type,
186                 int flags, char *opts)
187 {
188         mnt->mnt_fsname = fsname;
189         mnt->mnt_dir    = dir;
190         mnt->mnt_type   = type;
191         mnt->mnt_opts   = fix_opts_string(flags & ~MS_NOMTAB, opts);
192
193         /* these are always zero for NFS */
194         mnt->mnt_freq   = 0;
195         mnt->mnt_passno = 0;
196 }
197
198 /* Create mtab with a root entry.  */
199 static void
200 create_mtab (void) {
201         struct mntentchn *fstab;
202         struct mntent mnt;
203         int flags;
204         mntFILE *mfp;
205
206         /* Avoid writing if the mtab is a symlink to /proc/mounts, since
207            that would create a file /proc/mounts in case the proc filesystem
208            is not mounted, and the fchmod below would also fail. */
209         if (mtab_is_a_symlink()) {
210                 return EX_SUCCESS;
211         }
212
213         lock_mtab();
214
215         mfp = nfs_setmntent (MOUNTED, "a+");
216         if (mfp == NULL || mfp->mntent_fp == NULL) {
217                 int errsv = errno;
218                 die (EX_FILEIO, _("mount: can't open %s for writing: %s"),
219                      MOUNTED, strerror (errsv));
220         }
221
222         /* Find the root entry by looking it up in fstab */
223         if ((fstab = getfsfile ("/")) || (fstab = getfsfile ("root"))) {
224                 char *extra_opts;
225                 parse_opts (fstab->m.mnt_opts, &flags, &extra_opts);
226                 init_mntent(&mnt, xstrdup(fstab->m.mnt_fsname), "/",
227                                 fstab->m.mnt_type, flags, extra_opts);
228                 free(extra_opts);
229
230                 if (nfs_addmntent (mfp, &mnt) == 1) {
231                         int errsv = errno;
232                         die (EX_FILEIO, _("mount: error writing %s: %s"),
233                              _PATH_MOUNTED, strerror (errsv));
234                 }
235         }
236         if (fchmod (fileno (mfp->mntent_fp), 0644) < 0)
237                 if (errno != EROFS) {
238                         int errsv = errno;
239                         die (EX_FILEIO,
240                              _("mount: error changing mode of %s: %s"),
241                              _PATH_MOUNTED, strerror (errsv));
242                 }
243         nfs_endmntent (mfp);
244
245         unlock_mtab();
246
247         reset_mtab_info();
248 }
249
250 static int add_mtab(char *spec, char *mount_point, char *fstype,
251                         int flags, char *opts)
252 {
253         struct mntent ment;
254         int result = EX_SUCCESS;
255
256         init_mntent(&ment, spec, mount_point, fstype, flags, opts);
257
258         if (!nomtab && mtab_does_not_exist()) {
259                 if (verbose > 1)
260                         printf(_("mount: no %s found - creating it..\n"),
261                                MOUNTED);
262                 create_mtab ();
263         }
264
265         if (!nomtab && mtab_is_writable()) {
266                 if (flags & MS_REMOUNT)
267                         update_mtab(ment.mnt_dir, &ment);
268                 else {
269                         mntFILE *mtab;
270                         
271                         lock_mtab();
272                         mtab = nfs_setmntent(MOUNTED, "a+");
273                         if (mtab == NULL || mtab->mntent_fp == NULL) {
274                                 nfs_error(_("Can't open mtab: %s"),
275                                                 strerror(errno));
276                                 result = EX_FILEIO;
277                         } else {
278                                 if (nfs_addmntent(mtab, &ment) == 1) {
279                                         nfs_error(_("Can't write mount entry to mtab: %s"),
280                                                         strerror(errno));
281                                         result = EX_FILEIO;
282                                 }
283                         }
284                         nfs_endmntent(mtab);
285                         unlock_mtab();
286                 }
287         }
288
289         free(ment.mnt_opts);
290
291         return result;
292 }
293
294 static void parse_opt(const char *opt, int *mask, char *extra_opts, size_t len)
295 {
296         const struct opt_map *om;
297
298         for (om = opt_map; om->opt != NULL; om++) {
299                 if (!strcmp (opt, om->opt)) {
300                         if (om->inv)
301                                 *mask &= ~om->mask;
302                         else
303                                 *mask |= om->mask;
304                         return;
305                 }
306         }
307
308         len -= strlen(extra_opts);
309
310         if (*extra_opts && --len > 0)
311                 strcat(extra_opts, ",");
312
313         if ((len -= strlen(opt)) > 0)
314                 strcat(extra_opts, opt);
315 }
316
317 /*
318  * Convert the provided mount command-line options into the 4th &
319  * 5th arguments to mount(2).  Output parameter "@flags" gets the
320  * standard options (indicated by MS_ bits), and output parameter
321  * "@extra_opts" gets all the filesystem-specific options.
322  */
323 static void parse_opts(const char *options, int *flags, char **extra_opts)
324 {
325         if (options != NULL) {
326                 char *opts = xstrdup(options);
327                 char *opt, *p;
328                 size_t len = strlen(opts) + 1;  /* include room for a null */
329                 int open_quote = 0;
330
331                 *extra_opts = xmalloc(len);
332                 **extra_opts = '\0';
333
334                 for (p = opts, opt = NULL; p && *p; p++) {
335                         if (!opt)
336                                 opt = p;        /* begin of the option item */
337                         if (*p == '"')
338                                 open_quote ^= 1; /* reverse the status */
339                         if (open_quote)
340                                 continue;       /* still in a quoted block */
341                         if (*p == ',')
342                                 *p = '\0';      /* terminate the option item */
343
344                         /* end of option item or last item */
345                         if (*p == '\0' || *(p + 1) == '\0') {
346                                 parse_opt(opt, flags, *extra_opts, len);
347                                 opt = NULL;
348                         }
349                 }
350                 free(opts);
351         }
352 }
353
354 static int try_mount(char *spec, char *mount_point, int flags,
355                         char *fs_type, char **extra_opts, char *mount_opts,
356                         int fake, int bg)
357 {
358         int ret;
359
360         if (string)
361                 ret = nfsmount_string(spec, mount_point, fs_type, flags,
362                                         extra_opts, fake, bg);
363         else {
364                 if (strcmp(fs_type, "nfs4") == 0)
365                         ret = nfs4mount(spec, mount_point, flags,
366                                         extra_opts, fake, bg);
367                 else
368                         ret = nfsmount(spec, mount_point, flags,
369                                         extra_opts, fake, bg);
370         }
371
372         if (ret)
373                 return ret;
374
375         if (!fake)
376                 print_one(spec, mount_point, fs_type, mount_opts);
377
378         return add_mtab(spec, mount_point, fs_type, flags, *extra_opts);
379 }
380
381 int main(int argc, char *argv[])
382 {
383         int c, flags = 0, mnt_err = 1, fake = 0;
384         char *spec = NULL, *mount_point = NULL, *fs_type = "nfs";
385         char *extra_opts = NULL, *mount_opts = NULL;
386         uid_t uid = getuid();
387
388         progname = basename(argv[0]);
389
390         nfs_mount_data_version = discover_nfs_mount_data_version(&string);
391
392         if(!strncmp(progname, "umount", strlen("umount")))
393                 exit(nfsumount(argc, argv));
394
395         if ((argc < 3)) {
396                 mount_usage();
397                 exit(EX_USAGE);
398         }
399
400         mount_config_init(progname);
401
402         while ((c = getopt_long(argc, argv, "rvVwfno:hs",
403                                 longopts, NULL)) != -1) {
404                 switch (c) {
405                 case 'r':
406                         flags |= MS_RDONLY;
407                         break;
408                 case 'v':
409                         ++verbose;
410                         break;
411                 case 'V':
412                         printf("%s: ("PACKAGE_STRING")\n", progname);
413                         exit(EX_SUCCESS);
414                 case 'w':
415                         flags &= ~MS_RDONLY;
416                         break;
417                 case 'f':
418                         ++fake;
419                         break;
420                 case 'n':
421                         ++nomtab;
422                         break;
423                 case 'o':              /* specify mount options */
424                         if (mount_opts)
425                                 mount_opts = xstrconcat3(mount_opts, ",", optarg);
426                         else
427                                 mount_opts = xstrdup(optarg);
428                         break;
429                 case 's':
430                         ++sloppy;
431                         break;
432                 case 'h':
433                 default:
434                         mount_usage();
435                         goto out_usage;
436                 }
437         }
438
439         /*
440          * Extra non-option words at the end are bogus...
441          */
442         if (optind != argc - 2) {
443                 mount_usage();
444                 goto out_usage;
445         } else {
446                 while (optind < argc) {
447                         if (!spec)
448                                 spec = argv[optind];
449                         else
450                                 mount_point = argv[optind];
451                         optind++;
452                 }
453         }
454
455         if (strcmp(progname, "mount.nfs4") == 0)
456                 fs_type = "nfs4";
457
458         /*
459          * If a non-root user is attempting to mount, make sure the
460          * user's requested options match the options specified in
461          * /etc/fstab; otherwise, don't allow the mount.
462          */
463         if (uid != 0) {
464                 struct mntentchn *mc;
465
466                 if ((mc = getfsfile(mount_point)) == NULL ||
467                     strcmp(mc->m.mnt_fsname, spec) != 0 ||
468                     strcmp(mc->m.mnt_type, fs_type) != 0) {
469                         nfs_error(_("%s: permission denied: no match for %s "
470                                 "found in /etc/fstab"), progname, mount_point);
471                         goto out_usage;
472                 }
473
474                 /*
475                  * 'mount' munges the options from fstab before passing them
476                  * to us, so it is non-trivial to test that we have the correct
477                  * set of options and we don't want to trust what the user
478                  * gave us, so just take whatever is in /etc/fstab.
479                  */
480                 mount_opts = strdup(mc->m.mnt_opts);
481         }
482
483         mount_point = canonicalize(mount_point);
484         if (!mount_point) {
485                 nfs_error(_("%s: no mount point provided"), progname);
486                 goto out_usage;
487         }
488         if (mount_point[0] != '/') {
489                 nfs_error(_("%s: unrecognized mount point %s"),
490                         progname, mount_point);
491                 mnt_err = EX_USAGE;
492                 goto out;
493         }
494         /*
495          * Concatenate mount options from the configuration file
496          */
497         mount_opts = mount_config_opts(spec, mount_point, mount_opts);
498
499         parse_opts(mount_opts, &flags, &extra_opts);
500
501         if (uid != 0) {
502                 if (!(flags & (MS_USERS|MS_USER))) {
503                         nfs_error(_("%s: permission denied"), progname);
504                         mnt_err = EX_USAGE;
505                         goto out;
506                 }
507
508                 if (geteuid() != 0) {
509                         nfs_error(_("%s: not installed setuid - "
510                                     "\"user\" NFS mounts not supported."), progname);
511                         exit(EX_FAIL);
512                 }
513         }
514
515         if (chk_mountpoint(mount_point)) {
516                 mnt_err = EX_USAGE;
517                 goto out;
518         }
519
520         mnt_err = try_mount(spec, mount_point, flags, fs_type, &extra_opts,
521                                 mount_opts, fake, FOREGROUND);
522         if (mnt_err == EX_BG) {
523                 printf(_("%s: backgrounding \"%s\"\n"),
524                         progname, spec);
525                 printf(_("%s: mount options: \"%s\"\n"),
526                         progname, extra_opts);
527
528                 fflush(stdout);
529
530                 /*
531                  * Parent exits immediately with success.
532                  */
533                 if (daemon(0, 0)) {
534                         nfs_error(_("%s: failed to start "
535                                         "background process: %s\n"),
536                                                 progname, strerror(errno));
537                         exit(EX_FAIL);
538                 }
539
540                 mnt_err = try_mount(spec, mount_point, flags, fs_type,
541                                         &extra_opts, mount_opts, fake,
542                                         BACKGROUND);
543                 if (verbose && mnt_err)
544                         printf(_("%s: giving up \"%s\"\n"),
545                                 progname, spec);
546         }
547
548 out:
549         free(mount_opts);
550         free(extra_opts);
551         free(mount_point);
552         exit(mnt_err);
553
554 out_usage:
555         free(mount_opts);
556         exit(EX_USAGE);
557 }