dns_server: Do not look for a wildcard for @
[Samba.git] / source3 / auth / token_util.c
blob03c4b646007b8d1f61e6a76aea5938ff6e5cf56a
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 sid_copy(&domadm, dom_sid);
195 sid_append_rid( &domadm, DOMAIN_RID_ADMINS );
197 /* Add Administrators if the user beloongs to Domain Admins */
199 if ( nt_token_check_sid( &domadm, token ) ) {
200 status = add_sid_to_array(token,
201 &global_sid_Builtin_Administrators,
202 &token->sids, &token->num_sids);
203 if (!NT_STATUS_IS_OK(status)) {
204 return status;
208 return NT_STATUS_OK;
211 static NTSTATUS finalize_local_nt_token(struct security_token *result,
212 bool is_guest);
214 NTSTATUS get_user_sid_info3_and_extra(const struct netr_SamInfo3 *info3,
215 const struct extra_auth_info *extra,
216 struct dom_sid *sid)
218 /* USER SID */
219 if (info3->base.rid == (uint32_t)(-1)) {
220 /* this is a signal the user was fake and generated,
221 * the actual SID we want to use is stored in the extra
222 * sids */
223 if (is_null_sid(&extra->user_sid)) {
224 /* we couldn't find the user sid, bail out */
225 DEBUG(3, ("Invalid user SID\n"));
226 return NT_STATUS_UNSUCCESSFUL;
228 sid_copy(sid, &extra->user_sid);
229 } else {
230 sid_copy(sid, info3->base.domain_sid);
231 sid_append_rid(sid, info3->base.rid);
233 return NT_STATUS_OK;
236 NTSTATUS create_local_nt_token_from_info3(TALLOC_CTX *mem_ctx,
237 bool is_guest,
238 const struct netr_SamInfo3 *info3,
239 const struct extra_auth_info *extra,
240 struct security_token **ntok)
242 struct security_token *usrtok = NULL;
243 NTSTATUS status;
244 int i;
246 DEBUG(10, ("Create local NT token for %s\n",
247 info3->base.account_name.string));
249 usrtok = talloc_zero(mem_ctx, struct security_token);
250 if (!usrtok) {
251 DEBUG(0, ("talloc failed\n"));
252 return NT_STATUS_NO_MEMORY;
255 /* Add the user and primary group sid FIRST */
256 /* check if the user rid is the special "Domain Guests" rid.
257 * If so pick the first sid for the extra sids instead as it
258 * is a local fake account */
259 usrtok->sids = talloc_array(usrtok, struct dom_sid, 2);
260 if (!usrtok->sids) {
261 TALLOC_FREE(usrtok);
262 return NT_STATUS_NO_MEMORY;
264 usrtok->num_sids = 2;
266 status = get_user_sid_info3_and_extra(info3, extra, &usrtok->sids[0]);
267 if (!NT_STATUS_IS_OK(status)) {
268 TALLOC_FREE(usrtok);
269 return status;
272 /* GROUP SID */
273 if (info3->base.primary_gid == (uint32_t)(-1)) {
274 /* this is a signal the user was fake and generated,
275 * the actual SID we want to use is stored in the extra
276 * sids */
277 if (is_null_sid(&extra->pgid_sid)) {
278 /* we couldn't find the user sid, bail out */
279 DEBUG(3, ("Invalid group SID\n"));
280 TALLOC_FREE(usrtok);
281 return NT_STATUS_UNSUCCESSFUL;
283 sid_copy(&usrtok->sids[1], &extra->pgid_sid);
284 } else {
285 sid_copy(&usrtok->sids[1], info3->base.domain_sid);
286 sid_append_rid(&usrtok->sids[1],
287 info3->base.primary_gid);
290 /* Now the SIDs we got from authentication. These are the ones from
291 * the info3 struct or from the pdb_enum_group_memberships, depending
292 * on who authenticated the user.
293 * Note that we start the for loop at "1" here, we already added the
294 * first group sid as primary above. */
296 for (i = 0; i < info3->base.groups.count; i++) {
297 struct dom_sid tmp_sid;
299 sid_copy(&tmp_sid, info3->base.domain_sid);
300 sid_append_rid(&tmp_sid, info3->base.groups.rids[i].rid);
302 status = add_sid_to_array_unique(usrtok, &tmp_sid,
303 &usrtok->sids,
304 &usrtok->num_sids);
305 if (!NT_STATUS_IS_OK(status)) {
306 DEBUG(3, ("Failed to add SID to nt token\n"));
307 TALLOC_FREE(usrtok);
308 return status;
312 /* now also add extra sids if they are not the special user/group
313 * sids */
314 for (i = 0; i < info3->sidcount; i++) {
315 status = add_sid_to_array_unique(usrtok,
316 info3->sids[i].sid,
317 &usrtok->sids,
318 &usrtok->num_sids);
319 if (!NT_STATUS_IS_OK(status)) {
320 DEBUG(3, ("Failed to add SID to nt token\n"));
321 TALLOC_FREE(usrtok);
322 return status;
326 status = finalize_local_nt_token(usrtok, is_guest);
327 if (!NT_STATUS_IS_OK(status)) {
328 DEBUG(3, ("Failed to finalize nt token\n"));
329 TALLOC_FREE(usrtok);
330 return status;
333 *ntok = usrtok;
334 return NT_STATUS_OK;
337 /*******************************************************************
338 Create a NT token for the user, expanding local aliases
339 *******************************************************************/
341 struct security_token *create_local_nt_token(TALLOC_CTX *mem_ctx,
342 const struct dom_sid *user_sid,
343 bool is_guest,
344 int num_groupsids,
345 const struct dom_sid *groupsids)
347 struct security_token *result = NULL;
348 int i;
349 NTSTATUS status;
351 DEBUG(10, ("Create local NT token for %s\n",
352 sid_string_dbg(user_sid)));
354 if (!(result = talloc_zero(mem_ctx, struct security_token))) {
355 DEBUG(0, ("talloc failed\n"));
356 return NULL;
359 /* Add the user and primary group sid */
361 status = add_sid_to_array(result, user_sid,
362 &result->sids, &result->num_sids);
363 if (!NT_STATUS_IS_OK(status)) {
364 TALLOC_FREE(result);
365 return NULL;
368 /* For guest, num_groupsids may be zero. */
369 if (num_groupsids) {
370 status = add_sid_to_array(result, &groupsids[0],
371 &result->sids,
372 &result->num_sids);
373 if (!NT_STATUS_IS_OK(status)) {
374 TALLOC_FREE(result);
375 return NULL;
379 /* Now the SIDs we got from authentication. These are the ones from
380 * the info3 struct or from the pdb_enum_group_memberships, depending
381 * on who authenticated the user.
382 * Note that we start the for loop at "1" here, we already added the
383 * first group sid as primary above. */
385 for (i=1; i<num_groupsids; i++) {
386 status = add_sid_to_array_unique(result, &groupsids[i],
387 &result->sids,
388 &result->num_sids);
389 if (!NT_STATUS_IS_OK(status)) {
390 TALLOC_FREE(result);
391 return NULL;
395 status = finalize_local_nt_token(result, is_guest);
396 if (!NT_STATUS_IS_OK(status)) {
397 TALLOC_FREE(result);
398 return NULL;
401 return result;
404 /***************************************************
405 Merge in any groups from /etc/group.
406 ***************************************************/
408 static NTSTATUS add_local_groups(struct security_token *result,
409 bool is_guest)
411 gid_t *gids = NULL;
412 uint32_t getgroups_num_group_sids = 0;
413 struct passwd *pass = NULL;
414 TALLOC_CTX *tmp_ctx = talloc_stackframe();
415 int i;
417 if (is_guest) {
419 * Guest is a special case. It's always
420 * a user that can be looked up, but
421 * result->sids[0] is set to DOMAIN\Guest.
422 * Lookup by account name instead.
424 pass = Get_Pwnam_alloc(tmp_ctx, lp_guest_account());
425 } else {
426 uid_t uid;
428 /* For non-guest result->sids[0] is always the user sid. */
429 if (!sid_to_uid(&result->sids[0], &uid)) {
431 * Non-mappable SID like SYSTEM.
432 * Can't be in any /etc/group groups.
434 TALLOC_FREE(tmp_ctx);
435 return NT_STATUS_OK;
438 pass = getpwuid_alloc(tmp_ctx, uid);
439 if (pass == NULL) {
440 DEBUG(1, ("SID %s -> getpwuid(%u) failed\n",
441 sid_string_dbg(&result->sids[0]),
442 (unsigned int)uid));
446 if (!pass) {
447 TALLOC_FREE(tmp_ctx);
448 return NT_STATUS_UNSUCCESSFUL;
452 * Now we must get any groups this user has been
453 * added to in /etc/group and merge them in.
454 * This has to be done in every code path
455 * that creates an NT token, as remote users
456 * may have been added to the local /etc/group
457 * database. Tokens created merely from the
458 * info3 structs (via the DC or via the krb5 PAC)
459 * won't have these local groups. Note the
460 * groups added here will only be UNIX groups
461 * (S-1-22-2-XXXX groups) as getgroups_unix_user()
462 * turns off winbindd before calling getgroups().
464 * NB. This is duplicating work already
465 * done in the 'unix_user:' case of
466 * create_token_from_sid() but won't
467 * do anything other than be inefficient
468 * in that case.
471 if (!getgroups_unix_user(tmp_ctx, pass->pw_name, pass->pw_gid,
472 &gids, &getgroups_num_group_sids)) {
473 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
474 pass->pw_name));
475 TALLOC_FREE(tmp_ctx);
476 return NT_STATUS_UNSUCCESSFUL;
479 for (i=0; i<getgroups_num_group_sids; i++) {
480 NTSTATUS status;
481 struct dom_sid grp_sid;
482 gid_to_sid(&grp_sid, gids[i]);
484 status = add_sid_to_array_unique(result,
485 &grp_sid,
486 &result->sids,
487 &result->num_sids);
488 if (!NT_STATUS_IS_OK(status)) {
489 DEBUG(3, ("Failed to add UNIX SID to nt token\n"));
490 TALLOC_FREE(tmp_ctx);
491 return status;
494 TALLOC_FREE(tmp_ctx);
495 return NT_STATUS_OK;
498 static NTSTATUS finalize_local_nt_token(struct security_token *result,
499 bool is_guest)
501 struct dom_sid dom_sid;
502 NTSTATUS status;
503 struct acct_info *info;
505 /* Add any local groups. */
507 status = add_local_groups(result, is_guest);
508 if (!NT_STATUS_IS_OK(status)) {
509 return status;
512 /* Add in BUILTIN sids */
514 status = add_sid_to_array(result, &global_sid_World,
515 &result->sids, &result->num_sids);
516 if (!NT_STATUS_IS_OK(status)) {
517 return status;
519 status = add_sid_to_array(result, &global_sid_Network,
520 &result->sids, &result->num_sids);
521 if (!NT_STATUS_IS_OK(status)) {
522 return status;
525 if (is_guest) {
526 status = add_sid_to_array(result, &global_sid_Builtin_Guests,
527 &result->sids,
528 &result->num_sids);
529 if (!NT_STATUS_IS_OK(status)) {
530 return status;
532 } else {
533 status = add_sid_to_array(result,
534 &global_sid_Authenticated_Users,
535 &result->sids,
536 &result->num_sids);
537 if (!NT_STATUS_IS_OK(status)) {
538 return status;
542 info = talloc_zero(talloc_tos(), struct acct_info);
543 if (info == NULL) {
544 DEBUG(0, ("talloc failed!\n"));
545 return NT_STATUS_NO_MEMORY;
548 /* Deal with the BUILTIN\Administrators group. If the SID can
549 be resolved then assume that the add_aliasmem( S-1-5-32 )
550 handled it. */
552 status = pdb_get_aliasinfo(&global_sid_Builtin_Administrators, info);
553 if (!NT_STATUS_IS_OK(status)) {
555 become_root();
556 if (!secrets_fetch_domain_sid(lp_workgroup(), &dom_sid)) {
557 status = NT_STATUS_OK;
558 DEBUG(3, ("Failed to fetch domain sid for %s\n",
559 lp_workgroup()));
560 } else {
561 status = create_builtin_administrators(&dom_sid);
563 unbecome_root();
565 if (NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE)) {
566 /* Add BUILTIN\Administrators directly to token. */
567 status = add_builtin_administrators(result, &dom_sid);
568 if ( !NT_STATUS_IS_OK(status) ) {
569 DEBUG(3, ("Failed to check for local "
570 "Administrators membership (%s)\n",
571 nt_errstr(status)));
573 } else if (!NT_STATUS_IS_OK(status)) {
574 DEBUG(2, ("WARNING: Failed to create "
575 "BUILTIN\\Administrators group! Can "
576 "Winbind allocate gids?\n"));
580 /* Deal with the BUILTIN\Users group. If the SID can
581 be resolved then assume that the add_aliasmem( S-1-5-32 )
582 handled it. */
584 status = pdb_get_aliasinfo(&global_sid_Builtin_Users, info);
585 if (!NT_STATUS_IS_OK(status)) {
587 become_root();
588 if (!secrets_fetch_domain_sid(lp_workgroup(), &dom_sid)) {
589 status = NT_STATUS_OK;
590 DEBUG(3, ("Failed to fetch domain sid for %s\n",
591 lp_workgroup()));
592 } else {
593 status = create_builtin_users(&dom_sid);
595 unbecome_root();
597 if (!NT_STATUS_EQUAL(status, NT_STATUS_PROTOCOL_UNREACHABLE) &&
598 !NT_STATUS_IS_OK(status))
600 DEBUG(2, ("WARNING: Failed to create BUILTIN\\Users group! "
601 "Can Winbind allocate gids?\n"));
605 TALLOC_FREE(info);
607 /* Deal with local groups */
609 if (lp_winbind_nested_groups()) {
611 become_root();
613 /* Now add the aliases. First the one from our local SAM */
615 status = add_aliases(get_global_sam_sid(), result);
617 if (!NT_STATUS_IS_OK(status)) {
618 unbecome_root();
619 return status;
622 /* Finally the builtin ones */
624 status = add_aliases(&global_sid_Builtin, result);
626 if (!NT_STATUS_IS_OK(status)) {
627 unbecome_root();
628 return status;
631 unbecome_root();
634 /* Add privileges based on current user sids */
636 get_privileges_for_sids(&result->privilege_mask, result->sids,
637 result->num_sids);
639 return NT_STATUS_OK;
642 /****************************************************************************
643 prints a UNIX 'token' to debug output.
644 ****************************************************************************/
646 void debug_unix_user_token(int dbg_class, int dbg_lev, uid_t uid, gid_t gid,
647 int n_groups, gid_t *groups)
649 int i;
650 DEBUGC(dbg_class, dbg_lev,
651 ("UNIX token of user %ld\n", (long int)uid));
653 DEBUGADDC(dbg_class, dbg_lev,
654 ("Primary group is %ld and contains %i supplementary "
655 "groups\n", (long int)gid, n_groups));
656 for (i = 0; i < n_groups; i++)
657 DEBUGADDC(dbg_class, dbg_lev, ("Group[%3i]: %ld\n", i,
658 (long int)groups[i]));
662 * Create an artificial NT token given just a domain SID.
664 * We have 3 cases:
666 * unmapped unix users: Go directly to nss to find the user's group.
668 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
670 * If the user is provided by winbind, the primary gid is set to "domain
671 * users" of the user's domain. For an explanation why this is necessary, see
672 * the thread starting at
673 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
676 static NTSTATUS create_token_from_sid(TALLOC_CTX *mem_ctx,
677 const struct dom_sid *user_sid,
678 bool is_guest,
679 uid_t *uid, gid_t *gid,
680 char **found_username,
681 struct security_token **token)
683 NTSTATUS result = NT_STATUS_NO_SUCH_USER;
684 TALLOC_CTX *tmp_ctx = talloc_stackframe();
685 gid_t *gids;
686 struct dom_sid *group_sids;
687 struct dom_sid tmp_sid;
688 uint32_t num_group_sids;
689 uint32_t num_gids;
690 uint32_t i;
691 uint32_t high, low;
692 bool range_ok;
694 if (sid_check_is_in_our_sam(user_sid)) {
695 bool ret;
696 uint32_t pdb_num_group_sids;
697 /* This is a passdb user, so ask passdb */
699 struct samu *sam_acct = NULL;
701 if ( !(sam_acct = samu_new( tmp_ctx )) ) {
702 result = NT_STATUS_NO_MEMORY;
703 goto done;
706 become_root();
707 ret = pdb_getsampwsid(sam_acct, user_sid);
708 unbecome_root();
710 if (!ret) {
711 DEBUG(1, ("pdb_getsampwsid(%s) failed\n",
712 sid_string_dbg(user_sid)));
713 DEBUGADD(1, ("Fall back to unix user\n"));
714 goto unix_user;
717 result = pdb_enum_group_memberships(tmp_ctx, sam_acct,
718 &group_sids, &gids,
719 &pdb_num_group_sids);
720 if (!NT_STATUS_IS_OK(result)) {
721 DEBUG(1, ("enum_group_memberships failed for %s: "
722 "%s\n", sid_string_dbg(user_sid),
723 nt_errstr(result)));
724 DEBUGADD(1, ("Fall back to unix uid lookup\n"));
725 goto unix_user;
727 num_group_sids = pdb_num_group_sids;
729 /* see the smb_panic() in pdb_default_enum_group_memberships */
730 SMB_ASSERT(num_group_sids > 0);
732 /* Ensure we're returning the found_username on the right context. */
733 *found_username = talloc_strdup(mem_ctx,
734 pdb_get_username(sam_acct));
736 if (*found_username == NULL) {
737 result = NT_STATUS_NO_MEMORY;
738 goto done;
742 * If the SID from lookup_name() was the guest sid, passdb knows
743 * about the mapping of guest sid to lp_guest_account()
744 * username and will return the unix_pw info for a guest
745 * user. Use it if it's there, else lookup the *uid details
746 * using Get_Pwnam_alloc(). See bug #6291 for details. JRA.
749 /* We must always assign the *uid. */
750 if (sam_acct->unix_pw == NULL) {
751 struct passwd *pwd = Get_Pwnam_alloc(sam_acct, *found_username );
752 if (!pwd) {
753 DEBUG(10, ("Get_Pwnam_alloc failed for %s\n",
754 *found_username));
755 result = NT_STATUS_NO_SUCH_USER;
756 goto done;
758 result = samu_set_unix(sam_acct, pwd );
759 if (!NT_STATUS_IS_OK(result)) {
760 DEBUG(10, ("samu_set_unix failed for %s\n",
761 *found_username));
762 result = NT_STATUS_NO_SUCH_USER;
763 goto done;
766 *uid = sam_acct->unix_pw->pw_uid;
768 } else if (sid_check_is_in_unix_users(user_sid)) {
769 uint32_t getgroups_num_group_sids;
770 /* This is a unix user not in passdb. We need to ask nss
771 * directly, without consulting passdb */
773 struct passwd *pass;
776 * This goto target is used as a fallback for the passdb
777 * case. The concrete bug report is when passdb gave us an
778 * unmapped gid.
781 unix_user:
783 if (!sid_to_uid(user_sid, uid)) {
784 DEBUG(1, ("unix_user case, sid_to_uid for %s failed\n",
785 sid_string_dbg(user_sid)));
786 result = NT_STATUS_NO_SUCH_USER;
787 goto done;
790 uid_to_unix_users_sid(*uid, &tmp_sid);
791 user_sid = &tmp_sid;
793 pass = getpwuid_alloc(tmp_ctx, *uid);
794 if (pass == NULL) {
795 DEBUG(1, ("getpwuid(%u) failed\n",
796 (unsigned int)*uid));
797 goto done;
800 if (!getgroups_unix_user(tmp_ctx, pass->pw_name, pass->pw_gid,
801 &gids, &getgroups_num_group_sids)) {
802 DEBUG(1, ("getgroups_unix_user for user %s failed\n",
803 pass->pw_name));
804 goto done;
806 num_group_sids = getgroups_num_group_sids;
808 group_sids = talloc_array(tmp_ctx, struct dom_sid, num_group_sids);
809 if (group_sids == NULL) {
810 DEBUG(1, ("talloc_array failed\n"));
811 result = NT_STATUS_NO_MEMORY;
812 goto done;
815 for (i=0; i<num_group_sids; i++) {
816 gid_to_sid(&group_sids[i], gids[i]);
819 /* In getgroups_unix_user we always set the primary gid */
820 SMB_ASSERT(num_group_sids > 0);
822 /* Ensure we're returning the found_username on the right context. */
823 *found_username = talloc_strdup(mem_ctx, pass->pw_name);
824 if (*found_username == NULL) {
825 result = NT_STATUS_NO_MEMORY;
826 goto done;
828 } else {
830 /* This user is from winbind, force the primary gid to the
831 * user's "domain users" group. Under certain circumstances
832 * (user comes from NT4), this might be a loss of
833 * information. But we can not rely on winbind getting the
834 * correct info. AD might prohibit winbind looking up that
835 * information. */
837 /* We must always assign the *uid. */
838 if (!sid_to_uid(user_sid, uid)) {
839 DEBUG(1, ("winbindd case, sid_to_uid for %s failed\n",
840 sid_string_dbg(user_sid)));
841 result = NT_STATUS_NO_SUCH_USER;
842 goto done;
845 num_group_sids = 1;
846 group_sids = talloc_array(tmp_ctx, struct dom_sid, num_group_sids);
847 if (group_sids == NULL) {
848 DEBUG(1, ("talloc_array failed\n"));
849 result = NT_STATUS_NO_MEMORY;
850 goto done;
853 gids = talloc_array(tmp_ctx, gid_t, num_group_sids);
854 if (gids == NULL) {
855 result = NT_STATUS_NO_MEMORY;
856 goto done;
859 sid_copy(&group_sids[0], user_sid);
860 sid_split_rid(&group_sids[0], NULL);
861 sid_append_rid(&group_sids[0], DOMAIN_RID_USERS);
863 if (!sid_to_gid(&group_sids[0], &gids[0])) {
864 DEBUG(1, ("sid_to_gid(%s) failed\n",
865 sid_string_dbg(&group_sids[0])));
866 goto done;
869 *found_username = NULL;
872 *gid = gids[0];
874 /* Add the "Unix Group" SID for each gid to catch mapped groups
875 and their Unix equivalent. This is to solve the backwards
876 compatibility problem of 'valid users = +ntadmin' where
877 ntadmin has been paired with "Domain Admins" in the group
878 mapping table. Otherwise smb.conf would need to be changed
879 to 'valid user = "Domain Admins"'. --jerry */
881 num_gids = num_group_sids;
882 range_ok = lp_idmap_default_range(&low, &high);
883 for ( i=0; i<num_gids; i++ ) {
884 struct dom_sid unix_group_sid;
886 /* don't pickup anything managed by Winbind */
887 if (range_ok && (gids[i] >= low) && (gids[i] <= high)) {
888 continue;
891 gid_to_unix_groups_sid(gids[i], &unix_group_sid);
893 result = add_sid_to_array_unique(tmp_ctx, &unix_group_sid,
894 &group_sids, &num_group_sids);
895 if (!NT_STATUS_IS_OK(result)) {
896 goto done;
900 /* Ensure we're creating the nt_token on the right context. */
901 *token = create_local_nt_token(mem_ctx, user_sid,
902 is_guest, num_group_sids, group_sids);
904 if (*token == NULL) {
905 result = NT_STATUS_NO_MEMORY;
906 goto done;
909 result = NT_STATUS_OK;
910 done:
911 TALLOC_FREE(tmp_ctx);
912 return result;
916 * Create an artificial NT token given just a username. (Initially intended
917 * for force user)
919 * We go through lookup_name() to avoid problems we had with 'winbind use
920 * default domain'.
922 * We have 3 cases:
924 * unmapped unix users: Go directly to nss to find the user's group.
926 * A passdb user: The list of groups is provided by pdb_enum_group_memberships.
928 * If the user is provided by winbind, the primary gid is set to "domain
929 * users" of the user's domain. For an explanation why this is necessary, see
930 * the thread starting at
931 * http://lists.samba.org/archive/samba-technical/2006-January/044803.html.
934 NTSTATUS create_token_from_username(TALLOC_CTX *mem_ctx, const char *username,
935 bool is_guest,
936 uid_t *uid, gid_t *gid,
937 char **found_username,
938 struct security_token **token)
940 NTSTATUS result = NT_STATUS_NO_SUCH_USER;
941 TALLOC_CTX *tmp_ctx = talloc_stackframe();
942 struct dom_sid user_sid;
943 enum lsa_SidType type;
945 if (!lookup_name_smbconf(tmp_ctx, username, LOOKUP_NAME_ALL,
946 NULL, NULL, &user_sid, &type)) {
947 DEBUG(1, ("lookup_name_smbconf for %s failed\n", username));
948 goto done;
951 if (type != SID_NAME_USER) {
952 DEBUG(1, ("%s is a %s, not a user\n", username,
953 sid_type_lookup(type)));
954 goto done;
957 result = create_token_from_sid(mem_ctx, &user_sid, is_guest, uid, gid, found_username, token);
959 if (!NT_STATUS_IS_OK(result)) {
960 goto done;
964 * If result == NT_STATUS_OK then
965 * we know we have a valid token. Ensure
966 * we also have a valid username to match.
969 if (*found_username == NULL) {
970 *found_username = talloc_strdup(mem_ctx, username);
971 if (*found_username == NULL) {
972 result = NT_STATUS_NO_MEMORY;
976 done:
977 TALLOC_FREE(tmp_ctx);
978 return result;
981 /***************************************************************************
982 Build upon create_token_from_sid:
984 Expensive helper function to figure out whether a user given its sid is
985 member of a particular group.
986 ***************************************************************************/
988 bool user_sid_in_group_sid(const struct dom_sid *sid, const struct dom_sid *group_sid)
990 NTSTATUS status;
991 uid_t uid;
992 gid_t gid;
993 char *found_username;
994 struct security_token *token;
995 bool result = false;
996 enum lsa_SidType type;
997 TALLOC_CTX *mem_ctx = talloc_stackframe();
999 if (!lookup_sid(mem_ctx, sid,
1000 NULL, NULL, &type)) {
1001 DEBUG(1, ("lookup_sid for %s failed\n", dom_sid_string(mem_ctx, sid)));
1002 goto done;
1005 if (type != SID_NAME_USER) {
1006 DEBUG(5, ("%s is a %s, not a user\n", dom_sid_string(mem_ctx, sid),
1007 sid_type_lookup(type)));
1008 goto done;
1011 status = create_token_from_sid(mem_ctx, sid, False,
1012 &uid, &gid, &found_username,
1013 &token);
1015 if (!NT_STATUS_IS_OK(status)) {
1016 DEBUG(10, ("could not create token for %s\n", dom_sid_string(mem_ctx, sid)));
1017 goto done;
1020 result = security_token_has_sid(token, group_sid);
1022 done:
1023 TALLOC_FREE(mem_ctx);
1024 return result;
1027 /***************************************************************************
1028 Build upon create_token_from_username:
1030 Expensive helper function to figure out whether a user given its name is
1031 member of a particular group.
1032 ***************************************************************************/
1034 bool user_in_group_sid(const char *username, const struct dom_sid *group_sid)
1036 NTSTATUS status;
1037 uid_t uid;
1038 gid_t gid;
1039 char *found_username;
1040 struct security_token *token;
1041 bool result;
1042 TALLOC_CTX *mem_ctx = talloc_stackframe();
1044 status = create_token_from_username(mem_ctx, username, False,
1045 &uid, &gid, &found_username,
1046 &token);
1048 if (!NT_STATUS_IS_OK(status)) {
1049 DEBUG(10, ("could not create token for %s\n", username));
1050 TALLOC_FREE(mem_ctx);
1051 return False;
1054 result = security_token_has_sid(token, group_sid);
1056 TALLOC_FREE(mem_ctx);
1057 return result;
1060 bool user_in_group(const char *username, const char *groupname)
1062 TALLOC_CTX *mem_ctx = talloc_stackframe();
1063 struct dom_sid group_sid;
1064 bool ret;
1066 ret = lookup_name(mem_ctx, groupname, LOOKUP_NAME_ALL,
1067 NULL, NULL, &group_sid, NULL);
1068 TALLOC_FREE(mem_ctx);
1070 if (!ret) {
1071 DEBUG(10, ("lookup_name for (%s) failed.\n", groupname));
1072 return False;
1075 return user_in_group_sid(username, &group_sid);
1078 /* END */