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 "../libcli/security/security.h"
37 #include "../librpc/gen_ndr/krb5pac.h"
38 #include "passdb/machine_sid.h"
40 #include "../lib/tsocket/tsocket.h"
41 #include "auth/kerberos/pac_utils.h"
42 #include "auth/gensec/gensec.h"
43 #include "librpc/crypto/gse_krb5.h"
44 #include "lib/afs/afs_funcs.h"
45 #include "libsmb/samlogon_cache.h"
46 #include "rpc_client/util_netlogon.h"
47 #include "param/param.h"
48 #include "messaging/messaging.h"
49 #include "lib/crypto/gnutls_helpers.h"
51 #include "lib/crypto/gnutls_helpers.h"
52 #include <gnutls/crypto.h>
55 #define DBGC_CLASS DBGC_WINBIND
57 #define LOGON_KRB5_FAIL_CLOCK_SKEW 0x02000000
59 static NTSTATUS
append_info3_as_txt(TALLOC_CTX
*mem_ctx
,
60 struct winbindd_response
*resp
,
61 uint16_t validation_level
,
62 union netr_Validation
*validation
)
64 struct netr_SamInfo3
*info3
= NULL
;
67 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
68 TALLOC_CTX
*frame
= talloc_stackframe();
70 status
= map_validation_to_info3(frame
,
74 if (!NT_STATUS_IS_OK(status
)) {
78 resp
->data
.auth
.info3
.logon_time
=
79 nt_time_to_unix(info3
->base
.logon_time
);
80 resp
->data
.auth
.info3
.logoff_time
=
81 nt_time_to_unix(info3
->base
.logoff_time
);
82 resp
->data
.auth
.info3
.kickoff_time
=
83 nt_time_to_unix(info3
->base
.kickoff_time
);
84 resp
->data
.auth
.info3
.pass_last_set_time
=
85 nt_time_to_unix(info3
->base
.last_password_change
);
86 resp
->data
.auth
.info3
.pass_can_change_time
=
87 nt_time_to_unix(info3
->base
.allow_password_change
);
88 resp
->data
.auth
.info3
.pass_must_change_time
=
89 nt_time_to_unix(info3
->base
.force_password_change
);
91 resp
->data
.auth
.info3
.logon_count
= info3
->base
.logon_count
;
92 resp
->data
.auth
.info3
.bad_pw_count
= info3
->base
.bad_password_count
;
94 resp
->data
.auth
.info3
.user_rid
= info3
->base
.rid
;
95 resp
->data
.auth
.info3
.group_rid
= info3
->base
.primary_gid
;
96 sid_to_fstring(resp
->data
.auth
.info3
.dom_sid
, info3
->base
.domain_sid
);
98 resp
->data
.auth
.info3
.num_groups
= info3
->base
.groups
.count
;
99 resp
->data
.auth
.info3
.user_flgs
= info3
->base
.user_flags
;
101 resp
->data
.auth
.info3
.acct_flags
= info3
->base
.acct_flags
;
102 resp
->data
.auth
.info3
.num_other_sids
= info3
->sidcount
;
104 fstrcpy(resp
->data
.auth
.info3
.user_name
,
105 info3
->base
.account_name
.string
);
106 fstrcpy(resp
->data
.auth
.info3
.full_name
,
107 info3
->base
.full_name
.string
);
108 fstrcpy(resp
->data
.auth
.info3
.logon_script
,
109 info3
->base
.logon_script
.string
);
110 fstrcpy(resp
->data
.auth
.info3
.profile_path
,
111 info3
->base
.profile_path
.string
);
112 fstrcpy(resp
->data
.auth
.info3
.home_dir
,
113 info3
->base
.home_directory
.string
);
114 fstrcpy(resp
->data
.auth
.info3
.dir_drive
,
115 info3
->base
.home_drive
.string
);
117 fstrcpy(resp
->data
.auth
.info3
.logon_srv
,
118 info3
->base
.logon_server
.string
);
119 fstrcpy(resp
->data
.auth
.info3
.logon_dom
,
120 info3
->base
.logon_domain
.string
);
122 resp
->data
.auth
.validation_level
= validation_level
;
123 if (validation_level
== 6) {
124 fstrcpy(resp
->data
.auth
.info6
.dns_domainname
,
125 validation
->sam6
->dns_domainname
.string
);
126 fstrcpy(resp
->data
.auth
.info6
.principal_name
,
127 validation
->sam6
->principal_name
.string
);
130 ex
= talloc_strdup(frame
, "");
132 status
= NT_STATUS_NO_MEMORY
;
136 for (i
=0; i
< info3
->base
.groups
.count
; i
++) {
137 ex
= talloc_asprintf_append_buffer(ex
, "0x%08X:0x%08X\n",
138 info3
->base
.groups
.rids
[i
].rid
,
139 info3
->base
.groups
.rids
[i
].attributes
);
141 status
= NT_STATUS_NO_MEMORY
;
146 for (i
=0; i
< info3
->sidcount
; i
++) {
147 struct dom_sid_buf sidbuf
;
149 ex
= talloc_asprintf_append_buffer(
152 dom_sid_str_buf(info3
->sids
[i
].sid
, &sidbuf
),
153 info3
->sids
[i
].attributes
);
155 status
= NT_STATUS_NO_MEMORY
;
160 resp
->length
+= talloc_get_size(ex
);
161 resp
->extra_data
.data
= talloc_move(mem_ctx
, &ex
);
163 status
= NT_STATUS_OK
;
169 static NTSTATUS
append_info3_as_ndr(TALLOC_CTX
*mem_ctx
,
170 struct winbindd_response
*resp
,
171 struct netr_SamInfo3
*info3
)
174 enum ndr_err_code ndr_err
;
176 ndr_err
= ndr_push_struct_blob(&blob
, mem_ctx
, info3
,
177 (ndr_push_flags_fn_t
)ndr_push_netr_SamInfo3
);
178 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
179 DEBUG(0,("append_info3_as_ndr: failed to append\n"));
180 return ndr_map_error2ntstatus(ndr_err
);
183 resp
->extra_data
.data
= blob
.data
;
184 resp
->length
+= blob
.length
;
189 static NTSTATUS
append_unix_username(TALLOC_CTX
*mem_ctx
,
190 struct winbindd_response
*resp
,
191 const struct netr_SamInfo3
*info3
,
192 const char *name_domain
,
193 const char *name_user
)
195 /* We've been asked to return the unix username, per
196 'winbind use default domain' settings and the like */
198 const char *nt_username
, *nt_domain
, *unix_username
;
200 nt_domain
= talloc_strdup(mem_ctx
, info3
->base
.logon_domain
.string
);
202 /* If the server didn't give us one, just use the one
204 nt_domain
= name_domain
;
207 nt_username
= talloc_strdup(mem_ctx
, info3
->base
.account_name
.string
);
209 /* If the server didn't give us one, just use the one
211 nt_username
= name_user
;
214 unix_username
= fill_domain_username_talloc(mem_ctx
,
218 if (unix_username
== NULL
) {
219 return NT_STATUS_NO_MEMORY
;
222 fstrcpy(resp
->data
.auth
.unix_username
, unix_username
);
224 DEBUG(5, ("Setting unix username to [%s]\n",
225 resp
->data
.auth
.unix_username
));
230 static NTSTATUS
append_afs_token(TALLOC_CTX
*mem_ctx
,
231 struct winbindd_response
*resp
,
232 const struct netr_SamInfo3
*info3
,
233 const char *name_domain
,
234 const char *name_user
)
236 char *afsname
= NULL
;
240 afsname
= talloc_strdup(mem_ctx
, lp_afs_username_map());
241 if (afsname
== NULL
) {
242 return NT_STATUS_NO_MEMORY
;
245 afsname
= talloc_string_sub(mem_ctx
,
246 lp_afs_username_map(),
248 afsname
= talloc_string_sub(mem_ctx
, afsname
,
250 afsname
= talloc_string_sub(mem_ctx
, afsname
,
254 struct dom_sid user_sid
;
255 struct dom_sid_buf sidstr
;
257 sid_compose(&user_sid
, info3
->base
.domain_sid
,
259 afsname
= talloc_string_sub(
263 dom_sid_str_buf(&user_sid
, &sidstr
));
266 if (afsname
== NULL
) {
267 return NT_STATUS_NO_MEMORY
;
270 if (!strlower_m(afsname
)) {
271 return NT_STATUS_INVALID_PARAMETER
;
274 DEBUG(10, ("Generating token for user %s\n", afsname
));
276 cell
= strchr(afsname
, '@');
279 return NT_STATUS_NO_MEMORY
;
285 token
= afs_createtoken_str(afsname
, cell
);
289 resp
->extra_data
.data
= talloc_strdup(mem_ctx
, token
);
290 if (resp
->extra_data
.data
== NULL
) {
291 return NT_STATUS_NO_MEMORY
;
293 resp
->length
+= strlen((const char *)resp
->extra_data
.data
)+1;
298 static NTSTATUS
check_info3_in_group(struct netr_SamInfo3
*info3
,
299 const char *group_sid
)
301 * Check whether a user belongs to a group or list of groups.
303 * @param mem_ctx talloc memory context.
304 * @param info3 user information, including group membership info.
305 * @param group_sid One or more groups , separated by commas.
307 * @return NT_STATUS_OK on success,
308 * NT_STATUS_LOGON_FAILURE if the user does not belong,
309 * or other NT_STATUS_IS_ERR(status) for other kinds of failure.
312 struct dom_sid
*require_membership_of_sid
;
313 uint32_t num_require_membership_of_sid
;
318 struct security_token
*token
;
319 TALLOC_CTX
*frame
= talloc_stackframe();
322 /* Parse the 'required group' SID */
324 if (!group_sid
|| !group_sid
[0]) {
325 /* NO sid supplied, all users may access */
330 token
= talloc_zero(talloc_tos(), struct security_token
);
332 DEBUG(0, ("talloc failed\n"));
334 return NT_STATUS_NO_MEMORY
;
337 num_require_membership_of_sid
= 0;
338 require_membership_of_sid
= NULL
;
342 while (next_token_talloc(talloc_tos(), &p
, &req_sid
, ",")) {
343 if (!string_to_sid(&sid
, req_sid
)) {
344 DEBUG(0, ("check_info3_in_group: could not parse %s "
345 "as a SID!", req_sid
));
347 return NT_STATUS_INVALID_PARAMETER
;
350 status
= add_sid_to_array(talloc_tos(), &sid
,
351 &require_membership_of_sid
,
352 &num_require_membership_of_sid
);
353 if (!NT_STATUS_IS_OK(status
)) {
354 DEBUG(0, ("add_sid_to_array failed\n"));
360 status
= sid_array_from_info3(talloc_tos(), info3
,
364 if (!NT_STATUS_IS_OK(status
)) {
369 if (!NT_STATUS_IS_OK(status
= add_aliases(get_global_sam_sid(),
371 || !NT_STATUS_IS_OK(status
= add_aliases(&global_sid_Builtin
,
373 DEBUG(3, ("could not add aliases: %s\n",
379 security_token_debug(DBGC_CLASS
, 10, token
);
381 for (i
=0; i
<num_require_membership_of_sid
; i
++) {
382 struct dom_sid_buf buf
;
383 DEBUG(10, ("Checking SID %s\n",
384 dom_sid_str_buf(&require_membership_of_sid
[i
],
386 if (nt_token_check_sid(&require_membership_of_sid
[i
],
388 DEBUG(10, ("Access ok\n"));
394 /* Do not distinguish this error from a wrong username/pw */
397 return NT_STATUS_LOGON_FAILURE
;
400 struct winbindd_domain
*find_auth_domain(uint8_t flags
,
401 const char *domain_name
)
403 struct winbindd_domain
*domain
;
406 domain
= find_domain_from_name_noinit(domain_name
);
407 if (domain
== NULL
) {
408 DEBUG(3, ("Authentication for domain [%s] refused "
409 "as it is not a trusted domain\n",
414 if (domain
->secure_channel_type
!= SEC_CHAN_NULL
) {
418 return domain
->routing_domain
;
421 if (strequal(domain_name
, get_global_sam_name())) {
422 return find_domain_from_name_noinit(domain_name
);
425 if (lp_winbind_use_krb5_enterprise_principals()) {
427 * If we use enterprise principals
428 * we always go trough our primary domain
429 * and follow the WRONG_REALM replies.
431 flags
&= ~WBFLAG_PAM_CONTACT_TRUSTDOM
;
434 /* we can auth against trusted domains */
435 if (flags
& WBFLAG_PAM_CONTACT_TRUSTDOM
) {
436 domain
= find_domain_from_name_noinit(domain_name
);
437 if (domain
== NULL
) {
438 DEBUG(3, ("Authentication for domain [%s] skipped "
439 "as it is not a trusted domain\n",
446 return find_our_domain();
449 static void fake_password_policy(struct winbindd_response
*r
,
450 const struct netr_SamBaseInfo
*bi
)
452 NTTIME min_password_age
;
453 NTTIME max_password_age
;
455 if (bi
->allow_password_change
> bi
->last_password_change
) {
456 min_password_age
= bi
->allow_password_change
-
457 bi
->last_password_change
;
459 min_password_age
= 0;
462 if (bi
->force_password_change
> bi
->last_password_change
) {
463 max_password_age
= bi
->force_password_change
-
464 bi
->last_password_change
;
466 max_password_age
= 0;
469 r
->data
.auth
.policy
.min_length_password
= 0;
470 r
->data
.auth
.policy
.password_history
= 0;
471 r
->data
.auth
.policy
.password_properties
= 0;
472 r
->data
.auth
.policy
.expire
=
473 nt_time_to_unix_abs(&max_password_age
);
474 r
->data
.auth
.policy
.min_passwordage
=
475 nt_time_to_unix_abs(&min_password_age
);
478 static void fill_in_password_policy(struct winbindd_response
*r
,
479 const struct samr_DomInfo1
*p
)
481 r
->data
.auth
.policy
.min_length_password
=
482 p
->min_password_length
;
483 r
->data
.auth
.policy
.password_history
=
484 p
->password_history_length
;
485 r
->data
.auth
.policy
.password_properties
=
486 p
->password_properties
;
487 r
->data
.auth
.policy
.expire
=
488 nt_time_to_unix_abs((const NTTIME
*)&(p
->max_password_age
));
489 r
->data
.auth
.policy
.min_passwordage
=
490 nt_time_to_unix_abs((const NTTIME
*)&(p
->min_password_age
));
493 static NTSTATUS
fillup_password_policy(struct winbindd_domain
*domain
,
494 struct winbindd_response
*response
)
496 TALLOC_CTX
*frame
= talloc_stackframe();
498 struct samr_DomInfo1 password_policy
;
500 if ( !winbindd_can_contact_domain( domain
) ) {
501 DEBUG(5,("fillup_password_policy: No inbound trust to "
502 "contact domain %s\n", domain
->name
));
503 status
= NT_STATUS_NOT_SUPPORTED
;
507 status
= wb_cache_password_policy(domain
, talloc_tos(),
509 if (NT_STATUS_IS_ERR(status
)) {
513 fill_in_password_policy(response
, &password_policy
);
520 static NTSTATUS
get_max_bad_attempts_from_lockout_policy(struct winbindd_domain
*domain
,
522 uint16_t *lockout_threshold
)
524 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
525 struct samr_DomInfo12 lockout_policy
;
527 *lockout_threshold
= 0;
529 status
= wb_cache_lockout_policy(domain
, mem_ctx
, &lockout_policy
);
530 if (NT_STATUS_IS_ERR(status
)) {
534 *lockout_threshold
= lockout_policy
.lockout_threshold
;
539 static NTSTATUS
get_pwd_properties(struct winbindd_domain
*domain
,
541 uint32_t *password_properties
)
543 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
544 struct samr_DomInfo1 password_policy
;
546 *password_properties
= 0;
548 status
= wb_cache_password_policy(domain
, mem_ctx
, &password_policy
);
549 if (NT_STATUS_IS_ERR(status
)) {
553 *password_properties
= password_policy
.password_properties
;
560 static const char *generate_krb5_ccache(TALLOC_CTX
*mem_ctx
,
563 const char **user_ccache_file
)
565 /* accept FILE and WRFILE as krb5_cc_type from the client and then
566 * build the full ccname string based on the user's uid here -
569 const char *gen_cc
= NULL
;
572 if (strequal(type
, "FILE")) {
573 gen_cc
= talloc_asprintf(
574 mem_ctx
, "FILE:/tmp/krb5cc_%d", uid
);
576 if (strequal(type
, "WRFILE")) {
577 gen_cc
= talloc_asprintf(
578 mem_ctx
, "WRFILE:/tmp/krb5cc_%d", uid
);
580 if (strequal(type
, "KEYRING")) {
581 gen_cc
= talloc_asprintf(
582 mem_ctx
, "KEYRING:persistent:%d", uid
);
584 if (strequal(type
, "KCM")) {
585 gen_cc
= talloc_asprintf(mem_ctx
,
590 if (strnequal(type
, "FILE:/", 6) ||
591 strnequal(type
, "WRFILE:/", 8) ||
592 strnequal(type
, "DIR:/", 5)) {
594 /* we allow only one "%u" substitution */
598 p
= strchr(type
, '%');
603 if (p
!= NULL
&& *p
== 'u' && strchr(p
, '%') == NULL
) {
604 char uid_str
[sizeof("18446744073709551615")];
606 snprintf(uid_str
, sizeof(uid_str
), "%u", uid
);
608 gen_cc
= talloc_string_sub2(mem_ctx
,
612 /* remove_unsafe_characters */
616 /* allow_trailing_dollar */
623 *user_ccache_file
= gen_cc
;
625 if (gen_cc
== NULL
) {
626 gen_cc
= talloc_strdup(mem_ctx
, "MEMORY:winbindd_pam_ccache");
628 if (gen_cc
== NULL
) {
629 DEBUG(0,("out of memory\n"));
633 DEBUG(10, ("using ccache: %s%s\n", gen_cc
,
634 (*user_ccache_file
== NULL
) ? " (internal)":""));
641 uid_t
get_uid_from_request(struct winbindd_request
*request
)
645 uid
= request
->data
.auth
.uid
;
647 if (uid
== (uid_t
)-1) {
648 DEBUG(1,("invalid uid: '%u'\n", (unsigned int)uid
));
654 /**********************************************************************
655 Authenticate a user with a clear text password using Kerberos and fill up
657 **********************************************************************/
659 static NTSTATUS
winbindd_raw_kerberos_login(TALLOC_CTX
*mem_ctx
,
660 struct winbindd_domain
*domain
,
663 const char *krb5_cc_type
,
665 struct netr_SamInfo6
**info6
,
669 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
670 krb5_error_code krb5_ret
;
671 const char *cc
= NULL
;
672 const char *principal_s
= NULL
;
673 const char *service
= NULL
;
675 fstring name_namespace
, name_domain
, name_user
;
676 time_t ticket_lifetime
= 0;
677 time_t renewal_until
= 0;
679 time_t time_offset
= 0;
680 const char *user_ccache_file
;
681 struct PAC_LOGON_INFO
*logon_info
= NULL
;
682 struct PAC_UPN_DNS_INFO
*upn_dns_info
= NULL
;
683 struct PAC_DATA
*pac_data
= NULL
;
684 struct PAC_DATA_CTR
*pac_data_ctr
= NULL
;
685 const char *local_service
;
687 struct netr_SamInfo6
*info6_copy
= NULL
;
692 if (domain
->alt_name
== NULL
) {
693 return NT_STATUS_INVALID_PARAMETER
;
697 * prepare a krb5_cc_cache string for the user */
700 DEBUG(0,("no valid uid\n"));
703 cc
= generate_krb5_ccache(mem_ctx
,
708 return NT_STATUS_NO_MEMORY
;
713 * get kerberos properties */
715 if (domain
->private_data
) {
716 ads
= (ADS_STRUCT
*)domain
->private_data
;
717 time_offset
= ads
->auth
.time_offset
;
722 * do kerberos auth and setup ccache as the user */
724 ok
= parse_domain_user(user
, name_namespace
, name_domain
, name_user
);
726 return NT_STATUS_INVALID_PARAMETER
;
729 realm
= talloc_strdup(mem_ctx
, domain
->alt_name
);
731 return NT_STATUS_NO_MEMORY
;
734 if (!strupper_m(realm
)) {
735 return NT_STATUS_INVALID_PARAMETER
;
738 if (lp_winbind_use_krb5_enterprise_principals() &&
739 name_namespace
[0] != '\0')
741 principal_s
= talloc_asprintf(mem_ctx
,
747 principal_s
= talloc_asprintf(mem_ctx
,
752 if (principal_s
== NULL
) {
753 return NT_STATUS_NO_MEMORY
;
756 service
= talloc_asprintf(mem_ctx
, "%s/%s@%s", KRB5_TGS_NAME
, realm
, realm
);
757 if (service
== NULL
) {
758 return NT_STATUS_NO_MEMORY
;
761 local_service
= talloc_asprintf(mem_ctx
, "%s$@%s",
762 lp_netbios_name(), lp_realm());
763 if (local_service
== NULL
) {
764 return NT_STATUS_NO_MEMORY
;
768 /* if this is a user ccache, we need to act as the user to let the krb5
769 * library handle the chown, etc. */
771 /************************ ENTERING NON-ROOT **********************/
773 if (user_ccache_file
!= NULL
) {
774 set_effective_uid(uid
);
775 DEBUG(10,("winbindd_raw_kerberos_login: uid is %d\n", uid
));
778 result
= kerberos_return_pac(mem_ctx
,
787 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
791 if (user_ccache_file
!= NULL
) {
792 gain_root_privilege();
795 /************************ RETURNED TO ROOT **********************/
797 if (!NT_STATUS_IS_OK(result
)) {
801 if (pac_data_ctr
== NULL
) {
805 pac_data
= pac_data_ctr
->pac_data
;
806 if (pac_data
== NULL
) {
810 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
812 if (pac_data
->buffers
[i
].type
== PAC_TYPE_LOGON_INFO
) {
813 logon_info
= pac_data
->buffers
[i
].info
->logon_info
.info
;
817 if (pac_data
->buffers
[i
].type
== PAC_TYPE_UPN_DNS_INFO
) {
818 upn_dns_info
= &pac_data
->buffers
[i
].info
->upn_dns_info
;
823 if (logon_info
== NULL
) {
824 DEBUG(10,("Missing logon_info in ticket of %s\n",
826 return NT_STATUS_INVALID_PARAMETER
;
829 DEBUG(10,("winbindd_raw_kerberos_login: winbindd validated ticket of %s\n",
832 result
= create_info6_from_pac(mem_ctx
, logon_info
,
833 upn_dns_info
, &info6_copy
);
834 if (!NT_STATUS_IS_OK(result
)) {
838 /* if we had a user's ccache then return that string for the pam
841 if (user_ccache_file
!= NULL
) {
843 fstrcpy(krb5ccname
, user_ccache_file
);
845 result
= add_ccache_to_list(principal_s
,
857 if (!NT_STATUS_IS_OK(result
)) {
858 DEBUG(10,("winbindd_raw_kerberos_login: failed to add ccache to list: %s\n",
863 /* need to delete the memory cred cache, it is not used anymore */
865 krb5_ret
= ads_kdestroy(cc
);
867 DEBUG(3,("winbindd_raw_kerberos_login: "
868 "could not destroy krb5 credential cache: "
869 "%s\n", error_message(krb5_ret
)));
878 * Do not delete an existing valid credential cache, if the user
879 * e.g. enters a wrong password
881 if ((strequal(krb5_cc_type
, "FILE") || strequal(krb5_cc_type
, "WRFILE"))
882 && user_ccache_file
!= NULL
) {
886 /* we could have created a new credential cache with a valid tgt in it
887 * but we werent able to get or verify the service ticket for this
888 * local host and therefor didn't get the PAC, we need to remove that
889 * cache entirely now */
891 krb5_ret
= ads_kdestroy(cc
);
893 DEBUG(3,("winbindd_raw_kerberos_login: "
894 "could not destroy krb5 credential cache: "
895 "%s\n", error_message(krb5_ret
)));
898 if (!NT_STATUS_IS_OK(remove_ccache(user
))) {
899 DEBUG(3,("winbindd_raw_kerberos_login: "
900 "could not remove ccache for user %s\n",
906 return NT_STATUS_NOT_SUPPORTED
;
907 #endif /* HAVE_KRB5 */
910 /****************************************************************
911 ****************************************************************/
913 bool check_request_flags(uint32_t flags
)
915 uint32_t flags_edata
= WBFLAG_PAM_AFS_TOKEN
|
916 WBFLAG_PAM_INFO3_TEXT
|
917 WBFLAG_PAM_INFO3_NDR
;
919 if ( ( (flags
& flags_edata
) == WBFLAG_PAM_AFS_TOKEN
) ||
920 ( (flags
& flags_edata
) == WBFLAG_PAM_INFO3_NDR
) ||
921 ( (flags
& flags_edata
) == WBFLAG_PAM_INFO3_TEXT
)||
922 !(flags
& flags_edata
) ) {
926 DEBUG(1, ("check_request_flags: invalid request flags[0x%08X]\n",
932 /****************************************************************
933 ****************************************************************/
935 NTSTATUS
append_auth_data(TALLOC_CTX
*mem_ctx
,
936 struct winbindd_response
*resp
,
937 uint32_t request_flags
,
938 uint16_t validation_level
,
939 union netr_Validation
*validation
,
940 const char *name_domain
,
941 const char *name_user
)
943 struct netr_SamInfo3
*info3
= NULL
;
944 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
946 result
= map_validation_to_info3(talloc_tos(),
950 if (!NT_STATUS_IS_OK(result
)) {
954 if (request_flags
& WBFLAG_PAM_USER_SESSION_KEY
) {
955 memcpy(resp
->data
.auth
.user_session_key
,
957 sizeof(resp
->data
.auth
.user_session_key
)
961 if (request_flags
& WBFLAG_PAM_LMKEY
) {
962 memcpy(resp
->data
.auth
.first_8_lm_hash
,
963 info3
->base
.LMSessKey
.key
,
964 sizeof(resp
->data
.auth
.first_8_lm_hash
)
968 if (request_flags
& WBFLAG_PAM_UNIX_NAME
) {
969 result
= append_unix_username(mem_ctx
, resp
,
970 info3
, name_domain
, name_user
);
971 if (!NT_STATUS_IS_OK(result
)) {
972 DEBUG(10,("Failed to append Unix Username: %s\n",
978 /* currently, anything from here on potentially overwrites extra_data. */
980 if (request_flags
& WBFLAG_PAM_INFO3_NDR
) {
981 result
= append_info3_as_ndr(mem_ctx
, resp
, info3
);
982 if (!NT_STATUS_IS_OK(result
)) {
983 DEBUG(10,("Failed to append INFO3 (NDR): %s\n",
989 if (request_flags
& WBFLAG_PAM_INFO3_TEXT
) {
990 result
= append_info3_as_txt(mem_ctx
, resp
,
993 if (!NT_STATUS_IS_OK(result
)) {
994 DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
1000 if (request_flags
& WBFLAG_PAM_AFS_TOKEN
) {
1001 result
= append_afs_token(mem_ctx
, resp
,
1002 info3
, name_domain
, name_user
);
1003 if (!NT_STATUS_IS_OK(result
)) {
1004 DEBUG(10,("Failed to append AFS token: %s\n",
1005 nt_errstr(result
)));
1010 result
= NT_STATUS_OK
;
1016 static NTSTATUS
winbindd_dual_pam_auth_cached(struct winbindd_domain
*domain
,
1017 struct winbindd_cli_state
*state
,
1018 struct netr_SamInfo3
**info3
)
1020 NTSTATUS result
= NT_STATUS_LOGON_FAILURE
;
1021 uint16_t max_allowed_bad_attempts
;
1022 fstring name_namespace
, name_domain
, name_user
;
1024 enum lsa_SidType type
;
1025 uchar new_nt_pass
[NT_HASH_LEN
];
1026 const uint8_t *cached_nt_pass
;
1027 const uint8_t *cached_salt
;
1028 struct netr_SamInfo3
*my_info3
;
1029 time_t kickoff_time
, must_change_time
;
1030 bool password_good
= false;
1033 struct winbindd_tdc_domain
*tdc_domain
= NULL
;
1038 ZERO_STRUCTP(info3
);
1040 DEBUG(10,("winbindd_dual_pam_auth_cached\n"));
1042 /* Parse domain and username */
1044 ok
= parse_domain_user(state
->request
->data
.auth
.user
,
1049 DBG_DEBUG("parse_domain_user failed\n");
1050 return NT_STATUS_NO_SUCH_USER
;
1053 if (!lookup_cached_name(name_namespace
,
1058 DEBUG(10,("winbindd_dual_pam_auth_cached: no such user in the cache\n"));
1059 return NT_STATUS_NO_SUCH_USER
;
1062 if (type
!= SID_NAME_USER
) {
1063 DEBUG(10,("winbindd_dual_pam_auth_cached: not a user (%s)\n", sid_type_lookup(type
)));
1064 return NT_STATUS_LOGON_FAILURE
;
1067 result
= winbindd_get_creds(domain
,
1073 if (!NT_STATUS_IS_OK(result
)) {
1074 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get creds: %s\n", nt_errstr(result
)));
1080 E_md4hash(state
->request
->data
.auth
.pass
, new_nt_pass
);
1082 dump_data_pw("new_nt_pass", new_nt_pass
, NT_HASH_LEN
);
1083 dump_data_pw("cached_nt_pass", cached_nt_pass
, NT_HASH_LEN
);
1085 dump_data_pw("cached_salt", cached_salt
, NT_HASH_LEN
);
1089 /* In this case we didn't store the nt_hash itself,
1090 but the MD5 combination of salt + nt_hash. */
1091 uchar salted_hash
[NT_HASH_LEN
];
1092 gnutls_hash_hd_t hash_hnd
= NULL
;
1095 rc
= gnutls_hash_init(&hash_hnd
, GNUTLS_DIG_MD5
);
1097 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
);
1100 rc
= gnutls_hash(hash_hnd
, cached_salt
, 16);
1102 gnutls_hash_deinit(hash_hnd
, NULL
);
1103 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
);
1105 rc
= gnutls_hash(hash_hnd
, new_nt_pass
, 16);
1107 gnutls_hash_deinit(hash_hnd
, NULL
);
1108 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
);
1110 gnutls_hash_deinit(hash_hnd
, salted_hash
);
1112 password_good
= (memcmp(cached_nt_pass
, salted_hash
,
1115 /* Old cached cred - direct store of nt_hash (bad bad bad !). */
1116 password_good
= (memcmp(cached_nt_pass
, new_nt_pass
,
1120 if (password_good
) {
1122 /* User *DOES* know the password, update logon_time and reset
1125 my_info3
->base
.user_flags
|= NETLOGON_CACHED_ACCOUNT
;
1127 if (my_info3
->base
.acct_flags
& ACB_AUTOLOCK
) {
1128 return NT_STATUS_ACCOUNT_LOCKED_OUT
;
1131 if (my_info3
->base
.acct_flags
& ACB_DISABLED
) {
1132 return NT_STATUS_ACCOUNT_DISABLED
;
1135 if (my_info3
->base
.acct_flags
& ACB_WSTRUST
) {
1136 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
;
1139 if (my_info3
->base
.acct_flags
& ACB_SVRTRUST
) {
1140 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT
;
1143 if (my_info3
->base
.acct_flags
& ACB_DOMTRUST
) {
1144 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
;
1147 if (!(my_info3
->base
.acct_flags
& ACB_NORMAL
)) {
1148 DEBUG(0,("winbindd_dual_pam_auth_cached: whats wrong with that one?: 0x%08x\n",
1149 my_info3
->base
.acct_flags
));
1150 return NT_STATUS_LOGON_FAILURE
;
1153 kickoff_time
= nt_time_to_unix(my_info3
->base
.kickoff_time
);
1154 if (kickoff_time
!= 0 && time(NULL
) > kickoff_time
) {
1155 return NT_STATUS_ACCOUNT_EXPIRED
;
1158 must_change_time
= nt_time_to_unix(my_info3
->base
.force_password_change
);
1159 if (must_change_time
!= 0 && must_change_time
< time(NULL
)) {
1160 /* we allow grace logons when the password has expired */
1161 my_info3
->base
.user_flags
|= NETLOGON_GRACE_LOGON
;
1162 /* return NT_STATUS_PASSWORD_EXPIRED; */
1167 if ((state
->request
->flags
& WBFLAG_PAM_KRB5
) &&
1168 ((tdc_domain
= wcache_tdc_fetch_domain(state
->mem_ctx
, name_domain
)) != NULL
) &&
1169 ((tdc_domain
->trust_type
& LSA_TRUST_TYPE_UPLEVEL
) ||
1170 /* used to cope with the case winbindd starting without network. */
1171 !strequal(tdc_domain
->domain_name
, tdc_domain
->dns_name
))) {
1174 const char *cc
= NULL
;
1176 const char *principal_s
= NULL
;
1177 const char *service
= NULL
;
1178 const char *user_ccache_file
;
1180 if (domain
->alt_name
== NULL
) {
1181 return NT_STATUS_INVALID_PARAMETER
;
1184 uid
= get_uid_from_request(state
->request
);
1186 DEBUG(0,("winbindd_dual_pam_auth_cached: invalid uid\n"));
1187 return NT_STATUS_INVALID_PARAMETER
;
1190 cc
= generate_krb5_ccache(state
->mem_ctx
,
1191 state
->request
->data
.auth
.krb5_cc_type
,
1192 state
->request
->data
.auth
.uid
,
1195 return NT_STATUS_NO_MEMORY
;
1198 realm
= talloc_strdup(state
->mem_ctx
, domain
->alt_name
);
1199 if (realm
== NULL
) {
1200 return NT_STATUS_NO_MEMORY
;
1203 if (!strupper_m(realm
)) {
1204 return NT_STATUS_INVALID_PARAMETER
;
1207 principal_s
= talloc_asprintf(state
->mem_ctx
, "%s@%s", name_user
, realm
);
1208 if (principal_s
== NULL
) {
1209 return NT_STATUS_NO_MEMORY
;
1212 service
= talloc_asprintf(state
->mem_ctx
, "%s/%s@%s", KRB5_TGS_NAME
, realm
, realm
);
1213 if (service
== NULL
) {
1214 return NT_STATUS_NO_MEMORY
;
1217 if (user_ccache_file
!= NULL
) {
1219 fstrcpy(state
->response
->data
.auth
.krb5ccname
,
1222 result
= add_ccache_to_list(principal_s
,
1225 state
->request
->data
.auth
.user
,
1226 state
->request
->data
.auth
.pass
,
1230 time(NULL
) + lp_winbind_cache_time(),
1231 time(NULL
) + WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
1234 if (!NT_STATUS_IS_OK(result
)) {
1235 DEBUG(10,("winbindd_dual_pam_auth_cached: failed "
1236 "to add ccache to list: %s\n",
1237 nt_errstr(result
)));
1241 #endif /* HAVE_KRB5 */
1243 /* FIXME: we possibly should handle logon hours as well (does xp when
1244 * offline?) see auth/auth_sam.c:sam_account_ok for details */
1246 unix_to_nt_time(&my_info3
->base
.logon_time
, time(NULL
));
1247 my_info3
->base
.bad_password_count
= 0;
1249 result
= winbindd_update_creds_by_info3(domain
,
1250 state
->request
->data
.auth
.user
,
1251 state
->request
->data
.auth
.pass
,
1253 if (!NT_STATUS_IS_OK(result
)) {
1254 DEBUG(1,("winbindd_dual_pam_auth_cached: failed to update creds: %s\n",
1255 nt_errstr(result
)));
1259 return NT_STATUS_OK
;
1263 /* User does *NOT* know the correct password, modify info3 accordingly, but only if online */
1264 if (domain
->online
== false) {
1268 /* failure of this is not critical */
1269 result
= get_max_bad_attempts_from_lockout_policy(domain
, state
->mem_ctx
, &max_allowed_bad_attempts
);
1270 if (!NT_STATUS_IS_OK(result
)) {
1271 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get max_allowed_bad_attempts. "
1272 "Won't be able to honour account lockout policies\n"));
1275 /* increase counter */
1276 my_info3
->base
.bad_password_count
++;
1278 if (max_allowed_bad_attempts
== 0) {
1283 if (my_info3
->base
.bad_password_count
>= max_allowed_bad_attempts
) {
1285 uint32_t password_properties
;
1287 result
= get_pwd_properties(domain
, state
->mem_ctx
, &password_properties
);
1288 if (!NT_STATUS_IS_OK(result
)) {
1289 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get password properties.\n"));
1292 if ((my_info3
->base
.rid
!= DOMAIN_RID_ADMINISTRATOR
) ||
1293 (password_properties
& DOMAIN_PASSWORD_LOCKOUT_ADMINS
)) {
1294 my_info3
->base
.acct_flags
|= ACB_AUTOLOCK
;
1299 result
= winbindd_update_creds_by_info3(domain
,
1300 state
->request
->data
.auth
.user
,
1304 if (!NT_STATUS_IS_OK(result
)) {
1305 DEBUG(0,("winbindd_dual_pam_auth_cached: failed to update creds %s\n",
1306 nt_errstr(result
)));
1309 return NT_STATUS_LOGON_FAILURE
;
1312 static NTSTATUS
winbindd_dual_pam_auth_kerberos(struct winbindd_domain
*domain
,
1313 struct winbindd_cli_state
*state
,
1314 struct netr_SamInfo6
**info6
)
1316 struct winbindd_domain
*contact_domain
;
1317 fstring name_namespace
, name_domain
, name_user
;
1321 DEBUG(10,("winbindd_dual_pam_auth_kerberos\n"));
1323 /* Parse domain and username */
1325 ok
= parse_domain_user(state
->request
->data
.auth
.user
,
1330 result
= NT_STATUS_INVALID_PARAMETER
;
1334 /* what domain should we contact? */
1336 if (lp_winbind_use_krb5_enterprise_principals()) {
1337 contact_domain
= find_auth_domain(0, name_namespace
);
1339 contact_domain
= find_domain_from_name(name_namespace
);
1341 if (contact_domain
== NULL
) {
1342 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1343 state
->request
->data
.auth
.user
, name_domain
, name_user
, name_namespace
));
1344 result
= NT_STATUS_NO_SUCH_USER
;
1348 if (contact_domain
->initialized
&&
1349 contact_domain
->active_directory
) {
1353 if (!contact_domain
->initialized
) {
1354 init_dc_connection(contact_domain
, false);
1357 if (!contact_domain
->active_directory
) {
1358 DEBUG(3,("krb5 auth requested but domain (%s) is not Active Directory\n",
1359 contact_domain
->name
));
1360 return NT_STATUS_INVALID_LOGON_TYPE
;
1363 result
= winbindd_raw_kerberos_login(
1364 state
->mem_ctx
, contact_domain
,
1365 state
->request
->data
.auth
.user
,
1366 state
->request
->data
.auth
.pass
,
1367 state
->request
->data
.auth
.krb5_cc_type
,
1368 get_uid_from_request(state
->request
),
1369 info6
, state
->response
->data
.auth
.krb5ccname
);
1374 static NTSTATUS
winbindd_dual_auth_passdb(TALLOC_CTX
*mem_ctx
,
1375 uint32_t logon_parameters
,
1378 const uint64_t logon_id
,
1379 const char *client_name
,
1380 const int client_pid
,
1381 const DATA_BLOB
*challenge
,
1382 const DATA_BLOB
*lm_resp
,
1383 const DATA_BLOB
*nt_resp
,
1384 const struct tsocket_address
*remote
,
1385 const struct tsocket_address
*local
,
1387 uint8_t *pauthoritative
,
1388 struct netr_SamInfo3
**pinfo3
)
1390 struct auth_context
*auth_context
;
1391 struct auth_serversupplied_info
*server_info
;
1392 struct auth_usersupplied_info
*user_info
= NULL
;
1393 struct netr_SamInfo3
*info3
;
1396 TALLOC_CTX
*frame
= talloc_stackframe();
1399 * We are authoritative by default
1401 *pauthoritative
= 1;
1403 status
= make_user_info(frame
, &user_info
, user
, user
, domain
, domain
,
1404 lp_netbios_name(), remote
, local
,
1406 lm_resp
, nt_resp
, NULL
, NULL
,
1407 NULL
, AUTH_PASSWORD_RESPONSE
);
1408 if (!NT_STATUS_IS_OK(status
)) {
1409 DEBUG(10, ("make_user_info failed: %s\n", nt_errstr(status
)));
1414 user_info
->logon_parameters
= logon_parameters
;
1415 user_info
->logon_id
= logon_id
;
1416 user_info
->auth_description
= talloc_asprintf(
1417 frame
, "PASSDB, %s, %d", client_name
, client_pid
);
1418 if (user_info
->auth_description
== NULL
) {
1420 return NT_STATUS_NO_MEMORY
;
1423 /* We don't want any more mapping of the username */
1424 user_info
->mapped_state
= True
;
1426 /* We don't want to come back to winbindd or to do PAM account checks */
1427 user_info
->flags
|= USER_INFO_INFO3_AND_NO_AUTHZ
;
1430 user_info
->flags
|= USER_INFO_INTERACTIVE_LOGON
;
1433 status
= make_auth3_context_for_winbind(frame
, &auth_context
);
1434 if (!NT_STATUS_IS_OK(status
)) {
1435 DBG_ERR("make_auth3_context_for_winbind failed: %s\n",
1441 ok
= auth3_context_set_challenge(auth_context
,
1442 challenge
->data
, "fixed");
1445 return NT_STATUS_NO_MEMORY
;
1448 status
= auth_check_ntlm_password(mem_ctx
,
1453 if (!NT_STATUS_IS_OK(status
)) {
1458 info3
= talloc_zero(mem_ctx
, struct netr_SamInfo3
);
1459 if (info3
== NULL
) {
1461 return NT_STATUS_NO_MEMORY
;
1464 status
= serverinfo_to_SamInfo3(server_info
, info3
);
1465 if (!NT_STATUS_IS_OK(status
)) {
1468 DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
1469 nt_errstr(status
)));
1474 DEBUG(10, ("Authenticaticating user %s\\%s returned %s\n", domain
,
1475 user
, nt_errstr(status
)));
1480 static NTSTATUS
winbind_samlogon_retry_loop(struct winbindd_domain
*domain
,
1481 TALLOC_CTX
*mem_ctx
,
1482 uint32_t logon_parameters
,
1483 const char *username
,
1484 const char *password
,
1485 const char *domainname
,
1486 const char *workstation
,
1487 const uint64_t logon_id
,
1488 bool plaintext_given
,
1489 const uint8_t chal
[8],
1490 DATA_BLOB lm_response
,
1491 DATA_BLOB nt_response
,
1493 uint8_t *authoritative
,
1495 uint16_t *_validation_level
,
1496 union netr_Validation
**_validation
)
1499 int netr_attempts
= 0;
1502 enum netr_LogonInfoClass logon_type_i
;
1503 enum netr_LogonInfoClass logon_type_n
;
1504 uint16_t validation_level
= UINT16_MAX
;
1505 union netr_Validation
*validation
= NULL
;
1508 struct rpc_pipe_client
*netlogon_pipe
;
1509 struct netlogon_creds_cli_context
*netlogon_creds_ctx
= NULL
;
1513 result
= cm_connect_netlogon_secure(domain
, &netlogon_pipe
,
1514 &netlogon_creds_ctx
);
1516 if (NT_STATUS_EQUAL(result
,
1517 NT_STATUS_CANT_ACCESS_DOMAIN_INFO
)) {
1519 * This means we don't have a trust account.
1522 result
= NT_STATUS_NO_SUCH_USER
;
1526 if (!NT_STATUS_IS_OK(result
)) {
1527 DEBUG(3,("Could not open handle to NETLOGON pipe "
1528 "(error: %s, attempts: %d)\n",
1529 nt_errstr(result
), netr_attempts
));
1531 /* After the first retry always close the connection */
1532 if (netr_attempts
> 0) {
1533 DEBUG(3, ("This is again a problem for this "
1534 "particular call, forcing the close "
1535 "of this connection\n"));
1536 invalidate_cm_connection(domain
);
1539 /* After the second retry failover to the next DC */
1540 if (netr_attempts
> 1) {
1542 * If the netlogon server is not reachable then
1543 * it is possible that the DC is rebuilding
1544 * sysvol and shutdown netlogon for that time.
1545 * We should failover to the next dc.
1547 DEBUG(3, ("This is the third problem for this "
1548 "particular call, adding DC to the "
1549 "negative cache list: %s %s\n", domain
->name
, domain
->dcname
));
1550 add_failed_connection_entry(domain
->name
,
1553 saf_delete(domain
->name
);
1556 /* Only allow 3 retries */
1557 if (netr_attempts
< 3) {
1558 DEBUG(3, ("The connection to netlogon "
1559 "failed, retrying\n"));
1567 logon_type_i
= NetlogonInteractiveInformation
;
1568 logon_type_n
= NetlogonNetworkInformation
;
1569 if (domain
->domain_trust_attribs
& LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
) {
1570 logon_type_i
= NetlogonInteractiveTransitiveInformation
;
1571 logon_type_n
= NetlogonNetworkTransitiveInformation
;
1574 if (domain
->domain_trust_attribs
& LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE
) {
1575 logon_type_i
= NetlogonInteractiveTransitiveInformation
;
1576 logon_type_n
= NetlogonNetworkTransitiveInformation
;
1579 if (domain
->domain_trust_attribs
& LSA_TRUST_ATTRIBUTE_NON_TRANSITIVE
) {
1580 logon_type_i
= NetlogonInteractiveInformation
;
1581 logon_type_n
= NetlogonNetworkInformation
;
1584 if (domain
->domain_trust_attribs
& LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN
) {
1585 logon_type_i
= NetlogonInteractiveInformation
;
1586 logon_type_n
= NetlogonNetworkInformation
;
1590 if (plaintext_given
) {
1591 result
= rpccli_netlogon_password_logon(
1593 netlogon_pipe
->binding_handle
,
1606 } else if (interactive
) {
1607 result
= rpccli_netlogon_interactive_logon(
1609 netlogon_pipe
->binding_handle
,
1624 result
= rpccli_netlogon_network_logon(
1626 netlogon_pipe
->binding_handle
,
1644 * we increment this after the "feature negotiation"
1645 * for can_do_samlogon_ex and can_do_validation6
1649 /* We have to try a second time as cm_connect_netlogon
1650 might not yet have noticed that the DC has killed
1653 if (!rpccli_is_connected(netlogon_pipe
)) {
1658 /* if we get access denied, a possible cause was that we had
1659 an open connection to the DC, but someone changed our
1660 machine account password out from underneath us using 'net
1661 rpc changetrustpw' */
1663 if ( NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) ) {
1664 DEBUG(1,("winbind_samlogon_retry_loop: sam_logon returned "
1665 "ACCESS_DENIED. Maybe the DC has Restrict "
1666 "NTLM set or the trust account "
1667 "password was changed and we didn't know it. "
1668 "Killing connections to domain %s\n",
1670 invalidate_cm_connection(domain
);
1674 if (NT_STATUS_EQUAL(result
, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
)) {
1676 * Got DCERPC_FAULT_OP_RNG_ERROR for SamLogon
1677 * (no Ex). This happens against old Samba
1678 * DCs, if LogonSamLogonEx() fails with an error
1679 * e.g. NT_STATUS_NO_SUCH_USER or NT_STATUS_WRONG_PASSWORD.
1681 * The server will log something like this:
1682 * api_net_sam_logon_ex: Failed to marshall NET_R_SAM_LOGON_EX.
1684 * This sets the whole connection into a fault_state mode
1685 * and all following request get NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE.
1687 * This also happens to our retry with LogonSamLogonWithFlags()
1688 * and LogonSamLogon().
1690 * In order to recover from this situation, we need to
1691 * drop the connection.
1693 invalidate_cm_connection(domain
);
1694 result
= NT_STATUS_LOGON_FAILURE
;
1698 } while ( (attempts
< 2) && retry
);
1700 if (NT_STATUS_EQUAL(result
, NT_STATUS_IO_TIMEOUT
)) {
1701 DEBUG(3,("winbind_samlogon_retry_loop: sam_network_logon(ex) "
1702 "returned NT_STATUS_IO_TIMEOUT after the retry. "
1703 "Killing connections to domain %s\n",
1705 invalidate_cm_connection(domain
);
1708 if (!NT_STATUS_IS_OK(result
)) {
1712 *_validation_level
= validation_level
;
1713 *_validation
= validation
;
1714 return NT_STATUS_OK
;
1717 static NTSTATUS
nt_dual_auth_passdb(TALLOC_CTX
*mem_ctx
,
1719 fstring name_domain
,
1722 const char *client_name
,
1723 const int client_pid
,
1724 const struct tsocket_address
*remote
,
1725 const struct tsocket_address
*local
,
1726 uint8_t *authoritative
,
1727 struct netr_SamInfo3
**info3
)
1729 unsigned char local_nt_response
[24];
1731 DATA_BLOB chal_blob
;
1735 /* do password magic */
1737 generate_random_buffer(chal
, sizeof(chal
));
1738 chal_blob
= data_blob_const(chal
, sizeof(chal
));
1740 if (lp_client_ntlmv2_auth()) {
1741 DATA_BLOB server_chal
;
1742 DATA_BLOB names_blob
;
1743 server_chal
= data_blob_const(chal
, 8);
1745 /* note that the 'workgroup' here is for the local
1746 machine. The 'server name' must match the
1747 'workstation' passed to the actual SamLogon call.
1749 names_blob
= NTLMv2_generate_names_blob(mem_ctx
,
1753 if (!SMBNTLMv2encrypt(mem_ctx
, name_user
, name_domain
,
1754 pass
, &server_chal
, &names_blob
,
1755 &lm_resp
, &nt_resp
, NULL
, NULL
)) {
1756 data_blob_free(&names_blob
);
1757 DEBUG(0, ("SMBNTLMv2encrypt() failed!\n"));
1758 return NT_STATUS_NO_MEMORY
;
1760 data_blob_free(&names_blob
);
1763 lm_resp
= data_blob_null
;
1765 rc
= SMBNTencrypt(pass
, chal
, local_nt_response
);
1767 DEBUG(0, ("SMBNTencrypt() failed!\n"));
1768 return gnutls_error_to_ntstatus(rc
,
1769 NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER
);
1772 nt_resp
= data_blob_talloc(mem_ctx
, local_nt_response
,
1773 sizeof(local_nt_response
));
1776 return winbindd_dual_auth_passdb(talloc_tos(), 0, name_domain
,
1777 name_user
, logon_id
, client_name
,
1778 client_pid
, &chal_blob
, &lm_resp
,
1779 &nt_resp
, remote
, local
,
1780 true, /* interactive */
1781 authoritative
, info3
);
1784 static NTSTATUS
winbindd_dual_pam_auth_samlogon(
1785 TALLOC_CTX
*mem_ctx
,
1786 struct winbindd_domain
*domain
,
1790 const char *client_name
,
1791 const int client_pid
,
1792 uint32_t request_flags
,
1793 const struct tsocket_address
*remote
,
1794 const struct tsocket_address
*local
,
1795 uint16_t *_validation_level
,
1796 union netr_Validation
**_validation
)
1798 fstring name_namespace
, name_domain
, name_user
;
1800 uint8_t authoritative
= 0;
1802 uint16_t validation_level
= 0;
1803 union netr_Validation
*validation
= NULL
;
1804 struct netr_SamBaseInfo
*base_info
= NULL
;
1807 DEBUG(10,("winbindd_dual_pam_auth_samlogon\n"));
1809 /* Parse domain and username */
1811 ok
= parse_domain_user(user
, name_namespace
, name_domain
, name_user
);
1813 return NT_STATUS_INVALID_PARAMETER
;
1817 * We check against domain->name instead of
1818 * name_domain, as find_auth_domain() ->
1819 * find_domain_from_name_noinit() already decided
1820 * that we are in a child for the correct domain.
1822 * name_domain can also be lp_realm()
1823 * we need to check against domain->name.
1825 if (strequal(domain
->name
, get_global_sam_name())) {
1826 struct netr_SamInfo3
*info3
= NULL
;
1828 result
= nt_dual_auth_passdb(mem_ctx
, name_user
, name_domain
,
1829 pass
, logon_id
, client_name
,
1830 client_pid
, remote
, local
,
1831 &authoritative
, &info3
);
1834 * We need to try the remote NETLOGON server if this is
1835 * not authoritative (for example on the RODC).
1837 if (authoritative
!= 0) {
1838 if (NT_STATUS_IS_OK(result
)) {
1839 result
= map_info3_to_validation(
1845 if (!NT_STATUS_IS_OK(result
)) {
1854 /* check authentication loop */
1856 result
= winbind_samlogon_retry_loop(domain
,
1864 true, /* plaintext_given */
1866 data_blob_null
, data_blob_null
,
1867 true, /* interactive */
1872 if (!NT_STATUS_IS_OK(result
)) {
1876 /* handle the case where a NT4 DC does not fill in the acct_flags in
1877 * the samlogon reply info3. When accurate info3 is required by the
1878 * caller, we look up the account flags ourselves - gd */
1880 switch (validation_level
) {
1882 base_info
= &validation
->sam3
->base
;
1885 base_info
= &validation
->sam6
->base
;
1888 DBG_ERR("Bad validation level %d", (int)validation_level
);
1889 result
= NT_STATUS_INTERNAL_ERROR
;
1892 if ((request_flags
& WBFLAG_PAM_INFO3_TEXT
) &&
1893 (base_info
->acct_flags
== 0))
1895 struct rpc_pipe_client
*samr_pipe
;
1896 struct policy_handle samr_domain_handle
, user_pol
;
1897 union samr_UserInfo
*info
= NULL
;
1898 NTSTATUS status_tmp
, result_tmp
;
1899 uint32_t acct_flags
;
1900 struct dcerpc_binding_handle
*b
;
1902 status_tmp
= cm_connect_sam(domain
, mem_ctx
, false,
1903 &samr_pipe
, &samr_domain_handle
);
1905 if (!NT_STATUS_IS_OK(status_tmp
)) {
1906 DEBUG(3, ("could not open handle to SAMR pipe: %s\n",
1907 nt_errstr(status_tmp
)));
1911 b
= samr_pipe
->binding_handle
;
1913 status_tmp
= dcerpc_samr_OpenUser(b
, mem_ctx
,
1914 &samr_domain_handle
,
1915 MAXIMUM_ALLOWED_ACCESS
,
1920 if (!NT_STATUS_IS_OK(status_tmp
)) {
1921 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1922 nt_errstr(status_tmp
)));
1925 if (!NT_STATUS_IS_OK(result_tmp
)) {
1926 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1927 nt_errstr(result_tmp
)));
1931 status_tmp
= dcerpc_samr_QueryUserInfo(b
, mem_ctx
,
1937 if (any_nt_status_not_ok(status_tmp
, result_tmp
,
1939 DEBUG(3, ("could not query user info on SAMR pipe: %s\n",
1940 nt_errstr(status_tmp
)));
1941 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result_tmp
);
1945 acct_flags
= info
->info16
.acct_flags
;
1947 if (acct_flags
== 0) {
1948 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result_tmp
);
1952 base_info
->acct_flags
= acct_flags
;
1954 DEBUG(10,("successfully retrieved acct_flags 0x%x\n", acct_flags
));
1956 dcerpc_samr_Close(b
, mem_ctx
, &user_pol
, &result_tmp
);
1960 if (NT_STATUS_IS_OK(result
)) {
1961 *_validation_level
= validation_level
;
1962 *_validation
= validation
;
1968 * @brief build a tsocket_address for the remote address of the supplied socket
1971 static struct tsocket_address
*get_remote_address(TALLOC_CTX
*mem_ctx
, int sock
)
1973 struct sockaddr_storage st
= {0};
1974 struct sockaddr
*sar
= (struct sockaddr
*)&st
;
1975 socklen_t sa_len
= sizeof(st
);
1976 struct tsocket_address
*remote
= NULL
;
1979 ret
= getpeername(sock
, sar
, &sa_len
);
1981 DBG_ERR("getpeername failed - %s", strerror(errno
));
1984 ret
= tsocket_address_bsd_from_sockaddr(mem_ctx
, sar
, sa_len
, &remote
);
1986 DBG_ERR("tsocket_address_bsd_from_sockaddr failed - %s",
1994 * @brief build a tsocket_address for the local address of the supplied socket
1997 static struct tsocket_address
*get_local_address(TALLOC_CTX
*mem_ctx
, int sock
)
1999 struct sockaddr_storage st
= {0};
2000 struct sockaddr
*sar
= (struct sockaddr
*)&st
;
2001 socklen_t sa_len
= sizeof(st
);
2002 struct tsocket_address
*local
= NULL
;
2005 ret
= getsockname(sock
, sar
, &sa_len
);
2007 DBG_ERR("getsockname failed - %s", strerror(errno
));
2010 ret
= tsocket_address_bsd_from_sockaddr(mem_ctx
, sar
, sa_len
, &local
);
2012 DBG_ERR("tsocket_address_bsd_from_sockaddr failed - %s",
2020 * @brief generate an authentication message in the logs.
2023 static void log_authentication(
2024 TALLOC_CTX
*mem_ctx
,
2025 const struct winbindd_domain
*domain
,
2026 const struct winbindd_cli_state
*state
,
2027 const struct timeval start_time
,
2028 const uint64_t logon_id
,
2029 const char *command
,
2030 const char *user_name
,
2031 const char *domain_name
,
2032 const char *workstation
,
2033 const DATA_BLOB lm_resp
,
2034 const DATA_BLOB nt_resp
,
2035 const struct tsocket_address
*remote
,
2036 const struct tsocket_address
*local
,
2040 struct auth_usersupplied_info
*ui
= NULL
;
2041 struct dom_sid
*sid
= NULL
;
2042 struct loadparm_context
*lp_ctx
= NULL
;
2043 struct imessaging_context
*msg_ctx
= NULL
;
2045 ui
= talloc_zero(mem_ctx
, struct auth_usersupplied_info
);
2046 ui
->logon_id
= logon_id
;
2047 ui
->service_description
= "winbind";
2048 ui
->password
.response
.nt
.length
= nt_resp
.length
;
2049 ui
->password
.response
.nt
.data
= nt_resp
.data
;
2050 ui
->password
.response
.lanman
.length
= lm_resp
.length
;
2051 ui
->password
.response
.lanman
.data
= lm_resp
.data
;
2052 if (nt_resp
.length
== 0 && lm_resp
.length
== 0) {
2053 ui
->password_state
= AUTH_PASSWORD_PLAIN
;
2055 ui
->password_state
= AUTH_PASSWORD_RESPONSE
;
2058 * In the event of a failure ui->auth_description will be null,
2059 * the logging code handles this correctly so it can be ignored.
2061 ui
->auth_description
= talloc_asprintf(
2065 state
->request
->client_name
,
2067 if (ui
->auth_description
== NULL
) {
2068 DBG_ERR("OOM Unable to create auth_description");
2070 ui
->client
.account_name
= user_name
;
2071 ui
->client
.domain_name
= domain_name
;
2072 ui
->workstation_name
= workstation
;
2073 ui
->remote_host
= remote
;
2074 ui
->local_host
= local
;
2076 sid
= dom_sid_parse_talloc(
2077 ui
, state
->response
->data
.auth
.info3
.dom_sid
);
2079 sid_append_rid(sid
, state
->response
->data
.auth
.info3
.user_rid
);
2082 if (lp_auth_event_notification()) {
2083 lp_ctx
= loadparm_init_s3(ui
, loadparm_s3_helpers());
2084 msg_ctx
= imessaging_client_init(
2085 ui
, lp_ctx
, global_event_context());
2087 log_authentication_event(
2093 state
->response
->data
.auth
.info3
.logon_dom
,
2094 state
->response
->data
.auth
.info3
.user_name
,
2099 enum winbindd_result
winbindd_dual_pam_auth(struct winbindd_domain
*domain
,
2100 struct winbindd_cli_state
*state
)
2102 NTSTATUS result
= NT_STATUS_LOGON_FAILURE
;
2103 NTSTATUS krb5_result
= NT_STATUS_OK
;
2104 fstring name_namespace
, name_domain
, name_user
;
2106 fstring domain_user
;
2107 uint16_t validation_level
= UINT16_MAX
;
2108 union netr_Validation
*validation
= NULL
;
2109 NTSTATUS name_map_status
= NT_STATUS_UNSUCCESSFUL
;
2111 uint64_t logon_id
= 0;
2112 const struct timeval start_time
= timeval_current();
2113 const struct tsocket_address
*remote
= NULL
;
2114 const struct tsocket_address
*local
= NULL
;
2116 /* Ensure null termination */
2117 state
->request
->data
.auth
.user
[sizeof(state
->request
->data
.auth
.user
)-1]='\0';
2119 /* Ensure null termination */
2120 state
->request
->data
.auth
.pass
[sizeof(state
->request
->data
.auth
.pass
)-1]='\0';
2123 * Generate a logon_id for this session.
2125 logon_id
= generate_random_u64();
2126 remote
= get_remote_address(state
->mem_ctx
, state
->sock
);
2127 local
= get_local_address(state
->mem_ctx
, state
->sock
);
2128 DEBUG(3, ("[%5lu]: dual pam auth %s\n", (unsigned long)state
->pid
,
2129 state
->request
->data
.auth
.user
));
2131 /* Parse domain and username */
2133 name_map_status
= normalize_name_unmap(state
->mem_ctx
,
2134 state
->request
->data
.auth
.user
,
2137 /* If the name normalization didn't actually do anything,
2138 just use the original name */
2140 if (!NT_STATUS_IS_OK(name_map_status
) &&
2141 !NT_STATUS_EQUAL(name_map_status
, NT_STATUS_FILE_RENAMED
))
2143 mapped_user
= state
->request
->data
.auth
.user
;
2146 ok
= parse_domain_user(mapped_user
,
2151 result
= NT_STATUS_INVALID_PARAMETER
;
2152 goto process_result
;
2155 if ( mapped_user
!= state
->request
->data
.auth
.user
) {
2156 fstr_sprintf( domain_user
, "%s%c%s", name_domain
,
2157 *lp_winbind_separator(),
2159 strlcpy( state
->request
->data
.auth
.user
, domain_user
,
2160 sizeof(state
->request
->data
.auth
.user
));
2163 if (!domain
->online
) {
2164 result
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
2165 if (domain
->startup
) {
2166 /* Logons are very important to users. If we're offline and
2167 we get a request within the first 30 seconds of startup,
2168 try very hard to find a DC and go online. */
2170 DEBUG(10,("winbindd_dual_pam_auth: domain: %s offline and auth "
2171 "request in startup mode.\n", domain
->name
));
2173 winbindd_flush_negative_conn_cache(domain
);
2174 result
= init_dc_connection(domain
, false);
2178 DEBUG(10,("winbindd_dual_pam_auth: domain: %s last was %s\n", domain
->name
, domain
->online
? "online":"offline"));
2180 /* Check for Kerberos authentication */
2181 if (domain
->online
&& (state
->request
->flags
& WBFLAG_PAM_KRB5
)) {
2182 struct netr_SamInfo6
*info6
= NULL
;
2184 result
= winbindd_dual_pam_auth_kerberos(domain
, state
, &info6
);
2185 /* save for later */
2186 krb5_result
= result
;
2188 if (NT_STATUS_IS_OK(result
)) {
2189 DEBUG(10,("winbindd_dual_pam_auth_kerberos succeeded\n"));
2191 result
= map_info6_to_validation(state
->mem_ctx
,
2196 if (!NT_STATUS_IS_OK(result
)) {
2197 DBG_ERR("map_info6_to_validation failed\n");
2200 goto process_result
;
2203 DBG_DEBUG("winbindd_dual_pam_auth_kerberos failed: %s\n",
2206 if (NT_STATUS_EQUAL(result
, NT_STATUS_NO_LOGON_SERVERS
) ||
2207 NT_STATUS_EQUAL(result
, NT_STATUS_IO_TIMEOUT
) ||
2208 NT_STATUS_EQUAL(result
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
)) {
2209 DEBUG(10,("winbindd_dual_pam_auth_kerberos setting domain to offline\n"));
2210 set_domain_offline( domain
);
2214 /* there are quite some NT_STATUS errors where there is no
2215 * point in retrying with a samlogon, we explictly have to take
2216 * care not to increase the bad logon counter on the DC */
2218 if (NT_STATUS_EQUAL(result
, NT_STATUS_ACCOUNT_DISABLED
) ||
2219 NT_STATUS_EQUAL(result
, NT_STATUS_ACCOUNT_EXPIRED
) ||
2220 NT_STATUS_EQUAL(result
, NT_STATUS_ACCOUNT_LOCKED_OUT
) ||
2221 NT_STATUS_EQUAL(result
, NT_STATUS_INVALID_LOGON_HOURS
) ||
2222 NT_STATUS_EQUAL(result
, NT_STATUS_INVALID_WORKSTATION
) ||
2223 NT_STATUS_EQUAL(result
, NT_STATUS_LOGON_FAILURE
) ||
2224 NT_STATUS_EQUAL(result
, NT_STATUS_NO_SUCH_USER
) ||
2225 NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_EXPIRED
) ||
2226 NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_MUST_CHANGE
) ||
2227 NT_STATUS_EQUAL(result
, NT_STATUS_WRONG_PASSWORD
)) {
2231 if (state
->request
->flags
& WBFLAG_PAM_FALLBACK_AFTER_KRB5
) {
2232 DEBUG(3,("falling back to samlogon\n"));
2240 /* Check for Samlogon authentication */
2241 if (domain
->online
) {
2242 struct netr_SamBaseInfo
*base_info
= NULL
;
2244 result
= winbindd_dual_pam_auth_samlogon(
2245 state
->mem_ctx
, domain
,
2246 state
->request
->data
.auth
.user
,
2247 state
->request
->data
.auth
.pass
,
2249 state
->request
->client_name
,
2251 state
->request
->flags
,
2257 if (NT_STATUS_IS_OK(result
)) {
2258 DEBUG(10,("winbindd_dual_pam_auth_samlogon succeeded\n"));
2260 switch (validation_level
) {
2262 base_info
= &validation
->sam3
->base
;
2265 base_info
= &validation
->sam6
->base
;
2268 DBG_ERR("Bad validation level %d\n",
2270 result
= NT_STATUS_INTERNAL_ERROR
;
2274 /* add the Krb5 err if we have one */
2275 if ( NT_STATUS_EQUAL(krb5_result
, NT_STATUS_TIME_DIFFERENCE_AT_DC
) ) {
2276 base_info
->user_flags
|= LOGON_KRB5_FAIL_CLOCK_SKEW
;
2279 goto process_result
;
2282 DEBUG(10,("winbindd_dual_pam_auth_samlogon failed: %s\n",
2283 nt_errstr(result
)));
2285 if (NT_STATUS_EQUAL(result
, NT_STATUS_NO_LOGON_SERVERS
) ||
2286 NT_STATUS_EQUAL(result
, NT_STATUS_IO_TIMEOUT
) ||
2287 NT_STATUS_EQUAL(result
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
))
2289 DEBUG(10,("winbindd_dual_pam_auth_samlogon setting domain to offline\n"));
2290 set_domain_offline( domain
);
2294 if (domain
->online
) {
2295 /* We're still online - fail. */
2301 /* Check for Cached logons */
2302 if (!domain
->online
&& (state
->request
->flags
& WBFLAG_PAM_CACHED_LOGIN
) &&
2303 lp_winbind_offline_logon()) {
2304 struct netr_SamInfo3
*info3
= NULL
;
2306 result
= winbindd_dual_pam_auth_cached(domain
, state
, &info3
);
2308 if (!NT_STATUS_IS_OK(result
)) {
2309 DEBUG(10,("winbindd_dual_pam_auth_cached failed: %s\n", nt_errstr(result
)));
2312 DEBUG(10,("winbindd_dual_pam_auth_cached succeeded\n"));
2314 result
= map_info3_to_validation(state
->mem_ctx
,
2319 if (!NT_STATUS_IS_OK(result
)) {
2320 DBG_ERR("map_info3_to_validation failed\n");
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(state
->mem_ctx
,
2354 if (cached_info3
!= NULL
&&
2355 cached_info3
->base
.full_name
.string
!= NULL
) {
2356 base_info
->full_name
.string
= talloc_strdup(
2358 cached_info3
->base
.full_name
.string
);
2359 if (base_info
->full_name
.string
== NULL
) {
2360 result
= NT_STATUS_NO_MEMORY
;
2365 /* this might fail so we don't check the return code */
2366 wcache_query_user_fullname(domain
,
2369 &base_info
->full_name
.string
);
2373 result
= map_validation_to_info3(talloc_tos(),
2377 if (!NT_STATUS_IS_OK(result
)) {
2381 wcache_invalidate_samlogon(find_domain_from_name(name_domain
),
2383 netsamlogon_cache_store(name_user
, info3
);
2385 /* save name_to_sid info as early as possible (only if
2386 this is our primary domain so we don't invalidate
2387 the cache entry by storing the seq_num for the wrong
2389 if ( domain
->primary
) {
2390 cache_name2sid(domain
, name_domain
, name_user
,
2391 SID_NAME_USER
, &user_sid
);
2394 /* Check if the user is in the right group */
2396 result
= check_info3_in_group(
2398 state
->request
->data
.auth
.require_membership_of_sid
);
2399 if (!NT_STATUS_IS_OK(result
)) {
2400 DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
2401 state
->request
->data
.auth
.user
,
2402 state
->request
->data
.auth
.require_membership_of_sid
));
2406 if (!is_allowed_domain(info3
->base
.logon_domain
.string
)) {
2407 DBG_NOTICE("Authentication failed for user [%s] "
2408 "from firewalled domain [%s]\n",
2409 info3
->base
.account_name
.string
,
2410 info3
->base
.logon_domain
.string
);
2411 result
= NT_STATUS_AUTHENTICATION_FIREWALL_FAILED
;
2415 result
= append_auth_data(state
->mem_ctx
, state
->response
,
2416 state
->request
->flags
,
2419 name_domain
, name_user
);
2420 if (!NT_STATUS_IS_OK(result
)) {
2424 if ((state
->request
->flags
& WBFLAG_PAM_CACHED_LOGIN
)
2425 && lp_winbind_offline_logon()) {
2427 result
= winbindd_store_creds(domain
,
2428 state
->request
->data
.auth
.user
,
2429 state
->request
->data
.auth
.pass
,
2433 if (state
->request
->flags
& WBFLAG_PAM_GET_PWD_POLICY
) {
2435 * WBFLAG_PAM_GET_PWD_POLICY is not used within
2436 * any Samba caller anymore.
2438 * We just fake this based on the effective values
2439 * for the user, for legacy callers.
2441 fake_password_policy(state
->response
, &info3
->base
);
2444 result
= NT_STATUS_OK
;
2448 /* give us a more useful (more correct?) error code */
2449 if ((NT_STATUS_EQUAL(result
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
) ||
2450 (NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
)))) {
2451 result
= NT_STATUS_NO_LOGON_SERVERS
;
2454 set_auth_errors(state
->response
, result
);
2456 DEBUG(NT_STATUS_IS_OK(result
) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n",
2457 state
->request
->data
.auth
.user
,
2458 state
->response
->data
.auth
.nt_status_string
,
2459 state
->response
->data
.auth
.pam_error
));
2462 * Log the winbind pam authentication, the logon_id will tie this to
2463 * any of the logons invoked from this request.
2481 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
2484 NTSTATUS
winbind_dual_SamLogon(struct winbindd_domain
*domain
,
2485 TALLOC_CTX
*mem_ctx
,
2487 uint32_t logon_parameters
,
2488 const char *name_user
,
2489 const char *name_domain
,
2490 const char *workstation
,
2491 const uint64_t logon_id
,
2492 const char* client_name
,
2493 const int client_pid
,
2494 const uint8_t chal
[8],
2495 DATA_BLOB lm_response
,
2496 DATA_BLOB nt_response
,
2497 const struct tsocket_address
*remote
,
2498 const struct tsocket_address
*local
,
2499 uint8_t *authoritative
,
2502 uint16_t *_validation_level
,
2503 union netr_Validation
**_validation
)
2505 uint16_t validation_level
= 0;
2506 union netr_Validation
*validation
= NULL
;
2510 * We check against domain->name instead of
2511 * name_domain, as find_auth_domain() ->
2512 * find_domain_from_name_noinit() already decided
2513 * that we are in a child for the correct domain.
2515 * name_domain can also be lp_realm()
2516 * we need to check against domain->name.
2518 if (!skip_sam
&& strequal(domain
->name
, get_global_sam_name())) {
2519 DATA_BLOB chal_blob
= data_blob_const(
2521 struct netr_SamInfo3
*info3
= NULL
;
2523 result
= winbindd_dual_auth_passdb(
2526 name_domain
, name_user
,
2530 &chal_blob
, &lm_response
, &nt_response
,
2536 if (NT_STATUS_IS_OK(result
)) {
2537 result
= map_info3_to_validation(mem_ctx
,
2542 if (!NT_STATUS_IS_OK(result
)) {
2548 * We need to try the remote NETLOGON server if this is
2549 * not authoritative.
2551 if (*authoritative
!= 0) {
2553 goto process_result
;
2557 result
= winbind_samlogon_retry_loop(domain
,
2561 NULL
, /* password */
2563 /* Bug #3248 - found by Stefan Burkei. */
2564 workstation
, /* We carefully set this above so use it... */
2566 false, /* plaintext_given */
2575 if (!NT_STATUS_IS_OK(result
)) {
2581 if (NT_STATUS_IS_OK(result
)) {
2582 struct dom_sid user_sid
;
2583 TALLOC_CTX
*base_ctx
= NULL
;
2584 struct netr_SamBaseInfo
*base_info
= NULL
;
2585 struct netr_SamInfo3
*info3
= NULL
;
2587 switch (validation_level
) {
2589 base_ctx
= validation
->sam3
;
2590 base_info
= &validation
->sam3
->base
;
2593 base_ctx
= validation
->sam6
;
2594 base_info
= &validation
->sam6
->base
;
2597 result
= NT_STATUS_INTERNAL_ERROR
;
2601 sid_compose(&user_sid
, base_info
->domain_sid
, base_info
->rid
);
2603 if (base_info
->full_name
.string
== NULL
) {
2604 struct netr_SamInfo3
*cached_info3
;
2606 cached_info3
= netsamlogon_cache_get(mem_ctx
,
2608 if (cached_info3
!= NULL
&&
2609 cached_info3
->base
.full_name
.string
!= NULL
)
2611 base_info
->full_name
.string
= talloc_strdup(
2613 cached_info3
->base
.full_name
.string
);
2616 /* this might fail so we don't check the return code */
2617 wcache_query_user_fullname(domain
,
2620 &base_info
->full_name
.string
);
2624 result
= map_validation_to_info3(talloc_tos(),
2628 if (!NT_STATUS_IS_OK(result
)) {
2631 wcache_invalidate_samlogon(find_domain_from_name(name_domain
),
2633 netsamlogon_cache_store(name_user
, info3
);
2639 /* give us a more useful (more correct?) error code */
2640 if ((NT_STATUS_EQUAL(result
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
) ||
2641 (NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
)))) {
2642 result
= NT_STATUS_NO_LOGON_SERVERS
;
2645 DEBUG(NT_STATUS_IS_OK(result
) ? 5 : 2,
2646 ("NTLM CRAP authentication for user [%s]\\[%s] returned %s\n",
2649 nt_errstr(result
)));
2651 if (!NT_STATUS_IS_OK(result
)) {
2655 *_validation_level
= validation_level
;
2656 *_validation
= validation
;
2657 return NT_STATUS_OK
;
2660 enum winbindd_result
winbindd_dual_pam_auth_crap(struct winbindd_domain
*domain
,
2661 struct winbindd_cli_state
*state
)
2664 const char *name_user
= NULL
;
2665 const char *name_domain
= NULL
;
2666 const char *workstation
;
2667 uint64_t logon_id
= 0;
2668 uint8_t authoritative
= 0;
2670 uint16_t validation_level
;
2671 union netr_Validation
*validation
= NULL
;
2672 DATA_BLOB lm_resp
= { 0 }, nt_resp
= { 0 };
2673 const struct timeval start_time
= timeval_current();
2674 const struct tsocket_address
*remote
= NULL
;
2675 const struct tsocket_address
*local
= NULL
;
2677 /* This is child-only, so no check for privileged access is needed
2680 /* Ensure null termination */
2681 state
->request
->data
.auth_crap
.user
[sizeof(state
->request
->data
.auth_crap
.user
)-1]=0;
2682 state
->request
->data
.auth_crap
.domain
[sizeof(state
->request
->data
.auth_crap
.domain
)-1]=0;
2684 name_user
= state
->request
->data
.auth_crap
.user
;
2685 name_domain
= state
->request
->data
.auth_crap
.domain
;
2686 workstation
= state
->request
->data
.auth_crap
.workstation
;
2687 logon_id
= generate_random_u64();
2688 remote
= get_remote_address(state
->mem_ctx
, state
->sock
);
2689 local
= get_local_address(state
->mem_ctx
, state
->sock
);
2691 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state
->pid
,
2692 name_domain
, name_user
));
2694 if (state
->request
->data
.auth_crap
.lm_resp_len
> sizeof(state
->request
->data
.auth_crap
.lm_resp
)
2695 || state
->request
->data
.auth_crap
.nt_resp_len
> sizeof(state
->request
->data
.auth_crap
.nt_resp
)) {
2696 if (!(state
->request
->flags
& WBFLAG_BIG_NTLMV2_BLOB
) ||
2697 state
->request
->extra_len
!= state
->request
->data
.auth_crap
.nt_resp_len
) {
2698 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n",
2699 state
->request
->data
.auth_crap
.lm_resp_len
,
2700 state
->request
->data
.auth_crap
.nt_resp_len
));
2701 result
= NT_STATUS_INVALID_PARAMETER
;
2706 lm_resp
= data_blob_talloc(state
->mem_ctx
, state
->request
->data
.auth_crap
.lm_resp
,
2707 state
->request
->data
.auth_crap
.lm_resp_len
);
2709 if (state
->request
->flags
& WBFLAG_BIG_NTLMV2_BLOB
) {
2710 nt_resp
= data_blob_talloc(state
->mem_ctx
,
2711 state
->request
->extra_data
.data
,
2712 state
->request
->data
.auth_crap
.nt_resp_len
);
2714 nt_resp
= data_blob_talloc(state
->mem_ctx
,
2715 state
->request
->data
.auth_crap
.nt_resp
,
2716 state
->request
->data
.auth_crap
.nt_resp_len
);
2719 result
= winbind_dual_SamLogon(domain
,
2721 false, /* interactive */
2722 state
->request
->data
.auth_crap
.logon_parameters
,
2725 /* Bug #3248 - found by Stefan Burkei. */
2726 workstation
, /* We carefully set this above so use it... */
2728 state
->request
->client_name
,
2729 state
->request
->pid
,
2730 state
->request
->data
.auth_crap
.chal
,
2740 if (!NT_STATUS_IS_OK(result
)) {
2741 state
->response
->data
.auth
.authoritative
= authoritative
;
2745 if (NT_STATUS_IS_OK(result
)) {
2746 struct netr_SamInfo3
*info3
= NULL
;
2748 result
= map_validation_to_info3(state
->mem_ctx
,
2752 if (!NT_STATUS_IS_OK(result
)) {
2756 /* Check if the user is in the right group */
2757 result
= check_info3_in_group(
2759 state
->request
->data
.auth_crap
.require_membership_of_sid
);
2760 if (!NT_STATUS_IS_OK(result
)) {
2761 DEBUG(3, ("User %s is not in the required group (%s), so "
2762 "crap authentication is rejected\n",
2763 state
->request
->data
.auth_crap
.user
,
2764 state
->request
->data
.auth_crap
.require_membership_of_sid
));
2768 if (!is_allowed_domain(info3
->base
.logon_domain
.string
)) {
2769 DBG_NOTICE("Authentication failed for user [%s] "
2770 "from firewalled domain [%s]\n",
2771 info3
->base
.account_name
.string
,
2772 info3
->base
.logon_domain
.string
);
2773 state
->response
->data
.auth
.authoritative
= true;
2774 result
= NT_STATUS_AUTHENTICATION_FIREWALL_FAILED
;
2778 result
= append_auth_data(state
->mem_ctx
, state
->response
,
2779 state
->request
->flags
,
2782 name_domain
, name_user
);
2783 if (!NT_STATUS_IS_OK(result
)) {
2790 if (state
->request
->flags
& WBFLAG_PAM_NT_STATUS_SQUASH
) {
2791 result
= nt_status_squash(result
);
2794 set_auth_errors(state
->response
, result
);
2796 * Log the winbind pam authentication, the logon_id will tie this to
2797 * any of the logons invoked from this request.
2815 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
2818 enum winbindd_result
winbindd_dual_pam_chauthtok(struct winbindd_domain
*contact_domain
,
2819 struct winbindd_cli_state
*state
)
2822 char *newpass
= NULL
;
2823 struct policy_handle dom_pol
;
2824 struct rpc_pipe_client
*cli
= NULL
;
2825 bool got_info
= false;
2826 struct samr_DomInfo1
*info
= NULL
;
2827 struct userPwdChangeFailureInformation
*reject
= NULL
;
2828 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2829 fstring
namespace, domain
, user
;
2830 struct dcerpc_binding_handle
*b
= NULL
;
2833 ZERO_STRUCT(dom_pol
);
2835 DEBUG(3, ("[%5lu]: dual pam chauthtok %s\n", (unsigned long)state
->pid
,
2836 state
->request
->data
.auth
.user
));
2838 ok
= parse_domain_user(state
->request
->data
.chauthtok
.user
,
2846 if (!is_allowed_domain(domain
)) {
2847 DBG_NOTICE("Authentication failed for user [%s] "
2848 "from firewalled domain [%s]\n",
2850 result
= NT_STATUS_AUTHENTICATION_FIREWALL_FAILED
;
2854 /* Change password */
2856 oldpass
= state
->request
->data
.chauthtok
.oldpass
;
2857 newpass
= state
->request
->data
.chauthtok
.newpass
;
2859 /* Initialize reject reason */
2860 state
->response
->data
.auth
.reject_reason
= Undefined
;
2862 /* Get sam handle */
2864 result
= cm_connect_sam(contact_domain
, state
->mem_ctx
, true, &cli
,
2866 if (!NT_STATUS_IS_OK(result
)) {
2867 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain
));
2871 b
= cli
->binding_handle
;
2873 result
= rpccli_samr_chgpasswd_user3(cli
, state
->mem_ctx
,
2880 /* Windows 2003 returns NT_STATUS_PASSWORD_RESTRICTION */
2882 if (NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_RESTRICTION
) ) {
2884 fill_in_password_policy(state
->response
, info
);
2886 state
->response
->data
.auth
.reject_reason
=
2887 reject
->extendedFailureReason
;
2892 /* atm the pidl generated rpccli_samr_ChangePasswordUser3 function will
2893 * return with NT_STATUS_BUFFER_TOO_SMALL for w2k dcs as w2k just
2894 * returns with 4byte error code (NT_STATUS_NOT_SUPPORTED) which is too
2895 * short to comply with the samr_ChangePasswordUser3 idl - gd */
2897 /* only fallback when the chgpasswd_user3 call is not supported */
2898 if (NT_STATUS_EQUAL(result
, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
) ||
2899 NT_STATUS_EQUAL(result
, NT_STATUS_NOT_SUPPORTED
) ||
2900 NT_STATUS_EQUAL(result
, NT_STATUS_BUFFER_TOO_SMALL
) ||
2901 NT_STATUS_EQUAL(result
, NT_STATUS_NOT_IMPLEMENTED
)) {
2903 DEBUG(10,("Password change with chgpasswd_user3 failed with: %s, retrying chgpasswd_user2\n",
2904 nt_errstr(result
)));
2906 result
= rpccli_samr_chgpasswd_user2(cli
, state
->mem_ctx
, user
, newpass
, oldpass
);
2908 /* Windows 2000 returns NT_STATUS_ACCOUNT_RESTRICTION.
2909 Map to the same status code as Windows 2003. */
2911 if ( NT_STATUS_EQUAL(NT_STATUS_ACCOUNT_RESTRICTION
, result
) ) {
2912 result
= NT_STATUS_PASSWORD_RESTRICTION
;
2918 if (NT_STATUS_IS_OK(result
)
2919 && (state
->request
->flags
& WBFLAG_PAM_CACHED_LOGIN
)
2920 && lp_winbind_offline_logon()) {
2921 result
= winbindd_update_creds_by_name(contact_domain
, user
,
2923 /* Again, this happens when we login from gdm or xdm
2924 * and the password expires, *BUT* cached crendentials
2925 * doesn't exist. winbindd_update_creds_by_name()
2926 * returns NT_STATUS_NO_SUCH_USER.
2927 * This is not a failure.
2930 if (NT_STATUS_EQUAL(result
, NT_STATUS_NO_SUCH_USER
)) {
2931 result
= NT_STATUS_OK
;
2934 if (!NT_STATUS_IS_OK(result
)) {
2935 DEBUG(10, ("Failed to store creds: %s\n",
2936 nt_errstr(result
)));
2937 goto process_result
;
2941 if (!NT_STATUS_IS_OK(result
) && !got_info
&& contact_domain
) {
2943 NTSTATUS policy_ret
;
2945 policy_ret
= fillup_password_policy(
2946 contact_domain
, state
->response
);
2948 /* failure of this is non critical, it will just provide no
2949 * additional information to the client why the change has
2950 * failed - Guenther */
2952 if (!NT_STATUS_IS_OK(policy_ret
)) {
2953 DEBUG(10,("Failed to get password policies: %s\n", nt_errstr(policy_ret
)));
2954 goto process_result
;
2960 if (strequal(contact_domain
->name
, get_global_sam_name())) {
2961 /* FIXME: internal rpc pipe does not cache handles yet */
2963 if (is_valid_policy_hnd(&dom_pol
)) {
2965 dcerpc_samr_Close(b
, state
->mem_ctx
, &dom_pol
, &_result
);
2971 set_auth_errors(state
->response
, result
);
2973 DEBUG(NT_STATUS_IS_OK(result
) ? 5 : 2,
2974 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
2977 state
->response
->data
.auth
.nt_status_string
,
2978 state
->response
->data
.auth
.pam_error
));
2980 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
2983 enum winbindd_result
winbindd_dual_pam_logoff(struct winbindd_domain
*domain
,
2984 struct winbindd_cli_state
*state
)
2986 NTSTATUS result
= NT_STATUS_NOT_SUPPORTED
;
2988 DEBUG(3, ("[%5lu]: pam dual logoff %s\n", (unsigned long)state
->pid
,
2989 state
->request
->data
.logoff
.user
));
2991 if (!(state
->request
->flags
& WBFLAG_PAM_KRB5
)) {
2992 result
= NT_STATUS_OK
;
2993 goto process_result
;
2996 if (state
->request
->data
.logoff
.krb5ccname
[0] == '\0') {
2997 result
= NT_STATUS_OK
;
2998 goto process_result
;
3003 if (state
->request
->data
.logoff
.uid
== (uid_t
)-1) {
3004 DEBUG(0,("winbindd_pam_logoff: invalid uid\n"));
3005 goto process_result
;
3008 /* what we need here is to find the corresponding krb5 ccache name *we*
3009 * created for a given username and destroy it */
3011 if (!ccache_entry_exists(state
->request
->data
.logoff
.user
)) {
3012 result
= NT_STATUS_OK
;
3013 DEBUG(10,("winbindd_pam_logoff: no entry found.\n"));
3014 goto process_result
;
3017 if (!ccache_entry_identical(state
->request
->data
.logoff
.user
,
3018 state
->request
->data
.logoff
.uid
,
3019 state
->request
->data
.logoff
.krb5ccname
)) {
3020 DEBUG(0,("winbindd_pam_logoff: cached entry differs.\n"));
3021 goto process_result
;
3024 result
= remove_ccache(state
->request
->data
.logoff
.user
);
3025 if (!NT_STATUS_IS_OK(result
)) {
3026 DEBUG(0,("winbindd_pam_logoff: failed to remove ccache: %s\n",
3027 nt_errstr(result
)));
3028 goto process_result
;
3032 * Remove any mlock'ed memory creds in the child
3033 * we might be using for krb5 ticket renewal.
3036 winbindd_delete_memory_creds(state
->request
->data
.logoff
.user
);
3039 result
= NT_STATUS_NOT_SUPPORTED
;
3045 set_auth_errors(state
->response
, result
);
3047 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
3050 /* Change user password with auth crap*/
3052 enum winbindd_result
winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domain
*domainSt
, struct winbindd_cli_state
*state
)
3055 DATA_BLOB new_nt_password
;
3056 DATA_BLOB old_nt_hash_enc
;
3057 DATA_BLOB new_lm_password
;
3058 DATA_BLOB old_lm_hash_enc
;
3059 fstring
namespace, domain
, user
;
3060 struct policy_handle dom_pol
;
3061 struct winbindd_domain
*contact_domain
= domainSt
;
3062 struct rpc_pipe_client
*cli
= NULL
;
3063 struct dcerpc_binding_handle
*b
= NULL
;
3065 ZERO_STRUCT(dom_pol
);
3067 /* Ensure null termination */
3068 state
->request
->data
.chng_pswd_auth_crap
.user
[
3069 sizeof(state
->request
->data
.chng_pswd_auth_crap
.user
)-1]=0;
3070 state
->request
->data
.chng_pswd_auth_crap
.domain
[
3071 sizeof(state
->request
->data
.chng_pswd_auth_crap
.domain
)-1]=0;
3073 namespace[0] = '\0';
3076 DEBUG(3, ("[%5lu]: pam change pswd auth crap domain: %s user: %s\n",
3077 (unsigned long)state
->pid
,
3078 state
->request
->data
.chng_pswd_auth_crap
.domain
,
3079 state
->request
->data
.chng_pswd_auth_crap
.user
));
3081 if (lp_winbind_offline_logon()) {
3082 DEBUG(0,("Refusing password change as winbind offline logons are enabled. "));
3083 DEBUGADD(0,("Changing passwords here would risk inconsistent logons\n"));
3084 result
= NT_STATUS_ACCESS_DENIED
;
3088 if (*state
->request
->data
.chng_pswd_auth_crap
.domain
) {
3089 fstrcpy(domain
,state
->request
->data
.chng_pswd_auth_crap
.domain
);
3093 ok
= parse_domain_user(state
->request
->data
.chng_pswd_auth_crap
.user
,
3098 result
= NT_STATUS_INVALID_PARAMETER
;
3103 DEBUG(3,("no domain specified with username (%s) - "
3105 state
->request
->data
.chng_pswd_auth_crap
.user
));
3106 result
= NT_STATUS_NO_SUCH_USER
;
3111 if (!*domain
&& lp_winbind_use_default_domain()) {
3112 fstrcpy(domain
,lp_workgroup());
3115 if (!is_allowed_domain(domain
)) {
3116 DBG_NOTICE("Authentication failed for user [%s] "
3117 "from firewalled domain [%s]\n",
3118 state
->request
->data
.chng_pswd_auth_crap
.user
,
3120 result
= NT_STATUS_AUTHENTICATION_FIREWALL_FAILED
;
3125 fstrcpy(user
, state
->request
->data
.chng_pswd_auth_crap
.user
);
3128 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n",
3129 (unsigned long)state
->pid
, domain
, user
));
3131 /* Change password */
3132 new_nt_password
= data_blob_const(
3133 state
->request
->data
.chng_pswd_auth_crap
.new_nt_pswd
,
3134 state
->request
->data
.chng_pswd_auth_crap
.new_nt_pswd_len
);
3136 old_nt_hash_enc
= data_blob_const(
3137 state
->request
->data
.chng_pswd_auth_crap
.old_nt_hash_enc
,
3138 state
->request
->data
.chng_pswd_auth_crap
.old_nt_hash_enc_len
);
3140 if(state
->request
->data
.chng_pswd_auth_crap
.new_lm_pswd_len
> 0) {
3141 new_lm_password
= data_blob_const(
3142 state
->request
->data
.chng_pswd_auth_crap
.new_lm_pswd
,
3143 state
->request
->data
.chng_pswd_auth_crap
.new_lm_pswd_len
);
3145 old_lm_hash_enc
= data_blob_const(
3146 state
->request
->data
.chng_pswd_auth_crap
.old_lm_hash_enc
,
3147 state
->request
->data
.chng_pswd_auth_crap
.old_lm_hash_enc_len
);
3149 new_lm_password
= data_blob_null
;
3150 old_lm_hash_enc
= data_blob_null
;
3153 /* Get sam handle */
3155 result
= cm_connect_sam(contact_domain
, state
->mem_ctx
, true, &cli
, &dom_pol
);
3156 if (!NT_STATUS_IS_OK(result
)) {
3157 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain
));
3161 b
= cli
->binding_handle
;
3163 result
= rpccli_samr_chng_pswd_auth_crap(
3164 cli
, state
->mem_ctx
, user
, new_nt_password
, old_nt_hash_enc
,
3165 new_lm_password
, old_lm_hash_enc
);
3169 if (strequal(contact_domain
->name
, get_global_sam_name())) {
3170 /* FIXME: internal rpc pipe does not cache handles yet */
3172 if (is_valid_policy_hnd(&dom_pol
)) {
3174 dcerpc_samr_Close(b
, state
->mem_ctx
, &dom_pol
, &_result
);
3180 set_auth_errors(state
->response
, result
);
3182 DEBUG(NT_STATUS_IS_OK(result
) ? 5 : 2,
3183 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
3185 state
->response
->data
.auth
.nt_status_string
,
3186 state
->response
->data
.auth
.pam_error
));
3188 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
3192 static NTSTATUS
extract_pac_vrfy_sigs(TALLOC_CTX
*mem_ctx
, DATA_BLOB pac_blob
,
3193 struct PAC_DATA
**p_pac_data
)
3195 krb5_context krbctx
= NULL
;
3196 krb5_error_code k5ret
;
3198 krb5_kt_cursor cursor
;
3199 krb5_keytab_entry entry
;
3200 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
3203 ZERO_STRUCT(cursor
);
3205 k5ret
= smb_krb5_init_context_common(&krbctx
);
3207 DBG_ERR("kerberos init context failed (%s)\n",
3208 error_message(k5ret
));
3209 status
= krb5_to_nt_status(k5ret
);
3213 k5ret
= gse_krb5_get_server_keytab(krbctx
, &keytab
);
3215 DEBUG(1, ("Failed to get keytab: %s\n",
3216 error_message(k5ret
)));
3217 status
= krb5_to_nt_status(k5ret
);
3221 k5ret
= krb5_kt_start_seq_get(krbctx
, keytab
, &cursor
);
3223 DEBUG(1, ("Failed to start seq: %s\n",
3224 error_message(k5ret
)));
3225 status
= krb5_to_nt_status(k5ret
);
3229 k5ret
= krb5_kt_next_entry(krbctx
, keytab
, &entry
, &cursor
);
3230 while (k5ret
== 0) {
3231 status
= kerberos_decode_pac(mem_ctx
,
3234 NULL
, /* krbtgt_keyblock */
3235 KRB5_KT_KEY(&entry
), /* service_keyblock */
3236 NULL
, /* client_principal */
3237 0, /* tgs_authtime */
3239 if (NT_STATUS_IS_OK(status
)) {
3242 k5ret
= smb_krb5_kt_free_entry(krbctx
, &entry
);
3243 k5ret
= krb5_kt_next_entry(krbctx
, keytab
, &entry
, &cursor
);
3246 k5ret
= krb5_kt_end_seq_get(krbctx
, keytab
, &cursor
);
3248 DEBUG(1, ("Failed to end seq: %s\n",
3249 error_message(k5ret
)));
3252 k5ret
= krb5_kt_close(krbctx
, keytab
);
3254 DEBUG(1, ("Failed to close keytab: %s\n",
3255 error_message(k5ret
)));
3258 krb5_free_context(krbctx
);
3263 NTSTATUS
winbindd_pam_auth_pac_verify(struct winbindd_cli_state
*state
,
3265 uint16_t *p_validation_level
,
3266 union netr_Validation
**p_validation
)
3268 struct winbindd_request
*req
= state
->request
;
3270 struct PAC_DATA
*pac_data
= NULL
;
3271 struct PAC_LOGON_INFO
*logon_info
= NULL
;
3272 struct PAC_UPN_DNS_INFO
*upn_dns_info
= NULL
;
3273 struct netr_SamInfo6
*info6
= NULL
;
3274 uint16_t validation_level
= 0;
3275 union netr_Validation
*validation
= NULL
;
3276 struct netr_SamInfo3
*info3_copy
= NULL
;
3278 bool is_trusted
= false;
3281 *p_is_trusted
= false;
3282 *p_validation_level
= 0;
3283 *p_validation
= NULL
;
3285 pac_blob
= data_blob_const(req
->extra_data
.data
, req
->extra_len
);
3286 result
= extract_pac_vrfy_sigs(state
->mem_ctx
, pac_blob
, &pac_data
);
3287 if (NT_STATUS_IS_OK(result
)) {
3290 if (NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
3291 /* Try without signature verification */
3292 result
= kerberos_decode_pac(state
->mem_ctx
,
3294 NULL
, /* krb5_context */
3295 NULL
, /* krbtgt_keyblock */
3296 NULL
, /* service_keyblock */
3297 NULL
, /* client_principal */
3298 0, /* tgs_authtime */
3301 if (!NT_STATUS_IS_OK(result
)) {
3302 DEBUG(1, ("Error during PAC signature verification: %s\n",
3303 nt_errstr(result
)));
3307 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
3308 if (pac_data
->buffers
[i
].type
== PAC_TYPE_LOGON_INFO
) {
3309 logon_info
= pac_data
->buffers
[i
].info
->logon_info
.info
;
3312 if (pac_data
->buffers
[i
].type
== PAC_TYPE_UPN_DNS_INFO
) {
3313 upn_dns_info
= &pac_data
->buffers
[i
].info
->upn_dns_info
;
3318 result
= create_info6_from_pac(state
->mem_ctx
,
3322 if (!NT_STATUS_IS_OK(result
)) {
3326 if (!is_allowed_domain(info6
->base
.logon_domain
.string
)) {
3327 DBG_NOTICE("Authentication failed for user [%s] "
3328 "from firewalled domain [%s]\n",
3329 info6
->base
.account_name
.string
,
3330 info6
->base
.logon_domain
.string
);
3331 return NT_STATUS_AUTHENTICATION_FIREWALL_FAILED
;
3334 result
= map_info6_to_validation(state
->mem_ctx
,
3338 if (!NT_STATUS_IS_OK(result
)) {
3342 result
= map_validation_to_info3(state
->mem_ctx
,
3346 if (!NT_STATUS_IS_OK(result
)) {
3352 * Signature verification succeeded, we can
3353 * trust the PAC and prime the netsamlogon
3354 * and name2sid caches. DO NOT DO THIS
3355 * in the signature verification failed
3358 struct winbindd_domain
*domain
= NULL
;
3360 netsamlogon_cache_store(NULL
, info3_copy
);
3363 * We're in the parent here, so find the child
3364 * pointer from the PAC domain name.
3366 domain
= find_lookup_domain_from_name(
3367 info3_copy
->base
.logon_domain
.string
);
3368 if (domain
&& domain
->primary
) {
3369 struct dom_sid user_sid
;
3370 struct dom_sid_buf buf
;
3372 sid_compose(&user_sid
,
3373 info3_copy
->base
.domain_sid
,
3374 info3_copy
->base
.rid
);
3376 cache_name2sid_trusted(domain
,
3377 info3_copy
->base
.logon_domain
.string
,
3378 info3_copy
->base
.account_name
.string
,
3382 DBG_INFO("PAC for user %s\\%s SID %s primed cache\n",
3383 info3_copy
->base
.logon_domain
.string
,
3384 info3_copy
->base
.account_name
.string
,
3385 dom_sid_str_buf(&user_sid
, &buf
));
3389 *p_is_trusted
= is_trusted
;
3390 *p_validation_level
= validation_level
;
3391 *p_validation
= validation
;
3392 return NT_STATUS_OK
;
3394 #else /* HAVE_KRB5 */
3395 NTSTATUS
winbindd_pam_auth_pac_verify(struct winbindd_cli_state
*state
,
3397 uint16_t *p_validation_level
,
3398 union netr_Validation
**p_validation
);
3401 *p_is_trusted
= false;
3402 *p_validation_level
= 0;
3403 *p_validation
= NULL
;
3404 return NT_STATUS_NO_SUCH_USER
;
3406 #endif /* HAVE_KRB5 */