s4-ldb: added an environment varibale LDB_WARN_UNINDEXED
[samba.git] / source4 / lib / ldb / ldb_tdb / ldb_tdb.c
index a146b96b2056393c203b3a4170725687a48f8c08..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
@@ -36,7 +36,7 @@
  *
  *  Modifications:
  *
- *  - description: make the module use asyncronous calls
+ *  - description: make the module use asynchronous calls
  *    date: Feb 2006
  *    Author: Simo Sorce
  *
@@ -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,10 +175,10 @@ 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);
-       int i, j;
+       unsigned int i, j;
 
        if (! ldb_dn_is_special(msg->dn) ||
            ! ldb_dn_check_special(msg->dn, LTDB_ATTRIBUTES)) {
@@ -230,7 +232,8 @@ static int ltdb_modified(struct ldb_module *module, struct ldb_dn *dn)
        }
 
        /* If the modify was to @OPTIONS, reload the cache */
-       if (ldb_dn_is_special(dn) &&
+       if (ret == LDB_SUCCESS &&
+           ldb_dn_is_special(dn) &&
            (ldb_dn_check_special(dn, LTDB_OPTIONS)) ) {
                ret = ltdb_cache_reload(module);
        }
@@ -277,27 +280,20 @@ static int ltdb_add_internal(struct ldb_module *module,
                             const struct ldb_message *msg)
 {
        struct ldb_context *ldb = ldb_module_get_ctx(module);
-       int ret = LDB_SUCCESS, 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;
-       }
+       int ret = LDB_SUCCESS;
+       unsigned int i;
 
        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));
@@ -335,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) {
@@ -377,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;
        }
@@ -511,7 +512,7 @@ static int msg_delete_attribute(struct ldb_module *module,
 
        el = ldb_msg_find_element(msg, name);
        if (el == NULL) {
-               return -1;
+               return LDB_ERR_NO_SUCH_ATTRIBUTE;
        }
        i = el - msg->elements;
 
@@ -528,13 +529,13 @@ static int msg_delete_attribute(struct ldb_module *module,
        msg->elements = talloc_realloc(msg, msg->elements,
                                       struct ldb_message_element,
                                       msg->num_elements);
-       return 0;
+       return LDB_SUCCESS;
 }
 
 /*
   delete all elements matching an attribute name/value
 
-  return 0 on success, -1 on failure
+  return LDB Error on failure
 */
 static int msg_delete_element(struct ldb_module *module,
                              struct ldb_message *msg,
@@ -549,23 +550,32 @@ static int msg_delete_element(struct ldb_module *module,
 
        found = find_element(msg, name);
        if (found == -1) {
-               return -1;
+               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);
                        }
 
                        ret = ltdb_index_del_value(module, msg->dn, el, i);
                        if (ret != LDB_SUCCESS) {
-                               return -1;
+                               return ret;
                        }
 
                        if (i<el->num_values-1) {
@@ -577,12 +587,12 @@ static int msg_delete_element(struct ldb_module *module,
 
                        /* per definition we find in a canonicalised message an
                           attribute value only once. So we are finished here */
-                       return 0;
+                       return LDB_SUCCESS;
                }
        }
 
        /* Not found */
-       return -1;
+       return LDB_ERR_NO_SUCH_ATTRIBUTE;
 }
 
 
@@ -604,8 +614,14 @@ 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;
+       unsigned int i, j, k;
        int ret = LDB_SUCCESS, idx;
+       struct ldb_control *control_permissive = NULL;
+
+       if (req) {
+               control_permissive = ldb_request_get_control(req,
+                                       LDB_CONTROL_PERMISSIVE_MODIFY_OID);
+       }
 
        tdb_key = ltdb_key(module, msg->dn);
        if (!tdb_key.dptr) {
@@ -618,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;
@@ -642,23 +658,42 @@ 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;
                        }
 
-                       if (a && a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
+                       /* make a copy of the array so that a permissive
+                        * control can remove duplicates without changing the
+                        * original values, but do not copy data as we do not
+                        * need to keep it around once the operation is
+                        * finished */
+                       if (control_permissive) {
+                               el = talloc(msg2, struct ldb_message_element);
+                               if (!el) {
+                                       ret = LDB_ERR_OTHER;
+                                       goto done;
+                               }
+                               el->name = msg->elements[i].name;
+                               el->num_values = msg->elements[i].num_values;
+                               el->values = talloc_array(el, struct ldb_val, el->num_values);
+                               if (el->values == NULL) {
+                                       ret = LDB_ERR_OTHER;
+                                       goto done;
+                               }
+                               for (j = 0; j < el->num_values; j++) {
+                                       el->values[j] = msg->elements[i].values[j];
+                               }
+                       }
+
+                       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));
@@ -674,32 +709,50 @@ 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++) {
+                               for (j = 0; j < el->num_values; j++) {
                                        if (ldb_msg_find_val(el2, &el->values[j]) != NULL) {
-                                               ldb_asprintf_errstring(ldb, "%s: value #%d already exists", el->name, j);
+                                               if (control_permissive) {
+                                                       /* remove this one as if it was never added */
+                                                       el->num_values--;
+                                                       for (k = j; k < el->num_values; k++) {
+                                                               el->values[k] = el->values[k + 1];
+                                                       }
+                                                       j--; /* rewind */
+
+                                                       continue;
+                                               }
+
+                                               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;
                                        }
@@ -733,16 +786,10 @@ int ltdb_modify_internal(struct ldb_module *module,
                        break;
 
                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;
@@ -753,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;
                                }
@@ -796,23 +848,35 @@ int ltdb_modify_internal(struct ldb_module *module,
 
                        if (msg->elements[i].num_values == 0) {
                                /* Delete the whole attribute */
-                               if (msg_delete_attribute(module, ldb, msg2,
-                                                        msg->elements[i].name) != 0) {
-                                       ldb_asprintf_errstring(ldb, "No such attribute: %s for delete on %s",
+                               ret = msg_delete_attribute(module, ldb, msg2,
+                                                          msg->elements[i].name);
+                               if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
+                                   control_permissive) {
+                                       ret = LDB_SUCCESS;
+                               } else {
+                                       ldb_asprintf_errstring(ldb,
+                                                              "attribute '%s': no such attribute for delete on '%s'",
                                                               msg->elements[i].name, dn);
-                                       ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
+                               }
+                               if (ret != LDB_SUCCESS) {
                                        goto done;
                                }
                        } else {
                                /* Delete specified values from an attribute */
                                for (j=0; j < msg->elements[i].num_values; j++) {
-                                       if (msg_delete_element(module,
-                                                              msg2,
-                                                              msg->elements[i].name,
-                                                              &msg->elements[i].values[j]) != 0) {
-                                               ldb_asprintf_errstring(ldb, "No matching attribute value when deleting attribute: %s on %s",
+                                       ret = msg_delete_element(module,
+                                                                msg2,
+                                                                msg->elements[i].name,
+                                                                &msg->elements[i].values[j]);
+                                       if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
+                                           control_permissive) {
+                                               ret = LDB_SUCCESS;
+                                       } else {
+                                               ldb_asprintf_errstring(ldb,
+                                                                      "attribute '%s': no matching attribute value while deleting attribute on '%s'",
                                                                       msg->elements[i].name, dn);
-                                               ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
+                                       }
+                                       if (ret != LDB_SUCCESS) {
                                                goto done;
                                        }
                                }
@@ -820,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;
                }
@@ -884,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;
        }
@@ -1042,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;
@@ -1213,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) {
@@ -1243,17 +1307,22 @@ static int ltdb_request_destructor(void *ptr)
 static int ltdb_handle_request(struct ldb_module *module,
                               struct ldb_request *req)
 {
+       struct ldb_control *control_permissive;
        struct ldb_context *ldb;
        struct tevent_context *ev;
        struct ltdb_context *ac;
        struct tevent_timer *te;
        struct timeval tv;
-       int i;
+       unsigned int i;
 
        ldb = ldb_module_get_ctx(module);
 
+       control_permissive = ldb_request_get_control(req,
+                                       LDB_CONTROL_PERMISSIVE_MODIFY_OID);
+
        for (i = 0; req->controls && req->controls[i]; i++) {
-               if (req->controls[i]->critical) {
+               if (req->controls[i]->critical &&
+                   req->controls[i] != control_permissive) {
                        ldb_asprintf_errstring(ldb, "Unsupported critical extension %s",
                                               req->controls[i]->oid);
                        return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
@@ -1305,8 +1374,24 @@ static int ltdb_handle_request(struct ldb_module *module,
        return LDB_SUCCESS;
 }
 
+static int ltdb_init_rootdse(struct ldb_module *module)
+{
+       struct ldb_context *ldb;
+       int ret;
+
+       ldb = ldb_module_get_ctx(module);
+
+       ret = ldb_mod_register_control(module,
+                                      LDB_CONTROL_PERMISSIVE_MODIFY_OID);
+       /* ignore errors on this - we expect it for non-sam databases */
+
+       /* there can be no module beyond the backend, just return */
+       return LDB_SUCCESS;
+}
+
 static const struct ldb_module_ops ltdb_ops = {
        .name              = "tdb",
+       .init_context      = ltdb_init_rootdse,
        .search            = ltdb_handle_request,
        .add               = ltdb_handle_request,
        .modify            = ltdb_handle_request,
@@ -1378,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);
@@ -1398,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);
+}