]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/mount.c
umount.nfs: eliminate a nearly empty header file.
[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                 printf("%s on %s type %s", spec, node, type);
179
180                 if (opts != NULL)
181                         printf(" (%s)", opts);
182
183                 printf("\n");
184         }
185 }
186
187 /*
188  * Build a canonical mount option string for /etc/mtab.
189  */
190 static char *fix_opts_string(int flags, const char *extra_opts)
191 {
192         const struct opt_map *om;
193         char *new_opts;
194
195         new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");
196         if (flags & MS_USER) {
197                 /* record who mounted this so they can unmount */
198                 struct passwd *pw = getpwuid(getuid());
199                 if(pw)
200                         new_opts = xstrconcat3(new_opts, ",user=", pw->pw_name);
201         }
202         if (flags & MS_USERS)
203                 new_opts = xstrconcat3(new_opts, ",users", "");
204         
205         for (om = opt_map; om->opt != NULL; om++) {
206                 if (om->skip)
207                         continue;
208                 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
209                         continue;
210                 new_opts = xstrconcat3(new_opts, ",", om->opt);
211                 flags &= ~om->mask;
212         }
213         if (extra_opts && *extra_opts) {
214                 new_opts = xstrconcat3(new_opts, ",", extra_opts);
215         }
216         return new_opts;
217 }
218
219 static int add_mtab(char *spec, char *mount_point, char *fstype,
220                         int flags, char *opts, int freq, int pass)
221 {
222         struct mntent ment;
223         FILE *mtab;
224         int result = EX_FILEIO;
225
226         ment.mnt_fsname = spec;
227         ment.mnt_dir = mount_point;
228         ment.mnt_type = fstype;
229         ment.mnt_opts = fix_opts_string(flags, opts);
230         ment.mnt_freq = freq;
231         ment.mnt_passno = pass;
232
233         if (flags & MS_REMOUNT) {
234                 update_mtab(ment.mnt_dir, &ment);
235                 return 0;
236         }
237
238         lock_mtab();
239
240         if ((mtab = setmntent(MOUNTED, "a+")) == NULL) {
241                 unlock_mtab();
242                 nfs_error(_("Can't open mtab: %s"),
243                                 strerror(errno));
244                 goto fail_unlock;
245         }
246
247         if (addmntent(mtab, &ment) == 1) {
248                 nfs_error(_("Can't write mount entry to mtab: %s"),
249                                 strerror(errno));
250                 goto fail_close;
251         }
252
253         if (fchmod(fileno(mtab), 0644) == -1) {
254                 nfs_error(_("Can't set permissions on mtab: %s"),
255                                 strerror(errno));
256                 goto fail_close;
257         }
258
259         result = 0;
260
261 fail_close:
262         endmntent(mtab);
263 fail_unlock:
264         unlock_mtab();
265
266         return result;
267 }
268
269 void mount_usage(void)
270 {
271         printf("usage: %s remotetarget dir [-rvVwfnh] [-o nfsoptions]\n",
272                 progname);
273         printf("options:\n");
274         printf("\t-r\t\tMount file system readonly\n");
275         printf("\t-v\t\tVerbose\n");
276         printf("\t-V\t\tPrint version\n");
277         printf("\t-w\t\tMount file system read-write\n");
278         printf("\t-f\t\tFake mount, do not actually mount\n");
279         printf("\t-n\t\tDo not update /etc/mtab\n");
280         printf("\t-s\t\tTolerate sloppy mount options rather than failing.\n");
281         printf("\t-h\t\tPrint this help\n");
282         printf("\tnfsoptions\tRefer to mount.nfs(8) or nfs(5)\n\n");
283 }
284
285 static void parse_opt(const char *opt, int *mask, char *extra_opts, int len)
286 {
287         const struct opt_map *om;
288
289         for (om = opt_map; om->opt != NULL; om++) {
290                 if (!strcmp (opt, om->opt)) {
291                         if (om->inv)
292                                 *mask &= ~om->mask;
293                         else
294                                 *mask |= om->mask;
295                         return;
296                 }
297         }
298
299         len -= strlen(extra_opts);
300
301         if (*extra_opts && --len > 0)
302                 strcat(extra_opts, ",");
303
304         if ((len -= strlen(opt)) > 0)
305                 strcat(extra_opts, opt);
306 }
307
308 /*
309  * Convert the provided mount command-line options into the 4th &
310  * 5th arguments to mount(2).  Output parameter "@flags" gets the
311  * standard options (indicated by MS_ bits), and output parameter
312  * "@extra_opts" gets all the filesystem-specific options.
313  */
314 static void parse_opts(const char *options, int *flags, char **extra_opts)
315 {
316         if (options != NULL) {
317                 char *opts = xstrdup(options);
318                 char *opt, *p;
319                 int len = strlen(opts) + 1;     /* include room for a null */
320                 int open_quote = 0;
321
322                 *extra_opts = xmalloc(len);
323                 **extra_opts = '\0';
324
325                 for (p = opts, opt = NULL; p && *p; p++) {
326                         if (!opt)
327                                 opt = p;        /* begin of the option item */
328                         if (*p == '"')
329                                 open_quote ^= 1; /* reverse the status */
330                         if (open_quote)
331                                 continue;       /* still in a quoted block */
332                         if (*p == ',')
333                                 *p = '\0';      /* terminate the option item */
334
335                         /* end of option item or last item */
336                         if (*p == '\0' || *(p + 1) == '\0') {
337                                 parse_opt(opt, flags, *extra_opts, len);
338                                 opt = NULL;
339                         }
340                 }
341                 free(opts);
342         }
343 }
344
345 static int chk_mountpoint(char *mount_point)
346 {
347         struct stat sb;
348
349         if (stat(mount_point, &sb) < 0){
350                 mount_error(NULL, mount_point, errno);
351                 return 1;
352         }
353         if (S_ISDIR(sb.st_mode) == 0){
354                 mount_error(NULL, mount_point, ENOTDIR);
355                 return 1;
356         }
357         if (access(mount_point, X_OK) < 0) {
358                 mount_error(NULL, mount_point, errno);
359                 return 1;
360         }
361
362         return 0;
363 }
364
365 int main(int argc, char *argv[])
366 {
367         int c, flags = 0, mnt_err = 1, fake = 0;
368         char *spec, *mount_point, *fs_type = "nfs";
369         char *extra_opts = NULL, *mount_opts = NULL;
370         uid_t uid = getuid();
371
372         progname = basename(argv[0]);
373
374         discover_nfs_mount_data_version();
375
376         if(!strncmp(progname, "umount", strlen("umount")))
377                 exit(nfsumount(argc, argv));
378
379         if (argv[1] && argv[1][0] == '-') {
380                 if(argv[1][1] == 'V')
381                         printf("%s ("PACKAGE_STRING")\n", progname);
382                 else
383                         mount_usage();
384                 exit(0);
385         }
386
387         if ((argc < 3)) {
388                 mount_usage();
389                 exit(EX_USAGE);
390         }
391
392         spec = argv[1];
393         mount_point = argv[2];
394
395         argv[2] = argv[0]; /* so that getopt error messages are correct */
396         while ((c = getopt_long(argc - 2, argv + 2, "rvVwfno:hs",
397                                 longopts, NULL)) != -1) {
398                 switch (c) {
399                 case 'r':
400                         flags |= MS_RDONLY;
401                         break;
402                 case 'v':
403                         ++verbose;
404                         break;
405                 case 'V':
406                         printf("%s: ("PACKAGE_STRING")\n", progname);
407                         return 0;
408                 case 'w':
409                         flags &= ~MS_RDONLY;
410                         break;
411                 case 'f':
412                         ++fake;
413                         break;
414                 case 'n':
415                         ++nomtab;
416                         break;
417                 case 'o':              /* specify mount options */
418                         if (mount_opts)
419                                 mount_opts = xstrconcat3(mount_opts, ",", optarg);
420                         else
421                                 mount_opts = xstrdup(optarg);
422                         break;
423                 case 's':
424                         ++sloppy;
425                         break;
426                 case 'h':
427                 default:
428                         mount_usage();
429                         exit(EX_USAGE);
430                 }
431         }
432
433         /*
434          * Extra non-option words at the end are bogus...
435          */
436         if (optind != argc - 2) {
437                 mount_usage();
438                 exit(EX_USAGE);
439         }
440
441         if (strcmp(progname, "mount.nfs4") == 0)
442                 fs_type = "nfs4";
443
444         /*
445          * If a non-root user is attempting to mount, make sure the
446          * user's requested options match the options specified in
447          * /etc/fstab; otherwise, don't allow the mount.
448          */
449         if (uid != 0) {
450                 struct mntentchn *mc;
451
452                 if ((mc = getfsfile(mount_point)) == NULL ||
453                     strcmp(mc->m.mnt_fsname, spec) != 0 ||
454                     strcmp(mc->m.mnt_type, fs_type) != 0) {
455                         nfs_error(_("%s: permission denied: no match for %s "
456                                 "found in /etc/fstab"), progname, mount_point);
457                         exit(EX_USAGE);
458                 }
459
460                 /*
461                  * 'mount' munges the options from fstab before passing them
462                  * to us, so it is non-trivial to test that we have the correct
463                  * set of options and we don't want to trust what the user
464                  * gave us, so just take whatever is in /etc/fstab.
465                  */
466                 mount_opts = strdup(mc->m.mnt_opts);
467         }
468
469         mount_point = canonicalize(mount_point);
470         if (!mount_point) {
471                 nfs_error(_("%s: no mount point provided"), progname);
472                 exit(EX_USAGE);
473         }
474         if (mount_point[0] != '/') {
475                 nfs_error(_("%s: unrecognized mount point %s"),
476                         progname, mount_point);
477                 mnt_err = EX_USAGE;
478                 goto out;
479         }
480
481         parse_opts(mount_opts, &flags, &extra_opts);
482
483         if (uid != 0) {
484                 if (!(flags & (MS_USERS|MS_USER))) {
485                         nfs_error(_("%s: permission denied"), progname);
486                         mnt_err = EX_USAGE;
487                         goto out;
488                 }
489         }
490
491         if (chk_mountpoint(mount_point)) {
492                 mnt_err = EX_USAGE;
493                 goto out;
494         }
495
496         if (strcmp(fs_type, "nfs4") == 0)
497                 mnt_err = nfs4mount(spec, mount_point, flags, &extra_opts, fake);
498         else
499                 mnt_err = nfsmount(spec, mount_point, flags, &extra_opts, fake);
500
501         if (mnt_err)
502                 exit(EX_FAIL);
503
504         if (!fake)
505                 print_one(spec, mount_point, fs_type, mount_opts);
506
507         if (!nomtab)
508                 mnt_err = add_mtab(spec, mount_point, fs_type, flags, extra_opts,
509                                 0, 0 /* these are always zero for NFS */ );
510
511 out:
512         free(mount_point);
513         exit(mnt_err);
514 }