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, see <http://www.gnu.org/licenses/>.
27 * Component: ldb attribute scoped query control module
29 * Description: this module searches all the the objects pointed
30 * by the DNs contained in the references attribute
35 #include "ldb_includes.h"
39 enum {ASQ_INIT, ASQ_SEARCH_BASE, ASQ_SEARCH_MULTI} step;
41 struct ldb_module *module;
42 struct ldb_request *orig_req;
44 struct ldb_asq_control *asq_ctrl;
46 const char * const *req_attrs;
50 ASQ_CTRL_INVALID_ATTRIBUTE_SYNTAX = 21,
51 ASQ_CTRL_UNWILLING_TO_PERFORM = 53,
52 ASQ_CTRL_AFFECTS_MULTIPLE_DSA = 71
55 struct ldb_request *base_req;
56 struct ldb_reply *base_res;
58 struct ldb_request **reqs;
62 struct ldb_control **controls;
65 static struct ldb_handle *init_handle(struct ldb_request *req, struct ldb_module *module)
67 struct asq_context *ac;
70 h = talloc_zero(req, struct ldb_handle);
72 ldb_set_errstring(module->ldb, "Out of Memory");
78 ac = talloc_zero(h, struct asq_context);
80 ldb_set_errstring(module->ldb, "Out of Memory");
85 h->private_data = (void *)ac;
87 h->state = LDB_ASYNC_INIT;
88 h->status = LDB_SUCCESS;
97 static int asq_terminate(struct ldb_handle *handle)
99 struct asq_context *ac;
100 struct ldb_reply *ares;
101 struct ldb_asq_control *asq;
104 ac = talloc_get_type(handle->private_data, struct asq_context);
106 return LDB_ERR_OPERATIONS_ERROR;
109 handle->status = LDB_SUCCESS;
110 handle->state = LDB_ASYNC_DONE;
112 ares = talloc_zero(ac, struct ldb_reply);
114 return LDB_ERR_OPERATIONS_ERROR;
116 ares->type = LDB_REPLY_DONE;
119 for (i = 0; ac->controls[i]; i++);
120 ares->controls = talloc_move(ares, &ac->controls);
125 ares->controls = talloc_realloc(ares, ares->controls, struct ldb_control *, i + 2);
127 if (ares->controls == NULL)
128 return LDB_ERR_OPERATIONS_ERROR;
130 ares->controls[i] = talloc(ares->controls, struct ldb_control);
131 if (ares->controls[i] == NULL)
132 return LDB_ERR_OPERATIONS_ERROR;
134 ares->controls[i]->oid = LDB_CONTROL_ASQ_OID;
135 ares->controls[i]->critical = 0;
137 asq = talloc_zero(ares->controls[i], struct ldb_asq_control);
139 return LDB_ERR_OPERATIONS_ERROR;
141 asq->result = ac->asq_ret;
143 ares->controls[i]->data = asq;
145 ares->controls[i + 1] = NULL;
147 ac->orig_req->callback(ac->module->ldb, ac->orig_req->context, ares);
152 static int asq_base_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
154 struct asq_context *ac;
156 if (!context || !ares) {
157 ldb_set_errstring(ldb, "NULL Context or Result in callback");
161 ac = talloc_get_type(context, struct asq_context);
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);
176 return LDB_ERR_OPERATIONS_ERROR;
179 static int asq_reqs_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
181 struct asq_context *ac;
183 if (!context || !ares) {
184 ldb_set_errstring(ldb, "NULL Context or Result in callback");
188 ac = talloc_get_type(context, struct asq_context);
193 /* we are interested only in the single reply (base search) we receive here */
194 if (ares->type == LDB_REPLY_ENTRY) {
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);
200 } else { /* ignore any REFERRAL or DONE reply */
207 return LDB_ERR_OPERATIONS_ERROR;
210 static int asq_build_first_request(struct asq_context *ac)
214 ac->base_req = talloc_zero(ac, struct ldb_request);
215 if (ac->base_req == NULL) return LDB_ERR_OPERATIONS_ERROR;
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;
224 base_attrs[0] = talloc_strdup(base_attrs, ac->asq_ctrl->source_attribute);
225 if (base_attrs[0] == NULL) return LDB_ERR_OPERATIONS_ERROR;
227 base_attrs[1] = NULL;
228 ac->base_req->op.search.attrs = (const char * const *)base_attrs;
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);
237 static int asq_build_multiple_requests(struct asq_context *ac, struct ldb_handle *handle)
239 struct ldb_message_element *el;
242 /* look up the DNs */
243 if (ac->base_res == NULL) {
244 return LDB_ERR_NO_SUCH_OBJECT;
246 el = ldb_msg_find_element(ac->base_res->message, ac->req_attribute);
247 /* no values found */
249 ac->asq_ret = ASQ_CTRL_SUCCESS;
250 return asq_terminate(handle);
253 ac->num_reqs = el->num_values;
255 ac->reqs = talloc_array(ac, struct ldb_request *, ac->num_reqs);
256 if (ac->reqs == NULL) {
257 return LDB_ERR_OPERATIONS_ERROR;
260 for (i = 0; i < el->num_values; i++) {
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);
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;
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]);
283 static int asq_search_continue(struct ldb_handle *handle)
285 struct asq_context *ac;
288 if (!handle || !handle->private_data) {
289 return LDB_ERR_OPERATIONS_ERROR;
292 if (handle->state == LDB_ASYNC_DONE) {
293 return handle->status;
296 ac = talloc_get_type(handle->private_data, struct asq_context);
298 return LDB_ERR_OPERATIONS_ERROR;
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);
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;
314 /* get the object to retrieve the DNs to search */
315 ret = asq_build_first_request(ac);
316 if (ret != LDB_SUCCESS) {
320 ac->step = ASQ_SEARCH_BASE;
322 handle->state = LDB_ASYNC_PENDING;
323 handle->status = LDB_SUCCESS;
325 return ldb_request(ac->module->ldb, ac->base_req);
327 case ASQ_SEARCH_BASE:
329 ret = ldb_wait(ac->base_req->handle, LDB_WAIT_NONE);
331 if (ret != LDB_SUCCESS) {
332 handle->status = ret;
336 if (ac->base_req->handle->status != LDB_SUCCESS) {
337 handle->status = ac->base_req->handle->status;
341 if (ac->base_req->handle->state == LDB_ASYNC_DONE) {
343 /* build up the requests call chain */
344 ret = asq_build_multiple_requests(ac, handle);
345 if (ret != LDB_SUCCESS) {
348 if (handle->state == LDB_ASYNC_DONE) {
352 ac->step = ASQ_SEARCH_MULTI;
354 return ldb_request(ac->module->ldb, ac->reqs[ac->cur_req]);
357 /* request still pending, return to cycle again */
360 case ASQ_SEARCH_MULTI:
362 ret = ldb_wait(ac->reqs[ac->cur_req]->handle, LDB_WAIT_NONE);
364 if (ret != LDB_SUCCESS) {
365 handle->status = ret;
368 if (ac->reqs[ac->cur_req]->handle->status != LDB_SUCCESS) {
369 handle->status = ac->reqs[ac->cur_req]->handle->status;
372 if (ac->reqs[ac->cur_req]->handle->state == LDB_ASYNC_DONE) {
375 if (ac->cur_req < ac->num_reqs) {
376 return ldb_request(ac->module->ldb, ac->reqs[ac->cur_req]);
379 return asq_terminate(handle);
382 /* request still pending, return to cycle again */
386 ret = LDB_ERR_OPERATIONS_ERROR;
391 handle->state = LDB_ASYNC_DONE;
395 static int asq_search(struct ldb_module *module, struct ldb_request *req)
397 struct ldb_control *control;
398 struct asq_context *ac;
399 struct ldb_handle *h;
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);
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;
416 h = init_handle(req, module);
418 return LDB_ERR_OPERATIONS_ERROR;
420 ac = talloc_get_type(h->private_data, struct asq_context);
422 ac->asq_ctrl = talloc_get_type(control->data, struct ldb_asq_control);
424 return LDB_ERR_PROTOCOL_ERROR;
429 return asq_search_continue(h);
432 static int asq_wait(struct ldb_handle *handle, enum ldb_wait_type type)
436 if (!handle || !handle->private_data) {
437 return LDB_ERR_OPERATIONS_ERROR;
440 if (type == LDB_WAIT_ALL) {
441 while (handle->state != LDB_ASYNC_DONE) {
442 ret = asq_search_continue(handle);
443 if (ret != LDB_SUCCESS) {
448 return handle->status;
451 return asq_search_continue(handle);
454 static int asq_init(struct ldb_module *module)
456 struct ldb_request *req;
459 req = talloc_zero(module, struct ldb_request);
461 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "asq: Out of memory!\n");
462 return LDB_ERR_OPERATIONS_ERROR;
465 req->operation = LDB_REQ_REGISTER_CONTROL;
466 req->op.reg_control.oid = LDB_CONTROL_ASQ_OID;
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");
473 return ldb_next_init(module);
477 static const struct ldb_module_ops asq_ops = {
479 .search = asq_search,
481 .init_context = asq_init
484 int ldb_asq_init(void)
486 return ldb_register_module(&asq_ops);