r3453: - split out the auth and popt includes
[kai/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 "lib/cmdline/popt_common.h"
24
25 static void writediff(struct registry_key *oldkey, struct registry_key *newkey, FILE *out)
26 {
27         int i;
28         struct registry_key *t1, *t2;
29         struct registry_value *v1, *v2;
30         WERROR error1, error2;
31         TALLOC_CTX *mem_ctx = talloc_init("writediff");
32
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)));
39                 }
40         }
41
42         talloc_destroy(mem_ctx);
43
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)));
46                 return;
47         }
48
49         mem_ctx = talloc_init("writediff");
50
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)));
57                 }
58                 writediff(t2, t1, out);
59         }
60
61         talloc_destroy(mem_ctx);
62
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)));
65                 return;
66         }
67
68
69         mem_ctx = talloc_init("writediff");
70
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));
76                 }
77
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)));
80                 }
81         }
82
83         talloc_destroy(mem_ctx);
84
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)));
87                 return;
88         }
89
90         mem_ctx = talloc_init("writediff");
91
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);
97                 } else {
98                         DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
99                 }
100         }
101
102         talloc_destroy(mem_ctx);
103
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)));
106                 return;
107         }
108 }
109
110  int main(int argc, char **argv)
111 {
112         int opt;
113         poptContext pc;
114         const char *backend1 = NULL, *backend2 = NULL;
115         const char *location2;
116         const char *credentials1= NULL, *credentials2 = NULL;
117         char *outputfile = NULL;
118         FILE *fd = stdout;
119         struct registry_context *h1 = NULL, *h2;
120         int from_null = 0;
121         int i;
122         WERROR error, error2;
123         struct poptOption long_options[] = {
124                 POPT_AUTOHELP
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" },
129                 POPT_TABLEEND
130         };
131
132
133         if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
134                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
135         }
136
137
138         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
139
140         while((opt = poptGetNextOpt(pc)) != -1) {
141                 switch(opt)     {
142                 case 'c':
143                         if(!credentials1 && !from_null) credentials1 = poptGetOptArg(pc);
144                         else if(!credentials2) credentials2 = poptGetOptArg(pc);
145                         break;
146                 case 'b':
147                         if(!backend1 && !from_null) backend1 = poptGetOptArg(pc);
148                         else if(!backend2) backend2 = poptGetOptArg(pc);
149                         break;
150                 }
151         }
152         setup_logging(argv[0], True);
153
154         if(!from_null) {
155                 const char *location1;
156                 location1 = poptGetArg(pc);
157                 if(!location1) {
158                         poptPrintUsage(pc, stderr, 0);
159                         return 1;
160                 }
161
162                 if(!backend1) backend1 = "rpc";
163
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);
167                         return 1;
168                 }
169         } 
170
171         location2 = poptGetArg(pc);
172         if(!location2) {
173                 poptPrintUsage(pc, stderr, 0);
174                 return 2;
175         }
176
177         if(!backend2) backend2 = "rpc";
178
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);
182                 return 1;
183         }
184
185         poptFreeContext(pc);
186
187         if(outputfile) {
188                 fd = fopen(outputfile, "w+");
189                 if(!fd) {
190                         fprintf(stderr, "Unable to open '%s'\n", outputfile);
191                         return 1;
192                 }
193         }
194
195         fprintf(fd, "REGEDIT4\n\n");
196         fprintf(fd, "; Generated using regdiff\n");
197
198         error2 = error = WERR_OK; 
199
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); 
202         }
203
204         fclose(fd);
205
206         return 0;
207 }