diff options
author | Jelmer Vernooij <jelmer@samba.org> | 2007-08-26 15:16:40 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 15:02:34 -0500 |
commit | b409d4120f9ae451f93a2322267c0f346531d9f3 (patch) | |
tree | 04ac2189f16db99537c36320a9d9e7c3c9d43b6f /source4/lib/registry/tools | |
parent | 8e789517b723955f1530837058d5e9fe98aba19f (diff) | |
download | samba-b409d4120f9ae451f93a2322267c0f346531d9f3.tar.gz samba-b409d4120f9ae451f93a2322267c0f346531d9f3.tar.bz2 samba-b409d4120f9ae451f93a2322267c0f346531d9f3.zip |
r24667: Finally merge the registry improvements that Wilco Baan Hofman and I have
been working on for at least half a year now. Contains the following
improvements:
* proper layering (finally!) for the registry library. Distinction is
now made between 'real' backends (local, remote, wine, etc) and
the low-level hive backends (regf, creg, ldb, ...) that are only used
by the local registry backend
* tests for all important hive and registry operations
* re-enable RPC-WINREG tests (still needs more work though, as
some return values aren't checked yet)
* write support for REGF files
* dir backend now supports setting/reading values, creating keys
* support for storing security descriptors
* remove CREG backend as it was incomplete, didn't match the data model
and wasn't used at all anyway
* support for parsing ADM files as used by the policy editor (see lib/policy)
* support for parsing PREG files (format used by .POL files)
* new streaming interface for registry diffs (improves speed and memory usage
for regdiff/regpatch significantly)
... and fixes a large number of bugs in the registry code
(This used to be commit 7a1eec6358bc863dfc671c542b7185d3e39d7b5a)
Diffstat (limited to 'source4/lib/registry/tools')
-rw-r--r-- | source4/lib/registry/tools/common.c | 75 | ||||
-rw-r--r-- | source4/lib/registry/tools/regdiff.c | 103 | ||||
-rw-r--r-- | source4/lib/registry/tools/regpatch.c | 31 | ||||
-rw-r--r-- | source4/lib/registry/tools/regshell.c | 266 | ||||
-rw-r--r-- | source4/lib/registry/tools/regtree.c | 114 |
5 files changed, 382 insertions, 207 deletions
diff --git a/source4/lib/registry/tools/common.c b/source4/lib/registry/tools/common.c new file mode 100644 index 0000000000..c8b0945c2c --- /dev/null +++ b/source4/lib/registry/tools/common.c @@ -0,0 +1,75 @@ +/* + Unix SMB/CIFS implementation. + Popt routines specifically for registry + + Copyright (C) Jelmer Vernooij 2007 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" +#include "auth/credentials/credentials.h" +#include "lib/registry/registry.h" + +struct registry_context *reg_common_open_remote(const char *remote, struct cli_credentials *creds) +{ + struct registry_context *h; + WERROR error; + + error = reg_open_remote(&h, NULL, creds, remote, NULL); + + if (!W_ERROR_IS_OK(error)) { + fprintf(stderr, "Unable to open remote registry at %s:%s \n", remote, win_errstr(error)); + return NULL; + } + + return h; +} + +struct registry_key *reg_common_open_file(const char *path, struct cli_credentials *creds) +{ + struct hive_key *hive_root; + struct registry_context *h; + WERROR error; + + error = reg_open_hive(NULL, path, NULL, creds, &hive_root); + + if(!W_ERROR_IS_OK(error)) { + fprintf(stderr, "Unable to open '%s': %s \n", path, win_errstr(error)); + return NULL; + } + + error = reg_open_local(NULL, &h, NULL, creds); + if (!W_ERROR_IS_OK(error)) { + fprintf(stderr, "Unable to initialize local registry: %s\n", win_errstr(error)); + return NULL; + } + + return reg_import_hive_key(h, hive_root, -1, NULL); +} + +struct registry_context *reg_common_open_local(struct cli_credentials *creds) +{ + WERROR error; + struct registry_context *h; + + error = reg_open_samba(NULL, &h, NULL, creds); + + if(!W_ERROR_IS_OK(error)) { + fprintf(stderr, "Unable to open local registry:%s \n", win_errstr(error)); + return NULL; + } + + return h; +} diff --git a/source4/lib/registry/tools/regdiff.c b/source4/lib/registry/tools/regdiff.c index 6eb8a78caf..8030457f5c 100644 --- a/source4/lib/registry/tools/regdiff.c +++ b/source4/lib/registry/tools/regdiff.c @@ -2,7 +2,8 @@ Unix SMB/CIFS implementation. simple registry frontend - Copyright (C) Jelmer Vernooij 2004-2005 + Copyright (C) Jelmer Vernooij 2004-2007 + Copyright (C) Wilco Baan Hofman 2006 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,63 +23,115 @@ #include "lib/registry/registry.h" #include "lib/events/events.h" #include "lib/cmdline/popt_common.h" +#include "lib/registry/tools/common.h" -int main(int argc, char **argv) +enum reg_backend { REG_UNKNOWN, REG_LOCAL, REG_REMOTE, REG_NULL }; + +static struct registry_context *open_backend(poptContext pc, enum reg_backend backend, const char *remote_host) +{ + WERROR error; + struct registry_context *ctx; + + switch (backend) { + case REG_UNKNOWN: + poptPrintUsage(pc, stderr, 0); + return NULL; + case REG_LOCAL: + error = reg_open_samba(NULL, &ctx, NULL, cmdline_credentials); + break; + case REG_REMOTE: + error = reg_open_remote(&ctx, NULL, cmdline_credentials, remote_host, NULL); + break; + case REG_NULL: + error = reg_open_local(NULL, &ctx, NULL, cmdline_credentials); + break; + } + + if (!W_ERROR_IS_OK(error)) { + fprintf(stderr, "Error: %s\n", win_errstr(error)); + return NULL; + } + + return ctx; +} + +int main(int argc, const char **argv) { int opt; poptContext pc; char *outputfile = NULL; + enum reg_backend backend1 = REG_UNKNOWN, backend2 = REG_UNKNOWN; + const char *remote1 = NULL, *remote2 = NULL; struct registry_context *h1 = NULL, *h2 = NULL; - int from_null = 0; WERROR error; - struct reg_diff *diff; struct poptOption long_options[] = { POPT_AUTOHELP - {"output", 'o', POPT_ARG_STRING, &outputfile, 'o', "output file to use", NULL }, - {"null", 'n', POPT_ARG_NONE, &from_null, 'n', "Diff from NULL", NULL }, - {"remote", 'R', POPT_ARG_STRING, NULL, 0, "Connect to remote server" , NULL }, - {"local", 'L', POPT_ARG_NONE, NULL, 0, "Open local registry", NULL }, + {"output", 'o', POPT_ARG_STRING, &outputfile, 0, "output file to use", NULL }, + {"null", 'n', POPT_ARG_NONE, NULL, 'n', "Diff from NULL", NULL }, + {"remote", 'R', POPT_ARG_STRING, NULL, 'R', "Connect to remote server" , NULL }, + {"local", 'L', POPT_ARG_NONE, NULL, 'L', "Open local registry", NULL }, POPT_COMMON_SAMBA POPT_COMMON_CREDENTIALS POPT_COMMON_VERSION { NULL } }; + TALLOC_CTX *ctx; + void *callback_data; + struct reg_diff_callbacks *callbacks; - registry_init(); + ctx = talloc_init("regdiff"); - pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0); + pc = poptGetContext(argv[0], argc, argv, long_options,0); while((opt = poptGetNextOpt(pc)) != -1) { error = WERR_OK; switch(opt) { case 'L': - if (!h1 && !from_null) error = reg_open_local(NULL, &h1, NULL, cmdline_credentials); - else if (!h2) error = reg_open_local(NULL, &h2, NULL, cmdline_credentials); + if (backend1 == REG_UNKNOWN) + backend1 = REG_LOCAL; + else if (backend2 == REG_UNKNOWN) + backend2 = REG_LOCAL; + break; + case 'n': + if (backend1 == REG_UNKNOWN) + backend1 = REG_NULL; + else if (backend2 == REG_UNKNOWN) + backend2 = REG_NULL; break; case 'R': - if (!h1 && !from_null) - error = reg_open_remote(&h1, NULL, cmdline_credentials, - poptGetOptArg(pc), NULL); - else if (!h2) error = reg_open_remote(&h2, NULL, cmdline_credentials, - poptGetOptArg(pc), NULL); + if (backend1 == REG_UNKNOWN) { + backend1 = REG_REMOTE; + remote1 = poptGetOptArg(pc); + } else if (backend2 == REG_UNKNOWN) { + backend2 = REG_REMOTE; + remote2 = poptGetOptArg(pc); + } break; } - if (!W_ERROR_IS_OK(error)) { - fprintf(stderr, "Error: %s\n", win_errstr(error)); - return 1; - } } + h1 = open_backend(pc, backend1, remote1); + if (h1 == NULL) + return 1; + + h2 = open_backend(pc, backend2, remote2); + if (h2 == NULL) + return 1; + poptFreeContext(pc); - diff = reg_generate_diff(NULL, h1, h2); - if (!diff) { - fprintf(stderr, "Unable to generate diff between keys\n"); + error = reg_dotreg_diff_save(ctx, outputfile, &callbacks, &callback_data); + if (!W_ERROR_IS_OK(error)) { + fprintf(stderr, "Problem saving registry diff to '%s': %s\n", outputfile, win_errstr(error)); return -1; } - reg_diff_save(diff, outputfile); + error = reg_generate_diff(h1, h2, callbacks, callback_data); + if (!W_ERROR_IS_OK(error)) { + fprintf(stderr, "Unable to generate diff between keys: %s\n", win_errstr(error)); + return -1; + } return 0; } diff --git a/source4/lib/registry/tools/regpatch.c b/source4/lib/registry/tools/regpatch.c index 83ad5575ef..1e6d15a7af 100644 --- a/source4/lib/registry/tools/regpatch.c +++ b/source4/lib/registry/tools/regpatch.c @@ -2,7 +2,7 @@ Unix SMB/CIFS implementation. simple registry frontend - Copyright (C) 2004-2005 Jelmer Vernooij, jelmer@samba.org + Copyright (C) 2004-2007 Jelmer Vernooij, jelmer@samba.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,6 +22,8 @@ #include "lib/events/events.h" #include "lib/registry/registry.h" #include "lib/cmdline/popt_common.h" +#include "lib/registry/tools/common.h" +#include "lib/registry/patchfile.h" int main(int argc, char **argv) { @@ -29,12 +31,12 @@ int main(int argc, char **argv) poptContext pc; const char *patch; struct registry_context *h; + const char *file = NULL; const char *remote = NULL; - struct reg_diff *diff; - WERROR error; struct poptOption long_options[] = { POPT_AUTOHELP {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL}, + {"file", 'F', POPT_ARG_STRING, &file, 0, "file path", NULL }, POPT_COMMON_SAMBA POPT_COMMON_CREDENTIALS { NULL } @@ -45,29 +47,24 @@ int main(int argc, char **argv) while((opt = poptGetNextOpt(pc)) != -1) { } - registry_init(); - if (remote) { - error = reg_open_remote (&h, NULL, cmdline_credentials, remote, NULL); + h = reg_common_open_remote (remote, cmdline_credentials); } else { - error = reg_open_local (NULL, &h, NULL, cmdline_credentials); + h = reg_common_open_local (cmdline_credentials); } - - if (W_ERROR_IS_OK(error)) { - fprintf(stderr, "Error: %s\n", win_errstr(error)); + + if (h == NULL) return 1; - } patch = poptGetArg(pc); - poptFreeContext(pc); - - diff = reg_diff_load(NULL, patch); - if (!diff) { - fprintf(stderr, "Unable to load registry patch from `%s'\n", patch); + if (patch == NULL) { + poptPrintUsage(pc, stderr, 0); return 1; } - reg_diff_apply(diff, h); + poptFreeContext(pc); + + reg_diff_apply(patch, h); return 0; } diff --git a/source4/lib/registry/tools/regshell.c b/source4/lib/registry/tools/regshell.c index f431c81bf8..0887bc91f3 100644 --- a/source4/lib/registry/tools/regshell.c +++ b/source4/lib/registry/tools/regshell.c @@ -2,7 +2,7 @@ Unix SMB/CIFS implementation. simple registry frontend - Copyright (C) Jelmer Vernooij 2004 + Copyright (C) Jelmer Vernooij 2004-2007 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -25,8 +25,15 @@ #include "system/time.h" #include "lib/smbreadline/smbreadline.h" #include "librpc/gen_ndr/ndr_security.h" +#include "lib/registry/tools/common.h" -/* +struct regshell_context { + struct registry_context *registry; + const char *path; + struct registry_key *current; +}; + +/* * * ck/cd - change key * ls - list values/keys * rmval/rm - remove value @@ -40,29 +47,40 @@ * exit */ -static struct registry_key *cmd_info(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_info(struct regshell_context *ctx, int argc, char **argv) { struct security_descriptor *sec_desc = NULL; time_t last_mod; WERROR error; + const char *classname; + NTTIME last_change; + + error = reg_key_get_info(ctx, ctx->current, &classname, NULL, NULL, &last_change); + if (!W_ERROR_IS_OK(error)) { + printf("Error getting key info: %s\n", win_errstr(error)); + return error; + } + - printf("Name: %s\n", cur->name); - printf("Full path: %s\n", cur->path); - printf("Key Class: %s\n", cur->class_name); - last_mod = nt_time_to_unix(cur->last_mod); + printf("Name: %s\n", strchr(ctx->path, '\\')?strrchr(ctx->path, '\\')+1: + ctx->path); + printf("Full path: %s\n", ctx->path); + printf("Key Class: %s\n", classname); + last_mod = nt_time_to_unix(last_change); printf("Time Last Modified: %s\n", ctime(&last_mod)); - error = reg_get_sec_desc(mem_ctx, cur, &sec_desc); + error = reg_get_sec_desc(ctx, ctx->current, &sec_desc); if (!W_ERROR_IS_OK(error)) { printf("Error getting security descriptor\n"); - } else { - ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "Security", sec_desc); - } + return error; + } + ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "Security", sec_desc); talloc_free(sec_desc); - return cur; + + return WERR_OK; } -static struct registry_key *cmd_predef(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *cur, int argc, char **argv) +static WERROR cmd_predef(struct regshell_context *ctx, int argc, char **argv) { struct registry_key *ret = NULL; if (argc < 2) { @@ -70,165 +88,195 @@ static struct registry_key *cmd_predef(TALLOC_CTX *mem_ctx, struct registry_cont } else if (!ctx) { fprintf(stderr, "No full registry loaded, no predefined keys defined\n"); } else { - WERROR error = reg_get_predefined_key_by_name(ctx, argv[1], &ret); + WERROR error = reg_get_predefined_key_by_name(ctx->registry, argv[1], &ret); if (!W_ERROR_IS_OK(error)) { fprintf(stderr, "Error opening predefined key %s: %s\n", argv[1], win_errstr(error)); - ret = NULL; + return error; } } - return ret; + + return WERR_OK; } -static struct registry_key *cmd_pwd(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_pwd(struct regshell_context *ctx, + int argc, char **argv) { - printf("%s\n", cur->path); - return cur; + printf("%s\n", ctx->path); + return WERR_OK; } -static struct registry_key *cmd_set(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_set(struct regshell_context *ctx, int argc, char **argv) { struct registry_value val; WERROR error; if (argc < 4) { fprintf(stderr, "Usage: set value-name type value\n"); - return cur; + return WERR_INVALID_PARAM; } - if (!reg_string_to_val(mem_ctx, argv[2], argv[3], &val.data_type, &val.data)) { + if (!reg_string_to_val(ctx, argv[2], argv[3], &val.data_type, + &val.data)) { fprintf(stderr, "Unable to interpret data\n"); - return cur; + return WERR_INVALID_PARAM; } - error = reg_val_set(cur, argv[1], val.data_type, val.data); + error = reg_val_set(ctx->current, argv[1], val.data_type, val.data); if (!W_ERROR_IS_OK(error)) { fprintf(stderr, "Error setting value: %s\n", win_errstr(error)); - return NULL; + return error; } - return cur; + + return WERR_OK; } -static struct registry_key *cmd_ck(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_ck(struct regshell_context *ctx, int argc, char **argv) { struct registry_key *new = NULL; WERROR error; + if(argc < 2) { - new = cur; + new = ctx->current; } else { - error = reg_open_key(mem_ctx, cur, argv[1], &new); + error = reg_open_key(ctx->registry, ctx->current, argv[1], &new); if(!W_ERROR_IS_OK(error)) { DEBUG(0, ("Error opening specified key: %s\n", win_errstr(error))); - return NULL; + return error; } } - printf("Current path is: %s\n", new->path); + ctx->path = talloc_asprintf(ctx, "%s\\%s", ctx->path, argv[1]); + printf("Current path is: %s\n", ctx->path); + ctx->current = new; - return new; + return WERR_OK; } -static struct registry_key *cmd_print(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_print(struct regshell_context *ctx, int argc, char **argv) { - struct registry_value *value; + uint32_t value_type; + DATA_BLOB value_data; WERROR error; if (argc != 2) { fprintf(stderr, "Usage: print <valuename>"); - return NULL; + return WERR_INVALID_PARAM; } - error = reg_key_get_value_by_name(mem_ctx, cur, argv[1], &value); + error = reg_key_get_value_by_name(ctx, ctx->current, argv[1], + &value_type, &value_data); if (!W_ERROR_IS_OK(error)) { fprintf(stderr, "No such value '%s'\n", argv[1]); - return NULL; + return error; } - printf("%s\n%s\n", str_regtype(value->data_type), reg_val_data_string(mem_ctx, value->data_type, &value->data)); - return NULL; + printf("%s\n%s\n", str_regtype(value_type), + reg_val_data_string(ctx, value_type, value_data)); + + return WERR_OK; } -static struct registry_key *cmd_ls(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_ls(struct regshell_context *ctx, int argc, char **argv) { int i; WERROR error; struct registry_value *value; - struct registry_key *sub; - for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, cur, i, &sub)); i++) { - printf("K %s\n", sub->name); + uint32_t data_type; + DATA_BLOB data; + const char *name; + + for (i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(ctx, ctx->current, i, &name, NULL, NULL)); i++) { + printf("K %s\n", name); } - if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) { + if (!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) { DEBUG(0, ("Error occured while browsing thru keys: %s\n", win_errstr(error))); } - for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(mem_ctx, cur, i, &value)); i++) { - printf("V \"%s\" %s %s\n", value->name, str_regtype(value->data_type), reg_val_data_string(mem_ctx, value->data_type, &value->data)); + for (i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(ctx, ctx->current, i, &name, &data_type, &data)); i++) { + printf("V \"%s\" %s %s\n", value->name, str_regtype(data_type), + reg_val_data_string(ctx, data_type, data)); } - return NULL; + return WERR_OK; } -static struct registry_key *cmd_mkkey(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_mkkey(struct regshell_context *ctx, int argc, char **argv) { struct registry_key *tmp; + WERROR error; + if(argc < 2) { fprintf(stderr, "Usage: mkkey <keyname>\n"); - return NULL; + return WERR_INVALID_PARAM; } + + error = reg_key_add_name(ctx, ctx->current, argv[1], 0, NULL, &tmp); - if(!W_ERROR_IS_OK(reg_key_add_name(mem_ctx, cur, argv[1], 0, NULL, &tmp))) { + if (!W_ERROR_IS_OK(error)) { fprintf(stderr, "Error adding new subkey '%s'\n", argv[1]); - return NULL; + return error; } - return NULL; + return WERR_OK; } -static struct registry_key *cmd_rmkey(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_rmkey(struct regshell_context *ctx, + int argc, char **argv) { + WERROR error; + if(argc < 2) { fprintf(stderr, "Usage: rmkey <name>\n"); - return NULL; + return WERR_INVALID_PARAM; } - if(!W_ERROR_IS_OK(reg_key_del(cur, argv[1]))) { + error = reg_key_del(ctx->current, argv[1]); + if(!W_ERROR_IS_OK(error)) { fprintf(stderr, "Error deleting '%s'\n", argv[1]); + return error; } else { fprintf(stderr, "Successfully deleted '%s'\n", argv[1]); } - return NULL; + return WERR_OK; } -static struct registry_key *cmd_rmval(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_rmval(struct regshell_context *ctx, int argc, char **argv) { + WERROR error; + if(argc < 2) { fprintf(stderr, "Usage: rmval <valuename>\n"); - return NULL; + return WERR_INVALID_PARAM; } - if(!W_ERROR_IS_OK(reg_del_value(cur, argv[1]))) { + error = reg_del_value(ctx->current, argv[1]); + if(!W_ERROR_IS_OK(error)) { fprintf(stderr, "Error deleting value '%s'\n", argv[1]); + return error; } else { fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]); } - return NULL; + return WERR_OK; } -static struct registry_key *cmd_exit(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_exit(struct regshell_context *ctx, + int argc, char **argv) { exit(0); - return NULL; + return WERR_OK; } -static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *, int, char **); +static WERROR cmd_help(struct regshell_context *ctx, int, char **); static struct { const char *name; const char *alias; const char *help; - struct registry_key *(*handle)(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *, int argc, char **argv); + WERROR (*handle)(struct regshell_context *ctx, + int argc, char **argv); } regshell_cmds[] = { {"ck", "cd", "Change current key", cmd_ck }, {"info", "i", "Show detailed information of a key", cmd_info }, @@ -245,17 +293,19 @@ static struct { {NULL } }; -static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *cur, int argc, char **argv) +static WERROR cmd_help(struct regshell_context *ctx, + int argc, char **argv) { int i; printf("Available commands:\n"); for(i = 0; regshell_cmds[i].name; i++) { printf("%s - %s\n", regshell_cmds[i].name, regshell_cmds[i].help); } - return NULL; + return WERR_OK; } -static struct registry_key *process_cmd(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *k, char *line) +static WERROR process_cmd(struct regshell_context *ctx, + char *line) { int argc; char **argv = NULL; @@ -263,19 +313,19 @@ static struct registry_key *process_cmd(TALLOC_CTX *mem_ctx, struct registry_con if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) { fprintf(stderr, "regshell: %s\n", poptStrerror(ret)); - return k; + return WERR_INVALID_PARAM; } for(i = 0; regshell_cmds[i].name; i++) { if(!strcmp(regshell_cmds[i].name, argv[0]) || (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) { - return regshell_cmds[i].handle(mem_ctx, ctx, k, argc, argv); + return regshell_cmds[i].handle(ctx, argc, argv); } } fprintf(stderr, "No such command '%s'\n", argv[0]); - return k; + return WERR_INVALID_PARAM; } #define MAX_COMPLETIONS 100 @@ -333,7 +383,7 @@ cleanup: static char **reg_complete_key(const char *text, int start, int end) { struct registry_key *base; - struct registry_key *subkey; + const char *subkeyname; int i, j = 1; int samelen = 0; int len; @@ -351,10 +401,11 @@ static char **reg_complete_key(const char *text, int start, int end) len = strlen(text); for(i = 0; j < MAX_COMPLETIONS-1; i++) { - status = reg_key_get_subkey_by_index(mem_ctx, base, i, &subkey); + status = reg_key_get_subkey_by_index(mem_ctx, base, i, &subkeyname, + NULL, NULL); if(W_ERROR_IS_OK(status)) { - if(!strncmp(text, subkey->name, len)) { - matches[j] = strdup(subkey->name); + if(!strncmp(text, subkeyname, len)) { + matches[j] = strdup(subkeyname); j++; if (j == 1) @@ -381,7 +432,8 @@ static char **reg_complete_key(const char *text, int start, int end) if (j == 2) { /* Exact match */ asprintf(&matches[0], "%s%s", base_n, matches[1]); } else { - asprintf(&matches[0], "%s%s", base_n, talloc_strndup(mem_ctx, matches[1], samelen)); + asprintf(&matches[0], "%s%s", base_n, + talloc_strndup(mem_ctx, matches[1], samelen)); } talloc_free(mem_ctx); @@ -400,19 +452,17 @@ static char **reg_completion(const char *text, int start, int end) } } - int main(int argc, char **argv) +int main(int argc, char **argv) { int opt; - const char *backend = NULL; - struct registry_key *curkey = NULL; + const char *file = NULL; poptContext pc; - WERROR error; - TALLOC_CTX *mem_ctx = talloc_init("cmd"); const char *remote = NULL; - struct registry_context *h = NULL; + struct regshell_context *ctx; + bool ret = true; struct poptOption long_options[] = { POPT_AUTOHELP - {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL}, + {"file", 'F', POPT_ARG_STRING, &file, 0, "open hive file", NULL }, {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL}, POPT_COMMON_SAMBA POPT_COMMON_CREDENTIALS @@ -425,64 +475,62 @@ static char **reg_completion(const char *text, int start, int end) while((opt = poptGetNextOpt(pc)) != -1) { } - registry_init(); + ctx = talloc_zero(NULL, struct regshell_context); - if (remote) { - error = reg_open_remote (&h, NULL, cmdline_credentials, remote, NULL); - } else if (backend) { - error = reg_open_hive(NULL, backend, poptGetArg(pc), NULL, cmdline_credentials, &curkey); + if (remote != NULL) { + ctx->registry = reg_common_open_remote(remote, cmdline_credentials); + } else if (file != NULL) { + ctx->current = reg_common_open_file(file, cmdline_credentials); + ctx->registry = ctx->current->context; + ctx->path = talloc_strdup(ctx, ""); } else { - error = reg_open_local(NULL, &h, NULL, cmdline_credentials); + ctx->registry = reg_common_open_local(cmdline_credentials); } - if(!W_ERROR_IS_OK(error)) { - fprintf(stderr, "Unable to open registry\n"); + if (ctx->registry == NULL) return 1; - } - if (h) { + if (ctx->current == NULL) { int i; for (i = 0; reg_predefined_keys[i].handle; i++) { WERROR err; - err = reg_get_predefined_key(h, reg_predefined_keys[i].handle, &curkey); + err = reg_get_predefined_key(ctx->registry, + reg_predefined_keys[i].handle, + &ctx->current); if (W_ERROR_IS_OK(err)) { + ctx->path = talloc_strdup(ctx, reg_predefined_keys[i].name); break; } else { - curkey = NULL; + ctx->current = NULL; } } } - if (!curkey) { + if (ctx->current == NULL) { fprintf(stderr, "Unable to access any of the predefined keys\n"); return -1; } poptFreeContext(pc); - while(True) { + while (true) { char *line, *prompt; - if(curkey->hive->root->name) { - asprintf(&prompt, "%s:%s> ", curkey->hive->root->name, curkey->path); - } else { - asprintf(&prompt, "%s> ", curkey->path); - } + asprintf(&prompt, "%s> ", ctx->path); - current_key = curkey; /* No way to pass a void * pointer - via readline :-( */ + current_key = ctx->current; /* No way to pass a void * pointer + via readline :-( */ line = smb_readline(prompt, NULL, reg_completion); - if(!line) + if (line == NULL) break; - if(line[0] != '\n') { - struct registry_key *new = process_cmd(mem_ctx, h, curkey, line); - if(new)curkey = new; + if (line[0] != '\n') { + ret = W_ERROR_IS_OK(process_cmd(ctx, line)); } } - talloc_free(mem_ctx); + talloc_free(ctx); - return 0; + return (ret?0:1); } diff --git a/source4/lib/registry/tools/regtree.c b/source4/lib/registry/tools/regtree.c index d026d2824f..8d2460a93e 100644 --- a/source4/lib/registry/tools/regtree.c +++ b/source4/lib/registry/tools/regtree.c @@ -2,7 +2,7 @@ Unix SMB/CIFS implementation. simple registry frontend - Copyright (C) Jelmer Vernooij 2004 + Copyright (C) Jelmer Vernooij 2004-2007 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -20,52 +20,66 @@ #include "includes.h" #include "lib/registry/registry.h" +#include "lib/registry/tools/common.h" #include "lib/events/events.h" #include "lib/cmdline/popt_common.h" -static void print_tree(int l, struct registry_key *p, int fullpath, int novals) +/** + * Print a registry key recursively + * + * @param level Level at which to print + * @param p Key to print + * @param fullpath Whether the full pat hshould be printed or just the last bit + * @param novals Whether values should not be printed + */ +static void print_tree(int level, struct registry_key *p, + const char *name, + bool fullpath, bool novals) { struct registry_key *subkey; - struct registry_value *value; + const char *valuename; + const char *keyname; + uint32_t value_type; + DATA_BLOB value_data; struct security_descriptor *sec_desc; WERROR error; int i; TALLOC_CTX *mem_ctx; - for(i = 0; i < l; i++) putchar(' '); - - /* Hive name */ - if(p->hive->root == p) { - if(p->hive->root->name) printf("%s\n", p->hive->root->name); else printf("<No Name>\n"); - } else { - if(!p->name) printf("<No Name>\n"); - if(fullpath) printf("%s\n", p->path); - else printf("%s\n", p->name?p->name:"(NULL)"); - } + for(i = 0; i < level; i++) putchar(' '); puts(name); mem_ctx = talloc_init("print_tree"); - for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, p, i, &subkey)); i++) { - print_tree(l+1, subkey, fullpath, novals); + for (i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, p, i, &keyname, NULL, NULL)); i++) { + SMB_ASSERT(strlen(keyname) > 0); + if (!W_ERROR_IS_OK(reg_open_key(mem_ctx, p, keyname, &subkey))) + continue; + print_tree(level+1, subkey, (fullpath && strlen(name))? + talloc_asprintf(mem_ctx, "%s\\%s", name, keyname): + keyname, fullpath, novals); } talloc_free(mem_ctx); if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) { - DEBUG(0, ("Error occured while fetching subkeys for '%s': %s\n", p->path, win_errstr(error))); + DEBUG(0, ("Error occured while fetching subkeys for '%s': %s\n", + name, win_errstr(error))); } - if(!novals) { + if (!novals) { mem_ctx = talloc_init("print_tree"); - for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(mem_ctx, p, i, &value)); i++) { + for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(mem_ctx, + p, i, &valuename, &value_type, &value_data)); i++) { int j; char *desc; - for(j = 0; j < l+1; j++) putchar(' '); - desc = reg_val_description(mem_ctx, value); + for(j = 0; j < level+1; j++) putchar(' '); + desc = reg_val_description(mem_ctx, valuename, value_type, + value_data); printf("%s\n", desc); } talloc_free(mem_ctx); if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) { - DEBUG(0, ("Error occured while fetching values for '%s': %s\n", p->path, win_errstr(error))); + DEBUG(0, ("Error occured while fetching values for '%s': %s\n", + name, win_errstr(error))); } } @@ -79,21 +93,22 @@ static void print_tree(int l, struct registry_key *p, int fullpath, int novals) int main(int argc, char **argv) { int opt, i; - const char *backend = NULL; + const char *file = NULL; const char *remote = NULL; poptContext pc; struct registry_context *h = NULL; - struct registry_key *root = NULL; + struct registry_key *start_key = NULL; WERROR error; - int fullpath = 0, no_values = 0; + bool fullpath = false, no_values = false; struct poptOption long_options[] = { POPT_AUTOHELP - {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL}, - {"fullpath", 'f', POPT_ARG_NONE, &fullpath, 0, "show full paths", NULL}, + {"file", 'F', POPT_ARG_STRING, &file, 0, "file path", NULL }, {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL }, + {"fullpath", 'f', POPT_ARG_NONE, &fullpath, 0, "show full paths", NULL}, {"no-values", 'V', POPT_ARG_NONE, &no_values, 0, "don't show values", NULL}, POPT_COMMON_SAMBA POPT_COMMON_CREDENTIALS + POPT_COMMON_VERSION { NULL } }; @@ -102,48 +117,35 @@ int main(int argc, char **argv) while((opt = poptGetNextOpt(pc)) != -1) { } - registry_init(); - - if (remote) { - error = reg_open_remote(&h, NULL, cmdline_credentials, remote, NULL); - - if(!W_ERROR_IS_OK(error)) { - fprintf(stderr, "Unable to open remote registry at %s:%s \n", remote, win_errstr(error)); - return 1; - } - - } else if (backend) { - error = reg_open_hive(NULL, backend, poptGetArg(pc), NULL, cmdline_credentials, &root); - - if(!W_ERROR_IS_OK(error)) { - fprintf(stderr, "Unable to open '%s' with backend '%s':%s \n", poptGetArg(pc), backend, win_errstr(error)); - return 1; - } + if (remote != NULL) { + h = reg_common_open_remote(remote, cmdline_credentials); + } else if (file != NULL) { + start_key = reg_common_open_file(file, cmdline_credentials); } else { - error = reg_open_local (NULL, &h, NULL, cmdline_credentials); - - if(!W_ERROR_IS_OK(error)) { - fprintf(stderr, "Unable to open local registry:%s \n", win_errstr(error)); - return 1; - } - + h = reg_common_open_local(cmdline_credentials); } + if (h == NULL && start_key == NULL) + return 1; + poptFreeContext(pc); error = WERR_OK; - if (root != NULL) { - print_tree(0, root, fullpath, no_values); + if (start_key != NULL) { + print_tree(0, start_key, "", fullpath, no_values); } else { for(i = 0; reg_predefined_keys[i].handle; i++) { - error = reg_get_predefined_key(h, reg_predefined_keys[i].handle, &root); + error = reg_get_predefined_key(h, reg_predefined_keys[i].handle, + &start_key); if (!W_ERROR_IS_OK(error)) { - fprintf(stderr, "Skipping %s\n", reg_predefined_keys[i].name); + fprintf(stderr, "Skipping %s: %s\n", reg_predefined_keys[i].name, + win_errstr(error)); continue; } - SMB_ASSERT(root); - print_tree(0, root, fullpath, no_values); + SMB_ASSERT(start_key != NULL); + print_tree(0, start_key, reg_predefined_keys[i].name, fullpath, + no_values); } } |