]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/mount.c
a7ad035237b464fd269a2f0b1cf6fb3de968b797
[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 int main(int argc, char *argv[])
365 {
366         int c, flags = 0, mnt_err = 1, fake = 0;
367         char *spec, *mount_point, *fs_type = "nfs";
368         char *extra_opts = NULL, *mount_opts = NULL;
369         uid_t uid = getuid();
370
371         progname = basename(argv[0]);
372
373         discover_nfs_mount_data_version();
374
375         if(!strncmp(progname, "umount", strlen("umount")))
376                 exit(nfsumount(argc, argv));
377
378         if (argv[1] && argv[1][0] == '-') {
379                 if(argv[1][1] == 'V')
380                         printf("%s ("PACKAGE_STRING")\n", progname);
381                 else
382                         mount_usage();
383                 exit(0);
384         }
385
386         if ((argc < 3)) {
387                 mount_usage();
388                 exit(EX_USAGE);
389         }
390
391         spec = argv[1];
392         mount_point = argv[2];
393
394         argv[2] = argv[0]; /* so that getopt error messages are correct */
395         while ((c = getopt_long(argc - 2, argv + 2, "rvVwfno:hs",
396                                 longopts, NULL)) != -1) {
397                 switch (c) {
398                 case 'r':
399                         flags |= MS_RDONLY;
400                         break;
401                 case 'v':
402                         ++verbose;
403                         break;
404                 case 'V':
405                         printf("%s: ("PACKAGE_STRING")\n", progname);
406                         return 0;
407                 case 'w':
408                         flags &= ~MS_RDONLY;
409                         break;
410                 case 'f':
411                         ++fake;
412                         break;
413                 case 'n':
414                         ++nomtab;
415                         break;
416                 case 'o':              /* specify mount options */
417                         if (mount_opts)
418                                 mount_opts = xstrconcat3(mount_opts, ",", optarg);
419                         else
420                                 mount_opts = xstrdup(optarg);
421                         break;
422                 case 's':
423                         ++sloppy;
424                         break;
425                 case 'h':
426                 default:
427                         mount_usage();
428                         exit(EX_USAGE);
429                 }
430         }
431
432         /*
433          * Extra non-option words at the end are bogus...
434          */
435         if (optind != argc - 2) {
436                 mount_usage();
437                 exit(EX_USAGE);
438         }
439
440         if (strcmp(progname, "mount.nfs4") == 0)
441                 fs_type = "nfs4";
442
443         /*
444          * If a non-root user is attempting to mount, make sure the
445          * user's requested options match the options specified in
446          * /etc/fstab; otherwise, don't allow the mount.
447          */
448         if (uid != 0) {
449                 struct mntentchn *mc;
450
451                 if ((mc = getfsfile(mount_point)) == NULL ||
452                     strcmp(mc->m.mnt_fsname, spec) != 0 ||
453                     strcmp(mc->m.mnt_type, fs_type) != 0) {
454                         nfs_error(_("%s: permission denied: no match for %s "
455                                 "found in /etc/fstab"), progname, mount_point);
456                         exit(EX_USAGE);
457                 }
458
459                 /*
460                  * 'mount' munges the options from fstab before passing them
461                  * to us, so it is non-trivial to test that we have the correct
462                  * set of options and we don't want to trust what the user
463                  * gave us, so just take whatever is in /etc/fstab.
464                  */
465                 mount_opts = strdup(mc->m.mnt_opts);
466         }
467
468         mount_point = canonicalize(mount_point);
469         if (!mount_point) {
470                 nfs_error(_("%s: no mount point provided"), progname);
471                 exit(EX_USAGE);
472         }
473         if (mount_point[0] != '/') {
474                 nfs_error(_("%s: unrecognized mount point %s"),
475                         progname, mount_point);
476                 mnt_err = EX_USAGE;
477                 goto out;
478         }
479
480         parse_opts(mount_opts, &flags, &extra_opts);
481
482         if (uid != 0) {
483                 if (!(flags & (MS_USERS|MS_USER))) {
484                         nfs_error(_("%s: permission denied"), progname);
485                         mnt_err = EX_USAGE;
486                         goto out;
487                 }
488         }
489
490         if (chk_mountpoint(mount_point)) {
491                 mnt_err = EX_USAGE;
492                 goto out;
493         }
494
495         if (strcmp(fs_type, "nfs4") == 0)
496                 mnt_err = nfs4mount(spec, mount_point, flags, &extra_opts, fake);
497         else
498                 mnt_err = nfsmount(spec, mount_point, flags, &extra_opts, fake);
499
500         if (mnt_err)
501                 exit(EX_FAIL);
502
503         if (!fake)
504                 print_one(spec, mount_point, fs_type, mount_opts);
505
506         if (!nomtab)
507                 mnt_err = add_mtab(spec, mount_point, fs_type, flags, extra_opts,
508                                 0, 0 /* these are always zero for NFS */ );
509
510 out:
511         free(mount_point);
512         exit(mnt_err);
513 }