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)
34 #ifdef HAVE_SYS_PRIV_H
41 #define DEBUG(x, y) printf y
42 #define smb_panic(x) exit(1)
45 /****************************************************************************
46 abort if we haven't set the uid correctly
47 ****************************************************************************/
48 static void assert_uid(uid_t ruid, uid_t euid)
50 if ((euid != (uid_t)-1 && geteuid() != euid) ||
51 (ruid != (uid_t)-1 && getuid() != ruid)) {
52 DEBUG(0,("Failed to set uid privileges to (%d,%d) now set to (%d,%d)\n",
54 (int)getuid(), (int)geteuid()));
55 smb_panic("failed to set uid\n");
60 /****************************************************************************
61 abort if we haven't set the gid correctly
62 ****************************************************************************/
63 static void assert_gid(gid_t rgid, gid_t egid)
65 if ((egid != (gid_t)-1 && getegid() != egid) ||
66 (rgid != (gid_t)-1 && getgid() != rgid)) {
67 DEBUG(0,("Failed to set gid privileges to (%d,%d) now set to (%d,%d) uid=(%d,%d)\n",
69 (int)getgid(), (int)getegid(),
70 (int)getuid(), (int)geteuid()));
71 smb_panic("failed to set gid\n");
76 /****************************************************************************
77 Gain root privilege before doing something.
78 We want to end up with ruid==euid==0
79 ****************************************************************************/
80 void gain_root_privilege(void)
95 setuidx(ID_EFFECTIVE, 0);
99 /* this is needed on some systems */
106 /****************************************************************************
107 Ensure our real and effective groups are zero.
108 we want to end up with rgid==egid==0
109 ****************************************************************************/
110 void gain_root_group_privilege(void)
125 setgidx(ID_EFFECTIVE, 0);
135 /****************************************************************************
136 Set *only* the effective uid.
137 we want to end up with ruid==0 and euid==uid
138 ****************************************************************************/
139 void set_effective_uid(uid_t uid)
142 setresuid(-1,uid,-1);
154 setuidx(ID_EFFECTIVE, uid);
160 /****************************************************************************
161 Set *only* the effective gid.
162 we want to end up with rgid==0 and egid==gid
163 ****************************************************************************/
164 void set_effective_gid(gid_t gid)
167 setresgid(-1,gid,-1);
179 setgidx(ID_EFFECTIVE, gid);
185 static uid_t saved_euid, saved_ruid;
187 /****************************************************************************
188 save the real and effective uid for later restoration. Used by the quotas
190 ****************************************************************************/
191 void save_re_uid(void)
193 saved_ruid = getuid();
194 saved_euid = geteuid();
198 /****************************************************************************
200 ****************************************************************************/
201 void restore_re_uid(void)
203 set_effective_uid(0);
204 set_effective_uid(saved_euid);
205 if (getuid() != saved_ruid) setuid(saved_ruid);
206 set_effective_uid(saved_euid);
208 assert_uid(saved_ruid, saved_euid);
211 /****************************************************************************
212 set the real AND effective uid to the current effective uid in a way that
213 allows root to be regained.
214 This is only possible on some platforms.
215 ****************************************************************************/
218 uid_t uid = geteuid();
221 setresuid(geteuid(), -1, -1);
240 assert_uid(uid, uid);
245 /****************************************************************************
246 Become the specified uid and gid - permanently !
247 there should be no way back if possible
248 ****************************************************************************/
249 void become_user_permanently(uid_t uid, gid_t gid)
252 * First - gain root privilege. We do this to ensure
253 * we can lose it again.
256 gain_root_privilege();
257 gain_root_group_privilege();
260 setresgid(gid,gid,gid);
262 setresuid(uid,uid,uid);
282 setgidx(ID_REAL, gid);
283 setgidx(ID_EFFECTIVE, gid);
285 setuidx(ID_REAL, uid);
286 setuidx(ID_EFFECTIVE, uid);
290 assert_uid(uid, uid);
291 assert_gid(gid, gid);
298 #if (defined(AIX) && defined(USE_SETREUID))
299 /* setreuid is badly broken on AIX 4.1, we avoid it completely */
300 fprintf(stderr,"avoiding possibly broken setreuid\n");
304 /* assume that if we have the functions then they work */
305 fprintf(stderr,"not running as root: assuming OK\n");
309 gain_root_privilege();
310 gain_root_group_privilege();
311 set_effective_gid(1);
312 set_effective_uid(1);
313 gain_root_privilege();
314 gain_root_group_privilege();
315 become_user_permanently(1, 1);
318 fprintf(stderr,"uid not set permanently\n");