s3-passdb: Fix typo in comment.
[Samba.git] / source3 / registry / regfio.c
blobd64eab84f9a72942b95d2fa3b0a745a61c8ab4bf
1 /*
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/>.
20 #include "includes.h"
21 #include "regfio.h"
22 #include "reg_objects.h"
23 #include "../librpc/gen_ndr/ndr_security.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_REGISTRY
28 /*******************************************************************
30 * TODO : Right now this code basically ignores classnames.
32 ******************************************************************/
35 /*******************************************************************
36 *******************************************************************/
38 static int write_block( REGF_FILE *file, prs_struct *ps, uint32 offset )
40 int bytes_written, returned;
41 char *buffer = prs_data_p( ps );
42 uint32 buffer_size = prs_data_size( ps );
43 SMB_STRUCT_STAT sbuf;
45 if ( file->fd == -1 )
46 return -1;
48 /* check for end of file */
50 if (sys_fstat(file->fd, &sbuf, false)) {
51 DEBUG(0,("write_block: stat() failed! (%s)\n", strerror(errno)));
52 return -1;
55 if ( lseek( file->fd, offset, SEEK_SET ) == -1 ) {
56 DEBUG(0,("write_block: lseek() failed! (%s)\n", strerror(errno) ));
57 return -1;
60 bytes_written = returned = 0;
61 while ( bytes_written < buffer_size ) {
62 if ( (returned = write( file->fd, buffer+bytes_written, buffer_size-bytes_written )) == -1 ) {
63 DEBUG(0,("write_block: write() failed! (%s)\n", strerror(errno) ));
64 return False;
67 bytes_written += returned;
70 return bytes_written;
73 /*******************************************************************
74 *******************************************************************/
76 static int read_block( REGF_FILE *file, prs_struct *ps, uint32 file_offset, uint32 block_size )
78 int bytes_read, returned;
79 char *buffer;
80 SMB_STRUCT_STAT sbuf;
82 /* check for end of file */
84 if (sys_fstat(file->fd, &sbuf, false)) {
85 DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno)));
86 return -1;
89 if ( (size_t)file_offset >= sbuf.st_ex_size )
90 return -1;
92 /* if block_size == 0, we are parsing HBIN records and need
93 to read some of the header to get the block_size from there */
95 if ( block_size == 0 ) {
96 char hdr[0x20];
98 if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
99 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
100 return -1;
103 returned = read( file->fd, hdr, 0x20 );
104 if ( (returned == -1) || (returned < 0x20) ) {
105 DEBUG(0,("read_block: failed to read in HBIN header. Is the file corrupt?\n"));
106 return -1;
109 /* make sure this is an hbin header */
111 if ( strncmp( hdr, "hbin", HBIN_HDR_SIZE ) != 0 ) {
112 DEBUG(0,("read_block: invalid block header!\n"));
113 return -1;
116 block_size = IVAL( hdr, 0x08 );
119 DEBUG(10,("read_block: block_size == 0x%x\n", block_size ));
121 /* set the offset, initialize the buffer, and read the block from disk */
123 if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
124 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
125 return -1;
128 if (!prs_init( ps, block_size, file->mem_ctx, UNMARSHALL )) {
129 DEBUG(0,("read_block: prs_init() failed! (%s)\n", strerror(errno) ));
130 return -1;
132 buffer = prs_data_p( ps );
133 bytes_read = returned = 0;
135 while ( bytes_read < block_size ) {
136 if ( (returned = read( file->fd, buffer+bytes_read, block_size-bytes_read )) == -1 ) {
137 DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno) ));
138 return False;
140 if ( (returned == 0) && (bytes_read < block_size) ) {
141 DEBUG(0,("read_block: not a vald registry file ?\n" ));
142 return False;
145 bytes_read += returned;
148 return bytes_read;
151 /*******************************************************************
152 *******************************************************************/
154 static bool write_hbin_block( REGF_FILE *file, REGF_HBIN *hbin )
156 if ( !hbin->dirty )
157 return True;
159 /* write free space record if any is available */
161 if ( hbin->free_off != REGF_OFFSET_NONE ) {
162 uint32 header = 0xffffffff;
164 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) )
165 return False;
166 if ( !prs_uint32( "free_size", &hbin->ps, 0, &hbin->free_size ) )
167 return False;
168 if ( !prs_uint32( "free_header", &hbin->ps, 0, &header ) )
169 return False;
172 hbin->dirty = (write_block( file, &hbin->ps, hbin->file_off ) != -1);
174 return hbin->dirty;
177 /*******************************************************************
178 *******************************************************************/
180 static bool hbin_block_close( REGF_FILE *file, REGF_HBIN *hbin )
182 REGF_HBIN *p;
184 /* remove the block from the open list and flush it to disk */
186 for ( p=file->block_list; p && p!=hbin; p=p->next )
189 if ( p == hbin ) {
190 DLIST_REMOVE( file->block_list, hbin );
192 else
193 DEBUG(0,("hbin_block_close: block not in open list!\n"));
195 if ( !write_hbin_block( file, hbin ) )
196 return False;
198 return True;
201 /*******************************************************************
202 *******************************************************************/
204 static bool prs_regf_block( const char *desc, prs_struct *ps, int depth, REGF_FILE *file )
206 prs_debug(ps, depth, desc, "prs_regf_block");
207 depth++;
209 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)file->header, sizeof( file->header )) )
210 return False;
212 /* yes, these values are always identical so store them only once */
214 if ( !prs_uint32( "unknown1", ps, depth, &file->unknown1 ))
215 return False;
216 if ( !prs_uint32( "unknown1 (again)", ps, depth, &file->unknown1 ))
217 return False;
219 /* get the modtime */
221 if ( !prs_set_offset( ps, 0x0c ) )
222 return False;
223 if ( !smb_io_time( "modtime", &file->mtime, ps, depth ) )
224 return False;
226 /* constants */
228 if ( !prs_uint32( "unknown2", ps, depth, &file->unknown2 ))
229 return False;
230 if ( !prs_uint32( "unknown3", ps, depth, &file->unknown3 ))
231 return False;
232 if ( !prs_uint32( "unknown4", ps, depth, &file->unknown4 ))
233 return False;
234 if ( !prs_uint32( "unknown5", ps, depth, &file->unknown5 ))
235 return False;
237 /* get file offsets */
239 if ( !prs_set_offset( ps, 0x24 ) )
240 return False;
241 if ( !prs_uint32( "data_offset", ps, depth, &file->data_offset ))
242 return False;
243 if ( !prs_uint32( "last_block", ps, depth, &file->last_block ))
244 return False;
246 /* one more constant */
248 if ( !prs_uint32( "unknown6", ps, depth, &file->unknown6 ))
249 return False;
251 /* get the checksum */
253 if ( !prs_set_offset( ps, 0x01fc ) )
254 return False;
255 if ( !prs_uint32( "checksum", ps, depth, &file->checksum ))
256 return False;
258 return True;
261 /*******************************************************************
262 *******************************************************************/
264 static bool prs_hbin_block( const char *desc, prs_struct *ps, int depth, REGF_HBIN *hbin )
266 uint32 block_size2;
268 prs_debug(ps, depth, desc, "prs_regf_block");
269 depth++;
271 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)hbin->header, sizeof( hbin->header )) )
272 return False;
274 if ( !prs_uint32( "first_hbin_off", ps, depth, &hbin->first_hbin_off ))
275 return False;
277 /* The dosreg.cpp comments say that the block size is at 0x1c.
278 According to a WINXP NTUSER.dat file, this is wrong. The block_size
279 is at 0x08 */
281 if ( !prs_uint32( "block_size", ps, depth, &hbin->block_size ))
282 return False;
284 block_size2 = hbin->block_size;
285 prs_set_offset( ps, 0x1c );
286 if ( !prs_uint32( "block_size2", ps, depth, &block_size2 ))
287 return False;
289 if ( MARSHALLING(ps) )
290 hbin->dirty = True;
293 return True;
296 /*******************************************************************
297 *******************************************************************/
299 static bool prs_nk_rec( const char *desc, prs_struct *ps, int depth, REGF_NK_REC *nk )
301 uint16 class_length, name_length;
302 uint32 start;
303 uint32 data_size, start_off, end_off;
304 uint32 unknown_off = REGF_OFFSET_NONE;
306 nk->hbin_off = prs_offset( ps );
307 start = nk->hbin_off;
309 prs_debug(ps, depth, desc, "prs_nk_rec");
310 depth++;
312 /* back up and get the data_size */
314 if ( !prs_set_offset( ps, prs_offset(ps)-sizeof(uint32)) )
315 return False;
316 start_off = prs_offset( ps );
317 if ( !prs_uint32( "rec_size", ps, depth, &nk->rec_size ))
318 return False;
320 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)nk->header, sizeof( nk->header )) )
321 return False;
323 if ( !prs_uint16( "key_type", ps, depth, &nk->key_type ))
324 return False;
325 if ( !smb_io_time( "mtime", &nk->mtime, ps, depth ))
326 return False;
328 if ( !prs_set_offset( ps, start+0x0010 ) )
329 return False;
330 if ( !prs_uint32( "parent_off", ps, depth, &nk->parent_off ))
331 return False;
332 if ( !prs_uint32( "num_subkeys", ps, depth, &nk->num_subkeys ))
333 return False;
335 if ( !prs_set_offset( ps, start+0x001c ) )
336 return False;
337 if ( !prs_uint32( "subkeys_off", ps, depth, &nk->subkeys_off ))
338 return False;
339 if ( !prs_uint32( "unknown_off", ps, depth, &unknown_off) )
340 return False;
342 if ( !prs_set_offset( ps, start+0x0024 ) )
343 return False;
344 if ( !prs_uint32( "num_values", ps, depth, &nk->num_values ))
345 return False;
346 if ( !prs_uint32( "values_off", ps, depth, &nk->values_off ))
347 return False;
348 if ( !prs_uint32( "sk_off", ps, depth, &nk->sk_off ))
349 return False;
350 if ( !prs_uint32( "classname_off", ps, depth, &nk->classname_off ))
351 return False;
353 if ( !prs_uint32( "max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname))
354 return False;
355 if ( !prs_uint32( "max_bytes_subkeyclassname", ps, depth, &nk->max_bytes_subkeyclassname))
356 return False;
357 if ( !prs_uint32( "max_bytes_valuename", ps, depth, &nk->max_bytes_valuename))
358 return False;
359 if ( !prs_uint32( "max_bytes_value", ps, depth, &nk->max_bytes_value))
360 return False;
361 if ( !prs_uint32( "unknown index", ps, depth, &nk->unk_index))
362 return False;
364 name_length = nk->keyname ? strlen(nk->keyname) : 0 ;
365 class_length = nk->classname ? strlen(nk->classname) : 0 ;
366 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
367 return False;
368 if ( !prs_uint16( "class_length", ps, depth, &class_length ))
369 return False;
371 if ( class_length ) {
375 if ( name_length ) {
376 if ( UNMARSHALLING(ps) ) {
377 if ( !(nk->keyname = PRS_ALLOC_MEM( ps, char, name_length+1 )) )
378 return False;
381 if ( !prs_uint8s( True, "name", ps, depth, (uint8*)nk->keyname, name_length) )
382 return False;
384 if ( UNMARSHALLING(ps) )
385 nk->keyname[name_length] = '\0';
388 end_off = prs_offset( ps );
390 /* data_size must be divisible by 8 and large enough to hold the original record */
392 data_size = ((start_off - end_off) & 0xfffffff8 );
393 if ( data_size > nk->rec_size )
394 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size));
396 if ( MARSHALLING(ps) )
397 nk->hbin->dirty = True;
399 return True;
402 /*******************************************************************
403 *******************************************************************/
405 static uint32 regf_block_checksum( prs_struct *ps )
407 char *buffer = prs_data_p( ps );
408 uint32 checksum, x;
409 int i;
411 /* XOR of all bytes 0x0000 - 0x01FB */
413 checksum = x = 0;
415 for ( i=0; i<0x01FB; i+=4 ) {
416 x = IVAL(buffer, i );
417 checksum ^= x;
420 return checksum;
423 /*******************************************************************
424 *******************************************************************/
426 static bool read_regf_block( REGF_FILE *file )
428 prs_struct ps;
429 uint32 checksum;
431 /* grab the first block from the file */
433 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) == -1 )
434 return False;
436 /* parse the block and verify the checksum */
438 if ( !prs_regf_block( "regf_header", &ps, 0, file ) )
439 return False;
441 checksum = regf_block_checksum( &ps );
443 prs_mem_free( &ps );
445 if ( file->checksum != checksum ) {
446 DEBUG(0,("read_regf_block: invalid checksum\n" ));
447 return False;
450 return True;
453 /*******************************************************************
454 *******************************************************************/
456 static REGF_HBIN* read_hbin_block( REGF_FILE *file, off_t offset )
458 REGF_HBIN *hbin;
459 uint32 record_size, curr_off, block_size, header;
461 if ( !(hbin = TALLOC_ZERO_P(file->mem_ctx, REGF_HBIN)) )
462 return NULL;
463 hbin->file_off = offset;
464 hbin->free_off = -1;
466 if ( read_block( file, &hbin->ps, offset, 0 ) == -1 )
467 return NULL;
469 if ( !prs_hbin_block( "hbin", &hbin->ps, 0, hbin ) )
470 return NULL;
472 /* this should be the same thing as hbin->block_size but just in case */
474 block_size = prs_data_size( &hbin->ps );
476 /* Find the available free space offset. Always at the end,
477 so walk the record list and stop when you get to the end.
478 The end is defined by a record header of 0xffffffff. The
479 previous 4 bytes contains the amount of free space remaining
480 in the hbin block. */
482 /* remember that the record_size is in the 4 bytes preceeding the record itself */
484 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE-sizeof(uint32) ) )
485 return False;
487 record_size = 0;
488 header = 0;
489 curr_off = prs_offset( &hbin->ps );
490 while ( header != 0xffffffff ) {
491 /* not done yet so reset the current offset to the
492 next record_size field */
494 curr_off = curr_off+record_size;
496 /* for some reason the record_size of the last record in
497 an hbin block can extend past the end of the block
498 even though the record fits within the remaining
499 space....aaarrrgggghhhhhh */
501 if ( curr_off >= block_size ) {
502 record_size = -1;
503 curr_off = -1;
504 break;
507 if ( !prs_set_offset( &hbin->ps, curr_off) )
508 return False;
510 if ( !prs_uint32( "rec_size", &hbin->ps, 0, &record_size ) )
511 return False;
512 if ( !prs_uint32( "header", &hbin->ps, 0, &header ) )
513 return False;
515 SMB_ASSERT( record_size != 0 );
517 if ( record_size & 0x80000000 ) {
518 /* absolute_value(record_size) */
519 record_size = (record_size ^ 0xffffffff) + 1;
523 /* save the free space offset */
525 if ( header == 0xffffffff ) {
527 /* account for the fact that the curr_off is 4 bytes behind the actual
528 record header */
530 hbin->free_off = curr_off + sizeof(uint32);
531 hbin->free_size = record_size;
534 DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin->free_off));
536 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE ) )
537 return False;
539 return hbin;
542 /*******************************************************************
543 Input a random offset and receive the corresponding HBIN
544 block for it
545 *******************************************************************/
547 static bool hbin_contains_offset( REGF_HBIN *hbin, uint32 offset )
549 if ( !hbin )
550 return False;
552 if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) )
553 return True;
555 return False;
558 /*******************************************************************
559 Input a random offset and receive the corresponding HBIN
560 block for it
561 *******************************************************************/
563 static REGF_HBIN* lookup_hbin_block( REGF_FILE *file, uint32 offset )
565 REGF_HBIN *hbin = NULL;
566 uint32 block_off;
568 /* start with the open list */
570 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
571 DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%lx]\n", hbin->file_off, (unsigned long)hbin ));
572 if ( hbin_contains_offset( hbin, offset ) )
573 return hbin;
576 if ( !hbin ) {
577 /* start at the beginning */
579 block_off = REGF_BLOCKSIZE;
580 do {
581 /* cleanup before the next round */
582 if ( hbin )
583 prs_mem_free( &hbin->ps );
585 hbin = read_hbin_block( file, block_off );
587 if ( hbin )
588 block_off = hbin->file_off + hbin->block_size;
590 } while ( hbin && !hbin_contains_offset( hbin, offset ) );
593 if ( hbin )
594 DLIST_ADD( file->block_list, hbin );
596 return hbin;
599 /*******************************************************************
600 *******************************************************************/
602 static bool prs_hash_rec( const char *desc, prs_struct *ps, int depth, REGF_HASH_REC *hash )
604 prs_debug(ps, depth, desc, "prs_hash_rec");
605 depth++;
607 if ( !prs_uint32( "nk_off", ps, depth, &hash->nk_off ))
608 return False;
609 if ( !prs_uint8s( True, "keycheck", ps, depth, hash->keycheck, sizeof( hash->keycheck )) )
610 return False;
612 return True;
615 /*******************************************************************
616 *******************************************************************/
618 static bool hbin_prs_lf_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk )
620 int i;
621 REGF_LF_REC *lf = &nk->subkeys;
622 uint32 data_size, start_off, end_off;
624 prs_debug(&hbin->ps, depth, desc, "prs_lf_records");
625 depth++;
627 /* check if we have anything to do first */
629 if ( nk->num_subkeys == 0 )
630 return True;
632 /* move to the LF record */
634 if ( !prs_set_offset( &hbin->ps, nk->subkeys_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
635 return False;
637 /* backup and get the data_size */
639 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
640 return False;
641 start_off = prs_offset( &hbin->ps );
642 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &lf->rec_size ))
643 return False;
645 if ( !prs_uint8s( True, "header", &hbin->ps, depth, (uint8*)lf->header, sizeof( lf->header )) )
646 return False;
648 if ( !prs_uint16( "num_keys", &hbin->ps, depth, &lf->num_keys))
649 return False;
651 if ( UNMARSHALLING(&hbin->ps) ) {
652 if (lf->num_keys) {
653 if ( !(lf->hashes = PRS_ALLOC_MEM( &hbin->ps, REGF_HASH_REC, lf->num_keys )) )
654 return False;
655 } else {
656 lf->hashes = NULL;
660 for ( i=0; i<lf->num_keys; i++ ) {
661 if ( !prs_hash_rec( "hash_rec", &hbin->ps, depth, &lf->hashes[i] ) )
662 return False;
665 end_off = prs_offset( &hbin->ps );
667 /* data_size must be divisible by 8 and large enough to hold the original record */
669 data_size = ((start_off - end_off) & 0xfffffff8 );
670 if ( data_size > lf->rec_size )
671 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->rec_size));
673 if ( MARSHALLING(&hbin->ps) )
674 hbin->dirty = True;
676 return True;
679 /*******************************************************************
680 *******************************************************************/
682 static bool hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_SK_REC *sk )
684 prs_struct *ps = &hbin->ps;
685 uint16 tag = 0xFFFF;
686 uint32 data_size, start_off, end_off;
689 prs_debug(ps, depth, desc, "hbin_prs_sk_rec");
690 depth++;
692 if ( !prs_set_offset( &hbin->ps, sk->sk_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
693 return False;
695 /* backup and get the data_size */
697 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
698 return False;
699 start_off = prs_offset( &hbin->ps );
700 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &sk->rec_size ))
701 return False;
703 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)sk->header, sizeof( sk->header )) )
704 return False;
705 if ( !prs_uint16( "tag", ps, depth, &tag))
706 return False;
708 if ( !prs_uint32( "prev_sk_off", ps, depth, &sk->prev_sk_off))
709 return False;
710 if ( !prs_uint32( "next_sk_off", ps, depth, &sk->next_sk_off))
711 return False;
712 if ( !prs_uint32( "ref_count", ps, depth, &sk->ref_count))
713 return False;
714 if ( !prs_uint32( "size", ps, depth, &sk->size))
715 return False;
718 NTSTATUS status;
719 TALLOC_CTX *mem_ctx = prs_get_mem_context(&hbin->ps);
720 DATA_BLOB blob;
722 if (MARSHALLING(&hbin->ps)) {
723 status = marshall_sec_desc(mem_ctx,
724 sk->sec_desc,
725 &blob.data, &blob.length);
726 if (!NT_STATUS_IS_OK(status))
727 return False;
728 if (!prs_copy_data_in(&hbin->ps, (const char *)blob.data, blob.length))
729 return False;
730 } else {
731 blob = data_blob_const(prs_data_p(&hbin->ps),
732 prs_data_size(&hbin->ps));
733 status = unmarshall_sec_desc(mem_ctx,
734 blob.data, blob.length,
735 &sk->sec_desc);
736 if (!NT_STATUS_IS_OK(status))
737 return False;
738 prs_set_offset(&hbin->ps, blob.length);
742 end_off = prs_offset( &hbin->ps );
744 /* data_size must be divisible by 8 and large enough to hold the original record */
746 data_size = ((start_off - end_off) & 0xfffffff8 );
747 if ( data_size > sk->rec_size )
748 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->rec_size));
750 if ( MARSHALLING(&hbin->ps) )
751 hbin->dirty = True;
753 return True;
756 /*******************************************************************
757 *******************************************************************/
759 static bool hbin_prs_vk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_VK_REC *vk, REGF_FILE *file )
761 uint32 offset;
762 uint16 name_length;
763 prs_struct *ps = &hbin->ps;
764 uint32 data_size, start_off, end_off;
766 prs_debug(ps, depth, desc, "prs_vk_rec");
767 depth++;
769 /* backup and get the data_size */
771 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
772 return False;
773 start_off = prs_offset( &hbin->ps );
774 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &vk->rec_size ))
775 return False;
777 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)vk->header, sizeof( vk->header )) )
778 return False;
780 if ( MARSHALLING(&hbin->ps) )
781 name_length = strlen(vk->valuename);
783 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
784 return False;
785 if ( !prs_uint32( "data_size", ps, depth, &vk->data_size ))
786 return False;
787 if ( !prs_uint32( "data_off", ps, depth, &vk->data_off ))
788 return False;
789 if ( !prs_uint32( "type", ps, depth, &vk->type))
790 return False;
791 if ( !prs_uint16( "flag", ps, depth, &vk->flag))
792 return False;
794 offset = prs_offset( ps );
795 offset += 2; /* skip 2 bytes */
796 prs_set_offset( ps, offset );
798 /* get the name */
800 if ( vk->flag&VK_FLAG_NAME_PRESENT ) {
802 if ( UNMARSHALLING(&hbin->ps) ) {
803 if ( !(vk->valuename = PRS_ALLOC_MEM( ps, char, name_length+1 )))
804 return False;
806 if ( !prs_uint8s( True, "name", ps, depth, (uint8*)vk->valuename, name_length ) )
807 return False;
810 end_off = prs_offset( &hbin->ps );
812 /* get the data if necessary */
814 if ( vk->data_size != 0 ) {
815 bool charmode = False;
817 if ( (vk->type == REG_SZ) || (vk->type == REG_MULTI_SZ) )
818 charmode = True;
820 /* the data is stored in the offset if the size <= 4 */
822 if ( !(vk->data_size & VK_DATA_IN_OFFSET) ) {
823 REGF_HBIN *hblock = hbin;
824 uint32 data_rec_size;
826 if ( UNMARSHALLING(&hbin->ps) ) {
827 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, vk->data_size) ) )
828 return False;
831 /* this data can be in another hbin */
832 if ( !hbin_contains_offset( hbin, vk->data_off ) ) {
833 if ( !(hblock = lookup_hbin_block( file, vk->data_off )) )
834 return False;
836 if ( !(prs_set_offset( &hblock->ps, (vk->data_off+HBIN_HDR_SIZE-hblock->first_hbin_off)-sizeof(uint32) )) )
837 return False;
839 if ( MARSHALLING(&hblock->ps) ) {
840 data_rec_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
841 data_rec_size = ( data_rec_size - 1 ) ^ 0xFFFFFFFF;
843 if ( !prs_uint32( "data_rec_size", &hblock->ps, depth, &data_rec_size ))
844 return False;
845 if ( !prs_uint8s( charmode, "data", &hblock->ps, depth, vk->data, vk->data_size) )
846 return False;
848 if ( MARSHALLING(&hblock->ps) )
849 hblock->dirty = True;
851 else {
852 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, 4 ) ) )
853 return False;
854 SIVAL( vk->data, 0, vk->data_off );
859 /* data_size must be divisible by 8 and large enough to hold the original record */
861 data_size = ((start_off - end_off ) & 0xfffffff8 );
862 if ( data_size != vk->rec_size )
863 DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size, vk->rec_size));
865 if ( MARSHALLING(&hbin->ps) )
866 hbin->dirty = True;
868 return True;
871 /*******************************************************************
872 read a VK record which is contained in the HBIN block stored
873 in the prs_struct *ps.
874 *******************************************************************/
876 static bool hbin_prs_vk_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk, REGF_FILE *file )
878 int i;
879 uint32 record_size;
881 prs_debug(&hbin->ps, depth, desc, "prs_vk_records");
882 depth++;
884 /* check if we have anything to do first */
886 if ( nk->num_values == 0 )
887 return True;
889 if ( UNMARSHALLING(&hbin->ps) ) {
890 if ( !(nk->values = PRS_ALLOC_MEM( &hbin->ps, REGF_VK_REC, nk->num_values ) ) )
891 return False;
894 /* convert the offset to something relative to this HBIN block */
896 if ( !prs_set_offset( &hbin->ps, nk->values_off+HBIN_HDR_SIZE-hbin->first_hbin_off-sizeof(uint32)) )
897 return False;
899 if ( MARSHALLING( &hbin->ps) ) {
900 record_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
901 record_size = (record_size - 1) ^ 0xFFFFFFFF;
904 if ( !prs_uint32( "record_size", &hbin->ps, depth, &record_size ) )
905 return False;
907 for ( i=0; i<nk->num_values; i++ ) {
908 if ( !prs_uint32( "vk_off", &hbin->ps, depth, &nk->values[i].rec_off ) )
909 return False;
912 for ( i=0; i<nk->num_values; i++ ) {
913 REGF_HBIN *sub_hbin = hbin;
914 uint32 new_offset;
916 if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) ) {
917 sub_hbin = lookup_hbin_block( file, nk->values[i].rec_off );
918 if ( !sub_hbin ) {
919 DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n",
920 nk->values[i].hbin_off));
921 return False;
925 new_offset = nk->values[i].rec_off + HBIN_HDR_SIZE - sub_hbin->first_hbin_off;
926 if ( !prs_set_offset( &sub_hbin->ps, new_offset ) )
927 return False;
928 if ( !hbin_prs_vk_rec( "vk_rec", sub_hbin, depth, &nk->values[i], file ) )
929 return False;
932 if ( MARSHALLING(&hbin->ps) )
933 hbin->dirty = True;
936 return True;
940 /*******************************************************************
941 *******************************************************************/
943 static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32 offset )
945 REGF_SK_REC *p_sk;
947 for ( p_sk=file->sec_desc_list; p_sk; p_sk=p_sk->next ) {
948 if ( p_sk->sk_off == offset )
949 return p_sk;
952 return NULL;
955 /*******************************************************************
956 *******************************************************************/
958 static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, struct security_descriptor *sd )
960 REGF_SK_REC *p;
962 for ( p=file->sec_desc_list; p; p=p->next ) {
963 if ( security_descriptor_equal( p->sec_desc, sd ) )
964 return p;
967 /* failure */
969 return NULL;
972 /*******************************************************************
973 *******************************************************************/
975 static bool hbin_prs_key( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk )
977 int depth = 0;
978 REGF_HBIN *sub_hbin;
980 prs_debug(&hbin->ps, depth, "", "fetch_key");
981 depth++;
983 /* get the initial nk record */
985 if ( !prs_nk_rec( "nk_rec", &hbin->ps, depth, nk ))
986 return False;
988 /* fill in values */
990 if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) {
991 sub_hbin = hbin;
992 if ( !hbin_contains_offset( hbin, nk->values_off ) ) {
993 sub_hbin = lookup_hbin_block( file, nk->values_off );
994 if ( !sub_hbin ) {
995 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n",
996 nk->values_off));
997 return False;
1001 if ( !hbin_prs_vk_records( "vk_rec", sub_hbin, depth, nk, file ))
1002 return False;
1005 /* now get subkeys */
1007 if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) {
1008 sub_hbin = hbin;
1009 if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) {
1010 sub_hbin = lookup_hbin_block( file, nk->subkeys_off );
1011 if ( !sub_hbin ) {
1012 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n",
1013 nk->subkeys_off));
1014 return False;
1018 if ( !hbin_prs_lf_records( "lf_rec", sub_hbin, depth, nk ))
1019 return False;
1022 /* get the to the security descriptor. First look if we have already parsed it */
1024 if ( (nk->sk_off!=REGF_OFFSET_NONE) && !( nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )) ) {
1026 sub_hbin = hbin;
1027 if ( !hbin_contains_offset( hbin, nk->sk_off ) ) {
1028 sub_hbin = lookup_hbin_block( file, nk->sk_off );
1029 if ( !sub_hbin ) {
1030 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_offset [0x%x]\n",
1031 nk->subkeys_off));
1032 return False;
1036 if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) )
1037 return False;
1038 nk->sec_desc->sk_off = nk->sk_off;
1039 if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin, depth, nk->sec_desc ))
1040 return False;
1042 /* add to the list of security descriptors (ref_count has been read from the files) */
1044 nk->sec_desc->sk_off = nk->sk_off;
1045 DLIST_ADD( file->sec_desc_list, nk->sec_desc );
1048 return True;
1051 /*******************************************************************
1052 *******************************************************************/
1054 static bool next_record( REGF_HBIN *hbin, const char *hdr, bool *eob )
1056 uint8 header[REC_HDR_SIZE];
1057 uint32 record_size;
1058 uint32 curr_off, block_size;
1059 bool found = False;
1060 prs_struct *ps = &hbin->ps;
1062 curr_off = prs_offset( ps );
1063 if ( curr_off == 0 )
1064 prs_set_offset( ps, HBIN_HEADER_REC_SIZE );
1066 /* assume that the current offset is at the record header
1067 and we need to backup to read the record size */
1069 curr_off -= sizeof(uint32);
1071 block_size = prs_data_size( ps );
1072 record_size = 0;
1073 memset( header, 0x0, sizeof(uint8)*REC_HDR_SIZE );
1074 while ( !found ) {
1076 curr_off = curr_off+record_size;
1077 if ( curr_off >= block_size )
1078 break;
1080 if ( !prs_set_offset( &hbin->ps, curr_off) )
1081 return False;
1083 if ( !prs_uint32( "record_size", ps, 0, &record_size ) )
1084 return False;
1085 if ( !prs_uint8s( True, "header", ps, 0, header, REC_HDR_SIZE ) )
1086 return False;
1088 if ( record_size & 0x80000000 ) {
1089 /* absolute_value(record_size) */
1090 record_size = (record_size ^ 0xffffffff) + 1;
1093 if ( memcmp( header, hdr, REC_HDR_SIZE ) == 0 ) {
1094 found = True;
1095 curr_off += sizeof(uint32);
1099 /* mark prs_struct as done ( at end ) if no more SK records */
1100 /* mark end-of-block as True */
1102 if ( !found ) {
1103 prs_set_offset( &hbin->ps, prs_data_size(&hbin->ps) );
1104 *eob = True;
1105 return False;
1108 if ( !prs_set_offset( ps, curr_off ) )
1109 return False;
1111 return True;
1114 /*******************************************************************
1115 *******************************************************************/
1117 static bool next_nk_record( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk, bool *eob )
1119 if ( next_record( hbin, "nk", eob ) && hbin_prs_key( file, hbin, nk ) )
1120 return True;
1122 return False;
1125 /*******************************************************************
1126 Intialize the newly created REGF_BLOCK in *file and write the
1127 block header to disk
1128 *******************************************************************/
1130 static bool init_regf_block( REGF_FILE *file )
1132 prs_struct ps;
1133 bool result = True;
1135 if ( !prs_init( &ps, REGF_BLOCKSIZE, file->mem_ctx, MARSHALL ) )
1136 return False;
1138 memcpy( file->header, "regf", REGF_HDR_SIZE );
1139 file->data_offset = 0x20;
1140 file->last_block = 0x1000;
1142 /* set mod time */
1144 unix_to_nt_time( &file->mtime, time(NULL) );
1146 /* hard coded values...no diea what these are ... maybe in time */
1148 file->unknown1 = 0x2;
1149 file->unknown2 = 0x1;
1150 file->unknown3 = 0x3;
1151 file->unknown4 = 0x0;
1152 file->unknown5 = 0x1;
1153 file->unknown6 = 0x1;
1155 /* write header to the buffer */
1157 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1158 result = False;
1159 goto out;
1162 /* calculate the checksum, re-marshall data (to include the checksum)
1163 and write to disk */
1165 file->checksum = regf_block_checksum( &ps );
1166 prs_set_offset( &ps, 0 );
1167 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1168 result = False;
1169 goto out;
1172 if ( write_block( file, &ps, 0 ) == -1 ) {
1173 DEBUG(0,("init_regf_block: Failed to initialize registry header block!\n"));
1174 result = False;
1175 goto out;
1178 out:
1179 prs_mem_free( &ps );
1181 return result;
1183 /*******************************************************************
1184 Open the registry file and then read in the REGF block to get the
1185 first hbin offset.
1186 *******************************************************************/
1188 REGF_FILE* regfio_open( const char *filename, int flags, int mode )
1190 REGF_FILE *rb;
1192 if ( !(rb = SMB_MALLOC_P(REGF_FILE)) ) {
1193 DEBUG(0,("ERROR allocating memory\n"));
1194 return NULL;
1196 ZERO_STRUCTP( rb );
1197 rb->fd = -1;
1199 if ( !(rb->mem_ctx = talloc_init( "read_regf_block" )) ) {
1200 regfio_close( rb );
1201 return NULL;
1204 rb->open_flags = flags;
1206 /* open and existing file */
1208 if ( (rb->fd = open(filename, flags, mode)) == -1 ) {
1209 DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename, strerror(errno)));
1210 regfio_close( rb );
1211 return NULL;
1214 /* check if we are creating a new file or overwriting an existing one */
1216 if ( flags & (O_CREAT|O_TRUNC) ) {
1217 if ( !init_regf_block( rb ) ) {
1218 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1219 regfio_close( rb );
1220 return NULL;
1223 /* success */
1224 return rb;
1227 /* read in an existing file */
1229 if ( !read_regf_block( rb ) ) {
1230 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1231 regfio_close( rb );
1232 return NULL;
1235 /* success */
1237 return rb;
1240 /*******************************************************************
1241 *******************************************************************/
1243 static void regfio_mem_free( REGF_FILE *file )
1245 /* free any talloc()'d memory */
1247 if ( file && file->mem_ctx )
1248 talloc_destroy( file->mem_ctx );
1251 /*******************************************************************
1252 *******************************************************************/
1254 int regfio_close( REGF_FILE *file )
1256 int fd;
1258 /* cleanup for a file opened for write */
1260 if ((file->fd != -1) && (file->open_flags & (O_WRONLY|O_RDWR))) {
1261 prs_struct ps;
1262 REGF_SK_REC *sk;
1264 /* write of sd list */
1266 for ( sk=file->sec_desc_list; sk; sk=sk->next ) {
1267 hbin_prs_sk_rec( "sk_rec", sk->hbin, 0, sk );
1270 /* flush any dirty blocks */
1272 while ( file->block_list ) {
1273 hbin_block_close( file, file->block_list );
1276 ZERO_STRUCT( ps );
1278 unix_to_nt_time( &file->mtime, time(NULL) );
1280 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) != -1 ) {
1281 /* now use for writing */
1282 prs_switch_type( &ps, MARSHALL );
1284 /* stream the block once, generate the checksum,
1285 and stream it again */
1286 prs_set_offset( &ps, 0 );
1287 prs_regf_block( "regf_blocK", &ps, 0, file );
1288 file->checksum = regf_block_checksum( &ps );
1289 prs_set_offset( &ps, 0 );
1290 prs_regf_block( "regf_blocK", &ps, 0, file );
1292 /* now we are ready to write it to disk */
1293 if ( write_block( file, &ps, 0 ) == -1 )
1294 DEBUG(0,("regfio_close: failed to update the regf header block!\n"));
1297 prs_mem_free( &ps );
1300 regfio_mem_free( file );
1302 /* nothing tdo do if there is no open file */
1304 if (file->fd == -1)
1305 return 0;
1307 fd = file->fd;
1308 file->fd = -1;
1309 SAFE_FREE( file );
1311 return close( fd );
1314 /*******************************************************************
1315 *******************************************************************/
1317 static void regfio_flush( REGF_FILE *file )
1319 REGF_HBIN *hbin;
1321 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
1322 write_hbin_block( file, hbin );
1326 /*******************************************************************
1327 There should be only *one* root key in the registry file based
1328 on my experience. --jerry
1329 *******************************************************************/
1331 REGF_NK_REC* regfio_rootkey( REGF_FILE *file )
1333 REGF_NK_REC *nk;
1334 REGF_HBIN *hbin;
1335 uint32 offset = REGF_BLOCKSIZE;
1336 bool found = False;
1337 bool eob;
1339 if ( !file )
1340 return NULL;
1342 if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) ) {
1343 DEBUG(0,("regfio_rootkey: talloc() failed!\n"));
1344 return NULL;
1347 /* scan through the file on HBIN block at a time looking
1348 for an NK record with a type == 0x002c.
1349 Normally this is the first nk record in the first hbin
1350 block (but I'm not assuming that for now) */
1352 while ( (hbin = read_hbin_block( file, offset )) ) {
1353 eob = False;
1355 while ( !eob) {
1356 if ( next_nk_record( file, hbin, nk, &eob ) ) {
1357 if ( nk->key_type == NK_TYPE_ROOTKEY ) {
1358 found = True;
1359 break;
1362 prs_mem_free( &hbin->ps );
1365 if ( found )
1366 break;
1368 offset += hbin->block_size;
1371 if ( !found ) {
1372 DEBUG(0,("regfio_rootkey: corrupt registry file ? No root key record located\n"));
1373 return NULL;
1376 DLIST_ADD( file->block_list, hbin );
1378 return nk;
1381 /*******************************************************************
1382 This acts as an interator over the subkeys defined for a given
1383 NK record. Remember that offsets are from the *first* HBIN block.
1384 *******************************************************************/
1386 REGF_NK_REC* regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk )
1388 REGF_NK_REC *subkey;
1389 REGF_HBIN *hbin;
1390 uint32 nk_offset;
1392 /* see if there is anything left to report */
1394 if ( !nk || (nk->subkeys_off==REGF_OFFSET_NONE) || (nk->subkey_index >= nk->num_subkeys) )
1395 return NULL;
1397 /* find the HBIN block which should contain the nk record */
1399 if ( !(hbin = lookup_hbin_block( file, nk->subkeys.hashes[nk->subkey_index].nk_off )) ) {
1400 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n",
1401 nk->subkeys.hashes[nk->subkey_index].nk_off));
1402 return NULL;
1405 nk_offset = nk->subkeys.hashes[nk->subkey_index].nk_off;
1406 if ( !prs_set_offset( &hbin->ps, (HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off) ) )
1407 return NULL;
1409 nk->subkey_index++;
1410 if ( !(subkey = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) )
1411 return NULL;
1413 if ( !hbin_prs_key( file, hbin, subkey ) )
1414 return NULL;
1416 return subkey;
1420 /*******************************************************************
1421 *******************************************************************/
1423 static REGF_HBIN* regf_hbin_allocate( REGF_FILE *file, uint32 block_size )
1425 REGF_HBIN *hbin;
1426 SMB_STRUCT_STAT sbuf;
1428 if ( !(hbin = TALLOC_ZERO_P( file->mem_ctx, REGF_HBIN )) )
1429 return NULL;
1431 memcpy( hbin->header, "hbin", sizeof(HBIN_HDR_SIZE) );
1434 if (sys_fstat(file->fd, &sbuf, false)) {
1435 DEBUG(0,("regf_hbin_allocate: stat() failed! (%s)\n", strerror(errno)));
1436 return NULL;
1439 hbin->file_off = sbuf.st_ex_size;
1441 hbin->free_off = HBIN_HEADER_REC_SIZE;
1442 hbin->free_size = block_size - hbin->free_off + sizeof(uint32);;
1444 hbin->block_size = block_size;
1445 hbin->first_hbin_off = hbin->file_off - REGF_BLOCKSIZE;
1447 if ( !prs_init( &hbin->ps, block_size, file->mem_ctx, MARSHALL ) )
1448 return NULL;
1450 if ( !prs_hbin_block( "new_hbin", &hbin->ps, 0, hbin ) )
1451 return NULL;
1453 if ( !write_hbin_block( file, hbin ) )
1454 return NULL;
1456 file->last_block = hbin->file_off;
1458 return hbin;
1461 /*******************************************************************
1462 *******************************************************************/
1464 static void update_free_space( REGF_HBIN *hbin, uint32 size_used )
1466 hbin->free_off += size_used;
1467 hbin->free_size -= size_used;
1469 if ( hbin->free_off >= hbin->block_size ) {
1470 hbin->free_off = REGF_OFFSET_NONE;
1473 return;
1476 /*******************************************************************
1477 *******************************************************************/
1479 static REGF_HBIN* find_free_space( REGF_FILE *file, uint32 size )
1481 REGF_HBIN *hbin, *p_hbin;
1482 uint32 block_off;
1483 bool cached;
1485 /* check open block list */
1487 for ( hbin=file->block_list; hbin!=NULL; hbin=hbin->next ) {
1488 /* only check blocks that actually have available space */
1490 if ( hbin->free_off == REGF_OFFSET_NONE )
1491 continue;
1493 /* check for a large enough available chunk */
1495 if ( (hbin->block_size - hbin->free_off) >= size ) {
1496 DLIST_PROMOTE( file->block_list, hbin );
1497 goto done;
1501 /* parse the file until we find a block with
1502 enough free space; save the last non-filled hbin */
1504 block_off = REGF_BLOCKSIZE;
1505 do {
1506 /* cleanup before the next round */
1507 cached = False;
1508 if ( hbin )
1509 prs_mem_free( &hbin->ps );
1511 hbin = read_hbin_block( file, block_off );
1513 if ( hbin ) {
1515 /* make sure that we don't already have this block in memory */
1517 for ( p_hbin=file->block_list; p_hbin!=NULL; p_hbin=p_hbin->next ) {
1518 if ( p_hbin->file_off == hbin->file_off ) {
1519 cached = True;
1520 break;
1524 block_off = hbin->file_off + hbin->block_size;
1526 if ( cached ) {
1527 prs_mem_free( &hbin->ps );
1528 hbin = NULL;
1529 continue;
1532 /* if (cached block or (new block and not enough free space)) then continue looping */
1533 } while ( cached || (hbin && (hbin->free_size < size)) );
1535 /* no free space; allocate a new one */
1537 if ( !hbin ) {
1538 uint32 alloc_size;
1540 /* allocate in multiples of REGF_ALLOC_BLOCK; make sure (size + hbin_header) fits */
1542 alloc_size = (((size+HBIN_HEADER_REC_SIZE) / REGF_ALLOC_BLOCK ) + 1 ) * REGF_ALLOC_BLOCK;
1544 if ( !(hbin = regf_hbin_allocate( file, alloc_size )) ) {
1545 DEBUG(0,("find_free_space: regf_hbin_allocate() failed!\n"));
1546 return NULL;
1548 DLIST_ADD( file->block_list, hbin );
1551 done:
1552 /* set the offset to be ready to write */
1554 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) )
1555 return NULL;
1557 /* write the record size as a placeholder for now, it should be
1558 probably updated by the caller once it all of the data necessary
1559 for the record */
1561 if ( !prs_uint32("allocated_size", &hbin->ps, 0, &size) )
1562 return False;
1564 update_free_space( hbin, size );
1566 return hbin;
1569 /*******************************************************************
1570 *******************************************************************/
1572 static uint32 sk_record_data_size( struct security_descriptor * sd )
1574 uint32 size, size_mod8;
1576 size_mod8 = 0;
1578 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1580 size = sizeof(uint32)*5 + ndr_size_security_descriptor(sd, 0) + sizeof(uint32);
1582 /* multiple of 8 */
1583 size_mod8 = size & 0xfffffff8;
1584 if ( size_mod8 < size )
1585 size_mod8 += 8;
1587 return size_mod8;
1590 /*******************************************************************
1591 *******************************************************************/
1593 static uint32 vk_record_data_size( REGF_VK_REC *vk )
1595 uint32 size, size_mod8;
1597 size_mod8 = 0;
1599 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1601 size = REC_HDR_SIZE + (sizeof(uint16)*3) + (sizeof(uint32)*3) + sizeof(uint32);
1603 if ( vk->valuename )
1604 size += strlen(vk->valuename);
1606 /* multiple of 8 */
1607 size_mod8 = size & 0xfffffff8;
1608 if ( size_mod8 < size )
1609 size_mod8 += 8;
1611 return size_mod8;
1614 /*******************************************************************
1615 *******************************************************************/
1617 static uint32 lf_record_data_size( uint32 num_keys )
1619 uint32 size, size_mod8;
1621 size_mod8 = 0;
1623 /* the record size is sizeof(hdr) + num_keys + sizeof of hash_array + data_size_uint32 */
1625 size = REC_HDR_SIZE + sizeof(uint16) + (sizeof(REGF_HASH_REC) * num_keys) + sizeof(uint32);
1627 /* multiple of 8 */
1628 size_mod8 = size & 0xfffffff8;
1629 if ( size_mod8 < size )
1630 size_mod8 += 8;
1632 return size_mod8;
1635 /*******************************************************************
1636 *******************************************************************/
1638 static uint32 nk_record_data_size( REGF_NK_REC *nk )
1640 uint32 size, size_mod8;
1642 size_mod8 = 0;
1644 /* the record size is static + length_of_keyname + length_of_classname + data_size_uint32 */
1646 size = 0x4c + strlen(nk->keyname) + sizeof(uint32);
1648 if ( nk->classname )
1649 size += strlen( nk->classname );
1651 /* multiple of 8 */
1652 size_mod8 = size & 0xfffffff8;
1653 if ( size_mod8 < size )
1654 size_mod8 += 8;
1656 return size_mod8;
1659 /*******************************************************************
1660 *******************************************************************/
1662 static bool create_vk_record(REGF_FILE *file, REGF_VK_REC *vk,
1663 struct regval_blob *value)
1665 char *name = regval_name(value);
1666 REGF_HBIN *data_hbin;
1668 ZERO_STRUCTP( vk );
1670 memcpy( vk->header, "vk", REC_HDR_SIZE );
1672 if ( name ) {
1673 vk->valuename = talloc_strdup( file->mem_ctx, regval_name(value) );
1674 vk->flag = VK_FLAG_NAME_PRESENT;
1677 vk->data_size = regval_size( value );
1678 vk->type = regval_type( value );
1680 if ( vk->data_size > sizeof(uint32) ) {
1681 uint32 data_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
1683 vk->data = (uint8 *)TALLOC_MEMDUP( file->mem_ctx,
1684 regval_data_p(value),
1685 vk->data_size );
1686 if (vk->data == NULL) {
1687 return False;
1690 /* go ahead and store the offset....we'll pick this hbin block back up when
1691 we stream the data */
1693 if ((data_hbin = find_free_space(file, data_size )) == NULL) {
1694 return False;
1696 vk->data_off = prs_offset( &data_hbin->ps ) + data_hbin->first_hbin_off - HBIN_HDR_SIZE;
1698 else {
1699 /* make sure we don't try to copy from a NULL value pointer */
1701 if ( vk->data_size != 0 )
1702 memcpy( &vk->data_off, regval_data_p(value), sizeof(uint32) );
1703 vk->data_size |= VK_DATA_IN_OFFSET;
1706 return True;
1709 /*******************************************************************
1710 *******************************************************************/
1712 static int hashrec_cmp( REGF_HASH_REC *h1, REGF_HASH_REC *h2 )
1714 return StrCaseCmp( h1->fullname, h2->fullname );
1717 /*******************************************************************
1718 *******************************************************************/
1720 REGF_NK_REC* regfio_write_key( REGF_FILE *file, const char *name,
1721 struct regval_ctr *values, struct regsubkey_ctr *subkeys,
1722 struct security_descriptor *sec_desc, REGF_NK_REC *parent )
1724 REGF_NK_REC *nk;
1725 REGF_HBIN *vlist_hbin = NULL;
1726 uint32 size;
1728 if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) )
1729 return NULL;
1731 memcpy( nk->header, "nk", REC_HDR_SIZE );
1733 if ( !parent )
1734 nk->key_type = NK_TYPE_ROOTKEY;
1735 else
1736 nk->key_type = NK_TYPE_NORMALKEY;
1738 /* store the parent offset (or -1 if a the root key */
1740 nk->parent_off = parent ? (parent->hbin_off + parent->hbin->file_off - REGF_BLOCKSIZE - HBIN_HDR_SIZE ) : REGF_OFFSET_NONE;
1742 /* no classname currently */
1744 nk->classname_off = REGF_OFFSET_NONE;
1745 nk->classname = NULL;
1746 nk->keyname = talloc_strdup( file->mem_ctx, name );
1748 /* current modification time */
1750 unix_to_nt_time( &nk->mtime, time(NULL) );
1752 /* allocate the record on disk */
1754 size = nk_record_data_size( nk );
1755 nk->rec_size = ( size - 1 ) ^ 0XFFFFFFFF;
1756 if ((nk->hbin = find_free_space( file, size )) == NULL) {
1757 return NULL;
1759 nk->hbin_off = prs_offset( &nk->hbin->ps );
1761 /* Update the hash record in the parent */
1763 if ( parent ) {
1764 REGF_HASH_REC *hash = &parent->subkeys.hashes[parent->subkey_index];
1766 hash->nk_off = prs_offset( &nk->hbin->ps ) + nk->hbin->first_hbin_off - HBIN_HDR_SIZE;
1767 memcpy( hash->keycheck, name, sizeof(uint32) );
1768 hash->fullname = talloc_strdup( file->mem_ctx, name );
1769 parent->subkey_index++;
1771 /* sort the list by keyname */
1772 TYPESAFE_QSORT(parent->subkeys.hashes, parent->subkey_index, hashrec_cmp);
1774 if ( !hbin_prs_lf_records( "lf_rec", parent->subkeys.hbin, 0, parent ) )
1775 return False;
1778 /* write the security descriptor */
1780 nk->sk_off = REGF_OFFSET_NONE;
1781 if ( sec_desc ) {
1782 uint32 sk_size = sk_record_data_size( sec_desc );
1783 REGF_HBIN *sk_hbin;
1785 /* search for it in the existing list of sd's */
1787 if ( (nk->sec_desc = find_sk_record_by_sec_desc( file, sec_desc )) == NULL ) {
1788 /* not found so add it to the list */
1790 if (!(sk_hbin = find_free_space( file, sk_size ))) {
1791 return NULL;
1794 if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) )
1795 return NULL;
1797 /* now we have to store the security descriptor in the list and
1798 update the offsets */
1800 memcpy( nk->sec_desc->header, "sk", REC_HDR_SIZE );
1801 nk->sec_desc->hbin = sk_hbin;
1802 nk->sec_desc->hbin_off = prs_offset( &sk_hbin->ps );
1803 nk->sec_desc->sk_off = prs_offset( &sk_hbin->ps ) + sk_hbin->first_hbin_off - HBIN_HDR_SIZE;
1804 nk->sec_desc->rec_size = (sk_size-1) ^ 0xFFFFFFFF;
1806 nk->sec_desc->sec_desc = sec_desc;
1807 nk->sec_desc->ref_count = 0;
1809 /* size value must be self-inclusive */
1810 nk->sec_desc->size = ndr_size_security_descriptor(sec_desc, 0)
1811 + sizeof(uint32);
1813 DLIST_ADD_END( file->sec_desc_list, nk->sec_desc, REGF_SK_REC *);
1815 /* update the offsets for us and the previous sd in the list.
1816 if this is the first record, then just set the next and prev
1817 offsets to ourself. */
1819 if ( DLIST_PREV(nk->sec_desc) ) {
1820 REGF_SK_REC *prev = DLIST_PREV(nk->sec_desc);
1822 nk->sec_desc->prev_sk_off = prev->hbin_off + prev->hbin->first_hbin_off - HBIN_HDR_SIZE;
1823 prev->next_sk_off = nk->sec_desc->sk_off;
1825 /* the end must loop around to the front */
1826 nk->sec_desc->next_sk_off = file->sec_desc_list->sk_off;
1828 /* and first must loop around to the tail */
1829 file->sec_desc_list->prev_sk_off = nk->sec_desc->sk_off;
1830 } else {
1831 nk->sec_desc->prev_sk_off = nk->sec_desc->sk_off;
1832 nk->sec_desc->next_sk_off = nk->sec_desc->sk_off;
1836 /* bump the reference count +1 */
1838 nk->sk_off = nk->sec_desc->sk_off;
1839 nk->sec_desc->ref_count++;
1842 /* write the subkeys */
1844 nk->subkeys_off = REGF_OFFSET_NONE;
1845 if ( (nk->num_subkeys = regsubkey_ctr_numkeys( subkeys )) != 0 ) {
1846 uint32 lf_size = lf_record_data_size( nk->num_subkeys );
1847 uint32 namelen;
1848 int i;
1850 if (!(nk->subkeys.hbin = find_free_space( file, lf_size ))) {
1851 return NULL;
1853 nk->subkeys.hbin_off = prs_offset( &nk->subkeys.hbin->ps );
1854 nk->subkeys.rec_size = (lf_size-1) ^ 0xFFFFFFFF;
1855 nk->subkeys_off = prs_offset( &nk->subkeys.hbin->ps ) + nk->subkeys.hbin->first_hbin_off - HBIN_HDR_SIZE;
1857 memcpy( nk->subkeys.header, "lf", REC_HDR_SIZE );
1859 nk->subkeys.num_keys = nk->num_subkeys;
1860 if (nk->subkeys.num_keys) {
1861 if ( !(nk->subkeys.hashes = TALLOC_ZERO_ARRAY( file->mem_ctx, REGF_HASH_REC, nk->subkeys.num_keys )) )
1862 return NULL;
1863 } else {
1864 nk->subkeys.hashes = NULL;
1866 nk->subkey_index = 0;
1868 /* update the max_bytes_subkey{name,classname} fields */
1869 for ( i=0; i<nk->num_subkeys; i++ ) {
1870 namelen = strlen( regsubkey_ctr_specific_key(subkeys, i) );
1871 if ( namelen*2 > nk->max_bytes_subkeyname )
1872 nk->max_bytes_subkeyname = namelen * 2;
1876 /* write the values */
1878 nk->values_off = REGF_OFFSET_NONE;
1879 if ( (nk->num_values = regval_ctr_numvals( values )) != 0 ) {
1880 uint32 vlist_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
1881 int i;
1883 if (!(vlist_hbin = find_free_space( file, vlist_size ))) {
1884 return NULL;
1886 nk->values_off = prs_offset( &vlist_hbin->ps ) + vlist_hbin->first_hbin_off - HBIN_HDR_SIZE;
1888 if (nk->num_values) {
1889 if ( !(nk->values = TALLOC_ARRAY( file->mem_ctx, REGF_VK_REC, nk->num_values )) )
1890 return NULL;
1891 } else {
1892 nk->values = NULL;
1895 /* create the vk records */
1897 for ( i=0; i<nk->num_values; i++ ) {
1898 uint32 vk_size, namelen, datalen;
1899 struct regval_blob *r;
1901 r = regval_ctr_specific_value( values, i );
1902 create_vk_record( file, &nk->values[i], r );
1903 vk_size = vk_record_data_size( &nk->values[i] );
1904 nk->values[i].hbin = find_free_space( file, vk_size );
1905 nk->values[i].hbin_off = prs_offset( &nk->values[i].hbin->ps );
1906 nk->values[i].rec_size = ( vk_size - 1 ) ^ 0xFFFFFFFF;
1907 nk->values[i].rec_off = prs_offset( &nk->values[i].hbin->ps )
1908 + nk->values[i].hbin->first_hbin_off
1909 - HBIN_HDR_SIZE;
1911 /* update the max bytes fields if necessary */
1913 namelen = strlen( regval_name(r) );
1914 if ( namelen*2 > nk->max_bytes_valuename )
1915 nk->max_bytes_valuename = namelen * 2;
1917 datalen = regval_size( r );
1918 if ( datalen > nk->max_bytes_value )
1919 nk->max_bytes_value = datalen;
1923 /* stream the records */
1925 prs_set_offset( &nk->hbin->ps, nk->hbin_off );
1926 if ( !prs_nk_rec( "nk_rec", &nk->hbin->ps, 0, nk ) )
1927 return False;
1929 if ( nk->num_values ) {
1930 if ( !hbin_prs_vk_records( "vk_records", vlist_hbin, 0, nk, file ) )
1931 return False;
1935 regfio_flush( file );
1937 return nk;