Revert "vfs_glusterfs: explain that/why we use the raw tevent_context in init_gluster...
[samba.git] / source3 / rpc_server / rpc_server.c
index 43f1b3d605ce6eccae5e25d6957d531f3ce57b48..36dff114f90775a66430041d51cb8cdcb0f7cef3 100644 (file)
 #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 **session_info)
-{
-       NTSTATUS status;
-
-       status = make_session_info_guest(mem_ctx, session_info);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
-       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 struct tsocket_address *local_address,
-                                   const struct tsocket_address *remote_address,
-                                   struct auth_session_info *session_info,
-                                   struct pipes_struct **_p,
-                                   int *perrno)
+int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
+                            struct messaging_context *msg_ctx,
+                            const char *pipe_name,
+                            enum dcerpc_transport_t transport,
+                            const struct tsocket_address *remote_address,
+                            const struct tsocket_address *local_address,
+                            struct auth_session_info *session_info,
+                            struct pipes_struct **_p,
+                            int *perrno)
 {
        struct pipes_struct *p;
-       NTSTATUS status;
        int ret;
 
        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;
@@ -81,58 +62,9 @@ static int make_server_pipes_struct(TALLOC_CTX *mem_ctx,
                p->session_info = talloc_steal(p, session_info);
 
        } else {
-               struct auth_user_info_dc *auth_user_info_dc;
-               struct auth_serversupplied_info *server_info;
-               struct netr_SamInfo3 *info3;
-
-               /* 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;
-
-               /* 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 (!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;
-               }
-
-               /*
-                * 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 = EINVAL;
-                       return -1;
-               }
+               DEBUG(0, ("Supplied session_info in make_server_pipes_struct was incomplete!"));
+               *perrno = EINVAL;
+               return -1;
        }
 
        *_p = p;
@@ -171,7 +103,7 @@ int create_named_pipe_socket(const char *pipe_name)
         * 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)) {
+       if (!directory_create_or_exist(lp_ncalrpc_dir(), 0755)) {
                DEBUG(0, ("Failed to create pipe directory %s - %s\n",
                          lp_ncalrpc_dir(), strerror(errno)));
                goto out;
@@ -183,7 +115,7 @@ int create_named_pipe_socket(const char *pipe_name)
                goto out;
        }
 
-       if (!directory_create_or_exist(np_dir, geteuid(), 0700)) {
+       if (!directory_create_or_exist_strict(np_dir, geteuid(), 0700)) {
                DEBUG(0, ("Failed to create pipe directory %s - %s\n",
                          np_dir, strerror(errno)));
                goto out;
@@ -196,7 +128,7 @@ int create_named_pipe_socket(const char *pipe_name)
                goto out;
        }
 
-       DEBUG(10, ("Openened pipe socket fd %d for %s\n", fd, pipe_name));
+       DEBUG(10, ("Opened pipe socket fd %d for %s\n", fd, pipe_name));
 
 out:
        talloc_free(np_dir);
@@ -209,6 +141,7 @@ bool setup_named_pipe_socket(const char *pipe_name,
 {
        struct dcerpc_ncacn_listen_state *state;
        struct tevent_fd *fde;
+       int rc;
 
        state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
        if (!state) {
@@ -225,10 +158,17 @@ bool setup_named_pipe_socket(const char *pipe_name,
                goto out;
        }
 
+       rc = listen(state->fd, 5);
+       if (rc < 0) {
+               DEBUG(0, ("Failed to listen on pipe socket %s: %s\n",
+                         pipe_name, strerror(errno)));
+               goto out;
+       }
+
        state->ev_ctx = ev_ctx;
        state->msg_ctx = msg_ctx;
 
-       DEBUG(10, ("Openened pipe socket fd %d for %s\n",
+       DEBUG(10, ("Opened pipe socket fd %d for %s\n",
                   state->fd, pipe_name));
 
        fde = tevent_add_fd(ev_ctx,
@@ -276,6 +216,7 @@ static void named_pipe_listener(struct tevent_context *ev,
                }
                return;
        }
+       smb_set_close_on_exec(sd);
 
        DEBUG(6, ("Accepted socket %d\n", sd));
 
@@ -290,36 +231,6 @@ static void named_pipe_listener(struct tevent_context *ev,
  * Accepts connections from clients and process requests using the appropriate
  * dispatcher table. */
 
-struct named_pipe_client {
-       const char *pipe_name;
-
-       struct tevent_context *ev;
-       struct messaging_context *msg_ctx;
-
-       uint16_t file_type;
-       uint16_t device_state;
-       uint64_t allocation_size;
-
-       struct tstream_context *tstream;
-
-       struct tsocket_address *client;
-       char *client_name;
-       struct tsocket_address *server;
-       char *server_name;
-
-       struct auth_session_info *session_info;
-
-       struct pipes_struct *p;
-
-       struct tevent_queue *write_queue;
-
-       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) {
@@ -328,6 +239,44 @@ static int named_pipe_destructor(struct named_pipe_client *npc)
        return 0;
 }
 
+struct named_pipe_client *named_pipe_client_init(TALLOC_CTX *mem_ctx,
+                                                struct tevent_context *ev_ctx,
+                                                struct messaging_context *msg_ctx,
+                                                const char *pipe_name,
+                                                named_pipe_termination_fn *term_fn,
+                                                uint16_t file_type,
+                                                uint16_t device_state,
+                                                uint64_t allocation_size,
+                                                void *private_data)
+{
+       struct named_pipe_client *npc;
+
+       npc = talloc_zero(mem_ctx, struct named_pipe_client);
+       if (npc == NULL) {
+               DEBUG(0, ("Out of memory!\n"));
+               return NULL;
+       }
+       talloc_set_destructor(npc, named_pipe_destructor);
+
+       npc->pipe_name = talloc_strdup(npc, pipe_name);
+       if (npc->pipe_name == NULL) {
+               DEBUG(0, ("Out of memory!\n"));
+               talloc_free(npc);
+               return NULL;
+       }
+
+       npc->ev = ev_ctx;
+       npc->msg_ctx = msg_ctx;
+       npc->term_fn = term_fn;
+       npc->private_data = private_data;
+
+       npc->file_type = file_type;
+       npc->device_state = device_state;
+       npc->allocation_size = allocation_size;
+
+       return npc;
+}
+
 static void named_pipe_accept_done(struct tevent_req *subreq);
 
 void named_pipe_accept_function(struct tevent_context *ev_ctx,
@@ -396,7 +345,6 @@ void named_pipe_accept_function(struct tevent_context *ev_ctx,
        tevent_req_set_callback(subreq, named_pipe_accept_done, npc);
 }
 
-static void named_pipe_packet_process(struct tevent_req *subreq);
 static void named_pipe_packet_done(struct tevent_req *subreq);
 
 static void named_pipe_accept_done(struct tevent_req *subreq)
@@ -409,10 +357,10 @@ static void named_pipe_accept_done(struct tevent_req *subreq)
 
        ret = tstream_npa_accept_existing_recv(subreq, &error, npc,
                                                &npc->tstream,
-                                               &npc->client,
-                                               &npc->client_name,
-                                               &npc->server,
-                                               &npc->server_name,
+                                               &npc->remote_client_addr,
+                                               &npc->remote_client_name,
+                                               &npc->local_server_addr,
+                                               &npc->local_server_name,
                                                &session_info_transport);
 
        npc->session_info = talloc_move(npc, &session_info_transport->session_info);
@@ -428,8 +376,10 @@ static void named_pipe_accept_done(struct tevent_req *subreq)
        ret = make_server_pipes_struct(npc,
                                       npc->msg_ctx,
                                       npc->pipe_name, NCACN_NP,
-                                       false, npc->server, npc->client, npc->session_info,
-                                       &npc->p, &error);
+                                      npc->remote_client_addr,
+                                      npc->local_server_addr,
+                                      npc->session_info,
+                                      &npc->p, &error);
        if (ret != 0) {
                DEBUG(2, ("Failed to create pipes_struct! (%s)\n",
                          strerror(error)));
@@ -445,7 +395,7 @@ static void named_pipe_accept_done(struct tevent_req *subreq)
        /* 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"));
+               DEBUG(2, ("Failed to start receiving packets\n"));
                goto fail;
        }
        tevent_req_set_callback(subreq, named_pipe_packet_process, npc);
@@ -453,13 +403,13 @@ static void named_pipe_accept_done(struct tevent_req *subreq)
 
 fail:
        DEBUG(2, ("Fatal error. Terminating client(%s) connection!\n",
-                 npc->client_name));
+                 npc->remote_client_name));
        /* terminate client connection */
        talloc_free(npc);
        return;
 }
 
-static void named_pipe_packet_process(struct tevent_req *subreq)
+void named_pipe_packet_process(struct tevent_req *subreq)
 {
        struct named_pipe_client *npc =
                tevent_req_callback_data(subreq, struct named_pipe_client);
@@ -467,9 +417,6 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
        DATA_BLOB recv_buffer = data_blob_null;
        struct ncacn_packet *pkt;
        NTSTATUS status;
-       ssize_t data_left;
-       ssize_t data_used;
-       char *data;
        uint32_t to_send;
        size_t i;
        bool ok;
@@ -480,23 +427,20 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
                goto fail;
        }
 
-       data_left = recv_buffer.length;
-       data = (char *)recv_buffer.data;
-
-       while (data_left) {
-
-               data_used = process_incoming_data(npc->p, data, data_left);
-               if (data_used < 0) {
-                       DEBUG(3, ("Failed to process dceprc request!\n"));
-                       status = NT_STATUS_UNEXPECTED_IO_ERROR;
-                       goto fail;
-               }
-
-               data_left -= data_used;
-               data += data_used;
+       /* dcerpc_read_ncacn_packet_recv() returns a full PDU */
+       npc->p->in_data.pdu_needed_len = 0;
+       npc->p->in_data.pdu = recv_buffer;
+       if (dcerpc_get_endian_flag(&recv_buffer) & DCERPC_DREP_LE) {
+               npc->p->endian = RPC_LITTLE_ENDIAN;
+       } else {
+               npc->p->endian = RPC_BIG_ENDIAN;
        }
+       DEBUG(10, ("PDU is in %s Endian format!\n",
+                  npc->p->endian ? "Big" : "Little"));
+       process_complete_pdu(npc->p, pkt);
 
-       /* Do not leak this buffer, npc is a long lived context */
+       /* reset pipe state and free PDU */
+       npc->p->in_data.pdu.length = 0;
        talloc_free(recv_buffer.data);
        talloc_free(pkt);
 
@@ -550,7 +494,7 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
                /* Wait for the next packet */
                subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
                if (!subreq) {
-                       DEBUG(2, ("Failed to start receving packets\n"));
+                       DEBUG(2, ("Failed to start receiving packets\n"));
                        status = NT_STATUS_NO_MEMORY;
                        goto fail;
                }
@@ -588,7 +532,7 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
 fail:
        DEBUG(2, ("Fatal error(%s). "
                  "Terminating client(%s) connection!\n",
-                 nt_errstr(status), npc->client_name));
+                 nt_errstr(status), npc->remote_client_name));
        /* terminate client connection */
        talloc_free(npc);
        return;
@@ -612,6 +556,12 @@ static void named_pipe_packet_done(struct tevent_req *subreq)
                return;
        }
 
+       if (npc->p->fault_state != 0) {
+               DEBUG(2, ("Disconnect after fault\n"));
+               sys_errno = EINVAL;
+               goto fail;
+       }
+
        /* clear out any data that may have been left around */
        npc->count = 0;
        TALLOC_FREE(npc->iov);
@@ -624,7 +574,7 @@ static void named_pipe_packet_done(struct tevent_req *subreq)
        /* Wait for the next packet */
        subreq = dcerpc_read_ncacn_packet_send(npc, npc->ev, npc->tstream);
        if (!subreq) {
-               DEBUG(2, ("Failed to start receving packets\n"));
+               DEBUG(2, ("Failed to start receiving packets\n"));
                sys_errno = ENOMEM;
                goto fail;
        }
@@ -634,7 +584,7 @@ static void named_pipe_packet_done(struct tevent_req *subreq)
 fail:
        DEBUG(2, ("Fatal error(%s). "
                  "Terminating client(%s) connection!\n",
-                 strerror(sys_errno), npc->client_name));
+                 strerror(sys_errno), npc->remote_client_name));
        /* terminate client connection */
        talloc_free(npc);
        return;
@@ -656,13 +606,13 @@ int create_tcpip_socket(const struct sockaddr_storage *ifss, uint16_t *port)
        if (*port == 0) {
                uint16_t i;
 
-               for (i = SERVER_TCP_LOW_PORT; i <= SERVER_TCP_HIGH_PORT; i++) {
+               for (i = lp_rpc_low_port(); i <= lp_rpc_high_port(); i++) {
                        fd = open_socket_in(SOCK_STREAM,
                                            i,
                                            0,
                                            ifss,
                                            false);
-                       if (fd > 0) {
+                       if (fd >= 0) {
                                *port = i;
                                break;
                        }
@@ -724,7 +674,7 @@ uint16_t setup_dcerpc_ncacn_tcpip_socket(struct tevent_context *ev_ctx,
                goto out;
        }
 
-       DEBUG(10, ("setup_tcpip_socket: openened socket fd %d for port %u\n",
+       DEBUG(10, ("setup_tcpip_socket: opened socket fd %d for port %u\n",
                   state->fd, state->ep.port));
 
        fde = tevent_add_fd(state->ev_ctx,
@@ -773,6 +723,7 @@ static void dcerpc_ncacn_tcpip_listener(struct tevent_context *ev,
                }
                return;
        }
+       smb_set_close_on_exec(s);
 
        rc = tsocket_address_bsd_from_sockaddr(state,
                                               (struct sockaddr *)(void *) &addr,
@@ -819,6 +770,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(), 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, ("Opened 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,
@@ -826,6 +803,7 @@ bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
 {
        struct dcerpc_ncacn_listen_state *state;
        struct tevent_fd *fde;
+       int rc;
 
        state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
        if (state == NULL) {
@@ -839,29 +817,26 @@ 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)));
+       state->fd = create_dcerpc_ncalrpc_socket(name);
+       if (state->fd == -1) {
                goto out;
        }
 
-       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));
+       rc = listen(state->fd, 5);
+       if (rc < 0) {
+               DEBUG(0, ("Failed to listen on ncalrpc socket %s: %s\n",
+                         name, strerror(errno)));
                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;
 
@@ -899,14 +874,18 @@ static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
        struct dcerpc_ncacn_listen_state *state =
                        talloc_get_type_abort(private_data,
                                              struct dcerpc_ncacn_listen_state);
-       struct tsocket_address *cli_addr = NULL;
+       struct tsocket_address *cli_addr = NULL, *srv_addr = NULL;
        struct sockaddr_un sunaddr;
        struct sockaddr *addr = (struct sockaddr *)(void *)&sunaddr;
        socklen_t len = sizeof(sunaddr);
+       struct sockaddr_un sunaddr_server;
+       struct sockaddr *addr_server = (struct sockaddr *)(void *)&sunaddr_server;
+       socklen_t len_server = sizeof(sunaddr_server);
        int sd = -1;
        int rc;
 
        ZERO_STRUCT(sunaddr);
+       ZERO_STRUCT(sunaddr_server);
 
        sd = accept(state->fd, addr, &len);
        if (sd == -1) {
@@ -915,6 +894,7 @@ static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
                }
                return;
        }
+       smb_set_close_on_exec(sd);
 
        rc = tsocket_address_bsd_from_sockaddr(state,
                                               addr, len,
@@ -924,13 +904,29 @@ static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
                return;
        }
 
-       DEBUG(10, ("Accepted ncalrpc socket %d\n", sd));
+       rc = getsockname(sd, addr_server, &len_server);
+       if (rc < 0) {
+               close(sd);
+               return;
+       }
+
+       rc = tsocket_address_bsd_from_sockaddr(state,
+                                              addr_server,
+                                              len_server,
+                                              &srv_addr);
+       if (rc < 0) {
+               close(sd);
+               return;
+       }
+
+       DEBUG(10, ("Accepted ncalrpc socket %s (fd: %d)\n",
+                  sunaddr.sun_path, sd));
 
        dcerpc_ncacn_accept(state->ev_ctx,
                            state->msg_ctx,
                            NCALRPC,
                            state->ep.name,
-                           cli_addr, NULL, sd,
+                           cli_addr, srv_addr, sd,
                            state->disconnect_fn);
 }
 
@@ -948,10 +944,10 @@ struct dcerpc_ncacn_conn {
        struct tstream_context *tstream;
        struct tevent_queue *send_queue;
 
-       struct tsocket_address *client;
-       char *client_name;
-       struct tsocket_address *server;
-       char *server_name;
+       struct tsocket_address *remote_client_addr;
+       char *remote_client_name;
+       struct tsocket_address *local_server_addr;
+       char *local_server_name;
        struct auth_session_info *session_info;
 
        struct iovec *iov;
@@ -971,11 +967,11 @@ void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
                         dcerpc_ncacn_disconnect_fn fn) {
        struct dcerpc_ncacn_conn *ncacn_conn;
        struct tevent_req *subreq;
-       bool system_user = false;
        char *pipe_name;
        NTSTATUS status;
        int sys_errno;
        uid_t uid;
+       gid_t gid;
        int rc;
 
        DEBUG(10, ("dcerpc_ncacn_accept\n"));
@@ -993,31 +989,37 @@ void dcerpc_ncacn_accept(struct tevent_context *ev_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")) {
-               ncacn_conn->client_name =
-                       tsocket_address_inet_addr_string(ncacn_conn->client,
+       ncacn_conn->remote_client_addr = talloc_move(ncacn_conn, &cli_addr);
+       if (tsocket_address_is_inet(ncacn_conn->remote_client_addr, "ip")) {
+               ncacn_conn->remote_client_name =
+                       tsocket_address_inet_addr_string(ncacn_conn->remote_client_addr,
                                                         ncacn_conn);
        } else {
-               ncacn_conn->client_name =
-                       tsocket_address_unix_path(ncacn_conn->client,
+               ncacn_conn->remote_client_name =
+                       tsocket_address_unix_path(ncacn_conn->remote_client_addr,
                                                  ncacn_conn);
        }
-       if (ncacn_conn->client_name == NULL) {
-               DEBUG(0, ("Out of memory!\n"));
+       if (ncacn_conn->remote_client_name == NULL) {
+               DEBUG(0, ("Out of memory obtaining remote socket address as a string!\n"));
                talloc_free(ncacn_conn);
                close(s);
                return;
        }
 
        if (srv_addr != NULL) {
-               ncacn_conn->server = talloc_move(ncacn_conn, &srv_addr);
-
-               ncacn_conn->server_name =
-                       tsocket_address_inet_addr_string(ncacn_conn->server,
-                                                        ncacn_conn);
-               if (ncacn_conn->server_name == NULL) {
-                       DEBUG(0, ("Out of memory!\n"));
+               ncacn_conn->local_server_addr = talloc_move(ncacn_conn, &srv_addr);
+
+               if (tsocket_address_is_inet(ncacn_conn->local_server_addr, "ip")) {
+                       ncacn_conn->local_server_name =
+                               tsocket_address_inet_addr_string(ncacn_conn->local_server_addr,
+                                                                ncacn_conn);
+               } else {
+                       ncacn_conn->local_server_name =
+                               tsocket_address_unix_path(ncacn_conn->local_server_addr,
+                                                         ncacn_conn);
+               }
+               if (ncacn_conn->local_server_name == NULL) {
+                       DEBUG(0, ("Out of memory obtaining local socket address as a string!\n"));
                        talloc_free(ncacn_conn);
                        close(s);
                        return;
@@ -1026,7 +1028,7 @@ void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
 
        switch (transport) {
                case NCACN_IP_TCP:
-                       pipe_name = tsocket_address_string(ncacn_conn->client,
+                       pipe_name = tsocket_address_string(ncacn_conn->remote_client_addr,
                                                           ncacn_conn);
                        if (pipe_name == NULL) {
                                close(s);
@@ -1036,14 +1038,38 @@ void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
 
                        break;
                case NCALRPC:
-                       rc = sys_getpeereid(s, &uid);
+                       rc = getpeereid(s, &uid, &gid);
                        if (rc < 0) {
-                               DEBUG(2, ("Failed to get ncalrpc connecting uid!"));
+                               DEBUG(2, ("Failed to get ncalrpc connecting "
+                                         "uid - %s!\n", strerror(errno)));
                        } else {
                                if (uid == sec_initial_uid()) {
-                                       system_user = true;
+                                       TALLOC_FREE(ncacn_conn->remote_client_addr);
+
+                                       rc = tsocket_address_unix_from_path(ncacn_conn,
+                                                                           AS_SYSTEM_MAGIC_PATH_TOKEN,
+                                                                           &ncacn_conn->remote_client_addr);
+                                       if (rc < 0) {
+                                               DEBUG(0, ("Out of memory building magic ncalrpc_as_system path!\n"));
+                                               talloc_free(ncacn_conn);
+                                               close(s);
+                                               return;
+                                       }
+
+                                       TALLOC_FREE(ncacn_conn->remote_client_name);
+                                       ncacn_conn->remote_client_name
+                                               = tsocket_address_unix_path(ncacn_conn->remote_client_addr,
+                                                                           ncacn_conn);
+                                       if (ncacn_conn->remote_client_name == NULL) {
+                                               DEBUG(0, ("Out of memory getting magic ncalrpc_as_system string!\n"));
+                                               talloc_free(ncacn_conn);
+                                               close(s);
+                                               return;
+                                       }
                                }
                        }
+
+                       FALL_THROUGH;
                case NCACN_NP:
                        pipe_name = talloc_strdup(ncacn_conn,
                                                  name);
@@ -1082,11 +1108,11 @@ void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
        }
 
        if (ncacn_conn->session_info == NULL) {
-               status = auth_anonymous_session_info(ncacn_conn,
+               status = make_session_info_anonymous(ncacn_conn,
                                                     &ncacn_conn->session_info);
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(2, ("Failed to create "
-                                 "auth_anonymous_session_info - %s\n",
+                                 "make_session_info_anonymous - %s\n",
                                  nt_errstr(status)));
                        talloc_free(ncacn_conn);
                        return;
@@ -1097,9 +1123,8 @@ void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
                                      ncacn_conn->msg_ctx,
                                      pipe_name,
                                      ncacn_conn->transport,
-                                     system_user,
-                                     ncacn_conn->server,
-                                     ncacn_conn->client,
+                                     ncacn_conn->remote_client_addr,
+                                     ncacn_conn->local_server_addr,
                                      ncacn_conn->session_info,
                                      &ncacn_conn->p,
                                      &sys_errno);
@@ -1113,7 +1138,7 @@ void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
        ncacn_conn->send_queue = tevent_queue_create(ncacn_conn,
                                                        "dcerpc send queue");
        if (ncacn_conn->send_queue == NULL) {
-               DEBUG(0, ("Out of memory!\n"));
+               DEBUG(0, ("Out of memory building dcerpc send queue!\n"));
                talloc_free(ncacn_conn);
                return;
        }
@@ -1142,10 +1167,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;
-       char *data;
        NTSTATUS status;
        bool ok;
 
@@ -1161,22 +1183,20 @@ static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
                goto fail;
        }
 
-       data_left = recv_buffer.length;
-       data = (char *) recv_buffer.data;
-
-       while (data_left) {
-               data_used = process_incoming_data(ncacn_conn->p, data, data_left);
-               if (data_used < 0) {
-                       DEBUG(3, ("Failed to process dcerpc request!\n"));
-                       status = NT_STATUS_UNEXPECTED_IO_ERROR;
-                       goto fail;
-               }
-
-               data_left -= data_used;
-               data += data_used;
+       /* dcerpc_read_ncacn_packet_recv() returns a full PDU */
+       ncacn_conn->p->in_data.pdu_needed_len = 0;
+       ncacn_conn->p->in_data.pdu = recv_buffer;
+       if (dcerpc_get_endian_flag(&recv_buffer) & DCERPC_DREP_LE) {
+               ncacn_conn->p->endian = RPC_LITTLE_ENDIAN;
+       } else {
+               ncacn_conn->p->endian = RPC_BIG_ENDIAN;
        }
+       DEBUG(10, ("PDU is in %s Endian format!\n",
+                  ncacn_conn->p->endian ? "Big" : "Little"));
+       process_complete_pdu(ncacn_conn->p, pkt);
 
-       /* Do not leak this buffer */
+       /* reset pipe state and free PDU */
+       ncacn_conn->p->in_data.pdu.length = 0;
        talloc_free(recv_buffer.data);
        talloc_free(pkt);
 
@@ -1252,7 +1272,7 @@ static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
                                                       ncacn_conn->ev_ctx,
                                                       ncacn_conn->tstream);
                if (subreq == NULL) {
-                       DEBUG(2, ("Failed to start receving packets\n"));
+                       DEBUG(2, ("Failed to start receiving packets\n"));
                        status = NT_STATUS_NO_MEMORY;
                        goto fail;
                }
@@ -1280,7 +1300,7 @@ static void dcerpc_ncacn_packet_process(struct tevent_req *subreq)
 
 fail:
        DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
-                 ncacn_conn->client_name, nt_errstr(status)));
+                 ncacn_conn->remote_client_name, nt_errstr(status)));
 
        /* Terminate client connection */
        talloc_free(ncacn_conn);
@@ -1303,6 +1323,12 @@ static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
                goto fail;
        }
 
+       if (ncacn_conn->p->fault_state != 0) {
+               DEBUG(2, ("Disconnect after fault\n"));
+               sys_errno = EINVAL;
+               goto fail;
+       }
+
        /* clear out any data that may have been left around */
        ncacn_conn->count = 0;
        TALLOC_FREE(ncacn_conn->iov);
@@ -1317,7 +1343,7 @@ static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
                                               ncacn_conn->ev_ctx,
                                               ncacn_conn->tstream);
        if (subreq == NULL) {
-               DEBUG(2, ("Failed to start receving packets\n"));
+               DEBUG(2, ("Failed to start receiving packets\n"));
                status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
@@ -1327,7 +1353,7 @@ static void dcerpc_ncacn_packet_done(struct tevent_req *subreq)
 
 fail:
        DEBUG(3, ("Terminating client(%s) connection! - '%s'\n",
-                 ncacn_conn->client_name, nt_errstr(status)));
+                 ncacn_conn->remote_client_name, nt_errstr(status)));
 
        /* Terminate client connection */
        talloc_free(ncacn_conn);