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;
63 status = map_validation_to_info3(talloc_tos(),
67 if (!NT_STATUS_IS_OK(status)) {
71 resp->data.auth.info3.logon_time =
72 nt_time_to_unix(info3->base.logon_time);
73 resp->data.auth.info3.logoff_time =
74 nt_time_to_unix(info3->base.logoff_time);
75 resp->data.auth.info3.kickoff_time =
76 nt_time_to_unix(info3->base.kickoff_time);
77 resp->data.auth.info3.pass_last_set_time =
78 nt_time_to_unix(info3->base.last_password_change);
79 resp->data.auth.info3.pass_can_change_time =
80 nt_time_to_unix(info3->base.allow_password_change);
81 resp->data.auth.info3.pass_must_change_time =
82 nt_time_to_unix(info3->base.force_password_change);
84 resp->data.auth.info3.logon_count = info3->base.logon_count;
85 resp->data.auth.info3.bad_pw_count = info3->base.bad_password_count;
87 resp->data.auth.info3.user_rid = info3->base.rid;
88 resp->data.auth.info3.group_rid = info3->base.primary_gid;
89 sid_to_fstring(resp->data.auth.info3.dom_sid, info3->base.domain_sid);
91 resp->data.auth.info3.num_groups = info3->base.groups.count;
92 resp->data.auth.info3.user_flgs = info3->base.user_flags;
94 resp->data.auth.info3.acct_flags = info3->base.acct_flags;
95 resp->data.auth.info3.num_other_sids = info3->sidcount;
97 fstrcpy(resp->data.auth.info3.user_name,
98 info3->base.account_name.string);
99 fstrcpy(resp->data.auth.info3.full_name,
100 info3->base.full_name.string);
101 fstrcpy(resp->data.auth.info3.logon_script,
102 info3->base.logon_script.string);
103 fstrcpy(resp->data.auth.info3.profile_path,
104 info3->base.profile_path.string);
105 fstrcpy(resp->data.auth.info3.home_dir,
106 info3->base.home_directory.string);
107 fstrcpy(resp->data.auth.info3.dir_drive,
108 info3->base.home_drive.string);
110 fstrcpy(resp->data.auth.info3.logon_srv,
111 info3->base.logon_server.string);
112 fstrcpy(resp->data.auth.info3.logon_dom,
113 info3->base.logon_domain.string);
115 ex = talloc_strdup(mem_ctx, "");
118 return NT_STATUS_NO_MEMORY;
121 for (i=0; i < info3->base.groups.count; i++) {
122 ex = talloc_asprintf_append_buffer(ex, "0x%08X:0x%08X\n",
123 info3->base.groups.rids[i].rid,
124 info3->base.groups.rids[i].attributes);
127 return NT_STATUS_NO_MEMORY;
131 for (i=0; i < info3->sidcount; i++) {
134 sid = dom_sid_string(mem_ctx, info3->sids[i].sid);
137 return NT_STATUS_NO_MEMORY;
140 ex = talloc_asprintf_append_buffer(ex, "%s:0x%08X\n",
142 info3->sids[i].attributes);
145 return NT_STATUS_NO_MEMORY;
151 resp->extra_data.data = ex;
152 resp->length += talloc_get_size(ex);
158 static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx,
159 struct winbindd_response *resp,
160 struct netr_SamInfo3 *info3)
163 enum ndr_err_code ndr_err;
165 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, info3,
166 (ndr_push_flags_fn_t)ndr_push_netr_SamInfo3);
167 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
168 DEBUG(0,("append_info3_as_ndr: failed to append\n"));
169 return ndr_map_error2ntstatus(ndr_err);
172 resp->extra_data.data = blob.data;
173 resp->length += blob.length;
178 static NTSTATUS append_unix_username(TALLOC_CTX *mem_ctx,
179 struct winbindd_response *resp,
180 const struct netr_SamInfo3 *info3,
181 const char *name_domain,
182 const char *name_user)
184 /* We've been asked to return the unix username, per
185 'winbind use default domain' settings and the like */
187 const char *nt_username, *nt_domain;
189 nt_domain = talloc_strdup(mem_ctx, info3->base.logon_domain.string);
191 /* If the server didn't give us one, just use the one
193 nt_domain = name_domain;
196 nt_username = talloc_strdup(mem_ctx, info3->base.account_name.string);
198 /* If the server didn't give us one, just use the one
200 nt_username = name_user;
203 fill_domain_username(resp->data.auth.unix_username,
204 nt_domain, nt_username, true);
206 DEBUG(5, ("Setting unix username to [%s]\n",
207 resp->data.auth.unix_username));
212 static NTSTATUS append_afs_token(TALLOC_CTX *mem_ctx,
213 struct winbindd_response *resp,
214 const struct netr_SamInfo3 *info3,
215 const char *name_domain,
216 const char *name_user)
218 char *afsname = NULL;
222 afsname = talloc_strdup(mem_ctx, lp_afs_username_map());
223 if (afsname == NULL) {
224 return NT_STATUS_NO_MEMORY;
227 afsname = talloc_string_sub(mem_ctx,
228 lp_afs_username_map(),
230 afsname = talloc_string_sub(mem_ctx, afsname,
232 afsname = talloc_string_sub(mem_ctx, afsname,
236 struct dom_sid user_sid;
239 sid_compose(&user_sid, info3->base.domain_sid,
241 sid_to_fstring(sidstr, &user_sid);
242 afsname = talloc_string_sub(mem_ctx, afsname,
246 if (afsname == NULL) {
247 return NT_STATUS_NO_MEMORY;
250 if (!strlower_m(afsname)) {
251 return NT_STATUS_INVALID_PARAMETER;
254 DEBUG(10, ("Generating token for user %s\n", afsname));
256 cell = strchr(afsname, '@');
259 return NT_STATUS_NO_MEMORY;
265 token = afs_createtoken_str(afsname, cell);
269 resp->extra_data.data = talloc_strdup(mem_ctx, token);
270 if (resp->extra_data.data == NULL) {
271 return NT_STATUS_NO_MEMORY;
273 resp->length += strlen((const char *)resp->extra_data.data)+1;
278 static NTSTATUS check_info3_in_group(struct netr_SamInfo3 *info3,
279 const char *group_sid)
281 * Check whether a user belongs to a group or list of groups.
283 * @param mem_ctx talloc memory context.
284 * @param info3 user information, including group membership info.
285 * @param group_sid One or more groups , separated by commas.
287 * @return NT_STATUS_OK on success,
288 * NT_STATUS_LOGON_FAILURE if the user does not belong,
289 * or other NT_STATUS_IS_ERR(status) for other kinds of failure.
292 struct dom_sid *require_membership_of_sid;
293 uint32_t num_require_membership_of_sid;
298 struct security_token *token;
299 TALLOC_CTX *frame = talloc_stackframe();
302 /* Parse the 'required group' SID */
304 if (!group_sid || !group_sid[0]) {
305 /* NO sid supplied, all users may access */
310 token = talloc_zero(talloc_tos(), struct security_token);
312 DEBUG(0, ("talloc failed\n"));
314 return NT_STATUS_NO_MEMORY;
317 num_require_membership_of_sid = 0;
318 require_membership_of_sid = NULL;
322 while (next_token_talloc(talloc_tos(), &p, &req_sid, ",")) {
323 if (!string_to_sid(&sid, req_sid)) {
324 DEBUG(0, ("check_info3_in_group: could not parse %s "
325 "as a SID!", req_sid));
327 return NT_STATUS_INVALID_PARAMETER;
330 status = add_sid_to_array(talloc_tos(), &sid,
331 &require_membership_of_sid,
332 &num_require_membership_of_sid);
333 if (!NT_STATUS_IS_OK(status)) {
334 DEBUG(0, ("add_sid_to_array failed\n"));
340 status = sid_array_from_info3(talloc_tos(), info3,
344 if (!NT_STATUS_IS_OK(status)) {
349 if (!NT_STATUS_IS_OK(status = add_aliases(get_global_sam_sid(),
351 || !NT_STATUS_IS_OK(status = add_aliases(&global_sid_Builtin,
353 DEBUG(3, ("could not add aliases: %s\n",
359 security_token_debug(DBGC_CLASS, 10, token);
361 for (i=0; i<num_require_membership_of_sid; i++) {
362 DEBUG(10, ("Checking SID %s\n", sid_string_dbg(
363 &require_membership_of_sid[i])));
364 if (nt_token_check_sid(&require_membership_of_sid[i],
366 DEBUG(10, ("Access ok\n"));
372 /* Do not distinguish this error from a wrong username/pw */
375 return NT_STATUS_LOGON_FAILURE;
378 struct winbindd_domain *find_auth_domain(uint8_t flags,
379 const char *domain_name)
381 struct winbindd_domain *domain;
384 domain = find_domain_from_name_noinit(domain_name);
385 if (domain == NULL) {
386 DEBUG(3, ("Authentication for domain [%s] refused "
387 "as it is not a trusted domain\n",
393 if (strequal(domain_name, get_global_sam_name())) {
394 return find_domain_from_name_noinit(domain_name);
397 /* we can auth against trusted domains */
398 if (flags & WBFLAG_PAM_CONTACT_TRUSTDOM) {
399 domain = find_domain_from_name_noinit(domain_name);
400 if (domain == NULL) {
401 DEBUG(3, ("Authentication for domain [%s] skipped "
402 "as it is not a trusted domain\n",
409 return find_our_domain();
412 static void fake_password_policy(struct winbindd_response *r,
413 const struct netr_SamBaseInfo *bi)
415 NTTIME min_password_age;
416 NTTIME max_password_age;
418 if (bi->allow_password_change > bi->last_password_change) {
419 min_password_age = bi->allow_password_change -
420 bi->last_password_change;
422 min_password_age = 0;
425 if (bi->force_password_change > bi->last_password_change) {
426 max_password_age = bi->force_password_change -
427 bi->last_password_change;
429 max_password_age = 0;
432 r->data.auth.policy.min_length_password = 0;
433 r->data.auth.policy.password_history = 0;
434 r->data.auth.policy.password_properties = 0;
435 r->data.auth.policy.expire =
436 nt_time_to_unix_abs(&max_password_age);
437 r->data.auth.policy.min_passwordage =
438 nt_time_to_unix_abs(&min_password_age);
441 static void fill_in_password_policy(struct winbindd_response *r,
442 const struct samr_DomInfo1 *p)
444 r->data.auth.policy.min_length_password =
445 p->min_password_length;
446 r->data.auth.policy.password_history =
447 p->password_history_length;
448 r->data.auth.policy.password_properties =
449 p->password_properties;
450 r->data.auth.policy.expire =
451 nt_time_to_unix_abs((const NTTIME *)&(p->max_password_age));
452 r->data.auth.policy.min_passwordage =
453 nt_time_to_unix_abs((const NTTIME *)&(p->min_password_age));
456 static NTSTATUS fillup_password_policy(struct winbindd_domain *domain,
457 struct winbindd_response *response)
459 TALLOC_CTX *frame = talloc_stackframe();
461 struct samr_DomInfo1 password_policy;
463 if ( !winbindd_can_contact_domain( domain ) ) {
464 DEBUG(5,("fillup_password_policy: No inbound trust to "
465 "contact domain %s\n", domain->name));
466 status = NT_STATUS_NOT_SUPPORTED;
470 status = wb_cache_password_policy(domain, talloc_tos(),
472 if (NT_STATUS_IS_ERR(status)) {
476 fill_in_password_policy(response, &password_policy);
483 static NTSTATUS get_max_bad_attempts_from_lockout_policy(struct winbindd_domain *domain,
485 uint16_t *lockout_threshold)
487 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
488 struct samr_DomInfo12 lockout_policy;
490 *lockout_threshold = 0;
492 status = wb_cache_lockout_policy(domain, mem_ctx, &lockout_policy);
493 if (NT_STATUS_IS_ERR(status)) {
497 *lockout_threshold = lockout_policy.lockout_threshold;
502 static NTSTATUS get_pwd_properties(struct winbindd_domain *domain,
504 uint32_t *password_properties)
506 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
507 struct samr_DomInfo1 password_policy;
509 *password_properties = 0;
511 status = wb_cache_password_policy(domain, mem_ctx, &password_policy);
512 if (NT_STATUS_IS_ERR(status)) {
516 *password_properties = password_policy.password_properties;
523 static const char *generate_krb5_ccache(TALLOC_CTX *mem_ctx,
526 const char **user_ccache_file)
528 /* accept FILE and WRFILE as krb5_cc_type from the client and then
529 * build the full ccname string based on the user's uid here -
532 const char *gen_cc = NULL;
535 if (strequal(type, "FILE")) {
536 gen_cc = talloc_asprintf(
537 mem_ctx, "FILE:/tmp/krb5cc_%d", uid);
539 if (strequal(type, "WRFILE")) {
540 gen_cc = talloc_asprintf(
541 mem_ctx, "WRFILE:/tmp/krb5cc_%d", uid);
543 if (strequal(type, "KEYRING")) {
544 gen_cc = talloc_asprintf(
545 mem_ctx, "KEYRING:persistent:%d", uid);
548 if (strnequal(type, "FILE:/", 6) ||
549 strnequal(type, "WRFILE:/", 8) ||
550 strnequal(type, "DIR:/", 5)) {
552 /* we allow only one "%u" substitution */
556 p = strchr(type, '%');
561 if (p != NULL && *p == 'u' && strchr(p, '%') == NULL) {
562 char uid_str[sizeof("18446744073709551615")];
564 snprintf(uid_str, sizeof(uid_str), "%u", uid);
566 gen_cc = talloc_string_sub2(mem_ctx,
570 /* remove_unsafe_characters */
574 /* allow_trailing_dollar */
581 *user_ccache_file = gen_cc;
583 if (gen_cc == NULL) {
584 gen_cc = talloc_strdup(mem_ctx, "MEMORY:winbindd_pam_ccache");
586 if (gen_cc == NULL) {
587 DEBUG(0,("out of memory\n"));
591 DEBUG(10, ("using ccache: %s%s\n", gen_cc,
592 (*user_ccache_file == NULL) ? " (internal)":""));
599 uid_t get_uid_from_request(struct winbindd_request *request)
603 uid = request->data.auth.uid;
605 if (uid == (uid_t)-1) {
606 DEBUG(1,("invalid uid: '%u'\n", (unsigned int)uid));
612 /**********************************************************************
613 Authenticate a user with a clear text password using Kerberos and fill up
615 **********************************************************************/
617 static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
618 struct winbindd_domain *domain,
621 const char *krb5_cc_type,
623 struct netr_SamInfo3 **info3,
627 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
628 krb5_error_code krb5_ret;
629 const char *cc = NULL;
630 const char *principal_s = NULL;
631 const char *service = NULL;
633 fstring name_domain, name_user;
634 time_t ticket_lifetime = 0;
635 time_t renewal_until = 0;
637 time_t time_offset = 0;
638 const char *user_ccache_file;
639 struct PAC_LOGON_INFO *logon_info = NULL;
640 struct PAC_DATA *pac_data = NULL;
641 struct PAC_DATA_CTR *pac_data_ctr = NULL;
642 const char *local_service;
644 struct netr_SamInfo3 *info3_copy = NULL;
648 if (domain->alt_name == NULL) {
649 return NT_STATUS_INVALID_PARAMETER;
653 * prepare a krb5_cc_cache string for the user */
656 DEBUG(0,("no valid uid\n"));
659 cc = generate_krb5_ccache(mem_ctx,
664 return NT_STATUS_NO_MEMORY;
669 * get kerberos properties */
671 if (domain->private_data) {
672 ads = (ADS_STRUCT *)domain->private_data;
673 time_offset = ads->auth.time_offset;
678 * do kerberos auth and setup ccache as the user */
680 parse_domain_user(user, name_domain, name_user);
682 realm = talloc_strdup(mem_ctx, domain->alt_name);
684 return NT_STATUS_NO_MEMORY;
687 if (!strupper_m(realm)) {
688 return NT_STATUS_INVALID_PARAMETER;
691 principal_s = talloc_asprintf(mem_ctx, "%s@%s", name_user, realm);
692 if (principal_s == NULL) {
693 return NT_STATUS_NO_MEMORY;
696 service = talloc_asprintf(mem_ctx, "%s/%s@%s", KRB5_TGS_NAME, realm, realm);
697 if (service == NULL) {
698 return NT_STATUS_NO_MEMORY;
701 local_service = talloc_asprintf(mem_ctx, "%s$@%s",
702 lp_netbios_name(), lp_realm());
703 if (local_service == NULL) {
704 return NT_STATUS_NO_MEMORY;
708 /* if this is a user ccache, we need to act as the user to let the krb5
709 * library handle the chown, etc. */
711 /************************ ENTERING NON-ROOT **********************/
713 if (user_ccache_file != NULL) {
714 set_effective_uid(uid);
715 DEBUG(10,("winbindd_raw_kerberos_login: uid is %d\n", uid));
718 result = kerberos_return_pac(mem_ctx,
727 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
731 if (user_ccache_file != NULL) {
732 gain_root_privilege();
735 /************************ RETURNED TO ROOT **********************/
737 if (!NT_STATUS_IS_OK(result)) {
741 if (pac_data_ctr == NULL) {
745 pac_data = pac_data_ctr->pac_data;
746 if (pac_data == NULL) {
750 for (i=0; i < pac_data->num_buffers; i++) {
752 if (pac_data->buffers[i].type != PAC_TYPE_LOGON_INFO) {
756 logon_info = pac_data->buffers[i].info->logon_info.info;
758 return NT_STATUS_INVALID_PARAMETER;
764 if (logon_info == NULL) {
765 DEBUG(10,("Missing logon_info in ticket of %s\n",
767 return NT_STATUS_INVALID_PARAMETER;
770 DEBUG(10,("winbindd_raw_kerberos_login: winbindd validated ticket of %s\n",
773 result = create_info3_from_pac_logon_info(mem_ctx, logon_info, &info3_copy);
774 if (!NT_STATUS_IS_OK(result)) {
778 /* if we had a user's ccache then return that string for the pam
781 if (user_ccache_file != NULL) {
783 fstrcpy(krb5ccname, user_ccache_file);
785 result = add_ccache_to_list(principal_s,
797 if (!NT_STATUS_IS_OK(result)) {
798 DEBUG(10,("winbindd_raw_kerberos_login: failed to add ccache to list: %s\n",
803 /* need to delete the memory cred cache, it is not used anymore */
805 krb5_ret = ads_kdestroy(cc);
807 DEBUG(3,("winbindd_raw_kerberos_login: "
808 "could not destroy krb5 credential cache: "
809 "%s\n", error_message(krb5_ret)));
818 * Do not delete an existing valid credential cache, if the user
819 * e.g. enters a wrong password
821 if ((strequal(krb5_cc_type, "FILE") || strequal(krb5_cc_type, "WRFILE"))
822 && user_ccache_file != NULL) {
826 /* we could have created a new credential cache with a valid tgt in it
827 * but we werent able to get or verify the service ticket for this
828 * local host and therefor didn't get the PAC, we need to remove that
829 * cache entirely now */
831 krb5_ret = ads_kdestroy(cc);
833 DEBUG(3,("winbindd_raw_kerberos_login: "
834 "could not destroy krb5 credential cache: "
835 "%s\n", error_message(krb5_ret)));
838 if (!NT_STATUS_IS_OK(remove_ccache(user))) {
839 DEBUG(3,("winbindd_raw_kerberos_login: "
840 "could not remove ccache for user %s\n",
846 return NT_STATUS_NOT_SUPPORTED;
847 #endif /* HAVE_KRB5 */
850 /****************************************************************
851 ****************************************************************/
853 bool check_request_flags(uint32_t flags)
855 uint32_t flags_edata = WBFLAG_PAM_AFS_TOKEN |
856 WBFLAG_PAM_INFO3_TEXT |
857 WBFLAG_PAM_INFO3_NDR;
859 if ( ( (flags & flags_edata) == WBFLAG_PAM_AFS_TOKEN) ||
860 ( (flags & flags_edata) == WBFLAG_PAM_INFO3_NDR) ||
861 ( (flags & flags_edata) == WBFLAG_PAM_INFO3_TEXT)||
862 !(flags & flags_edata) ) {
866 DEBUG(1, ("check_request_flags: invalid request flags[0x%08X]\n",
872 /****************************************************************
873 ****************************************************************/
875 NTSTATUS append_auth_data(TALLOC_CTX *mem_ctx,
876 struct winbindd_response *resp,
877 uint32_t request_flags,
878 uint16_t validation_level,
879 union netr_Validation *validation,
880 const char *name_domain,
881 const char *name_user)
883 struct netr_SamInfo3 *info3 = NULL;
886 result = map_validation_to_info3(talloc_tos(),
890 if (!NT_STATUS_IS_OK(result)) {
894 if (request_flags & WBFLAG_PAM_USER_SESSION_KEY) {
895 memcpy(resp->data.auth.user_session_key,
897 sizeof(resp->data.auth.user_session_key)
901 if (request_flags & WBFLAG_PAM_LMKEY) {
902 memcpy(resp->data.auth.first_8_lm_hash,
903 info3->base.LMSessKey.key,
904 sizeof(resp->data.auth.first_8_lm_hash)
908 if (request_flags & WBFLAG_PAM_UNIX_NAME) {
909 result = append_unix_username(mem_ctx, resp,
910 info3, name_domain, name_user);
911 if (!NT_STATUS_IS_OK(result)) {
912 DEBUG(10,("Failed to append Unix Username: %s\n",
919 /* currently, anything from here on potentially overwrites extra_data. */
921 if (request_flags & WBFLAG_PAM_INFO3_NDR) {
922 result = append_info3_as_ndr(mem_ctx, resp, info3);
923 if (!NT_STATUS_IS_OK(result)) {
924 DEBUG(10,("Failed to append INFO3 (NDR): %s\n",
931 if (request_flags & WBFLAG_PAM_INFO3_TEXT) {
932 result = append_info3_as_txt(mem_ctx, resp,
935 if (!NT_STATUS_IS_OK(result)) {
936 DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
943 if (request_flags & WBFLAG_PAM_AFS_TOKEN) {
944 result = append_afs_token(mem_ctx, resp,
945 info3, name_domain, name_user);
946 if (!NT_STATUS_IS_OK(result)) {
947 DEBUG(10,("Failed to append AFS token: %s\n",
958 static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
959 struct winbindd_cli_state *state,
960 struct netr_SamInfo3 **info3)
962 NTSTATUS result = NT_STATUS_LOGON_FAILURE;
963 uint16_t max_allowed_bad_attempts;
964 fstring name_domain, name_user;
966 enum lsa_SidType type;
967 uchar new_nt_pass[NT_HASH_LEN];
968 const uint8_t *cached_nt_pass;
969 const uint8_t *cached_salt;
970 struct netr_SamInfo3 *my_info3;
971 time_t kickoff_time, must_change_time;
972 bool password_good = false;
974 struct winbindd_tdc_domain *tdc_domain = NULL;
981 DEBUG(10,("winbindd_dual_pam_auth_cached\n"));
983 /* Parse domain and username */
985 parse_domain_user(state->request->data.auth.user, name_domain, name_user);
988 if (!lookup_cached_name(name_domain,
992 DEBUG(10,("winbindd_dual_pam_auth_cached: no such user in the cache\n"));
993 return NT_STATUS_NO_SUCH_USER;
996 if (type != SID_NAME_USER) {
997 DEBUG(10,("winbindd_dual_pam_auth_cached: not a user (%s)\n", sid_type_lookup(type)));
998 return NT_STATUS_LOGON_FAILURE;
1001 result = winbindd_get_creds(domain,
1007 if (!NT_STATUS_IS_OK(result)) {
1008 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get creds: %s\n", nt_errstr(result)));
1014 E_md4hash(state->request->data.auth.pass, new_nt_pass);
1016 dump_data_pw("new_nt_pass", new_nt_pass, NT_HASH_LEN);
1017 dump_data_pw("cached_nt_pass", cached_nt_pass, NT_HASH_LEN);
1019 dump_data_pw("cached_salt", cached_salt, NT_HASH_LEN);
1023 /* In this case we didn't store the nt_hash itself,
1024 but the MD5 combination of salt + nt_hash. */
1025 uchar salted_hash[NT_HASH_LEN];
1026 E_md5hash(cached_salt, new_nt_pass, salted_hash);
1028 password_good = (memcmp(cached_nt_pass, salted_hash,
1031 /* Old cached cred - direct store of nt_hash (bad bad bad !). */
1032 password_good = (memcmp(cached_nt_pass, new_nt_pass,
1036 if (password_good) {
1038 /* User *DOES* know the password, update logon_time and reset
1041 my_info3->base.user_flags |= NETLOGON_CACHED_ACCOUNT;
1043 if (my_info3->base.acct_flags & ACB_AUTOLOCK) {
1044 return NT_STATUS_ACCOUNT_LOCKED_OUT;
1047 if (my_info3->base.acct_flags & ACB_DISABLED) {
1048 return NT_STATUS_ACCOUNT_DISABLED;
1051 if (my_info3->base.acct_flags & ACB_WSTRUST) {
1052 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
1055 if (my_info3->base.acct_flags & ACB_SVRTRUST) {
1056 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
1059 if (my_info3->base.acct_flags & ACB_DOMTRUST) {
1060 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
1063 if (!(my_info3->base.acct_flags & ACB_NORMAL)) {
1064 DEBUG(0,("winbindd_dual_pam_auth_cached: whats wrong with that one?: 0x%08x\n",
1065 my_info3->base.acct_flags));
1066 return NT_STATUS_LOGON_FAILURE;
1069 kickoff_time = nt_time_to_unix(my_info3->base.kickoff_time);
1070 if (kickoff_time != 0 && time(NULL) > kickoff_time) {
1071 return NT_STATUS_ACCOUNT_EXPIRED;
1074 must_change_time = nt_time_to_unix(my_info3->base.force_password_change);
1075 if (must_change_time != 0 && must_change_time < time(NULL)) {
1076 /* we allow grace logons when the password has expired */
1077 my_info3->base.user_flags |= NETLOGON_GRACE_LOGON;
1078 /* return NT_STATUS_PASSWORD_EXPIRED; */
1083 if ((state->request->flags & WBFLAG_PAM_KRB5) &&
1084 ((tdc_domain = wcache_tdc_fetch_domain(state->mem_ctx, name_domain)) != NULL) &&
1085 ((tdc_domain->trust_type & LSA_TRUST_TYPE_UPLEVEL) ||
1086 /* used to cope with the case winbindd starting without network. */
1087 !strequal(tdc_domain->domain_name, tdc_domain->dns_name))) {
1090 const char *cc = NULL;
1092 const char *principal_s = NULL;
1093 const char *service = NULL;
1094 const char *user_ccache_file;
1096 if (domain->alt_name == NULL) {
1097 return NT_STATUS_INVALID_PARAMETER;
1100 uid = get_uid_from_request(state->request);
1102 DEBUG(0,("winbindd_dual_pam_auth_cached: invalid uid\n"));
1103 return NT_STATUS_INVALID_PARAMETER;
1106 cc = generate_krb5_ccache(state->mem_ctx,
1107 state->request->data.auth.krb5_cc_type,
1108 state->request->data.auth.uid,
1111 return NT_STATUS_NO_MEMORY;
1114 realm = talloc_strdup(state->mem_ctx, domain->alt_name);
1115 if (realm == NULL) {
1116 return NT_STATUS_NO_MEMORY;
1119 if (!strupper_m(realm)) {
1120 return NT_STATUS_INVALID_PARAMETER;
1123 principal_s = talloc_asprintf(state->mem_ctx, "%s@%s", name_user, realm);
1124 if (principal_s == NULL) {
1125 return NT_STATUS_NO_MEMORY;
1128 service = talloc_asprintf(state->mem_ctx, "%s/%s@%s", KRB5_TGS_NAME, realm, realm);
1129 if (service == NULL) {
1130 return NT_STATUS_NO_MEMORY;
1133 if (user_ccache_file != NULL) {
1135 fstrcpy(state->response->data.auth.krb5ccname,
1138 result = add_ccache_to_list(principal_s,
1141 state->request->data.auth.user,
1142 state->request->data.auth.pass,
1146 time(NULL) + lp_winbind_cache_time(),
1147 time(NULL) + WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
1150 if (!NT_STATUS_IS_OK(result)) {
1151 DEBUG(10,("winbindd_dual_pam_auth_cached: failed "
1152 "to add ccache to list: %s\n",
1153 nt_errstr(result)));
1157 #endif /* HAVE_KRB5 */
1159 /* FIXME: we possibly should handle logon hours as well (does xp when
1160 * offline?) see auth/auth_sam.c:sam_account_ok for details */
1162 unix_to_nt_time(&my_info3->base.logon_time, time(NULL));
1163 my_info3->base.bad_password_count = 0;
1165 result = winbindd_update_creds_by_info3(domain,
1166 state->request->data.auth.user,
1167 state->request->data.auth.pass,
1169 if (!NT_STATUS_IS_OK(result)) {
1170 DEBUG(1,("winbindd_dual_pam_auth_cached: failed to update creds: %s\n",
1171 nt_errstr(result)));
1175 return NT_STATUS_OK;
1179 /* User does *NOT* know the correct password, modify info3 accordingly, but only if online */
1180 if (domain->online == false) {
1184 /* failure of this is not critical */
1185 result = get_max_bad_attempts_from_lockout_policy(domain, state->mem_ctx, &max_allowed_bad_attempts);
1186 if (!NT_STATUS_IS_OK(result)) {
1187 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get max_allowed_bad_attempts. "
1188 "Won't be able to honour account lockout policies\n"));
1191 /* increase counter */
1192 my_info3->base.bad_password_count++;
1194 if (max_allowed_bad_attempts == 0) {
1199 if (my_info3->base.bad_password_count >= max_allowed_bad_attempts) {
1201 uint32_t password_properties;
1203 result = get_pwd_properties(domain, state->mem_ctx, &password_properties);
1204 if (!NT_STATUS_IS_OK(result)) {
1205 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get password properties.\n"));
1208 if ((my_info3->base.rid != DOMAIN_RID_ADMINISTRATOR) ||
1209 (password_properties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)) {
1210 my_info3->base.acct_flags |= ACB_AUTOLOCK;
1215 result = winbindd_update_creds_by_info3(domain,
1216 state->request->data.auth.user,
1220 if (!NT_STATUS_IS_OK(result)) {
1221 DEBUG(0,("winbindd_dual_pam_auth_cached: failed to update creds %s\n",
1222 nt_errstr(result)));
1225 return NT_STATUS_LOGON_FAILURE;
1228 static NTSTATUS winbindd_dual_pam_auth_kerberos(struct winbindd_domain *domain,
1229 struct winbindd_cli_state *state,
1230 struct netr_SamInfo3 **info3)
1232 struct winbindd_domain *contact_domain;
1233 fstring name_domain, name_user;
1236 DEBUG(10,("winbindd_dual_pam_auth_kerberos\n"));
1238 /* Parse domain and username */
1240 parse_domain_user(state->request->data.auth.user, name_domain, name_user);
1242 /* what domain should we contact? */
1245 if (!(contact_domain = find_domain_from_name(name_domain))) {
1246 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1247 state->request->data.auth.user, name_domain, name_user, name_domain));
1248 result = NT_STATUS_NO_SUCH_USER;
1253 if (is_myname(name_domain)) {
1254 DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
1255 result = NT_STATUS_NO_SUCH_USER;
1259 contact_domain = find_domain_from_name(name_domain);
1260 if (contact_domain == NULL) {
1261 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1262 state->request->data.auth.user, name_domain, name_user, name_domain));
1264 result = NT_STATUS_NO_SUCH_USER;
1269 if (contact_domain->initialized &&
1270 contact_domain->active_directory) {
1274 if (!contact_domain->initialized) {
1275 init_dc_connection(contact_domain, false);
1278 if (!contact_domain->active_directory) {
1279 DEBUG(3,("krb5 auth requested but domain is not Active Directory\n"));
1280 return NT_STATUS_INVALID_LOGON_TYPE;
1283 result = winbindd_raw_kerberos_login(
1284 state->mem_ctx, contact_domain,
1285 state->request->data.auth.user,
1286 state->request->data.auth.pass,
1287 state->request->data.auth.krb5_cc_type,
1288 get_uid_from_request(state->request),
1289 info3, state->response->data.auth.krb5ccname);
1294 static NTSTATUS winbindd_dual_auth_passdb(TALLOC_CTX *mem_ctx,
1295 uint32_t logon_parameters,
1296 const char *domain, const char *user,
1297 const DATA_BLOB *challenge,
1298 const DATA_BLOB *lm_resp,
1299 const DATA_BLOB *nt_resp,
1301 uint8_t *pauthoritative,
1302 struct netr_SamInfo3 **pinfo3)
1304 struct auth_context *auth_context;
1305 struct auth_serversupplied_info *server_info;
1306 struct auth_usersupplied_info *user_info = NULL;
1307 struct tsocket_address *local;
1308 struct netr_SamInfo3 *info3;
1312 TALLOC_CTX *frame = talloc_stackframe();
1315 * We are authoritative by default
1317 *pauthoritative = 1;
1319 rc = tsocket_address_inet_from_strings(frame,
1326 return NT_STATUS_NO_MEMORY;
1330 * TODO: We should get the service description passed in from
1331 * the winbind client, so we can have "smb2", "squid" or "samr" logged
1334 status = make_user_info(frame, &user_info, user, user, domain, domain,
1335 lp_netbios_name(), local, local,
1337 lm_resp, nt_resp, NULL, NULL,
1338 NULL, AUTH_PASSWORD_RESPONSE);
1339 if (!NT_STATUS_IS_OK(status)) {
1340 DEBUG(10, ("make_user_info failed: %s\n", nt_errstr(status)));
1345 user_info->logon_parameters = logon_parameters;
1347 /* We don't want any more mapping of the username */
1348 user_info->mapped_state = True;
1350 /* We don't want to come back to winbindd or to do PAM account checks */
1351 user_info->flags |= USER_INFO_INFO3_AND_NO_AUTHZ;
1354 user_info->flags |= USER_INFO_INTERACTIVE_LOGON;
1357 status = make_auth3_context_for_winbind(frame, &auth_context);
1358 if (!NT_STATUS_IS_OK(status)) {
1359 DBG_ERR("make_auth3_context_for_winbind failed: %s\n",
1365 ok = auth3_context_set_challenge(auth_context,
1366 challenge->data, "fixed");
1369 return NT_STATUS_NO_MEMORY;
1372 status = auth_check_ntlm_password(mem_ctx,
1377 if (!NT_STATUS_IS_OK(status)) {
1382 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
1383 if (info3 == NULL) {
1385 return NT_STATUS_NO_MEMORY;
1388 status = serverinfo_to_SamInfo3(server_info, info3);
1389 if (!NT_STATUS_IS_OK(status)) {
1392 DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
1393 nt_errstr(status)));
1398 DEBUG(10, ("Authenticaticating user %s\\%s returned %s\n", domain,
1399 user, nt_errstr(status)));
1404 static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
1405 TALLOC_CTX *mem_ctx,
1406 uint32_t logon_parameters,
1407 const char *username,
1408 const char *password,
1409 const char *domainname,
1410 const char *workstation,
1411 const uint8_t chal[8],
1412 DATA_BLOB lm_response,
1413 DATA_BLOB nt_response,
1415 uint8_t *authoritative,
1417 uint16_t *_validation_level,
1418 union netr_Validation **_validation)
1421 int netr_attempts = 0;
1424 uint16_t validation_level;
1425 union netr_Validation *validation = NULL;
1428 struct rpc_pipe_client *netlogon_pipe;
1432 result = cm_connect_netlogon(domain, &netlogon_pipe);
1434 if (NT_STATUS_EQUAL(result,
1435 NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
1437 * This means we don't have a trust account.
1440 result = NT_STATUS_NO_SUCH_USER;
1444 if (!NT_STATUS_IS_OK(result)) {
1445 DEBUG(3,("Could not open handle to NETLOGON pipe "
1446 "(error: %s, attempts: %d)\n",
1447 nt_errstr(result), netr_attempts));
1449 /* After the first retry always close the connection */
1450 if (netr_attempts > 0) {
1451 DEBUG(3, ("This is again a problem for this "
1452 "particular call, forcing the close "
1453 "of this connection\n"));
1454 invalidate_cm_connection(domain);
1457 /* After the second retry failover to the next DC */
1458 if (netr_attempts > 1) {
1460 * If the netlogon server is not reachable then
1461 * it is possible that the DC is rebuilding
1462 * sysvol and shutdown netlogon for that time.
1463 * We should failover to the next dc.
1465 DEBUG(3, ("This is the third problem for this "
1466 "particular call, adding DC to the "
1467 "negative cache list: %s %s\n", domain->name, domain->dcname));
1468 add_failed_connection_entry(domain->name,
1471 saf_delete(domain->name);
1474 /* Only allow 3 retries */
1475 if (netr_attempts < 3) {
1476 DEBUG(3, ("The connection to netlogon "
1477 "failed, retrying\n"));
1485 if (domain->conn.netlogon_creds_ctx == NULL) {
1486 DBG_NOTICE("No security credentials available for "
1487 "domain [%s]\n", domainname);
1488 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1489 } else if (interactive) {
1490 result = rpccli_netlogon_password_logon(
1491 domain->conn.netlogon_creds_ctx,
1492 netlogon_pipe->binding_handle,
1499 NetlogonInteractiveInformation,
1505 result = rpccli_netlogon_network_logon(
1506 domain->conn.netlogon_creds_ctx,
1507 netlogon_pipe->binding_handle,
1523 * we increment this after the "feature negotiation"
1524 * for can_do_samlogon_ex and can_do_validation6
1528 /* We have to try a second time as cm_connect_netlogon
1529 might not yet have noticed that the DC has killed
1532 if (!rpccli_is_connected(netlogon_pipe)) {
1537 /* if we get access denied, a possible cause was that we had
1538 and open connection to the DC, but someone changed our
1539 machine account password out from underneath us using 'net
1540 rpc changetrustpw' */
1542 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1543 DEBUG(1,("winbind_samlogon_retry_loop: sam_logon returned "
1544 "ACCESS_DENIED. Maybe the DC has Restrict "
1545 "NTLM set or the trust account "
1546 "password was changed and we didn't know it. "
1547 "Killing connections to domain %s\n",
1549 invalidate_cm_connection(domain);
1553 if (NT_STATUS_EQUAL(result, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
1555 * Got DCERPC_FAULT_OP_RNG_ERROR for SamLogon
1556 * (no Ex). This happens against old Samba
1557 * DCs, if LogonSamLogonEx() fails with an error
1558 * e.g. NT_STATUS_NO_SUCH_USER or NT_STATUS_WRONG_PASSWORD.
1560 * The server will log something like this:
1561 * api_net_sam_logon_ex: Failed to marshall NET_R_SAM_LOGON_EX.
1563 * This sets the whole connection into a fault_state mode
1564 * and all following request get NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE.
1566 * This also happens to our retry with LogonSamLogonWithFlags()
1567 * and LogonSamLogon().
1569 * In order to recover from this situation, we need to
1570 * drop the connection.
1572 invalidate_cm_connection(domain);
1573 result = NT_STATUS_LOGON_FAILURE;
1577 } while ( (attempts < 2) && retry );
1579 if (NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT)) {
1580 DEBUG(3,("winbind_samlogon_retry_loop: sam_network_logon(ex) "
1581 "returned NT_STATUS_IO_TIMEOUT after the retry. "
1582 "Killing connections to domain %s\n",
1584 invalidate_cm_connection(domain);
1587 if (!NT_STATUS_IS_OK(result)) {
1591 *_validation_level = validation_level;
1592 *_validation = validation;
1593 return NT_STATUS_OK;
1596 static NTSTATUS winbindd_dual_pam_auth_samlogon(
1597 TALLOC_CTX *mem_ctx,
1598 struct winbindd_domain *domain,
1601 uint32_t request_flags,
1602 uint16_t *_validation_level,
1603 union netr_Validation **_validation)
1609 unsigned char local_nt_response[24];
1610 fstring name_domain, name_user;
1612 uint8_t authoritative = 0;
1614 uint16_t validation_level;
1615 union netr_Validation *validation = NULL;
1616 struct netr_SamBaseInfo *base_info = NULL;
1618 DEBUG(10,("winbindd_dual_pam_auth_samlogon\n"));
1620 /* Parse domain and username */
1622 parse_domain_user(user, name_domain, name_user);
1625 * We check against domain->name instead of
1626 * name_domain, as find_auth_domain() ->
1627 * find_domain_from_name_noinit() already decided
1628 * that we are in a child for the correct domain.
1630 * name_domain can also be lp_realm()
1631 * we need to check against domain->name.
1633 if (strequal(domain->name, get_global_sam_name())) {
1634 DATA_BLOB chal_blob = data_blob_const(chal, sizeof(chal));
1635 struct netr_SamInfo3 *info3 = NULL;
1637 /* do password magic */
1639 generate_random_buffer(chal, sizeof(chal));
1641 if (lp_client_ntlmv2_auth()) {
1642 DATA_BLOB server_chal;
1643 DATA_BLOB names_blob;
1644 server_chal = data_blob_const(chal, 8);
1646 /* note that the 'workgroup' here is for the local
1647 machine. The 'server name' must match the
1648 'workstation' passed to the actual SamLogon call.
1650 names_blob = NTLMv2_generate_names_blob(
1651 mem_ctx, lp_netbios_name(), lp_workgroup());
1653 if (!SMBNTLMv2encrypt(mem_ctx, name_user, name_domain,
1657 &lm_resp, &nt_resp, NULL, NULL)) {
1658 data_blob_free(&names_blob);
1659 DEBUG(0, ("winbindd_pam_auth: SMBNTLMv2encrypt() failed!\n"));
1660 result = NT_STATUS_NO_MEMORY;
1663 data_blob_free(&names_blob);
1665 lm_resp = data_blob_null;
1666 SMBNTencrypt(pass, chal, local_nt_response);
1668 nt_resp = data_blob_talloc(mem_ctx, local_nt_response,
1669 sizeof(local_nt_response));
1672 result = winbindd_dual_auth_passdb(
1673 talloc_tos(), 0, name_domain, name_user,
1674 &chal_blob, &lm_resp, &nt_resp,
1675 true, /* interactive */
1678 if (NT_STATUS_IS_OK(result)) {
1679 result = map_info3_to_validation(mem_ctx,
1684 if (!NT_STATUS_IS_OK(result)) {
1690 * We need to try the remote NETLOGON server if this is
1691 * not authoritative (for example on the RODC).
1693 if (authoritative != 0) {
1698 /* check authentication loop */
1700 result = winbind_samlogon_retry_loop(domain,
1708 data_blob_null, data_blob_null,
1709 true, /* interactive */
1714 if (!NT_STATUS_IS_OK(result)) {
1718 /* handle the case where a NT4 DC does not fill in the acct_flags in
1719 * the samlogon reply info3. When accurate info3 is required by the
1720 * caller, we look up the account flags ourselves - gd */
1722 switch (validation_level) {
1724 base_info = &validation->sam3->base;
1727 base_info = &validation->sam6->base;
1730 DBG_ERR("Bad validation level %d", (int)validation_level);
1731 result = NT_STATUS_INTERNAL_ERROR;
1734 if ((request_flags & WBFLAG_PAM_INFO3_TEXT) &&
1735 (base_info->acct_flags == 0))
1737 struct rpc_pipe_client *samr_pipe;
1738 struct policy_handle samr_domain_handle, user_pol;
1739 union samr_UserInfo *info = NULL;
1740 NTSTATUS status_tmp, result_tmp;
1741 uint32_t acct_flags;
1742 struct dcerpc_binding_handle *b;
1744 status_tmp = cm_connect_sam(domain, mem_ctx, false,
1745 &samr_pipe, &samr_domain_handle);
1747 if (!NT_STATUS_IS_OK(status_tmp)) {
1748 DEBUG(3, ("could not open handle to SAMR pipe: %s\n",
1749 nt_errstr(status_tmp)));
1753 b = samr_pipe->binding_handle;
1755 status_tmp = dcerpc_samr_OpenUser(b, mem_ctx,
1756 &samr_domain_handle,
1757 MAXIMUM_ALLOWED_ACCESS,
1762 if (!NT_STATUS_IS_OK(status_tmp)) {
1763 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1764 nt_errstr(status_tmp)));
1767 if (!NT_STATUS_IS_OK(result_tmp)) {
1768 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1769 nt_errstr(result_tmp)));
1773 status_tmp = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1779 if (any_nt_status_not_ok(status_tmp, result_tmp,
1781 DEBUG(3, ("could not query user info on SAMR pipe: %s\n",
1782 nt_errstr(status_tmp)));
1783 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1787 acct_flags = info->info16.acct_flags;
1789 if (acct_flags == 0) {
1790 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1794 base_info->acct_flags = acct_flags;
1796 DEBUG(10,("successfully retrieved acct_flags 0x%x\n", acct_flags));
1798 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1802 if (NT_STATUS_IS_OK(result)) {
1803 *_validation_level = validation_level;
1804 *_validation = validation;
1809 enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
1810 struct winbindd_cli_state *state)
1812 NTSTATUS result = NT_STATUS_LOGON_FAILURE;
1813 NTSTATUS krb5_result = NT_STATUS_OK;
1814 fstring name_domain, name_user;
1816 fstring domain_user;
1817 uint16_t validation_level;
1818 union netr_Validation *validation = NULL;
1819 NTSTATUS name_map_status = NT_STATUS_UNSUCCESSFUL;
1821 /* Ensure null termination */
1822 state->request->data.auth.user[sizeof(state->request->data.auth.user)-1]='\0';
1824 /* Ensure null termination */
1825 state->request->data.auth.pass[sizeof(state->request->data.auth.pass)-1]='\0';
1827 DEBUG(3, ("[%5lu]: dual pam auth %s\n", (unsigned long)state->pid,
1828 state->request->data.auth.user));
1830 /* Parse domain and username */
1832 name_map_status = normalize_name_unmap(state->mem_ctx,
1833 state->request->data.auth.user,
1836 /* If the name normalization didnt' actually do anything,
1837 just use the original name */
1839 if (!NT_STATUS_IS_OK(name_map_status) &&
1840 !NT_STATUS_EQUAL(name_map_status, NT_STATUS_FILE_RENAMED))
1842 mapped_user = state->request->data.auth.user;
1845 parse_domain_user(mapped_user, name_domain, name_user);
1847 if ( mapped_user != state->request->data.auth.user ) {
1848 fstr_sprintf( domain_user, "%s%c%s", name_domain,
1849 *lp_winbind_separator(),
1851 strlcpy( state->request->data.auth.user, domain_user,
1852 sizeof(state->request->data.auth.user));
1855 if (!domain->online) {
1856 result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1857 if (domain->startup) {
1858 /* Logons are very important to users. If we're offline and
1859 we get a request within the first 30 seconds of startup,
1860 try very hard to find a DC and go online. */
1862 DEBUG(10,("winbindd_dual_pam_auth: domain: %s offline and auth "
1863 "request in startup mode.\n", domain->name ));
1865 winbindd_flush_negative_conn_cache(domain);
1866 result = init_dc_connection(domain, false);
1870 DEBUG(10,("winbindd_dual_pam_auth: domain: %s last was %s\n", domain->name, domain->online ? "online":"offline"));
1872 /* Check for Kerberos authentication */
1873 if (domain->online && (state->request->flags & WBFLAG_PAM_KRB5)) {
1874 struct netr_SamInfo3 *info3 = NULL;
1876 result = winbindd_dual_pam_auth_kerberos(domain, state, &info3);
1877 /* save for later */
1878 krb5_result = result;
1881 if (NT_STATUS_IS_OK(result)) {
1882 DEBUG(10,("winbindd_dual_pam_auth_kerberos succeeded\n"));
1884 result = map_info3_to_validation(state->mem_ctx,
1889 if (!NT_STATUS_IS_OK(result)) {
1890 DBG_ERR("map_info3_to_validation failed\n");
1893 goto process_result;
1896 DBG_DEBUG("winbindd_dual_pam_auth_kerberos failed: %s\n",
1899 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS) ||
1900 NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT) ||
1901 NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
1902 DEBUG(10,("winbindd_dual_pam_auth_kerberos setting domain to offline\n"));
1903 set_domain_offline( domain );
1907 /* there are quite some NT_STATUS errors where there is no
1908 * point in retrying with a samlogon, we explictly have to take
1909 * care not to increase the bad logon counter on the DC */
1911 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_DISABLED) ||
1912 NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_EXPIRED) ||
1913 NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_LOCKED_OUT) ||
1914 NT_STATUS_EQUAL(result, NT_STATUS_INVALID_LOGON_HOURS) ||
1915 NT_STATUS_EQUAL(result, NT_STATUS_INVALID_WORKSTATION) ||
1916 NT_STATUS_EQUAL(result, NT_STATUS_LOGON_FAILURE) ||
1917 NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER) ||
1918 NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED) ||
1919 NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) ||
1920 NT_STATUS_EQUAL(result, NT_STATUS_WRONG_PASSWORD)) {
1924 if (state->request->flags & WBFLAG_PAM_FALLBACK_AFTER_KRB5) {
1925 DEBUG(3,("falling back to samlogon\n"));
1933 /* Check for Samlogon authentication */
1934 if (domain->online) {
1935 struct netr_SamBaseInfo *base_info = NULL;
1937 result = winbindd_dual_pam_auth_samlogon(
1938 state->mem_ctx, domain,
1939 state->request->data.auth.user,
1940 state->request->data.auth.pass,
1941 state->request->flags,
1945 if (NT_STATUS_IS_OK(result)) {
1946 DEBUG(10,("winbindd_dual_pam_auth_samlogon succeeded\n"));
1948 switch (validation_level) {
1950 base_info = &validation->sam3->base;
1953 base_info = &validation->sam6->base;
1956 DBG_ERR("Bad validation level %d\n",
1958 result = NT_STATUS_INTERNAL_ERROR;
1962 /* add the Krb5 err if we have one */
1963 if ( NT_STATUS_EQUAL(krb5_result, NT_STATUS_TIME_DIFFERENCE_AT_DC ) ) {
1964 base_info->user_flags |= LOGON_KRB5_FAIL_CLOCK_SKEW;
1967 goto process_result;
1970 DEBUG(10,("winbindd_dual_pam_auth_samlogon failed: %s\n",
1971 nt_errstr(result)));
1973 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS) ||
1974 NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT) ||
1975 NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND))
1977 DEBUG(10,("winbindd_dual_pam_auth_samlogon setting domain to offline\n"));
1978 set_domain_offline( domain );
1982 if (domain->online) {
1983 /* We're still online - fail. */
1989 /* Check for Cached logons */
1990 if (!domain->online && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN) &&
1991 lp_winbind_offline_logon()) {
1992 struct netr_SamInfo3 *info3 = NULL;
1994 result = winbindd_dual_pam_auth_cached(domain, state, &info3);
1996 if (!NT_STATUS_IS_OK(result)) {
1997 DEBUG(10,("winbindd_dual_pam_auth_cached failed: %s\n", nt_errstr(result)));
2000 DEBUG(10,("winbindd_dual_pam_auth_cached succeeded\n"));
2002 result = map_info3_to_validation(state->mem_ctx,
2007 if (!NT_STATUS_IS_OK(result)) {
2008 DBG_ERR("map_info3_to_validation failed\n");
2015 if (NT_STATUS_IS_OK(result)) {
2016 struct dom_sid user_sid;
2017 TALLOC_CTX *base_ctx = NULL;
2018 struct netr_SamBaseInfo *base_info = NULL;
2019 struct netr_SamInfo3 *info3 = NULL;
2021 switch (validation_level) {
2023 base_ctx = validation->sam3;
2024 base_info = &validation->sam3->base;
2027 base_ctx = validation->sam6;
2028 base_info = &validation->sam6->base;
2031 result = NT_STATUS_INTERNAL_ERROR;
2035 sid_compose(&user_sid, base_info->domain_sid, base_info->rid);
2037 if (base_info->full_name.string == NULL) {
2038 struct netr_SamInfo3 *cached_info3;
2040 cached_info3 = netsamlogon_cache_get(state->mem_ctx,
2042 if (cached_info3 != NULL &&
2043 cached_info3->base.full_name.string != NULL) {
2044 base_info->full_name.string = talloc_strdup(
2046 cached_info3->base.full_name.string);
2047 if (base_info->full_name.string == NULL) {
2048 result = NT_STATUS_NO_MEMORY;
2053 /* this might fail so we don't check the return code */
2054 wcache_query_user_fullname(domain,
2057 &base_info->full_name.string);
2061 result = map_validation_to_info3(talloc_tos(),
2065 if (!NT_STATUS_IS_OK(result)) {
2069 wcache_invalidate_samlogon(find_domain_from_name(name_domain),
2071 netsamlogon_cache_store(name_user, info3);
2073 /* save name_to_sid info as early as possible (only if
2074 this is our primary domain so we don't invalidate
2075 the cache entry by storing the seq_num for the wrong
2077 if ( domain->primary ) {
2078 cache_name2sid(domain, name_domain, name_user,
2079 SID_NAME_USER, &user_sid);
2082 /* Check if the user is in the right group */
2084 result = check_info3_in_group(
2086 state->request->data.auth.require_membership_of_sid);
2087 if (!NT_STATUS_IS_OK(result)) {
2088 DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
2089 state->request->data.auth.user,
2090 state->request->data.auth.require_membership_of_sid));
2094 result = append_auth_data(state->mem_ctx, state->response,
2095 state->request->flags,
2098 name_domain, name_user);
2099 if (!NT_STATUS_IS_OK(result)) {
2103 if ((state->request->flags & WBFLAG_PAM_CACHED_LOGIN)
2104 && lp_winbind_offline_logon()) {
2106 result = winbindd_store_creds(domain,
2107 state->request->data.auth.user,
2108 state->request->data.auth.pass,
2112 if (state->request->flags & WBFLAG_PAM_GET_PWD_POLICY) {
2114 * WBFLAG_PAM_GET_PWD_POLICY is not used within
2115 * any Samba caller anymore.
2117 * We just fake this based on the effective values
2118 * for the user, for legacy callers.
2120 fake_password_policy(state->response, &info3->base);
2123 result = NT_STATUS_OK;
2127 /* give us a more useful (more correct?) error code */
2128 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
2129 (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
2130 result = NT_STATUS_NO_LOGON_SERVERS;
2133 set_auth_errors(state->response, result);
2135 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n",
2136 state->request->data.auth.user,
2137 state->response->data.auth.nt_status_string,
2138 state->response->data.auth.pam_error));
2140 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2143 NTSTATUS winbind_dual_SamLogon(struct winbindd_domain *domain,
2144 TALLOC_CTX *mem_ctx,
2145 uint32_t logon_parameters,
2146 const char *name_user,
2147 const char *name_domain,
2148 const char *workstation,
2149 const uint8_t chal[8],
2150 DATA_BLOB lm_response,
2151 DATA_BLOB nt_response,
2152 uint8_t *authoritative,
2155 uint16_t *_validation_level,
2156 union netr_Validation **_validation)
2158 uint16_t validation_level;
2159 union netr_Validation *validation = NULL;
2163 * We check against domain->name instead of
2164 * name_domain, as find_auth_domain() ->
2165 * find_domain_from_name_noinit() already decided
2166 * that we are in a child for the correct domain.
2168 * name_domain can also be lp_realm()
2169 * we need to check against domain->name.
2171 if (!skip_sam && strequal(domain->name, get_global_sam_name())) {
2172 DATA_BLOB chal_blob = data_blob_const(
2174 struct netr_SamInfo3 *info3 = NULL;
2176 result = winbindd_dual_auth_passdb(
2179 name_domain, name_user,
2180 &chal_blob, &lm_response, &nt_response,
2181 false, /* interactive */
2184 if (NT_STATUS_IS_OK(result)) {
2185 result = map_info3_to_validation(mem_ctx,
2190 if (!NT_STATUS_IS_OK(result)) {
2196 * We need to try the remote NETLOGON server if this is
2197 * not authoritative.
2199 if (*authoritative != 0) {
2201 goto process_result;
2205 result = winbind_samlogon_retry_loop(domain,
2209 NULL, /* password */
2211 /* Bug #3248 - found by Stefan Burkei. */
2212 workstation, /* We carefully set this above so use it... */
2216 false, /* interactive */
2221 if (!NT_STATUS_IS_OK(result)) {
2227 if (NT_STATUS_IS_OK(result)) {
2228 struct dom_sid user_sid;
2229 TALLOC_CTX *base_ctx = NULL;
2230 struct netr_SamBaseInfo *base_info = NULL;
2231 struct netr_SamInfo3 *info3 = NULL;
2233 switch (validation_level) {
2235 base_ctx = validation->sam3;
2236 base_info = &validation->sam3->base;
2239 base_ctx = validation->sam6;
2240 base_info = &validation->sam6->base;
2243 result = NT_STATUS_INTERNAL_ERROR;
2247 sid_compose(&user_sid, base_info->domain_sid, base_info->rid);
2249 if (base_info->full_name.string == NULL) {
2250 struct netr_SamInfo3 *cached_info3;
2252 cached_info3 = netsamlogon_cache_get(mem_ctx,
2254 if (cached_info3 != NULL &&
2255 cached_info3->base.full_name.string != NULL)
2257 base_info->full_name.string = talloc_strdup(
2259 cached_info3->base.full_name.string);
2262 /* this might fail so we don't check the return code */
2263 wcache_query_user_fullname(domain,
2266 &base_info->full_name.string);
2270 result = map_validation_to_info3(talloc_tos(),
2274 if (!NT_STATUS_IS_OK(result)) {
2277 wcache_invalidate_samlogon(find_domain_from_name(name_domain),
2279 netsamlogon_cache_store(name_user, info3);
2285 /* give us a more useful (more correct?) error code */
2286 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
2287 (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
2288 result = NT_STATUS_NO_LOGON_SERVERS;
2291 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2292 ("NTLM CRAP authentication for user [%s]\\[%s] returned %s\n",
2295 nt_errstr(result)));
2297 if (!NT_STATUS_IS_OK(result)) {
2301 *_validation_level = validation_level;
2302 *_validation = validation;
2303 return NT_STATUS_OK;
2306 enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
2307 struct winbindd_cli_state *state)
2310 const char *name_user = NULL;
2311 const char *name_domain = NULL;
2312 const char *workstation;
2313 uint8_t authoritative = 0;
2315 uint16_t validation_level;
2316 union netr_Validation *validation = NULL;
2317 DATA_BLOB lm_resp, nt_resp;
2319 /* This is child-only, so no check for privileged access is needed
2322 /* Ensure null termination */
2323 state->request->data.auth_crap.user[sizeof(state->request->data.auth_crap.user)-1]=0;
2324 state->request->data.auth_crap.domain[sizeof(state->request->data.auth_crap.domain)-1]=0;
2326 name_user = state->request->data.auth_crap.user;
2327 name_domain = state->request->data.auth_crap.domain;
2328 workstation = state->request->data.auth_crap.workstation;
2330 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state->pid,
2331 name_domain, name_user));
2333 if (state->request->data.auth_crap.lm_resp_len > sizeof(state->request->data.auth_crap.lm_resp)
2334 || state->request->data.auth_crap.nt_resp_len > sizeof(state->request->data.auth_crap.nt_resp)) {
2335 if (!(state->request->flags & WBFLAG_BIG_NTLMV2_BLOB) ||
2336 state->request->extra_len != state->request->data.auth_crap.nt_resp_len) {
2337 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n",
2338 state->request->data.auth_crap.lm_resp_len,
2339 state->request->data.auth_crap.nt_resp_len));
2340 result = NT_STATUS_INVALID_PARAMETER;
2345 lm_resp = data_blob_talloc(state->mem_ctx, state->request->data.auth_crap.lm_resp,
2346 state->request->data.auth_crap.lm_resp_len);
2348 if (state->request->flags & WBFLAG_BIG_NTLMV2_BLOB) {
2349 nt_resp = data_blob_talloc(state->mem_ctx,
2350 state->request->extra_data.data,
2351 state->request->data.auth_crap.nt_resp_len);
2353 nt_resp = data_blob_talloc(state->mem_ctx,
2354 state->request->data.auth_crap.nt_resp,
2355 state->request->data.auth_crap.nt_resp_len);
2358 result = winbind_dual_SamLogon(domain,
2360 state->request->data.auth_crap.logon_parameters,
2363 /* Bug #3248 - found by Stefan Burkei. */
2364 workstation, /* We carefully set this above so use it... */
2365 state->request->data.auth_crap.chal,
2373 if (!NT_STATUS_IS_OK(result)) {
2374 state->response->data.auth.authoritative = authoritative;
2378 if (NT_STATUS_IS_OK(result)) {
2379 struct netr_SamInfo3 *info3 = NULL;
2381 result = map_validation_to_info3(state->mem_ctx,
2385 if (!NT_STATUS_IS_OK(result)) {
2389 /* Check if the user is in the right group */
2390 result = check_info3_in_group(
2392 state->request->data.auth_crap.require_membership_of_sid);
2393 if (!NT_STATUS_IS_OK(result)) {
2394 DEBUG(3, ("User %s is not in the required group (%s), so "
2395 "crap authentication is rejected\n",
2396 state->request->data.auth_crap.user,
2397 state->request->data.auth_crap.require_membership_of_sid));
2401 result = append_auth_data(state->mem_ctx, state->response,
2402 state->request->flags,
2405 name_domain, name_user);
2406 if (!NT_STATUS_IS_OK(result)) {
2413 if (state->request->flags & WBFLAG_PAM_NT_STATUS_SQUASH) {
2414 result = nt_status_squash(result);
2417 set_auth_errors(state->response, result);
2419 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2422 enum winbindd_result winbindd_dual_pam_chauthtok(struct winbindd_domain *contact_domain,
2423 struct winbindd_cli_state *state)
2426 char *newpass = NULL;
2427 struct policy_handle dom_pol;
2428 struct rpc_pipe_client *cli = NULL;
2429 bool got_info = false;
2430 struct samr_DomInfo1 *info = NULL;
2431 struct userPwdChangeFailureInformation *reject = NULL;
2432 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2433 fstring domain, user;
2434 struct dcerpc_binding_handle *b = NULL;
2436 ZERO_STRUCT(dom_pol);
2438 DEBUG(3, ("[%5lu]: dual pam chauthtok %s\n", (unsigned long)state->pid,
2439 state->request->data.auth.user));
2441 if (!parse_domain_user(state->request->data.chauthtok.user, domain, user)) {
2445 /* Change password */
2447 oldpass = state->request->data.chauthtok.oldpass;
2448 newpass = state->request->data.chauthtok.newpass;
2450 /* Initialize reject reason */
2451 state->response->data.auth.reject_reason = Undefined;
2453 /* Get sam handle */
2455 result = cm_connect_sam(contact_domain, state->mem_ctx, true, &cli,
2457 if (!NT_STATUS_IS_OK(result)) {
2458 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
2462 b = cli->binding_handle;
2464 result = rpccli_samr_chgpasswd_user3(cli, state->mem_ctx,
2471 /* Windows 2003 returns NT_STATUS_PASSWORD_RESTRICTION */
2473 if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION) ) {
2475 fill_in_password_policy(state->response, info);
2477 state->response->data.auth.reject_reason =
2478 reject->extendedFailureReason;
2483 /* atm the pidl generated rpccli_samr_ChangePasswordUser3 function will
2484 * return with NT_STATUS_BUFFER_TOO_SMALL for w2k dcs as w2k just
2485 * returns with 4byte error code (NT_STATUS_NOT_SUPPORTED) which is too
2486 * short to comply with the samr_ChangePasswordUser3 idl - gd */
2488 /* only fallback when the chgpasswd_user3 call is not supported */
2489 if (NT_STATUS_EQUAL(result, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE) ||
2490 NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED) ||
2491 NT_STATUS_EQUAL(result, NT_STATUS_BUFFER_TOO_SMALL) ||
2492 NT_STATUS_EQUAL(result, NT_STATUS_NOT_IMPLEMENTED)) {
2494 DEBUG(10,("Password change with chgpasswd_user3 failed with: %s, retrying chgpasswd_user2\n",
2495 nt_errstr(result)));
2497 result = rpccli_samr_chgpasswd_user2(cli, state->mem_ctx, user, newpass, oldpass);
2499 /* Windows 2000 returns NT_STATUS_ACCOUNT_RESTRICTION.
2500 Map to the same status code as Windows 2003. */
2502 if ( NT_STATUS_EQUAL(NT_STATUS_ACCOUNT_RESTRICTION, result ) ) {
2503 result = NT_STATUS_PASSWORD_RESTRICTION;
2509 if (NT_STATUS_IS_OK(result)
2510 && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN)
2511 && lp_winbind_offline_logon()) {
2512 result = winbindd_update_creds_by_name(contact_domain, user,
2514 /* Again, this happens when we login from gdm or xdm
2515 * and the password expires, *BUT* cached crendentials
2516 * doesn't exist. winbindd_update_creds_by_name()
2517 * returns NT_STATUS_NO_SUCH_USER.
2518 * This is not a failure.
2521 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER)) {
2522 result = NT_STATUS_OK;
2525 if (!NT_STATUS_IS_OK(result)) {
2526 DEBUG(10, ("Failed to store creds: %s\n",
2527 nt_errstr(result)));
2528 goto process_result;
2532 if (!NT_STATUS_IS_OK(result) && !got_info && contact_domain) {
2534 NTSTATUS policy_ret;
2536 policy_ret = fillup_password_policy(
2537 contact_domain, state->response);
2539 /* failure of this is non critical, it will just provide no
2540 * additional information to the client why the change has
2541 * failed - Guenther */
2543 if (!NT_STATUS_IS_OK(policy_ret)) {
2544 DEBUG(10,("Failed to get password policies: %s\n", nt_errstr(policy_ret)));
2545 goto process_result;
2551 if (strequal(contact_domain->name, get_global_sam_name())) {
2552 /* FIXME: internal rpc pipe does not cache handles yet */
2554 if (is_valid_policy_hnd(&dom_pol)) {
2556 dcerpc_samr_Close(b, state->mem_ctx, &dom_pol, &_result);
2562 set_auth_errors(state->response, result);
2564 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2565 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
2568 state->response->data.auth.nt_status_string,
2569 state->response->data.auth.pam_error));
2571 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2574 enum winbindd_result winbindd_dual_pam_logoff(struct winbindd_domain *domain,
2575 struct winbindd_cli_state *state)
2577 NTSTATUS result = NT_STATUS_NOT_SUPPORTED;
2579 DEBUG(3, ("[%5lu]: pam dual logoff %s\n", (unsigned long)state->pid,
2580 state->request->data.logoff.user));
2582 if (!(state->request->flags & WBFLAG_PAM_KRB5)) {
2583 result = NT_STATUS_OK;
2584 goto process_result;
2587 if (state->request->data.logoff.krb5ccname[0] == '\0') {
2588 result = NT_STATUS_OK;
2589 goto process_result;
2594 if (state->request->data.logoff.uid == (uid_t)-1) {
2595 DEBUG(0,("winbindd_pam_logoff: invalid uid\n"));
2596 goto process_result;
2599 /* what we need here is to find the corresponding krb5 ccache name *we*
2600 * created for a given username and destroy it */
2602 if (!ccache_entry_exists(state->request->data.logoff.user)) {
2603 result = NT_STATUS_OK;
2604 DEBUG(10,("winbindd_pam_logoff: no entry found.\n"));
2605 goto process_result;
2608 if (!ccache_entry_identical(state->request->data.logoff.user,
2609 state->request->data.logoff.uid,
2610 state->request->data.logoff.krb5ccname)) {
2611 DEBUG(0,("winbindd_pam_logoff: cached entry differs.\n"));
2612 goto process_result;
2615 result = remove_ccache(state->request->data.logoff.user);
2616 if (!NT_STATUS_IS_OK(result)) {
2617 DEBUG(0,("winbindd_pam_logoff: failed to remove ccache: %s\n",
2618 nt_errstr(result)));
2619 goto process_result;
2623 * Remove any mlock'ed memory creds in the child
2624 * we might be using for krb5 ticket renewal.
2627 winbindd_delete_memory_creds(state->request->data.logoff.user);
2630 result = NT_STATUS_NOT_SUPPORTED;
2636 set_auth_errors(state->response, result);
2638 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2641 /* Change user password with auth crap*/
2643 enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domain *domainSt, struct winbindd_cli_state *state)
2646 DATA_BLOB new_nt_password;
2647 DATA_BLOB old_nt_hash_enc;
2648 DATA_BLOB new_lm_password;
2649 DATA_BLOB old_lm_hash_enc;
2650 fstring domain,user;
2651 struct policy_handle dom_pol;
2652 struct winbindd_domain *contact_domain = domainSt;
2653 struct rpc_pipe_client *cli = NULL;
2654 struct dcerpc_binding_handle *b = NULL;
2656 ZERO_STRUCT(dom_pol);
2658 /* Ensure null termination */
2659 state->request->data.chng_pswd_auth_crap.user[
2660 sizeof(state->request->data.chng_pswd_auth_crap.user)-1]=0;
2661 state->request->data.chng_pswd_auth_crap.domain[
2662 sizeof(state->request->data.chng_pswd_auth_crap.domain)-1]=0;
2666 DEBUG(3, ("[%5lu]: pam change pswd auth crap domain: %s user: %s\n",
2667 (unsigned long)state->pid,
2668 state->request->data.chng_pswd_auth_crap.domain,
2669 state->request->data.chng_pswd_auth_crap.user));
2671 if (lp_winbind_offline_logon()) {
2672 DEBUG(0,("Refusing password change as winbind offline logons are enabled. "));
2673 DEBUGADD(0,("Changing passwords here would risk inconsistent logons\n"));
2674 result = NT_STATUS_ACCESS_DENIED;
2678 if (*state->request->data.chng_pswd_auth_crap.domain) {
2679 fstrcpy(domain,state->request->data.chng_pswd_auth_crap.domain);
2681 parse_domain_user(state->request->data.chng_pswd_auth_crap.user,
2685 DEBUG(3,("no domain specified with username (%s) - "
2687 state->request->data.chng_pswd_auth_crap.user));
2688 result = NT_STATUS_NO_SUCH_USER;
2693 if (!*domain && lp_winbind_use_default_domain()) {
2694 fstrcpy(domain,lp_workgroup());
2698 fstrcpy(user, state->request->data.chng_pswd_auth_crap.user);
2701 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n",
2702 (unsigned long)state->pid, domain, user));
2704 /* Change password */
2705 new_nt_password = data_blob_const(
2706 state->request->data.chng_pswd_auth_crap.new_nt_pswd,
2707 state->request->data.chng_pswd_auth_crap.new_nt_pswd_len);
2709 old_nt_hash_enc = data_blob_const(
2710 state->request->data.chng_pswd_auth_crap.old_nt_hash_enc,
2711 state->request->data.chng_pswd_auth_crap.old_nt_hash_enc_len);
2713 if(state->request->data.chng_pswd_auth_crap.new_lm_pswd_len > 0) {
2714 new_lm_password = data_blob_const(
2715 state->request->data.chng_pswd_auth_crap.new_lm_pswd,
2716 state->request->data.chng_pswd_auth_crap.new_lm_pswd_len);
2718 old_lm_hash_enc = data_blob_const(
2719 state->request->data.chng_pswd_auth_crap.old_lm_hash_enc,
2720 state->request->data.chng_pswd_auth_crap.old_lm_hash_enc_len);
2722 new_lm_password = data_blob_null;
2723 old_lm_hash_enc = data_blob_null;
2726 /* Get sam handle */
2728 result = cm_connect_sam(contact_domain, state->mem_ctx, true, &cli, &dom_pol);
2729 if (!NT_STATUS_IS_OK(result)) {
2730 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
2734 b = cli->binding_handle;
2736 result = rpccli_samr_chng_pswd_auth_crap(
2737 cli, state->mem_ctx, user, new_nt_password, old_nt_hash_enc,
2738 new_lm_password, old_lm_hash_enc);
2742 if (strequal(contact_domain->name, get_global_sam_name())) {
2743 /* FIXME: internal rpc pipe does not cache handles yet */
2745 if (is_valid_policy_hnd(&dom_pol)) {
2747 dcerpc_samr_Close(b, state->mem_ctx, &dom_pol, &_result);
2753 set_auth_errors(state->response, result);
2755 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2756 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
2758 state->response->data.auth.nt_status_string,
2759 state->response->data.auth.pam_error));
2761 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2765 static NTSTATUS extract_pac_vrfy_sigs(TALLOC_CTX *mem_ctx, DATA_BLOB pac_blob,
2766 struct PAC_LOGON_INFO **logon_info)
2768 krb5_context krbctx = NULL;
2769 krb5_error_code k5ret;
2771 krb5_kt_cursor cursor;
2772 krb5_keytab_entry entry;
2773 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2776 ZERO_STRUCT(cursor);
2778 k5ret = krb5_init_context(&krbctx);
2780 DEBUG(1, ("Failed to initialize kerberos context: %s\n",
2781 error_message(k5ret)));
2782 status = krb5_to_nt_status(k5ret);
2786 k5ret = gse_krb5_get_server_keytab(krbctx, &keytab);
2788 DEBUG(1, ("Failed to get keytab: %s\n",
2789 error_message(k5ret)));
2790 status = krb5_to_nt_status(k5ret);
2794 k5ret = krb5_kt_start_seq_get(krbctx, keytab, &cursor);
2796 DEBUG(1, ("Failed to start seq: %s\n",
2797 error_message(k5ret)));
2798 status = krb5_to_nt_status(k5ret);
2802 k5ret = krb5_kt_next_entry(krbctx, keytab, &entry, &cursor);
2803 while (k5ret == 0) {
2804 status = kerberos_pac_logon_info(mem_ctx, pac_blob,
2806 KRB5_KT_KEY(&entry), NULL, 0,
2808 if (NT_STATUS_IS_OK(status)) {
2811 k5ret = smb_krb5_kt_free_entry(krbctx, &entry);
2812 k5ret = krb5_kt_next_entry(krbctx, keytab, &entry, &cursor);
2815 k5ret = krb5_kt_end_seq_get(krbctx, keytab, &cursor);
2817 DEBUG(1, ("Failed to end seq: %s\n",
2818 error_message(k5ret)));
2821 k5ret = krb5_kt_close(krbctx, keytab);
2823 DEBUG(1, ("Failed to close keytab: %s\n",
2824 error_message(k5ret)));
2827 krb5_free_context(krbctx);
2832 NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
2833 struct netr_SamInfo3 **info3)
2835 struct winbindd_request *req = state->request;
2837 struct PAC_LOGON_INFO *logon_info = NULL;
2838 struct netr_SamInfo3 *info3_copy = NULL;
2841 pac_blob = data_blob_const(req->extra_data.data, req->extra_len);
2842 result = extract_pac_vrfy_sigs(state->mem_ctx, pac_blob, &logon_info);
2843 if (!NT_STATUS_IS_OK(result) &&
2844 !NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
2845 DEBUG(1, ("Error during PAC signature verification: %s\n",
2846 nt_errstr(result)));
2852 * Signature verification succeeded, we can
2853 * trust the PAC and prime the netsamlogon
2854 * and name2sid caches. DO NOT DO THIS
2855 * in the signature verification failed
2858 struct winbindd_domain *domain = NULL;
2860 result = create_info3_from_pac_logon_info(state->mem_ctx,
2863 if (!NT_STATUS_IS_OK(result)) {
2866 netsamlogon_cache_store(NULL, info3_copy);
2869 * We're in the parent here, so find the child
2870 * pointer from the PAC domain name.
2872 domain = find_lookup_domain_from_name(
2873 info3_copy->base.logon_domain.string);
2874 if (domain && domain->primary ) {
2875 struct dom_sid user_sid;
2877 sid_compose(&user_sid,
2878 info3_copy->base.domain_sid,
2879 info3_copy->base.rid);
2881 cache_name2sid_trusted(domain,
2882 info3_copy->base.logon_domain.string,
2883 info3_copy->base.account_name.string,
2887 DBG_INFO("PAC for user %s\\%s SID %s primed cache\n",
2888 info3_copy->base.logon_domain.string,
2889 info3_copy->base.account_name.string,
2890 sid_string_dbg(&user_sid));
2894 /* Try without signature verification */
2895 result = kerberos_pac_logon_info(state->mem_ctx, pac_blob, NULL,
2896 NULL, NULL, NULL, 0,
2898 if (!NT_STATUS_IS_OK(result)) {
2899 DEBUG(10, ("Could not extract PAC: %s\n",
2900 nt_errstr(result)));
2905 * Don't strictly need to copy here,
2906 * but it makes it explicit we're
2907 * returning a copy talloc'ed off
2908 * the state->mem_ctx.
2910 info3_copy = copy_netr_SamInfo3(state->mem_ctx,
2911 &logon_info->info3);
2912 if (info3_copy == NULL) {
2913 return NT_STATUS_NO_MEMORY;
2918 *info3 = info3_copy;
2920 return NT_STATUS_OK;
2922 #else /* HAVE_KRB5 */
2923 NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
2924 struct netr_SamInfo3 **info3)
2926 return NT_STATUS_NO_SUCH_USER;
2928 #endif /* HAVE_KRB5 */