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 "reg_parse_prs.h"
23 #include "reg_objects.h"
24 #include "../librpc/gen_ndr/ndr_security.h"
27 #define DBGC_CLASS DBGC_REGISTRY
29 /*******************************************************************
31 * TODO : Right now this code basically ignores classnames.
33 ******************************************************************/
35 /*******************************************************************
36 Reads or writes an NTTIME structure.
37 ********************************************************************/
39 static bool smb_io_time(const char *desc, NTTIME *nttime, prs_struct *ps, int depth)
45 prs_debug(ps, depth, desc, "smb_io_time");
51 if (MARSHALLING(ps)) {
52 low = *nttime & 0xFFFFFFFF;
56 if(!prs_uint32("low ", ps, depth, &low)) /* low part */
58 if(!prs_uint32("high", ps, depth, &high)) /* high part */
61 if (UNMARSHALLING(ps)) {
62 *nttime = (((uint64_t)high << 32) + low);
68 /*******************************************************************
69 *******************************************************************/
71 static int write_block( REGF_FILE *file, prs_struct *ps, uint32 offset )
73 int bytes_written, returned;
74 char *buffer = prs_data_p( ps );
75 uint32 buffer_size = prs_data_size( ps );
81 /* check for end of file */
83 if (sys_fstat(file->fd, &sbuf, false)) {
84 DEBUG(0,("write_block: stat() failed! (%s)\n", strerror(errno)));
88 if ( lseek( file->fd, offset, SEEK_SET ) == -1 ) {
89 DEBUG(0,("write_block: lseek() failed! (%s)\n", strerror(errno) ));
93 bytes_written = returned = 0;
94 while ( bytes_written < buffer_size ) {
95 if ( (returned = write( file->fd, buffer+bytes_written, buffer_size-bytes_written )) == -1 ) {
96 DEBUG(0,("write_block: write() failed! (%s)\n", strerror(errno) ));
100 bytes_written += returned;
103 return bytes_written;
106 /*******************************************************************
107 *******************************************************************/
109 static int read_block( REGF_FILE *file, prs_struct *ps, uint32 file_offset, uint32 block_size )
111 int bytes_read, returned;
113 SMB_STRUCT_STAT sbuf;
115 /* check for end of file */
117 if (sys_fstat(file->fd, &sbuf, false)) {
118 DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno)));
122 if ( (size_t)file_offset >= sbuf.st_ex_size )
125 /* if block_size == 0, we are parsing HBIN records and need
126 to read some of the header to get the block_size from there */
128 if ( block_size == 0 ) {
131 if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
132 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
136 returned = read( file->fd, hdr, 0x20 );
137 if ( (returned == -1) || (returned < 0x20) ) {
138 DEBUG(0,("read_block: failed to read in HBIN header. Is the file corrupt?\n"));
142 /* make sure this is an hbin header */
144 if ( strncmp( hdr, "hbin", HBIN_HDR_SIZE ) != 0 ) {
145 DEBUG(0,("read_block: invalid block header!\n"));
149 block_size = IVAL( hdr, 0x08 );
152 DEBUG(10,("read_block: block_size == 0x%x\n", block_size ));
154 /* set the offset, initialize the buffer, and read the block from disk */
156 if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
157 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
161 if (!prs_init( ps, block_size, file->mem_ctx, UNMARSHALL )) {
162 DEBUG(0,("read_block: prs_init() failed! (%s)\n", strerror(errno) ));
165 buffer = prs_data_p( ps );
166 bytes_read = returned = 0;
168 while ( bytes_read < block_size ) {
169 if ( (returned = read( file->fd, buffer+bytes_read, block_size-bytes_read )) == -1 ) {
170 DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno) ));
173 if ( (returned == 0) && (bytes_read < block_size) ) {
174 DEBUG(0,("read_block: not a vald registry file ?\n" ));
178 bytes_read += returned;
184 /*******************************************************************
185 *******************************************************************/
187 static bool write_hbin_block( REGF_FILE *file, REGF_HBIN *hbin )
192 /* write free space record if any is available */
194 if ( hbin->free_off != REGF_OFFSET_NONE ) {
195 uint32 header = 0xffffffff;
197 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) )
199 if ( !prs_uint32( "free_size", &hbin->ps, 0, &hbin->free_size ) )
201 if ( !prs_uint32( "free_header", &hbin->ps, 0, &header ) )
205 hbin->dirty = (write_block( file, &hbin->ps, hbin->file_off ) != -1);
210 /*******************************************************************
211 *******************************************************************/
213 static bool hbin_block_close( REGF_FILE *file, REGF_HBIN *hbin )
217 /* remove the block from the open list and flush it to disk */
219 for ( p=file->block_list; p && p!=hbin; p=p->next )
223 DLIST_REMOVE( file->block_list, hbin );
226 DEBUG(0,("hbin_block_close: block not in open list!\n"));
228 if ( !write_hbin_block( file, hbin ) )
234 /*******************************************************************
235 *******************************************************************/
237 static bool prs_regf_block( const char *desc, prs_struct *ps, int depth, REGF_FILE *file )
239 prs_debug(ps, depth, desc, "prs_regf_block");
242 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)file->header, sizeof( file->header )) )
245 /* yes, these values are always identical so store them only once */
247 if ( !prs_uint32( "unknown1", ps, depth, &file->unknown1 ))
249 if ( !prs_uint32( "unknown1 (again)", ps, depth, &file->unknown1 ))
252 /* get the modtime */
254 if ( !prs_set_offset( ps, 0x0c ) )
256 if ( !smb_io_time( "modtime", &file->mtime, ps, depth ) )
261 if ( !prs_uint32( "unknown2", ps, depth, &file->unknown2 ))
263 if ( !prs_uint32( "unknown3", ps, depth, &file->unknown3 ))
265 if ( !prs_uint32( "unknown4", ps, depth, &file->unknown4 ))
267 if ( !prs_uint32( "unknown5", ps, depth, &file->unknown5 ))
270 /* get file offsets */
272 if ( !prs_set_offset( ps, 0x24 ) )
274 if ( !prs_uint32( "data_offset", ps, depth, &file->data_offset ))
276 if ( !prs_uint32( "last_block", ps, depth, &file->last_block ))
279 /* one more constant */
281 if ( !prs_uint32( "unknown6", ps, depth, &file->unknown6 ))
284 /* get the checksum */
286 if ( !prs_set_offset( ps, 0x01fc ) )
288 if ( !prs_uint32( "checksum", ps, depth, &file->checksum ))
294 /*******************************************************************
295 *******************************************************************/
297 static bool prs_hbin_block( const char *desc, prs_struct *ps, int depth, REGF_HBIN *hbin )
301 prs_debug(ps, depth, desc, "prs_regf_block");
304 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)hbin->header, sizeof( hbin->header )) )
307 if ( !prs_uint32( "first_hbin_off", ps, depth, &hbin->first_hbin_off ))
310 /* The dosreg.cpp comments say that the block size is at 0x1c.
311 According to a WINXP NTUSER.dat file, this is wrong. The block_size
314 if ( !prs_uint32( "block_size", ps, depth, &hbin->block_size ))
317 block_size2 = hbin->block_size;
318 prs_set_offset( ps, 0x1c );
319 if ( !prs_uint32( "block_size2", ps, depth, &block_size2 ))
322 if ( MARSHALLING(ps) )
329 /*******************************************************************
330 *******************************************************************/
332 static bool prs_nk_rec( const char *desc, prs_struct *ps, int depth, REGF_NK_REC *nk )
334 uint16 class_length, name_length;
336 uint32 data_size, start_off, end_off;
337 uint32 unknown_off = REGF_OFFSET_NONE;
339 nk->hbin_off = prs_offset( ps );
340 start = nk->hbin_off;
342 prs_debug(ps, depth, desc, "prs_nk_rec");
345 /* back up and get the data_size */
347 if ( !prs_set_offset( ps, prs_offset(ps)-sizeof(uint32)) )
349 start_off = prs_offset( ps );
350 if ( !prs_uint32( "rec_size", ps, depth, &nk->rec_size ))
353 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)nk->header, sizeof( nk->header )) )
356 if ( !prs_uint16( "key_type", ps, depth, &nk->key_type ))
358 if ( !smb_io_time( "mtime", &nk->mtime, ps, depth ))
361 if ( !prs_set_offset( ps, start+0x0010 ) )
363 if ( !prs_uint32( "parent_off", ps, depth, &nk->parent_off ))
365 if ( !prs_uint32( "num_subkeys", ps, depth, &nk->num_subkeys ))
368 if ( !prs_set_offset( ps, start+0x001c ) )
370 if ( !prs_uint32( "subkeys_off", ps, depth, &nk->subkeys_off ))
372 if ( !prs_uint32( "unknown_off", ps, depth, &unknown_off) )
375 if ( !prs_set_offset( ps, start+0x0024 ) )
377 if ( !prs_uint32( "num_values", ps, depth, &nk->num_values ))
379 if ( !prs_uint32( "values_off", ps, depth, &nk->values_off ))
381 if ( !prs_uint32( "sk_off", ps, depth, &nk->sk_off ))
383 if ( !prs_uint32( "classname_off", ps, depth, &nk->classname_off ))
386 if ( !prs_uint32( "max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname))
388 if ( !prs_uint32( "max_bytes_subkeyclassname", ps, depth, &nk->max_bytes_subkeyclassname))
390 if ( !prs_uint32( "max_bytes_valuename", ps, depth, &nk->max_bytes_valuename))
392 if ( !prs_uint32( "max_bytes_value", ps, depth, &nk->max_bytes_value))
394 if ( !prs_uint32( "unknown index", ps, depth, &nk->unk_index))
397 name_length = nk->keyname ? strlen(nk->keyname) : 0 ;
398 class_length = nk->classname ? strlen(nk->classname) : 0 ;
399 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
401 if ( !prs_uint16( "class_length", ps, depth, &class_length ))
404 if ( class_length ) {
409 if ( UNMARSHALLING(ps) ) {
410 if ( !(nk->keyname = PRS_ALLOC_MEM( ps, char, name_length+1 )) )
414 if ( !prs_uint8s( True, "name", ps, depth, (uint8*)nk->keyname, name_length) )
417 if ( UNMARSHALLING(ps) )
418 nk->keyname[name_length] = '\0';
421 end_off = prs_offset( ps );
423 /* data_size must be divisible by 8 and large enough to hold the original record */
425 data_size = ((start_off - end_off) & 0xfffffff8 );
426 if ( data_size > nk->rec_size )
427 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size));
429 if ( MARSHALLING(ps) )
430 nk->hbin->dirty = True;
435 /*******************************************************************
436 *******************************************************************/
438 static uint32 regf_block_checksum( prs_struct *ps )
440 char *buffer = prs_data_p( ps );
444 /* XOR of all bytes 0x0000 - 0x01FB */
448 for ( i=0; i<0x01FB; i+=4 ) {
449 x = IVAL(buffer, i );
456 /*******************************************************************
457 *******************************************************************/
459 static bool read_regf_block( REGF_FILE *file )
464 /* grab the first block from the file */
466 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) == -1 )
469 /* parse the block and verify the checksum */
471 if ( !prs_regf_block( "regf_header", &ps, 0, file ) )
474 checksum = regf_block_checksum( &ps );
478 if ( file->checksum != checksum ) {
479 DEBUG(0,("read_regf_block: invalid checksum\n" ));
486 /*******************************************************************
487 *******************************************************************/
489 static REGF_HBIN* read_hbin_block( REGF_FILE *file, off_t offset )
492 uint32 record_size, curr_off, block_size, header;
494 if ( !(hbin = TALLOC_ZERO_P(file->mem_ctx, REGF_HBIN)) )
496 hbin->file_off = offset;
499 if ( read_block( file, &hbin->ps, offset, 0 ) == -1 )
502 if ( !prs_hbin_block( "hbin", &hbin->ps, 0, hbin ) )
505 /* this should be the same thing as hbin->block_size but just in case */
507 block_size = prs_data_size( &hbin->ps );
509 /* Find the available free space offset. Always at the end,
510 so walk the record list and stop when you get to the end.
511 The end is defined by a record header of 0xffffffff. The
512 previous 4 bytes contains the amount of free space remaining
513 in the hbin block. */
515 /* remember that the record_size is in the 4 bytes preceeding the record itself */
517 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE-sizeof(uint32) ) )
522 curr_off = prs_offset( &hbin->ps );
523 while ( header != 0xffffffff ) {
524 /* not done yet so reset the current offset to the
525 next record_size field */
527 curr_off = curr_off+record_size;
529 /* for some reason the record_size of the last record in
530 an hbin block can extend past the end of the block
531 even though the record fits within the remaining
532 space....aaarrrgggghhhhhh */
534 if ( curr_off >= block_size ) {
540 if ( !prs_set_offset( &hbin->ps, curr_off) )
543 if ( !prs_uint32( "rec_size", &hbin->ps, 0, &record_size ) )
545 if ( !prs_uint32( "header", &hbin->ps, 0, &header ) )
548 SMB_ASSERT( record_size != 0 );
550 if ( record_size & 0x80000000 ) {
551 /* absolute_value(record_size) */
552 record_size = (record_size ^ 0xffffffff) + 1;
556 /* save the free space offset */
558 if ( header == 0xffffffff ) {
560 /* account for the fact that the curr_off is 4 bytes behind the actual
563 hbin->free_off = curr_off + sizeof(uint32);
564 hbin->free_size = record_size;
567 DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin->free_off));
569 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE ) )
575 /*******************************************************************
576 Input a random offset and receive the corresponding HBIN
578 *******************************************************************/
580 static bool hbin_contains_offset( REGF_HBIN *hbin, uint32 offset )
585 if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) )
591 /*******************************************************************
592 Input a random offset and receive the corresponding HBIN
594 *******************************************************************/
596 static REGF_HBIN* lookup_hbin_block( REGF_FILE *file, uint32 offset )
598 REGF_HBIN *hbin = NULL;
601 /* start with the open list */
603 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
604 DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%lx]\n", hbin->file_off, (unsigned long)hbin ));
605 if ( hbin_contains_offset( hbin, offset ) )
610 /* start at the beginning */
612 block_off = REGF_BLOCKSIZE;
614 /* cleanup before the next round */
616 prs_mem_free( &hbin->ps );
618 hbin = read_hbin_block( file, block_off );
621 block_off = hbin->file_off + hbin->block_size;
623 } while ( hbin && !hbin_contains_offset( hbin, offset ) );
627 DLIST_ADD( file->block_list, hbin );
632 /*******************************************************************
633 *******************************************************************/
635 static bool prs_hash_rec( const char *desc, prs_struct *ps, int depth, REGF_HASH_REC *hash )
637 prs_debug(ps, depth, desc, "prs_hash_rec");
640 if ( !prs_uint32( "nk_off", ps, depth, &hash->nk_off ))
642 if ( !prs_uint8s( True, "keycheck", ps, depth, hash->keycheck, sizeof( hash->keycheck )) )
648 /*******************************************************************
649 *******************************************************************/
651 static bool hbin_prs_lf_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk )
654 REGF_LF_REC *lf = &nk->subkeys;
655 uint32 data_size, start_off, end_off;
657 prs_debug(&hbin->ps, depth, desc, "prs_lf_records");
660 /* check if we have anything to do first */
662 if ( nk->num_subkeys == 0 )
665 /* move to the LF record */
667 if ( !prs_set_offset( &hbin->ps, nk->subkeys_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
670 /* backup and get the data_size */
672 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
674 start_off = prs_offset( &hbin->ps );
675 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &lf->rec_size ))
678 if ( !prs_uint8s( True, "header", &hbin->ps, depth, (uint8*)lf->header, sizeof( lf->header )) )
681 if ( !prs_uint16( "num_keys", &hbin->ps, depth, &lf->num_keys))
684 if ( UNMARSHALLING(&hbin->ps) ) {
686 if ( !(lf->hashes = PRS_ALLOC_MEM( &hbin->ps, REGF_HASH_REC, lf->num_keys )) )
693 for ( i=0; i<lf->num_keys; i++ ) {
694 if ( !prs_hash_rec( "hash_rec", &hbin->ps, depth, &lf->hashes[i] ) )
698 end_off = prs_offset( &hbin->ps );
700 /* data_size must be divisible by 8 and large enough to hold the original record */
702 data_size = ((start_off - end_off) & 0xfffffff8 );
703 if ( data_size > lf->rec_size )
704 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->rec_size));
706 if ( MARSHALLING(&hbin->ps) )
712 /*******************************************************************
713 *******************************************************************/
715 static bool hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_SK_REC *sk )
717 prs_struct *ps = &hbin->ps;
719 uint32 data_size, start_off, end_off;
722 prs_debug(ps, depth, desc, "hbin_prs_sk_rec");
725 if ( !prs_set_offset( &hbin->ps, sk->sk_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
728 /* backup and get the data_size */
730 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
732 start_off = prs_offset( &hbin->ps );
733 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &sk->rec_size ))
736 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)sk->header, sizeof( sk->header )) )
738 if ( !prs_uint16( "tag", ps, depth, &tag))
741 if ( !prs_uint32( "prev_sk_off", ps, depth, &sk->prev_sk_off))
743 if ( !prs_uint32( "next_sk_off", ps, depth, &sk->next_sk_off))
745 if ( !prs_uint32( "ref_count", ps, depth, &sk->ref_count))
747 if ( !prs_uint32( "size", ps, depth, &sk->size))
752 TALLOC_CTX *mem_ctx = prs_get_mem_context(&hbin->ps);
755 if (MARSHALLING(&hbin->ps)) {
756 status = marshall_sec_desc(mem_ctx,
758 &blob.data, &blob.length);
759 if (!NT_STATUS_IS_OK(status))
761 if (!prs_copy_data_in(&hbin->ps, (const char *)blob.data, blob.length))
764 blob = data_blob_const(prs_data_p(&hbin->ps),
765 prs_data_size(&hbin->ps));
766 status = unmarshall_sec_desc(mem_ctx,
767 blob.data, blob.length,
769 if (!NT_STATUS_IS_OK(status))
771 prs_set_offset(&hbin->ps, blob.length);
775 end_off = prs_offset( &hbin->ps );
777 /* data_size must be divisible by 8 and large enough to hold the original record */
779 data_size = ((start_off - end_off) & 0xfffffff8 );
780 if ( data_size > sk->rec_size )
781 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->rec_size));
783 if ( MARSHALLING(&hbin->ps) )
789 /*******************************************************************
790 *******************************************************************/
792 static bool hbin_prs_vk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_VK_REC *vk, REGF_FILE *file )
796 prs_struct *ps = &hbin->ps;
797 uint32 data_size, start_off, end_off;
799 prs_debug(ps, depth, desc, "prs_vk_rec");
802 /* backup and get the data_size */
804 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
806 start_off = prs_offset( &hbin->ps );
807 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &vk->rec_size ))
810 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)vk->header, sizeof( vk->header )) )
813 if ( MARSHALLING(&hbin->ps) )
814 name_length = strlen(vk->valuename);
816 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
818 if ( !prs_uint32( "data_size", ps, depth, &vk->data_size ))
820 if ( !prs_uint32( "data_off", ps, depth, &vk->data_off ))
822 if ( !prs_uint32( "type", ps, depth, &vk->type))
824 if ( !prs_uint16( "flag", ps, depth, &vk->flag))
827 offset = prs_offset( ps );
828 offset += 2; /* skip 2 bytes */
829 prs_set_offset( ps, offset );
833 if ( vk->flag&VK_FLAG_NAME_PRESENT ) {
835 if ( UNMARSHALLING(&hbin->ps) ) {
836 if ( !(vk->valuename = PRS_ALLOC_MEM( ps, char, name_length+1 )))
839 if ( !prs_uint8s( True, "name", ps, depth, (uint8*)vk->valuename, name_length ) )
843 end_off = prs_offset( &hbin->ps );
845 /* get the data if necessary */
847 if ( vk->data_size != 0 ) {
848 bool charmode = False;
850 if ( (vk->type == REG_SZ) || (vk->type == REG_MULTI_SZ) )
853 /* the data is stored in the offset if the size <= 4 */
855 if ( !(vk->data_size & VK_DATA_IN_OFFSET) ) {
856 REGF_HBIN *hblock = hbin;
857 uint32 data_rec_size;
859 if ( UNMARSHALLING(&hbin->ps) ) {
860 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, vk->data_size) ) )
864 /* this data can be in another hbin */
865 if ( !hbin_contains_offset( hbin, vk->data_off ) ) {
866 if ( !(hblock = lookup_hbin_block( file, vk->data_off )) )
869 if ( !(prs_set_offset( &hblock->ps, (vk->data_off+HBIN_HDR_SIZE-hblock->first_hbin_off)-sizeof(uint32) )) )
872 if ( MARSHALLING(&hblock->ps) ) {
873 data_rec_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
874 data_rec_size = ( data_rec_size - 1 ) ^ 0xFFFFFFFF;
876 if ( !prs_uint32( "data_rec_size", &hblock->ps, depth, &data_rec_size ))
878 if ( !prs_uint8s( charmode, "data", &hblock->ps, depth, vk->data, vk->data_size) )
881 if ( MARSHALLING(&hblock->ps) )
882 hblock->dirty = True;
885 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, 4 ) ) )
887 SIVAL( vk->data, 0, vk->data_off );
892 /* data_size must be divisible by 8 and large enough to hold the original record */
894 data_size = ((start_off - end_off ) & 0xfffffff8 );
895 if ( data_size != vk->rec_size )
896 DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size, vk->rec_size));
898 if ( MARSHALLING(&hbin->ps) )
904 /*******************************************************************
905 read a VK record which is contained in the HBIN block stored
906 in the prs_struct *ps.
907 *******************************************************************/
909 static bool hbin_prs_vk_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk, REGF_FILE *file )
914 prs_debug(&hbin->ps, depth, desc, "prs_vk_records");
917 /* check if we have anything to do first */
919 if ( nk->num_values == 0 )
922 if ( UNMARSHALLING(&hbin->ps) ) {
923 if ( !(nk->values = PRS_ALLOC_MEM( &hbin->ps, REGF_VK_REC, nk->num_values ) ) )
927 /* convert the offset to something relative to this HBIN block */
929 if ( !prs_set_offset( &hbin->ps, nk->values_off+HBIN_HDR_SIZE-hbin->first_hbin_off-sizeof(uint32)) )
932 if ( MARSHALLING( &hbin->ps) ) {
933 record_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
934 record_size = (record_size - 1) ^ 0xFFFFFFFF;
937 if ( !prs_uint32( "record_size", &hbin->ps, depth, &record_size ) )
940 for ( i=0; i<nk->num_values; i++ ) {
941 if ( !prs_uint32( "vk_off", &hbin->ps, depth, &nk->values[i].rec_off ) )
945 for ( i=0; i<nk->num_values; i++ ) {
946 REGF_HBIN *sub_hbin = hbin;
949 if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) ) {
950 sub_hbin = lookup_hbin_block( file, nk->values[i].rec_off );
952 DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n",
953 nk->values[i].hbin_off));
958 new_offset = nk->values[i].rec_off + HBIN_HDR_SIZE - sub_hbin->first_hbin_off;
959 if ( !prs_set_offset( &sub_hbin->ps, new_offset ) )
961 if ( !hbin_prs_vk_rec( "vk_rec", sub_hbin, depth, &nk->values[i], file ) )
965 if ( MARSHALLING(&hbin->ps) )
973 /*******************************************************************
974 *******************************************************************/
976 static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32 offset )
980 for ( p_sk=file->sec_desc_list; p_sk; p_sk=p_sk->next ) {
981 if ( p_sk->sk_off == offset )
988 /*******************************************************************
989 *******************************************************************/
991 static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, struct security_descriptor *sd )
995 for ( p=file->sec_desc_list; p; p=p->next ) {
996 if ( security_descriptor_equal( p->sec_desc, sd ) )
1005 /*******************************************************************
1006 *******************************************************************/
1008 static bool hbin_prs_key( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk )
1011 REGF_HBIN *sub_hbin;
1013 prs_debug(&hbin->ps, depth, "", "fetch_key");
1016 /* get the initial nk record */
1018 if ( !prs_nk_rec( "nk_rec", &hbin->ps, depth, nk ))
1021 /* fill in values */
1023 if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) {
1025 if ( !hbin_contains_offset( hbin, nk->values_off ) ) {
1026 sub_hbin = lookup_hbin_block( file, nk->values_off );
1028 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n",
1034 if ( !hbin_prs_vk_records( "vk_rec", sub_hbin, depth, nk, file ))
1038 /* now get subkeys */
1040 if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) {
1042 if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) {
1043 sub_hbin = lookup_hbin_block( file, nk->subkeys_off );
1045 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n",
1051 if ( !hbin_prs_lf_records( "lf_rec", sub_hbin, depth, nk ))
1055 /* get the to the security descriptor. First look if we have already parsed it */
1057 if ( (nk->sk_off!=REGF_OFFSET_NONE) && !( nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )) ) {
1060 if ( !hbin_contains_offset( hbin, nk->sk_off ) ) {
1061 sub_hbin = lookup_hbin_block( file, nk->sk_off );
1063 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_offset [0x%x]\n",
1069 if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) )
1071 nk->sec_desc->sk_off = nk->sk_off;
1072 if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin, depth, nk->sec_desc ))
1075 /* add to the list of security descriptors (ref_count has been read from the files) */
1077 nk->sec_desc->sk_off = nk->sk_off;
1078 DLIST_ADD( file->sec_desc_list, nk->sec_desc );
1084 /*******************************************************************
1085 *******************************************************************/
1087 static bool next_record( REGF_HBIN *hbin, const char *hdr, bool *eob )
1089 uint8 header[REC_HDR_SIZE];
1091 uint32 curr_off, block_size;
1093 prs_struct *ps = &hbin->ps;
1095 curr_off = prs_offset( ps );
1096 if ( curr_off == 0 )
1097 prs_set_offset( ps, HBIN_HEADER_REC_SIZE );
1099 /* assume that the current offset is at the record header
1100 and we need to backup to read the record size */
1102 curr_off -= sizeof(uint32);
1104 block_size = prs_data_size( ps );
1106 memset( header, 0x0, sizeof(uint8)*REC_HDR_SIZE );
1109 curr_off = curr_off+record_size;
1110 if ( curr_off >= block_size )
1113 if ( !prs_set_offset( &hbin->ps, curr_off) )
1116 if ( !prs_uint32( "record_size", ps, 0, &record_size ) )
1118 if ( !prs_uint8s( True, "header", ps, 0, header, REC_HDR_SIZE ) )
1121 if ( record_size & 0x80000000 ) {
1122 /* absolute_value(record_size) */
1123 record_size = (record_size ^ 0xffffffff) + 1;
1126 if ( memcmp( header, hdr, REC_HDR_SIZE ) == 0 ) {
1128 curr_off += sizeof(uint32);
1132 /* mark prs_struct as done ( at end ) if no more SK records */
1133 /* mark end-of-block as True */
1136 prs_set_offset( &hbin->ps, prs_data_size(&hbin->ps) );
1141 if ( !prs_set_offset( ps, curr_off ) )
1147 /*******************************************************************
1148 *******************************************************************/
1150 static bool next_nk_record( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk, bool *eob )
1152 if ( next_record( hbin, "nk", eob ) && hbin_prs_key( file, hbin, nk ) )
1158 /*******************************************************************
1159 Intialize the newly created REGF_BLOCK in *file and write the
1160 block header to disk
1161 *******************************************************************/
1163 static bool init_regf_block( REGF_FILE *file )
1168 if ( !prs_init( &ps, REGF_BLOCKSIZE, file->mem_ctx, MARSHALL ) )
1171 memcpy( file->header, "regf", REGF_HDR_SIZE );
1172 file->data_offset = 0x20;
1173 file->last_block = 0x1000;
1177 unix_to_nt_time( &file->mtime, time(NULL) );
1179 /* hard coded values...no diea what these are ... maybe in time */
1181 file->unknown1 = 0x2;
1182 file->unknown2 = 0x1;
1183 file->unknown3 = 0x3;
1184 file->unknown4 = 0x0;
1185 file->unknown5 = 0x1;
1186 file->unknown6 = 0x1;
1188 /* write header to the buffer */
1190 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1195 /* calculate the checksum, re-marshall data (to include the checksum)
1196 and write to disk */
1198 file->checksum = regf_block_checksum( &ps );
1199 prs_set_offset( &ps, 0 );
1200 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1205 if ( write_block( file, &ps, 0 ) == -1 ) {
1206 DEBUG(0,("init_regf_block: Failed to initialize registry header block!\n"));
1212 prs_mem_free( &ps );
1216 /*******************************************************************
1217 Open the registry file and then read in the REGF block to get the
1219 *******************************************************************/
1221 REGF_FILE* regfio_open( const char *filename, int flags, int mode )
1225 if ( !(rb = SMB_MALLOC_P(REGF_FILE)) ) {
1226 DEBUG(0,("ERROR allocating memory\n"));
1232 if ( !(rb->mem_ctx = talloc_init( "read_regf_block" )) ) {
1237 rb->open_flags = flags;
1239 /* open and existing file */
1241 if ( (rb->fd = open(filename, flags, mode)) == -1 ) {
1242 DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename, strerror(errno)));
1247 /* check if we are creating a new file or overwriting an existing one */
1249 if ( flags & (O_CREAT|O_TRUNC) ) {
1250 if ( !init_regf_block( rb ) ) {
1251 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1260 /* read in an existing file */
1262 if ( !read_regf_block( rb ) ) {
1263 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1273 /*******************************************************************
1274 *******************************************************************/
1276 static void regfio_mem_free( REGF_FILE *file )
1278 /* free any talloc()'d memory */
1280 if ( file && file->mem_ctx )
1281 talloc_destroy( file->mem_ctx );
1284 /*******************************************************************
1285 *******************************************************************/
1287 int regfio_close( REGF_FILE *file )
1291 /* cleanup for a file opened for write */
1293 if ((file->fd != -1) && (file->open_flags & (O_WRONLY|O_RDWR))) {
1297 /* write of sd list */
1299 for ( sk=file->sec_desc_list; sk; sk=sk->next ) {
1300 hbin_prs_sk_rec( "sk_rec", sk->hbin, 0, sk );
1303 /* flush any dirty blocks */
1305 while ( file->block_list ) {
1306 hbin_block_close( file, file->block_list );
1311 unix_to_nt_time( &file->mtime, time(NULL) );
1313 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) != -1 ) {
1314 /* now use for writing */
1315 prs_switch_type( &ps, MARSHALL );
1317 /* stream the block once, generate the checksum,
1318 and stream it again */
1319 prs_set_offset( &ps, 0 );
1320 prs_regf_block( "regf_blocK", &ps, 0, file );
1321 file->checksum = regf_block_checksum( &ps );
1322 prs_set_offset( &ps, 0 );
1323 prs_regf_block( "regf_blocK", &ps, 0, file );
1325 /* now we are ready to write it to disk */
1326 if ( write_block( file, &ps, 0 ) == -1 )
1327 DEBUG(0,("regfio_close: failed to update the regf header block!\n"));
1330 prs_mem_free( &ps );
1333 regfio_mem_free( file );
1335 /* nothing tdo do if there is no open file */
1347 /*******************************************************************
1348 *******************************************************************/
1350 static void regfio_flush( REGF_FILE *file )
1354 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
1355 write_hbin_block( file, hbin );
1359 /*******************************************************************
1360 There should be only *one* root key in the registry file based
1361 on my experience. --jerry
1362 *******************************************************************/
1364 REGF_NK_REC* regfio_rootkey( REGF_FILE *file )
1368 uint32 offset = REGF_BLOCKSIZE;
1375 if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) ) {
1376 DEBUG(0,("regfio_rootkey: talloc() failed!\n"));
1380 /* scan through the file on HBIN block at a time looking
1381 for an NK record with a type == 0x002c.
1382 Normally this is the first nk record in the first hbin
1383 block (but I'm not assuming that for now) */
1385 while ( (hbin = read_hbin_block( file, offset )) ) {
1389 if ( next_nk_record( file, hbin, nk, &eob ) ) {
1390 if ( nk->key_type == NK_TYPE_ROOTKEY ) {
1395 prs_mem_free( &hbin->ps );
1401 offset += hbin->block_size;
1405 DEBUG(0,("regfio_rootkey: corrupt registry file ? No root key record located\n"));
1409 DLIST_ADD( file->block_list, hbin );
1414 /*******************************************************************
1415 This acts as an interator over the subkeys defined for a given
1416 NK record. Remember that offsets are from the *first* HBIN block.
1417 *******************************************************************/
1419 REGF_NK_REC* regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk )
1421 REGF_NK_REC *subkey;
1425 /* see if there is anything left to report */
1427 if ( !nk || (nk->subkeys_off==REGF_OFFSET_NONE) || (nk->subkey_index >= nk->num_subkeys) )
1430 /* find the HBIN block which should contain the nk record */
1432 if ( !(hbin = lookup_hbin_block( file, nk->subkeys.hashes[nk->subkey_index].nk_off )) ) {
1433 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n",
1434 nk->subkeys.hashes[nk->subkey_index].nk_off));
1438 nk_offset = nk->subkeys.hashes[nk->subkey_index].nk_off;
1439 if ( !prs_set_offset( &hbin->ps, (HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off) ) )
1443 if ( !(subkey = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) )
1446 if ( !hbin_prs_key( file, hbin, subkey ) )
1453 /*******************************************************************
1454 *******************************************************************/
1456 static REGF_HBIN* regf_hbin_allocate( REGF_FILE *file, uint32 block_size )
1459 SMB_STRUCT_STAT sbuf;
1461 if ( !(hbin = TALLOC_ZERO_P( file->mem_ctx, REGF_HBIN )) )
1464 memcpy( hbin->header, "hbin", sizeof(HBIN_HDR_SIZE) );
1467 if (sys_fstat(file->fd, &sbuf, false)) {
1468 DEBUG(0,("regf_hbin_allocate: stat() failed! (%s)\n", strerror(errno)));
1472 hbin->file_off = sbuf.st_ex_size;
1474 hbin->free_off = HBIN_HEADER_REC_SIZE;
1475 hbin->free_size = block_size - hbin->free_off + sizeof(uint32);;
1477 hbin->block_size = block_size;
1478 hbin->first_hbin_off = hbin->file_off - REGF_BLOCKSIZE;
1480 if ( !prs_init( &hbin->ps, block_size, file->mem_ctx, MARSHALL ) )
1483 if ( !prs_hbin_block( "new_hbin", &hbin->ps, 0, hbin ) )
1486 if ( !write_hbin_block( file, hbin ) )
1489 file->last_block = hbin->file_off;
1494 /*******************************************************************
1495 *******************************************************************/
1497 static void update_free_space( REGF_HBIN *hbin, uint32 size_used )
1499 hbin->free_off += size_used;
1500 hbin->free_size -= size_used;
1502 if ( hbin->free_off >= hbin->block_size ) {
1503 hbin->free_off = REGF_OFFSET_NONE;
1509 /*******************************************************************
1510 *******************************************************************/
1512 static REGF_HBIN* find_free_space( REGF_FILE *file, uint32 size )
1514 REGF_HBIN *hbin, *p_hbin;
1518 /* check open block list */
1520 for ( hbin=file->block_list; hbin!=NULL; hbin=hbin->next ) {
1521 /* only check blocks that actually have available space */
1523 if ( hbin->free_off == REGF_OFFSET_NONE )
1526 /* check for a large enough available chunk */
1528 if ( (hbin->block_size - hbin->free_off) >= size ) {
1529 DLIST_PROMOTE( file->block_list, hbin );
1534 /* parse the file until we find a block with
1535 enough free space; save the last non-filled hbin */
1537 block_off = REGF_BLOCKSIZE;
1539 /* cleanup before the next round */
1542 prs_mem_free( &hbin->ps );
1544 hbin = read_hbin_block( file, block_off );
1548 /* make sure that we don't already have this block in memory */
1550 for ( p_hbin=file->block_list; p_hbin!=NULL; p_hbin=p_hbin->next ) {
1551 if ( p_hbin->file_off == hbin->file_off ) {
1557 block_off = hbin->file_off + hbin->block_size;
1560 prs_mem_free( &hbin->ps );
1565 /* if (cached block or (new block and not enough free space)) then continue looping */
1566 } while ( cached || (hbin && (hbin->free_size < size)) );
1568 /* no free space; allocate a new one */
1573 /* allocate in multiples of REGF_ALLOC_BLOCK; make sure (size + hbin_header) fits */
1575 alloc_size = (((size+HBIN_HEADER_REC_SIZE) / REGF_ALLOC_BLOCK ) + 1 ) * REGF_ALLOC_BLOCK;
1577 if ( !(hbin = regf_hbin_allocate( file, alloc_size )) ) {
1578 DEBUG(0,("find_free_space: regf_hbin_allocate() failed!\n"));
1581 DLIST_ADD( file->block_list, hbin );
1585 /* set the offset to be ready to write */
1587 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) )
1590 /* write the record size as a placeholder for now, it should be
1591 probably updated by the caller once it all of the data necessary
1594 if ( !prs_uint32("allocated_size", &hbin->ps, 0, &size) )
1597 update_free_space( hbin, size );
1602 /*******************************************************************
1603 *******************************************************************/
1605 static uint32 sk_record_data_size( struct security_descriptor * sd )
1607 uint32 size, size_mod8;
1611 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1613 size = sizeof(uint32)*5 + ndr_size_security_descriptor(sd, 0) + sizeof(uint32);
1616 size_mod8 = size & 0xfffffff8;
1617 if ( size_mod8 < size )
1623 /*******************************************************************
1624 *******************************************************************/
1626 static uint32 vk_record_data_size( REGF_VK_REC *vk )
1628 uint32 size, size_mod8;
1632 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1634 size = REC_HDR_SIZE + (sizeof(uint16)*3) + (sizeof(uint32)*3) + sizeof(uint32);
1636 if ( vk->valuename )
1637 size += strlen(vk->valuename);
1640 size_mod8 = size & 0xfffffff8;
1641 if ( size_mod8 < size )
1647 /*******************************************************************
1648 *******************************************************************/
1650 static uint32 lf_record_data_size( uint32 num_keys )
1652 uint32 size, size_mod8;
1656 /* the record size is sizeof(hdr) + num_keys + sizeof of hash_array + data_size_uint32 */
1658 size = REC_HDR_SIZE + sizeof(uint16) + (sizeof(REGF_HASH_REC) * num_keys) + sizeof(uint32);
1661 size_mod8 = size & 0xfffffff8;
1662 if ( size_mod8 < size )
1668 /*******************************************************************
1669 *******************************************************************/
1671 static uint32 nk_record_data_size( REGF_NK_REC *nk )
1673 uint32 size, size_mod8;
1677 /* the record size is static + length_of_keyname + length_of_classname + data_size_uint32 */
1679 size = 0x4c + strlen(nk->keyname) + sizeof(uint32);
1681 if ( nk->classname )
1682 size += strlen( nk->classname );
1685 size_mod8 = size & 0xfffffff8;
1686 if ( size_mod8 < size )
1692 /*******************************************************************
1693 *******************************************************************/
1695 static bool create_vk_record(REGF_FILE *file, REGF_VK_REC *vk,
1696 struct regval_blob *value)
1698 char *name = regval_name(value);
1699 REGF_HBIN *data_hbin;
1703 memcpy( vk->header, "vk", REC_HDR_SIZE );
1706 vk->valuename = talloc_strdup( file->mem_ctx, regval_name(value) );
1707 vk->flag = VK_FLAG_NAME_PRESENT;
1710 vk->data_size = regval_size( value );
1711 vk->type = regval_type( value );
1713 if ( vk->data_size > sizeof(uint32) ) {
1714 uint32 data_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
1716 vk->data = (uint8 *)TALLOC_MEMDUP( file->mem_ctx,
1717 regval_data_p(value),
1719 if (vk->data == NULL) {
1723 /* go ahead and store the offset....we'll pick this hbin block back up when
1724 we stream the data */
1726 if ((data_hbin = find_free_space(file, data_size )) == NULL) {
1729 vk->data_off = prs_offset( &data_hbin->ps ) + data_hbin->first_hbin_off - HBIN_HDR_SIZE;
1732 /* make sure we don't try to copy from a NULL value pointer */
1734 if ( vk->data_size != 0 )
1735 memcpy( &vk->data_off, regval_data_p(value), sizeof(uint32) );
1736 vk->data_size |= VK_DATA_IN_OFFSET;
1742 /*******************************************************************
1743 *******************************************************************/
1745 static int hashrec_cmp( REGF_HASH_REC *h1, REGF_HASH_REC *h2 )
1747 return StrCaseCmp( h1->fullname, h2->fullname );
1750 /*******************************************************************
1751 *******************************************************************/
1753 REGF_NK_REC* regfio_write_key( REGF_FILE *file, const char *name,
1754 struct regval_ctr *values, struct regsubkey_ctr *subkeys,
1755 struct security_descriptor *sec_desc, REGF_NK_REC *parent )
1758 REGF_HBIN *vlist_hbin = NULL;
1761 if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) )
1764 memcpy( nk->header, "nk", REC_HDR_SIZE );
1767 nk->key_type = NK_TYPE_ROOTKEY;
1769 nk->key_type = NK_TYPE_NORMALKEY;
1771 /* store the parent offset (or -1 if a the root key */
1773 nk->parent_off = parent ? (parent->hbin_off + parent->hbin->file_off - REGF_BLOCKSIZE - HBIN_HDR_SIZE ) : REGF_OFFSET_NONE;
1775 /* no classname currently */
1777 nk->classname_off = REGF_OFFSET_NONE;
1778 nk->classname = NULL;
1779 nk->keyname = talloc_strdup( file->mem_ctx, name );
1781 /* current modification time */
1783 unix_to_nt_time( &nk->mtime, time(NULL) );
1785 /* allocate the record on disk */
1787 size = nk_record_data_size( nk );
1788 nk->rec_size = ( size - 1 ) ^ 0XFFFFFFFF;
1789 if ((nk->hbin = find_free_space( file, size )) == NULL) {
1792 nk->hbin_off = prs_offset( &nk->hbin->ps );
1794 /* Update the hash record in the parent */
1797 REGF_HASH_REC *hash = &parent->subkeys.hashes[parent->subkey_index];
1799 hash->nk_off = prs_offset( &nk->hbin->ps ) + nk->hbin->first_hbin_off - HBIN_HDR_SIZE;
1800 memcpy( hash->keycheck, name, sizeof(uint32) );
1801 hash->fullname = talloc_strdup( file->mem_ctx, name );
1802 parent->subkey_index++;
1804 /* sort the list by keyname */
1805 TYPESAFE_QSORT(parent->subkeys.hashes, parent->subkey_index, hashrec_cmp);
1807 if ( !hbin_prs_lf_records( "lf_rec", parent->subkeys.hbin, 0, parent ) )
1811 /* write the security descriptor */
1813 nk->sk_off = REGF_OFFSET_NONE;
1815 uint32 sk_size = sk_record_data_size( sec_desc );
1818 /* search for it in the existing list of sd's */
1820 if ( (nk->sec_desc = find_sk_record_by_sec_desc( file, sec_desc )) == NULL ) {
1821 /* not found so add it to the list */
1823 if (!(sk_hbin = find_free_space( file, sk_size ))) {
1827 if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) )
1830 /* now we have to store the security descriptor in the list and
1831 update the offsets */
1833 memcpy( nk->sec_desc->header, "sk", REC_HDR_SIZE );
1834 nk->sec_desc->hbin = sk_hbin;
1835 nk->sec_desc->hbin_off = prs_offset( &sk_hbin->ps );
1836 nk->sec_desc->sk_off = prs_offset( &sk_hbin->ps ) + sk_hbin->first_hbin_off - HBIN_HDR_SIZE;
1837 nk->sec_desc->rec_size = (sk_size-1) ^ 0xFFFFFFFF;
1839 nk->sec_desc->sec_desc = sec_desc;
1840 nk->sec_desc->ref_count = 0;
1842 /* size value must be self-inclusive */
1843 nk->sec_desc->size = ndr_size_security_descriptor(sec_desc, 0)
1846 DLIST_ADD_END( file->sec_desc_list, nk->sec_desc, REGF_SK_REC *);
1848 /* update the offsets for us and the previous sd in the list.
1849 if this is the first record, then just set the next and prev
1850 offsets to ourself. */
1852 if ( DLIST_PREV(nk->sec_desc) ) {
1853 REGF_SK_REC *prev = DLIST_PREV(nk->sec_desc);
1855 nk->sec_desc->prev_sk_off = prev->hbin_off + prev->hbin->first_hbin_off - HBIN_HDR_SIZE;
1856 prev->next_sk_off = nk->sec_desc->sk_off;
1858 /* the end must loop around to the front */
1859 nk->sec_desc->next_sk_off = file->sec_desc_list->sk_off;
1861 /* and first must loop around to the tail */
1862 file->sec_desc_list->prev_sk_off = nk->sec_desc->sk_off;
1864 nk->sec_desc->prev_sk_off = nk->sec_desc->sk_off;
1865 nk->sec_desc->next_sk_off = nk->sec_desc->sk_off;
1869 /* bump the reference count +1 */
1871 nk->sk_off = nk->sec_desc->sk_off;
1872 nk->sec_desc->ref_count++;
1875 /* write the subkeys */
1877 nk->subkeys_off = REGF_OFFSET_NONE;
1878 if ( (nk->num_subkeys = regsubkey_ctr_numkeys( subkeys )) != 0 ) {
1879 uint32 lf_size = lf_record_data_size( nk->num_subkeys );
1883 if (!(nk->subkeys.hbin = find_free_space( file, lf_size ))) {
1886 nk->subkeys.hbin_off = prs_offset( &nk->subkeys.hbin->ps );
1887 nk->subkeys.rec_size = (lf_size-1) ^ 0xFFFFFFFF;
1888 nk->subkeys_off = prs_offset( &nk->subkeys.hbin->ps ) + nk->subkeys.hbin->first_hbin_off - HBIN_HDR_SIZE;
1890 memcpy( nk->subkeys.header, "lf", REC_HDR_SIZE );
1892 nk->subkeys.num_keys = nk->num_subkeys;
1893 if (nk->subkeys.num_keys) {
1894 if ( !(nk->subkeys.hashes = TALLOC_ZERO_ARRAY( file->mem_ctx, REGF_HASH_REC, nk->subkeys.num_keys )) )
1897 nk->subkeys.hashes = NULL;
1899 nk->subkey_index = 0;
1901 /* update the max_bytes_subkey{name,classname} fields */
1902 for ( i=0; i<nk->num_subkeys; i++ ) {
1903 namelen = strlen( regsubkey_ctr_specific_key(subkeys, i) );
1904 if ( namelen*2 > nk->max_bytes_subkeyname )
1905 nk->max_bytes_subkeyname = namelen * 2;
1909 /* write the values */
1911 nk->values_off = REGF_OFFSET_NONE;
1912 if ( (nk->num_values = regval_ctr_numvals( values )) != 0 ) {
1913 uint32 vlist_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
1916 if (!(vlist_hbin = find_free_space( file, vlist_size ))) {
1919 nk->values_off = prs_offset( &vlist_hbin->ps ) + vlist_hbin->first_hbin_off - HBIN_HDR_SIZE;
1921 if (nk->num_values) {
1922 if ( !(nk->values = TALLOC_ARRAY( file->mem_ctx, REGF_VK_REC, nk->num_values )) )
1928 /* create the vk records */
1930 for ( i=0; i<nk->num_values; i++ ) {
1931 uint32 vk_size, namelen, datalen;
1932 struct regval_blob *r;
1934 r = regval_ctr_specific_value( values, i );
1935 create_vk_record( file, &nk->values[i], r );
1936 vk_size = vk_record_data_size( &nk->values[i] );
1937 nk->values[i].hbin = find_free_space( file, vk_size );
1938 nk->values[i].hbin_off = prs_offset( &nk->values[i].hbin->ps );
1939 nk->values[i].rec_size = ( vk_size - 1 ) ^ 0xFFFFFFFF;
1940 nk->values[i].rec_off = prs_offset( &nk->values[i].hbin->ps )
1941 + nk->values[i].hbin->first_hbin_off
1944 /* update the max bytes fields if necessary */
1946 namelen = strlen( regval_name(r) );
1947 if ( namelen*2 > nk->max_bytes_valuename )
1948 nk->max_bytes_valuename = namelen * 2;
1950 datalen = regval_size( r );
1951 if ( datalen > nk->max_bytes_value )
1952 nk->max_bytes_value = datalen;
1956 /* stream the records */
1958 prs_set_offset( &nk->hbin->ps, nk->hbin_off );
1959 if ( !prs_nk_rec( "nk_rec", &nk->hbin->ps, 0, nk ) )
1962 if ( nk->num_values ) {
1963 if ( !hbin_prs_vk_records( "vk_records", vlist_hbin, 0, nk, file ) )
1968 regfio_flush( file );