#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
-#include "compat.h"
#include "util.h"
#include "pathconfig.h"
#include <assert.h>
#include <ctype.h>
#include <sys/wait.h>
+#include <libgen.h>
#define MNTPOINT "/etc"
#define OPTION_FSTAB 1027
#define OPTION_CHROOT 1028
#define OPTION_SECURE 1029
+#define OPTION_RBIND 1030
static struct option const
CMDLINE_OPTIONS[] = {
{ "fstab", required_argument, 0, OPTION_FSTAB },
{ "chroot", required_argument, 0, OPTION_CHROOT },
{ "secure", no_argument, 0, OPTION_SECURE },
+ { "rbind", no_argument, 0, OPTION_RBIND },
{ 0, 0, 0, 0 }
};
+#ifndef MS_REC
+# define MS_REC 0x4000
+#endif
+
static struct FstabOptions {
char const * const opt;
unsigned long const or_flag;
unsigned long const and_flag;
bool const is_dflt;
} const FSTAB_OPTIONS[] = {
+ { "rbind", MS_BIND|MS_REC, ~0, false },
{ "bind", MS_BIND, ~0, false },
{ "move", MS_MOVE, ~0, false },
#if 0
static void
showHelp(int fd, char const *cmd, int res)
{
+ VSERVER_DECLARE_CMD(cmd);
+
WRITE_MSG(fd, "Usage: ");
WRITE_STR(fd, cmd);
WRITE_MSG(fd,
- " [--help] [--version] [--bind] [--move] [-t <type>] [-n]\n"
+ " [--help] [--version] [--bind] [--move] [--rbind] [-t <type>] [-n]\n"
" [--mtab <filename>] [--fstab <filename>] [--chroot <dirname>] \n"
" [--secure] -a|([-o <options>] [--] <src> <dst>)\n\n"
"Executes mount-operations in the given chroot-dir: it assumes sources in the\n"
if (opt->rootdir!=0 &&
chroot(opt->rootdir)==-1) {
- perror("chroot()");
+ perror("secure-mount: chroot()");
return -1;
}
fd=open(opt->mtab, O_CREAT|O_APPEND|O_WRONLY, 0644);
- if (fd==-1) perror("open()");
+ if (fd==-1) perror("secure-mount: open(<mtab>)");
if (fchroot(opt->cur_rootdir_fd)==-1) {
- perror("fchroot()");
+ perror("secure-mount: fchroot()");
goto err1;
}
if (fd==-1) goto err0;
if (flock(fd, LOCK_EX)==-1) {
- perror("flock()");
+ perror("secure-mount: flock()");
goto err1;
}
writeStrX(fd, " ")==-1 ||
writeStrX(fd, mnt->data ? mnt->data : "defaults")==-1 ||
writeStrX(fd, " 0 0\n")==-1) {
- perror("write()");
+ perror("secure-mount: write()");
goto err1;
}
if (mnt->flags & MS_BIND) argv[idx++] = "--bind";
else if (mnt->flags & MS_MOVE) argv[idx++] = "--move";
- if (mnt->data) {
+ if (mnt->data &&
+ strcmp(mnt->data, "defaults")!=0) {
argv[idx++] = "-o";
argv[idx++] = mnt->data;
}
pid = fork();
if (pid==-1) {
- perror("fork()");
+ perror("secure-mount: fork()");
return false;
}
if (pid==0) {
execv(mount_prog, const_cast(char **)(argv));
- perror("execv()");
+ PERROR_Q("secure-mount: execv", mount_prog);
exit(1);
}
if (wait4(pid, &status, 0, 0)==-1) {
- perror("wait4()");
+ perror("secure-mount: wait4()");
return false;
}
if (opt->rootdir!=0) {
if (chdir(opt->rootdir)==-1) {
- perror("chdir()");
+ PERROR_Q("secure-mount: chdir", opt->rootdir);
return false;
}
if (opt->is_secure) {
if (chdirSecure(dir)==-1) {
- perror("chdirSecure()");
+ PERROR_Q("secure-mount: chdirSecure", dir);
return false;
}
}
else {
if (*dir!='\0' &&
chdir(dir)==-1) {
- perror("chdir()");
+ PERROR_Q("secure-mount: chdir", dir);
return false;
}
}
- if (mnt->flags&MS_BIND) {
+ if (mnt->flags & (MS_BIND|MS_MOVE)) {
if (mount(mnt->src, ".",
mnt->type ? mnt->type : "",
mnt->flags, mnt->data)==-1) {
- perror("mount()");
+ perror("secure-mount: mount()");
return false;
}
}
if ((mnt->flags&MS_BIND) && opt->rootdir!=0 &&
(verifyPosition(mnt->src, opt->rootdir, mnt->dst)==-1 ||
fchroot(opt->cur_rootdir_fd)==-1)) {
- perror("verifyPosition/fchroot");
+ perror("secure-mount: verifyPosition/fchroot");
// TODO: what is with unmounting?
return false;
}
parseFstabLine(struct MountInfo *info, char *buf)
{
while (isspace(*buf)) ++buf;
- if (*buf=='#') return prIGNORE;
+ if (*buf=='#' || *buf=='\0') return prIGNORE;
info->src = buf;
MOVE_TO_NEXT_FIELD(buf, false);
assert(opt->fstab!=0);
fd = open(opt->fstab, O_RDONLY);
if (fd==-1) {
- perror("open(<fstab>)");
+ perror("secure-mount: open(<fstab>)");
goto err0;
}
len = lseek(fd, 0, SEEK_END);
if (len==-1 ||
lseek(fd, 0, SEEK_SET)==-1) {
- perror("lseek(<fstab>)");
+ perror("secure-mount: lseek(<fstab>)");
goto err1;
}
{
- char buf[len+1];
+ char buf[len+2];
char *ptr, *ptrptr;
if (read(fd, buf, len+1)!=len) {
- perror("read()");
+ perror("secure-mount: read()");
goto err1;
}
- buf[len] = '\0';
+ buf[len] = '#'; // workaround for broken dietlibc strtok_r()
+ // implementation
+ buf[len+1] = '\0';
ptr = strtok_r(buf, "\n", &ptrptr);
while (ptr) {
switch (parseFstabLine(&mnt, ptr)) {
case prFAIL :
- WRITE_MSG(2, "Failed to parse/mount fstab-line beginning with '");
+ WRITE_MSG(2, "Failed to parse fstab-line beginning with '");
WRITE_STR(2, ptr);
WRITE_MSG(2, "'\n");
goto err1;
};
struct Options opt = {
- .mtab = "/etc/mtab",
- .fstab = "/etc/fstab",
- .rootdir = 0,
- .ignore_mtab = false,
- .mount_all = false,
- .is_secure = false,
-
- .cur_rootdir_fd = open("/", O_RDONLY|O_DIRECTORY)
+ .mtab = "/etc/mtab",
+ .fstab = "/etc/fstab",
+ .rootdir = 0,
+ .ignore_mtab = false,
+ .mount_all = false,
+ .is_secure = false,
+ .cur_rootdir_fd = -1
};
+ opt.cur_rootdir_fd = open("/", O_RDONLY|O_DIRECTORY);
+
if (opt.cur_rootdir_fd==-1) {
- perror("open(\"/\")");
+ perror("secure-mount: open(\"/\")");
return EXIT_FAILURE;
}
case 'n' : opt.ignore_mtab = true; break;
case 'a' : opt.mount_all = true; break;
case 'o' : mnt.data = optarg; break;
+ case OPTION_RBIND : mnt.flags |= MS_REC; /*@fallthrough@*/
case OPTION_BIND : mnt.flags |= MS_BIND; break;
case OPTION_MOVE : mnt.flags |= MS_MOVE; break;
case OPTION_MTAB : opt.mtab = optarg; break;