source: trunk/lib/regfio.c @ 70

Last change on this file since 70 was 65, checked in by tim, 18 years ago

fixed a bug in types

added paranoid checks in smb_deps to prevent NULL pointer dereferences and corner-case memory leaks.

  • Property svn:keywords set to Id
File size: 37.3 KB
RevLine 
[30]1/*
2 * Branched from Samba project Subversion repository, version #7470:
3 *   http://websvn.samba.org/cgi-bin/viewcvs.cgi/trunk/source/registry/regfio.c
4 *
5 * Unix SMB/CIFS implementation.
6 * Windows NT registry I/O library
7 *
[61]8 * Copyright (C) 2005-2006 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: regfio.c 65 2006-07-23 17:00:20Z tim $
25 */
26
27#include "../include/regfio.h"
28
29
[32]30/* Registry types mapping */
[61]31const unsigned int regfio_num_reg_types = 11;
32static const char* regfio_type_names[] =
[65]33  {"NONE", "SZ", "EXPAND_SZ", "BINARY", "DWORD", "DWORD_BE", "LINK",
[61]34   "MULTI_SZ", "RSRC_LIST", "RSRC_DESC", "RSRC_REQ_LIST"};
[30]35
[32]36
37/* Returns NULL on error */
[41]38const char* regfio_type_val2str(unsigned int val)
[32]39{
[61]40  if(val == REG_KEY)
41    return "KEY";
42 
43  if(val >= regfio_num_reg_types)
44    return NULL;
45 
46  return regfio_type_names[val];
[32]47}
48
49
[61]50/* Returns -1 on error */
[41]51int regfio_type_str2val(const char* str)
[32]52{
53  int i;
54
[61]55  if(strcmp("KEY", str) == 0)
56    return REG_KEY;
[32]57
[61]58  for(i=0; i < regfio_num_reg_types; i++)
59    if (strcmp(regfio_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;
[32]66}
67
68
[53]69/* Security descriptor parsing functions  */
70
71const char* regfio_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: this could probably be more efficient */
88char* regfio_ace_flags2str(uint8 flags)
89{
90  char* flg_output = malloc(21*sizeof(char));
91  int some = 0;
92
93  if(flg_output == NULL)
94    return NULL;
95
96  flg_output[0] = '\0';
97  if (!flags)
98    return flg_output;
99
100  if (flags & 0x01) {
101    if (some) strcat(flg_output, " ");
102    some = 1;
103    strcat(flg_output, "OI");
104  }
105  if (flags & 0x02) {
106    if (some) strcat(flg_output, " ");
107    some = 1;
108    strcat(flg_output, "CI");
109  }
110  if (flags & 0x04) {
111    if (some) strcat(flg_output, " ");
112    some = 1;
113    strcat(flg_output, "NP");
114  }
115  if (flags & 0x08) {
116    if (some) strcat(flg_output, " ");
117    some = 1;
118    strcat(flg_output, "IO");
119  }
120  if (flags & 0x10) {
121    if (some) strcat(flg_output, " ");
122    some = 1;
123    strcat(flg_output, "IA");
124  }
[59]125  /* XXX: Is this check right?  0xF == 1|2|4|8, which makes it redundant... */
[53]126  if (flags == 0xF) {
127    if (some) strcat(flg_output, " ");
128    some = 1;
129    strcat(flg_output, "VI");
130  }
131
132  return flg_output;
133}
134
135
136char* regfio_ace_perms2str(uint32 perms)
137{
138  char* ret_val = malloc(9*sizeof(char));
139  if(ret_val == NULL)
140    return NULL;
141
142  /* XXX: this should probably be parsed better */
143  sprintf(ret_val, "%.8X", perms);
144
145  return ret_val;
146}
147
148
149char* regfio_sid2str(DOM_SID* sid)
150{
151  uint32 i, size = MAXSUBAUTHS*11 + 24;
152  uint32 left = size;
153  uint8 comps = sid->num_auths;
154  char* ret_val = malloc(size);
155 
156  if(ret_val == NULL)
157    return NULL;
158
159  if(comps > MAXSUBAUTHS)
160    comps = MAXSUBAUTHS;
161
162  left -= sprintf(ret_val, "S-%u-%u", sid->sid_rev_num, sid->id_auth[5]);
163
164  for (i = 0; i < comps; i++) 
165    left -= snprintf(ret_val+(size-left), left, "-%u", sid->sub_auths[i]);
166
167  return ret_val;
168}
169
170
171char* regfio_get_acl(SEC_ACL* acl)
172{
173  uint32 i, extra, size = 0;
174  const char* type_str;
175  char* flags_str;
176  char* perms_str;
177  char* sid_str;
[61]178  char* ace_delim = "";
[53]179  char* ret_val = NULL;
[61]180  char* tmp_val = NULL;
181  bool failed = false;
[53]182  char field_delim = ':';
183
[61]184  for (i = 0; i < acl->num_aces && !failed; i++)
[53]185  {
186    sid_str = regfio_sid2str(&acl->ace[i].trustee);
187    type_str = regfio_ace_type2str(acl->ace[i].type);
188    perms_str = regfio_ace_perms2str(acl->ace[i].info.mask);
189    flags_str = regfio_ace_flags2str(acl->ace[i].flags);
190   
[61]191    if(flags_str != NULL && perms_str != NULL 
192       && type_str != NULL && sid_str != NULL)
193    {
194      /* XXX: this is slow */
195      extra = strlen(sid_str) + strlen(type_str) 
196        + strlen(perms_str) + strlen(flags_str)+5;
197      tmp_val = realloc(ret_val, size+extra);
[53]198
[61]199      if(tmp_val == NULL)
200      {
201        free(ret_val);
202        failed = true;
203      }
204      else
205      {
206        ret_val = tmp_val;
207        size += snprintf(ret_val+size, extra, "%s%s%c%s%c%s%c%s",
208                         ace_delim,sid_str,
209                         field_delim,type_str,
210                         field_delim,perms_str,
211                         field_delim,flags_str);
212        ace_delim = "|";
213      }
214    }
215    else
216      failed = true;
217
218    if(sid_str != NULL)
219      free(sid_str);
220    if(sid_str != NULL)
221      free(perms_str);
222    if(sid_str != NULL)
223      free(flags_str);
[53]224  }
225
226  return ret_val;
227}
228
229
230char* regfio_get_sacl(SEC_DESC *sec_desc)
231{
232  if (sec_desc->sacl)
233    return regfio_get_acl(sec_desc->sacl);
234  else
235    return NULL;
236}
237
238
239char* regfio_get_dacl(SEC_DESC *sec_desc)
240{
241  if (sec_desc->dacl)
242    return regfio_get_acl(sec_desc->dacl);
243  else
244    return NULL;
245}
246
247
248char* regfio_get_owner(SEC_DESC *sec_desc)
249{
250  return regfio_sid2str(sec_desc->owner_sid);
251}
252
253
254char* regfio_get_group(SEC_DESC *sec_desc)
255{
256  return regfio_sid2str(sec_desc->grp_sid);
257}
258
259
260
[30]261/*******************************************************************
[31]262 *******************************************************************/
[30]263static int read_block( REGF_FILE *file, prs_struct *ps, uint32 file_offset, 
264                       uint32 block_size )
265{
[61]266  const int hdr_size = 0x20;
[31]267  int bytes_read, returned;
268  char *buffer;
269  SMB_STRUCT_STAT sbuf;
[30]270
[31]271  /* check for end of file */
[30]272
[31]273  if ( fstat( file->fd, &sbuf ) ) {
274    /*DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno)));*/
275    return -1;
276  }
[30]277
[31]278  if ( (size_t)file_offset >= sbuf.st_size )
279    return -1;
[30]280       
[31]281  /* if block_size == 0, we are parsnig HBIN records and need
282     to read some of the header to get the block_size from there */
[30]283           
[31]284  if ( block_size == 0 ) {
285    uint8 hdr[0x20];
[30]286
[31]287    if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
288      /*DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));*/
289      return -1;
290    }
[30]291
[61]292    bytes_read = returned = 0;
293    while (bytes_read < hdr_size)
294    {
295      returned = read(file->fd, hdr + bytes_read, hdr_size - bytes_read);
296      if(returned == -1 && errno != EINTR && errno != EAGAIN)
297      {
298        /*DEBUG(0,("read_block: read of hdr failed (%s)\n",strerror(errno)));*/
299        return -1;
300      }
301
302      if(returned == 0)
303        return -1;
304
305      bytes_read += returned;
[31]306    }
[30]307
[31]308    /* make sure this is an hbin header */
[30]309
[53]310    if ( strncmp( (char*)hdr, "hbin", HBIN_HDR_SIZE ) != 0 ) {
[31]311      /*DEBUG(0,("read_block: invalid block header!\n"));*/
312      return -1;
313    }
[30]314
[31]315    block_size = IVAL( hdr, 0x08 );
316  }
[30]317
[31]318  /*DEBUG(10,("read_block: block_size == 0x%x\n", block_size ));*/
[30]319
[31]320  /* set the offset, initialize the buffer, and read the block from disk */
[30]321
[31]322  if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
323    /*DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));*/
324    return -1;
325  }
[30]326       
[31]327  prs_init( ps, block_size, file->mem_ctx, UNMARSHALL );
328  buffer = ps->data_p;
329  bytes_read = returned = 0;
[30]330
[32]331  while ( bytes_read < block_size ) 
332  {
[61]333    returned = read(file->fd, buffer+bytes_read, block_size-bytes_read);
334    if(returned == -1 && errno != EINTR && errno != EAGAIN)
[32]335    {
[31]336      /*DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno) ));*/
[61]337      return -1;
[31]338    }
[61]339
[32]340    if ((returned == 0) && (bytes_read < block_size)) 
341    {
[31]342      /*DEBUG(0,("read_block: not a vald registry file ?\n" ));*/
[61]343      return -1;
[31]344    }   
[32]345
[31]346    bytes_read += returned;
347  }
[30]348       
[31]349  return bytes_read;
[30]350}
351
352
353/*******************************************************************
[31]354 *******************************************************************/
[53]355static bool prs_regf_block(const char *desc, prs_struct *ps, 
356                           int depth, REGF_FILE *file)
[30]357{
[31]358  depth++;
[30]359       
[53]360  if(!prs_uint8s(true, "header", ps, depth, file->header, sizeof(file->header)))
[31]361    return false;
[30]362       
[31]363  /* yes, these values are always identical so store them only once */
[30]364       
[31]365  if ( !prs_uint32( "unknown1", ps, depth, &file->unknown1 ))
366    return false;
367  if ( !prs_uint32( "unknown1 (again)", ps, depth, &file->unknown1 ))
368    return false;
[30]369
[31]370  /* get the modtime */
[30]371       
[31]372  if ( !prs_set_offset( ps, 0x0c ) )
373    return false;
374  if ( !smb_io_time( "modtime", &file->mtime, ps, depth ) )
375    return false;
[30]376
[31]377  /* constants */
[30]378       
[31]379  if ( !prs_uint32( "unknown2", ps, depth, &file->unknown2 ))
380    return false;
381  if ( !prs_uint32( "unknown3", ps, depth, &file->unknown3 ))
382    return false;
383  if ( !prs_uint32( "unknown4", ps, depth, &file->unknown4 ))
384    return false;
385  if ( !prs_uint32( "unknown5", ps, depth, &file->unknown5 ))
386    return false;
[30]387
[31]388  /* get file offsets */
[30]389       
[31]390  if ( !prs_set_offset( ps, 0x24 ) )
391    return false;
392  if ( !prs_uint32( "data_offset", ps, depth, &file->data_offset ))
393    return false;
394  if ( !prs_uint32( "last_block", ps, depth, &file->last_block ))
395    return false;
[30]396               
[31]397  /* one more constant */
[30]398       
[31]399  if ( !prs_uint32( "unknown6", ps, depth, &file->unknown6 ))
400    return false;
[30]401               
[31]402  /* get the checksum */
[30]403       
[31]404  if ( !prs_set_offset( ps, 0x01fc ) )
405    return false;
406  if ( !prs_uint32( "checksum", ps, depth, &file->checksum ))
407    return false;
[30]408       
[31]409  return true;
[30]410}
411
412
413/*******************************************************************
[31]414 *******************************************************************/
[53]415static bool prs_hbin_block(const char *desc, prs_struct *ps, 
416                           int depth, REGF_HBIN *hbin)
[30]417{
[31]418  uint32 block_size2;
[30]419
[31]420  depth++;
[30]421       
[53]422  if(!prs_uint8s(true, "header", ps, depth, hbin->header, sizeof(hbin->header)))
[31]423    return false;
[30]424
[31]425  if ( !prs_uint32( "first_hbin_off", ps, depth, &hbin->first_hbin_off ))
426    return false;
[30]427
[31]428  /* The dosreg.cpp comments say that the block size is at 0x1c.
429     According to a WINXP NTUSER.dat file, this is wrong.  The block_size
430     is at 0x08 */
[30]431
[31]432  if ( !prs_uint32( "block_size", ps, depth, &hbin->block_size ))
433    return false;
[30]434
[31]435  block_size2 = hbin->block_size;
436  prs_set_offset( ps, 0x1c );
437  if ( !prs_uint32( "block_size2", ps, depth, &block_size2 ))
438    return false;
[30]439
[31]440  if ( !ps->io )
441    hbin->dirty = true;
[30]442       
443
[31]444  return true;
[30]445}
446
447
448/*******************************************************************
[31]449 *******************************************************************/
[33]450static bool prs_nk_rec( const char *desc, prs_struct *ps, 
451                        int depth, REGF_NK_REC *nk )
[30]452{
[31]453  uint16 class_length, name_length;
454  uint32 start;
455  uint32 data_size, start_off, end_off;
456  uint32 unknown_off = REGF_OFFSET_NONE;
[30]457
[31]458  nk->hbin_off = ps->data_offset;
459  start = nk->hbin_off;
[30]460       
[31]461  depth++;
[30]462       
[33]463  /* back up and get the data_size */   
[31]464  if ( !prs_set_offset( ps, ps->data_offset-sizeof(uint32)) )
465    return false;
466  start_off = ps->data_offset;
467  if ( !prs_uint32( "rec_size", ps, depth, &nk->rec_size ))
468    return false;
[30]469       
[33]470  if (!prs_uint8s(true, "header", ps, depth, nk->header, sizeof(nk->header)))
[31]471    return false;
[30]472               
[31]473  if ( !prs_uint16( "key_type", ps, depth, &nk->key_type ))
474    return false;
475  if ( !smb_io_time( "mtime", &nk->mtime, ps, depth ))
476    return false;
[30]477               
[31]478  if ( !prs_set_offset( ps, start+0x0010 ) )
479    return false;
480  if ( !prs_uint32( "parent_off", ps, depth, &nk->parent_off ))
481    return false;
482  if ( !prs_uint32( "num_subkeys", ps, depth, &nk->num_subkeys ))
483    return false;
[30]484               
[31]485  if ( !prs_set_offset( ps, start+0x001c ) )
486    return false;
487  if ( !prs_uint32( "subkeys_off", ps, depth, &nk->subkeys_off ))
488    return false;
489  if ( !prs_uint32( "unknown_off", ps, depth, &unknown_off) )
490    return false;
[30]491               
[31]492  if ( !prs_set_offset( ps, start+0x0024 ) )
493    return false;
494  if ( !prs_uint32( "num_values", ps, depth, &nk->num_values ))
495    return false;
496  if ( !prs_uint32( "values_off", ps, depth, &nk->values_off ))
497    return false;
498  if ( !prs_uint32( "sk_off", ps, depth, &nk->sk_off ))
499    return false;
500  if ( !prs_uint32( "classname_off", ps, depth, &nk->classname_off ))
501    return false;
[30]502
[33]503  if (!prs_uint32("max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname))
[31]504    return false;
[33]505  if ( !prs_uint32( "max_bytes_subkeyclassname", ps, 
506                    depth, &nk->max_bytes_subkeyclassname))
507  { return false; }
[31]508  if ( !prs_uint32( "max_bytes_valuename", ps, depth, &nk->max_bytes_valuename))
509    return false;
510  if ( !prs_uint32( "max_bytes_value", ps, depth, &nk->max_bytes_value))
511    return false;
512  if ( !prs_uint32( "unknown index", ps, depth, &nk->unk_index))
513    return false;
[30]514
[31]515  name_length = nk->keyname ? strlen(nk->keyname) : 0 ;
516  class_length = nk->classname ? strlen(nk->classname) : 0 ;
517  if ( !prs_uint16( "name_length", ps, depth, &name_length ))
518    return false;
519  if ( !prs_uint16( "class_length", ps, depth, &class_length ))
520    return false;       
[30]521               
[33]522  if ( class_length ) 
523  {
[31]524    ;;
525  }
[30]526       
[33]527  if ( name_length ) 
528  {
529    if(ps->io && !(nk->keyname = (char*)zcalloc(sizeof(char), name_length+1)))
[31]530        return false;
[30]531
[53]532    if(!prs_uint8s(true, "name", ps, depth, (uint8*)nk->keyname, name_length))
[31]533      return false;
[30]534
[53]535    if(ps->io)
[31]536      nk->keyname[name_length] = '\0';
537  }
[30]538
[31]539  end_off = ps->data_offset;
[30]540
[33]541  /* data_size must be divisible by 8 and large enough to hold
542     the original record */
[30]543
[31]544  data_size = ((start_off - end_off) & 0xfffffff8 );
[33]545  /*if ( data_size > nk->rec_size )
546      DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size));*/
[30]547
[33]548  if ( !ps->io )
549    nk->hbin->dirty = true;
550 
551  nk->subkey_index = 0;
[31]552  return true;
[30]553}
554
555
556/*******************************************************************
[31]557 *******************************************************************/
[30]558static uint32 regf_block_checksum( prs_struct *ps )
559{
[31]560  char *buffer = ps->data_p;
561  uint32 checksum, x;
562  int i;
[30]563
[31]564  /* XOR of all bytes 0x0000 - 0x01FB */
[30]565               
[31]566  checksum = x = 0;
[30]567       
[31]568  for ( i=0; i<0x01FB; i+=4 ) {
569    x = IVAL(buffer, i );
570    checksum ^= x;
571  }
[30]572       
[31]573  return checksum;
[30]574}
575
576
577/*******************************************************************
[31]578 *******************************************************************/
[30]579static bool read_regf_block( REGF_FILE *file )
580{
[31]581  prs_struct ps;
582  uint32 checksum;
[30]583       
[31]584  /* grab the first block from the file */
[30]585               
[31]586  if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) == -1 )
587    return false;
[30]588       
[31]589  /* parse the block and verify the checksum */
[30]590       
[31]591  if ( !prs_regf_block( "regf_header", &ps, 0, file ) )
592    return false;       
[30]593               
[31]594  checksum = regf_block_checksum( &ps );
[30]595       
[31]596  if(ps.is_dynamic)
597    SAFE_FREE(ps.data_p);
598  ps.is_dynamic = false;
599  ps.buffer_size = 0;
600  ps.data_offset = 0;
[30]601
[31]602  if ( file->checksum !=  checksum ) {
603    /*DEBUG(0,("read_regf_block: invalid checksum\n" ));*/
604    return false;
605  }
[30]606
[31]607  return true;
[30]608}
609
610
611/*******************************************************************
[31]612 *******************************************************************/
[30]613static REGF_HBIN* read_hbin_block( REGF_FILE *file, off_t offset )
614{
[31]615  REGF_HBIN *hbin;
616  uint32 record_size, curr_off, block_size, header;
[30]617       
[31]618  if ( !(hbin = (REGF_HBIN*)zalloc(sizeof(REGF_HBIN))) ) 
619    return NULL;
620  hbin->file_off = offset;
621  hbin->free_off = -1;
[30]622               
[31]623  if ( read_block( file, &hbin->ps, offset, 0 ) == -1 )
624    return NULL;
[30]625       
[31]626  if ( !prs_hbin_block( "hbin", &hbin->ps, 0, hbin ) )
627    return NULL;       
[30]628
[31]629  /* this should be the same thing as hbin->block_size but just in case */
[30]630
[31]631  block_size = hbin->ps.buffer_size;
[30]632
[31]633  /* Find the available free space offset.  Always at the end,
634     so walk the record list and stop when you get to the end.
635     The end is defined by a record header of 0xffffffff.  The
636     previous 4 bytes contains the amount of free space remaining
637     in the hbin block. */
[30]638
[31]639  /* remember that the record_size is in the 4 bytes preceeding the record itself */
[30]640
[31]641  if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE-sizeof(uint32) ) )
642    return false;
[30]643
[31]644  record_size = 0;
645  curr_off = hbin->ps.data_offset;
646  while ( header != 0xffffffff ) {
647    /* not done yet so reset the current offset to the
648       next record_size field */
[30]649
[31]650    curr_off = curr_off+record_size;
[30]651
[31]652    /* for some reason the record_size of the last record in
653       an hbin block can extend past the end of the block
654       even though the record fits within the remaining
655       space....aaarrrgggghhhhhh */
[30]656
[31]657    if ( curr_off >= block_size ) {
658      record_size = -1;
659      curr_off = -1;
660      break;
661    }
[30]662
[31]663    if ( !prs_set_offset( &hbin->ps, curr_off) )
664      return false;
[30]665
[31]666    if ( !prs_uint32( "rec_size", &hbin->ps, 0, &record_size ) )
667      return false;
668    if ( !prs_uint32( "header", &hbin->ps, 0, &header ) )
669      return false;
[30]670               
[31]671    assert( record_size != 0 );
[30]672
[31]673    if ( record_size & 0x80000000 ) {
674      /* absolute_value(record_size) */
675      record_size = (record_size ^ 0xffffffff) + 1;
676    }
677  }
[30]678
[31]679  /* save the free space offset */
[30]680
[31]681  if ( header == 0xffffffff ) {
[30]682
[31]683    /* account for the fact that the curr_off is 4 bytes behind the actual
684       record header */
[30]685
[31]686    hbin->free_off = curr_off + sizeof(uint32);
687    hbin->free_size = record_size;
688  }
[30]689
[31]690  /*DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin->free_off));*/
[30]691
[31]692  if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE )  )
693    return false;
[30]694       
[31]695  return hbin;
[30]696}
697
698
699/*******************************************************************
700 Input a randon offset and receive the correpsonding HBIN
701 block for it
702*******************************************************************/
703static bool hbin_contains_offset( REGF_HBIN *hbin, uint32 offset )
704{
[31]705  if ( !hbin )
706    return false;
[30]707       
[31]708  if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) )
709    return true;
[30]710               
[31]711  return false;
[30]712}
713
714
715/*******************************************************************
716 Input a randon offset and receive the correpsonding HBIN
717 block for it
718*******************************************************************/
719static REGF_HBIN* lookup_hbin_block( REGF_FILE *file, uint32 offset )
720{
[31]721  REGF_HBIN *hbin = NULL;
722  uint32 block_off;
[30]723
[31]724  /* start with the open list */
[30]725
[31]726  for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
727    /* DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%x]\n", hbin->file_off, (uint32)hbin ));*/
728    if ( hbin_contains_offset( hbin, offset ) )
729      return hbin;
730  }
[30]731       
[31]732  if ( !hbin ) {
733    /* start at the beginning */
[30]734
[31]735    block_off = REGF_BLOCKSIZE;
736    do {
737      /* cleanup before the next round */
738      if ( hbin )
739      {
740        if(hbin->ps.is_dynamic)
741          SAFE_FREE(hbin->ps.data_p);
742        hbin->ps.is_dynamic = false;
743        hbin->ps.buffer_size = 0;
744        hbin->ps.data_offset = 0;
745      }
[30]746
[31]747      hbin = read_hbin_block( file, block_off );
[30]748
[31]749      if ( hbin ) 
750        block_off = hbin->file_off + hbin->block_size;
[30]751
[31]752    } while ( hbin && !hbin_contains_offset( hbin, offset ) );
753  }
[30]754
[31]755  if ( hbin )
756    DLIST_ADD( file->block_list, hbin );
[30]757
[31]758  return hbin;
[30]759}
760
761
762/*******************************************************************
[31]763 *******************************************************************/
[30]764static bool prs_hash_rec( const char *desc, prs_struct *ps, int depth, REGF_HASH_REC *hash )
765{
[31]766  depth++;
[30]767
[31]768  if ( !prs_uint32( "nk_off", ps, depth, &hash->nk_off ))
769    return false;
770  if ( !prs_uint8s( true, "keycheck", ps, depth, hash->keycheck, sizeof( hash->keycheck )) )
771    return false;
[30]772       
[31]773  return true;
[30]774}
775
776
777/*******************************************************************
[31]778 *******************************************************************/
[53]779static bool hbin_prs_lf_records(const char *desc, REGF_HBIN *hbin, 
780                                int depth, REGF_NK_REC *nk)
[30]781{
[31]782  int i;
783  REGF_LF_REC *lf = &nk->subkeys;
784  uint32 data_size, start_off, end_off;
[30]785
[31]786  depth++;
[30]787
[31]788  /* check if we have anything to do first */
[30]789       
[31]790  if ( nk->num_subkeys == 0 )
791    return true;
[30]792
[31]793  /* move to the LF record */
[30]794
[31]795  if ( !prs_set_offset( &hbin->ps, nk->subkeys_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
796    return false;
[30]797
[31]798  /* backup and get the data_size */
[30]799       
[31]800  if ( !prs_set_offset( &hbin->ps, hbin->ps.data_offset-sizeof(uint32)) )
801    return false;
802  start_off = hbin->ps.data_offset;
803  if ( !prs_uint32( "rec_size", &hbin->ps, depth, &lf->rec_size ))
804    return false;
[30]805
[53]806  if(!prs_uint8s(true, "header", &hbin->ps, depth, 
807                 lf->header, sizeof(lf->header)))
[31]808    return false;
[30]809               
[31]810  if ( !prs_uint16( "num_keys", &hbin->ps, depth, &lf->num_keys))
811    return false;
[30]812
[31]813  if ( hbin->ps.io ) {
814    if ( !(lf->hashes = (REGF_HASH_REC*)zcalloc(sizeof(REGF_HASH_REC), lf->num_keys )) )
815      return false;
816  }
[30]817
[31]818  for ( i=0; i<lf->num_keys; i++ ) {
819    if ( !prs_hash_rec( "hash_rec", &hbin->ps, depth, &lf->hashes[i] ) )
820      return false;
821  }
[30]822
[31]823  end_off = hbin->ps.data_offset;
[30]824
[31]825  /* data_size must be divisible by 8 and large enough to hold the original record */
[30]826
[31]827  data_size = ((start_off - end_off) & 0xfffffff8 );
[33]828  /*  if ( data_size > lf->rec_size )*/
[31]829    /*DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->rec_size));*/
[30]830
[33]831  if ( !hbin->ps.io )
832    hbin->dirty = true;
[30]833
[31]834  return true;
[30]835}
836
837
838/*******************************************************************
[31]839 *******************************************************************/
[30]840static bool hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_SK_REC *sk )
841{
[31]842  prs_struct *ps = &hbin->ps;
843  uint16 tag = 0xFFFF;
844  uint32 data_size, start_off, end_off;
[30]845
846
[31]847  depth++;
[30]848
[31]849  if ( !prs_set_offset( &hbin->ps, sk->sk_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
850    return false;
[30]851
[31]852  /* backup and get the data_size */
[30]853       
[31]854  if ( !prs_set_offset( &hbin->ps, hbin->ps.data_offset-sizeof(uint32)) )
855    return false;
856  start_off = hbin->ps.data_offset;
857  if ( !prs_uint32( "rec_size", &hbin->ps, depth, &sk->rec_size ))
858    return false;
[30]859
[53]860  if (!prs_uint8s(true, "header", ps, depth, sk->header, sizeof(sk->header)))
[31]861    return false;
862  if ( !prs_uint16( "tag", ps, depth, &tag))
863    return false;
[30]864
[31]865  if ( !prs_uint32( "prev_sk_off", ps, depth, &sk->prev_sk_off))
866    return false;
867  if ( !prs_uint32( "next_sk_off", ps, depth, &sk->next_sk_off))
868    return false;
869  if ( !prs_uint32( "ref_count", ps, depth, &sk->ref_count))
870    return false;
871  if ( !prs_uint32( "size", ps, depth, &sk->size))
872    return false;
[30]873
[31]874  if ( !sec_io_desc( "sec_desc", &sk->sec_desc, ps, depth )) 
875    return false;
[30]876
[31]877  end_off = hbin->ps.data_offset;
[30]878
[31]879  /* data_size must be divisible by 8 and large enough to hold the original record */
[30]880
[31]881  data_size = ((start_off - end_off) & 0xfffffff8 );
[33]882  /*  if ( data_size > sk->rec_size )*/
[31]883    /*DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->rec_size));*/
[30]884
[33]885  if ( !hbin->ps.io )
886    hbin->dirty = true;
[30]887
[31]888  return true;
[30]889}
890
891
892/*******************************************************************
[31]893 *******************************************************************/
[30]894static bool hbin_prs_vk_rec( const char *desc, REGF_HBIN *hbin, int depth, 
895                             REGF_VK_REC *vk, REGF_FILE *file )
896{
[31]897  uint32 offset;
898  uint16 name_length;
899  prs_struct *ps = &hbin->ps;
900  uint32 data_size, start_off, end_off;
[30]901
[31]902  depth++;
[30]903
[31]904  /* backup and get the data_size */
[30]905       
[31]906  if ( !prs_set_offset( &hbin->ps, hbin->ps.data_offset-sizeof(uint32)) )
907    return false;
908  start_off = hbin->ps.data_offset;
909  if ( !prs_uint32( "rec_size", &hbin->ps, depth, &vk->rec_size ))
910    return false;
[30]911
[31]912  if ( !prs_uint8s( true, "header", ps, depth, vk->header, sizeof( vk->header )) )
913    return false;
[30]914
[31]915  if ( !hbin->ps.io )
916    name_length = strlen(vk->valuename);
[30]917
[31]918  if ( !prs_uint16( "name_length", ps, depth, &name_length ))
919    return false;
920  if ( !prs_uint32( "data_size", ps, depth, &vk->data_size ))
921    return false;
922  if ( !prs_uint32( "data_off", ps, depth, &vk->data_off ))
923    return false;
924  if ( !prs_uint32( "type", ps, depth, &vk->type))
925    return false;
926  if ( !prs_uint16( "flag", ps, depth, &vk->flag))
927    return false;
[30]928
[31]929  offset = ps->data_offset;
930  offset += 2;  /* skip 2 bytes */
931  prs_set_offset( ps, offset );
[30]932
[31]933  /* get the name */
[30]934
[31]935  if ( vk->flag&VK_FLAG_NAME_PRESENT ) {
[30]936
[31]937    if ( hbin->ps.io ) {
938      if ( !(vk->valuename = (char*)zcalloc(sizeof(char), name_length+1 )))
939        return false;
940    }
[53]941    if ( !prs_uint8s(true, "name", ps, depth, 
942                     (uint8*)vk->valuename, name_length) )
[31]943      return false;
944  }
[30]945
[31]946  end_off = hbin->ps.data_offset;
[30]947
[31]948  /* get the data if necessary */
[30]949
[32]950  if ( vk->data_size != 0 ) 
951  {
[31]952    bool charmode = false;
[30]953
[31]954    if ( (vk->type == REG_SZ) || (vk->type == REG_MULTI_SZ) )
955      charmode = true;
[30]956
[31]957    /* the data is stored in the offset if the size <= 4 */
[32]958    if ( !(vk->data_size & VK_DATA_IN_OFFSET) ) 
959    {
[31]960      REGF_HBIN *hblock = hbin;
961      uint32 data_rec_size;
[30]962
[32]963      if ( hbin->ps.io ) 
964      {
[31]965        if ( !(vk->data = (uint8*)zcalloc(sizeof(uint8), vk->data_size) ) )
966          return false;
967      }
[30]968
[31]969      /* this data can be in another hbin */
[32]970      if ( !hbin_contains_offset( hbin, vk->data_off ) ) 
971      {
[31]972        if ( !(hblock = lookup_hbin_block( file, vk->data_off )) )
973          return false;
974      }
[32]975      if (!(prs_set_offset(&hblock->ps, 
976                           (vk->data_off
977                            + HBIN_HDR_SIZE
978                            - hblock->first_hbin_off)
979                           - sizeof(uint32))))
980      { return false; }
[30]981
[32]982      if ( !hblock->ps.io ) 
983      {
[31]984        data_rec_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
985        data_rec_size = ( data_rec_size - 1 ) ^ 0xFFFFFFFF;
986      }
987      if ( !prs_uint32( "data_rec_size", &hblock->ps, depth, &data_rec_size ))
988        return false;
[32]989      if(!prs_uint8s(charmode, "data", &hblock->ps, depth, 
990                     vk->data, vk->data_size))
[31]991        return false;
[30]992
[31]993      if ( !hblock->ps.io )
994        hblock->dirty = true;
995    }
[32]996    else 
997    {
998      if(!(vk->data = zcalloc(sizeof(uint8), 4)))
[31]999        return false;
1000      SIVAL( vk->data, 0, vk->data_off );
1001    }
[30]1002               
[31]1003  }
[30]1004
[31]1005  /* data_size must be divisible by 8 and large enough to hold the original record */
[30]1006
[31]1007  data_size = ((start_off - end_off ) & 0xfffffff8 );
[32]1008  /*if ( data_size !=  vk->rec_size )
1009    DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size, vk->rec_size));*/
[30]1010
[32]1011  if ( !hbin->ps.io )
1012    hbin->dirty = true;
[30]1013
[31]1014  return true;
[30]1015}
1016
1017
1018/*******************************************************************
1019 read a VK record which is contained in the HBIN block stored
1020 in the prs_struct *ps.
1021*******************************************************************/
[32]1022static bool hbin_prs_vk_records(const char *desc, REGF_HBIN *hbin, 
1023                                int depth, REGF_NK_REC *nk, REGF_FILE *file)
[30]1024{
[31]1025  int i;
1026  uint32 record_size;
[30]1027
[31]1028  depth++;
[30]1029       
[31]1030  /* check if we have anything to do first */
[32]1031  if(nk->num_values == 0)
[31]1032    return true;
[30]1033               
[32]1034  if(hbin->ps.io)
1035  {
1036    if (!(nk->values = (REGF_VK_REC*)zcalloc(sizeof(REGF_VK_REC), 
1037                                              nk->num_values )))
[31]1038      return false;
1039  }
[30]1040       
[31]1041  /* convert the offset to something relative to this HBIN block */
[32]1042  if (!prs_set_offset(&hbin->ps, 
1043                      nk->values_off
1044                      + HBIN_HDR_SIZE
1045                      - hbin->first_hbin_off
1046                      - sizeof(uint32)))
1047  { return false; }
[30]1048
[32]1049  if ( !hbin->ps.io ) 
1050  { 
[31]1051    record_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
1052    record_size = (record_size - 1) ^ 0xFFFFFFFF;
1053  }
[30]1054
[31]1055  if ( !prs_uint32( "record_size", &hbin->ps, depth, &record_size ) )
1056    return false;
[30]1057               
[32]1058  for ( i=0; i<nk->num_values; i++ ) 
1059  {
[31]1060    if ( !prs_uint32( "vk_off", &hbin->ps, depth, &nk->values[i].rec_off ) )
1061      return false;
1062  }
[30]1063
[32]1064  for ( i=0; i<nk->num_values; i++ ) 
1065  {
[31]1066    REGF_HBIN *sub_hbin = hbin;
1067    uint32 new_offset;
[30]1068       
[32]1069    if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) ) 
1070    {
[31]1071      sub_hbin = lookup_hbin_block( file, nk->values[i].rec_off );
[32]1072      if ( !sub_hbin ) 
1073      {
[31]1074        /*DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n",
1075          nk->values[i].hbin_off));*/
1076        return false;
1077      }
1078    }
[30]1079               
[32]1080    new_offset = nk->values[i].rec_off
1081      + HBIN_HDR_SIZE
1082      - sub_hbin->first_hbin_off;
1083
1084    if (!prs_set_offset(&sub_hbin->ps, new_offset))
[31]1085      return false;
[32]1086    if (!hbin_prs_vk_rec("vk_rec", sub_hbin, depth, &nk->values[i], file))
[31]1087      return false;
1088  }
[30]1089
[31]1090  if ( !hbin->ps.io )
1091    hbin->dirty = true;
[30]1092
[31]1093  return true;
[30]1094}
1095
1096
1097/*******************************************************************
[31]1098 *******************************************************************/
[30]1099static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32 offset )
1100{
[31]1101  REGF_SK_REC *p_sk;
[30]1102       
[31]1103  for ( p_sk=file->sec_desc_list; p_sk; p_sk=p_sk->next ) {
1104    if ( p_sk->sk_off == offset ) 
1105      return p_sk;
1106  }
[30]1107       
[31]1108  return NULL;
[30]1109}
1110
[32]1111
[30]1112/*******************************************************************
[31]1113 *******************************************************************/
[30]1114static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, SEC_DESC *sd )
1115{
[31]1116  REGF_SK_REC *p;
[30]1117
[31]1118  for ( p=file->sec_desc_list; p; p=p->next ) {
1119    if ( sec_desc_equal( p->sec_desc, sd ) )
1120      return p;
1121  }
[30]1122
[31]1123  /* failure */
[30]1124
[31]1125  return NULL;
[30]1126}
1127
[32]1128
[30]1129/*******************************************************************
[31]1130 *******************************************************************/
[30]1131static bool hbin_prs_key( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk )
1132{
[31]1133  int depth = 0;
1134  REGF_HBIN *sub_hbin;
[30]1135       
[31]1136  depth++;
[30]1137
[31]1138  /* get the initial nk record */
[33]1139  if (!prs_nk_rec("nk_rec", &hbin->ps, depth, nk))
[31]1140    return false;
[30]1141
[31]1142  /* fill in values */
[32]1143  if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) 
1144  {
[31]1145    sub_hbin = hbin;
[32]1146    if ( !hbin_contains_offset( hbin, nk->values_off ) ) 
1147    {
[31]1148      sub_hbin = lookup_hbin_block( file, nk->values_off );
[32]1149      if ( !sub_hbin ) 
1150      {
[31]1151        /*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n",
1152          nk->values_off));*/
1153        return false;
1154      }
1155    }
[30]1156               
[32]1157    if(!hbin_prs_vk_records("vk_rec", sub_hbin, depth, nk, file))
[31]1158      return false;
1159  }
[30]1160               
[31]1161  /* now get subkeys */
[32]1162  if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) 
1163  {
[31]1164    sub_hbin = hbin;
[32]1165    if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) 
1166    {
[31]1167      sub_hbin = lookup_hbin_block( file, nk->subkeys_off );
[32]1168      if ( !sub_hbin ) 
1169      {
[31]1170        /*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n",
1171          nk->subkeys_off));*/
1172        return false;
1173      }
1174    }
[30]1175               
[32]1176    if (!hbin_prs_lf_records("lf_rec", sub_hbin, depth, nk))
[31]1177      return false;
1178  }
[30]1179
[31]1180  /* get the to the security descriptor.  First look if we have already parsed it */
[30]1181       
[32]1182  if ((nk->sk_off!=REGF_OFFSET_NONE) 
1183      && !(nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )))
1184  {
[31]1185    sub_hbin = hbin;
[32]1186    if (!hbin_contains_offset(hbin, nk->sk_off))
1187    {
[31]1188      sub_hbin = lookup_hbin_block( file, nk->sk_off );
1189      if ( !sub_hbin ) {
1190        /*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_offset [0x%x]\n",
1191          nk->subkeys_off));*/
1192        return false;
1193      }
1194    }
[30]1195               
[31]1196    if ( !(nk->sec_desc = (REGF_SK_REC*)zalloc(sizeof(REGF_SK_REC) )) )
1197      return false;
1198    nk->sec_desc->sk_off = nk->sk_off;
1199    if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin, depth, nk->sec_desc ))
1200      return false;
[30]1201                       
[31]1202    /* add to the list of security descriptors (ref_count has been read from the files) */
[30]1203
[31]1204    nk->sec_desc->sk_off = nk->sk_off;
1205    DLIST_ADD( file->sec_desc_list, nk->sec_desc );
1206  }
[30]1207               
[31]1208  return true;
[30]1209}
1210
[32]1211
[30]1212/*******************************************************************
[31]1213 *******************************************************************/
[30]1214static bool next_record( REGF_HBIN *hbin, const char *hdr, bool *eob )
1215{
[53]1216  uint8 header[REC_HDR_SIZE] = "";
[31]1217  uint32 record_size;
1218  uint32 curr_off, block_size;
1219  bool found = false;
1220  prs_struct *ps = &hbin->ps;
[30]1221       
[31]1222  curr_off = ps->data_offset;
1223  if ( curr_off == 0 )
1224    prs_set_offset( ps, HBIN_HEADER_REC_SIZE );
[30]1225
[31]1226  /* assume that the current offset is at the reacord header
1227     and we need to backup to read the record size */
1228  curr_off -= sizeof(uint32);
[30]1229
[31]1230  block_size = ps->buffer_size;
1231  record_size = 0;
[32]1232  while ( !found ) 
1233  {
[31]1234    curr_off = curr_off+record_size;
1235    if ( curr_off >= block_size ) 
1236      break;
[30]1237
[31]1238    if ( !prs_set_offset( &hbin->ps, curr_off) )
1239      return false;
[30]1240
[31]1241    if ( !prs_uint32( "record_size", ps, 0, &record_size ) )
1242      return false;
1243    if ( !prs_uint8s( true, "header", ps, 0, header, REC_HDR_SIZE ) )
1244      return false;
[30]1245
[31]1246    if ( record_size & 0x80000000 ) {
1247      /* absolute_value(record_size) */
1248      record_size = (record_size ^ 0xffffffff) + 1;
1249    }
[30]1250
[31]1251    if ( memcmp( header, hdr, REC_HDR_SIZE ) == 0 ) {
1252      found = true;
1253      curr_off += sizeof(uint32);
1254    }
1255  } 
[30]1256
[31]1257  /* mark prs_struct as done ( at end ) if no more SK records */
[32]1258  /* mark end-of-block as true */       
1259  if ( !found )
[31]1260  {
1261    prs_set_offset( &hbin->ps, hbin->ps.buffer_size );
1262    *eob = true;
1263    return false;
1264  }
[32]1265
1266  if (!prs_set_offset(ps, curr_off))
[31]1267    return false;
[30]1268
[31]1269  return true;
[30]1270}
1271
1272
1273/*******************************************************************
[31]1274 *******************************************************************/
[32]1275static bool next_nk_record(REGF_FILE *file, REGF_HBIN *hbin, 
1276                           REGF_NK_REC *nk, bool *eob)
[30]1277{
[32]1278  if (next_record(hbin, "nk", eob) 
1279      && hbin_prs_key(file, hbin, nk))
[31]1280    return true;
[30]1281       
[31]1282  return false;
[30]1283}
1284
1285
1286/*******************************************************************
1287 Open the registry file and then read in the REGF block to get the
1288 first hbin offset.
1289*******************************************************************/
1290REGF_FILE* regfio_open( const char *filename )
1291{
[31]1292  REGF_FILE *rb;
1293  int flags = O_RDONLY;
[30]1294
[31]1295  if ( !(rb = (REGF_FILE*)malloc(sizeof(REGF_FILE))) ) {
1296    /* DEBUG(0,("ERROR allocating memory\n")); */
1297    return NULL;
1298  }
1299  memset(rb, 0, sizeof(REGF_FILE));
1300  rb->fd = -1;
[30]1301       
[31]1302  /*    if ( !(rb->mem_ctx = talloc_init( "read_regf_block" )) )
1303    {
1304    regfio_close( rb );
1305    return NULL;
1306    }
1307  */
1308  rb->open_flags = flags;
[30]1309       
[31]1310  /* open and existing file */
[30]1311
[31]1312  if ( (rb->fd = open(filename, flags)) == -1 ) {
1313    /* DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename, strerror(errno)));*/
1314    regfio_close( rb );
1315    return NULL;
1316  }
[30]1317       
[31]1318  /* read in an existing file */
[30]1319       
[31]1320  if ( !read_regf_block( rb ) ) {
1321    /* DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));*/
1322    regfio_close( rb );
1323    return NULL;
1324  }
[30]1325       
[31]1326  /* success */
[30]1327       
[31]1328  return rb;
[30]1329}
1330
1331
1332/*******************************************************************
[31]1333 *******************************************************************/
[30]1334static void regfio_mem_free( REGF_FILE *file )
1335{
[31]1336  /* free any zalloc()'d memory */
[30]1337       
1338  /*    if ( file && file->mem_ctx )
[31]1339    free(file->mem_ctx);
[30]1340  */
1341}
1342
1343
1344/*******************************************************************
[31]1345 *******************************************************************/
[30]1346int regfio_close( REGF_FILE *file )
1347{
[31]1348  int fd;
[30]1349
[31]1350  regfio_mem_free( file );
[30]1351
[31]1352  /* nothing to do if there is no open file */
[30]1353
[31]1354  if ( !file || (file->fd == -1) )
1355    return 0;
[30]1356               
[31]1357  fd = file->fd;
1358  file->fd = -1;
1359  SAFE_FREE( file );
[30]1360
[31]1361  return close( fd );
[30]1362}
1363
1364
1365/*******************************************************************
1366 There should be only *one* root key in the registry file based
1367 on my experience.  --jerry
1368*******************************************************************/
1369REGF_NK_REC* regfio_rootkey( REGF_FILE *file )
1370{
[31]1371  REGF_NK_REC *nk;
1372  REGF_HBIN   *hbin;
1373  uint32      offset = REGF_BLOCKSIZE;
1374  bool        found = false;
1375  bool        eob;
[30]1376       
[31]1377  if ( !file )
1378    return NULL;
[30]1379               
[31]1380  if ( !(nk = (REGF_NK_REC*)zalloc(sizeof(REGF_NK_REC) )) ) {
1381    /*DEBUG(0,("regfio_rootkey: zalloc() failed!\n"));*/
1382    return NULL;
1383  }
[30]1384       
[31]1385  /* scan through the file on HBIN block at a time looking
1386     for an NK record with a type == 0x002c.
1387     Normally this is the first nk record in the first hbin
1388     block (but I'm not assuming that for now) */
[30]1389       
[31]1390  while ( (hbin = read_hbin_block( file, offset )) ) {
1391    eob = false;
[30]1392
[31]1393    while ( !eob) {
1394      if ( next_nk_record( file, hbin, nk, &eob ) ) {
1395        if ( nk->key_type == NK_TYPE_ROOTKEY ) {
1396          found = true;
1397          break;
1398        }
1399      }
1400      if(hbin->ps.is_dynamic)
1401        SAFE_FREE(hbin->ps.data_p);
1402      hbin->ps.is_dynamic = false;
1403      hbin->ps.buffer_size = 0;
1404      hbin->ps.data_offset = 0;
1405    }
[30]1406               
[31]1407    if ( found ) 
1408      break;
[30]1409
[31]1410    offset += hbin->block_size;
1411  }
[30]1412       
[31]1413  if ( !found ) {
1414    /*DEBUG(0,("regfio_rootkey: corrupt registry file ?  No root key record located\n"));*/
1415    return NULL;
1416  }
[30]1417
[31]1418  DLIST_ADD( file->block_list, hbin );
[30]1419
[31]1420  return nk;           
[30]1421}
1422
1423
[33]1424/* XXX: An interator struct should be used instead, and this function
1425 *   should operate on it, so the state of iteration isn't stored in the
1426 * REGF_NK_REC struct itself.
1427 */
[30]1428/*******************************************************************
1429 This acts as an interator over the subkeys defined for a given
1430 NK record.  Remember that offsets are from the *first* HBIN block.
1431*******************************************************************/
1432REGF_NK_REC* regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk )
1433{
[31]1434  REGF_NK_REC *subkey;
1435  REGF_HBIN   *hbin;
1436  uint32      nk_offset;
[30]1437
[31]1438  /* see if there is anything left to report */
[32]1439  if (!nk || (nk->subkeys_off==REGF_OFFSET_NONE) 
1440      || (nk->subkey_index >= nk->num_subkeys))
[31]1441    return NULL;
[30]1442
[31]1443  /* find the HBIN block which should contain the nk record */
1444  if(!(hbin
1445       = lookup_hbin_block(file, nk->subkeys.hashes[nk->subkey_index].nk_off )))
1446  {
1447    /*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n",
1448      nk->subkeys.hashes[nk->subkey_index].nk_off));*/
1449    return NULL;
1450  }
[30]1451       
[31]1452  nk_offset = nk->subkeys.hashes[nk->subkey_index].nk_off;
[32]1453  if(!prs_set_offset(&hbin->ps, 
1454                     (HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off)))
[31]1455    return NULL;
[30]1456               
[31]1457  nk->subkey_index++;
[32]1458  if(!(subkey = (REGF_NK_REC*)zalloc(sizeof(REGF_NK_REC))))
[31]1459    return NULL;
[30]1460
[32]1461  if(!hbin_prs_key(file, hbin, subkey))
[31]1462    return NULL;
[32]1463
[31]1464  return subkey;
[30]1465}
Note: See TracBrowser for help on using the repository browser.