winbindd: prepare find_auth_domain() transitive trusts on a DC
[samba.git] / source3 / winbindd / winbindd_pam.c
index b41291e37512e1aaeb8206757a991a780c91a97f..a14b801cb9434895cfe5d2fb5e6bdc6541e2674a 100644 (file)
@@ -41,6 +41,9 @@
 #include "auth/kerberos/pac_utils.h"
 #include "auth/gensec/gensec.h"
 #include "librpc/crypto/gse_krb5.h"
+#include "lib/afs/afs_funcs.h"
+#include "libsmb/samlogon_cache.h"
+#include "rpc_client/util_netlogon.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
 
 static NTSTATUS append_info3_as_txt(TALLOC_CTX *mem_ctx,
                                    struct winbindd_response *resp,
-                                   struct netr_SamInfo3 *info3)
+                                   uint16_t validation_level,
+                                   union netr_Validation *validation)
 {
-       char *ex;
+       struct netr_SamInfo3 *info3 = NULL;
+       char *ex = NULL;
        uint32_t i;
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       status = map_validation_to_info3(frame,
+                                        validation_level,
+                                        validation,
+                                        &info3);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
 
        resp->data.auth.info3.logon_time =
                nt_time_to_unix(info3->base.logon_time);
@@ -98,34 +113,55 @@ static NTSTATUS append_info3_as_txt(TALLOC_CTX *mem_ctx,
        fstrcpy(resp->data.auth.info3.logon_dom,
                info3->base.logon_domain.string);
 
-       ex = talloc_strdup(mem_ctx, "");
-       NT_STATUS_HAVE_NO_MEMORY(ex);
+       resp->data.auth.validation_level = validation_level;
+       if (validation_level == 6) {
+               fstrcpy(resp->data.auth.info6.dns_domainname,
+                       validation->sam6->dns_domainname.string);
+               fstrcpy(resp->data.auth.info6.principal_name,
+                       validation->sam6->principal_name.string);
+       }
+
+       ex = talloc_strdup(frame, "");
+       if (ex == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
+       }
 
        for (i=0; i < info3->base.groups.count; i++) {
                ex = talloc_asprintf_append_buffer(ex, "0x%08X:0x%08X\n",
                                                   info3->base.groups.rids[i].rid,
                                                   info3->base.groups.rids[i].attributes);
-               NT_STATUS_HAVE_NO_MEMORY(ex);
+               if (ex == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto out;
+               }
        }
 
        for (i=0; i < info3->sidcount; i++) {
                char *sid;
 
-               sid = dom_sid_string(mem_ctx, info3->sids[i].sid);
-               NT_STATUS_HAVE_NO_MEMORY(sid);
+               sid = dom_sid_string(frame, info3->sids[i].sid);
+               if (sid == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto out;
+               }
 
                ex = talloc_asprintf_append_buffer(ex, "%s:0x%08X\n",
                                                   sid,
                                                   info3->sids[i].attributes);
-               NT_STATUS_HAVE_NO_MEMORY(ex);
-
-               talloc_free(sid);
+               if (ex == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto out;
+               }
        }
 
-       resp->extra_data.data = ex;
        resp->length += talloc_get_size(ex);
+       resp->extra_data.data = talloc_move(mem_ctx, &ex);
 
-       return NT_STATUS_OK;
+       status = NT_STATUS_OK;
+out:
+       TALLOC_FREE(frame);
+       return status;
 }
 
 static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx,
@@ -359,8 +395,14 @@ struct winbindd_domain *find_auth_domain(uint8_t flags,
                        DEBUG(3, ("Authentication for domain [%s] refused "
                                  "as it is not a trusted domain\n",
                                  domain_name));
+                       return NULL;
+               }
+
+               if (domain->secure_channel_type != SEC_CHAN_NULL) {
+                       return domain;
                }
-               return domain;
+
+               return domain->routing_domain;
        }
 
        if (strequal(domain_name, get_global_sam_name())) {
@@ -382,6 +424,35 @@ struct winbindd_domain *find_auth_domain(uint8_t flags,
        return find_our_domain();
 }
 
+static void fake_password_policy(struct winbindd_response *r,
+                                const struct netr_SamBaseInfo *bi)
+{
+       NTTIME min_password_age;
+       NTTIME max_password_age;
+
+       if (bi->allow_password_change > bi->last_password_change) {
+               min_password_age = bi->allow_password_change -
+                                  bi->last_password_change;
+       } else {
+               min_password_age = 0;
+       }
+
+       if (bi->force_password_change > bi->last_password_change) {
+               max_password_age = bi->force_password_change -
+                                  bi->last_password_change;
+       } else {
+               max_password_age = 0;
+       }
+
+       r->data.auth.policy.min_length_password = 0;
+       r->data.auth.policy.password_history = 0;
+       r->data.auth.policy.password_properties = 0;
+       r->data.auth.policy.expire =
+               nt_time_to_unix_abs(&max_password_age);
+       r->data.auth.policy.min_passwordage =
+               nt_time_to_unix_abs(&min_password_age);
+}
+
 static void fill_in_password_policy(struct winbindd_response *r,
                                    const struct samr_DomInfo1 *p)
 {
@@ -401,7 +472,6 @@ static NTSTATUS fillup_password_policy(struct winbindd_domain *domain,
                                       struct winbindd_response *response)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct winbindd_methods *methods;
        NTSTATUS status;
        struct samr_DomInfo1 password_policy;
 
@@ -412,9 +482,8 @@ static NTSTATUS fillup_password_policy(struct winbindd_domain *domain,
                goto done;
        }
 
-       methods = domain->methods;
-
-       status = methods->password_policy(domain, talloc_tos(), &password_policy);
+       status = wb_cache_password_policy(domain, talloc_tos(),
+                                         &password_policy);
        if (NT_STATUS_IS_ERR(status)) {
                goto done;
        }
@@ -428,17 +497,14 @@ done:
 
 static NTSTATUS get_max_bad_attempts_from_lockout_policy(struct winbindd_domain *domain,
                                                         TALLOC_CTX *mem_ctx,
-                                                        uint16 *lockout_threshold)
+                                                        uint16_t *lockout_threshold)
 {
-       struct winbindd_methods *methods;
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
        struct samr_DomInfo12 lockout_policy;
 
        *lockout_threshold = 0;
 
-       methods = domain->methods;
-
-       status = methods->lockout_policy(domain, mem_ctx, &lockout_policy);
+       status = wb_cache_lockout_policy(domain, mem_ctx, &lockout_policy);
        if (NT_STATUS_IS_ERR(status)) {
                return status;
        }
@@ -450,17 +516,14 @@ static NTSTATUS get_max_bad_attempts_from_lockout_policy(struct winbindd_domain
 
 static NTSTATUS get_pwd_properties(struct winbindd_domain *domain,
                                   TALLOC_CTX *mem_ctx,
-                                  uint32 *password_properties)
+                                  uint32_t *password_properties)
 {
-       struct winbindd_methods *methods;
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
        struct samr_DomInfo1 password_policy;
 
        *password_properties = 0;
 
-       methods = domain->methods;
-
-       status = methods->password_policy(domain, mem_ctx, &password_policy);
+       status = wb_cache_password_policy(domain, mem_ctx, &password_policy);
        if (NT_STATUS_IS_ERR(status)) {
                return status;
        }
@@ -511,7 +574,20 @@ static const char *generate_krb5_ccache(TALLOC_CTX *mem_ctx,
                                p++;
 
                                if (p != NULL && *p == 'u' && strchr(p, '%') == NULL) {
-                                       gen_cc = talloc_asprintf(mem_ctx, type, uid);
+                                       char uid_str[sizeof("18446744073709551615")];
+
+                                       snprintf(uid_str, sizeof(uid_str), "%u", uid);
+
+                                       gen_cc = talloc_string_sub2(mem_ctx,
+                                                       type,
+                                                       "%u",
+                                                       uid_str,
+                                                       /* remove_unsafe_characters */
+                                                       false,
+                                                       /* replace_once */
+                                                       true,
+                                                       /* allow_trailing_dollar */
+                                                       false);
                                }
                        }
                }
@@ -541,7 +617,7 @@ uid_t get_uid_from_request(struct winbindd_request *request)
 
        uid = request->data.auth.uid;
 
-       if (uid < 0) {
+       if (uid == (uid_t)-1) {
                DEBUG(1,("invalid uid: '%u'\n", (unsigned int)uid));
                return -1;
        }
@@ -579,7 +655,8 @@ static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
        struct PAC_DATA *pac_data = NULL;
        struct PAC_DATA_CTR *pac_data_ctr = NULL;
        const char *local_service;
-       int i;
+       uint32_t i;
+       struct netr_SamInfo3 *info3_copy = NULL;
 
        *info3 = NULL;
 
@@ -699,11 +776,20 @@ static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
                break;
        }
 
-       *info3 = &logon_info->info3;
+       if (logon_info == NULL) {
+               DEBUG(10,("Missing logon_info in ticket of %s\n",
+                       principal_s));
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
        DEBUG(10,("winbindd_raw_kerberos_login: winbindd validated ticket of %s\n",
                principal_s));
 
+       result = create_info3_from_pac_logon_info(mem_ctx, logon_info, &info3_copy);
+       if (!NT_STATUS_IS_OK(result)) {
+               goto failed;
+       }
+
        /* if we had a user's ccache then return that string for the pam
         * environment */
 
@@ -739,7 +825,7 @@ static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
                }
 
        }
-
+       *info3 = info3_copy;
        return NT_STATUS_OK;
 
 failed:
@@ -804,11 +890,21 @@ bool check_request_flags(uint32_t flags)
 NTSTATUS append_auth_data(TALLOC_CTX *mem_ctx,
                          struct winbindd_response *resp,
                          uint32_t request_flags,
-                         struct netr_SamInfo3 *info3,
+                         uint16_t validation_level,
+                         union netr_Validation *validation,
                          const char *name_domain,
                          const char *name_user)
 {
-       NTSTATUS result;
+       struct netr_SamInfo3 *info3 = NULL;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+
+       result = map_validation_to_info3(talloc_tos(),
+                                        validation_level,
+                                        validation,
+                                        &info3);
+       if (!NT_STATUS_IS_OK(result)) {
+               goto out;
+       }
 
        if (request_flags & WBFLAG_PAM_USER_SESSION_KEY) {
                memcpy(resp->data.auth.user_session_key,
@@ -830,7 +926,7 @@ NTSTATUS append_auth_data(TALLOC_CTX *mem_ctx,
                if (!NT_STATUS_IS_OK(result)) {
                        DEBUG(10,("Failed to append Unix Username: %s\n",
                                nt_errstr(result)));
-                       return result;
+                       goto out;
                }
        }
 
@@ -841,16 +937,18 @@ NTSTATUS append_auth_data(TALLOC_CTX *mem_ctx,
                if (!NT_STATUS_IS_OK(result)) {
                        DEBUG(10,("Failed to append INFO3 (NDR): %s\n",
                                nt_errstr(result)));
-                       return result;
+                       goto out;
                }
        }
 
        if (request_flags & WBFLAG_PAM_INFO3_TEXT) {
-               result = append_info3_as_txt(mem_ctx, resp, info3);
+               result = append_info3_as_txt(mem_ctx, resp,
+                                            validation_level,
+                                            validation);
                if (!NT_STATUS_IS_OK(result)) {
                        DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
                                nt_errstr(result)));
-                       return result;
+                       goto out;
                }
        }
 
@@ -860,11 +958,14 @@ NTSTATUS append_auth_data(TALLOC_CTX *mem_ctx,
                if (!NT_STATUS_IS_OK(result)) {
                        DEBUG(10,("Failed to append AFS token: %s\n",
                                nt_errstr(result)));
-                       return result;
+                       goto out;
                }
        }
 
-       return NT_STATUS_OK;
+       result = NT_STATUS_OK;
+out:
+       TALLOC_FREE(info3);
+       return result;
 }
 
 static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
@@ -872,13 +973,13 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
                                              struct netr_SamInfo3 **info3)
 {
        NTSTATUS result = NT_STATUS_LOGON_FAILURE;
-       uint16 max_allowed_bad_attempts;
+       uint16_t max_allowed_bad_attempts;
        fstring name_domain, name_user;
        struct dom_sid sid;
        enum lsa_SidType type;
        uchar new_nt_pass[NT_HASH_LEN];
-       const uint8 *cached_nt_pass;
-       const uint8 *cached_salt;
+       const uint8_t *cached_nt_pass;
+       const uint8_t *cached_salt;
        struct netr_SamInfo3 *my_info3;
        time_t kickoff_time, must_change_time;
        bool password_good = false;
@@ -994,7 +1095,7 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
 #ifdef HAVE_KRB5
                if ((state->request->flags & WBFLAG_PAM_KRB5) &&
                    ((tdc_domain = wcache_tdc_fetch_domain(state->mem_ctx, name_domain)) != NULL) &&
-                   ((tdc_domain->trust_type & NETR_TRUST_TYPE_UPLEVEL) ||
+                   ((tdc_domain->trust_type & LSA_TRUST_TYPE_UPLEVEL) ||
                    /* used to cope with the case winbindd starting without network. */
                    !strequal(tdc_domain->domain_name, tdc_domain->dns_name))) {
 
@@ -1110,7 +1211,7 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
        /* lockout user */
        if (my_info3->base.bad_password_count >= max_allowed_bad_attempts) {
 
-               uint32 password_properties;
+               uint32_t password_properties;
 
                result = get_pwd_properties(domain, state->mem_ctx, &password_properties);
                if (!NT_STATUS_IS_OK(result)) {
@@ -1184,7 +1285,7 @@ static NTSTATUS winbindd_dual_pam_auth_kerberos(struct winbindd_domain *domain,
        }
 
        if (!contact_domain->initialized) {
-               init_dc_connection(contact_domain);
+               init_dc_connection(contact_domain, false);
        }
 
        if (!contact_domain->active_directory) {
@@ -1209,68 +1310,152 @@ static NTSTATUS winbindd_dual_auth_passdb(TALLOC_CTX *mem_ctx,
                                          const DATA_BLOB *challenge,
                                          const DATA_BLOB *lm_resp,
                                          const DATA_BLOB *nt_resp,
+                                         bool interactive,
+                                         uint8_t *pauthoritative,
                                          struct netr_SamInfo3 **pinfo3)
 {
+       struct auth_context *auth_context;
+       struct auth_serversupplied_info *server_info;
        struct auth_usersupplied_info *user_info = NULL;
        struct tsocket_address *local;
+       struct netr_SamInfo3 *info3;
        NTSTATUS status;
+       bool ok;
        int rc;
+       TALLOC_CTX *frame = talloc_stackframe();
 
-       rc = tsocket_address_inet_from_strings(mem_ctx,
+       /*
+        * We are authoritative by default
+        */
+       *pauthoritative = 1;
+
+       rc = tsocket_address_inet_from_strings(frame,
                                               "ip",
                                               "127.0.0.1",
                                               0,
                                               &local);
        if (rc < 0) {
+               TALLOC_FREE(frame);
                return NT_STATUS_NO_MEMORY;
        }
-       status = make_user_info(&user_info, user, user, domain, domain,
-                               lp_netbios_name(), local, lm_resp, nt_resp, NULL, NULL,
+
+       /*
+        * TODO: We should get the service description passed in from
+        * the winbind client, so we can have "smb2", "squid" or "samr" logged
+        * here.
+        */
+       status = make_user_info(frame, &user_info, user, user, domain, domain,
+                               lp_netbios_name(), local, local,
+                               "winbind",
+                               lm_resp, nt_resp, NULL, NULL,
                                NULL, AUTH_PASSWORD_RESPONSE);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(10, ("make_user_info failed: %s\n", nt_errstr(status)));
+               TALLOC_FREE(frame);
                return status;
        }
+
        user_info->logon_parameters = logon_parameters;
 
        /* We don't want any more mapping of the username */
        user_info->mapped_state = True;
 
-       status = check_sam_security_info3(challenge, talloc_tos(), user_info,
-                                         pinfo3);
-       free_user_info(&user_info);
+       /* We don't want to come back to winbindd or to do PAM account checks */
+       user_info->flags |= USER_INFO_INFO3_AND_NO_AUTHZ;
+
+       if (interactive) {
+               user_info->flags |= USER_INFO_INTERACTIVE_LOGON;
+       }
+
+       status = make_auth3_context_for_winbind(frame, &auth_context);
+       if (!NT_STATUS_IS_OK(status)) {
+               DBG_ERR("make_auth3_context_for_winbind failed: %s\n",
+                       nt_errstr(status));
+               TALLOC_FREE(frame);
+               return status;
+       }
+
+       ok = auth3_context_set_challenge(auth_context,
+                                        challenge->data, "fixed");
+       if (!ok) {
+               TALLOC_FREE(frame);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       status = auth_check_ntlm_password(mem_ctx,
+                                         auth_context,
+                                         user_info,
+                                         &server_info,
+                                         pauthoritative);
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(frame);
+               return status;
+       }
+
+       info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
+       if (info3 == NULL) {
+               TALLOC_FREE(frame);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       status = serverinfo_to_SamInfo3(server_info, info3);
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(frame);
+               TALLOC_FREE(info3);
+               DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
+                         nt_errstr(status)));
+               return status;
+       }
+
+       *pinfo3 = info3;
        DEBUG(10, ("Authenticaticating user %s\\%s returned %s\n", domain,
                   user, nt_errstr(status)));
+       TALLOC_FREE(frame);
        return status;
 }
 
 static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
                                            TALLOC_CTX *mem_ctx,
                                            uint32_t logon_parameters,
-                                           const char *server,
                                            const char *username,
+                                           const char *password,
                                            const char *domainname,
                                            const char *workstation,
                                            const uint8_t chal[8],
                                            DATA_BLOB lm_response,
                                            DATA_BLOB nt_response,
-                                           struct netr_SamInfo3 **info3)
+                                           bool interactive,
+                                           uint8_t *authoritative,
+                                           uint32_t *flags,
+                                           uint16_t *_validation_level,
+                                           union netr_Validation **_validation)
 {
        int attempts = 0;
        int netr_attempts = 0;
        bool retry = false;
        NTSTATUS result;
+       enum netr_LogonInfoClass logon_type_i;
+       enum netr_LogonInfoClass logon_type_n;
+       uint16_t validation_level = UINT16_MAX;
+       union netr_Validation *validation = NULL;
 
        do {
                struct rpc_pipe_client *netlogon_pipe;
-               uint8_t authoritative = 0;
-               uint32_t flags = 0;
 
-               ZERO_STRUCTP(info3);
                retry = false;
 
                result = cm_connect_netlogon(domain, &netlogon_pipe);
 
+               if (NT_STATUS_EQUAL(result,
+                                   NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
+                       /*
+                        * This means we don't have a trust account.
+                        */
+                       *authoritative = 0;
+                       result = NT_STATUS_NO_SUCH_USER;
+                       break;
+               }
+
                if (!NT_STATUS_IS_OK(result)) {
                        DEBUG(3,("Could not open handle to NETLOGON pipe "
                                 "(error: %s, attempts: %d)\n",
@@ -1281,7 +1466,7 @@ static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
                                DEBUG(3, ("This is again a problem for this "
                                          "particular call, forcing the close "
                                          "of this connection\n"));
-                               invalidate_cm_connection(&domain->conn);
+                               invalidate_cm_connection(domain);
                        }
 
                        /* After the second retry failover to the next DC */
@@ -1294,7 +1479,7 @@ static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
                                 */
                                DEBUG(3, ("This is the third problem for this "
                                          "particular call, adding DC to the "
-                                         "negative cache list\n"));
+                                         "negative cache list: %s %s\n", domain->name, domain->dcname));
                                add_failed_connection_entry(domain->name,
                                                            domain->dcname,
                                                            result);
@@ -1311,21 +1496,67 @@ static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
                        }
                        return result;
                }
-               netr_attempts = 0;
 
-               result = rpccli_netlogon_network_logon(domain->conn.netlogon_creds,
-                                               netlogon_pipe->binding_handle,
-                                               mem_ctx,
-                                               logon_parameters,
-                                               username,
-                                               domainname,
-                                               workstation,
-                                               chal,
-                                               lm_response,
-                                               nt_response,
-                                               &authoritative,
-                                               &flags,
-                                               info3);
+               logon_type_i = NetlogonInteractiveInformation;
+               logon_type_n = NetlogonNetworkInformation;
+               if (domain->domain_trust_attribs & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST) {
+                       logon_type_i = NetlogonInteractiveTransitiveInformation;
+                       logon_type_n = NetlogonNetworkTransitiveInformation;
+               }
+
+               if (domain->domain_trust_attribs & LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
+                       logon_type_i = NetlogonInteractiveTransitiveInformation;
+                       logon_type_n = NetlogonNetworkTransitiveInformation;
+               }
+
+               if (domain->domain_trust_attribs & LSA_TRUST_ATTRIBUTE_NON_TRANSITIVE) {
+                       logon_type_i = NetlogonInteractiveInformation;
+                       logon_type_n = NetlogonNetworkInformation;
+               }
+
+               if (domain->domain_trust_attribs & LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN) {
+                       logon_type_i = NetlogonInteractiveInformation;
+                       logon_type_n = NetlogonNetworkInformation;
+               }
+
+               netr_attempts = 0;
+               if (domain->conn.netlogon_creds_ctx == NULL) {
+                       DBG_NOTICE("No security credentials available for "
+                                 "domain [%s]\n", domainname);
+                       result = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
+               } else if (interactive) {
+                       result = rpccli_netlogon_password_logon(
+                               domain->conn.netlogon_creds_ctx,
+                               netlogon_pipe->binding_handle,
+                               mem_ctx,
+                               logon_parameters,
+                               domainname,
+                               username,
+                               password,
+                               workstation,
+                               logon_type_i,
+                               authoritative,
+                               flags,
+                               &validation_level,
+                               &validation);
+               } else {
+                       result = rpccli_netlogon_network_logon(
+                               domain->conn.netlogon_creds_ctx,
+                               netlogon_pipe->binding_handle,
+                               mem_ctx,
+                               logon_parameters,
+                               username,
+                               domainname,
+                               workstation,
+                               chal,
+                               lm_response,
+                               nt_response,
+                               logon_type_n,
+                               authoritative,
+                               flags,
+                               &validation_level,
+                               &validation);
+               }
 
                /*
                 * we increment this after the "feature negotiation"
@@ -1348,12 +1579,13 @@ static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
                   rpc changetrustpw' */
 
                if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
-                       DEBUG(3,("winbind_samlogon_retry_loop: sam_logon returned "
-                                "ACCESS_DENIED.  Maybe the trust account "
+                       DEBUG(1,("winbind_samlogon_retry_loop: sam_logon returned "
+                                "ACCESS_DENIED.  Maybe the DC has Restrict "
+                                "NTLM set or the trust account "
                                "password was changed and we didn't know it. "
                                 "Killing connections to domain %s\n",
                                domainname));
-                       invalidate_cm_connection(&domain->conn);
+                       invalidate_cm_connection(domain);
                        retry = true;
                }
 
@@ -1376,7 +1608,7 @@ static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
                         * In order to recover from this situation, we need to
                         * drop the connection.
                         */
-                       invalidate_cm_connection(&domain->conn);
+                       invalidate_cm_connection(domain);
                        result = NT_STATUS_LOGON_FAILURE;
                        break;
                }
@@ -1385,20 +1617,29 @@ static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
 
        if (NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT)) {
                DEBUG(3,("winbind_samlogon_retry_loop: sam_network_logon(ex) "
-                               "returned NT_STATUS_IO_TIMEOUT after the retry."
+                               "returned NT_STATUS_IO_TIMEOUT after the retry. "
                                "Killing connections to domain %s\n",
                        domainname));
-               invalidate_cm_connection(&domain->conn);
+               invalidate_cm_connection(domain);
        }
-       return result;
+
+       if (!NT_STATUS_IS_OK(result)) {
+               return result;
+       }
+
+       *_validation_level = validation_level;
+       *_validation = validation;
+       return NT_STATUS_OK;
 }
 
-static NTSTATUS winbindd_dual_pam_auth_samlogon(TALLOC_CTX *mem_ctx,
-                                               struct winbindd_domain *domain,
-                                               const char *user,
-                                               const char *pass,
-                                               uint32_t request_flags,
-                                               struct netr_SamInfo3 **info3)
+static NTSTATUS winbindd_dual_pam_auth_samlogon(
+       TALLOC_CTX *mem_ctx,
+       struct winbindd_domain *domain,
+       const char *user,
+       const char *pass,
+       uint32_t request_flags,
+       uint16_t *_validation_level,
+       union netr_Validation **_validation)
 {
 
        uchar chal[8];
@@ -1407,9 +1648,11 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(TALLOC_CTX *mem_ctx,
        unsigned char local_nt_response[24];
        fstring name_domain, name_user;
        NTSTATUS result;
-       struct netr_SamInfo3 *my_info3 = NULL;
-
-       *info3 = NULL;
+       uint8_t authoritative = 0;
+       uint32_t flags = 0;
+       uint16_t validation_level;
+       union netr_Validation *validation = NULL;
+       struct netr_SamBaseInfo *base_info = NULL;
 
        DEBUG(10,("winbindd_dual_pam_auth_samlogon\n"));
 
@@ -1417,48 +1660,80 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(TALLOC_CTX *mem_ctx,
 
        parse_domain_user(user, name_domain, name_user);
 
-       /* do password magic */
+       /*
+        * We check against domain->name instead of
+        * name_domain, as find_auth_domain() ->
+        * find_domain_from_name_noinit() already decided
+        * that we are in a child for the correct domain.
+        *
+        * name_domain can also be lp_realm()
+        * we need to check against domain->name.
+        */
+       if (strequal(domain->name, get_global_sam_name())) {
+               DATA_BLOB chal_blob = data_blob_const(chal, sizeof(chal));
+               struct netr_SamInfo3 *info3 = NULL;
+
+               /* do password magic */
+
+               generate_random_buffer(chal, sizeof(chal));
+
+               if (lp_client_ntlmv2_auth()) {
+                       DATA_BLOB server_chal;
+                       DATA_BLOB names_blob;
+                       server_chal = data_blob_const(chal, 8);
+
+                       /* note that the 'workgroup' here is for the local
+                          machine.  The 'server name' must match the
+                          'workstation' passed to the actual SamLogon call.
+                       */
+                       names_blob = NTLMv2_generate_names_blob(
+                               mem_ctx, lp_netbios_name(), lp_workgroup());
+
+                       if (!SMBNTLMv2encrypt(mem_ctx, name_user, name_domain,
+                                             pass,
+                                             &server_chal,
+                                             &names_blob,
+                                             &lm_resp, &nt_resp, NULL, NULL)) {
+                               data_blob_free(&names_blob);
+                               DEBUG(0, ("winbindd_pam_auth: SMBNTLMv2encrypt() failed!\n"));
+                               result = NT_STATUS_NO_MEMORY;
+                               goto done;
+                       }
+                       data_blob_free(&names_blob);
+               } else {
+                       lm_resp = data_blob_null;
+                       SMBNTencrypt(pass, chal, local_nt_response);
 
-       generate_random_buffer(chal, sizeof(chal));
+                       nt_resp = data_blob_talloc(mem_ctx, local_nt_response,
+                                                  sizeof(local_nt_response));
+               }
 
-       if (lp_client_ntlmv2_auth()) {
-               DATA_BLOB server_chal;
-               DATA_BLOB names_blob;
-               server_chal = data_blob_const(chal, 8);
+               result = winbindd_dual_auth_passdb(
+                       talloc_tos(), 0, name_domain, name_user,
+                       &chal_blob, &lm_resp, &nt_resp,
+                       true, /* interactive */
+                       &authoritative,
+                       &info3);
 
-               /* note that the 'workgroup' here is for the local
-                  machine.  The 'server name' must match the
-                  'workstation' passed to the actual SamLogon call.
-               */
-               names_blob = NTLMv2_generate_names_blob(
-                       mem_ctx, lp_netbios_name(), lp_workgroup());
+               /*
+                * We need to try the remote NETLOGON server if this is
+                * not authoritative (for example on the RODC).
+                */
+               if (authoritative != 0) {
+                       if (NT_STATUS_IS_OK(result)) {
+                               result = map_info3_to_validation(
+                                               mem_ctx,
+                                               info3,
+                                               &validation_level,
+                                               &validation);
+                               TALLOC_FREE(info3);
+                               if (!NT_STATUS_IS_OK(result)) {
+                                       goto done;
+                               }
+                       }
 
-               if (!SMBNTLMv2encrypt(mem_ctx, name_user, name_domain,
-                                     pass,
-                                     &server_chal,
-                                     &names_blob,
-                                     &lm_resp, &nt_resp, NULL, NULL)) {
-                       data_blob_free(&names_blob);
-                       DEBUG(0, ("winbindd_pam_auth: SMBNTLMv2encrypt() failed!\n"));
-                       result = NT_STATUS_NO_MEMORY;
                        goto done;
                }
-               data_blob_free(&names_blob);
-       } else {
-               lm_resp = data_blob_null;
-               SMBNTencrypt(pass, chal, local_nt_response);
-
-               nt_resp = data_blob_talloc(mem_ctx, local_nt_response,
-                                          sizeof(local_nt_response));
-       }
-
-       if (strequal(name_domain, get_global_sam_name())) {
-               DATA_BLOB chal_blob = data_blob_const(chal, sizeof(chal));
-
-               result = winbindd_dual_auth_passdb(
-                       mem_ctx, 0, name_domain, name_user,
-                       &chal_blob, &lm_resp, &nt_resp, info3);
-               goto done;
        }
 
        /* check authentication loop */
@@ -1466,33 +1741,48 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(TALLOC_CTX *mem_ctx,
        result = winbind_samlogon_retry_loop(domain,
                                             mem_ctx,
                                             0,
-                                            domain->dcname,
                                             name_user,
+                                            pass,
                                             name_domain,
                                             lp_netbios_name(),
-                                            chal,
-                                            lm_resp,
-                                            nt_resp,
-                                            &my_info3);
+                                            NULL,
+                                            data_blob_null, data_blob_null,
+                                            true, /* interactive */
+                                            &authoritative,
+                                            &flags,
+                                            &validation_level,
+                                            &validation);
        if (!NT_STATUS_IS_OK(result)) {
                goto done;
        }
 
        /* handle the case where a NT4 DC does not fill in the acct_flags in
         * the samlogon reply info3. When accurate info3 is required by the
-        * caller, we look up the account flags ourselve - gd */
+        * caller, we look up the account flags ourselves - gd */
 
+       switch (validation_level) {
+       case 3:
+               base_info = &validation->sam3->base;
+               break;
+       case 6:
+               base_info = &validation->sam6->base;
+               break;
+       default:
+               DBG_ERR("Bad validation level %d", (int)validation_level);
+               result = NT_STATUS_INTERNAL_ERROR;
+               goto done;
+       }
        if ((request_flags & WBFLAG_PAM_INFO3_TEXT) &&
-           NT_STATUS_IS_OK(result) && (my_info3->base.acct_flags == 0)) {
-
+           (base_info->acct_flags == 0))
+       {
                struct rpc_pipe_client *samr_pipe;
                struct policy_handle samr_domain_handle, user_pol;
                union samr_UserInfo *info = NULL;
                NTSTATUS status_tmp, result_tmp;
-               uint32 acct_flags;
+               uint32_t acct_flags;
                struct dcerpc_binding_handle *b;
 
-               status_tmp = cm_connect_sam(domain, mem_ctx,
+               status_tmp = cm_connect_sam(domain, mem_ctx, false,
                                            &samr_pipe, &samr_domain_handle);
 
                if (!NT_STATUS_IS_OK(status_tmp)) {
@@ -1506,7 +1796,7 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(TALLOC_CTX *mem_ctx,
                status_tmp = dcerpc_samr_OpenUser(b, mem_ctx,
                                                  &samr_domain_handle,
                                                  MAXIMUM_ALLOWED_ACCESS,
-                                                 my_info3->base.rid,
+                                                 base_info->rid,
                                                  &user_pol,
                                                  &result_tmp);
 
@@ -1527,18 +1817,13 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(TALLOC_CTX *mem_ctx,
                                                       &info,
                                                       &result_tmp);
 
-               if (!NT_STATUS_IS_OK(status_tmp)) {
+               if (any_nt_status_not_ok(status_tmp, result_tmp,
+                                        &status_tmp)) {
                        DEBUG(3, ("could not query user info on SAMR pipe: %s\n",
                                nt_errstr(status_tmp)));
                        dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
                        goto done;
                }
-               if (!NT_STATUS_IS_OK(result_tmp)) {
-                       DEBUG(3, ("could not query user info on SAMR pipe: %s\n",
-                               nt_errstr(result_tmp)));
-                       dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
-                       goto done;
-               }
 
                acct_flags = info->info16.acct_flags;
 
@@ -1547,15 +1832,18 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(TALLOC_CTX *mem_ctx,
                        goto done;
                }
 
-               my_info3->base.acct_flags = acct_flags;
+               base_info->acct_flags = acct_flags;
 
                DEBUG(10,("successfully retrieved acct_flags 0x%x\n", acct_flags));
 
                dcerpc_samr_Close(b, mem_ctx, &user_pol, &result_tmp);
        }
 
-       *info3 = my_info3;
 done:
+       if (NT_STATUS_IS_OK(result)) {
+               *_validation_level = validation_level;
+               *_validation = validation;
+       }
        return result;
 }
 
@@ -1567,7 +1855,8 @@ enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
        fstring name_domain, name_user;
        char *mapped_user;
        fstring domain_user;
-       struct netr_SamInfo3 *info3 = NULL;
+       uint16_t validation_level = UINT16_MAX;
+       union netr_Validation *validation = NULL;
        NTSTATUS name_map_status = NT_STATUS_UNSUCCESSFUL;
 
        /* Ensure null termination */
@@ -1615,7 +1904,7 @@ enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
                                "request in startup mode.\n", domain->name ));
 
                        winbindd_flush_negative_conn_cache(domain);
-                       result = init_dc_connection(domain);
+                       result = init_dc_connection(domain, false);
                }
        }
 
@@ -1623,6 +1912,7 @@ enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
 
        /* Check for Kerberos authentication */
        if (domain->online && (state->request->flags & WBFLAG_PAM_KRB5)) {
+               struct netr_SamInfo3 *info3 = NULL;
 
                result = winbindd_dual_pam_auth_kerberos(domain, state, &info3);
                /* save for later */
@@ -1631,11 +1921,22 @@ enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
 
                if (NT_STATUS_IS_OK(result)) {
                        DEBUG(10,("winbindd_dual_pam_auth_kerberos succeeded\n"));
+
+                       result = map_info3_to_validation(state->mem_ctx,
+                                                        info3,
+                                                        &validation_level,
+                                                        &validation);
+                       TALLOC_FREE(info3);
+                       if (!NT_STATUS_IS_OK(result)) {
+                               DBG_ERR("map_info3_to_validation failed\n");
+                               goto done;
+                       }
                        goto process_result;
-               } else {
-                       DEBUG(10,("winbindd_dual_pam_auth_kerberos failed: %s\n", nt_errstr(result)));
                }
 
+               DBG_DEBUG("winbindd_dual_pam_auth_kerberos failed: %s\n",
+                         nt_errstr(result));
+
                if (NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS) ||
                    NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT) ||
                    NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
@@ -1672,19 +1973,38 @@ enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
 sam_logon:
        /* Check for Samlogon authentication */
        if (domain->online) {
+               struct netr_SamBaseInfo *base_info = NULL;
+
                result = winbindd_dual_pam_auth_samlogon(
                        state->mem_ctx, domain,
                        state->request->data.auth.user,
                        state->request->data.auth.pass,
                        state->request->flags,
-                       &info3);
+                       &validation_level,
+                       &validation);
 
                if (NT_STATUS_IS_OK(result)) {
                        DEBUG(10,("winbindd_dual_pam_auth_samlogon succeeded\n"));
+
+                       switch (validation_level) {
+                       case 3:
+                               base_info = &validation->sam3->base;
+                               break;
+                       case 6:
+                               base_info = &validation->sam6->base;
+                               break;
+                       default:
+                               DBG_ERR("Bad validation level %d\n",
+                                       validation_level);
+                               result = NT_STATUS_INTERNAL_ERROR;
+                               goto done;
+                       }
+
                        /* add the Krb5 err if we have one */
                        if ( NT_STATUS_EQUAL(krb5_result, NT_STATUS_TIME_DIFFERENCE_AT_DC ) ) {
-                               info3->base.user_flags |= LOGON_KRB5_FAIL_CLOCK_SKEW;
+                               base_info->user_flags |= LOGON_KRB5_FAIL_CLOCK_SKEW;
                        }
+
                        goto process_result;
                }
 
@@ -1700,43 +2020,92 @@ sam_logon:
                        goto cached_logon;
                }
 
-                       if (domain->online) {
-                               /* We're still online - fail. */
-                               goto done;
-                       }
+               if (domain->online) {
+                       /* We're still online - fail. */
+                       goto done;
+               }
        }
 
 cached_logon:
        /* Check for Cached logons */
        if (!domain->online && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN) &&
            lp_winbind_offline_logon()) {
+               struct netr_SamInfo3 *info3 = NULL;
 
                result = winbindd_dual_pam_auth_cached(domain, state, &info3);
 
-               if (NT_STATUS_IS_OK(result)) {
-                       DEBUG(10,("winbindd_dual_pam_auth_cached succeeded\n"));
-                       goto process_result;
-               } else {
+               if (!NT_STATUS_IS_OK(result)) {
                        DEBUG(10,("winbindd_dual_pam_auth_cached failed: %s\n", nt_errstr(result)));
                        goto done;
                }
+               DEBUG(10,("winbindd_dual_pam_auth_cached succeeded\n"));
+
+               result = map_info3_to_validation(state->mem_ctx,
+                                                info3,
+                                                &validation_level,
+                                                &validation);
+               TALLOC_FREE(info3);
+               if (!NT_STATUS_IS_OK(result)) {
+                       DBG_ERR("map_info3_to_validation failed\n");
+                       goto done;
+               }
        }
 
 process_result:
 
        if (NT_STATUS_IS_OK(result)) {
-
                struct dom_sid user_sid;
-
-               /* In all codepaths where result == NT_STATUS_OK info3 must have
-                  been initialized. */
-               if (!info3) {
+               TALLOC_CTX *base_ctx = NULL;
+               struct netr_SamBaseInfo *base_info = NULL;
+               struct netr_SamInfo3 *info3 = NULL;
+
+               switch (validation_level) {
+               case 3:
+                       base_ctx = validation->sam3;
+                       base_info = &validation->sam3->base;
+                       break;
+               case 6:
+                       base_ctx = validation->sam6;
+                       base_info = &validation->sam6->base;
+                       break;
+               default:
                        result = NT_STATUS_INTERNAL_ERROR;
                        goto done;
                }
 
-               sid_compose(&user_sid, info3->base.domain_sid,
-                           info3->base.rid);
+               sid_compose(&user_sid, base_info->domain_sid, base_info->rid);
+
+               if (base_info->full_name.string == NULL) {
+                       struct netr_SamInfo3 *cached_info3;
+
+                       cached_info3 = netsamlogon_cache_get(state->mem_ctx,
+                                                            &user_sid);
+                       if (cached_info3 != NULL &&
+                           cached_info3->base.full_name.string != NULL) {
+                               base_info->full_name.string = talloc_strdup(
+                                       base_ctx,
+                                       cached_info3->base.full_name.string);
+                               if (base_info->full_name.string == NULL) {
+                                       result = NT_STATUS_NO_MEMORY;
+                                       goto done;
+                               }
+                       } else {
+
+                               /* this might fail so we don't check the return code */
+                               wcache_query_user_fullname(domain,
+                                               base_ctx,
+                                               &user_sid,
+                                               &base_info->full_name.string);
+                       }
+               }
+
+               result = map_validation_to_info3(talloc_tos(),
+                                                validation_level,
+                                                validation,
+                                                &info3);
+               if (!NT_STATUS_IS_OK(result)) {
+                       goto done;
+               }
 
                wcache_invalidate_samlogon(find_domain_from_name(name_domain),
                                           &user_sid);
@@ -1764,7 +2133,9 @@ process_result:
                }
 
                result = append_auth_data(state->mem_ctx, state->response,
-                                         state->request->flags, info3,
+                                         state->request->flags,
+                                         validation_level,
+                                         validation,
                                          name_domain, name_user);
                if (!NT_STATUS_IS_OK(result)) {
                        goto done;
@@ -1780,27 +2151,14 @@ process_result:
                }
 
                if (state->request->flags & WBFLAG_PAM_GET_PWD_POLICY) {
-                       struct winbindd_domain *our_domain = find_our_domain();
-
-                       /* This is not entirely correct I believe, but it is
-                          consistent.  Only apply the password policy settings
-                          too warn users for our own domain.  Cannot obtain these
-                          from trusted DCs all the  time so don't do it at all.
-                          -- jerry */
-
-                       result = NT_STATUS_NOT_SUPPORTED;
-                       if (our_domain == domain ) {
-                               result = fillup_password_policy(
-                                       our_domain, state->response);
-                       }
-
-                       if (!NT_STATUS_IS_OK(result)
-                           && !NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED) )
-                       {
-                               DEBUG(10,("Failed to get password policies for domain %s: %s\n",
-                                         domain->name, nt_errstr(result)));
-                               goto done;
-                       }
+                       /*
+                        * WBFLAG_PAM_GET_PWD_POLICY is not used within
+                        * any Samba caller anymore.
+                        *
+                        * We just fake this based on the effective values
+                        * for the user, for legacy callers.
+                        */
+                       fake_password_policy(state->response, &info3->base);
                }
 
                result = NT_STATUS_OK;
@@ -1823,15 +2181,180 @@ done:
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
 
+NTSTATUS winbind_dual_SamLogon(struct winbindd_domain *domain,
+                              TALLOC_CTX *mem_ctx,
+                              uint32_t logon_parameters,
+                              const char *name_user,
+                              const char *name_domain,
+                              const char *workstation,
+                              const uint8_t chal[8],
+                              DATA_BLOB lm_response,
+                              DATA_BLOB nt_response,
+                              uint8_t *authoritative,
+                              bool skip_sam,
+                              uint32_t *flags,
+                              uint16_t *_validation_level,
+                              union netr_Validation **_validation)
+{
+       uint16_t validation_level;
+       union netr_Validation *validation = NULL;
+       NTSTATUS result;
+
+       /*
+        * We check against domain->name instead of
+        * name_domain, as find_auth_domain() ->
+        * find_domain_from_name_noinit() already decided
+        * that we are in a child for the correct domain.
+        *
+        * name_domain can also be lp_realm()
+        * we need to check against domain->name.
+        */
+       if (!skip_sam && strequal(domain->name, get_global_sam_name())) {
+               DATA_BLOB chal_blob = data_blob_const(
+                       chal, 8);
+               struct netr_SamInfo3 *info3 = NULL;
+
+               result = winbindd_dual_auth_passdb(
+                       talloc_tos(),
+                       logon_parameters,
+                       name_domain, name_user,
+                       &chal_blob, &lm_response, &nt_response,
+                       false, /* interactive */
+                       authoritative,
+                       &info3);
+               if (NT_STATUS_IS_OK(result)) {
+                       result = map_info3_to_validation(mem_ctx,
+                                                        info3,
+                                                        &validation_level,
+                                                        &validation);
+                       TALLOC_FREE(info3);
+                       if (!NT_STATUS_IS_OK(result)) {
+                               goto done;
+                       }
+               }
+
+               /*
+                * We need to try the remote NETLOGON server if this is
+                * not authoritative.
+                */
+               if (*authoritative != 0) {
+                       *flags = 0;
+                       goto process_result;
+               }
+       }
+
+       result = winbind_samlogon_retry_loop(domain,
+                                            mem_ctx,
+                                            logon_parameters,
+                                            name_user,
+                                            NULL, /* password */
+                                            name_domain,
+                                            /* Bug #3248 - found by Stefan Burkei. */
+                                            workstation, /* We carefully set this above so use it... */
+                                            chal,
+                                            lm_response,
+                                            nt_response,
+                                            false, /* interactive */
+                                            authoritative,
+                                            flags,
+                                            &validation_level,
+                                            &validation);
+       if (!NT_STATUS_IS_OK(result)) {
+               goto done;
+       }
+
+process_result:
+
+       if (NT_STATUS_IS_OK(result)) {
+               struct dom_sid user_sid;
+               TALLOC_CTX *base_ctx = NULL;
+               struct netr_SamBaseInfo *base_info = NULL;
+               struct netr_SamInfo3 *info3 = NULL;
+
+               switch (validation_level) {
+               case 3:
+                       base_ctx = validation->sam3;
+                       base_info = &validation->sam3->base;
+                       break;
+               case 6:
+                       base_ctx = validation->sam6;
+                       base_info = &validation->sam6->base;
+                       break;
+               default:
+                       result = NT_STATUS_INTERNAL_ERROR;
+                       goto done;
+               }
+
+               sid_compose(&user_sid, base_info->domain_sid, base_info->rid);
+
+               if (base_info->full_name.string == NULL) {
+                       struct netr_SamInfo3 *cached_info3;
+
+                       cached_info3 = netsamlogon_cache_get(mem_ctx,
+                                                            &user_sid);
+                       if (cached_info3 != NULL &&
+                           cached_info3->base.full_name.string != NULL)
+                       {
+                               base_info->full_name.string = talloc_strdup(
+                                       base_ctx,
+                                       cached_info3->base.full_name.string);
+                       } else {
+
+                               /* this might fail so we don't check the return code */
+                               wcache_query_user_fullname(domain,
+                                               base_ctx,
+                                               &user_sid,
+                                               &base_info->full_name.string);
+                       }
+               }
+
+               result = map_validation_to_info3(talloc_tos(),
+                                                validation_level,
+                                                validation,
+                                                &info3);
+               if (!NT_STATUS_IS_OK(result)) {
+                       goto done;
+               }
+               wcache_invalidate_samlogon(find_domain_from_name(name_domain),
+                                          &user_sid);
+               netsamlogon_cache_store(name_user, info3);
+               TALLOC_FREE(info3);
+       }
+
+done:
+
+       /* give us a more useful (more correct?) error code */
+       if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
+           (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
+               result = NT_STATUS_NO_LOGON_SERVERS;
+       }
+
+       DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
+             ("NTLM CRAP authentication for user [%s]\\[%s] returned %s\n",
+              name_domain,
+              name_user,
+              nt_errstr(result)));
+
+       if (!NT_STATUS_IS_OK(result)) {
+               return result;
+       }
+
+       *_validation_level = validation_level;
+       *_validation = validation;
+       return NT_STATUS_OK;
+}
+
 enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
                                                 struct winbindd_cli_state *state)
 {
        NTSTATUS result;
-       struct netr_SamInfo3 *info3 = NULL;
        const char *name_user = NULL;
        const char *name_domain = NULL;
        const char *workstation;
-
+       uint8_t authoritative = 0;
+       uint32_t flags = 0;
+       uint16_t validation_level;
+       union netr_Validation *validation = NULL;
        DATA_BLOB lm_resp, nt_resp;
 
        /* This is child-only, so no check for privileged access is needed
@@ -1873,48 +2396,38 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
                                           state->request->data.auth_crap.nt_resp_len);
        }
 
-       if (strequal(name_domain, get_global_sam_name())) {
-               DATA_BLOB chal_blob = data_blob_const(
-                       state->request->data.auth_crap.chal,
-                       sizeof(state->request->data.auth_crap.chal));
-
-               result = winbindd_dual_auth_passdb(
-                       state->mem_ctx,
-                       state->request->data.auth_crap.logon_parameters,
-                       name_domain, name_user,
-                       &chal_blob, &lm_resp, &nt_resp, &info3);
-               goto process_result;
-       }
-
-       result = winbind_samlogon_retry_loop(domain,
-                                            state->mem_ctx,
-                                            state->request->data.auth_crap.logon_parameters,
-                                            domain->dcname,
-                                            name_user,
-                                            name_domain,
-                                            /* Bug #3248 - found by Stefan Burkei. */
-                                            workstation, /* We carefully set this above so use it... */
-                                            state->request->data.auth_crap.chal,
-                                            lm_resp,
-                                            nt_resp,
-                                            &info3);
+       result = winbind_dual_SamLogon(domain,
+                                      state->mem_ctx,
+                                      state->request->data.auth_crap.logon_parameters,
+                                      name_user,
+                                      name_domain,
+                                      /* Bug #3248 - found by Stefan Burkei. */
+                                      workstation, /* We carefully set this above so use it... */
+                                      state->request->data.auth_crap.chal,
+                                      lm_resp,
+                                      nt_resp,
+                                      &authoritative,
+                                      false,
+                                      &flags,
+                                      &validation_level,
+                                      &validation);
        if (!NT_STATUS_IS_OK(result)) {
+               state->response->data.auth.authoritative = authoritative;
                goto done;
        }
 
-process_result:
-
        if (NT_STATUS_IS_OK(result)) {
-               struct dom_sid user_sid;
+               struct netr_SamInfo3 *info3 = NULL;
 
-               sid_compose(&user_sid, info3->base.domain_sid,
-                           info3->base.rid);
-               wcache_invalidate_samlogon(find_domain_from_name(name_domain),
-                                          &user_sid);
-               netsamlogon_cache_store(name_user, info3);
+               result = map_validation_to_info3(state->mem_ctx,
+                                                validation_level,
+                                                validation,
+                                                &info3);
+               if (!NT_STATUS_IS_OK(result)) {
+                       goto done;
+               }
 
                /* Check if the user is in the right group */
-
                result = check_info3_in_group(
                        info3,
                        state->request->data.auth_crap.require_membership_of_sid);
@@ -1927,7 +2440,9 @@ process_result:
                }
 
                result = append_auth_data(state->mem_ctx, state->response,
-                                         state->request->flags, info3,
+                                         state->request->flags,
+                                         validation_level,
+                                         validation,
                                          name_domain, name_user);
                if (!NT_STATUS_IS_OK(result)) {
                        goto done;
@@ -1936,25 +2451,12 @@ process_result:
 
 done:
 
-       /* give us a more useful (more correct?) error code */
-       if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
-           (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
-               result = NT_STATUS_NO_LOGON_SERVERS;
-       }
-
        if (state->request->flags & WBFLAG_PAM_NT_STATUS_SQUASH) {
                result = nt_status_squash(result);
        }
 
        set_auth_errors(state->response, result);
 
-       DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
-             ("NTLM CRAP authentication for user [%s]\\[%s] returned %s (PAM: %d)\n",
-              name_domain,
-              name_user,
-              state->response->data.auth.nt_status_string,
-              state->response->data.auth.pam_error));
-
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
 
@@ -1991,7 +2493,7 @@ enum winbindd_result winbindd_dual_pam_chauthtok(struct winbindd_domain *contact
 
        /* Get sam handle */
 
-       result = cm_connect_sam(contact_domain, state->mem_ctx, &cli,
+       result = cm_connect_sam(contact_domain, state->mem_ctx, true, &cli,
                                &dom_pol);
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
@@ -2130,7 +2632,7 @@ enum winbindd_result winbindd_dual_pam_logoff(struct winbindd_domain *domain,
 
 #ifdef HAVE_KRB5
 
-       if (state->request->data.logoff.uid < 0) {
+       if (state->request->data.logoff.uid == (uid_t)-1) {
                DEBUG(0,("winbindd_pam_logoff: invalid uid\n"));
                goto process_result;
        }
@@ -2264,7 +2766,7 @@ enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domai
 
        /* Get sam handle */
 
-       result = cm_connect_sam(contact_domain, state->mem_ctx, &cli, &dom_pol);
+       result = cm_connect_sam(contact_domain, state->mem_ctx, true, &cli, &dom_pol);
        if (!NT_STATUS_IS_OK(result)) {
                DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
                goto done;
@@ -2374,6 +2876,7 @@ NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
        struct winbindd_request *req = state->request;
        DATA_BLOB pac_blob;
        struct PAC_LOGON_INFO *logon_info = NULL;
+       struct netr_SamInfo3 *info3_copy = NULL;
        NTSTATUS result;
 
        pac_blob = data_blob_const(req->extra_data.data, req->extra_len);
@@ -2386,8 +2889,47 @@ NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
        }
 
        if (logon_info) {
-               /* Signature verification succeeded, trust the PAC */
-               netsamlogon_cache_store(NULL, &logon_info->info3);
+               /*
+                * Signature verification succeeded, we can
+                * trust the PAC and prime the netsamlogon
+                * and name2sid caches. DO NOT DO THIS
+                * in the signature verification failed
+                * code path.
+                */
+               struct winbindd_domain *domain = NULL;
+
+               result = create_info3_from_pac_logon_info(state->mem_ctx,
+                                                       logon_info,
+                                                       &info3_copy);
+               if (!NT_STATUS_IS_OK(result)) {
+                       return result;
+               }
+               netsamlogon_cache_store(NULL, info3_copy);
+
+               /*
+                * We're in the parent here, so find the child
+                * pointer from the PAC domain name.
+                */
+               domain = find_lookup_domain_from_name(
+                               info3_copy->base.logon_domain.string);
+               if (domain && domain->primary ) {
+                       struct dom_sid user_sid;
+
+                       sid_compose(&user_sid,
+                               info3_copy->base.domain_sid,
+                               info3_copy->base.rid);
+
+                       cache_name2sid_trusted(domain,
+                               info3_copy->base.logon_domain.string,
+                               info3_copy->base.account_name.string,
+                               SID_NAME_USER,
+                               &user_sid);
+
+                       DBG_INFO("PAC for user %s\\%s SID %s primed cache\n",
+                               info3_copy->base.logon_domain.string,
+                               info3_copy->base.account_name.string,
+                               sid_string_dbg(&user_sid));
+               }
 
        } else {
                /* Try without signature verification */
@@ -2399,9 +2941,23 @@ NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
                                   nt_errstr(result)));
                        return result;
                }
+               if (logon_info) {
+                       /*
+                        * Don't strictly need to copy here,
+                        * but it makes it explicit we're
+                        * returning a copy talloc'ed off
+                        * the state->mem_ctx.
+                        */
+                       result = copy_netr_SamInfo3(state->mem_ctx,
+                                                   &logon_info->info3,
+                                                   &info3_copy);
+                       if (!NT_STATUS_IS_OK(result)) {
+                               return result;
+                       }
+               }
        }
 
-       *info3 = &logon_info->info3;
+       *info3 = info3_copy;
 
        return NT_STATUS_OK;
 }