]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
ixfrdist: Fix brokn 'uid' and 'gid' parsing for non-numerical values 14242/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 24 May 2024 14:34:14 +0000 (16:34 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 24 May 2024 15:17:03 +0000 (17:17 +0200)
Introduced in 48d1f24b87dfd95696c0db8311c81dc622717102

pdns/ixfrdist.cc

index 9cebccded5faf871172180b5cfb3023410c73744..bbac3d6f970721a8070030e4f5c9c3f5bbd04e60 100644 (file)
@@ -1647,15 +1647,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) {
@@ -1696,15 +1697,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) {
@@ -1712,8 +1714,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);
       }
     }
@@ -1799,11 +1804,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;
@@ -1815,6 +1815,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) {