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 server side sort control module
30 * Description: this module sorts the results of a search
36 #include "ldb/include/includes.h"
39 struct ldb_context *ldb;
40 const struct ldb_attrib_handler *h;
41 const char *attribute;
46 struct sort_async_context {
47 struct ldb_module *module;
49 int (*up_callback)(struct ldb_context *, void *, struct ldb_async_result *);
56 struct ldb_request *req;
57 struct ldb_message **msgs;
59 struct ldb_control **controls;
63 const struct ldb_attrib_handler *h;
67 static struct ldb_async_handle *init_handle(void *mem_ctx, struct ldb_module *module,
69 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
72 struct sort_async_context *ac;
73 struct ldb_async_handle *h;
75 h = talloc_zero(mem_ctx, struct ldb_async_handle);
77 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
83 ac = talloc_zero(h, struct sort_async_context);
85 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
90 h->private_data = (void *)ac;
93 ac->up_context = context;
94 ac->up_callback = callback;
95 ac->timeout = timeout;
100 static int build_response(void *mem_ctx, struct ldb_control ***ctrls, int result, const char *desc)
102 struct ldb_control **controls;
103 struct ldb_sort_resp_control *resp;
108 for (i = 0; controls[i]; i++);
109 controls = talloc_realloc(mem_ctx, controls, struct ldb_control *, i + 2);
112 controls = talloc_array(mem_ctx, struct ldb_control *, 2);
115 return LDB_ERR_OPERATIONS_ERROR;
119 controls[i+1] = NULL;
120 controls[i] = talloc(controls, struct ldb_control);
122 return LDB_ERR_OPERATIONS_ERROR;
124 controls[i]->oid = LDB_CONTROL_SORT_RESP_OID;
125 controls[i]->critical = 0;
127 resp = talloc(controls[i], struct ldb_sort_resp_control);
129 return LDB_ERR_OPERATIONS_ERROR;
131 resp->result = result;
132 resp->attr_desc = talloc_strdup(resp, desc);
134 if (! resp->attr_desc )
135 return LDB_ERR_OPERATIONS_ERROR;
137 controls[i]->data = resp;
142 static int sort_compare(struct ldb_message **msg1, struct ldb_message **msg2, void *opaque)
144 struct opaque *data = (struct opaque *)opaque;
145 struct ldb_message_element *el1, *el2;
147 if (data->result != 0) {
148 /* an error occurred previously,
149 * let's exit the sorting by returning always 0 */
153 el1 = ldb_msg_find_element(*msg1, data->attribute);
154 el2 = ldb_msg_find_element(*msg2, data->attribute);
157 /* the attribute was not found return and
164 return data->h->comparison_fn(data->ldb, data, &el2->values[0], &el1->values[0]);
166 return data->h->comparison_fn(data->ldb, data, &el1->values[0], &el2->values[0]);
169 static int sort_compare_async(struct ldb_message **msg1, struct ldb_message **msg2, void *opaque)
171 struct sort_async_context *ac = talloc_get_type(opaque, struct sort_async_context);
172 struct ldb_message_element *el1, *el2;
174 if (ac->sort_result != 0) {
175 /* an error occurred previously,
176 * let's exit the sorting by returning always 0 */
180 el1 = ldb_msg_find_element(*msg1, ac->attributeName);
181 el2 = ldb_msg_find_element(*msg2, ac->attributeName);
184 /* the attribute was not found return and
186 ac->sort_result = 53;
191 return ac->h->comparison_fn(ac->module->ldb, ac, &el2->values[0], &el1->values[0]);
193 return ac->h->comparison_fn(ac->module->ldb, ac, &el1->values[0], &el2->values[0]);
197 static int server_sort_search(struct ldb_module *module, struct ldb_control *control, struct ldb_request *req)
199 struct ldb_result *sort_result = NULL;
200 struct ldb_control **saved_controls;
201 struct ldb_server_sort_control **sort_ctrls;
205 sort_ctrls = talloc_get_type(control->data, struct ldb_server_sort_control *);
207 return LDB_ERR_PROTOCOL_ERROR;
210 /* FIXME: we do not support more than one attribute for sorting right now */
211 /* FIXME: we need to check if the attribute type exist or return an error */
212 if (sort_ctrls[1] != NULL)
215 if (!do_sort && control->critical) {
216 sort_result = talloc_zero(req, struct ldb_result);
218 return LDB_ERR_OPERATIONS_ERROR;
220 req->op.search.res = sort_result;
222 /* 53 = unwilling to perform */
223 if ((ret = build_response(sort_result, &sort_result->controls, 53, "sort control is not complete yet")) != LDB_SUCCESS) {
227 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
230 /* save it locally and remove it from the list */
231 if (!save_controls(control, req, &saved_controls)) {
232 return LDB_ERR_OPERATIONS_ERROR;
235 ret = ldb_next_request(module, req);
237 if (req->controls) talloc_free(req->controls);
238 req->controls = saved_controls;
240 if (ret != LDB_SUCCESS) {
248 data = talloc(module, struct opaque);
250 return LDB_ERR_OPERATIONS_ERROR;
252 data->attribute = sort_ctrls[0]->attributeName;
253 data->reverse = sort_ctrls[0]->reverse;
254 data->ldb = module->ldb;
255 data->h = ldb_attrib_handler(data->ldb, data->attribute);
257 sort_result = req->op.search.res;
259 ldb_qsort(sort_result->msgs,
261 sizeof(struct ldb_message *),
263 (ldb_qsort_cmp_fn_t)sort_compare);
265 result = data->result;
272 if ((ret = build_response(sort_result, &sort_result->controls, result, "sort control is not complete yet")) != LDB_SUCCESS) {
279 static int server_sort_search_async_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
281 struct sort_async_context *ac = NULL;
283 if (!context || !ares) {
284 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
288 ac = talloc_get_type(context, struct sort_async_context);
290 if (ares->type == LDB_REPLY_ENTRY) {
291 ac->msgs = talloc_realloc(ac, ac->msgs, struct ldb_message *, ac->num_msgs + 2);
296 ac->msgs[ac->num_msgs + 1] = NULL;
298 ac->msgs[ac->num_msgs] = talloc_steal(ac->msgs, ares->message);
299 if (! ac->msgs[ac->num_msgs]) {
306 if (ares->type == LDB_REPLY_REFERRAL) {
307 ac->referrals = talloc_realloc(ac, ac->referrals, char *, ac->num_refs + 2);
308 if (! ac->referrals) {
312 ac->referrals[ac->num_refs + 1] = NULL;
314 ac->referrals[ac->num_refs] = talloc_steal(ac->referrals, ares->referral);
315 if (! ac->referrals[ac->num_refs]) {
322 if (ares->type == LDB_REPLY_DONE) {
323 if (ares->controls) {
324 ac->controls = talloc_steal(ac, ares->controls);
325 if (! ac->controls) {
336 return LDB_ERR_OPERATIONS_ERROR;
339 static int server_sort_search_async(struct ldb_module *module, struct ldb_control *control, struct ldb_request *req)
341 struct ldb_server_sort_control **sort_ctrls;
342 struct ldb_control **saved_controls;
343 struct sort_async_context *ac;
344 struct ldb_async_handle *h;
347 req->async.handle = NULL;
349 if (!req->async.callback || !req->async.context) {
350 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Async interface called with NULL callback function or NULL context"));
351 return LDB_ERR_OPERATIONS_ERROR;
354 h = init_handle(req, module, req->async.context, req->async.callback, req->async.timeout);
356 return LDB_ERR_OPERATIONS_ERROR;
358 ac = talloc_get_type(h->private_data, struct sort_async_context);
360 sort_ctrls = talloc_get_type(control->data, struct ldb_server_sort_control *);
362 return LDB_ERR_PROTOCOL_ERROR;
365 /* FIXME: we do not support more than one attribute for sorting right now */
366 /* FIXME: we need to check if the attribute type exist or return an error */
368 if (sort_ctrls[1] != NULL) {
369 if (control->critical) {
370 struct ldb_async_result *ares;
372 ares = talloc_zero(req, struct ldb_async_result);
374 return LDB_ERR_OPERATIONS_ERROR;
376 /* 53 = unwilling to perform */
377 ares->type = LDB_REPLY_DONE;
378 if ((ret = build_response(ares, &ares->controls, 53, "sort control is not complete yet")) != LDB_SUCCESS) {
382 h->status = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
383 h->state = LDB_ASYNC_DONE;
384 ret = ac->up_callback(module->ldb, ac->up_context, ares);
388 /* just pass the call down and don't do any sorting */
389 ldb_next_request(module, req);
393 ac->attributeName = sort_ctrls[0]->attributeName;
394 ac->orderingRule = sort_ctrls[0]->orderingRule;
395 ac->reverse = sort_ctrls[0]->reverse;
397 ac->req = talloc(req, struct ldb_request);
399 ac->req->operation = req->operation;
400 ac->req->op.search.base = req->op.search.base;
401 ac->req->op.search.scope = req->op.search.scope;
402 ac->req->op.search.tree = req->op.search.tree;
403 ac->req->op.search.attrs = req->op.search.attrs;
404 ac->req->controls = req->controls;
406 /* save it locally and remove it from the list */
407 /* we do not need to replace them later as we
408 * are keeping the original req intact */
409 if (!save_controls(control, ac->req, &saved_controls)) {
410 return LDB_ERR_OPERATIONS_ERROR;
413 ac->req->creds = req->creds;
415 ac->req->async.context = ac;
416 ac->req->async.callback = server_sort_search_async_callback;
417 ac->req->async.timeout = req->async.timeout;
419 req->async.handle = h;
421 return ldb_next_request(module, ac->req);
424 static int server_sort(struct ldb_module *module, struct ldb_request *req)
426 struct ldb_control *control;
428 /* check if there's a paged request control */
429 control = get_control_from_list(req->controls, LDB_CONTROL_SERVER_SORT_OID);
430 if (control == NULL) {
431 /* not found go on */
432 return ldb_next_request(module, req);
435 switch (req->operation) {
438 return server_sort_search(module, control, req);
440 case LDB_ASYNC_SEARCH:
441 return server_sort_search_async(module, control, req);
444 return LDB_ERR_PROTOCOL_ERROR;
449 static int server_sort_async_results(struct ldb_async_handle *handle)
451 struct sort_async_context *ac;
452 struct ldb_async_result *ares;
455 ac = talloc_get_type(handle->private_data, struct sort_async_context);
457 ac->h = ldb_attrib_handler(ac->module->ldb, ac->attributeName);
460 ldb_qsort(ac->msgs, ac->num_msgs,
461 sizeof(struct ldb_message *),
462 ac, (ldb_qsort_cmp_fn_t)sort_compare_async);
464 for (i = 0; i < ac->num_msgs; i++) {
465 ares = talloc_zero(ac, struct ldb_async_result);
467 handle->status = LDB_ERR_OPERATIONS_ERROR;
468 return handle->status;
471 ares->type = LDB_REPLY_ENTRY;
472 ares->message = talloc_steal(ares, ac->msgs[i]);
474 handle->status = ac->up_callback(ac->module->ldb, ac->up_context, ares);
475 if (handle->status != LDB_SUCCESS) {
476 return handle->status;
480 for (i = 0; i < ac->num_refs; i++) {
481 ares = talloc_zero(ac, struct ldb_async_result);
483 handle->status = LDB_ERR_OPERATIONS_ERROR;
484 return handle->status;
487 ares->type = LDB_REPLY_REFERRAL;
488 ares->referral = talloc_steal(ares, ac->referrals[i]);
490 handle->status = ac->up_callback(ac->module->ldb, ac->up_context, ares);
491 if (handle->status != LDB_SUCCESS) {
492 return handle->status;
496 ares = talloc_zero(ac, struct ldb_async_result);
498 handle->status = LDB_ERR_OPERATIONS_ERROR;
499 return handle->status;
502 ares->type = LDB_REPLY_DONE;
503 ares->controls = talloc_steal(ares, ac->controls);
505 handle->status = ac->up_callback(ac->module->ldb, ac->up_context, ares);
506 if (handle->status != LDB_SUCCESS) {
507 return handle->status;
510 if ((ret = build_response(ac, &ac->controls, ac->sort_result, "sort control is not complete yet")) != LDB_SUCCESS) {
517 static int server_sort_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type)
519 struct sort_async_context *ac;
522 if (!handle || !handle->private_data) {
523 return LDB_ERR_OPERATIONS_ERROR;
526 ac = talloc_get_type(handle->private_data, struct sort_async_context);
528 ret = ldb_async_wait(ac->req->async.handle, type);
530 if (ret != LDB_SUCCESS) {
531 handle->status = ret;
535 handle->state = ac->req->async.handle->state;
536 handle->status = ac->req->async.handle->status;
538 if (handle->status != LDB_SUCCESS) {
539 return handle->status;
542 if (handle->state == LDB_ASYNC_DONE) {
543 ret = server_sort_async_results(handle);
549 static int server_sort_init(struct ldb_module *module)
551 struct ldb_request request;
554 request.operation = LDB_REQ_REGISTER;
555 request.op.reg.oid = LDB_CONTROL_SERVER_SORT_OID;
556 request.controls = NULL;
558 ret = ldb_request(module->ldb, &request);
559 if (ret != LDB_SUCCESS) {
560 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "server_sort: Unable to register control with rootdse!\n");
561 return LDB_ERR_OTHER;
564 return ldb_next_init(module);
567 static const struct ldb_module_ops server_sort_ops = {
568 .name = "server_sort",
569 .request = server_sort,
570 .async_wait = server_sort_async_wait,
571 .init_context = server_sort_init
574 int ldb_sort_init(void)
576 return ldb_register_module(&server_sort_ops);