jail: create resolv.conf symlink for netns jails
authorDaniel Golle <daniel@makrotopia.org>
Fri, 3 Jan 2020 10:29:17 +0000 (12:29 +0200)
committerDaniel Golle <daniel@makrotopia.org>
Tue, 21 Jan 2020 10:45:26 +0000 (12:45 +0200)
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
jail/jail.c

index 2d23ad22ddceebd8e082ec8901116579bf8ed27c..9b8d1a91b2152b7a21031349f55962727a0d5a99 100644 (file)
@@ -96,7 +96,7 @@ static int mkdir_p(char *dir, mode_t mask)
        return ret;
 }
 
-int mount_bind(const char *root, const char *path, int readonly, int error)
+static int _mount_bind(const char *root, const char *path, const char *target, int readonly, int error)
 {
        struct stat s;
        char new[PATH_MAX];
@@ -107,12 +107,13 @@ int mount_bind(const char *root, const char *path, int readonly, int error)
                return error;
        }
 
-       snprintf(new, sizeof(new), "%s%s", root, path);
+       snprintf(new, sizeof(new), "%s%s", root, target?target:path);
+
        if (S_ISDIR(s.st_mode)) {
                mkdir_p(new, 0755);
        } else {
                mkdir_p(dirname(new), 0755);
-               snprintf(new, sizeof(new), "%s%s", root, path);
+               snprintf(new, sizeof(new), "%s%s", root, target?target:path);
                fd = creat(new, 0644);
                if (fd == -1) {
                        ERROR("creat(%s) failed: %m\n", new);
@@ -136,6 +137,10 @@ int mount_bind(const char *root, const char *path, int readonly, int error)
        return 0;
 }
 
+int mount_bind(const char *root, const char *path, int readonly, int error) {
+       return _mount_bind(root, path, NULL, readonly, error);
+}
+
 static int build_jail_fs(void)
 {
        char jail_root[] = "/tmp/ujail-XXXXXX";
@@ -165,6 +170,18 @@ static int build_jail_fs(void)
                return -1;
        }
 
+       if (opts.namespace & NAMESPACE_NET) {
+               char hostdir[PATH_MAX], jailetc[PATH_MAX], jaillink[PATH_MAX];
+
+               snprintf(hostdir, PATH_MAX, "/tmp/resolv.conf-%s.d", opts.name);
+               mkdir_p(hostdir, 0755);
+               _mount_bind(jail_root, hostdir, "/tmp/resolv.conf.d", 1, -1);
+               snprintf(jailetc, PATH_MAX, "%s/etc", jail_root);
+               mkdir_p(jailetc, 0755);
+               snprintf(jaillink, PATH_MAX, "%s/etc/resolv.conf", jail_root);
+               symlink("../tmp/resolv.conf.d/resolv.conf.auto", jaillink);
+       }
+
        char dirbuf[sizeof(jail_root) + 4];
        snprintf(dirbuf, sizeof(dirbuf), "%s/old", jail_root);
        mkdir(dirbuf, 0755);