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/cmdline/popt_common.h"
25 static void writediff(struct registry_key *oldkey, struct registry_key *newkey, FILE *out)
28 struct registry_key *t1, *t2;
29 struct registry_value *v1, *v2;
30 WERROR error1, error2;
31 TALLOC_CTX *mem_ctx = talloc_init("writediff");
33 for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(mem_ctx, oldkey, i, &t1)); i++) {
34 error2 = reg_key_get_subkey_by_name(mem_ctx, newkey, t1->name, &t2);
35 if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
36 fprintf(out, "-%s\n", t1->path+1);
37 } else if(!W_ERROR_IS_OK(error2)) {
38 DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
42 talloc_destroy(mem_ctx);
44 if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
45 DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
49 mem_ctx = talloc_init("writediff");
51 for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(mem_ctx, newkey, i, &t1)); i++) {
52 error2 = reg_key_get_subkey_by_name(mem_ctx, oldkey, t1->name, &t2);
53 if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
54 fprintf(out, "\n[%s]\n", t1->path+1);
55 } else if(!W_ERROR_IS_OK(error2)) {
56 DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
58 writediff(t2, t1, out);
61 talloc_destroy(mem_ctx);
63 if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
64 DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
69 mem_ctx = talloc_init("writediff");
71 for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, newkey, i, &v1)); i++) {
72 error2 = reg_key_get_value_by_name(mem_ctx, oldkey, v1->name, &v2);
73 if ((W_ERROR_IS_OK(error2) && (v2->data_len != v1->data_len || memcmp(v1->data_blk, v2->data_blk, v1->data_len)))
74 || W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
75 fprintf(out, "\"%s\"=%s:%s\n", v1->name, str_regtype(v1->data_type), reg_val_data_string(mem_ctx, v1));
78 if(!W_ERROR_IS_OK(error2) && !W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
79 DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
83 talloc_destroy(mem_ctx);
85 if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
86 DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
90 mem_ctx = talloc_init("writediff");
92 for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, oldkey, i, &v1)); i++) {
93 error2 = reg_key_get_value_by_name(mem_ctx, newkey, v1->name, &v2);
94 if(W_ERROR_IS_OK(error2)) {
95 } else if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
96 fprintf(out, "\"%s\"=-\n", v1->name);
98 DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
102 talloc_destroy(mem_ctx);
104 if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
105 DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
110 int main(int argc, char **argv)
114 const char *backend1 = NULL, *backend2 = NULL;
115 const char *location2;
116 const char *credentials1= NULL, *credentials2 = NULL;
117 char *outputfile = NULL;
119 struct registry_context *h1 = NULL, *h2;
122 WERROR error, error2;
123 struct poptOption long_options[] = {
125 {"backend", 'b', POPT_ARG_STRING, NULL, 'b', "backend to use", NULL},
126 {"credentials", 'c', POPT_ARG_STRING, NULL, 'c', "credentials", NULL},
127 {"output", 'o', POPT_ARG_STRING, &outputfile, 'o', "output file to use", NULL },
128 {"null", 'n', POPT_ARG_NONE, &from_null, 'n', "Diff from NULL" },
133 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
134 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
138 pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
140 while((opt = poptGetNextOpt(pc)) != -1) {
143 if(!credentials1 && !from_null) credentials1 = poptGetOptArg(pc);
144 else if(!credentials2) credentials2 = poptGetOptArg(pc);
147 if(!backend1 && !from_null) backend1 = poptGetOptArg(pc);
148 else if(!backend2) backend2 = poptGetOptArg(pc);
152 setup_logging(argv[0], True);
155 const char *location1;
156 location1 = poptGetArg(pc);
158 poptPrintUsage(pc, stderr, 0);
162 if(!backend1) backend1 = "rpc";
164 error = reg_open(&h1, backend1, location1, credentials1);
165 if(!W_ERROR_IS_OK(error)) {
166 fprintf(stderr, "Unable to open '%s' with backend '%s'\n", location1, backend1);
171 location2 = poptGetArg(pc);
173 poptPrintUsage(pc, stderr, 0);
177 if(!backend2) backend2 = "rpc";
179 error = reg_open(&h2, backend2, location2, credentials2);
180 if(!W_ERROR_IS_OK(error)) {
181 fprintf(stderr, "Unable to open '%s' with backend '%s'\n", location2, backend2);
188 fd = fopen(outputfile, "w+");
190 fprintf(stderr, "Unable to open '%s'\n", outputfile);
195 fprintf(fd, "REGEDIT4\n\n");
196 fprintf(fd, "; Generated using regdiff\n");
198 error2 = error = WERR_OK;
200 for(i = 0; (!h1 || i < h1->num_hives) && i < h2->num_hives; i++) {
201 writediff(h1?h1->hives[i]->root:NULL, h2->hives[i]->root, fd);