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