]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
ixfrdist: Fix brokn 'uid' and 'gid' parsing for non-numerical values 14251/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 24 May 2024 14:34:14 +0000 (16:34 +0200)
committerPeter van Dijk <peter.van.dijk@powerdns.com>
Mon, 27 May 2024 11:05:54 +0000 (13:05 +0200)
Introduced in 48d1f24b87dfd95696c0db8311c81dc622717102

(cherry picked from commit f4d0470792e1c6c55ab17a74a312ed5a2fe08d2c)

pdns/ixfrdist.cc

index 1daec24b245a8aca9cc82fc206951569e443a351..ac51e5ba2be75228165991cb32b6a3bdda50dbc1 100644 (file)
@@ -1642,15 +1642,16 @@ static std::optional<IXFRDistConfiguration> parseConfiguration(int argc, char**
     }
 
     if (config["gid"].IsDefined()) {
+      bool gidParsed = false;
       auto gid = config["gid"].as<string>();
       try {
         configuration.gid = pdns::checked_stoi<gid_t>(gid);
+        gidParsed = true;
       }
       catch (const std::exception& e) {
-        g_log<<Logger::Error<<"Can not parse gid "<<gid<<endl;
-        had_error = true;
+        configuration.gid = 0;
       }
-      if (configuration.gid != 0) {
+      if (!gidParsed) {
         //NOLINTNEXTLINE(concurrency-mt-unsafe): only one thread at this point
         const struct group *gr = getgrnam(gid.c_str());
         if (gr == nullptr) {
@@ -1691,15 +1692,16 @@ static std::optional<IXFRDistConfiguration> parseConfiguration(int argc, char**
     }
 
     if (config["uid"].IsDefined()) {
+      bool uidParsed = false;
       auto uid = config["uid"].as<string>();
       try {
         configuration.uid = pdns::checked_stoi<uid_t>(uid);
+        uidParsed = true;
       }
       catch (const std::exception& e) {
-        g_log<<Logger::Error<<"Can not parse uid "<<uid<<endl;
-        had_error = true;
+        configuration.uid = 0;
       }
-      if (configuration.uid != 0) {
+      if (!uidParsed) {
         //NOLINTNEXTLINE(concurrency-mt-unsafe): only one thread at this point
         const struct passwd *pw = getpwnam(uid.c_str());
         if (pw == nullptr) {
@@ -1707,8 +1709,11 @@ static std::optional<IXFRDistConfiguration> parseConfiguration(int argc, char**
           had_error = true;
         } else {
           configuration.uid = pw->pw_uid;
+          uidParsed = true;
         }
         //NOLINTNEXTLINE(concurrency-mt-unsafe): only one thread at this point
+      }
+      if (uidParsed) {
         configuration.userInfo = getpwuid(configuration.uid);
       }
     }
@@ -1794,11 +1799,6 @@ int main(int argc, char** argv) {
     }
 
     if (configuration->uid != 0) {
-      g_log<<Logger::Notice<<"Dropping effective user-id to "<<configuration->uid<<endl;
-      if (setuid(configuration->uid) < 0) {
-        g_log<<Logger::Error<<"Could not set user id to "<<configuration->uid<<": "<<stringerror()<<endl;
-        had_error = true;
-      }
       if (configuration->userInfo == nullptr) {
         if (setgroups(0, nullptr) < 0) {
           g_log<<Logger::Error<<"Unable to drop supplementary gids: "<<stringerror()<<endl;
@@ -1810,6 +1810,12 @@ int main(int argc, char** argv) {
           had_error = true;
         }
       }
+
+      g_log<<Logger::Notice<<"Dropping effective user-id to "<<configuration->uid<<endl;
+      if (setuid(configuration->uid) < 0) {
+        g_log<<Logger::Error<<"Could not set user id to "<<configuration->uid<<": "<<stringerror()<<endl;
+        had_error = true;
+      }
     }
 
     if (had_error) {