a4abb4bc2cd3498878e07ab13a31fcc4d62dbf63
[gd/samba-autobuild/.git] / source4 / rpc_server / remote / dcesrv_remote.c
1 /* 
2    Unix SMB/CIFS implementation.
3    remote dcerpc operations
4
5    Copyright (C) Stefan (metze) Metzmacher 2004
6    Copyright (C) Julien Kerihuel 2008-2009
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2010
8
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.
13    
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.
18    
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/>.
21 */
22
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"
30
31 NTSTATUS dcerpc_server_remote_init(TALLOC_CTX *ctx);
32
33 #define DCESRV_REMOTE_ASSOC_MAGIC 0x782f50c4
34 struct dcesrv_remote_assoc {
35         uint32_t assoc_group_id;
36 };
37
38 #define DCESRV_REMOTE_PRIVATE_MAGIC 0x7eceafa6
39 struct dcesrv_remote_private {
40         struct dcerpc_pipe *c_pipe;
41 };
42
43 static NTSTATUS remote_op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
44 {
45         return NT_STATUS_OK;
46 }
47
48 static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface, uint32_t if_version)
49 {
50         return NT_STATUS_OK;
51 }
52
53 static NTSTATUS remote_get_private(struct dcesrv_call_state *dce_call,
54                                    struct dcesrv_remote_private **_priv)
55 {
56         const struct ndr_interface_table *table =
57                 (const struct ndr_interface_table *)dce_call->context->iface->private_data;
58         struct dcesrv_remote_private *priv = NULL;
59         struct dcesrv_remote_assoc *assoc = NULL;
60         const char *binding = NULL;
61         const char *user, *pass, *domain;
62         struct cli_credentials *credentials;
63         bool must_free_credentials = false;
64         bool machine_account;
65         bool allow_anonymous;
66         struct dcerpc_binding           *b;
67         struct composite_context        *pipe_conn_req;
68         uint32_t flags = 0;
69         NTSTATUS status;
70
71         priv = dcesrv_iface_state_find_conn(dce_call,
72                                             DCESRV_REMOTE_PRIVATE_MAGIC,
73                                             struct dcesrv_remote_private);
74         if (priv != NULL) {
75                 *_priv = priv;
76                 return NT_STATUS_OK;
77         }
78
79         priv = talloc_zero(dce_call, struct dcesrv_remote_private);
80         if (priv == NULL) {
81                 return NT_STATUS_NO_MEMORY;
82         }
83
84         assoc = dcesrv_iface_state_find_assoc(dce_call,
85                                         DCESRV_REMOTE_ASSOC_MAGIC,
86                                         struct dcesrv_remote_assoc);
87         if (assoc == NULL) {
88                 assoc = talloc_zero(dce_call, struct dcesrv_remote_assoc);
89                 if (assoc == NULL) {
90                         return NT_STATUS_NO_MEMORY;
91                 }
92         }
93
94         binding = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx,
95                                     NULL,
96                                     "dcerpc_remote",
97                                     "binding");
98         if (binding == NULL) {
99                 DEBUG(0,("You must specify a DCE/RPC binding string\n"));
100                 return NT_STATUS_INVALID_PARAMETER;
101         }
102
103         user = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "user");
104         pass = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dcerpc_remote", "password");
105         domain = lpcfg_parm_string(dce_call->conn->dce_ctx->lp_ctx, NULL, "dceprc_remote", "domain");
106
107         machine_account = lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx,
108                                           NULL,
109                                           "dcerpc_remote",
110                                           "use_machine_account",
111                                           false);
112         allow_anonymous = lpcfg_parm_bool(dce_call->conn->dce_ctx->lp_ctx,
113                                           NULL,
114                                           "dcerpc_remote",
115                                           "allow_anonymous_fallback",
116                                           false);
117
118         credentials = dcesrv_call_credentials(dce_call);
119
120         if (user && pass) {
121                 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
122                 credentials = cli_credentials_init(priv);
123                 if (!credentials) {
124                         return NT_STATUS_NO_MEMORY;
125                 }
126                 must_free_credentials = true;
127                 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
128                 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
129                 if (domain) {
130                         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
131                 }
132                 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
133         } else if (machine_account) {
134                 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using machine account\n"));
135                 credentials = cli_credentials_init(priv);
136                 if (!credentials) {
137                         return NT_STATUS_NO_MEMORY;
138                 }
139                 must_free_credentials = true;
140                 cli_credentials_set_conf(credentials, dce_call->conn->dce_ctx->lp_ctx);
141                 if (domain) {
142                         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
143                 }
144                 status = cli_credentials_set_machine_account(credentials, dce_call->conn->dce_ctx->lp_ctx);
145                 if (!NT_STATUS_IS_OK(status)) {
146                         return status;
147                 }
148         } else if (credentials != NULL) {
149                 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using delegated credentials\n"));
150         } else if (allow_anonymous) {
151                 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using anonymous\n"));
152                 credentials = cli_credentials_init_anon(priv);
153                 if (!credentials) {
154                         return NT_STATUS_NO_MEMORY;
155                 }
156                 must_free_credentials = true;
157         } else {
158                 DEBUG(1,("dcerpc_remote: RPC Proxy: You must supply binding, user and password or have delegated credentials\n"));
159                 return NT_STATUS_INVALID_PARAMETER;
160         }
161
162         /* parse binding string to the structure */
163         status = dcerpc_parse_binding(priv, binding, &b);
164         if (!NT_STATUS_IS_OK(status)) {
165                 DEBUG(0, ("Failed to parse dcerpc binding '%s'\n", binding));
166                 return status;
167         }
168
169         /* If we already have a remote association group ID, then use that */
170         if (assoc->assoc_group_id != 0) {
171                 status = dcerpc_binding_set_assoc_group_id(b,
172                         assoc->assoc_group_id);
173                 if (!NT_STATUS_IS_OK(status)) {
174                         DEBUG(0, ("dcerpc_binding_set_assoc_group_id() - %s'\n",
175                                   nt_errstr(status)));
176                         return status;
177                 }
178         }
179
180         status = dcerpc_binding_set_abstract_syntax(b, &table->syntax_id);
181         if (!NT_STATUS_IS_OK(status)) {
182                 DEBUG(0, ("dcerpc_binding_set_abstract_syntax() - %s'\n",
183                           nt_errstr(status)));
184                 return status;
185         }
186
187         if (dce_call->conn->state_flags & DCESRV_CALL_STATE_FLAG_MULTIPLEXED) {
188                 status = dcerpc_binding_set_flags(b, DCERPC_CONCURRENT_MULTIPLEX, 0);
189                 if (!NT_STATUS_IS_OK(status)) {
190                         DEBUG(0, ("dcerpc_binding_set_flags(CONC_MPX) - %s'\n",
191                                   nt_errstr(status)));
192                         return status;
193                 }
194         }
195
196         DEBUG(3, ("Using binding %s\n", dcerpc_binding_string(dce_call->context, b)));
197
198         pipe_conn_req = dcerpc_pipe_connect_b_send(priv, b, table,
199                                                    credentials, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx);
200         status = dcerpc_pipe_connect_b_recv(pipe_conn_req, priv, &(priv->c_pipe));
201         
202         if (must_free_credentials) {
203                 talloc_free(credentials);
204         }
205
206         if (!NT_STATUS_IS_OK(status)) {
207                 return status;
208         }
209
210         if (dce_call->conn->state_flags & DCESRV_CALL_STATE_FLAG_MULTIPLEXED) {
211                 flags = dcerpc_binding_get_flags(priv->c_pipe->binding);
212                 if (!(flags & DCERPC_CONCURRENT_MULTIPLEX)) {
213                         DEBUG(1,("dcerpc_remote: RPC Proxy: "
214                                  "Remote server doesn't support MPX\n"));
215                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
216                 }
217         }
218
219         if (assoc->assoc_group_id == 0) {
220                 assoc->assoc_group_id =
221                         dcerpc_binding_get_assoc_group_id(priv->c_pipe->binding);
222                 if (assoc->assoc_group_id == 0) {
223                         return NT_STATUS_INVALID_NETWORK_RESPONSE;
224                 }
225
226                 status = dcesrv_iface_state_store_assoc(dce_call,
227                                                 DCESRV_REMOTE_ASSOC_MAGIC,
228                                                 assoc);
229                 if (!NT_STATUS_IS_OK(status)) {
230                         return status;
231                 }
232         }
233
234         status = dcesrv_iface_state_store_conn(dce_call,
235                                         DCESRV_REMOTE_PRIVATE_MAGIC,
236                                         priv);
237         if (!NT_STATUS_IS_OK(status)) {
238                 return status;
239         }
240
241         *_priv = priv;
242         return NT_STATUS_OK;
243 }
244
245 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
246 {
247         enum ndr_err_code ndr_err;
248         const struct ndr_interface_table *table = (const struct ndr_interface_table *)dce_call->context->iface->private_data;
249         uint16_t opnum = dce_call->pkt.u.request.opnum;
250
251         dce_call->fault_code = 0;
252
253         if (opnum >= table->num_calls) {
254                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
255                 return NT_STATUS_NET_WRITE_FAULT;
256         }
257
258         /*
259          * We don't have support for calls with pipes.
260          */
261         if (table->calls[opnum].in_pipes.num_pipes != 0) {
262                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
263                 return NT_STATUS_NET_WRITE_FAULT;
264         }
265         if (table->calls[opnum].out_pipes.num_pipes != 0) {
266                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
267                 return NT_STATUS_NET_WRITE_FAULT;
268         }
269
270         *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
271         if (!*r) {
272                 return NT_STATUS_NO_MEMORY;
273         }
274
275         /* unravel the NDR for the packet */
276         ndr_err = table->calls[opnum].ndr_pull(pull, NDR_IN, *r);
277         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
278                 dcerpc_log_packet(dce_call->conn->packet_log_dir,
279                                                   table, opnum, NDR_IN,
280                                   &dce_call->pkt.u.request.stub_and_verifier);
281                 dce_call->fault_code = DCERPC_FAULT_NDR;
282                 return NT_STATUS_NET_WRITE_FAULT;
283         }
284
285         return NT_STATUS_OK;
286 }
287
288 static void remote_op_dispatch_done(struct tevent_req *subreq);
289
290 struct dcesrv_remote_call {
291         struct dcesrv_call_state *dce_call;
292         struct dcesrv_remote_private *priv;
293 };
294
295 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
296 {
297         struct dcesrv_remote_call *rcall = NULL;
298         struct dcesrv_remote_private *priv = NULL;
299         uint16_t opnum = dce_call->pkt.u.request.opnum;
300         const struct ndr_interface_table *table = dce_call->context->iface->private_data;
301         const struct ndr_interface_call *call;
302         const char *name;
303         struct tevent_req *subreq;
304         NTSTATUS status;
305
306         name = table->calls[opnum].name;
307         call = &table->calls[opnum];
308
309         status = remote_get_private(dce_call, &priv);
310         if (!NT_STATUS_IS_OK(status)) {
311                 DEBUG(0,("dcesrv_remote: call[%s] %s\n", name, nt_errstr(status)));
312                 return status;
313         }
314
315         rcall = talloc_zero(dce_call, struct dcesrv_remote_call);
316         if (rcall == NULL) {
317                 return NT_STATUS_NO_MEMORY;
318         }
319         rcall->dce_call = dce_call;
320         rcall->priv = priv;
321
322         if (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
323                 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);            
324         }
325
326         priv->c_pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
327
328         /* we didn't use the return code of this function as we only check the last_fault_code */
329         subreq = dcerpc_binding_handle_call_send(rcall, dce_call->event_ctx,
330                                                  priv->c_pipe->binding_handle,
331                                                  NULL, table,
332                                                  opnum, mem_ctx, r);
333         if (subreq == NULL) {
334                 DEBUG(0,("dcesrv_remote: call[%s] dcerpc_binding_handle_call_send() failed!\n", name));
335                 return NT_STATUS_NO_MEMORY;
336         }
337         tevent_req_set_callback(subreq, remote_op_dispatch_done, rcall);
338
339         dce_call->state_flags |= DCESRV_CALL_STATE_FLAG_ASYNC;
340         return NT_STATUS_OK;
341 }
342
343 static void remote_op_dispatch_done(struct tevent_req *subreq)
344 {
345         struct dcesrv_remote_call *rcall =
346                 tevent_req_callback_data(subreq,
347                 struct dcesrv_remote_call);
348         struct dcesrv_call_state *dce_call = rcall->dce_call;
349         struct dcesrv_remote_private *priv = rcall->priv;
350         uint16_t opnum = dce_call->pkt.u.request.opnum;
351         const struct ndr_interface_table *table = dce_call->context->iface->private_data;
352         const struct ndr_interface_call *call;
353         const char *name;
354         NTSTATUS status;
355
356         name = table->calls[opnum].name;
357         call = &table->calls[opnum];
358
359         /* we didn't use the return code of this function as we only check the last_fault_code */
360         status = dcerpc_binding_handle_call_recv(subreq);
361         TALLOC_FREE(subreq);
362
363         dce_call->fault_code = priv->c_pipe->last_fault_code;
364         if (dce_call->fault_code != 0) {
365                 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",
366                         name, dcerpc_errstr(dce_call, dce_call->fault_code)));
367                 goto reply;
368         }
369
370         if (NT_STATUS_IS_OK(status) &&
371             (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
372                 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, dce_call->r);
373         }
374
375 reply:
376         status = dcesrv_reply(dce_call);
377         if (!NT_STATUS_IS_OK(status)) {
378                 DEBUG(0,("dcesrv_remote: call[%s]: dcesrv_reply() failed - %s\n",
379                         name, nt_errstr(status)));
380         }
381 }
382
383 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
384 {
385         enum ndr_err_code ndr_err;
386         const struct ndr_interface_table *table = dce_call->context->iface->private_data;
387         uint16_t opnum = dce_call->pkt.u.request.opnum;
388
389         /* unravel the NDR for the packet */
390         ndr_err = table->calls[opnum].ndr_push(push, NDR_OUT, r);
391         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
392                 dce_call->fault_code = DCERPC_FAULT_NDR;
393                 return NT_STATUS_NET_WRITE_FAULT;
394         }
395
396         return NT_STATUS_OK;
397 }
398
399 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
400 {
401         unsigned int i;
402         const struct ndr_interface_table *table = iface->private_data;
403
404         for (i=0;i<table->endpoints->count;i++) {
405                 NTSTATUS ret;
406                 const char *name = table->endpoints->names[i];
407
408                 ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
409                 if (!NT_STATUS_IS_OK(ret)) {
410                         DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
411                         return ret;
412                 }
413         }
414
415         return NT_STATUS_OK;
416 }
417
418 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
419 {
420         unsigned int i;
421         char **ifaces = str_list_make(dce_ctx, lpcfg_parm_string(dce_ctx->lp_ctx, NULL, "dcerpc_remote", "interfaces"),NULL);
422
423         if (!ifaces) {
424                 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
425                 return NT_STATUS_OK;
426         }
427
428         for (i=0;ifaces[i];i++) {
429                 NTSTATUS ret;
430                 struct dcesrv_interface iface;
431                 
432                 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
433                         DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
434                         talloc_free(ifaces);
435                         return NT_STATUS_UNSUCCESSFUL;
436                 }
437
438                 ret = remote_register_one_iface(dce_ctx, &iface);
439                 if (!NT_STATUS_IS_OK(ret)) {
440                         DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
441                         talloc_free(ifaces);
442                         return ret;
443                 }
444         }
445
446         talloc_free(ifaces);
447         return NT_STATUS_OK;
448 }
449
450 static bool remote_fill_interface(struct dcesrv_interface *iface, const struct ndr_interface_table *if_tabl)
451 {
452         iface->name = if_tabl->name;
453         iface->syntax_id = if_tabl->syntax_id;
454         
455         iface->bind = remote_op_bind;
456         iface->unbind = NULL;
457
458         iface->ndr_pull = remote_op_ndr_pull;
459         iface->dispatch = remote_op_dispatch;
460         iface->reply = remote_op_reply;
461         iface->ndr_push = remote_op_ndr_push;
462
463         iface->private_data = if_tabl;
464         iface->flags = 0;
465
466         return true;
467 }
468
469 static bool remote_op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
470 {
471         const struct ndr_interface_list *l;
472
473         for (l=ndr_table_list();l;l=l->next) {
474                 if (l->table->syntax_id.if_version == if_version &&
475                         GUID_equal(&l->table->syntax_id.uuid, uuid)==0) {
476                         return remote_fill_interface(iface, l->table);
477                 }
478         }
479
480         return false;   
481 }
482
483 static bool remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
484 {
485         const struct ndr_interface_table *tbl = ndr_table_by_name(name);
486
487         if (tbl)
488                 return remote_fill_interface(iface, tbl);
489
490         return false;   
491 }
492
493 NTSTATUS dcerpc_server_remote_init(TALLOC_CTX *ctx)
494 {
495         NTSTATUS ret;
496         static const struct dcesrv_endpoint_server ep_server = {
497                 /* fill in our name */
498                 .name = "remote",
499
500                 /* fill in all the operations */
501                 .init_server = remote_op_init_server,
502
503                 .interface_by_uuid = remote_op_interface_by_uuid,
504                 .interface_by_name = remote_op_interface_by_name
505         };
506
507         /* register ourselves with the DCERPC subsystem. */
508         ret = dcerpc_register_ep_server(&ep_server);
509         if (!NT_STATUS_IS_OK(ret)) {
510                 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
511                 return ret;
512         }
513
514         /* We need the full DCE/RPC interface table */
515         ndr_table_init();
516
517         return ret;
518 }