56f38e7d4bace04294e65a2b2667a02cb63ae650
[metze/samba/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-2007
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "lib/registry/registry.h"
23 #include "lib/registry/tools/common.h"
24 #include "lib/events/events.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "param/param.h"
27
28 /**
29  * Print a registry key recursively
30  *
31  * @param level Level at which to print
32  * @param p Key to print
33  * @param fullpath Whether the full pat hshould be printed or just the last bit
34  * @param novals Whether values should not be printed
35  */
36 static void print_tree(unsigned int level, struct registry_key *p,
37                        const char *name,
38                        bool fullpath, bool novals)
39 {
40         struct registry_key *subkey;
41         const char *valuename, *keyname;
42         uint32_t valuetype;
43         DATA_BLOB valuedata;
44         struct security_descriptor *sec_desc;
45         WERROR error;
46         unsigned int i;
47         TALLOC_CTX *mem_ctx;
48
49         for(i = 0; i < level; i++) putchar(' ');
50         puts(name);
51
52         mem_ctx = talloc_init("print_tree");
53         for (i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx,
54                                                                       p,
55                                                                       i,
56                                                                       &keyname,
57                                                                       NULL,
58                                                                       NULL)); i++) {
59
60                 SMB_ASSERT(strlen(keyname) > 0);
61                 if (!W_ERROR_IS_OK(reg_open_key(mem_ctx, p, keyname, &subkey)))
62                         continue;
63
64                 print_tree(level+1, subkey, (fullpath && strlen(name))?
65                                                talloc_asprintf(mem_ctx, "%s\\%s",
66                                                                name, keyname):
67                                                keyname, fullpath, novals);
68                 talloc_free(subkey);
69         }
70         talloc_free(mem_ctx);
71
72         if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
73                 DEBUG(0, ("Error occurred while fetching subkeys for '%s': %s\n",
74                                   name, win_errstr(error)));
75         }
76
77         if (!novals) {
78                 mem_ctx = talloc_init("print_tree");
79                 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(
80                         mem_ctx, p, i, &valuename, &valuetype, &valuedata));
81                         i++) {
82                         unsigned int j;
83                         for(j = 0; j < level+1; j++) putchar(' ');
84                         printf("%s\n",  reg_val_description(mem_ctx,
85                                 valuename, valuetype, valuedata));
86                 }
87                 talloc_free(mem_ctx);
88
89                 if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
90                         DEBUG(0, ("Error occurred while fetching values for '%s': %s\n",
91                                 name, win_errstr(error)));
92                 }
93         }
94
95         mem_ctx = talloc_init("sec_desc");
96         if (!W_ERROR_IS_OK(reg_get_sec_desc(mem_ctx, p, &sec_desc))) {
97                 DEBUG(0, ("Error getting security descriptor\n"));
98         }
99         talloc_free(mem_ctx);
100 }
101
102 int main(int argc, const char **argv)
103 {
104         int opt;
105         unsigned int i;
106         const char *file = NULL;
107         const char *remote = NULL;
108         poptContext pc;
109         struct registry_context *h = NULL;
110         struct registry_key *start_key = NULL;
111         struct tevent_context *ev_ctx;
112         WERROR error;
113         bool fullpath = false, no_values = false;
114         struct poptOption long_options[] = {
115                 POPT_AUTOHELP
116                 {"file", 'F', POPT_ARG_STRING, &file, 0, "file path", NULL },
117                 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL },
118                 {"fullpath", 'f', POPT_ARG_NONE, &fullpath, 0, "show full paths", NULL},
119                 {"no-values", 'V', POPT_ARG_NONE, &no_values, 0, "don't show values", NULL},
120                 POPT_COMMON_SAMBA
121                 POPT_COMMON_CREDENTIALS
122                 POPT_COMMON_VERSION
123                 { NULL }
124         };
125
126         pc = poptGetContext(argv[0], argc, argv, long_options,0);
127
128         while((opt = poptGetNextOpt(pc)) != -1) {
129         }
130
131         ev_ctx = s4_event_context_init(NULL);
132
133         if (remote != NULL) {
134                 h = reg_common_open_remote(remote, ev_ctx, cmdline_lp_ctx, cmdline_credentials);
135         } else if (file != NULL) {
136                 start_key = reg_common_open_file(file, ev_ctx, cmdline_lp_ctx, cmdline_credentials);
137         } else {
138                 h = reg_common_open_local(cmdline_credentials, ev_ctx, cmdline_lp_ctx);
139         }
140
141         if (h == NULL && start_key == NULL)
142                 return 1;
143
144         poptFreeContext(pc);
145
146         error = WERR_OK;
147
148         if (start_key != NULL) {
149                 print_tree(0, start_key, "", fullpath, no_values);
150         } else {
151                 for(i = 0; reg_predefined_keys[i].handle; i++) {
152                         error = reg_get_predefined_key(h,
153                                                        reg_predefined_keys[i].handle,
154                                                        &start_key);
155                         if (!W_ERROR_IS_OK(error)) {
156                                 fprintf(stderr, "Skipping %s: %s\n",
157                                         reg_predefined_keys[i].name,
158                                         win_errstr(error));
159                                 continue;
160                         }
161                         SMB_ASSERT(start_key != NULL);
162                         print_tree(0, start_key, reg_predefined_keys[i].name,
163                                    fullpath, no_values);
164                 }
165         }
166
167         return 0;
168 }