merging for 2.2.6pre1
[Samba.git] / source / lib / util_sec.c
blob08209190deffe86b1f826e3011562d285daf0ef6
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
4 Copyright (C) Jeremy Allison 1998.
5 rewritten for version 2.0.6 by Tridge
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #ifndef AUTOCONF_TEST
23 #include "includes.h"
24 #else
25 /* we are running this code in autoconf test mode to see which type of setuid
26 function works */
27 #if defined(HAVE_UNISTD_H)
28 #include <unistd.h>
29 #endif
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <errno.h>
35 #ifdef HAVE_SYS_PRIV_H
36 #include <sys/priv.h>
37 #endif
38 #ifdef HAVE_SYS_ID_H
39 #include <sys/id.h>
40 #endif
42 #define DEBUG(x, y) printf y
43 #define smb_panic(x) exit(1)
44 #define BOOL int
45 #endif
47 /* are we running as non-root? This is used by the regresison test code,
48 and potentially also for sites that want non-root smbd */
49 static uid_t initial_uid;
50 static gid_t initial_gid;
52 /****************************************************************************
53 remember what uid we got started as - this allows us to run correctly
54 as non-root while catching trapdoor systems
55 ****************************************************************************/
56 void sec_init(void)
58 initial_uid = geteuid();
59 initial_gid = getegid();
62 /****************************************************************************
63 some code (eg. winbindd) needs to know what uid we started as
64 ****************************************************************************/
65 uid_t sec_initial_uid(void)
67 return initial_uid;
70 /****************************************************************************
71 some code (eg. winbindd, profiling shm) needs to know what gid we started as
72 ****************************************************************************/
73 gid_t sec_initial_gid(void)
75 return initial_gid;
78 /****************************************************************************
79 are we running in non-root mode?
80 ****************************************************************************/
81 BOOL non_root_mode(void)
83 return (initial_uid != (uid_t)0);
86 /****************************************************************************
87 abort if we haven't set the uid correctly
88 ****************************************************************************/
89 static void assert_uid(uid_t ruid, uid_t euid)
91 if ((euid != (uid_t)-1 && geteuid() != euid) ||
92 (ruid != (uid_t)-1 && getuid() != ruid)) {
93 if (!non_root_mode()) {
94 DEBUG(0,("Failed to set uid privileges to (%d,%d) now set to (%d,%d)\n",
95 (int)ruid, (int)euid,
96 (int)getuid(), (int)geteuid()));
97 smb_panic("failed to set uid\n");
98 exit(1);
103 /****************************************************************************
104 abort if we haven't set the gid correctly
105 ****************************************************************************/
106 static void assert_gid(gid_t rgid, gid_t egid)
108 if ((egid != (gid_t)-1 && getegid() != egid) ||
109 (rgid != (gid_t)-1 && getgid() != rgid)) {
110 if (!non_root_mode()) {
111 DEBUG(0,("Failed to set gid privileges to (%d,%d) now set to (%d,%d) uid=(%d,%d)\n",
112 (int)rgid, (int)egid,
113 (int)getgid(), (int)getegid(),
114 (int)getuid(), (int)geteuid()));
115 smb_panic("failed to set gid\n");
116 exit(1);
121 /****************************************************************************
122 Gain root privilege before doing something.
123 We want to end up with ruid==euid==0
124 ****************************************************************************/
125 void gain_root_privilege(void)
127 #if USE_SETRESUID
128 setresuid(0,0,0);
129 #endif
131 #if USE_SETEUID
132 seteuid(0);
133 #endif
135 #if USE_SETREUID
136 setreuid(0, 0);
137 #endif
139 #if USE_SETUIDX
140 setuidx(ID_EFFECTIVE, 0);
141 setuidx(ID_REAL, 0);
142 #endif
144 /* this is needed on some systems */
145 setuid(0);
147 assert_uid(0, 0);
151 /****************************************************************************
152 Ensure our real and effective groups are zero.
153 we want to end up with rgid==egid==0
154 ****************************************************************************/
155 void gain_root_group_privilege(void)
157 #if USE_SETRESUID
158 setresgid(0,0,0);
159 #endif
161 #if USE_SETREUID
162 setregid(0,0);
163 #endif
165 #if USE_SETEUID
166 setegid(0);
167 #endif
169 #if USE_SETUIDX
170 setgidx(ID_EFFECTIVE, 0);
171 setgidx(ID_REAL, 0);
172 #endif
174 setgid(0);
176 assert_gid(0, 0);
180 /****************************************************************************
181 Set *only* the effective uid.
182 we want to end up with ruid==0 and euid==uid
183 ****************************************************************************/
184 void set_effective_uid(uid_t uid)
186 #if USE_SETRESUID
187 setresuid(-1,uid,-1);
188 #endif
190 #if USE_SETREUID
191 setreuid(-1,uid);
192 #endif
194 #if USE_SETEUID
195 seteuid(uid);
196 #endif
198 #if USE_SETUIDX
199 setuidx(ID_EFFECTIVE, uid);
200 #endif
202 assert_uid(-1, uid);
205 /****************************************************************************
206 Set *only* the effective gid.
207 we want to end up with rgid==0 and egid==gid
208 ****************************************************************************/
209 void set_effective_gid(gid_t gid)
211 #if USE_SETRESUID
212 setresgid(-1,gid,-1);
213 #endif
215 #if USE_SETREUID
216 setregid(-1,gid);
217 #endif
219 #if USE_SETEUID
220 setegid(gid);
221 #endif
223 #if USE_SETUIDX
224 setgidx(ID_EFFECTIVE, gid);
225 #endif
227 assert_gid(-1, gid);
230 static uid_t saved_euid, saved_ruid;
231 static gid_t saved_egid, saved_rgid;
233 /****************************************************************************
234 save the real and effective uid for later restoration. Used by the quotas
235 code
236 ****************************************************************************/
237 void save_re_uid(void)
239 saved_ruid = getuid();
240 saved_euid = geteuid();
244 /****************************************************************************
245 and restore them!
246 ****************************************************************************/
247 void restore_re_uid(void)
249 set_effective_uid(0);
251 #if USE_SETRESUID
252 setresuid(saved_ruid, saved_euid, -1);
253 #elif USE_SETREUID
254 setreuid(saved_ruid, -1);
255 setreuid(-1,saved_euid);
256 #elif USE_SETUIDX
257 setuidx(ID_REAL, saved_ruid);
258 setuidx(ID_EFFECTIVE, saved_euid);
259 #else
260 set_effective_uid(saved_euid);
261 if (getuid() != saved_ruid)
262 setuid(saved_ruid);
263 set_effective_uid(saved_euid);
264 #endif
266 assert_uid(saved_ruid, saved_euid);
269 /****************************************************************************
270 Save the real and effective gid for later restoration. Used by the
271 getgroups code
272 ****************************************************************************/
274 void save_re_gid(void)
276 saved_rgid = getgid();
277 saved_egid = getegid();
280 /****************************************************************************
281 And restore them!
282 ****************************************************************************/
284 void restore_re_gid(void)
286 #if USE_SETRESUID
287 setresgid(saved_rgid, saved_egid, -1);
288 #elif USE_SETREUID
289 setregid(saved_rgid, -1);
290 setregid(-1,saved_egid);
291 #elif USE_SETUIDX
292 setgidx(ID_REAL, saved_rgid);
293 setgidx(ID_EFFECTIVE, saved_egid);
294 #else
295 set_effective_gid(saved_egid);
296 if (getgid() != saved_rgid)
297 setgid(saved_rgid);
298 set_effective_gid(saved_egid);
299 #endif
301 assert_gid(saved_rgid, saved_egid);
304 /****************************************************************************
305 set the real AND effective uid to the current effective uid in a way that
306 allows root to be regained.
307 This is only possible on some platforms.
308 ****************************************************************************/
309 int set_re_uid(void)
311 uid_t uid = geteuid();
313 #if USE_SETRESUID
314 setresuid(geteuid(), -1, -1);
315 #endif
317 #if USE_SETREUID
318 setreuid(0, 0);
319 setreuid(uid, -1);
320 setreuid(-1, uid);
321 #endif
323 #if USE_SETEUID
324 /* can't be done */
325 return -1;
326 #endif
328 #if USE_SETUIDX
329 /* can't be done */
330 return -1;
331 #endif
333 assert_uid(uid, uid);
334 return 0;
338 /****************************************************************************
339 Become the specified uid and gid - permanently !
340 there should be no way back if possible
341 ****************************************************************************/
342 void become_user_permanently(uid_t uid, gid_t gid)
345 * First - gain root privilege. We do this to ensure
346 * we can lose it again.
349 gain_root_privilege();
350 gain_root_group_privilege();
352 #if USE_SETRESUID
353 setresgid(gid,gid,gid);
354 setgid(gid);
355 setresuid(uid,uid,uid);
356 setuid(uid);
357 #endif
359 #if USE_SETREUID
360 setregid(gid,gid);
361 setgid(gid);
362 setreuid(uid,uid);
363 setuid(uid);
364 #endif
366 #if USE_SETEUID
367 setegid(gid);
368 setgid(gid);
369 setuid(uid);
370 seteuid(uid);
371 setuid(uid);
372 #endif
374 #if USE_SETUIDX
375 setgidx(ID_REAL, gid);
376 setgidx(ID_EFFECTIVE, gid);
377 setgid(gid);
378 setuidx(ID_REAL, uid);
379 setuidx(ID_EFFECTIVE, uid);
380 setuid(uid);
381 #endif
383 assert_uid(uid, uid);
384 assert_gid(gid, gid);
387 #ifdef AUTOCONF_TEST
389 /****************************************************************************
390 this function just checks that we don't get ENOSYS back
391 ****************************************************************************/
392 static int have_syscall(void)
394 errno = 0;
396 #if USE_SETRESUID
397 setresuid(-1,-1,-1);
398 #endif
400 #if USE_SETREUID
401 setreuid(-1,-1);
402 #endif
404 #if USE_SETEUID
405 seteuid(-1);
406 #endif
408 #if USE_SETUIDX
409 setuidx(ID_EFFECTIVE, -1);
410 #endif
412 if (errno == ENOSYS) return -1;
414 return 0;
417 main()
419 if (getuid() != 0) {
420 #if (defined(AIX) && defined(USE_SETREUID))
421 /* setreuid is badly broken on AIX 4.1, we avoid it completely */
422 fprintf(stderr,"avoiding possibly broken setreuid\n");
423 exit(1);
424 #endif
426 /* if not running as root then at least check to see if we get ENOSYS - this
427 handles Linux 2.0.x with glibc 2.1 */
428 fprintf(stderr,"not running as root: checking for ENOSYS\n");
429 exit(have_syscall());
432 gain_root_privilege();
433 gain_root_group_privilege();
434 set_effective_gid(1);
435 set_effective_uid(1);
436 save_re_uid();
437 restore_re_uid();
438 gain_root_privilege();
439 gain_root_group_privilege();
440 become_user_permanently(1, 1);
441 setuid(0);
442 if (getuid() == 0) {
443 fprintf(stderr,"uid not set permanently\n");
444 exit(1);
447 printf("OK\n");
449 exit(0);
451 #endif
453 /****************************************************************************
454 Check if we are setuid root. Used in libsmb and smbpasswd paranoia checks.
455 ****************************************************************************/
456 BOOL is_setuid_root(void)
458 return (geteuid() == (uid_t)0) && (getuid() != (uid_t)0);