s4-ldb: added an environment varibale LDB_WARN_UNINDEXED
[samba.git] / source4 / lib / ldb / ldb_tdb / ldb_tdb.c
index cf625385cb4354398cf7222eb7117e5eb48e0056..066d4085bbacff3466b209605351a503bb2a47da 100644 (file)
@@ -4,7 +4,7 @@
    Copyright (C) Andrew Tridgell 2004
    Copyright (C) Stefan Metzmacher 2004
    Copyright (C) Simo Sorce 2006-2008
-   Copyright (C) Matthias Dieter Wallnöfer 2009
+   Copyright (C) Matthias Dieter Wallnöfer 2009-2010
 
      ** NOTE! The following LGPL license applies to the ldb
      ** library. This does NOT imply that all of Samba is released
@@ -77,6 +77,8 @@ int ltdb_err_map(enum TDB_ERROR tdb_code)
                return LDB_ERR_NO_SUCH_OBJECT;
        case TDB_ERR_RDONLY:
                return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
+       default:
+               break;
        }
        return LDB_ERR_OTHER;
 }
@@ -173,7 +175,7 @@ failed:
   currently only @ATTRIBUTES is checked
 */
 static int ltdb_check_special_dn(struct ldb_module *module,
-                         const struct ldb_message *msg)
+                                const struct ldb_message *msg)
 {
        struct ldb_context *ldb = ldb_module_get_ctx(module);
        unsigned int i, j;
@@ -281,25 +283,17 @@ static int ltdb_add_internal(struct ldb_module *module,
        int ret = LDB_SUCCESS;
        unsigned int i;
 
-       ret = ltdb_check_special_dn(module, msg);
-       if (ret != LDB_SUCCESS) {
-               return ret;
-       }
-
-       if (ltdb_cache_load(module) != 0) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
        for (i=0;i<msg->num_elements;i++) {
                struct ldb_message_element *el = &msg->elements[i];
                const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
 
                if (el->num_values == 0) {
-                       ldb_asprintf_errstring(ldb, "attribute %s on %s specified, but with 0 values (illegal)", 
+                       ldb_asprintf_errstring(ldb, "attribute '%s' on '%s' specified, but with 0 values (illegal)",
                                               el->name, ldb_dn_get_linearized(msg->dn));
                        return LDB_ERR_CONSTRAINT_VIOLATION;
                }
-               if (a && a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
+               if (a && (a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) &&
+                   !(el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
                        if (el->num_values > 1) {
                                ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
                                                       el->name, ldb_dn_get_linearized(msg->dn));
@@ -337,6 +331,11 @@ static int ltdb_add(struct ltdb_context *ctx)
        struct ldb_request *req = ctx->req;
        int ret = LDB_SUCCESS;
 
+       ret = ltdb_check_special_dn(module, req->op.add.message);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+
        ldb_request_set_state(req, LDB_ASYNC_PENDING);
 
        if (ltdb_cache_load(module) != 0) {
@@ -379,7 +378,7 @@ static int ltdb_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
        struct ldb_message *msg;
        int ret = LDB_SUCCESS;
 
-       msg = talloc(module, struct ldb_message);
+       msg = ldb_msg_new(module);
        if (msg == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
@@ -554,13 +553,22 @@ static int msg_delete_element(struct ldb_module *module,
                return LDB_ERR_NO_SUCH_ATTRIBUTE;
        }
 
-       el = &msg->elements[found];
+       i = (unsigned int) found;
+       el = &(msg->elements[i]);
 
        a = ldb_schema_attribute_by_name(ldb, el->name);
 
        for (i=0;i<el->num_values;i++) {
-               if (a->syntax->comparison_fn(ldb, ldb,
-                                            &el->values[i], val) == 0) {
+               bool matched;
+               if (a->syntax->operator_fn) {
+                       ret = a->syntax->operator_fn(ldb, LDB_OP_EQUALITY, a,
+                                                    &el->values[i], val, &matched);
+                       if (ret != LDB_SUCCESS) return ret;
+               } else {
+                       matched = (a->syntax->comparison_fn(ldb, ldb,
+                                                           &el->values[i], val) == 0);
+               }
+               if (matched) {
                        if (el->num_values == 1) {
                                return msg_delete_attribute(module, ldb, msg, name);
                        }
@@ -606,7 +614,7 @@ int ltdb_modify_internal(struct ldb_module *module,
        struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
        TDB_DATA tdb_key, tdb_data;
        struct ldb_message *msg2;
-       unsigned i, j, k;
+       unsigned int i, j, k;
        int ret = LDB_SUCCESS, idx;
        struct ldb_control *control_permissive = NULL;
 
@@ -626,7 +634,7 @@ int ltdb_modify_internal(struct ldb_module *module,
                return ltdb_err_map(tdb_error(ltdb->tdb));
        }
 
-       msg2 = talloc(tdb_key.dptr, struct ldb_message);
+       msg2 = ldb_msg_new(tdb_key.dptr);
        if (msg2 == NULL) {
                free(tdb_data.dptr);
                ret = LDB_ERR_OTHER;
@@ -650,18 +658,12 @@ int ltdb_modify_internal(struct ldb_module *module,
                const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
                const char *dn;
 
-               if (ldb_attr_cmp(el->name, "distinguishedName") == 0) {
-                       ldb_asprintf_errstring(ldb, "it is not permitted to perform a modify on 'distinguishedName' (use rename instead): %s",
-                                              ldb_dn_get_linearized(msg2->dn));
-                       ret = LDB_ERR_CONSTRAINT_VIOLATION;
-                       goto done;
-               }
-
                switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
                case LDB_FLAG_MOD_ADD:
 
                        if (el->num_values == 0) {
-                               ldb_asprintf_errstring(ldb, "attribute %s on %s specified, but with 0 values (illigal)",
+                               ldb_asprintf_errstring(ldb,
+                                                      "attribute '%s': attribute on '%s' specified, but with 0 values (illegal)",
                                                       el->name, ldb_dn_get_linearized(msg2->dn));
                                ret = LDB_ERR_CONSTRAINT_VIOLATION;
                                goto done;
@@ -690,7 +692,8 @@ int ltdb_modify_internal(struct ldb_module *module,
                                }
                        }
 
-                       if (a && a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
+                       if (a && (a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) &&
+                           !(el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
                                if (el->num_values > 1) {
                                        ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
                                                               el->name, ldb_dn_get_linearized(msg2->dn));
@@ -706,22 +709,25 @@ int ltdb_modify_internal(struct ldb_module *module,
                                        ret = LDB_ERR_OTHER;
                                        goto done;
                                }
-                               ret = ltdb_index_add_element(module, msg2->dn, el);
+                               ret = ltdb_index_add_element(module, msg2->dn,
+                                                            el);
                                if (ret != LDB_SUCCESS) {
                                        goto done;
                                }
                        } else {
+                               j = (unsigned int) idx;
+                               el2 = &(msg2->elements[j]);
+
                                /* We cannot add another value on a existing one
                                   if the attribute is single-valued */
-                               if (a && a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
+                               if (a && (a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) &&
+                                   !(el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
                                        ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
                                                               el->name, ldb_dn_get_linearized(msg2->dn));
                                        ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
                                        goto done;
                                }
 
-                               el2 = &(msg2->elements[idx]);
-
                                /* Check that values don't exist yet on multi-
                                   valued attributes or aren't provided twice */
                                for (j = 0; j < el->num_values; j++) {
@@ -737,12 +743,16 @@ int ltdb_modify_internal(struct ldb_module *module,
                                                        continue;
                                                }
 
-                                               ldb_asprintf_errstring(ldb, "%s: value #%d already exists", el->name, j);
+                                               ldb_asprintf_errstring(ldb,
+                                                                      "attribute '%s': value #%u on '%s' already exists",
+                                                                      el->name, j, ldb_dn_get_linearized(msg2->dn));
                                                ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
                                                goto done;
                                        }
                                        if (ldb_msg_find_val(el, &el->values[j]) != &el->values[j]) {
-                                               ldb_asprintf_errstring(ldb, "%s: value #%d provided more than once", el->name, j);
+                                               ldb_asprintf_errstring(ldb,
+                                                                      "attribute '%s': value #%u on '%s' provided more than once",
+                                                                      el->name, j, ldb_dn_get_linearized(msg2->dn));
                                                ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
                                                goto done;
                                        }
@@ -777,16 +787,9 @@ int ltdb_modify_internal(struct ldb_module *module,
 
                case LDB_FLAG_MOD_REPLACE:
 
-                       if (a && a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
-                               /* the RELAX control overrides this
-                                  check for replace. This is needed as
-                                  DRS replication can produce multiple
-                                  values here for a single valued
-                                  attribute when the values are deleted
-                                  links
-                               */
-                               if (el->num_values > 1 &&
-                                   (!req || !ldb_request_get_control(req, LDB_CONTROL_RELAX_OID))) {
+                       if (a && (a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) &&
+                           !(el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
+                               if (el->num_values > 1) {
                                        ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
                                                               el->name, ldb_dn_get_linearized(msg2->dn));
                                        ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
@@ -797,22 +800,27 @@ int ltdb_modify_internal(struct ldb_module *module,
                        /* TODO: This is O(n^2) - replace with more efficient check */
                        for (j=0; j<el->num_values; j++) {
                                if (ldb_msg_find_val(el, &el->values[j]) != &el->values[j]) {
-                                       ldb_asprintf_errstring(ldb, "%s: value #%d provided more than once", el->name, j);
+                                       ldb_asprintf_errstring(ldb,
+                                                              "attribute '%s': value #%u on '%s' provided more than once",
+                                                              el->name, j, ldb_dn_get_linearized(msg2->dn));
                                        ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
                                        goto done;
                                }
                        }
 
+                       /* Checks if element already exists */
                        idx = find_element(msg2, el->name);
                        if (idx != -1) {
-                               el2 = &(msg2->elements[idx]);
+                               j = (unsigned int) idx;
+                               el2 = &(msg2->elements[j]);
                                if (ldb_msg_element_compare(el, el2) == 0) {
                                        /* we are replacing with the same values */
                                        continue;
                                }
                        
                                /* Delete the attribute if it exists in the DB */
-                               if (msg_delete_attribute(module, ldb, msg2, el->name) != 0) {
+                               if (msg_delete_attribute(module, ldb, msg2,
+                                                        el->name) != 0) {
                                        ret = LDB_ERR_OTHER;
                                        goto done;
                                }
@@ -846,7 +854,8 @@ int ltdb_modify_internal(struct ldb_module *module,
                                    control_permissive) {
                                        ret = LDB_SUCCESS;
                                } else {
-                                       ldb_asprintf_errstring(ldb, "No such attribute: %s for delete on %s",
+                                       ldb_asprintf_errstring(ldb,
+                                                              "attribute '%s': no such attribute for delete on '%s'",
                                                               msg->elements[i].name, dn);
                                }
                                if (ret != LDB_SUCCESS) {
@@ -863,7 +872,8 @@ int ltdb_modify_internal(struct ldb_module *module,
                                            control_permissive) {
                                                ret = LDB_SUCCESS;
                                        } else {
-                                               ldb_asprintf_errstring(ldb, "No matching attribute value when deleting attribute: %s on %s",
+                                               ldb_asprintf_errstring(ldb,
+                                                                      "attribute '%s': no matching attribute value while deleting attribute on '%s'",
                                                                       msg->elements[i].name, dn);
                                        }
                                        if (ret != LDB_SUCCESS) {
@@ -874,9 +884,9 @@ int ltdb_modify_internal(struct ldb_module *module,
                        break;
                default:
                        ldb_asprintf_errstring(ldb,
-                               "Invalid ldb_modify flags on %s: 0x%x",
-                               msg->elements[i].name,
-                               msg->elements[i].flags & LDB_FLAG_MOD_MASK);
+                                              "attribute '%s': invalid modify flags on '%s': 0x%x",
+                                              msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
+                                              msg->elements[i].flags & LDB_FLAG_MOD_MASK);
                        ret = LDB_ERR_PROTOCOL_ERROR;
                        goto done;
                }
@@ -938,7 +948,7 @@ static int ltdb_rename(struct ltdb_context *ctx)
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       msg = talloc(ctx, struct ldb_message);
+       msg = ldb_msg_new(ctx);
        if (msg == NULL) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
@@ -1096,7 +1106,7 @@ static int ltdb_sequence_number(struct ltdb_context *ctx,
 
        dn = ldb_dn_new(tmp_ctx, ldb, LTDB_BASEINFO);
 
-       msg = talloc(tmp_ctx, struct ldb_message);
+       msg = ldb_msg_new(tmp_ctx);
        if (msg == NULL) {
                ret = LDB_ERR_OPERATIONS_ERROR;
                goto done;
@@ -1267,7 +1277,7 @@ static void ltdb_callback(struct tevent_context *ev,
                goto done;
        default:
                /* no other op supported */
-               ret = LDB_ERR_UNWILLING_TO_PERFORM;
+               ret = LDB_ERR_PROTOCOL_ERROR;
        }
 
        if (!ctx->request_terminated) {
@@ -1303,7 +1313,7 @@ static int ltdb_handle_request(struct ldb_module *module,
        struct ltdb_context *ac;
        struct tevent_timer *te;
        struct timeval tv;
-       int i;
+       unsigned int i;
 
        ldb = ldb_module_get_ctx(module);
 
@@ -1373,10 +1383,7 @@ static int ltdb_init_rootdse(struct ldb_module *module)
 
        ret = ldb_mod_register_control(module,
                                       LDB_CONTROL_PERMISSIVE_MODIFY_OID);
-       if (ret != LDB_SUCCESS) {
-               ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_tdb: "
-                         "Unable to register control with rootdse!");
-       }
+       /* ignore errors on this - we expect it for non-sam databases */
 
        /* there can be no module beyond the backend, just return */
        return LDB_SUCCESS;
@@ -1456,6 +1463,10 @@ static int ltdb_connect(struct ldb_context *ldb, const char *url,
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
+       if (getenv("LDB_WARN_UNINDEXED")) {
+               ltdb->warn_unindexed = true;
+       }
+
        ltdb->sequence_number = 0;
 
        module = ldb_module_new(ldb, ldb, "ldb_tdb backend", &ltdb_ops);
@@ -1476,7 +1487,8 @@ static int ltdb_connect(struct ldb_context *ldb, const char *url,
        return LDB_SUCCESS;
 }
 
-const struct ldb_backend_ops ldb_tdb_backend_ops = {
-       .name = "tdb",
-       .connect_fn = ltdb_connect,
-};
+int ldb_tdb_init(const char *version)
+{
+       LDB_MODULE_CHECK_VERSION(version);
+       return ldb_register_backend("tdb", ltdb_connect, false);
+}