2 Unix SMB/CIFS implementation.
5 Copyright (C) Jelmer Vernooij 2004-2007
6 Copyright (C) Wilco Baan Hofman 2006-2008
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 3 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 UCS-2 */
26 #include "lib/registry/registry.h"
27 #include "system/filesys.h"
28 #include "param/param.h"
32 * @brief Registry patch files
35 #define HEADER_STRING "REGEDIT4"
39 struct smb_iconv_convenience *iconv_convenience;
42 static WERROR reg_dotreg_diff_add_key(void *_data, const char *key_name)
44 struct dotreg_data *data = (struct dotreg_data *)_data;
46 fdprintf(data->fd, "\n[%s]\n", key_name);
51 static WERROR reg_dotreg_diff_del_key(void *_data, const char *key_name)
53 struct dotreg_data *data = (struct dotreg_data *)_data;
55 fdprintf(data->fd, "\n[-%s]\n", key_name);
60 static WERROR reg_dotreg_diff_set_value(void *_data, const char *path,
61 const char *value_name,
62 uint32_t value_type, DATA_BLOB value)
64 struct dotreg_data *data = (struct dotreg_data *)_data;
66 fdprintf(data->fd, "\"%s\"=%s:%s\n",
67 value_name, str_regtype(value_type),
68 reg_val_data_string(NULL, data->iconv_convenience, value_type, value));
73 static WERROR reg_dotreg_diff_del_value(void *_data, const char *path,
74 const char *value_name)
76 struct dotreg_data *data = (struct dotreg_data *)_data;
78 fdprintf(data->fd, "\"%s\"=-\n", value_name);
83 static WERROR reg_dotreg_diff_done(void *_data)
85 struct dotreg_data *data = (struct dotreg_data *)_data;
93 static WERROR reg_dotreg_diff_del_all_values(void *callback_data,
96 return WERR_NOT_SUPPORTED;
102 _PUBLIC_ WERROR reg_dotreg_diff_save(TALLOC_CTX *ctx, const char *filename,
103 struct smb_iconv_convenience *iconv_convenience,
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 = iconv_convenience;
115 data->fd = open(filename, O_CREAT|O_WRONLY, 0755);
117 DEBUG(0, ("Unable to open %s\n", filename));
121 data->fd = STDOUT_FILENO;
124 fdprintf(data->fd, "%s\n\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);