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 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"
31 * ls - list values/keys
32 * rmval/rm - remove value
33 * rmkey/rmdir - remove key
34 * mkkey/mkdir - make key
36 * info - show key info
43 static struct registry_key *cmd_info(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
45 struct security_descriptor *sec_desc = NULL;
49 printf("Name: %s\n", cur->name);
50 printf("Full path: %s\n", cur->path);
51 printf("Key Class: %s\n", cur->class_name);
52 last_mod = nt_time_to_unix(cur->last_mod);
53 printf("Time Last Modified: %s\n", ctime(&last_mod));
55 error = reg_get_sec_desc(mem_ctx, cur, &sec_desc);
56 if (!W_ERROR_IS_OK(error)) {
57 printf("Error getting security descriptor\n");
59 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "Security", sec_desc);
61 talloc_free(sec_desc);
65 static struct registry_key *cmd_predef(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *cur, int argc, char **argv)
67 struct registry_key *ret = NULL;
69 fprintf(stderr, "Usage: predef predefined-key-name\n");
71 fprintf(stderr, "No full registry loaded, no predefined keys defined\n");
73 WERROR error = reg_get_predefined_key_by_name(ctx, argv[1], &ret);
75 if (!W_ERROR_IS_OK(error)) {
76 fprintf(stderr, "Error opening predefined key %s: %s\n", argv[1], win_errstr(error));
83 static struct registry_key *cmd_pwd(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
85 printf("%s\n", cur->path);
89 static struct registry_key *cmd_set(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
91 struct registry_value val;
95 fprintf(stderr, "Usage: set value-name type value\n");
99 if (!reg_string_to_val(mem_ctx, argv[2], argv[3], &val.data_type, &val.data)) {
100 fprintf(stderr, "Unable to interpret data\n");
104 error = reg_val_set(cur, argv[1], val.data_type, val.data);
105 if (!W_ERROR_IS_OK(error)) {
106 fprintf(stderr, "Error setting value: %s\n", win_errstr(error));
112 static struct registry_key *cmd_ck(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
114 struct registry_key *new = NULL;
119 error = reg_open_key(mem_ctx, cur, argv[1], &new);
120 if(!W_ERROR_IS_OK(error)) {
121 DEBUG(0, ("Error opening specified key: %s\n", win_errstr(error)));
126 printf("Current path is: %s\n", new->path);
131 static struct registry_key *cmd_print(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
133 struct registry_value *value;
137 fprintf(stderr, "Usage: print <valuename>");
141 error = reg_key_get_value_by_name(mem_ctx, cur, argv[1], &value);
142 if (!W_ERROR_IS_OK(error)) {
143 fprintf(stderr, "No such value '%s'\n", argv[1]);
147 printf("%s\n%s\n", str_regtype(value->data_type), reg_val_data_string(mem_ctx, value->data_type, &value->data));
151 static struct registry_key *cmd_ls(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
155 struct registry_value *value;
156 struct registry_key *sub;
157 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, cur, i, &sub)); i++) {
158 printf("K %s\n", sub->name);
161 if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
162 DEBUG(0, ("Error occured while browsing thru keys: %s\n", win_errstr(error)));
165 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(mem_ctx, cur, i, &value)); i++) {
166 printf("V \"%s\" %s %s\n", value->name, str_regtype(value->data_type), reg_val_data_string(mem_ctx, value->data_type, &value->data));
171 static struct registry_key *cmd_mkkey(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
173 struct registry_key *tmp;
175 fprintf(stderr, "Usage: mkkey <keyname>\n");
179 if(!W_ERROR_IS_OK(reg_key_add_name(mem_ctx, cur, argv[1], 0, NULL, &tmp))) {
180 fprintf(stderr, "Error adding new subkey '%s'\n", argv[1]);
187 static struct registry_key *cmd_rmkey(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
190 fprintf(stderr, "Usage: rmkey <name>\n");
194 if(!W_ERROR_IS_OK(reg_key_del(cur, argv[1]))) {
195 fprintf(stderr, "Error deleting '%s'\n", argv[1]);
197 fprintf(stderr, "Successfully deleted '%s'\n", argv[1]);
203 static struct registry_key *cmd_rmval(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
206 fprintf(stderr, "Usage: rmval <valuename>\n");
210 if(!W_ERROR_IS_OK(reg_del_value(cur, argv[1]))) {
211 fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
213 fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]);
219 static struct registry_key *cmd_exit(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
225 static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *, int, char **);
231 struct registry_key *(*handle)(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *, int argc, char **argv);
232 } regshell_cmds[] = {
233 {"ck", "cd", "Change current key", cmd_ck },
234 {"info", "i", "Show detailed information of a key", cmd_info },
235 {"list", "ls", "List values/keys in current key", cmd_ls },
236 {"print", "p", "Print value", cmd_print },
237 {"mkkey", "mkdir", "Make new key", cmd_mkkey },
238 {"rmval", "rm", "Remove value", cmd_rmval },
239 {"rmkey", "rmdir", "Remove key", cmd_rmkey },
240 {"pwd", "pwk", "Printing current key", cmd_pwd },
241 {"set", "update", "Update value", cmd_set },
242 {"help", "?", "Help", cmd_help },
243 {"exit", "quit", "Exit", cmd_exit },
244 {"predef", "predefined", "Go to predefined key", cmd_predef },
248 static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *cur, int argc, char **argv)
251 printf("Available commands:\n");
252 for(i = 0; regshell_cmds[i].name; i++) {
253 printf("%s - %s\n", regshell_cmds[i].name, regshell_cmds[i].help);
258 static struct registry_key *process_cmd(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *k, char *line)
264 if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
265 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
269 for(i = 0; regshell_cmds[i].name; i++) {
270 if(!strcmp(regshell_cmds[i].name, argv[0]) ||
271 (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) {
272 return regshell_cmds[i].handle(mem_ctx, ctx, k, argc, argv);
276 fprintf(stderr, "No such command '%s'\n", argv[0]);
281 #define MAX_COMPLETIONS 100
283 static struct registry_key *current_key = NULL;
285 static char **reg_complete_command(const char *text, int start, int end)
287 /* Complete command */
289 int i, len, samelen=0, count=1;
291 matches = malloc_array_p(char *, MAX_COMPLETIONS);
292 if (!matches) return NULL;
296 for (i=0;regshell_cmds[i].handle && count < MAX_COMPLETIONS-1;i++) {
297 if (strncmp(text, regshell_cmds[i].name, len) == 0) {
298 matches[count] = strdup(regshell_cmds[i].name);
302 samelen = strlen(matches[count]);
304 while (strncmp(matches[count], matches[count-1], samelen) != 0)
311 case 0: /* should never happen */
315 matches[0] = strdup(matches[1]);
318 matches[0] = strndup(matches[1], samelen);
320 matches[count] = NULL;
326 free(matches[count]);
333 static char **reg_complete_key(const char *text, int start, int end)
335 struct registry_key *base;
336 struct registry_key *subkey;
341 const char *base_n = "";
345 matches = malloc_array_p(char *, MAX_COMPLETIONS);
346 if (!matches) return NULL;
348 mem_ctx = talloc_init("completion");
353 for(i = 0; j < MAX_COMPLETIONS-1; i++) {
354 status = reg_key_get_subkey_by_index(mem_ctx, base, i, &subkey);
355 if(W_ERROR_IS_OK(status)) {
356 if(!strncmp(text, subkey->name, len)) {
357 matches[j] = strdup(subkey->name);
361 samelen = strlen(matches[j]);
363 while (strncmp(matches[j], matches[j-1], samelen) != 0)
366 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
369 printf("Error creating completion list: %s\n", win_errstr(status));
370 talloc_free(mem_ctx);
375 if (j == 1) { /* No matches at all */
377 talloc_free(mem_ctx);
381 if (j == 2) { /* Exact match */
382 asprintf(&matches[0], "%s%s", base_n, matches[1]);
384 asprintf(&matches[0], "%s%s", base_n, talloc_strndup(mem_ctx, matches[1], samelen));
386 talloc_free(mem_ctx);
392 static char **reg_completion(const char *text, int start, int end)
394 smb_readline_ca_char(' ');
397 return reg_complete_command(text, start, end);
399 return reg_complete_key(text, start, end);
403 int main(int argc, char **argv)
406 const char *backend = NULL;
407 struct registry_key *curkey = NULL;
410 TALLOC_CTX *mem_ctx = talloc_init("cmd");
411 const char *remote = NULL;
412 struct registry_context *h = NULL;
413 struct poptOption long_options[] = {
415 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
416 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
418 POPT_COMMON_CREDENTIALS
423 pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
425 while((opt = poptGetNextOpt(pc)) != -1) {
431 error = reg_open_remote (&h, NULL, cmdline_credentials, remote, NULL);
432 } else if (backend) {
433 error = reg_open_hive(NULL, backend, poptGetArg(pc), NULL, cmdline_credentials, &curkey);
435 error = reg_open_local(NULL, &h, NULL, cmdline_credentials);
438 if(!W_ERROR_IS_OK(error)) {
439 fprintf(stderr, "Unable to open registry\n");
446 for (i = 0; reg_predefined_keys[i].handle; i++) {
448 err = reg_get_predefined_key(h, reg_predefined_keys[i].handle, &curkey);
449 if (W_ERROR_IS_OK(err)) {
458 fprintf(stderr, "Unable to access any of the predefined keys\n");
467 if(curkey->hive->root->name) {
468 asprintf(&prompt, "%s:%s> ", curkey->hive->root->name, curkey->path);
470 asprintf(&prompt, "%s> ", curkey->path);
473 current_key = curkey; /* No way to pass a void * pointer
475 line = smb_readline(prompt, NULL, reg_completion);
480 if(line[0] != '\n') {
481 struct registry_key *new = process_cmd(mem_ctx, h, curkey, line);
485 talloc_free(mem_ctx);