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)
48 /* are we running as non-root? This is used by the regresison test code,
49 and potentially also for sites that want non-root smbd */
50 static uid_t initial_uid;
52 /****************************************************************************
53 remember what uid we got started as - this allows us to run correctly
54 as non-root while catching trapdoor systems
55 ****************************************************************************/
58 initial_uid = geteuid();
59 if (initial_uid != (uid_t)0) {
60 /* the DEBUG() subsystem has not been initialised when this is called */
61 fprintf(stderr, "WARNING: running as non-root. Some functionality will be missing\n");
65 /****************************************************************************
66 are we running in non-root mode?
67 ****************************************************************************/
68 BOOL non_root_mode(void)
70 return (initial_uid != (uid_t)0);
73 /****************************************************************************
74 abort if we haven't set the uid correctly
75 ****************************************************************************/
76 static void assert_uid(uid_t ruid, uid_t euid)
78 if ((euid != (uid_t)-1 && geteuid() != euid) ||
79 (ruid != (uid_t)-1 && getuid() != ruid)) {
80 if (!non_root_mode()) {
81 DEBUG(0,("Failed to set uid privileges to (%d,%d) now set to (%d,%d)\n",
83 (int)getuid(), (int)geteuid()));
84 smb_panic("failed to set uid\n");
90 /****************************************************************************
91 abort if we haven't set the gid correctly
92 ****************************************************************************/
93 static void assert_gid(gid_t rgid, gid_t egid)
95 if ((egid != (gid_t)-1 && getegid() != egid) ||
96 (rgid != (gid_t)-1 && getgid() != rgid)) {
97 if (!non_root_mode()) {
98 DEBUG(0,("Failed to set gid privileges to (%d,%d) now set to (%d,%d) uid=(%d,%d)\n",
100 (int)getgid(), (int)getegid(),
101 (int)getuid(), (int)geteuid()));
102 smb_panic("failed to set gid\n");
108 /****************************************************************************
109 Gain root privilege before doing something.
110 We want to end up with ruid==euid==0
111 ****************************************************************************/
112 void gain_root_privilege(void)
127 setuidx(ID_EFFECTIVE, 0);
131 /* this is needed on some systems */
138 /****************************************************************************
139 Ensure our real and effective groups are zero.
140 we want to end up with rgid==egid==0
141 ****************************************************************************/
142 void gain_root_group_privilege(void)
157 setgidx(ID_EFFECTIVE, 0);
167 /****************************************************************************
168 Set *only* the effective uid.
169 we want to end up with ruid==0 and euid==uid
170 ****************************************************************************/
171 void set_effective_uid(uid_t uid)
174 setresuid(-1,uid,-1);
186 setuidx(ID_EFFECTIVE, uid);
192 /****************************************************************************
193 Set *only* the effective gid.
194 we want to end up with rgid==0 and egid==gid
195 ****************************************************************************/
196 void set_effective_gid(gid_t gid)
199 setresgid(-1,gid,-1);
211 setgidx(ID_EFFECTIVE, gid);
217 static uid_t saved_euid, saved_ruid;
219 /****************************************************************************
220 save the real and effective uid for later restoration. Used by the quotas
222 ****************************************************************************/
223 void save_re_uid(void)
225 saved_ruid = getuid();
226 saved_euid = geteuid();
230 /****************************************************************************
232 ****************************************************************************/
233 void restore_re_uid(void)
235 set_effective_uid(0);
238 setresuid(saved_ruid, saved_euid, -1);
240 setreuid(saved_ruid, -1);
241 setreuid(-1,saved_euid);
243 setuidx(ID_REAL, saved_ruid);
244 setuidx(ID_EFFECTIVE, saved_euid);
246 set_effective_uid(saved_euid);
247 if (getuid() != saved_ruid)
249 set_effective_uid(saved_euid);
252 assert_uid(saved_ruid, saved_euid);
255 /****************************************************************************
256 set the real AND effective uid to the current effective uid in a way that
257 allows root to be regained.
258 This is only possible on some platforms.
259 ****************************************************************************/
262 uid_t uid = geteuid();
265 setresuid(geteuid(), -1, -1);
284 assert_uid(uid, uid);
289 /****************************************************************************
290 Become the specified uid and gid - permanently !
291 there should be no way back if possible
292 ****************************************************************************/
293 void become_user_permanently(uid_t uid, gid_t gid)
296 * First - gain root privilege. We do this to ensure
297 * we can lose it again.
300 gain_root_privilege();
301 gain_root_group_privilege();
304 setresgid(gid,gid,gid);
306 setresuid(uid,uid,uid);
326 setgidx(ID_REAL, gid);
327 setgidx(ID_EFFECTIVE, gid);
329 setuidx(ID_REAL, uid);
330 setuidx(ID_EFFECTIVE, uid);
334 assert_uid(uid, uid);
335 assert_gid(gid, gid);
340 /****************************************************************************
341 this function just checks that we don't get ENOSYS back
342 ****************************************************************************/
343 static int have_syscall(void)
360 setuidx(ID_EFFECTIVE, -1);
363 if (errno == ENOSYS) return -1;
371 #if (defined(AIX) && defined(USE_SETREUID))
372 /* setreuid is badly broken on AIX 4.1, we avoid it completely */
373 fprintf(stderr,"avoiding possibly broken setreuid\n");
377 /* if not running as root then at least check to see if we get ENOSYS - this
378 handles Linux 2.0.x with glibc 2.1 */
379 fprintf(stderr,"not running as root: checking for ENOSYS\n");
380 exit(have_syscall());
383 gain_root_privilege();
384 gain_root_group_privilege();
385 set_effective_gid(1);
386 set_effective_uid(1);
389 gain_root_privilege();
390 gain_root_group_privilege();
391 become_user_permanently(1, 1);
394 fprintf(stderr,"uid not set permanently\n");