regedit: List values for the selected key.
[garming/samba-autobuild/.git] / source3 / utils / regedit.c
1 /*
2  * Samba Unix/Linux SMB client library
3  * Registry Editor
4  * Copyright (C) Christopher Davis 2012
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
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>
29
30 /* load all available hives */
31 static struct tree_node *load_hives(TALLOC_CTX *mem_ctx,
32                                     struct registry_context *ctx)
33 {
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
45         };
46         struct tree_node *root, *prev, *node;
47         struct registry_key *key;
48         WERROR rv;
49         size_t i;
50
51         root = NULL;
52         prev = NULL;
53
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;
58                 }
59
60                 node = tree_node_new(mem_ctx, NULL, hives[i], key);
61                 if (node == NULL) {
62                         return NULL;
63                 }
64
65                 if (root == NULL) {
66                         root = node;
67                 }
68                 if (prev) {
69                         tree_node_append(prev, node);
70                 }
71                 prev = node;
72         }
73
74         return root;
75 }
76
77 /* test navigating available hives */
78 static void display_test_window(TALLOC_CTX *mem_ctx,
79                                 struct registry_context *ctx)
80 {
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;
86
87         initscr();
88         start_color();
89         cbreak();
90         noecho();
91         keypad(stdscr, TRUE);
92
93         main_window = newwin(25, 80, 0, 0);
94         SMB_ASSERT(main_window != NULL);
95
96         keypad(main_window, TRUE);
97
98         mvwprintw(main_window, 0, 0, "Path: ");
99         path_label = derwin(main_window, 1, 65, 0, 6);
100         wprintw(path_label, "/");
101
102         root = load_hives(mem_ctx, ctx);
103         SMB_ASSERT(root != NULL);
104
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);
108
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);
112
113         refresh();
114         tree_view_show(view);
115         value_list_show(vl);
116
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                         }
136                         break;
137                 case KEY_LEFT:
138                         node = item_userptr(current_item(view->menu));
139                         if (node && node->parent) {
140                                 tree_node_print_path(path_label, node->child_head);
141                                 tree_view_update(view,
142                                         tree_node_first(node->parent));
143                         }
144                         break;
145                 }
146
147                 tree_view_show(view);
148                 value_list_show(vl);
149         }
150
151         endwin();
152 }
153
154 int main(int argc, char **argv)
155 {
156         struct poptOption long_options[] = {
157                 POPT_AUTOHELP
158                 /* ... */
159                 POPT_COMMON_SAMBA
160                 POPT_COMMON_CONNECTION
161                 POPT_COMMON_CREDENTIALS
162                 POPT_TABLEEND
163         };
164         int opt;
165         poptContext pc;
166         struct user_auth_info *auth_info;
167         TALLOC_CTX *frame;
168         struct registry_context *ctx;
169         WERROR rv;
170
171         talloc_enable_leak_report_full();
172
173         frame = talloc_stackframe();
174
175         setup_logging("regedit", DEBUG_DEFAULT_STDERR);
176         lp_set_cmdline("log level", "0");
177
178         /* process options */
179         auth_info = user_auth_info_init(frame);
180         if (auth_info == NULL) {
181                 exit(1);
182         }
183         popt_common_set_auth_info(auth_info);
184         pc = poptGetContext("regedit", argc, (const char **)argv, long_options, 0);
185
186         while ((opt = poptGetNextOpt(pc)) != -1) {
187                 /* TODO */
188         }
189
190         if (!lp_load_global(get_dyn_CONFIGFILE())) {
191                 DEBUG(0, ("ERROR loading config file...\n"));
192                 exit(1);
193         }
194
195         /* some simple tests */
196
197         rv = reg_open_samba3(frame, &ctx);
198         SMB_ASSERT(W_ERROR_IS_OK(rv));
199
200         display_test_window(frame, ctx);
201
202         //talloc_report_full(frame, stdout);
203
204         TALLOC_FREE(frame);
205
206         return 0;
207 }