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 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->up_callback(ac->module->ldb, ac->up_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);
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);
173 return LDB_ERR_OPERATIONS_ERROR;
176 static int asq_reqs_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
178 struct asq_context *ac;
180 if (!context || !ares) {
181 ldb_set_errstring(ldb, "NULL Context or Result in callback");
185 ac = talloc_get_type(context, struct asq_context);
187 /* we are interested only in the single reply (base search) we receive here */
188 if (ares->type == LDB_REPLY_ENTRY) {
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);
194 } else { /* ignore any REFERRAL or DONE reply */
201 return LDB_ERR_OPERATIONS_ERROR;
204 static int asq_search(struct ldb_module *module, struct ldb_request *req)
206 struct ldb_control *control;
207 struct ldb_asq_control *asq_ctrl;
208 struct asq_context *ac;
209 struct ldb_handle *h;
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);
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;
228 asq_ctrl = talloc_get_type(control->data, struct ldb_asq_control);
230 return LDB_ERR_PROTOCOL_ERROR;
233 h = init_handle(req, module, req->context, req->callback);
235 return LDB_ERR_OPERATIONS_ERROR;
237 ac = talloc_get_type(h->private_data, struct asq_context);
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);
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;
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;
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);
273 ac->step = ASQ_SEARCH_BASE;
275 ret = ldb_request(module->ldb, ac->base_req);
277 if (ret != LDB_SUCCESS) {
284 static int asq_requests(struct ldb_handle *handle) {
285 struct asq_context *ac;
286 struct ldb_message_element *el;
289 ac = talloc_get_type(handle->private_data, struct asq_context);
291 /* look up the DNs */
292 if (ac->base_res == NULL) {
293 return LDB_ERR_NO_SUCH_OBJECT;
295 el = ldb_msg_find_element(ac->base_res->message, ac->req_attribute);
296 /* no values found */
298 ac->asq_ret = ASQ_CTRL_SUCCESS;
299 return asq_terminate(handle);
302 /* build up the requests call chain */
303 ac->num_reqs = el->num_values;
305 ac->reqs = talloc_array(ac, struct ldb_request *, ac->num_reqs);
306 if (ac->reqs == NULL) {
307 return LDB_ERR_OPERATIONS_ERROR;
310 for (i = 0; i < el->num_values; i++) {
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);
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;
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]);
330 ac->step = ASQ_SEARCH_MULTI;
335 static int asq_wait_none(struct ldb_handle *handle)
337 struct asq_context *ac;
340 if (!handle || !handle->private_data) {
341 return LDB_ERR_OPERATIONS_ERROR;
344 if (handle->state == LDB_ASYNC_DONE) {
345 return handle->status;
348 handle->state = LDB_ASYNC_PENDING;
349 handle->status = LDB_SUCCESS;
351 ac = talloc_get_type(handle->private_data, struct asq_context);
355 case ASQ_SEARCH_BASE:
356 ret = ldb_wait(ac->base_req->handle, LDB_WAIT_NONE);
358 if (ret != LDB_SUCCESS) {
359 handle->status = ret;
363 if (ac->base_req->handle->status != LDB_SUCCESS) {
364 handle->status = ac->base_req->handle->status;
367 if (ac->base_req->handle->state != LDB_ASYNC_DONE) {
371 ret = asq_requests(handle);
373 /* no break nor return,
374 * the set of requests is performed in ASQ_SEARCH_MULTI
377 case ASQ_SEARCH_MULTI:
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) {
386 ret = ldb_wait(ac->reqs[ac->cur_req]->handle, LDB_WAIT_NONE);
388 if (ret != LDB_SUCCESS) {
389 handle->status = ret;
392 if (ac->reqs[ac->cur_req]->handle->status != LDB_SUCCESS) {
393 handle->status = ac->reqs[ac->cur_req]->handle->status;
396 if (ac->reqs[ac->cur_req]->handle->state == LDB_ASYNC_DONE) {
400 if (ac->cur_req < ac->num_reqs) {
404 return asq_terminate(handle);
407 ret = LDB_ERR_OPERATIONS_ERROR;
414 handle->state = LDB_ASYNC_DONE;
418 static int asq_wait_all(struct ldb_handle *handle)
422 while (handle->state != LDB_ASYNC_DONE) {
423 ret = asq_wait_none(handle);
424 if (ret != LDB_SUCCESS) {
429 return handle->status;
432 static int asq_wait(struct ldb_handle *handle, enum ldb_wait_type type)
434 if (type == LDB_WAIT_ALL) {
435 return asq_wait_all(handle);
437 return asq_wait_none(handle);
441 static int asq_init(struct ldb_module *module)
443 struct ldb_request *req;
446 req = talloc_zero(module, struct ldb_request);
448 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "asq: Out of memory!\n");
449 return LDB_ERR_OPERATIONS_ERROR;
452 req->operation = LDB_REQ_REGISTER_CONTROL;
453 req->op.reg_control.oid = LDB_CONTROL_ASQ_OID;
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");
460 return ldb_next_init(module);
464 static const struct ldb_module_ops asq_ops = {
466 .search = asq_search,
468 .init_context = asq_init
471 int ldb_asq_init(void)
473 return ldb_register_module(&asq_ops);