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