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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "reg_objects.h"
23 #include "../librpc/gen_ndr/ndr_security.h"
26 #define DBGC_CLASS DBGC_REGISTRY
28 /*******************************************************************
30 * TODO : Right now this code basically ignores classnames.
32 ******************************************************************/
35 /*******************************************************************
36 *******************************************************************/
38 static int write_block( REGF_FILE
*file
, prs_struct
*ps
, uint32 offset
)
40 int bytes_written
, returned
;
41 char *buffer
= prs_data_p( ps
);
42 uint32 buffer_size
= prs_data_size( ps
);
48 /* check for end of file */
50 if (sys_fstat(file
->fd
, &sbuf
, false)) {
51 DEBUG(0,("write_block: stat() failed! (%s)\n", strerror(errno
)));
55 if ( lseek( file
->fd
, offset
, SEEK_SET
) == -1 ) {
56 DEBUG(0,("write_block: lseek() failed! (%s)\n", strerror(errno
) ));
60 bytes_written
= returned
= 0;
61 while ( bytes_written
< buffer_size
) {
62 if ( (returned
= write( file
->fd
, buffer
+bytes_written
, buffer_size
-bytes_written
)) == -1 ) {
63 DEBUG(0,("write_block: write() failed! (%s)\n", strerror(errno
) ));
67 bytes_written
+= returned
;
73 /*******************************************************************
74 *******************************************************************/
76 static int read_block( REGF_FILE
*file
, prs_struct
*ps
, uint32 file_offset
, uint32 block_size
)
78 int bytes_read
, returned
;
82 /* check for end of file */
84 if (sys_fstat(file
->fd
, &sbuf
, false)) {
85 DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno
)));
89 if ( (size_t)file_offset
>= sbuf
.st_ex_size
)
92 /* if block_size == 0, we are parsing HBIN records and need
93 to read some of the header to get the block_size from there */
95 if ( block_size
== 0 ) {
98 if ( lseek( file
->fd
, file_offset
, SEEK_SET
) == -1 ) {
99 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno
) ));
103 returned
= read( file
->fd
, hdr
, 0x20 );
104 if ( (returned
== -1) || (returned
< 0x20) ) {
105 DEBUG(0,("read_block: failed to read in HBIN header. Is the file corrupt?\n"));
109 /* make sure this is an hbin header */
111 if ( strncmp( hdr
, "hbin", HBIN_HDR_SIZE
) != 0 ) {
112 DEBUG(0,("read_block: invalid block header!\n"));
116 block_size
= IVAL( hdr
, 0x08 );
119 DEBUG(10,("read_block: block_size == 0x%x\n", block_size
));
121 /* set the offset, initialize the buffer, and read the block from disk */
123 if ( lseek( file
->fd
, file_offset
, SEEK_SET
) == -1 ) {
124 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno
) ));
128 if (!prs_init( ps
, block_size
, file
->mem_ctx
, UNMARSHALL
)) {
129 DEBUG(0,("read_block: prs_init() failed! (%s)\n", strerror(errno
) ));
132 buffer
= prs_data_p( ps
);
133 bytes_read
= returned
= 0;
135 while ( bytes_read
< block_size
) {
136 if ( (returned
= read( file
->fd
, buffer
+bytes_read
, block_size
-bytes_read
)) == -1 ) {
137 DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno
) ));
140 if ( (returned
== 0) && (bytes_read
< block_size
) ) {
141 DEBUG(0,("read_block: not a vald registry file ?\n" ));
145 bytes_read
+= returned
;
151 /*******************************************************************
152 *******************************************************************/
154 static bool write_hbin_block( REGF_FILE
*file
, REGF_HBIN
*hbin
)
159 /* write free space record if any is available */
161 if ( hbin
->free_off
!= REGF_OFFSET_NONE
) {
162 uint32 header
= 0xffffffff;
164 if ( !prs_set_offset( &hbin
->ps
, hbin
->free_off
-sizeof(uint32
) ) )
166 if ( !prs_uint32( "free_size", &hbin
->ps
, 0, &hbin
->free_size
) )
168 if ( !prs_uint32( "free_header", &hbin
->ps
, 0, &header
) )
172 hbin
->dirty
= (write_block( file
, &hbin
->ps
, hbin
->file_off
) != -1);
177 /*******************************************************************
178 *******************************************************************/
180 static bool hbin_block_close( REGF_FILE
*file
, REGF_HBIN
*hbin
)
184 /* remove the block from the open list and flush it to disk */
186 for ( p
=file
->block_list
; p
&& p
!=hbin
; p
=p
->next
)
190 DLIST_REMOVE( file
->block_list
, hbin
);
193 DEBUG(0,("hbin_block_close: block not in open list!\n"));
195 if ( !write_hbin_block( file
, hbin
) )
201 /*******************************************************************
202 *******************************************************************/
204 static bool prs_regf_block( const char *desc
, prs_struct
*ps
, int depth
, REGF_FILE
*file
)
206 prs_debug(ps
, depth
, desc
, "prs_regf_block");
209 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8
*)file
->header
, sizeof( file
->header
)) )
212 /* yes, these values are always identical so store them only once */
214 if ( !prs_uint32( "unknown1", ps
, depth
, &file
->unknown1
))
216 if ( !prs_uint32( "unknown1 (again)", ps
, depth
, &file
->unknown1
))
219 /* get the modtime */
221 if ( !prs_set_offset( ps
, 0x0c ) )
223 if ( !smb_io_time( "modtime", &file
->mtime
, ps
, depth
) )
228 if ( !prs_uint32( "unknown2", ps
, depth
, &file
->unknown2
))
230 if ( !prs_uint32( "unknown3", ps
, depth
, &file
->unknown3
))
232 if ( !prs_uint32( "unknown4", ps
, depth
, &file
->unknown4
))
234 if ( !prs_uint32( "unknown5", ps
, depth
, &file
->unknown5
))
237 /* get file offsets */
239 if ( !prs_set_offset( ps
, 0x24 ) )
241 if ( !prs_uint32( "data_offset", ps
, depth
, &file
->data_offset
))
243 if ( !prs_uint32( "last_block", ps
, depth
, &file
->last_block
))
246 /* one more constant */
248 if ( !prs_uint32( "unknown6", ps
, depth
, &file
->unknown6
))
251 /* get the checksum */
253 if ( !prs_set_offset( ps
, 0x01fc ) )
255 if ( !prs_uint32( "checksum", ps
, depth
, &file
->checksum
))
261 /*******************************************************************
262 *******************************************************************/
264 static bool prs_hbin_block( const char *desc
, prs_struct
*ps
, int depth
, REGF_HBIN
*hbin
)
268 prs_debug(ps
, depth
, desc
, "prs_regf_block");
271 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8
*)hbin
->header
, sizeof( hbin
->header
)) )
274 if ( !prs_uint32( "first_hbin_off", ps
, depth
, &hbin
->first_hbin_off
))
277 /* The dosreg.cpp comments say that the block size is at 0x1c.
278 According to a WINXP NTUSER.dat file, this is wrong. The block_size
281 if ( !prs_uint32( "block_size", ps
, depth
, &hbin
->block_size
))
284 block_size2
= hbin
->block_size
;
285 prs_set_offset( ps
, 0x1c );
286 if ( !prs_uint32( "block_size2", ps
, depth
, &block_size2
))
289 if ( MARSHALLING(ps
) )
296 /*******************************************************************
297 *******************************************************************/
299 static bool prs_nk_rec( const char *desc
, prs_struct
*ps
, int depth
, REGF_NK_REC
*nk
)
301 uint16 class_length
, name_length
;
303 uint32 data_size
, start_off
, end_off
;
304 uint32 unknown_off
= REGF_OFFSET_NONE
;
306 nk
->hbin_off
= prs_offset( ps
);
307 start
= nk
->hbin_off
;
309 prs_debug(ps
, depth
, desc
, "prs_nk_rec");
312 /* back up and get the data_size */
314 if ( !prs_set_offset( ps
, prs_offset(ps
)-sizeof(uint32
)) )
316 start_off
= prs_offset( ps
);
317 if ( !prs_uint32( "rec_size", ps
, depth
, &nk
->rec_size
))
320 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8
*)nk
->header
, sizeof( nk
->header
)) )
323 if ( !prs_uint16( "key_type", ps
, depth
, &nk
->key_type
))
325 if ( !smb_io_time( "mtime", &nk
->mtime
, ps
, depth
))
328 if ( !prs_set_offset( ps
, start
+0x0010 ) )
330 if ( !prs_uint32( "parent_off", ps
, depth
, &nk
->parent_off
))
332 if ( !prs_uint32( "num_subkeys", ps
, depth
, &nk
->num_subkeys
))
335 if ( !prs_set_offset( ps
, start
+0x001c ) )
337 if ( !prs_uint32( "subkeys_off", ps
, depth
, &nk
->subkeys_off
))
339 if ( !prs_uint32( "unknown_off", ps
, depth
, &unknown_off
) )
342 if ( !prs_set_offset( ps
, start
+0x0024 ) )
344 if ( !prs_uint32( "num_values", ps
, depth
, &nk
->num_values
))
346 if ( !prs_uint32( "values_off", ps
, depth
, &nk
->values_off
))
348 if ( !prs_uint32( "sk_off", ps
, depth
, &nk
->sk_off
))
350 if ( !prs_uint32( "classname_off", ps
, depth
, &nk
->classname_off
))
353 if ( !prs_uint32( "max_bytes_subkeyname", ps
, depth
, &nk
->max_bytes_subkeyname
))
355 if ( !prs_uint32( "max_bytes_subkeyclassname", ps
, depth
, &nk
->max_bytes_subkeyclassname
))
357 if ( !prs_uint32( "max_bytes_valuename", ps
, depth
, &nk
->max_bytes_valuename
))
359 if ( !prs_uint32( "max_bytes_value", ps
, depth
, &nk
->max_bytes_value
))
361 if ( !prs_uint32( "unknown index", ps
, depth
, &nk
->unk_index
))
364 name_length
= nk
->keyname
? strlen(nk
->keyname
) : 0 ;
365 class_length
= nk
->classname
? strlen(nk
->classname
) : 0 ;
366 if ( !prs_uint16( "name_length", ps
, depth
, &name_length
))
368 if ( !prs_uint16( "class_length", ps
, depth
, &class_length
))
371 if ( class_length
) {
376 if ( UNMARSHALLING(ps
) ) {
377 if ( !(nk
->keyname
= PRS_ALLOC_MEM( ps
, char, name_length
+1 )) )
381 if ( !prs_uint8s( True
, "name", ps
, depth
, (uint8
*)nk
->keyname
, name_length
) )
384 if ( UNMARSHALLING(ps
) )
385 nk
->keyname
[name_length
] = '\0';
388 end_off
= prs_offset( ps
);
390 /* data_size must be divisible by 8 and large enough to hold the original record */
392 data_size
= ((start_off
- end_off
) & 0xfffffff8 );
393 if ( data_size
> nk
->rec_size
)
394 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size
, nk
->rec_size
));
396 if ( MARSHALLING(ps
) )
397 nk
->hbin
->dirty
= True
;
402 /*******************************************************************
403 *******************************************************************/
405 static uint32
regf_block_checksum( prs_struct
*ps
)
407 char *buffer
= prs_data_p( ps
);
411 /* XOR of all bytes 0x0000 - 0x01FB */
415 for ( i
=0; i
<0x01FB; i
+=4 ) {
416 x
= IVAL(buffer
, i
);
423 /*******************************************************************
424 *******************************************************************/
426 static bool read_regf_block( REGF_FILE
*file
)
431 /* grab the first block from the file */
433 if ( read_block( file
, &ps
, 0, REGF_BLOCKSIZE
) == -1 )
436 /* parse the block and verify the checksum */
438 if ( !prs_regf_block( "regf_header", &ps
, 0, file
) )
441 checksum
= regf_block_checksum( &ps
);
445 if ( file
->checksum
!= checksum
) {
446 DEBUG(0,("read_regf_block: invalid checksum\n" ));
453 /*******************************************************************
454 *******************************************************************/
456 static REGF_HBIN
* read_hbin_block( REGF_FILE
*file
, off_t offset
)
459 uint32 record_size
, curr_off
, block_size
, header
;
461 if ( !(hbin
= TALLOC_ZERO_P(file
->mem_ctx
, REGF_HBIN
)) )
463 hbin
->file_off
= offset
;
466 if ( read_block( file
, &hbin
->ps
, offset
, 0 ) == -1 )
469 if ( !prs_hbin_block( "hbin", &hbin
->ps
, 0, hbin
) )
472 /* this should be the same thing as hbin->block_size but just in case */
474 block_size
= prs_data_size( &hbin
->ps
);
476 /* Find the available free space offset. Always at the end,
477 so walk the record list and stop when you get to the end.
478 The end is defined by a record header of 0xffffffff. The
479 previous 4 bytes contains the amount of free space remaining
480 in the hbin block. */
482 /* remember that the record_size is in the 4 bytes preceeding the record itself */
484 if ( !prs_set_offset( &hbin
->ps
, file
->data_offset
+HBIN_HDR_SIZE
-sizeof(uint32
) ) )
489 curr_off
= prs_offset( &hbin
->ps
);
490 while ( header
!= 0xffffffff ) {
491 /* not done yet so reset the current offset to the
492 next record_size field */
494 curr_off
= curr_off
+record_size
;
496 /* for some reason the record_size of the last record in
497 an hbin block can extend past the end of the block
498 even though the record fits within the remaining
499 space....aaarrrgggghhhhhh */
501 if ( curr_off
>= block_size
) {
507 if ( !prs_set_offset( &hbin
->ps
, curr_off
) )
510 if ( !prs_uint32( "rec_size", &hbin
->ps
, 0, &record_size
) )
512 if ( !prs_uint32( "header", &hbin
->ps
, 0, &header
) )
515 SMB_ASSERT( record_size
!= 0 );
517 if ( record_size
& 0x80000000 ) {
518 /* absolute_value(record_size) */
519 record_size
= (record_size
^ 0xffffffff) + 1;
523 /* save the free space offset */
525 if ( header
== 0xffffffff ) {
527 /* account for the fact that the curr_off is 4 bytes behind the actual
530 hbin
->free_off
= curr_off
+ sizeof(uint32
);
531 hbin
->free_size
= record_size
;
534 DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin
->free_off
));
536 if ( !prs_set_offset( &hbin
->ps
, file
->data_offset
+HBIN_HDR_SIZE
) )
542 /*******************************************************************
543 Input a random offset and receive the corresponding HBIN
545 *******************************************************************/
547 static bool hbin_contains_offset( REGF_HBIN
*hbin
, uint32 offset
)
552 if ( (offset
> hbin
->first_hbin_off
) && (offset
< (hbin
->first_hbin_off
+hbin
->block_size
)) )
558 /*******************************************************************
559 Input a random offset and receive the corresponding HBIN
561 *******************************************************************/
563 static REGF_HBIN
* lookup_hbin_block( REGF_FILE
*file
, uint32 offset
)
565 REGF_HBIN
*hbin
= NULL
;
568 /* start with the open list */
570 for ( hbin
=file
->block_list
; hbin
; hbin
=hbin
->next
) {
571 DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%lx]\n", hbin
->file_off
, (unsigned long)hbin
));
572 if ( hbin_contains_offset( hbin
, offset
) )
577 /* start at the beginning */
579 block_off
= REGF_BLOCKSIZE
;
581 /* cleanup before the next round */
583 prs_mem_free( &hbin
->ps
);
585 hbin
= read_hbin_block( file
, block_off
);
588 block_off
= hbin
->file_off
+ hbin
->block_size
;
590 } while ( hbin
&& !hbin_contains_offset( hbin
, offset
) );
594 DLIST_ADD( file
->block_list
, hbin
);
599 /*******************************************************************
600 *******************************************************************/
602 static bool prs_hash_rec( const char *desc
, prs_struct
*ps
, int depth
, REGF_HASH_REC
*hash
)
604 prs_debug(ps
, depth
, desc
, "prs_hash_rec");
607 if ( !prs_uint32( "nk_off", ps
, depth
, &hash
->nk_off
))
609 if ( !prs_uint8s( True
, "keycheck", ps
, depth
, hash
->keycheck
, sizeof( hash
->keycheck
)) )
615 /*******************************************************************
616 *******************************************************************/
618 static bool hbin_prs_lf_records( const char *desc
, REGF_HBIN
*hbin
, int depth
, REGF_NK_REC
*nk
)
621 REGF_LF_REC
*lf
= &nk
->subkeys
;
622 uint32 data_size
, start_off
, end_off
;
624 prs_debug(&hbin
->ps
, depth
, desc
, "prs_lf_records");
627 /* check if we have anything to do first */
629 if ( nk
->num_subkeys
== 0 )
632 /* move to the LF record */
634 if ( !prs_set_offset( &hbin
->ps
, nk
->subkeys_off
+ HBIN_HDR_SIZE
- hbin
->first_hbin_off
) )
637 /* backup and get the data_size */
639 if ( !prs_set_offset( &hbin
->ps
, prs_offset(&hbin
->ps
)-sizeof(uint32
)) )
641 start_off
= prs_offset( &hbin
->ps
);
642 if ( !prs_uint32( "rec_size", &hbin
->ps
, depth
, &lf
->rec_size
))
645 if ( !prs_uint8s( True
, "header", &hbin
->ps
, depth
, (uint8
*)lf
->header
, sizeof( lf
->header
)) )
648 if ( !prs_uint16( "num_keys", &hbin
->ps
, depth
, &lf
->num_keys
))
651 if ( UNMARSHALLING(&hbin
->ps
) ) {
653 if ( !(lf
->hashes
= PRS_ALLOC_MEM( &hbin
->ps
, REGF_HASH_REC
, lf
->num_keys
)) )
660 for ( i
=0; i
<lf
->num_keys
; i
++ ) {
661 if ( !prs_hash_rec( "hash_rec", &hbin
->ps
, depth
, &lf
->hashes
[i
] ) )
665 end_off
= prs_offset( &hbin
->ps
);
667 /* data_size must be divisible by 8 and large enough to hold the original record */
669 data_size
= ((start_off
- end_off
) & 0xfffffff8 );
670 if ( data_size
> lf
->rec_size
)
671 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size
, lf
->rec_size
));
673 if ( MARSHALLING(&hbin
->ps
) )
679 /*******************************************************************
680 *******************************************************************/
682 static bool hbin_prs_sk_rec( const char *desc
, REGF_HBIN
*hbin
, int depth
, REGF_SK_REC
*sk
)
684 prs_struct
*ps
= &hbin
->ps
;
686 uint32 data_size
, start_off
, end_off
;
689 prs_debug(ps
, depth
, desc
, "hbin_prs_sk_rec");
692 if ( !prs_set_offset( &hbin
->ps
, sk
->sk_off
+ HBIN_HDR_SIZE
- hbin
->first_hbin_off
) )
695 /* backup and get the data_size */
697 if ( !prs_set_offset( &hbin
->ps
, prs_offset(&hbin
->ps
)-sizeof(uint32
)) )
699 start_off
= prs_offset( &hbin
->ps
);
700 if ( !prs_uint32( "rec_size", &hbin
->ps
, depth
, &sk
->rec_size
))
703 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8
*)sk
->header
, sizeof( sk
->header
)) )
705 if ( !prs_uint16( "tag", ps
, depth
, &tag
))
708 if ( !prs_uint32( "prev_sk_off", ps
, depth
, &sk
->prev_sk_off
))
710 if ( !prs_uint32( "next_sk_off", ps
, depth
, &sk
->next_sk_off
))
712 if ( !prs_uint32( "ref_count", ps
, depth
, &sk
->ref_count
))
714 if ( !prs_uint32( "size", ps
, depth
, &sk
->size
))
719 TALLOC_CTX
*mem_ctx
= prs_get_mem_context(&hbin
->ps
);
722 if (MARSHALLING(&hbin
->ps
)) {
723 status
= marshall_sec_desc(mem_ctx
,
725 &blob
.data
, &blob
.length
);
726 if (!NT_STATUS_IS_OK(status
))
728 if (!prs_copy_data_in(&hbin
->ps
, (const char *)blob
.data
, blob
.length
))
731 blob
= data_blob_const(prs_data_p(&hbin
->ps
),
732 prs_data_size(&hbin
->ps
));
733 status
= unmarshall_sec_desc(mem_ctx
,
734 blob
.data
, blob
.length
,
736 if (!NT_STATUS_IS_OK(status
))
738 prs_set_offset(&hbin
->ps
, blob
.length
);
742 end_off
= prs_offset( &hbin
->ps
);
744 /* data_size must be divisible by 8 and large enough to hold the original record */
746 data_size
= ((start_off
- end_off
) & 0xfffffff8 );
747 if ( data_size
> sk
->rec_size
)
748 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size
, sk
->rec_size
));
750 if ( MARSHALLING(&hbin
->ps
) )
756 /*******************************************************************
757 *******************************************************************/
759 static bool hbin_prs_vk_rec( const char *desc
, REGF_HBIN
*hbin
, int depth
, REGF_VK_REC
*vk
, REGF_FILE
*file
)
763 prs_struct
*ps
= &hbin
->ps
;
764 uint32 data_size
, start_off
, end_off
;
766 prs_debug(ps
, depth
, desc
, "prs_vk_rec");
769 /* backup and get the data_size */
771 if ( !prs_set_offset( &hbin
->ps
, prs_offset(&hbin
->ps
)-sizeof(uint32
)) )
773 start_off
= prs_offset( &hbin
->ps
);
774 if ( !prs_uint32( "rec_size", &hbin
->ps
, depth
, &vk
->rec_size
))
777 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8
*)vk
->header
, sizeof( vk
->header
)) )
780 if ( MARSHALLING(&hbin
->ps
) )
781 name_length
= strlen(vk
->valuename
);
783 if ( !prs_uint16( "name_length", ps
, depth
, &name_length
))
785 if ( !prs_uint32( "data_size", ps
, depth
, &vk
->data_size
))
787 if ( !prs_uint32( "data_off", ps
, depth
, &vk
->data_off
))
789 if ( !prs_uint32( "type", ps
, depth
, &vk
->type
))
791 if ( !prs_uint16( "flag", ps
, depth
, &vk
->flag
))
794 offset
= prs_offset( ps
);
795 offset
+= 2; /* skip 2 bytes */
796 prs_set_offset( ps
, offset
);
800 if ( vk
->flag
&VK_FLAG_NAME_PRESENT
) {
802 if ( UNMARSHALLING(&hbin
->ps
) ) {
803 if ( !(vk
->valuename
= PRS_ALLOC_MEM( ps
, char, name_length
+1 )))
806 if ( !prs_uint8s( True
, "name", ps
, depth
, (uint8
*)vk
->valuename
, name_length
) )
810 end_off
= prs_offset( &hbin
->ps
);
812 /* get the data if necessary */
814 if ( vk
->data_size
!= 0 ) {
815 bool charmode
= False
;
817 if ( (vk
->type
== REG_SZ
) || (vk
->type
== REG_MULTI_SZ
) )
820 /* the data is stored in the offset if the size <= 4 */
822 if ( !(vk
->data_size
& VK_DATA_IN_OFFSET
) ) {
823 REGF_HBIN
*hblock
= hbin
;
824 uint32 data_rec_size
;
826 if ( UNMARSHALLING(&hbin
->ps
) ) {
827 if ( !(vk
->data
= PRS_ALLOC_MEM( ps
, uint8
, vk
->data_size
) ) )
831 /* this data can be in another hbin */
832 if ( !hbin_contains_offset( hbin
, vk
->data_off
) ) {
833 if ( !(hblock
= lookup_hbin_block( file
, vk
->data_off
)) )
836 if ( !(prs_set_offset( &hblock
->ps
, (vk
->data_off
+HBIN_HDR_SIZE
-hblock
->first_hbin_off
)-sizeof(uint32
) )) )
839 if ( MARSHALLING(&hblock
->ps
) ) {
840 data_rec_size
= ( (vk
->data_size
+sizeof(uint32
)) & 0xfffffff8 ) + 8;
841 data_rec_size
= ( data_rec_size
- 1 ) ^ 0xFFFFFFFF;
843 if ( !prs_uint32( "data_rec_size", &hblock
->ps
, depth
, &data_rec_size
))
845 if ( !prs_uint8s( charmode
, "data", &hblock
->ps
, depth
, vk
->data
, vk
->data_size
) )
848 if ( MARSHALLING(&hblock
->ps
) )
849 hblock
->dirty
= True
;
852 if ( !(vk
->data
= PRS_ALLOC_MEM( ps
, uint8
, 4 ) ) )
854 SIVAL( vk
->data
, 0, vk
->data_off
);
859 /* data_size must be divisible by 8 and large enough to hold the original record */
861 data_size
= ((start_off
- end_off
) & 0xfffffff8 );
862 if ( data_size
!= vk
->rec_size
)
863 DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size
, vk
->rec_size
));
865 if ( MARSHALLING(&hbin
->ps
) )
871 /*******************************************************************
872 read a VK record which is contained in the HBIN block stored
873 in the prs_struct *ps.
874 *******************************************************************/
876 static bool hbin_prs_vk_records( const char *desc
, REGF_HBIN
*hbin
, int depth
, REGF_NK_REC
*nk
, REGF_FILE
*file
)
881 prs_debug(&hbin
->ps
, depth
, desc
, "prs_vk_records");
884 /* check if we have anything to do first */
886 if ( nk
->num_values
== 0 )
889 if ( UNMARSHALLING(&hbin
->ps
) ) {
890 if ( !(nk
->values
= PRS_ALLOC_MEM( &hbin
->ps
, REGF_VK_REC
, nk
->num_values
) ) )
894 /* convert the offset to something relative to this HBIN block */
896 if ( !prs_set_offset( &hbin
->ps
, nk
->values_off
+HBIN_HDR_SIZE
-hbin
->first_hbin_off
-sizeof(uint32
)) )
899 if ( MARSHALLING( &hbin
->ps
) ) {
900 record_size
= ( ( nk
->num_values
* sizeof(uint32
) ) & 0xfffffff8 ) + 8;
901 record_size
= (record_size
- 1) ^ 0xFFFFFFFF;
904 if ( !prs_uint32( "record_size", &hbin
->ps
, depth
, &record_size
) )
907 for ( i
=0; i
<nk
->num_values
; i
++ ) {
908 if ( !prs_uint32( "vk_off", &hbin
->ps
, depth
, &nk
->values
[i
].rec_off
) )
912 for ( i
=0; i
<nk
->num_values
; i
++ ) {
913 REGF_HBIN
*sub_hbin
= hbin
;
916 if ( !hbin_contains_offset( hbin
, nk
->values
[i
].rec_off
) ) {
917 sub_hbin
= lookup_hbin_block( file
, nk
->values
[i
].rec_off
);
919 DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n",
920 nk
->values
[i
].hbin_off
));
925 new_offset
= nk
->values
[i
].rec_off
+ HBIN_HDR_SIZE
- sub_hbin
->first_hbin_off
;
926 if ( !prs_set_offset( &sub_hbin
->ps
, new_offset
) )
928 if ( !hbin_prs_vk_rec( "vk_rec", sub_hbin
, depth
, &nk
->values
[i
], file
) )
932 if ( MARSHALLING(&hbin
->ps
) )
940 /*******************************************************************
941 *******************************************************************/
943 static REGF_SK_REC
* find_sk_record_by_offset( REGF_FILE
*file
, uint32 offset
)
947 for ( p_sk
=file
->sec_desc_list
; p_sk
; p_sk
=p_sk
->next
) {
948 if ( p_sk
->sk_off
== offset
)
955 /*******************************************************************
956 *******************************************************************/
958 static REGF_SK_REC
* find_sk_record_by_sec_desc( REGF_FILE
*file
, struct security_descriptor
*sd
)
962 for ( p
=file
->sec_desc_list
; p
; p
=p
->next
) {
963 if ( security_descriptor_equal( p
->sec_desc
, sd
) )
972 /*******************************************************************
973 *******************************************************************/
975 static bool hbin_prs_key( REGF_FILE
*file
, REGF_HBIN
*hbin
, REGF_NK_REC
*nk
)
980 prs_debug(&hbin
->ps
, depth
, "", "fetch_key");
983 /* get the initial nk record */
985 if ( !prs_nk_rec( "nk_rec", &hbin
->ps
, depth
, nk
))
990 if ( nk
->num_values
&& (nk
->values_off
!=REGF_OFFSET_NONE
) ) {
992 if ( !hbin_contains_offset( hbin
, nk
->values_off
) ) {
993 sub_hbin
= lookup_hbin_block( file
, nk
->values_off
);
995 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n",
1001 if ( !hbin_prs_vk_records( "vk_rec", sub_hbin
, depth
, nk
, file
))
1005 /* now get subkeys */
1007 if ( nk
->num_subkeys
&& (nk
->subkeys_off
!=REGF_OFFSET_NONE
) ) {
1009 if ( !hbin_contains_offset( hbin
, nk
->subkeys_off
) ) {
1010 sub_hbin
= lookup_hbin_block( file
, nk
->subkeys_off
);
1012 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n",
1018 if ( !hbin_prs_lf_records( "lf_rec", sub_hbin
, depth
, nk
))
1022 /* get the to the security descriptor. First look if we have already parsed it */
1024 if ( (nk
->sk_off
!=REGF_OFFSET_NONE
) && !( nk
->sec_desc
= find_sk_record_by_offset( file
, nk
->sk_off
)) ) {
1027 if ( !hbin_contains_offset( hbin
, nk
->sk_off
) ) {
1028 sub_hbin
= lookup_hbin_block( file
, nk
->sk_off
);
1030 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_offset [0x%x]\n",
1036 if ( !(nk
->sec_desc
= TALLOC_ZERO_P( file
->mem_ctx
, REGF_SK_REC
)) )
1038 nk
->sec_desc
->sk_off
= nk
->sk_off
;
1039 if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin
, depth
, nk
->sec_desc
))
1042 /* add to the list of security descriptors (ref_count has been read from the files) */
1044 nk
->sec_desc
->sk_off
= nk
->sk_off
;
1045 DLIST_ADD( file
->sec_desc_list
, nk
->sec_desc
);
1051 /*******************************************************************
1052 *******************************************************************/
1054 static bool next_record( REGF_HBIN
*hbin
, const char *hdr
, bool *eob
)
1056 uint8 header
[REC_HDR_SIZE
];
1058 uint32 curr_off
, block_size
;
1060 prs_struct
*ps
= &hbin
->ps
;
1062 curr_off
= prs_offset( ps
);
1063 if ( curr_off
== 0 )
1064 prs_set_offset( ps
, HBIN_HEADER_REC_SIZE
);
1066 /* assume that the current offset is at the record header
1067 and we need to backup to read the record size */
1069 curr_off
-= sizeof(uint32
);
1071 block_size
= prs_data_size( ps
);
1073 memset( header
, 0x0, sizeof(uint8
)*REC_HDR_SIZE
);
1076 curr_off
= curr_off
+record_size
;
1077 if ( curr_off
>= block_size
)
1080 if ( !prs_set_offset( &hbin
->ps
, curr_off
) )
1083 if ( !prs_uint32( "record_size", ps
, 0, &record_size
) )
1085 if ( !prs_uint8s( True
, "header", ps
, 0, header
, REC_HDR_SIZE
) )
1088 if ( record_size
& 0x80000000 ) {
1089 /* absolute_value(record_size) */
1090 record_size
= (record_size
^ 0xffffffff) + 1;
1093 if ( memcmp( header
, hdr
, REC_HDR_SIZE
) == 0 ) {
1095 curr_off
+= sizeof(uint32
);
1099 /* mark prs_struct as done ( at end ) if no more SK records */
1100 /* mark end-of-block as True */
1103 prs_set_offset( &hbin
->ps
, prs_data_size(&hbin
->ps
) );
1108 if ( !prs_set_offset( ps
, curr_off
) )
1114 /*******************************************************************
1115 *******************************************************************/
1117 static bool next_nk_record( REGF_FILE
*file
, REGF_HBIN
*hbin
, REGF_NK_REC
*nk
, bool *eob
)
1119 if ( next_record( hbin
, "nk", eob
) && hbin_prs_key( file
, hbin
, nk
) )
1125 /*******************************************************************
1126 Intialize the newly created REGF_BLOCK in *file and write the
1127 block header to disk
1128 *******************************************************************/
1130 static bool init_regf_block( REGF_FILE
*file
)
1135 if ( !prs_init( &ps
, REGF_BLOCKSIZE
, file
->mem_ctx
, MARSHALL
) )
1138 memcpy( file
->header
, "regf", REGF_HDR_SIZE
);
1139 file
->data_offset
= 0x20;
1140 file
->last_block
= 0x1000;
1144 unix_to_nt_time( &file
->mtime
, time(NULL
) );
1146 /* hard coded values...no diea what these are ... maybe in time */
1148 file
->unknown1
= 0x2;
1149 file
->unknown2
= 0x1;
1150 file
->unknown3
= 0x3;
1151 file
->unknown4
= 0x0;
1152 file
->unknown5
= 0x1;
1153 file
->unknown6
= 0x1;
1155 /* write header to the buffer */
1157 if ( !prs_regf_block( "regf_header", &ps
, 0, file
) ) {
1162 /* calculate the checksum, re-marshall data (to include the checksum)
1163 and write to disk */
1165 file
->checksum
= regf_block_checksum( &ps
);
1166 prs_set_offset( &ps
, 0 );
1167 if ( !prs_regf_block( "regf_header", &ps
, 0, file
) ) {
1172 if ( write_block( file
, &ps
, 0 ) == -1 ) {
1173 DEBUG(0,("init_regf_block: Failed to initialize registry header block!\n"));
1179 prs_mem_free( &ps
);
1183 /*******************************************************************
1184 Open the registry file and then read in the REGF block to get the
1186 *******************************************************************/
1188 REGF_FILE
* regfio_open( const char *filename
, int flags
, int mode
)
1192 if ( !(rb
= SMB_MALLOC_P(REGF_FILE
)) ) {
1193 DEBUG(0,("ERROR allocating memory\n"));
1199 if ( !(rb
->mem_ctx
= talloc_init( "read_regf_block" )) ) {
1204 rb
->open_flags
= flags
;
1206 /* open and existing file */
1208 if ( (rb
->fd
= open(filename
, flags
, mode
)) == -1 ) {
1209 DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename
, strerror(errno
)));
1214 /* check if we are creating a new file or overwriting an existing one */
1216 if ( flags
& (O_CREAT
|O_TRUNC
) ) {
1217 if ( !init_regf_block( rb
) ) {
1218 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1227 /* read in an existing file */
1229 if ( !read_regf_block( rb
) ) {
1230 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1240 /*******************************************************************
1241 *******************************************************************/
1243 static void regfio_mem_free( REGF_FILE
*file
)
1245 /* free any talloc()'d memory */
1247 if ( file
&& file
->mem_ctx
)
1248 talloc_destroy( file
->mem_ctx
);
1251 /*******************************************************************
1252 *******************************************************************/
1254 int regfio_close( REGF_FILE
*file
)
1258 /* cleanup for a file opened for write */
1260 if ((file
->fd
!= -1) && (file
->open_flags
& (O_WRONLY
|O_RDWR
))) {
1264 /* write of sd list */
1266 for ( sk
=file
->sec_desc_list
; sk
; sk
=sk
->next
) {
1267 hbin_prs_sk_rec( "sk_rec", sk
->hbin
, 0, sk
);
1270 /* flush any dirty blocks */
1272 while ( file
->block_list
) {
1273 hbin_block_close( file
, file
->block_list
);
1278 unix_to_nt_time( &file
->mtime
, time(NULL
) );
1280 if ( read_block( file
, &ps
, 0, REGF_BLOCKSIZE
) != -1 ) {
1281 /* now use for writing */
1282 prs_switch_type( &ps
, MARSHALL
);
1284 /* stream the block once, generate the checksum,
1285 and stream it again */
1286 prs_set_offset( &ps
, 0 );
1287 prs_regf_block( "regf_blocK", &ps
, 0, file
);
1288 file
->checksum
= regf_block_checksum( &ps
);
1289 prs_set_offset( &ps
, 0 );
1290 prs_regf_block( "regf_blocK", &ps
, 0, file
);
1292 /* now we are ready to write it to disk */
1293 if ( write_block( file
, &ps
, 0 ) == -1 )
1294 DEBUG(0,("regfio_close: failed to update the regf header block!\n"));
1297 prs_mem_free( &ps
);
1300 regfio_mem_free( file
);
1302 /* nothing tdo do if there is no open file */
1314 /*******************************************************************
1315 *******************************************************************/
1317 static void regfio_flush( REGF_FILE
*file
)
1321 for ( hbin
=file
->block_list
; hbin
; hbin
=hbin
->next
) {
1322 write_hbin_block( file
, hbin
);
1326 /*******************************************************************
1327 There should be only *one* root key in the registry file based
1328 on my experience. --jerry
1329 *******************************************************************/
1331 REGF_NK_REC
* regfio_rootkey( REGF_FILE
*file
)
1335 uint32 offset
= REGF_BLOCKSIZE
;
1342 if ( !(nk
= TALLOC_ZERO_P( file
->mem_ctx
, REGF_NK_REC
)) ) {
1343 DEBUG(0,("regfio_rootkey: talloc() failed!\n"));
1347 /* scan through the file on HBIN block at a time looking
1348 for an NK record with a type == 0x002c.
1349 Normally this is the first nk record in the first hbin
1350 block (but I'm not assuming that for now) */
1352 while ( (hbin
= read_hbin_block( file
, offset
)) ) {
1356 if ( next_nk_record( file
, hbin
, nk
, &eob
) ) {
1357 if ( nk
->key_type
== NK_TYPE_ROOTKEY
) {
1362 prs_mem_free( &hbin
->ps
);
1368 offset
+= hbin
->block_size
;
1372 DEBUG(0,("regfio_rootkey: corrupt registry file ? No root key record located\n"));
1376 DLIST_ADD( file
->block_list
, hbin
);
1381 /*******************************************************************
1382 This acts as an interator over the subkeys defined for a given
1383 NK record. Remember that offsets are from the *first* HBIN block.
1384 *******************************************************************/
1386 REGF_NK_REC
* regfio_fetch_subkey( REGF_FILE
*file
, REGF_NK_REC
*nk
)
1388 REGF_NK_REC
*subkey
;
1392 /* see if there is anything left to report */
1394 if ( !nk
|| (nk
->subkeys_off
==REGF_OFFSET_NONE
) || (nk
->subkey_index
>= nk
->num_subkeys
) )
1397 /* find the HBIN block which should contain the nk record */
1399 if ( !(hbin
= lookup_hbin_block( file
, nk
->subkeys
.hashes
[nk
->subkey_index
].nk_off
)) ) {
1400 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n",
1401 nk
->subkeys
.hashes
[nk
->subkey_index
].nk_off
));
1405 nk_offset
= nk
->subkeys
.hashes
[nk
->subkey_index
].nk_off
;
1406 if ( !prs_set_offset( &hbin
->ps
, (HBIN_HDR_SIZE
+ nk_offset
- hbin
->first_hbin_off
) ) )
1410 if ( !(subkey
= TALLOC_ZERO_P( file
->mem_ctx
, REGF_NK_REC
)) )
1413 if ( !hbin_prs_key( file
, hbin
, subkey
) )
1420 /*******************************************************************
1421 *******************************************************************/
1423 static REGF_HBIN
* regf_hbin_allocate( REGF_FILE
*file
, uint32 block_size
)
1426 SMB_STRUCT_STAT sbuf
;
1428 if ( !(hbin
= TALLOC_ZERO_P( file
->mem_ctx
, REGF_HBIN
)) )
1431 memcpy( hbin
->header
, "hbin", sizeof(HBIN_HDR_SIZE
) );
1434 if (sys_fstat(file
->fd
, &sbuf
, false)) {
1435 DEBUG(0,("regf_hbin_allocate: stat() failed! (%s)\n", strerror(errno
)));
1439 hbin
->file_off
= sbuf
.st_ex_size
;
1441 hbin
->free_off
= HBIN_HEADER_REC_SIZE
;
1442 hbin
->free_size
= block_size
- hbin
->free_off
+ sizeof(uint32
);;
1444 hbin
->block_size
= block_size
;
1445 hbin
->first_hbin_off
= hbin
->file_off
- REGF_BLOCKSIZE
;
1447 if ( !prs_init( &hbin
->ps
, block_size
, file
->mem_ctx
, MARSHALL
) )
1450 if ( !prs_hbin_block( "new_hbin", &hbin
->ps
, 0, hbin
) )
1453 if ( !write_hbin_block( file
, hbin
) )
1456 file
->last_block
= hbin
->file_off
;
1461 /*******************************************************************
1462 *******************************************************************/
1464 static void update_free_space( REGF_HBIN
*hbin
, uint32 size_used
)
1466 hbin
->free_off
+= size_used
;
1467 hbin
->free_size
-= size_used
;
1469 if ( hbin
->free_off
>= hbin
->block_size
) {
1470 hbin
->free_off
= REGF_OFFSET_NONE
;
1476 /*******************************************************************
1477 *******************************************************************/
1479 static REGF_HBIN
* find_free_space( REGF_FILE
*file
, uint32 size
)
1481 REGF_HBIN
*hbin
, *p_hbin
;
1485 /* check open block list */
1487 for ( hbin
=file
->block_list
; hbin
!=NULL
; hbin
=hbin
->next
) {
1488 /* only check blocks that actually have available space */
1490 if ( hbin
->free_off
== REGF_OFFSET_NONE
)
1493 /* check for a large enough available chunk */
1495 if ( (hbin
->block_size
- hbin
->free_off
) >= size
) {
1496 DLIST_PROMOTE( file
->block_list
, hbin
);
1501 /* parse the file until we find a block with
1502 enough free space; save the last non-filled hbin */
1504 block_off
= REGF_BLOCKSIZE
;
1506 /* cleanup before the next round */
1509 prs_mem_free( &hbin
->ps
);
1511 hbin
= read_hbin_block( file
, block_off
);
1515 /* make sure that we don't already have this block in memory */
1517 for ( p_hbin
=file
->block_list
; p_hbin
!=NULL
; p_hbin
=p_hbin
->next
) {
1518 if ( p_hbin
->file_off
== hbin
->file_off
) {
1524 block_off
= hbin
->file_off
+ hbin
->block_size
;
1527 prs_mem_free( &hbin
->ps
);
1532 /* if (cached block or (new block and not enough free space)) then continue looping */
1533 } while ( cached
|| (hbin
&& (hbin
->free_size
< size
)) );
1535 /* no free space; allocate a new one */
1540 /* allocate in multiples of REGF_ALLOC_BLOCK; make sure (size + hbin_header) fits */
1542 alloc_size
= (((size
+HBIN_HEADER_REC_SIZE
) / REGF_ALLOC_BLOCK
) + 1 ) * REGF_ALLOC_BLOCK
;
1544 if ( !(hbin
= regf_hbin_allocate( file
, alloc_size
)) ) {
1545 DEBUG(0,("find_free_space: regf_hbin_allocate() failed!\n"));
1548 DLIST_ADD( file
->block_list
, hbin
);
1552 /* set the offset to be ready to write */
1554 if ( !prs_set_offset( &hbin
->ps
, hbin
->free_off
-sizeof(uint32
) ) )
1557 /* write the record size as a placeholder for now, it should be
1558 probably updated by the caller once it all of the data necessary
1561 if ( !prs_uint32("allocated_size", &hbin
->ps
, 0, &size
) )
1564 update_free_space( hbin
, size
);
1569 /*******************************************************************
1570 *******************************************************************/
1572 static uint32
sk_record_data_size( struct security_descriptor
* sd
)
1574 uint32 size
, size_mod8
;
1578 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1580 size
= sizeof(uint32
)*5 + ndr_size_security_descriptor(sd
, 0) + sizeof(uint32
);
1583 size_mod8
= size
& 0xfffffff8;
1584 if ( size_mod8
< size
)
1590 /*******************************************************************
1591 *******************************************************************/
1593 static uint32
vk_record_data_size( REGF_VK_REC
*vk
)
1595 uint32 size
, size_mod8
;
1599 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1601 size
= REC_HDR_SIZE
+ (sizeof(uint16
)*3) + (sizeof(uint32
)*3) + sizeof(uint32
);
1603 if ( vk
->valuename
)
1604 size
+= strlen(vk
->valuename
);
1607 size_mod8
= size
& 0xfffffff8;
1608 if ( size_mod8
< size
)
1614 /*******************************************************************
1615 *******************************************************************/
1617 static uint32
lf_record_data_size( uint32 num_keys
)
1619 uint32 size
, size_mod8
;
1623 /* the record size is sizeof(hdr) + num_keys + sizeof of hash_array + data_size_uint32 */
1625 size
= REC_HDR_SIZE
+ sizeof(uint16
) + (sizeof(REGF_HASH_REC
) * num_keys
) + sizeof(uint32
);
1628 size_mod8
= size
& 0xfffffff8;
1629 if ( size_mod8
< size
)
1635 /*******************************************************************
1636 *******************************************************************/
1638 static uint32
nk_record_data_size( REGF_NK_REC
*nk
)
1640 uint32 size
, size_mod8
;
1644 /* the record size is static + length_of_keyname + length_of_classname + data_size_uint32 */
1646 size
= 0x4c + strlen(nk
->keyname
) + sizeof(uint32
);
1648 if ( nk
->classname
)
1649 size
+= strlen( nk
->classname
);
1652 size_mod8
= size
& 0xfffffff8;
1653 if ( size_mod8
< size
)
1659 /*******************************************************************
1660 *******************************************************************/
1662 static bool create_vk_record(REGF_FILE
*file
, REGF_VK_REC
*vk
,
1663 struct regval_blob
*value
)
1665 char *name
= regval_name(value
);
1666 REGF_HBIN
*data_hbin
;
1670 memcpy( vk
->header
, "vk", REC_HDR_SIZE
);
1673 vk
->valuename
= talloc_strdup( file
->mem_ctx
, regval_name(value
) );
1674 vk
->flag
= VK_FLAG_NAME_PRESENT
;
1677 vk
->data_size
= regval_size( value
);
1678 vk
->type
= regval_type( value
);
1680 if ( vk
->data_size
> sizeof(uint32
) ) {
1681 uint32 data_size
= ( (vk
->data_size
+sizeof(uint32
)) & 0xfffffff8 ) + 8;
1683 vk
->data
= (uint8
*)TALLOC_MEMDUP( file
->mem_ctx
,
1684 regval_data_p(value
),
1686 if (vk
->data
== NULL
) {
1690 /* go ahead and store the offset....we'll pick this hbin block back up when
1691 we stream the data */
1693 if ((data_hbin
= find_free_space(file
, data_size
)) == NULL
) {
1696 vk
->data_off
= prs_offset( &data_hbin
->ps
) + data_hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1699 /* make sure we don't try to copy from a NULL value pointer */
1701 if ( vk
->data_size
!= 0 )
1702 memcpy( &vk
->data_off
, regval_data_p(value
), sizeof(uint32
) );
1703 vk
->data_size
|= VK_DATA_IN_OFFSET
;
1709 /*******************************************************************
1710 *******************************************************************/
1712 static int hashrec_cmp( REGF_HASH_REC
*h1
, REGF_HASH_REC
*h2
)
1714 return StrCaseCmp( h1
->fullname
, h2
->fullname
);
1717 /*******************************************************************
1718 *******************************************************************/
1720 REGF_NK_REC
* regfio_write_key( REGF_FILE
*file
, const char *name
,
1721 struct regval_ctr
*values
, struct regsubkey_ctr
*subkeys
,
1722 struct security_descriptor
*sec_desc
, REGF_NK_REC
*parent
)
1725 REGF_HBIN
*vlist_hbin
= NULL
;
1728 if ( !(nk
= TALLOC_ZERO_P( file
->mem_ctx
, REGF_NK_REC
)) )
1731 memcpy( nk
->header
, "nk", REC_HDR_SIZE
);
1734 nk
->key_type
= NK_TYPE_ROOTKEY
;
1736 nk
->key_type
= NK_TYPE_NORMALKEY
;
1738 /* store the parent offset (or -1 if a the root key */
1740 nk
->parent_off
= parent
? (parent
->hbin_off
+ parent
->hbin
->file_off
- REGF_BLOCKSIZE
- HBIN_HDR_SIZE
) : REGF_OFFSET_NONE
;
1742 /* no classname currently */
1744 nk
->classname_off
= REGF_OFFSET_NONE
;
1745 nk
->classname
= NULL
;
1746 nk
->keyname
= talloc_strdup( file
->mem_ctx
, name
);
1748 /* current modification time */
1750 unix_to_nt_time( &nk
->mtime
, time(NULL
) );
1752 /* allocate the record on disk */
1754 size
= nk_record_data_size( nk
);
1755 nk
->rec_size
= ( size
- 1 ) ^ 0XFFFFFFFF;
1756 if ((nk
->hbin
= find_free_space( file
, size
)) == NULL
) {
1759 nk
->hbin_off
= prs_offset( &nk
->hbin
->ps
);
1761 /* Update the hash record in the parent */
1764 REGF_HASH_REC
*hash
= &parent
->subkeys
.hashes
[parent
->subkey_index
];
1766 hash
->nk_off
= prs_offset( &nk
->hbin
->ps
) + nk
->hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1767 memcpy( hash
->keycheck
, name
, sizeof(uint32
) );
1768 hash
->fullname
= talloc_strdup( file
->mem_ctx
, name
);
1769 parent
->subkey_index
++;
1771 /* sort the list by keyname */
1772 TYPESAFE_QSORT(parent
->subkeys
.hashes
, parent
->subkey_index
, hashrec_cmp
);
1774 if ( !hbin_prs_lf_records( "lf_rec", parent
->subkeys
.hbin
, 0, parent
) )
1778 /* write the security descriptor */
1780 nk
->sk_off
= REGF_OFFSET_NONE
;
1782 uint32 sk_size
= sk_record_data_size( sec_desc
);
1785 /* search for it in the existing list of sd's */
1787 if ( (nk
->sec_desc
= find_sk_record_by_sec_desc( file
, sec_desc
)) == NULL
) {
1788 /* not found so add it to the list */
1790 if (!(sk_hbin
= find_free_space( file
, sk_size
))) {
1794 if ( !(nk
->sec_desc
= TALLOC_ZERO_P( file
->mem_ctx
, REGF_SK_REC
)) )
1797 /* now we have to store the security descriptor in the list and
1798 update the offsets */
1800 memcpy( nk
->sec_desc
->header
, "sk", REC_HDR_SIZE
);
1801 nk
->sec_desc
->hbin
= sk_hbin
;
1802 nk
->sec_desc
->hbin_off
= prs_offset( &sk_hbin
->ps
);
1803 nk
->sec_desc
->sk_off
= prs_offset( &sk_hbin
->ps
) + sk_hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1804 nk
->sec_desc
->rec_size
= (sk_size
-1) ^ 0xFFFFFFFF;
1806 nk
->sec_desc
->sec_desc
= sec_desc
;
1807 nk
->sec_desc
->ref_count
= 0;
1809 /* size value must be self-inclusive */
1810 nk
->sec_desc
->size
= ndr_size_security_descriptor(sec_desc
, 0)
1813 DLIST_ADD_END( file
->sec_desc_list
, nk
->sec_desc
, REGF_SK_REC
*);
1815 /* update the offsets for us and the previous sd in the list.
1816 if this is the first record, then just set the next and prev
1817 offsets to ourself. */
1819 if ( DLIST_PREV(nk
->sec_desc
) ) {
1820 REGF_SK_REC
*prev
= DLIST_PREV(nk
->sec_desc
);
1822 nk
->sec_desc
->prev_sk_off
= prev
->hbin_off
+ prev
->hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1823 prev
->next_sk_off
= nk
->sec_desc
->sk_off
;
1825 /* the end must loop around to the front */
1826 nk
->sec_desc
->next_sk_off
= file
->sec_desc_list
->sk_off
;
1828 /* and first must loop around to the tail */
1829 file
->sec_desc_list
->prev_sk_off
= nk
->sec_desc
->sk_off
;
1831 nk
->sec_desc
->prev_sk_off
= nk
->sec_desc
->sk_off
;
1832 nk
->sec_desc
->next_sk_off
= nk
->sec_desc
->sk_off
;
1836 /* bump the reference count +1 */
1838 nk
->sk_off
= nk
->sec_desc
->sk_off
;
1839 nk
->sec_desc
->ref_count
++;
1842 /* write the subkeys */
1844 nk
->subkeys_off
= REGF_OFFSET_NONE
;
1845 if ( (nk
->num_subkeys
= regsubkey_ctr_numkeys( subkeys
)) != 0 ) {
1846 uint32 lf_size
= lf_record_data_size( nk
->num_subkeys
);
1850 if (!(nk
->subkeys
.hbin
= find_free_space( file
, lf_size
))) {
1853 nk
->subkeys
.hbin_off
= prs_offset( &nk
->subkeys
.hbin
->ps
);
1854 nk
->subkeys
.rec_size
= (lf_size
-1) ^ 0xFFFFFFFF;
1855 nk
->subkeys_off
= prs_offset( &nk
->subkeys
.hbin
->ps
) + nk
->subkeys
.hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1857 memcpy( nk
->subkeys
.header
, "lf", REC_HDR_SIZE
);
1859 nk
->subkeys
.num_keys
= nk
->num_subkeys
;
1860 if (nk
->subkeys
.num_keys
) {
1861 if ( !(nk
->subkeys
.hashes
= TALLOC_ZERO_ARRAY( file
->mem_ctx
, REGF_HASH_REC
, nk
->subkeys
.num_keys
)) )
1864 nk
->subkeys
.hashes
= NULL
;
1866 nk
->subkey_index
= 0;
1868 /* update the max_bytes_subkey{name,classname} fields */
1869 for ( i
=0; i
<nk
->num_subkeys
; i
++ ) {
1870 namelen
= strlen( regsubkey_ctr_specific_key(subkeys
, i
) );
1871 if ( namelen
*2 > nk
->max_bytes_subkeyname
)
1872 nk
->max_bytes_subkeyname
= namelen
* 2;
1876 /* write the values */
1878 nk
->values_off
= REGF_OFFSET_NONE
;
1879 if ( (nk
->num_values
= regval_ctr_numvals( values
)) != 0 ) {
1880 uint32 vlist_size
= ( ( nk
->num_values
* sizeof(uint32
) ) & 0xfffffff8 ) + 8;
1883 if (!(vlist_hbin
= find_free_space( file
, vlist_size
))) {
1886 nk
->values_off
= prs_offset( &vlist_hbin
->ps
) + vlist_hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1888 if (nk
->num_values
) {
1889 if ( !(nk
->values
= TALLOC_ARRAY( file
->mem_ctx
, REGF_VK_REC
, nk
->num_values
)) )
1895 /* create the vk records */
1897 for ( i
=0; i
<nk
->num_values
; i
++ ) {
1898 uint32 vk_size
, namelen
, datalen
;
1899 struct regval_blob
*r
;
1901 r
= regval_ctr_specific_value( values
, i
);
1902 create_vk_record( file
, &nk
->values
[i
], r
);
1903 vk_size
= vk_record_data_size( &nk
->values
[i
] );
1904 nk
->values
[i
].hbin
= find_free_space( file
, vk_size
);
1905 nk
->values
[i
].hbin_off
= prs_offset( &nk
->values
[i
].hbin
->ps
);
1906 nk
->values
[i
].rec_size
= ( vk_size
- 1 ) ^ 0xFFFFFFFF;
1907 nk
->values
[i
].rec_off
= prs_offset( &nk
->values
[i
].hbin
->ps
)
1908 + nk
->values
[i
].hbin
->first_hbin_off
1911 /* update the max bytes fields if necessary */
1913 namelen
= strlen( regval_name(r
) );
1914 if ( namelen
*2 > nk
->max_bytes_valuename
)
1915 nk
->max_bytes_valuename
= namelen
* 2;
1917 datalen
= regval_size( r
);
1918 if ( datalen
> nk
->max_bytes_value
)
1919 nk
->max_bytes_value
= datalen
;
1923 /* stream the records */
1925 prs_set_offset( &nk
->hbin
->ps
, nk
->hbin_off
);
1926 if ( !prs_nk_rec( "nk_rec", &nk
->hbin
->ps
, 0, nk
) )
1929 if ( nk
->num_values
) {
1930 if ( !hbin_prs_vk_records( "vk_records", vlist_hbin
, 0, nk
, file
) )
1935 regfio_flush( file
);