s3:cli_pipe: remove unused cli_rpc_pipe_open_generic_auth()
[bbaumbach/samba-autobuild/.git] / source3 / rpc_client / cli_pipe.c
index 2743a07ebfd711b9e1ef2dd5aafd4760a3a6254f..8227ef0b0bd1394c6b3d69c781776c53783f5349 100644 (file)
  */
 
 #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_schannel.h"
 #include "../librpc/gen_ndr/ndr_dssetup.h"
 #include "../libcli/auth/schannel.h"
-#include "../libcli/auth/spnego.h"
-#include "../auth/ntlmssp/ntlmssp.h"
+#include "../libcli/auth/netlogon_creds_cli.h"
 #include "auth_generic.h"
 #include "librpc/gen_ndr/ndr_dcerpc.h"
 #include "librpc/gen_ndr/ndr_netlogon_c.h"
@@ -58,9 +57,9 @@ static const char *rpccli_pipe_txt(TALLOC_CTX *mem_ctx,
  Rpc pipe call id.
  ********************************************************************/
 
-static uint32 get_rpc_call_id(void)
+static uint32_t get_rpc_call_id(void)
 {
-       static uint32 call_id = 0;
+       static uint32_t call_id = 0;
        return ++call_id;
 }
 
@@ -284,6 +283,10 @@ static struct tevent_req *get_complete_frag_send(TALLOC_CTX *mem_ctx,
        }
 
        state->frag_len = dcerpc_get_frag_length(pdu);
+       if (state->frag_len < RPC_HEADER_LEN) {
+               tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
+               return tevent_req_post(req, ev);
+       }
 
        /*
         * Ensure we have frag_len bytes of data.
@@ -332,6 +335,10 @@ static void get_complete_frag_got_header(struct tevent_req *subreq)
        }
 
        state->frag_len = dcerpc_get_frag_length(state->pdu);
+       if (state->frag_len < RPC_HEADER_LEN) {
+               tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
+               return;
+       }
 
        if (!data_blob_realloc(NULL, state->pdu, state->frag_len)) {
                tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
@@ -381,12 +388,13 @@ static NTSTATUS cli_pipe_validate_current_pdu(TALLOC_CTX *mem_ctx,
                                                struct ncacn_packet *pkt,
                                                DATA_BLOB *pdu,
                                                uint8_t expected_pkt_type,
+                                               uint32_t call_id,
                                                DATA_BLOB *rdata,
                                                DATA_BLOB *reply_pdu)
 {
-       struct dcerpc_response *r;
-       NTSTATUS ret = NT_STATUS_OK;
-       size_t pad_len = 0;
+       const struct dcerpc_response *r = NULL;
+       DATA_BLOB tmp_stub = data_blob_null;
+       NTSTATUS ret;
 
        /*
         * Point the return values at the real data including the RPC
@@ -394,50 +402,128 @@ static NTSTATUS cli_pipe_validate_current_pdu(TALLOC_CTX *mem_ctx,
         */
        *rdata = *pdu;
 
+       if ((pkt->ptype == DCERPC_PKT_BIND_ACK) &&
+           !(pkt->pfc_flags & DCERPC_PFC_FLAG_LAST)) {
+               /*
+                * TODO: do we still need this hack which was introduced
+                * in commit a42afcdcc7ab9aa9ed193ae36d3dbb10843447f0.
+                *
+                * I don't even know what AS/U might be...
+                */
+               DEBUG(5, (__location__ ": bug in server (AS/U?), setting "
+                         "fragment first/last ON.\n"));
+               pkt->pfc_flags |= DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST;
+       }
+
        /* Ensure we have the correct type. */
        switch (pkt->ptype) {
-       case DCERPC_PKT_ALTER_RESP:
+       case DCERPC_PKT_BIND_NAK:
+               DEBUG(1, (__location__ ": Bind NACK received from %s!\n",
+                         rpccli_pipe_txt(talloc_tos(), cli)));
+
+               ret = dcerpc_verify_ncacn_packet_header(pkt,
+                                               DCERPC_PKT_BIND_NAK,
+                                               0, /* max_auth_info */
+                                               DCERPC_PFC_FLAG_FIRST |
+                                               DCERPC_PFC_FLAG_LAST,
+                                               0); /* optional flags */
+               if (!NT_STATUS_IS_OK(ret)) {
+                       DEBUG(1, (__location__ ": Connection to %s got an unexpected "
+                                 "RPC packet type - %u, expected %u: %s\n",
+                                 rpccli_pipe_txt(talloc_tos(), cli),
+                                 pkt->ptype, expected_pkt_type,
+                                 nt_errstr(ret)));
+                       NDR_PRINT_DEBUG(ncacn_packet, pkt);
+                       return ret;
+               }
+
+               /* Use this for now... */
+               return NT_STATUS_NETWORK_ACCESS_DENIED;
+
        case DCERPC_PKT_BIND_ACK:
+               ret = dcerpc_verify_ncacn_packet_header(pkt,
+                                       expected_pkt_type,
+                                       pkt->u.bind_ack.auth_info.length,
+                                       DCERPC_PFC_FLAG_FIRST |
+                                       DCERPC_PFC_FLAG_LAST,
+                                       DCERPC_PFC_FLAG_CONC_MPX |
+                                       DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN);
+               if (!NT_STATUS_IS_OK(ret)) {
+                       DEBUG(1, (__location__ ": Connection to %s got an unexpected "
+                                 "RPC packet type - %u, expected %u: %s\n",
+                                 rpccli_pipe_txt(talloc_tos(), cli),
+                                 pkt->ptype, expected_pkt_type,
+                                 nt_errstr(ret)));
+                       NDR_PRINT_DEBUG(ncacn_packet, pkt);
+                       return ret;
+               }
 
-               /* Client code never receives this kind of packets */
                break;
 
+       case DCERPC_PKT_ALTER_RESP:
+               ret = dcerpc_verify_ncacn_packet_header(pkt,
+                                       expected_pkt_type,
+                                       pkt->u.alter_resp.auth_info.length,
+                                       DCERPC_PFC_FLAG_FIRST |
+                                       DCERPC_PFC_FLAG_LAST,
+                                       DCERPC_PFC_FLAG_CONC_MPX |
+                                       DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN);
+               if (!NT_STATUS_IS_OK(ret)) {
+                       DEBUG(1, (__location__ ": Connection to %s got an unexpected "
+                                 "RPC packet type - %u, expected %u: %s\n",
+                                 rpccli_pipe_txt(talloc_tos(), cli),
+                                 pkt->ptype, expected_pkt_type,
+                                 nt_errstr(ret)));
+                       NDR_PRINT_DEBUG(ncacn_packet, pkt);
+                       return ret;
+               }
+
+               break;
 
        case DCERPC_PKT_RESPONSE:
 
                r = &pkt->u.response;
 
+               ret = dcerpc_verify_ncacn_packet_header(pkt,
+                                               expected_pkt_type,
+                                               r->stub_and_verifier.length,
+                                               0, /* required_flags */
+                                               DCERPC_PFC_FLAG_FIRST |
+                                               DCERPC_PFC_FLAG_LAST);
+               if (!NT_STATUS_IS_OK(ret)) {
+                       DEBUG(1, (__location__ ": Connection to %s got an unexpected "
+                                 "RPC packet type - %u, expected %u: %s\n",
+                                 rpccli_pipe_txt(talloc_tos(), cli),
+                                 pkt->ptype, expected_pkt_type,
+                                 nt_errstr(ret)));
+                       NDR_PRINT_DEBUG(ncacn_packet, pkt);
+                       return ret;
+               }
+
+               tmp_stub.data = r->stub_and_verifier.data;
+               tmp_stub.length = r->stub_and_verifier.length;
+
                /* Here's where we deal with incoming sign/seal. */
                ret = dcerpc_check_auth(cli->auth, pkt,
-                                       &r->stub_and_verifier,
+                                       &tmp_stub,
                                        DCERPC_RESPONSE_LENGTH,
-                                       pdu, &pad_len);
+                                       pdu);
                if (!NT_STATUS_IS_OK(ret)) {
+                       DEBUG(1, (__location__ ": Connection to %s got an unexpected "
+                                 "RPC packet type - %u, expected %u: %s\n",
+                                 rpccli_pipe_txt(talloc_tos(), cli),
+                                 pkt->ptype, expected_pkt_type,
+                                 nt_errstr(ret)));
+                       NDR_PRINT_DEBUG(ncacn_packet, pkt);
                        return ret;
                }
 
-               if (pkt->frag_length < DCERPC_RESPONSE_LENGTH + pad_len) {
-                       return NT_STATUS_BUFFER_TOO_SMALL;
-               }
-
                /* Point the return values at the NDR data. */
-               rdata->data = r->stub_and_verifier.data;
-
-               if (pkt->auth_length) {
-                       /* We've already done integer wrap tests in
-                        * dcerpc_check_auth(). */
-                       rdata->length = r->stub_and_verifier.length
-                                        - pad_len
-                                        - DCERPC_AUTH_TRAILER_LENGTH
-                                        - pkt->auth_length;
-               } else {
-                       rdata->length = r->stub_and_verifier.length;
-               }
+               *rdata = tmp_stub;
 
-               DEBUG(10, ("Got pdu len %lu, data_len %lu, ss_len %u\n",
+               DEBUG(10, ("Got pdu len %lu, data_len %lu\n",
                           (long unsigned int)pdu->length,
-                          (long unsigned int)rdata->length,
-                          (unsigned int)pad_len));
+                          (long unsigned int)rdata->length));
 
                /*
                 * If this is the first reply, and the allocation hint is
@@ -458,14 +544,24 @@ static NTSTATUS cli_pipe_validate_current_pdu(TALLOC_CTX *mem_ctx,
 
                break;
 
-       case DCERPC_PKT_BIND_NAK:
-               DEBUG(1, (__location__ ": Bind NACK received from %s!\n",
-                         rpccli_pipe_txt(talloc_tos(), cli)));
-               /* Use this for now... */
-               return NT_STATUS_NETWORK_ACCESS_DENIED;
-
        case DCERPC_PKT_FAULT:
 
+               ret = dcerpc_verify_ncacn_packet_header(pkt,
+                                               DCERPC_PKT_FAULT,
+                                               0, /* max_auth_info */
+                                               DCERPC_PFC_FLAG_FIRST |
+                                               DCERPC_PFC_FLAG_LAST,
+                                               DCERPC_PFC_FLAG_DID_NOT_EXECUTE);
+               if (!NT_STATUS_IS_OK(ret)) {
+                       DEBUG(1, (__location__ ": Connection to %s got an unexpected "
+                                 "RPC packet type - %u, expected %u: %s\n",
+                                 rpccli_pipe_txt(talloc_tos(), cli),
+                                 pkt->ptype, expected_pkt_type,
+                                 nt_errstr(ret)));
+                       NDR_PRINT_DEBUG(ncacn_packet, pkt);
+                       return ret;
+               }
+
                DEBUG(1, (__location__ ": RPC fault code %s received "
                          "from %s!\n",
                          dcerpc_errstr(talloc_tos(),
@@ -479,26 +575,16 @@ static NTSTATUS cli_pipe_validate_current_pdu(TALLOC_CTX *mem_ctx,
                          "from %s!\n",
                          (unsigned int)pkt->ptype,
                          rpccli_pipe_txt(talloc_tos(), cli)));
-               return NT_STATUS_INVALID_INFO_CLASS;
+               return NT_STATUS_RPC_PROTOCOL_ERROR;
        }
 
-       if (pkt->ptype != expected_pkt_type) {
+
+       if (pkt->call_id != call_id) {
                DEBUG(3, (__location__ ": Connection to %s got an unexpected "
-                         "RPC packet type - %u, not %u\n",
+                         "RPC call_id - %u, not %u\n",
                          rpccli_pipe_txt(talloc_tos(), cli),
-                         pkt->ptype, expected_pkt_type));
-               return NT_STATUS_INVALID_INFO_CLASS;
-       }
-
-       /* Do this just before return - we don't want to modify any rpc header
-          data before now as we may have needed to do cryptographic actions on
-          it before. */
-
-       if ((pkt->ptype == DCERPC_PKT_BIND_ACK) &&
-           !(pkt->pfc_flags & DCERPC_PFC_FLAG_LAST)) {
-               DEBUG(5, (__location__ ": bug in server (AS/U?), setting "
-                         "fragment first/last ON.\n"));
-               pkt->pfc_flags |= DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST;
+                         pkt->call_id, call_id));
+               return NT_STATUS_RPC_PROTOCOL_ERROR;
        }
 
        return NT_STATUS_OK;
@@ -693,6 +779,7 @@ struct rpc_api_pipe_state {
        struct tevent_context *ev;
        struct rpc_pipe_client *cli;
        uint8_t expected_pkt_type;
+       uint32_t call_id;
 
        DATA_BLOB incoming_frag;
        struct ncacn_packet *pkt;
@@ -711,7 +798,8 @@ static struct tevent_req *rpc_api_pipe_send(TALLOC_CTX *mem_ctx,
                                            struct tevent_context *ev,
                                            struct rpc_pipe_client *cli,
                                            DATA_BLOB *data, /* Outgoing PDU */
-                                           uint8_t expected_pkt_type)
+                                           uint8_t expected_pkt_type,
+                                           uint32_t call_id)
 {
        struct tevent_req *req, *subreq;
        struct rpc_api_pipe_state *state;
@@ -725,9 +813,7 @@ static struct tevent_req *rpc_api_pipe_send(TALLOC_CTX *mem_ctx,
        state->ev = ev;
        state->cli = cli;
        state->expected_pkt_type = expected_pkt_type;
-       state->incoming_frag = data_blob_null;
-       state->reply_pdu = data_blob_null;
-       state->reply_pdu_offset = 0;
+       state->call_id = call_id;
        state->endianess = DCERPC_DREP_LE;
 
        /*
@@ -851,31 +937,39 @@ static void rpc_api_pipe_got_pdu(struct tevent_req *subreq)
 
        state->pkt = talloc(state, struct ncacn_packet);
        if (!state->pkt) {
+               /*
+                * TODO: do a real async disconnect ...
+                *
+                * For now do it sync...
+                */
+               TALLOC_FREE(state->cli->transport);
                tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
 
        status = dcerpc_pull_ncacn_packet(state->pkt,
                                          &state->incoming_frag,
-                                         state->pkt,
-                                         !state->endianess);
+                                         state->pkt);
        if (!NT_STATUS_IS_OK(status)) {
+               /*
+                * TODO: do a real async disconnect ...
+                *
+                * For now do it sync...
+                */
+               TALLOC_FREE(state->cli->transport);
                tevent_req_nterror(req, status);
                return;
        }
 
-       if (state->incoming_frag.length != state->pkt->frag_length) {
-               DEBUG(5, ("Incorrect pdu length %u, expected %u\n",
-                         (unsigned int)state->incoming_frag.length,
-                         (unsigned int)state->pkt->frag_length));
-               tevent_req_nterror(req,  NT_STATUS_INVALID_PARAMETER);
-               return;
+       if (DEBUGLEVEL >= 10) {
+               NDR_PRINT_DEBUG(ncacn_packet, state->pkt);
        }
 
        status = cli_pipe_validate_current_pdu(state,
                                                state->cli, state->pkt,
                                                &state->incoming_frag,
                                                state->expected_pkt_type,
+                                               state->call_id,
                                                &rdata,
                                                &state->reply_pdu);
 
@@ -884,6 +978,28 @@ static void rpc_api_pipe_got_pdu(struct tevent_req *subreq)
                  (unsigned)state->reply_pdu_offset,
                  nt_errstr(status)));
 
+       if (state->pkt->ptype != DCERPC_PKT_FAULT && !NT_STATUS_IS_OK(status)) {
+               /*
+                * TODO: do a real async disconnect ...
+                *
+                * For now do it sync...
+                */
+               TALLOC_FREE(state->cli->transport);
+       } else if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTOCOL_ERROR)) {
+               /*
+                * TODO: do a real async disconnect ...
+                *
+                * For now do it sync...
+                */
+               TALLOC_FREE(state->cli->transport);
+       } else if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_SEC_PKG_ERROR)) {
+               /*
+                * TODO: do a real async disconnect ...
+                *
+                * For now do it sync...
+                */
+               TALLOC_FREE(state->cli->transport);
+       }
        if (!NT_STATUS_IS_OK(status)) {
                tevent_req_nterror(req, status);
                return;
@@ -908,7 +1024,24 @@ static void rpc_api_pipe_got_pdu(struct tevent_req *subreq)
                         "%s\n",
                         state->endianess?"little":"big",
                         state->pkt->drep[0]?"little":"big"));
-               tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
+               /*
+                * TODO: do a real async disconnect ...
+                *
+                * For now do it sync...
+                */
+               TALLOC_FREE(state->cli->transport);
+               tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
+               return;
+       }
+
+       if (state->reply_pdu_offset + rdata.length > MAX_RPC_DATA_SIZE) {
+               /*
+                * TODO: do a real async disconnect ...
+                *
+                * For now do it sync...
+                */
+               TALLOC_FREE(state->cli->transport);
+               tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
                return;
        }
 
@@ -916,6 +1049,12 @@ static void rpc_api_pipe_got_pdu(struct tevent_req *subreq)
        if (state->reply_pdu.length < state->reply_pdu_offset + rdata.length) {
                if (!data_blob_realloc(NULL, &state->reply_pdu,
                                state->reply_pdu_offset + rdata.length)) {
+                       /*
+                        * TODO: do a real async disconnect ...
+                        *
+                        * For now do it sync...
+                        */
+                       TALLOC_FREE(state->cli->transport);
                        tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
                        return;
                }
@@ -944,6 +1083,14 @@ static void rpc_api_pipe_got_pdu(struct tevent_req *subreq)
 
        subreq = get_complete_frag_send(state, state->ev, state->cli,
                                        &state->incoming_frag);
+       if (subreq == NULL) {
+               /*
+                * TODO: do a real async disconnect ...
+                *
+                * For now do it sync...
+                */
+               TALLOC_FREE(state->cli->transport);
+       }
        if (tevent_req_nomem(subreq, req)) {
                return;
        }
@@ -984,53 +1131,37 @@ static NTSTATUS rpc_api_pipe_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
 
 static NTSTATUS create_generic_auth_rpc_bind_req(struct rpc_pipe_client *cli,
                                                 TALLOC_CTX *mem_ctx,
-                                                DATA_BLOB *auth_token)
+                                                DATA_BLOB *auth_token,
+                                                bool *client_hdr_signing)
 {
        struct gensec_security *gensec_security;
        DATA_BLOB null_blob = data_blob_null;
+       NTSTATUS status;
 
-       gensec_security = talloc_get_type_abort(cli->auth->auth_ctx,
-                                       struct gensec_security);
+       gensec_security = cli->auth->auth_ctx;
 
        DEBUG(5, ("create_generic_auth_rpc_bind_req: generate first token\n"));
-       return gensec_update(gensec_security, mem_ctx, NULL, null_blob, auth_token);
-}
+       status = gensec_update(gensec_security, mem_ctx, null_blob, auth_token);
 
-/*******************************************************************
- Creates schannel auth bind.
- ********************************************************************/
-
-static NTSTATUS create_schannel_auth_rpc_bind_req(struct rpc_pipe_client *cli,
-                                                 DATA_BLOB *auth_token)
-{
-       NTSTATUS status;
-       struct NL_AUTH_MESSAGE r;
-
-       /* Use lp_workgroup() if domain not specified */
-
-       if (!cli->auth->domain || !cli->auth->domain[0]) {
-               cli->auth->domain = talloc_strdup(cli, lp_workgroup());
-               if (cli->auth->domain == NULL) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+       if (!NT_STATUS_IS_OK(status) &&
+           !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED))
+       {
+               return status;
        }
 
-       /*
-        * Now marshall the data into the auth parse_struct.
-        */
-
-       r.MessageType                   = NL_NEGOTIATE_REQUEST;
-       r.Flags                         = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
-                                         NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
-       r.oem_netbios_domain.a          = cli->auth->domain;
-       r.oem_netbios_computer.a        = lp_netbios_name();
+       if (client_hdr_signing == NULL) {
+               return status;
+       }
 
-       status = dcerpc_push_schannel_bind(cli, &r, auth_token);
-       if (!NT_STATUS_IS_OK(status)) {
+       if (cli->auth->auth_level < DCERPC_AUTH_LEVEL_PACKET) {
+               *client_hdr_signing = false;
                return status;
        }
 
-       return NT_STATUS_OK;
+       *client_hdr_signing = gensec_have_feature(gensec_security,
+                                               GENSEC_FEATURE_SIGN_PKT_HEADER);
+
+       return status;
 }
 
 /*******************************************************************
@@ -1039,21 +1170,27 @@ static NTSTATUS create_schannel_auth_rpc_bind_req(struct rpc_pipe_client *cli,
 
 static NTSTATUS create_bind_or_alt_ctx_internal(TALLOC_CTX *mem_ctx,
                                                enum dcerpc_pkt_type ptype,
-                                               uint32 rpc_call_id,
+                                               uint32_t rpc_call_id,
                                                const struct ndr_syntax_id *abstract,
                                                const struct ndr_syntax_id *transfer,
                                                const DATA_BLOB *auth_info,
+                                               bool client_hdr_signing,
                                                DATA_BLOB *blob)
 {
-       uint16 auth_len = auth_info->length;
+       uint16_t auth_len = auth_info->length;
        NTSTATUS status;
        union dcerpc_payload u;
        struct dcerpc_ctx_list ctx_list;
+       uint8_t pfc_flags = DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST;
 
        if (auth_len) {
                auth_len -= DCERPC_AUTH_TRAILER_LENGTH;
        }
 
+       if (client_hdr_signing) {
+               pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
+       }
+
        ctx_list.context_id = 0;
        ctx_list.num_transfer_syntaxes = 1;
        ctx_list.abstract_syntax = *abstract;
@@ -1067,9 +1204,7 @@ static NTSTATUS create_bind_or_alt_ctx_internal(TALLOC_CTX *mem_ctx,
        u.bind.auth_info        = *auth_info;
 
        status = dcerpc_push_ncacn_packet(mem_ctx,
-                                         ptype,
-                                         DCERPC_PFC_FLAG_FIRST |
-                                         DCERPC_PFC_FLAG_LAST,
+                                         ptype, pfc_flags,
                                          auth_len,
                                          rpc_call_id,
                                          &u,
@@ -1089,46 +1224,29 @@ static NTSTATUS create_bind_or_alt_ctx_internal(TALLOC_CTX *mem_ctx,
 static NTSTATUS create_rpc_bind_req(TALLOC_CTX *mem_ctx,
                                    struct rpc_pipe_client *cli,
                                    struct pipe_auth_data *auth,
-                                   uint32 rpc_call_id,
+                                   uint32_t rpc_call_id,
                                    const struct ndr_syntax_id *abstract,
                                    const struct ndr_syntax_id *transfer,
                                    DATA_BLOB *rpc_out)
 {
        DATA_BLOB auth_token = data_blob_null;
        DATA_BLOB auth_info = data_blob_null;
-       NTSTATUS ret = NT_STATUS_OK;
+       NTSTATUS ret;
 
        switch (auth->auth_type) {
-       case DCERPC_AUTH_TYPE_SCHANNEL:
-               ret = create_schannel_auth_rpc_bind_req(cli, &auth_token);
-               if (!NT_STATUS_IS_OK(ret)) {
-                       return ret;
-               }
+       case DCERPC_AUTH_TYPE_NONE:
                break;
 
-       case DCERPC_AUTH_TYPE_NTLMSSP:
-       case DCERPC_AUTH_TYPE_KRB5:
-       case DCERPC_AUTH_TYPE_SPNEGO:
-               ret = create_generic_auth_rpc_bind_req(cli, mem_ctx, &auth_token);
+       default:
+               ret = create_generic_auth_rpc_bind_req(cli, mem_ctx,
+                                                      &auth_token,
+                                                      &auth->client_hdr_signing);
 
                if (!NT_STATUS_IS_OK(ret) &&
                    !NT_STATUS_EQUAL(ret, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
                        return ret;
                }
                break;
-
-       case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
-               auth_token = data_blob_talloc(mem_ctx,
-                                             "NCALRPC_AUTH_TOKEN",
-                                             18);
-               break;
-
-       case DCERPC_AUTH_TYPE_NONE:
-               break;
-
-       default:
-               /* "Can't" happen. */
-               return NT_STATUS_INVALID_INFO_CLASS;
        }
 
        if (auth_token.length != 0) {
@@ -1136,7 +1254,7 @@ static NTSTATUS create_rpc_bind_req(TALLOC_CTX *mem_ctx,
                                                auth->auth_type,
                                                auth->auth_level,
                                                0, /* auth_pad_length */
-                                               1, /* auth_context_id */
+                                               auth->auth_context_id,
                                                &auth_token,
                                                &auth_info);
                if (!NT_STATUS_IS_OK(ret)) {
@@ -1151,6 +1269,7 @@ static NTSTATUS create_rpc_bind_req(TALLOC_CTX *mem_ctx,
                                              abstract,
                                              transfer,
                                              &auth_info,
+                                             auth->client_hdr_signing,
                                              rpc_out);
        return ret;
 }
@@ -1167,22 +1286,29 @@ struct rpc_api_pipe_req_state {
        struct rpc_pipe_client *cli;
        uint8_t op_num;
        uint32_t call_id;
-       DATA_BLOB *req_data;
+       const DATA_BLOB *req_data;
+       const struct GUID *object_uuid;
        uint32_t req_data_sent;
+       DATA_BLOB req_trailer;
+       uint32_t req_trailer_sent;
+       bool verify_bitmask1;
+       bool verify_pcontext;
        DATA_BLOB rpc_out;
        DATA_BLOB reply_pdu;
 };
 
 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_verification_trailer(struct rpc_api_pipe_req_state *state);
 static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
                                  bool *is_last_frag);
 
-struct tevent_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
+static struct tevent_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
                                         struct tevent_context *ev,
                                         struct rpc_pipe_client *cli,
                                         uint8_t op_num,
-                                        DATA_BLOB *req_data)
+                                        const struct GUID *object_uuid,
+                                        const DATA_BLOB *req_data)
 {
        struct tevent_req *req, *subreq;
        struct rpc_api_pipe_req_state *state;
@@ -1197,6 +1323,7 @@ struct tevent_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
        state->ev = ev;
        state->cli = cli;
        state->op_num = op_num;
+       state->object_uuid = object_uuid;
        state->req_data = req_data;
        state->req_data_sent = 0;
        state->call_id = get_rpc_call_id();
@@ -1210,6 +1337,11 @@ struct tevent_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
                goto post_status;
        }
 
+       status = prepare_verification_trailer(state);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto post_status;
+       }
+
        status = prepare_next_frag(state, &is_last_frag);
        if (!NT_STATUS_IS_OK(status)) {
                goto post_status;
@@ -1218,7 +1350,8 @@ struct tevent_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
        if (is_last_frag) {
                subreq = rpc_api_pipe_send(state, ev, state->cli,
                                           &state->rpc_out,
-                                          DCERPC_PKT_RESPONSE);
+                                          DCERPC_PKT_RESPONSE,
+                                          state->call_id);
                if (subreq == NULL) {
                        goto fail;
                }
@@ -1243,25 +1376,163 @@ struct tevent_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
        return NULL;
 }
 
+static NTSTATUS prepare_verification_trailer(struct rpc_api_pipe_req_state *state)
+{
+       struct pipe_auth_data *a = state->cli->auth;
+       struct dcerpc_sec_verification_trailer *t;
+       struct dcerpc_sec_vt *c = NULL;
+       struct ndr_push *ndr = NULL;
+       enum ndr_err_code ndr_err;
+       size_t align = 0;
+       size_t pad = 0;
+
+       if (a == NULL) {
+               return NT_STATUS_OK;
+       }
+
+       if (a->auth_level < DCERPC_AUTH_LEVEL_PACKET) {
+               return NT_STATUS_OK;
+       }
+
+       t = talloc_zero(state, struct dcerpc_sec_verification_trailer);
+       if (t == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (!a->verified_bitmask1) {
+               t->commands = talloc_realloc(t, t->commands,
+                                            struct dcerpc_sec_vt,
+                                            t->count.count + 1);
+               if (t->commands == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               c = &t->commands[t->count.count++];
+               ZERO_STRUCTP(c);
+
+               c->command = DCERPC_SEC_VT_COMMAND_BITMASK1;
+               if (a->client_hdr_signing) {
+                       c->u.bitmask1 = DCERPC_SEC_VT_CLIENT_SUPPORTS_HEADER_SIGNING;
+               }
+               state->verify_bitmask1 = true;
+       }
+
+       if (!state->cli->verified_pcontext) {
+               t->commands = talloc_realloc(t, t->commands,
+                                            struct dcerpc_sec_vt,
+                                            t->count.count + 1);
+               if (t->commands == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               c = &t->commands[t->count.count++];
+               ZERO_STRUCTP(c);
+
+               c->command = DCERPC_SEC_VT_COMMAND_PCONTEXT;
+               c->u.pcontext.abstract_syntax = state->cli->abstract_syntax;
+               c->u.pcontext.transfer_syntax = state->cli->transfer_syntax;
+
+               state->verify_pcontext = true;
+       }
+
+       if (!a->hdr_signing) {
+               t->commands = talloc_realloc(t, t->commands,
+                                            struct dcerpc_sec_vt,
+                                            t->count.count + 1);
+               if (t->commands == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               c = &t->commands[t->count.count++];
+               ZERO_STRUCTP(c);
+
+               c->command = DCERPC_SEC_VT_COMMAND_HEADER2;
+               c->u.header2.ptype = DCERPC_PKT_REQUEST;
+               c->u.header2.drep[0] = DCERPC_DREP_LE;
+               c->u.header2.drep[1] = 0;
+               c->u.header2.drep[2] = 0;
+               c->u.header2.drep[3] = 0;
+               c->u.header2.call_id = state->call_id;
+               c->u.header2.context_id = 0;
+               c->u.header2.opnum = state->op_num;
+       }
+
+       if (t->count.count == 0) {
+               TALLOC_FREE(t);
+               return NT_STATUS_OK;
+       }
+
+       c = &t->commands[t->count.count - 1];
+       c->command |= DCERPC_SEC_VT_COMMAND_END;
+
+       if (DEBUGLEVEL >= 10) {
+               NDR_PRINT_DEBUG(dcerpc_sec_verification_trailer, t);
+       }
+
+       ndr = ndr_push_init_ctx(state);
+       if (ndr == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       ndr_err = ndr_push_dcerpc_sec_verification_trailer(ndr,
+                                               NDR_SCALARS | NDR_BUFFERS,
+                                               t);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               return ndr_map_error2ntstatus(ndr_err);
+       }
+       state->req_trailer = ndr_push_blob(ndr);
+
+       align = state->req_data->length & 0x3;
+       if (align > 0) {
+               pad = 4 - align;
+       }
+       if (pad > 0) {
+               bool ok;
+               uint8_t *p;
+               const uint8_t zeros[4] = { 0, };
+
+               ok = data_blob_append(ndr, &state->req_trailer, zeros, pad);
+               if (!ok) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               /* move the padding to the start */
+               p = state->req_trailer.data;
+               memmove(p + pad, p, state->req_trailer.length - pad);
+               memset(p, 0, pad);
+       }
+
+       return NT_STATUS_OK;
+}
+
 static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
                                  bool *is_last_frag)
 {
-       size_t data_sent_thistime;
        size_t auth_len;
        size_t frag_len;
        uint8_t flags = 0;
        size_t pad_len;
        size_t data_left;
+       size_t data_thistime;
+       size_t trailer_left;
+       size_t trailer_thistime = 0;
+       size_t total_left;
+       size_t total_thistime;
        NTSTATUS status;
+       bool ok;
        union dcerpc_payload u;
 
        data_left = state->req_data->length - state->req_data_sent;
+       trailer_left = state->req_trailer.length - state->req_trailer_sent;
+       total_left = data_left + trailer_left;
+       if ((total_left < data_left) || (total_left < trailer_left)) {
+               /*
+                * overflow
+                */
+               return NT_STATUS_INVALID_PARAMETER_MIX;
+       }
 
        status = dcerpc_guess_sizes(state->cli->auth,
-                                   DCERPC_REQUEST_LENGTH, data_left,
+                                   DCERPC_REQUEST_LENGTH, total_left,
                                    state->cli->max_xmit_frag,
-                                   CLIENT_NDR_PADDING_SIZE,
-                                   &data_sent_thistime,
+                                   &total_thistime,
                                    &frag_len, &auth_len, &pad_len);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -1271,18 +1542,29 @@ static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
                flags = DCERPC_PFC_FLAG_FIRST;
        }
 
-       if (data_sent_thistime == data_left) {
+       if (total_thistime == total_left) {
                flags |= DCERPC_PFC_FLAG_LAST;
        }
 
+       data_thistime = MIN(total_thistime, data_left);
+       if (data_thistime < total_thistime) {
+               trailer_thistime = total_thistime - data_thistime;
+       }
+
        data_blob_free(&state->rpc_out);
 
        ZERO_STRUCT(u.request);
 
-       u.request.alloc_hint    = state->req_data->length;
+       u.request.alloc_hint    = total_left;
        u.request.context_id    = 0;
        u.request.opnum         = state->op_num;
 
+       if (state->object_uuid) {
+               flags |= DCERPC_PFC_FLAG_OBJECT_UUID;
+               u.request.object.object = *state->object_uuid;
+               frag_len += ndr_size_GUID(state->object_uuid, 0);
+       }
+
        status = dcerpc_push_ncacn_packet(state,
                                          DCERPC_PKT_REQUEST,
                                          flags,
@@ -1299,18 +1581,33 @@ static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
         * at this stage */
        dcerpc_set_frag_length(&state->rpc_out, frag_len);
 
-       /* Copy in the data. */
-       if (!data_blob_append(NULL, &state->rpc_out,
+       if (data_thistime > 0) {
+               /* Copy in the data. */
+               ok = data_blob_append(NULL, &state->rpc_out,
                                state->req_data->data + state->req_data_sent,
-                               data_sent_thistime)) {
-               return NT_STATUS_NO_MEMORY;
+                               data_thistime);
+               if (!ok) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               state->req_data_sent += data_thistime;
+       }
+
+       if (trailer_thistime > 0) {
+               /* Copy in the verification trailer. */
+               ok = data_blob_append(NULL, &state->rpc_out,
+                               state->req_trailer.data + state->req_trailer_sent,
+                               trailer_thistime);
+               if (!ok) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               state->req_trailer_sent += trailer_thistime;
        }
 
        switch (state->cli->auth->auth_level) {
        case DCERPC_AUTH_LEVEL_NONE:
        case DCERPC_AUTH_LEVEL_CONNECT:
-       case DCERPC_AUTH_LEVEL_PACKET:
                break;
+       case DCERPC_AUTH_LEVEL_PACKET:
        case DCERPC_AUTH_LEVEL_INTEGRITY:
        case DCERPC_AUTH_LEVEL_PRIVACY:
                status = dcerpc_add_auth_footer(state->cli->auth, pad_len,
@@ -1323,7 +1620,6 @@ static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       state->req_data_sent += data_sent_thistime;
        *is_last_frag = ((flags & DCERPC_PFC_FLAG_LAST) != 0);
 
        return status;
@@ -1354,7 +1650,8 @@ static void rpc_api_pipe_req_write_done(struct tevent_req *subreq)
        if (is_last_frag) {
                subreq = rpc_api_pipe_send(state, state->ev, state->cli,
                                           &state->rpc_out,
-                                          DCERPC_PKT_RESPONSE);
+                                          DCERPC_PKT_RESPONSE,
+                                          state->call_id);
                if (tevent_req_nomem(subreq, req)) {
                        return;
                }
@@ -1386,10 +1683,24 @@ static void rpc_api_pipe_req_done(struct tevent_req *subreq)
                tevent_req_nterror(req, status);
                return;
        }
+
+       if (state->cli->auth == NULL) {
+               tevent_req_done(req);
+               return;
+       }
+
+       if (state->verify_bitmask1) {
+               state->cli->auth->verified_bitmask1 = true;
+       }
+
+       if (state->verify_pcontext) {
+               state->cli->verified_pcontext = true;
+       }
+
        tevent_req_done(req);
 }
 
-NTSTATUS rpc_api_pipe_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+static NTSTATUS rpc_api_pipe_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                               DATA_BLOB *reply_pdu)
 {
        struct rpc_api_pipe_req_state *state = tevent_req_data(
@@ -1441,7 +1752,7 @@ static bool check_bind_response(const struct dcerpc_bind_ack *r,
 
        if (r->num_results != 0x1 || ctx.result != 0) {
                DEBUG(2,("bind_rpc_pipe: bind denied results: %d reason: %x\n",
-                         r->num_results, ctx.reason));
+                         r->num_results, ctx.reason.value));
        }
 
        DEBUG(5,("check_bind_response: accepted!\n"));
@@ -1457,9 +1768,8 @@ static bool check_bind_response(const struct dcerpc_bind_ack *r,
 
 static NTSTATUS create_rpc_bind_auth3(TALLOC_CTX *mem_ctx,
                                struct rpc_pipe_client *cli,
-                               uint32 rpc_call_id,
-                               enum dcerpc_AuthType auth_type,
-                               enum dcerpc_AuthLevel auth_level,
+                               struct pipe_auth_data *auth,
+                               uint32_t rpc_call_id,
                                DATA_BLOB *pauth_blob,
                                DATA_BLOB *rpc_out)
 {
@@ -1469,10 +1779,10 @@ static NTSTATUS create_rpc_bind_auth3(TALLOC_CTX *mem_ctx,
        u.auth3._pad = 0;
 
        status = dcerpc_push_dcerpc_auth(mem_ctx,
-                                        auth_type,
-                                        auth_level,
+                                        auth->auth_type,
+                                        auth->auth_level,
                                         0, /* auth_pad_length */
-                                        1, /* auth_context_id */
+                                        auth->auth_context_id,
                                         pauth_blob,
                                         &u.auth3.auth_info);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1502,9 +1812,8 @@ static NTSTATUS create_rpc_bind_auth3(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static NTSTATUS create_rpc_alter_context(TALLOC_CTX *mem_ctx,
-                                       enum dcerpc_AuthType auth_type,
-                                       enum dcerpc_AuthLevel auth_level,
-                                       uint32 rpc_call_id,
+                                       struct pipe_auth_data *auth,
+                                       uint32_t rpc_call_id,
                                        const struct ndr_syntax_id *abstract,
                                        const struct ndr_syntax_id *transfer,
                                        const DATA_BLOB *pauth_blob, /* spnego auth blob already created. */
@@ -1514,10 +1823,10 @@ static NTSTATUS create_rpc_alter_context(TALLOC_CTX *mem_ctx,
        NTSTATUS status;
 
        status = dcerpc_push_dcerpc_auth(mem_ctx,
-                                        auth_type,
-                                        auth_level,
+                                        auth->auth_type,
+                                        auth->auth_level,
                                         0, /* auth_pad_length */
-                                        1, /* auth_context_id */
+                                        auth->auth_context_id,
                                         pauth_blob,
                                         &auth_info);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1530,6 +1839,7 @@ static NTSTATUS create_rpc_alter_context(TALLOC_CTX *mem_ctx,
                                                 abstract,
                                                 transfer,
                                                 &auth_info,
+                                                false, /* client_hdr_signing */
                                                 rpc_out);
        data_blob_free(&auth_info);
        return status;
@@ -1545,15 +1855,9 @@ struct rpc_pipe_bind_state {
        DATA_BLOB rpc_out;
        bool auth3;
        uint32_t rpc_call_id;
-       struct netr_Authenticator auth;
-       struct netr_Authenticator return_auth;
-       struct netlogon_creds_CredentialState *creds;
-       union netr_Capabilities capabilities;
-       struct netr_LogonGetCapabilities r;
 };
 
 static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq);
-static void rpc_pipe_bind_step_two_trigger(struct tevent_req *req);
 static NTSTATUS rpc_bind_next_send(struct tevent_req *req,
                                   struct rpc_pipe_bind_state *state,
                                   DATA_BLOB *credentials);
@@ -1600,7 +1904,7 @@ struct tevent_req *rpc_pipe_bind_send(TALLOC_CTX *mem_ctx,
        }
 
        subreq = rpc_api_pipe_send(state, ev, cli, &state->rpc_out,
-                                  DCERPC_PKT_BIND_ACK);
+                                  DCERPC_PKT_BIND_ACK, state->rpc_call_id);
        if (subreq == NULL) {
                goto fail;
        }
@@ -1649,45 +1953,63 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
                return;
        }
 
+       if (pkt->ptype == DCERPC_PKT_BIND_ACK) {
+               if (pkt->pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN) {
+                       if (pauth->client_hdr_signing) {
+                               pauth->hdr_signing = true;
+                       }
+               }
+       }
+
        state->cli->max_xmit_frag = pkt->u.bind_ack.max_xmit_frag;
-       state->cli->max_recv_frag = pkt->u.bind_ack.max_recv_frag;
 
        switch(pauth->auth_type) {
 
        case DCERPC_AUTH_TYPE_NONE:
-       case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
                /* Bind complete. */
                tevent_req_done(req);
                return;
 
-       case DCERPC_AUTH_TYPE_SCHANNEL:
-               rpc_pipe_bind_step_two_trigger(req);
-               return;
-
-       case DCERPC_AUTH_TYPE_NTLMSSP:
-       case DCERPC_AUTH_TYPE_SPNEGO:
-       case DCERPC_AUTH_TYPE_KRB5:
-               /* Paranoid lenght checks */
-               if (pkt->frag_length < DCERPC_AUTH_TRAILER_LENGTH
-                                               + pkt->auth_length) {
-                       tevent_req_nterror(req,
-                                       NT_STATUS_INFO_LENGTH_MISMATCH);
+       default:
+               if (pkt->auth_length == 0) {
+                       tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
                        return;
                }
+
                /* get auth credentials */
-               status = dcerpc_pull_dcerpc_auth(talloc_tos(),
-                                                &pkt->u.bind_ack.auth_info,
-                                                &auth, false);
+               status = dcerpc_pull_auth_trailer(pkt, talloc_tos(),
+                                                 &pkt->u.bind_ack.auth_info,
+                                                 &auth, NULL, true);
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(0, ("Failed to pull dcerpc auth: %s.\n",
                                  nt_errstr(status)));
                        tevent_req_nterror(req, status);
                        return;
                }
-               break;
 
-       default:
-               goto err_out;
+               if (auth.auth_type != pauth->auth_type) {
+                       DEBUG(0, (__location__ " Auth type %u mismatch expected %u.\n",
+                                 auth.auth_type, pauth->auth_type));
+                       tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
+                       return;
+               }
+
+               if (auth.auth_level != pauth->auth_level) {
+                       DEBUG(0, (__location__ " Auth level %u mismatch expected %u.\n",
+                                 auth.auth_level, pauth->auth_level));
+                       tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
+                       return;
+               }
+
+               if (auth.auth_context_id != pauth->auth_context_id) {
+                       DEBUG(0, (__location__ " Auth context id %u mismatch expected %u.\n",
+                                 (unsigned)auth.auth_context_id,
+                                 (unsigned)pauth->auth_context_id));
+                       tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
+                       return;
+               }
+
+               break;
        }
 
        /*
@@ -1697,24 +2019,26 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
        switch(pauth->auth_type) {
 
        case DCERPC_AUTH_TYPE_NONE:
-       case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
-       case DCERPC_AUTH_TYPE_SCHANNEL:
                /* Bind complete. */
                tevent_req_done(req);
                return;
 
-       case DCERPC_AUTH_TYPE_NTLMSSP:
-       case DCERPC_AUTH_TYPE_KRB5:
-       case DCERPC_AUTH_TYPE_SPNEGO:
-               gensec_security = talloc_get_type_abort(pauth->auth_ctx,
-                                               struct gensec_security);
-               status = gensec_update(gensec_security, state, NULL,
+       default:
+               gensec_security = pauth->auth_ctx;
+
+
+               status = gensec_update(gensec_security, state,
                                       auth.credentials, &auth_token);
                if (NT_STATUS_EQUAL(status,
                                    NT_STATUS_MORE_PROCESSING_REQUIRED)) {
                        status = rpc_bind_next_send(req, state,
                                                        &auth_token);
                } else if (NT_STATUS_IS_OK(status)) {
+                       if (pauth->hdr_signing) {
+                               gensec_want_feature(gensec_security,
+                                                   GENSEC_FEATURE_SIGN_PKT_HEADER);
+                       }
+
                        if (auth_token.length == 0) {
                                /* Bind complete. */
                                tevent_req_done(req);
@@ -1724,164 +2048,12 @@ static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
                                                        &auth_token);
                }
                break;
-
-       default:
-               goto err_out;
        }
 
        if (!NT_STATUS_IS_OK(status)) {
                tevent_req_nterror(req, status);
        }
        return;
-
-err_out:
-       DEBUG(0,("cli_finish_bind_auth: unknown auth type %u\n",
-                (unsigned int)state->cli->auth->auth_type));
-       tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
-}
-
-static void rpc_pipe_bind_step_two_done(struct tevent_req *subreq);
-
-static void rpc_pipe_bind_step_two_trigger(struct tevent_req *req)
-{
-       struct rpc_pipe_bind_state *state =
-               tevent_req_data(req,
-                               struct rpc_pipe_bind_state);
-       struct dcerpc_binding_handle *b = state->cli->binding_handle;
-       struct schannel_state *schannel_auth =
-               talloc_get_type_abort(state->cli->auth->auth_ctx,
-                                     struct schannel_state);
-       struct tevent_req *subreq;
-
-       if (schannel_auth == NULL ||
-           !ndr_syntax_id_equal(&state->cli->abstract_syntax,
-                                &ndr_table_netlogon.syntax_id)) {
-               tevent_req_done(req);
-               return;
-       }
-
-       ZERO_STRUCT(state->return_auth);
-
-       state->creds = netlogon_creds_copy(state, schannel_auth->creds);
-       if (state->creds == NULL) {
-               tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
-               return;
-       }
-
-       netlogon_creds_client_authenticator(state->creds, &state->auth);
-
-       state->r.in.server_name = state->cli->srv_name_slash;
-       state->r.in.computer_name = state->creds->computer_name;
-       state->r.in.credential = &state->auth;
-       state->r.in.query_level = 1;
-       state->r.in.return_authenticator = &state->return_auth;
-
-       state->r.out.capabilities = &state->capabilities;
-       state->r.out.return_authenticator = &state->return_auth;
-
-       subreq = dcerpc_netr_LogonGetCapabilities_r_send(talloc_tos(),
-                                                        state->ev,
-                                                        b,
-                                                        &state->r);
-       if (subreq == NULL) {
-               tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
-               return;
-       }
-
-       tevent_req_set_callback(subreq, rpc_pipe_bind_step_two_done, req);
-       return;
-}
-
-static void rpc_pipe_bind_step_two_done(struct tevent_req *subreq)
-{
-       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);
-       NTSTATUS status;
-
-       status = dcerpc_netr_LogonGetCapabilities_r_recv(subreq, talloc_tos());
-       TALLOC_FREE(subreq);
-       if (NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE)) {
-               if (state->cli->dc->negotiate_flags &
-                   NETLOGON_NEG_SUPPORTS_AES) {
-                       DEBUG(5, ("AES is not supported and the error was %s\n",
-                                 nt_errstr(status)));
-                       tevent_req_nterror(req,
-                                          NT_STATUS_INVALID_NETWORK_RESPONSE);
-                       return;
-               }
-
-               /* This is probably NT */
-               DEBUG(5, ("We are checking against an NT - %s\n",
-                         nt_errstr(status)));
-               tevent_req_done(req);
-               return;
-       } else if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("dcerpc_netr_LogonGetCapabilities_r_recv failed with %s\n",
-                         nt_errstr(status)));
-               tevent_req_nterror(req, status);
-               return;
-       }
-
-       if (NT_STATUS_EQUAL(state->r.out.result, NT_STATUS_NOT_IMPLEMENTED)) {
-               if (state->creds->negotiate_flags & NETLOGON_NEG_SUPPORTS_AES) {
-                       /* This means AES isn't supported. */
-                       DEBUG(5, ("AES is not supported and the error was %s\n",
-                                 nt_errstr(state->r.out.result)));
-                       tevent_req_nterror(req,
-                                          NT_STATUS_INVALID_NETWORK_RESPONSE);
-                       return;
-               }
-
-               /* This is probably an old Samba version */
-               DEBUG(5, ("We are checking against an old Samba version - %s\n",
-                         nt_errstr(state->r.out.result)));
-               tevent_req_done(req);
-               return;
-       }
-
-       /* We need to check the credential state here, cause win2k3 and earlier
-        * returns NT_STATUS_NOT_IMPLEMENTED */
-       if (!netlogon_creds_client_check(state->creds,
-                                        &state->r.out.return_authenticator->cred)) {
-               /*
-                * Server replied with bad credential. Fail.
-                */
-               DEBUG(0,("rpc_pipe_bind_step_two_done: server %s "
-                        "replied with bad credential\n",
-                        state->cli->desthost));
-               tevent_req_nterror(req, NT_STATUS_UNSUCCESSFUL);
-               return;
-       }
-
-       TALLOC_FREE(state->cli->dc);
-       state->cli->dc = talloc_steal(state->cli, state->creds);
-
-       if (!NT_STATUS_IS_OK(state->r.out.result)) {
-               DEBUG(0, ("dcerpc_netr_LogonGetCapabilities_r_recv failed with %s\n",
-                         nt_errstr(state->r.out.result)));
-               tevent_req_nterror(req, state->r.out.result);
-               return;
-       }
-
-       if (state->creds->negotiate_flags !=
-           state->r.out.capabilities->server_capabilities) {
-               DEBUG(0, ("The client capabilities don't match the server "
-                         "capabilities: local[0x%08X] remote[0x%08X]\n",
-                         state->creds->negotiate_flags,
-                         state->capabilities.server_capabilities));
-               tevent_req_nterror(req,
-                                  NT_STATUS_INVALID_NETWORK_RESPONSE);
-               return;
-       }
-
-       /* TODO: Add downgrade dectection. */
-
-       tevent_req_done(req);
-       return;
 }
 
 static NTSTATUS rpc_bind_next_send(struct tevent_req *req,
@@ -1895,9 +2067,7 @@ static NTSTATUS rpc_bind_next_send(struct tevent_req *req,
        /* Now prepare the alter context pdu. */
        data_blob_free(&state->rpc_out);
 
-       status = create_rpc_alter_context(state,
-                                         auth->auth_type,
-                                         auth->auth_level,
+       status = create_rpc_alter_context(state, auth,
                                          state->rpc_call_id,
                                          &state->cli->abstract_syntax,
                                          &state->cli->transfer_syntax,
@@ -1908,7 +2078,8 @@ static NTSTATUS rpc_bind_next_send(struct tevent_req *req,
        }
 
        subreq = rpc_api_pipe_send(state, state->ev, state->cli,
-                                  &state->rpc_out, DCERPC_PKT_ALTER_RESP);
+                                  &state->rpc_out, DCERPC_PKT_ALTER_RESP,
+                                  state->rpc_call_id);
        if (subreq == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -1929,10 +2100,8 @@ static NTSTATUS rpc_bind_finish_send(struct tevent_req *req,
        /* Now prepare the auth3 context pdu. */
        data_blob_free(&state->rpc_out);
 
-       status = create_rpc_bind_auth3(state, state->cli,
+       status = create_rpc_bind_auth3(state, state->cli, auth,
                                        state->rpc_call_id,
-                                       auth->auth_type,
-                                       auth->auth_level,
                                        auth_token,
                                        &state->rpc_out);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1940,7 +2109,8 @@ static NTSTATUS rpc_bind_finish_send(struct tevent_req *req,
        }
 
        subreq = rpc_api_pipe_send(state, state->ev, state->cli,
-                                  &state->rpc_out, DCERPC_PKT_AUTH3);
+                                  &state->rpc_out, DCERPC_PKT_AUTH3,
+                                  state->rpc_call_id);
        if (subreq == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -1973,8 +2143,7 @@ NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
                goto fail;
        }
 
-       if (!tevent_req_poll(req, ev)) {
-               status = map_nt_error_from_unix(errno);
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
                goto fail;
        }
 
@@ -2041,6 +2210,25 @@ static uint32_t rpccli_bh_set_timeout(struct dcerpc_binding_handle *h,
        return rpccli_set_timeout(hs->rpc_cli, timeout);
 }
 
+static void rpccli_bh_auth_info(struct dcerpc_binding_handle *h,
+                               enum dcerpc_AuthType *auth_type,
+                               enum dcerpc_AuthLevel *auth_level)
+{
+       struct rpccli_bh_state *hs = dcerpc_binding_handle_data(h,
+                                    struct rpccli_bh_state);
+
+       if (hs->rpc_cli == NULL) {
+               return;
+       }
+
+       if (hs->rpc_cli->auth == NULL) {
+               return;
+       }
+
+       *auth_type = hs->rpc_cli->auth->auth_type;
+       *auth_level = hs->rpc_cli->auth->auth_level;
+}
+
 struct rpccli_bh_raw_call_state {
        DATA_BLOB in_data;
        DATA_BLOB out_data;
@@ -2080,7 +2268,7 @@ static struct tevent_req *rpccli_bh_raw_call_send(TALLOC_CTX *mem_ctx,
        }
 
        subreq = rpc_api_pipe_req_send(state, ev, hs->rpc_cli,
-                                      opnum, &state->in_data);
+                                      opnum, object, &state->in_data);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -2165,8 +2353,9 @@ static struct tevent_req *rpccli_bh_disconnect_send(TALLOC_CTX *mem_ctx,
        /*
         * TODO: do a real async disconnect ...
         *
-        * For now the caller needs to free rpc_cli
+        * For now we do it sync...
         */
+       TALLOC_FREE(hs->rpc_cli->transport);
        hs->rpc_cli = NULL;
 
        tevent_req_done(req);
@@ -2220,6 +2409,7 @@ static const struct dcerpc_binding_handle_ops rpccli_bh_ops = {
        .name                   = "rpccli",
        .is_connected           = rpccli_bh_is_connected,
        .set_timeout            = rpccli_bh_set_timeout,
+       .auth_info              = rpccli_bh_auth_info,
        .raw_call_send          = rpccli_bh_raw_call_send,
        .raw_call_recv          = rpccli_bh_raw_call_recv,
        .disconnect_send        = rpccli_bh_disconnect_send,
@@ -2230,15 +2420,17 @@ static const struct dcerpc_binding_handle_ops rpccli_bh_ops = {
 };
 
 /* initialise a rpc_pipe_client binding handle */
-struct dcerpc_binding_handle *rpccli_bh_create(struct rpc_pipe_client *c)
+struct dcerpc_binding_handle *rpccli_bh_create(struct rpc_pipe_client *c,
+                                       const struct GUID *object,
+                                       const struct ndr_interface_table *table)
 {
        struct dcerpc_binding_handle *h;
        struct rpccli_bh_state *hs;
 
        h = dcerpc_binding_handle_create(c,
                                         &rpccli_bh_ops,
-                                        NULL,
-                                        NULL, /* TODO */
+                                        object,
+                                        table,
                                         &hs,
                                         struct rpccli_bh_state,
                                         __location__);
@@ -2250,50 +2442,54 @@ struct dcerpc_binding_handle *rpccli_bh_create(struct rpc_pipe_client *c)
        return h;
 }
 
-NTSTATUS rpccli_ncalrpc_bind_data(TALLOC_CTX *mem_ctx,
-                                 struct pipe_auth_data **presult)
+NTSTATUS rpccli_anon_bind_data(TALLOC_CTX *mem_ctx,
+                              struct pipe_auth_data **presult)
 {
        struct pipe_auth_data *result;
+       struct auth_generic_state *auth_generic_ctx;
+       NTSTATUS status;
 
-       result = talloc(mem_ctx, struct pipe_auth_data);
+       result = talloc_zero(mem_ctx, struct pipe_auth_data);
        if (result == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       result->auth_type = DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM;
-       result->auth_level = DCERPC_AUTH_LEVEL_CONNECT;
+       result->auth_type = DCERPC_AUTH_TYPE_NONE;
+       result->auth_level = DCERPC_AUTH_LEVEL_NONE;
+       result->auth_context_id = 0;
 
-       result->user_name = talloc_strdup(result, "");
-       result->domain = talloc_strdup(result, "");
-       if ((result->user_name == NULL) || (result->domain == NULL)) {
-               TALLOC_FREE(result);
-               return NT_STATUS_NO_MEMORY;
+       status = auth_generic_client_prepare(result,
+                                            &auth_generic_ctx);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("Failed to create auth_generic context: %s\n",
+                         nt_errstr(status)));
        }
 
-       *presult = result;
-       return NT_STATUS_OK;
-}
-
-NTSTATUS rpccli_anon_bind_data(TALLOC_CTX *mem_ctx,
-                              struct pipe_auth_data **presult)
-{
-       struct pipe_auth_data *result;
-
-       result = talloc(mem_ctx, struct pipe_auth_data);
-       if (result == NULL) {
-               return NT_STATUS_NO_MEMORY;
+       status = auth_generic_set_username(auth_generic_ctx, "");
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("Failed to set username: %s\n",
+                         nt_errstr(status)));
        }
 
-       result->auth_type = DCERPC_AUTH_TYPE_NONE;
-       result->auth_level = DCERPC_AUTH_LEVEL_NONE;
+       status = auth_generic_set_domain(auth_generic_ctx, "");
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("Failed to set domain: %s\n",
+                         nt_errstr(status)));
+               return status;
+       }
 
-       result->user_name = talloc_strdup(result, "");
-       result->domain = talloc_strdup(result, "");
-       if ((result->user_name == NULL) || (result->domain == NULL)) {
-               TALLOC_FREE(result);
-               return NT_STATUS_NO_MEMORY;
+       status = gensec_set_credentials(auth_generic_ctx->gensec_security,
+                                       auth_generic_ctx->credentials);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("Failed to set GENSEC credentials: %s\n",
+                         nt_errstr(status)));
+               return status;
        }
+       talloc_unlink(auth_generic_ctx, auth_generic_ctx->credentials);
+       auth_generic_ctx->credentials = NULL;
 
+       result->auth_ctx = talloc_move(result, &auth_generic_ctx->gensec_security);
+       talloc_free(auth_generic_ctx);
        *presult = result;
        return NT_STATUS_OK;
 }
@@ -2307,26 +2503,21 @@ static NTSTATUS rpccli_generic_bind_data(TALLOC_CTX *mem_ctx,
                                         const char *username,
                                         const char *password,
                                         enum credentials_use_kerberos use_kerberos,
+                                        struct netlogon_creds_CredentialState *creds,
                                         struct pipe_auth_data **presult)
 {
        struct auth_generic_state *auth_generic_ctx;
        struct pipe_auth_data *result;
        NTSTATUS status;
 
-       result = talloc(mem_ctx, struct pipe_auth_data);
+       result = talloc_zero(mem_ctx, struct pipe_auth_data);
        if (result == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
        result->auth_type = auth_type;
        result->auth_level = auth_level;
-
-       result->user_name = talloc_strdup(result, username);
-       result->domain = talloc_strdup(result, domain);
-       if ((result->user_name == NULL) || (result->domain == NULL)) {
-               status = NT_STATUS_NO_MEMORY;
-               goto fail;
-       }
+       result->auth_context_id = 1;
 
        status = auth_generic_client_prepare(result,
                                             &auth_generic_ctx);
@@ -2360,6 +2551,7 @@ static NTSTATUS rpccli_generic_bind_data(TALLOC_CTX *mem_ctx,
        }
 
        cli_credentials_set_kerberos_state(auth_generic_ctx->credentials, use_kerberos);
+       cli_credentials_set_netlogon_creds(auth_generic_ctx->credentials, creds);
 
        status = auth_generic_client_start_by_authtype(auth_generic_ctx, auth_type, auth_level);
        if (!NT_STATUS_IS_OK(status)) {
@@ -2376,44 +2568,78 @@ static NTSTATUS rpccli_generic_bind_data(TALLOC_CTX *mem_ctx,
        return status;
 }
 
-NTSTATUS rpccli_schannel_bind_data(TALLOC_CTX *mem_ctx, const char *domain,
-                                  enum dcerpc_AuthLevel auth_level,
-                                  struct netlogon_creds_CredentialState *creds,
-                                  struct pipe_auth_data **presult)
+/* This routine steals the creds pointer that is passed in */
+static NTSTATUS rpccli_generic_bind_data_from_creds(TALLOC_CTX *mem_ctx,
+                                                   enum dcerpc_AuthType auth_type,
+                                                   enum dcerpc_AuthLevel auth_level,
+                                                   const char *server,
+                                                   const char *target_service,
+                                                   struct cli_credentials *creds,
+                                                   struct pipe_auth_data **presult)
 {
-       struct schannel_state *schannel_auth;
+       struct auth_generic_state *auth_generic_ctx;
        struct pipe_auth_data *result;
+       NTSTATUS status;
 
-       result = talloc(mem_ctx, struct pipe_auth_data);
+       result = talloc_zero(mem_ctx, struct pipe_auth_data);
        if (result == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       result->auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
+       result->auth_type = auth_type;
        result->auth_level = auth_level;
+       result->auth_context_id = 1;
+
+       status = auth_generic_client_prepare(result,
+                                            &auth_generic_ctx);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto fail;
+       }
+
+       status = auth_generic_set_creds(auth_generic_ctx, creds);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto fail;
+       }
 
-       result->user_name = talloc_strdup(result, "");
-       result->domain = talloc_strdup(result, domain);
-       if ((result->user_name == NULL) || (result->domain == NULL)) {
+       status = gensec_set_target_service(auth_generic_ctx->gensec_security, target_service);
+       if (!NT_STATUS_IS_OK(status)) {
                goto fail;
        }
 
-       schannel_auth = talloc_zero(result, struct schannel_state);
-       if (schannel_auth == NULL) {
+       status = gensec_set_target_hostname(auth_generic_ctx->gensec_security, server);
+       if (!NT_STATUS_IS_OK(status)) {
                goto fail;
        }
 
-       schannel_auth->state = SCHANNEL_STATE_START;
-       schannel_auth->initiator = true;
-       schannel_auth->creds = netlogon_creds_copy(result, creds);
+       status = auth_generic_client_start_by_authtype(auth_generic_ctx, auth_type, auth_level);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto fail;
+       }
 
-       result->auth_ctx = schannel_auth;
+       result->auth_ctx = talloc_move(result, &auth_generic_ctx->gensec_security);
+       talloc_free(auth_generic_ctx);
        *presult = result;
        return NT_STATUS_OK;
 
  fail:
        TALLOC_FREE(result);
-       return NT_STATUS_NO_MEMORY;
+       return status;
+}
+
+NTSTATUS rpccli_ncalrpc_bind_data(TALLOC_CTX *mem_ctx,
+                                 struct pipe_auth_data **presult)
+{
+       return rpccli_generic_bind_data(mem_ctx,
+                                       DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM,
+                                       DCERPC_AUTH_LEVEL_CONNECT,
+                                       NULL, /* server */
+                                       "host", /* target_service */
+                                       NAME_NT_AUTHORITY, /* domain */
+                                       "SYSTEM",
+                                       NULL, /* password */
+                                       CRED_DONT_USE_KERBEROS,
+                                       NULL, /* netlogon_creds_CredentialState */
+                                       presult);
 }
 
 /**
@@ -2422,7 +2648,7 @@ NTSTATUS rpccli_schannel_bind_data(TALLOC_CTX *mem_ctx, const char *domain,
 static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
                                       const struct sockaddr_storage *ss_addr,
                                       uint16_t port,
-                                      const struct ndr_syntax_id *abstract_syntax,
+                                      const struct ndr_interface_table *table,
                                       struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
@@ -2435,19 +2661,23 @@ static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
                return NT_STATUS_NO_MEMORY;
        }
 
-       result->abstract_syntax = *abstract_syntax;
+       result->abstract_syntax = table->syntax_id;
        result->transfer_syntax = ndr_transfer_syntax_ndr;
 
        result->desthost = talloc_strdup(result, host);
+       if (result->desthost == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
        result->srv_name_slash = talloc_asprintf_strupper_m(
                result, "\\\\%s", result->desthost);
-       if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
+       if (result->srv_name_slash == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
        result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
-       result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
 
        if (ss_addr == NULL) {
                if (!resolve_name(host, &addr, NBT_NAME_SERVER, false)) {
@@ -2472,7 +2702,7 @@ static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
 
        result->transport->transport = NCACN_IP_TCP;
 
-       result->binding_handle = rpccli_bh_create(result);
+       result->binding_handle = rpccli_bh_create(result, NULL, table);
        if (result->binding_handle == NULL) {
                TALLOC_FREE(result);
                return NT_STATUS_NO_MEMORY;
@@ -2493,7 +2723,7 @@ static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
  */
 static NTSTATUS rpc_pipe_get_tcp_port(const char *host,
                                      const struct sockaddr_storage *addr,
-                                     const struct ndr_syntax_id *abstract_syntax,
+                                     const struct ndr_interface_table *table,
                                      uint16_t *pport)
 {
        NTSTATUS status;
@@ -2502,6 +2732,8 @@ static NTSTATUS rpc_pipe_get_tcp_port(const char *host,
        struct pipe_auth_data *auth = NULL;
        struct dcerpc_binding *map_binding = NULL;
        struct dcerpc_binding *res_binding = NULL;
+       enum dcerpc_transport_t transport;
+       const char *endpoint = NULL;
        struct epm_twr_t *map_tower = NULL;
        struct epm_twr_t *res_towers = NULL;
        struct policy_handle *entry_handle = NULL;
@@ -2516,15 +2748,16 @@ static NTSTATUS rpc_pipe_get_tcp_port(const char *host,
                goto done;
        }
 
-       if (ndr_syntax_id_equal(abstract_syntax,
+       if (ndr_syntax_id_equal(&table->syntax_id,
                                &ndr_table_epmapper.syntax_id)) {
                *pport = 135;
-               return NT_STATUS_OK;
+               status = NT_STATUS_OK;
+               goto done;
        }
 
        /* open the connection to the endpoint mapper */
        status = rpc_pipe_open_tcp_port(tmp_ctx, host, addr, 135,
-                                       &ndr_table_epmapper.syntax_id,
+                                       &ndr_table_epmapper,
                                        &epm_pipe);
 
        if (!NT_STATUS_IS_OK(status)) {
@@ -2544,16 +2777,17 @@ static NTSTATUS rpc_pipe_get_tcp_port(const char *host,
 
        /* create tower for asking the epmapper */
 
-       map_binding = talloc_zero(tmp_ctx, struct dcerpc_binding);
-       if (map_binding == NULL) {
-               status = NT_STATUS_NO_MEMORY;
+       status = dcerpc_parse_binding(tmp_ctx, "ncacn_ip_tcp:[135]",
+                                     &map_binding);
+       if (!NT_STATUS_IS_OK(status)) {
                goto done;
        }
 
-       map_binding->transport = NCACN_IP_TCP;
-       map_binding->object = *abstract_syntax;
-       map_binding->host = host; /* needed? */
-       map_binding->endpoint = "0"; /* correct? needed? */
+       status = dcerpc_binding_set_abstract_syntax(map_binding,
+                                                   &table->syntax_id);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
 
        map_tower = talloc_zero(tmp_ctx, struct epm_twr_t);
        if (map_tower == NULL) {
@@ -2587,7 +2821,7 @@ static NTSTATUS rpc_pipe_get_tcp_port(const char *host,
        status = dcerpc_epm_Map(epm_handle,
                                tmp_ctx,
                                discard_const_p(struct GUID,
-                                             &(abstract_syntax->uuid)),
+                                             &(table->syntax_id.uuid)),
                                map_tower,
                                entry_handle,
                                max_towers,
@@ -2618,13 +2852,21 @@ static NTSTATUS rpc_pipe_get_tcp_port(const char *host,
                goto done;
        }
 
+       transport = dcerpc_binding_get_transport(res_binding);
+       endpoint = dcerpc_binding_get_string_option(res_binding, "endpoint");
+
        /* are further checks here necessary? */
-       if (res_binding->transport != NCACN_IP_TCP) {
-               status = NT_STATUS_UNSUCCESSFUL;
+       if (transport != NCACN_IP_TCP) {
+               status = NT_STATUS_INVALID_NETWORK_RESPONSE;
                goto done;
        }
 
-       *pport = (uint16_t)atoi(res_binding->endpoint);
+       if (endpoint == NULL) {
+               status = NT_STATUS_INVALID_NETWORK_RESPONSE;
+               goto done;
+       }
+
+       *pport = (uint16_t)atoi(endpoint);
 
 done:
        TALLOC_FREE(tmp_ctx);
@@ -2638,26 +2880,26 @@ done:
  */
 NTSTATUS rpc_pipe_open_tcp(TALLOC_CTX *mem_ctx, const char *host,
                           const struct sockaddr_storage *addr,
-                          const struct ndr_syntax_id *abstract_syntax,
+                          const struct ndr_interface_table *table,
                           struct rpc_pipe_client **presult)
 {
        NTSTATUS status;
        uint16_t port = 0;
 
-       status = rpc_pipe_get_tcp_port(host, addr, abstract_syntax, &port);
+       status = rpc_pipe_get_tcp_port(host, addr, table, &port);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
        return rpc_pipe_open_tcp_port(mem_ctx, host, addr, port,
-                                       abstract_syntax, presult);
+                                     table, presult);
 }
 
 /********************************************************************
  Create a rpc pipe client struct, connecting to a unix domain socket
  ********************************************************************/
 NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
-                              const struct ndr_syntax_id *abstract_syntax,
+                              const struct ndr_interface_table *table,
                               struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
@@ -2671,19 +2913,23 @@ NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
                return NT_STATUS_NO_MEMORY;
        }
 
-       result->abstract_syntax = *abstract_syntax;
+       result->abstract_syntax = table->syntax_id;
        result->transfer_syntax = ndr_transfer_syntax_ndr;
 
        result->desthost = get_myname(result);
+       if (result->desthost == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
        result->srv_name_slash = talloc_asprintf_strupper_m(
                result, "\\\\%s", result->desthost);
-       if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
+       if (result->srv_name_slash == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
        result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
-       result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
 
        fd = socket(AF_UNIX, SOCK_STREAM, 0);
        if (fd == -1) {
@@ -2711,7 +2957,7 @@ NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
 
        result->transport->transport = NCALRPC;
 
-       result->binding_handle = rpccli_bh_create(result);
+       result->binding_handle = rpccli_bh_create(result, NULL, table);
        if (result->binding_handle == NULL) {
                TALLOC_FREE(result);
                return NT_STATUS_NO_MEMORY;
@@ -2750,7 +2996,7 @@ static int rpc_pipe_client_np_ref_destructor(struct rpc_pipe_client_np_ref *np_r
  ****************************************************************************/
 
 static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
-                                const struct ndr_syntax_id *abstract_syntax,
+                                const struct ndr_interface_table *table,
                                 struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
@@ -2768,21 +3014,26 @@ static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
                return NT_STATUS_NO_MEMORY;
        }
 
-       result->abstract_syntax = *abstract_syntax;
+       result->abstract_syntax = table->syntax_id;
        result->transfer_syntax = ndr_transfer_syntax_ndr;
-       result->desthost = talloc_strdup(result, smbXcli_conn_remote_name(cli->conn));
-       result->srv_name_slash = talloc_asprintf_strupper_m(
-               result, "\\\\%s", result->desthost);
 
-       result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
-       result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
+       result->desthost = talloc_strdup(
+               result, smbXcli_conn_remote_name(cli->conn));
+       if (result->desthost == NULL) {
+               TALLOC_FREE(result);
+               return NT_STATUS_NO_MEMORY;
+       }
 
-       if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
+       result->srv_name_slash = talloc_asprintf_strupper_m(
+               result, "\\\\%s", result->desthost);
+       if (result->srv_name_slash == NULL) {
                TALLOC_FREE(result);
                return NT_STATUS_NO_MEMORY;
        }
 
-       status = rpc_transport_np_init(result, cli, abstract_syntax,
+       result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
+
+       status = rpc_transport_np_init(result, cli, table,
                                       &result->transport);
        if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(result);
@@ -2802,7 +3053,7 @@ static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
        DLIST_ADD(np_ref->cli->pipe_list, np_ref->pipe);
        talloc_set_destructor(np_ref, rpc_pipe_client_np_ref_destructor);
 
-       result->binding_handle = rpccli_bh_create(result);
+       result->binding_handle = rpccli_bh_create(result, NULL, table);
        if (result->binding_handle == NULL) {
                TALLOC_FREE(result);
                return NT_STATUS_NO_MEMORY;
@@ -2818,7 +3069,7 @@ static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
 
 static NTSTATUS cli_rpc_pipe_open(struct cli_state *cli,
                                  enum dcerpc_transport_t transport,
-                                 const struct ndr_syntax_id *interface,
+                                 const struct ndr_interface_table *table,
                                  struct rpc_pipe_client **presult)
 {
        switch (transport) {
@@ -2826,9 +3077,9 @@ static NTSTATUS cli_rpc_pipe_open(struct cli_state *cli,
                return rpc_pipe_open_tcp(NULL,
                                         smbXcli_conn_remote_name(cli->conn),
                                         smbXcli_conn_remote_sockaddr(cli->conn),
-                                        interface, presult);
+                                        table, presult);
        case NCACN_NP:
-               return rpc_pipe_open_np(cli, interface, presult);
+               return rpc_pipe_open_np(cli, table, presult);
        default:
                return NT_STATUS_NOT_IMPLEMENTED;
        }
@@ -2840,14 +3091,14 @@ static NTSTATUS cli_rpc_pipe_open(struct cli_state *cli,
 
 NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
                                            enum dcerpc_transport_t transport,
-                                           const struct ndr_syntax_id *interface,
+                                           const struct ndr_interface_table *table,
                                            struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
        struct pipe_auth_data *auth;
        NTSTATUS status;
 
-       status = cli_rpc_pipe_open(cli, transport, interface, &result);
+       status = cli_rpc_pipe_open(cli, transport, table, &result);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -2866,12 +3117,6 @@ NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
         * from the enclosing SMB creds
         */
 
-       TALLOC_FREE(auth->user_name);
-       TALLOC_FREE(auth->domain);
-
-       auth->user_name = talloc_strdup(auth, cli->user_name);
-       auth->domain = talloc_strdup(auth, cli->domain);
-
        if (transport == NCACN_NP) {
                struct smbXcli_session *session;
 
@@ -2888,15 +3133,10 @@ NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
                }
        }
 
-       if ((auth->user_name == NULL) || (auth->domain == NULL)) {
-               TALLOC_FREE(result);
-               return NT_STATUS_NO_MEMORY;
-       }
-
        status = rpc_pipe_bind(result, auth);
        if (!NT_STATUS_IS_OK(status)) {
                int lvl = 0;
-               if (ndr_syntax_id_equal(interface,
+               if (ndr_syntax_id_equal(&table->syntax_id,
                                        &ndr_table_dssetup.syntax_id)) {
                        /* non AD domains just don't have this pipe, avoid
                         * level 0 statement in that case - gd */
@@ -2904,7 +3144,7 @@ NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
                }
                DEBUG(lvl, ("cli_rpc_pipe_open_noauth: rpc_pipe_bind for pipe "
                            "%s failed with error %s\n",
-                           get_pipe_name_from_syntax(talloc_tos(), interface),
+                           table->name,
                            nt_errstr(status) ));
                TALLOC_FREE(result);
                return status;
@@ -2912,7 +3152,7 @@ NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
 
        DEBUG(10,("cli_rpc_pipe_open_noauth: opened pipe %s to machine "
                  "%s and bound anonymously.\n",
-                 get_pipe_name_from_syntax(talloc_tos(), interface),
+                 table->name,
                  result->desthost));
 
        *presult = result;
@@ -2923,61 +3163,60 @@ NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
  ****************************************************************************/
 
 NTSTATUS cli_rpc_pipe_open_noauth(struct cli_state *cli,
-                                 const struct ndr_syntax_id *interface,
+                                 const struct ndr_interface_table *table,
                                  struct rpc_pipe_client **presult)
 {
        return cli_rpc_pipe_open_noauth_transport(cli, NCACN_NP,
-                                                 interface, presult);
+                                                 table, presult);
 }
 
 /****************************************************************************
  Open a named pipe to an SMB server and bind using the mech specified
+
+ This routine references the creds pointer that is passed in
  ****************************************************************************/
 
-NTSTATUS cli_rpc_pipe_open_generic_auth(struct cli_state *cli,
-                                       const struct ndr_interface_table *table,
-                                       enum dcerpc_transport_t transport,
-                                       enum dcerpc_AuthType auth_type,
-                                       enum dcerpc_AuthLevel auth_level,
-                                       const char *server,
-                                       const char *domain,
-                                       const char *username,
-                                       const char *password,
-                                       struct rpc_pipe_client **presult)
+NTSTATUS cli_rpc_pipe_open_with_creds(struct cli_state *cli,
+                                     const struct ndr_interface_table *table,
+                                     enum dcerpc_transport_t transport,
+                                     enum dcerpc_AuthType auth_type,
+                                     enum dcerpc_AuthLevel auth_level,
+                                     const char *server,
+                                     struct cli_credentials *creds,
+                                     struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
        struct pipe_auth_data *auth = NULL;
        const char *target_service = table->authservices->names[0];
-       
        NTSTATUS status;
 
-       status = cli_rpc_pipe_open(cli, transport, &table->syntax_id, &result);
+       status = cli_rpc_pipe_open(cli, transport, table, &result);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       status = rpccli_generic_bind_data(result,
-                                         auth_type, auth_level,
-                                         server, target_service,
-                                         domain, username, password, 
-                                         CRED_AUTO_USE_KERBEROS,
-                                         &auth);
+       status = rpccli_generic_bind_data_from_creds(result,
+                                                    auth_type, auth_level,
+                                                    server, target_service,
+                                                    creds,
+                                                    &auth);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("rpccli_generic_bind_data returned %s\n",
-                         nt_errstr(status)));
+               DBG_ERR("rpccli_generic_bind_data_from_creds returned %s\n",
+                       nt_errstr(status));
                goto err;
        }
 
        status = rpc_pipe_bind(result, auth);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("cli_rpc_pipe_open_generic_auth: cli_rpc_pipe_bind failed with error %s\n",
-                       nt_errstr(status) ));
+               DBG_ERR("cli_rpc_pipe_bind failed with error %s\n",
+                       nt_errstr(status));
                goto err;
        }
 
-       DEBUG(10,("cli_rpc_pipe_open_generic_auth: opened pipe %s to "
-               "machine %s and bound as user %s\\%s.\n", table->name,
-                 result->desthost, domain, username));
+       DBG_DEBUG("opened pipe %s to machine %s and bound as user %s.\n",
+                 table->name,
+                 result->desthost,
+                 cli_credentials_get_unparsed_name(creds, talloc_tos()));
 
        *presult = result;
        return NT_STATUS_OK;
@@ -2988,131 +3227,123 @@ NTSTATUS cli_rpc_pipe_open_generic_auth(struct cli_state *cli,
        return status;
 }
 
-/****************************************************************************
- External interface.
- Open a named pipe to an SMB server and bind using schannel (bind type 68)
- using session_key. sign and seal.
-
- The *pdc will be stolen onto this new pipe
- ****************************************************************************/
-
-NTSTATUS cli_rpc_pipe_open_schannel_with_key(struct cli_state *cli,
-                                            const struct ndr_syntax_id *interface,
-                                            enum dcerpc_transport_t transport,
-                                            enum dcerpc_AuthLevel auth_level,
-                                            const char *domain,
-                                            struct netlogon_creds_CredentialState **pdc,
-                                            struct rpc_pipe_client **presult)
+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 *result;
-       struct pipe_auth_data *auth;
+       struct rpc_pipe_client *rpccli;
+       struct pipe_auth_data *rpcauth;
+       const char *target_service = table->authservices->names[0];
+       struct cli_credentials *cli_creds;
+       enum dcerpc_AuthLevel auth_level;
        NTSTATUS status;
 
-       status = cli_rpc_pipe_open(cli, transport, interface, &result);
+       status = cli_rpc_pipe_open(cli, transport, table, &rpccli);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       status = rpccli_schannel_bind_data(result, domain, auth_level,
-                                          *pdc, &auth);
+       auth_level = netlogon_creds_cli_auth_level(netlogon_creds);
+
+       status = netlogon_creds_bind_cli_credentials(
+               netlogon_creds, rpccli, &cli_creds);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("rpccli_schannel_bind_data returned %s\n",
-                         nt_errstr(status)));
-               TALLOC_FREE(result);
+               DBG_DEBUG("netlogon_creds_bind_cli_credentials failed: %s\n",
+                         nt_errstr(status));
+               TALLOC_FREE(rpccli);
                return status;
        }
 
-       status = rpc_pipe_bind(result, auth);
+       status = rpccli_generic_bind_data_from_creds(rpccli,
+                                                    DCERPC_AUTH_TYPE_SCHANNEL,
+                                                    auth_level,
+                                                    rpccli->desthost,
+                                                    target_service,
+                                                    cli_creds,
+                                                    &rpcauth);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("cli_rpc_pipe_open_schannel_with_key: "
-                         "cli_rpc_pipe_bind failed with error %s\n",
-                         nt_errstr(status) ));
-               TALLOC_FREE(result);
+               DEBUG(0, ("rpccli_generic_bind_data_from_creds returned %s\n",
+                         nt_errstr(status)));
+               TALLOC_FREE(rpccli);
                return status;
        }
 
-       /*
-        * The credentials on a new netlogon pipe are the ones we are passed
-        * in - copy them over
-        */
-       if (result->dc == NULL) {
-               result->dc = netlogon_creds_copy(result, *pdc);
-               if (result->dc == NULL) {
-                       TALLOC_FREE(result);
-                       return NT_STATUS_NO_MEMORY;
-               }
+       status = rpc_pipe_bind(rpccli, rpcauth);
+
+       /* No TALLOC_FREE, gensec takes references */
+       talloc_unlink(rpccli, cli_creds);
+       cli_creds = NULL;
+
+       if (!NT_STATUS_IS_OK(status)) {
+               DBG_DEBUG("rpc_pipe_bind failed with error %s\n",
+                         nt_errstr(status));
+               TALLOC_FREE(rpccli);
+               return status;
        }
 
-       DEBUG(10,("cli_rpc_pipe_open_schannel_with_key: opened pipe %s to machine %s "
-                 "for domain %s and bound using schannel.\n",
-                 get_pipe_name_from_syntax(talloc_tos(), interface),
-                 result->desthost, domain));
+       *_rpccli = rpccli;
 
-       *presult = result;
        return NT_STATUS_OK;
 }
 
-NTSTATUS cli_rpc_pipe_open_spnego(struct cli_state *cli,
-                                 const struct ndr_interface_table *table,
-                                 enum dcerpc_transport_t transport,
-                                 const char *oid,
-                                 enum dcerpc_AuthLevel auth_level,
-                                 const char *server,
-                                 const char *domain,
-                                 const char *username,
-                                 const char *password,
-                                 struct rpc_pipe_client **presult)
+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)
 {
-       struct rpc_pipe_client *result;
-       struct pipe_auth_data *auth = NULL;
-       const char *target_service = table->authservices->names[0];
-       
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct rpc_pipe_client *rpccli;
+       struct netlogon_creds_cli_lck *lck;
        NTSTATUS status;
-       enum credentials_use_kerberos use_kerberos;
 
-       if (strcmp(oid, GENSEC_OID_KERBEROS5) == 0) {
-               use_kerberos = CRED_MUST_USE_KERBEROS;
-       } else if (strcmp(oid, GENSEC_OID_NTLMSSP) == 0) {
-               use_kerberos = CRED_DONT_USE_KERBEROS;
-       } else {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       status = cli_rpc_pipe_open(cli, transport, &table->syntax_id, &result);
+       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 = rpccli_generic_bind_data(result,
-                                         DCERPC_AUTH_TYPE_SPNEGO, auth_level,
-                                         server, target_service,
-                                         domain, username, password, 
-                                         use_kerberos,
-                                         &auth);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("rpccli_generic_bind_data returned %s\n",
-                         nt_errstr(status)));
-               goto err;
+       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);
        }
-
-       status = rpc_pipe_bind(result, auth);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("cli_rpc_pipe_open_spnego: cli_rpc_pipe_bind failed with error %s\n",
-                       nt_errstr(status) ));
-               goto err;
+               DBG_DEBUG("cli_rpc_pipe_open_bind_schannel failed: %s\n",
+                         nt_errstr(status));
+               TALLOC_FREE(frame);
+               return status;
        }
 
-       DEBUG(10,("cli_rpc_pipe_open_spnego: opened pipe %s to "
-                 "machine %s.\n", table->name,
-                 result->desthost));
+       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;
+               }
+       }
 
-       *presult = result;
-       return NT_STATUS_OK;
+       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));
 
-  err:
+       TALLOC_FREE(frame);
 
-       TALLOC_FREE(result);
-       return status;
+       *_rpccli = rpccli;
+       return NT_STATUS_OK;
 }
 
 NTSTATUS cli_get_session_key(TALLOC_CTX *mem_ctx,
@@ -3121,7 +3352,6 @@ NTSTATUS cli_get_session_key(TALLOC_CTX *mem_ctx,
 {
        NTSTATUS status;
        struct pipe_auth_data *a;
-       struct schannel_state *schannel_auth;
        struct gensec_security *gensec_security;
        DATA_BLOB sk = data_blob_null;
        bool make_dup = false;
@@ -3137,31 +3367,19 @@ NTSTATUS cli_get_session_key(TALLOC_CTX *mem_ctx,
        }
 
        switch (cli->auth->auth_type) {
-       case DCERPC_AUTH_TYPE_SCHANNEL:
-               schannel_auth = talloc_get_type_abort(a->auth_ctx,
-                                                     struct schannel_state);
-               sk = data_blob_const(schannel_auth->creds->session_key, 16);
+       case DCERPC_AUTH_TYPE_NONE:
+               sk = data_blob_const(a->transport_session_key.data,
+                                    a->transport_session_key.length);
                make_dup = true;
                break;
-       case DCERPC_AUTH_TYPE_SPNEGO:
-       case DCERPC_AUTH_TYPE_NTLMSSP:
-       case DCERPC_AUTH_TYPE_KRB5:
-               gensec_security = talloc_get_type_abort(a->auth_ctx,
-                                               struct gensec_security);
+       default:
+               gensec_security = a->auth_ctx;
                status = gensec_session_key(gensec_security, mem_ctx, &sk);
                if (!NT_STATUS_IS_OK(status)) {
                        return status;
                }
                make_dup = false;
                break;
-       case DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM:
-       case DCERPC_AUTH_TYPE_NONE:
-               sk = data_blob_const(a->transport_session_key.data,
-                                    a->transport_session_key.length);
-               make_dup = true;
-               break;
-       default:
-               break;
        }
 
        if (!sk.data) {