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"
29 #include "param/param.h"
33 * @brief Registry patch files
36 #define HEADER_STRING "REGEDIT4"
40 struct smb_iconv_convenience *iconv_convenience;
43 static WERROR reg_dotreg_diff_add_key(void *_data, const char *key_name)
45 struct dotreg_data *data = (struct dotreg_data *)_data;
47 fdprintf(data->fd, "\n[%s]\n", key_name);
52 static WERROR reg_dotreg_diff_del_key(void *_data, const char *key_name)
54 struct dotreg_data *data = (struct dotreg_data *)_data;
56 fdprintf(data->fd, "\n[-%s]\n", key_name);
61 static WERROR reg_dotreg_diff_set_value(void *_data, const char *path,
62 const char *value_name,
63 uint32_t value_type, DATA_BLOB value)
65 struct dotreg_data *data = (struct dotreg_data *)_data;
67 fdprintf(data->fd, "\"%s\"=%s:%s\n",
68 value_name, str_regtype(value_type),
69 reg_val_data_string(NULL, data->iconv_convenience, value_type, value));
74 static WERROR reg_dotreg_diff_del_value(void *_data, const char *path,
75 const char *value_name)
77 struct dotreg_data *data = (struct dotreg_data *)_data;
79 fdprintf(data->fd, "\"%s\"=-\n", value_name);
84 static WERROR reg_dotreg_diff_done(void *_data)
86 struct dotreg_data *data = (struct dotreg_data *)_data;
94 static WERROR reg_dotreg_diff_del_all_values(void *callback_data,
97 return WERR_NOT_SUPPORTED;
103 _PUBLIC_ WERROR reg_dotreg_diff_save(TALLOC_CTX *ctx, const char *filename,
104 struct reg_diff_callbacks **callbacks,
105 void **callback_data)
107 struct dotreg_data *data;
109 data = talloc_zero(ctx, struct dotreg_data);
110 *callback_data = data;
112 data->iconv_convenience = lp_iconv_convenience(global_loadparm);
115 data->fd = open(filename, O_CREAT, 0755);
116 if (data->fd == -1) {
117 DEBUG(0, ("Unable to open %s\n", filename));
121 data->fd = STDOUT_FILENO;
124 fdprintf(data->fd, "%s\n", HEADER_STRING);
126 *callbacks = talloc(ctx, struct reg_diff_callbacks);
128 (*callbacks)->add_key = reg_dotreg_diff_add_key;
129 (*callbacks)->del_key = reg_dotreg_diff_del_key;
130 (*callbacks)->set_value = reg_dotreg_diff_set_value;
131 (*callbacks)->del_value = reg_dotreg_diff_del_value;
132 (*callbacks)->del_all_values = reg_dotreg_diff_del_all_values;
133 (*callbacks)->done = reg_dotreg_diff_done;
141 _PUBLIC_ WERROR reg_dotreg_diff_load(int fd,
142 struct smb_iconv_convenience *iconv_convenience,
143 const struct reg_diff_callbacks *callbacks,
148 TALLOC_CTX *mem_ctx = talloc_init("reg_dotreg_diff_load");
153 line = afdgets(fd, mem_ctx, 0);
155 DEBUG(0, ("Can't read from file.\n"));
156 talloc_free(mem_ctx);
158 return WERR_GENERAL_FAILURE;
161 while ((line = afdgets(fd, mem_ctx, 0))) {
162 /* Ignore comments and empty lines */
163 if (strlen(line) == 0 || line[0] == ';') {
174 if (line[0] == '[') {
175 p = strchr_m(line, ']');
176 if (p[strlen(p)-1] != ']') {
177 DEBUG(0, ("Missing ']'\n"));
178 return WERR_GENERAL_FAILURE;
181 if (line[1] == '-') {
182 curkey = talloc_strndup(line, line+2, strlen(line)-3);
184 error = callbacks->del_key(callback_data,
186 if (!W_ERROR_IS_OK(error)) {
187 DEBUG(0,("Error deleting key %s\n",
189 talloc_free(mem_ctx);
197 curkey = talloc_strndup(mem_ctx, line+1, strlen(line)-2);
199 error = callbacks->add_key(callback_data, curkey);
200 if (!W_ERROR_IS_OK(error)) {
201 DEBUG(0,("Error adding key %s\n", curkey));
202 talloc_free(mem_ctx);
210 /* Deleting/Changing value */
211 p = strchr_m(line, '=');
213 DEBUG(0, ("Malformed line\n"));
220 if (curkey == NULL) {
221 DEBUG(0, ("Value change without key\n"));
227 if (strcmp(p, "-") == 0) {
228 error = callbacks->del_value(callback_data,
230 if (!W_ERROR_IS_OK(error)) {
231 DEBUG(0, ("Error deleting value %s in key %s\n",
233 talloc_free(mem_ctx);
241 q = strchr_m(p, ':');
247 reg_string_to_val(line, iconv_convenience,
249 &value_type, &value);
251 error = callbacks->set_value(callback_data, curkey, line,
253 if (!W_ERROR_IS_OK(error)) {
254 DEBUG(0, ("Error setting value for %s in %s\n",
256 talloc_free(mem_ctx);