r907: fixing browse.dat bug -- don't include the resouce byte from the netbios name...
[jra/samba/.git] / source3 / lib / util_sec.c
index f79da8d2e4eb55dc1d59b89eadd85e43f3c2babd..26be27ea515b60d06ac689986cc32d4d8572b2da 100644 (file)
@@ -48,8 +48,6 @@
 static uid_t initial_uid;
 static gid_t initial_gid;
 
-static BOOL sec_initialised;
-
 /****************************************************************************
 remember what uid we got started as - this allows us to run correctly
 as non-root while catching trapdoor systems
@@ -58,8 +56,6 @@ void sec_init(void)
 {
        initial_uid = geteuid();
        initial_gid = getegid();
-
-       sec_initialised = True;
 }
 
 /****************************************************************************
@@ -67,9 +63,6 @@ some code (eg. winbindd) needs to know what uid we started as
 ****************************************************************************/
 uid_t sec_initial_uid(void)
 {
-       if (!sec_initialised)
-               smb_panic("sec_initial_uid() called before sec_init()\n");
-
        return initial_uid;
 }
 
@@ -78,9 +71,6 @@ some code (eg. winbindd, profiling shm) needs to know what gid we started as
 ****************************************************************************/
 gid_t sec_initial_gid(void)
 {
-       if (!sec_initialised)
-               smb_panic("sec_initial_gid() called before sec_init()\n");
-
        return initial_gid;
 }
 
@@ -89,9 +79,6 @@ are we running in non-root mode?
 ****************************************************************************/
 BOOL non_root_mode(void)
 {
-       if (!sec_initialised)
-               smb_panic("non_root_mode() called before sec_init()\n");
-
        return (initial_uid != (uid_t)0);
 }
 
@@ -190,13 +177,22 @@ void gain_root_group_privilege(void)
 
 
 /****************************************************************************
- Set *only* the effective uid.
- we want to end up with ruid==0 and euid==uid
+ Set effective uid, and possibly the real uid too.
+ We want to end up with either:
+  
+   ruid==uid and euid==uid
+
+ or
+
+   ruid==0 and euid==uid
+
+ depending on what the local OS will allow us to regain root from.
 ****************************************************************************/
 void set_effective_uid(uid_t uid)
 {
 #if USE_SETRESUID
-       setresuid(-1,uid,-1);
+        /* Set the effective as well as the real uid. */
+       setresuid(uid,uid,-1);
 #endif
 
 #if USE_SETREUID
@@ -240,6 +236,7 @@ void set_effective_gid(gid_t gid)
 }
 
 static uid_t saved_euid, saved_ruid;
+static gid_t saved_egid, saved_rgid;
 
 /****************************************************************************
  save the real and effective uid for later restoration. Used by the quotas
@@ -277,6 +274,41 @@ void restore_re_uid(void)
        assert_uid(saved_ruid, saved_euid);
 }
 
+
+/****************************************************************************
+ save the real and effective gid for later restoration. Used by the 
+ getgroups code
+****************************************************************************/
+void save_re_gid(void)
+{
+       saved_rgid = getgid();
+       saved_egid = getegid();
+}
+
+/****************************************************************************
+ and restore them!
+****************************************************************************/
+void restore_re_gid(void)
+{
+#if USE_SETRESUID
+       setresgid(saved_rgid, saved_egid, -1);
+#elif USE_SETREUID
+       setregid(saved_rgid, -1);
+       setregid(-1,saved_egid);
+#elif USE_SETUIDX
+       setgidx(ID_REAL, saved_rgid);
+       setgidx(ID_EFFECTIVE, saved_egid);
+#else
+       set_effective_gid(saved_egid);
+       if (getgid() != saved_rgid)
+               setgid(saved_rgid);
+       set_effective_gid(saved_egid);
+#endif
+
+       assert_gid(saved_rgid, saved_egid);
+}
+
+
 /****************************************************************************
  set the real AND effective uid to the current effective uid in a way that
  allows root to be regained.
@@ -427,7 +459,7 @@ main()
 #endif
 
 /****************************************************************************
-Check if we are setuid root.  Used in libsmb and smbpasswd parinoia checks.
+Check if we are setuid root.  Used in libsmb and smbpasswd paranoia checks.
 ****************************************************************************/
 BOOL is_setuid_root(void) 
 {