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(struct ldb_module *module, char *attr)
52 return ltdb_attribute_flags(module, attr) & LTDB_FLAG_CASE_INSENSITIVE;
56 form a TDB_DATA for a record key
59 note that the key for a record can depend on whether the
60 dn refers to a case sensitive index record or not
62 struct TDB_DATA ltdb_key(struct ldb_module *module, const char *dn)
64 struct ldb_context *ldb = module->ldb;
67 char *dn_folded = NULL;
68 const char *prefix = LTDB_INDEX ":";
73 most DNs are case insensitive. The exception is index DNs for
74 case sensitive attributes
76 there are 3 cases dealt with in this code:
78 1) if the dn doesn't start with @INDEX: then uppercase the attribute
79 names and the attributes values of case insensitive attributes
80 2) if the dn starts with @INDEX:attr and 'attr' is a case insensitive
81 attribute then uppercase whole dn
82 3) if the dn starts with @INDEX:attr and 'attr' is a case sensitive
83 attribute then uppercase up to the value of the attribute, but
86 if (strncmp(dn, prefix, strlen(prefix)) == 0 &&
87 (s = strchr(dn+strlen(prefix), ':'))) {
88 char *attr_name, *attr_name_folded;
89 attr_name = talloc_strndup(ldb, dn+strlen(prefix), (s-(dn+strlen(prefix))));
93 flags = ltdb_attribute_flags(module, attr_name);
95 if (flags & LTDB_FLAG_CASE_INSENSITIVE) {
96 dn_folded = ldb_casefold(ldb, dn);
98 attr_name_folded = ldb_casefold(ldb, attr_name);
99 if (!attr_name_folded) {
102 dn_folded = talloc_asprintf(ldb, "%s:%s:%s",
103 prefix, attr_name_folded,
105 talloc_free(attr_name_folded);
107 talloc_free(attr_name);
109 dn_folded = ldb_dn_fold(module, dn, ltdb_case_fold_attr_required);
116 key_str = talloc_asprintf(ldb, "DN=%s", dn_folded);
117 talloc_free(dn_folded);
124 key.dsize = strlen(key_str)+1;
136 lock the database for write - currently a single lock is used
138 static int ltdb_lock(struct ldb_module *module, const char *lockname)
140 struct ltdb_private *ltdb = module->private_data;
144 if (lockname == NULL) {
148 key = ltdb_key(module, lockname);
153 ret = tdb_chainlock(ltdb->tdb, key);
155 talloc_free(key.dptr);
161 unlock the database after a ltdb_lock()
163 static int ltdb_unlock(struct ldb_module *module, const char *lockname)
165 struct ltdb_private *ltdb = module->private_data;
168 if (lockname == NULL) {
172 key = ltdb_key(module, lockname);
177 tdb_chainunlock(ltdb->tdb, key);
179 talloc_free(key.dptr);
186 lock the database for read - use by ltdb_search
188 int ltdb_lock_read(struct ldb_module *module)
190 struct ltdb_private *ltdb = module->private_data;
193 key = ltdb_key(module, LDBLOCK);
197 ret = tdb_chainlock_read(ltdb->tdb, key);
198 talloc_free(key.dptr);
203 unlock the database after a ltdb_lock_read()
205 int ltdb_unlock_read(struct ldb_module *module)
207 struct ltdb_private *ltdb = module->private_data;
209 key = ltdb_key(module, LDBLOCK);
213 tdb_chainunlock_read(ltdb->tdb, key);
214 talloc_free(key.dptr);
219 check special dn's have valid attributes
220 currently only @ATTRIBUTES is checked
222 int ltdb_check_special_dn(struct ldb_module *module, const struct ldb_message *msg)
224 struct ltdb_private *ltdb = module->private_data;
227 if (strcmp(msg->dn, LTDB_ATTRIBUTES) != 0) {
231 /* we have @ATTRIBUTES, let's check attributes are fine */
232 /* should we check that we deny multivalued attributes ? */
233 for (i = 0; i < msg->num_elements; i++) {
234 for (j = 0; j < msg->elements[i].num_values; j++) {
235 if (ltdb_check_at_attributes_values(&msg->elements[i].values[j]) != 0) {
236 ltdb->last_err_string = "Invalid attribute value in an @ATTRIBUTES entry";
247 we've made a modification to a dn - possibly reindex and
248 update sequence number
250 static int ltdb_modified(struct ldb_module *module, const char *dn)
254 if (strcmp(dn, LTDB_INDEXLIST) == 0 ||
255 strcmp(dn, LTDB_ATTRIBUTES) == 0) {
256 ret = ltdb_reindex(module);
260 strcmp(dn, LTDB_BASEINFO) != 0) {
261 ret = ltdb_increase_sequence_number(module);
268 store a record into the db
270 int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs)
272 struct ltdb_private *ltdb = module->private_data;
273 TDB_DATA tdb_key, tdb_data;
276 tdb_key = ltdb_key(module, msg->dn);
281 ret = ltdb_pack_data(module, msg, &tdb_data);
283 talloc_free(tdb_key.dptr);
287 ret = tdb_store(ltdb->tdb, tdb_key, tdb_data, flgs);
292 ret = ltdb_index_add(module, msg);
294 tdb_delete(ltdb->tdb, tdb_key);
298 talloc_free(tdb_key.dptr);
299 talloc_free(tdb_data.dptr);
306 add a record to the database
308 static int ltdb_add(struct ldb_module *module, const struct ldb_message *msg)
310 struct ltdb_private *ltdb = module->private_data;
313 ltdb->last_err_string = NULL;
315 ret = ltdb_check_special_dn(module, msg);
320 if (ltdb_lock(module, LDBLOCK) != 0) {
324 if (ltdb_cache_load(module) != 0) {
325 ltdb_unlock(module, LDBLOCK);
329 ret = ltdb_store(module, msg, TDB_INSERT);
332 ltdb_modified(module, msg->dn);
335 ltdb_unlock(module, LDBLOCK);
341 delete a record from the database, not updating indexes (used for deleting
344 int ltdb_delete_noindex(struct ldb_module *module, const char *dn)
346 struct ltdb_private *ltdb = module->private_data;
350 tdb_key = ltdb_key(module, dn);
355 ret = tdb_delete(ltdb->tdb, tdb_key);
356 talloc_free(tdb_key.dptr);
362 delete a record from the database
364 static int ltdb_delete(struct ldb_module *module, const char *dn)
366 struct ltdb_private *ltdb = module->private_data;
368 struct ldb_message *msg = NULL;
370 ltdb->last_err_string = NULL;
372 if (ltdb_lock(module, LDBLOCK) != 0) {
376 if (ltdb_cache_load(module) != 0) {
380 msg = talloc(module, struct ldb_message);
385 /* in case any attribute of the message was indexed, we need
386 to fetch the old record */
387 ret = ltdb_search_dn1(module, dn, msg);
389 /* not finding the old record is an error */
393 ret = ltdb_delete_noindex(module, dn);
398 /* remove any indexed attributes */
399 ret = ltdb_index_del(module, msg);
402 ltdb_modified(module, dn);
406 ltdb_unlock(module, LDBLOCK);
411 ltdb_unlock(module, LDBLOCK);
417 find an element by attribute name. At the moment this does a linear search, it should
418 be re-coded to use a binary search once all places that modify records guarantee
421 return the index of the first matching element if found, otherwise -1
423 static int find_element(const struct ldb_message *msg, const char *name)
426 for (i=0;i<msg->num_elements;i++) {
427 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
436 add an element to an existing record. Assumes a elements array that we
437 can call re-alloc on, and assumed that we can re-use the data pointers from the
438 passed in additional values. Use with care!
440 returns 0 on success, -1 on failure (and sets errno)
442 static int msg_add_element(struct ldb_context *ldb,
443 struct ldb_message *msg, struct ldb_message_element *el)
445 struct ldb_message_element *e2;
448 e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
449 msg->num_elements+1);
457 e2 = &msg->elements[msg->num_elements];
460 e2->flags = el->flags;
462 if (el->num_values != 0) {
463 e2->values = talloc_array(msg->elements, struct ldb_val, el->num_values);
469 for (i=0;i<el->num_values;i++) {
470 e2->values[i] = el->values[i];
472 e2->num_values = el->num_values;
480 delete all elements having a specified attribute name
482 static int msg_delete_attribute(struct ldb_module *module,
483 struct ldb_context *ldb,
484 struct ldb_message *msg, const char *name)
488 for (i=0;i<msg->num_elements;i++) {
489 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
490 for (j=0;j<msg->elements[i].num_values;j++) {
491 ltdb_index_del_value(module, msg->dn, &msg->elements[i], j);
493 talloc_free(msg->elements[i].values);
494 if (msg->num_elements > (i+1)) {
495 memmove(&msg->elements[i],
497 sizeof(struct ldb_message_element)*
498 (msg->num_elements - (i+1)));
502 msg->elements = talloc_realloc(msg, msg->elements,
503 struct ldb_message_element,
512 delete all elements matching an attribute name/value
514 return 0 on success, -1 on failure
516 static int msg_delete_element(struct ldb_module *module,
517 struct ldb_message *msg,
519 const struct ldb_val *val)
521 struct ldb_context *ldb = module->ldb;
524 struct ldb_message_element *el;
526 found = find_element(msg, name);
531 el = &msg->elements[found];
533 for (i=0;i<el->num_values;i++) {
534 if (ltdb_val_equal(module, msg->elements[i].name, &el->values[i], val)) {
535 if (i<el->num_values-1) {
536 memmove(&el->values[i], &el->values[i+1],
537 sizeof(el->values[i])*(el->num_values-(i+1)));
540 if (el->num_values == 0) {
541 return msg_delete_attribute(module, ldb, msg, name);
552 modify a record - internal interface
554 yuck - this is O(n^2). Luckily n is usually small so we probably
555 get away with it, but if we ever have really large attribute lists
556 then we'll need to look at this again
558 int ltdb_modify_internal(struct ldb_module *module, const struct ldb_message *msg)
560 struct ldb_context *ldb = module->ldb;
561 struct ltdb_private *ltdb = module->private_data;
562 TDB_DATA tdb_key, tdb_data;
563 struct ldb_message *msg2;
567 tdb_key = ltdb_key(module, msg->dn);
572 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
573 if (!tdb_data.dptr) {
574 talloc_free(tdb_key.dptr);
578 msg2 = talloc(tdb_key.dptr, struct ldb_message);
580 talloc_free(tdb_key.dptr);
584 ret = ltdb_unpack_data(module, &tdb_data, msg2);
586 talloc_free(tdb_key.dptr);
595 for (i=0;i<msg->num_elements;i++) {
596 struct ldb_message_element *el = &msg->elements[i];
597 struct ldb_message_element *el2;
598 struct ldb_val *vals;
600 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
602 case LDB_FLAG_MOD_ADD:
603 /* add this element to the message. fail if it
605 ret = find_element(msg2, el->name);
608 if (msg_add_element(ldb, msg2, el) != 0) {
614 el2 = &msg2->elements[ret];
616 /* An attribute with this name already exists, add all
617 * values if they don't already exist. */
619 for (j=0;j<el->num_values;j++) {
620 if (ldb_msg_find_val(el2, &el->values[j])) {
621 ltdb->last_err_string =
622 "Type or value exists";
627 vals = talloc_realloc(msg2->elements, el2->values, struct ldb_val,
628 el2->num_values + el->num_values);
633 for (j=0;j<el->num_values;j++) {
634 vals[el2->num_values + j] =
635 ldb_val_dup(vals, &el->values[j]);
639 el2->num_values += el->num_values;
643 case LDB_FLAG_MOD_REPLACE:
644 /* replace all elements of this attribute name with the elements
645 listed. The attribute not existing is not an error */
646 msg_delete_attribute(module, ldb, msg2, msg->elements[i].name);
648 /* add the replacement element, if not empty */
649 if (msg->elements[i].num_values != 0 &&
650 msg_add_element(ldb, msg2, &msg->elements[i]) != 0) {
655 case LDB_FLAG_MOD_DELETE:
656 /* we could be being asked to delete all
657 values or just some values */
658 if (msg->elements[i].num_values == 0) {
659 if (msg_delete_attribute(module, ldb, msg2,
660 msg->elements[i].name) != 0) {
661 ltdb->last_err_string = "No such attribute";
666 for (j=0;j<msg->elements[i].num_values;j++) {
667 if (msg_delete_element(module,
669 msg->elements[i].name,
670 &msg->elements[i].values[j]) != 0) {
671 ltdb->last_err_string = "No such attribute";
674 if (ltdb_index_del_value(module, msg->dn, &msg->elements[i], j) != 0) {
682 /* we've made all the mods - save the modified record back into the database */
683 ret = ltdb_store(module, msg2, TDB_MODIFY);
685 talloc_free(tdb_key.dptr);
690 talloc_free(tdb_key.dptr);
698 static int ltdb_modify(struct ldb_module *module, const struct ldb_message *msg)
700 struct ltdb_private *ltdb = module->private_data;
703 ltdb->last_err_string = NULL;
705 ret = ltdb_check_special_dn(module, msg);
710 if (ltdb_lock(module, LDBLOCK) != 0) {
714 if (ltdb_cache_load(module) != 0) {
715 ltdb_unlock(module, LDBLOCK);
719 ret = ltdb_modify_internal(module, msg);
722 ltdb_modified(module, msg->dn);
725 ltdb_unlock(module, LDBLOCK);
733 static int ltdb_rename(struct ldb_module *module, const char *olddn, const char *newdn)
735 struct ltdb_private *ltdb = module->private_data;
737 struct ldb_message *msg;
738 const char *error_str;
740 ltdb->last_err_string = NULL;
742 if (ltdb_lock(module, LDBLOCK) != 0) {
746 msg = talloc(module, struct ldb_message);
751 /* in case any attribute of the message was indexed, we need
752 to fetch the old record */
753 ret = ltdb_search_dn1(module, olddn, msg);
755 /* not finding the old record is an error */
759 msg->dn = talloc_strdup(msg, newdn);
764 ret = ltdb_add(module, msg);
769 ret = ltdb_delete(module, olddn);
770 error_str = ltdb->last_err_string;
772 ltdb_delete(module, newdn);
775 ltdb->last_err_string = error_str;
778 ltdb_unlock(module, LDBLOCK);
784 ltdb_unlock(module, LDBLOCK);
790 return extended error information
792 static const char *ltdb_errstring(struct ldb_module *module)
794 struct ltdb_private *ltdb = module->private_data;
795 if (ltdb->last_err_string) {
796 return ltdb->last_err_string;
798 return tdb_errorstr(ltdb->tdb);
802 static const struct ldb_module_ops ltdb_ops = {
804 .search = ltdb_search,
805 .search_bytree = ltdb_search_bytree,
806 .add_record = ltdb_add,
807 .modify_record = ltdb_modify,
808 .delete_record = ltdb_delete,
809 .rename_record = ltdb_rename,
810 .named_lock = ltdb_lock,
811 .named_unlock = ltdb_unlock,
812 .errstring = ltdb_errstring
817 destroy the ltdb context
819 static int ltdb_destructor(void *p)
821 struct ltdb_private *ltdb = p;
822 tdb_close(ltdb->tdb);
827 connect to the database
829 struct ldb_context *ltdb_connect(const char *url,
831 const char *options[])
834 int tdb_flags, open_flags;
835 struct ltdb_private *ltdb;
837 struct ldb_context *ldb;
839 ldb = talloc_zero(NULL, struct ldb_context);
846 if (strchr(url, ':')) {
847 if (strncmp(url, "tdb://", 6) != 0) {
857 tdb_flags = TDB_DEFAULT;
859 if (flags & LDB_FLG_RDONLY) {
860 open_flags = O_RDONLY;
862 open_flags = O_CREAT | O_RDWR;
865 /* note that we use quite a large default hash size */
866 tdb = tdb_open(path, 10000, tdb_flags, open_flags, 0666);
872 ltdb = talloc_zero(ldb, struct ltdb_private);
881 ltdb->sequence_number = 0;
883 talloc_set_destructor(ltdb, ltdb_destructor);
885 ldb->modules = talloc(ldb, struct ldb_module);
891 ldb->modules->ldb = ldb;
892 ldb->modules->prev = ldb->modules->next = NULL;
893 ldb->modules->private_data = ltdb;
894 ldb->modules->ops = <db_ops;