2 Unix SMB/CIFS implementation.
5 Copyright (C) Jelmer Vernooij 2004-2007
6 Copyright (C) Wilco Baan Hofman 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* FIXME Newer .REG files, created by Windows XP and above use unicode UTF-16 */
26 #include "lib/registry/patchfile.h"
27 #include "lib/registry/registry.h"
28 #include "system/filesys.h"
32 * @brief Registry patch files
35 #define HEADER_STRING "REGEDIT4"
41 static WERROR reg_dotreg_diff_add_key(void *_data, const char *key_name)
43 struct dotreg_data *data = (struct dotreg_data *)_data;
45 fdprintf(data->fd, "\n[%s]\n", key_name);
50 static WERROR reg_dotreg_diff_del_key(void *_data, const char *key_name)
52 struct dotreg_data *data = (struct dotreg_data *)_data;
54 fdprintf(data->fd, "\n[-%s]\n", key_name);
59 static WERROR reg_dotreg_diff_set_value(void *_data, const char *path,
60 const char *value_name, uint32_t value_type, DATA_BLOB value)
62 struct dotreg_data *data = (struct dotreg_data *)_data;
64 fdprintf(data->fd, "\"%s\"=%s:%s\n",
65 value_name, str_regtype(value_type),
66 reg_val_data_string(NULL, value_type, value));
71 static WERROR reg_dotreg_diff_del_value(void *_data, const char *path, const char *value_name)
73 struct dotreg_data *data = (struct dotreg_data *)_data;
75 fdprintf(data->fd, "\"%s\"=-\n", value_name);
80 static WERROR reg_dotreg_diff_done(void *_data)
82 struct dotreg_data *data = _data;
90 static WERROR reg_dotreg_diff_del_all_values (void *callback_data, const char *key_name)
92 return WERR_NOT_SUPPORTED;
98 _PUBLIC_ WERROR reg_dotreg_diff_save(TALLOC_CTX *ctx, const char *filename,
99 struct reg_diff_callbacks **callbacks, void **callback_data)
101 struct dotreg_data *data;
103 data = talloc_zero(ctx, struct dotreg_data);
104 *callback_data = data;
107 data->fd = open(filename, O_CREAT, 0755);
108 if (data->fd == -1) {
109 DEBUG(0, ("Unable to open %s\n", filename));
113 data->fd = STDOUT_FILENO;
116 fdprintf(data->fd, "%s\n", HEADER_STRING);
118 *callbacks = talloc(ctx, struct reg_diff_callbacks);
120 (*callbacks)->add_key = reg_dotreg_diff_add_key;
121 (*callbacks)->del_key = reg_dotreg_diff_del_key;
122 (*callbacks)->set_value = reg_dotreg_diff_set_value;
123 (*callbacks)->del_value = reg_dotreg_diff_del_value;
124 (*callbacks)->del_all_values = reg_dotreg_diff_del_all_values;
125 (*callbacks)->done = reg_dotreg_diff_done;
133 _PUBLIC_ WERROR reg_dotreg_diff_load(int fd,
134 const struct reg_diff_callbacks *callbacks, void *callback_data)
138 TALLOC_CTX *mem_ctx = talloc_init("reg_dotreg_diff_load");
143 line = afdgets(fd, mem_ctx, 0);
145 DEBUG(0, ("Can't read from file.\n"));
146 talloc_free(mem_ctx);
148 return WERR_GENERAL_FAILURE;
151 while ((line = afdgets(fd, mem_ctx, 0))) {
152 /* Ignore comments and empty lines */
153 if (strlen(line) == 0 || line[0] == ';') {
164 if (line[0] == '[') {
165 p = strchr_m(line, ']');
166 if (p[strlen(p)-1] != ']') {
167 DEBUG(0, ("Missing ']'\n"));
168 return WERR_GENERAL_FAILURE;
171 if (line[1] == '-') {
172 curkey = talloc_strndup(line, line+2, strlen(line)-3);
174 error = callbacks->del_key(callback_data, curkey);
175 if (!W_ERROR_IS_OK(error)) {
176 DEBUG(0,("Error deleting key %s\n", curkey));
177 talloc_free(mem_ctx);
185 curkey = talloc_strndup(mem_ctx, line+1, strlen(line)-2);
187 error = callbacks->add_key(callback_data, curkey);
188 if (!W_ERROR_IS_OK(error)) {
189 DEBUG(0,("Error adding key %s\n", curkey));
190 talloc_free(mem_ctx);
198 /* Deleting/Changing value */
199 p = strchr_m(line, '=');
201 DEBUG(0, ("Malformed line\n"));
208 if (curkey == NULL) {
209 DEBUG(0, ("Value change without key\n"));
215 if (strcmp(p, "-") == 0) {
216 error = callbacks->del_value(callback_data, curkey, line);
217 if (!W_ERROR_IS_OK(error)) {
218 DEBUG(0, ("Error deleting value %s in key %s\n", line, curkey));
219 talloc_free(mem_ctx);
227 q = strchr_m(p, ':');
233 reg_string_to_val(line, q?p:"REG_SZ", q?q:p, &value_type, &value);
235 error = callbacks->set_value(callback_data, curkey, line, value_type, value);
236 if (!W_ERROR_IS_OK(error)) {
237 DEBUG(0, ("Error setting value for %s in %s\n", line, curkey));
238 talloc_free(mem_ctx);