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