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 server side sort control module
29 * Description: this module sorts the results of a search
34 #include "ldb_includes.h"
37 struct ldb_context *ldb;
38 const struct ldb_attrib_handler *h;
39 const char *attribute;
45 struct ldb_module *module;
47 int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
53 struct ldb_request *req;
54 struct ldb_message **msgs;
56 struct ldb_control **controls;
60 const struct ldb_schema_attribute *a;
64 static struct ldb_handle *init_handle(void *mem_ctx, struct ldb_module *module,
66 int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
68 struct sort_context *ac;
71 h = talloc_zero(mem_ctx, struct ldb_handle);
73 ldb_set_errstring(module->ldb, "Out of Memory");
79 ac = talloc_zero(h, struct sort_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;
92 ac->up_context = context;
93 ac->up_callback = callback;
98 static int build_response(void *mem_ctx, struct ldb_control ***ctrls, int result, const char *desc)
100 struct ldb_control **controls;
101 struct ldb_sort_resp_control *resp;
106 for (i = 0; controls[i]; i++);
107 controls = talloc_realloc(mem_ctx, controls, struct ldb_control *, i + 2);
110 controls = talloc_array(mem_ctx, struct ldb_control *, 2);
113 return LDB_ERR_OPERATIONS_ERROR;
117 controls[i+1] = NULL;
118 controls[i] = talloc(controls, struct ldb_control);
120 return LDB_ERR_OPERATIONS_ERROR;
122 controls[i]->oid = LDB_CONTROL_SORT_RESP_OID;
123 controls[i]->critical = 0;
125 resp = talloc(controls[i], struct ldb_sort_resp_control);
127 return LDB_ERR_OPERATIONS_ERROR;
129 resp->result = result;
130 resp->attr_desc = talloc_strdup(resp, desc);
132 if (! resp->attr_desc )
133 return LDB_ERR_OPERATIONS_ERROR;
135 controls[i]->data = resp;
140 static int sort_compare(struct ldb_message **msg1, struct ldb_message **msg2, void *opaque)
142 struct sort_context *ac = talloc_get_type(opaque, struct sort_context);
143 struct ldb_message_element *el1, *el2;
145 if (ac->sort_result != 0) {
146 /* an error occurred previously,
147 * let's exit the sorting by returning always 0 */
151 el1 = ldb_msg_find_element(*msg1, ac->attributeName);
152 el2 = ldb_msg_find_element(*msg2, ac->attributeName);
155 /* the attribute was not found return and
157 ac->sort_result = 53;
162 return ac->a->syntax->comparison_fn(ac->module->ldb, ac, &el2->values[0], &el1->values[0]);
164 return ac->a->syntax->comparison_fn(ac->module->ldb, ac, &el1->values[0], &el2->values[0]);
167 static int server_sort_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
169 struct sort_context *ac = NULL;
171 if (!context || !ares) {
172 ldb_set_errstring(ldb, "NULL Context or Result in callback");
176 ac = talloc_get_type(context, struct sort_context);
178 if (ares->type == LDB_REPLY_ENTRY) {
179 ac->msgs = talloc_realloc(ac, ac->msgs, struct ldb_message *, ac->num_msgs + 2);
184 ac->msgs[ac->num_msgs + 1] = NULL;
186 ac->msgs[ac->num_msgs] = talloc_move(ac->msgs, &ares->message);
190 if (ares->type == LDB_REPLY_REFERRAL) {
191 ac->referrals = talloc_realloc(ac, ac->referrals, char *, ac->num_refs + 2);
192 if (! ac->referrals) {
196 ac->referrals[ac->num_refs + 1] = NULL;
197 ac->referrals[ac->num_refs] = talloc_move(ac->referrals, &ares->referral);
202 if (ares->type == LDB_REPLY_DONE) {
203 ac->controls = talloc_move(ac, &ares->controls);
211 return LDB_ERR_OPERATIONS_ERROR;
214 static int server_sort_search(struct ldb_module *module, struct ldb_request *req)
216 struct ldb_control *control;
217 struct ldb_server_sort_control **sort_ctrls;
218 struct ldb_control **saved_controls;
219 struct sort_context *ac;
220 struct ldb_handle *h;
223 /* check if there's a paged request control */
224 control = ldb_request_get_control(req, LDB_CONTROL_SERVER_SORT_OID);
225 if (control == NULL) {
226 /* not found go on */
227 return ldb_next_request(module, req);
232 if (!req->callback || !req->context) {
233 ldb_set_errstring(module->ldb,
234 "Async interface called with NULL callback function or NULL context");
235 return LDB_ERR_OPERATIONS_ERROR;
238 h = init_handle(req, module, req->context, req->callback);
240 return LDB_ERR_OPERATIONS_ERROR;
242 ac = talloc_get_type(h->private_data, struct sort_context);
244 sort_ctrls = talloc_get_type(control->data, struct ldb_server_sort_control *);
246 return LDB_ERR_PROTOCOL_ERROR;
249 /* FIXME: we do not support more than one attribute for sorting right now */
250 /* FIXME: we need to check if the attribute type exist or return an error */
252 if (sort_ctrls[1] != NULL) {
253 if (control->critical) {
254 struct ldb_reply *ares;
256 ares = talloc_zero(req, struct ldb_reply);
258 return LDB_ERR_OPERATIONS_ERROR;
260 /* 53 = unwilling to perform */
261 ares->type = LDB_REPLY_DONE;
262 if ((ret = build_response(ares, &ares->controls, 53, "sort control is not complete yet")) != LDB_SUCCESS) {
266 h->status = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
267 h->state = LDB_ASYNC_DONE;
268 ret = ac->up_callback(module->ldb, ac->up_context, ares);
272 /* just pass the call down and don't do any sorting */
273 ldb_next_request(module, req);
277 ac->attributeName = sort_ctrls[0]->attributeName;
278 ac->orderingRule = sort_ctrls[0]->orderingRule;
279 ac->reverse = sort_ctrls[0]->reverse;
281 ac->req = talloc(req, struct ldb_request);
283 return LDB_ERR_OPERATIONS_ERROR;
285 ac->req->operation = req->operation;
286 ac->req->op.search.base = req->op.search.base;
287 ac->req->op.search.scope = req->op.search.scope;
288 ac->req->op.search.tree = req->op.search.tree;
289 ac->req->op.search.attrs = req->op.search.attrs;
290 ac->req->controls = req->controls;
292 /* save it locally and remove it from the list */
293 /* we do not need to replace them later as we
294 * are keeping the original req intact */
295 if (!save_controls(control, ac->req, &saved_controls)) {
296 return LDB_ERR_OPERATIONS_ERROR;
299 ac->req->context = ac;
300 ac->req->callback = server_sort_search_callback;
301 ldb_set_timeout_from_prev_req(module->ldb, req, ac->req);
305 return ldb_next_request(module, ac->req);
308 static int server_sort_results(struct ldb_handle *handle)
310 struct sort_context *ac;
311 struct ldb_reply *ares;
314 ac = talloc_get_type(handle->private_data, struct sort_context);
316 ac->a = ldb_schema_attribute_by_name(ac->module->ldb, ac->attributeName);
319 ldb_qsort(ac->msgs, ac->num_msgs,
320 sizeof(struct ldb_message *),
321 ac, (ldb_qsort_cmp_fn_t)sort_compare);
323 for (i = 0; i < ac->num_msgs; i++) {
324 ares = talloc_zero(ac, struct ldb_reply);
326 handle->status = LDB_ERR_OPERATIONS_ERROR;
327 return handle->status;
330 ares->type = LDB_REPLY_ENTRY;
331 ares->message = talloc_move(ares, &ac->msgs[i]);
333 handle->status = ac->up_callback(ac->module->ldb, ac->up_context, ares);
334 if (handle->status != LDB_SUCCESS) {
335 return handle->status;
339 for (i = 0; i < ac->num_refs; i++) {
340 ares = talloc_zero(ac, struct ldb_reply);
342 handle->status = LDB_ERR_OPERATIONS_ERROR;
343 return handle->status;
346 ares->type = LDB_REPLY_REFERRAL;
347 ares->referral = talloc_move(ares, &ac->referrals[i]);
349 handle->status = ac->up_callback(ac->module->ldb, ac->up_context, ares);
350 if (handle->status != LDB_SUCCESS) {
351 return handle->status;
355 ares = talloc_zero(ac, struct ldb_reply);
357 handle->status = LDB_ERR_OPERATIONS_ERROR;
358 return handle->status;
361 ares->type = LDB_REPLY_DONE;
362 ares->controls = talloc_move(ares, &ac->controls);
364 handle->status = ac->up_callback(ac->module->ldb, ac->up_context, ares);
365 if (handle->status != LDB_SUCCESS) {
366 return handle->status;
369 if ((ret = build_response(ac, &ac->controls, ac->sort_result, "sort control is not complete yet")) != LDB_SUCCESS) {
376 static int server_sort_wait_once(struct ldb_handle *handle)
378 struct sort_context *ac;
381 if (!handle || !handle->private_data) {
382 return LDB_ERR_OPERATIONS_ERROR;
385 ac = talloc_get_type(handle->private_data, struct sort_context);
387 ret = ldb_wait(ac->req->handle, LDB_WAIT_NONE);
389 if (ret != LDB_SUCCESS) {
390 handle->status = ret;
394 handle->state = ac->req->handle->state;
395 handle->status = ac->req->handle->status;
397 if (handle->status != LDB_SUCCESS) {
398 return handle->status;
401 if (handle->state == LDB_ASYNC_DONE) {
402 ret = server_sort_results(handle);
408 static int server_sort_wait(struct ldb_handle *handle, enum ldb_wait_type type)
412 if (!handle || !handle->private_data) {
413 return LDB_ERR_OPERATIONS_ERROR;
416 if (type == LDB_WAIT_ALL) {
417 while (handle->state != LDB_ASYNC_DONE) {
418 ret = server_sort_wait_once(handle);
419 if (ret != LDB_SUCCESS) {
424 return handle->status;
427 return server_sort_wait_once(handle);
430 static int server_sort_init(struct ldb_module *module)
432 struct ldb_request *req;
435 req = talloc(module, struct ldb_request);
437 return LDB_ERR_OPERATIONS_ERROR;
440 req->operation = LDB_REQ_REGISTER_CONTROL;
441 req->op.reg_control.oid = LDB_CONTROL_SERVER_SORT_OID;
442 req->controls = NULL;
444 ret = ldb_request(module->ldb, req);
445 if (ret != LDB_SUCCESS) {
446 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "server_sort: Unable to register control with rootdse!\n");
450 return ldb_next_init(module);
453 const struct ldb_module_ops ldb_server_sort_module_ops = {
454 .name = "server_sort",
455 .search = server_sort_search,
456 .wait = server_sort_wait,
457 .init_context = server_sort_init