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