]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/unix_utility.cc
rec: ensure correct service user on debian
[thirdparty/pdns.git] / pdns / unix_utility.cc
index f06d90cb602df11353bd0350a2ebd3772600a68b..0d180cfc84450aa2b87c83b277c88fa9b3e5a257 100644 (file)
@@ -124,9 +124,9 @@ void Utility::usleep(unsigned long usec)
 
 
 // Drops the program's group privileges.
-void Utility::dropGroupPrivs( int uid, int gid )
+void Utility::dropGroupPrivs( uid_t uid, gid_t gid )
 {
-  if(gid) {
+  if(gid && gid != getegid()) {
     if(setgid(gid)<0) {
       g_log<<Logger::Critical<<"Unable to set effective group id to "<<gid<<": "<<stringerror()<<endl;
       exit(1);
@@ -152,9 +152,9 @@ void Utility::dropGroupPrivs( int uid, int gid )
 
 
 // Drops the program's user privileges.
-void Utility::dropUserPrivs( int uid )
+void Utility::dropUserPrivs( uid_t uid )
 {
-  if(uid) {
+  if(uid && uid != geteuid()) {
     if(setuid(uid)<0) {
       g_log<<Logger::Critical<<"Unable to set effective user id to "<<uid<<": "<<stringerror()<<endl;
       exit(1);
@@ -181,9 +181,9 @@ int Utility::gettimeofday( struct timeval *tv, void *tz )
 
 
 // Retrieves a gid using a groupname.
-int Utility::makeGidNumeric(const string &group)
+gid_t Utility::makeGidNumeric(const string &group)
 {
-  int newgid;
+  gid_t newgid;
   if(!(newgid=atoi(group.c_str()))) {
     errno=0;
     struct group *gr=getgrnam(group.c_str());
@@ -198,9 +198,9 @@ int Utility::makeGidNumeric(const string &group)
 
 
 // Retrieves an uid using a username.
-int Utility::makeUidNumeric(const string &username)
+uid_t Utility::makeUidNumeric(const string &username)
 {
-  int newuid;
+  uid_t newuid;
   if(!(newuid=atoi(username.c_str()))) {
     struct passwd *pw=getpwnam(username.c_str());
     if(!pw) {
@@ -212,20 +212,14 @@ int Utility::makeUidNumeric(const string &username)
   return newuid;
 }
 
-
-// Returns a random number.
-long int Utility::random( void )
-{
-  return rand();
-}
-
 // Sets the random seed.
-void Utility::srandom( unsigned int seed )
+void Utility::srandom(void)
 {
-  ::srandom(seed);
+  struct timeval tv;
+  gettimeofday(&tv, 0);
+  ::srandom(tv.tv_sec ^ tv.tv_usec ^ getpid());
 }
 
-
 // Writes a vector.
 int Utility::writev(int socket, const iovec *vector, size_t count )
 {