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