always compile before commit :-)
[Samba.git] / source / lib / util_sec.c
blob5b8bdb44c1bd93ba31d663c9a37fbd1682a4bbe0
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 extern int DEBUGLEVEL;
25 #else
26 /* we are running this code in autoconf test mode to see which type of setuid
27 function works */
28 #if defined(HAVE_UNISTD_H)
29 #include <unistd.h>
30 #endif
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <errno.h>
36 #ifdef HAVE_SYS_PRIV_H
37 #include <sys/priv.h>
38 #endif
39 #ifdef HAVE_SYS_ID_H
40 #include <sys/id.h>
41 #endif
43 #define DEBUG(x, y) printf y
44 #define smb_panic(x) exit(1)
45 #define BOOL int
46 #endif
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;
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();
61 /****************************************************************************
62 are we running in non-root mode?
63 ****************************************************************************/
64 BOOL non_root_mode(void)
66 return (initial_uid != (uid_t)0);
69 /****************************************************************************
70 abort if we haven't set the uid correctly
71 ****************************************************************************/
72 static void assert_uid(uid_t ruid, uid_t euid)
74 if ((euid != (uid_t)-1 && geteuid() != euid) ||
75 (ruid != (uid_t)-1 && getuid() != ruid)) {
76 if (!non_root_mode()) {
77 DEBUG(0,("Failed to set uid privileges to (%d,%d) now set to (%d,%d)\n",
78 (int)ruid, (int)euid,
79 (int)getuid(), (int)geteuid()));
80 smb_panic("failed to set uid\n");
81 exit(1);
86 /****************************************************************************
87 abort if we haven't set the gid correctly
88 ****************************************************************************/
89 static void assert_gid(gid_t rgid, gid_t egid)
91 if ((egid != (gid_t)-1 && getegid() != egid) ||
92 (rgid != (gid_t)-1 && getgid() != rgid)) {
93 if (!non_root_mode()) {
94 DEBUG(0,("Failed to set gid privileges to (%d,%d) now set to (%d,%d) uid=(%d,%d)\n",
95 (int)rgid, (int)egid,
96 (int)getgid(), (int)getegid(),
97 (int)getuid(), (int)geteuid()));
98 smb_panic("failed to set gid\n");
99 exit(1);
104 /****************************************************************************
105 Gain root privilege before doing something.
106 We want to end up with ruid==euid==0
107 ****************************************************************************/
108 void gain_root_privilege(void)
110 #if USE_SETRESUID
111 setresuid(0,0,0);
112 #endif
114 #if USE_SETEUID
115 seteuid(0);
116 #endif
118 #if USE_SETREUID
119 setreuid(0, 0);
120 #endif
122 #if USE_SETUIDX
123 setuidx(ID_EFFECTIVE, 0);
124 setuidx(ID_REAL, 0);
125 #endif
127 /* this is needed on some systems */
128 setuid(0);
130 assert_uid(0, 0);
134 /****************************************************************************
135 Ensure our real and effective groups are zero.
136 we want to end up with rgid==egid==0
137 ****************************************************************************/
138 void gain_root_group_privilege(void)
140 #if USE_SETRESUID
141 setresgid(0,0,0);
142 #endif
144 #if USE_SETREUID
145 setregid(0,0);
146 #endif
148 #if USE_SETEUID
149 setegid(0);
150 #endif
152 #if USE_SETUIDX
153 setgidx(ID_EFFECTIVE, 0);
154 setgidx(ID_REAL, 0);
155 #endif
157 setgid(0);
159 assert_gid(0, 0);
163 /****************************************************************************
164 Set *only* the effective uid.
165 we want to end up with ruid==0 and euid==uid
166 ****************************************************************************/
167 void set_effective_uid(uid_t uid)
169 #if USE_SETRESUID
170 setresuid(-1,uid,-1);
171 #endif
173 #if USE_SETREUID
174 setreuid(-1,uid);
175 #endif
177 #if USE_SETEUID
178 seteuid(uid);
179 #endif
181 #if USE_SETUIDX
182 setuidx(ID_EFFECTIVE, uid);
183 #endif
185 assert_uid(-1, uid);
188 /****************************************************************************
189 Set *only* the effective gid.
190 we want to end up with rgid==0 and egid==gid
191 ****************************************************************************/
192 void set_effective_gid(gid_t gid)
194 #if USE_SETRESUID
195 setresgid(-1,gid,-1);
196 #endif
198 #if USE_SETREUID
199 setregid(-1,gid);
200 #endif
202 #if USE_SETEUID
203 setegid(gid);
204 #endif
206 #if USE_SETUIDX
207 setgidx(ID_EFFECTIVE, gid);
208 #endif
210 assert_gid(-1, gid);
213 static uid_t saved_euid, saved_ruid;
215 /****************************************************************************
216 save the real and effective uid for later restoration. Used by the quotas
217 code
218 ****************************************************************************/
219 void save_re_uid(void)
221 saved_ruid = getuid();
222 saved_euid = geteuid();
226 /****************************************************************************
227 and restore them!
228 ****************************************************************************/
229 void restore_re_uid(void)
231 set_effective_uid(0);
233 #if USE_SETRESUID
234 setresuid(saved_ruid, saved_euid, -1);
235 #elif USE_SETREUID
236 setreuid(saved_ruid, -1);
237 setreuid(-1,saved_euid);
238 #elif USE_SETUIDX
239 setuidx(ID_REAL, saved_ruid);
240 setuidx(ID_EFFECTIVE, saved_euid);
241 #else
242 set_effective_uid(saved_euid);
243 if (getuid() != saved_ruid)
244 setuid(saved_ruid);
245 set_effective_uid(saved_euid);
246 #endif
248 assert_uid(saved_ruid, saved_euid);
251 /****************************************************************************
252 set the real AND effective uid to the current effective uid in a way that
253 allows root to be regained.
254 This is only possible on some platforms.
255 ****************************************************************************/
256 int set_re_uid(void)
258 uid_t uid = geteuid();
260 #if USE_SETRESUID
261 setresuid(geteuid(), -1, -1);
262 #endif
264 #if USE_SETREUID
265 setreuid(0, 0);
266 setreuid(uid, -1);
267 setreuid(-1, uid);
268 #endif
270 #if USE_SETEUID
271 /* can't be done */
272 return -1;
273 #endif
275 #if USE_SETUIDX
276 /* can't be done */
277 return -1;
278 #endif
280 assert_uid(uid, uid);
281 return 0;
285 /****************************************************************************
286 Become the specified uid and gid - permanently !
287 there should be no way back if possible
288 ****************************************************************************/
289 void become_user_permanently(uid_t uid, gid_t gid)
292 * First - gain root privilege. We do this to ensure
293 * we can lose it again.
296 gain_root_privilege();
297 gain_root_group_privilege();
299 #if USE_SETRESUID
300 setresgid(gid,gid,gid);
301 setgid(gid);
302 setresuid(uid,uid,uid);
303 setuid(uid);
304 #endif
306 #if USE_SETREUID
307 setregid(gid,gid);
308 setgid(gid);
309 setreuid(uid,uid);
310 setuid(uid);
311 #endif
313 #if USE_SETEUID
314 setegid(gid);
315 setgid(gid);
316 setuid(uid);
317 seteuid(uid);
318 setuid(uid);
319 #endif
321 #if USE_SETUIDX
322 setgidx(ID_REAL, gid);
323 setgidx(ID_EFFECTIVE, gid);
324 setgid(gid);
325 setuidx(ID_REAL, uid);
326 setuidx(ID_EFFECTIVE, uid);
327 setuid(uid);
328 #endif
330 assert_uid(uid, uid);
331 assert_gid(gid, gid);
334 #ifdef AUTOCONF_TEST
336 /****************************************************************************
337 this function just checks that we don't get ENOSYS back
338 ****************************************************************************/
339 static int have_syscall(void)
341 errno = 0;
343 #if USE_SETRESUID
344 setresuid(-1,-1,-1);
345 #endif
347 #if USE_SETREUID
348 setreuid(-1,-1);
349 #endif
351 #if USE_SETEUID
352 seteuid(-1);
353 #endif
355 #if USE_SETUIDX
356 setuidx(ID_EFFECTIVE, -1);
357 #endif
359 if (errno == ENOSYS) return -1;
361 return 0;
364 main()
366 if (getuid() != 0) {
367 #if (defined(AIX) && defined(USE_SETREUID))
368 /* setreuid is badly broken on AIX 4.1, we avoid it completely */
369 fprintf(stderr,"avoiding possibly broken setreuid\n");
370 exit(1);
371 #endif
373 /* if not running as root then at least check to see if we get ENOSYS - this
374 handles Linux 2.0.x with glibc 2.1 */
375 fprintf(stderr,"not running as root: checking for ENOSYS\n");
376 exit(have_syscall());
379 gain_root_privilege();
380 gain_root_group_privilege();
381 set_effective_gid(1);
382 set_effective_uid(1);
383 save_re_uid();
384 restore_re_uid();
385 gain_root_privilege();
386 gain_root_group_privilege();
387 become_user_permanently(1, 1);
388 setuid(0);
389 if (getuid() == 0) {
390 fprintf(stderr,"uid not set permanently\n");
391 exit(1);
394 printf("OK\n");
396 exit(0);
398 #endif