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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "system/passwd.h" /* uid_wrapper */
23 #include "../lib/util/setid.h"
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 /* In autoconf/test mode include the definitions of samba_setXXX. */
44 #include "../lib/util/setid.c"
46 #define DEBUG(x, y) printf y
47 #define smb_panic(x) exit(1)
51 /* are we running as non-root? This is used by the regresison test code,
52 and potentially also for sites that want non-root smbd */
53 static uid_t initial_uid;
54 static gid_t initial_gid;
56 /****************************************************************************
57 remember what uid we got started as - this allows us to run correctly
58 as non-root while catching trapdoor systems
59 ****************************************************************************/
63 static int initialized;
66 initial_uid = geteuid();
67 initial_gid = getegid();
72 /****************************************************************************
73 some code (eg. winbindd) needs to know what uid we started as
74 ****************************************************************************/
75 uid_t sec_initial_uid(void)
80 /****************************************************************************
81 some code (eg. winbindd, profiling shm) needs to know what gid we started as
82 ****************************************************************************/
83 gid_t sec_initial_gid(void)
88 /****************************************************************************
89 are we running in non-root mode?
90 ****************************************************************************/
91 bool non_root_mode(void)
93 return (initial_uid != (uid_t)0);
96 /****************************************************************************
97 abort if we haven't set the uid correctly
98 ****************************************************************************/
99 static void assert_uid(uid_t ruid, uid_t euid)
101 if ((euid != (uid_t)-1 && geteuid() != euid) ||
102 (ruid != (uid_t)-1 && getuid() != ruid)) {
103 if (!non_root_mode()) {
104 DEBUG(0,("Failed to set uid privileges to (%d,%d) now set to (%d,%d)\n",
105 (int)ruid, (int)euid,
106 (int)getuid(), (int)geteuid()));
107 smb_panic("failed to set uid\n");
113 /****************************************************************************
114 abort if we haven't set the gid correctly
115 ****************************************************************************/
116 static void assert_gid(gid_t rgid, gid_t egid)
118 if ((egid != (gid_t)-1 && getegid() != egid) ||
119 (rgid != (gid_t)-1 && getgid() != rgid)) {
120 if (!non_root_mode()) {
121 DEBUG(0,("Failed to set gid privileges to (%d,%d) now set to (%d,%d) uid=(%d,%d)\n",
122 (int)rgid, (int)egid,
123 (int)getgid(), (int)getegid(),
124 (int)getuid(), (int)geteuid()));
125 smb_panic("failed to set gid\n");
131 /****************************************************************************
132 Gain root privilege before doing something.
133 We want to end up with ruid==euid==0
134 ****************************************************************************/
135 void gain_root_privilege(void)
137 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
138 samba_setresuid(0,0,0);
146 samba_setreuid(0, 0);
150 samba_setuidx(ID_EFFECTIVE, 0);
151 samba_setuidx(ID_REAL, 0);
154 /* this is needed on some systems */
161 /****************************************************************************
162 Ensure our real and effective groups are zero.
163 we want to end up with rgid==egid==0
164 ****************************************************************************/
165 void gain_root_group_privilege(void)
167 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
168 samba_setresgid(0,0,0);
180 samba_setgidx(ID_EFFECTIVE, 0);
181 samba_setgidx(ID_REAL, 0);
190 /****************************************************************************
191 Set effective uid, and possibly the real uid too.
192 We want to end up with either:
194 ruid==uid and euid==uid
198 ruid==0 and euid==uid
200 depending on what the local OS will allow us to regain root from.
201 ****************************************************************************/
202 void set_effective_uid(uid_t uid)
204 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
205 /* Set the effective as well as the real uid. */
206 if (samba_setresuid(uid,uid,-1) == -1) {
207 if (errno == EAGAIN) {
208 DEBUG(0, ("samba_setresuid failed with EAGAIN. uid(%d) "
209 "might be over its NPROC limit\n",
216 samba_setreuid(-1,uid);
224 samba_setuidx(ID_EFFECTIVE, uid);
230 /****************************************************************************
231 Set *only* the effective gid.
232 we want to end up with rgid==0 and egid==gid
233 ****************************************************************************/
234 void set_effective_gid(gid_t gid)
236 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
237 samba_setresgid(-1,gid,-1);
241 samba_setregid(-1,gid);
249 samba_setgidx(ID_EFFECTIVE, gid);
255 static uid_t saved_euid, saved_ruid;
256 static gid_t saved_egid, saved_rgid;
258 /****************************************************************************
259 save the real and effective uid for later restoration. Used by the quotas
261 ****************************************************************************/
262 void save_re_uid(void)
264 saved_ruid = getuid();
265 saved_euid = geteuid();
269 /****************************************************************************
271 ****************************************************************************/
273 void restore_re_uid_fromroot(void)
275 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
276 samba_setresuid(saved_ruid, saved_euid, -1);
278 samba_setreuid(saved_ruid, -1);
279 samba_setreuid(-1,saved_euid);
281 samba_setuidx(ID_REAL, saved_ruid);
282 samba_setuidx(ID_EFFECTIVE, saved_euid);
284 set_effective_uid(saved_euid);
285 if (getuid() != saved_ruid)
286 samba_setuid(saved_ruid);
287 set_effective_uid(saved_euid);
290 assert_uid(saved_ruid, saved_euid);
293 void restore_re_uid(void)
295 set_effective_uid(0);
296 restore_re_uid_fromroot();
299 /****************************************************************************
300 save the real and effective gid for later restoration. Used by the
302 ****************************************************************************/
303 void save_re_gid(void)
305 saved_rgid = getgid();
306 saved_egid = getegid();
309 /****************************************************************************
311 ****************************************************************************/
312 void restore_re_gid(void)
314 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
315 samba_setresgid(saved_rgid, saved_egid, -1);
317 samba_setregid(saved_rgid, -1);
318 samba_setregid(-1,saved_egid);
320 samba_setgidx(ID_REAL, saved_rgid);
321 samba_setgidx(ID_EFFECTIVE, saved_egid);
323 set_effective_gid(saved_egid);
324 if (getgid() != saved_rgid)
325 samba_setgid(saved_rgid);
326 set_effective_gid(saved_egid);
329 assert_gid(saved_rgid, saved_egid);
333 /****************************************************************************
334 set the real AND effective uid to the current effective uid in a way that
335 allows root to be regained.
336 This is only possible on some platforms.
337 ****************************************************************************/
340 uid_t uid = geteuid();
342 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
343 samba_setresuid(geteuid(), -1, -1);
347 samba_setreuid(0, 0);
348 samba_setreuid(uid, -1);
349 samba_setreuid(-1, uid);
362 assert_uid(uid, uid);
367 /****************************************************************************
368 Become the specified uid and gid - permanently !
369 there should be no way back if possible
370 ****************************************************************************/
371 void become_user_permanently(uid_t uid, gid_t gid)
374 * First - gain root privilege. We do this to ensure
375 * we can lose it again.
378 gain_root_privilege();
379 gain_root_group_privilege();
381 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
382 samba_setresgid(gid,gid,gid);
384 samba_setresuid(uid,uid,uid);
389 samba_setregid(gid,gid);
391 samba_setreuid(uid,uid);
404 samba_setgidx(ID_REAL, gid);
405 samba_setgidx(ID_EFFECTIVE, gid);
407 samba_setuidx(ID_REAL, uid);
408 samba_setuidx(ID_EFFECTIVE, uid);
412 assert_uid(uid, uid);
413 assert_gid(gid, gid);
418 /****************************************************************************
419 this function just checks that we don't get ENOSYS back
420 ****************************************************************************/
421 static int have_syscall(void)
425 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
426 samba_setresuid(-1,-1,-1);
430 samba_setreuid(-1,-1);
438 samba_setuidx(ID_EFFECTIVE, -1);
441 if (errno == ENOSYS) return -1;
449 #if (defined(AIX) && defined(USE_SETREUID))
450 /* setreuid is badly broken on AIX 4.1, we avoid it completely */
451 fprintf(stderr,"avoiding possibly broken setreuid\n");
455 /* if not running as root then at least check to see if we get ENOSYS - this
456 handles Linux 2.0.x with glibc 2.1 */
457 fprintf(stderr,"not running as root: checking for ENOSYS\n");
458 exit(have_syscall());
461 gain_root_privilege();
462 gain_root_group_privilege();
463 set_effective_gid(1);
464 set_effective_uid(1);
467 gain_root_privilege();
468 gain_root_group_privilege();
469 become_user_permanently(1, 1);
472 fprintf(stderr,"uid not set permanently\n");
482 /****************************************************************************
483 Check if we are setuid root. Used in libsmb and smbpasswd paranoia checks.
484 ****************************************************************************/
485 bool is_setuid_root(void)
487 return (geteuid() == (uid_t)0) && (getuid() != (uid_t)0);