summaryrefslogtreecommitdiff
path: root/source4/lib/registry/tools/regdiff.c
blob: a9d189c03321c610b756920dbb574100e7da598b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* 
   Unix SMB/CIFS implementation.
   simple registry frontend
   
   Copyright (C) Jelmer Vernooij 2004

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "dynconfig.h"
#include "registry.h"
#include "lib/cmdline/popt_common.h"

static void writediff(struct registry_key *oldkey, struct registry_key *newkey, FILE *out)
{
	int i;
	struct registry_key *t1, *t2;
	struct registry_value *v1, *v2;
	WERROR error1, error2;
	TALLOC_CTX *mem_ctx = talloc_init("writediff");

	for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(mem_ctx, oldkey, i, &t1)); i++) {
		error2 = reg_key_get_subkey_by_name(mem_ctx, newkey, t1->name, &t2);
		if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
			fprintf(out, "-%s\n", t1->path+1);
		} else if(!W_ERROR_IS_OK(error2)) {
			DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
		}
	}

	talloc_free(mem_ctx);

	if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
		DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
		return;
	}

	mem_ctx = talloc_init("writediff");

	for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(mem_ctx, newkey, i, &t1)); i++) {
		error2 = reg_key_get_subkey_by_name(mem_ctx, oldkey, t1->name, &t2);
		if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
			fprintf(out, "\n[%s]\n", t1->path+1);
		} else if(!W_ERROR_IS_OK(error2)) {
			DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
		}
		writediff(t2, t1, out);
	}

	talloc_free(mem_ctx);

	if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
		DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
		return;
	}


	mem_ctx = talloc_init("writediff");

	for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, newkey, i, &v1)); i++) {
		error2 = reg_key_get_value_by_name(mem_ctx, oldkey, v1->name, &v2);
		if ((W_ERROR_IS_OK(error2) && (v2->data_len != v1->data_len || memcmp(v1->data_blk, v2->data_blk, v1->data_len))) 
			|| W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
			fprintf(out, "\"%s\"=%s:%s\n", v1->name, str_regtype(v1->data_type), reg_val_data_string(mem_ctx, v1));
		}

		if(!W_ERROR_IS_OK(error2) && !W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
			DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
		}
	}

	talloc_free(mem_ctx);

	if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
		DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
		return;
	}

	mem_ctx = talloc_init("writediff");

	for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, oldkey, i, &v1)); i++) {
		error2 = reg_key_get_value_by_name(mem_ctx, newkey, v1->name, &v2);
		if(W_ERROR_IS_OK(error2)) {
		} else if(W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
			fprintf(out, "\"%s\"=-\n", v1->name);
		} else {
			DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
		}
	}

	talloc_free(mem_ctx);

	if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
		DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
		return;
	}
}

 int main(int argc, char **argv)
{
	int opt;
	poptContext pc;
	char *outputfile = NULL;
	FILE *fd = stdout;
	struct registry_context *h1 = NULL, *h2 = NULL;
	int from_null = 0;
	int i;
	WERROR error, error2;
	struct poptOption long_options[] = {
		POPT_AUTOHELP
		{"output", 'o', POPT_ARG_STRING, &outputfile, 'o', "output file to use", NULL },
		{"null", 'n', POPT_ARG_NONE, &from_null, 'n', "Diff from NULL", NULL },
		{"remote", 'R', POPT_ARG_STRING, NULL, 0, "Connect to remote server" , NULL },
		{"local", 'L', POPT_ARG_NONE, NULL, 0, "Open local registry", NULL },
		POPT_COMMON_SAMBA
		POPT_COMMON_CREDENTIALS
		POPT_COMMON_VERSION
		POPT_TABLEEND
	};

	regdiff_init_subsystems;

	pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);

	while((opt = poptGetNextOpt(pc)) != -1) {
		error = WERR_OK;
		switch(opt)	{
		case 'L':
			if (!h1 && !from_null) error = reg_open_local(&h1);
			else if (!h2) error = reg_open_local(&h2);
			break;
		case 'R':
			if (!h1 && !from_null) 
				error = reg_open_remote(&h1, cmdline_credentials, 
							poptGetOptArg(pc), NULL);
			else if (!h2) error = reg_open_remote(&h2, cmdline_credentials, 
							      poptGetOptArg(pc), NULL);
			break;
		}

		if (!W_ERROR_IS_OK(error)) {
			fprintf(stderr, "Error: %s\n", win_errstr(error));
			return 1;
		}
	}

	poptFreeContext(pc);

	if(outputfile) {
		fd = fopen(outputfile, "w+");
		if(!fd) {
			fprintf(stderr, "Unable to open '%s'\n", outputfile);
			return 1;
		}
	}

	fprintf(fd, "REGEDIT4\n\n");
	fprintf(fd, "; Generated using regdiff, part of Samba\n");

	error2 = error = WERR_OK; 

	for(i = HKEY_CLASSES_ROOT; i <= HKEY_PERFORMANCE_NLSTEXT; i++) {
		struct registry_key *r1, *r2;
		error = reg_get_predefined_key(h1, i, &r1);
		if (!W_ERROR_IS_OK(error)) {
			DEBUG(0, ("Unable to open hive %s for backend 1\n", reg_get_predef_name(i)));
			continue;
		}
		
		error = reg_get_predefined_key(h2, i, &r2);
		if (!W_ERROR_IS_OK(error)) {
			DEBUG(0, ("Unable to open hive %s for backend 2\n", reg_get_predef_name(i)));
			continue;
		}

		writediff(r1, r2, fd); 
	}

	fclose(fd);

	return 0;
}