s4-ldb: merged with master
[Samba/ekacnet.git] / source4 / lib / ldb / ldb_tdb / ldb_search.c
bloba128d9cc0c7da2a34103e7972d38957851dfc62d
1 /*
2 ldb database library
4 Copyright (C) Andrew Tridgell 2004
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: ldb
27 * Component: ldb search functions
29 * Description: functions to search ldb+tdb databases
31 * Author: Andrew Tridgell
34 #include "ldb_tdb.h"
37 add one element to a message
39 static int msg_add_element(struct ldb_message *ret,
40 const struct ldb_message_element *el,
41 int check_duplicates)
43 unsigned int i;
44 struct ldb_message_element *e2, *elnew;
46 if (check_duplicates && ldb_msg_find_element(ret, el->name)) {
47 /* its already there */
48 return 0;
51 e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
52 if (!e2) {
53 return -1;
55 ret->elements = e2;
57 elnew = &e2[ret->num_elements];
59 elnew->name = talloc_strdup(ret->elements, el->name);
60 if (!elnew->name) {
61 return -1;
64 if (el->num_values) {
65 elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
66 if (!elnew->values) {
67 return -1;
69 } else {
70 elnew->values = NULL;
73 for (i=0;i<el->num_values;i++) {
74 elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
75 if (elnew->values[i].length != el->values[i].length) {
76 return -1;
80 elnew->num_values = el->num_values;
82 ret->num_elements++;
84 return 0;
88 add the special distinguishedName element
90 static int msg_add_distinguished_name(struct ldb_message *msg)
92 struct ldb_message_element el;
93 struct ldb_val val;
94 int ret;
96 el.flags = 0;
97 el.name = "distinguishedName";
98 el.num_values = 1;
99 el.values = &val;
100 val.data = (uint8_t *)ldb_dn_alloc_linearized(msg, msg->dn);
101 val.length = strlen((char *)val.data);
103 ret = msg_add_element(msg, &el, 1);
104 return ret;
108 add all elements from one message into another
110 static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *ret,
111 const struct ldb_message *msg)
113 struct ldb_context *ldb;
114 unsigned int i;
115 int check_duplicates = (ret->num_elements != 0);
117 ldb = ldb_module_get_ctx(module);
119 if (msg_add_distinguished_name(ret) != 0) {
120 return -1;
123 for (i=0;i<msg->num_elements;i++) {
124 const struct ldb_schema_attribute *a;
125 a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);
126 if (a->flags & LDB_ATTR_FLAG_HIDDEN) {
127 continue;
129 if (msg_add_element(ret, &msg->elements[i],
130 check_duplicates) != 0) {
131 return -1;
135 return 0;
140 pull the specified list of attributes from a message
142 static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module,
143 TALLOC_CTX *mem_ctx,
144 const struct ldb_message *msg,
145 const char * const *attrs)
147 struct ldb_message *ret;
148 int i;
150 ret = talloc(mem_ctx, struct ldb_message);
151 if (!ret) {
152 return NULL;
155 ret->dn = ldb_dn_copy(ret, msg->dn);
156 if (!ret->dn) {
157 talloc_free(ret);
158 return NULL;
161 ret->num_elements = 0;
162 ret->elements = NULL;
164 if (!attrs) {
165 if (msg_add_all_elements(module, ret, msg) != 0) {
166 talloc_free(ret);
167 return NULL;
169 return ret;
172 for (i=0;attrs[i];i++) {
173 struct ldb_message_element *el;
175 if (strcmp(attrs[i], "*") == 0) {
176 if (msg_add_all_elements(module, ret, msg) != 0) {
177 talloc_free(ret);
178 return NULL;
180 continue;
183 if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
184 if (msg_add_distinguished_name(ret) != 0) {
185 return NULL;
187 continue;
190 el = ldb_msg_find_element(msg, attrs[i]);
191 if (!el) {
192 continue;
194 if (msg_add_element(ret, el, 1) != 0) {
195 talloc_free(ret);
196 return NULL;
200 return ret;
204 search the database for a single simple dn.
205 return LDB_ERR_NO_SUCH_OBJECT on record-not-found
206 and LDB_SUCCESS on success
208 static int ltdb_search_base(struct ldb_module *module, struct ldb_dn *dn)
210 void *data = ldb_module_get_private(module);
211 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
212 TDB_DATA tdb_key, tdb_data;
214 if (ldb_dn_is_null(dn)) {
215 return LDB_ERR_NO_SUCH_OBJECT;
218 /* form the key */
219 tdb_key = ltdb_key(module, dn);
220 if (!tdb_key.dptr) {
221 return LDB_ERR_OPERATIONS_ERROR;
224 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
225 talloc_free(tdb_key.dptr);
226 if (!tdb_data.dptr) {
227 return LDB_ERR_NO_SUCH_OBJECT;
230 free(tdb_data.dptr);
231 return LDB_SUCCESS;
235 search the database for a single tdb key, returning all attributes
236 in a single message
238 return LDB_ERR_NO_SUCH_OBJECT on record-not-found
239 and LDB_SUCCESS on success
241 int ltdb_search_dn1_key(struct ldb_module *module,
242 TDB_DATA tdb_key, struct ldb_message *msg)
244 void *data = ldb_module_get_private(module);
245 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
246 int ret;
247 TDB_DATA tdb_data;
249 memset(msg, 0, sizeof(*msg));
251 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
252 if (!tdb_data.dptr) {
253 return LDB_ERR_NO_SUCH_OBJECT;
256 msg->num_elements = 0;
257 msg->elements = NULL;
259 ret = ltdb_unpack_data(module, &tdb_data, msg);
260 free(tdb_data.dptr);
261 if (ret == -1) {
262 struct ldb_context *ldb = ldb_module_get_ctx(module);
263 ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %s\n",
264 ldb_dn_get_linearized(msg->dn));
265 return LDB_ERR_OPERATIONS_ERROR;
268 if (!msg->dn) {
269 return LDB_ERR_OPERATIONS_ERROR;
272 return LDB_SUCCESS;
276 search the database for a single simple dn, returning all attributes
277 in a single message
279 return LDB_ERR_NO_SUCH_OBJECT on record-not-found
280 and LDB_SUCCESS on success
283 int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg)
285 int ret;
286 TDB_DATA tdb_key;
288 memset(msg, 0, sizeof(*msg));
290 /* form the key */
291 tdb_key = ltdb_key(msg, dn);
292 if (!tdb_key.dptr) {
293 return LDB_ERR_OPERATIONS_ERROR;
296 ret = ltdb_search_dn1_key(module, tdb_key, msg);
297 talloc_free(tdb_key.dptr);
298 return ret;
302 add a set of attributes from a record to a set of results
303 return 0 on success, -1 on failure
305 int ltdb_add_attr_results(struct ldb_module *module,
306 TALLOC_CTX *mem_ctx,
307 struct ldb_message *msg,
308 const char * const attrs[],
309 unsigned int *count,
310 struct ldb_message ***res)
312 struct ldb_message *msg2;
313 struct ldb_message **res2;
315 /* pull the attributes that the user wants */
316 msg2 = ltdb_pull_attrs(module, mem_ctx, msg, attrs);
317 if (!msg2) {
318 return -1;
321 /* add to the results list */
322 res2 = talloc_realloc(mem_ctx, *res, struct ldb_message *, (*count)+2);
323 if (!res2) {
324 talloc_free(msg2);
325 return -1;
328 (*res) = res2;
330 (*res)[*count] = talloc_move(*res, &msg2);
331 (*res)[(*count)+1] = NULL;
332 (*count)++;
334 return 0;
340 filter the specified list of attributes from a message
341 removing not requested attrs.
343 int ltdb_filter_attrs(struct ldb_message *msg, const char * const *attrs)
345 int i, keep_all = 0;
347 if (attrs) {
348 /* check for special attrs */
349 for (i = 0; attrs[i]; i++) {
350 if (strcmp(attrs[i], "*") == 0) {
351 keep_all = 1;
352 break;
355 if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
356 if (msg_add_distinguished_name(msg) != 0) {
357 return -1;
361 } else {
362 keep_all = 1;
365 if (keep_all) {
366 if (msg_add_distinguished_name(msg) != 0) {
367 return -1;
369 return 0;
372 for (i = 0; i < msg->num_elements; i++) {
373 int j, found;
375 for (j = 0, found = 0; attrs[j]; j++) {
376 if (ldb_attr_cmp(msg->elements[i].name, attrs[j]) == 0) {
377 found = 1;
378 break;
382 if (!found) {
383 ldb_msg_remove_attr(msg, msg->elements[i].name);
384 i--;
388 return 0;
392 search function for a non-indexed search
394 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
396 struct ldb_context *ldb;
397 struct ltdb_context *ac;
398 struct ldb_message *msg;
399 int ret;
401 ac = talloc_get_type(state, struct ltdb_context);
402 ldb = ldb_module_get_ctx(ac->module);
404 if (key.dsize < 4 ||
405 strncmp((char *)key.dptr, "DN=", 3) != 0) {
406 return 0;
409 msg = ldb_msg_new(ac);
410 if (!msg) {
411 return -1;
414 /* unpack the record */
415 ret = ltdb_unpack_data(ac->module, &data, msg);
416 if (ret == -1) {
417 talloc_free(msg);
418 return -1;
421 /* see if it matches the given expression */
422 if (!ldb_match_msg(ldb, msg,
423 ac->tree, ac->base, ac->scope)) {
424 talloc_free(msg);
425 return 0;
428 /* filter the attributes that the user wants */
429 ret = ltdb_filter_attrs(msg, ac->attrs);
431 if (ret == -1) {
432 talloc_free(msg);
433 return -1;
436 ret = ldb_module_send_entry(ac->req, msg, NULL);
437 if (ret != LDB_SUCCESS) {
438 ac->request_terminated = true;
439 /* the callback failed, abort the operation */
440 return -1;
443 return 0;
448 search the database with a LDAP-like expression.
449 this is the "full search" non-indexed variant
451 static int ltdb_search_full(struct ltdb_context *ctx)
453 void *data = ldb_module_get_private(ctx->module);
454 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
455 int ret;
457 if (ltdb->in_transaction != 0) {
458 ret = tdb_traverse(ltdb->tdb, search_func, ctx);
459 } else {
460 ret = tdb_traverse_read(ltdb->tdb, search_func, ctx);
463 if (ret == -1) {
464 return LDB_ERR_OPERATIONS_ERROR;
467 return LDB_SUCCESS;
471 search the database with a LDAP-like expression.
472 choses a search method
474 int ltdb_search(struct ltdb_context *ctx)
476 struct ldb_context *ldb;
477 struct ldb_module *module = ctx->module;
478 struct ldb_request *req = ctx->req;
479 void *data = ldb_module_get_private(module);
480 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
481 int ret;
483 ldb = ldb_module_get_ctx(module);
485 ldb_request_set_state(req, LDB_ASYNC_PENDING);
487 if (ltdb_lock_read(module) != 0) {
488 return LDB_ERR_OPERATIONS_ERROR;
491 if (ltdb_cache_load(module) != 0) {
492 ltdb_unlock_read(module);
493 return LDB_ERR_OPERATIONS_ERROR;
496 if (req->op.search.tree == NULL) {
497 ltdb_unlock_read(module);
498 return LDB_ERR_OPERATIONS_ERROR;
501 if ((req->op.search.base == NULL) || (ldb_dn_is_null(req->op.search.base) == true)) {
503 /* Check what we should do with a NULL dn */
504 switch (req->op.search.scope) {
505 case LDB_SCOPE_BASE:
506 ldb_asprintf_errstring(ldb,
507 "NULL Base DN invalid for a base search");
508 ret = LDB_ERR_INVALID_DN_SYNTAX;
509 break;
510 case LDB_SCOPE_ONELEVEL:
511 ldb_asprintf_errstring(ldb,
512 "NULL Base DN invalid for a one-level search");
513 ret = LDB_ERR_INVALID_DN_SYNTAX;
514 break;
515 case LDB_SCOPE_SUBTREE:
516 default:
517 /* We accept subtree searches from a NULL base DN, ie over the whole DB */
518 ret = LDB_SUCCESS;
520 } else if (ldb_dn_is_valid(req->op.search.base) == false) {
522 /* We don't want invalid base DNs here */
523 ldb_asprintf_errstring(ldb,
524 "Invalid Base DN: %s",
525 ldb_dn_get_linearized(req->op.search.base));
526 ret = LDB_ERR_INVALID_DN_SYNTAX;
528 } else if (ltdb->check_base) {
529 /* This database has been marked as 'checkBaseOnSearch', so do a spot check of the base dn */
530 ret = ltdb_search_base(module, req->op.search.base);
532 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
533 ldb_asprintf_errstring(ldb,
534 "No such Base DN: %s",
535 ldb_dn_get_linearized(req->op.search.base));
538 } else {
539 /* If we are not checking the base DN life is easy */
540 ret = LDB_SUCCESS;
543 ctx->tree = req->op.search.tree;
544 ctx->scope = req->op.search.scope;
545 ctx->base = req->op.search.base;
546 ctx->attrs = req->op.search.attrs;
548 if (ret == LDB_SUCCESS) {
549 uint32_t match_count = 0;
551 ret = ltdb_search_indexed(ctx, &match_count);
552 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
553 /* Not in the index, therefore OK! */
554 ret = LDB_SUCCESS;
557 /* Check if we got just a normal error.
558 * In that case proceed to a full search unless we got a
559 * callback error */
560 if ( ! ctx->request_terminated && ret != LDB_SUCCESS) {
561 /* Not indexed, so we need to do a full scan */
562 #if 0
563 /* useful for debugging when slow performance
564 * is caused by unindexed searches */
565 char *expression = ldb_filter_from_tree(ctx, ctx->tree);
566 printf("FULL SEARCH: %s\n", expression);
567 talloc_free(expression);
568 #endif
569 if (match_count != 0) {
570 /* the indexing code gave an error
571 * after having returned at least one
572 * entry. This means the indexes are
573 * corrupt or a database record is
574 * corrupt. We cannot continue with a
575 * full search or we may return
576 * duplicate entries
578 return LDB_ERR_OPERATIONS_ERROR;
580 ret = ltdb_search_full(ctx);
581 if (ret != LDB_SUCCESS) {
582 ldb_set_errstring(ldb, "Indexed and full searches both failed!\n");
587 ltdb_unlock_read(module);
589 return ret;