4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Stefan Metzmacher 2004
6 Copyright (C) Simo Sorce 2006-2008
7 Copyright (C) Matthias Dieter Wallnöfer 2009-2010
9 ** NOTE! The following LGPL license applies to the ldb
10 ** library. This does NOT imply that all of Samba is released
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
30 * Component: ldb tdb backend
32 * Description: core functions for tdb backend
34 * Author: Andrew Tridgell
35 * Author: Stefan Metzmacher
39 * - description: make the module use asynchronous calls
43 * - description: make it possible to use event contexts
47 * - description: fix up memory leaks and small bugs
49 * Author: Matthias Dieter Wallnöfer
53 #include "ldb_private.h"
57 prevent memory errors on callbacks
60 struct ltdb_context
*ctx
;
64 map a tdb error code to a ldb error code
66 int ltdb_err_map(enum TDB_ERROR tdb_code
)
74 return LDB_ERR_OPERATIONS_ERROR
;
76 return LDB_ERR_PROTOCOL_ERROR
;
80 case TDB_ERR_LOCK_TIMEOUT
:
81 return LDB_ERR_TIME_LIMIT_EXCEEDED
;
83 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
85 return LDB_ERR_NO_SUCH_OBJECT
;
87 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
95 lock the database for read - use by ltdb_search and ltdb_sequence_number
97 int ltdb_lock_read(struct ldb_module
*module
)
99 void *data
= ldb_module_get_private(module
);
100 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
103 if (ltdb
->in_transaction
== 0 &&
104 ltdb
->read_lock_count
== 0) {
105 ret
= tdb_lockall_read(ltdb
->tdb
);
108 ltdb
->read_lock_count
++;
114 unlock the database after a ltdb_lock_read()
116 int ltdb_unlock_read(struct ldb_module
*module
)
118 void *data
= ldb_module_get_private(module
);
119 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
120 if (ltdb
->in_transaction
== 0 && ltdb
->read_lock_count
== 1) {
121 tdb_unlockall_read(ltdb
->tdb
);
122 ltdb
->read_lock_count
--;
125 ltdb
->read_lock_count
--;
131 * Determine if this key could hold a record. We allow the new GUID
132 * index, the old DN index and a possible future ID=
134 bool ltdb_key_is_record(TDB_DATA key
)
140 if (memcmp(key
.dptr
, "DN=", 3) == 0) {
144 if (memcmp(key
.dptr
, "ID=", 3) == 0) {
152 if (memcmp(key
.dptr
, "GUID=", 5) == 0) {
160 form a TDB_DATA for a record key
163 note that the key for a record can depend on whether the
164 dn refers to a case sensitive index record or not
166 TDB_DATA
ltdb_key(struct ldb_module
*module
, struct ldb_dn
*dn
)
168 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
170 char *key_str
= NULL
;
171 const char *dn_folded
= NULL
;
174 most DNs are case insensitive. The exception is index DNs for
175 case sensitive attributes
177 there are 3 cases dealt with in this code:
179 1) if the dn doesn't start with @ then uppercase the attribute
180 names and the attributes values of case insensitive attributes
181 2) if the dn starts with @ then leave it alone -
182 the indexing code handles the rest
185 dn_folded
= ldb_dn_get_casefold(dn
);
190 key_str
= talloc_strdup(ldb
, "DN=");
195 key_str
= talloc_strdup_append_buffer(key_str
, dn_folded
);
200 key
.dptr
= (uint8_t *)key_str
;
201 key
.dsize
= strlen(key_str
) + 1;
213 check special dn's have valid attributes
214 currently only @ATTRIBUTES is checked
216 static int ltdb_check_special_dn(struct ldb_module
*module
,
217 const struct ldb_message
*msg
)
219 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
222 if (! ldb_dn_is_special(msg
->dn
) ||
223 ! ldb_dn_check_special(msg
->dn
, LTDB_ATTRIBUTES
)) {
227 /* we have @ATTRIBUTES, let's check attributes are fine */
228 /* should we check that we deny multivalued attributes ? */
229 for (i
= 0; i
< msg
->num_elements
; i
++) {
230 if (ldb_attr_cmp(msg
->elements
[i
].name
, "distinguishedName") == 0) continue;
232 for (j
= 0; j
< msg
->elements
[i
].num_values
; j
++) {
233 if (ltdb_check_at_attributes_values(&msg
->elements
[i
].values
[j
]) != 0) {
234 ldb_set_errstring(ldb
, "Invalid attribute value in an @ATTRIBUTES entry");
235 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
245 we've made a modification to a dn - possibly reindex and
246 update sequence number
248 static int ltdb_modified(struct ldb_module
*module
, struct ldb_dn
*dn
)
250 int ret
= LDB_SUCCESS
;
251 struct ltdb_private
*ltdb
= talloc_get_type(ldb_module_get_private(module
), struct ltdb_private
);
253 /* only allow modifies inside a transaction, otherwise the
255 if (ltdb
->in_transaction
== 0) {
256 ldb_set_errstring(ldb_module_get_ctx(module
), "ltdb modify without transaction");
257 return LDB_ERR_OPERATIONS_ERROR
;
260 if (ldb_dn_is_special(dn
) &&
261 (ldb_dn_check_special(dn
, LTDB_INDEXLIST
) ||
262 ldb_dn_check_special(dn
, LTDB_ATTRIBUTES
)) )
264 if (ltdb
->warn_reindex
) {
265 ldb_debug(ldb_module_get_ctx(module
),
266 LDB_DEBUG_ERROR
, "Reindexing %s due to modification on %s",
267 tdb_name(ltdb
->tdb
), ldb_dn_get_linearized(dn
));
269 ret
= ltdb_reindex(module
);
272 /* If the modify was to a normal record, or any special except @BASEINFO, update the seq number */
273 if (ret
== LDB_SUCCESS
&&
274 !(ldb_dn_is_special(dn
) &&
275 ldb_dn_check_special(dn
, LTDB_BASEINFO
)) ) {
276 ret
= ltdb_increase_sequence_number(module
);
279 /* If the modify was to @OPTIONS, reload the cache */
280 if (ret
== LDB_SUCCESS
&&
281 ldb_dn_is_special(dn
) &&
282 (ldb_dn_check_special(dn
, LTDB_OPTIONS
)) ) {
283 ret
= ltdb_cache_reload(module
);
290 store a record into the db
292 int ltdb_store(struct ldb_module
*module
, const struct ldb_message
*msg
, int flgs
)
294 void *data
= ldb_module_get_private(module
);
295 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
296 TDB_DATA tdb_key
, tdb_data
;
297 struct ldb_val ldb_data
;
298 int ret
= LDB_SUCCESS
;
300 tdb_key
= ltdb_key(module
, msg
->dn
);
301 if (tdb_key
.dptr
== NULL
) {
302 return LDB_ERR_OTHER
;
305 ret
= ldb_pack_data(ldb_module_get_ctx(module
),
308 talloc_free(tdb_key
.dptr
);
309 return LDB_ERR_OTHER
;
312 tdb_data
.dptr
= ldb_data
.data
;
313 tdb_data
.dsize
= ldb_data
.length
;
315 ret
= tdb_store(ltdb
->tdb
, tdb_key
, tdb_data
, flgs
);
317 ret
= ltdb_err_map(tdb_error(ltdb
->tdb
));
322 talloc_free(tdb_key
.dptr
);
323 talloc_free(ldb_data
.data
);
330 check if a attribute is a single valued, for a given element
332 static bool ldb_tdb_single_valued(const struct ldb_schema_attribute
*a
,
333 struct ldb_message_element
*el
)
335 if (!a
) return false;
337 if (el
->flags
& LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK
) {
338 /* override from a ldb module, for example
339 used for the description field, which is
340 marked multi-valued in the schema but which
341 should not actually accept multiple
345 if (el
->flags
& LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK
) {
346 /* override from a ldb module, for example used for
347 deleted linked attribute entries */
351 if (a
->flags
& LDB_ATTR_FLAG_SINGLE_VALUE
) {
357 static int ltdb_add_internal(struct ldb_module
*module
,
358 struct ltdb_private
*ltdb
,
359 const struct ldb_message
*msg
,
360 bool check_single_value
)
362 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
363 int ret
= LDB_SUCCESS
;
366 for (i
=0;i
<msg
->num_elements
;i
++) {
367 struct ldb_message_element
*el
= &msg
->elements
[i
];
368 const struct ldb_schema_attribute
*a
= ldb_schema_attribute_by_name(ldb
, el
->name
);
370 if (el
->num_values
== 0) {
371 ldb_asprintf_errstring(ldb
, "attribute '%s' on '%s' specified, but with 0 values (illegal)",
372 el
->name
, ldb_dn_get_linearized(msg
->dn
));
373 return LDB_ERR_CONSTRAINT_VIOLATION
;
375 if (check_single_value
&&
376 el
->num_values
> 1 &&
377 ldb_tdb_single_valued(a
, el
)) {
378 ldb_asprintf_errstring(ldb
, "SINGLE-VALUE attribute %s on %s specified more than once",
379 el
->name
, ldb_dn_get_linearized(msg
->dn
));
380 return LDB_ERR_CONSTRAINT_VIOLATION
;
383 /* Do not check "@ATTRIBUTES" for duplicated values */
384 if (ldb_dn_is_special(msg
->dn
) &&
385 ldb_dn_check_special(msg
->dn
, LTDB_ATTRIBUTES
)) {
389 if (check_single_value
&&
391 LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK
)) {
392 struct ldb_val
*duplicate
= NULL
;
394 ret
= ldb_msg_find_duplicate_val(ldb
, discard_const(msg
),
396 if (ret
!= LDB_SUCCESS
) {
399 if (duplicate
!= NULL
) {
400 ldb_asprintf_errstring(
402 "attribute '%s': value '%.*s' on '%s' "
403 "provided more than once in ADD object",
405 (int)duplicate
->length
,
407 ldb_dn_get_linearized(msg
->dn
));
408 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
;
413 ret
= ltdb_store(module
, msg
, TDB_INSERT
);
414 if (ret
!= LDB_SUCCESS
) {
415 if (ret
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
416 ldb_asprintf_errstring(ldb
,
417 "Entry %s already exists",
418 ldb_dn_get_linearized(msg
->dn
));
423 ret
= ltdb_index_add_new(module
, ltdb
, msg
);
424 if (ret
!= LDB_SUCCESS
) {
428 ret
= ltdb_modified(module
, msg
->dn
);
434 add a record to the database
436 static int ltdb_add(struct ltdb_context
*ctx
)
438 struct ldb_module
*module
= ctx
->module
;
439 struct ldb_request
*req
= ctx
->req
;
440 void *data
= ldb_module_get_private(module
);
441 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
442 int ret
= LDB_SUCCESS
;
444 ret
= ltdb_check_special_dn(module
, req
->op
.add
.message
);
445 if (ret
!= LDB_SUCCESS
) {
449 ldb_request_set_state(req
, LDB_ASYNC_PENDING
);
451 if (ltdb_cache_load(module
) != 0) {
452 return LDB_ERR_OPERATIONS_ERROR
;
455 ret
= ltdb_add_internal(module
, ltdb
,
456 req
->op
.add
.message
, true);
462 delete a record from the database, not updating indexes (used for deleting
465 int ltdb_delete_noindex(struct ldb_module
*module
, struct ldb_dn
*dn
)
467 void *data
= ldb_module_get_private(module
);
468 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
472 tdb_key
= ltdb_key(module
, dn
);
474 return LDB_ERR_OTHER
;
477 ret
= tdb_delete(ltdb
->tdb
, tdb_key
);
478 talloc_free(tdb_key
.dptr
);
481 ret
= ltdb_err_map(tdb_error(ltdb
->tdb
));
487 static int ltdb_delete_internal(struct ldb_module
*module
, struct ldb_dn
*dn
)
489 struct ldb_message
*msg
;
490 int ret
= LDB_SUCCESS
;
492 msg
= ldb_msg_new(module
);
494 return LDB_ERR_OPERATIONS_ERROR
;
497 /* in case any attribute of the message was indexed, we need
498 to fetch the old record */
499 ret
= ltdb_search_dn1(module
, dn
, msg
, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
);
500 if (ret
!= LDB_SUCCESS
) {
501 /* not finding the old record is an error */
505 ret
= ltdb_delete_noindex(module
, dn
);
506 if (ret
!= LDB_SUCCESS
) {
510 /* remove any indexed attributes */
511 ret
= ltdb_index_delete(module
, msg
);
512 if (ret
!= LDB_SUCCESS
) {
516 ret
= ltdb_modified(module
, dn
);
517 if (ret
!= LDB_SUCCESS
) {
527 delete a record from the database
529 static int ltdb_delete(struct ltdb_context
*ctx
)
531 struct ldb_module
*module
= ctx
->module
;
532 struct ldb_request
*req
= ctx
->req
;
533 int ret
= LDB_SUCCESS
;
535 ldb_request_set_state(req
, LDB_ASYNC_PENDING
);
537 if (ltdb_cache_load(module
) != 0) {
538 return LDB_ERR_OPERATIONS_ERROR
;
541 ret
= ltdb_delete_internal(module
, req
->op
.del
.dn
);
547 find an element by attribute name. At the moment this does a linear search,
548 it should be re-coded to use a binary search once all places that modify
549 records guarantee sorted order
551 return the index of the first matching element if found, otherwise -1
553 static int find_element(const struct ldb_message
*msg
, const char *name
)
556 for (i
=0;i
<msg
->num_elements
;i
++) {
557 if (ldb_attr_cmp(msg
->elements
[i
].name
, name
) == 0) {
566 add an element to an existing record. Assumes a elements array that we
567 can call re-alloc on, and assumed that we can re-use the data pointers from
568 the passed in additional values. Use with care!
570 returns 0 on success, -1 on failure (and sets errno)
572 static int ltdb_msg_add_element(struct ldb_message
*msg
,
573 struct ldb_message_element
*el
)
575 struct ldb_message_element
*e2
;
578 if (el
->num_values
== 0) {
579 /* nothing to do here - we don't add empty elements */
583 e2
= talloc_realloc(msg
, msg
->elements
, struct ldb_message_element
,
584 msg
->num_elements
+1);
592 e2
= &msg
->elements
[msg
->num_elements
];
595 e2
->flags
= el
->flags
;
596 e2
->values
= talloc_array(msg
->elements
,
597 struct ldb_val
, el
->num_values
);
602 for (i
=0;i
<el
->num_values
;i
++) {
603 e2
->values
[i
] = el
->values
[i
];
605 e2
->num_values
= el
->num_values
;
613 delete all elements having a specified attribute name
615 static int msg_delete_attribute(struct ldb_module
*module
,
616 struct ltdb_private
*ltdb
,
617 struct ldb_message
*msg
, const char *name
)
621 struct ldb_message_element
*el
;
623 el
= ldb_msg_find_element(msg
, name
);
625 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
627 i
= el
- msg
->elements
;
629 ret
= ltdb_index_del_element(module
, ltdb
, msg
->dn
, el
);
630 if (ret
!= LDB_SUCCESS
) {
634 talloc_free(el
->values
);
635 if (msg
->num_elements
> (i
+1)) {
636 memmove(el
, el
+1, sizeof(*el
) * (msg
->num_elements
- (i
+1)));
639 msg
->elements
= talloc_realloc(msg
, msg
->elements
,
640 struct ldb_message_element
,
646 delete all elements matching an attribute name/value
648 return LDB Error on failure
650 static int msg_delete_element(struct ldb_module
*module
,
651 struct ltdb_private
*ltdb
,
652 struct ldb_message
*msg
,
654 const struct ldb_val
*val
)
656 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
659 struct ldb_message_element
*el
;
660 const struct ldb_schema_attribute
*a
;
662 found
= find_element(msg
, name
);
664 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
667 i
= (unsigned int) found
;
668 el
= &(msg
->elements
[i
]);
670 a
= ldb_schema_attribute_by_name(ldb
, el
->name
);
672 for (i
=0;i
<el
->num_values
;i
++) {
674 if (a
->syntax
->operator_fn
) {
675 ret
= a
->syntax
->operator_fn(ldb
, LDB_OP_EQUALITY
, a
,
676 &el
->values
[i
], val
, &matched
);
677 if (ret
!= LDB_SUCCESS
) return ret
;
679 matched
= (a
->syntax
->comparison_fn(ldb
, ldb
,
680 &el
->values
[i
], val
) == 0);
683 if (el
->num_values
== 1) {
684 return msg_delete_attribute(module
,
688 ret
= ltdb_index_del_value(module
, ltdb
, msg
->dn
, el
, i
);
689 if (ret
!= LDB_SUCCESS
) {
693 if (i
<el
->num_values
-1) {
694 memmove(&el
->values
[i
], &el
->values
[i
+1],
695 sizeof(el
->values
[i
])*
696 (el
->num_values
-(i
+1)));
700 /* per definition we find in a canonicalised message an
701 attribute value only once. So we are finished here */
707 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
712 modify a record - internal interface
714 yuck - this is O(n^2). Luckily n is usually small so we probably
715 get away with it, but if we ever have really large attribute lists
716 then we'll need to look at this again
718 'req' is optional, and is used to specify controls if supplied
720 int ltdb_modify_internal(struct ldb_module
*module
,
721 const struct ldb_message
*msg
,
722 struct ldb_request
*req
)
724 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
725 void *data
= ldb_module_get_private(module
);
726 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
727 struct ldb_message
*msg2
;
729 int ret
= LDB_SUCCESS
, idx
;
730 struct ldb_control
*control_permissive
= NULL
;
731 TALLOC_CTX
*mem_ctx
= talloc_new(req
);
733 if (mem_ctx
== NULL
) {
734 return ldb_module_oom(module
);
738 control_permissive
= ldb_request_get_control(req
,
739 LDB_CONTROL_PERMISSIVE_MODIFY_OID
);
742 msg2
= ldb_msg_new(mem_ctx
);
748 ret
= ltdb_search_dn1(module
, msg
->dn
,
750 LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
);
751 if (ret
!= LDB_SUCCESS
) {
755 for (i
=0; i
<msg
->num_elements
; i
++) {
756 struct ldb_message_element
*el
= &msg
->elements
[i
], *el2
;
757 struct ldb_val
*vals
;
758 const struct ldb_schema_attribute
*a
= ldb_schema_attribute_by_name(ldb
, el
->name
);
760 uint32_t options
= 0;
761 if (control_permissive
!= NULL
) {
762 options
|= LDB_MSG_FIND_COMMON_REMOVE_DUPLICATES
;
765 switch (msg
->elements
[i
].flags
& LDB_FLAG_MOD_MASK
) {
766 case LDB_FLAG_MOD_ADD
:
768 if (el
->num_values
== 0) {
769 ldb_asprintf_errstring(ldb
,
770 "attribute '%s': attribute on '%s' specified, but with 0 values (illegal)",
771 el
->name
, ldb_dn_get_linearized(msg2
->dn
));
772 ret
= LDB_ERR_CONSTRAINT_VIOLATION
;
776 /* make a copy of the array so that a permissive
777 * control can remove duplicates without changing the
778 * original values, but do not copy data as we do not
779 * need to keep it around once the operation is
781 if (control_permissive
) {
782 el
= talloc(msg2
, struct ldb_message_element
);
787 *el
= msg
->elements
[i
];
788 el
->values
= talloc_array(el
, struct ldb_val
, el
->num_values
);
789 if (el
->values
== NULL
) {
793 for (j
= 0; j
< el
->num_values
; j
++) {
794 el
->values
[j
] = msg
->elements
[i
].values
[j
];
798 if (el
->num_values
> 1 && ldb_tdb_single_valued(a
, el
)) {
799 ldb_asprintf_errstring(ldb
, "SINGLE-VALUE attribute %s on %s specified more than once",
800 el
->name
, ldb_dn_get_linearized(msg2
->dn
));
801 ret
= LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
;
805 /* Checks if element already exists */
806 idx
= find_element(msg2
, el
->name
);
808 if (ltdb_msg_add_element(msg2
, el
) != 0) {
812 ret
= ltdb_index_add_element(module
, ltdb
,
815 if (ret
!= LDB_SUCCESS
) {
819 j
= (unsigned int) idx
;
820 el2
= &(msg2
->elements
[j
]);
822 /* We cannot add another value on a existing one
823 if the attribute is single-valued */
824 if (ldb_tdb_single_valued(a
, el
)) {
825 ldb_asprintf_errstring(ldb
, "SINGLE-VALUE attribute %s on %s specified more than once",
826 el
->name
, ldb_dn_get_linearized(msg2
->dn
));
827 ret
= LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
;
831 /* Check that values don't exist yet on multi-
832 valued attributes or aren't provided twice */
834 LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK
)) {
835 struct ldb_val
*duplicate
= NULL
;
836 ret
= ldb_msg_find_common_values(ldb
,
843 LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
) {
844 ldb_asprintf_errstring(ldb
,
845 "attribute '%s': value "
846 "#%u on '%s' already "
847 "exists", el
->name
, j
,
848 ldb_dn_get_linearized(msg2
->dn
));
850 } else if (ret
!= LDB_SUCCESS
) {
854 ret
= ldb_msg_find_duplicate_val(
855 ldb
, msg2
, el
, &duplicate
, 0);
856 if (ret
!= LDB_SUCCESS
) {
859 if (duplicate
!= NULL
) {
860 ldb_asprintf_errstring(
862 "attribute '%s': value "
864 "provided more than "
867 (int)duplicate
->length
,
869 ldb_dn_get_linearized(msg
->dn
));
870 ret
= LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
;
875 /* Now combine existing and new values to a new
877 vals
= talloc_realloc(msg2
->elements
,
878 el2
->values
, struct ldb_val
,
879 el2
->num_values
+ el
->num_values
);
886 for (j
=0; j
<el
->num_values
; j
++) {
887 vals
[el2
->num_values
+ j
] =
888 ldb_val_dup(vals
, &el
->values
[j
]);
892 el2
->num_values
+= el
->num_values
;
894 ret
= ltdb_index_add_element(module
, ltdb
,
896 if (ret
!= LDB_SUCCESS
) {
903 case LDB_FLAG_MOD_REPLACE
:
905 if (el
->num_values
> 1 && ldb_tdb_single_valued(a
, el
)) {
906 ldb_asprintf_errstring(ldb
, "SINGLE-VALUE attribute %s on %s specified more than once",
907 el
->name
, ldb_dn_get_linearized(msg2
->dn
));
908 ret
= LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
;
913 * We don't need to check this if we have been
914 * pre-screened by the repl_meta_data module
915 * in Samba, or someone else who can claim to
916 * know what they are doing.
918 if (!(el
->flags
& LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK
)) {
919 struct ldb_val
*duplicate
= NULL
;
921 ret
= ldb_msg_find_duplicate_val(ldb
, msg2
, el
,
923 if (ret
!= LDB_SUCCESS
) {
926 if (duplicate
!= NULL
) {
927 ldb_asprintf_errstring(
929 "attribute '%s': value '%.*s' "
930 "on '%s' provided more than "
933 (int)duplicate
->length
,
935 ldb_dn_get_linearized(msg2
->dn
));
936 ret
= LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
;
941 /* Checks if element already exists */
942 idx
= find_element(msg2
, el
->name
);
944 j
= (unsigned int) idx
;
945 el2
= &(msg2
->elements
[j
]);
947 /* we consider two elements to be
948 * equal only if the order
949 * matches. This allows dbcheck to
950 * fix the ordering on attributes
951 * where order matters, such as
954 if (ldb_msg_element_equal_ordered(el
, el2
)) {
958 /* Delete the attribute if it exists in the DB */
959 if (msg_delete_attribute(module
, ltdb
,
967 /* Recreate it with the new values */
968 if (ltdb_msg_add_element(msg2
, el
) != 0) {
973 ret
= ltdb_index_add_element(module
, ltdb
,
975 if (ret
!= LDB_SUCCESS
) {
981 case LDB_FLAG_MOD_DELETE
:
982 dn
= ldb_dn_get_linearized(msg2
->dn
);
988 if (msg
->elements
[i
].num_values
== 0) {
989 /* Delete the whole attribute */
990 ret
= msg_delete_attribute(module
,
993 msg
->elements
[i
].name
);
994 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
&&
995 control_permissive
) {
998 ldb_asprintf_errstring(ldb
,
999 "attribute '%s': no such attribute for delete on '%s'",
1000 msg
->elements
[i
].name
, dn
);
1002 if (ret
!= LDB_SUCCESS
) {
1006 /* Delete specified values from an attribute */
1007 for (j
=0; j
< msg
->elements
[i
].num_values
; j
++) {
1008 ret
= msg_delete_element(module
,
1011 msg
->elements
[i
].name
,
1012 &msg
->elements
[i
].values
[j
]);
1013 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
&&
1014 control_permissive
) {
1016 } else if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
1017 ldb_asprintf_errstring(ldb
,
1018 "attribute '%s': no matching attribute value while deleting attribute on '%s'",
1019 msg
->elements
[i
].name
, dn
);
1021 if (ret
!= LDB_SUCCESS
) {
1028 ldb_asprintf_errstring(ldb
,
1029 "attribute '%s': invalid modify flags on '%s': 0x%x",
1030 msg
->elements
[i
].name
, ldb_dn_get_linearized(msg
->dn
),
1031 msg
->elements
[i
].flags
& LDB_FLAG_MOD_MASK
);
1032 ret
= LDB_ERR_PROTOCOL_ERROR
;
1037 ret
= ltdb_store(module
, msg2
, TDB_MODIFY
);
1038 if (ret
!= LDB_SUCCESS
) {
1042 ret
= ltdb_modified(module
, msg2
->dn
);
1043 if (ret
!= LDB_SUCCESS
) {
1048 TALLOC_FREE(mem_ctx
);
1055 static int ltdb_modify(struct ltdb_context
*ctx
)
1057 struct ldb_module
*module
= ctx
->module
;
1058 struct ldb_request
*req
= ctx
->req
;
1059 int ret
= LDB_SUCCESS
;
1061 ret
= ltdb_check_special_dn(module
, req
->op
.mod
.message
);
1062 if (ret
!= LDB_SUCCESS
) {
1066 ldb_request_set_state(req
, LDB_ASYNC_PENDING
);
1068 if (ltdb_cache_load(module
) != 0) {
1069 return LDB_ERR_OPERATIONS_ERROR
;
1072 ret
= ltdb_modify_internal(module
, req
->op
.mod
.message
, req
);
1080 static int ltdb_rename(struct ltdb_context
*ctx
)
1082 struct ldb_module
*module
= ctx
->module
;
1083 void *data
= ldb_module_get_private(module
);
1084 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
1085 struct ldb_request
*req
= ctx
->req
;
1086 struct ldb_message
*msg
;
1087 int ret
= LDB_SUCCESS
;
1088 TDB_DATA tdb_key
, tdb_key_old
;
1090 ldb_request_set_state(req
, LDB_ASYNC_PENDING
);
1092 if (ltdb_cache_load(ctx
->module
) != 0) {
1093 return LDB_ERR_OPERATIONS_ERROR
;
1096 msg
= ldb_msg_new(ctx
);
1098 return LDB_ERR_OPERATIONS_ERROR
;
1101 /* we need to fetch the old record to re-add under the new name */
1102 ret
= ltdb_search_dn1(module
, req
->op
.rename
.olddn
, msg
,
1103 LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
);
1104 if (ret
!= LDB_SUCCESS
) {
1105 /* not finding the old record is an error */
1109 /* We need to, before changing the DB, check if the new DN
1110 * exists, so we can return this error to the caller with an
1112 tdb_key
= ltdb_key(module
, req
->op
.rename
.newdn
);
1113 if (!tdb_key
.dptr
) {
1115 return LDB_ERR_OPERATIONS_ERROR
;
1118 tdb_key_old
= ltdb_key(module
, req
->op
.rename
.olddn
);
1119 if (!tdb_key_old
.dptr
) {
1121 talloc_free(tdb_key
.dptr
);
1122 return LDB_ERR_OPERATIONS_ERROR
;
1125 /* Only declare a conflict if the new DN already exists, and it isn't a case change on the old DN */
1126 if (tdb_key_old
.dsize
!= tdb_key
.dsize
|| memcmp(tdb_key
.dptr
, tdb_key_old
.dptr
, tdb_key
.dsize
) != 0) {
1127 if (tdb_exists(ltdb
->tdb
, tdb_key
)) {
1128 talloc_free(tdb_key_old
.dptr
);
1129 talloc_free(tdb_key
.dptr
);
1130 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1131 "Entry %s already exists",
1132 ldb_dn_get_linearized(req
->op
.rename
.newdn
));
1133 /* finding the new record already in the DB is an error */
1135 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
1138 talloc_free(tdb_key_old
.dptr
);
1139 talloc_free(tdb_key
.dptr
);
1141 /* Always delete first then add, to avoid conflicts with
1142 * unique indexes. We rely on the transaction to make this
1145 ret
= ltdb_delete_internal(module
, msg
->dn
);
1146 if (ret
!= LDB_SUCCESS
) {
1151 msg
->dn
= ldb_dn_copy(msg
, req
->op
.rename
.newdn
);
1152 if (msg
->dn
== NULL
) {
1154 return LDB_ERR_OPERATIONS_ERROR
;
1157 /* We don't check single value as we can have more than 1 with
1158 * deleted attributes. We could go through all elements but that's
1159 * maybe not the most efficient way
1161 ret
= ltdb_add_internal(module
, ltdb
, msg
, false);
1168 static int ltdb_start_trans(struct ldb_module
*module
)
1170 void *data
= ldb_module_get_private(module
);
1171 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
1173 if (tdb_transaction_start(ltdb
->tdb
) != 0) {
1174 return ltdb_err_map(tdb_error(ltdb
->tdb
));
1177 ltdb
->in_transaction
++;
1179 ltdb_index_transaction_start(module
);
1184 static int ltdb_prepare_commit(struct ldb_module
*module
)
1187 void *data
= ldb_module_get_private(module
);
1188 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
1190 if (ltdb
->in_transaction
!= 1) {
1194 ret
= ltdb_index_transaction_commit(module
);
1195 if (ret
!= LDB_SUCCESS
) {
1196 tdb_transaction_cancel(ltdb
->tdb
);
1197 ltdb
->in_transaction
--;
1201 if (tdb_transaction_prepare_commit(ltdb
->tdb
) != 0) {
1202 ret
= ltdb_err_map(tdb_error(ltdb
->tdb
));
1203 ltdb
->in_transaction
--;
1204 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1205 "Failure during tdb_transaction_prepare_commit(): %s -> %s",
1206 tdb_errorstr(ltdb
->tdb
),
1211 ltdb
->prepared_commit
= true;
1216 static int ltdb_end_trans(struct ldb_module
*module
)
1219 void *data
= ldb_module_get_private(module
);
1220 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
1222 if (!ltdb
->prepared_commit
) {
1223 ret
= ltdb_prepare_commit(module
);
1224 if (ret
!= LDB_SUCCESS
) {
1229 ltdb
->in_transaction
--;
1230 ltdb
->prepared_commit
= false;
1232 if (tdb_transaction_commit(ltdb
->tdb
) != 0) {
1233 ret
= ltdb_err_map(tdb_error(ltdb
->tdb
));
1234 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1235 "Failure during tdb_transaction_commit(): %s -> %s",
1236 tdb_errorstr(ltdb
->tdb
),
1244 static int ltdb_del_trans(struct ldb_module
*module
)
1246 void *data
= ldb_module_get_private(module
);
1247 struct ltdb_private
*ltdb
= talloc_get_type(data
, struct ltdb_private
);
1249 ltdb
->in_transaction
--;
1251 if (ltdb_index_transaction_cancel(module
) != 0) {
1252 tdb_transaction_cancel(ltdb
->tdb
);
1253 return ltdb_err_map(tdb_error(ltdb
->tdb
));
1256 tdb_transaction_cancel(ltdb
->tdb
);
1261 return sequenceNumber from @BASEINFO
1263 static int ltdb_sequence_number(struct ltdb_context
*ctx
,
1264 struct ldb_extended
**ext
)
1266 struct ldb_context
*ldb
;
1267 struct ldb_module
*module
= ctx
->module
;
1268 struct ldb_request
*req
= ctx
->req
;
1269 TALLOC_CTX
*tmp_ctx
= NULL
;
1270 struct ldb_seqnum_request
*seq
;
1271 struct ldb_seqnum_result
*res
;
1272 struct ldb_message
*msg
= NULL
;
1275 int ret
= LDB_SUCCESS
;
1277 ldb
= ldb_module_get_ctx(module
);
1279 seq
= talloc_get_type(req
->op
.extended
.data
,
1280 struct ldb_seqnum_request
);
1282 return LDB_ERR_OPERATIONS_ERROR
;
1285 ldb_request_set_state(req
, LDB_ASYNC_PENDING
);
1287 if (ltdb_lock_read(module
) != 0) {
1288 return LDB_ERR_OPERATIONS_ERROR
;
1291 res
= talloc_zero(req
, struct ldb_seqnum_result
);
1293 ret
= LDB_ERR_OPERATIONS_ERROR
;
1297 tmp_ctx
= talloc_new(req
);
1298 if (tmp_ctx
== NULL
) {
1299 ret
= LDB_ERR_OPERATIONS_ERROR
;
1303 dn
= ldb_dn_new(tmp_ctx
, ldb
, LTDB_BASEINFO
);
1305 ret
= LDB_ERR_OPERATIONS_ERROR
;
1309 msg
= ldb_msg_new(tmp_ctx
);
1311 ret
= LDB_ERR_OPERATIONS_ERROR
;
1315 ret
= ltdb_search_dn1(module
, dn
, msg
, 0);
1316 if (ret
!= LDB_SUCCESS
) {
1320 switch (seq
->type
) {
1321 case LDB_SEQ_HIGHEST_SEQ
:
1322 res
->seq_num
= ldb_msg_find_attr_as_uint64(msg
, LTDB_SEQUENCE_NUMBER
, 0);
1325 res
->seq_num
= ldb_msg_find_attr_as_uint64(msg
, LTDB_SEQUENCE_NUMBER
, 0);
1328 case LDB_SEQ_HIGHEST_TIMESTAMP
:
1329 date
= ldb_msg_find_attr_as_string(msg
, LTDB_MOD_TIMESTAMP
, NULL
);
1331 res
->seq_num
= ldb_string_to_time(date
);
1334 /* zero is as good as anything when we don't know */
1339 *ext
= talloc_zero(req
, struct ldb_extended
);
1341 ret
= LDB_ERR_OPERATIONS_ERROR
;
1344 (*ext
)->oid
= LDB_EXTENDED_SEQUENCE_NUMBER
;
1345 (*ext
)->data
= talloc_steal(*ext
, res
);
1348 talloc_free(tmp_ctx
);
1349 ltdb_unlock_read(module
);
1353 static void ltdb_request_done(struct ltdb_context
*ctx
, int error
)
1355 struct ldb_context
*ldb
;
1356 struct ldb_request
*req
;
1357 struct ldb_reply
*ares
;
1359 ldb
= ldb_module_get_ctx(ctx
->module
);
1362 /* if we already returned an error just return */
1363 if (ldb_request_get_status(req
) != LDB_SUCCESS
) {
1367 ares
= talloc_zero(req
, struct ldb_reply
);
1370 req
->callback(req
, NULL
);
1373 ares
->type
= LDB_REPLY_DONE
;
1374 ares
->error
= error
;
1376 req
->callback(req
, ares
);
1379 static void ltdb_timeout(struct tevent_context
*ev
,
1380 struct tevent_timer
*te
,
1384 struct ltdb_context
*ctx
;
1385 ctx
= talloc_get_type(private_data
, struct ltdb_context
);
1387 if (!ctx
->request_terminated
) {
1388 /* request is done now */
1389 ltdb_request_done(ctx
, LDB_ERR_TIME_LIMIT_EXCEEDED
);
1393 /* neutralize the spy */
1394 ctx
->spy
->ctx
= NULL
;
1400 static void ltdb_request_extended_done(struct ltdb_context
*ctx
,
1401 struct ldb_extended
*ext
,
1404 struct ldb_context
*ldb
;
1405 struct ldb_request
*req
;
1406 struct ldb_reply
*ares
;
1408 ldb
= ldb_module_get_ctx(ctx
->module
);
1411 /* if we already returned an error just return */
1412 if (ldb_request_get_status(req
) != LDB_SUCCESS
) {
1416 ares
= talloc_zero(req
, struct ldb_reply
);
1419 req
->callback(req
, NULL
);
1422 ares
->type
= LDB_REPLY_DONE
;
1423 ares
->response
= ext
;
1424 ares
->error
= error
;
1426 req
->callback(req
, ares
);
1429 static void ltdb_handle_extended(struct ltdb_context
*ctx
)
1431 struct ldb_extended
*ext
= NULL
;
1434 if (strcmp(ctx
->req
->op
.extended
.oid
,
1435 LDB_EXTENDED_SEQUENCE_NUMBER
) == 0) {
1436 /* get sequence number */
1437 ret
= ltdb_sequence_number(ctx
, &ext
);
1439 /* not recognized */
1440 ret
= LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
;
1443 ltdb_request_extended_done(ctx
, ext
, ret
);
1446 static void ltdb_callback(struct tevent_context
*ev
,
1447 struct tevent_timer
*te
,
1451 struct ltdb_context
*ctx
;
1454 ctx
= talloc_get_type(private_data
, struct ltdb_context
);
1456 if (ctx
->request_terminated
) {
1460 switch (ctx
->req
->operation
) {
1462 ret
= ltdb_search(ctx
);
1465 ret
= ltdb_add(ctx
);
1468 ret
= ltdb_modify(ctx
);
1471 ret
= ltdb_delete(ctx
);
1474 ret
= ltdb_rename(ctx
);
1477 ltdb_handle_extended(ctx
);
1480 /* no other op supported */
1481 ret
= LDB_ERR_PROTOCOL_ERROR
;
1484 if (!ctx
->request_terminated
) {
1485 /* request is done now */
1486 ltdb_request_done(ctx
, ret
);
1491 /* neutralize the spy */
1492 ctx
->spy
->ctx
= NULL
;
1498 static int ltdb_request_destructor(void *ptr
)
1500 struct ltdb_req_spy
*spy
= talloc_get_type(ptr
, struct ltdb_req_spy
);
1502 if (spy
->ctx
!= NULL
) {
1503 spy
->ctx
->spy
= NULL
;
1504 spy
->ctx
->request_terminated
= true;
1511 static int ltdb_handle_request(struct ldb_module
*module
,
1512 struct ldb_request
*req
)
1514 struct ldb_control
*control_permissive
;
1515 struct ldb_context
*ldb
;
1516 struct tevent_context
*ev
;
1517 struct ltdb_context
*ac
;
1518 struct tevent_timer
*te
;
1522 ldb
= ldb_module_get_ctx(module
);
1524 control_permissive
= ldb_request_get_control(req
,
1525 LDB_CONTROL_PERMISSIVE_MODIFY_OID
);
1527 for (i
= 0; req
->controls
&& req
->controls
[i
]; i
++) {
1528 if (req
->controls
[i
]->critical
&&
1529 req
->controls
[i
] != control_permissive
) {
1530 ldb_asprintf_errstring(ldb
, "Unsupported critical extension %s",
1531 req
->controls
[i
]->oid
);
1532 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
;
1536 if (req
->starttime
== 0 || req
->timeout
== 0) {
1537 ldb_set_errstring(ldb
, "Invalid timeout settings");
1538 return LDB_ERR_TIME_LIMIT_EXCEEDED
;
1541 ev
= ldb_handle_get_event_context(req
->handle
);
1543 ac
= talloc_zero(ldb
, struct ltdb_context
);
1546 return LDB_ERR_OPERATIONS_ERROR
;
1549 ac
->module
= module
;
1554 te
= tevent_add_timer(ev
, ac
, tv
, ltdb_callback
, ac
);
1557 return LDB_ERR_OPERATIONS_ERROR
;
1560 if (req
->timeout
> 0) {
1561 tv
.tv_sec
= req
->starttime
+ req
->timeout
;
1563 ac
->timeout_event
= tevent_add_timer(ev
, ac
, tv
,
1565 if (NULL
== ac
->timeout_event
) {
1567 return LDB_ERR_OPERATIONS_ERROR
;
1571 /* set a spy so that we do not try to use the request context
1572 * if it is freed before ltdb_callback fires */
1573 ac
->spy
= talloc(req
, struct ltdb_req_spy
);
1574 if (NULL
== ac
->spy
) {
1576 return LDB_ERR_OPERATIONS_ERROR
;
1580 talloc_set_destructor((TALLOC_CTX
*)ac
->spy
, ltdb_request_destructor
);
1585 static int ltdb_init_rootdse(struct ldb_module
*module
)
1587 /* ignore errors on this - we expect it for non-sam databases */
1588 ldb_mod_register_control(module
, LDB_CONTROL_PERMISSIVE_MODIFY_OID
);
1590 /* there can be no module beyond the backend, just return */
1594 static const struct ldb_module_ops ltdb_ops
= {
1596 .init_context
= ltdb_init_rootdse
,
1597 .search
= ltdb_handle_request
,
1598 .add
= ltdb_handle_request
,
1599 .modify
= ltdb_handle_request
,
1600 .del
= ltdb_handle_request
,
1601 .rename
= ltdb_handle_request
,
1602 .extended
= ltdb_handle_request
,
1603 .start_transaction
= ltdb_start_trans
,
1604 .end_transaction
= ltdb_end_trans
,
1605 .prepare_commit
= ltdb_prepare_commit
,
1606 .del_transaction
= ltdb_del_trans
,
1607 .read_lock
= ltdb_lock_read
,
1608 .read_unlock
= ltdb_unlock_read
,
1612 connect to the database
1614 static int ltdb_connect(struct ldb_context
*ldb
, const char *url
,
1615 unsigned int flags
, const char *options
[],
1616 struct ldb_module
**_module
)
1618 struct ldb_module
*module
;
1620 int tdb_flags
, open_flags
;
1621 struct ltdb_private
*ltdb
;
1624 * We hold locks, so we must use a private event context
1625 * on each returned handle
1628 ldb_set_require_private_event_context(ldb
);
1631 if (strchr(url
, ':')) {
1632 if (strncmp(url
, "tdb://", 6) != 0) {
1633 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
1634 "Invalid tdb URL '%s'", url
);
1635 return LDB_ERR_OPERATIONS_ERROR
;
1642 tdb_flags
= TDB_DEFAULT
| TDB_SEQNUM
;
1644 /* check for the 'nosync' option */
1645 if (flags
& LDB_FLG_NOSYNC
) {
1646 tdb_flags
|= TDB_NOSYNC
;
1649 /* and nommap option */
1650 if (flags
& LDB_FLG_NOMMAP
) {
1651 tdb_flags
|= TDB_NOMMAP
;
1654 if (flags
& LDB_FLG_RDONLY
) {
1655 open_flags
= O_RDONLY
;
1656 } else if (flags
& LDB_FLG_DONT_CREATE_DB
) {
1657 open_flags
= O_RDWR
;
1659 open_flags
= O_CREAT
| O_RDWR
;
1662 ltdb
= talloc_zero(ldb
, struct ltdb_private
);
1665 return LDB_ERR_OPERATIONS_ERROR
;
1668 /* note that we use quite a large default hash size */
1669 ltdb
->tdb
= ltdb_wrap_open(ltdb
, path
, 10000,
1670 tdb_flags
, open_flags
,
1671 ldb_get_create_perms(ldb
), ldb
);
1673 ldb_asprintf_errstring(ldb
,
1674 "Unable to open tdb '%s': %s", path
, strerror(errno
));
1675 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
1676 "Unable to open tdb '%s': %s", path
, strerror(errno
));
1678 if (errno
== EACCES
|| errno
== EPERM
) {
1679 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1681 return LDB_ERR_OPERATIONS_ERROR
;
1684 if (getenv("LDB_WARN_UNINDEXED")) {
1685 ltdb
->warn_unindexed
= true;
1688 if (getenv("LDB_WARN_REINDEX")) {
1689 ltdb
->warn_reindex
= true;
1692 ltdb
->sequence_number
= 0;
1694 module
= ldb_module_new(ldb
, ldb
, "ldb_tdb backend", <db_ops
);
1698 return LDB_ERR_OPERATIONS_ERROR
;
1700 ldb_module_set_private(module
, ltdb
);
1701 talloc_steal(module
, ltdb
);
1703 if (ltdb_cache_load(module
) != 0) {
1704 ldb_asprintf_errstring(ldb
,
1705 "Unable to load ltdb cache records of tdb '%s'", path
);
1706 talloc_free(module
);
1707 return LDB_ERR_OPERATIONS_ERROR
;
1714 int ldb_tdb_init(const char *version
)
1716 LDB_MODULE_CHECK_VERSION(version
);
1717 return ldb_register_backend("tdb", ltdb_connect
, false);