2 Unix SMB/CIFS implementation.
4 Winbind daemon - pam auth funcions
6 Copyright (C) Andrew Tridgell 2000
7 Copyright (C) Tim Potter 2001
8 Copyright (C) Andrew Bartlett 2001-2002
9 Copyright (C) Guenther Deschner 2005
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/>.
27 #include "libsmb/namequery.h"
28 #include "../libcli/auth/libcli_auth.h"
29 #include "../librpc/gen_ndr/ndr_samr_c.h"
30 #include "rpc_client/cli_pipe.h"
31 #include "rpc_client/cli_samr.h"
32 #include "../librpc/gen_ndr/ndr_netlogon.h"
33 #include "rpc_client/cli_netlogon.h"
35 #include "../lib/crypto/arcfour.h"
36 #include "../libcli/security/security.h"
38 #include "../librpc/gen_ndr/krb5pac.h"
39 #include "passdb/machine_sid.h"
41 #include "../lib/tsocket/tsocket.h"
42 #include "auth/kerberos/pac_utils.h"
43 #include "auth/gensec/gensec.h"
44 #include "librpc/crypto/gse_krb5.h"
45 #include "lib/afs/afs_funcs.h"
46 #include "libsmb/samlogon_cache.h"
47 #include "rpc_client/util_netlogon.h"
50 #define DBGC_CLASS DBGC_WINBIND
52 #define LOGON_KRB5_FAIL_CLOCK_SKEW 0x02000000
54 static NTSTATUS
append_info3_as_txt(TALLOC_CTX
*mem_ctx
,
55 struct winbindd_response
*resp
,
56 uint16_t validation_level
,
57 union netr_Validation
*validation
)
59 struct netr_SamInfo3
*info3
= NULL
;
62 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
63 TALLOC_CTX
*frame
= talloc_stackframe();
65 status
= map_validation_to_info3(frame
,
69 if (!NT_STATUS_IS_OK(status
)) {
73 resp
->data
.auth
.info3
.logon_time
=
74 nt_time_to_unix(info3
->base
.logon_time
);
75 resp
->data
.auth
.info3
.logoff_time
=
76 nt_time_to_unix(info3
->base
.logoff_time
);
77 resp
->data
.auth
.info3
.kickoff_time
=
78 nt_time_to_unix(info3
->base
.kickoff_time
);
79 resp
->data
.auth
.info3
.pass_last_set_time
=
80 nt_time_to_unix(info3
->base
.last_password_change
);
81 resp
->data
.auth
.info3
.pass_can_change_time
=
82 nt_time_to_unix(info3
->base
.allow_password_change
);
83 resp
->data
.auth
.info3
.pass_must_change_time
=
84 nt_time_to_unix(info3
->base
.force_password_change
);
86 resp
->data
.auth
.info3
.logon_count
= info3
->base
.logon_count
;
87 resp
->data
.auth
.info3
.bad_pw_count
= info3
->base
.bad_password_count
;
89 resp
->data
.auth
.info3
.user_rid
= info3
->base
.rid
;
90 resp
->data
.auth
.info3
.group_rid
= info3
->base
.primary_gid
;
91 sid_to_fstring(resp
->data
.auth
.info3
.dom_sid
, info3
->base
.domain_sid
);
93 resp
->data
.auth
.info3
.num_groups
= info3
->base
.groups
.count
;
94 resp
->data
.auth
.info3
.user_flgs
= info3
->base
.user_flags
;
96 resp
->data
.auth
.info3
.acct_flags
= info3
->base
.acct_flags
;
97 resp
->data
.auth
.info3
.num_other_sids
= info3
->sidcount
;
99 fstrcpy(resp
->data
.auth
.info3
.user_name
,
100 info3
->base
.account_name
.string
);
101 fstrcpy(resp
->data
.auth
.info3
.full_name
,
102 info3
->base
.full_name
.string
);
103 fstrcpy(resp
->data
.auth
.info3
.logon_script
,
104 info3
->base
.logon_script
.string
);
105 fstrcpy(resp
->data
.auth
.info3
.profile_path
,
106 info3
->base
.profile_path
.string
);
107 fstrcpy(resp
->data
.auth
.info3
.home_dir
,
108 info3
->base
.home_directory
.string
);
109 fstrcpy(resp
->data
.auth
.info3
.dir_drive
,
110 info3
->base
.home_drive
.string
);
112 fstrcpy(resp
->data
.auth
.info3
.logon_srv
,
113 info3
->base
.logon_server
.string
);
114 fstrcpy(resp
->data
.auth
.info3
.logon_dom
,
115 info3
->base
.logon_domain
.string
);
117 resp
->data
.auth
.validation_level
= validation_level
;
118 if (validation_level
== 6) {
119 fstrcpy(resp
->data
.auth
.info6
.dns_domainname
,
120 validation
->sam6
->dns_domainname
.string
);
121 fstrcpy(resp
->data
.auth
.info6
.principal_name
,
122 validation
->sam6
->principal_name
.string
);
125 ex
= talloc_strdup(frame
, "");
127 status
= NT_STATUS_NO_MEMORY
;
131 for (i
=0; i
< info3
->base
.groups
.count
; i
++) {
132 ex
= talloc_asprintf_append_buffer(ex
, "0x%08X:0x%08X\n",
133 info3
->base
.groups
.rids
[i
].rid
,
134 info3
->base
.groups
.rids
[i
].attributes
);
136 status
= NT_STATUS_NO_MEMORY
;
141 for (i
=0; i
< info3
->sidcount
; i
++) {
144 sid
= dom_sid_string(frame
, info3
->sids
[i
].sid
);
146 status
= NT_STATUS_NO_MEMORY
;
150 ex
= talloc_asprintf_append_buffer(ex
, "%s:0x%08X\n",
152 info3
->sids
[i
].attributes
);
154 status
= NT_STATUS_NO_MEMORY
;
159 resp
->length
+= talloc_get_size(ex
);
160 resp
->extra_data
.data
= talloc_move(mem_ctx
, &ex
);
162 status
= NT_STATUS_OK
;
168 static NTSTATUS
append_info3_as_ndr(TALLOC_CTX
*mem_ctx
,
169 struct winbindd_response
*resp
,
170 struct netr_SamInfo3
*info3
)
173 enum ndr_err_code ndr_err
;
175 ndr_err
= ndr_push_struct_blob(&blob
, mem_ctx
, info3
,
176 (ndr_push_flags_fn_t
)ndr_push_netr_SamInfo3
);
177 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
178 DEBUG(0,("append_info3_as_ndr: failed to append\n"));
179 return ndr_map_error2ntstatus(ndr_err
);
182 resp
->extra_data
.data
= blob
.data
;
183 resp
->length
+= blob
.length
;
188 static NTSTATUS
append_unix_username(TALLOC_CTX
*mem_ctx
,
189 struct winbindd_response
*resp
,
190 const struct netr_SamInfo3
*info3
,
191 const char *name_domain
,
192 const char *name_user
)
194 /* We've been asked to return the unix username, per
195 'winbind use default domain' settings and the like */
197 const char *nt_username
, *nt_domain
, *unix_username
;
199 nt_domain
= talloc_strdup(mem_ctx
, info3
->base
.logon_domain
.string
);
201 /* If the server didn't give us one, just use the one
203 nt_domain
= name_domain
;
206 nt_username
= talloc_strdup(mem_ctx
, info3
->base
.account_name
.string
);
208 /* If the server didn't give us one, just use the one
210 nt_username
= name_user
;
213 unix_username
= fill_domain_username_talloc(mem_ctx
,
217 if (unix_username
== NULL
) {
218 return NT_STATUS_NO_MEMORY
;
221 fstrcpy(resp
->data
.auth
.unix_username
, unix_username
);
223 DEBUG(5, ("Setting unix username to [%s]\n",
224 resp
->data
.auth
.unix_username
));
229 static NTSTATUS
append_afs_token(TALLOC_CTX
*mem_ctx
,
230 struct winbindd_response
*resp
,
231 const struct netr_SamInfo3
*info3
,
232 const char *name_domain
,
233 const char *name_user
)
235 char *afsname
= NULL
;
239 afsname
= talloc_strdup(mem_ctx
, lp_afs_username_map());
240 if (afsname
== NULL
) {
241 return NT_STATUS_NO_MEMORY
;
244 afsname
= talloc_string_sub(mem_ctx
,
245 lp_afs_username_map(),
247 afsname
= talloc_string_sub(mem_ctx
, afsname
,
249 afsname
= talloc_string_sub(mem_ctx
, afsname
,
253 struct dom_sid user_sid
;
256 sid_compose(&user_sid
, info3
->base
.domain_sid
,
258 sid_to_fstring(sidstr
, &user_sid
);
259 afsname
= talloc_string_sub(mem_ctx
, afsname
,
263 if (afsname
== NULL
) {
264 return NT_STATUS_NO_MEMORY
;
267 if (!strlower_m(afsname
)) {
268 return NT_STATUS_INVALID_PARAMETER
;
271 DEBUG(10, ("Generating token for user %s\n", afsname
));
273 cell
= strchr(afsname
, '@');
276 return NT_STATUS_NO_MEMORY
;
282 token
= afs_createtoken_str(afsname
, cell
);
286 resp
->extra_data
.data
= talloc_strdup(mem_ctx
, token
);
287 if (resp
->extra_data
.data
== NULL
) {
288 return NT_STATUS_NO_MEMORY
;
290 resp
->length
+= strlen((const char *)resp
->extra_data
.data
)+1;
295 static NTSTATUS
check_info3_in_group(struct netr_SamInfo3
*info3
,
296 const char *group_sid
)
298 * Check whether a user belongs to a group or list of groups.
300 * @param mem_ctx talloc memory context.
301 * @param info3 user information, including group membership info.
302 * @param group_sid One or more groups , separated by commas.
304 * @return NT_STATUS_OK on success,
305 * NT_STATUS_LOGON_FAILURE if the user does not belong,
306 * or other NT_STATUS_IS_ERR(status) for other kinds of failure.
309 struct dom_sid
*require_membership_of_sid
;
310 uint32_t num_require_membership_of_sid
;
315 struct security_token
*token
;
316 TALLOC_CTX
*frame
= talloc_stackframe();
319 /* Parse the 'required group' SID */
321 if (!group_sid
|| !group_sid
[0]) {
322 /* NO sid supplied, all users may access */
327 token
= talloc_zero(talloc_tos(), struct security_token
);
329 DEBUG(0, ("talloc failed\n"));
331 return NT_STATUS_NO_MEMORY
;
334 num_require_membership_of_sid
= 0;
335 require_membership_of_sid
= NULL
;
339 while (next_token_talloc(talloc_tos(), &p
, &req_sid
, ",")) {
340 if (!string_to_sid(&sid
, req_sid
)) {
341 DEBUG(0, ("check_info3_in_group: could not parse %s "
342 "as a SID!", req_sid
));
344 return NT_STATUS_INVALID_PARAMETER
;
347 status
= add_sid_to_array(talloc_tos(), &sid
,
348 &require_membership_of_sid
,
349 &num_require_membership_of_sid
);
350 if (!NT_STATUS_IS_OK(status
)) {
351 DEBUG(0, ("add_sid_to_array failed\n"));
357 status
= sid_array_from_info3(talloc_tos(), info3
,
361 if (!NT_STATUS_IS_OK(status
)) {
366 if (!NT_STATUS_IS_OK(status
= add_aliases(get_global_sam_sid(),
368 || !NT_STATUS_IS_OK(status
= add_aliases(&global_sid_Builtin
,
370 DEBUG(3, ("could not add aliases: %s\n",
376 security_token_debug(DBGC_CLASS
, 10, token
);
378 for (i
=0; i
<num_require_membership_of_sid
; i
++) {
379 DEBUG(10, ("Checking SID %s\n", sid_string_dbg(
380 &require_membership_of_sid
[i
])));
381 if (nt_token_check_sid(&require_membership_of_sid
[i
],
383 DEBUG(10, ("Access ok\n"));
389 /* Do not distinguish this error from a wrong username/pw */
392 return NT_STATUS_LOGON_FAILURE
;
395 struct winbindd_domain
*find_auth_domain(uint8_t flags
,
396 const char *domain_name
)
398 struct winbindd_domain
*domain
;
401 domain
= find_domain_from_name_noinit(domain_name
);
402 if (domain
== NULL
) {
403 DEBUG(3, ("Authentication for domain [%s] refused "
404 "as it is not a trusted domain\n",
409 if (domain
->secure_channel_type
!= SEC_CHAN_NULL
) {
413 return domain
->routing_domain
;
416 if (strequal(domain_name
, get_global_sam_name())) {
417 return find_domain_from_name_noinit(domain_name
);
420 /* we can auth against trusted domains */
421 if (flags
& WBFLAG_PAM_CONTACT_TRUSTDOM
) {
422 domain
= find_domain_from_name_noinit(domain_name
);
423 if (domain
== NULL
) {
424 DEBUG(3, ("Authentication for domain [%s] skipped "
425 "as it is not a trusted domain\n",
432 return find_our_domain();
435 static void fake_password_policy(struct winbindd_response
*r
,
436 const struct netr_SamBaseInfo
*bi
)
438 NTTIME min_password_age
;
439 NTTIME max_password_age
;
441 if (bi
->allow_password_change
> bi
->last_password_change
) {
442 min_password_age
= bi
->allow_password_change
-
443 bi
->last_password_change
;
445 min_password_age
= 0;
448 if (bi
->force_password_change
> bi
->last_password_change
) {
449 max_password_age
= bi
->force_password_change
-
450 bi
->last_password_change
;
452 max_password_age
= 0;
455 r
->data
.auth
.policy
.min_length_password
= 0;
456 r
->data
.auth
.policy
.password_history
= 0;
457 r
->data
.auth
.policy
.password_properties
= 0;
458 r
->data
.auth
.policy
.expire
=
459 nt_time_to_unix_abs(&max_password_age
);
460 r
->data
.auth
.policy
.min_passwordage
=
461 nt_time_to_unix_abs(&min_password_age
);
464 static void fill_in_password_policy(struct winbindd_response
*r
,
465 const struct samr_DomInfo1
*p
)
467 r
->data
.auth
.policy
.min_length_password
=
468 p
->min_password_length
;
469 r
->data
.auth
.policy
.password_history
=
470 p
->password_history_length
;
471 r
->data
.auth
.policy
.password_properties
=
472 p
->password_properties
;
473 r
->data
.auth
.policy
.expire
=
474 nt_time_to_unix_abs((const NTTIME
*)&(p
->max_password_age
));
475 r
->data
.auth
.policy
.min_passwordage
=
476 nt_time_to_unix_abs((const NTTIME
*)&(p
->min_password_age
));
479 static NTSTATUS
fillup_password_policy(struct winbindd_domain
*domain
,
480 struct winbindd_response
*response
)
482 TALLOC_CTX
*frame
= talloc_stackframe();
484 struct samr_DomInfo1 password_policy
;
486 if ( !winbindd_can_contact_domain( domain
) ) {
487 DEBUG(5,("fillup_password_policy: No inbound trust to "
488 "contact domain %s\n", domain
->name
));
489 status
= NT_STATUS_NOT_SUPPORTED
;
493 status
= wb_cache_password_policy(domain
, talloc_tos(),
495 if (NT_STATUS_IS_ERR(status
)) {
499 fill_in_password_policy(response
, &password_policy
);
506 static NTSTATUS
get_max_bad_attempts_from_lockout_policy(struct winbindd_domain
*domain
,
508 uint16_t *lockout_threshold
)
510 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
511 struct samr_DomInfo12 lockout_policy
;
513 *lockout_threshold
= 0;
515 status
= wb_cache_lockout_policy(domain
, mem_ctx
, &lockout_policy
);
516 if (NT_STATUS_IS_ERR(status
)) {
520 *lockout_threshold
= lockout_policy
.lockout_threshold
;
525 static NTSTATUS
get_pwd_properties(struct winbindd_domain
*domain
,
527 uint32_t *password_properties
)
529 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
530 struct samr_DomInfo1 password_policy
;
532 *password_properties
= 0;
534 status
= wb_cache_password_policy(domain
, mem_ctx
, &password_policy
);
535 if (NT_STATUS_IS_ERR(status
)) {
539 *password_properties
= password_policy
.password_properties
;
546 static const char *generate_krb5_ccache(TALLOC_CTX
*mem_ctx
,
549 const char **user_ccache_file
)
551 /* accept FILE and WRFILE as krb5_cc_type from the client and then
552 * build the full ccname string based on the user's uid here -
555 const char *gen_cc
= NULL
;
558 if (strequal(type
, "FILE")) {
559 gen_cc
= talloc_asprintf(
560 mem_ctx
, "FILE:/tmp/krb5cc_%d", uid
);
562 if (strequal(type
, "WRFILE")) {
563 gen_cc
= talloc_asprintf(
564 mem_ctx
, "WRFILE:/tmp/krb5cc_%d", uid
);
566 if (strequal(type
, "KEYRING")) {
567 gen_cc
= talloc_asprintf(
568 mem_ctx
, "KEYRING:persistent:%d", uid
);
571 if (strnequal(type
, "FILE:/", 6) ||
572 strnequal(type
, "WRFILE:/", 8) ||
573 strnequal(type
, "DIR:/", 5)) {
575 /* we allow only one "%u" substitution */
579 p
= strchr(type
, '%');
584 if (p
!= NULL
&& *p
== 'u' && strchr(p
, '%') == NULL
) {
585 char uid_str
[sizeof("18446744073709551615")];
587 snprintf(uid_str
, sizeof(uid_str
), "%u", uid
);
589 gen_cc
= talloc_string_sub2(mem_ctx
,
593 /* remove_unsafe_characters */
597 /* allow_trailing_dollar */
604 *user_ccache_file
= gen_cc
;
606 if (gen_cc
== NULL
) {
607 gen_cc
= talloc_strdup(mem_ctx
, "MEMORY:winbindd_pam_ccache");
609 if (gen_cc
== NULL
) {
610 DEBUG(0,("out of memory\n"));
614 DEBUG(10, ("using ccache: %s%s\n", gen_cc
,
615 (*user_ccache_file
== NULL
) ? " (internal)":""));
622 uid_t
get_uid_from_request(struct winbindd_request
*request
)
626 uid
= request
->data
.auth
.uid
;
628 if (uid
== (uid_t
)-1) {
629 DEBUG(1,("invalid uid: '%u'\n", (unsigned int)uid
));
635 /**********************************************************************
636 Authenticate a user with a clear text password using Kerberos and fill up
638 **********************************************************************/
640 static NTSTATUS
winbindd_raw_kerberos_login(TALLOC_CTX
*mem_ctx
,
641 struct winbindd_domain
*domain
,
644 const char *krb5_cc_type
,
646 struct netr_SamInfo6
**info6
,
650 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
651 krb5_error_code krb5_ret
;
652 const char *cc
= NULL
;
653 const char *principal_s
= NULL
;
654 const char *service
= NULL
;
656 fstring name_namespace
, name_domain
, name_user
;
657 time_t ticket_lifetime
= 0;
658 time_t renewal_until
= 0;
660 time_t time_offset
= 0;
661 const char *user_ccache_file
;
662 struct PAC_LOGON_INFO
*logon_info
= NULL
;
663 struct PAC_UPN_DNS_INFO
*upn_dns_info
= NULL
;
664 struct PAC_DATA
*pac_data
= NULL
;
665 struct PAC_DATA_CTR
*pac_data_ctr
= NULL
;
666 const char *local_service
;
668 struct netr_SamInfo6
*info6_copy
= NULL
;
673 if (domain
->alt_name
== NULL
) {
674 return NT_STATUS_INVALID_PARAMETER
;
678 * prepare a krb5_cc_cache string for the user */
681 DEBUG(0,("no valid uid\n"));
684 cc
= generate_krb5_ccache(mem_ctx
,
689 return NT_STATUS_NO_MEMORY
;
694 * get kerberos properties */
696 if (domain
->private_data
) {
697 ads
= (ADS_STRUCT
*)domain
->private_data
;
698 time_offset
= ads
->auth
.time_offset
;
703 * do kerberos auth and setup ccache as the user */
705 ok
= parse_domain_user(user
, name_namespace
, name_domain
, name_user
);
707 return NT_STATUS_INVALID_PARAMETER
;
710 realm
= talloc_strdup(mem_ctx
, domain
->alt_name
);
712 return NT_STATUS_NO_MEMORY
;
715 if (!strupper_m(realm
)) {
716 return NT_STATUS_INVALID_PARAMETER
;
719 principal_s
= talloc_asprintf(mem_ctx
, "%s@%s", name_user
, realm
);
720 if (principal_s
== NULL
) {
721 return NT_STATUS_NO_MEMORY
;
724 service
= talloc_asprintf(mem_ctx
, "%s/%s@%s", KRB5_TGS_NAME
, realm
, realm
);
725 if (service
== NULL
) {
726 return NT_STATUS_NO_MEMORY
;
729 local_service
= talloc_asprintf(mem_ctx
, "%s$@%s",
730 lp_netbios_name(), lp_realm());
731 if (local_service
== NULL
) {
732 return NT_STATUS_NO_MEMORY
;
736 /* if this is a user ccache, we need to act as the user to let the krb5
737 * library handle the chown, etc. */
739 /************************ ENTERING NON-ROOT **********************/
741 if (user_ccache_file
!= NULL
) {
742 set_effective_uid(uid
);
743 DEBUG(10,("winbindd_raw_kerberos_login: uid is %d\n", uid
));
746 result
= kerberos_return_pac(mem_ctx
,
755 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
759 if (user_ccache_file
!= NULL
) {
760 gain_root_privilege();
763 /************************ RETURNED TO ROOT **********************/
765 if (!NT_STATUS_IS_OK(result
)) {
769 if (pac_data_ctr
== NULL
) {
773 pac_data
= pac_data_ctr
->pac_data
;
774 if (pac_data
== NULL
) {
778 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
780 if (pac_data
->buffers
[i
].type
== PAC_TYPE_LOGON_INFO
) {
781 logon_info
= pac_data
->buffers
[i
].info
->logon_info
.info
;
785 if (pac_data
->buffers
[i
].type
== PAC_TYPE_UPN_DNS_INFO
) {
786 upn_dns_info
= &pac_data
->buffers
[i
].info
->upn_dns_info
;
791 if (logon_info
== NULL
) {
792 DEBUG(10,("Missing logon_info in ticket of %s\n",
794 return NT_STATUS_INVALID_PARAMETER
;
797 DEBUG(10,("winbindd_raw_kerberos_login: winbindd validated ticket of %s\n",
800 result
= create_info6_from_pac(mem_ctx
, logon_info
,
801 upn_dns_info
, &info6_copy
);
802 if (!NT_STATUS_IS_OK(result
)) {
806 /* if we had a user's ccache then return that string for the pam
809 if (user_ccache_file
!= NULL
) {
811 fstrcpy(krb5ccname
, user_ccache_file
);
813 result
= add_ccache_to_list(principal_s
,
825 if (!NT_STATUS_IS_OK(result
)) {
826 DEBUG(10,("winbindd_raw_kerberos_login: failed to add ccache to list: %s\n",
831 /* need to delete the memory cred cache, it is not used anymore */
833 krb5_ret
= ads_kdestroy(cc
);
835 DEBUG(3,("winbindd_raw_kerberos_login: "
836 "could not destroy krb5 credential cache: "
837 "%s\n", error_message(krb5_ret
)));
846 * Do not delete an existing valid credential cache, if the user
847 * e.g. enters a wrong password
849 if ((strequal(krb5_cc_type
, "FILE") || strequal(krb5_cc_type
, "WRFILE"))
850 && user_ccache_file
!= NULL
) {
854 /* we could have created a new credential cache with a valid tgt in it
855 * but we werent able to get or verify the service ticket for this
856 * local host and therefor didn't get the PAC, we need to remove that
857 * cache entirely now */
859 krb5_ret
= ads_kdestroy(cc
);
861 DEBUG(3,("winbindd_raw_kerberos_login: "
862 "could not destroy krb5 credential cache: "
863 "%s\n", error_message(krb5_ret
)));
866 if (!NT_STATUS_IS_OK(remove_ccache(user
))) {
867 DEBUG(3,("winbindd_raw_kerberos_login: "
868 "could not remove ccache for user %s\n",
874 return NT_STATUS_NOT_SUPPORTED
;
875 #endif /* HAVE_KRB5 */
878 /****************************************************************
879 ****************************************************************/
881 bool check_request_flags(uint32_t flags
)
883 uint32_t flags_edata
= WBFLAG_PAM_AFS_TOKEN
|
884 WBFLAG_PAM_INFO3_TEXT
|
885 WBFLAG_PAM_INFO3_NDR
;
887 if ( ( (flags
& flags_edata
) == WBFLAG_PAM_AFS_TOKEN
) ||
888 ( (flags
& flags_edata
) == WBFLAG_PAM_INFO3_NDR
) ||
889 ( (flags
& flags_edata
) == WBFLAG_PAM_INFO3_TEXT
)||
890 !(flags
& flags_edata
) ) {
894 DEBUG(1, ("check_request_flags: invalid request flags[0x%08X]\n",
900 /****************************************************************
901 ****************************************************************/
903 NTSTATUS
append_auth_data(TALLOC_CTX
*mem_ctx
,
904 struct winbindd_response
*resp
,
905 uint32_t request_flags
,
906 uint16_t validation_level
,
907 union netr_Validation
*validation
,
908 const char *name_domain
,
909 const char *name_user
)
911 struct netr_SamInfo3
*info3
= NULL
;
912 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
914 result
= map_validation_to_info3(talloc_tos(),
918 if (!NT_STATUS_IS_OK(result
)) {
922 if (request_flags
& WBFLAG_PAM_USER_SESSION_KEY
) {
923 memcpy(resp
->data
.auth
.user_session_key
,
925 sizeof(resp
->data
.auth
.user_session_key
)
929 if (request_flags
& WBFLAG_PAM_LMKEY
) {
930 memcpy(resp
->data
.auth
.first_8_lm_hash
,
931 info3
->base
.LMSessKey
.key
,
932 sizeof(resp
->data
.auth
.first_8_lm_hash
)
936 if (request_flags
& WBFLAG_PAM_UNIX_NAME
) {
937 result
= append_unix_username(mem_ctx
, resp
,
938 info3
, name_domain
, name_user
);
939 if (!NT_STATUS_IS_OK(result
)) {
940 DEBUG(10,("Failed to append Unix Username: %s\n",
946 /* currently, anything from here on potentially overwrites extra_data. */
948 if (request_flags
& WBFLAG_PAM_INFO3_NDR
) {
949 result
= append_info3_as_ndr(mem_ctx
, resp
, info3
);
950 if (!NT_STATUS_IS_OK(result
)) {
951 DEBUG(10,("Failed to append INFO3 (NDR): %s\n",
957 if (request_flags
& WBFLAG_PAM_INFO3_TEXT
) {
958 result
= append_info3_as_txt(mem_ctx
, resp
,
961 if (!NT_STATUS_IS_OK(result
)) {
962 DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
968 if (request_flags
& WBFLAG_PAM_AFS_TOKEN
) {
969 result
= append_afs_token(mem_ctx
, resp
,
970 info3
, name_domain
, name_user
);
971 if (!NT_STATUS_IS_OK(result
)) {
972 DEBUG(10,("Failed to append AFS token: %s\n",
978 result
= NT_STATUS_OK
;
984 static NTSTATUS
winbindd_dual_pam_auth_cached(struct winbindd_domain
*domain
,
985 struct winbindd_cli_state
*state
,
986 struct netr_SamInfo3
**info3
)
988 NTSTATUS result
= NT_STATUS_LOGON_FAILURE
;
989 uint16_t max_allowed_bad_attempts
;
990 fstring name_namespace
, name_domain
, name_user
;
992 enum lsa_SidType type
;
993 uchar new_nt_pass
[NT_HASH_LEN
];
994 const uint8_t *cached_nt_pass
;
995 const uint8_t *cached_salt
;
996 struct netr_SamInfo3
*my_info3
;
997 time_t kickoff_time
, must_change_time
;
998 bool password_good
= false;
1001 struct winbindd_tdc_domain
*tdc_domain
= NULL
;
1006 ZERO_STRUCTP(info3
);
1008 DEBUG(10,("winbindd_dual_pam_auth_cached\n"));
1010 /* Parse domain and username */
1012 ok
= parse_domain_user(state
->request
->data
.auth
.user
,
1017 DBG_DEBUG("parse_domain_user failed\n");
1018 return NT_STATUS_NO_SUCH_USER
;
1021 if (!lookup_cached_name(name_namespace
,
1026 DEBUG(10,("winbindd_dual_pam_auth_cached: no such user in the cache\n"));
1027 return NT_STATUS_NO_SUCH_USER
;
1030 if (type
!= SID_NAME_USER
) {
1031 DEBUG(10,("winbindd_dual_pam_auth_cached: not a user (%s)\n", sid_type_lookup(type
)));
1032 return NT_STATUS_LOGON_FAILURE
;
1035 result
= winbindd_get_creds(domain
,
1041 if (!NT_STATUS_IS_OK(result
)) {
1042 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get creds: %s\n", nt_errstr(result
)));
1048 E_md4hash(state
->request
->data
.auth
.pass
, new_nt_pass
);
1050 dump_data_pw("new_nt_pass", new_nt_pass
, NT_HASH_LEN
);
1051 dump_data_pw("cached_nt_pass", cached_nt_pass
, NT_HASH_LEN
);
1053 dump_data_pw("cached_salt", cached_salt
, NT_HASH_LEN
);
1057 /* In this case we didn't store the nt_hash itself,
1058 but the MD5 combination of salt + nt_hash. */
1059 uchar salted_hash
[NT_HASH_LEN
];
1060 E_md5hash(cached_salt
, new_nt_pass
, salted_hash
);
1062 password_good
= (memcmp(cached_nt_pass
, salted_hash
,
1065 /* Old cached cred - direct store of nt_hash (bad bad bad !). */
1066 password_good
= (memcmp(cached_nt_pass
, new_nt_pass
,
1070 if (password_good
) {
1072 /* User *DOES* know the password, update logon_time and reset
1075 my_info3
->base
.user_flags
|= NETLOGON_CACHED_ACCOUNT
;
1077 if (my_info3
->base
.acct_flags
& ACB_AUTOLOCK
) {
1078 return NT_STATUS_ACCOUNT_LOCKED_OUT
;
1081 if (my_info3
->base
.acct_flags
& ACB_DISABLED
) {
1082 return NT_STATUS_ACCOUNT_DISABLED
;
1085 if (my_info3
->base
.acct_flags
& ACB_WSTRUST
) {
1086 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
;
1089 if (my_info3
->base
.acct_flags
& ACB_SVRTRUST
) {
1090 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT
;
1093 if (my_info3
->base
.acct_flags
& ACB_DOMTRUST
) {
1094 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
;
1097 if (!(my_info3
->base
.acct_flags
& ACB_NORMAL
)) {
1098 DEBUG(0,("winbindd_dual_pam_auth_cached: whats wrong with that one?: 0x%08x\n",
1099 my_info3
->base
.acct_flags
));
1100 return NT_STATUS_LOGON_FAILURE
;
1103 kickoff_time
= nt_time_to_unix(my_info3
->base
.kickoff_time
);
1104 if (kickoff_time
!= 0 && time(NULL
) > kickoff_time
) {
1105 return NT_STATUS_ACCOUNT_EXPIRED
;
1108 must_change_time
= nt_time_to_unix(my_info3
->base
.force_password_change
);
1109 if (must_change_time
!= 0 && must_change_time
< time(NULL
)) {
1110 /* we allow grace logons when the password has expired */
1111 my_info3
->base
.user_flags
|= NETLOGON_GRACE_LOGON
;
1112 /* return NT_STATUS_PASSWORD_EXPIRED; */
1117 if ((state
->request
->flags
& WBFLAG_PAM_KRB5
) &&
1118 ((tdc_domain
= wcache_tdc_fetch_domain(state
->mem_ctx
, name_domain
)) != NULL
) &&
1119 ((tdc_domain
->trust_type
& LSA_TRUST_TYPE_UPLEVEL
) ||
1120 /* used to cope with the case winbindd starting without network. */
1121 !strequal(tdc_domain
->domain_name
, tdc_domain
->dns_name
))) {
1124 const char *cc
= NULL
;
1126 const char *principal_s
= NULL
;
1127 const char *service
= NULL
;
1128 const char *user_ccache_file
;
1130 if (domain
->alt_name
== NULL
) {
1131 return NT_STATUS_INVALID_PARAMETER
;
1134 uid
= get_uid_from_request(state
->request
);
1136 DEBUG(0,("winbindd_dual_pam_auth_cached: invalid uid\n"));
1137 return NT_STATUS_INVALID_PARAMETER
;
1140 cc
= generate_krb5_ccache(state
->mem_ctx
,
1141 state
->request
->data
.auth
.krb5_cc_type
,
1142 state
->request
->data
.auth
.uid
,
1145 return NT_STATUS_NO_MEMORY
;
1148 realm
= talloc_strdup(state
->mem_ctx
, domain
->alt_name
);
1149 if (realm
== NULL
) {
1150 return NT_STATUS_NO_MEMORY
;
1153 if (!strupper_m(realm
)) {
1154 return NT_STATUS_INVALID_PARAMETER
;
1157 principal_s
= talloc_asprintf(state
->mem_ctx
, "%s@%s", name_user
, realm
);
1158 if (principal_s
== NULL
) {
1159 return NT_STATUS_NO_MEMORY
;
1162 service
= talloc_asprintf(state
->mem_ctx
, "%s/%s@%s", KRB5_TGS_NAME
, realm
, realm
);
1163 if (service
== NULL
) {
1164 return NT_STATUS_NO_MEMORY
;
1167 if (user_ccache_file
!= NULL
) {
1169 fstrcpy(state
->response
->data
.auth
.krb5ccname
,
1172 result
= add_ccache_to_list(principal_s
,
1175 state
->request
->data
.auth
.user
,
1176 state
->request
->data
.auth
.pass
,
1180 time(NULL
) + lp_winbind_cache_time(),
1181 time(NULL
) + WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
1184 if (!NT_STATUS_IS_OK(result
)) {
1185 DEBUG(10,("winbindd_dual_pam_auth_cached: failed "
1186 "to add ccache to list: %s\n",
1187 nt_errstr(result
)));
1191 #endif /* HAVE_KRB5 */
1193 /* FIXME: we possibly should handle logon hours as well (does xp when
1194 * offline?) see auth/auth_sam.c:sam_account_ok for details */
1196 unix_to_nt_time(&my_info3
->base
.logon_time
, time(NULL
));
1197 my_info3
->base
.bad_password_count
= 0;
1199 result
= winbindd_update_creds_by_info3(domain
,
1200 state
->request
->data
.auth
.user
,
1201 state
->request
->data
.auth
.pass
,
1203 if (!NT_STATUS_IS_OK(result
)) {
1204 DEBUG(1,("winbindd_dual_pam_auth_cached: failed to update creds: %s\n",
1205 nt_errstr(result
)));
1209 return NT_STATUS_OK
;
1213 /* User does *NOT* know the correct password, modify info3 accordingly, but only if online */
1214 if (domain
->online
== false) {
1218 /* failure of this is not critical */
1219 result
= get_max_bad_attempts_from_lockout_policy(domain
, state
->mem_ctx
, &max_allowed_bad_attempts
);
1220 if (!NT_STATUS_IS_OK(result
)) {
1221 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get max_allowed_bad_attempts. "
1222 "Won't be able to honour account lockout policies\n"));
1225 /* increase counter */
1226 my_info3
->base
.bad_password_count
++;
1228 if (max_allowed_bad_attempts
== 0) {
1233 if (my_info3
->base
.bad_password_count
>= max_allowed_bad_attempts
) {
1235 uint32_t password_properties
;
1237 result
= get_pwd_properties(domain
, state
->mem_ctx
, &password_properties
);
1238 if (!NT_STATUS_IS_OK(result
)) {
1239 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get password properties.\n"));
1242 if ((my_info3
->base
.rid
!= DOMAIN_RID_ADMINISTRATOR
) ||
1243 (password_properties
& DOMAIN_PASSWORD_LOCKOUT_ADMINS
)) {
1244 my_info3
->base
.acct_flags
|= ACB_AUTOLOCK
;
1249 result
= winbindd_update_creds_by_info3(domain
,
1250 state
->request
->data
.auth
.user
,
1254 if (!NT_STATUS_IS_OK(result
)) {
1255 DEBUG(0,("winbindd_dual_pam_auth_cached: failed to update creds %s\n",
1256 nt_errstr(result
)));
1259 return NT_STATUS_LOGON_FAILURE
;
1262 static NTSTATUS
winbindd_dual_pam_auth_kerberos(struct winbindd_domain
*domain
,
1263 struct winbindd_cli_state
*state
,
1264 struct netr_SamInfo6
**info6
)
1266 struct winbindd_domain
*contact_domain
;
1267 fstring name_namespace
, name_domain
, name_user
;
1271 DEBUG(10,("winbindd_dual_pam_auth_kerberos\n"));
1273 /* Parse domain and username */
1275 ok
= parse_domain_user(state
->request
->data
.auth
.user
,
1280 result
= NT_STATUS_INVALID_PARAMETER
;
1284 /* what domain should we contact? */
1287 contact_domain
= find_domain_from_name(name_namespace
);
1288 if (contact_domain
== NULL
) {
1289 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1290 state
->request
->data
.auth
.user
, name_domain
, name_user
, name_domain
));
1291 result
= NT_STATUS_NO_SUCH_USER
;
1296 if (is_myname(name_domain
)) {
1297 DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain
));
1298 result
= NT_STATUS_NO_SUCH_USER
;
1302 contact_domain
= find_domain_from_name(name_namespace
);
1303 if (contact_domain
== NULL
) {
1304 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1305 state
->request
->data
.auth
.user
, name_domain
, name_user
, name_domain
));
1307 result
= NT_STATUS_NO_SUCH_USER
;
1312 if (contact_domain
->initialized
&&
1313 contact_domain
->active_directory
) {
1317 if (!contact_domain
->initialized
) {
1318 init_dc_connection(contact_domain
, false);
1321 if (!contact_domain
->active_directory
) {
1322 DEBUG(3,("krb5 auth requested but domain is not Active Directory\n"));
1323 return NT_STATUS_INVALID_LOGON_TYPE
;
1326 result
= winbindd_raw_kerberos_login(
1327 state
->mem_ctx
, contact_domain
,
1328 state
->request
->data
.auth
.user
,
1329 state
->request
->data
.auth
.pass
,
1330 state
->request
->data
.auth
.krb5_cc_type
,
1331 get_uid_from_request(state
->request
),
1332 info6
, state
->response
->data
.auth
.krb5ccname
);
1337 static NTSTATUS
winbindd_dual_auth_passdb(TALLOC_CTX
*mem_ctx
,
1338 uint32_t logon_parameters
,
1339 const char *domain
, const char *user
,
1340 const DATA_BLOB
*challenge
,
1341 const DATA_BLOB
*lm_resp
,
1342 const DATA_BLOB
*nt_resp
,
1344 uint8_t *pauthoritative
,
1345 struct netr_SamInfo3
**pinfo3
)
1347 struct auth_context
*auth_context
;
1348 struct auth_serversupplied_info
*server_info
;
1349 struct auth_usersupplied_info
*user_info
= NULL
;
1350 struct tsocket_address
*local
;
1351 struct netr_SamInfo3
*info3
;
1355 TALLOC_CTX
*frame
= talloc_stackframe();
1358 * We are authoritative by default
1360 *pauthoritative
= 1;
1362 rc
= tsocket_address_inet_from_strings(frame
,
1369 return NT_STATUS_NO_MEMORY
;
1373 * TODO: We should get the service description passed in from
1374 * the winbind client, so we can have "smb2", "squid" or "samr" logged
1377 status
= make_user_info(frame
, &user_info
, user
, user
, domain
, domain
,
1378 lp_netbios_name(), local
, local
,
1380 lm_resp
, nt_resp
, NULL
, NULL
,
1381 NULL
, AUTH_PASSWORD_RESPONSE
);
1382 if (!NT_STATUS_IS_OK(status
)) {
1383 DEBUG(10, ("make_user_info failed: %s\n", nt_errstr(status
)));
1388 user_info
->logon_parameters
= logon_parameters
;
1390 /* We don't want any more mapping of the username */
1391 user_info
->mapped_state
= True
;
1393 /* We don't want to come back to winbindd or to do PAM account checks */
1394 user_info
->flags
|= USER_INFO_INFO3_AND_NO_AUTHZ
;
1397 user_info
->flags
|= USER_INFO_INTERACTIVE_LOGON
;
1400 status
= make_auth3_context_for_winbind(frame
, &auth_context
);
1401 if (!NT_STATUS_IS_OK(status
)) {
1402 DBG_ERR("make_auth3_context_for_winbind failed: %s\n",
1408 ok
= auth3_context_set_challenge(auth_context
,
1409 challenge
->data
, "fixed");
1412 return NT_STATUS_NO_MEMORY
;
1415 status
= auth_check_ntlm_password(mem_ctx
,
1420 if (!NT_STATUS_IS_OK(status
)) {
1425 info3
= talloc_zero(mem_ctx
, struct netr_SamInfo3
);
1426 if (info3
== NULL
) {
1428 return NT_STATUS_NO_MEMORY
;
1431 status
= serverinfo_to_SamInfo3(server_info
, info3
);
1432 if (!NT_STATUS_IS_OK(status
)) {
1435 DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
1436 nt_errstr(status
)));
1441 DEBUG(10, ("Authenticaticating user %s\\%s returned %s\n", domain
,
1442 user
, nt_errstr(status
)));
1447 static NTSTATUS
winbind_samlogon_retry_loop(struct winbindd_domain
*domain
,
1448 TALLOC_CTX
*mem_ctx
,
1449 uint32_t logon_parameters
,
1450 const char *username
,
1451 const char *password
,
1452 const char *domainname
,
1453 const char *workstation
,
1454 bool plaintext_given
,
1455 const uint8_t chal
[8],
1456 DATA_BLOB lm_response
,
1457 DATA_BLOB nt_response
,
1459 uint8_t *authoritative
,
1461 uint16_t *_validation_level
,
1462 union netr_Validation
**_validation
)
1465 int netr_attempts
= 0;
1468 enum netr_LogonInfoClass logon_type_i
;
1469 enum netr_LogonInfoClass logon_type_n
;
1470 uint16_t validation_level
= UINT16_MAX
;
1471 union netr_Validation
*validation
= NULL
;
1474 struct rpc_pipe_client
*netlogon_pipe
;
1475 struct netlogon_creds_cli_context
*netlogon_creds_ctx
= NULL
;
1479 result
= cm_connect_netlogon_secure(domain
, &netlogon_pipe
,
1480 &netlogon_creds_ctx
);
1482 if (NT_STATUS_EQUAL(result
,
1483 NT_STATUS_CANT_ACCESS_DOMAIN_INFO
)) {
1485 * This means we don't have a trust account.
1488 result
= NT_STATUS_NO_SUCH_USER
;
1492 if (!NT_STATUS_IS_OK(result
)) {
1493 DEBUG(3,("Could not open handle to NETLOGON pipe "
1494 "(error: %s, attempts: %d)\n",
1495 nt_errstr(result
), netr_attempts
));
1497 /* After the first retry always close the connection */
1498 if (netr_attempts
> 0) {
1499 DEBUG(3, ("This is again a problem for this "
1500 "particular call, forcing the close "
1501 "of this connection\n"));
1502 invalidate_cm_connection(domain
);
1505 /* After the second retry failover to the next DC */
1506 if (netr_attempts
> 1) {
1508 * If the netlogon server is not reachable then
1509 * it is possible that the DC is rebuilding
1510 * sysvol and shutdown netlogon for that time.
1511 * We should failover to the next dc.
1513 DEBUG(3, ("This is the third problem for this "
1514 "particular call, adding DC to the "
1515 "negative cache list: %s %s\n", domain
->name
, domain
->dcname
));
1516 add_failed_connection_entry(domain
->name
,
1519 saf_delete(domain
->name
);
1522 /* Only allow 3 retries */
1523 if (netr_attempts
< 3) {
1524 DEBUG(3, ("The connection to netlogon "
1525 "failed, retrying\n"));
1533 logon_type_i
= NetlogonInteractiveInformation
;
1534 logon_type_n
= NetlogonNetworkInformation
;
1535 if (domain
->domain_trust_attribs
& LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
) {
1536 logon_type_i
= NetlogonInteractiveTransitiveInformation
;
1537 logon_type_n
= NetlogonNetworkTransitiveInformation
;
1540 if (domain
->domain_trust_attribs
& LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE
) {
1541 logon_type_i
= NetlogonInteractiveTransitiveInformation
;
1542 logon_type_n
= NetlogonNetworkTransitiveInformation
;
1545 if (domain
->domain_trust_attribs
& LSA_TRUST_ATTRIBUTE_NON_TRANSITIVE
) {
1546 logon_type_i
= NetlogonInteractiveInformation
;
1547 logon_type_n
= NetlogonNetworkInformation
;
1550 if (domain
->domain_trust_attribs
& LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN
) {
1551 logon_type_i
= NetlogonInteractiveInformation
;
1552 logon_type_n
= NetlogonNetworkInformation
;
1556 if (plaintext_given
) {
1557 result
= rpccli_netlogon_password_logon(
1559 netlogon_pipe
->binding_handle
,
1571 } else if (interactive
) {
1572 result
= rpccli_netlogon_interactive_logon(
1574 netlogon_pipe
->binding_handle
,
1588 result
= rpccli_netlogon_network_logon(
1590 netlogon_pipe
->binding_handle
,
1607 * we increment this after the "feature negotiation"
1608 * for can_do_samlogon_ex and can_do_validation6
1612 /* We have to try a second time as cm_connect_netlogon
1613 might not yet have noticed that the DC has killed
1616 if (!rpccli_is_connected(netlogon_pipe
)) {
1621 /* if we get access denied, a possible cause was that we had
1622 and open connection to the DC, but someone changed our
1623 machine account password out from underneath us using 'net
1624 rpc changetrustpw' */
1626 if ( NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) ) {
1627 DEBUG(1,("winbind_samlogon_retry_loop: sam_logon returned "
1628 "ACCESS_DENIED. Maybe the DC has Restrict "
1629 "NTLM set or the trust account "
1630 "password was changed and we didn't know it. "
1631 "Killing connections to domain %s\n",
1633 invalidate_cm_connection(domain
);
1637 if (NT_STATUS_EQUAL(result
, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
)) {
1639 * Got DCERPC_FAULT_OP_RNG_ERROR for SamLogon
1640 * (no Ex). This happens against old Samba
1641 * DCs, if LogonSamLogonEx() fails with an error
1642 * e.g. NT_STATUS_NO_SUCH_USER or NT_STATUS_WRONG_PASSWORD.
1644 * The server will log something like this:
1645 * api_net_sam_logon_ex: Failed to marshall NET_R_SAM_LOGON_EX.
1647 * This sets the whole connection into a fault_state mode
1648 * and all following request get NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE.
1650 * This also happens to our retry with LogonSamLogonWithFlags()
1651 * and LogonSamLogon().
1653 * In order to recover from this situation, we need to
1654 * drop the connection.
1656 invalidate_cm_connection(domain
);
1657 result
= NT_STATUS_LOGON_FAILURE
;
1661 } while ( (attempts
< 2) && retry
);
1663 if (NT_STATUS_EQUAL(result
, NT_STATUS_IO_TIMEOUT
)) {
1664 DEBUG(3,("winbind_samlogon_retry_loop: sam_network_logon(ex) "
1665 "returned NT_STATUS_IO_TIMEOUT after the retry. "
1666 "Killing connections to domain %s\n",
1668 invalidate_cm_connection(domain
);
1671 if (!NT_STATUS_IS_OK(result
)) {
1675 *_validation_level
= validation_level
;
1676 *_validation
= validation
;
1677 return NT_STATUS_OK
;
1680 static NTSTATUS
winbindd_dual_pam_auth_samlogon(
1681 TALLOC_CTX
*mem_ctx
,
1682 struct winbindd_domain
*domain
,
1685 uint32_t request_flags
,
1686 uint16_t *_validation_level
,
1687 union netr_Validation
**_validation
)
1693 unsigned char local_nt_response
[24];
1694 fstring name_namespace
, name_domain
, name_user
;
1696 uint8_t authoritative
= 0;
1698 uint16_t validation_level
;
1699 union netr_Validation
*validation
= NULL
;
1700 struct netr_SamBaseInfo
*base_info
= NULL
;
1703 DEBUG(10,("winbindd_dual_pam_auth_samlogon\n"));
1705 /* Parse domain and username */
1707 ok
= parse_domain_user(user
, name_namespace
, name_domain
, name_user
);
1709 return NT_STATUS_INVALID_PARAMETER
;
1713 * We check against domain->name instead of
1714 * name_domain, as find_auth_domain() ->
1715 * find_domain_from_name_noinit() already decided
1716 * that we are in a child for the correct domain.
1718 * name_domain can also be lp_realm()
1719 * we need to check against domain->name.
1721 if (strequal(domain
->name
, get_global_sam_name())) {
1722 DATA_BLOB chal_blob
= data_blob_const(chal
, sizeof(chal
));
1723 struct netr_SamInfo3
*info3
= NULL
;
1725 /* do password magic */
1727 generate_random_buffer(chal
, sizeof(chal
));
1729 if (lp_client_ntlmv2_auth()) {
1730 DATA_BLOB server_chal
;
1731 DATA_BLOB names_blob
;
1732 server_chal
= data_blob_const(chal
, 8);
1734 /* note that the 'workgroup' here is for the local
1735 machine. The 'server name' must match the
1736 'workstation' passed to the actual SamLogon call.
1738 names_blob
= NTLMv2_generate_names_blob(
1739 mem_ctx
, lp_netbios_name(), lp_workgroup());
1741 if (!SMBNTLMv2encrypt(mem_ctx
, name_user
, name_domain
,
1745 &lm_resp
, &nt_resp
, NULL
, NULL
)) {
1746 data_blob_free(&names_blob
);
1747 DEBUG(0, ("winbindd_pam_auth: SMBNTLMv2encrypt() failed!\n"));
1748 result
= NT_STATUS_NO_MEMORY
;
1751 data_blob_free(&names_blob
);
1753 lm_resp
= data_blob_null
;
1754 SMBNTencrypt(pass
, chal
, local_nt_response
);
1756 nt_resp
= data_blob_talloc(mem_ctx
, local_nt_response
,
1757 sizeof(local_nt_response
));
1760 result
= winbindd_dual_auth_passdb(
1761 talloc_tos(), 0, name_domain
, name_user
,
1762 &chal_blob
, &lm_resp
, &nt_resp
,
1763 true, /* interactive */
1768 * We need to try the remote NETLOGON server if this is
1769 * not authoritative (for example on the RODC).
1771 if (authoritative
!= 0) {
1772 if (NT_STATUS_IS_OK(result
)) {
1773 result
= map_info3_to_validation(
1779 if (!NT_STATUS_IS_OK(result
)) {
1788 /* check authentication loop */
1790 result
= winbind_samlogon_retry_loop(domain
,
1797 true, /* plaintext_given */
1799 data_blob_null
, data_blob_null
,
1800 true, /* interactive */
1805 if (!NT_STATUS_IS_OK(result
)) {
1809 /* handle the case where a NT4 DC does not fill in the acct_flags in
1810 * the samlogon reply info3. When accurate info3 is required by the
1811 * caller, we look up the account flags ourselves - gd */
1813 switch (validation_level
) {
1815 base_info
= &validation
->sam3
->base
;
1818 base_info
= &validation
->sam6
->base
;
1821 DBG_ERR("Bad validation level %d", (int)validation_level
);
1822 result
= NT_STATUS_INTERNAL_ERROR
;
1825 if ((request_flags
& WBFLAG_PAM_INFO3_TEXT
) &&
1826 (base_info
->acct_flags
== 0))
1828 struct rpc_pipe_client
*samr_pipe
;
1829 struct policy_handle samr_domain_handle
, user_pol
;
1830 union samr_UserInfo
*info
= NULL
;
1831 NTSTATUS status_tmp
, result_tmp
;
1832 uint32_t acct_flags
;
1833 struct dcerpc_binding_handle
*b
;
1835 status_tmp
= cm_connect_sam(domain
, mem_ctx
, false,
1836 &samr_pipe
, &samr_domain_handle
);
1838 if (!NT_STATUS_IS_OK(status_tmp
)) {
1839 DEBUG(3, ("could not open handle to SAMR pipe: %s\n",
1840 nt_errstr(status_tmp
)));
1844 b
= samr_pipe
->binding_handle
;
1846 status_tmp
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1847 &samr_domain_handle
,
1848 MAXIMUM_ALLOWED_ACCESS
,
1853 if (!NT_STATUS_IS_OK(status_tmp
)) {
1854 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1855 nt_errstr(status_tmp
)));
1858 if (!NT_STATUS_IS_OK(result_tmp
)) {
1859 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1860 nt_errstr(result_tmp
)));
1864 status_tmp
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1870 if (any_nt_status_not_ok(status_tmp
, result_tmp
,
1872 DEBUG(3, ("could not query user info on SAMR pipe: %s\n",
1873 nt_errstr(status_tmp
)));
1874 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result_tmp
);
1878 acct_flags
= info
->info16
.acct_flags
;
1880 if (acct_flags
== 0) {
1881 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result_tmp
);
1885 base_info
->acct_flags
= acct_flags
;
1887 DEBUG(10,("successfully retrieved acct_flags 0x%x\n", acct_flags
));
1889 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result_tmp
);
1893 if (NT_STATUS_IS_OK(result
)) {
1894 *_validation_level
= validation_level
;
1895 *_validation
= validation
;
1900 enum winbindd_result
winbindd_dual_pam_auth(struct winbindd_domain
*domain
,
1901 struct winbindd_cli_state
*state
)
1903 NTSTATUS result
= NT_STATUS_LOGON_FAILURE
;
1904 NTSTATUS krb5_result
= NT_STATUS_OK
;
1905 fstring name_namespace
, name_domain
, name_user
;
1907 fstring domain_user
;
1908 uint16_t validation_level
= UINT16_MAX
;
1909 union netr_Validation
*validation
= NULL
;
1910 NTSTATUS name_map_status
= NT_STATUS_UNSUCCESSFUL
;
1913 /* Ensure null termination */
1914 state
->request
->data
.auth
.user
[sizeof(state
->request
->data
.auth
.user
)-1]='\0';
1916 /* Ensure null termination */
1917 state
->request
->data
.auth
.pass
[sizeof(state
->request
->data
.auth
.pass
)-1]='\0';
1919 DEBUG(3, ("[%5lu]: dual pam auth %s\n", (unsigned long)state
->pid
,
1920 state
->request
->data
.auth
.user
));
1922 /* Parse domain and username */
1924 name_map_status
= normalize_name_unmap(state
->mem_ctx
,
1925 state
->request
->data
.auth
.user
,
1928 /* If the name normalization didnt' actually do anything,
1929 just use the original name */
1931 if (!NT_STATUS_IS_OK(name_map_status
) &&
1932 !NT_STATUS_EQUAL(name_map_status
, NT_STATUS_FILE_RENAMED
))
1934 mapped_user
= state
->request
->data
.auth
.user
;
1937 ok
= parse_domain_user(mapped_user
,
1942 result
= NT_STATUS_INVALID_PARAMETER
;
1943 goto process_result
;
1946 if ( mapped_user
!= state
->request
->data
.auth
.user
) {
1947 fstr_sprintf( domain_user
, "%s%c%s", name_domain
,
1948 *lp_winbind_separator(),
1950 strlcpy( state
->request
->data
.auth
.user
, domain_user
,
1951 sizeof(state
->request
->data
.auth
.user
));
1954 if (!domain
->online
) {
1955 result
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
1956 if (domain
->startup
) {
1957 /* Logons are very important to users. If we're offline and
1958 we get a request within the first 30 seconds of startup,
1959 try very hard to find a DC and go online. */
1961 DEBUG(10,("winbindd_dual_pam_auth: domain: %s offline and auth "
1962 "request in startup mode.\n", domain
->name
));
1964 winbindd_flush_negative_conn_cache(domain
);
1965 result
= init_dc_connection(domain
, false);
1969 DEBUG(10,("winbindd_dual_pam_auth: domain: %s last was %s\n", domain
->name
, domain
->online
? "online":"offline"));
1971 /* Check for Kerberos authentication */
1972 if (domain
->online
&& (state
->request
->flags
& WBFLAG_PAM_KRB5
)) {
1973 struct netr_SamInfo6
*info6
= NULL
;
1975 result
= winbindd_dual_pam_auth_kerberos(domain
, state
, &info6
);
1976 /* save for later */
1977 krb5_result
= result
;
1979 if (NT_STATUS_IS_OK(result
)) {
1980 DEBUG(10,("winbindd_dual_pam_auth_kerberos succeeded\n"));
1982 result
= map_info6_to_validation(state
->mem_ctx
,
1987 if (!NT_STATUS_IS_OK(result
)) {
1988 DBG_ERR("map_info6_to_validation failed\n");
1991 goto process_result
;
1994 DBG_DEBUG("winbindd_dual_pam_auth_kerberos failed: %s\n",
1997 if (NT_STATUS_EQUAL(result
, NT_STATUS_NO_LOGON_SERVERS
) ||
1998 NT_STATUS_EQUAL(result
, NT_STATUS_IO_TIMEOUT
) ||
1999 NT_STATUS_EQUAL(result
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
)) {
2000 DEBUG(10,("winbindd_dual_pam_auth_kerberos setting domain to offline\n"));
2001 set_domain_offline( domain
);
2005 /* there are quite some NT_STATUS errors where there is no
2006 * point in retrying with a samlogon, we explictly have to take
2007 * care not to increase the bad logon counter on the DC */
2009 if (NT_STATUS_EQUAL(result
, NT_STATUS_ACCOUNT_DISABLED
) ||
2010 NT_STATUS_EQUAL(result
, NT_STATUS_ACCOUNT_EXPIRED
) ||
2011 NT_STATUS_EQUAL(result
, NT_STATUS_ACCOUNT_LOCKED_OUT
) ||
2012 NT_STATUS_EQUAL(result
, NT_STATUS_INVALID_LOGON_HOURS
) ||
2013 NT_STATUS_EQUAL(result
, NT_STATUS_INVALID_WORKSTATION
) ||
2014 NT_STATUS_EQUAL(result
, NT_STATUS_LOGON_FAILURE
) ||
2015 NT_STATUS_EQUAL(result
, NT_STATUS_NO_SUCH_USER
) ||
2016 NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_EXPIRED
) ||
2017 NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_MUST_CHANGE
) ||
2018 NT_STATUS_EQUAL(result
, NT_STATUS_WRONG_PASSWORD
)) {
2022 if (state
->request
->flags
& WBFLAG_PAM_FALLBACK_AFTER_KRB5
) {
2023 DEBUG(3,("falling back to samlogon\n"));
2031 /* Check for Samlogon authentication */
2032 if (domain
->online
) {
2033 struct netr_SamBaseInfo
*base_info
= NULL
;
2035 result
= winbindd_dual_pam_auth_samlogon(
2036 state
->mem_ctx
, domain
,
2037 state
->request
->data
.auth
.user
,
2038 state
->request
->data
.auth
.pass
,
2039 state
->request
->flags
,
2043 if (NT_STATUS_IS_OK(result
)) {
2044 DEBUG(10,("winbindd_dual_pam_auth_samlogon succeeded\n"));
2046 switch (validation_level
) {
2048 base_info
= &validation
->sam3
->base
;
2051 base_info
= &validation
->sam6
->base
;
2054 DBG_ERR("Bad validation level %d\n",
2056 result
= NT_STATUS_INTERNAL_ERROR
;
2060 /* add the Krb5 err if we have one */
2061 if ( NT_STATUS_EQUAL(krb5_result
, NT_STATUS_TIME_DIFFERENCE_AT_DC
) ) {
2062 base_info
->user_flags
|= LOGON_KRB5_FAIL_CLOCK_SKEW
;
2065 goto process_result
;
2068 DEBUG(10,("winbindd_dual_pam_auth_samlogon failed: %s\n",
2069 nt_errstr(result
)));
2071 if (NT_STATUS_EQUAL(result
, NT_STATUS_NO_LOGON_SERVERS
) ||
2072 NT_STATUS_EQUAL(result
, NT_STATUS_IO_TIMEOUT
) ||
2073 NT_STATUS_EQUAL(result
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
))
2075 DEBUG(10,("winbindd_dual_pam_auth_samlogon setting domain to offline\n"));
2076 set_domain_offline( domain
);
2080 if (domain
->online
) {
2081 /* We're still online - fail. */
2087 /* Check for Cached logons */
2088 if (!domain
->online
&& (state
->request
->flags
& WBFLAG_PAM_CACHED_LOGIN
) &&
2089 lp_winbind_offline_logon()) {
2090 struct netr_SamInfo3
*info3
= NULL
;
2092 result
= winbindd_dual_pam_auth_cached(domain
, state
, &info3
);
2094 if (!NT_STATUS_IS_OK(result
)) {
2095 DEBUG(10,("winbindd_dual_pam_auth_cached failed: %s\n", nt_errstr(result
)));
2098 DEBUG(10,("winbindd_dual_pam_auth_cached succeeded\n"));
2100 result
= map_info3_to_validation(state
->mem_ctx
,
2105 if (!NT_STATUS_IS_OK(result
)) {
2106 DBG_ERR("map_info3_to_validation failed\n");
2113 if (NT_STATUS_IS_OK(result
)) {
2114 struct dom_sid user_sid
;
2115 TALLOC_CTX
*base_ctx
= NULL
;
2116 struct netr_SamBaseInfo
*base_info
= NULL
;
2117 struct netr_SamInfo3
*info3
= NULL
;
2119 switch (validation_level
) {
2121 base_ctx
= validation
->sam3
;
2122 base_info
= &validation
->sam3
->base
;
2125 base_ctx
= validation
->sam6
;
2126 base_info
= &validation
->sam6
->base
;
2129 result
= NT_STATUS_INTERNAL_ERROR
;
2133 sid_compose(&user_sid
, base_info
->domain_sid
, base_info
->rid
);
2135 if (base_info
->full_name
.string
== NULL
) {
2136 struct netr_SamInfo3
*cached_info3
;
2138 cached_info3
= netsamlogon_cache_get(state
->mem_ctx
,
2140 if (cached_info3
!= NULL
&&
2141 cached_info3
->base
.full_name
.string
!= NULL
) {
2142 base_info
->full_name
.string
= talloc_strdup(
2144 cached_info3
->base
.full_name
.string
);
2145 if (base_info
->full_name
.string
== NULL
) {
2146 result
= NT_STATUS_NO_MEMORY
;
2151 /* this might fail so we don't check the return code */
2152 wcache_query_user_fullname(domain
,
2155 &base_info
->full_name
.string
);
2159 result
= map_validation_to_info3(talloc_tos(),
2163 if (!NT_STATUS_IS_OK(result
)) {
2167 wcache_invalidate_samlogon(find_domain_from_name(name_domain
),
2169 netsamlogon_cache_store(name_user
, info3
);
2171 /* save name_to_sid info as early as possible (only if
2172 this is our primary domain so we don't invalidate
2173 the cache entry by storing the seq_num for the wrong
2175 if ( domain
->primary
) {
2176 cache_name2sid(domain
, name_domain
, name_user
,
2177 SID_NAME_USER
, &user_sid
);
2180 /* Check if the user is in the right group */
2182 result
= check_info3_in_group(
2184 state
->request
->data
.auth
.require_membership_of_sid
);
2185 if (!NT_STATUS_IS_OK(result
)) {
2186 DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
2187 state
->request
->data
.auth
.user
,
2188 state
->request
->data
.auth
.require_membership_of_sid
));
2192 result
= append_auth_data(state
->mem_ctx
, state
->response
,
2193 state
->request
->flags
,
2196 name_domain
, name_user
);
2197 if (!NT_STATUS_IS_OK(result
)) {
2201 if ((state
->request
->flags
& WBFLAG_PAM_CACHED_LOGIN
)
2202 && lp_winbind_offline_logon()) {
2204 result
= winbindd_store_creds(domain
,
2205 state
->request
->data
.auth
.user
,
2206 state
->request
->data
.auth
.pass
,
2210 if (state
->request
->flags
& WBFLAG_PAM_GET_PWD_POLICY
) {
2212 * WBFLAG_PAM_GET_PWD_POLICY is not used within
2213 * any Samba caller anymore.
2215 * We just fake this based on the effective values
2216 * for the user, for legacy callers.
2218 fake_password_policy(state
->response
, &info3
->base
);
2221 result
= NT_STATUS_OK
;
2225 /* give us a more useful (more correct?) error code */
2226 if ((NT_STATUS_EQUAL(result
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
) ||
2227 (NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
)))) {
2228 result
= NT_STATUS_NO_LOGON_SERVERS
;
2231 set_auth_errors(state
->response
, result
);
2233 DEBUG(NT_STATUS_IS_OK(result
) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n",
2234 state
->request
->data
.auth
.user
,
2235 state
->response
->data
.auth
.nt_status_string
,
2236 state
->response
->data
.auth
.pam_error
));
2238 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
2241 NTSTATUS
winbind_dual_SamLogon(struct winbindd_domain
*domain
,
2242 TALLOC_CTX
*mem_ctx
,
2244 uint32_t logon_parameters
,
2245 const char *name_user
,
2246 const char *name_domain
,
2247 const char *workstation
,
2248 const uint8_t chal
[8],
2249 DATA_BLOB lm_response
,
2250 DATA_BLOB nt_response
,
2251 uint8_t *authoritative
,
2254 uint16_t *_validation_level
,
2255 union netr_Validation
**_validation
)
2257 uint16_t validation_level
= 0;
2258 union netr_Validation
*validation
= NULL
;
2262 * We check against domain->name instead of
2263 * name_domain, as find_auth_domain() ->
2264 * find_domain_from_name_noinit() already decided
2265 * that we are in a child for the correct domain.
2267 * name_domain can also be lp_realm()
2268 * we need to check against domain->name.
2270 if (!skip_sam
&& strequal(domain
->name
, get_global_sam_name())) {
2271 DATA_BLOB chal_blob
= data_blob_const(
2273 struct netr_SamInfo3
*info3
= NULL
;
2275 result
= winbindd_dual_auth_passdb(
2278 name_domain
, name_user
,
2279 &chal_blob
, &lm_response
, &nt_response
,
2283 if (NT_STATUS_IS_OK(result
)) {
2284 result
= map_info3_to_validation(mem_ctx
,
2289 if (!NT_STATUS_IS_OK(result
)) {
2295 * We need to try the remote NETLOGON server if this is
2296 * not authoritative.
2298 if (*authoritative
!= 0) {
2300 goto process_result
;
2304 result
= winbind_samlogon_retry_loop(domain
,
2308 NULL
, /* password */
2310 /* Bug #3248 - found by Stefan Burkei. */
2311 workstation
, /* We carefully set this above so use it... */
2312 false, /* plaintext_given */
2321 if (!NT_STATUS_IS_OK(result
)) {
2327 if (NT_STATUS_IS_OK(result
)) {
2328 struct dom_sid user_sid
;
2329 TALLOC_CTX
*base_ctx
= NULL
;
2330 struct netr_SamBaseInfo
*base_info
= NULL
;
2331 struct netr_SamInfo3
*info3
= NULL
;
2333 switch (validation_level
) {
2335 base_ctx
= validation
->sam3
;
2336 base_info
= &validation
->sam3
->base
;
2339 base_ctx
= validation
->sam6
;
2340 base_info
= &validation
->sam6
->base
;
2343 result
= NT_STATUS_INTERNAL_ERROR
;
2347 sid_compose(&user_sid
, base_info
->domain_sid
, base_info
->rid
);
2349 if (base_info
->full_name
.string
== NULL
) {
2350 struct netr_SamInfo3
*cached_info3
;
2352 cached_info3
= netsamlogon_cache_get(mem_ctx
,
2354 if (cached_info3
!= NULL
&&
2355 cached_info3
->base
.full_name
.string
!= NULL
)
2357 base_info
->full_name
.string
= talloc_strdup(
2359 cached_info3
->base
.full_name
.string
);
2362 /* this might fail so we don't check the return code */
2363 wcache_query_user_fullname(domain
,
2366 &base_info
->full_name
.string
);
2370 result
= map_validation_to_info3(talloc_tos(),
2374 if (!NT_STATUS_IS_OK(result
)) {
2377 wcache_invalidate_samlogon(find_domain_from_name(name_domain
),
2379 netsamlogon_cache_store(name_user
, info3
);
2385 /* give us a more useful (more correct?) error code */
2386 if ((NT_STATUS_EQUAL(result
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
) ||
2387 (NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
)))) {
2388 result
= NT_STATUS_NO_LOGON_SERVERS
;
2391 DEBUG(NT_STATUS_IS_OK(result
) ? 5 : 2,
2392 ("NTLM CRAP authentication for user [%s]\\[%s] returned %s\n",
2395 nt_errstr(result
)));
2397 if (!NT_STATUS_IS_OK(result
)) {
2401 *_validation_level
= validation_level
;
2402 *_validation
= validation
;
2403 return NT_STATUS_OK
;
2406 enum winbindd_result
winbindd_dual_pam_auth_crap(struct winbindd_domain
*domain
,
2407 struct winbindd_cli_state
*state
)
2410 const char *name_user
= NULL
;
2411 const char *name_domain
= NULL
;
2412 const char *workstation
;
2413 uint8_t authoritative
= 0;
2415 uint16_t validation_level
;
2416 union netr_Validation
*validation
= NULL
;
2417 DATA_BLOB lm_resp
, nt_resp
;
2419 /* This is child-only, so no check for privileged access is needed
2422 /* Ensure null termination */
2423 state
->request
->data
.auth_crap
.user
[sizeof(state
->request
->data
.auth_crap
.user
)-1]=0;
2424 state
->request
->data
.auth_crap
.domain
[sizeof(state
->request
->data
.auth_crap
.domain
)-1]=0;
2426 name_user
= state
->request
->data
.auth_crap
.user
;
2427 name_domain
= state
->request
->data
.auth_crap
.domain
;
2428 workstation
= state
->request
->data
.auth_crap
.workstation
;
2430 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state
->pid
,
2431 name_domain
, name_user
));
2433 if (state
->request
->data
.auth_crap
.lm_resp_len
> sizeof(state
->request
->data
.auth_crap
.lm_resp
)
2434 || state
->request
->data
.auth_crap
.nt_resp_len
> sizeof(state
->request
->data
.auth_crap
.nt_resp
)) {
2435 if (!(state
->request
->flags
& WBFLAG_BIG_NTLMV2_BLOB
) ||
2436 state
->request
->extra_len
!= state
->request
->data
.auth_crap
.nt_resp_len
) {
2437 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n",
2438 state
->request
->data
.auth_crap
.lm_resp_len
,
2439 state
->request
->data
.auth_crap
.nt_resp_len
));
2440 result
= NT_STATUS_INVALID_PARAMETER
;
2445 lm_resp
= data_blob_talloc(state
->mem_ctx
, state
->request
->data
.auth_crap
.lm_resp
,
2446 state
->request
->data
.auth_crap
.lm_resp_len
);
2448 if (state
->request
->flags
& WBFLAG_BIG_NTLMV2_BLOB
) {
2449 nt_resp
= data_blob_talloc(state
->mem_ctx
,
2450 state
->request
->extra_data
.data
,
2451 state
->request
->data
.auth_crap
.nt_resp_len
);
2453 nt_resp
= data_blob_talloc(state
->mem_ctx
,
2454 state
->request
->data
.auth_crap
.nt_resp
,
2455 state
->request
->data
.auth_crap
.nt_resp_len
);
2458 result
= winbind_dual_SamLogon(domain
,
2460 false, /* interactive */
2461 state
->request
->data
.auth_crap
.logon_parameters
,
2464 /* Bug #3248 - found by Stefan Burkei. */
2465 workstation
, /* We carefully set this above so use it... */
2466 state
->request
->data
.auth_crap
.chal
,
2474 if (!NT_STATUS_IS_OK(result
)) {
2475 state
->response
->data
.auth
.authoritative
= authoritative
;
2479 if (NT_STATUS_IS_OK(result
)) {
2480 struct netr_SamInfo3
*info3
= NULL
;
2482 result
= map_validation_to_info3(state
->mem_ctx
,
2486 if (!NT_STATUS_IS_OK(result
)) {
2490 /* Check if the user is in the right group */
2491 result
= check_info3_in_group(
2493 state
->request
->data
.auth_crap
.require_membership_of_sid
);
2494 if (!NT_STATUS_IS_OK(result
)) {
2495 DEBUG(3, ("User %s is not in the required group (%s), so "
2496 "crap authentication is rejected\n",
2497 state
->request
->data
.auth_crap
.user
,
2498 state
->request
->data
.auth_crap
.require_membership_of_sid
));
2502 result
= append_auth_data(state
->mem_ctx
, state
->response
,
2503 state
->request
->flags
,
2506 name_domain
, name_user
);
2507 if (!NT_STATUS_IS_OK(result
)) {
2514 if (state
->request
->flags
& WBFLAG_PAM_NT_STATUS_SQUASH
) {
2515 result
= nt_status_squash(result
);
2518 set_auth_errors(state
->response
, result
);
2520 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
2523 enum winbindd_result
winbindd_dual_pam_chauthtok(struct winbindd_domain
*contact_domain
,
2524 struct winbindd_cli_state
*state
)
2527 char *newpass
= NULL
;
2528 struct policy_handle dom_pol
;
2529 struct rpc_pipe_client
*cli
= NULL
;
2530 bool got_info
= false;
2531 struct samr_DomInfo1
*info
= NULL
;
2532 struct userPwdChangeFailureInformation
*reject
= NULL
;
2533 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2534 fstring
namespace, domain
, user
;
2535 struct dcerpc_binding_handle
*b
= NULL
;
2538 ZERO_STRUCT(dom_pol
);
2540 DEBUG(3, ("[%5lu]: dual pam chauthtok %s\n", (unsigned long)state
->pid
,
2541 state
->request
->data
.auth
.user
));
2543 ok
= parse_domain_user(state
->request
->data
.chauthtok
.user
,
2551 /* Change password */
2553 oldpass
= state
->request
->data
.chauthtok
.oldpass
;
2554 newpass
= state
->request
->data
.chauthtok
.newpass
;
2556 /* Initialize reject reason */
2557 state
->response
->data
.auth
.reject_reason
= Undefined
;
2559 /* Get sam handle */
2561 result
= cm_connect_sam(contact_domain
, state
->mem_ctx
, true, &cli
,
2563 if (!NT_STATUS_IS_OK(result
)) {
2564 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain
));
2568 b
= cli
->binding_handle
;
2570 result
= rpccli_samr_chgpasswd_user3(cli
, state
->mem_ctx
,
2577 /* Windows 2003 returns NT_STATUS_PASSWORD_RESTRICTION */
2579 if (NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_RESTRICTION
) ) {
2581 fill_in_password_policy(state
->response
, info
);
2583 state
->response
->data
.auth
.reject_reason
=
2584 reject
->extendedFailureReason
;
2589 /* atm the pidl generated rpccli_samr_ChangePasswordUser3 function will
2590 * return with NT_STATUS_BUFFER_TOO_SMALL for w2k dcs as w2k just
2591 * returns with 4byte error code (NT_STATUS_NOT_SUPPORTED) which is too
2592 * short to comply with the samr_ChangePasswordUser3 idl - gd */
2594 /* only fallback when the chgpasswd_user3 call is not supported */
2595 if (NT_STATUS_EQUAL(result
, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
) ||
2596 NT_STATUS_EQUAL(result
, NT_STATUS_NOT_SUPPORTED
) ||
2597 NT_STATUS_EQUAL(result
, NT_STATUS_BUFFER_TOO_SMALL
) ||
2598 NT_STATUS_EQUAL(result
, NT_STATUS_NOT_IMPLEMENTED
)) {
2600 DEBUG(10,("Password change with chgpasswd_user3 failed with: %s, retrying chgpasswd_user2\n",
2601 nt_errstr(result
)));
2603 result
= rpccli_samr_chgpasswd_user2(cli
, state
->mem_ctx
, user
, newpass
, oldpass
);
2605 /* Windows 2000 returns NT_STATUS_ACCOUNT_RESTRICTION.
2606 Map to the same status code as Windows 2003. */
2608 if ( NT_STATUS_EQUAL(NT_STATUS_ACCOUNT_RESTRICTION
, result
) ) {
2609 result
= NT_STATUS_PASSWORD_RESTRICTION
;
2615 if (NT_STATUS_IS_OK(result
)
2616 && (state
->request
->flags
& WBFLAG_PAM_CACHED_LOGIN
)
2617 && lp_winbind_offline_logon()) {
2618 result
= winbindd_update_creds_by_name(contact_domain
, user
,
2620 /* Again, this happens when we login from gdm or xdm
2621 * and the password expires, *BUT* cached crendentials
2622 * doesn't exist. winbindd_update_creds_by_name()
2623 * returns NT_STATUS_NO_SUCH_USER.
2624 * This is not a failure.
2627 if (NT_STATUS_EQUAL(result
, NT_STATUS_NO_SUCH_USER
)) {
2628 result
= NT_STATUS_OK
;
2631 if (!NT_STATUS_IS_OK(result
)) {
2632 DEBUG(10, ("Failed to store creds: %s\n",
2633 nt_errstr(result
)));
2634 goto process_result
;
2638 if (!NT_STATUS_IS_OK(result
) && !got_info
&& contact_domain
) {
2640 NTSTATUS policy_ret
;
2642 policy_ret
= fillup_password_policy(
2643 contact_domain
, state
->response
);
2645 /* failure of this is non critical, it will just provide no
2646 * additional information to the client why the change has
2647 * failed - Guenther */
2649 if (!NT_STATUS_IS_OK(policy_ret
)) {
2650 DEBUG(10,("Failed to get password policies: %s\n", nt_errstr(policy_ret
)));
2651 goto process_result
;
2657 if (strequal(contact_domain
->name
, get_global_sam_name())) {
2658 /* FIXME: internal rpc pipe does not cache handles yet */
2660 if (is_valid_policy_hnd(&dom_pol
)) {
2662 dcerpc_samr_Close(b
, state
->mem_ctx
, &dom_pol
, &_result
);
2668 set_auth_errors(state
->response
, result
);
2670 DEBUG(NT_STATUS_IS_OK(result
) ? 5 : 2,
2671 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
2674 state
->response
->data
.auth
.nt_status_string
,
2675 state
->response
->data
.auth
.pam_error
));
2677 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
2680 enum winbindd_result
winbindd_dual_pam_logoff(struct winbindd_domain
*domain
,
2681 struct winbindd_cli_state
*state
)
2683 NTSTATUS result
= NT_STATUS_NOT_SUPPORTED
;
2685 DEBUG(3, ("[%5lu]: pam dual logoff %s\n", (unsigned long)state
->pid
,
2686 state
->request
->data
.logoff
.user
));
2688 if (!(state
->request
->flags
& WBFLAG_PAM_KRB5
)) {
2689 result
= NT_STATUS_OK
;
2690 goto process_result
;
2693 if (state
->request
->data
.logoff
.krb5ccname
[0] == '\0') {
2694 result
= NT_STATUS_OK
;
2695 goto process_result
;
2700 if (state
->request
->data
.logoff
.uid
== (uid_t
)-1) {
2701 DEBUG(0,("winbindd_pam_logoff: invalid uid\n"));
2702 goto process_result
;
2705 /* what we need here is to find the corresponding krb5 ccache name *we*
2706 * created for a given username and destroy it */
2708 if (!ccache_entry_exists(state
->request
->data
.logoff
.user
)) {
2709 result
= NT_STATUS_OK
;
2710 DEBUG(10,("winbindd_pam_logoff: no entry found.\n"));
2711 goto process_result
;
2714 if (!ccache_entry_identical(state
->request
->data
.logoff
.user
,
2715 state
->request
->data
.logoff
.uid
,
2716 state
->request
->data
.logoff
.krb5ccname
)) {
2717 DEBUG(0,("winbindd_pam_logoff: cached entry differs.\n"));
2718 goto process_result
;
2721 result
= remove_ccache(state
->request
->data
.logoff
.user
);
2722 if (!NT_STATUS_IS_OK(result
)) {
2723 DEBUG(0,("winbindd_pam_logoff: failed to remove ccache: %s\n",
2724 nt_errstr(result
)));
2725 goto process_result
;
2729 * Remove any mlock'ed memory creds in the child
2730 * we might be using for krb5 ticket renewal.
2733 winbindd_delete_memory_creds(state
->request
->data
.logoff
.user
);
2736 result
= NT_STATUS_NOT_SUPPORTED
;
2742 set_auth_errors(state
->response
, result
);
2744 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
2747 /* Change user password with auth crap*/
2749 enum winbindd_result
winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domain
*domainSt
, struct winbindd_cli_state
*state
)
2752 DATA_BLOB new_nt_password
;
2753 DATA_BLOB old_nt_hash_enc
;
2754 DATA_BLOB new_lm_password
;
2755 DATA_BLOB old_lm_hash_enc
;
2756 fstring
namespace, domain
, user
;
2757 struct policy_handle dom_pol
;
2758 struct winbindd_domain
*contact_domain
= domainSt
;
2759 struct rpc_pipe_client
*cli
= NULL
;
2760 struct dcerpc_binding_handle
*b
= NULL
;
2762 ZERO_STRUCT(dom_pol
);
2764 /* Ensure null termination */
2765 state
->request
->data
.chng_pswd_auth_crap
.user
[
2766 sizeof(state
->request
->data
.chng_pswd_auth_crap
.user
)-1]=0;
2767 state
->request
->data
.chng_pswd_auth_crap
.domain
[
2768 sizeof(state
->request
->data
.chng_pswd_auth_crap
.domain
)-1]=0;
2770 namespace[0] = '\0';
2773 DEBUG(3, ("[%5lu]: pam change pswd auth crap domain: %s user: %s\n",
2774 (unsigned long)state
->pid
,
2775 state
->request
->data
.chng_pswd_auth_crap
.domain
,
2776 state
->request
->data
.chng_pswd_auth_crap
.user
));
2778 if (lp_winbind_offline_logon()) {
2779 DEBUG(0,("Refusing password change as winbind offline logons are enabled. "));
2780 DEBUGADD(0,("Changing passwords here would risk inconsistent logons\n"));
2781 result
= NT_STATUS_ACCESS_DENIED
;
2785 if (*state
->request
->data
.chng_pswd_auth_crap
.domain
) {
2786 fstrcpy(domain
,state
->request
->data
.chng_pswd_auth_crap
.domain
);
2790 ok
= parse_domain_user(state
->request
->data
.chng_pswd_auth_crap
.user
,
2795 result
= NT_STATUS_INVALID_PARAMETER
;
2800 DEBUG(3,("no domain specified with username (%s) - "
2802 state
->request
->data
.chng_pswd_auth_crap
.user
));
2803 result
= NT_STATUS_NO_SUCH_USER
;
2808 if (!*domain
&& lp_winbind_use_default_domain()) {
2809 fstrcpy(domain
,lp_workgroup());
2813 fstrcpy(user
, state
->request
->data
.chng_pswd_auth_crap
.user
);
2816 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n",
2817 (unsigned long)state
->pid
, domain
, user
));
2819 /* Change password */
2820 new_nt_password
= data_blob_const(
2821 state
->request
->data
.chng_pswd_auth_crap
.new_nt_pswd
,
2822 state
->request
->data
.chng_pswd_auth_crap
.new_nt_pswd_len
);
2824 old_nt_hash_enc
= data_blob_const(
2825 state
->request
->data
.chng_pswd_auth_crap
.old_nt_hash_enc
,
2826 state
->request
->data
.chng_pswd_auth_crap
.old_nt_hash_enc_len
);
2828 if(state
->request
->data
.chng_pswd_auth_crap
.new_lm_pswd_len
> 0) {
2829 new_lm_password
= data_blob_const(
2830 state
->request
->data
.chng_pswd_auth_crap
.new_lm_pswd
,
2831 state
->request
->data
.chng_pswd_auth_crap
.new_lm_pswd_len
);
2833 old_lm_hash_enc
= data_blob_const(
2834 state
->request
->data
.chng_pswd_auth_crap
.old_lm_hash_enc
,
2835 state
->request
->data
.chng_pswd_auth_crap
.old_lm_hash_enc_len
);
2837 new_lm_password
= data_blob_null
;
2838 old_lm_hash_enc
= data_blob_null
;
2841 /* Get sam handle */
2843 result
= cm_connect_sam(contact_domain
, state
->mem_ctx
, true, &cli
, &dom_pol
);
2844 if (!NT_STATUS_IS_OK(result
)) {
2845 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain
));
2849 b
= cli
->binding_handle
;
2851 result
= rpccli_samr_chng_pswd_auth_crap(
2852 cli
, state
->mem_ctx
, user
, new_nt_password
, old_nt_hash_enc
,
2853 new_lm_password
, old_lm_hash_enc
);
2857 if (strequal(contact_domain
->name
, get_global_sam_name())) {
2858 /* FIXME: internal rpc pipe does not cache handles yet */
2860 if (is_valid_policy_hnd(&dom_pol
)) {
2862 dcerpc_samr_Close(b
, state
->mem_ctx
, &dom_pol
, &_result
);
2868 set_auth_errors(state
->response
, result
);
2870 DEBUG(NT_STATUS_IS_OK(result
) ? 5 : 2,
2871 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
2873 state
->response
->data
.auth
.nt_status_string
,
2874 state
->response
->data
.auth
.pam_error
));
2876 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
2880 static NTSTATUS
extract_pac_vrfy_sigs(TALLOC_CTX
*mem_ctx
, DATA_BLOB pac_blob
,
2881 struct PAC_DATA
**p_pac_data
)
2883 krb5_context krbctx
= NULL
;
2884 krb5_error_code k5ret
;
2886 krb5_kt_cursor cursor
;
2887 krb5_keytab_entry entry
;
2888 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2891 ZERO_STRUCT(cursor
);
2893 k5ret
= krb5_init_context(&krbctx
);
2895 DEBUG(1, ("Failed to initialize kerberos context: %s\n",
2896 error_message(k5ret
)));
2897 status
= krb5_to_nt_status(k5ret
);
2901 k5ret
= gse_krb5_get_server_keytab(krbctx
, &keytab
);
2903 DEBUG(1, ("Failed to get keytab: %s\n",
2904 error_message(k5ret
)));
2905 status
= krb5_to_nt_status(k5ret
);
2909 k5ret
= krb5_kt_start_seq_get(krbctx
, keytab
, &cursor
);
2911 DEBUG(1, ("Failed to start seq: %s\n",
2912 error_message(k5ret
)));
2913 status
= krb5_to_nt_status(k5ret
);
2917 k5ret
= krb5_kt_next_entry(krbctx
, keytab
, &entry
, &cursor
);
2918 while (k5ret
== 0) {
2919 status
= kerberos_decode_pac(mem_ctx
,
2922 NULL
, /* krbtgt_keyblock */
2923 KRB5_KT_KEY(&entry
), /* service_keyblock */
2924 NULL
, /* client_principal */
2925 0, /* tgs_authtime */
2927 if (NT_STATUS_IS_OK(status
)) {
2930 k5ret
= smb_krb5_kt_free_entry(krbctx
, &entry
);
2931 k5ret
= krb5_kt_next_entry(krbctx
, keytab
, &entry
, &cursor
);
2934 k5ret
= krb5_kt_end_seq_get(krbctx
, keytab
, &cursor
);
2936 DEBUG(1, ("Failed to end seq: %s\n",
2937 error_message(k5ret
)));
2940 k5ret
= krb5_kt_close(krbctx
, keytab
);
2942 DEBUG(1, ("Failed to close keytab: %s\n",
2943 error_message(k5ret
)));
2946 krb5_free_context(krbctx
);
2951 NTSTATUS
winbindd_pam_auth_pac_verify(struct winbindd_cli_state
*state
,
2953 uint16_t *p_validation_level
,
2954 union netr_Validation
**p_validation
)
2956 struct winbindd_request
*req
= state
->request
;
2958 struct PAC_DATA
*pac_data
= NULL
;
2959 struct PAC_LOGON_INFO
*logon_info
= NULL
;
2960 struct PAC_UPN_DNS_INFO
*upn_dns_info
= NULL
;
2961 struct netr_SamInfo6
*info6
= NULL
;
2962 uint16_t validation_level
= 0;
2963 union netr_Validation
*validation
= NULL
;
2964 struct netr_SamInfo3
*info3_copy
= NULL
;
2966 bool is_trusted
= false;
2969 *p_is_trusted
= false;
2970 *p_validation_level
= 0;
2971 *p_validation
= NULL
;
2973 pac_blob
= data_blob_const(req
->extra_data
.data
, req
->extra_len
);
2974 result
= extract_pac_vrfy_sigs(state
->mem_ctx
, pac_blob
, &pac_data
);
2975 if (NT_STATUS_IS_OK(result
)) {
2978 if (NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
2979 /* Try without signature verification */
2980 result
= kerberos_decode_pac(state
->mem_ctx
,
2982 NULL
, /* krb5_context */
2983 NULL
, /* krbtgt_keyblock */
2984 NULL
, /* service_keyblock */
2985 NULL
, /* client_principal */
2986 0, /* tgs_authtime */
2989 if (!NT_STATUS_IS_OK(result
)) {
2990 DEBUG(1, ("Error during PAC signature verification: %s\n",
2991 nt_errstr(result
)));
2995 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
2996 if (pac_data
->buffers
[i
].type
== PAC_TYPE_LOGON_INFO
) {
2997 logon_info
= pac_data
->buffers
[i
].info
->logon_info
.info
;
3000 if (pac_data
->buffers
[i
].type
== PAC_TYPE_UPN_DNS_INFO
) {
3001 upn_dns_info
= &pac_data
->buffers
[i
].info
->upn_dns_info
;
3006 result
= create_info6_from_pac(state
->mem_ctx
,
3010 if (!NT_STATUS_IS_OK(result
)) {
3014 result
= map_info6_to_validation(state
->mem_ctx
,
3018 if (!NT_STATUS_IS_OK(result
)) {
3022 result
= map_validation_to_info3(state
->mem_ctx
,
3026 if (!NT_STATUS_IS_OK(result
)) {
3032 * Signature verification succeeded, we can
3033 * trust the PAC and prime the netsamlogon
3034 * and name2sid caches. DO NOT DO THIS
3035 * in the signature verification failed
3038 struct winbindd_domain
*domain
= NULL
;
3040 netsamlogon_cache_store(NULL
, info3_copy
);
3043 * We're in the parent here, so find the child
3044 * pointer from the PAC domain name.
3046 domain
= find_lookup_domain_from_name(
3047 info3_copy
->base
.logon_domain
.string
);
3048 if (domain
&& domain
->primary
) {
3049 struct dom_sid user_sid
;
3051 sid_compose(&user_sid
,
3052 info3_copy
->base
.domain_sid
,
3053 info3_copy
->base
.rid
);
3055 cache_name2sid_trusted(domain
,
3056 info3_copy
->base
.logon_domain
.string
,
3057 info3_copy
->base
.account_name
.string
,
3061 DBG_INFO("PAC for user %s\\%s SID %s primed cache\n",
3062 info3_copy
->base
.logon_domain
.string
,
3063 info3_copy
->base
.account_name
.string
,
3064 sid_string_dbg(&user_sid
));
3068 *p_is_trusted
= is_trusted
;
3069 *p_validation_level
= validation_level
;
3070 *p_validation
= validation
;
3071 return NT_STATUS_OK
;
3073 #else /* HAVE_KRB5 */
3074 NTSTATUS
winbindd_pam_auth_pac_verify(struct winbindd_cli_state
*state
,
3076 uint16_t *p_validation_level
,
3077 union netr_Validation
**p_validation
);
3080 *p_is_trusted
= false;
3081 *p_validation_level
= 0;
3082 *p_validation
= NULL
;
3083 return NT_STATUS_NO_SUCH_USER
;
3085 #endif /* HAVE_KRB5 */