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