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"
42 #include <sys/mount.h>
44 #include <sys/types.h>
53 #define ENSC_WRAPPERS_FCNTL 1
56 #define MNTPOINT "/etc"
78 #define OPTION_BIND 1024
79 #define OPTION_MOVE 1025
80 #define OPTION_MTAB 1026
81 #define OPTION_FSTAB 1027
82 #define OPTION_CHROOT 1028
83 #define OPTION_SECURE 1029
84 #define OPTION_RBIND 1030
86 #define XFLAG_NOAUTO 0x01
88 static struct option const
90 { "help", no_argument, 0, 'h' },
91 { "version", no_argument, 0, 'v' },
92 { "bind", no_argument, 0, OPTION_BIND },
93 { "move", no_argument, 0, OPTION_MOVE },
94 { "mtab", required_argument, 0, OPTION_MTAB },
95 { "fstab", required_argument, 0, OPTION_FSTAB },
96 { "chroot", no_argument, 0, OPTION_CHROOT },
97 { "secure", no_argument, 0, OPTION_SECURE },
98 { "rbind", no_argument, 0, OPTION_RBIND },
103 # define MS_REC 0x4000
106 static struct FstabOption {
107 char const * const opt;
108 unsigned long const flag;
109 unsigned long const mask;
110 unsigned long const xflag;
112 } const FSTAB_OPTIONS[] = {
113 { "defaults", 0, (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|
114 MS_SYNCHRONOUS), 0, false },
115 { "rbind", MS_BIND|MS_REC, MS_BIND|MS_REC, 0, false },
116 { "bind", MS_BIND, MS_BIND, 0, false },
117 { "move", MS_MOVE, MS_MOVE, 0, false },
118 { "async", 0, MS_SYNCHRONOUS, 0, false },
119 { "sync", MS_SYNCHRONOUS, MS_SYNCHRONOUS, 0, false },
120 { "atime", 0, MS_NOATIME, 0, false },
121 { "noatime", MS_NOATIME, MS_NOATIME, 0, false },
122 { "dev", 0, MS_NODEV, 0, false },
123 { "nodev", MS_NODEV, MS_NODEV, 0, false },
124 { "exec", 0, MS_NOEXEC, 0, false },
125 { "noexec", MS_NOEXEC, MS_NOEXEC, 0, false },
126 { "suid", 0, MS_NOSUID, 0, false },
127 { "nosuid", MS_NOSUID, MS_NOSUID, 0, false },
128 { "ro", MS_RDONLY, MS_RDONLY, 0, false },
129 { "rw", 0, MS_RDONLY, 0, false },
131 { "remount", MS_REMOUNT, MS_REMOUNT, 0, false },
132 { "users", MS_NOEXEC|MS_NOSUID|MS_NODEV,
133 MS_NOEXEC|MS_NOSUID|MS_NODEV, 0, false },
134 { "mandlock", MS_MANDLOCK, MS_MANDLOCK, 0, false },
135 { "nodiratime", MS_NODIRATIME, MS_NODIRATIME, 0, false },
137 { "dirsync", MS_DIRSYNC, MS_DIRSYNC, 0, false },
139 { "_netdev", 0, 0, 0, false },
140 { "auto", 0, 0, 0, false },
141 { "noauto", 0, 0, XFLAG_NOAUTO, false },
142 { "user", 0, 0, 0, false },
143 { "nouser", 0, 0, 0, false },
146 int wrapper_exit_code = 1;
149 showHelp(int fd, char const *cmd, int res)
151 VSERVER_DECLARE_CMD(cmd);
153 WRITE_MSG(fd, "Usage: ");
156 " [--help] [--version] [--bind] [--move] [--rbind] [-t <type>] [-n]\n"
157 " [--mtab <filename>] [--fstab <filename>] [--chroot] \n"
158 " -a|([-o <options>] [--] <src> <dst>)\n\n"
159 "Executes mount-operations under the current directory: it assumes sources in\n"
160 "the current root-dir while destinations are expected in the chroot environment.\n\n"
161 "For non-trivial mount-operations it uses the external 'mount' program which\n"
162 "can be overridden by the $MOUNT environment variable.\n\n"
164 " --bind|move|rbind ... set the correspond flags; with this options\n"
165 " the mount will be executed internally without\n"
166 " calling an external mount program.\n"
167 " -t <type> ... assume the given filesystem type\n"
168 " -o <options> ... set additional options; see mount(2) for details\n"
169 " -n ... do not update the mtab-file\n"
170 " --mtab <filename> ... use <filename> as an alternative mtab file\n"
171 " [default: /etc/mtab]\n"
172 " --chroot ... chroot into the current directory before\n"
173 " mounting the filesystem\n"
174 " --fstab <filename> ... use <filename> as an alternative fstab file;\n"
175 " this option has an effect only with the '-a'\n"
176 " option [default: /etc/fstab]\n"
177 " -a ... mount everything listed in the fstab-file\n\n"
178 " <src> ... the source-filesystem; this path is absolute\n"
179 " to the current root-filesystem. Only valid\n"
180 " without the '-a' option.\n"
181 " <dst> ... the destination mount-point; when used with\n"
182 " '--chroot', this path is relative to the current\n"
183 " directory. Only valid without the '-a' option\n\n"
184 "Please report bugs to " PACKAGE_BUGREPORT "\n");
193 "secure-mount " VERSION " -- secure mounting of directories\n"
194 "This program is part of " PACKAGE_STRING "\n\n"
195 "Copyright (C) 2003 Enrico Scholz\n"
196 VERSION_COPYRIGHT_DISCLAIMER);
201 isSameObject(struct stat const *lhs,
202 struct stat const *rhs)
204 return (lhs->st_dev==rhs->st_dev &&
205 lhs->st_ino==rhs->st_ino);
211 if (fchdir(fd)==-1 || chroot(".")==-1) return -1;
216 writeX(int fd, void const *buf, size_t len)
218 if ((size_t)(write(fd, buf, len))!=len) return -1;
223 writeStrX(int fd, char const *str)
225 return writeX(fd, str, strlen(str));
228 static inline char const *
229 getType(struct MountInfo const *mnt)
231 if (mnt->type==0) return "none";
232 else if (strncmp(mnt->type, "ext", 3)==0) return "ufs";
233 else return mnt->type;
237 restoreRoot(struct Options const *opt)
239 if (opt->do_chroot!=0 && fchroot(opt->cur_rootdir_fd)==-1) {
240 perror("secure-mount: fchdir(\"/\")");
241 WRITE_MSG(2, "Failed to restore root-directory; aborting\n");
247 updateMtab(struct MountInfo const *mnt, struct Options const *opt)
251 assert(opt->mtab!=0);
253 if (opt->do_chroot && fchroot(opt->cur_dir_fd)==-1) {
254 perror("secure-mount: fchroot(\".\")");
258 fd=open(opt->mtab, O_CREAT|O_APPEND|O_WRONLY, 0644);
261 perror("secure-mount: open(<mtab>)");
265 if (flock(fd, LOCK_EX)==-1) {
266 perror("secure-mount: flock()");
270 if (writeStrX(fd, mnt->src)==-1 ||
271 writeStrX(fd, " ")==-1 ||
272 writeStrX(fd, mnt->dst)==-1 ||
273 writeStrX(fd, " ")==-1 ||
274 writeStrX(fd, getType(mnt))==-1 ||
275 writeStrX(fd, " ")==-1 ||
276 writeStrX(fd, mnt->data ? mnt->data : "defaults")==-1 ||
277 writeStrX(fd, " 0 0\n")==-1) {
278 perror("secure-mount: write()");
291 callExternalMount(struct MountInfo const *mnt)
293 char const * argv[10];
297 char const * mount_prog = getenv("MOUNT");
299 if (mount_prog==0) mount_prog = MOUNT_PROG;
301 argv[idx++] = mount_prog;
303 if (mnt->flag & MS_BIND) argv[idx++] = "--bind";
304 else if (mnt->flag & MS_MOVE) argv[idx++] = "--move";
307 strcmp(mnt->data, "defaults")!=0) {
309 argv[idx++] = mnt->data;
314 argv[idx++] = mnt->type;
317 argv[idx++] = mnt->src;
323 perror("secure-mount: fork()");
328 execv(mount_prog, const_cast(char **)(argv));
329 PERROR_Q("secure-mount: execv", mount_prog);
333 if (wait4(pid, &status, 0, 0)==-1) {
334 perror("secure-mount: wait4()");
338 return (WIFEXITED(status)) && (WEXITSTATUS(status)==0);
342 secureChdir(char const *dir, struct Options const *opt)
347 if (opt->do_chroot!=0 && fchroot(opt->cur_dir_fd)==-1) {
348 perror("secure-mount: fchroot(\".\")");
352 if (chdir(dir)==-1) {
353 PERROR_Q("secure-mount: chdir", dir);
357 dir_fd = open(".", O_RDONLY|O_DIRECTORY);
359 perror("secure-mount: open(\".\")");
364 if (fchdir(dir_fd)==-1)
365 PERROR_Q("secure-mount: fchdir", dir);
378 mountSingle(struct MountInfo const *mnt, struct Options const *opt)
382 if (!secureChdir(mnt->dst, opt))
385 if (mnt->flag & (MS_BIND|MS_MOVE)) {
386 if (mount(mnt->src, ".",
387 mnt->type ? mnt->type : "",
388 mnt->flag, mnt->data)==-1) {
389 perror("secure-mount: mount()");
393 else if (!callExternalMount(mnt))
396 if (!opt->ignore_mtab &&
397 updateMtab(mnt, opt)==-1) {
398 WRITE_MSG(2, "Failed to update mtab-file\n");
405 static struct FstabOption const *
406 searchOption(char const *opt, size_t len)
408 struct FstabOption const * i;
409 for (i=FSTAB_OPTIONS+0; i<FSTAB_OPTIONS+DIM_OF(FSTAB_OPTIONS); ++i)
410 if (strncmp(i->opt, opt, len)==0) return i;
416 transformOptionList(struct MountInfo *info)
418 char const * ptr = info->data;
421 char const * pos = strchr(ptr, ',');
422 struct FstabOption const * opt;
424 if (pos==0) pos = ptr+strlen(ptr);
425 opt = searchOption(ptr, pos-ptr);
428 info->flag &= ~opt->mask;
429 info->flag |= opt->flag;
430 info->xflag |= opt->xflag;
438 } while (*ptr!='\0');
443 #define MOVE_TO_NEXT_FIELD(PTR,ALLOW_EOL) \
444 while (!isspace(*PTR) && *PTR!='\0') ++PTR; \
445 if (!(ALLOW_EOL) && *PTR=='\0') return prFAIL; \
447 while (isspace(*PTR)) ++PTR
449 static enum {prDOIT, prFAIL, prIGNORE}
450 parseFstabLine(struct MountInfo *info, char *buf)
452 while (isspace(*buf)) ++buf;
453 if (*buf=='#' || *buf=='\0') return prIGNORE;
456 MOVE_TO_NEXT_FIELD(buf, false);
458 MOVE_TO_NEXT_FIELD(buf, false);
460 MOVE_TO_NEXT_FIELD(buf, false);
462 MOVE_TO_NEXT_FIELD(buf, true);
464 if (strcmp(info->type, "swap")==0) return prIGNORE;
465 if (strcmp(info->type, "none")==0) info->type = 0;
469 if (!transformOptionList(info)) return prFAIL;
470 if (info->xflag & XFLAG_NOAUTO) return prIGNORE;
475 #undef MOVE_TO_NEXT_FIELD
478 mountFstab(struct Options const *opt)
484 assert(opt->fstab!=0);
485 fd = open(opt->fstab, O_RDONLY);
487 perror("secure-mount: open(<fstab>)");
491 len = lseek(fd, 0, SEEK_END);
493 lseek(fd, 0, SEEK_SET)==-1) {
494 perror("secure-mount: lseek(<fstab>)");
502 if (read(fd, buf, len+1)!=len) {
503 perror("secure-mount: read()");
506 buf[len] = '#'; // workaround for broken dietlibc strtok_r()
510 ptr = strtok_r(buf, "\n", &ptrptr);
512 struct MountInfo mnt;
513 char * new_ptr = strtok_r(0, "\n", &ptrptr);
515 switch (parseFstabLine(&mnt, ptr)) {
517 WRITE_MSG(2, "Failed to parse fstab-line beginning with '");
522 case prIGNORE : break;
525 if (!mountSingle(&mnt, opt)) {
526 WRITE_MSG(2, "Failed to mount fstab-line beginning with '");
546 initFDs(struct Options *opt)
548 opt->cur_dir_fd = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
549 opt->cur_rootdir_fd = Eopen("/", O_RDONLY|O_DIRECTORY, 0);
551 Efcntl(opt->cur_dir_fd, F_SETFD, FD_CLOEXEC);
552 Efcntl(opt->cur_rootdir_fd, F_SETFD, FD_CLOEXEC);
555 int main(int argc, char *argv[])
557 struct MountInfo mnt = {
566 struct Options opt = {
568 .fstab = "/etc/fstab",
570 .ignore_mtab = false,
577 int c = getopt_long(argc, argv, "ht:nao:", CMDLINE_OPTIONS, 0);
581 case 'h' : showHelp(1, argv[0], 0);
582 case 'v' : showVersion();
583 case 't' : mnt.type = optarg; break;
584 case 'n' : opt.ignore_mtab = true; break;
585 case 'a' : opt.mount_all = true; break;
586 case 'o' : mnt.data = optarg; break;
587 case OPTION_RBIND : mnt.flag |= MS_REC; /*@fallthrough@*/
588 case OPTION_BIND : mnt.flag |= MS_BIND; break;
589 case OPTION_MOVE : mnt.flag |= MS_MOVE; break;
590 case OPTION_MTAB : opt.mtab = optarg; break;
591 case OPTION_FSTAB : opt.fstab = optarg; break;
592 case OPTION_CHROOT: opt.do_chroot = true; break;
594 WRITE_MSG(2, "secure-mount: The '--secure' option is deprecated...\n");
597 WRITE_MSG(2, "Try '");
598 WRITE_STR(2, argv[0]);
599 WRITE_MSG(2, " --help\" for more information.\n");
606 if (opt.mount_all && optind<argc) {
607 WRITE_MSG(2, "Can not specify <src> and '-a' at the same time\n");
612 signal(SIGCHLD, SIG_DFL);
615 if (!mountFstab(&opt)) return EXIT_FAILURE;
616 else return EXIT_SUCCESS;
619 if (optind+2!=argc) {
620 WRITE_MSG(2, "Invalid <src> <dst> pair specified\n");
625 mnt.data = strdup(mnt.data);
626 if (!transformOptionList(&mnt)) {
627 WRITE_MSG(2, "Invalid options specified\n");
632 mnt.src = argv[optind++];
633 mnt.dst = argv[optind++];
635 if (!mountSingle(&mnt, &opt)) return EXIT_FAILURE;