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