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