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/util/util_file.h"
30 #include "lib/registry/registry.h"
31 #include "system/filesys.h"
35 * @brief Registry patch files
38 #define HEADER_STRING "REGEDIT4"
45 * This is basically a copy of data_blob_hex_string_upper, but with comma's
46 * between the bytes in hex.
48 static char *dotreg_data_blob_hex_string(TALLOC_CTX
*mem_ctx
, const DATA_BLOB
*blob
)
53 hex_string
= talloc_array(mem_ctx
, char, (blob
->length
*3)+1);
58 for (i
= 0; i
< blob
->length
; i
++)
59 slprintf(&hex_string
[i
*3], 4, "%02X,", blob
->data
[i
]);
61 /* Remove last comma and NULL-terminate the string */
62 hex_string
[(blob
->length
*3)-1] = '\0';
67 * This is basically a copy of reg_val_data_string, except that this function
68 * has no 0x for dwords, everything else is regarded as binary, and binary
69 * strings are represented with bytes comma-separated.
71 static char *reg_val_dotreg_string(TALLOC_CTX
*mem_ctx
, uint32_t type
,
74 size_t converted_size
= 0;
78 return talloc_strdup(mem_ctx
, "");
83 convert_string_talloc(mem_ctx
,
84 CH_UTF16
, CH_UNIX
, data
.data
, data
.length
,
85 (void **)&ret
, &converted_size
);
88 case REG_DWORD_BIG_ENDIAN
:
89 SMB_ASSERT(data
.length
== sizeof(uint32_t));
90 ret
= talloc_asprintf(mem_ctx
, "%08x",
93 default: /* default means treat as binary */
95 ret
= dotreg_data_blob_hex_string(mem_ctx
, &data
);
102 static WERROR
reg_dotreg_diff_add_key(void *_data
, const char *key_name
)
104 struct dotreg_data
*data
= (struct dotreg_data
*)_data
;
106 fdprintf(data
->fd
, "\n[%s]\n", key_name
);
111 static WERROR
reg_dotreg_diff_del_key(void *_data
, const char *key_name
)
113 struct dotreg_data
*data
= (struct dotreg_data
*)_data
;
115 fdprintf(data
->fd
, "\n[-%s]\n", key_name
);
120 static WERROR
reg_dotreg_diff_set_value(void *_data
, const char *path
,
121 const char *value_name
,
122 uint32_t value_type
, DATA_BLOB value
)
124 struct dotreg_data
*data
= (struct dotreg_data
*)_data
;
125 char *data_string
= reg_val_dotreg_string(NULL
,
127 char *data_incl_type
;
129 W_ERROR_HAVE_NO_MEMORY(data_string
);
131 switch (value_type
) {
133 data_incl_type
= talloc_asprintf(data_string
, "\"%s\"",
137 data_incl_type
= talloc_asprintf(data_string
,
138 "dword:%s", data_string
);
141 data_incl_type
= talloc_asprintf(data_string
, "hex:%s",
145 data_incl_type
= talloc_asprintf(data_string
, "hex(%x):%s",
146 value_type
, data_string
);
150 if (value_name
[0] == '\0') {
151 fdprintf(data
->fd
, "@=%s\n", data_incl_type
);
153 fdprintf(data
->fd
, "\"%s\"=%s\n",
154 value_name
, data_incl_type
);
157 talloc_free(data_string
);
162 static WERROR
reg_dotreg_diff_del_value(void *_data
, const char *path
,
163 const char *value_name
)
165 struct dotreg_data
*data
= (struct dotreg_data
*)_data
;
167 fdprintf(data
->fd
, "\"%s\"=-\n", value_name
);
172 static WERROR
reg_dotreg_diff_done(void *_data
)
174 struct dotreg_data
*data
= (struct dotreg_data
*)_data
;
182 static WERROR
reg_dotreg_diff_del_all_values(void *callback_data
,
183 const char *key_name
)
185 return WERR_NOT_SUPPORTED
;
191 _PUBLIC_ WERROR
reg_dotreg_diff_save(TALLOC_CTX
*ctx
, const char *filename
,
192 struct reg_diff_callbacks
**callbacks
,
193 void **callback_data
)
195 struct dotreg_data
*data
;
197 data
= talloc_zero(ctx
, struct dotreg_data
);
198 *callback_data
= data
;
201 data
->fd
= open(filename
, O_CREAT
|O_WRONLY
, 0755);
203 DEBUG(0, ("Unable to open %s\n", filename
));
204 return WERR_FILE_NOT_FOUND
;
207 data
->fd
= STDOUT_FILENO
;
210 fdprintf(data
->fd
, "%s\n\n", HEADER_STRING
);
212 *callbacks
= talloc(ctx
, struct reg_diff_callbacks
);
214 (*callbacks
)->add_key
= reg_dotreg_diff_add_key
;
215 (*callbacks
)->del_key
= reg_dotreg_diff_del_key
;
216 (*callbacks
)->set_value
= reg_dotreg_diff_set_value
;
217 (*callbacks
)->del_value
= reg_dotreg_diff_del_value
;
218 (*callbacks
)->del_all_values
= reg_dotreg_diff_del_all_values
;
219 (*callbacks
)->done
= reg_dotreg_diff_done
;
227 _PUBLIC_ WERROR
reg_dotreg_diff_load(int fd
,
228 const struct reg_diff_callbacks
*callbacks
,
233 TALLOC_CTX
*mem_ctx
= talloc_init("reg_dotreg_diff_load");
238 char *type_str
= NULL
;
239 char *data_str
= NULL
;
241 bool continue_next_line
= 0;
243 line
= afdgets(fd
, mem_ctx
, 0);
245 DEBUG(0, ("Can't read from file.\n"));
246 talloc_free(mem_ctx
);
248 return WERR_GEN_FAILURE
;
251 while ((line
= afdgets(fd
, mem_ctx
, 0))) {
252 /* Remove '\r' if it's a Windows text file */
253 if (strlen(line
) && line
[strlen(line
)-1] == '\r') {
254 line
[strlen(line
)-1] = '\0';
257 /* Ignore comments and empty lines */
258 if (strlen(line
) == 0 || line
[0] == ';') {
269 if (line
[0] == '[') {
270 if (line
[strlen(line
)-1] != ']') {
271 DEBUG(0, ("Missing ']' on line: %s\n", line
));
277 if (line
[1] == '-') {
278 curkey
= talloc_strndup(line
, line
+2, strlen(line
)-3);
279 W_ERROR_HAVE_NO_MEMORY(curkey
);
281 error
= callbacks
->del_key(callback_data
,
284 if (!W_ERROR_IS_OK(error
)) {
285 DEBUG(0,("Error deleting key %s\n",
287 talloc_free(mem_ctx
);
295 curkey
= talloc_strndup(mem_ctx
, line
+1, strlen(line
)-2);
296 W_ERROR_HAVE_NO_MEMORY(curkey
);
298 error
= callbacks
->add_key(callback_data
, curkey
);
299 if (!W_ERROR_IS_OK(error
)) {
300 DEBUG(0,("Error adding key %s\n", curkey
));
301 talloc_free(mem_ctx
);
309 /* Deleting/Changing value */
310 if (continue_next_line
) {
311 continue_next_line
= 0;
313 /* Continued data start with two whitespaces */
314 if (line
[0] != ' ' || line
[1] != ' ') {
315 DEBUG(0, ("Malformed line: %s\n", line
));
321 /* Continue again if line ends with a backslash */
322 if (line
[strlen(line
)-1] == '\\') {
323 line
[strlen(line
)-1] = '\0';
324 continue_next_line
= 1;
325 data_str
= talloc_strdup_append(data_str
, p
);
329 data_str
= talloc_strdup_append(data_str
, p
);
331 p
= strchr_m(line
, '=');
333 DEBUG(0, ("Malformed line: %s\n", line
));
341 if (curkey
== NULL
) {
342 DEBUG(0, ("Value change without key\n"));
347 /* Values should be double-quoted */
348 if (line
[0] != '"') {
349 DEBUG(0, ("Malformed line\n"));
354 /* Chop of the quotes and store as value */
355 value
= talloc_strndup(mem_ctx
, line
+1,strlen(line
)-2);
359 error
= callbacks
->del_value(callback_data
,
362 /* Ignore if key does not exist (WERR_FILE_NOT_FOUND)
363 * Consistent with Windows behaviour */
364 if (!W_ERROR_IS_OK(error
) &&
365 !W_ERROR_EQUAL(error
, WERR_FILE_NOT_FOUND
)) {
366 DEBUG(0, ("Error deleting value %s in key %s\n",
368 talloc_free(mem_ctx
);
377 /* Do not look for colons in strings */
380 data_str
= talloc_strndup(mem_ctx
, p
+1,strlen(p
)-2);
382 /* Split the value type from the data */
383 q
= strchr_m(p
, ':');
387 type_str
= talloc_strdup(mem_ctx
, p
);
388 data_str
= talloc_strdup(mem_ctx
, q
);
390 data_str
= talloc_strdup(mem_ctx
, p
);
394 /* Backslash before the CRLF means continue on next line */
395 if (data_str
[strlen(data_str
)-1] == '\\') {
396 data_str
[strlen(data_str
)-1] = '\0';
398 continue_next_line
= 1;
402 DEBUG(9, ("About to write %s with type %s, length %ld: %s\n", value
, type_str
, (long) strlen(data_str
), data_str
));
403 result
= reg_string_to_val(value
,
404 type_str
?type_str
:"REG_SZ", data_str
,
407 DEBUG(0, ("Error converting string to value for line:\n%s\n",
409 return WERR_GEN_FAILURE
;
412 error
= callbacks
->set_value(callback_data
, curkey
, value
,
414 if (!W_ERROR_IS_OK(error
)) {
415 DEBUG(0, ("Error setting value for %s in %s\n",
417 talloc_free(mem_ctx
);
421 /* Clean up buffers */
422 if (type_str
!= NULL
) {
423 talloc_free(type_str
);
426 talloc_free(data_str
);
433 talloc_free(mem_ctx
);