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"
46 #include "rpc_client/util_netlogon.h"
49 #define DBGC_CLASS DBGC_WINBIND
51 #define LOGON_KRB5_FAIL_CLOCK_SKEW 0x02000000
53 static NTSTATUS append_info3_as_txt(TALLOC_CTX *mem_ctx,
54 struct winbindd_response *resp,
55 uint16_t validation_level,
56 union netr_Validation *validation)
58 struct netr_SamInfo3 *info3 = NULL;
61 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
62 TALLOC_CTX *frame = talloc_stackframe();
64 status = map_validation_to_info3(frame,
68 if (!NT_STATUS_IS_OK(status)) {
72 resp->data.auth.info3.logon_time =
73 nt_time_to_unix(info3->base.logon_time);
74 resp->data.auth.info3.logoff_time =
75 nt_time_to_unix(info3->base.logoff_time);
76 resp->data.auth.info3.kickoff_time =
77 nt_time_to_unix(info3->base.kickoff_time);
78 resp->data.auth.info3.pass_last_set_time =
79 nt_time_to_unix(info3->base.last_password_change);
80 resp->data.auth.info3.pass_can_change_time =
81 nt_time_to_unix(info3->base.allow_password_change);
82 resp->data.auth.info3.pass_must_change_time =
83 nt_time_to_unix(info3->base.force_password_change);
85 resp->data.auth.info3.logon_count = info3->base.logon_count;
86 resp->data.auth.info3.bad_pw_count = info3->base.bad_password_count;
88 resp->data.auth.info3.user_rid = info3->base.rid;
89 resp->data.auth.info3.group_rid = info3->base.primary_gid;
90 sid_to_fstring(resp->data.auth.info3.dom_sid, info3->base.domain_sid);
92 resp->data.auth.info3.num_groups = info3->base.groups.count;
93 resp->data.auth.info3.user_flgs = info3->base.user_flags;
95 resp->data.auth.info3.acct_flags = info3->base.acct_flags;
96 resp->data.auth.info3.num_other_sids = info3->sidcount;
98 fstrcpy(resp->data.auth.info3.user_name,
99 info3->base.account_name.string);
100 fstrcpy(resp->data.auth.info3.full_name,
101 info3->base.full_name.string);
102 fstrcpy(resp->data.auth.info3.logon_script,
103 info3->base.logon_script.string);
104 fstrcpy(resp->data.auth.info3.profile_path,
105 info3->base.profile_path.string);
106 fstrcpy(resp->data.auth.info3.home_dir,
107 info3->base.home_directory.string);
108 fstrcpy(resp->data.auth.info3.dir_drive,
109 info3->base.home_drive.string);
111 fstrcpy(resp->data.auth.info3.logon_srv,
112 info3->base.logon_server.string);
113 fstrcpy(resp->data.auth.info3.logon_dom,
114 info3->base.logon_domain.string);
116 resp->data.auth.validation_level = validation_level;
117 if (validation_level == 6) {
118 fstrcpy(resp->data.auth.info6.dns_domainname,
119 validation->sam6->dns_domainname.string);
120 fstrcpy(resp->data.auth.info6.principal_name,
121 validation->sam6->principal_name.string);
124 ex = talloc_strdup(frame, "");
126 status = NT_STATUS_NO_MEMORY;
130 for (i=0; i < info3->base.groups.count; i++) {
131 ex = talloc_asprintf_append_buffer(ex, "0x%08X:0x%08X\n",
132 info3->base.groups.rids[i].rid,
133 info3->base.groups.rids[i].attributes);
135 status = NT_STATUS_NO_MEMORY;
140 for (i=0; i < info3->sidcount; i++) {
143 sid = dom_sid_string(frame, info3->sids[i].sid);
145 status = NT_STATUS_NO_MEMORY;
149 ex = talloc_asprintf_append_buffer(ex, "%s:0x%08X\n",
151 info3->sids[i].attributes);
153 status = NT_STATUS_NO_MEMORY;
158 resp->length += talloc_get_size(ex);
159 resp->extra_data.data = talloc_move(mem_ctx, &ex);
161 status = NT_STATUS_OK;
167 static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx,
168 struct winbindd_response *resp,
169 struct netr_SamInfo3 *info3)
172 enum ndr_err_code ndr_err;
174 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, info3,
175 (ndr_push_flags_fn_t)ndr_push_netr_SamInfo3);
176 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
177 DEBUG(0,("append_info3_as_ndr: failed to append\n"));
178 return ndr_map_error2ntstatus(ndr_err);
181 resp->extra_data.data = blob.data;
182 resp->length += blob.length;
187 static NTSTATUS append_unix_username(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 /* We've been asked to return the unix username, per
194 'winbind use default domain' settings and the like */
196 const char *nt_username, *nt_domain;
198 nt_domain = talloc_strdup(mem_ctx, info3->base.logon_domain.string);
200 /* If the server didn't give us one, just use the one
202 nt_domain = name_domain;
205 nt_username = talloc_strdup(mem_ctx, info3->base.account_name.string);
207 /* If the server didn't give us one, just use the one
209 nt_username = name_user;
212 fill_domain_username(resp->data.auth.unix_username,
213 nt_domain, nt_username, true);
215 DEBUG(5, ("Setting unix username to [%s]\n",
216 resp->data.auth.unix_username));
221 static NTSTATUS append_afs_token(TALLOC_CTX *mem_ctx,
222 struct winbindd_response *resp,
223 const struct netr_SamInfo3 *info3,
224 const char *name_domain,
225 const char *name_user)
227 char *afsname = NULL;
231 afsname = talloc_strdup(mem_ctx, lp_afs_username_map());
232 if (afsname == NULL) {
233 return NT_STATUS_NO_MEMORY;
236 afsname = talloc_string_sub(mem_ctx,
237 lp_afs_username_map(),
239 afsname = talloc_string_sub(mem_ctx, afsname,
241 afsname = talloc_string_sub(mem_ctx, afsname,
245 struct dom_sid user_sid;
248 sid_compose(&user_sid, info3->base.domain_sid,
250 sid_to_fstring(sidstr, &user_sid);
251 afsname = talloc_string_sub(mem_ctx, afsname,
255 if (afsname == NULL) {
256 return NT_STATUS_NO_MEMORY;
259 if (!strlower_m(afsname)) {
260 return NT_STATUS_INVALID_PARAMETER;
263 DEBUG(10, ("Generating token for user %s\n", afsname));
265 cell = strchr(afsname, '@');
268 return NT_STATUS_NO_MEMORY;
274 token = afs_createtoken_str(afsname, cell);
278 resp->extra_data.data = talloc_strdup(mem_ctx, token);
279 if (resp->extra_data.data == NULL) {
280 return NT_STATUS_NO_MEMORY;
282 resp->length += strlen((const char *)resp->extra_data.data)+1;
287 static NTSTATUS check_info3_in_group(struct netr_SamInfo3 *info3,
288 const char *group_sid)
290 * Check whether a user belongs to a group or list of groups.
292 * @param mem_ctx talloc memory context.
293 * @param info3 user information, including group membership info.
294 * @param group_sid One or more groups , separated by commas.
296 * @return NT_STATUS_OK on success,
297 * NT_STATUS_LOGON_FAILURE if the user does not belong,
298 * or other NT_STATUS_IS_ERR(status) for other kinds of failure.
301 struct dom_sid *require_membership_of_sid;
302 uint32_t num_require_membership_of_sid;
307 struct security_token *token;
308 TALLOC_CTX *frame = talloc_stackframe();
311 /* Parse the 'required group' SID */
313 if (!group_sid || !group_sid[0]) {
314 /* NO sid supplied, all users may access */
319 token = talloc_zero(talloc_tos(), struct security_token);
321 DEBUG(0, ("talloc failed\n"));
323 return NT_STATUS_NO_MEMORY;
326 num_require_membership_of_sid = 0;
327 require_membership_of_sid = NULL;
331 while (next_token_talloc(talloc_tos(), &p, &req_sid, ",")) {
332 if (!string_to_sid(&sid, req_sid)) {
333 DEBUG(0, ("check_info3_in_group: could not parse %s "
334 "as a SID!", req_sid));
336 return NT_STATUS_INVALID_PARAMETER;
339 status = add_sid_to_array(talloc_tos(), &sid,
340 &require_membership_of_sid,
341 &num_require_membership_of_sid);
342 if (!NT_STATUS_IS_OK(status)) {
343 DEBUG(0, ("add_sid_to_array failed\n"));
349 status = sid_array_from_info3(talloc_tos(), info3,
353 if (!NT_STATUS_IS_OK(status)) {
358 if (!NT_STATUS_IS_OK(status = add_aliases(get_global_sam_sid(),
360 || !NT_STATUS_IS_OK(status = add_aliases(&global_sid_Builtin,
362 DEBUG(3, ("could not add aliases: %s\n",
368 security_token_debug(DBGC_CLASS, 10, token);
370 for (i=0; i<num_require_membership_of_sid; i++) {
371 DEBUG(10, ("Checking SID %s\n", sid_string_dbg(
372 &require_membership_of_sid[i])));
373 if (nt_token_check_sid(&require_membership_of_sid[i],
375 DEBUG(10, ("Access ok\n"));
381 /* Do not distinguish this error from a wrong username/pw */
384 return NT_STATUS_LOGON_FAILURE;
387 struct winbindd_domain *find_auth_domain(uint8_t flags,
388 const char *domain_name)
390 struct winbindd_domain *domain;
393 domain = find_domain_from_name_noinit(domain_name);
394 if (domain == NULL) {
395 DEBUG(3, ("Authentication for domain [%s] refused "
396 "as it is not a trusted domain\n",
401 if (domain->secure_channel_type != SEC_CHAN_NULL) {
405 return domain->routing_domain;
408 if (strequal(domain_name, get_global_sam_name())) {
409 return find_domain_from_name_noinit(domain_name);
412 /* we can auth against trusted domains */
413 if (flags & WBFLAG_PAM_CONTACT_TRUSTDOM) {
414 domain = find_domain_from_name_noinit(domain_name);
415 if (domain == NULL) {
416 DEBUG(3, ("Authentication for domain [%s] skipped "
417 "as it is not a trusted domain\n",
424 return find_our_domain();
427 static void fake_password_policy(struct winbindd_response *r,
428 const struct netr_SamBaseInfo *bi)
430 NTTIME min_password_age;
431 NTTIME max_password_age;
433 if (bi->allow_password_change > bi->last_password_change) {
434 min_password_age = bi->allow_password_change -
435 bi->last_password_change;
437 min_password_age = 0;
440 if (bi->force_password_change > bi->last_password_change) {
441 max_password_age = bi->force_password_change -
442 bi->last_password_change;
444 max_password_age = 0;
447 r->data.auth.policy.min_length_password = 0;
448 r->data.auth.policy.password_history = 0;
449 r->data.auth.policy.password_properties = 0;
450 r->data.auth.policy.expire =
451 nt_time_to_unix_abs(&max_password_age);
452 r->data.auth.policy.min_passwordage =
453 nt_time_to_unix_abs(&min_password_age);
456 static void fill_in_password_policy(struct winbindd_response *r,
457 const struct samr_DomInfo1 *p)
459 r->data.auth.policy.min_length_password =
460 p->min_password_length;
461 r->data.auth.policy.password_history =
462 p->password_history_length;
463 r->data.auth.policy.password_properties =
464 p->password_properties;
465 r->data.auth.policy.expire =
466 nt_time_to_unix_abs((const NTTIME *)&(p->max_password_age));
467 r->data.auth.policy.min_passwordage =
468 nt_time_to_unix_abs((const NTTIME *)&(p->min_password_age));
471 static NTSTATUS fillup_password_policy(struct winbindd_domain *domain,
472 struct winbindd_response *response)
474 TALLOC_CTX *frame = talloc_stackframe();
476 struct samr_DomInfo1 password_policy;
478 if ( !winbindd_can_contact_domain( domain ) ) {
479 DEBUG(5,("fillup_password_policy: No inbound trust to "
480 "contact domain %s\n", domain->name));
481 status = NT_STATUS_NOT_SUPPORTED;
485 status = wb_cache_password_policy(domain, talloc_tos(),
487 if (NT_STATUS_IS_ERR(status)) {
491 fill_in_password_policy(response, &password_policy);
498 static NTSTATUS get_max_bad_attempts_from_lockout_policy(struct winbindd_domain *domain,
500 uint16_t *lockout_threshold)
502 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
503 struct samr_DomInfo12 lockout_policy;
505 *lockout_threshold = 0;
507 status = wb_cache_lockout_policy(domain, mem_ctx, &lockout_policy);
508 if (NT_STATUS_IS_ERR(status)) {
512 *lockout_threshold = lockout_policy.lockout_threshold;
517 static NTSTATUS get_pwd_properties(struct winbindd_domain *domain,
519 uint32_t *password_properties)
521 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
522 struct samr_DomInfo1 password_policy;
524 *password_properties = 0;
526 status = wb_cache_password_policy(domain, mem_ctx, &password_policy);
527 if (NT_STATUS_IS_ERR(status)) {
531 *password_properties = password_policy.password_properties;
538 static const char *generate_krb5_ccache(TALLOC_CTX *mem_ctx,
541 const char **user_ccache_file)
543 /* accept FILE and WRFILE as krb5_cc_type from the client and then
544 * build the full ccname string based on the user's uid here -
547 const char *gen_cc = NULL;
550 if (strequal(type, "FILE")) {
551 gen_cc = talloc_asprintf(
552 mem_ctx, "FILE:/tmp/krb5cc_%d", uid);
554 if (strequal(type, "WRFILE")) {
555 gen_cc = talloc_asprintf(
556 mem_ctx, "WRFILE:/tmp/krb5cc_%d", uid);
558 if (strequal(type, "KEYRING")) {
559 gen_cc = talloc_asprintf(
560 mem_ctx, "KEYRING:persistent:%d", uid);
563 if (strnequal(type, "FILE:/", 6) ||
564 strnequal(type, "WRFILE:/", 8) ||
565 strnequal(type, "DIR:/", 5)) {
567 /* we allow only one "%u" substitution */
571 p = strchr(type, '%');
576 if (p != NULL && *p == 'u' && strchr(p, '%') == NULL) {
577 char uid_str[sizeof("18446744073709551615")];
579 snprintf(uid_str, sizeof(uid_str), "%u", uid);
581 gen_cc = talloc_string_sub2(mem_ctx,
585 /* remove_unsafe_characters */
589 /* allow_trailing_dollar */
596 *user_ccache_file = gen_cc;
598 if (gen_cc == NULL) {
599 gen_cc = talloc_strdup(mem_ctx, "MEMORY:winbindd_pam_ccache");
601 if (gen_cc == NULL) {
602 DEBUG(0,("out of memory\n"));
606 DEBUG(10, ("using ccache: %s%s\n", gen_cc,
607 (*user_ccache_file == NULL) ? " (internal)":""));
614 uid_t get_uid_from_request(struct winbindd_request *request)
618 uid = request->data.auth.uid;
620 if (uid == (uid_t)-1) {
621 DEBUG(1,("invalid uid: '%u'\n", (unsigned int)uid));
627 /**********************************************************************
628 Authenticate a user with a clear text password using Kerberos and fill up
630 **********************************************************************/
632 static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
633 struct winbindd_domain *domain,
636 const char *krb5_cc_type,
638 struct netr_SamInfo6 **info6,
642 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
643 krb5_error_code krb5_ret;
644 const char *cc = NULL;
645 const char *principal_s = NULL;
646 const char *service = NULL;
648 fstring name_domain, name_user;
649 time_t ticket_lifetime = 0;
650 time_t renewal_until = 0;
652 time_t time_offset = 0;
653 const char *user_ccache_file;
654 struct PAC_LOGON_INFO *logon_info = NULL;
655 struct PAC_UPN_DNS_INFO *upn_dns_info = NULL;
656 struct PAC_DATA *pac_data = NULL;
657 struct PAC_DATA_CTR *pac_data_ctr = NULL;
658 const char *local_service;
660 struct netr_SamInfo6 *info6_copy = NULL;
664 if (domain->alt_name == NULL) {
665 return NT_STATUS_INVALID_PARAMETER;
669 * prepare a krb5_cc_cache string for the user */
672 DEBUG(0,("no valid uid\n"));
675 cc = generate_krb5_ccache(mem_ctx,
680 return NT_STATUS_NO_MEMORY;
685 * get kerberos properties */
687 if (domain->private_data) {
688 ads = (ADS_STRUCT *)domain->private_data;
689 time_offset = ads->auth.time_offset;
694 * do kerberos auth and setup ccache as the user */
696 parse_domain_user(user, name_domain, name_user);
698 realm = talloc_strdup(mem_ctx, domain->alt_name);
700 return NT_STATUS_NO_MEMORY;
703 if (!strupper_m(realm)) {
704 return NT_STATUS_INVALID_PARAMETER;
707 principal_s = talloc_asprintf(mem_ctx, "%s@%s", name_user, realm);
708 if (principal_s == NULL) {
709 return NT_STATUS_NO_MEMORY;
712 service = talloc_asprintf(mem_ctx, "%s/%s@%s", KRB5_TGS_NAME, realm, realm);
713 if (service == NULL) {
714 return NT_STATUS_NO_MEMORY;
717 local_service = talloc_asprintf(mem_ctx, "%s$@%s",
718 lp_netbios_name(), lp_realm());
719 if (local_service == NULL) {
720 return NT_STATUS_NO_MEMORY;
724 /* if this is a user ccache, we need to act as the user to let the krb5
725 * library handle the chown, etc. */
727 /************************ ENTERING NON-ROOT **********************/
729 if (user_ccache_file != NULL) {
730 set_effective_uid(uid);
731 DEBUG(10,("winbindd_raw_kerberos_login: uid is %d\n", uid));
734 result = kerberos_return_pac(mem_ctx,
743 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
747 if (user_ccache_file != NULL) {
748 gain_root_privilege();
751 /************************ RETURNED TO ROOT **********************/
753 if (!NT_STATUS_IS_OK(result)) {
757 if (pac_data_ctr == NULL) {
761 pac_data = pac_data_ctr->pac_data;
762 if (pac_data == NULL) {
766 for (i=0; i < pac_data->num_buffers; i++) {
768 if (pac_data->buffers[i].type == PAC_TYPE_LOGON_INFO) {
769 logon_info = pac_data->buffers[i].info->logon_info.info;
773 if (pac_data->buffers[i].type == PAC_TYPE_UPN_DNS_INFO) {
774 upn_dns_info = &pac_data->buffers[i].info->upn_dns_info;
779 if (logon_info == NULL) {
780 DEBUG(10,("Missing logon_info in ticket of %s\n",
782 return NT_STATUS_INVALID_PARAMETER;
785 DEBUG(10,("winbindd_raw_kerberos_login: winbindd validated ticket of %s\n",
788 result = create_info6_from_pac(mem_ctx, logon_info,
789 upn_dns_info, &info6_copy);
790 if (!NT_STATUS_IS_OK(result)) {
794 /* if we had a user's ccache then return that string for the pam
797 if (user_ccache_file != NULL) {
799 fstrcpy(krb5ccname, user_ccache_file);
801 result = add_ccache_to_list(principal_s,
813 if (!NT_STATUS_IS_OK(result)) {
814 DEBUG(10,("winbindd_raw_kerberos_login: failed to add ccache to list: %s\n",
819 /* need to delete the memory cred cache, it is not used anymore */
821 krb5_ret = ads_kdestroy(cc);
823 DEBUG(3,("winbindd_raw_kerberos_login: "
824 "could not destroy krb5 credential cache: "
825 "%s\n", error_message(krb5_ret)));
834 * Do not delete an existing valid credential cache, if the user
835 * e.g. enters a wrong password
837 if ((strequal(krb5_cc_type, "FILE") || strequal(krb5_cc_type, "WRFILE"))
838 && user_ccache_file != NULL) {
842 /* we could have created a new credential cache with a valid tgt in it
843 * but we werent able to get or verify the service ticket for this
844 * local host and therefor didn't get the PAC, we need to remove that
845 * cache entirely now */
847 krb5_ret = ads_kdestroy(cc);
849 DEBUG(3,("winbindd_raw_kerberos_login: "
850 "could not destroy krb5 credential cache: "
851 "%s\n", error_message(krb5_ret)));
854 if (!NT_STATUS_IS_OK(remove_ccache(user))) {
855 DEBUG(3,("winbindd_raw_kerberos_login: "
856 "could not remove ccache for user %s\n",
862 return NT_STATUS_NOT_SUPPORTED;
863 #endif /* HAVE_KRB5 */
866 /****************************************************************
867 ****************************************************************/
869 bool check_request_flags(uint32_t flags)
871 uint32_t flags_edata = WBFLAG_PAM_AFS_TOKEN |
872 WBFLAG_PAM_INFO3_TEXT |
873 WBFLAG_PAM_INFO3_NDR;
875 if ( ( (flags & flags_edata) == WBFLAG_PAM_AFS_TOKEN) ||
876 ( (flags & flags_edata) == WBFLAG_PAM_INFO3_NDR) ||
877 ( (flags & flags_edata) == WBFLAG_PAM_INFO3_TEXT)||
878 !(flags & flags_edata) ) {
882 DEBUG(1, ("check_request_flags: invalid request flags[0x%08X]\n",
888 /****************************************************************
889 ****************************************************************/
891 NTSTATUS append_auth_data(TALLOC_CTX *mem_ctx,
892 struct winbindd_response *resp,
893 uint32_t request_flags,
894 uint16_t validation_level,
895 union netr_Validation *validation,
896 const char *name_domain,
897 const char *name_user)
899 struct netr_SamInfo3 *info3 = NULL;
900 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
902 result = map_validation_to_info3(talloc_tos(),
906 if (!NT_STATUS_IS_OK(result)) {
910 if (request_flags & WBFLAG_PAM_USER_SESSION_KEY) {
911 memcpy(resp->data.auth.user_session_key,
913 sizeof(resp->data.auth.user_session_key)
917 if (request_flags & WBFLAG_PAM_LMKEY) {
918 memcpy(resp->data.auth.first_8_lm_hash,
919 info3->base.LMSessKey.key,
920 sizeof(resp->data.auth.first_8_lm_hash)
924 if (request_flags & WBFLAG_PAM_UNIX_NAME) {
925 result = append_unix_username(mem_ctx, resp,
926 info3, name_domain, name_user);
927 if (!NT_STATUS_IS_OK(result)) {
928 DEBUG(10,("Failed to append Unix Username: %s\n",
934 /* currently, anything from here on potentially overwrites extra_data. */
936 if (request_flags & WBFLAG_PAM_INFO3_NDR) {
937 result = append_info3_as_ndr(mem_ctx, resp, info3);
938 if (!NT_STATUS_IS_OK(result)) {
939 DEBUG(10,("Failed to append INFO3 (NDR): %s\n",
945 if (request_flags & WBFLAG_PAM_INFO3_TEXT) {
946 result = append_info3_as_txt(mem_ctx, resp,
949 if (!NT_STATUS_IS_OK(result)) {
950 DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
956 if (request_flags & WBFLAG_PAM_AFS_TOKEN) {
957 result = append_afs_token(mem_ctx, resp,
958 info3, name_domain, name_user);
959 if (!NT_STATUS_IS_OK(result)) {
960 DEBUG(10,("Failed to append AFS token: %s\n",
966 result = NT_STATUS_OK;
972 static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
973 struct winbindd_cli_state *state,
974 struct netr_SamInfo3 **info3)
976 NTSTATUS result = NT_STATUS_LOGON_FAILURE;
977 uint16_t max_allowed_bad_attempts;
978 fstring name_domain, name_user;
980 enum lsa_SidType type;
981 uchar new_nt_pass[NT_HASH_LEN];
982 const uint8_t *cached_nt_pass;
983 const uint8_t *cached_salt;
984 struct netr_SamInfo3 *my_info3;
985 time_t kickoff_time, must_change_time;
986 bool password_good = false;
988 struct winbindd_tdc_domain *tdc_domain = NULL;
995 DEBUG(10,("winbindd_dual_pam_auth_cached\n"));
997 /* Parse domain and username */
999 parse_domain_user(state->request->data.auth.user, name_domain, name_user);
1002 if (!lookup_cached_name(name_domain,
1006 DEBUG(10,("winbindd_dual_pam_auth_cached: no such user in the cache\n"));
1007 return NT_STATUS_NO_SUCH_USER;
1010 if (type != SID_NAME_USER) {
1011 DEBUG(10,("winbindd_dual_pam_auth_cached: not a user (%s)\n", sid_type_lookup(type)));
1012 return NT_STATUS_LOGON_FAILURE;
1015 result = winbindd_get_creds(domain,
1021 if (!NT_STATUS_IS_OK(result)) {
1022 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get creds: %s\n", nt_errstr(result)));
1028 E_md4hash(state->request->data.auth.pass, new_nt_pass);
1030 dump_data_pw("new_nt_pass", new_nt_pass, NT_HASH_LEN);
1031 dump_data_pw("cached_nt_pass", cached_nt_pass, NT_HASH_LEN);
1033 dump_data_pw("cached_salt", cached_salt, NT_HASH_LEN);
1037 /* In this case we didn't store the nt_hash itself,
1038 but the MD5 combination of salt + nt_hash. */
1039 uchar salted_hash[NT_HASH_LEN];
1040 E_md5hash(cached_salt, new_nt_pass, salted_hash);
1042 password_good = (memcmp(cached_nt_pass, salted_hash,
1045 /* Old cached cred - direct store of nt_hash (bad bad bad !). */
1046 password_good = (memcmp(cached_nt_pass, new_nt_pass,
1050 if (password_good) {
1052 /* User *DOES* know the password, update logon_time and reset
1055 my_info3->base.user_flags |= NETLOGON_CACHED_ACCOUNT;
1057 if (my_info3->base.acct_flags & ACB_AUTOLOCK) {
1058 return NT_STATUS_ACCOUNT_LOCKED_OUT;
1061 if (my_info3->base.acct_flags & ACB_DISABLED) {
1062 return NT_STATUS_ACCOUNT_DISABLED;
1065 if (my_info3->base.acct_flags & ACB_WSTRUST) {
1066 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
1069 if (my_info3->base.acct_flags & ACB_SVRTRUST) {
1070 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
1073 if (my_info3->base.acct_flags & ACB_DOMTRUST) {
1074 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
1077 if (!(my_info3->base.acct_flags & ACB_NORMAL)) {
1078 DEBUG(0,("winbindd_dual_pam_auth_cached: whats wrong with that one?: 0x%08x\n",
1079 my_info3->base.acct_flags));
1080 return NT_STATUS_LOGON_FAILURE;
1083 kickoff_time = nt_time_to_unix(my_info3->base.kickoff_time);
1084 if (kickoff_time != 0 && time(NULL) > kickoff_time) {
1085 return NT_STATUS_ACCOUNT_EXPIRED;
1088 must_change_time = nt_time_to_unix(my_info3->base.force_password_change);
1089 if (must_change_time != 0 && must_change_time < time(NULL)) {
1090 /* we allow grace logons when the password has expired */
1091 my_info3->base.user_flags |= NETLOGON_GRACE_LOGON;
1092 /* return NT_STATUS_PASSWORD_EXPIRED; */
1097 if ((state->request->flags & WBFLAG_PAM_KRB5) &&
1098 ((tdc_domain = wcache_tdc_fetch_domain(state->mem_ctx, name_domain)) != NULL) &&
1099 ((tdc_domain->trust_type & LSA_TRUST_TYPE_UPLEVEL) ||
1100 /* used to cope with the case winbindd starting without network. */
1101 !strequal(tdc_domain->domain_name, tdc_domain->dns_name))) {
1104 const char *cc = NULL;
1106 const char *principal_s = NULL;
1107 const char *service = NULL;
1108 const char *user_ccache_file;
1110 if (domain->alt_name == NULL) {
1111 return NT_STATUS_INVALID_PARAMETER;
1114 uid = get_uid_from_request(state->request);
1116 DEBUG(0,("winbindd_dual_pam_auth_cached: invalid uid\n"));
1117 return NT_STATUS_INVALID_PARAMETER;
1120 cc = generate_krb5_ccache(state->mem_ctx,
1121 state->request->data.auth.krb5_cc_type,
1122 state->request->data.auth.uid,
1125 return NT_STATUS_NO_MEMORY;
1128 realm = talloc_strdup(state->mem_ctx, domain->alt_name);
1129 if (realm == NULL) {
1130 return NT_STATUS_NO_MEMORY;
1133 if (!strupper_m(realm)) {
1134 return NT_STATUS_INVALID_PARAMETER;
1137 principal_s = talloc_asprintf(state->mem_ctx, "%s@%s", name_user, realm);
1138 if (principal_s == NULL) {
1139 return NT_STATUS_NO_MEMORY;
1142 service = talloc_asprintf(state->mem_ctx, "%s/%s@%s", KRB5_TGS_NAME, realm, realm);
1143 if (service == NULL) {
1144 return NT_STATUS_NO_MEMORY;
1147 if (user_ccache_file != NULL) {
1149 fstrcpy(state->response->data.auth.krb5ccname,
1152 result = add_ccache_to_list(principal_s,
1155 state->request->data.auth.user,
1156 state->request->data.auth.pass,
1160 time(NULL) + lp_winbind_cache_time(),
1161 time(NULL) + WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
1164 if (!NT_STATUS_IS_OK(result)) {
1165 DEBUG(10,("winbindd_dual_pam_auth_cached: failed "
1166 "to add ccache to list: %s\n",
1167 nt_errstr(result)));
1171 #endif /* HAVE_KRB5 */
1173 /* FIXME: we possibly should handle logon hours as well (does xp when
1174 * offline?) see auth/auth_sam.c:sam_account_ok for details */
1176 unix_to_nt_time(&my_info3->base.logon_time, time(NULL));
1177 my_info3->base.bad_password_count = 0;
1179 result = winbindd_update_creds_by_info3(domain,
1180 state->request->data.auth.user,
1181 state->request->data.auth.pass,
1183 if (!NT_STATUS_IS_OK(result)) {
1184 DEBUG(1,("winbindd_dual_pam_auth_cached: failed to update creds: %s\n",
1185 nt_errstr(result)));
1189 return NT_STATUS_OK;
1193 /* User does *NOT* know the correct password, modify info3 accordingly, but only if online */
1194 if (domain->online == false) {
1198 /* failure of this is not critical */
1199 result = get_max_bad_attempts_from_lockout_policy(domain, state->mem_ctx, &max_allowed_bad_attempts);
1200 if (!NT_STATUS_IS_OK(result)) {
1201 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get max_allowed_bad_attempts. "
1202 "Won't be able to honour account lockout policies\n"));
1205 /* increase counter */
1206 my_info3->base.bad_password_count++;
1208 if (max_allowed_bad_attempts == 0) {
1213 if (my_info3->base.bad_password_count >= max_allowed_bad_attempts) {
1215 uint32_t password_properties;
1217 result = get_pwd_properties(domain, state->mem_ctx, &password_properties);
1218 if (!NT_STATUS_IS_OK(result)) {
1219 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get password properties.\n"));
1222 if ((my_info3->base.rid != DOMAIN_RID_ADMINISTRATOR) ||
1223 (password_properties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)) {
1224 my_info3->base.acct_flags |= ACB_AUTOLOCK;
1229 result = winbindd_update_creds_by_info3(domain,
1230 state->request->data.auth.user,
1234 if (!NT_STATUS_IS_OK(result)) {
1235 DEBUG(0,("winbindd_dual_pam_auth_cached: failed to update creds %s\n",
1236 nt_errstr(result)));
1239 return NT_STATUS_LOGON_FAILURE;
1242 static NTSTATUS winbindd_dual_pam_auth_kerberos(struct winbindd_domain *domain,
1243 struct winbindd_cli_state *state,
1244 struct netr_SamInfo6 **info6)
1246 struct winbindd_domain *contact_domain;
1247 fstring name_domain, name_user;
1250 DEBUG(10,("winbindd_dual_pam_auth_kerberos\n"));
1252 /* Parse domain and username */
1254 parse_domain_user(state->request->data.auth.user, name_domain, name_user);
1256 /* what domain should we contact? */
1259 if (!(contact_domain = find_domain_from_name(name_domain))) {
1260 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1261 state->request->data.auth.user, name_domain, name_user, name_domain));
1262 result = NT_STATUS_NO_SUCH_USER;
1267 if (is_myname(name_domain)) {
1268 DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
1269 result = NT_STATUS_NO_SUCH_USER;
1273 contact_domain = find_domain_from_name(name_domain);
1274 if (contact_domain == NULL) {
1275 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1276 state->request->data.auth.user, name_domain, name_user, name_domain));
1278 result = NT_STATUS_NO_SUCH_USER;
1283 if (contact_domain->initialized &&
1284 contact_domain->active_directory) {
1288 if (!contact_domain->initialized) {
1289 init_dc_connection(contact_domain, false);
1292 if (!contact_domain->active_directory) {
1293 DEBUG(3,("krb5 auth requested but domain is not Active Directory\n"));
1294 return NT_STATUS_INVALID_LOGON_TYPE;
1297 result = winbindd_raw_kerberos_login(
1298 state->mem_ctx, contact_domain,
1299 state->request->data.auth.user,
1300 state->request->data.auth.pass,
1301 state->request->data.auth.krb5_cc_type,
1302 get_uid_from_request(state->request),
1303 info6, state->response->data.auth.krb5ccname);
1308 static NTSTATUS winbindd_dual_auth_passdb(TALLOC_CTX *mem_ctx,
1309 uint32_t logon_parameters,
1310 const char *domain, const char *user,
1311 const DATA_BLOB *challenge,
1312 const DATA_BLOB *lm_resp,
1313 const DATA_BLOB *nt_resp,
1315 uint8_t *pauthoritative,
1316 struct netr_SamInfo3 **pinfo3)
1318 struct auth_context *auth_context;
1319 struct auth_serversupplied_info *server_info;
1320 struct auth_usersupplied_info *user_info = NULL;
1321 struct tsocket_address *local;
1322 struct netr_SamInfo3 *info3;
1326 TALLOC_CTX *frame = talloc_stackframe();
1329 * We are authoritative by default
1331 *pauthoritative = 1;
1333 rc = tsocket_address_inet_from_strings(frame,
1340 return NT_STATUS_NO_MEMORY;
1344 * TODO: We should get the service description passed in from
1345 * the winbind client, so we can have "smb2", "squid" or "samr" logged
1348 status = make_user_info(frame, &user_info, user, user, domain, domain,
1349 lp_netbios_name(), local, local,
1351 lm_resp, nt_resp, NULL, NULL,
1352 NULL, AUTH_PASSWORD_RESPONSE);
1353 if (!NT_STATUS_IS_OK(status)) {
1354 DEBUG(10, ("make_user_info failed: %s\n", nt_errstr(status)));
1359 user_info->logon_parameters = logon_parameters;
1361 /* We don't want any more mapping of the username */
1362 user_info->mapped_state = True;
1364 /* We don't want to come back to winbindd or to do PAM account checks */
1365 user_info->flags |= USER_INFO_INFO3_AND_NO_AUTHZ;
1368 user_info->flags |= USER_INFO_INTERACTIVE_LOGON;
1371 status = make_auth3_context_for_winbind(frame, &auth_context);
1372 if (!NT_STATUS_IS_OK(status)) {
1373 DBG_ERR("make_auth3_context_for_winbind failed: %s\n",
1379 ok = auth3_context_set_challenge(auth_context,
1380 challenge->data, "fixed");
1383 return NT_STATUS_NO_MEMORY;
1386 status = auth_check_ntlm_password(mem_ctx,
1391 if (!NT_STATUS_IS_OK(status)) {
1396 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
1397 if (info3 == NULL) {
1399 return NT_STATUS_NO_MEMORY;
1402 status = serverinfo_to_SamInfo3(server_info, info3);
1403 if (!NT_STATUS_IS_OK(status)) {
1406 DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
1407 nt_errstr(status)));
1412 DEBUG(10, ("Authenticaticating user %s\\%s returned %s\n", domain,
1413 user, nt_errstr(status)));
1418 static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
1419 TALLOC_CTX *mem_ctx,
1420 uint32_t logon_parameters,
1421 const char *username,
1422 const char *password,
1423 const char *domainname,
1424 const char *workstation,
1425 bool plaintext_given,
1426 const uint8_t chal[8],
1427 DATA_BLOB lm_response,
1428 DATA_BLOB nt_response,
1430 uint8_t *authoritative,
1432 uint16_t *_validation_level,
1433 union netr_Validation **_validation)
1436 int netr_attempts = 0;
1439 enum netr_LogonInfoClass logon_type_i;
1440 enum netr_LogonInfoClass logon_type_n;
1441 uint16_t validation_level = UINT16_MAX;
1442 union netr_Validation *validation = NULL;
1445 struct rpc_pipe_client *netlogon_pipe;
1446 struct netlogon_creds_cli_context *netlogon_creds_ctx = NULL;
1450 result = cm_connect_netlogon_secure(domain, &netlogon_pipe,
1451 &netlogon_creds_ctx);
1453 if (NT_STATUS_EQUAL(result,
1454 NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
1456 * This means we don't have a trust account.
1459 result = NT_STATUS_NO_SUCH_USER;
1463 if (!NT_STATUS_IS_OK(result)) {
1464 DEBUG(3,("Could not open handle to NETLOGON pipe "
1465 "(error: %s, attempts: %d)\n",
1466 nt_errstr(result), netr_attempts));
1468 /* After the first retry always close the connection */
1469 if (netr_attempts > 0) {
1470 DEBUG(3, ("This is again a problem for this "
1471 "particular call, forcing the close "
1472 "of this connection\n"));
1473 invalidate_cm_connection(domain);
1476 /* After the second retry failover to the next DC */
1477 if (netr_attempts > 1) {
1479 * If the netlogon server is not reachable then
1480 * it is possible that the DC is rebuilding
1481 * sysvol and shutdown netlogon for that time.
1482 * We should failover to the next dc.
1484 DEBUG(3, ("This is the third problem for this "
1485 "particular call, adding DC to the "
1486 "negative cache list: %s %s\n", domain->name, domain->dcname));
1487 add_failed_connection_entry(domain->name,
1490 saf_delete(domain->name);
1493 /* Only allow 3 retries */
1494 if (netr_attempts < 3) {
1495 DEBUG(3, ("The connection to netlogon "
1496 "failed, retrying\n"));
1504 logon_type_i = NetlogonInteractiveInformation;
1505 logon_type_n = NetlogonNetworkInformation;
1506 if (domain->domain_trust_attribs & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST) {
1507 logon_type_i = NetlogonInteractiveTransitiveInformation;
1508 logon_type_n = NetlogonNetworkTransitiveInformation;
1511 if (domain->domain_trust_attribs & LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
1512 logon_type_i = NetlogonInteractiveTransitiveInformation;
1513 logon_type_n = NetlogonNetworkTransitiveInformation;
1516 if (domain->domain_trust_attribs & LSA_TRUST_ATTRIBUTE_NON_TRANSITIVE) {
1517 logon_type_i = NetlogonInteractiveInformation;
1518 logon_type_n = NetlogonNetworkInformation;
1521 if (domain->domain_trust_attribs & LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN) {
1522 logon_type_i = NetlogonInteractiveInformation;
1523 logon_type_n = NetlogonNetworkInformation;
1527 if (plaintext_given) {
1528 result = rpccli_netlogon_password_logon(
1530 netlogon_pipe->binding_handle,
1542 } else if (interactive) {
1543 result = rpccli_netlogon_interactive_logon(
1545 netlogon_pipe->binding_handle,
1559 result = rpccli_netlogon_network_logon(
1561 netlogon_pipe->binding_handle,
1578 * we increment this after the "feature negotiation"
1579 * for can_do_samlogon_ex and can_do_validation6
1583 /* We have to try a second time as cm_connect_netlogon
1584 might not yet have noticed that the DC has killed
1587 if (!rpccli_is_connected(netlogon_pipe)) {
1592 /* if we get access denied, a possible cause was that we had
1593 and open connection to the DC, but someone changed our
1594 machine account password out from underneath us using 'net
1595 rpc changetrustpw' */
1597 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1598 DEBUG(1,("winbind_samlogon_retry_loop: sam_logon returned "
1599 "ACCESS_DENIED. Maybe the DC has Restrict "
1600 "NTLM set or the trust account "
1601 "password was changed and we didn't know it. "
1602 "Killing connections to domain %s\n",
1604 invalidate_cm_connection(domain);
1608 if (NT_STATUS_EQUAL(result, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
1610 * Got DCERPC_FAULT_OP_RNG_ERROR for SamLogon
1611 * (no Ex). This happens against old Samba
1612 * DCs, if LogonSamLogonEx() fails with an error
1613 * e.g. NT_STATUS_NO_SUCH_USER or NT_STATUS_WRONG_PASSWORD.
1615 * The server will log something like this:
1616 * api_net_sam_logon_ex: Failed to marshall NET_R_SAM_LOGON_EX.
1618 * This sets the whole connection into a fault_state mode
1619 * and all following request get NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE.
1621 * This also happens to our retry with LogonSamLogonWithFlags()
1622 * and LogonSamLogon().
1624 * In order to recover from this situation, we need to
1625 * drop the connection.
1627 invalidate_cm_connection(domain);
1628 result = NT_STATUS_LOGON_FAILURE;
1632 } while ( (attempts < 2) && retry );
1634 if (NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT)) {
1635 DEBUG(3,("winbind_samlogon_retry_loop: sam_network_logon(ex) "
1636 "returned NT_STATUS_IO_TIMEOUT after the retry. "
1637 "Killing connections to domain %s\n",
1639 invalidate_cm_connection(domain);
1642 if (!NT_STATUS_IS_OK(result)) {
1646 *_validation_level = validation_level;
1647 *_validation = validation;
1648 return NT_STATUS_OK;
1651 static NTSTATUS winbindd_dual_pam_auth_samlogon(
1652 TALLOC_CTX *mem_ctx,
1653 struct winbindd_domain *domain,
1656 uint32_t request_flags,
1657 uint16_t *_validation_level,
1658 union netr_Validation **_validation)
1664 unsigned char local_nt_response[24];
1665 fstring name_domain, name_user;
1667 uint8_t authoritative = 0;
1669 uint16_t validation_level;
1670 union netr_Validation *validation = NULL;
1671 struct netr_SamBaseInfo *base_info = NULL;
1673 DEBUG(10,("winbindd_dual_pam_auth_samlogon\n"));
1675 /* Parse domain and username */
1677 parse_domain_user(user, name_domain, name_user);
1680 * We check against domain->name instead of
1681 * name_domain, as find_auth_domain() ->
1682 * find_domain_from_name_noinit() already decided
1683 * that we are in a child for the correct domain.
1685 * name_domain can also be lp_realm()
1686 * we need to check against domain->name.
1688 if (strequal(domain->name, get_global_sam_name())) {
1689 DATA_BLOB chal_blob = data_blob_const(chal, sizeof(chal));
1690 struct netr_SamInfo3 *info3 = NULL;
1692 /* do password magic */
1694 generate_random_buffer(chal, sizeof(chal));
1696 if (lp_client_ntlmv2_auth()) {
1697 DATA_BLOB server_chal;
1698 DATA_BLOB names_blob;
1699 server_chal = data_blob_const(chal, 8);
1701 /* note that the 'workgroup' here is for the local
1702 machine. The 'server name' must match the
1703 'workstation' passed to the actual SamLogon call.
1705 names_blob = NTLMv2_generate_names_blob(
1706 mem_ctx, lp_netbios_name(), lp_workgroup());
1708 if (!SMBNTLMv2encrypt(mem_ctx, name_user, name_domain,
1712 &lm_resp, &nt_resp, NULL, NULL)) {
1713 data_blob_free(&names_blob);
1714 DEBUG(0, ("winbindd_pam_auth: SMBNTLMv2encrypt() failed!\n"));
1715 result = NT_STATUS_NO_MEMORY;
1718 data_blob_free(&names_blob);
1720 lm_resp = data_blob_null;
1721 SMBNTencrypt(pass, chal, local_nt_response);
1723 nt_resp = data_blob_talloc(mem_ctx, local_nt_response,
1724 sizeof(local_nt_response));
1727 result = winbindd_dual_auth_passdb(
1728 talloc_tos(), 0, name_domain, name_user,
1729 &chal_blob, &lm_resp, &nt_resp,
1730 true, /* interactive */
1735 * We need to try the remote NETLOGON server if this is
1736 * not authoritative (for example on the RODC).
1738 if (authoritative != 0) {
1739 if (NT_STATUS_IS_OK(result)) {
1740 result = map_info3_to_validation(
1746 if (!NT_STATUS_IS_OK(result)) {
1755 /* check authentication loop */
1757 result = winbind_samlogon_retry_loop(domain,
1764 true, /* plaintext_given */
1766 data_blob_null, data_blob_null,
1767 true, /* interactive */
1772 if (!NT_STATUS_IS_OK(result)) {
1776 /* handle the case where a NT4 DC does not fill in the acct_flags in
1777 * the samlogon reply info3. When accurate info3 is required by the
1778 * caller, we look up the account flags ourselves - gd */
1780 switch (validation_level) {
1782 base_info = &validation->sam3->base;
1785 base_info = &validation->sam6->base;
1788 DBG_ERR("Bad validation level %d", (int)validation_level);
1789 result = NT_STATUS_INTERNAL_ERROR;
1792 if ((request_flags & WBFLAG_PAM_INFO3_TEXT) &&
1793 (base_info->acct_flags == 0))
1795 struct rpc_pipe_client *samr_pipe;
1796 struct policy_handle samr_domain_handle, user_pol;
1797 union samr_UserInfo *info = NULL;
1798 NTSTATUS status_tmp, result_tmp;
1799 uint32_t acct_flags;
1800 struct dcerpc_binding_handle *b;
1802 status_tmp = cm_connect_sam(domain, mem_ctx, false,
1803 &samr_pipe, &samr_domain_handle);
1805 if (!NT_STATUS_IS_OK(status_tmp)) {
1806 DEBUG(3, ("could not open handle to SAMR pipe: %s\n",
1807 nt_errstr(status_tmp)));
1811 b = samr_pipe->binding_handle;
1813 status_tmp = dcerpc_samr_OpenUser(b, mem_ctx,
1814 &samr_domain_handle,
1815 MAXIMUM_ALLOWED_ACCESS,
1820 if (!NT_STATUS_IS_OK(status_tmp)) {
1821 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1822 nt_errstr(status_tmp)));
1825 if (!NT_STATUS_IS_OK(result_tmp)) {
1826 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1827 nt_errstr(result_tmp)));
1831 status_tmp = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1837 if (any_nt_status_not_ok(status_tmp, result_tmp,
1839 DEBUG(3, ("could not query user info on SAMR pipe: %s\n",
1840 nt_errstr(status_tmp)));
1841 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1845 acct_flags = info->info16.acct_flags;
1847 if (acct_flags == 0) {
1848 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1852 base_info->acct_flags = acct_flags;
1854 DEBUG(10,("successfully retrieved acct_flags 0x%x\n", acct_flags));
1856 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1860 if (NT_STATUS_IS_OK(result)) {
1861 *_validation_level = validation_level;
1862 *_validation = validation;
1867 enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
1868 struct winbindd_cli_state *state)
1870 NTSTATUS result = NT_STATUS_LOGON_FAILURE;
1871 NTSTATUS krb5_result = NT_STATUS_OK;
1872 fstring name_domain, name_user;
1874 fstring domain_user;
1875 uint16_t validation_level = UINT16_MAX;
1876 union netr_Validation *validation = NULL;
1877 NTSTATUS name_map_status = NT_STATUS_UNSUCCESSFUL;
1879 /* Ensure null termination */
1880 state->request->data.auth.user[sizeof(state->request->data.auth.user)-1]='\0';
1882 /* Ensure null termination */
1883 state->request->data.auth.pass[sizeof(state->request->data.auth.pass)-1]='\0';
1885 DEBUG(3, ("[%5lu]: dual pam auth %s\n", (unsigned long)state->pid,
1886 state->request->data.auth.user));
1888 /* Parse domain and username */
1890 name_map_status = normalize_name_unmap(state->mem_ctx,
1891 state->request->data.auth.user,
1894 /* If the name normalization didnt' actually do anything,
1895 just use the original name */
1897 if (!NT_STATUS_IS_OK(name_map_status) &&
1898 !NT_STATUS_EQUAL(name_map_status, NT_STATUS_FILE_RENAMED))
1900 mapped_user = state->request->data.auth.user;
1903 parse_domain_user(mapped_user, name_domain, name_user);
1905 if ( mapped_user != state->request->data.auth.user ) {
1906 fstr_sprintf( domain_user, "%s%c%s", name_domain,
1907 *lp_winbind_separator(),
1909 strlcpy( state->request->data.auth.user, domain_user,
1910 sizeof(state->request->data.auth.user));
1913 if (!domain->online) {
1914 result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1915 if (domain->startup) {
1916 /* Logons are very important to users. If we're offline and
1917 we get a request within the first 30 seconds of startup,
1918 try very hard to find a DC and go online. */
1920 DEBUG(10,("winbindd_dual_pam_auth: domain: %s offline and auth "
1921 "request in startup mode.\n", domain->name ));
1923 winbindd_flush_negative_conn_cache(domain);
1924 result = init_dc_connection(domain, false);
1928 DEBUG(10,("winbindd_dual_pam_auth: domain: %s last was %s\n", domain->name, domain->online ? "online":"offline"));
1930 /* Check for Kerberos authentication */
1931 if (domain->online && (state->request->flags & WBFLAG_PAM_KRB5)) {
1932 struct netr_SamInfo6 *info6 = NULL;
1934 result = winbindd_dual_pam_auth_kerberos(domain, state, &info6);
1935 /* save for later */
1936 krb5_result = result;
1938 if (NT_STATUS_IS_OK(result)) {
1939 DEBUG(10,("winbindd_dual_pam_auth_kerberos succeeded\n"));
1941 result = map_info6_to_validation(state->mem_ctx,
1946 if (!NT_STATUS_IS_OK(result)) {
1947 DBG_ERR("map_info6_to_validation failed\n");
1950 goto process_result;
1953 DBG_DEBUG("winbindd_dual_pam_auth_kerberos failed: %s\n",
1956 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS) ||
1957 NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT) ||
1958 NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
1959 DEBUG(10,("winbindd_dual_pam_auth_kerberos setting domain to offline\n"));
1960 set_domain_offline( domain );
1964 /* there are quite some NT_STATUS errors where there is no
1965 * point in retrying with a samlogon, we explictly have to take
1966 * care not to increase the bad logon counter on the DC */
1968 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_DISABLED) ||
1969 NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_EXPIRED) ||
1970 NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_LOCKED_OUT) ||
1971 NT_STATUS_EQUAL(result, NT_STATUS_INVALID_LOGON_HOURS) ||
1972 NT_STATUS_EQUAL(result, NT_STATUS_INVALID_WORKSTATION) ||
1973 NT_STATUS_EQUAL(result, NT_STATUS_LOGON_FAILURE) ||
1974 NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER) ||
1975 NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED) ||
1976 NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) ||
1977 NT_STATUS_EQUAL(result, NT_STATUS_WRONG_PASSWORD)) {
1981 if (state->request->flags & WBFLAG_PAM_FALLBACK_AFTER_KRB5) {
1982 DEBUG(3,("falling back to samlogon\n"));
1990 /* Check for Samlogon authentication */
1991 if (domain->online) {
1992 struct netr_SamBaseInfo *base_info = NULL;
1994 result = winbindd_dual_pam_auth_samlogon(
1995 state->mem_ctx, domain,
1996 state->request->data.auth.user,
1997 state->request->data.auth.pass,
1998 state->request->flags,
2002 if (NT_STATUS_IS_OK(result)) {
2003 DEBUG(10,("winbindd_dual_pam_auth_samlogon succeeded\n"));
2005 switch (validation_level) {
2007 base_info = &validation->sam3->base;
2010 base_info = &validation->sam6->base;
2013 DBG_ERR("Bad validation level %d\n",
2015 result = NT_STATUS_INTERNAL_ERROR;
2019 /* add the Krb5 err if we have one */
2020 if ( NT_STATUS_EQUAL(krb5_result, NT_STATUS_TIME_DIFFERENCE_AT_DC ) ) {
2021 base_info->user_flags |= LOGON_KRB5_FAIL_CLOCK_SKEW;
2024 goto process_result;
2027 DEBUG(10,("winbindd_dual_pam_auth_samlogon failed: %s\n",
2028 nt_errstr(result)));
2030 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS) ||
2031 NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT) ||
2032 NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND))
2034 DEBUG(10,("winbindd_dual_pam_auth_samlogon setting domain to offline\n"));
2035 set_domain_offline( domain );
2039 if (domain->online) {
2040 /* We're still online - fail. */
2046 /* Check for Cached logons */
2047 if (!domain->online && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN) &&
2048 lp_winbind_offline_logon()) {
2049 struct netr_SamInfo3 *info3 = NULL;
2051 result = winbindd_dual_pam_auth_cached(domain, state, &info3);
2053 if (!NT_STATUS_IS_OK(result)) {
2054 DEBUG(10,("winbindd_dual_pam_auth_cached failed: %s\n", nt_errstr(result)));
2057 DEBUG(10,("winbindd_dual_pam_auth_cached succeeded\n"));
2059 result = map_info3_to_validation(state->mem_ctx,
2064 if (!NT_STATUS_IS_OK(result)) {
2065 DBG_ERR("map_info3_to_validation failed\n");
2072 if (NT_STATUS_IS_OK(result)) {
2073 struct dom_sid user_sid;
2074 TALLOC_CTX *base_ctx = NULL;
2075 struct netr_SamBaseInfo *base_info = NULL;
2076 struct netr_SamInfo3 *info3 = NULL;
2078 switch (validation_level) {
2080 base_ctx = validation->sam3;
2081 base_info = &validation->sam3->base;
2084 base_ctx = validation->sam6;
2085 base_info = &validation->sam6->base;
2088 result = NT_STATUS_INTERNAL_ERROR;
2092 sid_compose(&user_sid, base_info->domain_sid, base_info->rid);
2094 if (base_info->full_name.string == NULL) {
2095 struct netr_SamInfo3 *cached_info3;
2097 cached_info3 = netsamlogon_cache_get(state->mem_ctx,
2099 if (cached_info3 != NULL &&
2100 cached_info3->base.full_name.string != NULL) {
2101 base_info->full_name.string = talloc_strdup(
2103 cached_info3->base.full_name.string);
2104 if (base_info->full_name.string == NULL) {
2105 result = NT_STATUS_NO_MEMORY;
2110 /* this might fail so we don't check the return code */
2111 wcache_query_user_fullname(domain,
2114 &base_info->full_name.string);
2118 result = map_validation_to_info3(talloc_tos(),
2122 if (!NT_STATUS_IS_OK(result)) {
2126 wcache_invalidate_samlogon(find_domain_from_name(name_domain),
2128 netsamlogon_cache_store(name_user, info3);
2130 /* save name_to_sid info as early as possible (only if
2131 this is our primary domain so we don't invalidate
2132 the cache entry by storing the seq_num for the wrong
2134 if ( domain->primary ) {
2135 cache_name2sid(domain, name_domain, name_user,
2136 SID_NAME_USER, &user_sid);
2139 /* Check if the user is in the right group */
2141 result = check_info3_in_group(
2143 state->request->data.auth.require_membership_of_sid);
2144 if (!NT_STATUS_IS_OK(result)) {
2145 DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
2146 state->request->data.auth.user,
2147 state->request->data.auth.require_membership_of_sid));
2151 result = append_auth_data(state->mem_ctx, state->response,
2152 state->request->flags,
2155 name_domain, name_user);
2156 if (!NT_STATUS_IS_OK(result)) {
2160 if ((state->request->flags & WBFLAG_PAM_CACHED_LOGIN)
2161 && lp_winbind_offline_logon()) {
2163 result = winbindd_store_creds(domain,
2164 state->request->data.auth.user,
2165 state->request->data.auth.pass,
2169 if (state->request->flags & WBFLAG_PAM_GET_PWD_POLICY) {
2171 * WBFLAG_PAM_GET_PWD_POLICY is not used within
2172 * any Samba caller anymore.
2174 * We just fake this based on the effective values
2175 * for the user, for legacy callers.
2177 fake_password_policy(state->response, &info3->base);
2180 result = NT_STATUS_OK;
2184 /* give us a more useful (more correct?) error code */
2185 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
2186 (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
2187 result = NT_STATUS_NO_LOGON_SERVERS;
2190 set_auth_errors(state->response, result);
2192 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n",
2193 state->request->data.auth.user,
2194 state->response->data.auth.nt_status_string,
2195 state->response->data.auth.pam_error));
2197 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2200 NTSTATUS winbind_dual_SamLogon(struct winbindd_domain *domain,
2201 TALLOC_CTX *mem_ctx,
2203 uint32_t logon_parameters,
2204 const char *name_user,
2205 const char *name_domain,
2206 const char *workstation,
2207 const uint8_t chal[8],
2208 DATA_BLOB lm_response,
2209 DATA_BLOB nt_response,
2210 uint8_t *authoritative,
2213 uint16_t *_validation_level,
2214 union netr_Validation **_validation)
2216 uint16_t validation_level;
2217 union netr_Validation *validation = NULL;
2221 * We check against domain->name instead of
2222 * name_domain, as find_auth_domain() ->
2223 * find_domain_from_name_noinit() already decided
2224 * that we are in a child for the correct domain.
2226 * name_domain can also be lp_realm()
2227 * we need to check against domain->name.
2229 if (!skip_sam && strequal(domain->name, get_global_sam_name())) {
2230 DATA_BLOB chal_blob = data_blob_const(
2232 struct netr_SamInfo3 *info3 = NULL;
2234 result = winbindd_dual_auth_passdb(
2237 name_domain, name_user,
2238 &chal_blob, &lm_response, &nt_response,
2242 if (NT_STATUS_IS_OK(result)) {
2243 result = map_info3_to_validation(mem_ctx,
2248 if (!NT_STATUS_IS_OK(result)) {
2254 * We need to try the remote NETLOGON server if this is
2255 * not authoritative.
2257 if (*authoritative != 0) {
2259 goto process_result;
2263 result = winbind_samlogon_retry_loop(domain,
2267 NULL, /* password */
2269 /* Bug #3248 - found by Stefan Burkei. */
2270 workstation, /* We carefully set this above so use it... */
2271 false, /* plaintext_given */
2280 if (!NT_STATUS_IS_OK(result)) {
2286 if (NT_STATUS_IS_OK(result)) {
2287 struct dom_sid user_sid;
2288 TALLOC_CTX *base_ctx = NULL;
2289 struct netr_SamBaseInfo *base_info = NULL;
2290 struct netr_SamInfo3 *info3 = NULL;
2292 switch (validation_level) {
2294 base_ctx = validation->sam3;
2295 base_info = &validation->sam3->base;
2298 base_ctx = validation->sam6;
2299 base_info = &validation->sam6->base;
2302 result = NT_STATUS_INTERNAL_ERROR;
2306 sid_compose(&user_sid, base_info->domain_sid, base_info->rid);
2308 if (base_info->full_name.string == NULL) {
2309 struct netr_SamInfo3 *cached_info3;
2311 cached_info3 = netsamlogon_cache_get(mem_ctx,
2313 if (cached_info3 != NULL &&
2314 cached_info3->base.full_name.string != NULL)
2316 base_info->full_name.string = talloc_strdup(
2318 cached_info3->base.full_name.string);
2321 /* this might fail so we don't check the return code */
2322 wcache_query_user_fullname(domain,
2325 &base_info->full_name.string);
2329 result = map_validation_to_info3(talloc_tos(),
2333 if (!NT_STATUS_IS_OK(result)) {
2336 wcache_invalidate_samlogon(find_domain_from_name(name_domain),
2338 netsamlogon_cache_store(name_user, info3);
2344 /* give us a more useful (more correct?) error code */
2345 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
2346 (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
2347 result = NT_STATUS_NO_LOGON_SERVERS;
2350 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2351 ("NTLM CRAP authentication for user [%s]\\[%s] returned %s\n",
2354 nt_errstr(result)));
2356 if (!NT_STATUS_IS_OK(result)) {
2360 *_validation_level = validation_level;
2361 *_validation = validation;
2362 return NT_STATUS_OK;
2365 enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
2366 struct winbindd_cli_state *state)
2369 const char *name_user = NULL;
2370 const char *name_domain = NULL;
2371 const char *workstation;
2372 uint8_t authoritative = 0;
2374 uint16_t validation_level;
2375 union netr_Validation *validation = NULL;
2376 DATA_BLOB lm_resp, nt_resp;
2378 /* This is child-only, so no check for privileged access is needed
2381 /* Ensure null termination */
2382 state->request->data.auth_crap.user[sizeof(state->request->data.auth_crap.user)-1]=0;
2383 state->request->data.auth_crap.domain[sizeof(state->request->data.auth_crap.domain)-1]=0;
2385 name_user = state->request->data.auth_crap.user;
2386 name_domain = state->request->data.auth_crap.domain;
2387 workstation = state->request->data.auth_crap.workstation;
2389 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state->pid,
2390 name_domain, name_user));
2392 if (state->request->data.auth_crap.lm_resp_len > sizeof(state->request->data.auth_crap.lm_resp)
2393 || state->request->data.auth_crap.nt_resp_len > sizeof(state->request->data.auth_crap.nt_resp)) {
2394 if (!(state->request->flags & WBFLAG_BIG_NTLMV2_BLOB) ||
2395 state->request->extra_len != state->request->data.auth_crap.nt_resp_len) {
2396 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n",
2397 state->request->data.auth_crap.lm_resp_len,
2398 state->request->data.auth_crap.nt_resp_len));
2399 result = NT_STATUS_INVALID_PARAMETER;
2404 lm_resp = data_blob_talloc(state->mem_ctx, state->request->data.auth_crap.lm_resp,
2405 state->request->data.auth_crap.lm_resp_len);
2407 if (state->request->flags & WBFLAG_BIG_NTLMV2_BLOB) {
2408 nt_resp = data_blob_talloc(state->mem_ctx,
2409 state->request->extra_data.data,
2410 state->request->data.auth_crap.nt_resp_len);
2412 nt_resp = data_blob_talloc(state->mem_ctx,
2413 state->request->data.auth_crap.nt_resp,
2414 state->request->data.auth_crap.nt_resp_len);
2417 result = winbind_dual_SamLogon(domain,
2419 false, /* interactive */
2420 state->request->data.auth_crap.logon_parameters,
2423 /* Bug #3248 - found by Stefan Burkei. */
2424 workstation, /* We carefully set this above so use it... */
2425 state->request->data.auth_crap.chal,
2433 if (!NT_STATUS_IS_OK(result)) {
2434 state->response->data.auth.authoritative = authoritative;
2438 if (NT_STATUS_IS_OK(result)) {
2439 struct netr_SamInfo3 *info3 = NULL;
2441 result = map_validation_to_info3(state->mem_ctx,
2445 if (!NT_STATUS_IS_OK(result)) {
2449 /* Check if the user is in the right group */
2450 result = check_info3_in_group(
2452 state->request->data.auth_crap.require_membership_of_sid);
2453 if (!NT_STATUS_IS_OK(result)) {
2454 DEBUG(3, ("User %s is not in the required group (%s), so "
2455 "crap authentication is rejected\n",
2456 state->request->data.auth_crap.user,
2457 state->request->data.auth_crap.require_membership_of_sid));
2461 result = append_auth_data(state->mem_ctx, state->response,
2462 state->request->flags,
2465 name_domain, name_user);
2466 if (!NT_STATUS_IS_OK(result)) {
2473 if (state->request->flags & WBFLAG_PAM_NT_STATUS_SQUASH) {
2474 result = nt_status_squash(result);
2477 set_auth_errors(state->response, result);
2479 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2482 enum winbindd_result winbindd_dual_pam_chauthtok(struct winbindd_domain *contact_domain,
2483 struct winbindd_cli_state *state)
2486 char *newpass = NULL;
2487 struct policy_handle dom_pol;
2488 struct rpc_pipe_client *cli = NULL;
2489 bool got_info = false;
2490 struct samr_DomInfo1 *info = NULL;
2491 struct userPwdChangeFailureInformation *reject = NULL;
2492 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2493 fstring domain, user;
2494 struct dcerpc_binding_handle *b = NULL;
2496 ZERO_STRUCT(dom_pol);
2498 DEBUG(3, ("[%5lu]: dual pam chauthtok %s\n", (unsigned long)state->pid,
2499 state->request->data.auth.user));
2501 if (!parse_domain_user(state->request->data.chauthtok.user, domain, user)) {
2505 /* Change password */
2507 oldpass = state->request->data.chauthtok.oldpass;
2508 newpass = state->request->data.chauthtok.newpass;
2510 /* Initialize reject reason */
2511 state->response->data.auth.reject_reason = Undefined;
2513 /* Get sam handle */
2515 result = cm_connect_sam(contact_domain, state->mem_ctx, true, &cli,
2517 if (!NT_STATUS_IS_OK(result)) {
2518 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
2522 b = cli->binding_handle;
2524 result = rpccli_samr_chgpasswd_user3(cli, state->mem_ctx,
2531 /* Windows 2003 returns NT_STATUS_PASSWORD_RESTRICTION */
2533 if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION) ) {
2535 fill_in_password_policy(state->response, info);
2537 state->response->data.auth.reject_reason =
2538 reject->extendedFailureReason;
2543 /* atm the pidl generated rpccli_samr_ChangePasswordUser3 function will
2544 * return with NT_STATUS_BUFFER_TOO_SMALL for w2k dcs as w2k just
2545 * returns with 4byte error code (NT_STATUS_NOT_SUPPORTED) which is too
2546 * short to comply with the samr_ChangePasswordUser3 idl - gd */
2548 /* only fallback when the chgpasswd_user3 call is not supported */
2549 if (NT_STATUS_EQUAL(result, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE) ||
2550 NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED) ||
2551 NT_STATUS_EQUAL(result, NT_STATUS_BUFFER_TOO_SMALL) ||
2552 NT_STATUS_EQUAL(result, NT_STATUS_NOT_IMPLEMENTED)) {
2554 DEBUG(10,("Password change with chgpasswd_user3 failed with: %s, retrying chgpasswd_user2\n",
2555 nt_errstr(result)));
2557 result = rpccli_samr_chgpasswd_user2(cli, state->mem_ctx, user, newpass, oldpass);
2559 /* Windows 2000 returns NT_STATUS_ACCOUNT_RESTRICTION.
2560 Map to the same status code as Windows 2003. */
2562 if ( NT_STATUS_EQUAL(NT_STATUS_ACCOUNT_RESTRICTION, result ) ) {
2563 result = NT_STATUS_PASSWORD_RESTRICTION;
2569 if (NT_STATUS_IS_OK(result)
2570 && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN)
2571 && lp_winbind_offline_logon()) {
2572 result = winbindd_update_creds_by_name(contact_domain, user,
2574 /* Again, this happens when we login from gdm or xdm
2575 * and the password expires, *BUT* cached crendentials
2576 * doesn't exist. winbindd_update_creds_by_name()
2577 * returns NT_STATUS_NO_SUCH_USER.
2578 * This is not a failure.
2581 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER)) {
2582 result = NT_STATUS_OK;
2585 if (!NT_STATUS_IS_OK(result)) {
2586 DEBUG(10, ("Failed to store creds: %s\n",
2587 nt_errstr(result)));
2588 goto process_result;
2592 if (!NT_STATUS_IS_OK(result) && !got_info && contact_domain) {
2594 NTSTATUS policy_ret;
2596 policy_ret = fillup_password_policy(
2597 contact_domain, state->response);
2599 /* failure of this is non critical, it will just provide no
2600 * additional information to the client why the change has
2601 * failed - Guenther */
2603 if (!NT_STATUS_IS_OK(policy_ret)) {
2604 DEBUG(10,("Failed to get password policies: %s\n", nt_errstr(policy_ret)));
2605 goto process_result;
2611 if (strequal(contact_domain->name, get_global_sam_name())) {
2612 /* FIXME: internal rpc pipe does not cache handles yet */
2614 if (is_valid_policy_hnd(&dom_pol)) {
2616 dcerpc_samr_Close(b, state->mem_ctx, &dom_pol, &_result);
2622 set_auth_errors(state->response, result);
2624 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2625 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
2628 state->response->data.auth.nt_status_string,
2629 state->response->data.auth.pam_error));
2631 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2634 enum winbindd_result winbindd_dual_pam_logoff(struct winbindd_domain *domain,
2635 struct winbindd_cli_state *state)
2637 NTSTATUS result = NT_STATUS_NOT_SUPPORTED;
2639 DEBUG(3, ("[%5lu]: pam dual logoff %s\n", (unsigned long)state->pid,
2640 state->request->data.logoff.user));
2642 if (!(state->request->flags & WBFLAG_PAM_KRB5)) {
2643 result = NT_STATUS_OK;
2644 goto process_result;
2647 if (state->request->data.logoff.krb5ccname[0] == '\0') {
2648 result = NT_STATUS_OK;
2649 goto process_result;
2654 if (state->request->data.logoff.uid == (uid_t)-1) {
2655 DEBUG(0,("winbindd_pam_logoff: invalid uid\n"));
2656 goto process_result;
2659 /* what we need here is to find the corresponding krb5 ccache name *we*
2660 * created for a given username and destroy it */
2662 if (!ccache_entry_exists(state->request->data.logoff.user)) {
2663 result = NT_STATUS_OK;
2664 DEBUG(10,("winbindd_pam_logoff: no entry found.\n"));
2665 goto process_result;
2668 if (!ccache_entry_identical(state->request->data.logoff.user,
2669 state->request->data.logoff.uid,
2670 state->request->data.logoff.krb5ccname)) {
2671 DEBUG(0,("winbindd_pam_logoff: cached entry differs.\n"));
2672 goto process_result;
2675 result = remove_ccache(state->request->data.logoff.user);
2676 if (!NT_STATUS_IS_OK(result)) {
2677 DEBUG(0,("winbindd_pam_logoff: failed to remove ccache: %s\n",
2678 nt_errstr(result)));
2679 goto process_result;
2683 * Remove any mlock'ed memory creds in the child
2684 * we might be using for krb5 ticket renewal.
2687 winbindd_delete_memory_creds(state->request->data.logoff.user);
2690 result = NT_STATUS_NOT_SUPPORTED;
2696 set_auth_errors(state->response, result);
2698 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2701 /* Change user password with auth crap*/
2703 enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domain *domainSt, struct winbindd_cli_state *state)
2706 DATA_BLOB new_nt_password;
2707 DATA_BLOB old_nt_hash_enc;
2708 DATA_BLOB new_lm_password;
2709 DATA_BLOB old_lm_hash_enc;
2710 fstring domain,user;
2711 struct policy_handle dom_pol;
2712 struct winbindd_domain *contact_domain = domainSt;
2713 struct rpc_pipe_client *cli = NULL;
2714 struct dcerpc_binding_handle *b = NULL;
2716 ZERO_STRUCT(dom_pol);
2718 /* Ensure null termination */
2719 state->request->data.chng_pswd_auth_crap.user[
2720 sizeof(state->request->data.chng_pswd_auth_crap.user)-1]=0;
2721 state->request->data.chng_pswd_auth_crap.domain[
2722 sizeof(state->request->data.chng_pswd_auth_crap.domain)-1]=0;
2726 DEBUG(3, ("[%5lu]: pam change pswd auth crap domain: %s user: %s\n",
2727 (unsigned long)state->pid,
2728 state->request->data.chng_pswd_auth_crap.domain,
2729 state->request->data.chng_pswd_auth_crap.user));
2731 if (lp_winbind_offline_logon()) {
2732 DEBUG(0,("Refusing password change as winbind offline logons are enabled. "));
2733 DEBUGADD(0,("Changing passwords here would risk inconsistent logons\n"));
2734 result = NT_STATUS_ACCESS_DENIED;
2738 if (*state->request->data.chng_pswd_auth_crap.domain) {
2739 fstrcpy(domain,state->request->data.chng_pswd_auth_crap.domain);
2741 parse_domain_user(state->request->data.chng_pswd_auth_crap.user,
2745 DEBUG(3,("no domain specified with username (%s) - "
2747 state->request->data.chng_pswd_auth_crap.user));
2748 result = NT_STATUS_NO_SUCH_USER;
2753 if (!*domain && lp_winbind_use_default_domain()) {
2754 fstrcpy(domain,lp_workgroup());
2758 fstrcpy(user, state->request->data.chng_pswd_auth_crap.user);
2761 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n",
2762 (unsigned long)state->pid, domain, user));
2764 /* Change password */
2765 new_nt_password = data_blob_const(
2766 state->request->data.chng_pswd_auth_crap.new_nt_pswd,
2767 state->request->data.chng_pswd_auth_crap.new_nt_pswd_len);
2769 old_nt_hash_enc = data_blob_const(
2770 state->request->data.chng_pswd_auth_crap.old_nt_hash_enc,
2771 state->request->data.chng_pswd_auth_crap.old_nt_hash_enc_len);
2773 if(state->request->data.chng_pswd_auth_crap.new_lm_pswd_len > 0) {
2774 new_lm_password = data_blob_const(
2775 state->request->data.chng_pswd_auth_crap.new_lm_pswd,
2776 state->request->data.chng_pswd_auth_crap.new_lm_pswd_len);
2778 old_lm_hash_enc = data_blob_const(
2779 state->request->data.chng_pswd_auth_crap.old_lm_hash_enc,
2780 state->request->data.chng_pswd_auth_crap.old_lm_hash_enc_len);
2782 new_lm_password = data_blob_null;
2783 old_lm_hash_enc = data_blob_null;
2786 /* Get sam handle */
2788 result = cm_connect_sam(contact_domain, state->mem_ctx, true, &cli, &dom_pol);
2789 if (!NT_STATUS_IS_OK(result)) {
2790 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
2794 b = cli->binding_handle;
2796 result = rpccli_samr_chng_pswd_auth_crap(
2797 cli, state->mem_ctx, user, new_nt_password, old_nt_hash_enc,
2798 new_lm_password, old_lm_hash_enc);
2802 if (strequal(contact_domain->name, get_global_sam_name())) {
2803 /* FIXME: internal rpc pipe does not cache handles yet */
2805 if (is_valid_policy_hnd(&dom_pol)) {
2807 dcerpc_samr_Close(b, state->mem_ctx, &dom_pol, &_result);
2813 set_auth_errors(state->response, result);
2815 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2816 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
2818 state->response->data.auth.nt_status_string,
2819 state->response->data.auth.pam_error));
2821 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2825 static NTSTATUS extract_pac_vrfy_sigs(TALLOC_CTX *mem_ctx, DATA_BLOB pac_blob,
2826 struct PAC_DATA **p_pac_data)
2828 krb5_context krbctx = NULL;
2829 krb5_error_code k5ret;
2831 krb5_kt_cursor cursor;
2832 krb5_keytab_entry entry;
2833 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2836 ZERO_STRUCT(cursor);
2838 k5ret = krb5_init_context(&krbctx);
2840 DEBUG(1, ("Failed to initialize kerberos context: %s\n",
2841 error_message(k5ret)));
2842 status = krb5_to_nt_status(k5ret);
2846 k5ret = gse_krb5_get_server_keytab(krbctx, &keytab);
2848 DEBUG(1, ("Failed to get keytab: %s\n",
2849 error_message(k5ret)));
2850 status = krb5_to_nt_status(k5ret);
2854 k5ret = krb5_kt_start_seq_get(krbctx, keytab, &cursor);
2856 DEBUG(1, ("Failed to start seq: %s\n",
2857 error_message(k5ret)));
2858 status = krb5_to_nt_status(k5ret);
2862 k5ret = krb5_kt_next_entry(krbctx, keytab, &entry, &cursor);
2863 while (k5ret == 0) {
2864 status = kerberos_decode_pac(mem_ctx,
2867 NULL, /* krbtgt_keyblock */
2868 KRB5_KT_KEY(&entry), /* service_keyblock */
2869 NULL, /* client_principal */
2870 0, /* tgs_authtime */
2872 if (NT_STATUS_IS_OK(status)) {
2875 k5ret = smb_krb5_kt_free_entry(krbctx, &entry);
2876 k5ret = krb5_kt_next_entry(krbctx, keytab, &entry, &cursor);
2879 k5ret = krb5_kt_end_seq_get(krbctx, keytab, &cursor);
2881 DEBUG(1, ("Failed to end seq: %s\n",
2882 error_message(k5ret)));
2885 k5ret = krb5_kt_close(krbctx, keytab);
2887 DEBUG(1, ("Failed to close keytab: %s\n",
2888 error_message(k5ret)));
2891 krb5_free_context(krbctx);
2896 NTSTATUS winbindd_pam_auth_pac_verify(struct winbindd_cli_state *state,
2898 uint16_t *p_validation_level,
2899 union netr_Validation **p_validation)
2901 struct winbindd_request *req = state->request;
2903 struct PAC_DATA *pac_data = NULL;
2904 struct PAC_LOGON_INFO *logon_info = NULL;
2905 struct PAC_UPN_DNS_INFO *upn_dns_info = NULL;
2906 struct netr_SamInfo6 *info6 = NULL;
2907 uint16_t validation_level = 0;
2908 union netr_Validation *validation = NULL;
2909 struct netr_SamInfo3 *info3_copy = NULL;
2911 bool is_trusted = false;
2914 *p_is_trusted = false;
2915 *p_validation_level = 0;
2916 *p_validation = NULL;
2918 pac_blob = data_blob_const(req->extra_data.data, req->extra_len);
2919 result = extract_pac_vrfy_sigs(state->mem_ctx, pac_blob, &pac_data);
2920 if (NT_STATUS_IS_OK(result)) {
2923 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
2924 /* Try without signature verification */
2925 result = kerberos_decode_pac(state->mem_ctx,
2927 NULL, /* krb5_context */
2928 NULL, /* krbtgt_keyblock */
2929 NULL, /* service_keyblock */
2930 NULL, /* client_principal */
2931 0, /* tgs_authtime */
2934 if (!NT_STATUS_IS_OK(result)) {
2935 DEBUG(1, ("Error during PAC signature verification: %s\n",
2936 nt_errstr(result)));
2940 for (i=0; i < pac_data->num_buffers; i++) {
2941 if (pac_data->buffers[i].type == PAC_TYPE_LOGON_INFO) {
2942 logon_info = pac_data->buffers[i].info->logon_info.info;
2945 if (pac_data->buffers[i].type == PAC_TYPE_UPN_DNS_INFO) {
2946 upn_dns_info = &pac_data->buffers[i].info->upn_dns_info;
2951 result = create_info6_from_pac(state->mem_ctx,
2955 if (!NT_STATUS_IS_OK(result)) {
2959 result = map_info6_to_validation(state->mem_ctx,
2963 if (!NT_STATUS_IS_OK(result)) {
2967 result = map_validation_to_info3(state->mem_ctx,
2971 if (!NT_STATUS_IS_OK(result)) {
2977 * Signature verification succeeded, we can
2978 * trust the PAC and prime the netsamlogon
2979 * and name2sid caches. DO NOT DO THIS
2980 * in the signature verification failed
2983 struct winbindd_domain *domain = NULL;
2985 netsamlogon_cache_store(NULL, info3_copy);
2988 * We're in the parent here, so find the child
2989 * pointer from the PAC domain name.
2991 domain = find_lookup_domain_from_name(
2992 info3_copy->base.logon_domain.string);
2993 if (domain && domain->primary ) {
2994 struct dom_sid user_sid;
2996 sid_compose(&user_sid,
2997 info3_copy->base.domain_sid,
2998 info3_copy->base.rid);
3000 cache_name2sid_trusted(domain,
3001 info3_copy->base.logon_domain.string,
3002 info3_copy->base.account_name.string,
3006 DBG_INFO("PAC for user %s\\%s SID %s primed cache\n",
3007 info3_copy->base.logon_domain.string,
3008 info3_copy->base.account_name.string,
3009 sid_string_dbg(&user_sid));
3013 *p_is_trusted = is_trusted;
3014 *p_validation_level = validation_level;
3015 *p_validation = validation;
3016 return NT_STATUS_OK;
3018 #else /* HAVE_KRB5 */
3019 NTSTATUS winbindd_pam_auth_pac_verify(struct winbindd_cli_state *state,
3021 uint16_t *p_validation_level,
3022 union netr_Validation **p_validation);
3025 *p_is_trusted = false;
3026 *p_validation_level = 0;
3027 *p_validation = NULL;
3028 return NT_STATUS_NO_SUCH_USER;
3030 #endif /* HAVE_KRB5 */