[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. |
---|
| 6 | * Windows NT registry I/O library |
---|
| 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 |
---|
| 13 | * the Free Software Foundation; version 2 of the License. |
---|
| 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 101 2008-03-29 00:46:37Z 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 | *****************************************************************************/ |
---|
| 374 | static bool regfi_parse_cell(int fd, uint32 offset, uint8* hdr, uint32 hdr_len, |
---|
| 375 | uint32* cell_length, bool* unalloc) |
---|
| 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(hdr_len > 0) |
---|
| 390 | { |
---|
| 391 | length = hdr_len; |
---|
| 392 | if((regfi_read(fd, hdr, &length) != 0) || length != hdr_len) |
---|
| 393 | return false; |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | if(raw_length < 0) |
---|
| 397 | { |
---|
| 398 | (*cell_length) = raw_length*(-1); |
---|
| 399 | (*unalloc) = false; |
---|
| 400 | } |
---|
| 401 | else |
---|
| 402 | { |
---|
| 403 | (*cell_length) = raw_length; |
---|
| 404 | (*unalloc) = true; |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | return true; |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | |
---|
[30] | 411 | /******************************************************************* |
---|
[101] | 412 | Input a random offset and receive the correpsonding HBIN |
---|
[30] | 413 | block for it |
---|
| 414 | *******************************************************************/ |
---|
| 415 | static bool hbin_contains_offset( REGF_HBIN *hbin, uint32 offset ) |
---|
| 416 | { |
---|
[31] | 417 | if ( !hbin ) |
---|
| 418 | return false; |
---|
[30] | 419 | |
---|
[31] | 420 | if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) ) |
---|
| 421 | return true; |
---|
[30] | 422 | |
---|
[31] | 423 | return false; |
---|
[30] | 424 | } |
---|
| 425 | |
---|
| 426 | |
---|
| 427 | /******************************************************************* |
---|
| 428 | Input a randon offset and receive the correpsonding HBIN |
---|
| 429 | block for it |
---|
| 430 | *******************************************************************/ |
---|
| 431 | static REGF_HBIN* lookup_hbin_block( REGF_FILE *file, uint32 offset ) |
---|
| 432 | { |
---|
[31] | 433 | REGF_HBIN *hbin = NULL; |
---|
| 434 | uint32 block_off; |
---|
[30] | 435 | |
---|
[31] | 436 | /* start with the open list */ |
---|
[30] | 437 | |
---|
[31] | 438 | for ( hbin=file->block_list; hbin; hbin=hbin->next ) { |
---|
| 439 | /* DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%x]\n", hbin->file_off, (uint32)hbin ));*/ |
---|
| 440 | if ( hbin_contains_offset( hbin, offset ) ) |
---|
| 441 | return hbin; |
---|
| 442 | } |
---|
[30] | 443 | |
---|
[31] | 444 | if ( !hbin ) { |
---|
| 445 | /* start at the beginning */ |
---|
[30] | 446 | |
---|
[31] | 447 | block_off = REGF_BLOCKSIZE; |
---|
| 448 | do { |
---|
| 449 | /* cleanup before the next round */ |
---|
| 450 | if ( hbin ) |
---|
| 451 | { |
---|
| 452 | if(hbin->ps.is_dynamic) |
---|
| 453 | SAFE_FREE(hbin->ps.data_p); |
---|
| 454 | hbin->ps.is_dynamic = false; |
---|
| 455 | hbin->ps.buffer_size = 0; |
---|
| 456 | hbin->ps.data_offset = 0; |
---|
| 457 | } |
---|
[30] | 458 | |
---|
[97] | 459 | hbin = regfi_parse_hbin(file, block_off, true, false); |
---|
[30] | 460 | |
---|
[31] | 461 | if ( hbin ) |
---|
| 462 | block_off = hbin->file_off + hbin->block_size; |
---|
[30] | 463 | |
---|
[31] | 464 | } while ( hbin && !hbin_contains_offset( hbin, offset ) ); |
---|
| 465 | } |
---|
[30] | 466 | |
---|
[31] | 467 | if ( hbin ) |
---|
[80] | 468 | /* XXX: this kind of caching needs to be re-evaluated */ |
---|
[31] | 469 | DLIST_ADD( file->block_list, hbin ); |
---|
[30] | 470 | |
---|
[31] | 471 | return hbin; |
---|
[30] | 472 | } |
---|
| 473 | |
---|
| 474 | |
---|
| 475 | /******************************************************************* |
---|
[31] | 476 | *******************************************************************/ |
---|
[30] | 477 | static bool prs_hash_rec( const char *desc, prs_struct *ps, int depth, REGF_HASH_REC *hash ) |
---|
| 478 | { |
---|
[31] | 479 | depth++; |
---|
[30] | 480 | |
---|
[31] | 481 | if ( !prs_uint32( "nk_off", ps, depth, &hash->nk_off )) |
---|
| 482 | return false; |
---|
[84] | 483 | if ( !prs_uint8s("keycheck", ps, depth, hash->keycheck, sizeof( hash->keycheck )) ) |
---|
[31] | 484 | return false; |
---|
[30] | 485 | |
---|
[31] | 486 | return true; |
---|
[30] | 487 | } |
---|
| 488 | |
---|
| 489 | |
---|
| 490 | /******************************************************************* |
---|
[31] | 491 | *******************************************************************/ |
---|
[53] | 492 | static bool hbin_prs_lf_records(const char *desc, REGF_HBIN *hbin, |
---|
| 493 | int depth, REGF_NK_REC *nk) |
---|
[30] | 494 | { |
---|
[31] | 495 | int i; |
---|
| 496 | REGF_LF_REC *lf = &nk->subkeys; |
---|
| 497 | uint32 data_size, start_off, end_off; |
---|
[30] | 498 | |
---|
[31] | 499 | depth++; |
---|
[30] | 500 | |
---|
[31] | 501 | /* check if we have anything to do first */ |
---|
[30] | 502 | |
---|
[31] | 503 | if ( nk->num_subkeys == 0 ) |
---|
| 504 | return true; |
---|
[30] | 505 | |
---|
[31] | 506 | /* move to the LF record */ |
---|
[30] | 507 | |
---|
[97] | 508 | if ( !prs_set_offset( &hbin->ps, nk->subkeys_off + HBIN_MAGIC_SIZE - hbin->first_hbin_off ) ) |
---|
[31] | 509 | return false; |
---|
[30] | 510 | |
---|
[31] | 511 | /* backup and get the data_size */ |
---|
[30] | 512 | |
---|
[31] | 513 | if ( !prs_set_offset( &hbin->ps, hbin->ps.data_offset-sizeof(uint32)) ) |
---|
| 514 | return false; |
---|
| 515 | start_off = hbin->ps.data_offset; |
---|
[99] | 516 | if ( !prs_uint32( "cell_size", &hbin->ps, depth, &lf->cell_size )) |
---|
[31] | 517 | return false; |
---|
[30] | 518 | |
---|
[84] | 519 | if(!prs_uint8s("header", &hbin->ps, depth, |
---|
[53] | 520 | lf->header, sizeof(lf->header))) |
---|
[31] | 521 | return false; |
---|
[101] | 522 | |
---|
| 523 | /*fprintf(stdout, "DEBUG: lf->header=%c%c\n", lf->header[0], lf->header[1]);*/ |
---|
| 524 | |
---|
[31] | 525 | if ( !prs_uint16( "num_keys", &hbin->ps, depth, &lf->num_keys)) |
---|
| 526 | return false; |
---|
[30] | 527 | |
---|
[31] | 528 | if ( hbin->ps.io ) { |
---|
| 529 | if ( !(lf->hashes = (REGF_HASH_REC*)zcalloc(sizeof(REGF_HASH_REC), lf->num_keys )) ) |
---|
| 530 | return false; |
---|
| 531 | } |
---|
[30] | 532 | |
---|
[31] | 533 | for ( i=0; i<lf->num_keys; i++ ) { |
---|
| 534 | if ( !prs_hash_rec( "hash_rec", &hbin->ps, depth, &lf->hashes[i] ) ) |
---|
| 535 | return false; |
---|
| 536 | } |
---|
[30] | 537 | |
---|
[31] | 538 | end_off = hbin->ps.data_offset; |
---|
[30] | 539 | |
---|
[31] | 540 | /* data_size must be divisible by 8 and large enough to hold the original record */ |
---|
[30] | 541 | |
---|
[31] | 542 | data_size = ((start_off - end_off) & 0xfffffff8 ); |
---|
[99] | 543 | /* if ( data_size > lf->cell_size )*/ |
---|
| 544 | /*DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->cell_size));*/ |
---|
[30] | 545 | |
---|
[31] | 546 | return true; |
---|
[30] | 547 | } |
---|
| 548 | |
---|
| 549 | |
---|
| 550 | /******************************************************************* |
---|
[31] | 551 | *******************************************************************/ |
---|
[30] | 552 | static bool hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_SK_REC *sk ) |
---|
| 553 | { |
---|
[31] | 554 | prs_struct *ps = &hbin->ps; |
---|
| 555 | uint16 tag = 0xFFFF; |
---|
| 556 | uint32 data_size, start_off, end_off; |
---|
[30] | 557 | |
---|
| 558 | |
---|
[31] | 559 | depth++; |
---|
[30] | 560 | |
---|
[97] | 561 | if ( !prs_set_offset( &hbin->ps, sk->sk_off + HBIN_MAGIC_SIZE - hbin->first_hbin_off ) ) |
---|
[31] | 562 | return false; |
---|
[30] | 563 | |
---|
[31] | 564 | /* backup and get the data_size */ |
---|
[30] | 565 | |
---|
[31] | 566 | if ( !prs_set_offset( &hbin->ps, hbin->ps.data_offset-sizeof(uint32)) ) |
---|
| 567 | return false; |
---|
| 568 | start_off = hbin->ps.data_offset; |
---|
[99] | 569 | if ( !prs_uint32( "cell_size", &hbin->ps, depth, &sk->cell_size )) |
---|
[31] | 570 | return false; |
---|
[30] | 571 | |
---|
[84] | 572 | if (!prs_uint8s("header", ps, depth, sk->header, sizeof(sk->header))) |
---|
[31] | 573 | return false; |
---|
| 574 | if ( !prs_uint16( "tag", ps, depth, &tag)) |
---|
| 575 | return false; |
---|
[30] | 576 | |
---|
[31] | 577 | if ( !prs_uint32( "prev_sk_off", ps, depth, &sk->prev_sk_off)) |
---|
| 578 | return false; |
---|
| 579 | if ( !prs_uint32( "next_sk_off", ps, depth, &sk->next_sk_off)) |
---|
| 580 | return false; |
---|
| 581 | if ( !prs_uint32( "ref_count", ps, depth, &sk->ref_count)) |
---|
| 582 | return false; |
---|
| 583 | if ( !prs_uint32( "size", ps, depth, &sk->size)) |
---|
| 584 | return false; |
---|
[30] | 585 | |
---|
[31] | 586 | if ( !sec_io_desc( "sec_desc", &sk->sec_desc, ps, depth )) |
---|
| 587 | return false; |
---|
[30] | 588 | |
---|
[31] | 589 | end_off = hbin->ps.data_offset; |
---|
[30] | 590 | |
---|
[31] | 591 | /* data_size must be divisible by 8 and large enough to hold the original record */ |
---|
[30] | 592 | |
---|
[31] | 593 | data_size = ((start_off - end_off) & 0xfffffff8 ); |
---|
[99] | 594 | /* if ( data_size > sk->cell_size )*/ |
---|
| 595 | /*DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->cell_size));*/ |
---|
[30] | 596 | |
---|
[31] | 597 | return true; |
---|
[30] | 598 | } |
---|
| 599 | |
---|
| 600 | |
---|
| 601 | |
---|
| 602 | /******************************************************************* |
---|
| 603 | read a VK record which is contained in the HBIN block stored |
---|
| 604 | in the prs_struct *ps. |
---|
| 605 | *******************************************************************/ |
---|
[101] | 606 | static bool hbin_prs_vk_records(const char* desc, REGF_HBIN* hbin, |
---|
| 607 | int depth, REGF_NK_REC* nk, REGF_FILE* file) |
---|
[30] | 608 | { |
---|
[31] | 609 | int i; |
---|
[101] | 610 | uint32 record_size, vk_raw_offset, vk_offset, vk_max_length; |
---|
| 611 | REGF_HBIN* sub_hbin; |
---|
[30] | 612 | |
---|
[31] | 613 | depth++; |
---|
[80] | 614 | |
---|
[31] | 615 | /* check if we have anything to do first */ |
---|
[32] | 616 | if(nk->num_values == 0) |
---|
[31] | 617 | return true; |
---|
[80] | 618 | |
---|
[32] | 619 | if(hbin->ps.io) |
---|
| 620 | { |
---|
[101] | 621 | if (!(nk->values = (REGF_VK_REC**)zcalloc(sizeof(REGF_VK_REC*), |
---|
[32] | 622 | nk->num_values ))) |
---|
[31] | 623 | return false; |
---|
| 624 | } |
---|
[80] | 625 | |
---|
[31] | 626 | /* convert the offset to something relative to this HBIN block */ |
---|
[32] | 627 | if (!prs_set_offset(&hbin->ps, |
---|
| 628 | nk->values_off |
---|
[97] | 629 | + HBIN_MAGIC_SIZE |
---|
[32] | 630 | - hbin->first_hbin_off |
---|
| 631 | - sizeof(uint32))) |
---|
| 632 | { return false; } |
---|
[30] | 633 | |
---|
[32] | 634 | if ( !hbin->ps.io ) |
---|
| 635 | { |
---|
[31] | 636 | record_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8; |
---|
| 637 | record_size = (record_size - 1) ^ 0xFFFFFFFF; |
---|
| 638 | } |
---|
[30] | 639 | |
---|
[31] | 640 | if ( !prs_uint32( "record_size", &hbin->ps, depth, &record_size ) ) |
---|
| 641 | return false; |
---|
[80] | 642 | |
---|
[32] | 643 | for ( i=0; i<nk->num_values; i++ ) |
---|
| 644 | { |
---|
[101] | 645 | if ( !prs_uint32( "vk_off", &hbin->ps, depth, &vk_raw_offset) ) |
---|
[31] | 646 | return false; |
---|
[101] | 647 | |
---|
| 648 | if(hbin_contains_offset(hbin, vk_raw_offset)) |
---|
| 649 | sub_hbin = hbin; |
---|
| 650 | else |
---|
[32] | 651 | { |
---|
[101] | 652 | sub_hbin = lookup_hbin_block( file, vk_raw_offset ); |
---|
| 653 | if (!sub_hbin) |
---|
[31] | 654 | return false; |
---|
| 655 | } |
---|
[80] | 656 | |
---|
[101] | 657 | vk_offset = vk_raw_offset + REGF_BLOCKSIZE; |
---|
| 658 | vk_max_length = sub_hbin->block_size - vk_offset + sizeof(uint32); |
---|
| 659 | if((nk->values[i] = regfi_parse_vk(file, vk_offset, vk_max_length, true)) |
---|
| 660 | == NULL) |
---|
[31] | 661 | return false; |
---|
| 662 | } |
---|
[30] | 663 | |
---|
[31] | 664 | return true; |
---|
[30] | 665 | } |
---|
| 666 | |
---|
| 667 | |
---|
| 668 | /******************************************************************* |
---|
[31] | 669 | *******************************************************************/ |
---|
[30] | 670 | static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32 offset ) |
---|
| 671 | { |
---|
[31] | 672 | REGF_SK_REC *p_sk; |
---|
[80] | 673 | |
---|
[31] | 674 | for ( p_sk=file->sec_desc_list; p_sk; p_sk=p_sk->next ) { |
---|
| 675 | if ( p_sk->sk_off == offset ) |
---|
| 676 | return p_sk; |
---|
| 677 | } |
---|
[80] | 678 | |
---|
[31] | 679 | return NULL; |
---|
[30] | 680 | } |
---|
| 681 | |
---|
[32] | 682 | |
---|
[30] | 683 | /******************************************************************* |
---|
[31] | 684 | *******************************************************************/ |
---|
[30] | 685 | static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, SEC_DESC *sd ) |
---|
| 686 | { |
---|
[31] | 687 | REGF_SK_REC *p; |
---|
[30] | 688 | |
---|
[31] | 689 | for ( p=file->sec_desc_list; p; p=p->next ) { |
---|
| 690 | if ( sec_desc_equal( p->sec_desc, sd ) ) |
---|
| 691 | return p; |
---|
| 692 | } |
---|
[30] | 693 | |
---|
[31] | 694 | /* failure */ |
---|
[30] | 695 | |
---|
[31] | 696 | return NULL; |
---|
[30] | 697 | } |
---|
| 698 | |
---|
[32] | 699 | |
---|
[30] | 700 | /******************************************************************* |
---|
[31] | 701 | *******************************************************************/ |
---|
[99] | 702 | static REGF_NK_REC* hbin_prs_key(REGF_FILE *file, REGF_HBIN *hbin) |
---|
[30] | 703 | { |
---|
[99] | 704 | REGF_HBIN* sub_hbin; |
---|
| 705 | REGF_NK_REC* nk; |
---|
| 706 | uint32 nk_cell_offset; |
---|
| 707 | uint32 nk_max_length; |
---|
[31] | 708 | int depth = 0; |
---|
[99] | 709 | |
---|
[31] | 710 | depth++; |
---|
[30] | 711 | |
---|
[31] | 712 | /* get the initial nk record */ |
---|
[99] | 713 | nk_cell_offset = hbin->file_off + hbin->ps.data_offset - sizeof(uint32); |
---|
| 714 | nk_max_length = hbin->block_size - hbin->ps.data_offset + sizeof(uint32); |
---|
| 715 | if ((nk = regfi_parse_nk(file, nk_cell_offset, nk_max_length, true)) == NULL) |
---|
[101] | 716 | { |
---|
| 717 | fprintf(stderr, "DEBUG: regfi_parse_nk returned NULL!\n"); |
---|
[99] | 718 | return NULL; |
---|
[101] | 719 | } |
---|
[30] | 720 | |
---|
[31] | 721 | /* fill in values */ |
---|
[32] | 722 | if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) |
---|
| 723 | { |
---|
[31] | 724 | sub_hbin = hbin; |
---|
[32] | 725 | if ( !hbin_contains_offset( hbin, nk->values_off ) ) |
---|
| 726 | { |
---|
[31] | 727 | sub_hbin = lookup_hbin_block( file, nk->values_off ); |
---|
[32] | 728 | if ( !sub_hbin ) |
---|
| 729 | { |
---|
[31] | 730 | /*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n", |
---|
| 731 | nk->values_off));*/ |
---|
[99] | 732 | return NULL; |
---|
[31] | 733 | } |
---|
| 734 | } |
---|
[30] | 735 | |
---|
[32] | 736 | if(!hbin_prs_vk_records("vk_rec", sub_hbin, depth, nk, file)) |
---|
[99] | 737 | return NULL; |
---|
[31] | 738 | } |
---|
[30] | 739 | |
---|
[31] | 740 | /* now get subkeys */ |
---|
[32] | 741 | if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) |
---|
| 742 | { |
---|
[31] | 743 | sub_hbin = hbin; |
---|
[32] | 744 | if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) |
---|
| 745 | { |
---|
[31] | 746 | sub_hbin = lookup_hbin_block( file, nk->subkeys_off ); |
---|
[32] | 747 | if ( !sub_hbin ) |
---|
| 748 | { |
---|
[31] | 749 | /*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n", |
---|
| 750 | nk->subkeys_off));*/ |
---|
[99] | 751 | return NULL; |
---|
[31] | 752 | } |
---|
| 753 | } |
---|
[30] | 754 | |
---|
[32] | 755 | if (!hbin_prs_lf_records("lf_rec", sub_hbin, depth, nk)) |
---|
[99] | 756 | return NULL; |
---|
[31] | 757 | } |
---|
[30] | 758 | |
---|
[31] | 759 | /* get the to the security descriptor. First look if we have already parsed it */ |
---|
[30] | 760 | |
---|
[32] | 761 | if ((nk->sk_off!=REGF_OFFSET_NONE) |
---|
| 762 | && !(nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off ))) |
---|
| 763 | { |
---|
[31] | 764 | sub_hbin = hbin; |
---|
[32] | 765 | if (!hbin_contains_offset(hbin, nk->sk_off)) |
---|
| 766 | { |
---|
[31] | 767 | sub_hbin = lookup_hbin_block( file, nk->sk_off ); |
---|
[99] | 768 | if ( !sub_hbin ) |
---|
| 769 | { |
---|
| 770 | free(nk); |
---|
[31] | 771 | /*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_offset [0x%x]\n", |
---|
| 772 | nk->subkeys_off));*/ |
---|
[99] | 773 | return NULL; |
---|
[31] | 774 | } |
---|
| 775 | } |
---|
[99] | 776 | |
---|
[31] | 777 | if ( !(nk->sec_desc = (REGF_SK_REC*)zalloc(sizeof(REGF_SK_REC) )) ) |
---|
[99] | 778 | return NULL; |
---|
[31] | 779 | nk->sec_desc->sk_off = nk->sk_off; |
---|
| 780 | if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin, depth, nk->sec_desc )) |
---|
[99] | 781 | return NULL; |
---|
[30] | 782 | |
---|
[31] | 783 | /* add to the list of security descriptors (ref_count has been read from the files) */ |
---|
[30] | 784 | |
---|
[31] | 785 | nk->sec_desc->sk_off = nk->sk_off; |
---|
[80] | 786 | /* XXX: this kind of caching needs to be re-evaluated */ |
---|
[31] | 787 | DLIST_ADD( file->sec_desc_list, nk->sec_desc ); |
---|
| 788 | } |
---|
[99] | 789 | |
---|
| 790 | return nk; |
---|
[30] | 791 | } |
---|
| 792 | |
---|
[32] | 793 | |
---|
[30] | 794 | /******************************************************************* |
---|
[31] | 795 | *******************************************************************/ |
---|
[30] | 796 | static bool next_record( REGF_HBIN *hbin, const char *hdr, bool *eob ) |
---|
| 797 | { |
---|
[53] | 798 | uint8 header[REC_HDR_SIZE] = ""; |
---|
[31] | 799 | uint32 record_size; |
---|
| 800 | uint32 curr_off, block_size; |
---|
| 801 | bool found = false; |
---|
| 802 | prs_struct *ps = &hbin->ps; |
---|
[30] | 803 | |
---|
[31] | 804 | curr_off = ps->data_offset; |
---|
| 805 | if ( curr_off == 0 ) |
---|
[97] | 806 | prs_set_offset( ps, HBIN_HEADER_REC_SIZE+4 ); |
---|
[30] | 807 | |
---|
[31] | 808 | /* assume that the current offset is at the reacord header |
---|
| 809 | and we need to backup to read the record size */ |
---|
| 810 | curr_off -= sizeof(uint32); |
---|
[30] | 811 | |
---|
[31] | 812 | block_size = ps->buffer_size; |
---|
| 813 | record_size = 0; |
---|
[32] | 814 | while ( !found ) |
---|
| 815 | { |
---|
[31] | 816 | curr_off = curr_off+record_size; |
---|
| 817 | if ( curr_off >= block_size ) |
---|
| 818 | break; |
---|
[30] | 819 | |
---|
[31] | 820 | if ( !prs_set_offset( &hbin->ps, curr_off) ) |
---|
| 821 | return false; |
---|
[30] | 822 | |
---|
[31] | 823 | if ( !prs_uint32( "record_size", ps, 0, &record_size ) ) |
---|
| 824 | return false; |
---|
[84] | 825 | if ( !prs_uint8s("header", ps, 0, header, REC_HDR_SIZE ) ) |
---|
[31] | 826 | return false; |
---|
[30] | 827 | |
---|
[31] | 828 | if ( record_size & 0x80000000 ) { |
---|
| 829 | /* absolute_value(record_size) */ |
---|
| 830 | record_size = (record_size ^ 0xffffffff) + 1; |
---|
| 831 | } |
---|
[30] | 832 | |
---|
[31] | 833 | if ( memcmp( header, hdr, REC_HDR_SIZE ) == 0 ) { |
---|
| 834 | found = true; |
---|
| 835 | curr_off += sizeof(uint32); |
---|
| 836 | } |
---|
| 837 | } |
---|
[30] | 838 | |
---|
[31] | 839 | /* mark prs_struct as done ( at end ) if no more SK records */ |
---|
[32] | 840 | /* mark end-of-block as true */ |
---|
| 841 | if ( !found ) |
---|
[31] | 842 | { |
---|
| 843 | prs_set_offset( &hbin->ps, hbin->ps.buffer_size ); |
---|
| 844 | *eob = true; |
---|
| 845 | return false; |
---|
| 846 | } |
---|
[32] | 847 | |
---|
| 848 | if (!prs_set_offset(ps, curr_off)) |
---|
[31] | 849 | return false; |
---|
[30] | 850 | |
---|
[31] | 851 | return true; |
---|
[30] | 852 | } |
---|
| 853 | |
---|
| 854 | |
---|
| 855 | /******************************************************************* |
---|
[31] | 856 | *******************************************************************/ |
---|
[99] | 857 | static REGF_NK_REC* next_nk_record(REGF_FILE *file, REGF_HBIN *hbin, bool *eob) |
---|
[30] | 858 | { |
---|
[99] | 859 | REGF_NK_REC* ret_val; |
---|
| 860 | if(next_record(hbin, "nk", eob) |
---|
| 861 | && (ret_val = hbin_prs_key(file, hbin)) != NULL) |
---|
| 862 | return ret_val; |
---|
| 863 | |
---|
| 864 | fprintf(stderr, "ACK!"); |
---|
| 865 | return NULL; |
---|
[30] | 866 | } |
---|
| 867 | |
---|
| 868 | |
---|
| 869 | /******************************************************************* |
---|
[97] | 870 | * Open the registry file and then read in the REGF block to get the |
---|
| 871 | * first hbin offset. |
---|
| 872 | *******************************************************************/ |
---|
| 873 | REGF_FILE* regfi_open(const char* filename) |
---|
[30] | 874 | { |
---|
[97] | 875 | REGF_FILE* rb; |
---|
| 876 | int fd; |
---|
[31] | 877 | int flags = O_RDONLY; |
---|
[30] | 878 | |
---|
[97] | 879 | /* open an existing file */ |
---|
| 880 | if ((fd = open(filename, flags)) == -1) |
---|
| 881 | { |
---|
[78] | 882 | /* DEBUG(0,("regfi_open: failure to open %s (%s)\n", filename, strerror(errno)));*/ |
---|
[31] | 883 | return NULL; |
---|
| 884 | } |
---|
[99] | 885 | |
---|
[31] | 886 | /* read in an existing file */ |
---|
[97] | 887 | if ((rb = regfi_parse_regf(fd, true)) == NULL) |
---|
| 888 | { |
---|
[78] | 889 | /* DEBUG(0,("regfi_open: Failed to read initial REGF block\n"));*/ |
---|
[97] | 890 | close(fd); |
---|
[31] | 891 | return NULL; |
---|
| 892 | } |
---|
[99] | 893 | |
---|
| 894 | rb->hbins = range_list_new(); |
---|
| 895 | rb->unalloc_cells = range_list_new(); |
---|
| 896 | if((rb->hbins == NULL) || (rb->unalloc_cells == NULL)) |
---|
| 897 | { |
---|
| 898 | close(fd); |
---|
| 899 | free(rb); |
---|
| 900 | return NULL; |
---|
| 901 | } |
---|
| 902 | |
---|
[31] | 903 | /* success */ |
---|
| 904 | return rb; |
---|
[30] | 905 | } |
---|
| 906 | |
---|
| 907 | |
---|
| 908 | /******************************************************************* |
---|
[31] | 909 | *******************************************************************/ |
---|
[78] | 910 | int regfi_close( REGF_FILE *file ) |
---|
[30] | 911 | { |
---|
[31] | 912 | int fd; |
---|
[30] | 913 | |
---|
[31] | 914 | /* nothing to do if there is no open file */ |
---|
[99] | 915 | if ((file == NULL) || (file->fd == -1)) |
---|
| 916 | return 0; |
---|
[30] | 917 | |
---|
[31] | 918 | fd = file->fd; |
---|
| 919 | file->fd = -1; |
---|
[99] | 920 | range_list_free(file->hbins); |
---|
| 921 | range_list_free(file->unalloc_cells); |
---|
| 922 | free(file); |
---|
[30] | 923 | |
---|
[31] | 924 | return close( fd ); |
---|
[30] | 925 | } |
---|
| 926 | |
---|
| 927 | |
---|
[80] | 928 | /****************************************************************************** |
---|
| 929 | * There should be only *one* root key in the registry file based |
---|
| 930 | * on my experience. --jerry |
---|
| 931 | *****************************************************************************/ |
---|
[78] | 932 | REGF_NK_REC* regfi_rootkey( REGF_FILE *file ) |
---|
[30] | 933 | { |
---|
[31] | 934 | REGF_NK_REC *nk; |
---|
| 935 | REGF_HBIN *hbin; |
---|
| 936 | uint32 offset = REGF_BLOCKSIZE; |
---|
| 937 | bool found = false; |
---|
| 938 | bool eob; |
---|
[99] | 939 | |
---|
| 940 | if(!file) |
---|
[31] | 941 | return NULL; |
---|
[99] | 942 | |
---|
[31] | 943 | /* scan through the file on HBIN block at a time looking |
---|
| 944 | for an NK record with a type == 0x002c. |
---|
| 945 | Normally this is the first nk record in the first hbin |
---|
| 946 | block (but I'm not assuming that for now) */ |
---|
[30] | 947 | |
---|
[99] | 948 | while((hbin = regfi_parse_hbin(file, offset, true, false))) |
---|
| 949 | { |
---|
[31] | 950 | eob = false; |
---|
[30] | 951 | |
---|
[99] | 952 | while(!eob) |
---|
| 953 | { |
---|
| 954 | if((nk = next_nk_record(file, hbin, &eob)) != NULL) |
---|
| 955 | { |
---|
| 956 | if ( nk->key_type == NK_TYPE_ROOTKEY ) |
---|
| 957 | { |
---|
[31] | 958 | found = true; |
---|
| 959 | break; |
---|
| 960 | } |
---|
| 961 | } |
---|
| 962 | if(hbin->ps.is_dynamic) |
---|
| 963 | SAFE_FREE(hbin->ps.data_p); |
---|
| 964 | hbin->ps.is_dynamic = false; |
---|
| 965 | hbin->ps.buffer_size = 0; |
---|
| 966 | hbin->ps.data_offset = 0; |
---|
| 967 | } |
---|
[30] | 968 | |
---|
[99] | 969 | if(found) |
---|
[31] | 970 | break; |
---|
[30] | 971 | |
---|
[31] | 972 | offset += hbin->block_size; |
---|
| 973 | } |
---|
[99] | 974 | |
---|
| 975 | if (!found) { |
---|
[78] | 976 | /*DEBUG(0,("regfi_rootkey: corrupt registry file ? No root key record located\n"));*/ |
---|
[31] | 977 | return NULL; |
---|
| 978 | } |
---|
[30] | 979 | |
---|
[80] | 980 | /* XXX: this kind of caching needs to be re-evaluated */ |
---|
[31] | 981 | DLIST_ADD( file->block_list, hbin ); |
---|
[30] | 982 | |
---|
[80] | 983 | return nk; |
---|
[30] | 984 | } |
---|
| 985 | |
---|
| 986 | |
---|
[80] | 987 | /****************************************************************************** |
---|
| 988 | *****************************************************************************/ |
---|
| 989 | void regfi_key_free(REGF_NK_REC* nk) |
---|
[30] | 990 | { |
---|
[80] | 991 | uint32 i; |
---|
| 992 | |
---|
| 993 | if((nk->values != NULL) && (nk->values_off!=REGF_OFFSET_NONE)) |
---|
| 994 | { |
---|
| 995 | for(i=0; i < nk->num_values; i++) |
---|
[81] | 996 | { |
---|
[101] | 997 | if(nk->values[i]->valuename != NULL) |
---|
| 998 | free(nk->values[i]->valuename); |
---|
| 999 | if(nk->values[i]->data != NULL) |
---|
| 1000 | free(nk->values[i]->data); |
---|
| 1001 | free(nk->values[i]); |
---|
[81] | 1002 | } |
---|
[80] | 1003 | free(nk->values); |
---|
| 1004 | } |
---|
[30] | 1005 | |
---|
[80] | 1006 | if(nk->keyname != NULL) |
---|
| 1007 | free(nk->keyname); |
---|
| 1008 | if(nk->classname != NULL) |
---|
| 1009 | free(nk->classname); |
---|
| 1010 | |
---|
| 1011 | /* XXX: not freeing hbin because these are cached. This needs to be reviewed. */ |
---|
| 1012 | /* XXX: not freeing sec_desc because these are cached. This needs to be reviewed. */ |
---|
| 1013 | free(nk); |
---|
| 1014 | } |
---|
| 1015 | |
---|
| 1016 | |
---|
| 1017 | /****************************************************************************** |
---|
| 1018 | *****************************************************************************/ |
---|
| 1019 | REGFI_ITERATOR* regfi_iterator_new(REGF_FILE* fh) |
---|
| 1020 | { |
---|
| 1021 | REGF_NK_REC* root; |
---|
| 1022 | REGFI_ITERATOR* ret_val = (REGFI_ITERATOR*)malloc(sizeof(REGFI_ITERATOR)); |
---|
| 1023 | if(ret_val == NULL) |
---|
| 1024 | return NULL; |
---|
| 1025 | |
---|
[81] | 1026 | root = regfi_rootkey(fh); |
---|
[80] | 1027 | if(root == NULL) |
---|
| 1028 | { |
---|
| 1029 | free(ret_val); |
---|
| 1030 | return NULL; |
---|
| 1031 | } |
---|
| 1032 | |
---|
| 1033 | ret_val->key_positions = void_stack_new(REGF_MAX_DEPTH); |
---|
| 1034 | if(ret_val->key_positions == NULL) |
---|
| 1035 | { |
---|
| 1036 | free(ret_val); |
---|
| 1037 | free(root); |
---|
| 1038 | return NULL; |
---|
| 1039 | } |
---|
| 1040 | |
---|
| 1041 | ret_val->f = fh; |
---|
| 1042 | ret_val->cur_key = root; |
---|
| 1043 | ret_val->cur_subkey = 0; |
---|
| 1044 | ret_val->cur_value = 0; |
---|
| 1045 | |
---|
| 1046 | return ret_val; |
---|
| 1047 | } |
---|
| 1048 | |
---|
| 1049 | |
---|
| 1050 | /****************************************************************************** |
---|
| 1051 | *****************************************************************************/ |
---|
| 1052 | void regfi_iterator_free(REGFI_ITERATOR* i) |
---|
| 1053 | { |
---|
| 1054 | REGFI_ITER_POSITION* cur; |
---|
| 1055 | |
---|
| 1056 | if(i->cur_key != NULL) |
---|
| 1057 | regfi_key_free(i->cur_key); |
---|
| 1058 | |
---|
| 1059 | while((cur = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions)) != NULL) |
---|
| 1060 | { |
---|
| 1061 | regfi_key_free(cur->nk); |
---|
| 1062 | free(cur); |
---|
| 1063 | } |
---|
| 1064 | |
---|
| 1065 | free(i); |
---|
| 1066 | } |
---|
| 1067 | |
---|
| 1068 | |
---|
| 1069 | |
---|
| 1070 | /****************************************************************************** |
---|
| 1071 | *****************************************************************************/ |
---|
| 1072 | /* XXX: some way of indicating reason for failure should be added. */ |
---|
| 1073 | bool regfi_iterator_down(REGFI_ITERATOR* i) |
---|
| 1074 | { |
---|
| 1075 | REGF_NK_REC* subkey; |
---|
| 1076 | REGFI_ITER_POSITION* pos; |
---|
| 1077 | |
---|
| 1078 | pos = (REGFI_ITER_POSITION*)malloc(sizeof(REGFI_ITER_POSITION)); |
---|
| 1079 | if(pos == NULL) |
---|
| 1080 | return false; |
---|
| 1081 | |
---|
[84] | 1082 | subkey = (REGF_NK_REC*)regfi_iterator_cur_subkey(i); |
---|
[80] | 1083 | if(subkey == NULL) |
---|
| 1084 | { |
---|
| 1085 | free(pos); |
---|
| 1086 | return false; |
---|
| 1087 | } |
---|
| 1088 | |
---|
| 1089 | pos->nk = i->cur_key; |
---|
| 1090 | pos->cur_subkey = i->cur_subkey; |
---|
| 1091 | if(!void_stack_push(i->key_positions, pos)) |
---|
| 1092 | { |
---|
| 1093 | free(pos); |
---|
| 1094 | regfi_key_free(subkey); |
---|
| 1095 | return false; |
---|
| 1096 | } |
---|
| 1097 | |
---|
| 1098 | i->cur_key = subkey; |
---|
| 1099 | i->cur_subkey = 0; |
---|
| 1100 | i->cur_value = 0; |
---|
| 1101 | |
---|
| 1102 | return true; |
---|
| 1103 | } |
---|
| 1104 | |
---|
| 1105 | |
---|
| 1106 | /****************************************************************************** |
---|
| 1107 | *****************************************************************************/ |
---|
| 1108 | bool regfi_iterator_up(REGFI_ITERATOR* i) |
---|
| 1109 | { |
---|
| 1110 | REGFI_ITER_POSITION* pos; |
---|
| 1111 | |
---|
| 1112 | pos = (REGFI_ITER_POSITION*)void_stack_pop(i->key_positions); |
---|
| 1113 | if(pos == NULL) |
---|
| 1114 | return false; |
---|
| 1115 | |
---|
| 1116 | regfi_key_free(i->cur_key); |
---|
| 1117 | i->cur_key = pos->nk; |
---|
| 1118 | i->cur_subkey = pos->cur_subkey; |
---|
| 1119 | i->cur_value = 0; |
---|
| 1120 | free(pos); |
---|
| 1121 | |
---|
| 1122 | return true; |
---|
| 1123 | } |
---|
| 1124 | |
---|
| 1125 | |
---|
| 1126 | /****************************************************************************** |
---|
| 1127 | *****************************************************************************/ |
---|
| 1128 | bool regfi_iterator_to_root(REGFI_ITERATOR* i) |
---|
| 1129 | { |
---|
| 1130 | while(regfi_iterator_up(i)) |
---|
| 1131 | continue; |
---|
| 1132 | |
---|
| 1133 | return true; |
---|
| 1134 | } |
---|
| 1135 | |
---|
| 1136 | |
---|
| 1137 | /****************************************************************************** |
---|
| 1138 | *****************************************************************************/ |
---|
| 1139 | bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* subkey_name) |
---|
| 1140 | { |
---|
| 1141 | REGF_NK_REC* subkey; |
---|
| 1142 | bool found = false; |
---|
| 1143 | uint32 old_subkey = i->cur_subkey; |
---|
| 1144 | |
---|
| 1145 | if(subkey_name == NULL) |
---|
| 1146 | return false; |
---|
| 1147 | |
---|
| 1148 | /* XXX: this alloc/free of each sub key might be a bit excessive */ |
---|
[84] | 1149 | subkey = (REGF_NK_REC*)regfi_iterator_first_subkey(i); |
---|
[80] | 1150 | while((subkey != NULL) && (found == false)) |
---|
| 1151 | { |
---|
| 1152 | if(subkey->keyname != NULL |
---|
| 1153 | && strcasecmp(subkey->keyname, subkey_name) == 0) |
---|
| 1154 | found = true; |
---|
[82] | 1155 | else |
---|
| 1156 | { |
---|
| 1157 | regfi_key_free(subkey); |
---|
[84] | 1158 | subkey = (REGF_NK_REC*)regfi_iterator_next_subkey(i); |
---|
[82] | 1159 | } |
---|
[80] | 1160 | } |
---|
| 1161 | |
---|
| 1162 | if(found == false) |
---|
| 1163 | { |
---|
| 1164 | i->cur_subkey = old_subkey; |
---|
| 1165 | return false; |
---|
| 1166 | } |
---|
| 1167 | |
---|
[82] | 1168 | regfi_key_free(subkey); |
---|
[80] | 1169 | return true; |
---|
| 1170 | } |
---|
| 1171 | |
---|
| 1172 | |
---|
| 1173 | /****************************************************************************** |
---|
| 1174 | *****************************************************************************/ |
---|
| 1175 | bool regfi_iterator_walk_path(REGFI_ITERATOR* i, const char** path) |
---|
| 1176 | { |
---|
| 1177 | uint32 x; |
---|
| 1178 | if(path == NULL) |
---|
| 1179 | return false; |
---|
| 1180 | |
---|
| 1181 | for(x=0; |
---|
| 1182 | ((path[x] != NULL) && regfi_iterator_find_subkey(i, path[x]) |
---|
| 1183 | && regfi_iterator_down(i)); |
---|
| 1184 | x++) |
---|
| 1185 | { continue; } |
---|
| 1186 | |
---|
| 1187 | if(path[x] == NULL) |
---|
| 1188 | return true; |
---|
| 1189 | |
---|
| 1190 | /* XXX: is this the right number of times? */ |
---|
| 1191 | for(; x > 0; x--) |
---|
| 1192 | regfi_iterator_up(i); |
---|
| 1193 | |
---|
| 1194 | return false; |
---|
| 1195 | } |
---|
| 1196 | |
---|
| 1197 | |
---|
| 1198 | /****************************************************************************** |
---|
| 1199 | *****************************************************************************/ |
---|
[84] | 1200 | const REGF_NK_REC* regfi_iterator_cur_key(REGFI_ITERATOR* i) |
---|
[80] | 1201 | { |
---|
| 1202 | return i->cur_key; |
---|
| 1203 | } |
---|
| 1204 | |
---|
| 1205 | |
---|
| 1206 | /****************************************************************************** |
---|
| 1207 | *****************************************************************************/ |
---|
[84] | 1208 | const REGF_NK_REC* regfi_iterator_first_subkey(REGFI_ITERATOR* i) |
---|
[80] | 1209 | { |
---|
| 1210 | i->cur_subkey = 0; |
---|
| 1211 | return regfi_iterator_cur_subkey(i); |
---|
| 1212 | } |
---|
| 1213 | |
---|
| 1214 | |
---|
| 1215 | /****************************************************************************** |
---|
| 1216 | *****************************************************************************/ |
---|
[84] | 1217 | const REGF_NK_REC* regfi_iterator_cur_subkey(REGFI_ITERATOR* i) |
---|
[80] | 1218 | { |
---|
| 1219 | REGF_NK_REC* subkey; |
---|
| 1220 | REGF_HBIN* hbin; |
---|
| 1221 | uint32 nk_offset; |
---|
| 1222 | |
---|
[31] | 1223 | /* see if there is anything left to report */ |
---|
[80] | 1224 | if (!(i->cur_key) || (i->cur_key->subkeys_off==REGF_OFFSET_NONE) |
---|
| 1225 | || (i->cur_subkey >= i->cur_key->num_subkeys)) |
---|
[31] | 1226 | return NULL; |
---|
[30] | 1227 | |
---|
[80] | 1228 | nk_offset = i->cur_key->subkeys.hashes[i->cur_subkey].nk_off; |
---|
| 1229 | |
---|
[31] | 1230 | /* find the HBIN block which should contain the nk record */ |
---|
[80] | 1231 | hbin = lookup_hbin_block(i->f, nk_offset); |
---|
| 1232 | if(!hbin) |
---|
[31] | 1233 | { |
---|
[80] | 1234 | /* XXX: should print out some kind of error message every time here */ |
---|
[31] | 1235 | /*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n", |
---|
[80] | 1236 | i->cur_key->subkeys.hashes[i->cur_subkey].nk_off));*/ |
---|
[31] | 1237 | return NULL; |
---|
| 1238 | } |
---|
[78] | 1239 | |
---|
[32] | 1240 | if(!prs_set_offset(&hbin->ps, |
---|
[97] | 1241 | HBIN_MAGIC_SIZE + nk_offset - hbin->first_hbin_off)) |
---|
[31] | 1242 | return NULL; |
---|
[30] | 1243 | |
---|
[99] | 1244 | if((subkey = hbin_prs_key(i->f, hbin)) == NULL) |
---|
[31] | 1245 | return NULL; |
---|
[30] | 1246 | |
---|
[31] | 1247 | return subkey; |
---|
[30] | 1248 | } |
---|
[80] | 1249 | |
---|
| 1250 | |
---|
| 1251 | /****************************************************************************** |
---|
| 1252 | *****************************************************************************/ |
---|
| 1253 | /* XXX: some way of indicating reason for failure should be added. */ |
---|
[84] | 1254 | const REGF_NK_REC* regfi_iterator_next_subkey(REGFI_ITERATOR* i) |
---|
[80] | 1255 | { |
---|
[84] | 1256 | const REGF_NK_REC* subkey; |
---|
[80] | 1257 | |
---|
| 1258 | i->cur_subkey++; |
---|
| 1259 | subkey = regfi_iterator_cur_subkey(i); |
---|
| 1260 | |
---|
| 1261 | if(subkey == NULL) |
---|
| 1262 | i->cur_subkey--; |
---|
| 1263 | |
---|
| 1264 | return subkey; |
---|
| 1265 | } |
---|
| 1266 | |
---|
| 1267 | |
---|
| 1268 | /****************************************************************************** |
---|
| 1269 | *****************************************************************************/ |
---|
| 1270 | bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* value_name) |
---|
| 1271 | { |
---|
[84] | 1272 | const REGF_VK_REC* cur; |
---|
[80] | 1273 | bool found = false; |
---|
| 1274 | |
---|
| 1275 | /* XXX: cur->valuename can be NULL in the registry. |
---|
| 1276 | * Should we allow for a way to search for that? |
---|
| 1277 | */ |
---|
| 1278 | if(value_name == NULL) |
---|
| 1279 | return false; |
---|
| 1280 | |
---|
| 1281 | cur = regfi_iterator_first_value(i); |
---|
| 1282 | while((cur != NULL) && (found == false)) |
---|
| 1283 | { |
---|
| 1284 | if((cur->valuename != NULL) |
---|
| 1285 | && (strcasecmp(cur->valuename, value_name) == 0)) |
---|
| 1286 | found = true; |
---|
[95] | 1287 | else |
---|
| 1288 | cur = regfi_iterator_next_value(i); |
---|
[80] | 1289 | } |
---|
| 1290 | |
---|
[94] | 1291 | return found; |
---|
[80] | 1292 | } |
---|
| 1293 | |
---|
| 1294 | |
---|
| 1295 | /****************************************************************************** |
---|
| 1296 | *****************************************************************************/ |
---|
[84] | 1297 | const REGF_VK_REC* regfi_iterator_first_value(REGFI_ITERATOR* i) |
---|
[80] | 1298 | { |
---|
| 1299 | i->cur_value = 0; |
---|
| 1300 | return regfi_iterator_cur_value(i); |
---|
| 1301 | } |
---|
| 1302 | |
---|
| 1303 | |
---|
| 1304 | /****************************************************************************** |
---|
| 1305 | *****************************************************************************/ |
---|
[84] | 1306 | const REGF_VK_REC* regfi_iterator_cur_value(REGFI_ITERATOR* i) |
---|
[80] | 1307 | { |
---|
| 1308 | REGF_VK_REC* ret_val = NULL; |
---|
| 1309 | if(i->cur_value < i->cur_key->num_values) |
---|
[101] | 1310 | ret_val = i->cur_key->values[i->cur_value]; |
---|
[80] | 1311 | |
---|
| 1312 | return ret_val; |
---|
| 1313 | } |
---|
| 1314 | |
---|
| 1315 | |
---|
| 1316 | /****************************************************************************** |
---|
| 1317 | *****************************************************************************/ |
---|
[84] | 1318 | const REGF_VK_REC* regfi_iterator_next_value(REGFI_ITERATOR* i) |
---|
[80] | 1319 | { |
---|
[84] | 1320 | const REGF_VK_REC* ret_val; |
---|
[80] | 1321 | |
---|
| 1322 | i->cur_value++; |
---|
| 1323 | ret_val = regfi_iterator_cur_value(i); |
---|
| 1324 | if(ret_val == NULL) |
---|
| 1325 | i->cur_value--; |
---|
| 1326 | |
---|
| 1327 | return ret_val; |
---|
| 1328 | } |
---|
[97] | 1329 | |
---|
| 1330 | |
---|
| 1331 | |
---|
| 1332 | /****************/ |
---|
| 1333 | /* Experimental */ |
---|
| 1334 | /****************/ |
---|
| 1335 | /* |
---|
| 1336 | typedef struct { |
---|
| 1337 | uint32 offset; |
---|
| 1338 | uint32 size; |
---|
| 1339 | } REGFI_CELL_INFO; |
---|
| 1340 | |
---|
| 1341 | typedef struct { |
---|
| 1342 | uint32 count |
---|
| 1343 | REGFI_CELL_INFO** cells; |
---|
| 1344 | } REGFI_CELL_LIST; |
---|
| 1345 | */ |
---|
| 1346 | |
---|
| 1347 | |
---|
| 1348 | /******************************************************************* |
---|
| 1349 | * Computes the checksum of the registry file header. |
---|
| 1350 | * buffer must be at least the size of an regf header (4096 bytes). |
---|
| 1351 | *******************************************************************/ |
---|
| 1352 | static uint32 regfi_compute_header_checksum(uint8* buffer) |
---|
| 1353 | { |
---|
| 1354 | uint32 checksum, x; |
---|
| 1355 | int i; |
---|
| 1356 | |
---|
| 1357 | /* XOR of all bytes 0x0000 - 0x01FB */ |
---|
| 1358 | |
---|
| 1359 | checksum = x = 0; |
---|
| 1360 | |
---|
| 1361 | for ( i=0; i<0x01FB; i+=4 ) { |
---|
| 1362 | x = IVAL(buffer, i ); |
---|
| 1363 | checksum ^= x; |
---|
| 1364 | } |
---|
| 1365 | |
---|
| 1366 | return checksum; |
---|
| 1367 | } |
---|
| 1368 | |
---|
| 1369 | |
---|
| 1370 | /******************************************************************* |
---|
| 1371 | * TODO: add way to return more detailed error information. |
---|
| 1372 | *******************************************************************/ |
---|
| 1373 | REGF_FILE* regfi_parse_regf(int fd, bool strict) |
---|
| 1374 | { |
---|
| 1375 | uint8 file_header[REGF_BLOCKSIZE]; |
---|
| 1376 | uint32 ret, length; |
---|
| 1377 | uint32 file_length; |
---|
| 1378 | struct stat sbuf; |
---|
| 1379 | REGF_FILE* ret_val; |
---|
| 1380 | |
---|
| 1381 | /* Determine file length. Must be at least big enough |
---|
| 1382 | * for the header and one hbin. |
---|
| 1383 | */ |
---|
| 1384 | if (fstat(fd, &sbuf) == -1) |
---|
| 1385 | return NULL; |
---|
| 1386 | file_length = sbuf.st_size; |
---|
| 1387 | if(file_length < REGF_BLOCKSIZE+REGF_ALLOC_BLOCK) |
---|
| 1388 | return NULL; |
---|
| 1389 | |
---|
| 1390 | ret_val = (REGF_FILE*)zalloc(sizeof(REGF_FILE)); |
---|
| 1391 | if(ret_val == NULL) |
---|
| 1392 | return NULL; |
---|
| 1393 | |
---|
| 1394 | ret_val->fd = fd; |
---|
| 1395 | ret_val->file_length = file_length; |
---|
| 1396 | |
---|
| 1397 | length = REGF_BLOCKSIZE; |
---|
| 1398 | if((ret = regfi_read(fd, file_header, &length)) != 0 |
---|
| 1399 | || length != REGF_BLOCKSIZE) |
---|
| 1400 | { |
---|
| 1401 | free(ret_val); |
---|
| 1402 | return NULL; |
---|
| 1403 | } |
---|
| 1404 | |
---|
| 1405 | ret_val->checksum = IVAL(file_header, 0x1FC); |
---|
| 1406 | ret_val->computed_checksum = regfi_compute_header_checksum(file_header); |
---|
| 1407 | if (strict && (ret_val->checksum != ret_val->computed_checksum)) |
---|
| 1408 | { |
---|
| 1409 | free(ret_val); |
---|
| 1410 | return NULL; |
---|
| 1411 | } |
---|
| 1412 | |
---|
| 1413 | memcpy(ret_val->magic, file_header, 4); |
---|
| 1414 | if(strict && (memcmp(ret_val->magic, "regf", 4) != 0)) |
---|
| 1415 | { |
---|
| 1416 | free(ret_val); |
---|
| 1417 | return NULL; |
---|
| 1418 | } |
---|
| 1419 | |
---|
| 1420 | ret_val->unknown1 = IVAL(file_header, 0x4); |
---|
| 1421 | ret_val->unknown2 = IVAL(file_header, 0x8); |
---|
| 1422 | |
---|
| 1423 | ret_val->mtime.low = IVAL(file_header, 0xC); |
---|
| 1424 | ret_val->mtime.high = IVAL(file_header, 0x10); |
---|
| 1425 | |
---|
| 1426 | ret_val->unknown3 = IVAL(file_header, 0x14); |
---|
| 1427 | ret_val->unknown4 = IVAL(file_header, 0x18); |
---|
| 1428 | ret_val->unknown5 = IVAL(file_header, 0x1C); |
---|
| 1429 | ret_val->unknown6 = IVAL(file_header, 0x20); |
---|
| 1430 | |
---|
| 1431 | ret_val->data_offset = IVAL(file_header, 0x24); |
---|
| 1432 | ret_val->last_block = IVAL(file_header, 0x28); |
---|
| 1433 | |
---|
| 1434 | ret_val->unknown7 = IVAL(file_header, 0x2C); |
---|
| 1435 | |
---|
| 1436 | return ret_val; |
---|
| 1437 | } |
---|
| 1438 | |
---|
| 1439 | |
---|
| 1440 | |
---|
| 1441 | /******************************************************************* |
---|
| 1442 | * Given real file offset, read and parse the hbin at that location |
---|
| 1443 | * along with it's associated cells. If save_unalloc is true, a list |
---|
| 1444 | * of unallocated cell offsets will be stored in TODO. |
---|
| 1445 | *******************************************************************/ |
---|
| 1446 | /* TODO: Need a way to return types of errors. Also need to free |
---|
| 1447 | * the hbin/ps when an error occurs. |
---|
| 1448 | */ |
---|
| 1449 | REGF_HBIN* regfi_parse_hbin(REGF_FILE* file, uint32 offset, |
---|
| 1450 | bool strict, bool save_unalloc) |
---|
| 1451 | { |
---|
| 1452 | REGF_HBIN *hbin; |
---|
| 1453 | uint8 hbin_header[HBIN_HEADER_REC_SIZE]; |
---|
| 1454 | uint32 length, curr_off; |
---|
| 1455 | int32 cell_len; |
---|
| 1456 | bool is_unalloc; |
---|
[99] | 1457 | |
---|
| 1458 | if(offset >= file->file_length) |
---|
| 1459 | return NULL; |
---|
[97] | 1460 | |
---|
| 1461 | if(lseek(file->fd, offset, SEEK_SET) == -1) |
---|
| 1462 | return NULL; |
---|
| 1463 | |
---|
| 1464 | length = HBIN_HEADER_REC_SIZE; |
---|
| 1465 | if((regfi_read(file->fd, hbin_header, &length) != 0) |
---|
| 1466 | || length != HBIN_HEADER_REC_SIZE) |
---|
| 1467 | return NULL; |
---|
| 1468 | |
---|
[99] | 1469 | |
---|
[97] | 1470 | if(lseek(file->fd, offset, SEEK_SET) == -1) |
---|
| 1471 | return NULL; |
---|
| 1472 | |
---|
[99] | 1473 | if(!(hbin = (REGF_HBIN*)zalloc(sizeof(REGF_HBIN)))) |
---|
| 1474 | return NULL; |
---|
| 1475 | hbin->file_off = offset; |
---|
| 1476 | |
---|
[97] | 1477 | memcpy(hbin->magic, hbin_header, 4); |
---|
| 1478 | if(strict && (memcmp(hbin->magic, "hbin", 4) != 0)) |
---|
[99] | 1479 | { |
---|
| 1480 | free(hbin); |
---|
[97] | 1481 | return NULL; |
---|
[99] | 1482 | } |
---|
[97] | 1483 | |
---|
| 1484 | hbin->first_hbin_off = IVAL(hbin_header, 0x4); |
---|
| 1485 | hbin->block_size = IVAL(hbin_header, 0x8); |
---|
| 1486 | /* this should be the same thing as hbin->block_size but just in case */ |
---|
| 1487 | hbin->next_block = IVAL(hbin_header, 0x1C); |
---|
| 1488 | |
---|
| 1489 | |
---|
| 1490 | /* Ensure the block size is a multiple of 0x1000 and doesn't run off |
---|
| 1491 | * the end of the file. |
---|
| 1492 | */ |
---|
| 1493 | /* TODO: This may need to be relaxed for dealing with |
---|
| 1494 | * partial or corrupt files. */ |
---|
| 1495 | if((offset + hbin->block_size > file->file_length) |
---|
| 1496 | || (hbin->block_size & 0xFFFFF000) != hbin->block_size) |
---|
[99] | 1497 | { |
---|
| 1498 | free(hbin); |
---|
[97] | 1499 | return NULL; |
---|
[99] | 1500 | } |
---|
[97] | 1501 | |
---|
| 1502 | /* TODO: need to get rid of this, but currently lots depends on the |
---|
| 1503 | * ps structure. |
---|
| 1504 | */ |
---|
| 1505 | if(!prs_init(&hbin->ps, hbin->block_size, file->mem_ctx, UNMARSHALL)) |
---|
[99] | 1506 | { |
---|
| 1507 | free(hbin); |
---|
[97] | 1508 | return NULL; |
---|
[99] | 1509 | } |
---|
[97] | 1510 | length = hbin->block_size; |
---|
| 1511 | if((regfi_read(file->fd, (uint8*)hbin->ps.data_p, &length) != 0) |
---|
| 1512 | || length != hbin->block_size) |
---|
[99] | 1513 | { |
---|
| 1514 | free(hbin); |
---|
[97] | 1515 | return NULL; |
---|
[99] | 1516 | } |
---|
[97] | 1517 | |
---|
| 1518 | |
---|
| 1519 | if(save_unalloc) |
---|
| 1520 | { |
---|
| 1521 | cell_len = 0; |
---|
| 1522 | curr_off = HBIN_HEADER_REC_SIZE; |
---|
| 1523 | while ( curr_off < hbin->block_size ) |
---|
| 1524 | { |
---|
[99] | 1525 | is_unalloc = false; |
---|
[97] | 1526 | cell_len = IVALS(hbin->ps.data_p, curr_off); |
---|
| 1527 | if(cell_len > 0) |
---|
| 1528 | is_unalloc = true; |
---|
| 1529 | else |
---|
| 1530 | cell_len = -1*cell_len; |
---|
| 1531 | |
---|
| 1532 | if((cell_len == 0) || ((cell_len & 0xFFFFFFFC) != cell_len)) |
---|
| 1533 | /* TODO: should report an error here. */ |
---|
| 1534 | break; |
---|
| 1535 | |
---|
| 1536 | /* for some reason the record_size of the last record in |
---|
| 1537 | an hbin block can extend past the end of the block |
---|
| 1538 | even though the record fits within the remaining |
---|
| 1539 | space....aaarrrgggghhhhhh */ |
---|
| 1540 | if(curr_off + cell_len >= hbin->block_size) |
---|
| 1541 | cell_len = hbin->block_size - curr_off; |
---|
| 1542 | |
---|
| 1543 | if(is_unalloc) |
---|
[99] | 1544 | range_list_add(file->unalloc_cells, hbin->file_off+curr_off, |
---|
| 1545 | cell_len, NULL); |
---|
[97] | 1546 | |
---|
| 1547 | curr_off = curr_off+cell_len; |
---|
| 1548 | } |
---|
| 1549 | } |
---|
| 1550 | |
---|
| 1551 | /* TODO: need to get rid of this, but currently lots depends on the |
---|
| 1552 | * ps structure. |
---|
| 1553 | */ |
---|
| 1554 | if(!prs_set_offset(&hbin->ps, file->data_offset+HBIN_MAGIC_SIZE)) |
---|
| 1555 | return NULL; |
---|
| 1556 | |
---|
| 1557 | return hbin; |
---|
| 1558 | } |
---|
| 1559 | |
---|
| 1560 | |
---|
[101] | 1561 | |
---|
[99] | 1562 | REGF_NK_REC* regfi_parse_nk(REGF_FILE* file, uint32 offset, |
---|
| 1563 | uint32 max_size, bool strict) |
---|
| 1564 | { |
---|
| 1565 | uint8 nk_header[REGFI_NK_MIN_LENGTH]; |
---|
| 1566 | REGF_NK_REC* ret_val; |
---|
| 1567 | uint32 length; |
---|
[101] | 1568 | uint32 cell_length; |
---|
| 1569 | bool unalloc = false; |
---|
[99] | 1570 | |
---|
[101] | 1571 | if(!regfi_parse_cell(file->fd, offset, nk_header, REGFI_NK_MIN_LENGTH, |
---|
| 1572 | &cell_length, &unalloc)) |
---|
| 1573 | return NULL; |
---|
[99] | 1574 | |
---|
| 1575 | /* A bit of validation before bothering to allocate memory */ |
---|
[101] | 1576 | if((nk_header[0x0] != 'n') || (nk_header[0x1] != 'k')) |
---|
| 1577 | { |
---|
| 1578 | /* TODO: deal with subkey-lists that reference other subkey-lists. */ |
---|
| 1579 | /*fprintf(stderr, "DEBUG: magic check failed! \"%c%c\"\n", nk_header[0x0], nk_header[0x1]);*/ |
---|
[99] | 1580 | return NULL; |
---|
[101] | 1581 | } |
---|
[99] | 1582 | |
---|
| 1583 | ret_val = (REGF_NK_REC*)zalloc(sizeof(REGF_NK_REC)); |
---|
| 1584 | if(ret_val == NULL) |
---|
| 1585 | return NULL; |
---|
| 1586 | |
---|
| 1587 | ret_val->offset = offset; |
---|
[101] | 1588 | ret_val->cell_size = cell_length; |
---|
| 1589 | |
---|
[99] | 1590 | if(ret_val->cell_size > max_size) |
---|
| 1591 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
| 1592 | if((ret_val->cell_size < REGFI_NK_MIN_LENGTH) |
---|
| 1593 | || (strict && ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8))) |
---|
| 1594 | { |
---|
| 1595 | free(ret_val); |
---|
| 1596 | return NULL; |
---|
| 1597 | } |
---|
| 1598 | |
---|
[101] | 1599 | ret_val->magic[0] = nk_header[0x0]; |
---|
| 1600 | ret_val->magic[1] = nk_header[0x1]; |
---|
| 1601 | ret_val->key_type = SVAL(nk_header, 0x2); |
---|
| 1602 | if((ret_val->key_type != NK_TYPE_NORMALKEY) |
---|
| 1603 | && (ret_val->key_type != NK_TYPE_ROOTKEY) |
---|
| 1604 | && (ret_val->key_type != NK_TYPE_LINKKEY) |
---|
| 1605 | && (ret_val->key_type != NK_TYPE_UNKNOWN1)) |
---|
[99] | 1606 | { |
---|
| 1607 | free(ret_val); |
---|
| 1608 | return NULL; |
---|
| 1609 | } |
---|
[101] | 1610 | |
---|
| 1611 | ret_val->mtime.low = IVAL(nk_header, 0x4); |
---|
| 1612 | ret_val->mtime.high = IVAL(nk_header, 0x8); |
---|
[99] | 1613 | |
---|
[101] | 1614 | ret_val->unknown1 = IVAL(nk_header, 0xC); |
---|
| 1615 | ret_val->parent_off = IVAL(nk_header, 0x10); |
---|
| 1616 | ret_val->num_subkeys = IVAL(nk_header, 0x14); |
---|
| 1617 | ret_val->unknown2 = IVAL(nk_header, 0x18); |
---|
| 1618 | ret_val->subkeys_off = IVAL(nk_header, 0x1C); |
---|
| 1619 | ret_val->unknown3 = IVAL(nk_header, 0x20); |
---|
| 1620 | ret_val->num_values = IVAL(nk_header, 0x24); |
---|
| 1621 | ret_val->values_off = IVAL(nk_header, 0x28); |
---|
| 1622 | ret_val->sk_off = IVAL(nk_header, 0x2C); |
---|
[99] | 1623 | /* TODO: currently we do nothing with class names. Need to investigate. */ |
---|
[101] | 1624 | ret_val->classname_off = IVAL(nk_header, 0x30); |
---|
[99] | 1625 | |
---|
[101] | 1626 | ret_val->max_bytes_subkeyname = IVAL(nk_header, 0x34); |
---|
| 1627 | ret_val->max_bytes_subkeyclassname = IVAL(nk_header, 0x38); |
---|
| 1628 | ret_val->max_bytes_valuename = IVAL(nk_header, 0x3C); |
---|
| 1629 | ret_val->max_bytes_value = IVAL(nk_header, 0x40); |
---|
| 1630 | ret_val->unk_index = IVAL(nk_header, 0x44); |
---|
[99] | 1631 | |
---|
[101] | 1632 | ret_val->name_length = SVAL(nk_header, 0x48); |
---|
| 1633 | ret_val->classname_length = SVAL(nk_header, 0x4A); |
---|
[99] | 1634 | |
---|
| 1635 | if(ret_val->name_length + REGFI_NK_MIN_LENGTH > ret_val->cell_size) |
---|
[101] | 1636 | { |
---|
| 1637 | if(strict) |
---|
| 1638 | { |
---|
| 1639 | free(ret_val); |
---|
| 1640 | return NULL; |
---|
| 1641 | } |
---|
| 1642 | else |
---|
| 1643 | ret_val->name_length = ret_val->cell_size - REGFI_NK_MIN_LENGTH; |
---|
| 1644 | } |
---|
| 1645 | else if (unalloc) |
---|
| 1646 | { /* Truncate cell_size if it's much larger than the apparent total record length. */ |
---|
| 1647 | /* Round up to the next multiple of 8 */ |
---|
| 1648 | length = (ret_val->name_length + REGFI_NK_MIN_LENGTH) & 0xFFFFFFF8; |
---|
| 1649 | if(length < ret_val->name_length + REGFI_NK_MIN_LENGTH) |
---|
| 1650 | length+=8; |
---|
[99] | 1651 | |
---|
[101] | 1652 | /* If cell_size is still greater, truncate. */ |
---|
| 1653 | if(length < ret_val->cell_size) |
---|
| 1654 | ret_val->cell_size = length; |
---|
| 1655 | } |
---|
| 1656 | |
---|
[99] | 1657 | ret_val->keyname = (char*)zalloc(sizeof(char)*(ret_val->name_length+1)); |
---|
| 1658 | if(ret_val->keyname == NULL) |
---|
| 1659 | { |
---|
| 1660 | free(ret_val); |
---|
| 1661 | return NULL; |
---|
| 1662 | } |
---|
| 1663 | |
---|
| 1664 | /* Don't need to seek, should be at the right offset */ |
---|
| 1665 | length = ret_val->name_length; |
---|
[101] | 1666 | if((regfi_read(file->fd, (uint8*)ret_val->keyname, &length) != 0) |
---|
[99] | 1667 | || length != ret_val->name_length) |
---|
| 1668 | { |
---|
| 1669 | free(ret_val->keyname); |
---|
| 1670 | free(ret_val); |
---|
| 1671 | return NULL; |
---|
| 1672 | } |
---|
| 1673 | ret_val->keyname[ret_val->name_length] = '\0'; |
---|
| 1674 | |
---|
| 1675 | return ret_val; |
---|
| 1676 | } |
---|
| 1677 | |
---|
| 1678 | |
---|
| 1679 | |
---|
[101] | 1680 | /******************************************************************* |
---|
| 1681 | *******************************************************************/ |
---|
| 1682 | REGF_VK_REC* regfi_parse_vk(REGF_FILE* file, uint32 offset, |
---|
| 1683 | uint32 max_size, bool strict) |
---|
[97] | 1684 | { |
---|
[101] | 1685 | REGF_VK_REC* ret_val; |
---|
| 1686 | uint8 vk_header[REGFI_VK_MIN_LENGTH]; |
---|
| 1687 | uint32 raw_data_size, length, cell_length; |
---|
| 1688 | bool unalloc = false; |
---|
[97] | 1689 | |
---|
[101] | 1690 | if(!regfi_parse_cell(file->fd, offset, vk_header, REGFI_VK_MIN_LENGTH, |
---|
| 1691 | &cell_length, &unalloc)) |
---|
| 1692 | return NULL; |
---|
| 1693 | |
---|
| 1694 | ret_val = (REGF_VK_REC*)zalloc(sizeof(REGF_VK_REC)); |
---|
| 1695 | if(ret_val == NULL) |
---|
| 1696 | return NULL; |
---|
| 1697 | |
---|
| 1698 | ret_val->offset = offset; |
---|
| 1699 | ret_val->cell_size = cell_length; |
---|
| 1700 | |
---|
| 1701 | if(ret_val->cell_size > max_size) |
---|
| 1702 | ret_val->cell_size = max_size & 0xFFFFFFF8; |
---|
| 1703 | if((ret_val->cell_size < REGFI_VK_MIN_LENGTH) |
---|
| 1704 | || (strict && ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8))) |
---|
[97] | 1705 | { |
---|
[101] | 1706 | free(ret_val); |
---|
| 1707 | return NULL; |
---|
| 1708 | } |
---|
[97] | 1709 | |
---|
[101] | 1710 | ret_val->magic[0] = vk_header[0x0]; |
---|
| 1711 | ret_val->magic[1] = vk_header[0x1]; |
---|
| 1712 | if((ret_val->magic[0] != 'v') || (ret_val->magic[1] != 'k')) |
---|
| 1713 | { |
---|
| 1714 | free(ret_val); |
---|
| 1715 | return NULL; |
---|
| 1716 | } |
---|
| 1717 | |
---|
| 1718 | ret_val->name_length = SVAL(vk_header, 0x2); |
---|
| 1719 | raw_data_size = IVAL(vk_header, 0x4); |
---|
| 1720 | ret_val->data_size = raw_data_size & ~VK_DATA_IN_OFFSET; |
---|
| 1721 | ret_val->data_off = IVAL(vk_header, 0x8); |
---|
| 1722 | ret_val->type = IVAL(vk_header, 0xC); |
---|
| 1723 | ret_val->flag = SVAL(vk_header, 0x10); |
---|
| 1724 | ret_val->unknown1 = SVAL(vk_header, 0x12); |
---|
| 1725 | |
---|
| 1726 | if(ret_val->flag & VK_FLAG_NAME_PRESENT) |
---|
| 1727 | { |
---|
| 1728 | if(ret_val->name_length + REGFI_VK_MIN_LENGTH > ret_val->cell_size) |
---|
| 1729 | { |
---|
| 1730 | if(strict) |
---|
| 1731 | { |
---|
| 1732 | free(ret_val); |
---|
| 1733 | return NULL; |
---|
| 1734 | } |
---|
| 1735 | else |
---|
| 1736 | ret_val->name_length = ret_val->cell_size - REGFI_VK_MIN_LENGTH; |
---|
| 1737 | } |
---|
| 1738 | |
---|
| 1739 | /* Round up to the next multiple of 8 */ |
---|
| 1740 | length = (ret_val->name_length + REGFI_NK_MIN_LENGTH) & 0xFFFFFFF8; |
---|
| 1741 | if(length < ret_val->name_length + REGFI_NK_MIN_LENGTH) |
---|
| 1742 | length+=8; |
---|
| 1743 | |
---|
| 1744 | ret_val->valuename = (char*)zalloc(sizeof(char)*(ret_val->name_length+1)); |
---|
| 1745 | if(ret_val->valuename == NULL) |
---|
| 1746 | { |
---|
| 1747 | free(ret_val); |
---|
| 1748 | return NULL; |
---|
| 1749 | } |
---|
| 1750 | |
---|
| 1751 | /* Don't need to seek, should be at the right offset */ |
---|
| 1752 | length = ret_val->name_length; |
---|
| 1753 | if((regfi_read(file->fd, (uint8*)ret_val->valuename, &length) != 0) |
---|
| 1754 | || length != ret_val->name_length) |
---|
| 1755 | { |
---|
| 1756 | free(ret_val->valuename); |
---|
| 1757 | free(ret_val); |
---|
| 1758 | return NULL; |
---|
| 1759 | } |
---|
| 1760 | ret_val->valuename[ret_val->name_length] = '\0'; |
---|
| 1761 | } |
---|
| 1762 | else |
---|
| 1763 | length = REGFI_VK_MIN_LENGTH; |
---|
| 1764 | |
---|
| 1765 | if(unalloc) |
---|
| 1766 | { |
---|
| 1767 | /* If cell_size is still greater, truncate. */ |
---|
| 1768 | if(length < ret_val->cell_size) |
---|
| 1769 | ret_val->cell_size = length; |
---|
| 1770 | } |
---|
| 1771 | |
---|
| 1772 | if(ret_val->data_size == 0) |
---|
| 1773 | ret_val->data = NULL; |
---|
| 1774 | else |
---|
| 1775 | { |
---|
| 1776 | ret_val->data = regfi_parse_data(file, ret_val->data_off+REGF_BLOCKSIZE, |
---|
| 1777 | raw_data_size, strict); |
---|
| 1778 | if(strict && (ret_val->data == NULL)) |
---|
| 1779 | { |
---|
| 1780 | free(ret_val->valuename); |
---|
| 1781 | free(ret_val); |
---|
| 1782 | return NULL; |
---|
| 1783 | } |
---|
| 1784 | } |
---|
| 1785 | |
---|
| 1786 | return ret_val; |
---|
[97] | 1787 | } |
---|
[101] | 1788 | |
---|
| 1789 | |
---|
| 1790 | uint8* regfi_parse_data(REGF_FILE* file, uint32 offset, uint32 length, bool strict) |
---|
| 1791 | { |
---|
| 1792 | uint8* ret_val; |
---|
| 1793 | uint32 read_length, cell_length; |
---|
| 1794 | bool unalloc; |
---|
| 1795 | |
---|
| 1796 | /* The data is stored in the offset if the size <= 4 */ |
---|
| 1797 | if (length & VK_DATA_IN_OFFSET) |
---|
| 1798 | { |
---|
| 1799 | length = length & ~VK_DATA_IN_OFFSET; |
---|
| 1800 | if(length > 4) |
---|
| 1801 | return NULL; |
---|
| 1802 | |
---|
| 1803 | if((ret_val = (uint8*)zalloc(sizeof(uint8)*length)) == NULL) |
---|
| 1804 | return NULL; |
---|
| 1805 | memcpy(ret_val, &offset, length); |
---|
| 1806 | } |
---|
| 1807 | else |
---|
| 1808 | { |
---|
| 1809 | if(!regfi_parse_cell(file->fd, offset, NULL, 0, |
---|
| 1810 | &cell_length, &unalloc)) |
---|
| 1811 | return NULL; |
---|
| 1812 | |
---|
| 1813 | if(cell_length < 8 || ((cell_length & 0xFFFFFFF8) != cell_length)) |
---|
| 1814 | return NULL; |
---|
| 1815 | |
---|
| 1816 | if(cell_length - 4 < length) |
---|
| 1817 | { |
---|
| 1818 | if(strict) |
---|
| 1819 | return NULL; |
---|
| 1820 | else |
---|
| 1821 | length = cell_length - 4; |
---|
| 1822 | } |
---|
| 1823 | |
---|
| 1824 | /* TODO: There is currently no check to ensure the data |
---|
| 1825 | * cell doesn't cross HBIN boundary. |
---|
| 1826 | */ |
---|
| 1827 | |
---|
| 1828 | if((ret_val = (uint8*)zalloc(sizeof(uint8)*length)) == NULL) |
---|
| 1829 | return NULL; |
---|
| 1830 | |
---|
| 1831 | read_length = length; |
---|
| 1832 | if((regfi_read(file->fd, ret_val, &read_length) != 0) |
---|
| 1833 | || read_length != length) |
---|
| 1834 | { |
---|
| 1835 | free(ret_val); |
---|
| 1836 | return NULL; |
---|
| 1837 | } |
---|
| 1838 | } |
---|
| 1839 | |
---|
| 1840 | return ret_val; |
---|
| 1841 | } |
---|