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"
29 #include "param/param.h"
31 struct regshell_context {
32 struct registry_context *registry;
34 struct registry_key *current;
39 * ls - list values/keys
40 * rmval/rm - remove value
41 * rmkey/rmdir - remove key
42 * mkkey/mkdir - make key
44 * info - show key info
51 static WERROR cmd_info(struct regshell_context *ctx, int argc, char **argv)
53 struct security_descriptor *sec_desc = NULL;
56 const char *classname = NULL;
58 uint32_t max_subkeynamelen;
59 uint32_t max_valnamelen;
60 uint32_t max_valbufsize;
64 error = reg_key_get_info(ctx, ctx->current, &classname, &num_subkeys, &num_values,
65 &last_change, &max_subkeynamelen, &max_valnamelen, &max_valbufsize);
66 if (!W_ERROR_IS_OK(error)) {
67 printf("Error getting key info: %s\n", win_errstr(error));
72 printf("Name: %s\n", strchr(ctx->path, '\\')?strrchr(ctx->path, '\\')+1:
74 printf("Full path: %s\n", ctx->path);
75 if (classname != NULL)
76 printf("Key Class: %s\n", classname);
77 last_mod = nt_time_to_unix(last_change);
78 printf("Time Last Modified: %s\n", ctime(&last_mod));
79 printf("Number of subkeys: %d\n", num_subkeys);
80 printf("Number of values: %d\n", num_values);
82 if (max_valnamelen > 0)
83 printf("Maximum value name length: %d\n", max_valnamelen);
85 if (max_valbufsize > 0)
86 printf("Maximum value data length: %d\n", max_valbufsize);
88 if (max_subkeynamelen > 0)
89 printf("Maximum sub key name length: %d\n", max_subkeynamelen);
91 error = reg_get_sec_desc(ctx, ctx->current, &sec_desc);
92 if (!W_ERROR_IS_OK(error)) {
93 printf("Error getting security descriptor\n");
96 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor,
97 "Security", sec_desc);
98 talloc_free(sec_desc);
103 static WERROR cmd_predef(struct regshell_context *ctx, int argc, char **argv)
105 struct registry_key *ret = NULL;
107 fprintf(stderr, "Usage: predef predefined-key-name\n");
109 fprintf(stderr, "No full registry loaded, no predefined keys defined\n");
111 WERROR error = reg_get_predefined_key_by_name(ctx->registry,
114 if (!W_ERROR_IS_OK(error)) {
115 fprintf(stderr, "Error opening predefined key %s: %s\n",
116 argv[1], win_errstr(error));
124 static WERROR cmd_pwd(struct regshell_context *ctx,
125 int argc, char **argv)
127 printf("%s\n", ctx->path);
131 static WERROR cmd_set(struct regshell_context *ctx, int argc, char **argv)
133 struct registry_value val;
137 fprintf(stderr, "Usage: set value-name type value\n");
138 return WERR_INVALID_PARAM;
141 if (!reg_string_to_val(ctx, argv[2], argv[3], &val.data_type,
143 fprintf(stderr, "Unable to interpret data\n");
144 return WERR_INVALID_PARAM;
147 error = reg_val_set(ctx->current, argv[1], val.data_type, val.data);
148 if (!W_ERROR_IS_OK(error)) {
149 fprintf(stderr, "Error setting value: %s\n", win_errstr(error));
156 static WERROR cmd_ck(struct regshell_context *ctx, int argc, char **argv)
158 struct registry_key *new = NULL;
164 error = reg_open_key(ctx->registry, ctx->current, argv[1],
166 if(!W_ERROR_IS_OK(error)) {
167 DEBUG(0, ("Error opening specified key: %s\n",
173 ctx->path = talloc_asprintf(ctx, "%s\\%s", ctx->path, argv[1]);
174 printf("Current path is: %s\n", ctx->path);
180 static WERROR cmd_print(struct regshell_context *ctx, int argc, char **argv)
183 DATA_BLOB value_data;
187 fprintf(stderr, "Usage: print <valuename>");
188 return WERR_INVALID_PARAM;
191 error = reg_key_get_value_by_name(ctx, ctx->current, argv[1],
192 &value_type, &value_data);
193 if (!W_ERROR_IS_OK(error)) {
194 fprintf(stderr, "No such value '%s'\n", argv[1]);
198 printf("%s\n%s\n", str_regtype(value_type),
199 reg_val_data_string(ctx, value_type, value_data));
204 static WERROR cmd_ls(struct regshell_context *ctx, int argc, char **argv)
210 const char *name = NULL;
212 for (i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(ctx,
218 printf("K %s\n", name);
221 if (!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
222 DEBUG(0, ("Error occured while browsing thru keys: %s\n",
226 for (i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(ctx,
232 printf("V \"%s\" %s %s\n", name, str_regtype(data_type),
233 reg_val_data_string(ctx, data_type, data));
238 static WERROR cmd_mkkey(struct regshell_context *ctx, int argc, char **argv)
240 struct registry_key *tmp;
244 fprintf(stderr, "Usage: mkkey <keyname>\n");
245 return WERR_INVALID_PARAM;
248 error = reg_key_add_name(ctx, ctx->current, argv[1], 0, NULL, &tmp);
250 if (!W_ERROR_IS_OK(error)) {
251 fprintf(stderr, "Error adding new subkey '%s'\n", argv[1]);
258 static WERROR cmd_rmkey(struct regshell_context *ctx,
259 int argc, char **argv)
264 fprintf(stderr, "Usage: rmkey <name>\n");
265 return WERR_INVALID_PARAM;
268 error = reg_key_del(ctx->current, argv[1]);
269 if(!W_ERROR_IS_OK(error)) {
270 fprintf(stderr, "Error deleting '%s'\n", argv[1]);
273 fprintf(stderr, "Successfully deleted '%s'\n", argv[1]);
279 static WERROR cmd_rmval(struct regshell_context *ctx, int argc, char **argv)
284 fprintf(stderr, "Usage: rmval <valuename>\n");
285 return WERR_INVALID_PARAM;
288 error = reg_del_value(ctx->current, argv[1]);
289 if(!W_ERROR_IS_OK(error)) {
290 fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
293 fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]);
299 _NORETURN_ static WERROR cmd_exit(struct regshell_context *ctx,
300 int argc, char **argv)
306 static WERROR cmd_help(struct regshell_context *ctx, int, char **);
312 WERROR (*handle)(struct regshell_context *ctx, int argc, char **argv);
313 } regshell_cmds[] = {
314 {"ck", "cd", "Change current key", cmd_ck },
315 {"info", "i", "Show detailed information of a key", cmd_info },
316 {"list", "ls", "List values/keys in current key", cmd_ls },
317 {"print", "p", "Print value", cmd_print },
318 {"mkkey", "mkdir", "Make new key", cmd_mkkey },
319 {"rmval", "rm", "Remove value", cmd_rmval },
320 {"rmkey", "rmdir", "Remove key", cmd_rmkey },
321 {"pwd", "pwk", "Printing current key", cmd_pwd },
322 {"set", "update", "Update value", cmd_set },
323 {"help", "?", "Help", cmd_help },
324 {"exit", "quit", "Exit", cmd_exit },
325 {"predef", "predefined", "Go to predefined key", cmd_predef },
329 static WERROR cmd_help(struct regshell_context *ctx,
330 int argc, char **argv)
333 printf("Available commands:\n");
334 for(i = 0; regshell_cmds[i].name; i++) {
335 printf("%s - %s\n", regshell_cmds[i].name,
336 regshell_cmds[i].help);
341 static WERROR process_cmd(struct regshell_context *ctx,
348 if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
349 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
350 return WERR_INVALID_PARAM;
353 for(i = 0; regshell_cmds[i].name; i++) {
354 if(!strcmp(regshell_cmds[i].name, argv[0]) ||
355 (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) {
356 return regshell_cmds[i].handle(ctx, argc, argv);
360 fprintf(stderr, "No such command '%s'\n", argv[0]);
362 return WERR_INVALID_PARAM;
365 #define MAX_COMPLETIONS 100
367 static struct registry_key *current_key = NULL;
369 static char **reg_complete_command(const char *text, int start, int end)
371 /* Complete command */
373 int i, len, samelen=0, count=1;
375 matches = malloc_array_p(char *, MAX_COMPLETIONS);
376 if (!matches) return NULL;
380 for (i=0;regshell_cmds[i].handle && count < MAX_COMPLETIONS-1;i++) {
381 if (strncmp(text, regshell_cmds[i].name, len) == 0) {
382 matches[count] = strdup(regshell_cmds[i].name);
386 samelen = strlen(matches[count]);
388 while (strncmp(matches[count], matches[count-1], samelen) != 0)
395 case 0: /* should never happen */
399 matches[0] = strdup(matches[1]);
402 matches[0] = strndup(matches[1], samelen);
404 matches[count] = NULL;
410 free(matches[count]);
417 static char **reg_complete_key(const char *text, int start, int end)
419 struct registry_key *base;
420 const char *subkeyname;
425 const char *base_n = "";
429 matches = malloc_array_p(char *, MAX_COMPLETIONS);
430 if (!matches) return NULL;
432 mem_ctx = talloc_init("completion");
437 for(i = 0; j < MAX_COMPLETIONS-1; i++) {
438 status = reg_key_get_subkey_by_index(mem_ctx, base, i,
439 &subkeyname, NULL, NULL);
440 if(W_ERROR_IS_OK(status)) {
441 if(!strncmp(text, subkeyname, len)) {
442 matches[j] = strdup(subkeyname);
446 samelen = strlen(matches[j]);
448 while (strncmp(matches[j], matches[j-1], samelen) != 0)
451 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
454 printf("Error creating completion list: %s\n",
456 talloc_free(mem_ctx);
461 if (j == 1) { /* No matches at all */
463 talloc_free(mem_ctx);
467 if (j == 2) { /* Exact match */
468 asprintf(&matches[0], "%s%s", base_n, matches[1]);
470 asprintf(&matches[0], "%s%s", base_n,
471 talloc_strndup(mem_ctx, matches[1], samelen));
473 talloc_free(mem_ctx);
479 static char **reg_completion(const char *text, int start, int end)
481 smb_readline_ca_char(' ');
484 return reg_complete_command(text, start, end);
486 return reg_complete_key(text, start, end);
490 int main(int argc, char **argv)
493 const char *file = NULL;
495 const char *remote = NULL;
496 struct regshell_context *ctx;
498 struct poptOption long_options[] = {
500 {"file", 'F', POPT_ARG_STRING, &file, 0, "open hive file", NULL },
501 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
503 POPT_COMMON_CREDENTIALS
508 pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
510 while((opt = poptGetNextOpt(pc)) != -1) {
513 ctx = talloc_zero(NULL, struct regshell_context);
515 if (remote != NULL) {
516 ctx->registry = reg_common_open_remote(remote, cmdline_lp_ctx,
517 cmdline_credentials);
518 } else if (file != NULL) {
519 ctx->current = reg_common_open_file(file, cmdline_lp_ctx, cmdline_credentials);
520 if (ctx->current == NULL)
522 ctx->registry = ctx->current->context;
523 ctx->path = talloc_strdup(ctx, "");
525 ctx->registry = reg_common_open_local(cmdline_credentials, cmdline_lp_ctx);
528 if (ctx->registry == NULL)
531 if (ctx->current == NULL) {
534 for (i = 0; reg_predefined_keys[i].handle; i++) {
536 err = reg_get_predefined_key(ctx->registry,
537 reg_predefined_keys[i].handle,
539 if (W_ERROR_IS_OK(err)) {
540 ctx->path = talloc_strdup(ctx,
541 reg_predefined_keys[i].name);
549 if (ctx->current == NULL) {
550 fprintf(stderr, "Unable to access any of the predefined keys\n");
559 asprintf(&prompt, "%s> ", ctx->path);
561 current_key = ctx->current; /* No way to pass a void * pointer
563 line = smb_readline(prompt, NULL, reg_completion);
570 if (line[0] != '\n') {
571 ret = W_ERROR_IS_OK(process_cmd(ctx, line));