r15944: rename LDB_ASYNC_ADD -> LDB_ADD, LDB_ASYNC_MODIFY -> LDB_MODIFY, etc...
[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 #define ASQ_CTRL_SUCCESS                        0
40 #define ASQ_CTRL_INVALID_ATTRIBUTE_SYNTAX       21
41 #define ASQ_CTRL_UNWILLING_TO_PERFORM           53
42 #define ASQ_CTRL_AFFECTS_MULTIPLE_DSA           71
43
44 struct asq_async_context {
45
46         enum {ASQ_SEARCH_BASE, ASQ_SEARCH_MULTI} step;
47
48         struct ldb_module *module;
49         void *up_context;
50         int (*up_callback)(struct ldb_context *, void *, struct ldb_async_result *);
51         int timeout;
52
53         const char * const *req_attrs;
54         char *req_attribute;
55         int asq_ret;
56
57         struct ldb_request *base_req;
58         struct ldb_async_result *base_res;
59
60         struct ldb_request **reqs;
61         int num_reqs;
62         int cur_req;
63
64         struct ldb_control **controls;
65 };
66
67 static struct ldb_async_handle *init_handle(void *mem_ctx, struct ldb_module *module,
68                                             void *context,
69                                             int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
70                                             int timeout)
71 {
72         struct asq_async_context *ac;
73         struct ldb_async_handle *h;
74
75         h = talloc_zero(mem_ctx, struct ldb_async_handle);
76         if (h == NULL) {
77                 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
78                 return NULL;
79         }
80
81         h->module = module;
82
83         ac = talloc_zero(h, struct asq_async_context);
84         if (ac == NULL) {
85                 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
86                 talloc_free(h);
87                 return NULL;
88         }
89
90         h->private_data = (void *)ac;
91
92         h->state = LDB_ASYNC_INIT;
93         h->status = LDB_SUCCESS;
94
95         ac->module = module;
96         ac->up_context = context;
97         ac->up_callback = callback;
98         ac->timeout = timeout;
99
100         return h;
101 }
102
103 static int asq_terminate(struct ldb_async_handle *handle)
104 {
105         struct asq_async_context *ac;
106         struct ldb_async_result *ares;
107         struct ldb_asq_control *asq;
108         int i;
109
110         ac = talloc_get_type(handle->private_data, struct asq_async_context);
111
112         handle->status = LDB_SUCCESS;
113         handle->state = LDB_ASYNC_DONE;
114
115         ares = talloc_zero(ac, struct ldb_async_result);
116         if (ares == NULL)
117                 return LDB_ERR_OPERATIONS_ERROR;
118
119         ares->type = LDB_REPLY_DONE;
120
121         if (ac->controls) {
122                 for (i = 0; ac->controls[i]; i++);
123                 ares->controls = talloc_steal(ares, ac->controls);
124         } else {
125                 i = 0;
126         }
127
128         ares->controls = talloc_realloc(ares, ares->controls, struct ldb_control *, i + 2);
129         
130         if (ares->controls == NULL)
131                 return LDB_ERR_OPERATIONS_ERROR;
132
133         ares->controls[i] = talloc(ares->controls, struct ldb_control);
134         if (ares->controls[i] == NULL)
135                 return LDB_ERR_OPERATIONS_ERROR;
136
137         ares->controls[i]->oid = LDB_CONTROL_ASQ_OID;
138         ares->controls[i]->critical = 0;
139
140         asq = talloc_zero(ares->controls[i], struct ldb_asq_control);
141         if (asq == NULL)
142                 return LDB_ERR_OPERATIONS_ERROR;
143
144         asq->result = ac->asq_ret;
145         
146         ares->controls[i]->data = asq;
147
148         ares->controls[i + 1] = NULL;
149
150         ac->up_callback(ac->module->ldb, ac->up_context, ares);
151
152         return LDB_SUCCESS;
153 }
154
155 static int asq_base_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
156 {
157         struct asq_async_context *ac;
158
159         if (!context || !ares) {
160                 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
161                 goto error;
162         }
163
164         ac = talloc_get_type(context, struct asq_async_context);
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_steal(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_async_result *ares)
180 {
181         struct asq_async_context *ac;
182
183         if (!context || !ares) {
184                 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
185                 goto error;
186         }
187
188         ac = talloc_get_type(context, struct asq_async_context);
189
190         /* we are interested only in the single reply (base search) we receive here */
191         if (ares->type == LDB_REPLY_ENTRY) {
192
193                 /* pass the message up to the original callback as we
194                  * do not have to elaborate on it any further */
195                 return ac->up_callback(ac->module->ldb, ac->up_context, ares);
196                 
197         } else { /* ignore any REFERRAL or DONE reply */
198                 talloc_free(ares);
199         }
200
201         return LDB_SUCCESS;
202 error:
203         talloc_free(ares);
204         return LDB_ERR_OPERATIONS_ERROR;
205 }
206
207 static int asq_search(struct ldb_module *module, struct ldb_request *req)
208 {
209         struct ldb_control *control;
210         struct ldb_asq_control *asq_ctrl;
211         struct asq_async_context *ac;
212         struct ldb_async_handle *h;
213         char **base_attrs;
214         int ret;
215
216         /* check if there's a paged request control */
217         control = get_control_from_list(req->controls, LDB_CONTROL_ASQ_OID);
218         if (control == NULL) {
219                 /* not found go on */
220                 return ldb_next_request(module, req);
221         }
222
223         req->async.handle = NULL;
224
225         if (!req->async.callback || !req->async.context) {
226                 ldb_set_errstring(module->ldb, talloc_asprintf(module,
227                                   "Async interface called with NULL callback function or NULL context"));
228                 return LDB_ERR_OPERATIONS_ERROR;
229         }
230         
231         asq_ctrl = talloc_get_type(control->data, struct ldb_asq_control);
232         if (!asq_ctrl) {
233                 return LDB_ERR_PROTOCOL_ERROR;
234         }
235
236         h = init_handle(req, module, req->async.context, req->async.callback, req->async.timeout);
237         if (!h) {
238                 return LDB_ERR_OPERATIONS_ERROR;
239         }
240         ac = talloc_get_type(h->private_data, struct asq_async_context);
241
242         req->async.handle = h;
243
244         /* check the search is well formed */
245         if (req->op.search.scope != LDB_SCOPE_BASE) {
246                 ac->asq_ret = ASQ_CTRL_UNWILLING_TO_PERFORM;
247                 return asq_terminate(h);
248         }
249
250         ac->req_attrs = req->op.search.attrs;
251         ac->req_attribute = talloc_strdup(ac, asq_ctrl->source_attribute);
252         if (ac->req_attribute == NULL)
253                 return LDB_ERR_OPERATIONS_ERROR;
254
255         /* get the object to retrieve the DNs to search */
256         ac->base_req = talloc_zero(req, struct ldb_request);
257         if (ac->base_req == NULL)
258                 return LDB_ERR_OPERATIONS_ERROR;
259         ac->base_req->operation = req->operation;
260         ac->base_req->op.search.base = req->op.search.base;
261         ac->base_req->op.search.scope = LDB_SCOPE_BASE;
262         ac->base_req->op.search.tree = req->op.search.tree;
263         base_attrs = talloc_array(ac->base_req, char *, 2);
264         if (base_attrs == NULL)
265                 return LDB_ERR_OPERATIONS_ERROR;
266         base_attrs[0] = talloc_strdup(base_attrs, asq_ctrl->source_attribute);
267         if (base_attrs[0] == NULL)
268                 return LDB_ERR_OPERATIONS_ERROR;
269         base_attrs[1] = NULL;
270         ac->base_req->op.search.attrs = (const char * const *)base_attrs;
271
272         ac->base_req->async.context = ac;
273         ac->base_req->async.callback = asq_base_callback;
274         ac->base_req->async.timeout = req->async.timeout;
275
276         ac->step = ASQ_SEARCH_BASE;
277
278         ret = ldb_request(module->ldb, ac->base_req);
279
280         if (ret != LDB_SUCCESS) {
281                 return ret;
282         }
283
284         return LDB_SUCCESS;
285 }
286
287 static int asq_async_requests(struct ldb_async_handle *handle) {
288         struct asq_async_context *ac;
289         struct ldb_message_element *el;
290         int i;
291
292         ac = talloc_get_type(handle->private_data, struct asq_async_context);
293
294         /* look up the DNs */
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_explode(ac->reqs[i], (const char *)el->values[i].data);
317                 if (ac->reqs[i]->op.search.base == NULL) {
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]->async.context = ac;
326                 ac->reqs[i]->async.callback = asq_reqs_callback;
327                 ac->reqs[i]->async.timeout = ac->base_req->async.timeout;
328         }
329
330         ac->step = ASQ_SEARCH_MULTI;
331
332         return LDB_SUCCESS;
333 }
334
335 static int asq_async_wait_none(struct ldb_async_handle *handle)
336 {
337         struct asq_async_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_async_context);
352
353
354         switch (ac->step) {
355         case ASQ_SEARCH_BASE:
356                 ret = ldb_async_wait(ac->base_req->async.handle, LDB_WAIT_NONE);
357                 
358                 if (ret != LDB_SUCCESS) {
359                         handle->status = ret;
360                         goto done;
361                 }
362
363                 if (ac->base_req->async.handle->status != LDB_SUCCESS) {
364                         handle->status = ac->base_req->async.handle->status;
365                         goto done;
366                 }
367                 if (ac->base_req->async.handle->state != LDB_ASYNC_DONE) {
368                         return LDB_SUCCESS;
369                 }
370
371                 ret = asq_async_requests(handle);
372
373         case ASQ_SEARCH_MULTI:
374
375                 if (ac->reqs[ac->cur_req]->async.handle == NULL) {
376                         ret = ldb_request(ac->module->ldb, ac->reqs[ac->cur_req]);
377                         if (ret != LDB_SUCCESS) {
378                                 return ret;
379                         }
380                 }
381
382                 ret = ldb_async_wait(ac->reqs[ac->cur_req]->async.handle, LDB_WAIT_NONE);
383                 
384                 if (ret != LDB_SUCCESS) {
385                         handle->status = ret;
386                         goto done;
387                 }
388                 if (ac->reqs[ac->cur_req]->async.handle->status != LDB_SUCCESS) {
389                         handle->status = ac->reqs[ac->cur_req]->async.handle->status;
390                 }
391
392                 if (ac->reqs[ac->cur_req]->async.handle->state == LDB_ASYNC_DONE) {
393                         ac->cur_req++;
394                 }
395
396                 if (ac->cur_req < ac->num_reqs) {
397                         return LDB_SUCCESS;
398                 }
399
400                 return asq_terminate(handle);
401
402         default:
403                 ret = LDB_ERR_OPERATIONS_ERROR;
404                 goto done;
405         }
406
407         ret = LDB_SUCCESS;
408
409 done:
410         handle->state = LDB_ASYNC_DONE;
411         return ret;
412 }
413
414 static int asq_async_wait_all(struct ldb_async_handle *handle)
415 {
416         int ret;
417
418         while (handle->state != LDB_ASYNC_DONE) {
419                 ret = asq_async_wait_none(handle);
420                 if (ret != LDB_SUCCESS) {
421                         return ret;
422                 }
423         }
424
425         return handle->status;
426 }
427
428 static int asq_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
429 {
430         if (type == LDB_WAIT_ALL) {
431                 return asq_async_wait_all(handle);
432         } else {
433                 return asq_async_wait_none(handle);
434         }
435 }
436
437 static int asq_init(struct ldb_module *module)
438 {
439         struct ldb_request request;
440         int ret;
441
442         request.operation = LDB_REQ_REGISTER;
443         request.op.reg.oid = LDB_CONTROL_ASQ_OID;
444         request.controls = NULL;
445
446         ret = ldb_request(module->ldb, &request);
447         if (ret != LDB_SUCCESS) {
448                 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "asq: Unable to register control with rootdse!\n");
449                 return LDB_ERR_OTHER;
450         }
451
452         return ldb_next_init(module);
453 }
454
455
456 static const struct ldb_module_ops asq_ops = {
457         .name              = "asq",
458         .search            = asq_search,
459         .async_wait        = asq_async_wait,
460         .init_context      = asq_init
461 };
462
463 int ldb_asq_init(void)
464 {
465         return ldb_register_module(&asq_ops);
466 }