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