2 Unix SMB/CIFS implementation.
4 Copyright (C) Tim Potter 2000
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 extern struct current_user current_user;
33 /* A stack of security contexts. We include the current context as being
34 the first one, so there is room for another MAX_SEC_CTX_DEPTH more. */
36 static struct sec_ctx sec_ctx_stack[MAX_SEC_CTX_DEPTH + 1];
37 static int sec_ctx_stack_ndx;
39 /****************************************************************************
40 Become the specified uid.
41 ****************************************************************************/
43 static BOOL become_uid(uid_t uid)
45 /* Check for dodgy uid values */
47 if (uid == (uid_t)-1 ||
48 ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
52 DEBUG(1,("WARNING: using uid %d is a security risk\n",
58 /* Set effective user id */
60 set_effective_uid(uid);
62 DO_PROFILE_INC(uid_changes);
66 /****************************************************************************
67 Become the specified gid.
68 ****************************************************************************/
70 static BOOL become_gid(gid_t gid)
72 /* Check for dodgy gid values */
74 if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) &&
75 (gid == (gid_t)65535))) {
79 DEBUG(1,("WARNING: using gid %d is a security risk\n",
85 /* Set effective group id */
87 set_effective_gid(gid);
91 /****************************************************************************
92 Become the specified uid and gid.
93 ****************************************************************************/
95 static BOOL become_id(uid_t uid, gid_t gid)
97 return become_gid(gid) && become_uid(uid);
100 /****************************************************************************
101 Drop back to root privileges in order to change to another user.
102 ****************************************************************************/
104 static void gain_root(void)
106 if (non_root_mode()) {
110 if (geteuid() != 0) {
111 set_effective_uid(0);
113 if (geteuid() != 0) {
115 ("Warning: You appear to have a trapdoor "
120 if (getegid() != 0) {
121 set_effective_gid(0);
123 if (getegid() != 0) {
125 ("Warning: You appear to have a trapdoor "
131 /****************************************************************************
132 Get the list of current groups.
133 ****************************************************************************/
135 int get_current_groups(gid_t gid, int *p_ngroups, gid_t **p_groups)
140 gid_t *groups = NULL;
145 /* this looks a little strange, but is needed to cope with
146 systems that put the current egid in the group list
147 returned from getgroups() (tridge) */
149 set_effective_gid(gid);
152 ngroups = sys_getgroups(0,&grp);
157 if((groups = (gid_t *)malloc(sizeof(gid_t)*(ngroups+1))) == NULL) {
158 DEBUG(0,("setup_groups malloc fail !\n"));
162 if ((ngroups = sys_getgroups(ngroups,groups)) == -1) {
168 (*p_ngroups) = ngroups;
169 (*p_groups) = groups;
171 DEBUG( 3, ( "get_current_groups: user is in %u groups: ", ngroups));
172 for (i = 0; i < ngroups; i++ ) {
173 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
175 DEBUG( 3, ( "\n" ) );
185 /****************************************************************************
186 Initialize the groups a user belongs to.
187 ****************************************************************************/
189 BOOL initialise_groups(char *user, uid_t uid, gid_t gid)
191 struct sec_ctx *prev_ctx_p;
194 if (non_root_mode()) {
200 /* Call initgroups() to get user groups */
202 if (initgroups(user,gid) == -1) {
203 DEBUG(0,("Unable to initgroups. Error was %s\n", strerror(errno) ));
205 if (gid < 0 || gid > 32767 || uid < 0 || uid > 32767) {
206 DEBUG(0,("This is probably a problem with the account %s\n", user));
213 /* Store groups in previous user's security context. This will
214 always work as the become_root() call increments the stack
217 prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx - 1];
219 SAFE_FREE(prev_ctx_p->groups);
220 prev_ctx_p->ngroups = 0;
222 get_current_groups(gid, &prev_ctx_p->ngroups, &prev_ctx_p->groups);
230 /****************************************************************************
231 Create a new security context on the stack. It is the same as the old
232 one. User changes are done using the set_sec_ctx() function.
233 ****************************************************************************/
235 BOOL push_sec_ctx(void)
237 struct sec_ctx *ctx_p;
239 /* Check we don't overflow our stack */
241 if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
242 DEBUG(0, ("Security context stack overflow!\n"));
243 smb_panic("Security context stack overflow!\n");
246 /* Store previous user context */
250 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
252 ctx_p->uid = geteuid();
253 ctx_p->gid = getegid();
255 DEBUG(3, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n",
256 (unsigned int)ctx_p->uid, (unsigned int)ctx_p->gid, sec_ctx_stack_ndx ));
258 ctx_p->token = dup_nt_token(sec_ctx_stack[sec_ctx_stack_ndx-1].token);
260 ctx_p->ngroups = sys_getgroups(0, NULL);
262 if (ctx_p->ngroups != 0) {
263 if (!(ctx_p->groups = malloc(ctx_p->ngroups * sizeof(gid_t)))) {
264 DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
265 delete_nt_token(&ctx_p->token);
269 sys_getgroups(ctx_p->ngroups, ctx_p->groups);
271 ctx_p->groups = NULL;
277 /****************************************************************************
278 Set the current security context to a given user.
279 ****************************************************************************/
281 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, NT_USER_TOKEN *token)
283 struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
285 /* Set the security context */
287 DEBUG(3, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
288 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
293 DEBUG(3, ("%d user groups: \n", ngroups));
294 for (i = 0; i < ngroups; i++) {
295 DEBUGADD(3, ("%u ", (unsigned int)groups[i]));
304 #ifdef HAVE_SETGROUPS
305 sys_setgroups(ngroups, groups);
308 ctx_p->ngroups = ngroups;
310 SAFE_FREE(ctx_p->groups);
311 if (token && (token == ctx_p->token))
312 smb_panic("DUPLICATE_TOKEN");
314 delete_nt_token(&ctx_p->token);
316 ctx_p->groups = memdup(groups, sizeof(gid_t) * ngroups);
317 ctx_p->token = dup_nt_token(token);
324 /* Update current_user stuff */
326 current_user.uid = uid;
327 current_user.gid = gid;
328 current_user.ngroups = ngroups;
329 current_user.groups = groups;
330 current_user.nt_user_token = ctx_p->token;
333 /****************************************************************************
335 ****************************************************************************/
337 void set_root_sec_ctx(void)
339 /* May need to worry about supplementary groups at some stage */
341 set_sec_ctx(0, 0, 0, NULL, NULL);
344 /****************************************************************************
345 Pop a security context from the stack.
346 ****************************************************************************/
348 BOOL pop_sec_ctx(void)
350 struct sec_ctx *ctx_p;
351 struct sec_ctx *prev_ctx_p;
353 /* Check for stack underflow */
355 if (sec_ctx_stack_ndx == 0) {
356 DEBUG(0, ("Security context stack underflow!\n"));
357 smb_panic("Security context stack underflow!\n");
360 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
362 /* Clear previous user info */
364 ctx_p->uid = (uid_t)-1;
365 ctx_p->gid = (gid_t)-1;
367 SAFE_FREE(ctx_p->groups);
370 delete_nt_token(&ctx_p->token);
372 /* Pop back previous user */
378 prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
380 #ifdef HAVE_SETGROUPS
381 sys_setgroups(prev_ctx_p->ngroups, prev_ctx_p->groups);
384 become_id(prev_ctx_p->uid, prev_ctx_p->gid);
386 /* Update current_user stuff */
388 current_user.uid = prev_ctx_p->uid;
389 current_user.gid = prev_ctx_p->gid;
390 current_user.ngroups = prev_ctx_p->ngroups;
391 current_user.groups = prev_ctx_p->groups;
392 current_user.nt_user_token = prev_ctx_p->token;
394 DEBUG(3, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
395 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
400 /* Initialise the security context system */
402 void init_sec_ctx(void)
405 struct sec_ctx *ctx_p;
407 /* Initialise security context stack */
409 memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
411 for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
412 sec_ctx_stack[i].uid = (uid_t)-1;
413 sec_ctx_stack[i].gid = (gid_t)-1;
416 /* Initialise first level of stack. It is the current context */
417 ctx_p = &sec_ctx_stack[0];
419 ctx_p->uid = geteuid();
420 ctx_p->gid = getegid();
422 get_current_groups(ctx_p->gid, &ctx_p->ngroups, &ctx_p->groups);
424 ctx_p->token = NULL; /* Maps to guest user. */
426 /* Initialise current_user global */
428 current_user.uid = ctx_p->uid;
429 current_user.gid = ctx_p->gid;
430 current_user.ngroups = ctx_p->ngroups;
431 current_user.groups = ctx_p->groups;
433 /* The conn and vuid are usually taken care of by other modules.
434 We initialise them here. */
436 current_user.conn = NULL;
437 current_user.vuid = UID_FIELD_INVALID;
438 current_user.nt_user_token = NULL;