r1118: Read-only enumeration of keys in the LDB backend works now :-)
authorJelmer Vernooij <jelmer@samba.org>
Sat, 12 Jun 2004 15:12:23 +0000 (15:12 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 17:56:38 +0000 (12:56 -0500)
(This used to be commit 456f97eed05047b20acceb50708b47209c37ca20)

source4/lib/registry/common/reg_interface.c
source4/lib/registry/reg_backend_ldb/reg_backend_ldb.c

index 6ad7ee69cb618fe5b5ea515b0bff9139760ac1e5..0a0cf05c2e3f9211cd8554a3045e4d4183554f0c 100644 (file)
@@ -85,6 +85,7 @@ static struct reg_init_function_entry *reg_find_backend_entry(const char *name)
        return NULL;
 }
 
+/* Check whether a certain backend is present */
 BOOL reg_has_backend(const char *backend)
 {
        return reg_find_backend_entry(backend) != NULL?True:False;
@@ -129,6 +130,7 @@ WERROR reg_open(const char *backend, const char *location, const char *credentia
        return werr;
 }
 
+/* Open a key by name (including the hive name!) */
 WERROR reg_open_key_abs(REG_HANDLE *handle, const char *name, REG_KEY **result)
 {
        REG_KEY *hive;
@@ -195,9 +197,13 @@ WERROR reg_open_key(REG_KEY *parent, const char *name, REG_KEY **result)
 
        mem_ctx = talloc_init("mem_ctx");
 
-       fullname = talloc_asprintf(mem_ctx, "%s%s%s", reg_key_get_path(parent), strlen(reg_key_get_path(parent))?"\\":"", name);
+       fullname = talloc_asprintf(mem_ctx, "%s%s%s", 
+                                               reg_key_get_path(parent), 
+                                               strlen(reg_key_get_path(parent))?"\\":"", 
+                                               name);
 
-       error = parent->handle->functions->open_key(parent->handle, parent->hive, fullname, result);
+       error = parent->handle->functions->open_key(parent->handle, 
+                                                                                               parent->hive, fullname, result);
 
        if(!W_ERROR_IS_OK(error)) {
                talloc_destroy(mem_ctx);
@@ -205,7 +211,8 @@ WERROR reg_open_key(REG_KEY *parent, const char *name, REG_KEY **result)
        }
                
        (*result)->handle = parent->handle;
-       (*result)->path = talloc_asprintf((*result)->mem_ctx, "%s\\%s", reg_key_get_path_abs(parent), (*result)->name);
+       (*result)->path = talloc_asprintf((*result)->mem_ctx, "%s\\%s", 
+                                                                reg_key_get_path_abs(parent), (*result)->name);
        (*result)->hive = parent->hive;
        talloc_steal(mem_ctx, (*result)->mem_ctx, fullname);
 
@@ -225,7 +232,8 @@ WERROR reg_key_get_value_by_index(REG_KEY *key, int idx, REG_VAL **val)
 
        } else if(key->handle->functions->fetch_values) {
                if(!key->cache_values)
-                       key->handle->functions->fetch_values(key, &key->cache_values_count, &key->cache_values);
+                       key->handle->functions->fetch_values(key, 
+                                                               &key->cache_values_count, &key->cache_values);
                
                if(idx < key->cache_values_count && idx >= 0) {
                        *val = reg_val_dup(key->cache_values[idx]);
@@ -251,7 +259,8 @@ WERROR reg_key_num_subkeys(REG_KEY *key, int *count)
 
        if(key->handle->functions->fetch_subkeys) {
                if(!key->cache_subkeys) 
-                       key->handle->functions->fetch_subkeys(key, &key->cache_subkeys_count, &key->cache_subkeys);
+                       key->handle->functions->fetch_subkeys(key, 
+                                                       &key->cache_subkeys_count, &key->cache_subkeys);
 
                *count = key->cache_subkeys_count;
                return WERR_OK;
@@ -304,7 +313,8 @@ WERROR reg_key_get_subkey_by_index(REG_KEY *key, int idx, REG_KEY **subkey)
                if(!NT_STATUS_IS_OK(status)) return status;
        } else if(key->handle->functions->fetch_subkeys) {
                if(!key->cache_subkeys) 
-                       key->handle->functions->fetch_subkeys(key, &key->cache_subkeys_count, &key->cache_subkeys);
+                       key->handle->functions->fetch_subkeys(key, 
+                                                               &key->cache_subkeys_count, &key->cache_subkeys);
 
                if(idx < key->cache_subkeys_count) {
                        *subkey = reg_key_dup(key->cache_subkeys[idx]);
@@ -315,7 +325,8 @@ WERROR reg_key_get_subkey_by_index(REG_KEY *key, int idx, REG_KEY **subkey)
                return WERR_NOT_SUPPORTED;
        }
 
-       (*subkey)->path = talloc_asprintf((*subkey)->mem_ctx, "%s\\%s", reg_key_get_path_abs(key), (*subkey)->name);
+       (*subkey)->path = talloc_asprintf((*subkey)->mem_ctx, "%s\\%s", 
+                                                                       reg_key_get_path_abs(key), (*subkey)->name);
        (*subkey)->handle = key->handle;
        (*subkey)->hive = key->hive;
 
index 0633c94616045511027dc918974adf96db758902..408ce52e4486aeffaba59a1274f6837df79caf5b 100644 (file)
 #include "includes.h"
 #include "lib/registry/common/registry.h"
 
-static char *reg_path_to_ldb(TALLOC_CTX *mem_ctx, const char *path)
+static char *reg_path_to_ldb(TALLOC_CTX *mem_ctx, const char *path, const char *add)
 {
-       char *ret = talloc_strdup(mem_ctx, "(dn=");
-       char *begin = (char *)path;
-       char *end = NULL;
-
-       while(begin) {
-               end = strchr(begin, '\\');
-               if(end)end = '\0';
-               if(end - begin != 0) ret = talloc_asprintf_append(mem_ctx, ret, "key=%s,", begin);
+       char *ret = talloc_strdup(mem_ctx, "");
+       char *mypath = strdup(path);
+       char *end = mypath, *begin;
+
+       if(add) 
+               ret = talloc_asprintf_append(mem_ctx, ret, "%s", add);
+
+       while(end) {
+               char *keyname;
+               begin = strrchr(end, '\\');
+
+               if(begin) keyname = begin + 1;
+               else keyname = mypath;
+
+               if(strlen(keyname))
+                       ret = talloc_asprintf_append(mem_ctx, ret, "key=%s,", keyname);
                        
-               if(end) {
-                       *end = '\\';
-                       begin = end+1;
-               } else begin = NULL;
+               if(begin) {
+                       begin[0] = '\0';
+                       end = begin-1;
+               } else {
+                       end = NULL;
+               }
        }
 
-       ret[strlen(ret)-1] = ')';
+       SAFE_FREE(mypath);
+
+       ret[strlen(ret)-1] = '\0';
+       
        printf("Doing search for : %s\n", ret);
        return ret;
 }
@@ -50,8 +63,12 @@ static char *reg_path_to_ldb(TALLOC_CTX *mem_ctx, const char *path)
 static WERROR ldb_open_registry(REG_HANDLE *handle, const char *location, const char *credentials)
 {
        struct ldb_context *c;
+
+       if (!location) return WERR_INVALID_PARAM;
        c = ldb_connect(location, 0, NULL);
 
+       ldb_set_debug_stderr(c);
+
        if(!c) return WERR_FOOBAR;
 
        handle->backend_data = c;
@@ -69,18 +86,36 @@ static WERROR ldb_fetch_subkeys(REG_KEY *k, int *count, REG_KEY ***subkeys)
 {
        struct ldb_context *c = k->handle->backend_data;
        char *path;
+       int ret, i, j;
        struct ldb_message **msg;
-       char *ldap_path;
        TALLOC_CTX *mem_ctx = talloc_init("ldb_path");
        REG_KEY *key = NULL;
-       ldap_path = reg_path_to_ldb(mem_ctx, reg_key_get_path(k));
-       
-       if(ldb_search(c, NULL, LDB_SCOPE_ONELEVEL, ldap_path, NULL,&msg) > 0) {
-               key = reg_key_new_abs(reg_key_get_path(k), k->handle, ldap_path);
-               talloc_steal(mem_ctx, key->mem_ctx, ldap_path);
-               /* FIXME */
+
+       ret = ldb_search(c, (char *)k->backend_data, LDB_SCOPE_ONELEVEL, "(key=*)", NULL,&msg);
+
+       if(ret < 0) {
+               DEBUG(0, ("Error getting subkeys for '%s': %s\n", k->backend_data, ldb_errstring(c)));
+               return WERR_FOOBAR;
        }
 
+       *subkeys = talloc_array_p(k->mem_ctx, REG_KEY *, ret);
+       j = 0;
+       for(i = 0; i < ret; i++) {
+               struct ldb_message_element *el;
+               char *name;
+               el = ldb_msg_find_element(msg[i], "key");
+
+               name = el->values[0].data;
+
+               /* Dirty hack to circumvent ldb_tdb bug */
+               if(k->backend_data && !strcmp(msg[i]->dn, (char *)k->backend_data)) continue;
+                       
+               (*subkeys)[j] = reg_key_new_rel(name, k, NULL);
+               (*subkeys)[j]->backend_data = talloc_strdup((*subkeys)[j]->mem_ctx, msg[i]->dn);
+               j++;
+       }
+       *count = j;
+
        ldb_search_free(c, msg);
        talloc_destroy(mem_ctx);
        return WERR_OK;
@@ -88,8 +123,10 @@ static WERROR ldb_fetch_subkeys(REG_KEY *k, int *count, REG_KEY ***subkeys)
 
 static WERROR ldb_get_hive(REG_HANDLE *h, int num, REG_KEY **key)
 {
-       /* FIXME */
-       return WERR_FOOBAR;     
+       if(num != 0) return WERR_NO_MORE_ITEMS;
+       *key = reg_key_new_abs("", h, NULL);
+//     (*key)->backend_data = talloc_strdup((*key)->mem_ctx, "");
+       return WERR_OK;
 }
 
 static WERROR ldb_open_key(REG_HANDLE *h, int num, const char *name, REG_KEY **key)
@@ -97,16 +134,26 @@ static WERROR ldb_open_key(REG_HANDLE *h, int num, const char *name, REG_KEY **k
        struct ldb_context *c = h->backend_data;
        char *path;
        struct ldb_message **msg;
-       char *ldap_path;
+       char *ldap_path, *new_ldap_path;
+       int ret;
        TALLOC_CTX *mem_ctx = talloc_init("ldb_path");
-       ldap_path = reg_path_to_ldb(mem_ctx, name);
+       if(num != 0) return WERR_NO_MORE_ITEMS;
+       ldap_path = reg_path_to_ldb(mem_ctx, name, NULL);
        
-       if(ldb_search(c, NULL, LDB_SCOPE_BASE, ldap_path, NULL,&msg) > 0) {
-               *key = reg_key_new_abs(name, h, ldap_path);
-               talloc_steal(mem_ctx, (*key)->mem_ctx, ldap_path);
-               /* FIXME */
+       ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "*", NULL,&msg);
+
+       if(ret == 0) {
+               return WERR_NO_MORE_ITEMS;
+       } else if(ret < 0) {
+               DEBUG(0, ("Error opening key '%s': %s\n", ldap_path, ldb_errstring(c)));
+               return WERR_FOOBAR;
        }
 
+       *key = reg_key_new_abs(name, h, ldap_path);
+       talloc_steal(mem_ctx, (*key)->mem_ctx, ldap_path);
+       printf("Got something!\n");
+       /* FIXME */
+
        ldb_search_free(c, msg);
        talloc_destroy(mem_ctx);
 
@@ -116,6 +163,7 @@ static WERROR ldb_open_key(REG_HANDLE *h, int num, const char *name, REG_KEY **k
 static struct registry_ops reg_backend_ldb = {
        .name = "ldb",
        .open_registry = ldb_open_registry,
+       .get_hive = ldb_get_hive,
        .close_registry = ldb_close_registry,
        .open_key = ldb_open_key,
        .fetch_subkeys = ldb_fetch_subkeys,