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 */
28 #include "system/passwd.h"
32 #include "../librpc/gen_ndr/netlogon.h"
33 #include "../libcli/security/security.h"
34 #include "../lib/util/util_pw.h"
36 #include "lib/privileges.h"
38 /****************************************************************************
39 Check for a SID in an struct security_token
40 ****************************************************************************/
42 bool nt_token_check_sid ( const struct dom_sid
*sid
, const struct security_token
*token
)
47 return security_token_has_sid(token
, sid
);
50 bool nt_token_check_domain_rid( struct security_token
*token
, uint32 rid
)
52 struct dom_sid domain_sid
;
54 /* if we are a domain member, the get the domain SID, else for
55 a DC or standalone server, use our own SID */
57 if ( lp_server_role() == ROLE_DOMAIN_MEMBER
) {
58 if ( !secrets_fetch_domain_sid( lp_workgroup(),
60 DEBUG(1,("nt_token_check_domain_rid: Cannot lookup "
61 "SID for domain [%s]\n", lp_workgroup()));
66 sid_copy( &domain_sid
, get_global_sam_sid() );
68 sid_append_rid( &domain_sid
, rid
);
70 return nt_token_check_sid( &domain_sid
, token
);\
73 /******************************************************************************
74 Create a token for the root user to be used internally by smbd.
75 This is similar to running under the context of the LOCAL_SYSTEM account
76 in Windows. This is a read-only token. Do not modify it or free() it.
77 Create a copy if you need to change it.
78 ******************************************************************************/
80 struct security_token
*get_root_nt_token( void )
82 struct security_token
*token
, *for_cache
;
83 struct dom_sid u_sid
, g_sid
;
87 cache_data
= memcache_lookup_talloc(
88 NULL
, SINGLETON_CACHE_TALLOC
,
89 data_blob_string_const_null("root_nt_token"));
91 if (cache_data
!= NULL
) {
92 return talloc_get_type_abort(
93 cache_data
, struct security_token
);
96 if ( !(pw
= getpwuid(0)) ) {
97 if ( !(pw
= getpwnam("root")) ) {
98 DEBUG(0,("get_root_nt_token: both getpwuid(0) "
99 "and getpwnam(\"root\") failed!\n"));
104 /* get the user and primary group SIDs; although the
105 BUILTIN\Administrators SId is really the one that matters here */
107 uid_to_sid(&u_sid
, pw
->pw_uid
);
108 gid_to_sid(&g_sid
, pw
->pw_gid
);
110 token
= create_local_nt_token(talloc_tos(), &u_sid
, False
,
111 1, &global_sid_Builtin_Administrators
);
113 security_token_set_privilege(token
, SEC_PRIV_DISK_OPERATOR
);
118 NULL
, SINGLETON_CACHE_TALLOC
,
119 data_blob_string_const_null("root_nt_token"), &for_cache
);
126 * Add alias SIDs from memberships within the partially created token SID list
129 NTSTATUS
add_aliases(const struct dom_sid
*domain_sid
,
130 struct security_token
*token
)
133 size_t i
, num_aliases
;
137 if (!(tmp_ctx
= talloc_init("add_aliases"))) {
138 return NT_STATUS_NO_MEMORY
;
144 status
= pdb_enum_alias_memberships(tmp_ctx
, domain_sid
,
147 &aliases
, &num_aliases
);
149 if (!NT_STATUS_IS_OK(status
)) {
150 DEBUG(10, ("pdb_enum_alias_memberships failed: %s\n",
155 for (i
=0; i
<num_aliases
; i
++) {
156 struct dom_sid alias_sid
;
157 sid_compose(&alias_sid
, domain_sid
, aliases
[i
]);
158 status
= add_sid_to_array_unique(token
, &alias_sid
,
161 if (!NT_STATUS_IS_OK(status
)) {
162 DEBUG(0, ("add_sid_to_array failed\n"));
168 TALLOC_FREE(tmp_ctx
);
172 /*******************************************************************
173 *******************************************************************/
175 static NTSTATUS
add_builtin_administrators(struct security_token
*token
,
176 const struct dom_sid
*dom_sid
)
178 struct dom_sid domadm
;
181 /* nothing to do if we aren't in a domain */
183 if ( !(IS_DC
|| lp_server_role()==ROLE_DOMAIN_MEMBER
) ) {
187 /* Find the Domain Admins SID */
190 sid_copy( &domadm
, get_global_sam_sid() );
192 sid_copy(&domadm
, dom_sid
);
194 sid_append_rid( &domadm
, DOMAIN_RID_ADMINS
);
196 /* Add Administrators if the user beloongs to Domain Admins */
198 if ( nt_token_check_sid( &domadm
, token
) ) {
199 status
= add_sid_to_array(token
,
200 &global_sid_Builtin_Administrators
,
201 &token
->sids
, &token
->num_sids
);
202 if (!NT_STATUS_IS_OK(status
)) {
210 static NTSTATUS
finalize_local_nt_token(struct security_token
*result
,
213 NTSTATUS
create_local_nt_token_from_info3(TALLOC_CTX
*mem_ctx
,
215 const struct netr_SamInfo3
*info3
,
216 const struct extra_auth_info
*extra
,
217 struct security_token
**ntok
)
219 struct security_token
*usrtok
= NULL
;
223 DEBUG(10, ("Create local NT token for %s\n",
224 info3
->base
.account_name
.string
));
226 usrtok
= talloc_zero(mem_ctx
, struct security_token
);
228 DEBUG(0, ("talloc failed\n"));
229 return NT_STATUS_NO_MEMORY
;
232 /* Add the user and primary group sid FIRST */
233 /* check if the user rid is the special "Domain Guests" rid.
234 * If so pick the first sid for the extra sids instead as it
235 * is a local fake account */
236 usrtok
->sids
= talloc_array(usrtok
, struct dom_sid
, 2);
239 return NT_STATUS_NO_MEMORY
;
241 usrtok
->num_sids
= 2;
244 if (info3
->base
.rid
== (uint32_t)(-1)) {
245 /* this is a signal the user was fake and generated,
246 * the actual SID we want to use is stored in the extra
248 if (is_null_sid(&extra
->user_sid
)) {
249 /* we couldn't find the user sid, bail out */
250 DEBUG(3, ("Invalid user SID\n"));
252 return NT_STATUS_UNSUCCESSFUL
;
254 sid_copy(&usrtok
->sids
[0], &extra
->user_sid
);
256 sid_copy(&usrtok
->sids
[0], info3
->base
.domain_sid
);
257 sid_append_rid(&usrtok
->sids
[0], info3
->base
.rid
);
261 if (info3
->base
.primary_gid
== (uint32_t)(-1)) {
262 /* this is a signal the user was fake and generated,
263 * the actual SID we want to use is stored in the extra
265 if (is_null_sid(&extra
->pgid_sid
)) {
266 /* we couldn't find the user sid, bail out */
267 DEBUG(3, ("Invalid group SID\n"));
269 return NT_STATUS_UNSUCCESSFUL
;
271 sid_copy(&usrtok
->sids
[1], &extra
->pgid_sid
);
273 sid_copy(&usrtok
->sids
[1], info3
->base
.domain_sid
);
274 sid_append_rid(&usrtok
->sids
[1],
275 info3
->base
.primary_gid
);
278 /* Now the SIDs we got from authentication. These are the ones from
279 * the info3 struct or from the pdb_enum_group_memberships, depending
280 * on who authenticated the user.
281 * Note that we start the for loop at "1" here, we already added the
282 * first group sid as primary above. */
284 for (i
= 0; i
< info3
->base
.groups
.count
; i
++) {
285 struct dom_sid tmp_sid
;
287 sid_copy(&tmp_sid
, info3
->base
.domain_sid
);
288 sid_append_rid(&tmp_sid
, info3
->base
.groups
.rids
[i
].rid
);
290 status
= add_sid_to_array_unique(usrtok
, &tmp_sid
,
293 if (!NT_STATUS_IS_OK(status
)) {
294 DEBUG(3, ("Failed to add SID to nt token\n"));
300 /* now also add extra sids if they are not the special user/group
302 for (i
= 0; i
< info3
->sidcount
; i
++) {
303 status
= add_sid_to_array_unique(usrtok
,
307 if (!NT_STATUS_IS_OK(status
)) {
308 DEBUG(3, ("Failed to add SID to nt token\n"));
314 status
= finalize_local_nt_token(usrtok
, is_guest
);
315 if (!NT_STATUS_IS_OK(status
)) {
316 DEBUG(3, ("Failed to finalize nt token\n"));
325 /*******************************************************************
326 Create a NT token for the user, expanding local aliases
327 *******************************************************************/
329 struct security_token
*create_local_nt_token(TALLOC_CTX
*mem_ctx
,
330 const struct dom_sid
*user_sid
,
333 const struct dom_sid
*groupsids
)
335 struct security_token
*result
= NULL
;
339 DEBUG(10, ("Create local NT token for %s\n",
340 sid_string_dbg(user_sid
)));
342 if (!(result
= talloc_zero(mem_ctx
, struct security_token
))) {
343 DEBUG(0, ("talloc failed\n"));
347 /* Add the user and primary group sid */
349 status
= add_sid_to_array(result
, user_sid
,
350 &result
->sids
, &result
->num_sids
);
351 if (!NT_STATUS_IS_OK(status
)) {
356 /* For guest, num_groupsids may be zero. */
358 status
= add_sid_to_array(result
, &groupsids
[0],
361 if (!NT_STATUS_IS_OK(status
)) {
367 /* Now the SIDs we got from authentication. These are the ones from
368 * the info3 struct or from the pdb_enum_group_memberships, depending
369 * on who authenticated the user.
370 * Note that we start the for loop at "1" here, we already added the
371 * first group sid as primary above. */
373 for (i
=1; i
<num_groupsids
; i
++) {
374 status
= add_sid_to_array_unique(result
, &groupsids
[i
],
377 if (!NT_STATUS_IS_OK(status
)) {
383 status
= finalize_local_nt_token(result
, is_guest
);
384 if (!NT_STATUS_IS_OK(status
)) {
392 /***************************************************
393 Merge in any groups from /etc/group.
394 ***************************************************/
396 static NTSTATUS
add_local_groups(struct security_token
*result
,
400 uint32_t getgroups_num_group_sids
= 0;
401 struct passwd
*pass
= NULL
;
402 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
407 * Guest is a special case. It's always
408 * a user that can be looked up, but
409 * result->sids[0] is set to DOMAIN\Guest.
410 * Lookup by account name instead.
412 pass
= Get_Pwnam_alloc(tmp_ctx
, lp_guest_account());
416 /* For non-guest result->sids[0] is always the user sid. */
417 if (!sid_to_uid(&result
->sids
[0], &uid
)) {
419 * Non-mappable SID like SYSTEM.
420 * Can't be in any /etc/group groups.
422 TALLOC_FREE(tmp_ctx
);
426 pass
= getpwuid_alloc(tmp_ctx
, uid
);
428 DEBUG(1, ("SID %s -> getpwuid(%u) failed\n",
429 sid_string_dbg(&result
->sids
[0]),
435 TALLOC_FREE(tmp_ctx
);
436 return NT_STATUS_UNSUCCESSFUL
;
440 * Now we must get any groups this user has been
441 * added to in /etc/group and merge them in.
442 * This has to be done in every code path
443 * that creates an NT token, as remote users
444 * may have been added to the local /etc/group
445 * database. Tokens created merely from the
446 * info3 structs (via the DC or via the krb5 PAC)
447 * won't have these local groups. Note the
448 * groups added here will only be UNIX groups
449 * (S-1-22-2-XXXX groups) as getgroups_unix_user()
450 * turns off winbindd before calling getgroups().
452 * NB. This is duplicating work already
453 * done in the 'unix_user:' case of
454 * create_token_from_sid() but won't
455 * do anything other than be inefficient
459 if (!getgroups_unix_user(tmp_ctx
, pass
->pw_name
, pass
->pw_gid
,
460 &gids
, &getgroups_num_group_sids
)) {
461 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
463 TALLOC_FREE(tmp_ctx
);
464 return NT_STATUS_UNSUCCESSFUL
;
467 for (i
=0; i
<getgroups_num_group_sids
; i
++) {
469 struct dom_sid grp_sid
;
470 gid_to_sid(&grp_sid
, gids
[i
]);
472 status
= add_sid_to_array_unique(result
,
476 if (!NT_STATUS_IS_OK(status
)) {
477 DEBUG(3, ("Failed to add UNIX SID to nt token\n"));
478 TALLOC_FREE(tmp_ctx
);
482 TALLOC_FREE(tmp_ctx
);
486 static NTSTATUS
finalize_local_nt_token(struct security_token
*result
,
489 struct dom_sid dom_sid
;
493 /* Add any local groups. */
495 status
= add_local_groups(result
, is_guest
);
496 if (!NT_STATUS_IS_OK(status
)) {
500 /* Add in BUILTIN sids */
502 status
= add_sid_to_array(result
, &global_sid_World
,
503 &result
->sids
, &result
->num_sids
);
504 if (!NT_STATUS_IS_OK(status
)) {
507 status
= add_sid_to_array(result
, &global_sid_Network
,
508 &result
->sids
, &result
->num_sids
);
509 if (!NT_STATUS_IS_OK(status
)) {
514 status
= add_sid_to_array(result
, &global_sid_Builtin_Guests
,
517 if (!NT_STATUS_IS_OK(status
)) {
521 status
= add_sid_to_array(result
,
522 &global_sid_Authenticated_Users
,
525 if (!NT_STATUS_IS_OK(status
)) {
530 /* Deal with the BUILTIN\Administrators group. If the SID can
531 be resolved then assume that the add_aliasmem( S-1-5-32 )
534 if (!sid_to_gid(&global_sid_Builtin_Administrators
, &gid
)) {
537 if (!secrets_fetch_domain_sid(lp_workgroup(), &dom_sid
)) {
538 status
= NT_STATUS_OK
;
539 DEBUG(3, ("Failed to fetch domain sid for %s\n",
542 status
= create_builtin_administrators(&dom_sid
);
546 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
547 /* Add BUILTIN\Administrators directly to token. */
548 status
= add_builtin_administrators(result
, &dom_sid
);
549 if ( !NT_STATUS_IS_OK(status
) ) {
550 DEBUG(3, ("Failed to check for local "
551 "Administrators membership (%s)\n",
554 } else if (!NT_STATUS_IS_OK(status
)) {
555 DEBUG(2, ("WARNING: Failed to create "
556 "BUILTIN\\Administrators group! Can "
557 "Winbind allocate gids?\n"));
561 /* Deal with the BUILTIN\Users group. If the SID can
562 be resolved then assume that the add_aliasmem( S-1-5-32 )
565 if (!sid_to_gid(&global_sid_Builtin_Users
, &gid
)) {
568 if (!secrets_fetch_domain_sid(lp_workgroup(), &dom_sid
)) {
569 status
= NT_STATUS_OK
;
570 DEBUG(3, ("Failed to fetch domain sid for %s\n",
573 status
= create_builtin_users(&dom_sid
);
577 if (!NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
) &&
578 !NT_STATUS_IS_OK(status
))
580 DEBUG(2, ("WARNING: Failed to create BUILTIN\\Users group! "
581 "Can Winbind allocate gids?\n"));
585 /* Deal with local groups */
587 if (lp_winbind_nested_groups()) {
591 /* Now add the aliases. First the one from our local SAM */
593 status
= add_aliases(get_global_sam_sid(), result
);
595 if (!NT_STATUS_IS_OK(status
)) {
600 /* Finally the builtin ones */
602 status
= add_aliases(&global_sid_Builtin
, result
);
604 if (!NT_STATUS_IS_OK(status
)) {
612 /* Add privileges based on current user sids */
614 get_privileges_for_sids(&result
->privilege_mask
, result
->sids
,
620 /****************************************************************************
621 prints a UNIX 'token' to debug output.
622 ****************************************************************************/
624 void debug_unix_user_token(int dbg_class
, int dbg_lev
, uid_t uid
, gid_t gid
,
625 int n_groups
, gid_t
*groups
)
628 DEBUGC(dbg_class
, dbg_lev
,
629 ("UNIX token of user %ld\n", (long int)uid
));
631 DEBUGADDC(dbg_class
, dbg_lev
,
632 ("Primary group is %ld and contains %i supplementary "
633 "groups\n", (long int)gid
, n_groups
));
634 for (i
= 0; i
< n_groups
; i
++)
635 DEBUGADDC(dbg_class
, dbg_lev
, ("Group[%3i]: %ld\n", i
,
636 (long int)groups
[i
]));
640 * Create an artificial NT token given just a domain SID.
644 * unmapped unix users: Go directly to nss to find the user's group.
646 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
648 * If the user is provided by winbind, the primary gid is set to "domain
649 * users" of the user's domain. For an explanation why this is necessary, see
650 * the thread starting at
651 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
654 static NTSTATUS
create_token_from_sid(TALLOC_CTX
*mem_ctx
,
655 const struct dom_sid
*user_sid
,
657 uid_t
*uid
, gid_t
*gid
,
658 char **found_username
,
659 struct security_token
**token
)
661 NTSTATUS result
= NT_STATUS_NO_SUCH_USER
;
662 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
664 struct dom_sid
*group_sids
;
665 struct dom_sid unix_group_sid
;
666 uint32_t num_group_sids
;
672 if (sid_check_is_in_our_sam(user_sid
)) {
674 uint32_t pdb_num_group_sids
;
675 /* This is a passdb user, so ask passdb */
677 struct samu
*sam_acct
= NULL
;
679 if ( !(sam_acct
= samu_new( tmp_ctx
)) ) {
680 result
= NT_STATUS_NO_MEMORY
;
685 ret
= pdb_getsampwsid(sam_acct
, user_sid
);
689 DEBUG(1, ("pdb_getsampwsid(%s) failed\n",
690 sid_string_dbg(user_sid
)));
691 DEBUGADD(1, ("Fall back to unix user\n"));
695 result
= pdb_enum_group_memberships(tmp_ctx
, sam_acct
,
697 &pdb_num_group_sids
);
698 if (!NT_STATUS_IS_OK(result
)) {
699 DEBUG(1, ("enum_group_memberships failed for %s: "
700 "%s\n", sid_string_dbg(user_sid
),
702 DEBUGADD(1, ("Fall back to unix uid lookup\n"));
705 num_group_sids
= pdb_num_group_sids
;
707 /* see the smb_panic() in pdb_default_enum_group_memberships */
708 SMB_ASSERT(num_group_sids
> 0);
712 /* Ensure we're returning the found_username on the right context. */
713 *found_username
= talloc_strdup(mem_ctx
,
714 pdb_get_username(sam_acct
));
716 if (*found_username
== NULL
) {
717 result
= NT_STATUS_NO_MEMORY
;
722 * If the SID from lookup_name() was the guest sid, passdb knows
723 * about the mapping of guest sid to lp_guest_account()
724 * username and will return the unix_pw info for a guest
725 * user. Use it if it's there, else lookup the *uid details
726 * using Get_Pwnam_alloc(). See bug #6291 for details. JRA.
729 /* We must always assign the *uid. */
730 if (sam_acct
->unix_pw
== NULL
) {
731 struct passwd
*pwd
= Get_Pwnam_alloc(sam_acct
, *found_username
);
733 DEBUG(10, ("Get_Pwnam_alloc failed for %s\n",
735 result
= NT_STATUS_NO_SUCH_USER
;
738 result
= samu_set_unix(sam_acct
, pwd
);
739 if (!NT_STATUS_IS_OK(result
)) {
740 DEBUG(10, ("samu_set_unix failed for %s\n",
742 result
= NT_STATUS_NO_SUCH_USER
;
746 *uid
= sam_acct
->unix_pw
->pw_uid
;
748 } else if (sid_check_is_in_unix_users(user_sid
)) {
749 struct dom_sid tmp_sid
;
750 uint32_t getgroups_num_group_sids
;
751 /* This is a unix user not in passdb. We need to ask nss
752 * directly, without consulting passdb */
757 * This goto target is used as a fallback for the passdb
758 * case. The concrete bug report is when passdb gave us an
764 if (!sid_to_uid(user_sid
, uid
)) {
765 DEBUG(1, ("unix_user case, sid_to_uid for %s failed\n",
766 sid_string_dbg(user_sid
)));
767 result
= NT_STATUS_NO_SUCH_USER
;
771 uid_to_unix_users_sid(*uid
, &tmp_sid
);
774 pass
= getpwuid_alloc(tmp_ctx
, *uid
);
776 DEBUG(1, ("getpwuid(%u) failed\n",
777 (unsigned int)*uid
));
781 if (!getgroups_unix_user(tmp_ctx
, pass
->pw_name
, pass
->pw_gid
,
782 &gids
, &getgroups_num_group_sids
)) {
783 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
787 num_group_sids
= getgroups_num_group_sids
;
789 if (num_group_sids
) {
790 group_sids
= talloc_array(tmp_ctx
, struct dom_sid
, num_group_sids
);
791 if (group_sids
== NULL
) {
792 DEBUG(1, ("talloc_array failed\n"));
793 result
= NT_STATUS_NO_MEMORY
;
800 for (i
=0; i
<num_group_sids
; i
++) {
801 gid_to_sid(&group_sids
[i
], gids
[i
]);
804 /* In getgroups_unix_user we always set the primary gid */
805 SMB_ASSERT(num_group_sids
> 0);
809 /* Ensure we're returning the found_username on the right context. */
810 *found_username
= talloc_strdup(mem_ctx
, pass
->pw_name
);
811 if (*found_username
== NULL
) {
812 result
= NT_STATUS_NO_MEMORY
;
817 /* This user is from winbind, force the primary gid to the
818 * user's "domain users" group. Under certain circumstances
819 * (user comes from NT4), this might be a loss of
820 * information. But we can not rely on winbind getting the
821 * correct info. AD might prohibit winbind looking up that
824 /* We must always assign the *uid. */
825 if (!sid_to_uid(user_sid
, uid
)) {
826 DEBUG(1, ("winbindd case, sid_to_uid for %s failed\n",
827 sid_string_dbg(user_sid
)));
828 result
= NT_STATUS_NO_SUCH_USER
;
833 group_sids
= talloc_array(tmp_ctx
, struct dom_sid
, num_group_sids
);
834 if (group_sids
== NULL
) {
835 DEBUG(1, ("talloc_array failed\n"));
836 result
= NT_STATUS_NO_MEMORY
;
840 sid_copy(&group_sids
[0], user_sid
);
841 sid_split_rid(&group_sids
[0], NULL
);
842 sid_append_rid(&group_sids
[0], DOMAIN_RID_USERS
);
844 if (!sid_to_gid(&group_sids
[0], gid
)) {
845 DEBUG(1, ("sid_to_gid(%s) failed\n",
846 sid_string_dbg(&group_sids
[0])));
852 *found_username
= NULL
;
855 /* Add the "Unix Group" SID for each gid to catch mapped groups
856 and their Unix equivalent. This is to solve the backwards
857 compatibility problem of 'valid users = +ntadmin' where
858 ntadmin has been paired with "Domain Admins" in the group
859 mapping table. Otherwise smb.conf would need to be changed
860 to 'valid user = "Domain Admins"'. --jerry */
862 num_gids
= num_group_sids
;
863 range_ok
= lp_idmap_default_range(&low
, &high
);
864 for ( i
=0; i
<num_gids
; i
++ ) {
866 /* don't pickup anything managed by Winbind */
867 if (range_ok
&& (gids
[i
] >= low
) && (gids
[i
] <= high
)) {
871 gid_to_unix_groups_sid(gids
[i
], &unix_group_sid
);
873 result
= add_sid_to_array_unique(tmp_ctx
, &unix_group_sid
,
874 &group_sids
, &num_group_sids
);
875 if (!NT_STATUS_IS_OK(result
)) {
880 /* Ensure we're creating the nt_token on the right context. */
881 *token
= create_local_nt_token(mem_ctx
, user_sid
,
882 is_guest
, num_group_sids
, group_sids
);
884 if (*token
== NULL
) {
885 result
= NT_STATUS_NO_MEMORY
;
889 result
= NT_STATUS_OK
;
891 TALLOC_FREE(tmp_ctx
);
896 * Create an artificial NT token given just a username. (Initially intended
899 * We go through lookup_name() to avoid problems we had with 'winbind use
904 * unmapped unix users: Go directly to nss to find the user's group.
906 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
908 * If the user is provided by winbind, the primary gid is set to "domain
909 * users" of the user's domain. For an explanation why this is necessary, see
910 * the thread starting at
911 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
914 NTSTATUS
create_token_from_username(TALLOC_CTX
*mem_ctx
, const char *username
,
916 uid_t
*uid
, gid_t
*gid
,
917 char **found_username
,
918 struct security_token
**token
)
920 NTSTATUS result
= NT_STATUS_NO_SUCH_USER
;
921 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
922 struct dom_sid user_sid
;
923 enum lsa_SidType type
;
925 if (!lookup_name_smbconf(tmp_ctx
, username
, LOOKUP_NAME_ALL
,
926 NULL
, NULL
, &user_sid
, &type
)) {
927 DEBUG(1, ("lookup_name_smbconf for %s failed\n", username
));
931 if (type
!= SID_NAME_USER
) {
932 DEBUG(1, ("%s is a %s, not a user\n", username
,
933 sid_type_lookup(type
)));
937 result
= create_token_from_sid(mem_ctx
, &user_sid
, is_guest
, uid
, gid
, found_username
, token
);
939 if (!NT_STATUS_IS_OK(result
)) {
944 * If result == NT_STATUS_OK then
945 * we know we have a valid token. Ensure
946 * we also have a valid username to match.
949 if (*found_username
== NULL
) {
950 *found_username
= talloc_strdup(mem_ctx
, username
);
951 if (*found_username
== NULL
) {
952 result
= NT_STATUS_NO_MEMORY
;
957 TALLOC_FREE(tmp_ctx
);
961 /***************************************************************************
962 Build upon create_token_from_sid:
964 Expensive helper function to figure out whether a user given its sid is
965 member of a particular group.
966 ***************************************************************************/
968 bool user_sid_in_group_sid(const struct dom_sid
*sid
, const struct dom_sid
*group_sid
)
973 char *found_username
;
974 struct security_token
*token
;
976 enum lsa_SidType type
;
977 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
979 if (!lookup_sid(mem_ctx
, sid
,
980 NULL
, NULL
, &type
)) {
981 DEBUG(1, ("lookup_sid for %s failed\n", dom_sid_string(mem_ctx
, sid
)));
985 if (type
!= SID_NAME_USER
) {
986 DEBUG(5, ("%s is a %s, not a user\n", dom_sid_string(mem_ctx
, sid
),
987 sid_type_lookup(type
)));
991 status
= create_token_from_sid(mem_ctx
, sid
, False
,
992 &uid
, &gid
, &found_username
,
995 if (!NT_STATUS_IS_OK(status
)) {
996 DEBUG(10, ("could not create token for %s\n", dom_sid_string(mem_ctx
, sid
)));
1000 result
= security_token_has_sid(token
, group_sid
);
1003 TALLOC_FREE(mem_ctx
);
1007 /***************************************************************************
1008 Build upon create_token_from_username:
1010 Expensive helper function to figure out whether a user given its name is
1011 member of a particular group.
1012 ***************************************************************************/
1014 bool user_in_group_sid(const char *username
, const struct dom_sid
*group_sid
)
1019 char *found_username
;
1020 struct security_token
*token
;
1022 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1024 status
= create_token_from_username(mem_ctx
, username
, False
,
1025 &uid
, &gid
, &found_username
,
1028 if (!NT_STATUS_IS_OK(status
)) {
1029 DEBUG(10, ("could not create token for %s\n", username
));
1030 TALLOC_FREE(mem_ctx
);
1034 result
= security_token_has_sid(token
, group_sid
);
1036 TALLOC_FREE(mem_ctx
);
1040 bool user_in_group(const char *username
, const char *groupname
)
1042 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1043 struct dom_sid group_sid
;
1046 ret
= lookup_name(mem_ctx
, groupname
, LOOKUP_NAME_ALL
,
1047 NULL
, NULL
, &group_sid
, NULL
);
1048 TALLOC_FREE(mem_ctx
);
1051 DEBUG(10, ("lookup_name for (%s) failed.\n", groupname
));
1055 return user_in_group_sid(username
, &group_sid
);