6ddffa1d6cae0d6c70b1f0b2bfc5008c636deb01
[jelmer/samba4-debian.git] / source / 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    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "rpc_server/dcerpc_server.h"
23 #include "auth/auth.h"
24 #include "auth/credentials/credentials.h"
25 #include "librpc/ndr/ndr_table.h"
26
27
28 struct dcesrv_remote_private {
29         struct dcerpc_pipe *c_pipe;
30 };
31
32 static NTSTATUS remote_op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
33 {
34         return NT_STATUS_OK;
35 }
36
37 static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
38 {
39         NTSTATUS status;
40         const struct ndr_interface_table *table;
41         struct dcesrv_remote_private *private;
42         const char *binding = lp_parm_string(-1, "dcerpc_remote", "binding");
43         const char *user, *pass, *domain;
44         struct cli_credentials *credentials;
45         BOOL machine_account;
46
47         machine_account = lp_parm_bool(-1, "dcerpc_remote", "use_machine_account", False);
48
49         private = talloc(dce_call->conn, struct dcesrv_remote_private);
50         if (!private) {
51                 return NT_STATUS_NO_MEMORY;     
52         }
53         
54         private->c_pipe = NULL;
55         dce_call->context->private = private;
56
57         if (!binding) {
58                 DEBUG(0,("You must specify a DCE/RPC binding string\n"));
59                 return NT_STATUS_INVALID_PARAMETER;
60         }
61
62         user = lp_parm_string(-1, "dcerpc_remote", "user");
63         pass = lp_parm_string(-1, "dcerpc_remote", "password");
64         domain = lp_parm_string(-1, "dceprc_remote", "domain");
65
66         table = ndr_table_by_uuid(&iface->syntax_id.uuid); /* FIXME: What about if_version ? */
67         if (!table) {
68                 dce_call->fault_code = DCERPC_FAULT_UNK_IF;
69                 return NT_STATUS_NET_WRITE_FAULT;
70         }
71
72         if (user && pass) {
73                 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
74                 credentials = cli_credentials_init(private);
75                 if (!credentials) {
76                         return NT_STATUS_NO_MEMORY;
77                 }
78                 cli_credentials_set_conf(credentials);
79                 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
80                 if (domain) {
81                         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
82                 }
83                 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
84         } else if (machine_account) {
85                 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using machine account\n"));
86                 credentials = cli_credentials_init(private);
87                 cli_credentials_set_conf(credentials);
88                 if (domain) {
89                         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
90                 }
91                 status = cli_credentials_set_machine_account(credentials);
92                 if (!NT_STATUS_IS_OK(status)) {
93                         return status;
94                 }
95         } else if (dce_call->conn->auth_state.session_info->credentials) {
96                 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using delegated credentials\n"));
97                 credentials = dce_call->conn->auth_state.session_info->credentials;
98         } else {
99                 DEBUG(1,("dcerpc_remote: RPC Proxy: You must supply binding, user and password or have delegated credentials\n"));
100                 return NT_STATUS_INVALID_PARAMETER;
101         }
102
103         status = dcerpc_pipe_connect(private, 
104                                      &(private->c_pipe), binding, table,
105                                      credentials, dce_call->event_ctx);
106
107         talloc_free(credentials);
108         if (!NT_STATUS_IS_OK(status)) {
109                 return status;
110         }
111
112         return NT_STATUS_OK;    
113 }
114
115 static void remote_op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
116 {
117         struct dcesrv_remote_private *private = context->private;
118
119         talloc_free(private->c_pipe);
120
121         return; 
122 }
123
124 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
125 {
126         NTSTATUS status;
127         const struct ndr_interface_table *table = dce_call->context->iface->private;
128         uint16_t opnum = dce_call->pkt.u.request.opnum;
129
130         dce_call->fault_code = 0;
131
132         if (opnum >= table->num_calls) {
133                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
134                 return NT_STATUS_NET_WRITE_FAULT;
135         }
136
137         *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
138         if (!*r) {
139                 return NT_STATUS_NO_MEMORY;
140         }
141
142         /* unravel the NDR for the packet */
143         status = table->calls[opnum].ndr_pull(pull, NDR_IN, *r);
144         if (!NT_STATUS_IS_OK(status)) {
145                 dcerpc_log_packet(table, opnum, NDR_IN,
146                                   &dce_call->pkt.u.request.stub_and_verifier);
147                 dce_call->fault_code = DCERPC_FAULT_NDR;
148                 return NT_STATUS_NET_WRITE_FAULT;
149         }
150
151         return NT_STATUS_OK;
152 }
153
154 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
155 {
156         struct dcesrv_remote_private *private = dce_call->context->private;
157         uint16_t opnum = dce_call->pkt.u.request.opnum;
158         const struct ndr_interface_table *table = dce_call->context->iface->private;
159         const struct ndr_interface_call *call;
160         const char *name;
161
162         name = table->calls[opnum].name;
163         call = &table->calls[opnum];
164
165         if (private->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
166                 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);            
167         }
168
169         private->c_pipe->conn->flags |= DCERPC_NDR_REF_ALLOC;
170
171         /* we didn't use the return code of this function as we only check the last_fault_code */
172         dcerpc_ndr_request(private->c_pipe, NULL, table, opnum, mem_ctx,r);
173
174         dce_call->fault_code = private->c_pipe->last_fault_code;
175         if (dce_call->fault_code != 0) {
176                 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",name, dcerpc_errstr(mem_ctx, dce_call->fault_code)));
177                 return NT_STATUS_NET_WRITE_FAULT;
178         }
179
180         if ((dce_call->fault_code == 0) && 
181             (private->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
182                 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, r);            
183         }
184
185         return NT_STATUS_OK;
186 }
187
188 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
189 {
190         NTSTATUS status;
191         const struct ndr_interface_table *table = dce_call->context->iface->private;
192         uint16_t opnum = dce_call->pkt.u.request.opnum;
193
194         /* unravel the NDR for the packet */
195         status = table->calls[opnum].ndr_push(push, NDR_OUT, r);
196         if (!NT_STATUS_IS_OK(status)) {
197                 dce_call->fault_code = DCERPC_FAULT_NDR;
198                 return NT_STATUS_NET_WRITE_FAULT;
199         }
200
201         return NT_STATUS_OK;
202 }
203
204 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
205 {
206         int i;
207         const struct ndr_interface_table *table = iface->private;
208
209         for (i=0;i<table->endpoints->count;i++) {
210                 NTSTATUS ret;
211                 const char *name = table->endpoints->names[i];
212
213                 ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
214                 if (!NT_STATUS_IS_OK(ret)) {
215                         DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
216                         return ret;
217                 }
218         }
219
220         return NT_STATUS_OK;
221 }
222
223 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
224 {
225         int i;
226         const char **ifaces = str_list_make(dce_ctx, lp_parm_string(-1,"dcerpc_remote","interfaces"),NULL);
227
228         if (!ifaces) {
229                 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
230                 return NT_STATUS_OK;
231         }
232
233         for (i=0;ifaces[i];i++) {
234                 NTSTATUS ret;
235                 struct dcesrv_interface iface;
236                 
237                 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
238                         DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
239                         talloc_free(ifaces);
240                         return NT_STATUS_UNSUCCESSFUL;
241                 }
242
243                 ret = remote_register_one_iface(dce_ctx, &iface);
244                 if (!NT_STATUS_IS_OK(ret)) {
245                         DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
246                         talloc_free(ifaces);
247                         return ret;
248                 }
249         }
250
251         talloc_free(ifaces);
252         return NT_STATUS_OK;
253 }
254
255 static BOOL remote_fill_interface(struct dcesrv_interface *iface, const struct ndr_interface_table *if_tabl)
256 {
257         iface->name = if_tabl->name;
258         iface->syntax_id = if_tabl->syntax_id;
259         
260         iface->bind = remote_op_bind;
261         iface->unbind = remote_op_unbind;
262
263         iface->ndr_pull = remote_op_ndr_pull;
264         iface->dispatch = remote_op_dispatch;
265         iface->reply = remote_op_reply;
266         iface->ndr_push = remote_op_ndr_push;
267
268         iface->private = if_tabl;
269
270         return True;
271 }
272
273 static BOOL remote_op_interface_by_uuid(struct dcesrv_interface *iface, const struct GUID *uuid, uint32_t if_version)
274 {
275         const struct ndr_interface_list *l;
276
277         for (l=ndr_table_list();l;l=l->next) {
278                 if (l->table->syntax_id.if_version == if_version &&
279                         GUID_equal(&l->table->syntax_id.uuid, uuid)==0) {
280                         return remote_fill_interface(iface, l->table);
281                 }
282         }
283
284         return False;   
285 }
286
287 static BOOL remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
288 {
289         const struct ndr_interface_table *tbl = ndr_table_by_name(name);
290
291         if (tbl)
292                 return remote_fill_interface(iface, tbl);
293
294         return False;   
295 }
296
297 NTSTATUS dcerpc_server_remote_init(void)
298 {
299         NTSTATUS ret;
300         struct dcesrv_endpoint_server ep_server;
301
302         ZERO_STRUCT(ep_server);
303
304         /* fill in our name */
305         ep_server.name = "remote";
306
307         /* fill in all the operations */
308         ep_server.init_server = remote_op_init_server;
309
310         ep_server.interface_by_uuid = remote_op_interface_by_uuid;
311         ep_server.interface_by_name = remote_op_interface_by_name;
312
313         /* register ourselves with the DCERPC subsystem. */
314         ret = dcerpc_register_ep_server(&ep_server);
315         if (!NT_STATUS_IS_OK(ret)) {
316                 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
317                 return ret;
318         }
319
320         /* We need the full DCE/RPC interface table */
321         ndr_table_init();
322
323         return ret;
324 }