2 Unix SMB/CIFS implementation.
3 Authentication utility functions
4 Copyright (C) Volker Lendecke 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "lib/util_unixsids.h"
23 #include "../librpc/gen_ndr/netlogon.h"
24 #include "../libcli/security/security.h"
25 #include "rpc_client/util_netlogon.h"
26 #include "nsswitch/libwbclient/wbclient.h"
27 #include "lib/winbind_util.h"
31 #define DBGC_CLASS DBGC_AUTH
33 /***************************************************************************
34 Make a server_info struct. Free with TALLOC_FREE().
35 ***************************************************************************/
37 struct auth_serversupplied_info
*make_server_info(TALLOC_CTX
*mem_ctx
)
39 struct auth_serversupplied_info
*result
;
41 result
= talloc_zero(mem_ctx
, struct auth_serversupplied_info
);
43 DEBUG(0, ("talloc failed\n"));
47 /* Initialise the uid and gid values to something non-zero
48 which may save us from giving away root access if there
49 is a bug in allocating these fields. */
51 result
->utok
.uid
= -1;
52 result
->utok
.gid
= -1;
57 /****************************************************************************
58 inits a netr_SamInfo2 structure from an auth_serversupplied_info. sam2 must
59 already be initialized and is used as the talloc parent for its members.
60 *****************************************************************************/
62 NTSTATUS
serverinfo_to_SamInfo2(struct auth_serversupplied_info
*server_info
,
63 struct netr_SamInfo2
*sam2
)
65 struct netr_SamInfo3
*info3
= NULL
;
68 status
= copy_netr_SamInfo3(sam2
,
71 if (!NT_STATUS_IS_OK(status
)) {
75 if (server_info
->session_key
.length
) {
76 memcpy(info3
->base
.key
.key
,
77 server_info
->session_key
.data
,
78 MIN(sizeof(info3
->base
.key
.key
),
79 server_info
->session_key
.length
));
81 if (server_info
->lm_session_key
.length
) {
82 memcpy(info3
->base
.LMSessKey
.key
,
83 server_info
->lm_session_key
.data
,
84 MIN(sizeof(info3
->base
.LMSessKey
.key
),
85 server_info
->lm_session_key
.length
));
88 sam2
->base
= info3
->base
;
93 /****************************************************************************
94 inits a netr_SamInfo3 structure from an auth_serversupplied_info. sam3 must
95 already be initialized and is used as the talloc parent for its members.
96 *****************************************************************************/
98 NTSTATUS
serverinfo_to_SamInfo3(const struct auth_serversupplied_info
*server_info
,
99 struct netr_SamInfo3
*sam3
)
101 struct netr_SamInfo3
*info3
= NULL
;
104 status
= copy_netr_SamInfo3(sam3
,
107 if (!NT_STATUS_IS_OK(status
)) {
111 if (server_info
->session_key
.length
) {
112 memcpy(info3
->base
.key
.key
,
113 server_info
->session_key
.data
,
114 MIN(sizeof(info3
->base
.key
.key
),
115 server_info
->session_key
.length
));
117 if (server_info
->lm_session_key
.length
) {
118 memcpy(info3
->base
.LMSessKey
.key
,
119 server_info
->lm_session_key
.data
,
120 MIN(sizeof(info3
->base
.LMSessKey
.key
),
121 server_info
->lm_session_key
.length
));
124 sam3
->base
= info3
->base
;
132 /****************************************************************************
133 inits a netr_SamInfo6 structure from an auth_serversupplied_info. sam6 must
134 already be initialized and is used as the talloc parent for its members.
135 *****************************************************************************/
137 NTSTATUS
serverinfo_to_SamInfo6(struct auth_serversupplied_info
*server_info
,
138 struct netr_SamInfo6
*sam6
)
140 struct pdb_domain_info
*dominfo
;
141 struct netr_SamInfo3
*info3
= NULL
;
144 if ((pdb_capabilities() & PDB_CAP_ADS
) == 0) {
145 DEBUG(10,("Not adding validation info level 6 "
146 "without ADS passdb backend\n"));
147 return NT_STATUS_INVALID_INFO_CLASS
;
150 dominfo
= pdb_get_domain_info(sam6
);
151 if (dominfo
== NULL
) {
152 return NT_STATUS_NO_MEMORY
;
155 status
= copy_netr_SamInfo3(sam6
,
158 if (!NT_STATUS_IS_OK(status
)) {
162 if (server_info
->session_key
.length
) {
163 memcpy(info3
->base
.key
.key
,
164 server_info
->session_key
.data
,
165 MIN(sizeof(info3
->base
.key
.key
),
166 server_info
->session_key
.length
));
168 if (server_info
->lm_session_key
.length
) {
169 memcpy(info3
->base
.LMSessKey
.key
,
170 server_info
->lm_session_key
.data
,
171 MIN(sizeof(info3
->base
.LMSessKey
.key
),
172 server_info
->lm_session_key
.length
));
175 sam6
->base
= info3
->base
;
180 sam6
->dns_domainname
.string
= talloc_strdup(sam6
, dominfo
->dns_domain
);
181 if (sam6
->dns_domainname
.string
== NULL
) {
182 return NT_STATUS_NO_MEMORY
;
185 sam6
->principal_name
.string
= talloc_asprintf(
186 sam6
, "%s@%s", sam6
->base
.account_name
.string
,
187 sam6
->dns_domainname
.string
);
188 if (sam6
->principal_name
.string
== NULL
) {
189 return NT_STATUS_NO_MEMORY
;
195 static NTSTATUS
append_netr_SidAttr(TALLOC_CTX
*mem_ctx
,
196 struct netr_SidAttr
**sids
,
198 const struct dom_sid2
*asid
,
203 *sids
= talloc_realloc(mem_ctx
, *sids
, struct netr_SidAttr
, t
+ 1);
205 return NT_STATUS_NO_MEMORY
;
207 (*sids
)[t
].sid
= dom_sid_dup(*sids
, asid
);
208 if ((*sids
)[t
].sid
== NULL
) {
209 return NT_STATUS_NO_MEMORY
;
211 (*sids
)[t
].attributes
= attributes
;
217 /* Fills the samr_RidWithAttributeArray with the provided sids.
218 * If it happens that we have additional groups that do not belong
219 * to the domain, add their sids as extra sids */
220 static NTSTATUS
group_sids_to_info3(struct netr_SamInfo3
*info3
,
221 const struct dom_sid
*sids
,
224 uint32_t attributes
= SE_GROUP_DEFAULT_FLAGS
;
225 struct samr_RidWithAttributeArray
*groups
;
226 struct dom_sid
*domain_sid
;
232 domain_sid
= info3
->base
.domain_sid
;
233 groups
= &info3
->base
.groups
;
235 groups
->rids
= talloc_array(info3
,
236 struct samr_RidWithAttribute
, num_sids
);
238 return NT_STATUS_NO_MEMORY
;
241 for (i
= 0; i
< num_sids
; i
++) {
242 ok
= sid_peek_check_rid(domain_sid
, &sids
[i
], &rid
);
244 /* store domain group rid */
245 groups
->rids
[groups
->count
].rid
= rid
;
246 groups
->rids
[groups
->count
].attributes
= attributes
;
251 /* if this wasn't a domain sid, add it as extra sid */
252 status
= append_netr_SidAttr(info3
, &info3
->sids
,
254 &sids
[i
], attributes
);
255 if (!NT_STATUS_IS_OK(status
)) {
264 * Merge resource SIDs, if any, into the passed in info3 structure.
267 static NTSTATUS
merge_resource_sids(const struct PAC_LOGON_INFO
*logon_info
,
268 struct netr_SamInfo3
*info3
)
271 const struct PAC_DOMAIN_GROUP_MEMBERSHIP
*rg
= NULL
;
273 if (logon_info
->info3
.base
.user_flags
& NETLOGON_RESOURCE_GROUPS
) {
274 rg
= &logon_info
->resource_groups
;
281 if (rg
->domain_sid
== NULL
) {
282 DEBUG(10, ("Missing Resource Group Domain SID\n"));
283 return NT_STATUS_INVALID_PARAMETER
;
286 /* The IDL layer would be a better place to check this, but to
287 * guard the integer addition below, we double-check */
288 if (rg
->groups
.count
> 65535) {
289 DEBUG(10, ("Too much Resource Group RIDs %u\n",
290 (unsigned)rg
->groups
.count
));
291 return NT_STATUS_INVALID_PARAMETER
;
295 * If there are any resource groups (SID Compression) add
296 * them to the extra sids portion of the info3 in the PAC.
298 * This makes the info3 look like it would if we got the info
299 * from the DC rather than the PAC.
303 * Construct a SID for each RID in the list and then append it
306 for (i
= 0; i
< rg
->groups
.count
; i
++) {
308 struct dom_sid new_sid
;
309 uint32_t attributes
= rg
->groups
.rids
[i
].attributes
;
310 struct dom_sid_buf buf
;
312 sid_compose(&new_sid
,
314 rg
->groups
.rids
[i
].rid
);
316 DEBUG(10, ("Adding SID %s to extra SIDS\n",
317 dom_sid_str_buf(&new_sid
, &buf
)));
319 status
= append_netr_SidAttr(info3
, &info3
->sids
,
323 if (!NT_STATUS_IS_OK(status
)) {
324 DEBUG(1, ("failed to append SID %s to extra SIDS: %s\n",
325 dom_sid_str_buf(&new_sid
, &buf
),
335 * Create a copy of an info3 struct from the struct PAC_LOGON_INFO,
336 * then merge resource SIDs, if any, into it. If successful return
337 * the created info3 struct.
340 NTSTATUS
create_info3_from_pac_logon_info(TALLOC_CTX
*mem_ctx
,
341 const struct PAC_LOGON_INFO
*logon_info
,
342 struct netr_SamInfo3
**pp_info3
)
345 struct netr_SamInfo3
*info3
= NULL
;
347 status
= copy_netr_SamInfo3(mem_ctx
,
350 if (!NT_STATUS_IS_OK(status
)) {
354 status
= merge_resource_sids(logon_info
, info3
);
355 if (!NT_STATUS_IS_OK(status
)) {
364 * Create a copy of an info6 struct from the PAC_UPN_DNS_INFO and PAC_LOGON_INFO
365 * then merge resource SIDs, if any, into it. If successful return the created
368 NTSTATUS
create_info6_from_pac(TALLOC_CTX
*mem_ctx
,
369 const struct PAC_LOGON_INFO
*logon_info
,
370 const struct PAC_UPN_DNS_INFO
*upn_dns_info
,
371 struct netr_SamInfo6
**pp_info6
)
374 struct netr_SamInfo6
*info6
= NULL
;
375 struct netr_SamInfo3
*info3
= NULL
;
377 info6
= talloc_zero(mem_ctx
, struct netr_SamInfo6
);
379 return NT_STATUS_NO_MEMORY
;
382 status
= copy_netr_SamInfo3(info6
,
385 if (!NT_STATUS_IS_OK(status
)) {
390 status
= merge_resource_sids(logon_info
, info3
);
391 if (!NT_STATUS_IS_OK(status
)) {
396 info6
->base
= info3
->base
;
397 info6
->sids
= info3
->sids
;
398 info6
->sidcount
= info3
->sidcount
;
400 if (upn_dns_info
!= NULL
) {
401 info6
->dns_domainname
.string
= talloc_strdup(info6
,
402 upn_dns_info
->dns_domain_name
);
403 if (info6
->dns_domainname
.string
== NULL
) {
405 return NT_STATUS_NO_MEMORY
;
407 info6
->principal_name
.string
= talloc_strdup(info6
,
408 upn_dns_info
->upn_name
);
409 if (info6
->principal_name
.string
== NULL
) {
411 return NT_STATUS_NO_MEMORY
;
420 * Check if this is a "Unix Users" domain user, or a
421 * "Unix Groups" domain group, we need to handle it
422 * in a special way if that's the case.
425 static NTSTATUS
SamInfo3_handle_sids(const char *username
,
426 const struct dom_sid
*user_sid
,
427 const struct dom_sid
*group_sid
,
428 struct netr_SamInfo3
*info3
,
429 struct dom_sid
*domain_sid
,
430 struct extra_auth_info
*extra
)
432 struct dom_sid_buf buf
;
434 if (sid_check_is_in_unix_users(user_sid
)) {
435 /* in info3 you can only set rids for the user and the
436 * primary group, and the domain sid must be that of
439 * Store a completely bogus value here.
440 * The real SID is stored in the extra sids.
441 * Other code will know to look there if (-1) is found
443 info3
->base
.rid
= (uint32_t)(-1);
444 sid_copy(&extra
->user_sid
, user_sid
);
446 DEBUG(10, ("Unix User found. Rid marked as "
447 "special and sid (%s) saved as extra sid\n",
448 dom_sid_str_buf(user_sid
, &buf
)));
450 sid_copy(domain_sid
, user_sid
);
451 sid_split_rid(domain_sid
, &info3
->base
.rid
);
454 if (is_null_sid(domain_sid
)) {
455 sid_copy(domain_sid
, get_global_sam_sid());
458 /* check if this is a "Unix Groups" domain group,
459 * if so we need special handling */
460 if (sid_check_is_in_unix_groups(group_sid
)) {
461 /* in info3 you can only set rids for the user and the
462 * primary group, and the domain sid must be that of
465 * Store a completely bogus value here.
466 * The real SID is stored in the extra sids.
467 * Other code will know to look there if (-1) is found
469 info3
->base
.primary_gid
= (uint32_t)(-1);
470 sid_copy(&extra
->pgid_sid
, group_sid
);
472 DEBUG(10, ("Unix Group found. Rid marked as "
473 "special and sid (%s) saved as extra sid\n",
474 dom_sid_str_buf(group_sid
, &buf
)));
476 bool ok
= sid_peek_check_rid(domain_sid
, group_sid
,
477 &info3
->base
.primary_gid
);
479 struct dom_sid_buf buf2
, buf3
;
480 DEBUG(1, ("The primary group domain sid(%s) does not "
481 "match the domain sid(%s) for %s(%s)\n",
482 dom_sid_str_buf(group_sid
, &buf
),
483 dom_sid_str_buf(domain_sid
, &buf2
),
485 dom_sid_str_buf(user_sid
, &buf3
)));
486 return NT_STATUS_INVALID_SID
;
492 #define RET_NOMEM(ptr) do { \
494 TALLOC_FREE(info3); \
495 return NT_STATUS_NO_MEMORY; \
498 NTSTATUS
samu_to_SamInfo3(TALLOC_CTX
*mem_ctx
,
500 const char *login_server
,
501 struct netr_SamInfo3
**_info3
,
502 struct extra_auth_info
*extra
)
504 struct netr_SamInfo3
*info3
;
505 const struct dom_sid
*user_sid
;
506 const struct dom_sid
*group_sid
;
507 struct dom_sid domain_sid
= {0};
508 struct dom_sid
*group_sids
;
509 uint32_t num_group_sids
= 0;
514 user_sid
= pdb_get_user_sid(samu
);
515 group_sid
= pdb_get_group_sid(samu
);
517 if (!user_sid
|| !group_sid
) {
518 DEBUG(1, ("Sam account is missing sids!\n"));
519 return NT_STATUS_UNSUCCESSFUL
;
522 info3
= talloc_zero(mem_ctx
, struct netr_SamInfo3
);
524 return NT_STATUS_NO_MEMORY
;
527 status
= SamInfo3_handle_sids(pdb_get_username(samu
),
534 if (!NT_STATUS_IS_OK(status
)) {
539 unix_to_nt_time(&info3
->base
.logon_time
, pdb_get_logon_time(samu
));
540 unix_to_nt_time(&info3
->base
.logoff_time
, get_time_t_max());
541 unix_to_nt_time(&info3
->base
.kickoff_time
, get_time_t_max());
542 unix_to_nt_time(&info3
->base
.last_password_change
,
543 pdb_get_pass_last_set_time(samu
));
544 unix_to_nt_time(&info3
->base
.allow_password_change
,
545 pdb_get_pass_can_change_time(samu
));
546 unix_to_nt_time(&info3
->base
.force_password_change
,
547 pdb_get_pass_must_change_time(samu
));
549 tmp
= pdb_get_username(samu
);
551 info3
->base
.account_name
.string
= talloc_strdup(info3
, tmp
);
552 RET_NOMEM(info3
->base
.account_name
.string
);
554 tmp
= pdb_get_fullname(samu
);
556 info3
->base
.full_name
.string
= talloc_strdup(info3
, tmp
);
557 RET_NOMEM(info3
->base
.full_name
.string
);
559 tmp
= pdb_get_logon_script(samu
);
561 info3
->base
.logon_script
.string
= talloc_strdup(info3
, tmp
);
562 RET_NOMEM(info3
->base
.logon_script
.string
);
564 tmp
= pdb_get_profile_path(samu
);
566 info3
->base
.profile_path
.string
= talloc_strdup(info3
, tmp
);
567 RET_NOMEM(info3
->base
.profile_path
.string
);
569 tmp
= pdb_get_homedir(samu
);
571 info3
->base
.home_directory
.string
= talloc_strdup(info3
, tmp
);
572 RET_NOMEM(info3
->base
.home_directory
.string
);
574 tmp
= pdb_get_dir_drive(samu
);
576 info3
->base
.home_drive
.string
= talloc_strdup(info3
, tmp
);
577 RET_NOMEM(info3
->base
.home_drive
.string
);
580 info3
->base
.logon_count
= pdb_get_logon_count(samu
);
581 info3
->base
.bad_password_count
= pdb_get_bad_password_count(samu
);
583 info3
->base
.logon_domain
.string
= talloc_strdup(info3
,
584 pdb_get_domain(samu
));
585 RET_NOMEM(info3
->base
.logon_domain
.string
);
587 info3
->base
.domain_sid
= dom_sid_dup(info3
, &domain_sid
);
588 RET_NOMEM(info3
->base
.domain_sid
);
590 status
= pdb_enum_group_memberships(mem_ctx
, samu
,
593 if (!NT_STATUS_IS_OK(status
)) {
594 DEBUG(1, ("Failed to get groups from sam account.\n"));
599 if (num_group_sids
) {
600 status
= group_sids_to_info3(info3
, group_sids
, num_group_sids
);
601 if (!NT_STATUS_IS_OK(status
)) {
607 /* We don't need sids and gids after the conversion */
608 TALLOC_FREE(group_sids
);
612 /* FIXME: should we add other flags ? */
613 info3
->base
.user_flags
= NETLOGON_EXTRA_SIDS
;
616 info3
->base
.logon_server
.string
= talloc_strdup(info3
, login_server
);
617 RET_NOMEM(info3
->base
.logon_server
.string
);
620 info3
->base
.acct_flags
= pdb_get_acct_ctrl(samu
);
626 NTSTATUS
passwd_to_SamInfo3(TALLOC_CTX
*mem_ctx
,
627 const char *unix_username
,
628 const struct passwd
*pwd
,
629 struct netr_SamInfo3
**pinfo3
,
630 struct extra_auth_info
*extra
)
632 struct netr_SamInfo3
*info3
;
635 const char *domain_name
= NULL
;
636 const char *user_name
= NULL
;
637 struct dom_sid domain_sid
;
638 struct dom_sid user_sid
;
639 struct dom_sid group_sid
;
640 enum lsa_SidType type
;
641 uint32_t num_sids
= 0;
642 struct dom_sid
*user_sids
= NULL
;
646 tmp_ctx
= talloc_stackframe();
648 ok
= lookup_name_smbconf(tmp_ctx
,
656 status
= NT_STATUS_NO_SUCH_USER
;
660 if (type
!= SID_NAME_USER
) {
661 status
= NT_STATUS_NO_SUCH_USER
;
665 ok
= winbind_lookup_usersids(tmp_ctx
,
669 /* Check if winbind is running */
672 * Winbind is running and the first element of the user_sids
673 * is the primary group.
676 group_sid
= user_sids
[0];
680 * Winbind is not running, try to create the group_sid from the
685 * This can lead to a primary group of S-1-22-2-XX which
686 * will be rejected by other Samba code.
688 gid_to_sid(&group_sid
, pwd
->pw_gid
);
692 * If we are a unix group, or a wellknown/builtin alias,
693 * set the group_sid to the
694 * 'Domain Users' RID of 513 which will always resolve to a
697 if (sid_check_is_in_unix_groups(&group_sid
) ||
698 sid_check_is_in_builtin(&group_sid
) ||
699 sid_check_is_in_wellknown_domain(&group_sid
)) {
700 if (sid_check_is_in_unix_users(&user_sid
)) {
701 sid_compose(&group_sid
,
702 get_global_sam_sid(),
705 sid_copy(&domain_sid
, &user_sid
);
706 sid_split_rid(&domain_sid
, NULL
);
707 sid_compose(&group_sid
,
713 /* Make sure we have a valid group sid */
714 is_null
= is_null_sid(&group_sid
);
716 status
= NT_STATUS_NO_SUCH_USER
;
720 /* Construct a netr_SamInfo3 from the information we have */
721 info3
= talloc_zero(tmp_ctx
, struct netr_SamInfo3
);
723 status
= NT_STATUS_NO_MEMORY
;
727 info3
->base
.account_name
.string
= talloc_strdup(info3
, unix_username
);
728 if (info3
->base
.account_name
.string
== NULL
) {
729 status
= NT_STATUS_NO_MEMORY
;
733 info3
->base
.logon_domain
.string
= talloc_strdup(info3
, domain_name
);
734 if (info3
->base
.logon_domain
.string
== NULL
) {
735 status
= NT_STATUS_NO_MEMORY
;
739 ZERO_STRUCT(domain_sid
);
741 status
= SamInfo3_handle_sids(unix_username
,
748 if (!NT_STATUS_IS_OK(status
)) {
752 info3
->base
.domain_sid
= dom_sid_dup(info3
, &domain_sid
);
753 if (info3
->base
.domain_sid
== NULL
) {
754 status
= NT_STATUS_NO_MEMORY
;
758 ok
= sid_peek_check_rid(&domain_sid
, &group_sid
,
759 &info3
->base
.primary_gid
);
761 struct dom_sid_buf buf1
, buf2
, buf3
;
763 DEBUG(1, ("The primary group domain sid(%s) does not "
764 "match the domain sid(%s) for %s(%s)\n",
765 dom_sid_str_buf(&group_sid
, &buf1
),
766 dom_sid_str_buf(&domain_sid
, &buf2
),
768 dom_sid_str_buf(&user_sid
, &buf3
)));
769 status
= NT_STATUS_INVALID_SID
;
773 info3
->base
.acct_flags
= ACB_NORMAL
;
776 status
= group_sids_to_info3(info3
, user_sids
, num_sids
);
777 if (!NT_STATUS_IS_OK(status
)) {
782 *pinfo3
= talloc_move(mem_ctx
, &info3
);
784 status
= NT_STATUS_OK
;
786 talloc_free(tmp_ctx
);