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
,
76 return talloc_strdup(mem_ctx
, "");
81 convert_string_talloc(mem_ctx
,
82 CH_UTF16
, CH_UNIX
, data
.data
, data
.length
,
83 (void **)&ret
, NULL
, false);
86 case REG_DWORD_BIG_ENDIAN
:
87 SMB_ASSERT(data
.length
== sizeof(uint32_t));
88 ret
= talloc_asprintf(mem_ctx
, "%08x",
91 default: /* default means treat as binary */
93 ret
= dotreg_data_blob_hex_string(mem_ctx
, &data
);
100 static WERROR
reg_dotreg_diff_add_key(void *_data
, const char *key_name
)
102 struct dotreg_data
*data
= (struct dotreg_data
*)_data
;
104 fdprintf(data
->fd
, "\n[%s]\n", key_name
);
109 static WERROR
reg_dotreg_diff_del_key(void *_data
, const char *key_name
)
111 struct dotreg_data
*data
= (struct dotreg_data
*)_data
;
113 fdprintf(data
->fd
, "\n[-%s]\n", key_name
);
118 static WERROR
reg_dotreg_diff_set_value(void *_data
, const char *path
,
119 const char *value_name
,
120 uint32_t value_type
, DATA_BLOB value
)
122 struct dotreg_data
*data
= (struct dotreg_data
*)_data
;
123 char *data_string
= reg_val_dotreg_string(NULL
,
125 char *data_incl_type
;
127 W_ERROR_HAVE_NO_MEMORY(data_string
);
129 switch (value_type
) {
131 data_incl_type
= talloc_asprintf(data_string
, "\"%s\"",
135 data_incl_type
= talloc_asprintf(data_string
,
136 "dword:%s", data_string
);
139 data_incl_type
= talloc_asprintf(data_string
, "hex:%s",
143 data_incl_type
= talloc_asprintf(data_string
, "hex(%x):%s",
144 value_type
, data_string
);
148 if (value_name
[0] == '\0') {
149 fdprintf(data
->fd
, "@=%s\n", data_incl_type
);
151 fdprintf(data
->fd
, "\"%s\"=%s\n",
152 value_name
, data_incl_type
);
155 talloc_free(data_string
);
160 static WERROR
reg_dotreg_diff_del_value(void *_data
, const char *path
,
161 const char *value_name
)
163 struct dotreg_data
*data
= (struct dotreg_data
*)_data
;
165 fdprintf(data
->fd
, "\"%s\"=-\n", value_name
);
170 static WERROR
reg_dotreg_diff_done(void *_data
)
172 struct dotreg_data
*data
= (struct dotreg_data
*)_data
;
180 static WERROR
reg_dotreg_diff_del_all_values(void *callback_data
,
181 const char *key_name
)
183 return WERR_NOT_SUPPORTED
;
189 _PUBLIC_ WERROR
reg_dotreg_diff_save(TALLOC_CTX
*ctx
, const char *filename
,
190 struct reg_diff_callbacks
**callbacks
,
191 void **callback_data
)
193 struct dotreg_data
*data
;
195 data
= talloc_zero(ctx
, struct dotreg_data
);
196 *callback_data
= data
;
199 data
->fd
= open(filename
, O_CREAT
|O_WRONLY
, 0755);
201 DEBUG(0, ("Unable to open %s\n", filename
));
205 data
->fd
= STDOUT_FILENO
;
208 fdprintf(data
->fd
, "%s\n\n", HEADER_STRING
);
210 *callbacks
= talloc(ctx
, struct reg_diff_callbacks
);
212 (*callbacks
)->add_key
= reg_dotreg_diff_add_key
;
213 (*callbacks
)->del_key
= reg_dotreg_diff_del_key
;
214 (*callbacks
)->set_value
= reg_dotreg_diff_set_value
;
215 (*callbacks
)->del_value
= reg_dotreg_diff_del_value
;
216 (*callbacks
)->del_all_values
= reg_dotreg_diff_del_all_values
;
217 (*callbacks
)->done
= reg_dotreg_diff_done
;
225 _PUBLIC_ WERROR
reg_dotreg_diff_load(int fd
,
226 const struct reg_diff_callbacks
*callbacks
,
231 TALLOC_CTX
*mem_ctx
= talloc_init("reg_dotreg_diff_load");
236 char *type_str
= NULL
;
239 bool continue_next_line
= 0;
241 line
= afdgets(fd
, mem_ctx
, 0);
243 DEBUG(0, ("Can't read from file.\n"));
244 talloc_free(mem_ctx
);
246 return WERR_GENERAL_FAILURE
;
249 while ((line
= afdgets(fd
, mem_ctx
, 0))) {
250 /* Remove '\r' if it's a Windows text file */
251 if (line
[strlen(line
)-1] == '\r') {
252 line
[strlen(line
)-1] = '\0';
255 /* Ignore comments and empty lines */
256 if (strlen(line
) == 0 || line
[0] == ';') {
267 if (line
[0] == '[') {
268 if (line
[strlen(line
)-1] != ']') {
269 DEBUG(0, ("Missing ']' on line: %s\n", line
));
275 if (line
[1] == '-') {
276 curkey
= talloc_strndup(line
, line
+2, strlen(line
)-3);
277 W_ERROR_HAVE_NO_MEMORY(curkey
);
279 error
= callbacks
->del_key(callback_data
,
282 if (!W_ERROR_IS_OK(error
)) {
283 DEBUG(0,("Error deleting key %s\n",
285 talloc_free(mem_ctx
);
293 curkey
= talloc_strndup(mem_ctx
, line
+1, strlen(line
)-2);
294 W_ERROR_HAVE_NO_MEMORY(curkey
);
296 error
= callbacks
->add_key(callback_data
, curkey
);
297 if (!W_ERROR_IS_OK(error
)) {
298 DEBUG(0,("Error adding key %s\n", curkey
));
299 talloc_free(mem_ctx
);
307 /* Deleting/Changing value */
308 if (continue_next_line
) {
309 continue_next_line
= 0;
311 /* Continued data start with two whitespaces */
312 if (line
[0] != ' ' || line
[1] != ' ') {
313 DEBUG(0, ("Malformed line: %s\n", line
));
319 /* Continue again if line ends with a backslash */
320 if (line
[strlen(line
)-1] == '\\') {
321 line
[strlen(line
)-1] = '\0';
322 continue_next_line
= 1;
323 data_str
= talloc_strdup_append(data_str
, p
);
327 data_str
= talloc_strdup_append(data_str
, p
);
329 p
= strchr_m(line
, '=');
331 DEBUG(0, ("Malformed line: %s\n", line
));
339 if (curkey
== NULL
) {
340 DEBUG(0, ("Value change without key\n"));
345 /* Values should be double-quoted */
346 if (line
[0] != '"') {
347 DEBUG(0, ("Malformed line\n"));
352 /* Chop of the quotes and store as value */
353 value
= talloc_strndup(mem_ctx
, line
+1,strlen(line
)-2);
357 error
= callbacks
->del_value(callback_data
,
360 /* Ignore if key does not exist (WERR_BADFILE)
361 * Consistent with Windows behaviour */
362 if (!W_ERROR_IS_OK(error
) &&
363 !W_ERROR_EQUAL(error
, WERR_BADFILE
)) {
364 DEBUG(0, ("Error deleting value %s in key %s\n",
366 talloc_free(mem_ctx
);
375 /* Do not look for colons in strings */
378 data_str
= talloc_strndup(mem_ctx
, p
+1,strlen(p
)-2);
380 /* Split the value type from the data */
381 q
= strchr_m(p
, ':');
385 type_str
= talloc_strdup(mem_ctx
, p
);
386 data_str
= talloc_strdup(mem_ctx
, q
);
388 data_str
= talloc_strdup(mem_ctx
, p
);
392 /* Backslash before the CRLF means continue on next line */
393 if (data_str
[strlen(data_str
)-1] == '\\') {
394 data_str
[strlen(data_str
)-1] = '\0';
396 continue_next_line
= 1;
400 DEBUG(9, ("About to write %s with type %s, length %ld: %s\n", value
, type_str
, (long) strlen(data_str
), data_str
));
401 result
= reg_string_to_val(value
,
402 type_str
?type_str
:"REG_SZ", data_str
,
405 DEBUG(0, ("Error converting string to value for line:\n%s\n",
407 return WERR_GENERAL_FAILURE
;
410 error
= callbacks
->set_value(callback_data
, curkey
, value
,
412 if (!W_ERROR_IS_OK(error
)) {
413 DEBUG(0, ("Error setting value for %s in %s\n",
415 talloc_free(mem_ctx
);
419 /* Clean up buffers */
420 if (type_str
!= NULL
) {
421 talloc_free(type_str
);
424 talloc_free(data_str
);
431 talloc_free(mem_ctx
);