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