2 Unix SMB/CIFS implementation.
3 Copyright (C) Jeremy Allison 1998.
4 rewritten for version 2.0.6 by Tridge
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* we are running this code in autoconf test mode to see which type of setuid
26 #if defined(HAVE_UNISTD_H)
31 #include <sys/types.h>
34 #ifdef HAVE_SYS_PRIV_H
41 #define DEBUG(x, y) printf y
42 #define smb_panic(x) exit(1)
46 /* are we running as non-root? This is used by the regresison test code,
47 and potentially also for sites that want non-root smbd */
48 static uid_t initial_uid;
49 static gid_t initial_gid;
51 static BOOL sec_initialised;
53 /****************************************************************************
54 remember what uid we got started as - this allows us to run correctly
55 as non-root while catching trapdoor systems
56 ****************************************************************************/
59 initial_uid = geteuid();
60 initial_gid = getegid();
62 sec_initialised = True;
65 /****************************************************************************
66 some code (eg. winbindd) needs to know what uid we started as
67 ****************************************************************************/
68 uid_t sec_initial_uid(void)
71 smb_panic("sec_initial_uid() called before sec_init()\n");
76 /****************************************************************************
77 some code (eg. winbindd, profiling shm) needs to know what gid we started as
78 ****************************************************************************/
79 gid_t sec_initial_gid(void)
82 smb_panic("sec_initial_gid() called before sec_init()\n");
87 /****************************************************************************
88 are we running in non-root mode?
89 ****************************************************************************/
90 BOOL non_root_mode(void)
93 smb_panic("non_root_mode() called before sec_init()\n");
95 return (initial_uid != (uid_t)0);
98 /****************************************************************************
99 abort if we haven't set the uid correctly
100 ****************************************************************************/
101 static void assert_uid(uid_t ruid, uid_t euid)
103 if ((euid != (uid_t)-1 && geteuid() != euid) ||
104 (ruid != (uid_t)-1 && getuid() != ruid)) {
105 if (!non_root_mode()) {
106 DEBUG(0,("Failed to set uid privileges to (%d,%d) now set to (%d,%d)\n",
107 (int)ruid, (int)euid,
108 (int)getuid(), (int)geteuid()));
109 smb_panic("failed to set uid\n");
115 /****************************************************************************
116 abort if we haven't set the gid correctly
117 ****************************************************************************/
118 static void assert_gid(gid_t rgid, gid_t egid)
120 if ((egid != (gid_t)-1 && getegid() != egid) ||
121 (rgid != (gid_t)-1 && getgid() != rgid)) {
122 if (!non_root_mode()) {
123 DEBUG(0,("Failed to set gid privileges to (%d,%d) now set to (%d,%d) uid=(%d,%d)\n",
124 (int)rgid, (int)egid,
125 (int)getgid(), (int)getegid(),
126 (int)getuid(), (int)geteuid()));
127 smb_panic("failed to set gid\n");
133 /****************************************************************************
134 Gain root privilege before doing something.
135 We want to end up with ruid==euid==0
136 ****************************************************************************/
137 void gain_root_privilege(void)
152 setuidx(ID_EFFECTIVE, 0);
156 /* this is needed on some systems */
163 /****************************************************************************
164 Ensure our real and effective groups are zero.
165 we want to end up with rgid==egid==0
166 ****************************************************************************/
167 void gain_root_group_privilege(void)
182 setgidx(ID_EFFECTIVE, 0);
192 /****************************************************************************
193 Set *only* the effective uid.
194 we want to end up with ruid==0 and euid==uid
195 ****************************************************************************/
196 void set_effective_uid(uid_t uid)
199 setresuid(-1,uid,-1);
211 setuidx(ID_EFFECTIVE, uid);
217 /****************************************************************************
218 Set *only* the effective gid.
219 we want to end up with rgid==0 and egid==gid
220 ****************************************************************************/
221 void set_effective_gid(gid_t gid)
224 setresgid(-1,gid,-1);
236 setgidx(ID_EFFECTIVE, gid);
242 static uid_t saved_euid, saved_ruid;
244 /****************************************************************************
245 save the real and effective uid for later restoration. Used by the quotas
247 ****************************************************************************/
248 void save_re_uid(void)
250 saved_ruid = getuid();
251 saved_euid = geteuid();
255 /****************************************************************************
257 ****************************************************************************/
258 void restore_re_uid(void)
260 set_effective_uid(0);
263 setresuid(saved_ruid, saved_euid, -1);
265 setreuid(saved_ruid, -1);
266 setreuid(-1,saved_euid);
268 setuidx(ID_REAL, saved_ruid);
269 setuidx(ID_EFFECTIVE, saved_euid);
271 set_effective_uid(saved_euid);
272 if (getuid() != saved_ruid)
274 set_effective_uid(saved_euid);
277 assert_uid(saved_ruid, saved_euid);
280 /****************************************************************************
281 set the real AND effective uid to the current effective uid in a way that
282 allows root to be regained.
283 This is only possible on some platforms.
284 ****************************************************************************/
287 uid_t uid = geteuid();
290 setresuid(geteuid(), -1, -1);
309 assert_uid(uid, uid);
314 /****************************************************************************
315 Become the specified uid and gid - permanently !
316 there should be no way back if possible
317 ****************************************************************************/
318 void become_user_permanently(uid_t uid, gid_t gid)
321 * First - gain root privilege. We do this to ensure
322 * we can lose it again.
325 gain_root_privilege();
326 gain_root_group_privilege();
329 setresgid(gid,gid,gid);
331 setresuid(uid,uid,uid);
351 setgidx(ID_REAL, gid);
352 setgidx(ID_EFFECTIVE, gid);
354 setuidx(ID_REAL, uid);
355 setuidx(ID_EFFECTIVE, uid);
359 assert_uid(uid, uid);
360 assert_gid(gid, gid);
365 /****************************************************************************
366 this function just checks that we don't get ENOSYS back
367 ****************************************************************************/
368 static int have_syscall(void)
385 setuidx(ID_EFFECTIVE, -1);
388 if (errno == ENOSYS) return -1;
396 #if (defined(AIX) && defined(USE_SETREUID))
397 /* setreuid is badly broken on AIX 4.1, we avoid it completely */
398 fprintf(stderr,"avoiding possibly broken setreuid\n");
402 /* if not running as root then at least check to see if we get ENOSYS - this
403 handles Linux 2.0.x with glibc 2.1 */
404 fprintf(stderr,"not running as root: checking for ENOSYS\n");
405 exit(have_syscall());
408 gain_root_privilege();
409 gain_root_group_privilege();
410 set_effective_gid(1);
411 set_effective_uid(1);
414 gain_root_privilege();
415 gain_root_group_privilege();
416 become_user_permanently(1, 1);
419 fprintf(stderr,"uid not set permanently\n");
429 /****************************************************************************
430 Check if we are setuid root. Used in libsmb and smbpasswd parinoia checks.
431 ****************************************************************************/
432 BOOL is_setuid_root(void)
434 return (geteuid() == (uid_t)0) && (getuid() != (uid_t)0);