4 Copyright (C) Simo Sorce 2005
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
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.
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.
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
28 * Component: ldb attribute scoped query control module
30 * Description: this module searches all the the objects pointed
31 * by the DNs contained in the references attribute
36 #include "ldb_includes.h"
40 enum {ASQ_INIT, ASQ_SEARCH_BASE, ASQ_SEARCH_MULTI} step;
42 struct ldb_module *module;
43 struct ldb_request *orig_req;
45 struct ldb_asq_control *asq_ctrl;
47 const char * const *req_attrs;
51 ASQ_CTRL_INVALID_ATTRIBUTE_SYNTAX = 21,
52 ASQ_CTRL_UNWILLING_TO_PERFORM = 53,
53 ASQ_CTRL_AFFECTS_MULTIPLE_DSA = 71
56 struct ldb_request *base_req;
57 struct ldb_reply *base_res;
59 struct ldb_request **reqs;
63 struct ldb_control **controls;
66 static struct ldb_handle *init_handle(struct ldb_request *req, struct ldb_module *module)
68 struct asq_context *ac;
71 h = talloc_zero(req, struct ldb_handle);
73 ldb_set_errstring(module->ldb, "Out of Memory");
79 ac = talloc_zero(h, struct asq_context);
81 ldb_set_errstring(module->ldb, "Out of Memory");
86 h->private_data = (void *)ac;
88 h->state = LDB_ASYNC_INIT;
89 h->status = LDB_SUCCESS;
98 static int asq_terminate(struct ldb_handle *handle)
100 struct asq_context *ac;
101 struct ldb_reply *ares;
102 struct ldb_asq_control *asq;
105 ac = talloc_get_type(handle->private_data, struct asq_context);
107 return LDB_ERR_OPERATIONS_ERROR;
110 handle->status = LDB_SUCCESS;
111 handle->state = LDB_ASYNC_DONE;
113 ares = talloc_zero(ac, struct ldb_reply);
115 return LDB_ERR_OPERATIONS_ERROR;
117 ares->type = LDB_REPLY_DONE;
120 for (i = 0; ac->controls[i]; i++);
121 ares->controls = talloc_move(ares, &ac->controls);
126 ares->controls = talloc_realloc(ares, ares->controls, struct ldb_control *, i + 2);
128 if (ares->controls == NULL)
129 return LDB_ERR_OPERATIONS_ERROR;
131 ares->controls[i] = talloc(ares->controls, struct ldb_control);
132 if (ares->controls[i] == NULL)
133 return LDB_ERR_OPERATIONS_ERROR;
135 ares->controls[i]->oid = LDB_CONTROL_ASQ_OID;
136 ares->controls[i]->critical = 0;
138 asq = talloc_zero(ares->controls[i], struct ldb_asq_control);
140 return LDB_ERR_OPERATIONS_ERROR;
142 asq->result = ac->asq_ret;
144 ares->controls[i]->data = asq;
146 ares->controls[i + 1] = NULL;
148 ac->orig_req->callback(ac->module->ldb, ac->orig_req->context, ares);
153 static int asq_base_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
155 struct asq_context *ac;
157 if (!context || !ares) {
158 ldb_set_errstring(ldb, "NULL Context or Result in callback");
162 ac = talloc_get_type(context, struct asq_context);
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);
177 return LDB_ERR_OPERATIONS_ERROR;
180 static int asq_reqs_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
182 struct asq_context *ac;
184 if (!context || !ares) {
185 ldb_set_errstring(ldb, "NULL Context or Result in callback");
189 ac = talloc_get_type(context, struct asq_context);
194 /* we are interested only in the single reply (base search) we receive here */
195 if (ares->type == LDB_REPLY_ENTRY) {
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);
201 } else { /* ignore any REFERRAL or DONE reply */
208 return LDB_ERR_OPERATIONS_ERROR;
211 static int asq_build_first_request(struct asq_context *ac)
215 ac->base_req = talloc_zero(ac, struct ldb_request);
216 if (ac->base_req == NULL) return LDB_ERR_OPERATIONS_ERROR;
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;
225 base_attrs[0] = talloc_strdup(base_attrs, ac->asq_ctrl->source_attribute);
226 if (base_attrs[0] == NULL) return LDB_ERR_OPERATIONS_ERROR;
228 base_attrs[1] = NULL;
229 ac->base_req->op.search.attrs = (const char * const *)base_attrs;
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);
238 static int asq_build_multiple_requests(struct asq_context *ac, struct ldb_handle *handle)
240 struct ldb_message_element *el;
243 /* look up the DNs */
244 if (ac->base_res == NULL) {
245 return LDB_ERR_NO_SUCH_OBJECT;
247 el = ldb_msg_find_element(ac->base_res->message, ac->req_attribute);
248 /* no values found */
250 ac->asq_ret = ASQ_CTRL_SUCCESS;
251 return asq_terminate(handle);
254 ac->num_reqs = el->num_values;
256 ac->reqs = talloc_array(ac, struct ldb_request *, ac->num_reqs);
257 if (ac->reqs == NULL) {
258 return LDB_ERR_OPERATIONS_ERROR;
261 for (i = 0; i < el->num_values; i++) {
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);
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;
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]);
284 static int asq_search_continue(struct ldb_handle *handle)
286 struct asq_context *ac;
289 if (!handle || !handle->private_data) {
290 return LDB_ERR_OPERATIONS_ERROR;
293 if (handle->state == LDB_ASYNC_DONE) {
294 return handle->status;
297 ac = talloc_get_type(handle->private_data, struct asq_context);
299 return LDB_ERR_OPERATIONS_ERROR;
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);
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;
315 /* get the object to retrieve the DNs to search */
316 ret = asq_build_first_request(ac);
317 if (ret != LDB_SUCCESS) {
321 ac->step = ASQ_SEARCH_BASE;
323 handle->state = LDB_ASYNC_PENDING;
324 handle->status = LDB_SUCCESS;
326 return ldb_request(ac->module->ldb, ac->base_req);
328 case ASQ_SEARCH_BASE:
330 ret = ldb_wait(ac->base_req->handle, LDB_WAIT_NONE);
332 if (ret != LDB_SUCCESS) {
333 handle->status = ret;
337 if (ac->base_req->handle->status != LDB_SUCCESS) {
338 handle->status = ac->base_req->handle->status;
342 if (ac->base_req->handle->state == LDB_ASYNC_DONE) {
344 /* build up the requests call chain */
345 ret = asq_build_multiple_requests(ac, handle);
346 if (ret != LDB_SUCCESS) {
349 if (handle->state == LDB_ASYNC_DONE) {
353 ac->step = ASQ_SEARCH_MULTI;
355 return ldb_request(ac->module->ldb, ac->reqs[ac->cur_req]);
358 /* request still pending, return to cycle again */
361 case ASQ_SEARCH_MULTI:
363 ret = ldb_wait(ac->reqs[ac->cur_req]->handle, LDB_WAIT_NONE);
365 if (ret != LDB_SUCCESS) {
366 handle->status = ret;
369 if (ac->reqs[ac->cur_req]->handle->status != LDB_SUCCESS) {
370 handle->status = ac->reqs[ac->cur_req]->handle->status;
373 if (ac->reqs[ac->cur_req]->handle->state == LDB_ASYNC_DONE) {
376 if (ac->cur_req < ac->num_reqs) {
377 return ldb_request(ac->module->ldb, ac->reqs[ac->cur_req]);
380 return asq_terminate(handle);
383 /* request still pending, return to cycle again */
387 ret = LDB_ERR_OPERATIONS_ERROR;
392 handle->state = LDB_ASYNC_DONE;
396 static int asq_search(struct ldb_module *module, struct ldb_request *req)
398 struct ldb_control *control;
399 struct asq_context *ac;
400 struct ldb_handle *h;
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);
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;
417 h = init_handle(req, module);
419 return LDB_ERR_OPERATIONS_ERROR;
421 ac = talloc_get_type(h->private_data, struct asq_context);
423 ac->asq_ctrl = talloc_get_type(control->data, struct ldb_asq_control);
425 return LDB_ERR_PROTOCOL_ERROR;
430 return asq_search_continue(h);
433 static int asq_wait_all(struct ldb_handle *handle)
437 while (handle->state != LDB_ASYNC_DONE) {
438 ret = asq_search_continue(handle);
439 if (ret != LDB_SUCCESS) {
444 return handle->status;
447 static int asq_wait(struct ldb_handle *handle, enum ldb_wait_type type)
449 if (type == LDB_WAIT_ALL) {
450 return asq_wait_all(handle);
452 return asq_search_continue(handle);
456 static int asq_init(struct ldb_module *module)
458 struct ldb_request *req;
461 req = talloc_zero(module, struct ldb_request);
463 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "asq: Out of memory!\n");
464 return LDB_ERR_OPERATIONS_ERROR;
467 req->operation = LDB_REQ_REGISTER_CONTROL;
468 req->op.reg_control.oid = LDB_CONTROL_ASQ_OID;
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");
475 return ldb_next_init(module);
479 static const struct ldb_module_ops asq_ops = {
481 .search = asq_search,
483 .init_context = asq_init
486 int ldb_asq_init(void)
488 return ldb_register_module(&asq_ops);