f2c7b7833509b3872d37f3b528693a6426c53e68
[metze/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    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "rpc_server/common/common.h"
25
26 typedef uint32_t error_status_t;
27
28 /* handle types for this module */
29 enum handle_types {HTYPE_LOOKUP};
30
31 /* a endpoint combined with an interface description */
32 struct dcesrv_ep_iface {
33         const char *name;
34         struct dcesrv_ep_description ep_description;
35         const char *uuid;
36         uint32_t if_version;
37 };
38
39 /*
40   simple routine to compare a GUID string to a GUID structure
41 */
42 static int guid_cmp(TALLOC_CTX *mem_ctx, const struct GUID *guid, const char *uuid_str)
43 {
44         const char *s = GUID_string(mem_ctx, guid);
45         if (!s || strcasecmp(s, uuid_str)) {
46                 return -1;
47         }
48         return 0;
49 }
50
51 /*
52   fill a protocol tower
53 */
54 static BOOL fill_protocol_tower(TALLOC_CTX *mem_ctx, struct epm_towers *twr, 
55                                 struct dcesrv_ep_iface *e)
56 {
57         twr->num_floors = 5;
58         twr->floors = talloc_array_p(mem_ctx, struct epm_floor, 5);
59         if (!twr->floors) {
60                 return False;
61         }
62         
63         twr->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
64         GUID_from_string(e->uuid, &twr->floors[0].lhs.info.uuid.uuid);
65         twr->floors[0].lhs.info.uuid.version = e->if_version;
66         twr->floors[0].rhs.rhs_data = data_blob_talloc_zero(mem_ctx, 2);
67         
68         /* encoded with NDR ... */
69         twr->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
70         GUID_from_string(NDR_GUID, &twr->floors[1].lhs.info.uuid.uuid);
71         twr->floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
72         twr->floors[1].rhs.rhs_data = data_blob_talloc_zero(mem_ctx, 2);
73         
74         /* on an RPC connection ... */
75         twr->floors[2].lhs.protocol = EPM_PROTOCOL_NCACN_RPC_C;
76         twr->floors[2].lhs.info.lhs_data = data_blob(NULL, 0);
77         twr->floors[2].rhs.rhs_data = data_blob_talloc_zero(mem_ctx, 2);
78
79         switch (e->ep_description.type) {
80         case ENDPOINT_SMB:
81                 /* on a SMB pipe ... */
82                 twr->floors[3].lhs.protocol = EPM_PROTOCOL_NCACN_SMB;
83                 twr->floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
84                 twr->floors[3].rhs.rhs_data.data = talloc_asprintf(mem_ctx, "\\PIPE\\%s", 
85                                                                    e->ep_description.info.smb_pipe);
86                 twr->floors[3].rhs.rhs_data.length = strlen(twr->floors[3].rhs.rhs_data.data)+1;
87                 
88                 /* on an NetBIOS link ... */
89                 twr->floors[4].lhs.protocol = EPM_PROTOCOL_NCACN_NETBIOS;
90                 twr->floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
91                 twr->floors[4].rhs.rhs_data.data = talloc_asprintf(mem_ctx, "\\\\%s", 
92                                                                    lp_netbios_name());
93                 twr->floors[4].rhs.rhs_data.length = strlen(twr->floors[4].rhs.rhs_data.data)+1;
94                 break;
95
96         case ENDPOINT_TCP:
97                 /* on a TCP connection ... */
98                 twr->floors[3].lhs.protocol = EPM_PROTOCOL_NCACN_TCP;
99                 twr->floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
100                 twr->floors[3].rhs.rhs_data = data_blob_talloc(mem_ctx, NULL, 2);
101                 RSSVAL(twr->floors[3].rhs.rhs_data.data, 0, e->ep_description.info.tcp_port);
102                 
103                 /* on an IP link ... */
104                 twr->floors[4].lhs.protocol = EPM_PROTOCOL_NCACN_IP;
105                 twr->floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
106                 twr->floors[4].rhs.rhs_data = data_blob_talloc_zero(mem_ctx, 4);
107                 /* TODO: we should fill in our IP address here as a hint to the 
108                    client */
109                 break;
110         }
111
112         return True;
113 }
114
115
116 /*
117   build a list of all interfaces handled by all endpoint servers
118 */
119 static uint32_t build_ep_list(TALLOC_CTX *mem_ctx,
120                             struct dcesrv_endpoint *endpoint_list,
121                             struct dcesrv_ep_iface **eps)
122 {
123         struct dcesrv_endpoint *d;
124         uint32_t total = 0;
125
126         (*eps) = NULL;
127         
128         for (d=endpoint_list; d; d=d->next) {
129                 struct dcesrv_if_list *iface;
130
131                 for (iface=d->interface_list;iface;iface=iface->next) {
132                         (*eps) = talloc_realloc_p(mem_ctx, *eps, 
133                                                   struct dcesrv_ep_iface,
134                                                   total + 1);
135                         if (!*eps) {
136                                 return 0;
137                         }
138                         (*eps)[total].name = iface->iface.ndr->name;
139                         (*eps)[total].uuid = iface->iface.ndr->uuid;
140                         (*eps)[total].if_version = iface->iface.ndr->if_version;
141                         (*eps)[total].ep_description = d->ep_description;
142                         total++;
143                 }
144         }
145
146         return total;
147 }
148
149
150 static error_status_t epm_Insert(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
151                                  struct epm_Insert *r)
152 {
153         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
154 }
155
156 static error_status_t epm_Delete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
157                                  struct epm_Delete *r)
158 {
159         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
160 }
161
162
163 /*
164   implement epm_Lookup. This call is used to enumerate the interfaces
165   available on a rpc server
166 */
167 static error_status_t epm_Lookup(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
168                                  struct epm_Lookup *r)
169 {
170         struct dcesrv_handle *h;
171         struct rpc_eps {
172                 uint32_t count;
173                 struct dcesrv_ep_iface *e;
174         } *eps;
175         uint32_t num_ents;
176         int i;
177
178         h = dcesrv_handle_fetch(dce_call->conn, r->in.entry_handle, HTYPE_LOOKUP);
179         DCESRV_CHECK_HANDLE(h);
180
181         eps = h->data;
182
183         if (!eps) {
184                 /* this is the first call - fill the list. Subsequent calls 
185                    will feed from this list, stored in the handle */
186                 eps = talloc_p(h->mem_ctx, struct rpc_eps);
187                 if (!eps) {
188                         return EPMAPPER_STATUS_NO_MEMORY;
189                 }
190                 h->data = eps;
191
192                 eps->count = build_ep_list(h->mem_ctx, dce_call->conn->dce_ctx->endpoint_list, &eps->e);
193         }
194
195         /* return the next N elements */
196         num_ents = r->in.max_ents;
197         if (num_ents > eps->count) {
198                 num_ents = eps->count;
199         }
200
201         *r->out.entry_handle = h->wire_handle;
202         r->out.num_ents = num_ents;
203
204         if (num_ents == 0) {
205                 r->out.entries = NULL;
206                 ZERO_STRUCTP(r->out.entry_handle);
207                 dcesrv_handle_destroy(dce_call->conn, h);
208                 return EPMAPPER_STATUS_NO_MORE_ENTRIES;
209         }
210
211         r->out.entries = talloc_array_p(mem_ctx, struct epm_entry_t, num_ents);
212         if (!r->out.entries) {
213                 return EPMAPPER_STATUS_NO_MEMORY;
214         }
215
216         for (i=0;i<num_ents;i++) {
217                 ZERO_STRUCT(r->out.entries[i].object);
218                 r->out.entries[i].annotation = eps->e[i].name;
219                 r->out.entries[i].tower = talloc_p(mem_ctx, struct epm_twr_t);
220                 if (!r->out.entries[i].tower) {
221                         return EPMAPPER_STATUS_NO_MEMORY;
222                 }
223
224                 if (!fill_protocol_tower(mem_ctx, &r->out.entries[i].tower->towers, &eps->e[i])) {
225                         return EPMAPPER_STATUS_NO_MEMORY;
226                 }
227         }
228
229         eps->count -= num_ents;
230         eps->e += num_ents;
231
232         return EPMAPPER_STATUS_OK;
233 }
234
235
236 /*
237   implement epm_Map. This is used to find the specific endpoint to talk to given
238   a generic protocol tower
239 */
240 static error_status_t epm_Map(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
241                               struct epm_Map *r)
242 {
243         uint32_t count;
244         int i;
245         struct dcesrv_ep_iface *eps;
246         struct epm_floor *floors;
247
248         count = build_ep_list(mem_ctx, dce_call->conn->dce_ctx->endpoint_list, &eps);
249
250         ZERO_STRUCT(*r->out.entry_handle);
251         r->out.num_towers = 1;
252         r->out.towers = talloc_p(mem_ctx, struct epm_twr_p_t);
253         if (!r->out.towers) {
254                 return EPMAPPER_STATUS_NO_MEMORY;
255         }
256         r->out.towers->twr = talloc_p(mem_ctx, struct epm_twr_t);
257         if (!r->out.towers->twr) {
258                 return EPMAPPER_STATUS_NO_MEMORY;
259         }
260         
261         if (!r->in.map_tower || r->in.max_towers == 0 ||
262             r->in.map_tower->towers.num_floors != 5) {
263                 goto failed;
264         }
265
266         floors = r->in.map_tower->towers.floors;
267
268         if (floors[0].lhs.protocol != EPM_PROTOCOL_UUID ||
269             floors[1].lhs.protocol != EPM_PROTOCOL_UUID ||
270             guid_cmp(mem_ctx, &floors[1].lhs.info.uuid.uuid, NDR_GUID) != 0 ||
271             floors[1].lhs.info.uuid.version != NDR_GUID_VERSION ||
272             floors[2].lhs.protocol != EPM_PROTOCOL_NCACN_RPC_C) {
273                 goto failed;
274         }
275         
276         for (i=0;i<count;i++) {
277                 if (guid_cmp(mem_ctx, &floors[0].lhs.info.uuid.uuid, eps[i].uuid) != 0 ||
278                     floors[0].lhs.info.uuid.version != eps[i].if_version) {
279                         continue;
280                 }
281                 switch (eps[i].ep_description.type) {
282                 case ENDPOINT_SMB:
283                         if (floors[3].lhs.protocol != EPM_PROTOCOL_NCACN_SMB ||
284                             floors[4].lhs.protocol != EPM_PROTOCOL_NCACN_NETBIOS) {
285                                 continue;
286                         }
287                         break;
288                 case ENDPOINT_TCP:
289                         if (floors[3].lhs.protocol != EPM_PROTOCOL_NCACN_TCP ||
290                             floors[4].lhs.protocol != EPM_PROTOCOL_NCACN_IP) {
291                                 continue;
292                         }
293                         break;
294                 }
295                 fill_protocol_tower(mem_ctx, &r->out.towers->twr->towers, &eps[i]);
296                 r->out.towers->twr->tower_length = 0;
297                 return EPMAPPER_STATUS_OK;
298         }
299
300
301 failed:
302         r->out.num_towers = 0;
303         r->out.towers->twr = NULL;
304
305         return EPMAPPER_STATUS_NO_MORE_ENTRIES;
306 }
307
308 static error_status_t epm_LookupHandleFree(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
309                                            struct epm_LookupHandleFree *r)
310 {
311         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
312 }
313
314 static error_status_t epm_InqObject(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
315                                     struct epm_InqObject *r)
316 {
317         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
318 }
319
320 static error_status_t epm_MgmtDelete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
321                                struct epm_MgmtDelete *r)
322 {
323         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
324 }
325
326 static error_status_t epm_MapAuth(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
327                             struct epm_MapAuth *r)
328 {
329         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
330 }
331
332 /* include the generated boilerplate */
333 #include "librpc/gen_ndr/ndr_epmapper_s.c"