Merge branch 'v4-0-test' into id10ts-registry
[samba.git] / source4 / lib / registry / ldb.c
index 11fa066a3502ebe1b8dc8257fbe456d12a25a6fc..a764ca6235322885b4d4049f4a0cfb2d66f41d72 100644 (file)
@@ -36,25 +36,30 @@ struct ldb_key_data
        int subkey_count, value_count;
 };
 
-static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx, struct ldb_message *msg,
+static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx, 
+                                struct smb_iconv_convenience *iconv_convenience,
+                                struct ldb_message *msg,
                                 const char **name, uint32_t *type,
                                 DATA_BLOB *data)
 {
        const struct ldb_val *val;
+       uint32_t value_type;
+
        if (name != NULL)
                *name = talloc_strdup(mem_ctx,
                                      ldb_msg_find_attr_as_string(msg, "value",
                                      NULL));
 
+       value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
        if (type != NULL)
-               *type = ldb_msg_find_attr_as_uint(msg, "type", 0);
+               *type = value_type; 
        val = ldb_msg_find_ldb_val(msg, "data");
 
-       switch (*type)
+       switch (value_type)
        {
        case REG_SZ:
        case REG_EXPAND_SZ:
-               data->length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF8, CH_UTF16,
+               data->length = convert_string_talloc(mem_ctx, iconv_convenience, CH_UTF8, CH_UTF16,
                                                     val->data, val->length,
                                                     (void **)&data->data);
                break;
@@ -108,6 +113,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)
 {
@@ -156,7 +170,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) {
@@ -249,7 +269,7 @@ static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
        return WERR_OK;
 }
 
-static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, const struct hive_key *k,
+static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
                                  int idx, const char **name,
                                  uint32_t *data_type, DATA_BLOB *data)
 {
@@ -263,7 +283,7 @@ static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, const struct hive_key *k,
        if (idx >= kd->value_count)
                return WERR_NO_MORE_ITEMS;
 
-       reg_ldb_unpack_value(mem_ctx, kd->values[idx],
+       reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm), kd->values[idx],
                             name, data_type, data);
 
        return WERR_OK;
@@ -290,9 +310,9 @@ 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);
+       reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm), res->msgs[0], NULL, data_type, data);
 
        return WERR_OK;
 }
@@ -319,7 +339,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);
@@ -337,15 +357,17 @@ static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
                         struct auth_session_info *session_info,
                         struct cli_credentials *credentials,
+                        struct loadparm_context *lp_ctx,
                         struct hive_key **k)
 {
        struct ldb_key_data *kd;
        struct ldb_context *wrap;
+       struct ldb_message *attrs_msg;
 
        if (location == NULL)
                return WERR_INVALID_PARAM;
 
-       wrap = ldb_wrap_connect(parent_ctx, global_loadparm,
+       wrap = ldb_wrap_connect(parent_ctx, lp_ctx,
                                location, session_info, credentials, 0, NULL);
 
        if (wrap == NULL) {
@@ -353,6 +375,15 @@ WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
                return WERR_FOOBAR;
        }
 
+       attrs_msg = ldb_msg_new(wrap);
+       W_ERROR_HAVE_NO_MEMORY(attrs_msg);
+       attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
+       W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
+       ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
+       ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
+
+       ldb_add(wrap, attrs_msg);
+
        ldb_set_debug_stderr(wrap);
 
        kd = talloc_zero(parent_ctx, struct ldb_key_data);
@@ -371,7 +402,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;
@@ -386,8 +417,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 < 0) {
-               DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parentkd->ldb)));
+       if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
+               return WERR_ALREADY_EXISTS;
+       }
+
+       if (ret != LDB_SUCCESS) {
+               DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
                return WERR_FOOBAR;
        }
 
@@ -400,52 +435,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;
-       } else if (ret < 0) {
-               DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(parentkd->ldb)));
+               return WERR_BADFILE;
+       } else if (ret != LDB_SUCCESS) {
+               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 < 0) {
-               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;
 }
 
@@ -461,18 +602,32 @@ 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 < 0) {
-               ret = ldb_modify(kd->ldb, msg);
-               if (ret < 0) {
-                       DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(kd->ldb)));
-                       talloc_free(mem_ctx);
-                       return WERR_FOOBAR;
+       if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
+               int i;
+               for (i = 0; i < msg->num_elements; i++) {
+                       msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
                }
+               ret = ldb_modify(kd->ldb, msg);
+       }
+
+       if (ret != LDB_SUCCESS) {
+               DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(kd->ldb)));
+               talloc_free(mem_ctx);
+               return WERR_FOOBAR;
        }
 
+       /* reset cache */
+       talloc_free(kd->values);
+       kd->values = NULL;
+
        talloc_free(mem_ctx);
        return WERR_OK;
 }
@@ -482,27 +637,77 @@ static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
                               const char **classname,
                               uint32_t *num_subkeys,
                               uint32_t *num_values,
-                              NTTIME *last_change_time)
+                              NTTIME *last_change_time,
+                              uint32_t *max_subkeynamelen,
+                              uint32_t *max_valnamelen,
+                              uint32_t *max_valbufsize)
 {
        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;
        }
 
        if (last_change_time != NULL)
                *last_change_time = 0;
 
+       if (max_subkeynamelen != NULL) {
+               int i;
+               struct ldb_message_element *el;
+
+               *max_subkeynamelen = 0;
+
+               for (i = 0; i < kd->subkey_count; i++) {
+                       el = ldb_msg_find_element(kd->subkeys[i], "key");
+                       *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
+               }
+       }
+
+       if (max_valnamelen != NULL || max_valbufsize != NULL) {
+               int i;
+               struct ldb_message_element *el;
+               W_ERROR_NOT_OK_RETURN(cache_values(kd));
+
+               if (max_valbufsize != NULL)
+                       *max_valbufsize = 0;
+
+               if (max_valnamelen != NULL)
+                       *max_valnamelen = 0;
+
+               for (i = 0; i < kd->value_count; i++) {
+                       if (max_valnamelen != NULL) {
+                               el = ldb_msg_find_element(kd->values[i], "value");
+                               *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
+                       }
+
+                       if (max_valbufsize != NULL) {
+                               DATA_BLOB data;
+                               reg_ldb_unpack_value(mem_ctx, 
+                                                    lp_iconv_convenience(global_loadparm),
+                                                    kd->values[i], NULL, 
+                                                    NULL, &data);
+                               *max_valbufsize = MAX(*max_valbufsize, data.length);
+                               talloc_free(data.data);
+                       }
+               }
+       }
+
        return WERR_OK;
 }