307ec3793e3ecaf20f3db82276fe3453d2b683ab
[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_free(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_free(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) && data_blob_equal(&v1->data, &v2->data)) 
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_free(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_free(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                 {"output", 'o', POPT_ARG_STRING, &outputfile, 'o', "output file to use", NULL },
125                 {"null", 'n', POPT_ARG_NONE, &from_null, 'n', "Diff from NULL", NULL },
126                 {"remote", 'R', POPT_ARG_STRING, NULL, 0, "Connect to remote server" , NULL },
127                 {"local", 'L', POPT_ARG_NONE, NULL, 0, "Open local registry", NULL },
128                 POPT_COMMON_SAMBA
129                 POPT_COMMON_CREDENTIALS
130                 POPT_COMMON_VERSION
131                 POPT_TABLEEND
132         };
133
134         regdiff_init_subsystems;
135
136         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
137
138         while((opt = poptGetNextOpt(pc)) != -1) {
139                 error = WERR_OK;
140                 switch(opt)     {
141                 case 'L':
142                         if (!h1 && !from_null) error = reg_open_local(&h1);
143                         else if (!h2) error = reg_open_local(&h2);
144                         break;
145                 case 'R':
146                         if (!h1 && !from_null) 
147                                 error = reg_open_remote(&h1, cmdline_credentials, 
148                                                         poptGetOptArg(pc), NULL);
149                         else if (!h2) error = reg_open_remote(&h2, cmdline_credentials, 
150                                                               poptGetOptArg(pc), NULL);
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
160         poptFreeContext(pc);
161
162         if(outputfile) {
163                 fd = fopen(outputfile, "w+");
164                 if(!fd) {
165                         fprintf(stderr, "Unable to open '%s'\n", outputfile);
166                         return 1;
167                 }
168         }
169
170         fprintf(fd, "REGEDIT4\n\n");
171         fprintf(fd, "; Generated using regdiff, part of Samba\n");
172
173         error2 = error = WERR_OK; 
174
175         for(i = HKEY_CLASSES_ROOT; i <= HKEY_PERFORMANCE_NLSTEXT; i++) {
176                 struct registry_key *r1, *r2;
177                 error = reg_get_predefined_key(h1, i, &r1);
178                 if (!W_ERROR_IS_OK(error)) {
179                         DEBUG(0, ("Unable to open hive %s for backend 1\n", reg_get_predef_name(i)));
180                         continue;
181                 }
182                 
183                 error = reg_get_predefined_key(h2, i, &r2);
184                 if (!W_ERROR_IS_OK(error)) {
185                         DEBUG(0, ("Unable to open hive %s for backend 2\n", reg_get_predef_name(i)));
186                         continue;
187                 }
188
189                 writediff(r1, r2, fd); 
190         }
191
192         fclose(fd);
193
194         return 0;
195 }