Fix spurious check of unix home directory during session_setup when add user script...
[Samba.git] / source / smbd / sec_ctx.c
blobc93ca5c438987f67bbae38bec45f4c4ac6982857
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 uid/user handling
5 Copyright (C) Tim Potter 2000
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.
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.
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.
22 #include "includes.h"
24 extern struct current_user current_user;
26 struct sec_ctx {
27 uid_t uid;
28 uid_t gid;
29 int ngroups;
30 gid_t *groups;
31 NT_USER_TOKEN *token;
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. */
37 static struct sec_ctx sec_ctx_stack[MAX_SEC_CTX_DEPTH + 1];
38 static int sec_ctx_stack_ndx;
40 /****************************************************************************
41 Become the specified uid.
42 ****************************************************************************/
44 static BOOL become_uid(uid_t uid)
46 /* Check for dodgy uid values */
48 if (uid == (uid_t)-1 ||
49 ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
50 static int done;
52 if (!done) {
53 DEBUG(1,("WARNING: using uid %d is a security risk\n",
54 (int)uid));
55 done = 1;
59 /* Set effective user id */
61 set_effective_uid(uid);
63 DO_PROFILE_INC(uid_changes);
64 return True;
67 /****************************************************************************
68 Become the specified gid.
69 ****************************************************************************/
71 static BOOL become_gid(gid_t gid)
73 /* Check for dodgy gid values */
75 if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) &&
76 (gid == (gid_t)65535))) {
77 static int done;
79 if (!done) {
80 DEBUG(1,("WARNING: using gid %d is a security risk\n",
81 (int)gid));
82 done = 1;
86 /* Set effective group id */
88 set_effective_gid(gid);
89 return True;
92 /****************************************************************************
93 Become the specified uid and gid.
94 ****************************************************************************/
96 static BOOL become_id(uid_t uid, gid_t gid)
98 return become_gid(gid) && become_uid(uid);
101 /****************************************************************************
102 Drop back to root privileges in order to change to another user.
103 ****************************************************************************/
105 static void gain_root(void)
107 if (non_root_mode()) {
108 return;
111 if (geteuid() != 0) {
112 set_effective_uid(0);
114 if (geteuid() != 0) {
115 DEBUG(0,
116 ("Warning: You appear to have a trapdoor "
117 "uid system\n"));
121 if (getegid() != 0) {
122 set_effective_gid(0);
124 if (getegid() != 0) {
125 DEBUG(0,
126 ("Warning: You appear to have a trapdoor "
127 "gid system\n"));
132 /****************************************************************************
133 Get the list of current groups.
134 ****************************************************************************/
136 int get_current_groups(gid_t gid, int *p_ngroups, gid_t **p_groups)
138 int i;
139 gid_t grp;
140 int ngroups;
141 gid_t *groups = NULL;
143 (*p_ngroups) = 0;
144 (*p_groups) = NULL;
146 /* this looks a little strange, but is needed to cope with
147 systems that put the current egid in the group list
148 returned from getgroups() (tridge) */
149 save_re_gid();
150 set_effective_gid(gid);
151 setgid(gid);
153 ngroups = sys_getgroups(0,&grp);
154 if (ngroups <= 0) {
155 goto fail;
158 if((groups = (gid_t *)malloc(sizeof(gid_t)*(ngroups+1))) == NULL) {
159 DEBUG(0,("setup_groups malloc fail !\n"));
160 goto fail;
163 if ((ngroups = sys_getgroups(ngroups,groups)) == -1) {
164 goto fail;
167 restore_re_gid();
169 (*p_ngroups) = ngroups;
170 (*p_groups) = groups;
172 DEBUG( 3, ( "get_current_groups: user is in %u groups: ", ngroups ) );
173 for (i = 0; i < ngroups; i++ ) {
174 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
176 DEBUG( 3, ( "\n" ) );
178 return ngroups;
180 fail:
181 SAFE_FREE(groups);
182 restore_re_gid();
183 return -1;
186 /****************************************************************************
187 Delete a SID token.
188 ****************************************************************************/
190 void delete_nt_token(NT_USER_TOKEN **pptoken)
192 if (*pptoken) {
193 NT_USER_TOKEN *ptoken = *pptoken;
194 SAFE_FREE( ptoken->user_sids );
195 ZERO_STRUCTP(ptoken);
197 SAFE_FREE(*pptoken);
200 /****************************************************************************
201 Duplicate a SID token.
202 ****************************************************************************/
204 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
206 NT_USER_TOKEN *token;
208 if (!ptoken)
209 return NULL;
211 if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
212 return NULL;
214 ZERO_STRUCTP(token);
216 if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
217 SAFE_FREE(token);
218 return NULL;
221 token->num_sids = ptoken->num_sids;
223 return token;
226 /****************************************************************************
227 Initialize the groups a user belongs to.
228 ****************************************************************************/
230 BOOL initialise_groups(char *user, uid_t uid, gid_t gid)
232 struct sec_ctx *prev_ctx_p;
233 BOOL result = True;
235 if (non_root_mode()) {
236 return True;
239 become_root();
241 /* Call initgroups() to get user groups */
243 if (winbind_initgroups(user,gid) == -1) {
244 DEBUG(0,("Unable to initgroups. Error was %s\n", strerror(errno) ));
245 if (getuid() == 0) {
246 if (gid < 0 || gid > 32767 || uid < 0 || uid > 32767) {
247 DEBUG(0,("This is probably a problem with the account %s\n", user));
250 result = False;
251 goto done;
254 /* Store groups in previous user's security context. This will
255 always work as the become_root() call increments the stack
256 pointer. */
258 prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx - 1];
260 SAFE_FREE(prev_ctx_p->groups);
261 prev_ctx_p->ngroups = 0;
263 get_current_groups(gid, &prev_ctx_p->ngroups, &prev_ctx_p->groups);
265 done:
266 unbecome_root();
268 return result;
271 /****************************************************************************
272 Create a new security context on the stack. It is the same as the old
273 one. User changes are done using the set_sec_ctx() function.
274 ****************************************************************************/
276 BOOL push_sec_ctx(void)
278 struct sec_ctx *ctx_p;
280 /* Check we don't overflow our stack */
282 if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
283 DEBUG(0, ("Security context stack overflow!\n"));
284 smb_panic("Security context stack overflow!\n");
287 /* Store previous user context */
289 sec_ctx_stack_ndx++;
291 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
293 ctx_p->uid = geteuid();
294 ctx_p->gid = getegid();
296 DEBUG(3, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n",
297 (unsigned int)ctx_p->uid, (unsigned int)ctx_p->gid, sec_ctx_stack_ndx ));
299 ctx_p->token = dup_nt_token(sec_ctx_stack[sec_ctx_stack_ndx-1].token);
301 ctx_p->ngroups = sys_getgroups(0, NULL);
303 if (ctx_p->ngroups != 0) {
304 if (!(ctx_p->groups = malloc(ctx_p->ngroups * sizeof(gid_t)))) {
305 DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
306 delete_nt_token(&ctx_p->token);
307 return False;
310 sys_getgroups(ctx_p->ngroups, ctx_p->groups);
311 } else {
312 ctx_p->groups = NULL;
315 return True;
318 /****************************************************************************
319 Set the current security context to a given user.
320 ****************************************************************************/
322 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, NT_USER_TOKEN *token)
324 struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
326 /* Set the security context */
328 DEBUG(3, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
329 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
331 if (ngroups) {
332 int i;
334 DEBUG(3, ("%d user groups: \n", ngroups));
335 for (i = 0; i < ngroups; i++) {
336 DEBUGADD(3, ("%u ", (unsigned int)groups[i]));
339 DEBUG(3, ("\n"));
343 gain_root();
345 #ifdef HAVE_SETGROUPS
346 sys_setgroups(ngroups, groups);
347 #endif
349 ctx_p->ngroups = ngroups;
351 SAFE_FREE(ctx_p->groups);
352 if (token && (token == ctx_p->token))
353 smb_panic("DUPLICATE_TOKEN");
355 delete_nt_token(&ctx_p->token);
357 ctx_p->groups = memdup(groups, sizeof(gid_t) * ngroups);
358 ctx_p->token = dup_nt_token(token);
360 become_id(uid, gid);
362 ctx_p->uid = uid;
363 ctx_p->gid = gid;
365 /* Update current_user stuff */
367 current_user.uid = uid;
368 current_user.gid = gid;
369 current_user.ngroups = ngroups;
370 current_user.groups = groups;
371 current_user.nt_user_token = ctx_p->token;
374 /****************************************************************************
375 Become root context.
376 ****************************************************************************/
378 void set_root_sec_ctx(void)
380 /* May need to worry about supplementary groups at some stage */
382 set_sec_ctx(0, 0, 0, NULL, NULL);
385 /****************************************************************************
386 Pop a security context from the stack.
387 ****************************************************************************/
389 BOOL pop_sec_ctx(void)
391 struct sec_ctx *ctx_p;
392 struct sec_ctx *prev_ctx_p;
394 /* Check for stack underflow */
396 if (sec_ctx_stack_ndx == 0) {
397 DEBUG(0, ("Security context stack underflow!\n"));
398 smb_panic("Security context stack underflow!\n");
401 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
403 /* Clear previous user info */
405 ctx_p->uid = (uid_t)-1;
406 ctx_p->gid = (gid_t)-1;
408 SAFE_FREE(ctx_p->groups);
409 ctx_p->ngroups = 0;
411 delete_nt_token(&ctx_p->token);
413 /* Pop back previous user */
415 sec_ctx_stack_ndx--;
417 gain_root();
419 prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
421 #ifdef HAVE_SETGROUPS
422 sys_setgroups(prev_ctx_p->ngroups, prev_ctx_p->groups);
423 #endif
425 become_id(prev_ctx_p->uid, prev_ctx_p->gid);
427 /* Update current_user stuff */
429 current_user.uid = prev_ctx_p->uid;
430 current_user.gid = prev_ctx_p->gid;
431 current_user.ngroups = prev_ctx_p->ngroups;
432 current_user.groups = prev_ctx_p->groups;
433 current_user.nt_user_token = prev_ctx_p->token;
435 DEBUG(3, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
436 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
438 return True;
441 /* Initialise the security context system */
443 void init_sec_ctx(void)
445 int i;
446 struct sec_ctx *ctx_p;
448 /* Initialise security context stack */
450 memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
452 for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
453 sec_ctx_stack[i].uid = (uid_t)-1;
454 sec_ctx_stack[i].gid = (gid_t)-1;
457 /* Initialise first level of stack. It is the current context */
458 ctx_p = &sec_ctx_stack[0];
460 ctx_p->uid = geteuid();
461 ctx_p->gid = getegid();
463 get_current_groups(ctx_p->gid, &ctx_p->ngroups, &ctx_p->groups);
465 ctx_p->token = NULL; /* Maps to guest user. */
467 /* Initialise current_user global */
469 current_user.uid = ctx_p->uid;
470 current_user.gid = ctx_p->gid;
471 current_user.ngroups = ctx_p->ngroups;
472 current_user.groups = ctx_p->groups;
474 /* The conn and vuid are usually taken care of by other modules.
475 We initialise them here. */
477 current_user.conn = NULL;
478 current_user.vuid = UID_FIELD_INVALID;
479 current_user.nt_user_token = NULL;