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 free a message that has all parts separately allocated
44 static void msg_free_all_parts(struct ldb_context *ldb, struct ldb_message *msg)
47 ldb_free(ldb, msg->dn);
48 for (i=0;i<msg->num_elements;i++) {
49 ldb_free(ldb, msg->elements[i].name);
50 for (j=0;j<msg->elements[i].num_values;j++) {
51 ldb_free(ldb, msg->elements[i].values[j].data);
53 ldb_free(ldb, msg->elements[i].values);
55 ldb_free(ldb, msg->elements);
61 add one element to a message
63 static int msg_add_element(struct ldb_context *ldb,
64 struct ldb_message *ret, const struct ldb_message_element *el)
67 struct ldb_message_element *e2, *elnew;
69 e2 = ldb_realloc_p(ldb, ret->elements, struct ldb_message_element, ret->num_elements+1);
75 elnew = &e2[ret->num_elements];
77 elnew->name = ldb_strdup(ldb, el->name);
83 elnew->values = ldb_malloc_array_p(ldb, struct ldb_val, el->num_values);
91 for (i=0;i<el->num_values;i++) {
92 elnew->values[i] = ldb_val_dup(ldb, &el->values[i]);
93 if (elnew->values[i].length != el->values[i].length) {
98 elnew->num_values = el->num_values;
106 add all elements from one message into another
108 static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *ret,
109 const struct ldb_message *msg)
111 struct ldb_context *ldb = module->ldb;
114 for (i=0;i<msg->num_elements;i++) {
115 int flags = ltdb_attribute_flags(module, msg->elements[i].name);
116 if ((msg->dn[0] != '@') && (flags & LTDB_FLAG_HIDDEN)) {
119 if (msg_add_element(ldb, ret, &msg->elements[i]) != 0) {
129 pull the specified list of attributes from a message
131 static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module,
132 const struct ldb_message *msg,
133 const char * const *attrs)
135 struct ldb_context *ldb = module->ldb;
136 struct ldb_message *ret;
139 ret = ldb_malloc_p(ldb, struct ldb_message);
144 ret->dn = ldb_strdup(ldb, msg->dn);
150 ret->num_elements = 0;
151 ret->elements = NULL;
152 ret->private_data = NULL;
155 if (msg_add_all_elements(module, ret, msg) != 0) {
156 msg_free_all_parts(ldb, ret);
162 for (i=0;attrs[i];i++) {
163 struct ldb_message_element *el;
165 if (strcmp(attrs[i], "*") == 0) {
166 if (msg_add_all_elements(module, ret, msg) != 0) {
167 msg_free_all_parts(ldb, ret);
173 if (ldb_attr_cmp(attrs[i], "dn") == 0) {
174 struct ldb_message_element el2;
178 el2.name = ldb_strdup(ldb, "dn");
180 msg_free_all_parts(ldb, ret);
181 ldb_free(ldb, el2.name);
187 val.length = strlen(ret->dn);
189 if (msg_add_element(ldb, ret, &el2) != 0) {
190 msg_free_all_parts(ldb, ret);
191 ldb_free(ldb, el2.name);
194 ldb_free(ldb, el2.name);
198 el = ldb_msg_find_element(msg, attrs[i]);
202 if (msg_add_element(ldb, ret, el) != 0) {
203 msg_free_all_parts(ldb, ret);
214 see if a ldb_val is a wildcard
215 return 1 if yes, 0 if no
217 int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name,
218 const struct ldb_val *val)
222 /* all attribute types recognise the "*" wildcard */
223 if (val->length == 1 && strncmp((char *)val->data, "*", 1) == 0) {
227 if (strpbrk(val->data, "*?") == NULL) {
231 flags = ltdb_attribute_flags(module, attr_name);
232 if (flags & LTDB_FLAG_WILDCARD) {
241 free the results of a ltdb_search_dn1 search
243 void ltdb_search_dn1_free(struct ldb_module *module, struct ldb_message *msg)
245 struct ldb_context *ldb = module->ldb;
247 ldb_free(ldb, msg->private_data);
248 for (i=0;i<msg->num_elements;i++) {
249 ldb_free(ldb, msg->elements[i].values);
251 ldb_free(ldb, msg->elements);
252 memset(msg, 0, sizeof(*msg));
257 search the database for a single simple dn, returning all attributes
260 return 1 on success, 0 on record-not-found and -1 on error
262 int ltdb_search_dn1(struct ldb_module *module, const char *dn, struct ldb_message *msg)
264 struct ldb_context *ldb = module->ldb;
265 struct ltdb_private *ltdb = module->private_data;
267 TDB_DATA tdb_key, tdb_data, tdb_data2;
269 memset(msg, 0, sizeof(*msg));
272 tdb_key = ltdb_key(module, dn);
277 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
278 ldb_free(ldb, tdb_key.dptr);
279 if (!tdb_data.dptr) {
283 tdb_data2.dptr = ldb_malloc(ldb, tdb_data.dsize);
284 if (!tdb_data2.dptr) {
288 memcpy(tdb_data2.dptr, tdb_data.dptr, tdb_data.dsize);
290 tdb_data2.dsize = tdb_data.dsize;
292 msg->private_data = tdb_data2.dptr;
293 msg->num_elements = 0;
294 msg->elements = NULL;
296 ret = ltdb_unpack_data(module, &tdb_data2, msg);
298 ldb_free(ldb, tdb_data2.dptr);
303 msg->dn = ldb_strdup(ldb, dn);
306 ldb_free(ldb, tdb_data2.dptr);
315 search the database for a single simple dn
317 int ltdb_search_dn(struct ldb_module *module, char *dn,
318 const char * const attrs[], struct ldb_message ***res)
320 struct ldb_context *ldb = module->ldb;
322 struct ldb_message msg, *msg2;
324 ret = ltdb_search_dn1(module, dn, &msg);
329 msg2 = ltdb_pull_attrs(module, &msg, attrs);
331 ltdb_search_dn1_free(module, &msg);
337 *res = ldb_malloc_array_p(ldb, struct ldb_message *, 2);
339 msg_free_all_parts(ldb, msg2);
351 add a set of attributes from a record to a set of results
352 return 0 on success, -1 on failure
354 int ltdb_add_attr_results(struct ldb_module *module, struct ldb_message *msg,
355 const char * const attrs[],
357 struct ldb_message ***res)
359 struct ldb_context *ldb = module->ldb;
360 struct ldb_message *msg2;
361 struct ldb_message **res2;
363 /* pull the attributes that the user wants */
364 msg2 = ltdb_pull_attrs(module, msg, attrs);
369 /* add to the results list */
370 res2 = ldb_realloc_p(ldb, *res, struct ldb_message *, (*count)+2);
372 msg_free_all_parts(ldb, msg2);
378 (*res)[*count] = msg2;
379 (*res)[(*count)+1] = NULL;
387 internal search state during a full db search
389 struct ltdb_search_info {
390 struct ldb_module *module;
391 struct ldb_parse_tree *tree;
393 enum ldb_scope scope;
394 const char * const *attrs;
395 struct ldb_message **msgs;
402 search function for a non-indexed search
404 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
406 struct ltdb_search_info *sinfo = state;
407 struct ldb_message msg;
411 strncmp(key.dptr, "DN=", 3) != 0) {
415 /* unpack the record */
416 ret = ltdb_unpack_data(sinfo->module, &data, &msg);
423 msg.dn = key.dptr + 3;
426 /* see if it matches the given expression */
427 if (!ltdb_message_match(sinfo->module, &msg, sinfo->tree,
428 sinfo->base, sinfo->scope)) {
429 ltdb_unpack_data_free(sinfo->module, &msg);
433 ret = ltdb_add_attr_results(sinfo->module, &msg, sinfo->attrs, &sinfo->count, &sinfo->msgs);
439 ltdb_unpack_data_free(sinfo->module, &msg);
446 free a set of search results
448 int ltdb_search_free(struct ldb_module *module, struct ldb_message **msgs)
450 struct ldb_context *ldb = module->ldb;
451 struct ltdb_private *ltdb = module->private_data;
454 ltdb->last_err_string = NULL;
458 for (i=0;msgs[i];i++) {
459 msg_free_all_parts(ldb, msgs[i]);
468 search the database with a LDAP-like expression.
469 this is the "full search" non-indexed variant
471 static int ltdb_search_full(struct ldb_module *module,
473 enum ldb_scope scope,
474 struct ldb_parse_tree *tree,
475 const char * const attrs[], struct ldb_message ***res)
477 struct ltdb_private *ltdb = module->private_data;
479 struct ltdb_search_info sinfo;
482 sinfo.module = module;
490 ret = tdb_traverse(ltdb->tdb, search_func, &sinfo);
493 ltdb_search_free(module, sinfo.msgs);
503 search the database with a LDAP-like expression.
504 choses a search method
506 int ltdb_search(struct ldb_module *module, const char *base,
507 enum ldb_scope scope, const char *expression,
508 const char * const attrs[], struct ldb_message ***res)
510 struct ldb_context *ldb = module->ldb;
511 struct ltdb_private *ltdb = module->private_data;
512 struct ldb_parse_tree *tree;
515 ltdb->last_err_string = NULL;
517 if (ltdb_cache_load(module) != 0) {
523 /* form a parse tree for the expression */
524 tree = ldb_parse_tree(ldb, expression);
526 ltdb->last_err_string = "expression parse failed";
530 if (tree->operation == LDB_OP_SIMPLE &&
531 ldb_attr_cmp(tree->u.simple.attr, "dn") == 0 &&
532 !ltdb_has_wildcard(module, tree->u.simple.attr, &tree->u.simple.value)) {
533 /* yay! its a nice simple one */
534 ret = ltdb_search_dn(module, tree->u.simple.value.data, attrs, res);
536 ret = ltdb_search_indexed(module, base, scope, tree, attrs, res);
538 ret = ltdb_search_full(module, base, scope, tree, attrs, res);
542 ldb_parse_tree_free(ldb, tree);