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 casefold a dn. We need to uppercase the attribute names, and the
48 attribute values of case insensitive attributes. We also need to remove
49 extraneous spaces between elements
51 static char *ltdb_dn_fold(struct ldb_module *module, const char *dn)
53 const char *dn_orig = dn;
54 struct ldb_context *ldb = module->ldb;
55 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
59 ret = talloc_strdup(tmp_ctx, "");
60 if (ret == NULL) goto failed;
62 while ((len = strcspn(dn, ",")) > 0) {
63 char *p = strchr(dn, '=');
67 if (p == NULL || (p-dn) > len) goto failed;
69 attr = talloc_strndup(tmp_ctx, dn, p-dn);
70 if (attr == NULL) goto failed;
72 /* trim spaces from the attribute name */
73 while (' ' == *attr) attr++;
74 while (' ' == attr[strlen(attr)-1]) {
75 attr[strlen(attr)-1] = 0;
77 if (*attr == 0) goto failed;
79 value = talloc_strndup(tmp_ctx, p+1, len-(p+1-dn));
80 if (value == NULL) goto failed;
82 /* trim spaces from the value */
83 while (' ' == *value) value++;
84 while (' ' == value[strlen(value)-1]) {
85 value[strlen(value)-1] = 0;
87 if (*value == 0) goto failed;
89 flags = ltdb_attribute_flags(module, attr);
91 attr = ldb_casefold(ldb, attr);
92 if (attr == NULL) goto failed;
93 talloc_steal(tmp_ctx, attr);
95 if (flags & LTDB_FLAG_CASE_INSENSITIVE) {
96 value = ldb_casefold(ldb, value);
97 if (value == NULL) goto failed;
98 talloc_steal(tmp_ctx, value);
101 if (dn[len] == ',') {
102 ret = talloc_asprintf_append(ret, "%s=%s,", attr, value);
104 ret = talloc_asprintf_append(ret, "%s=%s", attr, value);
106 if (ret == NULL) goto failed;
109 if (*dn == ',') dn++;
112 talloc_steal(ldb, ret);
113 talloc_free(tmp_ctx);
117 talloc_free(tmp_ctx);
118 return ldb_casefold(ldb, dn_orig);
122 form a TDB_DATA for a record key
125 note that the key for a record can depend on whether the
126 dn refers to a case sensitive index record or not
128 struct TDB_DATA ltdb_key(struct ldb_module *module, const char *dn)
130 struct ldb_context *ldb = module->ldb;
132 char *key_str = NULL;
133 char *dn_folded = NULL;
134 const char *prefix = LTDB_INDEX ":";
139 most DNs are case insensitive. The exception is index DNs for
140 case sensitive attributes
142 there are 3 cases dealt with in this code:
144 1) if the dn doesn't start with @INDEX: then uppercase the attribute
145 names and the attributes values of case insensitive attributes
146 2) if the dn starts with @INDEX:attr and 'attr' is a case insensitive
147 attribute then uppercase whole dn
148 3) if the dn starts with @INDEX:attr and 'attr' is a case sensitive
149 attribute then uppercase up to the value of the attribute, but
152 if (strncmp(dn, prefix, strlen(prefix)) == 0 &&
153 (s = strchr(dn+strlen(prefix), ':'))) {
154 char *attr_name, *attr_name_folded;
155 attr_name = talloc_strndup(ldb, dn+strlen(prefix), (s-(dn+strlen(prefix))));
159 flags = ltdb_attribute_flags(module, attr_name);
161 if (flags & LTDB_FLAG_CASE_INSENSITIVE) {
162 dn_folded = ldb_casefold(ldb, dn);
164 attr_name_folded = ldb_casefold(ldb, attr_name);
165 if (!attr_name_folded) {
168 dn_folded = talloc_asprintf(ldb, "%s:%s:%s",
169 prefix, attr_name_folded,
171 talloc_free(attr_name_folded);
173 talloc_free(attr_name);
175 dn_folded = ltdb_dn_fold(module, dn);
182 key_str = talloc_asprintf(ldb, "DN=%s", dn_folded);
183 talloc_free(dn_folded);
190 key.dsize = strlen(key_str)+1;
202 lock the database for write - currently a single lock is used
204 static int ltdb_lock(struct ldb_module *module, const char *lockname)
206 struct ltdb_private *ltdb = module->private_data;
210 if (lockname == NULL) {
214 key = ltdb_key(module, lockname);
219 ret = tdb_chainlock(ltdb->tdb, key);
221 talloc_free(key.dptr);
227 unlock the database after a ltdb_lock()
229 static int ltdb_unlock(struct ldb_module *module, const char *lockname)
231 struct ltdb_private *ltdb = module->private_data;
234 if (lockname == NULL) {
238 key = ltdb_key(module, lockname);
243 tdb_chainunlock(ltdb->tdb, key);
245 talloc_free(key.dptr);
252 lock the database for read - use by ltdb_search
254 int ltdb_lock_read(struct ldb_module *module)
256 struct ltdb_private *ltdb = module->private_data;
259 key = ltdb_key(module, LDBLOCK);
263 ret = tdb_chainlock_read(ltdb->tdb, key);
264 talloc_free(key.dptr);
269 unlock the database after a ltdb_lock_read()
271 int ltdb_unlock_read(struct ldb_module *module)
273 struct ltdb_private *ltdb = module->private_data;
275 key = ltdb_key(module, LDBLOCK);
279 tdb_chainunlock_read(ltdb->tdb, key);
280 talloc_free(key.dptr);
285 check special dn's have valid attributes
286 currently only @ATTRIBUTES is checked
288 int ltdb_check_special_dn(struct ldb_module *module, const struct ldb_message *msg)
290 struct ltdb_private *ltdb = module->private_data;
293 if (strcmp(msg->dn, LTDB_ATTRIBUTES) != 0) {
297 /* we have @ATTRIBUTES, let's check attributes are fine */
298 /* should we check that we deny multivalued attributes ? */
299 for (i = 0; i < msg->num_elements; i++) {
300 for (j = 0; j < msg->elements[i].num_values; j++) {
301 if (ltdb_check_at_attributes_values(&msg->elements[i].values[j]) != 0) {
302 ltdb->last_err_string = "Invalid attribute value in an @ATTRIBUTES entry";
313 we've made a modification to a dn - possibly reindex and
314 update sequence number
316 static int ltdb_modified(struct ldb_module *module, const char *dn)
320 if (strcmp(dn, LTDB_INDEXLIST) == 0 ||
321 strcmp(dn, LTDB_ATTRIBUTES) == 0) {
322 ret = ltdb_reindex(module);
326 strcmp(dn, LTDB_BASEINFO) != 0) {
327 ret = ltdb_increase_sequence_number(module);
334 store a record into the db
336 int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs)
338 struct ltdb_private *ltdb = module->private_data;
339 TDB_DATA tdb_key, tdb_data;
342 tdb_key = ltdb_key(module, msg->dn);
347 ret = ltdb_pack_data(module, msg, &tdb_data);
349 talloc_free(tdb_key.dptr);
353 ret = tdb_store(ltdb->tdb, tdb_key, tdb_data, flgs);
358 ret = ltdb_index_add(module, msg);
360 tdb_delete(ltdb->tdb, tdb_key);
364 talloc_free(tdb_key.dptr);
365 talloc_free(tdb_data.dptr);
372 add a record to the database
374 static int ltdb_add(struct ldb_module *module, const struct ldb_message *msg)
376 struct ltdb_private *ltdb = module->private_data;
379 ltdb->last_err_string = NULL;
381 ret = ltdb_check_special_dn(module, msg);
386 if (ltdb_lock(module, LDBLOCK) != 0) {
390 if (ltdb_cache_load(module) != 0) {
391 ltdb_unlock(module, LDBLOCK);
395 ret = ltdb_store(module, msg, TDB_INSERT);
398 ltdb_modified(module, msg->dn);
401 ltdb_unlock(module, LDBLOCK);
407 delete a record from the database, not updating indexes (used for deleting
410 int ltdb_delete_noindex(struct ldb_module *module, const char *dn)
412 struct ltdb_private *ltdb = module->private_data;
416 tdb_key = ltdb_key(module, dn);
421 ret = tdb_delete(ltdb->tdb, tdb_key);
422 talloc_free(tdb_key.dptr);
428 delete a record from the database
430 static int ltdb_delete(struct ldb_module *module, const char *dn)
432 struct ltdb_private *ltdb = module->private_data;
434 struct ldb_message *msg = NULL;
436 ltdb->last_err_string = NULL;
438 if (ltdb_lock(module, LDBLOCK) != 0) {
442 if (ltdb_cache_load(module) != 0) {
446 msg = talloc(module, struct ldb_message);
451 /* in case any attribute of the message was indexed, we need
452 to fetch the old record */
453 ret = ltdb_search_dn1(module, dn, msg);
455 /* not finding the old record is an error */
459 ret = ltdb_delete_noindex(module, dn);
464 /* remove any indexed attributes */
465 ret = ltdb_index_del(module, msg);
468 ltdb_modified(module, dn);
472 ltdb_unlock(module, LDBLOCK);
477 ltdb_unlock(module, LDBLOCK);
483 find an element by attribute name. At the moment this does a linear search, it should
484 be re-coded to use a binary search once all places that modify records guarantee
487 return the index of the first matching element if found, otherwise -1
489 static int find_element(const struct ldb_message *msg, const char *name)
492 for (i=0;i<msg->num_elements;i++) {
493 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
502 add an element to an existing record. Assumes a elements array that we
503 can call re-alloc on, and assumed that we can re-use the data pointers from the
504 passed in additional values. Use with care!
506 returns 0 on success, -1 on failure (and sets errno)
508 static int msg_add_element(struct ldb_context *ldb,
509 struct ldb_message *msg, struct ldb_message_element *el)
511 struct ldb_message_element *e2;
514 e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
515 msg->num_elements+1);
523 e2 = &msg->elements[msg->num_elements];
526 e2->flags = el->flags;
528 if (el->num_values != 0) {
529 e2->values = talloc_array(msg->elements, struct ldb_val, el->num_values);
535 for (i=0;i<el->num_values;i++) {
536 e2->values[i] = el->values[i];
538 e2->num_values = el->num_values;
546 delete all elements having a specified attribute name
548 static int msg_delete_attribute(struct ldb_module *module,
549 struct ldb_context *ldb,
550 struct ldb_message *msg, const char *name)
554 for (i=0;i<msg->num_elements;i++) {
555 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
556 for (j=0;j<msg->elements[i].num_values;j++) {
557 ltdb_index_del_value(module, msg->dn, &msg->elements[i], j);
559 talloc_free(msg->elements[i].values);
560 if (msg->num_elements > (i+1)) {
561 memmove(&msg->elements[i],
563 sizeof(struct ldb_message_element)*
564 (msg->num_elements - (i+1)));
568 msg->elements = talloc_realloc(msg, msg->elements,
569 struct ldb_message_element,
578 delete all elements matching an attribute name/value
580 return 0 on success, -1 on failure
582 static int msg_delete_element(struct ldb_module *module,
583 struct ldb_message *msg,
585 const struct ldb_val *val)
587 struct ldb_context *ldb = module->ldb;
590 struct ldb_message_element *el;
592 found = find_element(msg, name);
597 el = &msg->elements[found];
599 for (i=0;i<el->num_values;i++) {
600 if (ltdb_val_equal(module, msg->elements[i].name, &el->values[i], val)) {
601 if (i<el->num_values-1) {
602 memmove(&el->values[i], &el->values[i+1],
603 sizeof(el->values[i])*(el->num_values-(i+1)));
606 if (el->num_values == 0) {
607 return msg_delete_attribute(module, ldb, msg, name);
618 modify a record - internal interface
620 yuck - this is O(n^2). Luckily n is usually small so we probably
621 get away with it, but if we ever have really large attribute lists
622 then we'll need to look at this again
624 int ltdb_modify_internal(struct ldb_module *module, const struct ldb_message *msg)
626 struct ldb_context *ldb = module->ldb;
627 struct ltdb_private *ltdb = module->private_data;
628 TDB_DATA tdb_key, tdb_data;
629 struct ldb_message *msg2;
633 tdb_key = ltdb_key(module, msg->dn);
638 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
639 if (!tdb_data.dptr) {
640 talloc_free(tdb_key.dptr);
644 msg2 = talloc(tdb_key.dptr, struct ldb_message);
646 talloc_free(tdb_key.dptr);
650 ret = ltdb_unpack_data(module, &tdb_data, msg2);
652 talloc_free(tdb_key.dptr);
661 for (i=0;i<msg->num_elements;i++) {
662 struct ldb_message_element *el = &msg->elements[i];
663 struct ldb_message_element *el2;
664 struct ldb_val *vals;
666 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
668 case LDB_FLAG_MOD_ADD:
669 /* add this element to the message. fail if it
671 ret = find_element(msg2, el->name);
674 if (msg_add_element(ldb, msg2, el) != 0) {
680 el2 = &msg2->elements[ret];
682 /* An attribute with this name already exists, add all
683 * values if they don't already exist. */
685 for (j=0;j<el->num_values;j++) {
686 if (ldb_msg_find_val(el2, &el->values[j])) {
687 ltdb->last_err_string =
688 "Type or value exists";
693 vals = talloc_realloc(msg2->elements, el2->values, struct ldb_val,
694 el2->num_values + el->num_values);
699 for (j=0;j<el->num_values;j++) {
700 vals[el2->num_values + j] =
701 ldb_val_dup(vals, &el->values[j]);
705 el2->num_values += el->num_values;
709 case LDB_FLAG_MOD_REPLACE:
710 /* replace all elements of this attribute name with the elements
711 listed. The attribute not existing is not an error */
712 msg_delete_attribute(module, ldb, msg2, msg->elements[i].name);
714 /* add the replacement element, if not empty */
715 if (msg->elements[i].num_values != 0 &&
716 msg_add_element(ldb, msg2, &msg->elements[i]) != 0) {
721 case LDB_FLAG_MOD_DELETE:
722 /* we could be being asked to delete all
723 values or just some values */
724 if (msg->elements[i].num_values == 0) {
725 if (msg_delete_attribute(module, ldb, msg2,
726 msg->elements[i].name) != 0) {
727 ltdb->last_err_string = "No such attribute";
732 for (j=0;j<msg->elements[i].num_values;j++) {
733 if (msg_delete_element(module,
735 msg->elements[i].name,
736 &msg->elements[i].values[j]) != 0) {
737 ltdb->last_err_string = "No such attribute";
740 if (ltdb_index_del_value(module, msg->dn, &msg->elements[i], j) != 0) {
748 /* we've made all the mods - save the modified record back into the database */
749 ret = ltdb_store(module, msg2, TDB_MODIFY);
751 talloc_free(tdb_key.dptr);
756 talloc_free(tdb_key.dptr);
764 static int ltdb_modify(struct ldb_module *module, const struct ldb_message *msg)
766 struct ltdb_private *ltdb = module->private_data;
769 ltdb->last_err_string = NULL;
771 ret = ltdb_check_special_dn(module, msg);
776 if (ltdb_lock(module, LDBLOCK) != 0) {
780 if (ltdb_cache_load(module) != 0) {
781 ltdb_unlock(module, LDBLOCK);
785 ret = ltdb_modify_internal(module, msg);
788 ltdb_modified(module, msg->dn);
791 ltdb_unlock(module, LDBLOCK);
799 static int ltdb_rename(struct ldb_module *module, const char *olddn, const char *newdn)
801 struct ltdb_private *ltdb = module->private_data;
803 struct ldb_message *msg;
804 const char *error_str;
806 ltdb->last_err_string = NULL;
808 if (ltdb_lock(module, LDBLOCK) != 0) {
812 msg = talloc(module, struct ldb_message);
817 /* in case any attribute of the message was indexed, we need
818 to fetch the old record */
819 ret = ltdb_search_dn1(module, olddn, msg);
821 /* not finding the old record is an error */
825 msg->dn = talloc_strdup(msg, newdn);
830 ret = ltdb_add(module, msg);
835 ret = ltdb_delete(module, olddn);
836 error_str = ltdb->last_err_string;
838 ltdb_delete(module, newdn);
841 ltdb->last_err_string = error_str;
844 ltdb_unlock(module, LDBLOCK);
850 ltdb_unlock(module, LDBLOCK);
856 return extended error information
858 static const char *ltdb_errstring(struct ldb_module *module)
860 struct ltdb_private *ltdb = module->private_data;
861 if (ltdb->last_err_string) {
862 return ltdb->last_err_string;
864 return tdb_errorstr(ltdb->tdb);
868 static const struct ldb_module_ops ltdb_ops = {
882 destroy the ltdb context
884 static int ltdb_destructor(void *p)
886 struct ltdb_private *ltdb = p;
887 tdb_close(ltdb->tdb);
892 connect to the database
894 struct ldb_context *ltdb_connect(const char *url,
896 const char *options[])
899 int tdb_flags, open_flags;
900 struct ltdb_private *ltdb;
902 struct ldb_context *ldb;
904 ldb = talloc_zero(NULL, struct ldb_context);
911 if (strchr(url, ':')) {
912 if (strncmp(url, "tdb://", 6) != 0) {
922 tdb_flags = TDB_DEFAULT;
924 if (flags & LDB_FLG_RDONLY) {
925 open_flags = O_RDONLY;
927 open_flags = O_CREAT | O_RDWR;
930 /* note that we use quite a large default hash size */
931 tdb = tdb_open(path, 10000, tdb_flags, open_flags, 0666);
937 ltdb = talloc_zero(ldb, struct ltdb_private);
946 ltdb->sequence_number = 0;
948 talloc_set_destructor(ltdb, ltdb_destructor);
950 ldb->modules = talloc(ldb, struct ldb_module);
956 ldb->modules->ldb = ldb;
957 ldb->modules->prev = ldb->modules->next = NULL;
958 ldb->modules->private_data = ltdb;
959 ldb->modules->ops = <db_ops;