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