4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Stefan Metzmacher 2004
8 ** NOTE! The following LGPL license applies to the ldb
9 ** library. This does NOT imply that all of Samba is released
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 2 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * Component: ldb tdb backend
32 * Description: core functions for tdb backend
34 * Author: Andrew Tridgell
35 * Author: Stefan Metzmacher
39 #include "ldb/include/includes.h"
41 #include "ldb/ldb_tdb/ldb_tdb.h"
45 map a tdb error code to a ldb error code
47 static int ltdb_err_map(enum TDB_ERROR tdb_code)
55 return LDB_ERR_OPERATIONS_ERROR;
57 return LDB_ERR_PROTOCOL_ERROR;
61 case TDB_ERR_LOCK_TIMEOUT:
62 return LDB_ERR_TIME_LIMIT_EXCEEDED;
64 return LDB_ERR_ENTRY_ALREADY_EXISTS;
66 return LDB_ERR_NO_SUCH_OBJECT;
68 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
75 form a TDB_DATA for a record key
78 note that the key for a record can depend on whether the
79 dn refers to a case sensitive index record or not
81 struct TDB_DATA ltdb_key(struct ldb_module *module, const struct ldb_dn *dn)
83 struct ldb_context *ldb = module->ldb;
86 char *dn_folded = NULL;
89 most DNs are case insensitive. The exception is index DNs for
90 case sensitive attributes
92 there are 3 cases dealt with in this code:
94 1) if the dn doesn't start with @ then uppercase the attribute
95 names and the attributes values of case insensitive attributes
96 2) if the dn starts with @ then leave it alone - the indexing code handles
100 dn_folded = ldb_dn_linearize_casefold(ldb, dn);
105 key_str = talloc_asprintf(ldb, "DN=%s", dn_folded);
107 talloc_free(dn_folded);
113 key.dptr = (uint8_t *)key_str;
114 key.dsize = strlen(key_str) + 1;
126 check special dn's have valid attributes
127 currently only @ATTRIBUTES is checked
129 int ltdb_check_special_dn(struct ldb_module *module, const struct ldb_message *msg)
133 if (! ldb_dn_is_special(msg->dn) ||
134 ! ldb_dn_check_special(msg->dn, LTDB_ATTRIBUTES)) {
138 /* we have @ATTRIBUTES, let's check attributes are fine */
139 /* should we check that we deny multivalued attributes ? */
140 for (i = 0; i < msg->num_elements; i++) {
141 for (j = 0; j < msg->elements[i].num_values; j++) {
142 if (ltdb_check_at_attributes_values(&msg->elements[i].values[j]) != 0) {
143 char *err_string = talloc_strdup(module, "Invalid attribute value in an @ATTRIBUTES entry");
145 ldb_set_errstring(module->ldb, err_string);
147 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
157 we've made a modification to a dn - possibly reindex and
158 update sequence number
160 static int ltdb_modified(struct ldb_module *module, const struct ldb_dn *dn)
164 if (ldb_dn_is_special(dn) &&
165 (ldb_dn_check_special(dn, LTDB_INDEXLIST) ||
166 ldb_dn_check_special(dn, LTDB_ATTRIBUTES)) ) {
167 ret = ltdb_reindex(module);
171 !(ldb_dn_is_special(dn) &&
172 ldb_dn_check_special(dn, LTDB_BASEINFO)) ) {
173 ret = ltdb_increase_sequence_number(module);
180 store a record into the db
182 int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs)
184 struct ltdb_private *ltdb = module->private_data;
185 TDB_DATA tdb_key, tdb_data;
188 tdb_key = ltdb_key(module, msg->dn);
190 return LDB_ERR_OTHER;
193 ret = ltdb_pack_data(module, msg, &tdb_data);
195 talloc_free(tdb_key.dptr);
196 return LDB_ERR_OTHER;
199 ret = tdb_store(ltdb->tdb, tdb_key, tdb_data, flgs);
201 ret = ltdb_err_map(tdb_error(ltdb->tdb));
205 ret = ltdb_index_add(module, msg);
207 tdb_delete(ltdb->tdb, tdb_key);
211 talloc_free(tdb_key.dptr);
212 talloc_free(tdb_data.dptr);
219 add a record to the database
221 static int ltdb_add(struct ldb_module *module, const struct ldb_message *msg)
225 ret = ltdb_check_special_dn(module, msg);
226 if (ret != LDB_SUCCESS) {
230 if (ltdb_cache_load(module) != 0) {
231 return LDB_ERR_OTHER;
234 ret = ltdb_store(module, msg, TDB_INSERT);
236 if (ret == LDB_SUCCESS) {
237 ltdb_modified(module, msg->dn);
245 delete a record from the database, not updating indexes (used for deleting
248 int ltdb_delete_noindex(struct ldb_module *module, const struct ldb_dn *dn)
250 struct ltdb_private *ltdb = module->private_data;
254 tdb_key = ltdb_key(module, dn);
256 return LDB_ERR_OTHER;
259 ret = tdb_delete(ltdb->tdb, tdb_key);
260 talloc_free(tdb_key.dptr);
263 ret = ltdb_err_map(tdb_error(ltdb->tdb));
270 delete a record from the database
272 static int ltdb_delete(struct ldb_module *module, const struct ldb_dn *dn)
274 struct ldb_message *msg = NULL;
275 int ret = LDB_ERR_OTHER;
277 if (ltdb_cache_load(module) != 0) {
281 msg = talloc(module, struct ldb_message);
286 /* in case any attribute of the message was indexed, we need
287 to fetch the old record */
288 ret = ltdb_search_dn1(module, dn, msg);
290 /* not finding the old record is an error */
291 ret = LDB_ERR_NO_SUCH_OBJECT;
295 ret = ltdb_delete_noindex(module, dn);
296 if (ret != LDB_SUCCESS) {
300 /* remove any indexed attributes */
301 ret = ltdb_index_del(module, msg);
302 if (ret == LDB_SUCCESS) {
303 ltdb_modified(module, dn);
317 find an element by attribute name. At the moment this does a linear search, it should
318 be re-coded to use a binary search once all places that modify records guarantee
321 return the index of the first matching element if found, otherwise -1
323 static int find_element(const struct ldb_message *msg, const char *name)
326 for (i=0;i<msg->num_elements;i++) {
327 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
336 add an element to an existing record. Assumes a elements array that we
337 can call re-alloc on, and assumed that we can re-use the data pointers from the
338 passed in additional values. Use with care!
340 returns 0 on success, -1 on failure (and sets errno)
342 static int msg_add_element(struct ldb_context *ldb,
343 struct ldb_message *msg, struct ldb_message_element *el)
345 struct ldb_message_element *e2;
348 e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
349 msg->num_elements+1);
357 e2 = &msg->elements[msg->num_elements];
360 e2->flags = el->flags;
362 if (el->num_values != 0) {
363 e2->values = talloc_array(msg->elements, struct ldb_val, el->num_values);
369 for (i=0;i<el->num_values;i++) {
370 e2->values[i] = el->values[i];
372 e2->num_values = el->num_values;
380 delete all elements having a specified attribute name
382 static int msg_delete_attribute(struct ldb_module *module,
383 struct ldb_context *ldb,
384 struct ldb_message *msg, const char *name)
389 dn = ldb_dn_linearize(ldb, msg->dn);
394 for (i=0;i<msg->num_elements;i++) {
395 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
396 for (j=0;j<msg->elements[i].num_values;j++) {
397 ltdb_index_del_value(module, dn, &msg->elements[i], j);
399 talloc_free(msg->elements[i].values);
400 if (msg->num_elements > (i+1)) {
401 memmove(&msg->elements[i],
403 sizeof(struct ldb_message_element)*
404 (msg->num_elements - (i+1)));
408 msg->elements = talloc_realloc(msg, msg->elements,
409 struct ldb_message_element,
419 delete all elements matching an attribute name/value
421 return 0 on success, -1 on failure
423 static int msg_delete_element(struct ldb_module *module,
424 struct ldb_message *msg,
426 const struct ldb_val *val)
428 struct ldb_context *ldb = module->ldb;
431 struct ldb_message_element *el;
432 const struct ldb_attrib_handler *h;
434 found = find_element(msg, name);
439 el = &msg->elements[found];
441 h = ldb_attrib_handler(ldb, el->name);
443 for (i=0;i<el->num_values;i++) {
444 if (h->comparison_fn(ldb, ldb, &el->values[i], val) == 0) {
445 if (i<el->num_values-1) {
446 memmove(&el->values[i], &el->values[i+1],
447 sizeof(el->values[i])*(el->num_values-(i+1)));
450 if (el->num_values == 0) {
451 return msg_delete_attribute(module, ldb, msg, name);
462 modify a record - internal interface
464 yuck - this is O(n^2). Luckily n is usually small so we probably
465 get away with it, but if we ever have really large attribute lists
466 then we'll need to look at this again
468 int ltdb_modify_internal(struct ldb_module *module, const struct ldb_message *msg)
470 struct ldb_context *ldb = module->ldb;
471 struct ltdb_private *ltdb = module->private_data;
472 TDB_DATA tdb_key, tdb_data;
473 struct ldb_message *msg2;
477 tdb_key = ltdb_key(module, msg->dn);
479 return LDB_ERR_OTHER;
482 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
483 if (!tdb_data.dptr) {
484 talloc_free(tdb_key.dptr);
485 return ltdb_err_map(tdb_error(ltdb->tdb));
488 msg2 = talloc(tdb_key.dptr, struct ldb_message);
490 talloc_free(tdb_key.dptr);
491 return LDB_ERR_OTHER;
494 ret = ltdb_unpack_data(module, &tdb_data, msg2);
496 talloc_free(tdb_key.dptr);
498 return LDB_ERR_OTHER;
505 for (i=0;i<msg->num_elements;i++) {
506 struct ldb_message_element *el = &msg->elements[i];
507 struct ldb_message_element *el2;
508 struct ldb_val *vals;
512 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
514 case LDB_FLAG_MOD_ADD:
515 /* add this element to the message. fail if it
517 ret = find_element(msg2, el->name);
520 if (msg_add_element(ldb, msg2, el) != 0) {
527 el2 = &msg2->elements[ret];
529 /* An attribute with this name already exists, add all
530 * values if they don't already exist. */
532 for (j=0;j<el->num_values;j++) {
533 if (ldb_msg_find_val(el2, &el->values[j])) {
534 err_string = talloc_strdup(module, "Type or value exists");
535 if (err_string) ldb_set_errstring(module->ldb, err_string);
536 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
541 vals = talloc_realloc(msg2->elements, el2->values, struct ldb_val,
542 el2->num_values + el->num_values);
547 for (j=0;j<el->num_values;j++) {
548 vals[el2->num_values + j] =
549 ldb_val_dup(vals, &el->values[j]);
553 el2->num_values += el->num_values;
557 case LDB_FLAG_MOD_REPLACE:
558 /* replace all elements of this attribute name with the elements
559 listed. The attribute not existing is not an error */
560 msg_delete_attribute(module, ldb, msg2, msg->elements[i].name);
562 /* add the replacement element, if not empty */
563 if (msg->elements[i].num_values != 0 &&
564 msg_add_element(ldb, msg2, &msg->elements[i]) != 0) {
569 case LDB_FLAG_MOD_DELETE:
571 dn = ldb_dn_linearize(msg2, msg->dn);
572 if (dn == NULL) goto failed;
574 /* we could be being asked to delete all
575 values or just some values */
576 if (msg->elements[i].num_values == 0) {
577 if (msg_delete_attribute(module, ldb, msg2,
578 msg->elements[i].name) != 0) {
579 err_string = talloc_asprintf(module, "No such attribute: %s", msg->elements[i].name);
580 if (err_string) ldb_set_errstring(module->ldb, err_string);
581 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
586 for (j=0;j<msg->elements[i].num_values;j++) {
587 if (msg_delete_element(module,
589 msg->elements[i].name,
590 &msg->elements[i].values[j]) != 0) {
591 err_string = talloc_asprintf(module, "No such attribute: %s", msg->elements[i].name);
592 if (err_string) ldb_set_errstring(module->ldb, err_string);
593 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
596 if (ltdb_index_del_value(module, dn, &msg->elements[i], j) != 0) {
602 err_string = talloc_asprintf(module, "Invalid ldb_modify flags on %s: 0x%x",
603 msg->elements[i].name,
604 msg->elements[i].flags & LDB_FLAG_MOD_MASK);
605 if (err_string) ldb_set_errstring(module->ldb, err_string);
606 ret = LDB_ERR_PROTOCOL_ERROR;
611 /* we've made all the mods - save the modified record back into the database */
612 ret = ltdb_store(module, msg2, TDB_MODIFY);
614 talloc_free(tdb_key.dptr);
619 talloc_free(tdb_key.dptr);
627 static int ltdb_modify(struct ldb_module *module, const struct ldb_message *msg)
631 ret = ltdb_check_special_dn(module, msg);
636 if (ltdb_cache_load(module) != 0) {
640 ret = ltdb_modify_internal(module, msg);
642 if (ret == LDB_SUCCESS) {
643 ltdb_modified(module, msg->dn);
652 static int ltdb_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
654 struct ldb_message *msg;
656 int ret = LDB_ERR_OTHER;
658 if (ltdb_cache_load(module) != 0) {
662 msg = talloc(module, struct ldb_message);
667 /* in case any attribute of the message was indexed, we need
668 to fetch the old record */
669 ret = ltdb_search_dn1(module, olddn, msg);
671 /* not finding the old record is an error */
672 ret = LDB_ERR_NO_SUCH_OBJECT;
676 msg->dn = ldb_dn_copy(msg, newdn);
682 ret = ltdb_add(module, msg);
683 if (ret != LDB_SUCCESS) {
687 ret = ltdb_delete(module, olddn);
688 error_str = talloc_strdup(module, ldb_errstring(module->ldb));
689 if (ret != LDB_SUCCESS) {
690 ltdb_delete(module, newdn);
693 ldb_set_errstring(module->ldb, error_str);
704 static int ltdb_start_trans(struct ldb_module *module)
706 struct ltdb_private *ltdb = module->private_data;
708 if (tdb_transaction_start(ltdb->tdb) != 0) {
709 return ltdb_err_map(tdb_error(ltdb->tdb));
715 static int ltdb_end_trans(struct ldb_module *module)
717 struct ltdb_private *ltdb = module->private_data;
719 if (tdb_transaction_commit(ltdb->tdb) != 0) {
720 return ltdb_err_map(tdb_error(ltdb->tdb));
726 static int ltdb_del_trans(struct ldb_module *module)
728 struct ltdb_private *ltdb = module->private_data;
730 if (tdb_transaction_cancel(ltdb->tdb) != 0) {
731 return ltdb_err_map(tdb_error(ltdb->tdb));
737 static int ltdb_request(struct ldb_module *module, struct ldb_request *req)
739 /* check for oustanding critical controls and return an error if found */
741 if (check_critical_controls(req->controls)) {
742 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
745 switch (req->operation) {
748 return ltdb_search_bytree(module,
750 req->op.search.scope,
752 req->op.search.attrs,
753 &req->op.search.res);
756 return ltdb_add(module, req->op.add.message);
759 return ltdb_modify(module, req->op.mod.message);
762 return ltdb_delete(module, req->op.del.dn);
765 return ltdb_rename(module,
766 req->op.rename.olddn,
767 req->op.rename.newdn);
770 return LDB_ERR_OPERATIONS_ERROR;
776 return sequenceNumber from @BASEINFO
778 static uint64_t ltdb_sequence_number(struct ldb_context *ldb)
780 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
781 const char *attrs[] = { "sequenceNumber", NULL };
782 struct ldb_result *res = NULL;
783 struct ldb_dn *dn = ldb_dn_explode(tmp_ctx, "@BASEINFO");
787 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, NULL, attrs, &res);
788 talloc_steal(tmp_ctx, res);
789 if (ret != LDB_SUCCESS || res->count != 1) {
790 talloc_free(tmp_ctx);
791 /* zero is as good as anything when we don't know */
795 seq_num = ldb_msg_find_uint64(res->msgs[0], "sequenceNumber", 0);
796 talloc_free(tmp_ctx);
800 static int ltdb_init_2(struct ldb_module *module)
805 static const struct ldb_module_ops ltdb_ops = {
807 .request = ltdb_request,
808 .start_transaction = ltdb_start_trans,
809 .end_transaction = ltdb_end_trans,
810 .del_transaction = ltdb_del_trans,
811 .second_stage_init = ltdb_init_2
816 connect to the database
818 int ltdb_connect(struct ldb_context *ldb, const char *url,
819 unsigned int flags, const char *options[])
822 int tdb_flags, open_flags;
823 struct ltdb_private *ltdb;
826 if (strchr(url, ':')) {
827 if (strncmp(url, "tdb://", 6) != 0) {
828 ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid tdb URL '%s'", url);
836 tdb_flags = TDB_DEFAULT;
838 /* check for the 'nosync' option */
839 if (flags & LDB_FLG_NOSYNC) {
840 tdb_flags |= TDB_NOSYNC;
843 if (flags & LDB_FLG_RDONLY) {
844 open_flags = O_RDONLY;
846 open_flags = O_CREAT | O_RDWR;
849 ltdb = talloc_zero(ldb, struct ltdb_private);
855 /* note that we use quite a large default hash size */
856 ltdb->tdb = ltdb_wrap_open(ltdb, path, 10000, tdb_flags, open_flags, 0666);
858 ldb_debug(ldb, LDB_DEBUG_ERROR, "Unable to open tdb '%s'\n", path);
863 ltdb->sequence_number = 0;
865 ldb->modules = talloc(ldb, struct ldb_module);
871 ldb->modules->ldb = ldb;
872 ldb->modules->prev = ldb->modules->next = NULL;
873 ldb->modules->private_data = ltdb;
874 ldb->modules->ops = <db_ops;
875 ldb->sequence_number = ltdb_sequence_number;