4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Stefan Metzmacher 2004
6 Copyright (C) Simo Sorce 2006-2008
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 asyncronous calls
43 * - description: make it possible to use event contexts
52 map a tdb error code to a ldb error code
54 static int ltdb_err_map(enum TDB_ERROR tdb_code)
62 return LDB_ERR_OPERATIONS_ERROR;
64 return LDB_ERR_PROTOCOL_ERROR;
68 case TDB_ERR_LOCK_TIMEOUT:
69 return LDB_ERR_TIME_LIMIT_EXCEEDED;
71 return LDB_ERR_ENTRY_ALREADY_EXISTS;
73 return LDB_ERR_NO_SUCH_OBJECT;
75 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
81 lock the database for read - use by ltdb_search and ltdb_sequence_number
83 int ltdb_lock_read(struct ldb_module *module)
85 void *data = ldb_module_get_private(module);
86 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
87 if (ltdb->in_transaction == 0) {
88 return tdb_lockall_read(ltdb->tdb);
94 unlock the database after a ltdb_lock_read()
96 int ltdb_unlock_read(struct ldb_module *module)
98 void *data = ldb_module_get_private(module);
99 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
100 if (ltdb->in_transaction == 0) {
101 return tdb_unlockall_read(ltdb->tdb);
108 form a TDB_DATA for a record key
111 note that the key for a record can depend on whether the
112 dn refers to a case sensitive index record or not
114 struct TDB_DATA ltdb_key(struct ldb_module *module, struct ldb_dn *dn)
116 struct ldb_context *ldb = ldb_module_get_ctx(module);
118 char *key_str = NULL;
119 const char *dn_folded = NULL;
122 most DNs are case insensitive. The exception is index DNs for
123 case sensitive attributes
125 there are 3 cases dealt with in this code:
127 1) if the dn doesn't start with @ then uppercase the attribute
128 names and the attributes values of case insensitive attributes
129 2) if the dn starts with @ then leave it alone -
130 the indexing code handles the rest
133 dn_folded = ldb_dn_get_casefold(dn);
138 key_str = talloc_strdup(ldb, "DN=");
143 key_str = talloc_strdup_append_buffer(key_str, dn_folded);
148 key.dptr = (uint8_t *)key_str;
149 key.dsize = strlen(key_str) + 1;
161 check special dn's have valid attributes
162 currently only @ATTRIBUTES is checked
164 static int ltdb_check_special_dn(struct ldb_module *module,
165 const struct ldb_message *msg)
167 struct ldb_context *ldb = ldb_module_get_ctx(module);
170 if (! ldb_dn_is_special(msg->dn) ||
171 ! ldb_dn_check_special(msg->dn, LTDB_ATTRIBUTES)) {
175 /* we have @ATTRIBUTES, let's check attributes are fine */
176 /* should we check that we deny multivalued attributes ? */
177 for (i = 0; i < msg->num_elements; i++) {
178 for (j = 0; j < msg->elements[i].num_values; j++) {
179 if (ltdb_check_at_attributes_values(&msg->elements[i].values[j]) != 0) {
180 ldb_set_errstring(ldb, "Invalid attribute value in an @ATTRIBUTES entry");
181 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
191 we've made a modification to a dn - possibly reindex and
192 update sequence number
194 static int ltdb_modified(struct ldb_module *module, struct ldb_dn *dn)
196 int ret = LDB_SUCCESS;
198 if (ldb_dn_is_special(dn) &&
199 (ldb_dn_check_special(dn, LTDB_INDEXLIST) ||
200 ldb_dn_check_special(dn, LTDB_ATTRIBUTES)) ) {
201 ret = ltdb_reindex(module);
204 if (ret == LDB_SUCCESS &&
205 !(ldb_dn_is_special(dn) &&
206 ldb_dn_check_special(dn, LTDB_BASEINFO)) ) {
207 ret = ltdb_increase_sequence_number(module);
214 store a record into the db
216 int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs)
218 void *data = ldb_module_get_private(module);
219 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
220 TDB_DATA tdb_key, tdb_data;
223 tdb_key = ltdb_key(module, msg->dn);
225 return LDB_ERR_OTHER;
228 ret = ltdb_pack_data(module, msg, &tdb_data);
230 talloc_free(tdb_key.dptr);
231 return LDB_ERR_OTHER;
234 ret = tdb_store(ltdb->tdb, tdb_key, tdb_data, flgs);
236 ret = ltdb_err_map(tdb_error(ltdb->tdb));
240 ret = ltdb_index_add(module, msg);
241 if (ret != LDB_SUCCESS) {
242 tdb_delete(ltdb->tdb, tdb_key);
246 talloc_free(tdb_key.dptr);
247 talloc_free(tdb_data.dptr);
253 static int ltdb_add_internal(struct ldb_module *module,
254 const struct ldb_message *msg)
256 struct ldb_context *ldb = ldb_module_get_ctx(module);
259 ret = ltdb_check_special_dn(module, msg);
260 if (ret != LDB_SUCCESS) {
264 if (ltdb_cache_load(module) != 0) {
265 return LDB_ERR_OPERATIONS_ERROR;
268 for (i=0;i<msg->num_elements;i++) {
269 struct ldb_message_element *el = &msg->elements[i];
270 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
272 if (el->num_values == 0) {
273 ldb_asprintf_errstring(ldb, "attribute %s on %s specified, but with 0 values (illegal)",
274 el->name, ldb_dn_get_linearized(msg->dn));
275 return LDB_ERR_CONSTRAINT_VIOLATION;
277 if (a && a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
278 if (el->num_values > 1) {
279 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s speicified more than once",
280 el->name, ldb_dn_get_linearized(msg->dn));
281 return LDB_ERR_CONSTRAINT_VIOLATION;
286 ret = ltdb_store(module, msg, TDB_INSERT);
288 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
289 ldb_asprintf_errstring(ldb,
290 "Entry %s already exists",
291 ldb_dn_get_linearized(msg->dn));
295 if (ret == LDB_SUCCESS) {
296 ret = ltdb_index_one(module, msg, 1);
297 if (ret != LDB_SUCCESS) {
301 ret = ltdb_modified(module, msg->dn);
302 if (ret != LDB_SUCCESS) {
311 add a record to the database
313 static int ltdb_add(struct ltdb_context *ctx)
315 struct ldb_module *module = ctx->module;
316 struct ldb_request *req = ctx->req;
319 ldb_request_set_state(req, LDB_ASYNC_PENDING);
321 tret = ltdb_add_internal(module, req->op.add.message);
322 if (tret != LDB_SUCCESS) {
330 delete a record from the database, not updating indexes (used for deleting
333 int ltdb_delete_noindex(struct ldb_module *module, struct ldb_dn *dn)
335 void *data = ldb_module_get_private(module);
336 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
340 tdb_key = ltdb_key(module, dn);
342 return LDB_ERR_OTHER;
345 ret = tdb_delete(ltdb->tdb, tdb_key);
346 talloc_free(tdb_key.dptr);
349 ret = ltdb_err_map(tdb_error(ltdb->tdb));
355 static int ltdb_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
357 struct ldb_message *msg;
360 msg = talloc(module, struct ldb_message);
362 return LDB_ERR_OPERATIONS_ERROR;
365 /* in case any attribute of the message was indexed, we need
366 to fetch the old record */
367 ret = ltdb_search_dn1(module, dn, msg);
368 if (ret != LDB_SUCCESS) {
369 /* not finding the old record is an error */
373 ret = ltdb_delete_noindex(module, dn);
374 if (ret != LDB_SUCCESS) {
378 /* remove one level attribute */
379 ret = ltdb_index_one(module, msg, 0);
380 if (ret != LDB_SUCCESS) {
384 /* remove any indexed attributes */
385 ret = ltdb_index_del(module, msg);
386 if (ret != LDB_SUCCESS) {
390 ret = ltdb_modified(module, dn);
391 if (ret != LDB_SUCCESS) {
401 delete a record from the database
403 static int ltdb_delete(struct ltdb_context *ctx)
405 struct ldb_module *module = ctx->module;
406 struct ldb_request *req = ctx->req;
409 ldb_request_set_state(req, LDB_ASYNC_PENDING);
411 if (ltdb_cache_load(module) != 0) {
412 return LDB_ERR_OPERATIONS_ERROR;
415 tret = ltdb_delete_internal(module, req->op.del.dn);
416 if (tret != LDB_SUCCESS) {
424 find an element by attribute name. At the moment this does a linear search,
425 it should be re-coded to use a binary search once all places that modify
426 records guarantee sorted order
428 return the index of the first matching element if found, otherwise -1
430 static int find_element(const struct ldb_message *msg, const char *name)
433 for (i=0;i<msg->num_elements;i++) {
434 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
443 add an element to an existing record. Assumes a elements array that we
444 can call re-alloc on, and assumed that we can re-use the data pointers from
445 the passed in additional values. Use with care!
447 returns 0 on success, -1 on failure (and sets errno)
449 static int msg_add_element(struct ldb_context *ldb,
450 struct ldb_message *msg,
451 struct ldb_message_element *el)
453 struct ldb_message_element *e2;
456 e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
457 msg->num_elements+1);
465 e2 = &msg->elements[msg->num_elements];
468 e2->flags = el->flags;
470 if (el->num_values != 0) {
471 e2->values = talloc_array(msg->elements,
472 struct ldb_val, el->num_values);
478 for (i=0;i<el->num_values;i++) {
479 e2->values[i] = el->values[i];
481 e2->num_values = el->num_values;
489 delete all elements having a specified attribute name
491 static int msg_delete_attribute(struct ldb_module *module,
492 struct ldb_context *ldb,
493 struct ldb_message *msg, const char *name)
498 dn = ldb_dn_get_linearized(msg->dn);
503 for (i=0;i<msg->num_elements;i++) {
504 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
505 for (j=0;j<msg->elements[i].num_values;j++) {
506 ltdb_index_del_value(module, dn,
507 &msg->elements[i], j);
509 talloc_free(msg->elements[i].values);
510 if (msg->num_elements > (i+1)) {
511 memmove(&msg->elements[i],
513 sizeof(struct ldb_message_element)*
514 (msg->num_elements - (i+1)));
518 msg->elements = talloc_realloc(msg, msg->elements,
519 struct ldb_message_element,
528 delete all elements matching an attribute name/value
530 return 0 on success, -1 on failure
532 static int msg_delete_element(struct ldb_module *module,
533 struct ldb_message *msg,
535 const struct ldb_val *val)
537 struct ldb_context *ldb = ldb_module_get_ctx(module);
540 struct ldb_message_element *el;
541 const struct ldb_schema_attribute *a;
543 found = find_element(msg, name);
548 el = &msg->elements[found];
550 a = ldb_schema_attribute_by_name(ldb, el->name);
552 for (i=0;i<el->num_values;i++) {
553 if (a->syntax->comparison_fn(ldb, ldb,
554 &el->values[i], val) == 0) {
555 if (i<el->num_values-1) {
556 memmove(&el->values[i], &el->values[i+1],
557 sizeof(el->values[i])*
558 (el->num_values-(i+1)));
561 if (el->num_values == 0) {
562 return msg_delete_attribute(module, ldb,
574 modify a record - internal interface
576 yuck - this is O(n^2). Luckily n is usually small so we probably
577 get away with it, but if we ever have really large attribute lists
578 then we'll need to look at this again
580 int ltdb_modify_internal(struct ldb_module *module,
581 const struct ldb_message *msg)
583 struct ldb_context *ldb = ldb_module_get_ctx(module);
584 void *data = ldb_module_get_private(module);
585 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
586 TDB_DATA tdb_key, tdb_data;
587 struct ldb_message *msg2;
591 tdb_key = ltdb_key(module, msg->dn);
593 return LDB_ERR_OTHER;
596 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
597 if (!tdb_data.dptr) {
598 talloc_free(tdb_key.dptr);
599 return ltdb_err_map(tdb_error(ltdb->tdb));
602 msg2 = talloc(tdb_key.dptr, struct ldb_message);
604 talloc_free(tdb_key.dptr);
605 return LDB_ERR_OTHER;
608 ret = ltdb_unpack_data(module, &tdb_data, msg2);
618 for (i=0;i<msg->num_elements;i++) {
619 struct ldb_message_element *el = &msg->elements[i];
620 struct ldb_message_element *el2;
621 struct ldb_val *vals;
623 const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
625 if (ldb_attr_cmp(el->name, "distinguishedName") == 0) {
626 ldb_asprintf_errstring(ldb, "it is not permitted to perform a modify on distinguishedName (use rename instead): %s",
627 ldb_dn_get_linearized(msg->dn));
628 return LDB_ERR_UNWILLING_TO_PERFORM;
631 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
632 case LDB_FLAG_MOD_ADD:
634 /* add this element to the message. fail if it
636 idx = find_element(msg2, el->name);
638 if (el->num_values == 0) {
639 ldb_asprintf_errstring(ldb, "attribute %s on %s speicified, but with 0 values (illigal)",
640 el->name, ldb_dn_get_linearized(msg->dn));
641 return LDB_ERR_CONSTRAINT_VIOLATION;
644 if (a && a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
645 if (el->num_values > 1) {
646 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s speicified more than once",
647 el->name, ldb_dn_get_linearized(msg->dn));
648 return LDB_ERR_CONSTRAINT_VIOLATION;
651 if (msg_add_element(ldb, msg2, el) != 0) {
658 /* If this is an add, then if it already
659 * exists in the object, then we violoate the
660 * single-value rule */
661 if (a && a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
662 return LDB_ERR_CONSTRAINT_VIOLATION;
665 el2 = &msg2->elements[idx];
667 /* An attribute with this name already exists,
668 * add all values if they don't already exist
669 * (check both the other elements to be added,
670 * and those already in the db). */
672 for (j=0;j<el->num_values;j++) {
673 if (ldb_msg_find_val(el2, &el->values[j])) {
674 ldb_asprintf_errstring(ldb, "%s: value #%d already exists", el->name, j);
675 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
678 if (ldb_msg_find_val(el, &el->values[j]) != &el->values[j]) {
679 ldb_asprintf_errstring(ldb, "%s: value #%d provided more than once", el->name, j);
680 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
685 vals = talloc_realloc(msg2->elements, el2->values, struct ldb_val,
686 el2->num_values + el->num_values);
693 for (j=0;j<el->num_values;j++) {
694 vals[el2->num_values + j] =
695 ldb_val_dup(vals, &el->values[j]);
699 el2->num_values += el->num_values;
703 case LDB_FLAG_MOD_REPLACE:
704 if (a && a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
705 if (el->num_values > 1) {
706 ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s speicified more than once",
707 el->name, ldb_dn_get_linearized(msg->dn));
708 return LDB_ERR_CONSTRAINT_VIOLATION;
711 /* replace all elements of this attribute name with the elements
712 listed. The attribute not existing is not an error */
713 msg_delete_attribute(module, ldb, msg2, el->name);
715 for (j=0;j<el->num_values;j++) {
716 if (ldb_msg_find_val(el, &el->values[j]) != &el->values[j]) {
717 ldb_asprintf_errstring(ldb, "%s: value #%d provided more than once", el->name, j);
718 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
723 /* add the replacement element, if not empty */
724 if (el->num_values != 0 &&
725 msg_add_element(ldb, msg2, el) != 0) {
731 case LDB_FLAG_MOD_DELETE:
733 dn = ldb_dn_get_linearized(msg->dn);
739 /* we could be being asked to delete all
740 values or just some values */
741 if (msg->elements[i].num_values == 0) {
742 if (msg_delete_attribute(module, ldb, msg2,
743 msg->elements[i].name) != 0) {
744 ldb_asprintf_errstring(ldb, "No such attribute: %s for delete on %s", msg->elements[i].name, dn);
745 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
750 for (j=0;j<msg->elements[i].num_values;j++) {
751 if (msg_delete_element(module,
753 msg->elements[i].name,
754 &msg->elements[i].values[j]) != 0) {
755 ldb_asprintf_errstring(ldb, "No matching attribute value when deleting attribute: %s on %s", msg->elements[i].name, dn);
756 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
759 ret = ltdb_index_del_value(module, dn, &msg->elements[i], j);
760 if (ret != LDB_SUCCESS) {
766 ldb_asprintf_errstring(ldb,
767 "Invalid ldb_modify flags on %s: 0x%x",
768 msg->elements[i].name,
769 msg->elements[i].flags & LDB_FLAG_MOD_MASK);
770 ret = LDB_ERR_PROTOCOL_ERROR;
775 /* we've made all the mods
776 * save the modified record back into the database */
777 ret = ltdb_store(module, msg2, TDB_MODIFY);
778 if (ret != LDB_SUCCESS) {
782 ret = ltdb_modified(module, msg->dn);
783 if (ret != LDB_SUCCESS) {
787 talloc_free(tdb_key.dptr);
792 talloc_free(tdb_key.dptr);
800 static int ltdb_modify(struct ltdb_context *ctx)
802 struct ldb_module *module = ctx->module;
803 struct ldb_request *req = ctx->req;
806 ldb_request_set_state(req, LDB_ASYNC_PENDING);
808 tret = ltdb_check_special_dn(module, req->op.mod.message);
809 if (tret != LDB_SUCCESS) {
813 if (ltdb_cache_load(module) != 0) {
814 return LDB_ERR_OPERATIONS_ERROR;
817 tret = ltdb_modify_internal(module, req->op.mod.message);
818 if (tret != LDB_SUCCESS) {
828 static int ltdb_rename(struct ltdb_context *ctx)
830 struct ldb_module *module = ctx->module;
831 struct ldb_request *req = ctx->req;
832 struct ldb_message *msg;
835 ldb_request_set_state(req, LDB_ASYNC_PENDING);
837 if (ltdb_cache_load(ctx->module) != 0) {
838 return LDB_ERR_OPERATIONS_ERROR;
841 msg = talloc(ctx, struct ldb_message);
843 return LDB_ERR_OPERATIONS_ERROR;
846 /* in case any attribute of the message was indexed, we need
847 to fetch the old record */
848 tret = ltdb_search_dn1(module, req->op.rename.olddn, msg);
849 if (tret != LDB_SUCCESS) {
850 /* not finding the old record is an error */
854 msg->dn = ldb_dn_copy(msg, req->op.rename.newdn);
856 return LDB_ERR_OPERATIONS_ERROR;
859 /* Always delete first then add, to avoid conflicts with
860 * unique indexes. We rely on the transaction to make this
863 tret = ltdb_delete_internal(module, req->op.rename.olddn);
864 if (tret != LDB_SUCCESS) {
868 tret = ltdb_add_internal(module, msg);
869 if (tret != LDB_SUCCESS) {
876 static int ltdb_start_trans(struct ldb_module *module)
878 void *data = ldb_module_get_private(module);
879 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
881 if (tdb_transaction_start(ltdb->tdb) != 0) {
882 return ltdb_err_map(tdb_error(ltdb->tdb));
885 ltdb->in_transaction++;
887 ltdb_index_transaction_start(module);
892 static int ltdb_prepare_commit(struct ldb_module *module)
894 void *data = ldb_module_get_private(module);
895 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
897 if (ltdb->in_transaction != 1) {
901 if (ltdb_index_transaction_commit(module) != 0) {
902 tdb_transaction_cancel(ltdb->tdb);
903 ltdb->in_transaction--;
904 return ltdb_err_map(tdb_error(ltdb->tdb));
907 if (tdb_transaction_prepare_commit(ltdb->tdb) != 0) {
908 ltdb->in_transaction--;
909 return ltdb_err_map(tdb_error(ltdb->tdb));
912 ltdb->prepared_commit = true;
917 static int ltdb_end_trans(struct ldb_module *module)
919 void *data = ldb_module_get_private(module);
920 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
922 if (!ltdb->prepared_commit) {
923 int ret = ltdb_prepare_commit(module);
924 if (ret != LDB_SUCCESS) {
929 ltdb->in_transaction--;
930 ltdb->prepared_commit = false;
932 if (tdb_transaction_commit(ltdb->tdb) != 0) {
933 return ltdb_err_map(tdb_error(ltdb->tdb));
939 static int ltdb_del_trans(struct ldb_module *module)
941 void *data = ldb_module_get_private(module);
942 struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
944 ltdb->in_transaction--;
946 if (ltdb_index_transaction_cancel(module) != 0) {
947 tdb_transaction_cancel(ltdb->tdb);
948 return ltdb_err_map(tdb_error(ltdb->tdb));
951 if (tdb_transaction_cancel(ltdb->tdb) != 0) {
952 return ltdb_err_map(tdb_error(ltdb->tdb));
959 return sequenceNumber from @BASEINFO
961 static int ltdb_sequence_number(struct ltdb_context *ctx,
962 struct ldb_extended **ext)
964 struct ldb_context *ldb;
965 struct ldb_module *module = ctx->module;
966 struct ldb_request *req = ctx->req;
968 struct ldb_seqnum_request *seq;
969 struct ldb_seqnum_result *res;
970 struct ldb_message *msg = NULL;
975 ldb = ldb_module_get_ctx(module);
977 seq = talloc_get_type(req->op.extended.data,
978 struct ldb_seqnum_request);
980 return LDB_ERR_OPERATIONS_ERROR;
983 ldb_request_set_state(req, LDB_ASYNC_PENDING);
985 if (ltdb_lock_read(module) != 0) {
986 return LDB_ERR_OPERATIONS_ERROR;
989 res = talloc_zero(req, struct ldb_seqnum_result);
991 ret = LDB_ERR_OPERATIONS_ERROR;
994 tmp_ctx = talloc_new(req);
995 if (tmp_ctx == NULL) {
996 ret = LDB_ERR_OPERATIONS_ERROR;
1000 dn = ldb_dn_new(tmp_ctx, ldb, LTDB_BASEINFO);
1002 msg = talloc(tmp_ctx, struct ldb_message);
1004 ret = LDB_ERR_OPERATIONS_ERROR;
1008 ret = ltdb_search_dn1(module, dn, msg);
1009 if (ret != LDB_SUCCESS) {
1013 switch (seq->type) {
1014 case LDB_SEQ_HIGHEST_SEQ:
1015 res->seq_num = ldb_msg_find_attr_as_uint64(msg, LTDB_SEQUENCE_NUMBER, 0);
1018 res->seq_num = ldb_msg_find_attr_as_uint64(msg, LTDB_SEQUENCE_NUMBER, 0);
1021 case LDB_SEQ_HIGHEST_TIMESTAMP:
1022 date = ldb_msg_find_attr_as_string(msg, LTDB_MOD_TIMESTAMP, NULL);
1024 res->seq_num = ldb_string_to_time(date);
1027 /* zero is as good as anything when we don't know */
1032 *ext = talloc_zero(req, struct ldb_extended);
1034 ret = LDB_ERR_OPERATIONS_ERROR;
1037 (*ext)->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
1038 (*ext)->data = talloc_steal(*ext, res);
1043 talloc_free(tmp_ctx);
1044 ltdb_unlock_read(module);
1048 static void ltdb_request_done(struct ltdb_context *ctx, int error)
1050 struct ldb_context *ldb;
1051 struct ldb_request *req;
1052 struct ldb_reply *ares;
1054 ldb = ldb_module_get_ctx(ctx->module);
1057 /* if we already returned an error just return */
1058 if (ldb_request_get_status(req) != LDB_SUCCESS) {
1062 ares = talloc_zero(req, struct ldb_reply);
1065 req->callback(req, NULL);
1068 ares->type = LDB_REPLY_DONE;
1069 ares->error = error;
1071 req->callback(req, ares);
1074 static void ltdb_timeout(struct tevent_context *ev,
1075 struct tevent_timer *te,
1079 struct ltdb_context *ctx;
1080 ctx = talloc_get_type(private_data, struct ltdb_context);
1082 if (!ctx->request_terminated) {
1083 /* request is done now */
1084 ltdb_request_done(ctx, LDB_ERR_TIME_LIMIT_EXCEEDED);
1087 if (!ctx->request_terminated) {
1088 /* neutralize the spy */
1089 ctx->spy->ctx = NULL;
1094 static void ltdb_request_extended_done(struct ltdb_context *ctx,
1095 struct ldb_extended *ext,
1098 struct ldb_context *ldb;
1099 struct ldb_request *req;
1100 struct ldb_reply *ares;
1102 ldb = ldb_module_get_ctx(ctx->module);
1105 /* if we already returned an error just return */
1106 if (ldb_request_get_status(req) != LDB_SUCCESS) {
1110 ares = talloc_zero(req, struct ldb_reply);
1113 req->callback(req, NULL);
1116 ares->type = LDB_REPLY_DONE;
1117 ares->response = ext;
1118 ares->error = error;
1120 req->callback(req, ares);
1123 static void ltdb_handle_extended(struct ltdb_context *ctx)
1125 struct ldb_extended *ext = NULL;
1128 if (strcmp(ctx->req->op.extended.oid,
1129 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1130 /* get sequence number */
1131 ret = ltdb_sequence_number(ctx, &ext);
1133 /* not recognized */
1134 ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1137 ltdb_request_extended_done(ctx, ext, ret);
1140 static void ltdb_callback(struct tevent_context *ev,
1141 struct tevent_timer *te,
1145 struct ltdb_context *ctx;
1148 ctx = talloc_get_type(private_data, struct ltdb_context);
1150 if (ctx->request_terminated) {
1154 switch (ctx->req->operation) {
1156 ret = ltdb_search(ctx);
1159 ret = ltdb_add(ctx);
1162 ret = ltdb_modify(ctx);
1165 ret = ltdb_delete(ctx);
1168 ret = ltdb_rename(ctx);
1171 ltdb_handle_extended(ctx);
1174 /* no other op supported */
1175 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1178 if (!ctx->request_terminated) {
1179 /* request is done now */
1180 ltdb_request_done(ctx, ret);
1184 if (!ctx->request_terminated) {
1185 /* neutralize the spy */
1186 ctx->spy->ctx = NULL;
1191 static int ltdb_request_destructor(void *ptr)
1193 struct ltdb_req_spy *spy = talloc_get_type(ptr, struct ltdb_req_spy);
1195 if (spy->ctx != NULL) {
1196 spy->ctx->request_terminated = true;
1202 static int ltdb_handle_request(struct ldb_module *module,
1203 struct ldb_request *req)
1205 struct ldb_context *ldb;
1206 struct tevent_context *ev;
1207 struct ltdb_context *ac;
1208 struct tevent_timer *te;
1211 if (check_critical_controls(req->controls)) {
1212 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1215 ldb = ldb_module_get_ctx(module);
1217 if (req->starttime == 0 || req->timeout == 0) {
1218 ldb_set_errstring(ldb, "Invalid timeout settings");
1219 return LDB_ERR_TIME_LIMIT_EXCEEDED;
1222 ev = ldb_get_event_context(ldb);
1224 ac = talloc_zero(ldb, struct ltdb_context);
1226 ldb_set_errstring(ldb, "Out of Memory");
1227 return LDB_ERR_OPERATIONS_ERROR;
1230 ac->module = module;
1235 te = tevent_add_timer(ev, ac, tv, ltdb_callback, ac);
1238 return LDB_ERR_OPERATIONS_ERROR;
1241 tv.tv_sec = req->starttime + req->timeout;
1242 ac->timeout_event = tevent_add_timer(ev, ac, tv, ltdb_timeout, ac);
1243 if (NULL == ac->timeout_event) {
1245 return LDB_ERR_OPERATIONS_ERROR;
1248 /* set a spy so that we do not try to use the request context
1249 * if it is freed before ltdb_callback fires */
1250 ac->spy = talloc(req, struct ltdb_req_spy);
1251 if (NULL == ac->spy) {
1253 return LDB_ERR_OPERATIONS_ERROR;
1257 talloc_set_destructor((TALLOC_CTX *)ac->spy, ltdb_request_destructor);
1262 static const struct ldb_module_ops ltdb_ops = {
1264 .search = ltdb_handle_request,
1265 .add = ltdb_handle_request,
1266 .modify = ltdb_handle_request,
1267 .del = ltdb_handle_request,
1268 .rename = ltdb_handle_request,
1269 .extended = ltdb_handle_request,
1270 .start_transaction = ltdb_start_trans,
1271 .end_transaction = ltdb_end_trans,
1272 .prepare_commit = ltdb_prepare_commit,
1273 .del_transaction = ltdb_del_trans,
1277 connect to the database
1279 static int ltdb_connect(struct ldb_context *ldb, const char *url,
1280 unsigned int flags, const char *options[],
1281 struct ldb_module **_module)
1283 struct ldb_module *module;
1285 int tdb_flags, open_flags;
1286 struct ltdb_private *ltdb;
1289 if (strchr(url, ':')) {
1290 if (strncmp(url, "tdb://", 6) != 0) {
1291 ldb_debug(ldb, LDB_DEBUG_ERROR,
1292 "Invalid tdb URL '%s'", url);
1300 tdb_flags = TDB_DEFAULT | TDB_SEQNUM;
1302 /* check for the 'nosync' option */
1303 if (flags & LDB_FLG_NOSYNC) {
1304 tdb_flags |= TDB_NOSYNC;
1307 /* and nommap option */
1308 if (flags & LDB_FLG_NOMMAP) {
1309 tdb_flags |= TDB_NOMMAP;
1312 if (flags & LDB_FLG_RDONLY) {
1313 open_flags = O_RDONLY;
1315 open_flags = O_CREAT | O_RDWR;
1318 ltdb = talloc_zero(ldb, struct ltdb_private);
1324 /* note that we use quite a large default hash size */
1325 ltdb->tdb = ltdb_wrap_open(ltdb, path, 10000,
1326 tdb_flags, open_flags,
1327 ldb_get_create_perms(ldb), ldb);
1329 ldb_debug(ldb, LDB_DEBUG_ERROR,
1330 "Unable to open tdb '%s'", path);
1335 ltdb->sequence_number = 0;
1337 module = ldb_module_new(ldb, ldb, "ldb_tdb backend", <db_ops);
1342 ldb_module_set_private(module, ltdb);
1344 if (ltdb_cache_load(module) != 0) {
1345 talloc_free(module);
1354 const struct ldb_backend_ops ldb_tdb_backend_ops = {
1356 .connect_fn = ltdb_connect