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