s4:rpc_server: add dcesrv_iface_state_{store,find}_{assoc,conn}() helpers
[samba.git] / source4 / rpc_server / service_rpc.c
index ddcf1c2a999166172aec47e42e207786a3c57c26..6cd7d417c19b23b14a98eeeb5fc58f28f7a83075 100644 (file)
 #include "includes.h"
 #include "librpc/gen_ndr/ndr_dcerpc.h"
 #include "auth/auth.h"
-#include "auth/gensec/gensec.h"
-#include "lib/util/dlinklist.h"
+#include "../lib/util/dlinklist.h"
 #include "rpc_server/dcerpc_server.h"
-#include "lib/events/events.h"
-#include "smbd/service_task.h"
-#include "smbd/service_stream.h"
-#include "smbd/service.h"
+#include "rpc_server/dcerpc_server_proto.h"
 #include "system/filesys.h"
-#include "libcli/security/security.h"
-#include "lib/socket/socket.h"
 #include "lib/messaging/irpc.h"
 #include "system/network.h"
 #include "lib/socket/netif.h"
-#include "build.h"
 #include "param/param.h"
-
-struct dcesrv_socket_context {
-       const struct dcesrv_endpoint *endpoint;
-       struct dcesrv_context *dcesrv_ctx;
-};
+#include "../lib/tsocket/tsocket.h"
+#include "librpc/rpc/dcerpc_proto.h"
+#include "../lib/util/tevent_ntstatus.h"
+#include "libcli/raw/smb.h"
+#include "../libcli/named_pipe_auth/npa_tstream.h"
+#include "smbd/process_model.h"
 
 /*
-  write_fn callback for dcesrv_output()
-*/
-static NTSTATUS dcerpc_write_fn(void *private_data, DATA_BLOB *out, size_t *nwritten)
-{
-       NTSTATUS status;
-       struct socket_context *sock = talloc_get_type(private_data, struct socket_context);
-       size_t sendlen;
-
-       status = socket_send(sock, out, &sendlen);
-       NT_STATUS_IS_ERR_RETURN(status);
-
-       *nwritten = sendlen;
-       return status;
-}
-
-static void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn, const char *reason)
-{
-       struct stream_connection *srv_conn;
-       srv_conn = talloc_get_type(dce_conn->transport.private_data,
-                                  struct stream_connection);
-
-       stream_terminate_connection(srv_conn, reason);
-}
-
-static void dcesrv_sock_report_output_data(struct dcesrv_connection *dcesrv_conn)
-{
-       struct stream_connection *srv_conn;
-       srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
-                                  struct stream_connection);
-
-       if (srv_conn && srv_conn->event.fde) {
-               EVENT_FD_WRITEABLE(srv_conn->event.fde);
-       }
-}
-
-static struct socket_address *dcesrv_sock_get_my_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx)
-{
-       struct stream_connection *srv_conn;
-       srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
-                                  struct stream_connection);
-
-       return socket_get_my_addr(srv_conn->socket, mem_ctx);
-}
-
-static struct socket_address *dcesrv_sock_get_peer_addr(struct dcesrv_connection *dcesrv_conn, TALLOC_CTX *mem_ctx)
-{
-       struct stream_connection *srv_conn;
-       srv_conn = talloc_get_type(dcesrv_conn->transport.private_data,
-                                  struct stream_connection);
-
-       return socket_get_peer_addr(srv_conn->socket, mem_ctx);
-}
-
-static void dcesrv_sock_accept(struct stream_connection *srv_conn)
-{
-       NTSTATUS status;
-       struct dcesrv_socket_context *dcesrv_sock = 
-               talloc_get_type(srv_conn->private, struct dcesrv_socket_context);
-       struct dcesrv_connection *dcesrv_conn = NULL;
-       struct auth_session_info *session_info = NULL;
-
-       status = auth_anonymous_session_info(srv_conn, dcesrv_sock->dcesrv_ctx->lp_ctx, &session_info);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0,("dcesrv_sock_accept: auth_anonymous_session_info failed: %s\n", 
-                       nt_errstr(status)));
-               stream_terminate_connection(srv_conn, nt_errstr(status));
-               return;
-       }
-
-       status = dcesrv_endpoint_connect(dcesrv_sock->dcesrv_ctx,
-                                        srv_conn,
-                                        dcesrv_sock->endpoint,
-                                        session_info,
-                                        srv_conn->event.ctx,
-                                        srv_conn->msg_ctx,
-                                        srv_conn->server_id,
-                                        DCESRV_CALL_STATE_FLAG_MAY_ASYNC,
-                                        &dcesrv_conn);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0,("dcesrv_sock_accept: dcesrv_endpoint_connect failed: %s\n", 
-                       nt_errstr(status)));
-               stream_terminate_connection(srv_conn, nt_errstr(status));
-               return;
-       }
-
-       dcesrv_conn->transport.private_data             = srv_conn;
-       dcesrv_conn->transport.report_output_data       = dcesrv_sock_report_output_data;
-       dcesrv_conn->transport.get_my_addr              = dcesrv_sock_get_my_addr;
-       dcesrv_conn->transport.get_peer_addr            = dcesrv_sock_get_peer_addr;
-
-       srv_conn->private = dcesrv_conn;
-
-       irpc_add_name(srv_conn->msg_ctx, "rpc_server");
-
-       return; 
-}
-
-static void dcesrv_sock_recv(struct stream_connection *conn, uint16_t flags)
-{
-       NTSTATUS status;
-       struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
-       DATA_BLOB tmp_blob;
-       size_t nread;
-
-       if (dce_conn->processing) {
-               EVENT_FD_NOT_READABLE(conn->event.fde);
-               return;
-       }
-
-       tmp_blob = data_blob_talloc(conn->socket, NULL, 0x1000);
-       if (tmp_blob.data == NULL) {
-               dcesrv_terminate_connection(dce_conn, "out of memory");
-               return;
-       }
-
-       status = socket_recv(conn->socket, tmp_blob.data, tmp_blob.length, &nread);
-       if (NT_STATUS_IS_ERR(status)) {
-               dcesrv_terminate_connection(dce_conn, nt_errstr(status));
-               return;
-       }
-       if (nread == 0) {
-               talloc_free(tmp_blob.data);
-               return;
-       }
-
-       tmp_blob.length = nread;
-
-       dce_conn->processing = true;
-       status = dcesrv_input(dce_conn, &tmp_blob);
-       dce_conn->processing = false;
-       talloc_free(tmp_blob.data);
-
-       EVENT_FD_READABLE(conn->event.fde);
-
-       if (!NT_STATUS_IS_OK(status)) {
-               dcesrv_terminate_connection(dce_conn, nt_errstr(status));
-               return;
-       }
-
-       if (dce_conn->call_list && dce_conn->call_list->replies) {
-               EVENT_FD_WRITEABLE(conn->event.fde);
-       }
-}
-
-static void dcesrv_sock_send(struct stream_connection *conn, uint16_t flags)
-{
-       struct dcesrv_connection *dce_conn = talloc_get_type(conn->private, struct dcesrv_connection);
-       NTSTATUS status;
-
-       status = dcesrv_output(dce_conn, conn->socket, dcerpc_write_fn);
-       if (NT_STATUS_IS_ERR(status)) {
-               dcesrv_terminate_connection(dce_conn, "eof on socket");
-               return;
-       }
-
-       if (!dce_conn->call_list || !dce_conn->call_list->replies) {
-               EVENT_FD_NOT_WRITEABLE(conn->event.fde);
-       }
-}
-
-
-static const struct stream_server_ops dcesrv_stream_ops = {
-       .name                   = "rpc",
-       .accept_connection      = dcesrv_sock_accept,
-       .recv_handler           = dcesrv_sock_recv,
-       .send_handler           = dcesrv_sock_send,
-};
-
-
-
-static NTSTATUS dcesrv_add_ep_unix(struct dcesrv_context *dce_ctx, 
-                                  struct loadparm_context *lp_ctx,
-                                  struct dcesrv_endpoint *e,
-                           struct event_context *event_ctx, const struct model_ops *model_ops)
-{
-       struct dcesrv_socket_context *dcesrv_sock;
-       uint16_t port = 1;
-       NTSTATUS status;
+ * Need to run the majority of the RPC endpoints in a single process to allow
+ * for shared handles, and the sharing of ldb contexts.
+ *
+ * However other endpoints are capable of being run in multiple processes
+ * e.g. NETLOGON.
+ *
+ * To support this the process model is manipulated to force those end points
+ * not supporting multiple processes into the single process model. The code
+ * responsible for this is in dcesrv_init_endpoints
+ *
+ */
+NTSTATUS server_service_rpc_init(TALLOC_CTX *);
 
-       dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
-       NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
-
-       /* remember the endpoint of this socket */
-       dcesrv_sock->endpoint           = e;
-       dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
-
-       status = stream_setup_socket(event_ctx, lp_ctx,
-                                    model_ops, &dcesrv_stream_ops, 
-                                    "unix", e->ep_description->endpoint, &port, 
-                                    lp_socket_options(lp_ctx), 
-                                    dcesrv_sock);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
-                        e->ep_description->endpoint, nt_errstr(status)));
-       }
-
-       return status;
-}
-
-static NTSTATUS dcesrv_add_ep_ncalrpc(struct dcesrv_context *dce_ctx, 
-                                     struct loadparm_context *lp_ctx,
-                                     struct dcesrv_endpoint *e,
-                                     struct event_context *event_ctx, const struct model_ops *model_ops)
+/*
+ * Initialise the rpc endpoints.
+ */
+static NTSTATUS dcesrv_init_endpoints(struct task_server *task,
+                                     struct dcesrv_context *dce_ctx,
+                                     bool use_single_process)
 {
-       struct dcesrv_socket_context *dcesrv_sock;
-       uint16_t port = 1;
-       char *full_path;
-       NTSTATUS status;
 
-       if (!e->ep_description->endpoint) {
-               /* No identifier specified: use DEFAULT. 
-                * DO NOT hardcode this value anywhere else. Rather, specify 
-                * no endpoint and let the epmapper worry about it. */
-               e->ep_description->endpoint = talloc_strdup(dce_ctx, "DEFAULT");
+       struct dcesrv_endpoint *e;
+       const struct model_ops *model_ops = NULL;
+
+       /*
+        * For those RPC services that run with shared context we need to
+        * ensure that they don't fork a new process on accept (standard_model).
+        * And as there is only one process handling these requests we need
+        * to handle accept errors in a similar manner to the single process
+        * model.
+        *
+        * To do this we override the process model operations with the single
+        * process operations. This is not the most elegant solution, but it is
+        * the least ugly, and is confined to the next block of code.
+        */
+       if (use_single_process == true) {
+               model_ops = process_model_startup("single");
+               if (model_ops == NULL) {
+                       DBG_ERR("Unable to load single process model");
+                       return NT_STATUS_INTERNAL_ERROR;
+               }
+       } else {
+               model_ops = task->model_ops;
        }
 
-       full_path = talloc_asprintf(dce_ctx, "%s/%s", lp_ncalrpc_dir(lp_ctx), 
-                                   e->ep_description->endpoint);
-
-       dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
-       NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
+       for (e = dce_ctx->endpoint_list; e; e = e->next) {
 
-       /* remember the endpoint of this socket */
-       dcesrv_sock->endpoint           = e;
-       dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
+               enum dcerpc_transport_t transport =
+                   dcerpc_binding_get_transport(e->ep_description);
 
-       status = stream_setup_socket(event_ctx, lp_ctx,
-                                    model_ops, &dcesrv_stream_ops, 
-                                    "unix", full_path, &port, 
-                                    lp_socket_options(lp_ctx), 
-                                    dcesrv_sock);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0,("service_setup_stream_socket(identifier=%s,path=%s) failed - %s\n",
-                        e->ep_description->endpoint, full_path, nt_errstr(status)));
+               if (transport == NCACN_HTTP) {
+                       /*
+                        * We don't support ncacn_http yet
+                        */
+                       continue;
+               }
+               if (e->use_single_process == use_single_process) {
+                       NTSTATUS status;
+                       status = dcesrv_add_ep(dce_ctx,
+                                              task->lp_ctx,
+                                              e,
+                                              task->event_ctx,
+                                              model_ops,
+                                              task->process_context);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               return status;
+                       }
+               }
        }
-       return status;
+       return NT_STATUS_OK;
 }
-
-
 /*
-  add a socket address to the list of events, one event per dcerpc endpoint
+ * Initialise the RPC service.
+ * And those end points that can be serviced by multiple processes.
+ * The endpoints that need to be run in a single process are setup in the
+ * post_fork hook.
 */
-static NTSTATUS add_socket_rpc_pipe_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
-                                        struct event_context *event_ctx, const struct model_ops *model_ops)
+static NTSTATUS dcesrv_task_init(struct task_server *task)
 {
-       struct dcesrv_socket_context *dcesrv_sock;
-       NTSTATUS status;
-                       
-       if (e->ep_description->endpoint == NULL) {
-               DEBUG(0, ("Endpoint mandatory for named pipes\n"));
-               return NT_STATUS_INVALID_PARAMETER;
-       }
-
-       dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
-       NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+       struct dcesrv_context *dce_ctx;
 
-       /* remember the endpoint of this socket */
-       dcesrv_sock->endpoint           = e;
-       dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
+       dcerpc_server_init(task->lp_ctx);
 
-       status = NT_STATUS_OK;
-#if 0
+       task_server_set_title(task, "task[dcesrv]");
 
-       status = stream_setup_smb_pipe(event_ctx, model_ops, &dcesrv_stream_ops, 
-                                    e->ep_description->endpoint, dcesrv_sock);
+       status = dcesrv_init_context(task->event_ctx,
+                                    task->lp_ctx,
+                                    lpcfg_dcerpc_endpoint_servers(task->lp_ctx),
+                                    &dce_ctx);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n", 
-                        e->ep_description->endpoint, nt_errstr(status)));
+               return status;
        }
-#endif
-       return status;
-}
-
-static NTSTATUS dcesrv_add_ep_np(struct dcesrv_context *dce_ctx, 
-                                struct loadparm_context *lp_ctx,
-                                struct dcesrv_endpoint *e,
-                                struct event_context *event_ctx, const struct model_ops *model_ops)
-{
-       NTSTATUS status;
 
-       status = add_socket_rpc_pipe_iface(dce_ctx, e, event_ctx, model_ops);
-       NT_STATUS_NOT_OK_RETURN(status);
-
-       return status;
-}
-
-/*
-  add a socket address to the list of events, one event per dcerpc endpoint
-*/
-static NTSTATUS add_socket_rpc_tcp_iface(struct dcesrv_context *dce_ctx, struct dcesrv_endpoint *e,
-                                        struct event_context *event_ctx, const struct model_ops *model_ops,
-                                        const char *address)
-{
-       struct dcesrv_socket_context *dcesrv_sock;
-       uint16_t port = 0;
-       NTSTATUS status;
-                       
-       if (e->ep_description->endpoint) {
-               port = atoi(e->ep_description->endpoint);
+       /* Make sure the directory for NCALRPC exists */
+       if (!directory_exist(lpcfg_ncalrpc_dir(task->lp_ctx))) {
+               mkdir(lpcfg_ncalrpc_dir(task->lp_ctx), 0755);
        }
-
-       dcesrv_sock = talloc(event_ctx, struct dcesrv_socket_context);
-       NT_STATUS_HAVE_NO_MEMORY(dcesrv_sock);
-
-       /* remember the endpoint of this socket */
-       dcesrv_sock->endpoint           = e;
-       dcesrv_sock->dcesrv_ctx         = talloc_reference(dcesrv_sock, dce_ctx);
-
-       status = stream_setup_socket(event_ctx, dce_ctx->lp_ctx,
-                                    model_ops, &dcesrv_stream_ops, 
-                                    "ipv4", address, &port, 
-                                    lp_socket_options(dce_ctx->lp_ctx), 
-                                    dcesrv_sock);
+       status = dcesrv_init_endpoints(task, dce_ctx, false);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n", 
-                        address, port, nt_errstr(status)));
-       }
-
-       if (e->ep_description->endpoint == NULL) {
-               e->ep_description->endpoint = talloc_asprintf(dce_ctx, "%d", port);
-       }
-
-       return status;
-}
-
-static NTSTATUS dcesrv_add_ep_tcp(struct dcesrv_context *dce_ctx, 
-                                 struct loadparm_context *lp_ctx,
-                                 struct dcesrv_endpoint *e,
-                                 struct event_context *event_ctx, const struct model_ops *model_ops)
-{
-       NTSTATUS status;
-
-       /* Add TCP/IP sockets */
-       if (lp_interfaces(lp_ctx) && lp_bind_interfaces_only(lp_ctx)) {
-               int num_interfaces;
-               int i;
-               struct interface *ifaces;
-
-               load_interfaces(dce_ctx, lp_interfaces(lp_ctx), &ifaces);
-
-               num_interfaces = iface_count(ifaces);
-               for(i = 0; i < num_interfaces; i++) {
-                       const char *address = iface_n_ip(ifaces, i);
-                       status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, address);
-                       NT_STATUS_NOT_OK_RETURN(status);
-               }
-       } else {
-               status = add_socket_rpc_tcp_iface(dce_ctx, e, event_ctx, model_ops, 
-                                                 lp_socket_address(lp_ctx));
-               NT_STATUS_NOT_OK_RETURN(status);
+               return status;
        }
 
+       task->private_data = dce_ctx;
        return NT_STATUS_OK;
 }
 
-
-static NTSTATUS dcesrv_add_ep(struct dcesrv_context *dce_ctx, 
-                             struct loadparm_context *lp_ctx,
-                             struct dcesrv_endpoint *e,
-                         struct event_context *event_ctx, const struct model_ops *model_ops)
-{
-       switch (e->ep_description->transport) {
-       case NCACN_UNIX_STREAM:
-               return dcesrv_add_ep_unix(dce_ctx, lp_ctx, e, event_ctx, model_ops);
-
-       case NCALRPC:
-               return dcesrv_add_ep_ncalrpc(dce_ctx, lp_ctx, e, event_ctx, model_ops);
-
-       case NCACN_IP_TCP:
-               return dcesrv_add_ep_tcp(dce_ctx, lp_ctx, e, event_ctx, model_ops);
-
-       case NCACN_NP:
-               return dcesrv_add_ep_np(dce_ctx, lp_ctx, e, event_ctx, model_ops);
-
-       default:
-               return NT_STATUS_NOT_SUPPORTED;
-       }
-}
-
 /*
-  open the dcerpc server sockets
-*/
-static void dcesrv_task_init(struct task_server *task)
+ * Initialise the endpoints that need to run in a single process fork.
+ * The endpoint registration is only done for the first process instance.
+ *
+ */
+static void dcesrv_post_fork(struct task_server *task,
+                            struct process_details *pd)
 {
-       NTSTATUS status;
-       struct dcesrv_context *dce_ctx;
-       struct dcesrv_endpoint *e;
-
-       task_server_set_title(task, "task[dcesrv]");
 
-       status = dcesrv_init_context(task->event_ctx,
-                                    task->lp_ctx,
-                                    lp_dcerpc_endpoint_servers(task->lp_ctx),
-                                    &dce_ctx);
-       if (!NT_STATUS_IS_OK(status)) goto failed;
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+       struct dcesrv_context *dce_ctx;
 
-       /* Make sure the directory for NCALRPC exists */
-       if (!directory_exist(lp_ncalrpc_dir(task->lp_ctx))) {
-               mkdir(lp_ncalrpc_dir(task->lp_ctx), 0755);
+       if (task->private_data == NULL) {
+               task_server_terminate(task, "dcerpc: No dcesrv_context", true);
+               return;
        }
-
-       for (e=dce_ctx->endpoint_list;e;e=e->next) {
-               status = dcesrv_add_ep(dce_ctx, task->lp_ctx, e, task->event_ctx, task->model_ops);
-               if (!NT_STATUS_IS_OK(status)) goto failed;
+       dce_ctx =
+           talloc_get_type_abort(task->private_data, struct dcesrv_context);
+
+       /*
+        * Ensure the single process endpoints are only available to the
+        * first instance.
+        */
+       if (pd->instances == 0) {
+               status = dcesrv_init_endpoints(task, dce_ctx, true);
+               if (!NT_STATUS_IS_OK(status)) {
+                       task_server_terminate(
+                           task,
+                           "dcerpc: Failed to initialise end points",
+                           true);
+                       return;
+               }
        }
 
-       return;
-failed:
-       task_server_terminate(task, "Failed to startup dcerpc server task");    
+       irpc_add_name(task->msg_ctx, "rpc_server");
 }
 
-NTSTATUS server_service_rpc_init(void)
+NTSTATUS server_service_rpc_init(TALLOC_CTX *ctx)
 {
-       init_module_fn static_init[] = { STATIC_dcerpc_server_MODULES };
-       init_module_fn *shared_init = load_samba_modules(NULL, global_loadparm, "dcerpc_server");
-
-       run_init_functions(static_init);
-       run_init_functions(shared_init);
-
-       talloc_free(shared_init);
-       
-       return register_server_service("rpc", dcesrv_task_init);
+       static const struct service_details details = {
+           .inhibit_fork_on_accept = false,
+           .inhibit_pre_fork = false,
+           .task_init = dcesrv_task_init,
+           .post_fork = dcesrv_post_fork};
+       return register_server_service(ctx, "rpc", &details);
 }