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 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)
42 static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface, uint32_t if_version)
45 const struct ndr_interface_table *table =
46 (const struct ndr_interface_table *)dce_call->context->iface->private_data;
47 struct dcesrv_remote_private *priv;
48 const char *binding = NULL;
49 const char *user, *pass, *domain;
50 struct cli_credentials *credentials;
51 bool must_free_credentials = false;
54 struct dcerpc_binding *b;
55 struct composite_context *pipe_conn_req;
58 priv = talloc(dce_call->conn, struct dcesrv_remote_private);
60 return NT_STATUS_NO_MEMORY;
64 dce_call->context->private_data = priv;
66 binding = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx,
70 if (binding == NULL) {
71 DEBUG(0,("You must specify a DCE/RPC binding string\n"));
72 return NT_STATUS_INVALID_PARAMETER;
75 user = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "user");
76 pass = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "password");
77 domain = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dceprc_remote", "domain");
79 machine_account = lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx,
82 "use_machine_account",
84 allow_anonymous = lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx,
87 "allow_anonymous_fallback",
90 credentials = dcesrv_call_credentials(dce_call);
93 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
94 credentials = cli_credentials_init(priv);
96 return NT_STATUS_NO_MEMORY;
98 must_free_credentials = true;
99 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
100 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
102 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
104 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
105 } else if (machine_account) {
106 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using machine account\n"));
107 credentials = cli_credentials_init(priv);
109 return NT_STATUS_NO_MEMORY;
111 must_free_credentials = true;
112 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
114 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
116 status = cli_credentials_set_machine_account(credentials, dce_call->conn->dce_ctx->lp_ctx);
117 if (!NT_STATUS_IS_OK(status)) {
120 } else if (credentials != NULL) {
121 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using delegated credentials\n"));
122 } else if (allow_anonymous) {
123 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using anonymous\n"));
124 credentials = cli_credentials_init_anon(priv);
126 return NT_STATUS_NO_MEMORY;
128 must_free_credentials = true;
130 DEBUG(1,("dcerpc_remote: RPC Proxy: You must supply binding, user and password or have delegated credentials\n"));
131 return NT_STATUS_INVALID_PARAMETER;
134 /* parse binding string to the structure */
135 status = dcerpc_parse_binding(dce_call->context, binding, &b);
136 if (!NT_STATUS_IS_OK(status)) {
137 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
141 /* If we already have a remote association group ID, then use that */
142 if (dce_call->conn->assoc_group->proxied_id != 0) {
143 status = dcerpc_binding_set_assoc_group_id(b,
144 dce_call->conn->assoc_group->proxied_id);
145 if (!NT_STATUS_IS_OK(status)) {
146 DEBUG(0, ("dcerpc_binding_set_assoc_group_id() - %s'\n",
152 status = dcerpc_binding_set_abstract_syntax(b, &table->syntax_id);
153 if (!NT_STATUS_IS_OK(status)) {
154 DEBUG(0, ("dcerpc_binding_set_abstract_syntax() - %s'\n",
159 if (dce_call->pkt.pfc_flags & DCERPC_PFC_FLAG_CONC_MPX) {
160 status = dcerpc_binding_set_flags(b, DCERPC_CONCURRENT_MULTIPLEX, 0);
161 if (!NT_STATUS_IS_OK(status)) {
162 DEBUG(0, ("dcerpc_binding_set_flags(CONC_MPX) - %s'\n",
168 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(dce_call->context, b)));
170 pipe_conn_req = dcerpc_pipe_connect_b_send(dce_call->context, b, table,
171 credentials, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx);
172 status = dcerpc_pipe_connect_b_recv(pipe_conn_req, dce_call->context, &(priv->c_pipe));
174 if (must_free_credentials) {
175 talloc_free(credentials);
178 if (!NT_STATUS_IS_OK(status)) {
182 if (dce_call->pkt.pfc_flags & DCERPC_PFC_FLAG_CONC_MPX) {
183 flags = dcerpc_binding_get_flags(priv->c_pipe->binding);
184 if (!(flags & DCERPC_CONCURRENT_MULTIPLEX)) {
185 DEBUG(1,("dcerpc_remote: RPC Proxy: "
186 "Remote server doesn't support MPX\n"));
187 return NT_STATUS_INVALID_NETWORK_RESPONSE;
191 if (dce_call->conn->assoc_group->proxied_id == 0) {
192 dce_call->conn->assoc_group->proxied_id =
193 dcerpc_binding_get_assoc_group_id(priv->c_pipe->binding);
196 if (!NT_STATUS_IS_OK(status)) {
203 static NTSTATUS remote_get_private(struct dcesrv_call_state *dce_call,
204 struct dcesrv_remote_private **_priv)
207 struct dcesrv_remote_private *priv = NULL;
209 ptr = dce_call->context->private_data;
210 priv = talloc_get_type_abort(ptr, struct dcesrv_remote_private);
216 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
218 enum ndr_err_code ndr_err;
219 const struct ndr_interface_table *table = (const struct ndr_interface_table *)dce_call->context->iface->private_data;
220 uint16_t opnum = dce_call->pkt.u.request.opnum;
222 dce_call->fault_code = 0;
224 if (opnum >= table->num_calls) {
225 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
226 return NT_STATUS_NET_WRITE_FAULT;
230 * We don't have support for calls with pipes.
232 if (table->calls[opnum].in_pipes.num_pipes != 0) {
233 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
234 return NT_STATUS_NET_WRITE_FAULT;
236 if (table->calls[opnum].out_pipes.num_pipes != 0) {
237 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
238 return NT_STATUS_NET_WRITE_FAULT;
241 *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
243 return NT_STATUS_NO_MEMORY;
246 /* unravel the NDR for the packet */
247 ndr_err = table->calls[opnum].ndr_pull(pull, NDR_IN, *r);
248 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
249 dcerpc_log_packet(dce_call->conn->packet_log_dir,
250 table, opnum, NDR_IN,
251 &dce_call->pkt.u.request.stub_and_verifier);
252 dce_call->fault_code = DCERPC_FAULT_NDR;
253 return NT_STATUS_NET_WRITE_FAULT;
259 static void remote_op_dispatch_done(struct tevent_req *subreq);
261 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
263 struct dcesrv_remote_private *priv = NULL;
264 uint16_t opnum = dce_call->pkt.u.request.opnum;
265 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
266 const struct ndr_interface_call *call;
268 struct tevent_req *subreq;
271 status = remote_get_private(dce_call, &priv);
272 if (!NT_STATUS_IS_OK(status)) {
276 name = table->calls[opnum].name;
277 call = &table->calls[opnum];
279 if (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
280 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);
283 priv->c_pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
285 /* we didn't use the return code of this function as we only check the last_fault_code */
286 subreq = dcerpc_binding_handle_call_send(dce_call, dce_call->event_ctx,
287 priv->c_pipe->binding_handle,
290 if (subreq == NULL) {
291 DEBUG(0,("dcesrv_remote: call[%s] dcerpc_binding_handle_call_send() failed!\n", name));
292 return NT_STATUS_NO_MEMORY;
294 tevent_req_set_callback(subreq, remote_op_dispatch_done, dce_call);
296 dce_call->state_flags |= DCESRV_CALL_STATE_FLAG_ASYNC;
300 static void remote_op_dispatch_done(struct tevent_req *subreq)
302 struct dcesrv_call_state *dce_call = tevent_req_callback_data(subreq,
303 struct dcesrv_call_state);
304 struct dcesrv_remote_private *priv = talloc_get_type_abort(dce_call->context->private_data,
305 struct dcesrv_remote_private);
306 uint16_t opnum = dce_call->pkt.u.request.opnum;
307 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
308 const struct ndr_interface_call *call;
312 name = table->calls[opnum].name;
313 call = &table->calls[opnum];
315 /* we didn't use the return code of this function as we only check the last_fault_code */
316 status = dcerpc_binding_handle_call_recv(subreq);
319 dce_call->fault_code = priv->c_pipe->last_fault_code;
320 if (dce_call->fault_code != 0) {
321 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",
322 name, dcerpc_errstr(dce_call, dce_call->fault_code)));
326 if (NT_STATUS_IS_OK(status) &&
327 (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
328 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, dce_call->r);
332 status = dcesrv_reply(dce_call);
333 if (!NT_STATUS_IS_OK(status)) {
334 DEBUG(0,("dcesrv_remote: call[%s]: dcesrv_reply() failed - %s\n",
335 name, nt_errstr(status)));
339 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
341 enum ndr_err_code ndr_err;
342 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
343 uint16_t opnum = dce_call->pkt.u.request.opnum;
345 /* unravel the NDR for the packet */
346 ndr_err = table->calls[opnum].ndr_push(push, NDR_OUT, r);
347 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
348 dce_call->fault_code = DCERPC_FAULT_NDR;
349 return NT_STATUS_NET_WRITE_FAULT;
355 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
358 const struct ndr_interface_table *table = iface->private_data;
360 for (i=0;i<table->endpoints->count;i++) {
362 const char *name = table->endpoints->names[i];
364 ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
365 if (!NT_STATUS_IS_OK(ret)) {
366 DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
374 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
377 char **ifaces = str_list_make(dce_ctx, lpcfg_parm_string(dce_ctx->lp_ctx, NULL, "dcerpc_remote", "interfaces"),NULL);
380 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
384 for (i=0;ifaces[i];i++) {
386 struct dcesrv_interface iface;
388 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
389 DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
391 return NT_STATUS_UNSUCCESSFUL;
394 ret = remote_register_one_iface(dce_ctx, &iface);
395 if (!NT_STATUS_IS_OK(ret)) {
396 DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
406 static bool remote_fill_interface(struct dcesrv_interface *iface, const struct ndr_interface_table *if_tabl)
408 iface->name = if_tabl->name;
409 iface->syntax_id = if_tabl->syntax_id;
411 iface->bind = remote_op_bind;
412 iface->unbind = NULL;
414 iface->ndr_pull = remote_op_ndr_pull;
415 iface->dispatch = remote_op_dispatch;
416 iface->reply = remote_op_reply;
417 iface->ndr_push = remote_op_ndr_push;
419 iface->private_data = if_tabl;
425 static bool remote_op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
427 const struct ndr_interface_list *l;
429 for (l=ndr_table_list();l;l=l->next) {
430 if (l->table->syntax_id.if_version == if_version &&
431 GUID_equal(&l->table->syntax_id.uuid, uuid)==0) {
432 return remote_fill_interface(iface, l->table);
439 static bool remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
441 const struct ndr_interface_table *tbl = ndr_table_by_name(name);
444 return remote_fill_interface(iface, tbl);
449 NTSTATUS dcerpc_server_remote_init(TALLOC_CTX *ctx)
452 static const struct dcesrv_endpoint_server ep_server = {
453 /* fill in our name */
456 /* fill in all the operations */
457 .init_server = remote_op_init_server,
459 .interface_by_uuid = remote_op_interface_by_uuid,
460 .interface_by_name = remote_op_interface_by_name
463 /* register ourselves with the DCERPC subsystem. */
464 ret = dcerpc_register_ep_server(&ep_server);
465 if (!NT_STATUS_IS_OK(ret)) {
466 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
470 /* We need the full DCE/RPC interface table */