s3:rpc_server: handle everything but AUTH_TYPE_NONE as gensec in verify_final
[obnox/samba/samba-obnox.git] / source3 / rpc_server / srv_pipe.c
index 9be2bc8d729b1c610860b555606d5d6daf306ade..aaf58871d3ecb9c7949a141c6850ee83a82dd566 100644 (file)
@@ -30,9 +30,8 @@
 #include "includes.h"
 #include "system/filesys.h"
 #include "srv_pipe_internal.h"
-#include "../librpc/gen_ndr/ndr_schannel.h"
-#include "../libcli/auth/schannel.h"
-#include "../libcli/auth/spnego.h"
+#include "../librpc/gen_ndr/dcerpc.h"
+#include "../librpc/rpc/rpc_common.h"
 #include "dcesrv_auth_generic.h"
 #include "rpc_server.h"
 #include "rpc_dce.h"
@@ -42,6 +41,9 @@
 #include "rpc_server/srv_pipe.h"
 #include "rpc_server/rpc_contexts.h"
 #include "lib/param/param.h"
+#include "librpc/ndr/ndr_table.h"
+#include "auth/gensec/gensec.h"
+#include "librpc/ndr/ndr_dcerpc.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_SRV
@@ -202,7 +204,7 @@ bool create_next_pdu(struct pipes_struct *p)
         * the pipe gets closed. JRA.
         */
        if (p->fault_state) {
-               setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
+               setup_fault_pdu(p, NT_STATUS(p->fault_state));
                return true;
        }
 
@@ -337,11 +339,12 @@ static bool check_bind_req(struct pipes_struct *p,
        bool ok;
 
        DEBUG(3,("check_bind_req for %s\n",
-                get_pipe_name_from_syntax(talloc_tos(), abstract)));
+                ndr_interface_name(&abstract->uuid,
+                                   abstract->if_version)));
 
        /* we have to check all now since win2k introduced a new UUID on the lsaprpc pipe */
        if (rpc_srv_pipe_exists_by_id(abstract) &&
-          ndr_syntax_id_equal(transfer, &ndr_transfer_syntax)) {
+          ndr_syntax_id_equal(transfer, &ndr_transfer_syntax_ndr)) {
                DEBUG(3, ("check_bind_req: %s -> %s rpc service\n",
                          rpc_srv_get_pipe_cli_name(abstract),
                          rpc_srv_get_pipe_srv_name(abstract)));
@@ -376,22 +379,13 @@ static bool check_bind_req(struct pipes_struct *p,
 
 /**
  * Is a named pipe known?
- * @param[in] cli_filename     The pipe name requested by the client
+ * @param[in] pipename         Just the filename
  * @result                     Do we want to serve this?
  */
-bool is_known_pipename(const char *cli_filename, struct ndr_syntax_id *syntax)
+bool is_known_pipename(const char *pipename, struct ndr_syntax_id *syntax)
 {
-       const char *pipename = cli_filename;
        NTSTATUS status;
 
-       if (strnequal(pipename, "\\PIPE\\", 6)) {
-               pipename += 5;
-       }
-
-       if (*pipename == '\\') {
-               pipename += 1;
-       }
-
        if (lp_disable_spoolss() && strequal(pipename, "spoolss")) {
                DEBUG(10, ("refusing spoolss access\n"));
                return false;
@@ -403,7 +397,7 @@ bool is_known_pipename(const char *cli_filename, struct ndr_syntax_id *syntax)
 
        status = smb_probe_module("rpc", pipename);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(10, ("is_known_pipename: %s unknown\n", cli_filename));
+               DEBUG(10, ("is_known_pipename: %s unknown\n", pipename));
                return false;
        }
        DEBUG(10, ("is_known_pipename: %s loaded dynamically\n", pipename));
@@ -421,133 +415,16 @@ bool is_known_pipename(const char *cli_filename, struct ndr_syntax_id *syntax)
        return false;
 }
 
-/*******************************************************************
- Handle an schannel bind auth.
-*******************************************************************/
-
-static bool pipe_schannel_auth_bind(struct pipes_struct *p,
-                                   TALLOC_CTX *mem_ctx,
-                                   struct dcerpc_auth *auth_info,
-                                   DATA_BLOB *response)
-{
-       struct NL_AUTH_MESSAGE neg;
-       struct NL_AUTH_MESSAGE reply;
-       bool ret;
-       NTSTATUS status;
-       struct netlogon_creds_CredentialState *creds;
-       enum ndr_err_code ndr_err;
-       struct schannel_state *schannel_auth;
-       struct loadparm_context *lp_ctx;
-
-       ndr_err = ndr_pull_struct_blob(
-                       &auth_info->credentials, mem_ctx, &neg,
-                       (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               DEBUG(0,("pipe_schannel_auth_bind: Could not unmarshal SCHANNEL auth neg\n"));
-               return false;
-       }
-
-       if (DEBUGLEVEL >= 10) {
-               NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &neg);
-       }
-
-       if (!(neg.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME)) {
-               DEBUG(0,("pipe_schannel_auth_bind: Did not receive netbios computer name\n"));
-               return false;
-       }
-
-       lp_ctx = loadparm_init_s3(p, loadparm_s3_context());
-       if (!lp_ctx) {
-               DEBUG(0,("pipe_schannel_auth_bind: loadparm_init_s3() failed!\n"));
-               return false;
-       }
-
-       /*
-        * The neg.oem_netbios_computer.a key here must match the remote computer name
-        * given in the DOM_CLNT_SRV.uni_comp_name used on all netlogon pipe
-        * operations that use credentials.
-        */
-
-       become_root();
-       status = schannel_get_creds_state(p, lp_ctx,
-                                           neg.oem_netbios_computer.a, &creds);
-       unbecome_root();
-       
-       talloc_unlink(p, lp_ctx);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("pipe_schannel_auth_bind: Attempt to bind using schannel without successful serverauth2\n"));
-               return False;
-       }
-
-       schannel_auth = talloc(p, struct schannel_state);
-       if (!schannel_auth) {
-               TALLOC_FREE(creds);
-               return False;
-       }
-
-       schannel_auth->state = SCHANNEL_STATE_START;
-       schannel_auth->seq_num = 0;
-       schannel_auth->initiator = false;
-       schannel_auth->creds = creds;
-
-       /*
-        * JRA. Should we also copy the schannel session key into the pipe session key p->session_key
-        * here ? We do that for NTLMSSP, but the session key is already set up from the vuser
-        * struct of the person who opened the pipe. I need to test this further. JRA.
-        *
-        * VL. As we are mapping this to guest set the generic key
-        * "SystemLibraryDTC" key here. It's a bit difficult to test against
-        * W2k3, as it does not allow schannel binds against SAMR and LSA
-        * anymore.
-        */
-
-       ret = session_info_set_session_key(p->session_info, generic_session_key());
-
-       if (!ret) {
-               DEBUG(0, ("session_info_set_session_key failed\n"));
-               return false;
-       }
-
-       /*** SCHANNEL verifier ***/
-
-       reply.MessageType                       = NL_NEGOTIATE_RESPONSE;
-       reply.Flags                             = 0;
-       reply.Buffer.dummy                      = 5; /* ??? actually I don't think
-                                                     * this has any meaning
-                                                     * here - gd */
-
-       ndr_err = ndr_push_struct_blob(response, mem_ctx, &reply,
-                      (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
-       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
-               DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
-               return false;
-       }
-
-       if (DEBUGLEVEL >= 10) {
-               NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &reply);
-       }
-
-       DEBUG(10,("pipe_schannel_auth_bind: schannel auth: domain [%s] myname [%s]\n",
-               neg.oem_netbios_domain.a, neg.oem_netbios_computer.a));
-
-       /* We're finished with this bind - no more packets. */
-       p->auth.auth_ctx = schannel_auth;
-       p->auth.auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
-
-       p->pipe_bound = True;
-
-       return True;
-}
-
 /*******************************************************************
  Handle an NTLMSSP bind auth.
 *******************************************************************/
 
 static bool pipe_auth_generic_bind(struct pipes_struct *p,
-                                  TALLOC_CTX *mem_ctx,
+                                  struct ncacn_packet *pkt,
                                   struct dcerpc_auth *auth_info,
                                   DATA_BLOB *response)
 {
+       TALLOC_CTX *mem_ctx = pkt;
        struct gensec_security *gensec_security = NULL;
         NTSTATUS status;
 
@@ -570,6 +447,17 @@ static bool pipe_auth_generic_bind(struct pipes_struct *p,
        p->auth.auth_ctx = gensec_security;
        p->auth.auth_type = auth_info->auth_type;
 
+       if (pkt->pfc_flags & DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN) {
+               p->auth.client_hdr_signing = true;
+               p->auth.hdr_signing = gensec_have_feature(gensec_security,
+                                               GENSEC_FEATURE_SIGN_PKT_HEADER);
+       }
+
+       if (p->auth.hdr_signing) {
+               gensec_want_feature(gensec_security,
+                                   GENSEC_FEATURE_SIGN_PKT_HEADER);
+       }
+
        return true;
 }
 
@@ -638,22 +526,23 @@ static bool pipe_auth_generic_verify_final(TALLOC_CTX *mem_ctx,
 static NTSTATUS pipe_auth_verify_final(struct pipes_struct *p)
 {
        struct gensec_security *gensec_security;
+       bool ok;
 
-       switch (p->auth.auth_type) {
-       case DCERPC_AUTH_TYPE_NTLMSSP:
-       case DCERPC_AUTH_TYPE_KRB5:
-       case DCERPC_AUTH_TYPE_SPNEGO:
-               gensec_security = talloc_get_type_abort(p->auth.auth_ctx,
-                                                       struct gensec_security);
-               if (!pipe_auth_generic_verify_final(p, gensec_security,
-                                               p->auth.auth_level,
-                                               &p->session_info)) {
-                       return NT_STATUS_ACCESS_DENIED;
-               }
-               break;
-       default:
-               DEBUG(0, (__location__ ": incorrect auth type (%u).\n",
-                         (unsigned int)p->auth.auth_type));
+       if (p->auth.auth_type == DCERPC_AUTH_TYPE_NONE) {
+               p->pipe_bound = true;
+               return NT_STATUS_OK;
+       }
+
+       gensec_security = talloc_get_type(p->auth.auth_ctx,
+                                         struct gensec_security);
+       if (gensec_security == NULL) {
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       ok = pipe_auth_generic_verify_final(p, gensec_security,
+                                           p->auth.auth_level,
+                                           &p->session_info);
+       if (!ok) {
                return NT_STATUS_ACCESS_DENIED;
        }
 
@@ -674,10 +563,12 @@ static bool api_pipe_bind_req(struct pipes_struct *p,
        unsigned int auth_type = DCERPC_AUTH_TYPE_NONE;
        NTSTATUS status;
        struct ndr_syntax_id id;
+       uint8_t pfc_flags = 0;
        union dcerpc_payload u;
        struct dcerpc_ack_ctx bind_ack_ctx;
        DATA_BLOB auth_resp = data_blob_null;
        DATA_BLOB auth_blob = data_blob_null;
+       const struct ndr_interface_table *table;
 
        /* No rebinds on a bound pipe - use alter context. */
        if (p->pipe_bound) {
@@ -695,27 +586,34 @@ static bool api_pipe_bind_req(struct pipes_struct *p,
         * that this is a pipe name we support.
         */
        id = pkt->u.bind.ctx_list[0].abstract_syntax;
+
+       table = ndr_table_by_uuid(&id.uuid);
+       if (table == NULL) {
+               DEBUG(0,("unknown interface\n"));
+               return false;
+       }
+
        if (rpc_srv_pipe_exists_by_id(&id)) {
                DEBUG(3, ("api_pipe_bind_req: %s -> %s rpc service\n",
                          rpc_srv_get_pipe_cli_name(&id),
                          rpc_srv_get_pipe_srv_name(&id)));
        } else {
                status = smb_probe_module(
-                       "rpc", get_pipe_name_from_syntax(
-                               talloc_tos(),
-                               &id));
+                       "rpc", dcerpc_default_transport_endpoint(pkt,
+                               NCACN_NP, table));
 
                if (NT_STATUS_IS_ERR(status)) {
                        DEBUG(3,("api_pipe_bind_req: Unknown rpc service name "
                                  "%s in bind request.\n",
-                                get_pipe_name_from_syntax(talloc_tos(), &id)));
+                                ndr_interface_name(&id.uuid,
+                                                   id.if_version)));
 
                        return setup_bind_nak(p, pkt);
                }
 
                if (rpc_srv_get_pipe_interface_by_cli_name(
-                               get_pipe_name_from_syntax(talloc_tos(),
-                                                         &id),
+                               dcerpc_default_transport_endpoint(pkt,
+                                       NCACN_NP, table),
                                &id)) {
                        DEBUG(3, ("api_pipe_bind_req: %s -> %s rpc service\n",
                                  rpc_srv_get_pipe_cli_name(&id),
@@ -723,8 +621,10 @@ static bool api_pipe_bind_req(struct pipes_struct *p,
                } else {
                        DEBUG(0, ("module %s doesn't provide functions for "
                                  "pipe %s!\n",
-                                 get_pipe_name_from_syntax(talloc_tos(), &id),
-                                 get_pipe_name_from_syntax(talloc_tos(), &id)));
+                                 ndr_interface_name(&id.uuid,
+                                                    id.if_version),
+                                 ndr_interface_name(&id.uuid,
+                                                    id.if_version)));
                        return setup_bind_nak(p, pkt);
                }
        }
@@ -753,14 +653,14 @@ static bool api_pipe_bind_req(struct pipes_struct *p,
                        pkt->u.bind.ctx_list[0].context_id)) {
 
                bind_ack_ctx.result = 0;
-               bind_ack_ctx.reason = 0;
+               bind_ack_ctx.reason.value = 0;
                bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
        } else {
                p->pipe_bound = False;
                /* Rejection reason: abstract syntax not supported */
                bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
-               bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
-               bind_ack_ctx.syntax = null_ndr_syntax_id;
+               bind_ack_ctx.reason.value = DCERPC_BIND_REASON_ASYNTAX;
+               bind_ack_ctx.syntax = ndr_syntax_id_null;
        }
 
        /*
@@ -820,10 +720,15 @@ static bool api_pipe_bind_req(struct pipes_struct *p,
                        break;
 
                case DCERPC_AUTH_TYPE_SCHANNEL:
-                       if (!pipe_schannel_auth_bind(p, pkt,
-                                               &auth_info, &auth_resp)) {
+                       if (!pipe_auth_generic_bind(p, pkt,
+                                                   &auth_info, &auth_resp)) {
+                               goto err_exit;
+                       }
+                       if (!session_info_set_session_key(p->session_info, generic_session_key())) {
+                               DEBUG(0, ("session_info_set_session_key failed\n"));
                                goto err_exit;
                        }
+                       p->pipe_bound = true;
                        break;
 
                case DCERPC_AUTH_TYPE_SPNEGO:
@@ -903,10 +808,15 @@ static bool api_pipe_bind_req(struct pipes_struct *p,
         * header and are never sending more than one PDU here.
         */
 
+       pfc_flags = DCERPC_PFC_FLAG_FIRST | DCERPC_PFC_FLAG_LAST;
+
+       if (p->auth.hdr_signing) {
+               pfc_flags |= DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN;
+       }
+
        status = dcerpc_push_ncacn_packet(p->mem_ctx,
                                          DCERPC_PKT_BIND_ACK,
-                                         DCERPC_PFC_FLAG_FIRST |
-                                               DCERPC_PFC_FLAG_LAST,
+                                         pfc_flags,
                                          auth_resp.length,
                                          pkt->call_id,
                                          &u,
@@ -976,7 +886,7 @@ bool api_pipe_bind_auth3(struct pipes_struct *p, struct ncacn_packet *pkt)
        DEBUG(5, ("api_pipe_bind_auth3: decode request. %d\n", __LINE__));
 
        if (pkt->auth_length == 0) {
-               DEBUG(0, ("No auth field sent for bind request!\n"));
+               DEBUG(1, ("No auth field sent for bind request!\n"));
                goto err;
        }
 
@@ -984,7 +894,7 @@ bool api_pipe_bind_auth3(struct pipes_struct *p, struct ncacn_packet *pkt)
        if (pkt->frag_length < RPC_HEADER_LEN
                                + DCERPC_AUTH_TRAILER_LENGTH
                                + pkt->auth_length) {
-                       DEBUG(0,("api_pipe_ntlmssp_auth_process: auth_len "
+                       DEBUG(1,("api_pipe_ntlmssp_auth_process: auth_len "
                                "%u is too large.\n",
                         (unsigned int)pkt->auth_length));
                goto err;
@@ -998,7 +908,7 @@ bool api_pipe_bind_auth3(struct pipes_struct *p, struct ncacn_packet *pkt)
                                         &pkt->u.auth3.auth_info,
                                         &auth_info, p->endian);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("Failed to unmarshall dcerpc_auth.\n"));
+               DEBUG(1, ("Failed to unmarshall dcerpc_auth.\n"));
                goto err;
        }
 
@@ -1008,7 +918,7 @@ bool api_pipe_bind_auth3(struct pipes_struct *p, struct ncacn_packet *pkt)
         */
 
        if (auth_info.auth_type != p->auth.auth_type) {
-               DEBUG(0, ("Auth type mismatch! Client sent %d, "
+               DEBUG(1, ("Auth type mismatch! Client sent %d, "
                          "but auth was started as type %d!\n",
                          auth_info.auth_type, p->auth.auth_type));
                goto err;
@@ -1025,7 +935,7 @@ bool api_pipe_bind_auth3(struct pipes_struct *p, struct ncacn_packet *pkt)
                                             &response);
                break;
        default:
-               DEBUG(0, (__location__ ": incorrect auth type (%u).\n",
+               DEBUG(1, (__location__ ": incorrect auth type (%u).\n",
                          (unsigned int)auth_info.auth_type));
                return false;
        }
@@ -1033,21 +943,21 @@ bool api_pipe_bind_auth3(struct pipes_struct *p, struct ncacn_packet *pkt)
        if (NT_STATUS_EQUAL(status,
                            NT_STATUS_MORE_PROCESSING_REQUIRED) ||
            response.length) {
-               DEBUG(0, (__location__ ": This was supposed to be the final "
+               DEBUG(1, (__location__ ": This was supposed to be the final "
                          "leg, but crypto machinery claims a response is "
                          "needed, aborting auth!\n"));
                data_blob_free(&response);
                goto err;
        }
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("Auth failed (%s)\n", nt_errstr(status)));
+               DEBUG(2, ("Auth failed (%s)\n", nt_errstr(status)));
                goto err;
        }
 
        /* Now verify auth was indeed successful and extract server info */
        status = pipe_auth_verify_final(p);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("Auth Verify failed (%s)\n", nt_errstr(status)));
+               DEBUG(2, ("Auth Verify failed (%s)\n", nt_errstr(status)));
                goto err;
        }
 
@@ -1101,14 +1011,14 @@ static bool api_pipe_alter_context(struct pipes_struct *p,
                        pkt->u.bind.ctx_list[0].context_id)) {
 
                bind_ack_ctx.result = 0;
-               bind_ack_ctx.reason = 0;
+               bind_ack_ctx.reason.value = 0;
                bind_ack_ctx.syntax = pkt->u.bind.ctx_list[0].transfer_syntaxes[0];
        } else {
                p->pipe_bound = False;
                /* Rejection reason: abstract syntax not supported */
                bind_ack_ctx.result = DCERPC_BIND_PROVIDER_REJECT;
-               bind_ack_ctx.reason = DCERPC_BIND_REASON_ASYNTAX;
-               bind_ack_ctx.syntax = null_ndr_syntax_id;
+               bind_ack_ctx.reason.value = DCERPC_BIND_REASON_ASYNTAX;
+               bind_ack_ctx.syntax = ndr_syntax_id_null;
        }
 
        /*
@@ -1296,6 +1206,41 @@ static bool api_rpcTNP(struct pipes_struct *p, struct ncacn_packet *pkt,
                       const struct api_struct *api_rpc_cmds, int n_cmds,
                       const struct ndr_syntax_id *syntax);
 
+static bool srv_pipe_check_verification_trailer(struct pipes_struct *p,
+                                               struct ncacn_packet *pkt,
+                                               struct pipe_rpc_fns *pipe_fns)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct dcerpc_sec_verification_trailer *vt = NULL;
+       const uint32_t bitmask1 =
+               p->auth.client_hdr_signing ? DCERPC_SEC_VT_CLIENT_SUPPORTS_HEADER_SIGNING : 0;
+       const struct dcerpc_sec_vt_pcontext pcontext = {
+               .abstract_syntax = pipe_fns->syntax,
+               .transfer_syntax = ndr_transfer_syntax_ndr,
+       };
+       const struct dcerpc_sec_vt_header2 header2 =
+              dcerpc_sec_vt_header2_from_ncacn_packet(pkt);
+       struct ndr_pull *ndr;
+       enum ndr_err_code ndr_err;
+       bool ret = false;
+
+       ndr = ndr_pull_init_blob(&p->in_data.data, frame);
+       if (ndr == NULL) {
+               goto done;
+       }
+
+       ndr_err = ndr_pop_dcerpc_sec_verification_trailer(ndr, frame, &vt);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               goto done;
+       }
+
+       ret = dcerpc_sec_verification_trailer_check(vt, &bitmask1,
+                                                   &pcontext, &header2);
+done:
+       TALLOC_FREE(frame);
+       return ret;
+}
+
 /****************************************************************************
  Find the correct RPC function to call for this request.
  If the pipe is authenticated then become the correct UNIX user
@@ -1305,45 +1250,53 @@ static bool api_rpcTNP(struct pipes_struct *p, struct ncacn_packet *pkt,
 static bool api_pipe_request(struct pipes_struct *p,
                                struct ncacn_packet *pkt)
 {
+       TALLOC_CTX *frame = talloc_stackframe();
        bool ret = False;
        struct pipe_rpc_fns *pipe_fns;
 
        if (!p->pipe_bound) {
                DEBUG(1, ("Pipe not bound!\n"));
                data_blob_free(&p->out_data.rdata);
-               return false;
-       }
-
-       if (!become_authenticated_pipe_user(p->session_info)) {
-               DEBUG(1, ("Failed to become pipe user!\n"));
-               data_blob_free(&p->out_data.rdata);
+               TALLOC_FREE(frame);
                return false;
        }
 
        /* get the set of RPC functions for this context */
-
        pipe_fns = find_pipe_fns_by_context(p->contexts,
                                            pkt->u.request.context_id);
-
-       if ( pipe_fns ) {
-               TALLOC_CTX *frame = talloc_stackframe();
-
-               DEBUG(5, ("Requested %s rpc service\n",
-                         get_pipe_name_from_syntax(talloc_tos(), &pipe_fns->syntax)));
-
-               ret = api_rpcTNP(p, pkt, pipe_fns->cmds, pipe_fns->n_cmds,
-                                &pipe_fns->syntax);
-
-               TALLOC_FREE(frame);
-       }
-       else {
+       if (pipe_fns == NULL) {
                DEBUG(0, ("No rpc function table associated with context "
                          "[%d]\n",
                          pkt->u.request.context_id));
+               data_blob_free(&p->out_data.rdata);
+               TALLOC_FREE(frame);
+               return false;
        }
 
+       if (!srv_pipe_check_verification_trailer(p, pkt, pipe_fns)) {
+               DEBUG(1, ("srv_pipe_check_verification_trailer: failed\n"));
+               setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_ACCESS_DENIED));
+               data_blob_free(&p->out_data.rdata);
+               TALLOC_FREE(frame);
+               return true;
+       }
+
+       if (!become_authenticated_pipe_user(p->session_info)) {
+               DEBUG(1, ("Failed to become pipe user!\n"));
+               data_blob_free(&p->out_data.rdata);
+               TALLOC_FREE(frame);
+               return false;
+       }
+
+       DEBUG(5, ("Requested %s rpc service\n",
+                 ndr_interface_name(&pipe_fns->syntax.uuid,
+                                    pipe_fns->syntax.if_version)));
+
+       ret = api_rpcTNP(p, pkt, pipe_fns->cmds, pipe_fns->n_cmds,
+                        &pipe_fns->syntax);
        unbecome_authenticated_pipe_user();
 
+       TALLOC_FREE(frame);
        return ret;
 }
 
@@ -1357,16 +1310,23 @@ static bool api_rpcTNP(struct pipes_struct *p, struct ncacn_packet *pkt,
 {
        int fn_num;
        uint32_t offset1;
+       const struct ndr_interface_table *table;
 
        /* interpret the command */
        DEBUG(4,("api_rpcTNP: %s op 0x%x - ",
-                get_pipe_name_from_syntax(talloc_tos(), syntax),
+                ndr_interface_name(&syntax->uuid, syntax->if_version),
                 pkt->u.request.opnum));
 
+       table = ndr_table_by_uuid(&syntax->uuid);
+       if (table == NULL) {
+               DEBUG(0,("unknown interface\n"));
+               return false;
+       }
+
        if (DEBUGLEVEL >= 50) {
                fstring name;
                slprintf(name, sizeof(name)-1, "in_%s",
-                        get_pipe_name_from_syntax(talloc_tos(), syntax));
+                        dcerpc_default_transport_endpoint(pkt, NCACN_NP, table));
                dump_pdu_region(name, pkt->u.request.opnum,
                                &p->in_data.data, 0,
                                p->in_data.data.length);
@@ -1399,37 +1359,30 @@ static bool api_rpcTNP(struct pipes_struct *p, struct ncacn_packet *pkt,
        /* do the actual command */
        if(!api_rpc_cmds[fn_num].fn(p)) {
                DEBUG(0,("api_rpcTNP: %s: %s failed.\n",
-                        get_pipe_name_from_syntax(talloc_tos(), syntax),
+                        ndr_interface_name(&syntax->uuid, syntax->if_version),
                         api_rpc_cmds[fn_num].name));
                data_blob_free(&p->out_data.rdata);
                return False;
        }
 
-       if (p->bad_handle_fault_state) {
-               DEBUG(4,("api_rpcTNP: bad handle fault return.\n"));
-               p->bad_handle_fault_state = False;
-               setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_CONTEXT_MISMATCH));
-               return True;
-       }
-
-       if (p->rng_fault_state) {
-               DEBUG(4, ("api_rpcTNP: rng fault return\n"));
-               p->rng_fault_state = False;
-               setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
-               return True;
+       if (p->fault_state) {
+               DEBUG(4,("api_rpcTNP: fault(%d) return.\n", p->fault_state));
+               setup_fault_pdu(p, NT_STATUS(p->fault_state));
+               p->fault_state = 0;
+               return true;
        }
 
        if (DEBUGLEVEL >= 50) {
                fstring name;
                slprintf(name, sizeof(name)-1, "out_%s",
-                        get_pipe_name_from_syntax(talloc_tos(), syntax));
+                        dcerpc_default_transport_endpoint(pkt, NCACN_NP, table));
                dump_pdu_region(name, pkt->u.request.opnum,
                                &p->out_data.rdata, offset1,
                                p->out_data.rdata.length);
        }
 
        DEBUG(5,("api_rpcTNP: called %s successfully\n",
-                get_pipe_name_from_syntax(talloc_tos(), syntax)));
+                ndr_interface_name(&syntax->uuid, syntax->if_version)));
 
        /* Check for buffer underflow in rpc parsing */
        if ((DEBUGLEVEL >= 10) &&
@@ -1471,7 +1424,7 @@ void set_incoming_fault(struct pipes_struct *p)
        data_blob_free(&p->in_data.data);
        p->in_data.pdu_needed_len = 0;
        p->in_data.pdu.length = 0;
-       p->fault_state = True;
+       p->fault_state = DCERPC_FAULT_CANT_PERFORM;
 
        DEBUG(10, ("Setting fault state\n"));
 }
@@ -1524,6 +1477,7 @@ static bool process_request_pdu(struct pipes_struct *p, struct ncacn_packet *pkt
 {
        NTSTATUS status;
        DATA_BLOB data;
+       struct dcerpc_sec_vt_header2 hdr2;
 
        if (!p->pipe_bound) {
                DEBUG(0,("process_request_pdu: rpc request with no bind.\n"));
@@ -1531,6 +1485,16 @@ static bool process_request_pdu(struct pipes_struct *p, struct ncacn_packet *pkt
                return False;
        }
 
+       hdr2 = dcerpc_sec_vt_header2_from_ncacn_packet(pkt);
+       if (pkt->pfc_flags & DCERPC_PFC_FLAG_FIRST) {
+               p->header2 = hdr2;
+       } else {
+               if (!dcerpc_sec_vt_header2_equal(&hdr2, &p->header2)) {
+                       set_incoming_fault(p);
+                       return false;
+               }
+       }
+
        /* Store the opnum */
        p->opnum = pkt->u.request.opnum;
 
@@ -1576,71 +1540,31 @@ static bool process_request_pdu(struct pipes_struct *p, struct ncacn_packet *pkt
                }
        }
 
-       if (pkt->pfc_flags & DCERPC_PFC_FLAG_LAST) {
-               bool ret = False;
-               /*
-                * Ok - we finally have a complete RPC stream.
-                * Call the rpc command to process it.
-                */
-
-               /*
-                * Process the complete data stream here.
-                */
-               if (pipe_init_outgoing_data(p)) {
-                       ret = api_pipe_request(p, pkt);
-               }
-
-               return ret;
-       }
-
-       return True;
-}
-
-/****************************************************************************
- Processes a finished PDU stored in p->in_data.pdu.
-****************************************************************************/
-
-void process_complete_pdu(struct pipes_struct *p)
-{
-       struct ncacn_packet *pkt = NULL;
-       NTSTATUS status;
-       bool reply = False;
-
-       if(p->fault_state) {
-               DEBUG(10,("RPC connection in fault state.\n"));
-               goto done;
-       }
-
-       pkt = talloc(p->mem_ctx, struct ncacn_packet);
-       if (!pkt) {
-               DEBUG(0, ("Out of memory!\n"));
-               goto done;
+       if (!(pkt->pfc_flags & DCERPC_PFC_FLAG_LAST)) {
+               return true;
        }
 
        /*
-        * Ensure we're using the corrent endianness for both the
-        * RPC header flags and the raw data we will be reading from.
+        * Ok - we finally have a complete RPC stream.
+        * Call the rpc command to process it.
         */
-       if (dcerpc_get_endian_flag(&p->in_data.pdu) & DCERPC_DREP_LE) {
-               p->endian = RPC_LITTLE_ENDIAN;
-       } else {
-               p->endian = RPC_BIG_ENDIAN;
-       }
-       DEBUG(10, ("PDU is in %s Endian format!\n", p->endian?"Big":"Little"));
 
-       status = dcerpc_pull_ncacn_packet(pkt, &p->in_data.pdu,
-                                         pkt, p->endian);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("Failed to unmarshal rpc packet: %s!\n",
-                         nt_errstr(status)));
-               goto done;
-       }
+       return api_pipe_request(p, pkt);
+}
+
+void process_complete_pdu(struct pipes_struct *p, struct ncacn_packet *pkt)
+{
+       bool reply = false;
 
        /* Store the call_id */
        p->call_id = pkt->call_id;
 
        DEBUG(10, ("Processing packet type %u\n", (unsigned int)pkt->ptype));
 
+       if (!pipe_init_outgoing_data(p)) {
+               goto done;
+       }
+
        switch (pkt->ptype) {
        case DCERPC_PKT_REQUEST:
                reply = process_request_pdu(p, pkt);
@@ -1673,9 +1597,7 @@ void process_complete_pdu(struct pipes_struct *p)
                /*
                 * We assume that a pipe bind is only in one pdu.
                 */
-               if (pipe_init_outgoing_data(p)) {
-                       reply = api_pipe_bind_req(p, pkt);
-               }
+               reply = api_pipe_bind_req(p, pkt);
                break;
 
        case DCERPC_PKT_BIND_ACK:
@@ -1690,9 +1612,7 @@ void process_complete_pdu(struct pipes_struct *p)
                /*
                 * We assume that a pipe bind is only in one pdu.
                 */
-               if (pipe_init_outgoing_data(p)) {
-                       reply = api_pipe_alter_context(p, pkt);
-               }
+               reply = api_pipe_alter_context(p, pkt);
                break;
 
        case DCERPC_PKT_ALTER_RESP:
@@ -1704,9 +1624,7 @@ void process_complete_pdu(struct pipes_struct *p)
                /*
                 * The third packet in an auth exchange.
                 */
-               if (pipe_init_outgoing_data(p)) {
-                       reply = api_pipe_bind_auth3(p, pkt);
-               }
+               reply = api_pipe_bind_auth3(p, pkt);
                break;
 
        case DCERPC_PKT_SHUTDOWN:
@@ -1759,16 +1677,7 @@ done:
                DEBUG(3,("DCE/RPC fault sent!"));
                set_incoming_fault(p);
                setup_fault_pdu(p, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR));
-               TALLOC_FREE(pkt);
-       } else {
-               /*
-                * Reset the lengths. We're ready for a new pdu.
-                */
-               TALLOC_FREE(p->in_data.pdu.data);
-               p->in_data.pdu_needed_len = 0;
-               p->in_data.pdu.length = 0;
        }
-
-       TALLOC_FREE(pkt);
+       /* pkt and p->in_data.pdu.data freed by caller */
 }