s3-rpc_client: make sure cli_rpc_pipe_open_schannel() does not always return NT_STATU...
[ira/wip.git] / source3 / rpc_client / cli_pipe.c
index 63d3689620f95a70e83b202e3c8502f51f581ce1..28d9d99d05376420a4433ec8403474a1002b514e 100644 (file)
  */
 
 #include "includes.h"
+#include "../libcli/auth/libcli_auth.h"
 #include "librpc/gen_ndr/cli_epmapper.h"
+#include "../librpc/gen_ndr/ndr_schannel.h"
+#include "../libcli/auth/schannel.h"
+#include "../libcli/auth/schannel_proto.h"
+#include "../libcli/auth/spnego.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_CLI
 
-/*******************************************************************
-interface/version dce/rpc pipe identification
-********************************************************************/
-
-#define PIPE_SRVSVC   "\\PIPE\\srvsvc"
-#define PIPE_SAMR     "\\PIPE\\samr"
-#define PIPE_WINREG   "\\PIPE\\winreg"
-#define PIPE_WKSSVC   "\\PIPE\\wkssvc"
-#define PIPE_NETLOGON "\\PIPE\\NETLOGON"
-#define PIPE_NTLSA    "\\PIPE\\ntlsa"
-#define PIPE_NTSVCS   "\\PIPE\\ntsvcs"
-#define PIPE_LSASS    "\\PIPE\\lsass"
-#define PIPE_LSARPC   "\\PIPE\\lsarpc"
-#define PIPE_SPOOLSS  "\\PIPE\\spoolss"
-#define PIPE_NETDFS   "\\PIPE\\netdfs"
-#define PIPE_ECHO     "\\PIPE\\rpcecho"
-#define PIPE_SHUTDOWN "\\PIPE\\initshutdown"
-#define PIPE_EPM      "\\PIPE\\epmapper"
-#define PIPE_SVCCTL   "\\PIPE\\svcctl"
-#define PIPE_EVENTLOG "\\PIPE\\eventlog"
-#define PIPE_EPMAPPER "\\PIPE\\epmapper"
-#define PIPE_DRSUAPI  "\\PIPE\\drsuapi"
+static const char *get_pipe_name_from_iface(
+       TALLOC_CTX *mem_ctx, const struct ndr_interface_table *interface)
+{
+       int i;
+       const struct ndr_interface_string_array *ep = interface->endpoints;
+       char *p;
 
-/*
- * IMPORTANT!!  If you update this structure, make sure to
- * update the index #defines in smb.h.
- */
+       for (i=0; i<ep->count; i++) {
+               if (strncmp(ep->names[i], "ncacn_np:[\\pipe\\", 16) == 0) {
+                       break;
+               }
+       }
+       if (i == ep->count) {
+               return NULL;
+       }
+
+       /*
+        * extract the pipe name without \\pipe from for example
+        * ncacn_np:[\\pipe\\epmapper]
+        */
+       p = strchr(ep->names[i]+15, ']');
+       if (p == NULL) {
+               return "PIPE";
+       }
+       return talloc_strndup(mem_ctx, ep->names[i]+15, p - ep->names[i] - 15);
+}
 
-static const struct pipe_id_info {
-       /* the names appear not to matter: the syntaxes _do_ matter */
+static const struct ndr_interface_table **interfaces;
 
-       const char *client_pipe;
-       const RPC_IFACE *abstr_syntax; /* this one is the abstract syntax id */
-} pipe_names [] =
+bool smb_register_ndr_interface(const struct ndr_interface_table *interface)
 {
-       { PIPE_LSARPC,          &ndr_table_lsarpc.syntax_id },
-       { PIPE_LSARPC,          &ndr_table_dssetup.syntax_id },
-       { PIPE_SAMR,            &ndr_table_samr.syntax_id },
-       { PIPE_NETLOGON,        &ndr_table_netlogon.syntax_id },
-       { PIPE_SRVSVC,          &ndr_table_srvsvc.syntax_id },
-       { PIPE_WKSSVC,          &ndr_table_wkssvc.syntax_id },
-       { PIPE_WINREG,          &ndr_table_winreg.syntax_id },
-       { PIPE_SPOOLSS,         &syntax_spoolss },
-       { PIPE_NETDFS,          &ndr_table_netdfs.syntax_id },
-       { PIPE_ECHO,            &ndr_table_rpcecho.syntax_id },
-       { PIPE_SHUTDOWN,        &ndr_table_initshutdown.syntax_id },
-       { PIPE_SVCCTL,          &ndr_table_svcctl.syntax_id },
-       { PIPE_EVENTLOG,        &ndr_table_eventlog.syntax_id },
-       { PIPE_NTSVCS,          &ndr_table_ntsvcs.syntax_id },
-       { PIPE_EPMAPPER,        &ndr_table_epmapper.syntax_id },
-       { PIPE_DRSUAPI,         &ndr_table_drsuapi.syntax_id },
-       { NULL, NULL }
-};
+       int num_interfaces = talloc_array_length(interfaces);
+       const struct ndr_interface_table **tmp;
+       int i;
+
+       for (i=0; i<num_interfaces; i++) {
+               if (ndr_syntax_id_equal(&interfaces[i]->syntax_id,
+                                       &interface->syntax_id)) {
+                       return true;
+               }
+       }
+
+       tmp = talloc_realloc(NULL, interfaces,
+                            const struct ndr_interface_table *,
+                            num_interfaces + 1);
+       if (tmp == NULL) {
+               DEBUG(1, ("smb_register_ndr_interface: talloc failed\n"));
+               return false;
+       }
+       interfaces = tmp;
+       interfaces[num_interfaces] = interface;
+       return true;
+}
+
+static bool initialize_interfaces(void)
+{
+       if (!smb_register_ndr_interface(&ndr_table_lsarpc)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_dssetup)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_samr)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_netlogon)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_srvsvc)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_wkssvc)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_winreg)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_spoolss)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_netdfs)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_rpcecho)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_initshutdown)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_svcctl)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_eventlog)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_ntsvcs)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_epmapper)) {
+               return false;
+       }
+       if (!smb_register_ndr_interface(&ndr_table_drsuapi)) {
+               return false;
+       }
+       return true;
+}
+
+const struct ndr_interface_table *get_iface_from_syntax(
+       const struct ndr_syntax_id *syntax)
+{
+       int num_interfaces;
+       int i;
+
+       if (interfaces == NULL) {
+               if (!initialize_interfaces()) {
+                       return NULL;
+               }
+       }
+       num_interfaces = talloc_array_length(interfaces);
+
+       for (i=0; i<num_interfaces; i++) {
+               if (ndr_syntax_id_equal(&interfaces[i]->syntax_id, syntax)) {
+                       return interfaces[i];
+               }
+       }
+
+       return NULL;
+}
 
 /****************************************************************************
  Return the pipe name from the interface.
  ****************************************************************************/
 
-const char *cli_get_pipe_name_from_iface(TALLOC_CTX *mem_ctx,
-                                        struct cli_state *cli,
-                                        const struct ndr_syntax_id *interface)
+const char *get_pipe_name_from_syntax(TALLOC_CTX *mem_ctx,
+                                     const struct ndr_syntax_id *syntax)
 {
-       int i;
-       for (i = 0; pipe_names[i].client_pipe; i++) {
-               if (ndr_syntax_id_equal(pipe_names[i].abstr_syntax,
-                                       interface)) {
-                       return &pipe_names[i].client_pipe[5];
+       const struct ndr_interface_table *interface;
+       char *guid_str;
+       const char *result;
+
+       interface = get_iface_from_syntax(syntax);
+       if (interface != NULL) {
+               result = get_pipe_name_from_iface(mem_ctx, interface);
+               if (result != NULL) {
+                       return result;
                }
        }
 
@@ -98,7 +181,18 @@ const char *cli_get_pipe_name_from_iface(TALLOC_CTX *mem_ctx,
         * interested in the known pipes mentioned in pipe_names[]
         */
 
-       return NULL;
+       guid_str = GUID_string(talloc_tos(), &syntax->uuid);
+       if (guid_str == NULL) {
+               return NULL;
+       }
+       result = talloc_asprintf(mem_ctx, "Interface %s.%d", guid_str,
+                                (int)syntax->if_version);
+       TALLOC_FREE(guid_str);
+
+       if (result == NULL) {
+               return "PIPE";
+       }
+       return result;
 }
 
 /********************************************************************
@@ -110,20 +204,20 @@ static int map_pipe_auth_type_to_rpc_auth_type(enum pipe_auth_type auth_type)
        switch (auth_type) {
 
        case PIPE_AUTH_TYPE_NONE:
-               return RPC_ANONYMOUS_AUTH_TYPE;
+               return DCERPC_AUTH_TYPE_NONE;
 
        case PIPE_AUTH_TYPE_NTLMSSP:
-               return RPC_NTLMSSP_AUTH_TYPE;
+               return DCERPC_AUTH_TYPE_NTLMSSP;
 
        case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
        case PIPE_AUTH_TYPE_SPNEGO_KRB5:
-               return RPC_SPNEGO_AUTH_TYPE;
+               return DCERPC_AUTH_TYPE_SPNEGO;
 
        case PIPE_AUTH_TYPE_SCHANNEL:
-               return RPC_SCHANNEL_AUTH_TYPE;
+               return DCERPC_AUTH_TYPE_SCHANNEL;
 
        case PIPE_AUTH_TYPE_KRB5:
-               return RPC_KRB5_AUTH_TYPE;
+               return DCERPC_AUTH_TYPE_KRB5;
 
        default:
                DEBUG(0,("map_pipe_auth_type_to_rpc_type: unknown pipe "
@@ -137,28 +231,13 @@ static int map_pipe_auth_type_to_rpc_auth_type(enum pipe_auth_type auth_type)
 /********************************************************************
  Pipe description for a DEBUG
  ********************************************************************/
-static char *rpccli_pipe_txt(TALLOC_CTX *mem_ctx, struct rpc_pipe_client *cli)
+static const char *rpccli_pipe_txt(TALLOC_CTX *mem_ctx,
+                                  struct rpc_pipe_client *cli)
 {
-       char *result;
-
-       switch (cli->transport_type) {
-       case NCACN_NP:
-               result = talloc_asprintf(mem_ctx, "host %s, pipe %s, "
-                                        "fnum 0x%x",
-                                        cli->desthost,
-                                        cli->trans.np.pipe_name,
-                                        (unsigned int)(cli->trans.np.fnum));
-               break;
-       case NCACN_IP_TCP:
-       case NCACN_UNIX_STREAM:
-               result = talloc_asprintf(mem_ctx, "host %s, fd %d",
-                                        cli->desthost, cli->trans.sock.fd);
-               break;
-       default:
-               result = talloc_asprintf(mem_ctx, "host %s", cli->desthost);
-               break;
+       char *result = talloc_asprintf(mem_ctx, "host %s", cli->desthost);
+       if (result == NULL) {
+               return "pipe";
        }
-       SMB_ASSERT(result != NULL);
        return result;
 }
 
@@ -205,271 +284,161 @@ static bool rpc_grow_buffer(prs_struct *pdu, size_t size)
 
 struct rpc_read_state {
        struct event_context *ev;
-       struct rpc_pipe_client *cli;
-       char *data;
+       struct rpc_cli_transport *transport;
+       uint8_t *data;
        size_t size;
        size_t num_read;
 };
 
-static void rpc_read_np_done(struct async_req *subreq);
-static void rpc_read_sock_done(struct async_req *subreq);
+static void rpc_read_done(struct tevent_req *subreq);
 
-static struct async_req *rpc_read_send(TALLOC_CTX *mem_ctx,
-                                      struct event_context *ev,
-                                      struct rpc_pipe_client *cli,
-                                      char *data, size_t size)
+static struct tevent_req *rpc_read_send(TALLOC_CTX *mem_ctx,
+                                       struct event_context *ev,
+                                       struct rpc_cli_transport *transport,
+                                       uint8_t *data, size_t size)
 {
-       struct async_req *result, *subreq;
+       struct tevent_req *req, *subreq;
        struct rpc_read_state *state;
 
-       result = async_req_new(mem_ctx);
-       if (result == NULL) {
+       req = tevent_req_create(mem_ctx, &state, struct rpc_read_state);
+       if (req == NULL) {
                return NULL;
        }
-       state = talloc(result, struct rpc_read_state);
-       if (state == NULL) {
-               goto fail;
-       }
-       result->private_data = state;
-
        state->ev = ev;
-       state->cli = cli;
+       state->transport = transport;
        state->data = data;
        state->size = size;
        state->num_read = 0;
 
        DEBUG(5, ("rpc_read_send: data_to_read: %u\n", (unsigned int)size));
 
-       if (cli->transport_type == NCACN_NP) {
-               subreq = cli_read_andx_send(
-                       state, ev, cli->trans.np.cli,
-                       cli->trans.np.fnum, 0, size);
-               if (subreq == NULL) {
-                       DEBUG(10, ("cli_read_andx_send failed\n"));
-                       goto fail;
-               }
-               subreq->async.fn = rpc_read_np_done;
-               subreq->async.priv = result;
-               return result;
-       }
-
-       if ((cli->transport_type == NCACN_IP_TCP)
-           || (cli->transport_type == NCACN_UNIX_STREAM)) {
-               subreq = recvall_send(state, ev, cli->trans.sock.fd,
-                                     data, size, 0);
-               if (subreq == NULL) {
-                       DEBUG(10, ("recvall_send failed\n"));
-                       goto fail;
-               }
-               subreq->async.fn = rpc_read_sock_done;
-               subreq->async.priv = result;
-               return result;
+       subreq = transport->read_send(state, ev, (uint8_t *)data, size,
+                                     transport->priv);
+       if (subreq == NULL) {
+               goto fail;
        }
+       tevent_req_set_callback(subreq, rpc_read_done, req);
+       return req;
 
-       if (async_post_status(result, ev, NT_STATUS_INVALID_PARAMETER)) {
-               return result;
-       }
  fail:
-       TALLOC_FREE(result);
+       TALLOC_FREE(req);
        return NULL;
 }
 
-static void rpc_read_np_done(struct async_req *subreq)
+static void rpc_read_done(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       struct rpc_read_state *state = talloc_get_type_abort(
-               req->private_data, struct rpc_read_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct rpc_read_state *state = tevent_req_data(
+               req, struct rpc_read_state);
        NTSTATUS status;
        ssize_t received;
-       uint8_t *rcvbuf;
 
-       status = cli_read_andx_recv(subreq, &received, &rcvbuf);
-       /*
-        * We can't TALLOC_FREE(subreq) as usual here, as rcvbuf still is a
-        * child of that.
-        */
-       if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
-               status = NT_STATUS_OK;
-       }
+       status = state->transport->read_recv(subreq, &received);
+       TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               TALLOC_FREE(subreq);
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
-       memcpy(state->data + state->num_read, rcvbuf, received);
-       TALLOC_FREE(subreq);
-
        state->num_read += received;
-
        if (state->num_read == state->size) {
-               async_req_done(req);
-               return;
-       }
-
-       subreq = cli_read_andx_send(
-               state, state->ev, state->cli->trans.np.cli,
-               state->cli->trans.np.fnum, 0,
-               state->size - state->num_read);
-
-       if (async_req_nomem(subreq, req)) {
+               tevent_req_done(req);
                return;
        }
 
-       subreq->async.fn = rpc_read_np_done;
-       subreq->async.priv = req;
-}
-
-static void rpc_read_sock_done(struct async_req *subreq)
-{
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       NTSTATUS status;
-
-       status = recvall_recv(subreq);
-       TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+       subreq = state->transport->read_send(state, state->ev,
+                                            state->data + state->num_read,
+                                            state->size - state->num_read,
+                                            state->transport->priv);
+       if (tevent_req_nomem(subreq, req)) {
                return;
        }
-
-       async_req_done(req);
+       tevent_req_set_callback(subreq, rpc_read_done, req);
 }
 
-static NTSTATUS rpc_read_recv(struct async_req *req)
+static NTSTATUS rpc_read_recv(struct tevent_req *req)
 {
-       return async_req_simple_recv(req);
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 struct rpc_write_state {
        struct event_context *ev;
-       struct rpc_pipe_client *cli;
-       const char *data;
+       struct rpc_cli_transport *transport;
+       const uint8_t *data;
        size_t size;
        size_t num_written;
 };
 
-static void rpc_write_np_done(struct async_req *subreq);
-static void rpc_write_sock_done(struct async_req *subreq);
+static void rpc_write_done(struct tevent_req *subreq);
 
-static struct async_req *rpc_write_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
-                                       struct rpc_pipe_client *cli,
-                                       const char *data, size_t size)
+static struct tevent_req *rpc_write_send(TALLOC_CTX *mem_ctx,
+                                        struct event_context *ev,
+                                        struct rpc_cli_transport *transport,
+                                        const uint8_t *data, size_t size)
 {
-       struct async_req *result, *subreq;
+       struct tevent_req *req, *subreq;
        struct rpc_write_state *state;
 
-       result = async_req_new(mem_ctx);
-       if (result == NULL) {
+       req = tevent_req_create(mem_ctx, &state, struct rpc_write_state);
+       if (req == NULL) {
                return NULL;
        }
-       state = talloc(result, struct rpc_write_state);
-       if (state == NULL) {
-               goto fail;
-       }
-       result->private_data = state;
-
        state->ev = ev;
-       state->cli = cli;
+       state->transport = transport;
        state->data = data;
        state->size = size;
        state->num_written = 0;
 
        DEBUG(5, ("rpc_write_send: data_to_write: %u\n", (unsigned int)size));
 
-       if (cli->transport_type == NCACN_NP) {
-               subreq = cli_write_andx_send(
-                       state, ev, cli->trans.np.cli,
-                       cli->trans.np.fnum, 8, /* 8 means message mode. */
-                       (uint8_t *)data, 0, size);
-               if (subreq == NULL) {
-                       DEBUG(10, ("cli_write_andx_send failed\n"));
-                       goto fail;
-               }
-               subreq->async.fn = rpc_write_np_done;
-               subreq->async.priv = result;
-               return result;
-       }
-
-       if ((cli->transport_type == NCACN_IP_TCP)
-           || (cli->transport_type == NCACN_UNIX_STREAM)) {
-               subreq = sendall_send(state, ev, cli->trans.sock.fd,
-                                     data, size, 0);
-               if (subreq == NULL) {
-                       DEBUG(10, ("sendall_send failed\n"));
-                       goto fail;
-               }
-               subreq->async.fn = rpc_write_sock_done;
-               subreq->async.priv = result;
-               return result;
-       }
-
-       if (async_post_status(result, ev, NT_STATUS_INVALID_PARAMETER)) {
-               return result;
+       subreq = transport->write_send(state, ev, data, size, transport->priv);
+       if (subreq == NULL) {
+               goto fail;
        }
+       tevent_req_set_callback(subreq, rpc_write_done, req);
+       return req;
  fail:
-       TALLOC_FREE(result);
+       TALLOC_FREE(req);
        return NULL;
 }
 
-static void rpc_write_np_done(struct async_req *subreq)
+static void rpc_write_done(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       struct rpc_write_state *state = talloc_get_type_abort(
-               req->private_data, struct rpc_write_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct rpc_write_state *state = tevent_req_data(
+               req, struct rpc_write_state);
        NTSTATUS status;
-       size_t written;
+       ssize_t written;
 
-       status = cli_write_andx_recv(subreq, &written);
+       status = state->transport->write_recv(subreq, &written);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
        state->num_written += written;
 
        if (state->num_written == state->size) {
-               async_req_done(req);
-               return;
-       }
-
-       subreq = cli_write_andx_send(
-               state, state->ev, state->cli->trans.np.cli,
-               state->cli->trans.np.fnum, 8,
-               (uint8_t *)(state->data + state->num_written),
-               0, state->size - state->num_written);
-
-       if (async_req_nomem(subreq, req)) {
+               tevent_req_done(req);
                return;
        }
 
-       subreq->async.fn = rpc_write_np_done;
-       subreq->async.priv = req;
-}
-
-static void rpc_write_sock_done(struct async_req *subreq)
-{
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       NTSTATUS status;
-
-       status = sendall_recv(subreq);
-       TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+       subreq = state->transport->write_send(state, state->ev,
+                                             state->data + state->num_written,
+                                             state->size - state->num_written,
+                                             state->transport->priv);
+       if (tevent_req_nomem(subreq, req)) {
                return;
        }
-
-       async_req_done(req);
+       tevent_req_set_callback(subreq, rpc_write_done, req);
 }
 
-static NTSTATUS rpc_write_recv(struct async_req *req)
+static NTSTATUS rpc_write_recv(struct tevent_req *req)
 {
-       return async_req_simple_recv(req);
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 
@@ -509,30 +478,25 @@ struct get_complete_frag_state {
        prs_struct *pdu;
 };
 
-static void get_complete_frag_got_header(struct async_req *subreq);
-static void get_complete_frag_got_rest(struct async_req *subreq);
+static void get_complete_frag_got_header(struct tevent_req *subreq);
+static void get_complete_frag_got_rest(struct tevent_req *subreq);
 
-static struct async_req *get_complete_frag_send(TALLOC_CTX *mem_ctx,
-                                              struct event_context *ev,
-                                              struct rpc_pipe_client *cli,
-                                              struct rpc_hdr_info *prhdr,
-                                              prs_struct *pdu)
+static struct tevent_req *get_complete_frag_send(TALLOC_CTX *mem_ctx,
+                                                struct event_context *ev,
+                                                struct rpc_pipe_client *cli,
+                                                struct rpc_hdr_info *prhdr,
+                                                prs_struct *pdu)
 {
-       struct async_req *result, *subreq;
+       struct tevent_req *req, *subreq;
        struct get_complete_frag_state *state;
        uint32_t pdu_len;
        NTSTATUS status;
 
-       result = async_req_new(mem_ctx);
-       if (result == NULL) {
+       req = tevent_req_create(mem_ctx, &state,
+                               struct get_complete_frag_state);
+       if (req == NULL) {
                return NULL;
        }
-       state = talloc(result, struct get_complete_frag_state);
-       if (state == NULL) {
-               goto fail;
-       }
-       result->private_data = state;
-
        state->ev = ev;
        state->cli = cli;
        state->prhdr = prhdr;
@@ -544,16 +508,18 @@ static struct async_req *get_complete_frag_send(TALLOC_CTX *mem_ctx,
                        status = NT_STATUS_NO_MEMORY;
                        goto post_status;
                }
-               subreq = rpc_read_send(state, state->ev, state->cli,
-                                      prs_data_p(state->pdu) + pdu_len,
-                                      RPC_HEADER_LEN - pdu_len);
+               subreq = rpc_read_send(
+                       state, state->ev,
+                       state->cli->transport,
+                       (uint8_t *)(prs_data_p(state->pdu) + pdu_len),
+                       RPC_HEADER_LEN - pdu_len);
                if (subreq == NULL) {
                        status = NT_STATUS_NO_MEMORY;
                        goto post_status;
                }
-               subreq->async.fn = get_complete_frag_got_header;
-               subreq->async.priv = result;
-               return result;
+               tevent_req_set_callback(subreq, get_complete_frag_got_header,
+                                       req);
+               return req;
        }
 
        status = parse_rpc_header(cli, prhdr, pdu);
@@ -569,51 +535,52 @@ static struct async_req *get_complete_frag_send(TALLOC_CTX *mem_ctx,
                        status = NT_STATUS_NO_MEMORY;
                        goto post_status;
                }
-               subreq = rpc_read_send(state, state->ev, state->cli,
-                                      prs_data_p(pdu) + pdu_len,
+               subreq = rpc_read_send(state, state->ev,
+                                      state->cli->transport,
+                                      (uint8_t *)(prs_data_p(pdu) + pdu_len),
                                       prhdr->frag_len - pdu_len);
                if (subreq == NULL) {
                        status = NT_STATUS_NO_MEMORY;
                        goto post_status;
                }
-               subreq->async.fn = get_complete_frag_got_rest;
-               subreq->async.priv = result;
-               return result;
+               tevent_req_set_callback(subreq, get_complete_frag_got_rest,
+                                       req);
+               return req;
        }
 
        status = NT_STATUS_OK;
  post_status:
-       if (async_post_status(result, ev, status)) {
-               return result;
+       if (NT_STATUS_IS_OK(status)) {
+               tevent_req_done(req);
+       } else {
+               tevent_req_nterror(req, status);
        }
- fail:
-       TALLOC_FREE(result);
-       return NULL;
+       return tevent_req_post(req, ev);
 }
 
-static void get_complete_frag_got_header(struct async_req *subreq)
+static void get_complete_frag_got_header(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       struct get_complete_frag_state *state = talloc_get_type_abort(
-               req->private_data, struct get_complete_frag_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct get_complete_frag_state *state = tevent_req_data(
+               req, struct get_complete_frag_state);
        NTSTATUS status;
 
        status = rpc_read_recv(subreq);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
        status = parse_rpc_header(state->cli, state->prhdr, state->pdu);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
        if (!rpc_grow_buffer(state->pdu, state->prhdr->frag_len)) {
-               async_req_error(req, NT_STATUS_NO_MEMORY);
+               tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
 
@@ -622,34 +589,34 @@ static void get_complete_frag_got_header(struct async_req *subreq)
         * RPC_HEADER_LEN bytes into state->pdu.
         */
 
-       subreq = rpc_read_send(state, state->ev, state->cli,
-                              prs_data_p(state->pdu) + RPC_HEADER_LEN,
-                              state->prhdr->frag_len - RPC_HEADER_LEN);
-       if (async_req_nomem(subreq, req)) {
+       subreq = rpc_read_send(
+               state, state->ev, state->cli->transport,
+               (uint8_t *)(prs_data_p(state->pdu) + RPC_HEADER_LEN),
+               state->prhdr->frag_len - RPC_HEADER_LEN);
+       if (tevent_req_nomem(subreq, req)) {
                return;
        }
-       subreq->async.fn = get_complete_frag_got_rest;
-       subreq->async.priv = req;
+       tevent_req_set_callback(subreq, get_complete_frag_got_rest, req);
 }
 
-static void get_complete_frag_got_rest(struct async_req *subreq)
+static void get_complete_frag_got_rest(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
        NTSTATUS status;
 
        status = rpc_read_recv(subreq);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
-       async_req_done(req);
+       tevent_req_done(req);
 }
 
-static NTSTATUS get_complete_frag_recv(struct async_req *req)
+static NTSTATUS get_complete_frag_recv(struct tevent_req *req)
 {
-       return async_req_simple_recv(req);
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 /****************************************************************************
@@ -673,8 +640,8 @@ static NTSTATUS cli_pipe_verify_ntlmssp(struct rpc_pipe_client *cli, RPC_HDR *pr
        DATA_BLOB auth_blob;
        NTSTATUS status;
 
-       if (cli->auth->auth_level == PIPE_AUTH_LEVEL_NONE
-           || cli->auth->auth_level == PIPE_AUTH_LEVEL_CONNECT) {
+       if (cli->auth->auth_level == DCERPC_AUTH_LEVEL_NONE
+           || cli->auth->auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
                return NT_STATUS_OK;
        }
 
@@ -719,7 +686,7 @@ static NTSTATUS cli_pipe_verify_ntlmssp(struct rpc_pipe_client *cli, RPC_HDR *pr
        auth_blob.length = auth_len;
 
        switch (cli->auth->auth_level) {
-               case PIPE_AUTH_LEVEL_PRIVACY:
+               case DCERPC_AUTH_LEVEL_PRIVACY:
                        /* Data is encrypted. */
                        status = ntlmssp_unseal_packet(ntlmssp_state,
                                                        data, data_len,
@@ -729,12 +696,12 @@ static NTSTATUS cli_pipe_verify_ntlmssp(struct rpc_pipe_client *cli, RPC_HDR *pr
                        if (!NT_STATUS_IS_OK(status)) {
                                DEBUG(0,("cli_pipe_verify_ntlmssp: failed to unseal "
                                        "packet from %s. Error was %s.\n",
-                                       rpccli_pipe_txt(debug_ctx(), cli),
+                                       rpccli_pipe_txt(talloc_tos(), cli),
                                        nt_errstr(status) ));
                                return status;
                        }
                        break;
-               case PIPE_AUTH_LEVEL_INTEGRITY:
+               case DCERPC_AUTH_LEVEL_INTEGRITY:
                        /* Data is signed. */
                        status = ntlmssp_check_packet(ntlmssp_state,
                                                        data, data_len,
@@ -744,7 +711,7 @@ static NTSTATUS cli_pipe_verify_ntlmssp(struct rpc_pipe_client *cli, RPC_HDR *pr
                        if (!NT_STATUS_IS_OK(status)) {
                                DEBUG(0,("cli_pipe_verify_ntlmssp: check signing failed on "
                                        "packet from %s. Error was %s.\n",
-                                       rpccli_pipe_txt(debug_ctx(), cli),
+                                       rpccli_pipe_txt(talloc_tos(), cli),
                                        nt_errstr(status) ));
                                return status;
                        }
@@ -784,19 +751,21 @@ static NTSTATUS cli_pipe_verify_schannel(struct rpc_pipe_client *cli, RPC_HDR *p
                                uint8 *p_ss_padding_len)
 {
        RPC_HDR_AUTH auth_info;
-       RPC_AUTH_SCHANNEL_CHK schannel_chk;
        uint32 auth_len = prhdr->auth_len;
        uint32 save_offset = prs_offset(current_pdu);
-       struct schannel_auth_struct *schannel_auth =
+       struct schannel_state *schannel_auth =
                cli->auth->a_u.schannel_auth;
+       uint8_t *data;
        uint32 data_len;
+       DATA_BLOB blob;
+       NTSTATUS status;
 
-       if (cli->auth->auth_level == PIPE_AUTH_LEVEL_NONE
-           || cli->auth->auth_level == PIPE_AUTH_LEVEL_CONNECT) {
+       if (cli->auth->auth_level == DCERPC_AUTH_LEVEL_NONE
+           || cli->auth->auth_level == DCERPC_AUTH_LEVEL_CONNECT) {
                return NT_STATUS_OK;
        }
 
-       if (auth_len != RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN) {
+       if (auth_len < RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN) {
                DEBUG(0,("cli_pipe_verify_schannel: auth_len %u.\n", (unsigned int)auth_len ));
                return NT_STATUS_INVALID_PARAMETER;
        }
@@ -826,33 +795,50 @@ static NTSTATUS cli_pipe_verify_schannel(struct rpc_pipe_client *cli, RPC_HDR *p
                return NT_STATUS_BUFFER_TOO_SMALL;
        }
 
-       if (auth_info.auth_type != RPC_SCHANNEL_AUTH_TYPE) {
+       if (auth_info.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
                DEBUG(0,("cli_pipe_verify_schannel: Invalid auth info %d on schannel\n",
                        auth_info.auth_type));
                return NT_STATUS_BUFFER_TOO_SMALL;
        }
 
-       if(!smb_io_rpc_auth_schannel_chk("", RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN,
-                               &schannel_chk, current_pdu, 0)) {
-               DEBUG(0,("cli_pipe_verify_schannel: failed to unmarshal RPC_AUTH_SCHANNEL_CHK.\n"));
-               return NT_STATUS_BUFFER_TOO_SMALL;
+       blob = data_blob_const(prs_data_p(current_pdu) + prs_offset(current_pdu), auth_len);
+
+       if (DEBUGLEVEL >= 10) {
+               dump_NL_AUTH_SIGNATURE(talloc_tos(), &blob);
        }
 
-       if (!schannel_decode(schannel_auth,
-                       cli->auth->auth_level,
-                       SENDER_IS_ACCEPTOR,
-                       &schannel_chk,
-                       prs_data_p(current_pdu)+RPC_HEADER_LEN+RPC_HDR_RESP_LEN,
-                       data_len)) {
+       data = (uint8_t *)prs_data_p(current_pdu)+RPC_HEADER_LEN+RPC_HDR_RESP_LEN;
+
+       switch (cli->auth->auth_level) {
+       case DCERPC_AUTH_LEVEL_PRIVACY:
+               status = netsec_incoming_packet(schannel_auth,
+                                               talloc_tos(),
+                                               true,
+                                               data,
+                                               data_len,
+                                               &blob);
+               break;
+       case DCERPC_AUTH_LEVEL_INTEGRITY:
+               status = netsec_incoming_packet(schannel_auth,
+                                               talloc_tos(),
+                                               false,
+                                               data,
+                                               data_len,
+                                               &blob);
+               break;
+       default:
+               status = NT_STATUS_INTERNAL_ERROR;
+               break;
+       }
+
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(3,("cli_pipe_verify_schannel: failed to decode PDU "
-                               "Connection to %s.\n",
-                               rpccli_pipe_txt(debug_ctx(), cli)));
+                               "Connection to %s (%s).\n",
+                               rpccli_pipe_txt(talloc_tos(), cli),
+                               nt_errstr(status)));
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       /* The sequence number gets incremented on both send and receive. */
-       schannel_auth->seq_num++;
-
        /*
         * Return the current pointer to the data offset.
         */
@@ -906,7 +892,7 @@ static NTSTATUS cli_pipe_validate_rpc_response(struct rpc_pipe_client *cli, RPC_
                                DEBUG(3, ("cli_pipe_validate_rpc_response: "
                                          "Connection to %s - got non-zero "
                                          "auth len %u.\n",
-                                       rpccli_pipe_txt(debug_ctx(), cli),
+                                       rpccli_pipe_txt(talloc_tos(), cli),
                                        (unsigned int)prhdr->auth_len ));
                                return NT_STATUS_INVALID_PARAMETER;
                        }
@@ -932,7 +918,7 @@ static NTSTATUS cli_pipe_validate_rpc_response(struct rpc_pipe_client *cli, RPC_
                default:
                        DEBUG(3, ("cli_pipe_validate_rpc_response: Connection "
                                  "to %s - unknown internal auth type %u.\n",
-                                 rpccli_pipe_txt(debug_ctx(), cli),
+                                 rpccli_pipe_txt(talloc_tos(), cli),
                                  cli->auth->auth_type ));
                        return NT_STATUS_INVALID_INFO_CLASS;
        }
@@ -970,14 +956,14 @@ static NTSTATUS cli_pipe_validate_current_pdu(struct rpc_pipe_client *cli, RPC_H
 
        /* Ensure we have the correct type. */
        switch (prhdr->pkt_type) {
-               case RPC_ALTCONTRESP:
-               case RPC_BINDACK:
+               case DCERPC_PKT_ALTER_RESP:
+               case DCERPC_PKT_BIND_ACK:
 
                        /* Alter context and bind ack share the same packet definitions. */
                        break;
 
 
-               case RPC_RESPONSE:
+               case DCERPC_PKT_RESPONSE:
                {
                        RPC_HDR_RESP rhdr_resp;
                        uint8 ss_padding_len = 0;
@@ -1033,14 +1019,14 @@ static NTSTATUS cli_pipe_validate_current_pdu(struct rpc_pipe_client *cli, RPC_H
                        break;
                }
 
-               case RPC_BINDNACK:
+               case DCERPC_PKT_BIND_NAK:
                        DEBUG(1, ("cli_pipe_validate_current_pdu: Bind NACK "
                                  "received from %s!\n",
-                                 rpccli_pipe_txt(debug_ctx(), cli)));
+                                 rpccli_pipe_txt(talloc_tos(), cli)));
                        /* Use this for now... */
                        return NT_STATUS_NETWORK_ACCESS_DENIED;
 
-               case RPC_FAULT:
+               case DCERPC_PKT_FAULT:
                {
                        RPC_HDR_RESP rhdr_resp;
                        RPC_HDR_FAULT fault_resp;
@@ -1057,8 +1043,8 @@ static NTSTATUS cli_pipe_validate_current_pdu(struct rpc_pipe_client *cli, RPC_H
 
                        DEBUG(1, ("cli_pipe_validate_current_pdu: RPC fault "
                                  "code %s received from %s!\n",
-                               dcerpc_errstr(debug_ctx(), NT_STATUS_V(fault_resp.status)),
-                               rpccli_pipe_txt(debug_ctx(), cli)));
+                               dcerpc_errstr(talloc_tos(), NT_STATUS_V(fault_resp.status)),
+                               rpccli_pipe_txt(talloc_tos(), cli)));
                        if (NT_STATUS_IS_OK(fault_resp.status)) {
                                return NT_STATUS_UNSUCCESSFUL;
                        } else {
@@ -1070,14 +1056,14 @@ static NTSTATUS cli_pipe_validate_current_pdu(struct rpc_pipe_client *cli, RPC_H
                        DEBUG(0, ("cli_pipe_validate_current_pdu: unknown packet type %u received "
                                "from %s!\n",
                                (unsigned int)prhdr->pkt_type,
-                               rpccli_pipe_txt(debug_ctx(), cli)));
+                               rpccli_pipe_txt(talloc_tos(), cli)));
                        return NT_STATUS_INVALID_INFO_CLASS;
        }
 
        if (prhdr->pkt_type != expected_pkt_type) {
                DEBUG(3, ("cli_pipe_validate_current_pdu: Connection to %s "
                          "got an unexpected RPC packet type - %u, not %u\n",
-                       rpccli_pipe_txt(debug_ctx(), cli),
+                       rpccli_pipe_txt(talloc_tos(), cli),
                        prhdr->pkt_type,
                        expected_pkt_type));
                return NT_STATUS_INVALID_INFO_CLASS;
@@ -1087,10 +1073,10 @@ static NTSTATUS cli_pipe_validate_current_pdu(struct rpc_pipe_client *cli, RPC_H
           data before now as we may have needed to do cryptographic actions on
           it before. */
 
-       if ((prhdr->pkt_type == RPC_BINDACK) && !(prhdr->flags & RPC_FLG_LAST)) {
+       if ((prhdr->pkt_type == DCERPC_PKT_BIND_ACK) && !(prhdr->flags & DCERPC_PFC_FLAG_LAST)) {
                DEBUG(5,("cli_pipe_validate_current_pdu: bug in server (AS/U?), "
                        "setting fragment first/last ON.\n"));
-               prhdr->flags |= RPC_FLG_FIRST|RPC_FLG_LAST;
+               prhdr->flags |= DCERPC_PFC_FLAG_FIRST|DCERPC_PFC_FLAG_LAST;
        }
 
        return NT_STATUS_OK;
@@ -1145,41 +1131,33 @@ static NTSTATUS cli_pipe_reset_current_pdu(struct rpc_pipe_client *cli, RPC_HDR
 
 struct cli_api_pipe_state {
        struct event_context *ev;
-       struct rpc_pipe_client *cli;
-       uint32_t max_rdata_len;
+       struct rpc_cli_transport *transport;
        uint8_t *rdata;
        uint32_t rdata_len;
 };
 
-static void cli_api_pipe_np_trans_done(struct async_req *subreq);
-static void cli_api_pipe_sock_send_done(struct async_req *subreq);
-static void cli_api_pipe_sock_read_done(struct async_req *subreq);
+static void cli_api_pipe_trans_done(struct tevent_req *subreq);
+static void cli_api_pipe_write_done(struct tevent_req *subreq);
+static void cli_api_pipe_read_done(struct tevent_req *subreq);
 
-static struct async_req *cli_api_pipe_send(TALLOC_CTX *mem_ctx,
-                                          struct event_context *ev,
-                                          struct rpc_pipe_client *cli,
-                                          uint8_t *data, size_t data_len,
-                                          uint32_t max_rdata_len)
+static struct tevent_req *cli_api_pipe_send(TALLOC_CTX *mem_ctx,
+                                           struct event_context *ev,
+                                           struct rpc_cli_transport *transport,
+                                           uint8_t *data, size_t data_len,
+                                           uint32_t max_rdata_len)
 {
-       struct async_req *result, *subreq;
+       struct tevent_req *req, *subreq;
        struct cli_api_pipe_state *state;
        NTSTATUS status;
 
-       result = async_req_new(mem_ctx);
-       if (result == NULL) {
+       req = tevent_req_create(mem_ctx, &state, struct cli_api_pipe_state);
+       if (req == NULL) {
                return NULL;
        }
-       state = talloc(result, struct cli_api_pipe_state);
-       if (state == NULL) {
-               goto fail;
-       }
-       result->private_data = state;
-
        state->ev = ev;
-       state->cli = cli;
-       state->max_rdata_len = max_rdata_len;
+       state->transport = transport;
 
-       if (state->max_rdata_len < RPC_HEADER_LEN) {
+       if (max_rdata_len < RPC_HEADER_LEN) {
                /*
                 * For a RPC reply we always need at least RPC_HEADER_LEN
                 * bytes. We check this here because we will receive
@@ -1189,120 +1167,117 @@ static struct async_req *cli_api_pipe_send(TALLOC_CTX *mem_ctx,
                goto post_status;
        }
 
-       if (cli->transport_type == NCACN_NP) {
-
-               uint16_t setup[2];
-               SSVAL(setup+0, 0, TRANSACT_DCERPCCMD);
-               SSVAL(setup+1, 0, cli->trans.np.fnum);
-
-               subreq = cli_trans_send(
-                       state, ev, cli->trans.np.cli, SMBtrans,
-                       "\\PIPE\\", 0, 0, 0, setup, 2, 0,
-                       NULL, 0, 0, data, data_len, max_rdata_len);
+       if (transport->trans_send != NULL) {
+               subreq = transport->trans_send(state, ev, data, data_len,
+                                              max_rdata_len, transport->priv);
                if (subreq == NULL) {
-                       status = NT_STATUS_NO_MEMORY;
-                       goto post_status;
+                       goto fail;
                }
-               subreq->async.fn = cli_api_pipe_np_trans_done;
-               subreq->async.priv = result;
-               return result;
+               tevent_req_set_callback(subreq, cli_api_pipe_trans_done, req);
+               return req;
        }
 
-       if ((cli->transport_type == NCACN_IP_TCP)
-           || (cli->transport_type == NCACN_UNIX_STREAM)) {
-               subreq = sendall_send(state, ev, cli->trans.sock.fd,
-                                     data, data_len, 0);
-               if (subreq == NULL) {
-                       status = NT_STATUS_NO_MEMORY;
-                       goto post_status;
-               }
-               subreq->async.fn = cli_api_pipe_sock_send_done;
-               subreq->async.priv = result;
-               return result;
+       /*
+        * If the transport does not provide a "trans" routine, i.e. for
+        * example the ncacn_ip_tcp transport, do the write/read step here.
+        */
+
+       subreq = rpc_write_send(state, ev, transport, data, data_len);
+       if (subreq == NULL) {
+               goto fail;
        }
+       tevent_req_set_callback(subreq, cli_api_pipe_write_done, req);
+       return req;
 
        status = NT_STATUS_INVALID_PARAMETER;
 
  post_status:
-       if (async_post_status(result, ev, status)) {
-               return result;
-       }
+       tevent_req_nterror(req, status);
+       return tevent_req_post(req, ev);
  fail:
-       TALLOC_FREE(result);
+       TALLOC_FREE(req);
        return NULL;
 }
 
-static void cli_api_pipe_np_trans_done(struct async_req *subreq)
+static void cli_api_pipe_trans_done(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       struct cli_api_pipe_state *state = talloc_get_type_abort(
-               req->private_data, struct cli_api_pipe_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_api_pipe_state *state = tevent_req_data(
+               req, struct cli_api_pipe_state);
        NTSTATUS status;
 
-       status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL,
-                               &state->rdata, &state->rdata_len);
+       status = state->transport->trans_recv(subreq, state, &state->rdata,
+                                             &state->rdata_len);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
-       async_req_done(req);
+       tevent_req_done(req);
 }
 
-static void cli_api_pipe_sock_send_done(struct async_req *subreq)
+static void cli_api_pipe_write_done(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       struct cli_api_pipe_state *state = talloc_get_type_abort(
-               req->private_data, struct cli_api_pipe_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_api_pipe_state *state = tevent_req_data(
+               req, struct cli_api_pipe_state);
        NTSTATUS status;
 
-       status = sendall_recv(subreq);
+       status = rpc_write_recv(subreq);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
        state->rdata = TALLOC_ARRAY(state, uint8_t, RPC_HEADER_LEN);
-       if (async_req_nomem(state->rdata, req)) {
+       if (tevent_req_nomem(state->rdata, req)) {
                return;
        }
-       state->rdata_len = RPC_HEADER_LEN;
 
-       subreq = recvall_send(state, state->ev, state->cli->trans.sock.fd,
-                             state->rdata, RPC_HEADER_LEN, 0);
-       if (async_req_nomem(subreq, req)) {
+       /*
+        * We don't need to use rpc_read_send here, the upper layer will cope
+        * with a short read, transport->trans_send could also return less
+        * than state->max_rdata_len.
+        */
+       subreq = state->transport->read_send(state, state->ev, state->rdata,
+                                            RPC_HEADER_LEN,
+                                            state->transport->priv);
+       if (tevent_req_nomem(subreq, req)) {
                return;
        }
-       subreq->async.fn = cli_api_pipe_sock_read_done;
-       subreq->async.priv = req;
+       tevent_req_set_callback(subreq, cli_api_pipe_read_done, req);
 }
 
-static void cli_api_pipe_sock_read_done(struct async_req *subreq)
+static void cli_api_pipe_read_done(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_api_pipe_state *state = tevent_req_data(
+               req, struct cli_api_pipe_state);
        NTSTATUS status;
+       ssize_t received;
 
-       status = recvall_recv(subreq);
+       status = state->transport->read_recv(subreq, &received);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
-       async_req_done(req);
+       state->rdata_len = received;
+       tevent_req_done(req);
 }
 
-static NTSTATUS cli_api_pipe_recv(struct async_req *req, TALLOC_CTX *mem_ctx,
+static NTSTATUS cli_api_pipe_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                                  uint8_t **prdata, uint32_t *prdata_len)
 {
-       struct cli_api_pipe_state *state = talloc_get_type_abort(
-               req->private_data, struct cli_api_pipe_state);
+       struct cli_api_pipe_state *state = tevent_req_data(
+               req, struct cli_api_pipe_state);
        NTSTATUS status;
 
-       if (async_req_is_error(req, &status)) {
+       if (tevent_req_is_nterror(req, &status)) {
                return status;
        }
 
@@ -1356,29 +1331,24 @@ static int rpc_api_pipe_state_destructor(struct rpc_api_pipe_state *state)
        return 0;
 }
 
-static void rpc_api_pipe_trans_done(struct async_req *subreq);
-static void rpc_api_pipe_got_pdu(struct async_req *subreq);
+static void rpc_api_pipe_trans_done(struct tevent_req *subreq);
+static void rpc_api_pipe_got_pdu(struct tevent_req *subreq);
 
-static struct async_req *rpc_api_pipe_send(TALLOC_CTX *mem_ctx,
-                                          struct event_context *ev,
-                                          struct rpc_pipe_client *cli,
-                                          prs_struct *data, /* Outgoing PDU */
-                                          uint8_t expected_pkt_type)
+static struct tevent_req *rpc_api_pipe_send(TALLOC_CTX *mem_ctx,
+                                           struct event_context *ev,
+                                           struct rpc_pipe_client *cli,
+                                           prs_struct *data, /* Outgoing PDU */
+                                           uint8_t expected_pkt_type)
 {
-       struct async_req *result, *subreq;
+       struct tevent_req *req, *subreq;
        struct rpc_api_pipe_state *state;
+       uint16_t max_recv_frag;
        NTSTATUS status;
 
-       result = async_req_new(mem_ctx);
-       if (result == NULL) {
+       req = tevent_req_create(mem_ctx, &state, struct rpc_api_pipe_state);
+       if (req == NULL) {
                return NULL;
        }
-       state = talloc(result, struct rpc_api_pipe_state);
-       if (state == NULL) {
-               goto fail;
-       }
-       result->private_data = state;
-
        state->ev = ev;
        state->cli = cli;
        state->expected_pkt_type = expected_pkt_type;
@@ -1388,7 +1358,7 @@ static struct async_req *rpc_api_pipe_send(TALLOC_CTX *mem_ctx,
 
        prs_init_empty(&state->incoming_pdu, state, UNMARSHALL);
        /* Make incoming_pdu dynamic with no memory. */
-       prs_give_memory(&state->incoming_pdu, 0, 0, true);
+       prs_give_memory(&state->incoming_pdu, NULL, 0, true);
 
        talloc_set_destructor(state, rpc_api_pipe_state_destructor);
 
@@ -1400,34 +1370,37 @@ static struct async_req *rpc_api_pipe_send(TALLOC_CTX *mem_ctx,
                goto post_status;
        }
 
-       DEBUG(5,("rpc_api_pipe: %s\n", rpccli_pipe_txt(debug_ctx(), cli)));
+       DEBUG(5,("rpc_api_pipe: %s\n", rpccli_pipe_txt(talloc_tos(), cli)));
+
+       max_recv_frag = cli->max_recv_frag;
+
+#if 0
+       max_recv_frag = RPC_HEADER_LEN + 10 + (sys_random() % 32);
+#endif
 
-       subreq = cli_api_pipe_send(state, ev, cli,
+       subreq = cli_api_pipe_send(state, ev, cli->transport,
                                   (uint8_t *)prs_data_p(data),
-                                  prs_offset(data), cli->max_recv_frag);
+                                  prs_offset(data), max_recv_frag);
        if (subreq == NULL) {
-               status = NT_STATUS_NO_MEMORY;
-               goto post_status;
+               goto fail;
        }
-       subreq->async.fn = rpc_api_pipe_trans_done;
-       subreq->async.priv = result;
-       return result;
+       tevent_req_set_callback(subreq, rpc_api_pipe_trans_done, req);
+       return req;
 
  post_status:
-       if (async_post_status(result, ev, status)) {
-               return result;
-       }
+       tevent_req_nterror(req, status);
+       return tevent_req_post(req, ev);
  fail:
-       TALLOC_FREE(result);
+       TALLOC_FREE(req);
        return NULL;
 }
 
-static void rpc_api_pipe_trans_done(struct async_req *subreq)
+static void rpc_api_pipe_trans_done(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       struct rpc_api_pipe_state *state = talloc_get_type_abort(
-               req->private_data, struct rpc_api_pipe_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct rpc_api_pipe_state *state = tevent_req_data(
+               req, struct rpc_api_pipe_state);
        NTSTATUS status;
        uint8_t *rdata = NULL;
        uint32_t rdata_len = 0;
@@ -1437,14 +1410,14 @@ static void rpc_api_pipe_trans_done(struct async_req *subreq)
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(5, ("cli_api_pipe failed: %s\n", nt_errstr(status)));
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
        if (rdata == NULL) {
                DEBUG(3,("rpc_api_pipe: %s failed to return data.\n",
-                        rpccli_pipe_txt(debug_ctx(), state->cli)));
-               async_req_done(req);
+                        rpccli_pipe_txt(talloc_tos(), state->cli)));
+               tevent_req_done(req);
                return;
        }
 
@@ -1455,7 +1428,7 @@ static void rpc_api_pipe_trans_done(struct async_req *subreq)
         */
        rdata_copy = (char *)memdup(rdata, rdata_len);
        TALLOC_FREE(rdata);
-       if (async_req_nomem(rdata_copy, req)) {
+       if (tevent_req_nomem(rdata_copy, req)) {
                return;
        }
        prs_give_memory(&state->incoming_frag, rdata_copy, rdata_len, true);
@@ -1463,19 +1436,18 @@ static void rpc_api_pipe_trans_done(struct async_req *subreq)
        /* Ensure we have enough data for a pdu. */
        subreq = get_complete_frag_send(state, state->ev, state->cli,
                                        &state->rhdr, &state->incoming_frag);
-       if (async_req_nomem(subreq, req)) {
+       if (tevent_req_nomem(subreq, req)) {
                return;
        }
-       subreq->async.fn = rpc_api_pipe_got_pdu;
-       subreq->async.priv = req;
+       tevent_req_set_callback(subreq, rpc_api_pipe_got_pdu, req);
 }
 
-static void rpc_api_pipe_got_pdu(struct async_req *subreq)
+static void rpc_api_pipe_got_pdu(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       struct rpc_api_pipe_state *state = talloc_get_type_abort(
-               req->private_data, struct rpc_api_pipe_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct rpc_api_pipe_state *state = tevent_req_data(
+               req, struct rpc_api_pipe_state);
        NTSTATUS status;
        char *rdata = NULL;
        uint32_t rdata_len = 0;
@@ -1485,7 +1457,7 @@ static void rpc_api_pipe_got_pdu(struct async_req *subreq)
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(5, ("get_complete_frag failed: %s\n",
                          nt_errstr(status)));
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
@@ -1500,11 +1472,11 @@ static void rpc_api_pipe_got_pdu(struct async_req *subreq)
                  nt_errstr(status)));
 
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
-       if ((state->rhdr.flags & RPC_FLG_FIRST)
+       if ((state->rhdr.flags & DCERPC_PFC_FLAG_FIRST)
            && (state->rhdr.pack_type[0] == 0)) {
                /*
                 * Set the data type correctly for big-endian data on the
@@ -1512,7 +1484,7 @@ static void rpc_api_pipe_got_pdu(struct async_req *subreq)
                 */
                DEBUG(10,("rpc_api_pipe: On %s PDU data format is "
                          "big-endian.\n",
-                         rpccli_pipe_txt(debug_ctx(), state->cli)));
+                         rpccli_pipe_txt(talloc_tos(), state->cli)));
                prs_set_endian_data(&state->incoming_pdu, RPC_BIG_ENDIAN);
        }
        /*
@@ -1524,13 +1496,13 @@ static void rpc_api_pipe_got_pdu(struct async_req *subreq)
                         "%s\n",
                         state->incoming_pdu.bigendian_data?"big":"little",
                         state->incoming_frag.bigendian_data?"big":"little"));
-               async_req_error(req, NT_STATUS_INVALID_PARAMETER);
+               tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
        /* Now copy the data portion out of the pdu into rbuf. */
        if (!prs_force_grow(&state->incoming_pdu, rdata_len)) {
-               async_req_error(req, NT_STATUS_NO_MEMORY);
+               tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
                return;
        }
 
@@ -1541,35 +1513,34 @@ static void rpc_api_pipe_got_pdu(struct async_req *subreq)
        status = cli_pipe_reset_current_pdu(state->cli, &state->rhdr,
                                            &state->incoming_frag);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
-       if (state->rhdr.flags & RPC_FLG_LAST) {
+       if (state->rhdr.flags & DCERPC_PFC_FLAG_LAST) {
                DEBUG(10,("rpc_api_pipe: %s returned %u bytes.\n",
-                         rpccli_pipe_txt(debug_ctx(), state->cli),
+                         rpccli_pipe_txt(talloc_tos(), state->cli),
                          (unsigned)prs_data_size(&state->incoming_pdu)));
-               async_req_done(req);
+               tevent_req_done(req);
                return;
        }
 
        subreq = get_complete_frag_send(state, state->ev, state->cli,
                                        &state->rhdr, &state->incoming_frag);
-       if (async_req_nomem(subreq, req)) {
+       if (tevent_req_nomem(subreq, req)) {
                return;
        }
-       subreq->async.fn = rpc_api_pipe_got_pdu;
-       subreq->async.priv = req;
+       tevent_req_set_callback(subreq, rpc_api_pipe_got_pdu, req);
 }
 
-static NTSTATUS rpc_api_pipe_recv(struct async_req *req, TALLOC_CTX *mem_ctx,
+static NTSTATUS rpc_api_pipe_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                                  prs_struct *reply_pdu)
 {
-       struct rpc_api_pipe_state *state = talloc_get_type_abort(
-               req->private_data, struct rpc_api_pipe_state);
+       struct rpc_api_pipe_state *state = tevent_req_data(
+               req, struct rpc_api_pipe_state);
        NTSTATUS status;
 
-       if (async_req_is_error(req, &status)) {
+       if (tevent_req_is_nterror(req, &status)) {
                return status;
        }
 
@@ -1585,47 +1556,12 @@ static NTSTATUS rpc_api_pipe_recv(struct async_req *req, TALLOC_CTX *mem_ctx,
        return NT_STATUS_OK;
 }
 
-#if 0
-static NTSTATUS rpc_api_pipe(TALLOC_CTX *mem_ctx, struct rpc_pipe_client *cli,
-                            prs_struct *data, /* Outgoing pdu fragment,
-                                               * already formatted for
-                                               * send. */
-                            prs_struct *rbuf, /* Incoming reply - return as
-                                               * an NDR stream. */
-                            uint8 expected_pkt_type)
-{
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
-       struct async_req *req;
-       NTSTATUS status = NT_STATUS_NO_MEMORY;
-
-       ev = event_context_init(frame);
-       if (ev == NULL) {
-               goto fail;
-       }
-
-       req = rpc_api_pipe_send(frame, ev, cli, data, expected_pkt_type);
-       if (req == NULL) {
-               goto fail;
-       }
-
-       while (req->state < ASYNC_REQ_DONE) {
-               event_loop_once(ev);
-       }
-
-       status = rpc_api_pipe_recv(req, mem_ctx, rbuf);
- fail:
-       TALLOC_FREE(frame);
-       return status;
-}
-#endif
-
 /*******************************************************************
  Creates krb5 auth bind.
  ********************************************************************/
 
 static NTSTATUS create_krb5_auth_bind_req( struct rpc_pipe_client *cli,
-                                               enum pipe_auth_level auth_level,
+                                               enum dcerpc_AuthLevel auth_level,
                                                RPC_HDR_AUTH *pauth_out,
                                                prs_struct *auth_data)
 {
@@ -1636,7 +1572,7 @@ static NTSTATUS create_krb5_auth_bind_req( struct rpc_pipe_client *cli,
        DATA_BLOB tkt_wrapped = data_blob_null;
 
        /* We may change the pad length before marshalling. */
-       init_rpc_hdr_auth(pauth_out, RPC_KRB5_AUTH_TYPE, (int)auth_level, 0, 1);
+       init_rpc_hdr_auth(pauth_out, DCERPC_AUTH_TYPE_KRB5, (int)auth_level, 0, 1);
 
        DEBUG(5, ("create_krb5_auth_bind_req: creating a service ticket for principal %s\n",
                a->service_principal ));
@@ -1644,7 +1580,7 @@ static NTSTATUS create_krb5_auth_bind_req( struct rpc_pipe_client *cli,
        /* Create the ticket for the service principal and return it in a gss-api wrapped blob. */
 
        ret = cli_krb5_get_ticket(a->service_principal, 0, &tkt,
-                       &a->session_key, (uint32)AP_OPTS_MUTUAL_REQUIRED, NULL, NULL);
+                       &a->session_key, (uint32)AP_OPTS_MUTUAL_REQUIRED, NULL, NULL, NULL);
 
        if (ret) {
                DEBUG(1,("create_krb5_auth_bind_req: cli_krb5_get_ticket for principal %s "
@@ -1684,7 +1620,7 @@ static NTSTATUS create_krb5_auth_bind_req( struct rpc_pipe_client *cli,
  ********************************************************************/
 
 static NTSTATUS create_spnego_ntlmssp_auth_rpc_bind_req( struct rpc_pipe_client *cli,
-                                               enum pipe_auth_level auth_level,
+                                               enum dcerpc_AuthLevel auth_level,
                                                RPC_HDR_AUTH *pauth_out,
                                                prs_struct *auth_data)
 {
@@ -1694,7 +1630,7 @@ static NTSTATUS create_spnego_ntlmssp_auth_rpc_bind_req( struct rpc_pipe_client
        DATA_BLOB spnego_msg = data_blob_null;
 
        /* We may change the pad length before marshalling. */
-       init_rpc_hdr_auth(pauth_out, RPC_SPNEGO_AUTH_TYPE, (int)auth_level, 0, 1);
+       init_rpc_hdr_auth(pauth_out, DCERPC_AUTH_TYPE_SPNEGO, (int)auth_level, 0, 1);
 
        DEBUG(5, ("create_spnego_ntlmssp_auth_rpc_bind_req: Processing NTLMSSP Negotiate\n"));
        nt_status = ntlmssp_update(cli->auth->a_u.ntlmssp_state,
@@ -1731,7 +1667,7 @@ static NTSTATUS create_spnego_ntlmssp_auth_rpc_bind_req( struct rpc_pipe_client
  ********************************************************************/
 
 static NTSTATUS create_ntlmssp_auth_rpc_bind_req( struct rpc_pipe_client *cli,
-                                               enum pipe_auth_level auth_level,
+                                               enum dcerpc_AuthLevel auth_level,
                                                RPC_HDR_AUTH *pauth_out,
                                                prs_struct *auth_data)
 {
@@ -1740,7 +1676,7 @@ static NTSTATUS create_ntlmssp_auth_rpc_bind_req( struct rpc_pipe_client *cli,
        DATA_BLOB request = data_blob_null;
 
        /* We may change the pad length before marshalling. */
-       init_rpc_hdr_auth(pauth_out, RPC_NTLMSSP_AUTH_TYPE, (int)auth_level, 0, 1);
+       init_rpc_hdr_auth(pauth_out, DCERPC_AUTH_TYPE_NTLMSSP, (int)auth_level, 0, 1);
 
        DEBUG(5, ("create_ntlmssp_auth_rpc_bind_req: Processing NTLMSSP Negotiate\n"));
        nt_status = ntlmssp_update(cli->auth->a_u.ntlmssp_state,
@@ -1772,14 +1708,16 @@ static NTSTATUS create_ntlmssp_auth_rpc_bind_req( struct rpc_pipe_client *cli,
  ********************************************************************/
 
 static NTSTATUS create_schannel_auth_rpc_bind_req( struct rpc_pipe_client *cli,
-                                               enum pipe_auth_level auth_level,
+                                               enum dcerpc_AuthLevel auth_level,
                                                RPC_HDR_AUTH *pauth_out,
                                                prs_struct *auth_data)
 {
-       RPC_AUTH_SCHANNEL_NEG schannel_neg;
+       struct NL_AUTH_MESSAGE r;
+       enum ndr_err_code ndr_err;
+       DATA_BLOB blob;
 
        /* We may change the pad length before marshalling. */
-       init_rpc_hdr_auth(pauth_out, RPC_SCHANNEL_AUTH_TYPE, (int)auth_level, 0, 1);
+       init_rpc_hdr_auth(pauth_out, DCERPC_AUTH_TYPE_SCHANNEL, (int)auth_level, 0, 1);
 
        /* Use lp_workgroup() if domain not specified */
 
@@ -1790,32 +1728,46 @@ static NTSTATUS create_schannel_auth_rpc_bind_req( struct rpc_pipe_client *cli,
                }
        }
 
-       init_rpc_auth_schannel_neg(&schannel_neg, cli->auth->domain,
-                                  global_myname());
-
        /*
         * Now marshall the data into the auth parse_struct.
         */
 
-       if(!smb_io_rpc_auth_schannel_neg("schannel_neg",
-                                      &schannel_neg, auth_data, 0)) {
-               DEBUG(0,("Failed to marshall RPC_AUTH_SCHANNEL_NEG.\n"));
+       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        = global_myname();
+
+       ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), NULL, &r,
+                      (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               DEBUG(0,("Failed to marshall NL_AUTH_MESSAGE.\n"));
                prs_mem_free(auth_data);
-               return NT_STATUS_NO_MEMORY;
+               return ndr_map_error2ntstatus(ndr_err);
        }
 
-       return NT_STATUS_OK;
-}
+       if (DEBUGLEVEL >= 10) {
+               NDR_PRINT_DEBUG(NL_AUTH_MESSAGE, &r);
+       }
+
+       if (!prs_copy_data_in(auth_data, (const char *)blob.data, blob.length))
+       {
+               prs_mem_free(auth_data);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       return NT_STATUS_OK;
+}
 
 /*******************************************************************
  Creates the internals of a DCE/RPC bind request or alter context PDU.
  ********************************************************************/
 
-static NTSTATUS create_bind_or_alt_ctx_internal(enum RPC_PKT_TYPE pkt_type,
+static NTSTATUS create_bind_or_alt_ctx_internal(enum dcerpc_pkt_type pkt_type,
                                                prs_struct *rpc_out, 
                                                uint32 rpc_call_id,
-                                               const RPC_IFACE *abstract,
-                                               const RPC_IFACE *transfer,
+                                               const struct ndr_syntax_id *abstract,
+                                               const struct ndr_syntax_id *transfer,
                                                RPC_HDR_AUTH *phdr_auth,
                                                prs_struct *pauth_info)
 {
@@ -1846,7 +1798,7 @@ static NTSTATUS create_bind_or_alt_ctx_internal(enum RPC_PKT_TYPE pkt_type,
        }
 
        /* Create the request RPC_HDR */
-       init_rpc_hdr(&hdr, pkt_type, RPC_FLG_FIRST|RPC_FLG_LAST, rpc_call_id, frag_len, auth_len);
+       init_rpc_hdr(&hdr, pkt_type, DCERPC_PFC_FLAG_FIRST|DCERPC_PFC_FLAG_LAST, rpc_call_id, frag_len, auth_len);
 
        /* Marshall the RPC header */
        if(!smb_io_rpc_hdr("hdr"   , &hdr, rpc_out, 0)) {
@@ -1896,10 +1848,10 @@ static NTSTATUS create_bind_or_alt_ctx_internal(enum RPC_PKT_TYPE pkt_type,
 static NTSTATUS create_rpc_bind_req(struct rpc_pipe_client *cli,
                                prs_struct *rpc_out, 
                                uint32 rpc_call_id,
-                               const RPC_IFACE *abstract,
-                               const RPC_IFACE *transfer,
+                               const struct ndr_syntax_id *abstract,
+                               const struct ndr_syntax_id *transfer,
                                enum pipe_auth_type auth_type,
-                               enum pipe_auth_level auth_level)
+                               enum dcerpc_AuthLevel auth_level)
 {
        RPC_HDR_AUTH hdr_auth;
        prs_struct auth_info;
@@ -1950,7 +1902,7 @@ static NTSTATUS create_rpc_bind_req(struct rpc_pipe_client *cli,
                        return NT_STATUS_INVALID_INFO_CLASS;
        }
 
-       ret = create_bind_or_alt_ctx_internal(RPC_BIND,
+       ret = create_bind_or_alt_ctx_internal(DCERPC_PKT_BIND,
                                                rpc_out, 
                                                rpc_call_id,
                                                abstract,
@@ -1995,7 +1947,7 @@ static NTSTATUS add_ntlmssp_auth_footer(struct rpc_pipe_client *cli,
        }
 
        switch (cli->auth->auth_level) {
-               case PIPE_AUTH_LEVEL_PRIVACY:
+               case DCERPC_AUTH_LEVEL_PRIVACY:
                        /* Data portion is encrypted. */
                        status = ntlmssp_seal_packet(cli->auth->a_u.ntlmssp_state,
                                        (unsigned char *)prs_data_p(outgoing_pdu) + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
@@ -2009,7 +1961,7 @@ static NTSTATUS add_ntlmssp_auth_footer(struct rpc_pipe_client *cli,
                        }
                        break;
 
-               case PIPE_AUTH_LEVEL_INTEGRITY:
+               case DCERPC_AUTH_LEVEL_INTEGRITY:
                        /* Data is signed. */
                        status = ntlmssp_sign_packet(cli->auth->a_u.ntlmssp_state,
                                        (unsigned char *)prs_data_p(outgoing_pdu) + RPC_HEADER_LEN + RPC_HDR_RESP_LEN,
@@ -2053,10 +2005,11 @@ static NTSTATUS add_schannel_auth_footer(struct rpc_pipe_client *cli,
                                        prs_struct *outgoing_pdu)
 {
        RPC_HDR_AUTH auth_info;
-       RPC_AUTH_SCHANNEL_CHK verf;
-       struct schannel_auth_struct *sas = cli->auth->a_u.schannel_auth;
+       struct schannel_state *sas = cli->auth->a_u.schannel_auth;
        char *data_p = prs_data_p(outgoing_pdu) + RPC_HEADER_LEN + RPC_HDR_RESP_LEN;
        size_t data_and_pad_len = prs_offset(outgoing_pdu) - RPC_HEADER_LEN - RPC_HDR_RESP_LEN;
+       DATA_BLOB blob;
+       NTSTATUS status;
 
        if (!sas) {
                return NT_STATUS_INVALID_PARAMETER;
@@ -2074,35 +2027,45 @@ static NTSTATUS add_schannel_auth_footer(struct rpc_pipe_client *cli,
                return NT_STATUS_NO_MEMORY;
        }
 
+       DEBUG(10,("add_schannel_auth_footer: SCHANNEL seq_num=%d\n",
+                       sas->seq_num));
+
        switch (cli->auth->auth_level) {
-               case PIPE_AUTH_LEVEL_PRIVACY:
-               case PIPE_AUTH_LEVEL_INTEGRITY:
-                       DEBUG(10,("add_schannel_auth_footer: SCHANNEL seq_num=%d\n",
-                               sas->seq_num));
-
-                       schannel_encode(sas,
-                                       cli->auth->auth_level,
-                                       SENDER_IS_INITIATOR,
-                                       &verf,
-                                       data_p,
-                                       data_and_pad_len);
-
-                       sas->seq_num++;
-                       break;
+       case DCERPC_AUTH_LEVEL_PRIVACY:
+               status = netsec_outgoing_packet(sas,
+                                               talloc_tos(),
+                                               true,
+                                               (uint8_t *)data_p,
+                                               data_and_pad_len,
+                                               &blob);
+               break;
+       case DCERPC_AUTH_LEVEL_INTEGRITY:
+               status = netsec_outgoing_packet(sas,
+                                               talloc_tos(),
+                                               false,
+                                               (uint8_t *)data_p,
+                                               data_and_pad_len,
+                                               &blob);
+               break;
+       default:
+               status = NT_STATUS_INTERNAL_ERROR;
+               break;
+       }
 
-               default:
-                       /* Can't happen. */
-                       smb_panic("bad auth level");
-                       /* Notreached. */
-                       return NT_STATUS_INVALID_PARAMETER;
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1,("add_schannel_auth_footer: failed to process packet: %s\n",
+                       nt_errstr(status)));
+               return status;
+       }
+
+       if (DEBUGLEVEL >= 10) {
+               dump_NL_AUTH_SIGNATURE(talloc_tos(), &blob);
        }
 
        /* Finally marshall the blob. */
-       smb_io_rpc_auth_schannel_chk("",
-                       RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN,
-                       &verf,
-                       outgoing_pdu,
-                       0);
+       if (!prs_copy_data_in(outgoing_pdu, (const char *)blob.data, blob.length)) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
        return NT_STATUS_OK;
 }
@@ -2120,9 +2083,15 @@ static uint32 calculate_data_len_tosend(struct rpc_pipe_client *cli,
 {
        uint32 data_space, data_len;
 
+#if 0
+       if ((data_left > 0) && (sys_random() % 2)) {
+               data_left = MAX(data_left/2, 1);
+       }
+#endif
+
        switch (cli->auth->auth_level) {
-               case PIPE_AUTH_LEVEL_NONE:
-               case PIPE_AUTH_LEVEL_CONNECT:
+               case DCERPC_AUTH_LEVEL_NONE:
+               case DCERPC_AUTH_LEVEL_CONNECT:
                        data_space = cli->max_xmit_frag - RPC_HEADER_LEN - RPC_HDR_REQ_LEN;
                        data_len = MIN(data_space, data_left);
                        *p_ss_padding = 0;
@@ -2130,8 +2099,8 @@ static uint32 calculate_data_len_tosend(struct rpc_pipe_client *cli,
                        *p_frag_len = RPC_HEADER_LEN + RPC_HDR_REQ_LEN + data_len;
                        return data_len;
 
-               case PIPE_AUTH_LEVEL_INTEGRITY:
-               case PIPE_AUTH_LEVEL_PRIVACY:
+               case DCERPC_AUTH_LEVEL_INTEGRITY:
+               case DCERPC_AUTH_LEVEL_PRIVACY:
                        /* Treat the same for all authenticated rpc requests. */
                        switch(cli->auth->auth_type) {
                                case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
@@ -2191,32 +2160,27 @@ static int rpc_api_pipe_req_state_destructor(struct rpc_api_pipe_req_state *s)
        return 0;
 }
 
-static void rpc_api_pipe_req_write_done(struct async_req *subreq);
-static void rpc_api_pipe_req_done(struct async_req *subreq);
+static void rpc_api_pipe_req_write_done(struct tevent_req *subreq);
+static void rpc_api_pipe_req_done(struct tevent_req *subreq);
 static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
                                  bool *is_last_frag);
 
-struct async_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
-                                       struct event_context *ev,
-                                       struct rpc_pipe_client *cli,
-                                       uint8_t op_num,
-                                       prs_struct *req_data)
+struct tevent_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
+                                        struct event_context *ev,
+                                        struct rpc_pipe_client *cli,
+                                        uint8_t op_num,
+                                        prs_struct *req_data)
 {
-       struct async_req *result, *subreq;
+       struct tevent_req *req, *subreq;
        struct rpc_api_pipe_req_state *state;
        NTSTATUS status;
        bool is_last_frag;
 
-       result = async_req_new(mem_ctx);
-       if (result == NULL) {
+       req = tevent_req_create(mem_ctx, &state,
+                               struct rpc_api_pipe_req_state);
+       if (req == NULL) {
                return NULL;
        }
-       state = talloc(result, struct rpc_api_pipe_req_state);
-       if (state == NULL) {
-               goto fail;
-       }
-       result->private_data = state;
-
        state->ev = ev;
        state->cli = cli;
        state->op_num = op_num;
@@ -2235,8 +2199,7 @@ struct async_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
 
        if (!prs_init(&state->outgoing_frag, cli->max_xmit_frag,
                      state, MARSHALL)) {
-               status = NT_STATUS_NO_MEMORY;
-               goto post_status;
+               goto fail;
        }
 
        talloc_set_destructor(state, rpc_api_pipe_req_state_destructor);
@@ -2249,32 +2212,29 @@ struct async_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
        if (is_last_frag) {
                subreq = rpc_api_pipe_send(state, ev, state->cli,
                                           &state->outgoing_frag,
-                                          RPC_RESPONSE);
+                                          DCERPC_PKT_RESPONSE);
                if (subreq == NULL) {
-                       status = NT_STATUS_NO_MEMORY;
-                       goto post_status;
+                       goto fail;
                }
-               subreq->async.fn = rpc_api_pipe_req_done;
-               subreq->async.priv = result;
+               tevent_req_set_callback(subreq, rpc_api_pipe_req_done, req);
        } else {
-               subreq = rpc_write_send(state, ev, cli,
-                                       prs_data_p(&state->outgoing_frag),
-                                       prs_offset(&state->outgoing_frag));
+               subreq = rpc_write_send(
+                       state, ev, cli->transport,
+                       (uint8_t *)prs_data_p(&state->outgoing_frag),
+                       prs_offset(&state->outgoing_frag));
                if (subreq == NULL) {
-                       status = NT_STATUS_NO_MEMORY;
-                       goto post_status;
+                       goto fail;
                }
-               subreq->async.fn = rpc_api_pipe_req_write_done;
-               subreq->async.priv = result;
+               tevent_req_set_callback(subreq, rpc_api_pipe_req_write_done,
+                                       req);
        }
-       return result;
+       return req;
 
  post_status:
-       if (async_post_status(result, ev, status)) {
-               return result;
-       }
+       tevent_req_nterror(req, status);
+       return tevent_req_post(req, ev);
  fail:
-       TALLOC_FREE(result);
+       TALLOC_FREE(req);
        return NULL;
 }
 
@@ -2298,11 +2258,11 @@ static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
                state->cli, data_left, &frag_len, &auth_len, &ss_padding);
 
        if (state->req_data_sent == 0) {
-               flags = RPC_FLG_FIRST;
+               flags = DCERPC_PFC_FLAG_FIRST;
        }
 
        if (data_sent_thistime == data_left) {
-               flags |= RPC_FLG_LAST;
+               flags |= DCERPC_PFC_FLAG_LAST;
        }
 
        if (!prs_set_offset(&state->outgoing_frag, 0)) {
@@ -2310,7 +2270,7 @@ static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
        }
 
        /* Create and marshall the header and request header. */
-       init_rpc_hdr(&hdr, RPC_REQUEST, flags, state->call_id, frag_len,
+       init_rpc_hdr(&hdr, DCERPC_PKT_REQUEST, flags, state->call_id, frag_len,
                     auth_len);
 
        if (!smb_io_rpc_hdr("hdr    ", &hdr, &state->outgoing_frag, 0)) {
@@ -2358,79 +2318,85 @@ static NTSTATUS prepare_next_frag(struct rpc_api_pipe_req_state *state,
        }
 
        state->req_data_sent += data_sent_thistime;
-       *is_last_frag = ((flags & RPC_FLG_LAST) != 0);
+       *is_last_frag = ((flags & DCERPC_PFC_FLAG_LAST) != 0);
 
        return status;
 }
 
-static void rpc_api_pipe_req_write_done(struct async_req *subreq)
+static void rpc_api_pipe_req_write_done(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       struct rpc_api_pipe_req_state *state = talloc_get_type_abort(
-               req->private_data, struct rpc_api_pipe_req_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct rpc_api_pipe_req_state *state = tevent_req_data(
+               req, struct rpc_api_pipe_req_state);
        NTSTATUS status;
        bool is_last_frag;
 
        status = rpc_write_recv(subreq);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
        status = prepare_next_frag(state, &is_last_frag);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
        if (is_last_frag) {
                subreq = rpc_api_pipe_send(state, state->ev, state->cli,
                                           &state->outgoing_frag,
-                                          RPC_RESPONSE);
-               if (async_req_nomem(subreq, req)) {
+                                          DCERPC_PKT_RESPONSE);
+               if (tevent_req_nomem(subreq, req)) {
                        return;
                }
-               subreq->async.fn = rpc_api_pipe_req_done;
-               subreq->async.priv = req;
+               tevent_req_set_callback(subreq, rpc_api_pipe_req_done, req);
        } else {
-               subreq = rpc_write_send(state, state->ev, state->cli,
-                                       prs_data_p(&state->outgoing_frag),
-                                       prs_offset(&state->outgoing_frag));
-               if (async_req_nomem(subreq, req)) {
+               subreq = rpc_write_send(
+                       state, state->ev,
+                       state->cli->transport,
+                       (uint8_t *)prs_data_p(&state->outgoing_frag),
+                       prs_offset(&state->outgoing_frag));
+               if (tevent_req_nomem(subreq, req)) {
                        return;
                }
-               subreq->async.fn = rpc_api_pipe_req_write_done;
-               subreq->async.priv = req;
+               tevent_req_set_callback(subreq, rpc_api_pipe_req_write_done,
+                                       req);
        }
 }
 
-static void rpc_api_pipe_req_done(struct async_req *subreq)
+static void rpc_api_pipe_req_done(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       struct rpc_api_pipe_req_state *state = talloc_get_type_abort(
-               req->private_data, struct rpc_api_pipe_req_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct rpc_api_pipe_req_state *state = tevent_req_data(
+               req, struct rpc_api_pipe_req_state);
        NTSTATUS status;
 
        status = rpc_api_pipe_recv(subreq, state, &state->reply_pdu);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
-       async_req_done(req);
+       tevent_req_done(req);
 }
 
-NTSTATUS rpc_api_pipe_req_recv(struct async_req *req, TALLOC_CTX *mem_ctx,
+NTSTATUS rpc_api_pipe_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                               prs_struct *reply_pdu)
 {
-       struct rpc_api_pipe_req_state *state = talloc_get_type_abort(
-               req->private_data, struct rpc_api_pipe_req_state);
+       struct rpc_api_pipe_req_state *state = tevent_req_data(
+               req, struct rpc_api_pipe_req_state);
        NTSTATUS status;
 
-       if (async_req_is_error(req, &status)) {
+       if (tevent_req_is_nterror(req, &status)) {
+               /*
+                * We always have to initialize to reply pdu, even if there is
+                * none. The rpccli_* caller routines expect this.
+                */
+               prs_init_empty(reply_pdu, mem_ctx, UNMARSHALL);
                return status;
        }
 
@@ -2446,36 +2412,6 @@ NTSTATUS rpc_api_pipe_req_recv(struct async_req *req, TALLOC_CTX *mem_ctx,
        return NT_STATUS_OK;
 }
 
-NTSTATUS rpc_api_pipe_req(TALLOC_CTX *mem_ctx, struct rpc_pipe_client *cli,
-                       uint8 op_num,
-                       prs_struct *in_data,
-                       prs_struct *out_data)
-{
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
-       struct async_req *req;
-       NTSTATUS status = NT_STATUS_NO_MEMORY;
-
-       ev = event_context_init(frame);
-       if (ev == NULL) {
-               goto fail;
-       }
-
-       req = rpc_api_pipe_req_send(frame, ev, cli, op_num, in_data);
-       if (req == NULL) {
-               goto fail;
-       }
-
-       while (req->state < ASYNC_REQ_DONE) {
-               event_loop_once(ev);
-       }
-
-       status = rpc_api_pipe_req_recv(req, mem_ctx, out_data);
- fail:
-       TALLOC_FREE(frame);
-       return status;
-}
-
 #if 0
 /****************************************************************************
  Set the handle state.
@@ -2527,500 +2463,135 @@ static bool rpc_pipe_set_hnd_state(struct rpc_pipe_client *cli,
  Check the rpc bind acknowledge response.
 ****************************************************************************/
 
-static bool check_bind_response(RPC_HDR_BA *hdr_ba, const RPC_IFACE *transfer)
+static bool check_bind_response(RPC_HDR_BA *hdr_ba,
+                               const struct ndr_syntax_id *transfer)
 {
        if ( hdr_ba->addr.len == 0) {
                DEBUG(4,("Ignoring length check -- ASU bug (server didn't fill in the pipe name correctly)"));
        }
 
        /* check the transfer syntax */
-       if ((hdr_ba->transfer.if_version != transfer->if_version) ||
-            (memcmp(&hdr_ba->transfer.uuid, &transfer->uuid, sizeof(transfer->uuid)) !=0)) {
-               DEBUG(2,("bind_rpc_pipe: transfer syntax differs\n"));
-               return False;
-       }
-
-       if (hdr_ba->res.num_results != 0x1 || hdr_ba->res.result != 0) {
-               DEBUG(2,("bind_rpc_pipe: bind denied results: %d reason: %x\n",
-                         hdr_ba->res.num_results, hdr_ba->res.reason));
-       }
-
-       DEBUG(5,("check_bind_response: accepted!\n"));
-       return True;
-}
-
-/*******************************************************************
- Creates a DCE/RPC bind authentication response.
- This is the packet that is sent back to the server once we
- have received a BIND-ACK, to finish the third leg of
- the authentication handshake.
- ********************************************************************/
-
-static NTSTATUS create_rpc_bind_auth3(struct rpc_pipe_client *cli,
-                               uint32 rpc_call_id,
-                               enum pipe_auth_type auth_type,
-                               enum pipe_auth_level auth_level,
-                               DATA_BLOB *pauth_blob,
-                               prs_struct *rpc_out)
-{
-       RPC_HDR hdr;
-       RPC_HDR_AUTH hdr_auth;
-       uint32 pad = 0;
-
-       /* Create the request RPC_HDR */
-       init_rpc_hdr(&hdr, RPC_AUTH3, RPC_FLG_FIRST|RPC_FLG_LAST, rpc_call_id,
-                    RPC_HEADER_LEN + 4 /* pad */ + RPC_HDR_AUTH_LEN + pauth_blob->length,
-                    pauth_blob->length );
-
-       /* Marshall it. */
-       if(!smb_io_rpc_hdr("hdr", &hdr, rpc_out, 0)) {
-               DEBUG(0,("create_rpc_bind_auth3: failed to marshall RPC_HDR.\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       /*
-               I'm puzzled about this - seems to violate the DCE RPC auth rules,
-               about padding - shouldn't this pad to length 8 ? JRA.
-       */
-
-       /* 4 bytes padding. */
-       if (!prs_uint32("pad", rpc_out, 0, &pad)) {
-               DEBUG(0,("create_rpc_bind_auth3: failed to marshall 4 byte pad.\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       /* Create the request RPC_HDR_AUTHA */
-       init_rpc_hdr_auth(&hdr_auth,
-                       map_pipe_auth_type_to_rpc_auth_type(auth_type),
-                       auth_level, 0, 1);
-
-       if(!smb_io_rpc_hdr_auth("hdr_auth", &hdr_auth, rpc_out, 0)) {
-               DEBUG(0,("create_rpc_bind_auth3: failed to marshall RPC_HDR_AUTHA.\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       /*
-        * Append the auth data to the outgoing buffer.
-        */
-
-       if(!prs_copy_data_in(rpc_out, (char *)pauth_blob->data, pauth_blob->length)) {
-               DEBUG(0,("create_rpc_bind_auth3: failed to marshall auth blob.\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       return NT_STATUS_OK;
-}
-
-/****************************************************************************
- Create and send the third packet in an RPC auth.
-****************************************************************************/
-
-#if 0
-static NTSTATUS rpc_finish_auth3_bind(struct rpc_pipe_client *cli,
-                               RPC_HDR *phdr,
-                               prs_struct *rbuf,
-                               uint32 rpc_call_id,
-                               enum pipe_auth_type auth_type,
-                               enum pipe_auth_level auth_level)
-{
-       DATA_BLOB server_response = data_blob_null;
-       DATA_BLOB client_reply = data_blob_null;
-       RPC_HDR_AUTH hdr_auth;
-       NTSTATUS nt_status;
-       prs_struct rpc_out;
-       ssize_t ret;
-
-       if (!phdr->auth_len || (phdr->frag_len < phdr->auth_len + RPC_HDR_AUTH_LEN)) {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       /* Process the returned NTLMSSP blob first. */
-       if (!prs_set_offset(rbuf, phdr->frag_len - phdr->auth_len - RPC_HDR_AUTH_LEN)) {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       if(!smb_io_rpc_hdr_auth("hdr_auth", &hdr_auth, rbuf, 0)) {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       /* TODO - check auth_type/auth_level match. */
-
-       server_response = data_blob(NULL, phdr->auth_len);
-       prs_copy_data_out((char *)server_response.data, rbuf, phdr->auth_len);
-
-       nt_status = ntlmssp_update(cli->auth->a_u.ntlmssp_state,
-                                  server_response,
-                                  &client_reply);
-
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               DEBUG(0,("rpc_finish_auth3_bind: NTLMSSP update using server blob failed.\n"));
-               data_blob_free(&server_response);
-               return nt_status;
-       }
-
-       prs_init_empty(&rpc_out, prs_get_mem_context(rbuf), MARSHALL);
-
-       nt_status = create_rpc_bind_auth3(cli, rpc_call_id,
-                               auth_type, auth_level,
-                               &client_reply, &rpc_out);
-
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               prs_mem_free(&rpc_out);
-               data_blob_free(&client_reply);
-               data_blob_free(&server_response);
-               return nt_status;
-       }
-
-       switch (cli->transport_type) {
-       case NCACN_NP:
-               /* 8 here is named pipe message mode. */
-               ret = cli_write(cli->trans.np.cli, cli->trans.np.fnum,
-                               0x8, prs_data_p(&rpc_out), 0,
-                               (size_t)prs_offset(&rpc_out));
-               break;
-
-               if (ret != (ssize_t)prs_offset(&rpc_out)) {
-                       nt_status = cli_get_nt_error(cli->trans.np.cli);
-               }
-       case NCACN_IP_TCP:
-       case NCACN_UNIX_STREAM:
-               ret = write_data(cli->trans.sock.fd, prs_data_p(&rpc_out),
-                                (size_t)prs_offset(&rpc_out));
-               if (ret != (ssize_t)prs_offset(&rpc_out)) {
-                       nt_status = map_nt_error_from_unix(errno);
-               }
-               break;
-       default:
-               DEBUG(0, ("unknown transport type %d\n", cli->transport_type));
-               return NT_STATUS_INTERNAL_ERROR;
-       }
-
-       if (ret != (ssize_t)prs_offset(&rpc_out)) {
-               DEBUG(0,("rpc_send_auth_auth3: write failed. Return was %s\n",
-                        nt_errstr(nt_status)));
-               prs_mem_free(&rpc_out);
-               data_blob_free(&client_reply);
-               data_blob_free(&server_response);
-               return nt_status;
-       }
-
-       DEBUG(5,("rpc_send_auth_auth3: %s sent auth3 response ok.\n",
-                rpccli_pipe_txt(debug_ctx(), cli)));
-
-       prs_mem_free(&rpc_out);
-       data_blob_free(&client_reply);
-       data_blob_free(&server_response);
-       return NT_STATUS_OK;
-}
-#endif
-
-/*******************************************************************
- Creates a DCE/RPC bind alter context authentication request which
- may contain a spnego auth blobl
- ********************************************************************/
-
-static NTSTATUS create_rpc_alter_context(uint32 rpc_call_id,
-                                       const RPC_IFACE *abstract,
-                                       const RPC_IFACE *transfer,
-                                       enum pipe_auth_level auth_level,
-                                       const DATA_BLOB *pauth_blob, /* spnego auth blob already created. */
-                                       prs_struct *rpc_out)
-{
-       RPC_HDR_AUTH hdr_auth;
-       prs_struct auth_info;
-       NTSTATUS ret = NT_STATUS_OK;
-
-       ZERO_STRUCT(hdr_auth);
-       if (!prs_init(&auth_info, RPC_HDR_AUTH_LEN, prs_get_mem_context(rpc_out), MARSHALL))
-               return NT_STATUS_NO_MEMORY;
-
-       /* We may change the pad length before marshalling. */
-       init_rpc_hdr_auth(&hdr_auth, RPC_SPNEGO_AUTH_TYPE, (int)auth_level, 0, 1);
-
-       if (pauth_blob->length) {
-               if (!prs_copy_data_in(&auth_info, (const char *)pauth_blob->data, pauth_blob->length)) {
-                       prs_mem_free(&auth_info);
-                       return NT_STATUS_NO_MEMORY;
-               }
-       }
-
-       ret = create_bind_or_alt_ctx_internal(RPC_ALTCONT,
-                                               rpc_out, 
-                                               rpc_call_id,
-                                               abstract,
-                                               transfer,
-                                               &hdr_auth,
-                                               &auth_info);
-       prs_mem_free(&auth_info);
-       return ret;
-}
-
-/*******************************************************************
- Third leg of the SPNEGO bind mechanism - sends alter context PDU
- and gets a response.
- ********************************************************************/
-
-#if 0
-static NTSTATUS rpc_finish_spnego_ntlmssp_bind(struct rpc_pipe_client *cli,
-                                RPC_HDR *phdr,
-                                prs_struct *rbuf,
-                                uint32 rpc_call_id,
-                               const RPC_IFACE *abstract,
-                               const RPC_IFACE *transfer,
-                                enum pipe_auth_type auth_type,
-                                enum pipe_auth_level auth_level)
-{
-       DATA_BLOB server_spnego_response = data_blob_null;
-       DATA_BLOB server_ntlm_response = data_blob_null;
-       DATA_BLOB client_reply = data_blob_null;
-       DATA_BLOB tmp_blob = data_blob_null;
-       RPC_HDR_AUTH hdr_auth;
-       NTSTATUS nt_status;
-       prs_struct rpc_out;
-
-       if (!phdr->auth_len || (phdr->frag_len < phdr->auth_len + RPC_HDR_AUTH_LEN)) {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       /* Process the returned NTLMSSP blob first. */
-       if (!prs_set_offset(rbuf, phdr->frag_len - phdr->auth_len - RPC_HDR_AUTH_LEN)) {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       if(!smb_io_rpc_hdr_auth("hdr_auth", &hdr_auth, rbuf, 0)) {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       server_spnego_response = data_blob(NULL, phdr->auth_len);
-       prs_copy_data_out((char *)server_spnego_response.data, rbuf, phdr->auth_len);
-
-       /* The server might give us back two challenges - tmp_blob is for the second. */
-       if (!spnego_parse_challenge(server_spnego_response, &server_ntlm_response, &tmp_blob)) {
-               data_blob_free(&server_spnego_response);
-               data_blob_free(&server_ntlm_response);
-               data_blob_free(&tmp_blob);
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       /* We're finished with the server spnego response and the tmp_blob. */
-       data_blob_free(&server_spnego_response);
-       data_blob_free(&tmp_blob);
-
-       nt_status = ntlmssp_update(cli->auth->a_u.ntlmssp_state,
-                                  server_ntlm_response,
-                                  &client_reply);
-
-       /* Finished with the server_ntlm response */
-       data_blob_free(&server_ntlm_response);
-
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               DEBUG(0,("rpc_finish_spnego_ntlmssp_bind: NTLMSSP update using server blob failed.\n"));
-               data_blob_free(&client_reply);
-               return nt_status;
-       }
-
-       /* SPNEGO wrap the client reply. */
-       tmp_blob = spnego_gen_auth(client_reply);
-       data_blob_free(&client_reply);
-       client_reply = tmp_blob;
-       tmp_blob = data_blob_null; /* Ensure it's safe to free this just in case. */
-
-       /* Now prepare the alter context pdu. */
-       prs_init_empty(&rpc_out, prs_get_mem_context(rbuf), MARSHALL);
-
-       nt_status = create_rpc_alter_context(rpc_call_id,
-                                               abstract,
-                                               transfer,
-                                               auth_level,
-                                               &client_reply,
-                                               &rpc_out);
-
-       data_blob_free(&client_reply);
-
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               prs_mem_free(&rpc_out);
-               return nt_status;
-       }
-
-       /* Initialize the returning data struct. */
-       prs_mem_free(rbuf);
-
-       nt_status = rpc_api_pipe(talloc_tos(), cli, &rpc_out, rbuf,
-                                RPC_ALTCONTRESP);
-       prs_mem_free(&rpc_out);
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               return nt_status;
-       }
-
-       /* Get the auth blob from the reply. */
-       if(!smb_io_rpc_hdr("rpc_hdr   ", phdr, rbuf, 0)) {
-               DEBUG(0,("rpc_finish_spnego_ntlmssp_bind: Failed to unmarshall RPC_HDR.\n"));
-               return NT_STATUS_BUFFER_TOO_SMALL;
-       }
-
-       if (!prs_set_offset(rbuf, phdr->frag_len - phdr->auth_len - RPC_HDR_AUTH_LEN)) {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       if(!smb_io_rpc_hdr_auth("hdr_auth", &hdr_auth, rbuf, 0)) {
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       server_spnego_response = data_blob(NULL, phdr->auth_len);
-       prs_copy_data_out((char *)server_spnego_response.data, rbuf, phdr->auth_len);
-
-       /* Check we got a valid auth response. */
-       if (!spnego_parse_auth_response(server_spnego_response, NT_STATUS_OK, OID_NTLMSSP, &tmp_blob)) {
-               data_blob_free(&server_spnego_response);
-               data_blob_free(&tmp_blob);
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       data_blob_free(&server_spnego_response);
-       data_blob_free(&tmp_blob);
-
-       DEBUG(5,("rpc_finish_spnego_ntlmssp_bind: alter context request to "
-                "%s.\n", rpccli_pipe_txt(debug_ctx(), cli)));
-
-       return NT_STATUS_OK;
-}
-#endif
-
-/****************************************************************************
- Do an rpc bind.
-****************************************************************************/
-
-#if 0
-NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
-                      struct cli_pipe_auth_data *auth)
-{
-       RPC_HDR hdr;
-       RPC_HDR_BA hdr_ba;
-       prs_struct rpc_out;
-       prs_struct rbuf;
-       uint32 rpc_call_id;
-       NTSTATUS status;
-
-       DEBUG(5,("Bind RPC Pipe: %s auth_type %u, auth_level %u\n",
-               rpccli_pipe_txt(debug_ctx(), cli),
-               (unsigned int)auth->auth_type,
-               (unsigned int)auth->auth_level ));
+       if ((hdr_ba->transfer.if_version != transfer->if_version) ||
+            (memcmp(&hdr_ba->transfer.uuid, &transfer->uuid, sizeof(transfer->uuid)) !=0)) {
+               DEBUG(2,("bind_rpc_pipe: transfer syntax differs\n"));
+               return False;
+       }
 
-       cli->auth = talloc_move(cli, &auth);
+       if (hdr_ba->res.num_results != 0x1 || hdr_ba->res.result != 0) {
+               DEBUG(2,("bind_rpc_pipe: bind denied results: %d reason: %x\n",
+                         hdr_ba->res.num_results, hdr_ba->res.reason));
+       }
 
-       prs_init_empty(&rpc_out, talloc_tos(), MARSHALL);
+       DEBUG(5,("check_bind_response: accepted!\n"));
+       return True;
+}
 
-       rpc_call_id = get_rpc_call_id();
+/*******************************************************************
+ Creates a DCE/RPC bind authentication response.
+ This is the packet that is sent back to the server once we
+ have received a BIND-ACK, to finish the third leg of
+ the authentication handshake.
+ ********************************************************************/
 
-       /* Marshall the outgoing data. */
-       status = create_rpc_bind_req(cli, &rpc_out, rpc_call_id,
-                               &cli->abstract_syntax,
-                               &cli->transfer_syntax,
-                               cli->auth->auth_type,
-                               cli->auth->auth_level);
+static NTSTATUS create_rpc_bind_auth3(struct rpc_pipe_client *cli,
+                               uint32 rpc_call_id,
+                               enum pipe_auth_type auth_type,
+                               enum dcerpc_AuthLevel auth_level,
+                               DATA_BLOB *pauth_blob,
+                               prs_struct *rpc_out)
+{
+       RPC_HDR hdr;
+       RPC_HDR_AUTH hdr_auth;
+       uint32 pad = 0;
 
-       if (!NT_STATUS_IS_OK(status)) {
-               prs_mem_free(&rpc_out);
-               return status;
-       }
+       /* Create the request RPC_HDR */
+       init_rpc_hdr(&hdr, DCERPC_PKT_AUTH3, DCERPC_PFC_FLAG_FIRST|DCERPC_PFC_FLAG_LAST, rpc_call_id,
+                    RPC_HEADER_LEN + 4 /* pad */ + RPC_HDR_AUTH_LEN + pauth_blob->length,
+                    pauth_blob->length );
 
-       /* send data on \PIPE\.  receive a response */
-       status = rpc_api_pipe(talloc_tos(), cli, &rpc_out, &rbuf, RPC_BINDACK);
-       prs_mem_free(&rpc_out);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       /* Marshall it. */
+       if(!smb_io_rpc_hdr("hdr", &hdr, rpc_out, 0)) {
+               DEBUG(0,("create_rpc_bind_auth3: failed to marshall RPC_HDR.\n"));
+               return NT_STATUS_NO_MEMORY;
        }
 
-       DEBUG(3,("rpc_pipe_bind: %s bind request returned ok.\n",
-                rpccli_pipe_txt(debug_ctx(), cli)));
+       /*
+               I'm puzzled about this - seems to violate the DCE RPC auth rules,
+               about padding - shouldn't this pad to length 8 ? JRA.
+       */
 
-       /* Unmarshall the RPC header */
-       if(!smb_io_rpc_hdr("hdr"   , &hdr, &rbuf, 0)) {
-               DEBUG(0,("rpc_pipe_bind: failed to unmarshall RPC_HDR.\n"));
-               prs_mem_free(&rbuf);
-               return NT_STATUS_BUFFER_TOO_SMALL;
+       /* 4 bytes padding. */
+       if (!prs_uint32("pad", rpc_out, 0, &pad)) {
+               DEBUG(0,("create_rpc_bind_auth3: failed to marshall 4 byte pad.\n"));
+               return NT_STATUS_NO_MEMORY;
        }
 
-       if(!smb_io_rpc_hdr_ba("", &hdr_ba, &rbuf, 0)) {
-               DEBUG(0,("rpc_pipe_bind: Failed to unmarshall RPC_HDR_BA.\n"));
-               prs_mem_free(&rbuf);
-               return NT_STATUS_BUFFER_TOO_SMALL;
-       }
+       /* Create the request RPC_HDR_AUTHA */
+       init_rpc_hdr_auth(&hdr_auth,
+                       map_pipe_auth_type_to_rpc_auth_type(auth_type),
+                       auth_level, 0, 1);
 
-       if(!check_bind_response(&hdr_ba, &cli->transfer_syntax)) {
-               DEBUG(2,("rpc_pipe_bind: check_bind_response failed.\n"));
-               prs_mem_free(&rbuf);
-               return NT_STATUS_BUFFER_TOO_SMALL;
+       if(!smb_io_rpc_hdr_auth("hdr_auth", &hdr_auth, rpc_out, 0)) {
+               DEBUG(0,("create_rpc_bind_auth3: failed to marshall RPC_HDR_AUTHA.\n"));
+               return NT_STATUS_NO_MEMORY;
        }
 
-       cli->max_xmit_frag = hdr_ba.bba.max_tsize;
-       cli->max_recv_frag = hdr_ba.bba.max_rsize;
+       /*
+        * Append the auth data to the outgoing buffer.
+        */
 
-       /* For authenticated binds we may need to do 3 or 4 leg binds. */
-       switch(cli->auth->auth_type) {
+       if(!prs_copy_data_in(rpc_out, (char *)pauth_blob->data, pauth_blob->length)) {
+               DEBUG(0,("create_rpc_bind_auth3: failed to marshall auth blob.\n"));
+               return NT_STATUS_NO_MEMORY;
+       }
 
-               case PIPE_AUTH_TYPE_NONE:
-               case PIPE_AUTH_TYPE_SCHANNEL:
-                       /* Bind complete. */
-                       break;
+       return NT_STATUS_OK;
+}
 
-               case PIPE_AUTH_TYPE_NTLMSSP:
-                       /* Need to send AUTH3 packet - no reply. */
-                       status = rpc_finish_auth3_bind(
-                               cli, &hdr, &rbuf, rpc_call_id,
-                               cli->auth->auth_type,
-                               cli->auth->auth_level);
-                       if (!NT_STATUS_IS_OK(status)) {
-                               prs_mem_free(&rbuf);
-                               return status;
-                       }
-                       break;
+/*******************************************************************
+ Creates a DCE/RPC bind alter context authentication request which
+ may contain a spnego auth blobl
+ ********************************************************************/
 
-               case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
-                       /* Need to send alter context request and reply. */
-                       status = rpc_finish_spnego_ntlmssp_bind(
-                               cli, &hdr, &rbuf, rpc_call_id,
-                               &cli->abstract_syntax, &cli->transfer_syntax,
-                               cli->auth->auth_type, cli->auth->auth_level);
-                       if (!NT_STATUS_IS_OK(status)) {
-                               prs_mem_free(&rbuf);
-                               return status;
-                       }
-                       break;
+static NTSTATUS create_rpc_alter_context(uint32 rpc_call_id,
+                                       const struct ndr_syntax_id *abstract,
+                                       const struct ndr_syntax_id *transfer,
+                                       enum dcerpc_AuthLevel auth_level,
+                                       const DATA_BLOB *pauth_blob, /* spnego auth blob already created. */
+                                       prs_struct *rpc_out)
+{
+       RPC_HDR_AUTH hdr_auth;
+       prs_struct auth_info;
+       NTSTATUS ret = NT_STATUS_OK;
 
-               case PIPE_AUTH_TYPE_KRB5:
-                       /* */
+       ZERO_STRUCT(hdr_auth);
+       if (!prs_init(&auth_info, RPC_HDR_AUTH_LEN, prs_get_mem_context(rpc_out), MARSHALL))
+               return NT_STATUS_NO_MEMORY;
 
-               default:
-                       DEBUG(0,("cli_finish_bind_auth: unknown auth type "
-                                "%u\n", (unsigned int)cli->auth->auth_type));
-                       prs_mem_free(&rbuf);
-                       return NT_STATUS_INVALID_INFO_CLASS;
-       }
+       /* We may change the pad length before marshalling. */
+       init_rpc_hdr_auth(&hdr_auth, DCERPC_AUTH_TYPE_SPNEGO, (int)auth_level, 0, 1);
 
-       /* For NTLMSSP ensure the server gave us the auth_level we wanted. */
-       if (cli->auth->auth_type == PIPE_AUTH_TYPE_NTLMSSP
-           || cli->auth->auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP) {
-               if (cli->auth->auth_level == PIPE_AUTH_LEVEL_INTEGRITY) {
-                       if (!(cli->auth->a_u.ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SIGN)) {
-                               DEBUG(0,("cli_finish_bind_auth: requested NTLMSSSP signing and server refused.\n"));
-                               prs_mem_free(&rbuf);
-                               return NT_STATUS_INVALID_PARAMETER;
-                       }
-               }
-               if (cli->auth->auth_level == PIPE_AUTH_LEVEL_INTEGRITY) {
-                       if (!(cli->auth->a_u.ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_SEAL)) {
-                               DEBUG(0,("cli_finish_bind_auth: requested NTLMSSSP sealing and server refused.\n"));
-                               prs_mem_free(&rbuf);
-                               return NT_STATUS_INVALID_PARAMETER;
-                       }
+       if (pauth_blob->length) {
+               if (!prs_copy_data_in(&auth_info, (const char *)pauth_blob->data, pauth_blob->length)) {
+                       prs_mem_free(&auth_info);
+                       return NT_STATUS_NO_MEMORY;
                }
        }
 
-       prs_mem_free(&rbuf);
-       return NT_STATUS_OK;
+       ret = create_bind_or_alt_ctx_internal(DCERPC_PKT_ALTER,
+                                               rpc_out, 
+                                               rpc_call_id,
+                                               abstract,
+                                               transfer,
+                                               &hdr_auth,
+                                               &auth_info);
+       prs_mem_free(&auth_info);
+       return ret;
 }
-#endif
+
+/****************************************************************************
+ Do an rpc bind.
+****************************************************************************/
 
 struct rpc_pipe_bind_state {
        struct event_context *ev;
@@ -3035,39 +2606,34 @@ static int rpc_pipe_bind_state_destructor(struct rpc_pipe_bind_state *state)
        return 0;
 }
 
-static void rpc_pipe_bind_step_one_done(struct async_req *subreq);
-static NTSTATUS rpc_finish_auth3_bind_send(struct async_req *req,
+static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq);
+static NTSTATUS rpc_finish_auth3_bind_send(struct tevent_req *req,
                                           struct rpc_pipe_bind_state *state,
                                           struct rpc_hdr_info *phdr,
                                           prs_struct *reply_pdu);
-static void rpc_bind_auth3_write_done(struct async_req *subreq);
-static NTSTATUS rpc_finish_spnego_ntlmssp_bind_send(struct async_req *req,
+static void rpc_bind_auth3_write_done(struct tevent_req *subreq);
+static NTSTATUS rpc_finish_spnego_ntlmssp_bind_send(struct tevent_req *req,
                                                    struct rpc_pipe_bind_state *state,
                                                    struct rpc_hdr_info *phdr,
                                                    prs_struct *reply_pdu);
-static void rpc_bind_ntlmssp_api_done(struct async_req *subreq);
+static void rpc_bind_ntlmssp_api_done(struct tevent_req *subreq);
 
-struct async_req *rpc_pipe_bind_send(TALLOC_CTX *mem_ctx,
-                                    struct event_context *ev,
-                                    struct rpc_pipe_client *cli,
-                                    struct cli_pipe_auth_data *auth)
+struct tevent_req *rpc_pipe_bind_send(TALLOC_CTX *mem_ctx,
+                                     struct event_context *ev,
+                                     struct rpc_pipe_client *cli,
+                                     struct cli_pipe_auth_data *auth)
 {
-       struct async_req *result, *subreq;
+       struct tevent_req *req, *subreq;
        struct rpc_pipe_bind_state *state;
        NTSTATUS status;
 
-       result = async_req_new(mem_ctx);
-       if (result == NULL) {
+       req = tevent_req_create(mem_ctx, &state, struct rpc_pipe_bind_state);
+       if (req == NULL) {
                return NULL;
        }
-       state = talloc(result, struct rpc_pipe_bind_state);
-       if (state == NULL) {
-               goto fail;
-       }
-       result->private_data = state;
 
        DEBUG(5,("Bind RPC Pipe: %s auth_type %u, auth_level %u\n",
-               rpccli_pipe_txt(debug_ctx(), cli),
+               rpccli_pipe_txt(talloc_tos(), cli),
                (unsigned int)auth->auth_type,
                (unsigned int)auth->auth_level ));
 
@@ -3093,30 +2659,27 @@ struct async_req *rpc_pipe_bind_send(TALLOC_CTX *mem_ctx,
        }
 
        subreq = rpc_api_pipe_send(state, ev, cli, &state->rpc_out,
-                                  RPC_BINDACK);
+                                  DCERPC_PKT_BIND_ACK);
        if (subreq == NULL) {
-               status = NT_STATUS_NO_MEMORY;
-               goto post_status;
+               goto fail;
        }
-       subreq->async.fn = rpc_pipe_bind_step_one_done;
-       subreq->async.priv = result;
-       return result;
+       tevent_req_set_callback(subreq, rpc_pipe_bind_step_one_done, req);
+       return req;
 
  post_status:
-       if (async_post_status(result, ev, status)) {
-               return result;
-       }
+       tevent_req_nterror(req, status);
+       return tevent_req_post(req, ev);
  fail:
-       TALLOC_FREE(result);
+       TALLOC_FREE(req);
        return NULL;
 }
 
-static void rpc_pipe_bind_step_one_done(struct async_req *subreq)
+static void rpc_pipe_bind_step_one_done(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       struct rpc_pipe_bind_state *state = talloc_get_type_abort(
-               req->private_data, struct rpc_pipe_bind_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct rpc_pipe_bind_state *state = tevent_req_data(
+               req, struct rpc_pipe_bind_state);
        prs_struct reply_pdu;
        struct rpc_hdr_info hdr;
        struct rpc_hdr_ba_info hdr_ba;
@@ -3126,29 +2689,32 @@ static void rpc_pipe_bind_step_one_done(struct async_req *subreq)
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(3, ("rpc_pipe_bind: %s bind request returned %s\n",
-                         rpccli_pipe_txt(debug_ctx(), state->cli),
+                         rpccli_pipe_txt(talloc_tos(), state->cli),
                          nt_errstr(status)));
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
        /* Unmarshall the RPC header */
        if (!smb_io_rpc_hdr("hdr", &hdr, &reply_pdu, 0)) {
                DEBUG(0, ("rpc_pipe_bind: failed to unmarshall RPC_HDR.\n"));
-               async_req_error(req, NT_STATUS_BUFFER_TOO_SMALL);
+               prs_mem_free(&reply_pdu);
+               tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
                return;
        }
 
        if (!smb_io_rpc_hdr_ba("", &hdr_ba, &reply_pdu, 0)) {
                DEBUG(0, ("rpc_pipe_bind: Failed to unmarshall "
                          "RPC_HDR_BA.\n"));
-               async_req_error(req, NT_STATUS_BUFFER_TOO_SMALL);
+               prs_mem_free(&reply_pdu);
+               tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
                return;
        }
 
        if (!check_bind_response(&hdr_ba, &state->cli->transfer_syntax)) {
                DEBUG(2, ("rpc_pipe_bind: check_bind_response failed.\n"));
-               async_req_error(req, NT_STATUS_BUFFER_TOO_SMALL);
+               prs_mem_free(&reply_pdu);
+               tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
                return;
        }
 
@@ -3164,15 +2730,17 @@ static void rpc_pipe_bind_step_one_done(struct async_req *subreq)
        case PIPE_AUTH_TYPE_NONE:
        case PIPE_AUTH_TYPE_SCHANNEL:
                /* Bind complete. */
-               async_req_done(req);
+               prs_mem_free(&reply_pdu);
+               tevent_req_done(req);
                break;
 
        case PIPE_AUTH_TYPE_NTLMSSP:
                /* Need to send AUTH3 packet - no reply. */
                status = rpc_finish_auth3_bind_send(req, state, &hdr,
                                                    &reply_pdu);
+               prs_mem_free(&reply_pdu);
                if (!NT_STATUS_IS_OK(status)) {
-                       async_req_error(req, status);
+                       tevent_req_nterror(req, status);
                }
                break;
 
@@ -3180,8 +2748,9 @@ static void rpc_pipe_bind_step_one_done(struct async_req *subreq)
                /* Need to send alter context request and reply. */
                status = rpc_finish_spnego_ntlmssp_bind_send(req, state, &hdr,
                                                             &reply_pdu);
+               prs_mem_free(&reply_pdu);
                if (!NT_STATUS_IS_OK(status)) {
-                       async_req_error(req, status);
+                       tevent_req_nterror(req, status);
                }
                break;
 
@@ -3191,11 +2760,12 @@ static void rpc_pipe_bind_step_one_done(struct async_req *subreq)
        default:
                DEBUG(0,("cli_finish_bind_auth: unknown auth type %u\n",
                         (unsigned int)state->cli->auth->auth_type));
-               async_req_error(req, NT_STATUS_INTERNAL_ERROR);
+               prs_mem_free(&reply_pdu);
+               tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
        }
 }
 
-static NTSTATUS rpc_finish_auth3_bind_send(struct async_req *req,
+static NTSTATUS rpc_finish_auth3_bind_send(struct tevent_req *req,
                                           struct rpc_pipe_bind_state *state,
                                           struct rpc_hdr_info *phdr,
                                           prs_struct *reply_pdu)
@@ -3203,7 +2773,7 @@ static NTSTATUS rpc_finish_auth3_bind_send(struct async_req *req,
        DATA_BLOB server_response = data_blob_null;
        DATA_BLOB client_reply = data_blob_null;
        struct rpc_hdr_auth_info hdr_auth;
-       struct async_req *subreq;
+       struct tevent_req *subreq;
        NTSTATUS status;
 
        if ((phdr->auth_len == 0)
@@ -3248,33 +2818,32 @@ static NTSTATUS rpc_finish_auth3_bind_send(struct async_req *req,
                return status;
        }
 
-       subreq = rpc_write_send(state, state->ev, state->cli,
-                               prs_data_p(&state->rpc_out),
+       subreq = rpc_write_send(state, state->ev, state->cli->transport,
+                               (uint8_t *)prs_data_p(&state->rpc_out),
                                prs_offset(&state->rpc_out));
        if (subreq == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
-       subreq->async.fn = rpc_bind_auth3_write_done;
-       subreq->async.priv = req;
+       tevent_req_set_callback(subreq, rpc_bind_auth3_write_done, req);
        return NT_STATUS_OK;
 }
 
-static void rpc_bind_auth3_write_done(struct async_req *subreq)
+static void rpc_bind_auth3_write_done(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
        NTSTATUS status;
 
        status = rpc_write_recv(subreq);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
-       async_req_done(req);
+       tevent_req_done(req);
 }
 
-static NTSTATUS rpc_finish_spnego_ntlmssp_bind_send(struct async_req *req,
+static NTSTATUS rpc_finish_spnego_ntlmssp_bind_send(struct tevent_req *req,
                                                    struct rpc_pipe_bind_state *state,
                                                    struct rpc_hdr_info *phdr,
                                                    prs_struct *reply_pdu)
@@ -3284,7 +2853,7 @@ static NTSTATUS rpc_finish_spnego_ntlmssp_bind_send(struct async_req *req,
        DATA_BLOB client_reply = data_blob_null;
        DATA_BLOB tmp_blob = data_blob_null;
        RPC_HDR_AUTH hdr_auth;
-       struct async_req *subreq;
+       struct tevent_req *subreq;
        NTSTATUS status;
 
        if ((phdr->auth_len == 0)
@@ -3358,21 +2927,20 @@ static NTSTATUS rpc_finish_spnego_ntlmssp_bind_send(struct async_req *req,
        }
 
        subreq = rpc_api_pipe_send(state, state->ev, state->cli,
-                                  &state->rpc_out, RPC_ALTCONTRESP);
+                                  &state->rpc_out, DCERPC_PKT_ALTER_RESP);
        if (subreq == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
-       subreq->async.fn = rpc_bind_ntlmssp_api_done;
-       subreq->async.priv = req;
+       tevent_req_set_callback(subreq, rpc_bind_ntlmssp_api_done, req);
        return NT_STATUS_OK;
 }
 
-static void rpc_bind_ntlmssp_api_done(struct async_req *subreq)
+static void rpc_bind_ntlmssp_api_done(struct tevent_req *subreq)
 {
-       struct async_req *req = talloc_get_type_abort(
-               subreq->async.priv, struct async_req);
-       struct rpc_pipe_bind_state *state = talloc_get_type_abort(
-               req->private_data, struct rpc_pipe_bind_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct rpc_pipe_bind_state *state = tevent_req_data(
+               req, struct rpc_pipe_bind_state);
        DATA_BLOB server_spnego_response = data_blob_null;
        DATA_BLOB tmp_blob = data_blob_null;
        prs_struct reply_pdu;
@@ -3383,7 +2951,7 @@ static void rpc_bind_ntlmssp_api_done(struct async_req *subreq)
        status = rpc_api_pipe_recv(subreq, talloc_tos(), &reply_pdu);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_error(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
@@ -3391,19 +2959,19 @@ static void rpc_bind_ntlmssp_api_done(struct async_req *subreq)
        if (!smb_io_rpc_hdr("rpc_hdr   ", &hdr, &reply_pdu, 0)) {
                DEBUG(0, ("rpc_finish_spnego_ntlmssp_bind: Failed to "
                          "unmarshall RPC_HDR.\n"));
-               async_req_error(req, NT_STATUS_BUFFER_TOO_SMALL);
+               tevent_req_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
                return;
        }
 
        if (!prs_set_offset(
                    &reply_pdu,
                    hdr.frag_len - hdr.auth_len - RPC_HDR_AUTH_LEN)) {
-               async_req_error(req, NT_STATUS_INVALID_PARAMETER);
+               tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
        if (!smb_io_rpc_hdr_auth("hdr_auth", &hdr_auth, &reply_pdu, 0)) {
-               async_req_error(req, NT_STATUS_INVALID_PARAMETER);
+               tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
@@ -3416,7 +2984,7 @@ static void rpc_bind_ntlmssp_api_done(struct async_req *subreq)
                                        OID_NTLMSSP, &tmp_blob)) {
                data_blob_free(&server_spnego_response);
                data_blob_free(&tmp_blob);
-               async_req_error(req, NT_STATUS_INVALID_PARAMETER);
+               tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
                return;
        }
 
@@ -3424,13 +2992,13 @@ static void rpc_bind_ntlmssp_api_done(struct async_req *subreq)
        data_blob_free(&tmp_blob);
 
        DEBUG(5,("rpc_finish_spnego_ntlmssp_bind: alter context request to "
-                "%s.\n", rpccli_pipe_txt(debug_ctx(), state->cli)));
-       async_req_done(req);
+                "%s.\n", rpccli_pipe_txt(talloc_tos(), state->cli)));
+       tevent_req_done(req);
 }
 
-NTSTATUS rpc_pipe_bind_recv(struct async_req *req)
+NTSTATUS rpc_pipe_bind_recv(struct tevent_req *req)
 {
-       return async_req_simple_recv(req);
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
@@ -3438,21 +3006,24 @@ NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
 {
        TALLOC_CTX *frame = talloc_stackframe();
        struct event_context *ev;
-       struct async_req *req;
-       NTSTATUS status = NT_STATUS_NO_MEMORY;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_OK;
 
        ev = event_context_init(frame);
        if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
        req = rpc_pipe_bind_send(frame, ev, cli, auth);
        if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
-       while (req->state < ASYNC_REQ_DONE) {
-               event_loop_once(ev);
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
        }
 
        status = rpc_pipe_bind_recv(req);
@@ -3461,56 +3032,33 @@ NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
        return status;
 }
 
-unsigned int rpccli_set_timeout(struct rpc_pipe_client *cli,
+unsigned int rpccli_set_timeout(struct rpc_pipe_client *rpc_cli,
                                unsigned int timeout)
 {
-       return cli_set_timeout(cli->trans.np.cli, timeout);
-}
-
-bool rpccli_get_pwd_hash(struct rpc_pipe_client *cli, uint8_t nt_hash[16])
-{
-       if ((cli->auth->auth_type == PIPE_AUTH_TYPE_NTLMSSP)
-           || (cli->auth->auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP)) {
-               memcpy(nt_hash, cli->auth->a_u.ntlmssp_state->nt_hash, 16);
-               return true;
-       }
-
-       if (cli->transport_type == NCACN_NP) {
-               E_md4hash(cli->trans.np.cli->pwd.password, nt_hash);
-               return true;
-       }
+       struct cli_state *cli = rpc_pipe_np_smb_conn(rpc_cli);
 
-       return false;
-}
-
-struct cli_state *rpc_pipe_np_smb_conn(struct rpc_pipe_client *p)
-{
-       if (p->transport_type == NCACN_NP) {
-               return p->trans.np.cli;
+       if (cli == NULL) {
+               return 0;
        }
-       return NULL;
+       return cli_set_timeout(cli, timeout);
 }
 
-static int rpc_pipe_destructor(struct rpc_pipe_client *p)
+bool rpccli_get_pwd_hash(struct rpc_pipe_client *rpc_cli, uint8_t nt_hash[16])
 {
-       if (p->transport_type == NCACN_NP) {
-               bool ret;
-               ret = cli_close(p->trans.np.cli, p->trans.np.fnum);
-               if (!ret) {
-                       DEBUG(1, ("rpc_pipe_destructor: cli_close failed on "
-                                 "pipe %s. Error was %s\n",
-                                 rpccli_pipe_txt(debug_ctx(), p),
-                                 cli_errstr(p->trans.np.cli)));
-               }
-
-               DEBUG(10, ("rpc_pipe_destructor: closed %s\n",
-                          rpccli_pipe_txt(debug_ctx(), p)));
+       struct cli_state *cli;
 
-               DLIST_REMOVE(p->trans.np.cli->pipe_list, p);
-               return ret ? -1 : 0;
+       if ((rpc_cli->auth->auth_type == PIPE_AUTH_TYPE_NTLMSSP)
+           || (rpc_cli->auth->auth_type == PIPE_AUTH_TYPE_SPNEGO_NTLMSSP)) {
+               memcpy(nt_hash, rpc_cli->auth->a_u.ntlmssp_state->nt_hash, 16);
+               return true;
        }
 
-       return -1;
+       cli = rpc_pipe_np_smb_conn(rpc_cli);
+       if (cli == NULL) {
+               return false;
+       }
+       E_md4hash(cli->password ? cli->password : "", nt_hash);
+       return true;
 }
 
 NTSTATUS rpccli_anon_bind_data(TALLOC_CTX *mem_ctx,
@@ -3524,7 +3072,7 @@ NTSTATUS rpccli_anon_bind_data(TALLOC_CTX *mem_ctx,
        }
 
        result->auth_type = PIPE_AUTH_TYPE_NONE;
-       result->auth_level = PIPE_AUTH_LEVEL_NONE;
+       result->auth_level = DCERPC_AUTH_LEVEL_NONE;
 
        result->user_name = talloc_strdup(result, "");
        result->domain = talloc_strdup(result, "");
@@ -3545,7 +3093,7 @@ static int cli_auth_ntlmssp_data_destructor(struct cli_pipe_auth_data *auth)
 
 NTSTATUS rpccli_ntlmssp_bind_data(TALLOC_CTX *mem_ctx,
                                  enum pipe_auth_type auth_type,
-                                 enum pipe_auth_level auth_level,
+                                 enum dcerpc_AuthLevel auth_level,
                                  const char *domain,
                                  const char *username,
                                  const char *password,
@@ -3597,9 +3145,9 @@ NTSTATUS rpccli_ntlmssp_bind_data(TALLOC_CTX *mem_ctx,
        result->a_u.ntlmssp_state->neg_flags &=
                ~(NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_SEAL);
 
-       if (auth_level == PIPE_AUTH_LEVEL_INTEGRITY) {
+       if (auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
                result->a_u.ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
-       } else if (auth_level == PIPE_AUTH_LEVEL_PRIVACY) {
+       } else if (auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
                result->a_u.ntlmssp_state->neg_flags
                        |= NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN;
        }
@@ -3613,8 +3161,8 @@ NTSTATUS rpccli_ntlmssp_bind_data(TALLOC_CTX *mem_ctx,
 }
 
 NTSTATUS rpccli_schannel_bind_data(TALLOC_CTX *mem_ctx, const char *domain,
-                                  enum pipe_auth_level auth_level,
-                                  const uint8_t sess_key[16],
+                                  enum dcerpc_AuthLevel auth_level,
+                                  struct netlogon_creds_CredentialState *creds,
                                   struct cli_pipe_auth_data **presult)
 {
        struct cli_pipe_auth_data *result;
@@ -3633,15 +3181,15 @@ NTSTATUS rpccli_schannel_bind_data(TALLOC_CTX *mem_ctx, const char *domain,
                goto fail;
        }
 
-       result->a_u.schannel_auth = talloc(result,
-                                          struct schannel_auth_struct);
+       result->a_u.schannel_auth = talloc(result, struct schannel_state);
        if (result->a_u.schannel_auth == NULL) {
                goto fail;
        }
 
-       memcpy(result->a_u.schannel_auth->sess_key, sess_key,
-              sizeof(result->a_u.schannel_auth->sess_key));
+       result->a_u.schannel_auth->state = SCHANNEL_STATE_START;
        result->a_u.schannel_auth->seq_num = 0;
+       result->a_u.schannel_auth->initiator = true;
+       result->a_u.schannel_auth->creds = creds;
 
        *presult = result;
        return NT_STATUS_OK;
@@ -3660,7 +3208,7 @@ static int cli_auth_kerberos_data_destructor(struct kerberos_auth_struct *auth)
 #endif
 
 NTSTATUS rpccli_kerberos_bind_data(TALLOC_CTX *mem_ctx,
-                                  enum pipe_auth_level auth_level,
+                                  enum dcerpc_AuthLevel auth_level,
                                   const char *service_princ,
                                   const char *username,
                                   const char *password,
@@ -3718,12 +3266,6 @@ NTSTATUS rpccli_kerberos_bind_data(TALLOC_CTX *mem_ctx,
 #endif
 }
 
-static int rpc_pipe_sock_destructor(struct rpc_pipe_client *p)
-{
-       close(p->trans.sock.fd);
-       return 0;
-}
-
 /**
  * Create an rpc pipe client struct, connecting to a tcp port.
  */
@@ -3735,16 +3277,18 @@ static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
        struct rpc_pipe_client *result;
        struct sockaddr_storage addr;
        NTSTATUS status;
+       int fd;
 
        result = TALLOC_ZERO_P(mem_ctx, struct rpc_pipe_client);
        if (result == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       result->transport_type = NCACN_IP_TCP;
-
        result->abstract_syntax = *abstract_syntax;
        result->transfer_syntax = ndr_transfer_syntax;
+       result->dispatch = cli_do_rpc_ndr;
+       result->dispatch_send = cli_do_rpc_ndr_send;
+       result->dispatch_recv = cli_do_rpc_ndr_recv;
 
        result->desthost = talloc_strdup(result, host);
        result->srv_name_slash = talloc_asprintf_strupper_m(
@@ -3757,17 +3301,24 @@ static NTSTATUS rpc_pipe_open_tcp_port(TALLOC_CTX *mem_ctx, const char *host,
        result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
        result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
 
-       if (!resolve_name(host, &addr, 0)) {
+       if (!resolve_name(host, &addr, 0, false)) {
                status = NT_STATUS_NOT_FOUND;
                goto fail;
        }
 
-       status = open_socket_out(&addr, port, 60, &result->trans.sock.fd);
+       status = open_socket_out(&addr, port, 60, &fd);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto fail;
+       }
+       set_socket_options(fd, lp_socket_options());
+
+       status = rpc_transport_sock_init(result, fd, &result->transport);
        if (!NT_STATUS_IS_OK(status)) {
+               close(fd);
                goto fail;
        }
 
-       talloc_set_destructor(result, rpc_pipe_sock_destructor);
+       result->transport->transport = NCACN_IP_TCP;
 
        *presult = result;
        return NT_STATUS_OK;
@@ -3942,18 +3493,20 @@ NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
        struct rpc_pipe_client *result;
        struct sockaddr_un addr;
        NTSTATUS status;
+       int fd;
 
        result = talloc_zero(mem_ctx, struct rpc_pipe_client);
        if (result == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       result->transport_type = NCACN_UNIX_STREAM;
-
        result->abstract_syntax = *abstract_syntax;
        result->transfer_syntax = ndr_transfer_syntax;
+       result->dispatch = cli_do_rpc_ndr;
+       result->dispatch_send = cli_do_rpc_ndr_send;
+       result->dispatch_recv = cli_do_rpc_ndr_recv;
 
-       result->desthost = talloc_get_myname(result);
+       result->desthost = get_myname(result);
        result->srv_name_slash = talloc_asprintf_strupper_m(
                result, "\\\\%s", result->desthost);
        if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
@@ -3964,26 +3517,31 @@ NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
        result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
        result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
 
-       result->trans.sock.fd = socket(AF_UNIX, SOCK_STREAM, 0);
-       if (result->trans.sock.fd == -1) {
+       fd = socket(AF_UNIX, SOCK_STREAM, 0);
+       if (fd == -1) {
                status = map_nt_error_from_unix(errno);
                goto fail;
        }
 
-       talloc_set_destructor(result, rpc_pipe_sock_destructor);
-
        ZERO_STRUCT(addr);
        addr.sun_family = AF_UNIX;
        strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
 
-       if (sys_connect(result->trans.sock.fd,
-                       (struct sockaddr *)&addr) == -1) {
+       if (sys_connect(fd, (struct sockaddr *)(void *)&addr) == -1) {
                DEBUG(0, ("connect(%s) failed: %s\n", socket_path,
                          strerror(errno)));
-               close(result->trans.sock.fd);
+               close(fd);
                return map_nt_error_from_unix(errno);
        }
 
+       status = rpc_transport_sock_init(result, fd, &result->transport);
+       if (!NT_STATUS_IS_OK(status)) {
+               close(fd);
+               goto fail;
+       }
+
+       result->transport->transport = NCALRPC;
+
        *presult = result;
        return NT_STATUS_OK;
 
@@ -3992,6 +3550,16 @@ NTSTATUS rpc_pipe_open_ncalrpc(TALLOC_CTX *mem_ctx, const char *socket_path,
        return status;
 }
 
+static int rpc_pipe_client_np_destructor(struct rpc_pipe_client *p)
+{
+       struct cli_state *cli;
+
+       cli = rpc_pipe_np_smb_conn(p);
+       if (cli != NULL) {
+               DLIST_REMOVE(cli->pipe_list, p);
+       }
+       return 0;
+}
 
 /****************************************************************************
  Open a named pipe over SMB to a remote server.
@@ -4011,7 +3579,7 @@ static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
                                 struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
-       int fnum;
+       NTSTATUS status;
 
        /* sanity check to protect against crashes */
 
@@ -4024,19 +3592,11 @@ static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
                return NT_STATUS_NO_MEMORY;
        }
 
-       result->transport_type = NCACN_NP;
-
-       result->trans.np.pipe_name = cli_get_pipe_name_from_iface(
-               result, cli, abstract_syntax);
-       if (result->trans.np.pipe_name == NULL) {
-               DEBUG(1, ("Could not find pipe for interface\n"));
-               TALLOC_FREE(result);
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       result->trans.np.cli = cli;
        result->abstract_syntax = *abstract_syntax;
        result->transfer_syntax = ndr_transfer_syntax;
+       result->dispatch = cli_do_rpc_ndr;
+       result->dispatch_send = cli_do_rpc_ndr_send;
+       result->dispatch_recv = cli_do_rpc_ndr_recv;
        result->desthost = talloc_strdup(result, cli->desthost);
        result->srv_name_slash = talloc_asprintf_strupper_m(
                result, "\\\\%s", result->desthost);
@@ -4049,21 +3609,76 @@ static NTSTATUS rpc_pipe_open_np(struct cli_state *cli,
                return NT_STATUS_NO_MEMORY;
        }
 
-       fnum = cli_nt_create(cli, result->trans.np.pipe_name,
-                            DESIRED_ACCESS_PIPE);
-       if (fnum == -1) {
-               DEBUG(3,("rpc_pipe_open_np: cli_nt_create failed on pipe %s "
-                        "to machine %s.  Error was %s\n",
-                        result->trans.np.pipe_name, cli->desthost,
-                        cli_errstr(cli)));
+       status = rpc_transport_np_init(result, cli, abstract_syntax,
+                                      &result->transport);
+       if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(result);
-               return cli_get_nt_error(cli);
+               return status;
        }
 
-       result->trans.np.fnum = fnum;
+       result->transport->transport = NCACN_NP;
 
        DLIST_ADD(cli->pipe_list, result);
-       talloc_set_destructor(result, rpc_pipe_destructor);
+       talloc_set_destructor(result, rpc_pipe_client_np_destructor);
+
+       *presult = result;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS rpc_pipe_open_local(TALLOC_CTX *mem_ctx,
+                            struct rpc_cli_smbd_conn *conn,
+                            const struct ndr_syntax_id *syntax,
+                            struct rpc_pipe_client **presult)
+{
+       struct rpc_pipe_client *result;
+       struct cli_pipe_auth_data *auth;
+       NTSTATUS status;
+
+       result = talloc(mem_ctx, struct rpc_pipe_client);
+       if (result == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       result->abstract_syntax = *syntax;
+       result->transfer_syntax = ndr_transfer_syntax;
+       result->dispatch = cli_do_rpc_ndr;
+       result->dispatch_send = cli_do_rpc_ndr_send;
+       result->dispatch_recv = cli_do_rpc_ndr_recv;
+       result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
+       result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
+
+       result->desthost = talloc_strdup(result, global_myname());
+       result->srv_name_slash = talloc_asprintf_strupper_m(
+               result, "\\\\%s", global_myname());
+       if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
+               TALLOC_FREE(result);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       status = rpc_transport_smbd_init(result, conn, syntax,
+                                        &result->transport);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("rpc_transport_smbd_init failed: %s\n",
+                         nt_errstr(status)));
+               TALLOC_FREE(result);
+               return status;
+       }
+
+       status = rpccli_anon_bind_data(result, &auth);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("rpccli_anon_bind_data failed: %s\n",
+                         nt_errstr(status)));
+               TALLOC_FREE(result);
+               return status;
+       }
+
+       status = rpc_pipe_bind(result, auth);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("rpc_pipe_bind failed: %s\n", nt_errstr(status)));
+               TALLOC_FREE(result);
+               return status;
+       }
+
+       result->transport->transport = NCACN_INTERNAL;
 
        *presult = result;
        return NT_STATUS_OK;
@@ -4074,34 +3689,35 @@ 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,
                                  struct rpc_pipe_client **presult)
 {
-       if (ndr_syntax_id_equal(interface, &ndr_table_drsuapi.syntax_id)) {
-               /*
-                * We should have a better way to figure out this drsuapi
-                * speciality...
-                */
+       switch (transport) {
+       case NCACN_IP_TCP:
                return rpc_pipe_open_tcp(NULL, cli->desthost, interface,
                                         presult);
+       case NCACN_NP:
+               return rpc_pipe_open_np(cli, interface, presult);
+       default:
+               return NT_STATUS_NOT_IMPLEMENTED;
        }
-
-       return rpc_pipe_open_np(cli, interface, presult);
 }
 
 /****************************************************************************
  Open a named pipe to an SMB server and bind anonymously.
  ****************************************************************************/
 
-NTSTATUS cli_rpc_pipe_open_noauth(struct cli_state *cli,
-                                 const struct ndr_syntax_id *interface,
-                                 struct rpc_pipe_client **presult)
+NTSTATUS cli_rpc_pipe_open_noauth_transport(struct cli_state *cli,
+                                           enum dcerpc_transport_t transport,
+                                           const struct ndr_syntax_id *interface,
+                                           struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
        struct cli_pipe_auth_data *auth;
        NTSTATUS status;
 
-       status = cli_rpc_pipe_open(cli, interface, &result);
+       status = cli_rpc_pipe_open(cli, transport, interface, &result);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -4145,29 +3761,41 @@ NTSTATUS cli_rpc_pipe_open_noauth(struct cli_state *cli,
                }
                DEBUG(lvl, ("cli_rpc_pipe_open_noauth: rpc_pipe_bind for pipe "
                            "%s failed with error %s\n",
-                           cli_get_pipe_name_from_iface(debug_ctx(), cli,
-                                                        interface),
+                           get_pipe_name_from_syntax(talloc_tos(), interface),
                            nt_errstr(status) ));
                TALLOC_FREE(result);
                return status;
        }
 
        DEBUG(10,("cli_rpc_pipe_open_noauth: opened pipe %s to machine "
-                 "%s and bound anonymously.\n", result->trans.np.pipe_name,
-                 cli->desthost ));
+                 "%s and bound anonymously.\n",
+                 get_pipe_name_from_syntax(talloc_tos(), interface),
+                 cli->desthost));
 
        *presult = result;
        return NT_STATUS_OK;
 }
 
+/****************************************************************************
+ ****************************************************************************/
+
+NTSTATUS cli_rpc_pipe_open_noauth(struct cli_state *cli,
+                                 const struct ndr_syntax_id *interface,
+                                 struct rpc_pipe_client **presult)
+{
+       return cli_rpc_pipe_open_noauth_transport(cli, NCACN_NP,
+                                                 interface, presult);
+}
+
 /****************************************************************************
  Open a named pipe to an SMB server and bind using NTLMSSP or SPNEGO NTLMSSP
  ****************************************************************************/
 
 static NTSTATUS cli_rpc_pipe_open_ntlmssp_internal(struct cli_state *cli,
                                                   const struct ndr_syntax_id *interface,
+                                                  enum dcerpc_transport_t transport,
                                                   enum pipe_auth_type auth_type,
-                                                  enum pipe_auth_level auth_level,
+                                                  enum dcerpc_AuthLevel auth_level,
                                                   const char *domain,
                                                   const char *username,
                                                   const char *password,
@@ -4177,14 +3805,14 @@ static NTSTATUS cli_rpc_pipe_open_ntlmssp_internal(struct cli_state *cli,
        struct cli_pipe_auth_data *auth;
        NTSTATUS status;
 
-       status = cli_rpc_pipe_open(cli, interface, &result);
+       status = cli_rpc_pipe_open(cli, transport, interface, &result);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
        status = rpccli_ntlmssp_bind_data(
                result, auth_type, auth_level, domain, username,
-               cli->pwd.null_pwd ? NULL : password, &auth);
+               password, &auth);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("rpccli_ntlmssp_bind_data returned %s\n",
                          nt_errstr(status)));
@@ -4200,8 +3828,8 @@ static NTSTATUS cli_rpc_pipe_open_ntlmssp_internal(struct cli_state *cli,
 
        DEBUG(10,("cli_rpc_pipe_open_ntlmssp_internal: opened pipe %s to "
                "machine %s and bound NTLMSSP as user %s\\%s.\n",
-               result->trans.np.pipe_name, cli->desthost,
-               domain, username ));
+                 get_pipe_name_from_syntax(talloc_tos(), interface),
+                 cli->desthost, domain, username ));
 
        *presult = result;
        return NT_STATUS_OK;
@@ -4219,7 +3847,8 @@ static NTSTATUS cli_rpc_pipe_open_ntlmssp_internal(struct cli_state *cli,
 
 NTSTATUS cli_rpc_pipe_open_ntlmssp(struct cli_state *cli,
                                   const struct ndr_syntax_id *interface,
-                                  enum pipe_auth_level auth_level,
+                                  enum dcerpc_transport_t transport,
+                                  enum dcerpc_AuthLevel auth_level,
                                   const char *domain,
                                   const char *username,
                                   const char *password,
@@ -4227,6 +3856,7 @@ NTSTATUS cli_rpc_pipe_open_ntlmssp(struct cli_state *cli,
 {
        return cli_rpc_pipe_open_ntlmssp_internal(cli,
                                                interface,
+                                               transport,
                                                PIPE_AUTH_TYPE_NTLMSSP,
                                                auth_level,
                                                domain,
@@ -4242,7 +3872,8 @@ NTSTATUS cli_rpc_pipe_open_ntlmssp(struct cli_state *cli,
 
 NTSTATUS cli_rpc_pipe_open_spnego_ntlmssp(struct cli_state *cli,
                                          const struct ndr_syntax_id *interface,
-                                         enum pipe_auth_level auth_level,
+                                         enum dcerpc_transport_t transport,
+                                         enum dcerpc_AuthLevel auth_level,
                                          const char *domain,
                                          const char *username,
                                          const char *password,
@@ -4250,6 +3881,7 @@ NTSTATUS cli_rpc_pipe_open_spnego_ntlmssp(struct cli_state *cli,
 {
        return cli_rpc_pipe_open_ntlmssp_internal(cli,
                                                interface,
+                                               transport,
                                                PIPE_AUTH_TYPE_SPNEGO_NTLMSSP,
                                                auth_level,
                                                domain,
@@ -4266,7 +3898,7 @@ static NTSTATUS get_schannel_session_key_common(struct rpc_pipe_client *netlogon
                                                const char *domain,
                                                uint32 *pneg_flags)
 {
-       uint32 sec_chan_type = 0;
+       enum netr_SchannelType sec_chan_type = 0;
        unsigned char machine_pwd[16];
        const char *machine_account;
        NTSTATUS status;
@@ -4343,26 +3975,29 @@ NTSTATUS get_schannel_session_key(struct cli_state *cli,
  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 pipe_auth_level auth_level,
+                                            enum dcerpc_transport_t transport,
+                                            enum dcerpc_AuthLevel auth_level,
                                             const char *domain,
-                                            const struct dcinfo *pdc,
+                                            struct netlogon_creds_CredentialState **pdc,
                                             struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
        struct cli_pipe_auth_data *auth;
        NTSTATUS status;
 
-       status = cli_rpc_pipe_open(cli, interface, &result);
+       status = cli_rpc_pipe_open(cli, transport, interface, &result);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
        status = rpccli_schannel_bind_data(result, domain, auth_level,
-                                          pdc->sess_key, &auth);
+                                          *pdc, &auth);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("rpccli_schannel_bind_data returned %s\n",
                          nt_errstr(status)));
@@ -4381,19 +4016,14 @@ NTSTATUS cli_rpc_pipe_open_schannel_with_key(struct cli_state *cli,
 
        /*
         * The credentials on a new netlogon pipe are the ones we are passed
-        * in - copy them over.
+        * in - reference them in
         */
-       result->dc = (struct dcinfo *)talloc_memdup(result, pdc, sizeof(*pdc));
-       if (result->dc == NULL) {
-               DEBUG(0, ("talloc failed\n"));
-               TALLOC_FREE(result);
-               return NT_STATUS_NO_MEMORY;
-       }
+       result->dc = talloc_move(result, pdc);
 
        DEBUG(10,("cli_rpc_pipe_open_schannel_with_key: opened pipe %s to machine %s "
-               "for domain %s "
-               "and bound using schannel.\n",
-               result->trans.np.pipe_name, cli->desthost, domain ));
+                 "for domain %s and bound using schannel.\n",
+                 get_pipe_name_from_syntax(talloc_tos(), interface),
+                 cli->desthost, domain ));
 
        *presult = result;
        return NT_STATUS_OK;
@@ -4416,7 +4046,8 @@ static NTSTATUS get_schannel_session_key_auth_ntlmssp(struct cli_state *cli,
        NTSTATUS status;
 
        status = cli_rpc_pipe_open_spnego_ntlmssp(
-               cli, &ndr_table_netlogon.syntax_id, PIPE_AUTH_LEVEL_PRIVACY,
+               cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
+               DCERPC_AUTH_LEVEL_PRIVACY,
                domain, username, password, &netlogon_pipe);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -4441,7 +4072,8 @@ static NTSTATUS get_schannel_session_key_auth_ntlmssp(struct cli_state *cli,
 
 NTSTATUS cli_rpc_pipe_open_ntlmssp_auth_schannel(struct cli_state *cli,
                                                 const struct ndr_syntax_id *interface,
-                                                enum pipe_auth_level auth_level,
+                                                enum dcerpc_transport_t transport,
+                                                enum dcerpc_AuthLevel auth_level,
                                                 const char *domain,
                                                 const char *username,
                                                 const char *password,
@@ -4462,7 +4094,7 @@ NTSTATUS cli_rpc_pipe_open_ntlmssp_auth_schannel(struct cli_state *cli,
        }
 
        status = cli_rpc_pipe_open_schannel_with_key(
-               cli, interface, auth_level, domain, netlogon_pipe->dc,
+               cli, interface, transport, auth_level, domain, &netlogon_pipe->dc,
                &result);
 
        /* Now we've bound using the session key we can close the netlog pipe. */
@@ -4481,7 +4113,8 @@ NTSTATUS cli_rpc_pipe_open_ntlmssp_auth_schannel(struct cli_state *cli,
 
 NTSTATUS cli_rpc_pipe_open_schannel(struct cli_state *cli,
                                    const struct ndr_syntax_id *interface,
-                                   enum pipe_auth_level auth_level,
+                                   enum dcerpc_transport_t transport,
+                                   enum dcerpc_AuthLevel auth_level,
                                    const char *domain,
                                    struct rpc_pipe_client **presult)
 {
@@ -4490,6 +4123,8 @@ NTSTATUS cli_rpc_pipe_open_schannel(struct cli_state *cli,
        struct rpc_pipe_client *result = NULL;
        NTSTATUS status;
 
+       *presult = NULL;
+
        status = get_schannel_session_key(cli, domain, &neg_flags,
                                          &netlogon_pipe);
        if (!NT_STATUS_IS_OK(status)) {
@@ -4500,7 +4135,7 @@ NTSTATUS cli_rpc_pipe_open_schannel(struct cli_state *cli,
        }
 
        status = cli_rpc_pipe_open_schannel_with_key(
-               cli, interface, auth_level, domain, netlogon_pipe->dc,
+               cli, interface, transport, auth_level, domain, &netlogon_pipe->dc,
                &result);
 
        /* Now we've bound using the session key we can close the netlog pipe. */
@@ -4510,7 +4145,7 @@ NTSTATUS cli_rpc_pipe_open_schannel(struct cli_state *cli,
                *presult = result;
        }
 
-       return NT_STATUS_OK;
+       return status;
 }
 
 /****************************************************************************
@@ -4521,7 +4156,7 @@ NTSTATUS cli_rpc_pipe_open_schannel(struct cli_state *cli,
 
 NTSTATUS cli_rpc_pipe_open_krb5(struct cli_state *cli,
                                const struct ndr_syntax_id *interface,
-                               enum pipe_auth_level auth_level,
+                               enum dcerpc_AuthLevel auth_level,
                                const char *service_princ,
                                const char *username,
                                const char *password,
@@ -4532,7 +4167,7 @@ NTSTATUS cli_rpc_pipe_open_krb5(struct cli_state *cli,
        struct cli_pipe_auth_data *auth;
        NTSTATUS status;
 
-       status = cli_rpc_pipe_open(cli, interface, &result);
+       status = cli_rpc_pipe_open(cli, NCACN_NP, interface, &result);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -4577,7 +4212,7 @@ NTSTATUS cli_get_session_key(TALLOC_CTX *mem_ctx,
        switch (cli->auth->auth_type) {
                case PIPE_AUTH_TYPE_SCHANNEL:
                        *session_key = data_blob_talloc(mem_ctx,
-                               cli->auth->a_u.schannel_auth->sess_key, 16);
+                               cli->auth->a_u.schannel_auth->creds->session_key, 16);
                        break;
                case PIPE_AUTH_TYPE_NTLMSSP:
                case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP: