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 flags = dcerpc_binding_get_flags(priv->c_pipe->binding);
183 if (!(flags & DCERPC_CONCURRENT_MULTIPLEX)) {
184 dce_call->state_flags &= ~DCESRV_CALL_STATE_FLAG_MULTIPLEXED;
187 if (dce_call->conn->assoc_group->proxied_id == 0) {
188 dce_call->conn->assoc_group->proxied_id =
189 dcerpc_binding_get_assoc_group_id(priv->c_pipe->binding);
192 if (!NT_STATUS_IS_OK(status)) {
199 static NTSTATUS remote_get_private(struct dcesrv_call_state *dce_call,
200 struct dcesrv_remote_private **_priv)
203 struct dcesrv_remote_private *priv = NULL;
205 ptr = dce_call->context->private_data;
206 priv = talloc_get_type_abort(ptr, struct dcesrv_remote_private);
212 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
214 enum ndr_err_code ndr_err;
215 const struct ndr_interface_table *table = (const struct ndr_interface_table *)dce_call->context->iface->private_data;
216 uint16_t opnum = dce_call->pkt.u.request.opnum;
218 dce_call->fault_code = 0;
220 if (opnum >= table->num_calls) {
221 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
222 return NT_STATUS_NET_WRITE_FAULT;
226 * We don't have support for calls with pipes.
228 if (table->calls[opnum].in_pipes.num_pipes != 0) {
229 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
230 return NT_STATUS_NET_WRITE_FAULT;
232 if (table->calls[opnum].out_pipes.num_pipes != 0) {
233 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
234 return NT_STATUS_NET_WRITE_FAULT;
237 *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
239 return NT_STATUS_NO_MEMORY;
242 /* unravel the NDR for the packet */
243 ndr_err = table->calls[opnum].ndr_pull(pull, NDR_IN, *r);
244 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
245 dcerpc_log_packet(dce_call->conn->packet_log_dir,
246 table, opnum, NDR_IN,
247 &dce_call->pkt.u.request.stub_and_verifier);
248 dce_call->fault_code = DCERPC_FAULT_NDR;
249 return NT_STATUS_NET_WRITE_FAULT;
255 static void remote_op_dispatch_done(struct tevent_req *subreq);
257 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
259 struct dcesrv_remote_private *priv = NULL;
260 uint16_t opnum = dce_call->pkt.u.request.opnum;
261 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
262 const struct ndr_interface_call *call;
264 struct tevent_req *subreq;
267 status = remote_get_private(dce_call, &priv);
268 if (!NT_STATUS_IS_OK(status)) {
272 name = table->calls[opnum].name;
273 call = &table->calls[opnum];
275 if (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
276 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);
279 priv->c_pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
281 /* we didn't use the return code of this function as we only check the last_fault_code */
282 subreq = dcerpc_binding_handle_call_send(dce_call, dce_call->event_ctx,
283 priv->c_pipe->binding_handle,
286 if (subreq == NULL) {
287 DEBUG(0,("dcesrv_remote: call[%s] dcerpc_binding_handle_call_send() failed!\n", name));
288 return NT_STATUS_NO_MEMORY;
290 tevent_req_set_callback(subreq, remote_op_dispatch_done, dce_call);
292 dce_call->state_flags |= DCESRV_CALL_STATE_FLAG_ASYNC;
296 static void remote_op_dispatch_done(struct tevent_req *subreq)
298 struct dcesrv_call_state *dce_call = tevent_req_callback_data(subreq,
299 struct dcesrv_call_state);
300 struct dcesrv_remote_private *priv = talloc_get_type_abort(dce_call->context->private_data,
301 struct dcesrv_remote_private);
302 uint16_t opnum = dce_call->pkt.u.request.opnum;
303 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
304 const struct ndr_interface_call *call;
308 name = table->calls[opnum].name;
309 call = &table->calls[opnum];
311 /* we didn't use the return code of this function as we only check the last_fault_code */
312 status = dcerpc_binding_handle_call_recv(subreq);
315 dce_call->fault_code = priv->c_pipe->last_fault_code;
316 if (dce_call->fault_code != 0) {
317 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",
318 name, dcerpc_errstr(dce_call, dce_call->fault_code)));
322 if (NT_STATUS_IS_OK(status) &&
323 (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
324 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, dce_call->r);
328 status = dcesrv_reply(dce_call);
329 if (!NT_STATUS_IS_OK(status)) {
330 DEBUG(0,("dcesrv_remote: call[%s]: dcesrv_reply() failed - %s\n",
331 name, nt_errstr(status)));
335 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
337 enum ndr_err_code ndr_err;
338 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
339 uint16_t opnum = dce_call->pkt.u.request.opnum;
341 /* unravel the NDR for the packet */
342 ndr_err = table->calls[opnum].ndr_push(push, NDR_OUT, r);
343 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
344 dce_call->fault_code = DCERPC_FAULT_NDR;
345 return NT_STATUS_NET_WRITE_FAULT;
351 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
354 const struct ndr_interface_table *table = iface->private_data;
356 for (i=0;i<table->endpoints->count;i++) {
358 const char *name = table->endpoints->names[i];
360 ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
361 if (!NT_STATUS_IS_OK(ret)) {
362 DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
370 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
373 char **ifaces = str_list_make(dce_ctx, lpcfg_parm_string(dce_ctx->lp_ctx, NULL, "dcerpc_remote", "interfaces"),NULL);
376 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
380 for (i=0;ifaces[i];i++) {
382 struct dcesrv_interface iface;
384 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
385 DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
387 return NT_STATUS_UNSUCCESSFUL;
390 ret = remote_register_one_iface(dce_ctx, &iface);
391 if (!NT_STATUS_IS_OK(ret)) {
392 DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
402 static bool remote_fill_interface(struct dcesrv_interface *iface, const struct ndr_interface_table *if_tabl)
404 iface->name = if_tabl->name;
405 iface->syntax_id = if_tabl->syntax_id;
407 iface->bind = remote_op_bind;
408 iface->unbind = NULL;
410 iface->ndr_pull = remote_op_ndr_pull;
411 iface->dispatch = remote_op_dispatch;
412 iface->reply = remote_op_reply;
413 iface->ndr_push = remote_op_ndr_push;
415 iface->private_data = if_tabl;
421 static bool remote_op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
423 const struct ndr_interface_list *l;
425 for (l=ndr_table_list();l;l=l->next) {
426 if (l->table->syntax_id.if_version == if_version &&
427 GUID_equal(&l->table->syntax_id.uuid, uuid)==0) {
428 return remote_fill_interface(iface, l->table);
435 static bool remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
437 const struct ndr_interface_table *tbl = ndr_table_by_name(name);
440 return remote_fill_interface(iface, tbl);
445 NTSTATUS dcerpc_server_remote_init(TALLOC_CTX *ctx)
448 static const struct dcesrv_endpoint_server ep_server = {
449 /* fill in our name */
452 /* fill in all the operations */
453 .init_server = remote_op_init_server,
455 .interface_by_uuid = remote_op_interface_by_uuid,
456 .interface_by_name = remote_op_interface_by_name
459 /* register ourselves with the DCERPC subsystem. */
460 ret = dcerpc_register_ep_server(&ep_server);
461 if (!NT_STATUS_IS_OK(ret)) {
462 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
466 /* We need the full DCE/RPC interface table */