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
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 2 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, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb search functions
30 * Description: functions to search ldb+tdb databases
32 * Author: Andrew Tridgell
36 #include "ldb/include/ldb.h"
37 #include "ldb/include/ldb_private.h"
38 #include "ldb/ldb_tdb/ldb_tdb.h"
41 add one element to a message
43 static int msg_add_element(struct ldb_context *ldb,
44 struct ldb_message *ret, const struct ldb_message_element *el)
47 struct ldb_message_element *e2, *elnew;
49 e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
55 elnew = &e2[ret->num_elements];
57 elnew->name = talloc_strdup(ret->elements, el->name);
63 elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
71 for (i=0;i<el->num_values;i++) {
72 elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
73 if (elnew->values[i].length != el->values[i].length) {
78 elnew->num_values = el->num_values;
86 add all elements from one message into another
88 static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *ret,
89 const struct ldb_message *msg)
91 struct ldb_context *ldb = module->ldb;
94 for (i=0;i<msg->num_elements;i++) {
95 const struct ldb_attrib_handler *h;
96 h = ldb_attrib_handler(ldb, msg->elements[i].name);
97 if ((msg->dn[0] != '@') && (h->flags & LDB_ATTR_FLAG_HIDDEN)) {
100 if (msg_add_element(ldb, ret, &msg->elements[i]) != 0) {
110 pull the specified list of attributes from a message
112 static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module,
113 const struct ldb_message *msg,
114 const char * const *attrs)
116 struct ldb_context *ldb = module->ldb;
117 struct ldb_message *ret;
120 ret = talloc(ldb, struct ldb_message);
125 ret->dn = talloc_strdup(ret, msg->dn);
131 ret->num_elements = 0;
132 ret->elements = NULL;
135 if (msg_add_all_elements(module, ret, msg) != 0) {
142 for (i=0;attrs[i];i++) {
143 struct ldb_message_element *el;
145 if (strcmp(attrs[i], "*") == 0) {
146 if (msg_add_all_elements(module, ret, msg) != 0) {
153 if (ldb_attr_cmp(attrs[i], "dn") == 0 ||
154 ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
155 struct ldb_message_element el2;
159 el2.name = talloc_strdup(ret, attrs[i]);
167 val.length = strlen(ret->dn);
169 if (msg_add_element(ldb, ret, &el2) != 0) {
173 talloc_free(discard_const_p(char, el2.name));
177 el = ldb_msg_find_element(msg, attrs[i]);
181 if (msg_add_element(ldb, ret, el) != 0) {
192 search the database for a single simple dn, returning all attributes
195 return 1 on success, 0 on record-not-found and -1 on error
197 int ltdb_search_dn1(struct ldb_module *module, const char *dn, struct ldb_message *msg)
199 struct ltdb_private *ltdb = module->private_data;
201 TDB_DATA tdb_key, tdb_data, tdb_data2;
203 memset(msg, 0, sizeof(*msg));
206 tdb_key = ltdb_key(module, dn);
211 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
212 talloc_free(tdb_key.dptr);
213 if (!tdb_data.dptr) {
217 tdb_data2.dptr = talloc_memdup(msg, tdb_data.dptr, tdb_data.dsize);
219 if (!tdb_data2.dptr) {
222 tdb_data2.dsize = tdb_data.dsize;
224 msg->num_elements = 0;
225 msg->elements = NULL;
227 ret = ltdb_unpack_data(module, &tdb_data2, msg);
229 talloc_free(tdb_data2.dptr);
234 msg->dn = talloc_strdup(tdb_data2.dptr, dn);
237 talloc_free(tdb_data2.dptr);
246 search the database for a single simple dn
248 static int ltdb_search_dn(struct ldb_module *module, const char *dn,
249 const char * const attrs[], struct ldb_message ***res)
251 struct ldb_context *ldb = module->ldb;
252 struct ltdb_private *ltdb = module->private_data;
254 struct ldb_message *msg, *msg2;
258 if (ltdb_lock_read(module) != 0) {
262 ltdb->last_err_string = NULL;
264 if (ltdb_cache_load(module) != 0) {
265 ltdb_unlock_read(module);
269 *res = talloc_array(ldb, struct ldb_message *, 2);
274 msg = talloc(*res, struct ldb_message);
279 ret = ltdb_search_dn1(module, dn, msg);
283 ltdb_unlock_read(module);
287 msg2 = ltdb_pull_attrs(module, msg, attrs);
294 (*res)[0] = talloc_steal(*res, msg2);
297 ltdb_unlock_read(module);
303 ltdb_unlock_read(module);
309 add a set of attributes from a record to a set of results
310 return 0 on success, -1 on failure
312 int ltdb_add_attr_results(struct ldb_module *module, struct ldb_message *msg,
313 const char * const attrs[],
315 struct ldb_message ***res)
317 struct ldb_context *ldb = module->ldb;
318 struct ldb_message *msg2;
319 struct ldb_message **res2;
321 /* pull the attributes that the user wants */
322 msg2 = ltdb_pull_attrs(module, msg, attrs);
327 /* add to the results list */
328 res2 = talloc_realloc(ldb, *res, struct ldb_message *, (*count)+2);
336 (*res)[*count] = talloc_steal(*res, msg2);
337 (*res)[(*count)+1] = NULL;
345 internal search state during a full db search
347 struct ltdb_search_info {
348 struct ldb_module *module;
349 struct ldb_parse_tree *tree;
351 enum ldb_scope scope;
352 const char * const *attrs;
353 struct ldb_message **msgs;
360 search function for a non-indexed search
362 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
364 struct ltdb_search_info *sinfo = state;
365 struct ldb_message *msg;
369 strncmp(key.dptr, "DN=", 3) != 0) {
373 msg = talloc(sinfo, struct ldb_message);
378 /* unpack the record */
379 ret = ltdb_unpack_data(sinfo->module, &data, msg);
387 msg->dn = key.dptr + 3;
390 /* see if it matches the given expression */
391 if (!ldb_match_msg(sinfo->module->ldb, msg, sinfo->tree,
392 sinfo->base, sinfo->scope)) {
397 ret = ltdb_add_attr_results(sinfo->module, msg, sinfo->attrs, &sinfo->count, &sinfo->msgs);
410 search the database with a LDAP-like expression.
411 this is the "full search" non-indexed variant
413 static int ltdb_search_full(struct ldb_module *module,
415 enum ldb_scope scope,
416 struct ldb_parse_tree *tree,
417 const char * const attrs[], struct ldb_message ***res)
419 struct ltdb_private *ltdb = module->private_data;
421 struct ltdb_search_info *sinfo;
423 sinfo = talloc(ltdb, struct ltdb_search_info);
429 sinfo->module = module;
430 sinfo->scope = scope;
432 sinfo->attrs = attrs;
437 ret = tdb_traverse(ltdb->tdb, search_func, sinfo);
444 *res = talloc_steal(ltdb, sinfo->msgs);
445 count = sinfo->count;
454 search the database with a LDAP-like expression.
455 choses a search method
457 int ltdb_search_bytree(struct ldb_module *module, const char *base,
458 enum ldb_scope scope, struct ldb_parse_tree *tree,
459 const char * const attrs[], struct ldb_message ***res)
461 struct ltdb_private *ltdb = module->private_data;
464 /* it is important that we handle dn queries this way, and not
465 via a full db search, otherwise ldb is horribly slow */
466 if (tree->operation == LDB_OP_EQUALITY &&
467 (ldb_attr_cmp(tree->u.equality.attr, "dn") == 0 ||
468 ldb_attr_cmp(tree->u.equality.attr, "distinguishedName") == 0)) {
469 return ltdb_search_dn(module, tree->u.equality.value.data, attrs, res);
472 if (ltdb_lock_read(module) != 0) {
476 ltdb->last_err_string = NULL;
478 if (ltdb_cache_load(module) != 0) {
479 ltdb_unlock_read(module);
485 ret = ltdb_search_indexed(module, base, scope, tree, attrs, res);
487 ret = ltdb_search_full(module, base, scope, tree, attrs, res);
490 ltdb_unlock_read(module);
497 search the database with a LDAP-like expression.
498 choses a search method
500 int ltdb_search(struct ldb_module *module, const char *base,
501 enum ldb_scope scope, const char *expression,
502 const char * const attrs[], struct ldb_message ***res)
504 struct ltdb_private *ltdb = module->private_data;
505 struct ldb_parse_tree *tree;
508 /* check if we are looking for a simple dn */
509 if (scope == LDB_SCOPE_BASE && (expression == NULL || expression[0] == '\0')) {
510 ret = ltdb_search_dn(module, base, attrs, res);
514 tree = ldb_parse_tree(ltdb, expression);
516 ltdb->last_err_string = "expression parse failed";
520 ret = ltdb_search_bytree(module, base, scope, tree, attrs, res);