s3: Remove unused arg "user_sid" from winbindd_store_creds
[samba.git] / source3 / winbindd / winbindd_pam.c
index b379e2c7a942ec94914c4c8bd95263225cc3f54e..55764178502f810a915ddc2e5fe6bce0374fa4aa 100644 (file)
 #include "includes.h"
 #include "winbindd.h"
 #include "../libcli/auth/libcli_auth.h"
+#include "../librpc/gen_ndr/cli_samr.h"
+#include "rpc_client/cli_samr.h"
+#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/dom_sid.h"
+#include "ads.h"
+#include "../librpc/gen_ndr/krb5pac.h"
+
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
 
@@ -118,7 +128,7 @@ static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx,
        DATA_BLOB blob;
        enum ndr_err_code ndr_err;
 
-       ndr_err = ndr_push_struct_blob(&blob, mem_ctx, NULL, info3,
+       ndr_err = ndr_push_struct_blob(&blob, mem_ctx, info3,
                                       (ndr_push_flags_fn_t)ndr_push_netr_SamInfo3);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                DEBUG(0,("append_info3_as_ndr: failed to append\n"));
@@ -189,11 +199,11 @@ static NTSTATUS append_afs_token(TALLOC_CTX *mem_ctx,
                                    "%U", name_user);
 
        {
-               DOM_SID user_sid;
+               struct dom_sid user_sid;
                fstring sidstr;
 
-               sid_copy(&user_sid, info3->base.domain_sid);
-               sid_append_rid(&user_sid, info3->base.rid);
+               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);
@@ -231,8 +241,7 @@ static NTSTATUS append_afs_token(TALLOC_CTX *mem_ctx,
        return NT_STATUS_OK;
 }
 
-static NTSTATUS check_info3_in_group(TALLOC_CTX *mem_ctx,
-                                    struct netr_SamInfo3 *info3,
+static NTSTATUS check_info3_in_group(struct netr_SamInfo3 *info3,
                                     const char *group_sid)
 /**
  * Check whether a user belongs to a group or list of groups.
@@ -246,14 +255,14 @@ static NTSTATUS check_info3_in_group(TALLOC_CTX *mem_ctx,
  *    or other NT_STATUS_IS_ERR(status) for other kinds of failure.
  */
 {
-       DOM_SID *require_membership_of_sid;
+       struct dom_sid *require_membership_of_sid;
        size_t num_require_membership_of_sid;
        char *req_sid;
        const char *p;
-       DOM_SID sid;
+       struct dom_sid sid;
        size_t i;
        struct nt_user_token *token;
-       TALLOC_CTX *frame = NULL;
+       TALLOC_CTX *frame = talloc_stackframe();
        NTSTATUS status;
 
        /* Parse the 'required group' SID */
@@ -263,8 +272,10 @@ static NTSTATUS check_info3_in_group(TALLOC_CTX *mem_ctx,
                return NT_STATUS_OK;
        }
 
-       if (!(token = TALLOC_ZERO_P(mem_ctx, struct nt_user_token))) {
+       token = talloc_zero(talloc_tos(), struct nt_user_token);
+       if (token == NULL) {
                DEBUG(0, ("talloc failed\n"));
+               TALLOC_FREE(frame);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -273,8 +284,7 @@ static NTSTATUS check_info3_in_group(TALLOC_CTX *mem_ctx,
 
        p = group_sid;
 
-       frame = talloc_stackframe();
-       while (next_token_talloc(frame, &p, &req_sid, ",")) {
+       while (next_token_talloc(talloc_tos(), &p, &req_sid, ",")) {
                if (!string_to_sid(&sid, req_sid)) {
                        DEBUG(0, ("check_info3_in_group: could not parse %s "
                                  "as a SID!", req_sid));
@@ -282,7 +292,7 @@ static NTSTATUS check_info3_in_group(TALLOC_CTX *mem_ctx,
                        return NT_STATUS_INVALID_PARAMETER;
                }
 
-               status = add_sid_to_array(mem_ctx, &sid,
+               status = add_sid_to_array(talloc_tos(), &sid,
                                          &require_membership_of_sid,
                                          &num_require_membership_of_sid);
                if (!NT_STATUS_IS_OK(status)) {
@@ -292,13 +302,12 @@ static NTSTATUS check_info3_in_group(TALLOC_CTX *mem_ctx,
                }
        }
 
-       TALLOC_FREE(frame);
-
-       status = sid_array_from_info3(mem_ctx, info3,
-                                     &token->user_sids,
+       status = sid_array_from_info3(talloc_tos(), info3,
+                                     &token->sids,
                                      &token->num_sids,
                                      true, false);
        if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(frame);
                return status;
        }
 
@@ -308,6 +317,7 @@ static NTSTATUS check_info3_in_group(TALLOC_CTX *mem_ctx,
                                                     token))) {
                DEBUG(3, ("could not add aliases: %s\n",
                          nt_errstr(status)));
+               TALLOC_FREE(frame);
                return status;
        }
 
@@ -319,12 +329,14 @@ static NTSTATUS check_info3_in_group(TALLOC_CTX *mem_ctx,
                if (nt_token_check_sid(&require_membership_of_sid[i],
                                       token)) {
                        DEBUG(10, ("Access ok\n"));
+                       TALLOC_FREE(frame);
                        return NT_STATUS_OK;
                }
        }
 
        /* Do not distinguish this error from a wrong username/pw */
 
+       TALLOC_FREE(frame);
        return NT_STATUS_LOGON_FAILURE;
 }
 
@@ -343,11 +355,8 @@ struct winbindd_domain *find_auth_domain(uint8_t flags,
                return domain;
        }
 
-       if (is_myname(domain_name)) {
-               DEBUG(3, ("Authentication for domain %s (local domain "
-                         "to this server) not supported at this "
-                         "stage\n", domain_name));
-               return NULL;
+       if (strequal(domain_name, get_global_sam_name())) {
+               return find_domain_from_name_noinit(domain_name);
        }
 
        /* we can auth against trusted domains */
@@ -520,11 +529,11 @@ static void setup_return_cc_name(struct winbindd_cli_state *state, const char *c
 
 #endif
 
-static uid_t get_uid_from_state(struct winbindd_cli_state *state)
+uid_t get_uid_from_request(struct winbindd_request *request)
 {
-       uid_t uid = -1;
+       uid_t uid;
 
-       uid = state->request->data.auth.uid;
+       uid = request->data.auth.uid;
 
        if (uid < 0) {
                DEBUG(1,("invalid uid: '%u'\n", (unsigned int)uid));
@@ -533,6 +542,11 @@ static uid_t get_uid_from_state(struct winbindd_cli_state *state)
        return uid;
 }
 
+static uid_t get_uid_from_state(struct winbindd_cli_state *state)
+{
+       return get_uid_from_request(state->request);
+}
+
 /**********************************************************************
  Authenticate a user with a clear text password using Kerberos and fill up
  ccache if required
@@ -556,8 +570,7 @@ static NTSTATUS winbindd_raw_kerberos_login(struct winbindd_domain *domain,
        ADS_STRUCT *ads;
        time_t time_offset = 0;
        bool internal_ccache = true;
-
-       ZERO_STRUCTP(info3);
+       struct PAC_LOGON_INFO *logon_info = NULL;
 
        *info3 = NULL;
 
@@ -615,17 +628,18 @@ static NTSTATUS winbindd_raw_kerberos_login(struct winbindd_domain *domain,
                DEBUG(10,("winbindd_raw_kerberos_login: uid is %d\n", uid));
        }
 
-       result = kerberos_return_info3_from_pac(state->mem_ctx,
-                                               principal_s,
-                                               state->request->data.auth.pass,
-                                               time_offset,
-                                               &ticket_lifetime,
-                                               &renewal_until,
-                                               cc,
-                                               true,
-                                               true,
-                                               WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
-                                               info3);
+       result = kerberos_return_pac(state->mem_ctx,
+                                    principal_s,
+                                    state->request->data.auth.pass,
+                                    time_offset,
+                                    &ticket_lifetime,
+                                    &renewal_until,
+                                    cc,
+                                    true,
+                                    true,
+                                    WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
+                                    NULL,
+                                    &logon_info);
        if (!internal_ccache) {
                gain_root_privilege();
        }
@@ -636,6 +650,8 @@ static NTSTATUS winbindd_raw_kerberos_login(struct winbindd_domain *domain,
                goto failed;
        }
 
+       *info3 = &logon_info->info3;
+
        DEBUG(10,("winbindd_raw_kerberos_login: winbindd validated ticket of %s\n",
                principal_s));
 
@@ -705,7 +721,7 @@ failed:
 /****************************************************************
 ****************************************************************/
 
-static bool check_request_flags(uint32_t flags)
+bool check_request_flags(uint32_t flags)
 {
        uint32_t flags_edata = WBFLAG_PAM_AFS_TOKEN |
                               WBFLAG_PAM_INFO3_TEXT |
@@ -718,7 +734,8 @@ static bool check_request_flags(uint32_t flags)
                return true;
        }
 
-       DEBUG(1,("check_request_flags: invalid request flags[0x%08X]\n",flags));
+       DEBUG(1, ("check_request_flags: invalid request flags[0x%08X]\n",
+                 flags));
 
        return false;
 }
@@ -726,10 +743,10 @@ static bool check_request_flags(uint32_t flags)
 /****************************************************************
 ****************************************************************/
 
-NTSTATUS append_auth_data(struct winbindd_cli_state *state,
-                         struct netr_SamInfo3 *info3,
-                         const char *name_domain,
-                         const char *name_user)
+static NTSTATUS append_auth_data(struct winbindd_cli_state *state,
+                                struct netr_SamInfo3 *info3,
+                                const char *name_domain,
+                                const char *name_user)
 {
        NTSTATUS result;
        uint32_t flags = state->request->flags;
@@ -748,10 +765,11 @@ NTSTATUS append_auth_data(struct winbindd_cli_state *state,
                       /* 8 */);
        }
 
-       if (flags & WBFLAG_PAM_INFO3_TEXT) {
-               result = append_info3_as_txt(state->mem_ctx, state, info3);
+       if (flags & WBFLAG_PAM_UNIX_NAME) {
+               result = append_unix_username(state->mem_ctx, state, info3,
+                                             name_domain, name_user);
                if (!NT_STATUS_IS_OK(result)) {
-                       DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
+                       DEBUG(10,("Failed to append Unix Username: %s\n",
                                nt_errstr(result)));
                        return result;
                }
@@ -768,11 +786,10 @@ NTSTATUS append_auth_data(struct winbindd_cli_state *state,
                }
        }
 
-       if (flags & WBFLAG_PAM_UNIX_NAME) {
-               result = append_unix_username(state->mem_ctx, state, info3,
-                                             name_domain, name_user);
+       if (flags & WBFLAG_PAM_INFO3_TEXT) {
+               result = append_info3_as_txt(state->mem_ctx, state, info3);
                if (!NT_STATUS_IS_OK(result)) {
-                       DEBUG(10,("Failed to append Unix Username: %s\n",
+                       DEBUG(10,("Failed to append INFO3 (TXT): %s\n",
                                nt_errstr(result)));
                        return result;
                }
@@ -791,69 +808,6 @@ NTSTATUS append_auth_data(struct winbindd_cli_state *state,
        return NT_STATUS_OK;
 }
 
-void winbindd_pam_auth(struct winbindd_cli_state *state)
-{
-       struct winbindd_domain *domain;
-       fstring name_domain, name_user;
-       char *mapped_user = NULL;
-       NTSTATUS result;
-       NTSTATUS name_map_status = NT_STATUS_UNSUCCESSFUL;
-
-       /* Ensure null termination */
-       state->request->data.auth.user
-               [sizeof(state->request->data.auth.user)-1]='\0';
-
-       /* Ensure null termination */
-       state->request->data.auth.pass
-               [sizeof(state->request->data.auth.pass)-1]='\0';
-
-       DEBUG(3, ("[%5lu]: pam auth %s\n", (unsigned long)state->pid,
-                 state->request->data.auth.user));
-
-       if (!check_request_flags(state->request->flags)) {
-               result = NT_STATUS_INVALID_PARAMETER_MIX;
-               goto done;
-       }
-
-       /* Parse domain and username */
-
-       name_map_status = normalize_name_unmap(state->mem_ctx,
-                                              state->request->data.auth.user,
-                                              &mapped_user);
-
-       /* If the name normalization didnt' actually do anything,
-          just use the original name */
-
-       if (!NT_STATUS_IS_OK(name_map_status) &&
-           !NT_STATUS_EQUAL(name_map_status, NT_STATUS_FILE_RENAMED))
-       {
-               mapped_user = state->request->data.auth.user;
-       }
-
-       if (!canonicalize_username(mapped_user, name_domain, name_user)) {
-               result = NT_STATUS_NO_SUCH_USER;
-               goto done;
-       }
-
-       domain = find_auth_domain(state->request->flags, name_domain);
-
-       if (domain == NULL) {
-               result = NT_STATUS_NO_SUCH_USER;
-               goto done;
-       }
-
-       sendto_domain(state, domain);
-       return;
- done:
-       set_auth_errors(state->response, result);
-       DEBUG(5, ("Plain text authentication for %s returned %s "
-                 "(PAM: %d)\n",
-                 state->request->data.auth.user,
-                 state->response->data.auth.nt_status_string,
-                 state->response->data.auth.pam_error));
-       request_error(state);
-}
-
 static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
                                              struct winbindd_cli_state *state,
                                              struct netr_SamInfo3 **info3)
@@ -861,7 +815,7 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
        NTSTATUS result = NT_STATUS_LOGON_FAILURE;
        uint16 max_allowed_bad_attempts;
        fstring name_domain, name_user;
-       DOM_SID sid;
+       struct dom_sid sid;
        enum lsa_SidType type;
        uchar new_nt_pass[NT_HASH_LEN];
        const uint8 *cached_nt_pass;
@@ -982,7 +936,9 @@ 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 & NETR_TRUST_TYPE_UPLEVEL) ||
+                   /* used to cope with the case winbindd starting without network. */
+                   !strequal(tdc_domain->domain_name, tdc_domain->dns_name))) {
 
                        uid_t uid = -1;
                        const char *cc = NULL;
@@ -1089,7 +1045,7 @@ static NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
                        DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get password properties.\n"));
                }
 
-               if ((my_info3->base.rid != DOMAIN_USER_RID_ADMIN) ||
+               if ((my_info3->base.rid != DOMAIN_RID_ADMINISTRATOR) ||
                    (password_properties & DOMAIN_PASSWORD_LOCKOUT_ADMINS)) {
                        my_info3->base.acct_flags |= ACB_AUTOLOCK;
                }
@@ -1169,6 +1125,34 @@ done:
        return result;
 }
 
+static NTSTATUS winbindd_dual_auth_passdb(TALLOC_CTX *mem_ctx,
+                                         const char *domain, const char *user,
+                                         const DATA_BLOB *challenge,
+                                         const DATA_BLOB *lm_resp,
+                                         const DATA_BLOB *nt_resp,
+                                         struct netr_SamInfo3 **pinfo3)
+{
+       struct auth_usersupplied_info *user_info = NULL;
+       NTSTATUS status;
+
+       status = make_user_info(&user_info, user, user, domain, domain,
+                               global_myname(), 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)));
+               return status;
+       }
+
+       /* 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);
+       DEBUG(10, ("Authenticated user %s\\%s successfully\n", domain, user));
+       return NT_STATUS_OK;
+}
+
 typedef        NTSTATUS (*netlogon_fn_t)(struct rpc_pipe_client *cli,
                                  TALLOC_CTX *mem_ctx,
                                  uint32 logon_parameters,
@@ -1193,7 +1177,6 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(struct winbindd_domain *domain,
        int attempts = 0;
        unsigned char local_lm_response[24];
        unsigned char local_nt_response[24];
-       struct winbindd_domain *contact_domain;
        fstring name_domain, name_user;
        bool retry;
        NTSTATUS result;
@@ -1209,8 +1192,8 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(struct winbindd_domain *domain,
 
        /* do password magic */
 
+       generate_random_buffer(chal, sizeof(chal));
 
-       generate_random_buffer(chal, 8);
        if (lp_client_ntlmv2_auth()) {
                DATA_BLOB server_chal;
                DATA_BLOB names_blob;
@@ -1264,24 +1247,13 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(struct winbindd_domain *domain,
                                           sizeof(local_nt_response));
        }
 
-       /* what domain should we contact? */
+       if (strequal(name_domain, get_global_sam_name())) {
+               DATA_BLOB chal_blob = data_blob_const(chal, sizeof(chal));
 
-       if ( IS_DC ) {
-               if (!(contact_domain = find_domain_from_name(name_domain))) {
-                       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;
-                       goto done;
-               }
-
-       } else {
-               if (is_myname(name_domain)) {
-                       DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
-                       result =  NT_STATUS_NO_SUCH_USER;
-                       goto done;
-               }
-
-               contact_domain = find_our_domain();
+               result = winbindd_dual_auth_passdb(
+                       state->mem_ctx, name_domain, name_user,
+                       &chal_blob, &lm_resp, &nt_resp, info3);
+               goto done;
        }
 
        /* check authentication loop */
@@ -1292,7 +1264,7 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(struct winbindd_domain *domain,
                ZERO_STRUCTP(my_info3);
                retry = false;
 
-               result = cm_connect_netlogon(contact_domain, &netlogon_pipe);
+               result = cm_connect_netlogon(domain, &netlogon_pipe);
 
                if (!NT_STATUS_IS_OK(result)) {
                        DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
@@ -1320,14 +1292,14 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(struct winbindd_domain *domain,
                 *  -- abartlet 21 April 2008
                 */
 
-               logon_fn = contact_domain->can_do_samlogon_ex
+               logon_fn = domain->can_do_samlogon_ex
                        ? rpccli_netlogon_sam_network_logon_ex
                        : rpccli_netlogon_sam_network_logon;
 
                result = logon_fn(netlogon_pipe,
                                  state->mem_ctx,
                                  0,
-                                 contact_domain->dcname, /* server name */
+                                 domain->dcname,         /* server name */
                                  name_user,              /* user name */
                                  name_domain,            /* target domain */
                                  global_myname(),        /* workstation */
@@ -1338,10 +1310,10 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(struct winbindd_domain *domain,
                attempts += 1;
 
                if ((NT_STATUS_V(result) == DCERPC_FAULT_OP_RNG_ERROR)
-                   && contact_domain->can_do_samlogon_ex) {
+                   && domain->can_do_samlogon_ex) {
                        DEBUG(3, ("Got a DC that can not do NetSamLogonEx, "
                                  "retrying with NetSamLogon\n"));
-                       contact_domain->can_do_samlogon_ex = false;
+                       domain->can_do_samlogon_ex = false;
                        retry = true;
                        continue;
                }
@@ -1350,7 +1322,7 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(struct winbindd_domain *domain,
                   might not yet have noticed that the DC has killed
                   our connection. */
 
-               if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
+               if (!rpccli_is_connected(netlogon_pipe)) {
                        retry = true;
                        continue;
                }
@@ -1366,7 +1338,7 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(struct winbindd_domain *domain,
                                "password was changed and we didn't know it. "
                                 "Killing connections to domain %s\n",
                                name_domain));
-                       invalidate_cm_connection(&contact_domain->conn);
+                       invalidate_cm_connection(&domain->conn);
                        retry = true;
                }
 
@@ -1385,7 +1357,7 @@ static NTSTATUS winbindd_dual_pam_auth_samlogon(struct winbindd_domain *domain,
                NTSTATUS status_tmp;
                uint32 acct_flags;
 
-               status_tmp = cm_connect_sam(contact_domain, state->mem_ctx,
+               status_tmp = cm_connect_sam(domain, state->mem_ctx,
                                            &samr_pipe, &samr_domain_handle);
 
                if (!NT_STATUS_IS_OK(status_tmp)) {
@@ -1480,12 +1452,14 @@ enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
        parse_domain_user(mapped_user, name_domain, name_user);
 
        if ( mapped_user != state->request->data.auth.user ) {
-               fstr_sprintf( domain_user, "%s\\%s", name_domain, name_user );
+               fstr_sprintf( domain_user, "%s%c%s", name_domain,
+                       *lp_winbind_separator(),
+                       name_user );
                safe_strcpy( state->request->data.auth.user, domain_user,
                             sizeof(state->request->data.auth.user)-1 );
        }
 
-       if (domain->online == false) {
+       if (!domain->online) {
                result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
                if (domain->startup) {
                        /* Logons are very important to users. If we're offline and
@@ -1539,7 +1513,7 @@ enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
                    NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED) ||
                    NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) ||
                    NT_STATUS_EQUAL(result, NT_STATUS_WRONG_PASSWORD)) {
-                       goto process_result;
+                       goto done;
                }
 
                if (state->request->flags & WBFLAG_PAM_FALLBACK_AFTER_KRB5) {
@@ -1602,7 +1576,7 @@ process_result:
 
        if (NT_STATUS_IS_OK(result)) {
 
-               DOM_SID user_sid;
+               struct dom_sid user_sid;
 
                /* In all codepaths where result == NT_STATUS_OK info3 must have
                   been initialized. */
@@ -1627,8 +1601,10 @@ process_result:
 
                /* Check if the user is in the right group */
 
-               if (!NT_STATUS_IS_OK(result = check_info3_in_group(state->mem_ctx, info3,
-                                       state->request->data.auth.require_membership_of_sid))) {
+               result = check_info3_in_group(
+                       info3,
+                       state->request->data.auth.require_membership_of_sid);
+               if (!NT_STATUS_IS_OK(result)) {
                        DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
                                  state->request->data.auth.user,
                                  state->request->data.auth.require_membership_of_sid));
@@ -1643,30 +1619,12 @@ process_result:
 
                if ((state->request->flags & WBFLAG_PAM_CACHED_LOGIN)) {
 
-                       /* Store in-memory creds for single-signon using ntlm_auth. */
-                       result = winbindd_add_memory_creds(state->request->data.auth.user,
-                                                       get_uid_from_state(state),
-                                                       state->request->data.auth.pass);
-
-                       if (!NT_STATUS_IS_OK(result)) {
-                               DEBUG(10,("Failed to store memory creds: %s\n", nt_errstr(result)));
-                               goto done;
-                       }
-
                        if (lp_winbind_offline_logon()) {
                                result = winbindd_store_creds(domain,
                                                      state->mem_ctx,
                                                      state->request->data.auth.user,
                                                      state->request->data.auth.pass,
-                                                     info3, NULL);
-                               if (!NT_STATUS_IS_OK(result)) {
-
-                                       /* Release refcount. */
-                                       winbindd_delete_memory_creds(state->request->data.auth.user);
-
-                                       DEBUG(10,("Failed to store creds: %s\n", nt_errstr(result)));
-                                       goto done;
-                               }
+                                                     info3);
                        }
                }
 
@@ -1714,77 +1672,6 @@ done:
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
 
-
-/**********************************************************************
- Challenge Response Authentication Protocol
-**********************************************************************/
-
-void winbindd_pam_auth_crap(struct winbindd_cli_state *state)
-{
-       struct winbindd_domain *domain = NULL;
-       const char *domain_name = NULL;
-       NTSTATUS result;
-
-       if (!check_request_flags(state->request->flags)) {
-               result = NT_STATUS_INVALID_PARAMETER_MIX;
-               goto done;
-       }
-
-       if (!state->privileged) {
-               DEBUG(2, ("winbindd_pam_auth_crap: non-privileged access "
-                         "denied.  !\n"));
-               DEBUGADD(2, ("winbindd_pam_auth_crap: Ensure permissions "
-                            "on %s are set correctly.\n",
-                            get_winbind_priv_pipe_dir()));
-               /* send a better message than ACCESS_DENIED */
-               fstr_sprintf(state->response->data.auth.error_string,
-                            "winbind client not authorized to use "
-                            "winbindd_pam_auth_crap. Ensure permissions on "
-                            "%s are set correctly.",
-                            get_winbind_priv_pipe_dir());
-               result = NT_STATUS_ACCESS_DENIED;
-               goto done;
-       }
-
-       /* Ensure null termination */
-       state->request->data.auth_crap.user
-               [sizeof(state->request->data.auth_crap.user)-1]=0;
-       state->request->data.auth_crap.domain
-               [sizeof(state->request->data.auth_crap.domain)-1]=0;
-
-       DEBUG(3, ("[%5lu]: pam auth crap domain: [%s] user: %s\n",
-                 (unsigned long)state->pid,
-                 state->request->data.auth_crap.domain,
-                 state->request->data.auth_crap.user));
-
-       if (*state->request->data.auth_crap.domain != '\0') {
-               domain_name = state->request->data.auth_crap.domain;
-       } else if (lp_winbind_use_default_domain()) {
-               domain_name = lp_workgroup();
-       }
-
-       if (domain_name != NULL)
-               domain = find_auth_domain(state->request->flags, domain_name);
-
-       if (domain != NULL) {
-               sendto_domain(state, domain);
-               return;
-       }
-
-       result = NT_STATUS_NO_SUCH_USER;
-
- done:
-       set_auth_errors(state->response, result);
-       DEBUG(5, ("CRAP authentication for %s\\%s returned %s (PAM: %d)\n",
-                 state->request->data.auth_crap.domain,
-                 state->request->data.auth_crap.user,
-                 state->response->data.auth.nt_status_string,
-                 state->response->data.auth.pam_error));
-       request_error(state);
-       return;
-}
-
-
 enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
                                                 struct winbindd_cli_state *state)
 {
@@ -1794,7 +1681,6 @@ 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;
-       struct winbindd_domain *contact_domain;
        int attempts = 0;
        bool retry;
 
@@ -1859,22 +1745,15 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
                                           state->request->data.auth_crap.nt_resp_len);
        }
 
-       /* what domain should we contact? */
+       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));
 
-       if ( IS_DC ) {
-               if (!(contact_domain = find_domain_from_name(name_domain))) {
-                       DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n",
-                                 state->request->data.auth_crap.user, name_domain, name_user, name_domain));
-                       result = NT_STATUS_NO_SUCH_USER;
-                       goto done;
-               }
-       } else {
-               if (is_myname(name_domain)) {
-                       DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
-                       result =  NT_STATUS_NO_SUCH_USER;
-                       goto done;
-               }
-               contact_domain = find_our_domain();
+               result = winbindd_dual_auth_passdb(
+                       state->mem_ctx, name_domain, name_user,
+                       &chal_blob, &lm_resp, &nt_resp, &info3);
+               goto process_result;
        }
 
        do {
@@ -1883,7 +1762,7 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
                retry = false;
 
                netlogon_pipe = NULL;
-               result = cm_connect_netlogon(contact_domain, &netlogon_pipe);
+               result = cm_connect_netlogon(domain, &netlogon_pipe);
 
                if (!NT_STATUS_IS_OK(result)) {
                        DEBUG(3, ("could not open handle to NETLOGON pipe (error: %s)\n",
@@ -1891,14 +1770,14 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
                        goto done;
                }
 
-               logon_fn = contact_domain->can_do_samlogon_ex
+               logon_fn = domain->can_do_samlogon_ex
                        ? rpccli_netlogon_sam_network_logon_ex
                        : rpccli_netlogon_sam_network_logon;
 
                result = logon_fn(netlogon_pipe,
                                  state->mem_ctx,
                                  state->request->data.auth_crap.logon_parameters,
-                                 contact_domain->dcname,
+                                 domain->dcname,
                                  name_user,
                                  name_domain,
                                  /* Bug #3248 - found by Stefan Burkei. */
@@ -1909,10 +1788,10 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
                                  &info3);
 
                if ((NT_STATUS_V(result) == DCERPC_FAULT_OP_RNG_ERROR)
-                   && contact_domain->can_do_samlogon_ex) {
+                   && domain->can_do_samlogon_ex) {
                        DEBUG(3, ("Got a DC that can not do NetSamLogonEx, "
                                  "retrying with NetSamLogon\n"));
-                       contact_domain->can_do_samlogon_ex = false;
+                       domain->can_do_samlogon_ex = false;
                        retry = true;
                        continue;
                }
@@ -1923,7 +1802,7 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
                   might not yet have noticed that the DC has killed
                   our connection. */
 
-               if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
+               if (!rpccli_is_connected(netlogon_pipe)) {
                        retry = true;
                        continue;
                }
@@ -1938,12 +1817,14 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
                                "password was changed and we didn't know it. "
                                 "Killing connections to domain %s\n",
                                name_domain));
-                       invalidate_cm_connection(&contact_domain->conn);
+                       invalidate_cm_connection(&domain->conn);
                        retry = true;
                }
 
        } while ( (attempts < 2) && retry );
 
+process_result:
+
        if (NT_STATUS_IS_OK(result)) {
 
                wcache_invalidate_samlogon(find_domain_from_name(name_domain), info3);
@@ -1951,8 +1832,10 @@ enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
 
                /* Check if the user is in the right group */
 
-               if (!NT_STATUS_IS_OK(result = check_info3_in_group(state->mem_ctx, info3,
-                                                       state->request->data.auth_crap.require_membership_of_sid))) {
+               result = check_info3_in_group(
+                       info3,
+                       state->request->data.auth_crap.require_membership_of_sid);
+               if (!NT_STATUS_IS_OK(result)) {
                        DEBUG(3, ("User %s is not in the required group (%s), so "
                                  "crap authentication is rejected\n",
                                  state->request->data.auth_crap.user,
@@ -1991,72 +1874,21 @@ done:
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
 
-/* Change a user password */
-
-void winbindd_pam_chauthtok(struct winbindd_cli_state *state)
-{
-       fstring domain, user;
-       char *mapped_user;
-       struct winbindd_domain *contact_domain;
-       NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
-
-       DEBUG(3, ("[%5lu]: pam chauthtok %s\n", (unsigned long)state->pid,
-               state->request->data.chauthtok.user));
-
-       /* Setup crap */
-
-       nt_status = normalize_name_unmap(state->mem_ctx,
-                                        state->request->data.chauthtok.user,
-                                        &mapped_user);
-
-       /* Update the chauthtok name if we did any mapping */
-
-       if (NT_STATUS_IS_OK(nt_status) ||
-           NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED))
-       {
-               fstrcpy(state->request->data.chauthtok.user, mapped_user);
-       }
-
-       /* Must pass in state->...chauthtok.user because
-          canonicalize_username() assumes an fstring().  Since
-          we have already copied it (if necessary), this is ok. */
-
-       if (!canonicalize_username(state->request->data.chauthtok.user, domain, user)) {
-               set_auth_errors(state->response, NT_STATUS_NO_SUCH_USER);
-               DEBUG(5, ("winbindd_pam_chauthtok: canonicalize_username %s failed with %s"
-                         "(PAM: %d)\n",
-                         state->request->data.auth.user,
-                         state->response->data.auth.nt_status_string,
-                         state->response->data.auth.pam_error));
-               request_error(state);
-               return;
-       }
-
-       contact_domain = find_domain_from_name(domain);
-       if (!contact_domain) {
-               set_auth_errors(state->response, NT_STATUS_NO_SUCH_USER);
-               DEBUG(3, ("Cannot change password for [%s] -> [%s]\\[%s] as %s is not a trusted domain\n",
-                         state->request->data.chauthtok.user, domain, user, domain));
-               request_error(state);
-               return;
-       }
-
-       sendto_domain(state, contact_domain);
-}
-
 enum winbindd_result winbindd_dual_pam_chauthtok(struct winbindd_domain *contact_domain,
                                                 struct winbindd_cli_state *state)
 {
        char *oldpass;
        char *newpass = NULL;
        struct policy_handle dom_pol;
-       struct rpc_pipe_client *cli;
+       struct rpc_pipe_client *cli = NULL;
        bool got_info = false;
        struct samr_DomInfo1 *info = NULL;
-       struct samr_ChangeReject *reject = NULL;
+       struct userPwdChangeFailureInformation *reject = NULL;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        fstring domain, user;
 
+       ZERO_STRUCT(dom_pol);
+
        DEBUG(3, ("[%5lu]: dual pam chauthtok %s\n", (unsigned long)state->pid,
                  state->request->data.auth.user));
 
@@ -2095,7 +1927,7 @@ enum winbindd_result winbindd_dual_pam_chauthtok(struct winbindd_domain *contact
                fill_in_password_policy(state->response, info);
 
                state->response->data.auth.reject_reason =
-                       reject->reason;
+                       reject->extendedFailureReason;
 
                got_info = true;
        }
@@ -2127,26 +1959,6 @@ enum winbindd_result winbindd_dual_pam_chauthtok(struct winbindd_domain *contact
 done:
 
        if (NT_STATUS_IS_OK(result) && (state->request->flags & WBFLAG_PAM_CACHED_LOGIN)) {
-
-               /* Update the single sign-on memory creds. */
-               result = winbindd_replace_memory_creds(state->request->data.chauthtok.user,
-                                                       newpass);
-
-               /* When we login from gdm or xdm and password expires,
-                * we change password, but there are no memory crendentials
-                * So, winbindd_replace_memory_creds() returns
-                * NT_STATUS_OBJECT_NAME_NOT_FOUND. This is not a failure.
-                * --- BoYang
-                * */
-               if (NT_STATUS_EQUAL(result, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
-                       result = NT_STATUS_OK;
-               }
-
-               if (!NT_STATUS_IS_OK(result)) {
-                       DEBUG(10,("Failed to replace memory creds: %s\n", nt_errstr(result)));
-                       goto process_result;
-               }
-
                if (lp_winbind_offline_logon()) {
                        result = winbindd_update_creds_by_name(contact_domain,
                                                         state->mem_ctx, user,
@@ -2187,6 +1999,16 @@ done:
 
 process_result:
 
+       if (strequal(contact_domain->name, get_global_sam_name())) {
+               /* FIXME: internal rpc pipe does not cache handles yet */
+               if (cli) {
+                       if (is_valid_policy_hnd(&dom_pol)) {
+                               rpccli_samr_Close(cli, state->mem_ctx, &dom_pol);
+                       }
+                       TALLOC_FREE(cli);
+               }
+       }
+
        set_auth_errors(state->response, result);
 
        DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,
@@ -2199,72 +2021,6 @@ process_result:
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
 
-void winbindd_pam_logoff(struct winbindd_cli_state *state)
-{
-       struct winbindd_domain *domain;
-       fstring name_domain, user;
-       uid_t caller_uid = (uid_t)-1;
-       uid_t request_uid = state->request->data.logoff.uid;
-
-       DEBUG(3, ("[%5lu]: pam logoff %s\n", (unsigned long)state->pid,
-               state->request->data.logoff.user));
-
-       /* Ensure null termination */
-       state->request->data.logoff.user
-               [sizeof(state->request->data.logoff.user)-1]='\0';
-
-       state->request->data.logoff.krb5ccname
-               [sizeof(state->request->data.logoff.krb5ccname)-1]='\0';
-
-       if (request_uid == (gid_t)-1) {
-               goto failed;
-       }
-
-       if (!canonicalize_username(state->request->data.logoff.user, name_domain, user)) {
-               goto failed;
-       }
-
-       if ((domain = find_auth_domain(state->request->flags,
-                                      name_domain)) == NULL) {
-               goto failed;
-       }
-
-       if ((sys_getpeereid(state->sock, &caller_uid)) != 0) {
-               DEBUG(1,("winbindd_pam_logoff: failed to check peerid: %s\n",
-                       strerror(errno)));
-               goto failed;
-       }
-
-       switch (caller_uid) {
-               case -1:
-                       goto failed;
-               case 0:
-                       /* root must be able to logoff any user - gd */
-                       state->request->data.logoff.uid = request_uid;
-                       break;
-               default:
-                       if (caller_uid != request_uid) {
-                               DEBUG(1,("winbindd_pam_logoff: caller requested invalid uid\n"));
-                               goto failed;
-                       }
-                       state->request->data.logoff.uid = caller_uid;
-                       break;
-       }
-
-       sendto_domain(state, domain);
-       return;
-
- failed:
-       set_auth_errors(state->response, NT_STATUS_NO_SUCH_USER);
-       DEBUG(5, ("Pam Logoff for %s returned %s "
-                 "(PAM: %d)\n",
-                 state->request->data.logoff.user,
-                 state->response->data.auth.nt_status_string,
-                 state->response->data.auth.pam_error));
-       request_error(state);
-       return;
-}
-
 enum winbindd_result winbindd_dual_pam_logoff(struct winbindd_domain *domain,
                                              struct winbindd_cli_state *state)
 {
@@ -2319,7 +2075,6 @@ enum winbindd_result winbindd_dual_pam_logoff(struct winbindd_domain *domain,
 
 process_result:
 
-       winbindd_delete_memory_creds(state->request->data.logoff.user);
 
        set_auth_errors(state->response, result);
 
@@ -2328,48 +2083,6 @@ process_result:
 
 /* Change user password with auth crap*/
 
-void winbindd_pam_chng_pswd_auth_crap(struct winbindd_cli_state *state)
-{
-       struct winbindd_domain *domain = NULL;
-       const char *domain_name = NULL;
-
-       /* Ensure null termination */
-       state->request->data.chng_pswd_auth_crap.user[
-               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;
-
-       DEBUG(3, ("[%5lu]: pam change pswd auth crap domain: %s user: %s\n",
-                 (unsigned long)state->pid,
-                 state->request->data.chng_pswd_auth_crap.domain,
-                 state->request->data.chng_pswd_auth_crap.user));
-
-       if (*state->request->data.chng_pswd_auth_crap.domain != '\0') {
-               domain_name = state->request->data.chng_pswd_auth_crap.domain;
-       } else if (lp_winbind_use_default_domain()) {
-               domain_name = lp_workgroup();
-       }
-
-       if (domain_name != NULL)
-               domain = find_domain_from_name(domain_name);
-
-       if (domain != NULL) {
-               DEBUG(7, ("[%5lu]: pam auth crap changing pswd in domain: "
-                         "%s\n", (unsigned long)state->pid,domain->name));
-               sendto_domain(state, domain);
-               return;
-       }
-
-       set_auth_errors(state->response, NT_STATUS_NO_SUCH_USER);
-       DEBUG(5, ("CRAP change password  for %s\\%s returned %s (PAM: %d)\n",
-                 state->request->data.chng_pswd_auth_crap.domain,
-                 state->request->data.chng_pswd_auth_crap.user,
-                 state->response->data.auth.nt_status_string,
-                 state->response->data.auth.pam_error));
-       request_error(state);
-       return;
-}
-
 enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domain *domainSt, struct winbindd_cli_state *state)
 {
        NTSTATUS result;
@@ -2380,7 +2093,9 @@ enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domai
        fstring  domain,user;
        struct policy_handle dom_pol;
        struct winbindd_domain *contact_domain = domainSt;
-       struct rpc_pipe_client *cli;
+       struct rpc_pipe_client *cli = NULL;
+
+       ZERO_STRUCT(dom_pol);
 
        /* Ensure null termination */
        state->request->data.chng_pswd_auth_crap.user[
@@ -2429,24 +2144,20 @@ enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domai
                  (unsigned long)state->pid, domain, user));
 
        /* Change password */
-       new_nt_password = data_blob_talloc(
-               state->mem_ctx,
+       new_nt_password = data_blob_const(
                state->request->data.chng_pswd_auth_crap.new_nt_pswd,
                state->request->data.chng_pswd_auth_crap.new_nt_pswd_len);
 
-       old_nt_hash_enc = data_blob_talloc(
-               state->mem_ctx,
+       old_nt_hash_enc = data_blob_const(
                state->request->data.chng_pswd_auth_crap.old_nt_hash_enc,
                state->request->data.chng_pswd_auth_crap.old_nt_hash_enc_len);
 
        if(state->request->data.chng_pswd_auth_crap.new_lm_pswd_len > 0)        {
-               new_lm_password = data_blob_talloc(
-                       state->mem_ctx,
+               new_lm_password = data_blob_const(
                        state->request->data.chng_pswd_auth_crap.new_lm_pswd,
                        state->request->data.chng_pswd_auth_crap.new_lm_pswd_len);
 
-               old_lm_hash_enc = data_blob_talloc(
-                       state->mem_ctx,
+               old_lm_hash_enc = data_blob_const(
                        state->request->data.chng_pswd_auth_crap.old_lm_hash_enc,
                        state->request->data.chng_pswd_auth_crap.old_lm_hash_enc_len);
        } else {
@@ -2468,6 +2179,16 @@ enum winbindd_result winbindd_dual_pam_chng_pswd_auth_crap(struct winbindd_domai
 
  done:
 
+       if (strequal(contact_domain->name, get_global_sam_name())) {
+               /* FIXME: internal rpc pipe does not cache handles yet */
+               if (cli) {
+                       if (is_valid_policy_hnd(&dom_pol)) {
+                               rpccli_samr_Close(cli, state->mem_ctx, &dom_pol);
+                       }
+                       TALLOC_FREE(cli);
+               }
+       }
+
        set_auth_errors(state->response, result);
 
        DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2,