Make Samba3 use the new common libcli/auth code
[ira/wip.git] / source3 / rpc_client / cli_pipe.c
index dbd6930718787596e5fefc39b4dd1f784a728cd6..567794db88d8f0e0f44eab958260250210d4444c 100644 (file)
@@ -18,6 +18,7 @@
  */
 
 #include "includes.h"
+#include "../libcli/auth/libcli_auth.h"
 #include "librpc/gen_ndr/cli_epmapper.h"
 
 #undef DBGC_CLASS
@@ -1034,7 +1035,7 @@ struct cli_api_pipe_state {
        uint32_t rdata_len;
 };
 
-static void cli_api_pipe_trans_done(struct async_req *subreq);
+static void cli_api_pipe_trans_done(struct tevent_req *subreq);
 static void cli_api_pipe_write_done(struct tevent_req *subreq);
 static void cli_api_pipe_read_done(struct tevent_req *subreq);
 
@@ -1044,9 +1045,7 @@ static struct tevent_req *cli_api_pipe_send(TALLOC_CTX *mem_ctx,
                                            uint8_t *data, size_t data_len,
                                            uint32_t max_rdata_len)
 {
-       struct tevent_req *req;
-       struct async_req *subreq;
-       struct tevent_req *subreq2;
+       struct tevent_req *req, *subreq;
        struct cli_api_pipe_state *state;
        NTSTATUS status;
 
@@ -1071,11 +1070,9 @@ static struct tevent_req *cli_api_pipe_send(TALLOC_CTX *mem_ctx,
                subreq = transport->trans_send(state, ev, data, data_len,
                                               max_rdata_len, transport->priv);
                if (subreq == NULL) {
-                       status = NT_STATUS_NO_MEMORY;
-                       goto post_status;
+                       goto fail;
                }
-               subreq->async.fn = cli_api_pipe_trans_done;
-               subreq->async.priv = req;
+               tevent_req_set_callback(subreq, cli_api_pipe_trans_done, req);
                return req;
        }
 
@@ -1084,31 +1081,27 @@ static struct tevent_req *cli_api_pipe_send(TALLOC_CTX *mem_ctx,
         * example the ncacn_ip_tcp transport, do the write/read step here.
         */
 
-       subreq2 = rpc_write_send(state, ev, transport, data, data_len);
-       if (subreq2 == NULL) {
+       subreq = rpc_write_send(state, ev, transport, data, data_len);
+       if (subreq == NULL) {
                goto fail;
        }
-       tevent_req_set_callback(subreq2, cli_api_pipe_write_done, req);
+       tevent_req_set_callback(subreq, cli_api_pipe_write_done, req);
        return req;
 
        status = NT_STATUS_INVALID_PARAMETER;
 
  post_status:
-       if (NT_STATUS_IS_OK(status)) {
-               tevent_req_done(req);
-       } else {
-               tevent_req_nterror(req, status);
-       }
+       tevent_req_nterror(req, status);
        return tevent_req_post(req, ev);
  fail:
        TALLOC_FREE(req);
        return NULL;
 }
 
-static void cli_api_pipe_trans_done(struct async_req *subreq)
+static void cli_api_pipe_trans_done(struct tevent_req *subreq)
 {
-       struct tevent_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct tevent_req);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
        struct cli_api_pipe_state *state = tevent_req_data(
                req, struct cli_api_pipe_state);
        NTSTATUS status;
@@ -2299,19 +2292,24 @@ NTSTATUS rpc_api_pipe_req(TALLOC_CTX *mem_ctx, struct rpc_pipe_client *cli,
        TALLOC_CTX *frame = talloc_stackframe();
        struct event_context *ev;
        struct tevent_req *req;
-       NTSTATUS status = NT_STATUS_NO_MEMORY;
+       NTSTATUS status = NT_STATUS_OK;
 
        ev = event_context_init(frame);
        if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
        req = rpc_api_pipe_req_send(frame, ev, cli, op_num, in_data);
        if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
-       tevent_req_poll(req, ev);
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
+       }
 
        status = rpc_api_pipe_req_recv(req, mem_ctx, out_data);
  fail:
@@ -2513,29 +2511,28 @@ static int rpc_pipe_bind_state_destructor(struct rpc_pipe_bind_state *state)
 }
 
 static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq);
-static NTSTATUS rpc_finish_auth3_bind_send(struct async_req *req,
+static NTSTATUS rpc_finish_auth3_bind_send(struct tevent_req *req,
                                           struct rpc_pipe_bind_state *state,
                                           struct rpc_hdr_info *phdr,
                                           prs_struct *reply_pdu);
 static void rpc_bind_auth3_write_done(struct tevent_req *subreq);
-static NTSTATUS rpc_finish_spnego_ntlmssp_bind_send(struct async_req *req,
+static NTSTATUS rpc_finish_spnego_ntlmssp_bind_send(struct tevent_req *req,
                                                    struct rpc_pipe_bind_state *state,
                                                    struct rpc_hdr_info *phdr,
                                                    prs_struct *reply_pdu);
 static void rpc_bind_ntlmssp_api_done(struct tevent_req *subreq);
 
-struct async_req *rpc_pipe_bind_send(TALLOC_CTX *mem_ctx,
-                                    struct event_context *ev,
-                                    struct rpc_pipe_client *cli,
-                                    struct cli_pipe_auth_data *auth)
+struct tevent_req *rpc_pipe_bind_send(TALLOC_CTX *mem_ctx,
+                                     struct event_context *ev,
+                                     struct rpc_pipe_client *cli,
+                                     struct cli_pipe_auth_data *auth)
 {
-       struct async_req *result;
-       struct tevent_req *subreq;
+       struct tevent_req *req, *subreq;
        struct rpc_pipe_bind_state *state;
        NTSTATUS status;
 
-       if (!async_req_setup(mem_ctx, &result, &state,
-                            struct rpc_pipe_bind_state)) {
+       req = tevent_req_create(mem_ctx, &state, struct rpc_pipe_bind_state);
+       if (req == NULL) {
                return NULL;
        }
 
@@ -2570,24 +2567,23 @@ struct async_req *rpc_pipe_bind_send(TALLOC_CTX *mem_ctx,
        if (subreq == NULL) {
                goto fail;
        }
-       tevent_req_set_callback(subreq, rpc_pipe_bind_step_one_done, result);
-       return result;
+       tevent_req_set_callback(subreq, rpc_pipe_bind_step_one_done, req);
+       return req;
 
  post_status:
-       if (async_post_ntstatus(result, ev, status)) {
-               return result;
-       }
+       tevent_req_nterror(req, status);
+       return tevent_req_post(req, ev);
  fail:
-       TALLOC_FREE(result);
+       TALLOC_FREE(req);
        return NULL;
 }
 
 static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
 {
-       struct async_req *req = tevent_req_callback_data(
-               subreq, struct async_req);
-       struct rpc_pipe_bind_state *state = talloc_get_type_abort(
-               req->private_data, struct rpc_pipe_bind_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct rpc_pipe_bind_state *state = tevent_req_data(
+               req, struct rpc_pipe_bind_state);
        prs_struct reply_pdu;
        struct rpc_hdr_info hdr;
        struct rpc_hdr_ba_info hdr_ba;
@@ -2599,7 +2595,7 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
                DEBUG(3, ("rpc_pipe_bind: %s bind request returned %s\n",
                          rpccli_pipe_txt(debug_ctx(), state->cli),
                          nt_errstr(status)));
-               async_req_nterror(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
@@ -2607,7 +2603,7 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
        if (!smb_io_rpc_hdr("hdr", &hdr, &reply_pdu, 0)) {
                DEBUG(0, ("rpc_pipe_bind: failed to unmarshall RPC_HDR.\n"));
                prs_mem_free(&reply_pdu);
-               async_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
+               tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
                return;
        }
 
@@ -2615,14 +2611,14 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
                DEBUG(0, ("rpc_pipe_bind: Failed to unmarshall "
                          "RPC_HDR_BA.\n"));
                prs_mem_free(&reply_pdu);
-               async_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
+               tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
                return;
        }
 
        if (!check_bind_response(&hdr_ba, &state->cli->transfer_syntax)) {
                DEBUG(2, ("rpc_pipe_bind: check_bind_response failed.\n"));
                prs_mem_free(&reply_pdu);
-               async_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
+               tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
                return;
        }
 
@@ -2639,7 +2635,7 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
        case PIPE_AUTH_TYPE_SCHANNEL:
                /* Bind complete. */
                prs_mem_free(&reply_pdu);
-               async_req_done(req);
+               tevent_req_done(req);
                break;
 
        case PIPE_AUTH_TYPE_NTLMSSP:
@@ -2648,7 +2644,7 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
                                                    &reply_pdu);
                prs_mem_free(&reply_pdu);
                if (!NT_STATUS_IS_OK(status)) {
-                       async_req_nterror(req, status);
+                       tevent_req_nterror(req, status);
                }
                break;
 
@@ -2658,7 +2654,7 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
                                                             &reply_pdu);
                prs_mem_free(&reply_pdu);
                if (!NT_STATUS_IS_OK(status)) {
-                       async_req_nterror(req, status);
+                       tevent_req_nterror(req, status);
                }
                break;
 
@@ -2669,11 +2665,11 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
                DEBUG(0,("cli_finish_bind_auth: unknown auth type %u\n",
                         (unsigned int)state->cli->auth->auth_type));
                prs_mem_free(&reply_pdu);
-               async_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
+               tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
        }
 }
 
-static NTSTATUS rpc_finish_auth3_bind_send(struct async_req *req,
+static NTSTATUS rpc_finish_auth3_bind_send(struct tevent_req *req,
                                           struct rpc_pipe_bind_state *state,
                                           struct rpc_hdr_info *phdr,
                                           prs_struct *reply_pdu)
@@ -2738,20 +2734,20 @@ static NTSTATUS rpc_finish_auth3_bind_send(struct async_req *req,
 
 static void rpc_bind_auth3_write_done(struct tevent_req *subreq)
 {
-       struct async_req *req = tevent_req_callback_data(
-               subreq, struct async_req);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
        NTSTATUS status;
 
        status = rpc_write_recv(subreq);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_nterror(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
-       async_req_done(req);
+       tevent_req_done(req);
 }
 
-static NTSTATUS rpc_finish_spnego_ntlmssp_bind_send(struct async_req *req,
+static NTSTATUS rpc_finish_spnego_ntlmssp_bind_send(struct tevent_req *req,
                                                    struct rpc_pipe_bind_state *state,
                                                    struct rpc_hdr_info *phdr,
                                                    prs_struct *reply_pdu)
@@ -2845,10 +2841,10 @@ static NTSTATUS rpc_finish_spnego_ntlmssp_bind_send(struct async_req *req,
 
 static void rpc_bind_ntlmssp_api_done(struct tevent_req *subreq)
 {
-       struct async_req *req = tevent_req_callback_data(
-               subreq, struct async_req);
-       struct rpc_pipe_bind_state *state = talloc_get_type_abort(
-               req->private_data, struct rpc_pipe_bind_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct rpc_pipe_bind_state *state = tevent_req_data(
+               req, struct rpc_pipe_bind_state);
        DATA_BLOB server_spnego_response = data_blob_null;
        DATA_BLOB tmp_blob = data_blob_null;
        prs_struct reply_pdu;
@@ -2859,7 +2855,7 @@ static void rpc_bind_ntlmssp_api_done(struct tevent_req *subreq)
        status = rpc_api_pipe_recv(subreq, talloc_tos(), &reply_pdu);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_nterror(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
@@ -2867,19 +2863,19 @@ static void rpc_bind_ntlmssp_api_done(struct tevent_req *subreq)
        if (!smb_io_rpc_hdr("rpc_hdr   ", &hdr, &reply_pdu, 0)) {
                DEBUG(0, ("rpc_finish_spnego_ntlmssp_bind: Failed to "
                          "unmarshall RPC_HDR.\n"));
-               async_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
+               tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
                return;
        }
 
        if (!prs_set_offset(
                    &reply_pdu,
                    hdr.frag_len - hdr.auth_len - RPC_HDR_AUTH_LEN)) {
-               async_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
        if (!smb_io_rpc_hdr_auth("hdr_auth", &hdr_auth, &reply_pdu, 0)) {
-               async_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
@@ -2892,7 +2888,7 @@ static void rpc_bind_ntlmssp_api_done(struct tevent_req *subreq)
                                        OID_NTLMSSP, &tmp_blob)) {
                data_blob_free(&server_spnego_response);
                data_blob_free(&tmp_blob);
-               async_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
@@ -2901,12 +2897,12 @@ static void rpc_bind_ntlmssp_api_done(struct tevent_req *subreq)
 
        DEBUG(5,("rpc_finish_spnego_ntlmssp_bind: alter context request to "
                 "%s.\n", rpccli_pipe_txt(debug_ctx(), state->cli)));
-       async_req_done(req);
+       tevent_req_done(req);
 }
 
-NTSTATUS rpc_pipe_bind_recv(struct async_req *req)
+NTSTATUS rpc_pipe_bind_recv(struct tevent_req *req)
 {
-       return async_req_simple_recv_ntstatus(req);
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
@@ -2914,21 +2910,24 @@ NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
 {
        TALLOC_CTX *frame = talloc_stackframe();
        struct event_context *ev;
-       struct async_req *req;
-       NTSTATUS status = NT_STATUS_NO_MEMORY;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_OK;
 
        ev = event_context_init(frame);
        if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
        req = rpc_pipe_bind_send(frame, ev, cli, auth);
        if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
-       while (req->state < ASYNC_REQ_DONE) {
-               event_loop_once(ev);
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
        }
 
        status = rpc_pipe_bind_recv(req);
@@ -3852,7 +3851,7 @@ NTSTATUS cli_rpc_pipe_open_schannel_with_key(struct cli_state *cli,
                                             const struct ndr_syntax_id *interface,
                                             enum pipe_auth_level auth_level,
                                             const char *domain,
-                                            const struct dcinfo *pdc,
+                                            struct netlogon_creds_CredentialState *pdc,
                                             struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
@@ -3865,7 +3864,7 @@ NTSTATUS cli_rpc_pipe_open_schannel_with_key(struct cli_state *cli,
        }
 
        status = rpccli_schannel_bind_data(result, domain, auth_level,
-                                          pdc->sess_key, &auth);
+                                          pdc->session_key, &auth);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("rpccli_schannel_bind_data returned %s\n",
                          nt_errstr(status)));
@@ -3884,11 +3883,11 @@ NTSTATUS cli_rpc_pipe_open_schannel_with_key(struct cli_state *cli,
 
        /*
         * The credentials on a new netlogon pipe are the ones we are passed
-        * in - copy them over.
+        * in - reference them in
         */
-       result->dc = (struct dcinfo *)talloc_memdup(result, pdc, sizeof(*pdc));
+       result->dc = talloc_reference(result, pdc);
        if (result->dc == NULL) {
-               DEBUG(0, ("talloc failed\n"));
+               DEBUG(0, ("talloc reference failed\n"));
                TALLOC_FREE(result);
                return NT_STATUS_NO_MEMORY;
        }