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