s3:auth: pass AUTH_SESSION_INFO_* flags to finalize_local_nt_token()
[Samba.git] / source3 / auth / token_util.c
blobacb916ab55ca962147b36d3f7f5ef48841edac8b
1 /*
2 * Unix SMB/CIFS implementation.
3 * Authentication utility functions
4 * Copyright (C) Andrew Tridgell 1992-1998
5 * Copyright (C) Andrew Bartlett 2001
6 * Copyright (C) Jeremy Allison 2000-2001
7 * Copyright (C) Rafal Szczesniak 2002
8 * Copyright (C) Volker Lendecke 2006
9 * Copyright (C) Michael Adam 2007
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 /* functions moved from auth/auth_util.c to minimize linker deps */
27 #include "includes.h"
28 #include "lib/util_unixsids.h"
29 #include "system/passwd.h"
30 #include "auth.h"
31 #include "secrets.h"
32 #include "../lib/util/memcache.h"
33 #include "../librpc/gen_ndr/netlogon.h"
34 #include "../libcli/security/security.h"
35 #include "../lib/util/util_pw.h"
36 #include "passdb.h"
37 #include "lib/privileges.h"
39 /****************************************************************************
40 Check for a SID in an struct security_token
41 ****************************************************************************/
43 bool nt_token_check_sid ( const struct dom_sid *sid, const struct security_token *token )
45 if ( !sid || !token )
46 return False;
48 return security_token_has_sid(token, sid);
51 bool nt_token_check_domain_rid( struct security_token *token, uint32_t rid )
53 struct dom_sid domain_sid;
55 /* if we are a domain member, the get the domain SID, else for
56 a DC or standalone server, use our own SID */
58 if ( lp_server_role() == ROLE_DOMAIN_MEMBER ) {
59 if ( !secrets_fetch_domain_sid( lp_workgroup(),
60 &domain_sid ) ) {
61 DEBUG(1,("nt_token_check_domain_rid: Cannot lookup "
62 "SID for domain [%s]\n", lp_workgroup()));
63 return False;
66 else
67 sid_copy( &domain_sid, get_global_sam_sid() );
69 sid_append_rid( &domain_sid, rid );
71 return nt_token_check_sid( &domain_sid, token );\
74 /******************************************************************************
75 Create a token for the root user to be used internally by smbd.
76 This is similar to running under the context of the LOCAL_SYSTEM account
77 in Windows. This is a read-only token. Do not modify it or free() it.
78 Create a copy if you need to change it.
79 ******************************************************************************/
81 struct security_token *get_root_nt_token( void )
83 struct security_token *token, *for_cache;
84 struct dom_sid u_sid, g_sid;
85 struct passwd *pw;
86 void *cache_data;
88 cache_data = memcache_lookup_talloc(
89 NULL, SINGLETON_CACHE_TALLOC,
90 data_blob_string_const_null("root_nt_token"));
92 if (cache_data != NULL) {
93 return talloc_get_type_abort(
94 cache_data, struct security_token);
97 if ( !(pw = getpwuid(0)) ) {
98 if ( !(pw = getpwnam("root")) ) {
99 DEBUG(0,("get_root_nt_token: both getpwuid(0) "
100 "and getpwnam(\"root\") failed!\n"));
101 return NULL;
105 /* get the user and primary group SIDs; although the
106 BUILTIN\Administrators SId is really the one that matters here */
108 uid_to_sid(&u_sid, pw->pw_uid);
109 gid_to_sid(&g_sid, pw->pw_gid);
111 token = create_local_nt_token(talloc_tos(), &u_sid, False,
112 1, &global_sid_Builtin_Administrators);
114 security_token_set_privilege(token, SEC_PRIV_DISK_OPERATOR);
116 for_cache = token;
118 memcache_add_talloc(
119 NULL, SINGLETON_CACHE_TALLOC,
120 data_blob_string_const_null("root_nt_token"), &for_cache);
122 return token;
127 * Add alias SIDs from memberships within the partially created token SID list
130 NTSTATUS add_aliases(const struct dom_sid *domain_sid,
131 struct security_token *token)
133 uint32_t *aliases;
134 size_t i, num_aliases;
135 NTSTATUS status;
136 TALLOC_CTX *tmp_ctx;
138 if (!(tmp_ctx = talloc_init("add_aliases"))) {
139 return NT_STATUS_NO_MEMORY;
142 aliases = NULL;
143 num_aliases = 0;
145 status = pdb_enum_alias_memberships(tmp_ctx, domain_sid,
146 token->sids,
147 token->num_sids,
148 &aliases, &num_aliases);
150 if (!NT_STATUS_IS_OK(status)) {
151 DEBUG(10, ("pdb_enum_alias_memberships failed: %s\n",
152 nt_errstr(status)));
153 goto done;
156 for (i=0; i<num_aliases; i++) {
157 struct dom_sid alias_sid;
158 sid_compose(&alias_sid, domain_sid, aliases[i]);
159 status = add_sid_to_array_unique(token, &alias_sid,
160 &token->sids,
161 &token->num_sids);
162 if (!NT_STATUS_IS_OK(status)) {
163 DEBUG(0, ("add_sid_to_array failed\n"));
164 goto done;
168 done:
169 TALLOC_FREE(tmp_ctx);
170 return NT_STATUS_OK;
173 /*******************************************************************
174 *******************************************************************/
176 static NTSTATUS add_builtin_administrators(struct security_token *token,
177 const struct dom_sid *dom_sid)
179 struct dom_sid domadm;
180 NTSTATUS status;
182 /* nothing to do if we aren't in a domain */
184 if ( !(IS_DC || lp_server_role()==ROLE_DOMAIN_MEMBER) ) {
185 return NT_STATUS_OK;
188 /* Find the Domain Admins SID */
190 if ( IS_DC ) {
191 sid_copy( &domadm, get_global_sam_sid() );
192 } else {
193 if (dom_sid == NULL) {
194 return NT_STATUS_INVALID_PARAMETER_MIX;
196 sid_copy(&domadm, dom_sid);
198 sid_append_rid( &domadm, DOMAIN_RID_ADMINS );
200 /* Add Administrators if the user beloongs to Domain Admins */
202 if ( nt_token_check_sid( &domadm, token ) ) {
203 status = add_sid_to_array(token,
204 &global_sid_Builtin_Administrators,
205 &token->sids, &token->num_sids);
206 if (!NT_STATUS_IS_OK(status)) {
207 return status;
211 return NT_STATUS_OK;
214 static NTSTATUS add_builtin_guests(struct security_token *token,
215 const struct dom_sid *dom_sid)
217 struct dom_sid tmp_sid;
218 NTSTATUS status;
221 * First check the local GUEST account.
223 sid_copy(&tmp_sid, get_global_sam_sid());
224 sid_append_rid(&tmp_sid, DOMAIN_RID_GUEST);
226 if (nt_token_check_sid(&tmp_sid, token)) {
227 status = add_sid_to_array_unique(token,
228 &global_sid_Builtin_Guests,
229 &token->sids, &token->num_sids);
230 if (!NT_STATUS_IS_OK(status)) {
231 return status;
234 return NT_STATUS_OK;
238 * First check the local GUESTS group.
240 sid_copy(&tmp_sid, get_global_sam_sid());
241 sid_append_rid(&tmp_sid, DOMAIN_RID_GUESTS);
243 if (nt_token_check_sid(&tmp_sid, token)) {
244 status = add_sid_to_array_unique(token,
245 &global_sid_Builtin_Guests,
246 &token->sids, &token->num_sids);
247 if (!NT_STATUS_IS_OK(status)) {
248 return status;
251 return NT_STATUS_OK;
254 if (lp_server_role() != ROLE_DOMAIN_MEMBER) {
255 return NT_STATUS_OK;
258 if (dom_sid == NULL) {
259 return NT_STATUS_INVALID_PARAMETER_MIX;
263 * First check the domain GUESTS group.
265 sid_copy(&tmp_sid, dom_sid);
266 sid_append_rid(&tmp_sid, DOMAIN_RID_GUESTS);
268 if (nt_token_check_sid(&tmp_sid, token)) {
269 status = add_sid_to_array_unique(token,
270 &global_sid_Builtin_Guests,
271 &token->sids, &token->num_sids);
272 if (!NT_STATUS_IS_OK(status)) {
273 return status;
276 return NT_STATUS_OK;
279 return NT_STATUS_OK;
282 static NTSTATUS add_local_groups(struct security_token *result,
283 bool is_guest);
284 static NTSTATUS finalize_local_nt_token(struct security_token *result,
285 uint32_t session_info_flags);
287 NTSTATUS get_user_sid_info3_and_extra(const struct netr_SamInfo3 *info3,
288 const struct extra_auth_info *extra,
289 struct dom_sid *sid)
291 /* USER SID */
292 if (info3->base.rid == (uint32_t)(-1)) {
293 /* this is a signal the user was fake and generated,
294 * the actual SID we want to use is stored in the extra
295 * sids */
296 if (is_null_sid(&extra->user_sid)) {
297 /* we couldn't find the user sid, bail out */
298 DEBUG(3, ("Invalid user SID\n"));
299 return NT_STATUS_UNSUCCESSFUL;
301 sid_copy(sid, &extra->user_sid);
302 } else {
303 sid_copy(sid, info3->base.domain_sid);
304 sid_append_rid(sid, info3->base.rid);
306 return NT_STATUS_OK;
309 NTSTATUS create_local_nt_token_from_info3(TALLOC_CTX *mem_ctx,
310 bool is_guest,
311 const struct netr_SamInfo3 *info3,
312 const struct extra_auth_info *extra,
313 struct security_token **ntok)
315 struct security_token *usrtok = NULL;
316 uint32_t session_info_flags = 0;
317 NTSTATUS status;
318 int i;
320 DEBUG(10, ("Create local NT token for %s\n",
321 info3->base.account_name.string));
323 usrtok = talloc_zero(mem_ctx, struct security_token);
324 if (!usrtok) {
325 DEBUG(0, ("talloc failed\n"));
326 return NT_STATUS_NO_MEMORY;
329 /* Add the user and primary group sid FIRST */
330 /* check if the user rid is the special "Domain Guests" rid.
331 * If so pick the first sid for the extra sids instead as it
332 * is a local fake account */
333 usrtok->sids = talloc_array(usrtok, struct dom_sid, 2);
334 if (!usrtok->sids) {
335 TALLOC_FREE(usrtok);
336 return NT_STATUS_NO_MEMORY;
338 usrtok->num_sids = 2;
340 status = get_user_sid_info3_and_extra(info3, extra, &usrtok->sids[0]);
341 if (!NT_STATUS_IS_OK(status)) {
342 TALLOC_FREE(usrtok);
343 return status;
346 /* GROUP SID */
347 if (info3->base.primary_gid == (uint32_t)(-1)) {
348 /* this is a signal the user was fake and generated,
349 * the actual SID we want to use is stored in the extra
350 * sids */
351 if (is_null_sid(&extra->pgid_sid)) {
352 /* we couldn't find the user sid, bail out */
353 DEBUG(3, ("Invalid group SID\n"));
354 TALLOC_FREE(usrtok);
355 return NT_STATUS_UNSUCCESSFUL;
357 sid_copy(&usrtok->sids[1], &extra->pgid_sid);
358 } else {
359 sid_copy(&usrtok->sids[1], info3->base.domain_sid);
360 sid_append_rid(&usrtok->sids[1],
361 info3->base.primary_gid);
364 /* Now the SIDs we got from authentication. These are the ones from
365 * the info3 struct or from the pdb_enum_group_memberships, depending
366 * on who authenticated the user.
367 * Note that we start the for loop at "1" here, we already added the
368 * first group sid as primary above. */
370 for (i = 0; i < info3->base.groups.count; i++) {
371 struct dom_sid tmp_sid;
373 sid_copy(&tmp_sid, info3->base.domain_sid);
374 sid_append_rid(&tmp_sid, info3->base.groups.rids[i].rid);
376 status = add_sid_to_array_unique(usrtok, &tmp_sid,
377 &usrtok->sids,
378 &usrtok->num_sids);
379 if (!NT_STATUS_IS_OK(status)) {
380 DEBUG(3, ("Failed to add SID to nt token\n"));
381 TALLOC_FREE(usrtok);
382 return status;
386 /* now also add extra sids if they are not the special user/group
387 * sids */
388 for (i = 0; i < info3->sidcount; i++) {
389 status = add_sid_to_array_unique(usrtok,
390 info3->sids[i].sid,
391 &usrtok->sids,
392 &usrtok->num_sids);
393 if (!NT_STATUS_IS_OK(status)) {
394 DEBUG(3, ("Failed to add SID to nt token\n"));
395 TALLOC_FREE(usrtok);
396 return status;
400 status = add_local_groups(usrtok, is_guest);
401 if (!NT_STATUS_IS_OK(status)) {
402 DEBUG(3, ("Failed to add local groups\n"));
403 TALLOC_FREE(usrtok);
404 return status;
407 session_info_flags |= AUTH_SESSION_INFO_DEFAULT_GROUPS;
408 if (!is_guest) {
409 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
412 status = finalize_local_nt_token(usrtok, session_info_flags);
413 if (!NT_STATUS_IS_OK(status)) {
414 DEBUG(3, ("Failed to finalize nt token\n"));
415 TALLOC_FREE(usrtok);
416 return status;
419 *ntok = usrtok;
420 return NT_STATUS_OK;
423 /*******************************************************************
424 Create a NT token for the user, expanding local aliases
425 *******************************************************************/
427 struct security_token *create_local_nt_token(TALLOC_CTX *mem_ctx,
428 const struct dom_sid *user_sid,
429 bool is_guest,
430 int num_groupsids,
431 const struct dom_sid *groupsids)
433 struct security_token *result = NULL;
434 int i;
435 NTSTATUS status;
436 uint32_t session_info_flags = 0;
438 DEBUG(10, ("Create local NT token for %s\n",
439 sid_string_dbg(user_sid)));
441 if (!(result = talloc_zero(mem_ctx, struct security_token))) {
442 DEBUG(0, ("talloc failed\n"));
443 return NULL;
446 /* Add the user and primary group sid */
448 status = add_sid_to_array(result, user_sid,
449 &result->sids, &result->num_sids);
450 if (!NT_STATUS_IS_OK(status)) {
451 TALLOC_FREE(result);
452 return NULL;
455 /* For guest, num_groupsids may be zero. */
456 if (num_groupsids) {
457 status = add_sid_to_array(result, &groupsids[0],
458 &result->sids,
459 &result->num_sids);
460 if (!NT_STATUS_IS_OK(status)) {
461 TALLOC_FREE(result);
462 return NULL;
466 /* Now the SIDs we got from authentication. These are the ones from
467 * the info3 struct or from the pdb_enum_group_memberships, depending
468 * on who authenticated the user.
469 * Note that we start the for loop at "1" here, we already added the
470 * first group sid as primary above. */
472 for (i=1; i<num_groupsids; i++) {
473 status = add_sid_to_array_unique(result, &groupsids[i],
474 &result->sids,
475 &result->num_sids);
476 if (!NT_STATUS_IS_OK(status)) {
477 TALLOC_FREE(result);
478 return NULL;
482 status = add_local_groups(result, is_guest);
483 if (!NT_STATUS_IS_OK(status)) {
484 TALLOC_FREE(result);
485 return NULL;
488 session_info_flags |= AUTH_SESSION_INFO_DEFAULT_GROUPS;
489 if (!is_guest) {
490 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
493 status = finalize_local_nt_token(result, session_info_flags);
494 if (!NT_STATUS_IS_OK(status)) {
495 TALLOC_FREE(result);
496 return NULL;
499 if (is_guest) {
501 * It's ugly, but for now it's
502 * needed to add Builtin_Guests
503 * here, the "local" token only
504 * consist of S-1-22-* SIDs
505 * and finalize_local_nt_token()
506 * doesn't have the chance to
507 * to detect it need to
508 * add Builtin_Guests via
509 * add_builtin_guests().
511 status = add_sid_to_array_unique(result,
512 &global_sid_Builtin_Guests,
513 &result->sids,
514 &result->num_sids);
515 if (!NT_STATUS_IS_OK(status)) {
516 DEBUG(3, ("Failed to add SID to nt token\n"));
517 TALLOC_FREE(result);
518 return NULL;
522 return result;
525 /***************************************************
526 Merge in any groups from /etc/group.
527 ***************************************************/
529 static NTSTATUS add_local_groups(struct security_token *result,
530 bool is_guest)
532 gid_t *gids = NULL;
533 uint32_t getgroups_num_group_sids = 0;
534 struct passwd *pass = NULL;
535 TALLOC_CTX *tmp_ctx = talloc_stackframe();
536 int i;
538 if (is_guest) {
540 * Guest is a special case. It's always
541 * a user that can be looked up, but
542 * result->sids[0] is set to DOMAIN\Guest.
543 * Lookup by account name instead.
545 pass = Get_Pwnam_alloc(tmp_ctx, lp_guest_account());
546 } else {
547 uid_t uid;
549 /* For non-guest result->sids[0] is always the user sid. */
550 if (!sid_to_uid(&result->sids[0], &uid)) {
552 * Non-mappable SID like SYSTEM.
553 * Can't be in any /etc/group groups.
555 TALLOC_FREE(tmp_ctx);
556 return NT_STATUS_OK;
559 pass = getpwuid_alloc(tmp_ctx, uid);
560 if (pass == NULL) {
561 DEBUG(1, ("SID %s -> getpwuid(%u) failed\n",
562 sid_string_dbg(&result->sids[0]),
563 (unsigned int)uid));
567 if (!pass) {
568 TALLOC_FREE(tmp_ctx);
569 return NT_STATUS_UNSUCCESSFUL;
573 * Now we must get any groups this user has been
574 * added to in /etc/group and merge them in.
575 * This has to be done in every code path
576 * that creates an NT token, as remote users
577 * may have been added to the local /etc/group
578 * database. Tokens created merely from the
579 * info3 structs (via the DC or via the krb5 PAC)
580 * won't have these local groups. Note the
581 * groups added here will only be UNIX groups
582 * (S-1-22-2-XXXX groups) as getgroups_unix_user()
583 * turns off winbindd before calling getgroups().
585 * NB. This is duplicating work already
586 * done in the 'unix_user:' case of
587 * create_token_from_sid() but won't
588 * do anything other than be inefficient
589 * in that case.
592 if (!getgroups_unix_user(tmp_ctx, pass->pw_name, pass->pw_gid,
593 &gids, &getgroups_num_group_sids)) {
594 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
595 pass->pw_name));
596 TALLOC_FREE(tmp_ctx);
597 return NT_STATUS_UNSUCCESSFUL;
600 for (i=0; i<getgroups_num_group_sids; i++) {
601 NTSTATUS status;
602 struct dom_sid grp_sid;
603 gid_to_sid(&grp_sid, gids[i]);
605 status = add_sid_to_array_unique(result,
606 &grp_sid,
607 &result->sids,
608 &result->num_sids);
609 if (!NT_STATUS_IS_OK(status)) {
610 DEBUG(3, ("Failed to add UNIX SID to nt token\n"));
611 TALLOC_FREE(tmp_ctx);
612 return status;
615 TALLOC_FREE(tmp_ctx);
616 return NT_STATUS_OK;
619 static NTSTATUS finalize_local_nt_token(struct security_token *result,
620 uint32_t session_info_flags)
622 struct dom_sid _dom_sid = { 0, };
623 struct dom_sid *domain_sid = NULL;
624 NTSTATUS status;
625 struct acct_info *info;
626 bool ok;
628 result->privilege_mask = 0;
629 result->rights_mask = 0;
631 if (result->num_sids == 0) {
632 return NT_STATUS_INVALID_TOKEN;
635 if (session_info_flags & AUTH_SESSION_INFO_DEFAULT_GROUPS) {
636 status = add_sid_to_array(result, &global_sid_World,
637 &result->sids, &result->num_sids);
638 if (!NT_STATUS_IS_OK(status)) {
639 return status;
641 status = add_sid_to_array(result, &global_sid_Network,
642 &result->sids, &result->num_sids);
643 if (!NT_STATUS_IS_OK(status)) {
644 return status;
649 * Don't expand nested groups of system, anonymous etc
651 * Note that they still get SID_WORLD and SID_NETWORK
652 * for now in order let existing tests pass.
654 * But SYSTEM doesn't get AUTHENTICATED_USERS
655 * and ANONYMOUS doesn't get BUILTIN GUESTS anymore.
657 if (security_token_is_anonymous(result)) {
658 return NT_STATUS_OK;
660 if (security_token_is_system(result)) {
661 result->privilege_mask = ~0;
662 return NT_STATUS_OK;
665 if (session_info_flags & AUTH_SESSION_INFO_AUTHENTICATED) {
666 status = add_sid_to_array(result,
667 &global_sid_Authenticated_Users,
668 &result->sids,
669 &result->num_sids);
670 if (!NT_STATUS_IS_OK(status)) {
671 return status;
675 /* Add in BUILTIN sids */
677 become_root();
678 ok = secrets_fetch_domain_sid(lp_workgroup(), &_dom_sid);
679 if (ok) {
680 domain_sid = &_dom_sid;
681 } else {
682 DEBUG(3, ("Failed to fetch domain sid for %s\n",
683 lp_workgroup()));
685 unbecome_root();
687 info = talloc_zero(talloc_tos(), struct acct_info);
688 if (info == NULL) {
689 DEBUG(0, ("talloc failed!\n"));
690 return NT_STATUS_NO_MEMORY;
693 /* Deal with the BUILTIN\Administrators group. If the SID can
694 be resolved then assume that the add_aliasmem( S-1-5-32 )
695 handled it. */
697 status = pdb_get_aliasinfo(&global_sid_Builtin_Administrators, info);
698 if (!NT_STATUS_IS_OK(status)) {
700 become_root();
701 status = create_builtin_administrators(domain_sid);
702 unbecome_root();
704 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
705 /* Add BUILTIN\Administrators directly to token. */
706 status = add_builtin_administrators(result, domain_sid);
707 if ( !NT_STATUS_IS_OK(status) ) {
708 DEBUG(3, ("Failed to check for local "
709 "Administrators membership (%s)\n",
710 nt_errstr(status)));
712 } else if (!NT_STATUS_IS_OK(status)) {
713 DEBUG(2, ("WARNING: Failed to create "
714 "BUILTIN\\Administrators group! Can "
715 "Winbind allocate gids?\n"));
719 /* Deal with the BUILTIN\Users group. If the SID can
720 be resolved then assume that the add_aliasmem( S-1-5-32 )
721 handled it. */
723 status = pdb_get_aliasinfo(&global_sid_Builtin_Users, info);
724 if (!NT_STATUS_IS_OK(status)) {
726 become_root();
727 status = create_builtin_users(domain_sid);
728 unbecome_root();
730 if (!NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE) &&
731 !NT_STATUS_IS_OK(status))
733 DEBUG(2, ("WARNING: Failed to create BUILTIN\\Users group! "
734 "Can Winbind allocate gids?\n"));
739 * Add BUILTIN\Guests directly to token.
740 * But only if the token already indicates
741 * real guest access by:
742 * - local GUEST account
743 * - local GUESTS group
744 * - domain GUESTS group
746 * Even if a user was authenticated, it
747 * can be member of a guest related group.
749 status = add_builtin_guests(result, domain_sid);
750 if (!NT_STATUS_IS_OK(status)) {
751 DEBUG(3, ("Failed to check for local "
752 "Guests membership (%s)\n",
753 nt_errstr(status)));
755 * This is a hard error.
757 return status;
760 TALLOC_FREE(info);
762 /* Deal with local groups */
764 if (lp_winbind_nested_groups()) {
766 become_root();
768 /* Now add the aliases. First the one from our local SAM */
770 status = add_aliases(get_global_sam_sid(), result);
772 if (!NT_STATUS_IS_OK(status)) {
773 unbecome_root();
774 return status;
777 /* Finally the builtin ones */
779 status = add_aliases(&global_sid_Builtin, result);
781 if (!NT_STATUS_IS_OK(status)) {
782 unbecome_root();
783 return status;
786 unbecome_root();
790 if (session_info_flags & AUTH_SESSION_INFO_SIMPLE_PRIVILEGES) {
791 if (security_token_has_builtin_administrators(result)) {
792 result->privilege_mask = ~0;
794 } else {
795 /* Add privileges based on current user sids */
796 get_privileges_for_sids(&result->privilege_mask, result->sids,
797 result->num_sids);
800 return NT_STATUS_OK;
803 /****************************************************************************
804 prints a UNIX 'token' to debug output.
805 ****************************************************************************/
807 void debug_unix_user_token(int dbg_class, int dbg_lev, uid_t uid, gid_t gid,
808 int n_groups, gid_t *groups)
810 int i;
811 DEBUGC(dbg_class, dbg_lev,
812 ("UNIX token of user %ld\n", (long int)uid));
814 DEBUGADDC(dbg_class, dbg_lev,
815 ("Primary group is %ld and contains %i supplementary "
816 "groups\n", (long int)gid, n_groups));
817 for (i = 0; i < n_groups; i++)
818 DEBUGADDC(dbg_class, dbg_lev, ("Group[%3i]: %ld\n", i,
819 (long int)groups[i]));
823 * Create an artificial NT token given just a domain SID.
825 * We have 3 cases:
827 * unmapped unix users: Go directly to nss to find the user's group.
829 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
831 * If the user is provided by winbind, the primary gid is set to "domain
832 * users" of the user's domain. For an explanation why this is necessary, see
833 * the thread starting at
834 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
837 static NTSTATUS create_token_from_sid(TALLOC_CTX *mem_ctx,
838 const struct dom_sid *user_sid,
839 bool is_guest,
840 uid_t *uid, gid_t *gid,
841 char **found_username,
842 struct security_token **token)
844 NTSTATUS result = NT_STATUS_NO_SUCH_USER;
845 TALLOC_CTX *tmp_ctx = talloc_stackframe();
846 gid_t *gids;
847 struct dom_sid *group_sids;
848 struct dom_sid tmp_sid;
849 uint32_t num_group_sids;
850 uint32_t num_gids;
851 uint32_t i;
852 uint32_t high, low;
853 bool range_ok;
855 if (sid_check_is_in_our_sam(user_sid)) {
856 bool ret;
857 uint32_t pdb_num_group_sids;
858 /* This is a passdb user, so ask passdb */
860 struct samu *sam_acct = NULL;
862 if ( !(sam_acct = samu_new( tmp_ctx )) ) {
863 result = NT_STATUS_NO_MEMORY;
864 goto done;
867 become_root();
868 ret = pdb_getsampwsid(sam_acct, user_sid);
869 unbecome_root();
871 if (!ret) {
872 DEBUG(1, ("pdb_getsampwsid(%s) failed\n",
873 sid_string_dbg(user_sid)));
874 DEBUGADD(1, ("Fall back to unix user\n"));
875 goto unix_user;
878 result = pdb_enum_group_memberships(tmp_ctx, sam_acct,
879 &group_sids, &gids,
880 &pdb_num_group_sids);
881 if (!NT_STATUS_IS_OK(result)) {
882 DEBUG(1, ("enum_group_memberships failed for %s: "
883 "%s\n", sid_string_dbg(user_sid),
884 nt_errstr(result)));
885 DEBUGADD(1, ("Fall back to unix uid lookup\n"));
886 goto unix_user;
888 num_group_sids = pdb_num_group_sids;
890 /* see the smb_panic() in pdb_default_enum_group_memberships */
891 SMB_ASSERT(num_group_sids > 0);
893 /* Ensure we're returning the found_username on the right context. */
894 *found_username = talloc_strdup(mem_ctx,
895 pdb_get_username(sam_acct));
897 if (*found_username == NULL) {
898 result = NT_STATUS_NO_MEMORY;
899 goto done;
903 * If the SID from lookup_name() was the guest sid, passdb knows
904 * about the mapping of guest sid to lp_guest_account()
905 * username and will return the unix_pw info for a guest
906 * user. Use it if it's there, else lookup the *uid details
907 * using Get_Pwnam_alloc(). See bug #6291 for details. JRA.
910 /* We must always assign the *uid. */
911 if (sam_acct->unix_pw == NULL) {
912 struct passwd *pwd = Get_Pwnam_alloc(sam_acct, *found_username );
913 if (!pwd) {
914 DEBUG(10, ("Get_Pwnam_alloc failed for %s\n",
915 *found_username));
916 result = NT_STATUS_NO_SUCH_USER;
917 goto done;
919 result = samu_set_unix(sam_acct, pwd );
920 if (!NT_STATUS_IS_OK(result)) {
921 DEBUG(10, ("samu_set_unix failed for %s\n",
922 *found_username));
923 result = NT_STATUS_NO_SUCH_USER;
924 goto done;
927 *uid = sam_acct->unix_pw->pw_uid;
929 } else if (sid_check_is_in_unix_users(user_sid)) {
930 uint32_t getgroups_num_group_sids;
931 /* This is a unix user not in passdb. We need to ask nss
932 * directly, without consulting passdb */
934 struct passwd *pass;
937 * This goto target is used as a fallback for the passdb
938 * case. The concrete bug report is when passdb gave us an
939 * unmapped gid.
942 unix_user:
944 if (!sid_to_uid(user_sid, uid)) {
945 DEBUG(1, ("unix_user case, sid_to_uid for %s failed\n",
946 sid_string_dbg(user_sid)));
947 result = NT_STATUS_NO_SUCH_USER;
948 goto done;
951 uid_to_unix_users_sid(*uid, &tmp_sid);
952 user_sid = &tmp_sid;
954 pass = getpwuid_alloc(tmp_ctx, *uid);
955 if (pass == NULL) {
956 DEBUG(1, ("getpwuid(%u) failed\n",
957 (unsigned int)*uid));
958 goto done;
961 if (!getgroups_unix_user(tmp_ctx, pass->pw_name, pass->pw_gid,
962 &gids, &getgroups_num_group_sids)) {
963 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
964 pass->pw_name));
965 goto done;
967 num_group_sids = getgroups_num_group_sids;
969 group_sids = talloc_array(tmp_ctx, struct dom_sid, num_group_sids);
970 if (group_sids == NULL) {
971 DEBUG(1, ("talloc_array failed\n"));
972 result = NT_STATUS_NO_MEMORY;
973 goto done;
976 for (i=0; i<num_group_sids; i++) {
977 gid_to_sid(&group_sids[i], gids[i]);
980 /* In getgroups_unix_user we always set the primary gid */
981 SMB_ASSERT(num_group_sids > 0);
983 /* Ensure we're returning the found_username on the right context. */
984 *found_username = talloc_strdup(mem_ctx, pass->pw_name);
985 if (*found_username == NULL) {
986 result = NT_STATUS_NO_MEMORY;
987 goto done;
989 } else {
991 /* This user is from winbind, force the primary gid to the
992 * user's "domain users" group. Under certain circumstances
993 * (user comes from NT4), this might be a loss of
994 * information. But we can not rely on winbind getting the
995 * correct info. AD might prohibit winbind looking up that
996 * information. */
998 /* We must always assign the *uid. */
999 if (!sid_to_uid(user_sid, uid)) {
1000 DEBUG(1, ("winbindd case, sid_to_uid for %s failed\n",
1001 sid_string_dbg(user_sid)));
1002 result = NT_STATUS_NO_SUCH_USER;
1003 goto done;
1006 num_group_sids = 1;
1007 group_sids = talloc_array(tmp_ctx, struct dom_sid, num_group_sids);
1008 if (group_sids == NULL) {
1009 DEBUG(1, ("talloc_array failed\n"));
1010 result = NT_STATUS_NO_MEMORY;
1011 goto done;
1014 gids = talloc_array(tmp_ctx, gid_t, num_group_sids);
1015 if (gids == NULL) {
1016 result = NT_STATUS_NO_MEMORY;
1017 goto done;
1020 sid_copy(&group_sids[0], user_sid);
1021 sid_split_rid(&group_sids[0], NULL);
1022 sid_append_rid(&group_sids[0], DOMAIN_RID_USERS);
1024 if (!sid_to_gid(&group_sids[0], &gids[0])) {
1025 DEBUG(1, ("sid_to_gid(%s) failed\n",
1026 sid_string_dbg(&group_sids[0])));
1027 goto done;
1030 *found_username = NULL;
1033 *gid = gids[0];
1035 /* Add the "Unix Group" SID for each gid to catch mapped groups
1036 and their Unix equivalent. This is to solve the backwards
1037 compatibility problem of 'valid users = +ntadmin' where
1038 ntadmin has been paired with "Domain Admins" in the group
1039 mapping table. Otherwise smb.conf would need to be changed
1040 to 'valid user = "Domain Admins"'. --jerry */
1042 num_gids = num_group_sids;
1043 range_ok = lp_idmap_default_range(&low, &high);
1044 for ( i=0; i<num_gids; i++ ) {
1045 struct dom_sid unix_group_sid;
1047 /* don't pickup anything managed by Winbind */
1048 if (range_ok && (gids[i] >= low) && (gids[i] <= high)) {
1049 continue;
1052 gid_to_unix_groups_sid(gids[i], &unix_group_sid);
1054 result = add_sid_to_array_unique(tmp_ctx, &unix_group_sid,
1055 &group_sids, &num_group_sids);
1056 if (!NT_STATUS_IS_OK(result)) {
1057 goto done;
1061 /* Ensure we're creating the nt_token on the right context. */
1062 *token = create_local_nt_token(mem_ctx, user_sid,
1063 is_guest, num_group_sids, group_sids);
1065 if (*token == NULL) {
1066 result = NT_STATUS_NO_MEMORY;
1067 goto done;
1070 result = NT_STATUS_OK;
1071 done:
1072 TALLOC_FREE(tmp_ctx);
1073 return result;
1077 * Create an artificial NT token given just a username. (Initially intended
1078 * for force user)
1080 * We go through lookup_name() to avoid problems we had with 'winbind use
1081 * default domain'.
1083 * We have 3 cases:
1085 * unmapped unix users: Go directly to nss to find the user's group.
1087 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
1089 * If the user is provided by winbind, the primary gid is set to "domain
1090 * users" of the user's domain. For an explanation why this is necessary, see
1091 * the thread starting at
1092 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
1095 NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username,
1096 bool is_guest,
1097 uid_t *uid, gid_t *gid,
1098 char **found_username,
1099 struct security_token **token)
1101 NTSTATUS result = NT_STATUS_NO_SUCH_USER;
1102 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1103 struct dom_sid user_sid;
1104 enum lsa_SidType type;
1106 if (!lookup_name_smbconf(tmp_ctx, username, LOOKUP_NAME_ALL,
1107 NULL, NULL, &user_sid, &type)) {
1108 DEBUG(1, ("lookup_name_smbconf for %s failed\n", username));
1109 goto done;
1112 if (type != SID_NAME_USER) {
1113 DEBUG(1, ("%s is a %s, not a user\n", username,
1114 sid_type_lookup(type)));
1115 goto done;
1118 result = create_token_from_sid(mem_ctx, &user_sid, is_guest, uid, gid, found_username, token);
1120 if (!NT_STATUS_IS_OK(result)) {
1121 goto done;
1125 * If result == NT_STATUS_OK then
1126 * we know we have a valid token. Ensure
1127 * we also have a valid username to match.
1130 if (*found_username == NULL) {
1131 *found_username = talloc_strdup(mem_ctx, username);
1132 if (*found_username == NULL) {
1133 result = NT_STATUS_NO_MEMORY;
1137 done:
1138 TALLOC_FREE(tmp_ctx);
1139 return result;
1142 /***************************************************************************
1143 Build upon create_token_from_sid:
1145 Expensive helper function to figure out whether a user given its sid is
1146 member of a particular group.
1147 ***************************************************************************/
1149 bool user_sid_in_group_sid(const struct dom_sid *sid, const struct dom_sid *group_sid)
1151 NTSTATUS status;
1152 uid_t uid;
1153 gid_t gid;
1154 char *found_username;
1155 struct security_token *token;
1156 bool result = false;
1157 enum lsa_SidType type;
1158 TALLOC_CTX *mem_ctx = talloc_stackframe();
1160 if (!lookup_sid(mem_ctx, sid,
1161 NULL, NULL, &type)) {
1162 DEBUG(1, ("lookup_sid for %s failed\n", dom_sid_string(mem_ctx, sid)));
1163 goto done;
1166 if (type != SID_NAME_USER) {
1167 DEBUG(5, ("%s is a %s, not a user\n", dom_sid_string(mem_ctx, sid),
1168 sid_type_lookup(type)));
1169 goto done;
1172 status = create_token_from_sid(mem_ctx, sid, False,
1173 &uid, &gid, &found_username,
1174 &token);
1176 if (!NT_STATUS_IS_OK(status)) {
1177 DEBUG(10, ("could not create token for %s\n", dom_sid_string(mem_ctx, sid)));
1178 goto done;
1181 result = security_token_has_sid(token, group_sid);
1183 done:
1184 TALLOC_FREE(mem_ctx);
1185 return result;
1188 /***************************************************************************
1189 Build upon create_token_from_username:
1191 Expensive helper function to figure out whether a user given its name is
1192 member of a particular group.
1193 ***************************************************************************/
1195 bool user_in_group_sid(const char *username, const struct dom_sid *group_sid)
1197 NTSTATUS status;
1198 uid_t uid;
1199 gid_t gid;
1200 char *found_username;
1201 struct security_token *token;
1202 bool result;
1203 TALLOC_CTX *mem_ctx = talloc_stackframe();
1205 status = create_token_from_username(mem_ctx, username, False,
1206 &uid, &gid, &found_username,
1207 &token);
1209 if (!NT_STATUS_IS_OK(status)) {
1210 DEBUG(10, ("could not create token for %s\n", username));
1211 TALLOC_FREE(mem_ctx);
1212 return False;
1215 result = security_token_has_sid(token, group_sid);
1217 TALLOC_FREE(mem_ctx);
1218 return result;
1221 bool user_in_group(const char *username, const char *groupname)
1223 TALLOC_CTX *mem_ctx = talloc_stackframe();
1224 struct dom_sid group_sid;
1225 bool ret;
1227 ret = lookup_name(mem_ctx, groupname, LOOKUP_NAME_ALL,
1228 NULL, NULL, &group_sid, NULL);
1229 TALLOC_FREE(mem_ctx);
1231 if (!ret) {
1232 DEBUG(10, ("lookup_name for (%s) failed.\n", groupname));
1233 return False;
1236 return user_in_group_sid(username, &group_sid);
1239 /* END */