s3:auth: remove static from finalize_local_nt_token()
[Samba.git] / source3 / auth / token_util.c
blobf015f8d2cd5c14b660688c5f6db97839818b7cde
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);
285 NTSTATUS get_user_sid_info3_and_extra(const struct netr_SamInfo3 *info3,
286 const struct extra_auth_info *extra,
287 struct dom_sid *sid)
289 /* USER SID */
290 if (info3->base.rid == (uint32_t)(-1)) {
291 /* this is a signal the user was fake and generated,
292 * the actual SID we want to use is stored in the extra
293 * sids */
294 if (is_null_sid(&extra->user_sid)) {
295 /* we couldn't find the user sid, bail out */
296 DEBUG(3, ("Invalid user SID\n"));
297 return NT_STATUS_UNSUCCESSFUL;
299 sid_copy(sid, &extra->user_sid);
300 } else {
301 sid_copy(sid, info3->base.domain_sid);
302 sid_append_rid(sid, info3->base.rid);
304 return NT_STATUS_OK;
307 NTSTATUS create_local_nt_token_from_info3(TALLOC_CTX *mem_ctx,
308 bool is_guest,
309 const struct netr_SamInfo3 *info3,
310 const struct extra_auth_info *extra,
311 struct security_token **ntok)
313 struct security_token *usrtok = NULL;
314 uint32_t session_info_flags = 0;
315 NTSTATUS status;
316 int i;
318 DEBUG(10, ("Create local NT token for %s\n",
319 info3->base.account_name.string));
321 usrtok = talloc_zero(mem_ctx, struct security_token);
322 if (!usrtok) {
323 DEBUG(0, ("talloc failed\n"));
324 return NT_STATUS_NO_MEMORY;
327 /* Add the user and primary group sid FIRST */
328 /* check if the user rid is the special "Domain Guests" rid.
329 * If so pick the first sid for the extra sids instead as it
330 * is a local fake account */
331 usrtok->sids = talloc_array(usrtok, struct dom_sid, 2);
332 if (!usrtok->sids) {
333 TALLOC_FREE(usrtok);
334 return NT_STATUS_NO_MEMORY;
336 usrtok->num_sids = 2;
338 status = get_user_sid_info3_and_extra(info3, extra, &usrtok->sids[0]);
339 if (!NT_STATUS_IS_OK(status)) {
340 TALLOC_FREE(usrtok);
341 return status;
344 /* GROUP SID */
345 if (info3->base.primary_gid == (uint32_t)(-1)) {
346 /* this is a signal the user was fake and generated,
347 * the actual SID we want to use is stored in the extra
348 * sids */
349 if (is_null_sid(&extra->pgid_sid)) {
350 /* we couldn't find the user sid, bail out */
351 DEBUG(3, ("Invalid group SID\n"));
352 TALLOC_FREE(usrtok);
353 return NT_STATUS_UNSUCCESSFUL;
355 sid_copy(&usrtok->sids[1], &extra->pgid_sid);
356 } else {
357 sid_copy(&usrtok->sids[1], info3->base.domain_sid);
358 sid_append_rid(&usrtok->sids[1],
359 info3->base.primary_gid);
362 /* Now the SIDs we got from authentication. These are the ones from
363 * the info3 struct or from the pdb_enum_group_memberships, depending
364 * on who authenticated the user.
365 * Note that we start the for loop at "1" here, we already added the
366 * first group sid as primary above. */
368 for (i = 0; i < info3->base.groups.count; i++) {
369 struct dom_sid tmp_sid;
371 sid_copy(&tmp_sid, info3->base.domain_sid);
372 sid_append_rid(&tmp_sid, info3->base.groups.rids[i].rid);
374 status = add_sid_to_array_unique(usrtok, &tmp_sid,
375 &usrtok->sids,
376 &usrtok->num_sids);
377 if (!NT_STATUS_IS_OK(status)) {
378 DEBUG(3, ("Failed to add SID to nt token\n"));
379 TALLOC_FREE(usrtok);
380 return status;
384 /* now also add extra sids if they are not the special user/group
385 * sids */
386 for (i = 0; i < info3->sidcount; i++) {
387 status = add_sid_to_array_unique(usrtok,
388 info3->sids[i].sid,
389 &usrtok->sids,
390 &usrtok->num_sids);
391 if (!NT_STATUS_IS_OK(status)) {
392 DEBUG(3, ("Failed to add SID to nt token\n"));
393 TALLOC_FREE(usrtok);
394 return status;
398 status = add_local_groups(usrtok, is_guest);
399 if (!NT_STATUS_IS_OK(status)) {
400 DEBUG(3, ("Failed to add local groups\n"));
401 TALLOC_FREE(usrtok);
402 return status;
405 session_info_flags |= AUTH_SESSION_INFO_DEFAULT_GROUPS;
406 if (!is_guest) {
407 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
410 status = finalize_local_nt_token(usrtok, session_info_flags);
411 if (!NT_STATUS_IS_OK(status)) {
412 DEBUG(3, ("Failed to finalize nt token\n"));
413 TALLOC_FREE(usrtok);
414 return status;
417 *ntok = usrtok;
418 return NT_STATUS_OK;
421 /*******************************************************************
422 Create a NT token for the user, expanding local aliases
423 *******************************************************************/
425 struct security_token *create_local_nt_token(TALLOC_CTX *mem_ctx,
426 const struct dom_sid *user_sid,
427 bool is_guest,
428 int num_groupsids,
429 const struct dom_sid *groupsids)
431 struct security_token *result = NULL;
432 int i;
433 NTSTATUS status;
434 uint32_t session_info_flags = 0;
436 DEBUG(10, ("Create local NT token for %s\n",
437 sid_string_dbg(user_sid)));
439 if (!(result = talloc_zero(mem_ctx, struct security_token))) {
440 DEBUG(0, ("talloc failed\n"));
441 return NULL;
444 /* Add the user and primary group sid */
446 status = add_sid_to_array(result, user_sid,
447 &result->sids, &result->num_sids);
448 if (!NT_STATUS_IS_OK(status)) {
449 TALLOC_FREE(result);
450 return NULL;
453 /* For guest, num_groupsids may be zero. */
454 if (num_groupsids) {
455 status = add_sid_to_array(result, &groupsids[0],
456 &result->sids,
457 &result->num_sids);
458 if (!NT_STATUS_IS_OK(status)) {
459 TALLOC_FREE(result);
460 return NULL;
464 /* Now the SIDs we got from authentication. These are the ones from
465 * the info3 struct or from the pdb_enum_group_memberships, depending
466 * on who authenticated the user.
467 * Note that we start the for loop at "1" here, we already added the
468 * first group sid as primary above. */
470 for (i=1; i<num_groupsids; i++) {
471 status = add_sid_to_array_unique(result, &groupsids[i],
472 &result->sids,
473 &result->num_sids);
474 if (!NT_STATUS_IS_OK(status)) {
475 TALLOC_FREE(result);
476 return NULL;
480 status = add_local_groups(result, is_guest);
481 if (!NT_STATUS_IS_OK(status)) {
482 TALLOC_FREE(result);
483 return NULL;
486 session_info_flags |= AUTH_SESSION_INFO_DEFAULT_GROUPS;
487 if (!is_guest) {
488 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
491 status = finalize_local_nt_token(result, session_info_flags);
492 if (!NT_STATUS_IS_OK(status)) {
493 TALLOC_FREE(result);
494 return NULL;
497 if (is_guest) {
499 * It's ugly, but for now it's
500 * needed to add Builtin_Guests
501 * here, the "local" token only
502 * consist of S-1-22-* SIDs
503 * and finalize_local_nt_token()
504 * doesn't have the chance to
505 * to detect it need to
506 * add Builtin_Guests via
507 * add_builtin_guests().
509 status = add_sid_to_array_unique(result,
510 &global_sid_Builtin_Guests,
511 &result->sids,
512 &result->num_sids);
513 if (!NT_STATUS_IS_OK(status)) {
514 DEBUG(3, ("Failed to add SID to nt token\n"));
515 TALLOC_FREE(result);
516 return NULL;
520 return result;
523 /***************************************************
524 Merge in any groups from /etc/group.
525 ***************************************************/
527 static NTSTATUS add_local_groups(struct security_token *result,
528 bool is_guest)
530 gid_t *gids = NULL;
531 uint32_t getgroups_num_group_sids = 0;
532 struct passwd *pass = NULL;
533 TALLOC_CTX *tmp_ctx = talloc_stackframe();
534 int i;
536 if (is_guest) {
538 * Guest is a special case. It's always
539 * a user that can be looked up, but
540 * result->sids[0] is set to DOMAIN\Guest.
541 * Lookup by account name instead.
543 pass = Get_Pwnam_alloc(tmp_ctx, lp_guest_account());
544 } else {
545 uid_t uid;
547 /* For non-guest result->sids[0] is always the user sid. */
548 if (!sid_to_uid(&result->sids[0], &uid)) {
550 * Non-mappable SID like SYSTEM.
551 * Can't be in any /etc/group groups.
553 TALLOC_FREE(tmp_ctx);
554 return NT_STATUS_OK;
557 pass = getpwuid_alloc(tmp_ctx, uid);
558 if (pass == NULL) {
559 DEBUG(1, ("SID %s -> getpwuid(%u) failed\n",
560 sid_string_dbg(&result->sids[0]),
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 * Add BUILTIN\Guests directly to token.
738 * But only if the token already indicates
739 * real guest access by:
740 * - local GUEST account
741 * - local GUESTS group
742 * - domain GUESTS group
744 * Even if a user was authenticated, it
745 * can be member of a guest related group.
747 status = add_builtin_guests(result, domain_sid);
748 if (!NT_STATUS_IS_OK(status)) {
749 DEBUG(3, ("Failed to check for local "
750 "Guests membership (%s)\n",
751 nt_errstr(status)));
753 * This is a hard error.
755 return status;
758 TALLOC_FREE(info);
760 /* Deal with local groups */
762 if (lp_winbind_nested_groups()) {
764 become_root();
766 /* Now add the aliases. First the one from our local SAM */
768 status = add_aliases(get_global_sam_sid(), result);
770 if (!NT_STATUS_IS_OK(status)) {
771 unbecome_root();
772 return status;
775 /* Finally the builtin ones */
777 status = add_aliases(&global_sid_Builtin, result);
779 if (!NT_STATUS_IS_OK(status)) {
780 unbecome_root();
781 return status;
784 unbecome_root();
788 if (session_info_flags & AUTH_SESSION_INFO_SIMPLE_PRIVILEGES) {
789 if (security_token_has_builtin_administrators(result)) {
790 result->privilege_mask = ~0;
792 } else {
793 /* Add privileges based on current user sids */
794 get_privileges_for_sids(&result->privilege_mask, result->sids,
795 result->num_sids);
798 return NT_STATUS_OK;
801 /****************************************************************************
802 prints a UNIX 'token' to debug output.
803 ****************************************************************************/
805 void debug_unix_user_token(int dbg_class, int dbg_lev, uid_t uid, gid_t gid,
806 int n_groups, gid_t *groups)
808 int i;
809 DEBUGC(dbg_class, dbg_lev,
810 ("UNIX token of user %ld\n", (long int)uid));
812 DEBUGADDC(dbg_class, dbg_lev,
813 ("Primary group is %ld and contains %i supplementary "
814 "groups\n", (long int)gid, n_groups));
815 for (i = 0; i < n_groups; i++)
816 DEBUGADDC(dbg_class, dbg_lev, ("Group[%3i]: %ld\n", i,
817 (long int)groups[i]));
821 * Create an artificial NT token given just a domain SID.
823 * We have 3 cases:
825 * unmapped unix users: Go directly to nss to find the user's group.
827 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
829 * If the user is provided by winbind, the primary gid is set to "domain
830 * users" of the user's domain. For an explanation why this is necessary, see
831 * the thread starting at
832 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
835 static NTSTATUS create_token_from_sid(TALLOC_CTX *mem_ctx,
836 const struct dom_sid *user_sid,
837 bool is_guest,
838 uid_t *uid, gid_t *gid,
839 char **found_username,
840 struct security_token **token)
842 NTSTATUS result = NT_STATUS_NO_SUCH_USER;
843 TALLOC_CTX *tmp_ctx = talloc_stackframe();
844 gid_t *gids;
845 struct dom_sid *group_sids;
846 struct dom_sid tmp_sid;
847 uint32_t num_group_sids;
848 uint32_t num_gids;
849 uint32_t i;
850 uint32_t high, low;
851 bool range_ok;
853 if (sid_check_is_in_our_sam(user_sid)) {
854 bool ret;
855 uint32_t pdb_num_group_sids;
856 /* This is a passdb user, so ask passdb */
858 struct samu *sam_acct = NULL;
860 if ( !(sam_acct = samu_new( tmp_ctx )) ) {
861 result = NT_STATUS_NO_MEMORY;
862 goto done;
865 become_root();
866 ret = pdb_getsampwsid(sam_acct, user_sid);
867 unbecome_root();
869 if (!ret) {
870 DEBUG(1, ("pdb_getsampwsid(%s) failed\n",
871 sid_string_dbg(user_sid)));
872 DEBUGADD(1, ("Fall back to unix user\n"));
873 goto unix_user;
876 result = pdb_enum_group_memberships(tmp_ctx, sam_acct,
877 &group_sids, &gids,
878 &pdb_num_group_sids);
879 if (!NT_STATUS_IS_OK(result)) {
880 DEBUG(1, ("enum_group_memberships failed for %s: "
881 "%s\n", sid_string_dbg(user_sid),
882 nt_errstr(result)));
883 DEBUGADD(1, ("Fall back to unix uid lookup\n"));
884 goto unix_user;
886 num_group_sids = pdb_num_group_sids;
888 /* see the smb_panic() in pdb_default_enum_group_memberships */
889 SMB_ASSERT(num_group_sids > 0);
891 /* Ensure we're returning the found_username on the right context. */
892 *found_username = talloc_strdup(mem_ctx,
893 pdb_get_username(sam_acct));
895 if (*found_username == NULL) {
896 result = NT_STATUS_NO_MEMORY;
897 goto done;
901 * If the SID from lookup_name() was the guest sid, passdb knows
902 * about the mapping of guest sid to lp_guest_account()
903 * username and will return the unix_pw info for a guest
904 * user. Use it if it's there, else lookup the *uid details
905 * using Get_Pwnam_alloc(). See bug #6291 for details. JRA.
908 /* We must always assign the *uid. */
909 if (sam_acct->unix_pw == NULL) {
910 struct passwd *pwd = Get_Pwnam_alloc(sam_acct, *found_username );
911 if (!pwd) {
912 DEBUG(10, ("Get_Pwnam_alloc failed for %s\n",
913 *found_username));
914 result = NT_STATUS_NO_SUCH_USER;
915 goto done;
917 result = samu_set_unix(sam_acct, pwd );
918 if (!NT_STATUS_IS_OK(result)) {
919 DEBUG(10, ("samu_set_unix failed for %s\n",
920 *found_username));
921 result = NT_STATUS_NO_SUCH_USER;
922 goto done;
925 *uid = sam_acct->unix_pw->pw_uid;
927 } else if (sid_check_is_in_unix_users(user_sid)) {
928 uint32_t getgroups_num_group_sids;
929 /* This is a unix user not in passdb. We need to ask nss
930 * directly, without consulting passdb */
932 struct passwd *pass;
935 * This goto target is used as a fallback for the passdb
936 * case. The concrete bug report is when passdb gave us an
937 * unmapped gid.
940 unix_user:
942 if (!sid_to_uid(user_sid, uid)) {
943 DEBUG(1, ("unix_user case, sid_to_uid for %s failed\n",
944 sid_string_dbg(user_sid)));
945 result = NT_STATUS_NO_SUCH_USER;
946 goto done;
949 uid_to_unix_users_sid(*uid, &tmp_sid);
950 user_sid = &tmp_sid;
952 pass = getpwuid_alloc(tmp_ctx, *uid);
953 if (pass == NULL) {
954 DEBUG(1, ("getpwuid(%u) failed\n",
955 (unsigned int)*uid));
956 goto done;
959 if (!getgroups_unix_user(tmp_ctx, pass->pw_name, pass->pw_gid,
960 &gids, &getgroups_num_group_sids)) {
961 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
962 pass->pw_name));
963 goto done;
965 num_group_sids = getgroups_num_group_sids;
967 group_sids = talloc_array(tmp_ctx, struct dom_sid, num_group_sids);
968 if (group_sids == NULL) {
969 DEBUG(1, ("talloc_array failed\n"));
970 result = NT_STATUS_NO_MEMORY;
971 goto done;
974 for (i=0; i<num_group_sids; i++) {
975 gid_to_sid(&group_sids[i], gids[i]);
978 /* In getgroups_unix_user we always set the primary gid */
979 SMB_ASSERT(num_group_sids > 0);
981 /* Ensure we're returning the found_username on the right context. */
982 *found_username = talloc_strdup(mem_ctx, pass->pw_name);
983 if (*found_username == NULL) {
984 result = NT_STATUS_NO_MEMORY;
985 goto done;
987 } else {
989 /* This user is from winbind, force the primary gid to the
990 * user's "domain users" group. Under certain circumstances
991 * (user comes from NT4), this might be a loss of
992 * information. But we can not rely on winbind getting the
993 * correct info. AD might prohibit winbind looking up that
994 * information. */
996 /* We must always assign the *uid. */
997 if (!sid_to_uid(user_sid, uid)) {
998 DEBUG(1, ("winbindd case, sid_to_uid for %s failed\n",
999 sid_string_dbg(user_sid)));
1000 result = NT_STATUS_NO_SUCH_USER;
1001 goto done;
1004 num_group_sids = 1;
1005 group_sids = talloc_array(tmp_ctx, struct dom_sid, num_group_sids);
1006 if (group_sids == NULL) {
1007 DEBUG(1, ("talloc_array failed\n"));
1008 result = NT_STATUS_NO_MEMORY;
1009 goto done;
1012 gids = talloc_array(tmp_ctx, gid_t, num_group_sids);
1013 if (gids == NULL) {
1014 result = NT_STATUS_NO_MEMORY;
1015 goto done;
1018 sid_copy(&group_sids[0], user_sid);
1019 sid_split_rid(&group_sids[0], NULL);
1020 sid_append_rid(&group_sids[0], DOMAIN_RID_USERS);
1022 if (!sid_to_gid(&group_sids[0], &gids[0])) {
1023 DEBUG(1, ("sid_to_gid(%s) failed\n",
1024 sid_string_dbg(&group_sids[0])));
1025 goto done;
1028 *found_username = NULL;
1031 *gid = gids[0];
1033 /* Add the "Unix Group" SID for each gid to catch mapped groups
1034 and their Unix equivalent. This is to solve the backwards
1035 compatibility problem of 'valid users = +ntadmin' where
1036 ntadmin has been paired with "Domain Admins" in the group
1037 mapping table. Otherwise smb.conf would need to be changed
1038 to 'valid user = "Domain Admins"'. --jerry */
1040 num_gids = num_group_sids;
1041 range_ok = lp_idmap_default_range(&low, &high);
1042 for ( i=0; i<num_gids; i++ ) {
1043 struct dom_sid unix_group_sid;
1045 /* don't pickup anything managed by Winbind */
1046 if (range_ok && (gids[i] >= low) && (gids[i] <= high)) {
1047 continue;
1050 gid_to_unix_groups_sid(gids[i], &unix_group_sid);
1052 result = add_sid_to_array_unique(tmp_ctx, &unix_group_sid,
1053 &group_sids, &num_group_sids);
1054 if (!NT_STATUS_IS_OK(result)) {
1055 goto done;
1059 /* Ensure we're creating the nt_token on the right context. */
1060 *token = create_local_nt_token(mem_ctx, user_sid,
1061 is_guest, num_group_sids, group_sids);
1063 if (*token == NULL) {
1064 result = NT_STATUS_NO_MEMORY;
1065 goto done;
1068 result = NT_STATUS_OK;
1069 done:
1070 TALLOC_FREE(tmp_ctx);
1071 return result;
1075 * Create an artificial NT token given just a username. (Initially intended
1076 * for force user)
1078 * We go through lookup_name() to avoid problems we had with 'winbind use
1079 * default domain'.
1081 * We have 3 cases:
1083 * unmapped unix users: Go directly to nss to find the user's group.
1085 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
1087 * If the user is provided by winbind, the primary gid is set to "domain
1088 * users" of the user's domain. For an explanation why this is necessary, see
1089 * the thread starting at
1090 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
1093 NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username,
1094 bool is_guest,
1095 uid_t *uid, gid_t *gid,
1096 char **found_username,
1097 struct security_token **token)
1099 NTSTATUS result = NT_STATUS_NO_SUCH_USER;
1100 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1101 struct dom_sid user_sid;
1102 enum lsa_SidType type;
1104 if (!lookup_name_smbconf(tmp_ctx, username, LOOKUP_NAME_ALL,
1105 NULL, NULL, &user_sid, &type)) {
1106 DEBUG(1, ("lookup_name_smbconf for %s failed\n", username));
1107 goto done;
1110 if (type != SID_NAME_USER) {
1111 DEBUG(1, ("%s is a %s, not a user\n", username,
1112 sid_type_lookup(type)));
1113 goto done;
1116 result = create_token_from_sid(mem_ctx, &user_sid, is_guest, uid, gid, found_username, token);
1118 if (!NT_STATUS_IS_OK(result)) {
1119 goto done;
1123 * If result == NT_STATUS_OK then
1124 * we know we have a valid token. Ensure
1125 * we also have a valid username to match.
1128 if (*found_username == NULL) {
1129 *found_username = talloc_strdup(mem_ctx, username);
1130 if (*found_username == NULL) {
1131 result = NT_STATUS_NO_MEMORY;
1135 done:
1136 TALLOC_FREE(tmp_ctx);
1137 return result;
1140 /***************************************************************************
1141 Build upon create_token_from_sid:
1143 Expensive helper function to figure out whether a user given its sid is
1144 member of a particular group.
1145 ***************************************************************************/
1147 bool user_sid_in_group_sid(const struct dom_sid *sid, const struct dom_sid *group_sid)
1149 NTSTATUS status;
1150 uid_t uid;
1151 gid_t gid;
1152 char *found_username;
1153 struct security_token *token;
1154 bool result = false;
1155 enum lsa_SidType type;
1156 TALLOC_CTX *mem_ctx = talloc_stackframe();
1158 if (!lookup_sid(mem_ctx, sid,
1159 NULL, NULL, &type)) {
1160 DEBUG(1, ("lookup_sid for %s failed\n", dom_sid_string(mem_ctx, sid)));
1161 goto done;
1164 if (type != SID_NAME_USER) {
1165 DEBUG(5, ("%s is a %s, not a user\n", dom_sid_string(mem_ctx, sid),
1166 sid_type_lookup(type)));
1167 goto done;
1170 status = create_token_from_sid(mem_ctx, sid, False,
1171 &uid, &gid, &found_username,
1172 &token);
1174 if (!NT_STATUS_IS_OK(status)) {
1175 DEBUG(10, ("could not create token for %s\n", dom_sid_string(mem_ctx, sid)));
1176 goto done;
1179 result = security_token_has_sid(token, group_sid);
1181 done:
1182 TALLOC_FREE(mem_ctx);
1183 return result;
1186 /***************************************************************************
1187 Build upon create_token_from_username:
1189 Expensive helper function to figure out whether a user given its name is
1190 member of a particular group.
1191 ***************************************************************************/
1193 bool user_in_group_sid(const char *username, const struct dom_sid *group_sid)
1195 NTSTATUS status;
1196 uid_t uid;
1197 gid_t gid;
1198 char *found_username;
1199 struct security_token *token;
1200 bool result;
1201 TALLOC_CTX *mem_ctx = talloc_stackframe();
1203 status = create_token_from_username(mem_ctx, username, False,
1204 &uid, &gid, &found_username,
1205 &token);
1207 if (!NT_STATUS_IS_OK(status)) {
1208 DEBUG(10, ("could not create token for %s\n", username));
1209 TALLOC_FREE(mem_ctx);
1210 return False;
1213 result = security_token_has_sid(token, group_sid);
1215 TALLOC_FREE(mem_ctx);
1216 return result;
1219 bool user_in_group(const char *username, const char *groupname)
1221 TALLOC_CTX *mem_ctx = talloc_stackframe();
1222 struct dom_sid group_sid;
1223 bool ret;
1225 ret = lookup_name(mem_ctx, groupname, LOOKUP_NAME_ALL,
1226 NULL, NULL, &group_sid, NULL);
1227 TALLOC_FREE(mem_ctx);
1229 if (!ret) {
1230 DEBUG(10, ("lookup_name for (%s) failed.\n", groupname));
1231 return False;
1234 return user_in_group_sid(username, &group_sid);
1237 /* END */