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