r23779: Change from v2 or later to v3 or later.
[gd/samba/.git] / source3 / lib / util_sec.c
index 26be27ea515b60d06ac689986cc32d4d8572b2da..0d928aad3d9a1b6f8ab57eb53c51042b9676d8c3 100644 (file)
@@ -5,7 +5,7 @@
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
@@ -52,10 +52,16 @@ static gid_t initial_gid;
 remember what uid we got started as - this allows us to run correctly
 as non-root while catching trapdoor systems
 ****************************************************************************/
+
 void sec_init(void)
 {
-       initial_uid = geteuid();
-       initial_gid = getegid();
+       static int initialized;
+
+       if (!initialized) {
+               initial_uid = geteuid();
+               initial_gid = getegid();
+               initialized = 1;
+       }
 }
 
 /****************************************************************************
@@ -192,7 +198,13 @@ void set_effective_uid(uid_t uid)
 {
 #if USE_SETRESUID
         /* Set the effective as well as the real uid. */
-       setresuid(uid,uid,-1);
+       if (setresuid(uid,uid,-1) == -1) {
+               if (errno == EAGAIN) {
+                       DEBUG(0, ("setresuid failed with EAGAIN. uid(%d) "
+                                 "might be over its NPROC limit\n",
+                                 (int)uid));
+               }
+       }
 #endif
 
 #if USE_SETREUID
@@ -252,10 +264,9 @@ void save_re_uid(void)
 /****************************************************************************
  and restore them!
 ****************************************************************************/
-void restore_re_uid(void)
-{
-       set_effective_uid(0);
 
+void restore_re_uid_fromroot(void)
+{
 #if USE_SETRESUID
        setresuid(saved_ruid, saved_euid, -1);
 #elif USE_SETREUID
@@ -274,6 +285,11 @@ void restore_re_uid(void)
        assert_uid(saved_ruid, saved_euid);
 }
 
+void restore_re_uid(void)
+{
+       set_effective_uid(0);
+       restore_re_uid_fromroot();
+}
 
 /****************************************************************************
  save the real and effective gid for later restoration. Used by the