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