Registry tool "regtree": Handle the default attribute in the right way
[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(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;
42         const char *keyname;
43         uint32_t valuetype;
44         DATA_BLOB valuedata;
45         struct security_descriptor *sec_desc;
46         WERROR error;
47         int i;
48         TALLOC_CTX *mem_ctx;
49
50         for(i = 0; i < level; i++) putchar(' '); 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                 SMB_ASSERT(strlen(keyname) > 0);
60                 if (!W_ERROR_IS_OK(reg_open_key(mem_ctx, p, keyname, &subkey)))
61                         continue;
62                 print_tree(level+1, subkey, (fullpath && strlen(name))?
63                                                 talloc_asprintf(mem_ctx, "%s\\%s",
64                                                                 name, keyname):
65                                                 keyname, fullpath, novals);
66         }
67         talloc_free(mem_ctx);
68
69         if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
70                 DEBUG(0, ("Error occured while fetching subkeys for '%s': %s\n",
71                                   name, win_errstr(error)));
72         }
73
74         if (!novals) {
75                 mem_ctx = talloc_init("print_tree");
76                 /* default value */
77                 if (W_ERROR_IS_OK(reg_key_get_value_by_index(mem_ctx, p, 0,
78                         &valuename, &valuetype, &valuedata))) {
79                         int j;
80                         for(j = 0; j < level+1; j++) putchar(' ');
81                         printf("%s\n",  reg_val_description(mem_ctx,
82                                 lp_iconv_convenience(cmdline_lp_ctx),
83                                 "(Default)", valuetype, valuedata));
84                 }
85                 /* other values */
86                 for(i = 1; W_ERROR_IS_OK(error = reg_key_get_value_by_index(
87                         mem_ctx, p, i, &valuename, &valuetype, &valuedata));
88                         i++) {
89                         int j;
90                         for(j = 0; j < level+1; j++) putchar(' ');
91                         printf("%s\n",  reg_val_description(mem_ctx,
92                                 lp_iconv_convenience(cmdline_lp_ctx), valuename,
93                                 valuetype, valuedata));
94                 }
95                 talloc_free(mem_ctx);
96
97                 if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
98                         DEBUG(0, ("Error occured while fetching values for '%s': %s\n",
99                                 name, win_errstr(error)));
100                 }
101         }
102
103         mem_ctx = talloc_init("sec_desc");
104         if (NT_STATUS_IS_ERR(reg_get_sec_desc(mem_ctx, p, &sec_desc))) {
105                 DEBUG(0, ("Error getting security descriptor\n"));
106         }
107         talloc_free(mem_ctx);
108 }
109
110 int main(int argc, char **argv)
111 {
112         int opt, i;
113         const char *file = NULL;
114         const char *remote = NULL;
115         poptContext pc;
116         struct registry_context *h = NULL;
117         struct registry_key *start_key = NULL;
118         struct event_context *ev_ctx;
119         WERROR error;
120         bool fullpath = false, no_values = false;
121         struct poptOption long_options[] = {
122                 POPT_AUTOHELP
123                 {"file", 'F', POPT_ARG_STRING, &file, 0, "file path", NULL },
124                 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL },
125                 {"fullpath", 'f', POPT_ARG_NONE, &fullpath, 0, "show full paths", NULL},
126                 {"no-values", 'V', POPT_ARG_NONE, &no_values, 0, "don't show values", NULL},
127                 POPT_COMMON_SAMBA
128                 POPT_COMMON_CREDENTIALS
129                 POPT_COMMON_VERSION
130                 { NULL }
131         };
132
133         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
134
135         while((opt = poptGetNextOpt(pc)) != -1) {
136         }
137
138         ev_ctx = s4_event_context_init(NULL);
139
140         if (remote != NULL) {
141                 h = reg_common_open_remote(remote, ev_ctx, cmdline_lp_ctx, cmdline_credentials);
142         } else if (file != NULL) {
143                 start_key = reg_common_open_file(file, ev_ctx, cmdline_lp_ctx, cmdline_credentials);
144         } else {
145                 h = reg_common_open_local(cmdline_credentials, ev_ctx, cmdline_lp_ctx);
146         }
147
148         if (h == NULL && start_key == NULL)
149                 return 1;
150
151         poptFreeContext(pc);
152
153         error = WERR_OK;
154
155         if (start_key != NULL) {
156                 print_tree(0, start_key, "", fullpath, no_values);
157         } else {
158                 for(i = 0; reg_predefined_keys[i].handle; i++) {
159                         error = reg_get_predefined_key(h,
160                                                        reg_predefined_keys[i].handle,
161                                                        &start_key);
162                         if (!W_ERROR_IS_OK(error)) {
163                                 fprintf(stderr, "Skipping %s: %s\n",
164                                         reg_predefined_keys[i].name,
165                                         win_errstr(error));
166                                 continue;
167                         }
168                         SMB_ASSERT(start_key != NULL);
169                         print_tree(0, start_key, reg_predefined_keys[i].name,
170                                    fullpath, no_values);
171                 }
172         }
173
174         return 0;
175 }