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/ldb.h"
40 #include "ldb/include/ldb_private.h"
41 #include "ldb/include/ldb_dn.h"
42 #include "ldb/ldb_tdb/ldb_tdb.h"
44 #define LDBLOCK "INT_LDBLOCK"
48 callback function used in call to ldb_dn_fold() for determining whether an
49 attribute type requires case folding.
51 static int ltdb_case_fold_attr_required(void * user_data, char *attr)
53 struct ldb_module *module = talloc_get_type(user_data, struct ldb_module);
55 return ltdb_attribute_flags(module, attr) & LTDB_FLAG_CASE_INSENSITIVE;
59 form a TDB_DATA for a record key
62 note that the key for a record can depend on whether the
63 dn refers to a case sensitive index record or not
65 struct TDB_DATA ltdb_key(struct ldb_module *module, const char *dn)
67 struct ldb_context *ldb = module->ldb;
70 char *dn_folded = NULL;
71 const char *prefix = LTDB_INDEX ":";
76 most DNs are case insensitive. The exception is index DNs for
77 case sensitive attributes
79 there are 3 cases dealt with in this code:
81 1) if the dn doesn't start with @INDEX: then uppercase the attribute
82 names and the attributes values of case insensitive attributes
83 2) if the dn starts with @INDEX:attr and 'attr' is a case insensitive
84 attribute then uppercase whole dn
85 3) if the dn starts with @INDEX:attr and 'attr' is a case sensitive
86 attribute then uppercase up to the value of the attribute, but
89 if (strncmp(dn, prefix, strlen(prefix)) == 0 &&
90 (s = strchr(dn+strlen(prefix), ':'))) {
91 char *attr_name, *attr_name_folded;
92 attr_name = talloc_strndup(ldb, dn+strlen(prefix), (s-(dn+strlen(prefix))));
96 flags = ltdb_attribute_flags(module, attr_name);
98 if (flags & LTDB_FLAG_CASE_INSENSITIVE) {
99 dn_folded = ldb_casefold(ldb, dn);
101 attr_name_folded = ldb_casefold(ldb, attr_name);
102 if (!attr_name_folded) {
105 dn_folded = talloc_asprintf(ldb, "%s:%s:%s",
106 prefix, attr_name_folded,
108 talloc_free(attr_name_folded);
110 talloc_free(attr_name);
112 /* special cases for tdb */
113 else if (*dn == '@' || strncmp(LDBLOCK, dn, strlen(LDBLOCK)) == 0) {
115 dn_folded = talloc_strdup(ldb, dn);
118 struct ldb_dn *edn, *cedn;
120 edn = ldb_dn_explode(ldb, dn);
124 cedn = ldb_dn_casefold(ldb, edn, module,
125 ltdb_case_fold_attr_required);
129 dn_folded = ldb_dn_linearize(ldb, cedn);
141 key_str = talloc_asprintf(ldb, "DN=%s", dn_folded);
142 talloc_free(dn_folded);
149 key.dsize = strlen(key_str)+1;
161 lock the database for write - currently a single lock is used
163 static int ltdb_lock(struct ldb_module *module, const char *lockname)
165 struct ltdb_private *ltdb = module->private_data;
170 if (lockname == NULL) {
174 lock_dn = talloc_asprintf(module->ldb, "%s_%s", LDBLOCK, lockname);
175 if (lock_dn == NULL) {
179 key = ltdb_key(module, lock_dn);
181 talloc_free(lock_dn);
185 ret = tdb_chainlock(ltdb->tdb, key);
187 talloc_free(key.dptr);
188 talloc_free(lock_dn);
194 unlock the database after a ltdb_lock()
196 static int ltdb_unlock(struct ldb_module *module, const char *lockname)
198 struct ltdb_private *ltdb = module->private_data;
202 if (lockname == NULL) {
206 lock_dn = talloc_asprintf(module->ldb, "%s_%s", LDBLOCK, lockname);
207 if (lock_dn == NULL) {
211 key = ltdb_key(module, lock_dn);
213 talloc_free(lock_dn);
217 tdb_chainunlock(ltdb->tdb, key);
219 talloc_free(key.dptr);
220 talloc_free(lock_dn);
227 lock the database for read - use by ltdb_search
229 int ltdb_lock_read(struct ldb_module *module)
231 struct ltdb_private *ltdb = module->private_data;
234 key = ltdb_key(module, LDBLOCK);
238 ret = tdb_chainlock_read(ltdb->tdb, key);
239 talloc_free(key.dptr);
244 unlock the database after a ltdb_lock_read()
246 int ltdb_unlock_read(struct ldb_module *module)
248 struct ltdb_private *ltdb = module->private_data;
250 key = ltdb_key(module, LDBLOCK);
254 tdb_chainunlock_read(ltdb->tdb, key);
255 talloc_free(key.dptr);
260 check special dn's have valid attributes
261 currently only @ATTRIBUTES is checked
263 int ltdb_check_special_dn(struct ldb_module *module, const struct ldb_message *msg)
265 struct ltdb_private *ltdb = module->private_data;
268 if (strcmp(msg->dn, LTDB_ATTRIBUTES) != 0) {
272 /* we have @ATTRIBUTES, let's check attributes are fine */
273 /* should we check that we deny multivalued attributes ? */
274 for (i = 0; i < msg->num_elements; i++) {
275 for (j = 0; j < msg->elements[i].num_values; j++) {
276 if (ltdb_check_at_attributes_values(&msg->elements[i].values[j]) != 0) {
277 ltdb->last_err_string = "Invalid attribute value in an @ATTRIBUTES entry";
288 we've made a modification to a dn - possibly reindex and
289 update sequence number
291 static int ltdb_modified(struct ldb_module *module, const char *dn)
295 if (strcmp(dn, LTDB_INDEXLIST) == 0 ||
296 strcmp(dn, LTDB_ATTRIBUTES) == 0) {
297 ret = ltdb_reindex(module);
301 strcmp(dn, LTDB_BASEINFO) != 0) {
302 ret = ltdb_increase_sequence_number(module);
309 store a record into the db
311 int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs)
313 struct ltdb_private *ltdb = module->private_data;
314 TDB_DATA tdb_key, tdb_data;
317 tdb_key = ltdb_key(module, msg->dn);
322 ret = ltdb_pack_data(module, msg, &tdb_data);
324 talloc_free(tdb_key.dptr);
328 ret = tdb_store(ltdb->tdb, tdb_key, tdb_data, flgs);
333 ret = ltdb_index_add(module, msg);
335 tdb_delete(ltdb->tdb, tdb_key);
339 talloc_free(tdb_key.dptr);
340 talloc_free(tdb_data.dptr);
347 add a record to the database
349 static int ltdb_add(struct ldb_module *module, const struct ldb_message *msg)
351 struct ltdb_private *ltdb = module->private_data;
354 ltdb->last_err_string = NULL;
356 ret = ltdb_check_special_dn(module, msg);
361 if (ltdb_lock(module, LDBLOCK) != 0) {
365 if (ltdb_cache_load(module) != 0) {
366 ltdb_unlock(module, LDBLOCK);
370 ret = ltdb_store(module, msg, TDB_INSERT);
373 ltdb_modified(module, msg->dn);
376 ltdb_unlock(module, LDBLOCK);
382 delete a record from the database, not updating indexes (used for deleting
385 int ltdb_delete_noindex(struct ldb_module *module, const char *dn)
387 struct ltdb_private *ltdb = module->private_data;
391 tdb_key = ltdb_key(module, dn);
396 ret = tdb_delete(ltdb->tdb, tdb_key);
397 talloc_free(tdb_key.dptr);
403 delete a record from the database
405 static int ltdb_delete(struct ldb_module *module, const char *dn)
407 struct ltdb_private *ltdb = module->private_data;
409 struct ldb_message *msg = NULL;
411 ltdb->last_err_string = NULL;
413 if (ltdb_lock(module, LDBLOCK) != 0) {
417 if (ltdb_cache_load(module) != 0) {
421 msg = talloc(module, struct ldb_message);
426 /* in case any attribute of the message was indexed, we need
427 to fetch the old record */
428 ret = ltdb_search_dn1(module, dn, msg);
430 /* not finding the old record is an error */
434 ret = ltdb_delete_noindex(module, dn);
439 /* remove any indexed attributes */
440 ret = ltdb_index_del(module, msg);
443 ltdb_modified(module, dn);
447 ltdb_unlock(module, LDBLOCK);
452 ltdb_unlock(module, LDBLOCK);
458 find an element by attribute name. At the moment this does a linear search, it should
459 be re-coded to use a binary search once all places that modify records guarantee
462 return the index of the first matching element if found, otherwise -1
464 static int find_element(const struct ldb_message *msg, const char *name)
467 for (i=0;i<msg->num_elements;i++) {
468 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
477 add an element to an existing record. Assumes a elements array that we
478 can call re-alloc on, and assumed that we can re-use the data pointers from the
479 passed in additional values. Use with care!
481 returns 0 on success, -1 on failure (and sets errno)
483 static int msg_add_element(struct ldb_context *ldb,
484 struct ldb_message *msg, struct ldb_message_element *el)
486 struct ldb_message_element *e2;
489 e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
490 msg->num_elements+1);
498 e2 = &msg->elements[msg->num_elements];
501 e2->flags = el->flags;
503 if (el->num_values != 0) {
504 e2->values = talloc_array(msg->elements, struct ldb_val, el->num_values);
510 for (i=0;i<el->num_values;i++) {
511 e2->values[i] = el->values[i];
513 e2->num_values = el->num_values;
521 delete all elements having a specified attribute name
523 static int msg_delete_attribute(struct ldb_module *module,
524 struct ldb_context *ldb,
525 struct ldb_message *msg, const char *name)
529 for (i=0;i<msg->num_elements;i++) {
530 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
531 for (j=0;j<msg->elements[i].num_values;j++) {
532 ltdb_index_del_value(module, msg->dn, &msg->elements[i], j);
534 talloc_free(msg->elements[i].values);
535 if (msg->num_elements > (i+1)) {
536 memmove(&msg->elements[i],
538 sizeof(struct ldb_message_element)*
539 (msg->num_elements - (i+1)));
543 msg->elements = talloc_realloc(msg, msg->elements,
544 struct ldb_message_element,
553 delete all elements matching an attribute name/value
555 return 0 on success, -1 on failure
557 static int msg_delete_element(struct ldb_module *module,
558 struct ldb_message *msg,
560 const struct ldb_val *val)
562 struct ldb_context *ldb = module->ldb;
565 struct ldb_message_element *el;
567 found = find_element(msg, name);
572 el = &msg->elements[found];
574 for (i=0;i<el->num_values;i++) {
575 if (ltdb_val_equal(module, msg->elements[i].name, &el->values[i], val)) {
576 if (i<el->num_values-1) {
577 memmove(&el->values[i], &el->values[i+1],
578 sizeof(el->values[i])*(el->num_values-(i+1)));
581 if (el->num_values == 0) {
582 return msg_delete_attribute(module, ldb, msg, name);
593 modify a record - internal interface
595 yuck - this is O(n^2). Luckily n is usually small so we probably
596 get away with it, but if we ever have really large attribute lists
597 then we'll need to look at this again
599 int ltdb_modify_internal(struct ldb_module *module, const struct ldb_message *msg)
601 struct ldb_context *ldb = module->ldb;
602 struct ltdb_private *ltdb = module->private_data;
603 TDB_DATA tdb_key, tdb_data;
604 struct ldb_message *msg2;
608 tdb_key = ltdb_key(module, msg->dn);
613 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
614 if (!tdb_data.dptr) {
615 talloc_free(tdb_key.dptr);
619 msg2 = talloc(tdb_key.dptr, struct ldb_message);
621 talloc_free(tdb_key.dptr);
625 ret = ltdb_unpack_data(module, &tdb_data, msg2);
627 talloc_free(tdb_key.dptr);
636 for (i=0;i<msg->num_elements;i++) {
637 struct ldb_message_element *el = &msg->elements[i];
638 struct ldb_message_element *el2;
639 struct ldb_val *vals;
641 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
643 case LDB_FLAG_MOD_ADD:
644 /* add this element to the message. fail if it
646 ret = find_element(msg2, el->name);
649 if (msg_add_element(ldb, msg2, el) != 0) {
655 el2 = &msg2->elements[ret];
657 /* An attribute with this name already exists, add all
658 * values if they don't already exist. */
660 for (j=0;j<el->num_values;j++) {
661 if (ldb_msg_find_val(el2, &el->values[j])) {
662 ltdb->last_err_string =
663 "Type or value exists";
668 vals = talloc_realloc(msg2->elements, el2->values, struct ldb_val,
669 el2->num_values + el->num_values);
674 for (j=0;j<el->num_values;j++) {
675 vals[el2->num_values + j] =
676 ldb_val_dup(vals, &el->values[j]);
680 el2->num_values += el->num_values;
684 case LDB_FLAG_MOD_REPLACE:
685 /* replace all elements of this attribute name with the elements
686 listed. The attribute not existing is not an error */
687 msg_delete_attribute(module, ldb, msg2, msg->elements[i].name);
689 /* add the replacement element, if not empty */
690 if (msg->elements[i].num_values != 0 &&
691 msg_add_element(ldb, msg2, &msg->elements[i]) != 0) {
696 case LDB_FLAG_MOD_DELETE:
697 /* we could be being asked to delete all
698 values or just some values */
699 if (msg->elements[i].num_values == 0) {
700 if (msg_delete_attribute(module, ldb, msg2,
701 msg->elements[i].name) != 0) {
702 ltdb->last_err_string = "No such attribute";
707 for (j=0;j<msg->elements[i].num_values;j++) {
708 if (msg_delete_element(module,
710 msg->elements[i].name,
711 &msg->elements[i].values[j]) != 0) {
712 ltdb->last_err_string = "No such attribute";
715 if (ltdb_index_del_value(module, msg->dn, &msg->elements[i], j) != 0) {
721 ltdb->last_err_string = "Invalid ldb_modify flags";
726 /* we've made all the mods - save the modified record back into the database */
727 ret = ltdb_store(module, msg2, TDB_MODIFY);
729 talloc_free(tdb_key.dptr);
734 talloc_free(tdb_key.dptr);
742 static int ltdb_modify(struct ldb_module *module, const struct ldb_message *msg)
744 struct ltdb_private *ltdb = module->private_data;
747 ltdb->last_err_string = NULL;
749 ret = ltdb_check_special_dn(module, msg);
754 if (ltdb_lock(module, LDBLOCK) != 0) {
758 if (ltdb_cache_load(module) != 0) {
759 ltdb_unlock(module, LDBLOCK);
763 ret = ltdb_modify_internal(module, msg);
766 ltdb_modified(module, msg->dn);
769 ltdb_unlock(module, LDBLOCK);
777 static int ltdb_rename(struct ldb_module *module, const char *olddn, const char *newdn)
779 struct ltdb_private *ltdb = module->private_data;
781 struct ldb_message *msg;
782 const char *error_str;
784 ltdb->last_err_string = NULL;
786 if (ltdb_lock(module, LDBLOCK) != 0) {
790 if (ltdb_cache_load(module) != 0) {
791 ltdb_unlock(module, LDBLOCK);
795 msg = talloc(module, struct ldb_message);
800 /* in case any attribute of the message was indexed, we need
801 to fetch the old record */
802 ret = ltdb_search_dn1(module, olddn, msg);
804 /* not finding the old record is an error */
808 msg->dn = talloc_strdup(msg, newdn);
813 ret = ltdb_add(module, msg);
818 ret = ltdb_delete(module, olddn);
819 error_str = ltdb->last_err_string;
821 ltdb_delete(module, newdn);
824 ltdb->last_err_string = error_str;
827 ltdb_unlock(module, LDBLOCK);
833 ltdb_unlock(module, LDBLOCK);
839 return extended error information
841 static const char *ltdb_errstring(struct ldb_module *module)
843 struct ltdb_private *ltdb = module->private_data;
844 if (ltdb->last_err_string) {
845 return ltdb->last_err_string;
847 return tdb_errorstr(ltdb->tdb);
851 static const struct ldb_module_ops ltdb_ops = {
853 .search = ltdb_search,
854 .search_bytree = ltdb_search_bytree,
855 .add_record = ltdb_add,
856 .modify_record = ltdb_modify,
857 .delete_record = ltdb_delete,
858 .rename_record = ltdb_rename,
859 .named_lock = ltdb_lock,
860 .named_unlock = ltdb_unlock,
861 .errstring = ltdb_errstring
866 destroy the ltdb context
868 static int ltdb_destructor(void *p)
870 struct ltdb_private *ltdb = p;
871 tdb_close(ltdb->tdb);
876 connect to the database
878 int ltdb_connect(struct ldb_context *ldb, const char *url,
879 unsigned int flags, const char *options[])
882 int tdb_flags, open_flags;
883 struct ltdb_private *ltdb;
887 if (strchr(url, ':')) {
888 if (strncmp(url, "tdb://", 6) != 0) {
889 ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid tdb URL '%s'", url);
897 tdb_flags = TDB_DEFAULT;
899 if (flags & LDB_FLG_RDONLY) {
900 open_flags = O_RDONLY;
902 open_flags = O_CREAT | O_RDWR;
905 /* note that we use quite a large default hash size */
906 tdb = tdb_open(path, 10000, tdb_flags, open_flags, 0666);
908 ldb_debug(ldb, LDB_DEBUG_ERROR, "Unable to open tdb '%s'", path);
912 ltdb = talloc_zero(ldb, struct ltdb_private);
920 ltdb->sequence_number = 0;
922 talloc_set_destructor(ltdb, ltdb_destructor);
924 ldb->modules = talloc(ldb, struct ldb_module);
930 ldb->modules->ldb = ldb;
931 ldb->modules->prev = ldb->modules->next = NULL;
932 ldb->modules->private_data = ltdb;
933 ldb->modules->ops = <db_ops;