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 "../libcli/auth/libcli_auth.h"
28 #include "../librpc/gen_ndr/ndr_samr_c.h"
29 #include "rpc_client/cli_pipe.h"
30 #include "rpc_client/cli_samr.h"
31 #include "../librpc/gen_ndr/ndr_netlogon.h"
32 #include "rpc_client/cli_netlogon.h"
34 #include "../lib/crypto/arcfour.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"
48 #define DBGC_CLASS DBGC_WINBIND
50 #define LOGON_KRB5_FAIL_CLOCK_SKEW 0x02000000
52 static NTSTATUS append_info3_as_txt(TALLOC_CTX *mem_ctx,
53 struct winbindd_response *resp,
54 struct netr_SamInfo3 *info3)
59 resp->data.auth.info3.logon_time =
60 nt_time_to_unix(info3->base.logon_time);
61 resp->data.auth.info3.logoff_time =
62 nt_time_to_unix(info3->base.logoff_time);
63 resp->data.auth.info3.kickoff_time =
64 nt_time_to_unix(info3->base.kickoff_time);
65 resp->data.auth.info3.pass_last_set_time =
66 nt_time_to_unix(info3->base.last_password_change);
67 resp->data.auth.info3.pass_can_change_time =
68 nt_time_to_unix(info3->base.allow_password_change);
69 resp->data.auth.info3.pass_must_change_time =
70 nt_time_to_unix(info3->base.force_password_change);
72 resp->data.auth.info3.logon_count = info3->base.logon_count;
73 resp->data.auth.info3.bad_pw_count = info3->base.bad_password_count;
75 resp->data.auth.info3.user_rid = info3->base.rid;
76 resp->data.auth.info3.group_rid = info3->base.primary_gid;
77 sid_to_fstring(resp->data.auth.info3.dom_sid, info3->base.domain_sid);
79 resp->data.auth.info3.num_groups = info3->base.groups.count;
80 resp->data.auth.info3.user_flgs = info3->base.user_flags;
82 resp->data.auth.info3.acct_flags = info3->base.acct_flags;
83 resp->data.auth.info3.num_other_sids = info3->sidcount;
85 fstrcpy(resp->data.auth.info3.user_name,
86 info3->base.account_name.string);
87 fstrcpy(resp->data.auth.info3.full_name,
88 info3->base.full_name.string);
89 fstrcpy(resp->data.auth.info3.logon_script,
90 info3->base.logon_script.string);
91 fstrcpy(resp->data.auth.info3.profile_path,
92 info3->base.profile_path.string);
93 fstrcpy(resp->data.auth.info3.home_dir,
94 info3->base.home_directory.string);
95 fstrcpy(resp->data.auth.info3.dir_drive,
96 info3->base.home_drive.string);
98 fstrcpy(resp->data.auth.info3.logon_srv,
99 info3->base.logon_server.string);
100 fstrcpy(resp->data.auth.info3.logon_dom,
101 info3->base.logon_domain.string);
103 ex = talloc_strdup(mem_ctx, "");
104 NT_STATUS_HAVE_NO_MEMORY(ex);
106 for (i=0; i < info3->base.groups.count; i++) {
107 ex = talloc_asprintf_append_buffer(ex, "0x%08X:0x%08X\n",
108 info3->base.groups.rids[i].rid,
109 info3->base.groups.rids[i].attributes);
110 NT_STATUS_HAVE_NO_MEMORY(ex);
113 for (i=0; i < info3->sidcount; i++) {
116 sid = dom_sid_string(mem_ctx, info3->sids[i].sid);
117 NT_STATUS_HAVE_NO_MEMORY(sid);
119 ex = talloc_asprintf_append_buffer(ex, "%s:0x%08X\n",
121 info3->sids[i].attributes);
122 NT_STATUS_HAVE_NO_MEMORY(ex);
127 resp->extra_data.data = ex;
128 resp->length += talloc_get_size(ex);
133 static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx,
134 struct winbindd_response *resp,
135 struct netr_SamInfo3 *info3)
138 enum ndr_err_code ndr_err;
140 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, info3,
141 (ndr_push_flags_fn_t)ndr_push_netr_SamInfo3);
142 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
143 DEBUG(0,("append_info3_as_ndr: failed to append\n"));
144 return ndr_map_error2ntstatus(ndr_err);
147 resp->extra_data.data = blob.data;
148 resp->length += blob.length;
153 static NTSTATUS append_unix_username(TALLOC_CTX *mem_ctx,
154 struct winbindd_response *resp,
155 const struct netr_SamInfo3 *info3,
156 const char *name_domain,
157 const char *name_user)
159 /* We've been asked to return the unix username, per
160 'winbind use default domain' settings and the like */
162 const char *nt_username, *nt_domain;
164 nt_domain = talloc_strdup(mem_ctx, info3->base.logon_domain.string);
166 /* If the server didn't give us one, just use the one
168 nt_domain = name_domain;
171 nt_username = talloc_strdup(mem_ctx, info3->base.account_name.string);
173 /* If the server didn't give us one, just use the one
175 nt_username = name_user;
178 fill_domain_username(resp->data.auth.unix_username,
179 nt_domain, nt_username, true);
181 DEBUG(5, ("Setting unix username to [%s]\n",
182 resp->data.auth.unix_username));
187 static NTSTATUS append_afs_token(TALLOC_CTX *mem_ctx,
188 struct winbindd_response *resp,
189 const struct netr_SamInfo3 *info3,
190 const char *name_domain,
191 const char *name_user)
193 char *afsname = NULL;
197 afsname = talloc_strdup(mem_ctx, lp_afs_username_map());
198 if (afsname == NULL) {
199 return NT_STATUS_NO_MEMORY;
202 afsname = talloc_string_sub(mem_ctx,
203 lp_afs_username_map(),
205 afsname = talloc_string_sub(mem_ctx, afsname,
207 afsname = talloc_string_sub(mem_ctx, afsname,
211 struct dom_sid user_sid;
214 sid_compose(&user_sid, info3->base.domain_sid,
216 sid_to_fstring(sidstr, &user_sid);
217 afsname = talloc_string_sub(mem_ctx, afsname,
221 if (afsname == NULL) {
222 return NT_STATUS_NO_MEMORY;
225 if (!strlower_m(afsname)) {
226 return NT_STATUS_INVALID_PARAMETER;
229 DEBUG(10, ("Generating token for user %s\n", afsname));
231 cell = strchr(afsname, '@');
234 return NT_STATUS_NO_MEMORY;
240 token = afs_createtoken_str(afsname, cell);
244 resp->extra_data.data = talloc_strdup(mem_ctx, token);
245 if (resp->extra_data.data == NULL) {
246 return NT_STATUS_NO_MEMORY;
248 resp->length += strlen((const char *)resp->extra_data.data)+1;
253 static NTSTATUS check_info3_in_group(struct netr_SamInfo3 *info3,
254 const char *group_sid)
256 * Check whether a user belongs to a group or list of groups.
258 * @param mem_ctx talloc memory context.
259 * @param info3 user information, including group membership info.
260 * @param group_sid One or more groups , separated by commas.
262 * @return NT_STATUS_OK on success,
263 * NT_STATUS_LOGON_FAILURE if the user does not belong,
264 * or other NT_STATUS_IS_ERR(status) for other kinds of failure.
267 struct dom_sid *require_membership_of_sid;
268 uint32_t num_require_membership_of_sid;
273 struct security_token *token;
274 TALLOC_CTX *frame = talloc_stackframe();
277 /* Parse the 'required group' SID */
279 if (!group_sid || !group_sid[0]) {
280 /* NO sid supplied, all users may access */
285 token = talloc_zero(talloc_tos(), struct security_token);
287 DEBUG(0, ("talloc failed\n"));
289 return NT_STATUS_NO_MEMORY;
292 num_require_membership_of_sid = 0;
293 require_membership_of_sid = NULL;
297 while (next_token_talloc(talloc_tos(), &p, &req_sid, ",")) {
298 if (!string_to_sid(&sid, req_sid)) {
299 DEBUG(0, ("check_info3_in_group: could not parse %s "
300 "as a SID!", req_sid));
302 return NT_STATUS_INVALID_PARAMETER;
305 status = add_sid_to_array(talloc_tos(), &sid,
306 &require_membership_of_sid,
307 &num_require_membership_of_sid);
308 if (!NT_STATUS_IS_OK(status)) {
309 DEBUG(0, ("add_sid_to_array failed\n"));
315 status = sid_array_from_info3(talloc_tos(), info3,
319 if (!NT_STATUS_IS_OK(status)) {
324 if (!NT_STATUS_IS_OK(status = add_aliases(get_global_sam_sid(),
326 || !NT_STATUS_IS_OK(status = add_aliases(&global_sid_Builtin,
328 DEBUG(3, ("could not add aliases: %s\n",
334 security_token_debug(DBGC_CLASS, 10, token);
336 for (i=0; i<num_require_membership_of_sid; i++) {
337 DEBUG(10, ("Checking SID %s\n", sid_string_dbg(
338 &require_membership_of_sid[i])));
339 if (nt_token_check_sid(&require_membership_of_sid[i],
341 DEBUG(10, ("Access ok\n"));
347 /* Do not distinguish this error from a wrong username/pw */
350 return NT_STATUS_LOGON_FAILURE;
353 struct winbindd_domain *find_auth_domain(uint8_t flags,
354 const char *domain_name)
356 struct winbindd_domain *domain;
359 domain = find_domain_from_name_noinit(domain_name);
360 if (domain == NULL) {
361 DEBUG(3, ("Authentication for domain [%s] refused "
362 "as it is not a trusted domain\n",
368 if (strequal(domain_name, get_global_sam_name())) {
369 return find_domain_from_name_noinit(domain_name);
372 /* we can auth against trusted domains */
373 if (flags & WBFLAG_PAM_CONTACT_TRUSTDOM) {
374 domain = find_domain_from_name_noinit(domain_name);
375 if (domain == NULL) {
376 DEBUG(3, ("Authentication for domain [%s] skipped "
377 "as it is not a trusted domain\n",
384 return find_our_domain();
387 static void fake_password_policy(struct winbindd_response *r,
388 const struct netr_SamBaseInfo *bi)
390 NTTIME min_password_age;
391 NTTIME max_password_age;
393 if (bi->allow_password_change > bi->last_password_change) {
394 min_password_age = bi->allow_password_change -
395 bi->last_password_change;
397 min_password_age = 0;
400 if (bi->force_password_change > bi->last_password_change) {
401 max_password_age = bi->force_password_change -
402 bi->last_password_change;
404 max_password_age = 0;
407 r->data.auth.policy.min_length_password = 0;
408 r->data.auth.policy.password_history = 0;
409 r->data.auth.policy.password_properties = 0;
410 r->data.auth.policy.expire =
411 nt_time_to_unix_abs(&max_password_age);
412 r->data.auth.policy.min_passwordage =
413 nt_time_to_unix_abs(&min_password_age);
416 static void fill_in_password_policy(struct winbindd_response *r,
417 const struct samr_DomInfo1 *p)
419 r->data.auth.policy.min_length_password =
420 p->min_password_length;
421 r->data.auth.policy.password_history =
422 p->password_history_length;
423 r->data.auth.policy.password_properties =
424 p->password_properties;
425 r->data.auth.policy.expire =
426 nt_time_to_unix_abs((const NTTIME *)&(p->max_password_age));
427 r->data.auth.policy.min_passwordage =
428 nt_time_to_unix_abs((const NTTIME *)&(p->min_password_age));
431 static NTSTATUS fillup_password_policy(struct winbindd_domain *domain,
432 struct winbindd_response *response)
434 TALLOC_CTX *frame = talloc_stackframe();
436 struct samr_DomInfo1 password_policy;
438 if ( !winbindd_can_contact_domain( domain ) ) {
439 DEBUG(5,("fillup_password_policy: No inbound trust to "
440 "contact domain %s\n", domain->name));
441 status = NT_STATUS_NOT_SUPPORTED;
445 status = wb_cache_password_policy(domain, talloc_tos(),
447 if (NT_STATUS_IS_ERR(status)) {
451 fill_in_password_policy(response, &password_policy);
458 static NTSTATUS get_max_bad_attempts_from_lockout_policy(struct winbindd_domain *domain,
460 uint16_t *lockout_threshold)
462 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
463 struct samr_DomInfo12 lockout_policy;
465 *lockout_threshold = 0;
467 status = wb_cache_lockout_policy(domain, mem_ctx, &lockout_policy);
468 if (NT_STATUS_IS_ERR(status)) {
472 *lockout_threshold = lockout_policy.lockout_threshold;
477 static NTSTATUS get_pwd_properties(struct winbindd_domain *domain,
479 uint32_t *password_properties)
481 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
482 struct samr_DomInfo1 password_policy;
484 *password_properties = 0;
486 status = wb_cache_password_policy(domain, mem_ctx, &password_policy);
487 if (NT_STATUS_IS_ERR(status)) {
491 *password_properties = password_policy.password_properties;
498 static const char *generate_krb5_ccache(TALLOC_CTX *mem_ctx,
501 const char **user_ccache_file)
503 /* accept FILE and WRFILE as krb5_cc_type from the client and then
504 * build the full ccname string based on the user's uid here -
507 const char *gen_cc = NULL;
510 if (strequal(type, "FILE")) {
511 gen_cc = talloc_asprintf(
512 mem_ctx, "FILE:/tmp/krb5cc_%d", uid);
514 if (strequal(type, "WRFILE")) {
515 gen_cc = talloc_asprintf(
516 mem_ctx, "WRFILE:/tmp/krb5cc_%d", uid);
518 if (strequal(type, "KEYRING")) {
519 gen_cc = talloc_asprintf(
520 mem_ctx, "KEYRING:persistent:%d", uid);
523 if (strnequal(type, "FILE:/", 6) ||
524 strnequal(type, "WRFILE:/", 8) ||
525 strnequal(type, "DIR:/", 5)) {
527 /* we allow only one "%u" substitution */
531 p = strchr(type, '%');
536 if (p != NULL && *p == 'u' && strchr(p, '%') == NULL) {
537 char uid_str[sizeof("18446744073709551615")];
539 snprintf(uid_str, sizeof(uid_str), "%u", uid);
541 gen_cc = talloc_string_sub2(mem_ctx,
545 /* remove_unsafe_characters */
549 /* allow_trailing_dollar */
556 *user_ccache_file = gen_cc;
558 if (gen_cc == NULL) {
559 gen_cc = talloc_strdup(mem_ctx, "MEMORY:winbindd_pam_ccache");
561 if (gen_cc == NULL) {
562 DEBUG(0,("out of memory\n"));
566 DEBUG(10, ("using ccache: %s%s\n", gen_cc,
567 (*user_ccache_file == NULL) ? " (internal)":""));
574 uid_t get_uid_from_request(struct winbindd_request *request)
578 uid = request->data.auth.uid;
580 if (uid == (uid_t)-1) {
581 DEBUG(1,("invalid uid: '%u'\n", (unsigned int)uid));
587 /**********************************************************************
588 Authenticate a user with a clear text password using Kerberos and fill up
590 **********************************************************************/
592 static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
593 struct winbindd_domain *domain,
596 const char *krb5_cc_type,
598 struct netr_SamInfo3 **info3,
602 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
603 krb5_error_code krb5_ret;
604 const char *cc = NULL;
605 const char *principal_s = NULL;
606 const char *service = NULL;
608 fstring name_domain, name_user;
609 time_t ticket_lifetime = 0;
610 time_t renewal_until = 0;
612 time_t time_offset = 0;
613 const char *user_ccache_file;
614 struct PAC_LOGON_INFO *logon_info = NULL;
615 struct PAC_DATA *pac_data = NULL;
616 struct PAC_DATA_CTR *pac_data_ctr = NULL;
617 const char *local_service;
619 struct netr_SamInfo3 *info3_copy = NULL;
623 if (domain->alt_name == NULL) {
624 return NT_STATUS_INVALID_PARAMETER;
628 * prepare a krb5_cc_cache string for the user */
631 DEBUG(0,("no valid uid\n"));
634 cc = generate_krb5_ccache(mem_ctx,
639 return NT_STATUS_NO_MEMORY;
644 * get kerberos properties */
646 if (domain->private_data) {
647 ads = (ADS_STRUCT *)domain->private_data;
648 time_offset = ads->auth.time_offset;
653 * do kerberos auth and setup ccache as the user */
655 parse_domain_user(user, name_domain, name_user);
657 realm = talloc_strdup(mem_ctx, domain->alt_name);
659 return NT_STATUS_NO_MEMORY;
662 if (!strupper_m(realm)) {
663 return NT_STATUS_INVALID_PARAMETER;
666 principal_s = talloc_asprintf(mem_ctx, "%s@%s", name_user, realm);
667 if (principal_s == NULL) {
668 return NT_STATUS_NO_MEMORY;
671 service = talloc_asprintf(mem_ctx, "%s/%s@%s", KRB5_TGS_NAME, realm, realm);
672 if (service == NULL) {
673 return NT_STATUS_NO_MEMORY;
676 local_service = talloc_asprintf(mem_ctx, "%s$@%s",
677 lp_netbios_name(), lp_realm());
678 if (local_service == NULL) {
679 return NT_STATUS_NO_MEMORY;
683 /* if this is a user ccache, we need to act as the user to let the krb5
684 * library handle the chown, etc. */
686 /************************ ENTERING NON-ROOT **********************/
688 if (user_ccache_file != NULL) {
689 set_effective_uid(uid);
690 DEBUG(10,("winbindd_raw_kerberos_login: uid is %d\n", uid));
693 result = kerberos_return_pac(mem_ctx,
702 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
706 if (user_ccache_file != NULL) {
707 gain_root_privilege();
710 /************************ RETURNED TO ROOT **********************/
712 if (!NT_STATUS_IS_OK(result)) {
716 if (pac_data_ctr == NULL) {
720 pac_data = pac_data_ctr->pac_data;
721 if (pac_data == NULL) {
725 for (i=0; i < pac_data->num_buffers; i++) {
727 if (pac_data->buffers[i].type != PAC_TYPE_LOGON_INFO) {
731 logon_info = pac_data->buffers[i].info->logon_info.info;
733 return NT_STATUS_INVALID_PARAMETER;
739 if (logon_info == NULL) {
740 DEBUG(10,("Missing logon_info in ticket of %s\n",
742 return NT_STATUS_INVALID_PARAMETER;
745 DEBUG(10,("winbindd_raw_kerberos_login: winbindd validated ticket of %s\n",
748 result = create_info3_from_pac_logon_info(mem_ctx, logon_info, &info3_copy);
749 if (!NT_STATUS_IS_OK(result)) {
753 /* if we had a user's ccache then return that string for the pam
756 if (user_ccache_file != NULL) {
758 fstrcpy(krb5ccname, user_ccache_file);
760 result = add_ccache_to_list(principal_s,
772 if (!NT_STATUS_IS_OK(result)) {
773 DEBUG(10,("winbindd_raw_kerberos_login: failed to add ccache to list: %s\n",
778 /* need to delete the memory cred cache, it is not used anymore */
780 krb5_ret = ads_kdestroy(cc);
782 DEBUG(3,("winbindd_raw_kerberos_login: "
783 "could not destroy krb5 credential cache: "
784 "%s\n", error_message(krb5_ret)));
793 * Do not delete an existing valid credential cache, if the user
794 * e.g. enters a wrong password
796 if ((strequal(krb5_cc_type, "FILE") || strequal(krb5_cc_type, "WRFILE"))
797 && user_ccache_file != NULL) {
801 /* we could have created a new credential cache with a valid tgt in it
802 * but we werent able to get or verify the service ticket for this
803 * local host and therefor didn't get the PAC, we need to remove that
804 * cache entirely now */
806 krb5_ret = ads_kdestroy(cc);
808 DEBUG(3,("winbindd_raw_kerberos_login: "
809 "could not destroy krb5 credential cache: "
810 "%s\n", error_message(krb5_ret)));
813 if (!NT_STATUS_IS_OK(remove_ccache(user))) {
814 DEBUG(3,("winbindd_raw_kerberos_login: "
815 "could not remove ccache for user %s\n",
821 return NT_STATUS_NOT_SUPPORTED;
822 #endif /* HAVE_KRB5 */
825 /****************************************************************
826 ****************************************************************/
828 bool check_request_flags(uint32_t flags)
830 uint32_t flags_edata = WBFLAG_PAM_AFS_TOKEN |
831 WBFLAG_PAM_INFO3_TEXT |
832 WBFLAG_PAM_INFO3_NDR;
834 if ( ( (flags & flags_edata) == WBFLAG_PAM_AFS_TOKEN) ||
835 ( (flags & flags_edata) == WBFLAG_PAM_INFO3_NDR) ||
836 ( (flags & flags_edata) == WBFLAG_PAM_INFO3_TEXT)||
837 !(flags & flags_edata) ) {
841 DEBUG(1, ("check_request_flags: invalid request flags[0x%08X]\n",
847 /****************************************************************
848 ****************************************************************/
850 NTSTATUS append_auth_data(TALLOC_CTX *mem_ctx,
851 struct winbindd_response *resp,
852 uint32_t request_flags,
853 struct netr_SamInfo3 *info3,
854 const char *name_domain,
855 const char *name_user)
859 if (request_flags & WBFLAG_PAM_USER_SESSION_KEY) {
860 memcpy(resp->data.auth.user_session_key,
862 sizeof(resp->data.auth.user_session_key)
866 if (request_flags & WBFLAG_PAM_LMKEY) {
867 memcpy(resp->data.auth.first_8_lm_hash,
868 info3->base.LMSessKey.key,
869 sizeof(resp->data.auth.first_8_lm_hash)
873 if (request_flags & WBFLAG_PAM_UNIX_NAME) {
874 result = append_unix_username(mem_ctx, resp,
875 info3, name_domain, name_user);
876 if (!NT_STATUS_IS_OK(result)) {
877 DEBUG(10,("Failed to append Unix Username: %s\n",
883 /* currently, anything from here on potentially overwrites extra_data. */
885 if (request_flags & WBFLAG_PAM_INFO3_NDR) {
886 result = append_info3_as_ndr(mem_ctx, resp, info3);
887 if (!NT_STATUS_IS_OK(result)) {
888 DEBUG(10,("Failed to append INFO3 (NDR): %s\n",
894 if (request_flags & WBFLAG_PAM_INFO3_TEXT) {
895 result = append_info3_as_txt(mem_ctx, resp, info3);
896 if (!NT_STATUS_IS_OK(result)) {
897 DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
903 if (request_flags & WBFLAG_PAM_AFS_TOKEN) {
904 result = append_afs_token(mem_ctx, resp,
905 info3, name_domain, name_user);
906 if (!NT_STATUS_IS_OK(result)) {
907 DEBUG(10,("Failed to append AFS token: %s\n",
916 static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
917 struct winbindd_cli_state *state,
918 struct netr_SamInfo3 **info3)
920 NTSTATUS result = NT_STATUS_LOGON_FAILURE;
921 uint16_t max_allowed_bad_attempts;
922 fstring name_domain, name_user;
924 enum lsa_SidType type;
925 uchar new_nt_pass[NT_HASH_LEN];
926 const uint8_t *cached_nt_pass;
927 const uint8_t *cached_salt;
928 struct netr_SamInfo3 *my_info3;
929 time_t kickoff_time, must_change_time;
930 bool password_good = false;
932 struct winbindd_tdc_domain *tdc_domain = NULL;
939 DEBUG(10,("winbindd_dual_pam_auth_cached\n"));
941 /* Parse domain and username */
943 parse_domain_user(state->request->data.auth.user, name_domain, name_user);
946 if (!lookup_cached_name(name_domain,
950 DEBUG(10,("winbindd_dual_pam_auth_cached: no such user in the cache\n"));
951 return NT_STATUS_NO_SUCH_USER;
954 if (type != SID_NAME_USER) {
955 DEBUG(10,("winbindd_dual_pam_auth_cached: not a user (%s)\n", sid_type_lookup(type)));
956 return NT_STATUS_LOGON_FAILURE;
959 result = winbindd_get_creds(domain,
965 if (!NT_STATUS_IS_OK(result)) {
966 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get creds: %s\n", nt_errstr(result)));
972 E_md4hash(state->request->data.auth.pass, new_nt_pass);
974 dump_data_pw("new_nt_pass", new_nt_pass, NT_HASH_LEN);
975 dump_data_pw("cached_nt_pass", cached_nt_pass, NT_HASH_LEN);
977 dump_data_pw("cached_salt", cached_salt, NT_HASH_LEN);
981 /* In this case we didn't store the nt_hash itself,
982 but the MD5 combination of salt + nt_hash. */
983 uchar salted_hash[NT_HASH_LEN];
984 E_md5hash(cached_salt, new_nt_pass, salted_hash);
986 password_good = (memcmp(cached_nt_pass, salted_hash,
989 /* Old cached cred - direct store of nt_hash (bad bad bad !). */
990 password_good = (memcmp(cached_nt_pass, new_nt_pass,
996 /* User *DOES* know the password, update logon_time and reset
999 my_info3->base.user_flags |= NETLOGON_CACHED_ACCOUNT;
1001 if (my_info3->base.acct_flags & ACB_AUTOLOCK) {
1002 return NT_STATUS_ACCOUNT_LOCKED_OUT;
1005 if (my_info3->base.acct_flags & ACB_DISABLED) {
1006 return NT_STATUS_ACCOUNT_DISABLED;
1009 if (my_info3->base.acct_flags & ACB_WSTRUST) {
1010 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
1013 if (my_info3->base.acct_flags & ACB_SVRTRUST) {
1014 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
1017 if (my_info3->base.acct_flags & ACB_DOMTRUST) {
1018 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
1021 if (!(my_info3->base.acct_flags & ACB_NORMAL)) {
1022 DEBUG(0,("winbindd_dual_pam_auth_cached: whats wrong with that one?: 0x%08x\n",
1023 my_info3->base.acct_flags));
1024 return NT_STATUS_LOGON_FAILURE;
1027 kickoff_time = nt_time_to_unix(my_info3->base.kickoff_time);
1028 if (kickoff_time != 0 && time(NULL) > kickoff_time) {
1029 return NT_STATUS_ACCOUNT_EXPIRED;
1032 must_change_time = nt_time_to_unix(my_info3->base.force_password_change);
1033 if (must_change_time != 0 && must_change_time < time(NULL)) {
1034 /* we allow grace logons when the password has expired */
1035 my_info3->base.user_flags |= NETLOGON_GRACE_LOGON;
1036 /* return NT_STATUS_PASSWORD_EXPIRED; */
1041 if ((state->request->flags & WBFLAG_PAM_KRB5) &&
1042 ((tdc_domain = wcache_tdc_fetch_domain(state->mem_ctx, name_domain)) != NULL) &&
1043 ((tdc_domain->trust_type & LSA_TRUST_TYPE_UPLEVEL) ||
1044 /* used to cope with the case winbindd starting without network. */
1045 !strequal(tdc_domain->domain_name, tdc_domain->dns_name))) {
1048 const char *cc = NULL;
1050 const char *principal_s = NULL;
1051 const char *service = NULL;
1052 const char *user_ccache_file;
1054 if (domain->alt_name == NULL) {
1055 return NT_STATUS_INVALID_PARAMETER;
1058 uid = get_uid_from_request(state->request);
1060 DEBUG(0,("winbindd_dual_pam_auth_cached: invalid uid\n"));
1061 return NT_STATUS_INVALID_PARAMETER;
1064 cc = generate_krb5_ccache(state->mem_ctx,
1065 state->request->data.auth.krb5_cc_type,
1066 state->request->data.auth.uid,
1069 return NT_STATUS_NO_MEMORY;
1072 realm = talloc_strdup(state->mem_ctx, domain->alt_name);
1073 if (realm == NULL) {
1074 return NT_STATUS_NO_MEMORY;
1077 if (!strupper_m(realm)) {
1078 return NT_STATUS_INVALID_PARAMETER;
1081 principal_s = talloc_asprintf(state->mem_ctx, "%s@%s", name_user, realm);
1082 if (principal_s == NULL) {
1083 return NT_STATUS_NO_MEMORY;
1086 service = talloc_asprintf(state->mem_ctx, "%s/%s@%s", KRB5_TGS_NAME, realm, realm);
1087 if (service == NULL) {
1088 return NT_STATUS_NO_MEMORY;
1091 if (user_ccache_file != NULL) {
1093 fstrcpy(state->response->data.auth.krb5ccname,
1096 result = add_ccache_to_list(principal_s,
1099 state->request->data.auth.user,
1100 state->request->data.auth.pass,
1104 time(NULL) + lp_winbind_cache_time(),
1105 time(NULL) + WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
1108 if (!NT_STATUS_IS_OK(result)) {
1109 DEBUG(10,("winbindd_dual_pam_auth_cached: failed "
1110 "to add ccache to list: %s\n",
1111 nt_errstr(result)));
1115 #endif /* HAVE_KRB5 */
1117 /* FIXME: we possibly should handle logon hours as well (does xp when
1118 * offline?) see auth/auth_sam.c:sam_account_ok for details */
1120 unix_to_nt_time(&my_info3->base.logon_time, time(NULL));
1121 my_info3->base.bad_password_count = 0;
1123 result = winbindd_update_creds_by_info3(domain,
1124 state->request->data.auth.user,
1125 state->request->data.auth.pass,
1127 if (!NT_STATUS_IS_OK(result)) {
1128 DEBUG(1,("winbindd_dual_pam_auth_cached: failed to update creds: %s\n",
1129 nt_errstr(result)));
1133 return NT_STATUS_OK;
1137 /* User does *NOT* know the correct password, modify info3 accordingly, but only if online */
1138 if (domain->online == false) {
1142 /* failure of this is not critical */
1143 result = get_max_bad_attempts_from_lockout_policy(domain, state->mem_ctx, &max_allowed_bad_attempts);
1144 if (!NT_STATUS_IS_OK(result)) {
1145 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get max_allowed_bad_attempts. "
1146 "Won't be able to honour account lockout policies\n"));
1149 /* increase counter */
1150 my_info3->base.bad_password_count++;
1152 if (max_allowed_bad_attempts == 0) {
1157 if (my_info3->base.bad_password_count >= max_allowed_bad_attempts) {
1159 uint32_t password_properties;
1161 result = get_pwd_properties(domain, state->mem_ctx, &password_properties);
1162 if (!NT_STATUS_IS_OK(result)) {
1163 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get password properties.\n"));
1166 if ((my_info3->base.rid != DOMAIN_RID_ADMINISTRATOR) ||
1167 (password_properties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)) {
1168 my_info3->base.acct_flags |= ACB_AUTOLOCK;
1173 result = winbindd_update_creds_by_info3(domain,
1174 state->request->data.auth.user,
1178 if (!NT_STATUS_IS_OK(result)) {
1179 DEBUG(0,("winbindd_dual_pam_auth_cached: failed to update creds %s\n",
1180 nt_errstr(result)));
1183 return NT_STATUS_LOGON_FAILURE;
1186 static NTSTATUS winbindd_dual_pam_auth_kerberos(struct winbindd_domain *domain,
1187 struct winbindd_cli_state *state,
1188 struct netr_SamInfo3 **info3)
1190 struct winbindd_domain *contact_domain;
1191 fstring name_domain, name_user;
1194 DEBUG(10,("winbindd_dual_pam_auth_kerberos\n"));
1196 /* Parse domain and username */
1198 parse_domain_user(state->request->data.auth.user, name_domain, name_user);
1200 /* what domain should we contact? */
1203 if (!(contact_domain = find_domain_from_name(name_domain))) {
1204 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1205 state->request->data.auth.user, name_domain, name_user, name_domain));
1206 result = NT_STATUS_NO_SUCH_USER;
1211 if (is_myname(name_domain)) {
1212 DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
1213 result = NT_STATUS_NO_SUCH_USER;
1217 contact_domain = find_domain_from_name(name_domain);
1218 if (contact_domain == NULL) {
1219 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1220 state->request->data.auth.user, name_domain, name_user, name_domain));
1222 result = NT_STATUS_NO_SUCH_USER;
1227 if (contact_domain->initialized &&
1228 contact_domain->active_directory) {
1232 if (!contact_domain->initialized) {
1233 init_dc_connection(contact_domain, false);
1236 if (!contact_domain->active_directory) {
1237 DEBUG(3,("krb5 auth requested but domain is not Active Directory\n"));
1238 return NT_STATUS_INVALID_LOGON_TYPE;
1241 result = winbindd_raw_kerberos_login(
1242 state->mem_ctx, contact_domain,
1243 state->request->data.auth.user,
1244 state->request->data.auth.pass,
1245 state->request->data.auth.krb5_cc_type,
1246 get_uid_from_request(state->request),
1247 info3, state->response->data.auth.krb5ccname);
1252 static NTSTATUS winbindd_dual_auth_passdb(TALLOC_CTX *mem_ctx,
1253 uint32_t logon_parameters,
1254 const char *domain, const char *user,
1255 const DATA_BLOB *challenge,
1256 const DATA_BLOB *lm_resp,
1257 const DATA_BLOB *nt_resp,
1259 uint8_t *pauthoritative,
1260 struct netr_SamInfo3 **pinfo3)
1262 struct auth_context *auth_context;
1263 struct auth_serversupplied_info *server_info;
1264 struct auth_usersupplied_info *user_info = NULL;
1265 struct tsocket_address *local;
1266 struct netr_SamInfo3 *info3;
1270 TALLOC_CTX *frame = talloc_stackframe();
1273 * We are authoritative by default
1275 *pauthoritative = 1;
1277 rc = tsocket_address_inet_from_strings(frame,
1284 return NT_STATUS_NO_MEMORY;
1288 * TODO: We should get the service description passed in from
1289 * the winbind client, so we can have "smb2", "squid" or "samr" logged
1292 status = make_user_info(frame, &user_info, user, user, domain, domain,
1293 lp_netbios_name(), local, local,
1295 lm_resp, nt_resp, NULL, NULL,
1296 NULL, AUTH_PASSWORD_RESPONSE);
1297 if (!NT_STATUS_IS_OK(status)) {
1298 DEBUG(10, ("make_user_info failed: %s\n", nt_errstr(status)));
1303 user_info->logon_parameters = logon_parameters;
1305 /* We don't want any more mapping of the username */
1306 user_info->mapped_state = True;
1308 /* We don't want to come back to winbindd or to do PAM account checks */
1309 user_info->flags |= USER_INFO_INFO3_AND_NO_AUTHZ;
1312 user_info->flags |= USER_INFO_INTERACTIVE_LOGON;
1315 status = make_auth3_context_for_winbind(frame, &auth_context);
1316 if (!NT_STATUS_IS_OK(status)) {
1317 DBG_ERR("make_auth3_context_for_winbind failed: %s\n",
1323 ok = auth3_context_set_challenge(auth_context,
1324 challenge->data, "fixed");
1327 return NT_STATUS_NO_MEMORY;
1330 status = auth_check_ntlm_password(mem_ctx,
1335 if (!NT_STATUS_IS_OK(status)) {
1340 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
1341 if (info3 == NULL) {
1343 return NT_STATUS_NO_MEMORY;
1346 status = serverinfo_to_SamInfo3(server_info, info3);
1347 if (!NT_STATUS_IS_OK(status)) {
1350 DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
1351 nt_errstr(status)));
1356 DEBUG(10, ("Authenticaticating user %s\\%s returned %s\n", domain,
1357 user, nt_errstr(status)));
1362 static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
1363 TALLOC_CTX *mem_ctx,
1364 uint32_t logon_parameters,
1365 const char *username,
1366 const char *password,
1367 const char *domainname,
1368 const char *workstation,
1369 const uint8_t chal[8],
1370 DATA_BLOB lm_response,
1371 DATA_BLOB nt_response,
1373 uint8_t *authoritative,
1375 struct netr_SamInfo3 **info3)
1378 int netr_attempts = 0;
1383 struct rpc_pipe_client *netlogon_pipe;
1385 ZERO_STRUCTP(info3);
1388 result = cm_connect_netlogon(domain, &netlogon_pipe);
1390 if (NT_STATUS_EQUAL(result,
1391 NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
1393 * This means we don't have a trust account.
1396 result = NT_STATUS_NO_SUCH_USER;
1400 if (!NT_STATUS_IS_OK(result)) {
1401 DEBUG(3,("Could not open handle to NETLOGON pipe "
1402 "(error: %s, attempts: %d)\n",
1403 nt_errstr(result), netr_attempts));
1405 /* After the first retry always close the connection */
1406 if (netr_attempts > 0) {
1407 DEBUG(3, ("This is again a problem for this "
1408 "particular call, forcing the close "
1409 "of this connection\n"));
1410 invalidate_cm_connection(domain);
1413 /* After the second retry failover to the next DC */
1414 if (netr_attempts > 1) {
1416 * If the netlogon server is not reachable then
1417 * it is possible that the DC is rebuilding
1418 * sysvol and shutdown netlogon for that time.
1419 * We should failover to the next dc.
1421 DEBUG(3, ("This is the third problem for this "
1422 "particular call, adding DC to the "
1423 "negative cache list: %s %s\n", domain->name, domain->dcname));
1424 add_failed_connection_entry(domain->name,
1427 saf_delete(domain->name);
1430 /* Only allow 3 retries */
1431 if (netr_attempts < 3) {
1432 DEBUG(3, ("The connection to netlogon "
1433 "failed, retrying\n"));
1441 if (domain->conn.netlogon_creds_ctx == NULL) {
1442 DBG_NOTICE("No security credentials available for "
1443 "domain [%s]\n", domainname);
1444 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1445 } else if (interactive) {
1446 result = rpccli_netlogon_password_logon(
1447 domain->conn.netlogon_creds_ctx,
1448 netlogon_pipe->binding_handle,
1455 NetlogonInteractiveInformation,
1460 result = rpccli_netlogon_network_logon(
1461 domain->conn.netlogon_creds_ctx,
1462 netlogon_pipe->binding_handle,
1477 * we increment this after the "feature negotiation"
1478 * for can_do_samlogon_ex and can_do_validation6
1482 /* We have to try a second time as cm_connect_netlogon
1483 might not yet have noticed that the DC has killed
1486 if (!rpccli_is_connected(netlogon_pipe)) {
1491 /* if we get access denied, a possible cause was that we had
1492 and open connection to the DC, but someone changed our
1493 machine account password out from underneath us using 'net
1494 rpc changetrustpw' */
1496 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1497 DEBUG(1,("winbind_samlogon_retry_loop: sam_logon returned "
1498 "ACCESS_DENIED. Maybe the DC has Restrict "
1499 "NTLM set or the trust account "
1500 "password was changed and we didn't know it. "
1501 "Killing connections to domain %s\n",
1503 invalidate_cm_connection(domain);
1507 if (NT_STATUS_EQUAL(result, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
1509 * Got DCERPC_FAULT_OP_RNG_ERROR for SamLogon
1510 * (no Ex). This happens against old Samba
1511 * DCs, if LogonSamLogonEx() fails with an error
1512 * e.g. NT_STATUS_NO_SUCH_USER or NT_STATUS_WRONG_PASSWORD.
1514 * The server will log something like this:
1515 * api_net_sam_logon_ex: Failed to marshall NET_R_SAM_LOGON_EX.
1517 * This sets the whole connection into a fault_state mode
1518 * and all following request get NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE.
1520 * This also happens to our retry with LogonSamLogonWithFlags()
1521 * and LogonSamLogon().
1523 * In order to recover from this situation, we need to
1524 * drop the connection.
1526 invalidate_cm_connection(domain);
1527 result = NT_STATUS_LOGON_FAILURE;
1531 } while ( (attempts < 2) && retry );
1533 if (NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT)) {
1534 DEBUG(3,("winbind_samlogon_retry_loop: sam_network_logon(ex) "
1535 "returned NT_STATUS_IO_TIMEOUT after the retry. "
1536 "Killing connections to domain %s\n",
1538 invalidate_cm_connection(domain);
1543 static NTSTATUS winbindd_dual_pam_auth_samlogon(TALLOC_CTX *mem_ctx,
1544 struct winbindd_domain *domain,
1547 uint32_t request_flags,
1548 struct netr_SamInfo3 **info3)
1554 unsigned char local_nt_response[24];
1555 fstring name_domain, name_user;
1557 struct netr_SamInfo3 *my_info3 = NULL;
1558 uint8_t authoritative = 0;
1563 DEBUG(10,("winbindd_dual_pam_auth_samlogon\n"));
1565 /* Parse domain and username */
1567 parse_domain_user(user, name_domain, name_user);
1570 * We check against domain->name instead of
1571 * name_domain, as find_auth_domain() ->
1572 * find_domain_from_name_noinit() already decided
1573 * that we are in a child for the correct domain.
1575 * name_domain can also be lp_realm()
1576 * we need to check against domain->name.
1578 if (strequal(domain->name, get_global_sam_name())) {
1579 DATA_BLOB chal_blob = data_blob_const(chal, sizeof(chal));
1581 /* do password magic */
1583 generate_random_buffer(chal, sizeof(chal));
1585 if (lp_client_ntlmv2_auth()) {
1586 DATA_BLOB server_chal;
1587 DATA_BLOB names_blob;
1588 server_chal = data_blob_const(chal, 8);
1590 /* note that the 'workgroup' here is for the local
1591 machine. The 'server name' must match the
1592 'workstation' passed to the actual SamLogon call.
1594 names_blob = NTLMv2_generate_names_blob(
1595 mem_ctx, lp_netbios_name(), lp_workgroup());
1597 if (!SMBNTLMv2encrypt(mem_ctx, name_user, name_domain,
1601 &lm_resp, &nt_resp, NULL, NULL)) {
1602 data_blob_free(&names_blob);
1603 DEBUG(0, ("winbindd_pam_auth: SMBNTLMv2encrypt() failed!\n"));
1604 result = NT_STATUS_NO_MEMORY;
1607 data_blob_free(&names_blob);
1609 lm_resp = data_blob_null;
1610 SMBNTencrypt(pass, chal, local_nt_response);
1612 nt_resp = data_blob_talloc(mem_ctx, local_nt_response,
1613 sizeof(local_nt_response));
1616 result = winbindd_dual_auth_passdb(
1617 mem_ctx, 0, name_domain, name_user,
1618 &chal_blob, &lm_resp, &nt_resp,
1619 true, /* interactive */
1624 * We need to try the remote NETLOGON server if this is
1625 * not authoritative (for example on the RODC).
1627 if (authoritative != 0) {
1632 /* check authentication loop */
1634 result = winbind_samlogon_retry_loop(domain,
1642 data_blob_null, data_blob_null,
1643 true, /* interactive */
1647 if (!NT_STATUS_IS_OK(result)) {
1651 /* handle the case where a NT4 DC does not fill in the acct_flags in
1652 * the samlogon reply info3. When accurate info3 is required by the
1653 * caller, we look up the account flags ourselves - gd */
1655 if ((request_flags & WBFLAG_PAM_INFO3_TEXT) &&
1656 NT_STATUS_IS_OK(result) && (my_info3->base.acct_flags == 0)) {
1658 struct rpc_pipe_client *samr_pipe;
1659 struct policy_handle samr_domain_handle, user_pol;
1660 union samr_UserInfo *info = NULL;
1661 NTSTATUS status_tmp, result_tmp;
1662 uint32_t acct_flags;
1663 struct dcerpc_binding_handle *b;
1665 status_tmp = cm_connect_sam(domain, mem_ctx, false,
1666 &samr_pipe, &samr_domain_handle);
1668 if (!NT_STATUS_IS_OK(status_tmp)) {
1669 DEBUG(3, ("could not open handle to SAMR pipe: %s\n",
1670 nt_errstr(status_tmp)));
1674 b = samr_pipe->binding_handle;
1676 status_tmp = dcerpc_samr_OpenUser(b, mem_ctx,
1677 &samr_domain_handle,
1678 MAXIMUM_ALLOWED_ACCESS,
1683 if (!NT_STATUS_IS_OK(status_tmp)) {
1684 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1685 nt_errstr(status_tmp)));
1688 if (!NT_STATUS_IS_OK(result_tmp)) {
1689 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1690 nt_errstr(result_tmp)));
1694 status_tmp = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1700 if (any_nt_status_not_ok(status_tmp, result_tmp,
1702 DEBUG(3, ("could not query user info on SAMR pipe: %s\n",
1703 nt_errstr(status_tmp)));
1704 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1708 acct_flags = info->info16.acct_flags;
1710 if (acct_flags == 0) {
1711 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1715 my_info3->base.acct_flags = acct_flags;
1717 DEBUG(10,("successfully retrieved acct_flags 0x%x\n", acct_flags));
1719 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1727 enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
1728 struct winbindd_cli_state *state)
1730 NTSTATUS result = NT_STATUS_LOGON_FAILURE;
1731 NTSTATUS krb5_result = NT_STATUS_OK;
1732 fstring name_domain, name_user;
1734 fstring domain_user;
1735 struct netr_SamInfo3 *info3 = NULL;
1736 NTSTATUS name_map_status = NT_STATUS_UNSUCCESSFUL;
1738 /* Ensure null termination */
1739 state->request->data.auth.user[sizeof(state->request->data.auth.user)-1]='\0';
1741 /* Ensure null termination */
1742 state->request->data.auth.pass[sizeof(state->request->data.auth.pass)-1]='\0';
1744 DEBUG(3, ("[%5lu]: dual pam auth %s\n", (unsigned long)state->pid,
1745 state->request->data.auth.user));
1747 /* Parse domain and username */
1749 name_map_status = normalize_name_unmap(state->mem_ctx,
1750 state->request->data.auth.user,
1753 /* If the name normalization didnt' actually do anything,
1754 just use the original name */
1756 if (!NT_STATUS_IS_OK(name_map_status) &&
1757 !NT_STATUS_EQUAL(name_map_status, NT_STATUS_FILE_RENAMED))
1759 mapped_user = state->request->data.auth.user;
1762 parse_domain_user(mapped_user, name_domain, name_user);
1764 if ( mapped_user != state->request->data.auth.user ) {
1765 fstr_sprintf( domain_user, "%s%c%s", name_domain,
1766 *lp_winbind_separator(),
1768 strlcpy( state->request->data.auth.user, domain_user,
1769 sizeof(state->request->data.auth.user));
1772 if (!domain->online) {
1773 result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1774 if (domain->startup) {
1775 /* Logons are very important to users. If we're offline and
1776 we get a request within the first 30 seconds of startup,
1777 try very hard to find a DC and go online. */
1779 DEBUG(10,("winbindd_dual_pam_auth: domain: %s offline and auth "
1780 "request in startup mode.\n", domain->name ));
1782 winbindd_flush_negative_conn_cache(domain);
1783 result = init_dc_connection(domain, false);
1787 DEBUG(10,("winbindd_dual_pam_auth: domain: %s last was %s\n", domain->name, domain->online ? "online":"offline"));
1789 /* Check for Kerberos authentication */
1790 if (domain->online && (state->request->flags & WBFLAG_PAM_KRB5)) {
1792 result = winbindd_dual_pam_auth_kerberos(domain, state, &info3);
1793 /* save for later */
1794 krb5_result = result;
1797 if (NT_STATUS_IS_OK(result)) {
1798 DEBUG(10,("winbindd_dual_pam_auth_kerberos succeeded\n"));
1799 goto process_result;
1801 DEBUG(10,("winbindd_dual_pam_auth_kerberos failed: %s\n", nt_errstr(result)));
1804 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS) ||
1805 NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT) ||
1806 NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
1807 DEBUG(10,("winbindd_dual_pam_auth_kerberos setting domain to offline\n"));
1808 set_domain_offline( domain );
1812 /* there are quite some NT_STATUS errors where there is no
1813 * point in retrying with a samlogon, we explictly have to take
1814 * care not to increase the bad logon counter on the DC */
1816 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_DISABLED) ||
1817 NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_EXPIRED) ||
1818 NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_LOCKED_OUT) ||
1819 NT_STATUS_EQUAL(result, NT_STATUS_INVALID_LOGON_HOURS) ||
1820 NT_STATUS_EQUAL(result, NT_STATUS_INVALID_WORKSTATION) ||
1821 NT_STATUS_EQUAL(result, NT_STATUS_LOGON_FAILURE) ||
1822 NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER) ||
1823 NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED) ||
1824 NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) ||
1825 NT_STATUS_EQUAL(result, NT_STATUS_WRONG_PASSWORD)) {
1829 if (state->request->flags & WBFLAG_PAM_FALLBACK_AFTER_KRB5) {
1830 DEBUG(3,("falling back to samlogon\n"));
1838 /* Check for Samlogon authentication */
1839 if (domain->online) {
1840 result = winbindd_dual_pam_auth_samlogon(
1841 state->mem_ctx, domain,
1842 state->request->data.auth.user,
1843 state->request->data.auth.pass,
1844 state->request->flags,
1847 if (NT_STATUS_IS_OK(result)) {
1848 DEBUG(10,("winbindd_dual_pam_auth_samlogon succeeded\n"));
1849 /* add the Krb5 err if we have one */
1850 if ( NT_STATUS_EQUAL(krb5_result, NT_STATUS_TIME_DIFFERENCE_AT_DC ) ) {
1851 info3->base.user_flags |= LOGON_KRB5_FAIL_CLOCK_SKEW;
1853 goto process_result;
1856 DEBUG(10,("winbindd_dual_pam_auth_samlogon failed: %s\n",
1857 nt_errstr(result)));
1859 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS) ||
1860 NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT) ||
1861 NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND))
1863 DEBUG(10,("winbindd_dual_pam_auth_samlogon setting domain to offline\n"));
1864 set_domain_offline( domain );
1868 if (domain->online) {
1869 /* We're still online - fail. */
1875 /* Check for Cached logons */
1876 if (!domain->online && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN) &&
1877 lp_winbind_offline_logon()) {
1879 result = winbindd_dual_pam_auth_cached(domain, state, &info3);
1881 if (NT_STATUS_IS_OK(result)) {
1882 DEBUG(10,("winbindd_dual_pam_auth_cached succeeded\n"));
1883 goto process_result;
1885 DEBUG(10,("winbindd_dual_pam_auth_cached failed: %s\n", nt_errstr(result)));
1892 if (NT_STATUS_IS_OK(result)) {
1894 struct dom_sid user_sid;
1896 /* In all codepaths where result == NT_STATUS_OK info3 must have
1897 been initialized. */
1899 result = NT_STATUS_INTERNAL_ERROR;
1903 sid_compose(&user_sid, info3->base.domain_sid,
1906 if (info3->base.full_name.string == NULL) {
1907 struct netr_SamInfo3 *cached_info3;
1909 cached_info3 = netsamlogon_cache_get(state->mem_ctx,
1911 if (cached_info3 != NULL &&
1912 cached_info3->base.full_name.string != NULL) {
1913 info3->base.full_name.string =
1914 talloc_strdup(info3,
1915 cached_info3->base.full_name.string);
1918 /* this might fail so we don't check the return code */
1919 wcache_query_user_fullname(domain,
1922 &info3->base.full_name.string);
1926 wcache_invalidate_samlogon(find_domain_from_name(name_domain),
1928 netsamlogon_cache_store(name_user, info3);
1930 /* save name_to_sid info as early as possible (only if
1931 this is our primary domain so we don't invalidate
1932 the cache entry by storing the seq_num for the wrong
1934 if ( domain->primary ) {
1935 cache_name2sid(domain, name_domain, name_user,
1936 SID_NAME_USER, &user_sid);
1939 /* Check if the user is in the right group */
1941 result = check_info3_in_group(
1943 state->request->data.auth.require_membership_of_sid);
1944 if (!NT_STATUS_IS_OK(result)) {
1945 DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
1946 state->request->data.auth.user,
1947 state->request->data.auth.require_membership_of_sid));
1951 result = append_auth_data(state->mem_ctx, state->response,
1952 state->request->flags, info3,
1953 name_domain, name_user);
1954 if (!NT_STATUS_IS_OK(result)) {
1958 if ((state->request->flags & WBFLAG_PAM_CACHED_LOGIN)
1959 && lp_winbind_offline_logon()) {
1961 result = winbindd_store_creds(domain,
1962 state->request->data.auth.user,
1963 state->request->data.auth.pass,
1967 if (state->request->flags & WBFLAG_PAM_GET_PWD_POLICY) {
1969 * WBFLAG_PAM_GET_PWD_POLICY is not used within
1970 * any Samba caller anymore.
1972 * We just fake this based on the effective values
1973 * for the user, for legacy callers.
1975 fake_password_policy(state->response, &info3->base);
1978 result = NT_STATUS_OK;
1982 /* give us a more useful (more correct?) error code */
1983 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
1984 (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
1985 result = NT_STATUS_NO_LOGON_SERVERS;
1988 set_auth_errors(state->response, result);
1990 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n",
1991 state->request->data.auth.user,
1992 state->response->data.auth.nt_status_string,
1993 state->response->data.auth.pam_error));
1995 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
1998 NTSTATUS winbind_dual_SamLogon(struct winbindd_domain *domain,
1999 TALLOC_CTX *mem_ctx,
2000 uint32_t logon_parameters,
2001 const char *name_user,
2002 const char *name_domain,
2003 const char *workstation,
2004 const uint8_t chal[8],
2005 DATA_BLOB lm_response,
2006 DATA_BLOB nt_response,
2007 uint8_t *authoritative,
2010 struct netr_SamInfo3 **info3)
2015 * We check against domain->name instead of
2016 * name_domain, as find_auth_domain() ->
2017 * find_domain_from_name_noinit() already decided
2018 * that we are in a child for the correct domain.
2020 * name_domain can also be lp_realm()
2021 * we need to check against domain->name.
2023 if (!skip_sam && strequal(domain->name, get_global_sam_name())) {
2024 DATA_BLOB chal_blob = data_blob_const(
2027 result = winbindd_dual_auth_passdb(
2030 name_domain, name_user,
2031 &chal_blob, &lm_response, &nt_response,
2032 false, /* interactive */
2037 * We need to try the remote NETLOGON server if this is
2038 * not authoritative.
2040 if (*authoritative != 0) {
2042 goto process_result;
2046 result = winbind_samlogon_retry_loop(domain,
2050 NULL, /* password */
2052 /* Bug #3248 - found by Stefan Burkei. */
2053 workstation, /* We carefully set this above so use it... */
2057 false, /* interactive */
2061 if (!NT_STATUS_IS_OK(result)) {
2067 if (NT_STATUS_IS_OK(result)) {
2068 struct dom_sid user_sid;
2070 sid_compose(&user_sid, (*info3)->base.domain_sid,
2071 (*info3)->base.rid);
2073 if ((*info3)->base.full_name.string == NULL) {
2074 struct netr_SamInfo3 *cached_info3;
2076 cached_info3 = netsamlogon_cache_get(mem_ctx,
2078 if (cached_info3 != NULL &&
2079 cached_info3->base.full_name.string != NULL) {
2080 (*info3)->base.full_name.string =
2081 talloc_strdup(*info3,
2082 cached_info3->base.full_name.string);
2085 /* this might fail so we don't check the return code */
2086 wcache_query_user_fullname(domain,
2089 &(*info3)->base.full_name.string);
2093 wcache_invalidate_samlogon(find_domain_from_name(name_domain),
2095 netsamlogon_cache_store(name_user, *info3);
2100 /* give us a more useful (more correct?) error code */
2101 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
2102 (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
2103 result = NT_STATUS_NO_LOGON_SERVERS;
2106 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2107 ("NTLM CRAP authentication for user [%s]\\[%s] returned %s\n",
2110 nt_errstr(result)));
2115 enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
2116 struct winbindd_cli_state *state)
2119 struct netr_SamInfo3 *info3 = NULL;
2120 const char *name_user = NULL;
2121 const char *name_domain = NULL;
2122 const char *workstation;
2123 uint8_t authoritative = 0;
2126 DATA_BLOB lm_resp, nt_resp;
2128 /* This is child-only, so no check for privileged access is needed
2131 /* Ensure null termination */
2132 state->request->data.auth_crap.user[sizeof(state->request->data.auth_crap.user)-1]=0;
2133 state->request->data.auth_crap.domain[sizeof(state->request->data.auth_crap.domain)-1]=0;
2135 name_user = state->request->data.auth_crap.user;
2136 name_domain = state->request->data.auth_crap.domain;
2137 workstation = state->request->data.auth_crap.workstation;
2139 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state->pid,
2140 name_domain, name_user));
2142 if (state->request->data.auth_crap.lm_resp_len > sizeof(state->request->data.auth_crap.lm_resp)
2143 || state->request->data.auth_crap.nt_resp_len > sizeof(state->request->data.auth_crap.nt_resp)) {
2144 if (!(state->request->flags & WBFLAG_BIG_NTLMV2_BLOB) ||
2145 state->request->extra_len != state->request->data.auth_crap.nt_resp_len) {
2146 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n",
2147 state->request->data.auth_crap.lm_resp_len,
2148 state->request->data.auth_crap.nt_resp_len));
2149 result = NT_STATUS_INVALID_PARAMETER;
2154 lm_resp = data_blob_talloc(state->mem_ctx, state->request->data.auth_crap.lm_resp,
2155 state->request->data.auth_crap.lm_resp_len);
2157 if (state->request->flags & WBFLAG_BIG_NTLMV2_BLOB) {
2158 nt_resp = data_blob_talloc(state->mem_ctx,
2159 state->request->extra_data.data,
2160 state->request->data.auth_crap.nt_resp_len);
2162 nt_resp = data_blob_talloc(state->mem_ctx,
2163 state->request->data.auth_crap.nt_resp,
2164 state->request->data.auth_crap.nt_resp_len);
2167 result = winbind_dual_SamLogon(domain,
2169 state->request->data.auth_crap.logon_parameters,
2172 /* Bug #3248 - found by Stefan Burkei. */
2173 workstation, /* We carefully set this above so use it... */
2174 state->request->data.auth_crap.chal,
2181 if (!NT_STATUS_IS_OK(result)) {
2182 state->response->data.auth.authoritative = authoritative;
2186 if (NT_STATUS_IS_OK(result)) {
2187 /* Check if the user is in the right group */
2189 result = check_info3_in_group(
2191 state->request->data.auth_crap.require_membership_of_sid);
2192 if (!NT_STATUS_IS_OK(result)) {
2193 DEBUG(3, ("User %s is not in the required group (%s), so "
2194 "crap authentication is rejected\n",
2195 state->request->data.auth_crap.user,
2196 state->request->data.auth_crap.require_membership_of_sid));
2200 result = append_auth_data(state->mem_ctx, state->response,
2201 state->request->flags, info3,
2202 name_domain, name_user);
2203 if (!NT_STATUS_IS_OK(result)) {
2210 if (state->request->flags & WBFLAG_PAM_NT_STATUS_SQUASH) {
2211 result = nt_status_squash(result);
2214 set_auth_errors(state->response, result);
2216 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2219 enum winbindd_result winbindd_dual_pam_chauthtok(struct winbindd_domain *contact_domain,
2220 struct winbindd_cli_state *state)
2223 char *newpass = NULL;
2224 struct policy_handle dom_pol;
2225 struct rpc_pipe_client *cli = NULL;
2226 bool got_info = false;
2227 struct samr_DomInfo1 *info = NULL;
2228 struct userPwdChangeFailureInformation *reject = NULL;
2229 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2230 fstring domain, user;
2231 struct dcerpc_binding_handle *b = NULL;
2233 ZERO_STRUCT(dom_pol);
2235 DEBUG(3, ("[%5lu]: dual pam chauthtok %s\n", (unsigned long)state->pid,
2236 state->request->data.auth.user));
2238 if (!parse_domain_user(state->request->data.chauthtok.user, domain, user)) {
2242 /* Change password */
2244 oldpass = state->request->data.chauthtok.oldpass;
2245 newpass = state->request->data.chauthtok.newpass;
2247 /* Initialize reject reason */
2248 state->response->data.auth.reject_reason = Undefined;
2250 /* Get sam handle */
2252 result = cm_connect_sam(contact_domain, state->mem_ctx, true, &cli,
2254 if (!NT_STATUS_IS_OK(result)) {
2255 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
2259 b = cli->binding_handle;
2261 result = rpccli_samr_chgpasswd_user3(cli, state->mem_ctx,
2268 /* Windows 2003 returns NT_STATUS_PASSWORD_RESTRICTION */
2270 if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION) ) {
2272 fill_in_password_policy(state->response, info);
2274 state->response->data.auth.reject_reason =
2275 reject->extendedFailureReason;
2280 /* atm the pidl generated rpccli_samr_ChangePasswordUser3 function will
2281 * return with NT_STATUS_BUFFER_TOO_SMALL for w2k dcs as w2k just
2282 * returns with 4byte error code (NT_STATUS_NOT_SUPPORTED) which is too
2283 * short to comply with the samr_ChangePasswordUser3 idl - gd */
2285 /* only fallback when the chgpasswd_user3 call is not supported */
2286 if (NT_STATUS_EQUAL(result, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE) ||
2287 NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED) ||
2288 NT_STATUS_EQUAL(result, NT_STATUS_BUFFER_TOO_SMALL) ||
2289 NT_STATUS_EQUAL(result, NT_STATUS_NOT_IMPLEMENTED)) {
2291 DEBUG(10,("Password change with chgpasswd_user3 failed with: %s, retrying chgpasswd_user2\n",
2292 nt_errstr(result)));
2294 result = rpccli_samr_chgpasswd_user2(cli, state->mem_ctx, user, newpass, oldpass);
2296 /* Windows 2000 returns NT_STATUS_ACCOUNT_RESTRICTION.
2297 Map to the same status code as Windows 2003. */
2299 if ( NT_STATUS_EQUAL(NT_STATUS_ACCOUNT_RESTRICTION, result ) ) {
2300 result = NT_STATUS_PASSWORD_RESTRICTION;
2306 if (NT_STATUS_IS_OK(result)
2307 && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN)
2308 && lp_winbind_offline_logon()) {
2309 result = winbindd_update_creds_by_name(contact_domain, user,
2311 /* Again, this happens when we login from gdm or xdm
2312 * and the password expires, *BUT* cached crendentials
2313 * doesn't exist. winbindd_update_creds_by_name()
2314 * returns NT_STATUS_NO_SUCH_USER.
2315 * This is not a failure.
2318 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER)) {
2319 result = NT_STATUS_OK;
2322 if (!NT_STATUS_IS_OK(result)) {
2323 DEBUG(10, ("Failed to store creds: %s\n",
2324 nt_errstr(result)));
2325 goto process_result;
2329 if (!NT_STATUS_IS_OK(result) && !got_info && contact_domain) {
2331 NTSTATUS policy_ret;
2333 policy_ret = fillup_password_policy(
2334 contact_domain, state->response);
2336 /* failure of this is non critical, it will just provide no
2337 * additional information to the client why the change has
2338 * failed - Guenther */
2340 if (!NT_STATUS_IS_OK(policy_ret)) {
2341 DEBUG(10,("Failed to get password policies: %s\n", nt_errstr(policy_ret)));
2342 goto process_result;
2348 if (strequal(contact_domain->name, get_global_sam_name())) {
2349 /* FIXME: internal rpc pipe does not cache handles yet */
2351 if (is_valid_policy_hnd(&dom_pol)) {
2353 dcerpc_samr_Close(b, state->mem_ctx, &dom_pol, &_result);
2359 set_auth_errors(state->response, result);
2361 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2362 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
2365 state->response->data.auth.nt_status_string,
2366 state->response->data.auth.pam_error));
2368 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2371 enum winbindd_result winbindd_dual_pam_logoff(struct winbindd_domain *domain,
2372 struct winbindd_cli_state *state)
2374 NTSTATUS result = NT_STATUS_NOT_SUPPORTED;
2376 DEBUG(3, ("[%5lu]: pam dual logoff %s\n", (unsigned long)state->pid,
2377 state->request->data.logoff.user));
2379 if (!(state->request->flags & WBFLAG_PAM_KRB5)) {
2380 result = NT_STATUS_OK;
2381 goto process_result;
2384 if (state->request->data.logoff.krb5ccname[0] == '\0') {
2385 result = NT_STATUS_OK;
2386 goto process_result;
2391 if (state->request->data.logoff.uid == (uid_t)-1) {
2392 DEBUG(0,("winbindd_pam_logoff: invalid uid\n"));
2393 goto process_result;
2396 /* what we need here is to find the corresponding krb5 ccache name *we*
2397 * created for a given username and destroy it */
2399 if (!ccache_entry_exists(state->request->data.logoff.user)) {
2400 result = NT_STATUS_OK;
2401 DEBUG(10,("winbindd_pam_logoff: no entry found.\n"));
2402 goto process_result;
2405 if (!ccache_entry_identical(state->request->data.logoff.user,
2406 state->request->data.logoff.uid,
2407 state->request->data.logoff.krb5ccname)) {
2408 DEBUG(0,("winbindd_pam_logoff: cached entry differs.\n"));
2409 goto process_result;
2412 result = remove_ccache(state->request->data.logoff.user);
2413 if (!NT_STATUS_IS_OK(result)) {
2414 DEBUG(0,("winbindd_pam_logoff: failed to remove ccache: %s\n",
2415 nt_errstr(result)));
2416 goto process_result;
2420 * Remove any mlock'ed memory creds in the child
2421 * we might be using for krb5 ticket renewal.
2424 winbindd_delete_memory_creds(state->request->data.logoff.user);
2427 result = NT_STATUS_NOT_SUPPORTED;
2433 set_auth_errors(state->response, result);
2435 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2438 /* Change user password with auth crap*/
2440 enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domain *domainSt, struct winbindd_cli_state *state)
2443 DATA_BLOB new_nt_password;
2444 DATA_BLOB old_nt_hash_enc;
2445 DATA_BLOB new_lm_password;
2446 DATA_BLOB old_lm_hash_enc;
2447 fstring domain,user;
2448 struct policy_handle dom_pol;
2449 struct winbindd_domain *contact_domain = domainSt;
2450 struct rpc_pipe_client *cli = NULL;
2451 struct dcerpc_binding_handle *b = NULL;
2453 ZERO_STRUCT(dom_pol);
2455 /* Ensure null termination */
2456 state->request->data.chng_pswd_auth_crap.user[
2457 sizeof(state->request->data.chng_pswd_auth_crap.user)-1]=0;
2458 state->request->data.chng_pswd_auth_crap.domain[
2459 sizeof(state->request->data.chng_pswd_auth_crap.domain)-1]=0;
2463 DEBUG(3, ("[%5lu]: pam change pswd auth crap domain: %s user: %s\n",
2464 (unsigned long)state->pid,
2465 state->request->data.chng_pswd_auth_crap.domain,
2466 state->request->data.chng_pswd_auth_crap.user));
2468 if (lp_winbind_offline_logon()) {
2469 DEBUG(0,("Refusing password change as winbind offline logons are enabled. "));
2470 DEBUGADD(0,("Changing passwords here would risk inconsistent logons\n"));
2471 result = NT_STATUS_ACCESS_DENIED;
2475 if (*state->request->data.chng_pswd_auth_crap.domain) {
2476 fstrcpy(domain,state->request->data.chng_pswd_auth_crap.domain);
2478 parse_domain_user(state->request->data.chng_pswd_auth_crap.user,
2482 DEBUG(3,("no domain specified with username (%s) - "
2484 state->request->data.chng_pswd_auth_crap.user));
2485 result = NT_STATUS_NO_SUCH_USER;
2490 if (!*domain && lp_winbind_use_default_domain()) {
2491 fstrcpy(domain,lp_workgroup());
2495 fstrcpy(user, state->request->data.chng_pswd_auth_crap.user);
2498 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n",
2499 (unsigned long)state->pid, domain, user));
2501 /* Change password */
2502 new_nt_password = data_blob_const(
2503 state->request->data.chng_pswd_auth_crap.new_nt_pswd,
2504 state->request->data.chng_pswd_auth_crap.new_nt_pswd_len);
2506 old_nt_hash_enc = data_blob_const(
2507 state->request->data.chng_pswd_auth_crap.old_nt_hash_enc,
2508 state->request->data.chng_pswd_auth_crap.old_nt_hash_enc_len);
2510 if(state->request->data.chng_pswd_auth_crap.new_lm_pswd_len > 0) {
2511 new_lm_password = data_blob_const(
2512 state->request->data.chng_pswd_auth_crap.new_lm_pswd,
2513 state->request->data.chng_pswd_auth_crap.new_lm_pswd_len);
2515 old_lm_hash_enc = data_blob_const(
2516 state->request->data.chng_pswd_auth_crap.old_lm_hash_enc,
2517 state->request->data.chng_pswd_auth_crap.old_lm_hash_enc_len);
2519 new_lm_password = data_blob_null;
2520 old_lm_hash_enc = data_blob_null;
2523 /* Get sam handle */
2525 result = cm_connect_sam(contact_domain, state->mem_ctx, true, &cli, &dom_pol);
2526 if (!NT_STATUS_IS_OK(result)) {
2527 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
2531 b = cli->binding_handle;
2533 result = rpccli_samr_chng_pswd_auth_crap(
2534 cli, state->mem_ctx, user, new_nt_password, old_nt_hash_enc,
2535 new_lm_password, old_lm_hash_enc);
2539 if (strequal(contact_domain->name, get_global_sam_name())) {
2540 /* FIXME: internal rpc pipe does not cache handles yet */
2542 if (is_valid_policy_hnd(&dom_pol)) {
2544 dcerpc_samr_Close(b, state->mem_ctx, &dom_pol, &_result);
2550 set_auth_errors(state->response, result);
2552 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2553 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
2555 state->response->data.auth.nt_status_string,
2556 state->response->data.auth.pam_error));
2558 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2562 static NTSTATUS extract_pac_vrfy_sigs(TALLOC_CTX *mem_ctx, DATA_BLOB pac_blob,
2563 struct PAC_LOGON_INFO **logon_info)
2565 krb5_context krbctx = NULL;
2566 krb5_error_code k5ret;
2568 krb5_kt_cursor cursor;
2569 krb5_keytab_entry entry;
2570 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2573 ZERO_STRUCT(cursor);
2575 k5ret = krb5_init_context(&krbctx);
2577 DEBUG(1, ("Failed to initialize kerberos context: %s\n",
2578 error_message(k5ret)));
2579 status = krb5_to_nt_status(k5ret);
2583 k5ret = gse_krb5_get_server_keytab(krbctx, &keytab);
2585 DEBUG(1, ("Failed to get keytab: %s\n",
2586 error_message(k5ret)));
2587 status = krb5_to_nt_status(k5ret);
2591 k5ret = krb5_kt_start_seq_get(krbctx, keytab, &cursor);
2593 DEBUG(1, ("Failed to start seq: %s\n",
2594 error_message(k5ret)));
2595 status = krb5_to_nt_status(k5ret);
2599 k5ret = krb5_kt_next_entry(krbctx, keytab, &entry, &cursor);
2600 while (k5ret == 0) {
2601 status = kerberos_pac_logon_info(mem_ctx, pac_blob,
2603 KRB5_KT_KEY(&entry), NULL, 0,
2605 if (NT_STATUS_IS_OK(status)) {
2608 k5ret = smb_krb5_kt_free_entry(krbctx, &entry);
2609 k5ret = krb5_kt_next_entry(krbctx, keytab, &entry, &cursor);
2612 k5ret = krb5_kt_end_seq_get(krbctx, keytab, &cursor);
2614 DEBUG(1, ("Failed to end seq: %s\n",
2615 error_message(k5ret)));
2618 k5ret = krb5_kt_close(krbctx, keytab);
2620 DEBUG(1, ("Failed to close keytab: %s\n",
2621 error_message(k5ret)));
2624 krb5_free_context(krbctx);
2629 NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
2630 struct netr_SamInfo3 **info3)
2632 struct winbindd_request *req = state->request;
2634 struct PAC_LOGON_INFO *logon_info = NULL;
2635 struct netr_SamInfo3 *info3_copy = NULL;
2638 pac_blob = data_blob_const(req->extra_data.data, req->extra_len);
2639 result = extract_pac_vrfy_sigs(state->mem_ctx, pac_blob, &logon_info);
2640 if (!NT_STATUS_IS_OK(result) &&
2641 !NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
2642 DEBUG(1, ("Error during PAC signature verification: %s\n",
2643 nt_errstr(result)));
2649 * Signature verification succeeded, we can
2650 * trust the PAC and prime the netsamlogon
2651 * and name2sid caches. DO NOT DO THIS
2652 * in the signature verification failed
2655 struct winbindd_domain *domain = NULL;
2657 result = create_info3_from_pac_logon_info(state->mem_ctx,
2660 if (!NT_STATUS_IS_OK(result)) {
2663 netsamlogon_cache_store(NULL, info3_copy);
2666 * We're in the parent here, so find the child
2667 * pointer from the PAC domain name.
2669 domain = find_lookup_domain_from_name(
2670 info3_copy->base.logon_domain.string);
2671 if (domain && domain->primary ) {
2672 struct dom_sid user_sid;
2674 sid_compose(&user_sid,
2675 info3_copy->base.domain_sid,
2676 info3_copy->base.rid);
2678 cache_name2sid_trusted(domain,
2679 info3_copy->base.logon_domain.string,
2680 info3_copy->base.account_name.string,
2684 DBG_INFO("PAC for user %s\\%s SID %s primed cache\n",
2685 info3_copy->base.logon_domain.string,
2686 info3_copy->base.account_name.string,
2687 sid_string_dbg(&user_sid));
2691 /* Try without signature verification */
2692 result = kerberos_pac_logon_info(state->mem_ctx, pac_blob, NULL,
2693 NULL, NULL, NULL, 0,
2695 if (!NT_STATUS_IS_OK(result)) {
2696 DEBUG(10, ("Could not extract PAC: %s\n",
2697 nt_errstr(result)));
2702 * Don't strictly need to copy here,
2703 * but it makes it explicit we're
2704 * returning a copy talloc'ed off
2705 * the state->mem_ctx.
2707 info3_copy = copy_netr_SamInfo3(state->mem_ctx,
2708 &logon_info->info3);
2709 if (info3_copy == NULL) {
2710 return NT_STATUS_NO_MEMORY;
2715 *info3 = info3_copy;
2717 return NT_STATUS_OK;
2719 #else /* HAVE_KRB5 */
2720 NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
2721 struct netr_SamInfo3 **info3)
2723 return NT_STATUS_NO_SUCH_USER;
2725 #endif /* HAVE_KRB5 */