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