r10026: Move registry header file to lib/registry
[bbaumbach/samba-autobuild/.git] / source4 / lib / registry / tools / regtree.c
index eb32b1e55ea6908f44a8b4991b7154559400b96a..c24e66412f4ddb9c7b22970b99a51e310d54d16a 100644 (file)
 */
 
 #include "includes.h"
+#include "dynconfig.h"
+#include "lib/registry/registry.h"
+#include "lib/cmdline/popt_common.h"
 
-void print_tree(int l, REG_KEY *p, int fullpath, int novals)
+static void print_tree(int l, struct registry_key *p, int fullpath, int novals)
 {
-       REG_KEY *subkey;
-       REG_VAL *value;
+       struct registry_key *subkey;
+       struct registry_value *value;
+       struct security_descriptor *sec_desc;
        WERROR error;
        int i;
+       TALLOC_CTX *mem_ctx;
 
        for(i = 0; i < l; i++) putchar(' ');
-       if(fullpath) printf("%s\n", reg_key_get_path(p));
-       else printf("%s\n", reg_key_name(p));
+       
+       /* Hive name */
+       if(p->hive->root == p) {
+               if(p->hive->root->name) printf("%s\n", p->hive->root->name); else printf("<No Name>\n");
+       } else {
+               if(!p->name) printf("<No Name>\n");
+               if(fullpath) printf("%s\n", p->path);
+               else printf("%s\n", p->name);
+       }
 
-       for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(p, i, &subkey)); i++) {
+       mem_ctx = talloc_init("print_tree");
+       for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, p, i, &subkey)); i++) {
                print_tree(l+1, subkey, fullpath, novals);
-               reg_key_free(subkey);
        }
+       talloc_free(mem_ctx);
 
        if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
-               DEBUG(0, ("Error occured while fetching subkeys for '%s': %s\n", reg_key_get_path(p), win_errstr(error)));
+               DEBUG(0, ("Error occured while fetching subkeys for '%s': %s\n", p->path, win_errstr(error)));
        }
 
        if(!novals) {
-               for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(p, i, &value)); i++) {
+               mem_ctx = talloc_init("print_tree");
+               for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(mem_ctx, p, i, &value)); i++) {
                        int j;
                        char *desc;
                        for(j = 0; j < l+1; j++) putchar(' ');
-                       desc = reg_val_description(value);
+                       desc = reg_val_description(mem_ctx, value);
                        printf("%s\n", desc);
-                       free(desc);
-                       reg_val_free(value);
                }
+               talloc_free(mem_ctx);
 
                if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
-                       DEBUG(0, ("Error occured while fetching subkeys for '%s'\n", reg_key_get_path(p)));
+                       DEBUG(0, ("Error occured while fetching values for '%s': %s\n", p->path, win_errstr(error)));
                }
        }
+
+       mem_ctx = talloc_init("sec_desc");
+       if (NT_STATUS_IS_ERR(reg_get_sec_desc(mem_ctx, p, &sec_desc))) {
+               DEBUG(0, ("Error getting security descriptor\n"));
+       }
+       talloc_free(mem_ctx);
 }
 
-int main (int argc, char **argv)
+int main(int argc, char **argv)
 {
-       uint32  setparms, checkparms;
-       int opt;
-       char *backend = "dir", *credentials = NULL;
+       int opt, i;
+       const char *backend = NULL;
+       const char *remote = NULL;
        poptContext pc;
-       REG_KEY *root;
-       REG_HANDLE *h;
+       struct registry_context *h = NULL;
+       struct registry_key *root = NULL;
        WERROR error;
        int fullpath = 0, no_values = 0;
        struct poptOption long_options[] = {
                POPT_AUTOHELP
                {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
                {"fullpath", 'f', POPT_ARG_NONE, &fullpath, 0, "show full paths", NULL},
-               {"credentials", 'c', POPT_ARG_STRING, &credentials, 0, "credentials (user%password)", NULL},
+               {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL },
                {"no-values", 'V', POPT_ARG_NONE, &no_values, 0, "don't show values", NULL},
+               POPT_COMMON_SAMBA       
+               POPT_COMMON_CREDENTIALS 
                POPT_TABLEEND
        };
 
+       regtree_init_subsystems;
+
        pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
        
        while((opt = poptGetNextOpt(pc)) != -1) {
        }
 
-       setup_logging("regtree", True);
+       if (remote) {
+               error = reg_open_remote(&h, cmdline_credentials, remote, NULL);
+       } else if (backend) {
+           error = reg_open_hive(NULL, backend, poptGetArg(pc), NULL, &root);
+       } else {
+               error = reg_open_local (&h);
+       }
 
-       error = reg_open(backend, poptPeekArg(pc), credentials, &h);
        if(!W_ERROR_IS_OK(error)) {
                fprintf(stderr, "Unable to open '%s' with backend '%s':%s \n", poptGetArg(pc), backend, win_errstr(error));
                return 1;
        }
        poptFreeContext(pc);
 
-       error = reg_get_root(h, &root);
-       if(!W_ERROR_IS_OK(error)) return 1;
-
-       print_tree(0, root, fullpath, no_values);
+       error = WERR_OK;
        
+       if (!h) {
+               print_tree(0, root, fullpath, no_values);
+       } else {
+               for(i = HKEY_CLASSES_ROOT; i < HKEY_PERFORMANCE_NLSTEXT; i++) {
+                       error = reg_get_predefined_key(h, i, &root);
+                       if (!W_ERROR_IS_OK(error)) {
+                               fprintf(stderr, "Skipping %s\n", reg_get_predef_name(i));
+                               continue;
+                       }
+                       print_tree(0, root, fullpath, no_values);
+               }
+       }
+
        return 0;
 }