s4:rpc_server/remote: add dcerpc_remote:allow_anonymous_fallback option
[samba.git] / source4 / rpc_server / remote / dcesrv_remote.c
index f01026b25fc5a1135851562075e6bfb1d22f17c6..4a441f34b232e2bc294292b5e7db1278097fbaae 100644 (file)
 */
 
 #include "includes.h"
+#include <tevent.h>
 #include "rpc_server/dcerpc_server.h"
 #include "auth/auth.h"
 #include "auth/credentials/credentials.h"
 #include "librpc/ndr/ndr_table.h"
 #include "param/param.h"
 
+NTSTATUS dcerpc_server_remote_init(TALLOC_CTX *ctx);
 
 struct dcesrv_remote_private {
        struct dcerpc_pipe *c_pipe;
@@ -40,17 +42,18 @@ static NTSTATUS remote_op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *
 static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface, uint32_t if_version)
 {
         NTSTATUS status;
-       const struct ndr_interface_table *table;
+       const struct ndr_interface_table *table =
+               (const struct ndr_interface_table *)dce_call->context->iface->private_data;
        struct dcesrv_remote_private *priv;
-       const char *binding = lp_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "binding");
+       const char *binding = NULL;
        const char *user, *pass, *domain;
        struct cli_credentials *credentials;
-       bool must_free_credentials = true;
+       bool must_free_credentials = false;
        bool machine_account;
+       bool allow_anonymous;
        struct dcerpc_binding           *b;
        struct composite_context        *pipe_conn_req;
-
-       machine_account = lp_parm_bool(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "use_machine_account", false);
+       uint32_t flags = 0;
 
        priv = talloc(dce_call->conn, struct dcesrv_remote_private);
        if (!priv) {
@@ -60,20 +63,31 @@ static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct
        priv->c_pipe = NULL;
        dce_call->context->private_data = priv;
 
-       if (!binding) {
+       binding = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx,
+                                   NULL,
+                                   "dcerpc_remote",
+                                   "binding");
+       if (binding == NULL) {
                DEBUG(0,("You must specify a DCE/RPC binding string\n"));
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       user = lp_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "user");
-       pass = lp_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "password");
-       domain = lp_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dceprc_remote", "domain");
+       user = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "user");
+       pass = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "password");
+       domain = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dceprc_remote", "domain");
 
-       table = ndr_table_by_uuid(&iface->syntax_id.uuid); /* FIXME: What about if_version ? */
-       if (!table) {
-               dce_call->fault_code = DCERPC_FAULT_UNK_IF;
-               return NT_STATUS_NET_WRITE_FAULT;
-       }
+       machine_account = lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx,
+                                         NULL,
+                                         "dcerpc_remote",
+                                         "use_machine_account",
+                                         false);
+       allow_anonymous = lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx,
+                                         NULL,
+                                         "dcerpc_remote",
+                                         "allow_anonymous_fallback",
+                                         false);
+
+       credentials = dcesrv_call_credentials(dce_call);
 
        if (user && pass) {
                DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
@@ -81,6 +95,7 @@ static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct
                if (!credentials) {
                        return NT_STATUS_NO_MEMORY;
                }
+               must_free_credentials = true;
                cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
                cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
                if (domain) {
@@ -90,6 +105,10 @@ static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct
        } else if (machine_account) {
                DEBUG(5, ("dcerpc_remote: RPC Proxy: Using machine account\n"));
                credentials = cli_credentials_init(priv);
+               if (!credentials) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               must_free_credentials = true;
                cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
                if (domain) {
                        cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
@@ -98,10 +117,15 @@ static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct
                if (!NT_STATUS_IS_OK(status)) {
                        return status;
                }
-       } else if (dce_call->conn->auth_state.session_info->credentials) {
+       } else if (credentials != NULL) {
                DEBUG(5, ("dcerpc_remote: RPC Proxy: Using delegated credentials\n"));
-               credentials = dce_call->conn->auth_state.session_info->credentials;
-               must_free_credentials = false;
+       } else if (allow_anonymous) {
+               DEBUG(5, ("dcerpc_remote: RPC Proxy: Using anonymous\n"));
+               credentials = cli_credentials_init_anon(priv);
+               if (!credentials) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               must_free_credentials = true;
        } else {
                DEBUG(1,("dcerpc_remote: RPC Proxy: You must supply binding, user and password or have delegated credentials\n"));
                return NT_STATUS_INVALID_PARAMETER;
@@ -113,15 +137,35 @@ static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct
                DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
                return status;
        }
-       
-       DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(dce_call->context, b)));
-       
+
        /* If we already have a remote association group ID, then use that */
-       if (dce_call->context->assoc_group->proxied_id != 0) {
-               b->assoc_group_id = dce_call->context->assoc_group->proxied_id;
+       if (dce_call->conn->assoc_group->proxied_id != 0) {
+               status = dcerpc_binding_set_assoc_group_id(b,
+                       dce_call->conn->assoc_group->proxied_id);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0, ("dcerpc_binding_set_assoc_group_id() - %s'\n",
+                                 nt_errstr(status)));
+                       return status;
+               }
+       }
+
+       status = dcerpc_binding_set_abstract_syntax(b, &table->syntax_id);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0, ("dcerpc_binding_set_abstract_syntax() - %s'\n",
+                         nt_errstr(status)));
+               return status;
+       }
+
+       if (dce_call->pkt.pfc_flags & DCERPC_PFC_FLAG_CONC_MPX) {
+               status = dcerpc_binding_set_flags(b, DCERPC_CONCURRENT_MULTIPLEX, 0);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0, ("dcerpc_binding_set_flags(CONC_MPX) - %s'\n",
+                                 nt_errstr(status)));
+                       return status;
+               }
        }
 
-       b->object.if_version = if_version;
+       DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(dce_call->context, b)));
 
        pipe_conn_req = dcerpc_pipe_connect_b_send(dce_call->context, b, table,
                                                   credentials, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx);
@@ -135,8 +179,14 @@ static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct
                return status;
        }
 
-       if (dce_call->context->assoc_group->proxied_id == 0) {
-               dce_call->context->assoc_group->proxied_id = priv->c_pipe->assoc_group_id;
+       flags = dcerpc_binding_get_flags(priv->c_pipe->binding);
+       if (!(flags & DCERPC_CONCURRENT_MULTIPLEX)) {
+               dce_call->state_flags &= ~DCESRV_CALL_STATE_FLAG_MULTIPLEXED;
+       }
+
+       if (dce_call->conn->assoc_group->proxied_id == 0) {
+               dce_call->conn->assoc_group->proxied_id =
+                       dcerpc_binding_get_assoc_group_id(priv->c_pipe->binding);
        }
 
        if (!NT_STATUS_IS_OK(status)) {
@@ -146,13 +196,17 @@ static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct
        return NT_STATUS_OK;    
 }
 
-static void remote_op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
+static NTSTATUS remote_get_private(struct dcesrv_call_state *dce_call,
+                                  struct dcesrv_remote_private **_priv)
 {
-       struct dcesrv_remote_private *priv = (struct dcesrv_remote_private *)context->private_data;
+       void *ptr = NULL;
+       struct dcesrv_remote_private *priv = NULL;
 
-       talloc_free(priv->c_pipe);
+       ptr = dce_call->context->private_data;
+       priv = talloc_get_type_abort(ptr, struct dcesrv_remote_private);
 
-       return; 
+       *_priv = priv;
+       return NT_STATUS_OK;
 }
 
 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
@@ -168,6 +222,18 @@ static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CT
                return NT_STATUS_NET_WRITE_FAULT;
        }
 
+       /*
+        * We don't have support for calls with pipes.
+        */
+       if (table->calls[opnum].in_pipes.num_pipes != 0) {
+               dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
+               return NT_STATUS_NET_WRITE_FAULT;
+       }
+       if (table->calls[opnum].out_pipes.num_pipes != 0) {
+               dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
+               return NT_STATUS_NET_WRITE_FAULT;
+       }
+
        *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
        if (!*r) {
                return NT_STATUS_NO_MEMORY;
@@ -186,13 +252,22 @@ static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CT
        return NT_STATUS_OK;
 }
 
+static void remote_op_dispatch_done(struct tevent_req *subreq);
+
 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
 {
-       struct dcesrv_remote_private *priv = dce_call->context->private_data;
+       struct dcesrv_remote_private *priv = NULL;
        uint16_t opnum = dce_call->pkt.u.request.opnum;
        const struct ndr_interface_table *table = dce_call->context->iface->private_data;
        const struct ndr_interface_call *call;
        const char *name;
+       struct tevent_req *subreq;
+       NTSTATUS status;
+
+       status = remote_get_private(dce_call, &priv);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
        name = table->calls[opnum].name;
        call = &table->calls[opnum];
@@ -204,20 +279,57 @@ static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CT
        priv->c_pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
 
        /* we didn't use the return code of this function as we only check the last_fault_code */
-       dcerpc_ndr_request(priv->c_pipe, NULL, table, opnum, mem_ctx,r);
+       subreq = dcerpc_binding_handle_call_send(dce_call, dce_call->event_ctx,
+                                                priv->c_pipe->binding_handle,
+                                                NULL, table,
+                                                opnum, mem_ctx, r);
+       if (subreq == NULL) {
+               DEBUG(0,("dcesrv_remote: call[%s] dcerpc_binding_handle_call_send() failed!\n", name));
+               return NT_STATUS_NO_MEMORY;
+       }
+       tevent_req_set_callback(subreq, remote_op_dispatch_done, dce_call);
+
+       dce_call->state_flags |= DCESRV_CALL_STATE_FLAG_ASYNC;
+       return NT_STATUS_OK;
+}
+
+static void remote_op_dispatch_done(struct tevent_req *subreq)
+{
+       struct dcesrv_call_state *dce_call = tevent_req_callback_data(subreq,
+                                            struct dcesrv_call_state);
+       struct dcesrv_remote_private *priv = talloc_get_type_abort(dce_call->context->private_data,
+                                                                  struct dcesrv_remote_private);
+       uint16_t opnum = dce_call->pkt.u.request.opnum;
+       const struct ndr_interface_table *table = dce_call->context->iface->private_data;
+       const struct ndr_interface_call *call;
+       const char *name;
+       NTSTATUS status;
+
+       name = table->calls[opnum].name;
+       call = &table->calls[opnum];
+
+       /* we didn't use the return code of this function as we only check the last_fault_code */
+       status = dcerpc_binding_handle_call_recv(subreq);
+       TALLOC_FREE(subreq);
 
        dce_call->fault_code = priv->c_pipe->last_fault_code;
        if (dce_call->fault_code != 0) {
-               DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",name, dcerpc_errstr(mem_ctx, dce_call->fault_code)));
-               return NT_STATUS_NET_WRITE_FAULT;
+               DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",
+                       name, dcerpc_errstr(dce_call, dce_call->fault_code)));
+               goto reply;
        }
 
-       if ((dce_call->fault_code == 0) && 
+       if (NT_STATUS_IS_OK(status) &&
            (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
-               ndr_print_function_debug(call->ndr_print, name, NDR_OUT, r);            
+               ndr_print_function_debug(call->ndr_print, name, NDR_OUT, dce_call->r);
        }
 
-       return NT_STATUS_OK;
+reply:
+       status = dcesrv_reply(dce_call);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("dcesrv_remote: call[%s]: dcesrv_reply() failed - %s\n",
+                       name, nt_errstr(status)));
+       }
 }
 
 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
@@ -258,7 +370,7 @@ static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const
 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
 {
        unsigned int i;
-       const char **ifaces = (const char **)str_list_make(dce_ctx, lp_parm_string(dce_ctx->lp_ctx, NULL, "dcerpc_remote", "interfaces"),NULL);
+       char **ifaces = str_list_make(dce_ctx, lpcfg_parm_string(dce_ctx->lp_ctx, NULL, "dcerpc_remote", "interfaces"),NULL);
 
        if (!ifaces) {
                DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
@@ -293,7 +405,7 @@ static bool remote_fill_interface(struct dcesrv_interface *iface, const struct n
        iface->syntax_id = if_tabl->syntax_id;
        
        iface->bind = remote_op_bind;
-       iface->unbind = remote_op_unbind;
+       iface->unbind = NULL;
 
        iface->ndr_pull = remote_op_ndr_pull;
        iface->dispatch = remote_op_dispatch;
@@ -301,6 +413,7 @@ static bool remote_fill_interface(struct dcesrv_interface *iface, const struct n
        iface->ndr_push = remote_op_ndr_push;
 
        iface->private_data = if_tabl;
+       iface->flags = 0;
 
        return true;
 }
@@ -329,21 +442,19 @@ static bool remote_op_interface_by_name(struct dcesrv_interface *iface, const ch
        return false;   
 }
 
-NTSTATUS dcerpc_server_remote_init(void)
+NTSTATUS dcerpc_server_remote_init(TALLOC_CTX *ctx)
 {
        NTSTATUS ret;
-       struct dcesrv_endpoint_server ep_server;
-
-       ZERO_STRUCT(ep_server);
-
-       /* fill in our name */
-       ep_server.name = "remote";
+       static const struct dcesrv_endpoint_server ep_server = {
+               /* fill in our name */
+               .name = "remote",
 
-       /* fill in all the operations */
-       ep_server.init_server = remote_op_init_server;
+               /* fill in all the operations */
+               .init_server = remote_op_init_server,
 
-       ep_server.interface_by_uuid = remote_op_interface_by_uuid;
-       ep_server.interface_by_name = remote_op_interface_by_name;
+               .interface_by_uuid = remote_op_interface_by_uuid,
+               .interface_by_name = remote_op_interface_by_name
+       };
 
        /* register ourselves with the DCERPC subsystem. */
        ret = dcerpc_register_ep_server(&ep_server);