Finish removal of iconv_convenience in public API's.
[amitay/samba.git] / source4 / winbind / wb_pam_auth.c
index 4874254eff91002977f9f115adc79b7df41b872a..9346cd5c384c102dafeb2a04b28ab7c00c347bab 100644 (file)
 #include "auth/credentials/credentials.h"
 #include "libcli/auth/libcli_auth.h"
 #include "librpc/gen_ndr/ndr_netlogon.h"
-#include "librpc/gen_ndr/ndr_netlogon_c.h"
-#include "librpc/gen_ndr/winbind.h"
+#include "param/param.h"
 
 /* Oh, there is so much to keep an eye on when authenticating a user.  Oh my! */
 struct pam_auth_crap_state {
        struct composite_context *ctx;
-       struct event_context *event_ctx;
+       struct tevent_context *event_ctx;
+       struct loadparm_context *lp_ctx;
 
        struct winbind_SamLogon *req;
        char *unix_username;
@@ -76,6 +76,7 @@ struct composite_context *wb_cmd_pam_auth_crap_send(TALLOC_CTX *mem_ctx,
        state = talloc(result, struct pam_auth_crap_state);
        if (state == NULL) goto failed;
        state->ctx = result;
+       state->lp_ctx = service->task->lp_ctx;
        result->private_data = state;
 
        state->req = talloc(state, struct winbind_SamLogon);
@@ -130,6 +131,7 @@ struct composite_context *wb_cmd_pam_auth_crap_send(TALLOC_CTX *mem_ctx,
 static void pam_auth_crap_recv_logon(struct composite_context *ctx)
 {
        DATA_BLOB tmp_blob;
+       enum ndr_err_code ndr_err;
        struct netr_SamBaseInfo *base;
        struct pam_auth_crap_state *state =
                talloc_get_type(ctx->async.private_data,
@@ -138,10 +140,13 @@ static void pam_auth_crap_recv_logon(struct composite_context *ctx)
        state->ctx->status = wb_sam_logon_recv(ctx, state, state->req);
        if (!composite_is_ok(state->ctx)) return;
 
-       state->ctx->status = ndr_push_struct_blob(
+       ndr_err = ndr_push_struct_blob(
                &tmp_blob, state, state->req->out.validation.sam3,
                (ndr_push_flags_fn_t)ndr_push_netr_SamInfo3);
-       if (!composite_is_ok(state->ctx)) return;
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               state->ctx->status = ndr_map_error2ntstatus(ndr_err);
+               if (!composite_is_ok(state->ctx)) return;
+       }
 
        /* The Samba3 protocol is a bit broken (due to non-IDL
         * heritage, so for compatability we must add a non-zero 4
@@ -171,7 +176,7 @@ static void pam_auth_crap_recv_logon(struct composite_context *ctx)
 
        state->unix_username = talloc_asprintf(state, "%s%s%s", 
                                               state->domain_name,
-                                              lp_winbind_separator(),
+                                              lp_winbind_separator(state->lp_ctx),
                                               state->user_name);
        if (composite_nomem(state->unix_username, state->ctx)) return;
 
@@ -206,36 +211,23 @@ NTSTATUS wb_cmd_pam_auth_crap_recv(struct composite_context *c,
 
 struct composite_context *wb_cmd_pam_auth_send(TALLOC_CTX *mem_ctx,
                                               struct wbsrv_service *service,
-                                              const char *domain,
-                                              const char *user,
-                                              const char *password)
+                                              struct cli_credentials *credentials)
 {
-       struct cli_credentials *credentials;
        const char *workstation;
        NTSTATUS status;
-
+       const char *user, *domain;
        DATA_BLOB chal, nt_resp, lm_resp, names_blob;
        int flags = CLI_CRED_NTLM_AUTH;
-       if (lp_client_lanman_auth()) {
+       if (lp_client_lanman_auth(service->task->lp_ctx)) {
                flags |= CLI_CRED_LANMAN_AUTH;
        }
 
-       if (lp_client_ntlmv2_auth()) {
+       if (lp_client_ntlmv2_auth(service->task->lp_ctx)) {
                flags |= CLI_CRED_NTLMv2_AUTH;
        }
 
        DEBUG(5, ("wbsrv_samba3_pam_auth called\n"));
 
-       credentials = cli_credentials_init(mem_ctx);
-       if (!credentials) {
-               return NULL;
-       }
-       cli_credentials_set_conf(credentials);
-       cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
-       cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
-
-       cli_credentials_set_password(credentials, password, CRED_SPECIFIED);
-
        chal = data_blob_talloc(mem_ctx, NULL, 8);
        if (!chal.data) {
                return NULL;
@@ -260,16 +252,36 @@ struct composite_context *wb_cmd_pam_auth_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
        return wb_cmd_pam_auth_crap_send(mem_ctx, service,
-                                        0 /* logon parameters */, 
+                                        MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT|MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT /* logon parameters */, 
                                         domain, user, workstation,
                                         chal, nt_resp, lm_resp);
 }
 
-NTSTATUS wb_cmd_pam_auth_recv(struct composite_context *c)
+NTSTATUS wb_cmd_pam_auth_recv(struct composite_context *c,
+                             TALLOC_CTX *mem_ctx,
+                             DATA_BLOB *info3,
+                             struct netr_UserSessionKey *user_session_key,
+                             struct netr_LMSessionKey *lm_key,
+                             char **unix_username)
 {
-       struct pam_auth_crap_state *state =
-               talloc_get_type(c->private_data, struct pam_auth_crap_state);
-       NTSTATUS status = composite_wait(c);
-       talloc_free(state);
-       return status;
+       struct pam_auth_crap_state *state =
+               talloc_get_type(c->private_data, struct pam_auth_crap_state);
+       NTSTATUS status = composite_wait(c);
+       if (NT_STATUS_IS_OK(status)) {
+               if (info3) {
+                       info3->length = state->info3.length;
+                       info3->data = talloc_steal(mem_ctx, state->info3.data);
+               }
+               if (user_session_key) {
+                       *user_session_key = state->user_session_key;
+               }
+               if (lm_key) {
+                       *lm_key = state->lm_key;
+               }
+               if (unix_username) {
+                       *unix_username = talloc_steal(mem_ctx, state->unix_username);
+               }
+       }
+       talloc_free(state);
+       return status;
 }