1 | /* |
---|
2 | * Branched from Samba project Subversion repository, version #7470: |
---|
3 | * http://viewcvs.samba.org/cgi-bin/viewcvs.cgi/trunk/source/registry/regfio.c?rev=7470&view=auto |
---|
4 | * |
---|
5 | * Windows NT (and later) registry parsing library |
---|
6 | * |
---|
7 | * Copyright (C) 2005-2009 Timothy D. Morgan |
---|
8 | * Copyright (C) 2005 Gerald (Jerry) Carter |
---|
9 | * |
---|
10 | * This program is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 3 of the License. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | * $Id: regfi.c 148 2009-02-22 23:22:59Z tim $ |
---|
24 | */ |
---|
25 | |
---|
26 | #include "regfi.h" |
---|
27 | |
---|
28 | |
---|
29 | /* Registry types mapping */ |
---|
30 | const unsigned int regfi_num_reg_types = 12; |
---|
31 | static const char* regfi_type_names[] = |
---|
32 | {"NONE", "SZ", "EXPAND_SZ", "BINARY", "DWORD", "DWORD_BE", "LINK", |
---|
33 | "MULTI_SZ", "RSRC_LIST", "RSRC_DESC", "RSRC_REQ_LIST", "QWORD"}; |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | /****************************************************************************** |
---|
38 | ******************************************************************************/ |
---|
39 | void regfi_add_message(REGFI_FILE* file, uint16 msg_type, const char* fmt, ...) |
---|
40 | { |
---|
41 | /* XXX: This function is not particularly efficient, |
---|
42 | * but then it is mostly used during errors. |
---|
43 | */ |
---|
44 | uint32 buf_size, buf_used; |
---|
45 | char* new_msg; |
---|
46 | va_list args; |
---|
47 | |
---|
48 | if((file->msg_mask & msg_type) != 0) |
---|
49 | { |
---|
50 | if(file->last_message == NULL) |
---|
51 | buf_used = 0; |
---|
52 | else |
---|
53 | buf_used = strlen(file->last_message); |
---|
54 | |
---|
55 | buf_size = buf_used+strlen(fmt)+160; |
---|
56 | new_msg = realloc(file->last_message, buf_size); |
---|
57 | if(new_msg == NULL) |
---|
58 | /* XXX: should we report this? */ |
---|
59 | return; |
---|
60 | |
---|
61 | switch (msg_type) |
---|
62 | { |
---|
63 | case REGFI_MSG_INFO: |
---|
64 | strcpy(new_msg+buf_used, "INFO: "); |
---|
65 | buf_used += 6; |
---|
66 | break; |
---|
67 | case REGFI_MSG_WARN: |
---|
68 | strcpy(new_msg+buf_used, "WARN: "); |
---|
69 | buf_used += 6; |
---|
70 | break; |
---|
71 | case REGFI_MSG_ERROR: |
---|
72 | strcpy(new_msg+buf_used, "ERROR: "); |
---|
73 | buf_used += 7; |
---|
74 | break; |
---|
75 | } |
---|
76 | |
---|
77 | va_start(args, fmt); |
---|
78 | vsnprintf(new_msg+buf_used, buf_size-buf_used, fmt, args); |
---|
79 | va_end(args); |
---|
80 | strncat(new_msg, "\n", buf_size-1); |
---|
81 | |
---|
82 | file->last_message = new_msg; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | /****************************************************************************** |
---|
88 | ******************************************************************************/ |
---|
89 | char* regfi_get_messages(REGFI_FILE* file) |
---|
90 | { |
---|
91 | char* ret_val = file->last_message; |
---|
92 | file->last_message = NULL; |
---|
93 | |
---|
94 | return ret_val; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | void regfi_set_message_mask(REGFI_FILE* file, uint16 mask) |
---|
99 | { |
---|
100 | file->msg_mask = mask; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | /* Returns NULL on error */ |
---|
105 | const char* regfi_type_val2str(unsigned int val) |
---|
106 | { |
---|
107 | if(val == REG_KEY) |
---|
108 | return "KEY"; |
---|
109 | |
---|
110 | if(val >= regfi_num_reg_types) |
---|
111 | return NULL; |
---|
112 | |
---|
113 | return regfi_type_names[val]; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | /* Returns -1 on error */ |
---|
118 | int regfi_type_str2val(const char* str) |
---|
119 | { |
---|
120 | int i; |
---|
121 | |
---|
122 | if(strcmp("KEY", str) == 0) |
---|
123 | return REG_KEY; |
---|
124 | |
---|
125 | for(i=0; i < regfi_num_reg_types; i++) |
---|
126 | if (strcmp(regfi_type_names[i], str) == 0) |
---|
127 | return i; |
---|
128 | |
---|
129 | if(strcmp("DWORD_LE", str) == 0) |
---|
130 | return REG_DWORD_LE; |
---|
131 | |
---|
132 | return -1; |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | /* Security descriptor formatting functions */ |
---|
137 | |
---|
138 | const char* regfi_ace_type2str(uint8 type) |
---|
139 | { |
---|
140 | static const char* map[7] |
---|
141 | = {"ALLOW", "DENY", "AUDIT", "ALARM", |
---|
142 | "ALLOW CPD", "OBJ ALLOW", "OBJ DENY"}; |
---|
143 | if(type < 7) |
---|
144 | return map[type]; |
---|
145 | else |
---|
146 | /* XXX: would be nice to return the unknown integer value. |
---|
147 | * However, as it is a const string, it can't be free()ed later on, |
---|
148 | * so that would need to change. |
---|
149 | */ |
---|
150 | return "UNKNOWN"; |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | /* XXX: need a better reference on the meaning of each flag. */ |
---|
155 | /* For more info, see: |
---|
156 | * http://msdn2.microsoft.com/en-us/library/aa772242.aspx |
---|
157 | */ |
---|
158 | char* regfi_ace_flags2str(uint8 flags) |
---|
159 | { |
---|
160 | static const char* flag_map[32] = |
---|
161 | { "OI", /* Object Inherit */ |
---|
162 | "CI", /* Container Inherit */ |
---|
163 | "NP", /* Non-Propagate */ |
---|
164 | "IO", /* Inherit Only */ |
---|
165 | "IA", /* Inherited ACE */ |
---|
166 | NULL, |
---|
167 | NULL, |
---|
168 | NULL, |
---|
169 | }; |
---|
170 | |
---|
171 | char* ret_val = malloc(35*sizeof(char)); |
---|
172 | char* fo = ret_val; |
---|
173 | uint32 i; |
---|
174 | uint8 f; |
---|
175 | |
---|
176 | if(ret_val == NULL) |
---|
177 | return NULL; |
---|
178 | |
---|
179 | fo[0] = '\0'; |
---|
180 | if (!flags) |
---|
181 | return ret_val; |
---|
182 | |
---|
183 | for(i=0; i < 8; i++) |
---|
184 | { |
---|
185 | f = (1<<i); |
---|
186 | if((flags & f) && (flag_map[i] != NULL)) |
---|
187 | { |
---|
188 | strcpy(fo, flag_map[i]); |
---|
189 | fo += strlen(flag_map[i]); |
---|
190 | *(fo++) = ' '; |
---|
191 | flags ^= f; |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | /* Any remaining unknown flags are added at the end in hex. */ |
---|
196 | if(flags != 0) |
---|
197 | sprintf(fo, "0x%.2X ", flags); |
---|
198 | |
---|
199 | /* Chop off the last space if we've written anything to ret_val */ |
---|
200 | if(fo != ret_val) |
---|
201 | fo[-1] = '\0'; |
---|
202 | |
---|
203 | return ret_val; |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | char* regfi_ace_perms2str(uint32 perms) |
---|
208 | { |
---|
209 | uint32 i, p; |
---|
210 | /* This is more than is needed by a fair margin. */ |
---|
211 | char* ret_val = malloc(350*sizeof(char)); |
---|
212 | char* r = ret_val; |
---|
213 | |
---|
214 | /* Each represents one of 32 permissions bits. NULL is for undefined/reserved bits. |
---|
215 | * For more information, see: |
---|
216 | * http://msdn2.microsoft.com/en-gb/library/aa374892.aspx |
---|
217 | * http://msdn2.microsoft.com/en-gb/library/ms724878.aspx |
---|
218 | */ |
---|
219 | static const char* perm_map[32] = |
---|
220 | {/* object-specific permissions (registry keys, in this case) */ |
---|
221 | "QRY_VAL", /* KEY_QUERY_VALUE */ |
---|
222 | "SET_VAL", /* KEY_SET_VALUE */ |
---|
223 | "CREATE_KEY", /* KEY_CREATE_SUB_KEY */ |
---|
224 | "ENUM_KEYS", /* KEY_ENUMERATE_SUB_KEYS */ |
---|
225 | "NOTIFY", /* KEY_NOTIFY */ |
---|
226 | "CREATE_LNK", /* KEY_CREATE_LINK - Reserved for system use. */ |
---|
227 | NULL, |
---|
228 | NULL, |
---|
229 | "WOW64_64", /* KEY_WOW64_64KEY */ |
---|
230 | "WOW64_32", /* KEY_WOW64_32KEY */ |
---|
231 | NULL, |
---|
232 | NULL, |
---|
233 | NULL, |
---|
234 | NULL, |
---|
235 | NULL, |
---|
236 | NULL, |
---|
237 | /* standard access rights */ |
---|
238 | "DELETE", /* DELETE */ |
---|
239 | "R_CONT", /* READ_CONTROL */ |
---|
240 | "W_DAC", /* WRITE_DAC */ |
---|
241 | "W_OWNER", /* WRITE_OWNER */ |
---|
242 | "SYNC", /* SYNCHRONIZE - Shouldn't be set in registries */ |
---|
243 | NULL, |
---|
244 | NULL, |
---|
245 | NULL, |
---|
246 | /* other generic */ |
---|
247 | "SYS_SEC", /* ACCESS_SYSTEM_SECURITY */ |
---|
248 | "MAX_ALLWD", /* MAXIMUM_ALLOWED */ |
---|
249 | NULL, |
---|
250 | NULL, |
---|
251 | "GEN_A", /* GENERIC_ALL */ |
---|
252 | "GEN_X", /* GENERIC_EXECUTE */ |
---|
253 | "GEN_W", /* GENERIC_WRITE */ |
---|
254 | "GEN_R", /* GENERIC_READ */ |
---|
255 | }; |
---|
256 | |
---|
257 | |
---|
258 | if(ret_val == NULL) |
---|
259 | return NULL; |
---|
260 | |
---|
261 | r[0] = '\0'; |
---|
262 | for(i=0; i < 32; i++) |
---|
263 | { |
---|
264 | p = (1<<i); |
---|
265 | if((perms & p) && (perm_map[i] != NULL)) |
---|
266 | { |
---|
267 | strcpy(r, perm_map[i]); |
---|
268 | r += strlen(perm_map[i]); |
---|
269 | *(r++) = ' '; |
---|
270 | perms ^= p; |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | /* Any remaining unknown permission bits are added at the end in hex. */ |
---|
275 | if(perms != 0) |
---|
276 | sprintf(r, "0x%.8X ", perms); |
---|
277 | |
---|
278 | /* Chop off the last space if we've written anything to ret_val */ |
---|
279 | if(r != ret_val) |
---|
280 | r[-1] = '\0'; |
---|
281 | |
---|
282 | return ret_val; |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | char* regfi_sid2str(WINSEC_DOM_SID* sid) |
---|
287 | { |
---|
288 | uint32 i, size = WINSEC_MAX_SUBAUTHS*11 + 24; |
---|
289 | uint32 left = size; |
---|
290 | uint8 comps = sid->num_auths; |
---|
291 | char* ret_val = malloc(size); |
---|
292 | |
---|
293 | if(ret_val == NULL) |
---|
294 | return NULL; |
---|
295 | |
---|
296 | if(comps > WINSEC_MAX_SUBAUTHS) |
---|
297 | comps = WINSEC_MAX_SUBAUTHS; |
---|
298 | |
---|
299 | left -= sprintf(ret_val, "S-%u-%u", sid->sid_rev_num, sid->id_auth[5]); |
---|
300 | |
---|
301 | for (i = 0; i < comps; i++) |
---|
302 | left -= snprintf(ret_val+(size-left), left, "-%u", sid->sub_auths[i]); |
---|
303 | |
---|
304 | return ret_val; |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | char* regfi_get_acl(WINSEC_ACL* acl) |
---|
309 | { |
---|
310 | uint32 i, extra, size = 0; |
---|
311 | const char* type_str; |
---|
312 | char* flags_str; |
---|
313 | char* perms_str; |
---|
314 | char* sid_str; |
---|
315 | char* ace_delim = ""; |
---|
316 | char* ret_val = NULL; |
---|
317 | char* tmp_val = NULL; |
---|
318 | bool failed = false; |
---|
319 | char field_delim = ':'; |
---|
320 | |
---|
321 | for (i = 0; i < acl->num_aces && !failed; i++) |
---|
322 | { |
---|
323 | sid_str = regfi_sid2str(acl->aces[i]->trustee); |
---|
324 | type_str = regfi_ace_type2str(acl->aces[i]->type); |
---|
325 | perms_str = regfi_ace_perms2str(acl->aces[i]->access_mask); |
---|
326 | flags_str = regfi_ace_flags2str(acl->aces[i]->flags); |
---|
327 | |
---|
328 | if(flags_str != NULL && perms_str != NULL |
---|
329 | && type_str != NULL && sid_str != NULL) |
---|
330 | { |
---|
331 | /* XXX: this is slow */ |
---|
332 | extra = strlen(sid_str) + strlen(type_str) |
---|
333 | + strlen(perms_str) + strlen(flags_str) + 5; |
---|
334 | tmp_val = realloc(ret_val, size+extra); |
---|
335 | |
---|
336 | if(tmp_val == NULL) |
---|
337 | { |
---|
338 | free(ret_val); |
---|
339 | ret_val = NULL; |
---|
340 | failed = true; |
---|
341 | } |
---|
342 | else |
---|
343 | { |
---|
344 | ret_val = tmp_val; |
---|
345 | size += sprintf(ret_val+size, "%s%s%c%s%c%s%c%s", |
---|
346 | ace_delim,sid_str, |
---|
347 | field_delim,type_str, |
---|
348 | field_delim,perms_str, |
---|
349 | field_delim,flags_str); |
---|
350 | ace_delim = "|"; |
---|
351 | } |
---|
352 | } |
---|
353 | else |
---|
354 | failed = true; |
---|
355 | |
---|
356 | if(sid_str != NULL) |
---|
357 | free(sid_str); |
---|
358 | if(sid_str != NULL) |
---|
359 | free(perms_str); |
---|
360 | if(sid_str != NULL) |
---|
361 | free(flags_str); |
---|
362 | } |
---|
363 | |
---|
364 | return ret_val; |
---|
365 | } |
---|
366 | |
---|
367 | |
---|
368 | char* regfi_get_sacl(WINSEC_DESC *sec_desc) |
---|
369 | { |
---|
370 | if (sec_desc->sacl) |
---|
371 | return regfi_get_acl(sec_desc->sacl); |
---|
372 | else |
---|
373 | return NULL; |
---|
374 | } |
---|
375 | |
---|
376 | |
---|
377 | char* regfi_get_dacl(WINSEC_DESC *sec_desc) |
---|
378 | { |
---|
379 | if (sec_desc->dacl) |
---|
380 | return regfi_get_acl(sec_desc->dacl); |
---|
381 | else |
---|
382 | return NULL; |
---|
383 | } |
---|
384 | |
---|
385 | |
---|
386 | char* regfi_get_owner(WINSEC_DESC *sec_desc) |
---|
387 | { |
---|
388 | return regfi_sid2str(sec_desc->owner_sid); |
---|
389 | } |
---|
390 | |
---|
391 | |
---|
392 | char* regfi_get_group(WINSEC_DESC *sec_desc) |
---|
393 | { |
---|
394 | return regfi_sid2str(sec_desc->grp_sid); |
---|
395 | } |
---|
396 | |
---|
397 | |
---|
398 | /***************************************************************************** |
---|
399 | * This function is just like read(2), except that it continues to |
---|
400 | * re-try reading from the file descriptor if EINTR or EAGAIN is received. |
---|
401 | * regfi_read will attempt to read length bytes from fd and write them to buf. |
---|
402 | * |
---|
403 | * On success, 0 is returned. Upon failure, an errno code is returned. |
---|
404 | * |
---|
405 | * The number of bytes successfully read is returned through the length |
---|
406 | * parameter by reference. If both the return value and length parameter are |
---|
407 | * returned as 0, then EOF was encountered immediately |
---|
408 | *****************************************************************************/ |
---|
409 | uint32 regfi_read(int fd, uint8* buf, uint32* length) |
---|
410 | { |
---|
411 | uint32 rsize = 0; |
---|
412 | uint32 rret = 0; |
---|
413 | |
---|
414 | do |
---|
415 | { |
---|
416 | rret = read(fd, buf + rsize, *length - rsize); |
---|
417 | if(rret > 0) |
---|
418 | rsize += rret; |
---|
419 | }while(*length - rsize > 0 |
---|
420 | && (rret > 0 || (rret == -1 && (errno == EAGAIN || errno == EINTR)))); |
---|
421 | |
---|
422 | *length = rsize; |
---|
423 | if (rret == -1 && errno != EINTR && errno != EAGAIN) |
---|
424 | return errno; |
---|
425 | |
---|
426 | return 0; |
---|
427 | } |
---|
428 | |
---|
429 | |
---|
430 | /***************************************************************************** |
---|
431 | * |
---|
432 | *****************************************************************************/ |
---|
433 | bool regfi_parse_cell(int fd, uint32 offset, uint8* hdr, uint32 hdr_len, |
---|
434 | uint32* cell_length, bool* unalloc) |
---|
435 | { |
---|
436 | uint32 length; |
---|
437 | int32 raw_length; |
---|
438 | uint8 tmp[4]; |
---|
439 | |
---|
440 | if(lseek(fd, offset, SEEK_SET) == -1) |
---|
441 | return false; |
---|
442 | |
---|
443 | length = 4; |
---|
444 | if((regfi_read(fd, tmp, &length) != 0) || length != 4) |
---|
445 | return false; |
---|
446 | raw_length = IVALS(tmp, 0); |
---|
447 | |
---|
448 | if(raw_length < 0) |
---|
449 | { |
---|
450 | (*cell_length) = raw_length*(-1); |
---|
451 | (*unalloc) = false; |
---|
452 | } |
---|
453 | else |
---|
454 | { |
---|
455 | (*cell_length) = raw_length; |
---|
456 | (*unalloc) = true; |
---|
457 | } |
---|
458 | |
---|
459 | if(*cell_length - 4 < hdr_len) |
---|
460 | return false; |
---|
461 | |
---|
462 | if(hdr_len > 0) |
---|
463 | { |
---|
464 | length = hdr_len; |
---|
465 | if((regfi_read(fd, hdr, &length) != 0) || length != hdr_len) |
---|
466 | return false; |
---|
467 | } |
---|
468 | |
---|
469 | return true; |
---|
470 | } |
---|
471 | |
---|
472 | |
---|
473 | /******************************************************************* |
---|
474 | * Given an offset and an hbin, is the offset within that hbin? |
---|
475 | * The offset is a virtual file offset. |
---|
476 | *******************************************************************/ |
---|
477 | static bool regfi_offset_in_hbin(const REGFI_HBIN* hbin, uint32 voffset) |
---|
478 | { |
---|
479 | if(!hbin) |
---|
480 | return false; |
---|
481 | |
---|
482 | if((voffset > hbin->first_hbin_off) |
---|
483 | && (voffset < (hbin->first_hbin_off + hbin->block_size))) |
---|
484 | return true; |
---|
485 | |
---|
486 | return false; |
---|
487 | } |
---|
488 | |
---|
489 | |
---|
490 | |
---|
491 | /******************************************************************* |
---|
492 | * Provide a virtual offset and receive the correpsonding HBIN |
---|
493 | * block for it. NULL if one doesn't exist. |
---|
494 | *******************************************************************/ |
---|
495 | const REGFI_HBIN* regfi_lookup_hbin(REGFI_FILE* file, uint32 voffset) |
---|
496 | { |
---|
497 | return (const REGFI_HBIN*)range_list_find_data(file->hbins, |
---|
498 | voffset+REGFI_REGF_SIZE); |
---|
499 | } |
---|
500 | |
---|
501 | |
---|
502 | |
---|
503 | /****************************************************************************** |
---|
504 | ******************************************************************************/ |
---|
505 | REGFI_SUBKEY_LIST* regfi_load_subkeylist(REGFI_FILE* file, uint32 offset, |
---|
506 | uint32 num_keys, uint32 max_size, |
---|
507 | bool strict) |
---|
508 | { |
---|
509 | REGFI_SUBKEY_LIST* ret_val; |
---|
510 | |
---|
511 | ret_val = regfi_load_subkeylist_aux(file, offset, max_size, strict, |
---|
512 | REGFI_MAX_SUBKEY_DEPTH); |
---|
513 | if(ret_val == NULL) |
---|
514 | { |
---|
515 | regfi_add_message(file, REGFI_MSG_WARN, "Failed to load subkey list at" |
---|
516 | " offset 0x%.8X.", offset); |
---|
517 | return NULL; |
---|
518 | } |
---|
519 | |
---|
520 | if(num_keys != ret_val->num_keys) |
---|
521 | { |
---|
522 | /* Not sure which should be authoritative, the number from the |
---|
523 | * NK record, or the number in the subkey list. Just emit a warning for |
---|
524 | * now if they don't match. |
---|
525 | */ |
---|
526 | regfi_add_message(file, REGFI_MSG_WARN, "Number of subkeys listed in parent" |
---|
527 | " (%d) did not match number found in subkey list/tree (%d)" |
---|
528 | " while parsing subkey list/tree at offset 0x%.8X.", |
---|
529 | num_keys, ret_val->num_keys, offset); |
---|
530 | } |
---|
531 | |
---|
532 | return ret_val; |
---|
533 | } |
---|
534 | |
---|
535 | |
---|
536 | /****************************************************************************** |
---|
537 | ******************************************************************************/ |
---|
538 | REGFI_SUBKEY_LIST* regfi_load_subkeylist_aux(REGFI_FILE* file, uint32 offset, |
---|
539 | uint32 max_size, bool strict, |
---|
540 | uint8 depth_left) |
---|
541 | { |
---|
542 | REGFI_SUBKEY_LIST* ret_val; |
---|
543 | REGFI_SUBKEY_LIST** sublists; |
---|
544 | const REGFI_HBIN* sublist_hbin; |
---|
545 | uint32 i, num_sublists, off, max_length; |
---|
546 | |
---|
547 | if(depth_left == 0) |
---|
548 | { |
---|
549 | regfi_add_message(file, REGFI_MSG_WARN, "Maximum depth reached" |
---|
550 | " while parsing subkey list/tree at offset 0x%.8X.", |
---|
551 | offset); |
---|
552 | return NULL; |
---|
553 | } |
---|
554 | |
---|
555 | ret_val = regfi_parse_subkeylist(file, offset, max_size, strict); |
---|
556 | if(ret_val == NULL) |
---|
557 | return NULL; |
---|
558 | |
---|
559 | if(ret_val->recursive_type) |
---|
560 | { |
---|
561 | num_sublists = ret_val->num_children; |
---|
562 | sublists = (REGFI_SUBKEY_LIST**)zalloc(num_sublists |
---|
563 | * sizeof(REGFI_SUBKEY_LIST*)); |
---|
564 | for(i=0; i < num_sublists; i++) |
---|
565 | { |
---|
566 | off = ret_val->elements[i].offset + REGFI_REGF_SIZE; |
---|
567 | sublist_hbin = regfi_lookup_hbin(file, ret_val->elements[i].offset); |
---|
568 | if(sublist_hbin == NULL) |
---|
569 | sublists[i] = NULL; |
---|
570 | else |
---|
571 | { |
---|
572 | max_length = sublist_hbin->block_size + sublist_hbin->file_off - off; |
---|
573 | sublists[i] = regfi_load_subkeylist_aux(file, off, max_length, strict, |
---|
574 | depth_left-1); |
---|
575 | } |
---|
576 | } |
---|
577 | free(ret_val); |
---|
578 | |
---|
579 | return regfi_merge_subkeylists(num_sublists, sublists, strict); |
---|
580 | } |
---|
581 | |
---|
582 | return ret_val; |
---|
583 | } |
---|
584 | |
---|
585 | |
---|
586 | /****************************************************************************** |
---|
587 | ******************************************************************************/ |
---|
588 | REGFI_SUBKEY_LIST* regfi_parse_subkeylist(REGFI_FILE* file, uint32 offset, |
---|
589 | uint32 max_size, bool strict) |
---|
590 | { |
---|
591 | REGFI_SUBKEY_LIST* ret_val; |
---|
592 | uint32 i, cell_length, length, elem_size; |
---|
593 | uint8* elements; |
---|
594 | uint8 buf[REGFI_SUBKEY_LIST_MIN_LEN]; |
---|
595 | bool unalloc; |
---|
596 | bool recursive_type; |
---|
597 | |
---|
598 | if(!regfi_parse_cell(file->fd, offset, buf, REGFI_SUBKEY_LIST_MIN_LEN, |
---|
599 | &cell_length, &unalloc)) |
---|
600 | { |
---|
601 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while " |
---|
602 | "parsing subkey-list at offset 0x%.8X.", offset); |
---|
603 | return NULL; |
---|
604 | } |
---|
605 | |
---|
606 | if(cell_length > max_size) |
---|
607 | { |
---|
608 | regfi_add_message(file, REGFI_MSG_WARN, "Cell size longer than max_size" |
---|
609 | " while parsing subkey-list at offset 0x%.8X.", offset); |
---|
610 | if(strict) |
---|
611 | return NULL; |
---|
612 | cell_length = max_size & 0xFFFFFFF8; |
---|
613 | } |
---|
614 | |
---|
615 | recursive_type = false; |
---|
616 | if(buf[0] == 'r' && buf[1] == 'i') |
---|
617 | { |
---|
618 | recursive_type = true; |
---|
619 | elem_size = sizeof(uint32); |
---|
620 | } |
---|
621 | else if(buf[0] == 'l' && buf[1] == 'i') |
---|
622 | elem_size = sizeof(uint32); |
---|
623 | else if((buf[0] == 'l') && (buf[1] == 'f' || buf[1] == 'h')) |
---|
624 | elem_size = sizeof(REGFI_SUBKEY_LIST_ELEM); |
---|
625 | else |
---|
626 | { |
---|
627 | regfi_add_message(file, REGFI_MSG_ERROR, "Unknown magic number" |
---|
628 | " (0x%.2X, 0x%.2X) encountered while parsing" |
---|
629 | " subkey-list at offset 0x%.8X.", buf[0], buf[1], offset); |
---|
630 | return NULL; |
---|
631 | } |
---|
632 | |
---|
633 | ret_val = (REGFI_SUBKEY_LIST*)zalloc(sizeof(REGFI_SUBKEY_LIST)); |
---|
634 | if(ret_val == NULL) |
---|
635 | return NULL; |
---|
636 | |
---|
637 | ret_val->offset = offset; |
---|
638 | ret_val->cell_size = cell_length; |
---|
639 | ret_val->magic[0] = buf[0]; |
---|
640 | ret_val->magic[1] = buf[1]; |
---|
641 | ret_val->recursive_type = recursive_type; |
---|
642 | ret_val->num_children = SVAL(buf, 0x2); |
---|
643 | |
---|
644 | if(!recursive_type) |
---|
645 | ret_val->num_keys = ret_val->num_children; |
---|
646 | |
---|
647 | length = elem_size*ret_val->num_children; |
---|
648 | if(cell_length - REGFI_SUBKEY_LIST_MIN_LEN - sizeof(uint32) < length) |
---|
649 | { |
---|
650 | regfi_add_message(file, REGFI_MSG_WARN, "Number of elements too large for" |
---|
651 | " cell while parsing subkey-list at offset 0x%.8X.", |
---|
652 | offset); |
---|
653 | if(strict) |
---|
654 | { |
---|
655 | free(ret_val); |
---|
656 | return NULL; |
---|
657 | } |
---|
658 | length = cell_length - REGFI_SUBKEY_LIST_MIN_LEN - sizeof(uint32); |
---|
659 | } |
---|
660 | |
---|
661 | ret_val->elements |
---|
662 | = (REGFI_SUBKEY_LIST_ELEM*)zalloc(ret_val->num_children |
---|
663 | * sizeof(REGFI_SUBKEY_LIST_ELEM)); |
---|
664 | if(ret_val->elements == NULL) |
---|
665 | { |
---|
666 | free(ret_val); |
---|
667 | return NULL; |
---|
668 | } |
---|
669 | |
---|
670 | elements = (uint8*)zalloc(length); |
---|
671 | if(elements == NULL) |
---|
672 | { |
---|
673 | free(ret_val->elements); |
---|
674 | free(ret_val); |
---|
675 | return NULL; |
---|
676 | } |
---|
677 | |
---|
678 | if(regfi_read(file->fd, elements, &length) != 0 |
---|
679 | || length != elem_size*ret_val->num_children) |
---|
680 | { |
---|
681 | free(ret_val->elements); |
---|
682 | free(ret_val); |
---|
683 | return NULL; |
---|
684 | } |
---|
685 | |
---|
686 | if(elem_size == sizeof(uint32)) |
---|
687 | { |
---|
688 | for (i=0; i < ret_val->num_children; i++) |
---|
689 | { |
---|
690 | ret_val->elements[i].offset = IVAL(elements, i*elem_size); |
---|
691 | ret_val->elements[i].hash = 0; |
---|
692 | } |
---|
693 | } |
---|
694 | else |
---|
695 | { |
---|
696 | for (i=0; i < ret_val->num_children; i++) |
---|
697 | { |
---|
698 | ret_val->elements[i].offset = IVAL(elements, i*elem_size); |
---|
699 | ret_val->elements[i].hash = IVAL(elements, i*elem_size+4); |
---|
700 | } |
---|
701 | } |
---|
702 | free(elements); |
---|
703 | |
---|
704 | return ret_val; |
---|
705 | } |
---|
706 | |
---|
707 | |
---|
708 | /******************************************************************* |
---|
709 | *******************************************************************/ |
---|
710 | REGFI_SUBKEY_LIST* regfi_merge_subkeylists(uint16 num_lists, |
---|
711 | REGFI_SUBKEY_LIST** lists, |
---|
712 | bool strict) |
---|
713 | { |
---|
714 | uint32 i,j,k; |
---|
715 | REGFI_SUBKEY_LIST* ret_val; |
---|
716 | |
---|
717 | if(lists == NULL) |
---|
718 | return NULL; |
---|
719 | ret_val = (REGFI_SUBKEY_LIST*)zalloc(sizeof(REGFI_SUBKEY_LIST)); |
---|
720 | |
---|
721 | if(ret_val == NULL) |
---|
722 | return NULL; |
---|
723 | |
---|
724 | /* Obtain total number of elements */ |
---|
725 | ret_val->num_keys = 0; |
---|
726 | for(i=0; i < num_lists; i++) |
---|
727 | { |
---|
728 | if(lists[i] != NULL) |
---|
729 | ret_val->num_keys += lists[i]->num_children; |
---|
730 | } |
---|
731 | ret_val->num_children = ret_val->num_keys; |
---|
732 | |
---|
733 | if(ret_val->num_keys > 0) |
---|
734 | { |
---|
735 | ret_val->elements = |
---|
736 | (REGFI_SUBKEY_LIST_ELEM*)zalloc(sizeof(REGFI_SUBKEY_LIST_ELEM) |
---|
737 | * ret_val->num_keys); |
---|
738 | k=0; |
---|
739 | |
---|
740 | if(ret_val->elements != NULL) |
---|
741 | { |
---|
742 | for(i=0; i < num_lists; i++) |
---|
743 | { |
---|
744 | if(lists[i] != NULL) |
---|
745 | { |
---|
746 | for(j=0; j < lists[i]->num_keys; j++) |
---|
747 | { |
---|
748 | ret_val->elements[k].hash=lists[i]->elements[j].hash; |
---|
749 | ret_val->elements[k++].offset=lists[i]->elements[j].offset; |
---|
750 | } |
---|
751 | } |
---|
752 | } |
---|
753 | } |
---|
754 | } |
---|
755 | |
---|
756 | for(i=0; i < num_lists; i++) |
---|
757 | regfi_subkeylist_free(lists[i]); |
---|
758 | free(lists); |
---|
759 | |
---|
760 | return ret_val; |
---|
761 | } |
---|
762 | |
---|
763 | |
---|
764 | /****************************************************************************** |
---|
765 | * |
---|
766 | ******************************************************************************/ |
---|
767 | REGFI_SK_REC* regfi_parse_sk(REGFI_FILE* file, uint32 offset, uint32 max_size, |
---|
768 | bool strict) |
---|
769 | { |
---|
770 | REGFI_SK_REC* ret_val; |
---|
771 | uint8* sec_desc_buf = NULL; |
---|
772 | uint32 cell_length, length; |
---|
773 | uint8 sk_header[REGFI_SK_MIN_LENGTH]; |
---|
774 | bool unalloc = false; |
---|
775 | |
---|
776 | if(!regfi_parse_cell(file->fd, offset, sk_header, REGFI_SK_MIN_LENGTH, |
---|
777 | &cell_length, &unalloc)) |
---|
778 | { |
---|
779 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse SK record cell" |
---|
780 | " at offset 0x%.8X.", offset); |
---|
781 | return NULL; |
---|
782 | } |
---|
783 | |
---|
784 | if(sk_header[0] != 's' || sk_header[1] != 'k') |
---|
785 | { |
---|
786 | regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch in parsing" |
---|
787 | " SK record at offset 0x%.8X.", offset); |
---|
788 | return NULL; |
---|
789 | } |
---|
790 | |
---|
791 | /* ret_val = (REGFI_SK_REC*)zalloc(sizeof(REGFI_SK_REC));*/ |
---|
792 | ret_val = talloc(NULL, REGFI_SK_REC); |
---|
793 | if(ret_val == NULL) |
---|
794 | return NULL; |
---|
795 | |
---|
796 | ret_val->offset = offset; |
---|
797 | /* XXX: Is there a way to be more conservative (shorter) with |
---|
798 | * cell length when cell is unallocated? |
---|
799 | */ |
---|
800 | ret_val->cell_size = cell_length; |
---|
801 | |
---|
802 | if(ret_val->cell_size > max_size) |
---|
803 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
804 | if((ret_val->cell_size < REGFI_SK_MIN_LENGTH) |
---|
805 | || (strict && ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8))) |
---|
806 | { |
---|
807 | regfi_add_message(file, REGFI_MSG_WARN, "Invalid cell size found while" |
---|
808 | " parsing SK record at offset 0x%.8X.", offset); |
---|
809 | goto fail; |
---|
810 | } |
---|
811 | |
---|
812 | ret_val->magic[0] = sk_header[0]; |
---|
813 | ret_val->magic[1] = sk_header[1]; |
---|
814 | |
---|
815 | ret_val->unknown_tag = SVAL(sk_header, 0x2); |
---|
816 | ret_val->prev_sk_off = IVAL(sk_header, 0x4); |
---|
817 | ret_val->next_sk_off = IVAL(sk_header, 0x8); |
---|
818 | ret_val->ref_count = IVAL(sk_header, 0xC); |
---|
819 | ret_val->desc_size = IVAL(sk_header, 0x10); |
---|
820 | |
---|
821 | if(ret_val->prev_sk_off != (ret_val->prev_sk_off & 0xFFFFFFF8) |
---|
822 | || ret_val->next_sk_off != (ret_val->next_sk_off & 0xFFFFFFF8)) |
---|
823 | { |
---|
824 | regfi_add_message(file, REGFI_MSG_WARN, "SK record's next/previous offsets" |
---|
825 | " are not a multiple of 8 while parsing SK record at" |
---|
826 | " offset 0x%.8X.", offset); |
---|
827 | goto fail; |
---|
828 | } |
---|
829 | |
---|
830 | if(ret_val->desc_size + REGFI_SK_MIN_LENGTH > ret_val->cell_size) |
---|
831 | { |
---|
832 | regfi_add_message(file, REGFI_MSG_WARN, "Security descriptor too large for" |
---|
833 | " cell while parsing SK record at offset 0x%.8X.", |
---|
834 | offset); |
---|
835 | goto fail; |
---|
836 | } |
---|
837 | |
---|
838 | sec_desc_buf = (uint8*)malloc(ret_val->desc_size); |
---|
839 | if(sec_desc_buf == NULL) |
---|
840 | goto fail; |
---|
841 | |
---|
842 | length = ret_val->desc_size; |
---|
843 | if(regfi_read(file->fd, sec_desc_buf, &length) != 0 |
---|
844 | || length != ret_val->desc_size) |
---|
845 | { |
---|
846 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read security" |
---|
847 | " descriptor while parsing SK record at offset 0x%.8X.", |
---|
848 | offset); |
---|
849 | goto fail; |
---|
850 | } |
---|
851 | |
---|
852 | if(!(ret_val->sec_desc = winsec_parse_desc(ret_val, sec_desc_buf, |
---|
853 | ret_val->desc_size))) |
---|
854 | { |
---|
855 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to parse security" |
---|
856 | " descriptor while parsing SK record at offset 0x%.8X.", |
---|
857 | offset); |
---|
858 | goto fail; |
---|
859 | } |
---|
860 | |
---|
861 | free(sec_desc_buf); |
---|
862 | return ret_val; |
---|
863 | |
---|
864 | fail: |
---|
865 | if(sec_desc_buf != NULL) |
---|
866 | free(sec_desc_buf); |
---|
867 | talloc_free(ret_val); |
---|
868 | return NULL; |
---|
869 | } |
---|
870 | |
---|
871 | |
---|
872 | REGFI_VALUE_LIST* regfi_parse_valuelist(REGFI_FILE* file, uint32 offset, |
---|
873 | uint32 num_values, bool strict) |
---|
874 | { |
---|
875 | REGFI_VALUE_LIST* ret_val; |
---|
876 | uint32 i, cell_length, length, read_len; |
---|
877 | bool unalloc; |
---|
878 | |
---|
879 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, &cell_length, &unalloc)) |
---|
880 | { |
---|
881 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read cell header" |
---|
882 | " while parsing value list at offset 0x%.8X.", offset); |
---|
883 | return NULL; |
---|
884 | } |
---|
885 | |
---|
886 | if(cell_length != (cell_length & 0xFFFFFFF8)) |
---|
887 | { |
---|
888 | regfi_add_message(file, REGFI_MSG_WARN, "Cell length not a multiple of 8" |
---|
889 | " while parsing value list at offset 0x%.8X.", offset); |
---|
890 | if(strict) |
---|
891 | return NULL; |
---|
892 | cell_length = cell_length & 0xFFFFFFF8; |
---|
893 | } |
---|
894 | |
---|
895 | if((num_values * sizeof(uint32)) > cell_length-sizeof(uint32)) |
---|
896 | { |
---|
897 | regfi_add_message(file, REGFI_MSG_WARN, "Too many values found" |
---|
898 | " while parsing value list at offset 0x%.8X.", offset); |
---|
899 | if(strict) |
---|
900 | return NULL; |
---|
901 | num_values = cell_length/sizeof(uint32) - sizeof(uint32); |
---|
902 | } |
---|
903 | |
---|
904 | read_len = num_values*sizeof(uint32); |
---|
905 | ret_val = (REGFI_VALUE_LIST*)malloc(sizeof(REGFI_VALUE_LIST)); |
---|
906 | if(ret_val == NULL) |
---|
907 | return NULL; |
---|
908 | |
---|
909 | ret_val->elements = (REGFI_VALUE_LIST_ELEM*)malloc(read_len); |
---|
910 | if(ret_val->elements == NULL) |
---|
911 | { |
---|
912 | free(ret_val); |
---|
913 | return NULL; |
---|
914 | } |
---|
915 | ret_val->num_values = num_values; |
---|
916 | |
---|
917 | length = read_len; |
---|
918 | if((regfi_read(file->fd, (uint8*)ret_val->elements, &length) != 0) |
---|
919 | || length != read_len) |
---|
920 | { |
---|
921 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read value pointers" |
---|
922 | " while parsing value list at offset 0x%.8X.", offset); |
---|
923 | free(ret_val->elements); |
---|
924 | free(ret_val); |
---|
925 | return NULL; |
---|
926 | } |
---|
927 | |
---|
928 | for(i=0; i < num_values; i++) |
---|
929 | { |
---|
930 | /* Fix endianness */ |
---|
931 | ret_val->elements[i] = IVAL(&ret_val->elements[i], 0); |
---|
932 | |
---|
933 | /* Validate the first num_values values to ensure they make sense */ |
---|
934 | if(strict) |
---|
935 | { |
---|
936 | /* XXX: Need to revisit this file length check when we start dealing |
---|
937 | * with partial files. */ |
---|
938 | if((ret_val->elements[i] + REGFI_REGF_SIZE > file->file_length) |
---|
939 | || ((ret_val->elements[i] & 0xFFFFFFF8) != ret_val->elements[i])) |
---|
940 | { |
---|
941 | regfi_add_message(file, REGFI_MSG_WARN, "Invalid value pointer" |
---|
942 | " (0x%.8X) found while parsing value list at offset" |
---|
943 | " 0x%.8X.", ret_val->elements[i], offset); |
---|
944 | free(ret_val->elements); |
---|
945 | free(ret_val); |
---|
946 | return NULL; |
---|
947 | } |
---|
948 | } |
---|
949 | } |
---|
950 | |
---|
951 | return ret_val; |
---|
952 | } |
---|
953 | |
---|
954 | |
---|
955 | |
---|
956 | /****************************************************************************** |
---|
957 | ******************************************************************************/ |
---|
958 | REGFI_VK_REC* regfi_load_value(REGFI_FILE* file, uint32 offset, |
---|
959 | bool strict) |
---|
960 | { |
---|
961 | REGFI_VK_REC* ret_val = NULL; |
---|
962 | const REGFI_HBIN* hbin; |
---|
963 | uint32 data_offset, data_maxsize; |
---|
964 | |
---|
965 | hbin = regfi_lookup_hbin(file, offset - REGFI_REGF_SIZE); |
---|
966 | if(!hbin) |
---|
967 | return NULL; |
---|
968 | |
---|
969 | ret_val = regfi_parse_vk(file, offset, |
---|
970 | hbin->block_size + hbin->file_off - offset, strict); |
---|
971 | |
---|
972 | if(ret_val == NULL) |
---|
973 | return NULL; |
---|
974 | |
---|
975 | if(ret_val->data_size == 0) |
---|
976 | ret_val->data = NULL; |
---|
977 | else |
---|
978 | { |
---|
979 | if(ret_val->data_in_offset) |
---|
980 | { |
---|
981 | ret_val->data = regfi_parse_data(file, ret_val->type, ret_val->data_off, |
---|
982 | ret_val->data_size, 4, |
---|
983 | ret_val->data_in_offset, strict); |
---|
984 | } |
---|
985 | else |
---|
986 | { |
---|
987 | hbin = regfi_lookup_hbin(file, ret_val->data_off); |
---|
988 | if(hbin) |
---|
989 | { |
---|
990 | data_offset = ret_val->data_off+REGFI_REGF_SIZE; |
---|
991 | data_maxsize = hbin->block_size + hbin->file_off - data_offset; |
---|
992 | ret_val->data = regfi_parse_data(file, ret_val->type, data_offset, |
---|
993 | ret_val->data_size, data_maxsize, |
---|
994 | ret_val->data_in_offset, strict); |
---|
995 | } |
---|
996 | else |
---|
997 | { |
---|
998 | regfi_add_message(file, REGFI_MSG_WARN, "Could not find HBIN for data" |
---|
999 | " while parsing VK record at offset 0x%.8X.", |
---|
1000 | ret_val->offset); |
---|
1001 | ret_val->data = NULL; |
---|
1002 | } |
---|
1003 | } |
---|
1004 | |
---|
1005 | if(ret_val->data == NULL) |
---|
1006 | { |
---|
1007 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse data record" |
---|
1008 | " while parsing VK record at offset 0x%.8X.", |
---|
1009 | ret_val->offset); |
---|
1010 | } |
---|
1011 | } |
---|
1012 | |
---|
1013 | return ret_val; |
---|
1014 | } |
---|
1015 | |
---|
1016 | |
---|
1017 | /****************************************************************************** |
---|
1018 | * If !strict, the list may contain NULLs, VK records may point to NULL. |
---|
1019 | ******************************************************************************/ |
---|
1020 | REGFI_VALUE_LIST* regfi_load_valuelist(REGFI_FILE* file, uint32 offset, |
---|
1021 | uint32 num_values, uint32 max_size, |
---|
1022 | bool strict) |
---|
1023 | { |
---|
1024 | uint32 usable_num_values; |
---|
1025 | |
---|
1026 | if((num_values+1) * sizeof(uint32) > max_size) |
---|
1027 | { |
---|
1028 | regfi_add_message(file, REGFI_MSG_WARN, "Number of values indicated by" |
---|
1029 | " parent key (%d) would cause cell to straddle HBIN" |
---|
1030 | " boundary while loading value list at offset" |
---|
1031 | " 0x%.8X.", num_values, offset); |
---|
1032 | if(strict) |
---|
1033 | return NULL; |
---|
1034 | usable_num_values = max_size/sizeof(uint32) - sizeof(uint32); |
---|
1035 | } |
---|
1036 | else |
---|
1037 | usable_num_values = num_values; |
---|
1038 | |
---|
1039 | return regfi_parse_valuelist(file, offset, usable_num_values, strict); |
---|
1040 | } |
---|
1041 | |
---|
1042 | |
---|
1043 | |
---|
1044 | /****************************************************************************** |
---|
1045 | * |
---|
1046 | ******************************************************************************/ |
---|
1047 | REGFI_NK_REC* regfi_load_key(REGFI_FILE* file, uint32 offset, bool strict) |
---|
1048 | { |
---|
1049 | const REGFI_HBIN* hbin; |
---|
1050 | const REGFI_HBIN* sub_hbin; |
---|
1051 | REGFI_NK_REC* nk; |
---|
1052 | uint32 max_length, off; |
---|
1053 | |
---|
1054 | hbin = regfi_lookup_hbin(file, offset-REGFI_REGF_SIZE); |
---|
1055 | if (hbin == NULL) |
---|
1056 | return NULL; |
---|
1057 | |
---|
1058 | /* get the initial nk record */ |
---|
1059 | max_length = hbin->block_size + hbin->file_off - offset; |
---|
1060 | if((nk = regfi_parse_nk(file, offset, max_length, true)) == NULL) |
---|
1061 | { |
---|
1062 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not load NK record at" |
---|
1063 | " offset 0x%.8X.", offset); |
---|
1064 | return NULL; |
---|
1065 | } |
---|
1066 | |
---|
1067 | /* get value list */ |
---|
1068 | if(nk->num_values && (nk->values_off!=REGFI_OFFSET_NONE)) |
---|
1069 | { |
---|
1070 | sub_hbin = hbin; |
---|
1071 | if(!regfi_offset_in_hbin(hbin, nk->values_off)) |
---|
1072 | sub_hbin = regfi_lookup_hbin(file, nk->values_off); |
---|
1073 | |
---|
1074 | if(sub_hbin == NULL) |
---|
1075 | { |
---|
1076 | if(strict) |
---|
1077 | { |
---|
1078 | free(nk); |
---|
1079 | return NULL; |
---|
1080 | } |
---|
1081 | else |
---|
1082 | nk->values = NULL; |
---|
1083 | |
---|
1084 | } |
---|
1085 | else |
---|
1086 | { |
---|
1087 | off = nk->values_off + REGFI_REGF_SIZE; |
---|
1088 | max_length = sub_hbin->block_size + sub_hbin->file_off - off; |
---|
1089 | nk->values = regfi_load_valuelist(file, off, nk->num_values, max_length, |
---|
1090 | true); |
---|
1091 | if(nk->values == NULL) |
---|
1092 | { |
---|
1093 | regfi_add_message(file, REGFI_MSG_WARN, "Could not load value list" |
---|
1094 | " for NK record at offset 0x%.8X.", offset); |
---|
1095 | if(strict) |
---|
1096 | { |
---|
1097 | free(nk); |
---|
1098 | return NULL; |
---|
1099 | } |
---|
1100 | } |
---|
1101 | } |
---|
1102 | } |
---|
1103 | |
---|
1104 | /* now get subkey list */ |
---|
1105 | if(nk->num_subkeys && (nk->subkeys_off != REGFI_OFFSET_NONE)) |
---|
1106 | { |
---|
1107 | sub_hbin = hbin; |
---|
1108 | if(!regfi_offset_in_hbin(hbin, nk->subkeys_off)) |
---|
1109 | sub_hbin = regfi_lookup_hbin(file, nk->subkeys_off); |
---|
1110 | |
---|
1111 | if (sub_hbin == NULL) |
---|
1112 | { |
---|
1113 | if(strict) |
---|
1114 | { |
---|
1115 | regfi_key_free(nk); |
---|
1116 | return NULL; |
---|
1117 | } |
---|
1118 | else |
---|
1119 | nk->subkeys = NULL; |
---|
1120 | } |
---|
1121 | else |
---|
1122 | { |
---|
1123 | off = nk->subkeys_off + REGFI_REGF_SIZE; |
---|
1124 | max_length = sub_hbin->block_size + sub_hbin->file_off - off; |
---|
1125 | nk->subkeys = regfi_load_subkeylist(file, off, nk->num_subkeys, |
---|
1126 | max_length, true); |
---|
1127 | |
---|
1128 | if(nk->subkeys == NULL) |
---|
1129 | { |
---|
1130 | regfi_add_message(file, REGFI_MSG_WARN, "Could not load subkey list" |
---|
1131 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
1132 | nk->num_subkeys = 0; |
---|
1133 | } |
---|
1134 | } |
---|
1135 | } |
---|
1136 | |
---|
1137 | return nk; |
---|
1138 | } |
---|
1139 | |
---|
1140 | |
---|
1141 | /****************************************************************************** |
---|
1142 | ******************************************************************************/ |
---|
1143 | const REGFI_SK_REC* regfi_load_sk(REGFI_FILE* file, uint32 offset, bool strict) |
---|
1144 | { |
---|
1145 | REGFI_SK_REC* ret_val = NULL; |
---|
1146 | const REGFI_HBIN* hbin; |
---|
1147 | uint32 max_length; |
---|
1148 | void* failure_ptr = NULL; |
---|
1149 | |
---|
1150 | /* First look if we have already parsed it */ |
---|
1151 | ret_val = (REGFI_SK_REC*)lru_cache_find(file->sk_cache, &offset, 4); |
---|
1152 | |
---|
1153 | /* Bail out if we have previously cached a parse failure at this offset. */ |
---|
1154 | if(ret_val == (void*)REGFI_OFFSET_NONE) |
---|
1155 | return NULL; |
---|
1156 | |
---|
1157 | if(ret_val == NULL) |
---|
1158 | { |
---|
1159 | hbin = regfi_lookup_hbin(file, offset - REGFI_REGF_SIZE); |
---|
1160 | if(hbin == NULL) |
---|
1161 | return NULL; |
---|
1162 | |
---|
1163 | max_length = hbin->block_size + hbin->file_off - offset; |
---|
1164 | ret_val = regfi_parse_sk(file, offset, max_length, strict); |
---|
1165 | if(ret_val == NULL) |
---|
1166 | { /* Cache the parse failure and bail out. */ |
---|
1167 | failure_ptr = talloc(NULL, uint32_t); |
---|
1168 | if(failure_ptr == NULL) |
---|
1169 | return NULL; |
---|
1170 | *(uint32_t*)failure_ptr = REGFI_OFFSET_NONE; |
---|
1171 | lru_cache_update(file->sk_cache, &offset, 4, failure_ptr); |
---|
1172 | return NULL; |
---|
1173 | } |
---|
1174 | |
---|
1175 | lru_cache_update(file->sk_cache, &offset, 4, ret_val); |
---|
1176 | } |
---|
1177 | |
---|
1178 | return ret_val; |
---|
1179 | } |
---|
1180 | |
---|
1181 | |
---|
1182 | |
---|
1183 | /****************************************************************************** |
---|
1184 | ******************************************************************************/ |
---|
1185 | static bool regfi_find_root_nk(REGFI_FILE* file, uint32 offset,uint32 hbin_size, |
---|
1186 | uint32* root_offset) |
---|
1187 | { |
---|
1188 | uint8 tmp[4]; |
---|
1189 | int32 record_size; |
---|
1190 | uint32 length, hbin_offset = 0; |
---|
1191 | REGFI_NK_REC* nk = NULL; |
---|
1192 | bool found = false; |
---|
1193 | |
---|
1194 | for(record_size=0; !found && (hbin_offset < hbin_size); ) |
---|
1195 | { |
---|
1196 | if(lseek(file->fd, offset+hbin_offset, SEEK_SET) == -1) |
---|
1197 | return false; |
---|
1198 | |
---|
1199 | length = 4; |
---|
1200 | if((regfi_read(file->fd, tmp, &length) != 0) || length != 4) |
---|
1201 | return false; |
---|
1202 | record_size = IVALS(tmp, 0); |
---|
1203 | |
---|
1204 | if(record_size < 0) |
---|
1205 | { |
---|
1206 | record_size = record_size*(-1); |
---|
1207 | nk = regfi_parse_nk(file, offset+hbin_offset, hbin_size-hbin_offset, true); |
---|
1208 | if(nk != NULL) |
---|
1209 | { |
---|
1210 | if((nk->key_type == REGFI_NK_TYPE_ROOTKEY1) |
---|
1211 | || (nk->key_type == REGFI_NK_TYPE_ROOTKEY2)) |
---|
1212 | { |
---|
1213 | found = true; |
---|
1214 | *root_offset = nk->offset; |
---|
1215 | } |
---|
1216 | free(nk); |
---|
1217 | } |
---|
1218 | } |
---|
1219 | |
---|
1220 | hbin_offset += record_size; |
---|
1221 | } |
---|
1222 | |
---|
1223 | return found; |
---|
1224 | } |
---|
1225 | |
---|
1226 | |
---|
1227 | /******************************************************************* |
---|
1228 | * Open the registry file and then read in the REGF block to get the |
---|
1229 | * first hbin offset. |
---|
1230 | *******************************************************************/ |
---|
1231 | REGFI_FILE* regfi_open(const char* filename) |
---|
1232 | { |
---|
1233 | struct stat sbuf; |
---|
1234 | REGFI_FILE* rb; |
---|
1235 | REGFI_HBIN* hbin = NULL; |
---|
1236 | uint32 hbin_off, file_length, cache_secret; |
---|
1237 | int fd; |
---|
1238 | bool rla; |
---|
1239 | |
---|
1240 | /* open an existing file */ |
---|
1241 | if ((fd = open(filename, REGFI_OPEN_FLAGS)) == -1) |
---|
1242 | { |
---|
1243 | /* fprintf(stderr, "regfi_open: failure to open %s (%s)\n", filename, strerror(errno));*/ |
---|
1244 | return NULL; |
---|
1245 | } |
---|
1246 | |
---|
1247 | /* Determine file length. Must be at least big enough |
---|
1248 | * for the header and one hbin. |
---|
1249 | */ |
---|
1250 | if (fstat(fd, &sbuf) == -1) |
---|
1251 | return NULL; |
---|
1252 | file_length = sbuf.st_size; |
---|
1253 | if(file_length < REGFI_REGF_SIZE+REGFI_HBIN_ALLOC) |
---|
1254 | return NULL; |
---|
1255 | |
---|
1256 | /* read in an existing file */ |
---|
1257 | if ((rb = regfi_parse_regf(fd, true)) == NULL) |
---|
1258 | { |
---|
1259 | /* fprintf(stderr, "regfi_open: Failed to read initial REGF block\n"); */ |
---|
1260 | close(fd); |
---|
1261 | return NULL; |
---|
1262 | } |
---|
1263 | rb->file_length = file_length; |
---|
1264 | |
---|
1265 | rb->hbins = range_list_new(); |
---|
1266 | if(rb->hbins == NULL) |
---|
1267 | { |
---|
1268 | /* fprintf(stderr, "regfi_open: Failed to create HBIN list.\n"); */ |
---|
1269 | range_list_free(rb->hbins); |
---|
1270 | close(fd); |
---|
1271 | free(rb); |
---|
1272 | return NULL; |
---|
1273 | } |
---|
1274 | |
---|
1275 | rla = true; |
---|
1276 | hbin_off = REGFI_REGF_SIZE; |
---|
1277 | hbin = regfi_parse_hbin(rb, hbin_off, true); |
---|
1278 | while(hbin && rla) |
---|
1279 | { |
---|
1280 | rla = range_list_add(rb->hbins, hbin->file_off, hbin->block_size, hbin); |
---|
1281 | if(rla) |
---|
1282 | talloc_steal(rb->hbins, hbin); |
---|
1283 | hbin_off = hbin->file_off + hbin->block_size; |
---|
1284 | hbin = regfi_parse_hbin(rb, hbin_off, true); |
---|
1285 | } |
---|
1286 | |
---|
1287 | /* This secret isn't very secret, but we don't need a good one. This |
---|
1288 | * secret is just designed to prevent someone from trying to blow our |
---|
1289 | * caching and make things slow. |
---|
1290 | */ |
---|
1291 | cache_secret = 0x15DEAD05^time(NULL)^(getpid()<<16); |
---|
1292 | |
---|
1293 | /* Cache an unlimited number of SK records. Typically there are very few. */ |
---|
1294 | rb->sk_cache = lru_cache_create_ctx(NULL, 0, cache_secret, true); |
---|
1295 | |
---|
1296 | /* Default message mask */ |
---|
1297 | rb->msg_mask = REGFI_MSG_ERROR|REGFI_MSG_WARN; |
---|
1298 | |
---|
1299 | /* success */ |
---|
1300 | return rb; |
---|
1301 | } |
---|
1302 | |
---|
1303 | |
---|
1304 | /****************************************************************************** |
---|
1305 | ******************************************************************************/ |
---|
1306 | int regfi_close(REGFI_FILE *file) |
---|
1307 | { |
---|
1308 | int fd; |
---|
1309 | |
---|
1310 | /* nothing to do if there is no open file */ |
---|
1311 | if ((file == NULL) || (file->fd == -1)) |
---|
1312 | return 0; |
---|
1313 | |
---|
1314 | fd = file->fd; |
---|
1315 | file->fd = -1; |
---|
1316 | |
---|
1317 | range_list_free(file->hbins); |
---|
1318 | |
---|
1319 | if(file->sk_cache != NULL) |
---|
1320 | lru_cache_destroy(file->sk_cache); |
---|
1321 | |
---|
1322 | free(file); |
---|
1323 | return close(fd); |
---|
1324 | } |
---|
1325 | |
---|
1326 | |
---|
1327 | /****************************************************************************** |
---|
1328 | * There should be only *one* root key in the registry file based |
---|
1329 | * on my experience. --jerry |
---|
1330 | ******************************************************************************/ |
---|
1331 | REGFI_NK_REC* regfi_rootkey(REGFI_FILE *file) |
---|
1332 | { |
---|
1333 | REGFI_NK_REC* nk = NULL; |
---|
1334 | REGFI_HBIN* hbin; |
---|
1335 | uint32 root_offset, i, num_hbins; |
---|
1336 | |
---|
1337 | if(!file) |
---|
1338 | return NULL; |
---|
1339 | |
---|
1340 | /* Scan through the file one HBIN block at a time looking |
---|
1341 | * for an NK record with a root key type. |
---|
1342 | * This is typically the first NK record in the first HBIN |
---|
1343 | * block (but we're not assuming that generally). |
---|
1344 | */ |
---|
1345 | num_hbins = range_list_size(file->hbins); |
---|
1346 | for(i=0; i < num_hbins; i++) |
---|
1347 | { |
---|
1348 | hbin = (REGFI_HBIN*)range_list_get(file->hbins, i)->data; |
---|
1349 | if(regfi_find_root_nk(file, hbin->file_off+REGFI_HBIN_HEADER_SIZE, |
---|
1350 | hbin->block_size-REGFI_HBIN_HEADER_SIZE, &root_offset)) |
---|
1351 | { |
---|
1352 | nk = regfi_load_key(file, root_offset, true); |
---|
1353 | break; |
---|
1354 | } |
---|
1355 | } |
---|
1356 | |
---|
1357 | return nk; |
---|
1358 | } |
---|
1359 | |
---|
1360 | |
---|
1361 | /****************************************************************************** |
---|
1362 | *****************************************************************************/ |
---|
1363 | void regfi_key_free(REGFI_NK_REC* nk) |
---|
1364 | { |
---|
1365 | if((nk->values != NULL) && (nk->values_off!=REGFI_OFFSET_NONE)) |
---|
1366 | { |
---|
1367 | if(nk->values->elements != NULL) |
---|
1368 | free(nk->values->elements); |
---|
1369 | free(nk->values); |
---|
1370 | } |
---|
1371 | |
---|
1372 | regfi_subkeylist_free(nk->subkeys); |
---|
1373 | |
---|
1374 | if(nk->keyname != NULL) |
---|
1375 | free(nk->keyname); |
---|
1376 | if(nk->classname != NULL) |
---|
1377 | free(nk->classname); |
---|
1378 | |
---|
1379 | /* XXX: not freeing sec_desc because these are cached. This needs to be reviewed. */ |
---|
1380 | free(nk); |
---|
1381 | } |
---|
1382 | |
---|
1383 | |
---|
1384 | /****************************************************************************** |
---|
1385 | *****************************************************************************/ |
---|
1386 | void regfi_subkeylist_free(REGFI_SUBKEY_LIST* list) |
---|
1387 | { |
---|
1388 | if(list != NULL) |
---|
1389 | { |
---|
1390 | free(list->elements); |
---|
1391 | free(list); |
---|
1392 | } |
---|
1393 | } |
---|
1394 | |
---|
1395 | |
---|
1396 | /****************************************************************************** |
---|
1397 | *****************************************************************************/ |
---|
1398 | REGFI_ITERATOR* regfi_iterator_new(REGFI_FILE* fh) |
---|
1399 | { |
---|
1400 | REGFI_NK_REC* root; |
---|
1401 | REGFI_ITERATOR* ret_val = (REGFI_ITERATOR*)malloc(sizeof(REGFI_ITERATOR)); |
---|
1402 | if(ret_val == NULL) |
---|
1403 | return NULL; |
---|
1404 | |
---|
1405 | root = regfi_rootkey(fh); |
---|
1406 | if(root == NULL) |
---|
1407 | { |
---|
1408 | free(ret_val); |
---|
1409 | return NULL; |
---|
1410 | } |
---|
1411 | |
---|
1412 | ret_val->key_positions = void_stack_new(REGFI_MAX_DEPTH); |
---|
1413 | if(ret_val->key_positions == NULL) |
---|
1414 | { |
---|
1415 | free(ret_val); |
---|
1416 | return NULL; |
---|
1417 | } |
---|
1418 | |
---|
1419 | ret_val->f = fh; |
---|
1420 | ret_val->cur_key = root; |
---|
1421 | ret_val->cur_subkey = 0; |
---|
1422 | ret_val->cur_value = 0; |
---|
1423 | |
---|
1424 | return ret_val; |
---|
1425 | } |
---|
1426 | |
---|
1427 | |
---|
1428 | /****************************************************************************** |
---|
1429 | *****************************************************************************/ |
---|
1430 | void regfi_iterator_free(REGFI_ITERATOR* i) |
---|
1431 | { |
---|
1432 | REGFI_ITER_POSITION* cur; |
---|
1433 | |
---|
1434 | if(i->cur_key != NULL) |
---|
1435 | regfi_key_free(i->cur_key); |
---|
1436 | |
---|
1437 | while((cur = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions)) != NULL) |
---|
1438 | { |
---|
1439 | regfi_key_free(cur->nk); |
---|
1440 | free(cur); |
---|
1441 | } |
---|
1442 | |
---|
1443 | void_stack_free(i->key_positions); |
---|
1444 | free(i); |
---|
1445 | } |
---|
1446 | |
---|
1447 | |
---|
1448 | |
---|
1449 | /****************************************************************************** |
---|
1450 | *****************************************************************************/ |
---|
1451 | /* XXX: some way of indicating reason for failure should be added. */ |
---|
1452 | bool regfi_iterator_down(REGFI_ITERATOR* i) |
---|
1453 | { |
---|
1454 | REGFI_NK_REC* subkey; |
---|
1455 | REGFI_ITER_POSITION* pos; |
---|
1456 | |
---|
1457 | pos = (REGFI_ITER_POSITION*)malloc(sizeof(REGFI_ITER_POSITION)); |
---|
1458 | if(pos == NULL) |
---|
1459 | return false; |
---|
1460 | |
---|
1461 | subkey = (REGFI_NK_REC*)regfi_iterator_cur_subkey(i); |
---|
1462 | if(subkey == NULL) |
---|
1463 | { |
---|
1464 | free(pos); |
---|
1465 | return false; |
---|
1466 | } |
---|
1467 | |
---|
1468 | pos->nk = i->cur_key; |
---|
1469 | pos->cur_subkey = i->cur_subkey; |
---|
1470 | if(!void_stack_push(i->key_positions, pos)) |
---|
1471 | { |
---|
1472 | free(pos); |
---|
1473 | regfi_key_free(subkey); |
---|
1474 | return false; |
---|
1475 | } |
---|
1476 | |
---|
1477 | i->cur_key = subkey; |
---|
1478 | i->cur_subkey = 0; |
---|
1479 | i->cur_value = 0; |
---|
1480 | |
---|
1481 | return true; |
---|
1482 | } |
---|
1483 | |
---|
1484 | |
---|
1485 | /****************************************************************************** |
---|
1486 | *****************************************************************************/ |
---|
1487 | bool regfi_iterator_up(REGFI_ITERATOR* i) |
---|
1488 | { |
---|
1489 | REGFI_ITER_POSITION* pos; |
---|
1490 | |
---|
1491 | pos = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions); |
---|
1492 | if(pos == NULL) |
---|
1493 | return false; |
---|
1494 | |
---|
1495 | regfi_key_free(i->cur_key); |
---|
1496 | i->cur_key = pos->nk; |
---|
1497 | i->cur_subkey = pos->cur_subkey; |
---|
1498 | i->cur_value = 0; |
---|
1499 | free(pos); |
---|
1500 | |
---|
1501 | return true; |
---|
1502 | } |
---|
1503 | |
---|
1504 | |
---|
1505 | /****************************************************************************** |
---|
1506 | *****************************************************************************/ |
---|
1507 | bool regfi_iterator_to_root(REGFI_ITERATOR* i) |
---|
1508 | { |
---|
1509 | while(regfi_iterator_up(i)) |
---|
1510 | continue; |
---|
1511 | |
---|
1512 | return true; |
---|
1513 | } |
---|
1514 | |
---|
1515 | |
---|
1516 | /****************************************************************************** |
---|
1517 | *****************************************************************************/ |
---|
1518 | bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* subkey_name) |
---|
1519 | { |
---|
1520 | REGFI_NK_REC* subkey; |
---|
1521 | bool found = false; |
---|
1522 | uint32 old_subkey = i->cur_subkey; |
---|
1523 | |
---|
1524 | if(subkey_name == NULL) |
---|
1525 | return false; |
---|
1526 | |
---|
1527 | /* XXX: this alloc/free of each sub key might be a bit excessive */ |
---|
1528 | subkey = (REGFI_NK_REC*)regfi_iterator_first_subkey(i); |
---|
1529 | while((subkey != NULL) && (found == false)) |
---|
1530 | { |
---|
1531 | if(subkey->keyname != NULL |
---|
1532 | && strcasecmp(subkey->keyname, subkey_name) == 0) |
---|
1533 | found = true; |
---|
1534 | else |
---|
1535 | { |
---|
1536 | regfi_key_free(subkey); |
---|
1537 | subkey = (REGFI_NK_REC*)regfi_iterator_next_subkey(i); |
---|
1538 | } |
---|
1539 | } |
---|
1540 | |
---|
1541 | if(found == false) |
---|
1542 | { |
---|
1543 | i->cur_subkey = old_subkey; |
---|
1544 | return false; |
---|
1545 | } |
---|
1546 | |
---|
1547 | regfi_key_free(subkey); |
---|
1548 | return true; |
---|
1549 | } |
---|
1550 | |
---|
1551 | |
---|
1552 | /****************************************************************************** |
---|
1553 | *****************************************************************************/ |
---|
1554 | bool regfi_iterator_walk_path(REGFI_ITERATOR* i, const char** path) |
---|
1555 | { |
---|
1556 | uint32 x; |
---|
1557 | if(path == NULL) |
---|
1558 | return false; |
---|
1559 | |
---|
1560 | for(x=0; |
---|
1561 | ((path[x] != NULL) && regfi_iterator_find_subkey(i, path[x]) |
---|
1562 | && regfi_iterator_down(i)); |
---|
1563 | x++) |
---|
1564 | { continue; } |
---|
1565 | |
---|
1566 | if(path[x] == NULL) |
---|
1567 | return true; |
---|
1568 | |
---|
1569 | /* XXX: is this the right number of times? */ |
---|
1570 | for(; x > 0; x--) |
---|
1571 | regfi_iterator_up(i); |
---|
1572 | |
---|
1573 | return false; |
---|
1574 | } |
---|
1575 | |
---|
1576 | |
---|
1577 | /****************************************************************************** |
---|
1578 | *****************************************************************************/ |
---|
1579 | const REGFI_NK_REC* regfi_iterator_cur_key(REGFI_ITERATOR* i) |
---|
1580 | { |
---|
1581 | return i->cur_key; |
---|
1582 | } |
---|
1583 | |
---|
1584 | |
---|
1585 | /****************************************************************************** |
---|
1586 | *****************************************************************************/ |
---|
1587 | const REGFI_SK_REC* regfi_iterator_cur_sk(REGFI_ITERATOR* i) |
---|
1588 | { |
---|
1589 | if(i->cur_key == NULL || i->cur_key->sk_off == REGFI_OFFSET_NONE) |
---|
1590 | return NULL; |
---|
1591 | |
---|
1592 | return regfi_load_sk(i->f, i->cur_key->sk_off + REGFI_REGF_SIZE, true); |
---|
1593 | } |
---|
1594 | |
---|
1595 | |
---|
1596 | /****************************************************************************** |
---|
1597 | *****************************************************************************/ |
---|
1598 | const REGFI_NK_REC* regfi_iterator_first_subkey(REGFI_ITERATOR* i) |
---|
1599 | { |
---|
1600 | i->cur_subkey = 0; |
---|
1601 | return regfi_iterator_cur_subkey(i); |
---|
1602 | } |
---|
1603 | |
---|
1604 | |
---|
1605 | /****************************************************************************** |
---|
1606 | *****************************************************************************/ |
---|
1607 | const REGFI_NK_REC* regfi_iterator_cur_subkey(REGFI_ITERATOR* i) |
---|
1608 | { |
---|
1609 | uint32 nk_offset; |
---|
1610 | |
---|
1611 | /* see if there is anything left to report */ |
---|
1612 | if (!(i->cur_key) || (i->cur_key->subkeys_off==REGFI_OFFSET_NONE) |
---|
1613 | || (i->cur_subkey >= i->cur_key->num_subkeys)) |
---|
1614 | return NULL; |
---|
1615 | |
---|
1616 | nk_offset = i->cur_key->subkeys->elements[i->cur_subkey].offset; |
---|
1617 | |
---|
1618 | return regfi_load_key(i->f, nk_offset+REGFI_REGF_SIZE, true); |
---|
1619 | } |
---|
1620 | |
---|
1621 | |
---|
1622 | /****************************************************************************** |
---|
1623 | *****************************************************************************/ |
---|
1624 | /* XXX: some way of indicating reason for failure should be added. */ |
---|
1625 | const REGFI_NK_REC* regfi_iterator_next_subkey(REGFI_ITERATOR* i) |
---|
1626 | { |
---|
1627 | const REGFI_NK_REC* subkey; |
---|
1628 | |
---|
1629 | i->cur_subkey++; |
---|
1630 | subkey = regfi_iterator_cur_subkey(i); |
---|
1631 | |
---|
1632 | if(subkey == NULL) |
---|
1633 | i->cur_subkey--; |
---|
1634 | |
---|
1635 | return subkey; |
---|
1636 | } |
---|
1637 | |
---|
1638 | |
---|
1639 | /****************************************************************************** |
---|
1640 | *****************************************************************************/ |
---|
1641 | bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* value_name) |
---|
1642 | { |
---|
1643 | const REGFI_VK_REC* cur; |
---|
1644 | bool found = false; |
---|
1645 | |
---|
1646 | /* XXX: cur->valuename can be NULL in the registry. |
---|
1647 | * Should we allow for a way to search for that? |
---|
1648 | */ |
---|
1649 | if(value_name == NULL) |
---|
1650 | return false; |
---|
1651 | |
---|
1652 | cur = regfi_iterator_first_value(i); |
---|
1653 | while((cur != NULL) && (found == false)) |
---|
1654 | { |
---|
1655 | if((cur->valuename != NULL) |
---|
1656 | && (strcasecmp(cur->valuename, value_name) == 0)) |
---|
1657 | found = true; |
---|
1658 | else |
---|
1659 | cur = regfi_iterator_next_value(i); |
---|
1660 | } |
---|
1661 | |
---|
1662 | return found; |
---|
1663 | } |
---|
1664 | |
---|
1665 | |
---|
1666 | /****************************************************************************** |
---|
1667 | *****************************************************************************/ |
---|
1668 | const REGFI_VK_REC* regfi_iterator_first_value(REGFI_ITERATOR* i) |
---|
1669 | { |
---|
1670 | i->cur_value = 0; |
---|
1671 | return regfi_iterator_cur_value(i); |
---|
1672 | } |
---|
1673 | |
---|
1674 | |
---|
1675 | /****************************************************************************** |
---|
1676 | *****************************************************************************/ |
---|
1677 | const REGFI_VK_REC* regfi_iterator_cur_value(REGFI_ITERATOR* i) |
---|
1678 | { |
---|
1679 | const REGFI_VK_REC* ret_val = NULL; |
---|
1680 | uint32 voffset; |
---|
1681 | |
---|
1682 | if(i->cur_key->values != NULL && i->cur_key->values->elements != NULL) |
---|
1683 | { |
---|
1684 | if(i->cur_value < i->cur_key->values->num_values) |
---|
1685 | { |
---|
1686 | voffset = i->cur_key->values->elements[i->cur_value]; |
---|
1687 | ret_val = regfi_load_value(i->f, voffset+REGFI_REGF_SIZE, true); |
---|
1688 | } |
---|
1689 | } |
---|
1690 | |
---|
1691 | return ret_val; |
---|
1692 | } |
---|
1693 | |
---|
1694 | |
---|
1695 | /****************************************************************************** |
---|
1696 | *****************************************************************************/ |
---|
1697 | const REGFI_VK_REC* regfi_iterator_next_value(REGFI_ITERATOR* i) |
---|
1698 | { |
---|
1699 | const REGFI_VK_REC* ret_val; |
---|
1700 | |
---|
1701 | i->cur_value++; |
---|
1702 | ret_val = regfi_iterator_cur_value(i); |
---|
1703 | if(ret_val == NULL) |
---|
1704 | i->cur_value--; |
---|
1705 | |
---|
1706 | return ret_val; |
---|
1707 | } |
---|
1708 | |
---|
1709 | |
---|
1710 | /******************************************************************* |
---|
1711 | * Computes the checksum of the registry file header. |
---|
1712 | * buffer must be at least the size of an regf header (4096 bytes). |
---|
1713 | *******************************************************************/ |
---|
1714 | static uint32 regfi_compute_header_checksum(uint8* buffer) |
---|
1715 | { |
---|
1716 | uint32 checksum, x; |
---|
1717 | int i; |
---|
1718 | |
---|
1719 | /* XOR of all bytes 0x0000 - 0x01FB */ |
---|
1720 | |
---|
1721 | checksum = x = 0; |
---|
1722 | |
---|
1723 | for ( i=0; i<0x01FB; i+=4 ) { |
---|
1724 | x = IVAL(buffer, i ); |
---|
1725 | checksum ^= x; |
---|
1726 | } |
---|
1727 | |
---|
1728 | return checksum; |
---|
1729 | } |
---|
1730 | |
---|
1731 | |
---|
1732 | /******************************************************************* |
---|
1733 | * XXX: Add way to return more detailed error information. |
---|
1734 | *******************************************************************/ |
---|
1735 | REGFI_FILE* regfi_parse_regf(int fd, bool strict) |
---|
1736 | { |
---|
1737 | uint8 file_header[REGFI_REGF_SIZE]; |
---|
1738 | uint32 length; |
---|
1739 | REGFI_FILE* ret_val; |
---|
1740 | |
---|
1741 | ret_val = (REGFI_FILE*)zalloc(sizeof(REGFI_FILE)); |
---|
1742 | if(ret_val == NULL) |
---|
1743 | return NULL; |
---|
1744 | |
---|
1745 | ret_val->fd = fd; |
---|
1746 | |
---|
1747 | length = REGFI_REGF_SIZE; |
---|
1748 | if((regfi_read(fd, file_header, &length)) != 0 |
---|
1749 | || length != REGFI_REGF_SIZE) |
---|
1750 | { |
---|
1751 | free(ret_val); |
---|
1752 | return NULL; |
---|
1753 | } |
---|
1754 | |
---|
1755 | ret_val->checksum = IVAL(file_header, 0x1FC); |
---|
1756 | ret_val->computed_checksum = regfi_compute_header_checksum(file_header); |
---|
1757 | if (strict && (ret_val->checksum != ret_val->computed_checksum)) |
---|
1758 | { |
---|
1759 | free(ret_val); |
---|
1760 | return NULL; |
---|
1761 | } |
---|
1762 | |
---|
1763 | memcpy(ret_val->magic, file_header, REGFI_REGF_MAGIC_SIZE); |
---|
1764 | if(strict && (memcmp(ret_val->magic, "regf", REGFI_REGF_MAGIC_SIZE) != 0)) |
---|
1765 | { |
---|
1766 | free(ret_val); |
---|
1767 | return NULL; |
---|
1768 | } |
---|
1769 | |
---|
1770 | ret_val->unknown1 = IVAL(file_header, 0x4); |
---|
1771 | ret_val->unknown2 = IVAL(file_header, 0x8); |
---|
1772 | |
---|
1773 | ret_val->mtime.low = IVAL(file_header, 0xC); |
---|
1774 | ret_val->mtime.high = IVAL(file_header, 0x10); |
---|
1775 | |
---|
1776 | ret_val->unknown3 = IVAL(file_header, 0x14); |
---|
1777 | ret_val->unknown4 = IVAL(file_header, 0x18); |
---|
1778 | ret_val->unknown5 = IVAL(file_header, 0x1C); |
---|
1779 | ret_val->unknown6 = IVAL(file_header, 0x20); |
---|
1780 | |
---|
1781 | ret_val->data_offset = IVAL(file_header, 0x24); |
---|
1782 | ret_val->last_block = IVAL(file_header, 0x28); |
---|
1783 | |
---|
1784 | ret_val->unknown7 = IVAL(file_header, 0x2C); |
---|
1785 | |
---|
1786 | return ret_val; |
---|
1787 | } |
---|
1788 | |
---|
1789 | |
---|
1790 | |
---|
1791 | /****************************************************************************** |
---|
1792 | * Given real file offset, read and parse the hbin at that location |
---|
1793 | * along with it's associated cells. |
---|
1794 | ******************************************************************************/ |
---|
1795 | REGFI_HBIN* regfi_parse_hbin(REGFI_FILE* file, uint32 offset, bool strict) |
---|
1796 | { |
---|
1797 | REGFI_HBIN *hbin; |
---|
1798 | uint8 hbin_header[REGFI_HBIN_HEADER_SIZE]; |
---|
1799 | uint32 length; |
---|
1800 | |
---|
1801 | if(offset >= file->file_length) |
---|
1802 | return NULL; |
---|
1803 | |
---|
1804 | if(lseek(file->fd, offset, SEEK_SET) == -1) |
---|
1805 | { |
---|
1806 | regfi_add_message(file, REGFI_MSG_ERROR, "Seek failed" |
---|
1807 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
1808 | return NULL; |
---|
1809 | } |
---|
1810 | |
---|
1811 | length = REGFI_HBIN_HEADER_SIZE; |
---|
1812 | if((regfi_read(file->fd, hbin_header, &length) != 0) |
---|
1813 | || length != REGFI_HBIN_HEADER_SIZE) |
---|
1814 | return NULL; |
---|
1815 | |
---|
1816 | if(lseek(file->fd, offset, SEEK_SET) == -1) |
---|
1817 | { |
---|
1818 | regfi_add_message(file, REGFI_MSG_ERROR, "Seek failed" |
---|
1819 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
1820 | return NULL; |
---|
1821 | } |
---|
1822 | |
---|
1823 | hbin = talloc(NULL, REGFI_HBIN); |
---|
1824 | if(hbin == NULL) |
---|
1825 | return NULL; |
---|
1826 | hbin->file_off = offset; |
---|
1827 | |
---|
1828 | memcpy(hbin->magic, hbin_header, 4); |
---|
1829 | if(strict && (memcmp(hbin->magic, "hbin", 4) != 0)) |
---|
1830 | { |
---|
1831 | regfi_add_message(file, REGFI_MSG_INFO, "Magic number mismatch " |
---|
1832 | "(%.2X %.2X %.2X %.2X) while parsing hbin at offset" |
---|
1833 | " 0x%.8X.", hbin->magic[0], hbin->magic[1], |
---|
1834 | hbin->magic[2], hbin->magic[3], offset); |
---|
1835 | talloc_free(hbin); |
---|
1836 | return NULL; |
---|
1837 | } |
---|
1838 | |
---|
1839 | hbin->first_hbin_off = IVAL(hbin_header, 0x4); |
---|
1840 | hbin->block_size = IVAL(hbin_header, 0x8); |
---|
1841 | /* this should be the same thing as hbin->block_size but just in case */ |
---|
1842 | hbin->next_block = IVAL(hbin_header, 0x1C); |
---|
1843 | |
---|
1844 | |
---|
1845 | /* Ensure the block size is a multiple of 0x1000 and doesn't run off |
---|
1846 | * the end of the file. |
---|
1847 | */ |
---|
1848 | /* XXX: This may need to be relaxed for dealing with |
---|
1849 | * partial or corrupt files. |
---|
1850 | */ |
---|
1851 | if((offset + hbin->block_size > file->file_length) |
---|
1852 | || (hbin->block_size & 0xFFFFF000) != hbin->block_size) |
---|
1853 | { |
---|
1854 | regfi_add_message(file, REGFI_MSG_ERROR, "The hbin offset is not aligned" |
---|
1855 | " or runs off the end of the file" |
---|
1856 | " while parsing hbin at offset 0x%.8X.", offset); |
---|
1857 | talloc_free(hbin); |
---|
1858 | return NULL; |
---|
1859 | } |
---|
1860 | |
---|
1861 | return hbin; |
---|
1862 | } |
---|
1863 | |
---|
1864 | |
---|
1865 | /******************************************************************* |
---|
1866 | *******************************************************************/ |
---|
1867 | REGFI_NK_REC* regfi_parse_nk(REGFI_FILE* file, uint32 offset, |
---|
1868 | uint32 max_size, bool strict) |
---|
1869 | { |
---|
1870 | uint8 nk_header[REGFI_NK_MIN_LENGTH]; |
---|
1871 | const REGFI_HBIN *hbin; |
---|
1872 | REGFI_NK_REC* ret_val; |
---|
1873 | uint32 length,cell_length; |
---|
1874 | uint32 class_offset, class_maxsize; |
---|
1875 | bool unalloc = false; |
---|
1876 | |
---|
1877 | if(!regfi_parse_cell(file->fd, offset, nk_header, REGFI_NK_MIN_LENGTH, |
---|
1878 | &cell_length, &unalloc)) |
---|
1879 | { |
---|
1880 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" |
---|
1881 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
1882 | return NULL; |
---|
1883 | } |
---|
1884 | |
---|
1885 | /* A bit of validation before bothering to allocate memory */ |
---|
1886 | if((nk_header[0x0] != 'n') || (nk_header[0x1] != 'k')) |
---|
1887 | { |
---|
1888 | regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch in parsing" |
---|
1889 | " NK record at offset 0x%.8X.", offset); |
---|
1890 | return NULL; |
---|
1891 | } |
---|
1892 | |
---|
1893 | ret_val = (REGFI_NK_REC*)zalloc(sizeof(REGFI_NK_REC)); |
---|
1894 | if(ret_val == NULL) |
---|
1895 | { |
---|
1896 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to allocate memory while" |
---|
1897 | " parsing NK record at offset 0x%.8X.", offset); |
---|
1898 | return NULL; |
---|
1899 | } |
---|
1900 | |
---|
1901 | ret_val->offset = offset; |
---|
1902 | ret_val->cell_size = cell_length; |
---|
1903 | |
---|
1904 | if(ret_val->cell_size > max_size) |
---|
1905 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
1906 | if((ret_val->cell_size < REGFI_NK_MIN_LENGTH) |
---|
1907 | || (strict && ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8))) |
---|
1908 | { |
---|
1909 | regfi_add_message(file, REGFI_MSG_WARN, "A length check failed while" |
---|
1910 | " parsing NK record at offset 0x%.8X.", offset); |
---|
1911 | free(ret_val); |
---|
1912 | return NULL; |
---|
1913 | } |
---|
1914 | |
---|
1915 | ret_val->magic[0] = nk_header[0x0]; |
---|
1916 | ret_val->magic[1] = nk_header[0x1]; |
---|
1917 | ret_val->key_type = SVAL(nk_header, 0x2); |
---|
1918 | if((ret_val->key_type != REGFI_NK_TYPE_NORMALKEY) |
---|
1919 | && (ret_val->key_type != REGFI_NK_TYPE_ROOTKEY1) |
---|
1920 | && (ret_val->key_type != REGFI_NK_TYPE_ROOTKEY2) |
---|
1921 | && (ret_val->key_type != REGFI_NK_TYPE_LINKKEY) |
---|
1922 | && (ret_val->key_type != REGFI_NK_TYPE_UNKNOWN1) |
---|
1923 | && (ret_val->key_type != REGFI_NK_TYPE_UNKNOWN2) |
---|
1924 | && (ret_val->key_type != REGFI_NK_TYPE_UNKNOWN3)) |
---|
1925 | { |
---|
1926 | regfi_add_message(file, REGFI_MSG_WARN, "Unknown key type (0x%.4X) while" |
---|
1927 | " parsing NK record at offset 0x%.8X.", |
---|
1928 | ret_val->key_type, offset); |
---|
1929 | } |
---|
1930 | |
---|
1931 | ret_val->mtime.low = IVAL(nk_header, 0x4); |
---|
1932 | ret_val->mtime.high = IVAL(nk_header, 0x8); |
---|
1933 | /* If the key is unallocated and the MTIME is earlier than Jan 1, 1990 |
---|
1934 | * or later than Jan 1, 2290, we consider this a bad key. This helps |
---|
1935 | * weed out some false positives during deleted data recovery. |
---|
1936 | */ |
---|
1937 | if(unalloc |
---|
1938 | && ((ret_val->mtime.high < REGFI_MTIME_MIN_HIGH |
---|
1939 | && ret_val->mtime.low < REGFI_MTIME_MIN_LOW) |
---|
1940 | || (ret_val->mtime.high > REGFI_MTIME_MAX_HIGH |
---|
1941 | && ret_val->mtime.low > REGFI_MTIME_MAX_LOW))) |
---|
1942 | return NULL; |
---|
1943 | |
---|
1944 | ret_val->unknown1 = IVAL(nk_header, 0xC); |
---|
1945 | ret_val->parent_off = IVAL(nk_header, 0x10); |
---|
1946 | ret_val->num_subkeys = IVAL(nk_header, 0x14); |
---|
1947 | ret_val->unknown2 = IVAL(nk_header, 0x18); |
---|
1948 | ret_val->subkeys_off = IVAL(nk_header, 0x1C); |
---|
1949 | ret_val->unknown3 = IVAL(nk_header, 0x20); |
---|
1950 | ret_val->num_values = IVAL(nk_header, 0x24); |
---|
1951 | ret_val->values_off = IVAL(nk_header, 0x28); |
---|
1952 | ret_val->sk_off = IVAL(nk_header, 0x2C); |
---|
1953 | ret_val->classname_off = IVAL(nk_header, 0x30); |
---|
1954 | |
---|
1955 | ret_val->max_bytes_subkeyname = IVAL(nk_header, 0x34); |
---|
1956 | ret_val->max_bytes_subkeyclassname = IVAL(nk_header, 0x38); |
---|
1957 | ret_val->max_bytes_valuename = IVAL(nk_header, 0x3C); |
---|
1958 | ret_val->max_bytes_value = IVAL(nk_header, 0x40); |
---|
1959 | ret_val->unk_index = IVAL(nk_header, 0x44); |
---|
1960 | |
---|
1961 | ret_val->name_length = SVAL(nk_header, 0x48); |
---|
1962 | ret_val->classname_length = SVAL(nk_header, 0x4A); |
---|
1963 | |
---|
1964 | |
---|
1965 | if(ret_val->name_length + REGFI_NK_MIN_LENGTH > ret_val->cell_size) |
---|
1966 | { |
---|
1967 | if(strict) |
---|
1968 | { |
---|
1969 | regfi_add_message(file, REGFI_MSG_ERROR, "Contents too large for cell" |
---|
1970 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
1971 | free(ret_val); |
---|
1972 | return NULL; |
---|
1973 | } |
---|
1974 | else |
---|
1975 | ret_val->name_length = ret_val->cell_size - REGFI_NK_MIN_LENGTH; |
---|
1976 | } |
---|
1977 | else if (unalloc) |
---|
1978 | { /* Truncate cell_size if it's much larger than the apparent total record length. */ |
---|
1979 | /* Round up to the next multiple of 8 */ |
---|
1980 | length = (ret_val->name_length + REGFI_NK_MIN_LENGTH) & 0xFFFFFFF8; |
---|
1981 | if(length < ret_val->name_length + REGFI_NK_MIN_LENGTH) |
---|
1982 | length+=8; |
---|
1983 | |
---|
1984 | /* If cell_size is still greater, truncate. */ |
---|
1985 | if(length < ret_val->cell_size) |
---|
1986 | ret_val->cell_size = length; |
---|
1987 | } |
---|
1988 | |
---|
1989 | ret_val->keyname = (char*)zalloc(sizeof(char)*(ret_val->name_length+1)); |
---|
1990 | if(ret_val->keyname == NULL) |
---|
1991 | { |
---|
1992 | free(ret_val); |
---|
1993 | return NULL; |
---|
1994 | } |
---|
1995 | |
---|
1996 | /* Don't need to seek, should be at the right offset */ |
---|
1997 | length = ret_val->name_length; |
---|
1998 | if((regfi_read(file->fd, (uint8*)ret_val->keyname, &length) != 0) |
---|
1999 | || length != ret_val->name_length) |
---|
2000 | { |
---|
2001 | regfi_add_message(file, REGFI_MSG_ERROR, "Failed to read key name" |
---|
2002 | " while parsing NK record at offset 0x%.8X.", offset); |
---|
2003 | free(ret_val->keyname); |
---|
2004 | free(ret_val); |
---|
2005 | return NULL; |
---|
2006 | } |
---|
2007 | ret_val->keyname[ret_val->name_length] = '\0'; |
---|
2008 | |
---|
2009 | if(ret_val->classname_off != REGFI_OFFSET_NONE) |
---|
2010 | { |
---|
2011 | hbin = regfi_lookup_hbin(file, ret_val->classname_off); |
---|
2012 | if(hbin) |
---|
2013 | { |
---|
2014 | class_offset = ret_val->classname_off+REGFI_REGF_SIZE; |
---|
2015 | class_maxsize = hbin->block_size + hbin->file_off - class_offset; |
---|
2016 | ret_val->classname |
---|
2017 | = regfi_parse_classname(file, class_offset, &ret_val->classname_length, |
---|
2018 | class_maxsize, strict); |
---|
2019 | } |
---|
2020 | else |
---|
2021 | { |
---|
2022 | ret_val->classname = NULL; |
---|
2023 | regfi_add_message(file, REGFI_MSG_WARN, "Could not find hbin for class" |
---|
2024 | " name while parsing NK record at offset 0x%.8X.", |
---|
2025 | offset); |
---|
2026 | } |
---|
2027 | |
---|
2028 | if(ret_val->classname == NULL) |
---|
2029 | { |
---|
2030 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse class" |
---|
2031 | " name while parsing NK record at offset 0x%.8X.", |
---|
2032 | offset); |
---|
2033 | return NULL; |
---|
2034 | } |
---|
2035 | } |
---|
2036 | |
---|
2037 | return ret_val; |
---|
2038 | } |
---|
2039 | |
---|
2040 | |
---|
2041 | char* regfi_parse_classname(REGFI_FILE* file, uint32 offset, |
---|
2042 | uint16* name_length, uint32 max_size, bool strict) |
---|
2043 | { |
---|
2044 | char* ret_val = NULL; |
---|
2045 | uint32 length; |
---|
2046 | uint32 cell_length; |
---|
2047 | bool unalloc = false; |
---|
2048 | |
---|
2049 | if(*name_length > 0 && offset != REGFI_OFFSET_NONE |
---|
2050 | && offset == (offset & 0xFFFFFFF8)) |
---|
2051 | { |
---|
2052 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, &cell_length, &unalloc)) |
---|
2053 | { |
---|
2054 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" |
---|
2055 | " while parsing class name at offset 0x%.8X.", offset); |
---|
2056 | return NULL; |
---|
2057 | } |
---|
2058 | |
---|
2059 | if((cell_length & 0xFFFFFFF8) != cell_length) |
---|
2060 | { |
---|
2061 | regfi_add_message(file, REGFI_MSG_ERROR, "Cell length not a multiple of 8" |
---|
2062 | " while parsing class name at offset 0x%.8X.", offset); |
---|
2063 | return NULL; |
---|
2064 | } |
---|
2065 | |
---|
2066 | if(cell_length > max_size) |
---|
2067 | { |
---|
2068 | regfi_add_message(file, REGFI_MSG_WARN, "Cell stretches past hbin " |
---|
2069 | "boundary while parsing class name at offset 0x%.8X.", |
---|
2070 | offset); |
---|
2071 | if(strict) |
---|
2072 | return NULL; |
---|
2073 | cell_length = max_size; |
---|
2074 | } |
---|
2075 | |
---|
2076 | if((cell_length - 4) < *name_length) |
---|
2077 | { |
---|
2078 | regfi_add_message(file, REGFI_MSG_WARN, "Class name is larger than" |
---|
2079 | " cell_length while parsing class name at offset" |
---|
2080 | " 0x%.8X.", offset); |
---|
2081 | if(strict) |
---|
2082 | return NULL; |
---|
2083 | *name_length = cell_length - 4; |
---|
2084 | } |
---|
2085 | |
---|
2086 | ret_val = (char*)zalloc(*name_length); |
---|
2087 | if(ret_val != NULL) |
---|
2088 | { |
---|
2089 | length = *name_length; |
---|
2090 | if((regfi_read(file->fd, (uint8*)ret_val, &length) != 0) |
---|
2091 | || length != *name_length) |
---|
2092 | { |
---|
2093 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not read class name" |
---|
2094 | " while parsing class name at offset 0x%.8X.", offset); |
---|
2095 | free(ret_val); |
---|
2096 | return NULL; |
---|
2097 | } |
---|
2098 | } |
---|
2099 | } |
---|
2100 | |
---|
2101 | return ret_val; |
---|
2102 | } |
---|
2103 | |
---|
2104 | |
---|
2105 | /******************************************************************* |
---|
2106 | *******************************************************************/ |
---|
2107 | REGFI_VK_REC* regfi_parse_vk(REGFI_FILE* file, uint32 offset, |
---|
2108 | uint32 max_size, bool strict) |
---|
2109 | { |
---|
2110 | REGFI_VK_REC* ret_val; |
---|
2111 | uint8 vk_header[REGFI_VK_MIN_LENGTH]; |
---|
2112 | uint32 raw_data_size, length, cell_length; |
---|
2113 | bool unalloc = false; |
---|
2114 | |
---|
2115 | if(!regfi_parse_cell(file->fd, offset, vk_header, REGFI_VK_MIN_LENGTH, |
---|
2116 | &cell_length, &unalloc)) |
---|
2117 | { |
---|
2118 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell header" |
---|
2119 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
2120 | return NULL; |
---|
2121 | } |
---|
2122 | |
---|
2123 | ret_val = (REGFI_VK_REC*)zalloc(sizeof(REGFI_VK_REC)); |
---|
2124 | if(ret_val == NULL) |
---|
2125 | return NULL; |
---|
2126 | |
---|
2127 | ret_val->offset = offset; |
---|
2128 | ret_val->cell_size = cell_length; |
---|
2129 | |
---|
2130 | if(ret_val->cell_size > max_size) |
---|
2131 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
2132 | if((ret_val->cell_size < REGFI_VK_MIN_LENGTH) |
---|
2133 | || ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8)) |
---|
2134 | { |
---|
2135 | regfi_add_message(file, REGFI_MSG_WARN, "Invalid cell size encountered" |
---|
2136 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
2137 | free(ret_val); |
---|
2138 | return NULL; |
---|
2139 | } |
---|
2140 | |
---|
2141 | ret_val->magic[0] = vk_header[0x0]; |
---|
2142 | ret_val->magic[1] = vk_header[0x1]; |
---|
2143 | if((ret_val->magic[0] != 'v') || (ret_val->magic[1] != 'k')) |
---|
2144 | { |
---|
2145 | /* XXX: This does not account for deleted keys under Win2K which |
---|
2146 | * often have this (and the name length) overwritten with |
---|
2147 | * 0xFFFF. |
---|
2148 | */ |
---|
2149 | regfi_add_message(file, REGFI_MSG_WARN, "Magic number mismatch" |
---|
2150 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
2151 | free(ret_val); |
---|
2152 | return NULL; |
---|
2153 | } |
---|
2154 | |
---|
2155 | ret_val->name_length = SVAL(vk_header, 0x2); |
---|
2156 | raw_data_size = IVAL(vk_header, 0x4); |
---|
2157 | ret_val->data_size = raw_data_size & ~REGFI_VK_DATA_IN_OFFSET; |
---|
2158 | ret_val->data_in_offset = (bool)(raw_data_size & REGFI_VK_DATA_IN_OFFSET); |
---|
2159 | ret_val->data_off = IVAL(vk_header, 0x8); |
---|
2160 | ret_val->type = IVAL(vk_header, 0xC); |
---|
2161 | ret_val->flag = SVAL(vk_header, 0x10); |
---|
2162 | ret_val->unknown1 = SVAL(vk_header, 0x12); |
---|
2163 | |
---|
2164 | if(ret_val->flag & REGFI_VK_FLAG_NAME_PRESENT) |
---|
2165 | { |
---|
2166 | if(ret_val->name_length + REGFI_VK_MIN_LENGTH + 4 > ret_val->cell_size) |
---|
2167 | { |
---|
2168 | regfi_add_message(file, REGFI_MSG_WARN, "Name too long for remaining cell" |
---|
2169 | " space while parsing VK record at offset 0x%.8X.", |
---|
2170 | offset); |
---|
2171 | if(strict) |
---|
2172 | { |
---|
2173 | free(ret_val); |
---|
2174 | return NULL; |
---|
2175 | } |
---|
2176 | else |
---|
2177 | ret_val->name_length = ret_val->cell_size - REGFI_VK_MIN_LENGTH - 4; |
---|
2178 | } |
---|
2179 | |
---|
2180 | /* Round up to the next multiple of 8 */ |
---|
2181 | cell_length = (ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) & 0xFFFFFFF8; |
---|
2182 | if(cell_length < ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) |
---|
2183 | cell_length+=8; |
---|
2184 | |
---|
2185 | ret_val->valuename = (char*)zalloc(sizeof(char)*(ret_val->name_length+1)); |
---|
2186 | if(ret_val->valuename == NULL) |
---|
2187 | { |
---|
2188 | free(ret_val); |
---|
2189 | return NULL; |
---|
2190 | } |
---|
2191 | |
---|
2192 | length = ret_val->name_length; |
---|
2193 | if((regfi_read(file->fd, (uint8*)ret_val->valuename, &length) != 0) |
---|
2194 | || length != ret_val->name_length) |
---|
2195 | { |
---|
2196 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not read value name" |
---|
2197 | " while parsing VK record at offset 0x%.8X.", offset); |
---|
2198 | free(ret_val->valuename); |
---|
2199 | free(ret_val); |
---|
2200 | return NULL; |
---|
2201 | } |
---|
2202 | ret_val->valuename[ret_val->name_length] = '\0'; |
---|
2203 | |
---|
2204 | } |
---|
2205 | else |
---|
2206 | cell_length = REGFI_VK_MIN_LENGTH + 4; |
---|
2207 | |
---|
2208 | if(unalloc) |
---|
2209 | { |
---|
2210 | /* If cell_size is still greater, truncate. */ |
---|
2211 | if(cell_length < ret_val->cell_size) |
---|
2212 | ret_val->cell_size = cell_length; |
---|
2213 | } |
---|
2214 | |
---|
2215 | return ret_val; |
---|
2216 | } |
---|
2217 | |
---|
2218 | |
---|
2219 | uint8* regfi_parse_data(REGFI_FILE* file, |
---|
2220 | uint32 data_type, uint32 offset, |
---|
2221 | uint32 length, uint32 max_size, |
---|
2222 | bool data_in_offset, bool strict) |
---|
2223 | { |
---|
2224 | uint8* ret_val; |
---|
2225 | uint32 read_length, cell_length; |
---|
2226 | uint8 i; |
---|
2227 | bool unalloc; |
---|
2228 | |
---|
2229 | /* The data is typically stored in the offset if the size <= 4 */ |
---|
2230 | if(data_in_offset) |
---|
2231 | { |
---|
2232 | if(length > 4) |
---|
2233 | { |
---|
2234 | regfi_add_message(file, REGFI_MSG_ERROR, "Data in offset but length > 4" |
---|
2235 | " while parsing data record at offset 0x%.8X.", |
---|
2236 | offset); |
---|
2237 | return NULL; |
---|
2238 | } |
---|
2239 | |
---|
2240 | if((ret_val = (uint8*)zalloc(sizeof(uint8)*length)) == NULL) |
---|
2241 | return NULL; |
---|
2242 | |
---|
2243 | for(i = 0; i < length; i++) |
---|
2244 | ret_val[i] = (uint8)((offset >> i*8) & 0xFF); |
---|
2245 | } |
---|
2246 | else |
---|
2247 | { |
---|
2248 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, |
---|
2249 | &cell_length, &unalloc)) |
---|
2250 | { |
---|
2251 | regfi_add_message(file, REGFI_MSG_WARN, "Could not parse cell while" |
---|
2252 | " parsing data record at offset 0x%.8X.", offset); |
---|
2253 | return NULL; |
---|
2254 | } |
---|
2255 | |
---|
2256 | if((cell_length & 0xFFFFFFF8) != cell_length) |
---|
2257 | { |
---|
2258 | regfi_add_message(file, REGFI_MSG_WARN, "Cell length not multiple of 8" |
---|
2259 | " while parsing data record at offset 0x%.8X.", |
---|
2260 | offset); |
---|
2261 | return NULL; |
---|
2262 | } |
---|
2263 | |
---|
2264 | if(cell_length > max_size) |
---|
2265 | { |
---|
2266 | regfi_add_message(file, REGFI_MSG_WARN, "Cell extends past HBIN boundary" |
---|
2267 | " while parsing data record at offset 0x%.8X.", |
---|
2268 | offset); |
---|
2269 | if(strict) |
---|
2270 | return NULL; |
---|
2271 | else |
---|
2272 | cell_length = max_size; |
---|
2273 | } |
---|
2274 | |
---|
2275 | if(cell_length - 4 < length) |
---|
2276 | { |
---|
2277 | /* XXX: This strict condition has been triggered in multiple registries. |
---|
2278 | * Not sure the cause, but the data length values are very large, |
---|
2279 | * such as 53392. |
---|
2280 | */ |
---|
2281 | regfi_add_message(file, REGFI_MSG_WARN, "Data length (0x%.8X) larger than" |
---|
2282 | " remaining cell length (0x%.8X)" |
---|
2283 | " while parsing data record at offset 0x%.8X.", |
---|
2284 | length, cell_length - 4, offset); |
---|
2285 | if(strict) |
---|
2286 | return NULL; |
---|
2287 | else |
---|
2288 | length = cell_length - 4; |
---|
2289 | } |
---|
2290 | |
---|
2291 | if((ret_val = (uint8*)zalloc(sizeof(uint8)*length)) == NULL) |
---|
2292 | return NULL; |
---|
2293 | |
---|
2294 | read_length = length; |
---|
2295 | if((regfi_read(file->fd, ret_val, &read_length) != 0) |
---|
2296 | || read_length != length) |
---|
2297 | { |
---|
2298 | regfi_add_message(file, REGFI_MSG_ERROR, "Could not read data block while" |
---|
2299 | " parsing data record at offset 0x%.8X.", offset); |
---|
2300 | free(ret_val); |
---|
2301 | return NULL; |
---|
2302 | } |
---|
2303 | } |
---|
2304 | |
---|
2305 | return ret_val; |
---|
2306 | } |
---|
2307 | |
---|
2308 | |
---|
2309 | range_list* regfi_parse_unalloc_cells(REGFI_FILE* file) |
---|
2310 | { |
---|
2311 | range_list* ret_val; |
---|
2312 | REGFI_HBIN* hbin; |
---|
2313 | const range_list_element* hbins_elem; |
---|
2314 | uint32 i, num_hbins, curr_off, cell_len; |
---|
2315 | bool is_unalloc; |
---|
2316 | |
---|
2317 | ret_val = range_list_new(); |
---|
2318 | if(ret_val == NULL) |
---|
2319 | return NULL; |
---|
2320 | |
---|
2321 | num_hbins = range_list_size(file->hbins); |
---|
2322 | for(i=0; i<num_hbins; i++) |
---|
2323 | { |
---|
2324 | hbins_elem = range_list_get(file->hbins, i); |
---|
2325 | if(hbins_elem == NULL) |
---|
2326 | break; |
---|
2327 | hbin = (REGFI_HBIN*)hbins_elem->data; |
---|
2328 | |
---|
2329 | curr_off = REGFI_HBIN_HEADER_SIZE; |
---|
2330 | while(curr_off < hbin->block_size) |
---|
2331 | { |
---|
2332 | if(!regfi_parse_cell(file->fd, hbin->file_off+curr_off, NULL, 0, |
---|
2333 | &cell_len, &is_unalloc)) |
---|
2334 | break; |
---|
2335 | |
---|
2336 | if((cell_len == 0) || ((cell_len & 0xFFFFFFF8) != cell_len)) |
---|
2337 | { |
---|
2338 | regfi_add_message(file, REGFI_MSG_ERROR, "Bad cell length encountered" |
---|
2339 | " while parsing unallocated cells at offset 0x%.8X.", |
---|
2340 | hbin->file_off+curr_off); |
---|
2341 | break; |
---|
2342 | } |
---|
2343 | |
---|
2344 | /* for some reason the record_size of the last record in |
---|
2345 | an hbin block can extend past the end of the block |
---|
2346 | even though the record fits within the remaining |
---|
2347 | space....aaarrrgggghhhhhh */ |
---|
2348 | if(curr_off + cell_len >= hbin->block_size) |
---|
2349 | cell_len = hbin->block_size - curr_off; |
---|
2350 | |
---|
2351 | if(is_unalloc) |
---|
2352 | range_list_add(ret_val, hbin->file_off+curr_off, |
---|
2353 | cell_len, NULL); |
---|
2354 | |
---|
2355 | curr_off = curr_off+cell_len; |
---|
2356 | } |
---|
2357 | } |
---|
2358 | |
---|
2359 | return ret_val; |
---|
2360 | } |
---|