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