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