]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/mount.c
Imported Debian patch 1.0.9-1
[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
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   { "nfsvers", 1, 0, 't' },
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 /* Maybe these should now be freed for kernel use again */
80 #define MS_DUMMY        0x00000000
81 #define MS_USERS        0x40000000
82 #define MS_USER         0x20000000
83 #define MS_OWNER        0x10000000
84 #define MS_GROUP        0x08000000
85
86 static const struct opt_map opt_map[] = {
87   { "defaults", 0, 0, 0         },      /* default options */
88   { "ro",       1, 0, MS_RDONLY },      /* read-only */
89   { "rw",       1, 1, MS_RDONLY },      /* read-write */
90   { "exec",     0, 1, MS_NOEXEC },      /* permit execution of binaries */
91   { "noexec",   0, 0, MS_NOEXEC },      /* don't execute binaries */
92   { "suid",     0, 1, MS_NOSUID },      /* honor suid executables */
93   { "nosuid",   0, 0, MS_NOSUID },      /* don't honor suid executables */
94   { "dev",      0, 1, MS_NODEV  },      /* interpret device files  */
95   { "nodev",    0, 0, MS_NODEV  },      /* don't interpret devices */
96   { "sync",     0, 0, MS_SYNCHRONOUS},  /* synchronous I/O */
97   { "async",    0, 1, MS_SYNCHRONOUS},  /* asynchronous I/O */
98   { "dirsync",  0, 0, MS_DIRSYNC},      /* synchronous directory modifications */
99   { "remount",  0, 0, MS_REMOUNT},      /* Alter flags of mounted FS */
100   { "bind",     0, 0, MS_BIND   },      /* Remount part of tree elsewhere */
101   { "rbind",    0, 0, MS_BIND|MS_REC }, /* Idem, plus mounted subtrees */
102   { "auto",     0, 0, MS_DUMMY },      /* Can be mounted using -a */
103   { "noauto",   0, 0, MS_DUMMY },      /* Can  only be mounted explicitly */
104   { "users",    0, 0, MS_USERS  },      /* Allow ordinary user to mount */
105   { "nousers",  0, 0, MS_USERS  },      /* Forbid ordinary user to mount */
106   { "user",     0, 0, MS_USER   },      /* Allow ordinary user to mount */
107   { "nouser",   0, 0, MS_USER   },      /* Forbid ordinary user to mount */
108   { "owner",    0, 0, MS_OWNER  },      /* Let the owner of the device mount */
109   { "noowner",  0, 0, MS_OWNER  },      /* Device owner has no special privs */
110   { "group",    0, 0, MS_GROUP  },      /* Let the group of the device mount */
111   { "nogroup",  0, 0, MS_GROUP  },      /* 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                 struct passwd *pw = getpwuid(getuid());
148                 if(pw)
149                         new_opts = xstrconcat3(new_opts, ",user=", pw->pw_name);
150         }
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
164         return new_opts;
165 }
166
167 void copy_mntent(struct mntent *ment, nfs_mntent_t *nment)
168 {
169         /* Not sure why nfs_mntent_t should exist */
170         strcpy(nment->mnt_fsname, ment->mnt_fsname);
171         strcpy(nment->mnt_dir, ment->mnt_dir);
172         strcpy(nment->mnt_type, ment->mnt_type);
173         strcpy(nment->mnt_opts, ment->mnt_opts);
174         nment->mnt_freq = ment->mnt_freq;
175         nment->mnt_passno = ment->mnt_passno;
176 }
177
178 int add_mtab(char *fsname, char *mount_point, char *fstype, int flags, char *opts, int freq, int passno)
179 {
180         struct mntent ment;
181         int fd;
182         FILE *mtab;
183
184         ment.mnt_fsname = fsname;
185         ment.mnt_dir = mount_point;
186         ment.mnt_type = fstype;
187         ment.mnt_opts = fix_opts_string(flags, opts);
188         ment.mnt_freq = freq;
189         ment.mnt_passno= passno;
190
191         if(flags & MS_REMOUNT) {
192                 nfs_mntent_t nment;
193                 
194                 copy_mntent(&ment, &nment);
195                 update_mtab(nment.mnt_dir, &nment);
196                 return 0;
197         }
198
199         if ((fd = open(MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) == -1) {
200                 fprintf(stderr, "Can't get "MOUNTED"~ lock file");
201                 return 1;
202         }
203         close(fd);
204
205         if ((mtab = setmntent(MOUNTED, "a+")) == NULL) {
206                 fprintf(stderr, "Can't open " MOUNTED);
207                 return 1;
208         }
209
210         if (addmntent(mtab, &ment) == 1) {
211                 fprintf(stderr, "Can't write mount entry");
212                 return 1;
213         }
214
215         if (fchmod(fileno(mtab), 0644) == -1) {
216                 fprintf(stderr, "Can't set perms on " MOUNTED);
217                 return 1;
218         }
219
220         endmntent(mtab);
221
222         if (unlink(MOUNTED"~") == -1) {
223                 fprintf(stderr, "Can't remove "MOUNTED"~");
224                 return 1;
225         }
226
227         return 0;
228 }
229
230 int do_mount_syscall(char *spec, char *node, char *type, int flags, void *data)
231 {
232         return mount(spec, node, type, flags, data);
233 }
234
235 void mount_usage()
236 {
237         printf("usage: %s remotetarget dir [-rvVwfnh] [-t version] [-o nfsoptions]\n", progname);
238         printf("options:\n\t-r\t\tMount file system readonly\n");
239         printf("\t-v\t\tVerbose\n");
240         printf("\t-V\t\tPrint version\n");
241         printf("\t-w\t\tMount file system read-write\n");
242         printf("\t-f\t\tFake mount, don't actually mount\n");
243         printf("\t-n\t\tDo not update /etc/mtab\n");
244         printf("\t-h\t\tPrint this help\n");
245         printf("\tversion\t\tnfs4 - NFS version 4, nfs - older NFS version supported\n");
246         printf("\tnfsoptions\tRefer mount.nfs(8) or nfs(5)\n\n");
247 }
248
249 static inline void
250 parse_opt(const char *opt, int *mask, char *extra_opts, int len) {
251         const struct opt_map *om;
252
253         for (om = opt_map; om->opt != NULL; om++) {
254                 if (!strcmp (opt, om->opt)) {
255                         if (om->inv)
256                                 *mask &= ~om->mask;
257                         else
258                                 *mask |= om->mask;
259                         return;
260                 }
261         }
262
263         len -= strlen(extra_opts);
264
265         if (*extra_opts && --len > 0)
266                 strcat(extra_opts, ",");
267
268         if ((len -= strlen(opt)) > 0)
269                 strcat(extra_opts, opt);
270 }
271
272 /* Take -o options list and compute 4th and 5th args to mount(2).  flags
273    gets the standard options (indicated by bits) and extra_opts all the rest */
274 static void parse_opts (const char *options, int *flags, char **extra_opts)
275 {
276         if (options != NULL) {
277                 char *opts = xstrdup(options);
278                 char *opt;
279                 int len = strlen(opts) + 20;
280
281                 *extra_opts = xmalloc(len);
282                 **extra_opts = '\0';
283
284                 for (opt = strtok(opts, ","); opt; opt = strtok(NULL, ","))
285                         parse_opt(opt, flags, *extra_opts, len);
286
287                 free(opts);
288         }
289
290 }
291
292 static void mount_error(char *node)
293 {
294         switch(errno) {
295                 case ENOTDIR:
296                         fprintf(stderr, "%s: mount point %s is not a directory\n", progname, node);
297                         break;
298                 case EBUSY:
299                         fprintf(stderr, "%s: %s is already mounted or busy\n", progname, node);
300                         break;
301                 case ENOENT:
302                         fprintf(stderr, "%s: mount point %s does not exist\n", progname, node);
303                         break;
304                 default:
305                         fprintf(stderr, "%s: %s\n", progname, strerror(errno));
306         }
307 }
308
309 int main(int argc, char *argv[])
310 {
311         int c, flags = 0, nfs_mount_vers = 0, mnt_err = 1, fake = 0;
312         char *spec, *mount_point, *extra_opts = NULL;
313         char *mount_opts = NULL, *p;
314
315         progname = argv[0];
316         if ((p = strrchr(progname, '/')) != NULL)
317                 progname = p+1;
318
319         if(!strncmp(progname, "umount", strlen("umount"))) {
320                 if(argc < 2) {
321                         umount_usage();
322                         exit(1);
323                 }
324                 return(nfsumount(argc, argv));
325         }
326
327         if ((argc < 2)) {
328                 mount_usage();
329                 exit(1);
330         }
331
332         if(argv[1][0] == '-') {
333                 if(argv[1][1] == 'V')
334                         printf("%s ("PACKAGE_STRING")\n", progname);
335                 else
336                         mount_usage();
337                 return 0;
338         }
339
340         while ((c = getopt_long (argc - 2, argv + 2, "rt:vVwfno:h",
341                                 longopts, NULL)) != -1) {
342                 switch (c) {
343                 case 'r':
344                         flags |= MS_RDONLY;
345                         break;
346                 case 't':
347                         nfs_mount_vers = (strncmp(optarg, "nfs4", 4)) ? 0 : 4;
348                         break;
349                 case 'v':
350                         ++verbose;
351                         break;
352                 case 'V':
353                         printf("%s: ("PACKAGE_STRING")\n", progname);
354                         return 0;
355                 case 'w':
356                         flags &= ~MS_RDONLY;
357                         break;
358                 case 'f':
359                         ++fake;
360                         break;
361                 case 'n':
362                         ++nomtab;
363                         break;
364                 case 'o':              /* specify mount options */
365                         if (mount_opts)
366                                 mount_opts = xstrconcat3(mount_opts, ",", optarg);
367                         else
368                                 mount_opts = xstrdup(optarg);
369                         break;
370                 case 128: /* bind */
371                         mounttype = MS_BIND;
372                         break;
373                 case 129: /* replace */
374                         mounttype = MS_REPLACE;
375                         break;
376                 case 130: /* after */
377                         mounttype = MS_AFTER;
378                         break;
379                 case 131: /* before */
380                         mounttype = MS_BEFORE;
381                         break;
382                 case 132: /* over */
383                         mounttype = MS_OVER;
384                         break;
385                 case 133: /* move */
386                         mounttype = MS_MOVE;
387                         break;
388                 case 135: /* rbind */
389                         mounttype = MS_BIND | MS_REC;
390                         break;
391                 case 'h':
392                 default:
393                         mount_usage();
394                         exit(1);
395                 }
396         }
397
398         spec = argv[1];
399         mount_point = canonicalize(argv[2]);
400         
401         parse_opts(mount_opts, &flags, &extra_opts);
402
403         if (getuid() != 0 && !(flags & MS_USERS) && !(flags & MS_USER)) {
404                 fprintf(stderr, "%s: permission denied.\n", progname);
405                 exit(1);
406         }
407
408         if (!strcmp(progname, "mount.nfs4") || nfs_mount_vers == 4) {
409                 nfs_mount_vers = 4;
410                 mnt_err = nfs4mount(spec, mount_point, &flags, &extra_opts, &mount_opts, 0);
411         }
412         else {
413                 if (!strcmp(progname, "mount.nfs")) {
414                         mnt_err = nfsmount(spec, mount_point, &flags,
415                                         &extra_opts, &mount_opts, &nfs_mount_vers, 0);
416                 }
417         }
418
419         if (!mnt_err && !fake) {
420                 if(!(flags & MS_REMOUNT)) {
421                         mnt_err = do_mount_syscall(spec, mount_point,
422                                         nfs_mount_vers == 4 ? "nfs4" : "nfs", flags, mount_opts);
423                 
424                         if(mnt_err) {
425                                 mount_error(mount_point);
426                                 exit(-1);
427                         }
428                 }
429                 if(!nomtab) {
430                         add_mtab(spec, mount_point, nfs_mount_vers == 4 ? "nfs4" : "nfs",
431                                  flags, extra_opts, 0, 0);
432                 }
433         }
434
435         return 0;
436 }
437