s3:dbwrap fix return value of db_tdb_parse
[Samba/bb.git] / source3 / smbd / sec_ctx.c
blob99592815e3f953ee6407f9bc3265c877af27f2b6
1 /*
2 Unix SMB/CIFS implementation.
3 uid/user handling
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/>.
20 #include "includes.h"
21 #include "system/passwd.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "libcli/security/security_token.h"
25 #include "auth.h"
26 #include "smbprofile.h"
28 extern struct current_user current_user;
30 /****************************************************************************
31 Are two UNIX tokens equal ?
32 ****************************************************************************/
34 bool unix_token_equal(const struct security_unix_token *t1, const struct security_unix_token *t2)
36 if (t1->uid != t2->uid || t1->gid != t2->gid ||
37 t1->ngroups != t2->ngroups) {
38 return false;
40 if (memcmp(t1->groups, t2->groups,
41 t1->ngroups*sizeof(gid_t)) != 0) {
42 return false;
44 return true;
47 /****************************************************************************
48 Become the specified uid.
49 ****************************************************************************/
51 static bool become_uid(uid_t uid)
53 /* Check for dodgy uid values */
55 if (uid == (uid_t)-1 ||
56 ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
57 if (!become_uid_done) {
58 DEBUG(1,("WARNING: using uid %d is a security risk\n",
59 (int)uid));
60 become_uid_done = true;
64 /* Set effective user id */
66 set_effective_uid(uid);
68 DO_PROFILE_INC(uid_changes);
69 return True;
72 /****************************************************************************
73 Become the specified gid.
74 ****************************************************************************/
76 static bool become_gid(gid_t gid)
78 /* Check for dodgy gid values */
80 if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) &&
81 (gid == (gid_t)65535))) {
82 if (!become_gid_done) {
83 DEBUG(1,("WARNING: using gid %d is a security risk\n",
84 (int)gid));
85 become_gid_done = true;
89 /* Set effective group id */
91 set_effective_gid(gid);
92 return True;
95 /****************************************************************************
96 Become the specified uid and gid.
97 ****************************************************************************/
99 static bool become_id(uid_t uid, gid_t gid)
101 return become_gid(gid) && become_uid(uid);
104 /****************************************************************************
105 Drop back to root privileges in order to change to another user.
106 ****************************************************************************/
108 static void gain_root(void)
110 if (non_root_mode()) {
111 return;
114 if (geteuid() != 0) {
115 set_effective_uid(0);
117 if (geteuid() != 0) {
118 DEBUG(0,
119 ("Warning: You appear to have a trapdoor "
120 "uid system\n"));
124 if (getegid() != 0) {
125 set_effective_gid(0);
127 if (getegid() != 0) {
128 DEBUG(0,
129 ("Warning: You appear to have a trapdoor "
130 "gid system\n"));
135 /****************************************************************************
136 Get the list of current groups.
137 ****************************************************************************/
139 static int get_current_groups(gid_t gid, uint32_t *p_ngroups, gid_t **p_groups)
141 int i;
142 gid_t grp;
143 int ngroups;
144 gid_t *groups = NULL;
146 (*p_ngroups) = 0;
147 (*p_groups) = NULL;
149 /* this looks a little strange, but is needed to cope with
150 systems that put the current egid in the group list
151 returned from getgroups() (tridge) */
152 save_re_gid();
153 set_effective_gid(gid);
154 setgid(gid);
156 ngroups = sys_getgroups(0,&grp);
157 if (ngroups <= 0) {
158 goto fail;
161 if((groups = SMB_MALLOC_ARRAY(gid_t, ngroups+1)) == NULL) {
162 DEBUG(0,("setup_groups malloc fail !\n"));
163 goto fail;
166 if ((ngroups = sys_getgroups(ngroups,groups)) == -1) {
167 goto fail;
170 restore_re_gid();
172 (*p_ngroups) = ngroups;
173 (*p_groups) = groups;
175 DEBUG( 4, ( "get_current_groups: user is in %u groups: ", ngroups));
176 for (i = 0; i < ngroups; i++ ) {
177 DEBUG( 4, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
179 DEBUG( 4, ( "\n" ) );
181 return ngroups;
183 fail:
184 SAFE_FREE(groups);
185 restore_re_gid();
186 return -1;
189 /****************************************************************************
190 Create a new security context on the stack. It is the same as the old
191 one. User changes are done using the set_sec_ctx() function.
192 ****************************************************************************/
194 bool push_sec_ctx(void)
196 struct sec_ctx *ctx_p;
198 /* Check we don't overflow our stack */
200 if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
201 DEBUG(0, ("Security context stack overflow!\n"));
202 smb_panic("Security context stack overflow!");
205 /* Store previous user context */
207 sec_ctx_stack_ndx++;
209 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
211 ctx_p->ut.uid = geteuid();
212 ctx_p->ut.gid = getegid();
214 DEBUG(4, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n",
215 (unsigned int)ctx_p->ut.uid, (unsigned int)ctx_p->ut.gid, sec_ctx_stack_ndx ));
217 ctx_p->token = dup_nt_token(NULL,
218 sec_ctx_stack[sec_ctx_stack_ndx-1].token);
220 ctx_p->ut.ngroups = sys_getgroups(0, NULL);
222 if (ctx_p->ut.ngroups != 0) {
223 if (!(ctx_p->ut.groups = SMB_MALLOC_ARRAY(gid_t, ctx_p->ut.ngroups))) {
224 DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
225 TALLOC_FREE(ctx_p->token);
226 return False;
229 sys_getgroups(ctx_p->ut.ngroups, ctx_p->ut.groups);
230 } else {
231 ctx_p->ut.groups = NULL;
234 return True;
237 /****************************************************************************
238 Change UNIX security context. Calls panic if not successful so no return value.
239 ****************************************************************************/
241 #ifndef HAVE_DARWIN_INITGROUPS
243 /* Normal credential switch path. */
245 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
247 /* Start context switch */
248 gain_root();
249 #ifdef HAVE_SETGROUPS
250 if (sys_setgroups(gid, ngroups, groups) != 0 && !non_root_mode()) {
251 smb_panic("sys_setgroups failed");
253 #endif
254 become_id(uid, gid);
255 /* end context switch */
258 #else /* HAVE_DARWIN_INITGROUPS */
260 /* The Darwin groups implementation is a little unusual. The list of
261 * groups in the kernel credential is not exhaustive, but more like
262 * a cache. The full group list is held in userspace and checked
263 * dynamically.
265 * This is an optional mechanism, and setgroups(2) opts out
266 * of it. That is, if you call setgroups, then the list of groups you
267 * set are the only groups that are ever checked. This is not what we
268 * want. We want to opt in to the dynamic resolution mechanism, so we
269 * need to specify the uid of the user whose group list (cache) we are
270 * setting.
272 * The Darwin rules are:
273 * 1. Thou shalt setegid, initgroups and seteuid IN THAT ORDER
274 * 2. Thou shalt not pass more that NGROUPS_MAX to initgroups
275 * 3. Thou shalt leave the first entry in the groups list well alone
278 #include <sys/syscall.h>
280 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
282 int max = groups_max();
284 /* Start context switch */
285 gain_root();
287 become_gid(gid);
290 if (syscall(SYS_initgroups, (ngroups > max) ? max : ngroups,
291 groups, uid) == -1 && !non_root_mode()) {
292 DEBUG(0, ("WARNING: failed to set group list "
293 "(%d groups) for UID %ld: %s\n",
294 ngroups, uid, strerror(errno)));
295 smb_panic("sys_setgroups failed");
298 become_uid(uid);
299 /* end context switch */
302 #endif /* HAVE_DARWIN_INITGROUPS */
304 /****************************************************************************
305 Set the current security context to a given user.
306 ****************************************************************************/
308 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, struct security_token *token)
310 struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
312 /* Set the security context */
314 DEBUG(4, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
315 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
317 security_token_debug(DBGC_CLASS, 5, token);
318 debug_unix_user_token(DBGC_CLASS, 5, uid, gid, ngroups, groups);
320 /* Change uid, gid and supplementary group list. */
321 set_unix_security_ctx(uid, gid, ngroups, groups);
323 ctx_p->ut.ngroups = ngroups;
325 SAFE_FREE(ctx_p->ut.groups);
326 if (token && (token == ctx_p->token)) {
327 smb_panic("DUPLICATE_TOKEN");
330 TALLOC_FREE(ctx_p->token);
332 if (ngroups) {
333 ctx_p->ut.groups = (gid_t *)memdup(groups,
334 sizeof(gid_t) * ngroups);
335 if (!ctx_p->ut.groups) {
336 smb_panic("memdup failed");
338 } else {
339 ctx_p->ut.groups = NULL;
342 if (token) {
343 ctx_p->token = dup_nt_token(NULL, token);
344 if (!ctx_p->token) {
345 smb_panic("dup_nt_token failed");
347 } else {
348 ctx_p->token = NULL;
351 ctx_p->ut.uid = uid;
352 ctx_p->ut.gid = gid;
354 /* Update current_user stuff */
356 current_user.ut.uid = uid;
357 current_user.ut.gid = gid;
358 current_user.ut.ngroups = ngroups;
359 current_user.ut.groups = groups;
360 current_user.nt_user_token = ctx_p->token;
363 /****************************************************************************
364 Become root context.
365 ****************************************************************************/
367 void set_root_sec_ctx(void)
369 /* May need to worry about supplementary groups at some stage */
371 set_sec_ctx(0, 0, 0, NULL, NULL);
374 /****************************************************************************
375 Pop a security context from the stack.
376 ****************************************************************************/
378 bool pop_sec_ctx(void)
380 struct sec_ctx *ctx_p;
381 struct sec_ctx *prev_ctx_p;
383 /* Check for stack underflow */
385 if (sec_ctx_stack_ndx == 0) {
386 DEBUG(0, ("Security context stack underflow!\n"));
387 smb_panic("Security context stack underflow!");
390 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
392 /* Clear previous user info */
394 ctx_p->ut.uid = (uid_t)-1;
395 ctx_p->ut.gid = (gid_t)-1;
397 SAFE_FREE(ctx_p->ut.groups);
398 ctx_p->ut.ngroups = 0;
400 TALLOC_FREE(ctx_p->token);
402 /* Pop back previous user */
404 sec_ctx_stack_ndx--;
406 prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
408 /* Change uid, gid and supplementary group list. */
409 set_unix_security_ctx(prev_ctx_p->ut.uid,
410 prev_ctx_p->ut.gid,
411 prev_ctx_p->ut.ngroups,
412 prev_ctx_p->ut.groups);
414 /* Update current_user stuff */
416 current_user.ut.uid = prev_ctx_p->ut.uid;
417 current_user.ut.gid = prev_ctx_p->ut.gid;
418 current_user.ut.ngroups = prev_ctx_p->ut.ngroups;
419 current_user.ut.groups = prev_ctx_p->ut.groups;
420 current_user.nt_user_token = prev_ctx_p->token;
422 DEBUG(4, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
423 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
425 return True;
428 /* Initialise the security context system */
430 void init_sec_ctx(void)
432 int i;
433 struct sec_ctx *ctx_p;
435 /* Initialise security context stack */
437 memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
439 for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
440 sec_ctx_stack[i].ut.uid = (uid_t)-1;
441 sec_ctx_stack[i].ut.gid = (gid_t)-1;
444 /* Initialise first level of stack. It is the current context */
445 ctx_p = &sec_ctx_stack[0];
447 ctx_p->ut.uid = geteuid();
448 ctx_p->ut.gid = getegid();
450 get_current_groups(ctx_p->ut.gid, &ctx_p->ut.ngroups, &ctx_p->ut.groups);
452 ctx_p->token = NULL; /* Maps to guest user. */
454 /* Initialise current_user global */
456 current_user.ut.uid = ctx_p->ut.uid;
457 current_user.ut.gid = ctx_p->ut.gid;
458 current_user.ut.ngroups = ctx_p->ut.ngroups;
459 current_user.ut.groups = ctx_p->ut.groups;
461 /* The conn and vuid are usually taken care of by other modules.
462 We initialise them here. */
464 current_user.conn = NULL;
465 current_user.vuid = UID_FIELD_INVALID;
466 current_user.nt_user_token = NULL;