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