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/>.
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(TALLOC_CTX *ctx);
33 #define DCESRV_REMOTE_ASSOC_MAGIC 0x782f50c4
34 struct dcesrv_remote_assoc {
35 uint32_t assoc_group_id;
38 #define DCESRV_REMOTE_PRIVATE_MAGIC 0x7eceafa6
39 struct dcesrv_remote_private {
40 struct dcerpc_pipe *c_pipe;
43 static NTSTATUS remote_op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
48 static NTSTATUS remote_op_bind(struct dcesrv_connection_context *context,
49 const struct dcesrv_interface *iface)
54 static NTSTATUS remote_get_private(struct dcesrv_call_state *dce_call,
55 struct dcesrv_remote_private **_priv)
57 const struct ndr_interface_table *table =
58 (const struct ndr_interface_table *)dce_call->context->iface->private_data;
59 struct dcesrv_remote_private *priv = NULL;
60 struct dcesrv_remote_assoc *assoc = NULL;
61 const char *binding = NULL;
62 const char *user, *pass, *domain;
63 struct cli_credentials *credentials;
64 bool must_free_credentials = false;
67 struct dcerpc_binding *b;
68 struct composite_context *pipe_conn_req;
72 priv = dcesrv_iface_state_find_conn(dce_call,
73 DCESRV_REMOTE_PRIVATE_MAGIC,
74 struct dcesrv_remote_private);
80 priv = talloc_zero(dce_call, struct dcesrv_remote_private);
82 return NT_STATUS_NO_MEMORY;
85 assoc = dcesrv_iface_state_find_assoc(dce_call,
86 DCESRV_REMOTE_ASSOC_MAGIC,
87 struct dcesrv_remote_assoc);
89 assoc = talloc_zero(dce_call, struct dcesrv_remote_assoc);
91 return NT_STATUS_NO_MEMORY;
95 binding = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx,
99 if (binding == NULL) {
100 DEBUG(0,("You must specify a DCE/RPC binding string\n"));
101 return NT_STATUS_INVALID_PARAMETER;
104 user = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "user");
105 pass = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "password");
106 domain = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dceprc_remote", "domain");
108 machine_account = lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx,
111 "use_machine_account",
113 allow_anonymous = lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx,
116 "allow_anonymous_fallback",
119 credentials = dcesrv_call_credentials(dce_call);
122 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
123 credentials = cli_credentials_init(priv);
125 return NT_STATUS_NO_MEMORY;
127 must_free_credentials = true;
128 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
129 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
131 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
133 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
134 } else if (machine_account) {
135 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using machine account\n"));
136 credentials = cli_credentials_init(priv);
138 return NT_STATUS_NO_MEMORY;
140 must_free_credentials = true;
141 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
143 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
145 status = cli_credentials_set_machine_account(credentials, dce_call->conn->dce_ctx->lp_ctx);
146 if (!NT_STATUS_IS_OK(status)) {
149 } else if (credentials != NULL) {
150 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using delegated credentials\n"));
151 } else if (allow_anonymous) {
152 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using anonymous\n"));
153 credentials = cli_credentials_init_anon(priv);
155 return NT_STATUS_NO_MEMORY;
157 must_free_credentials = true;
159 DEBUG(1,("dcerpc_remote: RPC Proxy: You must supply binding, user and password or have delegated credentials\n"));
160 return NT_STATUS_INVALID_PARAMETER;
163 /* parse binding string to the structure */
164 status = dcerpc_parse_binding(priv, binding, &b);
165 if (!NT_STATUS_IS_OK(status)) {
166 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
170 /* If we already have a remote association group ID, then use that */
171 if (assoc->assoc_group_id != 0) {
172 status = dcerpc_binding_set_assoc_group_id(b,
173 assoc->assoc_group_id);
174 if (!NT_STATUS_IS_OK(status)) {
175 DEBUG(0, ("dcerpc_binding_set_assoc_group_id() - %s'\n",
181 status = dcerpc_binding_set_abstract_syntax(b, &table->syntax_id);
182 if (!NT_STATUS_IS_OK(status)) {
183 DEBUG(0, ("dcerpc_binding_set_abstract_syntax() - %s'\n",
188 if (dce_call->conn->state_flags & DCESRV_CALL_STATE_FLAG_MULTIPLEXED) {
189 status = dcerpc_binding_set_flags(b, DCERPC_CONCURRENT_MULTIPLEX, 0);
190 if (!NT_STATUS_IS_OK(status)) {
191 DEBUG(0, ("dcerpc_binding_set_flags(CONC_MPX) - %s'\n",
197 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(dce_call->context, b)));
199 pipe_conn_req = dcerpc_pipe_connect_b_send(priv, b, table,
200 credentials, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx);
201 status = dcerpc_pipe_connect_b_recv(pipe_conn_req, priv, &(priv->c_pipe));
203 if (must_free_credentials) {
204 talloc_free(credentials);
207 if (!NT_STATUS_IS_OK(status)) {
211 if (dce_call->conn->state_flags & DCESRV_CALL_STATE_FLAG_MULTIPLEXED) {
212 flags = dcerpc_binding_get_flags(priv->c_pipe->binding);
213 if (!(flags & DCERPC_CONCURRENT_MULTIPLEX)) {
214 DEBUG(1,("dcerpc_remote: RPC Proxy: "
215 "Remote server doesn't support MPX\n"));
216 return NT_STATUS_INVALID_NETWORK_RESPONSE;
220 if (assoc->assoc_group_id == 0) {
221 assoc->assoc_group_id =
222 dcerpc_binding_get_assoc_group_id(priv->c_pipe->binding);
223 if (assoc->assoc_group_id == 0) {
224 return NT_STATUS_INVALID_NETWORK_RESPONSE;
227 status = dcesrv_iface_state_store_assoc(dce_call,
228 DCESRV_REMOTE_ASSOC_MAGIC,
230 if (!NT_STATUS_IS_OK(status)) {
235 status = dcesrv_iface_state_store_conn(dce_call,
236 DCESRV_REMOTE_PRIVATE_MAGIC,
238 if (!NT_STATUS_IS_OK(status)) {
246 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
248 enum ndr_err_code ndr_err;
249 const struct ndr_interface_table *table = (const struct ndr_interface_table *)dce_call->context->iface->private_data;
250 uint16_t opnum = dce_call->pkt.u.request.opnum;
252 dce_call->fault_code = 0;
254 if (opnum >= table->num_calls) {
255 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
256 return NT_STATUS_NET_WRITE_FAULT;
260 * We don't have support for calls with pipes.
262 if (table->calls[opnum].in_pipes.num_pipes != 0) {
263 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
264 return NT_STATUS_NET_WRITE_FAULT;
266 if (table->calls[opnum].out_pipes.num_pipes != 0) {
267 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
268 return NT_STATUS_NET_WRITE_FAULT;
271 *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
273 return NT_STATUS_NO_MEMORY;
276 /* unravel the NDR for the packet */
277 ndr_err = table->calls[opnum].ndr_pull(pull, NDR_IN, *r);
278 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
279 dcerpc_log_packet(dce_call->conn->packet_log_dir,
280 table, opnum, NDR_IN,
281 &dce_call->pkt.u.request.stub_and_verifier);
282 dce_call->fault_code = DCERPC_FAULT_NDR;
283 return NT_STATUS_NET_WRITE_FAULT;
289 static void remote_op_dispatch_done(struct tevent_req *subreq);
291 struct dcesrv_remote_call {
292 struct dcesrv_call_state *dce_call;
293 struct dcesrv_remote_private *priv;
294 struct dcerpc_binding_handle_call_params params;
297 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
299 struct dcesrv_remote_call *rcall = NULL;
300 struct dcesrv_remote_private *priv = NULL;
301 uint16_t opnum = dce_call->pkt.u.request.opnum;
302 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
303 const struct ndr_interface_call *call;
305 struct tevent_req *subreq;
308 name = table->calls[opnum].name;
309 call = &table->calls[opnum];
311 status = remote_get_private(dce_call, &priv);
312 if (!NT_STATUS_IS_OK(status)) {
313 DEBUG(0,("dcesrv_remote: call[%s] %s\n", name, nt_errstr(status)));
317 rcall = talloc_zero(dce_call, struct dcesrv_remote_call);
319 return NT_STATUS_NO_MEMORY;
321 rcall->dce_call = dce_call;
323 rcall->params.r_mem = mem_ctx;
324 rcall->params.r_ptr = r;
325 rcall->params.in.num_pipes = 0;
326 rcall->params.in.pipes = NULL;
327 rcall->params.out.num_pipes = 0;
328 rcall->params.out.pipes = NULL;
330 if (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
331 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);
334 priv->c_pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
336 /* we didn't use the return code of this function as we only check the last_fault_code */
337 subreq = dcerpc_binding_handle_call_send(rcall, dce_call->event_ctx,
338 priv->c_pipe->binding_handle,
340 opnum, &rcall->params);
341 if (subreq == NULL) {
342 DEBUG(0,("dcesrv_remote: call[%s] dcerpc_binding_handle_call_send() failed!\n", name));
343 return NT_STATUS_NO_MEMORY;
345 tevent_req_set_callback(subreq, remote_op_dispatch_done, rcall);
347 dce_call->state_flags |= DCESRV_CALL_STATE_FLAG_ASYNC;
351 static void remote_op_dispatch_done(struct tevent_req *subreq)
353 struct dcesrv_remote_call *rcall =
354 tevent_req_callback_data(subreq,
355 struct dcesrv_remote_call);
356 struct dcesrv_call_state *dce_call = rcall->dce_call;
357 struct dcesrv_remote_private *priv = rcall->priv;
358 uint16_t opnum = dce_call->pkt.u.request.opnum;
359 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
360 const struct ndr_interface_call *call;
364 name = table->calls[opnum].name;
365 call = &table->calls[opnum];
367 /* we didn't use the return code of this function as we only check the last_fault_code */
368 status = dcerpc_binding_handle_call_recv(subreq);
371 dce_call->fault_code = priv->c_pipe->last_fault_code;
372 if (dce_call->fault_code != 0) {
373 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",
374 name, dcerpc_errstr(dce_call, dce_call->fault_code)));
378 if (NT_STATUS_IS_OK(status) &&
379 (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
380 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, dce_call->r);
384 status = dcesrv_reply(dce_call);
385 if (!NT_STATUS_IS_OK(status)) {
386 DEBUG(0,("dcesrv_remote: call[%s]: dcesrv_reply() failed - %s\n",
387 name, nt_errstr(status)));
391 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
393 enum ndr_err_code ndr_err;
394 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
395 uint16_t opnum = dce_call->pkt.u.request.opnum;
397 /* unravel the NDR for the packet */
398 ndr_err = table->calls[opnum].ndr_push(push, NDR_OUT, r);
399 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
400 dce_call->fault_code = DCERPC_FAULT_NDR;
401 return NT_STATUS_NET_WRITE_FAULT;
407 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
410 const struct ndr_interface_table *table = iface->private_data;
412 for (i=0;i<table->endpoints->count;i++) {
414 const char *name = table->endpoints->names[i];
416 ret = dcesrv_interface_register(dce_ctx, name, NULL, iface, NULL);
417 if (!NT_STATUS_IS_OK(ret)) {
418 DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
426 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
429 char **ifaces = str_list_make(dce_ctx, lpcfg_parm_string(dce_ctx->lp_ctx, NULL, "dcerpc_remote", "interfaces"),NULL);
432 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
436 for (i=0;ifaces[i];i++) {
438 struct dcesrv_interface iface;
440 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
441 DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
443 return NT_STATUS_UNSUCCESSFUL;
446 ret = remote_register_one_iface(dce_ctx, &iface);
447 if (!NT_STATUS_IS_OK(ret)) {
448 DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
458 static bool remote_fill_interface(struct dcesrv_interface *iface, const struct ndr_interface_table *if_tabl)
460 iface->name = if_tabl->name;
461 iface->syntax_id = if_tabl->syntax_id;
463 iface->bind = remote_op_bind;
464 iface->unbind = NULL;
466 iface->ndr_pull = remote_op_ndr_pull;
467 iface->dispatch = remote_op_dispatch;
468 iface->reply = remote_op_reply;
469 iface->ndr_push = remote_op_ndr_push;
471 iface->private_data = if_tabl;
477 static bool remote_op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
479 const struct ndr_interface_list *l;
481 for (l=ndr_table_list();l;l=l->next) {
482 if (l->table->syntax_id.if_version == if_version &&
483 GUID_equal(&l->table->syntax_id.uuid, uuid)==0) {
484 return remote_fill_interface(iface, l->table);
491 static bool remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
493 const struct ndr_interface_table *tbl = ndr_table_by_name(name);
496 return remote_fill_interface(iface, tbl);
501 NTSTATUS dcerpc_server_remote_init(TALLOC_CTX *ctx)
504 static const struct dcesrv_endpoint_server ep_server = {
505 /* fill in our name */
508 /* fill in all the operations */
509 .init_server = remote_op_init_server,
511 .interface_by_uuid = remote_op_interface_by_uuid,
512 .interface_by_name = remote_op_interface_by_name
515 /* register ourselves with the DCERPC subsystem. */
516 ret = dcerpc_register_ep_server(&ep_server);
517 if (!NT_STATUS_IS_OK(ret)) {
518 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
522 /* We need the full DCE/RPC interface table */