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"
26 #define DBGC_CLASS DBGC_REGISTRY
28 /*******************************************************************
30 * TODO : Right now this code basically ignores classnames.
32 ******************************************************************/
34 #if defined(PARANOID_MALLOC_CHECKER)
35 #define PRS_ALLOC_MEM(ps, type, count) (type *)prs_alloc_mem_((ps),sizeof(type),(count))
37 #define PRS_ALLOC_MEM(ps, type, count) (type *)prs_alloc_mem((ps),sizeof(type),(count))
40 /*******************************************************************
41 Reads or writes an NTTIME structure.
42 ********************************************************************/
44 static bool smb_io_time(const char *desc, NTTIME *nttime, prs_struct *ps, int depth)
50 prs_debug(ps, depth, desc, "smb_io_time");
56 if (MARSHALLING(ps)) {
57 low = *nttime & 0xFFFFFFFF;
61 if(!prs_uint32("low ", ps, depth, &low)) /* low part */
63 if(!prs_uint32("high", ps, depth, &high)) /* high part */
66 if (UNMARSHALLING(ps)) {
67 *nttime = (((uint64_t)high << 32) + low);
73 /*******************************************************************
74 *******************************************************************/
76 static int write_block( REGF_FILE *file, prs_struct *ps, uint32 offset )
78 int bytes_written, returned;
79 char *buffer = prs_data_p( ps );
80 uint32 buffer_size = prs_data_size( ps );
86 /* check for end of file */
88 if (sys_fstat(file->fd, &sbuf, false)) {
89 DEBUG(0,("write_block: stat() failed! (%s)\n", strerror(errno)));
93 if ( lseek( file->fd, offset, SEEK_SET ) == -1 ) {
94 DEBUG(0,("write_block: lseek() failed! (%s)\n", strerror(errno) ));
98 bytes_written = returned = 0;
99 while ( bytes_written < buffer_size ) {
100 if ( (returned = write( file->fd, buffer+bytes_written, buffer_size-bytes_written )) == -1 ) {
101 DEBUG(0,("write_block: write() failed! (%s)\n", strerror(errno) ));
105 bytes_written += returned;
108 return bytes_written;
111 /*******************************************************************
112 *******************************************************************/
114 static int read_block( REGF_FILE *file, prs_struct *ps, uint32 file_offset, uint32 block_size )
116 int bytes_read, returned;
118 SMB_STRUCT_STAT sbuf;
120 /* check for end of file */
122 if (sys_fstat(file->fd, &sbuf, false)) {
123 DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno)));
127 if ( (size_t)file_offset >= sbuf.st_ex_size )
130 /* if block_size == 0, we are parsing HBIN records and need
131 to read some of the header to get the block_size from there */
133 if ( block_size == 0 ) {
136 if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
137 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
141 returned = read( file->fd, hdr, 0x20 );
142 if ( (returned == -1) || (returned < 0x20) ) {
143 DEBUG(0,("read_block: failed to read in HBIN header. Is the file corrupt?\n"));
147 /* make sure this is an hbin header */
149 if ( strncmp( hdr, "hbin", HBIN_HDR_SIZE ) != 0 ) {
150 DEBUG(0,("read_block: invalid block header!\n"));
154 block_size = IVAL( hdr, 0x08 );
157 DEBUG(10,("read_block: block_size == 0x%x\n", block_size ));
159 /* set the offset, initialize the buffer, and read the block from disk */
161 if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
162 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
166 if (!prs_init( ps, block_size, file->mem_ctx, UNMARSHALL )) {
167 DEBUG(0,("read_block: prs_init() failed! (%s)\n", strerror(errno) ));
170 buffer = prs_data_p( ps );
171 bytes_read = returned = 0;
173 while ( bytes_read < block_size ) {
174 if ( (returned = read( file->fd, buffer+bytes_read, block_size-bytes_read )) == -1 ) {
175 DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno) ));
178 if ( (returned == 0) && (bytes_read < block_size) ) {
179 DEBUG(0,("read_block: not a vald registry file ?\n" ));
183 bytes_read += returned;
189 /*******************************************************************
190 *******************************************************************/
192 static bool write_hbin_block( REGF_FILE *file, REGF_HBIN *hbin )
197 /* write free space record if any is available */
199 if ( hbin->free_off != REGF_OFFSET_NONE ) {
200 uint32 header = 0xffffffff;
202 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) )
204 if ( !prs_uint32( "free_size", &hbin->ps, 0, &hbin->free_size ) )
206 if ( !prs_uint32( "free_header", &hbin->ps, 0, &header ) )
210 hbin->dirty = (write_block( file, &hbin->ps, hbin->file_off ) != -1);
215 /*******************************************************************
216 *******************************************************************/
218 static bool hbin_block_close( REGF_FILE *file, REGF_HBIN *hbin )
222 /* remove the block from the open list and flush it to disk */
224 for ( p=file->block_list; p && p!=hbin; p=p->next )
228 DLIST_REMOVE( file->block_list, hbin );
231 DEBUG(0,("hbin_block_close: block not in open list!\n"));
233 if ( !write_hbin_block( file, hbin ) )
239 /*******************************************************************
240 *******************************************************************/
242 static bool prs_regf_block( const char *desc, prs_struct *ps, int depth, REGF_FILE *file )
244 prs_debug(ps, depth, desc, "prs_regf_block");
247 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)file->header, sizeof( file->header )) )
250 /* yes, these values are always identical so store them only once */
252 if ( !prs_uint32( "unknown1", ps, depth, &file->unknown1 ))
254 if ( !prs_uint32( "unknown1 (again)", ps, depth, &file->unknown1 ))
257 /* get the modtime */
259 if ( !prs_set_offset( ps, 0x0c ) )
261 if ( !smb_io_time( "modtime", &file->mtime, ps, depth ) )
266 if ( !prs_uint32( "unknown2", ps, depth, &file->unknown2 ))
268 if ( !prs_uint32( "unknown3", ps, depth, &file->unknown3 ))
270 if ( !prs_uint32( "unknown4", ps, depth, &file->unknown4 ))
272 if ( !prs_uint32( "unknown5", ps, depth, &file->unknown5 ))
275 /* get file offsets */
277 if ( !prs_set_offset( ps, 0x24 ) )
279 if ( !prs_uint32( "data_offset", ps, depth, &file->data_offset ))
281 if ( !prs_uint32( "last_block", ps, depth, &file->last_block ))
284 /* one more constant */
286 if ( !prs_uint32( "unknown6", ps, depth, &file->unknown6 ))
289 /* get the checksum */
291 if ( !prs_set_offset( ps, 0x01fc ) )
293 if ( !prs_uint32( "checksum", ps, depth, &file->checksum ))
299 /*******************************************************************
300 *******************************************************************/
302 static bool prs_hbin_block( const char *desc, prs_struct *ps, int depth, REGF_HBIN *hbin )
306 prs_debug(ps, depth, desc, "prs_regf_block");
309 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)hbin->header, sizeof( hbin->header )) )
312 if ( !prs_uint32( "first_hbin_off", ps, depth, &hbin->first_hbin_off ))
315 /* The dosreg.cpp comments say that the block size is at 0x1c.
316 According to a WINXP NTUSER.dat file, this is wrong. The block_size
319 if ( !prs_uint32( "block_size", ps, depth, &hbin->block_size ))
322 block_size2 = hbin->block_size;
323 prs_set_offset( ps, 0x1c );
324 if ( !prs_uint32( "block_size2", ps, depth, &block_size2 ))
327 if ( MARSHALLING(ps) )
334 /*******************************************************************
335 *******************************************************************/
337 static bool prs_nk_rec( const char *desc, prs_struct *ps, int depth, REGF_NK_REC *nk )
339 uint16 class_length, name_length;
341 uint32 data_size, start_off, end_off;
342 uint32 unknown_off = REGF_OFFSET_NONE;
344 nk->hbin_off = prs_offset( ps );
345 start = nk->hbin_off;
347 prs_debug(ps, depth, desc, "prs_nk_rec");
350 /* back up and get the data_size */
352 if ( !prs_set_offset( ps, prs_offset(ps)-sizeof(uint32)) )
354 start_off = prs_offset( ps );
355 if ( !prs_uint32( "rec_size", ps, depth, &nk->rec_size ))
358 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)nk->header, sizeof( nk->header )) )
361 if ( !prs_uint16( "key_type", ps, depth, &nk->key_type ))
363 if ( !smb_io_time( "mtime", &nk->mtime, ps, depth ))
366 if ( !prs_set_offset( ps, start+0x0010 ) )
368 if ( !prs_uint32( "parent_off", ps, depth, &nk->parent_off ))
370 if ( !prs_uint32( "num_subkeys", ps, depth, &nk->num_subkeys ))
373 if ( !prs_set_offset( ps, start+0x001c ) )
375 if ( !prs_uint32( "subkeys_off", ps, depth, &nk->subkeys_off ))
377 if ( !prs_uint32( "unknown_off", ps, depth, &unknown_off) )
380 if ( !prs_set_offset( ps, start+0x0024 ) )
382 if ( !prs_uint32( "num_values", ps, depth, &nk->num_values ))
384 if ( !prs_uint32( "values_off", ps, depth, &nk->values_off ))
386 if ( !prs_uint32( "sk_off", ps, depth, &nk->sk_off ))
388 if ( !prs_uint32( "classname_off", ps, depth, &nk->classname_off ))
391 if ( !prs_uint32( "max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname))
393 if ( !prs_uint32( "max_bytes_subkeyclassname", ps, depth, &nk->max_bytes_subkeyclassname))
395 if ( !prs_uint32( "max_bytes_valuename", ps, depth, &nk->max_bytes_valuename))
397 if ( !prs_uint32( "max_bytes_value", ps, depth, &nk->max_bytes_value))
399 if ( !prs_uint32( "unknown index", ps, depth, &nk->unk_index))
402 name_length = nk->keyname ? strlen(nk->keyname) : 0 ;
403 class_length = nk->classname ? strlen(nk->classname) : 0 ;
404 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
406 if ( !prs_uint16( "class_length", ps, depth, &class_length ))
409 if ( class_length ) {
414 if ( UNMARSHALLING(ps) ) {
415 if ( !(nk->keyname = PRS_ALLOC_MEM( ps, char, name_length+1 )) )
419 if ( !prs_uint8s( True, "name", ps, depth, (uint8*)nk->keyname, name_length) )
422 if ( UNMARSHALLING(ps) )
423 nk->keyname[name_length] = '\0';
426 end_off = prs_offset( ps );
428 /* data_size must be divisible by 8 and large enough to hold the original record */
430 data_size = ((start_off - end_off) & 0xfffffff8 );
431 if ( data_size > nk->rec_size )
432 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size));
434 if ( MARSHALLING(ps) )
435 nk->hbin->dirty = True;
440 /*******************************************************************
441 *******************************************************************/
443 static uint32 regf_block_checksum( prs_struct *ps )
445 char *buffer = prs_data_p( ps );
449 /* XOR of all bytes 0x0000 - 0x01FB */
453 for ( i=0; i<0x01FB; i+=4 ) {
454 x = IVAL(buffer, i );
461 /*******************************************************************
462 *******************************************************************/
464 static bool read_regf_block( REGF_FILE *file )
469 /* grab the first block from the file */
471 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) == -1 )
474 /* parse the block and verify the checksum */
476 if ( !prs_regf_block( "regf_header", &ps, 0, file ) )
479 checksum = regf_block_checksum( &ps );
483 if ( file->checksum != checksum ) {
484 DEBUG(0,("read_regf_block: invalid checksum\n" ));
491 /*******************************************************************
492 *******************************************************************/
494 static REGF_HBIN* read_hbin_block( REGF_FILE *file, off_t offset )
497 uint32 record_size, curr_off, block_size, header;
499 if ( !(hbin = TALLOC_ZERO_P(file->mem_ctx, REGF_HBIN)) )
501 hbin->file_off = offset;
504 if ( read_block( file, &hbin->ps, offset, 0 ) == -1 )
507 if ( !prs_hbin_block( "hbin", &hbin->ps, 0, hbin ) )
510 /* this should be the same thing as hbin->block_size but just in case */
512 block_size = prs_data_size( &hbin->ps );
514 /* Find the available free space offset. Always at the end,
515 so walk the record list and stop when you get to the end.
516 The end is defined by a record header of 0xffffffff. The
517 previous 4 bytes contains the amount of free space remaining
518 in the hbin block. */
520 /* remember that the record_size is in the 4 bytes preceeding the record itself */
522 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE-sizeof(uint32) ) )
527 curr_off = prs_offset( &hbin->ps );
528 while ( header != 0xffffffff ) {
529 /* not done yet so reset the current offset to the
530 next record_size field */
532 curr_off = curr_off+record_size;
534 /* for some reason the record_size of the last record in
535 an hbin block can extend past the end of the block
536 even though the record fits within the remaining
537 space....aaarrrgggghhhhhh */
539 if ( curr_off >= block_size ) {
545 if ( !prs_set_offset( &hbin->ps, curr_off) )
548 if ( !prs_uint32( "rec_size", &hbin->ps, 0, &record_size ) )
550 if ( !prs_uint32( "header", &hbin->ps, 0, &header ) )
553 SMB_ASSERT( record_size != 0 );
555 if ( record_size & 0x80000000 ) {
556 /* absolute_value(record_size) */
557 record_size = (record_size ^ 0xffffffff) + 1;
561 /* save the free space offset */
563 if ( header == 0xffffffff ) {
565 /* account for the fact that the curr_off is 4 bytes behind the actual
568 hbin->free_off = curr_off + sizeof(uint32);
569 hbin->free_size = record_size;
572 DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin->free_off));
574 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE ) )
580 /*******************************************************************
581 Input a random offset and receive the corresponding HBIN
583 *******************************************************************/
585 static bool hbin_contains_offset( REGF_HBIN *hbin, uint32 offset )
590 if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) )
596 /*******************************************************************
597 Input a random offset and receive the corresponding HBIN
599 *******************************************************************/
601 static REGF_HBIN* lookup_hbin_block( REGF_FILE *file, uint32 offset )
603 REGF_HBIN *hbin = NULL;
606 /* start with the open list */
608 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
609 DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%lx]\n", hbin->file_off, (unsigned long)hbin ));
610 if ( hbin_contains_offset( hbin, offset ) )
615 /* start at the beginning */
617 block_off = REGF_BLOCKSIZE;
619 /* cleanup before the next round */
621 prs_mem_free( &hbin->ps );
623 hbin = read_hbin_block( file, block_off );
626 block_off = hbin->file_off + hbin->block_size;
628 } while ( hbin && !hbin_contains_offset( hbin, offset ) );
632 DLIST_ADD( file->block_list, hbin );
637 /*******************************************************************
638 *******************************************************************/
640 static bool prs_hash_rec( const char *desc, prs_struct *ps, int depth, REGF_HASH_REC *hash )
642 prs_debug(ps, depth, desc, "prs_hash_rec");
645 if ( !prs_uint32( "nk_off", ps, depth, &hash->nk_off ))
647 if ( !prs_uint8s( True, "keycheck", ps, depth, hash->keycheck, sizeof( hash->keycheck )) )
653 /*******************************************************************
654 *******************************************************************/
656 static bool hbin_prs_lf_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk )
659 REGF_LF_REC *lf = &nk->subkeys;
660 uint32 data_size, start_off, end_off;
662 prs_debug(&hbin->ps, depth, desc, "prs_lf_records");
665 /* check if we have anything to do first */
667 if ( nk->num_subkeys == 0 )
670 /* move to the LF record */
672 if ( !prs_set_offset( &hbin->ps, nk->subkeys_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
675 /* backup and get the data_size */
677 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
679 start_off = prs_offset( &hbin->ps );
680 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &lf->rec_size ))
683 if ( !prs_uint8s( True, "header", &hbin->ps, depth, (uint8*)lf->header, sizeof( lf->header )) )
686 if ( !prs_uint16( "num_keys", &hbin->ps, depth, &lf->num_keys))
689 if ( UNMARSHALLING(&hbin->ps) ) {
691 if ( !(lf->hashes = PRS_ALLOC_MEM( &hbin->ps, REGF_HASH_REC, lf->num_keys )) )
698 for ( i=0; i<lf->num_keys; i++ ) {
699 if ( !prs_hash_rec( "hash_rec", &hbin->ps, depth, &lf->hashes[i] ) )
703 end_off = prs_offset( &hbin->ps );
705 /* data_size must be divisible by 8 and large enough to hold the original record */
707 data_size = ((start_off - end_off) & 0xfffffff8 );
708 if ( data_size > lf->rec_size )
709 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->rec_size));
711 if ( MARSHALLING(&hbin->ps) )
717 /*******************************************************************
718 *******************************************************************/
720 static bool hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_SK_REC *sk )
722 prs_struct *ps = &hbin->ps;
724 uint32 data_size, start_off, end_off;
727 prs_debug(ps, depth, desc, "hbin_prs_sk_rec");
730 if ( !prs_set_offset( &hbin->ps, sk->sk_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
733 /* backup and get the data_size */
735 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
737 start_off = prs_offset( &hbin->ps );
738 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &sk->rec_size ))
741 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)sk->header, sizeof( sk->header )) )
743 if ( !prs_uint16( "tag", ps, depth, &tag))
746 if ( !prs_uint32( "prev_sk_off", ps, depth, &sk->prev_sk_off))
748 if ( !prs_uint32( "next_sk_off", ps, depth, &sk->next_sk_off))
750 if ( !prs_uint32( "ref_count", ps, depth, &sk->ref_count))
752 if ( !prs_uint32( "size", ps, depth, &sk->size))
757 TALLOC_CTX *mem_ctx = prs_get_mem_context(&hbin->ps);
760 if (MARSHALLING(&hbin->ps)) {
761 status = marshall_sec_desc(mem_ctx,
763 &blob.data, &blob.length);
764 if (!NT_STATUS_IS_OK(status))
766 if (!prs_copy_data_in(&hbin->ps, (const char *)blob.data, blob.length))
769 blob = data_blob_const(prs_data_p(&hbin->ps),
770 prs_data_size(&hbin->ps));
771 status = unmarshall_sec_desc(mem_ctx,
772 blob.data, blob.length,
774 if (!NT_STATUS_IS_OK(status))
776 prs_set_offset(&hbin->ps, blob.length);
780 end_off = prs_offset( &hbin->ps );
782 /* data_size must be divisible by 8 and large enough to hold the original record */
784 data_size = ((start_off - end_off) & 0xfffffff8 );
785 if ( data_size > sk->rec_size )
786 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->rec_size));
788 if ( MARSHALLING(&hbin->ps) )
794 /*******************************************************************
795 *******************************************************************/
797 static bool hbin_prs_vk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_VK_REC *vk, REGF_FILE *file )
801 prs_struct *ps = &hbin->ps;
802 uint32 data_size, start_off, end_off;
804 prs_debug(ps, depth, desc, "prs_vk_rec");
807 /* backup and get the data_size */
809 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
811 start_off = prs_offset( &hbin->ps );
812 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &vk->rec_size ))
815 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)vk->header, sizeof( vk->header )) )
818 if ( MARSHALLING(&hbin->ps) )
819 name_length = strlen(vk->valuename);
821 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
823 if ( !prs_uint32( "data_size", ps, depth, &vk->data_size ))
825 if ( !prs_uint32( "data_off", ps, depth, &vk->data_off ))
827 if ( !prs_uint32( "type", ps, depth, &vk->type))
829 if ( !prs_uint16( "flag", ps, depth, &vk->flag))
832 offset = prs_offset( ps );
833 offset += 2; /* skip 2 bytes */
834 prs_set_offset( ps, offset );
838 if ( vk->flag&VK_FLAG_NAME_PRESENT ) {
840 if ( UNMARSHALLING(&hbin->ps) ) {
841 if ( !(vk->valuename = PRS_ALLOC_MEM( ps, char, name_length+1 )))
844 if ( !prs_uint8s( True, "name", ps, depth, (uint8*)vk->valuename, name_length ) )
848 end_off = prs_offset( &hbin->ps );
850 /* get the data if necessary */
852 if ( vk->data_size != 0 ) {
853 bool charmode = False;
855 if ( (vk->type == REG_SZ) || (vk->type == REG_MULTI_SZ) )
858 /* the data is stored in the offset if the size <= 4 */
860 if ( !(vk->data_size & VK_DATA_IN_OFFSET) ) {
861 REGF_HBIN *hblock = hbin;
862 uint32 data_rec_size;
864 if ( UNMARSHALLING(&hbin->ps) ) {
865 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, vk->data_size) ) )
869 /* this data can be in another hbin */
870 if ( !hbin_contains_offset( hbin, vk->data_off ) ) {
871 if ( !(hblock = lookup_hbin_block( file, vk->data_off )) )
874 if ( !(prs_set_offset( &hblock->ps, (vk->data_off+HBIN_HDR_SIZE-hblock->first_hbin_off)-sizeof(uint32) )) )
877 if ( MARSHALLING(&hblock->ps) ) {
878 data_rec_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
879 data_rec_size = ( data_rec_size - 1 ) ^ 0xFFFFFFFF;
881 if ( !prs_uint32( "data_rec_size", &hblock->ps, depth, &data_rec_size ))
883 if ( !prs_uint8s( charmode, "data", &hblock->ps, depth, vk->data, vk->data_size) )
886 if ( MARSHALLING(&hblock->ps) )
887 hblock->dirty = True;
890 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, 4 ) ) )
892 SIVAL( vk->data, 0, vk->data_off );
897 /* data_size must be divisible by 8 and large enough to hold the original record */
899 data_size = ((start_off - end_off ) & 0xfffffff8 );
900 if ( data_size != vk->rec_size )
901 DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size, vk->rec_size));
903 if ( MARSHALLING(&hbin->ps) )
909 /*******************************************************************
910 read a VK record which is contained in the HBIN block stored
911 in the prs_struct *ps.
912 *******************************************************************/
914 static bool hbin_prs_vk_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk, REGF_FILE *file )
919 prs_debug(&hbin->ps, depth, desc, "prs_vk_records");
922 /* check if we have anything to do first */
924 if ( nk->num_values == 0 )
927 if ( UNMARSHALLING(&hbin->ps) ) {
928 if ( !(nk->values = PRS_ALLOC_MEM( &hbin->ps, REGF_VK_REC, nk->num_values ) ) )
932 /* convert the offset to something relative to this HBIN block */
934 if ( !prs_set_offset( &hbin->ps, nk->values_off+HBIN_HDR_SIZE-hbin->first_hbin_off-sizeof(uint32)) )
937 if ( MARSHALLING( &hbin->ps) ) {
938 record_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
939 record_size = (record_size - 1) ^ 0xFFFFFFFF;
942 if ( !prs_uint32( "record_size", &hbin->ps, depth, &record_size ) )
945 for ( i=0; i<nk->num_values; i++ ) {
946 if ( !prs_uint32( "vk_off", &hbin->ps, depth, &nk->values[i].rec_off ) )
950 for ( i=0; i<nk->num_values; i++ ) {
951 REGF_HBIN *sub_hbin = hbin;
954 if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) ) {
955 sub_hbin = lookup_hbin_block( file, nk->values[i].rec_off );
957 DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n",
958 nk->values[i].hbin_off));
963 new_offset = nk->values[i].rec_off + HBIN_HDR_SIZE - sub_hbin->first_hbin_off;
964 if ( !prs_set_offset( &sub_hbin->ps, new_offset ) )
966 if ( !hbin_prs_vk_rec( "vk_rec", sub_hbin, depth, &nk->values[i], file ) )
970 if ( MARSHALLING(&hbin->ps) )
978 /*******************************************************************
979 *******************************************************************/
981 static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32 offset )
985 for ( p_sk=file->sec_desc_list; p_sk; p_sk=p_sk->next ) {
986 if ( p_sk->sk_off == offset )
993 /*******************************************************************
994 *******************************************************************/
996 static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, struct security_descriptor *sd )
1000 for ( p=file->sec_desc_list; p; p=p->next ) {
1001 if ( security_descriptor_equal( p->sec_desc, sd ) )
1010 /*******************************************************************
1011 *******************************************************************/
1013 static bool hbin_prs_key( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk )
1016 REGF_HBIN *sub_hbin;
1018 prs_debug(&hbin->ps, depth, "", "fetch_key");
1021 /* get the initial nk record */
1023 if ( !prs_nk_rec( "nk_rec", &hbin->ps, depth, nk ))
1026 /* fill in values */
1028 if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) {
1030 if ( !hbin_contains_offset( hbin, nk->values_off ) ) {
1031 sub_hbin = lookup_hbin_block( file, nk->values_off );
1033 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n",
1039 if ( !hbin_prs_vk_records( "vk_rec", sub_hbin, depth, nk, file ))
1043 /* now get subkeys */
1045 if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) {
1047 if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) {
1048 sub_hbin = lookup_hbin_block( file, nk->subkeys_off );
1050 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n",
1056 if ( !hbin_prs_lf_records( "lf_rec", sub_hbin, depth, nk ))
1060 /* get the to the security descriptor. First look if we have already parsed it */
1062 if ( (nk->sk_off!=REGF_OFFSET_NONE) && !( nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )) ) {
1065 if ( !hbin_contains_offset( hbin, nk->sk_off ) ) {
1066 sub_hbin = lookup_hbin_block( file, nk->sk_off );
1068 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_offset [0x%x]\n",
1074 if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) )
1076 nk->sec_desc->sk_off = nk->sk_off;
1077 if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin, depth, nk->sec_desc ))
1080 /* add to the list of security descriptors (ref_count has been read from the files) */
1082 nk->sec_desc->sk_off = nk->sk_off;
1083 DLIST_ADD( file->sec_desc_list, nk->sec_desc );
1089 /*******************************************************************
1090 *******************************************************************/
1092 static bool next_record( REGF_HBIN *hbin, const char *hdr, bool *eob )
1094 uint8 header[REC_HDR_SIZE];
1096 uint32 curr_off, block_size;
1098 prs_struct *ps = &hbin->ps;
1100 curr_off = prs_offset( ps );
1101 if ( curr_off == 0 )
1102 prs_set_offset( ps, HBIN_HEADER_REC_SIZE );
1104 /* assume that the current offset is at the record header
1105 and we need to backup to read the record size */
1107 curr_off -= sizeof(uint32);
1109 block_size = prs_data_size( ps );
1111 memset( header, 0x0, sizeof(uint8)*REC_HDR_SIZE );
1114 curr_off = curr_off+record_size;
1115 if ( curr_off >= block_size )
1118 if ( !prs_set_offset( &hbin->ps, curr_off) )
1121 if ( !prs_uint32( "record_size", ps, 0, &record_size ) )
1123 if ( !prs_uint8s( True, "header", ps, 0, header, REC_HDR_SIZE ) )
1126 if ( record_size & 0x80000000 ) {
1127 /* absolute_value(record_size) */
1128 record_size = (record_size ^ 0xffffffff) + 1;
1131 if ( memcmp( header, hdr, REC_HDR_SIZE ) == 0 ) {
1133 curr_off += sizeof(uint32);
1137 /* mark prs_struct as done ( at end ) if no more SK records */
1138 /* mark end-of-block as True */
1141 prs_set_offset( &hbin->ps, prs_data_size(&hbin->ps) );
1146 if ( !prs_set_offset( ps, curr_off ) )
1152 /*******************************************************************
1153 *******************************************************************/
1155 static bool next_nk_record( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk, bool *eob )
1157 if ( next_record( hbin, "nk", eob ) && hbin_prs_key( file, hbin, nk ) )
1163 /*******************************************************************
1164 Intialize the newly created REGF_BLOCK in *file and write the
1165 block header to disk
1166 *******************************************************************/
1168 static bool init_regf_block( REGF_FILE *file )
1173 if ( !prs_init( &ps, REGF_BLOCKSIZE, file->mem_ctx, MARSHALL ) )
1176 memcpy( file->header, "regf", REGF_HDR_SIZE );
1177 file->data_offset = 0x20;
1178 file->last_block = 0x1000;
1182 unix_to_nt_time( &file->mtime, time(NULL) );
1184 /* hard coded values...no diea what these are ... maybe in time */
1186 file->unknown1 = 0x2;
1187 file->unknown2 = 0x1;
1188 file->unknown3 = 0x3;
1189 file->unknown4 = 0x0;
1190 file->unknown5 = 0x1;
1191 file->unknown6 = 0x1;
1193 /* write header to the buffer */
1195 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1200 /* calculate the checksum, re-marshall data (to include the checksum)
1201 and write to disk */
1203 file->checksum = regf_block_checksum( &ps );
1204 prs_set_offset( &ps, 0 );
1205 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1210 if ( write_block( file, &ps, 0 ) == -1 ) {
1211 DEBUG(0,("init_regf_block: Failed to initialize registry header block!\n"));
1217 prs_mem_free( &ps );
1221 /*******************************************************************
1222 Open the registry file and then read in the REGF block to get the
1224 *******************************************************************/
1226 REGF_FILE* regfio_open( const char *filename, int flags, int mode )
1230 if ( !(rb = SMB_MALLOC_P(REGF_FILE)) ) {
1231 DEBUG(0,("ERROR allocating memory\n"));
1237 if ( !(rb->mem_ctx = talloc_init( "read_regf_block" )) ) {
1242 rb->open_flags = flags;
1244 /* open and existing file */
1246 if ( (rb->fd = open(filename, flags, mode)) == -1 ) {
1247 DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename, strerror(errno)));
1252 /* check if we are creating a new file or overwriting an existing one */
1254 if ( flags & (O_CREAT|O_TRUNC) ) {
1255 if ( !init_regf_block( rb ) ) {
1256 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1265 /* read in an existing file */
1267 if ( !read_regf_block( rb ) ) {
1268 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1278 /*******************************************************************
1279 *******************************************************************/
1281 static void regfio_mem_free( REGF_FILE *file )
1283 /* free any talloc()'d memory */
1285 if ( file && file->mem_ctx )
1286 talloc_destroy( file->mem_ctx );
1289 /*******************************************************************
1290 *******************************************************************/
1292 int regfio_close( REGF_FILE *file )
1296 /* cleanup for a file opened for write */
1298 if ((file->fd != -1) && (file->open_flags & (O_WRONLY|O_RDWR))) {
1302 /* write of sd list */
1304 for ( sk=file->sec_desc_list; sk; sk=sk->next ) {
1305 hbin_prs_sk_rec( "sk_rec", sk->hbin, 0, sk );
1308 /* flush any dirty blocks */
1310 while ( file->block_list ) {
1311 hbin_block_close( file, file->block_list );
1316 unix_to_nt_time( &file->mtime, time(NULL) );
1318 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) != -1 ) {
1319 /* now use for writing */
1320 prs_switch_type( &ps, MARSHALL );
1322 /* stream the block once, generate the checksum,
1323 and stream it again */
1324 prs_set_offset( &ps, 0 );
1325 prs_regf_block( "regf_blocK", &ps, 0, file );
1326 file->checksum = regf_block_checksum( &ps );
1327 prs_set_offset( &ps, 0 );
1328 prs_regf_block( "regf_blocK", &ps, 0, file );
1330 /* now we are ready to write it to disk */
1331 if ( write_block( file, &ps, 0 ) == -1 )
1332 DEBUG(0,("regfio_close: failed to update the regf header block!\n"));
1335 prs_mem_free( &ps );
1338 regfio_mem_free( file );
1340 /* nothing tdo do if there is no open file */
1352 /*******************************************************************
1353 *******************************************************************/
1355 static void regfio_flush( REGF_FILE *file )
1359 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
1360 write_hbin_block( file, hbin );
1364 /*******************************************************************
1365 There should be only *one* root key in the registry file based
1366 on my experience. --jerry
1367 *******************************************************************/
1369 REGF_NK_REC* regfio_rootkey( REGF_FILE *file )
1373 uint32 offset = REGF_BLOCKSIZE;
1380 if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) ) {
1381 DEBUG(0,("regfio_rootkey: talloc() failed!\n"));
1385 /* scan through the file on HBIN block at a time looking
1386 for an NK record with a type == 0x002c.
1387 Normally this is the first nk record in the first hbin
1388 block (but I'm not assuming that for now) */
1390 while ( (hbin = read_hbin_block( file, offset )) ) {
1394 if ( next_nk_record( file, hbin, nk, &eob ) ) {
1395 if ( nk->key_type == NK_TYPE_ROOTKEY ) {
1400 prs_mem_free( &hbin->ps );
1406 offset += hbin->block_size;
1410 DEBUG(0,("regfio_rootkey: corrupt registry file ? No root key record located\n"));
1414 DLIST_ADD( file->block_list, hbin );
1419 /*******************************************************************
1420 This acts as an interator over the subkeys defined for a given
1421 NK record. Remember that offsets are from the *first* HBIN block.
1422 *******************************************************************/
1424 REGF_NK_REC* regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk )
1426 REGF_NK_REC *subkey;
1430 /* see if there is anything left to report */
1432 if ( !nk || (nk->subkeys_off==REGF_OFFSET_NONE) || (nk->subkey_index >= nk->num_subkeys) )
1435 /* find the HBIN block which should contain the nk record */
1437 if ( !(hbin = lookup_hbin_block( file, nk->subkeys.hashes[nk->subkey_index].nk_off )) ) {
1438 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n",
1439 nk->subkeys.hashes[nk->subkey_index].nk_off));
1443 nk_offset = nk->subkeys.hashes[nk->subkey_index].nk_off;
1444 if ( !prs_set_offset( &hbin->ps, (HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off) ) )
1448 if ( !(subkey = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) )
1451 if ( !hbin_prs_key( file, hbin, subkey ) )
1458 /*******************************************************************
1459 *******************************************************************/
1461 static REGF_HBIN* regf_hbin_allocate( REGF_FILE *file, uint32 block_size )
1464 SMB_STRUCT_STAT sbuf;
1466 if ( !(hbin = TALLOC_ZERO_P( file->mem_ctx, REGF_HBIN )) )
1469 memcpy( hbin->header, "hbin", sizeof(HBIN_HDR_SIZE) );
1472 if (sys_fstat(file->fd, &sbuf, false)) {
1473 DEBUG(0,("regf_hbin_allocate: stat() failed! (%s)\n", strerror(errno)));
1477 hbin->file_off = sbuf.st_ex_size;
1479 hbin->free_off = HBIN_HEADER_REC_SIZE;
1480 hbin->free_size = block_size - hbin->free_off + sizeof(uint32);
1482 hbin->block_size = block_size;
1483 hbin->first_hbin_off = hbin->file_off - REGF_BLOCKSIZE;
1485 if ( !prs_init( &hbin->ps, block_size, file->mem_ctx, MARSHALL ) )
1488 if ( !prs_hbin_block( "new_hbin", &hbin->ps, 0, hbin ) )
1491 if ( !write_hbin_block( file, hbin ) )
1494 file->last_block = hbin->file_off;
1499 /*******************************************************************
1500 *******************************************************************/
1502 static void update_free_space( REGF_HBIN *hbin, uint32 size_used )
1504 hbin->free_off += size_used;
1505 hbin->free_size -= size_used;
1507 if ( hbin->free_off >= hbin->block_size ) {
1508 hbin->free_off = REGF_OFFSET_NONE;
1514 /*******************************************************************
1515 *******************************************************************/
1517 static REGF_HBIN* find_free_space( REGF_FILE *file, uint32 size )
1519 REGF_HBIN *hbin, *p_hbin;
1523 /* check open block list */
1525 for ( hbin=file->block_list; hbin!=NULL; hbin=hbin->next ) {
1526 /* only check blocks that actually have available space */
1528 if ( hbin->free_off == REGF_OFFSET_NONE )
1531 /* check for a large enough available chunk */
1533 if ( (hbin->block_size - hbin->free_off) >= size ) {
1534 DLIST_PROMOTE( file->block_list, hbin );
1539 /* parse the file until we find a block with
1540 enough free space; save the last non-filled hbin */
1542 block_off = REGF_BLOCKSIZE;
1544 /* cleanup before the next round */
1547 prs_mem_free( &hbin->ps );
1549 hbin = read_hbin_block( file, block_off );
1553 /* make sure that we don't already have this block in memory */
1555 for ( p_hbin=file->block_list; p_hbin!=NULL; p_hbin=p_hbin->next ) {
1556 if ( p_hbin->file_off == hbin->file_off ) {
1562 block_off = hbin->file_off + hbin->block_size;
1565 prs_mem_free( &hbin->ps );
1570 /* if (cached block or (new block and not enough free space)) then continue looping */
1571 } while ( cached || (hbin && (hbin->free_size < size)) );
1573 /* no free space; allocate a new one */
1578 /* allocate in multiples of REGF_ALLOC_BLOCK; make sure (size + hbin_header) fits */
1580 alloc_size = (((size+HBIN_HEADER_REC_SIZE) / REGF_ALLOC_BLOCK ) + 1 ) * REGF_ALLOC_BLOCK;
1582 if ( !(hbin = regf_hbin_allocate( file, alloc_size )) ) {
1583 DEBUG(0,("find_free_space: regf_hbin_allocate() failed!\n"));
1586 DLIST_ADD( file->block_list, hbin );
1590 /* set the offset to be ready to write */
1592 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) )
1595 /* write the record size as a placeholder for now, it should be
1596 probably updated by the caller once it all of the data necessary
1599 if ( !prs_uint32("allocated_size", &hbin->ps, 0, &size) )
1602 update_free_space( hbin, size );
1607 /*******************************************************************
1608 *******************************************************************/
1610 static uint32 sk_record_data_size( struct security_descriptor * sd )
1612 uint32 size, size_mod8;
1616 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1618 size = sizeof(uint32)*5 + ndr_size_security_descriptor(sd, 0) + sizeof(uint32);
1621 size_mod8 = size & 0xfffffff8;
1622 if ( size_mod8 < size )
1628 /*******************************************************************
1629 *******************************************************************/
1631 static uint32 vk_record_data_size( REGF_VK_REC *vk )
1633 uint32 size, size_mod8;
1637 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1639 size = REC_HDR_SIZE + (sizeof(uint16)*3) + (sizeof(uint32)*3) + sizeof(uint32);
1641 if ( vk->valuename )
1642 size += strlen(vk->valuename);
1645 size_mod8 = size & 0xfffffff8;
1646 if ( size_mod8 < size )
1652 /*******************************************************************
1653 *******************************************************************/
1655 static uint32 lf_record_data_size( uint32 num_keys )
1657 uint32 size, size_mod8;
1661 /* the record size is sizeof(hdr) + num_keys + sizeof of hash_array + data_size_uint32 */
1663 size = REC_HDR_SIZE + sizeof(uint16) + (sizeof(REGF_HASH_REC) * num_keys) + sizeof(uint32);
1666 size_mod8 = size & 0xfffffff8;
1667 if ( size_mod8 < size )
1673 /*******************************************************************
1674 *******************************************************************/
1676 static uint32 nk_record_data_size( REGF_NK_REC *nk )
1678 uint32 size, size_mod8;
1682 /* the record size is static + length_of_keyname + length_of_classname + data_size_uint32 */
1684 size = 0x4c + strlen(nk->keyname) + sizeof(uint32);
1686 if ( nk->classname )
1687 size += strlen( nk->classname );
1690 size_mod8 = size & 0xfffffff8;
1691 if ( size_mod8 < size )
1697 /*******************************************************************
1698 *******************************************************************/
1700 static bool create_vk_record(REGF_FILE *file, REGF_VK_REC *vk,
1701 struct regval_blob *value)
1703 char *name = regval_name(value);
1704 REGF_HBIN *data_hbin;
1708 memcpy( vk->header, "vk", REC_HDR_SIZE );
1711 vk->valuename = talloc_strdup( file->mem_ctx, regval_name(value) );
1712 vk->flag = VK_FLAG_NAME_PRESENT;
1715 vk->data_size = regval_size( value );
1716 vk->type = regval_type( value );
1718 if ( vk->data_size > sizeof(uint32) ) {
1719 uint32 data_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
1721 vk->data = (uint8 *)TALLOC_MEMDUP( file->mem_ctx,
1722 regval_data_p(value),
1724 if (vk->data == NULL) {
1728 /* go ahead and store the offset....we'll pick this hbin block back up when
1729 we stream the data */
1731 if ((data_hbin = find_free_space(file, data_size )) == NULL) {
1734 vk->data_off = prs_offset( &data_hbin->ps ) + data_hbin->first_hbin_off - HBIN_HDR_SIZE;
1737 /* make sure we don't try to copy from a NULL value pointer */
1739 if ( vk->data_size != 0 )
1740 memcpy( &vk->data_off, regval_data_p(value), sizeof(uint32) );
1741 vk->data_size |= VK_DATA_IN_OFFSET;
1747 /*******************************************************************
1748 *******************************************************************/
1750 static int hashrec_cmp( REGF_HASH_REC *h1, REGF_HASH_REC *h2 )
1752 return StrCaseCmp( h1->fullname, h2->fullname );
1755 /*******************************************************************
1756 *******************************************************************/
1758 REGF_NK_REC* regfio_write_key( REGF_FILE *file, const char *name,
1759 struct regval_ctr *values, struct regsubkey_ctr *subkeys,
1760 struct security_descriptor *sec_desc, REGF_NK_REC *parent )
1763 REGF_HBIN *vlist_hbin = NULL;
1766 if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) )
1769 memcpy( nk->header, "nk", REC_HDR_SIZE );
1772 nk->key_type = NK_TYPE_ROOTKEY;
1774 nk->key_type = NK_TYPE_NORMALKEY;
1776 /* store the parent offset (or -1 if a the root key */
1778 nk->parent_off = parent ? (parent->hbin_off + parent->hbin->file_off - REGF_BLOCKSIZE - HBIN_HDR_SIZE ) : REGF_OFFSET_NONE;
1780 /* no classname currently */
1782 nk->classname_off = REGF_OFFSET_NONE;
1783 nk->classname = NULL;
1784 nk->keyname = talloc_strdup( file->mem_ctx, name );
1786 /* current modification time */
1788 unix_to_nt_time( &nk->mtime, time(NULL) );
1790 /* allocate the record on disk */
1792 size = nk_record_data_size( nk );
1793 nk->rec_size = ( size - 1 ) ^ 0XFFFFFFFF;
1794 if ((nk->hbin = find_free_space( file, size )) == NULL) {
1797 nk->hbin_off = prs_offset( &nk->hbin->ps );
1799 /* Update the hash record in the parent */
1802 REGF_HASH_REC *hash = &parent->subkeys.hashes[parent->subkey_index];
1804 hash->nk_off = prs_offset( &nk->hbin->ps ) + nk->hbin->first_hbin_off - HBIN_HDR_SIZE;
1805 memcpy( hash->keycheck, name, sizeof(uint32) );
1806 hash->fullname = talloc_strdup( file->mem_ctx, name );
1807 parent->subkey_index++;
1809 /* sort the list by keyname */
1810 TYPESAFE_QSORT(parent->subkeys.hashes, parent->subkey_index, hashrec_cmp);
1812 if ( !hbin_prs_lf_records( "lf_rec", parent->subkeys.hbin, 0, parent ) )
1816 /* write the security descriptor */
1818 nk->sk_off = REGF_OFFSET_NONE;
1820 uint32 sk_size = sk_record_data_size( sec_desc );
1823 /* search for it in the existing list of sd's */
1825 if ( (nk->sec_desc = find_sk_record_by_sec_desc( file, sec_desc )) == NULL ) {
1826 /* not found so add it to the list */
1828 if (!(sk_hbin = find_free_space( file, sk_size ))) {
1832 if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) )
1835 /* now we have to store the security descriptor in the list and
1836 update the offsets */
1838 memcpy( nk->sec_desc->header, "sk", REC_HDR_SIZE );
1839 nk->sec_desc->hbin = sk_hbin;
1840 nk->sec_desc->hbin_off = prs_offset( &sk_hbin->ps );
1841 nk->sec_desc->sk_off = prs_offset( &sk_hbin->ps ) + sk_hbin->first_hbin_off - HBIN_HDR_SIZE;
1842 nk->sec_desc->rec_size = (sk_size-1) ^ 0xFFFFFFFF;
1844 nk->sec_desc->sec_desc = sec_desc;
1845 nk->sec_desc->ref_count = 0;
1847 /* size value must be self-inclusive */
1848 nk->sec_desc->size = ndr_size_security_descriptor(sec_desc, 0)
1851 DLIST_ADD_END( file->sec_desc_list, nk->sec_desc, REGF_SK_REC *);
1853 /* update the offsets for us and the previous sd in the list.
1854 if this is the first record, then just set the next and prev
1855 offsets to ourself. */
1857 if ( DLIST_PREV(nk->sec_desc) ) {
1858 REGF_SK_REC *prev = DLIST_PREV(nk->sec_desc);
1860 nk->sec_desc->prev_sk_off = prev->hbin_off + prev->hbin->first_hbin_off - HBIN_HDR_SIZE;
1861 prev->next_sk_off = nk->sec_desc->sk_off;
1863 /* the end must loop around to the front */
1864 nk->sec_desc->next_sk_off = file->sec_desc_list->sk_off;
1866 /* and first must loop around to the tail */
1867 file->sec_desc_list->prev_sk_off = nk->sec_desc->sk_off;
1869 nk->sec_desc->prev_sk_off = nk->sec_desc->sk_off;
1870 nk->sec_desc->next_sk_off = nk->sec_desc->sk_off;
1874 /* bump the reference count +1 */
1876 nk->sk_off = nk->sec_desc->sk_off;
1877 nk->sec_desc->ref_count++;
1880 /* write the subkeys */
1882 nk->subkeys_off = REGF_OFFSET_NONE;
1883 if ( (nk->num_subkeys = regsubkey_ctr_numkeys( subkeys )) != 0 ) {
1884 uint32 lf_size = lf_record_data_size( nk->num_subkeys );
1888 if (!(nk->subkeys.hbin = find_free_space( file, lf_size ))) {
1891 nk->subkeys.hbin_off = prs_offset( &nk->subkeys.hbin->ps );
1892 nk->subkeys.rec_size = (lf_size-1) ^ 0xFFFFFFFF;
1893 nk->subkeys_off = prs_offset( &nk->subkeys.hbin->ps ) + nk->subkeys.hbin->first_hbin_off - HBIN_HDR_SIZE;
1895 memcpy( nk->subkeys.header, "lf", REC_HDR_SIZE );
1897 nk->subkeys.num_keys = nk->num_subkeys;
1898 if (nk->subkeys.num_keys) {
1899 if ( !(nk->subkeys.hashes = TALLOC_ZERO_ARRAY( file->mem_ctx, REGF_HASH_REC, nk->subkeys.num_keys )) )
1902 nk->subkeys.hashes = NULL;
1904 nk->subkey_index = 0;
1906 /* update the max_bytes_subkey{name,classname} fields */
1907 for ( i=0; i<nk->num_subkeys; i++ ) {
1908 namelen = strlen( regsubkey_ctr_specific_key(subkeys, i) );
1909 if ( namelen*2 > nk->max_bytes_subkeyname )
1910 nk->max_bytes_subkeyname = namelen * 2;
1914 /* write the values */
1916 nk->values_off = REGF_OFFSET_NONE;
1917 if ( (nk->num_values = regval_ctr_numvals( values )) != 0 ) {
1918 uint32 vlist_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
1921 if (!(vlist_hbin = find_free_space( file, vlist_size ))) {
1924 nk->values_off = prs_offset( &vlist_hbin->ps ) + vlist_hbin->first_hbin_off - HBIN_HDR_SIZE;
1926 if (nk->num_values) {
1927 if ( !(nk->values = TALLOC_ARRAY( file->mem_ctx, REGF_VK_REC, nk->num_values )) )
1933 /* create the vk records */
1935 for ( i=0; i<nk->num_values; i++ ) {
1936 uint32 vk_size, namelen, datalen;
1937 struct regval_blob *r;
1939 r = regval_ctr_specific_value( values, i );
1940 create_vk_record( file, &nk->values[i], r );
1941 vk_size = vk_record_data_size( &nk->values[i] );
1942 nk->values[i].hbin = find_free_space( file, vk_size );
1943 nk->values[i].hbin_off = prs_offset( &nk->values[i].hbin->ps );
1944 nk->values[i].rec_size = ( vk_size - 1 ) ^ 0xFFFFFFFF;
1945 nk->values[i].rec_off = prs_offset( &nk->values[i].hbin->ps )
1946 + nk->values[i].hbin->first_hbin_off
1949 /* update the max bytes fields if necessary */
1951 namelen = strlen( regval_name(r) );
1952 if ( namelen*2 > nk->max_bytes_valuename )
1953 nk->max_bytes_valuename = namelen * 2;
1955 datalen = regval_size( r );
1956 if ( datalen > nk->max_bytes_value )
1957 nk->max_bytes_value = datalen;
1961 /* stream the records */
1963 prs_set_offset( &nk->hbin->ps, nk->hbin_off );
1964 if ( !prs_nk_rec( "nk_rec", &nk->hbin->ps, 0, nk ) )
1967 if ( nk->num_values ) {
1968 if ( !hbin_prs_vk_records( "vk_records", vlist_hbin, 0, nk, file ) )
1973 regfio_flush( file );