s3-rpc_server: Add create_dcerpc_ncalrpc_socket().
[idra/samba.git] / source3 / rpc_server / rpc_server.c
index 44c1b104c0ba6eba8dd9c95876b0f38f9e930ff6..c995e22ea05bb03ba0575dc0864fd8481c1b37e3 100644 (file)
@@ -2,6 +2,8 @@
    Unix SMB/Netbios implementation.
    Generic infrstructure for RPC Daemons
    Copyright (C) Simo Sorce 2010
+   Copyright (C) Andrew Bartlett 2011
+   Copyright (C) Andreas Schneider 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
@@ -18,8 +20,9 @@
 */
 
 #include "includes.h"
-#include "ntdomain.h"
+#include "rpc_server/rpc_pipes.h"
 #include "rpc_server/rpc_server.h"
+#include "rpc_server/rpc_config.h"
 #include "rpc_dce.h"
 #include "librpc/gen_ndr/netlogon.h"
 #include "librpc/gen_ndr/auth.h"
 static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
                                            struct auth_session_info **session_info)
 {
-       struct auth_session_info *i;
-       struct auth_serversupplied_info *s;
-       struct auth_user_info_dc *u;
-       union netr_Validation val;
        NTSTATUS status;
 
-       i = talloc_zero(mem_ctx, struct auth_session_info);
-       if (i == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       status = make_server_info_guest(i, &s);
+       status = make_session_info_guest(mem_ctx, session_info);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       i->security_token = s->security_token;
-       i->session_key    = s->session_key;
-
-       val.sam3 = s->info3;
-
-       status = make_user_info_dc_netlogon_validation(mem_ctx,
-                                                      "",
-                                                      3,
-                                                      &val,
-                                                      &u);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("conversion of info3 into user_info_dc failed!\n"));
-               return status;
-       }
-       i->info = talloc_move(i, &u->info);
-       talloc_free(u);
-
-       *session_info = i;
-
        return NT_STATUS_OK;
 }
 
 /* Creates a pipes_struct and initializes it with the information
  * sent from the client */
 static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
+                                   struct messaging_context *msg_ctx,
                                    const char *pipe_name,
                                    enum dcerpc_transport_t transport,
                                    bool ncalrpc_as_system,
-                                   const char *client_address,
-                                   const char *server_address,
+                                   const struct tsocket_address *local_address,
+                                   const struct tsocket_address *remote_address,
                                    struct auth_session_info *session_info,
                                    struct pipes_struct **_p,
                                    int *perrno)
 {
-       struct netr_SamInfo3 *info3;
-       struct auth_user_info_dc *auth_user_info_dc;
        struct pipes_struct *p;
-       struct auth_serversupplied_info *server_info;
        NTSTATUS status;
+       int ret;
 
-       p = talloc_zero(mem_ctx, struct pipes_struct);
-       if (!p) {
-               *perrno = ENOMEM;
-               return -1;
-       }
-
-       p->transport = transport;
-       p->ncalrpc_as_system = ncalrpc_as_system;
-
-       p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
-       if (!p->mem_ctx) {
-               TALLOC_FREE(p);
-               *perrno = ENOMEM;
-               return -1;
-       }
-
-       data_blob_free(&p->in_data.data);
-       data_blob_free(&p->in_data.pdu);
-
-       p->endian = RPC_LITTLE_ENDIAN;
-
-       /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
-       auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
-       if (!auth_user_info_dc) {
-               TALLOC_FREE(p);
-               *perrno = ENOMEM;
+       ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
+                                    transport, RPC_LITTLE_ENDIAN,
+                                    ncalrpc_as_system,
+                                    remote_address, local_address, &p);
+       if (ret) {
+               *perrno = ret;
                return -1;
        }
 
-       auth_user_info_dc->num_sids = session_info->security_token->num_sids;
-       auth_user_info_dc->sids = session_info->security_token->sids;
-       auth_user_info_dc->info = session_info->info;
-       auth_user_info_dc->user_session_key = session_info->session_key;
-
-       /* This creates the input structure that make_server_info_info3 is looking for */
-       status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
-                                                   &info3);
+       if (session_info->unix_token && session_info->unix_info && session_info->security_token) {
+               /* Don't call create_local_token(), we already have the full details here */
+               p->session_info = talloc_steal(p, session_info);
 
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
-               TALLOC_FREE(p);
-               *perrno = EINVAL;
-               return -1;
-       }
-
-       status = make_server_info_info3(p,
-                                       info3->base.account_name.string,
-                                       info3->base.domain.string,
-                                       &server_info, info3);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to init server info\n"));
-               TALLOC_FREE(p);
-               *perrno = EINVAL;
-               return -1;
-       }
+       } else {
+               struct auth_user_info_dc *auth_user_info_dc;
+               struct auth_serversupplied_info *server_info;
+               struct netr_SamInfo3 *info3;
 
-       /*
-        * Some internal functions need a local token to determine access to
-        * resoutrces.
-        */
-       status = create_local_token(p, server_info, &session_info->session_key, &p->session_info);
-       talloc_free(server_info);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to init local auth token\n"));
-               TALLOC_FREE(p);
-               *perrno = EINVAL;
-               return -1;
-       }
+               /* Fake up an auth_user_info_dc for now, to make an info3, to make the session_info structure */
+               auth_user_info_dc = talloc_zero(p, struct auth_user_info_dc);
+               if (!auth_user_info_dc) {
+                       TALLOC_FREE(p);
+                       *perrno = ENOMEM;
+                       return -1;
+               }
 
-       /* Now override the session_info->security_token with the exact
-        * security_token we were given from the other side,
-        * regardless of what we just calculated */
-       p->session_info->security_token = talloc_move(p->session_info, &session_info->security_token);
+               auth_user_info_dc->num_sids = session_info->security_token->num_sids;
+               auth_user_info_dc->sids = session_info->security_token->sids;
+               auth_user_info_dc->info = session_info->info;
+               auth_user_info_dc->user_session_key = session_info->session_key;
 
-       p->client_id = talloc_zero(p, struct client_address);
-       if (!p->client_id) {
-               TALLOC_FREE(p);
-               *perrno = ENOMEM;
-               return -1;
-       }
-       strlcpy(p->client_id->addr,
-               client_address, sizeof(p->client_id->addr));
-       p->client_id->name = talloc_strdup(p->client_id, client_address);
-       if (p->client_id->name == NULL) {
-               TALLOC_FREE(p);
-               *perrno = ENOMEM;
-               return -1;
-       }
+               /* This creates the input structure that make_server_info_info3 is looking for */
+               status = auth_convert_user_info_dc_saminfo3(p, auth_user_info_dc,
+                                                           &info3);
 
-       if (server_address != NULL) {
-               p->server_id = talloc_zero(p, struct client_address);
-               if (p->client_id == NULL) {
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(1, ("Failed to convert auth_user_info_dc into netr_SamInfo3\n"));
                        TALLOC_FREE(p);
-                       *perrno = ENOMEM;
+                       *perrno = EINVAL;
                        return -1;
                }
 
-               strlcpy(p->server_id->addr,
-                       server_address,
-                       sizeof(p->server_id->addr));
+               status = make_server_info_info3(p,
+                                               info3->base.account_name.string,
+                                               info3->base.domain.string,
+                                               &server_info, info3);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(1, ("Failed to init server info\n"));
+                       TALLOC_FREE(p);
+                       *perrno = EINVAL;
+                       return -1;
+               }
 
-               p->server_id->name = talloc_strdup(p->server_id,
-                                                  server_address);
-               if (p->server_id->name == NULL) {
+               /*
+                * Some internal functions need a local token to determine access to
+                * resources.
+                */
+               status = create_local_token(p, server_info, &session_info->session_key, info3->base.account_name.string,
+                                           &p->session_info);
+               talloc_free(server_info);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(1, ("Failed to init local auth token\n"));
                        TALLOC_FREE(p);
-                       *perrno = ENOMEM;
+                       *perrno = EINVAL;
                        return -1;
                }
        }
 
-       talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
-
        *_p = p;
        return 0;
 }
@@ -231,24 +161,10 @@ static void named_pipe_listener(struct tevent_context *ev,
                                uint16_t flags,
                                void *private_data);
 
-bool setup_named_pipe_socket(const char *pipe_name,
-                            struct tevent_context *ev_ctx)
+int create_named_pipe_socket(const char *pipe_name)
 {
-       struct dcerpc_ncacn_listen_state *state;
-       struct tevent_fd *fde;
-       char *np_dir;
-
-       state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
-       if (!state) {
-               DEBUG(0, ("Out of memory\n"));
-               return false;
-       }
-       state->ep.name = talloc_strdup(state, pipe_name);
-       if (state->ep.name == NULL) {
-               DEBUG(0, ("Out of memory\n"));
-               goto out;
-       }
-       state->fd = -1;
+       char *np_dir = NULL;
+       int fd = -1;
 
        /*
         * As lp_ncalrpc_dir() should have 0755, but
@@ -261,7 +177,7 @@ bool setup_named_pipe_socket(const char *pipe_name,
                goto out;
        }
 
-       np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
+       np_dir = talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir());
        if (!np_dir) {
                DEBUG(0, ("Out of memory\n"));
                goto out;
@@ -273,13 +189,45 @@ bool setup_named_pipe_socket(const char *pipe_name,
                goto out;
        }
 
-       state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
-       if (state->fd == -1) {
+       fd = create_pipe_sock(np_dir, pipe_name, 0700);
+       if (fd == -1) {
                DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
                          np_dir, pipe_name));
                goto out;
        }
 
+       DEBUG(10, ("Openened pipe socket fd %d for %s\n", fd, pipe_name));
+
+out:
+       talloc_free(np_dir);
+       return fd;
+}
+
+bool setup_named_pipe_socket(const char *pipe_name,
+                            struct tevent_context *ev_ctx,
+                            struct messaging_context *msg_ctx)
+{
+       struct dcerpc_ncacn_listen_state *state;
+       struct tevent_fd *fde;
+
+       state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
+       if (!state) {
+               DEBUG(0, ("Out of memory\n"));
+               return false;
+       }
+       state->ep.name = talloc_strdup(state, pipe_name);
+       if (state->ep.name == NULL) {
+               DEBUG(0, ("Out of memory\n"));
+               goto out;
+       }
+       state->fd = create_named_pipe_socket(pipe_name);
+       if (state->fd == -1) {
+               goto out;
+       }
+
+       state->ev_ctx = ev_ctx;
+       state->msg_ctx = msg_ctx;
+
        DEBUG(10, ("Openened pipe socket fd %d for %s\n",
                   state->fd, pipe_name));
 
@@ -302,8 +250,6 @@ out:
        return false;
 }
 
-static void named_pipe_accept_function(const char *pipe_name, int fd);
-
 static void named_pipe_listener(struct tevent_context *ev,
                                struct tevent_fd *fde,
                                uint16_t flags,
@@ -333,7 +279,10 @@ static void named_pipe_listener(struct tevent_context *ev,
 
        DEBUG(6, ("Accepted socket %d\n", sd));
 
-       named_pipe_accept_function(state->ep.name, sd);
+       named_pipe_accept_function(state->ev_ctx,
+                                  state->msg_ctx,
+                                  state->ep.name,
+                                  sd, NULL, 0);
 }
 
 
@@ -357,6 +306,7 @@ struct named_pipe_client {
        char *client_name;
        struct tsocket_address *server;
        char *server_name;
+
        struct auth_session_info *session_info;
 
        struct pipes_struct *p;
@@ -365,26 +315,52 @@ struct named_pipe_client {
 
        struct iovec *iov;
        size_t count;
+
+       named_pipe_termination_fn *term_fn;
+       void *private_data;
 };
 
+static int named_pipe_destructor(struct named_pipe_client *npc)
+{
+       if (npc->term_fn) {
+               npc->term_fn(npc->private_data);
+       }
+       return 0;
+}
+
 static void named_pipe_accept_done(struct tevent_req *subreq);
 
-static void named_pipe_accept_function(const char *pipe_name, int fd)
+void named_pipe_accept_function(struct tevent_context *ev_ctx,
+                               struct messaging_context *msg_ctx,
+                               const char *pipe_name, int fd,
+                               named_pipe_termination_fn *term_fn,
+                               void *private_data)
 {
        struct named_pipe_client *npc;
        struct tstream_context *plain;
        struct tevent_req *subreq;
        int ret;
 
-       npc = talloc_zero(NULL, struct named_pipe_client);
+       npc = talloc_zero(ev_ctx, struct named_pipe_client);
        if (!npc) {
                DEBUG(0, ("Out of memory!\n"));
                close(fd);
                return;
        }
-       npc->pipe_name = pipe_name;
-       npc->ev = server_event_context();
-       npc->msg_ctx = server_messaging_context();
+
+       npc->pipe_name = talloc_strdup(npc, pipe_name);
+       if (npc->pipe_name == NULL) {
+               DEBUG(0, ("Out of memory!\n"));
+               TALLOC_FREE(npc);
+               close(fd);
+               return;
+       }
+       npc->ev = ev_ctx;
+       npc->msg_ctx = msg_ctx;
+       npc->term_fn = term_fn;
+       npc->private_data = private_data;
+
+       talloc_set_destructor(npc, named_pipe_destructor);
 
        /* make sure socket is in NON blocking state */
        ret = set_blocking(fd, false);
@@ -428,7 +404,6 @@ static void named_pipe_accept_done(struct tevent_req *subreq)
        struct auth_session_info_transport *session_info_transport;
        struct named_pipe_client *npc =
                tevent_req_callback_data(subreq, struct named_pipe_client);
-       const char *cli_addr;
        int error;
        int ret;
 
@@ -450,27 +425,16 @@ static void named_pipe_accept_done(struct tevent_req *subreq)
                return;
        }
 
-       if (tsocket_address_is_inet(npc->client, "ip")) {
-               cli_addr = tsocket_address_inet_addr_string(npc->client,
-                                                           subreq);
-               if (cli_addr == NULL) {
-                       TALLOC_FREE(npc);
-                       return;
-               }
-       } else {
-               cli_addr = "";
-       }
-
        ret = make_server_pipes_struct(npc,
+                                      npc->msg_ctx,
                                       npc->pipe_name, NCACN_NP,
-                                       false, cli_addr, NULL, npc->session_info,
+                                       false, npc->server, npc->client, npc->session_info,
                                        &npc->p, &error);
        if (ret != 0) {
                DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
                          strerror(error)));
                goto fail;
        }
-       npc->p->msg_ctx = npc->msg_ctx;
 
        npc->write_queue = tevent_queue_create(npc, "np_server_write_queue");
        if (!npc->write_queue) {
@@ -507,6 +471,7 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
        ssize_t data_used;
        char *data;
        uint32_t to_send;
+       size_t i;
        bool ok;
 
        status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
@@ -540,13 +505,6 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
        to_send = out->frag.length - out->current_pdu_sent;
        if (to_send > 0) {
 
-               DEBUG(10, ("Current_pdu_len = %u, "
-                          "current_pdu_sent = %u "
-                          "Returning %u bytes\n",
-                          (unsigned int)out->frag.length,
-                          (unsigned int)out->current_pdu_sent,
-                          (unsigned int)to_send));
-
                npc->iov = talloc_zero(npc, struct iovec);
                if (!npc->iov) {
                        status = NT_STATUS_NO_MEMORY;
@@ -583,11 +541,6 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
                npc->iov[npc->count].iov_base = out->frag.data;
                npc->iov[npc->count].iov_len = out->frag.length;
 
-               DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
-                          (unsigned int)npc->count,
-                          (unsigned int)npc->iov[npc->count].iov_len));
-               dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
-                               npc->iov[npc->count].iov_len);
                npc->count++;
        }
 
@@ -605,19 +558,31 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
                return;
        }
 
-       DEBUG(10, ("Sending a total of %u bytes\n",
+       DEBUG(10, ("Sending %u fragments in a total of %u bytes\n",
+                  (unsigned int)npc->count,
                   (unsigned int)npc->p->out_data.data_sent_length));
 
-       subreq = tstream_writev_queue_send(npc, npc->ev,
-                                          npc->tstream,
-                                          npc->write_queue,
-                                          npc->iov, npc->count);
-       if (!subreq) {
-               DEBUG(2, ("Failed to send packet\n"));
-               status = NT_STATUS_NO_MEMORY;
-               goto fail;
+       for (i = 0; i < npc->count; i++) {
+               DEBUG(10, ("Sending PDU number: %d, PDU Length: %u\n",
+                         (unsigned int)i,
+                         (unsigned int)npc->iov[i].iov_len));
+               dump_data(11, (const uint8_t *)npc->iov[i].iov_base,
+                               npc->iov[i].iov_len);
+
+               subreq = tstream_writev_queue_send(npc,
+                                                  npc->ev,
+                                                  npc->tstream,
+                                                  npc->write_queue,
+                                                  (npc->iov + i),
+                                                  1);
+               if (!subreq) {
+                       DEBUG(2, ("Failed to send packet\n"));
+                       status = NT_STATUS_NO_MEMORY;
+                       goto fail;
+               }
+               tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
        }
-       tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
+
        return;
 
 fail:
@@ -643,6 +608,10 @@ static void named_pipe_packet_done(struct tevent_req *subreq)
                goto fail;
        }
 
+       if (tevent_queue_length(npc->write_queue) > 0) {
+               return;
+       }
+
        /* clear out any data that may have been left around */
        npc->count = 0;
        TALLOC_FREE(npc->iov);
@@ -650,6 +619,8 @@ static void named_pipe_packet_done(struct tevent_req *subreq)
        data_blob_free(&npc->p->out_data.frag);
        data_blob_free(&npc->p->out_data.rdata);
 
+       talloc_free_children(npc->p->mem_ctx);
+
        /* Wait for the next packet */
        subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
        if (!subreq) {
@@ -669,16 +640,6 @@ fail:
        return;
 }
 
-static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
-                               struct messaging_context *msg_ctx,
-                               enum dcerpc_transport_t transport,
-                               const char *name,
-                               uint16_t port,
-                               struct tsocket_address *cli_addr,
-                               struct tsocket_address *srv_addr,
-                               int s,
-                               dcerpc_ncacn_disconnect_fn fn);
-
 /********************************************************************
  * Start listening on the tcp/ip socket
  ********************************************************************/
@@ -688,6 +649,41 @@ static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
                                        uint16_t flags,
                                        void *private_data);
 
+int create_tcpip_socket(const struct sockaddr_storage *ifss, uint16_t *port)
+{
+       int fd = -1;
+
+       if (*port == 0) {
+               uint16_t i;
+
+               for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
+                       fd = open_socket_in(SOCK_STREAM,
+                                           i,
+                                           0,
+                                           ifss,
+                                           false);
+                       if (fd > 0) {
+                               *port = i;
+                               break;
+                       }
+               }
+       } else {
+               fd = open_socket_in(SOCK_STREAM,
+                                   *port,
+                                   0,
+                                   ifss,
+                                   true);
+       }
+       if (fd == -1) {
+               DEBUG(0, ("Failed to create socket on port %u!\n", *port));
+               return -1;
+       }
+
+       DEBUG(10, ("Opened tcpip socket fd %d for port %u\n", fd, *port));
+
+       return fd;
+}
+
 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
                                         struct messaging_context *msg_ctx,
                                         const struct sockaddr_storage *ifss,
@@ -707,30 +703,8 @@ uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
        state->ep.port = port;
        state->disconnect_fn = NULL;
 
-       if (state->ep.port == 0) {
-               uint16_t i;
-
-               for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
-                       state->fd = open_socket_in(SOCK_STREAM,
-                                                  i,
-                                                  0,
-                                                  ifss,
-                                                  false);
-                       if (state->fd > 0) {
-                               state->ep.port = i;
-                               break;
-                       }
-               }
-       } else {
-               state->fd = open_socket_in(SOCK_STREAM,
-                                          state->ep.port,
-                                          0,
-                                          ifss,
-                                          true);
-       }
+       state->fd = create_tcpip_socket(ifss, &state->ep.port);
        if (state->fd == -1) {
-               DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Failed to create "
-                         "socket on port %u!\n", state->ep.port));
                goto out;
        }
 
@@ -830,7 +804,6 @@ static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
                            state->msg_ctx,
                            NCACN_IP_TCP,
                            NULL,
-                           state->ep.port,
                            cli_addr,
                            srv_addr,
                            s,
@@ -846,6 +819,32 @@ static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
                                    uint16_t flags,
                                    void *private_data);
 
+int create_dcerpc_ncalrpc_socket(const char *name)
+{
+       int fd = -1;
+
+       if (name == NULL) {
+               name = "DEFAULT";
+       }
+
+       if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
+               DEBUG(0, ("Failed to create ncalrpc directory %s - %s\n",
+                         lp_ncalrpc_dir(), strerror(errno)));
+               return -1;
+       }
+
+       fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
+       if (fd == -1) {
+               DEBUG(0, ("Failed to create ncalrpc socket! [%s/%s]\n",
+                         lp_ncalrpc_dir(), name));
+               return -1;
+       }
+
+       DEBUG(10, ("Openened ncalrpc socket fd %d for %s\n", fd, name));
+
+       return fd;
+}
+
 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
                                 struct messaging_context *msg_ctx,
                                 const char *name,
@@ -866,29 +865,19 @@ bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
        if (name == NULL) {
                name = "DEFAULT";
        }
-       state->ep.name = talloc_strdup(state, name);
 
+       state->ep.name = talloc_strdup(state, name);
        if (state->ep.name == NULL) {
                DEBUG(0, ("Out of memory\n"));
                talloc_free(state);
                return false;
        }
 
-       if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
-               DEBUG(0, ("Failed to create pipe directory %s - %s\n",
-                         lp_ncalrpc_dir(), strerror(errno)));
-               goto out;
-       }
-
-       state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
+       state->fd = create_dcerpc_ncalrpc_socket(name);
        if (state->fd == -1) {
-               DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
-                         lp_ncalrpc_dir(), name));
                goto out;
        }
 
-       DEBUG(10, ("Openened pipe socket fd %d for %s\n", state->fd, name));
-
        state->ev_ctx = ev_ctx;
        state->msg_ctx = msg_ctx;
 
@@ -956,7 +945,7 @@ static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
        dcerpc_ncacn_accept(state->ev_ctx,
                            state->msg_ctx,
                            NCALRPC,
-                           state->ep.name, 0,
+                           state->ep.name,
                            cli_addr, NULL, sd,
                            state->disconnect_fn);
 }
@@ -964,11 +953,6 @@ static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
 struct dcerpc_ncacn_conn {
        enum dcerpc_transport_t transport;
 
-       union {
-               const char *name;
-               uint16_t port;
-       } ep;
-
        int sock;
 
        struct pipes_struct *p;
@@ -993,19 +977,16 @@ struct dcerpc_ncacn_conn {
 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
 
-static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
-                               struct messaging_context *msg_ctx,
-                               enum dcerpc_transport_t transport,
-                               const char *name,
-                               uint16_t port,
-                               struct tsocket_address *cli_addr,
-                               struct tsocket_address *srv_addr,
-                               int s,
-                               dcerpc_ncacn_disconnect_fn fn) {
+void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
+                        struct messaging_context *msg_ctx,
+                        enum dcerpc_transport_t transport,
+                        const char *name,
+                        struct tsocket_address *cli_addr,
+                        struct tsocket_address *srv_addr,
+                        int s,
+                        dcerpc_ncacn_disconnect_fn fn) {
        struct dcerpc_ncacn_conn *ncacn_conn;
        struct tevent_req *subreq;
-       const char *cli_str;
-       const char *srv_str = NULL;
        bool system_user = false;
        char *pipe_name;
        NTSTATUS status;
@@ -1061,8 +1042,6 @@ static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
 
        switch (transport) {
                case NCACN_IP_TCP:
-                       ncacn_conn->ep.port = port;
-
                        pipe_name = tsocket_address_string(ncacn_conn->client,
                                                           ncacn_conn);
                        if (pipe_name == NULL) {
@@ -1082,13 +1061,6 @@ static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
                                }
                        }
                case NCACN_NP:
-                       ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
-                       if (ncacn_conn->ep.name == NULL) {
-                               close(s);
-                               talloc_free(ncacn_conn);
-                               return;
-                       }
-
                        pipe_name = talloc_strdup(ncacn_conn,
                                                  name);
                        if (pipe_name == NULL) {
@@ -1125,20 +1097,6 @@ static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
                return;
        }
 
-       if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
-               cli_str = ncacn_conn->client_name;
-       } else {
-               cli_str = "";
-       }
-
-       if (ncacn_conn->server != NULL) {
-               if (tsocket_address_is_inet(ncacn_conn->server, "ip")) {
-                       srv_str = ncacn_conn->server_name;
-               } else {
-                       srv_str = NULL;
-               }
-       }
-
        if (ncacn_conn->session_info == NULL) {
                status = auth_anonymous_session_info(ncacn_conn,
                                                     &ncacn_conn->session_info);
@@ -1152,11 +1110,12 @@ static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
        }
 
        rc = make_server_pipes_struct(ncacn_conn,
+                                     ncacn_conn->msg_ctx,
                                      pipe_name,
                                      ncacn_conn->transport,
                                      system_user,
-                                     cli_str,
-                                     srv_str,
+                                     ncacn_conn->server,
+                                     ncacn_conn->client,
                                      ncacn_conn->session_info,
                                      &ncacn_conn->p,
                                      &sys_errno);
@@ -1367,6 +1326,8 @@ static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
        data_blob_free(&ncacn_conn->p->out_data.frag);
        data_blob_free(&ncacn_conn->p->out_data.rdata);
 
+       talloc_free_children(ncacn_conn->p->mem_ctx);
+
        /* Wait for the next packet */
        subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
                                               ncacn_conn->ev_ctx,