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 form a TDB_DATA for a record key
50 note that the key for a record can depend on whether the
51 dn refers to a case sensitive index record or not
53 struct TDB_DATA ltdb_key(struct ldb_module *module, const char *dn)
55 struct ldb_context *ldb = module->ldb;
58 char *dn_folded = NULL;
59 struct ldb_dn *edn = NULL;
60 struct ldb_dn *cedn = NULL;
63 most DNs are case insensitive. The exception is index DNs for
64 case sensitive attributes
66 there are 3 cases dealt with in this code:
68 1) if the dn doesn't start with @ then uppercase the attribute
69 names and the attributes values of case insensitive attributes
70 2) if the dn starts with @ then leave it alone - the indexing code handles
74 dn_folded = talloc_strdup(ldb, dn);
76 edn = ldb_dn_explode(ldb, dn);
80 cedn = ldb_dn_casefold(ldb, edn);
84 dn_folded = ldb_dn_linearize(ldb, cedn);
92 key_str = talloc_asprintf(ldb, "DN=%s", dn_folded);
93 talloc_free(dn_folded);
100 key.dsize = strlen(key_str) + 1;
114 lock the database for write - currently a single lock is used
116 static int ltdb_lock(struct ldb_module *module, const char *lockname)
118 struct ltdb_private *ltdb = module->private_data;
123 if (lockname == NULL) {
127 lock_dn = talloc_asprintf(module->ldb, "%s_%s", LDBLOCK, lockname);
128 if (lock_dn == NULL) {
132 key = ltdb_key(module, lock_dn);
134 talloc_free(lock_dn);
138 ret = tdb_chainlock(ltdb->tdb, key);
140 talloc_free(key.dptr);
141 talloc_free(lock_dn);
147 unlock the database after a ltdb_lock()
149 static int ltdb_unlock(struct ldb_module *module, const char *lockname)
151 struct ltdb_private *ltdb = module->private_data;
155 if (lockname == NULL) {
159 lock_dn = talloc_asprintf(module->ldb, "%s_%s", LDBLOCK, lockname);
160 if (lock_dn == NULL) {
164 key = ltdb_key(module, lock_dn);
166 talloc_free(lock_dn);
170 tdb_chainunlock(ltdb->tdb, key);
172 talloc_free(key.dptr);
173 talloc_free(lock_dn);
180 lock the database for read - use by ltdb_search
182 int ltdb_lock_read(struct ldb_module *module)
184 struct ltdb_private *ltdb = module->private_data;
187 key = ltdb_key(module, LDBLOCK);
191 ret = tdb_chainlock_read(ltdb->tdb, key);
192 talloc_free(key.dptr);
197 unlock the database after a ltdb_lock_read()
199 int ltdb_unlock_read(struct ldb_module *module)
201 struct ltdb_private *ltdb = module->private_data;
203 key = ltdb_key(module, LDBLOCK);
207 tdb_chainunlock_read(ltdb->tdb, key);
208 talloc_free(key.dptr);
213 check special dn's have valid attributes
214 currently only @ATTRIBUTES is checked
216 int ltdb_check_special_dn(struct ldb_module *module, const struct ldb_message *msg)
218 struct ltdb_private *ltdb = module->private_data;
221 if (strcmp(msg->dn, LTDB_ATTRIBUTES) != 0) {
225 /* we have @ATTRIBUTES, let's check attributes are fine */
226 /* should we check that we deny multivalued attributes ? */
227 for (i = 0; i < msg->num_elements; i++) {
228 for (j = 0; j < msg->elements[i].num_values; j++) {
229 if (ltdb_check_at_attributes_values(&msg->elements[i].values[j]) != 0) {
230 ltdb->last_err_string = "Invalid attribute value in an @ATTRIBUTES entry";
241 we've made a modification to a dn - possibly reindex and
242 update sequence number
244 static int ltdb_modified(struct ldb_module *module, const char *dn)
248 if (strcmp(dn, LTDB_INDEXLIST) == 0 ||
249 strcmp(dn, LTDB_ATTRIBUTES) == 0) {
250 ret = ltdb_reindex(module);
254 strcmp(dn, LTDB_BASEINFO) != 0) {
255 ret = ltdb_increase_sequence_number(module);
262 store a record into the db
264 int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs)
266 struct ltdb_private *ltdb = module->private_data;
267 TDB_DATA tdb_key, tdb_data;
270 tdb_key = ltdb_key(module, msg->dn);
275 ret = ltdb_pack_data(module, msg, &tdb_data);
277 talloc_free(tdb_key.dptr);
281 ret = tdb_store(ltdb->tdb, tdb_key, tdb_data, flgs);
286 ret = ltdb_index_add(module, msg);
288 tdb_delete(ltdb->tdb, tdb_key);
292 talloc_free(tdb_key.dptr);
293 talloc_free(tdb_data.dptr);
300 add a record to the database
302 static int ltdb_add(struct ldb_module *module, const struct ldb_message *msg)
304 struct ltdb_private *ltdb = module->private_data;
307 ltdb->last_err_string = NULL;
309 ret = ltdb_check_special_dn(module, msg);
314 if (ltdb_lock(module, LDBLOCK) != 0) {
318 if (ltdb_cache_load(module) != 0) {
319 ltdb_unlock(module, LDBLOCK);
323 ret = ltdb_store(module, msg, TDB_INSERT);
326 ltdb_modified(module, msg->dn);
329 ltdb_unlock(module, LDBLOCK);
335 delete a record from the database, not updating indexes (used for deleting
338 int ltdb_delete_noindex(struct ldb_module *module, const char *dn)
340 struct ltdb_private *ltdb = module->private_data;
344 tdb_key = ltdb_key(module, dn);
349 ret = tdb_delete(ltdb->tdb, tdb_key);
350 talloc_free(tdb_key.dptr);
356 delete a record from the database
358 static int ltdb_delete(struct ldb_module *module, const char *dn)
360 struct ltdb_private *ltdb = module->private_data;
362 struct ldb_message *msg = NULL;
364 ltdb->last_err_string = NULL;
366 if (ltdb_lock(module, LDBLOCK) != 0) {
370 if (ltdb_cache_load(module) != 0) {
374 msg = talloc(module, struct ldb_message);
379 /* in case any attribute of the message was indexed, we need
380 to fetch the old record */
381 ret = ltdb_search_dn1(module, dn, msg);
383 /* not finding the old record is an error */
387 ret = ltdb_delete_noindex(module, dn);
392 /* remove any indexed attributes */
393 ret = ltdb_index_del(module, msg);
396 ltdb_modified(module, dn);
400 ltdb_unlock(module, LDBLOCK);
405 ltdb_unlock(module, LDBLOCK);
411 find an element by attribute name. At the moment this does a linear search, it should
412 be re-coded to use a binary search once all places that modify records guarantee
415 return the index of the first matching element if found, otherwise -1
417 static int find_element(const struct ldb_message *msg, const char *name)
420 for (i=0;i<msg->num_elements;i++) {
421 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
430 add an element to an existing record. Assumes a elements array that we
431 can call re-alloc on, and assumed that we can re-use the data pointers from the
432 passed in additional values. Use with care!
434 returns 0 on success, -1 on failure (and sets errno)
436 static int msg_add_element(struct ldb_context *ldb,
437 struct ldb_message *msg, struct ldb_message_element *el)
439 struct ldb_message_element *e2;
442 e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
443 msg->num_elements+1);
451 e2 = &msg->elements[msg->num_elements];
454 e2->flags = el->flags;
456 if (el->num_values != 0) {
457 e2->values = talloc_array(msg->elements, struct ldb_val, el->num_values);
463 for (i=0;i<el->num_values;i++) {
464 e2->values[i] = el->values[i];
466 e2->num_values = el->num_values;
474 delete all elements having a specified attribute name
476 static int msg_delete_attribute(struct ldb_module *module,
477 struct ldb_context *ldb,
478 struct ldb_message *msg, const char *name)
482 for (i=0;i<msg->num_elements;i++) {
483 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
484 for (j=0;j<msg->elements[i].num_values;j++) {
485 ltdb_index_del_value(module, msg->dn, &msg->elements[i], j);
487 talloc_free(msg->elements[i].values);
488 if (msg->num_elements > (i+1)) {
489 memmove(&msg->elements[i],
491 sizeof(struct ldb_message_element)*
492 (msg->num_elements - (i+1)));
496 msg->elements = talloc_realloc(msg, msg->elements,
497 struct ldb_message_element,
506 delete all elements matching an attribute name/value
508 return 0 on success, -1 on failure
510 static int msg_delete_element(struct ldb_module *module,
511 struct ldb_message *msg,
513 const struct ldb_val *val)
515 struct ldb_context *ldb = module->ldb;
518 struct ldb_message_element *el;
519 const struct ldb_attrib_handler *h;
521 found = find_element(msg, name);
526 el = &msg->elements[found];
528 h = ldb_attrib_handler(ldb, el->name);
530 for (i=0;i<el->num_values;i++) {
531 if (h->comparison_fn(ldb, ldb, &el->values[i], val) == 0) {
532 if (i<el->num_values-1) {
533 memmove(&el->values[i], &el->values[i+1],
534 sizeof(el->values[i])*(el->num_values-(i+1)));
537 if (el->num_values == 0) {
538 return msg_delete_attribute(module, ldb, msg, name);
549 modify a record - internal interface
551 yuck - this is O(n^2). Luckily n is usually small so we probably
552 get away with it, but if we ever have really large attribute lists
553 then we'll need to look at this again
555 int ltdb_modify_internal(struct ldb_module *module, const struct ldb_message *msg)
557 struct ldb_context *ldb = module->ldb;
558 struct ltdb_private *ltdb = module->private_data;
559 TDB_DATA tdb_key, tdb_data;
560 struct ldb_message *msg2;
564 tdb_key = ltdb_key(module, msg->dn);
569 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
570 if (!tdb_data.dptr) {
571 talloc_free(tdb_key.dptr);
575 msg2 = talloc(tdb_key.dptr, struct ldb_message);
577 talloc_free(tdb_key.dptr);
581 ret = ltdb_unpack_data(module, &tdb_data, msg2);
583 talloc_free(tdb_key.dptr);
592 for (i=0;i<msg->num_elements;i++) {
593 struct ldb_message_element *el = &msg->elements[i];
594 struct ldb_message_element *el2;
595 struct ldb_val *vals;
597 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
599 case LDB_FLAG_MOD_ADD:
600 /* add this element to the message. fail if it
602 ret = find_element(msg2, el->name);
605 if (msg_add_element(ldb, msg2, el) != 0) {
611 el2 = &msg2->elements[ret];
613 /* An attribute with this name already exists, add all
614 * values if they don't already exist. */
616 for (j=0;j<el->num_values;j++) {
617 if (ldb_msg_find_val(el2, &el->values[j])) {
618 ltdb->last_err_string =
619 "Type or value exists";
624 vals = talloc_realloc(msg2->elements, el2->values, struct ldb_val,
625 el2->num_values + el->num_values);
630 for (j=0;j<el->num_values;j++) {
631 vals[el2->num_values + j] =
632 ldb_val_dup(vals, &el->values[j]);
636 el2->num_values += el->num_values;
640 case LDB_FLAG_MOD_REPLACE:
641 /* replace all elements of this attribute name with the elements
642 listed. The attribute not existing is not an error */
643 msg_delete_attribute(module, ldb, msg2, msg->elements[i].name);
645 /* add the replacement element, if not empty */
646 if (msg->elements[i].num_values != 0 &&
647 msg_add_element(ldb, msg2, &msg->elements[i]) != 0) {
652 case LDB_FLAG_MOD_DELETE:
653 /* we could be being asked to delete all
654 values or just some values */
655 if (msg->elements[i].num_values == 0) {
656 if (msg_delete_attribute(module, ldb, msg2,
657 msg->elements[i].name) != 0) {
658 ltdb->last_err_string = "No such attribute";
663 for (j=0;j<msg->elements[i].num_values;j++) {
664 if (msg_delete_element(module,
666 msg->elements[i].name,
667 &msg->elements[i].values[j]) != 0) {
668 ltdb->last_err_string = "No such attribute";
671 if (ltdb_index_del_value(module, msg->dn, &msg->elements[i], j) != 0) {
677 ltdb->last_err_string = "Invalid ldb_modify flags";
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 if (ltdb_cache_load(module) != 0) {
747 ltdb_unlock(module, LDBLOCK);
751 msg = talloc(module, struct ldb_message);
756 /* in case any attribute of the message was indexed, we need
757 to fetch the old record */
758 ret = ltdb_search_dn1(module, olddn, msg);
760 /* not finding the old record is an error */
764 msg->dn = talloc_strdup(msg, newdn);
769 ret = ltdb_add(module, msg);
774 ret = ltdb_delete(module, olddn);
775 error_str = ltdb->last_err_string;
777 ltdb_delete(module, newdn);
780 ltdb->last_err_string = error_str;
783 ltdb_unlock(module, LDBLOCK);
789 ltdb_unlock(module, LDBLOCK);
795 return extended error information
797 static const char *ltdb_errstring(struct ldb_module *module)
799 struct ltdb_private *ltdb = module->private_data;
800 if (ltdb->last_err_string) {
801 return ltdb->last_err_string;
803 return tdb_errorstr(ltdb->tdb);
807 static const struct ldb_module_ops ltdb_ops = {
809 .search = ltdb_search,
810 .search_bytree = ltdb_search_bytree,
811 .add_record = ltdb_add,
812 .modify_record = ltdb_modify,
813 .delete_record = ltdb_delete,
814 .rename_record = ltdb_rename,
815 .named_lock = ltdb_lock,
816 .named_unlock = ltdb_unlock,
817 .errstring = ltdb_errstring
822 connect to the database
824 int ltdb_connect(struct ldb_context *ldb, const char *url,
825 unsigned int flags, const char *options[])
828 int tdb_flags, open_flags;
829 struct ltdb_private *ltdb;
832 if (strchr(url, ':')) {
833 if (strncmp(url, "tdb://", 6) != 0) {
834 ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid tdb URL '%s'", url);
842 tdb_flags = TDB_DEFAULT;
844 if (flags & LDB_FLG_RDONLY) {
845 open_flags = O_RDONLY;
847 open_flags = O_CREAT | O_RDWR;
850 ltdb = talloc_zero(ldb, struct ltdb_private);
856 /* note that we use quite a large default hash size */
857 ltdb->tdb = ltdb_wrap_open(ltdb, path, 10000, tdb_flags, open_flags, 0666);
859 ldb_debug(ldb, LDB_DEBUG_ERROR, "Unable to open tdb '%s'\n", path);
864 ltdb->sequence_number = 0;
866 ldb->modules = talloc(ldb, struct ldb_module);
872 ldb->modules->ldb = ldb;
873 ldb->modules->prev = ldb->modules->next = NULL;
874 ldb->modules->private_data = ltdb;
875 ldb->modules->ops = <db_ops;