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
34 #include "ldb_includes.h"
39 add one element to a message
41 static int msg_add_element(struct ldb_message *ret,
42 const struct ldb_message_element *el,
46 struct ldb_message_element *e2, *elnew;
48 if (check_duplicates && ldb_msg_find_element(ret, el->name)) {
49 /* its already there */
53 e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
59 elnew = &e2[ret->num_elements];
61 elnew->name = talloc_strdup(ret->elements, el->name);
67 elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
75 for (i=0;i<el->num_values;i++) {
76 elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
77 if (elnew->values[i].length != el->values[i].length) {
82 elnew->num_values = el->num_values;
90 add the special distinguishedName element
92 static int msg_add_distinguished_name(struct ldb_message *msg)
94 struct ldb_message_element el;
99 el.name = "distinguishedName";
102 val.data = (uint8_t *)ldb_dn_alloc_linearized(msg, msg->dn);
103 val.length = strlen((char *)val.data);
105 ret = msg_add_element(msg, &el, 1);
110 add all elements from one message into another
112 static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *ret,
113 const struct ldb_message *msg)
115 struct ldb_context *ldb = module->ldb;
117 int check_duplicates = (ret->num_elements != 0);
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) {
205 search the database for a single simple dn, returning all attributes
208 return LDB_ERR_NO_SUCH_OBJECT on record-not-found
209 and LDB_SUCCESS on success
211 int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg)
213 struct ltdb_private *ltdb = module->private_data;
215 TDB_DATA tdb_key, tdb_data;
217 memset(msg, 0, sizeof(*msg));
220 tdb_key = ltdb_key(module, dn);
222 return LDB_ERR_OPERATIONS_ERROR;
225 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
226 talloc_free(tdb_key.dptr);
227 if (!tdb_data.dptr) {
228 return LDB_ERR_NO_SUCH_OBJECT;
231 msg->num_elements = 0;
232 msg->elements = NULL;
234 ret = ltdb_unpack_data(module, &tdb_data, msg);
237 return LDB_ERR_OPERATIONS_ERROR;
241 msg->dn = ldb_dn_copy(msg, dn);
244 return LDB_ERR_OPERATIONS_ERROR;
251 lock the database for read - use by ltdb_search
253 static int ltdb_lock_read(struct ldb_module *module)
255 struct ltdb_private *ltdb = module->private_data;
256 return tdb_lockall_read(ltdb->tdb);
260 unlock the database after a ltdb_lock_read()
262 static int ltdb_unlock_read(struct ldb_module *module)
264 struct ltdb_private *ltdb = module->private_data;
265 return tdb_unlockall_read(ltdb->tdb);
269 add a set of attributes from a record to a set of results
270 return 0 on success, -1 on failure
272 int ltdb_add_attr_results(struct ldb_module *module,
274 struct ldb_message *msg,
275 const char * const attrs[],
277 struct ldb_message ***res)
279 struct ldb_message *msg2;
280 struct ldb_message **res2;
282 /* pull the attributes that the user wants */
283 msg2 = ltdb_pull_attrs(module, mem_ctx, msg, attrs);
288 /* add to the results list */
289 res2 = talloc_realloc(mem_ctx, *res, struct ldb_message *, (*count)+2);
297 (*res)[*count] = talloc_move(*res, &msg2);
298 (*res)[(*count)+1] = NULL;
307 filter the specified list of attributes from a message
308 removing not requested attrs.
310 int ltdb_filter_attrs(struct ldb_message *msg, const char * const *attrs)
315 /* check for special attrs */
316 for (i = 0; attrs[i]; i++) {
317 if (strcmp(attrs[i], "*") == 0) {
322 if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
323 if (msg_add_distinguished_name(msg) != 0) {
333 if (msg_add_distinguished_name(msg) != 0) {
339 for (i = 0; i < msg->num_elements; i++) {
342 for (j = 0, found = 0; attrs[j]; j++) {
343 if (ldb_attr_cmp(msg->elements[i].name, attrs[j]) == 0) {
350 ldb_msg_remove_attr(msg, msg->elements[i].name);
359 search function for a non-indexed search
361 static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
363 struct ldb_handle *handle = talloc_get_type(state, struct ldb_handle);
364 struct ltdb_context *ac = talloc_get_type(handle->private_data, struct ltdb_context);
365 struct ldb_reply *ares = NULL;
369 strncmp((char *)key.dptr, "DN=", 3) != 0) {
373 ares = talloc_zero(ac, struct ldb_reply);
375 handle->status = LDB_ERR_OPERATIONS_ERROR;
376 handle->state = LDB_ASYNC_DONE;
380 ares->message = ldb_msg_new(ares);
381 if (!ares->message) {
382 handle->status = LDB_ERR_OPERATIONS_ERROR;
383 handle->state = LDB_ASYNC_DONE;
388 /* unpack the record */
389 ret = ltdb_unpack_data(ac->module, &data, ares->message);
395 if (!ares->message->dn) {
396 ares->message->dn = ldb_dn_new(ares->message, ac->module->ldb, (char *)key.dptr + 3);
397 if (ares->message->dn == NULL) {
398 handle->status = LDB_ERR_OPERATIONS_ERROR;
399 handle->state = LDB_ASYNC_DONE;
405 /* see if it matches the given expression */
406 if (!ldb_match_msg(ac->module->ldb, ares->message, ac->tree,
407 ac->base, ac->scope)) {
412 /* filter the attributes that the user wants */
413 ret = ltdb_filter_attrs(ares->message, ac->attrs);
416 handle->status = LDB_ERR_OPERATIONS_ERROR;
417 handle->state = LDB_ASYNC_DONE;
422 ares->type = LDB_REPLY_ENTRY;
423 handle->state = LDB_ASYNC_PENDING;
424 handle->status = ac->callback(ac->module->ldb, ac->context, ares);
426 if (handle->status != LDB_SUCCESS) {
427 /* don't try to free ares here, the callback is in charge of that */
436 search the database with a LDAP-like expression.
437 this is the "full search" non-indexed variant
439 static int ltdb_search_full(struct ldb_handle *handle)
441 struct ltdb_context *ac = talloc_get_type(handle->private_data, struct ltdb_context);
442 struct ltdb_private *ltdb = talloc_get_type(ac->module->private_data, struct ltdb_private);
445 ret = tdb_traverse_read(ltdb->tdb, search_func, handle);
448 handle->status = LDB_ERR_OPERATIONS_ERROR;
451 handle->state = LDB_ASYNC_DONE;
456 search the database with a LDAP-like expression.
457 choses a search method
459 int ltdb_search(struct ldb_module *module, struct ldb_request *req)
461 struct ltdb_private *ltdb = talloc_get_type(module->private_data, struct ltdb_private);
462 struct ltdb_context *ltdb_ac;
463 struct ldb_reply *ares;
466 if ((( ! ldb_dn_is_valid(req->op.search.base)) || ldb_dn_is_null(req->op.search.base)) &&
467 (req->op.search.scope == LDB_SCOPE_BASE || req->op.search.scope == LDB_SCOPE_ONELEVEL))
468 return LDB_ERR_OPERATIONS_ERROR;
470 if (ltdb_lock_read(module) != 0) {
471 return LDB_ERR_OPERATIONS_ERROR;
474 if (ltdb_cache_load(module) != 0) {
475 ltdb_unlock_read(module);
476 return LDB_ERR_OPERATIONS_ERROR;
479 if (req->op.search.tree == NULL) {
480 ltdb_unlock_read(module);
481 return LDB_ERR_OPERATIONS_ERROR;
484 req->handle = init_ltdb_handle(ltdb, module, req);
485 if (req->handle == NULL) {
486 ltdb_unlock_read(module);
487 return LDB_ERR_OPERATIONS_ERROR;
489 ltdb_ac = talloc_get_type(req->handle->private_data, struct ltdb_context);
491 ltdb_ac->tree = req->op.search.tree;
492 ltdb_ac->scope = req->op.search.scope;
493 ltdb_ac->base = req->op.search.base;
494 ltdb_ac->attrs = req->op.search.attrs;
496 ret = ltdb_search_indexed(req->handle);
497 if (ret == LDB_ERR_OPERATIONS_ERROR) {
498 ret = ltdb_search_full(req->handle);
500 if (ret != LDB_SUCCESS) {
501 ldb_set_errstring(module->ldb, "Indexed and full searches both failed!\n");
502 req->handle->state = LDB_ASYNC_DONE;
503 req->handle->status = ret;
506 /* Finally send an LDB_REPLY_DONE packet when searching is finished */
508 ares = talloc_zero(req, struct ldb_reply);
510 ltdb_unlock_read(module);
511 return LDB_ERR_OPERATIONS_ERROR;
514 req->handle->state = LDB_ASYNC_DONE;
515 ares->type = LDB_REPLY_DONE;
517 ret = req->callback(module->ldb, req->context, ares);
518 req->handle->status = ret;
520 ltdb_unlock_read(module);