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