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 2 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
37 #include "ldb/include/includes.h"
41 enum {ASQ_SEARCH_BASE, ASQ_SEARCH_MULTI} step;
43 struct ldb_module *module;
45 int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
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(void *mem_ctx, struct ldb_module *module,
68 int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
70 struct asq_context *ac;
73 h = talloc_zero(mem_ctx, struct ldb_handle);
75 ldb_set_errstring(module->ldb, "Out of Memory");
81 ac = talloc_zero(h, struct asq_context);
83 ldb_set_errstring(module->ldb, "Out of Memory");
88 h->private_data = (void *)ac;
90 h->state = LDB_ASYNC_INIT;
91 h->status = LDB_SUCCESS;
94 ac->up_context = context;
95 ac->up_callback = callback;
100 static int asq_terminate(struct ldb_handle *handle)
102 struct asq_context *ac;
103 struct ldb_reply *ares;
104 struct ldb_asq_control *asq;
107 ac = talloc_get_type(handle->private_data, struct asq_context);
109 return LDB_ERR_OPERATIONS_ERROR;
112 handle->status = LDB_SUCCESS;
113 handle->state = LDB_ASYNC_DONE;
115 ares = talloc_zero(ac, struct ldb_reply);
117 return LDB_ERR_OPERATIONS_ERROR;
119 ares->type = LDB_REPLY_DONE;
122 for (i = 0; ac->controls[i]; i++);
123 ares->controls = talloc_move(ares, &ac->controls);
128 ares->controls = talloc_realloc(ares, ares->controls, struct ldb_control *, i + 2);
130 if (ares->controls == NULL)
131 return LDB_ERR_OPERATIONS_ERROR;
133 ares->controls[i] = talloc(ares->controls, struct ldb_control);
134 if (ares->controls[i] == NULL)
135 return LDB_ERR_OPERATIONS_ERROR;
137 ares->controls[i]->oid = LDB_CONTROL_ASQ_OID;
138 ares->controls[i]->critical = 0;
140 asq = talloc_zero(ares->controls[i], struct ldb_asq_control);
142 return LDB_ERR_OPERATIONS_ERROR;
144 asq->result = ac->asq_ret;
146 ares->controls[i]->data = asq;
148 ares->controls[i + 1] = NULL;
150 ac->up_callback(ac->module->ldb, ac->up_context, ares);
155 static int asq_base_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
157 struct asq_context *ac;
159 if (!context || !ares) {
160 ldb_set_errstring(ldb, "NULL Context or Result in callback");
164 ac = talloc_get_type(context, struct asq_context);
169 /* we are interested only in the single reply (base search) we receive here */
170 if (ares->type == LDB_REPLY_ENTRY) {
171 ac->base_res = talloc_move(ac, &ares);
179 return LDB_ERR_OPERATIONS_ERROR;
182 static int asq_reqs_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
184 struct asq_context *ac;
186 if (!context || !ares) {
187 ldb_set_errstring(ldb, "NULL Context or Result in callback");
191 ac = talloc_get_type(context, struct asq_context);
196 /* we are interested only in the single reply (base search) we receive here */
197 if (ares->type == LDB_REPLY_ENTRY) {
199 /* pass the message up to the original callback as we
200 * do not have to elaborate on it any further */
201 return ac->up_callback(ac->module->ldb, ac->up_context, ares);
203 } else { /* ignore any REFERRAL or DONE reply */
210 return LDB_ERR_OPERATIONS_ERROR;
213 static int asq_search(struct ldb_module *module, struct ldb_request *req)
215 struct ldb_control *control;
216 struct ldb_asq_control *asq_ctrl;
217 struct asq_context *ac;
218 struct ldb_handle *h;
222 /* check if there's a paged request control */
223 control = get_control_from_list(req->controls, LDB_CONTROL_ASQ_OID);
224 if (control == NULL) {
225 /* not found go on */
226 return ldb_next_request(module, req);
231 if (!req->callback || !req->context) {
232 ldb_set_errstring(module->ldb,
233 "Async interface called with NULL callback function or NULL context");
234 return LDB_ERR_OPERATIONS_ERROR;
237 asq_ctrl = talloc_get_type(control->data, struct ldb_asq_control);
239 return LDB_ERR_PROTOCOL_ERROR;
242 h = init_handle(req, module, req->context, req->callback);
244 return LDB_ERR_OPERATIONS_ERROR;
246 ac = talloc_get_type(h->private_data, struct asq_context);
250 /* check the search is well formed */
251 if (req->op.search.scope != LDB_SCOPE_BASE) {
252 ac->asq_ret = ASQ_CTRL_UNWILLING_TO_PERFORM;
253 return asq_terminate(h);
256 ac->req_attrs = req->op.search.attrs;
257 ac->req_attribute = talloc_strdup(ac, asq_ctrl->source_attribute);
258 if (ac->req_attribute == NULL)
259 return LDB_ERR_OPERATIONS_ERROR;
261 /* get the object to retrieve the DNs to search */
262 ac->base_req = talloc_zero(req, struct ldb_request);
263 if (ac->base_req == NULL)
264 return LDB_ERR_OPERATIONS_ERROR;
265 ac->base_req->operation = req->operation;
266 ac->base_req->op.search.base = req->op.search.base;
267 ac->base_req->op.search.scope = LDB_SCOPE_BASE;
268 ac->base_req->op.search.tree = req->op.search.tree;
269 base_attrs = talloc_array(ac->base_req, char *, 2);
270 if (base_attrs == NULL)
271 return LDB_ERR_OPERATIONS_ERROR;
272 base_attrs[0] = talloc_strdup(base_attrs, asq_ctrl->source_attribute);
273 if (base_attrs[0] == NULL)
274 return LDB_ERR_OPERATIONS_ERROR;
275 base_attrs[1] = NULL;
276 ac->base_req->op.search.attrs = (const char * const *)base_attrs;
278 ac->base_req->context = ac;
279 ac->base_req->callback = asq_base_callback;
280 ldb_set_timeout_from_prev_req(module->ldb, req, ac->base_req);
282 ac->step = ASQ_SEARCH_BASE;
284 ret = ldb_request(module->ldb, ac->base_req);
286 if (ret != LDB_SUCCESS) {
293 static int asq_requests(struct ldb_handle *handle) {
294 struct asq_context *ac;
295 struct ldb_message_element *el;
298 ac = talloc_get_type(handle->private_data, struct asq_context);
300 return LDB_ERR_OPERATIONS_ERROR;
303 /* look up the DNs */
304 if (ac->base_res == NULL) {
305 return LDB_ERR_NO_SUCH_OBJECT;
307 el = ldb_msg_find_element(ac->base_res->message, ac->req_attribute);
308 /* no values found */
310 ac->asq_ret = ASQ_CTRL_SUCCESS;
311 return asq_terminate(handle);
314 /* build up the requests call chain */
315 ac->num_reqs = el->num_values;
317 ac->reqs = talloc_array(ac, struct ldb_request *, ac->num_reqs);
318 if (ac->reqs == NULL) {
319 return LDB_ERR_OPERATIONS_ERROR;
322 for (i = 0; i < el->num_values; i++) {
324 ac->reqs[i] = talloc_zero(ac->reqs, struct ldb_request);
325 if (ac->reqs[i] == NULL)
326 return LDB_ERR_OPERATIONS_ERROR;
327 ac->reqs[i]->operation = LDB_SEARCH;
328 ac->reqs[i]->op.search.base = ldb_dn_new(ac->reqs[i], ac->module->ldb, (const char *)el->values[i].data);
329 if ( ! ldb_dn_validate(ac->reqs[i]->op.search.base)) {
330 ac->asq_ret = ASQ_CTRL_INVALID_ATTRIBUTE_SYNTAX;
331 return asq_terminate(handle);
333 ac->reqs[i]->op.search.scope = LDB_SCOPE_BASE;
334 ac->reqs[i]->op.search.tree = ac->base_req->op.search.tree;
335 ac->reqs[i]->op.search.attrs = ac->req_attrs;
337 ac->reqs[i]->context = ac;
338 ac->reqs[i]->callback = asq_reqs_callback;
339 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->base_req, ac->reqs[i]);
342 ac->step = ASQ_SEARCH_MULTI;
347 static int asq_wait_none(struct ldb_handle *handle)
349 struct asq_context *ac;
352 if (!handle || !handle->private_data) {
353 return LDB_ERR_OPERATIONS_ERROR;
356 if (handle->state == LDB_ASYNC_DONE) {
357 return handle->status;
360 handle->state = LDB_ASYNC_PENDING;
361 handle->status = LDB_SUCCESS;
363 ac = talloc_get_type(handle->private_data, struct asq_context);
365 return LDB_ERR_OPERATIONS_ERROR;
369 case ASQ_SEARCH_BASE:
370 ret = ldb_wait(ac->base_req->handle, LDB_WAIT_NONE);
372 if (ret != LDB_SUCCESS) {
373 handle->status = ret;
377 if (ac->base_req->handle->status != LDB_SUCCESS) {
378 handle->status = ac->base_req->handle->status;
381 if (ac->base_req->handle->state != LDB_ASYNC_DONE) {
385 ret = asq_requests(handle);
387 /* no break nor return,
388 * the set of requests is performed in ASQ_SEARCH_MULTI
391 case ASQ_SEARCH_MULTI:
393 if (ac->reqs[ac->cur_req]->handle == NULL) {
394 ret = ldb_request(ac->module->ldb, ac->reqs[ac->cur_req]);
395 if (ret != LDB_SUCCESS) {
400 ret = ldb_wait(ac->reqs[ac->cur_req]->handle, LDB_WAIT_NONE);
402 if (ret != LDB_SUCCESS) {
403 handle->status = ret;
406 if (ac->reqs[ac->cur_req]->handle->status != LDB_SUCCESS) {
407 handle->status = ac->reqs[ac->cur_req]->handle->status;
410 if (ac->reqs[ac->cur_req]->handle->state == LDB_ASYNC_DONE) {
414 if (ac->cur_req < ac->num_reqs) {
418 return asq_terminate(handle);
421 ret = LDB_ERR_OPERATIONS_ERROR;
428 handle->state = LDB_ASYNC_DONE;
432 static int asq_wait_all(struct ldb_handle *handle)
436 while (handle->state != LDB_ASYNC_DONE) {
437 ret = asq_wait_none(handle);
438 if (ret != LDB_SUCCESS) {
443 return handle->status;
446 static int asq_wait(struct ldb_handle *handle, enum ldb_wait_type type)
448 if (type == LDB_WAIT_ALL) {
449 return asq_wait_all(handle);
451 return asq_wait_none(handle);
455 static int asq_init(struct ldb_module *module)
457 struct ldb_request *req;
460 req = talloc_zero(module, struct ldb_request);
462 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "asq: Out of memory!\n");
463 return LDB_ERR_OPERATIONS_ERROR;
466 req->operation = LDB_REQ_REGISTER_CONTROL;
467 req->op.reg_control.oid = LDB_CONTROL_ASQ_OID;
469 ret = ldb_request(module->ldb, req);
470 if (ret != LDB_SUCCESS) {
471 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "asq: Unable to register control with rootdse!\n");
474 return ldb_next_init(module);
478 static const struct ldb_module_ops asq_ops = {
480 .search = asq_search,
482 .init_context = asq_init
485 int ldb_asq_init(void)
487 return ldb_register_module(&asq_ops);