2 Unix SMB/CIFS implementation.
5 Copyright (C) Jelmer Vernooij 2004
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "lib/registry/registry.h"
23 #include "system/filesys.h"
27 * @brief Registry patch files
30 #define DEFAULT_IDENT_STRING "SAMBA4 REGISTRY"
32 static struct reg_diff_key *diff_find_add_key(struct reg_diff *diff, const char *path)
36 for (i = 0; diff->numkeys; i++) {
37 if (!strcasecmp(diff->keys[i].name, path))
38 return &diff->keys[i];
41 diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
42 diff->keys[diff->numkeys].name = talloc_strdup(diff->keys, path);
43 diff->keys[diff->numkeys].changetype = REG_DIFF_CHANGE_KEY;
44 diff->keys[diff->numkeys].numvalues = 0;
45 diff->keys[diff->numkeys].values = NULL;
52 * Generate difference between two keys
54 static WERROR reg_generate_diff_key(struct reg_diff *diff, struct registry_key *oldkey, struct registry_key *newkey)
57 struct registry_key *t1, *t2;
58 struct registry_value *v1, *v2;
59 WERROR error1, error2;
60 TALLOC_CTX *mem_ctx = talloc_init("writediff");
62 /* Subkeys that were deleted */
63 for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(mem_ctx, oldkey, i, &t1)); i++) {
64 error2 = reg_key_get_subkey_by_name(mem_ctx, newkey, t1->name, &t2);
66 if (W_ERROR_IS_OK(error2))
69 if (!W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
70 DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
74 /* newkey didn't have such a subkey, add del diff */
75 diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
76 diff->keys[diff->numkeys].name = talloc_strdup(diff->keys, t1->path);
77 diff->keys[diff->numkeys].changetype = REG_DIFF_DEL_KEY;
81 if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
82 DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
87 /* Subkeys that were added */
88 for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_subkey_by_index(mem_ctx, newkey, i, &t1)); i++) {
89 error2 = reg_key_get_subkey_by_name(mem_ctx, oldkey, t1->name, &t2);
91 if (W_ERROR_IS_OK(error2))
94 if (!W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
95 DEBUG(0, ("Error occured while getting subkey by name: %d\n", W_ERROR_V(error2)));
99 /* oldkey didn't have such a subkey, add add diff */
100 diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
101 diff->keys[diff->numkeys].name = talloc_strdup(diff->keys, t1->path);
102 diff->keys[diff->numkeys].changetype = REG_DIFF_CHANGE_KEY;
103 diff->keys[diff->numkeys].numvalues = 0;
104 diff->keys[diff->numkeys].values = NULL;
107 reg_generate_diff_key(diff, t1, t2);
110 if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
111 DEBUG(0, ("Error occured while getting subkey by index: %d\n", W_ERROR_V(error1)));
112 talloc_free(mem_ctx);
116 /* Values that were changed */
117 for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, newkey, i, &v1)); i++) {
118 struct reg_diff_key *thiskey = NULL;
119 error2 = reg_key_get_value_by_name(mem_ctx, oldkey, v1->name, &v2);
121 if(!W_ERROR_IS_OK(error2) &&
122 !W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
123 DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
127 if (W_ERROR_IS_OK(error2) && data_blob_cmp(&v1->data, &v2->data) == 0)
130 thiskey = diff_find_add_key(diff, oldkey->path);
131 thiskey->values = talloc_realloc(diff, thiskey->values, struct reg_diff_value, thiskey->numvalues+2);
132 thiskey->values[thiskey->numvalues].name = talloc_strdup(thiskey->values, v1->name);
133 thiskey->values[thiskey->numvalues].type = v2->data_type;
134 thiskey->values[thiskey->numvalues].changetype = REG_DIFF_SET_VAL;
135 thiskey->values[thiskey->numvalues].data = data_blob_dup_talloc(thiskey->values, &v2->data);
136 thiskey->numvalues++;
139 if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
140 DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
141 talloc_free(mem_ctx);
145 /* Values that were deleted */
146 for(i = 0; W_ERROR_IS_OK(error1 = reg_key_get_value_by_index(mem_ctx, oldkey, i, &v1)); i++) {
147 struct reg_diff_key *thiskey = NULL;
148 error2 = reg_key_get_value_by_name(mem_ctx, newkey, v1->name, &v2);
150 if (W_ERROR_IS_OK(error2))
153 if (!W_ERROR_EQUAL(error2, WERR_DEST_NOT_FOUND)) {
154 DEBUG(0, ("Error occured while getting value by name: %d\n", W_ERROR_V(error2)));
158 thiskey = diff_find_add_key(diff, oldkey->path);
159 thiskey->values = talloc_realloc(diff, thiskey->values, struct reg_diff_value, thiskey->numvalues+2);
160 thiskey->values[thiskey->numvalues].name = talloc_strdup(thiskey->values, v1->name);
161 thiskey->values[thiskey->numvalues].changetype = REG_DIFF_DEL_VAL;
162 thiskey->numvalues++;
165 if(!W_ERROR_EQUAL(error1, WERR_NO_MORE_ITEMS)) {
166 DEBUG(0, ("Error occured while getting value by index: %d\n", W_ERROR_V(error1)));
167 talloc_free(mem_ctx);
171 talloc_free(mem_ctx);
176 * Generate diff between two registry contexts
178 _PUBLIC_ struct reg_diff *reg_generate_diff(TALLOC_CTX *mem_ctx, struct registry_context *ctx1, struct registry_context *ctx2)
180 struct reg_diff *diff = talloc_zero(mem_ctx, struct reg_diff);
184 for(i = HKEY_CLASSES_ROOT; i <= HKEY_PERFORMANCE_NLSTEXT; i++) {
185 struct registry_key *r1, *r2;
186 error = reg_get_predefined_key(ctx1, i, &r1);
187 if (!W_ERROR_IS_OK(error)) {
188 DEBUG(0, ("Unable to open hive %s for backend 1\n", reg_get_predef_name(i)));
192 error = reg_get_predefined_key(ctx2, i, &r2);
193 if (!W_ERROR_IS_OK(error)) {
194 DEBUG(0, ("Unable to open hive %s for backend 2\n", reg_get_predef_name(i)));
198 reg_generate_diff_key(diff, r1, r2);
207 _PUBLIC_ WERROR reg_diff_save(const struct reg_diff *diff, const char *filename)
212 xf = open(filename, O_CREAT, 0755);
214 DEBUG(0, ("Unable to open %s\n", filename));
220 fdprintf(xf, "%s\n\n", diff->format?diff->format:DEFAULT_IDENT_STRING);
222 for (i = 0; i < diff->numkeys; i++) {
223 if (diff->keys[i].changetype == REG_DIFF_DEL_KEY) {
224 fdprintf(xf, "-%s\n\n", diff->keys[i].name);
228 fdprintf(xf, "[%s]\n", diff->keys[i].name);
230 for (j = 0; j < diff->keys[i].numvalues; j++) {
231 fdprintf(xf, "\"%s\"=", diff->keys[i].values[j].name);
232 switch (diff->keys[i].values[j].changetype) {
233 case REG_DIFF_DEL_VAL:
236 case REG_DIFF_SET_VAL:
237 fdprintf(xf, "%s:%s\n",
238 str_regtype(diff->keys[i].values[j].type),
239 reg_val_data_string(NULL,
240 diff->keys[i].values[j].type,
241 &diff->keys[i].values[j].data));
257 _PUBLIC_ struct reg_diff *reg_diff_load(TALLOC_CTX *ctx, const char *fn)
259 struct reg_diff *diff;
262 struct reg_diff_key *curkey = NULL;
263 struct reg_diff_value *curval;
265 fd = open(fn, O_RDONLY, 0);
267 DEBUG(0, ("Error opening registry patch file `%s'\n", fn));
271 diff = talloc_zero(ctx, struct reg_diff);
277 diff->format = afdgets(fd, diff, 0);
284 while ((line = afdgets(fd, diff, 0))) {
285 /* Ignore comments and empty lines */
286 if (strlen(line) == 0 || line[0] == ';') {
293 if (line[0] == '[') {
294 p = strchr_m(line, ']');
295 if (p[strlen(p)-2] != ']') {
296 DEBUG(0, ("Malformed line\n"));
299 diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
300 diff->keys[diff->numkeys].name = talloc_strndup(diff->keys, line+1, strlen(line)-2);
301 diff->keys[diff->numkeys].changetype = REG_DIFF_CHANGE_KEY;
302 diff->keys[diff->numkeys].numvalues = 0;
303 diff->keys[diff->numkeys].values = NULL;
304 curkey = &diff->keys[diff->numkeys];
311 if (line[0] == '-') {
312 diff->keys = talloc_realloc(diff, diff->keys, struct reg_diff_key, diff->numkeys+2);
313 diff->keys[diff->numkeys].name = talloc_strdup(diff->keys, line+1);
314 diff->keys[diff->numkeys].changetype = REG_DIFF_DEL_KEY;
320 /* Deleting/Changing value */
321 p = strchr_m(line, '=');
323 DEBUG(0, ("Malformed line\n"));
330 if (curkey == NULL) {
331 DEBUG(0, ("Value change without key\n"));
336 curkey->values = talloc_realloc(diff->keys, curkey->values, struct reg_diff_value, curkey->numvalues+2);
337 curval = &curkey->values[curkey->numvalues];
339 curval->name = talloc_strdup(curkey->values, line);
342 if (strcmp(p, "-")) {
343 curval->changetype = REG_DIFF_DEL_VAL;
348 q = strchr_m(p, ':');
354 curval->changetype = REG_DIFF_SET_VAL;
355 reg_string_to_val(curkey->values, q?p:"REG_SZ", q?q:p, &curval->type, &curval->data);
366 * Apply diff to a registry context
368 _PUBLIC_ BOOL reg_diff_apply (const struct reg_diff *diff, struct registry_context *ctx)
370 TALLOC_CTX *mem_ctx = talloc_init("apply_cmd_file");
371 struct registry_key *tmp = NULL;
375 for (i = 0; i < diff->numkeys; i++) {
376 if (diff->keys[i].changetype == REG_DIFF_DEL_KEY) {
377 error = reg_key_del_abs(ctx, diff->keys[i].name);
379 if(!W_ERROR_IS_OK(error)) {
380 DEBUG(0, ("Unable to delete key '%s'\n", diff->keys[i].name));
387 /* Add / change key */
388 error = reg_open_key_abs(mem_ctx, ctx, diff->keys[i].name, &tmp);
390 /* If we found it, apply the other bits, else create such a key */
391 if (W_ERROR_EQUAL(error, WERR_DEST_NOT_FOUND)) {
392 if(!W_ERROR_IS_OK(reg_key_add_abs(mem_ctx, ctx, diff->keys[i].name, 0, NULL, &tmp))) {
393 DEBUG(0, ("Error adding new key '%s'\n", diff->keys[i].name));
398 for (j = 0; j < diff->keys[i].numvalues; j++) {
399 if (diff->keys[i].values[j].changetype == REG_DIFF_DEL_VAL) {
400 error = reg_del_value(tmp, diff->keys[i].values[j].name);
401 if (!W_ERROR_IS_OK(error)) {
402 DEBUG(0, ("Error deleting value '%s'\n", diff->keys[i].values[j].name));
406 error = reg_val_set(tmp, diff->keys[i].values[j].name,
407 diff->keys[i].values[j].type,
408 diff->keys[i].values[j].data);
409 if (!W_ERROR_IS_OK(error)) {
410 DEBUG(0, ("Error setting value '%s'\n", diff->keys[i].values[j].name));