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 #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
;
51 static gid_t initial_gid
;
53 /****************************************************************************
54 remember what uid we got started as - this allows us to run correctly
55 as non-root while catching trapdoor systems
56 ****************************************************************************/
60 static int initialized
;
63 initial_uid
= geteuid();
64 initial_gid
= getegid();
69 /****************************************************************************
70 some code (eg. winbindd) needs to know what uid we started as
71 ****************************************************************************/
72 uid_t
sec_initial_uid(void)
77 /****************************************************************************
78 some code (eg. winbindd, profiling shm) needs to know what gid we started as
79 ****************************************************************************/
80 gid_t
sec_initial_gid(void)
86 * @brief Check if we are running in root mode.
88 * @return If we samba root privileges it returns true, false otehrwise.
97 if (uid_wrapper_enabled()) {
98 return (euid
== initial_uid
|| euid
== (uid_t
)0);
102 return (initial_uid
== euid
);
105 /****************************************************************************
106 are we running in non-root mode?
107 ****************************************************************************/
108 bool non_root_mode(void)
110 return (initial_uid
!= (uid_t
)0);
113 /****************************************************************************
114 abort if we haven't set the uid correctly
115 ****************************************************************************/
116 static void assert_uid(uid_t ruid
, uid_t euid
)
118 if ((euid
!= (uid_t
)-1 && geteuid() != euid
) ||
119 (ruid
!= (uid_t
)-1 && getuid() != ruid
)) {
120 if (!non_root_mode()) {
121 DEBUG(0,("Failed to set uid privileges to (%d,%d) now set to (%d,%d)\n",
122 (int)ruid
, (int)euid
,
123 (int)getuid(), (int)geteuid()));
124 smb_panic("failed to set uid\n");
130 /****************************************************************************
131 abort if we haven't set the gid correctly
132 ****************************************************************************/
133 static void assert_gid(gid_t rgid
, gid_t egid
)
135 if ((egid
!= (gid_t
)-1 && getegid() != egid
) ||
136 (rgid
!= (gid_t
)-1 && getgid() != rgid
)) {
137 if (!non_root_mode()) {
138 DEBUG(0,("Failed to set gid privileges to (%d,%d) now set to (%d,%d) uid=(%d,%d)\n",
139 (int)rgid
, (int)egid
,
140 (int)getgid(), (int)getegid(),
141 (int)getuid(), (int)geteuid()));
142 smb_panic("failed to set gid\n");
148 /****************************************************************************
149 Gain root privilege before doing something.
150 We want to end up with ruid==euid==0
151 ****************************************************************************/
152 void gain_root_privilege(void)
154 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
155 samba_setresuid(0,0,0);
163 samba_setreuid(0, 0);
167 samba_setuidx(ID_EFFECTIVE
, 0);
168 samba_setuidx(ID_REAL
, 0);
171 /* this is needed on some systems */
178 /****************************************************************************
179 Ensure our real and effective groups are zero.
180 we want to end up with rgid==egid==0
181 ****************************************************************************/
182 void gain_root_group_privilege(void)
184 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
185 samba_setresgid(0,0,0);
197 samba_setgidx(ID_EFFECTIVE
, 0);
198 samba_setgidx(ID_REAL
, 0);
207 /****************************************************************************
208 Set effective uid, and possibly the real uid too.
209 We want to end up with either:
211 ruid==uid and euid==uid
215 ruid==0 and euid==uid
217 depending on what the local OS will allow us to regain root from.
218 ****************************************************************************/
219 void set_effective_uid(uid_t uid
)
221 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
222 /* Set the effective as well as the real uid. */
223 if (samba_setresuid(uid
,uid
,-1) == -1) {
224 if (errno
== EAGAIN
) {
225 DEBUG(0, ("samba_setresuid failed with EAGAIN. uid(%d) "
226 "might be over its NPROC limit\n",
233 samba_setreuid(-1,uid
);
241 samba_setuidx(ID_EFFECTIVE
, uid
);
247 /****************************************************************************
248 Set *only* the effective gid.
249 we want to end up with rgid==0 and egid==gid
250 ****************************************************************************/
251 void set_effective_gid(gid_t gid
)
253 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
254 samba_setresgid(-1,gid
,-1);
258 samba_setregid(-1,gid
);
266 samba_setgidx(ID_EFFECTIVE
, gid
);
272 static uid_t saved_euid
, saved_ruid
;
273 static gid_t saved_egid
, saved_rgid
;
275 /****************************************************************************
276 save the real and effective uid for later restoration. Used by the quotas
278 ****************************************************************************/
279 void save_re_uid(void)
281 saved_ruid
= getuid();
282 saved_euid
= geteuid();
286 /****************************************************************************
288 ****************************************************************************/
290 void restore_re_uid_fromroot(void)
292 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
293 samba_setresuid(saved_ruid
, saved_euid
, -1);
295 samba_setreuid(saved_ruid
, -1);
296 samba_setreuid(-1,saved_euid
);
298 samba_setuidx(ID_REAL
, saved_ruid
);
299 samba_setuidx(ID_EFFECTIVE
, saved_euid
);
301 set_effective_uid(saved_euid
);
302 if (getuid() != saved_ruid
)
303 samba_setuid(saved_ruid
);
304 set_effective_uid(saved_euid
);
307 assert_uid(saved_ruid
, saved_euid
);
310 void restore_re_uid(void)
312 set_effective_uid(0);
313 restore_re_uid_fromroot();
316 /****************************************************************************
317 save the real and effective gid for later restoration. Used by the
319 ****************************************************************************/
320 void save_re_gid(void)
322 saved_rgid
= getgid();
323 saved_egid
= getegid();
326 /****************************************************************************
328 ****************************************************************************/
329 void restore_re_gid(void)
331 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
332 samba_setresgid(saved_rgid
, saved_egid
, -1);
334 samba_setregid(saved_rgid
, -1);
335 samba_setregid(-1,saved_egid
);
337 samba_setgidx(ID_REAL
, saved_rgid
);
338 samba_setgidx(ID_EFFECTIVE
, saved_egid
);
340 set_effective_gid(saved_egid
);
341 if (getgid() != saved_rgid
)
342 samba_setgid(saved_rgid
);
343 set_effective_gid(saved_egid
);
346 assert_gid(saved_rgid
, saved_egid
);
350 /****************************************************************************
351 set the real AND effective uid to the current effective uid in a way that
352 allows root to be regained.
353 This is only possible on some platforms.
354 ****************************************************************************/
357 uid_t uid
= geteuid();
359 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
360 samba_setresuid(uid
, uid
, -1);
364 samba_setreuid(0, 0);
365 samba_setreuid(uid
, -1);
366 samba_setreuid(-1, uid
);
379 assert_uid(uid
, uid
);
384 /****************************************************************************
385 Become the specified uid and gid - permanently !
386 there should be no way back if possible
387 ****************************************************************************/
388 void become_user_permanently(uid_t uid
, gid_t gid
)
391 * First - gain root privilege. We do this to ensure
392 * we can lose it again.
395 gain_root_privilege();
396 gain_root_group_privilege();
398 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
399 samba_setresgid(gid
,gid
,gid
);
401 samba_setresuid(uid
,uid
,uid
);
406 samba_setregid(gid
,gid
);
408 samba_setreuid(uid
,uid
);
421 samba_setgidx(ID_REAL
, gid
);
422 samba_setgidx(ID_EFFECTIVE
, gid
);
424 samba_setuidx(ID_REAL
, uid
);
425 samba_setuidx(ID_EFFECTIVE
, uid
);
429 assert_uid(uid
, uid
);
430 assert_gid(gid
, gid
);
433 /**********************************************************
434 Function to set thread specific credentials. Leave
435 saved-set uid/gid alone.Must be thread-safe code.
436 **********************************************************/
438 int set_thread_credentials(uid_t uid
,
443 #if defined(USE_LINUX_THREAD_CREDENTIALS)
445 * With Linux thread-specific credentials
446 * we know we have setresuid/setresgid
452 if (samba_setresuid(0, 0, -1) != 0) {
455 /* Set our primary gid. */
456 /* Set rg=gid, eg=gid */
457 if (samba_setresgid(gid
, gid
, -1) != 0) {
460 /* Set extra groups list. */
461 if (samba_setgroups(setlen
, gidset
) != 0) {
464 /* Become the requested user. */
465 /* Set ru=uid, eu=uid */
466 if (samba_setresuid(uid
, uid
, -1) != 0) {
469 if (geteuid() != uid
|| getuid() != uid
||
470 getegid() != gid
|| getgid() != gid
) {
471 smb_panic("set_thread_credentials failed\n");
483 /****************************************************************************
484 this function just checks that we don't get ENOSYS back
485 ****************************************************************************/
486 static int have_syscall(void)
490 #if defined(USE_SETRESUID) || defined(USE_LINUX_THREAD_CREDENTIALS)
491 samba_setresuid(-1,-1,-1);
495 samba_setreuid(-1,-1);
503 samba_setuidx(ID_EFFECTIVE
, -1);
506 if (errno
== ENOSYS
) return -1;
514 #if (defined(AIX) && defined(USE_SETREUID))
515 /* setreuid is badly broken on AIX 4.1, we avoid it completely */
516 fprintf(stderr
,"avoiding possibly broken setreuid\n");
520 /* if not running as root then at least check to see if we get ENOSYS - this
521 handles Linux 2.0.x with glibc 2.1 */
522 fprintf(stderr
,"not running as root: checking for ENOSYS\n");
523 exit(have_syscall());
526 gain_root_privilege();
527 gain_root_group_privilege();
528 set_effective_gid(1);
529 set_effective_uid(1);
532 gain_root_privilege();
533 gain_root_group_privilege();
534 become_user_permanently(1, 1);
537 fprintf(stderr
,"uid not set permanently\n");
547 /****************************************************************************
548 Check if we are setuid root. Used in libsmb and smbpasswd paranoia checks.
549 ****************************************************************************/
550 bool is_setuid_root(void)
552 return (geteuid() == (uid_t
)0) && (getuid() != (uid_t
)0);