r18643: Fix a 64-bit warning
[Samba/nascimento.git] / source / lib / util_sec.c
blob3f8cb690cd016115fbc4891e1745414ec93efc87
1 /*
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.
21 #ifndef AUTOCONF_TEST
22 #include "includes.h"
23 #else
24 /* we are running this code in autoconf test mode to see which type of setuid
25 function works */
26 #if defined(HAVE_UNISTD_H)
27 #include <unistd.h>
28 #endif
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <errno.h>
34 #ifdef HAVE_SYS_PRIV_H
35 #include <sys/priv.h>
36 #endif
37 #ifdef HAVE_SYS_ID_H
38 #include <sys/id.h>
39 #endif
41 #define DEBUG(x, y) printf y
42 #define smb_panic(x) exit(1)
43 #define BOOL int
44 #endif
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 /****************************************************************************
52 remember what uid we got started as - this allows us to run correctly
53 as non-root while catching trapdoor systems
54 ****************************************************************************/
56 void sec_init(void)
58 static int initialized;
60 if (!initialized) {
61 initial_uid = geteuid();
62 initial_gid = getegid();
63 initialized = 1;
67 /****************************************************************************
68 some code (eg. winbindd) needs to know what uid we started as
69 ****************************************************************************/
70 uid_t sec_initial_uid(void)
72 return initial_uid;
75 /****************************************************************************
76 some code (eg. winbindd, profiling shm) needs to know what gid we started as
77 ****************************************************************************/
78 gid_t sec_initial_gid(void)
80 return initial_gid;
83 /****************************************************************************
84 are we running in non-root mode?
85 ****************************************************************************/
86 BOOL non_root_mode(void)
88 return (initial_uid != (uid_t)0);
91 /****************************************************************************
92 abort if we haven't set the uid correctly
93 ****************************************************************************/
94 static void assert_uid(uid_t ruid, uid_t euid)
96 if ((euid != (uid_t)-1 && geteuid() != euid) ||
97 (ruid != (uid_t)-1 && getuid() != ruid)) {
98 if (!non_root_mode()) {
99 DEBUG(0,("Failed to set uid privileges to (%d,%d) now set to (%d,%d)\n",
100 (int)ruid, (int)euid,
101 (int)getuid(), (int)geteuid()));
102 smb_panic("failed to set uid\n");
103 exit(1);
108 /****************************************************************************
109 abort if we haven't set the gid correctly
110 ****************************************************************************/
111 static void assert_gid(gid_t rgid, gid_t egid)
113 if ((egid != (gid_t)-1 && getegid() != egid) ||
114 (rgid != (gid_t)-1 && getgid() != rgid)) {
115 if (!non_root_mode()) {
116 DEBUG(0,("Failed to set gid privileges to (%d,%d) now set to (%d,%d) uid=(%d,%d)\n",
117 (int)rgid, (int)egid,
118 (int)getgid(), (int)getegid(),
119 (int)getuid(), (int)geteuid()));
120 smb_panic("failed to set gid\n");
121 exit(1);
126 /****************************************************************************
127 Gain root privilege before doing something.
128 We want to end up with ruid==euid==0
129 ****************************************************************************/
130 void gain_root_privilege(void)
132 #if USE_SETRESUID
133 setresuid(0,0,0);
134 #endif
136 #if USE_SETEUID
137 seteuid(0);
138 #endif
140 #if USE_SETREUID
141 setreuid(0, 0);
142 #endif
144 #if USE_SETUIDX
145 setuidx(ID_EFFECTIVE, 0);
146 setuidx(ID_REAL, 0);
147 #endif
149 /* this is needed on some systems */
150 setuid(0);
152 assert_uid(0, 0);
156 /****************************************************************************
157 Ensure our real and effective groups are zero.
158 we want to end up with rgid==egid==0
159 ****************************************************************************/
160 void gain_root_group_privilege(void)
162 #if USE_SETRESUID
163 setresgid(0,0,0);
164 #endif
166 #if USE_SETREUID
167 setregid(0,0);
168 #endif
170 #if USE_SETEUID
171 setegid(0);
172 #endif
174 #if USE_SETUIDX
175 setgidx(ID_EFFECTIVE, 0);
176 setgidx(ID_REAL, 0);
177 #endif
179 setgid(0);
181 assert_gid(0, 0);
185 /****************************************************************************
186 Set effective uid, and possibly the real uid too.
187 We want to end up with either:
189 ruid==uid and euid==uid
193 ruid==0 and euid==uid
195 depending on what the local OS will allow us to regain root from.
196 ****************************************************************************/
197 void set_effective_uid(uid_t uid)
199 #if USE_SETRESUID
200 /* Set the effective as well as the real uid. */
201 setresuid(uid,uid,-1);
202 #endif
204 #if USE_SETREUID
205 setreuid(-1,uid);
206 #endif
208 #if USE_SETEUID
209 seteuid(uid);
210 #endif
212 #if USE_SETUIDX
213 setuidx(ID_EFFECTIVE, uid);
214 #endif
216 assert_uid(-1, uid);
219 /****************************************************************************
220 Set *only* the effective gid.
221 we want to end up with rgid==0 and egid==gid
222 ****************************************************************************/
223 void set_effective_gid(gid_t gid)
225 #if USE_SETRESUID
226 setresgid(-1,gid,-1);
227 #endif
229 #if USE_SETREUID
230 setregid(-1,gid);
231 #endif
233 #if USE_SETEUID
234 setegid(gid);
235 #endif
237 #if USE_SETUIDX
238 setgidx(ID_EFFECTIVE, gid);
239 #endif
241 assert_gid(-1, gid);
244 static uid_t saved_euid, saved_ruid;
245 static gid_t saved_egid, saved_rgid;
247 /****************************************************************************
248 save the real and effective uid for later restoration. Used by the quotas
249 code
250 ****************************************************************************/
251 void save_re_uid(void)
253 saved_ruid = getuid();
254 saved_euid = geteuid();
258 /****************************************************************************
259 and restore them!
260 ****************************************************************************/
262 static void restore_re_uid_fromroot(void)
264 #if USE_SETRESUID
265 setresuid(saved_ruid, saved_euid, -1);
266 #elif USE_SETREUID
267 setreuid(saved_ruid, -1);
268 setreuid(-1,saved_euid);
269 #elif USE_SETUIDX
270 setuidx(ID_REAL, saved_ruid);
271 setuidx(ID_EFFECTIVE, saved_euid);
272 #else
273 set_effective_uid(saved_euid);
274 if (getuid() != saved_ruid)
275 setuid(saved_ruid);
276 set_effective_uid(saved_euid);
277 #endif
279 assert_uid(saved_ruid, saved_euid);
282 void restore_re_uid(void)
284 set_effective_uid(0);
285 restore_re_uid_fromroot();
288 /****************************************************************************
289 Lightweight become root - no group change.
290 ****************************************************************************/
292 void become_root_uid_only(void)
294 save_re_uid();
295 set_effective_uid(0);
298 /****************************************************************************
299 Lightweight unbecome root - no group change. Expects we are root already,
300 saves errno across call boundary.
301 ****************************************************************************/
303 void unbecome_root_uid_only(void)
305 int saved_errno = errno;
306 restore_re_uid_fromroot();
307 errno = saved_errno;
310 /****************************************************************************
311 save the real and effective gid for later restoration. Used by the
312 getgroups code
313 ****************************************************************************/
314 void save_re_gid(void)
316 saved_rgid = getgid();
317 saved_egid = getegid();
320 /****************************************************************************
321 and restore them!
322 ****************************************************************************/
323 void restore_re_gid(void)
325 #if USE_SETRESUID
326 setresgid(saved_rgid, saved_egid, -1);
327 #elif USE_SETREUID
328 setregid(saved_rgid, -1);
329 setregid(-1,saved_egid);
330 #elif USE_SETUIDX
331 setgidx(ID_REAL, saved_rgid);
332 setgidx(ID_EFFECTIVE, saved_egid);
333 #else
334 set_effective_gid(saved_egid);
335 if (getgid() != saved_rgid)
336 setgid(saved_rgid);
337 set_effective_gid(saved_egid);
338 #endif
340 assert_gid(saved_rgid, saved_egid);
344 /****************************************************************************
345 set the real AND effective uid to the current effective uid in a way that
346 allows root to be regained.
347 This is only possible on some platforms.
348 ****************************************************************************/
349 int set_re_uid(void)
351 uid_t uid = geteuid();
353 #if USE_SETRESUID
354 setresuid(geteuid(), -1, -1);
355 #endif
357 #if USE_SETREUID
358 setreuid(0, 0);
359 setreuid(uid, -1);
360 setreuid(-1, uid);
361 #endif
363 #if USE_SETEUID
364 /* can't be done */
365 return -1;
366 #endif
368 #if USE_SETUIDX
369 /* can't be done */
370 return -1;
371 #endif
373 assert_uid(uid, uid);
374 return 0;
378 /****************************************************************************
379 Become the specified uid and gid - permanently !
380 there should be no way back if possible
381 ****************************************************************************/
382 void become_user_permanently(uid_t uid, gid_t gid)
385 * First - gain root privilege. We do this to ensure
386 * we can lose it again.
389 gain_root_privilege();
390 gain_root_group_privilege();
392 #if USE_SETRESUID
393 setresgid(gid,gid,gid);
394 setgid(gid);
395 setresuid(uid,uid,uid);
396 setuid(uid);
397 #endif
399 #if USE_SETREUID
400 setregid(gid,gid);
401 setgid(gid);
402 setreuid(uid,uid);
403 setuid(uid);
404 #endif
406 #if USE_SETEUID
407 setegid(gid);
408 setgid(gid);
409 setuid(uid);
410 seteuid(uid);
411 setuid(uid);
412 #endif
414 #if USE_SETUIDX
415 setgidx(ID_REAL, gid);
416 setgidx(ID_EFFECTIVE, gid);
417 setgid(gid);
418 setuidx(ID_REAL, uid);
419 setuidx(ID_EFFECTIVE, uid);
420 setuid(uid);
421 #endif
423 assert_uid(uid, uid);
424 assert_gid(gid, gid);
427 #ifdef AUTOCONF_TEST
429 /****************************************************************************
430 this function just checks that we don't get ENOSYS back
431 ****************************************************************************/
432 static int have_syscall(void)
434 errno = 0;
436 #if USE_SETRESUID
437 setresuid(-1,-1,-1);
438 #endif
440 #if USE_SETREUID
441 setreuid(-1,-1);
442 #endif
444 #if USE_SETEUID
445 seteuid(-1);
446 #endif
448 #if USE_SETUIDX
449 setuidx(ID_EFFECTIVE, -1);
450 #endif
452 if (errno == ENOSYS) return -1;
454 return 0;
457 main()
459 if (getuid() != 0) {
460 #if (defined(AIX) && defined(USE_SETREUID))
461 /* setreuid is badly broken on AIX 4.1, we avoid it completely */
462 fprintf(stderr,"avoiding possibly broken setreuid\n");
463 exit(1);
464 #endif
466 /* if not running as root then at least check to see if we get ENOSYS - this
467 handles Linux 2.0.x with glibc 2.1 */
468 fprintf(stderr,"not running as root: checking for ENOSYS\n");
469 exit(have_syscall());
472 gain_root_privilege();
473 gain_root_group_privilege();
474 set_effective_gid(1);
475 set_effective_uid(1);
476 save_re_uid();
477 restore_re_uid();
478 gain_root_privilege();
479 gain_root_group_privilege();
480 become_user_permanently(1, 1);
481 setuid(0);
482 if (getuid() == 0) {
483 fprintf(stderr,"uid not set permanently\n");
484 exit(1);
487 printf("OK\n");
489 exit(0);
491 #endif
493 /****************************************************************************
494 Check if we are setuid root. Used in libsmb and smbpasswd paranoia checks.
495 ****************************************************************************/
496 BOOL is_setuid_root(void)
498 return (geteuid() == (uid_t)0) && (getuid() != (uid_t)0);