507f720823538bc0be618b806f71f5bbe0e715f6
[kai/samba.git] / source3 / registry / regfio.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Windows NT registry I/O library
4  * Copyright (c) Gerald (Jerry) Carter               2005
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.  
18  */
19
20 #include "includes.h"
21 #include "regfio.h"
22 #include "../librpc/gen_ndr/ndr_security.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
26
27 /*******************************************************************
28  *
29  * TODO : Right now this code basically ignores classnames.
30  *
31  ******************************************************************/
32
33 #if defined(PARANOID_MALLOC_CHECKER)
34 #define PRS_ALLOC_MEM(ps, type, count) (type *)prs_alloc_mem_((ps),sizeof(type),(count))
35 #else
36 #define PRS_ALLOC_MEM(ps, type, count) (type *)prs_alloc_mem((ps),sizeof(type),(count))
37 #endif
38
39 /*******************************************************************
40  Reads or writes an NTTIME structure.
41 ********************************************************************/
42
43 static bool smb_io_time(const char *desc, NTTIME *nttime, prs_struct *ps, int depth)
44 {
45         uint32 low, high;
46         if (nttime == NULL)
47                 return False;
48
49         prs_debug(ps, depth, desc, "smb_io_time");
50         depth++;
51
52         if(!prs_align(ps))
53                 return False;
54
55         if (MARSHALLING(ps)) {
56                 low = *nttime & 0xFFFFFFFF;
57                 high = *nttime >> 32;
58         }
59
60         if(!prs_uint32("low ", ps, depth, &low)) /* low part */
61                 return False;
62         if(!prs_uint32("high", ps, depth, &high)) /* high part */
63                 return False;
64
65         if (UNMARSHALLING(ps)) {
66                 *nttime = (((uint64_t)high << 32) + low);
67         }
68
69         return True;
70 }
71
72 /*******************************************************************
73 *******************************************************************/
74
75 static int write_block( REGF_FILE *file, prs_struct *ps, uint32 offset )
76 {
77         int bytes_written, returned;
78         char *buffer = prs_data_p( ps );
79         uint32 buffer_size = prs_data_size( ps );
80         SMB_STRUCT_STAT sbuf;
81
82         if ( file->fd == -1 )
83                 return -1;
84
85         /* check for end of file */
86
87         if (sys_fstat(file->fd, &sbuf, false)) {
88                 DEBUG(0,("write_block: stat() failed! (%s)\n", strerror(errno)));
89                 return -1;
90         }
91
92         if ( lseek( file->fd, offset, SEEK_SET ) == -1 ) {
93                 DEBUG(0,("write_block: lseek() failed! (%s)\n", strerror(errno) ));
94                 return -1;
95         }
96         
97         bytes_written = returned = 0;
98         while ( bytes_written < buffer_size ) {
99                 if ( (returned = write( file->fd, buffer+bytes_written, buffer_size-bytes_written )) == -1 ) {
100                         DEBUG(0,("write_block: write() failed! (%s)\n", strerror(errno) ));
101                         return False;
102                 }
103                                 
104                 bytes_written += returned;
105         }
106         
107         return bytes_written;
108 }
109
110 /*******************************************************************
111 *******************************************************************/
112
113 static int read_block( REGF_FILE *file, prs_struct *ps, uint32 file_offset, uint32 block_size )
114 {
115         int bytes_read, returned;
116         char *buffer;
117         SMB_STRUCT_STAT sbuf;
118
119         /* check for end of file */
120
121         if (sys_fstat(file->fd, &sbuf, false)) {
122                 DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno)));
123                 return -1;
124         }
125
126         if ( (size_t)file_offset >= sbuf.st_ex_size )
127                 return -1;
128         
129         /* if block_size == 0, we are parsing HBIN records and need 
130            to read some of the header to get the block_size from there */
131            
132         if ( block_size == 0 ) {
133                 char hdr[0x20];
134
135                 if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
136                         DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
137                         return -1;
138                 }
139
140                 returned = read( file->fd, hdr, 0x20 );
141                 if ( (returned == -1) || (returned < 0x20) ) {
142                         DEBUG(0,("read_block: failed to read in HBIN header. Is the file corrupt?\n"));
143                         return -1;
144                 }
145
146                 /* make sure this is an hbin header */
147
148                 if ( strncmp( hdr, "hbin", HBIN_HDR_SIZE ) != 0 ) {
149                         DEBUG(0,("read_block: invalid block header!\n"));
150                         return -1;
151                 }
152
153                 block_size = IVAL( hdr, 0x08 );
154         }
155
156         DEBUG(10,("read_block: block_size == 0x%x\n", block_size ));
157
158         /* set the offset, initialize the buffer, and read the block from disk */
159
160         if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
161                 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
162                 return -1;
163         }
164         
165         if (!prs_init( ps, block_size, file->mem_ctx, UNMARSHALL )) {
166                 DEBUG(0,("read_block: prs_init() failed! (%s)\n", strerror(errno) ));
167                 return -1;
168         }
169         buffer = prs_data_p( ps );
170         bytes_read = returned = 0;
171
172         while ( bytes_read < block_size ) {
173                 if ( (returned = read( file->fd, buffer+bytes_read, block_size-bytes_read )) == -1 ) {
174                         DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno) ));
175                         return False;
176                 }
177                 if ( (returned == 0) && (bytes_read < block_size) ) {
178                         DEBUG(0,("read_block: not a vald registry file ?\n" ));
179                         return False;
180                 }       
181                 
182                 bytes_read += returned;
183         }
184         
185         return bytes_read;
186 }
187
188 /*******************************************************************
189 *******************************************************************/
190
191 static bool write_hbin_block( REGF_FILE *file, REGF_HBIN *hbin )
192 {
193         if ( !hbin->dirty )
194                 return True;
195
196         /* write free space record if any is available */
197
198         if ( hbin->free_off != REGF_OFFSET_NONE ) {
199                 uint32 header = 0xffffffff;
200
201                 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) )
202                         return False;
203                 if ( !prs_uint32( "free_size", &hbin->ps, 0, &hbin->free_size ) )
204                         return False;
205                 if ( !prs_uint32( "free_header", &hbin->ps, 0, &header ) )
206                         return False;
207         }
208
209         hbin->dirty = (write_block( file, &hbin->ps, hbin->file_off ) != -1);
210
211         return hbin->dirty;
212 }
213
214 /*******************************************************************
215 *******************************************************************/
216
217 static bool hbin_block_close( REGF_FILE *file, REGF_HBIN *hbin )
218 {
219         REGF_HBIN *p;
220
221         /* remove the block from the open list and flush it to disk */
222
223         for ( p=file->block_list; p && p!=hbin; p=p->next )
224                 ;;
225
226         if ( p == hbin ) {
227                 DLIST_REMOVE( file->block_list, hbin );
228         }
229         else
230                 DEBUG(0,("hbin_block_close: block not in open list!\n"));
231
232         if ( !write_hbin_block( file, hbin ) )
233                 return False;
234
235         return True;
236 }
237
238 /*******************************************************************
239 *******************************************************************/
240
241 static bool prs_regf_block( const char *desc, prs_struct *ps, int depth, REGF_FILE *file )
242 {
243         prs_debug(ps, depth, desc, "prs_regf_block");
244         depth++;
245         
246         if ( !prs_uint8s( True, "header", ps, depth, (uint8*)file->header, sizeof( file->header )) )
247                 return False;
248         
249         /* yes, these values are always identical so store them only once */
250         
251         if ( !prs_uint32( "unknown1", ps, depth, &file->unknown1 ))
252                 return False;
253         if ( !prs_uint32( "unknown1 (again)", ps, depth, &file->unknown1 ))
254                 return False;
255
256         /* get the modtime */
257         
258         if ( !prs_set_offset( ps, 0x0c ) )
259                 return False;
260         if ( !smb_io_time( "modtime", &file->mtime, ps, depth ) )
261                 return False;
262
263         /* constants */
264         
265         if ( !prs_uint32( "unknown2", ps, depth, &file->unknown2 ))
266                 return False;
267         if ( !prs_uint32( "unknown3", ps, depth, &file->unknown3 ))
268                 return False;
269         if ( !prs_uint32( "unknown4", ps, depth, &file->unknown4 ))
270                 return False;
271         if ( !prs_uint32( "unknown5", ps, depth, &file->unknown5 ))
272                 return False;
273
274         /* get file offsets */
275         
276         if ( !prs_set_offset( ps, 0x24 ) )
277                 return False;
278         if ( !prs_uint32( "data_offset", ps, depth, &file->data_offset ))
279                 return False;
280         if ( !prs_uint32( "last_block", ps, depth, &file->last_block ))
281                 return False;
282                 
283         /* one more constant */
284         
285         if ( !prs_uint32( "unknown6", ps, depth, &file->unknown6 ))
286                 return False;
287                 
288         /* get the checksum */
289         
290         if ( !prs_set_offset( ps, 0x01fc ) )
291                 return False;
292         if ( !prs_uint32( "checksum", ps, depth, &file->checksum ))
293                 return False;
294         
295         return True;
296 }
297
298 /*******************************************************************
299 *******************************************************************/
300
301 static bool prs_hbin_block( const char *desc, prs_struct *ps, int depth, REGF_HBIN *hbin )
302 {
303         uint32 block_size2;
304
305         prs_debug(ps, depth, desc, "prs_regf_block");
306         depth++;
307         
308         if ( !prs_uint8s( True, "header", ps, depth, (uint8*)hbin->header, sizeof( hbin->header )) )
309                 return False;
310
311         if ( !prs_uint32( "first_hbin_off", ps, depth, &hbin->first_hbin_off ))
312                 return False;
313
314         /* The dosreg.cpp comments say that the block size is at 0x1c.
315            According to a WINXP NTUSER.dat file, this is wrong.  The block_size
316            is at 0x08 */
317
318         if ( !prs_uint32( "block_size", ps, depth, &hbin->block_size ))
319                 return False;
320
321         block_size2 = hbin->block_size;
322         prs_set_offset( ps, 0x1c );
323         if ( !prs_uint32( "block_size2", ps, depth, &block_size2 ))
324                 return False;
325
326         if ( MARSHALLING(ps) )
327                 hbin->dirty = True;
328         
329
330         return True;
331 }
332
333 /*******************************************************************
334 *******************************************************************/
335
336 static bool prs_nk_rec( const char *desc, prs_struct *ps, int depth, REGF_NK_REC *nk )
337 {
338         uint16 class_length, name_length;
339         uint32 start;
340         uint32 data_size, start_off, end_off;
341         uint32 unknown_off = REGF_OFFSET_NONE;
342
343         nk->hbin_off = prs_offset( ps );
344         start = nk->hbin_off;
345         
346         prs_debug(ps, depth, desc, "prs_nk_rec");
347         depth++;
348         
349         /* back up and get the data_size */
350         
351         if ( !prs_set_offset( ps, prs_offset(ps)-sizeof(uint32)) )
352                 return False;
353         start_off = prs_offset( ps );
354         if ( !prs_uint32( "rec_size", ps, depth, &nk->rec_size ))
355                 return False;
356         
357         if ( !prs_uint8s( True, "header", ps, depth, (uint8*)nk->header, sizeof( nk->header )) )
358                 return False;
359                 
360         if ( !prs_uint16( "key_type", ps, depth, &nk->key_type ))
361                 return False;
362         if ( !smb_io_time( "mtime", &nk->mtime, ps, depth ))
363                 return False;
364                 
365         if ( !prs_set_offset( ps, start+0x0010 ) )
366                 return False;
367         if ( !prs_uint32( "parent_off", ps, depth, &nk->parent_off ))
368                 return False;
369         if ( !prs_uint32( "num_subkeys", ps, depth, &nk->num_subkeys ))
370                 return False;
371                 
372         if ( !prs_set_offset( ps, start+0x001c ) )
373                 return False;
374         if ( !prs_uint32( "subkeys_off", ps, depth, &nk->subkeys_off ))
375                 return False;
376         if ( !prs_uint32( "unknown_off", ps, depth, &unknown_off) )
377                 return False;
378                 
379         if ( !prs_set_offset( ps, start+0x0024 ) )
380                 return False;
381         if ( !prs_uint32( "num_values", ps, depth, &nk->num_values ))
382                 return False;
383         if ( !prs_uint32( "values_off", ps, depth, &nk->values_off ))
384                 return False;
385         if ( !prs_uint32( "sk_off", ps, depth, &nk->sk_off ))
386                 return False;
387         if ( !prs_uint32( "classname_off", ps, depth, &nk->classname_off ))
388                 return False;
389
390         if ( !prs_uint32( "max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname))
391                 return False;
392         if ( !prs_uint32( "max_bytes_subkeyclassname", ps, depth, &nk->max_bytes_subkeyclassname))
393                 return False;
394         if ( !prs_uint32( "max_bytes_valuename", ps, depth, &nk->max_bytes_valuename))
395                 return False;
396         if ( !prs_uint32( "max_bytes_value", ps, depth, &nk->max_bytes_value))
397                 return False;
398         if ( !prs_uint32( "unknown index", ps, depth, &nk->unk_index))
399                 return False;
400
401         name_length = nk->keyname ? strlen(nk->keyname) : 0 ;
402         class_length = nk->classname ? strlen(nk->classname) : 0 ;
403         if ( !prs_uint16( "name_length", ps, depth, &name_length ))
404                 return False;
405         if ( !prs_uint16( "class_length", ps, depth, &class_length ))
406                 return False;   
407                 
408         if ( class_length ) {
409                 ;;
410         }
411         
412         if ( name_length ) {
413                 if ( UNMARSHALLING(ps) ) {
414                         if ( !(nk->keyname = PRS_ALLOC_MEM( ps, char, name_length+1 )) )
415                                 return False;
416                 }
417
418                 if ( !prs_uint8s( True, "name", ps, depth, (uint8*)nk->keyname, name_length) )
419                         return False;
420
421                 if ( UNMARSHALLING(ps) ) 
422                         nk->keyname[name_length] = '\0';
423         }
424
425         end_off = prs_offset( ps );
426
427         /* data_size must be divisible by 8 and large enough to hold the original record */
428
429         data_size = ((start_off - end_off) & 0xfffffff8 );
430         if ( data_size > nk->rec_size )
431                 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size));
432
433         if ( MARSHALLING(ps) )
434                 nk->hbin->dirty = True;
435
436         return True;
437 }
438
439 /*******************************************************************
440 *******************************************************************/
441
442 static uint32 regf_block_checksum( prs_struct *ps )
443 {
444         char *buffer = prs_data_p( ps );
445         uint32 checksum, x;
446         int i;
447
448         /* XOR of all bytes 0x0000 - 0x01FB */
449                 
450         checksum = x = 0;
451         
452         for ( i=0; i<0x01FB; i+=4 ) {
453                 x = IVAL(buffer, i );
454                 checksum ^= x;
455         }
456         
457         return checksum;
458 }
459
460 /*******************************************************************
461 *******************************************************************/
462
463 static bool read_regf_block( REGF_FILE *file )
464 {
465         prs_struct ps;
466         uint32 checksum;
467         
468         /* grab the first block from the file */
469                 
470         if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) == -1 )
471                 return False;
472         
473         /* parse the block and verify the checksum */
474         
475         if ( !prs_regf_block( "regf_header", &ps, 0, file ) )
476                 return False;   
477                 
478         checksum = regf_block_checksum( &ps );
479         
480         prs_mem_free( &ps );
481         
482         if ( file->checksum !=  checksum ) {
483                 DEBUG(0,("read_regf_block: invalid checksum\n" ));
484                 return False;
485         }
486
487         return True;
488 }
489
490 /*******************************************************************
491 *******************************************************************/
492
493 static REGF_HBIN* read_hbin_block( REGF_FILE *file, off_t offset )
494 {
495         REGF_HBIN *hbin;
496         uint32 record_size, curr_off, block_size, header;
497         
498         if ( !(hbin = TALLOC_ZERO_P(file->mem_ctx, REGF_HBIN)) ) 
499                 return NULL;
500         hbin->file_off = offset;
501         hbin->free_off = -1;
502                 
503         if ( read_block( file, &hbin->ps, offset, 0 ) == -1 )
504                 return NULL;
505         
506         if ( !prs_hbin_block( "hbin", &hbin->ps, 0, hbin ) )
507                 return NULL;    
508
509         /* this should be the same thing as hbin->block_size but just in case */
510
511         block_size = prs_data_size( &hbin->ps );        
512
513         /* Find the available free space offset.  Always at the end,
514            so walk the record list and stop when you get to the end.
515            The end is defined by a record header of 0xffffffff.  The 
516            previous 4 bytes contains the amount of free space remaining 
517            in the hbin block. */
518
519         /* remember that the record_size is in the 4 bytes preceeding the record itself */
520
521         if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE-sizeof(uint32) ) )
522                 return False;
523
524         record_size = 0;
525         header = 0;
526         curr_off = prs_offset( &hbin->ps );
527         while ( header != 0xffffffff ) {
528                 /* not done yet so reset the current offset to the 
529                    next record_size field */
530
531                 curr_off = curr_off+record_size;
532
533                 /* for some reason the record_size of the last record in
534                    an hbin block can extend past the end of the block
535                    even though the record fits within the remaining 
536                    space....aaarrrgggghhhhhh */
537
538                 if ( curr_off >= block_size ) {
539                         record_size = -1;
540                         curr_off = -1;
541                         break;
542                 }
543
544                 if ( !prs_set_offset( &hbin->ps, curr_off) )
545                         return False;
546
547                 if ( !prs_uint32( "rec_size", &hbin->ps, 0, &record_size ) )
548                         return False;
549                 if ( !prs_uint32( "header", &hbin->ps, 0, &header ) )
550                         return False;
551                 
552                 SMB_ASSERT( record_size != 0 );
553
554                 if ( record_size & 0x80000000 ) {
555                         /* absolute_value(record_size) */
556                         record_size = (record_size ^ 0xffffffff) + 1;
557                 }
558         }
559
560         /* save the free space offset */
561
562         if ( header == 0xffffffff ) {
563
564                 /* account for the fact that the curr_off is 4 bytes behind the actual 
565                    record header */
566
567                 hbin->free_off = curr_off + sizeof(uint32);
568                 hbin->free_size = record_size;
569         }
570
571         DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin->free_off));
572
573         if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE )  )
574                 return False;
575         
576         return hbin;
577 }
578
579 /*******************************************************************
580  Input a random offset and receive the corresponding HBIN 
581  block for it
582 *******************************************************************/
583
584 static bool hbin_contains_offset( REGF_HBIN *hbin, uint32 offset )
585 {
586         if ( !hbin )
587                 return False;
588         
589         if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) )
590                 return True;
591                 
592         return False;
593 }
594
595 /*******************************************************************
596  Input a random offset and receive the corresponding HBIN 
597  block for it
598 *******************************************************************/
599
600 static REGF_HBIN* lookup_hbin_block( REGF_FILE *file, uint32 offset )
601 {
602         REGF_HBIN *hbin = NULL;
603         uint32 block_off;
604
605         /* start with the open list */
606
607         for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
608                 DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%lx]\n", hbin->file_off, (unsigned long)hbin ));
609                 if ( hbin_contains_offset( hbin, offset ) )
610                         return hbin;
611         }
612         
613         if ( !hbin ) {
614                 /* start at the beginning */
615
616                 block_off = REGF_BLOCKSIZE;
617                 do {
618                         /* cleanup before the next round */
619                         if ( hbin )
620                                 prs_mem_free( &hbin->ps );
621
622                         hbin = read_hbin_block( file, block_off );
623
624                         if ( hbin ) 
625                                 block_off = hbin->file_off + hbin->block_size;
626
627                 } while ( hbin && !hbin_contains_offset( hbin, offset ) );
628         }
629
630         if ( hbin )
631                 DLIST_ADD( file->block_list, hbin );
632
633         return hbin;
634 }
635
636 /*******************************************************************
637 *******************************************************************/
638
639 static bool prs_hash_rec( const char *desc, prs_struct *ps, int depth, REGF_HASH_REC *hash )
640 {
641         prs_debug(ps, depth, desc, "prs_hash_rec");
642         depth++;
643
644         if ( !prs_uint32( "nk_off", ps, depth, &hash->nk_off ))
645                 return False;
646         if ( !prs_uint8s( True, "keycheck", ps, depth, hash->keycheck, sizeof( hash->keycheck )) )
647                 return False;
648         
649         return True;
650 }
651
652 /*******************************************************************
653 *******************************************************************/
654
655 static bool hbin_prs_lf_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk )
656 {
657         int i;
658         REGF_LF_REC *lf = &nk->subkeys;
659         uint32 data_size, start_off, end_off;
660
661         prs_debug(&hbin->ps, depth, desc, "prs_lf_records");
662         depth++;
663
664         /* check if we have anything to do first */
665         
666         if ( nk->num_subkeys == 0 )
667                 return True;
668
669         /* move to the LF record */
670
671         if ( !prs_set_offset( &hbin->ps, nk->subkeys_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
672                 return False;
673
674         /* backup and get the data_size */
675         
676         if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
677                 return False;
678         start_off = prs_offset( &hbin->ps );
679         if ( !prs_uint32( "rec_size", &hbin->ps, depth, &lf->rec_size ))
680                 return False;
681
682         if ( !prs_uint8s( True, "header", &hbin->ps, depth, (uint8*)lf->header, sizeof( lf->header )) )
683                 return False;
684                 
685         if ( !prs_uint16( "num_keys", &hbin->ps, depth, &lf->num_keys))
686                 return False;
687
688         if ( UNMARSHALLING(&hbin->ps) ) {
689                 if (lf->num_keys) {
690                         if ( !(lf->hashes = PRS_ALLOC_MEM( &hbin->ps, REGF_HASH_REC, lf->num_keys )) )
691                                 return False;
692                 } else {
693                         lf->hashes = NULL;
694                 }
695         }
696
697         for ( i=0; i<lf->num_keys; i++ ) {
698                 if ( !prs_hash_rec( "hash_rec", &hbin->ps, depth, &lf->hashes[i] ) )
699                         return False;
700         }
701
702         end_off = prs_offset( &hbin->ps );
703
704         /* data_size must be divisible by 8 and large enough to hold the original record */
705
706         data_size = ((start_off - end_off) & 0xfffffff8 );
707         if ( data_size > lf->rec_size )
708                 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->rec_size));
709
710         if ( MARSHALLING(&hbin->ps) )
711                 hbin->dirty = True;
712
713         return True;
714 }
715
716 /*******************************************************************
717 *******************************************************************/
718
719 static bool hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_SK_REC *sk )
720 {
721         prs_struct *ps = &hbin->ps;
722         uint16 tag = 0xFFFF;
723         uint32 data_size, start_off, end_off;
724
725
726         prs_debug(ps, depth, desc, "hbin_prs_sk_rec");
727         depth++;
728
729         if ( !prs_set_offset( &hbin->ps, sk->sk_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
730                 return False;
731
732         /* backup and get the data_size */
733         
734         if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
735                 return False;
736         start_off = prs_offset( &hbin->ps );
737         if ( !prs_uint32( "rec_size", &hbin->ps, depth, &sk->rec_size ))
738                 return False;
739
740         if ( !prs_uint8s( True, "header", ps, depth, (uint8*)sk->header, sizeof( sk->header )) )
741                 return False;
742         if ( !prs_uint16( "tag", ps, depth, &tag))
743                 return False;
744
745         if ( !prs_uint32( "prev_sk_off", ps, depth, &sk->prev_sk_off))
746                 return False;
747         if ( !prs_uint32( "next_sk_off", ps, depth, &sk->next_sk_off))
748                 return False;
749         if ( !prs_uint32( "ref_count", ps, depth, &sk->ref_count))
750                 return False;
751         if ( !prs_uint32( "size", ps, depth, &sk->size))
752                 return False;
753
754         {
755                 NTSTATUS status;
756                 TALLOC_CTX *mem_ctx = prs_get_mem_context(&hbin->ps);
757                 DATA_BLOB blob;
758
759                 if (MARSHALLING(&hbin->ps)) {
760                         status = marshall_sec_desc(mem_ctx,
761                                                    sk->sec_desc,
762                                                    &blob.data, &blob.length);
763                         if (!NT_STATUS_IS_OK(status))
764                                 return False;
765                         if (!prs_copy_data_in(&hbin->ps, (const char *)blob.data, blob.length))
766                                 return False;
767                 } else {
768                         blob = data_blob_const(prs_data_p(&hbin->ps),
769                                                prs_data_size(&hbin->ps));
770                         status = unmarshall_sec_desc(mem_ctx,
771                                                      blob.data, blob.length,
772                                                      &sk->sec_desc);
773                         if (!NT_STATUS_IS_OK(status))
774                                 return False;
775                         prs_set_offset(&hbin->ps, blob.length);
776                 }
777         }
778
779         end_off = prs_offset( &hbin->ps );
780
781         /* data_size must be divisible by 8 and large enough to hold the original record */
782
783         data_size = ((start_off - end_off) & 0xfffffff8 );
784         if ( data_size > sk->rec_size )
785                 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->rec_size));
786
787         if ( MARSHALLING(&hbin->ps) )
788                 hbin->dirty = True;
789
790         return True;
791 }
792
793 /*******************************************************************
794 *******************************************************************/
795
796 static bool hbin_prs_vk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_VK_REC *vk, REGF_FILE *file )
797 {
798         uint32 offset;
799         uint16 name_length;
800         prs_struct *ps = &hbin->ps;
801         uint32 data_size, start_off, end_off;
802
803         prs_debug(ps, depth, desc, "prs_vk_rec");
804         depth++;
805
806         /* backup and get the data_size */
807         
808         if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
809                 return False;
810         start_off = prs_offset( &hbin->ps );
811         if ( !prs_uint32( "rec_size", &hbin->ps, depth, &vk->rec_size ))
812                 return False;
813
814         if ( !prs_uint8s( True, "header", ps, depth, (uint8*)vk->header, sizeof( vk->header )) )
815                 return False;
816
817         if ( MARSHALLING(&hbin->ps) )
818                 name_length = strlen(vk->valuename);
819
820         if ( !prs_uint16( "name_length", ps, depth, &name_length ))
821                 return False;
822         if ( !prs_uint32( "data_size", ps, depth, &vk->data_size ))
823                 return False;
824         if ( !prs_uint32( "data_off", ps, depth, &vk->data_off ))
825                 return False;
826         if ( !prs_uint32( "type", ps, depth, &vk->type))
827                 return False;
828         if ( !prs_uint16( "flag", ps, depth, &vk->flag))
829                 return False;
830
831         offset = prs_offset( ps );
832         offset += 2;    /* skip 2 bytes */
833         prs_set_offset( ps, offset );
834
835         /* get the name */
836
837         if ( vk->flag&VK_FLAG_NAME_PRESENT ) {
838
839                 if ( UNMARSHALLING(&hbin->ps) ) {
840                         if ( !(vk->valuename = PRS_ALLOC_MEM( ps, char, name_length+1 )))
841                                 return False;
842                 }
843                 if ( !prs_uint8s( True, "name", ps, depth, (uint8*)vk->valuename, name_length ) )
844                         return False;
845         }
846
847         end_off = prs_offset( &hbin->ps );
848
849         /* get the data if necessary */
850
851         if ( vk->data_size != 0 ) {
852                 bool charmode = False;
853
854                 if ( (vk->type == REG_SZ) || (vk->type == REG_MULTI_SZ) )
855                         charmode = True;
856
857                 /* the data is stored in the offset if the size <= 4 */
858
859                 if ( !(vk->data_size & VK_DATA_IN_OFFSET) ) {
860                         REGF_HBIN *hblock = hbin;
861                         uint32 data_rec_size;
862
863                         if ( UNMARSHALLING(&hbin->ps) ) {
864                                 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, vk->data_size) ) )
865                                         return False;
866                         }
867
868                         /* this data can be in another hbin */
869                         if ( !hbin_contains_offset( hbin, vk->data_off ) ) {
870                                 if ( !(hblock = lookup_hbin_block( file, vk->data_off )) )
871                                         return False;
872                         }
873                         if ( !(prs_set_offset( &hblock->ps, (vk->data_off+HBIN_HDR_SIZE-hblock->first_hbin_off)-sizeof(uint32) )) )
874                                 return False;
875
876                         if ( MARSHALLING(&hblock->ps) ) {
877                                 data_rec_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
878                                 data_rec_size = ( data_rec_size - 1 ) ^ 0xFFFFFFFF;
879                         }
880                         if ( !prs_uint32( "data_rec_size", &hblock->ps, depth, &data_rec_size ))
881                                 return False;
882                         if ( !prs_uint8s( charmode, "data", &hblock->ps, depth, vk->data, vk->data_size) )
883                                 return False;
884
885                         if ( MARSHALLING(&hblock->ps) )
886                                 hblock->dirty = True;
887                 }
888                 else {
889                         if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, 4 ) ) )
890                                 return False;
891                         SIVAL( vk->data, 0, vk->data_off );
892                 }
893                 
894         }
895
896         /* data_size must be divisible by 8 and large enough to hold the original record */
897
898         data_size = ((start_off - end_off ) & 0xfffffff8 );
899         if ( data_size !=  vk->rec_size )
900                 DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size, vk->rec_size));
901
902         if ( MARSHALLING(&hbin->ps) )
903                 hbin->dirty = True;
904
905         return True;
906 }
907
908 /*******************************************************************
909  read a VK record which is contained in the HBIN block stored 
910  in the prs_struct *ps.
911 *******************************************************************/
912
913 static bool hbin_prs_vk_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk, REGF_FILE *file )
914 {
915         int i;
916         uint32 record_size;
917
918         prs_debug(&hbin->ps, depth, desc, "prs_vk_records");
919         depth++;
920         
921         /* check if we have anything to do first */
922         
923         if ( nk->num_values == 0 )
924                 return True;
925                 
926         if ( UNMARSHALLING(&hbin->ps) ) {
927                 if ( !(nk->values = PRS_ALLOC_MEM( &hbin->ps, REGF_VK_REC, nk->num_values ) ) )
928                         return False;
929         }
930         
931         /* convert the offset to something relative to this HBIN block */
932         
933         if ( !prs_set_offset( &hbin->ps, nk->values_off+HBIN_HDR_SIZE-hbin->first_hbin_off-sizeof(uint32)) )
934                 return False;
935
936         if ( MARSHALLING( &hbin->ps) ) { 
937                 record_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
938                 record_size = (record_size - 1) ^ 0xFFFFFFFF;
939         }
940
941         if ( !prs_uint32( "record_size", &hbin->ps, depth, &record_size ) )
942                 return False;
943                 
944         for ( i=0; i<nk->num_values; i++ ) {
945                 if ( !prs_uint32( "vk_off", &hbin->ps, depth, &nk->values[i].rec_off ) )
946                         return False;
947         }
948
949         for ( i=0; i<nk->num_values; i++ ) {
950                 REGF_HBIN *sub_hbin = hbin;
951                 uint32 new_offset;
952         
953                 if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) ) {
954                         sub_hbin = lookup_hbin_block( file, nk->values[i].rec_off );
955                         if ( !sub_hbin ) {
956                                 DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n", 
957                                         nk->values[i].hbin_off));
958                                 return False;
959                         }
960                 }
961                 
962                 new_offset = nk->values[i].rec_off + HBIN_HDR_SIZE - sub_hbin->first_hbin_off;
963                 if ( !prs_set_offset( &sub_hbin->ps, new_offset ) )
964                         return False;
965                 if ( !hbin_prs_vk_rec( "vk_rec", sub_hbin, depth, &nk->values[i], file ) )
966                         return False;
967         }
968
969         if ( MARSHALLING(&hbin->ps) )
970                 hbin->dirty = True;
971
972
973         return True;
974 }
975
976
977 /*******************************************************************
978 *******************************************************************/
979
980 static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32 offset )
981 {
982         REGF_SK_REC *p_sk;
983         
984         for ( p_sk=file->sec_desc_list; p_sk; p_sk=p_sk->next ) {
985                 if ( p_sk->sk_off == offset ) 
986                         return p_sk;
987         }
988         
989         return NULL;
990 }
991
992 /*******************************************************************
993 *******************************************************************/
994
995 static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, struct security_descriptor *sd )
996 {
997         REGF_SK_REC *p;
998
999         for ( p=file->sec_desc_list; p; p=p->next ) {
1000                 if ( security_descriptor_equal( p->sec_desc, sd ) )
1001                         return p;
1002         }
1003
1004         /* failure */
1005
1006         return NULL;
1007 }
1008
1009 /*******************************************************************
1010 *******************************************************************/
1011
1012 static bool hbin_prs_key( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk )
1013 {
1014         int depth = 0;
1015         REGF_HBIN *sub_hbin;
1016         
1017         prs_debug(&hbin->ps, depth, "", "fetch_key");
1018         depth++;
1019
1020         /* get the initial nk record */
1021         
1022         if ( !prs_nk_rec( "nk_rec", &hbin->ps, depth, nk ))
1023                 return False;
1024
1025         /* fill in values */
1026         
1027         if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) {
1028                 sub_hbin = hbin;
1029                 if ( !hbin_contains_offset( hbin, nk->values_off ) ) {
1030                         sub_hbin = lookup_hbin_block( file, nk->values_off );
1031                         if ( !sub_hbin ) {
1032                                 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n", 
1033                                         nk->values_off));
1034                                 return False;
1035                         }
1036                 }
1037                 
1038                 if ( !hbin_prs_vk_records( "vk_rec", sub_hbin, depth, nk, file ))
1039                         return False;
1040         }
1041                 
1042         /* now get subkeys */
1043         
1044         if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) {
1045                 sub_hbin = hbin;
1046                 if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) {
1047                         sub_hbin = lookup_hbin_block( file, nk->subkeys_off );
1048                         if ( !sub_hbin ) {
1049                                 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n", 
1050                                         nk->subkeys_off));
1051                                 return False;
1052                         }
1053                 }
1054                 
1055                 if ( !hbin_prs_lf_records( "lf_rec", sub_hbin, depth, nk ))
1056                         return False;
1057         }
1058
1059         /* get the to the security descriptor.  First look if we have already parsed it */
1060         
1061         if ( (nk->sk_off!=REGF_OFFSET_NONE) && !( nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )) ) {
1062
1063                 sub_hbin = hbin;
1064                 if ( !hbin_contains_offset( hbin, nk->sk_off ) ) {
1065                         sub_hbin = lookup_hbin_block( file, nk->sk_off );
1066                         if ( !sub_hbin ) {
1067                                 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_offset [0x%x]\n", 
1068                                         nk->subkeys_off));
1069                                 return False;
1070                         }
1071                 }
1072                 
1073                 if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) )
1074                         return False;
1075                 nk->sec_desc->sk_off = nk->sk_off;
1076                 if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin, depth, nk->sec_desc ))
1077                         return False;
1078                         
1079                 /* add to the list of security descriptors (ref_count has been read from the files) */
1080
1081                 nk->sec_desc->sk_off = nk->sk_off;
1082                 DLIST_ADD( file->sec_desc_list, nk->sec_desc );
1083         }
1084                 
1085         return True;
1086 }
1087
1088 /*******************************************************************
1089 *******************************************************************/
1090
1091 static bool next_record( REGF_HBIN *hbin, const char *hdr, bool *eob )
1092 {
1093         uint8 header[REC_HDR_SIZE];
1094         uint32 record_size;
1095         uint32 curr_off, block_size;
1096         bool found = False;
1097         prs_struct *ps = &hbin->ps;
1098         
1099         curr_off = prs_offset( ps );
1100         if ( curr_off == 0 )
1101                 prs_set_offset( ps, HBIN_HEADER_REC_SIZE );
1102
1103         /* assume that the current offset is at the record header 
1104            and we need to backup to read the record size */
1105
1106         curr_off -= sizeof(uint32);
1107
1108         block_size = prs_data_size( ps );
1109         record_size = 0;
1110         memset( header, 0x0, sizeof(uint8)*REC_HDR_SIZE );
1111         while ( !found ) {
1112
1113                 curr_off = curr_off+record_size;
1114                 if ( curr_off >= block_size ) 
1115                         break;
1116
1117                 if ( !prs_set_offset( &hbin->ps, curr_off) )
1118                         return False;
1119
1120                 if ( !prs_uint32( "record_size", ps, 0, &record_size ) )
1121                         return False;
1122                 if ( !prs_uint8s( True, "header", ps, 0, header, REC_HDR_SIZE ) )
1123                         return False;
1124
1125                 if ( record_size & 0x80000000 ) {
1126                         /* absolute_value(record_size) */
1127                         record_size = (record_size ^ 0xffffffff) + 1;
1128                 }
1129
1130                 if ( memcmp( header, hdr, REC_HDR_SIZE ) == 0 ) {
1131                         found = True;
1132                         curr_off += sizeof(uint32);
1133                 }
1134         } 
1135
1136         /* mark prs_struct as done ( at end ) if no more SK records */
1137         /* mark end-of-block as True */
1138         
1139         if ( !found ) {
1140                 prs_set_offset( &hbin->ps, prs_data_size(&hbin->ps) );
1141                 *eob = True;
1142                 return False;
1143         }
1144                 
1145         if ( !prs_set_offset( ps, curr_off ) )
1146                 return False;
1147
1148         return True;
1149 }
1150
1151 /*******************************************************************
1152 *******************************************************************/
1153
1154 static bool next_nk_record( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk, bool *eob )
1155 {
1156         if ( next_record( hbin, "nk", eob ) && hbin_prs_key( file, hbin, nk ) )
1157                 return True;
1158         
1159         return False;
1160 }
1161
1162 /*******************************************************************
1163  Intialize the newly created REGF_BLOCK in *file and write the 
1164  block header to disk 
1165 *******************************************************************/
1166
1167 static bool init_regf_block( REGF_FILE *file )
1168 {       
1169         prs_struct ps;
1170         bool result = True;
1171         
1172         if ( !prs_init( &ps, REGF_BLOCKSIZE, file->mem_ctx, MARSHALL ) )
1173                 return False;
1174                 
1175         memcpy( file->header, "regf", REGF_HDR_SIZE );
1176         file->data_offset = 0x20;
1177         file->last_block  = 0x1000;
1178         
1179         /* set mod time */
1180         
1181         unix_to_nt_time( &file->mtime, time(NULL) );
1182         
1183         /* hard coded values...no diea what these are ... maybe in time */
1184         
1185         file->unknown1 = 0x2;
1186         file->unknown2 = 0x1;
1187         file->unknown3 = 0x3;
1188         file->unknown4 = 0x0;
1189         file->unknown5 = 0x1;
1190         file->unknown6 = 0x1;
1191         
1192         /* write header to the buffer */
1193         
1194         if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1195                 result = False;
1196                 goto out;
1197         }
1198         
1199         /* calculate the checksum, re-marshall data (to include the checksum) 
1200            and write to disk */
1201         
1202         file->checksum = regf_block_checksum( &ps );
1203         prs_set_offset( &ps, 0 );
1204         if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1205                 result = False;
1206                 goto out;
1207         }
1208                 
1209         if ( write_block( file, &ps, 0 ) == -1 ) {
1210                 DEBUG(0,("init_regf_block: Failed to initialize registry header block!\n"));
1211                 result = False;
1212                 goto out;
1213         }
1214         
1215 out:
1216         prs_mem_free( &ps );
1217
1218         return result;
1219 }
1220 /*******************************************************************
1221  Open the registry file and then read in the REGF block to get the 
1222  first hbin offset.
1223 *******************************************************************/
1224
1225  REGF_FILE* regfio_open( const char *filename, int flags, int mode )
1226 {
1227         REGF_FILE *rb;
1228         
1229         if ( !(rb = SMB_MALLOC_P(REGF_FILE)) ) {
1230                 DEBUG(0,("ERROR allocating memory\n"));
1231                 return NULL;
1232         }
1233         ZERO_STRUCTP( rb );
1234         rb->fd = -1;
1235         
1236         if ( !(rb->mem_ctx = talloc_init( "read_regf_block" )) ) {
1237                 regfio_close( rb );
1238                 return NULL;
1239         }
1240
1241         rb->open_flags = flags;
1242         
1243         /* open and existing file */
1244
1245         if ( (rb->fd = open(filename, flags, mode)) == -1 ) {
1246                 DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename, strerror(errno)));
1247                 regfio_close( rb );
1248                 return NULL;
1249         }
1250         
1251         /* check if we are creating a new file or overwriting an existing one */
1252                 
1253         if ( flags & (O_CREAT|O_TRUNC) ) {
1254                 if ( !init_regf_block( rb ) ) {
1255                         DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1256                         regfio_close( rb );
1257                         return NULL;
1258                 }
1259                 
1260                 /* success */
1261                 return rb;
1262         }
1263         
1264         /* read in an existing file */
1265         
1266         if ( !read_regf_block( rb ) ) {
1267                 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1268                 regfio_close( rb );
1269                 return NULL;
1270         }
1271         
1272         /* success */
1273         
1274         return rb;
1275 }
1276
1277 /*******************************************************************
1278 *******************************************************************/
1279
1280 static void regfio_mem_free( REGF_FILE *file )
1281 {
1282         /* free any talloc()'d memory */
1283         
1284         if ( file && file->mem_ctx )
1285                 talloc_destroy( file->mem_ctx );        
1286 }
1287
1288 /*******************************************************************
1289 *******************************************************************/
1290
1291  int regfio_close( REGF_FILE *file )
1292 {
1293         int fd;
1294
1295         /* cleanup for a file opened for write */
1296
1297         if ((file->fd != -1) && (file->open_flags & (O_WRONLY|O_RDWR))) {
1298                 prs_struct ps;
1299                 REGF_SK_REC *sk;
1300
1301                 /* write of sd list */
1302
1303                 for ( sk=file->sec_desc_list; sk; sk=sk->next ) {
1304                         hbin_prs_sk_rec( "sk_rec", sk->hbin, 0, sk );
1305                 }
1306
1307                 /* flush any dirty blocks */
1308
1309                 while ( file->block_list ) {
1310                         hbin_block_close( file, file->block_list );
1311                 } 
1312
1313                 ZERO_STRUCT( ps );
1314
1315                 unix_to_nt_time( &file->mtime, time(NULL) );
1316
1317                 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) != -1 ) {
1318                         /* now use for writing */
1319                         prs_switch_type( &ps, MARSHALL );
1320
1321                         /* stream the block once, generate the checksum, 
1322                            and stream it again */
1323                         prs_set_offset( &ps, 0 );
1324                         prs_regf_block( "regf_blocK", &ps, 0, file );
1325                         file->checksum = regf_block_checksum( &ps );
1326                         prs_set_offset( &ps, 0 );
1327                         prs_regf_block( "regf_blocK", &ps, 0, file );
1328
1329                         /* now we are ready to write it to disk */
1330                         if ( write_block( file, &ps, 0 ) == -1 )
1331                                 DEBUG(0,("regfio_close: failed to update the regf header block!\n"));
1332                 }
1333
1334                 prs_mem_free( &ps );
1335         }
1336         
1337         regfio_mem_free( file );
1338
1339         /* nothing tdo do if there is no open file */
1340
1341         if (file->fd == -1)
1342                 return 0;
1343                 
1344         fd = file->fd;
1345         file->fd = -1;
1346         SAFE_FREE( file );
1347
1348         return close( fd );
1349 }
1350
1351 /*******************************************************************
1352 *******************************************************************/
1353
1354 static void regfio_flush( REGF_FILE *file )
1355 {
1356         REGF_HBIN *hbin;
1357
1358         for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
1359                 write_hbin_block( file, hbin );
1360         }
1361 }
1362
1363 /*******************************************************************
1364  There should be only *one* root key in the registry file based 
1365  on my experience.  --jerry
1366 *******************************************************************/
1367
1368 REGF_NK_REC* regfio_rootkey( REGF_FILE *file )
1369 {
1370         REGF_NK_REC *nk;
1371         REGF_HBIN   *hbin;
1372         uint32      offset = REGF_BLOCKSIZE;
1373         bool        found = False;
1374         bool        eob;
1375         
1376         if ( !file )
1377                 return NULL;
1378                 
1379         if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) ) {
1380                 DEBUG(0,("regfio_rootkey: talloc() failed!\n"));
1381                 return NULL;
1382         }
1383         
1384         /* scan through the file on HBIN block at a time looking 
1385            for an NK record with a type == 0x002c.
1386            Normally this is the first nk record in the first hbin 
1387            block (but I'm not assuming that for now) */
1388         
1389         while ( (hbin = read_hbin_block( file, offset )) ) {
1390                 eob = False;
1391
1392                 while ( !eob) {
1393                         if ( next_nk_record( file, hbin, nk, &eob ) ) {
1394                                 if ( nk->key_type == NK_TYPE_ROOTKEY ) {
1395                                         found = True;
1396                                         break;
1397                                 }
1398                         }
1399                         prs_mem_free( &hbin->ps );
1400                 }
1401                 
1402                 if ( found ) 
1403                         break;
1404
1405                 offset += hbin->block_size;
1406         }
1407         
1408         if ( !found ) {
1409                 DEBUG(0,("regfio_rootkey: corrupt registry file ?  No root key record located\n"));
1410                 return NULL;
1411         }
1412
1413         DLIST_ADD( file->block_list, hbin );
1414
1415         return nk;              
1416 }
1417
1418 /*******************************************************************
1419  This acts as an interator over the subkeys defined for a given 
1420  NK record.  Remember that offsets are from the *first* HBIN block.
1421 *******************************************************************/
1422
1423  REGF_NK_REC* regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk )
1424 {
1425         REGF_NK_REC *subkey;
1426         REGF_HBIN   *hbin;
1427         uint32      nk_offset;
1428
1429         /* see if there is anything left to report */
1430         
1431         if ( !nk || (nk->subkeys_off==REGF_OFFSET_NONE) || (nk->subkey_index >= nk->num_subkeys) )
1432                 return NULL;
1433
1434         /* find the HBIN block which should contain the nk record */
1435         
1436         if ( !(hbin = lookup_hbin_block( file, nk->subkeys.hashes[nk->subkey_index].nk_off )) ) {
1437                 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n", 
1438                         nk->subkeys.hashes[nk->subkey_index].nk_off));
1439                 return NULL;
1440         }
1441         
1442         nk_offset = nk->subkeys.hashes[nk->subkey_index].nk_off;
1443         if ( !prs_set_offset( &hbin->ps, (HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off) ) )
1444                 return NULL;
1445                 
1446         nk->subkey_index++;
1447         if ( !(subkey = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) )
1448                 return NULL;
1449                 
1450         if ( !hbin_prs_key( file, hbin, subkey ) )
1451                 return NULL;
1452         
1453         return subkey;
1454 }
1455
1456
1457 /*******************************************************************
1458 *******************************************************************/
1459
1460 static REGF_HBIN* regf_hbin_allocate( REGF_FILE *file, uint32 block_size )
1461 {
1462         REGF_HBIN *hbin;
1463         SMB_STRUCT_STAT sbuf;
1464
1465         if ( !(hbin = TALLOC_ZERO_P( file->mem_ctx, REGF_HBIN )) )
1466                 return NULL;
1467
1468         memcpy( hbin->header, "hbin", sizeof(HBIN_HDR_SIZE) );
1469
1470
1471         if (sys_fstat(file->fd, &sbuf, false)) {
1472                 DEBUG(0,("regf_hbin_allocate: stat() failed! (%s)\n", strerror(errno)));
1473                 return NULL;
1474         }
1475
1476         hbin->file_off       = sbuf.st_ex_size;
1477
1478         hbin->free_off       = HBIN_HEADER_REC_SIZE;
1479         hbin->free_size      = block_size - hbin->free_off + sizeof(uint32);
1480
1481         hbin->block_size     = block_size;
1482         hbin->first_hbin_off = hbin->file_off - REGF_BLOCKSIZE;
1483
1484         if ( !prs_init( &hbin->ps, block_size, file->mem_ctx, MARSHALL ) )
1485                 return NULL;
1486
1487         if ( !prs_hbin_block( "new_hbin", &hbin->ps, 0, hbin ) )
1488                 return NULL;
1489
1490         if ( !write_hbin_block( file, hbin ) )
1491                 return NULL;
1492
1493         file->last_block = hbin->file_off;
1494
1495         return hbin;
1496 }
1497
1498 /*******************************************************************
1499 *******************************************************************/
1500
1501 static void update_free_space( REGF_HBIN *hbin, uint32 size_used )
1502 {
1503         hbin->free_off  += size_used;
1504         hbin->free_size -= size_used;
1505
1506         if ( hbin->free_off >= hbin->block_size ) {
1507                 hbin->free_off = REGF_OFFSET_NONE;
1508         }
1509
1510         return;
1511 }
1512
1513 /*******************************************************************
1514 *******************************************************************/
1515
1516 static REGF_HBIN* find_free_space( REGF_FILE *file, uint32 size )
1517 {
1518         REGF_HBIN *hbin, *p_hbin;
1519         uint32 block_off;
1520         bool cached;
1521
1522         /* check open block list */
1523
1524         for ( hbin=file->block_list; hbin!=NULL; hbin=hbin->next ) {
1525                 /* only check blocks that actually have available space */
1526
1527                 if ( hbin->free_off == REGF_OFFSET_NONE )
1528                         continue;
1529
1530                 /* check for a large enough available chunk */
1531
1532                 if ( (hbin->block_size - hbin->free_off) >= size ) {
1533                         DLIST_PROMOTE( file->block_list, hbin );
1534                         goto done;                      
1535                 }
1536         }
1537
1538         /* parse the file until we find a block with 
1539            enough free space; save the last non-filled hbin */
1540
1541         block_off = REGF_BLOCKSIZE;
1542         do {
1543                 /* cleanup before the next round */
1544                 cached = False;
1545                 if ( hbin )
1546                         prs_mem_free( &hbin->ps );
1547
1548                 hbin = read_hbin_block( file, block_off );
1549
1550                 if ( hbin ) {
1551
1552                         /* make sure that we don't already have this block in memory */
1553
1554                         for ( p_hbin=file->block_list; p_hbin!=NULL; p_hbin=p_hbin->next ) {
1555                                 if ( p_hbin->file_off == hbin->file_off ) {
1556                                         cached = True;  
1557                                         break;
1558                                 }
1559                         }
1560
1561                         block_off = hbin->file_off + hbin->block_size;
1562
1563                         if ( cached ) {
1564                                 prs_mem_free( &hbin->ps );
1565                                 hbin = NULL;
1566                                 continue;
1567                         }
1568                 }
1569         /* if (cached block or (new block and not enough free space)) then continue looping */
1570         } while ( cached || (hbin && (hbin->free_size < size)) );
1571         
1572         /* no free space; allocate a new one */
1573
1574         if ( !hbin ) {
1575                 uint32 alloc_size;
1576
1577                 /* allocate in multiples of REGF_ALLOC_BLOCK; make sure (size + hbin_header) fits */
1578
1579                 alloc_size = (((size+HBIN_HEADER_REC_SIZE) / REGF_ALLOC_BLOCK ) + 1 ) * REGF_ALLOC_BLOCK;
1580
1581                 if ( !(hbin = regf_hbin_allocate( file, alloc_size )) ) {
1582                         DEBUG(0,("find_free_space: regf_hbin_allocate() failed!\n"));
1583                         return NULL;
1584                 }
1585                 DLIST_ADD( file->block_list, hbin );
1586         }
1587
1588 done:
1589         /* set the offset to be ready to write */
1590
1591         if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) )
1592                 return NULL;
1593
1594         /* write the record size as a placeholder for now, it should be
1595            probably updated by the caller once it all of the data necessary 
1596            for the record */
1597
1598         if ( !prs_uint32("allocated_size", &hbin->ps, 0, &size) )
1599                 return False;
1600
1601         update_free_space( hbin, size );
1602         
1603         return hbin;
1604 }
1605
1606 /*******************************************************************
1607 *******************************************************************/
1608
1609 static uint32 sk_record_data_size( struct security_descriptor * sd )
1610 {
1611         uint32 size, size_mod8;
1612
1613         size_mod8 = 0;
1614
1615         /* the record size is sizeof(hdr) + name + static members + data_size_field */
1616
1617         size = sizeof(uint32)*5 + ndr_size_security_descriptor(sd, 0) + sizeof(uint32);
1618
1619         /* multiple of 8 */
1620         size_mod8 = size & 0xfffffff8;
1621         if ( size_mod8 < size )
1622                 size_mod8 += 8;
1623
1624         return size_mod8;
1625 }
1626
1627 /*******************************************************************
1628 *******************************************************************/
1629
1630 static uint32 vk_record_data_size( REGF_VK_REC *vk )
1631 {
1632         uint32 size, size_mod8;
1633
1634         size_mod8 = 0;
1635
1636         /* the record size is sizeof(hdr) + name + static members + data_size_field */
1637
1638         size = REC_HDR_SIZE + (sizeof(uint16)*3) + (sizeof(uint32)*3) + sizeof(uint32);
1639
1640         if ( vk->valuename )
1641                 size += strlen(vk->valuename);
1642
1643         /* multiple of 8 */
1644         size_mod8 = size & 0xfffffff8;
1645         if ( size_mod8 < size )
1646                 size_mod8 += 8;
1647
1648         return size_mod8;
1649 }
1650
1651 /*******************************************************************
1652 *******************************************************************/
1653
1654 static uint32 lf_record_data_size( uint32 num_keys )
1655 {
1656         uint32 size, size_mod8;
1657
1658         size_mod8 = 0;
1659
1660         /* the record size is sizeof(hdr) + num_keys + sizeof of hash_array + data_size_uint32 */
1661
1662         size = REC_HDR_SIZE + sizeof(uint16) + (sizeof(REGF_HASH_REC) * num_keys) + sizeof(uint32);
1663
1664         /* multiple of 8 */
1665         size_mod8 = size & 0xfffffff8;
1666         if ( size_mod8 < size )
1667                 size_mod8 += 8;
1668
1669         return size_mod8;
1670 }
1671
1672 /*******************************************************************
1673 *******************************************************************/
1674
1675 static uint32 nk_record_data_size( REGF_NK_REC *nk )
1676 {
1677         uint32 size, size_mod8;
1678
1679         size_mod8 = 0;
1680
1681         /* the record size is static + length_of_keyname + length_of_classname + data_size_uint32 */
1682
1683         size = 0x4c + strlen(nk->keyname) + sizeof(uint32);
1684
1685         if ( nk->classname )
1686                 size += strlen( nk->classname );
1687
1688         /* multiple of 8 */
1689         size_mod8 = size & 0xfffffff8;
1690         if ( size_mod8 < size )
1691                 size_mod8 += 8;
1692
1693         return size_mod8;
1694 }
1695
1696 /*******************************************************************
1697 *******************************************************************/
1698
1699 static bool create_vk_record(REGF_FILE *file, REGF_VK_REC *vk,
1700                              struct regval_blob *value)
1701 {
1702         char *name = regval_name(value);
1703         REGF_HBIN *data_hbin;
1704
1705         ZERO_STRUCTP( vk );
1706
1707         memcpy( vk->header, "vk", REC_HDR_SIZE );
1708
1709         if ( name ) {
1710                 vk->valuename = talloc_strdup( file->mem_ctx, regval_name(value) );
1711                 vk->flag = VK_FLAG_NAME_PRESENT;
1712         }
1713
1714         vk->data_size = regval_size( value );
1715         vk->type      = regval_type( value );
1716
1717         if ( vk->data_size > sizeof(uint32) ) {
1718                 uint32 data_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
1719
1720                 vk->data = (uint8 *)TALLOC_MEMDUP( file->mem_ctx,
1721                                                    regval_data_p(value),
1722                                                    vk->data_size );
1723                 if (vk->data == NULL) {
1724                         return False;
1725                 }
1726
1727                 /* go ahead and store the offset....we'll pick this hbin block back up when 
1728                    we stream the data */
1729
1730                 if ((data_hbin = find_free_space(file, data_size )) == NULL) {
1731                         return False;
1732                 }
1733                 vk->data_off = prs_offset( &data_hbin->ps ) + data_hbin->first_hbin_off - HBIN_HDR_SIZE;
1734         }
1735         else {
1736                 /* make sure we don't try to copy from a NULL value pointer */
1737
1738                 if ( vk->data_size != 0 ) 
1739                         memcpy( &vk->data_off, regval_data_p(value), sizeof(uint32) );
1740                 vk->data_size |= VK_DATA_IN_OFFSET;             
1741         }
1742
1743         return True;
1744 }
1745
1746 /*******************************************************************
1747 *******************************************************************/
1748
1749 static int hashrec_cmp( REGF_HASH_REC *h1, REGF_HASH_REC *h2 )
1750 {
1751         return StrCaseCmp( h1->fullname, h2->fullname );
1752 }
1753
1754 /*******************************************************************
1755 *******************************************************************/
1756
1757  REGF_NK_REC* regfio_write_key( REGF_FILE *file, const char *name,
1758                                struct regval_ctr *values, struct regsubkey_ctr *subkeys,
1759                                struct security_descriptor *sec_desc, REGF_NK_REC *parent )
1760 {
1761         REGF_NK_REC *nk;
1762         REGF_HBIN *vlist_hbin = NULL;
1763         uint32 size;
1764
1765         if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) )
1766                 return NULL;
1767
1768         memcpy( nk->header, "nk", REC_HDR_SIZE );
1769
1770         if ( !parent )
1771                 nk->key_type = NK_TYPE_ROOTKEY;
1772         else
1773                 nk->key_type = NK_TYPE_NORMALKEY;
1774
1775         /* store the parent offset (or -1 if a the root key */
1776
1777         nk->parent_off = parent ? (parent->hbin_off + parent->hbin->file_off - REGF_BLOCKSIZE - HBIN_HDR_SIZE ) : REGF_OFFSET_NONE;
1778
1779         /* no classname currently */
1780
1781         nk->classname_off = REGF_OFFSET_NONE;
1782         nk->classname = NULL;
1783         nk->keyname = talloc_strdup( file->mem_ctx, name );
1784
1785         /* current modification time */
1786
1787         unix_to_nt_time( &nk->mtime, time(NULL) );
1788
1789         /* allocate the record on disk */
1790
1791         size = nk_record_data_size( nk );
1792         nk->rec_size = ( size - 1 ) ^ 0XFFFFFFFF;
1793         if ((nk->hbin = find_free_space( file, size )) == NULL) {
1794                 return NULL;
1795         }
1796         nk->hbin_off = prs_offset( &nk->hbin->ps );
1797
1798         /* Update the hash record in the parent */
1799         
1800         if ( parent ) {
1801                 REGF_HASH_REC *hash = &parent->subkeys.hashes[parent->subkey_index];
1802
1803                 hash->nk_off = prs_offset( &nk->hbin->ps ) + nk->hbin->first_hbin_off - HBIN_HDR_SIZE;
1804                 memcpy( hash->keycheck, name, sizeof(uint32) );
1805                 hash->fullname = talloc_strdup( file->mem_ctx, name );
1806                 parent->subkey_index++;
1807
1808                 /* sort the list by keyname */
1809                 TYPESAFE_QSORT(parent->subkeys.hashes, parent->subkey_index, hashrec_cmp);
1810
1811                 if ( !hbin_prs_lf_records( "lf_rec", parent->subkeys.hbin, 0, parent ) )
1812                         return False;
1813         }
1814
1815         /* write the security descriptor */
1816
1817         nk->sk_off = REGF_OFFSET_NONE;
1818         if ( sec_desc ) {
1819                 uint32 sk_size = sk_record_data_size( sec_desc );
1820                 REGF_HBIN *sk_hbin;
1821
1822                 /* search for it in the existing list of sd's */
1823
1824                 if ( (nk->sec_desc = find_sk_record_by_sec_desc( file, sec_desc )) == NULL ) {
1825                         /* not found so add it to the list */
1826
1827                         if (!(sk_hbin = find_free_space( file, sk_size ))) {
1828                                 return NULL;
1829                         }
1830
1831                         if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) )
1832                                 return NULL;
1833         
1834                         /* now we have to store the security descriptor in the list and 
1835                            update the offsets */
1836
1837                         memcpy( nk->sec_desc->header, "sk", REC_HDR_SIZE );
1838                         nk->sec_desc->hbin      = sk_hbin;
1839                         nk->sec_desc->hbin_off  = prs_offset( &sk_hbin->ps );
1840                         nk->sec_desc->sk_off    = prs_offset( &sk_hbin->ps ) + sk_hbin->first_hbin_off - HBIN_HDR_SIZE;
1841                         nk->sec_desc->rec_size  = (sk_size-1)  ^ 0xFFFFFFFF;
1842
1843                         nk->sec_desc->sec_desc  = sec_desc;
1844                         nk->sec_desc->ref_count = 0;
1845                         
1846                         /* size value must be self-inclusive */
1847                         nk->sec_desc->size      = ndr_size_security_descriptor(sec_desc, 0)
1848                                 + sizeof(uint32);
1849
1850                         DLIST_ADD_END( file->sec_desc_list, nk->sec_desc, REGF_SK_REC *);
1851
1852                         /* update the offsets for us and the previous sd in the list.
1853                            if this is the first record, then just set the next and prev
1854                            offsets to ourself. */
1855
1856                         if ( DLIST_PREV(nk->sec_desc) ) {
1857                                 REGF_SK_REC *prev = DLIST_PREV(nk->sec_desc);
1858
1859                                 nk->sec_desc->prev_sk_off = prev->hbin_off + prev->hbin->first_hbin_off - HBIN_HDR_SIZE;
1860                                 prev->next_sk_off = nk->sec_desc->sk_off;
1861
1862                                 /* the end must loop around to the front */
1863                                 nk->sec_desc->next_sk_off = file->sec_desc_list->sk_off;
1864
1865                                 /* and first must loop around to the tail */
1866                                 file->sec_desc_list->prev_sk_off = nk->sec_desc->sk_off;
1867                         } else {
1868                                 nk->sec_desc->prev_sk_off = nk->sec_desc->sk_off;
1869                                 nk->sec_desc->next_sk_off = nk->sec_desc->sk_off;
1870                         }
1871                 }
1872
1873                 /* bump the reference count +1 */
1874
1875                 nk->sk_off = nk->sec_desc->sk_off;
1876                 nk->sec_desc->ref_count++;
1877         }
1878
1879         /* write the subkeys */
1880
1881         nk->subkeys_off = REGF_OFFSET_NONE;
1882         if ( (nk->num_subkeys = regsubkey_ctr_numkeys( subkeys )) != 0 ) {
1883                 uint32 lf_size = lf_record_data_size( nk->num_subkeys );
1884                 uint32 namelen;
1885                 int i;
1886                 
1887                 if (!(nk->subkeys.hbin = find_free_space( file, lf_size ))) {
1888                         return NULL;
1889                 }
1890                 nk->subkeys.hbin_off = prs_offset( &nk->subkeys.hbin->ps );
1891                 nk->subkeys.rec_size = (lf_size-1) ^ 0xFFFFFFFF;
1892                 nk->subkeys_off = prs_offset( &nk->subkeys.hbin->ps ) + nk->subkeys.hbin->first_hbin_off - HBIN_HDR_SIZE;
1893
1894                 memcpy( nk->subkeys.header, "lf", REC_HDR_SIZE );
1895                 
1896                 nk->subkeys.num_keys = nk->num_subkeys;
1897                 if (nk->subkeys.num_keys) {
1898                         if ( !(nk->subkeys.hashes = TALLOC_ZERO_ARRAY( file->mem_ctx, REGF_HASH_REC, nk->subkeys.num_keys )) )
1899                                 return NULL;
1900                 } else {
1901                         nk->subkeys.hashes = NULL;
1902                 }
1903                 nk->subkey_index = 0;
1904
1905                 /* update the max_bytes_subkey{name,classname} fields */
1906                 for ( i=0; i<nk->num_subkeys; i++ ) {
1907                         namelen = strlen( regsubkey_ctr_specific_key(subkeys, i) );
1908                         if ( namelen*2 > nk->max_bytes_subkeyname )
1909                                 nk->max_bytes_subkeyname = namelen * 2;
1910                 }
1911         }
1912
1913         /* write the values */
1914
1915         nk->values_off = REGF_OFFSET_NONE;
1916         if ( (nk->num_values = regval_ctr_numvals( values )) != 0 ) {
1917                 uint32 vlist_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
1918                 int i;
1919                 
1920                 if (!(vlist_hbin = find_free_space( file, vlist_size ))) {
1921                         return NULL;
1922                 }
1923                 nk->values_off = prs_offset( &vlist_hbin->ps ) + vlist_hbin->first_hbin_off - HBIN_HDR_SIZE;
1924         
1925                 if (nk->num_values) {
1926                         if ( !(nk->values = TALLOC_ARRAY( file->mem_ctx, REGF_VK_REC, nk->num_values )) )
1927                                 return NULL;
1928                 } else {
1929                         nk->values = NULL;
1930                 }
1931
1932                 /* create the vk records */
1933
1934                 for ( i=0; i<nk->num_values; i++ ) {
1935                         uint32 vk_size, namelen, datalen;
1936                         struct regval_blob *r;
1937
1938                         r = regval_ctr_specific_value( values, i );
1939                         create_vk_record( file, &nk->values[i], r );
1940                         vk_size = vk_record_data_size( &nk->values[i] );
1941                         nk->values[i].hbin = find_free_space( file, vk_size );
1942                         nk->values[i].hbin_off = prs_offset( &nk->values[i].hbin->ps );
1943                         nk->values[i].rec_size = ( vk_size - 1 ) ^ 0xFFFFFFFF;
1944                         nk->values[i].rec_off = prs_offset( &nk->values[i].hbin->ps ) 
1945                                 + nk->values[i].hbin->first_hbin_off 
1946                                 - HBIN_HDR_SIZE;
1947
1948                         /* update the max bytes fields if necessary */
1949
1950                         namelen = strlen( regval_name(r) );
1951                         if ( namelen*2 > nk->max_bytes_valuename )
1952                                 nk->max_bytes_valuename = namelen * 2;
1953
1954                         datalen = regval_size( r );
1955                         if ( datalen > nk->max_bytes_value )
1956                                 nk->max_bytes_value = datalen;
1957                 }
1958         }
1959
1960         /* stream the records */        
1961         
1962         prs_set_offset( &nk->hbin->ps, nk->hbin_off );
1963         if ( !prs_nk_rec( "nk_rec", &nk->hbin->ps, 0, nk ) )
1964                 return False;
1965
1966         if ( nk->num_values ) {
1967                 if ( !hbin_prs_vk_records( "vk_records", vlist_hbin, 0, nk, file ) )
1968                         return False;
1969         }
1970
1971
1972         regfio_flush( file );
1973
1974         return nk;
1975 }
1976