rpcclient/rpcclient.c: Non-void return in void function.
[tprouty/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 (geteuid() != 0) {
113                 set_effective_uid(0);
114
115                 if (geteuid() != 0) {
116                         DEBUG(0,
117                               ("Warning: You appear to have a trapdoor "
118                                "uid system\n"));
119                 }
120         }
121
122         if (getegid() != 0) {
123                 set_effective_gid(0);
124
125                 if (getegid() != 0) {
126                         DEBUG(0,
127                               ("Warning: You appear to have a trapdoor "
128                                "gid system\n"));
129                 }
130         }
131 }
132
133 /****************************************************************************
134  Get the list of current groups.
135 ****************************************************************************/
136
137 int get_current_groups(int *p_ngroups, gid_t **p_groups)
138 {
139         int i;
140         gid_t grp;
141         int ngroups = sys_getgroups(0,&grp);
142         gid_t *groups;
143
144         (*p_ngroups) = 0;
145         (*p_groups) = NULL;
146
147         if (ngroups <= 0)
148                 return -1;
149
150         if((groups = (gid_t *)malloc(sizeof(gid_t)*ngroups)) == NULL) {
151                 DEBUG(0,("setup_groups malloc fail !\n"));
152                 return -1;
153         }
154
155         if ((ngroups = sys_getgroups(ngroups,groups)) == -1) {
156                 safe_free(groups);
157                 return -1;
158         }
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  Initialize the groups a user belongs to.
215 ****************************************************************************/
216
217 BOOL initialise_groups(char *user, uid_t uid, gid_t gid)
218 {
219         struct sec_ctx *prev_ctx_p;
220         BOOL result = True;
221
222         become_root();
223
224         /* Call initgroups() to get user groups */
225
226         if (winbind_initgroups(user,gid) == -1) {
227                 DEBUG(0,("Unable to initgroups. Error was %s\n", strerror(errno) ));
228                 if (getuid() == 0) {
229                         if (gid < 0 || gid > 32767 || uid < 0 || uid > 32767) {
230                                 DEBUG(0,("This is probably a problem with the account %s\n", user));
231                         }
232                 }
233                 result = False;
234                 goto done;
235         }
236
237         /* Store groups in previous user's security context.  This will
238            always work as the become_root() call increments the stack
239            pointer. */
240
241         prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx - 1];
242
243         safe_free(prev_ctx_p->groups);
244         prev_ctx_p->groups = NULL;
245         prev_ctx_p->ngroups = 0;
246
247         get_current_groups(&prev_ctx_p->ngroups, &prev_ctx_p->groups);
248
249  done:
250         unbecome_root();
251
252         return result;
253 }
254
255 /****************************************************************************
256  Create a new security context on the stack.  It is the same as the old
257  one.  User changes are done using the set_sec_ctx() function.
258 ****************************************************************************/
259
260 BOOL push_sec_ctx(void)
261 {
262         struct sec_ctx *ctx_p;
263
264         /* Check we don't overflow our stack */
265
266         if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
267                 DEBUG(0, ("Security context stack overflow!\n"));
268                 smb_panic("Security context stack overflow!\n");
269         }
270
271         /* Store previous user context */
272
273         sec_ctx_stack_ndx++;
274
275         ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
276
277         ctx_p->uid = geteuid();
278         ctx_p->gid = getegid();
279
280         DEBUG(3, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n", 
281                   (unsigned int)ctx_p->uid, (unsigned int)ctx_p->gid, sec_ctx_stack_ndx ));
282
283         ctx_p->token = dup_nt_token(sec_ctx_stack[sec_ctx_stack_ndx-1].token);
284
285         ctx_p->ngroups = sys_getgroups(0, NULL);
286
287         if (ctx_p->ngroups != 0) {
288                 if (!(ctx_p->groups = malloc(ctx_p->ngroups * sizeof(gid_t)))) {
289                         DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
290                         delete_nt_token(&ctx_p->token);
291                         return False;
292                 }
293
294                 sys_getgroups(ctx_p->ngroups, ctx_p->groups);
295         } else {
296                 ctx_p->groups = NULL;
297         }
298
299         return True;
300 }
301
302 /****************************************************************************
303  Set the current security context to a given user.
304 ****************************************************************************/
305
306 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, NT_USER_TOKEN *token)
307 {
308         struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
309         
310         /* Set the security context */
311
312         DEBUG(3, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n", 
313                 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
314
315         if (ngroups) {
316                 int i;
317
318                 DEBUG(3, ("%d user groups: \n", ngroups));
319                 for (i = 0; i < ngroups; i++) {
320                         DEBUGADD(3, ("%u ", (unsigned int)groups[i]));
321                 }
322
323                 DEBUG(3, ("\n"));
324         }
325         
326
327         gain_root();
328
329 #ifdef HAVE_SETGROUPS
330         sys_setgroups(ngroups, groups);
331 #endif
332
333         ctx_p->ngroups = ngroups;
334
335         safe_free(ctx_p->groups);
336         if (token && (token == ctx_p->token))
337                 smb_panic("DUPLICATE_TOKEN");
338
339         delete_nt_token(&ctx_p->token);
340         
341         ctx_p->groups = memdup(groups, sizeof(gid_t) * ngroups);
342         ctx_p->token = dup_nt_token(token);
343
344         become_id(uid, gid);
345
346         ctx_p->uid = uid;
347         ctx_p->gid = gid;
348
349         /* Update current_user stuff */
350
351         current_user.uid = uid;
352         current_user.gid = gid;
353         current_user.ngroups = ngroups;
354         current_user.groups = groups;
355         current_user.nt_user_token = token;
356 }
357
358 /****************************************************************************
359  Become root context.
360 ****************************************************************************/
361
362 void set_root_sec_ctx(void)
363 {
364         /* May need to worry about supplementary groups at some stage */
365
366         set_sec_ctx(0, 0, 0, NULL, NULL);
367 }
368
369 /****************************************************************************
370  Pop a security context from the stack.
371 ****************************************************************************/
372
373 BOOL pop_sec_ctx(void)
374 {
375         struct sec_ctx *ctx_p;
376         struct sec_ctx *prev_ctx_p;
377
378         /* Check for stack underflow */
379
380         if (sec_ctx_stack_ndx == 0) {
381                 DEBUG(0, ("Security context stack underflow!\n"));
382                 smb_panic("Security context stack underflow!\n");
383         }
384
385         ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
386
387         /* Clear previous user info */
388
389         ctx_p->uid = (uid_t)-1;
390         ctx_p->gid = (gid_t)-1;
391
392         safe_free(ctx_p->groups);
393         ctx_p->ngroups = 0;
394
395         delete_nt_token(&ctx_p->token);
396
397         /* Pop back previous user */
398
399         sec_ctx_stack_ndx--;
400
401         gain_root();
402
403         prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
404
405 #ifdef HAVE_SETGROUPS
406         sys_setgroups(prev_ctx_p->ngroups, prev_ctx_p->groups);
407 #endif
408
409         become_id(prev_ctx_p->uid, prev_ctx_p->gid);
410
411         /* Update current_user stuff */
412
413         current_user.uid = prev_ctx_p->uid;
414         current_user.gid = prev_ctx_p->gid;
415         current_user.ngroups = prev_ctx_p->ngroups;
416         current_user.groups = prev_ctx_p->groups;
417         current_user.nt_user_token = prev_ctx_p->token;
418
419         DEBUG(3, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n", 
420                 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
421
422         return True;
423 }
424
425 /* Initialise the security context system */
426
427 void init_sec_ctx(void)
428 {
429         int i;
430         struct sec_ctx *ctx_p;
431
432         /* Initialise security context stack */
433
434         memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
435
436         for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
437                 sec_ctx_stack[i].uid = (uid_t)-1;
438                 sec_ctx_stack[i].gid = (gid_t)-1;
439         }
440
441         /* Initialise first level of stack.  It is the current context */
442         ctx_p = &sec_ctx_stack[0];
443
444         ctx_p->uid = geteuid();
445         ctx_p->gid = getegid();
446
447         get_current_groups(&ctx_p->ngroups, &ctx_p->groups);
448
449         ctx_p->token = NULL; /* Maps to guest user. */
450
451         /* Initialise current_user global */
452
453         current_user.uid = ctx_p->uid;
454         current_user.gid = ctx_p->gid;
455         current_user.ngroups = ctx_p->ngroups;
456         current_user.groups = ctx_p->groups;
457
458         /* The conn and vuid are usually taken care of by other modules.
459            We initialise them here. */
460
461         current_user.conn = NULL;
462         current_user.vuid = UID_FIELD_INVALID;
463         current_user.nt_user_token = NULL;
464 }