b0204edc9e39b24c81c84137f4b9d79a77be8d55
[metze/samba/wip.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         struct dcerpc_binding_handle_call_params params;
295 };
296
297 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
298 {
299         struct dcesrv_remote_call *rcall = NULL;
300         struct dcesrv_remote_private *priv = NULL;
301         uint16_t opnum = dce_call->pkt.u.request.opnum;
302         const struct ndr_interface_table *table = dce_call->context->iface->private_data;
303         const struct ndr_interface_call *call;
304         const char *name;
305         struct tevent_req *subreq;
306         NTSTATUS status;
307
308         name = table->calls[opnum].name;
309         call = &table->calls[opnum];
310
311         status = remote_get_private(dce_call, &priv);
312         if (!NT_STATUS_IS_OK(status)) {
313                 DEBUG(0,("dcesrv_remote: call[%s] %s\n", name, nt_errstr(status)));
314                 return status;
315         }
316
317         rcall = talloc_zero(dce_call, struct dcesrv_remote_call);
318         if (rcall == NULL) {
319                 return NT_STATUS_NO_MEMORY;
320         }
321         rcall->dce_call = dce_call;
322         rcall->priv = priv;
323         rcall->params.r_mem = mem_ctx;
324         rcall->params.r_ptr = r;
325         rcall->params.in.num_pipes = 0;
326         rcall->params.in.pipes = NULL;
327         rcall->params.out.num_pipes = 0;
328         rcall->params.out.pipes = NULL;
329
330         if (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
331                 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);            
332         }
333
334         priv->c_pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
335
336         /* we didn't use the return code of this function as we only check the last_fault_code */
337         subreq = dcerpc_binding_handle_call_send(rcall, dce_call->event_ctx,
338                                                  priv->c_pipe->binding_handle,
339                                                  NULL, table,
340                                                  opnum, &rcall->params);
341         if (subreq == NULL) {
342                 DEBUG(0,("dcesrv_remote: call[%s] dcerpc_binding_handle_call_send() failed!\n", name));
343                 return NT_STATUS_NO_MEMORY;
344         }
345         tevent_req_set_callback(subreq, remote_op_dispatch_done, rcall);
346
347         dce_call->state_flags |= DCESRV_CALL_STATE_FLAG_ASYNC;
348         return NT_STATUS_OK;
349 }
350
351 static void remote_op_dispatch_done(struct tevent_req *subreq)
352 {
353         struct dcesrv_remote_call *rcall =
354                 tevent_req_callback_data(subreq,
355                 struct dcesrv_remote_call);
356         struct dcesrv_call_state *dce_call = rcall->dce_call;
357         struct dcesrv_remote_private *priv = rcall->priv;
358         uint16_t opnum = dce_call->pkt.u.request.opnum;
359         const struct ndr_interface_table *table = dce_call->context->iface->private_data;
360         const struct ndr_interface_call *call;
361         const char *name;
362         NTSTATUS status;
363
364         name = table->calls[opnum].name;
365         call = &table->calls[opnum];
366
367         /* we didn't use the return code of this function as we only check the last_fault_code */
368         status = dcerpc_binding_handle_call_recv(subreq);
369         TALLOC_FREE(subreq);
370
371         dce_call->fault_code = priv->c_pipe->last_fault_code;
372         if (dce_call->fault_code != 0) {
373                 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",
374                         name, dcerpc_errstr(dce_call, dce_call->fault_code)));
375                 goto reply;
376         }
377
378         if (NT_STATUS_IS_OK(status) &&
379             (priv->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
380                 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, dce_call->r);
381         }
382
383 reply:
384         status = dcesrv_reply(dce_call);
385         if (!NT_STATUS_IS_OK(status)) {
386                 DEBUG(0,("dcesrv_remote: call[%s]: dcesrv_reply() failed - %s\n",
387                         name, nt_errstr(status)));
388         }
389 }
390
391 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
392 {
393         enum ndr_err_code ndr_err;
394         const struct ndr_interface_table *table = dce_call->context->iface->private_data;
395         uint16_t opnum = dce_call->pkt.u.request.opnum;
396
397         /* unravel the NDR for the packet */
398         ndr_err = table->calls[opnum].ndr_push(push, NDR_OUT, r);
399         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
400                 dce_call->fault_code = DCERPC_FAULT_NDR;
401                 return NT_STATUS_NET_WRITE_FAULT;
402         }
403
404         return NT_STATUS_OK;
405 }
406
407 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
408 {
409         unsigned int i;
410         const struct ndr_interface_table *table = iface->private_data;
411
412         for (i=0;i<table->endpoints->count;i++) {
413                 NTSTATUS ret;
414                 const char *name = table->endpoints->names[i];
415
416                 ret = dcesrv_interface_register(dce_ctx, name, NULL, iface, NULL);
417                 if (!NT_STATUS_IS_OK(ret)) {
418                         DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
419                         return ret;
420                 }
421         }
422
423         return NT_STATUS_OK;
424 }
425
426 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
427 {
428         unsigned int i;
429         char **ifaces = str_list_make(dce_ctx, lpcfg_parm_string(dce_ctx->lp_ctx, NULL, "dcerpc_remote", "interfaces"),NULL);
430
431         if (!ifaces) {
432                 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
433                 return NT_STATUS_OK;
434         }
435
436         for (i=0;ifaces[i];i++) {
437                 NTSTATUS ret;
438                 struct dcesrv_interface iface;
439                 
440                 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
441                         DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
442                         talloc_free(ifaces);
443                         return NT_STATUS_UNSUCCESSFUL;
444                 }
445
446                 ret = remote_register_one_iface(dce_ctx, &iface);
447                 if (!NT_STATUS_IS_OK(ret)) {
448                         DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
449                         talloc_free(ifaces);
450                         return ret;
451                 }
452         }
453
454         talloc_free(ifaces);
455         return NT_STATUS_OK;
456 }
457
458 static bool remote_fill_interface(struct dcesrv_interface *iface, const struct ndr_interface_table *if_tabl)
459 {
460         iface->name = if_tabl->name;
461         iface->syntax_id = if_tabl->syntax_id;
462         
463         iface->bind = remote_op_bind;
464         iface->unbind = NULL;
465
466         iface->ndr_pull = remote_op_ndr_pull;
467         iface->dispatch = remote_op_dispatch;
468         iface->reply = remote_op_reply;
469         iface->ndr_push = remote_op_ndr_push;
470
471         iface->private_data = if_tabl;
472         iface->flags = 0;
473
474         return true;
475 }
476
477 static bool remote_op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
478 {
479         const struct ndr_interface_list *l;
480
481         for (l=ndr_table_list();l;l=l->next) {
482                 if (l->table->syntax_id.if_version == if_version &&
483                         GUID_equal(&l->table->syntax_id.uuid, uuid)==0) {
484                         return remote_fill_interface(iface, l->table);
485                 }
486         }
487
488         return false;   
489 }
490
491 static bool remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
492 {
493         const struct ndr_interface_table *tbl = ndr_table_by_name(name);
494
495         if (tbl)
496                 return remote_fill_interface(iface, tbl);
497
498         return false;   
499 }
500
501 NTSTATUS dcerpc_server_remote_init(TALLOC_CTX *ctx)
502 {
503         NTSTATUS ret;
504         static const struct dcesrv_endpoint_server ep_server = {
505                 /* fill in our name */
506                 .name = "remote",
507
508                 /* fill in all the operations */
509                 .init_server = remote_op_init_server,
510
511                 .interface_by_uuid = remote_op_interface_by_uuid,
512                 .interface_by_name = remote_op_interface_by_name
513         };
514
515         /* register ourselves with the DCERPC subsystem. */
516         ret = dcerpc_register_ep_server(&ep_server);
517         if (!NT_STATUS_IS_OK(ret)) {
518                 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
519                 return ret;
520         }
521
522         /* We need the full DCE/RPC interface table */
523         ndr_table_init();
524
525         return ret;
526 }