Add a descriptive comment to our usage of setresuid. lib/afs.c needs
[Samba/gebeck_regimport.git] / source3 / lib / util_sec.c
blob1980b8bfb7cae887023f34002bedfbc92c2f0f6f
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 ****************************************************************************/
55 void sec_init(void)
57 initial_uid = geteuid();
58 initial_gid = getegid();
61 /****************************************************************************
62 some code (eg. winbindd) needs to know what uid we started as
63 ****************************************************************************/
64 uid_t sec_initial_uid(void)
66 return initial_uid;
69 /****************************************************************************
70 some code (eg. winbindd, profiling shm) needs to know what gid we started as
71 ****************************************************************************/
72 gid_t sec_initial_gid(void)
74 return initial_gid;
77 /****************************************************************************
78 are we running in non-root mode?
79 ****************************************************************************/
80 BOOL non_root_mode(void)
82 return (initial_uid != (uid_t)0);
85 /****************************************************************************
86 abort if we haven't set the uid correctly
87 ****************************************************************************/
88 static void assert_uid(uid_t ruid, uid_t euid)
90 if ((euid != (uid_t)-1 && geteuid() != euid) ||
91 (ruid != (uid_t)-1 && getuid() != ruid)) {
92 if (!non_root_mode()) {
93 DEBUG(0,("Failed to set uid privileges to (%d,%d) now set to (%d,%d)\n",
94 (int)ruid, (int)euid,
95 (int)getuid(), (int)geteuid()));
96 smb_panic("failed to set uid\n");
97 exit(1);
102 /****************************************************************************
103 abort if we haven't set the gid correctly
104 ****************************************************************************/
105 static void assert_gid(gid_t rgid, gid_t egid)
107 if ((egid != (gid_t)-1 && getegid() != egid) ||
108 (rgid != (gid_t)-1 && getgid() != rgid)) {
109 if (!non_root_mode()) {
110 DEBUG(0,("Failed to set gid privileges to (%d,%d) now set to (%d,%d) uid=(%d,%d)\n",
111 (int)rgid, (int)egid,
112 (int)getgid(), (int)getegid(),
113 (int)getuid(), (int)geteuid()));
114 smb_panic("failed to set gid\n");
115 exit(1);
120 /****************************************************************************
121 Gain root privilege before doing something.
122 We want to end up with ruid==euid==0
123 ****************************************************************************/
124 void gain_root_privilege(void)
126 #if USE_SETRESUID
127 setresuid(0,0,0);
128 #endif
130 #if USE_SETEUID
131 seteuid(0);
132 #endif
134 #if USE_SETREUID
135 setreuid(0, 0);
136 #endif
138 #if USE_SETUIDX
139 setuidx(ID_EFFECTIVE, 0);
140 setuidx(ID_REAL, 0);
141 #endif
143 /* this is needed on some systems */
144 setuid(0);
146 assert_uid(0, 0);
150 /****************************************************************************
151 Ensure our real and effective groups are zero.
152 we want to end up with rgid==egid==0
153 ****************************************************************************/
154 void gain_root_group_privilege(void)
156 #if USE_SETRESUID
157 setresgid(0,0,0);
158 #endif
160 #if USE_SETREUID
161 setregid(0,0);
162 #endif
164 #if USE_SETEUID
165 setegid(0);
166 #endif
168 #if USE_SETUIDX
169 setgidx(ID_EFFECTIVE, 0);
170 setgidx(ID_REAL, 0);
171 #endif
173 setgid(0);
175 assert_gid(0, 0);
179 /****************************************************************************
180 Set *only* the effective uid.
181 we want to end up with ruid==0 and euid==uid
182 ****************************************************************************/
183 void set_effective_uid(uid_t uid)
185 #if USE_SETRESUID
186 /* On Systems which have this function, would it not be more
187 * appropriate to also set the real uid by doing
188 * setresuid(uid,uid,-1)? This would make patching AFS
189 * unnecessary. See comment in lib/afs.c. */
190 setresuid(-1,uid,-1);
191 #endif
193 #if USE_SETREUID
194 setreuid(-1,uid);
195 #endif
197 #if USE_SETEUID
198 seteuid(uid);
199 #endif
201 #if USE_SETUIDX
202 setuidx(ID_EFFECTIVE, uid);
203 #endif
205 assert_uid(-1, uid);
208 /****************************************************************************
209 Set *only* the effective gid.
210 we want to end up with rgid==0 and egid==gid
211 ****************************************************************************/
212 void set_effective_gid(gid_t gid)
214 #if USE_SETRESUID
215 setresgid(-1,gid,-1);
216 #endif
218 #if USE_SETREUID
219 setregid(-1,gid);
220 #endif
222 #if USE_SETEUID
223 setegid(gid);
224 #endif
226 #if USE_SETUIDX
227 setgidx(ID_EFFECTIVE, gid);
228 #endif
230 assert_gid(-1, gid);
233 static uid_t saved_euid, saved_ruid;
234 static gid_t saved_egid, saved_rgid;
236 /****************************************************************************
237 save the real and effective uid for later restoration. Used by the quotas
238 code
239 ****************************************************************************/
240 void save_re_uid(void)
242 saved_ruid = getuid();
243 saved_euid = geteuid();
247 /****************************************************************************
248 and restore them!
249 ****************************************************************************/
250 void restore_re_uid(void)
252 set_effective_uid(0);
254 #if USE_SETRESUID
255 setresuid(saved_ruid, saved_euid, -1);
256 #elif USE_SETREUID
257 setreuid(saved_ruid, -1);
258 setreuid(-1,saved_euid);
259 #elif USE_SETUIDX
260 setuidx(ID_REAL, saved_ruid);
261 setuidx(ID_EFFECTIVE, saved_euid);
262 #else
263 set_effective_uid(saved_euid);
264 if (getuid() != saved_ruid)
265 setuid(saved_ruid);
266 set_effective_uid(saved_euid);
267 #endif
269 assert_uid(saved_ruid, saved_euid);
273 /****************************************************************************
274 save the real and effective gid for later restoration. Used by the
275 getgroups code
276 ****************************************************************************/
277 void save_re_gid(void)
279 saved_rgid = getgid();
280 saved_egid = getegid();
283 /****************************************************************************
284 and restore them!
285 ****************************************************************************/
286 void restore_re_gid(void)
288 #if USE_SETRESUID
289 setresgid(saved_rgid, saved_egid, -1);
290 #elif USE_SETREUID
291 setregid(saved_rgid, -1);
292 setregid(-1,saved_egid);
293 #elif USE_SETUIDX
294 setgidx(ID_REAL, saved_rgid);
295 setgidx(ID_EFFECTIVE, saved_egid);
296 #else
297 set_effective_gid(saved_egid);
298 if (getgid() != saved_rgid)
299 setgid(saved_rgid);
300 set_effective_gid(saved_egid);
301 #endif
303 assert_gid(saved_rgid, saved_egid);
307 /****************************************************************************
308 set the real AND effective uid to the current effective uid in a way that
309 allows root to be regained.
310 This is only possible on some platforms.
311 ****************************************************************************/
312 int set_re_uid(void)
314 uid_t uid = geteuid();
316 #if USE_SETRESUID
317 setresuid(geteuid(), -1, -1);
318 #endif
320 #if USE_SETREUID
321 setreuid(0, 0);
322 setreuid(uid, -1);
323 setreuid(-1, uid);
324 #endif
326 #if USE_SETEUID
327 /* can't be done */
328 return -1;
329 #endif
331 #if USE_SETUIDX
332 /* can't be done */
333 return -1;
334 #endif
336 assert_uid(uid, uid);
337 return 0;
341 /****************************************************************************
342 Become the specified uid and gid - permanently !
343 there should be no way back if possible
344 ****************************************************************************/
345 void become_user_permanently(uid_t uid, gid_t gid)
348 * First - gain root privilege. We do this to ensure
349 * we can lose it again.
352 gain_root_privilege();
353 gain_root_group_privilege();
355 #if USE_SETRESUID
356 setresgid(gid,gid,gid);
357 setgid(gid);
358 setresuid(uid,uid,uid);
359 setuid(uid);
360 #endif
362 #if USE_SETREUID
363 setregid(gid,gid);
364 setgid(gid);
365 setreuid(uid,uid);
366 setuid(uid);
367 #endif
369 #if USE_SETEUID
370 setegid(gid);
371 setgid(gid);
372 setuid(uid);
373 seteuid(uid);
374 setuid(uid);
375 #endif
377 #if USE_SETUIDX
378 setgidx(ID_REAL, gid);
379 setgidx(ID_EFFECTIVE, gid);
380 setgid(gid);
381 setuidx(ID_REAL, uid);
382 setuidx(ID_EFFECTIVE, uid);
383 setuid(uid);
384 #endif
386 assert_uid(uid, uid);
387 assert_gid(gid, gid);
390 #ifdef AUTOCONF_TEST
392 /****************************************************************************
393 this function just checks that we don't get ENOSYS back
394 ****************************************************************************/
395 static int have_syscall(void)
397 errno = 0;
399 #if USE_SETRESUID
400 setresuid(-1,-1,-1);
401 #endif
403 #if USE_SETREUID
404 setreuid(-1,-1);
405 #endif
407 #if USE_SETEUID
408 seteuid(-1);
409 #endif
411 #if USE_SETUIDX
412 setuidx(ID_EFFECTIVE, -1);
413 #endif
415 if (errno == ENOSYS) return -1;
417 return 0;
420 main()
422 if (getuid() != 0) {
423 #if (defined(AIX) && defined(USE_SETREUID))
424 /* setreuid is badly broken on AIX 4.1, we avoid it completely */
425 fprintf(stderr,"avoiding possibly broken setreuid\n");
426 exit(1);
427 #endif
429 /* if not running as root then at least check to see if we get ENOSYS - this
430 handles Linux 2.0.x with glibc 2.1 */
431 fprintf(stderr,"not running as root: checking for ENOSYS\n");
432 exit(have_syscall());
435 gain_root_privilege();
436 gain_root_group_privilege();
437 set_effective_gid(1);
438 set_effective_uid(1);
439 save_re_uid();
440 restore_re_uid();
441 gain_root_privilege();
442 gain_root_group_privilege();
443 become_user_permanently(1, 1);
444 setuid(0);
445 if (getuid() == 0) {
446 fprintf(stderr,"uid not set permanently\n");
447 exit(1);
450 printf("OK\n");
452 exit(0);
454 #endif
456 /****************************************************************************
457 Check if we are setuid root. Used in libsmb and smbpasswd paranoia checks.
458 ****************************************************************************/
459 BOOL is_setuid_root(void)
461 return (geteuid() == (uid_t)0) && (getuid() != (uid_t)0);