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