s3-rpc_server: Add named_pipe_client_init() function.
[nivanova/samba-autobuild/.git] / source3 / rpc_server / rpc_server.c
index c66d74771a77db0df509ed33b6dcd025e869c9cf..f283559dddd4e6d6966453abba1cba01938f64d4 100644 (file)
@@ -22,6 +22,7 @@
 #include "includes.h"
 #include "rpc_server/rpc_pipes.h"
 #include "rpc_server/rpc_server.h"
+#include "rpc_server/rpc_config.h"
 #include "rpc_dce.h"
 #include "librpc/gen_ndr/netlogon.h"
 #include "librpc/gen_ndr/auth.h"
@@ -51,19 +52,18 @@ static NTSTATUS auth_anonymous_session_info(TALLOC_CTX *mem_ctx,
 
 /* 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,
+                            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 pipes_struct *p;
-       NTSTATUS status;
        int ret;
 
        ret = make_base_pipes_struct(mem_ctx, msg_ctx, pipe_name,
@@ -80,58 +80,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;
@@ -160,28 +111,10 @@ static void named_pipe_listener(struct tevent_context *ev,
                                uint16_t flags,
                                void *private_data);
 
-bool setup_named_pipe_socket(const char *pipe_name,
-                            struct tevent_context *ev_ctx,
-                            struct messaging_context *msg_ctx)
+int create_named_pipe_socket(const char *pipe_name)
 {
-       struct dcerpc_ncacn_listen_state *state;
-       struct tevent_fd *fde;
-       char *np_dir;
-
-       state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
-       if (!state) {
-               DEBUG(0, ("Out of memory\n"));
-               return false;
-       }
-       state->ep.name = talloc_strdup(state, pipe_name);
-       if (state->ep.name == NULL) {
-               DEBUG(0, ("Out of memory\n"));
-               goto out;
-       }
-       state->fd = -1;
-
-       state->ev_ctx = ev_ctx;
-       state->msg_ctx = msg_ctx;
+       char *np_dir = NULL;
+       int fd = -1;
 
        /*
         * As lp_ncalrpc_dir() should have 0755, but
@@ -194,25 +127,64 @@ bool setup_named_pipe_socket(const char *pipe_name,
                goto out;
        }
 
-       np_dir = talloc_asprintf(state, "%s/np", lp_ncalrpc_dir());
+       np_dir = talloc_asprintf(talloc_tos(), "%s/np", lp_ncalrpc_dir());
        if (!np_dir) {
                DEBUG(0, ("Out of memory\n"));
                goto out;
        }
 
-       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;
        }
 
-       state->fd = create_pipe_sock(np_dir, pipe_name, 0700);
-       if (state->fd == -1) {
+       fd = create_pipe_sock(np_dir, pipe_name, 0700);
+       if (fd == -1) {
                DEBUG(0, ("Failed to create pipe socket! [%s/%s]\n",
                          np_dir, pipe_name));
                goto out;
        }
+
+       DEBUG(10, ("Openened pipe socket fd %d for %s\n", fd, pipe_name));
+
+out:
        talloc_free(np_dir);
+       return fd;
+}
+
+bool setup_named_pipe_socket(const char *pipe_name,
+                            struct tevent_context *ev_ctx,
+                            struct messaging_context *msg_ctx)
+{
+       struct dcerpc_ncacn_listen_state *state;
+       struct tevent_fd *fde;
+       int rc;
+
+       state = talloc(ev_ctx, struct dcerpc_ncacn_listen_state);
+       if (!state) {
+               DEBUG(0, ("Out of memory\n"));
+               return false;
+       }
+       state->ep.name = talloc_strdup(state, pipe_name);
+       if (state->ep.name == NULL) {
+               DEBUG(0, ("Out of memory\n"));
+               goto out;
+       }
+       state->fd = create_named_pipe_socket(pipe_name);
+       if (state->fd == -1) {
+               goto out;
+       }
+
+       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",
                   state->fd, pipe_name));
@@ -236,11 +208,6 @@ out:
        return false;
 }
 
-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,
                                uint16_t flags,
@@ -273,7 +240,7 @@ static void named_pipe_listener(struct tevent_context *ev,
        named_pipe_accept_function(state->ev_ctx,
                                   state->msg_ctx,
                                   state->ep.name,
-                                  sd);
+                                  sd, NULL, 0);
 }
 
 
@@ -281,39 +248,59 @@ 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;
+static int named_pipe_destructor(struct named_pipe_client *npc)
+{
+       if (npc->term_fn) {
+               npc->term_fn(npc->private_data);
+       }
+       return 0;
+}
 
-       struct tstream_context *tstream;
+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;
 
-       struct tsocket_address *client;
-       char *client_name;
-       struct tsocket_address *server;
-       char *server_name;
+       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);
 
-       struct auth_session_info *session_info;
+       npc->pipe_name = talloc_strdup(npc, pipe_name);
+       if (npc->pipe_name == NULL) {
+               DEBUG(0, ("Out of memory!\n"));
+               talloc_free(npc);
+               return NULL;
+       }
 
-       struct pipes_struct *p;
+       npc->ev = ev_ctx;
+       npc->msg_ctx = msg_ctx;
+       npc->term_fn = term_fn;
+       npc->private_data = private_data;
 
-       struct tevent_queue *write_queue;
+       npc->file_type = file_type;
+       npc->device_state = device_state;
+       npc->allocation_size = allocation_size;
 
-       struct iovec *iov;
-       size_t count;
-};
+       return npc;
+}
 
 static void named_pipe_accept_done(struct tevent_req *subreq);
 
-static void named_pipe_accept_function(struct tevent_context *ev_ctx,
-                                      struct messaging_context *msg_ctx,
-                                      const char *pipe_name,
-                                      int fd)
+void named_pipe_accept_function(struct tevent_context *ev_ctx,
+                               struct messaging_context *msg_ctx,
+                               const char *pipe_name, int fd,
+                               named_pipe_termination_fn *term_fn,
+                               void *private_data)
 {
        struct named_pipe_client *npc;
        struct tstream_context *plain;
@@ -336,6 +323,10 @@ static void named_pipe_accept_function(struct tevent_context *ev_ctx,
        }
        npc->ev = ev_ctx;
        npc->msg_ctx = msg_ctx;
+       npc->term_fn = term_fn;
+       npc->private_data = private_data;
+
+       talloc_set_destructor(npc, named_pipe_destructor);
 
        /* make sure socket is in NON blocking state */
        ret = set_blocking(fd, false);
@@ -371,7 +362,6 @@ static 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)
@@ -434,7 +424,7 @@ fail:
        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);
@@ -446,6 +436,7 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
        ssize_t data_used;
        char *data;
        uint32_t to_send;
+       size_t i;
        bool ok;
 
        status = dcerpc_read_ncacn_packet_recv(subreq, npc, &pkt, &recv_buffer);
@@ -479,13 +470,6 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
        to_send = out->frag.length - out->current_pdu_sent;
        if (to_send > 0) {
 
-               DEBUG(10, ("Current_pdu_len = %u, "
-                          "current_pdu_sent = %u "
-                          "Returning %u bytes\n",
-                          (unsigned int)out->frag.length,
-                          (unsigned int)out->current_pdu_sent,
-                          (unsigned int)to_send));
-
                npc->iov = talloc_zero(npc, struct iovec);
                if (!npc->iov) {
                        status = NT_STATUS_NO_MEMORY;
@@ -522,11 +506,6 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
                npc->iov[npc->count].iov_base = out->frag.data;
                npc->iov[npc->count].iov_len = out->frag.length;
 
-               DEBUG(10, ("PDU number: %d, PDU Length: %u\n",
-                          (unsigned int)npc->count,
-                          (unsigned int)npc->iov[npc->count].iov_len));
-               dump_data(11, (const uint8_t *)npc->iov[npc->count].iov_base,
-                               npc->iov[npc->count].iov_len);
                npc->count++;
        }
 
@@ -544,19 +523,31 @@ static void named_pipe_packet_process(struct tevent_req *subreq)
                return;
        }
 
-       DEBUG(10, ("Sending a total of %u bytes\n",
+       DEBUG(10, ("Sending %u fragments in a total of %u bytes\n",
+                  (unsigned int)npc->count,
                   (unsigned int)npc->p->out_data.data_sent_length));
 
-       subreq = tstream_writev_queue_send(npc, npc->ev,
-                                          npc->tstream,
-                                          npc->write_queue,
-                                          npc->iov, npc->count);
-       if (!subreq) {
-               DEBUG(2, ("Failed to send packet\n"));
-               status = NT_STATUS_NO_MEMORY;
-               goto fail;
+       for (i = 0; i < npc->count; i++) {
+               DEBUG(10, ("Sending PDU number: %d, PDU Length: %u\n",
+                         (unsigned int)i,
+                         (unsigned int)npc->iov[i].iov_len));
+               dump_data(11, (const uint8_t *)npc->iov[i].iov_base,
+                               npc->iov[i].iov_len);
+
+               subreq = tstream_writev_queue_send(npc,
+                                                  npc->ev,
+                                                  npc->tstream,
+                                                  npc->write_queue,
+                                                  (npc->iov + i),
+                                                  1);
+               if (!subreq) {
+                       DEBUG(2, ("Failed to send packet\n"));
+                       status = NT_STATUS_NO_MEMORY;
+                       goto fail;
+               }
+               tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
        }
-       tevent_req_set_callback(subreq, named_pipe_packet_done, npc);
+
        return;
 
 fail:
@@ -582,6 +573,10 @@ static void named_pipe_packet_done(struct tevent_req *subreq)
                goto fail;
        }
 
+       if (tevent_queue_length(npc->write_queue) > 0) {
+               return;
+       }
+
        /* clear out any data that may have been left around */
        npc->count = 0;
        TALLOC_FREE(npc->iov);
@@ -610,15 +605,6 @@ fail:
        return;
 }
 
-static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
-                               struct messaging_context *msg_ctx,
-                               enum dcerpc_transport_t transport,
-                               const char *name,
-                               struct tsocket_address *cli_addr,
-                               struct tsocket_address *srv_addr,
-                               int s,
-                               dcerpc_ncacn_disconnect_fn fn);
-
 /********************************************************************
  * Start listening on the tcp/ip socket
  ********************************************************************/
@@ -641,7 +627,7 @@ int create_tcpip_socket(const struct sockaddr_storage *ifss, uint16_t *port)
                                            0,
                                            ifss,
                                            false);
-                       if (fd > 0) {
+                       if (fd >= 0) {
                                *port = i;
                                break;
                        }
@@ -798,6 +784,32 @@ static void dcerpc_ncalrpc_listener(struct tevent_context *ev,
                                    uint16_t flags,
                                    void *private_data);
 
+int create_dcerpc_ncalrpc_socket(const char *name)
+{
+       int fd = -1;
+
+       if (name == NULL) {
+               name = "DEFAULT";
+       }
+
+       if (!directory_create_or_exist(lp_ncalrpc_dir(), geteuid(), 0755)) {
+               DEBUG(0, ("Failed to create ncalrpc directory %s - %s\n",
+                         lp_ncalrpc_dir(), strerror(errno)));
+               return -1;
+       }
+
+       fd = create_pipe_sock(lp_ncalrpc_dir(), name, 0755);
+       if (fd == -1) {
+               DEBUG(0, ("Failed to create ncalrpc socket! [%s/%s]\n",
+                         lp_ncalrpc_dir(), name));
+               return -1;
+       }
+
+       DEBUG(10, ("Openened ncalrpc socket fd %d for %s\n", fd, name));
+
+       return fd;
+}
+
 bool setup_dcerpc_ncalrpc_socket(struct tevent_context *ev_ctx,
                                 struct messaging_context *msg_ctx,
                                 const char *name,
@@ -805,6 +817,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) {
@@ -818,29 +831,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;
 
@@ -940,14 +950,14 @@ struct dcerpc_ncacn_conn {
 static void dcerpc_ncacn_packet_process(struct tevent_req *subreq);
 static void dcerpc_ncacn_packet_done(struct tevent_req *subreq);
 
-static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
-                               struct messaging_context *msg_ctx,
-                               enum dcerpc_transport_t transport,
-                               const char *name,
-                               struct tsocket_address *cli_addr,
-                               struct tsocket_address *srv_addr,
-                               int s,
-                               dcerpc_ncacn_disconnect_fn fn) {
+void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
+                        struct messaging_context *msg_ctx,
+                        enum dcerpc_transport_t transport,
+                        const char *name,
+                        struct tsocket_address *cli_addr,
+                        struct tsocket_address *srv_addr,
+                        int s,
+                        dcerpc_ncacn_disconnect_fn fn) {
        struct dcerpc_ncacn_conn *ncacn_conn;
        struct tevent_req *subreq;
        bool system_user = false;
@@ -955,6 +965,7 @@ static void dcerpc_ncacn_accept(struct tevent_context *ev_ctx,
        NTSTATUS status;
        int sys_errno;
        uid_t uid;
+       gid_t gid;
        int rc;
 
        DEBUG(10, ("dcerpc_ncacn_accept\n"));
@@ -1015,14 +1026,16 @@ static 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;
                                }
                        }
+                       /* FALL TROUGH */
                case NCACN_NP:
                        pipe_name = talloc_strdup(ncacn_conn,
                                                  name);