2 Unix SMB/CIFS implementation.
3 simple registry frontend
5 Copyright (C) Jelmer Vernooij 2004
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "lib/registry/registry.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "lib/events/events.h"
26 #include "lib/registry/reg_backend_rpc.h"
27 #include "system/time.h"
28 #include "lib/smbreadline/smbreadline.h"
29 #include "librpc/gen_ndr/ndr_security.h"
33 * ls - list values/keys
34 * rmval/rm - remove value
35 * rmkey/rmdir - remove key
36 * mkkey/mkdir - make key
38 * info - show key info
45 static struct registry_key *cmd_info(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
47 struct security_descriptor *sec_desc = NULL;
51 printf("Name: %s\n", cur->name);
52 printf("Full path: %s\n", cur->path);
53 printf("Key Class: %s\n", cur->class_name);
54 last_mod = nt_time_to_unix(cur->last_mod);
55 printf("Time Last Modified: %s\n", ctime(&last_mod));
57 error = reg_get_sec_desc(mem_ctx, cur, &sec_desc);
58 if (!W_ERROR_IS_OK(error)) {
59 printf("Error getting security descriptor\n");
61 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "Security", sec_desc);
63 talloc_free(sec_desc);
67 static struct registry_key *cmd_predef(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *cur, int argc, char **argv)
69 struct registry_key *ret = NULL;
71 fprintf(stderr, "Usage: predef predefined-key-name\n");
73 fprintf(stderr, "No full registry loaded, no predefined keys defined\n");
75 WERROR error = reg_get_predefined_key_by_name(ctx, argv[1], &ret);
77 if (!W_ERROR_IS_OK(error)) {
78 fprintf(stderr, "Error opening predefined key %s: %s\n", argv[1], win_errstr(error));
85 static struct registry_key *cmd_pwd(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
87 printf("%s\n", cur->path);
91 static struct registry_key *cmd_set(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
93 struct registry_value val;
97 fprintf(stderr, "Usage: set value-name type value\n");
101 if (!reg_string_to_val(mem_ctx, argv[2], argv[3], &val.data_type, &val.data)) {
102 fprintf(stderr, "Unable to interpret data\n");
106 error = reg_val_set(cur, argv[1], val.data_type, val.data);
107 if (!W_ERROR_IS_OK(error)) {
108 fprintf(stderr, "Error setting value: %s\n", win_errstr(error));
114 static struct registry_key *cmd_ck(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
116 struct registry_key *new = NULL;
121 error = reg_open_key(mem_ctx, cur, argv[1], &new);
122 if(!W_ERROR_IS_OK(error)) {
123 DEBUG(0, ("Error opening specified key: %s\n", win_errstr(error)));
128 printf("Current path is: %s\n", new->path);
133 static struct registry_key *cmd_print(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
135 struct registry_value *value;
139 fprintf(stderr, "Usage: print <valuename>");
143 error = reg_key_get_value_by_name(mem_ctx, cur, argv[1], &value);
144 if (!W_ERROR_IS_OK(error)) {
145 fprintf(stderr, "No such value '%s'\n", argv[1]);
149 printf("%s\n%s\n", str_regtype(value->data_type), reg_val_data_string(mem_ctx, value->data_type, &value->data));
153 static struct registry_key *cmd_ls(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
157 struct registry_value *value;
158 struct registry_key *sub;
159 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, cur, i, &sub)); i++) {
160 printf("K %s\n", sub->name);
163 if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
164 DEBUG(0, ("Error occured while browsing thru keys: %s\n", win_errstr(error)));
167 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(mem_ctx, cur, i, &value)); i++) {
168 printf("V \"%s\" %s %s\n", value->name, str_regtype(value->data_type), reg_val_data_string(mem_ctx, value->data_type, &value->data));
173 static struct registry_key *cmd_mkkey(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
175 struct registry_key *tmp;
177 fprintf(stderr, "Usage: mkkey <keyname>\n");
181 if(!W_ERROR_IS_OK(reg_key_add_name(mem_ctx, cur, argv[1], 0, NULL, &tmp))) {
182 fprintf(stderr, "Error adding new subkey '%s'\n", argv[1]);
189 static struct registry_key *cmd_rmkey(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
192 fprintf(stderr, "Usage: rmkey <name>\n");
196 if(!W_ERROR_IS_OK(reg_key_del(cur, argv[1]))) {
197 fprintf(stderr, "Error deleting '%s'\n", argv[1]);
199 fprintf(stderr, "Successfully deleted '%s'\n", argv[1]);
205 static struct registry_key *cmd_rmval(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
208 fprintf(stderr, "Usage: rmval <valuename>\n");
212 if(!W_ERROR_IS_OK(reg_del_value(cur, argv[1]))) {
213 fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
215 fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]);
221 static struct registry_key *cmd_exit(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
227 static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *, int, char **);
233 struct registry_key *(*handle)(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *, int argc, char **argv);
234 } regshell_cmds[] = {
235 {"ck", "cd", "Change current key", cmd_ck },
236 {"info", "i", "Show detailed information of a key", cmd_info },
237 {"list", "ls", "List values/keys in current key", cmd_ls },
238 {"print", "p", "Print value", cmd_print },
239 {"mkkey", "mkdir", "Make new key", cmd_mkkey },
240 {"rmval", "rm", "Remove value", cmd_rmval },
241 {"rmkey", "rmdir", "Remove key", cmd_rmkey },
242 {"pwd", "pwk", "Printing current key", cmd_pwd },
243 {"set", "update", "Update value", cmd_set },
244 {"help", "?", "Help", cmd_help },
245 {"exit", "quit", "Exit", cmd_exit },
246 {"predef", "predefined", "Go to predefined key", cmd_predef },
250 static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *cur, int argc, char **argv)
253 printf("Available commands:\n");
254 for(i = 0; regshell_cmds[i].name; i++) {
255 printf("%s - %s\n", regshell_cmds[i].name, regshell_cmds[i].help);
260 static struct registry_key *process_cmd(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *k, char *line)
266 if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
267 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
271 for(i = 0; regshell_cmds[i].name; i++) {
272 if(!strcmp(regshell_cmds[i].name, argv[0]) ||
273 (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) {
274 return regshell_cmds[i].handle(mem_ctx, ctx, k, argc, argv);
278 fprintf(stderr, "No such command '%s'\n", argv[0]);
283 #define MAX_COMPLETIONS 100
285 static struct registry_key *current_key = NULL;
287 static char **reg_complete_command(const char *text, int start, int end)
289 /* Complete command */
291 int i, len, samelen=0, count=1;
293 matches = malloc_array_p(char *, MAX_COMPLETIONS);
294 if (!matches) return NULL;
298 for (i=0;regshell_cmds[i].handle && count < MAX_COMPLETIONS-1;i++) {
299 if (strncmp(text, regshell_cmds[i].name, len) == 0) {
300 matches[count] = strdup(regshell_cmds[i].name);
304 samelen = strlen(matches[count]);
306 while (strncmp(matches[count], matches[count-1], samelen) != 0)
313 case 0: /* should never happen */
317 matches[0] = strdup(matches[1]);
320 matches[0] = strndup(matches[1], samelen);
322 matches[count] = NULL;
328 free(matches[count]);
335 static char **reg_complete_key(const char *text, int start, int end)
337 struct registry_key *base;
338 struct registry_key *subkey;
343 const char *base_n = "";
347 matches = malloc_array_p(char *, MAX_COMPLETIONS);
348 if (!matches) return NULL;
350 mem_ctx = talloc_init("completion");
355 for(i = 0; j < MAX_COMPLETIONS-1; i++) {
356 status = reg_key_get_subkey_by_index(mem_ctx, base, i, &subkey);
357 if(W_ERROR_IS_OK(status)) {
358 if(!strncmp(text, subkey->name, len)) {
359 matches[j] = strdup(subkey->name);
363 samelen = strlen(matches[j]);
365 while (strncmp(matches[j], matches[j-1], samelen) != 0)
368 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
371 printf("Error creating completion list: %s\n", win_errstr(status));
372 talloc_free(mem_ctx);
377 if (j == 1) { /* No matches at all */
379 talloc_free(mem_ctx);
383 if (j == 2) { /* Exact match */
384 asprintf(&matches[0], "%s%s", base_n, matches[1]);
386 asprintf(&matches[0], "%s%s", base_n, talloc_strndup(mem_ctx, matches[1], samelen));
388 talloc_free(mem_ctx);
394 static char **reg_completion(const char *text, int start, int end)
396 smb_readline_ca_char(' ');
399 return reg_complete_command(text, start, end);
401 return reg_complete_key(text, start, end);
405 int main(int argc, char **argv)
408 const char *backend = NULL;
409 struct registry_key *curkey = NULL;
412 TALLOC_CTX *mem_ctx = talloc_init("cmd");
413 const char *remote = NULL;
414 struct registry_context *h = NULL;
415 struct poptOption long_options[] = {
417 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
418 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
420 POPT_COMMON_CREDENTIALS
425 pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
427 while((opt = poptGetNextOpt(pc)) != -1) {
433 error = reg_open_remote (&h, NULL, cmdline_credentials, remote, NULL);
434 } else if (backend) {
435 error = reg_open_hive(NULL, backend, poptGetArg(pc), NULL, cmdline_credentials, &curkey);
437 error = reg_open_local(&h, NULL, cmdline_credentials);
440 if(!W_ERROR_IS_OK(error)) {
441 fprintf(stderr, "Unable to open registry\n");
448 for (i = 0; reg_predefined_keys[i].handle; i++) {
450 err = reg_get_predefined_key(h, reg_predefined_keys[i].handle, &curkey);
451 if (W_ERROR_IS_OK(err)) {
460 fprintf(stderr, "Unable to access any of the predefined keys\n");
469 if(curkey->hive->root->name) {
470 asprintf(&prompt, "%s:%s> ", curkey->hive->root->name, curkey->path);
472 asprintf(&prompt, "%s> ", curkey->path);
475 current_key = curkey; /* No way to pass a void * pointer
477 line = smb_readline(prompt, NULL, reg_completion);
482 if(line[0] != '\n') {
483 struct registry_key *new = process_cmd(mem_ctx, h, curkey, line);
487 talloc_free(mem_ctx);