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