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 if (sys_setgroups(gid, ngroups, groups) != 0) {
243 smb_panic("sys_setgroups failed");
247 /* end context switch */
250 #else /* HAVE_DARWIN_INITGROUPS */
252 /* The Darwin groups implementation is a little unusual. The list of
253 * groups in the kernel credential is not exhaustive, but more like
254 * a cache. The full group list is held in userspace and checked
257 * This is an optional mechanism, and setgroups(2) opts out
258 * of it. That is, if you call setgroups, then the list of groups you
259 * set are the only groups that are ever checked. This is not what we
260 * want. We want to opt in to the dynamic resolution mechanism, so we
261 * need to specify the uid of the user whose group list (cache) we are
264 * The Darwin rules are:
265 * 1. Thou shalt setegid, initgroups and seteuid IN THAT ORDER
266 * 2. Thou shalt not pass more that NGROUPS_MAX to initgroups
267 * 3. Thou shalt leave the first entry in the groups list well alone
270 #include <sys/syscall.h>
272 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
274 int max = groups_max();
276 /* Start context switch */
282 if (syscall(SYS_initgroups, (ngroups > max) ? max : ngroups,
284 DEBUG(0, ("WARNING: failed to set group list "
285 "(%d groups) for UID %ld: %s\n",
286 ngroups, uid, strerror(errno)));
287 smb_panic("sys_setgroups failed");
291 /* end context switch */
294 #endif /* HAVE_DARWIN_INITGROUPS */
296 /****************************************************************************
297 Set the current security context to a given user.
298 ****************************************************************************/
300 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, NT_USER_TOKEN *token)
302 struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
304 /* Set the security context */
306 DEBUG(3, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
307 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
309 debug_nt_user_token(DBGC_CLASS, 5, token);
310 debug_unix_user_token(DBGC_CLASS, 5, uid, gid, ngroups, groups);
312 /* Change uid, gid and supplementary group list. */
313 set_unix_security_ctx(uid, gid, ngroups, groups);
315 ctx_p->ut.ngroups = ngroups;
317 SAFE_FREE(ctx_p->ut.groups);
318 if (token && (token == ctx_p->token)) {
319 smb_panic("DUPLICATE_TOKEN");
322 TALLOC_FREE(ctx_p->token);
325 ctx_p->ut.groups = (gid_t *)memdup(groups,
326 sizeof(gid_t) * ngroups);
327 if (!ctx_p->ut.groups) {
328 smb_panic("memdup failed");
331 ctx_p->ut.groups = NULL;
335 ctx_p->token = dup_nt_token(NULL, token);
337 smb_panic("dup_nt_token failed");
346 /* Update current_user stuff */
348 current_user.ut.uid = uid;
349 current_user.ut.gid = gid;
350 current_user.ut.ngroups = ngroups;
351 current_user.ut.groups = groups;
352 current_user.nt_user_token = ctx_p->token;
355 /****************************************************************************
357 ****************************************************************************/
359 void set_root_sec_ctx(void)
361 /* May need to worry about supplementary groups at some stage */
363 set_sec_ctx(0, 0, 0, NULL, NULL);
366 /****************************************************************************
367 Pop a security context from the stack.
368 ****************************************************************************/
370 BOOL pop_sec_ctx(void)
372 struct sec_ctx *ctx_p;
373 struct sec_ctx *prev_ctx_p;
375 /* Check for stack underflow */
377 if (sec_ctx_stack_ndx == 0) {
378 DEBUG(0, ("Security context stack underflow!\n"));
379 smb_panic("Security context stack underflow!");
382 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
384 /* Clear previous user info */
386 ctx_p->ut.uid = (uid_t)-1;
387 ctx_p->ut.gid = (gid_t)-1;
389 SAFE_FREE(ctx_p->ut.groups);
390 ctx_p->ut.ngroups = 0;
392 TALLOC_FREE(ctx_p->token);
394 /* Pop back previous user */
398 prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
400 /* Change uid, gid and supplementary group list. */
401 set_unix_security_ctx(prev_ctx_p->ut.uid,
403 prev_ctx_p->ut.ngroups,
404 prev_ctx_p->ut.groups);
406 /* Update current_user stuff */
408 current_user.ut.uid = prev_ctx_p->ut.uid;
409 current_user.ut.gid = prev_ctx_p->ut.gid;
410 current_user.ut.ngroups = prev_ctx_p->ut.ngroups;
411 current_user.ut.groups = prev_ctx_p->ut.groups;
412 current_user.nt_user_token = prev_ctx_p->token;
414 DEBUG(3, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
415 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
420 /* Initialise the security context system */
422 void init_sec_ctx(void)
425 struct sec_ctx *ctx_p;
427 /* Initialise security context stack */
429 memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
431 for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
432 sec_ctx_stack[i].ut.uid = (uid_t)-1;
433 sec_ctx_stack[i].ut.gid = (gid_t)-1;
436 /* Initialise first level of stack. It is the current context */
437 ctx_p = &sec_ctx_stack[0];
439 ctx_p->ut.uid = geteuid();
440 ctx_p->ut.gid = getegid();
442 get_current_groups(ctx_p->ut.gid, &ctx_p->ut.ngroups, &ctx_p->ut.groups);
444 ctx_p->token = NULL; /* Maps to guest user. */
446 /* Initialise current_user global */
448 current_user.ut.uid = ctx_p->ut.uid;
449 current_user.ut.gid = ctx_p->ut.gid;
450 current_user.ut.ngroups = ctx_p->ut.ngroups;
451 current_user.ut.groups = ctx_p->ut.groups;
453 /* The conn and vuid are usually taken care of by other modules.
454 We initialise them here. */
456 current_user.conn = NULL;
457 current_user.vuid = UID_FIELD_INVALID;
458 current_user.nt_user_token = NULL;