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;
296 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
298 struct dcesrv_remote_call *rcall = NULL;
299 struct dcesrv_remote_private *priv = NULL;
300 uint16_t opnum = dce_call->pkt.u.request.opnum;
301 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
302 const struct ndr_interface_call *call;
304 struct tevent_req *subreq;
307 name = table->calls[opnum].name;
308 call = &table->calls[opnum];
310 status = remote_get_private(dce_call, &priv);
311 if (!NT_STATUS_IS_OK(status)) {
312 DEBUG(0,("dcesrv_remote: call[%s] %s\n", name, nt_errstr(status)));
316 rcall = talloc_zero(dce_call, struct dcesrv_remote_call);
318 return NT_STATUS_NO_MEMORY;
320 rcall->dce_call = dce_call;
323 if (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
324 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);
327 priv->c_pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
329 /* we didn't use the return code of this function as we only check the last_fault_code */
330 subreq = dcerpc_binding_handle_call_send(rcall, dce_call->event_ctx,
331 priv->c_pipe->binding_handle,
334 if (subreq == NULL) {
335 DEBUG(0,("dcesrv_remote: call[%s] dcerpc_binding_handle_call_send() failed!\n", name));
336 return NT_STATUS_NO_MEMORY;
338 tevent_req_set_callback(subreq, remote_op_dispatch_done, rcall);
340 dce_call->state_flags |= DCESRV_CALL_STATE_FLAG_ASYNC;
344 static void remote_op_dispatch_done(struct tevent_req *subreq)
346 struct dcesrv_remote_call *rcall =
347 tevent_req_callback_data(subreq,
348 struct dcesrv_remote_call);
349 struct dcesrv_call_state *dce_call = rcall->dce_call;
350 struct dcesrv_remote_private *priv = rcall->priv;
351 uint16_t opnum = dce_call->pkt.u.request.opnum;
352 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
353 const struct ndr_interface_call *call;
357 name = table->calls[opnum].name;
358 call = &table->calls[opnum];
360 /* we didn't use the return code of this function as we only check the last_fault_code */
361 status = dcerpc_binding_handle_call_recv(subreq);
364 dce_call->fault_code = priv->c_pipe->last_fault_code;
365 if (dce_call->fault_code != 0) {
366 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",
367 name, dcerpc_errstr(dce_call, dce_call->fault_code)));
371 if (NT_STATUS_IS_OK(status) &&
372 (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
373 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, dce_call->r);
377 status = dcesrv_reply(dce_call);
378 if (!NT_STATUS_IS_OK(status)) {
379 DEBUG(0,("dcesrv_remote: call[%s]: dcesrv_reply() failed - %s\n",
380 name, nt_errstr(status)));
384 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
386 enum ndr_err_code ndr_err;
387 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
388 uint16_t opnum = dce_call->pkt.u.request.opnum;
390 /* unravel the NDR for the packet */
391 ndr_err = table->calls[opnum].ndr_push(push, NDR_OUT, r);
392 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
393 dce_call->fault_code = DCERPC_FAULT_NDR;
394 return NT_STATUS_NET_WRITE_FAULT;
400 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
403 const struct ndr_interface_table *table = iface->private_data;
405 for (i=0;i<table->endpoints->count;i++) {
407 const char *name = table->endpoints->names[i];
409 ret = dcesrv_interface_register(dce_ctx, name, NULL, iface, NULL);
410 if (!NT_STATUS_IS_OK(ret)) {
411 DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
419 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
422 char **ifaces = str_list_make(dce_ctx, lpcfg_parm_string(dce_ctx->lp_ctx, NULL, "dcerpc_remote", "interfaces"),NULL);
425 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
429 for (i=0;ifaces[i];i++) {
431 struct dcesrv_interface iface;
433 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
434 DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
436 return NT_STATUS_UNSUCCESSFUL;
439 ret = remote_register_one_iface(dce_ctx, &iface);
440 if (!NT_STATUS_IS_OK(ret)) {
441 DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
451 static bool remote_fill_interface(struct dcesrv_interface *iface, const struct ndr_interface_table *if_tabl)
453 iface->name = if_tabl->name;
454 iface->syntax_id = if_tabl->syntax_id;
456 iface->bind = remote_op_bind;
457 iface->unbind = NULL;
459 iface->ndr_pull = remote_op_ndr_pull;
460 iface->dispatch = remote_op_dispatch;
461 iface->reply = remote_op_reply;
462 iface->ndr_push = remote_op_ndr_push;
464 iface->private_data = if_tabl;
470 static bool remote_op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
472 const struct ndr_interface_list *l;
474 for (l=ndr_table_list();l;l=l->next) {
475 if (l->table->syntax_id.if_version == if_version &&
476 GUID_equal(&l->table->syntax_id.uuid, uuid)==0) {
477 return remote_fill_interface(iface, l->table);
484 static bool remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
486 const struct ndr_interface_table *tbl = ndr_table_by_name(name);
489 return remote_fill_interface(iface, tbl);
494 NTSTATUS dcerpc_server_remote_init(TALLOC_CTX *ctx)
497 static const struct dcesrv_endpoint_server ep_server = {
498 /* fill in our name */
501 /* fill in all the operations */
502 .init_server = remote_op_init_server,
504 .interface_by_uuid = remote_op_interface_by_uuid,
505 .interface_by_name = remote_op_interface_by_name
508 /* register ourselves with the DCERPC subsystem. */
509 ret = dcerpc_register_ep_server(&ep_server);
510 if (!NT_STATUS_IS_OK(ret)) {
511 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
515 /* We need the full DCE/RPC interface table */