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