s3:rpc_client: pass object and table to rpccli_bh_create()
[mat/samba.git] / source3 / rpc_server / rpc_ncacn_np.c
index 60cd9a37e75f03cd2e2544a7ddbd20b38afeb57d..c58f97dec9879fe5131c6d792df993c0a82f5b10 100644 (file)
@@ -5,6 +5,7 @@
  *  Largely re-written : 2005
  *  Copyright (C) Jeremy Allison               1998 - 2005
  *  Copyright (C) Simo Sorce                   2010
+ *  Copyright (C) Andrew Bartlett              2011
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  */
 
 #include "includes.h"
+#include "rpc_client/cli_pipe.h"
 #include "rpc_server/srv_pipe_internal.h"
 #include "rpc_dce.h"
 #include "../libcli/named_pipe_auth/npa_tstream.h"
 #include "rpc_server/rpc_ncacn_np.h"
 #include "librpc/gen_ndr/netlogon.h"
+#include "librpc/gen_ndr/auth.h"
+#include "../auth/auth_sam_reply.h"
+#include "auth.h"
+#include "rpc_server/rpc_pipes.h"
+#include "../lib/tsocket/tsocket.h"
+#include "../lib/util/tevent_ntstatus.h"
+#include "rpc_contexts.h"
+#include "rpc_server/rpc_config.h"
+#include "librpc/ndr/ndr_table.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_SRV
 
-static int pipes_open;
-
-static struct pipes_struct *InternalPipes;
-
-/* TODO
- * the following prototypes are declared here to avoid
- * code being moved about too much for a patch to be
- * disrupted / less obvious.
- *
- * these functions, and associated functions that they
- * call, should be moved behind a .so module-loading
- * system _anyway_.  so that's the next step...
- */
-
-/****************************************************************************
- Internal Pipe iterator functions.
-****************************************************************************/
-
-struct pipes_struct *get_first_internal_pipe(void)
-{
-       return InternalPipes;
-}
-
-struct pipes_struct *get_next_internal_pipe(struct pipes_struct *p)
-{
-       return p->next;
-}
-
-static void free_pipe_rpc_context_internal( PIPE_RPC_FNS *list )
-{
-       PIPE_RPC_FNS *tmp = list;
-       PIPE_RPC_FNS *tmp2;
-
-       while (tmp) {
-               tmp2 = tmp->next;
-               SAFE_FREE(tmp);
-               tmp = tmp2;
-       }
-
-       return;
-}
-
-bool check_open_pipes(void)
-{
-       struct pipes_struct *p;
-
-       for (p = InternalPipes; p != NULL; p = p->next) {
-               if (num_pipe_handles(p) != 0) {
-                       return true;
-               }
-       }
-       return false;
-}
-
-/****************************************************************************
- Close an rpc pipe.
-****************************************************************************/
-
-int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
-{
-       if (!p) {
-               DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
-               return False;
-       }
-
-       TALLOC_FREE(p->auth.auth_ctx);
-
-       free_pipe_rpc_context_internal( p->contexts );
-
-       /* Free the handles database. */
-       close_policy_by_pipe(p);
-
-       DLIST_REMOVE(InternalPipes, p);
-
-       ZERO_STRUCTP(p);
-
-       return 0;
-}
-
 /****************************************************************************
  Make an internal namedpipes structure
 ****************************************************************************/
 
 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
                                              const struct ndr_syntax_id *syntax,
-                                             struct client_address *client_id,
-                                             const struct auth_serversupplied_info *server_info,
+                                             const struct tsocket_address *remote_address,
+                                             const struct auth_session_info *session_info,
                                              struct messaging_context *msg_ctx)
 {
        struct pipes_struct *p;
+       struct pipe_rpc_fns *context_fns;
+       const char *pipe_name;
+       int ret;
+       const struct ndr_interface_table *table;
 
-       DEBUG(4,("Create pipe requested %s\n",
-                get_pipe_name_from_syntax(talloc_tos(), syntax)));
-
-       p = TALLOC_ZERO_P(mem_ctx, struct pipes_struct);
-
-       if (!p) {
-               DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
+       table = ndr_table_by_uuid(&syntax->uuid);
+       if (table == NULL) {
+               DEBUG(0,("unknown interface\n"));
                return NULL;
        }
 
-       p->mem_ctx = talloc_named(p, 0, "pipe %s %p",
-                                get_pipe_name_from_syntax(talloc_tos(),
-                                                          syntax), p);
-       if (p->mem_ctx == NULL) {
-               DEBUG(0,("open_rpc_pipe_p: talloc_init failed.\n"));
-               TALLOC_FREE(p);
+       pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
+
+       DEBUG(4,("Create pipe requested %s\n", pipe_name));
+
+       ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
+                                    NCALRPC, RPC_LITTLE_ENDIAN, false,
+                                    remote_address, NULL, &p);
+       if (ret) {
+               DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
                return NULL;
        }
 
@@ -146,28 +81,31 @@ struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       p->server_info = copy_serverinfo(p, server_info);
-       if (p->server_info == NULL) {
+       p->session_info = copy_session_info(p, session_info);
+       if (p->session_info == NULL) {
                DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
                close_policy_by_pipe(p);
                TALLOC_FREE(p);
                return NULL;
        }
 
-       p->msg_ctx = msg_ctx;
-
-       DLIST_ADD(InternalPipes, p);
-
-       p->client_id = client_id;
-
-       p->endian = RPC_LITTLE_ENDIAN;
+       context_fns = talloc(p, struct pipe_rpc_fns);
+       if (context_fns == NULL) {
+               DEBUG(0,("talloc() failed!\n"));
+               TALLOC_FREE(p);
+               return NULL;
+       }
 
-       p->syntax = *syntax;
+       context_fns->next = context_fns->prev = NULL;
+       context_fns->n_cmds = rpc_srv_get_pipe_num_cmds(syntax);
+       context_fns->cmds = rpc_srv_get_pipe_cmds(syntax);
+       context_fns->context_id = 0;
+       context_fns->syntax = *syntax;
 
-       DEBUG(4,("Created internal pipe %s (pipes_open=%d)\n",
-                get_pipe_name_from_syntax(talloc_tos(), syntax), pipes_open));
+       /* add to the list of open contexts */
+       DLIST_ADD(p->contexts, context_fns);
 
-       talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
+       DEBUG(4,("Created internal pipe %s\n", pipe_name));
 
        return p;
 }
@@ -178,8 +116,9 @@ static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
                                const DATA_BLOB *in_data,
                                DATA_BLOB *out_data)
 {
-       uint32_t num_cmds = rpc_srv_get_pipe_num_cmds(&p->syntax);
-       const struct api_struct *cmds = rpc_srv_get_pipe_cmds(&p->syntax);
+       struct pipe_rpc_fns *fns = find_pipe_fns_by_context(p->contexts, 0);
+       uint32_t num_cmds = fns->n_cmds;
+       const struct api_struct *cmds = fns->cmds;
        uint32_t i;
        bool ok;
 
@@ -208,24 +147,13 @@ static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
        }
 
        if (p->fault_state) {
-               p->fault_state = false;
-               data_blob_free(&p->out_data.rdata);
-               talloc_free_children(p->mem_ctx);
-               return NT_STATUS_RPC_CALL_FAILED;
-       }
-
-       if (p->bad_handle_fault_state) {
-               p->bad_handle_fault_state = false;
-               data_blob_free(&p->out_data.rdata);
-               talloc_free_children(p->mem_ctx);
-               return NT_STATUS_RPC_SS_CONTEXT_MISMATCH;
-       }
+               NTSTATUS status;
 
-       if (p->rng_fault_state) {
-               p->rng_fault_state = false;
+               status = NT_STATUS(p->fault_state);
+               p->fault_state = 0;
                data_blob_free(&p->out_data.rdata);
                talloc_free_children(p->mem_ctx);
-               return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
+               return status;
        }
 
        *out_data = p->out_data.rdata;
@@ -292,7 +220,7 @@ static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
 
        ok = rpcint_bh_is_connected(h);
        if (!ok) {
-               tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
+               tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
                return tevent_req_post(req, ev);
        }
 
@@ -354,7 +282,7 @@ static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
 
        ok = rpcint_bh_is_connected(h);
        if (!ok) {
-               tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
+               tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
                return tevent_req_post(req, ev);
        }
 
@@ -428,8 +356,8 @@ static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
                        const struct ndr_syntax_id *abstract_syntax,
                        const struct ndr_interface_table *ndr_table,
-                       struct client_address *client_id,
-                       const struct auth_serversupplied_info *server_info,
+                       const struct tsocket_address *remote_address,
+                       const struct auth_session_info *session_info,
                        struct messaging_context *msg_ctx,
                        struct dcerpc_binding_handle **binding_handle)
 {
@@ -452,8 +380,8 @@ static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
        }
        hs->p = make_internal_rpc_pipe_p(hs,
                                         abstract_syntax,
-                                        client_id,
-                                        server_info,
+                                        remote_address,
+                                        session_info,
                                         msg_ctx);
        if (hs->p == NULL) {
                TALLOC_FREE(h);
@@ -470,7 +398,7 @@ static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
  *
  * @param[in]  ndr_table Normally the ndr_table_<name>.
  *
- * @param[in]  client_id The info about the connected client.
+ * @param[in]  remote_address The info about the connected client.
  *
  * @param[in]  serversupplied_info The server supplied authentication function.
  *
@@ -488,75 +416,81 @@ static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
  *
  *   status = rpcint_binding_handle(tmp_ctx,
  *                                  &ndr_table_winreg,
- *                                  p->client_id,
- *                                  p->server_info,
+ *                                  p->remote_address,
+ *                                  p->session_info,
  *                                  p->msg_ctx
  *                                  &winreg_binding);
  * @endcode
  */
 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
                               const struct ndr_interface_table *ndr_table,
-                              struct client_address *client_id,
-                              const struct auth_serversupplied_info *server_info,
+                              const struct tsocket_address *remote_address,
+                              const struct auth_session_info *session_info,
                               struct messaging_context *msg_ctx,
                               struct dcerpc_binding_handle **binding_handle)
 {
-       return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, client_id,
-                                       server_info, msg_ctx, binding_handle);
+       return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, remote_address,
+                                       session_info, msg_ctx, binding_handle);
 }
 
 /**
- * @brief Create a new RPC client context which uses a local dispatch function.
+ * @internal
+ *
+ * @brief Create a new RPC client context which uses a local transport.
+ *
+ * This creates a local transport. It is a shortcut to directly call the server
+ * functions and avoid marshalling.
+ * NOTE: this function should be used only by rpc_pipe_open_interface()
  *
  * @param[in]  mem_ctx  The memory context to use.
  *
  * @param[in]  abstract_syntax Normally the syntax_id of the autogenerated
  *                             ndr_table_<name>.
  *
- * @param[in]  dispatch The corresponding autogenerated dispatch function
- *                      rpc_<name>_dispatch.
- *
  * @param[in]  serversupplied_info The server supplied authentication function.
  *
+ * @param[in]  remote_address The client address information.
+ *
+ * @param[in]  msg_ctx  The messaging context to use.
+ *
  * @param[out] presult  A pointer to store the connected rpc client pipe.
  *
  * @return              NT_STATUS_OK on success, a corresponding NT status if an
  *                      error occured.
- *
- * @code
- *   struct rpc_pipe_client *winreg_pipe;
- *   NTSTATUS status;
- *
- *   status = rpc_pipe_open_internal(tmp_ctx,
- *                                   &ndr_table_winreg.syntax_id,
- *                                   rpc_winreg_dispatch,
- *                                   p->server_info,
- *                                   &winreg_pipe);
- * @endcode
  */
 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
                                const struct ndr_syntax_id *abstract_syntax,
-                               const struct auth_serversupplied_info *serversupplied_info,
-                               struct client_address *client_id,
+                               const struct auth_session_info *session_info,
+                               const struct tsocket_address *remote_address,
                                struct messaging_context *msg_ctx,
                                struct rpc_pipe_client **presult)
 {
        struct rpc_pipe_client *result;
        NTSTATUS status;
 
-       result = TALLOC_ZERO_P(mem_ctx, struct rpc_pipe_client);
+       result = talloc_zero(mem_ctx, struct rpc_pipe_client);
        if (result == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
        result->abstract_syntax = *abstract_syntax;
-       result->transfer_syntax = ndr_transfer_syntax;
+       result->transfer_syntax = ndr_transfer_syntax_ndr;
+
+       if (remote_address == NULL) {
+               struct tsocket_address *local;
+               int rc;
+
+               rc = tsocket_address_inet_from_strings(mem_ctx,
+                                                      "ip",
+                                                      "127.0.0.1",
+                                                      0,
+                                                      &local);
+               if (rc < 0) {
+                       TALLOC_FREE(result);
+                       return NT_STATUS_NO_MEMORY;
+               }
 
-       if (client_id == NULL) {
-               static struct client_address unknown;
-               strlcpy(unknown.addr, "<UNKNOWN>", sizeof(unknown.addr));
-               unknown.name = "<UNKNOWN>";
-               client_id = &unknown;
+               remote_address = local;
        }
 
        result->max_xmit_frag = -1;
@@ -565,8 +499,8 @@ NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
        status = rpcint_binding_handle_ex(result,
                                          abstract_syntax,
                                          NULL,
-                                         client_id,
-                                         serversupplied_info,
+                                         remote_address,
+                                         session_info,
                                          msg_ctx,
                                          &result->binding_handle);
        if (!NT_STATUS_IS_OK(status)) {
@@ -587,15 +521,14 @@ struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
                                const char *pipe_name,
                                const struct tsocket_address *local_address,
                                const struct tsocket_address *remote_address,
-                               const struct auth_serversupplied_info *server_info)
+                               const struct auth_session_info *session_info)
 {
        struct np_proxy_state *result;
        char *socket_np_dir;
        const char *socket_dir;
        struct tevent_context *ev;
        struct tevent_req *subreq;
-       struct netr_SamInfo3 *info3;
-       NTSTATUS status;
+       struct auth_session_info_transport *session_info_t;
        bool ok;
        int ret;
        int sys_errno;
@@ -628,7 +561,7 @@ struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
                GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
                lp_ncalrpc_dir());
        if (socket_dir == NULL) {
-               DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
+               DEBUG(0, ("external_rpc_pipe:socket_dir not set\n"));
                goto fail;
        }
        socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
@@ -637,17 +570,16 @@ struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
                goto fail;
        }
 
-       info3 = talloc_zero(talloc_tos(), struct netr_SamInfo3);
-       if (info3 == NULL) {
+       session_info_t = talloc_zero(talloc_tos(), struct auth_session_info_transport);
+       if (session_info_t == NULL) {
                DEBUG(0, ("talloc failed\n"));
                goto fail;
        }
 
-       status = serverinfo_to_SamInfo3(server_info, NULL, 0, info3);
-       if (!NT_STATUS_IS_OK(status)) {
-               TALLOC_FREE(info3);
-               DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
-                         nt_errstr(status)));
+       session_info_t->session_info = copy_session_info(session_info_t,
+                                                        session_info);
+       if (session_info_t->session_info == NULL) {
+               DEBUG(0, ("copy_session_info failed\n"));
                goto fail;
        }
 
@@ -659,15 +591,13 @@ struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
                                          NULL, /* client_name */
                                          local_address, /* server_addr */
                                          NULL, /* server_name */
-                                         info3,
-                                         server_info->user_session_key,
-                                         data_blob_null /* delegated_creds */);
+                                         session_info_t);
        if (subreq == NULL) {
                unbecome_root();
                DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
                          "user %s\\%s failed\n",
-                         socket_np_dir, pipe_name, info3->base.domain.string,
-                         info3->base.account_name.string));
+                         socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
+                         session_info_t->session_info->info->account_name));
                goto fail;
        }
        ok = tevent_req_poll(subreq, ev);
@@ -675,8 +605,8 @@ struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
        if (!ok) {
                DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
                          "failed for tstream_npa_connect: %s\n",
-                         socket_np_dir, pipe_name, info3->base.domain.string,
-                         info3->base.account_name.string,
+                         socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
+                         session_info_t->session_info->info->account_name,
                          strerror(errno)));
                goto fail;
 
@@ -689,10 +619,14 @@ struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
                                       &result->allocation_size);
        TALLOC_FREE(subreq);
        if (ret != 0) {
-               DEBUG(0, ("tstream_npa_connect_recv  to %s for pipe %s and "
+               int l = 1;
+               if (errno == ENOENT) {
+                       l = 2;
+               }
+               DEBUG(l, ("tstream_npa_connect_recv  to %s for pipe %s and "
                          "user %s\\%s failed: %s\n",
-                         socket_np_dir, pipe_name, info3->base.domain.string,
-                         info3->base.account_name.string,
+                         socket_np_dir, pipe_name, session_info_t->session_info->info->domain_name,
+                         session_info_t->session_info->info->account_name,
                          strerror(sys_errno)));
                goto fail;
        }
@@ -706,8 +640,8 @@ struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
 
 static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
                                const char *pipe_name,
-                               const struct ndr_syntax_id *abstract_syntax,
-                               const struct auth_serversupplied_info *server_info,
+                               const struct ndr_interface_table *table,
+                               const struct auth_session_info *session_info,
                                struct rpc_pipe_client **_result)
 {
        struct tsocket_address *local, *remote;
@@ -730,7 +664,7 @@ static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
        }
 
        proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
-                                               local, remote, server_info);
+                                               local, remote, session_info);
        if (!proxy_state) {
                return NT_STATUS_UNSUCCESSFUL;
        }
@@ -741,8 +675,8 @@ static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
                goto done;
        }
 
-       result->abstract_syntax = *abstract_syntax;
-       result->transfer_syntax = ndr_transfer_syntax;
+       result->abstract_syntax = table->syntax_id;
+       result->transfer_syntax = ndr_transfer_syntax_ndr;
 
        result->desthost = get_myname(result);
        result->srv_name_slash = talloc_asprintf_strupper_m(
@@ -756,14 +690,19 @@ static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
        result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
 
        status = rpc_transport_tstream_init(result,
-                                           proxy_state->npipe,
-                                           proxy_state->read_queue,
-                                           proxy_state->write_queue,
+                                           &proxy_state->npipe,
                                            &result->transport);
        if (!NT_STATUS_IS_OK(status)) {
                goto done;
        }
 
+       result->binding_handle = rpccli_bh_create(result, NULL, table);
+       if (result->binding_handle == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               DEBUG(0, ("Failed to create binding handle.\n"));
+               goto done;
+       }
+
        result->auth = talloc_zero(result, struct pipe_auth_data);
        if (!result->auth) {
                status = NT_STATUS_NO_MEMORY;
@@ -780,9 +719,10 @@ static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
 
        status = rpc_pipe_bind(result, auth);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("Failed to bind spoolss pipe.\n"));
+               DEBUG(0, ("Failed to bind external pipe.\n"));
                goto done;
        }
+
 done:
        if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(result);
@@ -793,36 +733,57 @@ done:
 }
 
 /**
- * @brief Create a new RPC client context which uses a local dispatch function.
+ * @brief Create a new RPC client context which uses a local dispatch function
+ *       or a remote transport, depending on rpc_server configuration for the
+ *       specific service.
+ *
+ * @param[in]  mem_ctx  The memory context to use.
+ *
+ * @param[in]  abstract_syntax Normally the syntax_id of the autogenerated
+ *                             ndr_table_<name>.
+ *
+ * @param[in]  serversupplied_info The server supplied authentication function.
+ *
+ * @param[in]  remote_address The client address information.
+ *
+ * @param[in]  msg_ctx  The messaging context to use.
+ *
+ * @param[out] presult  A pointer to store the connected rpc client pipe.
+ *
+ * @return              NT_STATUS_OK on success, a corresponding NT status if an
+ *                      error occured.
  *
- * @param mem_ctx      The memory context on which thje pipe will ultimately
- *                     be allocated
- * @param name         The pipe name to connect to.
- * @param server_info  Credentials to use for the connection.
- * @param pipe         [in|out] Checks if a pipe is connected, and connects it
- *                              if not
+ * @code
+ *   struct rpc_pipe_client *winreg_pipe;
+ *   NTSTATUS status;
  *
- * @return              NT_STATUS_OK on success, a corresponding NT status if
- *                     an error occured.
+ *   status = rpc_pipe_open_interface(tmp_ctx,
+ *                                    &ndr_table_winreg.syntax_id,
+ *                                    p->session_info,
+ *                                    remote_address,
+ *                                    &winreg_pipe);
+ * @endcode
  */
 
 NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
-                                const struct ndr_syntax_id *syntax,
-                                const struct auth_serversupplied_info *server_info,
-                                struct client_address *client_id,
+                                const struct ndr_interface_table *table,
+                                const struct auth_session_info *session_info,
+                                const struct tsocket_address *remote_address,
                                 struct messaging_context *msg_ctx,
                                 struct rpc_pipe_client **cli_pipe)
 {
        struct rpc_pipe_client *cli = NULL;
-       const char *server_type;
+       enum rpc_service_mode_e pipe_mode;
        const char *pipe_name;
        NTSTATUS status;
        TALLOC_CTX *tmp_ctx;
 
-       if (cli_pipe && rpccli_is_connected(*cli_pipe)) {
-               return NT_STATUS_OK;
-       } else {
-               TALLOC_FREE(*cli_pipe);
+       if (cli_pipe != NULL) {
+               if (rpccli_is_connected(*cli_pipe)) {
+                       return NT_STATUS_OK;
+               } else {
+                       TALLOC_FREE(*cli_pipe);
+               }
        }
 
        tmp_ctx = talloc_stackframe();
@@ -830,42 +791,53 @@ NTSTATUS rpc_pipe_open_interface(TALLOC_CTX *mem_ctx,
                return NT_STATUS_NO_MEMORY;
        }
 
-       pipe_name = get_pipe_name_from_syntax(tmp_ctx, syntax);
+       pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
        if (pipe_name == NULL) {
                status = NT_STATUS_INVALID_PARAMETER;
                goto done;
        }
 
-       DEBUG(10, ("Connecting to %s pipe.\n", pipe_name));
+       while (pipe_name[0] == '\\') {
+               pipe_name++;
+       }
+
+       DEBUG(5, ("Connecting to %s pipe.\n", pipe_name));
 
-       server_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
-                                          "rpc_server", pipe_name,
-                                          "embedded");
-       if (StrCaseCmp(server_type, "embedded") == 0) {
+       pipe_mode = rpc_service_mode(pipe_name);
+
+       switch (pipe_mode) {
+       case RPC_SERVICE_MODE_EMBEDDED:
                status = rpc_pipe_open_internal(tmp_ctx,
-                                               syntax, server_info,
-                                               client_id, msg_ctx,
+                                               &table->syntax_id, session_info,
+                                               remote_address, msg_ctx,
                                                &cli);
                if (!NT_STATUS_IS_OK(status)) {
                        goto done;
                }
-       } else {
+               break;
+       case RPC_SERVICE_MODE_EXTERNAL:
                /* It would be nice to just use rpc_pipe_open_ncalrpc() but
                 * for now we need to use the special proxy setup to connect
                 * to spoolssd. */
 
                status = rpc_pipe_open_external(tmp_ctx,
-                                               pipe_name, syntax,
-                                               server_info,
+                                               pipe_name, table,
+                                               session_info,
                                                &cli);
                if (!NT_STATUS_IS_OK(status)) {
                        goto done;
                }
-       }
+               break;
+       case RPC_SERVICE_MODE_DISABLED:
+               status = NT_STATUS_NOT_IMPLEMENTED;
+               DEBUG(0, ("Service pipe %s is disabled in config file: %s",
+                         pipe_name, nt_errstr(status)));
+               goto done;
+        }
 
        status = NT_STATUS_OK;
 done:
-       if (NT_STATUS_IS_OK(status)) {
+       if (NT_STATUS_IS_OK(status) && cli_pipe != NULL) {
                *cli_pipe = talloc_move(mem_ctx, &cli);
        }
        TALLOC_FREE(tmp_ctx);