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 int flags = ltdb_attribute_flags(ldb, msg->elements[i].name);
139 if (flags & LTDB_FLAG_HIDDEN) {
142 if (msg_add_element(ldb, ret, &msg->elements[i]) != 0) {
152 pull the specified list of attributes from a message
154 static struct ldb_message *ltdb_pull_attrs(struct ldb_context *ldb,
155 const struct ldb_message *msg,
156 const char * const *attrs)
158 struct ldb_message *ret;
161 ret = ldb_malloc_p(ldb, struct ldb_message);
166 ret->dn = ldb_strdup(ldb, msg->dn);
172 ret->num_elements = 0;
173 ret->elements = NULL;
174 ret->private_data = NULL;
177 if (msg_add_all_elements(ldb, ret, msg) != 0) {
178 msg_free_all_parts(ldb, ret);
184 for (i=0;attrs[i];i++) {
185 struct ldb_message_element *el;
187 if (strcmp(attrs[i], "*") == 0) {
188 if (msg_add_all_elements(ldb, ret, msg) != 0) {
189 msg_free_all_parts(ldb, ret);
195 el = ldb_msg_find_element(msg, attrs[i]);
199 if (msg_add_element(ldb, ret, el) != 0) {
200 msg_free_all_parts(ldb, ret);
211 see if a ldb_val is a wildcard
212 return 1 if yes, 0 if no
214 int ltdb_has_wildcard(struct ldb_context *ldb, const char *attr_name,
215 const struct ldb_val *val)
219 /* all attribute types recognise the "*" wildcard */
220 if (val->length == 1 && strncmp((char *)val->data, "*", 1) == 0) {
224 if (strpbrk(val->data, "*?") == NULL) {
228 flags = ltdb_attribute_flags(ldb, attr_name);
229 if (flags & LTDB_FLAG_WILDCARD) {
238 free the results of a ltdb_search_dn1 search
240 void ltdb_search_dn1_free(struct ldb_context *ldb, struct ldb_message *msg)
243 ldb_free(ldb, msg->private_data);
244 for (i=0;i<msg->num_elements;i++) {
245 ldb_free(ldb, msg->elements[i].values);
247 ldb_free(ldb, msg->elements);
248 memset(msg, 0, sizeof(*msg));
253 search the database for a single simple dn, returning all attributes
256 return 1 on success, 0 on record-not-found and -1 on error
258 int ltdb_search_dn1(struct ldb_context *ldb, const char *dn, struct ldb_message *msg)
260 struct ltdb_private *ltdb = ldb->private_data;
262 TDB_DATA tdb_key, tdb_data, tdb_data2;
264 memset(msg, 0, sizeof(*msg));
267 tdb_key = ltdb_key(ldb, dn);
272 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
273 ldb_free(ldb, tdb_key.dptr);
274 if (!tdb_data.dptr) {
278 tdb_data2.dptr = ldb_malloc(ldb, tdb_data.dsize);
279 if (!tdb_data2.dptr) {
283 memcpy(tdb_data2.dptr, tdb_data.dptr, tdb_data.dsize);
285 tdb_data2.dsize = tdb_data.dsize;
287 msg->private_data = tdb_data2.dptr;
288 msg->num_elements = 0;
289 msg->elements = NULL;
291 ret = ltdb_unpack_data(ldb, &tdb_data2, msg);
293 free(tdb_data2.dptr);
298 msg->dn = ldb_strdup(ldb, dn);
301 ldb_free(ldb, tdb_data2.dptr);
310 search the database for a single simple dn
312 int ltdb_search_dn(struct ldb_context *ldb, char *dn,
313 const char * const attrs[], struct ldb_message ***res)
316 struct ldb_message msg, *msg2;
318 ret = ltdb_search_dn1(ldb, dn, &msg);
323 msg2 = ltdb_pull_attrs(ldb, &msg, attrs);
325 ltdb_search_dn1_free(ldb, &msg);
331 *res = ldb_malloc_array_p(ldb, struct ldb_message *, 2);
333 msg_free_all_parts(ldb, msg2);
345 add a set of attributes from a record to a set of results
346 return 0 on success, -1 on failure
348 int ltdb_add_attr_results(struct ldb_context *ldb, struct ldb_message *msg,
349 const char * const attrs[],
351 struct ldb_message ***res)
353 struct ldb_message *msg2;
354 struct ldb_message **res2;
356 /* pull the attributes that the user wants */
357 msg2 = ltdb_pull_attrs(ldb, msg, attrs);
362 /* add to the results list */
363 res2 = ldb_realloc_p(ldb, *res, struct ldb_message *, (*count)+2);
365 msg_free_all_parts(ldb, msg2);
371 (*res)[*count] = msg2;
372 (*res)[(*count)+1] = NULL;
380 internal search state during a full db search
382 struct ltdb_search_info {
383 struct ldb_context *ldb;
384 struct ldb_parse_tree *tree;
386 enum ldb_scope scope;
387 const char * const *attrs;
388 struct ldb_message **msgs;
395 search function for a non-indexed search
397 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
399 struct ltdb_search_info *sinfo = state;
400 struct ldb_message msg;
404 strncmp(key.dptr, "DN=", 3) != 0) {
408 /* unpack the record */
409 ret = ltdb_unpack_data(sinfo->ldb, &data, &msg);
416 msg.dn = key.dptr + 3;
419 /* see if it matches the given expression */
420 if (!ldb_message_match(sinfo->ldb, &msg, sinfo->tree,
421 sinfo->base, sinfo->scope)) {
422 ltdb_unpack_data_free(sinfo->ldb, &msg);
426 ret = ltdb_add_attr_results(sinfo->ldb, &msg, sinfo->attrs, &sinfo->count, &sinfo->msgs);
432 ltdb_unpack_data_free(sinfo->ldb, &msg);
439 free a set of search results
441 int ltdb_search_free(struct ldb_context *ldb, struct ldb_message **msgs)
443 struct ltdb_private *ltdb = ldb->private_data;
446 ltdb->last_err_string = NULL;
450 for (i=0;msgs[i];i++) {
451 msg_free_all_parts(ldb, msgs[i]);
460 search the database with a LDAP-like expression.
461 this is the "full search" non-indexed varient
463 static int ltdb_search_full(struct ldb_context *ldb,
465 enum ldb_scope scope,
466 struct ldb_parse_tree *tree,
467 const char * const attrs[], struct ldb_message ***res)
469 struct ltdb_private *ltdb = ldb->private_data;
471 struct ltdb_search_info sinfo;
482 ret = tdb_traverse(ltdb->tdb, search_func, &sinfo);
485 ltdb_search_free(ldb, sinfo.msgs);
495 search the database with a LDAP-like expression.
496 choses a search method
498 int ltdb_search(struct ldb_context *ldb, const char *base,
499 enum ldb_scope scope, const char *expression,
500 const char * const attrs[], struct ldb_message ***res)
502 struct ltdb_private *ltdb = ldb->private_data;
503 struct ldb_parse_tree *tree;
506 ltdb->last_err_string = NULL;
508 if (ltdb_cache_load(ldb) != 0) {
514 /* form a parse tree for the expression */
515 tree = ldb_parse_tree(ldb, expression);
517 ltdb->last_err_string = "expression parse failed";
521 if (tree->operation == LDB_OP_SIMPLE &&
522 ldb_attr_cmp(tree->u.simple.attr, "dn") == 0 &&
523 !ltdb_has_wildcard(ldb, tree->u.simple.attr, &tree->u.simple.value)) {
524 /* yay! its a nice simple one */
525 ret = ltdb_search_dn(ldb, tree->u.simple.value.data, attrs, res);
527 ret = ltdb_search_indexed(ldb, base, scope, tree, attrs, res);
529 ret = ltdb_search_full(ldb, base, scope, tree, attrs, res);
533 ldb_parse_tree_free(ldb, tree);