source: trunk/lib/regfio.c @ 32

Last change on this file since 32 was 32, checked in by tim, 19 years ago

Added key name retrieval to test utility

Code formatting changes to regfio.c

Added type<->string mapping functions to regfio library

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