s3:winbindd: Remove unused arcfour.h from PAM handling
[samba.git] / source3 / winbindd / winbindd_pam.c
index 56c2d3a7455aec5a653f1ff8f111a6db285750e2..2d533773fb9e2777d4f3241e89ed91226ad8ec26 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "includes.h"
 #include "winbindd.h"
+#include "libsmb/namequery.h"
 #include "../libcli/auth/libcli_auth.h"
 #include "../librpc/gen_ndr/ndr_samr_c.h"
 #include "rpc_client/cli_pipe.h"
@@ -31,7 +32,6 @@
 #include "../librpc/gen_ndr/ndr_netlogon.h"
 #include "rpc_client/cli_netlogon.h"
 #include "smb_krb5.h"
-#include "../lib/crypto/arcfour.h"
 #include "../libcli/security/security.h"
 #include "ads.h"
 #include "../librpc/gen_ndr/krb5pac.h"
@@ -44,6 +44,9 @@
 #include "lib/afs/afs_funcs.h"
 #include "libsmb/samlogon_cache.h"
 #include "rpc_client/util_netlogon.h"
+#include "libads/krb5_errs.h"
+#include "param/param.h"
+#include "messaging/messaging.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);
@@ -101,34 +116,51 @@ 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;
+               struct dom_sid_buf sidbuf;
 
-               sid = dom_sid_string(mem_ctx, info3->sids[i].sid);
-               NT_STATUS_HAVE_NO_MEMORY(sid);
-
-               ex = talloc_asprintf_append_buffer(ex, "%s:0x%08X\n",
-                                                  sid,
-                                                  info3->sids[i].attributes);
-               NT_STATUS_HAVE_NO_MEMORY(ex);
-
-               talloc_free(sid);
+               ex = talloc_asprintf_append_buffer(
+                       ex,
+                       "%s:0x%08X\n",
+                       dom_sid_str_buf(info3->sids[i].sid, &sidbuf),
+                       info3->sids[i].attributes);
+               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,
@@ -160,7 +192,7 @@ static NTSTATUS append_unix_username(TALLOC_CTX *mem_ctx,
        /* We've been asked to return the unix username, per
           'winbind use default domain' settings and the like */
 
-       const char *nt_username, *nt_domain;
+       const char *nt_username, *nt_domain, *unix_username;
 
        nt_domain = talloc_strdup(mem_ctx, info3->base.logon_domain.string);
        if (!nt_domain) {
@@ -176,8 +208,15 @@ static NTSTATUS append_unix_username(TALLOC_CTX *mem_ctx,
                nt_username = name_user;
        }
 
-       fill_domain_username(resp->data.auth.unix_username,
-                            nt_domain, nt_username, true);
+       unix_username = fill_domain_username_talloc(mem_ctx,
+                                                   nt_domain,
+                                                   nt_username,
+                                                   true);
+       if (unix_username == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       fstrcpy(resp->data.auth.unix_username, unix_username);
 
        DEBUG(5, ("Setting unix username to [%s]\n",
                  resp->data.auth.unix_username));
@@ -210,13 +249,15 @@ static NTSTATUS append_afs_token(TALLOC_CTX *mem_ctx,
 
        {
                struct dom_sid user_sid;
-               fstring sidstr;
+               struct dom_sid_buf sidstr;
 
                sid_compose(&user_sid, info3->base.domain_sid,
                            info3->base.rid);
-               sid_to_fstring(sidstr, &user_sid);
-               afsname = talloc_string_sub(mem_ctx, afsname,
-                                           "%s", sidstr);
+               afsname = talloc_string_sub(
+                       mem_ctx,
+                       afsname,
+                       "%s",
+                       dom_sid_str_buf(&user_sid, &sidstr));
        }
 
        if (afsname == NULL) {
@@ -335,8 +376,10 @@ static NTSTATUS check_info3_in_group(struct netr_SamInfo3 *info3,
        security_token_debug(DBGC_CLASS, 10, token);
 
        for (i=0; i<num_require_membership_of_sid; i++) {
-               DEBUG(10, ("Checking SID %s\n", sid_string_dbg(
-                                  &require_membership_of_sid[i])));
+               struct dom_sid_buf buf;
+               DEBUG(10, ("Checking SID %s\n",
+                          dom_sid_str_buf(&require_membership_of_sid[i],
+                                          &buf)));
                if (nt_token_check_sid(&require_membership_of_sid[i],
                                       token)) {
                        DEBUG(10, ("Access ok\n"));
@@ -362,8 +405,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())) {
@@ -596,7 +645,7 @@ static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
                                            const char *pass,
                                            const char *krb5_cc_type,
                                            uid_t uid,
-                                           struct netr_SamInfo3 **info3,
+                                           struct netr_SamInfo6 **info6,
                                            fstring krb5ccname)
 {
 #ifdef HAVE_KRB5
@@ -606,20 +655,22 @@ static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
        const char *principal_s = NULL;
        const char *service = NULL;
        char *realm = NULL;
-       fstring name_domain, name_user;
+       fstring name_namespace, name_domain, name_user;
        time_t ticket_lifetime = 0;
        time_t renewal_until = 0;
        ADS_STRUCT *ads;
        time_t time_offset = 0;
        const char *user_ccache_file;
        struct PAC_LOGON_INFO *logon_info = NULL;
+       struct PAC_UPN_DNS_INFO *upn_dns_info = NULL;
        struct PAC_DATA *pac_data = NULL;
        struct PAC_DATA_CTR *pac_data_ctr = NULL;
        const char *local_service;
        uint32_t i;
-       struct netr_SamInfo3 *info3_copy = NULL;
+       struct netr_SamInfo6 *info6_copy = NULL;
+       bool ok;
 
-       *info3 = NULL;
+       *info6 = NULL;
 
        if (domain->alt_name == NULL) {
                return NT_STATUS_INVALID_PARAMETER;
@@ -653,7 +704,10 @@ static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
        /* 3rd step:
         * do kerberos auth and setup ccache as the user */
 
-       parse_domain_user(user, name_domain, name_user);
+       ok = parse_domain_user(user, name_namespace, name_domain, name_user);
+       if (!ok) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
        realm = talloc_strdup(mem_ctx, domain->alt_name);
        if (realm == NULL) {
@@ -725,16 +779,15 @@ static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
 
        for (i=0; i < pac_data->num_buffers; i++) {
 
-               if (pac_data->buffers[i].type != PAC_TYPE_LOGON_INFO) {
+               if (pac_data->buffers[i].type == PAC_TYPE_LOGON_INFO) {
+                       logon_info = pac_data->buffers[i].info->logon_info.info;
                        continue;
                }
 
-               logon_info = pac_data->buffers[i].info->logon_info.info;
-               if (!logon_info) {
-                       return NT_STATUS_INVALID_PARAMETER;
+               if (pac_data->buffers[i].type == PAC_TYPE_UPN_DNS_INFO) {
+                       upn_dns_info = &pac_data->buffers[i].info->upn_dns_info;
+                       continue;
                }
-
-               break;
        }
 
        if (logon_info == NULL) {
@@ -746,7 +799,8 @@ static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
        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);
+       result = create_info6_from_pac(mem_ctx, logon_info,
+                                      upn_dns_info, &info6_copy);
        if (!NT_STATUS_IS_OK(result)) {
                goto failed;
        }
@@ -786,7 +840,7 @@ static NTSTATUS winbindd_raw_kerberos_login(TALLOC_CTX *mem_ctx,
                }
 
        }
-       *info3 = info3_copy;
+       *info6 = info6_copy;
        return NT_STATUS_OK;
 
 failed:
@@ -851,11 +905,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,
@@ -877,7 +941,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;
                }
        }
 
@@ -888,16 +952,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;
                }
        }
 
@@ -907,11 +973,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,
@@ -920,7 +989,7 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
 {
        NTSTATUS result = NT_STATUS_LOGON_FAILURE;
        uint16_t max_allowed_bad_attempts;
-       fstring name_domain, name_user;
+       fstring name_namespace, name_domain, name_user;
        struct dom_sid sid;
        enum lsa_SidType type;
        uchar new_nt_pass[NT_HASH_LEN];
@@ -929,6 +998,7 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
        struct netr_SamInfo3 *my_info3;
        time_t kickoff_time, must_change_time;
        bool password_good = false;
+       bool ok;
 #ifdef HAVE_KRB5
        struct winbindd_tdc_domain *tdc_domain = NULL;
 #endif
@@ -941,10 +1011,17 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
 
        /* Parse domain and username */
 
-       parse_domain_user(state->request->data.auth.user, name_domain, name_user);
-
+       ok = parse_domain_user(state->request->data.auth.user,
+                              name_namespace,
+                              name_domain,
+                              name_user);
+       if (!ok) {
+               DBG_DEBUG("parse_domain_user failed\n");
+               return NT_STATUS_NO_SUCH_USER;
+       }
 
-       if (!lookup_cached_name(name_domain,
+       if (!lookup_cached_name(name_namespace,
+                               name_domain,
                                name_user,
                                &sid,
                                &type)) {
@@ -1186,22 +1263,31 @@ failed:
 
 static NTSTATUS winbindd_dual_pam_auth_kerberos(struct winbindd_domain *domain,
                                                struct winbindd_cli_state *state,
-                                               struct netr_SamInfo3 **info3)
+                                               struct netr_SamInfo6 **info6)
 {
        struct winbindd_domain *contact_domain;
-       fstring name_domain, name_user;
+       fstring name_namespace, name_domain, name_user;
        NTSTATUS result;
+       bool ok;
 
        DEBUG(10,("winbindd_dual_pam_auth_kerberos\n"));
 
        /* Parse domain and username */
 
-       parse_domain_user(state->request->data.auth.user, name_domain, name_user);
+       ok = parse_domain_user(state->request->data.auth.user,
+                              name_namespace,
+                              name_domain,
+                              name_user);
+       if (!ok) {
+               result = NT_STATUS_INVALID_PARAMETER;
+               goto done;
+       }
 
        /* what domain should we contact? */
 
        if ( IS_DC ) {
-               if (!(contact_domain = find_domain_from_name(name_domain))) {
+               contact_domain = find_domain_from_name(name_namespace);
+               if (contact_domain == NULL) {
                        DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
                                  state->request->data.auth.user, name_domain, name_user, name_domain));
                        result = NT_STATUS_NO_SUCH_USER;
@@ -1215,7 +1301,7 @@ static NTSTATUS winbindd_dual_pam_auth_kerberos(struct winbindd_domain *domain,
                        goto done;
                }
 
-               contact_domain = find_domain_from_name(name_domain);
+               contact_domain = find_domain_from_name(name_namespace);
                if (contact_domain == NULL) {
                        DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
                                  state->request->data.auth.user, name_domain, name_user, name_domain));
@@ -1245,17 +1331,23 @@ try_login:
                state->request->data.auth.pass,
                state->request->data.auth.krb5_cc_type,
                get_uid_from_request(state->request),
-               info3, state->response->data.auth.krb5ccname);
+               info6, state->response->data.auth.krb5ccname);
 done:
        return result;
 }
 
 static NTSTATUS winbindd_dual_auth_passdb(TALLOC_CTX *mem_ctx,
                                          uint32_t logon_parameters,
-                                         const char *domain, const char *user,
+                                         const char *domain,
+                                         const char *user,
+                                         const uint64_t logon_id,
+                                         const char *client_name,
+                                         const int client_pid,
                                          const DATA_BLOB *challenge,
                                          const DATA_BLOB *lm_resp,
                                          const DATA_BLOB *nt_resp,
+                                         const struct tsocket_address *remote,
+                                         const struct tsocket_address *local,
                                          bool interactive,
                                          uint8_t *pauthoritative,
                                          struct netr_SamInfo3 **pinfo3)
@@ -1263,11 +1355,9 @@ static NTSTATUS winbindd_dual_auth_passdb(TALLOC_CTX *mem_ctx,
        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();
 
        /*
@@ -1275,23 +1365,8 @@ static NTSTATUS winbindd_dual_auth_passdb(TALLOC_CTX *mem_ctx,
         */
        *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;
-       }
-
-       /*
-        * 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,
+                               lp_netbios_name(), remote, local,
                                "winbind",
                                lm_resp, nt_resp, NULL, NULL,
                                NULL, AUTH_PASSWORD_RESPONSE);
@@ -1302,6 +1377,13 @@ static NTSTATUS winbindd_dual_auth_passdb(TALLOC_CTX *mem_ctx,
        }
 
        user_info->logon_parameters = logon_parameters;
+       user_info->logon_id = logon_id;
+       user_info->auth_description = talloc_asprintf(
+               frame, "PASSDB, %s, %d", client_name, client_pid);
+       if (user_info->auth_description == NULL) {
+               TALLOC_FREE(frame);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        /* We don't want any more mapping of the username */
        user_info->mapped_state = True;
@@ -1367,6 +1449,8 @@ static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
                                            const char *password,
                                            const char *domainname,
                                            const char *workstation,
+                                           const uint64_t logon_id,
+                                           bool plaintext_given,
                                            const uint8_t chal[8],
                                            DATA_BLOB lm_response,
                                            DATA_BLOB nt_response,
@@ -1380,15 +1464,19 @@ static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
        int netr_attempts = 0;
        bool retry = false;
        NTSTATUS result;
-       uint16_t validation_level;
+       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;
+               struct netlogon_creds_cli_context *netlogon_creds_ctx = NULL;
 
                retry = false;
 
-               result = cm_connect_netlogon(domain, &netlogon_pipe);
+               result = cm_connect_netlogon_secure(domain, &netlogon_pipe,
+                                                   &netlogon_creds_ctx);
 
                if (NT_STATUS_EQUAL(result,
                                    NT_STATUS_CANT_ACCESS_DOMAIN_INFO)) {
@@ -1440,14 +1528,33 @@ static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
                        }
                        return result;
                }
+
+               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) {
+               if (plaintext_given) {
                        result = rpccli_netlogon_password_logon(
-                               domain->conn.netlogon_creds_ctx,
+                               netlogon_creds_ctx,
                                netlogon_pipe->binding_handle,
                                mem_ctx,
                                logon_parameters,
@@ -1455,23 +1562,43 @@ static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
                                username,
                                password,
                                workstation,
-                               NetlogonInteractiveInformation,
+                               logon_id,
+                               logon_type_i,
+                               authoritative,
+                               flags,
+                               &validation_level,
+                               &validation);
+               } else if (interactive) {
+                       result = rpccli_netlogon_interactive_logon(
+                               netlogon_creds_ctx,
+                               netlogon_pipe->binding_handle,
+                               mem_ctx,
+                               logon_parameters,
+                               username,
+                               domainname,
+                               workstation,
+                               logon_id,
+                               lm_response,
+                               nt_response,
+                               logon_type_i,
                                authoritative,
                                flags,
                                &validation_level,
                                &validation);
                } else {
                        result = rpccli_netlogon_network_logon(
-                               domain->conn.netlogon_creds_ctx,
+                               netlogon_creds_ctx,
                                netlogon_pipe->binding_handle,
                                mem_ctx,
                                logon_parameters,
                                username,
                                domainname,
                                workstation,
+                               logon_id,
                                chal,
                                lm_response,
                                nt_response,
+                               logon_type_n,
                                authoritative,
                                flags,
                                &validation_level,
@@ -1494,7 +1621,7 @@ static NTSTATUS winbind_samlogon_retry_loop(struct winbindd_domain *domain,
                }
 
                /* if we get access denied, a possible cause was that we had
-                  and open connection to the DC, but someone changed our
+                  an open connection to the DC, but someone changed our
                   machine account password out from underneath us using 'net
                   rpc changetrustpw' */
 
@@ -1557,7 +1684,12 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(
        struct winbindd_domain *domain,
        const char *user,
        const char *pass,
+       uint64_t logon_id,
+       const char *client_name,
+       const int client_pid,
        uint32_t request_flags,
+       const struct tsocket_address *remote,
+       const struct tsocket_address *local,
        uint16_t *_validation_level,
        union netr_Validation **_validation)
 {
@@ -1566,19 +1698,23 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(
        DATA_BLOB lm_resp;
        DATA_BLOB nt_resp;
        unsigned char local_nt_response[24];
-       fstring name_domain, name_user;
+       fstring name_namespace, name_domain, name_user;
        NTSTATUS result;
        uint8_t authoritative = 0;
        uint32_t flags = 0;
        uint16_t validation_level;
        union netr_Validation *validation = NULL;
        struct netr_SamBaseInfo *base_info = NULL;
+       bool ok;
 
        DEBUG(10,("winbindd_dual_pam_auth_samlogon\n"));
 
        /* Parse domain and username */
 
-       parse_domain_user(user, name_domain, name_user);
+       ok = parse_domain_user(user, name_namespace, name_domain, name_user);
+       if (!ok) {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
        /*
         * We check against domain->name instead of
@@ -1630,26 +1766,33 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(
 
                result = winbindd_dual_auth_passdb(
                        talloc_tos(), 0, name_domain, name_user,
+                       logon_id,
+                       client_name,
+                       client_pid,
                        &chal_blob, &lm_resp, &nt_resp,
+                       remote,
+                       local,
                        true, /* 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 (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;
+                               }
+                       }
+
                        goto done;
                }
        }
@@ -1663,6 +1806,8 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(
                                             pass,
                                             name_domain,
                                             lp_netbios_name(),
+                                            logon_id,
+                                            true, /* plaintext_given */
                                             NULL,
                                             data_blob_null, data_blob_null,
                                             true, /* interactive */
@@ -1765,16 +1910,155 @@ done:
        return result;
 }
 
+/*
+ * @brief build a tsocket_address for the remote address of the supplied socket
+ *
+ */
+static struct tsocket_address *get_remote_address(TALLOC_CTX *mem_ctx, int sock)
+{
+       struct sockaddr_storage st = {0};
+       struct sockaddr *sar = (struct sockaddr *)&st;
+       socklen_t sa_len = sizeof(st);
+       struct tsocket_address *remote = NULL;
+       int ret = 0;
+
+       ret = getpeername(sock, sar, &sa_len);
+       if (ret != 0) {
+               DBG_ERR("getpeername failed - %s", strerror(errno));
+               return NULL;
+       }
+       ret = tsocket_address_bsd_from_sockaddr(mem_ctx, sar, sa_len, &remote);
+       if (ret != 0) {
+               DBG_ERR("tsocket_address_bsd_from_sockaddr failed - %s",
+                       strerror(errno));
+               return NULL;
+       }
+       return remote;
+}
+
+/*
+ * @brief build a tsocket_address for the local address of the supplied socket
+ *
+ */
+static struct tsocket_address *get_local_address(TALLOC_CTX *mem_ctx, int sock)
+{
+       struct sockaddr_storage st = {0};
+       struct sockaddr *sar = (struct sockaddr *)&st;
+       socklen_t sa_len = sizeof(st);
+       struct tsocket_address *local = NULL;
+       int ret = 0;
+
+       ret = getsockname(sock, sar, &sa_len);
+       if (ret != 0) {
+               DBG_ERR("getsockname failed - %s", strerror(errno));
+               return NULL;
+       }
+       ret = tsocket_address_bsd_from_sockaddr(mem_ctx, sar, sa_len, &local);
+       if (ret != 0) {
+               DBG_ERR("tsocket_address_bsd_from_sockaddr failed - %s",
+                       strerror(errno));
+               return NULL;
+       }
+       return local;
+}
+
+/*
+ * @brief generate an authentication message in the logs.
+ *
+ */
+static void log_authentication(
+       TALLOC_CTX *mem_ctx,
+       const struct winbindd_domain *domain,
+       const struct winbindd_cli_state *state,
+       const struct timeval start_time,
+       const uint64_t logon_id,
+       const char *command,
+       const char *user_name,
+       const char *domain_name,
+       const char *workstation,
+       const DATA_BLOB lm_resp,
+       const DATA_BLOB nt_resp,
+       const struct tsocket_address *remote,
+       const struct tsocket_address *local,
+       NTSTATUS result)
+{
+
+       struct auth_usersupplied_info *ui = NULL;
+       struct dom_sid *sid = NULL;
+       struct loadparm_context *lp_ctx = NULL;
+       struct imessaging_context *msg_ctx = NULL;
+
+       ui = talloc_zero(mem_ctx, struct auth_usersupplied_info);
+       ui->logon_id = logon_id;
+       ui->service_description = "winbind";
+       ui->password.response.nt.length = nt_resp.length;
+       ui->password.response.nt.data = nt_resp.data;
+       ui->password.response.lanman.length = lm_resp.length;
+       ui->password.response.lanman.data = lm_resp.data;
+       if (nt_resp.length == 0 && lm_resp.length == 0) {
+               ui->password_state = AUTH_PASSWORD_PLAIN;
+       } else {
+               ui->password_state = AUTH_PASSWORD_RESPONSE;
+       }
+       /*
+        * In the event of a failure ui->auth_description will be null,
+        * the logging code handles this correctly so it can be ignored.
+        */
+       ui->auth_description = talloc_asprintf(
+               ui,
+               "%s, %s, %d",
+               command,
+               state->request->client_name,
+               state->pid);
+       if (ui->auth_description == NULL) {
+               DBG_ERR("OOM Unable to create auth_description");
+       }
+       ui->client.account_name = user_name;
+       ui->client.domain_name = domain_name;
+       ui->workstation_name = workstation;
+       ui->remote_host = remote;
+       ui->local_host = local;
+
+       sid = dom_sid_parse_talloc(
+           ui, state->response->data.auth.info3.dom_sid);
+       if (sid != NULL) {
+               sid_append_rid(sid, state->response->data.auth.info3.user_rid);
+       }
+
+       if (lp_auth_event_notification()) {
+               lp_ctx = loadparm_init_s3(ui, loadparm_s3_helpers());
+               msg_ctx = imessaging_client_init(
+                   ui, lp_ctx, global_event_context());
+       }
+       log_authentication_event(
+           msg_ctx,
+           lp_ctx,
+           &start_time,
+           ui,
+           result,
+           state->response->data.auth.info3.logon_dom,
+           state->response->data.auth.info3.user_name,
+           state->response->data.auth.unix_username,
+           sid);
+       TALLOC_FREE(ui);
+}
+
 enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
                                            struct winbindd_cli_state *state)
 {
        NTSTATUS result = NT_STATUS_LOGON_FAILURE;
        NTSTATUS krb5_result = NT_STATUS_OK;
-       fstring name_domain, name_user;
+       fstring name_namespace, 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;
+       bool ok;
+       uint64_t logon_id = 0;
+       const struct timeval start_time = timeval_current();
+       const struct tsocket_address *remote = NULL;
+       const struct tsocket_address *local = NULL;
 
        /* Ensure null termination */
        state->request->data.auth.user[sizeof(state->request->data.auth.user)-1]='\0';
@@ -1782,6 +2066,12 @@ enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
        /* Ensure null termination */
        state->request->data.auth.pass[sizeof(state->request->data.auth.pass)-1]='\0';
 
+       /*
+        * Generate a logon_id for this session.
+        */
+       logon_id = generate_random_u64();
+       remote = get_remote_address(state->mem_ctx, state->sock);
+       local = get_local_address(state->mem_ctx, state->sock);
        DEBUG(3, ("[%5lu]: dual pam auth %s\n", (unsigned long)state->pid,
                  state->request->data.auth.user));
 
@@ -1800,7 +2090,14 @@ enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
                mapped_user = state->request->data.auth.user;
        }
 
-       parse_domain_user(mapped_user, name_domain, name_user);
+       ok = parse_domain_user(mapped_user,
+                              name_namespace,
+                              name_domain,
+                              name_user);
+       if (!ok) {
+               result = NT_STATUS_INVALID_PARAMETER;
+               goto process_result;
+       }
 
        if ( mapped_user != state->request->data.auth.user ) {
                fstr_sprintf( domain_user, "%s%c%s", name_domain,
@@ -1829,14 +2126,24 @@ 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_SamInfo6 *info6 = NULL;
 
-               result = winbindd_dual_pam_auth_kerberos(domain, state, &info3);
+               result = winbindd_dual_pam_auth_kerberos(domain, state, &info6);
                /* save for later */
                krb5_result = result;
 
-
                if (NT_STATUS_IS_OK(result)) {
                        DEBUG(10,("winbindd_dual_pam_auth_kerberos succeeded\n"));
+
+                       result = map_info6_to_validation(state->mem_ctx,
+                                                        info6,
+                                                        &validation_level,
+                                                        &validation);
+                       TALLOC_FREE(info6);
+                       if (!NT_STATUS_IS_OK(result)) {
+                               DBG_ERR("map_info6_to_validation failed\n");
+                               goto done;
+                       }
                        goto process_result;
                }
 
@@ -1879,15 +2186,18 @@ enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
 sam_logon:
        /* Check for Samlogon authentication */
        if (domain->online) {
-               uint16_t validation_level;
-               union netr_Validation *validation = NULL;
                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,
+                       logon_id,
+                       state->request->client_name,
+                       state->pid,
                        state->request->flags,
+                       remote,
+                       local,
                        &validation_level,
                        &validation);
 
@@ -1913,14 +2223,6 @@ sam_logon:
                                base_info->user_flags |= LOGON_KRB5_FAIL_CLOCK_SKEW;
                        }
 
-                       result = map_validation_to_info3(state->mem_ctx,
-                                                        validation_level,
-                                                        validation,
-                                                        &info3);
-                       TALLOC_FREE(validation);
-                       if (!NT_STATUS_IS_OK(result)) {
-                               goto done;
-                       }
                        goto process_result;
                }
 
@@ -1946,54 +2248,83 @@ 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;
+               TALLOC_CTX *base_ctx = NULL;
+               struct netr_SamBaseInfo *base_info = NULL;
+               struct netr_SamInfo3 *info3 = NULL;
 
-               /* In all codepaths where result == NT_STATUS_OK info3 must have
-                  been initialized. */
-               if (!info3) {
+               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 (info3->base.full_name.string == NULL) {
+               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) {
-                               info3->base.full_name.string =
-                                       talloc_strdup(info3,
-                                                     cached_info3->base.full_name.string);
+                               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,
-                                               info3,
+                                               base_ctx,
                                                &user_sid,
-                                               &info3->base.full_name.string);
+                                               &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);
@@ -2020,7 +2351,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;
@@ -2063,25 +2396,51 @@ done:
              state->response->data.auth.nt_status_string,
              state->response->data.auth.pam_error));
 
+       /*
+        * Log the winbind pam authentication, the logon_id will tie this to
+        * any of the logons invoked from this request.
+        */
+       log_authentication(
+           state->mem_ctx,
+           domain,
+           state,
+           start_time,
+           logon_id,
+           "PAM_AUTH",
+           name_user,
+           name_domain,
+           NULL,
+           data_blob_null,
+           data_blob_null,
+           remote,
+           local,
+           result);
+
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
 
 NTSTATUS winbind_dual_SamLogon(struct winbindd_domain *domain,
                               TALLOC_CTX *mem_ctx,
+                              bool interactive,
                               uint32_t logon_parameters,
                               const char *name_user,
                               const char *name_domain,
                               const char *workstation,
+                              const uint64_t logon_id,
+                              const char* client_name,
+                              const int client_pid,
                               const uint8_t chal[8],
                               DATA_BLOB lm_response,
                               DATA_BLOB nt_response,
+                              const struct tsocket_address *remote,
+                              const struct tsocket_address *local,
                               uint8_t *authoritative,
                               bool skip_sam,
                               uint32_t *flags,
                               uint16_t *_validation_level,
                               union netr_Validation **_validation)
 {
-       uint16_t validation_level;
+       uint16_t validation_level = 0;
        union netr_Validation *validation = NULL;
        NTSTATUS result;
 
@@ -2103,8 +2462,13 @@ NTSTATUS winbind_dual_SamLogon(struct winbindd_domain *domain,
                        talloc_tos(),
                        logon_parameters,
                        name_domain, name_user,
+                       logon_id,
+                       client_name,
+                       client_pid,
                        &chal_blob, &lm_response, &nt_response,
-                       false, /* interactive */
+                       remote,
+                       local,
+                       interactive,
                        authoritative,
                        &info3);
                if (NT_STATUS_IS_OK(result)) {
@@ -2136,10 +2500,12 @@ NTSTATUS winbind_dual_SamLogon(struct winbindd_domain *domain,
                                             name_domain,
                                             /* Bug #3248 - found by Stefan Burkei. */
                                             workstation, /* We carefully set this above so use it... */
+                                            logon_id,
+                                            false, /* plaintext_given */
                                             chal,
                                             lm_response,
                                             nt_response,
-                                            false, /* interactive */
+                                            interactive,
                                             authoritative,
                                             flags,
                                             &validation_level,
@@ -2236,11 +2602,15 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
        const char *name_user = NULL;
        const char *name_domain = NULL;
        const char *workstation;
+       uint64_t logon_id = 0;
        uint8_t authoritative = 0;
        uint32_t flags = 0;
        uint16_t validation_level;
        union netr_Validation *validation = NULL;
        DATA_BLOB lm_resp, nt_resp;
+       const struct timeval start_time = timeval_current();
+       const struct tsocket_address *remote = NULL;
+       const struct tsocket_address *local = NULL;
 
        /* This is child-only, so no check for privileged access is needed
           anymore */
@@ -2252,6 +2622,9 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
        name_user = state->request->data.auth_crap.user;
        name_domain = state->request->data.auth_crap.domain;
        workstation = state->request->data.auth_crap.workstation;
+       logon_id = generate_random_u64();
+       remote = get_remote_address(state->mem_ctx, state->sock);
+       local = get_local_address(state->mem_ctx, state->sock);
 
        DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state->pid,
                  name_domain, name_user));
@@ -2283,14 +2656,20 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
 
        result = winbind_dual_SamLogon(domain,
                                       state->mem_ctx,
+                                      false, /* interactive */
                                       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... */
+                                      logon_id,
+                                      state->request->client_name,
+                                      state->request->pid,
                                       state->request->data.auth_crap.chal,
                                       lm_resp,
                                       nt_resp,
+                                      remote,
+                                      local,
                                       &authoritative,
                                       false,
                                       &flags,
@@ -2308,7 +2687,6 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
                                                 validation_level,
                                                 validation,
                                                 &info3);
-               TALLOC_FREE(validation);
                if (!NT_STATUS_IS_OK(result)) {
                        goto done;
                }
@@ -2326,7 +2704,9 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
                }
 
                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;
@@ -2340,6 +2720,25 @@ done:
        }
 
        set_auth_errors(state->response, result);
+       /*
+        * Log the winbind pam authentication, the logon_id will tie this to
+        * any of the logons invoked from this request.
+        */
+       log_authentication(
+           state->mem_ctx,
+           domain,
+           state,
+           start_time,
+           logon_id,
+           "NTLM_AUTH",
+           name_user,
+           name_domain,
+           workstation,
+           lm_resp,
+            nt_resp,
+           remote,
+           local,
+           result);
 
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
@@ -2355,15 +2754,20 @@ enum winbindd_result winbindd_dual_pam_chauthtok(struct winbindd_domain *contact
        struct samr_DomInfo1 *info = NULL;
        struct userPwdChangeFailureInformation *reject = NULL;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       fstring domain, user;
+       fstring namespace, domain, user;
        struct dcerpc_binding_handle *b = NULL;
+       bool ok;
 
        ZERO_STRUCT(dom_pol);
 
        DEBUG(3, ("[%5lu]: dual pam chauthtok %s\n", (unsigned long)state->pid,
                  state->request->data.auth.user));
 
-       if (!parse_domain_user(state->request->data.chauthtok.user, domain, user)) {
+       ok = parse_domain_user(state->request->data.chauthtok.user,
+                              namespace,
+                              domain,
+                              user);
+       if (!ok) {
                goto done;
        }
 
@@ -2572,7 +2976,7 @@ enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domai
        DATA_BLOB old_nt_hash_enc;
        DATA_BLOB new_lm_password;
        DATA_BLOB old_lm_hash_enc;
-       fstring  domain,user;
+       fstring  namespace, domain, user;
        struct policy_handle dom_pol;
        struct winbindd_domain *contact_domain = domainSt;
        struct rpc_pipe_client *cli = NULL;
@@ -2585,8 +2989,9 @@ enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domai
                sizeof(state->request->data.chng_pswd_auth_crap.user)-1]=0;
        state->request->data.chng_pswd_auth_crap.domain[
                sizeof(state->request->data.chng_pswd_auth_crap.domain)-1]=0;
-       *domain = 0;
-       *user = 0;
+       domain[0] = '\0';
+       namespace[0] = '\0';
+       user[0] = '\0';
 
        DEBUG(3, ("[%5lu]: pam change pswd auth crap domain: %s user: %s\n",
                  (unsigned long)state->pid,
@@ -2603,8 +3008,16 @@ enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domai
        if (*state->request->data.chng_pswd_auth_crap.domain) {
                fstrcpy(domain,state->request->data.chng_pswd_auth_crap.domain);
        } else {
-               parse_domain_user(state->request->data.chng_pswd_auth_crap.user,
-                                 domain, user);
+               bool ok;
+
+               ok = parse_domain_user(state->request->data.chng_pswd_auth_crap.user,
+                                      namespace,
+                                      domain,
+                                      user);
+               if (!ok) {
+                       result = NT_STATUS_INVALID_PARAMETER;
+                       goto done;
+               }
 
                if(!*domain) {
                        DEBUG(3,("no domain specified with username (%s) - "
@@ -2688,7 +3101,7 @@ enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domai
 
 #ifdef HAVE_KRB5
 static NTSTATUS extract_pac_vrfy_sigs(TALLOC_CTX *mem_ctx, DATA_BLOB pac_blob,
-                                     struct PAC_LOGON_INFO **logon_info)
+                                     struct PAC_DATA **p_pac_data)
 {
        krb5_context krbctx = NULL;
        krb5_error_code k5ret;
@@ -2700,10 +3113,10 @@ static NTSTATUS extract_pac_vrfy_sigs(TALLOC_CTX *mem_ctx, DATA_BLOB pac_blob,
        ZERO_STRUCT(entry);
        ZERO_STRUCT(cursor);
 
-       k5ret = krb5_init_context(&krbctx);
+       k5ret = smb_krb5_init_context_common(&krbctx);
        if (k5ret) {
-               DEBUG(1, ("Failed to initialize kerberos context: %s\n",
-                         error_message(k5ret)));
+               DBG_ERR("kerberos init context failed (%s)\n",
+                       error_message(k5ret));
                status = krb5_to_nt_status(k5ret);
                goto out;
        }
@@ -2726,10 +3139,14 @@ static NTSTATUS extract_pac_vrfy_sigs(TALLOC_CTX *mem_ctx, DATA_BLOB pac_blob,
 
        k5ret = krb5_kt_next_entry(krbctx, keytab, &entry, &cursor);
        while (k5ret == 0) {
-               status = kerberos_pac_logon_info(mem_ctx, pac_blob,
-                                                krbctx, NULL,
-                                                KRB5_KT_KEY(&entry), NULL, 0,
-                                                logon_info);
+               status = kerberos_decode_pac(mem_ctx,
+                                            pac_blob,
+                                            krbctx,
+                                            NULL, /* krbtgt_keyblock */
+                                            KRB5_KT_KEY(&entry), /* service_keyblock */
+                                            NULL, /* client_principal */
+                                            0, /* tgs_authtime */
+                                            p_pac_data);
                if (NT_STATUS_IS_OK(status)) {
                        break;
                }
@@ -2754,25 +3171,86 @@ out:
        return status;
 }
 
-NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
-                                   struct netr_SamInfo3 **info3)
+NTSTATUS winbindd_pam_auth_pac_verify(struct winbindd_cli_state *state,
+                                     bool *p_is_trusted,
+                                     uint16_t *p_validation_level,
+                                     union netr_Validation **p_validation)
 {
        struct winbindd_request *req = state->request;
        DATA_BLOB pac_blob;
+       struct PAC_DATA *pac_data = NULL;
        struct PAC_LOGON_INFO *logon_info = NULL;
+       struct PAC_UPN_DNS_INFO *upn_dns_info = NULL;
+       struct netr_SamInfo6 *info6 = NULL;
+       uint16_t validation_level = 0;
+       union netr_Validation *validation = NULL;
        struct netr_SamInfo3 *info3_copy = NULL;
        NTSTATUS result;
+       bool is_trusted = false;
+       uint32_t i;
+
+       *p_is_trusted = false;
+       *p_validation_level = 0;
+       *p_validation = NULL;
 
        pac_blob = data_blob_const(req->extra_data.data, req->extra_len);
-       result = extract_pac_vrfy_sigs(state->mem_ctx, pac_blob, &logon_info);
-       if (!NT_STATUS_IS_OK(result) &&
-           !NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
+       result = extract_pac_vrfy_sigs(state->mem_ctx, pac_blob, &pac_data);
+       if (NT_STATUS_IS_OK(result)) {
+               is_trusted = true;
+       }
+       if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED)) {
+               /* Try without signature verification */
+               result = kerberos_decode_pac(state->mem_ctx,
+                                            pac_blob,
+                                            NULL, /* krb5_context */
+                                            NULL, /* krbtgt_keyblock */
+                                            NULL, /* service_keyblock */
+                                            NULL, /* client_principal */
+                                            0, /* tgs_authtime */
+                                            &pac_data);
+       }
+       if (!NT_STATUS_IS_OK(result)) {
                DEBUG(1, ("Error during PAC signature verification: %s\n",
                          nt_errstr(result)));
                return result;
        }
 
-       if (logon_info) {
+       for (i=0; i < pac_data->num_buffers; i++) {
+               if (pac_data->buffers[i].type == PAC_TYPE_LOGON_INFO) {
+                       logon_info = pac_data->buffers[i].info->logon_info.info;
+                       continue;
+               }
+               if (pac_data->buffers[i].type == PAC_TYPE_UPN_DNS_INFO) {
+                       upn_dns_info = &pac_data->buffers[i].info->upn_dns_info;
+                       continue;
+               }
+       }
+
+       result = create_info6_from_pac(state->mem_ctx,
+                                      logon_info,
+                                      upn_dns_info,
+                                      &info6);
+       if (!NT_STATUS_IS_OK(result)) {
+               return result;
+       }
+
+       result = map_info6_to_validation(state->mem_ctx,
+                                        info6,
+                                        &validation_level,
+                                        &validation);
+       if (!NT_STATUS_IS_OK(result)) {
+               return result;
+       }
+
+       result = map_validation_to_info3(state->mem_ctx,
+                                        validation_level,
+                                        validation,
+                                        &info3_copy);
+       if (!NT_STATUS_IS_OK(result)) {
+               return result;
+       }
+
+       if (is_trusted) {
                /*
                 * Signature verification succeeded, we can
                 * trust the PAC and prime the netsamlogon
@@ -2782,12 +3260,6 @@ NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
                 */
                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);
 
                /*
@@ -2798,6 +3270,7 @@ NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
                                info3_copy->base.logon_domain.string);
                if (domain && domain->primary ) {
                        struct dom_sid user_sid;
+                       struct dom_sid_buf buf;
 
                        sid_compose(&user_sid,
                                info3_copy->base.domain_sid,
@@ -2812,42 +3285,25 @@ NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
                        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 */
-               result = kerberos_pac_logon_info(state->mem_ctx, pac_blob, NULL,
-                                                NULL, NULL, NULL, 0,
-                                                &logon_info);
-               if (!NT_STATUS_IS_OK(result)) {
-                       DEBUG(10, ("Could not extract PAC: %s\n",
-                                  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.
-                        */
-                       info3_copy = copy_netr_SamInfo3(state->mem_ctx,
-                                       &logon_info->info3);
-                       if (info3_copy == NULL) {
-                               return NT_STATUS_NO_MEMORY;
-                       }
+                               dom_sid_str_buf(&user_sid, &buf));
                }
        }
 
-       *info3 = info3_copy;
-
+       *p_is_trusted = is_trusted;
+       *p_validation_level = validation_level;
+       *p_validation = validation;
        return NT_STATUS_OK;
 }
 #else /* HAVE_KRB5 */
-NTSTATUS winbindd_pam_auth_pac_send(struct winbindd_cli_state *state,
-                                   struct netr_SamInfo3 **info3)
+NTSTATUS winbindd_pam_auth_pac_verify(struct winbindd_cli_state *state,
+                                     bool *p_is_trusted,
+                                     uint16_t *p_validation_level,
+                                     union netr_Validation **p_validation);
 {
+
+       *p_is_trusted = false;
+       *p_validation_level = 0;
+       *p_validation = NULL;
        return NT_STATUS_NO_SUCH_USER;
 }
 #endif /* HAVE_KRB5 */