25e56b24c8f57f08acb678db66cc5362bd5ec91f
[ira/wip.git] / source4 / lib / ldb / modules / sort.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2005-2008
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 "ldb_includes.h"
35
36 struct opaque {
37         struct ldb_context *ldb;
38         const struct ldb_attrib_handler *h;
39         const char *attribute;
40         int reverse;
41         int result;
42 };
43
44 struct sort_context {
45         struct ldb_module *module;
46
47         char *attributeName;
48         char *orderingRule;
49         int reverse;
50
51         struct ldb_request *req;
52         struct ldb_message **msgs;
53         char **referrals;
54         int num_msgs;
55         int num_refs;
56
57         const struct ldb_schema_attribute *a;
58         int sort_result;
59 };
60
61 static int build_response(void *mem_ctx, struct ldb_control ***ctrls, int result, const char *desc)
62 {
63         struct ldb_control **controls;
64         struct ldb_sort_resp_control *resp;
65         int i;
66
67         if (*ctrls) {
68                 controls = *ctrls;
69                 for (i = 0; controls[i]; i++);
70                 controls = talloc_realloc(mem_ctx, controls, struct ldb_control *, i + 2);
71         } else {
72                 i = 0;
73                 controls = talloc_array(mem_ctx, struct ldb_control *, 2);
74         }
75         if (! controls )
76                 return LDB_ERR_OPERATIONS_ERROR;
77
78         *ctrls = controls;
79
80         controls[i+1] = NULL;
81         controls[i] = talloc(controls, struct ldb_control);
82         if (! controls[i] )
83                 return LDB_ERR_OPERATIONS_ERROR;
84
85         controls[i]->oid = LDB_CONTROL_SORT_RESP_OID;
86         controls[i]->critical = 0;
87
88         resp = talloc(controls[i], struct ldb_sort_resp_control);
89         if (! resp )
90                 return LDB_ERR_OPERATIONS_ERROR;
91
92         resp->result = result;
93         resp->attr_desc = talloc_strdup(resp, desc);
94
95         if (! resp->attr_desc )
96                 return LDB_ERR_OPERATIONS_ERROR;
97         
98         controls[i]->data = resp;
99
100         return LDB_SUCCESS;
101 }
102
103 static int sort_compare(struct ldb_message **msg1, struct ldb_message **msg2, void *opaque)
104 {
105         struct sort_context *ac = talloc_get_type(opaque, struct sort_context);
106         struct ldb_message_element *el1, *el2;
107
108         if (!ac || ac->sort_result != 0) {
109                 /* an error occurred previously,
110                  * let's exit the sorting by returning always 0 */
111                 return 0;
112         }
113
114         el1 = ldb_msg_find_element(*msg1, ac->attributeName);
115         el2 = ldb_msg_find_element(*msg2, ac->attributeName);
116
117         if (!el1 || !el2) {
118                 /* the attribute was not found return and
119                  * set an error */
120                 ac->sort_result = LDB_ERR_UNWILLING_TO_PERFORM;
121                 return 0;
122         }
123
124         if (ac->reverse)
125                 return ac->a->syntax->comparison_fn(ac->module->ldb, ac, &el2->values[0], &el1->values[0]);
126
127         return ac->a->syntax->comparison_fn(ac->module->ldb, ac, &el1->values[0], &el2->values[0]);
128 }
129
130 static int server_sort_results(struct sort_context *ac)
131 {
132         struct ldb_reply *ares;
133         int i, ret;
134
135         ac->a = ldb_schema_attribute_by_name(ac->module->ldb, ac->attributeName);
136         ac->sort_result = 0;
137
138         ldb_qsort(ac->msgs, ac->num_msgs,
139                   sizeof(struct ldb_message *),
140                   ac, (ldb_qsort_cmp_fn_t)sort_compare);
141
142         if (ac->sort_result != LDB_SUCCESS) {
143                 return ac->sort_result;
144         }
145
146         for (i = 0; i < ac->num_msgs; i++) {
147                 ares = talloc_zero(ac, struct ldb_reply);
148                 if (!ares) {
149                         return LDB_ERR_OPERATIONS_ERROR;
150                 }
151
152                 ares->type = LDB_REPLY_ENTRY;
153                 ares->message = talloc_move(ares, &ac->msgs[i]);
154
155                 ret = ldb_module_send_entry(ac->req, ares->message, ares->controls);
156                 if (ret != LDB_SUCCESS) {
157                         return ret;
158                 }
159         }
160
161         for (i = 0; i < ac->num_refs; i++) {
162                 ares = talloc_zero(ac, struct ldb_reply);
163                 if (!ares) {
164                         return LDB_ERR_OPERATIONS_ERROR;
165                 }
166
167                 ares->type = LDB_REPLY_REFERRAL;
168                 ares->referral = talloc_move(ares, &ac->referrals[i]);
169
170                 ret = ldb_module_send_referral(ac->req, ares->referral);
171                 if (ret != LDB_SUCCESS) {
172                         return ret;
173                 }
174         }
175
176         return LDB_SUCCESS;
177 }
178
179 static int server_sort_search_callback(struct ldb_request *req, struct ldb_reply *ares)
180 {
181         struct sort_context *ac;
182         int ret;
183
184         ac = talloc_get_type(req->context, struct sort_context);
185
186         if (!ares) {
187                 return ldb_module_done(ac->req, NULL, NULL,
188                                         LDB_ERR_OPERATIONS_ERROR);
189         }
190         if (ares->error != LDB_SUCCESS) {
191                 return ldb_module_done(ac->req, ares->controls,
192                                         ares->response, ares->error);
193         }
194
195         switch (ares->type) {
196         case LDB_REPLY_ENTRY:
197                 ac->msgs = talloc_realloc(ac, ac->msgs, struct ldb_message *, ac->num_msgs + 2);
198                 if (! ac->msgs) {
199                         talloc_free(ares);
200                         ldb_oom(ac->module->ldb);
201                         return ldb_module_done(ac->req, NULL, NULL,
202                                                 LDB_ERR_OPERATIONS_ERROR);
203                 }
204
205                 ac->msgs[ac->num_msgs] = talloc_steal(ac->msgs, ares->message);
206                 ac->num_msgs++;
207                 ac->msgs[ac->num_msgs] = NULL;
208
209                 break;
210
211         case LDB_REPLY_REFERRAL:
212                 ac->referrals = talloc_realloc(ac, ac->referrals, char *, ac->num_refs + 2);
213                 if (! ac->referrals) {
214                         talloc_free(ares);
215                         ldb_oom(ac->module->ldb);
216                         return ldb_module_done(ac->req, NULL, NULL,
217                                                 LDB_ERR_OPERATIONS_ERROR);
218                 }
219
220                 ac->referrals[ac->num_refs] = talloc_steal(ac->referrals, ares->referral);
221                 ac->num_refs++;
222                 ac->referrals[ac->num_refs] = NULL;
223
224                 break;
225
226         case LDB_REPLY_DONE:
227
228                 ret = server_sort_results(ac);
229                 return ldb_module_done(ac->req, ares->controls,
230                                         ares->response, ret);
231         }
232
233         talloc_free(ares);
234         return LDB_SUCCESS;
235 }
236
237 static int server_sort_search(struct ldb_module *module, struct ldb_request *req)
238 {
239         struct ldb_control *control;
240         struct ldb_server_sort_control **sort_ctrls;
241         struct ldb_control **saved_controls;
242         struct ldb_control **controls;
243         struct ldb_request *down_req;
244         struct sort_context *ac;
245         int ret;
246
247         /* check if there's a paged request control */
248         control = ldb_request_get_control(req, LDB_CONTROL_SERVER_SORT_OID);
249         if (control == NULL) {
250                 /* not found go on */
251                 return ldb_next_request(module, req);
252         }
253
254         ac = talloc_zero(req, struct sort_context);
255         if (ac == NULL) {
256                 ldb_oom(module->ldb);
257                 return LDB_ERR_OPERATIONS_ERROR;
258         }
259
260         ac->module = module;
261         ac->req = req;
262
263         sort_ctrls = talloc_get_type(control->data, struct ldb_server_sort_control *);
264         if (!sort_ctrls) {
265                 return LDB_ERR_PROTOCOL_ERROR;
266         }
267
268         /* FIXME: we do not support more than one attribute for sorting right now */
269         /* FIXME: we need to check if the attribute type exist or return an error */
270                 
271         if (sort_ctrls[1] != NULL) {
272                 if (control->critical) {
273
274                         /* callback immediately */
275                         ret = build_response(req, &controls,
276                                              LDB_ERR_UNWILLING_TO_PERFORM,
277                                              "sort control is not complete yet");
278                         if (ret != LDB_SUCCESS) {
279                                 return ldb_module_done(req, NULL, NULL,
280                                                     LDB_ERR_OPERATIONS_ERROR);
281                         }
282
283                         return ldb_module_done(req, controls, NULL, ret);
284                 } else {
285                         /* just pass the call down and don't do any sorting */
286                         return ldb_next_request(module, req);
287                 }
288         }
289
290         ac->attributeName = sort_ctrls[0]->attributeName;
291         ac->orderingRule = sort_ctrls[0]->orderingRule;
292         ac->reverse = sort_ctrls[0]->reverse;
293
294         ret = ldb_build_search_req_ex(&down_req, module->ldb, ac,
295                                         req->op.search.base,
296                                         req->op.search.scope,
297                                         req->op.search.tree,
298                                         req->op.search.attrs,
299                                         req->controls,
300                                         ac,
301                                         server_sort_search_callback,
302                                         req);
303         if (ret != LDB_SUCCESS) {
304                 return LDB_ERR_OPERATIONS_ERROR;
305         }
306
307         /* save it locally and remove it from the list */
308         /* we do not need to replace them later as we
309          * are keeping the original req intact */
310         if (!save_controls(control, down_req, &saved_controls)) {
311                 return LDB_ERR_OPERATIONS_ERROR;
312         }
313
314         return ldb_next_request(module, down_req);
315 }
316
317 static int server_sort_init(struct ldb_module *module)
318 {
319         int ret;
320
321         ret = ldb_mod_register_control(module, LDB_CONTROL_SERVER_SORT_OID);
322         if (ret != LDB_SUCCESS) {
323                 ldb_debug(module->ldb, LDB_DEBUG_WARNING,
324                         "server_sort:"
325                         "Unable to register control with rootdse!\n");
326         }
327
328         return ldb_next_init(module);
329 }
330
331 const struct ldb_module_ops ldb_server_sort_module_ops = {
332         .name              = "server_sort",
333         .search            = server_sort_search,
334         .init_context      = server_sort_init
335 };