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