util:datablob: data_blob_pad checks its alignment assumption
[samba.git] / source3 / registry / regfio.c
blob2ddac2179b24cef4f68452e391f5e8a8b34bbda7
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 ssize_t 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 if (returned < 0 || bytes_read > SSIZE_MAX - returned) {
186 DBG_ERR("Integer overflow\n");
187 return false;
189 bytes_read += returned;
192 return bytes_read;
195 /*******************************************************************
196 *******************************************************************/
198 static bool write_hbin_block( REGF_FILE *file, REGF_HBIN *hbin )
200 if ( !hbin->dirty )
201 return True;
203 /* write free space record if any is available */
205 if ( hbin->free_off != REGF_OFFSET_NONE ) {
206 uint32_t header = 0xffffffff;
208 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32_t) ) )
209 return False;
210 if ( !prs_uint32( "free_size", &hbin->ps, 0, &hbin->free_size ) )
211 return False;
212 if ( !prs_uint32( "free_header", &hbin->ps, 0, &header ) )
213 return False;
216 hbin->dirty = (write_block( file, &hbin->ps, hbin->file_off ) != -1);
218 return hbin->dirty;
221 /*******************************************************************
222 *******************************************************************/
224 static bool hbin_block_close( REGF_FILE *file, REGF_HBIN *hbin )
226 REGF_HBIN *p;
228 /* remove the block from the open list and flush it to disk */
230 for ( p=file->block_list; p && p!=hbin; p=p->next )
233 if ( p == hbin ) {
234 DLIST_REMOVE( file->block_list, hbin );
236 else
237 DEBUG(0,("hbin_block_close: block not in open list!\n"));
239 if ( !write_hbin_block( file, hbin ) )
240 return False;
242 return True;
245 /*******************************************************************
246 *******************************************************************/
248 static bool prs_regf_block( const char *desc, prs_struct *ps, int depth, REGF_FILE *file )
250 prs_debug(ps, depth, desc, "prs_regf_block");
251 depth++;
253 if ( !prs_uint8s( True, "header", ps, depth, (uint8_t *)file->header, sizeof( file->header )) )
254 return False;
256 /* yes, these values are always identical so store them only once */
258 if ( !prs_uint32( "unknown1", ps, depth, &file->unknown1 ))
259 return False;
260 if ( !prs_uint32( "unknown1 (again)", ps, depth, &file->unknown1 ))
261 return False;
263 /* get the modtime */
265 if ( !prs_set_offset( ps, 0x0c ) )
266 return False;
267 if ( !smb_io_time( "modtime", &file->mtime, ps, depth ) )
268 return False;
270 /* constants */
272 if ( !prs_uint32( "unknown2", ps, depth, &file->unknown2 ))
273 return False;
274 if ( !prs_uint32( "unknown3", ps, depth, &file->unknown3 ))
275 return False;
276 if ( !prs_uint32( "unknown4", ps, depth, &file->unknown4 ))
277 return False;
278 if ( !prs_uint32( "unknown5", ps, depth, &file->unknown5 ))
279 return False;
281 /* get file offsets */
283 if ( !prs_set_offset( ps, 0x24 ) )
284 return False;
285 if ( !prs_uint32( "data_offset", ps, depth, &file->data_offset ))
286 return False;
287 if ( !prs_uint32( "last_block", ps, depth, &file->last_block ))
288 return False;
290 /* one more constant */
292 if ( !prs_uint32( "unknown6", ps, depth, &file->unknown6 ))
293 return False;
295 /* get the checksum */
297 if ( !prs_set_offset( ps, 0x01fc ) )
298 return False;
299 if ( !prs_uint32( "checksum", ps, depth, &file->checksum ))
300 return False;
302 return True;
305 /*******************************************************************
306 *******************************************************************/
308 static bool prs_hbin_block( const char *desc, prs_struct *ps, int depth, REGF_HBIN *hbin )
310 uint32_t block_size2;
312 prs_debug(ps, depth, desc, "prs_hbin_block");
313 depth++;
315 if ( !prs_uint8s( True, "header", ps, depth, (uint8_t*)hbin->header, sizeof( hbin->header )) )
316 return False;
318 if ( !prs_uint32( "first_hbin_off", ps, depth, &hbin->first_hbin_off ))
319 return False;
321 /* The dosreg.cpp comments say that the block size is at 0x1c.
322 According to a WINXP NTUSER.dat file, this is wrong. The block_size
323 is at 0x08 */
325 if ( !prs_uint32( "block_size", ps, depth, &hbin->block_size ))
326 return False;
328 block_size2 = hbin->block_size;
329 prs_set_offset( ps, 0x1c );
330 if ( !prs_uint32( "block_size2", ps, depth, &block_size2 ))
331 return False;
333 if ( MARSHALLING(ps) )
334 hbin->dirty = True;
337 return True;
340 /*******************************************************************
341 *******************************************************************/
343 static bool prs_nk_rec( const char *desc, prs_struct *ps, int depth, REGF_NK_REC *nk )
345 uint16_t class_length, name_length;
346 uint32_t start;
347 uint32_t data_size, start_off, end_off;
348 uint32_t unknown_off = REGF_OFFSET_NONE;
350 nk->hbin_off = prs_offset( ps );
351 start = nk->hbin_off;
353 prs_debug(ps, depth, desc, "prs_nk_rec");
354 depth++;
356 /* back up and get the data_size */
358 if ( !prs_set_offset( ps, prs_offset(ps)-sizeof(uint32_t)) )
359 return False;
360 start_off = prs_offset( ps );
361 if ( !prs_uint32( "rec_size", ps, depth, &nk->rec_size ))
362 return False;
364 if ( !prs_uint8s( True, "header", ps, depth, (uint8_t *)nk->header, sizeof( nk->header )) )
365 return False;
367 if ( !prs_uint16( "key_type", ps, depth, &nk->key_type ))
368 return False;
369 if ( !smb_io_time( "mtime", &nk->mtime, ps, depth ))
370 return False;
372 if ( !prs_set_offset( ps, start+0x0010 ) )
373 return False;
374 if ( !prs_uint32( "parent_off", ps, depth, &nk->parent_off ))
375 return False;
376 if ( !prs_uint32( "num_subkeys", ps, depth, &nk->num_subkeys ))
377 return False;
379 if ( !prs_set_offset( ps, start+0x001c ) )
380 return False;
381 if ( !prs_uint32( "subkeys_off", ps, depth, &nk->subkeys_off ))
382 return False;
383 if ( !prs_uint32( "unknown_off", ps, depth, &unknown_off) )
384 return False;
386 if ( !prs_set_offset( ps, start+0x0024 ) )
387 return False;
388 if ( !prs_uint32( "num_values", ps, depth, &nk->num_values ))
389 return False;
390 if ( !prs_uint32( "values_off", ps, depth, &nk->values_off ))
391 return False;
392 if ( !prs_uint32( "sk_off", ps, depth, &nk->sk_off ))
393 return False;
394 if ( !prs_uint32( "classname_off", ps, depth, &nk->classname_off ))
395 return False;
397 if ( !prs_uint32( "max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname))
398 return False;
399 if ( !prs_uint32( "max_bytes_subkeyclassname", ps, depth, &nk->max_bytes_subkeyclassname))
400 return False;
401 if ( !prs_uint32( "max_bytes_valuename", ps, depth, &nk->max_bytes_valuename))
402 return False;
403 if ( !prs_uint32( "max_bytes_value", ps, depth, &nk->max_bytes_value))
404 return False;
405 if ( !prs_uint32( "unknown index", ps, depth, &nk->unk_index))
406 return False;
408 name_length = nk->keyname ? strlen(nk->keyname) : 0 ;
409 class_length = nk->classname ? strlen(nk->classname) : 0 ;
410 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
411 return False;
412 if ( !prs_uint16( "class_length", ps, depth, &class_length ))
413 return False;
415 if ( class_length ) {
419 if ( name_length ) {
420 if ( UNMARSHALLING(ps) ) {
421 if ( !(nk->keyname = PRS_ALLOC_MEM( ps, char, name_length+1 )) )
422 return False;
425 if ( !prs_uint8s( True, "name", ps, depth, (uint8_t *)nk->keyname, name_length) )
426 return False;
428 if ( UNMARSHALLING(ps) )
429 nk->keyname[name_length] = '\0';
432 end_off = prs_offset( ps );
434 /* data_size must be divisible by 8 and large enough to hold the original record */
436 data_size = ((start_off - end_off) & 0xfffffff8 );
437 if ( data_size > nk->rec_size )
438 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size));
440 if ( MARSHALLING(ps) )
441 nk->hbin->dirty = True;
443 return True;
446 /*******************************************************************
447 *******************************************************************/
449 static uint32_t regf_block_checksum( prs_struct *ps )
451 char *buffer = prs_data_p( ps );
452 uint32_t checksum, x;
453 int i;
455 /* XOR of all bytes 0x0000 - 0x01FB */
457 checksum = x = 0;
459 for ( i=0; i<0x01FB; i+=4 ) {
460 x = IVAL(buffer, i );
461 checksum ^= x;
464 return checksum;
467 /*******************************************************************
468 *******************************************************************/
470 static bool read_regf_block( REGF_FILE *file )
472 prs_struct ps;
473 uint32_t checksum;
475 /* grab the first block from the file */
477 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) == -1 )
478 return False;
480 /* parse the block and verify the checksum */
482 if ( !prs_regf_block( "regf_header", &ps, 0, file ) )
483 return False;
485 checksum = regf_block_checksum( &ps );
487 prs_mem_free( &ps );
489 if ( file->checksum != checksum && !file->ignore_checksums) {
490 DEBUG(0,("read_regf_block: invalid checksum\n" ));
491 return False;
494 return True;
497 /*******************************************************************
498 *******************************************************************/
500 static REGF_HBIN* read_hbin_block( REGF_FILE *file, off_t offset )
502 REGF_HBIN *hbin;
503 uint32_t record_size, curr_off, block_size, header;
505 if ( !(hbin = talloc_zero(file->mem_ctx, REGF_HBIN)) )
506 return NULL;
507 hbin->file_off = offset;
508 hbin->free_off = -1;
510 if ( read_block( file, &hbin->ps, offset, 0 ) == -1 )
511 return NULL;
513 if ( !prs_hbin_block( "hbin", &hbin->ps, 0, hbin ) )
514 return NULL;
516 /* this should be the same thing as hbin->block_size but just in case */
518 block_size = prs_data_size( &hbin->ps );
520 /* Find the available free space offset. Always at the end,
521 so walk the record list and stop when you get to the end.
522 The end is defined by a record header of 0xffffffff. The
523 previous 4 bytes contains the amount of free space remaining
524 in the hbin block. */
526 /* remember that the record_size is in the 4 bytes preceding the record itself */
528 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE-sizeof(uint32_t) ) )
529 return NULL;
531 record_size = 0;
532 header = 0;
533 curr_off = prs_offset( &hbin->ps );
534 while ( header != 0xffffffff ) {
535 /* not done yet so reset the current offset to the
536 next record_size field */
538 curr_off = curr_off+record_size;
540 /* for some reason the record_size of the last record in
541 an hbin block can extend past the end of the block
542 even though the record fits within the remaining
543 space....aaarrrgggghhhhhh */
545 if ( curr_off >= block_size ) {
546 record_size = -1;
547 curr_off = -1;
548 break;
551 if ( !prs_set_offset( &hbin->ps, curr_off) )
552 return NULL;
554 if ( !prs_uint32( "rec_size", &hbin->ps, 0, &record_size ) )
555 return NULL;
556 if ( !prs_uint32( "header", &hbin->ps, 0, &header ) )
557 return NULL;
559 if (record_size == 0)
560 return NULL;
562 if ( record_size & 0x80000000 ) {
563 /* absolute_value(record_size) */
564 record_size = (record_size ^ 0xffffffff) + 1;
568 /* save the free space offset */
570 if ( header == 0xffffffff ) {
572 /* account for the fact that the curr_off is 4 bytes behind the actual
573 record header */
575 hbin->free_off = curr_off + sizeof(uint32_t);
576 hbin->free_size = record_size;
579 DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin->free_off));
581 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE ) )
582 return NULL;
584 return hbin;
587 /*******************************************************************
588 Input a random offset and receive the corresponding HBIN
589 block for it
590 *******************************************************************/
592 static bool hbin_contains_offset( REGF_HBIN *hbin, uint32_t offset )
594 if ( !hbin )
595 return False;
597 if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) )
598 return True;
600 return False;
603 /*******************************************************************
604 Input a random offset and receive the corresponding HBIN
605 block for it
606 *******************************************************************/
608 static REGF_HBIN* lookup_hbin_block( REGF_FILE *file, uint32_t offset )
610 REGF_HBIN *hbin = NULL;
611 uint32_t block_off;
613 /* start with the open list */
615 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
616 DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%lx]\n", hbin->file_off, (unsigned long)hbin ));
617 if ( hbin_contains_offset( hbin, offset ) )
618 return hbin;
621 if ( !hbin ) {
622 /* start at the beginning */
624 block_off = REGF_BLOCKSIZE;
625 do {
626 /* cleanup before the next round */
627 if ( hbin )
628 prs_mem_free( &hbin->ps );
630 hbin = read_hbin_block( file, block_off );
632 if ( hbin )
633 block_off = hbin->file_off + hbin->block_size;
635 } while ( hbin && !hbin_contains_offset( hbin, offset ) );
638 if ( hbin )
639 DLIST_ADD( file->block_list, hbin );
641 return hbin;
644 /*******************************************************************
645 *******************************************************************/
647 static bool prs_hash_rec( const char *desc, prs_struct *ps, int depth, REGF_HASH_REC *hash )
649 prs_debug(ps, depth, desc, "prs_hash_rec");
650 depth++;
652 if ( !prs_uint32( "nk_off", ps, depth, &hash->nk_off ))
653 return False;
654 if ( !prs_uint8s( True, "keycheck", ps, depth, hash->keycheck, sizeof( hash->keycheck )) )
655 return False;
657 return True;
660 /*******************************************************************
661 *******************************************************************/
663 static bool hbin_prs_lf_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk )
665 int i;
666 REGF_LF_REC *lf = &nk->subkeys;
667 uint32_t data_size, start_off, end_off;
669 prs_debug(&hbin->ps, depth, desc, "prs_lf_records");
670 depth++;
672 /* check if we have anything to do first */
674 if ( nk->num_subkeys == 0 )
675 return True;
677 /* move to the LF record */
679 if ( !prs_set_offset( &hbin->ps, nk->subkeys_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
680 return False;
682 /* backup and get the data_size */
684 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32_t)) )
685 return False;
686 start_off = prs_offset( &hbin->ps );
687 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &lf->rec_size ))
688 return False;
690 if ( !prs_uint8s( True, "header", &hbin->ps, depth, (uint8_t *)lf->header, sizeof( lf->header )) )
691 return False;
693 if ( !prs_uint16( "num_keys", &hbin->ps, depth, &lf->num_keys))
694 return False;
696 if ( UNMARSHALLING(&hbin->ps) ) {
697 if (lf->num_keys) {
698 if ( !(lf->hashes = PRS_ALLOC_MEM( &hbin->ps, REGF_HASH_REC, lf->num_keys )) )
699 return False;
700 } else {
701 lf->hashes = NULL;
705 for ( i=0; i<lf->num_keys; i++ ) {
706 if ( !prs_hash_rec( "hash_rec", &hbin->ps, depth, &lf->hashes[i] ) )
707 return False;
710 end_off = prs_offset( &hbin->ps );
712 /* data_size must be divisible by 8 and large enough to hold the original record */
714 data_size = ((start_off - end_off) & 0xfffffff8 );
715 if ( data_size > lf->rec_size )
716 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->rec_size));
718 if ( MARSHALLING(&hbin->ps) )
719 hbin->dirty = True;
721 return True;
724 /*******************************************************************
725 *******************************************************************/
727 static bool hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_SK_REC *sk )
729 prs_struct *ps = &hbin->ps;
730 uint16_t tag = 0xFFFF;
731 uint32_t data_size, start_off, end_off;
734 prs_debug(ps, depth, desc, "hbin_prs_sk_rec");
735 depth++;
737 if ( !prs_set_offset( &hbin->ps, sk->sk_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
738 return False;
740 /* backup and get the data_size */
742 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32_t)) )
743 return False;
744 start_off = prs_offset( &hbin->ps );
745 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &sk->rec_size ))
746 return False;
748 if ( !prs_uint8s( True, "header", ps, depth, (uint8_t *)sk->header, sizeof( sk->header )) )
749 return False;
750 if ( !prs_uint16( "tag", ps, depth, &tag))
751 return False;
753 if ( !prs_uint32( "prev_sk_off", ps, depth, &sk->prev_sk_off))
754 return False;
755 if ( !prs_uint32( "next_sk_off", ps, depth, &sk->next_sk_off))
756 return False;
757 if ( !prs_uint32( "ref_count", ps, depth, &sk->ref_count))
758 return False;
759 if ( !prs_uint32( "size", ps, depth, &sk->size))
760 return False;
763 NTSTATUS status;
764 TALLOC_CTX *mem_ctx = prs_get_mem_context(&hbin->ps);
765 DATA_BLOB blob;
767 if (MARSHALLING(&hbin->ps)) {
768 status = marshall_sec_desc(mem_ctx,
769 sk->sec_desc,
770 &blob.data, &blob.length);
771 if (!NT_STATUS_IS_OK(status))
772 return False;
773 if (!prs_copy_data_in(&hbin->ps, (const char *)blob.data, blob.length))
774 return False;
775 } else {
776 blob = data_blob_const(
777 prs_data_p(&hbin->ps) + prs_offset(&hbin->ps),
778 prs_data_size(&hbin->ps) - prs_offset(&hbin->ps)
780 status = unmarshall_sec_desc(mem_ctx,
781 blob.data, blob.length,
782 &sk->sec_desc);
783 if (!NT_STATUS_IS_OK(status))
784 return False;
785 prs_set_offset(&hbin->ps, blob.length);
789 end_off = prs_offset( &hbin->ps );
791 /* data_size must be divisible by 8 and large enough to hold the original record */
793 data_size = ((start_off - end_off) & 0xfffffff8 );
794 if ( data_size > sk->rec_size )
795 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->rec_size));
797 if ( MARSHALLING(&hbin->ps) )
798 hbin->dirty = True;
800 return True;
803 /*******************************************************************
804 *******************************************************************/
806 static bool hbin_prs_vk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_VK_REC *vk, REGF_FILE *file )
808 uint32_t offset;
809 uint16_t name_length;
810 prs_struct *ps = &hbin->ps;
811 uint32_t data_size, start_off, end_off;
813 prs_debug(ps, depth, desc, "prs_vk_rec");
814 depth++;
816 /* backup and get the data_size */
818 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32_t)) )
819 return False;
820 start_off = prs_offset( &hbin->ps );
821 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &vk->rec_size ))
822 return False;
824 if ( !prs_uint8s( True, "header", ps, depth, (uint8_t *)vk->header, sizeof( vk->header )) )
825 return False;
827 if ( MARSHALLING(&hbin->ps) )
828 name_length = strlen(vk->valuename);
830 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
831 return False;
832 if ( !prs_uint32( "data_size", ps, depth, &vk->data_size ))
833 return False;
834 if ( !prs_uint32( "data_off", ps, depth, &vk->data_off ))
835 return False;
836 if ( !prs_uint32( "type", ps, depth, &vk->type))
837 return False;
838 if ( !prs_uint16( "flag", ps, depth, &vk->flag))
839 return False;
841 offset = prs_offset( ps );
842 offset += 2; /* skip 2 bytes */
843 prs_set_offset( ps, offset );
845 /* get the name */
847 if ( vk->flag&VK_FLAG_NAME_PRESENT ) {
849 if ( UNMARSHALLING(&hbin->ps) ) {
850 if ( !(vk->valuename = PRS_ALLOC_MEM( ps, char, name_length+1 )))
851 return False;
853 if ( !prs_uint8s( True, "name", ps, depth, (uint8_t *)vk->valuename, name_length ) )
854 return False;
857 end_off = prs_offset( &hbin->ps );
859 /* get the data if necessary */
861 if ( vk->data_size != 0 ) {
862 bool charmode = False;
864 if ( (vk->type == REG_SZ) || (vk->type == REG_MULTI_SZ) )
865 charmode = True;
867 /* the data is stored in the offset if the size <= 4 */
869 if ( !(vk->data_size & VK_DATA_IN_OFFSET) ) {
870 REGF_HBIN *hblock = hbin;
871 uint32_t data_rec_size;
873 if ( UNMARSHALLING(&hbin->ps) ) {
874 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8_t, vk->data_size) ) )
875 return False;
878 /* this data can be in another hbin */
879 if ( !hbin_contains_offset( hbin, vk->data_off ) ) {
880 if ( !(hblock = lookup_hbin_block( file, vk->data_off )) )
881 return False;
883 if ( !(prs_set_offset( &hblock->ps, (vk->data_off+HBIN_HDR_SIZE-hblock->first_hbin_off)-sizeof(uint32_t) )) )
884 return False;
886 if ( MARSHALLING(&hblock->ps) ) {
887 data_rec_size = ( (vk->data_size+sizeof(uint32_t)) & 0xfffffff8 ) + 8;
888 data_rec_size = ( data_rec_size - 1 ) ^ 0xFFFFFFFF;
890 if ( !prs_uint32( "data_rec_size", &hblock->ps, depth, &data_rec_size ))
891 return False;
892 if ( !prs_uint8s( charmode, "data", &hblock->ps, depth, vk->data, vk->data_size) )
893 return False;
895 if ( MARSHALLING(&hblock->ps) )
896 hblock->dirty = True;
898 else {
899 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8_t, 4 ) ) )
900 return False;
901 SIVAL( vk->data, 0, vk->data_off );
906 /* data_size must be divisible by 8 and large enough to hold the original record */
908 data_size = ((start_off - end_off ) & 0xfffffff8 );
909 if ( data_size != vk->rec_size )
910 DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size, vk->rec_size));
912 if ( MARSHALLING(&hbin->ps) )
913 hbin->dirty = True;
915 return True;
918 /*******************************************************************
919 read a VK record which is contained in the HBIN block stored
920 in the prs_struct *ps.
921 *******************************************************************/
923 static bool hbin_prs_vk_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk, REGF_FILE *file )
925 int i;
926 uint32_t record_size;
928 prs_debug(&hbin->ps, depth, desc, "prs_vk_records");
929 depth++;
931 /* check if we have anything to do first */
933 if ( nk->num_values == 0 )
934 return True;
936 if ( UNMARSHALLING(&hbin->ps) ) {
937 if ( !(nk->values = PRS_ALLOC_MEM( &hbin->ps, REGF_VK_REC, nk->num_values ) ) )
938 return False;
941 /* convert the offset to something relative to this HBIN block */
943 if ( !prs_set_offset( &hbin->ps, nk->values_off+HBIN_HDR_SIZE-hbin->first_hbin_off-sizeof(uint32_t)) )
944 return False;
946 if ( MARSHALLING( &hbin->ps) ) {
947 record_size = ( ( nk->num_values * sizeof(uint32_t) ) & 0xfffffff8 ) + 8;
948 record_size = (record_size - 1) ^ 0xFFFFFFFF;
951 if ( !prs_uint32( "record_size", &hbin->ps, depth, &record_size ) )
952 return False;
954 for ( i=0; i<nk->num_values; i++ ) {
955 if ( !prs_uint32( "vk_off", &hbin->ps, depth, &nk->values[i].rec_off ) )
956 return False;
959 for ( i=0; i<nk->num_values; i++ ) {
960 REGF_HBIN *sub_hbin = hbin;
961 uint32_t new_offset;
963 if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) ) {
964 sub_hbin = lookup_hbin_block( file, nk->values[i].rec_off );
965 if ( !sub_hbin ) {
966 DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n",
967 nk->values[i].hbin_off));
968 return False;
972 new_offset = nk->values[i].rec_off + HBIN_HDR_SIZE - sub_hbin->first_hbin_off;
973 if ( !prs_set_offset( &sub_hbin->ps, new_offset ) )
974 return False;
975 if ( !hbin_prs_vk_rec( "vk_rec", sub_hbin, depth, &nk->values[i], file ) )
976 return False;
979 if ( MARSHALLING(&hbin->ps) )
980 hbin->dirty = True;
983 return True;
987 /*******************************************************************
988 *******************************************************************/
990 static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32_t offset )
992 REGF_SK_REC *p_sk;
994 for ( p_sk=file->sec_desc_list; p_sk; p_sk=p_sk->next ) {
995 if ( p_sk->sk_off == offset )
996 return p_sk;
999 return NULL;
1002 /*******************************************************************
1003 *******************************************************************/
1005 static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, struct security_descriptor *sd )
1007 REGF_SK_REC *p;
1009 for ( p=file->sec_desc_list; p; p=p->next ) {
1010 if ( security_descriptor_equal( p->sec_desc, sd ) )
1011 return p;
1014 /* failure */
1016 return NULL;
1019 /*******************************************************************
1020 *******************************************************************/
1022 static bool hbin_prs_key( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk )
1024 int depth = 0;
1025 REGF_HBIN *sub_hbin;
1027 prs_debug(&hbin->ps, depth, "", "prs_key");
1028 depth++;
1030 /* get the initial nk record */
1032 if ( !prs_nk_rec( "nk_rec", &hbin->ps, depth, nk ))
1033 return False;
1035 /* fill in values */
1037 if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) {
1038 sub_hbin = hbin;
1039 if ( !hbin_contains_offset( hbin, nk->values_off ) ) {
1040 sub_hbin = lookup_hbin_block( file, nk->values_off );
1041 if ( !sub_hbin ) {
1042 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n",
1043 nk->values_off));
1044 return False;
1048 if ( !hbin_prs_vk_records( "vk_rec", sub_hbin, depth, nk, file ))
1049 return False;
1052 /* now get subkeys */
1054 if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) {
1055 sub_hbin = hbin;
1056 if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) {
1057 sub_hbin = lookup_hbin_block( file, nk->subkeys_off );
1058 if ( !sub_hbin ) {
1059 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n",
1060 nk->subkeys_off));
1061 return False;
1065 if ( !hbin_prs_lf_records( "lf_rec", sub_hbin, depth, nk ))
1066 return False;
1069 /* get the to the security descriptor. First look if we have already parsed it */
1071 if ( (nk->sk_off!=REGF_OFFSET_NONE) && !( nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )) ) {
1073 sub_hbin = hbin;
1074 if ( !hbin_contains_offset( hbin, nk->sk_off ) ) {
1075 sub_hbin = lookup_hbin_block( file, nk->sk_off );
1076 if ( !sub_hbin ) {
1077 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_off [0x%x]\n",
1078 nk->sk_off));
1079 return False;
1083 if ( !(nk->sec_desc = talloc_zero( file->mem_ctx, REGF_SK_REC )) )
1084 return False;
1085 nk->sec_desc->sk_off = nk->sk_off;
1086 if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin, depth, nk->sec_desc ))
1087 return False;
1089 /* add to the list of security descriptors (ref_count has been read from the files) */
1091 nk->sec_desc->sk_off = nk->sk_off;
1092 DLIST_ADD( file->sec_desc_list, nk->sec_desc );
1095 return True;
1098 /*******************************************************************
1099 *******************************************************************/
1101 static bool next_record( REGF_HBIN *hbin, const char *hdr, bool *eob )
1103 uint8_t header[REC_HDR_SIZE];
1104 uint32_t record_size;
1105 uint32_t curr_off, block_size;
1106 bool found = False;
1107 prs_struct *ps = &hbin->ps;
1109 curr_off = prs_offset( ps );
1110 if ( curr_off == 0 )
1111 prs_set_offset( ps, HBIN_HEADER_REC_SIZE );
1113 /* assume that the current offset is at the record header
1114 and we need to backup to read the record size */
1116 curr_off -= sizeof(uint32_t);
1118 block_size = prs_data_size( ps );
1119 record_size = 0;
1120 memset( header, 0x0, sizeof(uint8_t)*REC_HDR_SIZE );
1121 while ( !found ) {
1123 curr_off = curr_off+record_size;
1124 if ( curr_off >= block_size )
1125 break;
1127 if ( !prs_set_offset( &hbin->ps, curr_off) )
1128 return False;
1130 if ( !prs_uint32( "record_size", ps, 0, &record_size ) )
1131 return False;
1132 if ( !prs_uint8s( True, "header", ps, 0, header, REC_HDR_SIZE ) )
1133 return False;
1135 if (record_size & 0x80000000) {
1136 /* absolute_value(record_size) */
1137 record_size = (record_size ^ 0xffffffff) + 1;
1140 if (record_size < sizeof(REC_HDR_SIZE)) {
1141 return false;
1144 if (memcmp(header, hdr, REC_HDR_SIZE) == 0) {
1145 found = True;
1146 curr_off += sizeof(uint32_t);
1150 /* mark prs_struct as done ( at end ) if no more SK records */
1151 /* mark end-of-block as True */
1153 if ( !found ) {
1154 prs_set_offset( &hbin->ps, prs_data_size(&hbin->ps) );
1155 *eob = True;
1156 return False;
1159 if ( !prs_set_offset( ps, curr_off ) )
1160 return False;
1162 return True;
1165 /*******************************************************************
1166 *******************************************************************/
1168 static bool next_nk_record( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk, bool *eob )
1170 if ( next_record( hbin, "nk", eob ) && hbin_prs_key( file, hbin, nk ) )
1171 return True;
1173 return False;
1176 /*******************************************************************
1177 Initialize the newly created REGF_BLOCK in *file and write the
1178 block header to disk
1179 *******************************************************************/
1181 static bool init_regf_block( REGF_FILE *file )
1183 prs_struct ps;
1184 bool result = True;
1186 if ( !prs_init( &ps, REGF_BLOCKSIZE, file->mem_ctx, MARSHALL ) )
1187 return False;
1189 memcpy( file->header, "regf", REGF_HDR_SIZE );
1190 file->data_offset = 0x20;
1191 file->last_block = 0x1000;
1193 /* set mod time */
1195 unix_to_nt_time( &file->mtime, time(NULL) );
1197 /* hard coded values...no idea what these are ... maybe in time */
1199 file->unknown1 = 0x2;
1200 file->unknown2 = 0x1;
1201 file->unknown3 = 0x3;
1202 file->unknown4 = 0x0;
1203 file->unknown5 = 0x1;
1204 file->unknown6 = 0x1;
1206 /* write header to the buffer */
1208 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1209 result = False;
1210 goto out;
1213 /* calculate the checksum, re-marshall data (to include the checksum)
1214 and write to disk */
1216 file->checksum = regf_block_checksum( &ps );
1217 prs_set_offset( &ps, 0 );
1218 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1219 result = False;
1220 goto out;
1223 if ( write_block( file, &ps, 0 ) == -1 ) {
1224 DEBUG(0,("init_regf_block: Failed to initialize registry header block!\n"));
1225 result = False;
1226 goto out;
1229 out:
1230 prs_mem_free( &ps );
1232 return result;
1234 /*******************************************************************
1235 Open the registry file and then read in the REGF block to get the
1236 first hbin offset.
1237 *******************************************************************/
1239 REGF_FILE* regfio_open( const char *filename, int flags, int mode )
1241 REGF_FILE *rb;
1243 if ( !(rb = SMB_MALLOC_P(REGF_FILE)) ) {
1244 DEBUG(0,("ERROR allocating memory\n"));
1245 return NULL;
1247 ZERO_STRUCTP( rb );
1248 rb->fd = -1;
1249 rb->ignore_checksums = false;
1251 if ( !(rb->mem_ctx = talloc_init( "regfio_open" )) ) {
1252 regfio_close( rb );
1253 return NULL;
1256 rb->open_flags = flags;
1258 /* open and existing file */
1260 if ( (rb->fd = open(filename, flags, mode)) == -1 ) {
1261 DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename, strerror(errno)));
1262 regfio_close( rb );
1263 return NULL;
1266 /* check if we are creating a new file or overwriting an existing one */
1268 if ( flags & (O_CREAT|O_TRUNC) ) {
1269 if ( !init_regf_block( rb ) ) {
1270 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1271 regfio_close( rb );
1272 return NULL;
1275 /* success */
1276 return rb;
1279 /* read in an existing file */
1281 if ( !read_regf_block( rb ) ) {
1282 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1283 regfio_close( rb );
1284 return NULL;
1287 /* success */
1289 return rb;
1292 /*******************************************************************
1293 *******************************************************************/
1295 static void regfio_mem_free( REGF_FILE *file )
1297 /* free any talloc()'d memory */
1299 if ( file && file->mem_ctx )
1300 talloc_destroy( file->mem_ctx );
1303 /*******************************************************************
1304 *******************************************************************/
1306 int regfio_close( REGF_FILE *file )
1308 int fd;
1310 /* cleanup for a file opened for write */
1312 if ((file->fd != -1) && (file->open_flags & (O_WRONLY|O_RDWR))) {
1313 prs_struct ps;
1314 REGF_SK_REC *sk;
1316 /* write of sd list */
1318 for ( sk=file->sec_desc_list; sk; sk=sk->next ) {
1319 hbin_prs_sk_rec( "sk_rec", sk->hbin, 0, sk );
1322 /* flush any dirty blocks */
1324 while ( file->block_list ) {
1325 hbin_block_close( file, file->block_list );
1328 ZERO_STRUCT( ps );
1330 unix_to_nt_time( &file->mtime, time(NULL) );
1332 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) != -1 ) {
1333 /* now use for writing */
1334 prs_switch_type( &ps, MARSHALL );
1336 /* stream the block once, generate the checksum,
1337 and stream it again */
1338 prs_set_offset( &ps, 0 );
1339 prs_regf_block( "regf_blocK", &ps, 0, file );
1340 file->checksum = regf_block_checksum( &ps );
1341 prs_set_offset( &ps, 0 );
1342 prs_regf_block( "regf_blocK", &ps, 0, file );
1344 /* now we are ready to write it to disk */
1345 if ( write_block( file, &ps, 0 ) == -1 )
1346 DEBUG(0,("regfio_close: failed to update the regf header block!\n"));
1349 prs_mem_free( &ps );
1352 regfio_mem_free( file );
1354 /* nothing tdo do if there is no open file */
1356 if (file->fd == -1)
1357 return 0;
1359 fd = file->fd;
1360 file->fd = -1;
1361 SAFE_FREE( file );
1363 return close( fd );
1366 /*******************************************************************
1367 *******************************************************************/
1369 static void regfio_flush( REGF_FILE *file )
1371 REGF_HBIN *hbin;
1373 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
1374 write_hbin_block( file, hbin );
1378 /*******************************************************************
1379 There should be only *one* root key in the registry file based
1380 on my experience. --jerry
1381 *******************************************************************/
1383 REGF_NK_REC* regfio_rootkey( REGF_FILE *file )
1385 REGF_NK_REC *nk;
1386 REGF_HBIN *hbin;
1387 uint32_t offset = REGF_BLOCKSIZE;
1388 bool found = False;
1389 bool eob;
1391 if ( !file )
1392 return NULL;
1394 if ( !(nk = talloc_zero( file->mem_ctx, REGF_NK_REC )) ) {
1395 DEBUG(0,("regfio_rootkey: talloc() failed!\n"));
1396 return NULL;
1399 /* scan through the file on HBIN block at a time looking
1400 for an NK record with a type == 0x002c.
1401 Normally this is the first nk record in the first hbin
1402 block (but I'm not assuming that for now) */
1404 while ( (hbin = read_hbin_block( file, offset )) ) {
1405 eob = False;
1407 while ( !eob) {
1408 if ( next_nk_record( file, hbin, nk, &eob ) ) {
1409 if ( nk->key_type == NK_TYPE_ROOTKEY ) {
1410 found = True;
1411 break;
1414 prs_mem_free( &hbin->ps );
1417 if ( found )
1418 break;
1420 offset += hbin->block_size;
1423 if ( !found ) {
1424 DEBUG(0,("regfio_rootkey: corrupt registry file ? No root key record located\n"));
1425 return NULL;
1428 DLIST_ADD( file->block_list, hbin );
1430 return nk;
1433 /*******************************************************************
1434 This acts as an iterator over the subkeys defined for a given
1435 NK record. Remember that offsets are from the *first* HBIN block.
1436 *******************************************************************/
1438 REGF_NK_REC* regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk )
1440 REGF_NK_REC *subkey;
1441 REGF_HBIN *hbin;
1442 uint32_t nk_offset;
1444 /* see if there is anything left to report */
1446 if (nk == NULL ||
1447 nk->subkeys.hashes == NULL ||
1448 nk->subkey_index >= nk->subkeys.num_keys ||
1449 (nk->subkeys_off == REGF_OFFSET_NONE) ||
1450 (nk->subkey_index >= nk->num_subkeys)) {
1451 return NULL;
1454 /* find the HBIN block which should contain the nk record */
1456 hbin = lookup_hbin_block(file,
1457 nk->subkeys.hashes[nk->subkey_index].nk_off);
1458 if (hbin == NULL) {
1459 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n",
1460 nk->subkeys.hashes[nk->subkey_index].nk_off));
1461 return NULL;
1464 nk_offset = nk->subkeys.hashes[nk->subkey_index].nk_off;
1465 if ( !prs_set_offset( &hbin->ps, (HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off) ) )
1466 return NULL;
1468 nk->subkey_index++;
1469 if ( !(subkey = talloc_zero( file->mem_ctx, REGF_NK_REC )) )
1470 return NULL;
1472 if ( !hbin_prs_key( file, hbin, subkey ) )
1473 return NULL;
1475 return subkey;
1479 /*******************************************************************
1480 *******************************************************************/
1482 static REGF_HBIN* regf_hbin_allocate( REGF_FILE *file, uint32_t block_size )
1484 REGF_HBIN *hbin;
1485 SMB_STRUCT_STAT sbuf;
1487 if ( !(hbin = talloc_zero( file->mem_ctx, REGF_HBIN )) )
1488 return NULL;
1490 memcpy( hbin->header, "hbin", HBIN_HDR_SIZE);
1493 if (sys_fstat(file->fd, &sbuf, false)) {
1494 DEBUG(0,("regf_hbin_allocate: stat() failed! (%s)\n", strerror(errno)));
1495 return NULL;
1498 hbin->file_off = sbuf.st_ex_size;
1500 hbin->free_off = HBIN_HEADER_REC_SIZE;
1501 hbin->free_size = block_size - hbin->free_off + sizeof(uint32_t);
1503 hbin->block_size = block_size;
1504 hbin->first_hbin_off = hbin->file_off - REGF_BLOCKSIZE;
1506 if ( !prs_init( &hbin->ps, block_size, file->mem_ctx, MARSHALL ) )
1507 return NULL;
1509 if ( !prs_hbin_block( "new_hbin", &hbin->ps, 0, hbin ) )
1510 return NULL;
1512 if ( !write_hbin_block( file, hbin ) )
1513 return NULL;
1515 file->last_block = hbin->file_off;
1517 return hbin;
1520 /*******************************************************************
1521 *******************************************************************/
1523 static void update_free_space( REGF_HBIN *hbin, uint32_t size_used )
1525 hbin->free_off += size_used;
1526 hbin->free_size -= size_used;
1528 if ( hbin->free_off >= hbin->block_size ) {
1529 hbin->free_off = REGF_OFFSET_NONE;
1532 return;
1535 /*******************************************************************
1536 *******************************************************************/
1538 static REGF_HBIN* find_free_space( REGF_FILE *file, uint32_t size )
1540 REGF_HBIN *hbin, *p_hbin;
1541 uint32_t block_off;
1542 bool cached;
1544 /* check open block list */
1546 for ( hbin=file->block_list; hbin!=NULL; hbin=hbin->next ) {
1547 /* only check blocks that actually have available space */
1549 if ( hbin->free_off == REGF_OFFSET_NONE )
1550 continue;
1552 /* check for a large enough available chunk */
1554 if ( (hbin->block_size - hbin->free_off) >= size ) {
1555 DLIST_PROMOTE( file->block_list, hbin );
1556 goto done;
1560 /* parse the file until we find a block with
1561 enough free space; save the last non-filled hbin */
1563 block_off = REGF_BLOCKSIZE;
1564 do {
1565 /* cleanup before the next round */
1566 cached = False;
1567 if ( hbin )
1568 prs_mem_free( &hbin->ps );
1570 hbin = read_hbin_block( file, block_off );
1572 if ( hbin ) {
1574 /* make sure that we don't already have this block in memory */
1576 for ( p_hbin=file->block_list; p_hbin!=NULL; p_hbin=p_hbin->next ) {
1577 if ( p_hbin->file_off == hbin->file_off ) {
1578 cached = True;
1579 break;
1583 block_off = hbin->file_off + hbin->block_size;
1585 if ( cached ) {
1586 prs_mem_free( &hbin->ps );
1587 hbin = NULL;
1588 continue;
1591 /* if (cached block or (new block and not enough free space)) then continue looping */
1592 } while ( cached || (hbin && (hbin->free_size < size)) );
1594 /* no free space; allocate a new one */
1596 if ( !hbin ) {
1597 uint32_t alloc_size;
1599 /* allocate in multiples of REGF_ALLOC_BLOCK; make sure (size + hbin_header) fits */
1601 alloc_size = (((size+HBIN_HEADER_REC_SIZE) / REGF_ALLOC_BLOCK ) + 1 ) * REGF_ALLOC_BLOCK;
1603 if ( !(hbin = regf_hbin_allocate( file, alloc_size )) ) {
1604 DEBUG(0,("find_free_space: regf_hbin_allocate() failed!\n"));
1605 return NULL;
1607 DLIST_ADD( file->block_list, hbin );
1610 done:
1611 /* set the offset to be ready to write */
1613 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32_t) ) )
1614 return NULL;
1616 /* write the record size as a placeholder for now, it should be
1617 probably updated by the caller once it all of the data necessary
1618 for the record */
1620 if ( !prs_uint32("allocated_size", &hbin->ps, 0, &size) )
1621 return NULL;
1623 update_free_space( hbin, size );
1625 return hbin;
1628 /*******************************************************************
1629 *******************************************************************/
1631 static uint32_t sk_record_data_size( struct security_descriptor * sd )
1633 uint32_t size, size_mod8;
1635 size_mod8 = 0;
1637 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1639 size = sizeof(uint32_t)*5 + ndr_size_security_descriptor(sd, 0) + sizeof(uint32_t);
1641 /* multiple of 8 */
1642 size_mod8 = size & 0xfffffff8;
1643 if ( size_mod8 < size )
1644 size_mod8 += 8;
1646 return size_mod8;
1649 /*******************************************************************
1650 *******************************************************************/
1652 static uint32_t vk_record_data_size( REGF_VK_REC *vk )
1654 uint32_t size, size_mod8;
1656 size_mod8 = 0;
1658 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1660 size = REC_HDR_SIZE + (sizeof(uint16_t)*3) + (sizeof(uint32_t)*3) + sizeof(uint32_t);
1662 if ( vk->valuename )
1663 size += strlen(vk->valuename);
1665 /* multiple of 8 */
1666 size_mod8 = size & 0xfffffff8;
1667 if ( size_mod8 < size )
1668 size_mod8 += 8;
1670 return size_mod8;
1673 /*******************************************************************
1674 *******************************************************************/
1676 static uint32_t lf_record_data_size( uint32_t num_keys )
1678 uint32_t size, size_mod8;
1680 size_mod8 = 0;
1682 /* the record size is sizeof(hdr) + num_keys + sizeof of hash_array + data_size_uint32_t */
1684 size = REC_HDR_SIZE + sizeof(uint16_t) + (sizeof(REGF_HASH_REC) * num_keys) + sizeof(uint32_t);
1686 /* multiple of 8 */
1687 size_mod8 = size & 0xfffffff8;
1688 if ( size_mod8 < size )
1689 size_mod8 += 8;
1691 return size_mod8;
1694 /*******************************************************************
1695 *******************************************************************/
1697 static uint32_t nk_record_data_size( REGF_NK_REC *nk )
1699 uint32_t size, size_mod8;
1701 size_mod8 = 0;
1703 /* the record size is static + length_of_keyname + length_of_classname + data_size_uint32_t */
1705 size = 0x4c + strlen(nk->keyname) + sizeof(uint32_t);
1707 if ( nk->classname )
1708 size += strlen( nk->classname );
1710 /* multiple of 8 */
1711 size_mod8 = size & 0xfffffff8;
1712 if ( size_mod8 < size )
1713 size_mod8 += 8;
1715 return size_mod8;
1718 /*******************************************************************
1719 *******************************************************************/
1721 static bool create_vk_record(REGF_FILE *file, REGF_VK_REC *vk,
1722 struct regval_blob *value)
1724 char *name = regval_name(value);
1725 REGF_HBIN *data_hbin;
1727 ZERO_STRUCTP( vk );
1729 memcpy( vk->header, "vk", REC_HDR_SIZE );
1731 if ( name ) {
1732 vk->valuename = talloc_strdup( file->mem_ctx, regval_name(value) );
1733 vk->flag = VK_FLAG_NAME_PRESENT;
1736 vk->data_size = regval_size( value );
1737 vk->type = regval_type( value );
1739 if ( vk->data_size > sizeof(uint32_t) ) {
1740 uint32_t data_size = ( (vk->data_size+sizeof(uint32_t)) & 0xfffffff8 ) + 8;
1742 vk->data = (uint8_t *)talloc_memdup( file->mem_ctx,
1743 regval_data_p(value),
1744 vk->data_size );
1745 if (vk->data == NULL) {
1746 return False;
1749 /* go ahead and store the offset....we'll pick this hbin block back up when
1750 we stream the data */
1752 if ((data_hbin = find_free_space(file, data_size )) == NULL) {
1753 return False;
1755 vk->data_off = prs_offset( &data_hbin->ps ) + data_hbin->first_hbin_off - HBIN_HDR_SIZE;
1757 else {
1758 /* make sure we don't try to copy from a NULL value pointer */
1760 if ( vk->data_size != 0 )
1761 memcpy( &vk->data_off, regval_data_p(value), vk->data_size);
1762 vk->data_size |= VK_DATA_IN_OFFSET;
1765 return True;
1768 /*******************************************************************
1769 *******************************************************************/
1771 static int hashrec_cmp( REGF_HASH_REC *h1, REGF_HASH_REC *h2 )
1773 return strcasecmp_m( h1->fullname, h2->fullname );
1776 /*******************************************************************
1777 *******************************************************************/
1779 REGF_NK_REC* regfio_write_key( REGF_FILE *file, const char *name,
1780 struct regval_ctr *values, struct regsubkey_ctr *subkeys,
1781 struct security_descriptor *sec_desc, REGF_NK_REC *parent )
1783 REGF_NK_REC *nk;
1784 REGF_HBIN *vlist_hbin = NULL;
1785 uint32_t size;
1787 if ( !(nk = talloc_zero( file->mem_ctx, REGF_NK_REC )) )
1788 return NULL;
1790 memcpy( nk->header, "nk", REC_HDR_SIZE );
1792 if ( !parent )
1793 nk->key_type = NK_TYPE_ROOTKEY;
1794 else
1795 nk->key_type = NK_TYPE_NORMALKEY;
1797 /* store the parent offset (or -1 if a the root key */
1799 nk->parent_off = parent ? (parent->hbin_off + parent->hbin->file_off - REGF_BLOCKSIZE - HBIN_HDR_SIZE ) : REGF_OFFSET_NONE;
1801 /* no classname currently */
1803 nk->classname_off = REGF_OFFSET_NONE;
1804 nk->classname = NULL;
1805 nk->keyname = talloc_strdup( file->mem_ctx, name );
1807 /* current modification time */
1809 unix_to_nt_time( &nk->mtime, time(NULL) );
1811 /* allocate the record on disk */
1813 size = nk_record_data_size( nk );
1814 nk->rec_size = ( size - 1 ) ^ 0XFFFFFFFF;
1815 if ((nk->hbin = find_free_space( file, size )) == NULL) {
1816 return NULL;
1818 nk->hbin_off = prs_offset( &nk->hbin->ps );
1820 /* Update the hash record in the parent */
1822 if ( parent ) {
1823 REGF_HASH_REC *hash = &parent->subkeys.hashes[parent->subkey_index];
1825 hash->nk_off = prs_offset( &nk->hbin->ps ) + nk->hbin->first_hbin_off - HBIN_HDR_SIZE;
1826 memcpy(hash->keycheck, name, MIN(strlen(name),sizeof(uint32_t)));
1827 hash->fullname = talloc_strdup( file->mem_ctx, name );
1828 parent->subkey_index++;
1830 /* sort the list by keyname */
1831 TYPESAFE_QSORT(parent->subkeys.hashes, parent->subkey_index, hashrec_cmp);
1833 if ( !hbin_prs_lf_records( "lf_rec", parent->subkeys.hbin, 0, parent ) )
1834 return NULL;
1837 /* write the security descriptor */
1839 nk->sk_off = REGF_OFFSET_NONE;
1840 if ( sec_desc ) {
1841 uint32_t sk_size = sk_record_data_size( sec_desc );
1842 REGF_HBIN *sk_hbin;
1844 /* search for it in the existing list of sd's */
1846 if ( (nk->sec_desc = find_sk_record_by_sec_desc( file, sec_desc )) == NULL ) {
1847 /* not found so add it to the list */
1849 if (!(sk_hbin = find_free_space( file, sk_size ))) {
1850 return NULL;
1853 if ( !(nk->sec_desc = talloc_zero( file->mem_ctx, REGF_SK_REC )) )
1854 return NULL;
1856 /* now we have to store the security descriptor in the list and
1857 update the offsets */
1859 memcpy( nk->sec_desc->header, "sk", REC_HDR_SIZE );
1860 nk->sec_desc->hbin = sk_hbin;
1861 nk->sec_desc->hbin_off = prs_offset( &sk_hbin->ps );
1862 nk->sec_desc->sk_off = prs_offset( &sk_hbin->ps ) + sk_hbin->first_hbin_off - HBIN_HDR_SIZE;
1863 nk->sec_desc->rec_size = (sk_size-1) ^ 0xFFFFFFFF;
1865 nk->sec_desc->sec_desc = sec_desc;
1866 nk->sec_desc->ref_count = 0;
1868 /* size value must be self-inclusive */
1869 nk->sec_desc->size = ndr_size_security_descriptor(sec_desc, 0)
1870 + sizeof(uint32_t);
1872 DLIST_ADD_END( file->sec_desc_list, nk->sec_desc);
1874 /* update the offsets for us and the previous sd in the list.
1875 if this is the first record, then just set the next and prev
1876 offsets to ourself. */
1878 if ( DLIST_PREV(nk->sec_desc) ) {
1879 REGF_SK_REC *prev = DLIST_PREV(nk->sec_desc);
1881 nk->sec_desc->prev_sk_off = prev->hbin_off + prev->hbin->first_hbin_off - HBIN_HDR_SIZE;
1882 prev->next_sk_off = nk->sec_desc->sk_off;
1884 /* the end must loop around to the front */
1885 nk->sec_desc->next_sk_off = file->sec_desc_list->sk_off;
1887 /* and first must loop around to the tail */
1888 file->sec_desc_list->prev_sk_off = nk->sec_desc->sk_off;
1889 } else {
1890 nk->sec_desc->prev_sk_off = nk->sec_desc->sk_off;
1891 nk->sec_desc->next_sk_off = nk->sec_desc->sk_off;
1895 /* bump the reference count +1 */
1897 nk->sk_off = nk->sec_desc->sk_off;
1898 nk->sec_desc->ref_count++;
1901 /* write the subkeys */
1903 nk->subkeys_off = REGF_OFFSET_NONE;
1904 if ( (nk->num_subkeys = regsubkey_ctr_numkeys( subkeys )) != 0 ) {
1905 uint32_t lf_size = lf_record_data_size( nk->num_subkeys );
1906 uint32_t namelen;
1907 int i;
1909 if (!(nk->subkeys.hbin = find_free_space( file, lf_size ))) {
1910 return NULL;
1912 nk->subkeys.hbin_off = prs_offset( &nk->subkeys.hbin->ps );
1913 nk->subkeys.rec_size = (lf_size-1) ^ 0xFFFFFFFF;
1914 nk->subkeys_off = prs_offset( &nk->subkeys.hbin->ps ) + nk->subkeys.hbin->first_hbin_off - HBIN_HDR_SIZE;
1916 memcpy( nk->subkeys.header, "lf", REC_HDR_SIZE );
1918 nk->subkeys.num_keys = nk->num_subkeys;
1919 if (nk->subkeys.num_keys) {
1920 if ( !(nk->subkeys.hashes = talloc_zero_array( file->mem_ctx, REGF_HASH_REC, nk->subkeys.num_keys )) )
1921 return NULL;
1922 } else {
1923 nk->subkeys.hashes = NULL;
1925 nk->subkey_index = 0;
1927 /* update the max_bytes_subkey{name,classname} fields */
1928 for ( i=0; i<nk->num_subkeys; i++ ) {
1929 namelen = strlen( regsubkey_ctr_specific_key(subkeys, i) );
1930 if ( namelen*2 > nk->max_bytes_subkeyname )
1931 nk->max_bytes_subkeyname = namelen * 2;
1935 /* write the values */
1937 nk->values_off = REGF_OFFSET_NONE;
1938 if ( (nk->num_values = regval_ctr_numvals( values )) != 0 ) {
1939 uint32_t vlist_size = ( ( nk->num_values * sizeof(uint32_t) ) & 0xfffffff8 ) + 8;
1940 int i;
1942 if (!(vlist_hbin = find_free_space( file, vlist_size ))) {
1943 return NULL;
1945 nk->values_off = prs_offset( &vlist_hbin->ps ) + vlist_hbin->first_hbin_off - HBIN_HDR_SIZE;
1947 if (nk->num_values) {
1948 if ( !(nk->values = talloc_array( file->mem_ctx, REGF_VK_REC, nk->num_values )) )
1949 return NULL;
1950 } else {
1951 nk->values = NULL;
1954 /* create the vk records */
1956 for ( i=0; i<nk->num_values; i++ ) {
1957 uint32_t vk_size, namelen, datalen;
1958 struct regval_blob *r;
1960 r = regval_ctr_specific_value( values, i );
1961 create_vk_record( file, &nk->values[i], r );
1962 vk_size = vk_record_data_size( &nk->values[i] );
1963 nk->values[i].hbin = find_free_space( file, vk_size );
1964 nk->values[i].hbin_off = prs_offset( &nk->values[i].hbin->ps );
1965 nk->values[i].rec_size = ( vk_size - 1 ) ^ 0xFFFFFFFF;
1966 nk->values[i].rec_off = prs_offset( &nk->values[i].hbin->ps )
1967 + nk->values[i].hbin->first_hbin_off
1968 - HBIN_HDR_SIZE;
1970 /* update the max bytes fields if necessary */
1972 namelen = strlen( regval_name(r) );
1973 if ( namelen*2 > nk->max_bytes_valuename )
1974 nk->max_bytes_valuename = namelen * 2;
1976 datalen = regval_size( r );
1977 if ( datalen > nk->max_bytes_value )
1978 nk->max_bytes_value = datalen;
1982 /* stream the records */
1984 prs_set_offset( &nk->hbin->ps, nk->hbin_off );
1985 if ( !prs_nk_rec( "nk_rec", &nk->hbin->ps, 0, nk ) )
1986 return NULL;
1988 if ( nk->num_values ) {
1989 if ( !hbin_prs_vk_records( "vk_records", vlist_hbin, 0, nk, file ) )
1990 return NULL;
1994 regfio_flush( file );
1996 return nk;