s4-ldb: don't allow modifies outside a transaction.
[ira/wip.git] / source4 / lib / ldb / ldb_tdb / ldb_tdb.c
index 4943f81df59f5d5557bcfd5a0efb152e8e780c0b..d6175863dae7446675e752100a59c87f8da0ea44 100644 (file)
@@ -55,7 +55,7 @@
 /*
   map a tdb error code to a ldb error code
 */
-static int ltdb_err_map(enum TDB_ERROR tdb_code)
+int ltdb_err_map(enum TDB_ERROR tdb_code)
 {
        switch (tdb_code) {
        case TDB_SUCCESS:
@@ -200,6 +200,14 @@ static int ltdb_check_special_dn(struct ldb_module *module,
 static int ltdb_modified(struct ldb_module *module, struct ldb_dn *dn)
 {
        int ret = LDB_SUCCESS;
+       struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
+
+       /* only allow modifies inside a transaction, otherwise the
+        * ldb is unsafe */
+       if (ltdb->in_transaction == 0) {
+               ldb_set_errstring(ldb_module_get_ctx(module), "ltdb modify without transaction");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
 
        if (ldb_dn_is_special(dn) &&
            (ldb_dn_check_special(dn, LTDB_INDEXLIST) ||
@@ -234,7 +242,7 @@ int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flg
        int ret = LDB_SUCCESS;
 
        tdb_key = ltdb_key(module, msg->dn);
-       if (!tdb_key.dptr) {
+       if (tdb_key.dptr == NULL) {
                return LDB_ERR_OTHER;
        }
 
@@ -250,11 +258,6 @@ int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flg
                goto done;
        }
 
-       ret = ltdb_index_add(module, msg);
-       if (ret != LDB_SUCCESS) {
-               tdb_delete(ltdb->tdb, tdb_key);
-       }
-
 done:
        talloc_free(tdb_key.dptr);
        talloc_free(tdb_data.dptr);
@@ -271,12 +274,11 @@ static int ltdb_add_internal(struct ldb_module *module,
 
        ret = ltdb_check_special_dn(module, msg);
        if (ret != LDB_SUCCESS) {
-               goto done;
+               return ret;
        }
 
        if (ltdb_cache_load(module) != 0) {
-               ret = LDB_ERR_OPERATIONS_ERROR;
-               goto done;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
        for (i=0;i<msg->num_elements;i++) {
@@ -286,15 +288,13 @@ static int ltdb_add_internal(struct ldb_module *module,
                if (el->num_values == 0) {
                        ldb_asprintf_errstring(ldb, "attribute %s on %s specified, but with 0 values (illegal)", 
                                               el->name, ldb_dn_get_linearized(msg->dn));
-                       ret = LDB_ERR_CONSTRAINT_VIOLATION;
-                       goto done;
+                       return LDB_ERR_CONSTRAINT_VIOLATION;
                }
                if (a && a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
                        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));
-                               ret = LDB_ERR_CONSTRAINT_VIOLATION;
-                               goto done;
+                               return LDB_ERR_CONSTRAINT_VIOLATION;
                        }
                }
        }
@@ -306,20 +306,16 @@ static int ltdb_add_internal(struct ldb_module *module,
                                               "Entry %s already exists",
                                               ldb_dn_get_linearized(msg->dn));
                }
-               goto done;
+               return ret;
        }
 
-       ret = ltdb_index_one(module, msg, 1);
+       ret = ltdb_index_add_new(module, msg);
        if (ret != LDB_SUCCESS) {
-               goto done;
+               return ret;
        }
 
        ret = ltdb_modified(module, msg->dn);
-       if (ret != LDB_SUCCESS) {
-               goto done;
-       }
 
-done:
        return ret;
 }
 
@@ -392,14 +388,8 @@ static int ltdb_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
                goto done;
        }
 
-       /* remove one level attribute */
-       ret = ltdb_index_one(module, msg, 0);
-       if (ret != LDB_SUCCESS) {
-               goto done;
-       }
-
        /* remove any indexed attributes */
-       ret = ltdb_index_del(module, msg);
+       ret = ltdb_index_delete(module, msg);
        if (ret != LDB_SUCCESS) {
                goto done;
        }
@@ -460,9 +450,9 @@ static int find_element(const struct ldb_message *msg, const char *name)
 
   returns 0 on success, -1 on failure (and sets errno)
 */
-static int msg_add_element(struct ldb_context *ldb,
-                          struct ldb_message *msg,
-                          struct ldb_message_element *el)
+static int ltdb_msg_add_element(struct ldb_context *ldb,
+                               struct ldb_message *msg,
+                               struct ldb_message_element *el)
 {
        struct ldb_message_element *e2;
        unsigned int i;
@@ -509,40 +499,35 @@ static int msg_delete_attribute(struct ldb_module *module,
                                struct ldb_message *msg, const char *name)
 {
        const char *dn;
-       unsigned int i, j;
+       unsigned int i;
+       int ret;
+       struct ldb_message_element *el;
 
        dn = ldb_dn_get_linearized(msg->dn);
        if (dn == NULL) {
                return -1;
        }
 
-       for (i=0;i<msg->num_elements;i++) {
-               if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
-                       for (j=0;j<msg->elements[i].num_values;j++) {
-                               ltdb_index_del_value(module, dn,
-                                                    &msg->elements[i], j);
-                       }
-                       talloc_free(msg->elements[i].values);
-                       if (msg->num_elements > (i+1)) {
-                               memmove(&msg->elements[i],
-                                       &msg->elements[i+1],
-                                       sizeof(struct ldb_message_element)*
-                                       (msg->num_elements - (i+1)));
-                       }
-                       msg->num_elements--;
-                       i--;
-                       msg->elements = talloc_realloc(msg, msg->elements,
-                                                      struct ldb_message_element,
-                                                      msg->num_elements);
+       el = ldb_msg_find_element(msg, name);
+       if (el == NULL) {
+               return -1;
+       }
+       i = el - msg->elements;
 
-                       /* per definition we find in a canonicalised message an
-                          attribute only once. So we are finished here. */
-                       return 0;
-               }
+       ret = ltdb_index_del_element(module, dn, el);
+       if (ret != LDB_SUCCESS) {
+               return ret;
        }
 
-       /* Not found */
-       return -1;
+       talloc_free(el->values);
+       if (msg->num_elements > (i+1)) {
+               memmove(el, el+1, sizeof(*el) * (msg->num_elements - (i+1)));
+       }
+       msg->num_elements--;
+       msg->elements = talloc_realloc(msg, msg->elements,
+                                      struct ldb_message_element,
+                                      msg->num_elements);
+       return 0;
 }
 
 /*
@@ -557,7 +542,7 @@ static int msg_delete_element(struct ldb_module *module,
 {
        struct ldb_context *ldb = ldb_module_get_ctx(module);
        unsigned int i;
-       int found;
+       int found, ret;
        struct ldb_message_element *el;
        const struct ldb_schema_attribute *a;
 
@@ -572,17 +557,22 @@ static int msg_delete_element(struct ldb_module *module,
 
        for (i=0;i<el->num_values;i++) {
                if (a->syntax->comparison_fn(ldb, ldb,
-                                               &el->values[i], val) == 0) {
+                                            &el->values[i], val) == 0) {
+                       if (el->num_values == 1) {
+                               return msg_delete_attribute(module, ldb, msg, name);
+                       }
+
+                       ret = ltdb_index_del_value(module, ldb_dn_get_linearized(msg->dn), el, i);
+                       if (ret != LDB_SUCCESS) {
+                               return -1;
+                       }
+
                        if (i<el->num_values-1) {
                                memmove(&el->values[i], &el->values[i+1],
                                        sizeof(el->values[i])*
                                                (el->num_values-(i+1)));
                        }
                        el->num_values--;
-                       if (el->num_values == 0) {
-                               return msg_delete_attribute(module, ldb,
-                                                           msg, name);
-                       }
 
                        /* per definition we find in a canonicalised message an
                           attribute value only once. So we are finished here */
@@ -676,10 +666,14 @@ int ltdb_modify_internal(struct ldb_module *module,
                        /* Checks if element already exists */
                        idx = find_element(msg2, el->name);
                        if (idx == -1) {
-                               if (msg_add_element(ldb, msg2, el) != 0) {
+                               if (ltdb_msg_add_element(ldb, msg2, el) != 0) {
                                        ret = LDB_ERR_OTHER;
                                        goto done;
                                }
+                               ret = ltdb_index_add_element(module, msg->dn, el);
+                               if (ret != LDB_SUCCESS) {
+                                       goto done;
+                               }
                        } else {
                                /* We cannot add another value on a existing one
                                   if the attribute is single-valued */
@@ -710,8 +704,8 @@ int ltdb_modify_internal(struct ldb_module *module,
                                /* Now combine existing and new values to a new
                                   attribute record */
                                vals = talloc_realloc(msg2->elements,
-                                       el2->values, struct ldb_val,
-                                       el2->num_values + el->num_values);
+                                                     el2->values, struct ldb_val,
+                                                     el2->num_values + el->num_values);
                                if (vals == NULL) {
                                        ldb_oom(ldb);
                                        ret = LDB_ERR_OTHER;
@@ -725,6 +719,11 @@ int ltdb_modify_internal(struct ldb_module *module,
 
                                el2->values = vals;
                                el2->num_values += el->num_values;
+
+                               ret = ltdb_index_add_element(module, msg->dn, el);
+                               if (ret != LDB_SUCCESS) {
+                                       goto done;
+                               }
                        }
 
                        break;
@@ -739,6 +738,7 @@ 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);
@@ -747,15 +747,32 @@ int ltdb_modify_internal(struct ldb_module *module,
                                }
                        }
 
-                       /* Delete the attribute if it exists in the DB */
-                       msg_delete_attribute(module, ldb, msg2, el->name);
+                       idx = find_element(msg2, el->name);
+                       if (idx != -1) {
+                               el2 = &(msg2->elements[idx]);
+                               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 */
+                               ret = msg_delete_attribute(module, ldb, msg2, el->name);
+                               if (ret != LDB_SUCCESS) {
+                                       goto done;
+                               }
+                       }
 
                        /* Recreate it with the new values */
-                       if (msg_add_element(ldb, msg2, el) != 0) {
+                       if (ltdb_msg_add_element(ldb, msg2, el) != 0) {
                                ret = LDB_ERR_OTHER;
                                goto done;
                        }
 
+                       ret = ltdb_index_add_element(module, msg->dn, el);
+                       if (ret != LDB_SUCCESS) {
+                               goto done;
+                       }
+
                        break;
 
                case LDB_FLAG_MOD_DELETE:
@@ -786,15 +803,8 @@ int ltdb_modify_internal(struct ldb_module *module,
                                                ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
                                                goto done;
                                        }
-
-                                       ret = ltdb_index_del_value(module, dn,
-                                               &msg->elements[i], j);
-                                       if (ret != LDB_SUCCESS) {
-                                               goto done;
-                                       }
                                }
                        }
-
                        break;
                default:
                        ldb_asprintf_errstring(ldb,
@@ -864,8 +874,7 @@ static int ltdb_rename(struct ltdb_context *ctx)
 
        msg = talloc(ctx, struct ldb_message);
        if (msg == NULL) {
-               ret = LDB_ERR_OPERATIONS_ERROR;
-               goto done;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
        /* in case any attribute of the message was indexed, we need
@@ -873,13 +882,12 @@ static int ltdb_rename(struct ltdb_context *ctx)
        ret = ltdb_search_dn1(module, req->op.rename.olddn, msg);
        if (ret != LDB_SUCCESS) {
                /* not finding the old record is an error */
-               goto done;
+               return ret;
        }
 
        msg->dn = ldb_dn_copy(msg, req->op.rename.newdn);
        if (msg->dn == NULL) {
-               ret = LDB_ERR_OPERATIONS_ERROR;
-               goto done;
+               return LDB_ERR_OPERATIONS_ERROR;
        }
 
        /* Always delete first then add, to avoid conflicts with
@@ -888,12 +896,11 @@ static int ltdb_rename(struct ltdb_context *ctx)
         */
        ret = ltdb_delete_internal(module, req->op.rename.olddn);
        if (ret != LDB_SUCCESS) {
-               goto done;
+               return ret;
        }
 
        ret = ltdb_add_internal(module, msg);
 
-done:
        return ret;
 }
 
@@ -1362,6 +1369,7 @@ static int ltdb_connect(struct ldb_context *ldb, const char *url,
                return -1;
        }
        ldb_module_set_private(module, ltdb);
+       talloc_steal(module, ltdb);
 
        if (ltdb_cache_load(module) != 0) {
                talloc_free(module);