s4-test/repl_schema: Remote global ldb connections
[Samba.git] / source3 / smbd / sec_ctx.c
blob2405eb51d4ad8f4e2310b4d4e9278b570ee02ab4
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 "smbd/globals.h"
22 #include "libcli/security/security_token.h"
24 extern struct current_user current_user;
26 /****************************************************************************
27 Are two UNIX tokens equal ?
28 ****************************************************************************/
30 bool unix_token_equal(const UNIX_USER_TOKEN *t1, const UNIX_USER_TOKEN *t2)
32 if (t1->uid != t2->uid || t1->gid != t2->gid ||
33 t1->ngroups != t2->ngroups) {
34 return false;
36 if (memcmp(t1->groups, t2->groups,
37 t1->ngroups*sizeof(gid_t)) != 0) {
38 return false;
40 return true;
43 /****************************************************************************
44 Become the specified uid.
45 ****************************************************************************/
47 static bool become_uid(uid_t uid)
49 /* Check for dodgy uid values */
51 if (uid == (uid_t)-1 ||
52 ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
53 if (!become_uid_done) {
54 DEBUG(1,("WARNING: using uid %d is a security risk\n",
55 (int)uid));
56 become_uid_done = true;
60 /* Set effective user id */
62 set_effective_uid(uid);
64 DO_PROFILE_INC(uid_changes);
65 return True;
68 /****************************************************************************
69 Become the specified gid.
70 ****************************************************************************/
72 static bool become_gid(gid_t gid)
74 /* Check for dodgy gid values */
76 if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) &&
77 (gid == (gid_t)65535))) {
78 if (!become_gid_done) {
79 DEBUG(1,("WARNING: using gid %d is a security risk\n",
80 (int)gid));
81 become_gid_done = true;
85 /* Set effective group id */
87 set_effective_gid(gid);
88 return True;
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()) {
107 return;
110 if (geteuid() != 0) {
111 set_effective_uid(0);
113 if (geteuid() != 0) {
114 DEBUG(0,
115 ("Warning: You appear to have a trapdoor "
116 "uid system\n"));
120 if (getegid() != 0) {
121 set_effective_gid(0);
123 if (getegid() != 0) {
124 DEBUG(0,
125 ("Warning: You appear to have a trapdoor "
126 "gid system\n"));
131 /****************************************************************************
132 Get the list of current groups.
133 ****************************************************************************/
135 static int get_current_groups(gid_t gid, size_t *p_ngroups, gid_t **p_groups)
137 int i;
138 gid_t grp;
139 int ngroups;
140 gid_t *groups = NULL;
142 (*p_ngroups) = 0;
143 (*p_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) */
148 save_re_gid();
149 set_effective_gid(gid);
150 setgid(gid);
152 ngroups = sys_getgroups(0,&grp);
153 if (ngroups <= 0) {
154 goto fail;
157 if((groups = SMB_MALLOC_ARRAY(gid_t, ngroups+1)) == NULL) {
158 DEBUG(0,("setup_groups malloc fail !\n"));
159 goto fail;
162 if ((ngroups = sys_getgroups(ngroups,groups)) == -1) {
163 goto fail;
166 restore_re_gid();
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" ) );
177 return ngroups;
179 fail:
180 SAFE_FREE(groups);
181 restore_re_gid();
182 return -1;
185 /****************************************************************************
186 Create a new security context on the stack. It is the same as the old
187 one. User changes are done using the set_sec_ctx() function.
188 ****************************************************************************/
190 bool push_sec_ctx(void)
192 struct sec_ctx *ctx_p;
194 /* Check we don't overflow our stack */
196 if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
197 DEBUG(0, ("Security context stack overflow!\n"));
198 smb_panic("Security context stack overflow!");
201 /* Store previous user context */
203 sec_ctx_stack_ndx++;
205 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
207 ctx_p->ut.uid = geteuid();
208 ctx_p->ut.gid = getegid();
210 DEBUG(3, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n",
211 (unsigned int)ctx_p->ut.uid, (unsigned int)ctx_p->ut.gid, sec_ctx_stack_ndx ));
213 ctx_p->token = dup_nt_token(NULL,
214 sec_ctx_stack[sec_ctx_stack_ndx-1].token);
216 ctx_p->ut.ngroups = sys_getgroups(0, NULL);
218 if (ctx_p->ut.ngroups != 0) {
219 if (!(ctx_p->ut.groups = SMB_MALLOC_ARRAY(gid_t, ctx_p->ut.ngroups))) {
220 DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
221 TALLOC_FREE(ctx_p->token);
222 return False;
225 sys_getgroups(ctx_p->ut.ngroups, ctx_p->ut.groups);
226 } else {
227 ctx_p->ut.groups = NULL;
230 return True;
233 /****************************************************************************
234 Change UNIX security context. Calls panic if not successful so no return value.
235 ****************************************************************************/
237 #ifndef HAVE_DARWIN_INITGROUPS
239 /* Normal credential switch path. */
241 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
243 /* Start context switch */
244 gain_root();
245 #ifdef HAVE_SETGROUPS
246 if (sys_setgroups(gid, ngroups, groups) != 0 && !non_root_mode()) {
247 smb_panic("sys_setgroups failed");
249 #endif
250 become_id(uid, gid);
251 /* end context switch */
254 #else /* HAVE_DARWIN_INITGROUPS */
256 /* The Darwin groups implementation is a little unusual. The list of
257 * groups in the kernel credential is not exhaustive, but more like
258 * a cache. The full group list is held in userspace and checked
259 * dynamically.
261 * This is an optional mechanism, and setgroups(2) opts out
262 * of it. That is, if you call setgroups, then the list of groups you
263 * set are the only groups that are ever checked. This is not what we
264 * want. We want to opt in to the dynamic resolution mechanism, so we
265 * need to specify the uid of the user whose group list (cache) we are
266 * setting.
268 * The Darwin rules are:
269 * 1. Thou shalt setegid, initgroups and seteuid IN THAT ORDER
270 * 2. Thou shalt not pass more that NGROUPS_MAX to initgroups
271 * 3. Thou shalt leave the first entry in the groups list well alone
274 #include <sys/syscall.h>
276 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
278 int max = groups_max();
280 /* Start context switch */
281 gain_root();
283 become_gid(gid);
286 if (syscall(SYS_initgroups, (ngroups > max) ? max : ngroups,
287 groups, uid) == -1 && !non_root_mode()) {
288 DEBUG(0, ("WARNING: failed to set group list "
289 "(%d groups) for UID %ld: %s\n",
290 ngroups, uid, strerror(errno)));
291 smb_panic("sys_setgroups failed");
294 become_uid(uid);
295 /* end context switch */
298 #endif /* HAVE_DARWIN_INITGROUPS */
300 /****************************************************************************
301 Set the current security context to a given user.
302 ****************************************************************************/
304 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, struct security_token *token)
306 struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
308 /* Set the security context */
310 DEBUG(3, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
311 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
313 security_token_debug(DBGC_CLASS, 5, token);
314 debug_unix_user_token(DBGC_CLASS, 5, uid, gid, ngroups, groups);
316 /* Change uid, gid and supplementary group list. */
317 set_unix_security_ctx(uid, gid, ngroups, groups);
319 ctx_p->ut.ngroups = ngroups;
321 SAFE_FREE(ctx_p->ut.groups);
322 if (token && (token == ctx_p->token)) {
323 smb_panic("DUPLICATE_TOKEN");
326 TALLOC_FREE(ctx_p->token);
328 if (ngroups) {
329 ctx_p->ut.groups = (gid_t *)memdup(groups,
330 sizeof(gid_t) * ngroups);
331 if (!ctx_p->ut.groups) {
332 smb_panic("memdup failed");
334 } else {
335 ctx_p->ut.groups = NULL;
338 if (token) {
339 ctx_p->token = dup_nt_token(NULL, token);
340 if (!ctx_p->token) {
341 smb_panic("dup_nt_token failed");
343 } else {
344 ctx_p->token = NULL;
347 ctx_p->ut.uid = uid;
348 ctx_p->ut.gid = gid;
350 /* Update current_user stuff */
352 current_user.ut.uid = uid;
353 current_user.ut.gid = gid;
354 current_user.ut.ngroups = ngroups;
355 current_user.ut.groups = groups;
356 current_user.nt_user_token = ctx_p->token;
359 /****************************************************************************
360 Become root context.
361 ****************************************************************************/
363 void set_root_sec_ctx(void)
365 /* May need to worry about supplementary groups at some stage */
367 set_sec_ctx(0, 0, 0, NULL, NULL);
370 /****************************************************************************
371 Pop a security context from the stack.
372 ****************************************************************************/
374 bool pop_sec_ctx(void)
376 struct sec_ctx *ctx_p;
377 struct sec_ctx *prev_ctx_p;
379 /* Check for stack underflow */
381 if (sec_ctx_stack_ndx == 0) {
382 DEBUG(0, ("Security context stack underflow!\n"));
383 smb_panic("Security context stack underflow!");
386 ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
388 /* Clear previous user info */
390 ctx_p->ut.uid = (uid_t)-1;
391 ctx_p->ut.gid = (gid_t)-1;
393 SAFE_FREE(ctx_p->ut.groups);
394 ctx_p->ut.ngroups = 0;
396 TALLOC_FREE(ctx_p->token);
398 /* Pop back previous user */
400 sec_ctx_stack_ndx--;
402 prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
404 /* Change uid, gid and supplementary group list. */
405 set_unix_security_ctx(prev_ctx_p->ut.uid,
406 prev_ctx_p->ut.gid,
407 prev_ctx_p->ut.ngroups,
408 prev_ctx_p->ut.groups);
410 /* Update current_user stuff */
412 current_user.ut.uid = prev_ctx_p->ut.uid;
413 current_user.ut.gid = prev_ctx_p->ut.gid;
414 current_user.ut.ngroups = prev_ctx_p->ut.ngroups;
415 current_user.ut.groups = prev_ctx_p->ut.groups;
416 current_user.nt_user_token = prev_ctx_p->token;
418 DEBUG(3, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
419 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
421 return True;
424 /* Initialise the security context system */
426 void init_sec_ctx(void)
428 int i;
429 struct sec_ctx *ctx_p;
431 /* Initialise security context stack */
433 memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
435 for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
436 sec_ctx_stack[i].ut.uid = (uid_t)-1;
437 sec_ctx_stack[i].ut.gid = (gid_t)-1;
440 /* Initialise first level of stack. It is the current context */
441 ctx_p = &sec_ctx_stack[0];
443 ctx_p->ut.uid = geteuid();
444 ctx_p->ut.gid = getegid();
446 get_current_groups(ctx_p->ut.gid, &ctx_p->ut.ngroups, &ctx_p->ut.groups);
448 ctx_p->token = NULL; /* Maps to guest user. */
450 /* Initialise current_user global */
452 current_user.ut.uid = ctx_p->ut.uid;
453 current_user.ut.gid = ctx_p->ut.gid;
454 current_user.ut.ngroups = ctx_p->ut.ngroups;
455 current_user.ut.groups = ctx_p->ut.groups;
457 /* The conn and vuid are usually taken care of by other modules.
458 We initialise them here. */
460 current_user.conn = NULL;
461 current_user.vuid = UID_FIELD_INVALID;
462 current_user.nt_user_token = NULL;