2 Unix SMB/CIFS implementation.
5 Copyright (C) Jelmer Vernooij 2004-2007
6 Copyright (C) Wilco Baan Hofman 2006-2010
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.
24 * - Newer .REG files, created by Windows XP and above use unicode UCS-2
25 * - @="" constructions should write value with empty name.
29 #include "lib/registry/registry.h"
30 #include "system/filesys.h"
34 * @brief Registry patch files
37 #define HEADER_STRING "REGEDIT4"
44 * This is basically a copy of data_blob_hex_string_upper, but with comma's
45 * between the bytes in hex.
47 static char *dotreg_data_blob_hex_string(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob)
52 hex_string = talloc_array(mem_ctx, char, (blob->length*3)+1);
57 for (i = 0; i < blob->length; i++)
58 slprintf(&hex_string[i*3], 4, "%02X,", blob->data[i]);
60 /* Remove last comma and NULL-terminate the string */
61 hex_string[(blob->length*3)-1] = '\0';
66 * This is basically a copy of reg_val_data_string, except that this function
67 * has no 0x for dwords, everything else is regarded as binary, and binary
68 * strings are represented with bytes comma-separated.
70 static char *reg_val_dotreg_string(TALLOC_CTX *mem_ctx, uint32_t type,
73 size_t converted_size = 0;
77 return talloc_strdup(mem_ctx, "");
82 convert_string_talloc(mem_ctx,
83 CH_UTF16, CH_UNIX, data.data, data.length,
84 (void **)&ret, &converted_size);
87 case REG_DWORD_BIG_ENDIAN:
88 SMB_ASSERT(data.length == sizeof(uint32_t));
89 ret = talloc_asprintf(mem_ctx, "%08x",
92 default: /* default means treat as binary */
94 ret = dotreg_data_blob_hex_string(mem_ctx, &data);
101 static WERROR reg_dotreg_diff_add_key(void *_data, const char *key_name)
103 struct dotreg_data *data = (struct dotreg_data *)_data;
105 fdprintf(data->fd, "\n[%s]\n", key_name);
110 static WERROR reg_dotreg_diff_del_key(void *_data, const char *key_name)
112 struct dotreg_data *data = (struct dotreg_data *)_data;
114 fdprintf(data->fd, "\n[-%s]\n", key_name);
119 static WERROR reg_dotreg_diff_set_value(void *_data, const char *path,
120 const char *value_name,
121 uint32_t value_type, DATA_BLOB value)
123 struct dotreg_data *data = (struct dotreg_data *)_data;
124 char *data_string = reg_val_dotreg_string(NULL,
126 char *data_incl_type;
128 W_ERROR_HAVE_NO_MEMORY(data_string);
130 switch (value_type) {
132 data_incl_type = talloc_asprintf(data_string, "\"%s\"",
136 data_incl_type = talloc_asprintf(data_string,
137 "dword:%s", data_string);
140 data_incl_type = talloc_asprintf(data_string, "hex:%s",
144 data_incl_type = talloc_asprintf(data_string, "hex(%x):%s",
145 value_type, data_string);
149 if (value_name[0] == '\0') {
150 fdprintf(data->fd, "@=%s\n", data_incl_type);
152 fdprintf(data->fd, "\"%s\"=%s\n",
153 value_name, data_incl_type);
156 talloc_free(data_string);
161 static WERROR reg_dotreg_diff_del_value(void *_data, const char *path,
162 const char *value_name)
164 struct dotreg_data *data = (struct dotreg_data *)_data;
166 fdprintf(data->fd, "\"%s\"=-\n", value_name);
171 static WERROR reg_dotreg_diff_done(void *_data)
173 struct dotreg_data *data = (struct dotreg_data *)_data;
181 static WERROR reg_dotreg_diff_del_all_values(void *callback_data,
182 const char *key_name)
184 return WERR_NOT_SUPPORTED;
190 _PUBLIC_ WERROR reg_dotreg_diff_save(TALLOC_CTX *ctx, const char *filename,
191 struct reg_diff_callbacks **callbacks,
192 void **callback_data)
194 struct dotreg_data *data;
196 data = talloc_zero(ctx, struct dotreg_data);
197 *callback_data = data;
200 data->fd = open(filename, O_CREAT|O_WRONLY, 0755);
202 DEBUG(0, ("Unable to open %s\n", filename));
203 return WERR_FILE_NOT_FOUND;
206 data->fd = STDOUT_FILENO;
209 fdprintf(data->fd, "%s\n\n", HEADER_STRING);
211 *callbacks = talloc(ctx, struct reg_diff_callbacks);
213 (*callbacks)->add_key = reg_dotreg_diff_add_key;
214 (*callbacks)->del_key = reg_dotreg_diff_del_key;
215 (*callbacks)->set_value = reg_dotreg_diff_set_value;
216 (*callbacks)->del_value = reg_dotreg_diff_del_value;
217 (*callbacks)->del_all_values = reg_dotreg_diff_del_all_values;
218 (*callbacks)->done = reg_dotreg_diff_done;
226 _PUBLIC_ WERROR reg_dotreg_diff_load(int fd,
227 const struct reg_diff_callbacks *callbacks,
232 TALLOC_CTX *mem_ctx = talloc_init("reg_dotreg_diff_load");
237 char *type_str = NULL;
238 char *data_str = NULL;
240 bool continue_next_line = 0;
242 line = afdgets(fd, mem_ctx, 0);
244 DEBUG(0, ("Can't read from file.\n"));
245 talloc_free(mem_ctx);
247 return WERR_GEN_FAILURE;
250 while ((line = afdgets(fd, mem_ctx, 0))) {
251 /* Remove '\r' if it's a Windows text file */
252 if (strlen(line) && line[strlen(line)-1] == '\r') {
253 line[strlen(line)-1] = '\0';
256 /* Ignore comments and empty lines */
257 if (strlen(line) == 0 || line[0] == ';') {
268 if (line[0] == '[') {
269 if (line[strlen(line)-1] != ']') {
270 DEBUG(0, ("Missing ']' on line: %s\n", line));
276 if (line[1] == '-') {
277 curkey = talloc_strndup(line, line+2, strlen(line)-3);
278 W_ERROR_HAVE_NO_MEMORY(curkey);
280 error = callbacks->del_key(callback_data,
283 if (!W_ERROR_IS_OK(error)) {
284 DEBUG(0,("Error deleting key %s\n",
286 talloc_free(mem_ctx);
294 curkey = talloc_strndup(mem_ctx, line+1, strlen(line)-2);
295 W_ERROR_HAVE_NO_MEMORY(curkey);
297 error = callbacks->add_key(callback_data, curkey);
298 if (!W_ERROR_IS_OK(error)) {
299 DEBUG(0,("Error adding key %s\n", curkey));
300 talloc_free(mem_ctx);
308 /* Deleting/Changing value */
309 if (continue_next_line) {
310 continue_next_line = 0;
312 /* Continued data start with two whitespaces */
313 if (line[0] != ' ' || line[1] != ' ') {
314 DEBUG(0, ("Malformed line: %s\n", line));
320 /* Continue again if line ends with a backslash */
321 if (line[strlen(line)-1] == '\\') {
322 line[strlen(line)-1] = '\0';
323 continue_next_line = 1;
324 data_str = talloc_strdup_append(data_str, p);
328 data_str = talloc_strdup_append(data_str, p);
330 p = strchr_m(line, '=');
332 DEBUG(0, ("Malformed line: %s\n", line));
340 if (curkey == NULL) {
341 DEBUG(0, ("Value change without key\n"));
346 /* Values should be double-quoted */
347 if (line[0] != '"') {
348 DEBUG(0, ("Malformed line\n"));
353 /* Chop of the quotes and store as value */
354 value = talloc_strndup(mem_ctx, line+1,strlen(line)-2);
358 error = callbacks->del_value(callback_data,
361 /* Ignore if key does not exist (WERR_FILE_NOT_FOUND)
362 * Consistent with Windows behaviour */
363 if (!W_ERROR_IS_OK(error) &&
364 !W_ERROR_EQUAL(error, WERR_FILE_NOT_FOUND)) {
365 DEBUG(0, ("Error deleting value %s in key %s\n",
367 talloc_free(mem_ctx);
376 /* Do not look for colons in strings */
379 data_str = talloc_strndup(mem_ctx, p+1,strlen(p)-2);
381 /* Split the value type from the data */
382 q = strchr_m(p, ':');
386 type_str = talloc_strdup(mem_ctx, p);
387 data_str = talloc_strdup(mem_ctx, q);
389 data_str = talloc_strdup(mem_ctx, p);
393 /* Backslash before the CRLF means continue on next line */
394 if (data_str[strlen(data_str)-1] == '\\') {
395 data_str[strlen(data_str)-1] = '\0';
397 continue_next_line = 1;
401 DEBUG(9, ("About to write %s with type %s, length %ld: %s\n", value, type_str, (long) strlen(data_str), data_str));
402 result = reg_string_to_val(value,
403 type_str?type_str:"REG_SZ", data_str,
406 DEBUG(0, ("Error converting string to value for line:\n%s\n",
408 return WERR_GEN_FAILURE;
411 error = callbacks->set_value(callback_data, curkey, value,
413 if (!W_ERROR_IS_OK(error)) {
414 DEBUG(0, ("Error setting value for %s in %s\n",
416 talloc_free(mem_ctx);
420 /* Clean up buffers */
421 if (type_str != NULL) {
422 talloc_free(type_str);
425 talloc_free(data_str);
432 talloc_free(mem_ctx);