r15356: Remove unused 'flags' argument from socket_send() and friends.
[bbaumbach/samba-autobuild/.git] / source4 / lib / registry / patchfile.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Reading .REG files
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/registry/registry.h"
24 #include "system/filesys.h"
25
26 /**
27  * @file
28  * @brief Registry patch files
29  */
30
31 #define DEFAULT_IDENT_STRING "SAMBA4 REGISTRY"
32
33 static struct reg_diff_key *diff_find_add_key(struct reg_diff *diff, const char *path)
34 {
35         int i;
36         
37         for (i = 0; diff->numkeys; i++) {
38                 if (!strcasecmp(diff->keys[i].name, path))
39                         return &diff->keys[i];
40         }
41
42         diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
43         diff->keys[diff->numkeys].name = talloc_strdup(diff->keys, path);
44         diff->keys[diff->numkeys].changetype = REG_DIFF_CHANGE_KEY;
45         diff->keys[diff->numkeys].numvalues = 0;
46         diff->keys[diff->numkeys].values = NULL;
47
48         diff->numkeys++;
49         return NULL;
50 }
51
52 /*
53  * Generate difference between two keys
54  */
55 static WERROR reg_generate_diff_key(struct reg_diff *diff, struct registry_key *oldkey, struct registry_key *newkey)
56 {
57         int i;
58         struct registry_key *t1, *t2;
59         struct registry_value *v1, *v2;
60         WERROR error1, error2;
61         TALLOC_CTX *mem_ctx = talloc_init("writediff");
62
63         /* Subkeys that were deleted */
64         for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(mem_ctx, oldkey, i, &t1)); i++) {
65                 error2 = reg_key_get_subkey_by_name(mem_ctx, newkey, t1->name, &t2);
66
67                 if (W_ERROR_IS_OK(error2))
68                         continue;
69
70                 if (!W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
71                         DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
72                         return error2;
73                 }
74
75                 /* newkey didn't have such a subkey, add del diff */
76                 diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
77                 diff->keys[diff->numkeys].name = talloc_strdup(diff->keys, t1->path);
78                 diff->keys[diff->numkeys].changetype = REG_DIFF_DEL_KEY;
79                 diff->numkeys++;
80         }
81
82         if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
83                 DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
84                 talloc_free(mem_ctx);
85                 return error1;
86         }
87
88         /* Subkeys that were added */
89         for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(mem_ctx, newkey, i, &t1)); i++) {
90                 error2 = reg_key_get_subkey_by_name(mem_ctx, oldkey, t1->name, &t2);
91                         
92                 if (W_ERROR_IS_OK(error2))
93                         continue;
94                 
95                 if (!W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
96                         DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
97                         return error2;
98                 }
99
100                 /* oldkey didn't have such a subkey, add add diff */
101                 diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
102                 diff->keys[diff->numkeys].name = talloc_strdup(diff->keys, t1->path);
103                 diff->keys[diff->numkeys].changetype = REG_DIFF_CHANGE_KEY;
104                 diff->keys[diff->numkeys].numvalues = 0;
105                 diff->keys[diff->numkeys].values = NULL;
106                 diff->numkeys++;
107
108                 reg_generate_diff_key(diff, t1, t2);
109         }
110
111         if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
112                 DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
113                 talloc_free(mem_ctx);
114                 return error1;
115         }
116
117         /* Values that were changed */
118         for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, newkey, i, &v1)); i++) {
119                 struct reg_diff_key *thiskey = NULL;
120                 error2 = reg_key_get_value_by_name(mem_ctx, oldkey, v1->name, &v2);
121         
122                 if(!W_ERROR_IS_OK(error2) && 
123                    !W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
124                         DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
125                         return error2;
126                 }
127
128                 if (W_ERROR_IS_OK(error2) && data_blob_equal(&v1->data, &v2->data))
129                         continue;
130
131                 thiskey = diff_find_add_key(diff, oldkey->path);
132                 thiskey->values = talloc_realloc(diff, thiskey->values, struct reg_diff_value, thiskey->numvalues+2);
133                 thiskey->values[thiskey->numvalues].name = talloc_strdup(thiskey->values, v1->name);
134                 thiskey->values[thiskey->numvalues].type = v2->data_type;
135                 thiskey->values[thiskey->numvalues].changetype = REG_DIFF_SET_VAL;
136                 thiskey->values[thiskey->numvalues].data = data_blob_dup_talloc(thiskey->values, &v2->data);
137                 thiskey->numvalues++;
138         }
139
140         if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
141                 DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
142                 talloc_free(mem_ctx);
143                 return error1;
144         }
145
146         /* Values that were deleted */
147         for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, oldkey, i, &v1)); i++) {
148                 struct reg_diff_key *thiskey = NULL;
149                 error2 = reg_key_get_value_by_name(mem_ctx, newkey, v1->name, &v2);
150
151                 if (W_ERROR_IS_OK(error2))
152                         continue;
153
154                 if (!W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
155                         DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
156                         return error2;
157                 }
158
159                 thiskey = diff_find_add_key(diff, oldkey->path);
160                 thiskey->values = talloc_realloc(diff, thiskey->values, struct reg_diff_value, thiskey->numvalues+2);
161                 thiskey->values[thiskey->numvalues].name = talloc_strdup(thiskey->values, v1->name);
162                 thiskey->values[thiskey->numvalues].changetype = REG_DIFF_DEL_VAL;
163                 thiskey->numvalues++;
164         }
165
166         if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
167                 DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
168                 talloc_free(mem_ctx);
169                 return error1;
170         }
171
172         talloc_free(mem_ctx);
173         return WERR_OK;
174 }
175
176 /**
177  * Generate diff between two registry contexts 
178  */
179 _PUBLIC_ struct reg_diff *reg_generate_diff(TALLOC_CTX *mem_ctx, struct registry_context *ctx1, struct registry_context *ctx2)
180 {
181         struct reg_diff *diff = talloc_zero(mem_ctx, struct reg_diff);
182         int i;
183         WERROR error;
184
185         for(i = HKEY_CLASSES_ROOT; i <= HKEY_PERFORMANCE_NLSTEXT; i++) {
186                 struct registry_key *r1, *r2;
187                 error = reg_get_predefined_key(ctx1, i, &r1);
188                 if (!W_ERROR_IS_OK(error)) {
189                         DEBUG(0, ("Unable to open hive %s for backend 1\n", reg_get_predef_name(i)));
190                         continue;
191                 }
192                 
193                 error = reg_get_predefined_key(ctx2, i, &r2);
194                 if (!W_ERROR_IS_OK(error)) {
195                         DEBUG(0, ("Unable to open hive %s for backend 2\n", reg_get_predef_name(i)));
196                         continue;
197                 }
198
199                 reg_generate_diff_key(diff, r1, r2);
200         }
201
202         return diff;
203 }
204
205 /**
206  * Save registry diff
207  */
208 _PUBLIC_ WERROR reg_diff_save(const struct reg_diff *diff, const char *filename)
209 {
210         int xf, i, j;
211
212         if (filename) {
213                 xf = open(filename, O_CREAT, 0755);
214                 if (xf == -1) {
215                         DEBUG(0, ("Unable to open %s\n", filename));
216                         return WERR_BADFILE;
217                 }
218         } else 
219                 xf = STDIN_FILENO;
220
221         fdprintf(xf, "%s\n\n", diff->format?diff->format:DEFAULT_IDENT_STRING);
222
223         for (i = 0; i < diff->numkeys; i++) {
224                 if (diff->keys[i].changetype == REG_DIFF_DEL_KEY) {
225                         fdprintf(xf, "-%s\n\n",  diff->keys[i].name);
226                         continue;
227                 }
228
229                 fdprintf(xf, "[%s]\n", diff->keys[i].name);
230
231                 for (j = 0; j < diff->keys[i].numvalues; j++) {
232                         fdprintf(xf, "\"%s\"=", diff->keys[i].values[j].name);
233                         switch (diff->keys[i].values[j].changetype) {
234                                 case REG_DIFF_DEL_VAL:
235                                         fdprintf(xf, "-\n");
236                                         break;
237                                 case REG_DIFF_SET_VAL:
238                                         fdprintf(xf, "%s:%s\n", 
239                                                          str_regtype(diff->keys[i].values[j].type), 
240                                                          reg_val_data_string(NULL, 
241                                                                 diff->keys[i].values[j].type, 
242                                                                 &diff->keys[i].values[j].data));
243                                         break;
244                         }
245                 }
246
247                 fdprintf(xf, "\n");
248         }
249
250         close(xf);
251
252         return WERR_OK;
253 }
254
255 /**
256  * Load diff file
257  */
258 _PUBLIC_ struct reg_diff *reg_diff_load(TALLOC_CTX *ctx, const char *fn)
259 {
260         struct reg_diff *diff;
261         int fd;
262         char *line, *p, *q;
263         struct reg_diff_key *curkey = NULL;
264         struct reg_diff_value *curval;
265
266         fd = open(fn, O_RDONLY, 0);
267         if (fd == -1) {
268                 DEBUG(0, ("Error opening registry patch file `%s'\n", fn));
269                 return NULL;
270         }
271
272         diff = talloc_zero(ctx, struct reg_diff);
273         if (diff == NULL) {
274                 close(fd);
275                 return NULL;
276         }
277         
278         diff->format = afdgets(fd, diff, 0);
279         if (!diff->format) {
280                 talloc_free(diff);
281                 close(fd);
282                 return NULL;
283         }
284
285         while ((line = afdgets(fd, diff, 0))) {
286                 /* Ignore comments and empty lines */
287                 if (strlen(line) == 0 || line[0] == ';') {
288                         curkey = NULL;
289                         talloc_free(line);
290                         continue;
291                 }
292
293                 /* Start of key */
294                 if (line[0] == '[') {
295                         p = strchr_m(line, ']');
296                         if (p[strlen(p)-2] != ']') {
297                                 DEBUG(0, ("Malformed line\n"));
298                                 return NULL;
299                         }
300                         diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
301                         diff->keys[diff->numkeys].name = talloc_strndup(diff->keys, line+1, strlen(line)-2);
302                         diff->keys[diff->numkeys].changetype = REG_DIFF_CHANGE_KEY;
303                         diff->keys[diff->numkeys].numvalues = 0;
304                         diff->keys[diff->numkeys].values = NULL;
305                         curkey = &diff->keys[diff->numkeys];
306                         diff->numkeys++;
307                         talloc_free(line);
308                         continue;
309                 }
310
311                 /* Deleting key */
312                 if (line[0] == '-') {
313                         diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
314                         diff->keys[diff->numkeys].name = talloc_strdup(diff->keys, line+1);
315                         diff->keys[diff->numkeys].changetype = REG_DIFF_DEL_KEY;
316                         diff->numkeys++;
317                         talloc_free(line);
318                         continue;
319                 }
320
321                 /* Deleting/Changing value */
322                 p = strchr_m(line, '=');
323                 if (p == NULL) {
324                         DEBUG(0, ("Malformed line\n"));
325                         talloc_free(line);
326                         continue;
327                 }
328
329                 *p = '\0'; p++;
330
331                 if (curkey == NULL) {
332                         DEBUG(0, ("Value change without key\n"));
333                         talloc_free(line);
334                         continue;
335                 }
336
337                 curkey->values = talloc_realloc(diff->keys, curkey->values, struct reg_diff_value, curkey->numvalues+2);
338                 curval = &curkey->values[curkey->numvalues];
339                 curkey->numvalues++;
340                 curval->name = talloc_strdup(curkey->values, line);
341
342                 /* Delete value */
343                 if (strcmp(p, "-")) {
344                         curval->changetype = REG_DIFF_DEL_VAL;
345                         talloc_free(line);
346                         continue;
347                 }
348                 
349                 q = strchr_m(p, ':');
350                 if (q) {
351                         *q = '\0'; 
352                         q++;
353                 }
354
355                 curval->changetype = REG_DIFF_SET_VAL;
356                 reg_string_to_val(curkey->values, q?p:"REG_SZ", q?q:p, &curval->type, &curval->data);
357
358                 talloc_free(line);
359         }
360
361         close(fd);
362
363         return diff;
364 }
365
366 /**
367  * Apply diff to a registry context 
368  */
369 _PUBLIC_ BOOL reg_diff_apply (const struct reg_diff *diff, struct registry_context *ctx)
370 {
371         TALLOC_CTX *mem_ctx = talloc_init("apply_cmd_file");
372         struct registry_key *tmp = NULL;
373         WERROR error;
374         int i, j;
375
376         for (i = 0; i < diff->numkeys; i++) {
377                 if (diff->keys[i].changetype == REG_DIFF_DEL_KEY) {
378                         error = reg_key_del_abs(ctx, diff->keys[i].name);
379
380                         if(!W_ERROR_IS_OK(error)) {
381                                 DEBUG(0, ("Unable to delete key '%s'\n", diff->keys[i].name));
382                                 return False;
383                         }
384
385                         continue;
386                 }
387
388                 /* Add / change key */
389                 error = reg_open_key_abs(mem_ctx, ctx, diff->keys[i].name, &tmp);
390
391                 /* If we found it, apply the other bits, else create such a key */
392                 if (W_ERROR_EQUAL(error, WERR_DEST_NOT_FOUND)) {
393                         if(!W_ERROR_IS_OK(reg_key_add_abs(mem_ctx, ctx, diff->keys[i].name, 0, NULL, &tmp))) {
394                                 DEBUG(0, ("Error adding new key '%s'\n", diff->keys[i].name));
395                                 return False;
396                         }
397                 }
398
399                 for (j = 0; j < diff->keys[i].numvalues; j++) {
400                         if (diff->keys[i].values[j].changetype == REG_DIFF_DEL_VAL) {
401                                 error = reg_del_value(tmp, diff->keys[i].values[j].name);
402                                 if (!W_ERROR_IS_OK(error)) {
403                                         DEBUG(0, ("Error deleting value '%s'\n", diff->keys[i].values[j].name));
404                                         return False;
405                                 }
406                         
407                                 error = reg_val_set(tmp, diff->keys[i].values[j].name, 
408                                                          diff->keys[i].values[j].type,
409                                                          diff->keys[i].values[j].data);
410                                 if (!W_ERROR_IS_OK(error)) {
411                                         DEBUG(0, ("Error setting value '%s'\n", diff->keys[i].values[j].name));
412                                         return False;
413                                 }       
414                         }
415                 }
416         }
417
418         return True;
419 }