s3-dcerpc: use dcerpc_push_ncacn_packet() for create_bind_or_alt_ctx_internal().
[kamenim/samba.git] / source3 / rpc_client / cli_pipe.c
index 2f7db99f205deab49396b6351cd092a5b79afd22..d4ff89849be567ba9ce1cb4cc674da9ec87ad417 100644 (file)
 #include "includes.h"
 #include "librpc/gen_ndr/cli_epmapper.h"
 #include "../librpc/gen_ndr/ndr_schannel.h"
+#include "../librpc/gen_ndr/ndr_lsa.h"
+#include "../librpc/gen_ndr/ndr_dssetup.h"
+#include "../librpc/gen_ndr/ndr_samr.h"
+#include "../librpc/gen_ndr/ndr_netlogon.h"
+#include "../librpc/gen_ndr/ndr_srvsvc.h"
+#include "../librpc/gen_ndr/ndr_wkssvc.h"
+#include "../librpc/gen_ndr/ndr_winreg.h"
+#include "../librpc/gen_ndr/ndr_spoolss.h"
+#include "../librpc/gen_ndr/ndr_dfs.h"
+#include "../librpc/gen_ndr/ndr_echo.h"
+#include "../librpc/gen_ndr/ndr_initshutdown.h"
+#include "../librpc/gen_ndr/ndr_svcctl.h"
+#include "../librpc/gen_ndr/ndr_eventlog.h"
+#include "../librpc/gen_ndr/ndr_ntsvcs.h"
+#include "../librpc/gen_ndr/ndr_epmapper.h"
+#include "../librpc/gen_ndr/ndr_drsuapi.h"
 #include "../libcli/auth/schannel.h"
 #include "../libcli/auth/spnego.h"
 #include "smb_krb5.h"
-#include "ntlmssp.h"
+#include "../libcli/auth/ntlmssp.h"
+#include "rpc_client/cli_netlogon.h"
+#include "librpc/gen_ndr/ndr_dcerpc.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_CLI
@@ -276,6 +294,68 @@ static bool rpc_grow_buffer(prs_struct *pdu, size_t size)
        return true;
 }
 
+/*******************************************************************
+*******************************************************************/
+
+NTSTATUS dcerpc_push_ncacn_packet(TALLOC_CTX *mem_ctx,
+                                 enum dcerpc_pkt_type ptype,
+                                 uint8_t pfc_flags,
+                                 uint16_t frag_length,
+                                 uint16_t auth_length,
+                                 uint32_t call_id,
+                                 union dcerpc_payload u,
+                                 DATA_BLOB *blob)
+{
+       struct ncacn_packet r;
+       enum ndr_err_code ndr_err;
+
+       r.rpc_vers              = 5;
+       r.rpc_vers_minor        = 0;
+       r.ptype                 = ptype;
+       r.pfc_flags             = pfc_flags;
+       r.drep[0]               = DCERPC_DREP_LE;
+       r.drep[1]               = 0;
+       r.drep[2]               = 0;
+       r.drep[3]               = 0;
+       r.frag_length           = frag_length;
+       r.auth_length           = auth_length;
+       r.call_id               = call_id;
+       r.u                     = u;
+
+       ndr_err = ndr_push_struct_blob(blob, mem_ctx, &r,
+               (ndr_push_flags_fn_t)ndr_push_ncacn_packet);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               return ndr_map_error2ntstatus(ndr_err);
+       }
+
+       if (DEBUGLEVEL >= 10) {
+               NDR_PRINT_DEBUG(ncacn_packet, &r);
+       }
+
+       return NT_STATUS_OK;
+}
+
+/*******************************************************************
+*******************************************************************/
+
+NTSTATUS dcerpc_pull_ncacn_packet(TALLOC_CTX *mem_ctx,
+                                 const DATA_BLOB *blob,
+                                 struct ncacn_packet *r)
+{
+       enum ndr_err_code ndr_err;
+
+       ndr_err = ndr_pull_struct_blob(blob, mem_ctx, r,
+               (ndr_pull_flags_fn_t)ndr_pull_ncacn_packet);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               return ndr_map_error2ntstatus(ndr_err);
+       }
+
+       if (DEBUGLEVEL >= 10) {
+               NDR_PRINT_DEBUG(ncacn_packet, r);
+       }
+
+       return NT_STATUS_OK;
+}
 
 /*******************************************************************
  Use SMBreadX to get rest of one fragment's worth of rpc data.
@@ -1060,27 +1140,25 @@ static NTSTATUS cli_pipe_validate_current_pdu(struct rpc_pipe_client *cli, RPC_H
 
                case DCERPC_PKT_FAULT:
                {
-                       RPC_HDR_RESP rhdr_resp;
-                       RPC_HDR_FAULT fault_resp;
+                       DATA_BLOB blob;
+                       struct ncacn_packet r;
 
-                       if(!smb_io_rpc_hdr_resp("rpc_hdr_resp", &rhdr_resp, current_pdu, 0)) {
-                               DEBUG(5,("cli_pipe_validate_current_pdu: failed to unmarshal RPC_HDR_RESP.\n"));
-                               return NT_STATUS_BUFFER_TOO_SMALL;
-                       }
+                       blob = data_blob_const(prs_data_p(current_pdu),
+                                              prs_data_size(current_pdu));
 
-                       if(!smb_io_rpc_hdr_fault("fault", &fault_resp, current_pdu, 0)) {
-                               DEBUG(5,("cli_pipe_validate_current_pdu: failed to unmarshal RPC_HDR_FAULT.\n"));
-                               return NT_STATUS_BUFFER_TOO_SMALL;
+                       ret = dcerpc_pull_ncacn_packet(cli, &blob, &r);
+                       if (!NT_STATUS_IS_OK(ret)) {
+                               return ret;
                        }
-
                        DEBUG(1, ("cli_pipe_validate_current_pdu: RPC fault "
                                  "code %s received from %s!\n",
-                               dcerpc_errstr(talloc_tos(), NT_STATUS_V(fault_resp.status)),
+                               dcerpc_errstr(talloc_tos(), r.u.fault.status),
                                rpccli_pipe_txt(talloc_tos(), cli)));
-                       if (NT_STATUS_IS_OK(fault_resp.status)) {
+
+                       if (NT_STATUS_IS_OK(NT_STATUS(r.u.fault.status))) {
                                return NT_STATUS_UNSUCCESSFUL;
                        } else {
-                               return fault_resp.status;
+                               return NT_STATUS(r.u.fault.status);
                        }
                }
 
@@ -1221,8 +1299,6 @@ static struct tevent_req *cli_api_pipe_send(TALLOC_CTX *mem_ctx,
        tevent_req_set_callback(subreq, cli_api_pipe_write_done, req);
        return req;
 
-       status = NT_STATUS_INVALID_PARAMETER;
-
  post_status:
        tevent_req_nterror(req, status);
        return tevent_req_post(req, ev);
@@ -1356,13 +1432,6 @@ struct rpc_api_pipe_state {
        uint32_t incoming_pdu_offset;
 };
 
-static int rpc_api_pipe_state_destructor(struct rpc_api_pipe_state *state)
-{
-       prs_mem_free(&state->incoming_frag);
-       prs_mem_free(&state->incoming_pdu);
-       return 0;
-}
-
 static void rpc_api_pipe_trans_done(struct tevent_req *subreq);
 static void rpc_api_pipe_got_pdu(struct tevent_req *subreq);
 
@@ -1392,8 +1461,6 @@ static struct tevent_req *rpc_api_pipe_send(TALLOC_CTX *mem_ctx,
        /* Make incoming_pdu dynamic with no memory. */
        prs_give_memory(&state->incoming_pdu, NULL, 0, true);
 
-       talloc_set_destructor(state, rpc_api_pipe_state_destructor);
-
        /*
         * Ensure we're not sending too much.
         */
@@ -1436,7 +1503,6 @@ static void rpc_api_pipe_trans_done(struct tevent_req *subreq)
        NTSTATUS status;
        uint8_t *rdata = NULL;
        uint32_t rdata_len = 0;
-       char *rdata_copy;
 
        status = cli_api_pipe_recv(subreq, state, &rdata, &rdata_len);
        TALLOC_FREE(subreq);
@@ -1454,16 +1520,11 @@ static void rpc_api_pipe_trans_done(struct tevent_req *subreq)
        }
 
        /*
-        * Give the memory received from cli_trans as dynamic to the current
-        * pdu. Duplicating it sucks, but prs_struct doesn't know about talloc
-        * :-(
+        * This is equivalent to a talloc_steal - gives rdata to
+        * the prs_struct state->incoming_frag.
         */
-       rdata_copy = (char *)memdup(rdata, rdata_len);
-       TALLOC_FREE(rdata);
-       if (tevent_req_nomem(rdata_copy, req)) {
-               return;
-       }
-       prs_give_memory(&state->incoming_frag, rdata_copy, rdata_len, true);
+       prs_give_memory(&state->incoming_frag, (char *)rdata, rdata_len, true);
+       rdata = NULL;
 
        /* Ensure we have enough data for a pdu. */
        subreq = get_complete_frag_send(state, state->ev, state->cli,
@@ -1580,9 +1641,10 @@ static NTSTATUS rpc_api_pipe_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
        reply_pdu->mem_ctx = mem_ctx;
 
        /*
-        * Prevent state->incoming_pdu from being freed in
-        * rpc_api_pipe_state_destructor()
+        * Prevent state->incoming_pdu from being freed
+        * when state is freed.
         */
+       talloc_steal(mem_ctx, prs_data_p(reply_pdu));
        prs_init_empty(&state->incoming_pdu, state, UNMARSHALL);
 
        return NT_STATUS_OK;
@@ -1621,7 +1683,6 @@ static NTSTATUS create_krb5_auth_bind_req( struct rpc_pipe_client *cli,
                        error_message(ret) ));
 
                data_blob_free(&tkt);
-               prs_mem_free(auth_data);
                return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -1633,7 +1694,6 @@ static NTSTATUS create_krb5_auth_bind_req( struct rpc_pipe_client *cli,
        /* Auth len in the rpc header doesn't include auth_header. */
        if (!prs_copy_data_in(auth_data, (char *)tkt_wrapped.data, tkt_wrapped.length)) {
                data_blob_free(&tkt_wrapped);
-               prs_mem_free(auth_data);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -1671,7 +1731,6 @@ static NTSTATUS create_spnego_ntlmssp_auth_rpc_bind_req( struct rpc_pipe_client
 
        if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
                data_blob_free(&request);
-               prs_mem_free(auth_data);
                return nt_status;
        }
 
@@ -1683,7 +1742,6 @@ static NTSTATUS create_spnego_ntlmssp_auth_rpc_bind_req( struct rpc_pipe_client
        /* Auth len in the rpc header doesn't include auth_header. */
        if (!prs_copy_data_in(auth_data, (char *)spnego_msg.data, spnego_msg.length)) {
                data_blob_free(&spnego_msg);
-               prs_mem_free(auth_data);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -1717,14 +1775,12 @@ static NTSTATUS create_ntlmssp_auth_rpc_bind_req( struct rpc_pipe_client *cli,
 
        if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
                data_blob_free(&request);
-               prs_mem_free(auth_data);
                return nt_status;
        }
 
        /* Auth len in the rpc header doesn't include auth_header. */
        if (!prs_copy_data_in(auth_data, (char *)request.data, request.length)) {
                data_blob_free(&request);
-               prs_mem_free(auth_data);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -1770,11 +1826,10 @@ static NTSTATUS create_schannel_auth_rpc_bind_req( struct rpc_pipe_client *cli,
        r.oem_netbios_domain.a          = cli->auth->domain;
        r.oem_netbios_computer.a        = global_myname();
 
-       ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), NULL, &r,
+       ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &r,
                       (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
-               prs_mem_free(auth_data);
                return ndr_map_error2ntstatus(ndr_err);
        }
 
@@ -1784,18 +1839,44 @@ static NTSTATUS create_schannel_auth_rpc_bind_req( struct rpc_pipe_client *cli,
 
        if (!prs_copy_data_in(auth_data, (const char *)blob.data, blob.length))
        {
-               prs_mem_free(auth_data);
                return NT_STATUS_NO_MEMORY;
        }
 
        return NT_STATUS_OK;
 }
 
+/*******************************************************************
+ ********************************************************************/
+
+static NTSTATUS init_dcerpc_ctx_list(TALLOC_CTX *mem_ctx,
+                                    const struct ndr_syntax_id *abstract_syntax,
+                                    const struct ndr_syntax_id *transfer_syntax,
+                                    struct dcerpc_ctx_list **ctx_list_p)
+{
+       struct dcerpc_ctx_list *ctx_list;
+
+       ctx_list = talloc_array(mem_ctx, struct dcerpc_ctx_list, 1);
+       NT_STATUS_HAVE_NO_MEMORY(ctx_list);
+
+       ctx_list[0].context_id                  = 0;
+       ctx_list[0].num_transfer_syntaxes       = 1;
+       ctx_list[0].abstract_syntax             = *abstract_syntax;
+       ctx_list[0].transfer_syntaxes           = talloc_array(ctx_list,
+                                                              struct ndr_syntax_id,
+                                                              ctx_list[0].num_transfer_syntaxes);
+       NT_STATUS_HAVE_NO_MEMORY(ctx_list[0].transfer_syntaxes);
+       ctx_list[0].transfer_syntaxes[0]        = *transfer_syntax;
+
+       *ctx_list_p = ctx_list;
+
+       return NT_STATUS_OK;
+}
+
 /*******************************************************************
  Creates the internals of a DCE/RPC bind request or alter context PDU.
  ********************************************************************/
 
-static NTSTATUS create_bind_or_alt_ctx_internal(enum dcerpc_pkt_type pkt_type,
+static NTSTATUS create_bind_or_alt_ctx_internal(enum dcerpc_pkt_type ptype,
                                                prs_struct *rpc_out, 
                                                uint32 rpc_call_id,
                                                const struct ndr_syntax_id *abstract,
@@ -1803,25 +1884,33 @@ static NTSTATUS create_bind_or_alt_ctx_internal(enum dcerpc_pkt_type pkt_type,
                                                RPC_HDR_AUTH *phdr_auth,
                                                prs_struct *pauth_info)
 {
-       RPC_HDR hdr;
-       RPC_HDR_RB hdr_rb;
-       RPC_CONTEXT rpc_ctx;
        uint16 auth_len = prs_offset(pauth_info);
        uint8 ss_padding_len = 0;
        uint16 frag_len = 0;
+       NTSTATUS status;
+       union dcerpc_payload u;
+       DATA_BLOB blob;
+       struct dcerpc_ctx_list *ctx_list;
 
-       /* create the RPC context. */
-       init_rpc_context(&rpc_ctx, 0 /* context id */, abstract, transfer);
+       status = init_dcerpc_ctx_list(rpc_out->mem_ctx, abstract, transfer,
+                                     &ctx_list);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
-       /* create the bind request RPC_HDR_RB */
-       init_rpc_hdr_rb(&hdr_rb, RPC_MAX_PDU_FRAG_LEN, RPC_MAX_PDU_FRAG_LEN, 0x0, &rpc_ctx);
+       u.bind.max_xmit_frag    = RPC_MAX_PDU_FRAG_LEN;
+       u.bind.max_recv_frag    = RPC_MAX_PDU_FRAG_LEN;
+       u.bind.assoc_group_id   = 0x0;
+       u.bind.num_contexts     = 1;
+       u.bind.ctx_list         = ctx_list;
+       u.bind.auth_info        = data_blob_null;
 
        /* Start building the frag length. */
-       frag_len = RPC_HEADER_LEN + RPC_HDR_RB_LEN(&hdr_rb);
+       frag_len = RPC_HEADER_LEN + RPC_HDR_RB_LEN(&u.bind);
 
        /* Do we need to pad ? */
        if (auth_len) {
-               uint16 data_len = RPC_HEADER_LEN + RPC_HDR_RB_LEN(&hdr_rb);
+               uint16_t data_len = RPC_HEADER_LEN + RPC_HDR_RB_LEN(&u.bind);
                if (data_len % CLIENT_NDR_PADDING_SIZE) {
                        ss_padding_len = CLIENT_NDR_PADDING_SIZE - (data_len % CLIENT_NDR_PADDING_SIZE);
                        phdr_auth->auth_pad_len = ss_padding_len;
@@ -1829,18 +1918,21 @@ static NTSTATUS create_bind_or_alt_ctx_internal(enum dcerpc_pkt_type pkt_type,
                frag_len += RPC_HDR_AUTH_LEN + auth_len + ss_padding_len;
        }
 
-       /* Create the request RPC_HDR */
-       init_rpc_hdr(&hdr, pkt_type, DCERPC_PFC_FLAG_FIRST|DCERPC_PFC_FLAG_LAST, rpc_call_id, frag_len, auth_len);
-
-       /* Marshall the RPC header */
-       if(!smb_io_rpc_hdr("hdr"   , &hdr, rpc_out, 0)) {
-               DEBUG(0,("create_bind_or_alt_ctx_internal: failed to marshall RPC_HDR.\n"));
-               return NT_STATUS_NO_MEMORY;
+       status = dcerpc_push_ncacn_packet(rpc_out->mem_ctx,
+                                         ptype,
+                                         DCERPC_PFC_FLAG_FIRST |
+                                         DCERPC_PFC_FLAG_LAST,
+                                         frag_len,
+                                         auth_len,
+                                         rpc_call_id,
+                                         u,
+                                         &blob);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("create_bind_or_alt_ctx_internal: failed to marshall RPC_HDR_RB.\n"));
+               return status;
        }
 
-       /* Marshall the bind request data */
-       if(!smb_io_rpc_hdr_rb("", &hdr_rb, rpc_out, 0)) {
-               DEBUG(0,("create_bind_or_alt_ctx_internal: failed to marshall RPC_HDR_RB.\n"));
+       if (!prs_copy_data_in(rpc_out, (char *)blob.data, blob.length)) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -1897,7 +1989,6 @@ static NTSTATUS create_rpc_bind_req(struct rpc_pipe_client *cli,
                case PIPE_AUTH_TYPE_SCHANNEL:
                        ret = create_schannel_auth_rpc_bind_req(cli, auth_level, &hdr_auth, &auth_info);
                        if (!NT_STATUS_IS_OK(ret)) {
-                               prs_mem_free(&auth_info);
                                return ret;
                        }
                        break;
@@ -1905,7 +1996,6 @@ static NTSTATUS create_rpc_bind_req(struct rpc_pipe_client *cli,
                case PIPE_AUTH_TYPE_NTLMSSP:
                        ret = create_ntlmssp_auth_rpc_bind_req(cli, auth_level, &hdr_auth, &auth_info);
                        if (!NT_STATUS_IS_OK(ret)) {
-                               prs_mem_free(&auth_info);
                                return ret;
                        }
                        break;
@@ -1913,7 +2003,6 @@ static NTSTATUS create_rpc_bind_req(struct rpc_pipe_client *cli,
                case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
                        ret = create_spnego_ntlmssp_auth_rpc_bind_req(cli, auth_level, &hdr_auth, &auth_info);
                        if (!NT_STATUS_IS_OK(ret)) {
-                               prs_mem_free(&auth_info);
                                return ret;
                        }
                        break;
@@ -1921,7 +2010,6 @@ static NTSTATUS create_rpc_bind_req(struct rpc_pipe_client *cli,
                case PIPE_AUTH_TYPE_KRB5:
                        ret = create_krb5_auth_bind_req(cli, auth_level, &hdr_auth, &auth_info);
                        if (!NT_STATUS_IS_OK(ret)) {
-                               prs_mem_free(&auth_info);
                                return ret;
                        }
                        break;
@@ -1942,7 +2030,6 @@ static NTSTATUS create_rpc_bind_req(struct rpc_pipe_client *cli,
                                                &hdr_auth,
                                                &auth_info);
 
-       prs_mem_free(&auth_info);
        return ret;
 }
 
@@ -1959,11 +2046,14 @@ static NTSTATUS add_ntlmssp_auth_footer(struct rpc_pipe_client *cli,
        NTSTATUS status;
        DATA_BLOB auth_blob = data_blob_null;
        uint16 data_and_pad_len = prs_offset(outgoing_pdu) - RPC_HEADER_LEN - RPC_HDR_RESP_LEN;
+       TALLOC_CTX *frame;
 
        if (!cli->auth->a_u.ntlmssp_state) {
                return NT_STATUS_INVALID_PARAMETER;
        }
 
+       frame = talloc_stackframe();
+
        /* Init and marshall the auth header. */
        init_rpc_hdr_auth(&auth_info,
                        map_pipe_auth_type_to_rpc_auth_type(
@@ -1974,7 +2064,7 @@ static NTSTATUS add_ntlmssp_auth_footer(struct rpc_pipe_client *cli,
 
        if(!smb_io_rpc_hdr_auth("hdr_auth", &auth_info, outgoing_pdu, 0)) {
                DEBUG(0,("add_ntlmssp_auth_footer: failed to marshall RPC_HDR_AUTH.\n"));
-               data_blob_free(&auth_blob);
+               talloc_free(frame);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -1982,13 +2072,14 @@ static NTSTATUS add_ntlmssp_auth_footer(struct rpc_pipe_client *cli,
                case DCERPC_AUTH_LEVEL_PRIVACY:
                        /* Data portion is encrypted. */
                        status = ntlmssp_seal_packet(cli->auth->a_u.ntlmssp_state,
+                                                    frame,
                                        (unsigned char *)prs_data_p(outgoing_pdu) + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
                                        data_and_pad_len,
                                        (unsigned char *)prs_data_p(outgoing_pdu),
                                        (size_t)prs_offset(outgoing_pdu),
                                        &auth_blob);
                        if (!NT_STATUS_IS_OK(status)) {
-                               data_blob_free(&auth_blob);
+                               talloc_free(frame);
                                return status;
                        }
                        break;
@@ -1996,13 +2087,14 @@ static NTSTATUS add_ntlmssp_auth_footer(struct rpc_pipe_client *cli,
                case DCERPC_AUTH_LEVEL_INTEGRITY:
                        /* Data is signed. */
                        status = ntlmssp_sign_packet(cli->auth->a_u.ntlmssp_state,
+                                                    frame,
                                        (unsigned char *)prs_data_p(outgoing_pdu) + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
                                        data_and_pad_len,
                                        (unsigned char *)prs_data_p(outgoing_pdu),
                                        (size_t)prs_offset(outgoing_pdu),
                                        &auth_blob);
                        if (!NT_STATUS_IS_OK(status)) {
-                               data_blob_free(&auth_blob);
+                               talloc_free(frame);
                                return status;
                        }
                        break;
@@ -2019,11 +2111,11 @@ static NTSTATUS add_ntlmssp_auth_footer(struct rpc_pipe_client *cli,
        if (!prs_copy_data_in(outgoing_pdu, (const char *)auth_blob.data, NTLMSSP_SIG_SIZE)) {
                DEBUG(0,("add_ntlmssp_auth_footer: failed to add %u bytes auth blob.\n",
                        (unsigned int)NTLMSSP_SIG_SIZE));
-               data_blob_free(&auth_blob);
+               talloc_free(frame);
                return NT_STATUS_NO_MEMORY;
        }
 
-       data_blob_free(&auth_blob);
+       talloc_free(frame);
        return NT_STATUS_OK;
 }
 
@@ -2185,13 +2277,6 @@ struct rpc_api_pipe_req_state {
        prs_struct reply_pdu;
 };
 
-static int rpc_api_pipe_req_state_destructor(struct rpc_api_pipe_req_state *s)
-{
-       prs_mem_free(&s->outgoing_frag);
-       prs_mem_free(&s->reply_pdu);
-       return 0;
-}
-
 static void rpc_api_pipe_req_write_done(struct tevent_req *subreq);
 static void rpc_api_pipe_req_done(struct tevent_req *subreq);
 static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
@@ -2234,8 +2319,6 @@ struct tevent_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
                goto fail;
        }
 
-       talloc_set_destructor(state, rpc_api_pipe_req_state_destructor);
-
        status = prepare_next_frag(state, &is_last_frag);
        if (!NT_STATUS_IS_OK(status)) {
                goto post_status;
@@ -2436,9 +2519,10 @@ NTSTATUS rpc_api_pipe_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
        reply_pdu->mem_ctx = mem_ctx;
 
        /*
-        * Prevent state->req_pdu from being freed in
-        * rpc_api_pipe_req_state_destructor()
+        * Prevent state->req_pdu from being freed
+        * when state is freed.
         */
+       talloc_steal(mem_ctx, prs_data_p(reply_pdu));
        prs_init_empty(&state->reply_pdu, state, UNMARSHALL);
 
        return NT_STATUS_OK;
@@ -2605,7 +2689,6 @@ static NTSTATUS create_rpc_alter_context(uint32 rpc_call_id,
 
        if (pauth_blob->length) {
                if (!prs_copy_data_in(&auth_info, (const char *)pauth_blob->data, pauth_blob->length)) {
-                       prs_mem_free(&auth_info);
                        return NT_STATUS_NO_MEMORY;
                }
        }
@@ -2617,7 +2700,6 @@ static NTSTATUS create_rpc_alter_context(uint32 rpc_call_id,
                                                transfer,
                                                &hdr_auth,
                                                &auth_info);
-       prs_mem_free(&auth_info);
        return ret;
 }
 
@@ -2632,12 +2714,6 @@ struct rpc_pipe_bind_state {
        uint32_t rpc_call_id;
 };
 
-static int rpc_pipe_bind_state_destructor(struct rpc_pipe_bind_state *state)
-{
-       prs_mem_free(&state->rpc_out);
-       return 0;
-}
-
 static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq);
 static NTSTATUS rpc_finish_auth3_bind_send(struct tevent_req *req,
                                           struct rpc_pipe_bind_state *state,
@@ -2674,7 +2750,6 @@ struct tevent_req *rpc_pipe_bind_send(TALLOC_CTX *mem_ctx,
        state->rpc_call_id = get_rpc_call_id();
 
        prs_init_empty(&state->rpc_out, state, MARSHALL);
-       talloc_set_destructor(state, rpc_pipe_bind_state_destructor);
 
        cli->auth = talloc_move(cli, &auth);
 
@@ -2730,7 +2805,6 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
        /* Unmarshall the RPC header */
        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);
                tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
                return;
        }
@@ -2738,14 +2812,12 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
        if (!smb_io_rpc_hdr_ba("", &hdr_ba, &reply_pdu, 0)) {
                DEBUG(0, ("rpc_pipe_bind: Failed to unmarshall "
                          "RPC_HDR_BA.\n"));
-               prs_mem_free(&reply_pdu);
                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);
                tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
                return;
        }
@@ -2762,7 +2834,6 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
        case PIPE_AUTH_TYPE_NONE:
        case PIPE_AUTH_TYPE_SCHANNEL:
                /* Bind complete. */
-               prs_mem_free(&reply_pdu);
                tevent_req_done(req);
                break;
 
@@ -2770,7 +2841,6 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
                /* Need to send AUTH3 packet - no reply. */
                status = rpc_finish_auth3_bind_send(req, state, &hdr,
                                                    &reply_pdu);
-               prs_mem_free(&reply_pdu);
                if (!NT_STATUS_IS_OK(status)) {
                        tevent_req_nterror(req, status);
                }
@@ -2780,7 +2850,6 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
                /* Need to send alter context request and reply. */
                status = rpc_finish_spnego_ntlmssp_bind_send(req, state, &hdr,
                                                             &reply_pdu);
-               prs_mem_free(&reply_pdu);
                if (!NT_STATUS_IS_OK(status)) {
                        tevent_req_nterror(req, status);
                }
@@ -2792,7 +2861,6 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
        default:
                DEBUG(0,("cli_finish_bind_auth: unknown auth type %u\n",
                         (unsigned int)state->cli->auth->auth_type));
-               prs_mem_free(&reply_pdu);
                tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
        }
 }
@@ -3064,33 +3132,40 @@ NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
        return status;
 }
 
+#define RPCCLI_DEFAULT_TIMEOUT 10000 /* 10 seconds. */
+
 unsigned int rpccli_set_timeout(struct rpc_pipe_client *rpc_cli,
                                unsigned int timeout)
 {
-       struct cli_state *cli;
+       unsigned int old;
 
-       if (rpc_cli->transport->transport == NCACN_NP) {
-               cli = rpc_pipe_np_smb_conn(rpc_cli);
-               if (cli == NULL) {
-                       return 0;
-               }
-               return cli_set_timeout(cli, timeout);
+       if (rpc_cli->transport == NULL) {
+               return RPCCLI_DEFAULT_TIMEOUT;
        }
 
-       if (rpc_cli->transport->transport == NCACN_IP_TCP ||
-           rpc_cli->transport->transport == NCALRPC) {
-               return rpccli_set_sock_timeout(rpc_cli, timeout);
+       if (rpc_cli->transport->set_timeout == NULL) {
+               return RPCCLI_DEFAULT_TIMEOUT;
        }
 
-       if (rpc_cli->transport->transport == NCACN_INTERNAL) {
-               cli = rpc_pipe_smbd_smb_conn(rpc_cli);
-               if (!cli) {
-                       return 0;
-               }
-               return cli_set_timeout(cli, timeout);
+       old = rpc_cli->transport->set_timeout(rpc_cli->transport->priv, timeout);
+       if (old == 0) {
+               return RPCCLI_DEFAULT_TIMEOUT;
        }
 
-       return 0;
+       return old;
+}
+
+bool rpccli_is_connected(struct rpc_pipe_client *rpc_cli)
+{
+       if (rpc_cli == NULL) {
+               return false;
+       }
+
+       if (rpc_cli->transport == NULL) {
+               return false;
+       }
+
+       return rpc_cli->transport->is_connected(rpc_cli->transport->priv);
 }
 
 bool rpccli_get_pwd_hash(struct rpc_pipe_client *rpc_cli, uint8_t nt_hash[16])
@@ -3167,7 +3242,11 @@ static NTSTATUS rpccli_ntlmssp_bind_data(TALLOC_CTX *mem_ctx,
                goto fail;
        }
 
-       status = ntlmssp_client_start(&result->a_u.ntlmssp_state);
+       status = ntlmssp_client_start(NULL,
+                                     global_myname(),
+                                     lp_workgroup(),
+                                     lp_client_ntlmv2_auth(),
+                                     &result->a_u.ntlmssp_state);
        if (!NT_STATUS_IS_OK(status)) {
                goto fail;
        }
@@ -3595,14 +3674,14 @@ NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
        return status;
 }
 
-static int rpc_pipe_client_np_destructor(struct rpc_pipe_client *p)
-{
+struct rpc_pipe_client_np_ref {
        struct cli_state *cli;
+       struct rpc_pipe_client *pipe;
+};
 
-       cli = rpc_pipe_np_smb_conn(p);
-       if (cli != NULL) {
-               DLIST_REMOVE(cli->pipe_list, p);
-       }
+static int rpc_pipe_client_np_ref_destructor(struct rpc_pipe_client_np_ref *np_ref)
+{
+       DLIST_REMOVE(np_ref->cli->pipe_list, np_ref->pipe);
        return 0;
 }
 
@@ -3625,6 +3704,7 @@ static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
 {
        struct rpc_pipe_client *result;
        NTSTATUS status;
+       struct rpc_pipe_client_np_ref *np_ref;
 
        /* sanity check to protect against crashes */
 
@@ -3663,8 +3743,16 @@ static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
 
        result->transport->transport = NCACN_NP;
 
-       DLIST_ADD(cli->pipe_list, result);
-       talloc_set_destructor(result, rpc_pipe_client_np_destructor);
+       np_ref = talloc(result->transport, struct rpc_pipe_client_np_ref);
+       if (np_ref == NULL) {
+               TALLOC_FREE(result);
+               return NT_STATUS_NO_MEMORY;
+       }
+       np_ref->cli = cli;
+       np_ref->pipe = result;
+
+       DLIST_ADD(np_ref->cli->pipe_list, np_ref->pipe);
+       talloc_set_destructor(np_ref, rpc_pipe_client_np_ref_destructor);
 
        *presult = result;
        return NT_STATUS_OK;