make sure we have BOOL in autoconf usage of util_sec.c
[mat/samba.git] / source3 / lib / util_sec.c
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
6
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.
11
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.
16
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.
20 */
21
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>
35
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
42
43 #define DEBUG(x, y) printf y
44 #define smb_panic(x) exit(1)
45 #define BOOL int
46 #endif
47
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
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)
57 {
58         initial_uid = geteuid();
59         if (initial_uid != (uid_t)0) {
60                 /* the DEBUG() subsystem has not been initialised when this is called */
61                 fprintf(stderr, "WARNING: running as non-root. Some functionality will be missing\n");
62         }
63 }
64
65 /****************************************************************************
66 are we running in non-root mode?
67 ****************************************************************************/
68 BOOL non_root_mode(void)
69 {
70         return (initial_uid != (uid_t)0);
71 }
72
73 /****************************************************************************
74 abort if we haven't set the uid correctly
75 ****************************************************************************/
76 static void assert_uid(uid_t ruid, uid_t euid)
77 {
78         if ((euid != (uid_t)-1 && geteuid() != euid) ||
79             (ruid != (uid_t)-1 && getuid() != ruid)) {
80                 if (!non_root_mode()) {
81                         DEBUG(0,("Failed to set uid privileges to (%d,%d) now set to (%d,%d)\n",
82                                  (int)ruid, (int)euid,
83                                  (int)getuid(), (int)geteuid()));
84                         smb_panic("failed to set uid\n");
85                         exit(1);
86                 }
87         }
88 }
89
90 /****************************************************************************
91 abort if we haven't set the gid correctly
92 ****************************************************************************/
93 static void assert_gid(gid_t rgid, gid_t egid)
94 {
95         if ((egid != (gid_t)-1 && getegid() != egid) ||
96             (rgid != (gid_t)-1 && getgid() != rgid)) {
97                 if (!non_root_mode()) {
98                         DEBUG(0,("Failed to set gid privileges to (%d,%d) now set to (%d,%d) uid=(%d,%d)\n",
99                                  (int)rgid, (int)egid,
100                                  (int)getgid(), (int)getegid(),
101                                  (int)getuid(), (int)geteuid()));
102                         smb_panic("failed to set gid\n");
103                         exit(1);
104                 }
105         }
106 }
107
108 /****************************************************************************
109  Gain root privilege before doing something. 
110  We want to end up with ruid==euid==0
111 ****************************************************************************/
112 void gain_root_privilege(void)
113 {       
114 #if USE_SETRESUID
115         setresuid(0,0,0);
116 #endif
117     
118 #if USE_SETEUID
119         seteuid(0);
120 #endif
121
122 #if USE_SETREUID
123         setreuid(0, 0);
124 #endif
125
126 #if USE_SETUIDX
127         setuidx(ID_EFFECTIVE, 0);
128         setuidx(ID_REAL, 0);
129 #endif
130
131         /* this is needed on some systems */
132         setuid(0);
133
134         assert_uid(0, 0);
135 }
136
137
138 /****************************************************************************
139  Ensure our real and effective groups are zero.
140  we want to end up with rgid==egid==0
141 ****************************************************************************/
142 void gain_root_group_privilege(void)
143 {
144 #if USE_SETRESUID
145         setresgid(0,0,0);
146 #endif
147
148 #if USE_SETREUID
149         setregid(0,0);
150 #endif
151
152 #if USE_SETEUID
153         setegid(0);
154 #endif
155
156 #if USE_SETUIDX
157         setgidx(ID_EFFECTIVE, 0);
158         setgidx(ID_REAL, 0);
159 #endif
160
161         setgid(0);
162
163         assert_gid(0, 0);
164 }
165
166
167 /****************************************************************************
168  Set *only* the effective uid.
169  we want to end up with ruid==0 and euid==uid
170 ****************************************************************************/
171 void set_effective_uid(uid_t uid)
172 {
173 #if USE_SETRESUID
174         setresuid(-1,uid,-1);
175 #endif
176
177 #if USE_SETREUID
178         setreuid(-1,uid);
179 #endif
180
181 #if USE_SETEUID
182         seteuid(uid);
183 #endif
184
185 #if USE_SETUIDX
186         setuidx(ID_EFFECTIVE, uid);
187 #endif
188
189         assert_uid(-1, uid);
190 }
191
192 /****************************************************************************
193  Set *only* the effective gid.
194  we want to end up with rgid==0 and egid==gid
195 ****************************************************************************/
196 void set_effective_gid(gid_t gid)
197 {
198 #if USE_SETRESUID
199         setresgid(-1,gid,-1);
200 #endif
201
202 #if USE_SETREUID
203         setregid(-1,gid);
204 #endif
205
206 #if USE_SETEUID
207         setegid(gid);
208 #endif
209
210 #if USE_SETUIDX
211         setgidx(ID_EFFECTIVE, gid);
212 #endif
213
214         assert_gid(-1, gid);
215 }
216
217 static uid_t saved_euid, saved_ruid;
218
219 /****************************************************************************
220  save the real and effective uid for later restoration. Used by the quotas
221  code
222 ****************************************************************************/
223 void save_re_uid(void)
224 {
225         saved_ruid = getuid();
226         saved_euid = geteuid();
227 }
228
229
230 /****************************************************************************
231  and restore them!
232 ****************************************************************************/
233 void restore_re_uid(void)
234 {
235         set_effective_uid(0);
236
237 #if USE_SETRESUID
238         setresuid(saved_ruid, saved_euid, -1);
239 #elif USE_SETREUID
240         setreuid(saved_ruid, -1);
241         setreuid(-1,saved_euid);
242 #elif USE_SETUIDX
243         setuidx(ID_REAL, saved_ruid);
244         setuidx(ID_EFFECTIVE, saved_euid);
245 #else
246         set_effective_uid(saved_euid);
247         if (getuid() != saved_ruid)
248                 setuid(saved_ruid);
249         set_effective_uid(saved_euid);
250 #endif
251
252         assert_uid(saved_ruid, saved_euid);
253 }
254
255 /****************************************************************************
256  set the real AND effective uid to the current effective uid in a way that
257  allows root to be regained.
258  This is only possible on some platforms.
259 ****************************************************************************/
260 int set_re_uid(void)
261 {
262         uid_t uid = geteuid();
263
264 #if USE_SETRESUID
265         setresuid(geteuid(), -1, -1);
266 #endif
267
268 #if USE_SETREUID
269         setreuid(0, 0);
270         setreuid(uid, -1);
271         setreuid(-1, uid);
272 #endif
273
274 #if USE_SETEUID
275         /* can't be done */
276         return -1;
277 #endif
278
279 #if USE_SETUIDX
280         /* can't be done */
281         return -1;
282 #endif
283
284         assert_uid(uid, uid);
285         return 0;
286 }
287
288
289 /****************************************************************************
290  Become the specified uid and gid - permanently !
291  there should be no way back if possible
292 ****************************************************************************/
293 void become_user_permanently(uid_t uid, gid_t gid)
294 {
295         /*
296          * First - gain root privilege. We do this to ensure
297          * we can lose it again.
298          */
299
300         gain_root_privilege();
301         gain_root_group_privilege();
302
303 #if USE_SETRESUID
304         setresgid(gid,gid,gid);
305         setgid(gid);
306         setresuid(uid,uid,uid);
307         setuid(uid);
308 #endif
309
310 #if USE_SETREUID
311         setregid(gid,gid);
312         setgid(gid);
313         setreuid(uid,uid);
314         setuid(uid);
315 #endif
316
317 #if USE_SETEUID
318         setegid(gid);
319         setgid(gid);
320         setuid(uid);
321         seteuid(uid);
322         setuid(uid);
323 #endif
324
325 #if USE_SETUIDX
326         setgidx(ID_REAL, gid);
327         setgidx(ID_EFFECTIVE, gid);
328         setgid(gid);
329         setuidx(ID_REAL, uid);
330         setuidx(ID_EFFECTIVE, uid);
331         setuid(uid);
332 #endif
333         
334         assert_uid(uid, uid);
335         assert_gid(gid, gid);
336 }
337
338 #ifdef AUTOCONF_TEST
339
340 /****************************************************************************
341 this function just checks that we don't get ENOSYS back
342 ****************************************************************************/
343 static int have_syscall(void)
344 {
345         errno = 0;
346
347 #if USE_SETRESUID
348         setresuid(-1,-1,-1);
349 #endif
350
351 #if USE_SETREUID
352         setreuid(-1,-1);
353 #endif
354
355 #if USE_SETEUID
356         seteuid(-1);
357 #endif
358
359 #if USE_SETUIDX
360         setuidx(ID_EFFECTIVE, -1);
361 #endif
362
363         if (errno == ENOSYS) return -1;
364         
365         return 0;
366 }
367
368 main()
369 {
370         if (getuid() != 0) {
371 #if (defined(AIX) && defined(USE_SETREUID))
372                 /* setreuid is badly broken on AIX 4.1, we avoid it completely */
373                 fprintf(stderr,"avoiding possibly broken setreuid\n");
374                 exit(1);
375 #endif
376
377                 /* if not running as root then at least check to see if we get ENOSYS - this 
378                    handles Linux 2.0.x with glibc 2.1 */
379                 fprintf(stderr,"not running as root: checking for ENOSYS\n");
380                 exit(have_syscall());
381         }
382
383         gain_root_privilege();
384         gain_root_group_privilege();
385         set_effective_gid(1);
386         set_effective_uid(1);
387         save_re_uid();
388         restore_re_uid();
389         gain_root_privilege();
390         gain_root_group_privilege();
391         become_user_permanently(1, 1);
392         setuid(0);
393         if (getuid() == 0) {
394                 fprintf(stderr,"uid not set permanently\n");
395                 exit(1);
396         }
397
398         printf("OK\n");
399
400         exit(0);
401 }
402 #endif