2 Unix SMB/CIFS implementation.
4 Winbind daemon - pam auth funcions
6 Copyright (C) Andrew Tridgell 2000
7 Copyright (C) Tim Potter 2001
8 Copyright (C) Andrew Bartlett 2001-2002
9 Copyright (C) Guenther Deschner 2005
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "../libcli/auth/libcli_auth.h"
28 #include "../librpc/gen_ndr/ndr_samr_c.h"
29 #include "rpc_client/cli_pipe.h"
30 #include "rpc_client/cli_samr.h"
31 #include "../librpc/gen_ndr/ndr_netlogon.h"
32 #include "rpc_client/cli_netlogon.h"
34 #include "../lib/crypto/arcfour.h"
35 #include "../libcli/security/security.h"
37 #include "../librpc/gen_ndr/krb5pac.h"
38 #include "passdb/machine_sid.h"
40 #include "../lib/tsocket/tsocket.h"
41 #include "auth/kerberos/pac_utils.h"
42 #include "auth/gensec/gensec.h"
43 #include "librpc/crypto/gse_krb5.h"
44 #include "lib/afs/afs_funcs.h"
45 #include "libsmb/samlogon_cache.h"
46 #include "rpc_client/util_netlogon.h"
49 #define DBGC_CLASS DBGC_WINBIND
51 #define LOGON_KRB5_FAIL_CLOCK_SKEW 0x02000000
53 static NTSTATUS append_info3_as_txt(TALLOC_CTX *mem_ctx,
54 struct winbindd_response *resp,
55 uint16_t validation_level,
56 union netr_Validation *validation)
58 struct netr_SamInfo3 *info3 = NULL;
61 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
62 TALLOC_CTX *frame = talloc_stackframe();
64 status = map_validation_to_info3(frame,
68 if (!NT_STATUS_IS_OK(status)) {
72 resp->data.auth.info3.logon_time =
73 nt_time_to_unix(info3->base.logon_time);
74 resp->data.auth.info3.logoff_time =
75 nt_time_to_unix(info3->base.logoff_time);
76 resp->data.auth.info3.kickoff_time =
77 nt_time_to_unix(info3->base.kickoff_time);
78 resp->data.auth.info3.pass_last_set_time =
79 nt_time_to_unix(info3->base.last_password_change);
80 resp->data.auth.info3.pass_can_change_time =
81 nt_time_to_unix(info3->base.allow_password_change);
82 resp->data.auth.info3.pass_must_change_time =
83 nt_time_to_unix(info3->base.force_password_change);
85 resp->data.auth.info3.logon_count = info3->base.logon_count;
86 resp->data.auth.info3.bad_pw_count = info3->base.bad_password_count;
88 resp->data.auth.info3.user_rid = info3->base.rid;
89 resp->data.auth.info3.group_rid = info3->base.primary_gid;
90 sid_to_fstring(resp->data.auth.info3.dom_sid, info3->base.domain_sid);
92 resp->data.auth.info3.num_groups = info3->base.groups.count;
93 resp->data.auth.info3.user_flgs = info3->base.user_flags;
95 resp->data.auth.info3.acct_flags = info3->base.acct_flags;
96 resp->data.auth.info3.num_other_sids = info3->sidcount;
98 fstrcpy(resp->data.auth.info3.user_name,
99 info3->base.account_name.string);
100 fstrcpy(resp->data.auth.info3.full_name,
101 info3->base.full_name.string);
102 fstrcpy(resp->data.auth.info3.logon_script,
103 info3->base.logon_script.string);
104 fstrcpy(resp->data.auth.info3.profile_path,
105 info3->base.profile_path.string);
106 fstrcpy(resp->data.auth.info3.home_dir,
107 info3->base.home_directory.string);
108 fstrcpy(resp->data.auth.info3.dir_drive,
109 info3->base.home_drive.string);
111 fstrcpy(resp->data.auth.info3.logon_srv,
112 info3->base.logon_server.string);
113 fstrcpy(resp->data.auth.info3.logon_dom,
114 info3->base.logon_domain.string);
116 resp->data.auth.validation_level = validation_level;
117 if (validation_level == 6) {
118 fstrcpy(resp->data.auth.info6.dns_domainname,
119 validation->sam6->dns_domainname.string);
120 fstrcpy(resp->data.auth.info6.principal_name,
121 validation->sam6->principal_name.string);
124 ex = talloc_strdup(frame, "");
126 status = NT_STATUS_NO_MEMORY;
130 for (i=0; i < info3->base.groups.count; i++) {
131 ex = talloc_asprintf_append_buffer(ex, "0x%08X:0x%08X\n",
132 info3->base.groups.rids[i].rid,
133 info3->base.groups.rids[i].attributes);
135 status = NT_STATUS_NO_MEMORY;
140 for (i=0; i < info3->sidcount; i++) {
143 sid = dom_sid_string(frame, info3->sids[i].sid);
145 status = NT_STATUS_NO_MEMORY;
149 ex = talloc_asprintf_append_buffer(ex, "%s:0x%08X\n",
151 info3->sids[i].attributes);
153 status = NT_STATUS_NO_MEMORY;
158 resp->length += talloc_get_size(ex);
159 resp->extra_data.data = talloc_move(mem_ctx, &ex);
161 status = NT_STATUS_OK;
167 static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx,
168 struct winbindd_response *resp,
169 struct netr_SamInfo3 *info3)
172 enum ndr_err_code ndr_err;
174 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, info3,
175 (ndr_push_flags_fn_t)ndr_push_netr_SamInfo3);
176 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
177 DEBUG(0,("append_info3_as_ndr: failed to append\n"));
178 return ndr_map_error2ntstatus(ndr_err);
181 resp->extra_data.data = blob.data;
182 resp->length += blob.length;
187 static NTSTATUS append_unix_username(TALLOC_CTX *mem_ctx,
188 struct winbindd_response *resp,
189 const struct netr_SamInfo3 *info3,
190 const char *name_domain,
191 const char *name_user)
193 /* We've been asked to return the unix username, per
194 'winbind use default domain' settings and the like */
196 const char *nt_username, *nt_domain;
198 nt_domain = talloc_strdup(mem_ctx, info3->base.logon_domain.string);
200 /* If the server didn't give us one, just use the one
202 nt_domain = name_domain;
205 nt_username = talloc_strdup(mem_ctx, info3->base.account_name.string);
207 /* If the server didn't give us one, just use the one
209 nt_username = name_user;
212 fill_domain_username(resp->data.auth.unix_username,
213 nt_domain, nt_username, true);
215 DEBUG(5, ("Setting unix username to [%s]\n",
216 resp->data.auth.unix_username));
221 static NTSTATUS append_afs_token(TALLOC_CTX *mem_ctx,
222 struct winbindd_response *resp,
223 const struct netr_SamInfo3 *info3,
224 const char *name_domain,
225 const char *name_user)
227 char *afsname = NULL;
231 afsname = talloc_strdup(mem_ctx, lp_afs_username_map());
232 if (afsname == NULL) {
233 return NT_STATUS_NO_MEMORY;
236 afsname = talloc_string_sub(mem_ctx,
237 lp_afs_username_map(),
239 afsname = talloc_string_sub(mem_ctx, afsname,
241 afsname = talloc_string_sub(mem_ctx, afsname,
245 struct dom_sid user_sid;
248 sid_compose(&user_sid, info3->base.domain_sid,
250 sid_to_fstring(sidstr, &user_sid);
251 afsname = talloc_string_sub(mem_ctx, afsname,
255 if (afsname == NULL) {
256 return NT_STATUS_NO_MEMORY;
259 if (!strlower_m(afsname)) {
260 return NT_STATUS_INVALID_PARAMETER;
263 DEBUG(10, ("Generating token for user %s\n", afsname));
265 cell = strchr(afsname, '@');
268 return NT_STATUS_NO_MEMORY;
274 token = afs_createtoken_str(afsname, cell);
278 resp->extra_data.data = talloc_strdup(mem_ctx, token);
279 if (resp->extra_data.data == NULL) {
280 return NT_STATUS_NO_MEMORY;
282 resp->length += strlen((const char *)resp->extra_data.data)+1;
287 static NTSTATUS check_info3_in_group(struct netr_SamInfo3 *info3,
288 const char *group_sid)
290 * Check whether a user belongs to a group or list of groups.
292 * @param mem_ctx talloc memory context.
293 * @param info3 user information, including group membership info.
294 * @param group_sid One or more groups , separated by commas.
296 * @return NT_STATUS_OK on success,
297 * NT_STATUS_LOGON_FAILURE if the user does not belong,
298 * or other NT_STATUS_IS_ERR(status) for other kinds of failure.
301 struct dom_sid *require_membership_of_sid;
302 uint32_t num_require_membership_of_sid;
307 struct security_token *token;
308 TALLOC_CTX *frame = talloc_stackframe();
311 /* Parse the 'required group' SID */
313 if (!group_sid || !group_sid[0]) {
314 /* NO sid supplied, all users may access */
319 token = talloc_zero(talloc_tos(), struct security_token);
321 DEBUG(0, ("talloc failed\n"));
323 return NT_STATUS_NO_MEMORY;
326 num_require_membership_of_sid = 0;
327 require_membership_of_sid = NULL;
331 while (next_token_talloc(talloc_tos(), &p, &req_sid, ",")) {
332 if (!string_to_sid(&sid, req_sid)) {
333 DEBUG(0, ("check_info3_in_group: could not parse %s "
334 "as a SID!", req_sid));
336 return NT_STATUS_INVALID_PARAMETER;
339 status = add_sid_to_array(talloc_tos(), &sid,
340 &require_membership_of_sid,
341 &num_require_membership_of_sid);
342 if (!NT_STATUS_IS_OK(status)) {
343 DEBUG(0, ("add_sid_to_array failed\n"));
349 status = sid_array_from_info3(talloc_tos(), info3,
353 if (!NT_STATUS_IS_OK(status)) {
358 if (!NT_STATUS_IS_OK(status = add_aliases(get_global_sam_sid(),
360 || !NT_STATUS_IS_OK(status = add_aliases(&global_sid_Builtin,
362 DEBUG(3, ("could not add aliases: %s\n",
368 security_token_debug(DBGC_CLASS, 10, token);
370 for (i=0; i<num_require_membership_of_sid; i++) {
371 DEBUG(10, ("Checking SID %s\n", sid_string_dbg(
372 &require_membership_of_sid[i])));
373 if (nt_token_check_sid(&require_membership_of_sid[i],
375 DEBUG(10, ("Access ok\n"));
381 /* Do not distinguish this error from a wrong username/pw */
384 return NT_STATUS_LOGON_FAILURE;
387 struct winbindd_domain *find_auth_domain(uint8_t flags,
388 const char *domain_name)
390 struct winbindd_domain *domain;
393 domain = find_domain_from_name_noinit(domain_name);
394 if (domain == NULL) {
395 DEBUG(3, ("Authentication for domain [%s] refused "
396 "as it is not a trusted domain\n",
401 if (domain->secure_channel_type != SEC_CHAN_NULL) {
405 return domain->routing_domain;
408 if (strequal(domain_name, get_global_sam_name())) {
409 return find_domain_from_name_noinit(domain_name);
412 /* we can auth against trusted domains */
413 if (flags & WBFLAG_PAM_CONTACT_TRUSTDOM) {
414 domain = find_domain_from_name_noinit(domain_name);
415 if (domain == NULL) {
416 DEBUG(3, ("Authentication for domain [%s] skipped "
417 "as it is not a trusted domain\n",
424 return find_our_domain();
427 static void fake_password_policy(struct winbindd_response *r,
428 const struct netr_SamBaseInfo *bi)
430 NTTIME min_password_age;
431 NTTIME max_password_age;
433 if (bi->allow_password_change > bi->last_password_change) {
434 min_password_age = bi->allow_password_change -
435 bi->last_password_change;
437 min_password_age = 0;
440 if (bi->force_password_change > bi->last_password_change) {
441 max_password_age = bi->force_password_change -
442 bi->last_password_change;
444 max_password_age = 0;
447 r->data.auth.policy.min_length_password = 0;
448 r->data.auth.policy.password_history = 0;
449 r->data.auth.policy.password_properties = 0;
450 r->data.auth.policy.expire =
451 nt_time_to_unix_abs(&max_password_age);
452 r->data.auth.policy.min_passwordage =
453 nt_time_to_unix_abs(&min_password_age);
456 static void fill_in_password_policy(struct winbindd_response *r,
457 const struct samr_DomInfo1 *p)
459 r->data.auth.policy.min_length_password =
460 p->min_password_length;
461 r->data.auth.policy.password_history =
462 p->password_history_length;
463 r->data.auth.policy.password_properties =
464 p->password_properties;
465 r->data.auth.policy.expire =
466 nt_time_to_unix_abs((const NTTIME *)&(p->max_password_age));
467 r->data.auth.policy.min_passwordage =
468 nt_time_to_unix_abs((const NTTIME *)&(p->min_password_age));
471 static NTSTATUS fillup_password_policy(struct winbindd_domain *domain,
472 struct winbindd_response *response)
474 TALLOC_CTX *frame = talloc_stackframe();
476 struct samr_DomInfo1 password_policy;
478 if ( !winbindd_can_contact_domain( domain ) ) {
479 DEBUG(5,("fillup_password_policy: No inbound trust to "
480 "contact domain %s\n", domain->name));
481 status = NT_STATUS_NOT_SUPPORTED;
485 status = wb_cache_password_policy(domain, talloc_tos(),
487 if (NT_STATUS_IS_ERR(status)) {
491 fill_in_password_policy(response, &password_policy);
498 static NTSTATUS get_max_bad_attempts_from_lockout_policy(struct winbindd_domain *domain,
500 uint16_t *lockout_threshold)
502 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
503 struct samr_DomInfo12 lockout_policy;
505 *lockout_threshold = 0;
507 status = wb_cache_lockout_policy(domain, mem_ctx, &lockout_policy);
508 if (NT_STATUS_IS_ERR(status)) {
512 *lockout_threshold = lockout_policy.lockout_threshold;
517 static NTSTATUS get_pwd_properties(struct winbindd_domain *domain,
519 uint32_t *password_properties)
521 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
522 struct samr_DomInfo1 password_policy;
524 *password_properties = 0;
526 status = wb_cache_password_policy(domain, mem_ctx, &password_policy);
527 if (NT_STATUS_IS_ERR(status)) {
531 *password_properties = password_policy.password_properties;
538 static const char *generate_krb5_ccache(TALLOC_CTX *mem_ctx,
541 const char **user_ccache_file)
543 /* accept FILE and WRFILE as krb5_cc_type from the client and then
544 * build the full ccname string based on the user's uid here -
547 const char *gen_cc = NULL;
550 if (strequal(type, "FILE")) {
551 gen_cc = talloc_asprintf(
552 mem_ctx, "FILE:/tmp/krb5cc_%d", uid);
554 if (strequal(type, "WRFILE")) {
555 gen_cc = talloc_asprintf(
556 mem_ctx, "WRFILE:/tmp/krb5cc_%d", uid);
558 if (strequal(type, "KEYRING")) {
559 gen_cc = talloc_asprintf(
560 mem_ctx, "KEYRING:persistent:%d", uid);
563 if (strnequal(type, "FILE:/", 6) ||
564 strnequal(type, "WRFILE:/", 8) ||
565 strnequal(type, "DIR:/", 5)) {
567 /* we allow only one "%u" substitution */
571 p = strchr(type, '%');
576 if (p != NULL && *p == 'u' && strchr(p, '%') == NULL) {
577 char uid_str[sizeof("18446744073709551615")];
579 snprintf(uid_str, sizeof(uid_str), "%u", uid);
581 gen_cc = talloc_string_sub2(mem_ctx,
585 /* remove_unsafe_characters */
589 /* allow_trailing_dollar */
596 *user_ccache_file = gen_cc;
598 if (gen_cc == NULL) {
599 gen_cc = talloc_strdup(mem_ctx, "MEMORY:winbindd_pam_ccache");
601 if (gen_cc == NULL) {
602 DEBUG(0,("out of memory\n"));
606 DEBUG(10, ("using ccache: %s%s\n", gen_cc,
607 (*user_ccache_file == NULL) ? " (internal)":""));
614 uid_t get_uid_from_request(struct winbindd_request *request)
618 uid = request->data.auth.uid;
620 if (uid == (uid_t)-1) {
621 DEBUG(1,("invalid uid: '%u'\n", (unsigned int)uid));
627 /**********************************************************************
628 Authenticate a user with a clear text password using Kerberos and fill up
630 **********************************************************************/
632 static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
633 struct winbindd_domain *domain,
636 const char *krb5_cc_type,
638 struct netr_SamInfo3 **info3,
642 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
643 krb5_error_code krb5_ret;
644 const char *cc = NULL;
645 const char *principal_s = NULL;
646 const char *service = NULL;
648 fstring name_domain, name_user;
649 time_t ticket_lifetime = 0;
650 time_t renewal_until = 0;
652 time_t time_offset = 0;
653 const char *user_ccache_file;
654 struct PAC_LOGON_INFO *logon_info = NULL;
655 struct PAC_DATA *pac_data = NULL;
656 struct PAC_DATA_CTR *pac_data_ctr = NULL;
657 const char *local_service;
659 struct netr_SamInfo3 *info3_copy = NULL;
663 if (domain->alt_name == NULL) {
664 return NT_STATUS_INVALID_PARAMETER;
668 * prepare a krb5_cc_cache string for the user */
671 DEBUG(0,("no valid uid\n"));
674 cc = generate_krb5_ccache(mem_ctx,
679 return NT_STATUS_NO_MEMORY;
684 * get kerberos properties */
686 if (domain->private_data) {
687 ads = (ADS_STRUCT *)domain->private_data;
688 time_offset = ads->auth.time_offset;
693 * do kerberos auth and setup ccache as the user */
695 parse_domain_user(user, name_domain, name_user);
697 realm = talloc_strdup(mem_ctx, domain->alt_name);
699 return NT_STATUS_NO_MEMORY;
702 if (!strupper_m(realm)) {
703 return NT_STATUS_INVALID_PARAMETER;
706 principal_s = talloc_asprintf(mem_ctx, "%s@%s", name_user, realm);
707 if (principal_s == NULL) {
708 return NT_STATUS_NO_MEMORY;
711 service = talloc_asprintf(mem_ctx, "%s/%s@%s", KRB5_TGS_NAME, realm, realm);
712 if (service == NULL) {
713 return NT_STATUS_NO_MEMORY;
716 local_service = talloc_asprintf(mem_ctx, "%s$@%s",
717 lp_netbios_name(), lp_realm());
718 if (local_service == NULL) {
719 return NT_STATUS_NO_MEMORY;
723 /* if this is a user ccache, we need to act as the user to let the krb5
724 * library handle the chown, etc. */
726 /************************ ENTERING NON-ROOT **********************/
728 if (user_ccache_file != NULL) {
729 set_effective_uid(uid);
730 DEBUG(10,("winbindd_raw_kerberos_login: uid is %d\n", uid));
733 result = kerberos_return_pac(mem_ctx,
742 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
746 if (user_ccache_file != NULL) {
747 gain_root_privilege();
750 /************************ RETURNED TO ROOT **********************/
752 if (!NT_STATUS_IS_OK(result)) {
756 if (pac_data_ctr == NULL) {
760 pac_data = pac_data_ctr->pac_data;
761 if (pac_data == NULL) {
765 for (i=0; i < pac_data->num_buffers; i++) {
767 if (pac_data->buffers[i].type != PAC_TYPE_LOGON_INFO) {
771 logon_info = pac_data->buffers[i].info->logon_info.info;
773 return NT_STATUS_INVALID_PARAMETER;
779 if (logon_info == NULL) {
780 DEBUG(10,("Missing logon_info in ticket of %s\n",
782 return NT_STATUS_INVALID_PARAMETER;
785 DEBUG(10,("winbindd_raw_kerberos_login: winbindd validated ticket of %s\n",
788 result = create_info3_from_pac_logon_info(mem_ctx, logon_info, &info3_copy);
789 if (!NT_STATUS_IS_OK(result)) {
793 /* if we had a user's ccache then return that string for the pam
796 if (user_ccache_file != NULL) {
798 fstrcpy(krb5ccname, user_ccache_file);
800 result = add_ccache_to_list(principal_s,
812 if (!NT_STATUS_IS_OK(result)) {
813 DEBUG(10,("winbindd_raw_kerberos_login: failed to add ccache to list: %s\n",
818 /* need to delete the memory cred cache, it is not used anymore */
820 krb5_ret = ads_kdestroy(cc);
822 DEBUG(3,("winbindd_raw_kerberos_login: "
823 "could not destroy krb5 credential cache: "
824 "%s\n", error_message(krb5_ret)));
833 * Do not delete an existing valid credential cache, if the user
834 * e.g. enters a wrong password
836 if ((strequal(krb5_cc_type, "FILE") || strequal(krb5_cc_type, "WRFILE"))
837 && user_ccache_file != NULL) {
841 /* we could have created a new credential cache with a valid tgt in it
842 * but we werent able to get or verify the service ticket for this
843 * local host and therefor didn't get the PAC, we need to remove that
844 * cache entirely now */
846 krb5_ret = ads_kdestroy(cc);
848 DEBUG(3,("winbindd_raw_kerberos_login: "
849 "could not destroy krb5 credential cache: "
850 "%s\n", error_message(krb5_ret)));
853 if (!NT_STATUS_IS_OK(remove_ccache(user))) {
854 DEBUG(3,("winbindd_raw_kerberos_login: "
855 "could not remove ccache for user %s\n",
861 return NT_STATUS_NOT_SUPPORTED;
862 #endif /* HAVE_KRB5 */
865 /****************************************************************
866 ****************************************************************/
868 bool check_request_flags(uint32_t flags)
870 uint32_t flags_edata = WBFLAG_PAM_AFS_TOKEN |
871 WBFLAG_PAM_INFO3_TEXT |
872 WBFLAG_PAM_INFO3_NDR;
874 if ( ( (flags & flags_edata) == WBFLAG_PAM_AFS_TOKEN) ||
875 ( (flags & flags_edata) == WBFLAG_PAM_INFO3_NDR) ||
876 ( (flags & flags_edata) == WBFLAG_PAM_INFO3_TEXT)||
877 !(flags & flags_edata) ) {
881 DEBUG(1, ("check_request_flags: invalid request flags[0x%08X]\n",
887 /****************************************************************
888 ****************************************************************/
890 NTSTATUS append_auth_data(TALLOC_CTX *mem_ctx,
891 struct winbindd_response *resp,
892 uint32_t request_flags,
893 uint16_t validation_level,
894 union netr_Validation *validation,
895 const char *name_domain,
896 const char *name_user)
898 struct netr_SamInfo3 *info3 = NULL;
899 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
901 result = map_validation_to_info3(talloc_tos(),
905 if (!NT_STATUS_IS_OK(result)) {
909 if (request_flags & WBFLAG_PAM_USER_SESSION_KEY) {
910 memcpy(resp->data.auth.user_session_key,
912 sizeof(resp->data.auth.user_session_key)
916 if (request_flags & WBFLAG_PAM_LMKEY) {
917 memcpy(resp->data.auth.first_8_lm_hash,
918 info3->base.LMSessKey.key,
919 sizeof(resp->data.auth.first_8_lm_hash)
923 if (request_flags & WBFLAG_PAM_UNIX_NAME) {
924 result = append_unix_username(mem_ctx, resp,
925 info3, name_domain, name_user);
926 if (!NT_STATUS_IS_OK(result)) {
927 DEBUG(10,("Failed to append Unix Username: %s\n",
933 /* currently, anything from here on potentially overwrites extra_data. */
935 if (request_flags & WBFLAG_PAM_INFO3_NDR) {
936 result = append_info3_as_ndr(mem_ctx, resp, info3);
937 if (!NT_STATUS_IS_OK(result)) {
938 DEBUG(10,("Failed to append INFO3 (NDR): %s\n",
944 if (request_flags & WBFLAG_PAM_INFO3_TEXT) {
945 result = append_info3_as_txt(mem_ctx, resp,
948 if (!NT_STATUS_IS_OK(result)) {
949 DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
955 if (request_flags & WBFLAG_PAM_AFS_TOKEN) {
956 result = append_afs_token(mem_ctx, resp,
957 info3, name_domain, name_user);
958 if (!NT_STATUS_IS_OK(result)) {
959 DEBUG(10,("Failed to append AFS token: %s\n",
965 result = NT_STATUS_OK;
971 static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
972 struct winbindd_cli_state *state,
973 struct netr_SamInfo3 **info3)
975 NTSTATUS result = NT_STATUS_LOGON_FAILURE;
976 uint16_t max_allowed_bad_attempts;
977 fstring name_domain, name_user;
979 enum lsa_SidType type;
980 uchar new_nt_pass[NT_HASH_LEN];
981 const uint8_t *cached_nt_pass;
982 const uint8_t *cached_salt;
983 struct netr_SamInfo3 *my_info3;
984 time_t kickoff_time, must_change_time;
985 bool password_good = false;
987 struct winbindd_tdc_domain *tdc_domain = NULL;
994 DEBUG(10,("winbindd_dual_pam_auth_cached\n"));
996 /* Parse domain and username */
998 parse_domain_user(state->request->data.auth.user, name_domain, name_user);
1001 if (!lookup_cached_name(name_domain,
1005 DEBUG(10,("winbindd_dual_pam_auth_cached: no such user in the cache\n"));
1006 return NT_STATUS_NO_SUCH_USER;
1009 if (type != SID_NAME_USER) {
1010 DEBUG(10,("winbindd_dual_pam_auth_cached: not a user (%s)\n", sid_type_lookup(type)));
1011 return NT_STATUS_LOGON_FAILURE;
1014 result = winbindd_get_creds(domain,
1020 if (!NT_STATUS_IS_OK(result)) {
1021 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get creds: %s\n", nt_errstr(result)));
1027 E_md4hash(state->request->data.auth.pass, new_nt_pass);
1029 dump_data_pw("new_nt_pass", new_nt_pass, NT_HASH_LEN);
1030 dump_data_pw("cached_nt_pass", cached_nt_pass, NT_HASH_LEN);
1032 dump_data_pw("cached_salt", cached_salt, NT_HASH_LEN);
1036 /* In this case we didn't store the nt_hash itself,
1037 but the MD5 combination of salt + nt_hash. */
1038 uchar salted_hash[NT_HASH_LEN];
1039 E_md5hash(cached_salt, new_nt_pass, salted_hash);
1041 password_good = (memcmp(cached_nt_pass, salted_hash,
1044 /* Old cached cred - direct store of nt_hash (bad bad bad !). */
1045 password_good = (memcmp(cached_nt_pass, new_nt_pass,
1049 if (password_good) {
1051 /* User *DOES* know the password, update logon_time and reset
1054 my_info3->base.user_flags |= NETLOGON_CACHED_ACCOUNT;
1056 if (my_info3->base.acct_flags & ACB_AUTOLOCK) {
1057 return NT_STATUS_ACCOUNT_LOCKED_OUT;
1060 if (my_info3->base.acct_flags & ACB_DISABLED) {
1061 return NT_STATUS_ACCOUNT_DISABLED;
1064 if (my_info3->base.acct_flags & ACB_WSTRUST) {
1065 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
1068 if (my_info3->base.acct_flags & ACB_SVRTRUST) {
1069 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
1072 if (my_info3->base.acct_flags & ACB_DOMTRUST) {
1073 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
1076 if (!(my_info3->base.acct_flags & ACB_NORMAL)) {
1077 DEBUG(0,("winbindd_dual_pam_auth_cached: whats wrong with that one?: 0x%08x\n",
1078 my_info3->base.acct_flags));
1079 return NT_STATUS_LOGON_FAILURE;
1082 kickoff_time = nt_time_to_unix(my_info3->base.kickoff_time);
1083 if (kickoff_time != 0 && time(NULL) > kickoff_time) {
1084 return NT_STATUS_ACCOUNT_EXPIRED;
1087 must_change_time = nt_time_to_unix(my_info3->base.force_password_change);
1088 if (must_change_time != 0 && must_change_time < time(NULL)) {
1089 /* we allow grace logons when the password has expired */
1090 my_info3->base.user_flags |= NETLOGON_GRACE_LOGON;
1091 /* return NT_STATUS_PASSWORD_EXPIRED; */
1096 if ((state->request->flags & WBFLAG_PAM_KRB5) &&
1097 ((tdc_domain = wcache_tdc_fetch_domain(state->mem_ctx, name_domain)) != NULL) &&
1098 ((tdc_domain->trust_type & LSA_TRUST_TYPE_UPLEVEL) ||
1099 /* used to cope with the case winbindd starting without network. */
1100 !strequal(tdc_domain->domain_name, tdc_domain->dns_name))) {
1103 const char *cc = NULL;
1105 const char *principal_s = NULL;
1106 const char *service = NULL;
1107 const char *user_ccache_file;
1109 if (domain->alt_name == NULL) {
1110 return NT_STATUS_INVALID_PARAMETER;
1113 uid = get_uid_from_request(state->request);
1115 DEBUG(0,("winbindd_dual_pam_auth_cached: invalid uid\n"));
1116 return NT_STATUS_INVALID_PARAMETER;
1119 cc = generate_krb5_ccache(state->mem_ctx,
1120 state->request->data.auth.krb5_cc_type,
1121 state->request->data.auth.uid,
1124 return NT_STATUS_NO_MEMORY;
1127 realm = talloc_strdup(state->mem_ctx, domain->alt_name);
1128 if (realm == NULL) {
1129 return NT_STATUS_NO_MEMORY;
1132 if (!strupper_m(realm)) {
1133 return NT_STATUS_INVALID_PARAMETER;
1136 principal_s = talloc_asprintf(state->mem_ctx, "%s@%s", name_user, realm);
1137 if (principal_s == NULL) {
1138 return NT_STATUS_NO_MEMORY;
1141 service = talloc_asprintf(state->mem_ctx, "%s/%s@%s", KRB5_TGS_NAME, realm, realm);
1142 if (service == NULL) {
1143 return NT_STATUS_NO_MEMORY;
1146 if (user_ccache_file != NULL) {
1148 fstrcpy(state->response->data.auth.krb5ccname,
1151 result = add_ccache_to_list(principal_s,
1154 state->request->data.auth.user,
1155 state->request->data.auth.pass,
1159 time(NULL) + lp_winbind_cache_time(),
1160 time(NULL) + WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
1163 if (!NT_STATUS_IS_OK(result)) {
1164 DEBUG(10,("winbindd_dual_pam_auth_cached: failed "
1165 "to add ccache to list: %s\n",
1166 nt_errstr(result)));
1170 #endif /* HAVE_KRB5 */
1172 /* FIXME: we possibly should handle logon hours as well (does xp when
1173 * offline?) see auth/auth_sam.c:sam_account_ok for details */
1175 unix_to_nt_time(&my_info3->base.logon_time, time(NULL));
1176 my_info3->base.bad_password_count = 0;
1178 result = winbindd_update_creds_by_info3(domain,
1179 state->request->data.auth.user,
1180 state->request->data.auth.pass,
1182 if (!NT_STATUS_IS_OK(result)) {
1183 DEBUG(1,("winbindd_dual_pam_auth_cached: failed to update creds: %s\n",
1184 nt_errstr(result)));
1188 return NT_STATUS_OK;
1192 /* User does *NOT* know the correct password, modify info3 accordingly, but only if online */
1193 if (domain->online == false) {
1197 /* failure of this is not critical */
1198 result = get_max_bad_attempts_from_lockout_policy(domain, state->mem_ctx, &max_allowed_bad_attempts);
1199 if (!NT_STATUS_IS_OK(result)) {
1200 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get max_allowed_bad_attempts. "
1201 "Won't be able to honour account lockout policies\n"));
1204 /* increase counter */
1205 my_info3->base.bad_password_count++;
1207 if (max_allowed_bad_attempts == 0) {
1212 if (my_info3->base.bad_password_count >= max_allowed_bad_attempts) {
1214 uint32_t password_properties;
1216 result = get_pwd_properties(domain, state->mem_ctx, &password_properties);
1217 if (!NT_STATUS_IS_OK(result)) {
1218 DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get password properties.\n"));
1221 if ((my_info3->base.rid != DOMAIN_RID_ADMINISTRATOR) ||
1222 (password_properties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)) {
1223 my_info3->base.acct_flags |= ACB_AUTOLOCK;
1228 result = winbindd_update_creds_by_info3(domain,
1229 state->request->data.auth.user,
1233 if (!NT_STATUS_IS_OK(result)) {
1234 DEBUG(0,("winbindd_dual_pam_auth_cached: failed to update creds %s\n",
1235 nt_errstr(result)));
1238 return NT_STATUS_LOGON_FAILURE;
1241 static NTSTATUS winbindd_dual_pam_auth_kerberos(struct winbindd_domain *domain,
1242 struct winbindd_cli_state *state,
1243 struct netr_SamInfo3 **info3)
1245 struct winbindd_domain *contact_domain;
1246 fstring name_domain, name_user;
1249 DEBUG(10,("winbindd_dual_pam_auth_kerberos\n"));
1251 /* Parse domain and username */
1253 parse_domain_user(state->request->data.auth.user, name_domain, name_user);
1255 /* what domain should we contact? */
1258 if (!(contact_domain = find_domain_from_name(name_domain))) {
1259 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1260 state->request->data.auth.user, name_domain, name_user, name_domain));
1261 result = NT_STATUS_NO_SUCH_USER;
1266 if (is_myname(name_domain)) {
1267 DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
1268 result = NT_STATUS_NO_SUCH_USER;
1272 contact_domain = find_domain_from_name(name_domain);
1273 if (contact_domain == NULL) {
1274 DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
1275 state->request->data.auth.user, name_domain, name_user, name_domain));
1277 result = NT_STATUS_NO_SUCH_USER;
1282 if (contact_domain->initialized &&
1283 contact_domain->active_directory) {
1287 if (!contact_domain->initialized) {
1288 init_dc_connection(contact_domain, false);
1291 if (!contact_domain->active_directory) {
1292 DEBUG(3,("krb5 auth requested but domain is not Active Directory\n"));
1293 return NT_STATUS_INVALID_LOGON_TYPE;
1296 result = winbindd_raw_kerberos_login(
1297 state->mem_ctx, contact_domain,
1298 state->request->data.auth.user,
1299 state->request->data.auth.pass,
1300 state->request->data.auth.krb5_cc_type,
1301 get_uid_from_request(state->request),
1302 info3, state->response->data.auth.krb5ccname);
1307 static NTSTATUS winbindd_dual_auth_passdb(TALLOC_CTX *mem_ctx,
1308 uint32_t logon_parameters,
1309 const char *domain, const char *user,
1310 const DATA_BLOB *challenge,
1311 const DATA_BLOB *lm_resp,
1312 const DATA_BLOB *nt_resp,
1314 uint8_t *pauthoritative,
1315 struct netr_SamInfo3 **pinfo3)
1317 struct auth_context *auth_context;
1318 struct auth_serversupplied_info *server_info;
1319 struct auth_usersupplied_info *user_info = NULL;
1320 struct tsocket_address *local;
1321 struct netr_SamInfo3 *info3;
1325 TALLOC_CTX *frame = talloc_stackframe();
1328 * We are authoritative by default
1330 *pauthoritative = 1;
1332 rc = tsocket_address_inet_from_strings(frame,
1339 return NT_STATUS_NO_MEMORY;
1343 * TODO: We should get the service description passed in from
1344 * the winbind client, so we can have "smb2", "squid" or "samr" logged
1347 status = make_user_info(frame, &user_info, user, user, domain, domain,
1348 lp_netbios_name(), local, local,
1350 lm_resp, nt_resp, NULL, NULL,
1351 NULL, AUTH_PASSWORD_RESPONSE);
1352 if (!NT_STATUS_IS_OK(status)) {
1353 DEBUG(10, ("make_user_info failed: %s\n", nt_errstr(status)));
1358 user_info->logon_parameters = logon_parameters;
1360 /* We don't want any more mapping of the username */
1361 user_info->mapped_state = True;
1363 /* We don't want to come back to winbindd or to do PAM account checks */
1364 user_info->flags |= USER_INFO_INFO3_AND_NO_AUTHZ;
1367 user_info->flags |= USER_INFO_INTERACTIVE_LOGON;
1370 status = make_auth3_context_for_winbind(frame, &auth_context);
1371 if (!NT_STATUS_IS_OK(status)) {
1372 DBG_ERR("make_auth3_context_for_winbind failed: %s\n",
1378 ok = auth3_context_set_challenge(auth_context,
1379 challenge->data, "fixed");
1382 return NT_STATUS_NO_MEMORY;
1385 status = auth_check_ntlm_password(mem_ctx,
1390 if (!NT_STATUS_IS_OK(status)) {
1395 info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
1396 if (info3 == NULL) {
1398 return NT_STATUS_NO_MEMORY;
1401 status = serverinfo_to_SamInfo3(server_info, info3);
1402 if (!NT_STATUS_IS_OK(status)) {
1405 DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
1406 nt_errstr(status)));
1411 DEBUG(10, ("Authenticaticating user %s\\%s returned %s\n", domain,
1412 user, nt_errstr(status)));
1417 static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
1418 TALLOC_CTX *mem_ctx,
1419 uint32_t logon_parameters,
1420 const char *username,
1421 const char *password,
1422 const char *domainname,
1423 const char *workstation,
1424 bool plaintext_given,
1425 const uint8_t chal[8],
1426 DATA_BLOB lm_response,
1427 DATA_BLOB nt_response,
1429 uint8_t *authoritative,
1431 uint16_t *_validation_level,
1432 union netr_Validation **_validation)
1435 int netr_attempts = 0;
1438 enum netr_LogonInfoClass logon_type_i;
1439 enum netr_LogonInfoClass logon_type_n;
1440 uint16_t validation_level = UINT16_MAX;
1441 union netr_Validation *validation = NULL;
1444 struct rpc_pipe_client *netlogon_pipe;
1448 result = cm_connect_netlogon(domain, &netlogon_pipe);
1450 if (NT_STATUS_EQUAL(result,
1451 NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
1453 * This means we don't have a trust account.
1456 result = NT_STATUS_NO_SUCH_USER;
1460 if (!NT_STATUS_IS_OK(result)) {
1461 DEBUG(3,("Could not open handle to NETLOGON pipe "
1462 "(error: %s, attempts: %d)\n",
1463 nt_errstr(result), netr_attempts));
1465 /* After the first retry always close the connection */
1466 if (netr_attempts > 0) {
1467 DEBUG(3, ("This is again a problem for this "
1468 "particular call, forcing the close "
1469 "of this connection\n"));
1470 invalidate_cm_connection(domain);
1473 /* After the second retry failover to the next DC */
1474 if (netr_attempts > 1) {
1476 * If the netlogon server is not reachable then
1477 * it is possible that the DC is rebuilding
1478 * sysvol and shutdown netlogon for that time.
1479 * We should failover to the next dc.
1481 DEBUG(3, ("This is the third problem for this "
1482 "particular call, adding DC to the "
1483 "negative cache list: %s %s\n", domain->name, domain->dcname));
1484 add_failed_connection_entry(domain->name,
1487 saf_delete(domain->name);
1490 /* Only allow 3 retries */
1491 if (netr_attempts < 3) {
1492 DEBUG(3, ("The connection to netlogon "
1493 "failed, retrying\n"));
1501 logon_type_i = NetlogonInteractiveInformation;
1502 logon_type_n = NetlogonNetworkInformation;
1503 if (domain->domain_trust_attribs & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST) {
1504 logon_type_i = NetlogonInteractiveTransitiveInformation;
1505 logon_type_n = NetlogonNetworkTransitiveInformation;
1508 if (domain->domain_trust_attribs & LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
1509 logon_type_i = NetlogonInteractiveTransitiveInformation;
1510 logon_type_n = NetlogonNetworkTransitiveInformation;
1513 if (domain->domain_trust_attribs & LSA_TRUST_ATTRIBUTE_NON_TRANSITIVE) {
1514 logon_type_i = NetlogonInteractiveInformation;
1515 logon_type_n = NetlogonNetworkInformation;
1518 if (domain->domain_trust_attribs & LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN) {
1519 logon_type_i = NetlogonInteractiveInformation;
1520 logon_type_n = NetlogonNetworkInformation;
1524 if (domain->conn.netlogon_creds_ctx == NULL) {
1525 DBG_NOTICE("No security credentials available for "
1526 "domain [%s]\n", domainname);
1527 result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1528 } else if (plaintext_given) {
1529 result = rpccli_netlogon_password_logon(
1530 domain->conn.netlogon_creds_ctx,
1531 netlogon_pipe->binding_handle,
1543 } else if (interactive) {
1544 result = rpccli_netlogon_interactive_logon(
1545 domain->conn.netlogon_creds_ctx,
1546 netlogon_pipe->binding_handle,
1560 result = rpccli_netlogon_network_logon(
1561 domain->conn.netlogon_creds_ctx,
1562 netlogon_pipe->binding_handle,
1579 * we increment this after the "feature negotiation"
1580 * for can_do_samlogon_ex and can_do_validation6
1584 /* We have to try a second time as cm_connect_netlogon
1585 might not yet have noticed that the DC has killed
1588 if (!rpccli_is_connected(netlogon_pipe)) {
1593 /* if we get access denied, a possible cause was that we had
1594 and open connection to the DC, but someone changed our
1595 machine account password out from underneath us using 'net
1596 rpc changetrustpw' */
1598 if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1599 DEBUG(1,("winbind_samlogon_retry_loop: sam_logon returned "
1600 "ACCESS_DENIED. Maybe the DC has Restrict "
1601 "NTLM set or the trust account "
1602 "password was changed and we didn't know it. "
1603 "Killing connections to domain %s\n",
1605 invalidate_cm_connection(domain);
1609 if (NT_STATUS_EQUAL(result, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
1611 * Got DCERPC_FAULT_OP_RNG_ERROR for SamLogon
1612 * (no Ex). This happens against old Samba
1613 * DCs, if LogonSamLogonEx() fails with an error
1614 * e.g. NT_STATUS_NO_SUCH_USER or NT_STATUS_WRONG_PASSWORD.
1616 * The server will log something like this:
1617 * api_net_sam_logon_ex: Failed to marshall NET_R_SAM_LOGON_EX.
1619 * This sets the whole connection into a fault_state mode
1620 * and all following request get NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE.
1622 * This also happens to our retry with LogonSamLogonWithFlags()
1623 * and LogonSamLogon().
1625 * In order to recover from this situation, we need to
1626 * drop the connection.
1628 invalidate_cm_connection(domain);
1629 result = NT_STATUS_LOGON_FAILURE;
1633 } while ( (attempts < 2) && retry );
1635 if (NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT)) {
1636 DEBUG(3,("winbind_samlogon_retry_loop: sam_network_logon(ex) "
1637 "returned NT_STATUS_IO_TIMEOUT after the retry. "
1638 "Killing connections to domain %s\n",
1640 invalidate_cm_connection(domain);
1643 if (!NT_STATUS_IS_OK(result)) {
1647 *_validation_level = validation_level;
1648 *_validation = validation;
1649 return NT_STATUS_OK;
1652 static NTSTATUS winbindd_dual_pam_auth_samlogon(
1653 TALLOC_CTX *mem_ctx,
1654 struct winbindd_domain *domain,
1657 uint32_t request_flags,
1658 uint16_t *_validation_level,
1659 union netr_Validation **_validation)
1665 unsigned char local_nt_response[24];
1666 fstring name_domain, name_user;
1668 uint8_t authoritative = 0;
1670 uint16_t validation_level;
1671 union netr_Validation *validation = NULL;
1672 struct netr_SamBaseInfo *base_info = NULL;
1674 DEBUG(10,("winbindd_dual_pam_auth_samlogon\n"));
1676 /* Parse domain and username */
1678 parse_domain_user(user, name_domain, name_user);
1681 * We check against domain->name instead of
1682 * name_domain, as find_auth_domain() ->
1683 * find_domain_from_name_noinit() already decided
1684 * that we are in a child for the correct domain.
1686 * name_domain can also be lp_realm()
1687 * we need to check against domain->name.
1689 if (strequal(domain->name, get_global_sam_name())) {
1690 DATA_BLOB chal_blob = data_blob_const(chal, sizeof(chal));
1691 struct netr_SamInfo3 *info3 = NULL;
1693 /* do password magic */
1695 generate_random_buffer(chal, sizeof(chal));
1697 if (lp_client_ntlmv2_auth()) {
1698 DATA_BLOB server_chal;
1699 DATA_BLOB names_blob;
1700 server_chal = data_blob_const(chal, 8);
1702 /* note that the 'workgroup' here is for the local
1703 machine. The 'server name' must match the
1704 'workstation' passed to the actual SamLogon call.
1706 names_blob = NTLMv2_generate_names_blob(
1707 mem_ctx, lp_netbios_name(), lp_workgroup());
1709 if (!SMBNTLMv2encrypt(mem_ctx, name_user, name_domain,
1713 &lm_resp, &nt_resp, NULL, NULL)) {
1714 data_blob_free(&names_blob);
1715 DEBUG(0, ("winbindd_pam_auth: SMBNTLMv2encrypt() failed!\n"));
1716 result = NT_STATUS_NO_MEMORY;
1719 data_blob_free(&names_blob);
1721 lm_resp = data_blob_null;
1722 SMBNTencrypt(pass, chal, local_nt_response);
1724 nt_resp = data_blob_talloc(mem_ctx, local_nt_response,
1725 sizeof(local_nt_response));
1728 result = winbindd_dual_auth_passdb(
1729 talloc_tos(), 0, name_domain, name_user,
1730 &chal_blob, &lm_resp, &nt_resp,
1731 true, /* interactive */
1736 * We need to try the remote NETLOGON server if this is
1737 * not authoritative (for example on the RODC).
1739 if (authoritative != 0) {
1740 if (NT_STATUS_IS_OK(result)) {
1741 result = map_info3_to_validation(
1747 if (!NT_STATUS_IS_OK(result)) {
1756 /* check authentication loop */
1758 result = winbind_samlogon_retry_loop(domain,
1765 true, /* plaintext_given */
1767 data_blob_null, data_blob_null,
1768 true, /* interactive */
1773 if (!NT_STATUS_IS_OK(result)) {
1777 /* handle the case where a NT4 DC does not fill in the acct_flags in
1778 * the samlogon reply info3. When accurate info3 is required by the
1779 * caller, we look up the account flags ourselves - gd */
1781 switch (validation_level) {
1783 base_info = &validation->sam3->base;
1786 base_info = &validation->sam6->base;
1789 DBG_ERR("Bad validation level %d", (int)validation_level);
1790 result = NT_STATUS_INTERNAL_ERROR;
1793 if ((request_flags & WBFLAG_PAM_INFO3_TEXT) &&
1794 (base_info->acct_flags == 0))
1796 struct rpc_pipe_client *samr_pipe;
1797 struct policy_handle samr_domain_handle, user_pol;
1798 union samr_UserInfo *info = NULL;
1799 NTSTATUS status_tmp, result_tmp;
1800 uint32_t acct_flags;
1801 struct dcerpc_binding_handle *b;
1803 status_tmp = cm_connect_sam(domain, mem_ctx, false,
1804 &samr_pipe, &samr_domain_handle);
1806 if (!NT_STATUS_IS_OK(status_tmp)) {
1807 DEBUG(3, ("could not open handle to SAMR pipe: %s\n",
1808 nt_errstr(status_tmp)));
1812 b = samr_pipe->binding_handle;
1814 status_tmp = dcerpc_samr_OpenUser(b, mem_ctx,
1815 &samr_domain_handle,
1816 MAXIMUM_ALLOWED_ACCESS,
1821 if (!NT_STATUS_IS_OK(status_tmp)) {
1822 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1823 nt_errstr(status_tmp)));
1826 if (!NT_STATUS_IS_OK(result_tmp)) {
1827 DEBUG(3, ("could not open user handle on SAMR pipe: %s\n",
1828 nt_errstr(result_tmp)));
1832 status_tmp = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1838 if (any_nt_status_not_ok(status_tmp, result_tmp,
1840 DEBUG(3, ("could not query user info on SAMR pipe: %s\n",
1841 nt_errstr(status_tmp)));
1842 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1846 acct_flags = info->info16.acct_flags;
1848 if (acct_flags == 0) {
1849 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1853 base_info->acct_flags = acct_flags;
1855 DEBUG(10,("successfully retrieved acct_flags 0x%x\n", acct_flags));
1857 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
1861 if (NT_STATUS_IS_OK(result)) {
1862 *_validation_level = validation_level;
1863 *_validation = validation;
1868 enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
1869 struct winbindd_cli_state *state)
1871 NTSTATUS result = NT_STATUS_LOGON_FAILURE;
1872 NTSTATUS krb5_result = NT_STATUS_OK;
1873 fstring name_domain, name_user;
1875 fstring domain_user;
1876 uint16_t validation_level = UINT16_MAX;
1877 union netr_Validation *validation = NULL;
1878 NTSTATUS name_map_status = NT_STATUS_UNSUCCESSFUL;
1880 /* Ensure null termination */
1881 state->request->data.auth.user[sizeof(state->request->data.auth.user)-1]='\0';
1883 /* Ensure null termination */
1884 state->request->data.auth.pass[sizeof(state->request->data.auth.pass)-1]='\0';
1886 DEBUG(3, ("[%5lu]: dual pam auth %s\n", (unsigned long)state->pid,
1887 state->request->data.auth.user));
1889 /* Parse domain and username */
1891 name_map_status = normalize_name_unmap(state->mem_ctx,
1892 state->request->data.auth.user,
1895 /* If the name normalization didnt' actually do anything,
1896 just use the original name */
1898 if (!NT_STATUS_IS_OK(name_map_status) &&
1899 !NT_STATUS_EQUAL(name_map_status, NT_STATUS_FILE_RENAMED))
1901 mapped_user = state->request->data.auth.user;
1904 parse_domain_user(mapped_user, name_domain, name_user);
1906 if ( mapped_user != state->request->data.auth.user ) {
1907 fstr_sprintf( domain_user, "%s%c%s", name_domain,
1908 *lp_winbind_separator(),
1910 strlcpy( state->request->data.auth.user, domain_user,
1911 sizeof(state->request->data.auth.user));
1914 if (!domain->online) {
1915 result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
1916 if (domain->startup) {
1917 /* Logons are very important to users. If we're offline and
1918 we get a request within the first 30 seconds of startup,
1919 try very hard to find a DC and go online. */
1921 DEBUG(10,("winbindd_dual_pam_auth: domain: %s offline and auth "
1922 "request in startup mode.\n", domain->name ));
1924 winbindd_flush_negative_conn_cache(domain);
1925 result = init_dc_connection(domain, false);
1929 DEBUG(10,("winbindd_dual_pam_auth: domain: %s last was %s\n", domain->name, domain->online ? "online":"offline"));
1931 /* Check for Kerberos authentication */
1932 if (domain->online && (state->request->flags & WBFLAG_PAM_KRB5)) {
1933 struct netr_SamInfo3 *info3 = NULL;
1935 result = winbindd_dual_pam_auth_kerberos(domain, state, &info3);
1936 /* save for later */
1937 krb5_result = result;
1940 if (NT_STATUS_IS_OK(result)) {
1941 DEBUG(10,("winbindd_dual_pam_auth_kerberos succeeded\n"));
1943 result = map_info3_to_validation(state->mem_ctx,
1948 if (!NT_STATUS_IS_OK(result)) {
1949 DBG_ERR("map_info3_to_validation failed\n");
1952 goto process_result;
1955 DBG_DEBUG("winbindd_dual_pam_auth_kerberos failed: %s\n",
1958 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS) ||
1959 NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT) ||
1960 NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
1961 DEBUG(10,("winbindd_dual_pam_auth_kerberos setting domain to offline\n"));
1962 set_domain_offline( domain );
1966 /* there are quite some NT_STATUS errors where there is no
1967 * point in retrying with a samlogon, we explictly have to take
1968 * care not to increase the bad logon counter on the DC */
1970 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_DISABLED) ||
1971 NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_EXPIRED) ||
1972 NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_LOCKED_OUT) ||
1973 NT_STATUS_EQUAL(result, NT_STATUS_INVALID_LOGON_HOURS) ||
1974 NT_STATUS_EQUAL(result, NT_STATUS_INVALID_WORKSTATION) ||
1975 NT_STATUS_EQUAL(result, NT_STATUS_LOGON_FAILURE) ||
1976 NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER) ||
1977 NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED) ||
1978 NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) ||
1979 NT_STATUS_EQUAL(result, NT_STATUS_WRONG_PASSWORD)) {
1983 if (state->request->flags & WBFLAG_PAM_FALLBACK_AFTER_KRB5) {
1984 DEBUG(3,("falling back to samlogon\n"));
1992 /* Check for Samlogon authentication */
1993 if (domain->online) {
1994 struct netr_SamBaseInfo *base_info = NULL;
1996 result = winbindd_dual_pam_auth_samlogon(
1997 state->mem_ctx, domain,
1998 state->request->data.auth.user,
1999 state->request->data.auth.pass,
2000 state->request->flags,
2004 if (NT_STATUS_IS_OK(result)) {
2005 DEBUG(10,("winbindd_dual_pam_auth_samlogon succeeded\n"));
2007 switch (validation_level) {
2009 base_info = &validation->sam3->base;
2012 base_info = &validation->sam6->base;
2015 DBG_ERR("Bad validation level %d\n",
2017 result = NT_STATUS_INTERNAL_ERROR;
2021 /* add the Krb5 err if we have one */
2022 if ( NT_STATUS_EQUAL(krb5_result, NT_STATUS_TIME_DIFFERENCE_AT_DC ) ) {
2023 base_info->user_flags |= LOGON_KRB5_FAIL_CLOCK_SKEW;
2026 goto process_result;
2029 DEBUG(10,("winbindd_dual_pam_auth_samlogon failed: %s\n",
2030 nt_errstr(result)));
2032 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS) ||
2033 NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT) ||
2034 NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND))
2036 DEBUG(10,("winbindd_dual_pam_auth_samlogon setting domain to offline\n"));
2037 set_domain_offline( domain );
2041 if (domain->online) {
2042 /* We're still online - fail. */
2048 /* Check for Cached logons */
2049 if (!domain->online && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN) &&
2050 lp_winbind_offline_logon()) {
2051 struct netr_SamInfo3 *info3 = NULL;
2053 result = winbindd_dual_pam_auth_cached(domain, state, &info3);
2055 if (!NT_STATUS_IS_OK(result)) {
2056 DEBUG(10,("winbindd_dual_pam_auth_cached failed: %s\n", nt_errstr(result)));
2059 DEBUG(10,("winbindd_dual_pam_auth_cached succeeded\n"));
2061 result = map_info3_to_validation(state->mem_ctx,
2066 if (!NT_STATUS_IS_OK(result)) {
2067 DBG_ERR("map_info3_to_validation failed\n");
2074 if (NT_STATUS_IS_OK(result)) {
2075 struct dom_sid user_sid;
2076 TALLOC_CTX *base_ctx = NULL;
2077 struct netr_SamBaseInfo *base_info = NULL;
2078 struct netr_SamInfo3 *info3 = NULL;
2080 switch (validation_level) {
2082 base_ctx = validation->sam3;
2083 base_info = &validation->sam3->base;
2086 base_ctx = validation->sam6;
2087 base_info = &validation->sam6->base;
2090 result = NT_STATUS_INTERNAL_ERROR;
2094 sid_compose(&user_sid, base_info->domain_sid, base_info->rid);
2096 if (base_info->full_name.string == NULL) {
2097 struct netr_SamInfo3 *cached_info3;
2099 cached_info3 = netsamlogon_cache_get(state->mem_ctx,
2101 if (cached_info3 != NULL &&
2102 cached_info3->base.full_name.string != NULL) {
2103 base_info->full_name.string = talloc_strdup(
2105 cached_info3->base.full_name.string);
2106 if (base_info->full_name.string == NULL) {
2107 result = NT_STATUS_NO_MEMORY;
2112 /* this might fail so we don't check the return code */
2113 wcache_query_user_fullname(domain,
2116 &base_info->full_name.string);
2120 result = map_validation_to_info3(talloc_tos(),
2124 if (!NT_STATUS_IS_OK(result)) {
2128 wcache_invalidate_samlogon(find_domain_from_name(name_domain),
2130 netsamlogon_cache_store(name_user, info3);
2132 /* save name_to_sid info as early as possible (only if
2133 this is our primary domain so we don't invalidate
2134 the cache entry by storing the seq_num for the wrong
2136 if ( domain->primary ) {
2137 cache_name2sid(domain, name_domain, name_user,
2138 SID_NAME_USER, &user_sid);
2141 /* Check if the user is in the right group */
2143 result = check_info3_in_group(
2145 state->request->data.auth.require_membership_of_sid);
2146 if (!NT_STATUS_IS_OK(result)) {
2147 DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
2148 state->request->data.auth.user,
2149 state->request->data.auth.require_membership_of_sid));
2153 result = append_auth_data(state->mem_ctx, state->response,
2154 state->request->flags,
2157 name_domain, name_user);
2158 if (!NT_STATUS_IS_OK(result)) {
2162 if ((state->request->flags & WBFLAG_PAM_CACHED_LOGIN)
2163 && lp_winbind_offline_logon()) {
2165 result = winbindd_store_creds(domain,
2166 state->request->data.auth.user,
2167 state->request->data.auth.pass,
2171 if (state->request->flags & WBFLAG_PAM_GET_PWD_POLICY) {
2173 * WBFLAG_PAM_GET_PWD_POLICY is not used within
2174 * any Samba caller anymore.
2176 * We just fake this based on the effective values
2177 * for the user, for legacy callers.
2179 fake_password_policy(state->response, &info3->base);
2182 result = NT_STATUS_OK;
2186 /* give us a more useful (more correct?) error code */
2187 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
2188 (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
2189 result = NT_STATUS_NO_LOGON_SERVERS;
2192 set_auth_errors(state->response, result);
2194 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n",
2195 state->request->data.auth.user,
2196 state->response->data.auth.nt_status_string,
2197 state->response->data.auth.pam_error));
2199 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2202 NTSTATUS winbind_dual_SamLogon(struct winbindd_domain *domain,
2203 TALLOC_CTX *mem_ctx,
2204 uint32_t logon_parameters,
2205 const char *name_user,
2206 const char *name_domain,
2207 const char *workstation,
2208 const uint8_t chal[8],
2209 DATA_BLOB lm_response,
2210 DATA_BLOB nt_response,
2211 uint8_t *authoritative,
2214 uint16_t *_validation_level,
2215 union netr_Validation **_validation)
2217 uint16_t validation_level;
2218 union netr_Validation *validation = NULL;
2222 * We check against domain->name instead of
2223 * name_domain, as find_auth_domain() ->
2224 * find_domain_from_name_noinit() already decided
2225 * that we are in a child for the correct domain.
2227 * name_domain can also be lp_realm()
2228 * we need to check against domain->name.
2230 if (!skip_sam && strequal(domain->name, get_global_sam_name())) {
2231 DATA_BLOB chal_blob = data_blob_const(
2233 struct netr_SamInfo3 *info3 = NULL;
2235 result = winbindd_dual_auth_passdb(
2238 name_domain, name_user,
2239 &chal_blob, &lm_response, &nt_response,
2240 false, /* interactive */
2243 if (NT_STATUS_IS_OK(result)) {
2244 result = map_info3_to_validation(mem_ctx,
2249 if (!NT_STATUS_IS_OK(result)) {
2255 * We need to try the remote NETLOGON server if this is
2256 * not authoritative.
2258 if (*authoritative != 0) {
2260 goto process_result;
2264 result = winbind_samlogon_retry_loop(domain,
2268 NULL, /* password */
2270 /* Bug #3248 - found by Stefan Burkei. */
2271 workstation, /* We carefully set this above so use it... */
2272 false, /* plaintext_given */
2276 false, /* interactive */
2281 if (!NT_STATUS_IS_OK(result)) {
2287 if (NT_STATUS_IS_OK(result)) {
2288 struct dom_sid user_sid;
2289 TALLOC_CTX *base_ctx = NULL;
2290 struct netr_SamBaseInfo *base_info = NULL;
2291 struct netr_SamInfo3 *info3 = NULL;
2293 switch (validation_level) {
2295 base_ctx = validation->sam3;
2296 base_info = &validation->sam3->base;
2299 base_ctx = validation->sam6;
2300 base_info = &validation->sam6->base;
2303 result = NT_STATUS_INTERNAL_ERROR;
2307 sid_compose(&user_sid, base_info->domain_sid, base_info->rid);
2309 if (base_info->full_name.string == NULL) {
2310 struct netr_SamInfo3 *cached_info3;
2312 cached_info3 = netsamlogon_cache_get(mem_ctx,
2314 if (cached_info3 != NULL &&
2315 cached_info3->base.full_name.string != NULL)
2317 base_info->full_name.string = talloc_strdup(
2319 cached_info3->base.full_name.string);
2322 /* this might fail so we don't check the return code */
2323 wcache_query_user_fullname(domain,
2326 &base_info->full_name.string);
2330 result = map_validation_to_info3(talloc_tos(),
2334 if (!NT_STATUS_IS_OK(result)) {
2337 wcache_invalidate_samlogon(find_domain_from_name(name_domain),
2339 netsamlogon_cache_store(name_user, info3);
2345 /* give us a more useful (more correct?) error code */
2346 if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
2347 (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
2348 result = NT_STATUS_NO_LOGON_SERVERS;
2351 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2352 ("NTLM CRAP authentication for user [%s]\\[%s] returned %s\n",
2355 nt_errstr(result)));
2357 if (!NT_STATUS_IS_OK(result)) {
2361 *_validation_level = validation_level;
2362 *_validation = validation;
2363 return NT_STATUS_OK;
2366 enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
2367 struct winbindd_cli_state *state)
2370 const char *name_user = NULL;
2371 const char *name_domain = NULL;
2372 const char *workstation;
2373 uint8_t authoritative = 0;
2375 uint16_t validation_level;
2376 union netr_Validation *validation = NULL;
2377 DATA_BLOB lm_resp, nt_resp;
2379 /* This is child-only, so no check for privileged access is needed
2382 /* Ensure null termination */
2383 state->request->data.auth_crap.user[sizeof(state->request->data.auth_crap.user)-1]=0;
2384 state->request->data.auth_crap.domain[sizeof(state->request->data.auth_crap.domain)-1]=0;
2386 name_user = state->request->data.auth_crap.user;
2387 name_domain = state->request->data.auth_crap.domain;
2388 workstation = state->request->data.auth_crap.workstation;
2390 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state->pid,
2391 name_domain, name_user));
2393 if (state->request->data.auth_crap.lm_resp_len > sizeof(state->request->data.auth_crap.lm_resp)
2394 || state->request->data.auth_crap.nt_resp_len > sizeof(state->request->data.auth_crap.nt_resp)) {
2395 if (!(state->request->flags & WBFLAG_BIG_NTLMV2_BLOB) ||
2396 state->request->extra_len != state->request->data.auth_crap.nt_resp_len) {
2397 DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n",
2398 state->request->data.auth_crap.lm_resp_len,
2399 state->request->data.auth_crap.nt_resp_len));
2400 result = NT_STATUS_INVALID_PARAMETER;
2405 lm_resp = data_blob_talloc(state->mem_ctx, state->request->data.auth_crap.lm_resp,
2406 state->request->data.auth_crap.lm_resp_len);
2408 if (state->request->flags & WBFLAG_BIG_NTLMV2_BLOB) {
2409 nt_resp = data_blob_talloc(state->mem_ctx,
2410 state->request->extra_data.data,
2411 state->request->data.auth_crap.nt_resp_len);
2413 nt_resp = data_blob_talloc(state->mem_ctx,
2414 state->request->data.auth_crap.nt_resp,
2415 state->request->data.auth_crap.nt_resp_len);
2418 result = winbind_dual_SamLogon(domain,
2420 state->request->data.auth_crap.logon_parameters,
2423 /* Bug #3248 - found by Stefan Burkei. */
2424 workstation, /* We carefully set this above so use it... */
2425 state->request->data.auth_crap.chal,
2433 if (!NT_STATUS_IS_OK(result)) {
2434 state->response->data.auth.authoritative = authoritative;
2438 if (NT_STATUS_IS_OK(result)) {
2439 struct netr_SamInfo3 *info3 = NULL;
2441 result = map_validation_to_info3(state->mem_ctx,
2445 if (!NT_STATUS_IS_OK(result)) {
2449 /* Check if the user is in the right group */
2450 result = check_info3_in_group(
2452 state->request->data.auth_crap.require_membership_of_sid);
2453 if (!NT_STATUS_IS_OK(result)) {
2454 DEBUG(3, ("User %s is not in the required group (%s), so "
2455 "crap authentication is rejected\n",
2456 state->request->data.auth_crap.user,
2457 state->request->data.auth_crap.require_membership_of_sid));
2461 result = append_auth_data(state->mem_ctx, state->response,
2462 state->request->flags,
2465 name_domain, name_user);
2466 if (!NT_STATUS_IS_OK(result)) {
2473 if (state->request->flags & WBFLAG_PAM_NT_STATUS_SQUASH) {
2474 result = nt_status_squash(result);
2477 set_auth_errors(state->response, result);
2479 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2482 enum winbindd_result winbindd_dual_pam_chauthtok(struct winbindd_domain *contact_domain,
2483 struct winbindd_cli_state *state)
2486 char *newpass = NULL;
2487 struct policy_handle dom_pol;
2488 struct rpc_pipe_client *cli = NULL;
2489 bool got_info = false;
2490 struct samr_DomInfo1 *info = NULL;
2491 struct userPwdChangeFailureInformation *reject = NULL;
2492 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2493 fstring domain, user;
2494 struct dcerpc_binding_handle *b = NULL;
2496 ZERO_STRUCT(dom_pol);
2498 DEBUG(3, ("[%5lu]: dual pam chauthtok %s\n", (unsigned long)state->pid,
2499 state->request->data.auth.user));
2501 if (!parse_domain_user(state->request->data.chauthtok.user, domain, user)) {
2505 /* Change password */
2507 oldpass = state->request->data.chauthtok.oldpass;
2508 newpass = state->request->data.chauthtok.newpass;
2510 /* Initialize reject reason */
2511 state->response->data.auth.reject_reason = Undefined;
2513 /* Get sam handle */
2515 result = cm_connect_sam(contact_domain, state->mem_ctx, true, &cli,
2517 if (!NT_STATUS_IS_OK(result)) {
2518 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
2522 b = cli->binding_handle;
2524 result = rpccli_samr_chgpasswd_user3(cli, state->mem_ctx,
2531 /* Windows 2003 returns NT_STATUS_PASSWORD_RESTRICTION */
2533 if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION) ) {
2535 fill_in_password_policy(state->response, info);
2537 state->response->data.auth.reject_reason =
2538 reject->extendedFailureReason;
2543 /* atm the pidl generated rpccli_samr_ChangePasswordUser3 function will
2544 * return with NT_STATUS_BUFFER_TOO_SMALL for w2k dcs as w2k just
2545 * returns with 4byte error code (NT_STATUS_NOT_SUPPORTED) which is too
2546 * short to comply with the samr_ChangePasswordUser3 idl - gd */
2548 /* only fallback when the chgpasswd_user3 call is not supported */
2549 if (NT_STATUS_EQUAL(result, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE) ||
2550 NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED) ||
2551 NT_STATUS_EQUAL(result, NT_STATUS_BUFFER_TOO_SMALL) ||
2552 NT_STATUS_EQUAL(result, NT_STATUS_NOT_IMPLEMENTED)) {
2554 DEBUG(10,("Password change with chgpasswd_user3 failed with: %s, retrying chgpasswd_user2\n",
2555 nt_errstr(result)));
2557 result = rpccli_samr_chgpasswd_user2(cli, state->mem_ctx, user, newpass, oldpass);
2559 /* Windows 2000 returns NT_STATUS_ACCOUNT_RESTRICTION.
2560 Map to the same status code as Windows 2003. */
2562 if ( NT_STATUS_EQUAL(NT_STATUS_ACCOUNT_RESTRICTION, result ) ) {
2563 result = NT_STATUS_PASSWORD_RESTRICTION;
2569 if (NT_STATUS_IS_OK(result)
2570 && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN)
2571 && lp_winbind_offline_logon()) {
2572 result = winbindd_update_creds_by_name(contact_domain, user,
2574 /* Again, this happens when we login from gdm or xdm
2575 * and the password expires, *BUT* cached crendentials
2576 * doesn't exist. winbindd_update_creds_by_name()
2577 * returns NT_STATUS_NO_SUCH_USER.
2578 * This is not a failure.
2581 if (NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER)) {
2582 result = NT_STATUS_OK;
2585 if (!NT_STATUS_IS_OK(result)) {
2586 DEBUG(10, ("Failed to store creds: %s\n",
2587 nt_errstr(result)));
2588 goto process_result;
2592 if (!NT_STATUS_IS_OK(result) && !got_info && contact_domain) {
2594 NTSTATUS policy_ret;
2596 policy_ret = fillup_password_policy(
2597 contact_domain, state->response);
2599 /* failure of this is non critical, it will just provide no
2600 * additional information to the client why the change has
2601 * failed - Guenther */
2603 if (!NT_STATUS_IS_OK(policy_ret)) {
2604 DEBUG(10,("Failed to get password policies: %s\n", nt_errstr(policy_ret)));
2605 goto process_result;
2611 if (strequal(contact_domain->name, get_global_sam_name())) {
2612 /* FIXME: internal rpc pipe does not cache handles yet */
2614 if (is_valid_policy_hnd(&dom_pol)) {
2616 dcerpc_samr_Close(b, state->mem_ctx, &dom_pol, &_result);
2622 set_auth_errors(state->response, result);
2624 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2625 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
2628 state->response->data.auth.nt_status_string,
2629 state->response->data.auth.pam_error));
2631 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2634 enum winbindd_result winbindd_dual_pam_logoff(struct winbindd_domain *domain,
2635 struct winbindd_cli_state *state)
2637 NTSTATUS result = NT_STATUS_NOT_SUPPORTED;
2639 DEBUG(3, ("[%5lu]: pam dual logoff %s\n", (unsigned long)state->pid,
2640 state->request->data.logoff.user));
2642 if (!(state->request->flags & WBFLAG_PAM_KRB5)) {
2643 result = NT_STATUS_OK;
2644 goto process_result;
2647 if (state->request->data.logoff.krb5ccname[0] == '\0') {
2648 result = NT_STATUS_OK;
2649 goto process_result;
2654 if (state->request->data.logoff.uid == (uid_t)-1) {
2655 DEBUG(0,("winbindd_pam_logoff: invalid uid\n"));
2656 goto process_result;
2659 /* what we need here is to find the corresponding krb5 ccache name *we*
2660 * created for a given username and destroy it */
2662 if (!ccache_entry_exists(state->request->data.logoff.user)) {
2663 result = NT_STATUS_OK;
2664 DEBUG(10,("winbindd_pam_logoff: no entry found.\n"));
2665 goto process_result;
2668 if (!ccache_entry_identical(state->request->data.logoff.user,
2669 state->request->data.logoff.uid,
2670 state->request->data.logoff.krb5ccname)) {
2671 DEBUG(0,("winbindd_pam_logoff: cached entry differs.\n"));
2672 goto process_result;
2675 result = remove_ccache(state->request->data.logoff.user);
2676 if (!NT_STATUS_IS_OK(result)) {
2677 DEBUG(0,("winbindd_pam_logoff: failed to remove ccache: %s\n",
2678 nt_errstr(result)));
2679 goto process_result;
2683 * Remove any mlock'ed memory creds in the child
2684 * we might be using for krb5 ticket renewal.
2687 winbindd_delete_memory_creds(state->request->data.logoff.user);
2690 result = NT_STATUS_NOT_SUPPORTED;
2696 set_auth_errors(state->response, result);
2698 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2701 /* Change user password with auth crap*/
2703 enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domain *domainSt, struct winbindd_cli_state *state)
2706 DATA_BLOB new_nt_password;
2707 DATA_BLOB old_nt_hash_enc;
2708 DATA_BLOB new_lm_password;
2709 DATA_BLOB old_lm_hash_enc;
2710 fstring domain,user;
2711 struct policy_handle dom_pol;
2712 struct winbindd_domain *contact_domain = domainSt;
2713 struct rpc_pipe_client *cli = NULL;
2714 struct dcerpc_binding_handle *b = NULL;
2716 ZERO_STRUCT(dom_pol);
2718 /* Ensure null termination */
2719 state->request->data.chng_pswd_auth_crap.user[
2720 sizeof(state->request->data.chng_pswd_auth_crap.user)-1]=0;
2721 state->request->data.chng_pswd_auth_crap.domain[
2722 sizeof(state->request->data.chng_pswd_auth_crap.domain)-1]=0;
2726 DEBUG(3, ("[%5lu]: pam change pswd auth crap domain: %s user: %s\n",
2727 (unsigned long)state->pid,
2728 state->request->data.chng_pswd_auth_crap.domain,
2729 state->request->data.chng_pswd_auth_crap.user));
2731 if (lp_winbind_offline_logon()) {
2732 DEBUG(0,("Refusing password change as winbind offline logons are enabled. "));
2733 DEBUGADD(0,("Changing passwords here would risk inconsistent logons\n"));
2734 result = NT_STATUS_ACCESS_DENIED;
2738 if (*state->request->data.chng_pswd_auth_crap.domain) {
2739 fstrcpy(domain,state->request->data.chng_pswd_auth_crap.domain);
2741 parse_domain_user(state->request->data.chng_pswd_auth_crap.user,
2745 DEBUG(3,("no domain specified with username (%s) - "
2747 state->request->data.chng_pswd_auth_crap.user));
2748 result = NT_STATUS_NO_SUCH_USER;
2753 if (!*domain && lp_winbind_use_default_domain()) {
2754 fstrcpy(domain,lp_workgroup());
2758 fstrcpy(user, state->request->data.chng_pswd_auth_crap.user);
2761 DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n",
2762 (unsigned long)state->pid, domain, user));
2764 /* Change password */
2765 new_nt_password = data_blob_const(
2766 state->request->data.chng_pswd_auth_crap.new_nt_pswd,
2767 state->request->data.chng_pswd_auth_crap.new_nt_pswd_len);
2769 old_nt_hash_enc = data_blob_const(
2770 state->request->data.chng_pswd_auth_crap.old_nt_hash_enc,
2771 state->request->data.chng_pswd_auth_crap.old_nt_hash_enc_len);
2773 if(state->request->data.chng_pswd_auth_crap.new_lm_pswd_len > 0) {
2774 new_lm_password = data_blob_const(
2775 state->request->data.chng_pswd_auth_crap.new_lm_pswd,
2776 state->request->data.chng_pswd_auth_crap.new_lm_pswd_len);
2778 old_lm_hash_enc = data_blob_const(
2779 state->request->data.chng_pswd_auth_crap.old_lm_hash_enc,
2780 state->request->data.chng_pswd_auth_crap.old_lm_hash_enc_len);
2782 new_lm_password = data_blob_null;
2783 old_lm_hash_enc = data_blob_null;
2786 /* Get sam handle */
2788 result = cm_connect_sam(contact_domain, state->mem_ctx, true, &cli, &dom_pol);
2789 if (!NT_STATUS_IS_OK(result)) {
2790 DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
2794 b = cli->binding_handle;
2796 result = rpccli_samr_chng_pswd_auth_crap(
2797 cli, state->mem_ctx, user, new_nt_password, old_nt_hash_enc,
2798 new_lm_password, old_lm_hash_enc);
2802 if (strequal(contact_domain->name, get_global_sam_name())) {
2803 /* FIXME: internal rpc pipe does not cache handles yet */
2805 if (is_valid_policy_hnd(&dom_pol)) {
2807 dcerpc_samr_Close(b, state->mem_ctx, &dom_pol, &_result);
2813 set_auth_errors(state->response, result);
2815 DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
2816 ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n",
2818 state->response->data.auth.nt_status_string,
2819 state->response->data.auth.pam_error));
2821 return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
2825 static NTSTATUS extract_pac_vrfy_sigs(TALLOC_CTX *mem_ctx, DATA_BLOB pac_blob,
2826 struct PAC_LOGON_INFO **logon_info)
2828 krb5_context krbctx = NULL;
2829 krb5_error_code k5ret;
2831 krb5_kt_cursor cursor;
2832 krb5_keytab_entry entry;
2833 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2836 ZERO_STRUCT(cursor);
2838 k5ret = krb5_init_context(&krbctx);
2840 DEBUG(1, ("Failed to initialize kerberos context: %s\n",
2841 error_message(k5ret)));
2842 status = krb5_to_nt_status(k5ret);
2846 k5ret = gse_krb5_get_server_keytab(krbctx, &keytab);
2848 DEBUG(1, ("Failed to get keytab: %s\n",
2849 error_message(k5ret)));
2850 status = krb5_to_nt_status(k5ret);
2854 k5ret = krb5_kt_start_seq_get(krbctx, keytab, &cursor);
2856 DEBUG(1, ("Failed to start seq: %s\n",
2857 error_message(k5ret)));
2858 status = krb5_to_nt_status(k5ret);
2862 k5ret = krb5_kt_next_entry(krbctx, keytab, &entry, &cursor);
2863 while (k5ret == 0) {
2864 status = kerberos_pac_logon_info(mem_ctx, pac_blob,
2866 KRB5_KT_KEY(&entry), NULL, 0,
2868 if (NT_STATUS_IS_OK(status)) {
2871 k5ret = smb_krb5_kt_free_entry(krbctx, &entry);
2872 k5ret = krb5_kt_next_entry(krbctx, keytab, &entry, &cursor);
2875 k5ret = krb5_kt_end_seq_get(krbctx, keytab, &cursor);
2877 DEBUG(1, ("Failed to end seq: %s\n",
2878 error_message(k5ret)));
2881 k5ret = krb5_kt_close(krbctx, keytab);
2883 DEBUG(1, ("Failed to close keytab: %s\n",
2884 error_message(k5ret)));
2887 krb5_free_context(krbctx);
2892 NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
2893 struct netr_SamInfo3 **info3)
2895 struct winbindd_request *req = state->request;
2897 struct PAC_LOGON_INFO *logon_info = NULL;
2898 struct netr_SamInfo3 *info3_copy = NULL;
2901 pac_blob = data_blob_const(req->extra_data.data, req->extra_len);
2902 result = extract_pac_vrfy_sigs(state->mem_ctx, pac_blob, &logon_info);
2903 if (!NT_STATUS_IS_OK(result) &&
2904 !NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
2905 DEBUG(1, ("Error during PAC signature verification: %s\n",
2906 nt_errstr(result)));
2912 * Signature verification succeeded, we can
2913 * trust the PAC and prime the netsamlogon
2914 * and name2sid caches. DO NOT DO THIS
2915 * in the signature verification failed
2918 struct winbindd_domain *domain = NULL;
2920 result = create_info3_from_pac_logon_info(state->mem_ctx,
2923 if (!NT_STATUS_IS_OK(result)) {
2926 netsamlogon_cache_store(NULL, info3_copy);
2929 * We're in the parent here, so find the child
2930 * pointer from the PAC domain name.
2932 domain = find_lookup_domain_from_name(
2933 info3_copy->base.logon_domain.string);
2934 if (domain && domain->primary ) {
2935 struct dom_sid user_sid;
2937 sid_compose(&user_sid,
2938 info3_copy->base.domain_sid,
2939 info3_copy->base.rid);
2941 cache_name2sid_trusted(domain,
2942 info3_copy->base.logon_domain.string,
2943 info3_copy->base.account_name.string,
2947 DBG_INFO("PAC for user %s\\%s SID %s primed cache\n",
2948 info3_copy->base.logon_domain.string,
2949 info3_copy->base.account_name.string,
2950 sid_string_dbg(&user_sid));
2954 /* Try without signature verification */
2955 result = kerberos_pac_logon_info(state->mem_ctx, pac_blob, NULL,
2956 NULL, NULL, NULL, 0,
2958 if (!NT_STATUS_IS_OK(result)) {
2959 DEBUG(10, ("Could not extract PAC: %s\n",
2960 nt_errstr(result)));
2965 * Don't strictly need to copy here,
2966 * but it makes it explicit we're
2967 * returning a copy talloc'ed off
2968 * the state->mem_ctx.
2970 result = copy_netr_SamInfo3(state->mem_ctx,
2973 if (!NT_STATUS_IS_OK(result)) {
2979 *info3 = info3_copy;
2981 return NT_STATUS_OK;
2983 #else /* HAVE_KRB5 */
2984 NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
2985 struct netr_SamInfo3 **info3)
2987 return NT_STATUS_NO_SUCH_USER;
2989 #endif /* HAVE_KRB5 */