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