auth: Move auth_session_info into IDL
[samba.git] / source3 / rpc_server / rpc_server.c
index 0ba945a37e783098a612183f22d2e0f1e9ad5a22..c7c77f037570e71d5942fe543fb16ca8f21a548f 100644 (file)
 #include "rpc_dce.h"
 #include "librpc/gen_ndr/netlogon.h"
 #include "librpc/gen_ndr/auth.h"
-#include "registry/reg_parse_prs.h"
 #include "lib/tsocket/tsocket.h"
 #include "libcli/named_pipe_auth/npa_tstream.h"
 #include "../auth/auth_sam_reply.h"
+#include "auth.h"
+#include "ntdomain.h"
 
 #define SERVER_TCP_LOW_PORT  1024
 #define SERVER_TCP_HIGH_PORT 1300
 
 static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
-                                           struct auth_session_info_transport **session_info)
+                                           struct auth_session_info **session_info)
 {
-       struct auth_session_info_transport *i;
+       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_transport);
+       i = talloc_zero(mem_ctx, struct auth_session_info);
        if (i == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -50,7 +51,7 @@ static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
        }
 
        i->security_token = s->security_token;
-       i->session_key    = s->user_session_key;
+       i->session_key    = s->session_key;
 
        val.sam3 = s->info3;
 
@@ -76,15 +77,18 @@ static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
 static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
                                    const char *pipe_name,
                                    const struct ndr_syntax_id id,
+                                   enum dcerpc_transport_t transport,
+                                   bool ncalrpc_as_system,
                                    const char *client_address,
                                    const char *server_address,
-                                   struct auth_session_info_transport *session_info,
+                                   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;
        bool ok;
 
@@ -94,6 +98,8 @@ static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
                return -1;
        }
        p->syntax = id;
+       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) {
@@ -143,7 +149,7 @@ static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
        status = make_server_info_info3(p,
                                        info3->base.account_name.string,
                                        info3->base.domain.string,
-                                       &p->session_info, info3);
+                                       &server_info, info3);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("Failed to init server info\n"));
                TALLOC_FREE(p);
@@ -155,7 +161,8 @@ static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
         * Some internal functions need a local token to determine access to
         * resoutrces.
         */
-       status = create_local_token(p->session_info);
+       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);
@@ -168,10 +175,6 @@ static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
         * regardless of what we just calculated */
        p->session_info->security_token = talloc_move(p->session_info, &session_info->security_token);
 
-       /* Also set the session key to the correct value */
-       p->session_info->user_session_key = session_info->session_key;
-       p->session_info->user_session_key.data = talloc_move(p->session_info, &session_info->session_key.data);
-
        p->client_id = talloc_zero(p, struct client_address);
        if (!p->client_id) {
                TALLOC_FREE(p);
@@ -214,83 +217,6 @@ static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
        return 0;
 }
 
-/* Add some helper functions to wrap the common ncacn packet reading functions
- * until we can share more dcerpc code */
-struct dcerpc_ncacn_read_packet_state {
-       struct ncacn_packet *pkt;
-       DATA_BLOB buffer;
-};
-
-static void dcerpc_ncacn_read_packet_done(struct tevent_req *subreq);
-
-static struct tevent_req *dcerpc_ncacn_read_packet_send(TALLOC_CTX *mem_ctx,
-                                       struct tevent_context *ev,
-                                       struct tstream_context *tstream)
-{
-       struct dcerpc_ncacn_read_packet_state *state;
-       struct tevent_req *req, *subreq;
-
-       req = tevent_req_create(mem_ctx, &state,
-                               struct dcerpc_ncacn_read_packet_state);
-       if (!req) {
-               return NULL;
-       }
-       ZERO_STRUCTP(state);
-
-       subreq = dcerpc_read_ncacn_packet_send(state, ev, tstream);
-       if (!subreq) {
-               tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
-               tevent_req_post(req, ev);
-               return req;
-       }
-       tevent_req_set_callback(subreq, dcerpc_ncacn_read_packet_done, req);
-
-       return req;
-}
-
-static void dcerpc_ncacn_read_packet_done(struct tevent_req *subreq)
-{
-       struct tevent_req *req =
-               tevent_req_callback_data(subreq, struct tevent_req);
-       struct dcerpc_ncacn_read_packet_state *state =
-               tevent_req_data(req, struct dcerpc_ncacn_read_packet_state);
-       NTSTATUS status;
-
-       status = dcerpc_read_ncacn_packet_recv(subreq, state,
-                                               &state->pkt,
-                                               &state->buffer);
-       TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(3, ("Failed to receive dceprc packet!\n"));
-               tevent_req_nterror(req, status);
-               return;
-       }
-
-       tevent_req_done(req);
-}
-
-static NTSTATUS dcerpc_ncacn_read_packet_recv(struct tevent_req *req,
-                                               TALLOC_CTX *mem_ctx,
-                                               DATA_BLOB *buffer)
-{
-       struct dcerpc_ncacn_read_packet_state *state =
-               tevent_req_data(req, struct dcerpc_ncacn_read_packet_state);
-       NTSTATUS status;
-
-       if (tevent_req_is_nterror(req, &status)) {
-               tevent_req_received(req);
-               return status;
-       }
-
-       buffer->data = talloc_move(mem_ctx, &state->buffer.data);
-       buffer->length = state->buffer.length;
-
-       tevent_req_received(req);
-       return NT_STATUS_OK;
-}
-
-
-
 /* Start listening on the appropriate unix socket and setup all is needed to
  * dispatch requests to the pipes rpc implementation */
 
@@ -305,6 +231,7 @@ struct dcerpc_ncacn_listen_state {
 
        struct tevent_context *ev_ctx;
        struct messaging_context *msg_ctx;
+       dcerpc_ncacn_disconnect_fn disconnect_fn;
 };
 
 static void named_pipe_listener(struct tevent_context *ev,
@@ -390,15 +317,14 @@ static void named_pipe_listener(struct tevent_context *ev,
 
        len = sizeof(sunaddr);
 
-       while (sd == -1) {
-               sd = accept(state->fd,
-                           (struct sockaddr *)(void *)&sunaddr, &len);
-               if (errno != EINTR) break;
-       }
+       sd = accept(state->fd,
+                   (struct sockaddr *)(void *)&sunaddr, &len);
 
        if (sd == -1) {
-               DEBUG(6, ("Failed to get a valid socket [%s]\n",
-                         strerror(errno)));
+               if (errno != EINTR) {
+                       DEBUG(6, ("Failed to get a valid socket [%s]\n",
+                                 strerror(errno)));
+               }
                return;
        }
 
@@ -429,7 +355,7 @@ struct named_pipe_client {
        char *client_name;
        struct tsocket_address *server;
        char *server_name;
-       struct auth_session_info_transport *session_info;
+       struct auth_session_info *session_info;
 
        struct pipes_struct *p;
 
@@ -507,6 +433,7 @@ static void named_pipe_packet_done(struct tevent_req *subreq);
 
 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;
@@ -519,7 +446,10 @@ static void named_pipe_accept_done(struct tevent_req *subreq)
                                                &npc->client_name,
                                                &npc->server,
                                                &npc->server_name,
-                                               &npc->session_info);
+                                               &session_info_transport);
+
+       npc->session_info = talloc_move(npc, &session_info_transport->session_info);
+
        TALLOC_FREE(subreq);
        if (ret != 0) {
                DEBUG(2, ("Failed to accept named pipe connection! (%s)\n",
@@ -540,8 +470,8 @@ static void named_pipe_accept_done(struct tevent_req *subreq)
        }
 
        ret = make_server_pipes_struct(npc,
-                                       npc->pipe_name, npc->pipe_id,
-                                       cli_addr, NULL, npc->session_info,
+                                       npc->pipe_name, npc->pipe_id, NCACN_NP,
+                                       false, cli_addr, NULL, npc->session_info,
                                        &npc->p, &error);
        if (ret != 0) {
                DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
@@ -556,8 +486,8 @@ static void named_pipe_accept_done(struct tevent_req *subreq)
                goto fail;
        }
 
-       /* And now start receaving and processing packets */
-       subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
+       /* And now start receiving and processing packets */
+       subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
        if (!subreq) {
                DEBUG(2, ("Failed to start receving packets\n"));
                goto fail;
@@ -579,6 +509,7 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
                tevent_req_callback_data(subreq, struct named_pipe_client);
        struct _output_data *out = &npc->p->out_data;
        DATA_BLOB recv_buffer = data_blob_null;
+       struct ncacn_packet *pkt;
        NTSTATUS status;
        ssize_t data_left;
        ssize_t data_used;
@@ -586,7 +517,7 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
        uint32_t to_send;
        bool ok;
 
-       status = dcerpc_ncacn_read_packet_recv(subreq, npc, &recv_buffer);
+       status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
                goto fail;
@@ -610,6 +541,7 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
 
        /* Do not leak this buffer, npc is a long lived context */
        talloc_free(recv_buffer.data);
+       talloc_free(pkt);
 
        /* this is needed because of the way DCERPC Binds work in
         * the RPC marshalling code */
@@ -671,7 +603,7 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
         * data */
        if (npc->count == 0) {
                /* Wait for the next packet */
-               subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
+               subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
                if (!subreq) {
                        DEBUG(2, ("Failed to start receving packets\n"));
                        status = NT_STATUS_NO_MEMORY;
@@ -727,7 +659,7 @@ static void named_pipe_packet_done(struct tevent_req *subreq)
        data_blob_free(&npc->p->out_data.rdata);
 
        /* Wait for the next packet */
-       subreq = dcerpc_ncacn_read_packet_send(npc, npc->ev, npc->tstream);
+       subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
        if (!subreq) {
                DEBUG(2, ("Failed to start receving packets\n"));
                sys_errno = ENOMEM;
@@ -743,7 +675,18 @@ fail:
        /* terminate client connection */
        talloc_free(npc);
        return;
- }
+}
+
+static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
+                               struct messaging_context *msg_ctx,
+                               struct ndr_syntax_id syntax_id,
+                               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
@@ -767,12 +710,13 @@ uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
        state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
        if (state == NULL) {
                DEBUG(0, ("setup_dcerpc_ncacn_tcpip_socket: Out of memory\n"));
-               return false;
+               return 0;
        }
 
        state->syntax_id = syntax_id;
        state->fd = -1;
        state->ep.port = port;
+       state->disconnect_fn = NULL;
 
        if (state->ep.port == 0) {
                uint16_t i;
@@ -843,16 +787,6 @@ out:
        return 0;
 }
 
-static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
-                               struct messaging_context *msg_ctx,
-                               struct ndr_syntax_id syntax_id,
-                               enum dcerpc_transport_t transport,
-                               const char *name,
-                               uint16_t port,
-                               struct tsocket_address *cli_addr,
-                               struct tsocket_address *srv_addr,
-                               int s);
-
 static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
                                        struct tevent_fd *fde,
                                        uint16_t flags,
@@ -868,16 +802,12 @@ static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
        int s = -1;
        int rc;
 
-       while (s == -1) {
-               s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
-               if (s == -1 && errno != EINTR) {
-                       break;
-               }
-       }
-
+       s = accept(state->fd, (struct sockaddr *)(void *) &addr, &in_addrlen);
        if (s == -1) {
-               DEBUG(0,("tcpip_listener accept: %s\n",
-                        strerror(errno)));
+               if (errno != EINTR) {
+                       DEBUG(0,("tcpip_listener accept: %s\n",
+                                strerror(errno)));
+               }
                return;
        }
 
@@ -915,7 +845,134 @@ static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
                            state->ep.port,
                            cli_addr,
                            srv_addr,
-                           s);
+                           s,
+                           NULL);
+}
+
+/********************************************************************
+ * Start listening on the ncalrpc socket
+ ********************************************************************/
+
+static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
+                                   struct tevent_fd *fde,
+                                   uint16_t flags,
+                                   void *private_data);
+
+bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
+                                struct messaging_context *msg_ctx,
+                                struct ndr_syntax_id syntax_id,
+                                const char *name,
+                                dcerpc_ncacn_disconnect_fn fn)
+{
+       struct dcerpc_ncacn_listen_state *state;
+       struct tevent_fd *fde;
+
+       state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
+       if (state == NULL) {
+               DEBUG(0, ("Out of memory\n"));
+               return false;
+       }
+
+       state->syntax_id = syntax_id;
+       state->fd = -1;
+       state->disconnect_fn = fn;
+
+       if (name == NULL) {
+               name = "DEFAULT";
+       }
+       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(), 0700)) {
+               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, 0700);
+       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;
+
+       /* Set server socket to non-blocking for the accept. */
+       set_blocking(state->fd, false);
+
+       fde = tevent_add_fd(state->ev_ctx,
+                           state,
+                           state->fd,
+                           TEVENT_FD_READ,
+                           dcerpc_ncalrpc_listener,
+                           state);
+       if (fde == NULL) {
+               DEBUG(0, ("Failed to add event handler for ncalrpc!\n"));
+               goto out;
+       }
+
+       tevent_fd_set_auto_close(fde);
+
+       return true;
+out:
+       if (state->fd != -1) {
+               close(state->fd);
+       }
+       TALLOC_FREE(state);
+
+       return 0;
+}
+
+static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
+                                       struct tevent_fd *fde,
+                                       uint16_t flags,
+                                       void *private_data)
+{
+       struct dcerpc_ncacn_listen_state *state =
+                       talloc_get_type_abort(private_data,
+                                             struct dcerpc_ncacn_listen_state);
+       struct tsocket_address *cli_addr = NULL;
+       struct sockaddr_un sunaddr;
+       struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
+       socklen_t len = sizeof(sunaddr);
+       int sd = -1;
+       int rc;
+
+       ZERO_STRUCT(sunaddr);
+
+       sd = accept(state->fd, addr, &len);
+       if (sd == -1) {
+               if (errno != EINTR) {
+                       DEBUG(0, ("ncalrpc accept() failed: %s\n", strerror(errno)));
+               }
+               return;
+       }
+
+       rc = tsocket_address_bsd_from_sockaddr(state,
+                                              addr, len,
+                                              &cli_addr);
+       if (rc < 0) {
+               close(sd);
+               return;
+       }
+
+       DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
+
+       dcerpc_ncacn_accept(state->ev_ctx,
+                           state->msg_ctx,
+                           state->syntax_id, NCALRPC,
+                           state->ep.name, 0,
+                           cli_addr, NULL, sd,
+                           state->disconnect_fn);
 }
 
 struct dcerpc_ncacn_conn {
@@ -931,6 +988,7 @@ struct dcerpc_ncacn_conn {
        int sock;
 
        struct pipes_struct *p;
+       dcerpc_ncacn_disconnect_fn disconnect_fn;
 
        struct tevent_context *ev_ctx;
        struct messaging_context *msg_ctx;
@@ -942,7 +1000,7 @@ struct dcerpc_ncacn_conn {
        char *client_name;
        struct tsocket_address *server;
        char *server_name;
-       struct auth_session_info_transport *session_info;
+       struct auth_session_info *session_info;
 
        struct iovec *iov;
        size_t count;
@@ -959,45 +1017,34 @@ static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
                                uint16_t port,
                                struct tsocket_address *cli_addr,
                                struct tsocket_address *srv_addr,
-                               int s) {
+                               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;
        int sys_errno;
+       uid_t uid;
        int rc;
 
-       DEBUG(5, ("dcerpc_ncacn_accept\n"));
+       DEBUG(10, ("dcerpc_ncacn_accept\n"));
 
        ncacn_conn = talloc_zero(ev_ctx, struct dcerpc_ncacn_conn);
        if (ncacn_conn == NULL) {
-               DEBUG(0, ("dcerpc_ncacn_accept: Out of memory!\n"));
+               DEBUG(0, ("Out of memory!\n"));
                close(s);
                return;
        }
 
-       switch (transport) {
-               case NCACN_IP_TCP:
-                       ncacn_conn->ep.port = port;
-                       break;
-               case NCALRPC:
-               case NCACN_NP:
-                       ncacn_conn->ep.name = talloc_strdup(ncacn_conn, name);
-                       break;
-               default:
-                       DEBUG(0, ("dcerpc_ncacn_accept_function: "
-                                 "unknown transport: %u!\n", transport));
-                       talloc_free(ncacn_conn);
-                       close(s);
-                       return;
-       }
        ncacn_conn->transport = transport;
        ncacn_conn->syntax_id = syntax_id;
        ncacn_conn->ev_ctx = ev_ctx;
        ncacn_conn->msg_ctx = msg_ctx;
        ncacn_conn->sock = s;
+       ncacn_conn->disconnect_fn = fn;
 
        ncacn_conn->client = talloc_move(ncacn_conn, &cli_addr);
        if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
@@ -1010,7 +1057,7 @@ static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
                                                  ncacn_conn);
        }
        if (ncacn_conn->client_name == NULL) {
-               DEBUG(0, ("dcerpc_ncacn_accept: Out of memory!\n"));
+               DEBUG(0, ("Out of memory!\n"));
                talloc_free(ncacn_conn);
                close(s);
                return;
@@ -1023,49 +1070,79 @@ static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
                        tsocket_address_inet_addr_string(ncacn_conn->server,
                                                         ncacn_conn);
                if (ncacn_conn->server_name == NULL) {
-                       DEBUG(0, ("dcerpc_ncacn_accept: Out of memory!\n"));
+                       DEBUG(0, ("Out of memory!\n"));
                        talloc_free(ncacn_conn);
                        close(s);
                        return;
                }
        }
 
+       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) {
+                               close(s);
+                               talloc_free(ncacn_conn);
+                               return;
+                       }
+
+                       break;
+               case NCALRPC:
+                       rc = sys_getpeereid(s, &uid);
+                       if (rc < 0) {
+                               DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
+                       } else {
+                               if (uid == sec_initial_uid()) {
+                                       system_user = true;
+                               }
+                       }
+               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) {
+                               close(s);
+                               talloc_free(ncacn_conn);
+                               return;
+                       }
+                       break;
+               default:
+                       DEBUG(0, ("unknown dcerpc transport: %u!\n",
+                                 transport));
+                       talloc_free(ncacn_conn);
+                       close(s);
+                       return;
+       }
+
        rc = set_blocking(s, false);
        if (rc < 0) {
-               DEBUG(2, ("dcerpc_ncacn_accept: Failed to set socket to "
-                         "non-blocking\n"));
+               DEBUG(2, ("Failed to set dcerpc socket to non-blocking\n"));
                talloc_free(ncacn_conn);
                close(s);
                return;
        }
 
        /*
-        * As soon as we have tstream_bsd_existing_socket set up it will take
-        * care closing the socket.
+        * As soon as we have tstream_bsd_existing_socket set up it will
+        * take care of closing the socket.
         */
        rc = tstream_bsd_existing_socket(ncacn_conn, s, &ncacn_conn->tstream);
        if (rc < 0) {
-               DEBUG(2, ("dcerpc_ncacn_accept: Failed to create tstream "
-                         "socket\n"));
+               DEBUG(2, ("Failed to create tstream socket for dcerpc\n"));
                talloc_free(ncacn_conn);
                close(s);
                return;
        }
 
-       switch(ncacn_conn->transport) {
-               case NCACN_IP_TCP:
-                       pipe_name = tsocket_address_string(ncacn_conn->client,
-                                                          ncacn_conn);
-                       break;
-               case NCALRPC:
-                       pipe_name = talloc_strdup(ncacn_conn,
-                                                 ncacn_conn->ep.name);
-                       break;
-               default:
-                       talloc_free(ncacn_conn);
-                       return;
-       }
-
        if (tsocket_address_is_inet(ncacn_conn->client, "ip")) {
                cli_str = ncacn_conn->client_name;
        } else {
@@ -1084,7 +1161,7 @@ static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
                status = auth_anonymous_session_info(ncacn_conn,
                                                     &ncacn_conn->session_info);
                if (!NT_STATUS_IS_OK(status)) {
-                       DEBUG(2, ("dcerpc_ncacn_accept: Failed to create "
+                       DEBUG(2, ("Failed to create "
                                  "auth_anonymous_session_info - %s\n",
                                  nt_errstr(status)));
                        talloc_free(ncacn_conn);
@@ -1095,39 +1172,40 @@ static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
        rc = make_server_pipes_struct(ncacn_conn,
                                      pipe_name,
                                      ncacn_conn->syntax_id,
+                                     ncacn_conn->transport,
+                                     system_user,
                                      cli_str,
                                      srv_str,
                                      ncacn_conn->session_info,
                                      &ncacn_conn->p,
                                      &sys_errno);
        if (rc < 0) {
-               DEBUG(2, ("dcerpc_ncacn_accept: Failed to create pipe "
-                         "struct - %s", strerror(sys_errno)));
+               DEBUG(2, ("Failed to create pipe struct - %s",
+                         strerror(sys_errno)));
                talloc_free(ncacn_conn);
                return;
        }
 
        ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
-                       "dcerpc_tcpip_accept_function");
+                                                       "dcerpc send queue");
        if (ncacn_conn->send_queue == NULL) {
-               DEBUG(0, ("dcerpc_ncacn_accept_function: Out of memory!\n"));
+               DEBUG(0, ("Out of memory!\n"));
                talloc_free(ncacn_conn);
                return;
        }
 
-       subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
+       subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
                                               ncacn_conn->ev_ctx,
                                               ncacn_conn->tstream);
        if (subreq == NULL) {
-               DEBUG(2, ("dcerpc_ncacn_accept: Failed to send ncacn "
-                         "packet\n"));
+               DEBUG(2, ("Failed to send ncacn packet\n"));
                talloc_free(ncacn_conn);
                return;
        }
 
        tevent_req_set_callback(subreq, dcerpc_ncacn_packet_process, ncacn_conn);
 
-       DEBUG(5, ("dcerpc_ncacn_accept done\n"));
+       DEBUG(10, ("dcerpc_ncacn_accept done\n"));
 
        return;
 }
@@ -1139,6 +1217,7 @@ static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
 
        struct _output_data *out = &ncacn_conn->p->out_data;
        DATA_BLOB recv_buffer = data_blob_null;
+       struct ncacn_packet *pkt;
        ssize_t data_left;
        ssize_t data_used;
        uint32_t to_send;
@@ -1146,9 +1225,15 @@ static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
        NTSTATUS status;
        bool ok;
 
-       status = dcerpc_ncacn_read_packet_recv(subreq, ncacn_conn, &recv_buffer);
+       status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
+               if (ncacn_conn->disconnect_fn != NULL) {
+                       ok = ncacn_conn->disconnect_fn(ncacn_conn->p);
+                       if (!ok) {
+                               DEBUG(3, ("Failed to call disconnect function\n"));
+                       }
+               }
                goto fail;
        }
 
@@ -1169,6 +1254,7 @@ static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
 
        /* Do not leak this buffer */
        talloc_free(recv_buffer.data);
+       talloc_free(pkt);
 
        /*
         * This is needed because of the way DCERPC binds work in the RPC
@@ -1187,6 +1273,7 @@ static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
                ncacn_conn->iov = talloc_zero(ncacn_conn, struct iovec);
                if (ncacn_conn->iov == NULL) {
                        status = NT_STATUS_NO_MEMORY;
+                       DEBUG(3, ("Out of memory!\n"));
                        goto fail;
                }
                ncacn_conn->count = 1;
@@ -1215,6 +1302,7 @@ static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
                                                 struct iovec,
                                                 ncacn_conn->count + 1);
                if (ncacn_conn->iov == NULL) {
+                       DEBUG(3, ("Out of memory!\n"));
                        status = NT_STATUS_NO_MEMORY;
                        goto fail;
                }
@@ -1236,7 +1324,7 @@ static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
         */
        if (ncacn_conn->count == 0) {
                /* Wait for the next packet */
-               subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
+               subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
                                                       ncacn_conn->ev_ctx,
                                                       ncacn_conn->tstream);
                if (subreq == NULL) {
@@ -1267,9 +1355,8 @@ static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
        return;
 
 fail:
-       DEBUG(2, ("Fatal error(%s). "
-                 "Terminating client(%s) connection!\n",
-                 nt_errstr(status), ncacn_conn->client_name));
+       DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
+                 ncacn_conn->client_name, nt_errstr(status)));
 
        /* Terminate client connection */
        talloc_free(ncacn_conn);
@@ -1280,6 +1367,7 @@ static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
 {
        struct dcerpc_ncacn_conn *ncacn_conn =
                tevent_req_callback_data(subreq, struct dcerpc_ncacn_conn);
+       NTSTATUS status = NT_STATUS_OK;
        int sys_errno;
        int rc;
 
@@ -1287,6 +1375,7 @@ static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
        TALLOC_FREE(subreq);
        if (rc < 0) {
                DEBUG(2, ("Writev failed!\n"));
+               status = map_nt_error_from_unix(sys_errno);
                goto fail;
        }
 
@@ -1298,12 +1387,12 @@ static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
        data_blob_free(&ncacn_conn->p->out_data.rdata);
 
        /* Wait for the next packet */
-       subreq = dcerpc_ncacn_read_packet_send(ncacn_conn,
+       subreq = dcerpc_read_ncacn_packet_send(ncacn_conn,
                                               ncacn_conn->ev_ctx,
                                               ncacn_conn->tstream);
        if (subreq == NULL) {
                DEBUG(2, ("Failed to start receving packets\n"));
-               sys_errno = ENOMEM;
+               status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
@@ -1311,8 +1400,8 @@ static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
        return;
 
 fail:
-       DEBUG(2, ("Fatal error(%s). Terminating client(%s) connection!\n",
-                 strerror(sys_errno), ncacn_conn->client_name));
+       DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
+                 ncacn_conn->client_name, nt_errstr(status)));
 
        /* Terminate client connection */
        talloc_free(ncacn_conn);