r23551: Change data_blob_equal to data_blob_cmp, suitable for sorting with qsort().
[kai/samba-autobuild/.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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "librpc/gen_ndr/ndr_epmapper.h"
26 #include "rpc_server/dcerpc_server.h"
27 #include "rpc_server/common/common.h"
28
29 typedef uint32_t error_status_t;
30
31 /* handle types for this module */
32 enum handle_types {HTYPE_LOOKUP};
33
34 /* a endpoint combined with an interface description */
35 struct dcesrv_ep_iface {
36         const char *name;
37         struct epm_tower ep;
38 };
39
40 /*
41   build a list of all interfaces handled by all endpoint servers
42 */
43 static uint32_t build_ep_list(TALLOC_CTX *mem_ctx,
44                               struct dcesrv_endpoint *endpoint_list,
45                               struct dcesrv_ep_iface **eps)
46 {
47         struct dcesrv_endpoint *d;
48         uint32_t total = 0;
49         NTSTATUS status;
50
51         *eps = NULL;
52
53         for (d=endpoint_list; d; d=d->next) {
54                 struct dcesrv_if_list *iface;
55                 struct dcerpc_binding *description;
56
57                 for (iface=d->interface_list;iface;iface=iface->next) {
58                         (*eps) = talloc_realloc(mem_ctx, 
59                                                   *eps, 
60                                                   struct dcesrv_ep_iface,
61                                                   total + 1);
62                         if (!*eps) {
63                                 return 0;
64                         }
65                         (*eps)[total].name = iface->iface.name;
66
67                         description = d->ep_description;
68                         description->object = iface->iface.syntax_id;
69
70                         status = dcerpc_binding_build_tower(mem_ctx, description, &(*eps)[total].ep);
71                         if (NT_STATUS_IS_ERR(status)) {
72                                 DEBUG(1, ("Unable to build tower for %s\n", iface->iface.name));
73                                 continue;
74                         }
75                         total++;
76                 }
77         }
78
79         return total;
80 }
81
82
83 static error_status_t dcesrv_epm_Insert(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct epm_Insert *r)
84 {
85         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
86 }
87
88 static error_status_t dcesrv_epm_Delete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
89                                  struct epm_Delete *r)
90 {
91         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
92 }
93
94
95 /*
96   implement epm_Lookup. This call is used to enumerate the interfaces
97   available on a rpc server
98 */
99 static error_status_t dcesrv_epm_Lookup(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
100                                  struct epm_Lookup *r)
101 {
102         struct dcesrv_handle *h;
103         struct rpc_eps {
104                 uint32_t count;
105                 struct dcesrv_ep_iface *e;
106         } *eps;
107         uint32_t num_ents;
108         int i;
109
110         DCESRV_PULL_HANDLE_FAULT(h, r->in.entry_handle, HTYPE_LOOKUP);
111
112         eps = h->data;
113
114         if (!eps) {
115                 /* this is the first call - fill the list. Subsequent calls 
116                    will feed from this list, stored in the handle */
117                 eps = talloc(h, struct rpc_eps);
118                 if (!eps) {
119                         return EPMAPPER_STATUS_NO_MEMORY;
120                 }
121                 h->data = eps;
122
123                 eps->count = build_ep_list(h, dce_call->conn->dce_ctx->endpoint_list, &eps->e);
124         }
125
126         /* return the next N elements */
127         num_ents = r->in.max_ents;
128         if (num_ents > eps->count) {
129                 num_ents = eps->count;
130         }
131
132         *r->out.entry_handle = h->wire_handle;
133         r->out.num_ents = talloc(mem_ctx, uint32_t);
134         *r->out.num_ents = num_ents;
135
136         if (num_ents == 0) {
137                 r->out.entries = NULL;
138                 ZERO_STRUCTP(r->out.entry_handle);
139                 talloc_free(h);
140                 return EPMAPPER_STATUS_NO_MORE_ENTRIES;
141         }
142
143         r->out.entries = talloc_array(mem_ctx, struct epm_entry_t, num_ents);
144         if (!r->out.entries) {
145                 return EPMAPPER_STATUS_NO_MEMORY;
146         }
147
148         for (i=0;i<num_ents;i++) {
149                 ZERO_STRUCT(r->out.entries[i].object);
150                 r->out.entries[i].annotation = eps->e[i].name;
151                 r->out.entries[i].tower = talloc(mem_ctx, struct epm_twr_t);
152                 if (!r->out.entries[i].tower) {
153                         return EPMAPPER_STATUS_NO_MEMORY;
154                 }
155                 r->out.entries[i].tower->tower = eps->e[i].ep;
156         }
157
158         eps->count -= num_ents;
159         eps->e += num_ents;
160
161         return EPMAPPER_STATUS_OK;
162 }
163
164
165 /*
166   implement epm_Map. This is used to find the specific endpoint to talk to given
167   a generic protocol tower
168 */
169 static error_status_t dcesrv_epm_Map(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
170                               struct epm_Map *r)
171 {
172         uint32_t count;
173         int i;
174         struct dcesrv_ep_iface *eps;
175         struct epm_floor *floors;
176         enum dcerpc_transport_t transport;
177         struct dcerpc_syntax_id ndr_syntax;
178
179         count = build_ep_list(mem_ctx, dce_call->conn->dce_ctx->endpoint_list, &eps);
180
181         ZERO_STRUCT(*r->out.entry_handle);
182         r->out.num_towers = talloc(mem_ctx, uint32_t);
183         *r->out.num_towers = 1;
184         r->out.towers = talloc(mem_ctx, struct epm_twr_p_t);
185         if (!r->out.towers) {
186                 return EPMAPPER_STATUS_NO_MEMORY;
187         }
188         r->out.towers->twr = talloc(mem_ctx, struct epm_twr_t);
189         if (!r->out.towers->twr) {
190                 return EPMAPPER_STATUS_NO_MEMORY;
191         }
192         
193         if (!r->in.map_tower || r->in.max_towers == 0 || 
194             r->in.map_tower->tower.num_floors < 3) {
195                 goto failed;
196         }
197
198         floors = r->in.map_tower->tower.floors;
199
200         dcerpc_floor_get_lhs_data(&r->in.map_tower->tower.floors[1], &ndr_syntax);
201
202         if (floors[1].lhs.protocol != EPM_PROTOCOL_UUID ||
203                 !GUID_equal(&ndr_syntax.uuid, &ndr_transfer_syntax.uuid) ||
204             ndr_syntax.if_version != ndr_transfer_syntax.if_version) {
205                 goto failed;
206         }
207
208         transport = dcerpc_transport_by_tower(&r->in.map_tower->tower);
209
210         if (transport == -1) {
211                 DEBUG(2, ("Client requested unknown transport with levels: "));
212                 for (i = 2; i < r->in.map_tower->tower.num_floors; i++) {
213                         DEBUG(2, ("%d, ", r->in.map_tower->tower.floors[i].lhs.protocol));
214                 }
215                 DEBUG(2, ("\n"));
216                 goto failed;
217         }
218
219         for (i=0;i<count;i++) {
220                 if (
221                         data_blob_cmp(&r->in.map_tower->tower.floors[0].lhs.lhs_data, 
222                         &eps[i].ep.floors[0].lhs.lhs_data) != 0 
223                         || transport != dcerpc_transport_by_tower(&eps[i].ep)) {
224                         continue;
225                 }
226                 
227                 r->out.towers->twr->tower = eps[i].ep;
228                 r->out.towers->twr->tower_length = 0;
229                 return EPMAPPER_STATUS_OK;
230         }
231
232
233 failed:
234         *r->out.num_towers = 0;
235         r->out.towers->twr = NULL;
236
237         return EPMAPPER_STATUS_NO_MORE_ENTRIES;
238 }
239
240 static error_status_t dcesrv_epm_LookupHandleFree(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
241                                            struct epm_LookupHandleFree *r)
242 {
243         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
244 }
245
246 static error_status_t dcesrv_epm_InqObject(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
247                                     struct epm_InqObject *r)
248 {
249         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
250 }
251
252 static error_status_t dcesrv_epm_MgmtDelete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
253                                struct epm_MgmtDelete *r)
254 {
255         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
256 }
257
258 static error_status_t dcesrv_epm_MapAuth(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
259                             struct epm_MapAuth *r)
260 {
261         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
262 }
263
264 /* include the generated boilerplate */
265 #include "librpc/gen_ndr/ndr_epmapper_s.c"