s4/script/rodcdns: str type doesn't need decoding
[Samba.git] / source3 / registry / regfio.c
blob60c865d4d2268f1d0b8d5d1640f58d5d2bdcc2ba
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 "system/filesys.h"
22 #include "regfio.h"
23 #include "../librpc/gen_ndr/ndr_security.h"
24 #include "../libcli/security/security_descriptor.h"
25 #include "../libcli/security/secdesc.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_REGISTRY
30 /*******************************************************************
32 * TODO : Right now this code basically ignores classnames.
34 ******************************************************************/
36 #if defined(PARANOID_MALLOC_CHECKER)
37 #define PRS_ALLOC_MEM(ps, type, count) (type *)prs_alloc_mem_((ps),sizeof(type),(count))
38 #else
39 #define PRS_ALLOC_MEM(ps, type, count) (type *)prs_alloc_mem((ps),sizeof(type),(count))
40 #endif
42 /*******************************************************************
43 Reads or writes an NTTIME structure.
44 ********************************************************************/
46 static bool smb_io_time(const char *desc, NTTIME *nttime, prs_struct *ps, int depth)
48 uint32_t low, high;
49 if (nttime == NULL)
50 return False;
52 prs_debug(ps, depth, desc, "smb_io_time");
53 depth++;
55 if(!prs_align(ps))
56 return False;
58 if (MARSHALLING(ps)) {
59 low = *nttime & 0xFFFFFFFF;
60 high = *nttime >> 32;
63 if(!prs_uint32("low ", ps, depth, &low)) /* low part */
64 return False;
65 if(!prs_uint32("high", ps, depth, &high)) /* high part */
66 return False;
68 if (UNMARSHALLING(ps)) {
69 *nttime = (((uint64_t)high << 32) + low);
72 return True;
75 /*******************************************************************
76 *******************************************************************/
78 static int write_block( REGF_FILE *file, prs_struct *ps, uint32_t offset )
80 int bytes_written, returned;
81 char *buffer = prs_data_p( ps );
82 uint32_t buffer_size = prs_data_size( ps );
83 SMB_STRUCT_STAT sbuf;
85 if ( file->fd == -1 )
86 return -1;
88 /* check for end of file */
90 if (sys_fstat(file->fd, &sbuf, false)) {
91 DEBUG(0,("write_block: stat() failed! (%s)\n", strerror(errno)));
92 return -1;
95 if ( lseek( file->fd, offset, SEEK_SET ) == -1 ) {
96 DEBUG(0,("write_block: lseek() failed! (%s)\n", strerror(errno) ));
97 return -1;
100 bytes_written = returned = 0;
101 while ( bytes_written < buffer_size ) {
102 if ( (returned = write( file->fd, buffer+bytes_written, buffer_size-bytes_written )) == -1 ) {
103 DEBUG(0,("write_block: write() failed! (%s)\n", strerror(errno) ));
104 return False;
107 bytes_written += returned;
110 return bytes_written;
113 /*******************************************************************
114 *******************************************************************/
116 static int read_block( REGF_FILE *file, prs_struct *ps, uint32_t file_offset, uint32_t block_size )
118 int bytes_read, returned;
119 char *buffer;
120 SMB_STRUCT_STAT sbuf;
122 /* check for end of file */
124 if (sys_fstat(file->fd, &sbuf, false)) {
125 DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno)));
126 return -1;
129 if ( (size_t)file_offset >= sbuf.st_ex_size )
130 return -1;
132 /* if block_size == 0, we are parsing HBIN records and need
133 to read some of the header to get the block_size from there */
135 if ( block_size == 0 ) {
136 char hdr[0x20];
138 if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
139 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
140 return -1;
143 returned = read( file->fd, hdr, 0x20 );
144 if ( (returned == -1) || (returned < 0x20) ) {
145 DEBUG(0,("read_block: failed to read in HBIN header. Is the file corrupt?\n"));
146 return -1;
149 /* make sure this is an hbin header */
151 if ( strncmp( hdr, "hbin", HBIN_HDR_SIZE ) != 0 ) {
152 DEBUG(0,("read_block: invalid block header!\n"));
153 return -1;
156 block_size = IVAL( hdr, 0x08 );
159 DEBUG(10,("read_block: block_size == 0x%x\n", block_size ));
161 /* set the offset, initialize the buffer, and read the block from disk */
163 if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
164 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
165 return -1;
168 if (!prs_init( ps, block_size, file->mem_ctx, UNMARSHALL )) {
169 DEBUG(0,("read_block: prs_init() failed! (%s)\n", strerror(errno) ));
170 return -1;
172 buffer = prs_data_p( ps );
173 bytes_read = returned = 0;
175 while ( bytes_read < block_size ) {
176 if ( (returned = read( file->fd, buffer+bytes_read, block_size-bytes_read )) == -1 ) {
177 DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno) ));
178 return False;
180 if ( (returned == 0) && (bytes_read < block_size) ) {
181 DEBUG(0,("read_block: not a valid registry file ?\n" ));
182 return False;
185 bytes_read += returned;
188 return bytes_read;
191 /*******************************************************************
192 *******************************************************************/
194 static bool write_hbin_block( REGF_FILE *file, REGF_HBIN *hbin )
196 if ( !hbin->dirty )
197 return True;
199 /* write free space record if any is available */
201 if ( hbin->free_off != REGF_OFFSET_NONE ) {
202 uint32_t header = 0xffffffff;
204 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32_t) ) )
205 return False;
206 if ( !prs_uint32( "free_size", &hbin->ps, 0, &hbin->free_size ) )
207 return False;
208 if ( !prs_uint32( "free_header", &hbin->ps, 0, &header ) )
209 return False;
212 hbin->dirty = (write_block( file, &hbin->ps, hbin->file_off ) != -1);
214 return hbin->dirty;
217 /*******************************************************************
218 *******************************************************************/
220 static bool hbin_block_close( REGF_FILE *file, REGF_HBIN *hbin )
222 REGF_HBIN *p;
224 /* remove the block from the open list and flush it to disk */
226 for ( p=file->block_list; p && p!=hbin; p=p->next )
229 if ( p == hbin ) {
230 DLIST_REMOVE( file->block_list, hbin );
232 else
233 DEBUG(0,("hbin_block_close: block not in open list!\n"));
235 if ( !write_hbin_block( file, hbin ) )
236 return False;
238 return True;
241 /*******************************************************************
242 *******************************************************************/
244 static bool prs_regf_block( const char *desc, prs_struct *ps, int depth, REGF_FILE *file )
246 prs_debug(ps, depth, desc, "prs_regf_block");
247 depth++;
249 if ( !prs_uint8s( True, "header", ps, depth, (uint8_t *)file->header, sizeof( file->header )) )
250 return False;
252 /* yes, these values are always identical so store them only once */
254 if ( !prs_uint32( "unknown1", ps, depth, &file->unknown1 ))
255 return False;
256 if ( !prs_uint32( "unknown1 (again)", ps, depth, &file->unknown1 ))
257 return False;
259 /* get the modtime */
261 if ( !prs_set_offset( ps, 0x0c ) )
262 return False;
263 if ( !smb_io_time( "modtime", &file->mtime, ps, depth ) )
264 return False;
266 /* constants */
268 if ( !prs_uint32( "unknown2", ps, depth, &file->unknown2 ))
269 return False;
270 if ( !prs_uint32( "unknown3", ps, depth, &file->unknown3 ))
271 return False;
272 if ( !prs_uint32( "unknown4", ps, depth, &file->unknown4 ))
273 return False;
274 if ( !prs_uint32( "unknown5", ps, depth, &file->unknown5 ))
275 return False;
277 /* get file offsets */
279 if ( !prs_set_offset( ps, 0x24 ) )
280 return False;
281 if ( !prs_uint32( "data_offset", ps, depth, &file->data_offset ))
282 return False;
283 if ( !prs_uint32( "last_block", ps, depth, &file->last_block ))
284 return False;
286 /* one more constant */
288 if ( !prs_uint32( "unknown6", ps, depth, &file->unknown6 ))
289 return False;
291 /* get the checksum */
293 if ( !prs_set_offset( ps, 0x01fc ) )
294 return False;
295 if ( !prs_uint32( "checksum", ps, depth, &file->checksum ))
296 return False;
298 return True;
301 /*******************************************************************
302 *******************************************************************/
304 static bool prs_hbin_block( const char *desc, prs_struct *ps, int depth, REGF_HBIN *hbin )
306 uint32_t block_size2;
308 prs_debug(ps, depth, desc, "prs_hbin_block");
309 depth++;
311 if ( !prs_uint8s( True, "header", ps, depth, (uint8_t*)hbin->header, sizeof( hbin->header )) )
312 return False;
314 if ( !prs_uint32( "first_hbin_off", ps, depth, &hbin->first_hbin_off ))
315 return False;
317 /* The dosreg.cpp comments say that the block size is at 0x1c.
318 According to a WINXP NTUSER.dat file, this is wrong. The block_size
319 is at 0x08 */
321 if ( !prs_uint32( "block_size", ps, depth, &hbin->block_size ))
322 return False;
324 block_size2 = hbin->block_size;
325 prs_set_offset( ps, 0x1c );
326 if ( !prs_uint32( "block_size2", ps, depth, &block_size2 ))
327 return False;
329 if ( MARSHALLING(ps) )
330 hbin->dirty = True;
333 return True;
336 /*******************************************************************
337 *******************************************************************/
339 static bool prs_nk_rec( const char *desc, prs_struct *ps, int depth, REGF_NK_REC *nk )
341 uint16_t class_length, name_length;
342 uint32_t start;
343 uint32_t data_size, start_off, end_off;
344 uint32_t unknown_off = REGF_OFFSET_NONE;
346 nk->hbin_off = prs_offset( ps );
347 start = nk->hbin_off;
349 prs_debug(ps, depth, desc, "prs_nk_rec");
350 depth++;
352 /* back up and get the data_size */
354 if ( !prs_set_offset( ps, prs_offset(ps)-sizeof(uint32_t)) )
355 return False;
356 start_off = prs_offset( ps );
357 if ( !prs_uint32( "rec_size", ps, depth, &nk->rec_size ))
358 return False;
360 if ( !prs_uint8s( True, "header", ps, depth, (uint8_t *)nk->header, sizeof( nk->header )) )
361 return False;
363 if ( !prs_uint16( "key_type", ps, depth, &nk->key_type ))
364 return False;
365 if ( !smb_io_time( "mtime", &nk->mtime, ps, depth ))
366 return False;
368 if ( !prs_set_offset( ps, start+0x0010 ) )
369 return False;
370 if ( !prs_uint32( "parent_off", ps, depth, &nk->parent_off ))
371 return False;
372 if ( !prs_uint32( "num_subkeys", ps, depth, &nk->num_subkeys ))
373 return False;
375 if ( !prs_set_offset( ps, start+0x001c ) )
376 return False;
377 if ( !prs_uint32( "subkeys_off", ps, depth, &nk->subkeys_off ))
378 return False;
379 if ( !prs_uint32( "unknown_off", ps, depth, &unknown_off) )
380 return False;
382 if ( !prs_set_offset( ps, start+0x0024 ) )
383 return False;
384 if ( !prs_uint32( "num_values", ps, depth, &nk->num_values ))
385 return False;
386 if ( !prs_uint32( "values_off", ps, depth, &nk->values_off ))
387 return False;
388 if ( !prs_uint32( "sk_off", ps, depth, &nk->sk_off ))
389 return False;
390 if ( !prs_uint32( "classname_off", ps, depth, &nk->classname_off ))
391 return False;
393 if ( !prs_uint32( "max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname))
394 return False;
395 if ( !prs_uint32( "max_bytes_subkeyclassname", ps, depth, &nk->max_bytes_subkeyclassname))
396 return False;
397 if ( !prs_uint32( "max_bytes_valuename", ps, depth, &nk->max_bytes_valuename))
398 return False;
399 if ( !prs_uint32( "max_bytes_value", ps, depth, &nk->max_bytes_value))
400 return False;
401 if ( !prs_uint32( "unknown index", ps, depth, &nk->unk_index))
402 return False;
404 name_length = nk->keyname ? strlen(nk->keyname) : 0 ;
405 class_length = nk->classname ? strlen(nk->classname) : 0 ;
406 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
407 return False;
408 if ( !prs_uint16( "class_length", ps, depth, &class_length ))
409 return False;
411 if ( class_length ) {
415 if ( name_length ) {
416 if ( UNMARSHALLING(ps) ) {
417 if ( !(nk->keyname = PRS_ALLOC_MEM( ps, char, name_length+1 )) )
418 return False;
421 if ( !prs_uint8s( True, "name", ps, depth, (uint8_t *)nk->keyname, name_length) )
422 return False;
424 if ( UNMARSHALLING(ps) )
425 nk->keyname[name_length] = '\0';
428 end_off = prs_offset( ps );
430 /* data_size must be divisible by 8 and large enough to hold the original record */
432 data_size = ((start_off - end_off) & 0xfffffff8 );
433 if ( data_size > nk->rec_size )
434 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size));
436 if ( MARSHALLING(ps) )
437 nk->hbin->dirty = True;
439 return True;
442 /*******************************************************************
443 *******************************************************************/
445 static uint32_t regf_block_checksum( prs_struct *ps )
447 char *buffer = prs_data_p( ps );
448 uint32_t checksum, x;
449 int i;
451 /* XOR of all bytes 0x0000 - 0x01FB */
453 checksum = x = 0;
455 for ( i=0; i<0x01FB; i+=4 ) {
456 x = IVAL(buffer, i );
457 checksum ^= x;
460 return checksum;
463 /*******************************************************************
464 *******************************************************************/
466 static bool read_regf_block( REGF_FILE *file )
468 prs_struct ps;
469 uint32_t checksum;
471 /* grab the first block from the file */
473 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) == -1 )
474 return False;
476 /* parse the block and verify the checksum */
478 if ( !prs_regf_block( "regf_header", &ps, 0, file ) )
479 return False;
481 checksum = regf_block_checksum( &ps );
483 prs_mem_free( &ps );
485 if ( file->checksum != checksum ) {
486 DEBUG(0,("read_regf_block: invalid checksum\n" ));
487 return False;
490 return True;
493 /*******************************************************************
494 *******************************************************************/
496 static REGF_HBIN* read_hbin_block( REGF_FILE *file, off_t offset )
498 REGF_HBIN *hbin;
499 uint32_t record_size, curr_off, block_size, header;
501 if ( !(hbin = talloc_zero(file->mem_ctx, REGF_HBIN)) )
502 return NULL;
503 hbin->file_off = offset;
504 hbin->free_off = -1;
506 if ( read_block( file, &hbin->ps, offset, 0 ) == -1 )
507 return NULL;
509 if ( !prs_hbin_block( "hbin", &hbin->ps, 0, hbin ) )
510 return NULL;
512 /* this should be the same thing as hbin->block_size but just in case */
514 block_size = prs_data_size( &hbin->ps );
516 /* Find the available free space offset. Always at the end,
517 so walk the record list and stop when you get to the end.
518 The end is defined by a record header of 0xffffffff. The
519 previous 4 bytes contains the amount of free space remaining
520 in the hbin block. */
522 /* remember that the record_size is in the 4 bytes preceeding the record itself */
524 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE-sizeof(uint32_t) ) )
525 return NULL;
527 record_size = 0;
528 header = 0;
529 curr_off = prs_offset( &hbin->ps );
530 while ( header != 0xffffffff ) {
531 /* not done yet so reset the current offset to the
532 next record_size field */
534 curr_off = curr_off+record_size;
536 /* for some reason the record_size of the last record in
537 an hbin block can extend past the end of the block
538 even though the record fits within the remaining
539 space....aaarrrgggghhhhhh */
541 if ( curr_off >= block_size ) {
542 record_size = -1;
543 curr_off = -1;
544 break;
547 if ( !prs_set_offset( &hbin->ps, curr_off) )
548 return NULL;
550 if ( !prs_uint32( "rec_size", &hbin->ps, 0, &record_size ) )
551 return NULL;
552 if ( !prs_uint32( "header", &hbin->ps, 0, &header ) )
553 return NULL;
555 if (record_size == 0)
556 return NULL;
558 if ( record_size & 0x80000000 ) {
559 /* absolute_value(record_size) */
560 record_size = (record_size ^ 0xffffffff) + 1;
564 /* save the free space offset */
566 if ( header == 0xffffffff ) {
568 /* account for the fact that the curr_off is 4 bytes behind the actual
569 record header */
571 hbin->free_off = curr_off + sizeof(uint32_t);
572 hbin->free_size = record_size;
575 DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin->free_off));
577 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE ) )
578 return NULL;
580 return hbin;
583 /*******************************************************************
584 Input a random offset and receive the corresponding HBIN
585 block for it
586 *******************************************************************/
588 static bool hbin_contains_offset( REGF_HBIN *hbin, uint32_t offset )
590 if ( !hbin )
591 return False;
593 if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) )
594 return True;
596 return False;
599 /*******************************************************************
600 Input a random offset and receive the corresponding HBIN
601 block for it
602 *******************************************************************/
604 static REGF_HBIN* lookup_hbin_block( REGF_FILE *file, uint32_t offset )
606 REGF_HBIN *hbin = NULL;
607 uint32_t block_off;
609 /* start with the open list */
611 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
612 DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%lx]\n", hbin->file_off, (unsigned long)hbin ));
613 if ( hbin_contains_offset( hbin, offset ) )
614 return hbin;
617 if ( !hbin ) {
618 /* start at the beginning */
620 block_off = REGF_BLOCKSIZE;
621 do {
622 /* cleanup before the next round */
623 if ( hbin )
624 prs_mem_free( &hbin->ps );
626 hbin = read_hbin_block( file, block_off );
628 if ( hbin )
629 block_off = hbin->file_off + hbin->block_size;
631 } while ( hbin && !hbin_contains_offset( hbin, offset ) );
634 if ( hbin )
635 DLIST_ADD( file->block_list, hbin );
637 return hbin;
640 /*******************************************************************
641 *******************************************************************/
643 static bool prs_hash_rec( const char *desc, prs_struct *ps, int depth, REGF_HASH_REC *hash )
645 prs_debug(ps, depth, desc, "prs_hash_rec");
646 depth++;
648 if ( !prs_uint32( "nk_off", ps, depth, &hash->nk_off ))
649 return False;
650 if ( !prs_uint8s( True, "keycheck", ps, depth, hash->keycheck, sizeof( hash->keycheck )) )
651 return False;
653 return True;
656 /*******************************************************************
657 *******************************************************************/
659 static bool hbin_prs_lf_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk )
661 int i;
662 REGF_LF_REC *lf = &nk->subkeys;
663 uint32_t data_size, start_off, end_off;
665 prs_debug(&hbin->ps, depth, desc, "prs_lf_records");
666 depth++;
668 /* check if we have anything to do first */
670 if ( nk->num_subkeys == 0 )
671 return True;
673 /* move to the LF record */
675 if ( !prs_set_offset( &hbin->ps, nk->subkeys_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
676 return False;
678 /* backup and get the data_size */
680 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32_t)) )
681 return False;
682 start_off = prs_offset( &hbin->ps );
683 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &lf->rec_size ))
684 return False;
686 if ( !prs_uint8s( True, "header", &hbin->ps, depth, (uint8_t *)lf->header, sizeof( lf->header )) )
687 return False;
689 if ( !prs_uint16( "num_keys", &hbin->ps, depth, &lf->num_keys))
690 return False;
692 if ( UNMARSHALLING(&hbin->ps) ) {
693 if (lf->num_keys) {
694 if ( !(lf->hashes = PRS_ALLOC_MEM( &hbin->ps, REGF_HASH_REC, lf->num_keys )) )
695 return False;
696 } else {
697 lf->hashes = NULL;
701 for ( i=0; i<lf->num_keys; i++ ) {
702 if ( !prs_hash_rec( "hash_rec", &hbin->ps, depth, &lf->hashes[i] ) )
703 return False;
706 end_off = prs_offset( &hbin->ps );
708 /* data_size must be divisible by 8 and large enough to hold the original record */
710 data_size = ((start_off - end_off) & 0xfffffff8 );
711 if ( data_size > lf->rec_size )
712 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->rec_size));
714 if ( MARSHALLING(&hbin->ps) )
715 hbin->dirty = True;
717 return True;
720 /*******************************************************************
721 *******************************************************************/
723 static bool hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_SK_REC *sk )
725 prs_struct *ps = &hbin->ps;
726 uint16_t tag = 0xFFFF;
727 uint32_t data_size, start_off, end_off;
730 prs_debug(ps, depth, desc, "hbin_prs_sk_rec");
731 depth++;
733 if ( !prs_set_offset( &hbin->ps, sk->sk_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
734 return False;
736 /* backup and get the data_size */
738 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32_t)) )
739 return False;
740 start_off = prs_offset( &hbin->ps );
741 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &sk->rec_size ))
742 return False;
744 if ( !prs_uint8s( True, "header", ps, depth, (uint8_t *)sk->header, sizeof( sk->header )) )
745 return False;
746 if ( !prs_uint16( "tag", ps, depth, &tag))
747 return False;
749 if ( !prs_uint32( "prev_sk_off", ps, depth, &sk->prev_sk_off))
750 return False;
751 if ( !prs_uint32( "next_sk_off", ps, depth, &sk->next_sk_off))
752 return False;
753 if ( !prs_uint32( "ref_count", ps, depth, &sk->ref_count))
754 return False;
755 if ( !prs_uint32( "size", ps, depth, &sk->size))
756 return False;
759 NTSTATUS status;
760 TALLOC_CTX *mem_ctx = prs_get_mem_context(&hbin->ps);
761 DATA_BLOB blob;
763 if (MARSHALLING(&hbin->ps)) {
764 status = marshall_sec_desc(mem_ctx,
765 sk->sec_desc,
766 &blob.data, &blob.length);
767 if (!NT_STATUS_IS_OK(status))
768 return False;
769 if (!prs_copy_data_in(&hbin->ps, (const char *)blob.data, blob.length))
770 return False;
771 } else {
772 blob = data_blob_const(
773 prs_data_p(&hbin->ps) + prs_offset(&hbin->ps),
774 prs_data_size(&hbin->ps) - prs_offset(&hbin->ps)
776 status = unmarshall_sec_desc(mem_ctx,
777 blob.data, blob.length,
778 &sk->sec_desc);
779 if (!NT_STATUS_IS_OK(status))
780 return False;
781 prs_set_offset(&hbin->ps, blob.length);
785 end_off = prs_offset( &hbin->ps );
787 /* data_size must be divisible by 8 and large enough to hold the original record */
789 data_size = ((start_off - end_off) & 0xfffffff8 );
790 if ( data_size > sk->rec_size )
791 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->rec_size));
793 if ( MARSHALLING(&hbin->ps) )
794 hbin->dirty = True;
796 return True;
799 /*******************************************************************
800 *******************************************************************/
802 static bool hbin_prs_vk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_VK_REC *vk, REGF_FILE *file )
804 uint32_t offset;
805 uint16_t name_length;
806 prs_struct *ps = &hbin->ps;
807 uint32_t data_size, start_off, end_off;
809 prs_debug(ps, depth, desc, "prs_vk_rec");
810 depth++;
812 /* backup and get the data_size */
814 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32_t)) )
815 return False;
816 start_off = prs_offset( &hbin->ps );
817 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &vk->rec_size ))
818 return False;
820 if ( !prs_uint8s( True, "header", ps, depth, (uint8_t *)vk->header, sizeof( vk->header )) )
821 return False;
823 if ( MARSHALLING(&hbin->ps) )
824 name_length = strlen(vk->valuename);
826 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
827 return False;
828 if ( !prs_uint32( "data_size", ps, depth, &vk->data_size ))
829 return False;
830 if ( !prs_uint32( "data_off", ps, depth, &vk->data_off ))
831 return False;
832 if ( !prs_uint32( "type", ps, depth, &vk->type))
833 return False;
834 if ( !prs_uint16( "flag", ps, depth, &vk->flag))
835 return False;
837 offset = prs_offset( ps );
838 offset += 2; /* skip 2 bytes */
839 prs_set_offset( ps, offset );
841 /* get the name */
843 if ( vk->flag&VK_FLAG_NAME_PRESENT ) {
845 if ( UNMARSHALLING(&hbin->ps) ) {
846 if ( !(vk->valuename = PRS_ALLOC_MEM( ps, char, name_length+1 )))
847 return False;
849 if ( !prs_uint8s( True, "name", ps, depth, (uint8_t *)vk->valuename, name_length ) )
850 return False;
853 end_off = prs_offset( &hbin->ps );
855 /* get the data if necessary */
857 if ( vk->data_size != 0 ) {
858 bool charmode = False;
860 if ( (vk->type == REG_SZ) || (vk->type == REG_MULTI_SZ) )
861 charmode = True;
863 /* the data is stored in the offset if the size <= 4 */
865 if ( !(vk->data_size & VK_DATA_IN_OFFSET) ) {
866 REGF_HBIN *hblock = hbin;
867 uint32_t data_rec_size;
869 if ( UNMARSHALLING(&hbin->ps) ) {
870 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8_t, vk->data_size) ) )
871 return False;
874 /* this data can be in another hbin */
875 if ( !hbin_contains_offset( hbin, vk->data_off ) ) {
876 if ( !(hblock = lookup_hbin_block( file, vk->data_off )) )
877 return False;
879 if ( !(prs_set_offset( &hblock->ps, (vk->data_off+HBIN_HDR_SIZE-hblock->first_hbin_off)-sizeof(uint32_t) )) )
880 return False;
882 if ( MARSHALLING(&hblock->ps) ) {
883 data_rec_size = ( (vk->data_size+sizeof(uint32_t)) & 0xfffffff8 ) + 8;
884 data_rec_size = ( data_rec_size - 1 ) ^ 0xFFFFFFFF;
886 if ( !prs_uint32( "data_rec_size", &hblock->ps, depth, &data_rec_size ))
887 return False;
888 if ( !prs_uint8s( charmode, "data", &hblock->ps, depth, vk->data, vk->data_size) )
889 return False;
891 if ( MARSHALLING(&hblock->ps) )
892 hblock->dirty = True;
894 else {
895 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8_t, 4 ) ) )
896 return False;
897 SIVAL( vk->data, 0, vk->data_off );
902 /* data_size must be divisible by 8 and large enough to hold the original record */
904 data_size = ((start_off - end_off ) & 0xfffffff8 );
905 if ( data_size != vk->rec_size )
906 DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size, vk->rec_size));
908 if ( MARSHALLING(&hbin->ps) )
909 hbin->dirty = True;
911 return True;
914 /*******************************************************************
915 read a VK record which is contained in the HBIN block stored
916 in the prs_struct *ps.
917 *******************************************************************/
919 static bool hbin_prs_vk_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk, REGF_FILE *file )
921 int i;
922 uint32_t record_size;
924 prs_debug(&hbin->ps, depth, desc, "prs_vk_records");
925 depth++;
927 /* check if we have anything to do first */
929 if ( nk->num_values == 0 )
930 return True;
932 if ( UNMARSHALLING(&hbin->ps) ) {
933 if ( !(nk->values = PRS_ALLOC_MEM( &hbin->ps, REGF_VK_REC, nk->num_values ) ) )
934 return False;
937 /* convert the offset to something relative to this HBIN block */
939 if ( !prs_set_offset( &hbin->ps, nk->values_off+HBIN_HDR_SIZE-hbin->first_hbin_off-sizeof(uint32_t)) )
940 return False;
942 if ( MARSHALLING( &hbin->ps) ) {
943 record_size = ( ( nk->num_values * sizeof(uint32_t) ) & 0xfffffff8 ) + 8;
944 record_size = (record_size - 1) ^ 0xFFFFFFFF;
947 if ( !prs_uint32( "record_size", &hbin->ps, depth, &record_size ) )
948 return False;
950 for ( i=0; i<nk->num_values; i++ ) {
951 if ( !prs_uint32( "vk_off", &hbin->ps, depth, &nk->values[i].rec_off ) )
952 return False;
955 for ( i=0; i<nk->num_values; i++ ) {
956 REGF_HBIN *sub_hbin = hbin;
957 uint32_t new_offset;
959 if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) ) {
960 sub_hbin = lookup_hbin_block( file, nk->values[i].rec_off );
961 if ( !sub_hbin ) {
962 DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n",
963 nk->values[i].hbin_off));
964 return False;
968 new_offset = nk->values[i].rec_off + HBIN_HDR_SIZE - sub_hbin->first_hbin_off;
969 if ( !prs_set_offset( &sub_hbin->ps, new_offset ) )
970 return False;
971 if ( !hbin_prs_vk_rec( "vk_rec", sub_hbin, depth, &nk->values[i], file ) )
972 return False;
975 if ( MARSHALLING(&hbin->ps) )
976 hbin->dirty = True;
979 return True;
983 /*******************************************************************
984 *******************************************************************/
986 static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32_t offset )
988 REGF_SK_REC *p_sk;
990 for ( p_sk=file->sec_desc_list; p_sk; p_sk=p_sk->next ) {
991 if ( p_sk->sk_off == offset )
992 return p_sk;
995 return NULL;
998 /*******************************************************************
999 *******************************************************************/
1001 static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, struct security_descriptor *sd )
1003 REGF_SK_REC *p;
1005 for ( p=file->sec_desc_list; p; p=p->next ) {
1006 if ( security_descriptor_equal( p->sec_desc, sd ) )
1007 return p;
1010 /* failure */
1012 return NULL;
1015 /*******************************************************************
1016 *******************************************************************/
1018 static bool hbin_prs_key( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk )
1020 int depth = 0;
1021 REGF_HBIN *sub_hbin;
1023 prs_debug(&hbin->ps, depth, "", "prs_key");
1024 depth++;
1026 /* get the initial nk record */
1028 if ( !prs_nk_rec( "nk_rec", &hbin->ps, depth, nk ))
1029 return False;
1031 /* fill in values */
1033 if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) {
1034 sub_hbin = hbin;
1035 if ( !hbin_contains_offset( hbin, nk->values_off ) ) {
1036 sub_hbin = lookup_hbin_block( file, nk->values_off );
1037 if ( !sub_hbin ) {
1038 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n",
1039 nk->values_off));
1040 return False;
1044 if ( !hbin_prs_vk_records( "vk_rec", sub_hbin, depth, nk, file ))
1045 return False;
1048 /* now get subkeys */
1050 if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) {
1051 sub_hbin = hbin;
1052 if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) {
1053 sub_hbin = lookup_hbin_block( file, nk->subkeys_off );
1054 if ( !sub_hbin ) {
1055 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n",
1056 nk->subkeys_off));
1057 return False;
1061 if ( !hbin_prs_lf_records( "lf_rec", sub_hbin, depth, nk ))
1062 return False;
1065 /* get the to the security descriptor. First look if we have already parsed it */
1067 if ( (nk->sk_off!=REGF_OFFSET_NONE) && !( nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )) ) {
1069 sub_hbin = hbin;
1070 if ( !hbin_contains_offset( hbin, nk->sk_off ) ) {
1071 sub_hbin = lookup_hbin_block( file, nk->sk_off );
1072 if ( !sub_hbin ) {
1073 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_off [0x%x]\n",
1074 nk->sk_off));
1075 return False;
1079 if ( !(nk->sec_desc = talloc_zero( file->mem_ctx, REGF_SK_REC )) )
1080 return False;
1081 nk->sec_desc->sk_off = nk->sk_off;
1082 if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin, depth, nk->sec_desc ))
1083 return False;
1085 /* add to the list of security descriptors (ref_count has been read from the files) */
1087 nk->sec_desc->sk_off = nk->sk_off;
1088 DLIST_ADD( file->sec_desc_list, nk->sec_desc );
1091 return True;
1094 /*******************************************************************
1095 *******************************************************************/
1097 static bool next_record( REGF_HBIN *hbin, const char *hdr, bool *eob )
1099 uint8_t header[REC_HDR_SIZE];
1100 uint32_t record_size;
1101 uint32_t curr_off, block_size;
1102 bool found = False;
1103 prs_struct *ps = &hbin->ps;
1105 curr_off = prs_offset( ps );
1106 if ( curr_off == 0 )
1107 prs_set_offset( ps, HBIN_HEADER_REC_SIZE );
1109 /* assume that the current offset is at the record header
1110 and we need to backup to read the record size */
1112 curr_off -= sizeof(uint32_t);
1114 block_size = prs_data_size( ps );
1115 record_size = 0;
1116 memset( header, 0x0, sizeof(uint8_t)*REC_HDR_SIZE );
1117 while ( !found ) {
1119 curr_off = curr_off+record_size;
1120 if ( curr_off >= block_size )
1121 break;
1123 if ( !prs_set_offset( &hbin->ps, curr_off) )
1124 return False;
1126 if ( !prs_uint32( "record_size", ps, 0, &record_size ) )
1127 return False;
1128 if ( !prs_uint8s( True, "header", ps, 0, header, REC_HDR_SIZE ) )
1129 return False;
1131 if (record_size & 0x80000000) {
1132 /* absolute_value(record_size) */
1133 record_size = (record_size ^ 0xffffffff) + 1;
1136 if (record_size < sizeof(REC_HDR_SIZE)) {
1137 return false;
1140 if (memcmp(header, hdr, REC_HDR_SIZE) == 0) {
1141 found = True;
1142 curr_off += sizeof(uint32_t);
1146 /* mark prs_struct as done ( at end ) if no more SK records */
1147 /* mark end-of-block as True */
1149 if ( !found ) {
1150 prs_set_offset( &hbin->ps, prs_data_size(&hbin->ps) );
1151 *eob = True;
1152 return False;
1155 if ( !prs_set_offset( ps, curr_off ) )
1156 return False;
1158 return True;
1161 /*******************************************************************
1162 *******************************************************************/
1164 static bool next_nk_record( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk, bool *eob )
1166 if ( next_record( hbin, "nk", eob ) && hbin_prs_key( file, hbin, nk ) )
1167 return True;
1169 return False;
1172 /*******************************************************************
1173 Intialize the newly created REGF_BLOCK in *file and write the
1174 block header to disk
1175 *******************************************************************/
1177 static bool init_regf_block( REGF_FILE *file )
1179 prs_struct ps;
1180 bool result = True;
1182 if ( !prs_init( &ps, REGF_BLOCKSIZE, file->mem_ctx, MARSHALL ) )
1183 return False;
1185 memcpy( file->header, "regf", REGF_HDR_SIZE );
1186 file->data_offset = 0x20;
1187 file->last_block = 0x1000;
1189 /* set mod time */
1191 unix_to_nt_time( &file->mtime, time(NULL) );
1193 /* hard coded values...no diea what these are ... maybe in time */
1195 file->unknown1 = 0x2;
1196 file->unknown2 = 0x1;
1197 file->unknown3 = 0x3;
1198 file->unknown4 = 0x0;
1199 file->unknown5 = 0x1;
1200 file->unknown6 = 0x1;
1202 /* write header to the buffer */
1204 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1205 result = False;
1206 goto out;
1209 /* calculate the checksum, re-marshall data (to include the checksum)
1210 and write to disk */
1212 file->checksum = regf_block_checksum( &ps );
1213 prs_set_offset( &ps, 0 );
1214 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1215 result = False;
1216 goto out;
1219 if ( write_block( file, &ps, 0 ) == -1 ) {
1220 DEBUG(0,("init_regf_block: Failed to initialize registry header block!\n"));
1221 result = False;
1222 goto out;
1225 out:
1226 prs_mem_free( &ps );
1228 return result;
1230 /*******************************************************************
1231 Open the registry file and then read in the REGF block to get the
1232 first hbin offset.
1233 *******************************************************************/
1235 REGF_FILE* regfio_open( const char *filename, int flags, int mode )
1237 REGF_FILE *rb;
1239 if ( !(rb = SMB_MALLOC_P(REGF_FILE)) ) {
1240 DEBUG(0,("ERROR allocating memory\n"));
1241 return NULL;
1243 ZERO_STRUCTP( rb );
1244 rb->fd = -1;
1246 if ( !(rb->mem_ctx = talloc_init( "regfio_open" )) ) {
1247 regfio_close( rb );
1248 return NULL;
1251 rb->open_flags = flags;
1253 /* open and existing file */
1255 if ( (rb->fd = open(filename, flags, mode)) == -1 ) {
1256 DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename, strerror(errno)));
1257 regfio_close( rb );
1258 return NULL;
1261 /* check if we are creating a new file or overwriting an existing one */
1263 if ( flags & (O_CREAT|O_TRUNC) ) {
1264 if ( !init_regf_block( rb ) ) {
1265 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1266 regfio_close( rb );
1267 return NULL;
1270 /* success */
1271 return rb;
1274 /* read in an existing file */
1276 if ( !read_regf_block( rb ) ) {
1277 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1278 regfio_close( rb );
1279 return NULL;
1282 /* success */
1284 return rb;
1287 /*******************************************************************
1288 *******************************************************************/
1290 static void regfio_mem_free( REGF_FILE *file )
1292 /* free any talloc()'d memory */
1294 if ( file && file->mem_ctx )
1295 talloc_destroy( file->mem_ctx );
1298 /*******************************************************************
1299 *******************************************************************/
1301 int regfio_close( REGF_FILE *file )
1303 int fd;
1305 /* cleanup for a file opened for write */
1307 if ((file->fd != -1) && (file->open_flags & (O_WRONLY|O_RDWR))) {
1308 prs_struct ps;
1309 REGF_SK_REC *sk;
1311 /* write of sd list */
1313 for ( sk=file->sec_desc_list; sk; sk=sk->next ) {
1314 hbin_prs_sk_rec( "sk_rec", sk->hbin, 0, sk );
1317 /* flush any dirty blocks */
1319 while ( file->block_list ) {
1320 hbin_block_close( file, file->block_list );
1323 ZERO_STRUCT( ps );
1325 unix_to_nt_time( &file->mtime, time(NULL) );
1327 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) != -1 ) {
1328 /* now use for writing */
1329 prs_switch_type( &ps, MARSHALL );
1331 /* stream the block once, generate the checksum,
1332 and stream it again */
1333 prs_set_offset( &ps, 0 );
1334 prs_regf_block( "regf_blocK", &ps, 0, file );
1335 file->checksum = regf_block_checksum( &ps );
1336 prs_set_offset( &ps, 0 );
1337 prs_regf_block( "regf_blocK", &ps, 0, file );
1339 /* now we are ready to write it to disk */
1340 if ( write_block( file, &ps, 0 ) == -1 )
1341 DEBUG(0,("regfio_close: failed to update the regf header block!\n"));
1344 prs_mem_free( &ps );
1347 regfio_mem_free( file );
1349 /* nothing tdo do if there is no open file */
1351 if (file->fd == -1)
1352 return 0;
1354 fd = file->fd;
1355 file->fd = -1;
1356 SAFE_FREE( file );
1358 return close( fd );
1361 /*******************************************************************
1362 *******************************************************************/
1364 static void regfio_flush( REGF_FILE *file )
1366 REGF_HBIN *hbin;
1368 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
1369 write_hbin_block( file, hbin );
1373 /*******************************************************************
1374 There should be only *one* root key in the registry file based
1375 on my experience. --jerry
1376 *******************************************************************/
1378 REGF_NK_REC* regfio_rootkey( REGF_FILE *file )
1380 REGF_NK_REC *nk;
1381 REGF_HBIN *hbin;
1382 uint32_t offset = REGF_BLOCKSIZE;
1383 bool found = False;
1384 bool eob;
1386 if ( !file )
1387 return NULL;
1389 if ( !(nk = talloc_zero( file->mem_ctx, REGF_NK_REC )) ) {
1390 DEBUG(0,("regfio_rootkey: talloc() failed!\n"));
1391 return NULL;
1394 /* scan through the file on HBIN block at a time looking
1395 for an NK record with a type == 0x002c.
1396 Normally this is the first nk record in the first hbin
1397 block (but I'm not assuming that for now) */
1399 while ( (hbin = read_hbin_block( file, offset )) ) {
1400 eob = False;
1402 while ( !eob) {
1403 if ( next_nk_record( file, hbin, nk, &eob ) ) {
1404 if ( nk->key_type == NK_TYPE_ROOTKEY ) {
1405 found = True;
1406 break;
1409 prs_mem_free( &hbin->ps );
1412 if ( found )
1413 break;
1415 offset += hbin->block_size;
1418 if ( !found ) {
1419 DEBUG(0,("regfio_rootkey: corrupt registry file ? No root key record located\n"));
1420 return NULL;
1423 DLIST_ADD( file->block_list, hbin );
1425 return nk;
1428 /*******************************************************************
1429 This acts as an interator over the subkeys defined for a given
1430 NK record. Remember that offsets are from the *first* HBIN block.
1431 *******************************************************************/
1433 REGF_NK_REC* regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk )
1435 REGF_NK_REC *subkey;
1436 REGF_HBIN *hbin;
1437 uint32_t nk_offset;
1439 /* see if there is anything left to report */
1441 if (nk == NULL ||
1442 nk->subkeys.hashes == NULL ||
1443 nk->subkey_index >= nk->subkeys.num_keys ||
1444 (nk->subkeys_off == REGF_OFFSET_NONE) ||
1445 (nk->subkey_index >= nk->num_subkeys)) {
1446 return NULL;
1449 /* find the HBIN block which should contain the nk record */
1451 hbin = lookup_hbin_block(file,
1452 nk->subkeys.hashes[nk->subkey_index].nk_off);
1453 if (hbin == NULL) {
1454 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n",
1455 nk->subkeys.hashes[nk->subkey_index].nk_off));
1456 return NULL;
1459 nk_offset = nk->subkeys.hashes[nk->subkey_index].nk_off;
1460 if ( !prs_set_offset( &hbin->ps, (HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off) ) )
1461 return NULL;
1463 nk->subkey_index++;
1464 if ( !(subkey = talloc_zero( file->mem_ctx, REGF_NK_REC )) )
1465 return NULL;
1467 if ( !hbin_prs_key( file, hbin, subkey ) )
1468 return NULL;
1470 return subkey;
1474 /*******************************************************************
1475 *******************************************************************/
1477 static REGF_HBIN* regf_hbin_allocate( REGF_FILE *file, uint32_t block_size )
1479 REGF_HBIN *hbin;
1480 SMB_STRUCT_STAT sbuf;
1482 if ( !(hbin = talloc_zero( file->mem_ctx, REGF_HBIN )) )
1483 return NULL;
1485 memcpy( hbin->header, "hbin", HBIN_HDR_SIZE);
1488 if (sys_fstat(file->fd, &sbuf, false)) {
1489 DEBUG(0,("regf_hbin_allocate: stat() failed! (%s)\n", strerror(errno)));
1490 return NULL;
1493 hbin->file_off = sbuf.st_ex_size;
1495 hbin->free_off = HBIN_HEADER_REC_SIZE;
1496 hbin->free_size = block_size - hbin->free_off + sizeof(uint32_t);
1498 hbin->block_size = block_size;
1499 hbin->first_hbin_off = hbin->file_off - REGF_BLOCKSIZE;
1501 if ( !prs_init( &hbin->ps, block_size, file->mem_ctx, MARSHALL ) )
1502 return NULL;
1504 if ( !prs_hbin_block( "new_hbin", &hbin->ps, 0, hbin ) )
1505 return NULL;
1507 if ( !write_hbin_block( file, hbin ) )
1508 return NULL;
1510 file->last_block = hbin->file_off;
1512 return hbin;
1515 /*******************************************************************
1516 *******************************************************************/
1518 static void update_free_space( REGF_HBIN *hbin, uint32_t size_used )
1520 hbin->free_off += size_used;
1521 hbin->free_size -= size_used;
1523 if ( hbin->free_off >= hbin->block_size ) {
1524 hbin->free_off = REGF_OFFSET_NONE;
1527 return;
1530 /*******************************************************************
1531 *******************************************************************/
1533 static REGF_HBIN* find_free_space( REGF_FILE *file, uint32_t size )
1535 REGF_HBIN *hbin, *p_hbin;
1536 uint32_t block_off;
1537 bool cached;
1539 /* check open block list */
1541 for ( hbin=file->block_list; hbin!=NULL; hbin=hbin->next ) {
1542 /* only check blocks that actually have available space */
1544 if ( hbin->free_off == REGF_OFFSET_NONE )
1545 continue;
1547 /* check for a large enough available chunk */
1549 if ( (hbin->block_size - hbin->free_off) >= size ) {
1550 DLIST_PROMOTE( file->block_list, hbin );
1551 goto done;
1555 /* parse the file until we find a block with
1556 enough free space; save the last non-filled hbin */
1558 block_off = REGF_BLOCKSIZE;
1559 do {
1560 /* cleanup before the next round */
1561 cached = False;
1562 if ( hbin )
1563 prs_mem_free( &hbin->ps );
1565 hbin = read_hbin_block( file, block_off );
1567 if ( hbin ) {
1569 /* make sure that we don't already have this block in memory */
1571 for ( p_hbin=file->block_list; p_hbin!=NULL; p_hbin=p_hbin->next ) {
1572 if ( p_hbin->file_off == hbin->file_off ) {
1573 cached = True;
1574 break;
1578 block_off = hbin->file_off + hbin->block_size;
1580 if ( cached ) {
1581 prs_mem_free( &hbin->ps );
1582 hbin = NULL;
1583 continue;
1586 /* if (cached block or (new block and not enough free space)) then continue looping */
1587 } while ( cached || (hbin && (hbin->free_size < size)) );
1589 /* no free space; allocate a new one */
1591 if ( !hbin ) {
1592 uint32_t alloc_size;
1594 /* allocate in multiples of REGF_ALLOC_BLOCK; make sure (size + hbin_header) fits */
1596 alloc_size = (((size+HBIN_HEADER_REC_SIZE) / REGF_ALLOC_BLOCK ) + 1 ) * REGF_ALLOC_BLOCK;
1598 if ( !(hbin = regf_hbin_allocate( file, alloc_size )) ) {
1599 DEBUG(0,("find_free_space: regf_hbin_allocate() failed!\n"));
1600 return NULL;
1602 DLIST_ADD( file->block_list, hbin );
1605 done:
1606 /* set the offset to be ready to write */
1608 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32_t) ) )
1609 return NULL;
1611 /* write the record size as a placeholder for now, it should be
1612 probably updated by the caller once it all of the data necessary
1613 for the record */
1615 if ( !prs_uint32("allocated_size", &hbin->ps, 0, &size) )
1616 return NULL;
1618 update_free_space( hbin, size );
1620 return hbin;
1623 /*******************************************************************
1624 *******************************************************************/
1626 static uint32_t sk_record_data_size( struct security_descriptor * sd )
1628 uint32_t size, size_mod8;
1630 size_mod8 = 0;
1632 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1634 size = sizeof(uint32_t)*5 + ndr_size_security_descriptor(sd, 0) + sizeof(uint32_t);
1636 /* multiple of 8 */
1637 size_mod8 = size & 0xfffffff8;
1638 if ( size_mod8 < size )
1639 size_mod8 += 8;
1641 return size_mod8;
1644 /*******************************************************************
1645 *******************************************************************/
1647 static uint32_t vk_record_data_size( REGF_VK_REC *vk )
1649 uint32_t size, size_mod8;
1651 size_mod8 = 0;
1653 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1655 size = REC_HDR_SIZE + (sizeof(uint16_t)*3) + (sizeof(uint32_t)*3) + sizeof(uint32_t);
1657 if ( vk->valuename )
1658 size += strlen(vk->valuename);
1660 /* multiple of 8 */
1661 size_mod8 = size & 0xfffffff8;
1662 if ( size_mod8 < size )
1663 size_mod8 += 8;
1665 return size_mod8;
1668 /*******************************************************************
1669 *******************************************************************/
1671 static uint32_t lf_record_data_size( uint32_t num_keys )
1673 uint32_t size, size_mod8;
1675 size_mod8 = 0;
1677 /* the record size is sizeof(hdr) + num_keys + sizeof of hash_array + data_size_uint32_t */
1679 size = REC_HDR_SIZE + sizeof(uint16_t) + (sizeof(REGF_HASH_REC) * num_keys) + sizeof(uint32_t);
1681 /* multiple of 8 */
1682 size_mod8 = size & 0xfffffff8;
1683 if ( size_mod8 < size )
1684 size_mod8 += 8;
1686 return size_mod8;
1689 /*******************************************************************
1690 *******************************************************************/
1692 static uint32_t nk_record_data_size( REGF_NK_REC *nk )
1694 uint32_t size, size_mod8;
1696 size_mod8 = 0;
1698 /* the record size is static + length_of_keyname + length_of_classname + data_size_uint32_t */
1700 size = 0x4c + strlen(nk->keyname) + sizeof(uint32_t);
1702 if ( nk->classname )
1703 size += strlen( nk->classname );
1705 /* multiple of 8 */
1706 size_mod8 = size & 0xfffffff8;
1707 if ( size_mod8 < size )
1708 size_mod8 += 8;
1710 return size_mod8;
1713 /*******************************************************************
1714 *******************************************************************/
1716 static bool create_vk_record(REGF_FILE *file, REGF_VK_REC *vk,
1717 struct regval_blob *value)
1719 char *name = regval_name(value);
1720 REGF_HBIN *data_hbin;
1722 ZERO_STRUCTP( vk );
1724 memcpy( vk->header, "vk", REC_HDR_SIZE );
1726 if ( name ) {
1727 vk->valuename = talloc_strdup( file->mem_ctx, regval_name(value) );
1728 vk->flag = VK_FLAG_NAME_PRESENT;
1731 vk->data_size = regval_size( value );
1732 vk->type = regval_type( value );
1734 if ( vk->data_size > sizeof(uint32_t) ) {
1735 uint32_t data_size = ( (vk->data_size+sizeof(uint32_t)) & 0xfffffff8 ) + 8;
1737 vk->data = (uint8_t *)talloc_memdup( file->mem_ctx,
1738 regval_data_p(value),
1739 vk->data_size );
1740 if (vk->data == NULL) {
1741 return False;
1744 /* go ahead and store the offset....we'll pick this hbin block back up when
1745 we stream the data */
1747 if ((data_hbin = find_free_space(file, data_size )) == NULL) {
1748 return False;
1750 vk->data_off = prs_offset( &data_hbin->ps ) + data_hbin->first_hbin_off - HBIN_HDR_SIZE;
1752 else {
1753 /* make sure we don't try to copy from a NULL value pointer */
1755 if ( vk->data_size != 0 )
1756 memcpy( &vk->data_off, regval_data_p(value), vk->data_size);
1757 vk->data_size |= VK_DATA_IN_OFFSET;
1760 return True;
1763 /*******************************************************************
1764 *******************************************************************/
1766 static int hashrec_cmp( REGF_HASH_REC *h1, REGF_HASH_REC *h2 )
1768 return strcasecmp_m( h1->fullname, h2->fullname );
1771 /*******************************************************************
1772 *******************************************************************/
1774 REGF_NK_REC* regfio_write_key( REGF_FILE *file, const char *name,
1775 struct regval_ctr *values, struct regsubkey_ctr *subkeys,
1776 struct security_descriptor *sec_desc, REGF_NK_REC *parent )
1778 REGF_NK_REC *nk;
1779 REGF_HBIN *vlist_hbin = NULL;
1780 uint32_t size;
1782 if ( !(nk = talloc_zero( file->mem_ctx, REGF_NK_REC )) )
1783 return NULL;
1785 memcpy( nk->header, "nk", REC_HDR_SIZE );
1787 if ( !parent )
1788 nk->key_type = NK_TYPE_ROOTKEY;
1789 else
1790 nk->key_type = NK_TYPE_NORMALKEY;
1792 /* store the parent offset (or -1 if a the root key */
1794 nk->parent_off = parent ? (parent->hbin_off + parent->hbin->file_off - REGF_BLOCKSIZE - HBIN_HDR_SIZE ) : REGF_OFFSET_NONE;
1796 /* no classname currently */
1798 nk->classname_off = REGF_OFFSET_NONE;
1799 nk->classname = NULL;
1800 nk->keyname = talloc_strdup( file->mem_ctx, name );
1802 /* current modification time */
1804 unix_to_nt_time( &nk->mtime, time(NULL) );
1806 /* allocate the record on disk */
1808 size = nk_record_data_size( nk );
1809 nk->rec_size = ( size - 1 ) ^ 0XFFFFFFFF;
1810 if ((nk->hbin = find_free_space( file, size )) == NULL) {
1811 return NULL;
1813 nk->hbin_off = prs_offset( &nk->hbin->ps );
1815 /* Update the hash record in the parent */
1817 if ( parent ) {
1818 REGF_HASH_REC *hash = &parent->subkeys.hashes[parent->subkey_index];
1820 hash->nk_off = prs_offset( &nk->hbin->ps ) + nk->hbin->first_hbin_off - HBIN_HDR_SIZE;
1821 memcpy(hash->keycheck, name, MIN(strlen(name),sizeof(uint32_t)));
1822 hash->fullname = talloc_strdup( file->mem_ctx, name );
1823 parent->subkey_index++;
1825 /* sort the list by keyname */
1826 TYPESAFE_QSORT(parent->subkeys.hashes, parent->subkey_index, hashrec_cmp);
1828 if ( !hbin_prs_lf_records( "lf_rec", parent->subkeys.hbin, 0, parent ) )
1829 return NULL;
1832 /* write the security descriptor */
1834 nk->sk_off = REGF_OFFSET_NONE;
1835 if ( sec_desc ) {
1836 uint32_t sk_size = sk_record_data_size( sec_desc );
1837 REGF_HBIN *sk_hbin;
1839 /* search for it in the existing list of sd's */
1841 if ( (nk->sec_desc = find_sk_record_by_sec_desc( file, sec_desc )) == NULL ) {
1842 /* not found so add it to the list */
1844 if (!(sk_hbin = find_free_space( file, sk_size ))) {
1845 return NULL;
1848 if ( !(nk->sec_desc = talloc_zero( file->mem_ctx, REGF_SK_REC )) )
1849 return NULL;
1851 /* now we have to store the security descriptor in the list and
1852 update the offsets */
1854 memcpy( nk->sec_desc->header, "sk", REC_HDR_SIZE );
1855 nk->sec_desc->hbin = sk_hbin;
1856 nk->sec_desc->hbin_off = prs_offset( &sk_hbin->ps );
1857 nk->sec_desc->sk_off = prs_offset( &sk_hbin->ps ) + sk_hbin->first_hbin_off - HBIN_HDR_SIZE;
1858 nk->sec_desc->rec_size = (sk_size-1) ^ 0xFFFFFFFF;
1860 nk->sec_desc->sec_desc = sec_desc;
1861 nk->sec_desc->ref_count = 0;
1863 /* size value must be self-inclusive */
1864 nk->sec_desc->size = ndr_size_security_descriptor(sec_desc, 0)
1865 + sizeof(uint32_t);
1867 DLIST_ADD_END( file->sec_desc_list, nk->sec_desc);
1869 /* update the offsets for us and the previous sd in the list.
1870 if this is the first record, then just set the next and prev
1871 offsets to ourself. */
1873 if ( DLIST_PREV(nk->sec_desc) ) {
1874 REGF_SK_REC *prev = DLIST_PREV(nk->sec_desc);
1876 nk->sec_desc->prev_sk_off = prev->hbin_off + prev->hbin->first_hbin_off - HBIN_HDR_SIZE;
1877 prev->next_sk_off = nk->sec_desc->sk_off;
1879 /* the end must loop around to the front */
1880 nk->sec_desc->next_sk_off = file->sec_desc_list->sk_off;
1882 /* and first must loop around to the tail */
1883 file->sec_desc_list->prev_sk_off = nk->sec_desc->sk_off;
1884 } else {
1885 nk->sec_desc->prev_sk_off = nk->sec_desc->sk_off;
1886 nk->sec_desc->next_sk_off = nk->sec_desc->sk_off;
1890 /* bump the reference count +1 */
1892 nk->sk_off = nk->sec_desc->sk_off;
1893 nk->sec_desc->ref_count++;
1896 /* write the subkeys */
1898 nk->subkeys_off = REGF_OFFSET_NONE;
1899 if ( (nk->num_subkeys = regsubkey_ctr_numkeys( subkeys )) != 0 ) {
1900 uint32_t lf_size = lf_record_data_size( nk->num_subkeys );
1901 uint32_t namelen;
1902 int i;
1904 if (!(nk->subkeys.hbin = find_free_space( file, lf_size ))) {
1905 return NULL;
1907 nk->subkeys.hbin_off = prs_offset( &nk->subkeys.hbin->ps );
1908 nk->subkeys.rec_size = (lf_size-1) ^ 0xFFFFFFFF;
1909 nk->subkeys_off = prs_offset( &nk->subkeys.hbin->ps ) + nk->subkeys.hbin->first_hbin_off - HBIN_HDR_SIZE;
1911 memcpy( nk->subkeys.header, "lf", REC_HDR_SIZE );
1913 nk->subkeys.num_keys = nk->num_subkeys;
1914 if (nk->subkeys.num_keys) {
1915 if ( !(nk->subkeys.hashes = talloc_zero_array( file->mem_ctx, REGF_HASH_REC, nk->subkeys.num_keys )) )
1916 return NULL;
1917 } else {
1918 nk->subkeys.hashes = NULL;
1920 nk->subkey_index = 0;
1922 /* update the max_bytes_subkey{name,classname} fields */
1923 for ( i=0; i<nk->num_subkeys; i++ ) {
1924 namelen = strlen( regsubkey_ctr_specific_key(subkeys, i) );
1925 if ( namelen*2 > nk->max_bytes_subkeyname )
1926 nk->max_bytes_subkeyname = namelen * 2;
1930 /* write the values */
1932 nk->values_off = REGF_OFFSET_NONE;
1933 if ( (nk->num_values = regval_ctr_numvals( values )) != 0 ) {
1934 uint32_t vlist_size = ( ( nk->num_values * sizeof(uint32_t) ) & 0xfffffff8 ) + 8;
1935 int i;
1937 if (!(vlist_hbin = find_free_space( file, vlist_size ))) {
1938 return NULL;
1940 nk->values_off = prs_offset( &vlist_hbin->ps ) + vlist_hbin->first_hbin_off - HBIN_HDR_SIZE;
1942 if (nk->num_values) {
1943 if ( !(nk->values = talloc_array( file->mem_ctx, REGF_VK_REC, nk->num_values )) )
1944 return NULL;
1945 } else {
1946 nk->values = NULL;
1949 /* create the vk records */
1951 for ( i=0; i<nk->num_values; i++ ) {
1952 uint32_t vk_size, namelen, datalen;
1953 struct regval_blob *r;
1955 r = regval_ctr_specific_value( values, i );
1956 create_vk_record( file, &nk->values[i], r );
1957 vk_size = vk_record_data_size( &nk->values[i] );
1958 nk->values[i].hbin = find_free_space( file, vk_size );
1959 nk->values[i].hbin_off = prs_offset( &nk->values[i].hbin->ps );
1960 nk->values[i].rec_size = ( vk_size - 1 ) ^ 0xFFFFFFFF;
1961 nk->values[i].rec_off = prs_offset( &nk->values[i].hbin->ps )
1962 + nk->values[i].hbin->first_hbin_off
1963 - HBIN_HDR_SIZE;
1965 /* update the max bytes fields if necessary */
1967 namelen = strlen( regval_name(r) );
1968 if ( namelen*2 > nk->max_bytes_valuename )
1969 nk->max_bytes_valuename = namelen * 2;
1971 datalen = regval_size( r );
1972 if ( datalen > nk->max_bytes_value )
1973 nk->max_bytes_value = datalen;
1977 /* stream the records */
1979 prs_set_offset( &nk->hbin->ps, nk->hbin_off );
1980 if ( !prs_nk_rec( "nk_rec", &nk->hbin->ps, 0, nk ) )
1981 return NULL;
1983 if ( nk->num_values ) {
1984 if ( !hbin_prs_vk_records( "vk_records", vlist_hbin, 0, nk, file ) )
1985 return NULL;
1989 regfio_flush( file );
1991 return nk;