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