s3-rpc_server remove per-element copies of auth_session_info
[amitay/samba.git] / source3 / rpc_server / rpc_server.c
index 6f3d72d756245251d5c9fcd53694ee5dc21da39c..a851bd2aed5d6f831db9a741ce7e166104e8493a 100644 (file)
@@ -2,6 +2,7 @@
    Unix SMB/Netbios implementation.
    Generic infrstructure for RPC Daemons
    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 "ntdomain.h"
 #include "rpc_server/rpc_server.h"
 #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 "rpc_server/rpc_ncacn_np.h"
+#include "rpc_server/srv_pipe_hnd.h"
+#include "rpc_server/srv_pipe.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_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);
-       if (i == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       status = make_server_info_guest(i, &s);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
-       i->security_token = s->security_token;
-       i->session_key    = s->user_session_key;
-
-       val.sam3 = s->info3;
-
-       status = make_user_info_dc_netlogon_validation(mem_ctx,
-                                                      "",
-                                                      3,
-                                                      &val,
-                                                      &u);
+       status = make_session_info_guest(mem_ctx, session_info);
        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;
 }
@@ -75,29 +52,25 @@ static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
  * sent from the client */
 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 system_user,
-                                   const char *client_address,
-                                   const char *server_address,
-                                   struct auth_session_info_transport *session_info,
+                                   bool ncalrpc_as_system,
+                                   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;
        NTSTATUS status;
-       bool ok;
 
        p = talloc_zero(mem_ctx, struct pipes_struct);
        if (!p) {
                *perrno = ENOMEM;
                return -1;
        }
-       p->syntax = id;
+
        p->transport = transport;
-       p->system_user = system_user;
+       p->ncalrpc_as_system = ncalrpc_as_system;
 
        p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
        if (!p->mem_ctx) {
@@ -106,106 +79,79 @@ static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
                return -1;
        }
 
-       ok = init_pipe_handles(p, &id);
-       if (!ok) {
-               DEBUG(1, ("Failed to init handles\n"));
-               TALLOC_FREE(p);
-               *perrno = EINVAL;
-               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;
-               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;
+       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);
 
-       /* 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);
+       } else {
+               struct auth_user_info_dc *auth_user_info_dc;
+               struct auth_serversupplied_info *server_info;
+               struct netr_SamInfo3 *info3;
 
-       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;
-       }
+               /* 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;
+               }
 
-       status = make_server_info_info3(p,
-                                       info3->base.account_name.string,
-                                       info3->base.domain.string,
-                                       &p->session_info, info3);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to init server info\n"));
-               TALLOC_FREE(p);
-               *perrno = EINVAL;
-               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;
 
-       /*
-        * Some internal functions need a local token to determine access to
-        * resoutrces.
-        */
-       status = create_local_token(p->session_info);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(1, ("Failed to init local auth token\n"));
-               TALLOC_FREE(p);
-               *perrno = EINVAL;
-               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);
 
-       /* 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);
+               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;
+               }
 
-       /* 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);
+               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->client_id = talloc_zero(p, struct client_address);
-       if (!p->client_id) {
-               TALLOC_FREE(p);
-               *perrno = ENOMEM;
-               return -1;
+               /*
+                * Some internal functions need a local token to determine access to
+                * resources.
+                */
+               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;
+               }
        }
-       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) {
+
+       p->remote_address = tsocket_address_copy(remote_address, p);
+       if (p->remote_address == NULL) {
                TALLOC_FREE(p);
                *perrno = ENOMEM;
                return -1;
        }
 
-       if (server_address != NULL) {
-               p->server_id = talloc_zero(p, struct client_address);
-               if (p->client_id == NULL) {
-                       TALLOC_FREE(p);
-                       *perrno = ENOMEM;
-                       return -1;
-               }
-
-               strlcpy(p->server_id->addr,
-                       server_address,
-                       sizeof(p->server_id->addr));
-
-               p->server_id->name = talloc_strdup(p->server_id,
-                                                  server_address);
-               if (p->server_id->name == NULL) {
+       if (local_address != NULL) {
+               p->local_address = tsocket_address_copy(local_address, p);
+               if (p->local_address == NULL) {
                        TALLOC_FREE(p);
                        *perrno = ENOMEM;
                        return -1;
@@ -232,6 +178,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,
@@ -240,7 +187,8 @@ static void named_pipe_listener(struct tevent_context *ev,
                                void *private_data);
 
 bool setup_named_pipe_socket(const char *pipe_name,
-                            struct tevent_context *ev_ctx)
+                            struct tevent_context *ev_ctx,
+                            struct messaging_context *msg_ctx)
 {
        struct dcerpc_ncacn_listen_state *state;
        struct tevent_fd *fde;
@@ -258,6 +206,20 @@ bool setup_named_pipe_socket(const char *pipe_name,
        }
        state->fd = -1;
 
+       state->ev_ctx = ev_ctx;
+       state->msg_ctx = msg_ctx;
+
+       /*
+        * As lp_ncalrpc_dir() should have 0755, but
+        * lp_ncalrpc_dir()/np should have 0700, we need to
+        * create lp_ncalrpc_dir() first.
+        */
+       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;
+       }
+
        np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
        if (!np_dir) {
                DEBUG(0, ("Out of memory\n"));
@@ -299,7 +261,10 @@ out:
        return false;
 }
 
-static void named_pipe_accept_function(const char *pipe_name, int fd);
+static void named_pipe_accept_function(struct tevent_context *ev_ctx,
+                                      struct messaging_context *msg_ctx,
+                                      const char *pipe_name,
+                                      int fd);
 
 static void named_pipe_listener(struct tevent_context *ev,
                                struct tevent_fd *fde,
@@ -330,7 +295,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);
 }
 
 
@@ -340,7 +308,6 @@ static void named_pipe_listener(struct tevent_context *ev,
 
 struct named_pipe_client {
        const char *pipe_name;
-       struct ndr_syntax_id pipe_id;
 
        struct tevent_context *ev;
        struct messaging_context *msg_ctx;
@@ -355,7 +322,8 @@ 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;
 
@@ -367,22 +335,16 @@ struct named_pipe_client {
 
 static void named_pipe_accept_done(struct tevent_req *subreq);
 
-static void named_pipe_accept_function(const char *pipe_name, int fd)
+static void named_pipe_accept_function(struct tevent_context *ev_ctx,
+                                      struct messaging_context *msg_ctx,
+                                      const char *pipe_name,
+                                      int fd)
 {
-       struct ndr_syntax_id syntax;
        struct named_pipe_client *npc;
        struct tstream_context *plain;
        struct tevent_req *subreq;
-       bool ok;
        int ret;
 
-       ok = is_known_pipename(pipe_name, &syntax);
-       if (!ok) {
-               DEBUG(1, ("Unknown pipe [%s]\n", pipe_name));
-               close(fd);
-               return;
-       }
-
        npc = talloc_zero(NULL, struct named_pipe_client);
        if (!npc) {
                DEBUG(0, ("Out of memory!\n"));
@@ -390,9 +352,8 @@ static void named_pipe_accept_function(const char *pipe_name, int fd)
                return;
        }
        npc->pipe_name = pipe_name;
-       npc->pipe_id = syntax;
-       npc->ev = server_event_context();
-       npc->msg_ctx = server_messaging_context();
+       npc->ev = ev_ctx;
+       npc->msg_ctx = msg_ctx;
 
        /* make sure socket is in NON blocking state */
        ret = set_blocking(fd, false);
@@ -433,9 +394,9 @@ 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;
        int error;
        int ret;
 
@@ -445,7 +406,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",
@@ -454,20 +418,9 @@ 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->pipe_name, npc->pipe_id, NCACN_NP,
-                                       false, cli_addr, NULL, npc->session_info,
+                                      npc->pipe_name, NCACN_NP,
+                                       false, npc->server, npc->client, npc->session_info,
                                        &npc->p, &error);
        if (ret != 0) {
                DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
@@ -482,7 +435,7 @@ static void named_pipe_accept_done(struct tevent_req *subreq)
                goto fail;
        }
 
-       /* And now start receaving and processing packets */
+       /* 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"));
@@ -675,13 +628,13 @@ fail:
 
 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);
+                               int s,
+                               dcerpc_ncacn_disconnect_fn fn);
 
 /********************************************************************
  * Start listening on the tcp/ip socket
@@ -694,7 +647,6 @@ static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
 
 uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
                                         struct messaging_context *msg_ctx,
-                                        struct ndr_syntax_id syntax_id,
                                         const struct sockaddr_storage *ifss,
                                         uint16_t port)
 {
@@ -708,9 +660,9 @@ uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
                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;
@@ -833,13 +785,13 @@ static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
 
        dcerpc_ncacn_accept(state->ev_ctx,
                            state->msg_ctx,
-                           state->syntax_id,
                            NCACN_IP_TCP,
                            NULL,
                            state->ep.port,
                            cli_addr,
                            srv_addr,
-                           s);
+                           s,
+                           NULL);
 }
 
 /********************************************************************
@@ -853,8 +805,8 @@ static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
 
 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
                                 struct messaging_context *msg_ctx,
-                                struct ndr_syntax_id syntax_id,
-                                const char *name)
+                                const char *name,
+                                dcerpc_ncacn_disconnect_fn fn)
 {
        struct dcerpc_ncacn_listen_state *state;
        struct tevent_fd *fde;
@@ -865,8 +817,8 @@ bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
                return false;
        }
 
-       state->syntax_id = syntax_id;
        state->fd = -1;
+       state->disconnect_fn = fn;
 
        if (name == NULL) {
                name = "DEFAULT";
@@ -879,13 +831,13 @@ bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
                return false;
        }
 
-       if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0700)) {
+       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, 0700);
+       state->fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
        if (state->fd == -1) {
                DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
                          lp_ncalrpc_dir(), name));
@@ -960,14 +912,13 @@ static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
 
        dcerpc_ncacn_accept(state->ev_ctx,
                            state->msg_ctx,
-                           state->syntax_id, NCALRPC,
+                           NCALRPC,
                            state->ep.name, 0,
-                           cli_addr, NULL, sd);
+                           cli_addr, NULL, sd,
+                           state->disconnect_fn);
 }
 
 struct dcerpc_ncacn_conn {
-       struct ndr_syntax_id syntax_id;
-
        enum dcerpc_transport_t transport;
 
        union {
@@ -978,6 +929,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;
@@ -989,7 +941,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;
@@ -1000,13 +952,13 @@ 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,
-                               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) {
+                               int s,
+                               dcerpc_ncacn_disconnect_fn fn) {
        struct dcerpc_ncacn_conn *ncacn_conn;
        struct tevent_req *subreq;
        const char *cli_str;
@@ -1028,10 +980,10 @@ static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
        }
 
        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")) {
@@ -1158,11 +1110,10 @@ 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->server,
+                                     ncacn_conn->client,
                                      ncacn_conn->session_info,
                                      &ncacn_conn->p,
                                      &sys_errno);
@@ -1215,7 +1166,12 @@ static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
        status = dcerpc_read_ncacn_packet_recv(subreq, ncacn_conn, &pkt, &recv_buffer);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(3, ("Failed to receive ncacn packet!\n"));
+               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;
        }
 
@@ -1337,9 +1293,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);
@@ -1350,6 +1305,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;
 
@@ -1357,6 +1313,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;
        }
 
@@ -1373,7 +1330,7 @@ static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
                                               ncacn_conn->tstream);
        if (subreq == NULL) {
                DEBUG(2, ("Failed to start receving packets\n"));
-               sys_errno = ENOMEM;
+               status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
@@ -1381,8 +1338,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);