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