Fix the mess with ldb includes.
[Samba/ekacnet.git] / source4 / lib / ldb / modules / paged_searches.c
blob01e77cb22cc8c6ee4c95e6995c61e2c14328a584
1 /*
2 ldb database library
4 Copyright (C) Simo Sorce 2005-2008
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
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/>.
25 * Name: paged_searches
27 * Component: ldb paged searches module
29 * Description: this module detects if the remote ldap server supports
30 * paged results and use them to transparently access all objects
32 * Author: Simo Sorce
35 #include "includes.h"
36 #include "ldb_module.h"
38 #define PS_DEFAULT_PAGE_SIZE 500
39 /* 500 objects per query seem to be a decent compromise
40 * the default AD limit per request is 1000 entries */
42 struct private_data {
44 bool paged_supported;
47 struct ps_context {
48 struct ldb_module *module;
49 struct ldb_request *req;
51 bool pending;
53 char **saved_referrals;
54 int num_referrals;
57 static int check_ps_continuation(struct ldb_request *req, struct ldb_reply *ares)
59 struct ps_context *ac;
60 struct ldb_paged_control *rep_control, *req_control;
62 ac = talloc_get_type(req->context, struct ps_context);
64 /* look up our paged control */
65 if (!ares->controls || strcmp(LDB_CONTROL_PAGED_RESULTS_OID, ares->controls[0]->oid) != 0) {
66 /* something wrong here */
67 return LDB_ERR_OPERATIONS_ERROR;
70 rep_control = talloc_get_type(ares->controls[0]->data, struct ldb_paged_control);
71 if (rep_control->cookie_len == 0) {
72 /* we are done */
73 ac->pending = false;
74 return LDB_SUCCESS;
77 /* more processing required */
78 /* let's fill in the request control with the new cookie */
79 /* if there's a reply control we must find a request
80 * control matching it */
82 if (strcmp(LDB_CONTROL_PAGED_RESULTS_OID, req->controls[0]->oid) != 0) {
83 /* something wrong here */
84 return LDB_ERR_OPERATIONS_ERROR;
87 req_control = talloc_get_type(req->controls[0]->data, struct ldb_paged_control);
89 if (req_control->cookie) {
90 talloc_free(req_control->cookie);
93 req_control->cookie = talloc_memdup(req_control,
94 rep_control->cookie,
95 rep_control->cookie_len);
96 req_control->cookie_len = rep_control->cookie_len;
98 ac->pending = true;
99 return LDB_SUCCESS;
102 static int store_referral(struct ps_context *ac, char *referral)
104 ac->saved_referrals = talloc_realloc(ac, ac->saved_referrals, char *, ac->num_referrals + 2);
105 if (!ac->saved_referrals) {
106 return LDB_ERR_OPERATIONS_ERROR;
109 ac->saved_referrals[ac->num_referrals] = talloc_strdup(ac->saved_referrals, referral);
110 if (!ac->saved_referrals[ac->num_referrals]) {
111 return LDB_ERR_OPERATIONS_ERROR;
114 ac->num_referrals++;
115 ac->saved_referrals[ac->num_referrals] = NULL;
117 return LDB_SUCCESS;
120 static int send_referrals(struct ps_context *ac)
122 struct ldb_reply *ares;
123 int ret;
124 int i;
126 for (i = 0; i < ac->num_referrals; i++) {
127 ares = talloc_zero(ac->req, struct ldb_reply);
128 if (!ares) {
129 return LDB_ERR_OPERATIONS_ERROR;
132 ares->type = LDB_REPLY_REFERRAL;
133 ares->referral = ac->saved_referrals[i];
135 ret = ldb_module_send_referral(ac->req, ares->referral);
136 if (ret != LDB_SUCCESS) {
137 return ret;
141 return LDB_SUCCESS;
144 static int ps_next_request(struct ps_context *ac);
146 static int ps_callback(struct ldb_request *req, struct ldb_reply *ares)
148 struct ps_context *ac;
149 int ret;
151 ac = talloc_get_type(req->context, struct ps_context);
153 if (!ares) {
154 return ldb_module_done(ac->req, NULL, NULL,
155 LDB_ERR_OPERATIONS_ERROR);
157 if (ares->error != LDB_SUCCESS) {
158 return ldb_module_done(ac->req, ares->controls,
159 ares->response, ares->error);
162 switch (ares->type) {
163 case LDB_REPLY_ENTRY:
164 ret = ldb_module_send_entry(ac->req, ares->message, ares->controls);
165 if (ret != LDB_SUCCESS) {
166 return ldb_module_done(ac->req, NULL, NULL, ret);
168 break;
170 case LDB_REPLY_REFERRAL:
171 ret = store_referral(ac, ares->referral);
172 if (ret != LDB_SUCCESS) {
173 return ldb_module_done(ac->req, NULL, NULL, ret);
175 break;
177 case LDB_REPLY_DONE:
179 ret = check_ps_continuation(req, ares);
180 if (ret != LDB_SUCCESS) {
181 return ldb_module_done(ac->req, NULL, NULL, ret);
184 if (ac->pending) {
186 ret = ps_next_request(ac);
187 if (ret != LDB_SUCCESS) {
188 return ldb_module_done(ac->req,
189 NULL, NULL, ret);
192 } else {
194 /* send referrals */
195 ret = send_referrals(ac);
196 if (ret != LDB_SUCCESS) {
197 return ldb_module_done(ac->req,
198 NULL, NULL, ret);
201 /* send REPLY_DONE */
202 return ldb_module_done(ac->req, ares->controls,
203 ares->response, LDB_SUCCESS);
205 break;
208 talloc_free(ares);
209 return LDB_SUCCESS;
212 static int ps_search(struct ldb_module *module, struct ldb_request *req)
214 struct ldb_context *ldb;
215 struct private_data *private_data;
216 struct ps_context *ac;
218 private_data = talloc_get_type(ldb_module_get_private(module), struct private_data);
219 ldb = ldb_module_get_ctx(module);
221 /* check if paging is supported and if there is a any control */
222 if (!private_data || !private_data->paged_supported || req->controls) {
223 /* do not touch this request paged controls not
224 * supported or explicit controls have been set or we
225 * are just not setup yet */
226 return ldb_next_request(module, req);
229 ac = talloc_zero(req, struct ps_context);
230 if (ac == NULL) {
231 ldb_oom(ldb);
232 return LDB_ERR_OPERATIONS_ERROR;
235 ac->module = module;
236 ac->req = req;
237 ac->pending = false;
238 ac->saved_referrals = NULL;
239 ac->num_referrals = 0;
241 return ps_next_request(ac);
244 static int ps_next_request(struct ps_context *ac) {
246 struct ldb_context *ldb;
247 struct ldb_paged_control *control;
248 struct ldb_control **controls;
249 struct ldb_request *new_req;
250 int ret;
252 ldb = ldb_module_get_ctx(ac->module);
254 controls = talloc_array(ac, struct ldb_control *, 2);
255 if (!controls) {
256 return LDB_ERR_OPERATIONS_ERROR;
259 controls[0] = talloc(controls, struct ldb_control);
260 if (!controls[0]) {
261 return LDB_ERR_OPERATIONS_ERROR;
264 control = talloc(controls[0], struct ldb_paged_control);
265 if (!control) {
266 return LDB_ERR_OPERATIONS_ERROR;
269 control->size = PS_DEFAULT_PAGE_SIZE;
270 control->cookie = NULL;
271 control->cookie_len = 0;
273 controls[0]->oid = LDB_CONTROL_PAGED_RESULTS_OID;
274 controls[0]->critical = 1;
275 controls[0]->data = control;
276 controls[1] = NULL;
278 ret = ldb_build_search_req_ex(&new_req, ldb, ac,
279 ac->req->op.search.base,
280 ac->req->op.search.scope,
281 ac->req->op.search.tree,
282 ac->req->op.search.attrs,
283 controls,
285 ps_callback,
286 ac->req);
287 if (ret != LDB_SUCCESS) {
288 return ret;
290 talloc_steal(new_req, controls);
292 return ldb_next_request(ac->module, new_req);
295 static int check_supported_paged(struct ldb_request *req,
296 struct ldb_reply *ares)
298 struct private_data *data;
300 data = talloc_get_type(req->context, struct private_data);
302 if (!ares) {
303 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
305 if (ares->error != LDB_SUCCESS) {
306 return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
309 switch (ares->type) {
310 case LDB_REPLY_ENTRY:
311 if (ldb_msg_check_string_attribute(ares->message,
312 "supportedControl",
313 LDB_CONTROL_PAGED_RESULTS_OID)) {
314 data->paged_supported = true;
316 break;
318 case LDB_REPLY_REFERRAL:
319 /* ignore */
320 break;
322 case LDB_REPLY_DONE:
323 return ldb_request_done(req, LDB_SUCCESS);
326 talloc_free(ares);
327 return LDB_SUCCESS;
330 static int ps_init(struct ldb_module *module)
332 struct ldb_context *ldb;
333 static const char *attrs[] = { "supportedControl", NULL };
334 struct private_data *data;
335 struct ldb_dn *base;
336 int ret;
337 struct ldb_request *req;
339 ldb = ldb_module_get_ctx(module);
341 data = talloc(module, struct private_data);
342 if (data == NULL) {
343 ldb_oom(ldb);
344 return LDB_ERR_OPERATIONS_ERROR;
346 data->paged_supported = false;
348 ldb_module_set_private(module, data);
350 base = ldb_dn_new(module, ldb, "");
351 if (base == NULL) {
352 ldb_oom(ldb);
353 return LDB_ERR_OPERATIONS_ERROR;
355 ret = ldb_build_search_req(&req, ldb, module,
356 base, LDB_SCOPE_BASE,
357 "(objectClass=*)",
358 attrs, NULL,
359 data, check_supported_paged,
360 NULL);
361 if (ret != LDB_SUCCESS) {
362 return ret;
365 ret = ldb_next_request(module, req);
366 if (ret == LDB_SUCCESS) {
367 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
369 if (ret != LDB_SUCCESS) {
370 return ret;
373 talloc_free(base);
374 talloc_free(req);
376 return ldb_next_init(module);
379 _PUBLIC_ const struct ldb_module_ops ldb_paged_searches_module_ops = {
380 .name = "paged_searches",
381 .search = ps_search,
382 .init_context = ps_init