1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
/*
Unix SMB/CIFS implementation.
Reading .REG files
Copyright (C) Jelmer Vernooij 2004-2007
Copyright (C) Wilco Baan Hofman 2006-2010
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* FIXME:
* - Newer .REG files, created by Windows XP and above use unicode UCS-2
* - @="" constructions should write value with empty name.
*/
#include "includes.h"
#include "lib/registry/registry.h"
#include "system/filesys.h"
/**
* @file
* @brief Registry patch files
*/
#define HEADER_STRING "REGEDIT4"
struct dotreg_data {
int fd;
};
/*
* This is basically a copy of data_blob_hex_string_upper, but with comma's
* between the bytes in hex.
*/
static char *dotreg_data_blob_hex_string(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob)
{
size_t i;
char *hex_string;
hex_string = talloc_array(mem_ctx, char, (blob->length*3)+1);
if (!hex_string) {
return NULL;
}
for (i = 0; i < blob->length; i++)
slprintf(&hex_string[i*3], 4, "%02X,", blob->data[i]);
/* Remove last comma and NULL-terminate the string */
hex_string[(blob->length*3)-1] = '\0';
return hex_string;
}
/*
* This is basically a copy of reg_val_data_string, except that this function
* has no 0x for dwords, everything else is regarded as binary, and binary
* strings are represented with bytes comma-separated.
*/
static char *reg_val_dotreg_string(TALLOC_CTX *mem_ctx, uint32_t type,
const DATA_BLOB data)
{
size_t converted_size = 0;
char *ret = NULL;
if (data.length == 0)
return talloc_strdup(mem_ctx, "");
switch (type) {
case REG_EXPAND_SZ:
case REG_SZ:
convert_string_talloc(mem_ctx,
CH_UTF16, CH_UNIX, data.data, data.length,
(void **)&ret, &converted_size);
break;
case REG_DWORD:
case REG_DWORD_BIG_ENDIAN:
SMB_ASSERT(data.length == sizeof(uint32_t));
ret = talloc_asprintf(mem_ctx, "%08x",
IVAL(data.data, 0));
break;
default: /* default means treat as binary */
case REG_BINARY:
ret = dotreg_data_blob_hex_string(mem_ctx, &data);
break;
}
return ret;
}
static WERROR reg_dotreg_diff_add_key(void *_data, const char *key_name)
{
struct dotreg_data *data = (struct dotreg_data *)_data;
fdprintf(data->fd, "\n[%s]\n", key_name);
return WERR_OK;
}
static WERROR reg_dotreg_diff_del_key(void *_data, const char *key_name)
{
struct dotreg_data *data = (struct dotreg_data *)_data;
fdprintf(data->fd, "\n[-%s]\n", key_name);
return WERR_OK;
}
static WERROR reg_dotreg_diff_set_value(void *_data, const char *path,
const char *value_name,
uint32_t value_type, DATA_BLOB value)
{
struct dotreg_data *data = (struct dotreg_data *)_data;
char *data_string = reg_val_dotreg_string(NULL,
value_type, value);
char *data_incl_type;
W_ERROR_HAVE_NO_MEMORY(data_string);
switch (value_type) {
case REG_SZ:
data_incl_type = talloc_asprintf(data_string, "\"%s\"",
data_string);
break;
case REG_DWORD:
data_incl_type = talloc_asprintf(data_string,
"dword:%s", data_string);
break;
case REG_BINARY:
data_incl_type = talloc_asprintf(data_string, "hex:%s",
data_string);
break;
default:
data_incl_type = talloc_asprintf(data_string, "hex(%x):%s",
value_type, data_string);
break;
}
if (value_name[0] == '\0') {
fdprintf(data->fd, "@=%s\n", data_incl_type);
} else {
fdprintf(data->fd, "\"%s\"=%s\n",
value_name, data_incl_type);
}
talloc_free(data_string);
return WERR_OK;
}
static WERROR reg_dotreg_diff_del_value(void *_data, const char *path,
const char *value_name)
{
struct dotreg_data *data = (struct dotreg_data *)_data;
fdprintf(data->fd, "\"%s\"=-\n", value_name);
return WERR_OK;
}
static WERROR reg_dotreg_diff_done(void *_data)
{
struct dotreg_data *data = (struct dotreg_data *)_data;
close(data->fd);
talloc_free(data);
return WERR_OK;
}
static WERROR reg_dotreg_diff_del_all_values(void *callback_data,
const char *key_name)
{
return WERR_NOT_SUPPORTED;
}
/**
* Save registry diff
*/
_PUBLIC_ WERROR reg_dotreg_diff_save(TALLOC_CTX *ctx, const char *filename,
struct reg_diff_callbacks **callbacks,
void **callback_data)
{
struct dotreg_data *data;
data = talloc_zero(ctx, struct dotreg_data);
*callback_data = data;
if (filename) {
data->fd = open(filename, O_CREAT|O_WRONLY, 0755);
if (data->fd < 0) {
DEBUG(0, ("Unable to open %s\n", filename));
return WERR_FILE_NOT_FOUND;
}
} else {
data->fd = STDOUT_FILENO;
}
fdprintf(data->fd, "%s\n\n", HEADER_STRING);
*callbacks = talloc(ctx, struct reg_diff_callbacks);
(*callbacks)->add_key = reg_dotreg_diff_add_key;
(*callbacks)->del_key = reg_dotreg_diff_del_key;
(*callbacks)->set_value = reg_dotreg_diff_set_value;
(*callbacks)->del_value = reg_dotreg_diff_del_value;
(*callbacks)->del_all_values = reg_dotreg_diff_del_all_values;
(*callbacks)->done = reg_dotreg_diff_done;
return WERR_OK;
}
/**
* Load diff file
*/
_PUBLIC_ WERROR reg_dotreg_diff_load(int fd,
const struct reg_diff_callbacks *callbacks,
void *callback_data)
{
char *line, *p, *q;
char *curkey = NULL;
TALLOC_CTX *mem_ctx = talloc_init("reg_dotreg_diff_load");
WERROR error;
uint32_t value_type;
DATA_BLOB data;
bool result;
char *type_str = NULL;
char *data_str = NULL;
char *value = NULL;
bool continue_next_line = 0;
line = afdgets(fd, mem_ctx, 0);
if (!line) {
DEBUG(0, ("Can't read from file.\n"));
talloc_free(mem_ctx);
close(fd);
return WERR_GEN_FAILURE;
}
while ((line = afdgets(fd, mem_ctx, 0))) {
/* Remove '\r' if it's a Windows text file */
if (strlen(line) && line[strlen(line)-1] == '\r') {
line[strlen(line)-1] = '\0';
}
/* Ignore comments and empty lines */
if (strlen(line) == 0 || line[0] == ';') {
talloc_free(line);
if (curkey) {
talloc_free(curkey);
}
curkey = NULL;
continue;
}
/* Start of key */
if (line[0] == '[') {
if (line[strlen(line)-1] != ']') {
DEBUG(0, ("Missing ']' on line: %s\n", line));
talloc_free(line);
continue;
}
/* Deleting key */
if (line[1] == '-') {
curkey = talloc_strndup(line, line+2, strlen(line)-3);
W_ERROR_HAVE_NO_MEMORY(curkey);
error = callbacks->del_key(callback_data,
curkey);
if (!W_ERROR_IS_OK(error)) {
DEBUG(0,("Error deleting key %s\n",
curkey));
talloc_free(mem_ctx);
return error;
}
talloc_free(line);
curkey = NULL;
continue;
}
curkey = talloc_strndup(mem_ctx, line+1, strlen(line)-2);
W_ERROR_HAVE_NO_MEMORY(curkey);
error = callbacks->add_key(callback_data, curkey);
if (!W_ERROR_IS_OK(error)) {
DEBUG(0,("Error adding key %s\n", curkey));
talloc_free(mem_ctx);
return error;
}
talloc_free(line);
continue;
}
/* Deleting/Changing value */
if (continue_next_line) {
continue_next_line = 0;
/* Continued data start with two whitespaces */
if (line[0] != ' ' || line[1] != ' ') {
DEBUG(0, ("Malformed line: %s\n", line));
talloc_free(line);
continue;
}
p = line + 2;
/* Continue again if line ends with a backslash */
if (line[strlen(line)-1] == '\\') {
line[strlen(line)-1] = '\0';
continue_next_line = 1;
data_str = talloc_strdup_append(data_str, p);
talloc_free(line);
continue;
}
data_str = talloc_strdup_append(data_str, p);
} else {
p = strchr_m(line, '=');
if (p == NULL) {
DEBUG(0, ("Malformed line: %s\n", line));
talloc_free(line);
continue;
}
*p = '\0'; p++;
if (curkey == NULL) {
DEBUG(0, ("Value change without key\n"));
talloc_free(line);
continue;
}
/* Values should be double-quoted */
if (line[0] != '"') {
DEBUG(0, ("Malformed line\n"));
talloc_free(line);
continue;
}
/* Chop of the quotes and store as value */
value = talloc_strndup(mem_ctx, line+1,strlen(line)-2);
/* Delete value */
if (p[0] == '-') {
error = callbacks->del_value(callback_data,
curkey, value);
/* Ignore if key does not exist (WERR_FILE_NOT_FOUND)
* Consistent with Windows behaviour */
if (!W_ERROR_IS_OK(error) &&
!W_ERROR_EQUAL(error, WERR_FILE_NOT_FOUND)) {
DEBUG(0, ("Error deleting value %s in key %s\n",
value, curkey));
talloc_free(mem_ctx);
return error;
}
talloc_free(line);
talloc_free(value);
continue;
}
/* Do not look for colons in strings */
if (p[0] == '"') {
q = NULL;
data_str = talloc_strndup(mem_ctx, p+1,strlen(p)-2);
} else {
/* Split the value type from the data */
q = strchr_m(p, ':');
if (q) {
*q = '\0';
q++;
type_str = talloc_strdup(mem_ctx, p);
data_str = talloc_strdup(mem_ctx, q);
} else {
data_str = talloc_strdup(mem_ctx, p);
}
}
/* Backslash before the CRLF means continue on next line */
if (data_str[strlen(data_str)-1] == '\\') {
data_str[strlen(data_str)-1] = '\0';
talloc_free(line);
continue_next_line = 1;
continue;
}
}
DEBUG(9, ("About to write %s with type %s, length %ld: %s\n", value, type_str, (long) strlen(data_str), data_str));
result = reg_string_to_val(value,
type_str?type_str:"REG_SZ", data_str,
&value_type, &data);
if (!result) {
DEBUG(0, ("Error converting string to value for line:\n%s\n",
line));
return WERR_GEN_FAILURE;
}
error = callbacks->set_value(callback_data, curkey, value,
value_type, data);
if (!W_ERROR_IS_OK(error)) {
DEBUG(0, ("Error setting value for %s in %s\n",
value, curkey));
talloc_free(mem_ctx);
return error;
}
/* Clean up buffers */
if (type_str != NULL) {
talloc_free(type_str);
type_str = NULL;
}
talloc_free(data_str);
talloc_free(value);
talloc_free(line);
}
close(fd);
talloc_free(mem_ctx);
return WERR_OK;
}
|