s3:lib/events: add missing TEVENT_TRACE_BEFORE/AFTER_WAIT handling
[Samba/gebeck_regimport.git] / source4 / rpc_server / remote / dcesrv_remote.c
blob2d6be6adeeb13cc8b4a709d0c2d67bf663681e31
1 /*
2 Unix SMB/CIFS implementation.
3 remote dcerpc operations
5 Copyright (C) Stefan (metze) Metzmacher 2004
6 Copyright (C) Julien Kerihuel 2008-2009
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include <tevent.h>
25 #include "rpc_server/dcerpc_server.h"
26 #include "auth/auth.h"
27 #include "auth/credentials/credentials.h"
28 #include "librpc/ndr/ndr_table.h"
29 #include "param/param.h"
31 NTSTATUS dcerpc_server_remote_init(void);
33 struct dcesrv_remote_private {
34 struct dcerpc_pipe *c_pipe;
37 static NTSTATUS remote_op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
39 return NT_STATUS_OK;
42 static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface, uint32_t if_version)
44 NTSTATUS status;
45 const struct ndr_interface_table *table;
46 struct dcesrv_remote_private *priv;
47 const char *binding = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "binding");
48 const char *user, *pass, *domain;
49 struct cli_credentials *credentials;
50 bool must_free_credentials = true;
51 bool machine_account;
52 struct dcerpc_binding *b;
53 struct composite_context *pipe_conn_req;
55 machine_account = lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "use_machine_account", false);
57 priv = talloc(dce_call->conn, struct dcesrv_remote_private);
58 if (!priv) {
59 return NT_STATUS_NO_MEMORY;
62 priv->c_pipe = NULL;
63 dce_call->context->private_data = priv;
65 if (!binding) {
66 DEBUG(0,("You must specify a DCE/RPC binding string\n"));
67 return NT_STATUS_INVALID_PARAMETER;
70 user = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "user");
71 pass = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "password");
72 domain = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dceprc_remote", "domain");
74 table = ndr_table_by_uuid(&iface->syntax_id.uuid); /* FIXME: What about if_version ? */
75 if (!table) {
76 dce_call->fault_code = DCERPC_FAULT_UNK_IF;
77 return NT_STATUS_NET_WRITE_FAULT;
80 if (user && pass) {
81 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
82 credentials = cli_credentials_init(priv);
83 if (!credentials) {
84 return NT_STATUS_NO_MEMORY;
86 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
87 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
88 if (domain) {
89 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
91 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
92 } else if (machine_account) {
93 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using machine account\n"));
94 credentials = cli_credentials_init(priv);
95 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
96 if (domain) {
97 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
99 status = cli_credentials_set_machine_account(credentials, dce_call->conn->dce_ctx->lp_ctx);
100 if (!NT_STATUS_IS_OK(status)) {
101 return status;
103 } else if (dce_call->conn->auth_state.session_info->credentials) {
104 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using delegated credentials\n"));
105 credentials = dce_call->conn->auth_state.session_info->credentials;
106 must_free_credentials = false;
107 } else {
108 DEBUG(1,("dcerpc_remote: RPC Proxy: You must supply binding, user and password or have delegated credentials\n"));
109 return NT_STATUS_INVALID_PARAMETER;
112 /* parse binding string to the structure */
113 status = dcerpc_parse_binding(dce_call->context, binding, &b);
114 if (!NT_STATUS_IS_OK(status)) {
115 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
116 return status;
119 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(dce_call->context, b)));
121 /* If we already have a remote association group ID, then use that */
122 if (dce_call->context->assoc_group->proxied_id != 0) {
123 b->assoc_group_id = dce_call->context->assoc_group->proxied_id;
126 b->object.if_version = if_version;
128 pipe_conn_req = dcerpc_pipe_connect_b_send(dce_call->context, b, table,
129 credentials, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx);
130 status = dcerpc_pipe_connect_b_recv(pipe_conn_req, dce_call->context, &(priv->c_pipe));
132 if (must_free_credentials) {
133 talloc_free(credentials);
136 if (!NT_STATUS_IS_OK(status)) {
137 return status;
140 if (dce_call->context->assoc_group->proxied_id == 0) {
141 dce_call->context->assoc_group->proxied_id = priv->c_pipe->assoc_group_id;
144 if (!NT_STATUS_IS_OK(status)) {
145 return status;
148 return NT_STATUS_OK;
151 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
153 enum ndr_err_code ndr_err;
154 const struct ndr_interface_table *table = (const struct ndr_interface_table *)dce_call->context->iface->private_data;
155 uint16_t opnum = dce_call->pkt.u.request.opnum;
157 dce_call->fault_code = 0;
159 if (opnum >= table->num_calls) {
160 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
161 return NT_STATUS_NET_WRITE_FAULT;
164 *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
165 if (!*r) {
166 return NT_STATUS_NO_MEMORY;
169 /* unravel the NDR for the packet */
170 ndr_err = table->calls[opnum].ndr_pull(pull, NDR_IN, *r);
171 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
172 dcerpc_log_packet(dce_call->conn->packet_log_dir,
173 table, opnum, NDR_IN,
174 &dce_call->pkt.u.request.stub_and_verifier);
175 dce_call->fault_code = DCERPC_FAULT_NDR;
176 return NT_STATUS_NET_WRITE_FAULT;
179 return NT_STATUS_OK;
182 static void remote_op_dispatch_done(struct tevent_req *subreq);
184 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
186 struct dcesrv_remote_private *priv = talloc_get_type_abort(dce_call->context->private_data,
187 struct dcesrv_remote_private);
188 uint16_t opnum = dce_call->pkt.u.request.opnum;
189 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
190 const struct ndr_interface_call *call;
191 const char *name;
192 struct tevent_req *subreq;
194 name = table->calls[opnum].name;
195 call = &table->calls[opnum];
197 if (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
198 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);
201 priv->c_pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
203 /* we didn't use the return code of this function as we only check the last_fault_code */
204 subreq = dcerpc_binding_handle_call_send(dce_call, dce_call->event_ctx,
205 priv->c_pipe->binding_handle,
206 NULL, table,
207 opnum, mem_ctx, r);
208 if (subreq == NULL) {
209 DEBUG(0,("dcesrv_remote: call[%s] dcerpc_binding_handle_call_send() failed!\n", name));
210 return NT_STATUS_NO_MEMORY;
212 tevent_req_set_callback(subreq, remote_op_dispatch_done, dce_call);
214 dce_call->state_flags |= DCESRV_CALL_STATE_FLAG_ASYNC;
215 return NT_STATUS_OK;
218 static void remote_op_dispatch_done(struct tevent_req *subreq)
220 struct dcesrv_call_state *dce_call = tevent_req_callback_data(subreq,
221 struct dcesrv_call_state);
222 struct dcesrv_remote_private *priv = talloc_get_type_abort(dce_call->context->private_data,
223 struct dcesrv_remote_private);
224 uint16_t opnum = dce_call->pkt.u.request.opnum;
225 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
226 const struct ndr_interface_call *call;
227 const char *name;
228 NTSTATUS status;
230 name = table->calls[opnum].name;
231 call = &table->calls[opnum];
233 /* we didn't use the return code of this function as we only check the last_fault_code */
234 status = dcerpc_binding_handle_call_recv(subreq);
235 TALLOC_FREE(subreq);
237 dce_call->fault_code = priv->c_pipe->last_fault_code;
238 if (dce_call->fault_code != 0) {
239 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",
240 name, dcerpc_errstr(dce_call, dce_call->fault_code)));
241 goto reply;
244 if (NT_STATUS_IS_OK(status) &&
245 (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
246 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, dce_call->r);
249 reply:
250 status = dcesrv_reply(dce_call);
251 if (!NT_STATUS_IS_OK(status)) {
252 DEBUG(0,("dcesrv_remote: call[%s]: dcesrv_reply() failed - %s\n",
253 name, nt_errstr(status)));
257 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
259 enum ndr_err_code ndr_err;
260 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
261 uint16_t opnum = dce_call->pkt.u.request.opnum;
263 /* unravel the NDR for the packet */
264 ndr_err = table->calls[opnum].ndr_push(push, NDR_OUT, r);
265 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
266 dce_call->fault_code = DCERPC_FAULT_NDR;
267 return NT_STATUS_NET_WRITE_FAULT;
270 return NT_STATUS_OK;
273 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
275 unsigned int i;
276 const struct ndr_interface_table *table = iface->private_data;
278 for (i=0;i<table->endpoints->count;i++) {
279 NTSTATUS ret;
280 const char *name = table->endpoints->names[i];
282 ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
283 if (!NT_STATUS_IS_OK(ret)) {
284 DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
285 return ret;
289 return NT_STATUS_OK;
292 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
294 unsigned int i;
295 const char **ifaces = (const char **)str_list_make(dce_ctx, lpcfg_parm_string(dce_ctx->lp_ctx, NULL, "dcerpc_remote", "interfaces"),NULL);
297 if (!ifaces) {
298 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
299 return NT_STATUS_OK;
302 for (i=0;ifaces[i];i++) {
303 NTSTATUS ret;
304 struct dcesrv_interface iface;
306 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
307 DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
308 talloc_free(ifaces);
309 return NT_STATUS_UNSUCCESSFUL;
312 ret = remote_register_one_iface(dce_ctx, &iface);
313 if (!NT_STATUS_IS_OK(ret)) {
314 DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
315 talloc_free(ifaces);
316 return ret;
320 talloc_free(ifaces);
321 return NT_STATUS_OK;
324 static bool remote_fill_interface(struct dcesrv_interface *iface, const struct ndr_interface_table *if_tabl)
326 iface->name = if_tabl->name;
327 iface->syntax_id = if_tabl->syntax_id;
329 iface->bind = remote_op_bind;
330 iface->unbind = NULL;
332 iface->ndr_pull = remote_op_ndr_pull;
333 iface->dispatch = remote_op_dispatch;
334 iface->reply = remote_op_reply;
335 iface->ndr_push = remote_op_ndr_push;
337 iface->private_data = if_tabl;
339 return true;
342 static bool remote_op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
344 const struct ndr_interface_list *l;
346 for (l=ndr_table_list();l;l=l->next) {
347 if (l->table->syntax_id.if_version == if_version &&
348 GUID_equal(&l->table->syntax_id.uuid, uuid)==0) {
349 return remote_fill_interface(iface, l->table);
353 return false;
356 static bool remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
358 const struct ndr_interface_table *tbl = ndr_table_by_name(name);
360 if (tbl)
361 return remote_fill_interface(iface, tbl);
363 return false;
366 NTSTATUS dcerpc_server_remote_init(void)
368 NTSTATUS ret;
369 struct dcesrv_endpoint_server ep_server;
371 ZERO_STRUCT(ep_server);
373 /* fill in our name */
374 ep_server.name = "remote";
376 /* fill in all the operations */
377 ep_server.init_server = remote_op_init_server;
379 ep_server.interface_by_uuid = remote_op_interface_by_uuid;
380 ep_server.interface_by_name = remote_op_interface_by_name;
382 /* register ourselves with the DCERPC subsystem. */
383 ret = dcerpc_register_ep_server(&ep_server);
384 if (!NT_STATUS_IS_OK(ret)) {
385 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
386 return ret;
389 /* We need the full DCE/RPC interface table */
390 ndr_table_init();
392 return ret;