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 struct netr_SamInfo3 *info3)
60 resp->data.auth.info3.logon_time =
61 nt_time_to_unix(info3->base.logon_time);
62 resp->data.auth.info3.logoff_time =
63 nt_time_to_unix(info3->base.logoff_time);
64 resp->data.auth.info3.kickoff_time =
65 nt_time_to_unix(info3->base.kickoff_time);
66 resp->data.auth.info3.pass_last_set_time =
67 nt_time_to_unix(info3->base.last_password_change);
68 resp->data.auth.info3.pass_can_change_time =
69 nt_time_to_unix(info3->base.allow_password_change);
70 resp->data.auth.info3.pass_must_change_time =
71 nt_time_to_unix(info3->base.force_password_change);
73 resp->data.auth.info3.logon_count = info3->base.logon_count;
74 resp->data.auth.info3.bad_pw_count = info3->base.bad_password_count;
76 resp->data.auth.info3.user_rid = info3->base.rid;
77 resp->data.auth.info3.group_rid = info3->base.primary_gid;
78 sid_to_fstring(resp->data.auth.info3.dom_sid, info3->base.domain_sid);
80 resp->data.auth.info3.num_groups = info3->base.groups.count;
81 resp->data.auth.info3.user_flgs = info3->base.user_flags;
83 resp->data.auth.info3.acct_flags = info3->base.acct_flags;
84 resp->data.auth.info3.num_other_sids = info3->sidcount;
86 fstrcpy(resp->data.auth.info3.user_name,
87 info3->base.account_name.string);
88 fstrcpy(resp->data.auth.info3.full_name,
89 info3->base.full_name.string);
90 fstrcpy(resp->data.auth.info3.logon_script,
91 info3->base.logon_script.string);
92 fstrcpy(resp->data.auth.info3.profile_path,
93 info3->base.profile_path.string);
94 fstrcpy(resp->data.auth.info3.home_dir,
95 info3->base.home_directory.string);
96 fstrcpy(resp->data.auth.info3.dir_drive,
97 info3->base.home_drive.string);
99 fstrcpy(resp->data.auth.info3.logon_srv,
100 info3->base.logon_server.string);
101 fstrcpy(resp->data.auth.info3.logon_dom,
102 info3->base.logon_domain.string);
104 ex = talloc_strdup(mem_ctx, "");
105 NT_STATUS_HAVE_NO_MEMORY(ex);
107 for (i=0; i < info3->base.groups.count; i++) {
108 ex = talloc_asprintf_append_buffer(ex, "0x%08X:0x%08X\n",
109 info3->base.groups.rids[i].rid,
110 info3->base.groups.rids[i].attributes);
111 NT_STATUS_HAVE_NO_MEMORY(ex);
114 for (i=0; i < info3->sidcount; i++) {
117 sid = dom_sid_string(mem_ctx, info3->sids[i].sid);
118 NT_STATUS_HAVE_NO_MEMORY(sid);
120 ex = talloc_asprintf_append_buffer(ex, "%s:0x%08X\n",
122 info3->sids[i].attributes);
123 NT_STATUS_HAVE_NO_MEMORY(ex);
128 resp->extra_data.data = ex;
129 resp->length += talloc_get_size(ex);
134 static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx,
135 struct winbindd_response *resp,
136 struct netr_SamInfo3 *info3)
139 enum ndr_err_code ndr_err;
141 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, info3,
142 (ndr_push_flags_fn_t)ndr_push_netr_SamInfo3);
143 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
144 DEBUG(0,("append_info3_as_ndr: failed to append\n"));
145 return ndr_map_error2ntstatus(ndr_err);
148 resp->extra_data.data = blob.data;
149 resp->length += blob.length;
154 static NTSTATUS append_unix_username(TALLOC_CTX *mem_ctx,
155 struct winbindd_response *resp,
156 const struct netr_SamInfo3 *info3,
157 const char *name_domain,
158 const char *name_user)
160 /* We've been asked to return the unix username, per
161 'winbind use default domain' settings and the like */
163 const char *nt_username, *nt_domain;
165 nt_domain = talloc_strdup(mem_ctx, info3->base.logon_domain.string);
167 /* If the server didn't give us one, just use the one
169 nt_domain = name_domain;
172 nt_username = talloc_strdup(mem_ctx, info3->base.account_name.string);
174 /* If the server didn't give us one, just use the one
176 nt_username = name_user;
179 fill_domain_username(resp->data.auth.unix_username,
180 nt_domain, nt_username, true);
182 DEBUG(5, ("Setting unix username to [%s]\n",
183 resp->data.auth.unix_username));
188 static NTSTATUS append_afs_token(TALLOC_CTX *mem_ctx,
189 struct winbindd_response *resp,
190 const struct netr_SamInfo3 *info3,
191 const char *name_domain,
192 const char *name_user)
194 char *afsname = NULL;
198 afsname = talloc_strdup(mem_ctx, lp_afs_username_map());
199 if (afsname == NULL) {
200 return NT_STATUS_NO_MEMORY;
203 afsname = talloc_string_sub(mem_ctx,
204 lp_afs_username_map(),
206 afsname = talloc_string_sub(mem_ctx, afsname,
208 afsname = talloc_string_sub(mem_ctx, afsname,
212 struct dom_sid user_sid;
215 sid_compose(&user_sid, info3->base.domain_sid,
217 sid_to_fstring(sidstr, &user_sid);
218 afsname = talloc_string_sub(mem_ctx, afsname,
222 if (afsname == NULL) {
223 return NT_STATUS_NO_MEMORY;
226 if (!strlower_m(afsname)) {
227 return NT_STATUS_INVALID_PARAMETER;
230 DEBUG(10, ("Generating token for user %s\n", afsname));
232 cell = strchr(afsname, '@');
235 return NT_STATUS_NO_MEMORY;
241 token = afs_createtoken_str(afsname, cell);
245 resp->extra_data.data = talloc_strdup(mem_ctx, token);
246 if (resp->extra_data.data == NULL) {
247 return NT_STATUS_NO_MEMORY;
249 resp->length += strlen((const char *)resp->extra_data.data)+1;
254 static NTSTATUS check_info3_in_group(struct netr_SamInfo3 *info3,
255 const char *group_sid)
257 * Check whether a user belongs to a group or list of groups.
259 * @param mem_ctx talloc memory context.
260 * @param info3 user information, including group membership info.
261 * @param group_sid One or more groups , separated by commas.
263 * @return NT_STATUS_OK on success,
264 * NT_STATUS_LOGON_FAILURE if the user does not belong,
265 * or other NT_STATUS_IS_ERR(status) for other kinds of failure.
268 struct dom_sid *require_membership_of_sid;
269 uint32_t num_require_membership_of_sid;
274 struct security_token *token;
275 TALLOC_CTX *frame = talloc_stackframe();
278 /* Parse the 'required group' SID */
280 if (!group_sid || !group_sid[0]) {
281 /* NO sid supplied, all users may access */
286 token = talloc_zero(talloc_tos(), struct security_token);
288 DEBUG(0, ("talloc failed\n"));
290 return NT_STATUS_NO_MEMORY;
293 num_require_membership_of_sid = 0;
294 require_membership_of_sid = NULL;
298 while (next_token_talloc(talloc_tos(), &p, &req_sid, ",")) {
299 if (!string_to_sid(&sid, req_sid)) {
300 DEBUG(0, ("check_info3_in_group: could not parse %s "
301 "as a SID!", req_sid));
303 return NT_STATUS_INVALID_PARAMETER;
306 status = add_sid_to_array(talloc_tos(), &sid,
307 &require_membership_of_sid,
308 &num_require_membership_of_sid);
309 if (!NT_STATUS_IS_OK(status)) {
310 DEBUG(0, ("add_sid_to_array failed\n"));
316 status = sid_array_from_info3(talloc_tos(), info3,
320 if (!NT_STATUS_IS_OK(status)) {
325 if (!NT_STATUS_IS_OK(status = add_aliases(get_global_sam_sid(),
327 || !NT_STATUS_IS_OK(status = add_aliases(&global_sid_Builtin,
329 DEBUG(3, ("could not add aliases: %s\n",
335 security_token_debug(DBGC_CLASS, 10, token);
337 for (i=0; i<num_require_membership_of_sid; i++) {
338 DEBUG(10, ("Checking SID %s\n", sid_string_dbg(
339 &require_membership_of_sid[i])));
340 if (nt_token_check_sid(&require_membership_of_sid[i],
342 DEBUG(10, ("Access ok\n"));
348 /* Do not distinguish this error from a wrong username/pw */
351 return NT_STATUS_LOGON_FAILURE;
354 struct winbindd_domain *find_auth_domain(uint8_t flags,
355 const char *domain_name)
357 struct winbindd_domain *domain;
360 domain = find_domain_from_name_noinit(domain_name);
361 if (domain == NULL) {
362 DEBUG(3, ("Authentication for domain [%s] refused "
363 "as it is not a trusted domain\n",
369 if (strequal(domain_name, get_global_sam_name())) {
370 return find_domain_from_name_noinit(domain_name);
373 /* we can auth against trusted domains */
374 if (flags & WBFLAG_PAM_CONTACT_TRUSTDOM) {
375 domain = find_domain_from_name_noinit(domain_name);
376 if (domain == NULL) {
377 DEBUG(3, ("Authentication for domain [%s] skipped "
378 "as it is not a trusted domain\n",
385 return find_our_domain();
388 static void fake_password_policy(struct winbindd_response *r,
389 const struct netr_SamBaseInfo *bi)
391 NTTIME min_password_age;
392 NTTIME max_password_age;
394 if (bi->allow_password_change > bi->last_password_change) {
395 min_password_age = bi->allow_password_change -
396 bi->last_password_change;
398 min_password_age = 0;
401 if (bi->force_password_change > bi->last_password_change) {
402 max_password_age = bi->force_password_change -
403 bi->last_password_change;
405 max_password_age = 0;
408 r->data.auth.policy.min_length_password = 0;
409 r->data.auth.policy.password_history = 0;
410 r->data.auth.policy.password_properties = 0;
411 r->data.auth.policy.expire =
412 nt_time_to_unix_abs(&max_password_age);
413 r->data.auth.policy.min_passwordage =
414 nt_time_to_unix_abs(&min_password_age);
417 static void fill_in_password_policy(struct winbindd_response *r,
418 const struct samr_DomInfo1 *p)
420 r->data.auth.policy.min_length_password =
421 p->min_password_length;
422 r->data.auth.policy.password_history =
423 p->password_history_length;
424 r->data.auth.policy.password_properties =
425 p->password_properties;
426 r->data.auth.policy.expire =
427 nt_time_to_unix_abs((const NTTIME *)&(p->max_password_age));
428 r->data.auth.policy.min_passwordage =
429 nt_time_to_unix_abs((const NTTIME *)&(p->min_password_age));
432 static NTSTATUS fillup_password_policy(struct winbindd_domain *domain,
433 struct winbindd_response *response)
435 TALLOC_CTX *frame = talloc_stackframe();
437 struct samr_DomInfo1 password_policy;
439 if ( !winbindd_can_contact_domain( domain ) ) {
440 DEBUG(5,("fillup_password_policy: No inbound trust to "
441 "contact domain %s\n", domain->name));
442 status = NT_STATUS_NOT_SUPPORTED;
446 status = wb_cache_password_policy(domain, talloc_tos(),
448 if (NT_STATUS_IS_ERR(status)) {
452 fill_in_password_policy(response, &password_policy);
459 static NTSTATUS get_max_bad_attempts_from_lockout_policy(struct winbindd_domain *domain,
461 uint16_t *lockout_threshold)
463 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
464 struct samr_DomInfo12 lockout_policy;
466 *lockout_threshold = 0;
468 status = wb_cache_lockout_policy(domain, mem_ctx, &lockout_policy);
469 if (NT_STATUS_IS_ERR(status)) {
473 *lockout_threshold = lockout_policy.lockout_threshold;
478 static NTSTATUS get_pwd_properties(struct winbindd_domain *domain,
480 uint32_t *password_properties)
482 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
483 struct samr_DomInfo1 password_policy;
485 *password_properties = 0;
487 status = wb_cache_password_policy(domain, mem_ctx, &password_policy);
488 if (NT_STATUS_IS_ERR(status)) {
492 *password_properties = password_policy.password_properties;
499 static const char *generate_krb5_ccache(TALLOC_CTX *mem_ctx,
502 const char **user_ccache_file)
504 /* accept FILE and WRFILE as krb5_cc_type from the client and then
505 * build the full ccname string based on the user's uid here -
508 const char *gen_cc = NULL;
511 if (strequal(type, "FILE")) {
512 gen_cc = talloc_asprintf(
513 mem_ctx, "FILE:/tmp/krb5cc_%d", uid);
515 if (strequal(type, "WRFILE")) {
516 gen_cc = talloc_asprintf(
517 mem_ctx, "WRFILE:/tmp/krb5cc_%d", uid);
519 if (strequal(type, "KEYRING")) {
520 gen_cc = talloc_asprintf(
521 mem_ctx, "KEYRING:persistent:%d", uid);
524 if (strnequal(type, "FILE:/", 6) ||
525 strnequal(type, "WRFILE:/", 8) ||
526 strnequal(type, "DIR:/", 5)) {
528 /* we allow only one "%u" substitution */
532 p = strchr(type, '%');
537 if (p != NULL && *p == 'u' && strchr(p, '%') == NULL) {
538 char uid_str[sizeof("18446744073709551615")];
540 snprintf(uid_str, sizeof(uid_str), "%u", uid);
542 gen_cc = talloc_string_sub2(mem_ctx,
546 /* remove_unsafe_characters */
550 /* allow_trailing_dollar */
557 *user_ccache_file = gen_cc;
559 if (gen_cc == NULL) {
560 gen_cc = talloc_strdup(mem_ctx, "MEMORY:winbindd_pam_ccache");
562 if (gen_cc == NULL) {
563 DEBUG(0,("out of memory\n"));
567 DEBUG(10, ("using ccache: %s%s\n", gen_cc,
568 (*user_ccache_file == NULL) ? " (internal)":""));
575 uid_t get_uid_from_request(struct winbindd_request *request)
579 uid = request->data.auth.uid;
581 if (uid == (uid_t)-1) {
582 DEBUG(1,("invalid uid: '%u'\n", (unsigned int)uid));
588 /**********************************************************************
589 Authenticate a user with a clear text password using Kerberos and fill up
591 **********************************************************************/
593 static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
594 struct winbindd_domain *domain,
597 const char *krb5_cc_type,
599 struct netr_SamInfo3 **info3,
603 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
604 krb5_error_code krb5_ret;
605 const char *cc = NULL;
606 const char *principal_s = NULL;
607 const char *service = NULL;
609 fstring name_domain, name_user;
610 time_t ticket_lifetime = 0;
611 time_t renewal_until = 0;
613 time_t time_offset = 0;
614 const char *user_ccache_file;
615 struct PAC_LOGON_INFO *logon_info = NULL;
616 struct PAC_DATA *pac_data = NULL;
617 struct PAC_DATA_CTR *pac_data_ctr = NULL;
618 const char *local_service;
620 struct netr_SamInfo3 *info3_copy = NULL;
624 if (domain->alt_name == NULL) {
625 return NT_STATUS_INVALID_PARAMETER;
629 * prepare a krb5_cc_cache string for the user */
632 DEBUG(0,("no valid uid\n"));
635 cc = generate_krb5_ccache(mem_ctx,
640 return NT_STATUS_NO_MEMORY;
645 * get kerberos properties */
647 if (domain->private_data) {
648 ads = (ADS_STRUCT *)domain->private_data;
649 time_offset = ads->auth.time_offset;
654 * do kerberos auth and setup ccache as the user */
656 parse_domain_user(user, name_domain, name_user);
658 realm = talloc_strdup(mem_ctx, domain->alt_name);
660 return NT_STATUS_NO_MEMORY;
663 if (!strupper_m(realm)) {
664 return NT_STATUS_INVALID_PARAMETER;
667 principal_s = talloc_asprintf(mem_ctx, "%s@%s", name_user, realm);
668 if (principal_s == NULL) {
669 return NT_STATUS_NO_MEMORY;
672 service = talloc_asprintf(mem_ctx, "%s/%s@%s", KRB5_TGS_NAME, realm, realm);
673 if (service == NULL) {
674 return NT_STATUS_NO_MEMORY;
677 local_service = talloc_asprintf(mem_ctx, "%s$@%s",
678 lp_netbios_name(), lp_realm());
679 if (local_service == NULL) {
680 return NT_STATUS_NO_MEMORY;
684 /* if this is a user ccache, we need to act as the user to let the krb5
685 * library handle the chown, etc. */
687 /************************ ENTERING NON-ROOT **********************/
689 if (user_ccache_file != NULL) {
690 set_effective_uid(uid);
691 DEBUG(10,("winbindd_raw_kerberos_login: uid is %d\n", uid));
694 result = kerberos_return_pac(mem_ctx,
703 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
707 if (user_ccache_file != NULL) {
708 gain_root_privilege();
711 /************************ RETURNED TO ROOT **********************/
713 if (!NT_STATUS_IS_OK(result)) {
717 if (pac_data_ctr == NULL) {
721 pac_data = pac_data_ctr->pac_data;
722 if (pac_data == NULL) {
726 for (i=0; i < pac_data->num_buffers; i++) {
728 if (pac_data->buffers[i].type != PAC_TYPE_LOGON_INFO) {
732 logon_info = pac_data->buffers[i].info->logon_info.info;
734 return NT_STATUS_INVALID_PARAMETER;
740 if (logon_info == NULL) {
741 DEBUG(10,("Missing logon_info in ticket of %s\n",
743 return NT_STATUS_INVALID_PARAMETER;
746 DEBUG(10,("winbindd_raw_kerberos_login: winbindd validated ticket of %s\n",
749 result = create_info3_from_pac_logon_info(mem_ctx, logon_info, &info3_copy);
750 if (!NT_STATUS_IS_OK(result)) {
754 /* if we had a user's ccache then return that string for the pam
757 if (user_ccache_file != NULL) {
759 fstrcpy(krb5ccname, user_ccache_file);
761 result = add_ccache_to_list(principal_s,
773 if (!NT_STATUS_IS_OK(result)) {
774 DEBUG(10,("winbindd_raw_kerberos_login: failed to add ccache to list: %s\n",
779 /* need to delete the memory cred cache, it is not used anymore */
781 krb5_ret = ads_kdestroy(cc);
783 DEBUG(3,("winbindd_raw_kerberos_login: "
784 "could not destroy krb5 credential cache: "
785 "%s\n", error_message(krb5_ret)));
794 * Do not delete an existing valid credential cache, if the user
795 * e.g. enters a wrong password
797 if ((strequal(krb5_cc_type, "FILE") || strequal(krb5_cc_type, "WRFILE"))
798 && user_ccache_file != NULL) {
802 /* we could have created a new credential cache with a valid tgt in it
803 * but we werent able to get or verify the service ticket for this
804 * local host and therefor didn't get the PAC, we need to remove that
805 * cache entirely now */
807 krb5_ret = ads_kdestroy(cc);
809 DEBUG(3,("winbindd_raw_kerberos_login: "
810 "could not destroy krb5 credential cache: "
811 "%s\n", error_message(krb5_ret)));
814 if (!NT_STATUS_IS_OK(remove_ccache(user))) {
815 DEBUG(3,("winbindd_raw_kerberos_login: "
816 "could not remove ccache for user %s\n",
822 return NT_STATUS_NOT_SUPPORTED;
823 #endif /* HAVE_KRB5 */
826 /****************************************************************
827 ****************************************************************/
829 bool check_request_flags(uint32_t flags)
831 uint32_t flags_edata = WBFLAG_PAM_AFS_TOKEN |
832 WBFLAG_PAM_INFO3_TEXT |
833 WBFLAG_PAM_INFO3_NDR;
835 if ( ( (flags & flags_edata) == WBFLAG_PAM_AFS_TOKEN) ||
836 ( (flags & flags_edata) == WBFLAG_PAM_INFO3_NDR) ||
837 ( (flags & flags_edata) == WBFLAG_PAM_INFO3_TEXT)||
838 !(flags & flags_edata) ) {
842 DEBUG(1, ("check_request_flags: invalid request flags[0x%08X]\n",
848 /****************************************************************
849 ****************************************************************/
851 NTSTATUS append_auth_data(TALLOC_CTX *mem_ctx,
852 struct winbindd_response *resp,
853 uint32_t request_flags,
854 struct netr_SamInfo3 *info3,
855 const char *name_domain,
856 const char *name_user)
860 if (request_flags & WBFLAG_PAM_USER_SESSION_KEY) {
861 memcpy(resp->data.auth.user_session_key,
863 sizeof(resp->data.auth.user_session_key)
867 if (request_flags & WBFLAG_PAM_LMKEY) {
868 memcpy(resp->data.auth.first_8_lm_hash,
869 info3->base.LMSessKey.key,
870 sizeof(resp->data.auth.first_8_lm_hash)
874 if (request_flags & WBFLAG_PAM_UNIX_NAME) {
875 result = append_unix_username(mem_ctx, resp,
876 info3, name_domain, name_user);
877 if (!NT_STATUS_IS_OK(result)) {
878 DEBUG(10,("Failed to append Unix Username: %s\n",
884 /* currently, anything from here on potentially overwrites extra_data. */
886 if (request_flags & WBFLAG_PAM_INFO3_NDR) {
887 result = append_info3_as_ndr(mem_ctx, resp, info3);
888 if (!NT_STATUS_IS_OK(result)) {
889 DEBUG(10,("Failed to append INFO3 (NDR): %s\n",
895 if (request_flags & WBFLAG_PAM_INFO3_TEXT) {
896 result = append_info3_as_txt(mem_ctx, resp, info3);
897 if (!NT_STATUS_IS_OK(result)) {
898 DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
904 if (request_flags & WBFLAG_PAM_AFS_TOKEN) {
905 result = append_afs_token(mem_ctx, resp,
906 info3, name_domain, name_user);
907 if (!NT_STATUS_IS_OK(result)) {
908 DEBUG(10,("Failed to append AFS token: %s\n",
917 static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
918 struct winbindd_cli_state *state,
919 struct netr_SamInfo3 **info3)
921 NTSTATUS result = NT_STATUS_LOGON_FAILURE;
922 uint16_t max_allowed_bad_attempts;
923 fstring name_domain, name_user;
925 enum lsa_SidType type;
926 uchar new_nt_pass[NT_HASH_LEN];
927 const uint8_t *cached_nt_pass;
928 const uint8_t *cached_salt;
929 struct netr_SamInfo3 *my_info3;
930 time_t kickoff_time, must_change_time;
931 bool password_good = false;
933 struct winbindd_tdc_domain *tdc_domain = NULL;
940 DEBUG(10,("winbindd_dual_pam_auth_cached\n"));
942 /* Parse domain and username */
944 parse_domain_user(state->request->data.auth.user, name_domain, name_user);
947 if (!lookup_cached_name(name_domain,
951 DEBUG(10,("winbindd_dual_pam_auth_cached: no such user in the cache\n"));
952 return NT_STATUS_NO_SUCH_USER;
955 if (type != SID_NAME_USER) {
956 DEBUG(10,("winbindd_dual_pam_auth_cached: not a user (%s)\n", sid_type_lookup(type)));
957 return NT_STATUS_LOGON_FAILURE;
960 result = winbindd_get_creds(domain,
966 if (!NT_STATUS_IS_OK(result)) {
967 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get creds: %s\n", nt_errstr(result)));
973 E_md4hash(state->request->data.auth.pass, new_nt_pass);
975 dump_data_pw("new_nt_pass", new_nt_pass, NT_HASH_LEN);
976 dump_data_pw("cached_nt_pass", cached_nt_pass, NT_HASH_LEN);
978 dump_data_pw("cached_salt", cached_salt, NT_HASH_LEN);
982 /* In this case we didn't store the nt_hash itself,
983 but the MD5 combination of salt + nt_hash. */
984 uchar salted_hash[NT_HASH_LEN];
985 E_md5hash(cached_salt, new_nt_pass, salted_hash);
987 password_good = (memcmp(cached_nt_pass, salted_hash,
990 /* Old cached cred - direct store of nt_hash (bad bad bad !). */
991 password_good = (memcmp(cached_nt_pass, new_nt_pass,
997 /* User *DOES* know the password, update logon_time and reset
1000 my_info3->base.user_flags |= NETLOGON_CACHED_ACCOUNT;
1002 if (my_info3->base.acct_flags & ACB_AUTOLOCK) {
1003 return NT_STATUS_ACCOUNT_LOCKED_OUT;
1006 if (my_info3->base.acct_flags & ACB_DISABLED) {
1007 return NT_STATUS_ACCOUNT_DISABLED;
1010 if (my_info3->base.acct_flags & ACB_WSTRUST) {
1011 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
1014 if (my_info3->base.acct_flags & ACB_SVRTRUST) {
1015 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
1018 if (my_info3->base.acct_flags & ACB_DOMTRUST) {
1019 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
1022 if (!(my_info3->base.acct_flags & ACB_NORMAL)) {
1023 DEBUG(0,("winbindd_dual_pam_auth_cached: whats wrong with that one?: 0x%08x\n",
1024 my_info3->base.acct_flags));
1025 return NT_STATUS_LOGON_FAILURE;
1028 kickoff_time = nt_time_to_unix(my_info3->base.kickoff_time);
1029 if (kickoff_time != 0 && time(NULL) > kickoff_time) {
1030 return NT_STATUS_ACCOUNT_EXPIRED;
1033 must_change_time = nt_time_to_unix(my_info3->base.force_password_change);
1034 if (must_change_time != 0 && must_change_time < time(NULL)) {
1035 /* we allow grace logons when the password has expired */
1036 my_info3->base.user_flags |= NETLOGON_GRACE_LOGON;
1037 /* return NT_STATUS_PASSWORD_EXPIRED; */
1042 if ((state->request->flags & WBFLAG_PAM_KRB5) &&
1043 ((tdc_domain = wcache_tdc_fetch_domain(state->mem_ctx, name_domain)) != NULL) &&
1044 ((tdc_domain->trust_type & LSA_TRUST_TYPE_UPLEVEL) ||
1045 /* used to cope with the case winbindd starting without network. */
1046 !strequal(tdc_domain->domain_name, tdc_domain->dns_name))) {
1049 const char *cc = NULL;
1051 const char *principal_s = NULL;
1052 const char *service = NULL;
1053 const char *user_ccache_file;
1055 if (domain->alt_name == NULL) {
1056 return NT_STATUS_INVALID_PARAMETER;
1059 uid = get_uid_from_request(state->request);
1061 DEBUG(0,("winbindd_dual_pam_auth_cached: invalid uid\n"));
1062 return NT_STATUS_INVALID_PARAMETER;
1065 cc = generate_krb5_ccache(state->mem_ctx,
1066 state->request->data.auth.krb5_cc_type,
1067 state->request->data.auth.uid,
1070 return NT_STATUS_NO_MEMORY;
1073 realm = talloc_strdup(state->mem_ctx, domain->alt_name);
1074 if (realm == NULL) {
1075 return NT_STATUS_NO_MEMORY;
1078 if (!strupper_m(realm)) {
1079 return NT_STATUS_INVALID_PARAMETER;
1082 principal_s = talloc_asprintf(state->mem_ctx, "%s@%s", name_user, realm);
1083 if (principal_s == NULL) {
1084 return NT_STATUS_NO_MEMORY;
1087 service = talloc_asprintf(state->mem_ctx, "%s/%s@%s", KRB5_TGS_NAME, realm, realm);
1088 if (service == NULL) {
1089 return NT_STATUS_NO_MEMORY;
1092 if (user_ccache_file != NULL) {
1094 fstrcpy(state->response->data.auth.krb5ccname,
1097 result = add_ccache_to_list(principal_s,
1100 state->request->data.auth.user,
1101 state->request->data.auth.pass,
1105 time(NULL) + lp_winbind_cache_time(),
1106 time(NULL) + WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
1109 if (!NT_STATUS_IS_OK(result)) {
1110 DEBUG(10,("winbindd_dual_pam_auth_cached: failed "
1111 "to add ccache to list: %s\n",
1112 nt_errstr(result)));
1116 #endif /* HAVE_KRB5 */
1118 /* FIXME: we possibly should handle logon hours as well (does xp when
1119 * offline?) see auth/auth_sam.c:sam_account_ok for details */
1121 unix_to_nt_time(&my_info3->base.logon_time, time(NULL));
1122 my_info3->base.bad_password_count = 0;
1124 result = winbindd_update_creds_by_info3(domain,
1125 state->request->data.auth.user,
1126 state->request->data.auth.pass,
1128 if (!NT_STATUS_IS_OK(result)) {
1129 DEBUG(1,("winbindd_dual_pam_auth_cached: failed to update creds: %s\n",
1130 nt_errstr(result)));
1134 return NT_STATUS_OK;
1138 /* User does *NOT* know the correct password, modify info3 accordingly, but only if online */
1139 if (domain->online == false) {
1143 /* failure of this is not critical */
1144 result = get_max_bad_attempts_from_lockout_policy(domain, state->mem_ctx, &max_allowed_bad_attempts);
1145 if (!NT_STATUS_IS_OK(result)) {
1146 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get max_allowed_bad_attempts. "
1147 "Won't be able to honour account lockout policies\n"));
1150 /* increase counter */
1151 my_info3->base.bad_password_count++;
1153 if (max_allowed_bad_attempts == 0) {
1158 if (my_info3->base.bad_password_count >= max_allowed_bad_attempts) {
1160 uint32_t password_properties;
1162 result = get_pwd_properties(domain, state->mem_ctx, &password_properties);
1163 if (!NT_STATUS_IS_OK(result)) {
1164 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get password properties.\n"));
1167 if ((my_info3->base.rid != DOMAIN_RID_ADMINISTRATOR) ||
1168 (password_properties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)) {
1169 my_info3->base.acct_flags |= ACB_AUTOLOCK;
1174 result = winbindd_update_creds_by_info3(domain,
1175 state->request->data.auth.user,
1179 if (!NT_STATUS_IS_OK(result)) {
1180 DEBUG(0,("winbindd_dual_pam_auth_cached: failed to update creds %s\n",
1181 nt_errstr(result)));
1184 return NT_STATUS_LOGON_FAILURE;
1187 static NTSTATUS winbindd_dual_pam_auth_kerberos(struct winbindd_domain *domain,
1188 struct winbindd_cli_state *state,
1189 struct netr_SamInfo3 **info3)
1191 struct winbindd_domain *contact_domain;
1192 fstring name_domain, name_user;
1195 DEBUG(10,("winbindd_dual_pam_auth_kerberos\n"));
1197 /* Parse domain and username */
1199 parse_domain_user(state->request->data.auth.user, name_domain, name_user);
1201 /* what domain should we contact? */
1204 if (!(contact_domain = find_domain_from_name(name_domain))) {
1205 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1206 state->request->data.auth.user, name_domain, name_user, name_domain));
1207 result = NT_STATUS_NO_SUCH_USER;
1212 if (is_myname(name_domain)) {
1213 DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
1214 result = NT_STATUS_NO_SUCH_USER;
1218 contact_domain = find_domain_from_name(name_domain);
1219 if (contact_domain == NULL) {
1220 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1221 state->request->data.auth.user, name_domain, name_user, name_domain));
1223 result = NT_STATUS_NO_SUCH_USER;
1228 if (contact_domain->initialized &&
1229 contact_domain->active_directory) {
1233 if (!contact_domain->initialized) {
1234 init_dc_connection(contact_domain, false);
1237 if (!contact_domain->active_directory) {
1238 DEBUG(3,("krb5 auth requested but domain is not Active Directory\n"));
1239 return NT_STATUS_INVALID_LOGON_TYPE;
1242 result = winbindd_raw_kerberos_login(
1243 state->mem_ctx, contact_domain,
1244 state->request->data.auth.user,
1245 state->request->data.auth.pass,
1246 state->request->data.auth.krb5_cc_type,
1247 get_uid_from_request(state->request),
1248 info3, state->response->data.auth.krb5ccname);
1253 static NTSTATUS winbindd_dual_auth_passdb(TALLOC_CTX *mem_ctx,
1254 uint32_t logon_parameters,
1255 const char *domain, const char *user,
1256 const DATA_BLOB *challenge,
1257 const DATA_BLOB *lm_resp,
1258 const DATA_BLOB *nt_resp,
1260 uint8_t *pauthoritative,
1261 struct netr_SamInfo3 **pinfo3)
1263 struct auth_context *auth_context;
1264 struct auth_serversupplied_info *server_info;
1265 struct auth_usersupplied_info *user_info = NULL;
1266 struct tsocket_address *local;
1267 struct netr_SamInfo3 *info3;
1271 TALLOC_CTX *frame = talloc_stackframe();
1274 * We are authoritative by default
1276 *pauthoritative = 1;
1278 rc = tsocket_address_inet_from_strings(frame,
1285 return NT_STATUS_NO_MEMORY;
1289 * TODO: We should get the service description passed in from
1290 * the winbind client, so we can have "smb2", "squid" or "samr" logged
1293 status = make_user_info(frame, &user_info, user, user, domain, domain,
1294 lp_netbios_name(), local, local,
1296 lm_resp, nt_resp, NULL, NULL,
1297 NULL, AUTH_PASSWORD_RESPONSE);
1298 if (!NT_STATUS_IS_OK(status)) {
1299 DEBUG(10, ("make_user_info failed: %s\n", nt_errstr(status)));
1304 user_info->logon_parameters = logon_parameters;
1306 /* We don't want any more mapping of the username */
1307 user_info->mapped_state = True;
1309 /* We don't want to come back to winbindd or to do PAM account checks */
1310 user_info->flags |= USER_INFO_INFO3_AND_NO_AUTHZ;
1313 user_info->flags |= USER_INFO_INTERACTIVE_LOGON;
1316 status = make_auth3_context_for_winbind(frame, &auth_context);
1317 if (!NT_STATUS_IS_OK(status)) {
1318 DBG_ERR("make_auth3_context_for_winbind failed: %s\n",
1324 ok = auth3_context_set_challenge(auth_context,
1325 challenge->data, "fixed");
1328 return NT_STATUS_NO_MEMORY;
1331 status = auth_check_ntlm_password(mem_ctx,
1336 if (!NT_STATUS_IS_OK(status)) {
1341 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
1342 if (info3 == NULL) {
1344 return NT_STATUS_NO_MEMORY;
1347 status = serverinfo_to_SamInfo3(server_info, info3);
1348 if (!NT_STATUS_IS_OK(status)) {
1351 DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
1352 nt_errstr(status)));
1357 DEBUG(10, ("Authenticaticating user %s\\%s returned %s\n", domain,
1358 user, nt_errstr(status)));
1363 static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
1364 TALLOC_CTX *mem_ctx,
1365 uint32_t logon_parameters,
1366 const char *username,
1367 const char *password,
1368 const char *domainname,
1369 const char *workstation,
1370 const uint8_t chal[8],
1371 DATA_BLOB lm_response,
1372 DATA_BLOB nt_response,
1374 uint8_t *authoritative,
1376 uint16_t *_validation_level,
1377 union netr_Validation **_validation)
1380 int netr_attempts = 0;
1383 uint16_t validation_level;
1384 union netr_Validation *validation = NULL;
1387 struct rpc_pipe_client *netlogon_pipe;
1391 result = cm_connect_netlogon(domain, &netlogon_pipe);
1393 if (NT_STATUS_EQUAL(result,
1394 NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
1396 * This means we don't have a trust account.
1399 result = NT_STATUS_NO_SUCH_USER;
1403 if (!NT_STATUS_IS_OK(result)) {
1404 DEBUG(3,("Could not open handle to NETLOGON pipe "
1405 "(error: %s, attempts: %d)\n",
1406 nt_errstr(result), netr_attempts));
1408 /* After the first retry always close the connection */
1409 if (netr_attempts > 0) {
1410 DEBUG(3, ("This is again a problem for this "
1411 "particular call, forcing the close "
1412 "of this connection\n"));
1413 invalidate_cm_connection(domain);
1416 /* After the second retry failover to the next DC */
1417 if (netr_attempts > 1) {
1419 * If the netlogon server is not reachable then
1420 * it is possible that the DC is rebuilding
1421 * sysvol and shutdown netlogon for that time.
1422 * We should failover to the next dc.
1424 DEBUG(3, ("This is the third problem for this "
1425 "particular call, adding DC to the "
1426 "negative cache list: %s %s\n", domain->name, domain->dcname));
1427 add_failed_connection_entry(domain->name,
1430 saf_delete(domain->name);
1433 /* Only allow 3 retries */
1434 if (netr_attempts < 3) {
1435 DEBUG(3, ("The connection to netlogon "
1436 "failed, retrying\n"));
1444 if (domain->conn.netlogon_creds_ctx == NULL) {
1445 DBG_NOTICE("No security credentials available for "
1446 "domain [%s]\n", domainname);
1447 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1448 } else if (interactive) {
1449 result = rpccli_netlogon_password_logon(
1450 domain->conn.netlogon_creds_ctx,
1451 netlogon_pipe->binding_handle,
1458 NetlogonInteractiveInformation,
1464 result = rpccli_netlogon_network_logon(
1465 domain->conn.netlogon_creds_ctx,
1466 netlogon_pipe->binding_handle,
1482 * we increment this after the "feature negotiation"
1483 * for can_do_samlogon_ex and can_do_validation6
1487 /* We have to try a second time as cm_connect_netlogon
1488 might not yet have noticed that the DC has killed
1491 if (!rpccli_is_connected(netlogon_pipe)) {
1496 /* if we get access denied, a possible cause was that we had
1497 and open connection to the DC, but someone changed our
1498 machine account password out from underneath us using 'net
1499 rpc changetrustpw' */
1501 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1502 DEBUG(1,("winbind_samlogon_retry_loop: sam_logon returned "
1503 "ACCESS_DENIED. Maybe the DC has Restrict "
1504 "NTLM set or the trust account "
1505 "password was changed and we didn't know it. "
1506 "Killing connections to domain %s\n",
1508 invalidate_cm_connection(domain);
1512 if (NT_STATUS_EQUAL(result, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
1514 * Got DCERPC_FAULT_OP_RNG_ERROR for SamLogon
1515 * (no Ex). This happens against old Samba
1516 * DCs, if LogonSamLogonEx() fails with an error
1517 * e.g. NT_STATUS_NO_SUCH_USER or NT_STATUS_WRONG_PASSWORD.
1519 * The server will log something like this:
1520 * api_net_sam_logon_ex: Failed to marshall NET_R_SAM_LOGON_EX.
1522 * This sets the whole connection into a fault_state mode
1523 * and all following request get NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE.
1525 * This also happens to our retry with LogonSamLogonWithFlags()
1526 * and LogonSamLogon().
1528 * In order to recover from this situation, we need to
1529 * drop the connection.
1531 invalidate_cm_connection(domain);
1532 result = NT_STATUS_LOGON_FAILURE;
1536 } while ( (attempts < 2) && retry );
1538 if (NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT)) {
1539 DEBUG(3,("winbind_samlogon_retry_loop: sam_network_logon(ex) "
1540 "returned NT_STATUS_IO_TIMEOUT after the retry. "
1541 "Killing connections to domain %s\n",
1543 invalidate_cm_connection(domain);
1546 if (!NT_STATUS_IS_OK(result)) {
1550 *_validation_level = validation_level;
1551 *_validation = validation;
1552 return NT_STATUS_OK;
1555 static NTSTATUS winbindd_dual_pam_auth_samlogon(
1556 TALLOC_CTX *mem_ctx,
1557 struct winbindd_domain *domain,
1560 uint32_t request_flags,
1561 uint16_t *_validation_level,
1562 union netr_Validation **_validation)
1568 unsigned char local_nt_response[24];
1569 fstring name_domain, name_user;
1571 uint8_t authoritative = 0;
1573 uint16_t validation_level;
1574 union netr_Validation *validation = NULL;
1575 struct netr_SamBaseInfo *base_info = NULL;
1577 DEBUG(10,("winbindd_dual_pam_auth_samlogon\n"));
1579 /* Parse domain and username */
1581 parse_domain_user(user, name_domain, name_user);
1584 * We check against domain->name instead of
1585 * name_domain, as find_auth_domain() ->
1586 * find_domain_from_name_noinit() already decided
1587 * that we are in a child for the correct domain.
1589 * name_domain can also be lp_realm()
1590 * we need to check against domain->name.
1592 if (strequal(domain->name, get_global_sam_name())) {
1593 DATA_BLOB chal_blob = data_blob_const(chal, sizeof(chal));
1594 struct netr_SamInfo3 *info3 = NULL;
1596 /* do password magic */
1598 generate_random_buffer(chal, sizeof(chal));
1600 if (lp_client_ntlmv2_auth()) {
1601 DATA_BLOB server_chal;
1602 DATA_BLOB names_blob;
1603 server_chal = data_blob_const(chal, 8);
1605 /* note that the 'workgroup' here is for the local
1606 machine. The 'server name' must match the
1607 'workstation' passed to the actual SamLogon call.
1609 names_blob = NTLMv2_generate_names_blob(
1610 mem_ctx, lp_netbios_name(), lp_workgroup());
1612 if (!SMBNTLMv2encrypt(mem_ctx, name_user, name_domain,
1616 &lm_resp, &nt_resp, NULL, NULL)) {
1617 data_blob_free(&names_blob);
1618 DEBUG(0, ("winbindd_pam_auth: SMBNTLMv2encrypt() failed!\n"));
1619 result = NT_STATUS_NO_MEMORY;
1622 data_blob_free(&names_blob);
1624 lm_resp = data_blob_null;
1625 SMBNTencrypt(pass, chal, local_nt_response);
1627 nt_resp = data_blob_talloc(mem_ctx, local_nt_response,
1628 sizeof(local_nt_response));
1631 result = winbindd_dual_auth_passdb(
1632 talloc_tos(), 0, name_domain, name_user,
1633 &chal_blob, &lm_resp, &nt_resp,
1634 true, /* interactive */
1637 if (NT_STATUS_IS_OK(result)) {
1638 result = map_info3_to_validation(mem_ctx,
1643 if (!NT_STATUS_IS_OK(result)) {
1649 * We need to try the remote NETLOGON server if this is
1650 * not authoritative (for example on the RODC).
1652 if (authoritative != 0) {
1657 /* check authentication loop */
1659 result = winbind_samlogon_retry_loop(domain,
1667 data_blob_null, data_blob_null,
1668 true, /* interactive */
1673 if (!NT_STATUS_IS_OK(result)) {
1677 /* handle the case where a NT4 DC does not fill in the acct_flags in
1678 * the samlogon reply info3. When accurate info3 is required by the
1679 * caller, we look up the account flags ourselves - gd */
1681 switch (validation_level) {
1683 base_info = &validation->sam3->base;
1686 base_info = &validation->sam6->base;
1689 DBG_ERR("Bad validation level %d", (int)validation_level);
1690 result = NT_STATUS_INTERNAL_ERROR;
1693 if ((request_flags & WBFLAG_PAM_INFO3_TEXT) &&
1694 (base_info->acct_flags == 0))
1696 struct rpc_pipe_client *samr_pipe;
1697 struct policy_handle samr_domain_handle, user_pol;
1698 union samr_UserInfo *info = NULL;
1699 NTSTATUS status_tmp, result_tmp;
1700 uint32_t acct_flags;
1701 struct dcerpc_binding_handle *b;
1703 status_tmp = cm_connect_sam(domain, mem_ctx, false,
1704 &samr_pipe, &samr_domain_handle);
1706 if (!NT_STATUS_IS_OK(status_tmp)) {
1707 DEBUG(3, ("could not open handle to SAMR pipe: %s\n",
1708 nt_errstr(status_tmp)));
1712 b = samr_pipe->binding_handle;
1714 status_tmp = dcerpc_samr_OpenUser(b, mem_ctx,
1715 &samr_domain_handle,
1716 MAXIMUM_ALLOWED_ACCESS,
1721 if (!NT_STATUS_IS_OK(status_tmp)) {
1722 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1723 nt_errstr(status_tmp)));
1726 if (!NT_STATUS_IS_OK(result_tmp)) {
1727 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1728 nt_errstr(result_tmp)));
1732 status_tmp = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1738 if (any_nt_status_not_ok(status_tmp, result_tmp,
1740 DEBUG(3, ("could not query user info on SAMR pipe: %s\n",
1741 nt_errstr(status_tmp)));
1742 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1746 acct_flags = info->info16.acct_flags;
1748 if (acct_flags == 0) {
1749 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1753 base_info->acct_flags = acct_flags;
1755 DEBUG(10,("successfully retrieved acct_flags 0x%x\n", acct_flags));
1757 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1761 if (NT_STATUS_IS_OK(result)) {
1762 *_validation_level = validation_level;
1763 *_validation = validation;
1768 enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
1769 struct winbindd_cli_state *state)
1771 NTSTATUS result = NT_STATUS_LOGON_FAILURE;
1772 NTSTATUS krb5_result = NT_STATUS_OK;
1773 fstring name_domain, name_user;
1775 fstring domain_user;
1776 struct netr_SamInfo3 *info3 = NULL;
1777 NTSTATUS name_map_status = NT_STATUS_UNSUCCESSFUL;
1779 /* Ensure null termination */
1780 state->request->data.auth.user[sizeof(state->request->data.auth.user)-1]='\0';
1782 /* Ensure null termination */
1783 state->request->data.auth.pass[sizeof(state->request->data.auth.pass)-1]='\0';
1785 DEBUG(3, ("[%5lu]: dual pam auth %s\n", (unsigned long)state->pid,
1786 state->request->data.auth.user));
1788 /* Parse domain and username */
1790 name_map_status = normalize_name_unmap(state->mem_ctx,
1791 state->request->data.auth.user,
1794 /* If the name normalization didnt' actually do anything,
1795 just use the original name */
1797 if (!NT_STATUS_IS_OK(name_map_status) &&
1798 !NT_STATUS_EQUAL(name_map_status, NT_STATUS_FILE_RENAMED))
1800 mapped_user = state->request->data.auth.user;
1803 parse_domain_user(mapped_user, name_domain, name_user);
1805 if ( mapped_user != state->request->data.auth.user ) {
1806 fstr_sprintf( domain_user, "%s%c%s", name_domain,
1807 *lp_winbind_separator(),
1809 strlcpy( state->request->data.auth.user, domain_user,
1810 sizeof(state->request->data.auth.user));
1813 if (!domain->online) {
1814 result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1815 if (domain->startup) {
1816 /* Logons are very important to users. If we're offline and
1817 we get a request within the first 30 seconds of startup,
1818 try very hard to find a DC and go online. */
1820 DEBUG(10,("winbindd_dual_pam_auth: domain: %s offline and auth "
1821 "request in startup mode.\n", domain->name ));
1823 winbindd_flush_negative_conn_cache(domain);
1824 result = init_dc_connection(domain, false);
1828 DEBUG(10,("winbindd_dual_pam_auth: domain: %s last was %s\n", domain->name, domain->online ? "online":"offline"));
1830 /* Check for Kerberos authentication */
1831 if (domain->online && (state->request->flags & WBFLAG_PAM_KRB5)) {
1833 result = winbindd_dual_pam_auth_kerberos(domain, state, &info3);
1834 /* save for later */
1835 krb5_result = result;
1838 if (NT_STATUS_IS_OK(result)) {
1839 DEBUG(10,("winbindd_dual_pam_auth_kerberos succeeded\n"));
1840 goto process_result;
1843 DBG_DEBUG("winbindd_dual_pam_auth_kerberos failed: %s\n",
1846 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS) ||
1847 NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT) ||
1848 NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
1849 DEBUG(10,("winbindd_dual_pam_auth_kerberos setting domain to offline\n"));
1850 set_domain_offline( domain );
1854 /* there are quite some NT_STATUS errors where there is no
1855 * point in retrying with a samlogon, we explictly have to take
1856 * care not to increase the bad logon counter on the DC */
1858 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_DISABLED) ||
1859 NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_EXPIRED) ||
1860 NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_LOCKED_OUT) ||
1861 NT_STATUS_EQUAL(result, NT_STATUS_INVALID_LOGON_HOURS) ||
1862 NT_STATUS_EQUAL(result, NT_STATUS_INVALID_WORKSTATION) ||
1863 NT_STATUS_EQUAL(result, NT_STATUS_LOGON_FAILURE) ||
1864 NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER) ||
1865 NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED) ||
1866 NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) ||
1867 NT_STATUS_EQUAL(result, NT_STATUS_WRONG_PASSWORD)) {
1871 if (state->request->flags & WBFLAG_PAM_FALLBACK_AFTER_KRB5) {
1872 DEBUG(3,("falling back to samlogon\n"));
1880 /* Check for Samlogon authentication */
1881 if (domain->online) {
1882 uint16_t validation_level;
1883 union netr_Validation *validation = NULL;
1884 struct netr_SamBaseInfo *base_info = NULL;
1886 result = winbindd_dual_pam_auth_samlogon(
1887 state->mem_ctx, domain,
1888 state->request->data.auth.user,
1889 state->request->data.auth.pass,
1890 state->request->flags,
1894 if (NT_STATUS_IS_OK(result)) {
1895 DEBUG(10,("winbindd_dual_pam_auth_samlogon succeeded\n"));
1897 switch (validation_level) {
1899 base_info = &validation->sam3->base;
1902 base_info = &validation->sam6->base;
1905 DBG_ERR("Bad validation level %d\n",
1907 result = NT_STATUS_INTERNAL_ERROR;
1911 /* add the Krb5 err if we have one */
1912 if ( NT_STATUS_EQUAL(krb5_result, NT_STATUS_TIME_DIFFERENCE_AT_DC ) ) {
1913 base_info->user_flags |= LOGON_KRB5_FAIL_CLOCK_SKEW;
1916 result = map_validation_to_info3(state->mem_ctx,
1920 TALLOC_FREE(validation);
1921 if (!NT_STATUS_IS_OK(result)) {
1924 goto process_result;
1927 DEBUG(10,("winbindd_dual_pam_auth_samlogon failed: %s\n",
1928 nt_errstr(result)));
1930 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS) ||
1931 NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT) ||
1932 NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND))
1934 DEBUG(10,("winbindd_dual_pam_auth_samlogon setting domain to offline\n"));
1935 set_domain_offline( domain );
1939 if (domain->online) {
1940 /* We're still online - fail. */
1946 /* Check for Cached logons */
1947 if (!domain->online && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN) &&
1948 lp_winbind_offline_logon()) {
1950 result = winbindd_dual_pam_auth_cached(domain, state, &info3);
1952 if (!NT_STATUS_IS_OK(result)) {
1953 DEBUG(10,("winbindd_dual_pam_auth_cached failed: %s\n", nt_errstr(result)));
1956 DEBUG(10,("winbindd_dual_pam_auth_cached succeeded\n"));
1961 if (NT_STATUS_IS_OK(result)) {
1963 struct dom_sid user_sid;
1965 /* In all codepaths where result == NT_STATUS_OK info3 must have
1966 been initialized. */
1968 result = NT_STATUS_INTERNAL_ERROR;
1972 sid_compose(&user_sid, info3->base.domain_sid,
1975 if (info3->base.full_name.string == NULL) {
1976 struct netr_SamInfo3 *cached_info3;
1978 cached_info3 = netsamlogon_cache_get(state->mem_ctx,
1980 if (cached_info3 != NULL &&
1981 cached_info3->base.full_name.string != NULL) {
1982 info3->base.full_name.string =
1983 talloc_strdup(info3,
1984 cached_info3->base.full_name.string);
1987 /* this might fail so we don't check the return code */
1988 wcache_query_user_fullname(domain,
1991 &info3->base.full_name.string);
1995 wcache_invalidate_samlogon(find_domain_from_name(name_domain),
1997 netsamlogon_cache_store(name_user, info3);
1999 /* save name_to_sid info as early as possible (only if
2000 this is our primary domain so we don't invalidate
2001 the cache entry by storing the seq_num for the wrong
2003 if ( domain->primary ) {
2004 cache_name2sid(domain, name_domain, name_user,
2005 SID_NAME_USER, &user_sid);
2008 /* Check if the user is in the right group */
2010 result = check_info3_in_group(
2012 state->request->data.auth.require_membership_of_sid);
2013 if (!NT_STATUS_IS_OK(result)) {
2014 DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
2015 state->request->data.auth.user,
2016 state->request->data.auth.require_membership_of_sid));
2020 result = append_auth_data(state->mem_ctx, state->response,
2021 state->request->flags, info3,
2022 name_domain, name_user);
2023 if (!NT_STATUS_IS_OK(result)) {
2027 if ((state->request->flags & WBFLAG_PAM_CACHED_LOGIN)
2028 && lp_winbind_offline_logon()) {
2030 result = winbindd_store_creds(domain,
2031 state->request->data.auth.user,
2032 state->request->data.auth.pass,
2036 if (state->request->flags & WBFLAG_PAM_GET_PWD_POLICY) {
2038 * WBFLAG_PAM_GET_PWD_POLICY is not used within
2039 * any Samba caller anymore.
2041 * We just fake this based on the effective values
2042 * for the user, for legacy callers.
2044 fake_password_policy(state->response, &info3->base);
2047 result = NT_STATUS_OK;
2051 /* give us a more useful (more correct?) error code */
2052 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
2053 (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
2054 result = NT_STATUS_NO_LOGON_SERVERS;
2057 set_auth_errors(state->response, result);
2059 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n",
2060 state->request->data.auth.user,
2061 state->response->data.auth.nt_status_string,
2062 state->response->data.auth.pam_error));
2064 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2067 NTSTATUS winbind_dual_SamLogon(struct winbindd_domain *domain,
2068 TALLOC_CTX *mem_ctx,
2069 uint32_t logon_parameters,
2070 const char *name_user,
2071 const char *name_domain,
2072 const char *workstation,
2073 const uint8_t chal[8],
2074 DATA_BLOB lm_response,
2075 DATA_BLOB nt_response,
2076 uint8_t *authoritative,
2079 uint16_t *_validation_level,
2080 union netr_Validation **_validation)
2082 uint16_t validation_level;
2083 union netr_Validation *validation = NULL;
2087 * We check against domain->name instead of
2088 * name_domain, as find_auth_domain() ->
2089 * find_domain_from_name_noinit() already decided
2090 * that we are in a child for the correct domain.
2092 * name_domain can also be lp_realm()
2093 * we need to check against domain->name.
2095 if (!skip_sam && strequal(domain->name, get_global_sam_name())) {
2096 DATA_BLOB chal_blob = data_blob_const(
2098 struct netr_SamInfo3 *info3 = NULL;
2100 result = winbindd_dual_auth_passdb(
2103 name_domain, name_user,
2104 &chal_blob, &lm_response, &nt_response,
2105 false, /* interactive */
2108 if (NT_STATUS_IS_OK(result)) {
2109 result = map_info3_to_validation(mem_ctx,
2114 if (!NT_STATUS_IS_OK(result)) {
2120 * We need to try the remote NETLOGON server if this is
2121 * not authoritative.
2123 if (*authoritative != 0) {
2125 goto process_result;
2129 result = winbind_samlogon_retry_loop(domain,
2133 NULL, /* password */
2135 /* Bug #3248 - found by Stefan Burkei. */
2136 workstation, /* We carefully set this above so use it... */
2140 false, /* interactive */
2145 if (!NT_STATUS_IS_OK(result)) {
2151 if (NT_STATUS_IS_OK(result)) {
2152 struct dom_sid user_sid;
2153 TALLOC_CTX *base_ctx = NULL;
2154 struct netr_SamBaseInfo *base_info = NULL;
2155 struct netr_SamInfo3 *info3 = NULL;
2157 switch (validation_level) {
2159 base_ctx = validation->sam3;
2160 base_info = &validation->sam3->base;
2163 base_ctx = validation->sam6;
2164 base_info = &validation->sam6->base;
2167 result = NT_STATUS_INTERNAL_ERROR;
2171 sid_compose(&user_sid, base_info->domain_sid, base_info->rid);
2173 if (base_info->full_name.string == NULL) {
2174 struct netr_SamInfo3 *cached_info3;
2176 cached_info3 = netsamlogon_cache_get(mem_ctx,
2178 if (cached_info3 != NULL &&
2179 cached_info3->base.full_name.string != NULL)
2181 base_info->full_name.string = talloc_strdup(
2183 cached_info3->base.full_name.string);
2186 /* this might fail so we don't check the return code */
2187 wcache_query_user_fullname(domain,
2190 &base_info->full_name.string);
2194 result = map_validation_to_info3(talloc_tos(),
2198 if (!NT_STATUS_IS_OK(result)) {
2201 wcache_invalidate_samlogon(find_domain_from_name(name_domain),
2203 netsamlogon_cache_store(name_user, info3);
2209 /* give us a more useful (more correct?) error code */
2210 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
2211 (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
2212 result = NT_STATUS_NO_LOGON_SERVERS;
2215 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2216 ("NTLM CRAP authentication for user [%s]\\[%s] returned %s\n",
2219 nt_errstr(result)));
2221 if (!NT_STATUS_IS_OK(result)) {
2225 *_validation_level = validation_level;
2226 *_validation = validation;
2227 return NT_STATUS_OK;
2230 enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
2231 struct winbindd_cli_state *state)
2234 const char *name_user = NULL;
2235 const char *name_domain = NULL;
2236 const char *workstation;
2237 uint8_t authoritative = 0;
2239 uint16_t validation_level;
2240 union netr_Validation *validation = NULL;
2241 DATA_BLOB lm_resp, nt_resp;
2243 /* This is child-only, so no check for privileged access is needed
2246 /* Ensure null termination */
2247 state->request->data.auth_crap.user[sizeof(state->request->data.auth_crap.user)-1]=0;
2248 state->request->data.auth_crap.domain[sizeof(state->request->data.auth_crap.domain)-1]=0;
2250 name_user = state->request->data.auth_crap.user;
2251 name_domain = state->request->data.auth_crap.domain;
2252 workstation = state->request->data.auth_crap.workstation;
2254 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state->pid,
2255 name_domain, name_user));
2257 if (state->request->data.auth_crap.lm_resp_len > sizeof(state->request->data.auth_crap.lm_resp)
2258 || state->request->data.auth_crap.nt_resp_len > sizeof(state->request->data.auth_crap.nt_resp)) {
2259 if (!(state->request->flags & WBFLAG_BIG_NTLMV2_BLOB) ||
2260 state->request->extra_len != state->request->data.auth_crap.nt_resp_len) {
2261 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n",
2262 state->request->data.auth_crap.lm_resp_len,
2263 state->request->data.auth_crap.nt_resp_len));
2264 result = NT_STATUS_INVALID_PARAMETER;
2269 lm_resp = data_blob_talloc(state->mem_ctx, state->request->data.auth_crap.lm_resp,
2270 state->request->data.auth_crap.lm_resp_len);
2272 if (state->request->flags & WBFLAG_BIG_NTLMV2_BLOB) {
2273 nt_resp = data_blob_talloc(state->mem_ctx,
2274 state->request->extra_data.data,
2275 state->request->data.auth_crap.nt_resp_len);
2277 nt_resp = data_blob_talloc(state->mem_ctx,
2278 state->request->data.auth_crap.nt_resp,
2279 state->request->data.auth_crap.nt_resp_len);
2282 result = winbind_dual_SamLogon(domain,
2284 state->request->data.auth_crap.logon_parameters,
2287 /* Bug #3248 - found by Stefan Burkei. */
2288 workstation, /* We carefully set this above so use it... */
2289 state->request->data.auth_crap.chal,
2297 if (!NT_STATUS_IS_OK(result)) {
2298 state->response->data.auth.authoritative = authoritative;
2302 if (NT_STATUS_IS_OK(result)) {
2303 struct netr_SamInfo3 *info3 = NULL;
2305 result = map_validation_to_info3(state->mem_ctx,
2309 TALLOC_FREE(validation);
2310 if (!NT_STATUS_IS_OK(result)) {
2314 /* Check if the user is in the right group */
2315 result = check_info3_in_group(
2317 state->request->data.auth_crap.require_membership_of_sid);
2318 if (!NT_STATUS_IS_OK(result)) {
2319 DEBUG(3, ("User %s is not in the required group (%s), so "
2320 "crap authentication is rejected\n",
2321 state->request->data.auth_crap.user,
2322 state->request->data.auth_crap.require_membership_of_sid));
2326 result = append_auth_data(state->mem_ctx, state->response,
2327 state->request->flags, info3,
2328 name_domain, name_user);
2329 if (!NT_STATUS_IS_OK(result)) {
2336 if (state->request->flags & WBFLAG_PAM_NT_STATUS_SQUASH) {
2337 result = nt_status_squash(result);
2340 set_auth_errors(state->response, result);
2342 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2345 enum winbindd_result winbindd_dual_pam_chauthtok(struct winbindd_domain *contact_domain,
2346 struct winbindd_cli_state *state)
2349 char *newpass = NULL;
2350 struct policy_handle dom_pol;
2351 struct rpc_pipe_client *cli = NULL;
2352 bool got_info = false;
2353 struct samr_DomInfo1 *info = NULL;
2354 struct userPwdChangeFailureInformation *reject = NULL;
2355 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2356 fstring domain, user;
2357 struct dcerpc_binding_handle *b = NULL;
2359 ZERO_STRUCT(dom_pol);
2361 DEBUG(3, ("[%5lu]: dual pam chauthtok %s\n", (unsigned long)state->pid,
2362 state->request->data.auth.user));
2364 if (!parse_domain_user(state->request->data.chauthtok.user, domain, user)) {
2368 /* Change password */
2370 oldpass = state->request->data.chauthtok.oldpass;
2371 newpass = state->request->data.chauthtok.newpass;
2373 /* Initialize reject reason */
2374 state->response->data.auth.reject_reason = Undefined;
2376 /* Get sam handle */
2378 result = cm_connect_sam(contact_domain, state->mem_ctx, true, &cli,
2380 if (!NT_STATUS_IS_OK(result)) {
2381 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
2385 b = cli->binding_handle;
2387 result = rpccli_samr_chgpasswd_user3(cli, state->mem_ctx,
2394 /* Windows 2003 returns NT_STATUS_PASSWORD_RESTRICTION */
2396 if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION) ) {
2398 fill_in_password_policy(state->response, info);
2400 state->response->data.auth.reject_reason =
2401 reject->extendedFailureReason;
2406 /* atm the pidl generated rpccli_samr_ChangePasswordUser3 function will
2407 * return with NT_STATUS_BUFFER_TOO_SMALL for w2k dcs as w2k just
2408 * returns with 4byte error code (NT_STATUS_NOT_SUPPORTED) which is too
2409 * short to comply with the samr_ChangePasswordUser3 idl - gd */
2411 /* only fallback when the chgpasswd_user3 call is not supported */
2412 if (NT_STATUS_EQUAL(result, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE) ||
2413 NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED) ||
2414 NT_STATUS_EQUAL(result, NT_STATUS_BUFFER_TOO_SMALL) ||
2415 NT_STATUS_EQUAL(result, NT_STATUS_NOT_IMPLEMENTED)) {
2417 DEBUG(10,("Password change with chgpasswd_user3 failed with: %s, retrying chgpasswd_user2\n",
2418 nt_errstr(result)));
2420 result = rpccli_samr_chgpasswd_user2(cli, state->mem_ctx, user, newpass, oldpass);
2422 /* Windows 2000 returns NT_STATUS_ACCOUNT_RESTRICTION.
2423 Map to the same status code as Windows 2003. */
2425 if ( NT_STATUS_EQUAL(NT_STATUS_ACCOUNT_RESTRICTION, result ) ) {
2426 result = NT_STATUS_PASSWORD_RESTRICTION;
2432 if (NT_STATUS_IS_OK(result)
2433 && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN)
2434 && lp_winbind_offline_logon()) {
2435 result = winbindd_update_creds_by_name(contact_domain, user,
2437 /* Again, this happens when we login from gdm or xdm
2438 * and the password expires, *BUT* cached crendentials
2439 * doesn't exist. winbindd_update_creds_by_name()
2440 * returns NT_STATUS_NO_SUCH_USER.
2441 * This is not a failure.
2444 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER)) {
2445 result = NT_STATUS_OK;
2448 if (!NT_STATUS_IS_OK(result)) {
2449 DEBUG(10, ("Failed to store creds: %s\n",
2450 nt_errstr(result)));
2451 goto process_result;
2455 if (!NT_STATUS_IS_OK(result) && !got_info && contact_domain) {
2457 NTSTATUS policy_ret;
2459 policy_ret = fillup_password_policy(
2460 contact_domain, state->response);
2462 /* failure of this is non critical, it will just provide no
2463 * additional information to the client why the change has
2464 * failed - Guenther */
2466 if (!NT_STATUS_IS_OK(policy_ret)) {
2467 DEBUG(10,("Failed to get password policies: %s\n", nt_errstr(policy_ret)));
2468 goto process_result;
2474 if (strequal(contact_domain->name, get_global_sam_name())) {
2475 /* FIXME: internal rpc pipe does not cache handles yet */
2477 if (is_valid_policy_hnd(&dom_pol)) {
2479 dcerpc_samr_Close(b, state->mem_ctx, &dom_pol, &_result);
2485 set_auth_errors(state->response, result);
2487 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2488 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
2491 state->response->data.auth.nt_status_string,
2492 state->response->data.auth.pam_error));
2494 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2497 enum winbindd_result winbindd_dual_pam_logoff(struct winbindd_domain *domain,
2498 struct winbindd_cli_state *state)
2500 NTSTATUS result = NT_STATUS_NOT_SUPPORTED;
2502 DEBUG(3, ("[%5lu]: pam dual logoff %s\n", (unsigned long)state->pid,
2503 state->request->data.logoff.user));
2505 if (!(state->request->flags & WBFLAG_PAM_KRB5)) {
2506 result = NT_STATUS_OK;
2507 goto process_result;
2510 if (state->request->data.logoff.krb5ccname[0] == '\0') {
2511 result = NT_STATUS_OK;
2512 goto process_result;
2517 if (state->request->data.logoff.uid == (uid_t)-1) {
2518 DEBUG(0,("winbindd_pam_logoff: invalid uid\n"));
2519 goto process_result;
2522 /* what we need here is to find the corresponding krb5 ccache name *we*
2523 * created for a given username and destroy it */
2525 if (!ccache_entry_exists(state->request->data.logoff.user)) {
2526 result = NT_STATUS_OK;
2527 DEBUG(10,("winbindd_pam_logoff: no entry found.\n"));
2528 goto process_result;
2531 if (!ccache_entry_identical(state->request->data.logoff.user,
2532 state->request->data.logoff.uid,
2533 state->request->data.logoff.krb5ccname)) {
2534 DEBUG(0,("winbindd_pam_logoff: cached entry differs.\n"));
2535 goto process_result;
2538 result = remove_ccache(state->request->data.logoff.user);
2539 if (!NT_STATUS_IS_OK(result)) {
2540 DEBUG(0,("winbindd_pam_logoff: failed to remove ccache: %s\n",
2541 nt_errstr(result)));
2542 goto process_result;
2546 * Remove any mlock'ed memory creds in the child
2547 * we might be using for krb5 ticket renewal.
2550 winbindd_delete_memory_creds(state->request->data.logoff.user);
2553 result = NT_STATUS_NOT_SUPPORTED;
2559 set_auth_errors(state->response, result);
2561 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2564 /* Change user password with auth crap*/
2566 enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domain *domainSt, struct winbindd_cli_state *state)
2569 DATA_BLOB new_nt_password;
2570 DATA_BLOB old_nt_hash_enc;
2571 DATA_BLOB new_lm_password;
2572 DATA_BLOB old_lm_hash_enc;
2573 fstring domain,user;
2574 struct policy_handle dom_pol;
2575 struct winbindd_domain *contact_domain = domainSt;
2576 struct rpc_pipe_client *cli = NULL;
2577 struct dcerpc_binding_handle *b = NULL;
2579 ZERO_STRUCT(dom_pol);
2581 /* Ensure null termination */
2582 state->request->data.chng_pswd_auth_crap.user[
2583 sizeof(state->request->data.chng_pswd_auth_crap.user)-1]=0;
2584 state->request->data.chng_pswd_auth_crap.domain[
2585 sizeof(state->request->data.chng_pswd_auth_crap.domain)-1]=0;
2589 DEBUG(3, ("[%5lu]: pam change pswd auth crap domain: %s user: %s\n",
2590 (unsigned long)state->pid,
2591 state->request->data.chng_pswd_auth_crap.domain,
2592 state->request->data.chng_pswd_auth_crap.user));
2594 if (lp_winbind_offline_logon()) {
2595 DEBUG(0,("Refusing password change as winbind offline logons are enabled. "));
2596 DEBUGADD(0,("Changing passwords here would risk inconsistent logons\n"));
2597 result = NT_STATUS_ACCESS_DENIED;
2601 if (*state->request->data.chng_pswd_auth_crap.domain) {
2602 fstrcpy(domain,state->request->data.chng_pswd_auth_crap.domain);
2604 parse_domain_user(state->request->data.chng_pswd_auth_crap.user,
2608 DEBUG(3,("no domain specified with username (%s) - "
2610 state->request->data.chng_pswd_auth_crap.user));
2611 result = NT_STATUS_NO_SUCH_USER;
2616 if (!*domain && lp_winbind_use_default_domain()) {
2617 fstrcpy(domain,lp_workgroup());
2621 fstrcpy(user, state->request->data.chng_pswd_auth_crap.user);
2624 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n",
2625 (unsigned long)state->pid, domain, user));
2627 /* Change password */
2628 new_nt_password = data_blob_const(
2629 state->request->data.chng_pswd_auth_crap.new_nt_pswd,
2630 state->request->data.chng_pswd_auth_crap.new_nt_pswd_len);
2632 old_nt_hash_enc = data_blob_const(
2633 state->request->data.chng_pswd_auth_crap.old_nt_hash_enc,
2634 state->request->data.chng_pswd_auth_crap.old_nt_hash_enc_len);
2636 if(state->request->data.chng_pswd_auth_crap.new_lm_pswd_len > 0) {
2637 new_lm_password = data_blob_const(
2638 state->request->data.chng_pswd_auth_crap.new_lm_pswd,
2639 state->request->data.chng_pswd_auth_crap.new_lm_pswd_len);
2641 old_lm_hash_enc = data_blob_const(
2642 state->request->data.chng_pswd_auth_crap.old_lm_hash_enc,
2643 state->request->data.chng_pswd_auth_crap.old_lm_hash_enc_len);
2645 new_lm_password = data_blob_null;
2646 old_lm_hash_enc = data_blob_null;
2649 /* Get sam handle */
2651 result = cm_connect_sam(contact_domain, state->mem_ctx, true, &cli, &dom_pol);
2652 if (!NT_STATUS_IS_OK(result)) {
2653 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
2657 b = cli->binding_handle;
2659 result = rpccli_samr_chng_pswd_auth_crap(
2660 cli, state->mem_ctx, user, new_nt_password, old_nt_hash_enc,
2661 new_lm_password, old_lm_hash_enc);
2665 if (strequal(contact_domain->name, get_global_sam_name())) {
2666 /* FIXME: internal rpc pipe does not cache handles yet */
2668 if (is_valid_policy_hnd(&dom_pol)) {
2670 dcerpc_samr_Close(b, state->mem_ctx, &dom_pol, &_result);
2676 set_auth_errors(state->response, result);
2678 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2679 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
2681 state->response->data.auth.nt_status_string,
2682 state->response->data.auth.pam_error));
2684 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2688 static NTSTATUS extract_pac_vrfy_sigs(TALLOC_CTX *mem_ctx, DATA_BLOB pac_blob,
2689 struct PAC_LOGON_INFO **logon_info)
2691 krb5_context krbctx = NULL;
2692 krb5_error_code k5ret;
2694 krb5_kt_cursor cursor;
2695 krb5_keytab_entry entry;
2696 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2699 ZERO_STRUCT(cursor);
2701 k5ret = krb5_init_context(&krbctx);
2703 DEBUG(1, ("Failed to initialize kerberos context: %s\n",
2704 error_message(k5ret)));
2705 status = krb5_to_nt_status(k5ret);
2709 k5ret = gse_krb5_get_server_keytab(krbctx, &keytab);
2711 DEBUG(1, ("Failed to get keytab: %s\n",
2712 error_message(k5ret)));
2713 status = krb5_to_nt_status(k5ret);
2717 k5ret = krb5_kt_start_seq_get(krbctx, keytab, &cursor);
2719 DEBUG(1, ("Failed to start seq: %s\n",
2720 error_message(k5ret)));
2721 status = krb5_to_nt_status(k5ret);
2725 k5ret = krb5_kt_next_entry(krbctx, keytab, &entry, &cursor);
2726 while (k5ret == 0) {
2727 status = kerberos_pac_logon_info(mem_ctx, pac_blob,
2729 KRB5_KT_KEY(&entry), NULL, 0,
2731 if (NT_STATUS_IS_OK(status)) {
2734 k5ret = smb_krb5_kt_free_entry(krbctx, &entry);
2735 k5ret = krb5_kt_next_entry(krbctx, keytab, &entry, &cursor);
2738 k5ret = krb5_kt_end_seq_get(krbctx, keytab, &cursor);
2740 DEBUG(1, ("Failed to end seq: %s\n",
2741 error_message(k5ret)));
2744 k5ret = krb5_kt_close(krbctx, keytab);
2746 DEBUG(1, ("Failed to close keytab: %s\n",
2747 error_message(k5ret)));
2750 krb5_free_context(krbctx);
2755 NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
2756 struct netr_SamInfo3 **info3)
2758 struct winbindd_request *req = state->request;
2760 struct PAC_LOGON_INFO *logon_info = NULL;
2761 struct netr_SamInfo3 *info3_copy = NULL;
2764 pac_blob = data_blob_const(req->extra_data.data, req->extra_len);
2765 result = extract_pac_vrfy_sigs(state->mem_ctx, pac_blob, &logon_info);
2766 if (!NT_STATUS_IS_OK(result) &&
2767 !NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
2768 DEBUG(1, ("Error during PAC signature verification: %s\n",
2769 nt_errstr(result)));
2775 * Signature verification succeeded, we can
2776 * trust the PAC and prime the netsamlogon
2777 * and name2sid caches. DO NOT DO THIS
2778 * in the signature verification failed
2781 struct winbindd_domain *domain = NULL;
2783 result = create_info3_from_pac_logon_info(state->mem_ctx,
2786 if (!NT_STATUS_IS_OK(result)) {
2789 netsamlogon_cache_store(NULL, info3_copy);
2792 * We're in the parent here, so find the child
2793 * pointer from the PAC domain name.
2795 domain = find_lookup_domain_from_name(
2796 info3_copy->base.logon_domain.string);
2797 if (domain && domain->primary ) {
2798 struct dom_sid user_sid;
2800 sid_compose(&user_sid,
2801 info3_copy->base.domain_sid,
2802 info3_copy->base.rid);
2804 cache_name2sid_trusted(domain,
2805 info3_copy->base.logon_domain.string,
2806 info3_copy->base.account_name.string,
2810 DBG_INFO("PAC for user %s\\%s SID %s primed cache\n",
2811 info3_copy->base.logon_domain.string,
2812 info3_copy->base.account_name.string,
2813 sid_string_dbg(&user_sid));
2817 /* Try without signature verification */
2818 result = kerberos_pac_logon_info(state->mem_ctx, pac_blob, NULL,
2819 NULL, NULL, NULL, 0,
2821 if (!NT_STATUS_IS_OK(result)) {
2822 DEBUG(10, ("Could not extract PAC: %s\n",
2823 nt_errstr(result)));
2828 * Don't strictly need to copy here,
2829 * but it makes it explicit we're
2830 * returning a copy talloc'ed off
2831 * the state->mem_ctx.
2833 info3_copy = copy_netr_SamInfo3(state->mem_ctx,
2834 &logon_info->info3);
2835 if (info3_copy == NULL) {
2836 return NT_STATUS_NO_MEMORY;
2841 *info3 = info3_copy;
2843 return NT_STATUS_OK;
2845 #else /* HAVE_KRB5 */
2846 NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
2847 struct netr_SamInfo3 **info3)
2849 return NT_STATUS_NO_SUCH_USER;
2851 #endif /* HAVE_KRB5 */