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