s3-build: fix the build of vfs_notify_fam.
[kai/samba-autobuild/.git] / source3 / lib / ldb / modules / sort.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2005
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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.
14
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.
19
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/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldb server side sort control module
28  *
29  *  Description: this module sorts the results of a search
30  *
31  *  Author: Simo Sorce
32  */
33
34 #include "includes.h"
35 #include "ldb/include/includes.h"
36
37 struct opaque {
38         struct ldb_context *ldb;
39         const struct ldb_attrib_handler *h;
40         const char *attribute;
41         int reverse;
42         int result;
43 };
44
45 struct sort_context {
46         struct ldb_module *module;
47         void *up_context;
48         int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
49
50         char *attributeName;
51         char *orderingRule;
52         int reverse;
53
54         struct ldb_request *req;
55         struct ldb_message **msgs;
56         char **referrals;
57         struct ldb_control **controls;
58         int num_msgs;
59         int num_refs;
60
61         const struct ldb_attrib_handler *h;
62         int sort_result;
63 };
64
65 static struct ldb_handle *init_handle(void *mem_ctx, struct ldb_module *module,
66                                             void *context,
67                                             int (*callback)(struct ldb_context *, void *, struct ldb_reply *))
68 {
69         struct sort_context *ac;
70         struct ldb_handle *h;
71
72         h = talloc_zero(mem_ctx, struct ldb_handle);
73         if (h == NULL) {
74                 ldb_set_errstring(module->ldb, "Out of Memory");
75                 return NULL;
76         }
77
78         h->module = module;
79
80         ac = talloc_zero(h, struct sort_context);
81         if (ac == NULL) {
82                 ldb_set_errstring(module->ldb, "Out of Memory");
83                 talloc_free(h);
84                 return NULL;
85         }
86
87         h->private_data = (void *)ac;
88
89         h->state = LDB_ASYNC_INIT;
90         h->status = LDB_SUCCESS;
91
92         ac->module = module;
93         ac->up_context = context;
94         ac->up_callback = callback;
95
96         return h;
97 }
98
99 static int build_response(void *mem_ctx, struct ldb_control ***ctrls, int result, const char *desc)
100 {
101         struct ldb_control **controls;
102         struct ldb_sort_resp_control *resp;
103         int i;
104
105         if (*ctrls) {
106                 controls = *ctrls;
107                 for (i = 0; controls[i]; i++);
108                 controls = talloc_realloc(mem_ctx, controls, struct ldb_control *, i + 2);
109         } else {
110                 i = 0;
111                 controls = talloc_array(mem_ctx, struct ldb_control *, 2);
112         }
113         if (! controls )
114                 return LDB_ERR_OPERATIONS_ERROR;
115
116         *ctrls = controls;
117
118         controls[i+1] = NULL;
119         controls[i] = talloc(controls, struct ldb_control);
120         if (! controls[i] )
121                 return LDB_ERR_OPERATIONS_ERROR;
122
123         controls[i]->oid = LDB_CONTROL_SORT_RESP_OID;
124         controls[i]->critical = 0;
125
126         resp = talloc(controls[i], struct ldb_sort_resp_control);
127         if (! resp )
128                 return LDB_ERR_OPERATIONS_ERROR;
129
130         resp->result = result;
131         resp->attr_desc = talloc_strdup(resp, desc);
132
133         if (! resp->attr_desc )
134                 return LDB_ERR_OPERATIONS_ERROR;
135         
136         controls[i]->data = resp;
137
138         return LDB_SUCCESS;
139 }
140
141 static int sort_compare(struct ldb_message **msg1, struct ldb_message **msg2, void *opaque)
142 {
143         struct sort_context *ac = talloc_get_type(opaque, struct sort_context);
144         struct ldb_message_element *el1, *el2;
145
146         if (ac->sort_result != 0) {
147                 /* an error occurred previously,
148                  * let's exit the sorting by returning always 0 */
149                 return 0;
150         }
151
152         el1 = ldb_msg_find_element(*msg1, ac->attributeName);
153         el2 = ldb_msg_find_element(*msg2, ac->attributeName);
154
155         if (!el1 || !el2) {
156                 /* the attribute was not found return and
157                  * set an error */
158                 ac->sort_result = 53;
159                 return 0;
160         }
161
162         if (ac->reverse)
163                 return ac->h->comparison_fn(ac->module->ldb, ac, &el2->values[0], &el1->values[0]);
164
165         return ac->h->comparison_fn(ac->module->ldb, ac, &el1->values[0], &el2->values[0]);
166 }
167
168 static int server_sort_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
169 {
170         struct sort_context *ac = NULL;
171         
172         if (!context || !ares) {
173                 ldb_set_errstring(ldb, "NULL Context or Result in callback");
174                 goto error;
175         }       
176
177         ac = talloc_get_type(context, struct sort_context);
178
179         if (ares->type == LDB_REPLY_ENTRY) {
180                 ac->msgs = talloc_realloc(ac, ac->msgs, struct ldb_message *, ac->num_msgs + 2);
181                 if (! ac->msgs) {
182                         goto error;
183                 }
184
185                 ac->msgs[ac->num_msgs + 1] = NULL;
186
187                 ac->msgs[ac->num_msgs] = talloc_move(ac->msgs, &ares->message);
188                 ac->num_msgs++;
189         }
190
191         if (ares->type == LDB_REPLY_REFERRAL) {
192                 ac->referrals = talloc_realloc(ac, ac->referrals, char *, ac->num_refs + 2);
193                 if (! ac->referrals) {
194                         goto error;
195                 }
196
197                 ac->referrals[ac->num_refs + 1] = NULL;
198                 ac->referrals[ac->num_refs] = talloc_move(ac->referrals, &ares->referral);
199
200                 ac->num_refs++;
201         }
202
203         if (ares->type == LDB_REPLY_DONE) {
204                 ac->controls = talloc_move(ac, &ares->controls);
205         }
206
207         talloc_free(ares);
208         return LDB_SUCCESS;
209
210 error:
211         talloc_free(ares);
212         return LDB_ERR_OPERATIONS_ERROR;
213 }
214
215 static int server_sort_search(struct ldb_module *module, struct ldb_request *req)
216 {
217         struct ldb_control *control;
218         struct ldb_server_sort_control **sort_ctrls;
219         struct ldb_control **saved_controls;
220         struct sort_context *ac;
221         struct ldb_handle *h;
222         int ret;
223
224         /* check if there's a paged request control */
225         control = get_control_from_list(req->controls, LDB_CONTROL_SERVER_SORT_OID);
226         if (control == NULL) {
227                 /* not found go on */
228                 return ldb_next_request(module, req);
229         }
230
231         req->handle = NULL;
232
233         if (!req->callback || !req->context) {
234                 ldb_set_errstring(module->ldb,
235                                   "Async interface called with NULL callback function or NULL context");
236                 return LDB_ERR_OPERATIONS_ERROR;
237         }
238         
239         h = init_handle(req, module, req->context, req->callback);
240         if (!h) {
241                 return LDB_ERR_OPERATIONS_ERROR;
242         }
243         ac = talloc_get_type(h->private_data, struct sort_context);
244
245         sort_ctrls = talloc_get_type(control->data, struct ldb_server_sort_control *);
246         if (!sort_ctrls) {
247                 return LDB_ERR_PROTOCOL_ERROR;
248         }
249
250         /* FIXME: we do not support more than one attribute for sorting right now */
251         /* FIXME: we need to check if the attribute type exist or return an error */
252                 
253         if (sort_ctrls[1] != NULL) {
254                 if (control->critical) {
255                         struct ldb_reply *ares;
256
257                         ares = talloc_zero(req, struct ldb_reply);
258                         if (!ares)
259                                 return LDB_ERR_OPERATIONS_ERROR;
260
261                         /* 53 = unwilling to perform */
262                         ares->type = LDB_REPLY_DONE;
263                         if ((ret = build_response(ares, &ares->controls, 53, "sort control is not complete yet")) != LDB_SUCCESS) {
264                                 return ret;
265                         }
266
267                         h->status = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
268                         h->state = LDB_ASYNC_DONE;
269                         ret = ac->up_callback(module->ldb, ac->up_context, ares);
270
271                         return ret;
272                 } else {
273                         /* just pass the call down and don't do any sorting */
274                         ldb_next_request(module, req);
275                 }
276         }
277
278         ac->attributeName = sort_ctrls[0]->attributeName;
279         ac->orderingRule = sort_ctrls[0]->orderingRule;
280         ac->reverse = sort_ctrls[0]->reverse;
281
282         ac->req = talloc(req, struct ldb_request);
283         if (!ac->req)
284                 return LDB_ERR_OPERATIONS_ERROR;
285
286         ac->req->operation = req->operation;
287         ac->req->op.search.base = req->op.search.base;
288         ac->req->op.search.scope = req->op.search.scope;
289         ac->req->op.search.tree = req->op.search.tree;
290         ac->req->op.search.attrs = req->op.search.attrs;
291         ac->req->controls = req->controls;
292
293         /* save it locally and remove it from the list */
294         /* we do not need to replace them later as we
295          * are keeping the original req intact */
296         if (!save_controls(control, ac->req, &saved_controls)) {
297                 return LDB_ERR_OPERATIONS_ERROR;
298         }
299
300         ac->req->context = ac;
301         ac->req->callback = server_sort_search_callback;
302         ldb_set_timeout_from_prev_req(module->ldb, req, ac->req);
303
304         req->handle = h;
305
306         return ldb_next_request(module, ac->req);
307 }
308
309 static int server_sort_results(struct ldb_handle *handle)
310 {
311         struct sort_context *ac;
312         struct ldb_reply *ares;
313         int i, ret;
314
315         ac = talloc_get_type(handle->private_data, struct sort_context);
316
317         ac->h = ldb_attrib_handler(ac->module->ldb, ac->attributeName);
318         ac->sort_result = 0;
319
320         ldb_qsort(ac->msgs, ac->num_msgs,
321                   sizeof(struct ldb_message *),
322                   ac, (ldb_qsort_cmp_fn_t)sort_compare);
323
324         for (i = 0; i < ac->num_msgs; i++) {
325                 ares = talloc_zero(ac, struct ldb_reply);
326                 if (!ares) {
327                         handle->status = LDB_ERR_OPERATIONS_ERROR;
328                         return handle->status;
329                 }
330
331                 ares->type = LDB_REPLY_ENTRY;
332                 ares->message = talloc_move(ares, &ac->msgs[i]);
333                 
334                 handle->status = ac->up_callback(ac->module->ldb, ac->up_context, ares);
335                 if (handle->status != LDB_SUCCESS) {
336                         return handle->status;
337                 }
338         }
339
340         for (i = 0; i < ac->num_refs; i++) {
341                 ares = talloc_zero(ac, struct ldb_reply);
342                 if (!ares) {
343                         handle->status = LDB_ERR_OPERATIONS_ERROR;
344                         return handle->status;
345                 }
346
347                 ares->type = LDB_REPLY_REFERRAL;
348                 ares->referral = talloc_move(ares, &ac->referrals[i]);
349                 
350                 handle->status = ac->up_callback(ac->module->ldb, ac->up_context, ares);
351                 if (handle->status != LDB_SUCCESS) {
352                         return handle->status;
353                 }
354         }
355
356         ares = talloc_zero(ac, struct ldb_reply);
357         if (!ares) {
358                 handle->status = LDB_ERR_OPERATIONS_ERROR;
359                 return handle->status;
360         }
361
362         ares->type = LDB_REPLY_DONE;
363         ares->controls = talloc_move(ares, &ac->controls);
364                 
365         handle->status = ac->up_callback(ac->module->ldb, ac->up_context, ares);
366         if (handle->status != LDB_SUCCESS) {
367                 return handle->status;
368         }
369
370         if ((ret = build_response(ac, &ac->controls, ac->sort_result, "sort control is not complete yet")) != LDB_SUCCESS) {
371                 return ret;
372         }
373
374         return LDB_SUCCESS;
375 }
376
377 static int server_sort_wait(struct ldb_handle *handle, enum ldb_wait_type type)
378 {
379         struct sort_context *ac;
380         int ret;
381     
382         if (!handle || !handle->private_data) {
383                 return LDB_ERR_OPERATIONS_ERROR;
384         }
385
386         ac = talloc_get_type(handle->private_data, struct sort_context);
387
388         ret = ldb_wait(ac->req->handle, type);
389
390         if (ret != LDB_SUCCESS) {
391                 handle->status = ret;
392                 return ret;
393         }
394                 
395         handle->state = ac->req->handle->state;
396         handle->status = ac->req->handle->status;
397
398         if (handle->status != LDB_SUCCESS) {
399                 return handle->status;
400         }
401
402         if (handle->state == LDB_ASYNC_DONE) {
403                 ret = server_sort_results(handle);
404         }
405
406         return ret;
407 }
408
409 static int server_sort_init(struct ldb_module *module)
410 {
411         struct ldb_request *req;
412         int ret;
413
414         req = talloc(module, struct ldb_request);
415         if (req == NULL) {
416                 return LDB_ERR_OPERATIONS_ERROR;
417         }
418
419         req->operation = LDB_REQ_REGISTER_CONTROL;
420         req->op.reg_control.oid = LDB_CONTROL_SERVER_SORT_OID;
421         req->controls = NULL;
422
423         ret = ldb_request(module->ldb, req);
424         if (ret != LDB_SUCCESS) {
425                 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "server_sort: Unable to register control with rootdse!\n");
426         }
427
428         talloc_free(req);
429         return ldb_next_init(module);
430 }
431
432 static const struct ldb_module_ops server_sort_ops = {
433         .name              = "server_sort",
434         .search            = server_sort_search,
435         .wait              = server_sort_wait,
436         .init_context      = server_sort_init
437 };
438
439 int ldb_sort_init(void)
440 {
441         return ldb_register_module(&server_sort_ops);
442 }