2 Unix SMB/Netbios implementation.
4 Copyright (C) Jeremy Allison 1998.
5 rewritten for version 2.0.6 by Tridge
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 extern int DEBUGLEVEL;
26 /* we are running this code in autoconf test mode to see which type of setuid
28 #if defined(HAVE_UNISTD_H)
33 #include <sys/types.h>
36 #ifdef HAVE_SYS_PRIV_H
43 #define DEBUG(x, y) printf y
44 #define smb_panic(x) exit(1)
47 /****************************************************************************
48 abort if we haven't set the uid correctly
49 ****************************************************************************/
50 static void assert_uid(uid_t ruid, uid_t euid)
52 if ((euid != (uid_t)-1 && geteuid() != euid) ||
53 (ruid != (uid_t)-1 && getuid() != ruid)) {
54 #ifndef SMB_REGRESSION_TEST
55 DEBUG(0,("Failed to set uid privileges to (%d,%d) now set to (%d,%d)\n",
57 (int)getuid(), (int)geteuid()));
58 smb_panic("failed to set uid\n");
64 /****************************************************************************
65 abort if we haven't set the gid correctly
66 ****************************************************************************/
67 static void assert_gid(gid_t rgid, gid_t egid)
69 if ((egid != (gid_t)-1 && getegid() != egid) ||
70 (rgid != (gid_t)-1 && getgid() != rgid)) {
71 #ifndef SMB_REGRESSION_TEST
72 DEBUG(0,("Failed to set gid privileges to (%d,%d) now set to (%d,%d) uid=(%d,%d)\n",
74 (int)getgid(), (int)getegid(),
75 (int)getuid(), (int)geteuid()));
76 smb_panic("failed to set gid\n");
82 /****************************************************************************
83 Gain root privilege before doing something.
84 We want to end up with ruid==euid==0
85 ****************************************************************************/
86 void gain_root_privilege(void)
101 setuidx(ID_EFFECTIVE, 0);
105 /* this is needed on some systems */
112 /****************************************************************************
113 Ensure our real and effective groups are zero.
114 we want to end up with rgid==egid==0
115 ****************************************************************************/
116 void gain_root_group_privilege(void)
131 setgidx(ID_EFFECTIVE, 0);
141 /****************************************************************************
142 Set *only* the effective uid.
143 we want to end up with ruid==0 and euid==uid
144 ****************************************************************************/
145 void set_effective_uid(uid_t uid)
148 setresuid(-1,uid,-1);
160 setuidx(ID_EFFECTIVE, uid);
166 /****************************************************************************
167 Set *only* the effective gid.
168 we want to end up with rgid==0 and egid==gid
169 ****************************************************************************/
170 void set_effective_gid(gid_t gid)
173 setresgid(-1,gid,-1);
185 setgidx(ID_EFFECTIVE, gid);
191 static uid_t saved_euid, saved_ruid;
193 /****************************************************************************
194 save the real and effective uid for later restoration. Used by the quotas
196 ****************************************************************************/
197 void save_re_uid(void)
199 saved_ruid = getuid();
200 saved_euid = geteuid();
204 /****************************************************************************
206 ****************************************************************************/
207 void restore_re_uid(void)
209 set_effective_uid(0);
212 setresuid(saved_ruid, saved_euid, -1);
214 setreuid(saved_ruid, -1);
215 setreuid(-1,saved_euid);
217 setuidx(ID_REAL, saved_ruid);
218 setuidx(ID_EFFECTIVE, saved_euid);
220 set_effective_uid(saved_euid);
221 if (getuid() != saved_ruid)
223 set_effective_uid(saved_euid);
226 assert_uid(saved_ruid, saved_euid);
229 /****************************************************************************
230 set the real AND effective uid to the current effective uid in a way that
231 allows root to be regained.
232 This is only possible on some platforms.
233 ****************************************************************************/
236 uid_t uid = geteuid();
239 setresuid(geteuid(), -1, -1);
258 assert_uid(uid, uid);
263 /****************************************************************************
264 Become the specified uid and gid - permanently !
265 there should be no way back if possible
266 ****************************************************************************/
267 void become_user_permanently(uid_t uid, gid_t gid)
270 * First - gain root privilege. We do this to ensure
271 * we can lose it again.
274 gain_root_privilege();
275 gain_root_group_privilege();
278 setresgid(gid,gid,gid);
280 setresuid(uid,uid,uid);
300 setgidx(ID_REAL, gid);
301 setgidx(ID_EFFECTIVE, gid);
303 setuidx(ID_REAL, uid);
304 setuidx(ID_EFFECTIVE, uid);
308 assert_uid(uid, uid);
309 assert_gid(gid, gid);
314 /****************************************************************************
315 this function just checks that we don't get ENOSYS back
316 ****************************************************************************/
317 static int have_syscall(void)
334 setuidx(ID_EFFECTIVE, -1);
337 if (errno == ENOSYS) return -1;
345 #if (defined(AIX) && defined(USE_SETREUID))
346 /* setreuid is badly broken on AIX 4.1, we avoid it completely */
347 fprintf(stderr,"avoiding possibly broken setreuid\n");
351 /* if not running as root then at least check to see if we get ENOSYS - this
352 handles Linux 2.0.x with glibc 2.1 */
353 fprintf(stderr,"not running as root: checking for ENOSYS\n");
354 exit(have_syscall());
357 gain_root_privilege();
358 gain_root_group_privilege();
359 set_effective_gid(1);
360 set_effective_uid(1);
363 gain_root_privilege();
364 gain_root_group_privilege();
365 become_user_permanently(1, 1);
368 fprintf(stderr,"uid not set permanently\n");