]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/mount.c
mount.nfs - NFSv4 mounts give wrong error message when server denies the mount
[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 },       /* Allow ordinary user to mount */
103   { "nousers",  0, 1, MS_DUMMY  },      /* Forbid ordinary user to mount */
104   { "user",     1, 0, MS_USER  },       /* Allow ordinary user to mount */
105   { "nouser",   0, 1, MS_DUMMY   },     /* Forbid ordinary user to mount */
106   { "owner",    0, 0, MS_DUMMY  },      /* Let the owner of the device mount */
107   { "noowner",  0, 0, MS_DUMMY  },      /* Device owner has no special privs */
108   { "group",    0, 0, MS_DUMMY  },      /* Let the group of the device mount */
109   { "nogroup",  0, 0, MS_DUMMY  },      /* Device group has no special privs */
110   { "_netdev",  0, 0, MS_DUMMY},        /* Device requires network */
111   { "comment",  0, 0, MS_DUMMY},        /* fstab comment only (kudzu,_netdev)*/
112
113   /* add new options here */
114 #ifdef MS_NOSUB
115   { "sub",      0, 1, MS_NOSUB  },      /* allow submounts */
116   { "nosub",    0, 0, MS_NOSUB  },      /* don't allow submounts */
117 #endif
118 #ifdef MS_SILENT
119   { "quiet",    0, 0, MS_SILENT    },   /* be quiet  */
120   { "loud",     0, 1, MS_SILENT    },   /* print out messages. */
121 #endif
122 #ifdef MS_MANDLOCK
123   { "mand",     0, 0, MS_MANDLOCK },    /* Allow mandatory locks on this FS */
124   { "nomand",   0, 1, MS_MANDLOCK },    /* Forbid mandatory locks on this FS */
125 #endif
126   { "loop",     1, 0, MS_DUMMY   },      /* use a loop device */
127 #ifdef MS_NOATIME
128   { "atime",    0, 1, MS_NOATIME },     /* Update access time */
129   { "noatime",  0, 0, MS_NOATIME },     /* Do not update access time */
130 #endif
131 #ifdef MS_NODIRATIME
132   { "diratime", 0, 1, MS_NODIRATIME },  /* Update dir access times */
133   { "nodiratime", 0, 0, MS_NODIRATIME },/* Do not update dir access times */
134 #endif
135   { NULL,       0, 0, 0         }
136 };
137
138 /* Try to build a canonical options string.  */
139 static char * fix_opts_string (int flags, const char *extra_opts) {
140         const struct opt_map *om;
141         char *new_opts;
142
143         new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");
144         if (flags & MS_USER) {
145                 /* record who mounted this so they can unmount */
146                 struct passwd *pw = getpwuid(getuid());
147                 if(pw)
148                         new_opts = xstrconcat3(new_opts, ",user=", pw->pw_name);
149         }
150         if (flags & MS_USERS)
151                 new_opts = xstrconcat3(new_opts, ",users", "");
152         
153         for (om = opt_map; om->opt != NULL; om++) {
154                 if (om->skip)
155                         continue;
156                 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
157                         continue;
158                 new_opts = xstrconcat3(new_opts, ",", om->opt);
159                 flags &= ~om->mask;
160         }
161         if (extra_opts && *extra_opts) {
162                 new_opts = xstrconcat3(new_opts, ",", extra_opts);
163         }
164         return new_opts;
165 }
166
167
168 int add_mtab(char *fsname, char *mount_point, char *fstype, int flags, char *opts, int freq, int passno)
169 {
170         struct mntent ment;
171         FILE *mtab;
172
173         ment.mnt_fsname = fsname;
174         ment.mnt_dir = mount_point;
175         ment.mnt_type = fstype;
176         ment.mnt_opts = fix_opts_string(flags, opts);
177         ment.mnt_freq = 0;
178         ment.mnt_passno= 0;
179
180         if(flags & MS_REMOUNT) {
181                 update_mtab(ment.mnt_dir, &ment);
182                 return 0;
183         }
184
185         lock_mtab();
186
187         if ((mtab = setmntent(MOUNTED, "a+")) == NULL) {
188                 fprintf(stderr, "Can't open " MOUNTED);
189                 return 1;
190         }
191
192         if (addmntent(mtab, &ment) == 1) {
193                 endmntent(mtab);
194                 unlock_mtab();
195                 fprintf(stderr, "Can't write mount entry");
196                 return 1;
197         }
198
199         if (fchmod(fileno(mtab), 0644) == -1) {
200                 endmntent(mtab);
201                 unlock_mtab();
202                 fprintf(stderr, "Can't set perms on " MOUNTED);
203                 return 1;
204         }
205
206         endmntent(mtab);
207
208         unlock_mtab();
209
210         return 0;
211 }
212
213 int do_mount_syscall(char *spec, char *node, char *type, int flags, void *data)
214 {
215         return mount(spec, node, type, flags, data);
216 }
217
218 void mount_usage()
219 {
220         printf("usage: %s remotetarget dir [-rvVwfnh] [-t version] [-o nfsoptions]\n", progname);
221         printf("options:\n\t-r\t\tMount file system readonly\n");
222         printf("\t-v\t\tVerbose\n");
223         printf("\t-V\t\tPrint version\n");
224         printf("\t-w\t\tMount file system read-write\n");
225         printf("\t-f\t\tFake mount, don't actually mount\n");
226         printf("\t-n\t\tDo not update /etc/mtab\n");
227         printf("\t-s\t\tTolerate sloppy mount options rather than failing.\n");
228         printf("\t-h\t\tPrint this help\n");
229         printf("\tversion\t\tnfs4 - NFS version 4, nfs - older NFS version supported\n");
230         printf("\tnfsoptions\tRefer mount.nfs(8) or nfs(5)\n\n");
231 }
232
233 static inline void
234 parse_opt(const char *opt, int *mask, char *extra_opts, int len) {
235         const struct opt_map *om;
236
237         for (om = opt_map; om->opt != NULL; om++) {
238                 if (!strcmp (opt, om->opt)) {
239                         if (om->inv)
240                                 *mask &= ~om->mask;
241                         else
242                                 *mask |= om->mask;
243                         return;
244                 }
245         }
246
247         len -= strlen(extra_opts);
248
249         if (*extra_opts && --len > 0)
250                 strcat(extra_opts, ",");
251
252         if ((len -= strlen(opt)) > 0)
253                 strcat(extra_opts, opt);
254 }
255
256 /* Take -o options list and compute 4th and 5th args to mount(2).  flags
257    gets the standard options (indicated by bits) and extra_opts all the rest */
258 static void parse_opts (const char *options, int *flags, char **extra_opts)
259 {
260         if (options != NULL) {
261                 char *opts = xstrdup(options);
262                 char *opt, *p;
263                 int len = strlen(opts) + 1;             /* include room for a null */
264                 int open_quote = 0;
265
266                 *extra_opts = xmalloc(len);
267                 **extra_opts = '\0';
268
269                 for (p=opts, opt=NULL; p && *p; p++) {
270                         if (!opt)
271                                 opt = p;                /* begin of the option item */
272                         if (*p == '"')
273                                 open_quote ^= 1;        /* reverse the status */
274                         if (open_quote)
275                                 continue;               /* still in a quoted block */
276                         if (*p == ',')
277                                 *p = '\0';              /* terminate the option item */
278                         /* end of option item or last item */
279                         if (*p == '\0' || *(p+1) == '\0') {
280                                 parse_opt(opt, flags, *extra_opts, len);
281                                 opt = NULL;
282                         }
283                 }
284                 free(opts);
285         }
286 }
287
288 static void mount_error(char *mntpnt, char *node)
289 {
290         switch(errno) {
291                 case ENOTDIR:
292                         fprintf(stderr, "%s: mount point %s is not a directory\n", 
293                                 progname, mntpnt);
294                         break;
295                 case EBUSY:
296                         fprintf(stderr, "%s: %s is already mounted or busy\n", 
297                                 progname, mntpnt);
298                         break;
299                 case ENOENT:
300                         if (node) {
301                                 fprintf(stderr, "%s: %s failed, reason given by server: %s\n",
302                                         progname, node, strerror(errno));
303                         } else
304                                 fprintf(stderr, "%s: mount point %s does not exist\n", 
305                                         progname, mntpnt);
306                         break;
307                 default:
308                         fprintf(stderr, "%s: %s\n", progname, strerror(errno));
309         }
310 }
311 static int chk_mountpoint(mount_point)
312 {
313         struct stat sb;
314
315         if (stat(mount_point, &sb) < 0){
316                 mount_error(mount_point, NULL);
317                 return 1;
318         }
319         if (S_ISDIR(sb.st_mode) == 0){
320                 errno = ENOTDIR;
321                 mount_error(mount_point, NULL);
322                 return 1;
323         }
324         if (access(mount_point, X_OK) < 0) {
325                 mount_error(mount_point, NULL);
326                 return 1;
327         }
328
329         return 0;
330 }
331
332 extern u_short getport(
333         struct sockaddr_in *saddr,
334         u_long prog,
335         u_long vers,
336         u_int prot);
337
338 static int probe_statd()
339 {
340         struct sockaddr_in addr;
341         u_short port;
342
343         memset(&addr, 0, sizeof(addr));
344         addr.sin_family = AF_INET;
345         addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
346         port = getport(&addr, 100024, 1, IPPROTO_UDP);
347
348         if (port == 0)
349                 return 0;
350         addr.sin_port = htons(port);
351
352         if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
353                 return 0;
354
355         return 1;
356 }
357
358 static int start_statd()
359 {
360         /* If /var/run/rpc.statd.pid exists and is non-empty,
361          * assume statd already running.
362          * If START_STATD not defined, or defined to a non-existent file,
363          * don't bother,
364          * else run that file (typically a shell script)
365          */
366         struct stat stb;
367
368         if (probe_statd())
369                 return 1;
370 #ifdef START_STATD
371         if (stat(START_STATD, &stb) ==0 &&
372             S_ISREG(stb.st_mode) &&
373             (stb.st_mode & S_IXUSR)) {
374                 system(START_STATD);
375                 if (probe_statd())
376                         return 1;
377         }
378 #endif
379         return 0;
380 }
381
382 int main(int argc, char *argv[])
383 {
384         int c, flags = 0, nfs_mount_vers = 0, mnt_err = 1, fake = 0;
385         char *spec, *mount_point, *extra_opts = NULL;
386         char *mount_opts = NULL, *p;
387         uid_t uid = getuid();
388
389         progname = argv[0];
390         if (!progname)
391                 exit(2);
392
393         if ((p = strrchr(progname, '/')) != NULL)
394                 progname = p+1;
395
396         if(!strncmp(progname, "umount", strlen("umount"))) {
397                 if(argc < 2) {
398                         umount_usage();
399                         exit(1);
400                 }
401                 exit(nfsumount(argc, argv) ? 0 : 1);
402         }
403
404         if(argv[1] && argv[1][0] == '-') {
405                 if(argv[1][1] == 'V')
406                         printf("%s ("PACKAGE_STRING")\n", progname);
407                 else
408                         mount_usage();
409                 return 0;
410         }
411
412         if ((argc < 3)) {
413                 mount_usage();
414                 exit(1);
415         }
416
417         spec = argv[1];
418         mount_point = argv[2];
419
420         argv[2] = argv[0]; /* so that getopt error messages are correct */
421         while ((c = getopt_long (argc - 2, argv + 2, "rt:vVwfno:hs",
422                                 longopts, NULL)) != -1) {
423                 switch (c) {
424                 case 'r':
425                         flags |= MS_RDONLY;
426                         break;
427                 case 't':
428                         if (strcmp(optarg, "nfs4") == 0)
429                                 nfs_mount_vers = 4;
430                         else if (strcmp(optarg, "nfs") == 0)
431                                 nfs_mount_vers = 0;
432                         else {
433                                 fprintf(stderr, "%s: unknown filesystem type: %s\n",
434                                         progname, optarg);
435                                 exit(1);
436                         }
437                         break;
438                 case 'v':
439                         ++verbose;
440                         break;
441                 case 'V':
442                         printf("%s: ("PACKAGE_STRING")\n", progname);
443                         return 0;
444                 case 'w':
445                         flags &= ~MS_RDONLY;
446                         break;
447                 case 'f':
448                         ++fake;
449                         break;
450                 case 'n':
451                         ++nomtab;
452                         break;
453                 case 'o':              /* specify mount options */
454                         if (mount_opts)
455                                 mount_opts = xstrconcat3(mount_opts, ",", optarg);
456                         else
457                                 mount_opts = xstrdup(optarg);
458                         break;
459                 case 's':
460                         ++sloppy;
461                         break;
462                 case 128: /* bind */
463                         mounttype = MS_BIND;
464                         break;
465                 case 129: /* replace */
466                         mounttype = MS_REPLACE;
467                         break;
468                 case 130: /* after */
469                         mounttype = MS_AFTER;
470                         break;
471                 case 131: /* before */
472                         mounttype = MS_BEFORE;
473                         break;
474                 case 132: /* over */
475                         mounttype = MS_OVER;
476                         break;
477                 case 133: /* move */
478                         mounttype = MS_MOVE;
479                         break;
480                 case 135: /* rbind */
481                         mounttype = MS_BIND | MS_REC;
482                         break;
483                 case 'h':
484                 default:
485                         mount_usage();
486                         exit(1);
487                 }
488         }
489         if (optind != argc-2) {
490                 /* Extra non-option words at the end... */
491                 mount_usage();
492                 exit(1);
493         }
494
495         if (strcmp(progname, "mount.nfs4") == 0)
496                 nfs_mount_vers = 4;
497
498         if (uid != 0) {
499                 /* don't even think about it unless options exactly
500                  * match fstab
501                  */
502                 struct mntentchn *mc;
503
504                 if ((mc = getfsfile(mount_point)) == NULL ||
505                     strcmp(mc->m.mnt_fsname, spec) != 0 ||
506                     strcmp(mc->m.mnt_type, (nfs_mount_vers == 4 ? "nfs4":"nfs")) != 0
507                     ) {
508                         fprintf(stderr, "%s: permission died - no match for fstab\n",
509                                 progname);
510                         exit(1);
511                 }
512                 /* 'mount' munges the options from fstab before passing them
513                  * to us, so it is non-trivial to test that we have the correct
514                  * set of options and we don't want to trust what the user
515                  * gave us, so just take whatever is in fstab
516                  */
517                 mount_opts = strdup(mc->m.mnt_opts);
518                 mounttype = 0;
519         }
520
521         mount_point = canonicalize(mount_point);
522         if (mount_point == NULL ||
523             mount_point[0] != '/') {
524                 fprintf(stderr, "%s: unknown mount point %s\n",
525                         progname, mount_point ? : "");
526                 exit(1);
527         }
528         
529         parse_opts(mount_opts, &flags, &extra_opts);
530
531         if (uid != 0) {
532             if (! (flags & (MS_USERS | MS_USER))) {
533                     fprintf(stderr, "%s: permission denied\n", progname);
534                     exit(1);
535             }
536         }
537
538         if (chk_mountpoint(mount_point))
539                 exit(EX_FAIL);
540
541         if (nfs_mount_vers == 4)
542                 mnt_err = nfs4mount(spec, mount_point, &flags, &extra_opts, &mount_opts, 0);
543         else {
544                 int need_statd = 0;
545                 mnt_err = nfsmount(spec, mount_point, &flags,
546                                    &extra_opts, &mount_opts,
547                                    0, &need_statd);
548                 if (!mnt_err && !fake && need_statd) {
549                         if (!start_statd()) {
550                                 fprintf(stderr,
551                                         "%s: rpc.statd is not running but is "
552                                         "required for remote locking\n"
553                                         "   Either use \"-o nolocks\" to keep "
554                                         "locks local, or start statd.\n",
555                                         progname);
556                                 exit(1);
557                         }
558                 }
559         }
560
561         if (mnt_err)
562                 exit(EX_FAIL);
563
564         if (!fake) {
565                 mnt_err = do_mount_syscall(spec, mount_point,
566                                            nfs_mount_vers == 4 ? "nfs4" : "nfs",
567                                            flags & ~(MS_USER|MS_USERS) ,
568                                            mount_opts);
569
570                 if (mnt_err) {
571                         mount_error(mount_point, spec);
572                         exit(EX_FAIL);
573                 }
574         }
575
576         if (!nomtab)
577                 add_mtab(spec, mount_point,
578                          nfs_mount_vers == 4 ? "nfs4" : "nfs",
579                          flags, extra_opts, 0, 0);
580
581         return 0;
582 }
583