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 (geteuid() != 0) {
113 set_effective_uid(0);
115 if (geteuid() != 0) {
117 ("Warning: You appear to have a trapdoor "
122 if (getegid() != 0) {
123 set_effective_gid(0);
125 if (getegid() != 0) {
127 ("Warning: You appear to have a trapdoor "
133 /****************************************************************************
134 Get the list of current groups.
135 ****************************************************************************/
137 int get_current_groups(int *p_ngroups, gid_t **p_groups)
141 int ngroups = sys_getgroups(0,&grp);
150 if((groups = (gid_t *)malloc(sizeof(gid_t)*ngroups)) == NULL) {
151 DEBUG(0,("setup_groups malloc fail !\n"));
155 if ((ngroups = sys_getgroups(ngroups,groups)) == -1)
158 (*p_ngroups) = ngroups;
159 (*p_groups) = groups;
161 DEBUG( 3, ( "get_current_groups: uid %u is in %u groups: ", (unsigned int)getuid() , ngroups ) );
162 for (i = 0; i < ngroups; i++ ) {
163 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
165 DEBUG( 3, ( "\n" ) );
170 /****************************************************************************
172 ****************************************************************************/
174 void delete_nt_token(NT_USER_TOKEN **pptoken)
177 NT_USER_TOKEN *ptoken = *pptoken;
178 safe_free( ptoken->user_sids );
179 ZERO_STRUCTP(ptoken);
185 /****************************************************************************
186 Duplicate a SID token.
187 ****************************************************************************/
189 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
191 NT_USER_TOKEN *token;
196 if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
201 if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
206 token->num_sids = ptoken->num_sids;
211 /****************************************************************************
212 Initialize the groups a user belongs to.
213 ****************************************************************************/
215 BOOL initialise_groups(char *user, uid_t uid, gid_t gid)
217 struct sec_ctx *prev_ctx_p;
222 /* Call initgroups() to get user groups */
224 if (winbind_initgroups(user,gid) == -1) {
225 DEBUG(0,("Unable to initgroups. Error was %s\n", strerror(errno) ));
227 if (gid < 0 || gid > 32767 || uid < 0 || uid > 32767) {
228 DEBUG(0,("This is probably a problem with the account %s\n", user));
235 /* Store groups in previous user's security context. This will
236 always work as the become_root() call increments the stack
239 prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx - 1];
241 safe_free(prev_ctx_p->groups);
242 prev_ctx_p->groups = NULL;
243 prev_ctx_p->ngroups = 0;
245 get_current_groups(&prev_ctx_p->ngroups, &prev_ctx_p->groups);
253 /****************************************************************************
254 Create a new security context on the stack. It is the same as the old
255 one. User changes are done using the set_sec_ctx() function.
256 ****************************************************************************/
258 BOOL push_sec_ctx(void)
260 struct sec_ctx *ctx_p;
262 /* Check we don't overflow our stack */
264 if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
265 DEBUG(0, ("Security context stack overflow!\n"));
266 smb_panic("Security context stack overflow!\n");
269 /* Store previous user context */
273 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
275 ctx_p->uid = geteuid();
276 ctx_p->gid = getegid();
278 DEBUG(3, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n",
279 (unsigned int)ctx_p->uid, (unsigned int)ctx_p->gid, sec_ctx_stack_ndx ));
281 ctx_p->token = dup_nt_token(sec_ctx_stack[sec_ctx_stack_ndx-1].token);
283 ctx_p->ngroups = sys_getgroups(0, NULL);
285 if (ctx_p->ngroups != 0) {
286 if (!(ctx_p->groups = malloc(ctx_p->ngroups * sizeof(gid_t)))) {
287 DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
288 delete_nt_token(&ctx_p->token);
292 sys_getgroups(ctx_p->ngroups, ctx_p->groups);
294 ctx_p->groups = NULL;
300 /****************************************************************************
301 Set the current security context to a given user.
302 ****************************************************************************/
304 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, NT_USER_TOKEN *token)
306 struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
308 /* Set the security context */
310 DEBUG(3, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
311 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
316 DEBUG(3, ("%d user groups: \n", ngroups));
317 for (i = 0; i < ngroups; i++) {
318 DEBUGADD(3, ("%u ", (unsigned int)groups[i]));
327 #ifdef HAVE_SETGROUPS
328 sys_setgroups(ngroups, groups);
331 ctx_p->ngroups = ngroups;
333 safe_free(ctx_p->groups);
334 if (token && (token == ctx_p->token))
335 smb_panic("DUPLICATE_TOKEN");
337 delete_nt_token(&ctx_p->token);
339 ctx_p->groups = memdup(groups, sizeof(gid_t) * ngroups);
340 ctx_p->token = dup_nt_token(token);
347 /* Update current_user stuff */
349 current_user.uid = uid;
350 current_user.gid = gid;
351 current_user.ngroups = ngroups;
352 current_user.groups = groups;
353 current_user.nt_user_token = token;
356 /****************************************************************************
358 ****************************************************************************/
360 void set_root_sec_ctx(void)
362 /* May need to worry about supplementary groups at some stage */
364 set_sec_ctx(0, 0, 0, NULL, NULL);
367 /****************************************************************************
368 Pop a security context from the stack.
369 ****************************************************************************/
371 BOOL pop_sec_ctx(void)
373 struct sec_ctx *ctx_p;
374 struct sec_ctx *prev_ctx_p;
376 /* Check for stack underflow */
378 if (sec_ctx_stack_ndx == 0) {
379 DEBUG(0, ("Security context stack underflow!\n"));
380 smb_panic("Security context stack underflow!\n");
383 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
385 /* Clear previous user info */
387 ctx_p->uid = (uid_t)-1;
388 ctx_p->gid = (gid_t)-1;
390 safe_free(ctx_p->groups);
393 delete_nt_token(&ctx_p->token);
395 /* Pop back previous user */
401 prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
403 #ifdef HAVE_SETGROUPS
404 sys_setgroups(prev_ctx_p->ngroups, prev_ctx_p->groups);
407 become_id(prev_ctx_p->uid, prev_ctx_p->gid);
409 /* Update current_user stuff */
411 current_user.uid = prev_ctx_p->uid;
412 current_user.gid = prev_ctx_p->gid;
413 current_user.ngroups = prev_ctx_p->ngroups;
414 current_user.groups = prev_ctx_p->groups;
415 current_user.nt_user_token = prev_ctx_p->token;
417 DEBUG(3, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
418 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
423 /* Initialise the security context system */
425 void init_sec_ctx(void)
428 struct sec_ctx *ctx_p;
430 /* Initialise security context stack */
432 memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
434 for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
435 sec_ctx_stack[i].uid = (uid_t)-1;
436 sec_ctx_stack[i].gid = (gid_t)-1;
439 /* Initialise first level of stack. It is the current context */
440 ctx_p = &sec_ctx_stack[0];
442 ctx_p->uid = geteuid();
443 ctx_p->gid = getegid();
445 get_current_groups(&ctx_p->ngroups, &ctx_p->groups);
447 ctx_p->token = NULL; /* Maps to guest user. */
449 /* Initialise current_user global */
451 current_user.uid = ctx_p->uid;
452 current_user.gid = ctx_p->gid;
453 current_user.ngroups = ctx_p->ngroups;
454 current_user.groups = ctx_p->groups;
456 /* The conn and vuid are usually taken care of by other modules.
457 We initialise them here. */
459 current_user.conn = NULL;
460 current_user.vuid = UID_FIELD_INVALID;
461 current_user.nt_user_token = NULL;