r1817: fixed fault code generation for unimplemented functions in epmapper
[nivanova/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
25
26 /* handle types for this module */
27 enum handle_types {HTYPE_LOOKUP};
28
29 /* a endpoint combined with an interface description */
30 struct dcesrv_ep_iface {
31         const char *name;
32         struct dcesrv_ep_description ep_description;
33         const char *uuid;
34         uint32_t if_version;
35 };
36
37 /*
38   simple routine to compare a GUID string to a GUID structure
39 */
40 static int guid_cmp(TALLOC_CTX *mem_ctx, const struct GUID *guid, const char *uuid_str)
41 {
42         const char *s = GUID_string(mem_ctx, guid);
43         if (!s || strcasecmp(s, uuid_str)) {
44                 return -1;
45         }
46         return 0;
47 }
48
49 /*
50   fill a protocol tower
51 */
52 static BOOL fill_protocol_tower(TALLOC_CTX *mem_ctx, struct epm_towers *twr, 
53                                 struct dcesrv_ep_iface *e)
54 {
55         twr->num_floors = 5;
56         twr->floors = talloc_array_p(mem_ctx, struct epm_floor, 5);
57         if (!twr->floors) {
58                 return False;
59         }
60         
61         twr->floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
62         GUID_from_string(e->uuid, &twr->floors[0].lhs.info.uuid.uuid);
63         twr->floors[0].lhs.info.uuid.version = e->if_version;
64         twr->floors[0].rhs.rhs_data = data_blob_talloc_zero(mem_ctx, 2);
65         
66         /* encoded with NDR ... */
67         twr->floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
68         GUID_from_string(NDR_GUID, &twr->floors[1].lhs.info.uuid.uuid);
69         twr->floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
70         twr->floors[1].rhs.rhs_data = data_blob_talloc_zero(mem_ctx, 2);
71         
72         /* on an RPC connection ... */
73         twr->floors[2].lhs.protocol = EPM_PROTOCOL_NCACN_RPC_C;
74         twr->floors[2].lhs.info.lhs_data = data_blob(NULL, 0);
75         twr->floors[2].rhs.rhs_data = data_blob_talloc_zero(mem_ctx, 2);
76
77         switch (e->ep_description.type) {
78         case ENDPOINT_SMB:
79                 /* on a SMB pipe ... */
80                 twr->floors[3].lhs.protocol = EPM_PROTOCOL_NCACN_SMB;
81                 twr->floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
82                 twr->floors[3].rhs.rhs_data.data = talloc_asprintf(mem_ctx, "\\PIPE\\%s", 
83                                                                    e->ep_description.info.smb_pipe);
84                 twr->floors[3].rhs.rhs_data.length = strlen(twr->floors[3].rhs.rhs_data.data)+1;
85                 
86                 /* on an NetBIOS link ... */
87                 twr->floors[4].lhs.protocol = EPM_PROTOCOL_NCACN_NETBIOS;
88                 twr->floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
89                 twr->floors[4].rhs.rhs_data.data = talloc_asprintf(mem_ctx, "\\\\%s", 
90                                                                    lp_netbios_name());
91                 twr->floors[4].rhs.rhs_data.length = strlen(twr->floors[4].rhs.rhs_data.data)+1;
92                 break;
93
94         case ENDPOINT_TCP:
95                 /* on a TCP connection ... */
96                 twr->floors[3].lhs.protocol = EPM_PROTOCOL_NCACN_TCP;
97                 twr->floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
98                 twr->floors[3].rhs.rhs_data = data_blob_talloc(mem_ctx, NULL, 2);
99                 RSSVAL(twr->floors[3].rhs.rhs_data.data, 0, e->ep_description.info.tcp_port);
100                 
101                 /* on an IP link ... */
102                 twr->floors[4].lhs.protocol = EPM_PROTOCOL_NCACN_IP;
103                 twr->floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
104                 twr->floors[4].rhs.rhs_data = data_blob_talloc_zero(mem_ctx, 4);
105                 /* TODO: we should fill in our IP address here as a hint to the 
106                    client */
107                 break;
108         }
109
110         return True;
111 }
112
113
114 /*
115   build a list of all interfaces handled by all endpoint servers
116 */
117 static uint32_t build_ep_list(TALLOC_CTX *mem_ctx,
118                             struct dcesrv_endpoint *endpoint_list,
119                             struct dcesrv_ep_iface **eps)
120 {
121         struct dcesrv_endpoint *d;
122         uint32_t total = 0;
123
124         (*eps) = NULL;
125         
126         for (d=endpoint_list; d; d=d->next) {
127                 struct dcesrv_if_list *iface;
128
129                 for (iface=d->interface_list;iface;iface=iface->next) {
130                         (*eps) = talloc_realloc_p(mem_ctx, *eps, 
131                                                   struct dcesrv_ep_iface,
132                                                   total + 1);
133                         if (!*eps) {
134                                 return 0;
135                         }
136                         (*eps)[total].name = iface->iface.ndr->name;
137                         (*eps)[total].uuid = iface->iface.ndr->uuid;
138                         (*eps)[total].if_version = iface->iface.ndr->if_version;
139                         (*eps)[total].ep_description = d->ep_description;
140                         total++;
141                 }
142         }
143
144         return total;
145 }
146
147
148 static NTSTATUS epm_Insert(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
149                            struct epm_Insert *r)
150 {
151         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
152 }
153
154 static NTSTATUS epm_Delete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
155                            struct epm_Delete *r)
156 {
157         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
158 }
159
160
161 /*
162   implement epm_Lookup. This call is used to enumerate the interfaces
163   available on a rpc server
164 */
165 static NTSTATUS epm_Lookup(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
166                            struct epm_Lookup *r)
167 {
168         struct dcesrv_handle *h;
169         struct rpc_eps {
170                 uint32_t count;
171                 struct dcesrv_ep_iface *e;
172         } *eps;
173         uint32_t num_ents;
174         int i;
175
176         h = dcesrv_handle_fetch(dce_call->conn, r->in.entry_handle, HTYPE_LOOKUP);
177         if (!h) {
178                 return NT_STATUS_INVALID_HANDLE;
179         }
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 NT_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         r->out.status = 0;
204
205         if (num_ents == 0) {
206                 r->out.entries = NULL;
207                 r->out.status  = EPMAPPER_STATUS_NO_MORE_ENTRIES;
208                 ZERO_STRUCTP(r->out.entry_handle);
209                 dcesrv_handle_destroy(dce_call->conn, h);
210                 return NT_STATUS_OK;
211         }
212
213         r->out.entries = talloc_array_p(mem_ctx, struct epm_entry_t, num_ents);
214         if (!r->out.entries) {
215                 return NT_STATUS_NO_MEMORY;
216         }
217
218         for (i=0;i<num_ents;i++) {
219                 ZERO_STRUCT(r->out.entries[i].object);
220                 r->out.entries[i].annotation = eps->e[i].name;
221                 r->out.entries[i].tower = talloc_p(mem_ctx, struct epm_twr_t);
222                 if (!r->out.entries[i].tower) {
223                         return NT_STATUS_NO_MEMORY;
224                 }
225
226                 if (!fill_protocol_tower(mem_ctx, &r->out.entries[i].tower->towers, &eps->e[i])) {
227                         return NT_STATUS_NO_MEMORY;
228                 }
229         }
230
231         eps->count -= num_ents;
232         eps->e += num_ents;
233
234         return NT_STATUS_OK;
235 }
236
237
238 /*
239   implement epm_Map. This is used to find the specific endpoint to talk to given
240   a generic protocol tower
241 */
242 static NTSTATUS epm_Map(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
243                         struct epm_Map *r)
244 {
245         uint32_t count;
246         int i;
247         struct dcesrv_ep_iface *eps;
248         struct epm_floor *floors;
249
250         count = build_ep_list(mem_ctx, dce_call->conn->dce_ctx->endpoint_list, &eps);
251
252         ZERO_STRUCT(*r->out.entry_handle);
253         r->out.num_towers = 1;
254         r->out.status = 0;
255         r->out.towers = talloc_p(mem_ctx, struct epm_twr_p_t);
256         if (!r->out.towers) {
257                 return NT_STATUS_NO_MEMORY;
258         }
259         r->out.towers->twr = talloc_p(mem_ctx, struct epm_twr_t);
260         if (!r->out.towers->twr) {
261                 return NT_STATUS_NO_MEMORY;
262         }
263         
264         if (!r->in.map_tower || r->in.max_towers == 0 ||
265             r->in.map_tower->towers.num_floors != 5) {
266                 goto failed;
267         }
268
269         floors = r->in.map_tower->towers.floors;
270
271         if (floors[0].lhs.protocol != EPM_PROTOCOL_UUID ||
272             floors[1].lhs.protocol != EPM_PROTOCOL_UUID ||
273             guid_cmp(mem_ctx, &floors[1].lhs.info.uuid.uuid, NDR_GUID) != 0 ||
274             floors[1].lhs.info.uuid.version != NDR_GUID_VERSION ||
275             floors[2].lhs.protocol != EPM_PROTOCOL_NCACN_RPC_C) {
276                 goto failed;
277         }
278         
279         for (i=0;i<count;i++) {
280                 if (guid_cmp(mem_ctx, &floors[0].lhs.info.uuid.uuid, eps[i].uuid) != 0 ||
281                     floors[0].lhs.info.uuid.version != eps[i].if_version) {
282                         continue;
283                 }
284                 switch (eps[i].ep_description.type) {
285                 case ENDPOINT_SMB:
286                         if (floors[3].lhs.protocol != EPM_PROTOCOL_NCACN_SMB ||
287                             floors[4].lhs.protocol != EPM_PROTOCOL_NCACN_NETBIOS) {
288                                 continue;
289                         }
290                         break;
291                 case ENDPOINT_TCP:
292                         if (floors[3].lhs.protocol != EPM_PROTOCOL_NCACN_TCP ||
293                             floors[4].lhs.protocol != EPM_PROTOCOL_NCACN_IP) {
294                                 continue;
295                         }
296                         break;
297                 }
298                 fill_protocol_tower(mem_ctx, &r->out.towers->twr->towers, &eps[i]);
299                 r->out.towers->twr->tower_length = 0;
300                 return NT_STATUS_OK;
301         }
302
303
304 failed:
305         r->out.num_towers = 0;
306         r->out.status = EPMAPPER_STATUS_NO_MORE_ENTRIES;
307         r->out.towers->twr = NULL;
308
309         return NT_STATUS_OK;
310 }
311
312 static NTSTATUS epm_LookupHandleFree(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
313                                      struct epm_LookupHandleFree *r)
314 {
315         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
316 }
317
318 static NTSTATUS epm_InqObject(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
319                               struct epm_InqObject *r)
320 {
321         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
322 }
323
324 static NTSTATUS epm_MgmtDelete(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, 
325                                struct epm_MgmtDelete *r)
326 {
327         DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
328 }
329
330 static NTSTATUS epm_MapAuth(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
331                             struct epm_MapAuth *r)
332 {
333         return NT_STATUS_NOT_IMPLEMENTED;
334 }
335
336 /* include the generated boilerplate */
337 #include "librpc/gen_ndr/ndr_epmapper_s.c"