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_errors.h"
41 #include "ldb/include/ldb_private.h"
42 #include "ldb/ldb_tdb/ldb_tdb.h"
46 map a tdb error code to a ldb error code
48 static int ltdb_err_map(enum TDB_ERROR tdb_code)
56 return LDB_ERR_OPERATIONS_ERROR;
58 return LDB_ERR_PROTOCOL_ERROR;
62 case TDB_ERR_LOCK_TIMEOUT:
63 return LDB_ERR_TIME_LIMIT_EXCEEDED;
65 return LDB_ERR_ENTRY_ALREADY_EXISTS;
67 return LDB_ERR_NO_SUCH_OBJECT;
69 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
76 form a TDB_DATA for a record key
79 note that the key for a record can depend on whether the
80 dn refers to a case sensitive index record or not
82 struct TDB_DATA ltdb_key(struct ldb_module *module, const struct ldb_dn *dn)
84 struct ldb_context *ldb = module->ldb;
87 char *dn_folded = NULL;
90 most DNs are case insensitive. The exception is index DNs for
91 case sensitive attributes
93 there are 3 cases dealt with in this code:
95 1) if the dn doesn't start with @ then uppercase the attribute
96 names and the attributes values of case insensitive attributes
97 2) if the dn starts with @ then leave it alone - the indexing code handles
101 dn_folded = ldb_dn_linearize_casefold(ldb, dn);
106 key_str = talloc_asprintf(ldb, "DN=%s", dn_folded);
108 talloc_free(dn_folded);
114 key.dptr = (uint8_t *)key_str;
115 key.dsize = strlen(key_str) + 1;
127 check special dn's have valid attributes
128 currently only @ATTRIBUTES is checked
130 int ltdb_check_special_dn(struct ldb_module *module, const struct ldb_message *msg)
134 if (! ldb_dn_is_special(msg->dn) ||
135 ! ldb_dn_check_special(msg->dn, LTDB_ATTRIBUTES)) {
139 /* we have @ATTRIBUTES, let's check attributes are fine */
140 /* should we check that we deny multivalued attributes ? */
141 for (i = 0; i < msg->num_elements; i++) {
142 for (j = 0; j < msg->elements[i].num_values; j++) {
143 if (ltdb_check_at_attributes_values(&msg->elements[i].values[j]) != 0) {
144 char *err_string = talloc_strdup(module, "Invalid attribute value in an @ATTRIBUTES entry");
146 ldb_set_errstring(module, err_string);
148 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
158 we've made a modification to a dn - possibly reindex and
159 update sequence number
161 static int ltdb_modified(struct ldb_module *module, const struct ldb_dn *dn)
165 if (ldb_dn_is_special(dn) &&
166 (ldb_dn_check_special(dn, LTDB_INDEXLIST) ||
167 ldb_dn_check_special(dn, LTDB_ATTRIBUTES)) ) {
168 ret = ltdb_reindex(module);
172 !(ldb_dn_is_special(dn) &&
173 ldb_dn_check_special(dn, LTDB_BASEINFO)) ) {
174 ret = ltdb_increase_sequence_number(module);
181 store a record into the db
183 int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs)
185 struct ltdb_private *ltdb = module->private_data;
186 TDB_DATA tdb_key, tdb_data;
189 tdb_key = ltdb_key(module, msg->dn);
191 return LDB_ERR_OTHER;
194 ret = ltdb_pack_data(module, msg, &tdb_data);
196 talloc_free(tdb_key.dptr);
197 return LDB_ERR_OTHER;
200 ret = tdb_store(ltdb->tdb, tdb_key, tdb_data, flgs);
202 ret = ltdb_err_map(tdb_error(ltdb->tdb));
206 ret = ltdb_index_add(module, msg);
208 tdb_delete(ltdb->tdb, tdb_key);
212 talloc_free(tdb_key.dptr);
213 talloc_free(tdb_data.dptr);
220 add a record to the database
222 static int ltdb_add(struct ldb_module *module, const struct ldb_message *msg)
226 ret = ltdb_check_special_dn(module, msg);
227 if (ret != LDB_SUCCESS) {
231 if (ltdb_cache_load(module) != 0) {
232 return LDB_ERR_OTHER;
235 ret = ltdb_store(module, msg, TDB_INSERT);
237 if (ret == LDB_SUCCESS) {
238 ltdb_modified(module, msg->dn);
246 delete a record from the database, not updating indexes (used for deleting
249 int ltdb_delete_noindex(struct ldb_module *module, const struct ldb_dn *dn)
251 struct ltdb_private *ltdb = module->private_data;
255 tdb_key = ltdb_key(module, dn);
257 return LDB_ERR_OTHER;
260 ret = tdb_delete(ltdb->tdb, tdb_key);
261 talloc_free(tdb_key.dptr);
264 ret = ltdb_err_map(tdb_error(ltdb->tdb));
271 delete a record from the database
273 static int ltdb_delete(struct ldb_module *module, const struct ldb_dn *dn)
275 struct ldb_message *msg = NULL;
276 int ret = LDB_ERR_OTHER;
278 if (ltdb_cache_load(module) != 0) {
282 msg = talloc(module, struct ldb_message);
287 /* in case any attribute of the message was indexed, we need
288 to fetch the old record */
289 ret = ltdb_search_dn1(module, dn, msg);
291 /* not finding the old record is an error */
292 ret = LDB_ERR_NO_SUCH_OBJECT;
296 ret = ltdb_delete_noindex(module, dn);
297 if (ret != LDB_SUCCESS) {
301 /* remove any indexed attributes */
302 ret = ltdb_index_del(module, msg);
303 if (ret == LDB_SUCCESS) {
304 ltdb_modified(module, dn);
318 find an element by attribute name. At the moment this does a linear search, it should
319 be re-coded to use a binary search once all places that modify records guarantee
322 return the index of the first matching element if found, otherwise -1
324 static int find_element(const struct ldb_message *msg, const char *name)
327 for (i=0;i<msg->num_elements;i++) {
328 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
337 add an element to an existing record. Assumes a elements array that we
338 can call re-alloc on, and assumed that we can re-use the data pointers from the
339 passed in additional values. Use with care!
341 returns 0 on success, -1 on failure (and sets errno)
343 static int msg_add_element(struct ldb_context *ldb,
344 struct ldb_message *msg, struct ldb_message_element *el)
346 struct ldb_message_element *e2;
349 e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
350 msg->num_elements+1);
358 e2 = &msg->elements[msg->num_elements];
361 e2->flags = el->flags;
363 if (el->num_values != 0) {
364 e2->values = talloc_array(msg->elements, struct ldb_val, el->num_values);
370 for (i=0;i<el->num_values;i++) {
371 e2->values[i] = el->values[i];
373 e2->num_values = el->num_values;
381 delete all elements having a specified attribute name
383 static int msg_delete_attribute(struct ldb_module *module,
384 struct ldb_context *ldb,
385 struct ldb_message *msg, const char *name)
390 dn = ldb_dn_linearize(ldb, msg->dn);
395 for (i=0;i<msg->num_elements;i++) {
396 if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
397 for (j=0;j<msg->elements[i].num_values;j++) {
398 ltdb_index_del_value(module, dn, &msg->elements[i], j);
400 talloc_free(msg->elements[i].values);
401 if (msg->num_elements > (i+1)) {
402 memmove(&msg->elements[i],
404 sizeof(struct ldb_message_element)*
405 (msg->num_elements - (i+1)));
409 msg->elements = talloc_realloc(msg, msg->elements,
410 struct ldb_message_element,
420 delete all elements matching an attribute name/value
422 return 0 on success, -1 on failure
424 static int msg_delete_element(struct ldb_module *module,
425 struct ldb_message *msg,
427 const struct ldb_val *val)
429 struct ldb_context *ldb = module->ldb;
432 struct ldb_message_element *el;
433 const struct ldb_attrib_handler *h;
435 found = find_element(msg, name);
440 el = &msg->elements[found];
442 h = ldb_attrib_handler(ldb, el->name);
444 for (i=0;i<el->num_values;i++) {
445 if (h->comparison_fn(ldb, ldb, &el->values[i], val) == 0) {
446 if (i<el->num_values-1) {
447 memmove(&el->values[i], &el->values[i+1],
448 sizeof(el->values[i])*(el->num_values-(i+1)));
451 if (el->num_values == 0) {
452 return msg_delete_attribute(module, ldb, msg, name);
463 modify a record - internal interface
465 yuck - this is O(n^2). Luckily n is usually small so we probably
466 get away with it, but if we ever have really large attribute lists
467 then we'll need to look at this again
469 int ltdb_modify_internal(struct ldb_module *module, const struct ldb_message *msg)
471 struct ldb_context *ldb = module->ldb;
472 struct ltdb_private *ltdb = module->private_data;
473 TDB_DATA tdb_key, tdb_data;
474 struct ldb_message *msg2;
478 tdb_key = ltdb_key(module, msg->dn);
480 return LDB_ERR_OTHER;
483 tdb_data = tdb_fetch(ltdb->tdb, tdb_key);
484 if (!tdb_data.dptr) {
485 talloc_free(tdb_key.dptr);
486 return ltdb_err_map(tdb_error(ltdb->tdb));
489 msg2 = talloc(tdb_key.dptr, struct ldb_message);
491 talloc_free(tdb_key.dptr);
492 return LDB_ERR_OTHER;
495 ret = ltdb_unpack_data(module, &tdb_data, msg2);
497 talloc_free(tdb_key.dptr);
499 return LDB_ERR_OTHER;
506 for (i=0;i<msg->num_elements;i++) {
507 struct ldb_message_element *el = &msg->elements[i];
508 struct ldb_message_element *el2;
509 struct ldb_val *vals;
513 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
515 case LDB_FLAG_MOD_ADD:
516 /* add this element to the message. fail if it
518 ret = find_element(msg2, el->name);
521 if (msg_add_element(ldb, msg2, el) != 0) {
528 el2 = &msg2->elements[ret];
530 /* An attribute with this name already exists, add all
531 * values if they don't already exist. */
533 for (j=0;j<el->num_values;j++) {
534 if (ldb_msg_find_val(el2, &el->values[j])) {
535 err_string = talloc_strdup(module, "Type or value exists");
536 if (err_string) ldb_set_errstring(module, err_string);
537 ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
542 vals = talloc_realloc(msg2->elements, el2->values, struct ldb_val,
543 el2->num_values + el->num_values);
548 for (j=0;j<el->num_values;j++) {
549 vals[el2->num_values + j] =
550 ldb_val_dup(vals, &el->values[j]);
554 el2->num_values += el->num_values;
558 case LDB_FLAG_MOD_REPLACE:
559 /* replace all elements of this attribute name with the elements
560 listed. The attribute not existing is not an error */
561 msg_delete_attribute(module, ldb, msg2, msg->elements[i].name);
563 /* add the replacement element, if not empty */
564 if (msg->elements[i].num_values != 0 &&
565 msg_add_element(ldb, msg2, &msg->elements[i]) != 0) {
570 case LDB_FLAG_MOD_DELETE:
572 dn = ldb_dn_linearize(msg2, msg->dn);
573 if (dn == NULL) goto failed;
575 /* we could be being asked to delete all
576 values or just some values */
577 if (msg->elements[i].num_values == 0) {
578 if (msg_delete_attribute(module, ldb, msg2,
579 msg->elements[i].name) != 0) {
580 err_string = talloc_strdup(module, "No such attribute");
581 if (err_string) ldb_set_errstring(module, err_string);
582 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
587 for (j=0;j<msg->elements[i].num_values;j++) {
588 if (msg_delete_element(module,
590 msg->elements[i].name,
591 &msg->elements[i].values[j]) != 0) {
592 err_string = talloc_strdup(module, "No such attribute");
593 if (err_string) ldb_set_errstring(module, err_string);
594 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
597 if (ltdb_index_del_value(module, dn, &msg->elements[i], j) != 0) {
603 err_string = talloc_strdup(module, "Invalid ldb_modify flags");
604 if (err_string) ldb_set_errstring(module, err_string);
605 ret = LDB_ERR_PROTOCOL_ERROR;
610 /* we've made all the mods - save the modified record back into the database */
611 ret = ltdb_store(module, msg2, TDB_MODIFY);
613 talloc_free(tdb_key.dptr);
618 talloc_free(tdb_key.dptr);
626 static int ltdb_modify(struct ldb_module *module, const struct ldb_message *msg)
630 ret = ltdb_check_special_dn(module, msg);
635 if (ltdb_cache_load(module) != 0) {
639 ret = ltdb_modify_internal(module, msg);
641 if (ret == LDB_SUCCESS) {
642 ltdb_modified(module, msg->dn);
651 static int ltdb_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
653 struct ldb_message *msg;
655 int ret = LDB_ERR_OTHER;
657 if (ltdb_cache_load(module) != 0) {
661 msg = talloc(module, struct ldb_message);
666 /* in case any attribute of the message was indexed, we need
667 to fetch the old record */
668 ret = ltdb_search_dn1(module, olddn, msg);
670 /* not finding the old record is an error */
671 ret = LDB_ERR_NO_SUCH_OBJECT;
675 msg->dn = ldb_dn_copy(msg, newdn);
681 ret = ltdb_add(module, msg);
682 if (ret != LDB_SUCCESS) {
686 ret = ltdb_delete(module, olddn);
687 error_str = talloc_strdup(module, ldb_errstring(module->ldb));
688 if (ret != LDB_SUCCESS) {
689 ltdb_delete(module, newdn);
692 ldb_set_errstring(module, error_str);
703 static int ltdb_start_trans(struct ldb_module *module)
705 struct ltdb_private *ltdb = module->private_data;
707 if (tdb_transaction_start(ltdb->tdb) != 0) {
708 return ltdb_err_map(tdb_error(ltdb->tdb));
714 static int ltdb_end_trans(struct ldb_module *module)
716 struct ltdb_private *ltdb = module->private_data;
718 if (tdb_transaction_commit(ltdb->tdb) != 0) {
719 return ltdb_err_map(tdb_error(ltdb->tdb));
725 static int ltdb_del_trans(struct ldb_module *module)
727 struct ltdb_private *ltdb = module->private_data;
729 if (tdb_transaction_cancel(ltdb->tdb) != 0) {
730 return ltdb_err_map(tdb_error(ltdb->tdb));
736 static int ltdb_request(struct ldb_module *module, struct ldb_request *req)
738 switch (req->operation) {
741 return ltdb_search_bytree(module,
743 req->op.search.scope,
745 req->op.search.attrs,
746 &req->op.search.res);
749 return ltdb_add(module, req->op.add.message);
752 return ltdb_modify(module, req->op.mod.message);
755 return ltdb_delete(module, req->op.del.dn);
758 return ltdb_rename(module,
759 req->op.rename.olddn,
760 req->op.rename.newdn);
763 return LDB_ERR_OPERATIONS_ERROR;
768 static const struct ldb_module_ops ltdb_ops = {
770 .request = ltdb_request,
771 .start_transaction = ltdb_start_trans,
772 .end_transaction = ltdb_end_trans,
773 .del_transaction = ltdb_del_trans
778 connect to the database
780 int ltdb_connect(struct ldb_context *ldb, const char *url,
781 unsigned int flags, const char *options[])
784 int tdb_flags, open_flags;
785 struct ltdb_private *ltdb;
788 if (strchr(url, ':')) {
789 if (strncmp(url, "tdb://", 6) != 0) {
790 ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid tdb URL '%s'", url);
798 tdb_flags = TDB_DEFAULT;
800 /* check for the 'nosync' option */
801 if (flags & LDB_FLG_NOSYNC) {
802 tdb_flags |= TDB_NOSYNC;
805 if (flags & LDB_FLG_RDONLY) {
806 open_flags = O_RDONLY;
808 open_flags = O_CREAT | O_RDWR;
811 ltdb = talloc_zero(ldb, struct ltdb_private);
817 /* note that we use quite a large default hash size */
818 ltdb->tdb = ltdb_wrap_open(ltdb, path, 10000, tdb_flags, open_flags, 0666);
820 ldb_debug(ldb, LDB_DEBUG_ERROR, "Unable to open tdb '%s'\n", path);
825 ltdb->sequence_number = 0;
827 ldb->modules = talloc(ldb, struct ldb_module);
833 ldb->modules->ldb = ldb;
834 ldb->modules->prev = ldb->modules->next = NULL;
835 ldb->modules->private_data = ltdb;
836 ldb->modules->ops = <db_ops;