1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | uid/user handling
|
---|
4 | Copyright (C) Tim Potter 2000
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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/>.
|
---|
18 | */
|
---|
19 |
|
---|
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"
|
---|
27 | #include "../lib/util/setid.h"
|
---|
28 |
|
---|
29 | extern struct current_user current_user;
|
---|
30 |
|
---|
31 | /****************************************************************************
|
---|
32 | Are two UNIX tokens equal ?
|
---|
33 | ****************************************************************************/
|
---|
34 |
|
---|
35 | bool unix_token_equal(const struct security_unix_token *t1, const struct security_unix_token *t2)
|
---|
36 | {
|
---|
37 | if (t1->uid != t2->uid || t1->gid != t2->gid ||
|
---|
38 | t1->ngroups != t2->ngroups) {
|
---|
39 | return false;
|
---|
40 | }
|
---|
41 | if (memcmp(t1->groups, t2->groups,
|
---|
42 | t1->ngroups*sizeof(gid_t)) != 0) {
|
---|
43 | return false;
|
---|
44 | }
|
---|
45 | return true;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /****************************************************************************
|
---|
49 | Become the specified uid.
|
---|
50 | ****************************************************************************/
|
---|
51 |
|
---|
52 | static bool become_uid(uid_t uid)
|
---|
53 | {
|
---|
54 | /* Check for dodgy uid values */
|
---|
55 |
|
---|
56 | if (uid == (uid_t)-1 ||
|
---|
57 | ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
|
---|
58 | if (!become_uid_done) {
|
---|
59 | DEBUG(1,("WARNING: using uid %d is a security risk\n",
|
---|
60 | (int)uid));
|
---|
61 | become_uid_done = true;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* Set effective user id */
|
---|
66 |
|
---|
67 | set_effective_uid(uid);
|
---|
68 |
|
---|
69 | return True;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /****************************************************************************
|
---|
73 | Become the specified gid.
|
---|
74 | ****************************************************************************/
|
---|
75 |
|
---|
76 | static bool become_gid(gid_t gid)
|
---|
77 | {
|
---|
78 | /* Check for dodgy gid values */
|
---|
79 |
|
---|
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;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | /* Set effective group id */
|
---|
90 |
|
---|
91 | set_effective_gid(gid);
|
---|
92 | return True;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /****************************************************************************
|
---|
96 | Become the specified uid and gid.
|
---|
97 | ****************************************************************************/
|
---|
98 |
|
---|
99 | static bool become_id(uid_t uid, gid_t gid)
|
---|
100 | {
|
---|
101 | return become_gid(gid) && become_uid(uid);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /****************************************************************************
|
---|
105 | Drop back to root privileges in order to change to another user.
|
---|
106 | ****************************************************************************/
|
---|
107 |
|
---|
108 | static void gain_root(void)
|
---|
109 | {
|
---|
110 | if (non_root_mode()) {
|
---|
111 | return;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (geteuid() != 0) {
|
---|
115 | set_effective_uid(0);
|
---|
116 |
|
---|
117 | if (geteuid() != 0) {
|
---|
118 | DEBUG(0,
|
---|
119 | ("Warning: You appear to have a trapdoor "
|
---|
120 | "uid system\n"));
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (getegid() != 0) {
|
---|
125 | set_effective_gid(0);
|
---|
126 |
|
---|
127 | if (getegid() != 0) {
|
---|
128 | DEBUG(0,
|
---|
129 | ("Warning: You appear to have a trapdoor "
|
---|
130 | "gid system\n"));
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | /****************************************************************************
|
---|
136 | Get the list of current groups.
|
---|
137 | ****************************************************************************/
|
---|
138 |
|
---|
139 | static int get_current_groups(gid_t gid, uint32_t *p_ngroups, gid_t **p_groups)
|
---|
140 | {
|
---|
141 | int i;
|
---|
142 | gid_t grp;
|
---|
143 | int ngroups;
|
---|
144 | gid_t *groups = NULL;
|
---|
145 |
|
---|
146 | (*p_ngroups) = 0;
|
---|
147 | (*p_groups) = NULL;
|
---|
148 |
|
---|
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 | samba_setgid(gid);
|
---|
155 |
|
---|
156 | ngroups = sys_getgroups(0,&grp);
|
---|
157 | if (ngroups <= 0) {
|
---|
158 | goto fail;
|
---|
159 | }
|
---|
160 |
|
---|
161 | if((groups = SMB_MALLOC_ARRAY(gid_t, ngroups+1)) == NULL) {
|
---|
162 | DEBUG(0,("setup_groups malloc fail !\n"));
|
---|
163 | goto fail;
|
---|
164 | }
|
---|
165 |
|
---|
166 | if ((ngroups = sys_getgroups(ngroups,groups)) == -1) {
|
---|
167 | goto fail;
|
---|
168 | }
|
---|
169 |
|
---|
170 | restore_re_gid();
|
---|
171 |
|
---|
172 | (*p_ngroups) = ngroups;
|
---|
173 | (*p_groups) = groups;
|
---|
174 |
|
---|
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] ) );
|
---|
178 | }
|
---|
179 | DEBUG( 4, ( "\n" ) );
|
---|
180 |
|
---|
181 | return ngroups;
|
---|
182 |
|
---|
183 | fail:
|
---|
184 | SAFE_FREE(groups);
|
---|
185 | restore_re_gid();
|
---|
186 | return -1;
|
---|
187 | }
|
---|
188 |
|
---|
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 | ****************************************************************************/
|
---|
193 |
|
---|
194 | bool push_sec_ctx(void)
|
---|
195 | {
|
---|
196 | struct sec_ctx *ctx_p;
|
---|
197 |
|
---|
198 | START_PROFILE(push_sec_ctx);
|
---|
199 |
|
---|
200 | /* Check we don't overflow our stack */
|
---|
201 |
|
---|
202 | if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
|
---|
203 | DEBUG(0, ("Security context stack overflow!\n"));
|
---|
204 | smb_panic("Security context stack overflow!");
|
---|
205 | }
|
---|
206 |
|
---|
207 | /* Store previous user context */
|
---|
208 |
|
---|
209 | sec_ctx_stack_ndx++;
|
---|
210 |
|
---|
211 | ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
|
---|
212 |
|
---|
213 | ctx_p->ut.uid = geteuid();
|
---|
214 | ctx_p->ut.gid = getegid();
|
---|
215 |
|
---|
216 | DEBUG(4, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n",
|
---|
217 | (unsigned int)ctx_p->ut.uid, (unsigned int)ctx_p->ut.gid, sec_ctx_stack_ndx ));
|
---|
218 |
|
---|
219 | ctx_p->token = dup_nt_token(NULL,
|
---|
220 | sec_ctx_stack[sec_ctx_stack_ndx-1].token);
|
---|
221 |
|
---|
222 | ctx_p->ut.ngroups = sys_getgroups(0, NULL);
|
---|
223 |
|
---|
224 | if (ctx_p->ut.ngroups != 0) {
|
---|
225 | if (!(ctx_p->ut.groups = SMB_MALLOC_ARRAY(gid_t, ctx_p->ut.ngroups))) {
|
---|
226 | DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
|
---|
227 | TALLOC_FREE(ctx_p->token);
|
---|
228 | return False;
|
---|
229 | }
|
---|
230 |
|
---|
231 | sys_getgroups(ctx_p->ut.ngroups, ctx_p->ut.groups);
|
---|
232 | } else {
|
---|
233 | ctx_p->ut.groups = NULL;
|
---|
234 | }
|
---|
235 |
|
---|
236 | END_PROFILE(push_sec_ctx);
|
---|
237 |
|
---|
238 | return True;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /****************************************************************************
|
---|
242 | Change UNIX security context. Calls panic if not successful so no return value.
|
---|
243 | ****************************************************************************/
|
---|
244 |
|
---|
245 | #ifndef HAVE_DARWIN_INITGROUPS
|
---|
246 |
|
---|
247 | /* Normal credential switch path. */
|
---|
248 |
|
---|
249 | static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
|
---|
250 | {
|
---|
251 | /* Start context switch */
|
---|
252 | gain_root();
|
---|
253 | #ifdef HAVE_SETGROUPS
|
---|
254 | if (sys_setgroups(gid, ngroups, groups) != 0 && !non_root_mode()) {
|
---|
255 | smb_panic("sys_setgroups failed");
|
---|
256 | }
|
---|
257 | #endif
|
---|
258 | become_id(uid, gid);
|
---|
259 | /* end context switch */
|
---|
260 | }
|
---|
261 |
|
---|
262 | #else /* HAVE_DARWIN_INITGROUPS */
|
---|
263 |
|
---|
264 | /* The Darwin groups implementation is a little unusual. The list of
|
---|
265 | * groups in the kernel credential is not exhaustive, but more like
|
---|
266 | * a cache. The full group list is held in userspace and checked
|
---|
267 | * dynamically.
|
---|
268 | *
|
---|
269 | * This is an optional mechanism, and setgroups(2) opts out
|
---|
270 | * of it. That is, if you call setgroups, then the list of groups you
|
---|
271 | * set are the only groups that are ever checked. This is not what we
|
---|
272 | * want. We want to opt in to the dynamic resolution mechanism, so we
|
---|
273 | * need to specify the uid of the user whose group list (cache) we are
|
---|
274 | * setting.
|
---|
275 | *
|
---|
276 | * The Darwin rules are:
|
---|
277 | * 1. Thou shalt setegid, initgroups and seteuid IN THAT ORDER
|
---|
278 | * 2. Thou shalt not pass more that NGROUPS_MAX to initgroups
|
---|
279 | * 3. Thou shalt leave the first entry in the groups list well alone
|
---|
280 | */
|
---|
281 |
|
---|
282 | #include <sys/syscall.h>
|
---|
283 |
|
---|
284 | static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
|
---|
285 | {
|
---|
286 | int max = groups_max();
|
---|
287 |
|
---|
288 | /* Start context switch */
|
---|
289 | gain_root();
|
---|
290 |
|
---|
291 | become_gid(gid);
|
---|
292 |
|
---|
293 |
|
---|
294 | if (syscall(SYS_initgroups, (ngroups > max) ? max : ngroups,
|
---|
295 | groups, uid) == -1 && !non_root_mode()) {
|
---|
296 | DEBUG(0, ("WARNING: failed to set group list "
|
---|
297 | "(%d groups) for UID %d: %s\n",
|
---|
298 | ngroups, uid, strerror(errno)));
|
---|
299 | smb_panic("sys_setgroups failed");
|
---|
300 | }
|
---|
301 |
|
---|
302 | become_uid(uid);
|
---|
303 | /* end context switch */
|
---|
304 | }
|
---|
305 |
|
---|
306 | #endif /* HAVE_DARWIN_INITGROUPS */
|
---|
307 |
|
---|
308 | /****************************************************************************
|
---|
309 | Set the current security context to a given user.
|
---|
310 | ****************************************************************************/
|
---|
311 |
|
---|
312 | static void set_sec_ctx_internal(uid_t uid, gid_t gid,
|
---|
313 | int ngroups, gid_t *groups,
|
---|
314 | const struct security_token *token)
|
---|
315 | {
|
---|
316 | struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
|
---|
317 |
|
---|
318 | /* Set the security context */
|
---|
319 |
|
---|
320 | DEBUG(4, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
|
---|
321 | (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
|
---|
322 |
|
---|
323 | security_token_debug(DBGC_CLASS, 5, token);
|
---|
324 | debug_unix_user_token(DBGC_CLASS, 5, uid, gid, ngroups, groups);
|
---|
325 |
|
---|
326 | /* Change uid, gid and supplementary group list. */
|
---|
327 | set_unix_security_ctx(uid, gid, ngroups, groups);
|
---|
328 |
|
---|
329 | ctx_p->ut.ngroups = ngroups;
|
---|
330 |
|
---|
331 | SAFE_FREE(ctx_p->ut.groups);
|
---|
332 | if (token && (token == ctx_p->token)) {
|
---|
333 | smb_panic("DUPLICATE_TOKEN");
|
---|
334 | }
|
---|
335 |
|
---|
336 | TALLOC_FREE(ctx_p->token);
|
---|
337 |
|
---|
338 | if (ngroups) {
|
---|
339 | ctx_p->ut.groups = (gid_t *)smb_xmemdup(groups,
|
---|
340 | sizeof(gid_t) * ngroups);
|
---|
341 | } else {
|
---|
342 | ctx_p->ut.groups = NULL;
|
---|
343 | }
|
---|
344 |
|
---|
345 | if (token) {
|
---|
346 | ctx_p->token = dup_nt_token(NULL, token);
|
---|
347 | if (!ctx_p->token) {
|
---|
348 | smb_panic("dup_nt_token failed");
|
---|
349 | }
|
---|
350 | } else {
|
---|
351 | ctx_p->token = NULL;
|
---|
352 | }
|
---|
353 |
|
---|
354 | ctx_p->ut.uid = uid;
|
---|
355 | ctx_p->ut.gid = gid;
|
---|
356 |
|
---|
357 | /* Update current_user stuff */
|
---|
358 |
|
---|
359 | current_user.ut.uid = uid;
|
---|
360 | current_user.ut.gid = gid;
|
---|
361 | current_user.ut.ngroups = ngroups;
|
---|
362 | current_user.ut.groups = groups;
|
---|
363 | current_user.nt_user_token = ctx_p->token;
|
---|
364 | }
|
---|
365 |
|
---|
366 | void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, const struct security_token *token)
|
---|
367 | {
|
---|
368 | START_PROFILE(set_sec_ctx);
|
---|
369 | set_sec_ctx_internal(uid, gid, ngroups, groups, token);
|
---|
370 | END_PROFILE(set_sec_ctx);
|
---|
371 | }
|
---|
372 |
|
---|
373 | /****************************************************************************
|
---|
374 | Become root context.
|
---|
375 | ****************************************************************************/
|
---|
376 |
|
---|
377 | void set_root_sec_ctx(void)
|
---|
378 | {
|
---|
379 | /* May need to worry about supplementary groups at some stage */
|
---|
380 |
|
---|
381 | START_PROFILE(set_root_sec_ctx);
|
---|
382 | set_sec_ctx_internal(0, 0, 0, NULL, NULL);
|
---|
383 | END_PROFILE(set_root_sec_ctx);
|
---|
384 | }
|
---|
385 |
|
---|
386 | /****************************************************************************
|
---|
387 | Pop a security context from the stack.
|
---|
388 | ****************************************************************************/
|
---|
389 |
|
---|
390 | bool pop_sec_ctx(void)
|
---|
391 | {
|
---|
392 | struct sec_ctx *ctx_p;
|
---|
393 | struct sec_ctx *prev_ctx_p;
|
---|
394 |
|
---|
395 | START_PROFILE(pop_sec_ctx);
|
---|
396 |
|
---|
397 | /* Check for stack underflow */
|
---|
398 |
|
---|
399 | if (sec_ctx_stack_ndx == 0) {
|
---|
400 | DEBUG(0, ("Security context stack underflow!\n"));
|
---|
401 | smb_panic("Security context stack underflow!");
|
---|
402 | }
|
---|
403 |
|
---|
404 | ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
|
---|
405 |
|
---|
406 | /* Clear previous user info */
|
---|
407 |
|
---|
408 | ctx_p->ut.uid = (uid_t)-1;
|
---|
409 | ctx_p->ut.gid = (gid_t)-1;
|
---|
410 |
|
---|
411 | SAFE_FREE(ctx_p->ut.groups);
|
---|
412 | ctx_p->ut.ngroups = 0;
|
---|
413 |
|
---|
414 | TALLOC_FREE(ctx_p->token);
|
---|
415 |
|
---|
416 | /* Pop back previous user */
|
---|
417 |
|
---|
418 | sec_ctx_stack_ndx--;
|
---|
419 |
|
---|
420 | prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
|
---|
421 |
|
---|
422 | /* Change uid, gid and supplementary group list. */
|
---|
423 | set_unix_security_ctx(prev_ctx_p->ut.uid,
|
---|
424 | prev_ctx_p->ut.gid,
|
---|
425 | prev_ctx_p->ut.ngroups,
|
---|
426 | prev_ctx_p->ut.groups);
|
---|
427 |
|
---|
428 | /* Update current_user stuff */
|
---|
429 |
|
---|
430 | current_user.ut.uid = prev_ctx_p->ut.uid;
|
---|
431 | current_user.ut.gid = prev_ctx_p->ut.gid;
|
---|
432 | current_user.ut.ngroups = prev_ctx_p->ut.ngroups;
|
---|
433 | current_user.ut.groups = prev_ctx_p->ut.groups;
|
---|
434 | current_user.nt_user_token = prev_ctx_p->token;
|
---|
435 |
|
---|
436 | END_PROFILE(pop_sec_ctx);
|
---|
437 |
|
---|
438 | DEBUG(4, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
|
---|
439 | (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
|
---|
440 |
|
---|
441 | return True;
|
---|
442 | }
|
---|
443 |
|
---|
444 | /* Initialise the security context system */
|
---|
445 |
|
---|
446 | void init_sec_ctx(void)
|
---|
447 | {
|
---|
448 | int i;
|
---|
449 | struct sec_ctx *ctx_p;
|
---|
450 |
|
---|
451 | /* Initialise security context stack */
|
---|
452 |
|
---|
453 | memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
|
---|
454 |
|
---|
455 | for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
|
---|
456 | sec_ctx_stack[i].ut.uid = (uid_t)-1;
|
---|
457 | sec_ctx_stack[i].ut.gid = (gid_t)-1;
|
---|
458 | }
|
---|
459 |
|
---|
460 | /* Initialise first level of stack. It is the current context */
|
---|
461 | ctx_p = &sec_ctx_stack[0];
|
---|
462 |
|
---|
463 | ctx_p->ut.uid = geteuid();
|
---|
464 | ctx_p->ut.gid = getegid();
|
---|
465 |
|
---|
466 | get_current_groups(ctx_p->ut.gid, &ctx_p->ut.ngroups, &ctx_p->ut.groups);
|
---|
467 |
|
---|
468 | ctx_p->token = NULL; /* Maps to guest user. */
|
---|
469 |
|
---|
470 | /* Initialise current_user global */
|
---|
471 |
|
---|
472 | current_user.ut.uid = ctx_p->ut.uid;
|
---|
473 | current_user.ut.gid = ctx_p->ut.gid;
|
---|
474 | current_user.ut.ngroups = ctx_p->ut.ngroups;
|
---|
475 | current_user.ut.groups = ctx_p->ut.groups;
|
---|
476 |
|
---|
477 | /* The conn and vuid are usually taken care of by other modules.
|
---|
478 | We initialise them here. */
|
---|
479 |
|
---|
480 | current_user.conn = NULL;
|
---|
481 | current_user.vuid = UID_FIELD_INVALID;
|
---|
482 | current_user.nt_user_token = NULL;
|
---|
483 | }
|
---|
484 |
|
---|
485 | /*************************************************************
|
---|
486 | Called when we're inside a become_root() temporary escalation
|
---|
487 | of privileges and the nt_user_token is NULL. Return the last
|
---|
488 | active token on the context stack. We know there is at least
|
---|
489 | one valid non-NULL token on the stack so panic if we underflow.
|
---|
490 | *************************************************************/
|
---|
491 |
|
---|
492 | const struct security_token *sec_ctx_active_token(void)
|
---|
493 | {
|
---|
494 | int stack_index = sec_ctx_stack_ndx;
|
---|
495 | struct sec_ctx *ctx_p = &sec_ctx_stack[stack_index];
|
---|
496 |
|
---|
497 | while (ctx_p->token == NULL) {
|
---|
498 | stack_index--;
|
---|
499 | if (stack_index < 0) {
|
---|
500 | DEBUG(0, ("Security context active token "
|
---|
501 | "stack underflow!\n"));
|
---|
502 | smb_panic("Security context active token "
|
---|
503 | "stack underflow!");
|
---|
504 | }
|
---|
505 | ctx_p = &sec_ctx_stack[stack_index];
|
---|
506 | }
|
---|
507 | return ctx_p->token;
|
---|
508 | }
|
---|