r22096: become_root_uid_only() is unneeded - it's only used in
[jra/samba/.git] / source3 / lib / util_sec.c
1 /*
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jeremy Allison 1998.
4    rewritten for version 2.0.6 by Tridge
5
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.
10
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.
15
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.
19 */
20
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>
33
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
40
41 #define DEBUG(x, y) printf y
42 #define smb_panic(x) exit(1)
43 #define BOOL int
44 #endif
45
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;
50
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
56 void sec_init(void)
57 {
58         static int initialized;
59
60         if (!initialized) {
61                 initial_uid = geteuid();
62                 initial_gid = getegid();
63                 initialized = 1;
64         }
65 }
66
67 /****************************************************************************
68 some code (eg. winbindd) needs to know what uid we started as
69 ****************************************************************************/
70 uid_t sec_initial_uid(void)
71 {
72         return initial_uid;
73 }
74
75 /****************************************************************************
76 some code (eg. winbindd, profiling shm) needs to know what gid we started as
77 ****************************************************************************/
78 gid_t sec_initial_gid(void)
79 {
80         return initial_gid;
81 }
82
83 /****************************************************************************
84 are we running in non-root mode?
85 ****************************************************************************/
86 BOOL non_root_mode(void)
87 {
88         return (initial_uid != (uid_t)0);
89 }
90
91 /****************************************************************************
92 abort if we haven't set the uid correctly
93 ****************************************************************************/
94 static void assert_uid(uid_t ruid, uid_t euid)
95 {
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);
104                 }
105         }
106 }
107
108 /****************************************************************************
109 abort if we haven't set the gid correctly
110 ****************************************************************************/
111 static void assert_gid(gid_t rgid, gid_t egid)
112 {
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);
122                 }
123         }
124 }
125
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)
131 {       
132 #if USE_SETRESUID
133         setresuid(0,0,0);
134 #endif
135     
136 #if USE_SETEUID
137         seteuid(0);
138 #endif
139
140 #if USE_SETREUID
141         setreuid(0, 0);
142 #endif
143
144 #if USE_SETUIDX
145         setuidx(ID_EFFECTIVE, 0);
146         setuidx(ID_REAL, 0);
147 #endif
148
149         /* this is needed on some systems */
150         setuid(0);
151
152         assert_uid(0, 0);
153 }
154
155
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)
161 {
162 #if USE_SETRESUID
163         setresgid(0,0,0);
164 #endif
165
166 #if USE_SETREUID
167         setregid(0,0);
168 #endif
169
170 #if USE_SETEUID
171         setegid(0);
172 #endif
173
174 #if USE_SETUIDX
175         setgidx(ID_EFFECTIVE, 0);
176         setgidx(ID_REAL, 0);
177 #endif
178
179         setgid(0);
180
181         assert_gid(0, 0);
182 }
183
184
185 /****************************************************************************
186  Set effective uid, and possibly the real uid too.
187  We want to end up with either:
188   
189    ruid==uid and euid==uid
190
191  or
192
193    ruid==0 and euid==uid
194
195  depending on what the local OS will allow us to regain root from.
196 ****************************************************************************/
197 void set_effective_uid(uid_t uid)
198 {
199 #if USE_SETRESUID
200         /* Set the effective as well as the real uid. */
201         if (setresuid(uid,uid,-1) == -1) {
202                 if (errno == EAGAIN) {
203                         DEBUG(0, ("setresuid failed with EAGAIN. uid(%d) "
204                                   "might be over its NPROC limit\n",
205                                   (int)uid));
206                 }
207         }
208 #endif
209
210 #if USE_SETREUID
211         setreuid(-1,uid);
212 #endif
213
214 #if USE_SETEUID
215         seteuid(uid);
216 #endif
217
218 #if USE_SETUIDX
219         setuidx(ID_EFFECTIVE, uid);
220 #endif
221
222         assert_uid(-1, uid);
223 }
224
225 /****************************************************************************
226  Set *only* the effective gid.
227  we want to end up with rgid==0 and egid==gid
228 ****************************************************************************/
229 void set_effective_gid(gid_t gid)
230 {
231 #if USE_SETRESUID
232         setresgid(-1,gid,-1);
233 #endif
234
235 #if USE_SETREUID
236         setregid(-1,gid);
237 #endif
238
239 #if USE_SETEUID
240         setegid(gid);
241 #endif
242
243 #if USE_SETUIDX
244         setgidx(ID_EFFECTIVE, gid);
245 #endif
246
247         assert_gid(-1, gid);
248 }
249
250 static uid_t saved_euid, saved_ruid;
251 static gid_t saved_egid, saved_rgid;
252
253 /****************************************************************************
254  save the real and effective uid for later restoration. Used by the quotas
255  code
256 ****************************************************************************/
257 void save_re_uid(void)
258 {
259         saved_ruid = getuid();
260         saved_euid = geteuid();
261 }
262
263
264 /****************************************************************************
265  and restore them!
266 ****************************************************************************/
267
268 void restore_re_uid_fromroot(void)
269 {
270 #if USE_SETRESUID
271         setresuid(saved_ruid, saved_euid, -1);
272 #elif USE_SETREUID
273         setreuid(saved_ruid, -1);
274         setreuid(-1,saved_euid);
275 #elif USE_SETUIDX
276         setuidx(ID_REAL, saved_ruid);
277         setuidx(ID_EFFECTIVE, saved_euid);
278 #else
279         set_effective_uid(saved_euid);
280         if (getuid() != saved_ruid)
281                 setuid(saved_ruid);
282         set_effective_uid(saved_euid);
283 #endif
284
285         assert_uid(saved_ruid, saved_euid);
286 }
287
288 void restore_re_uid(void)
289 {
290         set_effective_uid(0);
291         restore_re_uid_fromroot();
292 }
293
294 /****************************************************************************
295  save the real and effective gid for later restoration. Used by the 
296  getgroups code
297 ****************************************************************************/
298 void save_re_gid(void)
299 {
300         saved_rgid = getgid();
301         saved_egid = getegid();
302 }
303
304 /****************************************************************************
305  and restore them!
306 ****************************************************************************/
307 void restore_re_gid(void)
308 {
309 #if USE_SETRESUID
310         setresgid(saved_rgid, saved_egid, -1);
311 #elif USE_SETREUID
312         setregid(saved_rgid, -1);
313         setregid(-1,saved_egid);
314 #elif USE_SETUIDX
315         setgidx(ID_REAL, saved_rgid);
316         setgidx(ID_EFFECTIVE, saved_egid);
317 #else
318         set_effective_gid(saved_egid);
319         if (getgid() != saved_rgid)
320                 setgid(saved_rgid);
321         set_effective_gid(saved_egid);
322 #endif
323
324         assert_gid(saved_rgid, saved_egid);
325 }
326
327
328 /****************************************************************************
329  set the real AND effective uid to the current effective uid in a way that
330  allows root to be regained.
331  This is only possible on some platforms.
332 ****************************************************************************/
333 int set_re_uid(void)
334 {
335         uid_t uid = geteuid();
336
337 #if USE_SETRESUID
338         setresuid(geteuid(), -1, -1);
339 #endif
340
341 #if USE_SETREUID
342         setreuid(0, 0);
343         setreuid(uid, -1);
344         setreuid(-1, uid);
345 #endif
346
347 #if USE_SETEUID
348         /* can't be done */
349         return -1;
350 #endif
351
352 #if USE_SETUIDX
353         /* can't be done */
354         return -1;
355 #endif
356
357         assert_uid(uid, uid);
358         return 0;
359 }
360
361
362 /****************************************************************************
363  Become the specified uid and gid - permanently !
364  there should be no way back if possible
365 ****************************************************************************/
366 void become_user_permanently(uid_t uid, gid_t gid)
367 {
368         /*
369          * First - gain root privilege. We do this to ensure
370          * we can lose it again.
371          */
372
373         gain_root_privilege();
374         gain_root_group_privilege();
375
376 #if USE_SETRESUID
377         setresgid(gid,gid,gid);
378         setgid(gid);
379         setresuid(uid,uid,uid);
380         setuid(uid);
381 #endif
382
383 #if USE_SETREUID
384         setregid(gid,gid);
385         setgid(gid);
386         setreuid(uid,uid);
387         setuid(uid);
388 #endif
389
390 #if USE_SETEUID
391         setegid(gid);
392         setgid(gid);
393         setuid(uid);
394         seteuid(uid);
395         setuid(uid);
396 #endif
397
398 #if USE_SETUIDX
399         setgidx(ID_REAL, gid);
400         setgidx(ID_EFFECTIVE, gid);
401         setgid(gid);
402         setuidx(ID_REAL, uid);
403         setuidx(ID_EFFECTIVE, uid);
404         setuid(uid);
405 #endif
406         
407         assert_uid(uid, uid);
408         assert_gid(gid, gid);
409 }
410
411 #ifdef AUTOCONF_TEST
412
413 /****************************************************************************
414 this function just checks that we don't get ENOSYS back
415 ****************************************************************************/
416 static int have_syscall(void)
417 {
418         errno = 0;
419
420 #if USE_SETRESUID
421         setresuid(-1,-1,-1);
422 #endif
423
424 #if USE_SETREUID
425         setreuid(-1,-1);
426 #endif
427
428 #if USE_SETEUID
429         seteuid(-1);
430 #endif
431
432 #if USE_SETUIDX
433         setuidx(ID_EFFECTIVE, -1);
434 #endif
435
436         if (errno == ENOSYS) return -1;
437         
438         return 0;
439 }
440
441 main()
442 {
443         if (getuid() != 0) {
444 #if (defined(AIX) && defined(USE_SETREUID))
445                 /* setreuid is badly broken on AIX 4.1, we avoid it completely */
446                 fprintf(stderr,"avoiding possibly broken setreuid\n");
447                 exit(1);
448 #endif
449
450                 /* if not running as root then at least check to see if we get ENOSYS - this 
451                    handles Linux 2.0.x with glibc 2.1 */
452                 fprintf(stderr,"not running as root: checking for ENOSYS\n");
453                 exit(have_syscall());
454         }
455
456         gain_root_privilege();
457         gain_root_group_privilege();
458         set_effective_gid(1);
459         set_effective_uid(1);
460         save_re_uid();
461         restore_re_uid();
462         gain_root_privilege();
463         gain_root_group_privilege();
464         become_user_permanently(1, 1);
465         setuid(0);
466         if (getuid() == 0) {
467                 fprintf(stderr,"uid not set permanently\n");
468                 exit(1);
469         }
470
471         printf("OK\n");
472
473         exit(0);
474 }
475 #endif
476
477 /****************************************************************************
478 Check if we are setuid root.  Used in libsmb and smbpasswd paranoia checks.
479 ****************************************************************************/
480 BOOL is_setuid_root(void) 
481 {
482         return (geteuid() == (uid_t)0) && (getuid() != (uid_t)0);
483 }