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"
163 "Please report bugs to " PACKAGE_BUGREPORT "\n");
172 "secure-mount " VERSION " -- secure mounting of directories\n"
173 "This program is part of " PACKAGE_STRING "\n\n"
174 "Copyright (C) 2003 Enrico Scholz\n"
175 VERSION_COPYRIGHT_DISCLAIMER);
180 isSameObject(struct stat const *lhs,
181 struct stat const *rhs)
183 return (lhs->st_dev==rhs->st_dev &&
184 lhs->st_ino==rhs->st_ino);
190 if (fchdir(fd)==-1 || chroot(".")==-1) return -1;
195 writeX(int fd, void const *buf, size_t len)
197 if ((size_t)(write(fd, buf, len))!=len) return -1;
202 writeStrX(int fd, char const *str)
204 return writeX(fd, str, strlen(str));
207 static inline char const *
208 getType(struct MountInfo const *mnt)
210 if (mnt->type==0) return "none";
211 else if (strncmp(mnt->type, "ext", 3)==0) return "ufs";
212 else return mnt->type;
216 restoreRoot(struct Options const *opt)
218 if (opt->do_chroot!=0 && fchroot(opt->cur_rootdir_fd)==-1) {
219 perror("secure-mount: fchdir(\"/\")");
220 WRITE_MSG(2, "Failed to restore root-directory; aborting\n");
226 updateMtab(struct MountInfo const *mnt, struct Options const *opt)
230 assert(opt->mtab!=0);
232 if (opt->do_chroot && fchroot(opt->cur_dir_fd)==-1) {
233 perror("secure-mount: fchroot(\".\")");
237 fd=open(opt->mtab, O_CREAT|O_APPEND|O_WRONLY, 0644);
240 perror("secure-mount: open(<mtab>)");
244 if (flock(fd, LOCK_EX)==-1) {
245 perror("secure-mount: flock()");
249 if (writeStrX(fd, mnt->src)==-1 ||
250 writeStrX(fd, " ")==-1 ||
251 writeStrX(fd, mnt->dst)==-1 ||
252 writeStrX(fd, " ")==-1 ||
253 writeStrX(fd, getType(mnt))==-1 ||
254 writeStrX(fd, " ")==-1 ||
255 writeStrX(fd, mnt->data ? mnt->data : "defaults")==-1 ||
256 writeStrX(fd, " 0 0\n")==-1) {
257 perror("secure-mount: write()");
270 callExternalMount(struct MountInfo const *mnt)
272 char const * argv[10];
276 char const * mount_prog = getenv("MOUNT");
278 if (mount_prog==0) mount_prog = MOUNT_PROG;
280 argv[idx++] = mount_prog;
282 if (mnt->flag & MS_BIND) argv[idx++] = "--bind";
283 else if (mnt->flag & MS_MOVE) argv[idx++] = "--move";
286 strcmp(mnt->data, "defaults")!=0) {
288 argv[idx++] = mnt->data;
293 argv[idx++] = mnt->type;
296 argv[idx++] = mnt->src;
302 perror("secure-mount: fork()");
307 execv(mount_prog, const_cast(char **)(argv));
308 PERROR_Q("secure-mount: execv", mount_prog);
312 if (wait4(pid, &status, 0, 0)==-1) {
313 perror("secure-mount: wait4()");
317 return (WIFEXITED(status)) && (WEXITSTATUS(status)==0);
321 secureChdir(char const *dir, struct Options const *opt)
326 if (opt->do_chroot!=0 && fchroot(opt->cur_dir_fd)==-1) {
327 perror("secure-mount: fchroot(\".\")");
331 if (chdir(dir)==-1) {
332 PERROR_Q("secure-mount: chdir", dir);
336 dir_fd = open(".", O_RDONLY|O_DIRECTORY);
338 perror("secure-mount: open(\".\")");
343 if (fchdir(dir_fd)==-1)
344 PERROR_Q("secure-mount: fchdir", dir);
357 mountSingle(struct MountInfo const *mnt, struct Options const *opt)
361 if (!secureChdir(mnt->dst, opt))
364 if (mnt->flag & (MS_BIND|MS_MOVE)) {
365 if (mount(mnt->src, ".",
366 mnt->type ? mnt->type : "",
367 mnt->flag, mnt->data)==-1) {
368 perror("secure-mount: mount()");
372 else if (!callExternalMount(mnt))
375 if (!opt->ignore_mtab &&
376 updateMtab(mnt, opt)==-1) {
377 WRITE_MSG(2, "Failed to update mtab-file\n");
384 static struct FstabOption const *
385 searchOption(char const *opt, size_t len)
387 struct FstabOption const * i;
388 for (i=FSTAB_OPTIONS+0; i<FSTAB_OPTIONS+DIM_OF(FSTAB_OPTIONS); ++i)
389 if (strncmp(i->opt, opt, len)==0) return i;
395 transformOptionList(struct MountInfo *info)
397 char const * ptr = info->data;
400 char const * pos = strchr(ptr, ',');
401 struct FstabOption const * opt;
403 if (pos==0) pos = ptr+strlen(ptr);
404 opt = searchOption(ptr, pos-ptr);
407 info->flag &= ~opt->mask;
408 info->flag |= opt->flag;
409 info->xflag |= opt->xflag;
417 } while (*ptr!='\0');
422 #define MOVE_TO_NEXT_FIELD(PTR,ALLOW_EOL) \
423 while (!isspace(*PTR) && *PTR!='\0') ++PTR; \
424 if (!(ALLOW_EOL) && *PTR=='\0') return prFAIL; \
426 while (isspace(*PTR)) ++PTR
428 static enum {prDOIT, prFAIL, prIGNORE}
429 parseFstabLine(struct MountInfo *info, char *buf)
431 while (isspace(*buf)) ++buf;
432 if (*buf=='#' || *buf=='\0') return prIGNORE;
435 MOVE_TO_NEXT_FIELD(buf, false);
437 MOVE_TO_NEXT_FIELD(buf, false);
439 MOVE_TO_NEXT_FIELD(buf, false);
441 MOVE_TO_NEXT_FIELD(buf, true);
443 if (strcmp(info->type, "swap")==0) return prIGNORE;
444 if (strcmp(info->type, "none")==0) info->type = 0;
448 if (!transformOptionList(info)) return prFAIL;
449 if (info->xflag & XFLAG_NOAUTO) return prIGNORE;
454 #undef MOVE_TO_NEXT_FIELD
457 mountFstab(struct Options const *opt)
463 assert(opt->fstab!=0);
464 fd = open(opt->fstab, O_RDONLY);
466 perror("secure-mount: open(<fstab>)");
470 len = lseek(fd, 0, SEEK_END);
472 lseek(fd, 0, SEEK_SET)==-1) {
473 perror("secure-mount: lseek(<fstab>)");
481 if (read(fd, buf, len+1)!=len) {
482 perror("secure-mount: read()");
485 buf[len] = '#'; // workaround for broken dietlibc strtok_r()
489 ptr = strtok_r(buf, "\n", &ptrptr);
491 struct MountInfo mnt;
492 char * new_ptr = strtok_r(0, "\n", &ptrptr);
494 switch (parseFstabLine(&mnt, ptr)) {
496 WRITE_MSG(2, "Failed to parse fstab-line beginning with '");
501 case prIGNORE : break;
504 if (!mountSingle(&mnt, opt)) {
505 WRITE_MSG(2, "Failed to mount fstab-line beginning with '");
525 initFDs(struct Options *opt)
527 opt->cur_dir_fd = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
528 opt->cur_rootdir_fd = Eopen("/", O_RDONLY|O_DIRECTORY, 0);
530 Efcntl(opt->cur_dir_fd, F_SETFD, FD_CLOEXEC);
531 Efcntl(opt->cur_rootdir_fd, F_SETFD, FD_CLOEXEC);
534 int main(int argc, char *argv[])
536 struct MountInfo mnt = {
545 struct Options opt = {
547 .fstab = "/etc/fstab",
549 .ignore_mtab = false,
556 int c = getopt_long(argc, argv, "ht:nao:", CMDLINE_OPTIONS, 0);
560 case 'h' : showHelp(1, argv[0], 0);
561 case 'v' : showVersion();
562 case 't' : mnt.type = optarg; break;
563 case 'n' : opt.ignore_mtab = true; break;
564 case 'a' : opt.mount_all = true; break;
565 case 'o' : mnt.data = optarg; break;
566 case OPTION_RBIND : mnt.flag |= MS_REC; /*@fallthrough@*/
567 case OPTION_BIND : mnt.flag |= MS_BIND; break;
568 case OPTION_MOVE : mnt.flag |= MS_MOVE; break;
569 case OPTION_MTAB : opt.mtab = optarg; break;
570 case OPTION_FSTAB : opt.fstab = optarg; break;
571 case OPTION_CHROOT: opt.do_chroot = true; break;
573 WRITE_MSG(2, "secure-mount: The '--secure' option is deprecated...\n");
576 WRITE_MSG(2, "Try '");
577 WRITE_STR(2, argv[0]);
578 WRITE_MSG(2, " --help\" for more information.\n");
585 if (opt.mount_all && optind<argc) {
586 WRITE_MSG(2, "Can not specify <src> and '-a' at the same time\n");
591 signal(SIGCHLD, SIG_DFL);
594 if (!mountFstab(&opt)) return EXIT_FAILURE;
595 else return EXIT_SUCCESS;
598 if (optind+2!=argc) {
599 WRITE_MSG(2, "Invalid <src> <dst> pair specified\n");
604 mnt.data = strdup(mnt.data);
605 if (!transformOptionList(&mnt)) {
606 WRITE_MSG(2, "Invalid options specified\n");
611 mnt.src = argv[optind++];
612 mnt.dst = argv[optind++];
614 if (!mountSingle(&mnt, &opt)) return EXIT_FAILURE;