2 Unix SMB/Netbios implementation.
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.
24 extern int DEBUGLEVEL;
25 extern struct current_user current_user;
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. */
38 static struct sec_ctx sec_ctx_stack[MAX_SEC_CTX_DEPTH + 1];
39 static int sec_ctx_stack_ndx;
41 /****************************************************************************
42 Become the specified uid.
43 ****************************************************************************/
45 static BOOL become_uid(uid_t uid)
47 /* Check for dodgy uid values */
49 if (uid == (uid_t)-1 ||
50 ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
54 DEBUG(1,("WARNING: using uid %d is a security risk\n",
60 /* Set effective user id */
62 set_effective_uid(uid);
63 current_user.uid = uid;
65 DO_PROFILE_INC(uid_changes);
70 /****************************************************************************
71 Become the specified gid.
72 ****************************************************************************/
74 static BOOL become_gid(gid_t gid)
76 /* Check for dodgy gid values */
78 if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) &&
79 (gid == (gid_t)65535))) {
83 DEBUG(1,("WARNING: using gid %d is a security risk\n",
89 /* Set effective group id */
91 set_effective_gid(gid);
92 current_user.gid = gid;
97 /****************************************************************************
98 Become the specified uid and gid.
99 ****************************************************************************/
101 static BOOL become_id(uid_t uid, gid_t gid)
103 return become_gid(gid) && become_uid(uid);
106 /****************************************************************************
107 Drop back to root privileges in order to change to another user.
108 ****************************************************************************/
110 static void gain_root(void)
112 if (non_root_mode()) {
116 if (geteuid() != 0) {
117 set_effective_uid(0);
119 if (geteuid() != 0) {
121 ("Warning: You appear to have a trapdoor "
126 if (getegid() != 0) {
127 set_effective_gid(0);
129 if (getegid() != 0) {
131 ("Warning: You appear to have a trapdoor "
137 /****************************************************************************
138 Get the list of current groups.
139 ****************************************************************************/
141 int get_current_groups(int *p_ngroups, gid_t **p_groups)
145 int ngroups = sys_getgroups(0,&grp);
154 if((groups = (gid_t *)malloc(sizeof(gid_t)*ngroups)) == NULL) {
155 DEBUG(0,("setup_groups malloc fail !\n"));
159 if ((ngroups = sys_getgroups(ngroups,groups)) == -1) {
164 (*p_ngroups) = ngroups;
165 (*p_groups) = groups;
167 DEBUG( 3, ( "get_current_groups: uid %u is in %u groups: ", (unsigned int)getuid() , ngroups ) );
168 for (i = 0; i < ngroups; i++ ) {
169 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
171 DEBUG( 3, ( "\n" ) );
176 /****************************************************************************
178 ****************************************************************************/
180 void delete_nt_token(NT_USER_TOKEN **pptoken)
183 NT_USER_TOKEN *ptoken = *pptoken;
184 SAFE_FREE( ptoken->user_sids );
185 ZERO_STRUCTP(ptoken);
190 /****************************************************************************
191 Duplicate a SID token.
192 ****************************************************************************/
194 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
196 NT_USER_TOKEN *token;
201 if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
206 if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
211 token->num_sids = ptoken->num_sids;
216 /****************************************************************************
217 Initialize the groups a user belongs to.
218 ****************************************************************************/
220 BOOL initialise_groups(char *user, uid_t uid, gid_t gid)
222 struct sec_ctx *prev_ctx_p;
225 if (non_root_mode()) {
231 /* Call initgroups() to get user groups */
233 if (winbind_initgroups(user,gid) == -1) {
234 DEBUG(0,("Unable to initgroups. Error was %s\n", strerror(errno) ));
236 if (gid < 0 || gid > 32767 || uid < 0 || uid > 32767) {
237 DEBUG(0,("This is probably a problem with the account %s\n", user));
244 /* Store groups in previous user's security context. This will
245 always work as the become_root() call increments the stack
248 prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx - 1];
250 SAFE_FREE(prev_ctx_p->groups);
251 prev_ctx_p->ngroups = 0;
253 get_current_groups(&prev_ctx_p->ngroups, &prev_ctx_p->groups);
261 /****************************************************************************
262 Create a new security context on the stack. It is the same as the old
263 one. User changes are done using the set_sec_ctx() function.
264 ****************************************************************************/
266 BOOL push_sec_ctx(void)
268 struct sec_ctx *ctx_p;
270 /* Check we don't overflow our stack */
272 if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
273 DEBUG(0, ("Security context stack overflow!\n"));
274 smb_panic("Security context stack overflow!\n");
277 /* Store previous user context */
281 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
283 ctx_p->uid = geteuid();
284 ctx_p->gid = getegid();
286 DEBUG(3, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n",
287 (unsigned int)ctx_p->uid, (unsigned int)ctx_p->gid, sec_ctx_stack_ndx ));
289 ctx_p->token = dup_nt_token(sec_ctx_stack[sec_ctx_stack_ndx-1].token);
291 ctx_p->ngroups = sys_getgroups(0, NULL);
293 if (ctx_p->ngroups != 0) {
294 if (!(ctx_p->groups = malloc(ctx_p->ngroups * sizeof(gid_t)))) {
295 DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
296 delete_nt_token(&ctx_p->token);
300 sys_getgroups(ctx_p->ngroups, ctx_p->groups);
302 ctx_p->groups = NULL;
308 /****************************************************************************
309 Set the current security context to a given user.
310 ****************************************************************************/
312 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, NT_USER_TOKEN *token)
314 struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
316 /* Set the security context */
318 DEBUG(3, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
319 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
324 DEBUG(3, ("%d user groups: \n", ngroups));
325 for (i = 0; i < ngroups; i++) {
326 DEBUGADD(3, ("%u ", (unsigned int)groups[i]));
335 #ifdef HAVE_SETGROUPS
336 sys_setgroups(ngroups, groups);
339 ctx_p->ngroups = ngroups;
341 SAFE_FREE(ctx_p->groups);
342 if (token && (token == ctx_p->token))
343 smb_panic("DUPLICATE_TOKEN");
345 delete_nt_token(&ctx_p->token);
347 ctx_p->groups = memdup(groups, sizeof(gid_t) * ngroups);
348 ctx_p->token = dup_nt_token(token);
355 /* Update current_user stuff */
357 current_user.uid = uid;
358 current_user.gid = gid;
359 current_user.ngroups = ngroups;
360 current_user.groups = groups;
361 current_user.nt_user_token = ctx_p->token;
364 /****************************************************************************
366 ****************************************************************************/
368 void set_root_sec_ctx(void)
370 /* May need to worry about supplementary groups at some stage */
372 set_sec_ctx(0, 0, 0, NULL, NULL);
375 /****************************************************************************
376 Pop a security context from the stack.
377 ****************************************************************************/
379 BOOL pop_sec_ctx(void)
381 struct sec_ctx *ctx_p;
382 struct sec_ctx *prev_ctx_p;
384 /* Check for stack underflow */
386 if (sec_ctx_stack_ndx == 0) {
387 DEBUG(0, ("Security context stack underflow!\n"));
388 smb_panic("Security context stack underflow!\n");
391 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
393 /* Clear previous user info */
395 ctx_p->uid = (uid_t)-1;
396 ctx_p->gid = (gid_t)-1;
398 SAFE_FREE(ctx_p->groups);
401 delete_nt_token(&ctx_p->token);
403 /* Pop back previous user */
409 prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
411 #ifdef HAVE_SETGROUPS
412 sys_setgroups(prev_ctx_p->ngroups, prev_ctx_p->groups);
415 become_id(prev_ctx_p->uid, prev_ctx_p->gid);
417 /* Update current_user stuff */
419 current_user.uid = prev_ctx_p->uid;
420 current_user.gid = prev_ctx_p->gid;
421 current_user.ngroups = prev_ctx_p->ngroups;
422 current_user.groups = prev_ctx_p->groups;
423 current_user.nt_user_token = prev_ctx_p->token;
425 DEBUG(3, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
426 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
431 /* Initialise the security context system */
433 void init_sec_ctx(void)
436 struct sec_ctx *ctx_p;
438 /* Initialise security context stack */
440 memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
442 for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
443 sec_ctx_stack[i].uid = (uid_t)-1;
444 sec_ctx_stack[i].gid = (gid_t)-1;
447 /* Initialise first level of stack. It is the current context */
448 ctx_p = &sec_ctx_stack[0];
450 ctx_p->uid = geteuid();
451 ctx_p->gid = getegid();
453 get_current_groups(&ctx_p->ngroups, &ctx_p->groups);
455 ctx_p->token = NULL; /* Maps to guest user. */
457 /* Initialise current_user global */
459 current_user.uid = ctx_p->uid;
460 current_user.gid = ctx_p->gid;
461 current_user.ngroups = ctx_p->ngroups;
462 current_user.groups = ctx_p->groups;
464 /* The conn and vuid are usually taken care of by other modules.
465 We initialise them here. */
467 current_user.conn = NULL;
468 current_user.vuid = UID_FIELD_INVALID;
469 current_user.nt_user_token = NULL;