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