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 add all elements from one message into another
114 static int msg_add_all_elements(struct ldb_module
*module
, struct ldb_message
*ret
,
115 const struct ldb_message
*msg
)
117 struct ldb_context
*ldb
;
119 int check_duplicates
= (ret
->num_elements
!= 0);
121 ldb
= ldb_module_get_ctx(module
);
123 if (msg_add_distinguished_name(ret
) != 0) {
127 for (i
=0;i
<msg
->num_elements
;i
++) {
128 const struct ldb_schema_attribute
*a
;
129 a
= ldb_schema_attribute_by_name(ldb
, msg
->elements
[i
].name
);
130 if (a
->flags
& LDB_ATTR_FLAG_HIDDEN
) {
133 if (msg_add_element(ret
, &msg
->elements
[i
],
134 check_duplicates
) != 0) {
144 pull the specified list of attributes from a message
146 static struct ldb_message
*ltdb_pull_attrs(struct ldb_module
*module
,
148 const struct ldb_message
*msg
,
149 const char * const *attrs
)
151 struct ldb_message
*ret
;
154 ret
= talloc(mem_ctx
, struct ldb_message
);
159 ret
->dn
= ldb_dn_copy(ret
, msg
->dn
);
165 ret
->num_elements
= 0;
166 ret
->elements
= NULL
;
169 if (msg_add_all_elements(module
, ret
, msg
) != 0) {
176 for (i
=0;attrs
[i
];i
++) {
177 struct ldb_message_element
*el
;
179 if (strcmp(attrs
[i
], "*") == 0) {
180 if (msg_add_all_elements(module
, ret
, msg
) != 0) {
187 if (ldb_attr_cmp(attrs
[i
], "distinguishedName") == 0) {
188 if (msg_add_distinguished_name(ret
) != 0) {
194 el
= ldb_msg_find_element(msg
, attrs
[i
]);
198 if (msg_add_element(ret
, el
, 1) != 0) {
208 search the database for a single simple dn.
209 return LDB_ERR_NO_SUCH_OBJECT on record-not-found
210 and LDB_SUCCESS on success
212 static int ltdb_search_base(struct ldb_module
*module
, struct ldb_dn
*dn
)
214 void *data
= ldb_module_get_private(module
);
215 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
219 if (ldb_dn_is_null(dn
)) {
220 return LDB_ERR_NO_SUCH_OBJECT
;
224 tdb_key
= ltdb_key(module
, dn
);
226 return LDB_ERR_OPERATIONS_ERROR
;
229 exists
= tdb_exists(ltdb
->tdb
, tdb_key
);
230 talloc_free(tdb_key
.dptr
);
235 return LDB_ERR_NO_SUCH_OBJECT
;
238 struct ltdb_parse_data_unpack_ctx
{
239 struct ldb_message
*msg
;
240 struct ldb_module
*module
;
243 static int ltdb_parse_data_unpack(TDB_DATA key
, TDB_DATA data
,
246 struct ltdb_parse_data_unpack_ctx
*ctx
= private_data
;
248 struct ldb_context
*ldb
= ldb_module_get_ctx(ctx
->module
);
249 int ret
= ldb_unpack_data(ldb
, (struct ldb_val
*)&data
, ctx
->msg
);
251 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "Invalid data for index %*.*s\n",
252 (int)key
.dsize
, (int)key
.dsize
, key
.dptr
);
253 return LDB_ERR_OPERATIONS_ERROR
;
259 search the database for a single simple dn, returning all attributes
262 return LDB_ERR_NO_SUCH_OBJECT on record-not-found
263 and LDB_SUCCESS on success
265 int ltdb_search_dn1(struct ldb_module
*module
, struct ldb_dn
*dn
, struct ldb_message
*msg
)
267 void *data
= ldb_module_get_private(module
);
268 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
271 struct ltdb_parse_data_unpack_ctx ctx
= {
277 tdb_key
= ltdb_key(module
, dn
);
279 return LDB_ERR_OPERATIONS_ERROR
;
282 memset(msg
, 0, sizeof(*msg
));
284 msg
->num_elements
= 0;
285 msg
->elements
= NULL
;
287 ret
= tdb_parse_record(ltdb
->tdb
, tdb_key
,
288 ltdb_parse_data_unpack
, &ctx
);
289 talloc_free(tdb_key
.dptr
);
292 if (tdb_error(ltdb
->tdb
) == TDB_ERR_NOEXIST
) {
293 return LDB_ERR_NO_SUCH_OBJECT
;
295 return LDB_ERR_OPERATIONS_ERROR
;
296 } else if (ret
!= LDB_SUCCESS
) {
301 msg
->dn
= ldb_dn_copy(msg
, dn
);
304 return LDB_ERR_OPERATIONS_ERROR
;
311 add a set of attributes from a record to a set of results
312 return 0 on success, -1 on failure
314 int ltdb_add_attr_results(struct ldb_module
*module
,
316 struct ldb_message
*msg
,
317 const char * const attrs
[],
319 struct ldb_message
***res
)
321 struct ldb_message
*msg2
;
322 struct ldb_message
**res2
;
324 /* pull the attributes that the user wants */
325 msg2
= ltdb_pull_attrs(module
, mem_ctx
, msg
, attrs
);
330 /* add to the results list */
331 res2
= talloc_realloc(mem_ctx
, *res
, struct ldb_message
*, (*count
)+2);
339 (*res
)[*count
] = talloc_move(*res
, &msg2
);
340 (*res
)[(*count
)+1] = NULL
;
349 filter the specified list of attributes from a message
350 removing not requested attrs.
352 int ltdb_filter_attrs(struct ldb_message
*msg
, const char * const *attrs
)
356 struct ldb_message_element
*el2
;
357 uint32_t num_elements
;
360 /* check for special attrs */
361 for (i
= 0; attrs
[i
]; i
++) {
362 if (strcmp(attrs
[i
], "*") == 0) {
367 if (ldb_attr_cmp(attrs
[i
], "distinguishedName") == 0) {
368 if (msg_add_distinguished_name(msg
) != 0) {
378 if (msg_add_distinguished_name(msg
) != 0) {
384 el2
= talloc_array(msg
, struct ldb_message_element
, msg
->num_elements
);
390 for (i
= 0; i
< msg
->num_elements
; i
++) {
394 for (j
= 0; attrs
[j
]; j
++) {
395 if (ldb_attr_cmp(msg
->elements
[i
].name
, attrs
[j
]) == 0) {
402 el2
[num_elements
] = msg
->elements
[i
];
403 talloc_steal(el2
, el2
[num_elements
].name
);
404 talloc_steal(el2
, el2
[num_elements
].values
);
409 talloc_free(msg
->elements
);
411 if (num_elements
> 0) {
412 msg
->elements
= talloc_realloc(msg
, el2
, struct ldb_message_element
,
415 msg
->elements
= talloc_array(msg
, struct ldb_message_element
, 0);
418 if (msg
->elements
== NULL
) {
422 msg
->num_elements
= num_elements
;
428 search function for a non-indexed search
430 static int search_func(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA data
, void *state
)
432 struct ldb_context
*ldb
;
433 struct ltdb_context
*ac
;
434 struct ldb_message
*msg
;
438 ac
= talloc_get_type(state
, struct ltdb_context
);
439 ldb
= ldb_module_get_ctx(ac
->module
);
442 strncmp((char *)key
.dptr
, "DN=", 3) != 0) {
446 msg
= ldb_msg_new(ac
);
448 ac
->error
= LDB_ERR_OPERATIONS_ERROR
;
452 /* unpack the record */
453 ret
= ldb_unpack_data(ldb
, (struct ldb_val
*)&data
, msg
);
456 ac
->error
= LDB_ERR_OPERATIONS_ERROR
;
461 msg
->dn
= ldb_dn_new(msg
, ldb
,
462 (char *)key
.dptr
+ 3);
463 if (msg
->dn
== NULL
) {
465 ac
->error
= LDB_ERR_OPERATIONS_ERROR
;
470 /* see if it matches the given expression */
471 ret
= ldb_match_msg_error(ldb
, msg
,
472 ac
->tree
, ac
->base
, ac
->scope
, &matched
);
473 if (ret
!= LDB_SUCCESS
) {
475 ac
->error
= LDB_ERR_OPERATIONS_ERROR
;
483 /* filter the attributes that the user wants */
484 ret
= ltdb_filter_attrs(msg
, ac
->attrs
);
488 ac
->error
= LDB_ERR_OPERATIONS_ERROR
;
492 ret
= ldb_module_send_entry(ac
->req
, msg
, NULL
);
493 if (ret
!= LDB_SUCCESS
) {
494 ac
->request_terminated
= true;
495 /* the callback failed, abort the operation */
496 ac
->error
= LDB_ERR_OPERATIONS_ERROR
;
505 search the database with a LDAP-like expression.
506 this is the "full search" non-indexed variant
508 static int ltdb_search_full(struct ltdb_context
*ctx
)
510 void *data
= ldb_module_get_private(ctx
->module
);
511 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
514 ctx
->error
= LDB_SUCCESS
;
515 if (ltdb
->in_transaction
!= 0) {
516 ret
= tdb_traverse(ltdb
->tdb
, search_func
, ctx
);
518 ret
= tdb_traverse_read(ltdb
->tdb
, search_func
, ctx
);
522 return LDB_ERR_OPERATIONS_ERROR
;
529 search the database with a LDAP-like expression.
530 choses a search method
532 int ltdb_search(struct ltdb_context
*ctx
)
534 struct ldb_context
*ldb
;
535 struct ldb_module
*module
= ctx
->module
;
536 struct ldb_request
*req
= ctx
->req
;
537 void *data
= ldb_module_get_private(module
);
538 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
541 ldb
= ldb_module_get_ctx(module
);
543 ldb_request_set_state(req
, LDB_ASYNC_PENDING
);
545 if (ltdb_lock_read(module
) != 0) {
546 return LDB_ERR_OPERATIONS_ERROR
;
549 if (ltdb_cache_load(module
) != 0) {
550 ltdb_unlock_read(module
);
551 return LDB_ERR_OPERATIONS_ERROR
;
554 if (req
->op
.search
.tree
== NULL
) {
555 ltdb_unlock_read(module
);
556 return LDB_ERR_OPERATIONS_ERROR
;
559 if ((req
->op
.search
.base
== NULL
) || (ldb_dn_is_null(req
->op
.search
.base
) == true)) {
561 /* Check what we should do with a NULL dn */
562 switch (req
->op
.search
.scope
) {
564 ldb_asprintf_errstring(ldb
,
565 "NULL Base DN invalid for a base search");
566 ret
= LDB_ERR_INVALID_DN_SYNTAX
;
568 case LDB_SCOPE_ONELEVEL
:
569 ldb_asprintf_errstring(ldb
,
570 "NULL Base DN invalid for a one-level search");
571 ret
= LDB_ERR_INVALID_DN_SYNTAX
;
573 case LDB_SCOPE_SUBTREE
:
575 /* We accept subtree searches from a NULL base DN, ie over the whole DB */
578 } else if (ldb_dn_is_valid(req
->op
.search
.base
) == false) {
580 /* We don't want invalid base DNs here */
581 ldb_asprintf_errstring(ldb
,
582 "Invalid Base DN: %s",
583 ldb_dn_get_linearized(req
->op
.search
.base
));
584 ret
= LDB_ERR_INVALID_DN_SYNTAX
;
586 } else if (ltdb
->check_base
) {
587 /* This database has been marked as 'checkBaseOnSearch', so do a spot check of the base dn */
588 ret
= ltdb_search_base(module
, req
->op
.search
.base
);
590 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
591 ldb_asprintf_errstring(ldb
,
592 "No such Base DN: %s",
593 ldb_dn_get_linearized(req
->op
.search
.base
));
597 /* If we are not checking the base DN life is easy */
601 ctx
->tree
= req
->op
.search
.tree
;
602 ctx
->scope
= req
->op
.search
.scope
;
603 ctx
->base
= req
->op
.search
.base
;
604 ctx
->attrs
= req
->op
.search
.attrs
;
606 if (ret
== LDB_SUCCESS
) {
607 uint32_t match_count
= 0;
609 ret
= ltdb_search_indexed(ctx
, &match_count
);
610 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
611 /* Not in the index, therefore OK! */
615 /* Check if we got just a normal error.
616 * In that case proceed to a full search unless we got a
618 if ( ! ctx
->request_terminated
&& ret
!= LDB_SUCCESS
) {
619 /* Not indexed, so we need to do a full scan */
620 if (ltdb
->warn_unindexed
) {
621 /* useful for debugging when slow performance
622 * is caused by unindexed searches */
623 char *expression
= ldb_filter_from_tree(ctx
, ctx
->tree
);
624 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb FULL SEARCH: %s SCOPE: %s DN: %s",
626 req
->op
.search
.scope
==LDB_SCOPE_BASE
?"base":
627 req
->op
.search
.scope
==LDB_SCOPE_ONELEVEL
?"one":
628 req
->op
.search
.scope
==LDB_SCOPE_SUBTREE
?"sub":"UNKNOWN",
629 ldb_dn_get_linearized(req
->op
.search
.base
));
631 talloc_free(expression
);
633 if (match_count
!= 0) {
634 /* the indexing code gave an error
635 * after having returned at least one
636 * entry. This means the indexes are
637 * corrupt or a database record is
638 * corrupt. We cannot continue with a
639 * full search or we may return
642 ltdb_unlock_read(module
);
643 return LDB_ERR_OPERATIONS_ERROR
;
645 ret
= ltdb_search_full(ctx
);
646 if (ret
!= LDB_SUCCESS
) {
647 ldb_set_errstring(ldb
, "Indexed and full searches both failed!\n");
652 ltdb_unlock_read(module
);