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