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 3 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, see <http://www.gnu.org/licenses/>.
22 extern struct current_user current_user;
29 /* A stack of security contexts. We include the current context as being
30 the first one, so there is room for another MAX_SEC_CTX_DEPTH more. */
32 static struct sec_ctx sec_ctx_stack[MAX_SEC_CTX_DEPTH + 1];
33 static int sec_ctx_stack_ndx;
35 /****************************************************************************
36 Become the specified uid.
37 ****************************************************************************/
39 static BOOL become_uid(uid_t uid)
41 /* Check for dodgy uid values */
43 if (uid == (uid_t)-1 ||
44 ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
48 DEBUG(1,("WARNING: using uid %d is a security risk\n",
54 /* Set effective user id */
56 set_effective_uid(uid);
58 DO_PROFILE_INC(uid_changes);
62 /****************************************************************************
63 Become the specified gid.
64 ****************************************************************************/
66 static BOOL become_gid(gid_t gid)
68 /* Check for dodgy gid values */
70 if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) &&
71 (gid == (gid_t)65535))) {
75 DEBUG(1,("WARNING: using gid %d is a security risk\n",
81 /* Set effective group id */
83 set_effective_gid(gid);
87 /****************************************************************************
88 Become the specified uid and gid.
89 ****************************************************************************/
91 static BOOL become_id(uid_t uid, gid_t gid)
93 return become_gid(gid) && become_uid(uid);
96 /****************************************************************************
97 Drop back to root privileges in order to change to another user.
98 ****************************************************************************/
100 static void gain_root(void)
102 if (non_root_mode()) {
106 if (geteuid() != 0) {
107 set_effective_uid(0);
109 if (geteuid() != 0) {
111 ("Warning: You appear to have a trapdoor "
116 if (getegid() != 0) {
117 set_effective_gid(0);
119 if (getegid() != 0) {
121 ("Warning: You appear to have a trapdoor "
127 /****************************************************************************
128 Get the list of current groups.
129 ****************************************************************************/
131 static int get_current_groups(gid_t gid, int *p_ngroups, gid_t **p_groups)
136 gid_t *groups = NULL;
141 /* this looks a little strange, but is needed to cope with
142 systems that put the current egid in the group list
143 returned from getgroups() (tridge) */
145 set_effective_gid(gid);
148 ngroups = sys_getgroups(0,&grp);
153 if((groups = SMB_MALLOC_ARRAY(gid_t, ngroups+1)) == NULL) {
154 DEBUG(0,("setup_groups malloc fail !\n"));
158 if ((ngroups = sys_getgroups(ngroups,groups)) == -1) {
164 (*p_ngroups) = ngroups;
165 (*p_groups) = groups;
167 DEBUG( 3, ( "get_current_groups: user is in %u groups: ", ngroups));
168 for (i = 0; i < ngroups; i++ ) {
169 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
171 DEBUG( 3, ( "\n" ) );
181 /****************************************************************************
182 Create a new security context on the stack. It is the same as the old
183 one. User changes are done using the set_sec_ctx() function.
184 ****************************************************************************/
186 BOOL push_sec_ctx(void)
188 struct sec_ctx *ctx_p;
190 /* Check we don't overflow our stack */
192 if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
193 DEBUG(0, ("Security context stack overflow!\n"));
194 smb_panic("Security context stack overflow!");
197 /* Store previous user context */
201 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
203 ctx_p->ut.uid = geteuid();
204 ctx_p->ut.gid = getegid();
206 DEBUG(3, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n",
207 (unsigned int)ctx_p->ut.uid, (unsigned int)ctx_p->ut.gid, sec_ctx_stack_ndx ));
209 ctx_p->token = dup_nt_token(NULL,
210 sec_ctx_stack[sec_ctx_stack_ndx-1].token);
212 ctx_p->ut.ngroups = sys_getgroups(0, NULL);
214 if (ctx_p->ut.ngroups != 0) {
215 if (!(ctx_p->ut.groups = SMB_MALLOC_ARRAY(gid_t, ctx_p->ut.ngroups))) {
216 DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
217 TALLOC_FREE(ctx_p->token);
221 sys_getgroups(ctx_p->ut.ngroups, ctx_p->ut.groups);
223 ctx_p->ut.groups = NULL;
229 /****************************************************************************
230 Change UNIX security context. Calls panic if not successful so no return value.
231 ****************************************************************************/
233 #ifndef HAVE_DARWIN_INITGROUPS
235 /* Normal credential switch path. */
237 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
239 /* Start context switch */
241 #ifdef HAVE_SETGROUPS
242 sys_setgroups(gid, ngroups, groups);
245 /* end context switch */
248 #else /* HAVE_DARWIN_INITGROUPS */
250 /* The Darwin groups implementation is a little unusual. The list of
251 * groups in the kernel credential is not exhaustive, but more like
252 * a cache. The full group list is held in userspace and checked
255 * This is an optional mechanism, and setgroups(2) opts out
256 * of it. That is, if you call setgroups, then the list of groups you
257 * set are the only groups that are ever checked. This is not what we
258 * want. We want to opt in to the dynamic resolution mechanism, so we
259 * need to specify the uid of the user whose group list (cache) we are
262 * The Darwin rules are:
263 * 1. Thou shalt setegid, initgroups and seteuid IN THAT ORDER
264 * 2. Thou shalt not pass more that NGROUPS_MAX to initgroups
265 * 3. Thou shalt leave the first entry in the groups list well alone
268 #include <sys/syscall.h>
270 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
272 int max = groups_max();
274 /* Start context switch */
280 if (syscall(SYS_initgroups, (ngroups > max) ? max : ngroups,
282 DEBUG(0, ("WARNING: failed to set group list "
283 "(%d groups) for UID %ld: %s\n",
284 ngroups, uid, strerror(errno)));
288 /* end context switch */
291 #endif /* HAVE_DARWIN_INITGROUPS */
293 /****************************************************************************
294 Set the current security context to a given user.
295 ****************************************************************************/
297 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, NT_USER_TOKEN *token)
299 struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
301 /* Set the security context */
303 DEBUG(3, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
304 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
306 debug_nt_user_token(DBGC_CLASS, 5, token);
307 debug_unix_user_token(DBGC_CLASS, 5, uid, gid, ngroups, groups);
309 /* Change uid, gid and supplementary group list. */
310 set_unix_security_ctx(uid, gid, ngroups, groups);
312 ctx_p->ut.ngroups = ngroups;
314 SAFE_FREE(ctx_p->ut.groups);
315 if (token && (token == ctx_p->token)) {
316 smb_panic("DUPLICATE_TOKEN");
319 TALLOC_FREE(ctx_p->token);
322 ctx_p->ut.groups = (gid_t *)memdup(groups,
323 sizeof(gid_t) * ngroups);
324 if (!ctx_p->ut.groups) {
325 smb_panic("memdup failed");
328 ctx_p->ut.groups = NULL;
332 ctx_p->token = dup_nt_token(NULL, token);
334 smb_panic("dup_nt_token failed");
343 /* Update current_user stuff */
345 current_user.ut.uid = uid;
346 current_user.ut.gid = gid;
347 current_user.ut.ngroups = ngroups;
348 current_user.ut.groups = groups;
349 current_user.nt_user_token = ctx_p->token;
352 /****************************************************************************
354 ****************************************************************************/
356 void set_root_sec_ctx(void)
358 /* May need to worry about supplementary groups at some stage */
360 set_sec_ctx(0, 0, 0, NULL, NULL);
363 /****************************************************************************
364 Pop a security context from the stack.
365 ****************************************************************************/
367 BOOL pop_sec_ctx(void)
369 struct sec_ctx *ctx_p;
370 struct sec_ctx *prev_ctx_p;
372 /* Check for stack underflow */
374 if (sec_ctx_stack_ndx == 0) {
375 DEBUG(0, ("Security context stack underflow!\n"));
376 smb_panic("Security context stack underflow!");
379 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
381 /* Clear previous user info */
383 ctx_p->ut.uid = (uid_t)-1;
384 ctx_p->ut.gid = (gid_t)-1;
386 SAFE_FREE(ctx_p->ut.groups);
387 ctx_p->ut.ngroups = 0;
389 TALLOC_FREE(ctx_p->token);
391 /* Pop back previous user */
395 prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
397 /* Change uid, gid and supplementary group list. */
398 set_unix_security_ctx(prev_ctx_p->ut.uid,
400 prev_ctx_p->ut.ngroups,
401 prev_ctx_p->ut.groups);
403 /* Update current_user stuff */
405 current_user.ut.uid = prev_ctx_p->ut.uid;
406 current_user.ut.gid = prev_ctx_p->ut.gid;
407 current_user.ut.ngroups = prev_ctx_p->ut.ngroups;
408 current_user.ut.groups = prev_ctx_p->ut.groups;
409 current_user.nt_user_token = prev_ctx_p->token;
411 DEBUG(3, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
412 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
417 /* Initialise the security context system */
419 void init_sec_ctx(void)
422 struct sec_ctx *ctx_p;
424 /* Initialise security context stack */
426 memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
428 for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
429 sec_ctx_stack[i].ut.uid = (uid_t)-1;
430 sec_ctx_stack[i].ut.gid = (gid_t)-1;
433 /* Initialise first level of stack. It is the current context */
434 ctx_p = &sec_ctx_stack[0];
436 ctx_p->ut.uid = geteuid();
437 ctx_p->ut.gid = getegid();
439 get_current_groups(ctx_p->ut.gid, &ctx_p->ut.ngroups, &ctx_p->ut.groups);
441 ctx_p->token = NULL; /* Maps to guest user. */
443 /* Initialise current_user global */
445 current_user.ut.uid = ctx_p->ut.uid;
446 current_user.ut.gid = ctx_p->ut.gid;
447 current_user.ut.ngroups = ctx_p->ut.ngroups;
448 current_user.ut.groups = ctx_p->ut.groups;
450 /* The conn and vuid are usually taken care of by other modules.
451 We initialise them here. */
453 current_user.conn = NULL;
454 current_user.vuid = UID_FIELD_INVALID;
455 current_user.nt_user_token = NULL;