r4155: More destinction between hives and predefined keys
[bbaumbach/samba-autobuild/.git] / source4 / lib / registry / tools / regdiff.c
1 /* 
2    Unix SMB/CIFS implementation.
3    simple registry frontend
4    
5    Copyright (C) Jelmer Vernooij 2004
6
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23 #include "dynconfig.h"
24 #include "registry.h"
25 #include "lib/cmdline/popt_common.h"
26
27 static void writediff(struct registry_key *oldkey, struct registry_key *newkey, FILE *out)
28 {
29         int i;
30         struct registry_key *t1, *t2;
31         struct registry_value *v1, *v2;
32         WERROR error1, error2;
33         TALLOC_CTX *mem_ctx = talloc_init("writediff");
34
35         for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(mem_ctx, oldkey, i, &t1)); i++) {
36                 error2 = reg_key_get_subkey_by_name(mem_ctx, newkey, t1->name, &t2);
37                 if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
38                         fprintf(out, "-%s\n", t1->path+1);
39                 } else if(!W_ERROR_IS_OK(error2)) {
40                         DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
41                 }
42         }
43
44         talloc_destroy(mem_ctx);
45
46         if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
47                 DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
48                 return;
49         }
50
51         mem_ctx = talloc_init("writediff");
52
53         for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(mem_ctx, newkey, i, &t1)); i++) {
54                 error2 = reg_key_get_subkey_by_name(mem_ctx, oldkey, t1->name, &t2);
55                 if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
56                         fprintf(out, "\n[%s]\n", t1->path+1);
57                 } else if(!W_ERROR_IS_OK(error2)) {
58                         DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
59                 }
60                 writediff(t2, t1, out);
61         }
62
63         talloc_destroy(mem_ctx);
64
65         if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
66                 DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
67                 return;
68         }
69
70
71         mem_ctx = talloc_init("writediff");
72
73         for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, newkey, i, &v1)); i++) {
74                 error2 = reg_key_get_value_by_name(mem_ctx, oldkey, v1->name, &v2);
75                 if ((W_ERROR_IS_OK(error2) && (v2->data_len != v1->data_len || memcmp(v1->data_blk, v2->data_blk, v1->data_len))) 
76                         || W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
77                         fprintf(out, "\"%s\"=%s:%s\n", v1->name, str_regtype(v1->data_type), reg_val_data_string(mem_ctx, v1));
78                 }
79
80                 if(!W_ERROR_IS_OK(error2) && !W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
81                         DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
82                 }
83         }
84
85         talloc_destroy(mem_ctx);
86
87         if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
88                 DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
89                 return;
90         }
91
92         mem_ctx = talloc_init("writediff");
93
94         for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, oldkey, i, &v1)); i++) {
95                 error2 = reg_key_get_value_by_name(mem_ctx, newkey, v1->name, &v2);
96                 if(W_ERROR_IS_OK(error2)) {
97                 } else if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
98                         fprintf(out, "\"%s\"=-\n", v1->name);
99                 } else {
100                         DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
101                 }
102         }
103
104         talloc_destroy(mem_ctx);
105
106         if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
107                 DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
108                 return;
109         }
110 }
111
112  int main(int argc, char **argv)
113 {
114         int opt;
115         poptContext pc;
116         char *outputfile = NULL;
117         FILE *fd = stdout;
118         struct registry_context *h1 = NULL, *h2 = NULL;
119         int from_null = 0;
120         int i;
121         WERROR error, error2;
122         struct poptOption long_options[] = {
123                 POPT_AUTOHELP
124                 POPT_COMMON_CREDENTIALS
125                 {"output", 'o', POPT_ARG_STRING, &outputfile, 'o', "output file to use", NULL },
126                 {"null", 'n', POPT_ARG_NONE, &from_null, 'n', "Diff from NULL", NULL },
127                 {"remote", 'R', POPT_ARG_STRING, NULL, 0, "Connect to remote server" , NULL },
128                 {"local", 'L', POPT_ARG_NONE, NULL, 0, "Open local registry", NULL },
129                 POPT_TABLEEND
130         };
131
132         regdiff_init_subsystems;
133
134         if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
135                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
136         }
137
138
139         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
140
141         while((opt = poptGetNextOpt(pc)) != -1) {
142                 error = WERR_OK;
143                 switch(opt)     {
144                 case 'L':
145                         if (!h1 && !from_null) error = reg_open_local(&h1);
146                         else if (!h2) error = reg_open_local(&h2);
147                         break;
148                 case 'R':
149                         if (!h1 && !from_null) error = reg_open_remote(&h1, cmdline_get_username(), cmdline_get_userpassword(), poptGetOptArg(pc));
150                         else if (!h2) error = reg_open_remote(&h2, cmdline_get_username(), cmdline_get_userpassword(), poptGetOptArg(pc));
151                         break;
152                 }
153
154                 if (!W_ERROR_IS_OK(error)) {
155                         fprintf(stderr, "Error: %s\n", win_errstr(error));
156                         return 1;
157                 }
158         }
159         setup_logging(argv[0], True);
160
161         poptFreeContext(pc);
162
163         if(outputfile) {
164                 fd = fopen(outputfile, "w+");
165                 if(!fd) {
166                         fprintf(stderr, "Unable to open '%s'\n", outputfile);
167                         return 1;
168                 }
169         }
170
171         fprintf(fd, "REGEDIT4\n\n");
172         fprintf(fd, "; Generated using regdiff, part of Samba\n");
173
174         error2 = error = WERR_OK; 
175
176         for(i = HKEY_CLASSES_ROOT; i <= HKEY_PERFORMANCE_NLSTEXT; i++) {
177                 struct registry_key *r1, *r2;
178                 error = reg_get_predefined_key(h1, i, &r1);
179                 if (!W_ERROR_IS_OK(error)) {
180                         DEBUG(0, ("Unable to open hive %s for backend 1\n", reg_get_predef_name(i)));
181                         continue;
182                 }
183                 
184                 error = reg_get_predefined_key(h2, i, &r2);
185                 if (!W_ERROR_IS_OK(error)) {
186                         DEBUG(0, ("Unable to open hive %s for backend 2\n", reg_get_predef_name(i)));
187                         continue;
188                 }
189
190                 writediff(r1, r2, fd); 
191         }
192
193         fclose(fd);
194
195         return 0;
196 }