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_message *msg)
44 if (msg->dn) free(msg->dn);
45 for (i=0;i<msg->num_elements;i++) {
46 if (msg->elements[i].name) free(msg->elements[i].name);
47 for (j=0;j<msg->elements[i].num_values;j++) {
48 if (msg->elements[i].values[j].data)
49 free(msg->elements[i].values[j].data);
51 if (msg->elements[i].values) free(msg->elements[i].values);
59 duplicate a ldb_val structure
61 struct ldb_val ldb_val_dup(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 = malloc(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_message *ret, const struct ldb_message_element *el)
91 struct ldb_message_element *e2, *elnew;
93 e2 = realloc_p(ret->elements, struct ldb_message_element, ret->num_elements+1);
99 elnew = &e2[ret->num_elements];
101 elnew->name = strdup(el->name);
106 if (el->num_values) {
107 elnew->values = malloc_array_p(struct ldb_val, el->num_values);
108 if (!elnew->values) {
112 elnew->values = NULL;
115 for (i=0;i<el->num_values;i++) {
116 elnew->values[i] = ldb_val_dup(&el->values[i]);
117 if (elnew->values[i].length != el->values[i].length) {
122 elnew->num_values = el->num_values;
130 add all elements from one message into another
132 static int msg_add_all_elements(struct ldb_message *ret,
133 const struct ldb_message *msg)
136 for (i=0;i<msg->num_elements;i++) {
137 if (msg_add_element(ret, &msg->elements[i]) != 0) {
147 pull the specified list of attributes from a message
149 static struct ldb_message *ltdb_pull_attrs(struct ldb_context *ldb,
150 const struct ldb_message *msg,
153 struct ldb_message *ret;
156 ret = malloc_p(struct ldb_message);
161 ret->dn = strdup(msg->dn);
167 ret->num_elements = 0;
168 ret->elements = NULL;
169 ret->private_data = NULL;
172 if (msg_add_all_elements(ret, msg) != 0) {
173 msg_free_all_parts(ret);
179 for (i=0;attrs[i];i++) {
180 struct ldb_message_element *el;
182 if (strcmp(attrs[i], "*") == 0) {
183 if (msg_add_all_elements(ret, msg) != 0) {
184 msg_free_all_parts(ret);
190 el = ldb_msg_find_element(msg, attrs[i]);
194 if (msg_add_element(ret, el) != 0) {
195 msg_free_all_parts(ret);
206 see if a ldb_val is a wildcard
208 int ltdb_has_wildcard(const struct ldb_val *val)
210 if (val->length == 1 && ((char *)val->data)[0] == '*') {
218 free the results of a ltdb_search_dn1 search
220 void ltdb_search_dn1_free(struct ldb_context *ldb, struct ldb_message *msg)
223 if (msg->dn) free(msg->dn);
224 if (msg->private_data) free(msg->private_data);
225 for (i=0;i<msg->num_elements;i++) {
226 if (msg->elements[i].values) free(msg->elements[i].values);
228 if (msg->elements) free(msg->elements);
233 search the database for a single simple dn, returning all attributes
236 return 1 on success, 0 on record-not-found and -1 on error
238 int ltdb_search_dn1(struct ldb_context *ldb, const char *dn, struct ldb_message *msg)
240 struct ltdb_private *ltdb = ldb->private_data;
242 TDB_DATA tdb_key, tdb_data;
245 tdb_key = ltdb_key(dn);
250 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
252 if (!tdb_data.dptr) {
256 msg->dn = strdup(dn);
261 msg->private_data = tdb_data.dptr;
262 msg->num_elements = 0;
263 msg->elements = NULL;
265 ret = ltdb_unpack_data(ldb, &tdb_data, msg);
276 search the database for a single simple dn
278 int ltdb_search_dn(struct ldb_context *ldb, char *dn,
279 const char *attrs[], struct ldb_message ***res)
282 struct ldb_message msg, *msg2;
284 ret = ltdb_search_dn1(ldb, dn, &msg);
289 msg2 = ltdb_pull_attrs(ldb, &msg, attrs);
291 ltdb_search_dn1_free(ldb, &msg);
297 *res = malloc_array_p(struct ldb_message *, 2);
299 msg_free_all_parts(msg2);
311 add a set of attributes from a record to a set of results
312 return 0 on success, -1 on failure
314 int ltdb_add_attr_results(struct ldb_context *ldb, struct ldb_message *msg,
317 struct ldb_message ***res)
319 struct ldb_message *msg2;
320 struct ldb_message **res2;
322 /* pull the attributes that the user wants */
323 msg2 = ltdb_pull_attrs(ldb, msg, attrs);
328 /* add to the results list */
329 res2 = realloc_p(*res, struct ldb_message *, (*count)+2);
331 msg_free_all_parts(msg2);
337 (*res)[*count] = msg2;
338 (*res)[(*count)+1] = NULL;
346 internal search state during a full db search
348 struct ltdb_search_info {
349 struct ldb_context *ldb;
350 struct ldb_parse_tree *tree;
352 enum ldb_scope scope;
354 struct ldb_message **msgs;
361 search function for a non-indexed search
363 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
365 struct ltdb_search_info *sinfo = state;
366 struct ldb_message msg;
370 strncmp(key.dptr, "DN=", 3) != 0) {
374 msg.dn = key.dptr + 3;
376 /* unpack the record */
377 ret = ltdb_unpack_data(sinfo->ldb, &data, &msg);
383 /* see if it matches the given expression */
384 if (!ldb_message_match(sinfo->ldb, &msg, sinfo->tree,
385 sinfo->base, sinfo->scope)) {
386 ltdb_unpack_data_free(&msg);
390 ret = ltdb_add_attr_results(sinfo->ldb, &msg, sinfo->attrs, &sinfo->count, &sinfo->msgs);
396 ltdb_unpack_data_free(&msg);
403 free a set of search results
405 int ltdb_search_free(struct ldb_context *ldb, struct ldb_message **msgs)
411 for (i=0;msgs[i];i++) {
412 msg_free_all_parts(msgs[i]);
421 search the database with a LDAP-like expression.
422 this is the "full search" non-indexed varient
424 static int ltdb_search_full(struct ldb_context *ldb,
426 enum ldb_scope scope,
427 struct ldb_parse_tree *tree,
428 const char *attrs[], struct ldb_message ***res)
430 struct ltdb_private *ltdb = ldb->private_data;
432 struct ltdb_search_info sinfo;
443 ret = tdb_traverse(ltdb->tdb, search_func, &sinfo);
446 ltdb_search_free(ldb, sinfo.msgs);
456 search the database with a LDAP-like expression.
457 choses a search method
459 int ltdb_search(struct ldb_context *ldb, const char *base,
460 enum ldb_scope scope, const char *expression,
461 const char *attrs[], struct ldb_message ***res)
463 struct ldb_parse_tree *tree;
468 /* form a parse tree for the expression */
469 tree = ldb_parse_tree(expression);
474 if (tree->operation == LDB_OP_SIMPLE &&
475 strcmp(tree->u.simple.attr, "dn") == 0 &&
476 !ltdb_has_wildcard(&tree->u.simple.value)) {
477 /* yay! its a nice simple one */
478 ret = ltdb_search_dn(ldb, tree->u.simple.value.data, attrs, res);
480 ret = ltdb_search_indexed(ldb, base, scope, tree, attrs, res);
482 ret = ltdb_search_full(ldb, base, scope, tree, attrs, res);
486 ldb_parse_tree_free(tree);