source: trunk/lib/regfi.c @ 104

Last change on this file since 104 was 104, checked in by tim, 17 years ago

replaced lf (hash list) parsing code

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