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/>.
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "libcli/security/security_token.h"
25 extern struct current_user current_user;
27 /****************************************************************************
28 Are two UNIX tokens equal ?
29 ****************************************************************************/
31 bool unix_token_equal(const struct security_unix_token *t1, const struct security_unix_token *t2)
33 if (t1->uid != t2->uid || t1->gid != t2->gid ||
34 t1->ngroups != t2->ngroups) {
37 if (memcmp(t1->groups, t2->groups,
38 t1->ngroups*sizeof(gid_t)) != 0) {
44 /****************************************************************************
45 Become the specified uid.
46 ****************************************************************************/
48 static bool become_uid(uid_t uid)
50 /* Check for dodgy uid values */
52 if (uid == (uid_t)-1 ||
53 ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
54 if (!become_uid_done) {
55 DEBUG(1,("WARNING: using uid %d is a security risk\n",
57 become_uid_done = true;
61 /* Set effective user id */
63 set_effective_uid(uid);
65 DO_PROFILE_INC(uid_changes);
69 /****************************************************************************
70 Become the specified gid.
71 ****************************************************************************/
73 static bool become_gid(gid_t gid)
75 /* Check for dodgy gid values */
77 if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) &&
78 (gid == (gid_t)65535))) {
79 if (!become_gid_done) {
80 DEBUG(1,("WARNING: using gid %d is a security risk\n",
82 become_gid_done = true;
86 /* Set effective group id */
88 set_effective_gid(gid);
92 /****************************************************************************
93 Become the specified uid and gid.
94 ****************************************************************************/
96 static bool become_id(uid_t uid, gid_t gid)
98 return become_gid(gid) && become_uid(uid);
101 /****************************************************************************
102 Drop back to root privileges in order to change to another user.
103 ****************************************************************************/
105 static void gain_root(void)
107 if (non_root_mode()) {
111 if (geteuid() != 0) {
112 set_effective_uid(0);
114 if (geteuid() != 0) {
116 ("Warning: You appear to have a trapdoor "
121 if (getegid() != 0) {
122 set_effective_gid(0);
124 if (getegid() != 0) {
126 ("Warning: You appear to have a trapdoor "
132 /****************************************************************************
133 Get the list of current groups.
134 ****************************************************************************/
136 static int get_current_groups(gid_t gid, uint32_t *p_ngroups, gid_t **p_groups)
141 gid_t *groups = NULL;
146 /* this looks a little strange, but is needed to cope with
147 systems that put the current egid in the group list
148 returned from getgroups() (tridge) */
150 set_effective_gid(gid);
153 ngroups = sys_getgroups(0,&grp);
158 if((groups = SMB_MALLOC_ARRAY(gid_t, ngroups+1)) == NULL) {
159 DEBUG(0,("setup_groups malloc fail !\n"));
163 if ((ngroups = sys_getgroups(ngroups,groups)) == -1) {
169 (*p_ngroups) = ngroups;
170 (*p_groups) = groups;
172 DEBUG( 4, ( "get_current_groups: user is in %u groups: ", ngroups));
173 for (i = 0; i < ngroups; i++ ) {
174 DEBUG( 4, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
176 DEBUG( 4, ( "\n" ) );
186 /****************************************************************************
187 Create a new security context on the stack. It is the same as the old
188 one. User changes are done using the set_sec_ctx() function.
189 ****************************************************************************/
191 bool push_sec_ctx(void)
193 struct sec_ctx *ctx_p;
195 /* Check we don't overflow our stack */
197 if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
198 DEBUG(0, ("Security context stack overflow!\n"));
199 smb_panic("Security context stack overflow!");
202 /* Store previous user context */
206 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
208 ctx_p->ut.uid = geteuid();
209 ctx_p->ut.gid = getegid();
211 DEBUG(4, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n",
212 (unsigned int)ctx_p->ut.uid, (unsigned int)ctx_p->ut.gid, sec_ctx_stack_ndx ));
214 ctx_p->token = dup_nt_token(NULL,
215 sec_ctx_stack[sec_ctx_stack_ndx-1].token);
217 ctx_p->ut.ngroups = sys_getgroups(0, NULL);
219 if (ctx_p->ut.ngroups != 0) {
220 if (!(ctx_p->ut.groups = SMB_MALLOC_ARRAY(gid_t, ctx_p->ut.ngroups))) {
221 DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
222 TALLOC_FREE(ctx_p->token);
226 sys_getgroups(ctx_p->ut.ngroups, ctx_p->ut.groups);
228 ctx_p->ut.groups = NULL;
234 /****************************************************************************
235 Change UNIX security context. Calls panic if not successful so no return value.
236 ****************************************************************************/
238 #ifndef HAVE_DARWIN_INITGROUPS
240 /* Normal credential switch path. */
242 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
244 /* Start context switch */
246 #ifdef HAVE_SETGROUPS
247 if (sys_setgroups(gid, ngroups, groups) != 0 && !non_root_mode()) {
248 smb_panic("sys_setgroups failed");
252 /* end context switch */
255 #else /* HAVE_DARWIN_INITGROUPS */
257 /* The Darwin groups implementation is a little unusual. The list of
258 * groups in the kernel credential is not exhaustive, but more like
259 * a cache. The full group list is held in userspace and checked
262 * This is an optional mechanism, and setgroups(2) opts out
263 * of it. That is, if you call setgroups, then the list of groups you
264 * set are the only groups that are ever checked. This is not what we
265 * want. We want to opt in to the dynamic resolution mechanism, so we
266 * need to specify the uid of the user whose group list (cache) we are
269 * The Darwin rules are:
270 * 1. Thou shalt setegid, initgroups and seteuid IN THAT ORDER
271 * 2. Thou shalt not pass more that NGROUPS_MAX to initgroups
272 * 3. Thou shalt leave the first entry in the groups list well alone
275 #include <sys/syscall.h>
277 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
279 int max = groups_max();
281 /* Start context switch */
287 if (syscall(SYS_initgroups, (ngroups > max) ? max : ngroups,
288 groups, uid) == -1 && !non_root_mode()) {
289 DEBUG(0, ("WARNING: failed to set group list "
290 "(%d groups) for UID %ld: %s\n",
291 ngroups, uid, strerror(errno)));
292 smb_panic("sys_setgroups failed");
296 /* end context switch */
299 #endif /* HAVE_DARWIN_INITGROUPS */
301 /****************************************************************************
302 Set the current security context to a given user.
303 ****************************************************************************/
305 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, struct security_token *token)
307 struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
309 /* Set the security context */
311 DEBUG(4, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
312 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
314 security_token_debug(DBGC_CLASS, 5, token);
315 debug_unix_user_token(DBGC_CLASS, 5, uid, gid, ngroups, groups);
317 /* Change uid, gid and supplementary group list. */
318 set_unix_security_ctx(uid, gid, ngroups, groups);
320 ctx_p->ut.ngroups = ngroups;
322 SAFE_FREE(ctx_p->ut.groups);
323 if (token && (token == ctx_p->token)) {
324 smb_panic("DUPLICATE_TOKEN");
327 TALLOC_FREE(ctx_p->token);
330 ctx_p->ut.groups = (gid_t *)memdup(groups,
331 sizeof(gid_t) * ngroups);
332 if (!ctx_p->ut.groups) {
333 smb_panic("memdup failed");
336 ctx_p->ut.groups = NULL;
340 ctx_p->token = dup_nt_token(NULL, token);
342 smb_panic("dup_nt_token failed");
351 /* Update current_user stuff */
353 current_user.ut.uid = uid;
354 current_user.ut.gid = gid;
355 current_user.ut.ngroups = ngroups;
356 current_user.ut.groups = groups;
357 current_user.nt_user_token = ctx_p->token;
360 /****************************************************************************
362 ****************************************************************************/
364 void set_root_sec_ctx(void)
366 /* May need to worry about supplementary groups at some stage */
368 set_sec_ctx(0, 0, 0, NULL, NULL);
371 /****************************************************************************
372 Pop a security context from the stack.
373 ****************************************************************************/
375 bool pop_sec_ctx(void)
377 struct sec_ctx *ctx_p;
378 struct sec_ctx *prev_ctx_p;
380 /* Check for stack underflow */
382 if (sec_ctx_stack_ndx == 0) {
383 DEBUG(0, ("Security context stack underflow!\n"));
384 smb_panic("Security context stack underflow!");
387 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
389 /* Clear previous user info */
391 ctx_p->ut.uid = (uid_t)-1;
392 ctx_p->ut.gid = (gid_t)-1;
394 SAFE_FREE(ctx_p->ut.groups);
395 ctx_p->ut.ngroups = 0;
397 TALLOC_FREE(ctx_p->token);
399 /* Pop back previous user */
403 prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
405 /* Change uid, gid and supplementary group list. */
406 set_unix_security_ctx(prev_ctx_p->ut.uid,
408 prev_ctx_p->ut.ngroups,
409 prev_ctx_p->ut.groups);
411 /* Update current_user stuff */
413 current_user.ut.uid = prev_ctx_p->ut.uid;
414 current_user.ut.gid = prev_ctx_p->ut.gid;
415 current_user.ut.ngroups = prev_ctx_p->ut.ngroups;
416 current_user.ut.groups = prev_ctx_p->ut.groups;
417 current_user.nt_user_token = prev_ctx_p->token;
419 DEBUG(4, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
420 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
425 /* Initialise the security context system */
427 void init_sec_ctx(void)
430 struct sec_ctx *ctx_p;
432 /* Initialise security context stack */
434 memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
436 for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
437 sec_ctx_stack[i].ut.uid = (uid_t)-1;
438 sec_ctx_stack[i].ut.gid = (gid_t)-1;
441 /* Initialise first level of stack. It is the current context */
442 ctx_p = &sec_ctx_stack[0];
444 ctx_p->ut.uid = geteuid();
445 ctx_p->ut.gid = getegid();
447 get_current_groups(ctx_p->ut.gid, &ctx_p->ut.ngroups, &ctx_p->ut.groups);
449 ctx_p->token = NULL; /* Maps to guest user. */
451 /* Initialise current_user global */
453 current_user.ut.uid = ctx_p->ut.uid;
454 current_user.ut.gid = ctx_p->ut.gid;
455 current_user.ut.ngroups = ctx_p->ut.ngroups;
456 current_user.ut.groups = ctx_p->ut.groups;
458 /* The conn and vuid are usually taken care of by other modules.
459 We initialise them here. */
461 current_user.conn = NULL;
462 current_user.vuid = UID_FIELD_INVALID;
463 current_user.nt_user_token = NULL;