Merge branch 'master' of ssh://git.samba.org/data/git/samba into regsrv
[tprouty/samba.git] / source4 / lib / registry / ldb.c
index 25b8c583ed2f4c27650e36fc65f56092e2439472..9c1f59c4df6f75e28b6a39de651c33c4ca5fcbc2 100644 (file)
@@ -1,7 +1,8 @@
 /*
    Unix SMB/CIFS implementation.
    Registry interface
-   Copyright (C) Jelmer Vernooij  2004-2007.
+   Copyright (C) 2004-2007, Jelmer Vernooij, jelmer@samba.org
+   Copyright (C) 2008 Matthias Dieter Wallnöfer, mwallnoefer@yahoo.de
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -51,17 +52,31 @@ static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx,
                                      NULL));
 
        value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
-       if (type != NULL)
-               *type = value_type; 
+       *type = value_type; 
+
        val = ldb_msg_find_ldb_val(msg, "data");
 
        switch (value_type)
        {
        case REG_SZ:
        case REG_EXPAND_SZ:
-               data->length = convert_string_talloc(mem_ctx, iconv_convenience, CH_UTF8, CH_UTF16,
+               if (val != NULL)
+                       data->length = convert_string_talloc(mem_ctx, iconv_convenience, CH_UTF8, CH_UTF16,
                                                     val->data, val->length,
                                                     (void **)&data->data);
+               else {
+                       data->data = NULL;
+                       data->length = 0;
+               }
+               break;
+
+       case REG_BINARY:
+               if (val != NULL)
+                       *data = *val;
+               else {
+                       data->data = NULL;
+                       data->length = 0;
+               }
                break;
 
        case REG_DWORD: {
@@ -71,7 +86,7 @@ static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx,
                break;
 
        default:
-               *data = data_blob_talloc(mem_ctx, val->data, val->length);
+               *data = *val;
                break;
        }
 }
@@ -90,11 +105,22 @@ static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
        switch (type) {
        case REG_SZ:
        case REG_EXPAND_SZ:
-               val.length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX,
+               if (data.data[0] != '\0') {
+                       val.length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UTF8,
                                                   (void *)data.data,
                                                   data.length,
                                                   (void **)&val.data);
-               ldb_msg_add_value(msg, "data", &val, NULL);
+                       ldb_msg_add_value(msg, "data", &val, NULL);
+               } else {
+                       ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
+               }
+               break;
+
+       case REG_BINARY:
+               if (data.length > 0)
+                       ldb_msg_add_value(msg, "data", &data, NULL);
+               else
+                       ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
                break;
 
        case REG_DWORD:
@@ -228,9 +254,11 @@ static WERROR cache_values(struct ldb_key_data *kd)
                        ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
                return WERR_FOOBAR;
        }
+
        kd->value_count = res->count;
        kd->values = talloc_steal(kd, res->msgs);
        talloc_free(res);
+
        return WERR_OK;
 }
 
@@ -243,6 +271,15 @@ static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
 {
        struct ldb_message_element *el;
        struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
+       
+       /* Initialization */
+       if (name != NULL)
+               *name = NULL;
+       if (classname != NULL)
+               *classname = NULL; /* TODO: Store properly */
+       if (last_mod_time != NULL)
+               *last_mod_time = 0; /* TODO: we need to add this to the
+                                               ldb backend properly */
 
        /* Do a search if necessary */
        if (kd->subkeys == NULL) {
@@ -259,12 +296,34 @@ static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
        if (name != NULL)
                *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
 
-       if (classname != NULL)
-               *classname = NULL; /* TODO: Store properly */
+       return WERR_OK;
+}
 
-       if (last_mod_time != NULL)
-               *last_mod_time = 0; /* TODO: we need to add this to the
-                                               ldb backend properly */
+static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
+                                 const char **name, uint32_t *data_type,
+                                  DATA_BLOB *data)
+{
+       struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
+       struct ldb_context *c = kd->ldb;
+       const char* attrs[] = { "data", "type", NULL };
+       struct ldb_result *res;
+       int ret;
+
+       ret = ldb_search(c, kd->dn, LDB_SCOPE_BASE, "", attrs, &res);
+
+       if (ret != LDB_SUCCESS) {
+               DEBUG(0, ("Error getting default value for '%s': %s\n",
+                       ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
+               return WERR_FOOBAR;
+       }
+
+       if (res->count == 0 || res->msgs[0]->num_elements == 0)
+               return WERR_BADFILE;
+
+       reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
+                res->msgs[0], name, data_type, data);
+
+       talloc_free(res);
 
        return WERR_OK;
 }
@@ -275,6 +334,15 @@ static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
 {
        struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
 
+       /* if default value exists, give it back */
+       if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
+               data))) {
+               if (idx == 0)
+                       return WERR_OK;
+               else
+                       --idx;
+       }
+
        /* Do the search if necessary */
        if (kd->values == NULL) {
                W_ERROR_NOT_OK_RETURN(cache_values(kd));
@@ -283,8 +351,8 @@ static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
        if (idx >= kd->value_count)
                return WERR_NO_MORE_ITEMS;
 
-       reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm), kd->values[idx],
-                            name, data_type, data);
+       reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
+                        kd->values[idx], name, data_type, data);
 
        return WERR_OK;
 }
@@ -297,26 +365,32 @@ static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
        struct ldb_context *c = kd->ldb;
        struct ldb_result *res;
        int ret;
-       char *query = talloc_asprintf(mem_ctx, "(value=%s)", name);
+       char *query;
 
-       ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "%s", query);
+       if (strlen(name) == 0) {
+               /* default value */
+               return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
+       } else {
+               /* normal value */
+               query = talloc_asprintf(mem_ctx, "(value=%s)", name);
+               ret = ldb_search(c, kd->dn, &res, LDB_SCOPE_ONELEVEL, query, NULL, "%s", query);
+               talloc_free(query);
+
+               if (ret != LDB_SUCCESS) {
+                       DEBUG(0, ("Error getting values for '%s': %s\n",
+                               ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
+                       return WERR_FOOBAR;
+               }
 
-       talloc_free(query);
+               if (res->count == 0)
+                       return WERR_BADFILE;
 
-       if (ret != LDB_SUCCESS) {
-               DEBUG(0, ("Error getting values for '%s': %s\n",
-                       ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
-               talloc_free(res);
-               return WERR_FOOBAR;
-       }
+               reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
+                        res->msgs[0], NULL, data_type, data);
 
-       if (res->count == 0) {
                talloc_free(res);
-               return WERR_BADFILE;
        }
 
-       reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm), res->msgs[0], NULL, data_type, data);
-
        talloc_free(res);
        return WERR_OK;
 }
@@ -353,8 +427,6 @@ static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
 
        *key = (struct hive_key *)newkd;
 
-       talloc_free(res);
-
        return WERR_OK;
 }
 
@@ -451,25 +523,47 @@ static WERROR ldb_del_value (struct hive_key *key, const char *child)
 {
        int ret;
        struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
+       TALLOC_CTX *mem_ctx;
+       struct ldb_message *msg;
        struct ldb_dn *childdn;
 
-       childdn = ldb_dn_copy(kd->ldb, kd->dn);
-       if (!ldb_dn_add_child_fmt(childdn, "value=%s",
+       if (strlen(child) == 0) {
+               /* default value */
+               mem_ctx = talloc_init("ldb_del_value");
+
+               msg = talloc_zero(mem_ctx, struct ldb_message);
+               msg->dn = ldb_dn_copy(msg, kd->dn);
+               ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
+               ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
+
+               ret = ldb_modify(kd->ldb, msg);
+               if (ret != LDB_SUCCESS) {
+                       DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
+                       talloc_free(mem_ctx);
+                       return WERR_FOOBAR;
+               }
+
+               talloc_free(mem_ctx);
+       } else {
+               /* normal value */
+               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;
-       }
+               {
+                       talloc_free(childdn);
+                       return WERR_FOOBAR;
+               }
 
-       ret = ldb_delete(kd->ldb, childdn);
+               ret = ldb_delete(kd->ldb, childdn);
 
-       talloc_free(childdn);
+               talloc_free(childdn);
 
-       if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-               return WERR_BADFILE;
-       } else if (ret != LDB_SUCCESS) {
-               DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
-               return WERR_FOOBAR;
+               if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+                       return WERR_BADFILE;
+               } else if (ret != LDB_SUCCESS) {
+                       DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
+                       return WERR_FOOBAR;
+               }
        }
 
        /* reset cache */
@@ -605,26 +699,31 @@ static WERROR ldb_set_value(struct hive_key *parent,
        TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
 
        msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
-
        msg->dn = ldb_dn_copy(msg, kd->dn);
-       if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
+       
+       if (strlen(name) > 0) {
+               /* For a default value, we add/overwrite the attributes to/of the hive.
+                  For a normal value, we create new childs. */
+               if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
                                  reg_ldb_escape(mem_ctx, name)))
-       {
-               talloc_free(mem_ctx);
-               return WERR_FOOBAR;
+               {
+                       talloc_free(mem_ctx);
+                       return WERR_FOOBAR;
+               }
        }
 
        ret = ldb_add(kd->ldb, msg);
        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;
+                       if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE)
+                               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)));
+               DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
                talloc_free(mem_ctx);
                return WERR_FOOBAR;
        }
@@ -649,6 +748,22 @@ static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
 {
        struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
 
+       /* Initialization */
+       if (classname != NULL)
+               *classname = NULL;
+       if (num_subkeys != NULL)
+               *num_subkeys = 0;
+       if (num_values != NULL)
+               *num_values = 0;
+       if (last_change_time != NULL)
+               *last_change_time = 0;
+       if (max_subkeynamelen != NULL)
+               *max_subkeynamelen = 0;
+       if (max_valnamelen != NULL)
+               *max_valnamelen = 0;
+       if (max_valbufsize != NULL)
+               *max_valbufsize = 0;
+
        if (kd->subkeys == NULL) {
                W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
        }
@@ -657,20 +772,13 @@ static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
                W_ERROR_NOT_OK_RETURN(cache_values(kd));
        }
 
-       /* FIXME */
-       if (classname != NULL)
-               *classname = NULL;
-
        if (num_subkeys != NULL) {
                *num_subkeys = kd->subkey_count;
        }
-
        if (num_values != NULL) {
                *num_values = kd->value_count;
        }
 
-       if (last_change_time != NULL)
-               *last_change_time = 0;
 
        if (max_subkeynamelen != NULL) {
                int i;
@@ -702,11 +810,12 @@ static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
                        }
 
                        if (max_valbufsize != NULL) {
+                               uint32_t data_type;
                                DATA_BLOB data;
                                reg_ldb_unpack_value(mem_ctx, 
                                                     lp_iconv_convenience(global_loadparm),
                                                     kd->values[i], NULL, 
-                                                    NULL, &data);
+                                                    &data_type, &data);
                                *max_valbufsize = MAX(*max_valbufsize, data.length);
                                talloc_free(data.data);
                        }