summaryrefslogtreecommitdiffstats
path: root/source4/lib/registry/patchfile.c
blob: cdf35c7b70047f27bd9caa4c30b72fb4d283ee8a (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* 
   Unix SMB/CIFS implementation.
   Reading .REG files
   
   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 "lib/registry/registry.h"
#include "lib/cmdline/popt_common.h"
#include "system/filesys.h"

#define DEFAULT_IDENT_STRING "SAMBA4 REGISTRY"

static struct reg_diff_key *diff_find_add_key(struct reg_diff *diff, const char *path)
{
	int i;
	
	for (i = 0; diff->numkeys; i++) {
		if (!strcasecmp(diff->keys[i].name, path))
			return &diff->keys[i];
	}

	diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
	diff->keys[diff->numkeys].name = talloc_strdup(diff->keys, path);
	diff->keys[diff->numkeys].changetype = REG_DIFF_CHANGE_KEY;
	diff->keys[diff->numkeys].numvalues = 0;
	diff->keys[diff->numkeys].values = NULL;

	diff->numkeys++;
	return NULL;
}

/*
 * Generate difference between two keys
 */
static WERROR reg_generate_diff_key(struct reg_diff *diff, struct registry_key *oldkey, struct registry_key *newkey)
{
	int i;
	struct registry_key *t1, *t2;
	struct registry_value *v1, *v2;
	WERROR error1, error2;
	TALLOC_CTX *mem_ctx = talloc_init("writediff");

	/* Subkeys that were deleted */
	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_IS_OK(error2))
			continue;

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

		/* newkey didn't have such a subkey, add del diff */
		diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
		diff->keys[diff->numkeys].name = talloc_strdup(diff->keys, t1->path);
		diff->keys[diff->numkeys].changetype = REG_DIFF_DEL_KEY;
		diff->numkeys++;
	}

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

	/* Subkeys that were added */
	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_IS_OK(error2))
			continue;
		
		if (!W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
			DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
			return error2;
		}

		/* oldkey didn't have such a subkey, add add diff */
		diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
		diff->keys[diff->numkeys].name = talloc_strdup(diff->keys, t1->path);
		diff->keys[diff->numkeys].changetype = REG_DIFF_CHANGE_KEY;
		diff->keys[diff->numkeys].numvalues = 0;
		diff->keys[diff->numkeys].values = NULL;
		diff->numkeys++;

		reg_generate_diff_key(diff, t1, t2);
	}

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

	/* Values that were changed */
	for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, newkey, i, &v1)); i++) {
		struct reg_diff_key *thiskey = NULL;
		error2 = reg_key_get_value_by_name(mem_ctx, oldkey, v1->name, &v2);
	
		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)));
			return error2;
		}

		if (W_ERROR_IS_OK(error2) && data_blob_equal(&v1->data, &v2->data))
			continue;

		thiskey = diff_find_add_key(diff, oldkey->path);
		thiskey->values = talloc_realloc(diff, thiskey->values, struct reg_diff_value, thiskey->numvalues+2);
		thiskey->values[thiskey->numvalues].name = talloc_strdup(thiskey->values, v1->name);
		thiskey->values[thiskey->numvalues].type = v2->data_type;
		thiskey->values[thiskey->numvalues].changetype = REG_DIFF_SET_VAL;
		thiskey->values[thiskey->numvalues].data = data_blob_dup_talloc(thiskey->values, &v2->data);
		thiskey->numvalues++;
	}

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

	/* Values that were deleted */
	for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, oldkey, i, &v1)); i++) {
		struct reg_diff_key *thiskey = NULL;
		error2 = reg_key_get_value_by_name(mem_ctx, newkey, v1->name, &v2);

		if (W_ERROR_IS_OK(error2))
			continue;

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

		thiskey = diff_find_add_key(diff, oldkey->path);
		thiskey->values = talloc_realloc(diff, thiskey->values, struct reg_diff_value, thiskey->numvalues+2);
		thiskey->values[thiskey->numvalues].name = talloc_strdup(thiskey->values, v1->name);
		thiskey->values[thiskey->numvalues].changetype = REG_DIFF_DEL_VAL;
		thiskey->numvalues++;
	}

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

	talloc_free(mem_ctx);
	return WERR_OK;
}

/* 
 * Generate diff between two registry contexts 
 */
struct reg_diff *reg_generate_diff(TALLOC_CTX *mem_ctx, struct registry_context *ctx1, struct registry_context *ctx2)
{
	struct reg_diff *diff = talloc_zero(mem_ctx, struct reg_diff);
	int i;
	WERROR error;

	for(i = HKEY_CLASSES_ROOT; i <= HKEY_PERFORMANCE_NLSTEXT; i++) {
		struct registry_key *r1, *r2;
		error = reg_get_predefined_key(ctx1, 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(ctx2, 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;
		}

		reg_generate_diff_key(diff, r1, r2);
	}

	return diff;
}

/*
 * Save registry diff
 */
WERROR reg_diff_save(const struct reg_diff *diff, const char *filename)
{
	int xf, i, j;

	if (filename) {
		xf = open(filename, O_CREAT, 0755);
		if (xf == -1) {
			DEBUG(0, ("Unable to open %s\n", filename));
			return WERR_BADFILE;
		}
	} else 
		xf = STDIN_FILENO;

	fdprintf(xf, "%s\n\n", diff->format?diff->format:DEFAULT_IDENT_STRING);

	for (i = 0; i < diff->numkeys; i++) {
		if (diff->keys[i].changetype == REG_DIFF_DEL_KEY) {
			fdprintf(xf, "-%s\n\n",  diff->keys[i].name);
			continue;
		}

		fdprintf(xf, "[%s]\n", diff->keys[i].name);

		for (j = 0; j < diff->keys[i].numvalues; j++) {
			fdprintf(xf, "\"%s\"=", diff->keys[i].values[j].name);
			switch (diff->keys[i].values[j].changetype) {
				case REG_DIFF_DEL_VAL:
					fdprintf(xf, "-\n");
					break;
				case REG_DIFF_SET_VAL:
					fdprintf(xf, "%s:%s\n", 
							 str_regtype(diff->keys[i].values[j].type), 
							 reg_val_data_string(NULL, 
								diff->keys[i].values[j].type, 
								&diff->keys[i].values[j].data));
					break;
			}
		}

		fdprintf(xf, "\n");
	}

	close(xf);

	return WERR_OK;
}

/*
 * Load diff file
 */
struct reg_diff *reg_diff_load(TALLOC_CTX *ctx, const char *fn)
{
	struct reg_diff *diff;
	int fd;
	char *line, *p, *q;
	struct reg_diff_key *curkey;
	struct reg_diff_value *curval;

	fd = open(fn, O_RDONLY, 0);
	if (fd == -1) {
		DEBUG(0, ("Error opening registry patch file `%s'\n", fn));
		return NULL;
	}

	diff = talloc_zero(ctx, struct reg_diff);
	if (diff == NULL) {
		close(fd);
		return NULL;
	}
	
	diff->format = afdgets(fd, diff, 0);
	if (!diff->format) {
		talloc_free(diff);
		close(fd);
		return NULL;
	}

	while ((line = afdgets(fd, diff, 0))) {
		/* Ignore comments and empty lines */
		if (strlen(line) == 0 || line[0] == ';') {
			curkey = NULL;
			talloc_free(line);
			continue;
		}

		/* Start of key */
		if (line[0] == '[') {
			p = strchr_m(line, ']');
			if (p[strlen(p)-2] != ']') {
				DEBUG(0, ("Malformed line\n"));
				return NULL;
			}
			diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
			diff->keys[diff->numkeys].name = talloc_strndup(diff->keys, line+1, strlen(line)-2);
			diff->keys[diff->numkeys].changetype = REG_DIFF_CHANGE_KEY;
			diff->keys[diff->numkeys].numvalues = 0;
			diff->keys[diff->numkeys].values = NULL;
			curkey = &diff->keys[diff->numkeys];
			diff->numkeys++;
			talloc_free(line);
			continue;
		}

		/* Deleting key */
		if (line[0] == '-') {
			diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
			diff->keys[diff->numkeys].name = talloc_strdup(diff->keys, line+1);
			diff->keys[diff->numkeys].changetype = REG_DIFF_DEL_KEY;
			diff->numkeys++;
			talloc_free(line);
			continue;
		}

		/* Deleting/Changing value */
		p = strchr_m(line, '=');
		if (p == NULL) {
			DEBUG(0, ("Malformed line\n"));
			talloc_free(line);
			continue;
		}

		*p = '\0'; p++;

		if (curkey == NULL) {
			DEBUG(0, ("Value change without key\n"));
			talloc_free(line);
			continue;
		}

		curkey->values = talloc_realloc(diff->keys, curkey->values, struct reg_diff_value, curkey->numvalues+2);
		curval = &curkey->values[curkey->numvalues];
		curkey->numvalues++;
		curval->name = talloc_strdup(curkey->values, line);

		/* Delete value */
		if (strcmp(p, "-")) {
			curval->changetype = REG_DIFF_DEL_VAL;
			talloc_free(line);
			continue;
		}
		
		q = strchr_m(p, ':');
		if (q) {
			*q = '\0'; 
			q++;
		}

		curval->changetype = REG_DIFF_SET_VAL;
		reg_string_to_val(curkey->values, q?p:"REG_SZ", q?q:p, &curval->type, &curval->data);

		talloc_free(line);
	}

	close(fd);

	return diff;
}

/* 
 * Apply diff to a registry context 
 */
BOOL reg_diff_apply (const struct reg_diff *diff, struct registry_context *ctx)
{
	TALLOC_CTX *mem_ctx = talloc_init("apply_cmd_file");
	struct registry_key *tmp = NULL;
	WERROR error;
	int i, j;

	for (i = 0; i < diff->numkeys; i++) {
		if (diff->keys[i].changetype == REG_DIFF_DEL_KEY) {
			error = reg_key_del_abs(ctx, diff->keys[i].name);

			if(!W_ERROR_IS_OK(error)) {
				DEBUG(0, ("Unable to delete key '%s'\n", diff->keys[i].name));
				return False;
		  	}

			continue;
		}

		/* Add / change key */
		error = reg_open_key_abs(mem_ctx, ctx, diff->keys[i].name, &tmp);

		/* If we found it, apply the other bits, else create such a key */
		if (W_ERROR_EQUAL(error, WERR_DEST_NOT_FOUND)) {
			if(!W_ERROR_IS_OK(reg_key_add_abs(mem_ctx, ctx, diff->keys[i].name, 0, NULL, &tmp))) {
				DEBUG(0, ("Error adding new key '%s'\n", diff->keys[i].name));
				return False;
			}
		}

		for (j = 0; j < diff->keys[i].numvalues; j++) {
			if (diff->keys[i].values[j].changetype == REG_DIFF_DEL_VAL) {
				error = reg_del_value(tmp, diff->keys[i].values[j].name);
				if (!W_ERROR_IS_OK(error)) {
					DEBUG(0, ("Error deleting value '%s'\n", diff->keys[i].values[j].name));
					return False;
				}
			
				error = reg_val_set(tmp, diff->keys[i].values[j].name, 
							 diff->keys[i].values[j].type,
							 diff->keys[i].values[j].data);
				if (!W_ERROR_IS_OK(error)) {
					DEBUG(0, ("Error setting value '%s'\n", diff->keys[i].values[j].name));
					return False;
				}	
			}
		}
	}

	return True;
}