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