ctdb: Print locks latency in machinereadable stats
[Samba.git] / source3 / auth / token_util.c
blobf7ebd23e4aadfc4bc49454ac392496589d17fc30
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_compose(&tmp_sid, get_global_sam_sid(), DOMAIN_RID_GUEST);
225 if (nt_token_check_sid(&tmp_sid, token)) {
226 status = add_sid_to_array_unique(token,
227 &global_sid_Builtin_Guests,
228 &token->sids, &token->num_sids);
229 if (!NT_STATUS_IS_OK(status)) {
230 return status;
233 return NT_STATUS_OK;
237 * First check the local GUESTS group.
239 sid_compose(&tmp_sid, get_global_sam_sid(), DOMAIN_RID_GUESTS);
241 if (nt_token_check_sid(&tmp_sid, token)) {
242 status = add_sid_to_array_unique(token,
243 &global_sid_Builtin_Guests,
244 &token->sids, &token->num_sids);
245 if (!NT_STATUS_IS_OK(status)) {
246 return status;
249 return NT_STATUS_OK;
252 if (lp_server_role() != ROLE_DOMAIN_MEMBER) {
253 return NT_STATUS_OK;
256 if (dom_sid == NULL) {
257 return NT_STATUS_INVALID_PARAMETER_MIX;
261 * First check the domain GUESTS group.
263 sid_copy(&tmp_sid, dom_sid);
264 sid_append_rid(&tmp_sid, DOMAIN_RID_GUESTS);
266 if (nt_token_check_sid(&tmp_sid, token)) {
267 status = add_sid_to_array_unique(token,
268 &global_sid_Builtin_Guests,
269 &token->sids, &token->num_sids);
270 if (!NT_STATUS_IS_OK(status)) {
271 return status;
274 return NT_STATUS_OK;
277 return NT_STATUS_OK;
280 static NTSTATUS add_local_groups(struct security_token *result,
281 bool is_guest);
283 NTSTATUS get_user_sid_info3_and_extra(const struct netr_SamInfo3 *info3,
284 const struct extra_auth_info *extra,
285 struct dom_sid *sid)
287 /* USER SID */
288 if (info3->base.rid == (uint32_t)(-1)) {
289 /* this is a signal the user was fake and generated,
290 * the actual SID we want to use is stored in the extra
291 * sids */
292 if (is_null_sid(&extra->user_sid)) {
293 /* we couldn't find the user sid, bail out */
294 DEBUG(3, ("Invalid user SID\n"));
295 return NT_STATUS_UNSUCCESSFUL;
297 sid_copy(sid, &extra->user_sid);
298 } else {
299 sid_copy(sid, info3->base.domain_sid);
300 sid_append_rid(sid, info3->base.rid);
302 return NT_STATUS_OK;
305 NTSTATUS create_local_nt_token_from_info3(TALLOC_CTX *mem_ctx,
306 bool is_guest,
307 const struct netr_SamInfo3 *info3,
308 const struct extra_auth_info *extra,
309 struct security_token **ntok)
311 struct security_token *usrtok = NULL;
312 uint32_t session_info_flags = 0;
313 NTSTATUS status;
314 int i;
316 DEBUG(10, ("Create local NT token for %s\n",
317 info3->base.account_name.string));
319 usrtok = talloc_zero(mem_ctx, struct security_token);
320 if (!usrtok) {
321 DEBUG(0, ("talloc failed\n"));
322 return NT_STATUS_NO_MEMORY;
325 /* Add the user and primary group sid FIRST */
326 /* check if the user rid is the special "Domain Guests" rid.
327 * If so pick the first sid for the extra sids instead as it
328 * is a local fake account */
329 usrtok->sids = talloc_array(usrtok, struct dom_sid, 2);
330 if (!usrtok->sids) {
331 TALLOC_FREE(usrtok);
332 return NT_STATUS_NO_MEMORY;
334 usrtok->num_sids = 2;
336 status = get_user_sid_info3_and_extra(info3, extra, &usrtok->sids[0]);
337 if (!NT_STATUS_IS_OK(status)) {
338 TALLOC_FREE(usrtok);
339 return status;
342 /* GROUP SID */
343 if (info3->base.primary_gid == (uint32_t)(-1)) {
344 /* this is a signal the user was fake and generated,
345 * the actual SID we want to use is stored in the extra
346 * sids */
347 if (is_null_sid(&extra->pgid_sid)) {
348 /* we couldn't find the user sid, bail out */
349 DEBUG(3, ("Invalid group SID\n"));
350 TALLOC_FREE(usrtok);
351 return NT_STATUS_UNSUCCESSFUL;
353 sid_copy(&usrtok->sids[1], &extra->pgid_sid);
354 } else {
355 sid_copy(&usrtok->sids[1], info3->base.domain_sid);
356 sid_append_rid(&usrtok->sids[1],
357 info3->base.primary_gid);
360 /* Now the SIDs we got from authentication. These are the ones from
361 * the info3 struct or from the pdb_enum_group_memberships, depending
362 * on who authenticated the user.
363 * Note that we start the for loop at "1" here, we already added the
364 * first group sid as primary above. */
366 for (i = 0; i < info3->base.groups.count; i++) {
367 struct dom_sid tmp_sid;
369 sid_copy(&tmp_sid, info3->base.domain_sid);
370 sid_append_rid(&tmp_sid, info3->base.groups.rids[i].rid);
372 status = add_sid_to_array_unique(usrtok, &tmp_sid,
373 &usrtok->sids,
374 &usrtok->num_sids);
375 if (!NT_STATUS_IS_OK(status)) {
376 DEBUG(3, ("Failed to add SID to nt token\n"));
377 TALLOC_FREE(usrtok);
378 return status;
382 /* now also add extra sids if they are not the special user/group
383 * sids */
384 for (i = 0; i < info3->sidcount; i++) {
385 status = add_sid_to_array_unique(usrtok,
386 info3->sids[i].sid,
387 &usrtok->sids,
388 &usrtok->num_sids);
389 if (!NT_STATUS_IS_OK(status)) {
390 DEBUG(3, ("Failed to add SID to nt token\n"));
391 TALLOC_FREE(usrtok);
392 return status;
396 status = add_local_groups(usrtok, is_guest);
397 if (!NT_STATUS_IS_OK(status)) {
398 DEBUG(3, ("Failed to add local groups\n"));
399 TALLOC_FREE(usrtok);
400 return status;
403 session_info_flags |= AUTH_SESSION_INFO_DEFAULT_GROUPS;
404 if (!is_guest) {
405 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
408 status = finalize_local_nt_token(usrtok, session_info_flags);
409 if (!NT_STATUS_IS_OK(status)) {
410 DEBUG(3, ("Failed to finalize nt token\n"));
411 TALLOC_FREE(usrtok);
412 return status;
415 *ntok = usrtok;
416 return NT_STATUS_OK;
419 /*******************************************************************
420 Create a NT token for the user, expanding local aliases
421 *******************************************************************/
423 struct security_token *create_local_nt_token(TALLOC_CTX *mem_ctx,
424 const struct dom_sid *user_sid,
425 bool is_guest,
426 int num_groupsids,
427 const struct dom_sid *groupsids)
429 struct security_token *result = NULL;
430 int i;
431 NTSTATUS status;
432 uint32_t session_info_flags = 0;
433 struct dom_sid_buf buf;
435 DEBUG(10, ("Create local NT token for %s\n",
436 dom_sid_str_buf(user_sid, &buf)));
438 if (!(result = talloc_zero(mem_ctx, struct security_token))) {
439 DEBUG(0, ("talloc failed\n"));
440 return NULL;
443 /* Add the user and primary group sid */
445 status = add_sid_to_array(result, user_sid,
446 &result->sids, &result->num_sids);
447 if (!NT_STATUS_IS_OK(status)) {
448 TALLOC_FREE(result);
449 return NULL;
452 /* For guest, num_groupsids may be zero. */
453 if (num_groupsids) {
454 status = add_sid_to_array(result, &groupsids[0],
455 &result->sids,
456 &result->num_sids);
457 if (!NT_STATUS_IS_OK(status)) {
458 TALLOC_FREE(result);
459 return NULL;
463 /* Now the SIDs we got from authentication. These are the ones from
464 * the info3 struct or from the pdb_enum_group_memberships, depending
465 * on who authenticated the user.
466 * Note that we start the for loop at "1" here, we already added the
467 * first group sid as primary above. */
469 for (i=1; i<num_groupsids; i++) {
470 status = add_sid_to_array_unique(result, &groupsids[i],
471 &result->sids,
472 &result->num_sids);
473 if (!NT_STATUS_IS_OK(status)) {
474 TALLOC_FREE(result);
475 return NULL;
479 status = add_local_groups(result, is_guest);
480 if (!NT_STATUS_IS_OK(status)) {
481 TALLOC_FREE(result);
482 return NULL;
485 session_info_flags |= AUTH_SESSION_INFO_DEFAULT_GROUPS;
486 if (!is_guest) {
487 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
490 status = finalize_local_nt_token(result, session_info_flags);
491 if (!NT_STATUS_IS_OK(status)) {
492 TALLOC_FREE(result);
493 return NULL;
496 if (is_guest) {
498 * It's ugly, but for now it's
499 * needed to add Builtin_Guests
500 * here, the "local" token only
501 * consist of S-1-22-* SIDs
502 * and finalize_local_nt_token()
503 * doesn't have the chance to
504 * to detect it need to
505 * add Builtin_Guests via
506 * add_builtin_guests().
508 status = add_sid_to_array_unique(result,
509 &global_sid_Builtin_Guests,
510 &result->sids,
511 &result->num_sids);
512 if (!NT_STATUS_IS_OK(status)) {
513 DEBUG(3, ("Failed to add SID to nt token\n"));
514 TALLOC_FREE(result);
515 return NULL;
519 return result;
522 /***************************************************
523 Merge in any groups from /etc/group.
524 ***************************************************/
526 static NTSTATUS add_local_groups(struct security_token *result,
527 bool is_guest)
529 gid_t *gids = NULL;
530 uint32_t getgroups_num_group_sids = 0;
531 struct passwd *pass = NULL;
532 TALLOC_CTX *tmp_ctx = talloc_stackframe();
533 int i;
535 if (is_guest) {
537 * Guest is a special case. It's always
538 * a user that can be looked up, but
539 * result->sids[0] is set to DOMAIN\Guest.
540 * Lookup by account name instead.
542 pass = Get_Pwnam_alloc(tmp_ctx, lp_guest_account());
543 } else {
544 uid_t uid;
546 /* For non-guest result->sids[0] is always the user sid. */
547 if (!sid_to_uid(&result->sids[0], &uid)) {
549 * Non-mappable SID like SYSTEM.
550 * Can't be in any /etc/group groups.
552 TALLOC_FREE(tmp_ctx);
553 return NT_STATUS_OK;
556 pass = getpwuid_alloc(tmp_ctx, uid);
557 if (pass == NULL) {
558 struct dom_sid_buf buf;
559 DEBUG(1, ("SID %s -> getpwuid(%u) failed\n",
560 dom_sid_str_buf(&result->sids[0], &buf),
561 (unsigned int)uid));
565 if (!pass) {
566 TALLOC_FREE(tmp_ctx);
567 return NT_STATUS_UNSUCCESSFUL;
571 * Now we must get any groups this user has been
572 * added to in /etc/group and merge them in.
573 * This has to be done in every code path
574 * that creates an NT token, as remote users
575 * may have been added to the local /etc/group
576 * database. Tokens created merely from the
577 * info3 structs (via the DC or via the krb5 PAC)
578 * won't have these local groups. Note the
579 * groups added here will only be UNIX groups
580 * (S-1-22-2-XXXX groups) as getgroups_unix_user()
581 * turns off winbindd before calling getgroups().
583 * NB. This is duplicating work already
584 * done in the 'unix_user:' case of
585 * create_token_from_sid() but won't
586 * do anything other than be inefficient
587 * in that case.
590 if (!getgroups_unix_user(tmp_ctx, pass->pw_name, pass->pw_gid,
591 &gids, &getgroups_num_group_sids)) {
592 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
593 pass->pw_name));
594 TALLOC_FREE(tmp_ctx);
595 return NT_STATUS_UNSUCCESSFUL;
598 for (i=0; i<getgroups_num_group_sids; i++) {
599 NTSTATUS status;
600 struct dom_sid grp_sid;
601 gid_to_sid(&grp_sid, gids[i]);
603 status = add_sid_to_array_unique(result,
604 &grp_sid,
605 &result->sids,
606 &result->num_sids);
607 if (!NT_STATUS_IS_OK(status)) {
608 DEBUG(3, ("Failed to add UNIX SID to nt token\n"));
609 TALLOC_FREE(tmp_ctx);
610 return status;
613 TALLOC_FREE(tmp_ctx);
614 return NT_STATUS_OK;
617 NTSTATUS finalize_local_nt_token(struct security_token *result,
618 uint32_t session_info_flags)
620 struct dom_sid _dom_sid = { 0, };
621 struct dom_sid *domain_sid = NULL;
622 NTSTATUS status;
623 struct acct_info *info;
624 bool ok;
626 result->privilege_mask = 0;
627 result->rights_mask = 0;
629 if (result->num_sids == 0) {
630 return NT_STATUS_INVALID_TOKEN;
633 if (session_info_flags & AUTH_SESSION_INFO_DEFAULT_GROUPS) {
634 status = add_sid_to_array(result, &global_sid_World,
635 &result->sids, &result->num_sids);
636 if (!NT_STATUS_IS_OK(status)) {
637 return status;
639 status = add_sid_to_array(result, &global_sid_Network,
640 &result->sids, &result->num_sids);
641 if (!NT_STATUS_IS_OK(status)) {
642 return status;
647 * Don't expand nested groups of system, anonymous etc
649 * Note that they still get SID_WORLD and SID_NETWORK
650 * for now in order let existing tests pass.
652 * But SYSTEM doesn't get AUTHENTICATED_USERS
653 * and ANONYMOUS doesn't get BUILTIN GUESTS anymore.
655 if (security_token_is_anonymous(result)) {
656 return NT_STATUS_OK;
658 if (security_token_is_system(result)) {
659 result->privilege_mask = ~0;
660 return NT_STATUS_OK;
663 if (session_info_flags & AUTH_SESSION_INFO_AUTHENTICATED) {
664 status = add_sid_to_array(result,
665 &global_sid_Authenticated_Users,
666 &result->sids,
667 &result->num_sids);
668 if (!NT_STATUS_IS_OK(status)) {
669 return status;
673 /* Add in BUILTIN sids */
675 become_root();
676 ok = secrets_fetch_domain_sid(lp_workgroup(), &_dom_sid);
677 if (ok) {
678 domain_sid = &_dom_sid;
679 } else {
680 DEBUG(3, ("Failed to fetch domain sid for %s\n",
681 lp_workgroup()));
683 unbecome_root();
685 info = talloc_zero(talloc_tos(), struct acct_info);
686 if (info == NULL) {
687 DEBUG(0, ("talloc failed!\n"));
688 return NT_STATUS_NO_MEMORY;
691 /* Deal with the BUILTIN\Administrators group. If the SID can
692 be resolved then assume that the add_aliasmem( S-1-5-32 )
693 handled it. */
695 status = pdb_get_aliasinfo(&global_sid_Builtin_Administrators, info);
696 if (!NT_STATUS_IS_OK(status)) {
698 become_root();
699 status = create_builtin_administrators(domain_sid);
700 unbecome_root();
702 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
703 /* Add BUILTIN\Administrators directly to token. */
704 status = add_builtin_administrators(result, domain_sid);
705 if ( !NT_STATUS_IS_OK(status) ) {
706 DEBUG(3, ("Failed to check for local "
707 "Administrators membership (%s)\n",
708 nt_errstr(status)));
710 } else if (!NT_STATUS_IS_OK(status)) {
711 DEBUG(2, ("WARNING: Failed to create "
712 "BUILTIN\\Administrators group! Can "
713 "Winbind allocate gids?\n"));
717 /* Deal with the BUILTIN\Users group. If the SID can
718 be resolved then assume that the add_aliasmem( S-1-5-32 )
719 handled it. */
721 status = pdb_get_aliasinfo(&global_sid_Builtin_Users, info);
722 if (!NT_STATUS_IS_OK(status)) {
724 become_root();
725 status = create_builtin_users(domain_sid);
726 unbecome_root();
728 if (!NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE) &&
729 !NT_STATUS_IS_OK(status))
731 DEBUG(2, ("WARNING: Failed to create BUILTIN\\Users group! "
732 "Can Winbind allocate gids?\n"));
737 * Deal with the BUILTIN\Guests group. If the SID can
738 * be resolved then assume that the add_aliasmem( S-1-5-32 )
739 * handled it.
741 status = pdb_get_aliasinfo(&global_sid_Builtin_Guests, info);
742 if (!NT_STATUS_IS_OK(status)) {
744 become_root();
745 status = create_builtin_guests(domain_sid);
746 unbecome_root();
749 * NT_STATUS_PROTOCOL_UNREACHABLE:
750 * => winbindd is not running.
752 * NT_STATUS_ACCESS_DENIED:
753 * => no idmap config at all
754 * and wbint_AllocateGid()/winbind_allocate_gid()
755 * failed.
757 * NT_STATUS_NO_SUCH_GROUP:
758 * => no idmap config at all and
759 * "tdbsam:map builtin = no" means
760 * wbint_Sids2UnixIDs() fails.
762 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE) ||
763 NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
764 NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_GROUP)) {
766 * Add BUILTIN\Guests directly to token.
767 * But only if the token already indicates
768 * real guest access by:
769 * - local GUEST account
770 * - local GUESTS group
771 * - domain GUESTS group
773 * Even if a user was authenticated, it
774 * can be member of a guest related group.
776 status = add_builtin_guests(result, domain_sid);
777 if (!NT_STATUS_IS_OK(status)) {
778 DEBUG(3, ("Failed to check for local "
779 "Guests membership (%s)\n",
780 nt_errstr(status)));
782 * This is a hard error.
784 return status;
786 } else if (!NT_STATUS_IS_OK(status)) {
787 DEBUG(2, ("Failed to create "
788 "BUILTIN\\Guests group %s! Can "
789 "Winbind allocate gids?\n",
790 nt_errstr(status)));
792 * This is a hard error.
794 return status;
798 TALLOC_FREE(info);
800 /* Deal with local groups */
802 if (lp_winbind_nested_groups()) {
804 become_root();
806 /* Now add the aliases. First the one from our local SAM */
808 status = add_aliases(get_global_sam_sid(), result);
810 if (!NT_STATUS_IS_OK(status)) {
811 unbecome_root();
812 return status;
815 /* Finally the builtin ones */
817 status = add_aliases(&global_sid_Builtin, result);
819 if (!NT_STATUS_IS_OK(status)) {
820 unbecome_root();
821 return status;
824 unbecome_root();
827 if (session_info_flags & AUTH_SESSION_INFO_NTLM) {
828 struct dom_sid tmp_sid = { 0, };
830 ok = dom_sid_parse(SID_NT_NTLM_AUTHENTICATION, &tmp_sid);
831 if (!ok) {
832 return NT_STATUS_NO_MEMORY;
835 status = add_sid_to_array(result,
836 &tmp_sid,
837 &result->sids,
838 &result->num_sids);
839 if (!NT_STATUS_IS_OK(status)) {
840 return status;
844 if (session_info_flags & AUTH_SESSION_INFO_SIMPLE_PRIVILEGES) {
845 if (security_token_has_builtin_administrators(result)) {
846 result->privilege_mask = ~0;
848 } else {
849 /* Add privileges based on current user sids */
850 get_privileges_for_sids(&result->privilege_mask, result->sids,
851 result->num_sids);
854 return NT_STATUS_OK;
857 /****************************************************************************
858 prints a UNIX 'token' to debug output.
859 ****************************************************************************/
861 void debug_unix_user_token(int dbg_class, int dbg_lev, uid_t uid, gid_t gid,
862 int n_groups, gid_t *groups)
864 int i;
865 DEBUGC(dbg_class, dbg_lev,
866 ("UNIX token of user %ld\n", (long int)uid));
868 DEBUGADDC(dbg_class, dbg_lev,
869 ("Primary group is %ld and contains %i supplementary "
870 "groups\n", (long int)gid, n_groups));
871 for (i = 0; i < n_groups; i++)
872 DEBUGADDC(dbg_class, dbg_lev, ("Group[%3i]: %ld\n", i,
873 (long int)groups[i]));
877 * Create an artificial NT token given just a domain SID.
879 * We have 3 cases:
881 * unmapped unix users: Go directly to nss to find the user's group.
883 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
885 * If the user is provided by winbind, the primary gid is set to "domain
886 * users" of the user's domain. For an explanation why this is necessary, see
887 * the thread starting at
888 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
891 static NTSTATUS create_token_from_sid(TALLOC_CTX *mem_ctx,
892 const struct dom_sid *user_sid,
893 bool is_guest,
894 uid_t *uid, gid_t *gid,
895 char **found_username,
896 struct security_token **token)
898 NTSTATUS result = NT_STATUS_NO_SUCH_USER;
899 TALLOC_CTX *tmp_ctx = talloc_stackframe();
900 gid_t *gids;
901 struct dom_sid *group_sids;
902 struct dom_sid tmp_sid;
903 uint32_t num_group_sids;
904 uint32_t num_gids;
905 uint32_t i;
906 uint32_t high, low;
907 bool range_ok;
908 struct dom_sid_buf buf;
910 if (sid_check_is_in_our_sam(user_sid)) {
911 bool ret;
912 uint32_t pdb_num_group_sids;
913 /* This is a passdb user, so ask passdb */
915 struct samu *sam_acct = NULL;
917 if ( !(sam_acct = samu_new( tmp_ctx )) ) {
918 result = NT_STATUS_NO_MEMORY;
919 goto done;
922 become_root();
923 ret = pdb_getsampwsid(sam_acct, user_sid);
924 unbecome_root();
926 if (!ret) {
927 DEBUG(1, ("pdb_getsampwsid(%s) failed\n",
928 dom_sid_str_buf(user_sid, &buf)));
929 DEBUGADD(1, ("Fall back to unix user\n"));
930 goto unix_user;
933 result = pdb_enum_group_memberships(tmp_ctx, sam_acct,
934 &group_sids, &gids,
935 &pdb_num_group_sids);
936 if (!NT_STATUS_IS_OK(result)) {
937 DEBUG(1, ("enum_group_memberships failed for %s: "
938 "%s\n",
939 dom_sid_str_buf(user_sid, &buf),
940 nt_errstr(result)));
941 DEBUGADD(1, ("Fall back to unix uid lookup\n"));
942 goto unix_user;
944 num_group_sids = pdb_num_group_sids;
946 /* see the smb_panic() in pdb_default_enum_group_memberships */
947 SMB_ASSERT(num_group_sids > 0);
949 /* Ensure we're returning the found_username on the right context. */
950 *found_username = talloc_strdup(mem_ctx,
951 pdb_get_username(sam_acct));
953 if (*found_username == NULL) {
954 result = NT_STATUS_NO_MEMORY;
955 goto done;
959 * If the SID from lookup_name() was the guest sid, passdb knows
960 * about the mapping of guest sid to lp_guest_account()
961 * username and will return the unix_pw info for a guest
962 * user. Use it if it's there, else lookup the *uid details
963 * using Get_Pwnam_alloc(). See bug #6291 for details. JRA.
966 /* We must always assign the *uid. */
967 if (sam_acct->unix_pw == NULL) {
968 struct passwd *pwd = Get_Pwnam_alloc(sam_acct, *found_username );
969 if (!pwd) {
970 DEBUG(10, ("Get_Pwnam_alloc failed for %s\n",
971 *found_username));
972 result = NT_STATUS_NO_SUCH_USER;
973 goto done;
975 result = samu_set_unix(sam_acct, pwd );
976 if (!NT_STATUS_IS_OK(result)) {
977 DEBUG(10, ("samu_set_unix failed for %s\n",
978 *found_username));
979 result = NT_STATUS_NO_SUCH_USER;
980 goto done;
983 *uid = sam_acct->unix_pw->pw_uid;
985 } else if (sid_check_is_in_unix_users(user_sid)) {
986 uint32_t getgroups_num_group_sids;
987 /* This is a unix user not in passdb. We need to ask nss
988 * directly, without consulting passdb */
990 struct passwd *pass;
993 * This goto target is used as a fallback for the passdb
994 * case. The concrete bug report is when passdb gave us an
995 * unmapped gid.
998 unix_user:
1000 if (!sid_to_uid(user_sid, uid)) {
1001 DEBUG(1, ("unix_user case, sid_to_uid for %s failed\n",
1002 dom_sid_str_buf(user_sid, &buf)));
1003 result = NT_STATUS_NO_SUCH_USER;
1004 goto done;
1007 uid_to_unix_users_sid(*uid, &tmp_sid);
1008 user_sid = &tmp_sid;
1010 pass = getpwuid_alloc(tmp_ctx, *uid);
1011 if (pass == NULL) {
1012 DEBUG(1, ("getpwuid(%u) failed\n",
1013 (unsigned int)*uid));
1014 goto done;
1017 if (!getgroups_unix_user(tmp_ctx, pass->pw_name, pass->pw_gid,
1018 &gids, &getgroups_num_group_sids)) {
1019 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
1020 pass->pw_name));
1021 goto done;
1023 num_group_sids = getgroups_num_group_sids;
1025 group_sids = talloc_array(tmp_ctx, struct dom_sid, num_group_sids);
1026 if (group_sids == NULL) {
1027 DEBUG(1, ("talloc_array failed\n"));
1028 result = NT_STATUS_NO_MEMORY;
1029 goto done;
1032 for (i=0; i<num_group_sids; i++) {
1033 gid_to_sid(&group_sids[i], gids[i]);
1036 /* In getgroups_unix_user we always set the primary gid */
1037 SMB_ASSERT(num_group_sids > 0);
1039 /* Ensure we're returning the found_username on the right context. */
1040 *found_username = talloc_strdup(mem_ctx, pass->pw_name);
1041 if (*found_username == NULL) {
1042 result = NT_STATUS_NO_MEMORY;
1043 goto done;
1045 } else {
1047 /* This user is from winbind, force the primary gid to the
1048 * user's "domain users" group. Under certain circumstances
1049 * (user comes from NT4), this might be a loss of
1050 * information. But we can not rely on winbind getting the
1051 * correct info. AD might prohibit winbind looking up that
1052 * information. */
1054 /* We must always assign the *uid. */
1055 if (!sid_to_uid(user_sid, uid)) {
1056 DEBUG(1, ("winbindd case, sid_to_uid for %s failed\n",
1057 dom_sid_str_buf(user_sid, &buf)));
1058 result = NT_STATUS_NO_SUCH_USER;
1059 goto done;
1062 num_group_sids = 1;
1063 group_sids = talloc_array(tmp_ctx, struct dom_sid, num_group_sids);
1064 if (group_sids == NULL) {
1065 DEBUG(1, ("talloc_array failed\n"));
1066 result = NT_STATUS_NO_MEMORY;
1067 goto done;
1070 gids = talloc_array(tmp_ctx, gid_t, num_group_sids);
1071 if (gids == NULL) {
1072 result = NT_STATUS_NO_MEMORY;
1073 goto done;
1076 sid_copy(&group_sids[0], user_sid);
1077 sid_split_rid(&group_sids[0], NULL);
1078 sid_append_rid(&group_sids[0], DOMAIN_RID_USERS);
1080 if (!sid_to_gid(&group_sids[0], &gids[0])) {
1081 DEBUG(1, ("sid_to_gid(%s) failed\n",
1082 dom_sid_str_buf(&group_sids[0], &buf)));
1083 goto done;
1086 *found_username = NULL;
1089 *gid = gids[0];
1091 /* Add the "Unix Group" SID for each gid to catch mapped groups
1092 and their Unix equivalent. This is to solve the backwards
1093 compatibility problem of 'valid users = +ntadmin' where
1094 ntadmin has been paired with "Domain Admins" in the group
1095 mapping table. Otherwise smb.conf would need to be changed
1096 to 'valid user = "Domain Admins"'. --jerry */
1098 num_gids = num_group_sids;
1099 range_ok = lp_idmap_default_range(&low, &high);
1100 for ( i=0; i<num_gids; i++ ) {
1101 struct dom_sid unix_group_sid;
1103 /* don't pickup anything managed by Winbind */
1104 if (range_ok && (gids[i] >= low) && (gids[i] <= high)) {
1105 continue;
1108 gid_to_unix_groups_sid(gids[i], &unix_group_sid);
1110 result = add_sid_to_array_unique(tmp_ctx, &unix_group_sid,
1111 &group_sids, &num_group_sids);
1112 if (!NT_STATUS_IS_OK(result)) {
1113 goto done;
1117 /* Ensure we're creating the nt_token on the right context. */
1118 *token = create_local_nt_token(mem_ctx, user_sid,
1119 is_guest, num_group_sids, group_sids);
1121 if (*token == NULL) {
1122 result = NT_STATUS_NO_MEMORY;
1123 goto done;
1126 result = NT_STATUS_OK;
1127 done:
1128 TALLOC_FREE(tmp_ctx);
1129 return result;
1133 * Create an artificial NT token given just a username. (Initially intended
1134 * for force user)
1136 * We go through lookup_name() to avoid problems we had with 'winbind use
1137 * default domain'.
1139 * We have 3 cases:
1141 * unmapped unix users: Go directly to nss to find the user's group.
1143 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
1145 * If the user is provided by winbind, the primary gid is set to "domain
1146 * users" of the user's domain. For an explanation why this is necessary, see
1147 * the thread starting at
1148 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
1151 NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username,
1152 bool is_guest,
1153 uid_t *uid, gid_t *gid,
1154 char **found_username,
1155 struct security_token **token)
1157 NTSTATUS result = NT_STATUS_NO_SUCH_USER;
1158 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1159 struct dom_sid user_sid;
1160 enum lsa_SidType type;
1162 if (!lookup_name_smbconf(tmp_ctx, username, LOOKUP_NAME_ALL,
1163 NULL, NULL, &user_sid, &type)) {
1164 DEBUG(1, ("lookup_name_smbconf for %s failed\n", username));
1165 goto done;
1168 if (type != SID_NAME_USER) {
1169 DEBUG(1, ("%s is a %s, not a user\n", username,
1170 sid_type_lookup(type)));
1171 goto done;
1174 result = create_token_from_sid(mem_ctx, &user_sid, is_guest, uid, gid, found_username, token);
1176 if (!NT_STATUS_IS_OK(result)) {
1177 goto done;
1181 * If result == NT_STATUS_OK then
1182 * we know we have a valid token. Ensure
1183 * we also have a valid username to match.
1186 if (*found_username == NULL) {
1187 *found_username = talloc_strdup(mem_ctx, username);
1188 if (*found_username == NULL) {
1189 result = NT_STATUS_NO_MEMORY;
1193 done:
1194 TALLOC_FREE(tmp_ctx);
1195 return result;
1198 /***************************************************************************
1199 Build upon create_token_from_sid:
1201 Expensive helper function to figure out whether a user given its sid is
1202 member of a particular group.
1203 ***************************************************************************/
1205 bool user_sid_in_group_sid(const struct dom_sid *sid, const struct dom_sid *group_sid)
1207 NTSTATUS status;
1208 uid_t uid;
1209 gid_t gid;
1210 char *found_username;
1211 struct security_token *token;
1212 bool result = false;
1213 enum lsa_SidType type;
1214 TALLOC_CTX *mem_ctx = talloc_stackframe();
1215 struct dom_sid_buf buf;
1217 if (!lookup_sid(mem_ctx, sid,
1218 NULL, NULL, &type)) {
1219 DEBUG(1, ("lookup_sid for %s failed\n",
1220 dom_sid_str_buf(sid, &buf)));
1221 goto done;
1224 if (type != SID_NAME_USER) {
1225 DEBUG(5, ("%s is a %s, not a user\n",
1226 dom_sid_str_buf(sid, &buf),
1227 sid_type_lookup(type)));
1228 goto done;
1231 status = create_token_from_sid(mem_ctx, sid, False,
1232 &uid, &gid, &found_username,
1233 &token);
1235 if (!NT_STATUS_IS_OK(status)) {
1236 DEBUG(10, ("could not create token for %s\n",
1237 dom_sid_str_buf(sid, &buf)));
1238 goto done;
1241 result = security_token_has_sid(token, group_sid);
1243 done:
1244 TALLOC_FREE(mem_ctx);
1245 return result;
1248 /***************************************************************************
1249 Build upon create_token_from_username:
1251 Expensive helper function to figure out whether a user given its name is
1252 member of a particular group.
1253 ***************************************************************************/
1255 bool user_in_group_sid(const char *username, const struct dom_sid *group_sid)
1257 NTSTATUS status;
1258 uid_t uid;
1259 gid_t gid;
1260 char *found_username;
1261 struct security_token *token;
1262 bool result;
1263 TALLOC_CTX *mem_ctx = talloc_stackframe();
1265 status = create_token_from_username(mem_ctx, username, False,
1266 &uid, &gid, &found_username,
1267 &token);
1269 if (!NT_STATUS_IS_OK(status)) {
1270 DEBUG(10, ("could not create token for %s\n", username));
1271 TALLOC_FREE(mem_ctx);
1272 return False;
1275 result = security_token_has_sid(token, group_sid);
1277 TALLOC_FREE(mem_ctx);
1278 return result;
1281 bool user_in_group(const char *username, const char *groupname)
1283 TALLOC_CTX *mem_ctx = talloc_stackframe();
1284 struct dom_sid group_sid;
1285 bool ret;
1287 ret = lookup_name(mem_ctx, groupname, LOOKUP_NAME_ALL,
1288 NULL, NULL, &group_sid, NULL);
1289 TALLOC_FREE(mem_ctx);
1291 if (!ret) {
1292 DEBUG(10, ("lookup_name for (%s) failed.\n", groupname));
1293 return False;
1296 return user_in_group_sid(username, &group_sid);
1299 /* END */