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 */
30 #include "../librpc/gen_ndr/netlogon.h"
31 #include "../libcli/security/security.h"
32 #include "../lib/util/util_pw.h"
34 /****************************************************************************
35 Check for a SID in an struct security_token
36 ****************************************************************************/
38 bool nt_token_check_sid ( const struct dom_sid
*sid
, const struct security_token
*token
)
43 return security_token_has_sid(token
, sid
);
46 bool nt_token_check_domain_rid( struct security_token
*token
, uint32 rid
)
48 struct dom_sid domain_sid
;
50 /* if we are a domain member, the get the domain SID, else for
51 a DC or standalone server, use our own SID */
53 if ( lp_server_role() == ROLE_DOMAIN_MEMBER
) {
54 if ( !secrets_fetch_domain_sid( lp_workgroup(),
56 DEBUG(1,("nt_token_check_domain_rid: Cannot lookup "
57 "SID for domain [%s]\n", lp_workgroup()));
62 sid_copy( &domain_sid
, get_global_sam_sid() );
64 sid_append_rid( &domain_sid
, rid
);
66 return nt_token_check_sid( &domain_sid
, token
);\
69 /******************************************************************************
70 Create a token for the root user to be used internally by smbd.
71 This is similar to running under the context of the LOCAL_SYSTEM account
72 in Windows. This is a read-only token. Do not modify it or free() it.
73 Create a copy if you need to change it.
74 ******************************************************************************/
76 struct security_token
*get_root_nt_token( void )
78 struct security_token
*token
, *for_cache
;
79 struct dom_sid u_sid
, g_sid
;
83 cache_data
= memcache_lookup_talloc(
84 NULL
, SINGLETON_CACHE_TALLOC
,
85 data_blob_string_const_null("root_nt_token"));
87 if (cache_data
!= NULL
) {
88 return talloc_get_type_abort(
89 cache_data
, struct security_token
);
92 if ( !(pw
= sys_getpwuid(0)) ) {
93 if ( !(pw
= sys_getpwnam("root")) ) {
94 DEBUG(0,("get_root_nt_token: both sys_getpwuid(0) "
95 "and sys_getpwnam(\"root\") failed!\n"));
100 /* get the user and primary group SIDs; although the
101 BUILTIN\Administrators SId is really the one that matters here */
103 uid_to_sid(&u_sid
, pw
->pw_uid
);
104 gid_to_sid(&g_sid
, pw
->pw_gid
);
106 token
= create_local_nt_token(talloc_tos(), &u_sid
, False
,
107 1, &global_sid_Builtin_Administrators
);
109 security_token_set_privilege(token
, SEC_PRIV_DISK_OPERATOR
);
114 NULL
, SINGLETON_CACHE_TALLOC
,
115 data_blob_string_const_null("root_nt_token"), &for_cache
);
122 * Add alias SIDs from memberships within the partially created token SID list
125 NTSTATUS
add_aliases(const struct dom_sid
*domain_sid
,
126 struct security_token
*token
)
129 size_t i
, num_aliases
;
133 if (!(tmp_ctx
= talloc_init("add_aliases"))) {
134 return NT_STATUS_NO_MEMORY
;
140 status
= pdb_enum_alias_memberships(tmp_ctx
, domain_sid
,
143 &aliases
, &num_aliases
);
145 if (!NT_STATUS_IS_OK(status
)) {
146 DEBUG(10, ("pdb_enum_alias_memberships failed: %s\n",
151 for (i
=0; i
<num_aliases
; i
++) {
152 struct dom_sid alias_sid
;
153 sid_compose(&alias_sid
, domain_sid
, aliases
[i
]);
154 status
= add_sid_to_array_unique(token
, &alias_sid
,
157 if (!NT_STATUS_IS_OK(status
)) {
158 DEBUG(0, ("add_sid_to_array failed\n"));
164 TALLOC_FREE(tmp_ctx
);
168 /*******************************************************************
169 *******************************************************************/
171 static NTSTATUS
add_builtin_administrators(struct security_token
*token
,
172 const struct dom_sid
*dom_sid
)
174 struct dom_sid domadm
;
177 /* nothing to do if we aren't in a domain */
179 if ( !(IS_DC
|| lp_server_role()==ROLE_DOMAIN_MEMBER
) ) {
183 /* Find the Domain Admins SID */
186 sid_copy( &domadm
, get_global_sam_sid() );
188 sid_copy(&domadm
, dom_sid
);
190 sid_append_rid( &domadm
, DOMAIN_RID_ADMINS
);
192 /* Add Administrators if the user beloongs to Domain Admins */
194 if ( nt_token_check_sid( &domadm
, token
) ) {
195 status
= add_sid_to_array(token
,
196 &global_sid_Builtin_Administrators
,
197 &token
->sids
, &token
->num_sids
);
198 if (!NT_STATUS_IS_OK(status
)) {
207 * Create the requested BUILTIN if it doesn't already exist. This requires
208 * winbindd to be running.
210 * @param[in] rid BUILTIN rid to create
211 * @return Normal NTSTATUS return.
213 static NTSTATUS
create_builtin(uint32 rid
)
215 NTSTATUS status
= NT_STATUS_OK
;
219 if (!sid_compose(&sid
, &global_sid_Builtin
, rid
)) {
220 return NT_STATUS_NO_SUCH_ALIAS
;
223 if (!sid_to_gid(&sid
, &gid
)) {
224 if (!lp_winbind_nested_groups() || !winbind_ping()) {
225 return NT_STATUS_PROTOCOL_UNREACHABLE
;
227 status
= pdb_create_builtin_alias(rid
);
233 * Add sid as a member of builtin_sid.
235 * @param[in] builtin_sid An existing builtin group.
236 * @param[in] dom_sid sid to add as a member of builtin_sid.
237 * @return Normal NTSTATUS return
239 static NTSTATUS
add_sid_to_builtin(const struct dom_sid
*builtin_sid
,
240 const struct dom_sid
*dom_sid
)
242 NTSTATUS status
= NT_STATUS_OK
;
244 if (!dom_sid
|| !builtin_sid
) {
245 return NT_STATUS_INVALID_PARAMETER
;
248 status
= pdb_add_aliasmem(builtin_sid
, dom_sid
);
250 if (NT_STATUS_EQUAL(status
, NT_STATUS_MEMBER_IN_ALIAS
)) {
251 DEBUG(5, ("add_sid_to_builtin %s is already a member of %s\n",
252 sid_string_dbg(dom_sid
),
253 sid_string_dbg(builtin_sid
)));
257 if (!NT_STATUS_IS_OK(status
)) {
258 DEBUG(4, ("add_sid_to_builtin %s could not be added to %s: "
259 "%s\n", sid_string_dbg(dom_sid
),
260 sid_string_dbg(builtin_sid
), nt_errstr(status
)));
265 /*******************************************************************
266 *******************************************************************/
268 NTSTATUS
create_builtin_users(const struct dom_sid
*dom_sid
)
271 struct dom_sid dom_users
;
273 status
= create_builtin(BUILTIN_RID_USERS
);
274 if ( !NT_STATUS_IS_OK(status
) ) {
275 DEBUG(5,("create_builtin_users: Failed to create Users\n"));
279 /* add domain users */
280 if ((IS_DC
|| (lp_server_role() == ROLE_DOMAIN_MEMBER
))
281 && sid_compose(&dom_users
, dom_sid
, DOMAIN_RID_USERS
))
283 status
= add_sid_to_builtin(&global_sid_Builtin_Users
,
290 /*******************************************************************
291 *******************************************************************/
293 NTSTATUS
create_builtin_administrators(const struct dom_sid
*dom_sid
)
296 struct dom_sid dom_admins
, root_sid
;
298 enum lsa_SidType type
;
302 status
= create_builtin(BUILTIN_RID_ADMINISTRATORS
);
303 if ( !NT_STATUS_IS_OK(status
) ) {
304 DEBUG(5,("create_builtin_administrators: Failed to create Administrators\n"));
308 /* add domain admins */
309 if ((IS_DC
|| (lp_server_role() == ROLE_DOMAIN_MEMBER
))
310 && sid_compose(&dom_admins
, dom_sid
, DOMAIN_RID_ADMINS
))
312 status
= add_sid_to_builtin(&global_sid_Builtin_Administrators
,
314 if (!NT_STATUS_IS_OK(status
)) {
320 if ( (ctx
= talloc_init("create_builtin_administrators")) == NULL
) {
321 return NT_STATUS_NO_MEMORY
;
323 fstr_sprintf( root_name
, "%s\\root", get_global_sam_name() );
324 ret
= lookup_name(ctx
, root_name
, LOOKUP_NAME_DOMAIN
, NULL
, NULL
,
329 status
= add_sid_to_builtin(&global_sid_Builtin_Administrators
,
336 static NTSTATUS
finalize_local_nt_token(struct security_token
*result
,
339 NTSTATUS
create_local_nt_token_from_info3(TALLOC_CTX
*mem_ctx
,
341 struct netr_SamInfo3
*info3
,
342 struct extra_auth_info
*extra
,
343 struct security_token
**ntok
)
345 struct security_token
*usrtok
= NULL
;
349 DEBUG(10, ("Create local NT token for %s\n",
350 info3
->base
.account_name
.string
));
352 usrtok
= talloc_zero(mem_ctx
, struct security_token
);
354 DEBUG(0, ("talloc failed\n"));
355 return NT_STATUS_NO_MEMORY
;
358 /* Add the user and primary group sid FIRST */
359 /* check if the user rid is the special "Domain Guests" rid.
360 * If so pick the first sid for the extra sids instead as it
361 * is a local fake account */
362 usrtok
->sids
= talloc_array(usrtok
, struct dom_sid
, 2);
365 return NT_STATUS_NO_MEMORY
;
367 usrtok
->num_sids
= 2;
370 if (info3
->base
.rid
== (uint32_t)(-1)) {
371 /* this is a signal the user was fake and generated,
372 * the actual SID we want to use is stored in the extra
374 if (is_null_sid(&extra
->user_sid
)) {
375 /* we couldn't find the user sid, bail out */
376 DEBUG(3, ("Invalid user SID\n"));
378 return NT_STATUS_UNSUCCESSFUL
;
380 sid_copy(&usrtok
->sids
[0], &extra
->user_sid
);
382 sid_copy(&usrtok
->sids
[0], info3
->base
.domain_sid
);
383 sid_append_rid(&usrtok
->sids
[0], info3
->base
.rid
);
387 if (info3
->base
.primary_gid
== (uint32_t)(-1)) {
388 /* this is a signal the user was fake and generated,
389 * the actual SID we want to use is stored in the extra
391 if (is_null_sid(&extra
->pgid_sid
)) {
392 /* we couldn't find the user sid, bail out */
393 DEBUG(3, ("Invalid group SID\n"));
395 return NT_STATUS_UNSUCCESSFUL
;
397 sid_copy(&usrtok
->sids
[1], &extra
->pgid_sid
);
399 sid_copy(&usrtok
->sids
[1], info3
->base
.domain_sid
);
400 sid_append_rid(&usrtok
->sids
[1],
401 info3
->base
.primary_gid
);
404 /* Now the SIDs we got from authentication. These are the ones from
405 * the info3 struct or from the pdb_enum_group_memberships, depending
406 * on who authenticated the user.
407 * Note that we start the for loop at "1" here, we already added the
408 * first group sid as primary above. */
410 for (i
= 0; i
< info3
->base
.groups
.count
; i
++) {
411 struct dom_sid tmp_sid
;
413 sid_copy(&tmp_sid
, info3
->base
.domain_sid
);
414 sid_append_rid(&tmp_sid
, info3
->base
.groups
.rids
[i
].rid
);
416 status
= add_sid_to_array_unique(usrtok
, &tmp_sid
,
419 if (!NT_STATUS_IS_OK(status
)) {
420 DEBUG(3, ("Failed to add SID to nt token\n"));
426 /* now also add extra sids if they are not the special user/group
428 for (i
= 0; i
< info3
->sidcount
; i
++) {
429 status
= add_sid_to_array_unique(usrtok
,
433 if (!NT_STATUS_IS_OK(status
)) {
434 DEBUG(3, ("Failed to add SID to nt token\n"));
440 status
= finalize_local_nt_token(usrtok
, is_guest
);
441 if (!NT_STATUS_IS_OK(status
)) {
442 DEBUG(3, ("Failed to finalize nt token\n"));
451 /*******************************************************************
452 Create a NT token for the user, expanding local aliases
453 *******************************************************************/
455 struct security_token
*create_local_nt_token(TALLOC_CTX
*mem_ctx
,
456 const struct dom_sid
*user_sid
,
459 const struct dom_sid
*groupsids
)
461 struct security_token
*result
= NULL
;
465 DEBUG(10, ("Create local NT token for %s\n",
466 sid_string_dbg(user_sid
)));
468 if (!(result
= TALLOC_ZERO_P(mem_ctx
, struct security_token
))) {
469 DEBUG(0, ("talloc failed\n"));
473 /* Add the user and primary group sid */
475 status
= add_sid_to_array(result
, user_sid
,
476 &result
->sids
, &result
->num_sids
);
477 if (!NT_STATUS_IS_OK(status
)) {
482 /* For guest, num_groupsids may be zero. */
484 status
= add_sid_to_array(result
, &groupsids
[0],
487 if (!NT_STATUS_IS_OK(status
)) {
493 /* Now the SIDs we got from authentication. These are the ones from
494 * the info3 struct or from the pdb_enum_group_memberships, depending
495 * on who authenticated the user.
496 * Note that we start the for loop at "1" here, we already added the
497 * first group sid as primary above. */
499 for (i
=1; i
<num_groupsids
; i
++) {
500 status
= add_sid_to_array_unique(result
, &groupsids
[i
],
503 if (!NT_STATUS_IS_OK(status
)) {
509 status
= finalize_local_nt_token(result
, is_guest
);
510 if (!NT_STATUS_IS_OK(status
)) {
518 static NTSTATUS
finalize_local_nt_token(struct security_token
*result
,
521 struct dom_sid dom_sid
;
525 /* Add in BUILTIN sids */
527 status
= add_sid_to_array(result
, &global_sid_World
,
528 &result
->sids
, &result
->num_sids
);
529 if (!NT_STATUS_IS_OK(status
)) {
532 status
= add_sid_to_array(result
, &global_sid_Network
,
533 &result
->sids
, &result
->num_sids
);
534 if (!NT_STATUS_IS_OK(status
)) {
539 status
= add_sid_to_array(result
, &global_sid_Builtin_Guests
,
542 if (!NT_STATUS_IS_OK(status
)) {
546 status
= add_sid_to_array(result
,
547 &global_sid_Authenticated_Users
,
550 if (!NT_STATUS_IS_OK(status
)) {
555 /* Deal with the BUILTIN\Administrators group. If the SID can
556 be resolved then assume that the add_aliasmem( S-1-5-32 )
559 if (!sid_to_gid(&global_sid_Builtin_Administrators
, &gid
)) {
562 if (!secrets_fetch_domain_sid(lp_workgroup(), &dom_sid
)) {
563 status
= NT_STATUS_OK
;
564 DEBUG(3, ("Failed to fetch domain sid for %s\n",
567 status
= create_builtin_administrators(&dom_sid
);
571 if (NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
)) {
572 /* Add BUILTIN\Administrators directly to token. */
573 status
= add_builtin_administrators(result
, &dom_sid
);
574 if ( !NT_STATUS_IS_OK(status
) ) {
575 DEBUG(3, ("Failed to check for local "
576 "Administrators membership (%s)\n",
579 } else if (!NT_STATUS_IS_OK(status
)) {
580 DEBUG(2, ("WARNING: Failed to create "
581 "BUILTIN\\Administrators group! Can "
582 "Winbind allocate gids?\n"));
586 /* Deal with the BUILTIN\Users group. If the SID can
587 be resolved then assume that the add_aliasmem( S-1-5-32 )
590 if (!sid_to_gid(&global_sid_Builtin_Users
, &gid
)) {
593 if (!secrets_fetch_domain_sid(lp_workgroup(), &dom_sid
)) {
594 status
= NT_STATUS_OK
;
595 DEBUG(3, ("Failed to fetch domain sid for %s\n",
598 status
= create_builtin_users(&dom_sid
);
602 if (!NT_STATUS_EQUAL(status
, NT_STATUS_PROTOCOL_UNREACHABLE
) &&
603 !NT_STATUS_IS_OK(status
))
605 DEBUG(2, ("WARNING: Failed to create BUILTIN\\Users group! "
606 "Can Winbind allocate gids?\n"));
610 /* Deal with local groups */
612 if (lp_winbind_nested_groups()) {
616 /* Now add the aliases. First the one from our local SAM */
618 status
= add_aliases(get_global_sam_sid(), result
);
620 if (!NT_STATUS_IS_OK(status
)) {
625 /* Finally the builtin ones */
627 status
= add_aliases(&global_sid_Builtin
, result
);
629 if (!NT_STATUS_IS_OK(status
)) {
637 /* Add privileges based on current user sids */
639 get_privileges_for_sids(&result
->privilege_mask
, result
->sids
,
645 /****************************************************************************
646 prints a UNIX 'token' to debug output.
647 ****************************************************************************/
649 void debug_unix_user_token(int dbg_class
, int dbg_lev
, uid_t uid
, gid_t gid
,
650 int n_groups
, gid_t
*groups
)
653 DEBUGC(dbg_class
, dbg_lev
,
654 ("UNIX token of user %ld\n", (long int)uid
));
656 DEBUGADDC(dbg_class
, dbg_lev
,
657 ("Primary group is %ld and contains %i supplementary "
658 "groups\n", (long int)gid
, n_groups
));
659 for (i
= 0; i
< n_groups
; i
++)
660 DEBUGADDC(dbg_class
, dbg_lev
, ("Group[%3i]: %ld\n", i
,
661 (long int)groups
[i
]));
665 * Create an artificial NT token given just a username. (Initially intended
668 * We go through lookup_name() to avoid problems we had with 'winbind use
673 * unmapped unix users: Go directly to nss to find the user's group.
675 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
677 * If the user is provided by winbind, the primary gid is set to "domain
678 * users" of the user's domain. For an explanation why this is necessary, see
679 * the thread starting at
680 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
683 NTSTATUS
create_token_from_username(TALLOC_CTX
*mem_ctx
, const char *username
,
685 uid_t
*uid
, gid_t
*gid
,
686 char **found_username
,
687 struct security_token
**token
)
689 NTSTATUS result
= NT_STATUS_NO_SUCH_USER
;
690 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
691 struct dom_sid user_sid
;
692 enum lsa_SidType type
;
694 struct dom_sid
*group_sids
;
695 struct dom_sid unix_group_sid
;
696 uint32_t num_group_sids
;
700 if (!lookup_name_smbconf(tmp_ctx
, username
, LOOKUP_NAME_ALL
,
701 NULL
, NULL
, &user_sid
, &type
)) {
702 DEBUG(1, ("lookup_name_smbconf for %s failed\n", username
));
706 if (type
!= SID_NAME_USER
) {
707 DEBUG(1, ("%s is a %s, not a user\n", username
,
708 sid_type_lookup(type
)));
712 if (sid_check_is_in_our_domain(&user_sid
)) {
714 uint32_t pdb_num_group_sids
;
715 /* This is a passdb user, so ask passdb */
717 struct samu
*sam_acct
= NULL
;
719 if ( !(sam_acct
= samu_new( tmp_ctx
)) ) {
720 result
= NT_STATUS_NO_MEMORY
;
725 ret
= pdb_getsampwsid(sam_acct
, &user_sid
);
729 DEBUG(1, ("pdb_getsampwsid(%s) for user %s failed\n",
730 sid_string_dbg(&user_sid
), username
));
731 DEBUGADD(1, ("Fall back to unix user %s\n", username
));
735 result
= pdb_enum_group_memberships(tmp_ctx
, sam_acct
,
737 &pdb_num_group_sids
);
738 if (!NT_STATUS_IS_OK(result
)) {
739 DEBUG(1, ("enum_group_memberships failed for %s (%s): "
740 "%s\n", username
, sid_string_dbg(&user_sid
),
742 DEBUGADD(1, ("Fall back to unix user %s\n", username
));
745 num_group_sids
= pdb_num_group_sids
;
747 /* see the smb_panic() in pdb_default_enum_group_memberships */
748 SMB_ASSERT(num_group_sids
> 0);
752 /* Ensure we're returning the found_username on the right context. */
753 *found_username
= talloc_strdup(mem_ctx
,
754 pdb_get_username(sam_acct
));
757 * If the SID from lookup_name() was the guest sid, passdb knows
758 * about the mapping of guest sid to lp_guestaccount()
759 * username and will return the unix_pw info for a guest
760 * user. Use it if it's there, else lookup the *uid details
761 * using Get_Pwnam_alloc(). See bug #6291 for details. JRA.
764 /* We must always assign the *uid. */
765 if (sam_acct
->unix_pw
== NULL
) {
766 struct passwd
*pwd
= Get_Pwnam_alloc(sam_acct
, *found_username
);
768 DEBUG(10, ("Get_Pwnam_alloc failed for %s\n",
770 result
= NT_STATUS_NO_SUCH_USER
;
773 result
= samu_set_unix(sam_acct
, pwd
);
774 if (!NT_STATUS_IS_OK(result
)) {
775 DEBUG(10, ("samu_set_unix failed for %s\n",
777 result
= NT_STATUS_NO_SUCH_USER
;
781 *uid
= sam_acct
->unix_pw
->pw_uid
;
783 } else if (sid_check_is_in_unix_users(&user_sid
)) {
784 uint32_t getgroups_num_group_sids
;
785 /* This is a unix user not in passdb. We need to ask nss
786 * directly, without consulting passdb */
791 * This goto target is used as a fallback for the passdb
792 * case. The concrete bug report is when passdb gave us an
798 if (!sid_to_uid(&user_sid
, uid
)) {
799 DEBUG(1, ("unix_user case, sid_to_uid for %s (%s) failed\n",
800 username
, sid_string_dbg(&user_sid
)));
801 result
= NT_STATUS_NO_SUCH_USER
;
805 uid_to_unix_users_sid(*uid
, &user_sid
);
807 pass
= getpwuid_alloc(tmp_ctx
, *uid
);
809 DEBUG(1, ("getpwuid(%u) for user %s failed\n",
810 (unsigned int)*uid
, username
));
814 if (!getgroups_unix_user(tmp_ctx
, username
, pass
->pw_gid
,
815 &gids
, &getgroups_num_group_sids
)) {
816 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
820 num_group_sids
= getgroups_num_group_sids
;
822 if (num_group_sids
) {
823 group_sids
= TALLOC_ARRAY(tmp_ctx
, struct dom_sid
, num_group_sids
);
824 if (group_sids
== NULL
) {
825 DEBUG(1, ("TALLOC_ARRAY failed\n"));
826 result
= NT_STATUS_NO_MEMORY
;
833 for (i
=0; i
<num_group_sids
; i
++) {
834 gid_to_sid(&group_sids
[i
], gids
[i
]);
837 /* In getgroups_unix_user we always set the primary gid */
838 SMB_ASSERT(num_group_sids
> 0);
842 /* Ensure we're returning the found_username on the right context. */
843 *found_username
= talloc_strdup(mem_ctx
, pass
->pw_name
);
846 /* This user is from winbind, force the primary gid to the
847 * user's "domain users" group. Under certain circumstances
848 * (user comes from NT4), this might be a loss of
849 * information. But we can not rely on winbind getting the
850 * correct info. AD might prohibit winbind looking up that
853 /* We must always assign the *uid. */
854 if (!sid_to_uid(&user_sid
, uid
)) {
855 DEBUG(1, ("winbindd case, sid_to_uid for %s (%s) failed\n",
856 username
, sid_string_dbg(&user_sid
)));
857 result
= NT_STATUS_NO_SUCH_USER
;
862 group_sids
= TALLOC_ARRAY(tmp_ctx
, struct dom_sid
, num_group_sids
);
863 if (group_sids
== NULL
) {
864 DEBUG(1, ("TALLOC_ARRAY failed\n"));
865 result
= NT_STATUS_NO_MEMORY
;
869 sid_copy(&group_sids
[0], &user_sid
);
870 sid_split_rid(&group_sids
[0], NULL
);
871 sid_append_rid(&group_sids
[0], DOMAIN_RID_USERS
);
873 if (!sid_to_gid(&group_sids
[0], gid
)) {
874 DEBUG(1, ("sid_to_gid(%s) failed\n",
875 sid_string_dbg(&group_sids
[0])));
881 /* Ensure we're returning the found_username on the right context. */
882 *found_username
= talloc_strdup(mem_ctx
, username
);
885 /* Add the "Unix Group" SID for each gid to catch mapped groups
886 and their Unix equivalent. This is to solve the backwards
887 compatibility problem of 'valid users = +ntadmin' where
888 ntadmin has been paired with "Domain Admins" in the group
889 mapping table. Otherwise smb.conf would need to be changed
890 to 'valid user = "Domain Admins"'. --jerry */
892 num_gids
= num_group_sids
;
893 for ( i
=0; i
<num_gids
; i
++ ) {
896 /* don't pickup anything managed by Winbind */
898 if ( lp_idmap_gid(&low
, &high
) && (gids
[i
] >= low
) && (gids
[i
] <= high
) )
901 gid_to_unix_groups_sid(gids
[i
], &unix_group_sid
);
903 result
= add_sid_to_array_unique(tmp_ctx
, &unix_group_sid
,
904 &group_sids
, &num_group_sids
);
905 if (!NT_STATUS_IS_OK(result
)) {
910 /* Ensure we're creating the nt_token on the right context. */
911 *token
= create_local_nt_token(mem_ctx
, &user_sid
,
912 is_guest
, num_group_sids
, group_sids
);
914 if ((*token
== NULL
) || (*found_username
== NULL
)) {
915 result
= NT_STATUS_NO_MEMORY
;
919 result
= NT_STATUS_OK
;
921 TALLOC_FREE(tmp_ctx
);
925 /***************************************************************************
926 Build upon create_token_from_username:
928 Expensive helper function to figure out whether a user given its name is
929 member of a particular group.
930 ***************************************************************************/
932 bool user_in_group_sid(const char *username
, const struct dom_sid
*group_sid
)
937 char *found_username
;
938 struct security_token
*token
;
940 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
942 status
= create_token_from_username(mem_ctx
, username
, False
,
943 &uid
, &gid
, &found_username
,
946 if (!NT_STATUS_IS_OK(status
)) {
947 DEBUG(10, ("could not create token for %s\n", username
));
948 TALLOC_FREE(mem_ctx
);
952 result
= security_token_has_sid(token
, group_sid
);
954 TALLOC_FREE(mem_ctx
);
958 bool user_in_group(const char *username
, const char *groupname
)
960 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
961 struct dom_sid group_sid
;
964 ret
= lookup_name(mem_ctx
, groupname
, LOOKUP_NAME_ALL
,
965 NULL
, NULL
, &group_sid
, NULL
);
966 TALLOC_FREE(mem_ctx
);
969 DEBUG(10, ("lookup_name for (%s) failed.\n", groupname
));
973 return user_in_group_sid(username
, &group_sid
);