]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/fileutl.cc
DropPrivs in the solvers (just to be on the safe side)
[apt.git] / apt-pkg / contrib / fileutl.cc
index 1ba4674e583194155be2477097188fff8775e19f..da81edbcc9e54413eec1722dc993dd2f153681ae 100644 (file)
@@ -47,6 +47,7 @@
 #include <signal.h>
 #include <errno.h>
 #include <glob.h>
+#include <pwd.h>
 
 #include <set>
 #include <algorithm>
@@ -2050,3 +2051,19 @@ bool Rename(std::string From, std::string To)
    }   
    return true;
 }
+
+bool DropPrivs()
+{
+   if (getuid() != 0)
+      return true;
+
+   const std::string nobody = _config->Find("APT::User::Nobody", "nobody");
+   struct passwd *pw = getpwnam(nobody.c_str());
+   if (pw == NULL)
+      return _error->Warning("No user %s, can not drop rights", nobody.c_str());
+   if (setgid(pw->pw_gid) != 0)
+      return _error->Errno("setgid", "Failed to setgid");
+   if (setuid(pw->pw_uid) != 0)
+      return _error->Errno("setuid", "Failed to setuid");
+   return true;
+}