Remove warning about trapdoor systems for non-root mode.
[kai/samba.git] / source3 / smbd / sec_ctx.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    uid/user handling
5    Copyright (C) Tim Potter 2000
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 #include "includes.h"
23
24 extern int DEBUGLEVEL;
25 extern struct current_user current_user;
26
27 struct sec_ctx {
28         uid_t uid;
29         uid_t gid;
30         int ngroups;
31         gid_t *groups;
32         NT_USER_TOKEN *token;
33 };
34
35 /* A stack of security contexts.  We include the current context as being
36    the first one, so there is room for another MAX_SEC_CTX_DEPTH more. */
37
38 static struct sec_ctx sec_ctx_stack[MAX_SEC_CTX_DEPTH + 1];
39 static int sec_ctx_stack_ndx;
40
41 /****************************************************************************
42  Become the specified uid.
43 ****************************************************************************/
44
45 static BOOL become_uid(uid_t uid)
46 {
47         /* Check for dodgy uid values */
48
49         if (uid == (uid_t)-1 || 
50             ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
51                 static int done;
52  
53                 if (!done) {
54                         DEBUG(1,("WARNING: using uid %d is a security risk\n",
55                                  (int)uid));
56                         done = 1;
57                 }
58         }
59
60         /* Set effective user id */
61
62         set_effective_uid(uid);
63         current_user.uid = uid;
64
65         DO_PROFILE_INC(uid_changes);
66
67         return True;
68 }
69
70 /****************************************************************************
71  Become the specified gid.
72 ****************************************************************************/
73
74 static BOOL become_gid(gid_t gid)
75 {
76         /* Check for dodgy gid values */
77
78         if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) && 
79                                  (gid == (gid_t)65535))) {
80                 static int done;
81                 
82                 if (!done) {
83                         DEBUG(1,("WARNING: using gid %d is a security risk\n",
84                                  (int)gid));  
85                         done = 1;
86                 }
87         }
88   
89         /* Set effective group id */
90
91         set_effective_gid(gid);
92         current_user.gid = gid;
93         
94         return True;
95 }
96
97 /****************************************************************************
98  Become the specified uid and gid.
99 ****************************************************************************/
100
101 static BOOL become_id(uid_t uid, gid_t gid)
102 {
103         return become_gid(gid) && become_uid(uid);
104 }
105
106 /****************************************************************************
107  Drop back to root privileges in order to change to another user.
108 ****************************************************************************/
109
110 static void gain_root(void)
111 {
112         if (non_root_mode()) {
113                 return;
114         }
115
116         if (geteuid() != 0) {
117                 set_effective_uid(0);
118
119                 if (geteuid() != 0) {
120                         DEBUG(0,
121                               ("Warning: You appear to have a trapdoor "
122                                "uid system\n"));
123                 }
124         }
125
126         if (getegid() != 0) {
127                 set_effective_gid(0);
128
129                 if (getegid() != 0) {
130                         DEBUG(0,
131                               ("Warning: You appear to have a trapdoor "
132                                "gid system\n"));
133                 }
134         }
135 }
136
137 /****************************************************************************
138  Get the list of current groups.
139 ****************************************************************************/
140
141 int get_current_groups(int *p_ngroups, gid_t **p_groups)
142 {
143         int i;
144         gid_t grp;
145         int ngroups = sys_getgroups(0,&grp);
146         gid_t *groups;
147
148         (*p_ngroups) = 0;
149         (*p_groups) = NULL;
150
151         if (ngroups <= 0)
152                 return -1;
153
154         if((groups = (gid_t *)malloc(sizeof(gid_t)*ngroups)) == NULL) {
155                 DEBUG(0,("setup_groups malloc fail !\n"));
156                 return -1;
157         }
158
159         if ((ngroups = sys_getgroups(ngroups,groups)) == -1) {
160                 safe_free(groups);
161                 return -1;
162         }
163
164         (*p_ngroups) = ngroups;
165         (*p_groups) = groups;
166
167         DEBUG( 3, ( "get_current_groups: uid %u is in %u groups: ", (unsigned int)getuid() , ngroups ) );
168         for (i = 0; i < ngroups; i++ ) {
169                 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
170         }
171         DEBUG( 3, ( "\n" ) );
172
173     return ngroups;
174 }
175
176 /****************************************************************************
177  Delete a SID token.
178 ****************************************************************************/
179
180 void delete_nt_token(NT_USER_TOKEN **pptoken)
181 {
182     if (*pptoken) {
183                 NT_USER_TOKEN *ptoken = *pptoken;
184         safe_free( ptoken->user_sids );
185         ZERO_STRUCTP(ptoken);
186     }
187     safe_free(*pptoken);
188         *pptoken = NULL;
189 }
190
191 /****************************************************************************
192  Duplicate a SID token.
193 ****************************************************************************/
194
195 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
196 {
197         NT_USER_TOKEN *token;
198
199         if (!ptoken)
200                 return NULL;
201
202     if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
203         return NULL;
204
205     ZERO_STRUCTP(token);
206
207     if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
208         free(token);
209         return NULL;
210     }
211
212     token->num_sids = ptoken->num_sids;
213
214         return token;
215 }
216
217 /****************************************************************************
218  Initialize the groups a user belongs to.
219 ****************************************************************************/
220
221 BOOL initialise_groups(char *user, uid_t uid, gid_t gid)
222 {
223         struct sec_ctx *prev_ctx_p;
224         BOOL result = True;
225
226         become_root();
227
228         /* Call initgroups() to get user groups */
229
230         if (winbind_initgroups(user,gid) == -1) {
231                 DEBUG(0,("Unable to initgroups. Error was %s\n", strerror(errno) ));
232                 if (getuid() == 0) {
233                         if (gid < 0 || gid > 32767 || uid < 0 || uid > 32767) {
234                                 DEBUG(0,("This is probably a problem with the account %s\n", user));
235                         }
236                 }
237                 result = False;
238                 goto done;
239         }
240
241         /* Store groups in previous user's security context.  This will
242            always work as the become_root() call increments the stack
243            pointer. */
244
245         prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx - 1];
246
247         safe_free(prev_ctx_p->groups);
248         prev_ctx_p->groups = NULL;
249         prev_ctx_p->ngroups = 0;
250
251         get_current_groups(&prev_ctx_p->ngroups, &prev_ctx_p->groups);
252
253  done:
254         unbecome_root();
255
256         return result;
257 }
258
259 /****************************************************************************
260  Create a new security context on the stack.  It is the same as the old
261  one.  User changes are done using the set_sec_ctx() function.
262 ****************************************************************************/
263
264 BOOL push_sec_ctx(void)
265 {
266         struct sec_ctx *ctx_p;
267
268         /* Check we don't overflow our stack */
269
270         if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
271                 DEBUG(0, ("Security context stack overflow!\n"));
272                 smb_panic("Security context stack overflow!\n");
273         }
274
275         /* Store previous user context */
276
277         sec_ctx_stack_ndx++;
278
279         ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
280
281         ctx_p->uid = geteuid();
282         ctx_p->gid = getegid();
283
284         DEBUG(3, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n", 
285                   (unsigned int)ctx_p->uid, (unsigned int)ctx_p->gid, sec_ctx_stack_ndx ));
286
287         ctx_p->token = dup_nt_token(sec_ctx_stack[sec_ctx_stack_ndx-1].token);
288
289         ctx_p->ngroups = sys_getgroups(0, NULL);
290
291         if (ctx_p->ngroups != 0) {
292                 if (!(ctx_p->groups = malloc(ctx_p->ngroups * sizeof(gid_t)))) {
293                         DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
294                         delete_nt_token(&ctx_p->token);
295                         return False;
296                 }
297
298                 sys_getgroups(ctx_p->ngroups, ctx_p->groups);
299         } else {
300                 ctx_p->groups = NULL;
301         }
302
303         return True;
304 }
305
306 /****************************************************************************
307  Set the current security context to a given user.
308 ****************************************************************************/
309
310 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, NT_USER_TOKEN *token)
311 {
312         struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
313         
314         /* Set the security context */
315
316         DEBUG(3, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n", 
317                 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
318
319         if (ngroups) {
320                 int i;
321
322                 DEBUG(3, ("%d user groups: \n", ngroups));
323                 for (i = 0; i < ngroups; i++) {
324                         DEBUGADD(3, ("%u ", (unsigned int)groups[i]));
325                 }
326
327                 DEBUG(3, ("\n"));
328         }
329         
330
331         gain_root();
332
333 #ifdef HAVE_SETGROUPS
334         sys_setgroups(ngroups, groups);
335 #endif
336
337         ctx_p->ngroups = ngroups;
338
339         safe_free(ctx_p->groups);
340         if (token && (token == ctx_p->token))
341                 smb_panic("DUPLICATE_TOKEN");
342
343         delete_nt_token(&ctx_p->token);
344         
345         ctx_p->groups = memdup(groups, sizeof(gid_t) * ngroups);
346         ctx_p->token = dup_nt_token(token);
347
348         become_id(uid, gid);
349
350         ctx_p->uid = uid;
351         ctx_p->gid = gid;
352
353         /* Update current_user stuff */
354
355         current_user.uid = uid;
356         current_user.gid = gid;
357         current_user.ngroups = ngroups;
358         current_user.groups = groups;
359         current_user.nt_user_token = token;
360 }
361
362 /****************************************************************************
363  Become root context.
364 ****************************************************************************/
365
366 void set_root_sec_ctx(void)
367 {
368         /* May need to worry about supplementary groups at some stage */
369
370         set_sec_ctx(0, 0, 0, NULL, NULL);
371 }
372
373 /****************************************************************************
374  Pop a security context from the stack.
375 ****************************************************************************/
376
377 BOOL pop_sec_ctx(void)
378 {
379         struct sec_ctx *ctx_p;
380         struct sec_ctx *prev_ctx_p;
381
382         /* Check for stack underflow */
383
384         if (sec_ctx_stack_ndx == 0) {
385                 DEBUG(0, ("Security context stack underflow!\n"));
386                 smb_panic("Security context stack underflow!\n");
387         }
388
389         ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
390
391         /* Clear previous user info */
392
393         ctx_p->uid = (uid_t)-1;
394         ctx_p->gid = (gid_t)-1;
395
396         safe_free(ctx_p->groups);
397         ctx_p->ngroups = 0;
398
399         delete_nt_token(&ctx_p->token);
400
401         /* Pop back previous user */
402
403         sec_ctx_stack_ndx--;
404
405         gain_root();
406
407         prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
408
409 #ifdef HAVE_SETGROUPS
410         sys_setgroups(prev_ctx_p->ngroups, prev_ctx_p->groups);
411 #endif
412
413         become_id(prev_ctx_p->uid, prev_ctx_p->gid);
414
415         /* Update current_user stuff */
416
417         current_user.uid = prev_ctx_p->uid;
418         current_user.gid = prev_ctx_p->gid;
419         current_user.ngroups = prev_ctx_p->ngroups;
420         current_user.groups = prev_ctx_p->groups;
421         current_user.nt_user_token = prev_ctx_p->token;
422
423         DEBUG(3, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n", 
424                 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
425
426         return True;
427 }
428
429 /* Initialise the security context system */
430
431 void init_sec_ctx(void)
432 {
433         int i;
434         struct sec_ctx *ctx_p;
435
436         /* Initialise security context stack */
437
438         memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
439
440         for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
441                 sec_ctx_stack[i].uid = (uid_t)-1;
442                 sec_ctx_stack[i].gid = (gid_t)-1;
443         }
444
445         /* Initialise first level of stack.  It is the current context */
446         ctx_p = &sec_ctx_stack[0];
447
448         ctx_p->uid = geteuid();
449         ctx_p->gid = getegid();
450
451         get_current_groups(&ctx_p->ngroups, &ctx_p->groups);
452
453         ctx_p->token = NULL; /* Maps to guest user. */
454
455         /* Initialise current_user global */
456
457         current_user.uid = ctx_p->uid;
458         current_user.gid = ctx_p->gid;
459         current_user.ngroups = ctx_p->ngroups;
460         current_user.groups = ctx_p->groups;
461
462         /* The conn and vuid are usually taken care of by other modules.
463            We initialise them here. */
464
465         current_user.conn = NULL;
466         current_user.vuid = UID_FIELD_INVALID;
467         current_user.nt_user_token = NULL;
468 }