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