r825: - Introduce support for multiple roots (or 'hives')
[ab/samba.git/.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
24 static void writediff(REG_KEY *oldkey, REG_KEY *newkey, FILE *out)
25 {
26         int i;
27         REG_KEY *t1, *t2;
28         REG_VAL *v1, *v2;
29         WERROR error1, error2;
30
31         for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(oldkey, i, &t1)); i++) {
32                 error2 = reg_key_get_subkey_by_name(newkey, reg_key_name(t1), &t2);
33                 if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
34                         fprintf(out, "-%s\n", reg_key_get_path(t1)+1);
35                 } else if(!W_ERROR_IS_OK(error2)) {
36                         DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
37                 }
38         }
39
40         if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
41                 DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
42                 return;
43         }
44
45         for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(newkey, i, &t1)); i++) {
46                 error2 = reg_key_get_subkey_by_name(oldkey, reg_key_name(t1), &t2);
47                 if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
48                         fprintf(out, "\n[%s]\n", reg_key_get_path(t1)+1);
49                 } else if(!W_ERROR_IS_OK(error2)) {
50                         DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
51                 }
52                 writediff(t2, t1, out);
53         }
54
55         if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
56                 DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
57                 return;
58         }
59
60         for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(newkey, i, &v1)); i++) {
61                 error2 = reg_key_get_value_by_name(oldkey, reg_val_name(v1), &v2);
62                 if ((W_ERROR_IS_OK(error2) && (reg_val_size(v2) != reg_val_size(v1) || memcmp(reg_val_data_blk(v1), reg_val_data_blk(v2), reg_val_size(v1)))) 
63                         || W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
64                         fprintf(out, "\"%s\"=%s:%s\n", reg_val_name(v1), str_regtype(reg_val_type(v1)), reg_val_data_string(v1));
65                 }
66
67                 if(!W_ERROR_IS_OK(error2) && !W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
68                         DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
69                 }
70         }
71
72         if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
73                 DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
74                 return;
75         }
76
77
78         for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(oldkey, i, &v1)); i++) {
79                 error2 = reg_key_get_value_by_name(newkey, reg_val_name(v1), &v2);
80                 if(W_ERROR_IS_OK(error2)) {
81                 } else if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
82                         fprintf(out, "\"%s\"=-\n", reg_val_name(v1));
83                 } else {
84                         DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
85                 }
86         }
87
88         if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
89                 DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
90                 return;
91         }
92 }
93
94  int main(int argc, char **argv)
95 {
96         int opt;
97         poptContext pc;
98         const char *backend1 = NULL, *backend2 = NULL;
99         const char *location2;
100         const char *credentials1= NULL, *credentials2 = NULL;
101         char *outputfile = NULL;
102         FILE *fd = stdout;
103         REG_HANDLE *h1, *h2;
104         REG_KEY *root1 = NULL, *root2;
105         int from_null = 0;
106         int i;
107         WERROR error, error2;
108         struct poptOption long_options[] = {
109                 POPT_AUTOHELP
110                 {"backend", 'b', POPT_ARG_STRING, NULL, 'b', "backend to use", NULL},
111                 {"credentials", 'c', POPT_ARG_STRING, NULL, 'c', "credentials", NULL},
112                 {"output", 'o', POPT_ARG_STRING, &outputfile, 'o', "output file to use", NULL },
113                 {"null", 'n', POPT_ARG_NONE, &from_null, 'n', "Diff from NULL" },
114                 POPT_TABLEEND
115         };
116
117         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
118         
119         while((opt = poptGetNextOpt(pc)) != -1) {
120                 switch(opt)     {
121                 case 'c':
122                         if(!credentials1 && !from_null) credentials1 = poptGetOptArg(pc);
123                         else if(!credentials2) credentials2 = poptGetOptArg(pc);
124                         break;
125                 case 'b':
126                                 if(!backend1 && !from_null) backend1 = poptGetOptArg(pc);
127                                 else if(!backend2) backend2 = poptGetOptArg(pc);
128                                 break;
129                 }
130         }
131         setup_logging(argv[0], True);
132
133         if(!from_null) {
134                 const char *location1;
135                 location1 = poptGetArg(pc);
136                 if(!location1) {
137                         poptPrintUsage(pc, stderr, 0);
138                         return 1;
139                 }
140
141                 if(!backend1) backend1 = "dir";
142
143                 error = reg_open(backend1, location1, credentials1, &h1);
144                 if(!W_ERROR_IS_OK(error)) {
145                         fprintf(stderr, "Unable to open '%s' with backend '%s'\n", location1, backend1);
146                         return 1;
147                 }
148         }
149
150         location2 = poptGetArg(pc);
151         if(!location2) {
152                 poptPrintUsage(pc, stderr, 0);
153                 return 2;
154         }
155
156         if(!backend2) backend2 = "dir";
157         
158         error = reg_open(backend2, location2, credentials2, &h2);
159         if(!W_ERROR_IS_OK(error)) {
160                 fprintf(stderr, "Unable to open '%s' with backend '%s'\n", location2, backend2);
161                 return 1;
162         }
163         
164         poptFreeContext(pc);
165
166         if(outputfile) {
167                 fd = fopen(outputfile, "w+");
168                 if(!fd) {
169                         fprintf(stderr, "Unable to open '%s'\n", outputfile);
170                         return 1;
171                 }
172         }
173
174         fprintf(fd, "REGEDIT4\n\n");
175         fprintf(fd, "; Generated using regdiff\n");
176
177         error2 = error = WERR_OK; 
178         
179         for(i = 0; ; i++) {
180                 if(backend1) error = reg_get_hive(h1, i, &root1);
181                 else root1 = NULL;
182
183                 if(!W_ERROR_IS_OK(error)) break;
184         
185                 if(backend2) error2 = reg_get_hive(h2, i, &root2);
186                 else root2 = NULL;
187                 
188                 if(!W_ERROR_IS_OK(error2)) break;
189         
190                 writediff(root1, root2, fd); 
191
192                 if(!root1 && !root2) break;
193         }
194
195         fclose(fd);
196         
197         return 0;
198 }