r19831: Big ldb_dn optimization and interfaces enhancement patch
[kamenim/samba.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 2 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 "includes.h"
37 #include "ldb/include/includes.h"
38
39 struct asq_context {
40
41         enum {ASQ_SEARCH_BASE, ASQ_SEARCH_MULTI} step;
42
43         struct ldb_module *module;
44         void *up_context;
45         int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
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(void *mem_ctx, struct ldb_module *module,
67                                             void *context,
68                                             int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
69 {
70         struct asq_context *ac;
71         struct ldb_handle *h;
72
73         h = talloc_zero(mem_ctx, struct ldb_handle);
74         if (h == NULL) {
75                 ldb_set_errstring(module->ldb, "Out of Memory");
76                 return NULL;
77         }
78
79         h->module = module;
80
81         ac = talloc_zero(h, struct asq_context);
82         if (ac == NULL) {
83                 ldb_set_errstring(module->ldb, "Out of Memory");
84                 talloc_free(h);
85                 return NULL;
86         }
87
88         h->private_data = (void *)ac;
89
90         h->state = LDB_ASYNC_INIT;
91         h->status = LDB_SUCCESS;
92
93         ac->module = module;
94         ac->up_context = context;
95         ac->up_callback = callback;
96
97         return h;
98 }
99
100 static int asq_terminate(struct ldb_handle *handle)
101 {
102         struct asq_context *ac;
103         struct ldb_reply *ares;
104         struct ldb_asq_control *asq;
105         int i;
106
107         ac = talloc_get_type(handle->private_data, struct asq_context);
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->up_callback(ac->module->ldb, ac->up_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
163         /* we are interested only in the single reply (base search) we receive here */
164         if (ares->type == LDB_REPLY_ENTRY) {
165                 ac->base_res = talloc_move(ac, &ares);
166         } else {
167                 talloc_free(ares);
168         }
169
170         return LDB_SUCCESS;
171 error:
172         talloc_free(ares);
173         return LDB_ERR_OPERATIONS_ERROR;
174 }
175
176 static int asq_reqs_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
177 {
178         struct asq_context *ac;
179
180         if (!context || !ares) {
181                 ldb_set_errstring(ldb, "NULL Context or Result in callback");
182                 goto error;
183         }
184
185         ac = talloc_get_type(context, struct asq_context);
186
187         /* we are interested only in the single reply (base search) we receive here */
188         if (ares->type == LDB_REPLY_ENTRY) {
189
190                 /* pass the message up to the original callback as we
191                  * do not have to elaborate on it any further */
192                 return ac->up_callback(ac->module->ldb, ac->up_context, ares);
193                 
194         } else { /* ignore any REFERRAL or DONE reply */
195                 talloc_free(ares);
196         }
197
198         return LDB_SUCCESS;
199 error:
200         talloc_free(ares);
201         return LDB_ERR_OPERATIONS_ERROR;
202 }
203
204 static int asq_search(struct ldb_module *module, struct ldb_request *req)
205 {
206         struct ldb_control *control;
207         struct ldb_asq_control *asq_ctrl;
208         struct asq_context *ac;
209         struct ldb_handle *h;
210         char **base_attrs;
211         int ret;
212
213         /* check if there's a paged request control */
214         control = get_control_from_list(req->controls, LDB_CONTROL_ASQ_OID);
215         if (control == NULL) {
216                 /* not found go on */
217                 return ldb_next_request(module, req);
218         }
219
220         req->handle = NULL;
221
222         if (!req->callback || !req->context) {
223                 ldb_set_errstring(module->ldb,
224                                   "Async interface called with NULL callback function or NULL context");
225                 return LDB_ERR_OPERATIONS_ERROR;
226         }
227         
228         asq_ctrl = talloc_get_type(control->data, struct ldb_asq_control);
229         if (!asq_ctrl) {
230                 return LDB_ERR_PROTOCOL_ERROR;
231         }
232
233         h = init_handle(req, module, req->context, req->callback);
234         if (!h) {
235                 return LDB_ERR_OPERATIONS_ERROR;
236         }
237         ac = talloc_get_type(h->private_data, struct asq_context);
238
239         req->handle = h;
240
241         /* check the search is well formed */
242         if (req->op.search.scope != LDB_SCOPE_BASE) {
243                 ac->asq_ret = ASQ_CTRL_UNWILLING_TO_PERFORM;
244                 return asq_terminate(h);
245         }
246
247         ac->req_attrs = req->op.search.attrs;
248         ac->req_attribute = talloc_strdup(ac, asq_ctrl->source_attribute);
249         if (ac->req_attribute == NULL)
250                 return LDB_ERR_OPERATIONS_ERROR;
251
252         /* get the object to retrieve the DNs to search */
253         ac->base_req = talloc_zero(req, struct ldb_request);
254         if (ac->base_req == NULL)
255                 return LDB_ERR_OPERATIONS_ERROR;
256         ac->base_req->operation = req->operation;
257         ac->base_req->op.search.base = req->op.search.base;
258         ac->base_req->op.search.scope = LDB_SCOPE_BASE;
259         ac->base_req->op.search.tree = req->op.search.tree;
260         base_attrs = talloc_array(ac->base_req, char *, 2);
261         if (base_attrs == NULL)
262                 return LDB_ERR_OPERATIONS_ERROR;
263         base_attrs[0] = talloc_strdup(base_attrs, asq_ctrl->source_attribute);
264         if (base_attrs[0] == NULL)
265                 return LDB_ERR_OPERATIONS_ERROR;
266         base_attrs[1] = NULL;
267         ac->base_req->op.search.attrs = (const char * const *)base_attrs;
268
269         ac->base_req->context = ac;
270         ac->base_req->callback = asq_base_callback;
271         ldb_set_timeout_from_prev_req(module->ldb, req, ac->base_req);
272
273         ac->step = ASQ_SEARCH_BASE;
274
275         ret = ldb_request(module->ldb, ac->base_req);
276
277         if (ret != LDB_SUCCESS) {
278                 return ret;
279         }
280
281         return LDB_SUCCESS;
282 }
283
284 static int asq_requests(struct ldb_handle *handle) {
285         struct asq_context *ac;
286         struct ldb_message_element *el;
287         int i;
288
289         ac = talloc_get_type(handle->private_data, struct asq_context);
290
291         /* look up the DNs */
292         if (ac->base_res == NULL) {
293                 return LDB_ERR_NO_SUCH_OBJECT;
294         }
295         el = ldb_msg_find_element(ac->base_res->message, ac->req_attribute);
296         /* no values found */
297         if (el == NULL) {
298                 ac->asq_ret = ASQ_CTRL_SUCCESS;
299                 return asq_terminate(handle);
300         }
301
302         /* build up the requests call chain */
303         ac->num_reqs = el->num_values;
304         ac->cur_req = 0;
305         ac->reqs = talloc_array(ac, struct ldb_request *, ac->num_reqs);
306         if (ac->reqs == NULL) {
307                 return LDB_ERR_OPERATIONS_ERROR;
308         }
309
310         for (i = 0; i < el->num_values; i++) {
311
312                 ac->reqs[i] = talloc_zero(ac->reqs, struct ldb_request);
313                 if (ac->reqs[i] == NULL)
314                         return LDB_ERR_OPERATIONS_ERROR;
315                 ac->reqs[i]->operation = LDB_SEARCH;
316                 ac->reqs[i]->op.search.base = ldb_dn_new(ac->reqs[i], ac->module->ldb, (const char *)el->values[i].data);
317                 if ( ! ldb_dn_validate(ac->reqs[i]->op.search.base)) {
318                         ac->asq_ret = ASQ_CTRL_INVALID_ATTRIBUTE_SYNTAX;
319                         return asq_terminate(handle);
320                 }
321                 ac->reqs[i]->op.search.scope = LDB_SCOPE_BASE;
322                 ac->reqs[i]->op.search.tree = ac->base_req->op.search.tree;
323                 ac->reqs[i]->op.search.attrs = ac->req_attrs;
324
325                 ac->reqs[i]->context = ac;
326                 ac->reqs[i]->callback = asq_reqs_callback;
327                 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->base_req, ac->reqs[i]);
328         }
329
330         ac->step = ASQ_SEARCH_MULTI;
331
332         return LDB_SUCCESS;
333 }
334
335 static int asq_wait_none(struct ldb_handle *handle)
336 {
337         struct asq_context *ac;
338         int ret;
339     
340         if (!handle || !handle->private_data) {
341                 return LDB_ERR_OPERATIONS_ERROR;
342         }
343
344         if (handle->state == LDB_ASYNC_DONE) {
345                 return handle->status;
346         }
347
348         handle->state = LDB_ASYNC_PENDING;
349         handle->status = LDB_SUCCESS;
350
351         ac = talloc_get_type(handle->private_data, struct asq_context);
352
353
354         switch (ac->step) {
355         case ASQ_SEARCH_BASE:
356                 ret = ldb_wait(ac->base_req->handle, LDB_WAIT_NONE);
357                 
358                 if (ret != LDB_SUCCESS) {
359                         handle->status = ret;
360                         goto done;
361                 }
362
363                 if (ac->base_req->handle->status != LDB_SUCCESS) {
364                         handle->status = ac->base_req->handle->status;
365                         goto done;
366                 }
367                 if (ac->base_req->handle->state != LDB_ASYNC_DONE) {
368                         return LDB_SUCCESS;
369                 }
370
371                 ret = asq_requests(handle);
372
373                 /* no break nor return,
374                  * the set of requests is performed in ASQ_SEARCH_MULTI
375                  */
376                 
377         case ASQ_SEARCH_MULTI:
378
379                 if (ac->reqs[ac->cur_req]->handle == NULL) {
380                         ret = ldb_request(ac->module->ldb, ac->reqs[ac->cur_req]);
381                         if (ret != LDB_SUCCESS) {
382                                 return ret;
383                         }
384                 }
385
386                 ret = ldb_wait(ac->reqs[ac->cur_req]->handle, LDB_WAIT_NONE);
387                 
388                 if (ret != LDB_SUCCESS) {
389                         handle->status = ret;
390                         goto done;
391                 }
392                 if (ac->reqs[ac->cur_req]->handle->status != LDB_SUCCESS) {
393                         handle->status = ac->reqs[ac->cur_req]->handle->status;
394                 }
395
396                 if (ac->reqs[ac->cur_req]->handle->state == LDB_ASYNC_DONE) {
397                         ac->cur_req++;
398                 }
399
400                 if (ac->cur_req < ac->num_reqs) {
401                         return LDB_SUCCESS;
402                 }
403
404                 return asq_terminate(handle);
405
406         default:
407                 ret = LDB_ERR_OPERATIONS_ERROR;
408                 goto done;
409         }
410
411         ret = LDB_SUCCESS;
412
413 done:
414         handle->state = LDB_ASYNC_DONE;
415         return ret;
416 }
417
418 static int asq_wait_all(struct ldb_handle *handle)
419 {
420         int ret;
421
422         while (handle->state != LDB_ASYNC_DONE) {
423                 ret = asq_wait_none(handle);
424                 if (ret != LDB_SUCCESS) {
425                         return ret;
426                 }
427         }
428
429         return handle->status;
430 }
431
432 static int asq_wait(struct ldb_handle *handle, enum ldb_wait_type type)
433 {
434         if (type == LDB_WAIT_ALL) {
435                 return asq_wait_all(handle);
436         } else {
437                 return asq_wait_none(handle);
438         }
439 }
440
441 static int asq_init(struct ldb_module *module)
442 {
443         struct ldb_request *req;
444         int ret;
445
446         req = talloc_zero(module, struct ldb_request);
447         if (req == NULL) {
448                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "asq: Out of memory!\n");
449                 return LDB_ERR_OPERATIONS_ERROR;
450         }
451
452         req->operation = LDB_REQ_REGISTER_CONTROL;
453         req->op.reg_control.oid = LDB_CONTROL_ASQ_OID;
454
455         ret = ldb_request(module->ldb, req);
456         if (ret != LDB_SUCCESS) {
457                 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "asq: Unable to register control with rootdse!\n");
458         }
459
460         return ldb_next_init(module);
461 }
462
463
464 static const struct ldb_module_ops asq_ops = {
465         .name              = "asq",
466         .search            = asq_search,
467         .wait              = asq_wait,
468         .init_context      = asq_init
469 };
470
471 int ldb_asq_init(void)
472 {
473         return ldb_register_module(&asq_ops);
474 }