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