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 int flags = ltdb_attribute_flags(module, msg->elements[i].name);
96 if ((msg->dn[0] != '@') && (flags & LTDB_FLAG_HIDDEN)) {
99 if (msg_add_element(ldb, ret, &msg->elements[i]) != 0) {
109 pull the specified list of attributes from a message
111 static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module,
112 const struct ldb_message *msg,
113 const char * const *attrs)
115 struct ldb_context *ldb = module->ldb;
116 struct ldb_message *ret;
119 ret = talloc(ldb, struct ldb_message);
124 ret->dn = talloc_strdup(ret, msg->dn);
130 ret->num_elements = 0;
131 ret->elements = NULL;
134 if (msg_add_all_elements(module, ret, msg) != 0) {
141 for (i=0;attrs[i];i++) {
142 struct ldb_message_element *el;
144 if (strcmp(attrs[i], "*") == 0) {
145 if (msg_add_all_elements(module, ret, msg) != 0) {
152 if (ldb_attr_cmp(attrs[i], "dn") == 0 ||
153 ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
154 struct ldb_message_element el2;
158 el2.name = talloc_strdup(ret, attrs[i]);
166 val.length = strlen(ret->dn);
168 if (msg_add_element(ldb, ret, &el2) != 0) {
172 talloc_free(discard_const_p(char, el2.name));
176 el = ldb_msg_find_element(msg, attrs[i]);
180 if (msg_add_element(ldb, ret, el) != 0) {
192 see if a ldb_val is a wildcard
193 return 1 if yes, 0 if no
195 int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name,
196 const struct ldb_val *val)
200 /* all attribute types recognise the "*" wildcard */
201 if (val->length == 1 && strncmp((char *)val->data, "*", 1) == 0) {
205 if (strpbrk(val->data, "*?") == NULL) {
209 flags = ltdb_attribute_flags(module, attr_name);
210 if (flags & LTDB_FLAG_WILDCARD) {
219 search the database for a single simple dn, returning all attributes
222 return 1 on success, 0 on record-not-found and -1 on error
224 int ltdb_search_dn1(struct ldb_module *module, const char *dn, struct ldb_message *msg)
226 struct ltdb_private *ltdb = module->private_data;
228 TDB_DATA tdb_key, tdb_data, tdb_data2;
230 memset(msg, 0, sizeof(*msg));
233 tdb_key = ltdb_key(module, dn);
238 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
239 talloc_free(tdb_key.dptr);
240 if (!tdb_data.dptr) {
244 tdb_data2.dptr = talloc_memdup(msg, tdb_data.dptr, tdb_data.dsize);
246 if (!tdb_data2.dptr) {
249 tdb_data2.dsize = tdb_data.dsize;
251 msg->num_elements = 0;
252 msg->elements = NULL;
254 ret = ltdb_unpack_data(module, &tdb_data2, msg);
256 talloc_free(tdb_data2.dptr);
261 msg->dn = talloc_strdup(tdb_data2.dptr, dn);
264 talloc_free(tdb_data2.dptr);
273 search the database for a single simple dn
275 int ltdb_search_dn(struct ldb_module *module, const char *dn,
276 const char * const attrs[], struct ldb_message ***res)
278 struct ldb_context *ldb = module->ldb;
280 struct ldb_message *msg, *msg2;
282 *res = talloc_array(ldb, struct ldb_message *, 2);
287 msg = talloc(*res, struct ldb_message);
294 ret = ltdb_search_dn1(module, dn, msg);
301 msg2 = ltdb_pull_attrs(module, msg, attrs);
309 (*res)[0] = talloc_steal(*res, msg2);
317 add a set of attributes from a record to a set of results
318 return 0 on success, -1 on failure
320 int ltdb_add_attr_results(struct ldb_module *module, struct ldb_message *msg,
321 const char * const attrs[],
323 struct ldb_message ***res)
325 struct ldb_context *ldb = module->ldb;
326 struct ldb_message *msg2;
327 struct ldb_message **res2;
329 /* pull the attributes that the user wants */
330 msg2 = ltdb_pull_attrs(module, msg, attrs);
335 /* add to the results list */
336 res2 = talloc_realloc(ldb, *res, struct ldb_message *, (*count)+2);
344 (*res)[*count] = talloc_steal(*res, msg2);
345 (*res)[(*count)+1] = NULL;
353 internal search state during a full db search
355 struct ltdb_search_info {
356 struct ldb_module *module;
357 struct ldb_parse_tree *tree;
359 enum ldb_scope scope;
360 const char * const *attrs;
361 struct ldb_message **msgs;
368 search function for a non-indexed search
370 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
372 struct ltdb_search_info *sinfo = state;
373 struct ldb_message *msg;
377 strncmp(key.dptr, "DN=", 3) != 0) {
381 msg = talloc(sinfo, struct ldb_message);
386 /* unpack the record */
387 ret = ltdb_unpack_data(sinfo->module, &data, msg);
395 msg->dn = key.dptr + 3;
398 /* see if it matches the given expression */
399 if (!ltdb_message_match(sinfo->module, msg, sinfo->tree,
400 sinfo->base, sinfo->scope)) {
405 ret = ltdb_add_attr_results(sinfo->module, msg, sinfo->attrs, &sinfo->count, &sinfo->msgs);
418 search the database with a LDAP-like expression.
419 this is the "full search" non-indexed variant
421 static int ltdb_search_full(struct ldb_module *module,
423 enum ldb_scope scope,
424 struct ldb_parse_tree *tree,
425 const char * const attrs[], struct ldb_message ***res)
427 struct ltdb_private *ltdb = module->private_data;
429 struct ltdb_search_info *sinfo;
431 sinfo = talloc(ltdb, struct ltdb_search_info);
437 sinfo->module = module;
438 sinfo->scope = scope;
440 sinfo->attrs = attrs;
445 ret = tdb_traverse(ltdb->tdb, search_func, sinfo);
452 *res = talloc_steal(ltdb, sinfo->msgs);
453 count = sinfo->count;
462 search the database with a LDAP-like expression.
463 choses a search method
465 int ltdb_search_bytree(struct ldb_module *module, const char *base,
466 enum ldb_scope scope, struct ldb_parse_tree *tree,
467 const char * const attrs[], struct ldb_message ***res)
469 struct ltdb_private *ltdb = module->private_data;
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);
511 ltdb_unlock_read(module);
515 tree = ldb_parse_tree(ltdb, expression);
517 ltdb->last_err_string = "expression parse failed";
521 ret = ltdb_search_bytree(module, base, scope, tree, attrs, res);