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;
53 struct dcerpc_binding *b;
54 struct composite_context *pipe_conn_req;
57 machine_account = lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "use_machine_account", false);
59 priv = talloc(dce_call->conn, struct dcesrv_remote_private);
61 return NT_STATUS_NO_MEMORY;
65 dce_call->context->private_data = priv;
67 binding = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx,
71 if (binding == NULL) {
72 DEBUG(0,("You must specify a DCE/RPC binding string\n"));
73 return NT_STATUS_INVALID_PARAMETER;
76 user = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "user");
77 pass = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "password");
78 domain = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dceprc_remote", "domain");
80 credentials = dcesrv_call_credentials(dce_call);
83 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
84 credentials = cli_credentials_init(priv);
86 return NT_STATUS_NO_MEMORY;
88 must_free_credentials = true;
89 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
90 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
92 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
94 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
95 } else if (machine_account) {
96 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using machine account\n"));
97 credentials = cli_credentials_init(priv);
99 return NT_STATUS_NO_MEMORY;
101 must_free_credentials = true;
102 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
104 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
106 status = cli_credentials_set_machine_account(credentials, dce_call->conn->dce_ctx->lp_ctx);
107 if (!NT_STATUS_IS_OK(status)) {
110 } else if (credentials != NULL) {
111 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using delegated credentials\n"));
113 DEBUG(1,("dcerpc_remote: RPC Proxy: You must supply binding, user and password or have delegated credentials\n"));
114 return NT_STATUS_INVALID_PARAMETER;
117 /* parse binding string to the structure */
118 status = dcerpc_parse_binding(dce_call->context, binding, &b);
119 if (!NT_STATUS_IS_OK(status)) {
120 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
124 /* If we already have a remote association group ID, then use that */
125 if (dce_call->conn->assoc_group->proxied_id != 0) {
126 status = dcerpc_binding_set_assoc_group_id(b,
127 dce_call->conn->assoc_group->proxied_id);
128 if (!NT_STATUS_IS_OK(status)) {
129 DEBUG(0, ("dcerpc_binding_set_assoc_group_id() - %s'\n",
135 status = dcerpc_binding_set_abstract_syntax(b, &table->syntax_id);
136 if (!NT_STATUS_IS_OK(status)) {
137 DEBUG(0, ("dcerpc_binding_set_abstract_syntax() - %s'\n",
142 if (dce_call->pkt.pfc_flags & DCERPC_PFC_FLAG_CONC_MPX) {
143 status = dcerpc_binding_set_flags(b, DCERPC_CONCURRENT_MULTIPLEX, 0);
144 if (!NT_STATUS_IS_OK(status)) {
145 DEBUG(0, ("dcerpc_binding_set_flags(CONC_MPX) - %s'\n",
151 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(dce_call->context, b)));
153 pipe_conn_req = dcerpc_pipe_connect_b_send(dce_call->context, b, table,
154 credentials, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx);
155 status = dcerpc_pipe_connect_b_recv(pipe_conn_req, dce_call->context, &(priv->c_pipe));
157 if (must_free_credentials) {
158 talloc_free(credentials);
161 if (!NT_STATUS_IS_OK(status)) {
165 flags = dcerpc_binding_get_flags(priv->c_pipe->binding);
166 if (!(flags & DCERPC_CONCURRENT_MULTIPLEX)) {
167 dce_call->state_flags &= ~DCESRV_CALL_STATE_FLAG_MULTIPLEXED;
170 if (dce_call->conn->assoc_group->proxied_id == 0) {
171 dce_call->conn->assoc_group->proxied_id =
172 dcerpc_binding_get_assoc_group_id(priv->c_pipe->binding);
175 if (!NT_STATUS_IS_OK(status)) {
182 static NTSTATUS remote_get_private(struct dcesrv_call_state *dce_call,
183 struct dcesrv_remote_private **_priv)
186 struct dcesrv_remote_private *priv = NULL;
188 ptr = dce_call->context->private_data;
189 priv = talloc_get_type_abort(ptr, struct dcesrv_remote_private);
195 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
197 enum ndr_err_code ndr_err;
198 const struct ndr_interface_table *table = (const struct ndr_interface_table *)dce_call->context->iface->private_data;
199 uint16_t opnum = dce_call->pkt.u.request.opnum;
201 dce_call->fault_code = 0;
203 if (opnum >= table->num_calls) {
204 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
205 return NT_STATUS_NET_WRITE_FAULT;
209 * We don't have support for calls with pipes.
211 if (table->calls[opnum].in_pipes.num_pipes != 0) {
212 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
213 return NT_STATUS_NET_WRITE_FAULT;
215 if (table->calls[opnum].out_pipes.num_pipes != 0) {
216 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
217 return NT_STATUS_NET_WRITE_FAULT;
220 *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
222 return NT_STATUS_NO_MEMORY;
225 /* unravel the NDR for the packet */
226 ndr_err = table->calls[opnum].ndr_pull(pull, NDR_IN, *r);
227 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
228 dcerpc_log_packet(dce_call->conn->packet_log_dir,
229 table, opnum, NDR_IN,
230 &dce_call->pkt.u.request.stub_and_verifier);
231 dce_call->fault_code = DCERPC_FAULT_NDR;
232 return NT_STATUS_NET_WRITE_FAULT;
238 static void remote_op_dispatch_done(struct tevent_req *subreq);
240 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
242 struct dcesrv_remote_private *priv = NULL;
243 uint16_t opnum = dce_call->pkt.u.request.opnum;
244 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
245 const struct ndr_interface_call *call;
247 struct tevent_req *subreq;
250 status = remote_get_private(dce_call, &priv);
251 if (!NT_STATUS_IS_OK(status)) {
255 name = table->calls[opnum].name;
256 call = &table->calls[opnum];
258 if (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
259 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);
262 priv->c_pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
264 /* we didn't use the return code of this function as we only check the last_fault_code */
265 subreq = dcerpc_binding_handle_call_send(dce_call, dce_call->event_ctx,
266 priv->c_pipe->binding_handle,
269 if (subreq == NULL) {
270 DEBUG(0,("dcesrv_remote: call[%s] dcerpc_binding_handle_call_send() failed!\n", name));
271 return NT_STATUS_NO_MEMORY;
273 tevent_req_set_callback(subreq, remote_op_dispatch_done, dce_call);
275 dce_call->state_flags |= DCESRV_CALL_STATE_FLAG_ASYNC;
279 static void remote_op_dispatch_done(struct tevent_req *subreq)
281 struct dcesrv_call_state *dce_call = tevent_req_callback_data(subreq,
282 struct dcesrv_call_state);
283 struct dcesrv_remote_private *priv = talloc_get_type_abort(dce_call->context->private_data,
284 struct dcesrv_remote_private);
285 uint16_t opnum = dce_call->pkt.u.request.opnum;
286 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
287 const struct ndr_interface_call *call;
291 name = table->calls[opnum].name;
292 call = &table->calls[opnum];
294 /* we didn't use the return code of this function as we only check the last_fault_code */
295 status = dcerpc_binding_handle_call_recv(subreq);
298 dce_call->fault_code = priv->c_pipe->last_fault_code;
299 if (dce_call->fault_code != 0) {
300 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",
301 name, dcerpc_errstr(dce_call, dce_call->fault_code)));
305 if (NT_STATUS_IS_OK(status) &&
306 (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
307 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, dce_call->r);
311 status = dcesrv_reply(dce_call);
312 if (!NT_STATUS_IS_OK(status)) {
313 DEBUG(0,("dcesrv_remote: call[%s]: dcesrv_reply() failed - %s\n",
314 name, nt_errstr(status)));
318 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
320 enum ndr_err_code ndr_err;
321 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
322 uint16_t opnum = dce_call->pkt.u.request.opnum;
324 /* unravel the NDR for the packet */
325 ndr_err = table->calls[opnum].ndr_push(push, NDR_OUT, r);
326 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
327 dce_call->fault_code = DCERPC_FAULT_NDR;
328 return NT_STATUS_NET_WRITE_FAULT;
334 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
337 const struct ndr_interface_table *table = iface->private_data;
339 for (i=0;i<table->endpoints->count;i++) {
341 const char *name = table->endpoints->names[i];
343 ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
344 if (!NT_STATUS_IS_OK(ret)) {
345 DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
353 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
356 char **ifaces = str_list_make(dce_ctx, lpcfg_parm_string(dce_ctx->lp_ctx, NULL, "dcerpc_remote", "interfaces"),NULL);
359 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
363 for (i=0;ifaces[i];i++) {
365 struct dcesrv_interface iface;
367 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
368 DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
370 return NT_STATUS_UNSUCCESSFUL;
373 ret = remote_register_one_iface(dce_ctx, &iface);
374 if (!NT_STATUS_IS_OK(ret)) {
375 DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
385 static bool remote_fill_interface(struct dcesrv_interface *iface, const struct ndr_interface_table *if_tabl)
387 iface->name = if_tabl->name;
388 iface->syntax_id = if_tabl->syntax_id;
390 iface->bind = remote_op_bind;
391 iface->unbind = NULL;
393 iface->ndr_pull = remote_op_ndr_pull;
394 iface->dispatch = remote_op_dispatch;
395 iface->reply = remote_op_reply;
396 iface->ndr_push = remote_op_ndr_push;
398 iface->private_data = if_tabl;
404 static bool remote_op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
406 const struct ndr_interface_list *l;
408 for (l=ndr_table_list();l;l=l->next) {
409 if (l->table->syntax_id.if_version == if_version &&
410 GUID_equal(&l->table->syntax_id.uuid, uuid)==0) {
411 return remote_fill_interface(iface, l->table);
418 static bool remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
420 const struct ndr_interface_table *tbl = ndr_table_by_name(name);
423 return remote_fill_interface(iface, tbl);
428 NTSTATUS dcerpc_server_remote_init(TALLOC_CTX *ctx)
431 static const struct dcesrv_endpoint_server ep_server = {
432 /* fill in our name */
435 /* fill in all the operations */
436 .init_server = remote_op_init_server,
438 .interface_by_uuid = remote_op_interface_by_uuid,
439 .interface_by_name = remote_op_interface_by_name
442 /* register ourselves with the DCERPC subsystem. */
443 ret = dcerpc_register_ep_server(&ep_server);
444 if (!NT_STATUS_IS_OK(ret)) {
445 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
449 /* We need the full DCE/RPC interface table */