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