2 * Samba Unix/Linux SMB client library
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/>.
21 #include "popt_common.h"
22 #include "lib/util/data_blob.h"
23 #include "lib/registry/registry.h"
25 #include "regedit_treeview.h"
26 #include "regedit_valuelist.h"
27 #include "regedit_dialog.h"
34 #define KEY_WIDTH (COLS / 4)
35 #define KEY_HEIGHT (LINES - KEY_START_Y - 2)
36 #define VAL_START_X KEY_WIDTH
38 #define VAL_WIDTH (COLS - KEY_WIDTH)
39 #define VAL_HEIGHT (LINES - VAL_START_Y - 2)
41 #define HELP1_START_Y (LINES - 2)
42 #define HELP1_START_X 0
43 #define HELP1_WIDTH (LINES)
44 #define HELP2_START_Y (LINES - 1)
45 #define HELP2_START_X 0
46 #define HELP2_WIDTH (LINES)
47 #define PATH_START_Y 0
48 #define PATH_START_X 6
49 #define PATH_MAX_Y (COLS - 1)
50 #define PATH_WIDTH (COLS - 6)
51 #define PATH_WIDTH_MAX 1024
53 #define PAIR_YELLOW_CYAN 1
54 #define PAIR_BLACK_CYAN 2
60 struct value_list
*vl
;
61 struct tree_view
*keys
;
63 struct regedit_search_opts active_search
;
66 static struct regedit
*regedit_main
= NULL
;
68 static void show_path(struct regedit
*regedit
)
71 int start_win
= PATH_START_X
;
73 if (PATH_START_X
+ regedit
->path_len
> COLS
) {
74 start_pad
= 3 + PATH_START_X
+ regedit
->path_len
- COLS
;
75 mvprintw(PATH_START_Y
, start_win
, "...");
78 copywin(regedit
->path_label
, regedit
->main_window
, 0, start_pad
,
79 PATH_START_Y
, start_win
, PATH_START_Y
, PATH_MAX_Y
, false);
81 mvchgat(0, 0, COLS
, A_BOLD
, PAIR_YELLOW_CYAN
, NULL
);
84 static void print_path(struct regedit
*regedit
, struct tree_node
*node
)
86 regedit
->path_len
= tree_node_print_path(regedit
->path_label
, node
);
90 /* load all available hives */
91 static struct tree_node
*load_hives(TALLOC_CTX
*mem_ctx
,
92 struct registry_context
*ctx
)
94 const char *hives
[] = {
98 "HKEY_PERFORMANCE_DATA",
100 "HKEY_CURRENT_CONFIG",
102 "HKEY_PERFORMANCE_TEXT",
103 "HKEY_PERFORMANCE_NLSTEXT",
106 struct tree_node
*root
, *prev
, *node
;
107 struct registry_key
*key
;
114 for (i
= 0; hives
[i
] != NULL
; ++i
) {
115 rv
= reg_get_predefined_key_by_name(ctx
, hives
[i
], &key
);
116 if (!W_ERROR_IS_OK(rv
)) {
120 node
= tree_node_new(mem_ctx
, NULL
, hives
[i
], key
);
129 tree_node_append(prev
, node
);
137 static void print_help(struct regedit
*regedit
)
139 const char *khelp
= "[n] New Key [s] New Subkey [d] Del Key "
140 "[LEFT] Ascend [RIGHT] Descend";
141 const char *vhelp
= "[n] New Value [d] Del Value [ENTER] Edit "
143 const char *msg
= "KEYS";
144 const char *help
= khelp
;
145 const char *genhelp
= "[TAB] Switch sections [q] Quit "
146 "[UP] List up [DOWN] List down "
147 "[/] Search [x] Next";
150 if (!regedit
->tree_input
) {
155 move(HELP1_START_Y
, HELP1_START_X
);
157 attron(COLOR_PAIR(PAIR_BLACK_CYAN
));
158 mvaddstr(HELP1_START_Y
, HELP1_START_X
, help
);
159 pad
= COLS
- strlen(msg
) - strlen(help
);
160 for (i
= 0; i
< pad
; ++i
) {
163 attroff(COLOR_PAIR(PAIR_BLACK_CYAN
));
164 attron(COLOR_PAIR(PAIR_YELLOW_CYAN
) | A_BOLD
);
166 attroff(COLOR_PAIR(PAIR_YELLOW_CYAN
) | A_BOLD
);
168 move(HELP2_START_Y
, HELP2_START_X
);
170 mvaddstr(HELP2_START_Y
, HELP2_START_X
, genhelp
);
173 static void print_heading(struct regedit
*regedit
)
175 if (regedit
->tree_input
) {
176 tree_view_set_selected(regedit
->keys
, true);
177 value_list_set_selected(regedit
->vl
, false);
179 tree_view_set_selected(regedit
->keys
, false);
180 value_list_set_selected(regedit
->vl
, true);
186 static void load_values(struct regedit
*regedit
)
188 struct tree_node
*node
;
190 node
= tree_view_get_current_node(regedit
->keys
);
191 value_list_load(regedit
->vl
, node
->key
);
194 static void add_reg_key(struct regedit
*regedit
, struct tree_node
*node
,
200 if (!subkey
&& !node
->parent
) {
204 msg
= "Enter name of new key";
206 msg
= "Enter name of new subkey";
208 dialog_input(regedit
, &name
, "New Key", msg
);
211 struct registry_key
*new_key
;
212 struct tree_node
*new_node
;
213 struct tree_node
*list
;
214 struct tree_node
*parent
;
218 list
= node
->child_head
;
220 parent
= node
->parent
;
221 list
= tree_node_first(node
);
222 SMB_ASSERT(list
!= NULL
);
224 rv
= reg_key_add_name(regedit
, parent
->key
, name
,
225 NULL
, NULL
, &new_key
);
226 if (W_ERROR_IS_OK(rv
)) {
227 /* The list of subkeys may not be present in
228 cache yet, so if not, don't bother allocating
229 a new node for the key. */
231 new_node
= tree_node_new(parent
, parent
,
233 SMB_ASSERT(new_node
);
234 tree_node_insert_sorted(list
, new_node
);
237 list
= tree_node_first(node
);
238 tree_view_clear(regedit
->keys
);
239 tree_view_update(regedit
->keys
, list
);
241 dialog_notice(regedit
, DIA_ALERT
, "New Key",
242 "Failed to create key.");
244 talloc_free(discard_const(name
));
248 static WERROR
next_depth_first(struct tree_node
**node
)
252 SMB_ASSERT(node
!= NULL
&& *node
!= NULL
);
254 if (tree_node_has_children(*node
)) {
255 /* 1. If the node has children, go to the first one. */
256 rv
= tree_node_load_children(*node
);
257 if (W_ERROR_IS_OK(rv
)) {
258 SMB_ASSERT((*node
)->child_head
!= NULL
);
259 *node
= (*node
)->child_head
;
261 } else if ((*node
)->next
) {
262 /* 2. If there's a node directly after this one, go there */
263 *node
= (*node
)->next
;
265 /* 3. Otherwise, go up the hierarchy to find the next one */
267 *node
= (*node
)->parent
;
268 if (*node
&& (*node
)->next
) {
269 *node
= (*node
)->next
;
278 static WERROR
regedit_search_next(struct regedit
*regedit
)
281 struct regedit_search_opts
*opts
= ®edit
->active_search
;
283 if (opts
->search_recursive
) {
284 rv
= next_depth_first(&opts
->node
);
285 if (!W_ERROR_IS_OK(rv
)) {
289 opts
->node
= opts
->node
->next
;
295 static WERROR
regedit_search(struct regedit
*regedit
)
297 struct regedit_search_opts
*opts
;
298 struct tree_node
*found
;
301 opts
= ®edit
->active_search
;
303 if (!opts
->query
|| !opts
->match
) {
307 SMB_ASSERT(opts
->search_key
|| opts
->search_value
);
309 for (found
= NULL
; opts
->node
&& !found
; ) {
310 if (opts
->search_key
&&
311 opts
->match(opts
->node
->name
, opts
->query
)) {
314 if (opts
->search_value
) {
316 rv = regedit_search_value(regedit);
317 if (W_ERROR_IS_OK(rv)) {
319 } else if (!W_ERROR_EQUAL(rv, WERR_NO_MORE_ITEMS)) {
324 rv
= regedit_search_next(regedit
);
325 if (!W_ERROR_IS_OK(rv
)) {
331 /* Put the cursor on the node that was found */
332 if (!tree_view_is_node_visible(regedit
->keys
, found
)) {
333 tree_view_update(regedit
->keys
,
334 tree_node_first(found
));
335 print_path(regedit
, found
);
337 tree_view_set_current_node(regedit
->keys
, found
);
338 load_values(regedit
);
339 tree_view_show(regedit
->keys
);
340 value_list_show(regedit
->vl
);
348 static void handle_tree_input(struct regedit
*regedit
, int c
)
350 struct tree_node
*node
;
354 menu_driver(regedit
->keys
->menu
, REQ_DOWN_ITEM
);
355 load_values(regedit
);
358 menu_driver(regedit
->keys
->menu
, REQ_UP_ITEM
);
359 load_values(regedit
);
364 node
= item_userptr(current_item(regedit
->keys
->menu
));
365 if (node
&& tree_node_has_children(node
)) {
366 tree_node_load_children(node
);
367 print_path(regedit
, node
->child_head
);
368 tree_view_update(regedit
->keys
, node
->child_head
);
369 value_list_load(regedit
->vl
, node
->child_head
->key
);
373 node
= item_userptr(current_item(regedit
->keys
->menu
));
374 if (node
&& node
->parent
) {
375 print_path(regedit
, node
->parent
);
376 node
= tree_node_first(node
->parent
);
377 tree_view_update(regedit
->keys
, node
);
378 value_list_load(regedit
->vl
, node
->key
);
383 node
= item_userptr(current_item(regedit
->keys
->menu
));
384 add_reg_key(regedit
, node
, false);
388 node
= item_userptr(current_item(regedit
->keys
->menu
));
389 add_reg_key(regedit
, node
, true);
395 node
= item_userptr(current_item(regedit
->keys
->menu
));
399 sel
= dialog_notice(regedit
, DIA_CONFIRM
,
401 "Really delete key \"%s\"?",
403 if (sel
== DIALOG_OK
) {
405 struct tree_node
*pop
;
406 struct tree_node
*parent
= node
->parent
;
408 rv
= reg_key_del(node
, parent
->key
, node
->name
);
409 if (W_ERROR_IS_OK(rv
)) {
410 tree_view_clear(regedit
->keys
);
411 pop
= tree_node_pop(&node
);
412 tree_node_free_recursive(pop
);
413 node
= parent
->child_head
;
415 node
= tree_node_first(parent
);
416 print_path(regedit
, node
);
418 tree_view_update(regedit
->keys
, node
);
419 value_list_load(regedit
->vl
, node
->key
);
421 dialog_notice(regedit
, DIA_ALERT
, "Delete Key",
422 "Failed to delete key.");
429 tree_view_show(regedit
->keys
);
430 value_list_show(regedit
->vl
);
433 static void handle_value_input(struct regedit
*regedit
, int c
)
435 struct value_item
*vitem
;
436 bool binmode
= false;
440 menu_driver(regedit
->vl
->menu
, REQ_DOWN_ITEM
);
443 menu_driver(regedit
->vl
->menu
, REQ_UP_ITEM
);
451 vitem
= item_userptr(current_item(regedit
->vl
->menu
));
453 struct tree_node
*node
;
454 node
= item_userptr(current_item(regedit
->keys
->menu
));
455 dialog_edit_value(regedit
, node
->key
, vitem
->type
,
457 value_list_load(regedit
->vl
, node
->key
);
465 sel
= dialog_select_type(regedit
, &new_type
);
466 if (sel
== DIALOG_OK
) {
467 struct tree_node
*node
;
468 node
= item_userptr(current_item(regedit
->keys
->menu
));
469 dialog_edit_value(regedit
, node
->key
, new_type
, NULL
,
471 value_list_load(regedit
->vl
, node
->key
);
477 vitem
= item_userptr(current_item(regedit
->vl
->menu
));
481 sel
= dialog_notice(regedit
, DIA_CONFIRM
,
483 "Really delete value \"%s\"?",
485 if (sel
== DIALOG_OK
) {
486 ITEM
*it
= current_item(regedit
->keys
->menu
);
487 struct tree_node
*node
= item_userptr(it
);
488 reg_del_value(regedit
, node
->key
,
490 value_list_load(regedit
->vl
, node
->key
);
496 value_list_show(regedit
->vl
);
499 static bool find_substring(const char *haystack
, const char *needle
)
501 return strstr(haystack
, needle
) != NULL
;
504 static bool find_substring_nocase(const char *haystack
, const char *needle
)
506 return strcasestr(haystack
, needle
) != NULL
;
509 static void handle_main_input(struct regedit
*regedit
, int c
)
516 struct regedit_search_opts
*opts
;
518 opts
= ®edit
->active_search
;
520 talloc_free(discard_const(opts
->query
));
522 rv
= dialog_search_input(regedit
, opts
);
523 if (rv
== DIALOG_OK
) {
524 SMB_ASSERT(opts
->query
!= NULL
);
525 opts
->match
= find_substring
;
526 opts
->node
= regedit
->keys
->root
;
527 if (opts
->search_nocase
) {
528 opts
->match
= find_substring_nocase
;
530 if (opts
->search_relative
) {
532 tree_view_get_current_node(regedit
->keys
);
534 regedit_search(regedit
);
540 regedit_search(regedit
);
543 regedit
->tree_input
= !regedit
->tree_input
;
544 print_heading(regedit
);
547 if (regedit
->tree_input
) {
548 handle_tree_input(regedit
, c
);
550 handle_value_input(regedit
, c
);
555 int regedit_getch(void)
559 SMB_ASSERT(regedit_main
);
562 if (c
== KEY_RESIZE
) {
563 tree_view_resize(regedit_main
->keys
, KEY_HEIGHT
, KEY_WIDTH
,
564 KEY_START_Y
, KEY_START_X
);
565 value_list_resize(regedit_main
->vl
, VAL_HEIGHT
, VAL_WIDTH
,
566 VAL_START_Y
, VAL_START_X
);
567 print_heading(regedit_main
);
568 show_path(regedit_main
);
574 static void display_window(TALLOC_CTX
*mem_ctx
, struct registry_context
*ctx
)
576 struct regedit
*regedit
;
577 struct tree_node
*root
;
586 colors
= has_colors();
589 use_default_colors();
590 assume_default_colors(COLOR_WHITE
, COLOR_BLUE
);
591 init_pair(PAIR_YELLOW_CYAN
, COLOR_YELLOW
, COLOR_CYAN
);
592 init_pair(PAIR_BLACK_CYAN
, COLOR_BLACK
, COLOR_CYAN
);
595 regedit
= talloc_zero(mem_ctx
, struct regedit
);
596 SMB_ASSERT(regedit
!= NULL
);
597 regedit_main
= regedit
;
599 regedit
->main_window
= stdscr
;
600 keypad(regedit
->main_window
, TRUE
);
602 mvwprintw(regedit
->main_window
, 0, 0, "Path: ");
603 regedit
->path_label
= newpad(1, PATH_WIDTH_MAX
);
604 SMB_ASSERT(regedit
->path_label
);
605 wprintw(regedit
->path_label
, "/");
606 show_path(regedit_main
);
608 root
= load_hives(regedit
, ctx
);
609 SMB_ASSERT(root
!= NULL
);
611 regedit
->keys
= tree_view_new(regedit
, root
, KEY_HEIGHT
, KEY_WIDTH
,
612 KEY_START_Y
, KEY_START_X
);
613 SMB_ASSERT(regedit
->keys
!= NULL
);
615 regedit
->vl
= value_list_new(regedit
, VAL_HEIGHT
, VAL_WIDTH
,
616 VAL_START_Y
, VAL_START_X
);
617 SMB_ASSERT(regedit
->vl
!= NULL
);
619 regedit
->tree_input
= true;
620 print_heading(regedit
);
622 tree_view_show(regedit
->keys
);
623 menu_driver(regedit
->keys
->menu
, REQ_FIRST_ITEM
);
624 load_values(regedit
);
625 value_list_show(regedit
->vl
);
631 key
= regedit_getch();
633 handle_main_input(regedit
, key
);
636 } while (key
!= 'q' || key
== 'Q');
641 int main(int argc
, const char **argv
)
643 struct poptOption long_options
[] = {
647 POPT_COMMON_CONNECTION
648 POPT_COMMON_CREDENTIALS
653 struct user_auth_info
*auth_info
;
655 struct registry_context
*ctx
;
658 frame
= talloc_stackframe();
660 setup_logging("regedit", DEBUG_DEFAULT_STDERR
);
661 lp_set_cmdline("log level", "0");
663 /* process options */
664 auth_info
= user_auth_info_init(frame
);
665 if (auth_info
== NULL
) {
668 popt_common_set_auth_info(auth_info
);
669 pc
= poptGetContext("regedit", argc
, argv
, long_options
, 0);
671 while ((opt
= poptGetNextOpt(pc
)) != -1) {
675 if (!lp_load_global(get_dyn_CONFIGFILE())) {
676 DEBUG(0, ("ERROR loading config file...\n"));
680 /* some simple tests */
682 rv
= reg_open_samba3(frame
, &ctx
);
683 if (!W_ERROR_IS_OK(rv
)) {
689 display_window(frame
, ctx
);