s3-shadow-copy2: Add more debugs
[Samba.git] / source4 / rpc_server / remote / dcesrv_remote.c
blobbe4bd1253e0a1ab57acee6f256a35603c30fc0ab
1 /*
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/>.
23 #include "includes.h"
24 #include <tevent.h>
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(void);
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)
39 return NT_STATUS_OK;
42 static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface, uint32_t if_version)
44 NTSTATUS status;
45 const struct ndr_interface_table *table;
46 struct dcesrv_remote_private *priv;
47 const char *binding = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "binding");
48 const char *user, *pass, *domain;
49 struct cli_credentials *credentials;
50 bool must_free_credentials = true;
51 bool machine_account;
52 struct dcerpc_binding *b;
53 struct composite_context *pipe_conn_req;
55 machine_account = lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "use_machine_account", false);
57 priv = talloc(dce_call->conn, struct dcesrv_remote_private);
58 if (!priv) {
59 return NT_STATUS_NO_MEMORY;
62 priv->c_pipe = NULL;
63 dce_call->context->private_data = priv;
65 if (!binding) {
66 DEBUG(0,("You must specify a DCE/RPC binding string\n"));
67 return NT_STATUS_INVALID_PARAMETER;
70 user = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "user");
71 pass = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "password");
72 domain = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dceprc_remote", "domain");
74 table = ndr_table_by_syntax(&iface->syntax_id);
75 if (!table) {
76 dce_call->fault_code = DCERPC_FAULT_UNK_IF;
77 return NT_STATUS_NET_WRITE_FAULT;
80 if (user && pass) {
81 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
82 credentials = cli_credentials_init(priv);
83 if (!credentials) {
84 return NT_STATUS_NO_MEMORY;
86 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
87 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
88 if (domain) {
89 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
91 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
92 } else if (machine_account) {
93 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using machine account\n"));
94 credentials = cli_credentials_init(priv);
95 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
96 if (domain) {
97 cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
99 status = cli_credentials_set_machine_account(credentials, dce_call->conn->dce_ctx->lp_ctx);
100 if (!NT_STATUS_IS_OK(status)) {
101 return status;
103 } else if (dce_call->conn->auth_state.session_info->credentials) {
104 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using delegated credentials\n"));
105 credentials = dce_call->conn->auth_state.session_info->credentials;
106 must_free_credentials = false;
107 } else {
108 DEBUG(1,("dcerpc_remote: RPC Proxy: You must supply binding, user and password or have delegated credentials\n"));
109 return NT_STATUS_INVALID_PARAMETER;
112 /* parse binding string to the structure */
113 status = dcerpc_parse_binding(dce_call->context, binding, &b);
114 if (!NT_STATUS_IS_OK(status)) {
115 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
116 return status;
119 /* If we already have a remote association group ID, then use that */
120 if (dce_call->context->assoc_group->proxied_id != 0) {
121 status = dcerpc_binding_set_assoc_group_id(b,
122 dce_call->context->assoc_group->proxied_id);
123 if (!NT_STATUS_IS_OK(status)) {
124 DEBUG(0, ("dcerpc_binding_set_assoc_group_id() - %s'\n",
125 nt_errstr(status)));
126 return status;
130 status = dcerpc_binding_set_abstract_syntax(b, &iface->syntax_id);
131 if (!NT_STATUS_IS_OK(status)) {
132 DEBUG(0, ("dcerpc_binding_set_abstract_syntax() - %s'\n",
133 nt_errstr(status)));
134 return status;
137 DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(dce_call->context, b)));
139 pipe_conn_req = dcerpc_pipe_connect_b_send(dce_call->context, b, table,
140 credentials, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx);
141 status = dcerpc_pipe_connect_b_recv(pipe_conn_req, dce_call->context, &(priv->c_pipe));
143 if (must_free_credentials) {
144 talloc_free(credentials);
147 if (!NT_STATUS_IS_OK(status)) {
148 return status;
151 if (dce_call->context->assoc_group->proxied_id == 0) {
152 dce_call->context->assoc_group->proxied_id =
153 dcerpc_binding_get_assoc_group_id(priv->c_pipe->binding);
156 if (!NT_STATUS_IS_OK(status)) {
157 return status;
160 return NT_STATUS_OK;
163 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
165 enum ndr_err_code ndr_err;
166 const struct ndr_interface_table *table = (const struct ndr_interface_table *)dce_call->context->iface->private_data;
167 uint16_t opnum = dce_call->pkt.u.request.opnum;
169 dce_call->fault_code = 0;
171 if (opnum >= table->num_calls) {
172 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
173 return NT_STATUS_NET_WRITE_FAULT;
177 * We don't have support for calls with pipes.
179 if (table->calls[opnum].in_pipes.num_pipes != 0) {
180 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
181 return NT_STATUS_NET_WRITE_FAULT;
183 if (table->calls[opnum].out_pipes.num_pipes != 0) {
184 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
185 return NT_STATUS_NET_WRITE_FAULT;
188 *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
189 if (!*r) {
190 return NT_STATUS_NO_MEMORY;
193 /* unravel the NDR for the packet */
194 ndr_err = table->calls[opnum].ndr_pull(pull, NDR_IN, *r);
195 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
196 dcerpc_log_packet(dce_call->conn->packet_log_dir,
197 table, opnum, NDR_IN,
198 &dce_call->pkt.u.request.stub_and_verifier);
199 dce_call->fault_code = DCERPC_FAULT_NDR;
200 return NT_STATUS_NET_WRITE_FAULT;
203 return NT_STATUS_OK;
206 static void remote_op_dispatch_done(struct tevent_req *subreq);
208 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
210 struct dcesrv_remote_private *priv = talloc_get_type_abort(dce_call->context->private_data,
211 struct dcesrv_remote_private);
212 uint16_t opnum = dce_call->pkt.u.request.opnum;
213 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
214 const struct ndr_interface_call *call;
215 const char *name;
216 struct tevent_req *subreq;
218 name = table->calls[opnum].name;
219 call = &table->calls[opnum];
221 if (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
222 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);
225 priv->c_pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
227 /* we didn't use the return code of this function as we only check the last_fault_code */
228 subreq = dcerpc_binding_handle_call_send(dce_call, dce_call->event_ctx,
229 priv->c_pipe->binding_handle,
230 NULL, table,
231 opnum, mem_ctx, r);
232 if (subreq == NULL) {
233 DEBUG(0,("dcesrv_remote: call[%s] dcerpc_binding_handle_call_send() failed!\n", name));
234 return NT_STATUS_NO_MEMORY;
236 tevent_req_set_callback(subreq, remote_op_dispatch_done, dce_call);
238 dce_call->state_flags |= DCESRV_CALL_STATE_FLAG_ASYNC;
239 return NT_STATUS_OK;
242 static void remote_op_dispatch_done(struct tevent_req *subreq)
244 struct dcesrv_call_state *dce_call = tevent_req_callback_data(subreq,
245 struct dcesrv_call_state);
246 struct dcesrv_remote_private *priv = talloc_get_type_abort(dce_call->context->private_data,
247 struct dcesrv_remote_private);
248 uint16_t opnum = dce_call->pkt.u.request.opnum;
249 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
250 const struct ndr_interface_call *call;
251 const char *name;
252 NTSTATUS status;
254 name = table->calls[opnum].name;
255 call = &table->calls[opnum];
257 /* we didn't use the return code of this function as we only check the last_fault_code */
258 status = dcerpc_binding_handle_call_recv(subreq);
259 TALLOC_FREE(subreq);
261 dce_call->fault_code = priv->c_pipe->last_fault_code;
262 if (dce_call->fault_code != 0) {
263 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",
264 name, dcerpc_errstr(dce_call, dce_call->fault_code)));
265 goto reply;
268 if (NT_STATUS_IS_OK(status) &&
269 (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
270 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, dce_call->r);
273 reply:
274 status = dcesrv_reply(dce_call);
275 if (!NT_STATUS_IS_OK(status)) {
276 DEBUG(0,("dcesrv_remote: call[%s]: dcesrv_reply() failed - %s\n",
277 name, nt_errstr(status)));
281 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
283 enum ndr_err_code ndr_err;
284 const struct ndr_interface_table *table = dce_call->context->iface->private_data;
285 uint16_t opnum = dce_call->pkt.u.request.opnum;
287 /* unravel the NDR for the packet */
288 ndr_err = table->calls[opnum].ndr_push(push, NDR_OUT, r);
289 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
290 dce_call->fault_code = DCERPC_FAULT_NDR;
291 return NT_STATUS_NET_WRITE_FAULT;
294 return NT_STATUS_OK;
297 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
299 unsigned int i;
300 const struct ndr_interface_table *table = iface->private_data;
302 for (i=0;i<table->endpoints->count;i++) {
303 NTSTATUS ret;
304 const char *name = table->endpoints->names[i];
306 ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
307 if (!NT_STATUS_IS_OK(ret)) {
308 DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
309 return ret;
313 return NT_STATUS_OK;
316 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
318 unsigned int i;
319 char **ifaces = str_list_make(dce_ctx, lpcfg_parm_string(dce_ctx->lp_ctx, NULL, "dcerpc_remote", "interfaces"),NULL);
321 if (!ifaces) {
322 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
323 return NT_STATUS_OK;
326 for (i=0;ifaces[i];i++) {
327 NTSTATUS ret;
328 struct dcesrv_interface iface;
330 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
331 DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
332 talloc_free(ifaces);
333 return NT_STATUS_UNSUCCESSFUL;
336 ret = remote_register_one_iface(dce_ctx, &iface);
337 if (!NT_STATUS_IS_OK(ret)) {
338 DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
339 talloc_free(ifaces);
340 return ret;
344 talloc_free(ifaces);
345 return NT_STATUS_OK;
348 static bool remote_fill_interface(struct dcesrv_interface *iface, const struct ndr_interface_table *if_tabl)
350 iface->name = if_tabl->name;
351 iface->syntax_id = if_tabl->syntax_id;
353 iface->bind = remote_op_bind;
354 iface->unbind = NULL;
356 iface->ndr_pull = remote_op_ndr_pull;
357 iface->dispatch = remote_op_dispatch;
358 iface->reply = remote_op_reply;
359 iface->ndr_push = remote_op_ndr_push;
361 iface->private_data = if_tabl;
363 return true;
366 static bool remote_op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
368 const struct ndr_interface_list *l;
370 for (l=ndr_table_list();l;l=l->next) {
371 if (l->table->syntax_id.if_version == if_version &&
372 GUID_equal(&l->table->syntax_id.uuid, uuid)==0) {
373 return remote_fill_interface(iface, l->table);
377 return false;
380 static bool remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
382 const struct ndr_interface_table *tbl = ndr_table_by_name(name);
384 if (tbl)
385 return remote_fill_interface(iface, tbl);
387 return false;
390 NTSTATUS dcerpc_server_remote_init(void)
392 NTSTATUS ret;
393 struct dcesrv_endpoint_server ep_server;
395 ZERO_STRUCT(ep_server);
397 /* fill in our name */
398 ep_server.name = "remote";
400 /* fill in all the operations */
401 ep_server.init_server = remote_op_init_server;
403 ep_server.interface_by_uuid = remote_op_interface_by_uuid;
404 ep_server.interface_by_name = remote_op_interface_by_name;
406 /* register ourselves with the DCERPC subsystem. */
407 ret = dcerpc_register_ep_server(&ep_server);
408 if (!NT_STATUS_IS_OK(ret)) {
409 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
410 return ret;
413 /* We need the full DCE/RPC interface table */
414 ndr_table_init();
416 return ret;