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_private.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
;
83 elnew
->flags
= el
->flags
;
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";
104 val
.data
= (uint8_t *)ldb_dn_alloc_linearized(msg
, msg
->dn
);
105 val
.length
= strlen((char *)val
.data
);
107 ret
= msg_add_element(msg
, &el
, 1);
112 search the database for a single simple dn.
113 return LDB_ERR_NO_SUCH_OBJECT on record-not-found
114 and LDB_SUCCESS on success
116 static int ltdb_search_base(struct ldb_module
*module
, struct ldb_dn
*dn
)
118 void *data
= ldb_module_get_private(module
);
119 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
123 if (ldb_dn_is_null(dn
)) {
124 return LDB_ERR_NO_SUCH_OBJECT
;
128 tdb_key
= ltdb_key(module
, dn
);
130 return LDB_ERR_OPERATIONS_ERROR
;
133 exists
= tdb_exists(ltdb
->tdb
, tdb_key
);
134 talloc_free(tdb_key
.dptr
);
139 return LDB_ERR_NO_SUCH_OBJECT
;
142 struct ltdb_parse_data_unpack_ctx
{
143 struct ldb_message
*msg
;
144 struct ldb_module
*module
;
145 unsigned int unpack_flags
;
148 static int ltdb_parse_data_unpack(TDB_DATA key
, TDB_DATA data
,
151 struct ltdb_parse_data_unpack_ctx
*ctx
= private_data
;
152 unsigned int nb_elements_in_db
;
154 struct ldb_context
*ldb
= ldb_module_get_ctx(ctx
->module
);
155 struct ldb_val data_parse
= {
160 if (ctx
->unpack_flags
& LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
) {
162 * If we got LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
163 * we need at least do a memdup on the whole
164 * data buffer as that may change later
165 * and the caller needs a stable result.
167 data_parse
.data
= talloc_memdup(ctx
->msg
,
170 if (data_parse
.data
== NULL
) {
171 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
172 "Unable to allocate data(%d) for %*.*s\n",
174 (int)key
.dsize
, (int)key
.dsize
, key
.dptr
);
175 return LDB_ERR_OPERATIONS_ERROR
;
179 ret
= ldb_unpack_data_only_attr_list_flags(ldb
, &data_parse
,
185 if (data_parse
.data
!= data
.dptr
) {
186 talloc_free(data_parse
.data
);
189 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "Invalid data for index %*.*s\n",
190 (int)key
.dsize
, (int)key
.dsize
, key
.dptr
);
191 return LDB_ERR_OPERATIONS_ERROR
;
197 search the database for a single simple dn, returning all attributes
200 return LDB_ERR_NO_SUCH_OBJECT on record-not-found
201 and LDB_SUCCESS on success
203 int ltdb_search_dn1(struct ldb_module
*module
, struct ldb_dn
*dn
, struct ldb_message
*msg
,
204 unsigned int unpack_flags
)
206 void *data
= ldb_module_get_private(module
);
207 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
210 struct ltdb_parse_data_unpack_ctx ctx
= {
213 .unpack_flags
= unpack_flags
217 tdb_key
= ltdb_key(module
, dn
);
219 return LDB_ERR_OPERATIONS_ERROR
;
222 memset(msg
, 0, sizeof(*msg
));
224 msg
->num_elements
= 0;
225 msg
->elements
= NULL
;
227 ret
= tdb_parse_record(ltdb
->tdb
, tdb_key
,
228 ltdb_parse_data_unpack
, &ctx
);
229 talloc_free(tdb_key
.dptr
);
232 if (tdb_error(ltdb
->tdb
) == TDB_ERR_NOEXIST
) {
233 return LDB_ERR_NO_SUCH_OBJECT
;
235 return LDB_ERR_OPERATIONS_ERROR
;
236 } else if (ret
!= LDB_SUCCESS
) {
240 if ((unpack_flags
& LDB_UNPACK_DATA_FLAG_NO_DN
) == 0) {
242 msg
->dn
= ldb_dn_copy(msg
, dn
);
245 return LDB_ERR_OPERATIONS_ERROR
;
253 filter the specified list of attributes from a message
254 removing not requested attrs from the new message constructed.
256 The reason this makes a new message is that the old one may not be
257 individually allocated, which is what our callers expect.
260 int ltdb_filter_attrs(TALLOC_CTX
*mem_ctx
,
261 const struct ldb_message
*msg
, const char * const *attrs
,
262 struct ldb_message
**filtered_msg
)
265 bool keep_all
= false;
267 uint32_t num_elements
;
268 uint32_t elements_size
;
269 struct ldb_message
*msg2
;
271 msg2
= ldb_msg_new(mem_ctx
);
276 msg2
->dn
= ldb_dn_copy(msg2
, msg
->dn
);
277 if (msg2
->dn
== NULL
) {
282 /* check for special attrs */
283 for (i
= 0; attrs
[i
]; i
++) {
284 int cmp
= strcmp(attrs
[i
], "*");
289 cmp
= ldb_attr_cmp(attrs
[i
], "distinguishedName");
300 elements_size
= msg
->num_elements
+ 1;
302 /* Shortcuts for the simple cases */
303 } else if (add_dn
&& i
== 1) {
304 if (msg_add_distinguished_name(msg2
) != 0) {
307 *filtered_msg
= msg2
;
310 *filtered_msg
= msg2
;
313 /* Otherwise we are copying at most as many element as we have attributes */
318 msg2
->elements
= talloc_array(msg2
, struct ldb_message_element
,
320 if (msg2
->elements
== NULL
) goto failed
;
324 for (i
= 0; i
< msg
->num_elements
; i
++) {
325 struct ldb_message_element
*el
= &msg
->elements
[i
];
326 struct ldb_message_element
*el2
= &msg2
->elements
[num_elements
];
329 if (keep_all
== false) {
331 for (j
= 0; attrs
[j
]; j
++) {
332 int cmp
= ldb_attr_cmp(el
->name
, attrs
[j
]);
338 if (found
== false) {
343 el2
->name
= talloc_strdup(msg2
->elements
, el
->name
);
344 if (el2
->name
== NULL
) {
347 el2
->values
= talloc_array(msg2
->elements
, struct ldb_val
, el
->num_values
);
348 if (el2
->values
== NULL
) {
351 for (j
=0;j
<el
->num_values
;j
++) {
352 el2
->values
[j
] = ldb_val_dup(el2
->values
, &el
->values
[j
]);
353 if (el2
->values
[j
].data
== NULL
&& el
->values
[j
].length
!= 0) {
359 /* Pidginhole principle: we can't have more elements
360 * than the number of attributes if they are unique in
362 if (num_elements
> elements_size
) {
367 msg2
->num_elements
= num_elements
;
370 if (msg_add_distinguished_name(msg2
) != 0) {
375 if (msg2
->num_elements
> 0) {
376 msg2
->elements
= talloc_realloc(msg2
, msg2
->elements
,
377 struct ldb_message_element
,
379 if (msg2
->elements
== NULL
) {
383 talloc_free(msg2
->elements
);
384 msg2
->elements
= NULL
;
387 *filtered_msg
= msg2
;
395 search function for a non-indexed search
397 static int search_func(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *state
)
399 struct ldb_context
*ldb
;
400 struct ltdb_context
*ac
;
401 struct ldb_message
*msg
, *filtered_msg
;
402 const struct ldb_val val
= {
404 .length
= data
.dsize
,
408 unsigned int nb_elements_in_db
;
410 ac
= talloc_get_type(state
, struct ltdb_context
);
411 ldb
= ldb_module_get_ctx(ac
->module
);
413 if (ltdb_key_is_record(key
) == false) {
417 msg
= ldb_msg_new(ac
);
419 ac
->error
= LDB_ERR_OPERATIONS_ERROR
;
423 /* unpack the record */
424 ret
= ldb_unpack_data_only_attr_list_flags(ldb
, &val
,
427 LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
|
428 LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC
,
432 ac
->error
= LDB_ERR_OPERATIONS_ERROR
;
437 msg
->dn
= ldb_dn_new(msg
, ldb
,
438 (char *)key
.dptr
+ 3);
439 if (msg
->dn
== NULL
) {
441 ac
->error
= LDB_ERR_OPERATIONS_ERROR
;
446 /* see if it matches the given expression */
447 ret
= ldb_match_msg_error(ldb
, msg
,
448 ac
->tree
, ac
->base
, ac
->scope
, &matched
);
449 if (ret
!= LDB_SUCCESS
) {
451 ac
->error
= LDB_ERR_OPERATIONS_ERROR
;
459 /* filter the attributes that the user wants */
460 ret
= ltdb_filter_attrs(ac
, msg
, ac
->attrs
, &filtered_msg
);
464 ac
->error
= LDB_ERR_OPERATIONS_ERROR
;
468 ret
= ldb_module_send_entry(ac
->req
, filtered_msg
, NULL
);
469 if (ret
!= LDB_SUCCESS
) {
470 ac
->request_terminated
= true;
471 /* the callback failed, abort the operation */
472 ac
->error
= LDB_ERR_OPERATIONS_ERROR
;
481 search the database with a LDAP-like expression.
482 this is the "full search" non-indexed variant
484 static int ltdb_search_full(struct ltdb_context
*ctx
)
486 void *data
= ldb_module_get_private(ctx
->module
);
487 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
490 ctx
->error
= LDB_SUCCESS
;
491 if (ltdb
->in_transaction
!= 0) {
492 ret
= tdb_traverse(ltdb
->tdb
, search_func
, ctx
);
494 ret
= tdb_traverse_read(ltdb
->tdb
, search_func
, ctx
);
498 return LDB_ERR_OPERATIONS_ERROR
;
505 search the database with a LDAP-like expression.
506 choses a search method
508 int ltdb_search(struct ltdb_context
*ctx
)
510 struct ldb_context
*ldb
;
511 struct ldb_module
*module
= ctx
->module
;
512 struct ldb_request
*req
= ctx
->req
;
513 void *data
= ldb_module_get_private(module
);
514 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
517 ldb
= ldb_module_get_ctx(module
);
519 ldb_request_set_state(req
, LDB_ASYNC_PENDING
);
521 if (ltdb_lock_read(module
) != 0) {
522 return LDB_ERR_OPERATIONS_ERROR
;
525 if (ltdb_cache_load(module
) != 0) {
526 ltdb_unlock_read(module
);
527 return LDB_ERR_OPERATIONS_ERROR
;
530 if (req
->op
.search
.tree
== NULL
) {
531 ltdb_unlock_read(module
);
532 return LDB_ERR_OPERATIONS_ERROR
;
535 if ((req
->op
.search
.base
== NULL
) || (ldb_dn_is_null(req
->op
.search
.base
) == true)) {
537 /* Check what we should do with a NULL dn */
538 switch (req
->op
.search
.scope
) {
540 ldb_asprintf_errstring(ldb
,
541 "NULL Base DN invalid for a base search");
542 ret
= LDB_ERR_INVALID_DN_SYNTAX
;
544 case LDB_SCOPE_ONELEVEL
:
545 ldb_asprintf_errstring(ldb
,
546 "NULL Base DN invalid for a one-level search");
547 ret
= LDB_ERR_INVALID_DN_SYNTAX
;
549 case LDB_SCOPE_SUBTREE
:
551 /* We accept subtree searches from a NULL base DN, ie over the whole DB */
554 } else if (ldb_dn_is_valid(req
->op
.search
.base
) == false) {
556 /* We don't want invalid base DNs here */
557 ldb_asprintf_errstring(ldb
,
558 "Invalid Base DN: %s",
559 ldb_dn_get_linearized(req
->op
.search
.base
));
560 ret
= LDB_ERR_INVALID_DN_SYNTAX
;
562 } else if (ltdb
->check_base
) {
563 /* This database has been marked as 'checkBaseOnSearch', so do a spot check of the base dn */
564 ret
= ltdb_search_base(module
, req
->op
.search
.base
);
566 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
567 ldb_asprintf_errstring(ldb
,
568 "No such Base DN: %s",
569 ldb_dn_get_linearized(req
->op
.search
.base
));
573 /* If we are not checking the base DN life is easy */
577 ctx
->tree
= req
->op
.search
.tree
;
578 ctx
->scope
= req
->op
.search
.scope
;
579 ctx
->base
= req
->op
.search
.base
;
580 ctx
->attrs
= req
->op
.search
.attrs
;
582 if (ret
== LDB_SUCCESS
) {
583 uint32_t match_count
= 0;
585 ret
= ltdb_search_indexed(ctx
, &match_count
);
586 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
587 /* Not in the index, therefore OK! */
591 /* Check if we got just a normal error.
592 * In that case proceed to a full search unless we got a
594 if ( ! ctx
->request_terminated
&& ret
!= LDB_SUCCESS
) {
595 /* Not indexed, so we need to do a full scan */
596 if (ltdb
->warn_unindexed
) {
597 /* useful for debugging when slow performance
598 * is caused by unindexed searches */
599 char *expression
= ldb_filter_from_tree(ctx
, ctx
->tree
);
600 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb FULL SEARCH: %s SCOPE: %s DN: %s",
602 req
->op
.search
.scope
==LDB_SCOPE_BASE
?"base":
603 req
->op
.search
.scope
==LDB_SCOPE_ONELEVEL
?"one":
604 req
->op
.search
.scope
==LDB_SCOPE_SUBTREE
?"sub":"UNKNOWN",
605 ldb_dn_get_linearized(req
->op
.search
.base
));
607 talloc_free(expression
);
609 if (match_count
!= 0) {
610 /* the indexing code gave an error
611 * after having returned at least one
612 * entry. This means the indexes are
613 * corrupt or a database record is
614 * corrupt. We cannot continue with a
615 * full search or we may return
618 ltdb_unlock_read(module
);
619 return LDB_ERR_OPERATIONS_ERROR
;
621 ret
= ltdb_search_full(ctx
);
622 if (ret
!= LDB_SUCCESS
) {
623 ldb_set_errstring(ldb
, "Indexed and full searches both failed!\n");
628 ltdb_unlock_read(module
);