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"
39 #include "ldb/include/ldb_parse.h"
42 add one element to a message
44 static int msg_add_element(struct ldb_context *ldb,
45 struct ldb_message *ret, const struct ldb_message_element *el)
48 struct ldb_message_element *e2, *elnew;
50 e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
56 elnew = &e2[ret->num_elements];
58 elnew->name = talloc_strdup(ret->elements, el->name);
64 elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
72 for (i=0;i<el->num_values;i++) {
73 elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
74 if (elnew->values[i].length != el->values[i].length) {
79 elnew->num_values = el->num_values;
87 add all elements from one message into another
89 static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *ret,
90 const struct ldb_message *msg)
92 struct ldb_context *ldb = module->ldb;
95 for (i=0;i<msg->num_elements;i++) {
96 int flags = ltdb_attribute_flags(module, msg->elements[i].name);
97 if ((msg->dn[0] != '@') && (flags & LTDB_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(el2.name);
177 el = ldb_msg_find_element(msg, attrs[i]);
181 if (msg_add_element(ldb, ret, el) != 0) {
193 see if a ldb_val is a wildcard
194 return 1 if yes, 0 if no
196 int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name,
197 const struct ldb_val *val)
201 /* all attribute types recognise the "*" wildcard */
202 if (val->length == 1 && strncmp((char *)val->data, "*", 1) == 0) {
206 if (strpbrk(val->data, "*?") == NULL) {
210 flags = ltdb_attribute_flags(module, attr_name);
211 if (flags & LTDB_FLAG_WILDCARD) {
220 search the database for a single simple dn, returning all attributes
223 return 1 on success, 0 on record-not-found and -1 on error
225 int ltdb_search_dn1(struct ldb_module *module, const char *dn, struct ldb_message *msg)
227 struct ltdb_private *ltdb = module->private_data;
229 TDB_DATA tdb_key, tdb_data, tdb_data2;
231 memset(msg, 0, sizeof(*msg));
234 tdb_key = ltdb_key(module, dn);
239 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
240 talloc_free(tdb_key.dptr);
241 if (!tdb_data.dptr) {
245 tdb_data2.dptr = talloc_memdup(msg, tdb_data.dptr, tdb_data.dsize);
247 if (!tdb_data2.dptr) {
250 tdb_data2.dsize = tdb_data.dsize;
252 msg->num_elements = 0;
253 msg->elements = NULL;
255 ret = ltdb_unpack_data(module, &tdb_data2, msg);
257 talloc_free(tdb_data2.dptr);
262 msg->dn = talloc_strdup(tdb_data2.dptr, dn);
265 talloc_free(tdb_data2.dptr);
274 search the database for a single simple dn
276 int ltdb_search_dn(struct ldb_module *module, char *dn,
277 const char * const attrs[], struct ldb_message ***res)
279 struct ldb_context *ldb = module->ldb;
281 struct ldb_message *msg, *msg2;
283 *res = talloc_array(ldb, struct ldb_message *, 2);
288 msg = talloc(*res, struct ldb_message);
295 ret = ltdb_search_dn1(module, dn, msg);
302 msg2 = ltdb_pull_attrs(module, msg, attrs);
310 (*res)[0] = talloc_steal(*res, msg2);
318 add a set of attributes from a record to a set of results
319 return 0 on success, -1 on failure
321 int ltdb_add_attr_results(struct ldb_module *module, struct ldb_message *msg,
322 const char * const attrs[],
324 struct ldb_message ***res)
326 struct ldb_context *ldb = module->ldb;
327 struct ldb_message *msg2;
328 struct ldb_message **res2;
330 /* pull the attributes that the user wants */
331 msg2 = ltdb_pull_attrs(module, msg, attrs);
336 /* add to the results list */
337 res2 = talloc_realloc(ldb, *res, struct ldb_message *, (*count)+2);
345 (*res)[*count] = talloc_steal(*res, msg2);
346 (*res)[(*count)+1] = NULL;
354 internal search state during a full db search
356 struct ltdb_search_info {
357 struct ldb_module *module;
358 struct ldb_parse_tree *tree;
360 enum ldb_scope scope;
361 const char * const *attrs;
362 struct ldb_message **msgs;
369 search function for a non-indexed search
371 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
373 struct ltdb_search_info *sinfo = state;
374 struct ldb_message *msg;
378 strncmp(key.dptr, "DN=", 3) != 0) {
382 msg = talloc(sinfo, struct ldb_message);
387 /* unpack the record */
388 ret = ltdb_unpack_data(sinfo->module, &data, msg);
396 msg->dn = key.dptr + 3;
399 /* see if it matches the given expression */
400 if (!ltdb_message_match(sinfo->module, msg, sinfo->tree,
401 sinfo->base, sinfo->scope)) {
406 ret = ltdb_add_attr_results(sinfo->module, msg, sinfo->attrs, &sinfo->count, &sinfo->msgs);
419 search the database with a LDAP-like expression.
420 this is the "full search" non-indexed variant
422 static int ltdb_search_full(struct ldb_module *module,
424 enum ldb_scope scope,
425 struct ldb_parse_tree *tree,
426 const char * const attrs[], struct ldb_message ***res)
428 struct ltdb_private *ltdb = module->private_data;
430 struct ltdb_search_info *sinfo;
432 sinfo = talloc(ltdb, struct ltdb_search_info);
438 sinfo->module = module;
439 sinfo->scope = scope;
441 sinfo->attrs = attrs;
446 ret = tdb_traverse(ltdb->tdb, search_func, sinfo);
453 *res = talloc_steal(ltdb, sinfo->msgs);
454 count = sinfo->count;
463 search the database with a LDAP-like expression.
464 choses a search method
466 int ltdb_search(struct ldb_module *module, const char *base,
467 enum ldb_scope scope, const char *expression,
468 const char * const attrs[], struct ldb_message ***res)
470 struct ldb_context *ldb = module->ldb;
471 struct ltdb_private *ltdb = module->private_data;
472 struct ldb_parse_tree *tree;
475 if (ltdb_lock_read(module) != 0) {
479 ltdb->last_err_string = NULL;
481 if (ltdb_cache_load(module) != 0) {
482 ltdb_unlock_read(module);
488 /* form a parse tree for the expression */
489 tree = ldb_parse_tree(ldb, expression);
491 ltdb->last_err_string = "expression parse failed";
492 ltdb_unlock_read(module);
496 if (tree->operation == LDB_OP_SIMPLE &&
497 (ldb_attr_cmp(tree->u.simple.attr, "dn") == 0 ||
498 ldb_attr_cmp(tree->u.simple.attr, "distinguishedName") == 0) &&
499 !ltdb_has_wildcard(module, tree->u.simple.attr, &tree->u.simple.value)) {
500 /* yay! its a nice simple one */
501 ret = ltdb_search_dn(module, tree->u.simple.value.data, attrs, res);
503 ret = ltdb_search_indexed(module, base, scope, tree, attrs, res);
505 ret = ltdb_search_full(module, base, scope, tree, attrs, res);
509 ldb_parse_tree_free(ldb, tree);
510 ltdb_unlock_read(module);