Added an NT_USER_TOKEN structure that is copied/passed around associated
[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 #ifdef WITH_PROFILE
66         profile_p->uid_changes++;
67 #endif
68
69         return True;
70 }
71
72 /****************************************************************************
73  Become the specified gid.
74 ****************************************************************************/
75
76 static BOOL become_gid(gid_t gid)
77 {
78         /* Check for dodgy gid values */
79
80         if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) && 
81                                  (gid == (gid_t)65535))) {
82                 static int done;
83                 
84                 if (!done) {
85                         DEBUG(1,("WARNING: using gid %d is a security risk\n",
86                                  (int)gid));  
87                         done = 1;
88                 }
89         }
90   
91         /* Set effective group id */
92
93         set_effective_gid(gid);
94         current_user.gid = gid;
95         
96         return True;
97 }
98
99 /****************************************************************************
100  Become the specified uid and gid.
101 ****************************************************************************/
102
103 static BOOL become_id(uid_t uid, gid_t gid)
104 {
105         return become_gid(gid) && become_uid(uid);
106 }
107
108 /****************************************************************************
109  Drop back to root privileges in order to change to another user.
110 ****************************************************************************/
111
112 static void gain_root(void)
113 {
114         if (geteuid() != 0) {
115                 set_effective_uid(0);
116
117                 if (geteuid() != 0) {
118                         DEBUG(0,
119                               ("Warning: You appear to have a trapdoor "
120                                "uid system\n"));
121                 }
122         }
123
124         if (getegid() != 0) {
125                 set_effective_gid(0);
126
127                 if (getegid() != 0) {
128                         DEBUG(0,
129                               ("Warning: You appear to have a trapdoor "
130                                "gid system\n"));
131                 }
132         }
133 }
134
135 /****************************************************************************
136  Get the list of current groups.
137 ****************************************************************************/
138
139 int get_current_groups(int *p_ngroups, gid_t **p_groups)
140 {
141         int i;
142         gid_t grp;
143         int ngroups = sys_getgroups(0,&grp);
144         gid_t *groups;
145
146         (*p_ngroups) = 0;
147         (*p_groups) = NULL;
148
149         if (ngroups <= 0)
150                 return -1;
151
152         if((groups = (gid_t *)malloc(sizeof(gid_t)*ngroups)) == NULL) {
153                 DEBUG(0,("setup_groups malloc fail !\n"));
154                 return -1;
155         }
156
157         if ((ngroups = sys_getgroups(ngroups,groups)) == -1)
158                 return -1;
159
160         (*p_ngroups) = ngroups;
161         (*p_groups) = groups;
162
163         DEBUG( 3, ( "get_current_groups: uid %u is in %u groups: ", (unsigned int)getuid() , ngroups ) );
164         for (i = 0; i < ngroups; i++ ) {
165                 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
166         }
167         DEBUG( 3, ( "\n" ) );
168
169     return ngroups;
170 }
171
172 /****************************************************************************
173  Delete a SID token.
174 ****************************************************************************/
175
176 void delete_nt_token(NT_USER_TOKEN **pptoken)
177 {
178     if (*pptoken) {
179                 NT_USER_TOKEN *ptoken = *pptoken;
180         safe_free( ptoken->user_sids );
181         ZERO_STRUCTP(ptoken);
182     }
183     safe_free(*pptoken);
184         *pptoken = NULL;
185 }
186
187 /****************************************************************************
188  Duplicate a SID token.
189 ****************************************************************************/
190
191 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
192 {
193         NT_USER_TOKEN *token;
194
195         if (!ptoken)
196                 return NULL;
197
198     if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
199         return NULL;
200
201     ZERO_STRUCTP(token);
202
203     if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
204         free(token);
205         return NULL;
206     }
207
208     token->num_sids = ptoken->num_sids;
209
210         return token;
211 }
212
213 /****************************************************************************
214  Create a new security context on the stack.  It is the same as the old
215  one.  User changes are done using the set_sec_ctx() function.
216 ****************************************************************************/
217
218 BOOL push_sec_ctx(void)
219 {
220         struct sec_ctx *ctx_p;
221
222         /* Check we don't overflow our stack */
223
224         if (sec_ctx_stack_ndx == (MAX_SEC_CTX_DEPTH)) {
225                 DEBUG(0, ("Security context stack overflow!\n"));
226                 return False;
227         }
228
229         /* Store previous user context */
230
231         sec_ctx_stack_ndx++;
232
233         ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
234
235         ctx_p->uid = geteuid();
236         ctx_p->gid = getegid();
237
238         ctx_p->token = dup_nt_token(sec_ctx_stack[sec_ctx_stack_ndx-1].token);
239
240         ctx_p->ngroups = sys_getgroups(0, NULL);
241
242         if (ctx_p->ngroups != 0) {
243                 if (!(ctx_p->groups = malloc(ctx_p->ngroups * sizeof(gid_t)))) {
244                         DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
245                         delete_nt_token(&ctx_p->token);
246                         return False;
247                 }
248
249                 sys_getgroups(ctx_p->ngroups, ctx_p->groups);
250         } else {
251                 ctx_p->groups = NULL;
252         }
253
254         return True;
255 }
256
257 /****************************************************************************
258  Set the current security context to a given user.
259 ****************************************************************************/
260
261 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, NT_USER_TOKEN *token)
262 {
263         struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
264
265         /* Set the security context */
266
267         DEBUG(3, ("setting sec ctx (%d, %d)\n", uid, gid));
268
269         gain_root();
270
271 #ifdef HAVE_SETGROUPS
272         sys_setgroups(ngroups, groups);
273 #endif
274
275         ctx_p->ngroups = ngroups;
276
277         safe_free(ctx_p->groups);
278         delete_nt_token(&ctx_p->token);
279         
280         ctx_p->groups = memdup(groups, sizeof(gid_t) * ngroups);
281         ctx_p->token = dup_nt_token(token);
282
283         become_id(uid, gid);
284
285         ctx_p->uid = uid;
286         ctx_p->gid = gid;
287
288         /* Update current_user stuff */
289
290         current_user.uid = uid;
291         current_user.gid = gid;
292         current_user.ngroups = ngroups;
293         current_user.groups = groups;
294         current_user.nt_user_token = token;
295 }
296
297 /****************************************************************************
298  Become root context.
299 ****************************************************************************/
300
301 void set_root_sec_ctx(void)
302 {
303         /* May need to worry about supplementary groups at some stage */
304
305         set_sec_ctx(0, 0, 0, NULL, NULL);
306 }
307
308 /****************************************************************************
309  Pop a security context from the stack.
310 ****************************************************************************/
311
312 BOOL pop_sec_ctx(void)
313 {
314         struct sec_ctx *ctx_p;
315         struct sec_ctx *prev_ctx_p;
316
317         /* Check for stack underflow */
318
319         if (sec_ctx_stack_ndx == 0) {
320                 DEBUG(0, ("Security context stack underflow!\n"));
321                 return False;
322         }
323
324         ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
325
326         /* Clear previous user info */
327
328         ctx_p->uid = (uid_t)-1;
329         ctx_p->gid = (gid_t)-1;
330
331         safe_free(ctx_p->groups);
332         ctx_p->ngroups = 0;
333
334         delete_nt_token(&ctx_p->token);
335
336         /* Pop back previous user */
337
338         sec_ctx_stack_ndx--;
339
340         gain_root();
341
342         prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
343
344 #ifdef HAVE_SETGROUPS
345         sys_setgroups(prev_ctx_p->ngroups, prev_ctx_p->groups);
346 #endif
347
348         become_id(prev_ctx_p->uid, prev_ctx_p->gid);
349
350         /* Update current_user stuff */
351
352         current_user.uid = prev_ctx_p->uid;
353         current_user.gid = prev_ctx_p->gid;
354         current_user.ngroups = prev_ctx_p->ngroups;
355         current_user.groups = prev_ctx_p->groups;
356         current_user.nt_user_token = prev_ctx_p->token;
357
358         DEBUG(3, ("popped off to sec ctx (%d, %d)\n", geteuid(), getegid()));
359
360         return True;
361 }
362
363 /* Initialise the security context system */
364
365 void init_sec_ctx(void)
366 {
367         int i;
368         struct sec_ctx *ctx_p;
369
370         /* Initialise security context stack */
371
372         memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
373
374         for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
375                 sec_ctx_stack[i].uid = (uid_t)-1;
376                 sec_ctx_stack[i].gid = (gid_t)-1;
377         }
378
379         /* Initialise first level of stack.  It is the current context */
380         ctx_p = &sec_ctx_stack[0];
381
382         ctx_p->uid = geteuid();
383         ctx_p->gid = getegid();
384
385         get_current_groups(&ctx_p->ngroups, &ctx_p->groups);
386
387         ctx_p->token = NULL; /* Maps to guest user. */
388
389         /* Initialise current_user global */
390
391         current_user.uid = ctx_p->uid;
392         current_user.gid = ctx_p->gid;
393         current_user.ngroups = ctx_p->ngroups;
394         current_user.groups = ctx_p->groups;
395
396         /* The conn and vuid are usually taken care of by other modules.
397            We initialise them here. */
398
399         current_user.conn = NULL;
400         current_user.vuid = UID_FIELD_INVALID;
401         current_user.nt_user_token = NULL;
402 }