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/>.
21 #include "system/filesys.h"
23 #include "../librpc/gen_ndr/ndr_security.h"
24 #include "../libcli/security/security_descriptor.h"
25 #include "../libcli/security/secdesc.h"
28 #define DBGC_CLASS DBGC_REGISTRY
30 /*******************************************************************
32 * TODO : Right now this code basically ignores classnames.
34 ******************************************************************/
36 #if defined(PARANOID_MALLOC_CHECKER)
37 #define PRS_ALLOC_MEM(ps, type, count) (type *)prs_alloc_mem_((ps),sizeof(type),(count))
39 #define PRS_ALLOC_MEM(ps, type, count) (type *)prs_alloc_mem((ps),sizeof(type),(count))
42 /*******************************************************************
43 Reads or writes an NTTIME structure.
44 ********************************************************************/
46 static bool smb_io_time(const char *desc
, NTTIME
*nttime
, prs_struct
*ps
, int depth
)
52 prs_debug(ps
, depth
, desc
, "smb_io_time");
58 if (MARSHALLING(ps
)) {
59 low
= *nttime
& 0xFFFFFFFF;
63 if(!prs_uint32("low ", ps
, depth
, &low
)) /* low part */
65 if(!prs_uint32("high", ps
, depth
, &high
)) /* high part */
68 if (UNMARSHALLING(ps
)) {
69 *nttime
= (((uint64_t)high
<< 32) + low
);
75 /*******************************************************************
76 *******************************************************************/
78 static int write_block( REGF_FILE
*file
, prs_struct
*ps
, uint32_t offset
)
80 int bytes_written
, returned
;
81 char *buffer
= prs_data_p( ps
);
82 uint32_t buffer_size
= prs_data_size( ps
);
88 /* check for end of file */
90 if (sys_fstat(file
->fd
, &sbuf
, false)) {
91 DEBUG(0,("write_block: stat() failed! (%s)\n", strerror(errno
)));
95 if ( lseek( file
->fd
, offset
, SEEK_SET
) == -1 ) {
96 DEBUG(0,("write_block: lseek() failed! (%s)\n", strerror(errno
) ));
100 bytes_written
= returned
= 0;
101 while ( bytes_written
< buffer_size
) {
102 if ( (returned
= write( file
->fd
, buffer
+bytes_written
, buffer_size
-bytes_written
)) == -1 ) {
103 DEBUG(0,("write_block: write() failed! (%s)\n", strerror(errno
) ));
107 bytes_written
+= returned
;
110 return bytes_written
;
113 /*******************************************************************
114 *******************************************************************/
116 static int read_block( REGF_FILE
*file
, prs_struct
*ps
, uint32_t file_offset
, uint32_t block_size
)
118 int bytes_read
, returned
;
120 SMB_STRUCT_STAT sbuf
;
122 /* check for end of file */
124 if (sys_fstat(file
->fd
, &sbuf
, false)) {
125 DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno
)));
129 if ( (size_t)file_offset
>= sbuf
.st_ex_size
)
132 /* if block_size == 0, we are parsing HBIN records and need
133 to read some of the header to get the block_size from there */
135 if ( block_size
== 0 ) {
138 if ( lseek( file
->fd
, file_offset
, SEEK_SET
) == -1 ) {
139 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno
) ));
143 returned
= read( file
->fd
, hdr
, 0x20 );
144 if ( (returned
== -1) || (returned
< 0x20) ) {
145 DEBUG(0,("read_block: failed to read in HBIN header. Is the file corrupt?\n"));
149 /* make sure this is an hbin header */
151 if ( strncmp( hdr
, "hbin", HBIN_HDR_SIZE
) != 0 ) {
152 DEBUG(0,("read_block: invalid block header!\n"));
156 block_size
= IVAL( hdr
, 0x08 );
159 DEBUG(10,("read_block: block_size == 0x%x\n", block_size
));
161 /* set the offset, initialize the buffer, and read the block from disk */
163 if ( lseek( file
->fd
, file_offset
, SEEK_SET
) == -1 ) {
164 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno
) ));
168 if (!prs_init( ps
, block_size
, file
->mem_ctx
, UNMARSHALL
)) {
169 DEBUG(0,("read_block: prs_init() failed! (%s)\n", strerror(errno
) ));
172 buffer
= prs_data_p( ps
);
173 bytes_read
= returned
= 0;
175 while ( bytes_read
< block_size
) {
176 if ( (returned
= read( file
->fd
, buffer
+bytes_read
, block_size
-bytes_read
)) == -1 ) {
177 DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno
) ));
180 if ( (returned
== 0) && (bytes_read
< block_size
) ) {
181 DEBUG(0,("read_block: not a valid registry file ?\n" ));
185 bytes_read
+= returned
;
191 /*******************************************************************
192 *******************************************************************/
194 static bool write_hbin_block( REGF_FILE
*file
, REGF_HBIN
*hbin
)
199 /* write free space record if any is available */
201 if ( hbin
->free_off
!= REGF_OFFSET_NONE
) {
202 uint32_t header
= 0xffffffff;
204 if ( !prs_set_offset( &hbin
->ps
, hbin
->free_off
-sizeof(uint32_t) ) )
206 if ( !prs_uint32( "free_size", &hbin
->ps
, 0, &hbin
->free_size
) )
208 if ( !prs_uint32( "free_header", &hbin
->ps
, 0, &header
) )
212 hbin
->dirty
= (write_block( file
, &hbin
->ps
, hbin
->file_off
) != -1);
217 /*******************************************************************
218 *******************************************************************/
220 static bool hbin_block_close( REGF_FILE
*file
, REGF_HBIN
*hbin
)
224 /* remove the block from the open list and flush it to disk */
226 for ( p
=file
->block_list
; p
&& p
!=hbin
; p
=p
->next
)
230 DLIST_REMOVE( file
->block_list
, hbin
);
233 DEBUG(0,("hbin_block_close: block not in open list!\n"));
235 if ( !write_hbin_block( file
, hbin
) )
241 /*******************************************************************
242 *******************************************************************/
244 static bool prs_regf_block( const char *desc
, prs_struct
*ps
, int depth
, REGF_FILE
*file
)
246 prs_debug(ps
, depth
, desc
, "prs_regf_block");
249 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8_t *)file
->header
, sizeof( file
->header
)) )
252 /* yes, these values are always identical so store them only once */
254 if ( !prs_uint32( "unknown1", ps
, depth
, &file
->unknown1
))
256 if ( !prs_uint32( "unknown1 (again)", ps
, depth
, &file
->unknown1
))
259 /* get the modtime */
261 if ( !prs_set_offset( ps
, 0x0c ) )
263 if ( !smb_io_time( "modtime", &file
->mtime
, ps
, depth
) )
268 if ( !prs_uint32( "unknown2", ps
, depth
, &file
->unknown2
))
270 if ( !prs_uint32( "unknown3", ps
, depth
, &file
->unknown3
))
272 if ( !prs_uint32( "unknown4", ps
, depth
, &file
->unknown4
))
274 if ( !prs_uint32( "unknown5", ps
, depth
, &file
->unknown5
))
277 /* get file offsets */
279 if ( !prs_set_offset( ps
, 0x24 ) )
281 if ( !prs_uint32( "data_offset", ps
, depth
, &file
->data_offset
))
283 if ( !prs_uint32( "last_block", ps
, depth
, &file
->last_block
))
286 /* one more constant */
288 if ( !prs_uint32( "unknown6", ps
, depth
, &file
->unknown6
))
291 /* get the checksum */
293 if ( !prs_set_offset( ps
, 0x01fc ) )
295 if ( !prs_uint32( "checksum", ps
, depth
, &file
->checksum
))
301 /*******************************************************************
302 *******************************************************************/
304 static bool prs_hbin_block( const char *desc
, prs_struct
*ps
, int depth
, REGF_HBIN
*hbin
)
306 uint32_t block_size2
;
308 prs_debug(ps
, depth
, desc
, "prs_regf_block");
311 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8_t*)hbin
->header
, sizeof( hbin
->header
)) )
314 if ( !prs_uint32( "first_hbin_off", ps
, depth
, &hbin
->first_hbin_off
))
317 /* The dosreg.cpp comments say that the block size is at 0x1c.
318 According to a WINXP NTUSER.dat file, this is wrong. The block_size
321 if ( !prs_uint32( "block_size", ps
, depth
, &hbin
->block_size
))
324 block_size2
= hbin
->block_size
;
325 prs_set_offset( ps
, 0x1c );
326 if ( !prs_uint32( "block_size2", ps
, depth
, &block_size2
))
329 if ( MARSHALLING(ps
) )
336 /*******************************************************************
337 *******************************************************************/
339 static bool prs_nk_rec( const char *desc
, prs_struct
*ps
, int depth
, REGF_NK_REC
*nk
)
341 uint16_t class_length
, name_length
;
343 uint32_t data_size
, start_off
, end_off
;
344 uint32_t unknown_off
= REGF_OFFSET_NONE
;
346 nk
->hbin_off
= prs_offset( ps
);
347 start
= nk
->hbin_off
;
349 prs_debug(ps
, depth
, desc
, "prs_nk_rec");
352 /* back up and get the data_size */
354 if ( !prs_set_offset( ps
, prs_offset(ps
)-sizeof(uint32_t)) )
356 start_off
= prs_offset( ps
);
357 if ( !prs_uint32( "rec_size", ps
, depth
, &nk
->rec_size
))
360 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8_t *)nk
->header
, sizeof( nk
->header
)) )
363 if ( !prs_uint16( "key_type", ps
, depth
, &nk
->key_type
))
365 if ( !smb_io_time( "mtime", &nk
->mtime
, ps
, depth
))
368 if ( !prs_set_offset( ps
, start
+0x0010 ) )
370 if ( !prs_uint32( "parent_off", ps
, depth
, &nk
->parent_off
))
372 if ( !prs_uint32( "num_subkeys", ps
, depth
, &nk
->num_subkeys
))
375 if ( !prs_set_offset( ps
, start
+0x001c ) )
377 if ( !prs_uint32( "subkeys_off", ps
, depth
, &nk
->subkeys_off
))
379 if ( !prs_uint32( "unknown_off", ps
, depth
, &unknown_off
) )
382 if ( !prs_set_offset( ps
, start
+0x0024 ) )
384 if ( !prs_uint32( "num_values", ps
, depth
, &nk
->num_values
))
386 if ( !prs_uint32( "values_off", ps
, depth
, &nk
->values_off
))
388 if ( !prs_uint32( "sk_off", ps
, depth
, &nk
->sk_off
))
390 if ( !prs_uint32( "classname_off", ps
, depth
, &nk
->classname_off
))
393 if ( !prs_uint32( "max_bytes_subkeyname", ps
, depth
, &nk
->max_bytes_subkeyname
))
395 if ( !prs_uint32( "max_bytes_subkeyclassname", ps
, depth
, &nk
->max_bytes_subkeyclassname
))
397 if ( !prs_uint32( "max_bytes_valuename", ps
, depth
, &nk
->max_bytes_valuename
))
399 if ( !prs_uint32( "max_bytes_value", ps
, depth
, &nk
->max_bytes_value
))
401 if ( !prs_uint32( "unknown index", ps
, depth
, &nk
->unk_index
))
404 name_length
= nk
->keyname
? strlen(nk
->keyname
) : 0 ;
405 class_length
= nk
->classname
? strlen(nk
->classname
) : 0 ;
406 if ( !prs_uint16( "name_length", ps
, depth
, &name_length
))
408 if ( !prs_uint16( "class_length", ps
, depth
, &class_length
))
411 if ( class_length
) {
416 if ( UNMARSHALLING(ps
) ) {
417 if ( !(nk
->keyname
= PRS_ALLOC_MEM( ps
, char, name_length
+1 )) )
421 if ( !prs_uint8s( True
, "name", ps
, depth
, (uint8_t *)nk
->keyname
, name_length
) )
424 if ( UNMARSHALLING(ps
) )
425 nk
->keyname
[name_length
] = '\0';
428 end_off
= prs_offset( ps
);
430 /* data_size must be divisible by 8 and large enough to hold the original record */
432 data_size
= ((start_off
- end_off
) & 0xfffffff8 );
433 if ( data_size
> nk
->rec_size
)
434 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size
, nk
->rec_size
));
436 if ( MARSHALLING(ps
) )
437 nk
->hbin
->dirty
= True
;
442 /*******************************************************************
443 *******************************************************************/
445 static uint32_t regf_block_checksum( prs_struct
*ps
)
447 char *buffer
= prs_data_p( ps
);
448 uint32_t checksum
, x
;
451 /* XOR of all bytes 0x0000 - 0x01FB */
455 for ( i
=0; i
<0x01FB; i
+=4 ) {
456 x
= IVAL(buffer
, i
);
463 /*******************************************************************
464 *******************************************************************/
466 static bool read_regf_block( REGF_FILE
*file
)
471 /* grab the first block from the file */
473 if ( read_block( file
, &ps
, 0, REGF_BLOCKSIZE
) == -1 )
476 /* parse the block and verify the checksum */
478 if ( !prs_regf_block( "regf_header", &ps
, 0, file
) )
481 checksum
= regf_block_checksum( &ps
);
485 if ( file
->checksum
!= checksum
) {
486 DEBUG(0,("read_regf_block: invalid checksum\n" ));
493 /*******************************************************************
494 *******************************************************************/
496 static REGF_HBIN
* read_hbin_block( REGF_FILE
*file
, off_t offset
)
499 uint32_t record_size
, curr_off
, block_size
, header
;
501 if ( !(hbin
= talloc_zero(file
->mem_ctx
, REGF_HBIN
)) )
503 hbin
->file_off
= offset
;
506 if ( read_block( file
, &hbin
->ps
, offset
, 0 ) == -1 )
509 if ( !prs_hbin_block( "hbin", &hbin
->ps
, 0, hbin
) )
512 /* this should be the same thing as hbin->block_size but just in case */
514 block_size
= prs_data_size( &hbin
->ps
);
516 /* Find the available free space offset. Always at the end,
517 so walk the record list and stop when you get to the end.
518 The end is defined by a record header of 0xffffffff. The
519 previous 4 bytes contains the amount of free space remaining
520 in the hbin block. */
522 /* remember that the record_size is in the 4 bytes preceeding the record itself */
524 if ( !prs_set_offset( &hbin
->ps
, file
->data_offset
+HBIN_HDR_SIZE
-sizeof(uint32_t) ) )
529 curr_off
= prs_offset( &hbin
->ps
);
530 while ( header
!= 0xffffffff ) {
531 /* not done yet so reset the current offset to the
532 next record_size field */
534 curr_off
= curr_off
+record_size
;
536 /* for some reason the record_size of the last record in
537 an hbin block can extend past the end of the block
538 even though the record fits within the remaining
539 space....aaarrrgggghhhhhh */
541 if ( curr_off
>= block_size
) {
547 if ( !prs_set_offset( &hbin
->ps
, curr_off
) )
550 if ( !prs_uint32( "rec_size", &hbin
->ps
, 0, &record_size
) )
552 if ( !prs_uint32( "header", &hbin
->ps
, 0, &header
) )
555 SMB_ASSERT( record_size
!= 0 );
557 if ( record_size
& 0x80000000 ) {
558 /* absolute_value(record_size) */
559 record_size
= (record_size
^ 0xffffffff) + 1;
563 /* save the free space offset */
565 if ( header
== 0xffffffff ) {
567 /* account for the fact that the curr_off is 4 bytes behind the actual
570 hbin
->free_off
= curr_off
+ sizeof(uint32_t);
571 hbin
->free_size
= record_size
;
574 DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin
->free_off
));
576 if ( !prs_set_offset( &hbin
->ps
, file
->data_offset
+HBIN_HDR_SIZE
) )
582 /*******************************************************************
583 Input a random offset and receive the corresponding HBIN
585 *******************************************************************/
587 static bool hbin_contains_offset( REGF_HBIN
*hbin
, uint32_t offset
)
592 if ( (offset
> hbin
->first_hbin_off
) && (offset
< (hbin
->first_hbin_off
+hbin
->block_size
)) )
598 /*******************************************************************
599 Input a random offset and receive the corresponding HBIN
601 *******************************************************************/
603 static REGF_HBIN
* lookup_hbin_block( REGF_FILE
*file
, uint32_t offset
)
605 REGF_HBIN
*hbin
= NULL
;
608 /* start with the open list */
610 for ( hbin
=file
->block_list
; hbin
; hbin
=hbin
->next
) {
611 DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%lx]\n", hbin
->file_off
, (unsigned long)hbin
));
612 if ( hbin_contains_offset( hbin
, offset
) )
617 /* start at the beginning */
619 block_off
= REGF_BLOCKSIZE
;
621 /* cleanup before the next round */
623 prs_mem_free( &hbin
->ps
);
625 hbin
= read_hbin_block( file
, block_off
);
628 block_off
= hbin
->file_off
+ hbin
->block_size
;
630 } while ( hbin
&& !hbin_contains_offset( hbin
, offset
) );
634 DLIST_ADD( file
->block_list
, hbin
);
639 /*******************************************************************
640 *******************************************************************/
642 static bool prs_hash_rec( const char *desc
, prs_struct
*ps
, int depth
, REGF_HASH_REC
*hash
)
644 prs_debug(ps
, depth
, desc
, "prs_hash_rec");
647 if ( !prs_uint32( "nk_off", ps
, depth
, &hash
->nk_off
))
649 if ( !prs_uint8s( True
, "keycheck", ps
, depth
, hash
->keycheck
, sizeof( hash
->keycheck
)) )
655 /*******************************************************************
656 *******************************************************************/
658 static bool hbin_prs_lf_records( const char *desc
, REGF_HBIN
*hbin
, int depth
, REGF_NK_REC
*nk
)
661 REGF_LF_REC
*lf
= &nk
->subkeys
;
662 uint32_t data_size
, start_off
, end_off
;
664 prs_debug(&hbin
->ps
, depth
, desc
, "prs_lf_records");
667 /* check if we have anything to do first */
669 if ( nk
->num_subkeys
== 0 )
672 /* move to the LF record */
674 if ( !prs_set_offset( &hbin
->ps
, nk
->subkeys_off
+ HBIN_HDR_SIZE
- hbin
->first_hbin_off
) )
677 /* backup and get the data_size */
679 if ( !prs_set_offset( &hbin
->ps
, prs_offset(&hbin
->ps
)-sizeof(uint32_t)) )
681 start_off
= prs_offset( &hbin
->ps
);
682 if ( !prs_uint32( "rec_size", &hbin
->ps
, depth
, &lf
->rec_size
))
685 if ( !prs_uint8s( True
, "header", &hbin
->ps
, depth
, (uint8_t *)lf
->header
, sizeof( lf
->header
)) )
688 if ( !prs_uint16( "num_keys", &hbin
->ps
, depth
, &lf
->num_keys
))
691 if ( UNMARSHALLING(&hbin
->ps
) ) {
693 if ( !(lf
->hashes
= PRS_ALLOC_MEM( &hbin
->ps
, REGF_HASH_REC
, lf
->num_keys
)) )
700 for ( i
=0; i
<lf
->num_keys
; i
++ ) {
701 if ( !prs_hash_rec( "hash_rec", &hbin
->ps
, depth
, &lf
->hashes
[i
] ) )
705 end_off
= prs_offset( &hbin
->ps
);
707 /* data_size must be divisible by 8 and large enough to hold the original record */
709 data_size
= ((start_off
- end_off
) & 0xfffffff8 );
710 if ( data_size
> lf
->rec_size
)
711 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size
, lf
->rec_size
));
713 if ( MARSHALLING(&hbin
->ps
) )
719 /*******************************************************************
720 *******************************************************************/
722 static bool hbin_prs_sk_rec( const char *desc
, REGF_HBIN
*hbin
, int depth
, REGF_SK_REC
*sk
)
724 prs_struct
*ps
= &hbin
->ps
;
725 uint16_t tag
= 0xFFFF;
726 uint32_t data_size
, start_off
, end_off
;
729 prs_debug(ps
, depth
, desc
, "hbin_prs_sk_rec");
732 if ( !prs_set_offset( &hbin
->ps
, sk
->sk_off
+ HBIN_HDR_SIZE
- hbin
->first_hbin_off
) )
735 /* backup and get the data_size */
737 if ( !prs_set_offset( &hbin
->ps
, prs_offset(&hbin
->ps
)-sizeof(uint32_t)) )
739 start_off
= prs_offset( &hbin
->ps
);
740 if ( !prs_uint32( "rec_size", &hbin
->ps
, depth
, &sk
->rec_size
))
743 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8_t *)sk
->header
, sizeof( sk
->header
)) )
745 if ( !prs_uint16( "tag", ps
, depth
, &tag
))
748 if ( !prs_uint32( "prev_sk_off", ps
, depth
, &sk
->prev_sk_off
))
750 if ( !prs_uint32( "next_sk_off", ps
, depth
, &sk
->next_sk_off
))
752 if ( !prs_uint32( "ref_count", ps
, depth
, &sk
->ref_count
))
754 if ( !prs_uint32( "size", ps
, depth
, &sk
->size
))
759 TALLOC_CTX
*mem_ctx
= prs_get_mem_context(&hbin
->ps
);
762 if (MARSHALLING(&hbin
->ps
)) {
763 status
= marshall_sec_desc(mem_ctx
,
765 &blob
.data
, &blob
.length
);
766 if (!NT_STATUS_IS_OK(status
))
768 if (!prs_copy_data_in(&hbin
->ps
, (const char *)blob
.data
, blob
.length
))
771 blob
= data_blob_const(
772 prs_data_p(&hbin
->ps
) + prs_offset(&hbin
->ps
),
773 prs_data_size(&hbin
->ps
) - prs_offset(&hbin
->ps
)
775 status
= unmarshall_sec_desc(mem_ctx
,
776 blob
.data
, blob
.length
,
778 if (!NT_STATUS_IS_OK(status
))
780 prs_set_offset(&hbin
->ps
, blob
.length
);
784 end_off
= prs_offset( &hbin
->ps
);
786 /* data_size must be divisible by 8 and large enough to hold the original record */
788 data_size
= ((start_off
- end_off
) & 0xfffffff8 );
789 if ( data_size
> sk
->rec_size
)
790 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size
, sk
->rec_size
));
792 if ( MARSHALLING(&hbin
->ps
) )
798 /*******************************************************************
799 *******************************************************************/
801 static bool hbin_prs_vk_rec( const char *desc
, REGF_HBIN
*hbin
, int depth
, REGF_VK_REC
*vk
, REGF_FILE
*file
)
804 uint16_t name_length
;
805 prs_struct
*ps
= &hbin
->ps
;
806 uint32_t data_size
, start_off
, end_off
;
808 prs_debug(ps
, depth
, desc
, "prs_vk_rec");
811 /* backup and get the data_size */
813 if ( !prs_set_offset( &hbin
->ps
, prs_offset(&hbin
->ps
)-sizeof(uint32_t)) )
815 start_off
= prs_offset( &hbin
->ps
);
816 if ( !prs_uint32( "rec_size", &hbin
->ps
, depth
, &vk
->rec_size
))
819 if ( !prs_uint8s( True
, "header", ps
, depth
, (uint8_t *)vk
->header
, sizeof( vk
->header
)) )
822 if ( MARSHALLING(&hbin
->ps
) )
823 name_length
= strlen(vk
->valuename
);
825 if ( !prs_uint16( "name_length", ps
, depth
, &name_length
))
827 if ( !prs_uint32( "data_size", ps
, depth
, &vk
->data_size
))
829 if ( !prs_uint32( "data_off", ps
, depth
, &vk
->data_off
))
831 if ( !prs_uint32( "type", ps
, depth
, &vk
->type
))
833 if ( !prs_uint16( "flag", ps
, depth
, &vk
->flag
))
836 offset
= prs_offset( ps
);
837 offset
+= 2; /* skip 2 bytes */
838 prs_set_offset( ps
, offset
);
842 if ( vk
->flag
&VK_FLAG_NAME_PRESENT
) {
844 if ( UNMARSHALLING(&hbin
->ps
) ) {
845 if ( !(vk
->valuename
= PRS_ALLOC_MEM( ps
, char, name_length
+1 )))
848 if ( !prs_uint8s( True
, "name", ps
, depth
, (uint8_t *)vk
->valuename
, name_length
) )
852 end_off
= prs_offset( &hbin
->ps
);
854 /* get the data if necessary */
856 if ( vk
->data_size
!= 0 ) {
857 bool charmode
= False
;
859 if ( (vk
->type
== REG_SZ
) || (vk
->type
== REG_MULTI_SZ
) )
862 /* the data is stored in the offset if the size <= 4 */
864 if ( !(vk
->data_size
& VK_DATA_IN_OFFSET
) ) {
865 REGF_HBIN
*hblock
= hbin
;
866 uint32_t data_rec_size
;
868 if ( UNMARSHALLING(&hbin
->ps
) ) {
869 if ( !(vk
->data
= PRS_ALLOC_MEM( ps
, uint8_t, vk
->data_size
) ) )
873 /* this data can be in another hbin */
874 if ( !hbin_contains_offset( hbin
, vk
->data_off
) ) {
875 if ( !(hblock
= lookup_hbin_block( file
, vk
->data_off
)) )
878 if ( !(prs_set_offset( &hblock
->ps
, (vk
->data_off
+HBIN_HDR_SIZE
-hblock
->first_hbin_off
)-sizeof(uint32_t) )) )
881 if ( MARSHALLING(&hblock
->ps
) ) {
882 data_rec_size
= ( (vk
->data_size
+sizeof(uint32_t)) & 0xfffffff8 ) + 8;
883 data_rec_size
= ( data_rec_size
- 1 ) ^ 0xFFFFFFFF;
885 if ( !prs_uint32( "data_rec_size", &hblock
->ps
, depth
, &data_rec_size
))
887 if ( !prs_uint8s( charmode
, "data", &hblock
->ps
, depth
, vk
->data
, vk
->data_size
) )
890 if ( MARSHALLING(&hblock
->ps
) )
891 hblock
->dirty
= True
;
894 if ( !(vk
->data
= PRS_ALLOC_MEM( ps
, uint8_t, 4 ) ) )
896 SIVAL( vk
->data
, 0, vk
->data_off
);
901 /* data_size must be divisible by 8 and large enough to hold the original record */
903 data_size
= ((start_off
- end_off
) & 0xfffffff8 );
904 if ( data_size
!= vk
->rec_size
)
905 DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size
, vk
->rec_size
));
907 if ( MARSHALLING(&hbin
->ps
) )
913 /*******************************************************************
914 read a VK record which is contained in the HBIN block stored
915 in the prs_struct *ps.
916 *******************************************************************/
918 static bool hbin_prs_vk_records( const char *desc
, REGF_HBIN
*hbin
, int depth
, REGF_NK_REC
*nk
, REGF_FILE
*file
)
921 uint32_t record_size
;
923 prs_debug(&hbin
->ps
, depth
, desc
, "prs_vk_records");
926 /* check if we have anything to do first */
928 if ( nk
->num_values
== 0 )
931 if ( UNMARSHALLING(&hbin
->ps
) ) {
932 if ( !(nk
->values
= PRS_ALLOC_MEM( &hbin
->ps
, REGF_VK_REC
, nk
->num_values
) ) )
936 /* convert the offset to something relative to this HBIN block */
938 if ( !prs_set_offset( &hbin
->ps
, nk
->values_off
+HBIN_HDR_SIZE
-hbin
->first_hbin_off
-sizeof(uint32_t)) )
941 if ( MARSHALLING( &hbin
->ps
) ) {
942 record_size
= ( ( nk
->num_values
* sizeof(uint32_t) ) & 0xfffffff8 ) + 8;
943 record_size
= (record_size
- 1) ^ 0xFFFFFFFF;
946 if ( !prs_uint32( "record_size", &hbin
->ps
, depth
, &record_size
) )
949 for ( i
=0; i
<nk
->num_values
; i
++ ) {
950 if ( !prs_uint32( "vk_off", &hbin
->ps
, depth
, &nk
->values
[i
].rec_off
) )
954 for ( i
=0; i
<nk
->num_values
; i
++ ) {
955 REGF_HBIN
*sub_hbin
= hbin
;
958 if ( !hbin_contains_offset( hbin
, nk
->values
[i
].rec_off
) ) {
959 sub_hbin
= lookup_hbin_block( file
, nk
->values
[i
].rec_off
);
961 DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n",
962 nk
->values
[i
].hbin_off
));
967 new_offset
= nk
->values
[i
].rec_off
+ HBIN_HDR_SIZE
- sub_hbin
->first_hbin_off
;
968 if ( !prs_set_offset( &sub_hbin
->ps
, new_offset
) )
970 if ( !hbin_prs_vk_rec( "vk_rec", sub_hbin
, depth
, &nk
->values
[i
], file
) )
974 if ( MARSHALLING(&hbin
->ps
) )
982 /*******************************************************************
983 *******************************************************************/
985 static REGF_SK_REC
* find_sk_record_by_offset( REGF_FILE
*file
, uint32_t offset
)
989 for ( p_sk
=file
->sec_desc_list
; p_sk
; p_sk
=p_sk
->next
) {
990 if ( p_sk
->sk_off
== offset
)
997 /*******************************************************************
998 *******************************************************************/
1000 static REGF_SK_REC
* find_sk_record_by_sec_desc( REGF_FILE
*file
, struct security_descriptor
*sd
)
1004 for ( p
=file
->sec_desc_list
; p
; p
=p
->next
) {
1005 if ( security_descriptor_equal( p
->sec_desc
, sd
) )
1014 /*******************************************************************
1015 *******************************************************************/
1017 static bool hbin_prs_key( REGF_FILE
*file
, REGF_HBIN
*hbin
, REGF_NK_REC
*nk
)
1020 REGF_HBIN
*sub_hbin
;
1022 prs_debug(&hbin
->ps
, depth
, "", "fetch_key");
1025 /* get the initial nk record */
1027 if ( !prs_nk_rec( "nk_rec", &hbin
->ps
, depth
, nk
))
1030 /* fill in values */
1032 if ( nk
->num_values
&& (nk
->values_off
!=REGF_OFFSET_NONE
) ) {
1034 if ( !hbin_contains_offset( hbin
, nk
->values_off
) ) {
1035 sub_hbin
= lookup_hbin_block( file
, nk
->values_off
);
1037 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n",
1043 if ( !hbin_prs_vk_records( "vk_rec", sub_hbin
, depth
, nk
, file
))
1047 /* now get subkeys */
1049 if ( nk
->num_subkeys
&& (nk
->subkeys_off
!=REGF_OFFSET_NONE
) ) {
1051 if ( !hbin_contains_offset( hbin
, nk
->subkeys_off
) ) {
1052 sub_hbin
= lookup_hbin_block( file
, nk
->subkeys_off
);
1054 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n",
1060 if ( !hbin_prs_lf_records( "lf_rec", sub_hbin
, depth
, nk
))
1064 /* get the to the security descriptor. First look if we have already parsed it */
1066 if ( (nk
->sk_off
!=REGF_OFFSET_NONE
) && !( nk
->sec_desc
= find_sk_record_by_offset( file
, nk
->sk_off
)) ) {
1069 if ( !hbin_contains_offset( hbin
, nk
->sk_off
) ) {
1070 sub_hbin
= lookup_hbin_block( file
, nk
->sk_off
);
1072 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_off [0x%x]\n",
1078 if ( !(nk
->sec_desc
= talloc_zero( file
->mem_ctx
, REGF_SK_REC
)) )
1080 nk
->sec_desc
->sk_off
= nk
->sk_off
;
1081 if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin
, depth
, nk
->sec_desc
))
1084 /* add to the list of security descriptors (ref_count has been read from the files) */
1086 nk
->sec_desc
->sk_off
= nk
->sk_off
;
1087 DLIST_ADD( file
->sec_desc_list
, nk
->sec_desc
);
1093 /*******************************************************************
1094 *******************************************************************/
1096 static bool next_record( REGF_HBIN
*hbin
, const char *hdr
, bool *eob
)
1098 uint8_t header
[REC_HDR_SIZE
];
1099 uint32_t record_size
;
1100 uint32_t curr_off
, block_size
;
1102 prs_struct
*ps
= &hbin
->ps
;
1104 curr_off
= prs_offset( ps
);
1105 if ( curr_off
== 0 )
1106 prs_set_offset( ps
, HBIN_HEADER_REC_SIZE
);
1108 /* assume that the current offset is at the record header
1109 and we need to backup to read the record size */
1111 curr_off
-= sizeof(uint32_t);
1113 block_size
= prs_data_size( ps
);
1115 memset( header
, 0x0, sizeof(uint8_t)*REC_HDR_SIZE
);
1118 curr_off
= curr_off
+record_size
;
1119 if ( curr_off
>= block_size
)
1122 if ( !prs_set_offset( &hbin
->ps
, curr_off
) )
1125 if ( !prs_uint32( "record_size", ps
, 0, &record_size
) )
1127 if ( !prs_uint8s( True
, "header", ps
, 0, header
, REC_HDR_SIZE
) )
1130 if ( record_size
& 0x80000000 ) {
1131 /* absolute_value(record_size) */
1132 record_size
= (record_size
^ 0xffffffff) + 1;
1135 if ( memcmp( header
, hdr
, REC_HDR_SIZE
) == 0 ) {
1137 curr_off
+= sizeof(uint32_t);
1141 /* mark prs_struct as done ( at end ) if no more SK records */
1142 /* mark end-of-block as True */
1145 prs_set_offset( &hbin
->ps
, prs_data_size(&hbin
->ps
) );
1150 if ( !prs_set_offset( ps
, curr_off
) )
1156 /*******************************************************************
1157 *******************************************************************/
1159 static bool next_nk_record( REGF_FILE
*file
, REGF_HBIN
*hbin
, REGF_NK_REC
*nk
, bool *eob
)
1161 if ( next_record( hbin
, "nk", eob
) && hbin_prs_key( file
, hbin
, nk
) )
1167 /*******************************************************************
1168 Intialize the newly created REGF_BLOCK in *file and write the
1169 block header to disk
1170 *******************************************************************/
1172 static bool init_regf_block( REGF_FILE
*file
)
1177 if ( !prs_init( &ps
, REGF_BLOCKSIZE
, file
->mem_ctx
, MARSHALL
) )
1180 memcpy( file
->header
, "regf", REGF_HDR_SIZE
);
1181 file
->data_offset
= 0x20;
1182 file
->last_block
= 0x1000;
1186 unix_to_nt_time( &file
->mtime
, time(NULL
) );
1188 /* hard coded values...no diea what these are ... maybe in time */
1190 file
->unknown1
= 0x2;
1191 file
->unknown2
= 0x1;
1192 file
->unknown3
= 0x3;
1193 file
->unknown4
= 0x0;
1194 file
->unknown5
= 0x1;
1195 file
->unknown6
= 0x1;
1197 /* write header to the buffer */
1199 if ( !prs_regf_block( "regf_header", &ps
, 0, file
) ) {
1204 /* calculate the checksum, re-marshall data (to include the checksum)
1205 and write to disk */
1207 file
->checksum
= regf_block_checksum( &ps
);
1208 prs_set_offset( &ps
, 0 );
1209 if ( !prs_regf_block( "regf_header", &ps
, 0, file
) ) {
1214 if ( write_block( file
, &ps
, 0 ) == -1 ) {
1215 DEBUG(0,("init_regf_block: Failed to initialize registry header block!\n"));
1221 prs_mem_free( &ps
);
1225 /*******************************************************************
1226 Open the registry file and then read in the REGF block to get the
1228 *******************************************************************/
1230 REGF_FILE
* regfio_open( const char *filename
, int flags
, int mode
)
1234 if ( !(rb
= SMB_MALLOC_P(REGF_FILE
)) ) {
1235 DEBUG(0,("ERROR allocating memory\n"));
1241 if ( !(rb
->mem_ctx
= talloc_init( "read_regf_block" )) ) {
1246 rb
->open_flags
= flags
;
1248 /* open and existing file */
1250 if ( (rb
->fd
= open(filename
, flags
, mode
)) == -1 ) {
1251 DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename
, strerror(errno
)));
1256 /* check if we are creating a new file or overwriting an existing one */
1258 if ( flags
& (O_CREAT
|O_TRUNC
) ) {
1259 if ( !init_regf_block( rb
) ) {
1260 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1269 /* read in an existing file */
1271 if ( !read_regf_block( rb
) ) {
1272 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1282 /*******************************************************************
1283 *******************************************************************/
1285 static void regfio_mem_free( REGF_FILE
*file
)
1287 /* free any talloc()'d memory */
1289 if ( file
&& file
->mem_ctx
)
1290 talloc_destroy( file
->mem_ctx
);
1293 /*******************************************************************
1294 *******************************************************************/
1296 int regfio_close( REGF_FILE
*file
)
1300 /* cleanup for a file opened for write */
1302 if ((file
->fd
!= -1) && (file
->open_flags
& (O_WRONLY
|O_RDWR
))) {
1306 /* write of sd list */
1308 for ( sk
=file
->sec_desc_list
; sk
; sk
=sk
->next
) {
1309 hbin_prs_sk_rec( "sk_rec", sk
->hbin
, 0, sk
);
1312 /* flush any dirty blocks */
1314 while ( file
->block_list
) {
1315 hbin_block_close( file
, file
->block_list
);
1320 unix_to_nt_time( &file
->mtime
, time(NULL
) );
1322 if ( read_block( file
, &ps
, 0, REGF_BLOCKSIZE
) != -1 ) {
1323 /* now use for writing */
1324 prs_switch_type( &ps
, MARSHALL
);
1326 /* stream the block once, generate the checksum,
1327 and stream it again */
1328 prs_set_offset( &ps
, 0 );
1329 prs_regf_block( "regf_blocK", &ps
, 0, file
);
1330 file
->checksum
= regf_block_checksum( &ps
);
1331 prs_set_offset( &ps
, 0 );
1332 prs_regf_block( "regf_blocK", &ps
, 0, file
);
1334 /* now we are ready to write it to disk */
1335 if ( write_block( file
, &ps
, 0 ) == -1 )
1336 DEBUG(0,("regfio_close: failed to update the regf header block!\n"));
1339 prs_mem_free( &ps
);
1342 regfio_mem_free( file
);
1344 /* nothing tdo do if there is no open file */
1356 /*******************************************************************
1357 *******************************************************************/
1359 static void regfio_flush( REGF_FILE
*file
)
1363 for ( hbin
=file
->block_list
; hbin
; hbin
=hbin
->next
) {
1364 write_hbin_block( file
, hbin
);
1368 /*******************************************************************
1369 There should be only *one* root key in the registry file based
1370 on my experience. --jerry
1371 *******************************************************************/
1373 REGF_NK_REC
* regfio_rootkey( REGF_FILE
*file
)
1377 uint32_t offset
= REGF_BLOCKSIZE
;
1384 if ( !(nk
= talloc_zero( file
->mem_ctx
, REGF_NK_REC
)) ) {
1385 DEBUG(0,("regfio_rootkey: talloc() failed!\n"));
1389 /* scan through the file on HBIN block at a time looking
1390 for an NK record with a type == 0x002c.
1391 Normally this is the first nk record in the first hbin
1392 block (but I'm not assuming that for now) */
1394 while ( (hbin
= read_hbin_block( file
, offset
)) ) {
1398 if ( next_nk_record( file
, hbin
, nk
, &eob
) ) {
1399 if ( nk
->key_type
== NK_TYPE_ROOTKEY
) {
1404 prs_mem_free( &hbin
->ps
);
1410 offset
+= hbin
->block_size
;
1414 DEBUG(0,("regfio_rootkey: corrupt registry file ? No root key record located\n"));
1418 DLIST_ADD( file
->block_list
, hbin
);
1423 /*******************************************************************
1424 This acts as an interator over the subkeys defined for a given
1425 NK record. Remember that offsets are from the *first* HBIN block.
1426 *******************************************************************/
1428 REGF_NK_REC
* regfio_fetch_subkey( REGF_FILE
*file
, REGF_NK_REC
*nk
)
1430 REGF_NK_REC
*subkey
;
1434 /* see if there is anything left to report */
1436 if ( !nk
|| (nk
->subkeys_off
==REGF_OFFSET_NONE
) || (nk
->subkey_index
>= nk
->num_subkeys
) )
1439 /* find the HBIN block which should contain the nk record */
1441 if ( !(hbin
= lookup_hbin_block( file
, nk
->subkeys
.hashes
[nk
->subkey_index
].nk_off
)) ) {
1442 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n",
1443 nk
->subkeys
.hashes
[nk
->subkey_index
].nk_off
));
1447 nk_offset
= nk
->subkeys
.hashes
[nk
->subkey_index
].nk_off
;
1448 if ( !prs_set_offset( &hbin
->ps
, (HBIN_HDR_SIZE
+ nk_offset
- hbin
->first_hbin_off
) ) )
1452 if ( !(subkey
= talloc_zero( file
->mem_ctx
, REGF_NK_REC
)) )
1455 if ( !hbin_prs_key( file
, hbin
, subkey
) )
1462 /*******************************************************************
1463 *******************************************************************/
1465 static REGF_HBIN
* regf_hbin_allocate( REGF_FILE
*file
, uint32_t block_size
)
1468 SMB_STRUCT_STAT sbuf
;
1470 if ( !(hbin
= talloc_zero( file
->mem_ctx
, REGF_HBIN
)) )
1473 memcpy( hbin
->header
, "hbin", HBIN_HDR_SIZE
);
1476 if (sys_fstat(file
->fd
, &sbuf
, false)) {
1477 DEBUG(0,("regf_hbin_allocate: stat() failed! (%s)\n", strerror(errno
)));
1481 hbin
->file_off
= sbuf
.st_ex_size
;
1483 hbin
->free_off
= HBIN_HEADER_REC_SIZE
;
1484 hbin
->free_size
= block_size
- hbin
->free_off
+ sizeof(uint32_t);
1486 hbin
->block_size
= block_size
;
1487 hbin
->first_hbin_off
= hbin
->file_off
- REGF_BLOCKSIZE
;
1489 if ( !prs_init( &hbin
->ps
, block_size
, file
->mem_ctx
, MARSHALL
) )
1492 if ( !prs_hbin_block( "new_hbin", &hbin
->ps
, 0, hbin
) )
1495 if ( !write_hbin_block( file
, hbin
) )
1498 file
->last_block
= hbin
->file_off
;
1503 /*******************************************************************
1504 *******************************************************************/
1506 static void update_free_space( REGF_HBIN
*hbin
, uint32_t size_used
)
1508 hbin
->free_off
+= size_used
;
1509 hbin
->free_size
-= size_used
;
1511 if ( hbin
->free_off
>= hbin
->block_size
) {
1512 hbin
->free_off
= REGF_OFFSET_NONE
;
1518 /*******************************************************************
1519 *******************************************************************/
1521 static REGF_HBIN
* find_free_space( REGF_FILE
*file
, uint32_t size
)
1523 REGF_HBIN
*hbin
, *p_hbin
;
1527 /* check open block list */
1529 for ( hbin
=file
->block_list
; hbin
!=NULL
; hbin
=hbin
->next
) {
1530 /* only check blocks that actually have available space */
1532 if ( hbin
->free_off
== REGF_OFFSET_NONE
)
1535 /* check for a large enough available chunk */
1537 if ( (hbin
->block_size
- hbin
->free_off
) >= size
) {
1538 DLIST_PROMOTE( file
->block_list
, hbin
);
1543 /* parse the file until we find a block with
1544 enough free space; save the last non-filled hbin */
1546 block_off
= REGF_BLOCKSIZE
;
1548 /* cleanup before the next round */
1551 prs_mem_free( &hbin
->ps
);
1553 hbin
= read_hbin_block( file
, block_off
);
1557 /* make sure that we don't already have this block in memory */
1559 for ( p_hbin
=file
->block_list
; p_hbin
!=NULL
; p_hbin
=p_hbin
->next
) {
1560 if ( p_hbin
->file_off
== hbin
->file_off
) {
1566 block_off
= hbin
->file_off
+ hbin
->block_size
;
1569 prs_mem_free( &hbin
->ps
);
1574 /* if (cached block or (new block and not enough free space)) then continue looping */
1575 } while ( cached
|| (hbin
&& (hbin
->free_size
< size
)) );
1577 /* no free space; allocate a new one */
1580 uint32_t alloc_size
;
1582 /* allocate in multiples of REGF_ALLOC_BLOCK; make sure (size + hbin_header) fits */
1584 alloc_size
= (((size
+HBIN_HEADER_REC_SIZE
) / REGF_ALLOC_BLOCK
) + 1 ) * REGF_ALLOC_BLOCK
;
1586 if ( !(hbin
= regf_hbin_allocate( file
, alloc_size
)) ) {
1587 DEBUG(0,("find_free_space: regf_hbin_allocate() failed!\n"));
1590 DLIST_ADD( file
->block_list
, hbin
);
1594 /* set the offset to be ready to write */
1596 if ( !prs_set_offset( &hbin
->ps
, hbin
->free_off
-sizeof(uint32_t) ) )
1599 /* write the record size as a placeholder for now, it should be
1600 probably updated by the caller once it all of the data necessary
1603 if ( !prs_uint32("allocated_size", &hbin
->ps
, 0, &size
) )
1606 update_free_space( hbin
, size
);
1611 /*******************************************************************
1612 *******************************************************************/
1614 static uint32_t sk_record_data_size( struct security_descriptor
* sd
)
1616 uint32_t size
, size_mod8
;
1620 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1622 size
= sizeof(uint32_t)*5 + ndr_size_security_descriptor(sd
, 0) + sizeof(uint32_t);
1625 size_mod8
= size
& 0xfffffff8;
1626 if ( size_mod8
< size
)
1632 /*******************************************************************
1633 *******************************************************************/
1635 static uint32_t vk_record_data_size( REGF_VK_REC
*vk
)
1637 uint32_t size
, size_mod8
;
1641 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1643 size
= REC_HDR_SIZE
+ (sizeof(uint16_t)*3) + (sizeof(uint32_t)*3) + sizeof(uint32_t);
1645 if ( vk
->valuename
)
1646 size
+= strlen(vk
->valuename
);
1649 size_mod8
= size
& 0xfffffff8;
1650 if ( size_mod8
< size
)
1656 /*******************************************************************
1657 *******************************************************************/
1659 static uint32_t lf_record_data_size( uint32_t num_keys
)
1661 uint32_t size
, size_mod8
;
1665 /* the record size is sizeof(hdr) + num_keys + sizeof of hash_array + data_size_uint32_t */
1667 size
= REC_HDR_SIZE
+ sizeof(uint16_t) + (sizeof(REGF_HASH_REC
) * num_keys
) + sizeof(uint32_t);
1670 size_mod8
= size
& 0xfffffff8;
1671 if ( size_mod8
< size
)
1677 /*******************************************************************
1678 *******************************************************************/
1680 static uint32_t nk_record_data_size( REGF_NK_REC
*nk
)
1682 uint32_t size
, size_mod8
;
1686 /* the record size is static + length_of_keyname + length_of_classname + data_size_uint32_t */
1688 size
= 0x4c + strlen(nk
->keyname
) + sizeof(uint32_t);
1690 if ( nk
->classname
)
1691 size
+= strlen( nk
->classname
);
1694 size_mod8
= size
& 0xfffffff8;
1695 if ( size_mod8
< size
)
1701 /*******************************************************************
1702 *******************************************************************/
1704 static bool create_vk_record(REGF_FILE
*file
, REGF_VK_REC
*vk
,
1705 struct regval_blob
*value
)
1707 char *name
= regval_name(value
);
1708 REGF_HBIN
*data_hbin
;
1712 memcpy( vk
->header
, "vk", REC_HDR_SIZE
);
1715 vk
->valuename
= talloc_strdup( file
->mem_ctx
, regval_name(value
) );
1716 vk
->flag
= VK_FLAG_NAME_PRESENT
;
1719 vk
->data_size
= regval_size( value
);
1720 vk
->type
= regval_type( value
);
1722 if ( vk
->data_size
> sizeof(uint32_t) ) {
1723 uint32_t data_size
= ( (vk
->data_size
+sizeof(uint32_t)) & 0xfffffff8 ) + 8;
1725 vk
->data
= (uint8_t *)talloc_memdup( file
->mem_ctx
,
1726 regval_data_p(value
),
1728 if (vk
->data
== NULL
) {
1732 /* go ahead and store the offset....we'll pick this hbin block back up when
1733 we stream the data */
1735 if ((data_hbin
= find_free_space(file
, data_size
)) == NULL
) {
1738 vk
->data_off
= prs_offset( &data_hbin
->ps
) + data_hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1741 /* make sure we don't try to copy from a NULL value pointer */
1743 if ( vk
->data_size
!= 0 )
1744 memcpy( &vk
->data_off
, regval_data_p(value
), vk
->data_size
);
1745 vk
->data_size
|= VK_DATA_IN_OFFSET
;
1751 /*******************************************************************
1752 *******************************************************************/
1754 static int hashrec_cmp( REGF_HASH_REC
*h1
, REGF_HASH_REC
*h2
)
1756 return strcasecmp_m( h1
->fullname
, h2
->fullname
);
1759 /*******************************************************************
1760 *******************************************************************/
1762 REGF_NK_REC
* regfio_write_key( REGF_FILE
*file
, const char *name
,
1763 struct regval_ctr
*values
, struct regsubkey_ctr
*subkeys
,
1764 struct security_descriptor
*sec_desc
, REGF_NK_REC
*parent
)
1767 REGF_HBIN
*vlist_hbin
= NULL
;
1770 if ( !(nk
= talloc_zero( file
->mem_ctx
, REGF_NK_REC
)) )
1773 memcpy( nk
->header
, "nk", REC_HDR_SIZE
);
1776 nk
->key_type
= NK_TYPE_ROOTKEY
;
1778 nk
->key_type
= NK_TYPE_NORMALKEY
;
1780 /* store the parent offset (or -1 if a the root key */
1782 nk
->parent_off
= parent
? (parent
->hbin_off
+ parent
->hbin
->file_off
- REGF_BLOCKSIZE
- HBIN_HDR_SIZE
) : REGF_OFFSET_NONE
;
1784 /* no classname currently */
1786 nk
->classname_off
= REGF_OFFSET_NONE
;
1787 nk
->classname
= NULL
;
1788 nk
->keyname
= talloc_strdup( file
->mem_ctx
, name
);
1790 /* current modification time */
1792 unix_to_nt_time( &nk
->mtime
, time(NULL
) );
1794 /* allocate the record on disk */
1796 size
= nk_record_data_size( nk
);
1797 nk
->rec_size
= ( size
- 1 ) ^ 0XFFFFFFFF;
1798 if ((nk
->hbin
= find_free_space( file
, size
)) == NULL
) {
1801 nk
->hbin_off
= prs_offset( &nk
->hbin
->ps
);
1803 /* Update the hash record in the parent */
1806 REGF_HASH_REC
*hash
= &parent
->subkeys
.hashes
[parent
->subkey_index
];
1808 hash
->nk_off
= prs_offset( &nk
->hbin
->ps
) + nk
->hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1809 memcpy(hash
->keycheck
, name
, MIN(strlen(name
),sizeof(uint32_t)));
1810 hash
->fullname
= talloc_strdup( file
->mem_ctx
, name
);
1811 parent
->subkey_index
++;
1813 /* sort the list by keyname */
1814 TYPESAFE_QSORT(parent
->subkeys
.hashes
, parent
->subkey_index
, hashrec_cmp
);
1816 if ( !hbin_prs_lf_records( "lf_rec", parent
->subkeys
.hbin
, 0, parent
) )
1820 /* write the security descriptor */
1822 nk
->sk_off
= REGF_OFFSET_NONE
;
1824 uint32_t sk_size
= sk_record_data_size( sec_desc
);
1827 /* search for it in the existing list of sd's */
1829 if ( (nk
->sec_desc
= find_sk_record_by_sec_desc( file
, sec_desc
)) == NULL
) {
1830 /* not found so add it to the list */
1832 if (!(sk_hbin
= find_free_space( file
, sk_size
))) {
1836 if ( !(nk
->sec_desc
= talloc_zero( file
->mem_ctx
, REGF_SK_REC
)) )
1839 /* now we have to store the security descriptor in the list and
1840 update the offsets */
1842 memcpy( nk
->sec_desc
->header
, "sk", REC_HDR_SIZE
);
1843 nk
->sec_desc
->hbin
= sk_hbin
;
1844 nk
->sec_desc
->hbin_off
= prs_offset( &sk_hbin
->ps
);
1845 nk
->sec_desc
->sk_off
= prs_offset( &sk_hbin
->ps
) + sk_hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1846 nk
->sec_desc
->rec_size
= (sk_size
-1) ^ 0xFFFFFFFF;
1848 nk
->sec_desc
->sec_desc
= sec_desc
;
1849 nk
->sec_desc
->ref_count
= 0;
1851 /* size value must be self-inclusive */
1852 nk
->sec_desc
->size
= ndr_size_security_descriptor(sec_desc
, 0)
1855 DLIST_ADD_END( file
->sec_desc_list
, nk
->sec_desc
);
1857 /* update the offsets for us and the previous sd in the list.
1858 if this is the first record, then just set the next and prev
1859 offsets to ourself. */
1861 if ( DLIST_PREV(nk
->sec_desc
) ) {
1862 REGF_SK_REC
*prev
= DLIST_PREV(nk
->sec_desc
);
1864 nk
->sec_desc
->prev_sk_off
= prev
->hbin_off
+ prev
->hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1865 prev
->next_sk_off
= nk
->sec_desc
->sk_off
;
1867 /* the end must loop around to the front */
1868 nk
->sec_desc
->next_sk_off
= file
->sec_desc_list
->sk_off
;
1870 /* and first must loop around to the tail */
1871 file
->sec_desc_list
->prev_sk_off
= nk
->sec_desc
->sk_off
;
1873 nk
->sec_desc
->prev_sk_off
= nk
->sec_desc
->sk_off
;
1874 nk
->sec_desc
->next_sk_off
= nk
->sec_desc
->sk_off
;
1878 /* bump the reference count +1 */
1880 nk
->sk_off
= nk
->sec_desc
->sk_off
;
1881 nk
->sec_desc
->ref_count
++;
1884 /* write the subkeys */
1886 nk
->subkeys_off
= REGF_OFFSET_NONE
;
1887 if ( (nk
->num_subkeys
= regsubkey_ctr_numkeys( subkeys
)) != 0 ) {
1888 uint32_t lf_size
= lf_record_data_size( nk
->num_subkeys
);
1892 if (!(nk
->subkeys
.hbin
= find_free_space( file
, lf_size
))) {
1895 nk
->subkeys
.hbin_off
= prs_offset( &nk
->subkeys
.hbin
->ps
);
1896 nk
->subkeys
.rec_size
= (lf_size
-1) ^ 0xFFFFFFFF;
1897 nk
->subkeys_off
= prs_offset( &nk
->subkeys
.hbin
->ps
) + nk
->subkeys
.hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1899 memcpy( nk
->subkeys
.header
, "lf", REC_HDR_SIZE
);
1901 nk
->subkeys
.num_keys
= nk
->num_subkeys
;
1902 if (nk
->subkeys
.num_keys
) {
1903 if ( !(nk
->subkeys
.hashes
= talloc_zero_array( file
->mem_ctx
, REGF_HASH_REC
, nk
->subkeys
.num_keys
)) )
1906 nk
->subkeys
.hashes
= NULL
;
1908 nk
->subkey_index
= 0;
1910 /* update the max_bytes_subkey{name,classname} fields */
1911 for ( i
=0; i
<nk
->num_subkeys
; i
++ ) {
1912 namelen
= strlen( regsubkey_ctr_specific_key(subkeys
, i
) );
1913 if ( namelen
*2 > nk
->max_bytes_subkeyname
)
1914 nk
->max_bytes_subkeyname
= namelen
* 2;
1918 /* write the values */
1920 nk
->values_off
= REGF_OFFSET_NONE
;
1921 if ( (nk
->num_values
= regval_ctr_numvals( values
)) != 0 ) {
1922 uint32_t vlist_size
= ( ( nk
->num_values
* sizeof(uint32_t) ) & 0xfffffff8 ) + 8;
1925 if (!(vlist_hbin
= find_free_space( file
, vlist_size
))) {
1928 nk
->values_off
= prs_offset( &vlist_hbin
->ps
) + vlist_hbin
->first_hbin_off
- HBIN_HDR_SIZE
;
1930 if (nk
->num_values
) {
1931 if ( !(nk
->values
= talloc_array( file
->mem_ctx
, REGF_VK_REC
, nk
->num_values
)) )
1937 /* create the vk records */
1939 for ( i
=0; i
<nk
->num_values
; i
++ ) {
1940 uint32_t vk_size
, namelen
, datalen
;
1941 struct regval_blob
*r
;
1943 r
= regval_ctr_specific_value( values
, i
);
1944 create_vk_record( file
, &nk
->values
[i
], r
);
1945 vk_size
= vk_record_data_size( &nk
->values
[i
] );
1946 nk
->values
[i
].hbin
= find_free_space( file
, vk_size
);
1947 nk
->values
[i
].hbin_off
= prs_offset( &nk
->values
[i
].hbin
->ps
);
1948 nk
->values
[i
].rec_size
= ( vk_size
- 1 ) ^ 0xFFFFFFFF;
1949 nk
->values
[i
].rec_off
= prs_offset( &nk
->values
[i
].hbin
->ps
)
1950 + nk
->values
[i
].hbin
->first_hbin_off
1953 /* update the max bytes fields if necessary */
1955 namelen
= strlen( regval_name(r
) );
1956 if ( namelen
*2 > nk
->max_bytes_valuename
)
1957 nk
->max_bytes_valuename
= namelen
* 2;
1959 datalen
= regval_size( r
);
1960 if ( datalen
> nk
->max_bytes_value
)
1961 nk
->max_bytes_value
= datalen
;
1965 /* stream the records */
1967 prs_set_offset( &nk
->hbin
->ps
, nk
->hbin_off
);
1968 if ( !prs_nk_rec( "nk_rec", &nk
->hbin
->ps
, 0, nk
) )
1971 if ( nk
->num_values
) {
1972 if ( !hbin_prs_vk_records( "vk_records", vlist_hbin
, 0, nk
, file
) )
1977 regfio_flush( file
);