s3-rpc_server: Fix include order in srv_pipe_hnd.c.
[idra/samba.git] / source3 / rpc_server / srv_pipe_hnd.c
index df534893835ab9f1b50066ac3d20703909d3d221..3990ae9bcdb6bdadaa0d0de5def5e4c5522d06c0 100644 (file)
  */
 
 #include "includes.h"
-#include "../librpc/gen_ndr/srv_spoolss.h"
-#include "librpc/gen_ndr/ndr_named_pipe_auth.h"
-#include "../libcli/named_pipe_auth/npa_tstream.h"
-#include "rpc_server.h"
-#include "smbd/globals.h"
 #include "fake_file.h"
 #include "rpc_dce.h"
+#include "ntdomain.h"
+#include "rpc_server/rpc_ncacn_np.h"
+#include "rpc_server/srv_pipe_hnd.h"
+#include "rpc_server/srv_pipe.h"
+#include "rpc_server/rpc_server.h"
+#include "../lib/tsocket/tsocket.h"
+#include "../lib/util/tevent_ntstatus.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_SRV
@@ -35,7 +37,7 @@
  Ensures we have at least RPC_HEADER_LEN amount of data in the incoming buffer.
 ****************************************************************************/
 
-static ssize_t fill_rpc_header(struct pipes_struct *p, char *data, size_t data_to_copy)
+static ssize_t fill_rpc_header(struct pipes_struct *p, const char *data, size_t data_to_copy)
 {
        size_t len_needed_to_complete_hdr =
                MIN(data_to_copy, RPC_HEADER_LEN - p->in_data.pdu.length);
@@ -124,7 +126,7 @@ static void free_pipe_context(struct pipes_struct *p)
  Accepts incoming data on an rpc pipe. Processes the data in pdu sized units.
 ****************************************************************************/
 
-ssize_t process_incoming_data(struct pipes_struct *p, char *data, size_t n)
+ssize_t process_incoming_data(struct pipes_struct *p, const char *data, size_t n)
 {
        size_t data_to_copy = MIN(n, RPC_MAX_PDU_FRAG_LEN
                                        - p->in_data.pdu.length);
@@ -229,7 +231,7 @@ ssize_t process_incoming_data(struct pipes_struct *p, char *data, size_t n)
  Accepts incoming data on an internal rpc pipe.
 ****************************************************************************/
 
-static ssize_t write_to_internal_pipe(struct pipes_struct *p, char *data, size_t n)
+static ssize_t write_to_internal_pipe(struct pipes_struct *p, const char *data, size_t n)
 {
        size_t data_left = n;
 
@@ -278,7 +280,7 @@ static ssize_t read_from_internal_pipe(struct pipes_struct *p, char *data,
        }
 
        DEBUG(6,(" name: %s len: %u\n",
-                get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
+                get_pipe_name_from_syntax(talloc_tos(), &p->contexts->syntax),
                 (unsigned int)n));
 
        /*
@@ -296,7 +298,7 @@ static ssize_t read_from_internal_pipe(struct pipes_struct *p, char *data,
                 DEBUG(5,("read_from_pipe: too large read (%u) requested on "
                         "pipe %s. We can only service %d sized reads.\n",
                         (unsigned int)n,
-                        get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
+                        get_pipe_name_from_syntax(talloc_tos(), &p->contexts->syntax),
                         RPC_MAX_PDU_FRAG_LEN ));
                n = RPC_MAX_PDU_FRAG_LEN;
        }
@@ -317,7 +319,7 @@ static ssize_t read_from_internal_pipe(struct pipes_struct *p, char *data,
 
                DEBUG(10,("read_from_pipe: %s: current_pdu_len = %u, "
                          "current_pdu_sent = %u returning %d bytes.\n",
-                         get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
+                         get_pipe_name_from_syntax(talloc_tos(), &p->contexts->syntax),
                          (unsigned int)p->out_data.frag.length,
                          (unsigned int)p->out_data.current_pdu_sent,
                          (int)data_returned));
@@ -338,7 +340,7 @@ static ssize_t read_from_internal_pipe(struct pipes_struct *p, char *data,
 
        DEBUG(10,("read_from_pipe: %s: fault_state = %d : data_sent_length "
                  "= %u, p->out_data.rdata.length = %u.\n",
-                 get_pipe_name_from_syntax(talloc_tos(), &p->syntax),
+                 get_pipe_name_from_syntax(talloc_tos(), &p->contexts->syntax),
                  (int)p->fault_state,
                  (unsigned int)p->out_data.data_sent_length,
                  (unsigned int)p->out_data.rdata.length));
@@ -360,7 +362,7 @@ static ssize_t read_from_internal_pipe(struct pipes_struct *p, char *data,
 
        if(!create_next_pdu(p)) {
                DEBUG(0,("read_from_pipe: %s: create_next_pdu failed.\n",
-                        get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
+                        get_pipe_name_from_syntax(talloc_tos(), &p->contexts->syntax)));
                return -1;
        }
 
@@ -406,146 +408,17 @@ bool fsp_is_np(struct files_struct *fsp)
                || (type == FAKE_FILE_TYPE_NAMED_PIPE_PROXY));
 }
 
-struct np_proxy_state {
-       uint16_t file_type;
-       uint16_t device_state;
-       uint64_t allocation_size;
-       struct tstream_context *npipe;
-       struct tevent_queue *read_queue;
-       struct tevent_queue *write_queue;
-};
-
-static struct np_proxy_state *make_external_rpc_pipe_p(TALLOC_CTX *mem_ctx,
-                               const char *pipe_name,
-                               const struct tsocket_address *local_address,
-                               const struct tsocket_address *remote_address,
-                               struct auth_serversupplied_info *server_info)
-{
-       struct np_proxy_state *result;
-       char *socket_np_dir;
-       const char *socket_dir;
-       struct tevent_context *ev;
-       struct tevent_req *subreq;
-       struct netr_SamInfo3 *info3;
-       NTSTATUS status;
-       bool ok;
-       int ret;
-       int sys_errno;
-
-       result = talloc(mem_ctx, struct np_proxy_state);
-       if (result == NULL) {
-               DEBUG(0, ("talloc failed\n"));
-               return NULL;
-       }
-
-       result->read_queue = tevent_queue_create(result, "np_read");
-       if (result->read_queue == NULL) {
-               DEBUG(0, ("tevent_queue_create failed\n"));
-               goto fail;
-       }
-
-       result->write_queue = tevent_queue_create(result, "np_write");
-       if (result->write_queue == NULL) {
-               DEBUG(0, ("tevent_queue_create failed\n"));
-               goto fail;
-       }
-
-       ev = s3_tevent_context_init(talloc_tos());
-       if (ev == NULL) {
-               DEBUG(0, ("s3_tevent_context_init failed\n"));
-               goto fail;
-       }
-
-       socket_dir = lp_parm_const_string(
-               GLOBAL_SECTION_SNUM, "external_rpc_pipe", "socket_dir",
-               get_dyn_NCALRPCDIR());
-       if (socket_dir == NULL) {
-               DEBUG(0, ("externan_rpc_pipe:socket_dir not set\n"));
-               goto fail;
-       }
-       socket_np_dir = talloc_asprintf(talloc_tos(), "%s/np", socket_dir);
-       if (socket_np_dir == NULL) {
-               DEBUG(0, ("talloc_asprintf failed\n"));
-               goto fail;
-       }
-
-       info3 = talloc_zero(talloc_tos(), struct netr_SamInfo3);
-       if (info3 == NULL) {
-               DEBUG(0, ("talloc failed\n"));
-               goto fail;
-       }
-
-       status = serverinfo_to_SamInfo3(server_info, NULL, 0, info3);
-       if (!NT_STATUS_IS_OK(status)) {
-               TALLOC_FREE(info3);
-               DEBUG(0, ("serverinfo_to_SamInfo3 failed: %s\n",
-                         nt_errstr(status)));
-               goto fail;
-       }
-
-       become_root();
-       subreq = tstream_npa_connect_send(talloc_tos(), ev,
-                                         socket_np_dir,
-                                         pipe_name,
-                                         remote_address, /* client_addr */
-                                         NULL, /* client_name */
-                                         local_address, /* server_addr */
-                                         NULL, /* server_name */
-                                         info3,
-                                         server_info->user_session_key,
-                                         data_blob_null /* delegated_creds */);
-       if (subreq == NULL) {
-               unbecome_root();
-               DEBUG(0, ("tstream_npa_connect_send to %s for pipe %s and "
-                         "user %s\\%s failed\n",
-                         socket_np_dir, pipe_name, info3->base.domain.string,
-                         info3->base.account_name.string));
-               goto fail;
-       }
-       ok = tevent_req_poll(subreq, ev);
-       unbecome_root();
-       if (!ok) {
-               DEBUG(0, ("tevent_req_poll to %s for pipe %s and user %s\\%s "
-                         "failed for tstream_npa_connect: %s\n",
-                         socket_np_dir, pipe_name, info3->base.domain.string,
-                         info3->base.account_name.string,
-                         strerror(errno)));
-               goto fail;
-
-       }
-       ret = tstream_npa_connect_recv(subreq, &sys_errno,
-                                      result,
-                                      &result->npipe,
-                                      &result->file_type,
-                                      &result->device_state,
-                                      &result->allocation_size);
-       TALLOC_FREE(subreq);
-       if (ret != 0) {
-               DEBUG(0, ("tstream_npa_connect_recv  to %s for pipe %s and "
-                         "user %s\\%s failed: %s\n",
-                         socket_np_dir, pipe_name, info3->base.domain.string,
-                         info3->base.account_name.string,
-                         strerror(sys_errno)));
-               goto fail;
-       }
-
-       return result;
-
- fail:
-       TALLOC_FREE(result);
-       return NULL;
-}
-
 NTSTATUS np_open(TALLOC_CTX *mem_ctx, const char *name,
                 const struct tsocket_address *local_address,
                 const struct tsocket_address *remote_address,
-                struct client_address *client_id,
-                struct auth_serversupplied_info *server_info,
+                struct auth_session_info *session_info,
                 struct messaging_context *msg_ctx,
                 struct fake_file_handle **phandle)
 {
+       const char *rpcsrv_type;
        const char **proxy_list;
        struct fake_file_handle *handle;
+       bool external = false;
 
        proxy_list = lp_parm_string_list(-1, "np", "proxy", NULL);
 
@@ -554,13 +427,27 @@ NTSTATUS np_open(TALLOC_CTX *mem_ctx, const char *name,
                return NT_STATUS_NO_MEMORY;
        }
 
+       /* Check what is the server type for this pipe.
+          Defaults to "embedded" */
+       rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
+                                          "rpc_server", name,
+                                          "embedded");
+       if (strcasecmp_m(rpcsrv_type, "embedded") != 0) {
+               external = true;
+       }
+
+       /* Still support the old method for defining external servers */
        if ((proxy_list != NULL) && str_list_check_ci(proxy_list, name)) {
+               external = true;
+       }
+
+       if (external) {
                struct np_proxy_state *p;
 
                p = make_external_rpc_pipe_p(handle, name,
                                             local_address,
                                             remote_address,
-                                            server_info);
+                                            session_info);
 
                handle->type = FAKE_FILE_TYPE_NAMED_PIPE_PROXY;
                handle->private_data = p;
@@ -573,8 +460,8 @@ NTSTATUS np_open(TALLOC_CTX *mem_ctx, const char *name,
                        return NT_STATUS_OBJECT_NAME_NOT_FOUND;
                }
 
-               p = make_internal_rpc_pipe_p(handle, &syntax, client_id,
-                                            server_info, msg_ctx);
+               p = make_internal_rpc_pipe_p(handle, &syntax, remote_address,
+                                            session_info, msg_ctx);
 
                handle->type = FAKE_FILE_TYPE_NAMED_PIPE;
                handle->private_data = p;
@@ -647,7 +534,7 @@ struct tevent_req *np_write_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
                struct pipes_struct *p = talloc_get_type_abort(
                        handle->private_data, struct pipes_struct);
 
-               state->nwritten = write_to_internal_pipe(p, (char *)data, len);
+               state->nwritten = write_to_internal_pipe(p, (const char *)data, len);
 
                status = (state->nwritten >= 0)
                        ? NT_STATUS_OK : NT_STATUS_UNEXPECTED_IO_ERROR;
@@ -661,7 +548,7 @@ struct tevent_req *np_write_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
 
                state->ev = ev;
                state->p = p;
-               state->iov.iov_base = CONST_DISCARD(void *, data);
+               state->iov.iov_base = discard_const_p(void, data);
                state->iov.iov_len = len;
 
                subreq = tstream_writev_queue_send(state, ev,
@@ -903,149 +790,3 @@ NTSTATUS np_read_recv(struct tevent_req *req, ssize_t *nread,
        *is_data_outstanding = state->is_data_outstanding;
        return NT_STATUS_OK;
 }
-
-
-static NTSTATUS rpc_pipe_open_external(TALLOC_CTX *mem_ctx,
-                               const char *pipe_name,
-                               const struct ndr_syntax_id *abstract_syntax,
-                               struct auth_serversupplied_info *server_info,
-                               struct rpc_pipe_client **_result)
-{
-       struct tsocket_address *local, *remote;
-       struct rpc_pipe_client *result = NULL;
-       struct np_proxy_state *proxy_state = NULL;
-       struct pipe_auth_data *auth;
-       NTSTATUS status;
-       int ret;
-
-       /* this is an internal connection, fake up ip addresses */
-       ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
-                                               NULL, 0, &local);
-       if (ret) {
-               return NT_STATUS_NO_MEMORY;
-       }
-       ret = tsocket_address_inet_from_strings(talloc_tos(), "ip",
-                                               NULL, 0, &remote);
-       if (ret) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       proxy_state = make_external_rpc_pipe_p(mem_ctx, pipe_name,
-                                               local, remote, server_info);
-       if (!proxy_state) {
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       result = talloc_zero(mem_ctx, struct rpc_pipe_client);
-       if (result == NULL) {
-               status = NT_STATUS_NO_MEMORY;
-               goto done;
-       }
-
-       result->abstract_syntax = *abstract_syntax;
-       result->transfer_syntax = ndr_transfer_syntax;
-
-       result->desthost = get_myname(result);
-       result->srv_name_slash = talloc_asprintf_strupper_m(
-               result, "\\\\%s", result->desthost);
-       if ((result->desthost == NULL) || (result->srv_name_slash == NULL)) {
-               status = NT_STATUS_NO_MEMORY;
-               goto done;
-       }
-
-       result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
-       result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
-
-       status = rpc_transport_tstream_init(result,
-                                           proxy_state->npipe,
-                                           proxy_state->read_queue,
-                                           proxy_state->write_queue,
-                                           &result->transport);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto done;
-       }
-
-       result->auth = talloc_zero(result, struct pipe_auth_data);
-       if (!result->auth) {
-               status = NT_STATUS_NO_MEMORY;
-               goto done;
-       }
-       result->auth->auth_type = DCERPC_AUTH_TYPE_NONE;
-       result->auth->auth_level = DCERPC_AUTH_LEVEL_NONE;
-
-       status = rpccli_anon_bind_data(result, &auth);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("Failed to initialize anonymous bind.\n"));
-               goto done;
-       }
-
-       status = rpc_pipe_bind(result, auth);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("Failed to bind spoolss pipe.\n"));
-               goto done;
-       }
-done:
-       if (!NT_STATUS_IS_OK(status)) {
-               TALLOC_FREE(result);
-       }
-       TALLOC_FREE(proxy_state);
-       *_result = result;
-       return status;
-}
-
-/**
- * @brief Create a new RPC client context which uses a local dispatch function.
- *
- * @param[in]  conn  The connection struct that will hold the pipe
- *
- * @param[out] spoolss_pipe  A pointer to the connected rpc client pipe.
- *
- * @return              NT_STATUS_OK on success, a corresponding NT status if an
- *                      error occured.
- */
-NTSTATUS rpc_connect_spoolss_pipe(connection_struct *conn,
-                                 struct rpc_pipe_client **spoolss_pipe)
-{
-       const char *server_type;
-       NTSTATUS status;
-
-       DEBUG(10, ("Connecting to spoolss pipe.\n"));
-       *spoolss_pipe = NULL;
-
-       if (rpccli_is_connected(conn->spoolss_pipe)) {
-               *spoolss_pipe = conn->spoolss_pipe;
-               return NT_STATUS_OK;
-       } else {
-               TALLOC_FREE(conn->spoolss_pipe);
-       }
-
-       server_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
-                                          "rpc_server", "spoolss",
-                                          "embedded");
-       if (StrCaseCmp(server_type, "embedded") == 0) {
-               status = rpc_pipe_open_internal(conn,
-                                               &ndr_table_spoolss.syntax_id,
-                                               conn->server_info,
-                                               &conn->sconn->client_id,
-                                               conn->sconn->msg_ctx,
-                                               &conn->spoolss_pipe);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
-       } else {
-               /* It would be nice to just use rpc_pipe_open_ncalrpc() but
-                * for now we need to use the special proxy setup to connect
-                * to spoolssd. */
-
-               status = rpc_pipe_open_external(conn, "spoolss",
-                                               &ndr_table_spoolss.syntax_id,
-                                               conn->server_info,
-                                               &conn->spoolss_pipe);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
-       }
-
-       *spoolss_pipe = conn->spoolss_pipe;
-       return NT_STATUS_OK;
-}