regedit: Print value summary, fix heap corruption and path printing.
[Samba/id10ts.git] / source3 / utils / regedit.c
blobf66999a6143aacf8c1d3d7d1ab50028c829396fb
1 /*
2 * Samba Unix/Linux SMB client library
3 * Registry Editor
4 * Copyright (C) Christopher Davis 2012
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "popt_common.h"
22 #include "lib/util/data_blob.h"
23 #include "lib/registry/registry.h"
24 #include "regedit.h"
25 #include "regedit_treeview.h"
26 #include "regedit_valuelist.h"
27 #include <ncurses.h>
28 #include <menu.h>
30 /* load all available hives */
31 static struct tree_node *load_hives(TALLOC_CTX *mem_ctx,
32 struct registry_context *ctx)
34 const char *hives[] = {
35 "HKEY_CLASSES_ROOT",
36 "HKEY_CURRENT_USER",
37 "HKEY_LOCAL_MACHINE",
38 "HKEY_PERFORMANCE_DATA",
39 "HKEY_USERS",
40 "HKEY_CURRENT_CONFIG",
41 "HKEY_DYN_DATA",
42 "HKEY_PERFORMANCE_TEXT",
43 "HKEY_PERFORMANCE_NLSTEXT",
44 NULL
46 struct tree_node *root, *prev, *node;
47 struct registry_key *key;
48 WERROR rv;
49 size_t i;
51 root = NULL;
52 prev = NULL;
54 for (i = 0; hives[i] != NULL; ++i) {
55 rv = reg_get_predefined_key_by_name(ctx, hives[i], &key);
56 if (!W_ERROR_IS_OK(rv)) {
57 continue;
60 node = tree_node_new(mem_ctx, NULL, hives[i], key);
61 if (node == NULL) {
62 return NULL;
65 if (root == NULL) {
66 root = node;
68 if (prev) {
69 tree_node_append(prev, node);
71 prev = node;
74 return root;
77 /* test navigating available hives */
78 static void display_test_window(TALLOC_CTX *mem_ctx,
79 struct registry_context *ctx)
81 WINDOW *main_window, *path_label;
82 struct value_list *vl;
83 struct tree_view *view;
84 struct tree_node *root, *node;
85 int c;
87 initscr();
88 start_color();
89 cbreak();
90 noecho();
91 keypad(stdscr, TRUE);
93 main_window = newwin(25, 80, 0, 0);
94 SMB_ASSERT(main_window != NULL);
96 keypad(main_window, TRUE);
98 mvwprintw(main_window, 0, 0, "Path: ");
99 path_label = derwin(main_window, 1, 65, 0, 6);
100 wprintw(path_label, "/");
102 root = load_hives(mem_ctx, ctx);
103 SMB_ASSERT(root != NULL);
105 mvwprintw(main_window, 2, 0, "Keys");
106 view = tree_view_new(mem_ctx, root, main_window, 15, 24, 3, 0);
107 SMB_ASSERT(view != NULL);
109 mvwprintw(main_window, 2, 25, "Values");
110 vl = value_list_new(mem_ctx, main_window, 15, 40, 3, 25);
111 SMB_ASSERT(vl != NULL);
113 refresh();
114 tree_view_show(view);
115 value_list_show(vl);
117 while ((c = wgetch(main_window)) != 'q') {
118 switch (c) {
119 case KEY_DOWN:
120 menu_driver(view->menu, REQ_DOWN_ITEM);
121 node = item_userptr(current_item(view->menu));
122 value_list_load(vl, node->key);
123 break;
124 case KEY_UP:
125 menu_driver(view->menu, REQ_UP_ITEM);
126 node = item_userptr(current_item(view->menu));
127 value_list_load(vl, node->key);
128 break;
129 case KEY_RIGHT:
130 node = item_userptr(current_item(view->menu));
131 if (node && tree_node_has_children(node)) {
132 tree_node_load_children(node);
133 tree_node_print_path(path_label, node->child_head);
134 tree_view_update(view, node->child_head);
135 value_list_load(vl, node->child_head->key);
137 break;
138 case KEY_LEFT:
139 node = item_userptr(current_item(view->menu));
140 if (node && node->parent) {
141 tree_node_print_path(path_label, node->parent);
142 node = tree_node_first(node->parent);
143 tree_view_update(view, node);
144 value_list_load(vl, node->key);
146 break;
149 tree_view_show(view);
150 value_list_show(vl);
153 endwin();
156 int main(int argc, char **argv)
158 struct poptOption long_options[] = {
159 POPT_AUTOHELP
160 /* ... */
161 POPT_COMMON_SAMBA
162 POPT_COMMON_CONNECTION
163 POPT_COMMON_CREDENTIALS
164 POPT_TABLEEND
166 int opt;
167 poptContext pc;
168 struct user_auth_info *auth_info;
169 TALLOC_CTX *frame;
170 struct registry_context *ctx;
171 WERROR rv;
173 talloc_enable_leak_report_full();
175 frame = talloc_stackframe();
177 setup_logging("regedit", DEBUG_DEFAULT_STDERR);
178 lp_set_cmdline("log level", "0");
180 /* process options */
181 auth_info = user_auth_info_init(frame);
182 if (auth_info == NULL) {
183 exit(1);
185 popt_common_set_auth_info(auth_info);
186 pc = poptGetContext("regedit", argc, (const char **)argv, long_options, 0);
188 while ((opt = poptGetNextOpt(pc)) != -1) {
189 /* TODO */
192 if (!lp_load_global(get_dyn_CONFIGFILE())) {
193 DEBUG(0, ("ERROR loading config file...\n"));
194 exit(1);
197 /* some simple tests */
199 rv = reg_open_samba3(frame, &ctx);
200 SMB_ASSERT(W_ERROR_IS_OK(rv));
202 display_test_window(frame, ctx);
204 //talloc_report_full(frame, stdout);
206 TALLOC_FREE(frame);
208 return 0;