8519f994866303788f09d94604180ba92a94c08d
[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 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 dcerpc_binding ep_description;
38 };
39
40 /*
41   simple routine to compare a GUID string to a GUID structure
42 */
43 static int guid_cmp(TALLOC_CTX *mem_ctx, const struct GUID *guid, const char *uuid_str)
44 {
45         const char *s = GUID_string(mem_ctx, guid);
46         if (!s || strcasecmp(s, uuid_str)) {
47                 return -1;
48         }
49         return 0;
50 }
51
52 /*
53   build a list of all interfaces handled by all endpoint servers
54 */
55 static uint32_t build_ep_list(TALLOC_CTX *mem_ctx,
56                               struct dcesrv_endpoint *endpoint_list,
57                               struct dcesrv_ep_iface **eps)
58 {
59         struct dcesrv_endpoint *d;
60         uint32_t total = 0;
61
62         *eps = NULL;
63
64         for (d=endpoint_list; d; d=d->next) {
65                 struct dcesrv_if_list *iface;
66
67                 for (iface=d->interface_list;iface;iface=iface->next) {
68                         (*eps) = talloc_realloc_p(mem_ctx, 
69                                                   *eps, 
70                                                   struct dcesrv_ep_iface,
71                                                   total + 1);
72                         if (!*eps) {
73                                 return 0;
74                         }
75                         (*eps)[total].name = iface->iface.name;
76                         (*eps)[total].ep_description = d->ep_description;
77                         GUID_from_string(iface->iface.uuid, &(*eps)[total].ep_description.object);
78                         (*eps)[total].ep_description.object_version = iface->iface.if_version;
79                         total++;
80                 }
81         }
82
83         return total;
84 }
85
86
87 static error_status_t epm_Insert(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
88                                  struct epm_Insert *r)
89 {
90         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
91 }
92
93 static error_status_t epm_Delete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
94                                  struct epm_Delete *r)
95 {
96         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
97 }
98
99
100 /*
101   implement epm_Lookup. This call is used to enumerate the interfaces
102   available on a rpc server
103 */
104 static error_status_t epm_Lookup(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
105                                  struct epm_Lookup *r)
106 {
107         struct dcesrv_handle *h;
108         struct rpc_eps {
109                 uint32_t count;
110                 struct dcesrv_ep_iface *e;
111         } *eps;
112         uint32_t num_ents;
113         NTSTATUS status;
114         int i;
115
116         h = dcesrv_handle_fetch(dce_call->conn, r->in.entry_handle, HTYPE_LOOKUP);
117         DCESRV_CHECK_HANDLE(h);
118
119         eps = h->data;
120
121         if (!eps) {
122                 /* this is the first call - fill the list. Subsequent calls 
123                    will feed from this list, stored in the handle */
124                 eps = talloc_p(h, struct rpc_eps);
125                 if (!eps) {
126                         return EPMAPPER_STATUS_NO_MEMORY;
127                 }
128                 h->data = eps;
129
130                 eps->count = build_ep_list(h, dce_call->conn->dce_ctx->endpoint_list, &eps->e);
131         }
132
133         /* return the next N elements */
134         num_ents = r->in.max_ents;
135         if (num_ents > eps->count) {
136                 num_ents = eps->count;
137         }
138
139         *r->out.entry_handle = h->wire_handle;
140         r->out.num_ents = num_ents;
141
142         if (num_ents == 0) {
143                 r->out.entries = NULL;
144                 ZERO_STRUCTP(r->out.entry_handle);
145                 dcesrv_handle_destroy(dce_call->conn, h);
146                 return EPMAPPER_STATUS_NO_MORE_ENTRIES;
147         }
148
149         r->out.entries = talloc_array_p(mem_ctx, struct epm_entry_t, num_ents);
150         if (!r->out.entries) {
151                 return EPMAPPER_STATUS_NO_MEMORY;
152         }
153
154         for (i=0;i<num_ents;i++) {
155                 ZERO_STRUCT(r->out.entries[i].object);
156                 r->out.entries[i].annotation = eps->e[i].name;
157                 r->out.entries[i].tower = talloc_p(mem_ctx, struct epm_twr_t);
158                 if (!r->out.entries[i].tower) {
159                         return EPMAPPER_STATUS_NO_MEMORY;
160                 }
161
162                 status = dcerpc_binding_build_tower(mem_ctx, &eps->e[i].ep_description, &r->out.entries[i].tower->tower);
163                 if (NT_STATUS_IS_ERR(status)) {
164                         return EPMAPPER_STATUS_NO_MEMORY;
165                 }
166         }
167
168         eps->count -= num_ents;
169         eps->e += num_ents;
170
171         return EPMAPPER_STATUS_OK;
172 }
173
174
175 /*
176   implement epm_Map. This is used to find the specific endpoint to talk to given
177   a generic protocol tower
178 */
179 static error_status_t epm_Map(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
180                               struct epm_Map *r)
181 {
182         uint32_t count;
183         int i;
184         struct dcesrv_ep_iface *eps;
185         struct epm_floor *floors;
186         enum dcerpc_transport_t transport;
187         NTSTATUS status;
188
189         count = build_ep_list(mem_ctx, dce_call->conn->dce_ctx->endpoint_list, &eps);
190
191         ZERO_STRUCT(*r->out.entry_handle);
192         r->out.num_towers = 1;
193         r->out.towers = talloc_p(mem_ctx, struct epm_twr_p_t);
194         if (!r->out.towers) {
195                 return EPMAPPER_STATUS_NO_MEMORY;
196         }
197         r->out.towers->twr = talloc_p(mem_ctx, struct epm_twr_t);
198         if (!r->out.towers->twr) {
199                 return EPMAPPER_STATUS_NO_MEMORY;
200         }
201         
202         if (!r->in.map_tower || r->in.max_towers == 0 || 
203             r->in.map_tower->tower.num_floors < 3) {
204                 goto failed;
205         }
206
207         floors = r->in.map_tower->tower.floors;
208
209         if (floors[0].lhs.protocol != EPM_PROTOCOL_UUID ||
210             floors[1].lhs.protocol != EPM_PROTOCOL_UUID ||
211             guid_cmp(mem_ctx, &floors[1].lhs.info.uuid.uuid, NDR_GUID) != 0 ||
212             floors[1].lhs.info.uuid.version != NDR_GUID_VERSION) {
213                 goto failed;
214         }
215
216         transport = dcerpc_transport_by_tower(&r->in.map_tower->tower);
217
218         if (transport == -1) {
219                 DEBUG(1, ("Client requested unknown transport with levels: "));
220                 for (i = 2; i < r->in.map_tower->tower.num_floors; i++) {
221                         DEBUG(1, ("%d, ", r->in.map_tower->tower.floors[i].lhs.protocol));
222                 }
223                 goto failed;
224         }
225         
226         for (i=0;i<count;i++) {
227                 struct epm_tower t;
228                 if (!GUID_equal(&floors[0].lhs.info.uuid.uuid, &eps[i].ep_description.object) ||
229                     floors[0].lhs.info.uuid.version != eps[i].ep_description.object_version) {
230                         continue;
231                 }
232
233                 if (transport != eps[i].ep_description.transport) {
234                         continue;
235                 }
236                 
237                 status = dcerpc_binding_build_tower(mem_ctx, 
238                                                 &eps[i].ep_description, 
239                                                 &t);
240
241                 if (NT_STATUS_IS_ERR(status)) {
242                         return EPMAPPER_STATUS_NO_MEMORY;
243                 }
244                 r->out.towers->twr->tower = t;
245                 r->out.towers->twr->tower_length = 0;
246                 return EPMAPPER_STATUS_OK;
247         }
248
249
250 failed:
251         r->out.num_towers = 0;
252         r->out.towers->twr = NULL;
253
254         return EPMAPPER_STATUS_NO_MORE_ENTRIES;
255 }
256
257 static error_status_t epm_LookupHandleFree(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
258                                            struct epm_LookupHandleFree *r)
259 {
260         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
261 }
262
263 static error_status_t epm_InqObject(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
264                                     struct epm_InqObject *r)
265 {
266         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
267 }
268
269 static error_status_t epm_MgmtDelete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
270                                struct epm_MgmtDelete *r)
271 {
272         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
273 }
274
275 static error_status_t epm_MapAuth(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
276                             struct epm_MapAuth *r)
277 {
278         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
279 }
280
281 /* include the generated boilerplate */
282 #include "librpc/gen_ndr/ndr_epmapper_s.c"