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/ldb_tdb/ldb_tdb.h"
39 free a message that has all parts separately allocated
41 static void msg_free_all_parts(struct ldb_context *ldb, struct ldb_message *msg)
44 ldb_free(ldb, msg->dn);
45 for (i=0;i<msg->num_elements;i++) {
46 ldb_free(ldb, msg->elements[i].name);
47 for (j=0;j<msg->elements[i].num_values;j++) {
48 ldb_free(ldb, msg->elements[i].values[j].data);
50 ldb_free(ldb, msg->elements[i].values);
52 ldb_free(ldb, msg->elements);
58 duplicate a ldb_val structure
60 struct ldb_val ldb_val_dup(struct ldb_context *ldb,
61 const struct ldb_val *v)
64 v2.length = v->length;
70 /* the +1 is to cope with buggy C library routines like strndup
71 that look one byte beyond */
72 v2.data = ldb_malloc(ldb, v->length+1);
78 memcpy(v2.data, v->data, v->length);
79 ((char *)v2.data)[v->length] = 0;
86 add one element to a message
88 static int msg_add_element(struct ldb_context *ldb,
89 struct ldb_message *ret, const struct ldb_message_element *el)
92 struct ldb_message_element *e2, *elnew;
94 e2 = ldb_realloc_p(ldb, ret->elements, struct ldb_message_element, ret->num_elements+1);
100 elnew = &e2[ret->num_elements];
102 elnew->name = ldb_strdup(ldb, el->name);
107 if (el->num_values) {
108 elnew->values = ldb_malloc_array_p(ldb, struct ldb_val, el->num_values);
109 if (!elnew->values) {
113 elnew->values = NULL;
116 for (i=0;i<el->num_values;i++) {
117 elnew->values[i] = ldb_val_dup(ldb, &el->values[i]);
118 if (elnew->values[i].length != el->values[i].length) {
123 elnew->num_values = el->num_values;
131 add all elements from one message into another
133 static int msg_add_all_elements(struct ldb_context *ldb, struct ldb_message *ret,
134 const struct ldb_message *msg)
137 for (i=0;i<msg->num_elements;i++) {
138 if (msg_add_element(ldb, ret, &msg->elements[i]) != 0) {
148 pull the specified list of attributes from a message
150 static struct ldb_message *ltdb_pull_attrs(struct ldb_context *ldb,
151 const struct ldb_message *msg,
152 const char * const *attrs)
154 struct ldb_message *ret;
157 ret = ldb_malloc_p(ldb, struct ldb_message);
162 ret->dn = ldb_strdup(ldb, msg->dn);
168 ret->num_elements = 0;
169 ret->elements = NULL;
170 ret->private_data = NULL;
173 if (msg_add_all_elements(ldb, ret, msg) != 0) {
174 msg_free_all_parts(ldb, ret);
180 for (i=0;attrs[i];i++) {
181 struct ldb_message_element *el;
183 if (strcmp(attrs[i], "*") == 0) {
184 if (msg_add_all_elements(ldb, ret, msg) != 0) {
185 msg_free_all_parts(ldb, ret);
191 el = ldb_msg_find_element(msg, attrs[i]);
195 if (msg_add_element(ldb, ret, el) != 0) {
196 msg_free_all_parts(ldb, ret);
207 see if a ldb_val is a wildcard
208 return 1 if yes, 0 if no
210 int ltdb_has_wildcard(struct ldb_context *ldb, const char *attr_name,
211 const struct ldb_val *val)
215 /* all attribute types recognise the "*" wildcard */
216 if (val->length == 1 && strncmp((char *)val->data, "*", 1) == 0) {
220 if (strpbrk(val->data, "*?") == NULL) {
224 flags = ltdb_attribute_flags(ldb, attr_name);
225 if (flags & LTDB_FLAG_WILDCARD) {
234 free the results of a ltdb_search_dn1 search
236 void ltdb_search_dn1_free(struct ldb_context *ldb, struct ldb_message *msg)
239 ldb_free(ldb, msg->private_data);
240 for (i=0;i<msg->num_elements;i++) {
241 ldb_free(ldb, msg->elements[i].values);
243 ldb_free(ldb, msg->elements);
244 memset(msg, 0, sizeof(*msg));
249 search the database for a single simple dn, returning all attributes
252 return 1 on success, 0 on record-not-found and -1 on error
254 int ltdb_search_dn1(struct ldb_context *ldb, const char *dn, struct ldb_message *msg)
256 struct ltdb_private *ltdb = ldb->private_data;
258 TDB_DATA tdb_key, tdb_data, tdb_data2;
261 tdb_key = ltdb_key(ldb, dn);
266 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
267 ldb_free(ldb, tdb_key.dptr);
268 if (!tdb_data.dptr) {
272 tdb_data2.dptr = ldb_malloc(ldb, tdb_data.dsize);
273 if (!tdb_data2.dptr) {
277 memcpy(tdb_data2.dptr, tdb_data.dptr, tdb_data.dsize);
279 tdb_data2.dsize = tdb_data.dsize;
281 msg->private_data = tdb_data2.dptr;
282 msg->num_elements = 0;
283 msg->elements = NULL;
285 ret = ltdb_unpack_data(ldb, &tdb_data2, msg);
287 free(tdb_data2.dptr);
292 msg->dn = ldb_strdup(ldb, dn);
295 ldb_free(ldb, tdb_data2.dptr);
304 search the database for a single simple dn
306 int ltdb_search_dn(struct ldb_context *ldb, char *dn,
307 const char * const attrs[], struct ldb_message ***res)
310 struct ldb_message msg, *msg2;
312 ret = ltdb_search_dn1(ldb, dn, &msg);
317 msg2 = ltdb_pull_attrs(ldb, &msg, attrs);
319 ltdb_search_dn1_free(ldb, &msg);
325 *res = ldb_malloc_array_p(ldb, struct ldb_message *, 2);
327 msg_free_all_parts(ldb, msg2);
339 add a set of attributes from a record to a set of results
340 return 0 on success, -1 on failure
342 int ltdb_add_attr_results(struct ldb_context *ldb, struct ldb_message *msg,
343 const char * const attrs[],
345 struct ldb_message ***res)
347 struct ldb_message *msg2;
348 struct ldb_message **res2;
350 /* pull the attributes that the user wants */
351 msg2 = ltdb_pull_attrs(ldb, msg, attrs);
356 /* add to the results list */
357 res2 = ldb_realloc_p(ldb, *res, struct ldb_message *, (*count)+2);
359 msg_free_all_parts(ldb, msg2);
365 (*res)[*count] = msg2;
366 (*res)[(*count)+1] = NULL;
374 internal search state during a full db search
376 struct ltdb_search_info {
377 struct ldb_context *ldb;
378 struct ldb_parse_tree *tree;
380 enum ldb_scope scope;
381 const char * const *attrs;
382 struct ldb_message **msgs;
389 search function for a non-indexed search
391 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
393 struct ltdb_search_info *sinfo = state;
394 struct ldb_message msg;
398 strncmp(key.dptr, "DN=", 3) != 0) {
402 /* unpack the record */
403 ret = ltdb_unpack_data(sinfo->ldb, &data, &msg);
410 msg.dn = key.dptr + 3;
413 /* see if it matches the given expression */
414 if (!ldb_message_match(sinfo->ldb, &msg, sinfo->tree,
415 sinfo->base, sinfo->scope)) {
416 ltdb_unpack_data_free(sinfo->ldb, &msg);
420 ret = ltdb_add_attr_results(sinfo->ldb, &msg, sinfo->attrs, &sinfo->count, &sinfo->msgs);
426 ltdb_unpack_data_free(sinfo->ldb, &msg);
433 free a set of search results
435 int ltdb_search_free(struct ldb_context *ldb, struct ldb_message **msgs)
437 struct ltdb_private *ltdb = ldb->private_data;
440 ltdb->last_err_string = NULL;
444 for (i=0;msgs[i];i++) {
445 msg_free_all_parts(ldb, msgs[i]);
454 search the database with a LDAP-like expression.
455 this is the "full search" non-indexed varient
457 static int ltdb_search_full(struct ldb_context *ldb,
459 enum ldb_scope scope,
460 struct ldb_parse_tree *tree,
461 const char * const attrs[], struct ldb_message ***res)
463 struct ltdb_private *ltdb = ldb->private_data;
465 struct ltdb_search_info sinfo;
476 ret = tdb_traverse(ltdb->tdb, search_func, &sinfo);
479 ltdb_search_free(ldb, sinfo.msgs);
489 search the database with a LDAP-like expression.
490 choses a search method
492 int ltdb_search(struct ldb_context *ldb, const char *base,
493 enum ldb_scope scope, const char *expression,
494 const char * const attrs[], struct ldb_message ***res)
496 struct ltdb_private *ltdb = ldb->private_data;
497 struct ldb_parse_tree *tree;
500 ltdb->last_err_string = NULL;
502 if (ltdb_cache_load(ldb) != 0) {
508 /* form a parse tree for the expression */
509 tree = ldb_parse_tree(ldb, expression);
511 ltdb->last_err_string = "expression parse failed";
515 if (tree->operation == LDB_OP_SIMPLE &&
516 ldb_attr_cmp(tree->u.simple.attr, "dn") == 0 &&
517 !ltdb_has_wildcard(ldb, tree->u.simple.attr, &tree->u.simple.value)) {
518 /* yay! its a nice simple one */
519 ret = ltdb_search_dn(ldb, tree->u.simple.value.data, attrs, res);
521 ret = ltdb_search_indexed(ldb, base, scope, tree, attrs, res);
523 ret = ltdb_search_full(ldb, base, scope, tree, attrs, res);
527 ldb_parse_tree_free(ldb, tree);