STEP03c: HACK s3 client dcerpc_connection_set_use_trans_fn
[metze/samba/wip.git] / source3 / rpc_client / cli_pipe.c
index 5e87bad46a2c251b2f6e96c3ffa443ba8696c8e0..93fb387a49d4dcf093e1a65d20a20e5a048aa31e 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include "includes.h"
+#include "libsmb/namequery.h"
 #include "../lib/util/tevent_ntstatus.h"
 #include "librpc/gen_ndr/ndr_epmapper_c.h"
 #include "../librpc/gen_ndr/ndr_dssetup.h"
@@ -35,6 +36,8 @@
 #include "auth/gensec/gensec.h"
 #include "auth/credentials/credentials.h"
 #include "../libcli/smb/smbXcli_base.h"
+#include "../libcli/smb/tstream_smbXcli_np.h"
+#include "librpc/rpc/dcerpc_connection.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_CLI
@@ -2127,8 +2130,10 @@ NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
 {
        TALLOC_CTX *frame = talloc_stackframe();
        struct tevent_context *ev;
-       struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
+       struct dcerpc_call *call;
+       struct tevent_req *subreq;
+       struct dcerpc_presentation *pres[1];
 
        ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
@@ -2136,17 +2141,28 @@ NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
                goto fail;
        }
 
-       req = rpc_pipe_bind_send(frame, ev, cli, auth);
-       if (req == NULL) {
+       cli->sec = dcerpc_security_allocate(cli, cli->conn,
+                                          auth->auth_type,
+                                          auth->auth_level,
+                                          auth->auth_ctx);
+
+       call = dcerpc_call_allocate(frame, cli->assoc, NULL, NULL);
+
+       pres[0] = cli->pres;
+
+       subreq = dcerpc_do_bind_send(frame, ev, cli->conn, call,
+                                    cli->sec, ARRAY_SIZE(pres), pres);
+
+       if (subreq == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
-       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+       if (!tevent_req_poll_ntstatus(subreq, ev, &status)) {
                goto fail;
        }
 
-       status = rpc_pipe_bind_recv(req);
+       status = dcerpc_do_bind_recv(subreq);
  fail:
        TALLOC_FREE(frame);
        return status;
@@ -2229,6 +2245,7 @@ static void rpccli_bh_auth_info(struct dcerpc_binding_handle *h,
 }
 
 struct rpccli_bh_raw_call_state {
+       struct dcerpc_call *call;
        DATA_BLOB in_data;
        DATA_BLOB out_data;
        uint32_t out_flags;
@@ -2251,6 +2268,7 @@ static struct tevent_req *rpccli_bh_raw_call_send(TALLOC_CTX *mem_ctx,
        struct rpccli_bh_raw_call_state *state;
        bool ok;
        struct tevent_req *subreq;
+       bool bigendian = false;
 
        req = tevent_req_create(mem_ctx, &state,
                                struct rpccli_bh_raw_call_state);
@@ -2266,8 +2284,20 @@ static struct tevent_req *rpccli_bh_raw_call_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
 
-       subreq = rpc_api_pipe_req_send(state, ev, hs->rpc_cli,
-                                      opnum, object, &state->in_data);
+       state->call = dcerpc_call_allocate(state,
+                                          hs->rpc_cli->assoc,
+                                          hs->rpc_cli->sec,
+                                          hs->rpc_cli->pres);
+
+       if (in_flags & LIBNDR_FLAG_BIGENDIAN) {
+               bigendian = true;
+       }
+
+       subreq = dcerpc_do_request_send(state, ev, hs->rpc_cli->conn,
+                                       state->call, object, opnum,
+                                       &state->in_data, bigendian);
+//     subreq = rpc_api_pipe_req_send(state, ev, hs->rpc_cli,
+//                                    opnum, &state->in_data);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -2285,18 +2315,25 @@ static void rpccli_bh_raw_call_done(struct tevent_req *subreq)
                tevent_req_data(req,
                struct rpccli_bh_raw_call_state);
        NTSTATUS status;
+       bool bigendian = false;
 
        state->out_flags = 0;
 
        /* TODO: support bigendian responses */
 
-       status = rpc_api_pipe_req_recv(subreq, state, &state->out_data);
+       status = dcerpc_do_request_recv(subreq, state, &state->out_data,
+                                       &bigendian);
+       //status = rpc_api_pipe_req_recv(subreq, state, &state->out_data);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
                tevent_req_nterror(req, status);
                return;
        }
 
+       if (bigendian) {
+               state->out_flags |= LIBNDR_FLAG_BIGENDIAN;
+       }
+
        tevent_req_done(req);
 }
 
@@ -2696,6 +2733,16 @@ static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
 
        result->transport->transport = NCACN_IP_TCP;
 
+       result->assoc = dcerpc_association_create(result, 0);
+       result->conn = dcerpc_connection_create(result, result->assoc,
+                                               &result->transport->stream);
+       result->pres = dcerpc_presentation_allocate(result, result->conn, table,
+                                                   &ndr_transfer_syntax_ndr);
+       result->sec = dcerpc_security_allocate(result, result->conn,
+                                              DCERPC_AUTH_TYPE_NONE,
+                                              DCERPC_AUTH_LEVEL_NONE,
+                                              NULL);
+
        result->binding_handle = rpccli_bh_create(result, NULL, table);
        if (result->binding_handle == NULL) {
                TALLOC_FREE(result);
@@ -2946,6 +2993,16 @@ NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
 
        result->transport->transport = NCALRPC;
 
+       result->assoc = dcerpc_association_create(result, 0);
+       result->conn = dcerpc_connection_create(result, result->assoc,
+                                               &result->transport->stream);
+       result->pres = dcerpc_presentation_allocate(result, result->conn, table,
+                                                   &ndr_transfer_syntax_ndr);
+       result->sec = dcerpc_security_allocate(result, result->conn,
+                                              DCERPC_AUTH_TYPE_NONE,
+                                              DCERPC_AUTH_LEVEL_NONE,
+                                              NULL);
+
        result->binding_handle = rpccli_bh_create(result, NULL, table);
        if (result->binding_handle == NULL) {
                TALLOC_FREE(result);
@@ -3025,6 +3082,18 @@ static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
 
        result->transport->transport = NCACN_NP;
 
+       result->assoc = dcerpc_association_create(result, 0);
+       result->conn = dcerpc_connection_create(result, result->assoc,
+                                               &result->transport->stream);
+       dcerpc_connection_set_use_trans_fn(result->conn,
+                                          tstream_smbXcli_np_use_trans);
+       result->pres = dcerpc_presentation_allocate(result, result->conn, table,
+                                                   &ndr_transfer_syntax_ndr);
+       result->sec = dcerpc_security_allocate(result, result->conn,
+                                              DCERPC_AUTH_TYPE_NONE,
+                                              DCERPC_AUTH_LEVEL_NONE,
+                                              NULL);
+
        np_ref = talloc(result->transport, struct rpc_pipe_client_np_ref);
        if (np_ref == NULL) {
                TALLOC_FREE(result);
@@ -3272,34 +3341,25 @@ NTSTATUS cli_rpc_pipe_open_generic_auth(struct cli_state *cli,
        return status;
 }
 
-NTSTATUS cli_rpc_pipe_open_schannel_with_creds(struct cli_state *cli,
-                                              const struct ndr_interface_table *table,
-                                              enum dcerpc_transport_t transport,
-                                              struct netlogon_creds_cli_context *netlogon_creds,
-                                              struct rpc_pipe_client **_rpccli)
+NTSTATUS cli_rpc_pipe_open_bind_schannel(
+       struct cli_state *cli,
+       const struct ndr_interface_table *table,
+       enum dcerpc_transport_t transport,
+       struct netlogon_creds_cli_context *netlogon_creds,
+       struct rpc_pipe_client **_rpccli)
 {
        struct rpc_pipe_client *rpccli;
        struct pipe_auth_data *rpcauth;
        const char *target_service = table->authservices->names[0];
-       struct netlogon_creds_CredentialState *ncreds = NULL;
        struct cli_credentials *cli_creds;
        enum dcerpc_AuthLevel auth_level;
        NTSTATUS status;
-       int rpc_pipe_bind_dbglvl = 0;
 
        status = cli_rpc_pipe_open(cli, transport, table, &rpccli);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       status = netlogon_creds_cli_lock(netlogon_creds, rpccli, &ncreds);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("netlogon_creds_cli_lock returned %s\n",
-                         nt_errstr(status)));
-               TALLOC_FREE(rpccli);
-               return status;
-       }
-
        auth_level = netlogon_creds_cli_auth_level(netlogon_creds);
 
        status = netlogon_creds_bind_cli_credentials(
@@ -3331,40 +3391,70 @@ NTSTATUS cli_rpc_pipe_open_schannel_with_creds(struct cli_state *cli,
        talloc_unlink(rpccli, cli_creds);
        cli_creds = NULL;
 
-       if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_ACCESS_DENIED)) {
-               rpc_pipe_bind_dbglvl = 1;
-               netlogon_creds_cli_delete(netlogon_creds, ncreds);
-               TALLOC_FREE(ncreds);
-       }
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(rpc_pipe_bind_dbglvl,
-                     ("%s: rpc_pipe_bind failed with error %s\n",
-                      __func__, nt_errstr(status)));
+               DBG_DEBUG("rpc_pipe_bind failed with error %s\n",
+                         nt_errstr(status));
                TALLOC_FREE(rpccli);
                return status;
        }
 
-       TALLOC_FREE(ncreds);
+       *_rpccli = rpccli;
 
-       if (!ndr_syntax_id_equal(&table->syntax_id, &ndr_table_netlogon.syntax_id)) {
-               goto done;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_rpc_pipe_open_schannel_with_creds(struct cli_state *cli,
+                                              const struct ndr_interface_table *table,
+                                              enum dcerpc_transport_t transport,
+                                              struct netlogon_creds_cli_context *netlogon_creds,
+                                              struct rpc_pipe_client **_rpccli)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct rpc_pipe_client *rpccli;
+       struct netlogon_creds_cli_lck *lck;
+       NTSTATUS status;
+
+       status = netlogon_creds_cli_lck(
+               netlogon_creds, NETLOGON_CREDS_CLI_LCK_EXCLUSIVE,
+               frame, &lck);
+       if (!NT_STATUS_IS_OK(status)) {
+               DBG_WARNING("netlogon_creds_cli_lck returned %s\n",
+                           nt_errstr(status));
+               TALLOC_FREE(frame);
+               return status;
        }
 
-       status = netlogon_creds_cli_check(netlogon_creds,
-                                         rpccli->binding_handle);
+       status = cli_rpc_pipe_open_bind_schannel(
+               cli, table, transport, netlogon_creds, &rpccli);
+       if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_ACCESS_DENIED)) {
+               netlogon_creds_cli_delete_lck(netlogon_creds);
+       }
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("netlogon_creds_cli_check failed with %s\n",
-                         nt_errstr(status)));
-               TALLOC_FREE(rpccli);
+               DBG_DEBUG("cli_rpc_pipe_open_bind_schannel failed: %s\n",
+                         nt_errstr(status));
+               TALLOC_FREE(frame);
                return status;
        }
 
+       if (ndr_syntax_id_equal(&table->syntax_id,
+                               &ndr_table_netlogon.syntax_id)) {
+               status = netlogon_creds_cli_check(netlogon_creds,
+                                                 rpccli->binding_handle,
+                                                 NULL);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0, ("netlogon_creds_cli_check failed with %s\n",
+                                 nt_errstr(status)));
+                       TALLOC_FREE(frame);
+                       return status;
+               }
+       }
 
-done:
-       DEBUG(10,("%s: opened pipe %s to machine %s "
-                 "for domain %s and bound using schannel.\n",
-                 __func__, table->name,
-                 rpccli->desthost, cli_credentials_get_domain(cli_creds)));
+       DBG_DEBUG("opened pipe %s to machine %s with key %s "
+                 "and bound using schannel.\n",
+                 table->name, rpccli->desthost,
+                 netlogon_creds_cli_debug_string(netlogon_creds, lck));
+
+       TALLOC_FREE(frame);
 
        *_rpccli = rpccli;
        return NT_STATUS_OK;