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;
59 error = reg_key_get_info(ctx, ctx->current, &classname, NULL, NULL,
61 if (!W_ERROR_IS_OK(error)) {
62 printf("Error getting key info: %s\n", win_errstr(error));
67 printf("Name: %s\n", strchr(ctx->path, '\\')?strrchr(ctx->path, '\\')+1:
69 printf("Full path: %s\n", ctx->path);
70 printf("Key Class: %s\n", classname);
71 last_mod = nt_time_to_unix(last_change);
72 printf("Time Last Modified: %s\n", ctime(&last_mod));
74 error = reg_get_sec_desc(ctx, ctx->current, &sec_desc);
75 if (!W_ERROR_IS_OK(error)) {
76 printf("Error getting security descriptor\n");
79 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor,
80 "Security", sec_desc);
81 talloc_free(sec_desc);
86 static WERROR cmd_predef(struct regshell_context *ctx, int argc, char **argv)
88 struct registry_key *ret = NULL;
90 fprintf(stderr, "Usage: predef predefined-key-name\n");
92 fprintf(stderr, "No full registry loaded, no predefined keys defined\n");
94 WERROR error = reg_get_predefined_key_by_name(ctx->registry,
97 if (!W_ERROR_IS_OK(error)) {
98 fprintf(stderr, "Error opening predefined key %s: %s\n",
99 argv[1], win_errstr(error));
107 static WERROR cmd_pwd(struct regshell_context *ctx,
108 int argc, char **argv)
110 printf("%s\n", ctx->path);
114 static WERROR cmd_set(struct regshell_context *ctx, int argc, char **argv)
116 struct registry_value val;
120 fprintf(stderr, "Usage: set value-name type value\n");
121 return WERR_INVALID_PARAM;
124 if (!reg_string_to_val(ctx, argv[2], argv[3], &val.data_type,
126 fprintf(stderr, "Unable to interpret data\n");
127 return WERR_INVALID_PARAM;
130 error = reg_val_set(ctx->current, argv[1], val.data_type, val.data);
131 if (!W_ERROR_IS_OK(error)) {
132 fprintf(stderr, "Error setting value: %s\n", win_errstr(error));
139 static WERROR cmd_ck(struct regshell_context *ctx, int argc, char **argv)
141 struct registry_key *new = NULL;
147 error = reg_open_key(ctx->registry, ctx->current, argv[1],
149 if(!W_ERROR_IS_OK(error)) {
150 DEBUG(0, ("Error opening specified key: %s\n",
156 ctx->path = talloc_asprintf(ctx, "%s\\%s", ctx->path, argv[1]);
157 printf("Current path is: %s\n", ctx->path);
163 static WERROR cmd_print(struct regshell_context *ctx, int argc, char **argv)
166 DATA_BLOB value_data;
170 fprintf(stderr, "Usage: print <valuename>");
171 return WERR_INVALID_PARAM;
174 error = reg_key_get_value_by_name(ctx, ctx->current, argv[1],
175 &value_type, &value_data);
176 if (!W_ERROR_IS_OK(error)) {
177 fprintf(stderr, "No such value '%s'\n", argv[1]);
181 printf("%s\n%s\n", str_regtype(value_type),
182 reg_val_data_string(ctx, value_type, value_data));
187 static WERROR cmd_ls(struct regshell_context *ctx, int argc, char **argv)
191 struct registry_value *value;
196 for (i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(ctx,
202 printf("K %s\n", name);
205 if (!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
206 DEBUG(0, ("Error occured while browsing thru keys: %s\n",
210 for (i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(ctx,
216 printf("V \"%s\" %s %s\n", value->name, str_regtype(data_type),
217 reg_val_data_string(ctx, data_type, data));
222 static WERROR cmd_mkkey(struct regshell_context *ctx, int argc, char **argv)
224 struct registry_key *tmp;
228 fprintf(stderr, "Usage: mkkey <keyname>\n");
229 return WERR_INVALID_PARAM;
232 error = reg_key_add_name(ctx, ctx->current, argv[1], 0, NULL, &tmp);
234 if (!W_ERROR_IS_OK(error)) {
235 fprintf(stderr, "Error adding new subkey '%s'\n", argv[1]);
242 static WERROR cmd_rmkey(struct regshell_context *ctx,
243 int argc, char **argv)
248 fprintf(stderr, "Usage: rmkey <name>\n");
249 return WERR_INVALID_PARAM;
252 error = reg_key_del(ctx->current, argv[1]);
253 if(!W_ERROR_IS_OK(error)) {
254 fprintf(stderr, "Error deleting '%s'\n", argv[1]);
257 fprintf(stderr, "Successfully deleted '%s'\n", argv[1]);
263 static WERROR cmd_rmval(struct regshell_context *ctx, int argc, char **argv)
268 fprintf(stderr, "Usage: rmval <valuename>\n");
269 return WERR_INVALID_PARAM;
272 error = reg_del_value(ctx->current, argv[1]);
273 if(!W_ERROR_IS_OK(error)) {
274 fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
277 fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]);
283 _NORETURN_ static WERROR cmd_exit(struct regshell_context *ctx,
284 int argc, char **argv)
290 static WERROR cmd_help(struct regshell_context *ctx, int, char **);
296 WERROR (*handle)(struct regshell_context *ctx, int argc, char **argv);
297 } regshell_cmds[] = {
298 {"ck", "cd", "Change current key", cmd_ck },
299 {"info", "i", "Show detailed information of a key", cmd_info },
300 {"list", "ls", "List values/keys in current key", cmd_ls },
301 {"print", "p", "Print value", cmd_print },
302 {"mkkey", "mkdir", "Make new key", cmd_mkkey },
303 {"rmval", "rm", "Remove value", cmd_rmval },
304 {"rmkey", "rmdir", "Remove key", cmd_rmkey },
305 {"pwd", "pwk", "Printing current key", cmd_pwd },
306 {"set", "update", "Update value", cmd_set },
307 {"help", "?", "Help", cmd_help },
308 {"exit", "quit", "Exit", cmd_exit },
309 {"predef", "predefined", "Go to predefined key", cmd_predef },
313 static WERROR cmd_help(struct regshell_context *ctx,
314 int argc, char **argv)
317 printf("Available commands:\n");
318 for(i = 0; regshell_cmds[i].name; i++) {
319 printf("%s - %s\n", regshell_cmds[i].name,
320 regshell_cmds[i].help);
325 static WERROR process_cmd(struct regshell_context *ctx,
332 if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
333 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
334 return WERR_INVALID_PARAM;
337 for(i = 0; regshell_cmds[i].name; i++) {
338 if(!strcmp(regshell_cmds[i].name, argv[0]) ||
339 (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) {
340 return regshell_cmds[i].handle(ctx, argc, argv);
344 fprintf(stderr, "No such command '%s'\n", argv[0]);
346 return WERR_INVALID_PARAM;
349 #define MAX_COMPLETIONS 100
351 static struct registry_key *current_key = NULL;
353 static char **reg_complete_command(const char *text, int start, int end)
355 /* Complete command */
357 int i, len, samelen=0, count=1;
359 matches = malloc_array_p(char *, MAX_COMPLETIONS);
360 if (!matches) return NULL;
364 for (i=0;regshell_cmds[i].handle && count < MAX_COMPLETIONS-1;i++) {
365 if (strncmp(text, regshell_cmds[i].name, len) == 0) {
366 matches[count] = strdup(regshell_cmds[i].name);
370 samelen = strlen(matches[count]);
372 while (strncmp(matches[count], matches[count-1], samelen) != 0)
379 case 0: /* should never happen */
383 matches[0] = strdup(matches[1]);
386 matches[0] = strndup(matches[1], samelen);
388 matches[count] = NULL;
394 free(matches[count]);
401 static char **reg_complete_key(const char *text, int start, int end)
403 struct registry_key *base;
404 const char *subkeyname;
409 const char *base_n = "";
413 matches = malloc_array_p(char *, MAX_COMPLETIONS);
414 if (!matches) return NULL;
416 mem_ctx = talloc_init("completion");
421 for(i = 0; j < MAX_COMPLETIONS-1; i++) {
422 status = reg_key_get_subkey_by_index(mem_ctx, base, i,
423 &subkeyname, NULL, NULL);
424 if(W_ERROR_IS_OK(status)) {
425 if(!strncmp(text, subkeyname, len)) {
426 matches[j] = strdup(subkeyname);
430 samelen = strlen(matches[j]);
432 while (strncmp(matches[j], matches[j-1], samelen) != 0)
435 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
438 printf("Error creating completion list: %s\n",
440 talloc_free(mem_ctx);
445 if (j == 1) { /* No matches at all */
447 talloc_free(mem_ctx);
451 if (j == 2) { /* Exact match */
452 asprintf(&matches[0], "%s%s", base_n, matches[1]);
454 asprintf(&matches[0], "%s%s", base_n,
455 talloc_strndup(mem_ctx, matches[1], samelen));
457 talloc_free(mem_ctx);
463 static char **reg_completion(const char *text, int start, int end)
465 smb_readline_ca_char(' ');
468 return reg_complete_command(text, start, end);
470 return reg_complete_key(text, start, end);
474 int main(int argc, char **argv)
477 const char *file = NULL;
479 const char *remote = NULL;
480 struct regshell_context *ctx;
482 struct poptOption long_options[] = {
484 {"file", 'F', POPT_ARG_STRING, &file, 0, "open hive file", NULL },
485 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
487 POPT_COMMON_CREDENTIALS
492 pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
494 while((opt = poptGetNextOpt(pc)) != -1) {
497 ctx = talloc_zero(NULL, struct regshell_context);
499 if (remote != NULL) {
500 ctx->registry = reg_common_open_remote(remote,
501 cmdline_credentials);
502 } else if (file != NULL) {
503 ctx->current = reg_common_open_file(file, cmdline_credentials);
504 if (ctx->current == NULL)
506 ctx->registry = ctx->current->context;
507 ctx->path = talloc_strdup(ctx, "");
509 ctx->registry = reg_common_open_local(cmdline_credentials, global_loadparm);
512 if (ctx->registry == NULL)
515 if (ctx->current == NULL) {
518 for (i = 0; reg_predefined_keys[i].handle; i++) {
520 err = reg_get_predefined_key(ctx->registry,
521 reg_predefined_keys[i].handle,
523 if (W_ERROR_IS_OK(err)) {
524 ctx->path = talloc_strdup(ctx,
525 reg_predefined_keys[i].name);
533 if (ctx->current == NULL) {
534 fprintf(stderr, "Unable to access any of the predefined keys\n");
543 asprintf(&prompt, "%s> ", ctx->path);
545 current_key = ctx->current; /* No way to pass a void * pointer
547 line = smb_readline(prompt, NULL, reg_completion);
552 if (line[0] != '\n') {
553 ret = W_ERROR_IS_OK(process_cmd(ctx, line));