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/ldb_tdb/ldb_tdb.h"
43 #define LDBLOCK "INT_LDBLOCK"
47 callback function used in call to ldb_dn_fold() for determining whether an
48 attribute type requires case folding.
50 static int ltdb_case_fold_attr_required(void * user_data, char *attr)
52 struct ldb_module *module = user_data;
54 return ltdb_attribute_flags(module, attr) & LTDB_FLAG_CASE_INSENSITIVE;
58 form a TDB_DATA for a record key
61 note that the key for a record can depend on whether the
62 dn refers to a case sensitive index record or not
64 struct TDB_DATA ltdb_key(struct ldb_module *module, const char *dn)
66 struct ldb_context *ldb = module->ldb;
69 char *dn_folded = NULL;
70 const char *prefix = LTDB_INDEX ":";
75 most DNs are case insensitive. The exception is index DNs for
76 case sensitive attributes
78 there are 3 cases dealt with in this code:
80 1) if the dn doesn't start with @INDEX: then uppercase the attribute
81 names and the attributes values of case insensitive attributes
82 2) if the dn starts with @INDEX:attr and 'attr' is a case insensitive
83 attribute then uppercase whole dn
84 3) if the dn starts with @INDEX:attr and 'attr' is a case sensitive
85 attribute then uppercase up to the value of the attribute, but
88 if (strncmp(dn, prefix, strlen(prefix)) == 0 &&
89 (s = strchr(dn+strlen(prefix), ':'))) {
90 char *attr_name, *attr_name_folded;
91 attr_name = talloc_strndup(ldb, dn+strlen(prefix), (s-(dn+strlen(prefix))));
95 flags = ltdb_attribute_flags(module, attr_name);
97 if (flags & LTDB_FLAG_CASE_INSENSITIVE) {
98 dn_folded = ldb_casefold(ldb, dn);
100 attr_name_folded = ldb_casefold(ldb, attr_name);
101 if (!attr_name_folded) {
104 dn_folded = talloc_asprintf(ldb, "%s:%s:%s",
105 prefix, attr_name_folded,
107 talloc_free(attr_name_folded);
109 talloc_free(attr_name);
111 dn_folded = ldb_dn_fold(module->ldb, dn,
112 module, ltdb_case_fold_attr_required);
119 key_str = talloc_asprintf(ldb, "DN=%s", dn_folded);
120 talloc_free(dn_folded);
127 key.dsize = strlen(key_str)+1;
139 lock the database for write - currently a single lock is used
141 static int ltdb_lock(struct ldb_module *module, const char *lockname)
143 struct ltdb_private *ltdb = module->private_data;
147 if (lockname == NULL) {
151 key = ltdb_key(module, lockname);
156 ret = tdb_chainlock(ltdb->tdb, key);
158 talloc_free(key.dptr);
164 unlock the database after a ltdb_lock()
166 static int ltdb_unlock(struct ldb_module *module, const char *lockname)
168 struct ltdb_private *ltdb = module->private_data;
171 if (lockname == NULL) {
175 key = ltdb_key(module, lockname);
180 tdb_chainunlock(ltdb->tdb, key);
182 talloc_free(key.dptr);
189 lock the database for read - use by ltdb_search
191 int ltdb_lock_read(struct ldb_module *module)
193 struct ltdb_private *ltdb = module->private_data;
196 key = ltdb_key(module, LDBLOCK);
200 ret = tdb_chainlock_read(ltdb->tdb, key);
201 talloc_free(key.dptr);
206 unlock the database after a ltdb_lock_read()
208 int ltdb_unlock_read(struct ldb_module *module)
210 struct ltdb_private *ltdb = module->private_data;
212 key = ltdb_key(module, LDBLOCK);
216 tdb_chainunlock_read(ltdb->tdb, key);
217 talloc_free(key.dptr);
222 check special dn's have valid attributes
223 currently only @ATTRIBUTES is checked
225 int ltdb_check_special_dn(struct ldb_module *module, const struct ldb_message *msg)
227 struct ltdb_private *ltdb = module->private_data;
230 if (strcmp(msg->dn, LTDB_ATTRIBUTES) != 0) {
234 /* we have @ATTRIBUTES, let's check attributes are fine */
235 /* should we check that we deny multivalued attributes ? */
236 for (i = 0; i < msg->num_elements; i++) {
237 for (j = 0; j < msg->elements[i].num_values; j++) {
238 if (ltdb_check_at_attributes_values(&msg->elements[i].values[j]) != 0) {
239 ltdb->last_err_string = "Invalid attribute value in an @ATTRIBUTES entry";
250 we've made a modification to a dn - possibly reindex and
251 update sequence number
253 static int ltdb_modified(struct ldb_module *module, const char *dn)
257 if (strcmp(dn, LTDB_INDEXLIST) == 0 ||
258 strcmp(dn, LTDB_ATTRIBUTES) == 0) {
259 ret = ltdb_reindex(module);
263 strcmp(dn, LTDB_BASEINFO) != 0) {
264 ret = ltdb_increase_sequence_number(module);
271 store a record into the db
273 int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs)
275 struct ltdb_private *ltdb = module->private_data;
276 TDB_DATA tdb_key, tdb_data;
279 tdb_key = ltdb_key(module, msg->dn);
284 ret = ltdb_pack_data(module, msg, &tdb_data);
286 talloc_free(tdb_key.dptr);
290 ret = tdb_store(ltdb->tdb, tdb_key, tdb_data, flgs);
295 ret = ltdb_index_add(module, msg);
297 tdb_delete(ltdb->tdb, tdb_key);
301 talloc_free(tdb_key.dptr);
302 talloc_free(tdb_data.dptr);
309 add a record to the database
311 static int ltdb_add(struct ldb_module *module, const struct ldb_message *msg)
313 struct ltdb_private *ltdb = module->private_data;
316 ltdb->last_err_string = NULL;
318 ret = ltdb_check_special_dn(module, msg);
323 if (ltdb_lock(module, LDBLOCK) != 0) {
327 if (ltdb_cache_load(module) != 0) {
328 ltdb_unlock(module, LDBLOCK);
332 ret = ltdb_store(module, msg, TDB_INSERT);
335 ltdb_modified(module, msg->dn);
338 ltdb_unlock(module, LDBLOCK);
344 delete a record from the database, not updating indexes (used for deleting
347 int ltdb_delete_noindex(struct ldb_module *module, const char *dn)
349 struct ltdb_private *ltdb = module->private_data;
353 tdb_key = ltdb_key(module, dn);
358 ret = tdb_delete(ltdb->tdb, tdb_key);
359 talloc_free(tdb_key.dptr);
365 delete a record from the database
367 static int ltdb_delete(struct ldb_module *module, const char *dn)
369 struct ltdb_private *ltdb = module->private_data;
371 struct ldb_message *msg = NULL;
373 ltdb->last_err_string = NULL;
375 if (ltdb_lock(module, LDBLOCK) != 0) {
379 if (ltdb_cache_load(module) != 0) {
383 msg = talloc(module, struct ldb_message);
388 /* in case any attribute of the message was indexed, we need
389 to fetch the old record */
390 ret = ltdb_search_dn1(module, dn, msg);
392 /* not finding the old record is an error */
396 ret = ltdb_delete_noindex(module, dn);
401 /* remove any indexed attributes */
402 ret = ltdb_index_del(module, msg);
405 ltdb_modified(module, dn);
409 ltdb_unlock(module, LDBLOCK);
414 ltdb_unlock(module, LDBLOCK);
420 find an element by attribute name. At the moment this does a linear search, it should
421 be re-coded to use a binary search once all places that modify records guarantee
424 return the index of the first matching element if found, otherwise -1
426 static int find_element(const struct ldb_message *msg, const char *name)
429 for (i=0;i<msg->num_elements;i++) {
430 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
439 add an element to an existing record. Assumes a elements array that we
440 can call re-alloc on, and assumed that we can re-use the data pointers from the
441 passed in additional values. Use with care!
443 returns 0 on success, -1 on failure (and sets errno)
445 static int msg_add_element(struct ldb_context *ldb,
446 struct ldb_message *msg, struct ldb_message_element *el)
448 struct ldb_message_element *e2;
451 e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
452 msg->num_elements+1);
460 e2 = &msg->elements[msg->num_elements];
463 e2->flags = el->flags;
465 if (el->num_values != 0) {
466 e2->values = talloc_array(msg->elements, struct ldb_val, el->num_values);
472 for (i=0;i<el->num_values;i++) {
473 e2->values[i] = el->values[i];
475 e2->num_values = el->num_values;
483 delete all elements having a specified attribute name
485 static int msg_delete_attribute(struct ldb_module *module,
486 struct ldb_context *ldb,
487 struct ldb_message *msg, const char *name)
491 for (i=0;i<msg->num_elements;i++) {
492 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
493 for (j=0;j<msg->elements[i].num_values;j++) {
494 ltdb_index_del_value(module, msg->dn, &msg->elements[i], j);
496 talloc_free(msg->elements[i].values);
497 if (msg->num_elements > (i+1)) {
498 memmove(&msg->elements[i],
500 sizeof(struct ldb_message_element)*
501 (msg->num_elements - (i+1)));
505 msg->elements = talloc_realloc(msg, msg->elements,
506 struct ldb_message_element,
515 delete all elements matching an attribute name/value
517 return 0 on success, -1 on failure
519 static int msg_delete_element(struct ldb_module *module,
520 struct ldb_message *msg,
522 const struct ldb_val *val)
524 struct ldb_context *ldb = module->ldb;
527 struct ldb_message_element *el;
529 found = find_element(msg, name);
534 el = &msg->elements[found];
536 for (i=0;i<el->num_values;i++) {
537 if (ltdb_val_equal(module, msg->elements[i].name, &el->values[i], val)) {
538 if (i<el->num_values-1) {
539 memmove(&el->values[i], &el->values[i+1],
540 sizeof(el->values[i])*(el->num_values-(i+1)));
543 if (el->num_values == 0) {
544 return msg_delete_attribute(module, ldb, msg, name);
555 modify a record - internal interface
557 yuck - this is O(n^2). Luckily n is usually small so we probably
558 get away with it, but if we ever have really large attribute lists
559 then we'll need to look at this again
561 int ltdb_modify_internal(struct ldb_module *module, const struct ldb_message *msg)
563 struct ldb_context *ldb = module->ldb;
564 struct ltdb_private *ltdb = module->private_data;
565 TDB_DATA tdb_key, tdb_data;
566 struct ldb_message *msg2;
570 tdb_key = ltdb_key(module, msg->dn);
575 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
576 if (!tdb_data.dptr) {
577 talloc_free(tdb_key.dptr);
581 msg2 = talloc(tdb_key.dptr, struct ldb_message);
583 talloc_free(tdb_key.dptr);
587 ret = ltdb_unpack_data(module, &tdb_data, msg2);
589 talloc_free(tdb_key.dptr);
598 for (i=0;i<msg->num_elements;i++) {
599 struct ldb_message_element *el = &msg->elements[i];
600 struct ldb_message_element *el2;
601 struct ldb_val *vals;
603 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
605 case LDB_FLAG_MOD_ADD:
606 /* add this element to the message. fail if it
608 ret = find_element(msg2, el->name);
611 if (msg_add_element(ldb, msg2, el) != 0) {
617 el2 = &msg2->elements[ret];
619 /* An attribute with this name already exists, add all
620 * values if they don't already exist. */
622 for (j=0;j<el->num_values;j++) {
623 if (ldb_msg_find_val(el2, &el->values[j])) {
624 ltdb->last_err_string =
625 "Type or value exists";
630 vals = talloc_realloc(msg2->elements, el2->values, struct ldb_val,
631 el2->num_values + el->num_values);
636 for (j=0;j<el->num_values;j++) {
637 vals[el2->num_values + j] =
638 ldb_val_dup(vals, &el->values[j]);
642 el2->num_values += el->num_values;
646 case LDB_FLAG_MOD_REPLACE:
647 /* replace all elements of this attribute name with the elements
648 listed. The attribute not existing is not an error */
649 msg_delete_attribute(module, ldb, msg2, msg->elements[i].name);
651 /* add the replacement element, if not empty */
652 if (msg->elements[i].num_values != 0 &&
653 msg_add_element(ldb, msg2, &msg->elements[i]) != 0) {
658 case LDB_FLAG_MOD_DELETE:
659 /* we could be being asked to delete all
660 values or just some values */
661 if (msg->elements[i].num_values == 0) {
662 if (msg_delete_attribute(module, ldb, msg2,
663 msg->elements[i].name) != 0) {
664 ltdb->last_err_string = "No such attribute";
669 for (j=0;j<msg->elements[i].num_values;j++) {
670 if (msg_delete_element(module,
672 msg->elements[i].name,
673 &msg->elements[i].values[j]) != 0) {
674 ltdb->last_err_string = "No such attribute";
677 if (ltdb_index_del_value(module, msg->dn, &msg->elements[i], j) != 0) {
685 /* we've made all the mods - save the modified record back into the database */
686 ret = ltdb_store(module, msg2, TDB_MODIFY);
688 talloc_free(tdb_key.dptr);
693 talloc_free(tdb_key.dptr);
701 static int ltdb_modify(struct ldb_module *module, const struct ldb_message *msg)
703 struct ltdb_private *ltdb = module->private_data;
706 ltdb->last_err_string = NULL;
708 ret = ltdb_check_special_dn(module, msg);
713 if (ltdb_lock(module, LDBLOCK) != 0) {
717 if (ltdb_cache_load(module) != 0) {
718 ltdb_unlock(module, LDBLOCK);
722 ret = ltdb_modify_internal(module, msg);
725 ltdb_modified(module, msg->dn);
728 ltdb_unlock(module, LDBLOCK);
736 static int ltdb_rename(struct ldb_module *module, const char *olddn, const char *newdn)
738 struct ltdb_private *ltdb = module->private_data;
740 struct ldb_message *msg;
741 const char *error_str;
743 ltdb->last_err_string = NULL;
745 if (ltdb_lock(module, LDBLOCK) != 0) {
749 msg = talloc(module, struct ldb_message);
754 /* in case any attribute of the message was indexed, we need
755 to fetch the old record */
756 ret = ltdb_search_dn1(module, olddn, msg);
758 /* not finding the old record is an error */
762 msg->dn = talloc_strdup(msg, newdn);
767 ret = ltdb_add(module, msg);
772 ret = ltdb_delete(module, olddn);
773 error_str = ltdb->last_err_string;
775 ltdb_delete(module, newdn);
778 ltdb->last_err_string = error_str;
781 ltdb_unlock(module, LDBLOCK);
787 ltdb_unlock(module, LDBLOCK);
793 return extended error information
795 static const char *ltdb_errstring(struct ldb_module *module)
797 struct ltdb_private *ltdb = module->private_data;
798 if (ltdb->last_err_string) {
799 return ltdb->last_err_string;
801 return tdb_errorstr(ltdb->tdb);
805 static const struct ldb_module_ops ltdb_ops = {
807 .search = ltdb_search,
808 .search_bytree = ltdb_search_bytree,
809 .add_record = ltdb_add,
810 .modify_record = ltdb_modify,
811 .delete_record = ltdb_delete,
812 .rename_record = ltdb_rename,
813 .named_lock = ltdb_lock,
814 .named_unlock = ltdb_unlock,
815 .errstring = ltdb_errstring
820 destroy the ltdb context
822 static int ltdb_destructor(void *p)
824 struct ltdb_private *ltdb = p;
825 tdb_close(ltdb->tdb);
830 connect to the database
832 struct ldb_context *ltdb_connect(const char *url,
834 const char *options[])
837 int tdb_flags, open_flags;
838 struct ltdb_private *ltdb;
840 struct ldb_context *ldb;
842 ldb = talloc_zero(NULL, struct ldb_context);
849 if (strchr(url, ':')) {
850 if (strncmp(url, "tdb://", 6) != 0) {
860 tdb_flags = TDB_DEFAULT;
862 if (flags & LDB_FLG_RDONLY) {
863 open_flags = O_RDONLY;
865 open_flags = O_CREAT | O_RDWR;
868 /* note that we use quite a large default hash size */
869 tdb = tdb_open(path, 10000, tdb_flags, open_flags, 0666);
875 ltdb = talloc_zero(ldb, struct ltdb_private);
884 ltdb->sequence_number = 0;
886 talloc_set_destructor(ltdb, ltdb_destructor);
888 ldb->modules = talloc(ldb, struct ldb_module);
894 ldb->modules->ldb = ldb;
895 ldb->modules->prev = ldb->modules->next = NULL;
896 ldb->modules->private_data = ltdb;
897 ldb->modules->ops = <db_ops;