s3-registry: fix bug #8401 - registry/reg_format.c must include includes.h.
[kai/samba.git] / source3 / registry / reg_backend_db.c
index b89da95ae60f2abd6808f01a3a845c2bb58a37f2..4e10bf652a5adf7e77950cfac74b4019770fffd1 100644 (file)
@@ -2,7 +2,8 @@
  *  Unix SMB/CIFS implementation.
  *  Virtual Windows Registry Layer
  *  Copyright (C) Gerald Carter                     2002-2005
- *  Copyright (C) Michael Adam                      2007-2009
+ *  Copyright (C) Michael Adam                      2007-2011
+ *  Copyright (C) Gregor Beck                       2011
  *
  *  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
@@ -29,7 +30,8 @@
 #include "reg_objects.h"
 #include "nt_printing.h"
 #include "util_tdb.h"
-#include "dbwrap.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_open.h"
 #include "../libcli/security/secdesc.h"
 
 #undef DBGC_CLASS
@@ -39,7 +41,6 @@ static struct db_context *regdb = NULL;
 static int regdb_refcount;
 
 static bool regdb_key_exists(struct db_context *db, const char *key);
-static bool regdb_key_is_base_key(const char *key);
 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
                                        struct regsubkey_ctr *ctr);
 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
@@ -48,8 +49,13 @@ static int regdb_fetch_values_internal(struct db_context *db, const char* key,
                                       struct regval_ctr *values);
 static bool regdb_store_values_internal(struct db_context *db, const char *key,
                                        struct regval_ctr *values);
+static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
+                                     const char *key);
 
-static NTSTATUS create_sorted_subkeys(const char *key);
+static WERROR regdb_create_basekey(struct db_context *db, const char *key);
+static WERROR regdb_create_subkey_internal(struct db_context *db,
+                                          const char *key,
+                                          const char *subkey);
 
 /* List the deepest path into the registry.  All part components will be created.*/
 
@@ -114,113 +120,72 @@ static struct builtin_regkey_value builtin_registry_values[] = {
        { NULL, NULL, 0, { NULL } }
 };
 
-/**
- * Initialize a key in the registry:
- * create each component key of the specified path.
- */
-static WERROR init_registry_key_internal(struct db_context *db,
-                                        const char *add_path)
+static WERROR create_key_recursive(struct db_context *db,
+                                  char *path,
+                                  const char *subkey)
 {
        WERROR werr;
-       TALLOC_CTX *frame = talloc_stackframe();
-       char *path = NULL;
-       char *base = NULL;
-       char *remaining = NULL;
-       char *keyname;
-       char *subkeyname;
-       struct regsubkey_ctr *subkeys;
-       const char *p, *p2;
-
-       DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
+       char *p;
 
-       path = talloc_strdup(frame, add_path);
-       base = talloc_strdup(frame, "");
-       if (!path || !base) {
-               werr = WERR_NOMEM;
-               goto fail;
+       if (subkey == NULL) {
+               return WERR_INVALID_PARAM;
        }
-       p = path;
-
-       while (next_token_talloc(frame, &p, &keyname, "\\")) {
 
-               /* build up the registry path from the components */
+       if (path == NULL) {
+               return regdb_create_basekey(db, subkey);
+       }
 
-               if (*base) {
-                       base = talloc_asprintf(frame, "%s\\", base);
-                       if (!base) {
-                               werr = WERR_NOMEM;
-                               goto fail;
-                       }
-               }
-               base = talloc_asprintf_append(base, "%s", keyname);
-               if (!base) {
-                       werr = WERR_NOMEM;
-                       goto fail;
-               }
+       p = strrchr_m(path, '\\');
 
-               /* get the immediate subkeyname (if we have one ) */
+       if (p == NULL) {
+               werr = create_key_recursive(db, NULL, path);
+       } else {
+               *p = '\0';
+               werr = create_key_recursive(db, path, p+1);
+               *p = '\\';
+       }
 
-               subkeyname = talloc_strdup(frame, "");
-               if (!subkeyname) {
-                       werr = WERR_NOMEM;
-                       goto fail;
-               }
-               if (*p) {
-                       remaining = talloc_strdup(frame, p);
-                       if (!remaining) {
-                               werr = WERR_NOMEM;
-                               goto fail;
-                       }
-                       p2 = remaining;
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
 
-                       if (!next_token_talloc(frame, &p2,
-                                               &subkeyname, "\\"))
-                       {
-                               subkeyname = talloc_strdup(frame,p2);
-                               if (!subkeyname) {
-                                       werr = WERR_NOMEM;
-                                       goto fail;
-                               }
-                       }
-               }
+       werr = regdb_create_subkey_internal(db, path, subkey);
 
-               DEBUG(10,("init_registry_key: Storing key [%s] with "
-                         "subkey [%s]\n", base,
-                         *subkeyname ? subkeyname : "NULL"));
+done:
+       return werr;
+}
 
-               /* we don't really care if the lookup succeeds or not
-                * since we are about to update the record.
-                * We just want any subkeys already present */
+/**
+ * Initialize a key in the registry:
+ * create each component key of the specified path.
+ */
+static WERROR init_registry_key_internal(struct db_context *db,
+                                        const char *add_path)
+{
+       char *subkey, *key;
+       WERROR werr;
+       TALLOC_CTX *frame = talloc_stackframe();
 
-               werr = regsubkey_ctr_init(frame, &subkeys);
-               if (!W_ERROR_IS_OK(werr)) {
-                       DEBUG(0,("talloc() failure!\n"));
-                       goto fail;
-               }
+       if (add_path == NULL) {
+               werr = WERR_INVALID_PARAM;
+               goto done;
+       }
 
-               werr = regdb_fetch_keys_internal(db, base, subkeys);
-               if (!W_ERROR_IS_OK(werr) &&
-                   !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
-               {
-                       goto fail;
-               }
+       key = talloc_strdup(frame, add_path);
 
-               if (*subkeyname) {
-                       werr = regsubkey_ctr_addkey(subkeys, subkeyname);
-                       if (!W_ERROR_IS_OK(werr)) {
-                               goto fail;
-                       }
-               }
-               if (!regdb_store_keys_internal(db, base, subkeys)) {
-                       werr = WERR_CAN_NOT_COMPLETE;
-                       goto fail;
-               }
+       subkey = strrchr_m(key, '\\');
+       if (subkey == NULL) {
+               subkey = key;
+               key = NULL;
+       } else {
+               *subkey = '\0';
+               subkey++;
        }
 
-       werr = WERR_OK;
+       werr = create_key_recursive(db, key, subkey);
 
-fail:
-       TALLOC_FREE(frame);
+done:
+       talloc_free(frame);
        return werr;
 }
 
@@ -446,16 +411,16 @@ static int regdb_normalize_keynames_fn(struct db_record *rec,
        return 0;
 }
 
-static WERROR regdb_store_regdb_version(uint32_t version)
+static WERROR regdb_store_regdb_version(struct db_context *db, uint32_t version)
 {
        NTSTATUS status;
        const char *version_keyname = "INFO/version";
 
-       if (!regdb) {
+       if (db == NULL) {
                return WERR_CAN_NOT_COMPLETE;
        }
 
-       status = dbwrap_trans_store_int32(regdb, version_keyname, version);
+       status = dbwrap_trans_store_int32(db, version_keyname, version);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
                          version_keyname, version, nt_errstr(status)));
@@ -467,26 +432,119 @@ static WERROR regdb_store_regdb_version(uint32_t version)
        }
 }
 
-static WERROR regdb_upgrade_v1_to_v2(void)
+static WERROR regdb_upgrade_v1_to_v2(struct db_context *db)
 {
        TALLOC_CTX *mem_ctx;
        int rc;
        WERROR werr;
 
        mem_ctx = talloc_stackframe();
-       if (mem_ctx == NULL) {
-               return WERR_NOMEM;
-       }
 
-       rc = regdb->traverse(regdb, regdb_normalize_keynames_fn, mem_ctx);
+       rc = regdb->traverse(db, regdb_normalize_keynames_fn, mem_ctx);
 
-       talloc_destroy(mem_ctx);
+       talloc_free(mem_ctx);
 
        if (rc < 0) {
                return WERR_REG_IO_FAILURE;
        }
 
-       werr = regdb_store_regdb_version(REGVER_V2);
+       werr = regdb_store_regdb_version(db, REGVER_V2);
+       return werr;
+}
+
+static int regdb_upgrade_v2_to_v3_fn(struct db_record *rec, void *private_data)
+{
+       const char *keyname;
+       fstring subkeyname;
+       NTSTATUS status;
+       WERROR werr;
+       uint8_t *buf;
+       uint32_t buflen, len;
+       uint32_t num_items;
+       uint32_t i;
+
+       if (rec->key.dptr == NULL || rec->key.dsize == 0) {
+               return 0;
+       }
+
+       keyname = (const char *)rec->key.dptr;
+
+       if (strncmp(keyname, REG_SORTED_SUBKEYS_PREFIX,
+                   strlen(REG_SORTED_SUBKEYS_PREFIX)) == 0)
+       {
+               /* Delete the deprecated sorted subkeys cache. */
+
+               DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%s]\n", keyname));
+
+               status = rec->delete_rec(rec);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0, ("regdb_upgrade_v2_to_v3: tdb_delete for [%s] "
+                                 "failed!\n", keyname));
+                       return 1;
+               }
+
+               return 0;
+       }
+
+       if (strncmp(keyname, REG_VALUE_PREFIX, strlen(REG_VALUE_PREFIX)) == 0) {
+               DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname));
+               return 0;
+       }
+
+       if (strncmp(keyname, REG_SECDESC_PREFIX,
+                   strlen(REG_SECDESC_PREFIX)) == 0)
+       {
+               DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname));
+               return 0;
+       }
+
+       /*
+        * Found a regular subkey list record.
+        * Walk the list and create the list record for those
+        * subkeys that don't already have one.
+        */
+       DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkey list of [%s]\n",
+                  keyname));
+
+       buf = rec->value.dptr;
+       buflen = rec->value.dsize;
+
+       len = tdb_unpack(buf, buflen, "d", &num_items);
+       if (len == (uint32_t)-1) {
+               /* invalid or empty - skip */
+               return 0;
+       }
+
+       for (i=0; i<num_items; i++) {
+               len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
+               DEBUG(10, ("regdb_upgrade_v2_to_v3: "
+                          "writing subkey list for [%s\\%s]\n",
+                          keyname, subkeyname));
+               werr = regdb_store_subkey_list(regdb, keyname, subkeyname);
+               if (!W_ERROR_IS_OK(werr)) {
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
+static WERROR regdb_upgrade_v2_to_v3(struct db_context *db)
+{
+       int rc;
+       WERROR werr;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       rc = regdb->traverse(db, regdb_upgrade_v2_to_v3_fn, frame);
+       if (rc < 0) {
+               werr = WERR_REG_IO_FAILURE;
+               goto done;
+       }
+
+       werr = regdb_store_regdb_version(db, REGVER_V3);
+
+done:
+       talloc_free(frame);
        return werr;
 }
 
@@ -526,7 +584,7 @@ WERROR regdb_init(void)
        DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
                   regdb_refcount));
 
-       expected_version = REGVER_V2;
+       expected_version = REGVER_V3;
 
        vers_id = dbwrap_fetch_int32(regdb, vstring);
        if (vers_id == -1) {
@@ -534,7 +592,7 @@ WERROR regdb_init(void)
                           "(got %d), initializing to version %d\n",
                           vers_id, expected_version));
 
-               werr = regdb_store_regdb_version(expected_version);
+               werr = regdb_store_regdb_version(regdb, expected_version);
                return werr;
        }
 
@@ -545,29 +603,42 @@ WERROR regdb_init(void)
                return WERR_CAN_NOT_COMPLETE;
        }
 
-       if (vers_id == REGVER_V1) {
-               DEBUG(10, ("regdb_init: got registry db version %d, upgrading "
-                          "to version %d\n", REGVER_V1, REGVER_V2));
+       if (regdb->transaction_start(regdb) != 0) {
+               return WERR_REG_IO_FAILURE;
+       }
 
-               if (regdb->transaction_start(regdb) != 0) {
-                       return WERR_REG_IO_FAILURE;
-               }
+       if (vers_id == REGVER_V1) {
+               DEBUG(10, ("regdb_init: upgrading registry fromversion %d "
+                          "to %d\n", REGVER_V1, REGVER_V2));
 
-               werr = regdb_upgrade_v1_to_v2();
+               werr = regdb_upgrade_v1_to_v2(regdb);
                if (!W_ERROR_IS_OK(werr)) {
                        regdb->transaction_cancel(regdb);
                        return werr;
                }
 
-               if (regdb->transaction_commit(regdb) != 0) {
-                       return WERR_REG_IO_FAILURE;
+               vers_id = REGVER_V2;
+       }
+
+       if (vers_id == REGVER_V2) {
+               DEBUG(10, ("regdb_init: upgrading registry from version %d "
+                          "to %d\n", REGVER_V2, REGVER_V3));
+
+               werr = regdb_upgrade_v2_to_v3(regdb);
+               if (!W_ERROR_IS_OK(werr)) {
+                       regdb->transaction_cancel(regdb);
+                       return werr;
                }
 
-               vers_id = REGVER_V2;
+               vers_id = REGVER_V3;
        }
 
        /* future upgrade code should go here */
 
+       if (regdb->transaction_commit(regdb) != 0) {
+               return WERR_REG_IO_FAILURE;
+       }
+
        return WERR_OK;
 }
 
@@ -686,7 +757,7 @@ static WERROR regdb_delete_key_with_prefix(struct db_context *db,
 
        werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
 
-       /* treat "not" found" as ok */
+       /* treat "not found" as ok */
        if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
                werr = WERR_OK;
        }
@@ -712,12 +783,6 @@ static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname
        return regdb_delete_key_with_prefix(db, keyname, NULL);
 }
 
-static WERROR regdb_delete_sorted_subkeys(struct db_context *db,
-                                         const char *keyname)
-{
-       return regdb_delete_key_with_prefix(db, keyname, REG_SORTED_SUBKEYS_PREFIX);
-}
-
 
 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
 {
@@ -737,14 +802,6 @@ static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
                goto done;
        }
 
-       werr = regdb_delete_sorted_subkeys(db, keyname);
-       if (!W_ERROR_IS_OK(werr)) {
-               DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
-                         REG_SORTED_SUBKEYS_PREFIX, keyname,
-                         win_errstr(werr)));
-               goto done;
-       }
-
        werr = regdb_delete_subkeylist(db, keyname);
        if (!W_ERROR_IS_OK(werr)) {
                DEBUG(1, (__location__ " Deleting %s failed: %s\n",
@@ -847,12 +904,6 @@ static WERROR regdb_store_keys_internal2(struct db_context *db,
        dbuf.dsize = len;
        werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
                                                        TDB_REPLACE));
-       W_ERROR_NOT_OK_GOTO_DONE(werr);
-
-       /*
-        * recreate the sorted subkey cache for regdb_key_exists()
-        */
-       werr = ntstatus_to_werror(create_sorted_subkeys(keyname));
 
 done:
        TALLOC_FREE(ctx);
@@ -860,6 +911,59 @@ done:
        return werr;
 }
 
+/**
+ * Utility function to store a new empty list of
+ * subkeys of given key specified as parent and subkey name
+ * (thereby creating the key).
+ * If the parent keyname is NULL, then the "subkey" is
+ * interpreted as a base key.
+ * If the subkey list does already exist, it is not modified.
+ *
+ * Must be called from within a transaction.
+ */
+static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
+                                     const char *key)
+{
+       WERROR werr;
+       char *path = NULL;
+       struct regsubkey_ctr *subkeys = NULL;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       if (parent == NULL) {
+               path = talloc_strdup(frame, key);
+       } else {
+               path = talloc_asprintf(frame, "%s\\%s", parent, key);
+       }
+       if (!path) {
+               werr = WERR_NOMEM;
+               goto done;
+       }
+
+       werr = regsubkey_ctr_init(frame, &subkeys);
+       W_ERROR_NOT_OK_GOTO_DONE(werr);
+
+       werr = regdb_fetch_keys_internal(db, path, subkeys);
+       if (W_ERROR_IS_OK(werr)) {
+               /* subkey list exists already - don't modify */
+               goto done;
+       }
+
+       werr = regsubkey_ctr_reinit(subkeys);
+       W_ERROR_NOT_OK_GOTO_DONE(werr);
+
+       /* create a record with 0 subkeys */
+       werr = regdb_store_keys_internal2(db, path, subkeys);
+       if (!W_ERROR_IS_OK(werr)) {
+               DEBUG(0, ("regdb_store_keys: Failed to store new record for "
+                         "key [%s]: %s\n", path, win_errstr(werr)));
+               goto done;
+       }
+
+done:
+       talloc_free(frame);
+       return werr;
+}
+
 /***********************************************************************
  Store the new subkey record and create any child key records that
  do not currently exist
@@ -877,7 +981,7 @@ static NTSTATUS regdb_store_keys_action(struct db_context *db,
        WERROR werr;
        int num_subkeys, i;
        char *path = NULL;
-       struct regsubkey_ctr *subkeys = NULL, *old_subkeys = NULL;
+       struct regsubkey_ctr *old_subkeys = NULL;
        char *oldkeyname = NULL;
        TALLOC_CTX *mem_ctx = talloc_stackframe();
 
@@ -960,44 +1064,13 @@ static NTSTATUS regdb_store_keys_action(struct db_context *db,
 
        num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
 
-       if (num_subkeys == 0) {
-               werr = regsubkey_ctr_init(mem_ctx, &subkeys);
-               W_ERROR_NOT_OK_GOTO_DONE(werr);
-
-               werr = regdb_store_keys_internal2(db, store_ctx->key, subkeys);
-               if (!W_ERROR_IS_OK(werr)) {
-                       DEBUG(0,("regdb_store_keys: Failed to store "
-                                "new record for key [%s]: %s\n",
-                                store_ctx->key, win_errstr(werr)));
-                       goto done;
-               }
-               TALLOC_FREE(subkeys);
-       }
-
        for (i=0; i<num_subkeys; i++) {
-               path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
-                               regsubkey_ctr_specific_key(store_ctx->ctr, i));
-               if (!path) {
-                       werr = WERR_NOMEM;
-                       goto done;
-               }
-               werr = regsubkey_ctr_init(mem_ctx, &subkeys);
-               W_ERROR_NOT_OK_GOTO_DONE(werr);
+               const char *subkey;
 
-               werr = regdb_fetch_keys_internal(db, path, subkeys);
-               if (!W_ERROR_IS_OK(werr)) {
-                       /* create a record with 0 subkeys */
-                       werr = regdb_store_keys_internal2(db, path, subkeys);
-                       if (!W_ERROR_IS_OK(werr)) {
-                               DEBUG(0,("regdb_store_keys: Failed to store "
-                                        "new record for key [%s]: %s\n", path,
-                                        win_errstr(werr)));
-                               goto done;
-                       }
-               }
+               subkey = regsubkey_ctr_specific_key(store_ctx->ctr, i);
 
-               TALLOC_FREE(subkeys);
-               TALLOC_FREE(path);
+               werr = regdb_store_subkey_list(db, store_ctx->key, subkey);
+               W_ERROR_NOT_OK_GOTO_DONE(werr);
        }
 
        werr = WERR_OK;
@@ -1017,7 +1090,7 @@ static bool regdb_store_keys_internal(struct db_context *db, const char *key,
        bool ret = false;
        struct regdb_store_keys_context store_ctx;
 
-       if (!regdb_key_is_base_key(key) && !regdb_key_exists(db, key)) {
+       if (!regdb_key_exists(db, key)) {
                goto done;
        }
 
@@ -1120,19 +1193,23 @@ static NTSTATUS regdb_create_subkey_action(struct db_context *db,
                         win_errstr(werr)));
        }
 
+       werr = regdb_store_subkey_list(db, create_ctx->key, create_ctx->subkey);
+
 done:
        talloc_free(mem_ctx);
        return werror_to_ntstatus(werr);
 }
 
-static WERROR regdb_create_subkey(const char *key, const char *subkey)
+static WERROR regdb_create_subkey_internal(struct db_context *db,
+                                          const char *key,
+                                          const char *subkey)
 {
        WERROR werr;
        struct regsubkey_ctr *subkeys;
        TALLOC_CTX *mem_ctx = talloc_stackframe();
        struct regdb_create_subkey_context create_ctx;
 
-       if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
+       if (!regdb_key_exists(db, key)) {
                werr = WERR_NOT_FOUND;
                goto done;
        }
@@ -1140,7 +1217,7 @@ static WERROR regdb_create_subkey(const char *key, const char *subkey)
        werr = regsubkey_ctr_init(mem_ctx, &subkeys);
        W_ERROR_NOT_OK_GOTO_DONE(werr);
 
-       werr = regdb_fetch_keys_internal(regdb, key, subkeys);
+       werr = regdb_fetch_keys_internal(db, key, subkeys);
        W_ERROR_NOT_OK_GOTO_DONE(werr);
 
        if (regsubkey_ctr_key_exists(subkeys, subkey)) {
@@ -1153,7 +1230,7 @@ static WERROR regdb_create_subkey(const char *key, const char *subkey)
        create_ctx.key = key;
        create_ctx.subkey = subkey;
 
-       werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
+       werr = ntstatus_to_werror(dbwrap_trans_do(db,
                                                  regdb_create_subkey_action,
                                                  &create_ctx));
 
@@ -1162,6 +1239,46 @@ done:
        return werr;
 }
 
+static WERROR regdb_create_subkey(const char *key, const char *subkey)
+{
+       return regdb_create_subkey_internal(regdb, key, subkey);
+}
+
+/**
+ * create a base key
+ */
+
+struct regdb_create_basekey_context {
+       const char *key;
+};
+
+static NTSTATUS regdb_create_basekey_action(struct db_context *db,
+                                           void *private_data)
+{
+       WERROR werr;
+       struct regdb_create_basekey_context *create_ctx;
+
+       create_ctx = (struct regdb_create_basekey_context *)private_data;
+
+       werr = regdb_store_subkey_list(db, NULL, create_ctx->key);
+
+       return werror_to_ntstatus(werr);
+}
+
+static WERROR regdb_create_basekey(struct db_context *db, const char *key)
+{
+       WERROR werr;
+       struct regdb_create_subkey_context create_ctx;
+
+       create_ctx.key = key;
+
+       werr = ntstatus_to_werror(dbwrap_trans_do(db,
+                                                 regdb_create_basekey_action,
+                                                 &create_ctx));
+
+       return werr;
+}
+
 /**
  * create a subkey of a given key
  */
@@ -1170,6 +1287,7 @@ struct regdb_delete_subkey_context {
        const char *key;
        const char *subkey;
        const char *path;
+       bool lazy;
 };
 
 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
@@ -1185,6 +1303,10 @@ static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
        werr = regdb_delete_key_lists(db, delete_ctx->path);
        W_ERROR_NOT_OK_GOTO_DONE(werr);
 
+       if (delete_ctx->lazy) {
+               goto done;
+       }
+
        werr = regsubkey_ctr_init(mem_ctx, &subkeys);
        W_ERROR_NOT_OK_GOTO_DONE(werr);
 
@@ -1206,7 +1328,7 @@ done:
        return werror_to_ntstatus(werr);
 }
 
-static WERROR regdb_delete_subkey(const char *key, const char *subkey)
+static WERROR regdb_delete_subkey(const char *key, const char *subkey, bool lazy)
 {
        WERROR werr;
        char *path;
@@ -1232,6 +1354,7 @@ static WERROR regdb_delete_subkey(const char *key, const char *subkey)
        delete_ctx.key = key;
        delete_ctx.subkey = subkey;
        delete_ctx.path = path;
+       delete_ctx.lazy = lazy;
 
        werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
                                                  regdb_delete_subkey_action,
@@ -1261,14 +1384,27 @@ static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
 
 
 /**
- * check whether a given key name represents a base key,
- * i.e one without a subkey separator ('\').
+ * Check for the existence of a key.
+ *
+ * Existence of a key is authoritatively defined by
+ * the existence of the record that contains the list
+ * of its subkeys.
+ *
+ * Return false, if the record does not match the correct
+ * structure of an initial 4-byte counter and then a
+ * list of the corresponding number of zero-terminated
+ * strings.
  */
-static bool regdb_key_is_base_key(const char *key)
+static bool regdb_key_exists(struct db_context *db, const char *key)
 {
        TALLOC_CTX *mem_ctx = talloc_stackframe();
+       TDB_DATA value;
        bool ret = false;
        char *path;
+       uint32_t buflen;
+       const char *buf;
+       uint32_t num_items, i;
+       int32_t len;
 
        if (key == NULL) {
                goto done;
@@ -1284,326 +1420,70 @@ static bool regdb_key_is_base_key(const char *key)
                goto done;
        }
 
-       ret = (strrchr(path, '\\') == NULL);
-
-done:
-       TALLOC_FREE(mem_ctx);
-       return ret;
-}
-
-/*
- * regdb_key_exists() is a very frequent operation. It can be quite
- * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
- * subkeys and then compare the keyname linearly to all the parent's subkeys.
- *
- * The following code tries to make this operation as efficient as possible:
- * Per registry key we create a list of subkeys that is very efficient to
- * search for existence of a subkey. Its format is:
- *
- * 4 bytes num_subkeys
- * 4*num_subkey bytes offset into the string array
- * then follows a sorted list of subkeys in uppercase
- *
- * This record is created by create_sorted_subkeys() on demand if it does not
- * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
- * list, the parsing code and the binary search can be found in
- * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
- * the potentially large subkey record.
- *
- * The sorted subkey record is deleted in regdb_store_keys_internal2 and
- * recreated on demand.
- */
-
-static int cmp_keynames(char **p1, char **p2)
-{
-       return strcasecmp_m(*p1, *p2);
-}
-
-struct create_sorted_subkeys_context {
-       const char *key;
-       const char *sorted_keyname;
-};
-
-static NTSTATUS create_sorted_subkeys_action(struct db_context *db,
-                                            void *private_data)
-{
-       char **sorted_subkeys;
-       struct regsubkey_ctr *ctr;
-       NTSTATUS status;
-       char *buf;
-       char *p;
-       int i;
-       size_t len;
-       int num_subkeys;
-       struct create_sorted_subkeys_context *sorted_ctx;
-
-       sorted_ctx = (struct create_sorted_subkeys_context *)private_data;
-
-       /*
-        * In this function, we only treat failing of the actual write to
-        * the db as a real error. All preliminary errors, at a stage when
-        * nothing has been written to the DB yet are treated as success
-        * to be committed (as an empty transaction).
-        *
-        * The reason is that this (disposable) call might be nested in other
-        * transactions. Doing a cancel here would destroy the possibility of
-        * a transaction_commit for transactions that we might be wrapped in.
-        */
-
-       status = werror_to_ntstatus(regsubkey_ctr_init(talloc_tos(), &ctr));
-       if (!NT_STATUS_IS_OK(status)) {
-               /* don't treat this as an error */
-               status = NT_STATUS_OK;
-               goto done;
-       }
-
-       status = werror_to_ntstatus(regdb_fetch_keys_internal(db,
-                                                             sorted_ctx->key,
-                                                             ctr));
-       if (!NT_STATUS_IS_OK(status)) {
-               /* don't treat this as an error */
-               status = NT_STATUS_OK;
+       value = regdb_fetch_key_internal(db, mem_ctx, path);
+       if (value.dptr == NULL) {
                goto done;
        }
 
-       num_subkeys = regsubkey_ctr_numkeys(ctr);
-       sorted_subkeys = talloc_array(ctr, char *, num_subkeys);
-       if (sorted_subkeys == NULL) {
-               /* don't treat this as an error */
+       if (value.dsize == 0) {
+               DEBUG(10, ("regdb_key_exists: subkeylist-record for key "
+                         "[%s] is empty: Could be a deleted record in a "
+                         "clustered (ctdb) environment?\n",
+                         path));
                goto done;
        }
 
-       len = 4 + 4*num_subkeys;
-
-       for (i = 0; i < num_subkeys; i++) {
-               sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
-                                       regsubkey_ctr_specific_key(ctr, i));
-               if (sorted_subkeys[i] == NULL) {
-                       /* don't treat this as an error */
-                       goto done;
-               }
-               len += strlen(sorted_subkeys[i])+1;
-       }
-
-       TYPESAFE_QSORT(sorted_subkeys, num_subkeys, cmp_keynames);
-
-       buf = talloc_array(ctr, char, len);
-       if (buf == NULL) {
-               /* don't treat this as an error */
+       len = tdb_unpack(value.dptr, value.dsize, "d", &num_items);
+       if (len == (int32_t)-1) {
+               DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
+                         "[%s] is invalid: Could not parse initial 4-byte "
+                         "counter. record data length is %u.\n",
+                         path, (unsigned int)value.dsize));
                goto done;
        }
-       p = buf + 4 + 4*num_subkeys;
-
-       SIVAL(buf, 0, num_subkeys);
-
-       for (i=0; i < num_subkeys; i++) {
-               ptrdiff_t offset = p - buf;
-               SIVAL(buf, 4 + 4*i, offset);
-               strlcpy(p, sorted_subkeys[i], len-offset);
-               p += strlen(sorted_subkeys[i]) + 1;
-       }
-
-       status = dbwrap_store_bystring(
-               db, sorted_ctx->sorted_keyname, make_tdb_data((uint8_t *)buf,
-               len),
-               TDB_REPLACE);
 
-done:
-       talloc_free(ctr);
-       return status;
-}
-
-static NTSTATUS create_sorted_subkeys_internal(const char *key,
-                                              const char *sorted_keyname)
-{
-       NTSTATUS status;
-       struct create_sorted_subkeys_context sorted_ctx;
-
-       sorted_ctx.key = key;
-       sorted_ctx.sorted_keyname = sorted_keyname;
-
-       status = dbwrap_trans_do(regdb,
-                                create_sorted_subkeys_action,
-                                &sorted_ctx);
-
-       return status;
-}
-
-static NTSTATUS create_sorted_subkeys(const char *key)
-{
-       char *sorted_subkeys_keyname;
-       NTSTATUS status;
-
-       sorted_subkeys_keyname = talloc_asprintf(talloc_tos(), "%s\\%s",
-                                                REG_SORTED_SUBKEYS_PREFIX,
-                                                key);
-       if (sorted_subkeys_keyname == NULL) {
-               status = NT_STATUS_NO_MEMORY;
-               goto done;
-       }
-
-       status = create_sorted_subkeys_internal(key, sorted_subkeys_keyname);
-
-done:
-       return status;
-}
-
-struct scan_subkey_state {
-       char *name;
-       bool scanned;
-       bool found;
-};
-
-static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
-                                void *private_data)
-{
-       struct scan_subkey_state *state =
-               (struct scan_subkey_state *)private_data;
-       uint32_t num_subkeys;
-       uint32_t l, u;
-
-       if (data.dsize < sizeof(uint32_t)) {
-               return -1;
-       }
-
-       state->scanned = true;
-       state->found = false;
-
-       tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
-
-       l = 0;
-       u = num_subkeys;
-
-       while (l < u) {
-               uint32_t idx = (l+u)/2;
-               char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
-               int comparison = strcmp(state->name, s);
-
-               if (comparison < 0) {
-                       u = idx;
-               } else if (comparison > 0) {
-                       l = idx + 1;
-               } else {
-                       state->found = true;
-                       return 0;
-               }
-       }
-       return 0;
-}
-
-static bool scan_parent_subkeys(struct db_context *db, const char *parent,
-                               const char *name)
-{
-       char *path = NULL;
-       char *key = NULL;
-       struct scan_subkey_state state = { 0, };
-       bool result = false;
-       int res;
-
-       state.name = NULL;
-
-       path = normalize_reg_path(talloc_tos(), parent);
-       if (path == NULL) {
-               goto fail;
-       }
-
-       key = talloc_asprintf(talloc_tos(), "%s\\%s",
-                             REG_SORTED_SUBKEYS_PREFIX, path);
-       if (key == NULL) {
-               goto fail;
-       }
-
-       state.name = talloc_strdup_upper(talloc_tos(), name);
-       if (state.name == NULL) {
-               goto fail;
-       }
-       state.scanned = false;
-
-       res = db->parse_record(db, string_term_tdb_data(key),
-                              parent_subkey_scanner, &state);
-
-       if (state.scanned) {
-               result = state.found;
-       } else {
-               NTSTATUS status;
-
-               res = db->transaction_start(db);
-               if (res != 0) {
-                       DEBUG(0, ("error starting transaction\n"));
-                       goto fail;
-               }
-
-               DEBUG(2, (__location__ " WARNING: recreating the sorted "
-                         "subkeys cache for key '%s' from scan_parent_subkeys "
-                         "this should not happen (too frequently)...\n",
-                         path));
+       /*
+        * Note: the tdb_unpack check above implies that len <= value.dsize
+        */
+       buflen = value.dsize - len;
+       buf = (const char *)value.dptr + len;
 
-               status = create_sorted_subkeys_internal(path, key);
-               if (!NT_STATUS_IS_OK(status)) {
-                       res = db->transaction_cancel(db);
-                       if (res != 0) {
-                               smb_panic("Failed to cancel transaction.");
-                       }
-                       goto fail;
-               }
+       len = 0;
 
-               res = db->parse_record(db, string_term_tdb_data(key),
-                                      parent_subkey_scanner, &state);
-               if ((res == 0) && (state.scanned)) {
-                       result = state.found;
+       for (i = 0; i < num_items; i++) {
+               if (buflen == 0) {
+                       break;
                }
-
-               res = db->transaction_commit(db);
-               if (res != 0) {
-                       DEBUG(0, ("error committing transaction\n"));
-                       result = false;
+               len = strnlen(buf, buflen) + 1;
+               if (buflen < len) {
+                       DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record "
+                                 "for key [%s] is corrupt: %u items expected, "
+                                 "item number %u is not zero terminated.\n",
+                                 path, num_items, i+1));
+                       goto done;
                }
-       }
-
- fail:
-       TALLOC_FREE(path);
-       TALLOC_FREE(state.name);
-       return result;
-}
-
-/**
- * Check for the existence of a key.
- *
- * Existence of a key is authoritatively defined by its
- * existence in the list of subkeys of its parent key.
- * The exeption of this are keys without a parent key,
- * i.e. the "base" keys (HKLM, HKCU, ...).
- */
-static bool regdb_key_exists(struct db_context *db, const char *key)
-{
-       TALLOC_CTX *mem_ctx = talloc_stackframe();
-       TDB_DATA value;
-       bool ret = false;
-       char *path, *p;
 
-       if (key == NULL) {
-               goto done;
+               buf += len;
+               buflen -= len;
        }
 
-       path = normalize_reg_path(mem_ctx, key);
-       if (path == NULL) {
-               DEBUG(0, ("out of memory! (talloc failed)\n"));
+       if (buflen > 0) {
+               DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
+                         "[%s] is corrupt: %u items expected and found, but "
+                         "the record contains additional %u bytes\n",
+                         path, num_items, buflen));
                goto done;
        }
 
-       if (*path == '\0') {
+       if (i < num_items) {
+               DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
+                         "[%s] is corrupt: %u items expected, but only %u "
+                         "items found.\n",
+                         path, num_items, i+1));
                goto done;
        }
 
-       p = strrchr(path, '\\');
-       if (p == NULL) {
-               /* this is a base key */
-               value = regdb_fetch_key_internal(db, mem_ctx, path);
-               ret = (value.dptr != NULL);
-       } else {
-               *p = '\0';
-               ret = scan_parent_subkeys(db, path, p+1);
-       }
+       ret = true;
 
 done:
        TALLOC_FREE(mem_ctx);
@@ -1636,6 +1516,9 @@ static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
                goto done;
        }
 
+       werr = regsubkey_ctr_reinit(ctr);
+       W_ERROR_NOT_OK_GOTO_DONE(werr);
+
        werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
        W_ERROR_NOT_OK_GOTO_DONE(werr);
 
@@ -1655,9 +1538,6 @@ static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
                goto done;
        }
 
-       werr = regsubkey_ctr_reinit(ctr);
-       W_ERROR_NOT_OK_GOTO_DONE(werr);
-
        for (i=0; i<num_items; i++) {
                len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
                werr = regsubkey_ctr_addkey(ctr, subkeyname);