source: releases/0.3.0/lib/regfio.c @ 289

Last change on this file since 289 was 72, checked in by tim, 18 years ago

Added QWORD type support.

  • Property svn:keywords set to Id
File size: 37.3 KB
Line 
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-2006 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 72 2006-07-30 20:09:07Z tim $
25 */
26
27#include "../include/regfio.h"
28
29
30/* Registry types mapping */
31const unsigned int regfio_num_reg_types = 12;
32static const char* regfio_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* regfio_type_val2str(unsigned int val)
39{
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];
47}
48
49
50/* Returns -1 on error */
51int regfio_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 < 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;
66}
67
68
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  }
125  /* XXX: Is this check right?  0xF == 1|2|4|8, which makes it redundant... */
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;
178  char* ace_delim = "";
179  char* ret_val = NULL;
180  char* tmp_val = NULL;
181  bool failed = false;
182  char field_delim = ':';
183
184  for (i = 0; i < acl->num_aces && !failed; i++)
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   
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);
198
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);
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
261/*******************************************************************
262 *******************************************************************/
263static int read_block( REGF_FILE *file, prs_struct *ps, uint32 file_offset, 
264                       uint32 block_size )
265{
266  const int hdr_size = 0x20;
267  int bytes_read, returned;
268  char *buffer;
269  SMB_STRUCT_STAT sbuf;
270
271  /* check for end of file */
272
273  if ( fstat( file->fd, &sbuf ) ) {
274    /*DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno)));*/
275    return -1;
276  }
277
278  if ( (size_t)file_offset >= sbuf.st_size )
279    return -1;
280       
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 */
283           
284  if ( block_size == 0 ) {
285    uint8 hdr[0x20];
286
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    }
291
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;
306    }
307
308    /* make sure this is an hbin header */
309
310    if ( strncmp( (char*)hdr, "hbin", HBIN_HDR_SIZE ) != 0 ) {
311      /*DEBUG(0,("read_block: invalid block header!\n"));*/
312      return -1;
313    }
314
315    block_size = IVAL( hdr, 0x08 );
316  }
317
318  /*DEBUG(10,("read_block: block_size == 0x%x\n", block_size ));*/
319
320  /* set the offset, initialize the buffer, and read the block from disk */
321
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  }
326       
327  prs_init( ps, block_size, file->mem_ctx, UNMARSHALL );
328  buffer = ps->data_p;
329  bytes_read = returned = 0;
330
331  while ( bytes_read < block_size ) 
332  {
333    returned = read(file->fd, buffer+bytes_read, block_size-bytes_read);
334    if(returned == -1 && errno != EINTR && errno != EAGAIN)
335    {
336      /*DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno) ));*/
337      return -1;
338    }
339
340    if ((returned == 0) && (bytes_read < block_size)) 
341    {
342      /*DEBUG(0,("read_block: not a vald registry file ?\n" ));*/
343      return -1;
344    }   
345
346    bytes_read += returned;
347  }
348       
349  return bytes_read;
350}
351
352
353/*******************************************************************
354 *******************************************************************/
355static bool prs_regf_block(const char *desc, prs_struct *ps, 
356                           int depth, REGF_FILE *file)
357{
358  depth++;
359       
360  if(!prs_uint8s(true, "header", ps, depth, file->header, sizeof(file->header)))
361    return false;
362       
363  /* yes, these values are always identical so store them only once */
364       
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;
369
370  /* get the modtime */
371       
372  if ( !prs_set_offset( ps, 0x0c ) )
373    return false;
374  if ( !smb_io_time( "modtime", &file->mtime, ps, depth ) )
375    return false;
376
377  /* constants */
378       
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;
387
388  /* get file offsets */
389       
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;
396               
397  /* one more constant */
398       
399  if ( !prs_uint32( "unknown6", ps, depth, &file->unknown6 ))
400    return false;
401               
402  /* get the checksum */
403       
404  if ( !prs_set_offset( ps, 0x01fc ) )
405    return false;
406  if ( !prs_uint32( "checksum", ps, depth, &file->checksum ))
407    return false;
408       
409  return true;
410}
411
412
413/*******************************************************************
414 *******************************************************************/
415static bool prs_hbin_block(const char *desc, prs_struct *ps, 
416                           int depth, REGF_HBIN *hbin)
417{
418  uint32 block_size2;
419
420  depth++;
421       
422  if(!prs_uint8s(true, "header", ps, depth, hbin->header, sizeof(hbin->header)))
423    return false;
424
425  if ( !prs_uint32( "first_hbin_off", ps, depth, &hbin->first_hbin_off ))
426    return false;
427
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 */
431
432  if ( !prs_uint32( "block_size", ps, depth, &hbin->block_size ))
433    return false;
434
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;
439
440  if ( !ps->io )
441    hbin->dirty = true;
442       
443
444  return true;
445}
446
447
448/*******************************************************************
449 *******************************************************************/
450static bool prs_nk_rec( const char *desc, prs_struct *ps, 
451                        int depth, REGF_NK_REC *nk )
452{
453  uint16 class_length, name_length;
454  uint32 start;
455  uint32 data_size, start_off, end_off;
456  uint32 unknown_off = REGF_OFFSET_NONE;
457
458  nk->hbin_off = ps->data_offset;
459  start = nk->hbin_off;
460       
461  depth++;
462       
463  /* back up and get the data_size */   
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;
469       
470  if (!prs_uint8s(true, "header", ps, depth, nk->header, sizeof(nk->header)))
471    return false;
472               
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;
477               
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;
484               
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;
491               
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;
502
503  if (!prs_uint32("max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname))
504    return false;
505  if ( !prs_uint32( "max_bytes_subkeyclassname", ps, 
506                    depth, &nk->max_bytes_subkeyclassname))
507  { return false; }
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;
514
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;       
521               
522  if ( class_length ) 
523  {
524    ;;
525  }
526       
527  if ( name_length ) 
528  {
529    if(ps->io && !(nk->keyname = (char*)zcalloc(sizeof(char), name_length+1)))
530        return false;
531
532    if(!prs_uint8s(true, "name", ps, depth, (uint8*)nk->keyname, name_length))
533      return false;
534
535    if(ps->io)
536      nk->keyname[name_length] = '\0';
537  }
538
539  end_off = ps->data_offset;
540
541  /* data_size must be divisible by 8 and large enough to hold
542     the original record */
543
544  data_size = ((start_off - end_off) & 0xfffffff8 );
545  /*if ( data_size > nk->rec_size )
546      DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size));*/
547
548  if ( !ps->io )
549    nk->hbin->dirty = true;
550 
551  nk->subkey_index = 0;
552  return true;
553}
554
555
556/*******************************************************************
557 *******************************************************************/
558static uint32 regf_block_checksum( prs_struct *ps )
559{
560  char *buffer = ps->data_p;
561  uint32 checksum, x;
562  int i;
563
564  /* XOR of all bytes 0x0000 - 0x01FB */
565               
566  checksum = x = 0;
567       
568  for ( i=0; i<0x01FB; i+=4 ) {
569    x = IVAL(buffer, i );
570    checksum ^= x;
571  }
572       
573  return checksum;
574}
575
576
577/*******************************************************************
578 *******************************************************************/
579static bool read_regf_block( REGF_FILE *file )
580{
581  prs_struct ps;
582  uint32 checksum;
583       
584  /* grab the first block from the file */
585               
586  if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) == -1 )
587    return false;
588       
589  /* parse the block and verify the checksum */
590       
591  if ( !prs_regf_block( "regf_header", &ps, 0, file ) )
592    return false;       
593               
594  checksum = regf_block_checksum( &ps );
595       
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;
601
602  if ( file->checksum !=  checksum ) {
603    /*DEBUG(0,("read_regf_block: invalid checksum\n" ));*/
604    return false;
605  }
606
607  return true;
608}
609
610
611/*******************************************************************
612 *******************************************************************/
613static REGF_HBIN* read_hbin_block( REGF_FILE *file, off_t offset )
614{
615  REGF_HBIN *hbin;
616  uint32 record_size, curr_off, block_size, header;
617       
618  if ( !(hbin = (REGF_HBIN*)zalloc(sizeof(REGF_HBIN))) ) 
619    return NULL;
620  hbin->file_off = offset;
621  hbin->free_off = -1;
622               
623  if ( read_block( file, &hbin->ps, offset, 0 ) == -1 )
624    return NULL;
625       
626  if ( !prs_hbin_block( "hbin", &hbin->ps, 0, hbin ) )
627    return NULL;       
628
629  /* this should be the same thing as hbin->block_size but just in case */
630
631  block_size = hbin->ps.buffer_size;
632
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. */
638
639  /* remember that the record_size is in the 4 bytes preceeding the record itself */
640
641  if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE-sizeof(uint32) ) )
642    return false;
643
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 */
649
650    curr_off = curr_off+record_size;
651
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 */
656
657    if ( curr_off >= block_size ) {
658      record_size = -1;
659      curr_off = -1;
660      break;
661    }
662
663    if ( !prs_set_offset( &hbin->ps, curr_off) )
664      return false;
665
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;
670               
671    assert( record_size != 0 );
672
673    if ( record_size & 0x80000000 ) {
674      /* absolute_value(record_size) */
675      record_size = (record_size ^ 0xffffffff) + 1;
676    }
677  }
678
679  /* save the free space offset */
680
681  if ( header == 0xffffffff ) {
682
683    /* account for the fact that the curr_off is 4 bytes behind the actual
684       record header */
685
686    hbin->free_off = curr_off + sizeof(uint32);
687    hbin->free_size = record_size;
688  }
689
690  /*DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin->free_off));*/
691
692  if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE )  )
693    return false;
694       
695  return hbin;
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{
705  if ( !hbin )
706    return false;
707       
708  if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) )
709    return true;
710               
711  return false;
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{
721  REGF_HBIN *hbin = NULL;
722  uint32 block_off;
723
724  /* start with the open list */
725
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  }
731       
732  if ( !hbin ) {
733    /* start at the beginning */
734
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      }
746
747      hbin = read_hbin_block( file, block_off );
748
749      if ( hbin ) 
750        block_off = hbin->file_off + hbin->block_size;
751
752    } while ( hbin && !hbin_contains_offset( hbin, offset ) );
753  }
754
755  if ( hbin )
756    DLIST_ADD( file->block_list, hbin );
757
758  return hbin;
759}
760
761
762/*******************************************************************
763 *******************************************************************/
764static bool prs_hash_rec( const char *desc, prs_struct *ps, int depth, REGF_HASH_REC *hash )
765{
766  depth++;
767
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;
772       
773  return true;
774}
775
776
777/*******************************************************************
778 *******************************************************************/
779static bool hbin_prs_lf_records(const char *desc, REGF_HBIN *hbin, 
780                                int depth, REGF_NK_REC *nk)
781{
782  int i;
783  REGF_LF_REC *lf = &nk->subkeys;
784  uint32 data_size, start_off, end_off;
785
786  depth++;
787
788  /* check if we have anything to do first */
789       
790  if ( nk->num_subkeys == 0 )
791    return true;
792
793  /* move to the LF record */
794
795  if ( !prs_set_offset( &hbin->ps, nk->subkeys_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
796    return false;
797
798  /* backup and get the data_size */
799       
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;
805
806  if(!prs_uint8s(true, "header", &hbin->ps, depth, 
807                 lf->header, sizeof(lf->header)))
808    return false;
809               
810  if ( !prs_uint16( "num_keys", &hbin->ps, depth, &lf->num_keys))
811    return false;
812
813  if ( hbin->ps.io ) {
814    if ( !(lf->hashes = (REGF_HASH_REC*)zcalloc(sizeof(REGF_HASH_REC), lf->num_keys )) )
815      return false;
816  }
817
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  }
822
823  end_off = hbin->ps.data_offset;
824
825  /* data_size must be divisible by 8 and large enough to hold the original record */
826
827  data_size = ((start_off - end_off) & 0xfffffff8 );
828  /*  if ( data_size > lf->rec_size )*/
829    /*DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->rec_size));*/
830
831  if ( !hbin->ps.io )
832    hbin->dirty = true;
833
834  return true;
835}
836
837
838/*******************************************************************
839 *******************************************************************/
840static bool hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_SK_REC *sk )
841{
842  prs_struct *ps = &hbin->ps;
843  uint16 tag = 0xFFFF;
844  uint32 data_size, start_off, end_off;
845
846
847  depth++;
848
849  if ( !prs_set_offset( &hbin->ps, sk->sk_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
850    return false;
851
852  /* backup and get the data_size */
853       
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;
859
860  if (!prs_uint8s(true, "header", ps, depth, sk->header, sizeof(sk->header)))
861    return false;
862  if ( !prs_uint16( "tag", ps, depth, &tag))
863    return false;
864
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;
873
874  if ( !sec_io_desc( "sec_desc", &sk->sec_desc, ps, depth )) 
875    return false;
876
877  end_off = hbin->ps.data_offset;
878
879  /* data_size must be divisible by 8 and large enough to hold the original record */
880
881  data_size = ((start_off - end_off) & 0xfffffff8 );
882  /*  if ( data_size > sk->rec_size )*/
883    /*DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->rec_size));*/
884
885  if ( !hbin->ps.io )
886    hbin->dirty = true;
887
888  return true;
889}
890
891
892/*******************************************************************
893 *******************************************************************/
894static bool hbin_prs_vk_rec( const char *desc, REGF_HBIN *hbin, int depth, 
895                             REGF_VK_REC *vk, REGF_FILE *file )
896{
897  uint32 offset;
898  uint16 name_length;
899  prs_struct *ps = &hbin->ps;
900  uint32 data_size, start_off, end_off;
901
902  depth++;
903
904  /* backup and get the data_size */
905       
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;
911
912  if ( !prs_uint8s( true, "header", ps, depth, vk->header, sizeof( vk->header )) )
913    return false;
914
915  if ( !hbin->ps.io )
916    name_length = strlen(vk->valuename);
917
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;
928
929  offset = ps->data_offset;
930  offset += 2;  /* skip 2 bytes */
931  prs_set_offset( ps, offset );
932
933  /* get the name */
934
935  if ( vk->flag&VK_FLAG_NAME_PRESENT ) {
936
937    if ( hbin->ps.io ) {
938      if ( !(vk->valuename = (char*)zcalloc(sizeof(char), name_length+1 )))
939        return false;
940    }
941    if ( !prs_uint8s(true, "name", ps, depth, 
942                     (uint8*)vk->valuename, name_length) )
943      return false;
944  }
945
946  end_off = hbin->ps.data_offset;
947
948  /* get the data if necessary */
949
950  if ( vk->data_size != 0 ) 
951  {
952    bool charmode = false;
953
954    if ( (vk->type == REG_SZ) || (vk->type == REG_MULTI_SZ) )
955      charmode = true;
956
957    /* the data is stored in the offset if the size <= 4 */
958    if ( !(vk->data_size & VK_DATA_IN_OFFSET) ) 
959    {
960      REGF_HBIN *hblock = hbin;
961      uint32 data_rec_size;
962
963      if ( hbin->ps.io ) 
964      {
965        if ( !(vk->data = (uint8*)zcalloc(sizeof(uint8), vk->data_size) ) )
966          return false;
967      }
968
969      /* this data can be in another hbin */
970      if ( !hbin_contains_offset( hbin, vk->data_off ) ) 
971      {
972        if ( !(hblock = lookup_hbin_block( file, vk->data_off )) )
973          return false;
974      }
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; }
981
982      if ( !hblock->ps.io ) 
983      {
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;
989      if(!prs_uint8s(charmode, "data", &hblock->ps, depth, 
990                     vk->data, vk->data_size))
991        return false;
992
993      if ( !hblock->ps.io )
994        hblock->dirty = true;
995    }
996    else 
997    {
998      if(!(vk->data = zcalloc(sizeof(uint8), 4)))
999        return false;
1000      SIVAL( vk->data, 0, vk->data_off );
1001    }
1002               
1003  }
1004
1005  /* data_size must be divisible by 8 and large enough to hold the original record */
1006
1007  data_size = ((start_off - end_off ) & 0xfffffff8 );
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));*/
1010
1011  if ( !hbin->ps.io )
1012    hbin->dirty = true;
1013
1014  return true;
1015}
1016
1017
1018/*******************************************************************
1019 read a VK record which is contained in the HBIN block stored
1020 in the prs_struct *ps.
1021*******************************************************************/
1022static bool hbin_prs_vk_records(const char *desc, REGF_HBIN *hbin, 
1023                                int depth, REGF_NK_REC *nk, REGF_FILE *file)
1024{
1025  int i;
1026  uint32 record_size;
1027
1028  depth++;
1029       
1030  /* check if we have anything to do first */
1031  if(nk->num_values == 0)
1032    return true;
1033               
1034  if(hbin->ps.io)
1035  {
1036    if (!(nk->values = (REGF_VK_REC*)zcalloc(sizeof(REGF_VK_REC), 
1037                                              nk->num_values )))
1038      return false;
1039  }
1040       
1041  /* convert the offset to something relative to this HBIN block */
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; }
1048
1049  if ( !hbin->ps.io ) 
1050  { 
1051    record_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
1052    record_size = (record_size - 1) ^ 0xFFFFFFFF;
1053  }
1054
1055  if ( !prs_uint32( "record_size", &hbin->ps, depth, &record_size ) )
1056    return false;
1057               
1058  for ( i=0; i<nk->num_values; i++ ) 
1059  {
1060    if ( !prs_uint32( "vk_off", &hbin->ps, depth, &nk->values[i].rec_off ) )
1061      return false;
1062  }
1063
1064  for ( i=0; i<nk->num_values; i++ ) 
1065  {
1066    REGF_HBIN *sub_hbin = hbin;
1067    uint32 new_offset;
1068       
1069    if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) ) 
1070    {
1071      sub_hbin = lookup_hbin_block( file, nk->values[i].rec_off );
1072      if ( !sub_hbin ) 
1073      {
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    }
1079               
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))
1085      return false;
1086    if (!hbin_prs_vk_rec("vk_rec", sub_hbin, depth, &nk->values[i], file))
1087      return false;
1088  }
1089
1090  if ( !hbin->ps.io )
1091    hbin->dirty = true;
1092
1093  return true;
1094}
1095
1096
1097/*******************************************************************
1098 *******************************************************************/
1099static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32 offset )
1100{
1101  REGF_SK_REC *p_sk;
1102       
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  }
1107       
1108  return NULL;
1109}
1110
1111
1112/*******************************************************************
1113 *******************************************************************/
1114static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, SEC_DESC *sd )
1115{
1116  REGF_SK_REC *p;
1117
1118  for ( p=file->sec_desc_list; p; p=p->next ) {
1119    if ( sec_desc_equal( p->sec_desc, sd ) )
1120      return p;
1121  }
1122
1123  /* failure */
1124
1125  return NULL;
1126}
1127
1128
1129/*******************************************************************
1130 *******************************************************************/
1131static bool hbin_prs_key( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk )
1132{
1133  int depth = 0;
1134  REGF_HBIN *sub_hbin;
1135       
1136  depth++;
1137
1138  /* get the initial nk record */
1139  if (!prs_nk_rec("nk_rec", &hbin->ps, depth, nk))
1140    return false;
1141
1142  /* fill in values */
1143  if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) 
1144  {
1145    sub_hbin = hbin;
1146    if ( !hbin_contains_offset( hbin, nk->values_off ) ) 
1147    {
1148      sub_hbin = lookup_hbin_block( file, nk->values_off );
1149      if ( !sub_hbin ) 
1150      {
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    }
1156               
1157    if(!hbin_prs_vk_records("vk_rec", sub_hbin, depth, nk, file))
1158      return false;
1159  }
1160               
1161  /* now get subkeys */
1162  if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) 
1163  {
1164    sub_hbin = hbin;
1165    if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) 
1166    {
1167      sub_hbin = lookup_hbin_block( file, nk->subkeys_off );
1168      if ( !sub_hbin ) 
1169      {
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    }
1175               
1176    if (!hbin_prs_lf_records("lf_rec", sub_hbin, depth, nk))
1177      return false;
1178  }
1179
1180  /* get the to the security descriptor.  First look if we have already parsed it */
1181       
1182  if ((nk->sk_off!=REGF_OFFSET_NONE) 
1183      && !(nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )))
1184  {
1185    sub_hbin = hbin;
1186    if (!hbin_contains_offset(hbin, nk->sk_off))
1187    {
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    }
1195               
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;
1201                       
1202    /* add to the list of security descriptors (ref_count has been read from the files) */
1203
1204    nk->sec_desc->sk_off = nk->sk_off;
1205    DLIST_ADD( file->sec_desc_list, nk->sec_desc );
1206  }
1207               
1208  return true;
1209}
1210
1211
1212/*******************************************************************
1213 *******************************************************************/
1214static bool next_record( REGF_HBIN *hbin, const char *hdr, bool *eob )
1215{
1216  uint8 header[REC_HDR_SIZE] = "";
1217  uint32 record_size;
1218  uint32 curr_off, block_size;
1219  bool found = false;
1220  prs_struct *ps = &hbin->ps;
1221       
1222  curr_off = ps->data_offset;
1223  if ( curr_off == 0 )
1224    prs_set_offset( ps, HBIN_HEADER_REC_SIZE );
1225
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);
1229
1230  block_size = ps->buffer_size;
1231  record_size = 0;
1232  while ( !found ) 
1233  {
1234    curr_off = curr_off+record_size;
1235    if ( curr_off >= block_size ) 
1236      break;
1237
1238    if ( !prs_set_offset( &hbin->ps, curr_off) )
1239      return false;
1240
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;
1245
1246    if ( record_size & 0x80000000 ) {
1247      /* absolute_value(record_size) */
1248      record_size = (record_size ^ 0xffffffff) + 1;
1249    }
1250
1251    if ( memcmp( header, hdr, REC_HDR_SIZE ) == 0 ) {
1252      found = true;
1253      curr_off += sizeof(uint32);
1254    }
1255  } 
1256
1257  /* mark prs_struct as done ( at end ) if no more SK records */
1258  /* mark end-of-block as true */       
1259  if ( !found )
1260  {
1261    prs_set_offset( &hbin->ps, hbin->ps.buffer_size );
1262    *eob = true;
1263    return false;
1264  }
1265
1266  if (!prs_set_offset(ps, curr_off))
1267    return false;
1268
1269  return true;
1270}
1271
1272
1273/*******************************************************************
1274 *******************************************************************/
1275static bool next_nk_record(REGF_FILE *file, REGF_HBIN *hbin, 
1276                           REGF_NK_REC *nk, bool *eob)
1277{
1278  if (next_record(hbin, "nk", eob) 
1279      && hbin_prs_key(file, hbin, nk))
1280    return true;
1281       
1282  return false;
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{
1292  REGF_FILE *rb;
1293  int flags = O_RDONLY;
1294
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;
1301       
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;
1309       
1310  /* open and existing file */
1311
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  }
1317       
1318  /* read in an existing file */
1319       
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  }
1325       
1326  /* success */
1327       
1328  return rb;
1329}
1330
1331
1332/*******************************************************************
1333 *******************************************************************/
1334static void regfio_mem_free( REGF_FILE *file )
1335{
1336  /* free any zalloc()'d memory */
1337       
1338  /*    if ( file && file->mem_ctx )
1339    free(file->mem_ctx);
1340  */
1341}
1342
1343
1344/*******************************************************************
1345 *******************************************************************/
1346int regfio_close( REGF_FILE *file )
1347{
1348  int fd;
1349
1350  regfio_mem_free( file );
1351
1352  /* nothing to do if there is no open file */
1353
1354  if ( !file || (file->fd == -1) )
1355    return 0;
1356               
1357  fd = file->fd;
1358  file->fd = -1;
1359  SAFE_FREE( file );
1360
1361  return close( fd );
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{
1371  REGF_NK_REC *nk;
1372  REGF_HBIN   *hbin;
1373  uint32      offset = REGF_BLOCKSIZE;
1374  bool        found = false;
1375  bool        eob;
1376       
1377  if ( !file )
1378    return NULL;
1379               
1380  if ( !(nk = (REGF_NK_REC*)zalloc(sizeof(REGF_NK_REC) )) ) {
1381    /*DEBUG(0,("regfio_rootkey: zalloc() failed!\n"));*/
1382    return NULL;
1383  }
1384       
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) */
1389       
1390  while ( (hbin = read_hbin_block( file, offset )) ) {
1391    eob = false;
1392
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    }
1406               
1407    if ( found ) 
1408      break;
1409
1410    offset += hbin->block_size;
1411  }
1412       
1413  if ( !found ) {
1414    /*DEBUG(0,("regfio_rootkey: corrupt registry file ?  No root key record located\n"));*/
1415    return NULL;
1416  }
1417
1418  DLIST_ADD( file->block_list, hbin );
1419
1420  return nk;           
1421}
1422
1423
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 */
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{
1434  REGF_NK_REC *subkey;
1435  REGF_HBIN   *hbin;
1436  uint32      nk_offset;
1437
1438  /* see if there is anything left to report */
1439  if (!nk || (nk->subkeys_off==REGF_OFFSET_NONE) 
1440      || (nk->subkey_index >= nk->num_subkeys))
1441    return NULL;
1442
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  }
1451       
1452  nk_offset = nk->subkeys.hashes[nk->subkey_index].nk_off;
1453  if(!prs_set_offset(&hbin->ps, 
1454                     (HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off)))
1455    return NULL;
1456               
1457  nk->subkey_index++;
1458  if(!(subkey = (REGF_NK_REC*)zalloc(sizeof(REGF_NK_REC))))
1459    return NULL;
1460
1461  if(!hbin_prs_key(file, hbin, subkey))
1462    return NULL;
1463
1464  return subkey;
1465}
Note: See TracBrowser for help on using the repository browser.