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/>.
22 #include "lib/registry/registry.h"
23 #include "lib/cmdline/popt_common.h"
24 #include "lib/events/events.h"
25 #include "system/time.h"
26 #include "lib/smbreadline/smbreadline.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "lib/registry/tools/common.h"
30 struct regshell_context {
31 struct registry_context *registry;
33 struct registry_key *current;
38 * ls - list values/keys
39 * rmval/rm - remove value
40 * rmkey/rmdir - remove key
41 * mkkey/mkdir - make key
43 * info - show key info
50 static WERROR cmd_info(struct regshell_context *ctx, int argc, char **argv)
52 struct security_descriptor *sec_desc = NULL;
55 const char *classname;
58 error = reg_key_get_info(ctx, ctx->current, &classname, NULL, NULL,
60 if (!W_ERROR_IS_OK(error)) {
61 printf("Error getting key info: %s\n", win_errstr(error));
66 printf("Name: %s\n", strchr(ctx->path, '\\')?strrchr(ctx->path, '\\')+1:
68 printf("Full path: %s\n", ctx->path);
69 printf("Key Class: %s\n", classname);
70 last_mod = nt_time_to_unix(last_change);
71 printf("Time Last Modified: %s\n", ctime(&last_mod));
73 error = reg_get_sec_desc(ctx, ctx->current, &sec_desc);
74 if (!W_ERROR_IS_OK(error)) {
75 printf("Error getting security descriptor\n");
78 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor,
79 "Security", sec_desc);
80 talloc_free(sec_desc);
85 static WERROR cmd_predef(struct regshell_context *ctx, int argc, char **argv)
87 struct registry_key *ret = NULL;
89 fprintf(stderr, "Usage: predef predefined-key-name\n");
91 fprintf(stderr, "No full registry loaded, no predefined keys defined\n");
93 WERROR error = reg_get_predefined_key_by_name(ctx->registry,
96 if (!W_ERROR_IS_OK(error)) {
97 fprintf(stderr, "Error opening predefined key %s: %s\n",
98 argv[1], win_errstr(error));
106 static WERROR cmd_pwd(struct regshell_context *ctx,
107 int argc, char **argv)
109 printf("%s\n", ctx->path);
113 static WERROR cmd_set(struct regshell_context *ctx, int argc, char **argv)
115 struct registry_value val;
119 fprintf(stderr, "Usage: set value-name type value\n");
120 return WERR_INVALID_PARAM;
123 if (!reg_string_to_val(ctx, argv[2], argv[3], &val.data_type,
125 fprintf(stderr, "Unable to interpret data\n");
126 return WERR_INVALID_PARAM;
129 error = reg_val_set(ctx->current, argv[1], val.data_type, val.data);
130 if (!W_ERROR_IS_OK(error)) {
131 fprintf(stderr, "Error setting value: %s\n", win_errstr(error));
138 static WERROR cmd_ck(struct regshell_context *ctx, int argc, char **argv)
140 struct registry_key *new = NULL;
146 error = reg_open_key(ctx->registry, ctx->current, argv[1],
148 if(!W_ERROR_IS_OK(error)) {
149 DEBUG(0, ("Error opening specified key: %s\n",
155 ctx->path = talloc_asprintf(ctx, "%s\\%s", ctx->path, argv[1]);
156 printf("Current path is: %s\n", ctx->path);
162 static WERROR cmd_print(struct regshell_context *ctx, int argc, char **argv)
165 DATA_BLOB value_data;
169 fprintf(stderr, "Usage: print <valuename>");
170 return WERR_INVALID_PARAM;
173 error = reg_key_get_value_by_name(ctx, ctx->current, argv[1],
174 &value_type, &value_data);
175 if (!W_ERROR_IS_OK(error)) {
176 fprintf(stderr, "No such value '%s'\n", argv[1]);
180 printf("%s\n%s\n", str_regtype(value_type),
181 reg_val_data_string(ctx, value_type, value_data));
186 static WERROR cmd_ls(struct regshell_context *ctx, int argc, char **argv)
190 struct registry_value *value;
195 for (i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(ctx,
201 printf("K %s\n", name);
204 if (!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
205 DEBUG(0, ("Error occured while browsing thru keys: %s\n",
209 for (i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(ctx,
215 printf("V \"%s\" %s %s\n", value->name, str_regtype(data_type),
216 reg_val_data_string(ctx, data_type, data));
221 static WERROR cmd_mkkey(struct regshell_context *ctx, int argc, char **argv)
223 struct registry_key *tmp;
227 fprintf(stderr, "Usage: mkkey <keyname>\n");
228 return WERR_INVALID_PARAM;
231 error = reg_key_add_name(ctx, ctx->current, argv[1], 0, NULL, &tmp);
233 if (!W_ERROR_IS_OK(error)) {
234 fprintf(stderr, "Error adding new subkey '%s'\n", argv[1]);
241 static WERROR cmd_rmkey(struct regshell_context *ctx,
242 int argc, char **argv)
247 fprintf(stderr, "Usage: rmkey <name>\n");
248 return WERR_INVALID_PARAM;
251 error = reg_key_del(ctx->current, argv[1]);
252 if(!W_ERROR_IS_OK(error)) {
253 fprintf(stderr, "Error deleting '%s'\n", argv[1]);
256 fprintf(stderr, "Successfully deleted '%s'\n", argv[1]);
262 static WERROR cmd_rmval(struct regshell_context *ctx, int argc, char **argv)
267 fprintf(stderr, "Usage: rmval <valuename>\n");
268 return WERR_INVALID_PARAM;
271 error = reg_del_value(ctx->current, argv[1]);
272 if(!W_ERROR_IS_OK(error)) {
273 fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
276 fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]);
282 _NORETURN_ static WERROR cmd_exit(struct regshell_context *ctx,
283 int argc, char **argv)
289 static WERROR cmd_help(struct regshell_context *ctx, int, char **);
295 WERROR (*handle)(struct regshell_context *ctx, int argc, char **argv);
296 } regshell_cmds[] = {
297 {"ck", "cd", "Change current key", cmd_ck },
298 {"info", "i", "Show detailed information of a key", cmd_info },
299 {"list", "ls", "List values/keys in current key", cmd_ls },
300 {"print", "p", "Print value", cmd_print },
301 {"mkkey", "mkdir", "Make new key", cmd_mkkey },
302 {"rmval", "rm", "Remove value", cmd_rmval },
303 {"rmkey", "rmdir", "Remove key", cmd_rmkey },
304 {"pwd", "pwk", "Printing current key", cmd_pwd },
305 {"set", "update", "Update value", cmd_set },
306 {"help", "?", "Help", cmd_help },
307 {"exit", "quit", "Exit", cmd_exit },
308 {"predef", "predefined", "Go to predefined key", cmd_predef },
312 static WERROR cmd_help(struct regshell_context *ctx,
313 int argc, char **argv)
316 printf("Available commands:\n");
317 for(i = 0; regshell_cmds[i].name; i++) {
318 printf("%s - %s\n", regshell_cmds[i].name,
319 regshell_cmds[i].help);
324 static WERROR process_cmd(struct regshell_context *ctx,
331 if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
332 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
333 return WERR_INVALID_PARAM;
336 for(i = 0; regshell_cmds[i].name; i++) {
337 if(!strcmp(regshell_cmds[i].name, argv[0]) ||
338 (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) {
339 return regshell_cmds[i].handle(ctx, argc, argv);
343 fprintf(stderr, "No such command '%s'\n", argv[0]);
345 return WERR_INVALID_PARAM;
348 #define MAX_COMPLETIONS 100
350 static struct registry_key *current_key = NULL;
352 static char **reg_complete_command(const char *text, int start, int end)
354 /* Complete command */
356 int i, len, samelen=0, count=1;
358 matches = malloc_array_p(char *, MAX_COMPLETIONS);
359 if (!matches) return NULL;
363 for (i=0;regshell_cmds[i].handle && count < MAX_COMPLETIONS-1;i++) {
364 if (strncmp(text, regshell_cmds[i].name, len) == 0) {
365 matches[count] = strdup(regshell_cmds[i].name);
369 samelen = strlen(matches[count]);
371 while (strncmp(matches[count], matches[count-1], samelen) != 0)
378 case 0: /* should never happen */
382 matches[0] = strdup(matches[1]);
385 matches[0] = strndup(matches[1], samelen);
387 matches[count] = NULL;
393 free(matches[count]);
400 static char **reg_complete_key(const char *text, int start, int end)
402 struct registry_key *base;
403 const char *subkeyname;
408 const char *base_n = "";
412 matches = malloc_array_p(char *, MAX_COMPLETIONS);
413 if (!matches) return NULL;
415 mem_ctx = talloc_init("completion");
420 for(i = 0; j < MAX_COMPLETIONS-1; i++) {
421 status = reg_key_get_subkey_by_index(mem_ctx, base, i,
422 &subkeyname, NULL, NULL);
423 if(W_ERROR_IS_OK(status)) {
424 if(!strncmp(text, subkeyname, len)) {
425 matches[j] = strdup(subkeyname);
429 samelen = strlen(matches[j]);
431 while (strncmp(matches[j], matches[j-1], samelen) != 0)
434 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
437 printf("Error creating completion list: %s\n",
439 talloc_free(mem_ctx);
444 if (j == 1) { /* No matches at all */
446 talloc_free(mem_ctx);
450 if (j == 2) { /* Exact match */
451 asprintf(&matches[0], "%s%s", base_n, matches[1]);
453 asprintf(&matches[0], "%s%s", base_n,
454 talloc_strndup(mem_ctx, matches[1], samelen));
456 talloc_free(mem_ctx);
462 static char **reg_completion(const char *text, int start, int end)
464 smb_readline_ca_char(' ');
467 return reg_complete_command(text, start, end);
469 return reg_complete_key(text, start, end);
473 int main(int argc, char **argv)
476 const char *file = NULL;
478 const char *remote = NULL;
479 struct regshell_context *ctx;
481 struct poptOption long_options[] = {
483 {"file", 'F', POPT_ARG_STRING, &file, 0, "open hive file", NULL },
484 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
486 POPT_COMMON_CREDENTIALS
491 pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
493 while((opt = poptGetNextOpt(pc)) != -1) {
496 ctx = talloc_zero(NULL, struct regshell_context);
498 if (remote != NULL) {
499 ctx->registry = reg_common_open_remote(remote,
500 cmdline_credentials);
501 } else if (file != NULL) {
502 ctx->current = reg_common_open_file(file, cmdline_credentials);
503 if (ctx->current == NULL)
505 ctx->registry = ctx->current->context;
506 ctx->path = talloc_strdup(ctx, "");
508 ctx->registry = reg_common_open_local(cmdline_credentials);
511 if (ctx->registry == NULL)
514 if (ctx->current == NULL) {
517 for (i = 0; reg_predefined_keys[i].handle; i++) {
519 err = reg_get_predefined_key(ctx->registry,
520 reg_predefined_keys[i].handle,
522 if (W_ERROR_IS_OK(err)) {
523 ctx->path = talloc_strdup(ctx,
524 reg_predefined_keys[i].name);
532 if (ctx->current == NULL) {
533 fprintf(stderr, "Unable to access any of the predefined keys\n");
542 asprintf(&prompt, "%s> ", ctx->path);
544 current_key = ctx->current; /* No way to pass a void * pointer
546 line = smb_readline(prompt, NULL, reg_completion);
551 if (line[0] != '\n') {
552 ret = W_ERROR_IS_OK(process_cmd(ctx, line));