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
35
#include "ldb/include/includes.h"
37
#include "ldb/ldb_tdb/ldb_tdb.h"
40
add one element to a message
42
static int msg_add_element(struct ldb_message *ret,
43
const struct ldb_message_element *el,
47
struct ldb_message_element *e2, *elnew;
49
if (check_duplicates && ldb_msg_find_element(ret, el->name)) {
50
/* its already there */
54
e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
60
elnew = &e2[ret->num_elements];
62
elnew->name = talloc_strdup(ret->elements, el->name);
68
elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
76
for (i=0;i<el->num_values;i++) {
77
elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
78
if (elnew->values[i].length != el->values[i].length) {
83
elnew->num_values = el->num_values;
91
add the special distinguishedName element
93
static int msg_add_distinguished_name(struct ldb_message *msg)
95
struct ldb_message_element el;
100
el.name = "distinguishedName";
103
val.data = (uint8_t *)ldb_dn_linearize(msg, msg->dn);
104
val.length = strlen((char *)val.data);
106
ret = msg_add_element(msg, &el, 1);
111
add all elements from one message into another
113
static int msg_add_all_elements(struct ldb_module *module, struct ldb_message *ret,
114
const struct ldb_message *msg)
116
struct ldb_context *ldb = module->ldb;
118
int check_duplicates = (ret->num_elements != 0);
120
if (msg_add_distinguished_name(ret) != 0) {
124
for (i=0;i<msg->num_elements;i++) {
125
const struct ldb_attrib_handler *h;
126
h = ldb_attrib_handler(ldb, msg->elements[i].name);
127
if (h->flags & LDB_ATTR_FLAG_HIDDEN) {
130
if (msg_add_element(ret, &msg->elements[i],
131
check_duplicates) != 0) {
141
pull the specified list of attributes from a message
143
static struct ldb_message *ltdb_pull_attrs(struct ldb_module *module,
145
const struct ldb_message *msg,
146
const char * const *attrs)
148
struct ldb_message *ret;
151
ret = talloc(mem_ctx, struct ldb_message);
156
ret->dn = ldb_dn_copy(ret, msg->dn);
162
ret->num_elements = 0;
163
ret->elements = NULL;
166
if (msg_add_all_elements(module, ret, msg) != 0) {
173
for (i=0;attrs[i];i++) {
174
struct ldb_message_element *el;
176
if (strcmp(attrs[i], "*") == 0) {
177
if (msg_add_all_elements(module, ret, msg) != 0) {
184
if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
185
if (msg_add_distinguished_name(ret) != 0) {
191
el = ldb_msg_find_element(msg, attrs[i]);
195
if (msg_add_element(ret, el, 1) != 0) {
206
search the database for a single simple dn, returning all attributes
209
return 1 on success, 0 on record-not-found and -1 on error
211
int ltdb_search_dn1(struct ldb_module *module, const struct ldb_dn *dn, struct ldb_message *msg)
213
struct ltdb_private *ltdb =
214
(struct ltdb_private *)module->private_data;
216
TDB_DATA tdb_key, tdb_data;
218
memset(msg, 0, sizeof(*msg));
221
tdb_key = ltdb_key(module, dn);
226
tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
227
talloc_free(tdb_key.dptr);
228
if (!tdb_data.dptr) {
232
msg->num_elements = 0;
233
msg->elements = NULL;
235
ret = ltdb_unpack_data(module, &tdb_data, msg);
242
msg->dn = ldb_dn_copy(msg, dn);
252
lock the database for read - use by ltdb_search
254
static int ltdb_lock_read(struct ldb_module *module)
256
struct ltdb_private *ltdb =
257
(struct ltdb_private *)module->private_data;
258
return tdb_lockall_read(ltdb->tdb);
262
unlock the database after a ltdb_lock_read()
264
static int ltdb_unlock_read(struct ldb_module *module)
266
struct ltdb_private *ltdb =
267
(struct ltdb_private *)module->private_data;
268
return tdb_unlockall_read(ltdb->tdb);
272
add a set of attributes from a record to a set of results
273
return 0 on success, -1 on failure
275
int ltdb_add_attr_results(struct ldb_module *module,
277
struct ldb_message *msg,
278
const char * const attrs[],
280
struct ldb_message ***res)
282
struct ldb_message *msg2;
283
struct ldb_message **res2;
285
/* pull the attributes that the user wants */
286
msg2 = ltdb_pull_attrs(module, mem_ctx, msg, attrs);
291
/* add to the results list */
292
res2 = talloc_realloc(mem_ctx, *res, struct ldb_message *, (*count)+2);
300
(*res)[*count] = talloc_move(*res, &msg2);
301
(*res)[(*count)+1] = NULL;
310
filter the specified list of attributes from a message
311
removing not requested attrs.
313
int ltdb_filter_attrs(struct ldb_message *msg, const char * const *attrs)
318
/* check for special attrs */
319
for (i = 0; attrs[i]; i++) {
320
if (strcmp(attrs[i], "*") == 0) {
325
if (ldb_attr_cmp(attrs[i], "distinguishedName") == 0) {
326
if (msg_add_distinguished_name(msg) != 0) {
336
if (msg_add_distinguished_name(msg) != 0) {
342
for (i = 0; i < msg->num_elements; i++) {
345
for (j = 0, found = 0; attrs[j]; j++) {
346
if (ldb_attr_cmp(msg->elements[i].name, attrs[j]) == 0) {
353
ldb_msg_remove_attr(msg, msg->elements[i].name);
362
search function for a non-indexed search
364
static int search_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
366
struct ldb_handle *handle = talloc_get_type(state, struct ldb_handle);
367
struct ltdb_context *ac = talloc_get_type(handle->private_data, struct ltdb_context);
368
struct ldb_reply *ares = NULL;
372
strncmp((char *)key.dptr, "DN=", 3) != 0) {
376
ares = talloc_zero(ac, struct ldb_reply);
378
handle->status = LDB_ERR_OPERATIONS_ERROR;
379
handle->state = LDB_ASYNC_DONE;
383
ares->message = ldb_msg_new(ares);
384
if (!ares->message) {
385
handle->status = LDB_ERR_OPERATIONS_ERROR;
386
handle->state = LDB_ASYNC_DONE;
391
/* unpack the record */
392
ret = ltdb_unpack_data(ac->module, &data, ares->message);
398
if (!ares->message->dn) {
399
ares->message->dn = ldb_dn_explode(ares->message, (char *)key.dptr + 3);
400
if (ares->message->dn == NULL) {
401
handle->status = LDB_ERR_OPERATIONS_ERROR;
402
handle->state = LDB_ASYNC_DONE;
408
/* see if it matches the given expression */
409
if (!ldb_match_msg(ac->module->ldb, ares->message, ac->tree,
410
ac->base, ac->scope)) {
415
/* filter the attributes that the user wants */
416
ret = ltdb_filter_attrs(ares->message, ac->attrs);
419
handle->status = LDB_ERR_OPERATIONS_ERROR;
420
handle->state = LDB_ASYNC_DONE;
425
ares->type = LDB_REPLY_ENTRY;
426
handle->state = LDB_ASYNC_PENDING;
427
handle->status = ac->callback(ac->module->ldb, ac->context, ares);
429
if (handle->status != LDB_SUCCESS) {
430
/* don't try to free ares here, the callback is in charge of that */
439
search the database with a LDAP-like expression.
440
this is the "full search" non-indexed variant
442
static int ltdb_search_full(struct ldb_handle *handle)
444
struct ltdb_context *ac = talloc_get_type(handle->private_data, struct ltdb_context);
445
struct ltdb_private *ltdb = talloc_get_type(ac->module->private_data, struct ltdb_private);
448
ret = tdb_traverse_read(ltdb->tdb, search_func, handle);
451
handle->status = LDB_ERR_OPERATIONS_ERROR;
454
handle->state = LDB_ASYNC_DONE;
459
search the database with a LDAP-like expression.
460
choses a search method
462
int ltdb_search(struct ldb_module *module, struct ldb_request *req)
464
struct ltdb_private *ltdb = talloc_get_type(module->private_data, struct ltdb_private);
465
struct ltdb_context *ltdb_ac;
466
struct ldb_reply *ares;
469
if ((req->op.search.base == NULL || ldb_dn_get_comp_num(req->op.search.base) == 0) &&
470
(req->op.search.scope == LDB_SCOPE_BASE || req->op.search.scope == LDB_SCOPE_ONELEVEL))
471
return LDB_ERR_OPERATIONS_ERROR;
473
if (ltdb_lock_read(module) != 0) {
474
return LDB_ERR_OPERATIONS_ERROR;
477
if (ltdb_cache_load(module) != 0) {
478
ltdb_unlock_read(module);
479
return LDB_ERR_OPERATIONS_ERROR;
482
if (req->op.search.tree == NULL) {
483
ltdb_unlock_read(module);
484
return LDB_ERR_OPERATIONS_ERROR;
487
req->handle = init_ltdb_handle(ltdb, module, req);
488
if (req->handle == NULL) {
489
ltdb_unlock_read(module);
490
return LDB_ERR_OPERATIONS_ERROR;
492
ltdb_ac = talloc_get_type(req->handle->private_data, struct ltdb_context);
494
ltdb_ac->tree = req->op.search.tree;
495
ltdb_ac->scope = req->op.search.scope;
496
ltdb_ac->base = req->op.search.base;
497
ltdb_ac->attrs = req->op.search.attrs;
499
ret = ltdb_search_indexed(req->handle);
501
ret = ltdb_search_full(req->handle);
503
if (ret != LDB_SUCCESS) {
504
ldb_set_errstring(module->ldb, "Indexed and full searches both failed!\n");
505
req->handle->state = LDB_ASYNC_DONE;
506
req->handle->status = ret;
509
/* Finally send an LDB_REPLY_DONE packet when searching is finished */
511
ares = talloc_zero(req, struct ldb_reply);
513
ltdb_unlock_read(module);
514
return LDB_ERR_OPERATIONS_ERROR;
517
req->handle->state = LDB_ASYNC_DONE;
518
ares->type = LDB_REPLY_DONE;
520
ret = req->callback(module->ldb, req->context, ares);
521
req->handle->status = ret;
523
ltdb_unlock_read(module);