registry: Implement recursive deletes for ldb-backed registry.
[samba.git] / source4 / lib / registry / ldb.c
index d87bc6cf8e214c27121c89980b62ce995ecd9439..31b78d82463aaf9e6eb72112b5e943b6b15778dd 100644 (file)
@@ -111,6 +111,15 @@ static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
        return msg;
 }
 
+static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
+{
+       struct ldb_val val;
+
+       val.data = discard_const_p(uint8_t, value);
+       val.length = strlen(value);
+
+       return ldb_dn_escape_value(mem_ctx, val);
+}
 
 static int reg_close_ldb_key(struct ldb_key_data *key)
 {
@@ -159,7 +168,13 @@ static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
                else keyname = mypath;
 
                if(strlen(keyname)) {
-                       ldb_dn_add_base_fmt(ret, "key=%s", keyname);
+                       if (!ldb_dn_add_base_fmt(ret, "key=%s",
+                                                reg_ldb_escape(local_ctx,
+                                                               keyname)))
+                       {
+                               talloc_free(local_ctx);
+                               return NULL;
+                       }
                }
 
                if(begin) {
@@ -293,7 +308,7 @@ static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
        }
 
        if (res->count == 0)
-               return WERR_NOT_FOUND;
+               return WERR_BADFILE;
 
        reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
 
@@ -322,7 +337,7 @@ static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
                DEBUG(3, ("Key '%s' not found\n",
                        ldb_dn_get_linearized(ldap_path)));
                talloc_free(res);
-               return WERR_NOT_FOUND;
+               return WERR_BADFILE;
        }
 
        newkd = talloc_zero(mem_ctx, struct ldb_key_data);
@@ -385,7 +400,7 @@ static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
                          struct security_descriptor *sd,
                          struct hive_key **newkey)
 {
-       const struct ldb_key_data *parentkd = (const struct ldb_key_data *)parent;
+       struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
        struct ldb_message *msg;
        struct ldb_key_data *newkd;
        int ret;
@@ -400,8 +415,12 @@ static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
                                   talloc_strdup(mem_ctx, classname));
 
        ret = ldb_add(parentkd->ldb, msg);
+       if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
+               return WERR_ALREADY_EXISTS;
+       }
+
        if (ret != LDB_SUCCESS) {
-               DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parentkd->ldb)));
+               DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
                return WERR_FOOBAR;
        }
 
@@ -414,52 +433,158 @@ static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
 
        *newkey = (struct hive_key *)newkd;
 
+       /* reset cache */
+       talloc_free(parentkd->subkeys);
+       parentkd->subkeys = NULL;
+
        return WERR_OK;
 }
 
-static WERROR ldb_del_key(const struct hive_key *key, const char *child)
+static WERROR ldb_del_value (struct hive_key *key, const char *child)
 {
        int ret;
-       struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
+       struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
        struct ldb_dn *childdn;
 
-       childdn = ldb_dn_copy(parentkd->ldb, parentkd->dn);
-       ldb_dn_add_child_fmt(childdn, "key=%s", child);
+       childdn = ldb_dn_copy(kd->ldb, kd->dn);
+       if (!ldb_dn_add_child_fmt(childdn, "value=%s",
+                                 reg_ldb_escape(childdn, child)))
+       {
+               talloc_free(childdn);
+               return WERR_FOOBAR;
+       }
 
-       ret = ldb_delete(parentkd->ldb, childdn);
+       ret = ldb_delete(kd->ldb, childdn);
 
        talloc_free(childdn);
 
        if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-               return WERR_NOT_FOUND;
+               return WERR_BADFILE;
        } else if (ret != LDB_SUCCESS) {
-               DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(parentkd->ldb)));
+               DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
                return WERR_FOOBAR;
        }
 
+       /* reset cache */
+       talloc_free(kd->values);
+       kd->values = NULL;
+
        return WERR_OK;
 }
 
-static WERROR ldb_del_value (struct hive_key *key, const char *child)
+static WERROR ldb_del_key(const struct hive_key *key, const char *name)
 {
-       int ret;
-       struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
-       struct ldb_dn *childdn;
+       int i, ret;
+       struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
+       struct ldb_dn *ldap_path;
+       TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
+       struct ldb_context *c = parentkd->ldb;
+       struct ldb_result *res_keys;
+       struct ldb_result *res_vals;
+       WERROR werr;
+       struct hive_key *hk;
+
+       /* Verify key exists by opening it */
+       werr = ldb_open_key(mem_ctx, key, name, &hk);
+       if (!W_ERROR_IS_OK(werr)) {
+               talloc_free(mem_ctx);
+               return werr;
+       }
 
-       childdn = ldb_dn_copy(kd->ldb, kd->dn);
-       ldb_dn_add_child_fmt(childdn, "value=%s", child);
+       ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
+       if (!ldap_path) {
+               talloc_free(mem_ctx);
+               return WERR_FOOBAR;
+       }
 
-       ret = ldb_delete(kd->ldb, childdn);
+       /* Search for subkeys */
+       ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
+                        "(key=*)", NULL, &res_keys);
 
-       talloc_free(childdn);
+       if (ret != LDB_SUCCESS) {
+               DEBUG(0, ("Error getting subkeys for '%s': %s\n",
+                     ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
+               talloc_free(mem_ctx);
+               return WERR_FOOBAR;
+       }
 
-       if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-               return WERR_NOT_FOUND;
-       } else if (ret != LDB_SUCCESS) {
-               DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
+       /* Search for values */
+       ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
+                        "(value=*)", NULL, &res_vals);
+
+       if (ret != LDB_SUCCESS) {
+               DEBUG(0, ("Error getting values for '%s': %s\n",
+                     ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
+               talloc_free(mem_ctx);
                return WERR_FOOBAR;
        }
 
+       /* Start an explicit transaction */
+       ret = ldb_transaction_start(c);
+
+       if (ret != LDB_SUCCESS) {
+               DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
+               talloc_free(mem_ctx);
+               return WERR_FOOBAR;
+       }
+
+       if (res_keys->count || res_vals->count)
+       {
+               /* Delete any subkeys */
+               for (i = 0; i < res_keys->count; i++)
+               {
+                       werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
+                                                       res_keys->msgs[i],
+                                                       "key", NULL));
+                       if (!W_ERROR_IS_OK(werr)) {
+                               ret = ldb_transaction_cancel(c);
+                               talloc_free(mem_ctx);
+                               return werr;
+                       }
+               }
+
+               /* Delete any values */
+               for (i = 0; i < res_vals->count; i++)
+               {
+                       werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
+                                                       res_vals->msgs[i],
+                                                       "value", NULL));
+                       if (!W_ERROR_IS_OK(werr)) {
+                               ret = ldb_transaction_cancel(c);
+                               talloc_free(mem_ctx);
+                               return werr;
+                       }
+               }
+       }
+
+       /* Delete the key itself */
+       ret = ldb_delete(c, ldap_path);
+
+       if (ret != LDB_SUCCESS)
+       {
+               DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
+               ret = ldb_transaction_cancel(c);
+               talloc_free(mem_ctx);
+               return WERR_FOOBAR;
+       }
+
+       /* Commit the transaction */
+       ret = ldb_transaction_commit(c);
+
+       if (ret != LDB_SUCCESS)
+       {
+               DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
+               ret = ldb_transaction_cancel(c);
+               talloc_free(mem_ctx);
+               return WERR_FOOBAR;
+       }
+
+       talloc_free(mem_ctx);
+
+       /* reset cache */
+       talloc_free(parentkd->subkeys);
+       parentkd->subkeys = NULL;
+
        return WERR_OK;
 }
 
@@ -475,7 +600,12 @@ static WERROR ldb_set_value(struct hive_key *parent,
        msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
 
        msg->dn = ldb_dn_copy(msg, kd->dn);
-       ldb_dn_add_child_fmt(msg->dn, "value=%s", name);
+       if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
+                                 reg_ldb_escape(mem_ctx, name)))
+       {
+               talloc_free(mem_ctx);
+               return WERR_FOOBAR;
+       }
 
        ret = ldb_add(kd->ldb, msg);
        if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
@@ -492,6 +622,10 @@ static WERROR ldb_set_value(struct hive_key *parent,
                return WERR_FOOBAR;
        }
 
+       /* reset cache */
+       talloc_free(kd->values);
+       kd->values = NULL;
+
        talloc_free(mem_ctx);
        return WERR_OK;
 }
@@ -508,17 +642,23 @@ static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
 {
        struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
 
+       if (kd->subkeys == NULL) {
+               W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
+       }
+
+       if (kd->values == NULL) {
+               W_ERROR_NOT_OK_RETURN(cache_values(kd));
+       }
+
        /* FIXME */
        if (classname != NULL)
                *classname = NULL;
 
        if (num_subkeys != NULL) {
-               W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
                *num_subkeys = kd->subkey_count;
        }
 
        if (num_values != NULL) {
-               W_ERROR_NOT_OK_RETURN(cache_values(kd));
                *num_values = kd->value_count;
        }
 
@@ -528,7 +668,6 @@ static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
        if (max_subkeynamelen != NULL) {
                int i;
                struct ldb_message_element *el;
-               W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
 
                *max_subkeynamelen = 0;