s4:rpc_server: only pass context to op_bind() hooks
[samba.git] / source4 / rpc_server / epmapper / rpc_epmapper.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    endpoint server for the epmapper pipe
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Jelmer Vernooij 2004
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_epmapper.h"
25 #include "rpc_server/dcerpc_server.h"
26
27 #define DCESRV_INTERFACE_EPMAPPER_BIND(context, iface) \
28        dcesrv_interface_epmapper_bind(context, iface)
29 static NTSTATUS dcesrv_interface_epmapper_bind(struct dcesrv_connection_context *context,
30                                              const struct dcesrv_interface *iface)
31 {
32         return dcesrv_interface_bind_allow_connect(context, iface);
33 }
34
35 typedef uint32_t error_status_t;
36
37 /* handle types for this module */
38 enum handle_types {HTYPE_LOOKUP};
39
40 /* a endpoint combined with an interface description */
41 struct dcesrv_ep_iface {
42         const char *name;
43         struct epm_tower ep;
44 };
45
46 /*
47   build a list of all interfaces handled by all endpoint servers
48 */
49 static uint32_t build_ep_list(TALLOC_CTX *mem_ctx,
50                               struct dcesrv_endpoint *endpoint_list,
51                               struct dcesrv_ep_iface **eps)
52 {
53         struct dcesrv_endpoint *d;
54         uint32_t total = 0;
55         NTSTATUS status;
56
57         *eps = NULL;
58
59         for (d=endpoint_list; d; d=d->next) {
60                 struct dcesrv_if_list *iface;
61
62                 for (iface=d->interface_list;iface;iface=iface->next) {
63                         struct dcerpc_binding *description;
64
65                         (*eps) = talloc_realloc(mem_ctx, 
66                                                   *eps, 
67                                                   struct dcesrv_ep_iface,
68                                                   total + 1);
69                         if (!*eps) {
70                                 return 0;
71                         }
72                         (*eps)[total].name = iface->iface.name;
73
74                         description = dcerpc_binding_dup(*eps, d->ep_description);
75                         if (description == NULL) {
76                                 return 0;
77                         }
78
79                         status = dcerpc_binding_set_abstract_syntax(description,
80                                                         &iface->iface.syntax_id);
81                         if (!NT_STATUS_IS_OK(status)) {
82                                 return 0;
83                         }
84
85                         status = dcerpc_binding_build_tower(*eps, description, &(*eps)[total].ep);
86                         TALLOC_FREE(description);
87                         if (!NT_STATUS_IS_OK(status)) {
88                                 DEBUG(1, ("Unable to build tower for %s - %s\n",
89                                           iface->iface.name, nt_errstr(status)));
90                                 continue;
91                         }
92                         total++;
93                 }
94         }
95
96         return total;
97 }
98
99
100 static error_status_t dcesrv_epm_Insert(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct epm_Insert *r)
101 {
102         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
103 }
104
105 static error_status_t dcesrv_epm_Delete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
106                                  struct epm_Delete *r)
107 {
108         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
109 }
110
111
112 /*
113   implement epm_Lookup. This call is used to enumerate the interfaces
114   available on a rpc server
115 */
116 static error_status_t dcesrv_epm_Lookup(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
117                                  struct epm_Lookup *r)
118 {
119         struct dcesrv_handle *h;
120         struct rpc_eps {
121                 uint32_t count;
122                 struct dcesrv_ep_iface *e;
123         } *eps;
124         uint32_t num_ents;
125         unsigned int i;
126
127         DCESRV_PULL_HANDLE_FAULT(h, r->in.entry_handle, HTYPE_LOOKUP);
128
129         eps = h->data;
130
131         if (!eps) {
132                 /* this is the first call - fill the list. Subsequent calls 
133                    will feed from this list, stored in the handle */
134                 eps = talloc(h, struct rpc_eps);
135                 if (!eps) {
136                         return EPMAPPER_STATUS_NO_MEMORY;
137                 }
138                 h->data = eps;
139
140                 eps->count = build_ep_list(h, dce_call->conn->dce_ctx->endpoint_list, &eps->e);
141         }
142
143         /* return the next N elements */
144         num_ents = r->in.max_ents;
145         if (num_ents > eps->count) {
146                 num_ents = eps->count;
147         }
148
149         *r->out.entry_handle = h->wire_handle;
150         r->out.num_ents = talloc(mem_ctx, uint32_t);
151         *r->out.num_ents = num_ents;
152
153         if (num_ents == 0) {
154                 r->out.entries = NULL;
155                 ZERO_STRUCTP(r->out.entry_handle);
156                 talloc_free(h);
157                 return EPMAPPER_STATUS_NO_MORE_ENTRIES;
158         }
159
160         r->out.entries = talloc_array(mem_ctx, struct epm_entry_t, num_ents);
161         if (!r->out.entries) {
162                 return EPMAPPER_STATUS_NO_MEMORY;
163         }
164
165         for (i=0;i<num_ents;i++) {
166                 ZERO_STRUCT(r->out.entries[i].object);
167                 r->out.entries[i].annotation = eps->e[i].name;
168                 r->out.entries[i].tower = talloc(mem_ctx, struct epm_twr_t);
169                 if (!r->out.entries[i].tower) {
170                         return EPMAPPER_STATUS_NO_MEMORY;
171                 }
172                 r->out.entries[i].tower->tower = eps->e[i].ep;
173         }
174
175         eps->count -= num_ents;
176         eps->e += num_ents;
177
178         return EPMAPPER_STATUS_OK;
179 }
180
181
182 /*
183   implement epm_Map. This is used to find the specific endpoint to talk to given
184   a generic protocol tower
185 */
186 static error_status_t dcesrv_epm_Map(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
187                               struct epm_Map *r)
188 {
189         uint32_t count;
190         unsigned int i;
191         struct dcesrv_ep_iface *eps;
192         struct epm_floor *floors;
193         enum dcerpc_transport_t transport;
194         struct ndr_syntax_id ndr_syntax;
195
196         count = build_ep_list(mem_ctx, dce_call->conn->dce_ctx->endpoint_list, &eps);
197
198         ZERO_STRUCT(*r->out.entry_handle);
199         r->out.num_towers = talloc(mem_ctx, uint32_t);
200         if (!r->out.num_towers) {
201                 return EPMAPPER_STATUS_NO_MEMORY;
202         }
203         *r->out.num_towers = 1;
204         r->out.towers = talloc(mem_ctx, struct epm_twr_p_t);
205         if (!r->out.towers) {
206                 return EPMAPPER_STATUS_NO_MEMORY;
207         }
208         r->out.towers->twr = talloc(mem_ctx, struct epm_twr_t);
209         if (!r->out.towers->twr) {
210                 return EPMAPPER_STATUS_NO_MEMORY;
211         }
212         
213         if (!r->in.map_tower || r->in.max_towers == 0 || 
214             r->in.map_tower->tower.num_floors < 3) {
215                 goto failed;
216         }
217
218         floors = r->in.map_tower->tower.floors;
219
220         dcerpc_floor_get_lhs_data(&r->in.map_tower->tower.floors[1], &ndr_syntax);
221
222         if (floors[1].lhs.protocol != EPM_PROTOCOL_UUID ||
223                 !GUID_equal(&ndr_syntax.uuid, &ndr_transfer_syntax_ndr.uuid) ||
224             ndr_syntax.if_version != ndr_transfer_syntax_ndr.if_version) {
225                 goto failed;
226         }
227
228         transport = dcerpc_transport_by_tower(&r->in.map_tower->tower);
229
230         if (transport == -1) {
231                 DEBUG(2, ("Client requested unknown transport with levels: "));
232                 for (i = 2; i < r->in.map_tower->tower.num_floors; i++) {
233                         DEBUG(2, ("%d, ", r->in.map_tower->tower.floors[i].lhs.protocol));
234                 }
235                 DEBUG(2, ("\n"));
236                 goto failed;
237         }
238
239         for (i=0;i<count;i++) {
240                 if (
241                         data_blob_cmp(&r->in.map_tower->tower.floors[0].lhs.lhs_data, 
242                         &eps[i].ep.floors[0].lhs.lhs_data) != 0 
243                         || transport != dcerpc_transport_by_tower(&eps[i].ep)) {
244                         continue;
245                 }
246                 
247                 r->out.towers->twr->tower = eps[i].ep;
248                 r->out.towers->twr->tower_length = 0;
249                 return EPMAPPER_STATUS_OK;
250         }
251
252
253 failed:
254         *r->out.num_towers = 0;
255         r->out.towers->twr = NULL;
256
257         return EPMAPPER_STATUS_NO_MORE_ENTRIES;
258 }
259
260 static error_status_t dcesrv_epm_LookupHandleFree(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
261                                            struct epm_LookupHandleFree *r)
262 {
263         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
264 }
265
266 static error_status_t dcesrv_epm_InqObject(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
267                                     struct epm_InqObject *r)
268 {
269         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
270 }
271
272 static error_status_t dcesrv_epm_MgmtDelete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
273                                struct epm_MgmtDelete *r)
274 {
275         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
276 }
277
278 static error_status_t dcesrv_epm_MapAuth(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
279                             struct epm_MapAuth *r)
280 {
281         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
282 }
283
284 /* include the generated boilerplate */
285 #include "librpc/gen_ndr/ndr_epmapper_s.c"