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>
44 #include <sys/mount.h>
46 #include <sys/types.h>
55 #define ENSC_WRAPPERS_FCNTL 1
58 #define MNTPOINT "/etc"
80 #define OPTION_BIND 1024
81 #define OPTION_MOVE 1025
82 #define OPTION_MTAB 1026
83 #define OPTION_FSTAB 1027
84 #define OPTION_CHROOT 1028
85 #define OPTION_SECURE 1029
86 #define OPTION_RBIND 1030
88 #define XFLAG_NOAUTO 0x01
90 static struct option const
92 { "help", no_argument, 0, 'h' },
93 { "version", no_argument, 0, 'v' },
94 { "bind", no_argument, 0, OPTION_BIND },
95 { "move", no_argument, 0, OPTION_MOVE },
96 { "mtab", required_argument, 0, OPTION_MTAB },
97 { "fstab", required_argument, 0, OPTION_FSTAB },
98 { "chroot", no_argument, 0, OPTION_CHROOT },
99 { "secure", no_argument, 0, OPTION_SECURE },
100 { "rbind", no_argument, 0, OPTION_RBIND },
105 # define MS_REC 0x4000
108 static struct FstabOption {
109 char const * const opt;
110 unsigned long const flag;
111 unsigned long const mask;
112 unsigned long const xflag;
114 } const FSTAB_OPTIONS[] = {
115 { "defaults", 0, (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|
116 MS_SYNCHRONOUS), 0, false },
117 { "rbind", MS_BIND|MS_REC, MS_BIND|MS_REC, 0, false },
118 { "bind", MS_BIND, MS_BIND, 0, false },
119 { "move", MS_MOVE, MS_MOVE, 0, false },
120 { "async", 0, MS_SYNCHRONOUS, 0, false },
121 { "sync", MS_SYNCHRONOUS, MS_SYNCHRONOUS, 0, false },
122 { "atime", 0, MS_NOATIME, 0, false },
123 { "noatime", MS_NOATIME, MS_NOATIME, 0, false },
124 { "dev", 0, MS_NODEV, 0, false },
125 { "nodev", MS_NODEV, MS_NODEV, 0, false },
126 { "exec", 0, MS_NOEXEC, 0, false },
127 { "noexec", MS_NOEXEC, MS_NOEXEC, 0, false },
128 { "suid", 0, MS_NOSUID, 0, false },
129 { "nosuid", MS_NOSUID, MS_NOSUID, 0, false },
130 { "ro", MS_RDONLY, MS_RDONLY, 0, false },
131 { "rw", 0, MS_RDONLY, 0, false },
133 { "remount", MS_REMOUNT, MS_REMOUNT, 0, false },
134 { "users", MS_NOEXEC|MS_NOSUID|MS_NODEV,
135 MS_NOEXEC|MS_NOSUID|MS_NODEV, 0, false },
136 { "mandlock", MS_MANDLOCK, MS_MANDLOCK, 0, false },
137 { "nodiratime", MS_NODIRATIME, MS_NODIRATIME, 0, false },
139 { "dirsync", MS_DIRSYNC, MS_DIRSYNC, 0, false },
141 { "_netdev", 0, 0, 0, false },
142 { "auto", 0, 0, 0, false },
143 { "noauto", 0, 0, XFLAG_NOAUTO, false },
144 { "user", 0, 0, 0, false },
145 { "nouser", 0, 0, 0, false },
148 int wrapper_exit_code = 1;
151 showHelp(int fd, char const *cmd, int res)
153 VSERVER_DECLARE_CMD(cmd);
155 WRITE_MSG(fd, "Usage: ");
158 " [--help] [--version] [--bind] [--move] [--rbind] [-t <type>] [-n]\n"
159 " [--mtab <filename>] [--fstab <filename>] [--chroot] \n"
160 " -a|([-o <options>] [--] <src> <dst>)\n\n"
161 "Executes mount-operations under the current directory: it assumes sources in\n"
162 "the current root-dir while destinations are expected in the chroot environment.\n\n"
163 "For non-trivial mount-operations it uses the external 'mount' program which\n"
164 "can be overridden by the $MOUNT environment variable.\n\n"
166 " --bind|move|rbind ... set the correspond flags; with this options\n"
167 " the mount will be executed internally without\n"
168 " calling an external mount program.\n"
169 " -t <type> ... assume the given filesystem type\n"
170 " -o <options> ... set additional options; see mount(2) for details\n"
171 " -n ... do not update the mtab-file\n"
172 " --mtab <filename> ... use <filename> as an alternative mtab file\n"
173 " [default: /etc/mtab]\n"
174 " --chroot ... chroot into the current directory before\n"
175 " mounting the filesystem\n"
176 " --fstab <filename> ... use <filename> as an alternative fstab file;\n"
177 " this option has an effect only with the '-a'\n"
178 " option [default: /etc/fstab]\n"
179 " -a ... mount everything listed in the fstab-file\n\n"
180 " <src> ... the source-filesystem; this path is absolute\n"
181 " to the current root-filesystem. Only valid\n"
182 " without the '-a' option.\n"
183 " <dst> ... the destination mount-point; when used with\n"
184 " '--chroot', this path is relative to the current\n"
185 " directory. Only valid without the '-a' option\n\n"
186 "Please report bugs to " PACKAGE_BUGREPORT "\n");
195 "secure-mount " VERSION " -- secure mounting of directories\n"
196 "This program is part of " PACKAGE_STRING "\n\n"
197 "Copyright (C) 2003 Enrico Scholz\n"
198 VERSION_COPYRIGHT_DISCLAIMER);
203 isSameObject(struct stat const *lhs,
204 struct stat const *rhs)
206 return (lhs->st_dev==rhs->st_dev &&
207 lhs->st_ino==rhs->st_ino);
213 if (fchdir(fd)==-1 || chroot(".")==-1) return -1;
218 writeX(int fd, void const *buf, size_t len)
220 if ((size_t)(write(fd, buf, len))!=len) return -1;
225 writeStrX(int fd, char const *str)
227 return writeX(fd, str, strlen(str));
230 static inline char const *
231 getType(struct MountInfo const *mnt)
233 if (mnt->type==0) return "none";
234 else if (strncmp(mnt->type, "ext", 3)==0) return "ufs";
235 else return mnt->type;
239 restoreRoot(struct Options const *opt)
241 if (opt->do_chroot!=0 && fchroot(opt->cur_rootdir_fd)==-1) {
242 perror("secure-mount: fchdir(\"/\")");
243 WRITE_MSG(2, "Failed to restore root-directory; aborting\n");
249 updateMtab(struct MountInfo const *mnt, struct Options const *opt)
253 assert(opt->mtab!=0);
255 if (opt->do_chroot && fchroot(opt->cur_dir_fd)==-1) {
256 perror("secure-mount: fchroot(\".\")");
260 fd=open(opt->mtab, O_CREAT|O_APPEND|O_WRONLY, 0644);
263 perror("secure-mount: open(<mtab>)");
267 if (flock(fd, LOCK_EX)==-1) {
268 perror("secure-mount: flock()");
272 if (writeStrX(fd, mnt->src)==-1 ||
273 writeStrX(fd, " ")==-1 ||
274 writeStrX(fd, mnt->dst)==-1 ||
275 writeStrX(fd, " ")==-1 ||
276 writeStrX(fd, getType(mnt))==-1 ||
277 writeStrX(fd, " ")==-1 ||
278 writeStrX(fd, mnt->data ? mnt->data : "defaults")==-1 ||
279 writeStrX(fd, " 0 0\n")==-1) {
280 perror("secure-mount: write()");
293 callExternalMount(struct MountInfo const *mnt)
295 char const * argv[10];
299 char const * mount_prog = getenv("MOUNT");
301 if (mount_prog==0) mount_prog = MOUNT_PROG;
303 argv[idx++] = mount_prog;
305 if (mnt->flag & MS_BIND) argv[idx++] = "--bind";
306 else if (mnt->flag & MS_MOVE) argv[idx++] = "--move";
309 strcmp(mnt->data, "defaults")!=0) {
311 argv[idx++] = mnt->data;
316 argv[idx++] = mnt->type;
319 argv[idx++] = mnt->src;
325 perror("secure-mount: fork()");
330 execv(mount_prog, const_cast(char **)(argv));
331 PERROR_Q("secure-mount: execv", mount_prog);
335 if (wait4(pid, &status, 0, 0)==-1) {
336 perror("secure-mount: wait4()");
340 return (WIFEXITED(status)) && (WEXITSTATUS(status)==0);
344 secureChdir(char const *dir, struct Options const *opt)
349 if (opt->do_chroot!=0 && fchroot(opt->cur_dir_fd)==-1) {
350 perror("secure-mount: fchroot(\".\")");
354 if (chdir(dir)==-1) {
355 PERROR_Q("secure-mount: chdir", dir);
359 dir_fd = open(".", O_RDONLY|O_DIRECTORY);
361 perror("secure-mount: open(\".\")");
366 if (fchdir(dir_fd)==-1)
367 PERROR_Q("secure-mount: fchdir", dir);
380 mountSingle(struct MountInfo const *mnt, struct Options const *opt)
384 if (!secureChdir(mnt->dst, opt))
387 if (mnt->flag & (MS_BIND|MS_MOVE)) {
388 if (mount(mnt->src, ".",
389 mnt->type ? mnt->type : "",
390 mnt->flag, mnt->data)==-1) {
391 perror("secure-mount: mount()");
395 else if (!callExternalMount(mnt))
398 if (!opt->ignore_mtab &&
399 updateMtab(mnt, opt)==-1) {
400 WRITE_MSG(2, "Failed to update mtab-file\n");
407 static struct FstabOption const *
408 searchOption(char const *opt, size_t len)
410 struct FstabOption const * i;
411 for (i=FSTAB_OPTIONS+0; i<FSTAB_OPTIONS+DIM_OF(FSTAB_OPTIONS); ++i)
412 if (strncmp(i->opt, opt, len)==0) return i;
418 transformOptionList(struct MountInfo *info, size_t UNUSED *col)
420 char const * ptr = info->data;
423 char const * pos = strchr(ptr, ',');
424 struct FstabOption const * opt;
426 if (pos==0) pos = ptr+strlen(ptr);
427 opt = searchOption(ptr, pos-ptr);
430 info->flag &= ~opt->mask;
431 info->flag |= opt->flag;
432 info->xflag |= opt->xflag;
440 } while (*ptr!='\0');
445 #define MOVE_TO_NEXT_FIELD(PTR,ALLOW_EOL) \
446 while (!isspace(*PTR) && *PTR!='\0') ++PTR; \
447 if (col) *col = buf-start_buf+1; \
448 if (!(ALLOW_EOL) && *PTR=='\0') return prFAIL; \
450 while (isspace(*PTR)) ++PTR
452 static enum {prDOIT, prFAIL, prIGNORE}
453 parseFstabLine(struct MountInfo *info, char *buf, size_t *col)
455 char const * const start_buf = buf;
458 while (isspace(*buf)) ++buf;
459 if (*buf=='#' || *buf=='\0') return prIGNORE;
462 MOVE_TO_NEXT_FIELD(buf, false);
464 MOVE_TO_NEXT_FIELD(buf, false);
466 MOVE_TO_NEXT_FIELD(buf, false);
467 err_col = buf-start_buf+1;
469 MOVE_TO_NEXT_FIELD(buf, true);
471 if (strcmp(info->type, "swap")==0) return prIGNORE;
472 if (strcmp(info->type, "none")==0) info->type = 0;
476 if (col) *col = err_col;
477 if (!transformOptionList(info,col)) return prFAIL;
478 if (info->xflag & XFLAG_NOAUTO) return prIGNORE;
483 #undef MOVE_TO_NEXT_FIELD
486 showFstabPosition(int fd, char const *fname, size_t line_nr, size_t col_nr)
488 char buf[3*sizeof(line_nr)*2 + 4];
489 size_t len = utilvserver_fmt_uint(buf+1, line_nr)+1;
493 len += utilvserver_fmt_uint(buf+len, col_nr);
494 WRITE_STR(fd, fname);
500 mountFstab(struct Options const *opt)
506 assert(opt->fstab!=0);
507 fd = open(opt->fstab, O_RDONLY);
509 perror("secure-mount: open(<fstab>)");
513 len = lseek(fd, 0, SEEK_END);
515 lseek(fd, 0, SEEK_SET)==-1) {
516 perror("secure-mount: lseek(<fstab>)");
523 size_t line_nr=0, col_nr;
525 if (read(fd, buf, len+1)!=len) {
526 perror("secure-mount: read()");
529 buf[len] = '#'; // workaround for broken dietlibc strtok_r()
534 while ((ptr=strsep(&ptrptr, "\n")) != 0) {
535 struct MountInfo mnt;
538 switch (parseFstabLine(&mnt, ptr, &col_nr)) {
540 showFstabPosition(2, opt->fstab, line_nr, col_nr);
541 WRITE_MSG(2, ": syntax error\n");
544 case prIGNORE : break;
547 if (!mountSingle(&mnt, opt)) {
548 showFstabPosition(2, opt->fstab, line_nr, 1);
549 WRITE_MSG(2, ": failed to mount fstab-entry\n");
565 initFDs(struct Options *opt)
567 opt->cur_dir_fd = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
568 opt->cur_rootdir_fd = Eopen("/", O_RDONLY|O_DIRECTORY, 0);
570 Efcntl(opt->cur_dir_fd, F_SETFD, FD_CLOEXEC);
571 Efcntl(opt->cur_rootdir_fd, F_SETFD, FD_CLOEXEC);
574 int main(int argc, char *argv[])
576 struct MountInfo mnt = {
585 struct Options opt = {
587 .fstab = "/etc/fstab",
589 .ignore_mtab = false,
596 int c = getopt_long(argc, argv, "ht:nao:", CMDLINE_OPTIONS, 0);
600 case 'h' : showHelp(1, argv[0], 0);
601 case 'v' : showVersion();
602 case 't' : mnt.type = optarg; break;
603 case 'n' : opt.ignore_mtab = true; break;
604 case 'a' : opt.mount_all = true; break;
605 case 'o' : mnt.data = optarg; break;
606 case OPTION_RBIND : mnt.flag |= MS_REC; /*@fallthrough@*/
607 case OPTION_BIND : mnt.flag |= MS_BIND; break;
608 case OPTION_MOVE : mnt.flag |= MS_MOVE; break;
609 case OPTION_MTAB : opt.mtab = optarg; break;
610 case OPTION_FSTAB : opt.fstab = optarg; break;
611 case OPTION_CHROOT: opt.do_chroot = true; break;
613 WRITE_MSG(2, "secure-mount: The '--secure' option is deprecated...\n");
616 WRITE_MSG(2, "Try '");
617 WRITE_STR(2, argv[0]);
618 WRITE_MSG(2, " --help\" for more information.\n");
625 if (opt.mount_all && optind<argc) {
626 WRITE_MSG(2, "Can not specify <src> and '-a' at the same time\n");
631 signal(SIGCHLD, SIG_DFL);
634 if (!mountFstab(&opt)) return EXIT_FAILURE;
635 else return EXIT_SUCCESS;
638 if (optind+2!=argc) {
639 WRITE_MSG(2, "Invalid <src> <dst> pair specified\n");
644 mnt.data = strdup(mnt.data);
645 if (!transformOptionList(&mnt, 0)) {
646 WRITE_MSG(2, "Invalid options specified\n");
651 mnt.src = argv[optind++];
652 mnt.dst = argv[optind++];
654 if (!mountSingle(&mnt, &opt)) return EXIT_FAILURE;