[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 | * |
---|
[132] | 8 | * Copyright (C) 2005-2009 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 133 2009-01-12 17:07:58Z 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 | } |
---|
[128] | 543 | |
---|
[127] | 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 | |
---|
[133] | 690 | /* XXX: call should look more like: */ |
---|
| 691 | /*if (!(ret_val->sec_desc = winsec_parse_desc(sec_desc_buf, ret_val->desc_size)))*/ |
---|
[102] | 692 | if (!sec_io_desc("sec_desc", &ret_val->sec_desc, &ps, 0)) |
---|
| 693 | { |
---|
| 694 | free(ret_val); |
---|
| 695 | return NULL; |
---|
| 696 | } |
---|
| 697 | |
---|
| 698 | free(ps.data_p); |
---|
| 699 | |
---|
| 700 | return ret_val; |
---|
[30] | 701 | } |
---|
| 702 | |
---|
| 703 | |
---|
[111] | 704 | uint32* regfi_parse_valuelist(REGF_FILE* file, uint32 offset, |
---|
| 705 | uint32 num_values, bool strict) |
---|
| 706 | { |
---|
| 707 | uint32* ret_val; |
---|
| 708 | uint32 i, cell_length, length, read_len; |
---|
| 709 | bool unalloc; |
---|
[30] | 710 | |
---|
[111] | 711 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, &cell_length, &unalloc)) |
---|
| 712 | return NULL; |
---|
| 713 | |
---|
| 714 | if(cell_length != (cell_length & 0xFFFFFFF8)) |
---|
| 715 | { |
---|
| 716 | if(strict) |
---|
| 717 | return NULL; |
---|
| 718 | cell_length = cell_length & 0xFFFFFFF8; |
---|
| 719 | } |
---|
| 720 | if((num_values * sizeof(uint32)) > cell_length-sizeof(uint32)) |
---|
| 721 | return NULL; |
---|
| 722 | |
---|
| 723 | read_len = num_values*sizeof(uint32); |
---|
| 724 | ret_val = (uint32*)malloc(read_len); |
---|
| 725 | if(ret_val == NULL) |
---|
| 726 | return NULL; |
---|
| 727 | |
---|
| 728 | length = read_len; |
---|
| 729 | if((regfi_read(file->fd, (uint8*)ret_val, &length) != 0) || length != read_len) |
---|
| 730 | { |
---|
| 731 | free(ret_val); |
---|
| 732 | return NULL; |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | for(i=0; i < num_values; i++) |
---|
| 736 | { |
---|
| 737 | /* Fix endianness */ |
---|
| 738 | ret_val[i] = IVAL(&ret_val[i], 0); |
---|
| 739 | |
---|
| 740 | /* Validate the first num_values values to ensure they make sense */ |
---|
| 741 | if(strict) |
---|
| 742 | { |
---|
| 743 | if((ret_val[i] + REGF_BLOCKSIZE > file->file_length) |
---|
| 744 | || ((ret_val[i] & 0xFFFFFFF8) != ret_val[i])) |
---|
| 745 | { |
---|
| 746 | free(ret_val); |
---|
| 747 | return NULL; |
---|
| 748 | } |
---|
| 749 | } |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | return ret_val; |
---|
| 753 | } |
---|
| 754 | |
---|
| 755 | |
---|
| 756 | |
---|
[103] | 757 | /****************************************************************************** |
---|
[116] | 758 | * If !strict, the list may contain NULLs, VK records may point to NULL. |
---|
[103] | 759 | ******************************************************************************/ |
---|
| 760 | REGF_VK_REC** regfi_load_valuelist(REGF_FILE* file, uint32 offset, |
---|
[105] | 761 | uint32 num_values, uint32 max_size, |
---|
| 762 | bool strict) |
---|
[30] | 763 | { |
---|
[103] | 764 | REGF_VK_REC** ret_val; |
---|
[111] | 765 | REGF_HBIN* hbin; |
---|
[116] | 766 | uint32 i, vk_offset, vk_max_length, usable_num_values; |
---|
[111] | 767 | uint32* voffsets; |
---|
[30] | 768 | |
---|
[111] | 769 | if((num_values+1) * sizeof(uint32) > max_size) |
---|
[116] | 770 | { |
---|
| 771 | if(strict) |
---|
| 772 | return NULL; |
---|
| 773 | usable_num_values = max_size/sizeof(uint32) - sizeof(uint32); |
---|
| 774 | } |
---|
| 775 | else |
---|
| 776 | usable_num_values = num_values; |
---|
[103] | 777 | |
---|
[116] | 778 | voffsets = regfi_parse_valuelist(file, offset, usable_num_values, strict); |
---|
[111] | 779 | if(voffsets == NULL) |
---|
[103] | 780 | return NULL; |
---|
[30] | 781 | |
---|
[103] | 782 | ret_val = (REGF_VK_REC**)zalloc(sizeof(REGF_VK_REC*) * num_values); |
---|
| 783 | if(ret_val == NULL) |
---|
| 784 | { |
---|
[111] | 785 | free(voffsets); |
---|
[103] | 786 | return NULL; |
---|
[31] | 787 | } |
---|
[103] | 788 | |
---|
[116] | 789 | for(i=0; i < usable_num_values; i++) |
---|
[32] | 790 | { |
---|
[111] | 791 | hbin = regfi_lookup_hbin(file, voffsets[i]); |
---|
| 792 | if(!hbin) |
---|
[32] | 793 | { |
---|
[111] | 794 | free(voffsets); |
---|
[103] | 795 | free(ret_val); |
---|
| 796 | return NULL; |
---|
[31] | 797 | } |
---|
[133] | 798 | |
---|
[111] | 799 | vk_offset = voffsets[i] + REGF_BLOCKSIZE; |
---|
[131] | 800 | vk_max_length = hbin->block_size + hbin->file_off - vk_offset; |
---|
[111] | 801 | ret_val[i] = regfi_parse_vk(file, vk_offset, vk_max_length, strict); |
---|
[103] | 802 | if(ret_val[i] == NULL) |
---|
[111] | 803 | { /* If we're being strict, throw out the whole list. |
---|
| 804 | * Otherwise, let it be NULL. |
---|
| 805 | */ |
---|
| 806 | if(strict) |
---|
| 807 | { |
---|
| 808 | free(voffsets); |
---|
| 809 | free(ret_val); |
---|
| 810 | return NULL; |
---|
| 811 | } |
---|
[103] | 812 | } |
---|
[31] | 813 | } |
---|
[30] | 814 | |
---|
[111] | 815 | free(voffsets); |
---|
[103] | 816 | return ret_val; |
---|
[30] | 817 | } |
---|
| 818 | |
---|
| 819 | |
---|
| 820 | |
---|
| 821 | /******************************************************************* |
---|
[116] | 822 | * XXX: Need to add full key caching using a |
---|
| 823 | * custom cache structure. |
---|
[31] | 824 | *******************************************************************/ |
---|
[109] | 825 | REGF_NK_REC* regfi_load_key(REGF_FILE* file, uint32 offset, bool strict) |
---|
[30] | 826 | { |
---|
[105] | 827 | REGF_HBIN* hbin; |
---|
[99] | 828 | REGF_HBIN* sub_hbin; |
---|
| 829 | REGF_NK_REC* nk; |
---|
[105] | 830 | uint32 max_length, off; |
---|
[99] | 831 | |
---|
[106] | 832 | hbin = regfi_lookup_hbin(file, offset-REGF_BLOCKSIZE); |
---|
[105] | 833 | if (hbin == NULL) |
---|
| 834 | return NULL; |
---|
[30] | 835 | |
---|
[31] | 836 | /* get the initial nk record */ |
---|
[105] | 837 | max_length = hbin->block_size + hbin->file_off - offset; |
---|
| 838 | if ((nk = regfi_parse_nk(file, offset, max_length, true)) == NULL) |
---|
[99] | 839 | return NULL; |
---|
[30] | 840 | |
---|
[31] | 841 | /* fill in values */ |
---|
[105] | 842 | if(nk->num_values && (nk->values_off!=REGF_OFFSET_NONE)) |
---|
[32] | 843 | { |
---|
[31] | 844 | sub_hbin = hbin; |
---|
[106] | 845 | if(!regfi_offset_in_hbin(hbin, nk->values_off)) |
---|
| 846 | sub_hbin = regfi_lookup_hbin(file, nk->values_off); |
---|
[105] | 847 | |
---|
| 848 | if(sub_hbin == NULL) |
---|
[32] | 849 | { |
---|
[105] | 850 | if(strict) |
---|
[32] | 851 | { |
---|
[105] | 852 | free(nk); |
---|
[99] | 853 | return NULL; |
---|
[31] | 854 | } |
---|
[105] | 855 | else |
---|
| 856 | nk->values = NULL; |
---|
[133] | 857 | |
---|
[31] | 858 | } |
---|
[105] | 859 | else |
---|
[103] | 860 | { |
---|
[105] | 861 | off = nk->values_off + REGF_BLOCKSIZE; |
---|
| 862 | max_length = sub_hbin->block_size + sub_hbin->file_off - off; |
---|
| 863 | nk->values = regfi_load_valuelist(file, off, nk->num_values, max_length, |
---|
| 864 | true); |
---|
| 865 | if(strict && nk->values == NULL) |
---|
| 866 | { |
---|
| 867 | free(nk); |
---|
| 868 | return NULL; |
---|
| 869 | } |
---|
[133] | 870 | |
---|
[103] | 871 | } |
---|
[31] | 872 | } |
---|
[105] | 873 | |
---|
[31] | 874 | /* now get subkeys */ |
---|
[105] | 875 | if(nk->num_subkeys && (nk->subkeys_off != REGF_OFFSET_NONE)) |
---|
[32] | 876 | { |
---|
[31] | 877 | sub_hbin = hbin; |
---|
[106] | 878 | if(!regfi_offset_in_hbin(hbin, nk->subkeys_off)) |
---|
| 879 | sub_hbin = regfi_lookup_hbin(file, nk->subkeys_off); |
---|
[105] | 880 | |
---|
| 881 | if (sub_hbin == NULL) |
---|
[32] | 882 | { |
---|
[105] | 883 | if(strict) |
---|
[32] | 884 | { |
---|
[116] | 885 | regfi_key_free(nk); |
---|
[99] | 886 | return NULL; |
---|
[31] | 887 | } |
---|
[105] | 888 | else |
---|
| 889 | nk->subkeys = NULL; |
---|
[31] | 890 | } |
---|
[105] | 891 | else |
---|
[104] | 892 | { |
---|
[105] | 893 | off = nk->subkeys_off + REGF_BLOCKSIZE; |
---|
| 894 | max_length = sub_hbin->block_size + sub_hbin->file_off - off; |
---|
[127] | 895 | nk->subkeys = regfi_load_subkeylist(file, off, nk->num_subkeys, |
---|
| 896 | max_length, true); |
---|
[105] | 897 | if(nk->subkeys == NULL) |
---|
| 898 | { |
---|
[128] | 899 | /* XXX: Should we free the key and bail out here instead? |
---|
| 900 | * During nonstrict? |
---|
| 901 | */ |
---|
[105] | 902 | nk->num_subkeys = 0; |
---|
| 903 | } |
---|
[104] | 904 | } |
---|
[31] | 905 | } |
---|
[30] | 906 | |
---|
[99] | 907 | return nk; |
---|
[30] | 908 | } |
---|
| 909 | |
---|
[32] | 910 | |
---|
[102] | 911 | /****************************************************************************** |
---|
| 912 | |
---|
| 913 | ******************************************************************************/ |
---|
| 914 | static bool regfi_find_root_nk(REGF_FILE* file, uint32 offset, uint32 hbin_size, |
---|
| 915 | uint32* root_offset) |
---|
[30] | 916 | { |
---|
[102] | 917 | uint8 tmp[4]; |
---|
| 918 | int32 record_size; |
---|
| 919 | uint32 length, hbin_offset = 0; |
---|
| 920 | REGF_NK_REC* nk = NULL; |
---|
[31] | 921 | bool found = false; |
---|
[30] | 922 | |
---|
[102] | 923 | for(record_size=0; !found && (hbin_offset < hbin_size); ) |
---|
[32] | 924 | { |
---|
[102] | 925 | if(lseek(file->fd, offset+hbin_offset, SEEK_SET) == -1) |
---|
[31] | 926 | return false; |
---|
[102] | 927 | |
---|
| 928 | length = 4; |
---|
| 929 | if((regfi_read(file->fd, tmp, &length) != 0) || length != 4) |
---|
[31] | 930 | return false; |
---|
[102] | 931 | record_size = IVALS(tmp, 0); |
---|
[30] | 932 | |
---|
[102] | 933 | if(record_size < 0) |
---|
| 934 | { |
---|
| 935 | record_size = record_size*(-1); |
---|
| 936 | nk = regfi_parse_nk(file, offset+hbin_offset, hbin_size-hbin_offset, true); |
---|
| 937 | if(nk != NULL) |
---|
| 938 | { |
---|
[128] | 939 | if((nk->key_type == NK_TYPE_ROOTKEY1) |
---|
| 940 | || (nk->key_type == NK_TYPE_ROOTKEY2)) |
---|
[102] | 941 | { |
---|
| 942 | found = true; |
---|
| 943 | *root_offset = nk->offset; |
---|
| 944 | } |
---|
| 945 | free(nk); |
---|
| 946 | } |
---|
[31] | 947 | } |
---|
[30] | 948 | |
---|
[102] | 949 | hbin_offset += record_size; |
---|
[31] | 950 | } |
---|
[32] | 951 | |
---|
[102] | 952 | return found; |
---|
[30] | 953 | } |
---|
| 954 | |
---|
| 955 | |
---|
| 956 | /******************************************************************* |
---|
[97] | 957 | * Open the registry file and then read in the REGF block to get the |
---|
| 958 | * first hbin offset. |
---|
| 959 | *******************************************************************/ |
---|
[110] | 960 | REGF_FILE* regfi_open(const char* filename) |
---|
[30] | 961 | { |
---|
[97] | 962 | REGF_FILE* rb; |
---|
[106] | 963 | REGF_HBIN* hbin = NULL; |
---|
| 964 | uint32 hbin_off; |
---|
[97] | 965 | int fd; |
---|
[110] | 966 | bool rla; |
---|
[30] | 967 | |
---|
[97] | 968 | /* open an existing file */ |
---|
[107] | 969 | if ((fd = open(filename, O_RDONLY)) == -1) |
---|
[97] | 970 | { |
---|
[78] | 971 | /* DEBUG(0,("regfi_open: failure to open %s (%s)\n", filename, strerror(errno)));*/ |
---|
[31] | 972 | return NULL; |
---|
| 973 | } |
---|
[99] | 974 | |
---|
[31] | 975 | /* read in an existing file */ |
---|
[97] | 976 | if ((rb = regfi_parse_regf(fd, true)) == NULL) |
---|
| 977 | { |
---|
[78] | 978 | /* DEBUG(0,("regfi_open: Failed to read initial REGF block\n"));*/ |
---|
[97] | 979 | close(fd); |
---|
[31] | 980 | return NULL; |
---|
| 981 | } |
---|
[99] | 982 | |
---|
| 983 | rb->hbins = range_list_new(); |
---|
[110] | 984 | if(rb->hbins == NULL) |
---|
[99] | 985 | { |
---|
[106] | 986 | range_list_free(rb->hbins); |
---|
[99] | 987 | close(fd); |
---|
| 988 | free(rb); |
---|
| 989 | return NULL; |
---|
| 990 | } |
---|
[108] | 991 | |
---|
[106] | 992 | rla = true; |
---|
| 993 | hbin_off = REGF_BLOCKSIZE; |
---|
[110] | 994 | hbin = regfi_parse_hbin(rb, hbin_off, true); |
---|
[106] | 995 | while(hbin && rla) |
---|
| 996 | { |
---|
| 997 | hbin_off = hbin->file_off + hbin->block_size; |
---|
| 998 | rla = range_list_add(rb->hbins, hbin->file_off, hbin->block_size, hbin); |
---|
[110] | 999 | hbin = regfi_parse_hbin(rb, hbin_off, true); |
---|
[106] | 1000 | } |
---|
| 1001 | |
---|
[31] | 1002 | /* success */ |
---|
| 1003 | return rb; |
---|
[30] | 1004 | } |
---|
| 1005 | |
---|
| 1006 | |
---|
| 1007 | /******************************************************************* |
---|
[31] | 1008 | *******************************************************************/ |
---|
[78] | 1009 | int regfi_close( REGF_FILE *file ) |
---|
[30] | 1010 | { |
---|
[31] | 1011 | int fd; |
---|
[106] | 1012 | uint32 i; |
---|
[30] | 1013 | |
---|
[31] | 1014 | /* nothing to do if there is no open file */ |
---|
[99] | 1015 | if ((file == NULL) || (file->fd == -1)) |
---|
| 1016 | return 0; |
---|
[30] | 1017 | |
---|
[31] | 1018 | fd = file->fd; |
---|
| 1019 | file->fd = -1; |
---|
[106] | 1020 | for(i=0; i < range_list_size(file->hbins); i++) |
---|
| 1021 | free(range_list_get(file->hbins, i)->data); |
---|
[99] | 1022 | range_list_free(file->hbins); |
---|
[106] | 1023 | |
---|
[99] | 1024 | free(file); |
---|
[30] | 1025 | |
---|
[106] | 1026 | return close(fd); |
---|
[30] | 1027 | } |
---|
| 1028 | |
---|
| 1029 | |
---|
[80] | 1030 | /****************************************************************************** |
---|
| 1031 | * There should be only *one* root key in the registry file based |
---|
| 1032 | * on my experience. --jerry |
---|
| 1033 | *****************************************************************************/ |
---|
[102] | 1034 | REGF_NK_REC* regfi_rootkey(REGF_FILE *file) |
---|
[30] | 1035 | { |
---|
[102] | 1036 | REGF_NK_REC* nk = NULL; |
---|
| 1037 | REGF_HBIN* hbin; |
---|
[107] | 1038 | uint32 root_offset, i, num_hbins; |
---|
[99] | 1039 | |
---|
| 1040 | if(!file) |
---|
[31] | 1041 | return NULL; |
---|
[99] | 1042 | |
---|
[102] | 1043 | /* Scan through the file one HBIN block at a time looking |
---|
[31] | 1044 | for an NK record with a type == 0x002c. |
---|
| 1045 | Normally this is the first nk record in the first hbin |
---|
| 1046 | block (but I'm not assuming that for now) */ |
---|
[102] | 1047 | |
---|
[107] | 1048 | num_hbins = range_list_size(file->hbins); |
---|
| 1049 | for(i=0; i < num_hbins; i++) |
---|
[99] | 1050 | { |
---|
[107] | 1051 | hbin = (REGF_HBIN*)range_list_get(file->hbins, i)->data; |
---|
[102] | 1052 | if(regfi_find_root_nk(file, hbin->file_off+HBIN_HEADER_REC_SIZE, |
---|
| 1053 | hbin->block_size-HBIN_HEADER_REC_SIZE, &root_offset)) |
---|
[99] | 1054 | { |
---|
[105] | 1055 | nk = regfi_load_key(file, root_offset, true); |
---|
[102] | 1056 | break; |
---|
[31] | 1057 | } |
---|
| 1058 | } |
---|
[30] | 1059 | |
---|
[80] | 1060 | return nk; |
---|
[30] | 1061 | } |
---|
| 1062 | |
---|
| 1063 | |
---|
[80] | 1064 | /****************************************************************************** |
---|
| 1065 | *****************************************************************************/ |
---|
| 1066 | void regfi_key_free(REGF_NK_REC* nk) |
---|
[30] | 1067 | { |
---|
[80] | 1068 | uint32 i; |
---|
| 1069 | |
---|
| 1070 | if((nk->values != NULL) && (nk->values_off!=REGF_OFFSET_NONE)) |
---|
| 1071 | { |
---|
| 1072 | for(i=0; i < nk->num_values; i++) |
---|
[81] | 1073 | { |
---|
[101] | 1074 | if(nk->values[i]->valuename != NULL) |
---|
| 1075 | free(nk->values[i]->valuename); |
---|
| 1076 | if(nk->values[i]->data != NULL) |
---|
| 1077 | free(nk->values[i]->data); |
---|
| 1078 | free(nk->values[i]); |
---|
[81] | 1079 | } |
---|
[80] | 1080 | free(nk->values); |
---|
| 1081 | } |
---|
[30] | 1082 | |
---|
[127] | 1083 | regfi_subkeylist_free(nk->subkeys); |
---|
| 1084 | |
---|
[80] | 1085 | if(nk->keyname != NULL) |
---|
| 1086 | free(nk->keyname); |
---|
| 1087 | if(nk->classname != NULL) |
---|
| 1088 | free(nk->classname); |
---|
| 1089 | |
---|
| 1090 | /* XXX: not freeing hbin because these are cached. This needs to be reviewed. */ |
---|
| 1091 | /* XXX: not freeing sec_desc because these are cached. This needs to be reviewed. */ |
---|
| 1092 | free(nk); |
---|
| 1093 | } |
---|
| 1094 | |
---|
| 1095 | |
---|
| 1096 | /****************************************************************************** |
---|
| 1097 | *****************************************************************************/ |
---|
[127] | 1098 | void regfi_subkeylist_free(REGF_SUBKEY_LIST* list) |
---|
| 1099 | { |
---|
| 1100 | if(list != NULL) |
---|
| 1101 | { |
---|
| 1102 | free(list->elements); |
---|
| 1103 | free(list); |
---|
| 1104 | } |
---|
| 1105 | } |
---|
| 1106 | |
---|
| 1107 | |
---|
| 1108 | /****************************************************************************** |
---|
| 1109 | *****************************************************************************/ |
---|
[80] | 1110 | REGFI_ITERATOR* regfi_iterator_new(REGF_FILE* fh) |
---|
| 1111 | { |
---|
| 1112 | REGF_NK_REC* root; |
---|
| 1113 | REGFI_ITERATOR* ret_val = (REGFI_ITERATOR*)malloc(sizeof(REGFI_ITERATOR)); |
---|
| 1114 | if(ret_val == NULL) |
---|
| 1115 | return NULL; |
---|
| 1116 | |
---|
[81] | 1117 | root = regfi_rootkey(fh); |
---|
[80] | 1118 | if(root == NULL) |
---|
| 1119 | { |
---|
| 1120 | free(ret_val); |
---|
| 1121 | return NULL; |
---|
| 1122 | } |
---|
| 1123 | |
---|
| 1124 | ret_val->key_positions = void_stack_new(REGF_MAX_DEPTH); |
---|
| 1125 | if(ret_val->key_positions == NULL) |
---|
| 1126 | { |
---|
| 1127 | free(ret_val); |
---|
| 1128 | free(root); |
---|
| 1129 | return NULL; |
---|
| 1130 | } |
---|
| 1131 | |
---|
[116] | 1132 | /* This secret isn't very secret, but we don't need a good one. This |
---|
| 1133 | * secret is just designed to prevent someone from trying to blow our |
---|
| 1134 | * caching and make things slow. |
---|
| 1135 | */ |
---|
| 1136 | ret_val->sk_recs = lru_cache_create(127, 0x15DEAD05^time(NULL) |
---|
| 1137 | ^(getpid()<<16)^(getppid()<<8), |
---|
| 1138 | true); |
---|
[109] | 1139 | |
---|
[80] | 1140 | ret_val->f = fh; |
---|
| 1141 | ret_val->cur_key = root; |
---|
| 1142 | ret_val->cur_subkey = 0; |
---|
| 1143 | ret_val->cur_value = 0; |
---|
| 1144 | |
---|
| 1145 | return ret_val; |
---|
| 1146 | } |
---|
| 1147 | |
---|
| 1148 | |
---|
| 1149 | /****************************************************************************** |
---|
| 1150 | *****************************************************************************/ |
---|
| 1151 | void regfi_iterator_free(REGFI_ITERATOR* i) |
---|
| 1152 | { |
---|
| 1153 | REGFI_ITER_POSITION* cur; |
---|
| 1154 | |
---|
| 1155 | if(i->cur_key != NULL) |
---|
| 1156 | regfi_key_free(i->cur_key); |
---|
| 1157 | |
---|
| 1158 | while((cur = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions)) != NULL) |
---|
| 1159 | { |
---|
| 1160 | regfi_key_free(cur->nk); |
---|
| 1161 | free(cur); |
---|
| 1162 | } |
---|
| 1163 | |
---|
[109] | 1164 | lru_cache_destroy(i->sk_recs); |
---|
| 1165 | |
---|
[80] | 1166 | free(i); |
---|
| 1167 | } |
---|
| 1168 | |
---|
| 1169 | |
---|
| 1170 | |
---|
| 1171 | /****************************************************************************** |
---|
| 1172 | *****************************************************************************/ |
---|
| 1173 | /* XXX: some way of indicating reason for failure should be added. */ |
---|
| 1174 | bool regfi_iterator_down(REGFI_ITERATOR* i) |
---|
| 1175 | { |
---|
| 1176 | REGF_NK_REC* subkey; |
---|
| 1177 | REGFI_ITER_POSITION* pos; |
---|
| 1178 | |
---|
| 1179 | pos = (REGFI_ITER_POSITION*)malloc(sizeof(REGFI_ITER_POSITION)); |
---|
| 1180 | if(pos == NULL) |
---|
| 1181 | return false; |
---|
| 1182 | |
---|
[84] | 1183 | subkey = (REGF_NK_REC*)regfi_iterator_cur_subkey(i); |
---|
[80] | 1184 | if(subkey == NULL) |
---|
| 1185 | { |
---|
| 1186 | free(pos); |
---|
| 1187 | return false; |
---|
| 1188 | } |
---|
| 1189 | |
---|
| 1190 | pos->nk = i->cur_key; |
---|
| 1191 | pos->cur_subkey = i->cur_subkey; |
---|
| 1192 | if(!void_stack_push(i->key_positions, pos)) |
---|
| 1193 | { |
---|
| 1194 | free(pos); |
---|
| 1195 | regfi_key_free(subkey); |
---|
| 1196 | return false; |
---|
| 1197 | } |
---|
| 1198 | |
---|
| 1199 | i->cur_key = subkey; |
---|
| 1200 | i->cur_subkey = 0; |
---|
| 1201 | i->cur_value = 0; |
---|
| 1202 | |
---|
| 1203 | return true; |
---|
| 1204 | } |
---|
| 1205 | |
---|
| 1206 | |
---|
| 1207 | /****************************************************************************** |
---|
| 1208 | *****************************************************************************/ |
---|
| 1209 | bool regfi_iterator_up(REGFI_ITERATOR* i) |
---|
| 1210 | { |
---|
| 1211 | REGFI_ITER_POSITION* pos; |
---|
| 1212 | |
---|
| 1213 | pos = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions); |
---|
| 1214 | if(pos == NULL) |
---|
| 1215 | return false; |
---|
| 1216 | |
---|
| 1217 | regfi_key_free(i->cur_key); |
---|
| 1218 | i->cur_key = pos->nk; |
---|
| 1219 | i->cur_subkey = pos->cur_subkey; |
---|
| 1220 | i->cur_value = 0; |
---|
| 1221 | free(pos); |
---|
| 1222 | |
---|
| 1223 | return true; |
---|
| 1224 | } |
---|
| 1225 | |
---|
| 1226 | |
---|
| 1227 | /****************************************************************************** |
---|
| 1228 | *****************************************************************************/ |
---|
| 1229 | bool regfi_iterator_to_root(REGFI_ITERATOR* i) |
---|
| 1230 | { |
---|
| 1231 | while(regfi_iterator_up(i)) |
---|
| 1232 | continue; |
---|
| 1233 | |
---|
| 1234 | return true; |
---|
| 1235 | } |
---|
| 1236 | |
---|
| 1237 | |
---|
| 1238 | /****************************************************************************** |
---|
| 1239 | *****************************************************************************/ |
---|
| 1240 | bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* subkey_name) |
---|
| 1241 | { |
---|
| 1242 | REGF_NK_REC* subkey; |
---|
| 1243 | bool found = false; |
---|
| 1244 | uint32 old_subkey = i->cur_subkey; |
---|
[133] | 1245 | |
---|
[80] | 1246 | if(subkey_name == NULL) |
---|
| 1247 | return false; |
---|
| 1248 | |
---|
| 1249 | /* XXX: this alloc/free of each sub key might be a bit excessive */ |
---|
[84] | 1250 | subkey = (REGF_NK_REC*)regfi_iterator_first_subkey(i); |
---|
[80] | 1251 | while((subkey != NULL) && (found == false)) |
---|
| 1252 | { |
---|
| 1253 | if(subkey->keyname != NULL |
---|
| 1254 | && strcasecmp(subkey->keyname, subkey_name) == 0) |
---|
| 1255 | found = true; |
---|
[82] | 1256 | else |
---|
| 1257 | { |
---|
| 1258 | regfi_key_free(subkey); |
---|
[84] | 1259 | subkey = (REGF_NK_REC*)regfi_iterator_next_subkey(i); |
---|
[82] | 1260 | } |
---|
[80] | 1261 | } |
---|
| 1262 | |
---|
| 1263 | if(found == false) |
---|
| 1264 | { |
---|
| 1265 | i->cur_subkey = old_subkey; |
---|
| 1266 | return false; |
---|
| 1267 | } |
---|
| 1268 | |
---|
[82] | 1269 | regfi_key_free(subkey); |
---|
[80] | 1270 | return true; |
---|
| 1271 | } |
---|
| 1272 | |
---|
| 1273 | |
---|
| 1274 | /****************************************************************************** |
---|
| 1275 | *****************************************************************************/ |
---|
| 1276 | bool regfi_iterator_walk_path(REGFI_ITERATOR* i, const char** path) |
---|
| 1277 | { |
---|
| 1278 | uint32 x; |
---|
| 1279 | if(path == NULL) |
---|
| 1280 | return false; |
---|
| 1281 | |
---|
| 1282 | for(x=0; |
---|
| 1283 | ((path[x] != NULL) && regfi_iterator_find_subkey(i, path[x]) |
---|
| 1284 | && regfi_iterator_down(i)); |
---|
| 1285 | x++) |
---|
| 1286 | { continue; } |
---|
| 1287 | |
---|
| 1288 | if(path[x] == NULL) |
---|
| 1289 | return true; |
---|
| 1290 | |
---|
| 1291 | /* XXX: is this the right number of times? */ |
---|
| 1292 | for(; x > 0; x--) |
---|
| 1293 | regfi_iterator_up(i); |
---|
| 1294 | |
---|
| 1295 | return false; |
---|
| 1296 | } |
---|
| 1297 | |
---|
| 1298 | |
---|
| 1299 | /****************************************************************************** |
---|
| 1300 | *****************************************************************************/ |
---|
[84] | 1301 | const REGF_NK_REC* regfi_iterator_cur_key(REGFI_ITERATOR* i) |
---|
[80] | 1302 | { |
---|
| 1303 | return i->cur_key; |
---|
| 1304 | } |
---|
| 1305 | |
---|
| 1306 | |
---|
| 1307 | /****************************************************************************** |
---|
| 1308 | *****************************************************************************/ |
---|
[109] | 1309 | const REGF_SK_REC* regfi_iterator_cur_sk(REGFI_ITERATOR* i) |
---|
| 1310 | { |
---|
| 1311 | REGF_SK_REC* ret_val; |
---|
| 1312 | REGF_HBIN* hbin; |
---|
| 1313 | uint32 max_length, off; |
---|
| 1314 | |
---|
| 1315 | if(i->cur_key == NULL) |
---|
| 1316 | return NULL; |
---|
| 1317 | |
---|
| 1318 | /* First look if we have already parsed it */ |
---|
| 1319 | if((i->cur_key->sk_off!=REGF_OFFSET_NONE) |
---|
| 1320 | && !(ret_val =(REGF_SK_REC*)lru_cache_find(i->sk_recs, |
---|
| 1321 | &i->cur_key->sk_off, 4))) |
---|
| 1322 | { |
---|
| 1323 | hbin = regfi_lookup_hbin(i->f, i->cur_key->sk_off); |
---|
| 1324 | |
---|
| 1325 | if(hbin == NULL) |
---|
| 1326 | return NULL; |
---|
| 1327 | |
---|
| 1328 | off = i->cur_key->sk_off + REGF_BLOCKSIZE; |
---|
| 1329 | max_length = hbin->block_size + hbin->file_off - off; |
---|
| 1330 | ret_val = regfi_parse_sk(i->f, off, max_length, true); |
---|
| 1331 | if(ret_val == NULL) |
---|
| 1332 | return NULL; |
---|
| 1333 | |
---|
| 1334 | ret_val->sk_off = i->cur_key->sk_off; |
---|
| 1335 | lru_cache_update(i->sk_recs, &i->cur_key->sk_off, 4, ret_val); |
---|
| 1336 | } |
---|
| 1337 | |
---|
| 1338 | return ret_val; |
---|
| 1339 | } |
---|
| 1340 | |
---|
| 1341 | |
---|
| 1342 | |
---|
| 1343 | /****************************************************************************** |
---|
| 1344 | *****************************************************************************/ |
---|
[84] | 1345 | const REGF_NK_REC* regfi_iterator_first_subkey(REGFI_ITERATOR* i) |
---|
[80] | 1346 | { |
---|
| 1347 | i->cur_subkey = 0; |
---|
| 1348 | return regfi_iterator_cur_subkey(i); |
---|
| 1349 | } |
---|
| 1350 | |
---|
| 1351 | |
---|
| 1352 | /****************************************************************************** |
---|
| 1353 | *****************************************************************************/ |
---|
[84] | 1354 | const REGF_NK_REC* regfi_iterator_cur_subkey(REGFI_ITERATOR* i) |
---|
[80] | 1355 | { |
---|
| 1356 | uint32 nk_offset; |
---|
| 1357 | |
---|
[31] | 1358 | /* see if there is anything left to report */ |
---|
[80] | 1359 | if (!(i->cur_key) || (i->cur_key->subkeys_off==REGF_OFFSET_NONE) |
---|
| 1360 | || (i->cur_subkey >= i->cur_key->num_subkeys)) |
---|
[31] | 1361 | return NULL; |
---|
[30] | 1362 | |
---|
[127] | 1363 | nk_offset = i->cur_key->subkeys->elements[i->cur_subkey].nk_off; |
---|
[133] | 1364 | |
---|
[105] | 1365 | return regfi_load_key(i->f, nk_offset+REGF_BLOCKSIZE, true); |
---|
[30] | 1366 | } |
---|
[80] | 1367 | |
---|
| 1368 | |
---|
| 1369 | /****************************************************************************** |
---|
| 1370 | *****************************************************************************/ |
---|
| 1371 | /* XXX: some way of indicating reason for failure should be added. */ |
---|
[84] | 1372 | const REGF_NK_REC* regfi_iterator_next_subkey(REGFI_ITERATOR* i) |
---|
[80] | 1373 | { |
---|
[84] | 1374 | const REGF_NK_REC* subkey; |
---|
[80] | 1375 | |
---|
| 1376 | i->cur_subkey++; |
---|
| 1377 | subkey = regfi_iterator_cur_subkey(i); |
---|
| 1378 | |
---|
| 1379 | if(subkey == NULL) |
---|
| 1380 | i->cur_subkey--; |
---|
| 1381 | |
---|
| 1382 | return subkey; |
---|
| 1383 | } |
---|
| 1384 | |
---|
| 1385 | |
---|
| 1386 | /****************************************************************************** |
---|
| 1387 | *****************************************************************************/ |
---|
| 1388 | bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* value_name) |
---|
| 1389 | { |
---|
[84] | 1390 | const REGF_VK_REC* cur; |
---|
[80] | 1391 | bool found = false; |
---|
| 1392 | |
---|
| 1393 | /* XXX: cur->valuename can be NULL in the registry. |
---|
| 1394 | * Should we allow for a way to search for that? |
---|
| 1395 | */ |
---|
| 1396 | if(value_name == NULL) |
---|
| 1397 | return false; |
---|
| 1398 | |
---|
| 1399 | cur = regfi_iterator_first_value(i); |
---|
| 1400 | while((cur != NULL) && (found == false)) |
---|
| 1401 | { |
---|
| 1402 | if((cur->valuename != NULL) |
---|
| 1403 | && (strcasecmp(cur->valuename, value_name) == 0)) |
---|
| 1404 | found = true; |
---|
[95] | 1405 | else |
---|
| 1406 | cur = regfi_iterator_next_value(i); |
---|
[80] | 1407 | } |
---|
| 1408 | |
---|
[94] | 1409 | return found; |
---|
[80] | 1410 | } |
---|
| 1411 | |
---|
| 1412 | |
---|
| 1413 | /****************************************************************************** |
---|
| 1414 | *****************************************************************************/ |
---|
[84] | 1415 | const REGF_VK_REC* regfi_iterator_first_value(REGFI_ITERATOR* i) |
---|
[80] | 1416 | { |
---|
| 1417 | i->cur_value = 0; |
---|
| 1418 | return regfi_iterator_cur_value(i); |
---|
| 1419 | } |
---|
| 1420 | |
---|
| 1421 | |
---|
| 1422 | /****************************************************************************** |
---|
| 1423 | *****************************************************************************/ |
---|
[84] | 1424 | const REGF_VK_REC* regfi_iterator_cur_value(REGFI_ITERATOR* i) |
---|
[80] | 1425 | { |
---|
| 1426 | REGF_VK_REC* ret_val = NULL; |
---|
| 1427 | if(i->cur_value < i->cur_key->num_values) |
---|
[101] | 1428 | ret_val = i->cur_key->values[i->cur_value]; |
---|
[80] | 1429 | |
---|
| 1430 | return ret_val; |
---|
| 1431 | } |
---|
| 1432 | |
---|
| 1433 | |
---|
| 1434 | /****************************************************************************** |
---|
| 1435 | *****************************************************************************/ |
---|
[84] | 1436 | const REGF_VK_REC* regfi_iterator_next_value(REGFI_ITERATOR* i) |
---|
[80] | 1437 | { |
---|
[84] | 1438 | const REGF_VK_REC* ret_val; |
---|
[80] | 1439 | |
---|
| 1440 | i->cur_value++; |
---|
| 1441 | ret_val = regfi_iterator_cur_value(i); |
---|
| 1442 | if(ret_val == NULL) |
---|
| 1443 | i->cur_value--; |
---|
| 1444 | |
---|
| 1445 | return ret_val; |
---|
| 1446 | } |
---|
[97] | 1447 | |
---|
| 1448 | |
---|
| 1449 | |
---|
| 1450 | /******************************************************************* |
---|
| 1451 | * Computes the checksum of the registry file header. |
---|
| 1452 | * buffer must be at least the size of an regf header (4096 bytes). |
---|
| 1453 | *******************************************************************/ |
---|
| 1454 | static uint32 regfi_compute_header_checksum(uint8* buffer) |
---|
| 1455 | { |
---|
| 1456 | uint32 checksum, x; |
---|
| 1457 | int i; |
---|
| 1458 | |
---|
| 1459 | /* XOR of all bytes 0x0000 - 0x01FB */ |
---|
| 1460 | |
---|
| 1461 | checksum = x = 0; |
---|
| 1462 | |
---|
| 1463 | for ( i=0; i<0x01FB; i+=4 ) { |
---|
| 1464 | x = IVAL(buffer, i ); |
---|
| 1465 | checksum ^= x; |
---|
| 1466 | } |
---|
| 1467 | |
---|
| 1468 | return checksum; |
---|
| 1469 | } |
---|
| 1470 | |
---|
| 1471 | |
---|
| 1472 | /******************************************************************* |
---|
[116] | 1473 | * XXX: Add way to return more detailed error information. |
---|
[97] | 1474 | *******************************************************************/ |
---|
| 1475 | REGF_FILE* regfi_parse_regf(int fd, bool strict) |
---|
| 1476 | { |
---|
| 1477 | uint8 file_header[REGF_BLOCKSIZE]; |
---|
[102] | 1478 | uint32 length; |
---|
[97] | 1479 | uint32 file_length; |
---|
| 1480 | struct stat sbuf; |
---|
| 1481 | REGF_FILE* ret_val; |
---|
| 1482 | |
---|
| 1483 | /* Determine file length. Must be at least big enough |
---|
| 1484 | * for the header and one hbin. |
---|
| 1485 | */ |
---|
| 1486 | if (fstat(fd, &sbuf) == -1) |
---|
| 1487 | return NULL; |
---|
| 1488 | file_length = sbuf.st_size; |
---|
| 1489 | if(file_length < REGF_BLOCKSIZE+REGF_ALLOC_BLOCK) |
---|
| 1490 | return NULL; |
---|
| 1491 | |
---|
| 1492 | ret_val = (REGF_FILE*)zalloc(sizeof(REGF_FILE)); |
---|
| 1493 | if(ret_val == NULL) |
---|
| 1494 | return NULL; |
---|
| 1495 | |
---|
| 1496 | ret_val->fd = fd; |
---|
| 1497 | ret_val->file_length = file_length; |
---|
| 1498 | |
---|
| 1499 | length = REGF_BLOCKSIZE; |
---|
[102] | 1500 | if((regfi_read(fd, file_header, &length)) != 0 |
---|
[97] | 1501 | || length != REGF_BLOCKSIZE) |
---|
| 1502 | { |
---|
| 1503 | free(ret_val); |
---|
| 1504 | return NULL; |
---|
| 1505 | } |
---|
| 1506 | |
---|
| 1507 | ret_val->checksum = IVAL(file_header, 0x1FC); |
---|
| 1508 | ret_val->computed_checksum = regfi_compute_header_checksum(file_header); |
---|
| 1509 | if (strict && (ret_val->checksum != ret_val->computed_checksum)) |
---|
| 1510 | { |
---|
| 1511 | free(ret_val); |
---|
| 1512 | return NULL; |
---|
| 1513 | } |
---|
| 1514 | |
---|
| 1515 | memcpy(ret_val->magic, file_header, 4); |
---|
| 1516 | if(strict && (memcmp(ret_val->magic, "regf", 4) != 0)) |
---|
| 1517 | { |
---|
| 1518 | free(ret_val); |
---|
| 1519 | return NULL; |
---|
| 1520 | } |
---|
| 1521 | |
---|
| 1522 | ret_val->unknown1 = IVAL(file_header, 0x4); |
---|
| 1523 | ret_val->unknown2 = IVAL(file_header, 0x8); |
---|
| 1524 | |
---|
| 1525 | ret_val->mtime.low = IVAL(file_header, 0xC); |
---|
| 1526 | ret_val->mtime.high = IVAL(file_header, 0x10); |
---|
| 1527 | |
---|
| 1528 | ret_val->unknown3 = IVAL(file_header, 0x14); |
---|
| 1529 | ret_val->unknown4 = IVAL(file_header, 0x18); |
---|
| 1530 | ret_val->unknown5 = IVAL(file_header, 0x1C); |
---|
| 1531 | ret_val->unknown6 = IVAL(file_header, 0x20); |
---|
| 1532 | |
---|
| 1533 | ret_val->data_offset = IVAL(file_header, 0x24); |
---|
| 1534 | ret_val->last_block = IVAL(file_header, 0x28); |
---|
| 1535 | |
---|
| 1536 | ret_val->unknown7 = IVAL(file_header, 0x2C); |
---|
| 1537 | |
---|
| 1538 | return ret_val; |
---|
| 1539 | } |
---|
| 1540 | |
---|
| 1541 | |
---|
| 1542 | |
---|
| 1543 | /******************************************************************* |
---|
| 1544 | * Given real file offset, read and parse the hbin at that location |
---|
[110] | 1545 | * along with it's associated cells. |
---|
[97] | 1546 | *******************************************************************/ |
---|
[116] | 1547 | /* XXX: Need a way to return types of errors. |
---|
[97] | 1548 | */ |
---|
[110] | 1549 | REGF_HBIN* regfi_parse_hbin(REGF_FILE* file, uint32 offset, bool strict) |
---|
[97] | 1550 | { |
---|
| 1551 | REGF_HBIN *hbin; |
---|
| 1552 | uint8 hbin_header[HBIN_HEADER_REC_SIZE]; |
---|
[110] | 1553 | uint32 length; |
---|
[99] | 1554 | |
---|
| 1555 | if(offset >= file->file_length) |
---|
| 1556 | return NULL; |
---|
[97] | 1557 | |
---|
| 1558 | if(lseek(file->fd, offset, SEEK_SET) == -1) |
---|
| 1559 | return NULL; |
---|
| 1560 | |
---|
| 1561 | length = HBIN_HEADER_REC_SIZE; |
---|
| 1562 | if((regfi_read(file->fd, hbin_header, &length) != 0) |
---|
| 1563 | || length != HBIN_HEADER_REC_SIZE) |
---|
| 1564 | return NULL; |
---|
| 1565 | |
---|
[99] | 1566 | |
---|
[97] | 1567 | if(lseek(file->fd, offset, SEEK_SET) == -1) |
---|
| 1568 | return NULL; |
---|
| 1569 | |
---|
[99] | 1570 | if(!(hbin = (REGF_HBIN*)zalloc(sizeof(REGF_HBIN)))) |
---|
| 1571 | return NULL; |
---|
| 1572 | hbin->file_off = offset; |
---|
| 1573 | |
---|
[97] | 1574 | memcpy(hbin->magic, hbin_header, 4); |
---|
| 1575 | if(strict && (memcmp(hbin->magic, "hbin", 4) != 0)) |
---|
[99] | 1576 | { |
---|
| 1577 | free(hbin); |
---|
[97] | 1578 | return NULL; |
---|
[99] | 1579 | } |
---|
[97] | 1580 | |
---|
| 1581 | hbin->first_hbin_off = IVAL(hbin_header, 0x4); |
---|
| 1582 | hbin->block_size = IVAL(hbin_header, 0x8); |
---|
| 1583 | /* this should be the same thing as hbin->block_size but just in case */ |
---|
| 1584 | hbin->next_block = IVAL(hbin_header, 0x1C); |
---|
| 1585 | |
---|
| 1586 | |
---|
| 1587 | /* Ensure the block size is a multiple of 0x1000 and doesn't run off |
---|
| 1588 | * the end of the file. |
---|
| 1589 | */ |
---|
[116] | 1590 | /* XXX: This may need to be relaxed for dealing with |
---|
| 1591 | * partial or corrupt files. |
---|
| 1592 | */ |
---|
[97] | 1593 | if((offset + hbin->block_size > file->file_length) |
---|
| 1594 | || (hbin->block_size & 0xFFFFF000) != hbin->block_size) |
---|
[99] | 1595 | { |
---|
| 1596 | free(hbin); |
---|
[97] | 1597 | return NULL; |
---|
[99] | 1598 | } |
---|
[97] | 1599 | |
---|
| 1600 | return hbin; |
---|
| 1601 | } |
---|
| 1602 | |
---|
| 1603 | |
---|
[126] | 1604 | /******************************************************************* |
---|
| 1605 | *******************************************************************/ |
---|
[99] | 1606 | REGF_NK_REC* regfi_parse_nk(REGF_FILE* file, uint32 offset, |
---|
| 1607 | uint32 max_size, bool strict) |
---|
| 1608 | { |
---|
| 1609 | uint8 nk_header[REGFI_NK_MIN_LENGTH]; |
---|
[131] | 1610 | REGF_HBIN *hbin; |
---|
[99] | 1611 | REGF_NK_REC* ret_val; |
---|
[131] | 1612 | uint32 length,cell_length; |
---|
| 1613 | uint32 class_offset, class_maxsize; |
---|
[101] | 1614 | bool unalloc = false; |
---|
[99] | 1615 | |
---|
[101] | 1616 | if(!regfi_parse_cell(file->fd, offset, nk_header, REGFI_NK_MIN_LENGTH, |
---|
| 1617 | &cell_length, &unalloc)) |
---|
| 1618 | return NULL; |
---|
[99] | 1619 | |
---|
| 1620 | /* A bit of validation before bothering to allocate memory */ |
---|
[101] | 1621 | if((nk_header[0x0] != 'n') || (nk_header[0x1] != 'k')) |
---|
[99] | 1622 | return NULL; |
---|
| 1623 | |
---|
| 1624 | ret_val = (REGF_NK_REC*)zalloc(sizeof(REGF_NK_REC)); |
---|
| 1625 | if(ret_val == NULL) |
---|
| 1626 | return NULL; |
---|
| 1627 | |
---|
| 1628 | ret_val->offset = offset; |
---|
[101] | 1629 | ret_val->cell_size = cell_length; |
---|
| 1630 | |
---|
[99] | 1631 | if(ret_val->cell_size > max_size) |
---|
| 1632 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
| 1633 | if((ret_val->cell_size < REGFI_NK_MIN_LENGTH) |
---|
| 1634 | || (strict && ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8))) |
---|
| 1635 | { |
---|
| 1636 | free(ret_val); |
---|
| 1637 | return NULL; |
---|
| 1638 | } |
---|
| 1639 | |
---|
[101] | 1640 | ret_val->magic[0] = nk_header[0x0]; |
---|
| 1641 | ret_val->magic[1] = nk_header[0x1]; |
---|
| 1642 | ret_val->key_type = SVAL(nk_header, 0x2); |
---|
| 1643 | if((ret_val->key_type != NK_TYPE_NORMALKEY) |
---|
[129] | 1644 | && (ret_val->key_type != NK_TYPE_ROOTKEY1) |
---|
| 1645 | && (ret_val->key_type != NK_TYPE_ROOTKEY2) |
---|
[101] | 1646 | && (ret_val->key_type != NK_TYPE_LINKKEY) |
---|
| 1647 | && (ret_val->key_type != NK_TYPE_UNKNOWN1)) |
---|
[99] | 1648 | { |
---|
| 1649 | free(ret_val); |
---|
| 1650 | return NULL; |
---|
| 1651 | } |
---|
[101] | 1652 | |
---|
| 1653 | ret_val->mtime.low = IVAL(nk_header, 0x4); |
---|
| 1654 | ret_val->mtime.high = IVAL(nk_header, 0x8); |
---|
[116] | 1655 | /* If the key is unallocated and the MTIME is earlier than Jan 1, 1990 |
---|
| 1656 | * or later than Jan 1, 2290, we consider this a bad key. This helps |
---|
| 1657 | * weed out some false positives during deleted data recovery. |
---|
| 1658 | */ |
---|
| 1659 | if(unalloc |
---|
| 1660 | && ((ret_val->mtime.high < REGFI_MTIME_MIN_HIGH |
---|
| 1661 | && ret_val->mtime.low < REGFI_MTIME_MIN_LOW) |
---|
| 1662 | || (ret_val->mtime.high > REGFI_MTIME_MAX_HIGH |
---|
| 1663 | && ret_val->mtime.low > REGFI_MTIME_MAX_LOW))) |
---|
| 1664 | return NULL; |
---|
| 1665 | |
---|
[101] | 1666 | ret_val->unknown1 = IVAL(nk_header, 0xC); |
---|
| 1667 | ret_val->parent_off = IVAL(nk_header, 0x10); |
---|
| 1668 | ret_val->num_subkeys = IVAL(nk_header, 0x14); |
---|
| 1669 | ret_val->unknown2 = IVAL(nk_header, 0x18); |
---|
| 1670 | ret_val->subkeys_off = IVAL(nk_header, 0x1C); |
---|
| 1671 | ret_val->unknown3 = IVAL(nk_header, 0x20); |
---|
| 1672 | ret_val->num_values = IVAL(nk_header, 0x24); |
---|
| 1673 | ret_val->values_off = IVAL(nk_header, 0x28); |
---|
| 1674 | ret_val->sk_off = IVAL(nk_header, 0x2C); |
---|
| 1675 | ret_val->classname_off = IVAL(nk_header, 0x30); |
---|
[99] | 1676 | |
---|
[101] | 1677 | ret_val->max_bytes_subkeyname = IVAL(nk_header, 0x34); |
---|
| 1678 | ret_val->max_bytes_subkeyclassname = IVAL(nk_header, 0x38); |
---|
| 1679 | ret_val->max_bytes_valuename = IVAL(nk_header, 0x3C); |
---|
| 1680 | ret_val->max_bytes_value = IVAL(nk_header, 0x40); |
---|
| 1681 | ret_val->unk_index = IVAL(nk_header, 0x44); |
---|
[99] | 1682 | |
---|
[101] | 1683 | ret_val->name_length = SVAL(nk_header, 0x48); |
---|
| 1684 | ret_val->classname_length = SVAL(nk_header, 0x4A); |
---|
[99] | 1685 | |
---|
[125] | 1686 | |
---|
[99] | 1687 | if(ret_val->name_length + REGFI_NK_MIN_LENGTH > ret_val->cell_size) |
---|
[101] | 1688 | { |
---|
| 1689 | if(strict) |
---|
| 1690 | { |
---|
| 1691 | free(ret_val); |
---|
| 1692 | return NULL; |
---|
| 1693 | } |
---|
| 1694 | else |
---|
| 1695 | ret_val->name_length = ret_val->cell_size - REGFI_NK_MIN_LENGTH; |
---|
| 1696 | } |
---|
| 1697 | else if (unalloc) |
---|
| 1698 | { /* Truncate cell_size if it's much larger than the apparent total record length. */ |
---|
| 1699 | /* Round up to the next multiple of 8 */ |
---|
| 1700 | length = (ret_val->name_length + REGFI_NK_MIN_LENGTH) & 0xFFFFFFF8; |
---|
| 1701 | if(length < ret_val->name_length + REGFI_NK_MIN_LENGTH) |
---|
| 1702 | length+=8; |
---|
[99] | 1703 | |
---|
[101] | 1704 | /* If cell_size is still greater, truncate. */ |
---|
| 1705 | if(length < ret_val->cell_size) |
---|
| 1706 | ret_val->cell_size = length; |
---|
| 1707 | } |
---|
| 1708 | |
---|
[99] | 1709 | ret_val->keyname = (char*)zalloc(sizeof(char)*(ret_val->name_length+1)); |
---|
| 1710 | if(ret_val->keyname == NULL) |
---|
| 1711 | { |
---|
| 1712 | free(ret_val); |
---|
| 1713 | return NULL; |
---|
| 1714 | } |
---|
| 1715 | |
---|
| 1716 | /* Don't need to seek, should be at the right offset */ |
---|
| 1717 | length = ret_val->name_length; |
---|
[101] | 1718 | if((regfi_read(file->fd, (uint8*)ret_val->keyname, &length) != 0) |
---|
[99] | 1719 | || length != ret_val->name_length) |
---|
| 1720 | { |
---|
| 1721 | free(ret_val->keyname); |
---|
| 1722 | free(ret_val); |
---|
| 1723 | return NULL; |
---|
| 1724 | } |
---|
| 1725 | ret_val->keyname[ret_val->name_length] = '\0'; |
---|
| 1726 | |
---|
[126] | 1727 | if(ret_val->classname_off != REGF_OFFSET_NONE) |
---|
| 1728 | { |
---|
[131] | 1729 | hbin = regfi_lookup_hbin(file, ret_val->classname_off); |
---|
| 1730 | if(hbin) |
---|
| 1731 | { |
---|
| 1732 | class_offset = ret_val->classname_off+REGF_BLOCKSIZE; |
---|
| 1733 | class_maxsize = hbin->block_size + hbin->file_off - class_offset; |
---|
| 1734 | ret_val->classname |
---|
| 1735 | = regfi_parse_classname(file, class_offset, &ret_val->classname_length, |
---|
| 1736 | class_maxsize, strict); |
---|
| 1737 | } |
---|
| 1738 | else |
---|
| 1739 | ret_val->classname = NULL; |
---|
[126] | 1740 | /* |
---|
| 1741 | if(strict && ret_val->classname == NULL) |
---|
| 1742 | return NULL; |
---|
| 1743 | */ |
---|
| 1744 | } |
---|
[125] | 1745 | |
---|
[126] | 1746 | return ret_val; |
---|
| 1747 | } |
---|
| 1748 | |
---|
| 1749 | |
---|
| 1750 | char* regfi_parse_classname(REGF_FILE* file, uint32 offset, |
---|
[131] | 1751 | uint16* name_length, uint32 max_size, bool strict) |
---|
[126] | 1752 | { |
---|
| 1753 | char* ret_val = NULL; |
---|
| 1754 | uint32 length; |
---|
| 1755 | uint32 cell_length; |
---|
| 1756 | bool unalloc = false; |
---|
| 1757 | |
---|
| 1758 | if(*name_length > 0 && offset != REGF_OFFSET_NONE |
---|
| 1759 | && offset == (offset & 0xFFFFFFF8)) |
---|
[131] | 1760 | { |
---|
[126] | 1761 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, &cell_length, &unalloc)) |
---|
| 1762 | return NULL; |
---|
| 1763 | |
---|
[131] | 1764 | if((cell_length & 0xFFFFFFF8) != cell_length) |
---|
| 1765 | return NULL; |
---|
| 1766 | |
---|
| 1767 | if(cell_length > max_size) |
---|
[125] | 1768 | { |
---|
[126] | 1769 | if(strict) |
---|
| 1770 | return NULL; |
---|
[131] | 1771 | cell_length = max_size; |
---|
[126] | 1772 | } |
---|
[131] | 1773 | |
---|
| 1774 | if((cell_length - 4) < *name_length) |
---|
| 1775 | { |
---|
| 1776 | if(strict) |
---|
| 1777 | return NULL; |
---|
| 1778 | *name_length = cell_length - 4; |
---|
| 1779 | } |
---|
[126] | 1780 | |
---|
| 1781 | ret_val = (char*)zalloc(*name_length); |
---|
| 1782 | if(ret_val != NULL) |
---|
| 1783 | { |
---|
| 1784 | length = *name_length; |
---|
| 1785 | if((regfi_read(file->fd, (uint8*)ret_val, &length) != 0) |
---|
| 1786 | || length != *name_length) |
---|
[125] | 1787 | { |
---|
[126] | 1788 | free(ret_val); |
---|
| 1789 | return NULL; |
---|
[125] | 1790 | } |
---|
| 1791 | |
---|
| 1792 | /*printf("==> cell_length=%d, classname_length=%d, max_bytes_subkeyclassname=%d\n", cell_length, ret_val->classname_length, ret_val->max_bytes_subkeyclassname);*/ |
---|
| 1793 | } |
---|
| 1794 | } |
---|
| 1795 | |
---|
[99] | 1796 | return ret_val; |
---|
| 1797 | } |
---|
| 1798 | |
---|
| 1799 | |
---|
[101] | 1800 | /******************************************************************* |
---|
| 1801 | *******************************************************************/ |
---|
| 1802 | REGF_VK_REC* regfi_parse_vk(REGF_FILE* file, uint32 offset, |
---|
| 1803 | uint32 max_size, bool strict) |
---|
[97] | 1804 | { |
---|
[101] | 1805 | REGF_VK_REC* ret_val; |
---|
[131] | 1806 | REGF_HBIN *hbin; |
---|
[101] | 1807 | uint8 vk_header[REGFI_VK_MIN_LENGTH]; |
---|
| 1808 | uint32 raw_data_size, length, cell_length; |
---|
[131] | 1809 | uint32 data_offset, data_maxsize; |
---|
[101] | 1810 | bool unalloc = false; |
---|
[97] | 1811 | |
---|
[101] | 1812 | if(!regfi_parse_cell(file->fd, offset, vk_header, REGFI_VK_MIN_LENGTH, |
---|
| 1813 | &cell_length, &unalloc)) |
---|
| 1814 | return NULL; |
---|
[111] | 1815 | |
---|
[101] | 1816 | ret_val = (REGF_VK_REC*)zalloc(sizeof(REGF_VK_REC)); |
---|
| 1817 | if(ret_val == NULL) |
---|
| 1818 | return NULL; |
---|
| 1819 | |
---|
| 1820 | ret_val->offset = offset; |
---|
| 1821 | ret_val->cell_size = cell_length; |
---|
| 1822 | |
---|
| 1823 | if(ret_val->cell_size > max_size) |
---|
| 1824 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
| 1825 | if((ret_val->cell_size < REGFI_VK_MIN_LENGTH) |
---|
[111] | 1826 | || ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8)) |
---|
[97] | 1827 | { |
---|
[101] | 1828 | free(ret_val); |
---|
| 1829 | return NULL; |
---|
| 1830 | } |
---|
[97] | 1831 | |
---|
[101] | 1832 | ret_val->magic[0] = vk_header[0x0]; |
---|
| 1833 | ret_val->magic[1] = vk_header[0x1]; |
---|
| 1834 | if((ret_val->magic[0] != 'v') || (ret_val->magic[1] != 'k')) |
---|
| 1835 | { |
---|
[124] | 1836 | /* XXX: This does not account for deleted keys under Win2K which |
---|
| 1837 | * often have this (and the name length) overwritten with |
---|
| 1838 | * 0xFFFF. |
---|
| 1839 | */ |
---|
[101] | 1840 | free(ret_val); |
---|
| 1841 | return NULL; |
---|
| 1842 | } |
---|
| 1843 | |
---|
| 1844 | ret_val->name_length = SVAL(vk_header, 0x2); |
---|
| 1845 | raw_data_size = IVAL(vk_header, 0x4); |
---|
| 1846 | ret_val->data_size = raw_data_size & ~VK_DATA_IN_OFFSET; |
---|
[111] | 1847 | ret_val->data_in_offset = (bool)(raw_data_size & VK_DATA_IN_OFFSET); |
---|
[101] | 1848 | ret_val->data_off = IVAL(vk_header, 0x8); |
---|
| 1849 | ret_val->type = IVAL(vk_header, 0xC); |
---|
| 1850 | ret_val->flag = SVAL(vk_header, 0x10); |
---|
| 1851 | ret_val->unknown1 = SVAL(vk_header, 0x12); |
---|
| 1852 | |
---|
| 1853 | if(ret_val->flag & VK_FLAG_NAME_PRESENT) |
---|
| 1854 | { |
---|
[113] | 1855 | if(ret_val->name_length + REGFI_VK_MIN_LENGTH + 4 > ret_val->cell_size) |
---|
[101] | 1856 | { |
---|
| 1857 | if(strict) |
---|
| 1858 | { |
---|
| 1859 | free(ret_val); |
---|
| 1860 | return NULL; |
---|
| 1861 | } |
---|
| 1862 | else |
---|
[113] | 1863 | ret_val->name_length = ret_val->cell_size - REGFI_VK_MIN_LENGTH - 4; |
---|
[101] | 1864 | } |
---|
| 1865 | |
---|
| 1866 | /* Round up to the next multiple of 8 */ |
---|
[113] | 1867 | cell_length = (ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) & 0xFFFFFFF8; |
---|
| 1868 | if(cell_length < ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) |
---|
| 1869 | cell_length+=8; |
---|
[101] | 1870 | |
---|
| 1871 | ret_val->valuename = (char*)zalloc(sizeof(char)*(ret_val->name_length+1)); |
---|
| 1872 | if(ret_val->valuename == NULL) |
---|
| 1873 | { |
---|
| 1874 | free(ret_val); |
---|
| 1875 | return NULL; |
---|
| 1876 | } |
---|
[113] | 1877 | |
---|
[101] | 1878 | length = ret_val->name_length; |
---|
| 1879 | if((regfi_read(file->fd, (uint8*)ret_val->valuename, &length) != 0) |
---|
| 1880 | || length != ret_val->name_length) |
---|
| 1881 | { |
---|
| 1882 | free(ret_val->valuename); |
---|
| 1883 | free(ret_val); |
---|
| 1884 | return NULL; |
---|
| 1885 | } |
---|
| 1886 | ret_val->valuename[ret_val->name_length] = '\0'; |
---|
[133] | 1887 | |
---|
[101] | 1888 | } |
---|
| 1889 | else |
---|
[113] | 1890 | cell_length = REGFI_VK_MIN_LENGTH + 4; |
---|
[101] | 1891 | |
---|
| 1892 | if(unalloc) |
---|
| 1893 | { |
---|
| 1894 | /* If cell_size is still greater, truncate. */ |
---|
[113] | 1895 | if(cell_length < ret_val->cell_size) |
---|
| 1896 | ret_val->cell_size = cell_length; |
---|
[101] | 1897 | } |
---|
| 1898 | |
---|
| 1899 | if(ret_val->data_size == 0) |
---|
| 1900 | ret_val->data = NULL; |
---|
| 1901 | else |
---|
| 1902 | { |
---|
[133] | 1903 | if(ret_val->data_in_offset) |
---|
[131] | 1904 | { |
---|
[133] | 1905 | ret_val->data = regfi_parse_data(file, data_offset, |
---|
| 1906 | raw_data_size, 4, strict); |
---|
[131] | 1907 | } |
---|
| 1908 | else |
---|
[133] | 1909 | { |
---|
| 1910 | hbin = regfi_lookup_hbin(file, ret_val->data_off); |
---|
| 1911 | if(hbin) |
---|
| 1912 | { |
---|
| 1913 | data_offset = ret_val->data_off+REGF_BLOCKSIZE; |
---|
| 1914 | data_maxsize = hbin->block_size + hbin->file_off - data_offset; |
---|
| 1915 | ret_val->data = regfi_parse_data(file, data_offset, raw_data_size, |
---|
| 1916 | data_maxsize, strict); |
---|
| 1917 | |
---|
| 1918 | } |
---|
| 1919 | else |
---|
| 1920 | ret_val->data = NULL; |
---|
| 1921 | } |
---|
[131] | 1922 | |
---|
[101] | 1923 | if(strict && (ret_val->data == NULL)) |
---|
| 1924 | { |
---|
| 1925 | free(ret_val->valuename); |
---|
| 1926 | free(ret_val); |
---|
| 1927 | return NULL; |
---|
| 1928 | } |
---|
| 1929 | } |
---|
| 1930 | |
---|
| 1931 | return ret_val; |
---|
[97] | 1932 | } |
---|
[101] | 1933 | |
---|
| 1934 | |
---|
[131] | 1935 | uint8* regfi_parse_data(REGF_FILE* file, uint32 offset, uint32 length, |
---|
| 1936 | uint32 max_size, bool strict) |
---|
[101] | 1937 | { |
---|
| 1938 | uint8* ret_val; |
---|
| 1939 | uint32 read_length, cell_length; |
---|
[102] | 1940 | uint8 i; |
---|
[101] | 1941 | bool unalloc; |
---|
| 1942 | |
---|
| 1943 | /* The data is stored in the offset if the size <= 4 */ |
---|
[102] | 1944 | if (length & VK_DATA_IN_OFFSET) |
---|
[101] | 1945 | { |
---|
| 1946 | length = length & ~VK_DATA_IN_OFFSET; |
---|
| 1947 | if(length > 4) |
---|
| 1948 | return NULL; |
---|
| 1949 | |
---|
| 1950 | if((ret_val = (uint8*)zalloc(sizeof(uint8)*length)) == NULL) |
---|
| 1951 | return NULL; |
---|
[102] | 1952 | |
---|
| 1953 | offset = offset - REGF_BLOCKSIZE; |
---|
| 1954 | for(i = 0; i < length; i++) |
---|
| 1955 | ret_val[i] = (uint8)((offset >> i*8) & 0xFF); |
---|
[101] | 1956 | } |
---|
| 1957 | else |
---|
| 1958 | { |
---|
| 1959 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, |
---|
| 1960 | &cell_length, &unalloc)) |
---|
| 1961 | return NULL; |
---|
[111] | 1962 | |
---|
| 1963 | if((cell_length & 0xFFFFFFF8) != cell_length) |
---|
[101] | 1964 | return NULL; |
---|
| 1965 | |
---|
[131] | 1966 | if(cell_length > max_size) |
---|
| 1967 | { |
---|
| 1968 | if(strict) |
---|
| 1969 | return NULL; |
---|
| 1970 | else |
---|
| 1971 | cell_length = max_size; |
---|
| 1972 | } |
---|
| 1973 | |
---|
[101] | 1974 | if(cell_length - 4 < length) |
---|
| 1975 | { |
---|
[116] | 1976 | /* XXX: This strict condition has been triggered in multiple registries. |
---|
| 1977 | * Not sure the cause, but the data length values are very large, |
---|
| 1978 | * such as 53392. |
---|
[111] | 1979 | */ |
---|
[101] | 1980 | if(strict) |
---|
| 1981 | return NULL; |
---|
| 1982 | else |
---|
| 1983 | length = cell_length - 4; |
---|
| 1984 | } |
---|
| 1985 | |
---|
| 1986 | if((ret_val = (uint8*)zalloc(sizeof(uint8)*length)) == NULL) |
---|
| 1987 | return NULL; |
---|
| 1988 | |
---|
| 1989 | read_length = length; |
---|
| 1990 | if((regfi_read(file->fd, ret_val, &read_length) != 0) |
---|
| 1991 | || read_length != length) |
---|
| 1992 | { |
---|
| 1993 | free(ret_val); |
---|
| 1994 | return NULL; |
---|
| 1995 | } |
---|
| 1996 | } |
---|
| 1997 | |
---|
| 1998 | return ret_val; |
---|
| 1999 | } |
---|
[110] | 2000 | |
---|
| 2001 | |
---|
| 2002 | range_list* regfi_parse_unalloc_cells(REGF_FILE* file) |
---|
| 2003 | { |
---|
| 2004 | range_list* ret_val; |
---|
| 2005 | REGF_HBIN* hbin; |
---|
| 2006 | const range_list_element* hbins_elem; |
---|
| 2007 | uint32 i, num_hbins, curr_off, cell_len; |
---|
| 2008 | bool is_unalloc; |
---|
| 2009 | |
---|
| 2010 | ret_val = range_list_new(); |
---|
| 2011 | if(ret_val == NULL) |
---|
| 2012 | return NULL; |
---|
| 2013 | |
---|
| 2014 | num_hbins = range_list_size(file->hbins); |
---|
| 2015 | for(i=0; i<num_hbins; i++) |
---|
| 2016 | { |
---|
| 2017 | hbins_elem = range_list_get(file->hbins, i); |
---|
| 2018 | if(hbins_elem == NULL) |
---|
| 2019 | break; |
---|
| 2020 | hbin = (REGF_HBIN*)hbins_elem->data; |
---|
| 2021 | |
---|
| 2022 | curr_off = HBIN_HEADER_REC_SIZE; |
---|
| 2023 | while(curr_off < hbin->block_size) |
---|
| 2024 | { |
---|
| 2025 | if(!regfi_parse_cell(file->fd, hbin->file_off+curr_off, NULL, 0, |
---|
| 2026 | &cell_len, &is_unalloc)) |
---|
| 2027 | break; |
---|
| 2028 | |
---|
[113] | 2029 | if((cell_len == 0) || ((cell_len & 0xFFFFFFF8) != cell_len)) |
---|
[116] | 2030 | /* XXX: should report an error here. */ |
---|
[110] | 2031 | break; |
---|
| 2032 | |
---|
| 2033 | /* for some reason the record_size of the last record in |
---|
| 2034 | an hbin block can extend past the end of the block |
---|
| 2035 | even though the record fits within the remaining |
---|
| 2036 | space....aaarrrgggghhhhhh */ |
---|
| 2037 | if(curr_off + cell_len >= hbin->block_size) |
---|
| 2038 | cell_len = hbin->block_size - curr_off; |
---|
| 2039 | |
---|
| 2040 | if(is_unalloc) |
---|
| 2041 | range_list_add(ret_val, hbin->file_off+curr_off, |
---|
| 2042 | cell_len, NULL); |
---|
| 2043 | |
---|
| 2044 | curr_off = curr_off+cell_len; |
---|
| 2045 | } |
---|
| 2046 | } |
---|
| 2047 | |
---|
| 2048 | return ret_val; |
---|
| 2049 | } |
---|