Registry tools "regshell" and "regtree": Small fixup's
[Samba/ekacnet.git] / source4 / lib / registry / tools / regtree.c
blob2175f9c9d3432cefbede8c34318200ffcdb1bbfb
1 /*
2 Unix SMB/CIFS implementation.
3 simple registry frontend
5 Copyright (C) Jelmer Vernooij 2004-2007
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.
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.
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/>.
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"
28 /**
29 * Print a registry key recursively
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
36 static void print_tree(int level, struct registry_key *p,
37 const char *name,
38 bool fullpath, bool novals)
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 int i;
47 TALLOC_CTX *mem_ctx;
49 for(i = 0; i < level; i++) putchar(' '); puts(name);
51 mem_ctx = talloc_init("print_tree");
52 for (i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx,
55 &keyname,
56 NULL,
57 NULL)); i++) {
58 SMB_ASSERT(strlen(keyname) > 0);
59 if (!W_ERROR_IS_OK(reg_open_key(mem_ctx, p, keyname, &subkey)))
60 continue;
61 print_tree(level+1, subkey, (fullpath && strlen(name))?
62 talloc_asprintf(mem_ctx, "%s\\%s",
63 name, keyname):
64 keyname, fullpath, novals);
66 talloc_free(mem_ctx);
68 if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
69 DEBUG(0, ("Error occured while fetching subkeys for '%s': %s\n",
70 name, win_errstr(error)));
73 if (!novals) {
74 mem_ctx = talloc_init("print_tree");
75 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(
76 mem_ctx, p, i, &valuename, &valuetype, &valuedata));
77 i++) {
78 int j;
79 for(j = 0; j < level+1; j++) putchar(' ');
80 printf("%s\n", reg_val_description(mem_ctx,
81 lp_iconv_convenience(cmdline_lp_ctx), valuename,
82 valuetype, valuedata));
84 talloc_free(mem_ctx);
87 mem_ctx = talloc_init("sec_desc");
88 if (NT_STATUS_IS_ERR(reg_get_sec_desc(mem_ctx, p, &sec_desc))) {
89 DEBUG(0, ("Error getting security descriptor\n"));
91 talloc_free(mem_ctx);
94 int main(int argc, char **argv)
96 int opt, i;
97 const char *file = NULL;
98 const char *remote = NULL;
99 poptContext pc;
100 struct registry_context *h = NULL;
101 struct registry_key *start_key = NULL;
102 struct event_context *ev_ctx;
103 WERROR error;
104 bool fullpath = false, no_values = false;
105 struct poptOption long_options[] = {
106 POPT_AUTOHELP
107 {"file", 'F', POPT_ARG_STRING, &file, 0, "file path", NULL },
108 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL },
109 {"fullpath", 'f', POPT_ARG_NONE, &fullpath, 0, "show full paths", NULL},
110 {"no-values", 'V', POPT_ARG_NONE, &no_values, 0, "don't show values", NULL},
111 POPT_COMMON_SAMBA
112 POPT_COMMON_CREDENTIALS
113 POPT_COMMON_VERSION
114 { NULL }
117 pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
119 while((opt = poptGetNextOpt(pc)) != -1) {
122 ev_ctx = s4_event_context_init(NULL);
124 if (remote != NULL) {
125 h = reg_common_open_remote(remote, ev_ctx, cmdline_lp_ctx, cmdline_credentials);
126 } else if (file != NULL) {
127 start_key = reg_common_open_file(file, ev_ctx, cmdline_lp_ctx, cmdline_credentials);
128 } else {
129 h = reg_common_open_local(cmdline_credentials, ev_ctx, cmdline_lp_ctx);
132 if (h == NULL && start_key == NULL)
133 return 1;
135 poptFreeContext(pc);
137 error = WERR_OK;
139 if (start_key != NULL) {
140 print_tree(0, start_key, "", fullpath, no_values);
141 } else {
142 for(i = 0; reg_predefined_keys[i].handle; i++) {
143 error = reg_get_predefined_key(h,
144 reg_predefined_keys[i].handle,
145 &start_key);
146 if (!W_ERROR_IS_OK(error)) {
147 fprintf(stderr, "Skipping %s: %s\n",
148 reg_predefined_keys[i].name,
149 win_errstr(error));
150 continue;
152 SMB_ASSERT(start_key != NULL);
153 print_tree(0, start_key, reg_predefined_keys[i].name,
154 fullpath, no_values);
158 return 0;