3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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.
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.
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.
19 // secure-mount <general mount(8) options> [--chroot]
20 // [--mtab <mtabfile>] [--fstab <fstabfile>]
22 // Executes mount-operations under the current directory: it assumes sources
23 // in the current root-dir while destinations are expected in the chroot
32 #include "pathconfig.h"
34 #include <lib/internal.h>
45 #include <sys/mount.h>
47 #include <sys/types.h>
56 #define ENSC_WRAPPERS_FCNTL 1
59 #define MNTPOINT "/etc"
61 typedef enum { rfsYES, rfsNO, rfsONLY } RootFsOption;
85 #define OPTION_BIND 1024
86 #define OPTION_MOVE 1025
87 #define OPTION_MTAB 1026
88 #define OPTION_FSTAB 1027
89 #define OPTION_CHROOT 1028
90 #define OPTION_SECURE 1029
91 #define OPTION_RBIND 1030
92 #define OPTION_ROOTFS 1031
94 #define XFLAG_NOAUTO 0x01
96 static struct option const
98 { "help", no_argument, 0, 'h' },
99 { "version", no_argument, 0, 'v' },
100 { "bind", no_argument, 0, OPTION_BIND },
101 { "move", no_argument, 0, OPTION_MOVE },
102 { "mtab", required_argument, 0, OPTION_MTAB },
103 { "fstab", required_argument, 0, OPTION_FSTAB },
104 { "rootfs", required_argument, 0, OPTION_ROOTFS },
105 { "chroot", no_argument, 0, OPTION_CHROOT },
106 { "secure", no_argument, 0, OPTION_SECURE },
107 { "rbind", no_argument, 0, OPTION_RBIND },
112 # define MS_REC 0x4000
115 static struct FstabOption {
116 char const * const opt;
117 unsigned long const flag;
118 unsigned long const mask;
119 unsigned long const xflag;
121 } const FSTAB_OPTIONS[] = {
122 { "defaults", 0, (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|
123 MS_SYNCHRONOUS), 0, false },
124 { "rbind", MS_BIND|MS_REC, MS_BIND|MS_REC, 0, false },
125 { "bind", MS_BIND, MS_BIND, 0, false },
126 { "move", MS_MOVE, MS_MOVE, 0, false },
127 { "async", 0, MS_SYNCHRONOUS, 0, false },
128 { "sync", MS_SYNCHRONOUS, MS_SYNCHRONOUS, 0, false },
129 { "atime", 0, MS_NOATIME, 0, false },
130 { "noatime", MS_NOATIME, MS_NOATIME, 0, false },
131 { "dev", 0, MS_NODEV, 0, false },
132 { "nodev", MS_NODEV, MS_NODEV, 0, false },
133 { "exec", 0, MS_NOEXEC, 0, false },
134 { "noexec", MS_NOEXEC, MS_NOEXEC, 0, false },
135 { "suid", 0, MS_NOSUID, 0, false },
136 { "nosuid", MS_NOSUID, MS_NOSUID, 0, false },
137 { "ro", MS_RDONLY, MS_RDONLY, 0, false },
138 { "rw", 0, MS_RDONLY, 0, false },
140 { "remount", MS_REMOUNT, MS_REMOUNT, 0, false },
141 { "users", MS_NOEXEC|MS_NOSUID|MS_NODEV,
142 MS_NOEXEC|MS_NOSUID|MS_NODEV, 0, false },
143 { "mandlock", MS_MANDLOCK, MS_MANDLOCK, 0, false },
144 { "nodiratime", MS_NODIRATIME, MS_NODIRATIME, 0, false },
146 { "dirsync", MS_DIRSYNC, MS_DIRSYNC, 0, false },
148 { "_netdev", 0, 0, 0, false },
149 { "auto", 0, 0, 0, false },
150 { "noauto", 0, 0, XFLAG_NOAUTO, false },
151 { "user", 0, 0, 0, false },
152 { "nouser", 0, 0, 0, false },
155 int wrapper_exit_code = 1;
158 showHelp(int fd, char const *cmd, int res)
160 VSERVER_DECLARE_CMD(cmd);
162 WRITE_MSG(fd, "Usage: ");
165 " [--help] [--version] [--bind] [--move] [--rbind] [-t <type>] [--chroot]\n"
166 " [--mtab <filename>] [--fstab <filename>] [--rootfs yes|no|only]\n"
167 " [-n] -a|([-o <options>] [--] <src> <dst>)\n\n"
168 "Executes mount-operations under the current directory: it assumes sources in\n"
169 "the current root-dir while destinations are expected in the chroot environment.\n\n"
170 "For non-trivial mount-operations it uses the external 'mount' program which\n"
171 "can be overridden by the $MOUNT environment variable.\n\n"
173 " --bind|move|rbind ... set the correspond flags; with this options\n"
174 " the mount will be executed internally without\n"
175 " calling an external mount program.\n"
176 " -t <type> ... assume the given filesystem type\n"
177 " -o <options> ... set additional options; see mount(2) for details\n"
178 " -n ... do not update the mtab-file\n"
179 " --mtab <filename> ... use <filename> as an alternative mtab file\n"
180 " [default: /etc/mtab]\n"
181 " --chroot ... chroot into the current directory before\n"
182 " mounting the filesystem\n"
183 " --fstab <filename> ... use <filename> as an alternative fstab file;\n"
184 " this option has an effect only with the '-a'\n"
185 " option [default: /etc/fstab]\n"
186 " --rootfs yes|no|only ... specifies how to handle an entry for a rootfs\n"
187 " ('/') when processing an fstab file. 'yes' will\n"
188 " mount it among the other entries, 'only' will\n"
189 " mount only the rootfs entry, and 'no' will ignore\n"
190 " it and mount only the other entries [default: yes]\n"
191 " -a ... mount everything listed in the fstab-file\n\n"
192 " <src> ... the source-filesystem; this path is absolute\n"
193 " to the current root-filesystem. Only valid\n"
194 " without the '-a' option.\n"
195 " <dst> ... the destination mount-point; when used with\n"
196 " '--chroot', this path is relative to the current\n"
197 " directory. Only valid without the '-a' option\n\n"
198 "Please report bugs to " PACKAGE_BUGREPORT "\n");
207 "secure-mount " VERSION " -- secure mounting of directories\n"
208 "This program is part of " PACKAGE_STRING "\n\n"
209 "Copyright (C) 2003 Enrico Scholz\n"
210 VERSION_COPYRIGHT_DISCLAIMER);
215 isSameObject(struct stat const *lhs,
216 struct stat const *rhs)
218 return (lhs->st_dev==rhs->st_dev &&
219 lhs->st_ino==rhs->st_ino);
225 if (fchdir(fd)==-1 || chroot(".")==-1) return -1;
230 writeX(int fd, void const *buf, size_t len)
232 if ((size_t)(write(fd, buf, len))!=len) return -1;
237 writeStrX(int fd, char const *str)
239 return writeX(fd, str, strlen(str));
242 static inline char const *
243 getType(struct MountInfo const *mnt)
245 if (mnt->type==0) return "none";
246 else if (strncmp(mnt->type, "ext", 3)==0) return "ufs";
247 else return mnt->type;
251 restoreRoot(struct Options const *opt)
253 if (opt->do_chroot!=0 && fchroot(opt->cur_rootdir_fd)==-1) {
254 perror("secure-mount: fchdir(\"/\")");
255 WRITE_MSG(2, "Failed to restore root-directory; aborting\n");
261 updateMtab(struct MountInfo const *mnt, struct Options const *opt)
265 assert(opt->mtab!=0);
267 if (opt->do_chroot && fchroot(opt->cur_dir_fd)==-1) {
268 perror("secure-mount: fchroot(\".\")");
272 fd=open(opt->mtab, O_CREAT|O_APPEND|O_WRONLY, 0644);
275 perror("secure-mount: open(<mtab>)");
279 if (flock(fd, LOCK_EX)==-1) {
280 perror("secure-mount: flock()");
284 if (writeStrX(fd, mnt->src)==-1 ||
285 writeStrX(fd, " ")==-1 ||
286 writeStrX(fd, mnt->dst)==-1 ||
287 writeStrX(fd, " ")==-1 ||
288 writeStrX(fd, getType(mnt))==-1 ||
289 writeStrX(fd, " ")==-1 ||
290 writeStrX(fd, mnt->data ? mnt->data : "defaults")==-1 ||
291 writeStrX(fd, " 0 0\n")==-1) {
292 perror("secure-mount: write()");
305 callExternalMount(struct MountInfo const *mnt)
307 char const * argv[10];
311 char const * mount_prog = getenv("MOUNT");
313 if (mount_prog==0) mount_prog = MOUNT_PROG;
315 argv[idx++] = mount_prog;
317 if (mnt->flag & MS_BIND) argv[idx++] = "--bind";
318 else if (mnt->flag & MS_MOVE) argv[idx++] = "--move";
321 if (mnt->data && *mnt->data &&
322 strcmp(mnt->data, "defaults")!=0) {
323 if (mnt->mask & MS_NODEV)
324 argv[idx++] = mnt->data;
326 char * tmp = alloca(strlen(mnt->data) + sizeof("nodev,"));
327 strcpy(tmp, "nodev,");
328 strcat(tmp, mnt->data);
333 argv[idx++] = "nodev";
337 argv[idx++] = mnt->type;
340 argv[idx++] = mnt->src;
346 perror("secure-mount: fork()");
351 execv(mount_prog, const_cast(char **)(argv));
352 PERROR_Q("secure-mount: execv", mount_prog);
356 if (wait4(pid, &status, 0, 0)==-1) {
357 perror("secure-mount: wait4()");
361 return (WIFEXITED(status)) && (WEXITSTATUS(status)==0);
365 secureChdir(char const *dir, struct Options const *opt)
370 if (opt->do_chroot!=0 && fchroot(opt->cur_dir_fd)==-1) {
371 perror("secure-mount: fchroot(\".\")");
375 if (chdir(dir)==-1) {
376 PERROR_Q("secure-mount: chdir", dir);
380 dir_fd = open(".", O_RDONLY|O_DIRECTORY);
382 perror("secure-mount: open(\".\")");
387 if (fchdir(dir_fd)==-1)
388 PERROR_Q("secure-mount: fchdir", dir);
401 mountSingle(struct MountInfo const *mnt, struct Options const *opt)
405 if (!secureChdir(mnt->dst, opt))
408 if (mnt->flag & (MS_BIND|MS_MOVE)) {
409 unsigned long flag = mnt->flag;
410 if ((flag & MS_NODEV)==0) flag |= MS_NODEV;
412 if (mount(mnt->src, ".",
413 mnt->type ? mnt->type : "",
414 flag, mnt->data)==-1) {
415 perror("secure-mount: mount()");
419 else if (!callExternalMount(mnt))
422 if (!opt->ignore_mtab &&
423 updateMtab(mnt, opt)==-1) {
424 WRITE_MSG(2, "Failed to update mtab-file\n");
431 static struct FstabOption const *
432 searchOption(char const *opt, size_t len)
434 struct FstabOption const * i;
435 for (i=FSTAB_OPTIONS+0; i<FSTAB_OPTIONS+DIM_OF(FSTAB_OPTIONS); ++i)
436 if (strncmp(i->opt, opt, len)==0) return i;
442 transformOptionList(struct MountInfo *info, size_t UNUSED *col)
444 char const * ptr = info->data;
447 char const * pos = strchr(ptr, ',');
448 struct FstabOption const * opt;
450 if (pos==0) pos = ptr+strlen(ptr);
451 opt = searchOption(ptr, pos-ptr);
454 info->flag &= ~opt->mask;
455 info->flag |= opt->flag;
456 info->mask |= opt->mask;
457 info->xflag |= opt->xflag;
465 } while (*ptr!='\0');
470 #define MOVE_TO_NEXT_FIELD(PTR,ALLOW_EOL) \
471 while (!isspace(*PTR) && *PTR!='\0') ++PTR; \
472 if (col) *col = buf-start_buf+1; \
473 if (!(ALLOW_EOL) && *PTR=='\0') return prFAIL; \
475 while (isspace(*PTR)) ++PTR
477 static enum {prDOIT, prFAIL, prIGNORE}
478 parseFstabLine(struct MountInfo *info, char *buf, size_t *col)
480 char const * const start_buf = buf;
483 while (isspace(*buf)) ++buf;
484 if (*buf=='#' || *buf=='\0') return prIGNORE;
487 MOVE_TO_NEXT_FIELD(buf, false);
489 MOVE_TO_NEXT_FIELD(buf, false);
491 MOVE_TO_NEXT_FIELD(buf, false);
492 err_col = buf-start_buf+1;
494 MOVE_TO_NEXT_FIELD(buf, true);
496 info->flag = MS_NODEV;
500 if (strcmp(info->type, "swap") ==0) return prIGNORE;
501 else if (strcmp(info->type, "none") ==0) info->type = 0;
502 else if (strcmp(info->type, "devpts")==0) info->mask |= MS_NODEV;
504 if (col) *col = err_col;
505 if (!transformOptionList(info,col)) return prFAIL;
506 if (info->xflag & XFLAG_NOAUTO) return prIGNORE;
511 #undef MOVE_TO_NEXT_FIELD
514 showFstabPosition(int fd, char const *fname, size_t line_nr, size_t col_nr)
516 char buf[3*sizeof(line_nr)*2 + 4];
517 size_t len = utilvserver_fmt_uint(buf+1, line_nr)+1;
521 len += utilvserver_fmt_uint(buf+len, col_nr);
522 WRITE_STR(fd, fname);
528 mountFstab(struct Options const *opt)
534 assert(opt->fstab!=0);
535 fd = open(opt->fstab, O_RDONLY);
537 perror("secure-mount: open(<fstab>)");
541 len = lseek(fd, 0, SEEK_END);
543 lseek(fd, 0, SEEK_SET)==-1) {
544 perror("secure-mount: lseek(<fstab>)");
551 size_t line_nr=0, col_nr;
553 if (read(fd, buf, len+1)!=len) {
554 perror("secure-mount: read()");
557 buf[len] = '#'; // workaround for broken dietlibc strtok_r()
562 while ((ptr=strsep(&ptrptr, "\n")) != 0) {
563 struct MountInfo mnt;
566 switch (parseFstabLine(&mnt, ptr, &col_nr)) {
568 showFstabPosition(2, opt->fstab, line_nr, col_nr);
569 WRITE_MSG(2, ": syntax error\n");
572 case prIGNORE : break;
574 bool is_rootfs = (strcmp(mnt.dst, "/")==0);
576 if (( is_rootfs && opt->rootfs==rfsNO) ||
577 (!is_rootfs && opt->rootfs==rfsONLY)) { /* ignore the entry */ }
578 else if (!mountSingle(&mnt, opt)) {
579 showFstabPosition(2, opt->fstab, line_nr, 1);
580 WRITE_MSG(2, ": failed to mount fstab-entry\n");
597 initFDs(struct Options *opt)
599 opt->cur_dir_fd = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
600 opt->cur_rootdir_fd = Eopen("/", O_RDONLY|O_DIRECTORY, 0);
602 Efcntl(opt->cur_dir_fd, F_SETFD, FD_CLOEXEC);
603 Efcntl(opt->cur_rootdir_fd, F_SETFD, FD_CLOEXEC);
607 parseRootFS(char const *str)
609 if (strcasecmp(str, "yes")==0) return rfsYES;
610 else if (strcasecmp(str, "no")==0) return rfsNO;
611 else if (strcasecmp(str, "only")==0) return rfsONLY;
613 WRITE_MSG(2, "secure-mount: invalid option for '--rootfs': '");
616 exit(wrapper_exit_code);
620 int main(int argc, char *argv[])
622 struct MountInfo mnt = {
631 struct Options opt = {
633 .fstab = "/etc/fstab",
635 .ignore_mtab = false,
638 .cur_rootdir_fd = -1,
643 int c = getopt_long(argc, argv, "ht:nao:", CMDLINE_OPTIONS, 0);
647 case 'h' : showHelp(1, argv[0], 0);
648 case 'v' : showVersion();
649 case 't' : mnt.type = optarg; break;
650 case 'n' : opt.ignore_mtab = true; break;
651 case 'a' : opt.mount_all = true; break;
652 case 'o' : mnt.data = optarg; break;
653 case OPTION_RBIND : mnt.flag |= MS_REC; /*@fallthrough@*/
654 case OPTION_BIND : mnt.flag |= MS_BIND; break;
655 case OPTION_MOVE : mnt.flag |= MS_MOVE; break;
656 case OPTION_MTAB : opt.mtab = optarg; break;
657 case OPTION_FSTAB : opt.fstab = optarg; break;
658 case OPTION_CHROOT: opt.do_chroot = true; break;
659 case OPTION_ROOTFS: opt.rootfs = parseRootFS(optarg); break;
661 WRITE_MSG(2, "secure-mount: The '--secure' option is deprecated...\n");
664 WRITE_MSG(2, "Try '");
665 WRITE_STR(2, argv[0]);
666 WRITE_MSG(2, " --help\" for more information.\n");
673 if (opt.mount_all && optind<argc) {
674 WRITE_MSG(2, "Can not specify <src> and '-a' at the same time\n");
679 signal(SIGCHLD, SIG_DFL);
682 if (!mountFstab(&opt)) return EXIT_FAILURE;
683 else return EXIT_SUCCESS;
686 if (optind+2!=argc) {
687 WRITE_MSG(2, "Invalid <src> <dst> pair specified\n");
692 mnt.data = strdup(mnt.data);
693 if (!transformOptionList(&mnt, 0)) {
694 WRITE_MSG(2, "Invalid options specified\n");
699 mnt.src = argv[optind++];
700 mnt.dst = argv[optind++];
702 if (!mountSingle(&mnt, &opt)) return EXIT_FAILURE;