X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=util-vserver%2Fsrc%2Fsecure-mount.c;h=81fee1fd6bdc1b8042c325bf66edd68d10f71ed6;hb=1171042cfba2870d05f6dcc771f6331fb5c9ad78;hp=9f5c05ce8c00bbee578740e674bb1173eb9244ee;hpb=604c7e22a69f1e6c2e206304ef01b15bc8ab0774;p=util-vserver.git diff --git a/util-vserver/src/secure-mount.c b/util-vserver/src/secure-mount.c index 9f5c05c..81fee1f 100644 --- a/util-vserver/src/secure-mount.c +++ b/util-vserver/src/secure-mount.c @@ -28,7 +28,6 @@ #ifdef HAVE_CONFIG_H # include #endif -#include "compat.h" #include "util.h" #include "pathconfig.h" @@ -49,6 +48,7 @@ #include #include #include +#include #define MNTPOINT "/etc" @@ -78,6 +78,7 @@ struct Options { #define OPTION_FSTAB 1027 #define OPTION_CHROOT 1028 #define OPTION_SECURE 1029 +#define OPTION_RBIND 1030 static struct option const CMDLINE_OPTIONS[] = { @@ -89,6 +90,7 @@ 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 } }; @@ -120,10 +122,12 @@ static struct FstabOptions { 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 ] [-n]\n" + " [--help] [--version] [--bind] [--move] [--rbind] [-t ] [-n]\n" " [--mtab ] [--fstab ] [--chroot ] \n" " [--secure] -a|([-o ] [--] )\n\n" "Executes mount-operations in the given chroot-dir: it assumes sources in the\n" @@ -232,7 +236,7 @@ fchroot(int fd) static int writeX(int fd, void const *buf, size_t len) { - if (write(fd, buf, len)!=len) return -1; + if ((size_t)(write(fd, buf, len))!=len) return -1; return 0; } @@ -306,7 +310,8 @@ callExternalMount(struct MountInfo const *mnt) 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; } @@ -442,10 +447,10 @@ transformOptionList(struct MountInfo *info) while (isspace(*PTR)) ++PTR static enum {prDOIT, prFAIL, prIGNORE} -parseFstabLine(struct MountInfo *info, char *buf, struct Options const *opt) +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); @@ -491,23 +496,25 @@ mountFstab(struct Options const *opt) } { - char buf[len+1]; + char buf[len+2]; char *ptr, *ptrptr; if (read(fd, buf, len+1)!=len) { perror("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) { struct MountInfo mnt; char * new_ptr = strtok_r(0, "\n", &ptrptr); - switch (parseFstabLine(&mnt, ptr, opt)) { + 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; @@ -547,16 +554,17 @@ int main(int argc, char *argv[]) }; 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(\"/\")"); return EXIT_FAILURE; @@ -567,12 +575,13 @@ int main(int argc, char *argv[]) if (c==-1) break; switch (c) { - case 'h' : showHelp(2, argv[0], 0); + case 'h' : showHelp(1, argv[0], 0); case 'v' : showVersion(); case 't' : mnt.type = optarg; break; 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;