merge with 0.30.213
[util-vserver.git] / src / secure-mount.c
1 // $Id: secure-mount.c 2480 2007-01-28 11:35:19Z dhozac $    --*- c++ -*--
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19   // secure-mount <general mount(8) options> [--chroot]
20   //              [--mtab <mtabfile>] [--fstab <fstabfile>]
21   //
22   // Executes mount-operations under the current directory: it assumes sources
23   // in the current root-dir while destinations are expected in the chroot
24   // environment.
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include "util.h"
32 #include "pathconfig.h"
33
34 #include <lib/internal.h>
35
36 #include <getopt.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <strings.h>
43 #include <unistd.h>
44 #include <stdbool.h>
45 #include <sys/mount.h>
46 #include <sys/stat.h>
47 #include <sys/types.h>
48 #include <sys/file.h>
49 #include <linux/fs.h>
50 #include <assert.h>
51 #include <ctype.h>
52 #include <sys/wait.h>
53 #include <libgen.h>
54 #include <signal.h>
55 #include <stdlib.h>
56
57 #define ENSC_WRAPPERS_FCNTL     1
58 #define ENSC_WRAPPERS_UNISTD    1
59 #include <wrappers.h>
60
61 #define MNTPOINT        "/etc"
62
63 typedef enum { rfsYES, rfsNO, rfsONLY }         RootFsOption;
64
65 struct MountInfo {
66     char const *        src;
67     char const *        dst;
68     char const *        type;
69     unsigned long       flag;
70     unsigned long       xflag;
71     unsigned long       mask;
72     char *              data;
73     char *              data_parsed;
74 };
75
76 struct Options {
77     char const *        mtab;
78     char const *        fstab;
79     bool                do_chroot;
80     bool                ignore_mtab;
81     bool                mount_all;
82     RootFsOption        rootfs;
83
84     int                 cur_dir_fd;
85     int                 cur_rootdir_fd;
86 };
87
88 #define OPTION_BIND     1024
89 #define OPTION_MOVE     1025
90 #define OPTION_MTAB     1026
91 #define OPTION_FSTAB    1027
92 #define OPTION_CHROOT   1028
93 #define OPTION_SECURE   1029
94 #define OPTION_RBIND    1030
95 #define OPTION_ROOTFS   1031
96
97 #define XFLAG_NOAUTO    0x01
98
99 static struct option const
100 CMDLINE_OPTIONS[] = {
101   { "help",    no_argument,       0, 'h' },
102   { "version", no_argument,       0, 'v' },
103   { "bind",    no_argument,       0, OPTION_BIND },
104   { "move",    no_argument,       0, OPTION_MOVE },
105   { "mtab",    required_argument, 0, OPTION_MTAB },
106   { "fstab",   required_argument, 0, OPTION_FSTAB },
107   { "rootfs",  required_argument, 0, OPTION_ROOTFS },
108   { "chroot",  no_argument,       0, OPTION_CHROOT },
109   { "secure",  no_argument,       0, OPTION_SECURE },
110   { "rbind",   no_argument,       0, OPTION_RBIND },
111   { 0, 0, 0, 0 }
112 };
113
114 #ifndef MS_REC
115 #  define MS_REC        0x4000
116 #endif
117
118 static struct FstabOption {
119     char const * const  opt;
120     unsigned long const         flag;
121     unsigned long const         mask;
122     unsigned long const         xflag;
123     bool const                  is_dflt;
124 } const FSTAB_OPTIONS[] = {
125   { "defaults",   MS_NODEV,      (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|
126                                   MS_SYNCHRONOUS), 0, false },
127   { "rbind",      MS_BIND|MS_REC, MS_BIND|MS_REC,  0, false },
128   { "bind",       MS_BIND,        MS_BIND,         0, false },
129   { "move",       MS_MOVE,        MS_MOVE,         0, false },
130   { "async",      0,              MS_SYNCHRONOUS,  0, false },
131   { "sync",       MS_SYNCHRONOUS, MS_SYNCHRONOUS,  0, false },
132   { "atime",      0,              MS_NOATIME,      0, false },
133   { "noatime",    MS_NOATIME,     MS_NOATIME,      0, false },
134   { "dev",        0,              MS_NODEV,        0, false },
135   { "nodev",      MS_NODEV,       MS_NODEV,        0, false },
136   { "exec",       0,              MS_NOEXEC,       0, false },
137   { "noexec",     MS_NOEXEC,      MS_NOEXEC,       0, false },
138   { "suid",       0,              MS_NOSUID,       0, false },
139   { "nosuid",     MS_NOSUID,      MS_NOSUID,       0, false },
140   { "ro",         MS_RDONLY,      MS_RDONLY,       0, false },
141   { "rw",         0,              MS_RDONLY,       0, false },
142   
143   { "remount",    MS_REMOUNT,     MS_REMOUNT,      0, false },
144   { "users",      MS_NOEXEC|MS_NOSUID|MS_NODEV,
145                   MS_NOEXEC|MS_NOSUID|MS_NODEV,    0, false },
146   { "mandlock",   MS_MANDLOCK,    MS_MANDLOCK,     0, false },
147   { "nodiratime", MS_NODIRATIME,  MS_NODIRATIME,   0, false },
148 #ifdef MS_DIRSYNC  
149   { "dirsync",    MS_DIRSYNC,     MS_DIRSYNC,      0, false },
150 #endif
151   { "_netdev",    0,              0,               0, false },
152   { "auto",       0,              0,               0, false },
153   { "noauto",     0,              0,               XFLAG_NOAUTO, false },
154   { "user",       0,              0,               0, false },
155   { "nouser",     0,              0,               0, false },
156 };
157
158 int                     wrapper_exit_code = 1;
159
160 static void
161 showHelp(int fd, char const *cmd, int res)
162 {
163   VSERVER_DECLARE_CMD(cmd);
164   
165   WRITE_MSG(fd, "Usage:  ");
166   WRITE_STR(fd, cmd);
167   WRITE_MSG(fd,
168             " [--help] [--version] [--bind] [--move] [--rbind] [-t <type>] [--chroot]\n"
169             "            [--mtab <filename>] [--fstab <filename>] [--rootfs yes|no|only]\n"
170             "            [-n] -a|([-o <options>] [--] <src> <dst>)\n\n"
171             "Executes mount-operations under the current directory: it assumes sources in\n"
172             "the current root-dir while destinations are expected in the chroot environment.\n\n"
173             "For non-trivial mount-operations it uses the external 'mount' program which\n"
174             "can be overridden by the $MOUNT environment variable.\n\n"
175             "Options:\n"
176             "  --bind|move|rbind        ...  set the correspond flags; with this options\n"
177             "                                the mount will be executed internally without\n"
178             "                                calling an external mount program.\n"
179             "  -t <type>                ...  assume the given filesystem type\n"
180             "  -o <options>             ...  set additional options; see mount(2) for details\n"
181             "  -n                       ...  do not update the mtab-file\n"
182             "  --mtab <filename>        ...  use <filename> as an alternative mtab file\n"
183             "                                [default: /etc/mtab]\n"
184             "  --chroot                 ...  chroot into the current directory before\n"
185             "                                mounting the filesystem\n"
186             "  --fstab <filename>       ...  use <filename> as an alternative fstab file;\n"
187             "                                this option has an effect only with the '-a'\n"
188             "                                option [default: /etc/fstab]\n"
189             "  --rootfs yes|no|only     ...  specifies how to handle an entry for a rootfs\n"
190             "                                ('/') when processing an fstab file. 'yes' will\n"
191             "                                mount it among the other entries, 'only' will\n"
192             "                                mount only the rootfs entry, and 'no' will ignore\n"
193             "                                it and mount only the other entries [default: yes]\n"
194             "  -a                       ...  mount everything listed in the fstab-file\n\n"
195             "  <src>                    ...  the source-filesystem; this path is absolute\n"
196             "                                to the current root-filesystem. Only valid\n"
197             "                                without the '-a' option.\n"
198             "  <dst>                    ...  the destination mount-point; when used with\n"
199             "                                '--chroot', this path is relative to the current\n"
200             "                                directory. Only valid without the '-a' option\n\n"
201             "Please report bugs to " PACKAGE_BUGREPORT "\n");
202
203   exit(res);
204 }
205
206 static void
207 showVersion()
208 {
209   WRITE_MSG(1,
210             "secure-mount " VERSION " -- secure mounting of directories\n"
211             "This program is part of " PACKAGE_STRING "\n\n"
212             "Copyright (C) 2003 Enrico Scholz\n"
213             VERSION_COPYRIGHT_DISCLAIMER);
214   exit(0);
215 }
216
217 inline static bool
218 isSameObject(struct stat const *lhs,
219              struct stat const *rhs)
220 {
221   return (lhs->st_dev==rhs->st_dev &&
222           lhs->st_ino==rhs->st_ino);
223 }
224
225 static int
226 fchroot(int fd)
227 {
228   if (fchdir(fd)==-1 || chroot(".")==-1) return -1;
229   return 0;
230 }
231
232 static int
233 writeX(int fd, void const *buf, size_t len)
234 {
235   if ((size_t)(write(fd, buf, len))!=len) return -1;
236   return 0;
237 }
238
239 static int
240 writeStrX(int fd, char const *str)
241 {
242   return writeX(fd, str, strlen(str));
243 }
244
245 static inline char const *
246 getType(struct MountInfo const *mnt)
247 {
248   if (mnt->type==0)                         return "none";
249   else if (strncmp(mnt->type, "ext", 3)==0) return "ufs";
250   else                                      return mnt->type;
251 }
252
253 inline static void
254 restoreRoot(struct Options const *opt)
255 {
256   if (opt->do_chroot!=0 && fchroot(opt->cur_rootdir_fd)==-1) {
257     perror("secure-mount: fchdir(\"/\")");
258     WRITE_MSG(2, "Failed to restore root-directory; aborting\n");
259     exit(1);
260   }
261 }
262
263 static int
264 updateMtab(struct MountInfo const *mnt, struct Options const *opt)
265 {
266   int           res = -1;
267   int           fd;
268   assert(opt->mtab!=0);
269
270   if (opt->do_chroot && fchroot(opt->cur_dir_fd)==-1) {
271       perror("secure-mount: fchroot(\".\")");
272       return -1;
273   }
274
275   fd=open(opt->mtab, O_CREAT|O_APPEND|O_WRONLY, 0644);
276   
277   if (fd==-1) {
278     perror("secure-mount: open(<mtab>)");
279     goto err0;
280   }
281
282   if (flock(fd, LOCK_EX)==-1) {
283     perror("secure-mount: flock()");
284     goto err1;
285   }
286
287   if (writeStrX(fd, mnt->src)==-1 ||
288       writeStrX(fd, " ")==-1 ||
289       writeStrX(fd, mnt->dst)==-1 ||
290       writeStrX(fd, " ")==-1 ||
291       writeStrX(fd, getType(mnt))==-1 ||
292       writeStrX(fd, " ")==-1 ||
293       writeStrX(fd, mnt->data ? mnt->data : "defaults")==-1 ||
294       writeStrX(fd, " 0 0\n")==-1) {
295     perror("secure-mount: write()");
296     goto err1;
297   }
298
299   res = 0;
300
301   err1: close(fd);
302   err0:
303   restoreRoot(opt);
304   return res;
305 }
306
307 static bool
308 callExternalMount(struct MountInfo const *mnt)
309 {
310   char const *  argv[10];
311   size_t        idx = 0;
312   pid_t         pid;
313   int           status;
314   char const *  mount_prog = getenv("MOUNT");
315
316   if (mount_prog==0) mount_prog = MOUNT_PROG;
317
318   argv[idx++] = mount_prog;
319   argv[idx++] = "-n";
320   if      (mnt->flag & MS_BIND) argv[idx++] = "--bind";
321   else if (mnt->flag & MS_MOVE) argv[idx++] = "--move";
322
323   argv[idx++] = "-o";
324   if (mnt->data && *mnt->data &&
325       strcmp(mnt->data, "defaults")!=0) {
326     if (mnt->mask & MS_NODEV)
327       argv[idx++] = mnt->data;
328     else {
329       char *    tmp = alloca(strlen(mnt->data) + sizeof("nodev,"));
330       strcpy(tmp, "nodev,");
331       strcat(tmp, mnt->data);
332       argv[idx++] = tmp;
333     }
334   }
335   else
336     argv[idx++] = "nodev";
337
338   if (mnt->type) {
339     argv[idx++] = "-t";
340     argv[idx++] = mnt->type;
341   }
342
343   argv[idx++] = mnt->src;
344   argv[idx++] = ".";
345   argv[idx]   = 0;
346
347   pid = fork();
348   if (pid==-1) {
349     perror("secure-mount: fork()");
350     return false;
351   }
352
353   if (pid==0) {
354     execv(mount_prog, const_cast(char **)(argv));
355     PERROR_Q("secure-mount: execv", mount_prog);
356     exit(1);
357   }
358
359   if (wait4(pid, &status, 0, 0)==-1) {
360     perror("secure-mount: wait4()");
361     return false;
362   }
363
364   return (WIFEXITED(status)) && (WEXITSTATUS(status)==0);
365 }
366
367 inline static bool
368 secureChdir(char const *dir, struct Options const *opt)
369 {
370   int           dir_fd;
371   bool          res = false;
372   
373   if (opt->do_chroot!=0 && fchroot(opt->cur_dir_fd)==-1) {
374     perror("secure-mount: fchroot(\".\")");
375     return false;
376   }
377
378   if (chdir(dir)==-1) {
379     PERROR_Q("secure-mount: chdir", dir);
380     goto err;
381   }
382
383   dir_fd = open(".", O_RDONLY|O_DIRECTORY);
384   if (dir_fd==-1) {
385     perror("secure-mount: open(\".\")");
386     goto err;
387   }
388
389   restoreRoot(opt);
390   if (fchdir(dir_fd)==-1)
391     PERROR_Q("secure-mount: fchdir", dir);
392   else
393     res = true;
394   
395   close(dir_fd);
396   return res;
397
398   err:
399   restoreRoot(opt);
400   return false;
401 }
402
403 static bool
404 canHandleInternal(struct MountInfo const *mnt)
405 {
406   static char const *   FS[] = {
407     "tmpfs", "sysfs", "proc", "sockfs", "pipefs", "futexfs",
408     "inotifyfs", "devpts", "ext3", "ext2", "ramfs",
409     "hugetlbfs", "usbfs", "binfmt_misc",
410     0
411   };
412   char const **         i;
413   
414   if (!mnt)                                 return false;
415   else if ((mnt->flag & (MS_BIND|MS_MOVE))) return true;
416   else if (mnt->type==0)                    return false;
417
418   for (i=FS+0; *i!=0; ++i)
419     if (strcmp(mnt->type, *i)==0) return true;
420
421   return false;
422 }
423
424 static bool
425 mountSingle(struct MountInfo const *mnt, struct Options const *opt)
426 {
427   assert(mnt->dst!=0);
428   
429   if (!secureChdir(mnt->dst, opt))
430     return false;
431
432   if (canHandleInternal(mnt)) {
433     if (mount(mnt->src, ".",
434               mnt->type ? mnt->type : "",
435               mnt->flag,  mnt->data_parsed)==-1) {
436       perror("secure-mount: mount()");
437       return false;
438     }
439   }
440   else if (!callExternalMount(mnt))
441     return false;
442
443   if (!opt->ignore_mtab &&
444       updateMtab(mnt, opt)==-1) {
445     WRITE_MSG(2, "Failed to update mtab-file\n");
446       // no error
447   }
448   
449   return true;
450 }
451
452 static struct FstabOption const *
453 searchOption(char const *opt, size_t len)
454 {
455   struct FstabOption const *            i;
456   for (i=FSTAB_OPTIONS+0; i<FSTAB_OPTIONS+DIM_OF(FSTAB_OPTIONS); ++i)
457     if (strncmp(i->opt, opt, len)==0) return i;
458
459   return 0;
460 }
461
462 static bool
463 transformOptionList(struct MountInfo *info, size_t UNUSED *col)
464 {
465   char const *                  ptr = info->data;
466   char *                        data = malloc(strlen(info->data));
467   char *                        dst = data;
468
469   do {
470     char const *                pos = strchr(ptr, ',');
471     struct FstabOption const *  opt;
472     
473     if (pos==0) pos = ptr+strlen(ptr);
474     opt = searchOption(ptr, pos-ptr);
475
476     if (opt!=0) {
477       info->flag  &= ~opt->mask;
478       info->flag  |=  opt->flag;
479       info->mask  |=  opt->mask;
480       info->xflag |=  opt->xflag;
481     }
482     else {
483       if (dst != data)
484         *(dst++) = ',';
485       strncpy(dst, ptr, pos-ptr);
486       dst += pos - ptr;
487       *dst = '\0';
488     }
489
490     if (*pos!='\0')
491       ptr = pos+1;
492     else
493       ptr = pos;
494
495   } while (*ptr!='\0');
496
497   info->data_parsed = data;
498   return true;
499 }
500
501 #define MOVE_TO_NEXT_FIELD(PTR,ALLOW_EOL)               \
502   while (!isspace(*PTR) && *PTR!='\0') ++PTR;           \
503   if (col) *col = buf-start_buf+1;                      \
504   if (!(ALLOW_EOL) && *PTR=='\0') return prFAIL;        \
505   *PTR++ = '\0';                                        \
506   while (isspace(*PTR)) ++PTR
507
508 static enum {prDOIT, prFAIL, prIGNORE}
509   parseFstabLine(struct MountInfo *info, char *buf, size_t *col)
510 {
511   char const * const    start_buf = buf;
512   size_t                err_col;
513
514   while (isspace(*buf)) ++buf;
515   if (*buf=='#' || *buf=='\0')  return prIGNORE;
516
517   info->src  = buf;
518   MOVE_TO_NEXT_FIELD(buf, false);
519   info->dst  = buf;
520   MOVE_TO_NEXT_FIELD(buf, false);
521   info->type = buf;
522   MOVE_TO_NEXT_FIELD(buf, false);
523   err_col    = buf-start_buf+1;
524   info->data = buf;
525   MOVE_TO_NEXT_FIELD(buf, true);
526
527   info->flag  = MS_NODEV;
528   info->mask  = 0;
529   info->xflag = 0;
530
531   if      (strcmp(info->type, "swap")  ==0) return prIGNORE;
532   else if (strcmp(info->type, "none")  ==0) info->type  = 0;
533   else if (strcmp(info->type, "devpts")==0) {
534     info->mask |=  MS_NODEV;
535     info->flag &= ~MS_NODEV;
536   }
537
538   if (col) *col = err_col;
539   if (!transformOptionList(info,col)) return prFAIL;
540   if (info->xflag & XFLAG_NOAUTO)     return prIGNORE;
541
542   return prDOIT;
543 }
544
545 #undef MOVE_TO_NEXT_FIELD
546
547 static void
548 showFstabPosition(int fd, char const *fname, size_t line_nr, size_t col_nr)
549 {
550   char          buf[3*sizeof(line_nr)*2 + 4];
551   size_t        len = utilvserver_fmt_uint(buf+1, line_nr)+1;
552   
553   buf[0]     = ':';
554   buf[len++] = ':';
555   len += utilvserver_fmt_uint(buf+len, col_nr);
556   WRITE_STR(fd, fname);
557   Vwrite(fd, buf, len);
558 }
559
560
561 static bool
562 mountFstab(struct Options const *opt)
563 {
564   bool          res = false;
565   int           fd;
566   off_t         len;
567
568   assert(opt->fstab!=0);
569   fd = open(opt->fstab, O_RDONLY);
570   if (fd==-1) {
571     perror("secure-mount: open(<fstab>)");
572     goto err0;
573   }
574
575   len = lseek(fd, 0, SEEK_END); 
576   if (len==-1 ||
577       lseek(fd, 0, SEEK_SET)==-1) {
578     perror("secure-mount: lseek(<fstab>)");
579     goto err1;
580   }
581
582   {
583     char        buf[len+2];
584     char        *ptr, *ptrptr;
585     size_t      line_nr=0, col_nr;
586
587     if (read(fd, buf, len+1)!=len) {
588       perror("secure-mount: read()");
589       goto err1;
590     }
591     buf[len]   = '#';   // workaround for broken dietlibc strtok_r()
592                         // implementation
593     buf[len+1] = '\0';
594     ptrptr     = buf;
595
596     while ((ptr=strsep(&ptrptr, "\n")) != 0) {
597       struct MountInfo  mnt;
598       ++line_nr;
599
600       switch (parseFstabLine(&mnt, ptr, &col_nr)) {
601         case prFAIL     :
602           showFstabPosition(2, opt->fstab, line_nr, col_nr);
603           WRITE_MSG(2, ": syntax error\n");
604           goto err1;
605
606         case prIGNORE   :  break;
607         case prDOIT     : {
608           bool          is_rootfs = (strcmp(mnt.dst, "/")==0);
609           Echdir("/");
610           if (( is_rootfs && opt->rootfs==rfsNO) ||
611               (!is_rootfs && opt->rootfs==rfsONLY)) { /* ignore the entry */ }
612           else if (!mountSingle(&mnt, opt)) {
613             showFstabPosition(2, opt->fstab, line_nr, 1);
614             WRITE_MSG(2, ": failed to mount fstab-entry\n");
615           }
616           break;
617         }
618         default         :
619           assert(false);
620       }
621     }
622   }
623
624   res = true;
625
626   err1: close(fd);
627   err0: return res;
628 }
629
630 static void
631 initFDs(struct Options *opt)
632 {
633   opt->cur_dir_fd     = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
634   opt->cur_rootdir_fd = Eopen("/", O_RDONLY|O_DIRECTORY, 0);
635
636   Efcntl(opt->cur_dir_fd,     F_SETFD, FD_CLOEXEC);
637   Efcntl(opt->cur_rootdir_fd, F_SETFD, FD_CLOEXEC);
638 }
639
640 static RootFsOption
641 parseRootFS(char const *str)
642 {
643   if      (strcasecmp(str, "yes")==0)  return rfsYES;
644   else if (strcasecmp(str, "no")==0)   return rfsNO;
645   else if (strcasecmp(str, "only")==0) return rfsONLY;
646   else {
647     WRITE_MSG(2, "secure-mount: invalid option for '--rootfs': '");
648     WRITE_STR(2, str);
649     WRITE_MSG(2, "'\n");
650     exit(wrapper_exit_code);
651   }
652 }
653
654 int main(int argc, char *argv[])
655 {
656   struct MountInfo      mnt = {
657     .src         = 0,
658     .dst         = 0,
659     .type        = 0,
660     .flag        = MS_NODEV,
661     .xflag       = 0,
662     .data        = 0,
663   };
664
665   struct Options        opt = {
666     .mtab           = "/etc/mtab",
667     .fstab          = "/etc/fstab",
668     .do_chroot      = 0,
669     .ignore_mtab    = false,
670     .mount_all      = false,
671     .cur_dir_fd     = -1,
672     .cur_rootdir_fd = -1,
673     .rootfs         = rfsYES
674   };
675
676   while (1) {
677     int         c = getopt_long(argc, argv, "ht:nao:", CMDLINE_OPTIONS, 0);
678     if (c==-1) break;
679     
680     switch (c) {
681       case 'h'          :  showHelp(1, argv[0], 0);
682       case 'v'          :  showVersion();
683       case 't'          :  mnt.type = optarg;         break;
684       case 'n'          :  opt.ignore_mtab = true;    break;
685       case 'a'          :  opt.mount_all   = true;    break;
686       case 'o'          :  mnt.data        = optarg;  break;
687       case OPTION_RBIND :  mnt.flag       |= MS_REC;  /*@fallthrough@*/
688       case OPTION_BIND  :  mnt.flag       |= MS_BIND; break;
689       case OPTION_MOVE  :  mnt.flag       |= MS_MOVE; break;
690       case OPTION_MTAB  :  opt.mtab        = optarg;  break;
691       case OPTION_FSTAB :  opt.fstab       = optarg;  break;
692       case OPTION_CHROOT:  opt.do_chroot   = true;    break;
693       case OPTION_ROOTFS:  opt.rootfs      = parseRootFS(optarg); break;
694       case OPTION_SECURE:
695         WRITE_MSG(2, "secure-mount: The '--secure' option is deprecated...\n");
696         break;
697       default           :
698         WRITE_MSG(2, "Try '");
699         WRITE_STR(2, argv[0]);
700         WRITE_MSG(2, " --help' for more information.\n");
701         return EXIT_FAILURE;
702         break;
703     }
704   }
705
706
707   if (opt.mount_all && optind<argc) {
708     WRITE_MSG(2, "Can not specify <src> and '-a' at the same time\n");
709     return EXIT_FAILURE;
710   }
711
712   initFDs(&opt);
713   signal(SIGCHLD, SIG_DFL);
714   
715   if (opt.mount_all) {
716     if (!mountFstab(&opt)) return EXIT_FAILURE;
717     else                   return EXIT_SUCCESS;
718   }
719
720   if (optind+2!=argc) {
721     WRITE_MSG(2, "Invalid <src> <dst> pair specified\n");
722     return EXIT_FAILURE;
723   }
724
725   if (mnt.data) {
726     mnt.data = strdup(mnt.data);
727     if (!transformOptionList(&mnt, 0)) {
728       WRITE_MSG(2, "Invalid options specified\n");
729       return EXIT_FAILURE;
730     }
731   }
732     
733   mnt.src  = argv[optind++];
734   mnt.dst  = argv[optind++];
735
736   if (!mountSingle(&mnt, &opt)) return EXIT_FAILURE;
737     
738   return EXIT_SUCCESS;
739 }