Registry tool "regshell": Handle the default attribute in the right way
[Samba/fernandojvsilva.git] / source4 / lib / registry / tools / regshell.c
blobfb7a08e38f8a47dd6ad7ecce7bd385ea73dc4451
1 /*
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/>.
21 #include "includes.h"
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;
33 const char *path;
34 struct registry_key *current;
37 /* *
38 * ck/cd - change key
39 * ls - list values/keys
40 * rmval/rm - remove value
41 * rmkey/rmdir - remove key
42 * mkkey/mkdir - make key
43 * ch - change hive
44 * info - show key info
45 * save - save hive
46 * print - print value
47 * help
48 * exit
51 static WERROR cmd_info(struct regshell_context *ctx, int argc, char **argv)
53 struct security_descriptor *sec_desc = NULL;
54 time_t last_mod;
55 WERROR error;
56 const char *classname = NULL;
57 NTTIME last_change;
58 uint32_t max_subkeynamelen;
59 uint32_t max_valnamelen;
60 uint32_t max_valbufsize;
61 uint32_t num_subkeys;
62 uint32_t num_values;
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));
68 return error;
72 printf("Name: %s\n", strchr(ctx->path, '\\')?strrchr(ctx->path, '\\')+1:
73 ctx->path);
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");
94 return error;
96 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor,
97 "Security", sec_desc);
98 talloc_free(sec_desc);
100 return WERR_OK;
103 static WERROR cmd_predef(struct regshell_context *ctx, int argc, char **argv)
105 struct registry_key *ret = NULL;
106 if (argc < 2) {
107 fprintf(stderr, "Usage: predef predefined-key-name\n");
108 } else if (!ctx) {
109 fprintf(stderr, "No full registry loaded, no predefined keys defined\n");
110 } else {
111 WERROR error = reg_get_predefined_key_by_name(ctx->registry,
112 argv[1], &ret);
114 if (!W_ERROR_IS_OK(error)) {
115 fprintf(stderr, "Error opening predefined key %s: %s\n",
116 argv[1], win_errstr(error));
117 return error;
120 ctx->path = strupper_talloc(ctx, argv[1]);
121 ctx->current = ret;
124 return WERR_OK;
127 static WERROR cmd_pwd(struct regshell_context *ctx,
128 int argc, char **argv)
130 printf("%s\n", ctx->path);
131 return WERR_OK;
134 static WERROR cmd_set(struct regshell_context *ctx, int argc, char **argv)
136 struct registry_value val;
137 WERROR error;
139 if (argc < 4) {
140 fprintf(stderr, "Usage: set value-name type value\n");
141 return WERR_INVALID_PARAM;
144 if (!reg_string_to_val(ctx, lp_iconv_convenience(cmdline_lp_ctx),
145 argv[2], argv[3], &val.data_type,
146 &val.data)) {
147 fprintf(stderr, "Unable to interpret data\n");
148 return WERR_INVALID_PARAM;
151 error = reg_val_set(ctx->current, argv[1], val.data_type, val.data);
152 if (!W_ERROR_IS_OK(error)) {
153 fprintf(stderr, "Error setting value: %s\n", win_errstr(error));
154 return error;
157 return WERR_OK;
160 static WERROR cmd_ck(struct regshell_context *ctx, int argc, char **argv)
162 struct registry_key *new = NULL;
163 WERROR error;
165 if(argc == 2) {
166 error = reg_open_key(ctx->registry, ctx->current, argv[1],
167 &new);
168 if(!W_ERROR_IS_OK(error)) {
169 DEBUG(0, ("Error opening specified key: %s\n",
170 win_errstr(error)));
171 return error;
174 ctx->path = talloc_asprintf(ctx, "%s\\%s", ctx->path, argv[1]);
175 ctx->current = new;
177 printf("New path is: %s\n", ctx->path);
179 return WERR_OK;
182 static WERROR cmd_print(struct regshell_context *ctx, int argc, char **argv)
184 uint32_t value_type;
185 DATA_BLOB value_data;
186 WERROR error;
188 if (argc != 2) {
189 fprintf(stderr, "Usage: print <valuename>\n");
190 return WERR_INVALID_PARAM;
193 error = reg_key_get_value_by_name(ctx, ctx->current, argv[1],
194 &value_type, &value_data);
195 if (!W_ERROR_IS_OK(error)) {
196 fprintf(stderr, "No such value '%s'\n", argv[1]);
197 return error;
200 printf("%s\n%s\n", str_regtype(value_type),
201 reg_val_data_string(ctx, lp_iconv_convenience(cmdline_lp_ctx), value_type, value_data));
203 return WERR_OK;
206 static WERROR cmd_ls(struct regshell_context *ctx, int argc, char **argv)
208 int i;
209 WERROR error;
210 uint32_t valuetype;
211 DATA_BLOB valuedata;
212 const char *name = NULL;
214 for (i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(ctx,
215 ctx->current,
217 &name,
218 NULL,
219 NULL)); i++) {
220 printf("K %s\n", name);
223 if (!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
224 fprintf(stderr, "Error occured while browsing thru keys: %s\n",
225 win_errstr(error));
226 return error;
229 /* default value */
230 if (W_ERROR_IS_OK(reg_key_get_value_by_index(ctx, ctx->current, 0,
231 &name, &valuetype, &valuedata)))
232 printf("V \"(Default)\" %s %s\n", str_regtype(valuetype),
233 reg_val_data_string(ctx, lp_iconv_convenience(cmdline_lp_ctx), valuetype, valuedata));
234 /* other values */
235 for (i = 1; W_ERROR_IS_OK(error = reg_key_get_value_by_index(ctx,
236 ctx->current, i, &name, &valuetype, &valuedata)); i++)
237 printf("V \"%s\" %s %s\n", name, str_regtype(valuetype),
238 reg_val_data_string(ctx, lp_iconv_convenience(cmdline_lp_ctx), valuetype, valuedata));
240 return WERR_OK;
242 static WERROR cmd_mkkey(struct regshell_context *ctx, int argc, char **argv)
244 struct registry_key *tmp;
245 WERROR error;
247 if(argc < 2) {
248 fprintf(stderr, "Usage: mkkey <keyname>\n");
249 return WERR_INVALID_PARAM;
252 error = reg_key_add_name(ctx, ctx->current, argv[1], 0, NULL, &tmp);
254 if (!W_ERROR_IS_OK(error)) {
255 fprintf(stderr, "Error adding new subkey '%s'\n", argv[1]);
256 return error;
259 return WERR_OK;
262 static WERROR cmd_rmkey(struct regshell_context *ctx,
263 int argc, char **argv)
265 WERROR error;
267 if(argc < 2) {
268 fprintf(stderr, "Usage: rmkey <name>\n");
269 return WERR_INVALID_PARAM;
272 error = reg_key_del(ctx->current, argv[1]);
273 if(!W_ERROR_IS_OK(error)) {
274 fprintf(stderr, "Error deleting '%s'\n", argv[1]);
275 return error;
276 } else {
277 fprintf(stderr, "Successfully deleted '%s'\n", argv[1]);
280 return WERR_OK;
283 static WERROR cmd_rmval(struct regshell_context *ctx, int argc, char **argv)
285 WERROR error;
287 if(argc < 2) {
288 fprintf(stderr, "Usage: rmval <valuename>\n");
289 return WERR_INVALID_PARAM;
292 error = reg_del_value(ctx->current, argv[1]);
293 if(!W_ERROR_IS_OK(error)) {
294 fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
295 return error;
296 } else {
297 fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]);
300 return WERR_OK;
303 _NORETURN_ static WERROR cmd_exit(struct regshell_context *ctx,
304 int argc, char **argv)
306 exit(0);
307 return WERR_OK;
310 static WERROR cmd_help(struct regshell_context *ctx, int, char **);
312 static struct {
313 const char *name;
314 const char *alias;
315 const char *help;
316 WERROR (*handle)(struct regshell_context *ctx, int argc, char **argv);
317 } regshell_cmds[] = {
318 {"ck", "cd", "Change current key", cmd_ck },
319 {"info", "i", "Show detailed information of a key", cmd_info },
320 {"list", "ls", "List values/keys in current key", cmd_ls },
321 {"print", "p", "Print value", cmd_print },
322 {"mkkey", "mkdir", "Make new key", cmd_mkkey },
323 {"rmval", "rm", "Remove value", cmd_rmval },
324 {"rmkey", "rmdir", "Remove key", cmd_rmkey },
325 {"pwd", "pwk", "Printing current key", cmd_pwd },
326 {"set", "update", "Update value", cmd_set },
327 {"help", "?", "Help", cmd_help },
328 {"exit", "quit", "Exit", cmd_exit },
329 {"predef", "predefined", "Go to predefined key", cmd_predef },
330 {NULL }
333 static WERROR cmd_help(struct regshell_context *ctx,
334 int argc, char **argv)
336 int i;
337 printf("Available commands:\n");
338 for(i = 0; regshell_cmds[i].name; i++) {
339 printf("%s - %s\n", regshell_cmds[i].name,
340 regshell_cmds[i].help);
342 return WERR_OK;
345 static WERROR process_cmd(struct regshell_context *ctx,
346 char *line)
348 int argc;
349 char **argv = NULL;
350 int ret, i;
352 if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
353 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
354 return WERR_INVALID_PARAM;
357 for(i = 0; regshell_cmds[i].name; i++) {
358 if(!strcmp(regshell_cmds[i].name, argv[0]) ||
359 (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) {
360 return regshell_cmds[i].handle(ctx, argc, argv);
364 fprintf(stderr, "No such command '%s'\n", argv[0]);
366 return WERR_INVALID_PARAM;
369 #define MAX_COMPLETIONS 100
371 static struct registry_key *current_key = NULL;
373 static char **reg_complete_command(const char *text, int start, int end)
375 /* Complete command */
376 char **matches;
377 int i, len, samelen=0, count=1;
379 matches = malloc_array_p(char *, MAX_COMPLETIONS);
380 if (!matches) return NULL;
381 matches[0] = NULL;
383 len = strlen(text);
384 for (i=0;regshell_cmds[i].handle && count < MAX_COMPLETIONS-1;i++) {
385 if (strncmp(text, regshell_cmds[i].name, len) == 0) {
386 matches[count] = strdup(regshell_cmds[i].name);
387 if (!matches[count])
388 goto cleanup;
389 if (count == 1)
390 samelen = strlen(matches[count]);
391 else
392 while (strncmp(matches[count], matches[count-1], samelen) != 0)
393 samelen--;
394 count++;
398 switch (count) {
399 case 0: /* should never happen */
400 case 1:
401 goto cleanup;
402 case 2:
403 matches[0] = strdup(matches[1]);
404 break;
405 default:
406 matches[0] = strndup(matches[1], samelen);
408 matches[count] = NULL;
409 return matches;
411 cleanup:
412 count--;
413 while (count >= 0) {
414 free(matches[count]);
415 count--;
417 free(matches);
418 return NULL;
421 static char **reg_complete_key(const char *text, int start, int end)
423 struct registry_key *base;
424 const char *subkeyname;
425 int i, j = 1;
426 int samelen = 0;
427 int len;
428 char **matches;
429 const char *base_n = "";
430 TALLOC_CTX *mem_ctx;
431 WERROR status;
433 matches = malloc_array_p(char *, MAX_COMPLETIONS);
434 if (!matches) return NULL;
435 matches[0] = NULL;
436 mem_ctx = talloc_init("completion");
438 base = current_key;
440 len = strlen(text);
441 for(i = 0; j < MAX_COMPLETIONS-1; i++) {
442 status = reg_key_get_subkey_by_index(mem_ctx, base, i,
443 &subkeyname, NULL, NULL);
444 if(W_ERROR_IS_OK(status)) {
445 if(!strncmp(text, subkeyname, len)) {
446 matches[j] = strdup(subkeyname);
447 j++;
449 if (j == 1)
450 samelen = strlen(matches[j]);
451 else
452 while (strncmp(matches[j], matches[j-1], samelen) != 0)
453 samelen--;
455 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
456 break;
457 } else {
458 printf("Error creating completion list: %s\n",
459 win_errstr(status));
460 talloc_free(mem_ctx);
461 return NULL;
465 if (j == 1) { /* No matches at all */
466 SAFE_FREE(matches);
467 talloc_free(mem_ctx);
468 return NULL;
471 if (j == 2) { /* Exact match */
472 asprintf(&matches[0], "%s%s", base_n, matches[1]);
473 } else {
474 asprintf(&matches[0], "%s%s", base_n,
475 talloc_strndup(mem_ctx, matches[1], samelen));
477 talloc_free(mem_ctx);
479 matches[j] = NULL;
480 return matches;
483 static char **reg_completion(const char *text, int start, int end)
485 smb_readline_ca_char(' ');
487 if (start == 0) {
488 return reg_complete_command(text, start, end);
489 } else {
490 return reg_complete_key(text, start, end);
494 int main(int argc, char **argv)
496 int opt;
497 const char *file = NULL;
498 poptContext pc;
499 const char *remote = NULL;
500 struct regshell_context *ctx;
501 struct event_context *ev_ctx;
502 bool ret = true;
503 struct poptOption long_options[] = {
504 POPT_AUTOHELP
505 {"file", 'F', POPT_ARG_STRING, &file, 0, "open hive file", NULL },
506 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
507 POPT_COMMON_SAMBA
508 POPT_COMMON_CREDENTIALS
509 POPT_COMMON_VERSION
510 { NULL }
513 pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
515 while((opt = poptGetNextOpt(pc)) != -1) {
518 ctx = talloc_zero(NULL, struct regshell_context);
520 ev_ctx = s4_event_context_init(ctx);
522 if (remote != NULL) {
523 ctx->registry = reg_common_open_remote(remote, ev_ctx,
524 cmdline_lp_ctx, cmdline_credentials);
525 } else if (file != NULL) {
526 ctx->current = reg_common_open_file(file, ev_ctx, cmdline_lp_ctx, cmdline_credentials);
527 if (ctx->current == NULL)
528 return 1;
529 ctx->registry = ctx->current->context;
530 ctx->path = talloc_strdup(ctx, "");
531 } else {
532 ctx->registry = reg_common_open_local(cmdline_credentials, ev_ctx, cmdline_lp_ctx);
535 if (ctx->registry == NULL)
536 return 1;
538 if (ctx->current == NULL) {
539 int i;
541 for (i = 0; reg_predefined_keys[i].handle; i++) {
542 WERROR err;
543 err = reg_get_predefined_key(ctx->registry,
544 reg_predefined_keys[i].handle,
545 &ctx->current);
546 if (W_ERROR_IS_OK(err)) {
547 ctx->path = talloc_strdup(ctx,
548 reg_predefined_keys[i].name);
549 break;
550 } else {
551 ctx->current = NULL;
556 if (ctx->current == NULL) {
557 fprintf(stderr, "Unable to access any of the predefined keys\n");
558 return -1;
561 poptFreeContext(pc);
563 while (true) {
564 char *line, *prompt;
566 asprintf(&prompt, "%s> ", ctx->path);
568 current_key = ctx->current; /* No way to pass a void * pointer
569 via readline :-( */
570 line = smb_readline(prompt, NULL, reg_completion);
572 if (line == NULL) {
573 free(prompt);
574 break;
577 if (line[0] != '\n') {
578 ret = W_ERROR_IS_OK(process_cmd(ctx, line));
580 free(line);
581 free(prompt);
583 talloc_free(ctx);
585 return (ret?0:1);