9ba2419859b4dc6a46f133678cf84db098075730
[kamenim/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    
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "rpc_server/dcerpc_server.h"
24 #include "auth/auth.h"
25
26
27 struct dcesrv_remote_private {
28         struct dcerpc_pipe *c_pipe;
29 };
30
31 static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
32 {
33         NTSTATUS status;
34         struct dcesrv_remote_private *private;
35         const char *binding = lp_parm_string(-1, "dcerpc_remote", "binding");
36         const char *user, *pass, *domain;
37         struct cli_credentials *credentials;
38         BOOL machine_account;
39
40         machine_account = lp_parm_bool(-1, "dcerpc_remote", "use_machine_account", False);
41
42         private = talloc(dce_call->conn, struct dcesrv_remote_private);
43         if (!private) {
44                 return NT_STATUS_NO_MEMORY;     
45         }
46         
47         private->c_pipe = NULL;
48         dce_call->context->private = private;
49
50         if (!binding) {
51                 DEBUG(0,("You must specify a ncacn binding string\n"));
52                 return NT_STATUS_INVALID_PARAMETER;
53         }
54
55         user = lp_parm_string(-1, "dcerpc_remote", "user");
56         pass = lp_parm_string(-1, "dcerpc_remote", "password");
57         domain = lp_parm_string(-1, "dceprc_remote", "domain");
58
59         if (user && pass) {
60                 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using specified account\n"));
61                 credentials = cli_credentials_init(private);
62                 if (!credentials) {
63                         return NT_STATUS_NO_MEMORY;
64                 }
65                 cli_credentials_set_conf(credentials);
66                 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
67                 if (domain) {
68                         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
69                 }
70                 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
71         } else if (machine_account) {
72                 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using machine account\n"));
73                 credentials = cli_credentials_init(private);
74                 cli_credentials_set_conf(credentials);
75                 if (domain) {
76                         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
77                 }
78                 status = cli_credentials_set_machine_account(credentials);
79                 if (!NT_STATUS_IS_OK(status)) {
80                         return status;
81                 }
82         } else if (dce_call->conn->auth_state.session_info->credentials) {
83                 DEBUG(5, ("dcerpc_remote: RPC Proxy: Using delegated credentials\n"));
84                 credentials = dce_call->conn->auth_state.session_info->credentials;
85         } else {
86                 DEBUG(1,("dcerpc_remote: RPC Proxy: You must supply binding, user and password or have delegated credentials\n"));
87                 return NT_STATUS_INVALID_PARAMETER;
88         }
89
90         status = dcerpc_pipe_connect(private, 
91                                      &(private->c_pipe), binding, 
92                                      iface->uuid, iface->if_version, 
93                                      credentials, dce_call->event_ctx);
94
95         talloc_free(credentials);
96         if (!NT_STATUS_IS_OK(status)) {
97                 return status;
98         }
99
100         return NT_STATUS_OK;    
101 }
102
103 static void remote_op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
104 {
105         struct dcesrv_remote_private *private = context->private;
106
107         talloc_free(private->c_pipe);
108
109         return; 
110 }
111
112 static NTSTATUS remote_op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
113 {
114         NTSTATUS status;
115         const struct dcerpc_interface_table *table = dce_call->context->iface->private;
116         uint16_t opnum = dce_call->pkt.u.request.opnum;
117
118         dce_call->fault_code = 0;
119
120         if (opnum >= table->num_calls) {
121                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
122                 return NT_STATUS_NET_WRITE_FAULT;
123         }
124
125         *r = talloc_size(mem_ctx, table->calls[opnum].struct_size);
126         if (!*r) {
127                 return NT_STATUS_NO_MEMORY;
128         }
129
130         /* unravel the NDR for the packet */
131         status = table->calls[opnum].ndr_pull(pull, NDR_IN, *r);
132         if (!NT_STATUS_IS_OK(status)) {
133                 dcerpc_log_packet(table, opnum, NDR_IN,
134                                   &dce_call->pkt.u.request.stub_and_verifier);
135                 dce_call->fault_code = DCERPC_FAULT_NDR;
136                 return NT_STATUS_NET_WRITE_FAULT;
137         }
138
139         return NT_STATUS_OK;
140 }
141
142 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
143 {
144         struct dcesrv_remote_private *private = dce_call->context->private;
145         uint16_t opnum = dce_call->pkt.u.request.opnum;
146         const struct dcerpc_interface_table *table = dce_call->context->iface->private;
147         const struct dcerpc_interface_call *call;
148         const char *name;
149
150         name = table->calls[opnum].name;
151         call = &table->calls[opnum];
152
153         if (private->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_IN) {
154                 ndr_print_function_debug(call->ndr_print, name, NDR_IN | NDR_SET_VALUES, r);            
155         }
156
157         /* we didn't use the return code of this function as we only check the last_fault_code */
158         dcerpc_ndr_request(private->c_pipe, NULL, table, opnum, mem_ctx,r);
159
160         dce_call->fault_code = private->c_pipe->last_fault_code;
161         if (dce_call->fault_code != 0) {
162                 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",name, dcerpc_errstr(mem_ctx, dce_call->fault_code)));
163                 return NT_STATUS_NET_WRITE_FAULT;
164         }
165
166         if ((dce_call->fault_code == 0) && 
167             (private->c_pipe->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
168                 ndr_print_function_debug(call->ndr_print, name, NDR_OUT, r);            
169         }
170
171         return NT_STATUS_OK;
172 }
173
174 static NTSTATUS remote_op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
175 {
176         NTSTATUS status;
177         const struct dcerpc_interface_table *table = dce_call->context->iface->private;
178         uint16_t opnum = dce_call->pkt.u.request.opnum;
179
180         /* unravel the NDR for the packet */
181         status = table->calls[opnum].ndr_push(push, NDR_OUT, r);
182         if (!NT_STATUS_IS_OK(status)) {
183                 dce_call->fault_code = DCERPC_FAULT_NDR;
184                 return NT_STATUS_NET_WRITE_FAULT;
185         }
186
187         return NT_STATUS_OK;
188 }
189
190 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
191 {
192         int i;
193         const struct dcerpc_interface_table *table = iface->private;
194
195         for (i=0;i<table->endpoints->count;i++) {
196                 NTSTATUS ret;
197                 const char *name = table->endpoints->names[i];
198
199                 ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
200                 if (!NT_STATUS_IS_OK(ret)) {
201                         DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
202                         return ret;
203                 }
204         }
205
206         return NT_STATUS_OK;
207 }
208
209 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
210 {
211         int i;
212         const char **ifaces = str_list_make(dce_ctx, lp_parm_string(-1,"dcerpc_remote","interfaces"),NULL);
213
214         if (!ifaces) {
215                 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
216                 return NT_STATUS_OK;
217         }
218
219         for (i=0;ifaces[i];i++) {
220                 NTSTATUS ret;
221                 struct dcesrv_interface iface;
222                 
223                 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
224                         DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
225                         talloc_free(ifaces);
226                         return NT_STATUS_UNSUCCESSFUL;
227                 }
228
229                 ret = remote_register_one_iface(dce_ctx, &iface);
230                 if (!NT_STATUS_IS_OK(ret)) {
231                         DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
232                         talloc_free(ifaces);
233                         return ret;
234                 }
235         }
236
237         talloc_free(ifaces);
238         return NT_STATUS_OK;
239 }
240
241 static BOOL remote_fill_interface(struct dcesrv_interface *iface, const struct dcerpc_interface_table *if_tabl)
242 {
243         iface->name = if_tabl->name;
244         iface->uuid = if_tabl->uuid;
245         iface->if_version = if_tabl->if_version;
246         
247         iface->bind = remote_op_bind;
248         iface->unbind = remote_op_unbind;
249
250         iface->ndr_pull = remote_op_ndr_pull;
251         iface->dispatch = remote_op_dispatch;
252         iface->ndr_push = remote_op_ndr_push;
253
254         iface->private = if_tabl;
255
256         return True;
257 }
258
259 static BOOL remote_op_interface_by_uuid(struct dcesrv_interface *iface, const char *uuid, uint32_t if_version)
260 {
261         const struct dcerpc_interface_list *l;
262
263         for (l=librpc_dcerpc_pipes();l;l=l->next) {
264                 if (l->table->if_version == if_version &&
265                         strcmp(l->table->uuid, uuid)==0) {
266                         return remote_fill_interface(iface, l->table);
267                 }
268         }
269
270         return False;   
271 }
272
273 static BOOL remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
274 {
275         const struct dcerpc_interface_list *l;
276
277         for (l=librpc_dcerpc_pipes();l;l=l->next) {
278                 if (strcmp(l->table->name, name)==0) {
279                         return remote_fill_interface(iface, l->table);
280                 }
281         }
282
283         return False;   
284 }
285
286 NTSTATUS dcerpc_server_remote_init(void)
287 {
288         NTSTATUS ret;
289         struct dcesrv_endpoint_server ep_server;
290
291         ZERO_STRUCT(ep_server);
292
293         /* fill in our name */
294         ep_server.name = "remote";
295
296         /* fill in all the operations */
297         ep_server.init_server = remote_op_init_server;
298
299         ep_server.interface_by_uuid = remote_op_interface_by_uuid;
300         ep_server.interface_by_name = remote_op_interface_by_name;
301
302         /* register ourselves with the DCERPC subsystem. */
303         ret = dcerpc_register_ep_server(&ep_server);
304         if (!NT_STATUS_IS_OK(ret)) {
305                 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
306                 return ret;
307         }
308
309         return ret;
310 }