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 "librpc/gen_ndr/ndr_winbind.h"
31 #include "rpc_client/cli_pipe.h"
32 #include "rpc_client/cli_samr.h"
33 #include "../librpc/gen_ndr/ndr_netlogon.h"
34 #include "rpc_client/cli_netlogon.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"
48 #include "param/param.h"
49 #include "messaging/messaging.h"
50 #include "lib/util/string_wrappers.h"
51 #include "lib/crypto/gnutls_helpers.h"
53 #include "lib/crypto/gnutls_helpers.h"
54 #include <gnutls/crypto.h>
55 #include "lib/global_contexts.h"
58 #define DBGC_CLASS DBGC_WINBIND
60 #define LOGON_KRB5_FAIL_CLOCK_SKEW 0x02000000
62 static NTSTATUS
append_info3_as_txt(TALLOC_CTX
*mem_ctx
,
63 struct winbindd_response
*resp
,
64 uint16_t validation_level
,
65 union netr_Validation
*validation
)
67 struct netr_SamInfo3
*info3
= NULL
;
70 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
71 TALLOC_CTX
*frame
= talloc_stackframe();
73 status
= map_validation_to_info3(frame
,
77 if (!NT_STATUS_IS_OK(status
)) {
81 resp
->data
.auth
.info3
.logon_time
=
82 nt_time_to_unix(info3
->base
.logon_time
);
83 resp
->data
.auth
.info3
.logoff_time
=
84 nt_time_to_unix(info3
->base
.logoff_time
);
85 resp
->data
.auth
.info3
.kickoff_time
=
86 nt_time_to_unix(info3
->base
.kickoff_time
);
87 resp
->data
.auth
.info3
.pass_last_set_time
=
88 nt_time_to_unix(info3
->base
.last_password_change
);
89 resp
->data
.auth
.info3
.pass_can_change_time
=
90 nt_time_to_unix(info3
->base
.allow_password_change
);
91 resp
->data
.auth
.info3
.pass_must_change_time
=
92 nt_time_to_unix(info3
->base
.force_password_change
);
94 resp
->data
.auth
.info3
.logon_count
= info3
->base
.logon_count
;
95 resp
->data
.auth
.info3
.bad_pw_count
= info3
->base
.bad_password_count
;
97 resp
->data
.auth
.info3
.user_rid
= info3
->base
.rid
;
98 resp
->data
.auth
.info3
.group_rid
= info3
->base
.primary_gid
;
99 sid_to_fstring(resp
->data
.auth
.info3
.dom_sid
, info3
->base
.domain_sid
);
101 resp
->data
.auth
.info3
.num_groups
= info3
->base
.groups
.count
;
102 resp
->data
.auth
.info3
.user_flgs
= info3
->base
.user_flags
;
104 resp
->data
.auth
.info3
.acct_flags
= info3
->base
.acct_flags
;
105 resp
->data
.auth
.info3
.num_other_sids
= info3
->sidcount
;
107 fstrcpy(resp
->data
.auth
.info3
.user_name
,
108 info3
->base
.account_name
.string
);
109 fstrcpy(resp
->data
.auth
.info3
.full_name
,
110 info3
->base
.full_name
.string
);
111 fstrcpy(resp
->data
.auth
.info3
.logon_script
,
112 info3
->base
.logon_script
.string
);
113 fstrcpy(resp
->data
.auth
.info3
.profile_path
,
114 info3
->base
.profile_path
.string
);
115 fstrcpy(resp
->data
.auth
.info3
.home_dir
,
116 info3
->base
.home_directory
.string
);
117 fstrcpy(resp
->data
.auth
.info3
.dir_drive
,
118 info3
->base
.home_drive
.string
);
120 fstrcpy(resp
->data
.auth
.info3
.logon_srv
,
121 info3
->base
.logon_server
.string
);
122 fstrcpy(resp
->data
.auth
.info3
.logon_dom
,
123 info3
->base
.logon_domain
.string
);
125 resp
->data
.auth
.validation_level
= validation_level
;
126 if (validation_level
== 6) {
127 fstrcpy(resp
->data
.auth
.info6
.dns_domainname
,
128 validation
->sam6
->dns_domainname
.string
);
129 fstrcpy(resp
->data
.auth
.info6
.principal_name
,
130 validation
->sam6
->principal_name
.string
);
133 ex
= talloc_strdup(frame
, "");
135 status
= NT_STATUS_NO_MEMORY
;
139 for (i
=0; i
< info3
->base
.groups
.count
; i
++) {
140 ex
= talloc_asprintf_append_buffer(ex
, "0x%08X:0x%08X\n",
141 info3
->base
.groups
.rids
[i
].rid
,
142 info3
->base
.groups
.rids
[i
].attributes
);
144 status
= NT_STATUS_NO_MEMORY
;
149 for (i
=0; i
< info3
->sidcount
; i
++) {
150 struct dom_sid_buf sidbuf
;
152 ex
= talloc_asprintf_append_buffer(
155 dom_sid_str_buf(info3
->sids
[i
].sid
, &sidbuf
),
156 info3
->sids
[i
].attributes
);
158 status
= NT_STATUS_NO_MEMORY
;
163 resp
->length
+= talloc_get_size(ex
);
164 resp
->extra_data
.data
= talloc_move(mem_ctx
, &ex
);
166 status
= NT_STATUS_OK
;
172 static NTSTATUS
append_info3_as_ndr(TALLOC_CTX
*mem_ctx
,
173 struct winbindd_response
*resp
,
174 struct netr_SamInfo3
*info3
)
177 enum ndr_err_code ndr_err
;
179 ndr_err
= ndr_push_struct_blob(&blob
, mem_ctx
, info3
,
180 (ndr_push_flags_fn_t
)ndr_push_netr_SamInfo3
);
181 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
182 DEBUG(0,("append_info3_as_ndr: failed to append\n"));
183 return ndr_map_error2ntstatus(ndr_err
);
186 resp
->extra_data
.data
= blob
.data
;
187 resp
->length
+= blob
.length
;
192 static NTSTATUS
append_unix_username(uint16_t validation_level
,
193 union netr_Validation
*validation
,
194 const char *name_domain
,
195 const char *name_user
,
197 char **_unix_username
)
199 TALLOC_CTX
*tmp_ctx
= NULL
;
200 const char *nt_username
= NULL
;
201 const char *nt_domain
= NULL
;
202 char *unix_username
= NULL
;
203 struct netr_SamBaseInfo
*base_info
= NULL
;
206 tmp_ctx
= talloc_new(mem_ctx
);
207 if (tmp_ctx
== NULL
) {
208 return NT_STATUS_NO_MEMORY
;
211 /* We've been asked to return the unix username, per
212 'winbind use default domain' settings and the like */
214 switch (validation_level
) {
216 base_info
= &validation
->sam3
->base
;
219 base_info
= &validation
->sam6
->base
;
222 DBG_ERR("Invalid validation level %d\n", validation_level
);
223 status
= NT_STATUS_INTERNAL_ERROR
;
227 nt_domain
= talloc_strdup(tmp_ctx
, base_info
->logon_domain
.string
);
229 /* If the server didn't give us one, just use the one
231 nt_domain
= name_domain
;
234 nt_username
= talloc_strdup(tmp_ctx
, base_info
->account_name
.string
);
236 /* If the server didn't give us one, just use the one
238 nt_username
= name_user
;
241 unix_username
= fill_domain_username_talloc(tmp_ctx
,
245 if (unix_username
== NULL
) {
246 status
= NT_STATUS_NO_MEMORY
;
250 DBG_INFO("Setting unix username to [%s]\n", unix_username
);
252 *_unix_username
= talloc_move(mem_ctx
, &unix_username
);
254 status
= NT_STATUS_OK
;
256 TALLOC_FREE(tmp_ctx
);
261 static NTSTATUS
append_afs_token(uint16_t validation_level
,
262 union netr_Validation
*validation
,
263 const char *name_domain
,
264 const char *name_user
,
268 TALLOC_CTX
*tmp_ctx
= NULL
;
269 char *afsname
= NULL
;
272 struct netr_SamBaseInfo
*base_info
= NULL
;
275 tmp_ctx
= talloc_new(mem_ctx
);
276 if (tmp_ctx
== NULL
) {
277 return NT_STATUS_NO_MEMORY
;
280 switch (validation_level
) {
282 base_info
= &validation
->sam3
->base
;
285 base_info
= &validation
->sam6
->base
;
288 DBG_ERR("Invalid validation level %d\n", validation_level
);
289 status
= NT_STATUS_INTERNAL_ERROR
;
293 afsname
= talloc_strdup(tmp_ctx
, lp_afs_username_map());
294 if (afsname
== NULL
) {
295 status
= NT_STATUS_NO_MEMORY
;
299 afsname
= talloc_string_sub(tmp_ctx
,
300 lp_afs_username_map(),
302 afsname
= talloc_string_sub(tmp_ctx
, afsname
,
304 afsname
= talloc_string_sub(tmp_ctx
, afsname
,
308 struct dom_sid user_sid
;
309 struct dom_sid_buf sidstr
;
311 sid_compose(&user_sid
, base_info
->domain_sid
, base_info
->rid
);
312 afsname
= talloc_string_sub(
316 dom_sid_str_buf(&user_sid
, &sidstr
));
319 if (afsname
== NULL
) {
320 status
= NT_STATUS_NO_MEMORY
;
324 if (!strlower_m(afsname
)) {
325 status
= NT_STATUS_INVALID_PARAMETER
;
329 DEBUG(10, ("Generating token for user %s\n", afsname
));
331 cell
= strchr(afsname
, '@');
334 status
= NT_STATUS_NO_MEMORY
;
341 token
= afs_createtoken_str(afsname
, cell
);
343 status
= NT_STATUS_OK
;
347 talloc_steal(mem_ctx
, token
);
348 *_blob
= data_blob_string_const_null(token
);
350 status
= NT_STATUS_OK
;
352 TALLOC_FREE(tmp_ctx
);
357 static NTSTATUS
extra_data_to_sid_array(const char *group_sid
,
359 struct wbint_SidArray
**_sid_array
)
361 TALLOC_CTX
*tmp_ctx
= NULL
;
362 struct wbint_SidArray
*sid_array
= NULL
;
363 struct dom_sid
*require_membership_of_sid
= NULL
;
364 uint32_t num_require_membership_of_sid
= 0;
365 char *req_sid
= NULL
;
366 const char *p
= NULL
;
369 if (_sid_array
== NULL
) {
370 return NT_STATUS_INVALID_PARAMETER
;
375 tmp_ctx
= talloc_new(mem_ctx
);
376 if (tmp_ctx
== NULL
) {
377 return NT_STATUS_NO_MEMORY
;
380 sid_array
= talloc_zero(tmp_ctx
, struct wbint_SidArray
);
381 if (sid_array
== NULL
) {
382 status
= NT_STATUS_NO_MEMORY
;
386 if (!group_sid
|| !group_sid
[0]) {
387 /* NO sid supplied, all users may access */
388 status
= NT_STATUS_OK
;
390 * Always return an allocated wbint_SidArray,
391 * even if the array is empty.
396 num_require_membership_of_sid
= 0;
397 require_membership_of_sid
= NULL
;
400 while (next_token_talloc(tmp_ctx
, &p
, &req_sid
, ",")) {
403 if (!string_to_sid(&sid
, req_sid
)) {
404 DBG_ERR("check_info3_in_group: could not parse %s "
405 "as a SID!\n", req_sid
);
406 status
= NT_STATUS_INVALID_PARAMETER
;
410 status
= add_sid_to_array(tmp_ctx
, &sid
,
411 &require_membership_of_sid
,
412 &num_require_membership_of_sid
);
413 if (!NT_STATUS_IS_OK(status
)) {
414 DBG_ERR("add_sid_to_array failed\n");
419 sid_array
->num_sids
= num_require_membership_of_sid
;
420 sid_array
->sids
= talloc_move(sid_array
, &require_membership_of_sid
);
422 status
= NT_STATUS_OK
;
424 *_sid_array
= talloc_move(mem_ctx
, &sid_array
);
427 TALLOC_FREE(tmp_ctx
);
432 static NTSTATUS
check_info3_in_group(struct netr_SamInfo3
*info3
,
433 struct wbint_SidArray
*sid_array
)
435 * Check whether a user belongs to a group or list of groups.
437 * @param mem_ctx talloc memory context.
438 * @param info3 user information, including group membership info.
439 * @param group_sid One or more groups , separated by commas.
441 * @return NT_STATUS_OK on success,
442 * NT_STATUS_LOGON_FAILURE if the user does not belong,
443 * or other NT_STATUS_IS_ERR(status) for other kinds of failure.
447 struct security_token
*token
;
450 /* Parse the 'required group' SID */
452 if (sid_array
== NULL
|| sid_array
->num_sids
== 0) {
453 /* NO sid supplied, all users may access */
457 token
= talloc_zero(talloc_tos(), struct security_token
);
459 DEBUG(0, ("talloc failed\n"));
460 return NT_STATUS_NO_MEMORY
;
463 status
= sid_array_from_info3(talloc_tos(), info3
,
467 if (!NT_STATUS_IS_OK(status
)) {
471 if (!NT_STATUS_IS_OK(status
= add_aliases(get_global_sam_sid(),
473 || !NT_STATUS_IS_OK(status
= add_aliases(&global_sid_Builtin
,
475 DEBUG(3, ("could not add aliases: %s\n",
480 security_token_debug(DBGC_CLASS
, 10, token
);
482 for (i
=0; i
<sid_array
->num_sids
; i
++) {
483 struct dom_sid_buf buf
;
484 DEBUG(10, ("Checking SID %s\n",
485 dom_sid_str_buf(&sid_array
->sids
[i
],
487 if (nt_token_check_sid(&sid_array
->sids
[i
],
489 DEBUG(10, ("Access ok\n"));
494 /* Do not distinguish this error from a wrong username/pw */
496 return NT_STATUS_LOGON_FAILURE
;
499 struct winbindd_domain
*find_auth_domain(uint8_t flags
,
500 const char *domain_name
)
502 struct winbindd_domain
*domain
;
505 domain
= find_domain_from_name_noinit(domain_name
);
506 if (domain
== NULL
) {
507 DEBUG(3, ("Authentication for domain [%s] refused "
508 "as it is not a trusted domain\n",
513 if (domain
->secure_channel_type
!= SEC_CHAN_NULL
) {
517 return domain
->routing_domain
;
520 if (strequal(domain_name
, get_global_sam_name())) {
521 return find_domain_from_name_noinit(domain_name
);
524 if (lp_winbind_use_krb5_enterprise_principals()) {
526 * If we use enterprise principals
527 * we always go trough our primary domain
528 * and follow the WRONG_REALM replies.
530 flags
&= ~WBFLAG_PAM_CONTACT_TRUSTDOM
;
533 /* we can auth against trusted domains */
534 if (flags
& WBFLAG_PAM_CONTACT_TRUSTDOM
) {
535 domain
= find_domain_from_name_noinit(domain_name
);
536 if (domain
== NULL
) {
537 DEBUG(3, ("Authentication for domain [%s] skipped "
538 "as it is not a trusted domain\n",
545 return find_our_domain();
548 static void fake_password_policy(struct winbindd_response
*r
,
549 const struct netr_SamBaseInfo
*bi
)
551 NTTIME min_password_age
;
552 NTTIME max_password_age
;
554 if (bi
->allow_password_change
> bi
->last_password_change
) {
555 min_password_age
= bi
->allow_password_change
-
556 bi
->last_password_change
;
558 min_password_age
= 0;
561 if (bi
->force_password_change
> bi
->last_password_change
) {
562 max_password_age
= bi
->force_password_change
-
563 bi
->last_password_change
;
565 max_password_age
= 0;
568 r
->data
.auth
.policy
.min_length_password
= 0;
569 r
->data
.auth
.policy
.password_history
= 0;
570 r
->data
.auth
.policy
.password_properties
= 0;
571 r
->data
.auth
.policy
.expire
=
572 nt_time_to_unix_abs(&max_password_age
);
573 r
->data
.auth
.policy
.min_passwordage
=
574 nt_time_to_unix_abs(&min_password_age
);
577 static void fill_in_password_policy(struct winbindd_response
*r
,
578 const struct samr_DomInfo1
*p
)
580 r
->data
.auth
.policy
.min_length_password
=
581 p
->min_password_length
;
582 r
->data
.auth
.policy
.password_history
=
583 p
->password_history_length
;
584 r
->data
.auth
.policy
.password_properties
=
585 p
->password_properties
;
586 r
->data
.auth
.policy
.expire
=
587 nt_time_to_unix_abs((const NTTIME
*)&(p
->max_password_age
));
588 r
->data
.auth
.policy
.min_passwordage
=
589 nt_time_to_unix_abs((const NTTIME
*)&(p
->min_password_age
));
592 static NTSTATUS
fillup_password_policy(struct winbindd_domain
*domain
,
593 struct winbindd_response
*response
)
595 TALLOC_CTX
*frame
= talloc_stackframe();
597 struct samr_DomInfo1 password_policy
;
599 if ( !winbindd_can_contact_domain( domain
) ) {
600 DEBUG(5,("fillup_password_policy: No inbound trust to "
601 "contact domain %s\n", domain
->name
));
602 status
= NT_STATUS_NOT_SUPPORTED
;
606 status
= wb_cache_password_policy(domain
, talloc_tos(),
608 if (NT_STATUS_IS_ERR(status
)) {
612 fill_in_password_policy(response
, &password_policy
);
619 static NTSTATUS
get_max_bad_attempts_from_lockout_policy(struct winbindd_domain
*domain
,
621 uint16_t *lockout_threshold
)
623 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
624 struct samr_DomInfo12 lockout_policy
;
626 *lockout_threshold
= 0;
628 status
= wb_cache_lockout_policy(domain
, mem_ctx
, &lockout_policy
);
629 if (NT_STATUS_IS_ERR(status
)) {
633 *lockout_threshold
= lockout_policy
.lockout_threshold
;
638 static NTSTATUS
get_pwd_properties(struct winbindd_domain
*domain
,
640 uint32_t *password_properties
)
642 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
643 struct samr_DomInfo1 password_policy
;
645 *password_properties
= 0;
647 status
= wb_cache_password_policy(domain
, mem_ctx
, &password_policy
);
648 if (NT_STATUS_IS_ERR(status
)) {
652 *password_properties
= password_policy
.password_properties
;
659 static const char *generate_krb5_ccache(TALLOC_CTX
*mem_ctx
,
662 const char **user_ccache_file
)
664 /* accept FILE and WRFILE as krb5_cc_type from the client and then
665 * build the full ccname string based on the user's uid here -
668 const char *gen_cc
= NULL
;
671 if (strequal(type
, "FILE")) {
672 gen_cc
= talloc_asprintf(
673 mem_ctx
, "FILE:/tmp/krb5cc_%d", uid
);
675 if (strequal(type
, "WRFILE")) {
676 gen_cc
= talloc_asprintf(
677 mem_ctx
, "WRFILE:/tmp/krb5cc_%d", uid
);
679 if (strequal(type
, "KEYRING")) {
680 gen_cc
= talloc_asprintf(
681 mem_ctx
, "KEYRING:persistent:%d", uid
);
683 if (strequal(type
, "KCM")) {
684 gen_cc
= talloc_asprintf(mem_ctx
,
689 if (strnequal(type
, "FILE:/", 6) ||
690 strnequal(type
, "WRFILE:/", 8) ||
691 strnequal(type
, "DIR:/", 5)) {
693 /* we allow only one "%u" substitution */
697 p
= strchr(type
, '%');
702 if (p
!= NULL
&& *p
== 'u' && strchr(p
, '%') == NULL
) {
703 char uid_str
[sizeof("18446744073709551615")];
705 snprintf(uid_str
, sizeof(uid_str
), "%u", uid
);
707 gen_cc
= talloc_string_sub2(mem_ctx
,
711 /* remove_unsafe_characters */
715 /* allow_trailing_dollar */
722 *user_ccache_file
= gen_cc
;
724 if (gen_cc
== NULL
) {
725 gen_cc
= talloc_strdup(mem_ctx
, "MEMORY:winbindd_pam_ccache");
727 if (gen_cc
== NULL
) {
728 DEBUG(0,("out of memory\n"));
732 DEBUG(10, ("using ccache: %s%s\n", gen_cc
,
733 (*user_ccache_file
== NULL
) ? " (internal)":""));
740 uid_t
get_uid_from_request(struct winbindd_request
*request
)
744 uid
= request
->data
.auth
.uid
;
746 if (uid
== (uid_t
)-1) {
747 DEBUG(1,("invalid uid: '%u'\n", (unsigned int)uid
));
753 /**********************************************************************
754 Authenticate a user with a clear text password using Kerberos and fill up
756 **********************************************************************/
758 static NTSTATUS
winbindd_raw_kerberos_login(TALLOC_CTX
*mem_ctx
,
759 struct winbindd_domain
*domain
,
762 const char *krb5_cc_type
,
764 struct netr_SamInfo6
**info6
,
765 const char **_krb5ccname
)
768 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
769 krb5_error_code krb5_ret
;
770 const char *cc
= NULL
;
771 const char *principal_s
= NULL
;
772 const char *service
= NULL
;
774 fstring name_namespace
, name_domain
, name_user
;
775 time_t ticket_lifetime
= 0;
776 time_t renewal_until
= 0;
778 time_t time_offset
= 0;
779 const char *user_ccache_file
;
780 struct PAC_LOGON_INFO
*logon_info
= NULL
;
781 struct PAC_UPN_DNS_INFO
*upn_dns_info
= NULL
;
782 struct PAC_DATA
*pac_data
= NULL
;
783 struct PAC_DATA_CTR
*pac_data_ctr
= NULL
;
784 const char *local_service
;
786 struct netr_SamInfo6
*info6_copy
= NULL
;
787 char *canon_principal
= NULL
;
788 char *canon_realm
= NULL
;
793 if (domain
->alt_name
== NULL
) {
794 return NT_STATUS_INVALID_PARAMETER
;
797 if (_krb5ccname
!= NULL
) {
802 * prepare a krb5_cc_cache string for the user */
805 DEBUG(0,("no valid uid\n"));
808 cc
= generate_krb5_ccache(mem_ctx
,
813 return NT_STATUS_NO_MEMORY
;
818 * get kerberos properties */
820 if (domain
->private_data
) {
821 ads
= (ADS_STRUCT
*)domain
->private_data
;
822 time_offset
= ads
->auth
.time_offset
;
827 * do kerberos auth and setup ccache as the user */
829 ok
= parse_domain_user(user
, name_namespace
, name_domain
, name_user
);
831 return NT_STATUS_INVALID_PARAMETER
;
834 realm
= talloc_strdup(mem_ctx
, domain
->alt_name
);
836 return NT_STATUS_NO_MEMORY
;
839 if (!strupper_m(realm
)) {
840 return NT_STATUS_INVALID_PARAMETER
;
843 if (lp_winbind_use_krb5_enterprise_principals() &&
844 name_namespace
[0] != '\0')
846 principal_s
= talloc_asprintf(mem_ctx
,
852 principal_s
= talloc_asprintf(mem_ctx
,
857 if (principal_s
== NULL
) {
858 return NT_STATUS_NO_MEMORY
;
861 service
= talloc_asprintf(mem_ctx
, "%s/%s@%s", KRB5_TGS_NAME
, realm
, realm
);
862 if (service
== NULL
) {
863 return NT_STATUS_NO_MEMORY
;
866 local_service
= talloc_asprintf(mem_ctx
, "%s$@%s",
867 lp_netbios_name(), lp_realm());
868 if (local_service
== NULL
) {
869 return NT_STATUS_NO_MEMORY
;
873 /* if this is a user ccache, we need to act as the user to let the krb5
874 * library handle the chown, etc. */
876 /************************ ENTERING NON-ROOT **********************/
878 if (user_ccache_file
!= NULL
) {
879 set_effective_uid(uid
);
880 DEBUG(10,("winbindd_raw_kerberos_login: uid is %d\n", uid
));
883 result
= kerberos_return_pac(mem_ctx
,
892 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
898 if (user_ccache_file
!= NULL
) {
899 gain_root_privilege();
902 /************************ RETURNED TO ROOT **********************/
904 if (!NT_STATUS_IS_OK(result
)) {
908 if (pac_data_ctr
== NULL
) {
912 pac_data
= pac_data_ctr
->pac_data
;
913 if (pac_data
== NULL
) {
917 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
919 if (pac_data
->buffers
[i
].type
== PAC_TYPE_LOGON_INFO
) {
920 logon_info
= pac_data
->buffers
[i
].info
->logon_info
.info
;
924 if (pac_data
->buffers
[i
].type
== PAC_TYPE_UPN_DNS_INFO
) {
925 upn_dns_info
= &pac_data
->buffers
[i
].info
->upn_dns_info
;
930 if (logon_info
== NULL
) {
931 DEBUG(10,("Missing logon_info in ticket of %s\n",
933 return NT_STATUS_INVALID_PARAMETER
;
936 DEBUG(10,("winbindd_raw_kerberos_login: winbindd validated ticket of %s\n",
939 result
= create_info6_from_pac(mem_ctx
, logon_info
,
940 upn_dns_info
, &info6_copy
);
941 if (!NT_STATUS_IS_OK(result
)) {
945 /* if we had a user's ccache then return that string for the pam
948 if (user_ccache_file
!= NULL
) {
950 if (_krb5ccname
!= NULL
) {
951 *_krb5ccname
= talloc_steal(mem_ctx
, user_ccache_file
);
954 result
= add_ccache_to_list(principal_s
,
968 if (!NT_STATUS_IS_OK(result
)) {
969 DEBUG(10,("winbindd_raw_kerberos_login: failed to add ccache to list: %s\n",
974 /* need to delete the memory cred cache, it is not used anymore */
976 krb5_ret
= ads_kdestroy(cc
);
978 DEBUG(3,("winbindd_raw_kerberos_login: "
979 "could not destroy krb5 credential cache: "
980 "%s\n", error_message(krb5_ret
)));
989 * Do not delete an existing valid credential cache, if the user
990 * e.g. enters a wrong password
992 if ((strequal(krb5_cc_type
, "FILE") || strequal(krb5_cc_type
, "WRFILE"))
993 && user_ccache_file
!= NULL
) {
997 /* we could have created a new credential cache with a valid tgt in it
998 * but we werent able to get or verify the service ticket for this
999 * local host and therefor didn't get the PAC, we need to remove that
1000 * cache entirely now */
1002 krb5_ret
= ads_kdestroy(cc
);
1004 DEBUG(3,("winbindd_raw_kerberos_login: "
1005 "could not destroy krb5 credential cache: "
1006 "%s\n", error_message(krb5_ret
)));
1009 if (!NT_STATUS_IS_OK(remove_ccache(user
))) {
1010 DEBUG(3,("winbindd_raw_kerberos_login: "
1011 "could not remove ccache for user %s\n",
1017 return NT_STATUS_NOT_SUPPORTED
;
1018 #endif /* HAVE_KRB5 */
1021 /****************************************************************
1022 ****************************************************************/
1024 bool check_request_flags(uint32_t flags
)
1026 uint32_t flags_edata
= WBFLAG_PAM_AFS_TOKEN
|
1027 WBFLAG_PAM_INFO3_TEXT
|
1028 WBFLAG_PAM_INFO3_NDR
;
1030 if ( ( (flags
& flags_edata
) == WBFLAG_PAM_AFS_TOKEN
) ||
1031 ( (flags
& flags_edata
) == WBFLAG_PAM_INFO3_NDR
) ||
1032 ( (flags
& flags_edata
) == WBFLAG_PAM_INFO3_TEXT
)||
1033 !(flags
& flags_edata
) ) {
1037 DEBUG(1, ("check_request_flags: invalid request flags[0x%08X]\n",
1043 /****************************************************************
1044 ****************************************************************/
1046 NTSTATUS
append_auth_data(TALLOC_CTX
*mem_ctx
,
1047 struct winbindd_response
*resp
,
1048 uint32_t request_flags
,
1049 uint16_t validation_level
,
1050 union netr_Validation
*validation
,
1051 const char *name_domain
,
1052 const char *name_user
)
1054 struct netr_SamInfo3
*info3
= NULL
;
1055 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1057 result
= map_validation_to_info3(talloc_tos(),
1061 if (!NT_STATUS_IS_OK(result
)) {
1065 if (request_flags
& WBFLAG_PAM_USER_SESSION_KEY
) {
1066 memcpy(resp
->data
.auth
.user_session_key
,
1067 info3
->base
.key
.key
,
1068 sizeof(resp
->data
.auth
.user_session_key
)
1072 if (request_flags
& WBFLAG_PAM_LMKEY
) {
1073 memcpy(resp
->data
.auth
.first_8_lm_hash
,
1074 info3
->base
.LMSessKey
.key
,
1075 sizeof(resp
->data
.auth
.first_8_lm_hash
)
1079 if (request_flags
& WBFLAG_PAM_UNIX_NAME
) {
1080 char *unix_username
= NULL
;
1081 result
= append_unix_username(validation_level
,
1087 if (!NT_STATUS_IS_OK(result
)) {
1088 DEBUG(10,("Failed to append Unix Username: %s\n",
1089 nt_errstr(result
)));
1092 fstrcpy(resp
->data
.auth
.unix_username
, unix_username
);
1093 TALLOC_FREE(unix_username
);
1096 /* currently, anything from here on potentially overwrites extra_data. */
1098 if (request_flags
& WBFLAG_PAM_INFO3_NDR
) {
1099 result
= append_info3_as_ndr(mem_ctx
, resp
, info3
);
1100 if (!NT_STATUS_IS_OK(result
)) {
1101 DEBUG(10,("Failed to append INFO3 (NDR): %s\n",
1102 nt_errstr(result
)));
1107 if (request_flags
& WBFLAG_PAM_INFO3_TEXT
) {
1108 result
= append_info3_as_txt(mem_ctx
, resp
,
1111 if (!NT_STATUS_IS_OK(result
)) {
1112 DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
1113 nt_errstr(result
)));
1118 if (request_flags
& WBFLAG_PAM_AFS_TOKEN
) {
1119 DATA_BLOB blob
= data_blob_null
;
1120 result
= append_afs_token(validation_level
,
1126 if (!NT_STATUS_IS_OK(result
)) {
1127 DEBUG(10,("Failed to append AFS token: %s\n",
1128 nt_errstr(result
)));
1131 resp
->extra_data
.data
= blob
.data
;
1132 resp
->length
+= blob
.length
;
1135 result
= NT_STATUS_OK
;
1141 static NTSTATUS
winbindd_dual_pam_auth_cached(struct winbindd_domain
*domain
,
1142 struct winbindd_cli_state
*state
,
1143 struct netr_SamInfo3
**info3
)
1145 NTSTATUS result
= NT_STATUS_LOGON_FAILURE
;
1146 uint16_t max_allowed_bad_attempts
;
1147 fstring name_namespace
, name_domain
, name_user
;
1149 enum lsa_SidType type
;
1150 uchar new_nt_pass
[NT_HASH_LEN
];
1151 const uint8_t *cached_nt_pass
;
1152 const uint8_t *cached_salt
;
1153 struct netr_SamInfo3
*my_info3
;
1154 time_t kickoff_time
, must_change_time
;
1155 bool password_good
= false;
1158 struct winbindd_tdc_domain
*tdc_domain
= NULL
;
1163 ZERO_STRUCTP(info3
);
1165 DEBUG(10,("winbindd_dual_pam_auth_cached\n"));
1167 /* Parse domain and username */
1169 ok
= parse_domain_user(state
->request
->data
.auth
.user
,
1174 DBG_DEBUG("parse_domain_user failed\n");
1175 return NT_STATUS_NO_SUCH_USER
;
1178 if (!lookup_cached_name(name_namespace
,
1183 DEBUG(10,("winbindd_dual_pam_auth_cached: no such user in the cache\n"));
1184 return NT_STATUS_NO_SUCH_USER
;
1187 if (type
!= SID_NAME_USER
) {
1188 DEBUG(10,("winbindd_dual_pam_auth_cached: not a user (%s)\n", sid_type_lookup(type
)));
1189 return NT_STATUS_LOGON_FAILURE
;
1192 result
= winbindd_get_creds(domain
,
1198 if (!NT_STATUS_IS_OK(result
)) {
1199 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get creds: %s\n", nt_errstr(result
)));
1205 E_md4hash(state
->request
->data
.auth
.pass
, new_nt_pass
);
1207 dump_data_pw("new_nt_pass", new_nt_pass
, NT_HASH_LEN
);
1208 dump_data_pw("cached_nt_pass", cached_nt_pass
, NT_HASH_LEN
);
1210 dump_data_pw("cached_salt", cached_salt
, NT_HASH_LEN
);
1214 /* In this case we didn't store the nt_hash itself,
1215 but the MD5 combination of salt + nt_hash. */
1216 uchar salted_hash
[NT_HASH_LEN
];
1217 gnutls_hash_hd_t hash_hnd
= NULL
;
1220 rc
= gnutls_hash_init(&hash_hnd
, GNUTLS_DIG_MD5
);
1222 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
);
1225 rc
= gnutls_hash(hash_hnd
, cached_salt
, 16);
1227 gnutls_hash_deinit(hash_hnd
, NULL
);
1228 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
);
1230 rc
= gnutls_hash(hash_hnd
, new_nt_pass
, 16);
1232 gnutls_hash_deinit(hash_hnd
, NULL
);
1233 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
);
1235 gnutls_hash_deinit(hash_hnd
, salted_hash
);
1237 password_good
= (memcmp(cached_nt_pass
, salted_hash
,
1240 /* Old cached cred - direct store of nt_hash (bad bad bad !). */
1241 password_good
= (memcmp(cached_nt_pass
, new_nt_pass
,
1245 if (password_good
) {
1247 /* User *DOES* know the password, update logon_time and reset
1250 my_info3
->base
.user_flags
|= NETLOGON_CACHED_ACCOUNT
;
1252 if (my_info3
->base
.acct_flags
& ACB_AUTOLOCK
) {
1253 return NT_STATUS_ACCOUNT_LOCKED_OUT
;
1256 if (my_info3
->base
.acct_flags
& ACB_DISABLED
) {
1257 return NT_STATUS_ACCOUNT_DISABLED
;
1260 if (my_info3
->base
.acct_flags
& ACB_WSTRUST
) {
1261 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
;
1264 if (my_info3
->base
.acct_flags
& ACB_SVRTRUST
) {
1265 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT
;
1268 if (my_info3
->base
.acct_flags
& ACB_DOMTRUST
) {
1269 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
;
1272 if (!(my_info3
->base
.acct_flags
& ACB_NORMAL
)) {
1273 DEBUG(0,("winbindd_dual_pam_auth_cached: whats wrong with that one?: 0x%08x\n",
1274 my_info3
->base
.acct_flags
));
1275 return NT_STATUS_LOGON_FAILURE
;
1278 kickoff_time
= nt_time_to_unix(my_info3
->base
.kickoff_time
);
1279 if (kickoff_time
!= 0 && time(NULL
) > kickoff_time
) {
1280 return NT_STATUS_ACCOUNT_EXPIRED
;
1283 must_change_time
= nt_time_to_unix(my_info3
->base
.force_password_change
);
1284 if (must_change_time
!= 0 && must_change_time
< time(NULL
)) {
1285 /* we allow grace logons when the password has expired */
1286 my_info3
->base
.user_flags
|= NETLOGON_GRACE_LOGON
;
1287 /* return NT_STATUS_PASSWORD_EXPIRED; */
1292 if ((state
->request
->flags
& WBFLAG_PAM_KRB5
) &&
1293 ((tdc_domain
= wcache_tdc_fetch_domain(state
->mem_ctx
, name_domain
)) != NULL
) &&
1294 ((tdc_domain
->trust_type
& LSA_TRUST_TYPE_UPLEVEL
) ||
1295 /* used to cope with the case winbindd starting without network. */
1296 !strequal(tdc_domain
->domain_name
, tdc_domain
->dns_name
))) {
1299 const char *cc
= NULL
;
1301 const char *principal_s
= NULL
;
1302 const char *service
= NULL
;
1303 const char *user_ccache_file
;
1305 if (domain
->alt_name
== NULL
) {
1306 return NT_STATUS_INVALID_PARAMETER
;
1309 uid
= get_uid_from_request(state
->request
);
1311 DEBUG(0,("winbindd_dual_pam_auth_cached: invalid uid\n"));
1312 return NT_STATUS_INVALID_PARAMETER
;
1315 cc
= generate_krb5_ccache(state
->mem_ctx
,
1316 state
->request
->data
.auth
.krb5_cc_type
,
1317 state
->request
->data
.auth
.uid
,
1320 return NT_STATUS_NO_MEMORY
;
1323 realm
= talloc_strdup(state
->mem_ctx
, domain
->alt_name
);
1324 if (realm
== NULL
) {
1325 return NT_STATUS_NO_MEMORY
;
1328 if (!strupper_m(realm
)) {
1329 return NT_STATUS_INVALID_PARAMETER
;
1332 principal_s
= talloc_asprintf(state
->mem_ctx
, "%s@%s", name_user
, realm
);
1333 if (principal_s
== NULL
) {
1334 return NT_STATUS_NO_MEMORY
;
1337 service
= talloc_asprintf(state
->mem_ctx
, "%s/%s@%s", KRB5_TGS_NAME
, realm
, realm
);
1338 if (service
== NULL
) {
1339 return NT_STATUS_NO_MEMORY
;
1342 if (user_ccache_file
!= NULL
) {
1344 fstrcpy(state
->response
->data
.auth
.krb5ccname
,
1347 result
= add_ccache_to_list(principal_s
,
1350 state
->request
->data
.auth
.user
,
1351 state
->request
->data
.auth
.pass
,
1355 time(NULL
) + lp_winbind_cache_time(),
1356 time(NULL
) + WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
1361 if (!NT_STATUS_IS_OK(result
)) {
1362 DEBUG(10,("winbindd_dual_pam_auth_cached: failed "
1363 "to add ccache to list: %s\n",
1364 nt_errstr(result
)));
1368 #endif /* HAVE_KRB5 */
1370 /* FIXME: we possibly should handle logon hours as well (does xp when
1371 * offline?) see auth/auth_sam.c:sam_account_ok for details */
1373 unix_to_nt_time(&my_info3
->base
.logon_time
, time(NULL
));
1374 my_info3
->base
.bad_password_count
= 0;
1376 result
= winbindd_update_creds_by_info3(domain
,
1377 state
->request
->data
.auth
.user
,
1378 state
->request
->data
.auth
.pass
,
1380 if (!NT_STATUS_IS_OK(result
)) {
1381 DEBUG(1,("winbindd_dual_pam_auth_cached: failed to update creds: %s\n",
1382 nt_errstr(result
)));
1386 return NT_STATUS_OK
;
1390 /* User does *NOT* know the correct password, modify info3 accordingly, but only if online */
1391 if (domain
->online
== false) {
1395 /* failure of this is not critical */
1396 result
= get_max_bad_attempts_from_lockout_policy(domain
, state
->mem_ctx
, &max_allowed_bad_attempts
);
1397 if (!NT_STATUS_IS_OK(result
)) {
1398 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get max_allowed_bad_attempts. "
1399 "Won't be able to honour account lockout policies\n"));
1402 /* increase counter */
1403 my_info3
->base
.bad_password_count
++;
1405 if (max_allowed_bad_attempts
== 0) {
1410 if (my_info3
->base
.bad_password_count
>= max_allowed_bad_attempts
) {
1412 uint32_t password_properties
;
1414 result
= get_pwd_properties(domain
, state
->mem_ctx
, &password_properties
);
1415 if (!NT_STATUS_IS_OK(result
)) {
1416 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get password properties.\n"));
1419 if ((my_info3
->base
.rid
!= DOMAIN_RID_ADMINISTRATOR
) ||
1420 (password_properties
& DOMAIN_PASSWORD_LOCKOUT_ADMINS
)) {
1421 my_info3
->base
.acct_flags
|= ACB_AUTOLOCK
;
1426 result
= winbindd_update_creds_by_info3(domain
,
1427 state
->request
->data
.auth
.user
,
1431 if (!NT_STATUS_IS_OK(result
)) {
1432 DEBUG(0,("winbindd_dual_pam_auth_cached: failed to update creds %s\n",
1433 nt_errstr(result
)));
1436 return NT_STATUS_LOGON_FAILURE
;
1439 static NTSTATUS
winbindd_dual_pam_auth_kerberos(struct winbindd_domain
*domain
,
1442 const char *krb5_cc_type
,
1444 TALLOC_CTX
*mem_ctx
,
1445 struct netr_SamInfo6
**info6
,
1446 const char **_krb5ccname
)
1448 struct winbindd_domain
*contact_domain
;
1449 fstring name_namespace
, name_domain
, name_user
;
1453 DEBUG(10,("winbindd_dual_pam_auth_kerberos\n"));
1455 /* Parse domain and username */
1457 ok
= parse_domain_user(user
,
1462 result
= NT_STATUS_INVALID_PARAMETER
;
1466 /* what domain should we contact? */
1468 if (lp_winbind_use_krb5_enterprise_principals()) {
1469 contact_domain
= find_auth_domain(0, name_namespace
);
1471 contact_domain
= find_domain_from_name(name_namespace
);
1473 if (contact_domain
== NULL
) {
1474 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1475 user
, name_domain
, name_user
, name_namespace
));
1476 result
= NT_STATUS_NO_SUCH_USER
;
1480 if (contact_domain
->initialized
&&
1481 contact_domain
->active_directory
) {
1485 if (!contact_domain
->initialized
) {
1486 init_dc_connection(contact_domain
, false);
1489 if (!contact_domain
->active_directory
) {
1490 DEBUG(3,("krb5 auth requested but domain (%s) is not Active Directory\n",
1491 contact_domain
->name
));
1492 return NT_STATUS_INVALID_LOGON_TYPE
;
1495 result
= winbindd_raw_kerberos_login(
1508 static NTSTATUS
winbindd_dual_auth_passdb(TALLOC_CTX
*mem_ctx
,
1509 uint32_t logon_parameters
,
1512 const uint64_t logon_id
,
1513 const char *client_name
,
1514 const int client_pid
,
1515 const DATA_BLOB
*challenge
,
1516 const DATA_BLOB
*lm_resp
,
1517 const DATA_BLOB
*nt_resp
,
1518 const struct tsocket_address
*remote
,
1519 const struct tsocket_address
*local
,
1521 uint8_t *pauthoritative
,
1522 struct netr_SamInfo3
**pinfo3
)
1524 struct auth_context
*auth_context
;
1525 struct auth_serversupplied_info
*server_info
;
1526 struct auth_usersupplied_info
*user_info
= NULL
;
1527 struct netr_SamInfo3
*info3
;
1530 TALLOC_CTX
*frame
= talloc_stackframe();
1533 * We are authoritative by default
1535 *pauthoritative
= 1;
1537 status
= make_user_info(frame
, &user_info
, user
, user
, domain
, domain
,
1538 lp_netbios_name(), remote
, local
,
1540 lm_resp
, nt_resp
, NULL
, NULL
,
1541 NULL
, AUTH_PASSWORD_RESPONSE
);
1542 if (!NT_STATUS_IS_OK(status
)) {
1543 DEBUG(10, ("make_user_info failed: %s\n", nt_errstr(status
)));
1548 user_info
->logon_parameters
= logon_parameters
;
1549 user_info
->logon_id
= logon_id
;
1550 user_info
->auth_description
= talloc_asprintf(
1551 frame
, "PASSDB, %s, %d", client_name
, client_pid
);
1552 if (user_info
->auth_description
== NULL
) {
1554 return NT_STATUS_NO_MEMORY
;
1557 /* We don't want to come back to winbindd or to do PAM account checks */
1558 user_info
->flags
|= USER_INFO_INFO3_AND_NO_AUTHZ
;
1561 user_info
->flags
|= USER_INFO_INTERACTIVE_LOGON
;
1564 status
= make_auth3_context_for_winbind(frame
, &auth_context
);
1565 if (!NT_STATUS_IS_OK(status
)) {
1566 DBG_ERR("make_auth3_context_for_winbind failed: %s\n",
1572 ok
= auth3_context_set_challenge(auth_context
,
1573 challenge
->data
, "fixed");
1576 return NT_STATUS_NO_MEMORY
;
1579 status
= auth_check_ntlm_password(mem_ctx
,
1584 if (!NT_STATUS_IS_OK(status
)) {
1589 info3
= talloc_zero(mem_ctx
, struct netr_SamInfo3
);
1590 if (info3
== NULL
) {
1592 return NT_STATUS_NO_MEMORY
;
1595 status
= serverinfo_to_SamInfo3(server_info
, info3
);
1596 if (!NT_STATUS_IS_OK(status
)) {
1599 DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
1600 nt_errstr(status
)));
1605 DBG_DEBUG("Authenticating user %s\\%s returned %s\n",
1613 static NTSTATUS
winbind_samlogon_retry_loop(struct winbindd_domain
*domain
,
1614 TALLOC_CTX
*mem_ctx
,
1615 uint32_t logon_parameters
,
1616 const char *username
,
1617 const char *password
,
1618 const char *domainname
,
1619 const char *workstation
,
1620 const uint64_t logon_id
,
1621 bool plaintext_given
,
1622 const uint8_t chal
[8],
1623 DATA_BLOB lm_response
,
1624 DATA_BLOB nt_response
,
1626 uint8_t *authoritative
,
1628 uint16_t *_validation_level
,
1629 union netr_Validation
**_validation
)
1632 int netr_attempts
= 0;
1635 enum netr_LogonInfoClass logon_type_i
;
1636 enum netr_LogonInfoClass logon_type_n
;
1637 uint16_t validation_level
= UINT16_MAX
;
1638 union netr_Validation
*validation
= NULL
;
1639 TALLOC_CTX
*base_ctx
= NULL
;
1640 struct netr_SamBaseInfo
*base_info
= NULL
;
1643 struct rpc_pipe_client
*netlogon_pipe
;
1644 struct netlogon_creds_cli_context
*netlogon_creds_ctx
= NULL
;
1648 result
= cm_connect_netlogon_secure(domain
, &netlogon_pipe
,
1649 &netlogon_creds_ctx
);
1651 if (NT_STATUS_EQUAL(result
,
1652 NT_STATUS_CANT_ACCESS_DOMAIN_INFO
)) {
1654 * This means we don't have a trust account.
1657 result
= NT_STATUS_NO_SUCH_USER
;
1661 if (!NT_STATUS_IS_OK(result
)) {
1662 DEBUG(3,("Could not open handle to NETLOGON pipe "
1663 "(error: %s, attempts: %d)\n",
1664 nt_errstr(result
), netr_attempts
));
1666 /* After the first retry always close the connection */
1667 if (netr_attempts
> 0) {
1668 DEBUG(3, ("This is again a problem for this "
1669 "particular call, forcing the close "
1670 "of this connection\n"));
1671 invalidate_cm_connection(domain
);
1674 /* After the second retry failover to the next DC */
1675 if (netr_attempts
> 1) {
1677 * If the netlogon server is not reachable then
1678 * it is possible that the DC is rebuilding
1679 * sysvol and shutdown netlogon for that time.
1680 * We should failover to the next dc.
1682 DEBUG(3, ("This is the third problem for this "
1683 "particular call, adding DC to the "
1684 "negative cache list: %s %s\n", domain
->name
, domain
->dcname
));
1685 add_failed_connection_entry(domain
->name
,
1688 saf_delete(domain
->name
);
1691 /* Only allow 3 retries */
1692 if (netr_attempts
< 3) {
1693 DEBUG(3, ("The connection to netlogon "
1694 "failed, retrying\n"));
1702 logon_type_i
= NetlogonInteractiveInformation
;
1703 logon_type_n
= NetlogonNetworkInformation
;
1704 if (domain
->domain_trust_attribs
& LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
) {
1705 logon_type_i
= NetlogonInteractiveTransitiveInformation
;
1706 logon_type_n
= NetlogonNetworkTransitiveInformation
;
1709 if (domain
->domain_trust_attribs
& LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE
) {
1710 logon_type_i
= NetlogonInteractiveTransitiveInformation
;
1711 logon_type_n
= NetlogonNetworkTransitiveInformation
;
1714 if (domain
->domain_trust_attribs
& LSA_TRUST_ATTRIBUTE_NON_TRANSITIVE
) {
1715 logon_type_i
= NetlogonInteractiveInformation
;
1716 logon_type_n
= NetlogonNetworkInformation
;
1719 if (domain
->domain_trust_attribs
& LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN
) {
1720 logon_type_i
= NetlogonInteractiveInformation
;
1721 logon_type_n
= NetlogonNetworkInformation
;
1725 if (plaintext_given
) {
1726 result
= rpccli_netlogon_password_logon(
1728 netlogon_pipe
->binding_handle
,
1741 } else if (interactive
) {
1742 result
= rpccli_netlogon_interactive_logon(
1744 netlogon_pipe
->binding_handle
,
1759 result
= rpccli_netlogon_network_logon(
1761 netlogon_pipe
->binding_handle
,
1779 * we increment this after the "feature negotiation"
1780 * for can_do_samlogon_ex and can_do_validation6
1784 /* We have to try a second time as cm_connect_netlogon
1785 might not yet have noticed that the DC has killed
1788 if (!rpccli_is_connected(netlogon_pipe
)) {
1793 /* if we get access denied, a possible cause was that we had
1794 an open connection to the DC, but someone changed our
1795 machine account password out from underneath us using 'net
1796 rpc changetrustpw' */
1798 if ( NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) ) {
1799 DEBUG(1,("winbind_samlogon_retry_loop: sam_logon returned "
1800 "ACCESS_DENIED. Maybe the DC has Restrict "
1801 "NTLM set or the trust account "
1802 "password was changed and we didn't know it. "
1803 "Killing connections to domain %s\n",
1805 invalidate_cm_connection(domain
);
1809 if (NT_STATUS_EQUAL(result
, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
)) {
1811 * Got DCERPC_FAULT_OP_RNG_ERROR for SamLogon
1812 * (no Ex). This happens against old Samba
1813 * DCs, if LogonSamLogonEx() fails with an error
1814 * e.g. NT_STATUS_NO_SUCH_USER or NT_STATUS_WRONG_PASSWORD.
1816 * The server will log something like this:
1817 * api_net_sam_logon_ex: Failed to marshall NET_R_SAM_LOGON_EX.
1819 * This sets the whole connection into a fault_state mode
1820 * and all following request get NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE.
1822 * This also happens to our retry with LogonSamLogonWithFlags()
1823 * and LogonSamLogon().
1825 * In order to recover from this situation, we need to
1826 * drop the connection.
1828 invalidate_cm_connection(domain
);
1829 result
= NT_STATUS_LOGON_FAILURE
;
1833 } while ( (attempts
< 2) && retry
);
1835 if (NT_STATUS_EQUAL(result
, NT_STATUS_IO_TIMEOUT
)) {
1836 DEBUG(3,("winbind_samlogon_retry_loop: sam_network_logon(ex) "
1837 "returned NT_STATUS_IO_TIMEOUT after the retry. "
1838 "Killing connections to domain %s\n",
1840 invalidate_cm_connection(domain
);
1843 if (!NT_STATUS_IS_OK(result
)) {
1847 switch (validation_level
) {
1849 base_ctx
= validation
->sam3
;
1850 base_info
= &validation
->sam3
->base
;
1853 base_ctx
= validation
->sam6
;
1854 base_info
= &validation
->sam6
->base
;
1857 smb_panic(__location__
);
1860 if (base_info
->acct_flags
== 0 || base_info
->account_name
.string
== NULL
) {
1861 struct dom_sid user_sid
;
1862 struct dom_sid_buf sid_buf
;
1863 const char *acct_flags_src
= "server";
1864 const char *acct_name_src
= "server";
1867 * Handle the case where a NT4 DC does not fill in the acct_flags in
1868 * the samlogon reply info3. Yes, in 2021, there are still admins
1869 * arround with real NT4 DCs.
1871 * We used to call dcerpc_samr_QueryUserInfo(level=16) to fetch
1872 * acct_flags, but as NT4 DCs reject authentication with workstation
1873 * accounts with NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT, even if
1874 * MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT is specified, we only ever got
1875 * ACB_NORMAL back (maybe with ACB_PWNOEXP in addition).
1877 * For network logons NT4 DCs also skip the
1878 * account_name, so we have to fallback to the
1879 * one given by the client.
1882 if (base_info
->acct_flags
== 0) {
1883 base_info
->acct_flags
= ACB_NORMAL
;
1884 if (base_info
->force_password_change
== NTTIME_MAX
) {
1885 base_info
->acct_flags
|= ACB_PWNOEXP
;
1887 acct_flags_src
= "calculated";
1890 if (base_info
->account_name
.string
== NULL
) {
1891 base_info
->account_name
.string
= talloc_strdup(base_ctx
,
1893 if (base_info
->account_name
.string
== NULL
) {
1894 TALLOC_FREE(validation
);
1895 return NT_STATUS_NO_MEMORY
;
1897 acct_name_src
= "client";
1900 sid_compose(&user_sid
, base_info
->domain_sid
, base_info
->rid
);
1902 DBG_DEBUG("Fallback to %s_acct_flags[0x%x] %s_acct_name[%s] for %s\n",
1904 base_info
->acct_flags
,
1906 base_info
->account_name
.string
,
1907 dom_sid_str_buf(&user_sid
, &sid_buf
));
1910 *_validation_level
= validation_level
;
1911 *_validation
= validation
;
1912 return NT_STATUS_OK
;
1915 static NTSTATUS
nt_dual_auth_passdb(TALLOC_CTX
*mem_ctx
,
1917 fstring name_domain
,
1920 const char *client_name
,
1921 const int client_pid
,
1922 const struct tsocket_address
*remote
,
1923 const struct tsocket_address
*local
,
1924 uint8_t *authoritative
,
1925 struct netr_SamInfo3
**info3
)
1927 unsigned char local_nt_response
[24];
1929 DATA_BLOB chal_blob
;
1933 /* do password magic */
1935 generate_random_buffer(chal
, sizeof(chal
));
1936 chal_blob
= data_blob_const(chal
, sizeof(chal
));
1938 if (lp_client_ntlmv2_auth()) {
1939 DATA_BLOB server_chal
;
1940 DATA_BLOB names_blob
;
1941 server_chal
= data_blob_const(chal
, 8);
1943 /* note that the 'workgroup' here is for the local
1944 machine. The 'server name' must match the
1945 'workstation' passed to the actual SamLogon call.
1947 names_blob
= NTLMv2_generate_names_blob(mem_ctx
,
1951 if (!SMBNTLMv2encrypt(mem_ctx
, name_user
, name_domain
,
1952 pass
, &server_chal
, &names_blob
,
1953 &lm_resp
, &nt_resp
, NULL
, NULL
)) {
1954 data_blob_free(&names_blob
);
1955 DEBUG(0, ("SMBNTLMv2encrypt() failed!\n"));
1956 return NT_STATUS_NO_MEMORY
;
1958 data_blob_free(&names_blob
);
1961 lm_resp
= data_blob_null
;
1963 rc
= SMBNTencrypt(pass
, chal
, local_nt_response
);
1965 DEBUG(0, ("SMBNTencrypt() failed!\n"));
1966 return gnutls_error_to_ntstatus(rc
,
1967 NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER
);
1970 nt_resp
= data_blob_talloc(mem_ctx
, local_nt_response
,
1971 sizeof(local_nt_response
));
1974 return winbindd_dual_auth_passdb(talloc_tos(), 0, name_domain
,
1975 name_user
, logon_id
, client_name
,
1976 client_pid
, &chal_blob
, &lm_resp
,
1977 &nt_resp
, remote
, local
,
1978 true, /* interactive */
1979 authoritative
, info3
);
1982 static NTSTATUS
winbindd_dual_pam_auth_samlogon(
1983 TALLOC_CTX
*mem_ctx
,
1984 struct winbindd_domain
*domain
,
1988 const char *client_name
,
1989 const int client_pid
,
1990 uint32_t request_flags
,
1991 const struct tsocket_address
*remote
,
1992 const struct tsocket_address
*local
,
1993 uint16_t *_validation_level
,
1994 union netr_Validation
**_validation
)
1996 fstring name_namespace
, name_domain
, name_user
;
1998 uint8_t authoritative
= 1;
2000 uint16_t validation_level
= 0;
2001 union netr_Validation
*validation
= NULL
;
2004 DEBUG(10,("winbindd_dual_pam_auth_samlogon\n"));
2006 /* Parse domain and username */
2008 ok
= parse_domain_user(user
, name_namespace
, name_domain
, name_user
);
2010 return NT_STATUS_INVALID_PARAMETER
;
2014 * We check against domain->name instead of
2015 * name_domain, as find_auth_domain() ->
2016 * find_domain_from_name_noinit() already decided
2017 * that we are in a child for the correct domain.
2019 * name_domain can also be lp_realm()
2020 * we need to check against domain->name.
2022 if (strequal(domain
->name
, get_global_sam_name())) {
2023 struct netr_SamInfo3
*info3
= NULL
;
2025 result
= nt_dual_auth_passdb(mem_ctx
, name_user
, name_domain
,
2026 pass
, logon_id
, client_name
,
2027 client_pid
, remote
, local
,
2028 &authoritative
, &info3
);
2031 * We need to try the remote NETLOGON server if this is
2032 * not authoritative (for example on the RODC).
2034 if (authoritative
!= 0) {
2035 if (!NT_STATUS_IS_OK(result
)) {
2038 result
= map_info3_to_validation(mem_ctx
,
2043 if (!NT_STATUS_IS_OK(result
)) {
2051 /* check authentication loop */
2053 result
= winbind_samlogon_retry_loop(domain
,
2061 true, /* plaintext_given */
2063 data_blob_null
, data_blob_null
,
2064 true, /* interactive */
2069 if (!NT_STATUS_IS_OK(result
)) {
2074 *_validation_level
= validation_level
;
2075 *_validation
= validation
;
2077 return NT_STATUS_OK
;
2081 * @brief build a tsocket_address for the remote address of the supplied socket
2084 static struct tsocket_address
*get_remote_address(TALLOC_CTX
*mem_ctx
, int sock
)
2086 struct sockaddr_storage st
= {0};
2087 struct sockaddr
*sar
= (struct sockaddr
*)&st
;
2088 socklen_t sa_len
= sizeof(st
);
2089 struct tsocket_address
*remote
= NULL
;
2092 ret
= getpeername(sock
, sar
, &sa_len
);
2094 DBG_ERR("getpeername failed - %s", strerror(errno
));
2097 ret
= tsocket_address_bsd_from_sockaddr(mem_ctx
, sar
, sa_len
, &remote
);
2099 DBG_ERR("tsocket_address_bsd_from_sockaddr failed - %s",
2107 * @brief build a tsocket_address for the local address of the supplied socket
2110 static struct tsocket_address
*get_local_address(TALLOC_CTX
*mem_ctx
, int sock
)
2112 struct sockaddr_storage st
= {0};
2113 struct sockaddr
*sar
= (struct sockaddr
*)&st
;
2114 socklen_t sa_len
= sizeof(st
);
2115 struct tsocket_address
*local
= NULL
;
2118 ret
= getsockname(sock
, sar
, &sa_len
);
2120 DBG_ERR("getsockname failed - %s", strerror(errno
));
2123 ret
= tsocket_address_bsd_from_sockaddr(mem_ctx
, sar
, sa_len
, &local
);
2125 DBG_ERR("tsocket_address_bsd_from_sockaddr failed - %s",
2133 * @brief generate an authentication message in the logs.
2136 static void log_authentication(
2137 TALLOC_CTX
*mem_ctx
,
2138 const struct winbindd_domain
*domain
,
2139 const struct winbindd_cli_state
*state
,
2140 const struct timeval start_time
,
2141 const uint64_t logon_id
,
2142 const char *command
,
2143 const char *user_name
,
2144 const char *domain_name
,
2145 const char *workstation
,
2146 const DATA_BLOB lm_resp
,
2147 const DATA_BLOB nt_resp
,
2148 const struct tsocket_address
*remote
,
2149 const struct tsocket_address
*local
,
2153 struct auth_usersupplied_info
*ui
= NULL
;
2154 struct dom_sid
*sid
= NULL
;
2155 struct loadparm_context
*lp_ctx
= NULL
;
2156 struct imessaging_context
*msg_ctx
= NULL
;
2158 ui
= talloc_zero(mem_ctx
, struct auth_usersupplied_info
);
2159 ui
->logon_id
= logon_id
;
2160 ui
->service_description
= "winbind";
2161 ui
->password
.response
.nt
.length
= nt_resp
.length
;
2162 ui
->password
.response
.nt
.data
= nt_resp
.data
;
2163 ui
->password
.response
.lanman
.length
= lm_resp
.length
;
2164 ui
->password
.response
.lanman
.data
= lm_resp
.data
;
2165 if (nt_resp
.length
== 0 && lm_resp
.length
== 0) {
2166 ui
->password_state
= AUTH_PASSWORD_PLAIN
;
2168 ui
->password_state
= AUTH_PASSWORD_RESPONSE
;
2171 * In the event of a failure ui->auth_description will be null,
2172 * the logging code handles this correctly so it can be ignored.
2174 ui
->auth_description
= talloc_asprintf(
2178 state
->request
->client_name
,
2180 if (ui
->auth_description
== NULL
) {
2181 DBG_ERR("OOM Unable to create auth_description");
2183 ui
->client
.account_name
= user_name
;
2184 ui
->client
.domain_name
= domain_name
;
2185 ui
->workstation_name
= workstation
;
2186 ui
->remote_host
= remote
;
2187 ui
->local_host
= local
;
2189 sid
= dom_sid_parse_talloc(
2190 ui
, state
->response
->data
.auth
.info3
.dom_sid
);
2192 sid_append_rid(sid
, state
->response
->data
.auth
.info3
.user_rid
);
2195 if (lp_auth_event_notification()) {
2196 lp_ctx
= loadparm_init_s3(ui
, loadparm_s3_helpers());
2197 msg_ctx
= imessaging_client_init(
2198 ui
, lp_ctx
, global_event_context());
2200 log_authentication_event(
2206 state
->response
->data
.auth
.info3
.logon_dom
,
2207 state
->response
->data
.auth
.info3
.user_name
,
2212 enum winbindd_result
winbindd_dual_pam_auth(struct winbindd_domain
*domain
,
2213 struct winbindd_cli_state
*state
)
2215 NTSTATUS result
= NT_STATUS_LOGON_FAILURE
;
2216 NTSTATUS krb5_result
= NT_STATUS_OK
;
2217 fstring name_namespace
, name_domain
, name_user
;
2219 fstring domain_user
;
2220 uint16_t validation_level
= UINT16_MAX
;
2221 union netr_Validation
*validation
= NULL
;
2222 NTSTATUS name_map_status
= NT_STATUS_UNSUCCESSFUL
;
2224 uint64_t logon_id
= 0;
2225 const struct timeval start_time
= timeval_current();
2226 const struct tsocket_address
*remote
= NULL
;
2227 const struct tsocket_address
*local
= NULL
;
2229 /* Ensure null termination */
2230 state
->request
->data
.auth
.user
[sizeof(state
->request
->data
.auth
.user
)-1]='\0';
2232 /* Ensure null termination */
2233 state
->request
->data
.auth
.pass
[sizeof(state
->request
->data
.auth
.pass
)-1]='\0';
2236 * Generate a logon_id for this session.
2238 logon_id
= generate_random_u64();
2239 remote
= get_remote_address(state
->mem_ctx
, state
->sock
);
2240 local
= get_local_address(state
->mem_ctx
, state
->sock
);
2241 DEBUG(3, ("[%5lu]: dual pam auth %s\n", (unsigned long)state
->pid
,
2242 state
->request
->data
.auth
.user
));
2244 /* Parse domain and username */
2246 name_map_status
= normalize_name_unmap(state
->mem_ctx
,
2247 state
->request
->data
.auth
.user
,
2250 /* If the name normalization didn't actually do anything,
2251 just use the original name */
2253 if (!NT_STATUS_IS_OK(name_map_status
) &&
2254 !NT_STATUS_EQUAL(name_map_status
, NT_STATUS_FILE_RENAMED
))
2256 mapped_user
= state
->request
->data
.auth
.user
;
2259 ok
= parse_domain_user(mapped_user
,
2264 result
= NT_STATUS_INVALID_PARAMETER
;
2265 goto process_result
;
2268 if ( mapped_user
!= state
->request
->data
.auth
.user
) {
2269 fstr_sprintf( domain_user
, "%s%c%s", name_domain
,
2270 *lp_winbind_separator(),
2272 strlcpy( state
->request
->data
.auth
.user
, domain_user
,
2273 sizeof(state
->request
->data
.auth
.user
));
2276 if (!domain
->online
) {
2277 result
= NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
;
2278 if (domain
->startup
) {
2279 /* Logons are very important to users. If we're offline and
2280 we get a request within the first 30 seconds of startup,
2281 try very hard to find a DC and go online. */
2283 DEBUG(10,("winbindd_dual_pam_auth: domain: %s offline and auth "
2284 "request in startup mode.\n", domain
->name
));
2286 winbindd_flush_negative_conn_cache(domain
);
2287 result
= init_dc_connection(domain
, false);
2291 DEBUG(10,("winbindd_dual_pam_auth: domain: %s last was %s\n", domain
->name
, domain
->online
? "online":"offline"));
2293 /* Check for Kerberos authentication */
2294 if (domain
->online
&& (state
->request
->flags
& WBFLAG_PAM_KRB5
)) {
2295 struct netr_SamInfo6
*info6
= NULL
;
2296 const char *krb5ccname
= NULL
;
2298 result
= winbindd_dual_pam_auth_kerberos(
2300 state
->request
->data
.auth
.user
,
2301 state
->request
->data
.auth
.pass
,
2302 state
->request
->data
.auth
.krb5_cc_type
,
2303 get_uid_from_request(state
->request
),
2308 /* save for later */
2309 krb5_result
= result
;
2311 if (NT_STATUS_IS_OK(result
)) {
2312 DEBUG(10,("winbindd_dual_pam_auth_kerberos succeeded\n"));
2314 fstrcpy(state
->response
->data
.auth
.krb5ccname
,
2317 result
= map_info6_to_validation(state
->mem_ctx
,
2322 if (!NT_STATUS_IS_OK(result
)) {
2323 DBG_ERR("map_info6_to_validation failed\n");
2326 goto process_result
;
2329 DBG_DEBUG("winbindd_dual_pam_auth_kerberos failed: %s\n",
2332 if (NT_STATUS_EQUAL(result
, NT_STATUS_NO_LOGON_SERVERS
) ||
2333 NT_STATUS_EQUAL(result
, NT_STATUS_IO_TIMEOUT
) ||
2334 NT_STATUS_EQUAL(result
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
)) {
2335 DEBUG(10,("winbindd_dual_pam_auth_kerberos setting domain to offline\n"));
2336 set_domain_offline( domain
);
2340 /* there are quite some NT_STATUS errors where there is no
2341 * point in retrying with a samlogon, we explictly have to take
2342 * care not to increase the bad logon counter on the DC */
2344 if (NT_STATUS_EQUAL(result
, NT_STATUS_ACCOUNT_DISABLED
) ||
2345 NT_STATUS_EQUAL(result
, NT_STATUS_ACCOUNT_EXPIRED
) ||
2346 NT_STATUS_EQUAL(result
, NT_STATUS_ACCOUNT_LOCKED_OUT
) ||
2347 NT_STATUS_EQUAL(result
, NT_STATUS_INVALID_LOGON_HOURS
) ||
2348 NT_STATUS_EQUAL(result
, NT_STATUS_INVALID_WORKSTATION
) ||
2349 NT_STATUS_EQUAL(result
, NT_STATUS_LOGON_FAILURE
) ||
2350 NT_STATUS_EQUAL(result
, NT_STATUS_NO_SUCH_USER
) ||
2351 NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_EXPIRED
) ||
2352 NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_MUST_CHANGE
) ||
2353 NT_STATUS_EQUAL(result
, NT_STATUS_WRONG_PASSWORD
)) {
2357 if (state
->request
->flags
& WBFLAG_PAM_FALLBACK_AFTER_KRB5
) {
2358 DEBUG(3,("falling back to samlogon\n"));
2366 /* Check for Samlogon authentication */
2367 if (domain
->online
) {
2368 struct netr_SamBaseInfo
*base_info
= NULL
;
2370 result
= winbindd_dual_pam_auth_samlogon(
2371 state
->mem_ctx
, domain
,
2372 state
->request
->data
.auth
.user
,
2373 state
->request
->data
.auth
.pass
,
2375 state
->request
->client_name
,
2377 state
->request
->flags
,
2383 if (NT_STATUS_IS_OK(result
)) {
2384 DEBUG(10,("winbindd_dual_pam_auth_samlogon succeeded\n"));
2386 switch (validation_level
) {
2388 base_info
= &validation
->sam3
->base
;
2391 base_info
= &validation
->sam6
->base
;
2394 DBG_ERR("Bad validation level %d\n",
2396 result
= NT_STATUS_INTERNAL_ERROR
;
2400 /* add the Krb5 err if we have one */
2401 if ( NT_STATUS_EQUAL(krb5_result
, NT_STATUS_TIME_DIFFERENCE_AT_DC
) ) {
2402 base_info
->user_flags
|= LOGON_KRB5_FAIL_CLOCK_SKEW
;
2405 goto process_result
;
2408 DEBUG(10,("winbindd_dual_pam_auth_samlogon failed: %s\n",
2409 nt_errstr(result
)));
2411 if (NT_STATUS_EQUAL(result
, NT_STATUS_NO_LOGON_SERVERS
) ||
2412 NT_STATUS_EQUAL(result
, NT_STATUS_IO_TIMEOUT
) ||
2413 NT_STATUS_EQUAL(result
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
))
2415 DEBUG(10,("winbindd_dual_pam_auth_samlogon setting domain to offline\n"));
2416 set_domain_offline( domain
);
2420 if (domain
->online
) {
2421 /* We're still online - fail. */
2427 /* Check for Cached logons */
2428 if (!domain
->online
&& (state
->request
->flags
& WBFLAG_PAM_CACHED_LOGIN
) &&
2429 lp_winbind_offline_logon()) {
2430 struct netr_SamInfo3
*info3
= NULL
;
2432 result
= winbindd_dual_pam_auth_cached(domain
, state
, &info3
);
2434 if (!NT_STATUS_IS_OK(result
)) {
2435 DEBUG(10,("winbindd_dual_pam_auth_cached failed: %s\n", nt_errstr(result
)));
2438 DEBUG(10,("winbindd_dual_pam_auth_cached succeeded\n"));
2440 result
= map_info3_to_validation(state
->mem_ctx
,
2445 if (!NT_STATUS_IS_OK(result
)) {
2446 DBG_ERR("map_info3_to_validation failed\n");
2453 if (NT_STATUS_IS_OK(result
)) {
2454 struct dom_sid user_sid
;
2455 TALLOC_CTX
*base_ctx
= NULL
;
2456 struct netr_SamBaseInfo
*base_info
= NULL
;
2457 struct netr_SamInfo3
*info3
= NULL
;
2458 struct wbint_SidArray
*sid_array
= NULL
;
2460 switch (validation_level
) {
2462 base_ctx
= validation
->sam3
;
2463 base_info
= &validation
->sam3
->base
;
2466 base_ctx
= validation
->sam6
;
2467 base_info
= &validation
->sam6
->base
;
2470 result
= NT_STATUS_INTERNAL_ERROR
;
2474 sid_compose(&user_sid
, base_info
->domain_sid
, base_info
->rid
);
2476 if (base_info
->full_name
.string
== NULL
) {
2477 struct netr_SamInfo3
*cached_info3
;
2479 cached_info3
= netsamlogon_cache_get(state
->mem_ctx
,
2481 if (cached_info3
!= NULL
&&
2482 cached_info3
->base
.full_name
.string
!= NULL
) {
2483 base_info
->full_name
.string
= talloc_strdup(
2485 cached_info3
->base
.full_name
.string
);
2486 if (base_info
->full_name
.string
== NULL
) {
2487 result
= NT_STATUS_NO_MEMORY
;
2492 /* this might fail so we don't check the return code */
2493 wcache_query_user_fullname(domain
,
2496 &base_info
->full_name
.string
);
2500 result
= map_validation_to_info3(talloc_tos(),
2504 if (!NT_STATUS_IS_OK(result
)) {
2508 wcache_invalidate_samlogon(find_domain_from_name(name_domain
),
2510 netsamlogon_cache_store(name_user
, info3
);
2512 /* save name_to_sid info as early as possible (only if
2513 this is our primary domain so we don't invalidate
2514 the cache entry by storing the seq_num for the wrong
2516 if ( domain
->primary
) {
2517 cache_name2sid(domain
, name_domain
, name_user
,
2518 SID_NAME_USER
, &user_sid
);
2521 /* Check if the user is in the right group */
2523 result
= extra_data_to_sid_array(
2524 state
->request
->data
.auth
.require_membership_of_sid
,
2527 if (!NT_STATUS_IS_OK(result
)) {
2528 DBG_ERR("Failed to parse '%s' into a sid array: %s\n",
2529 state
->request
->data
.auth
.require_membership_of_sid
,
2534 result
= check_info3_in_group(info3
, sid_array
);
2535 if (!NT_STATUS_IS_OK(result
)) {
2536 char *s
= NDR_PRINT_STRUCT_STRING(state
->mem_ctx
,
2539 DBG_NOTICE("User %s is not in the required groups:\n",
2540 state
->request
->data
.auth
.user
);
2541 DEBUGADD(DBGLVL_NOTICE
, ("%s", s
));
2542 DEBUGADD(DBGLVL_NOTICE
,
2543 ("Plaintext authentication is rejected\n"));
2544 TALLOC_FREE(sid_array
);
2547 TALLOC_FREE(sid_array
);
2549 if (!is_allowed_domain(info3
->base
.logon_domain
.string
)) {
2550 DBG_NOTICE("Authentication failed for user [%s] "
2551 "from firewalled domain [%s]\n",
2552 info3
->base
.account_name
.string
,
2553 info3
->base
.logon_domain
.string
);
2554 result
= NT_STATUS_AUTHENTICATION_FIREWALL_FAILED
;
2558 result
= append_auth_data(state
->mem_ctx
, state
->response
,
2559 state
->request
->flags
,
2562 name_domain
, name_user
);
2563 if (!NT_STATUS_IS_OK(result
)) {
2567 if ((state
->request
->flags
& WBFLAG_PAM_CACHED_LOGIN
)
2568 && lp_winbind_offline_logon()) {
2570 result
= winbindd_store_creds(domain
,
2571 state
->request
->data
.auth
.user
,
2572 state
->request
->data
.auth
.pass
,
2576 if (state
->request
->flags
& WBFLAG_PAM_GET_PWD_POLICY
) {
2578 * WBFLAG_PAM_GET_PWD_POLICY is not used within
2579 * any Samba caller anymore.
2581 * We just fake this based on the effective values
2582 * for the user, for legacy callers.
2584 fake_password_policy(state
->response
, &info3
->base
);
2587 result
= NT_STATUS_OK
;
2591 /* give us a more useful (more correct?) error code */
2592 if ((NT_STATUS_EQUAL(result
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
) ||
2593 (NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
)))) {
2594 result
= NT_STATUS_NO_LOGON_SERVERS
;
2598 * Here we don't alter
2599 * state->response->data.auth.authoritative based
2600 * on the servers response
2601 * as we don't want a fallback to the local sam
2602 * for interactive PAM logons
2604 set_auth_errors(state
->response
, result
);
2606 DEBUG(NT_STATUS_IS_OK(result
) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n",
2607 state
->request
->data
.auth
.user
,
2608 state
->response
->data
.auth
.nt_status_string
,
2609 state
->response
->data
.auth
.pam_error
));
2612 * Log the winbind pam authentication, the logon_id will tie this to
2613 * any of the logons invoked from this request.
2631 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
2634 NTSTATUS
winbind_dual_SamLogon(struct winbindd_domain
*domain
,
2635 TALLOC_CTX
*mem_ctx
,
2637 uint32_t logon_parameters
,
2638 const char *name_user
,
2639 const char *name_domain
,
2640 const char *workstation
,
2641 const uint64_t logon_id
,
2642 const char* client_name
,
2643 const int client_pid
,
2644 const uint8_t chal
[8],
2645 DATA_BLOB lm_response
,
2646 DATA_BLOB nt_response
,
2647 const struct tsocket_address
*remote
,
2648 const struct tsocket_address
*local
,
2649 uint8_t *authoritative
,
2652 uint16_t *_validation_level
,
2653 union netr_Validation
**_validation
)
2655 uint16_t validation_level
= 0;
2656 union netr_Validation
*validation
= NULL
;
2660 * We check against domain->name instead of
2661 * name_domain, as find_auth_domain() ->
2662 * find_domain_from_name_noinit() already decided
2663 * that we are in a child for the correct domain.
2665 * name_domain can also be lp_realm()
2666 * we need to check against domain->name.
2668 if (!skip_sam
&& strequal(domain
->name
, get_global_sam_name())) {
2669 DATA_BLOB chal_blob
= data_blob_const(
2671 struct netr_SamInfo3
*info3
= NULL
;
2673 result
= winbindd_dual_auth_passdb(
2676 name_domain
, name_user
,
2680 &chal_blob
, &lm_response
, &nt_response
,
2686 if (NT_STATUS_IS_OK(result
)) {
2687 result
= map_info3_to_validation(mem_ctx
,
2692 if (!NT_STATUS_IS_OK(result
)) {
2698 * We need to try the remote NETLOGON server if this is
2699 * not authoritative.
2701 if (*authoritative
!= 0) {
2703 goto process_result
;
2707 result
= winbind_samlogon_retry_loop(domain
,
2711 NULL
, /* password */
2713 /* Bug #3248 - found by Stefan Burkei. */
2714 workstation
, /* We carefully set this above so use it... */
2716 false, /* plaintext_given */
2725 if (!NT_STATUS_IS_OK(result
)) {
2731 if (NT_STATUS_IS_OK(result
)) {
2732 struct dom_sid user_sid
;
2733 TALLOC_CTX
*base_ctx
= NULL
;
2734 struct netr_SamBaseInfo
*base_info
= NULL
;
2735 struct netr_SamInfo3
*info3
= NULL
;
2737 switch (validation_level
) {
2739 base_ctx
= validation
->sam3
;
2740 base_info
= &validation
->sam3
->base
;
2743 base_ctx
= validation
->sam6
;
2744 base_info
= &validation
->sam6
->base
;
2747 result
= NT_STATUS_INTERNAL_ERROR
;
2751 sid_compose(&user_sid
, base_info
->domain_sid
, base_info
->rid
);
2753 if (base_info
->full_name
.string
== NULL
) {
2754 struct netr_SamInfo3
*cached_info3
;
2756 cached_info3
= netsamlogon_cache_get(mem_ctx
,
2758 if (cached_info3
!= NULL
&&
2759 cached_info3
->base
.full_name
.string
!= NULL
)
2761 base_info
->full_name
.string
= talloc_strdup(
2763 cached_info3
->base
.full_name
.string
);
2766 /* this might fail so we don't check the return code */
2767 wcache_query_user_fullname(domain
,
2770 &base_info
->full_name
.string
);
2774 result
= map_validation_to_info3(talloc_tos(),
2778 if (!NT_STATUS_IS_OK(result
)) {
2781 wcache_invalidate_samlogon(find_domain_from_name(name_domain
),
2783 netsamlogon_cache_store(name_user
, info3
);
2789 /* give us a more useful (more correct?) error code */
2790 if ((NT_STATUS_EQUAL(result
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
) ||
2791 (NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
)))) {
2792 result
= NT_STATUS_NO_LOGON_SERVERS
;
2795 DEBUG(NT_STATUS_IS_OK(result
) ? 5 : 2,
2796 ("NTLM CRAP authentication for user [%s]\\[%s] returned %s\n",
2799 nt_errstr(result
)));
2801 if (!NT_STATUS_IS_OK(result
)) {
2805 *_validation_level
= validation_level
;
2806 *_validation
= validation
;
2807 return NT_STATUS_OK
;
2810 enum winbindd_result
winbindd_dual_pam_auth_crap(struct winbindd_domain
*domain
,
2811 struct winbindd_cli_state
*state
)
2814 const char *name_user
= NULL
;
2815 const char *name_domain
= NULL
;
2816 const char *workstation
;
2817 uint64_t logon_id
= 0;
2818 uint8_t authoritative
= 1;
2820 uint16_t validation_level
;
2821 union netr_Validation
*validation
= NULL
;
2822 DATA_BLOB lm_resp
= { 0 }, nt_resp
= { 0 };
2823 const struct timeval start_time
= timeval_current();
2824 const struct tsocket_address
*remote
= NULL
;
2825 const struct tsocket_address
*local
= NULL
;
2827 /* This is child-only, so no check for privileged access is needed
2830 /* Ensure null termination */
2831 state
->request
->data
.auth_crap
.user
[sizeof(state
->request
->data
.auth_crap
.user
)-1]=0;
2832 state
->request
->data
.auth_crap
.domain
[sizeof(state
->request
->data
.auth_crap
.domain
)-1]=0;
2834 name_user
= state
->request
->data
.auth_crap
.user
;
2835 name_domain
= state
->request
->data
.auth_crap
.domain
;
2836 workstation
= state
->request
->data
.auth_crap
.workstation
;
2837 logon_id
= generate_random_u64();
2838 remote
= get_remote_address(state
->mem_ctx
, state
->sock
);
2839 local
= get_local_address(state
->mem_ctx
, state
->sock
);
2841 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state
->pid
,
2842 name_domain
, name_user
));
2844 if (state
->request
->data
.auth_crap
.lm_resp_len
> sizeof(state
->request
->data
.auth_crap
.lm_resp
)
2845 || state
->request
->data
.auth_crap
.nt_resp_len
> sizeof(state
->request
->data
.auth_crap
.nt_resp
)) {
2846 if (!(state
->request
->flags
& WBFLAG_BIG_NTLMV2_BLOB
) ||
2847 state
->request
->extra_len
!= state
->request
->data
.auth_crap
.nt_resp_len
) {
2848 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n",
2849 state
->request
->data
.auth_crap
.lm_resp_len
,
2850 state
->request
->data
.auth_crap
.nt_resp_len
));
2851 result
= NT_STATUS_INVALID_PARAMETER
;
2856 lm_resp
= data_blob_talloc(state
->mem_ctx
, state
->request
->data
.auth_crap
.lm_resp
,
2857 state
->request
->data
.auth_crap
.lm_resp_len
);
2859 if (state
->request
->flags
& WBFLAG_BIG_NTLMV2_BLOB
) {
2860 nt_resp
= data_blob_talloc(state
->mem_ctx
,
2861 state
->request
->extra_data
.data
,
2862 state
->request
->data
.auth_crap
.nt_resp_len
);
2864 nt_resp
= data_blob_talloc(state
->mem_ctx
,
2865 state
->request
->data
.auth_crap
.nt_resp
,
2866 state
->request
->data
.auth_crap
.nt_resp_len
);
2869 result
= winbind_dual_SamLogon(domain
,
2871 false, /* interactive */
2872 state
->request
->data
.auth_crap
.logon_parameters
,
2875 /* Bug #3248 - found by Stefan Burkei. */
2876 workstation
, /* We carefully set this above so use it... */
2878 state
->request
->client_name
,
2879 state
->request
->pid
,
2880 state
->request
->data
.auth_crap
.chal
,
2890 if (!NT_STATUS_IS_OK(result
)) {
2894 if (NT_STATUS_IS_OK(result
)) {
2895 struct netr_SamInfo3
*info3
= NULL
;
2896 struct wbint_SidArray
*sid_array
= NULL
;
2898 result
= map_validation_to_info3(state
->mem_ctx
,
2902 if (!NT_STATUS_IS_OK(result
)) {
2906 result
= extra_data_to_sid_array(
2907 state
->request
->data
.auth_crap
.require_membership_of_sid
,
2910 if (!NT_STATUS_IS_OK(result
)) {
2911 DBG_ERR("Failed to parse '%s' into a sid array: %s\n",
2912 state
->request
->data
.auth_crap
.require_membership_of_sid
,
2917 /* Check if the user is in the right group */
2918 result
= check_info3_in_group(info3
, sid_array
);
2919 if (!NT_STATUS_IS_OK(result
)) {
2920 char *s
= NDR_PRINT_STRUCT_STRING(state
->mem_ctx
,
2923 DBG_NOTICE("User %s is not in the required groups:\n",
2924 state
->request
->data
.auth_crap
.user
);
2925 DEBUGADD(DBGLVL_NOTICE
, ("%s", s
));
2926 DEBUGADD(DBGLVL_NOTICE
,
2927 ("CRAP authentication is rejected\n"));
2928 TALLOC_FREE(sid_array
);
2931 TALLOC_FREE(sid_array
);
2933 if (!is_allowed_domain(info3
->base
.logon_domain
.string
)) {
2934 DBG_NOTICE("Authentication failed for user [%s] "
2935 "from firewalled domain [%s]\n",
2936 info3
->base
.account_name
.string
,
2937 info3
->base
.logon_domain
.string
);
2938 result
= NT_STATUS_AUTHENTICATION_FIREWALL_FAILED
;
2942 result
= append_auth_data(state
->mem_ctx
, state
->response
,
2943 state
->request
->flags
,
2946 name_domain
, name_user
);
2947 if (!NT_STATUS_IS_OK(result
)) {
2954 if (state
->request
->flags
& WBFLAG_PAM_NT_STATUS_SQUASH
) {
2955 result
= nt_status_squash(result
);
2958 set_auth_errors(state
->response
, result
);
2959 state
->response
->data
.auth
.authoritative
= authoritative
;
2962 * Log the winbind pam authentication, the logon_id will tie this to
2963 * any of the logons invoked from this request.
2981 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
2984 enum winbindd_result
winbindd_dual_pam_chauthtok(struct winbindd_domain
*contact_domain
,
2985 struct winbindd_cli_state
*state
)
2988 char *newpass
= NULL
;
2989 struct policy_handle dom_pol
;
2990 struct rpc_pipe_client
*cli
= NULL
;
2991 bool got_info
= false;
2992 struct samr_DomInfo1
*info
= NULL
;
2993 struct userPwdChangeFailureInformation
*reject
= NULL
;
2994 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2995 fstring
namespace, domain
, user
;
2996 struct dcerpc_binding_handle
*b
= NULL
;
2999 ZERO_STRUCT(dom_pol
);
3001 DEBUG(3, ("[%5lu]: dual pam chauthtok %s\n", (unsigned long)state
->pid
,
3002 state
->request
->data
.auth
.user
));
3004 ok
= parse_domain_user(state
->request
->data
.chauthtok
.user
,
3012 if (!is_allowed_domain(domain
)) {
3013 DBG_NOTICE("Authentication failed for user [%s] "
3014 "from firewalled domain [%s]\n",
3016 result
= NT_STATUS_AUTHENTICATION_FIREWALL_FAILED
;
3020 /* Change password */
3022 oldpass
= state
->request
->data
.chauthtok
.oldpass
;
3023 newpass
= state
->request
->data
.chauthtok
.newpass
;
3025 /* Initialize reject reason */
3026 state
->response
->data
.auth
.reject_reason
= Undefined
;
3028 /* Get sam handle */
3030 result
= cm_connect_sam(contact_domain
, state
->mem_ctx
, true, &cli
,
3032 if (!NT_STATUS_IS_OK(result
)) {
3033 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain
));
3037 b
= cli
->binding_handle
;
3039 result
= rpccli_samr_chgpasswd_user3(cli
, state
->mem_ctx
,
3046 /* Windows 2003 returns NT_STATUS_PASSWORD_RESTRICTION */
3048 if (NT_STATUS_EQUAL(result
, NT_STATUS_PASSWORD_RESTRICTION
) ) {
3050 fill_in_password_policy(state
->response
, info
);
3052 state
->response
->data
.auth
.reject_reason
=
3053 reject
->extendedFailureReason
;
3058 /* atm the pidl generated rpccli_samr_ChangePasswordUser3 function will
3059 * return with NT_STATUS_BUFFER_TOO_SMALL for w2k dcs as w2k just
3060 * returns with 4byte error code (NT_STATUS_NOT_SUPPORTED) which is too
3061 * short to comply with the samr_ChangePasswordUser3 idl - gd */
3063 /* only fallback when the chgpasswd_user3 call is not supported */
3064 if (NT_STATUS_EQUAL(result
, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
) ||
3065 NT_STATUS_EQUAL(result
, NT_STATUS_NOT_SUPPORTED
) ||
3066 NT_STATUS_EQUAL(result
, NT_STATUS_BUFFER_TOO_SMALL
) ||
3067 NT_STATUS_EQUAL(result
, NT_STATUS_NOT_IMPLEMENTED
)) {
3069 DEBUG(10,("Password change with chgpasswd_user3 failed with: %s, retrying chgpasswd_user2\n",
3070 nt_errstr(result
)));
3072 result
= rpccli_samr_chgpasswd_user2(cli
, state
->mem_ctx
, user
, newpass
, oldpass
);
3074 /* Windows 2000 returns NT_STATUS_ACCOUNT_RESTRICTION.
3075 Map to the same status code as Windows 2003. */
3077 if ( NT_STATUS_EQUAL(NT_STATUS_ACCOUNT_RESTRICTION
, result
) ) {
3078 result
= NT_STATUS_PASSWORD_RESTRICTION
;
3084 if (NT_STATUS_IS_OK(result
)
3085 && (state
->request
->flags
& WBFLAG_PAM_CACHED_LOGIN
)
3086 && lp_winbind_offline_logon()) {
3087 result
= winbindd_update_creds_by_name(contact_domain
, user
,
3089 /* Again, this happens when we login from gdm or xdm
3090 * and the password expires, *BUT* cached crendentials
3091 * doesn't exist. winbindd_update_creds_by_name()
3092 * returns NT_STATUS_NO_SUCH_USER.
3093 * This is not a failure.
3096 if (NT_STATUS_EQUAL(result
, NT_STATUS_NO_SUCH_USER
)) {
3097 result
= NT_STATUS_OK
;
3100 if (!NT_STATUS_IS_OK(result
)) {
3101 DEBUG(10, ("Failed to store creds: %s\n",
3102 nt_errstr(result
)));
3103 goto process_result
;
3107 if (!NT_STATUS_IS_OK(result
) && !got_info
&& contact_domain
) {
3109 NTSTATUS policy_ret
;
3111 policy_ret
= fillup_password_policy(
3112 contact_domain
, state
->response
);
3114 /* failure of this is non critical, it will just provide no
3115 * additional information to the client why the change has
3116 * failed - Guenther */
3118 if (!NT_STATUS_IS_OK(policy_ret
)) {
3119 DEBUG(10,("Failed to get password policies: %s\n", nt_errstr(policy_ret
)));
3120 goto process_result
;
3126 if (strequal(contact_domain
->name
, get_global_sam_name())) {
3127 /* FIXME: internal rpc pipe does not cache handles yet */
3129 if (is_valid_policy_hnd(&dom_pol
)) {
3131 dcerpc_samr_Close(b
, state
->mem_ctx
, &dom_pol
, &_result
);
3137 set_auth_errors(state
->response
, result
);
3139 DEBUG(NT_STATUS_IS_OK(result
) ? 5 : 2,
3140 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
3143 state
->response
->data
.auth
.nt_status_string
,
3144 state
->response
->data
.auth
.pam_error
));
3146 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
3149 enum winbindd_result
winbindd_dual_pam_logoff(struct winbindd_domain
*domain
,
3150 struct winbindd_cli_state
*state
)
3152 NTSTATUS result
= NT_STATUS_NOT_SUPPORTED
;
3154 DEBUG(3, ("[%5lu]: pam dual logoff %s\n", (unsigned long)state
->pid
,
3155 state
->request
->data
.logoff
.user
));
3157 if (!(state
->request
->flags
& WBFLAG_PAM_KRB5
)) {
3158 result
= NT_STATUS_OK
;
3159 goto process_result
;
3162 if (state
->request
->data
.logoff
.krb5ccname
[0] == '\0') {
3163 result
= NT_STATUS_OK
;
3164 goto process_result
;
3169 if (state
->request
->data
.logoff
.uid
== (uid_t
)-1) {
3170 DEBUG(0,("winbindd_pam_logoff: invalid uid\n"));
3171 goto process_result
;
3174 /* what we need here is to find the corresponding krb5 ccache name *we*
3175 * created for a given username and destroy it */
3177 if (!ccache_entry_exists(state
->request
->data
.logoff
.user
)) {
3178 result
= NT_STATUS_OK
;
3179 DEBUG(10,("winbindd_pam_logoff: no entry found.\n"));
3180 goto process_result
;
3183 if (!ccache_entry_identical(state
->request
->data
.logoff
.user
,
3184 state
->request
->data
.logoff
.uid
,
3185 state
->request
->data
.logoff
.krb5ccname
)) {
3186 DEBUG(0,("winbindd_pam_logoff: cached entry differs.\n"));
3187 goto process_result
;
3190 result
= remove_ccache(state
->request
->data
.logoff
.user
);
3191 if (!NT_STATUS_IS_OK(result
)) {
3192 DEBUG(0,("winbindd_pam_logoff: failed to remove ccache: %s\n",
3193 nt_errstr(result
)));
3194 goto process_result
;
3198 * Remove any mlock'ed memory creds in the child
3199 * we might be using for krb5 ticket renewal.
3202 winbindd_delete_memory_creds(state
->request
->data
.logoff
.user
);
3205 result
= NT_STATUS_NOT_SUPPORTED
;
3211 set_auth_errors(state
->response
, result
);
3213 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
3216 /* Change user password with auth crap*/
3218 enum winbindd_result
winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domain
*domainSt
, struct winbindd_cli_state
*state
)
3221 DATA_BLOB new_nt_password
;
3222 DATA_BLOB old_nt_hash_enc
;
3223 DATA_BLOB new_lm_password
;
3224 DATA_BLOB old_lm_hash_enc
;
3225 fstring
namespace, domain
, user
;
3226 struct policy_handle dom_pol
;
3227 struct winbindd_domain
*contact_domain
= domainSt
;
3228 struct rpc_pipe_client
*cli
= NULL
;
3229 struct dcerpc_binding_handle
*b
= NULL
;
3231 ZERO_STRUCT(dom_pol
);
3233 /* Ensure null termination */
3234 state
->request
->data
.chng_pswd_auth_crap
.user
[
3235 sizeof(state
->request
->data
.chng_pswd_auth_crap
.user
)-1]=0;
3236 state
->request
->data
.chng_pswd_auth_crap
.domain
[
3237 sizeof(state
->request
->data
.chng_pswd_auth_crap
.domain
)-1]=0;
3239 namespace[0] = '\0';
3242 DEBUG(3, ("[%5lu]: pam change pswd auth crap domain: %s user: %s\n",
3243 (unsigned long)state
->pid
,
3244 state
->request
->data
.chng_pswd_auth_crap
.domain
,
3245 state
->request
->data
.chng_pswd_auth_crap
.user
));
3247 if (lp_winbind_offline_logon()) {
3248 DEBUG(0,("Refusing password change as winbind offline logons are enabled. "));
3249 DEBUGADD(0,("Changing passwords here would risk inconsistent logons\n"));
3250 result
= NT_STATUS_ACCESS_DENIED
;
3254 if (*state
->request
->data
.chng_pswd_auth_crap
.domain
) {
3255 fstrcpy(domain
,state
->request
->data
.chng_pswd_auth_crap
.domain
);
3259 ok
= parse_domain_user(state
->request
->data
.chng_pswd_auth_crap
.user
,
3264 result
= NT_STATUS_INVALID_PARAMETER
;
3269 DEBUG(3,("no domain specified with username (%s) - "
3271 state
->request
->data
.chng_pswd_auth_crap
.user
));
3272 result
= NT_STATUS_NO_SUCH_USER
;
3277 if (!*domain
&& lp_winbind_use_default_domain()) {
3278 fstrcpy(domain
,lp_workgroup());
3281 if (!is_allowed_domain(domain
)) {
3282 DBG_NOTICE("Authentication failed for user [%s] "
3283 "from firewalled domain [%s]\n",
3284 state
->request
->data
.chng_pswd_auth_crap
.user
,
3286 result
= NT_STATUS_AUTHENTICATION_FIREWALL_FAILED
;
3291 fstrcpy(user
, state
->request
->data
.chng_pswd_auth_crap
.user
);
3294 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n",
3295 (unsigned long)state
->pid
, domain
, user
));
3297 /* Change password */
3298 new_nt_password
= data_blob_const(
3299 state
->request
->data
.chng_pswd_auth_crap
.new_nt_pswd
,
3300 state
->request
->data
.chng_pswd_auth_crap
.new_nt_pswd_len
);
3302 old_nt_hash_enc
= data_blob_const(
3303 state
->request
->data
.chng_pswd_auth_crap
.old_nt_hash_enc
,
3304 state
->request
->data
.chng_pswd_auth_crap
.old_nt_hash_enc_len
);
3306 if(state
->request
->data
.chng_pswd_auth_crap
.new_lm_pswd_len
> 0) {
3307 new_lm_password
= data_blob_const(
3308 state
->request
->data
.chng_pswd_auth_crap
.new_lm_pswd
,
3309 state
->request
->data
.chng_pswd_auth_crap
.new_lm_pswd_len
);
3311 old_lm_hash_enc
= data_blob_const(
3312 state
->request
->data
.chng_pswd_auth_crap
.old_lm_hash_enc
,
3313 state
->request
->data
.chng_pswd_auth_crap
.old_lm_hash_enc_len
);
3315 new_lm_password
= data_blob_null
;
3316 old_lm_hash_enc
= data_blob_null
;
3319 /* Get sam handle */
3321 result
= cm_connect_sam(contact_domain
, state
->mem_ctx
, true, &cli
, &dom_pol
);
3322 if (!NT_STATUS_IS_OK(result
)) {
3323 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain
));
3327 b
= cli
->binding_handle
;
3329 result
= rpccli_samr_chng_pswd_auth_crap(
3330 cli
, state
->mem_ctx
, user
, new_nt_password
, old_nt_hash_enc
,
3331 new_lm_password
, old_lm_hash_enc
);
3335 if (strequal(contact_domain
->name
, get_global_sam_name())) {
3336 /* FIXME: internal rpc pipe does not cache handles yet */
3338 if (is_valid_policy_hnd(&dom_pol
)) {
3340 dcerpc_samr_Close(b
, state
->mem_ctx
, &dom_pol
, &_result
);
3346 set_auth_errors(state
->response
, result
);
3348 DEBUG(NT_STATUS_IS_OK(result
) ? 5 : 2,
3349 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
3351 state
->response
->data
.auth
.nt_status_string
,
3352 state
->response
->data
.auth
.pam_error
));
3354 return NT_STATUS_IS_OK(result
) ? WINBINDD_OK
: WINBINDD_ERROR
;
3358 static NTSTATUS
extract_pac_vrfy_sigs(TALLOC_CTX
*mem_ctx
, DATA_BLOB pac_blob
,
3359 struct PAC_DATA
**p_pac_data
)
3361 krb5_context krbctx
= NULL
;
3362 krb5_error_code k5ret
;
3364 krb5_kt_cursor cursor
;
3365 krb5_keytab_entry entry
;
3366 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
3369 ZERO_STRUCT(cursor
);
3371 k5ret
= smb_krb5_init_context_common(&krbctx
);
3373 DBG_ERR("kerberos init context failed (%s)\n",
3374 error_message(k5ret
));
3375 status
= krb5_to_nt_status(k5ret
);
3379 k5ret
= gse_krb5_get_server_keytab(krbctx
, &keytab
);
3381 DEBUG(1, ("Failed to get keytab: %s\n",
3382 error_message(k5ret
)));
3383 status
= krb5_to_nt_status(k5ret
);
3387 k5ret
= krb5_kt_start_seq_get(krbctx
, keytab
, &cursor
);
3389 DEBUG(1, ("Failed to start seq: %s\n",
3390 error_message(k5ret
)));
3391 status
= krb5_to_nt_status(k5ret
);
3395 k5ret
= krb5_kt_next_entry(krbctx
, keytab
, &entry
, &cursor
);
3396 while (k5ret
== 0) {
3397 status
= kerberos_decode_pac(mem_ctx
,
3400 NULL
, /* krbtgt_keyblock */
3401 KRB5_KT_KEY(&entry
), /* service_keyblock */
3402 NULL
, /* client_principal */
3403 0, /* tgs_authtime */
3405 if (NT_STATUS_IS_OK(status
)) {
3408 k5ret
= smb_krb5_kt_free_entry(krbctx
, &entry
);
3409 k5ret
= krb5_kt_next_entry(krbctx
, keytab
, &entry
, &cursor
);
3412 k5ret
= krb5_kt_end_seq_get(krbctx
, keytab
, &cursor
);
3414 DEBUG(1, ("Failed to end seq: %s\n",
3415 error_message(k5ret
)));
3418 k5ret
= krb5_kt_close(krbctx
, keytab
);
3420 DEBUG(1, ("Failed to close keytab: %s\n",
3421 error_message(k5ret
)));
3424 krb5_free_context(krbctx
);
3429 NTSTATUS
winbindd_pam_auth_pac_verify(struct winbindd_cli_state
*state
,
3431 uint16_t *p_validation_level
,
3432 union netr_Validation
**p_validation
)
3434 struct winbindd_request
*req
= state
->request
;
3436 struct PAC_DATA
*pac_data
= NULL
;
3437 struct PAC_LOGON_INFO
*logon_info
= NULL
;
3438 struct PAC_UPN_DNS_INFO
*upn_dns_info
= NULL
;
3439 struct netr_SamInfo6
*info6
= NULL
;
3440 uint16_t validation_level
= 0;
3441 union netr_Validation
*validation
= NULL
;
3442 struct netr_SamInfo3
*info3_copy
= NULL
;
3444 bool is_trusted
= false;
3447 *p_is_trusted
= false;
3448 *p_validation_level
= 0;
3449 *p_validation
= NULL
;
3451 pac_blob
= data_blob_const(req
->extra_data
.data
, req
->extra_len
);
3452 result
= extract_pac_vrfy_sigs(state
->mem_ctx
, pac_blob
, &pac_data
);
3453 if (NT_STATUS_IS_OK(result
)) {
3456 if (NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
)) {
3457 /* Try without signature verification */
3458 result
= kerberos_decode_pac(state
->mem_ctx
,
3460 NULL
, /* krb5_context */
3461 NULL
, /* krbtgt_keyblock */
3462 NULL
, /* service_keyblock */
3463 NULL
, /* client_principal */
3464 0, /* tgs_authtime */
3467 if (!NT_STATUS_IS_OK(result
)) {
3468 DEBUG(1, ("Error during PAC signature verification: %s\n",
3469 nt_errstr(result
)));
3473 for (i
=0; i
< pac_data
->num_buffers
; i
++) {
3474 if (pac_data
->buffers
[i
].type
== PAC_TYPE_LOGON_INFO
) {
3475 logon_info
= pac_data
->buffers
[i
].info
->logon_info
.info
;
3478 if (pac_data
->buffers
[i
].type
== PAC_TYPE_UPN_DNS_INFO
) {
3479 upn_dns_info
= &pac_data
->buffers
[i
].info
->upn_dns_info
;
3484 result
= create_info6_from_pac(state
->mem_ctx
,
3488 if (!NT_STATUS_IS_OK(result
)) {
3492 if (!is_allowed_domain(info6
->base
.logon_domain
.string
)) {
3493 DBG_NOTICE("Authentication failed for user [%s] "
3494 "from firewalled domain [%s]\n",
3495 info6
->base
.account_name
.string
,
3496 info6
->base
.logon_domain
.string
);
3497 return NT_STATUS_AUTHENTICATION_FIREWALL_FAILED
;
3500 result
= map_info6_to_validation(state
->mem_ctx
,
3504 if (!NT_STATUS_IS_OK(result
)) {
3508 result
= map_validation_to_info3(state
->mem_ctx
,
3512 if (!NT_STATUS_IS_OK(result
)) {
3518 * Signature verification succeeded, we can
3519 * trust the PAC and prime the netsamlogon
3520 * and name2sid caches. DO NOT DO THIS
3521 * in the signature verification failed
3524 struct winbindd_domain
*domain
= NULL
;
3526 netsamlogon_cache_store(NULL
, info3_copy
);
3529 * We're in the parent here, so find the child
3530 * pointer from the PAC domain name.
3532 domain
= find_lookup_domain_from_name(
3533 info3_copy
->base
.logon_domain
.string
);
3534 if (domain
&& domain
->primary
) {
3535 struct dom_sid user_sid
;
3536 struct dom_sid_buf buf
;
3538 sid_compose(&user_sid
,
3539 info3_copy
->base
.domain_sid
,
3540 info3_copy
->base
.rid
);
3542 cache_name2sid_trusted(domain
,
3543 info3_copy
->base
.logon_domain
.string
,
3544 info3_copy
->base
.account_name
.string
,
3548 DBG_INFO("PAC for user %s\\%s SID %s primed cache\n",
3549 info3_copy
->base
.logon_domain
.string
,
3550 info3_copy
->base
.account_name
.string
,
3551 dom_sid_str_buf(&user_sid
, &buf
));
3555 *p_is_trusted
= is_trusted
;
3556 *p_validation_level
= validation_level
;
3557 *p_validation
= validation
;
3558 return NT_STATUS_OK
;
3560 #else /* HAVE_KRB5 */
3561 NTSTATUS
winbindd_pam_auth_pac_verify(struct winbindd_cli_state
*state
,
3563 uint16_t *p_validation_level
,
3564 union netr_Validation
**p_validation
);
3567 *p_is_trusted
= false;
3568 *p_validation_level
= 0;
3569 *p_validation
= NULL
;
3570 return NT_STATUS_NO_SUCH_USER
;
3572 #endif /* HAVE_KRB5 */