r23795: more v2->v3 conversion
[gd/samba-autobuild/.git] / source4 / lib / ldb / modules / asq.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2005
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 3 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb attribute scoped query control module
29  *
30  *  Description: this module searches all the the objects pointed
31  *               by the DNs contained in the references attribute
32  *
33  *  Author: Simo Sorce
34  */
35
36 #include "ldb_includes.h"
37
38 struct asq_context {
39
40         enum {ASQ_INIT, ASQ_SEARCH_BASE, ASQ_SEARCH_MULTI} step;
41
42         struct ldb_module *module;
43         struct ldb_request *orig_req;
44
45         struct ldb_asq_control *asq_ctrl;
46
47         const char * const *req_attrs;
48         char *req_attribute;
49         enum {
50                 ASQ_CTRL_SUCCESS                        = 0,
51                 ASQ_CTRL_INVALID_ATTRIBUTE_SYNTAX       = 21,
52                 ASQ_CTRL_UNWILLING_TO_PERFORM           = 53,
53                 ASQ_CTRL_AFFECTS_MULTIPLE_DSA           = 71
54         } asq_ret;
55
56         struct ldb_request *base_req;
57         struct ldb_reply *base_res;
58
59         struct ldb_request **reqs;
60         int num_reqs;
61         int cur_req;
62
63         struct ldb_control **controls;
64 };
65
66 static struct ldb_handle *init_handle(struct ldb_request *req, struct ldb_module *module)
67 {
68         struct asq_context *ac;
69         struct ldb_handle *h;
70
71         h = talloc_zero(req, struct ldb_handle);
72         if (h == NULL) {
73                 ldb_set_errstring(module->ldb, "Out of Memory");
74                 return NULL;
75         }
76
77         h->module = module;
78
79         ac = talloc_zero(h, struct asq_context);
80         if (ac == NULL) {
81                 ldb_set_errstring(module->ldb, "Out of Memory");
82                 talloc_free(h);
83                 return NULL;
84         }
85
86         h->private_data = (void *)ac;
87
88         h->state = LDB_ASYNC_INIT;
89         h->status = LDB_SUCCESS;
90
91         ac->step = ASQ_INIT;
92         ac->module = module;
93         ac->orig_req = req;
94
95         return h;
96 }
97
98 static int asq_terminate(struct ldb_handle *handle)
99 {
100         struct asq_context *ac;
101         struct ldb_reply *ares;
102         struct ldb_asq_control *asq;
103         int i;
104
105         ac = talloc_get_type(handle->private_data, struct asq_context);
106         if (ac == NULL) {
107                 return LDB_ERR_OPERATIONS_ERROR;
108         }
109
110         handle->status = LDB_SUCCESS;
111         handle->state = LDB_ASYNC_DONE;
112
113         ares = talloc_zero(ac, struct ldb_reply);
114         if (ares == NULL)
115                 return LDB_ERR_OPERATIONS_ERROR;
116
117         ares->type = LDB_REPLY_DONE;
118
119         if (ac->controls) {
120                 for (i = 0; ac->controls[i]; i++);
121                 ares->controls = talloc_move(ares, &ac->controls);
122         } else {
123                 i = 0;
124         }
125
126         ares->controls = talloc_realloc(ares, ares->controls, struct ldb_control *, i + 2);
127         
128         if (ares->controls == NULL)
129                 return LDB_ERR_OPERATIONS_ERROR;
130
131         ares->controls[i] = talloc(ares->controls, struct ldb_control);
132         if (ares->controls[i] == NULL)
133                 return LDB_ERR_OPERATIONS_ERROR;
134
135         ares->controls[i]->oid = LDB_CONTROL_ASQ_OID;
136         ares->controls[i]->critical = 0;
137
138         asq = talloc_zero(ares->controls[i], struct ldb_asq_control);
139         if (asq == NULL)
140                 return LDB_ERR_OPERATIONS_ERROR;
141
142         asq->result = ac->asq_ret;
143         
144         ares->controls[i]->data = asq;
145
146         ares->controls[i + 1] = NULL;
147
148         ac->orig_req->callback(ac->module->ldb, ac->orig_req->context, ares);
149
150         return LDB_SUCCESS;
151 }
152
153 static int asq_base_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
154 {
155         struct asq_context *ac;
156
157         if (!context || !ares) {
158                 ldb_set_errstring(ldb, "NULL Context or Result in callback");
159                 goto error;
160         }
161
162         ac = talloc_get_type(context, struct asq_context);
163         if (ac == NULL) {
164                 goto error;
165         }
166
167         /* we are interested only in the single reply (base search) we receive here */
168         if (ares->type == LDB_REPLY_ENTRY) {
169                 ac->base_res = talloc_move(ac, &ares);
170         } else {
171                 talloc_free(ares);
172         }
173
174         return LDB_SUCCESS;
175 error:
176         talloc_free(ares);
177         return LDB_ERR_OPERATIONS_ERROR;
178 }
179
180 static int asq_reqs_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
181 {
182         struct asq_context *ac;
183
184         if (!context || !ares) {
185                 ldb_set_errstring(ldb, "NULL Context or Result in callback");
186                 goto error;
187         }
188
189         ac = talloc_get_type(context, struct asq_context);
190         if (ac == NULL) {
191                 goto error;
192         }
193
194         /* we are interested only in the single reply (base search) we receive here */
195         if (ares->type == LDB_REPLY_ENTRY) {
196
197                 /* pass the message up to the original callback as we
198                  * do not have to elaborate on it any further */
199                 return ac->orig_req->callback(ac->module->ldb, ac->orig_req->context, ares);
200                 
201         } else { /* ignore any REFERRAL or DONE reply */
202                 talloc_free(ares);
203         }
204
205         return LDB_SUCCESS;
206 error:
207         talloc_free(ares);
208         return LDB_ERR_OPERATIONS_ERROR;
209 }
210
211 static int asq_build_first_request(struct asq_context *ac)
212 {
213         char **base_attrs;
214
215         ac->base_req = talloc_zero(ac, struct ldb_request);
216         if (ac->base_req == NULL) return LDB_ERR_OPERATIONS_ERROR;
217
218         ac->base_req->operation = ac->orig_req->operation;
219         ac->base_req->op.search.base = ac->orig_req->op.search.base;
220         ac->base_req->op.search.scope = LDB_SCOPE_BASE;
221         ac->base_req->op.search.tree = ac->orig_req->op.search.tree;
222         base_attrs = talloc_array(ac->base_req, char *, 2);
223         if (base_attrs == NULL) return LDB_ERR_OPERATIONS_ERROR;
224
225         base_attrs[0] = talloc_strdup(base_attrs, ac->asq_ctrl->source_attribute);
226         if (base_attrs[0] == NULL) return LDB_ERR_OPERATIONS_ERROR;
227
228         base_attrs[1] = NULL;
229         ac->base_req->op.search.attrs = (const char * const *)base_attrs;
230
231         ac->base_req->context = ac;
232         ac->base_req->callback = asq_base_callback;
233         ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, ac->base_req);
234
235         return LDB_SUCCESS;
236 }
237
238 static int asq_build_multiple_requests(struct asq_context *ac, struct ldb_handle *handle)
239 {
240         struct ldb_message_element *el;
241         int i;
242
243         /* look up the DNs */
244         if (ac->base_res == NULL) {
245                 return LDB_ERR_NO_SUCH_OBJECT;
246         }
247         el = ldb_msg_find_element(ac->base_res->message, ac->req_attribute);
248         /* no values found */
249         if (el == NULL) {
250                 ac->asq_ret = ASQ_CTRL_SUCCESS;
251                 return asq_terminate(handle);
252         }
253
254         ac->num_reqs = el->num_values;
255         ac->cur_req = 0;
256         ac->reqs = talloc_array(ac, struct ldb_request *, ac->num_reqs);
257         if (ac->reqs == NULL) {
258                 return LDB_ERR_OPERATIONS_ERROR;
259         }
260
261         for (i = 0; i < el->num_values; i++) {
262
263                 ac->reqs[i] = talloc_zero(ac->reqs, struct ldb_request);
264                 if (ac->reqs[i] == NULL)
265                         return LDB_ERR_OPERATIONS_ERROR;
266                 ac->reqs[i]->operation = LDB_SEARCH;
267                 ac->reqs[i]->op.search.base = ldb_dn_new(ac->reqs[i], ac->module->ldb, (const char *)el->values[i].data);
268                 if ( ! ldb_dn_validate(ac->reqs[i]->op.search.base)) {
269                         ac->asq_ret = ASQ_CTRL_INVALID_ATTRIBUTE_SYNTAX;
270                         return asq_terminate(handle);
271                 }
272                 ac->reqs[i]->op.search.scope = LDB_SCOPE_BASE;
273                 ac->reqs[i]->op.search.tree = ac->base_req->op.search.tree;
274                 ac->reqs[i]->op.search.attrs = ac->req_attrs;
275
276                 ac->reqs[i]->context = ac;
277                 ac->reqs[i]->callback = asq_reqs_callback;
278                 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->base_req, ac->reqs[i]);
279         }
280
281         return LDB_SUCCESS;
282 }
283
284 static int asq_search_continue(struct ldb_handle *handle)
285 {
286         struct asq_context *ac;
287         int ret;
288     
289         if (!handle || !handle->private_data) {
290                 return LDB_ERR_OPERATIONS_ERROR;
291         }
292
293         if (handle->state == LDB_ASYNC_DONE) {
294                 return handle->status;
295         }
296
297         ac = talloc_get_type(handle->private_data, struct asq_context);
298         if (ac == NULL) {
299                 return LDB_ERR_OPERATIONS_ERROR;
300         }
301
302         switch (ac->step) {
303         case ASQ_INIT:
304                 /* check the search is well formed */
305                 if (ac->orig_req->op.search.scope != LDB_SCOPE_BASE) {
306                         ac->asq_ret = ASQ_CTRL_UNWILLING_TO_PERFORM;
307                         return asq_terminate(handle);
308                 }
309
310                 ac->req_attrs = ac->orig_req->op.search.attrs;
311                 ac->req_attribute = talloc_strdup(ac, ac->asq_ctrl->source_attribute);
312                 if (ac->req_attribute == NULL)
313                         return LDB_ERR_OPERATIONS_ERROR;
314
315                 /* get the object to retrieve the DNs to search */
316                 ret = asq_build_first_request(ac);
317                 if (ret != LDB_SUCCESS) {
318                         return ret;
319                 }
320         
321                 ac->step = ASQ_SEARCH_BASE;
322
323                 handle->state = LDB_ASYNC_PENDING;
324                 handle->status = LDB_SUCCESS;
325
326                 return ldb_request(ac->module->ldb, ac->base_req);
327
328         case ASQ_SEARCH_BASE:
329
330                 ret = ldb_wait(ac->base_req->handle, LDB_WAIT_NONE);
331                 
332                 if (ret != LDB_SUCCESS) {
333                         handle->status = ret;
334                         goto done;
335                 }
336
337                 if (ac->base_req->handle->status != LDB_SUCCESS) {
338                         handle->status = ac->base_req->handle->status;
339                         goto done;
340                 }
341
342                 if (ac->base_req->handle->state == LDB_ASYNC_DONE) {
343
344                         /* build up the requests call chain */
345                         ret = asq_build_multiple_requests(ac, handle);
346                         if (ret != LDB_SUCCESS) {
347                                 return ret;
348                         }
349                         if (handle->state == LDB_ASYNC_DONE) {
350                                 return LDB_SUCCESS;
351                         }
352
353                         ac->step = ASQ_SEARCH_MULTI;
354
355                         return ldb_request(ac->module->ldb, ac->reqs[ac->cur_req]);
356                 }
357
358                 /* request still pending, return to cycle again */
359                 return LDB_SUCCESS;
360
361         case ASQ_SEARCH_MULTI:
362
363                 ret = ldb_wait(ac->reqs[ac->cur_req]->handle, LDB_WAIT_NONE);
364                 
365                 if (ret != LDB_SUCCESS) {
366                         handle->status = ret;
367                         goto done;
368                 }
369                 if (ac->reqs[ac->cur_req]->handle->status != LDB_SUCCESS) {
370                         handle->status = ac->reqs[ac->cur_req]->handle->status;
371                 }
372
373                 if (ac->reqs[ac->cur_req]->handle->state == LDB_ASYNC_DONE) {
374                         ac->cur_req++;
375
376                         if (ac->cur_req < ac->num_reqs) {
377                                 return ldb_request(ac->module->ldb, ac->reqs[ac->cur_req]);
378                         }
379
380                         return asq_terminate(handle);
381                 }
382
383                 /* request still pending, return to cycle again */
384                 return LDB_SUCCESS;
385
386         default:
387                 ret = LDB_ERR_OPERATIONS_ERROR;
388                 break;
389         }
390
391 done:
392         handle->state = LDB_ASYNC_DONE;
393         return ret;
394 }
395
396 static int asq_search(struct ldb_module *module, struct ldb_request *req)
397 {
398         struct ldb_control *control;
399         struct asq_context *ac;
400         struct ldb_handle *h;
401
402         /* check if there's a paged request control */
403         control = ldb_request_get_control(req, LDB_CONTROL_ASQ_OID);
404         if (control == NULL) {
405                 /* not found go on */
406                 return ldb_next_request(module, req);
407         }
408
409         req->handle = NULL;
410
411         if (!req->callback || !req->context) {
412                 ldb_set_errstring(module->ldb,
413                                   "Async interface called with NULL callback function or NULL context");
414                 return LDB_ERR_OPERATIONS_ERROR;
415         }
416
417         h = init_handle(req, module);
418         if (!h) {
419                 return LDB_ERR_OPERATIONS_ERROR;
420         }
421         ac = talloc_get_type(h->private_data, struct asq_context);
422
423         ac->asq_ctrl = talloc_get_type(control->data, struct ldb_asq_control);
424         if (!ac->asq_ctrl) {
425                 return LDB_ERR_PROTOCOL_ERROR;
426         }
427
428         req->handle = h;
429
430         return asq_search_continue(h);
431 }
432
433 static int asq_wait_all(struct ldb_handle *handle)
434 {
435         int ret;
436
437         while (handle->state != LDB_ASYNC_DONE) {
438                 ret = asq_search_continue(handle);
439                 if (ret != LDB_SUCCESS) {
440                         return ret;
441                 }
442         }
443
444         return handle->status;
445 }
446
447 static int asq_wait(struct ldb_handle *handle, enum ldb_wait_type type)
448 {
449         if (type == LDB_WAIT_ALL) {
450                 return asq_wait_all(handle);
451         } else {
452                 return asq_search_continue(handle);
453         }
454 }
455
456 static int asq_init(struct ldb_module *module)
457 {
458         struct ldb_request *req;
459         int ret;
460
461         req = talloc_zero(module, struct ldb_request);
462         if (req == NULL) {
463                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "asq: Out of memory!\n");
464                 return LDB_ERR_OPERATIONS_ERROR;
465         }
466
467         req->operation = LDB_REQ_REGISTER_CONTROL;
468         req->op.reg_control.oid = LDB_CONTROL_ASQ_OID;
469
470         ret = ldb_request(module->ldb, req);
471         if (ret != LDB_SUCCESS) {
472                 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "asq: Unable to register control with rootdse!\n");
473         }
474
475         return ldb_next_init(module);
476 }
477
478
479 static const struct ldb_module_ops asq_ops = {
480         .name              = "asq",
481         .search            = asq_search,
482         .wait              = asq_wait,
483         .init_context      = asq_init
484 };
485
486 int ldb_asq_init(void)
487 {
488         return ldb_register_module(&asq_ops);
489 }