]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
Fixup chroot (Thanks Jakob).
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Thu, 8 Nov 2007 10:44:30 +0000 (10:44 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Thu, 8 Nov 2007 10:44:30 +0000 (10:44 +0000)
git-svn-id: file:///svn/unbound/trunk@733 be551aaa-1e26-0410-a405-d3ace91eadb9

daemon/unbound.c
doc/Changelog

index c2e541d2e7728e57a9833972cc43a02b51d1a9c8..41bb53de38c952a8a029e6610f1c2a8de9a99eeb 100644 (file)
@@ -260,6 +260,11 @@ detach(struct config_file* cfg)
 static void
 do_chroot(struct daemon* daemon, struct config_file* cfg, int debug_mode)
 {
+       uid_t uid;
+       gid_t gid;
+       /* initialize, but not to 0 (root) */
+       memset(&uid, -12, sizeof(uid));
+       memset(&gid, -12, sizeof(gid));
        log_assert(cfg);
 
        /* daemonize last to be able to print error to user */
@@ -274,13 +279,9 @@ do_chroot(struct daemon* daemon, struct config_file* cfg, int debug_mode)
                struct passwd *pwd;
                if((pwd = getpwnam(cfg->username)) == NULL)
                        fatal_exit("user '%s' does not exist.", cfg->username);
-               if(setgid(pwd->pw_gid) != 0)
-                       fatal_exit("unable to set group id: %s", strerror(errno));
-               if(setuid(pwd->pw_uid) != 0)
-                       fatal_exit("unable to set user id: %s", strerror(errno));
+               uid = pwd->pw_uid;
+               gid = pwd->pw_gid;
                endpwent();
-               verbose(VERB_DETAIL, "drop user privileges, run as %s", 
-                       cfg->username);
        }
        if(cfg->chrootdir && cfg->chrootdir[0]) {
                if(chroot(cfg->chrootdir))
@@ -288,6 +289,14 @@ do_chroot(struct daemon* daemon, struct config_file* cfg, int debug_mode)
                                cfg->chrootdir, strerror(errno));
                verbose(VERB_DETAIL, "chroot to %s", cfg->chrootdir);
        }
+       if(cfg->username && cfg->username[0]) {
+               if(setgid(gid) != 0)
+                       fatal_exit("unable to set group id: %s", strerror(errno));
+               if(setuid(uid) != 0)
+                       fatal_exit("unable to set user id: %s", strerror(errno));
+               verbose(VERB_DETAIL, "drop user privileges, run as %s", 
+                       cfg->username);
+       }
        /* check old pid file before forking */
        if(cfg->pidfile && cfg->pidfile[0]) {
                checkoldpid(cfg);
index 1f79b38a969141b0a92ddfcd0f9f30eef09c5b47..d298e295523177d4645ede13aa9a58b209d6a889 100644 (file)
@@ -1,3 +1,6 @@
+8 November 2007: Wouter
+       - Fixup chroot and drop user privileges.
+
 1 November 2007: Wouter
        - Fixup of crash on reload, due to anchors in env not NULLed after
          dealloc during deinit.