2 * Unix SMB/CIFS implementation.
3 * Windows NT registry I/O library
4 * Copyright (c) Gerald (Jerry) Carter 2005
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 2 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /*******************************************************************
26 * TODO : Right now this code basically ignores classnames.
28 ******************************************************************/
31 /*******************************************************************
32 *******************************************************************/
34 static int write_block( REGF_FILE
*file
, prs_struct
*ps
, uint32 offset
)
36 int bytes_written
, returned
;
37 char *buffer
= prs_data_p( ps
);
38 uint32 buffer_size
= prs_data_size( ps
);
44 /* check for end of file */
46 if ( sys_fstat( file
->fd
, &sbuf
) ) {
47 DEBUG(0,("write_block: stat() failed! (%s)\n", strerror(errno
)));
51 if ( lseek( file
->fd
, offset
, SEEK_SET
) == -1 ) {
52 DEBUG(0,("write_block: lseek() failed! (%s)\n", strerror(errno
) ));
56 bytes_written
= returned
= 0;
57 while ( bytes_written
< buffer_size
) {
58 if ( (returned
= write( file
->fd
, buffer
+bytes_written
, buffer_size
-bytes_written
)) == -1 ) {
59 DEBUG(0,("write_block: write() failed! (%s)\n", strerror(errno
) ));
63 bytes_written
+= returned
;
69 /*******************************************************************
70 *******************************************************************/
72 static int read_block( REGF_FILE
*file
, prs_struct
*ps
, uint32 file_offset
, uint32 block_size
)
74 int bytes_read
, returned
;
78 /* check for end of file */
80 if ( sys_fstat( file
->fd
, &sbuf
) ) {
81 DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno
)));
85 if ( (size_t)file_offset
>= sbuf
.st_size
)
88 /* if block_size == 0, we are parsing HBIN records and need
89 to read some of the header to get the block_size from there */
91 if ( block_size
== 0 ) {
94 if ( lseek( file
->fd
, file_offset
, SEEK_SET
) == -1 ) {
95 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno
) ));
99 returned
= read( file
->fd
, hdr
, 0x20 );
100 if ( (returned
== -1) || (returned
< 0x20) ) {
101 DEBUG(0,("read_block: failed to read in HBIN header. Is the file corrupt?\n"));
105 /* make sure this is an hbin header */
107 if ( strncmp( hdr
, "hbin", HBIN_HDR_SIZE
) != 0 ) {
108 DEBUG(0,("read_block: invalid block header!\n"));
112 block_size
= IVAL( hdr
, 0x08 );
115 DEBUG(10,("read_block: block_size == 0x%x\n", block_size
));
117 /* set the offset, initialize the buffer, and read the block from disk */
119 if ( lseek( file
->fd
, file_offset
, SEEK_SET
) == -1 ) {
120 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno
) ));
124 prs_init( ps
, block_size
, file
->mem_ctx
, UNMARSHALL
);
125 buffer
= prs_data_p( ps
);
126 bytes_read
= returned
= 0;
128 while ( bytes_read
< block_size
) {
129 if ( (returned
= read( file
->fd
, buffer
+bytes_read
, block_size
-bytes_read
)) == -1 ) {
130 DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno
) ));
133 if ( (returned
== 0) && (bytes_read
< block_size
) ) {
134 DEBUG(0,("read_block: not a vald registry file ?\n" ));
138 bytes_read
+= returned
;
144 /*******************************************************************
145 *******************************************************************/
147 static BOOL
write_hbin_block( REGF_FILE
*file
, REGF_HBIN
*hbin
)
152 /* write free space record if any is available */
154 if ( hbin
->free_off
!= REGF_OFFSET_NONE
) {
155 uint32 header
= 0xffffffff;
157 if ( !prs_set_offset( &hbin
->ps
, hbin
->free_off
-sizeof(uint32
) ) )
159 if ( !prs_uint32( "free_size", &hbin
->ps
, 0, &hbin
->free_size
) )
161 if ( !prs_uint32( "free_header", &hbin
->ps
, 0, &header
) )
165 hbin
->dirty
= (write_block( file
, &hbin
->ps
, hbin
->file_off
) != -1);
170 /*******************************************************************
171 *******************************************************************/
173 static BOOL
hbin_block_close( REGF_FILE
*file
, REGF_HBIN
*hbin
)
177 /* remove the block from the open list and flush it to disk */
179 for ( p
=file
->block_list
; p
&& p
!=hbin
; p
=p
->next
)
183 DLIST_REMOVE( file
->block_list
, hbin
);
186 DEBUG(0,("hbin_block_close: block not in open list!\n"));
188 if ( !write_hbin_block( file
, hbin
) )
194 /*******************************************************************
195 *******************************************************************/
197 static BOOL
prs_regf_block( const char *desc
, prs_struct
*ps
, int depth
, REGF_FILE
*file
)
199 prs_debug(ps
, depth
, desc
, "prs_regf_block");
202 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8
*)file
->header
, sizeof( file
->header
)) )
205 /* yes, these values are always identical so store them only once */
207 if ( !prs_uint32( "unknown1", ps
, depth
, &file
->unknown1
))
209 if ( !prs_uint32( "unknown1 (again)", ps
, depth
, &file
->unknown1
))
212 /* get the modtime */
214 if ( !prs_set_offset( ps
, 0x0c ) )
216 if ( !smb_io_time( "modtime", &file
->mtime
, ps
, depth
) )
221 if ( !prs_uint32( "unknown2", ps
, depth
, &file
->unknown2
))
223 if ( !prs_uint32( "unknown3", ps
, depth
, &file
->unknown3
))
225 if ( !prs_uint32( "unknown4", ps
, depth
, &file
->unknown4
))
227 if ( !prs_uint32( "unknown5", ps
, depth
, &file
->unknown5
))
230 /* get file offsets */
232 if ( !prs_set_offset( ps
, 0x24 ) )
234 if ( !prs_uint32( "data_offset", ps
, depth
, &file
->data_offset
))
236 if ( !prs_uint32( "last_block", ps
, depth
, &file
->last_block
))
239 /* one more constant */
241 if ( !prs_uint32( "unknown6", ps
, depth
, &file
->unknown6
))
244 /* get the checksum */
246 if ( !prs_set_offset( ps
, 0x01fc ) )
248 if ( !prs_uint32( "checksum", ps
, depth
, &file
->checksum
))
254 /*******************************************************************
255 *******************************************************************/
257 static BOOL
prs_hbin_block( const char *desc
, prs_struct
*ps
, int depth
, REGF_HBIN
*hbin
)
261 prs_debug(ps
, depth
, desc
, "prs_regf_block");
264 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8
*)hbin
->header
, sizeof( hbin
->header
)) )
267 if ( !prs_uint32( "first_hbin_off", ps
, depth
, &hbin
->first_hbin_off
))
270 /* The dosreg.cpp comments say that the block size is at 0x1c.
271 According to a WINXP NTUSER.dat file, this is wrong. The block_size
274 if ( !prs_uint32( "block_size", ps
, depth
, &hbin
->block_size
))
277 block_size2
= hbin
->block_size
;
278 prs_set_offset( ps
, 0x1c );
279 if ( !prs_uint32( "block_size2", ps
, depth
, &block_size2
))
282 if ( MARSHALLING(ps
) )
289 /*******************************************************************
290 *******************************************************************/
292 static BOOL
prs_nk_rec( const char *desc
, prs_struct
*ps
, int depth
, REGF_NK_REC
*nk
)
294 uint16 class_length
, name_length
;
296 uint32 data_size
, start_off
, end_off
;
297 uint32 unknown_off
= REGF_OFFSET_NONE
;
299 nk
->hbin_off
= prs_offset( ps
);
300 start
= nk
->hbin_off
;
302 prs_debug(ps
, depth
, desc
, "prs_nk_rec");
305 /* back up and get the data_size */
307 if ( !prs_set_offset( ps
, prs_offset(ps
)-sizeof(uint32
)) )
309 start_off
= prs_offset( ps
);
310 if ( !prs_uint32( "rec_size", ps
, depth
, &nk
->rec_size
))
313 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8
*)nk
->header
, sizeof( nk
->header
)) )
316 if ( !prs_uint16( "key_type", ps
, depth
, &nk
->key_type
))
318 if ( !smb_io_time( "mtime", &nk
->mtime
, ps
, depth
))
321 if ( !prs_set_offset( ps
, start
+0x0010 ) )
323 if ( !prs_uint32( "parent_off", ps
, depth
, &nk
->parent_off
))
325 if ( !prs_uint32( "num_subkeys", ps
, depth
, &nk
->num_subkeys
))
328 if ( !prs_set_offset( ps
, start
+0x001c ) )
330 if ( !prs_uint32( "subkeys_off", ps
, depth
, &nk
->subkeys_off
))
332 if ( !prs_uint32( "unknown_off", ps
, depth
, &unknown_off
) )
335 if ( !prs_set_offset( ps
, start
+0x0024 ) )
337 if ( !prs_uint32( "num_values", ps
, depth
, &nk
->num_values
))
339 if ( !prs_uint32( "values_off", ps
, depth
, &nk
->values_off
))
341 if ( !prs_uint32( "sk_off", ps
, depth
, &nk
->sk_off
))
343 if ( !prs_uint32( "classname_off", ps
, depth
, &nk
->classname_off
))
346 if ( !prs_uint32( "max_bytes_subkeyname", ps
, depth
, &nk
->max_bytes_subkeyname
))
348 if ( !prs_uint32( "max_bytes_subkeyclassname", ps
, depth
, &nk
->max_bytes_subkeyclassname
))
350 if ( !prs_uint32( "max_bytes_valuename", ps
, depth
, &nk
->max_bytes_valuename
))
352 if ( !prs_uint32( "max_bytes_value", ps
, depth
, &nk
->max_bytes_value
))
354 if ( !prs_uint32( "unknown index", ps
, depth
, &nk
->unk_index
))
357 name_length
= nk
->keyname
? strlen(nk
->keyname
) : 0 ;
358 class_length
= nk
->classname
? strlen(nk
->classname
) : 0 ;
359 if ( !prs_uint16( "name_length", ps
, depth
, &name_length
))
361 if ( !prs_uint16( "class_length", ps
, depth
, &class_length
))
364 if ( class_length
) {
369 if ( UNMARSHALLING(ps
) ) {
370 if ( !(nk
->keyname
= PRS_ALLOC_MEM( ps
, char, name_length
+1 )) )
374 if ( !prs_uint8s( True
, "name", ps
, depth
, (uint8
*)nk
->keyname
, name_length
) )
377 if ( UNMARSHALLING(ps
) )
378 nk
->keyname
[name_length
] = '\0';
381 end_off
= prs_offset( ps
);
383 /* data_size must be divisible by 8 and large enough to hold the original record */
385 data_size
= ((start_off
- end_off
) & 0xfffffff8 );
386 if ( data_size
> nk
->rec_size
)
387 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size
, nk
->rec_size
));
389 if ( MARSHALLING(ps
) )
390 nk
->hbin
->dirty
= True
;
395 /*******************************************************************
396 *******************************************************************/
398 static uint32
regf_block_checksum( prs_struct
*ps
)
400 char *buffer
= prs_data_p( ps
);
404 /* XOR of all bytes 0x0000 - 0x01FB */
408 for ( i
=0; i
<0x01FB; i
+=4 ) {
409 x
= IVAL(buffer
, i
);
416 /*******************************************************************
417 *******************************************************************/
419 static BOOL
read_regf_block( REGF_FILE
*file
)
424 /* grab the first block from the file */
426 if ( read_block( file
, &ps
, 0, REGF_BLOCKSIZE
) == -1 )
429 /* parse the block and verify the checksum */
431 if ( !prs_regf_block( "regf_header", &ps
, 0, file
) )
434 checksum
= regf_block_checksum( &ps
);
438 if ( file
->checksum
!= checksum
) {
439 DEBUG(0,("read_regf_block: invalid checksum\n" ));
446 /*******************************************************************
447 *******************************************************************/
449 static REGF_HBIN
* read_hbin_block( REGF_FILE
*file
, off_t offset
)
452 uint32 record_size
, curr_off
, block_size
, header
;
454 if ( !(hbin
= TALLOC_ZERO_P(file
->mem_ctx
, REGF_HBIN
)) )
456 hbin
->file_off
= offset
;
459 if ( read_block( file
, &hbin
->ps
, offset
, 0 ) == -1 )
462 if ( !prs_hbin_block( "hbin", &hbin
->ps
, 0, hbin
) )
465 /* this should be the same thing as hbin->block_size but just in case */
467 block_size
= prs_data_size( &hbin
->ps
);
469 /* Find the available free space offset. Always at the end,
470 so walk the record list and stop when you get to the end.
471 The end is defined by a record header of 0xffffffff. The
472 previous 4 bytes contains the amount of free space remaining
473 in the hbin block. */
475 /* remember that the record_size is in the 4 bytes preceeding the record itself */
477 if ( !prs_set_offset( &hbin
->ps
, file
->data_offset
+HBIN_HDR_SIZE
-sizeof(uint32
) ) )
482 curr_off
= prs_offset( &hbin
->ps
);
483 while ( header
!= 0xffffffff ) {
484 /* not done yet so reset the current offset to the
485 next record_size field */
487 curr_off
= curr_off
+record_size
;
489 /* for some reason the record_size of the last record in
490 an hbin block can extend past the end of the block
491 even though the record fits within the remaining
492 space....aaarrrgggghhhhhh */
494 if ( curr_off
>= block_size
) {
500 if ( !prs_set_offset( &hbin
->ps
, curr_off
) )
503 if ( !prs_uint32( "rec_size", &hbin
->ps
, 0, &record_size
) )
505 if ( !prs_uint32( "header", &hbin
->ps
, 0, &header
) )
508 SMB_ASSERT( record_size
!= 0 );
510 if ( record_size
& 0x80000000 ) {
511 /* absolute_value(record_size) */
512 record_size
= (record_size
^ 0xffffffff) + 1;
516 /* save the free space offset */
518 if ( header
== 0xffffffff ) {
520 /* account for the fact that the curr_off is 4 bytes behind the actual
523 hbin
->free_off
= curr_off
+ sizeof(uint32
);
524 hbin
->free_size
= record_size
;
527 DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin
->free_off
));
529 if ( !prs_set_offset( &hbin
->ps
, file
->data_offset
+HBIN_HDR_SIZE
) )
535 /*******************************************************************
536 Input a random offset and receive the corresponding HBIN
538 *******************************************************************/
540 static BOOL
hbin_contains_offset( REGF_HBIN
*hbin
, uint32 offset
)
545 if ( (offset
> hbin
->first_hbin_off
) && (offset
< (hbin
->first_hbin_off
+hbin
->block_size
)) )
551 /*******************************************************************
552 Input a random offset and receive the corresponding HBIN
554 *******************************************************************/
556 static REGF_HBIN
* lookup_hbin_block( REGF_FILE
*file
, uint32 offset
)
558 REGF_HBIN
*hbin
= NULL
;
561 /* start with the open list */
563 for ( hbin
=file
->block_list
; hbin
; hbin
=hbin
->next
) {
564 DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%lx]\n", hbin
->file_off
, (unsigned long)hbin
));
565 if ( hbin_contains_offset( hbin
, offset
) )
570 /* start at the beginning */
572 block_off
= REGF_BLOCKSIZE
;
574 /* cleanup before the next round */
576 prs_mem_free( &hbin
->ps
);
578 hbin
= read_hbin_block( file
, block_off
);
581 block_off
= hbin
->file_off
+ hbin
->block_size
;
583 } while ( hbin
&& !hbin_contains_offset( hbin
, offset
) );
587 DLIST_ADD( file
->block_list
, hbin
);
592 /*******************************************************************
593 *******************************************************************/
595 static BOOL
prs_hash_rec( const char *desc
, prs_struct
*ps
, int depth
, REGF_HASH_REC
*hash
)
597 prs_debug(ps
, depth
, desc
, "prs_hash_rec");
600 if ( !prs_uint32( "nk_off", ps
, depth
, &hash
->nk_off
))
602 if ( !prs_uint8s( True
, "keycheck", ps
, depth
, hash
->keycheck
, sizeof( hash
->keycheck
)) )
608 /*******************************************************************
609 *******************************************************************/
611 static BOOL
hbin_prs_lf_records( const char *desc
, REGF_HBIN
*hbin
, int depth
, REGF_NK_REC
*nk
)
614 REGF_LF_REC
*lf
= &nk
->subkeys
;
615 uint32 data_size
, start_off
, end_off
;
617 prs_debug(&hbin
->ps
, depth
, desc
, "prs_lf_records");
620 /* check if we have anything to do first */
622 if ( nk
->num_subkeys
== 0 )
625 /* move to the LF record */
627 if ( !prs_set_offset( &hbin
->ps
, nk
->subkeys_off
+ HBIN_HDR_SIZE
- hbin
->first_hbin_off
) )
630 /* backup and get the data_size */
632 if ( !prs_set_offset( &hbin
->ps
, prs_offset(&hbin
->ps
)-sizeof(uint32
)) )
634 start_off
= prs_offset( &hbin
->ps
);
635 if ( !prs_uint32( "rec_size", &hbin
->ps
, depth
, &lf
->rec_size
))
638 if ( !prs_uint8s( True
, "header", &hbin
->ps
, depth
, (uint8
*)lf
->header
, sizeof( lf
->header
)) )
641 if ( !prs_uint16( "num_keys", &hbin
->ps
, depth
, &lf
->num_keys
))
644 if ( UNMARSHALLING(&hbin
->ps
) ) {
646 if ( !(lf
->hashes
= PRS_ALLOC_MEM( &hbin
->ps
, REGF_HASH_REC
, lf
->num_keys
)) )
653 for ( i
=0; i
<lf
->num_keys
; i
++ ) {
654 if ( !prs_hash_rec( "hash_rec", &hbin
->ps
, depth
, &lf
->hashes
[i
] ) )
658 end_off
= prs_offset( &hbin
->ps
);
660 /* data_size must be divisible by 8 and large enough to hold the original record */
662 data_size
= ((start_off
- end_off
) & 0xfffffff8 );
663 if ( data_size
> lf
->rec_size
)
664 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size
, lf
->rec_size
));
666 if ( MARSHALLING(&hbin
->ps
) )
672 /*******************************************************************
673 *******************************************************************/
675 static BOOL
hbin_prs_sk_rec( const char *desc
, REGF_HBIN
*hbin
, int depth
, REGF_SK_REC
*sk
)
677 prs_struct
*ps
= &hbin
->ps
;
679 uint32 data_size
, start_off
, end_off
;
682 prs_debug(ps
, depth
, desc
, "hbin_prs_sk_rec");
685 if ( !prs_set_offset( &hbin
->ps
, sk
->sk_off
+ HBIN_HDR_SIZE
- hbin
->first_hbin_off
) )
688 /* backup and get the data_size */
690 if ( !prs_set_offset( &hbin
->ps
, prs_offset(&hbin
->ps
)-sizeof(uint32
)) )
692 start_off
= prs_offset( &hbin
->ps
);
693 if ( !prs_uint32( "rec_size", &hbin
->ps
, depth
, &sk
->rec_size
))
696 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8
*)sk
->header
, sizeof( sk
->header
)) )
698 if ( !prs_uint16( "tag", ps
, depth
, &tag
))
701 if ( !prs_uint32( "prev_sk_off", ps
, depth
, &sk
->prev_sk_off
))
703 if ( !prs_uint32( "next_sk_off", ps
, depth
, &sk
->next_sk_off
))
705 if ( !prs_uint32( "ref_count", ps
, depth
, &sk
->ref_count
))
707 if ( !prs_uint32( "size", ps
, depth
, &sk
->size
))
710 if ( !sec_io_desc( "sec_desc", &sk
->sec_desc
, ps
, depth
))
713 end_off
= prs_offset( &hbin
->ps
);
715 /* data_size must be divisible by 8 and large enough to hold the original record */
717 data_size
= ((start_off
- end_off
) & 0xfffffff8 );
718 if ( data_size
> sk
->rec_size
)
719 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size
, sk
->rec_size
));
721 if ( MARSHALLING(&hbin
->ps
) )
727 /*******************************************************************
728 *******************************************************************/
730 static BOOL
hbin_prs_vk_rec( const char *desc
, REGF_HBIN
*hbin
, int depth
, REGF_VK_REC
*vk
, REGF_FILE
*file
)
734 prs_struct
*ps
= &hbin
->ps
;
735 uint32 data_size
, start_off
, end_off
;
737 prs_debug(ps
, depth
, desc
, "prs_vk_rec");
740 /* backup and get the data_size */
742 if ( !prs_set_offset( &hbin
->ps
, prs_offset(&hbin
->ps
)-sizeof(uint32
)) )
744 start_off
= prs_offset( &hbin
->ps
);
745 if ( !prs_uint32( "rec_size", &hbin
->ps
, depth
, &vk
->rec_size
))
748 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8
*)vk
->header
, sizeof( vk
->header
)) )
751 if ( MARSHALLING(&hbin
->ps
) )
752 name_length
= strlen(vk
->valuename
);
754 if ( !prs_uint16( "name_length", ps
, depth
, &name_length
))
756 if ( !prs_uint32( "data_size", ps
, depth
, &vk
->data_size
))
758 if ( !prs_uint32( "data_off", ps
, depth
, &vk
->data_off
))
760 if ( !prs_uint32( "type", ps
, depth
, &vk
->type
))
762 if ( !prs_uint16( "flag", ps
, depth
, &vk
->flag
))
765 offset
= prs_offset( ps
);
766 offset
+= 2; /* skip 2 bytes */
767 prs_set_offset( ps
, offset
);
771 if ( vk
->flag
&VK_FLAG_NAME_PRESENT
) {
773 if ( UNMARSHALLING(&hbin
->ps
) ) {
774 if ( !(vk
->valuename
= PRS_ALLOC_MEM( ps
, char, name_length
+1 )))
777 if ( !prs_uint8s( True
, "name", ps
, depth
, (uint8
*)vk
->valuename
, name_length
) )
781 end_off
= prs_offset( &hbin
->ps
);
783 /* get the data if necessary */
785 if ( vk
->data_size
!= 0 ) {
786 BOOL charmode
= False
;
788 if ( (vk
->type
== REG_SZ
) || (vk
->type
== REG_MULTI_SZ
) )
791 /* the data is stored in the offset if the size <= 4 */
793 if ( !(vk
->data_size
& VK_DATA_IN_OFFSET
) ) {
794 REGF_HBIN
*hblock
= hbin
;
795 uint32 data_rec_size
;
797 if ( UNMARSHALLING(&hbin
->ps
) ) {
798 if ( !(vk
->data
= PRS_ALLOC_MEM( ps
, uint8
, vk
->data_size
) ) )
802 /* this data can be in another hbin */
803 if ( !hbin_contains_offset( hbin
, vk
->data_off
) ) {
804 if ( !(hblock
= lookup_hbin_block( file
, vk
->data_off
)) )
807 if ( !(prs_set_offset( &hblock
->ps
, (vk
->data_off
+HBIN_HDR_SIZE
-hblock
->first_hbin_off
)-sizeof(uint32
) )) )
810 if ( MARSHALLING(&hblock
->ps
) ) {
811 data_rec_size
= ( (vk
->data_size
+sizeof(uint32
)) & 0xfffffff8 ) + 8;
812 data_rec_size
= ( data_rec_size
- 1 ) ^ 0xFFFFFFFF;
814 if ( !prs_uint32( "data_rec_size", &hblock
->ps
, depth
, &data_rec_size
))
816 if ( !prs_uint8s( charmode
, "data", &hblock
->ps
, depth
, vk
->data
, vk
->data_size
) )
819 if ( MARSHALLING(&hblock
->ps
) )
820 hblock
->dirty
= True
;
823 if ( !(vk
->data
= PRS_ALLOC_MEM( ps
, uint8
, 4 ) ) )
825 SIVAL( vk
->data
, 0, vk
->data_off
);
830 /* data_size must be divisible by 8 and large enough to hold the original record */
832 data_size
= ((start_off
- end_off
) & 0xfffffff8 );
833 if ( data_size
!= vk
->rec_size
)
834 DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size
, vk
->rec_size
));
836 if ( MARSHALLING(&hbin
->ps
) )
842 /*******************************************************************
843 read a VK record which is contained in the HBIN block stored
844 in the prs_struct *ps.
845 *******************************************************************/
847 static BOOL
hbin_prs_vk_records( const char *desc
, REGF_HBIN
*hbin
, int depth
, REGF_NK_REC
*nk
, REGF_FILE
*file
)
852 prs_debug(&hbin
->ps
, depth
, desc
, "prs_vk_records");
855 /* check if we have anything to do first */
857 if ( nk
->num_values
== 0 )
860 if ( UNMARSHALLING(&hbin
->ps
) ) {
861 if ( !(nk
->values
= PRS_ALLOC_MEM( &hbin
->ps
, REGF_VK_REC
, nk
->num_values
) ) )
865 /* convert the offset to something relative to this HBIN block */
867 if ( !prs_set_offset( &hbin
->ps
, nk
->values_off
+HBIN_HDR_SIZE
-hbin
->first_hbin_off
-sizeof(uint32
)) )
870 if ( MARSHALLING( &hbin
->ps
) ) {
871 record_size
= ( ( nk
->num_values
* sizeof(uint32
) ) & 0xfffffff8 ) + 8;
872 record_size
= (record_size
- 1) ^ 0xFFFFFFFF;
875 if ( !prs_uint32( "record_size", &hbin
->ps
, depth
, &record_size
) )
878 for ( i
=0; i
<nk
->num_values
; i
++ ) {
879 if ( !prs_uint32( "vk_off", &hbin
->ps
, depth
, &nk
->values
[i
].rec_off
) )
883 for ( i
=0; i
<nk
->num_values
; i
++ ) {
884 REGF_HBIN
*sub_hbin
= hbin
;
887 if ( !hbin_contains_offset( hbin
, nk
->values
[i
].rec_off
) ) {
888 sub_hbin
= lookup_hbin_block( file
, nk
->values
[i
].rec_off
);
890 DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n",
891 nk
->values
[i
].hbin_off
));
896 new_offset
= nk
->values
[i
].rec_off
+ HBIN_HDR_SIZE
- sub_hbin
->first_hbin_off
;
897 if ( !prs_set_offset( &sub_hbin
->ps
, new_offset
) )
899 if ( !hbin_prs_vk_rec( "vk_rec", sub_hbin
, depth
, &nk
->values
[i
], file
) )
903 if ( MARSHALLING(&hbin
->ps
) )
911 /*******************************************************************
912 *******************************************************************/
914 static REGF_SK_REC
* find_sk_record_by_offset( REGF_FILE
*file
, uint32 offset
)
918 for ( p_sk
=file
->sec_desc_list
; p_sk
; p_sk
=p_sk
->next
) {
919 if ( p_sk
->sk_off
== offset
)
926 /*******************************************************************
927 *******************************************************************/
929 static REGF_SK_REC
* find_sk_record_by_sec_desc( REGF_FILE
*file
, SEC_DESC
*sd
)
933 for ( p
=file
->sec_desc_list
; p
; p
=p
->next
) {
934 if ( sec_desc_equal( p
->sec_desc
, sd
) )
943 /*******************************************************************
944 *******************************************************************/
946 static BOOL
hbin_prs_key( REGF_FILE
*file
, REGF_HBIN
*hbin
, REGF_NK_REC
*nk
)
951 prs_debug(&hbin
->ps
, depth
, "", "fetch_key");
954 /* get the initial nk record */
956 if ( !prs_nk_rec( "nk_rec", &hbin
->ps
, depth
, nk
))
961 if ( nk
->num_values
&& (nk
->values_off
!=REGF_OFFSET_NONE
) ) {
963 if ( !hbin_contains_offset( hbin
, nk
->values_off
) ) {
964 sub_hbin
= lookup_hbin_block( file
, nk
->values_off
);
966 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n",
972 if ( !hbin_prs_vk_records( "vk_rec", sub_hbin
, depth
, nk
, file
))
976 /* now get subkeys */
978 if ( nk
->num_subkeys
&& (nk
->subkeys_off
!=REGF_OFFSET_NONE
) ) {
980 if ( !hbin_contains_offset( hbin
, nk
->subkeys_off
) ) {
981 sub_hbin
= lookup_hbin_block( file
, nk
->subkeys_off
);
983 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n",
989 if ( !hbin_prs_lf_records( "lf_rec", sub_hbin
, depth
, nk
))
993 /* get the to the security descriptor. First look if we have already parsed it */
995 if ( (nk
->sk_off
!=REGF_OFFSET_NONE
) && !( nk
->sec_desc
= find_sk_record_by_offset( file
, nk
->sk_off
)) ) {
998 if ( !hbin_contains_offset( hbin
, nk
->sk_off
) ) {
999 sub_hbin
= lookup_hbin_block( file
, nk
->sk_off
);
1001 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_offset [0x%x]\n",
1007 if ( !(nk
->sec_desc
= TALLOC_ZERO_P( file
->mem_ctx
, REGF_SK_REC
)) )
1009 nk
->sec_desc
->sk_off
= nk
->sk_off
;
1010 if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin
, depth
, nk
->sec_desc
))
1013 /* add to the list of security descriptors (ref_count has been read from the files) */
1015 nk
->sec_desc
->sk_off
= nk
->sk_off
;
1016 DLIST_ADD( file
->sec_desc_list
, nk
->sec_desc
);
1022 /*******************************************************************
1023 *******************************************************************/
1025 static BOOL
next_record( REGF_HBIN
*hbin
, const char *hdr
, BOOL
*eob
)
1027 uint8 header
[REC_HDR_SIZE
];
1029 uint32 curr_off
, block_size
;
1031 prs_struct
*ps
= &hbin
->ps
;
1033 curr_off
= prs_offset( ps
);
1034 if ( curr_off
== 0 )
1035 prs_set_offset( ps
, HBIN_HEADER_REC_SIZE
);
1037 /* assume that the current offset is at the record header
1038 and we need to backup to read the record size */
1040 curr_off
-= sizeof(uint32
);
1042 block_size
= prs_data_size( ps
);
1044 memset( header
, 0x0, sizeof(uint8
)*REC_HDR_SIZE
);
1047 curr_off
= curr_off
+record_size
;
1048 if ( curr_off
>= block_size
)
1051 if ( !prs_set_offset( &hbin
->ps
, curr_off
) )
1054 if ( !prs_uint32( "record_size", ps
, 0, &record_size
) )
1056 if ( !prs_uint8s( True
, "header", ps
, 0, header
, REC_HDR_SIZE
) )
1059 if ( record_size
& 0x80000000 ) {
1060 /* absolute_value(record_size) */
1061 record_size
= (record_size
^ 0xffffffff) + 1;
1064 if ( memcmp( header
, hdr
, REC_HDR_SIZE
) == 0 ) {
1066 curr_off
+= sizeof(uint32
);
1070 /* mark prs_struct as done ( at end ) if no more SK records */
1071 /* mark end-of-block as True */
1074 prs_set_offset( &hbin
->ps
, prs_data_size(&hbin
->ps
) );
1079 if ( !prs_set_offset( ps
, curr_off
) )
1085 /*******************************************************************
1086 *******************************************************************/
1088 static BOOL
next_nk_record( REGF_FILE
*file
, REGF_HBIN
*hbin
, REGF_NK_REC
*nk
, BOOL
*eob
)
1090 if ( next_record( hbin
, "nk", eob
) && hbin_prs_key( file
, hbin
, nk
) )
1096 /*******************************************************************
1097 Intialize the newly created REGF_BLOCK in *file and write the
1098 block header to disk
1099 *******************************************************************/
1101 static BOOL
init_regf_block( REGF_FILE
*file
)
1106 if ( !prs_init( &ps
, REGF_BLOCKSIZE
, file
->mem_ctx
, MARSHALL
) )
1109 memcpy( file
->header
, "regf", REGF_HDR_SIZE
);
1110 file
->data_offset
= 0x20;
1111 file
->last_block
= 0x1000;
1115 unix_to_nt_time( &file
->mtime
, time(NULL
) );
1117 /* hard coded values...no diea what these are ... maybe in time */
1119 file
->unknown1
= 0x2;
1120 file
->unknown2
= 0x1;
1121 file
->unknown3
= 0x3;
1122 file
->unknown4
= 0x0;
1123 file
->unknown5
= 0x1;
1124 file
->unknown6
= 0x1;
1126 /* write header to the buffer */
1128 if ( !prs_regf_block( "regf_header", &ps
, 0, file
) ) {
1133 /* calculate the checksum, re-marshall data (to include the checksum)
1134 and write to disk */
1136 file
->checksum
= regf_block_checksum( &ps
);
1137 prs_set_offset( &ps
, 0 );
1138 if ( !prs_regf_block( "regf_header", &ps
, 0, file
) ) {
1143 if ( write_block( file
, &ps
, 0 ) == -1 ) {
1144 DEBUG(0,("init_regf_block: Failed to initialize registry header block!\n"));
1150 prs_mem_free( &ps
);
1154 /*******************************************************************
1155 Open the registry file and then read in the REGF block to get the
1157 *******************************************************************/
1159 REGF_FILE
* regfio_open( const char *filename
, int flags
, int mode
)
1163 if ( !(rb
= SMB_MALLOC_P(REGF_FILE
)) ) {
1164 DEBUG(0,("ERROR allocating memory\n"));
1170 if ( !(rb
->mem_ctx
= talloc_init( "read_regf_block" )) ) {
1175 rb
->open_flags
= flags
;
1177 /* open and existing file */
1179 if ( (rb
->fd
= open(filename
, flags
, mode
)) == -1 ) {
1180 DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename
, strerror(errno
)));
1185 /* check if we are creating a new file or overwriting an existing one */
1187 if ( flags
& (O_CREAT
|O_TRUNC
) ) {
1188 if ( !init_regf_block( rb
) ) {
1189 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1198 /* read in an existing file */
1200 if ( !read_regf_block( rb
) ) {
1201 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1211 /*******************************************************************
1212 *******************************************************************/
1214 static void regfio_mem_free( REGF_FILE
*file
)
1216 /* free any talloc()'d memory */
1218 if ( file
&& file
->mem_ctx
)
1219 talloc_destroy( file
->mem_ctx
);
1222 /*******************************************************************
1223 *******************************************************************/
1225 int regfio_close( REGF_FILE
*file
)
1229 /* cleanup for a file opened for write */
1231 if ( file
->open_flags
& (O_WRONLY
|O_RDWR
) ) {
1235 /* write of sd list */
1237 for ( sk
=file
->sec_desc_list
; sk
; sk
=sk
->next
) {
1238 hbin_prs_sk_rec( "sk_rec", sk
->hbin
, 0, sk
);
1241 /* flush any dirty blocks */
1243 while ( file
->block_list
) {
1244 hbin_block_close( file
, file
->block_list
);
1249 unix_to_nt_time( &file
->mtime
, time(NULL
) );
1251 if ( read_block( file
, &ps
, 0, REGF_BLOCKSIZE
) != -1 ) {
1252 /* now use for writing */
1253 prs_switch_type( &ps
, MARSHALL
);
1255 /* stream the block once, generate the checksum,
1256 and stream it again */
1257 prs_set_offset( &ps
, 0 );
1258 prs_regf_block( "regf_blocK", &ps
, 0, file
);
1259 file
->checksum
= regf_block_checksum( &ps
);
1260 prs_set_offset( &ps
, 0 );
1261 prs_regf_block( "regf_blocK", &ps
, 0, file
);
1263 /* now we are ready to write it to disk */
1264 if ( write_block( file
, &ps
, 0 ) == -1 )
1265 DEBUG(0,("regfio_close: failed to update the regf header block!\n"));
1268 prs_mem_free( &ps
);
1271 regfio_mem_free( file
);
1273 /* nothing tdo do if there is no open file */
1275 if ( !file
|| (file
->fd
== -1) )
1285 /*******************************************************************
1286 *******************************************************************/
1288 static void regfio_flush( REGF_FILE
*file
)
1292 for ( hbin
=file
->block_list
; hbin
; hbin
=hbin
->next
) {
1293 write_hbin_block( file
, hbin
);
1297 /*******************************************************************
1298 There should be only *one* root key in the registry file based
1299 on my experience. --jerry
1300 *******************************************************************/
1302 REGF_NK_REC
* regfio_rootkey( REGF_FILE
*file
)
1306 uint32 offset
= REGF_BLOCKSIZE
;
1313 if ( !(nk
= TALLOC_ZERO_P( file
->mem_ctx
, REGF_NK_REC
)) ) {
1314 DEBUG(0,("regfio_rootkey: talloc() failed!\n"));
1318 /* scan through the file on HBIN block at a time looking
1319 for an NK record with a type == 0x002c.
1320 Normally this is the first nk record in the first hbin
1321 block (but I'm not assuming that for now) */
1323 while ( (hbin
= read_hbin_block( file
, offset
)) ) {
1327 if ( next_nk_record( file
, hbin
, nk
, &eob
) ) {
1328 if ( nk
->key_type
== NK_TYPE_ROOTKEY
) {
1333 prs_mem_free( &hbin
->ps
);
1339 offset
+= hbin
->block_size
;
1343 DEBUG(0,("regfio_rootkey: corrupt registry file ? No root key record located\n"));
1347 DLIST_ADD( file
->block_list
, hbin
);
1352 /*******************************************************************
1353 This acts as an interator over the subkeys defined for a given
1354 NK record. Remember that offsets are from the *first* HBIN block.
1355 *******************************************************************/
1357 REGF_NK_REC
* regfio_fetch_subkey( REGF_FILE
*file
, REGF_NK_REC
*nk
)
1359 REGF_NK_REC
*subkey
;
1363 /* see if there is anything left to report */
1365 if ( !nk
|| (nk
->subkeys_off
==REGF_OFFSET_NONE
) || (nk
->subkey_index
>= nk
->num_subkeys
) )
1368 /* find the HBIN block which should contain the nk record */
1370 if ( !(hbin
= lookup_hbin_block( file
, nk
->subkeys
.hashes
[nk
->subkey_index
].nk_off
)) ) {
1371 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n",
1372 nk
->subkeys
.hashes
[nk
->subkey_index
].nk_off
));
1376 nk_offset
= nk
->subkeys
.hashes
[nk
->subkey_index
].nk_off
;
1377 if ( !prs_set_offset( &hbin
->ps
, (HBIN_HDR_SIZE
+ nk_offset
- hbin
->first_hbin_off
) ) )
1381 if ( !(subkey
= TALLOC_ZERO_P( file
->mem_ctx
, REGF_NK_REC
)) )
1384 if ( !hbin_prs_key( file
, hbin
, subkey
) )
1391 /*******************************************************************
1392 *******************************************************************/
1394 static REGF_HBIN
* regf_hbin_allocate( REGF_FILE
*file
, uint32 block_size
)
1397 SMB_STRUCT_STAT sbuf
;
1399 if ( !(hbin
= TALLOC_ZERO_P( file
->mem_ctx
, REGF_HBIN
)) )
1402 memcpy( hbin
->header
, "hbin", sizeof(HBIN_HDR_SIZE
) );
1405 if ( sys_fstat( file
->fd
, &sbuf
) ) {
1406 DEBUG(0,("regf_hbin_allocate: stat() failed! (%s)\n", strerror(errno
)));
1410 hbin
->file_off
= sbuf
.st_size
;
1412 hbin
->free_off
= HBIN_HEADER_REC_SIZE
;
1413 hbin
->free_size
= block_size
- hbin
->free_off
+ sizeof(uint32
);;
1415 hbin
->block_size
= block_size
;
1416 hbin
->first_hbin_off
= hbin
->file_off
- REGF_BLOCKSIZE
;
1418 if ( !prs_init( &hbin
->ps
, block_size
, file
->mem_ctx
, MARSHALL
) )
1421 if ( !prs_hbin_block( "new_hbin", &hbin
->ps
, 0, hbin
) )
1424 if ( !write_hbin_block( file
, hbin
) )
1427 file
->last_block
= hbin
->file_off
;
1432 /*******************************************************************
1433 *******************************************************************/
1435 static void update_free_space( REGF_HBIN
*hbin
, uint32 size_used
)
1437 hbin
->free_off
+= size_used
;
1438 hbin
->free_size
-= size_used
;
1440 if ( hbin
->free_off
>= hbin
->block_size
) {
1441 hbin
->free_off
= REGF_OFFSET_NONE
;
1447 /*******************************************************************
1448 *******************************************************************/
1450 static REGF_HBIN
* find_free_space( REGF_FILE
*file
, uint32 size
)
1452 REGF_HBIN
*hbin
, *p_hbin
;
1456 /* check open block list */
1458 for ( hbin
=file
->block_list
; hbin
!=NULL
; hbin
=hbin
->next
) {
1459 /* only check blocks that actually have available space */
1461 if ( hbin
->free_off
== REGF_OFFSET_NONE
)
1464 /* check for a large enough available chunk */
1466 if ( (hbin
->block_size
- hbin
->free_off
) >= size
) {
1467 DLIST_PROMOTE( file
->block_list
, hbin
);
1472 /* parse the file until we find a block with
1473 enough free space; save the last non-filled hbin */
1475 block_off
= REGF_BLOCKSIZE
;
1477 /* cleanup before the next round */
1480 prs_mem_free( &hbin
->ps
);
1482 hbin
= read_hbin_block( file
, block_off
);
1486 /* make sure that we don't already have this block in memory */
1488 for ( p_hbin
=file
->block_list
; p_hbin
!=NULL
; p_hbin
=p_hbin
->next
) {
1489 if ( p_hbin
->file_off
== hbin
->file_off
) {
1495 block_off
= hbin
->file_off
+ hbin
->block_size
;
1498 prs_mem_free( &hbin
->ps
);
1503 /* if (cached block or (new block and not enough free space)) then continue looping */
1504 } while ( cached
|| (hbin
&& (hbin
->free_size
< size
)) );
1506 /* no free space; allocate a new one */
1511 /* allocate in multiples of REGF_ALLOC_BLOCK; make sure (size + hbin_header) fits */
1513 alloc_size
= (((size
+HBIN_HEADER_REC_SIZE
) / REGF_ALLOC_BLOCK
) + 1 ) * REGF_ALLOC_BLOCK
;
1515 if ( !(hbin
= regf_hbin_allocate( file
, alloc_size
)) ) {
1516 DEBUG(0,("find_free_space: regf_hbin_allocate() failed!\n"));
1519 DLIST_ADD( file
->block_list
, hbin
);
1523 /* set the offset to be ready to write */
1525 if ( !prs_set_offset( &hbin
->ps
, hbin
->free_off
-sizeof(uint32
) ) )
1528 /* write the record size as a placeholder for now, it should be
1529 probably updated by the caller once it all of the data necessary
1532 if ( !prs_uint32("allocated_size", &hbin
->ps
, 0, &size
) )
1535 update_free_space( hbin
, size
);
1540 /*******************************************************************
1541 *******************************************************************/
1543 static uint32
sk_record_data_size( SEC_DESC
* sd
)
1545 uint32 size
, size_mod8
;
1549 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1551 size
= sizeof(uint32
)*5 + sec_desc_size( sd
) + sizeof(uint32
);
1554 size_mod8
= size
& 0xfffffff8;
1555 if ( size_mod8
< size
)
1561 /*******************************************************************
1562 *******************************************************************/
1564 static uint32
vk_record_data_size( REGF_VK_REC
*vk
)
1566 uint32 size
, size_mod8
;
1570 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1572 size
= REC_HDR_SIZE
+ (sizeof(uint16
)*3) + (sizeof(uint32
)*3) + sizeof(uint32
);
1574 if ( vk
->valuename
)
1575 size
+= strlen(vk
->valuename
);
1578 size_mod8
= size
& 0xfffffff8;
1579 if ( size_mod8
< size
)
1585 /*******************************************************************
1586 *******************************************************************/
1588 static uint32
lf_record_data_size( uint32 num_keys
)
1590 uint32 size
, size_mod8
;
1594 /* the record size is sizeof(hdr) + num_keys + sizeof of hash_array + data_size_uint32 */
1596 size
= REC_HDR_SIZE
+ sizeof(uint16
) + (sizeof(REGF_HASH_REC
) * num_keys
) + sizeof(uint32
);
1599 size_mod8
= size
& 0xfffffff8;
1600 if ( size_mod8
< size
)
1606 /*******************************************************************
1607 *******************************************************************/
1609 static uint32
nk_record_data_size( REGF_NK_REC
*nk
)
1611 uint32 size
, size_mod8
;
1615 /* the record size is static + length_of_keyname + length_of_classname + data_size_uint32 */
1617 size
= 0x4c + strlen(nk
->keyname
) + sizeof(uint32
);
1619 if ( nk
->classname
)
1620 size
+= strlen( nk
->classname
);
1623 size_mod8
= size
& 0xfffffff8;
1624 if ( size_mod8
< size
)
1630 /*******************************************************************
1631 *******************************************************************/
1633 static BOOL
create_vk_record( REGF_FILE
*file
, REGF_VK_REC
*vk
, REGISTRY_VALUE
*value
)
1635 char *name
= regval_name(value
);
1636 REGF_HBIN
*data_hbin
;
1640 memcpy( vk
->header
, "vk", REC_HDR_SIZE
);
1643 vk
->valuename
= talloc_strdup( file
->mem_ctx
, regval_name(value
) );
1644 vk
->flag
= VK_FLAG_NAME_PRESENT
;
1647 vk
->data_size
= regval_size( value
);
1648 vk
->type
= regval_type( value
);
1650 if ( vk
->data_size
> sizeof(uint32
) ) {
1651 uint32 data_size
= ( (vk
->data_size
+sizeof(uint32
)) & 0xfffffff8 ) + 8;
1653 vk
->data
= (uint8
*)TALLOC_MEMDUP( file
->mem_ctx
,
1654 regval_data_p(value
),
1656 if (vk
->data
== NULL
) {
1660 /* go ahead and store the offset....we'll pick this hbin block back up when
1661 we stream the data */
1663 if ((data_hbin
= find_free_space(file
, data_size
)) == NULL
) {
1666 vk
->data_off
= prs_offset( &data_hbin
->ps
) + data_hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1669 /* make sure we don't try to copy from a NULL value pointer */
1671 if ( vk
->data_size
!= 0 )
1672 memcpy( &vk
->data_off
, regval_data_p(value
), sizeof(uint32
) );
1673 vk
->data_size
|= VK_DATA_IN_OFFSET
;
1679 /*******************************************************************
1680 *******************************************************************/
1682 static int hashrec_cmp( REGF_HASH_REC
*h1
, REGF_HASH_REC
*h2
)
1684 return StrCaseCmp( h1
->fullname
, h2
->fullname
);
1687 /*******************************************************************
1688 *******************************************************************/
1690 REGF_NK_REC
* regfio_write_key( REGF_FILE
*file
, const char *name
,
1691 REGVAL_CTR
*values
, REGSUBKEY_CTR
*subkeys
,
1692 SEC_DESC
*sec_desc
, REGF_NK_REC
*parent
)
1695 REGF_HBIN
*vlist_hbin
= NULL
;
1698 if ( !(nk
= TALLOC_ZERO_P( file
->mem_ctx
, REGF_NK_REC
)) )
1701 memcpy( nk
->header
, "nk", REC_HDR_SIZE
);
1704 nk
->key_type
= NK_TYPE_ROOTKEY
;
1706 nk
->key_type
= NK_TYPE_NORMALKEY
;
1708 /* store the parent offset (or -1 if a the root key */
1710 nk
->parent_off
= parent
? (parent
->hbin_off
+ parent
->hbin
->file_off
- REGF_BLOCKSIZE
- HBIN_HDR_SIZE
) : REGF_OFFSET_NONE
;
1712 /* no classname currently */
1714 nk
->classname_off
= REGF_OFFSET_NONE
;
1715 nk
->classname
= NULL
;
1716 nk
->keyname
= talloc_strdup( file
->mem_ctx
, name
);
1718 /* current modification time */
1720 unix_to_nt_time( &nk
->mtime
, time(NULL
) );
1722 /* allocate the record on disk */
1724 size
= nk_record_data_size( nk
);
1725 nk
->rec_size
= ( size
- 1 ) ^ 0XFFFFFFFF;
1726 if ((nk
->hbin
= find_free_space( file
, size
)) == NULL
) {
1729 nk
->hbin_off
= prs_offset( &nk
->hbin
->ps
);
1731 /* Update the hash record in the parent */
1734 REGF_HASH_REC
*hash
= &parent
->subkeys
.hashes
[parent
->subkey_index
];
1736 hash
->nk_off
= prs_offset( &nk
->hbin
->ps
) + nk
->hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1737 memcpy( hash
->keycheck
, name
, sizeof(uint32
) );
1738 hash
->fullname
= talloc_strdup( file
->mem_ctx
, name
);
1739 parent
->subkey_index
++;
1741 /* sort the list by keyname */
1743 qsort( parent
->subkeys
.hashes
, parent
->subkey_index
, sizeof(REGF_HASH_REC
), QSORT_CAST hashrec_cmp
);
1745 if ( !hbin_prs_lf_records( "lf_rec", parent
->subkeys
.hbin
, 0, parent
) )
1749 /* write the security descriptor */
1751 nk
->sk_off
= REGF_OFFSET_NONE
;
1753 uint32 sk_size
= sk_record_data_size( sec_desc
);
1756 /* search for it in the existing list of sd's */
1758 if ( (nk
->sec_desc
= find_sk_record_by_sec_desc( file
, sec_desc
)) == NULL
) {
1759 /* not found so add it to the list */
1761 if (!(sk_hbin
= find_free_space( file
, sk_size
))) {
1765 if ( !(nk
->sec_desc
= TALLOC_ZERO_P( file
->mem_ctx
, REGF_SK_REC
)) )
1768 /* now we have to store the security descriptor in the list and
1769 update the offsets */
1771 memcpy( nk
->sec_desc
->header
, "sk", REC_HDR_SIZE
);
1772 nk
->sec_desc
->hbin
= sk_hbin
;
1773 nk
->sec_desc
->hbin_off
= prs_offset( &sk_hbin
->ps
);
1774 nk
->sec_desc
->sk_off
= prs_offset( &sk_hbin
->ps
) + sk_hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1775 nk
->sec_desc
->rec_size
= (sk_size
-1) ^ 0xFFFFFFFF;
1777 nk
->sec_desc
->sec_desc
= sec_desc
;
1778 nk
->sec_desc
->ref_count
= 0;
1780 /* size value must be self-inclusive */
1781 nk
->sec_desc
->size
= sec_desc_size(sec_desc
) + sizeof(uint32
);
1783 DLIST_ADD_END( file
->sec_desc_list
, nk
->sec_desc
, REGF_SK_REC
*);
1785 /* update the offsets for us and the previous sd in the list.
1786 if this is the first record, then just set the next and prev
1787 offsets to ourself. */
1789 if ( nk
->sec_desc
->prev
) {
1790 REGF_SK_REC
*prev
= nk
->sec_desc
->prev
;
1792 nk
->sec_desc
->prev_sk_off
= prev
->hbin_off
+ prev
->hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1793 prev
->next_sk_off
= nk
->sec_desc
->sk_off
;
1795 /* the end must loop around to the front */
1796 nk
->sec_desc
->next_sk_off
= file
->sec_desc_list
->sk_off
;
1798 /* and first must loop around to the tail */
1799 file
->sec_desc_list
->prev_sk_off
= nk
->sec_desc
->sk_off
;
1801 nk
->sec_desc
->prev_sk_off
= nk
->sec_desc
->sk_off
;
1802 nk
->sec_desc
->next_sk_off
= nk
->sec_desc
->sk_off
;
1806 /* bump the reference count +1 */
1808 nk
->sk_off
= nk
->sec_desc
->sk_off
;
1809 nk
->sec_desc
->ref_count
++;
1812 /* write the subkeys */
1814 nk
->subkeys_off
= REGF_OFFSET_NONE
;
1815 if ( (nk
->num_subkeys
= regsubkey_ctr_numkeys( subkeys
)) != 0 ) {
1816 uint32 lf_size
= lf_record_data_size( nk
->num_subkeys
);
1820 if (!(nk
->subkeys
.hbin
= find_free_space( file
, lf_size
))) {
1823 nk
->subkeys
.hbin_off
= prs_offset( &nk
->subkeys
.hbin
->ps
);
1824 nk
->subkeys
.rec_size
= (lf_size
-1) ^ 0xFFFFFFFF;
1825 nk
->subkeys_off
= prs_offset( &nk
->subkeys
.hbin
->ps
) + nk
->subkeys
.hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1827 memcpy( nk
->subkeys
.header
, "lf", REC_HDR_SIZE
);
1829 nk
->subkeys
.num_keys
= nk
->num_subkeys
;
1830 if (nk
->subkeys
.num_keys
) {
1831 if ( !(nk
->subkeys
.hashes
= TALLOC_ZERO_ARRAY( file
->mem_ctx
, REGF_HASH_REC
, nk
->subkeys
.num_keys
)) )
1834 nk
->subkeys
.hashes
= NULL
;
1836 nk
->subkey_index
= 0;
1838 /* update the max_bytes_subkey{name,classname} fields */
1839 for ( i
=0; i
<nk
->num_subkeys
; i
++ ) {
1840 namelen
= strlen( regsubkey_ctr_specific_key(subkeys
, i
) );
1841 if ( namelen
*2 > nk
->max_bytes_subkeyname
)
1842 nk
->max_bytes_subkeyname
= namelen
* 2;
1846 /* write the values */
1848 nk
->values_off
= REGF_OFFSET_NONE
;
1849 if ( (nk
->num_values
= regval_ctr_numvals( values
)) != 0 ) {
1850 uint32 vlist_size
= ( ( nk
->num_values
* sizeof(uint32
) ) & 0xfffffff8 ) + 8;
1853 if (!(vlist_hbin
= find_free_space( file
, vlist_size
))) {
1856 nk
->values_off
= prs_offset( &vlist_hbin
->ps
) + vlist_hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1858 if (nk
->num_values
) {
1859 if ( !(nk
->values
= TALLOC_ARRAY( file
->mem_ctx
, REGF_VK_REC
, nk
->num_values
)) )
1865 /* create the vk records */
1867 for ( i
=0; i
<nk
->num_values
; i
++ ) {
1868 uint32 vk_size
, namelen
, datalen
;
1871 r
= regval_ctr_specific_value( values
, i
);
1872 create_vk_record( file
, &nk
->values
[i
], r
);
1873 vk_size
= vk_record_data_size( &nk
->values
[i
] );
1874 nk
->values
[i
].hbin
= find_free_space( file
, vk_size
);
1875 nk
->values
[i
].hbin_off
= prs_offset( &nk
->values
[i
].hbin
->ps
);
1876 nk
->values
[i
].rec_size
= ( vk_size
- 1 ) ^ 0xFFFFFFFF;
1877 nk
->values
[i
].rec_off
= prs_offset( &nk
->values
[i
].hbin
->ps
)
1878 + nk
->values
[i
].hbin
->first_hbin_off
1881 /* update the max bytes fields if necessary */
1883 namelen
= strlen( regval_name(r
) );
1884 if ( namelen
*2 > nk
->max_bytes_valuename
)
1885 nk
->max_bytes_valuename
= namelen
* 2;
1887 datalen
= regval_size( r
);
1888 if ( datalen
> nk
->max_bytes_value
)
1889 nk
->max_bytes_value
= datalen
;
1893 /* stream the records */
1895 prs_set_offset( &nk
->hbin
->ps
, nk
->hbin_off
);
1896 if ( !prs_nk_rec( "nk_rec", &nk
->hbin
->ps
, 0, nk
) )
1899 if ( nk
->num_values
) {
1900 if ( !hbin_prs_vk_records( "vk_records", vlist_hbin
, 0, nk
, file
) )
1905 regfio_flush( file
);