r3368: Default to rpc backend with binding "ncalrpc:" if no backend was specified...
[ira/wip.git] / source4 / lib / registry / tools / regtree.c
1 /* 
2    Unix SMB/CIFS implementation.
3    simple registry frontend
4    
5    Copyright (C) Jelmer Vernooij 2004
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 static void print_tree(int l, struct registry_key *p, int fullpath, int novals)
25 {
26         struct registry_key *subkey;
27         struct registry_value *value;
28         WERROR error;
29         int i;
30         TALLOC_CTX *mem_ctx;
31
32         for(i = 0; i < l; i++) putchar(' ');
33         
34         /* Hive name */
35         if(p->hive->root == p) {
36                 if(p->hive->name) printf("%s\n", p->hive->name); else printf("<No Name>\n");
37         } else {
38                 if(!p->name) printf("<No Name>\n");
39                 if(fullpath) printf("%s\n", p->path);
40                 else printf("%s\n", p->name);
41         }
42
43         mem_ctx = talloc_init("print_tree");
44         for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, p, i, &subkey)); i++) {
45                 print_tree(l+1, subkey, fullpath, novals);
46         }
47         talloc_destroy(mem_ctx);
48
49         if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
50                 DEBUG(0, ("Error occured while fetching subkeys for '%s': %s\n", p->path, win_errstr(error)));
51         }
52
53         if(!novals) {
54                 mem_ctx = talloc_init("print_tree");
55                 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(mem_ctx, p, i, &value)); i++) {
56                         int j;
57                         char *desc;
58                         for(j = 0; j < l+1; j++) putchar(' ');
59                         desc = reg_val_description(mem_ctx, value);
60                         printf("%s\n", desc);
61                 }
62                 talloc_destroy(mem_ctx);
63
64                 if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
65                         DEBUG(0, ("Error occured while fetching values for '%s': %s\n", p->path, win_errstr(error)));
66                 }
67         }
68 }
69
70  int main(int argc, char **argv)
71 {
72         int opt, i;
73         const char *backend = "rpc";
74         const char *credentials = NULL;
75         poptContext pc;
76         struct registry_context *h;
77         WERROR error;
78         int fullpath = 0, no_values = 0;
79         struct poptOption long_options[] = {
80                 POPT_AUTOHELP
81                 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
82                 {"fullpath", 'f', POPT_ARG_NONE, &fullpath, 0, "show full paths", NULL},
83                 {"credentials", 'c', POPT_ARG_STRING, &credentials, 0, "credentials (user%password)", NULL},
84                 {"no-values", 'V', POPT_ARG_NONE, &no_values, 0, "don't show values", NULL},
85                 POPT_TABLEEND
86         };
87
88
89         if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
90                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
91         }
92
93
94         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
95         
96         while((opt = poptGetNextOpt(pc)) != -1) {
97         }
98
99         setup_logging("regtree", True);
100
101         error = reg_open(&h, backend, poptPeekArg(pc), credentials);
102         if(!W_ERROR_IS_OK(error)) {
103                 fprintf(stderr, "Unable to open '%s' with backend '%s':%s \n", poptGetArg(pc), backend, win_errstr(error));
104                 return 1;
105         }
106         poptFreeContext(pc);
107
108         error = WERR_OK;
109
110         for(i = 0; i < h->num_hives; i++) {
111                 print_tree(0, h->hives[i]->root, fullpath, no_values);
112         }
113         
114         return 0;
115 }