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