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 3 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, see <http://www.gnu.org/licenses/>.
27 * Component: ldb search functions
29 * Description: functions to search ldb+tdb databases
31 * Author: Andrew Tridgell
37 add one element to a message
39 static int msg_add_element(struct ldb_message *ret,
40 const struct ldb_message_element *el,
44 struct ldb_message_element *e2, *elnew;
46 if (check_duplicates && ldb_msg_find_element(ret, el->name)) {
47 /* its already there */
51 e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
57 elnew = &e2[ret->num_elements];
59 elnew->name = talloc_strdup(ret->elements, el->name);
65 elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
73 for (i=0;i<el->num_values;i++) {
74 elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
75 if (elnew->values[i].length != el->values[i].length) {
80 elnew->num_values = el->num_values;
88 add the special distinguishedName element
90 static int msg_add_distinguished_name(struct ldb_message *msg)
92 struct ldb_message_element el;
97 el.name = "distinguishedName";
100 val.data = (uint8_t *)ldb_dn_alloc_linearized(msg, msg->dn);
101 val.length = strlen((char *)val.data);
103 ret = msg_add_element(msg, &el, 1);
108 add all elements from one message into another
110 static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *ret,
111 const struct ldb_message *msg)
113 struct ldb_context *ldb;
115 int check_duplicates = (ret->num_elements != 0);
117 ldb = ldb_module_get_ctx(module);
119 if (msg_add_distinguished_name(ret) != 0) {
123 for (i=0;i<msg->num_elements;i++) {
124 const struct ldb_schema_attribute *a;
125 a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);
126 if (a->flags & LDB_ATTR_FLAG_HIDDEN) {
129 if (msg_add_element(ret, &msg->elements[i],
130 check_duplicates) != 0) {
140 pull the specified list of attributes from a message
142 static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module,
144 const struct ldb_message *msg,
145 const char * const *attrs)
147 struct ldb_message *ret;
150 ret = talloc(mem_ctx, struct ldb_message);
155 ret->dn = ldb_dn_copy(ret, msg->dn);
161 ret->num_elements = 0;
162 ret->elements = NULL;
165 if (msg_add_all_elements(module, ret, msg) != 0) {
172 for (i=0;attrs[i];i++) {
173 struct ldb_message_element *el;
175 if (strcmp(attrs[i], "*") == 0) {
176 if (msg_add_all_elements(module, ret, msg) != 0) {
183 if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
184 if (msg_add_distinguished_name(ret) != 0) {
190 el = ldb_msg_find_element(msg, attrs[i]);
194 if (msg_add_element(ret, el, 1) != 0) {
204 search the database for a single simple dn.
205 return LDB_ERR_NO_SUCH_OBJECT on record-not-found
206 and LDB_SUCCESS on success
208 static int ltdb_search_base(struct ldb_module *module, struct ldb_dn *dn)
210 void *data = ldb_module_get_private(module);
211 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
212 TDB_DATA tdb_key, tdb_data;
214 if (ldb_dn_is_null(dn)) {
215 return LDB_ERR_NO_SUCH_OBJECT;
219 tdb_key = ltdb_key(module, dn);
221 return LDB_ERR_OPERATIONS_ERROR;
224 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
225 talloc_free(tdb_key.dptr);
226 if (!tdb_data.dptr) {
227 return LDB_ERR_NO_SUCH_OBJECT;
235 search the database for a single simple dn, returning all attributes
238 return LDB_ERR_NO_SUCH_OBJECT on record-not-found
239 and LDB_SUCCESS on success
241 int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg)
243 void *data = ldb_module_get_private(module);
244 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
246 TDB_DATA tdb_key, tdb_data;
248 memset(msg, 0, sizeof(*msg));
251 tdb_key = ltdb_key(module, dn);
253 return LDB_ERR_OPERATIONS_ERROR;
256 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
257 talloc_free(tdb_key.dptr);
258 if (!tdb_data.dptr) {
259 return LDB_ERR_NO_SUCH_OBJECT;
262 msg->num_elements = 0;
263 msg->elements = NULL;
265 ret = ltdb_unpack_data(module, &tdb_data, msg);
268 return LDB_ERR_OPERATIONS_ERROR;
272 msg->dn = ldb_dn_copy(msg, dn);
275 return LDB_ERR_OPERATIONS_ERROR;
282 add a set of attributes from a record to a set of results
283 return 0 on success, -1 on failure
285 int ltdb_add_attr_results(struct ldb_module *module,
287 struct ldb_message *msg,
288 const char * const attrs[],
290 struct ldb_message ***res)
292 struct ldb_message *msg2;
293 struct ldb_message **res2;
295 /* pull the attributes that the user wants */
296 msg2 = ltdb_pull_attrs(module, mem_ctx, msg, attrs);
301 /* add to the results list */
302 res2 = talloc_realloc(mem_ctx, *res, struct ldb_message *, (*count)+2);
310 (*res)[*count] = talloc_move(*res, &msg2);
311 (*res)[(*count)+1] = NULL;
320 filter the specified list of attributes from a message
321 removing not requested attrs.
323 int ltdb_filter_attrs(struct ldb_message *msg, const char * const *attrs)
328 /* check for special attrs */
329 for (i = 0; attrs[i]; i++) {
330 if (strcmp(attrs[i], "*") == 0) {
335 if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
336 if (msg_add_distinguished_name(msg) != 0) {
346 if (msg_add_distinguished_name(msg) != 0) {
352 for (i = 0; i < msg->num_elements; i++) {
355 for (j = 0, found = 0; attrs[j]; j++) {
356 if (ldb_attr_cmp(msg->elements[i].name, attrs[j]) == 0) {
363 ldb_msg_remove_attr(msg, msg->elements[i].name);
372 search function for a non-indexed search
374 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
376 struct ldb_context *ldb;
377 struct ltdb_context *ac;
378 struct ldb_message *msg;
381 ac = talloc_get_type(state, struct ltdb_context);
382 ldb = ldb_module_get_ctx(ac->module);
385 strncmp((char *)key.dptr, "DN=", 3) != 0) {
389 msg = ldb_msg_new(ac);
394 /* unpack the record */
395 ret = ltdb_unpack_data(ac->module, &data, msg);
402 msg->dn = ldb_dn_new(msg, ldb,
403 (char *)key.dptr + 3);
404 if (msg->dn == NULL) {
410 /* see if it matches the given expression */
411 if (!ldb_match_msg(ldb, msg,
412 ac->tree, ac->base, ac->scope)) {
417 /* filter the attributes that the user wants */
418 ret = ltdb_filter_attrs(msg, ac->attrs);
425 ret = ldb_module_send_entry(ac->req, msg, NULL);
426 if (ret != LDB_SUCCESS) {
427 ac->callback_failed = true;
428 /* the callback failed, abort the operation */
437 search the database with a LDAP-like expression.
438 this is the "full search" non-indexed variant
440 static int ltdb_search_full(struct ltdb_context *ctx)
442 void *data = ldb_module_get_private(ctx->module);
443 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
446 if (ltdb->in_transaction != 0) {
447 ret = tdb_traverse(ltdb->tdb, search_func, ctx);
449 ret = tdb_traverse_read(ltdb->tdb, search_func, ctx);
453 return LDB_ERR_OPERATIONS_ERROR;
460 search the database with a LDAP-like expression.
461 choses a search method
463 int ltdb_search(struct ltdb_context *ctx)
465 struct ldb_context *ldb;
466 struct ldb_module *module = ctx->module;
467 struct ldb_request *req = ctx->req;
468 void *data = ldb_module_get_private(module);
469 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
472 ldb = ldb_module_get_ctx(module);
474 ldb_request_set_state(req, LDB_ASYNC_PENDING);
476 if (ltdb_lock_read(module) != 0) {
477 return LDB_ERR_OPERATIONS_ERROR;
480 if (ltdb_cache_load(module) != 0) {
481 ltdb_unlock_read(module);
482 return LDB_ERR_OPERATIONS_ERROR;
485 if (req->op.search.tree == NULL) {
486 ltdb_unlock_read(module);
487 return LDB_ERR_OPERATIONS_ERROR;
490 if ((req->op.search.base == NULL) || (ldb_dn_is_null(req->op.search.base) == true)) {
492 /* Check what we should do with a NULL dn */
493 switch (req->op.search.scope) {
495 ldb_asprintf_errstring(ldb,
496 "NULL Base DN invalid for a base search");
497 ret = LDB_ERR_INVALID_DN_SYNTAX;
499 case LDB_SCOPE_ONELEVEL:
500 ldb_asprintf_errstring(ldb,
501 "NULL Base DN invalid for a one-level search");
502 ret = LDB_ERR_INVALID_DN_SYNTAX;
504 case LDB_SCOPE_SUBTREE:
506 /* We accept subtree searches from a NULL base DN, ie over the whole DB */
509 } else if (ldb_dn_is_valid(req->op.search.base) == false) {
511 /* We don't want invalid base DNs here */
512 ldb_asprintf_errstring(ldb,
513 "Invalid Base DN: %s",
514 ldb_dn_get_linearized(req->op.search.base));
515 ret = LDB_ERR_INVALID_DN_SYNTAX;
517 } else if (ltdb->check_base) {
518 /* This database has been marked as 'checkBaseOnSearch', so do a spot check of the base dn */
519 ret = ltdb_search_base(module, req->op.search.base);
521 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
522 ldb_asprintf_errstring(ldb,
523 "No such Base DN: %s",
524 ldb_dn_get_linearized(req->op.search.base));
528 /* If we are not checking the base DN life is easy */
532 ctx->tree = req->op.search.tree;
533 ctx->scope = req->op.search.scope;
534 ctx->base = req->op.search.base;
535 ctx->attrs = req->op.search.attrs;
537 if (ret == LDB_SUCCESS) {
538 ret = ltdb_search_indexed(ctx);
539 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
540 /* Not in the index, therefore OK! */
544 /* Check if we got just a normal error.
545 * In that case proceed to a full search unless we got a
547 if ( ! ctx->callback_failed && ret != LDB_SUCCESS) {
548 /* Not indexed, so we need to do a full scan */
549 ret = ltdb_search_full(ctx);
550 if (ret != LDB_SUCCESS) {
551 ldb_set_errstring(ldb, "Indexed and full searches both failed!\n");
556 ltdb_unlock_read(module);