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, &last_change);
59 if (!W_ERROR_IS_OK(error)) {
60 printf("Error getting key info: %s\n", win_errstr(error));
65 printf("Name: %s\n", strchr(ctx->path, '\\')?strrchr(ctx->path, '\\')+1:
67 printf("Full path: %s\n", ctx->path);
68 printf("Key Class: %s\n", classname);
69 last_mod = nt_time_to_unix(last_change);
70 printf("Time Last Modified: %s\n", ctime(&last_mod));
72 error = reg_get_sec_desc(ctx, ctx->current, &sec_desc);
73 if (!W_ERROR_IS_OK(error)) {
74 printf("Error getting security descriptor\n");
77 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "Security", sec_desc);
78 talloc_free(sec_desc);
83 static WERROR cmd_predef(struct regshell_context *ctx, int argc, char **argv)
85 struct registry_key *ret = NULL;
87 fprintf(stderr, "Usage: predef predefined-key-name\n");
89 fprintf(stderr, "No full registry loaded, no predefined keys defined\n");
91 WERROR error = reg_get_predefined_key_by_name(ctx->registry, argv[1], &ret);
93 if (!W_ERROR_IS_OK(error)) {
94 fprintf(stderr, "Error opening predefined key %s: %s\n", argv[1], win_errstr(error));
102 static WERROR cmd_pwd(struct regshell_context *ctx,
103 int argc, char **argv)
105 printf("%s\n", ctx->path);
109 static WERROR cmd_set(struct regshell_context *ctx, int argc, char **argv)
111 struct registry_value val;
115 fprintf(stderr, "Usage: set value-name type value\n");
116 return WERR_INVALID_PARAM;
119 if (!reg_string_to_val(ctx, argv[2], argv[3], &val.data_type,
121 fprintf(stderr, "Unable to interpret data\n");
122 return WERR_INVALID_PARAM;
125 error = reg_val_set(ctx->current, argv[1], val.data_type, val.data);
126 if (!W_ERROR_IS_OK(error)) {
127 fprintf(stderr, "Error setting value: %s\n", win_errstr(error));
134 static WERROR cmd_ck(struct regshell_context *ctx, int argc, char **argv)
136 struct registry_key *new = NULL;
142 error = reg_open_key(ctx->registry, ctx->current, argv[1], &new);
143 if(!W_ERROR_IS_OK(error)) {
144 DEBUG(0, ("Error opening specified key: %s\n", win_errstr(error)));
149 ctx->path = talloc_asprintf(ctx, "%s\\%s", ctx->path, argv[1]);
150 printf("Current path is: %s\n", ctx->path);
156 static WERROR cmd_print(struct regshell_context *ctx, int argc, char **argv)
159 DATA_BLOB value_data;
163 fprintf(stderr, "Usage: print <valuename>");
164 return WERR_INVALID_PARAM;
167 error = reg_key_get_value_by_name(ctx, ctx->current, argv[1],
168 &value_type, &value_data);
169 if (!W_ERROR_IS_OK(error)) {
170 fprintf(stderr, "No such value '%s'\n", argv[1]);
174 printf("%s\n%s\n", str_regtype(value_type),
175 reg_val_data_string(ctx, value_type, value_data));
180 static WERROR cmd_ls(struct regshell_context *ctx, int argc, char **argv)
184 struct registry_value *value;
189 for (i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(ctx, ctx->current, i, &name, NULL, NULL)); i++) {
190 printf("K %s\n", name);
193 if (!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
194 DEBUG(0, ("Error occured while browsing thru keys: %s\n", win_errstr(error)));
197 for (i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(ctx, ctx->current, i, &name, &data_type, &data)); i++) {
198 printf("V \"%s\" %s %s\n", value->name, str_regtype(data_type),
199 reg_val_data_string(ctx, data_type, data));
204 static WERROR cmd_mkkey(struct regshell_context *ctx, int argc, char **argv)
206 struct registry_key *tmp;
210 fprintf(stderr, "Usage: mkkey <keyname>\n");
211 return WERR_INVALID_PARAM;
214 error = reg_key_add_name(ctx, ctx->current, argv[1], 0, NULL, &tmp);
216 if (!W_ERROR_IS_OK(error)) {
217 fprintf(stderr, "Error adding new subkey '%s'\n", argv[1]);
224 static WERROR cmd_rmkey(struct regshell_context *ctx,
225 int argc, char **argv)
230 fprintf(stderr, "Usage: rmkey <name>\n");
231 return WERR_INVALID_PARAM;
234 error = reg_key_del(ctx->current, argv[1]);
235 if(!W_ERROR_IS_OK(error)) {
236 fprintf(stderr, "Error deleting '%s'\n", argv[1]);
239 fprintf(stderr, "Successfully deleted '%s'\n", argv[1]);
245 static WERROR cmd_rmval(struct regshell_context *ctx, int argc, char **argv)
250 fprintf(stderr, "Usage: rmval <valuename>\n");
251 return WERR_INVALID_PARAM;
254 error = reg_del_value(ctx->current, argv[1]);
255 if(!W_ERROR_IS_OK(error)) {
256 fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
259 fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]);
265 static WERROR cmd_exit(struct regshell_context *ctx,
266 int argc, char **argv)
272 static WERROR cmd_help(struct regshell_context *ctx, int, char **);
278 WERROR (*handle)(struct regshell_context *ctx,
279 int argc, char **argv);
280 } regshell_cmds[] = {
281 {"ck", "cd", "Change current key", cmd_ck },
282 {"info", "i", "Show detailed information of a key", cmd_info },
283 {"list", "ls", "List values/keys in current key", cmd_ls },
284 {"print", "p", "Print value", cmd_print },
285 {"mkkey", "mkdir", "Make new key", cmd_mkkey },
286 {"rmval", "rm", "Remove value", cmd_rmval },
287 {"rmkey", "rmdir", "Remove key", cmd_rmkey },
288 {"pwd", "pwk", "Printing current key", cmd_pwd },
289 {"set", "update", "Update value", cmd_set },
290 {"help", "?", "Help", cmd_help },
291 {"exit", "quit", "Exit", cmd_exit },
292 {"predef", "predefined", "Go to predefined key", cmd_predef },
296 static WERROR cmd_help(struct regshell_context *ctx,
297 int argc, char **argv)
300 printf("Available commands:\n");
301 for(i = 0; regshell_cmds[i].name; i++) {
302 printf("%s - %s\n", regshell_cmds[i].name, regshell_cmds[i].help);
307 static WERROR process_cmd(struct regshell_context *ctx,
314 if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
315 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
316 return WERR_INVALID_PARAM;
319 for(i = 0; regshell_cmds[i].name; i++) {
320 if(!strcmp(regshell_cmds[i].name, argv[0]) ||
321 (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) {
322 return regshell_cmds[i].handle(ctx, argc, argv);
326 fprintf(stderr, "No such command '%s'\n", argv[0]);
328 return WERR_INVALID_PARAM;
331 #define MAX_COMPLETIONS 100
333 static struct registry_key *current_key = NULL;
335 static char **reg_complete_command(const char *text, int start, int end)
337 /* Complete command */
339 int i, len, samelen=0, count=1;
341 matches = malloc_array_p(char *, MAX_COMPLETIONS);
342 if (!matches) return NULL;
346 for (i=0;regshell_cmds[i].handle && count < MAX_COMPLETIONS-1;i++) {
347 if (strncmp(text, regshell_cmds[i].name, len) == 0) {
348 matches[count] = strdup(regshell_cmds[i].name);
352 samelen = strlen(matches[count]);
354 while (strncmp(matches[count], matches[count-1], samelen) != 0)
361 case 0: /* should never happen */
365 matches[0] = strdup(matches[1]);
368 matches[0] = strndup(matches[1], samelen);
370 matches[count] = NULL;
376 free(matches[count]);
383 static char **reg_complete_key(const char *text, int start, int end)
385 struct registry_key *base;
386 const char *subkeyname;
391 const char *base_n = "";
395 matches = malloc_array_p(char *, MAX_COMPLETIONS);
396 if (!matches) return NULL;
398 mem_ctx = talloc_init("completion");
403 for(i = 0; j < MAX_COMPLETIONS-1; i++) {
404 status = reg_key_get_subkey_by_index(mem_ctx, base, i, &subkeyname,
406 if(W_ERROR_IS_OK(status)) {
407 if(!strncmp(text, subkeyname, len)) {
408 matches[j] = strdup(subkeyname);
412 samelen = strlen(matches[j]);
414 while (strncmp(matches[j], matches[j-1], samelen) != 0)
417 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
420 printf("Error creating completion list: %s\n", win_errstr(status));
421 talloc_free(mem_ctx);
426 if (j == 1) { /* No matches at all */
428 talloc_free(mem_ctx);
432 if (j == 2) { /* Exact match */
433 asprintf(&matches[0], "%s%s", base_n, matches[1]);
435 asprintf(&matches[0], "%s%s", base_n,
436 talloc_strndup(mem_ctx, matches[1], samelen));
438 talloc_free(mem_ctx);
444 static char **reg_completion(const char *text, int start, int end)
446 smb_readline_ca_char(' ');
449 return reg_complete_command(text, start, end);
451 return reg_complete_key(text, start, end);
455 int main(int argc, char **argv)
458 const char *file = NULL;
460 const char *remote = NULL;
461 struct regshell_context *ctx;
463 struct poptOption long_options[] = {
465 {"file", 'F', POPT_ARG_STRING, &file, 0, "open hive file", NULL },
466 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
468 POPT_COMMON_CREDENTIALS
473 pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
475 while((opt = poptGetNextOpt(pc)) != -1) {
478 ctx = talloc_zero(NULL, struct regshell_context);
480 if (remote != NULL) {
481 ctx->registry = reg_common_open_remote(remote, cmdline_credentials);
482 } else if (file != NULL) {
483 ctx->current = reg_common_open_file(file, cmdline_credentials);
484 if (ctx->current == NULL)
486 ctx->registry = ctx->current->context;
487 ctx->path = talloc_strdup(ctx, "");
489 ctx->registry = reg_common_open_local(cmdline_credentials);
492 if (ctx->registry == NULL)
495 if (ctx->current == NULL) {
498 for (i = 0; reg_predefined_keys[i].handle; i++) {
500 err = reg_get_predefined_key(ctx->registry,
501 reg_predefined_keys[i].handle,
503 if (W_ERROR_IS_OK(err)) {
504 ctx->path = talloc_strdup(ctx, reg_predefined_keys[i].name);
512 if (ctx->current == NULL) {
513 fprintf(stderr, "Unable to access any of the predefined keys\n");
522 asprintf(&prompt, "%s> ", ctx->path);
524 current_key = ctx->current; /* No way to pass a void * pointer
526 line = smb_readline(prompt, NULL, reg_completion);
531 if (line[0] != '\n') {
532 ret = W_ERROR_IS_OK(process_cmd(ctx, line));