s3: Fix malformed require_membership_of_sid.
[Samba.git] / source / registry / regfio.c
blobb8395d0c03cbacad24aec6ad85651976c6b4e31f
1 /*
2 * Unix SMB/CIFS implementation.
3 * Windows NT registry I/O library
4 * Copyright (c) Gerald (Jerry) Carter 2005
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "regfio.h"
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_REGISTRY
26 /*******************************************************************
28 * TODO : Right now this code basically ignores classnames.
30 ******************************************************************/
33 /*******************************************************************
34 *******************************************************************/
36 static int write_block( REGF_FILE *file, prs_struct *ps, uint32 offset )
38 int bytes_written, returned;
39 char *buffer = prs_data_p( ps );
40 uint32 buffer_size = prs_data_size( ps );
41 SMB_STRUCT_STAT sbuf;
43 if ( file->fd == -1 )
44 return -1;
46 /* check for end of file */
48 if ( sys_fstat( file->fd, &sbuf ) ) {
49 DEBUG(0,("write_block: stat() failed! (%s)\n", strerror(errno)));
50 return -1;
53 if ( lseek( file->fd, offset, SEEK_SET ) == -1 ) {
54 DEBUG(0,("write_block: lseek() failed! (%s)\n", strerror(errno) ));
55 return -1;
58 bytes_written = returned = 0;
59 while ( bytes_written < buffer_size ) {
60 if ( (returned = write( file->fd, buffer+bytes_written, buffer_size-bytes_written )) == -1 ) {
61 DEBUG(0,("write_block: write() failed! (%s)\n", strerror(errno) ));
62 return False;
65 bytes_written += returned;
68 return bytes_written;
71 /*******************************************************************
72 *******************************************************************/
74 static int read_block( REGF_FILE *file, prs_struct *ps, uint32 file_offset, uint32 block_size )
76 int bytes_read, returned;
77 char *buffer;
78 SMB_STRUCT_STAT sbuf;
80 /* check for end of file */
82 if ( sys_fstat( file->fd, &sbuf ) ) {
83 DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno)));
84 return -1;
87 if ( (size_t)file_offset >= sbuf.st_size )
88 return -1;
90 /* if block_size == 0, we are parsing HBIN records and need
91 to read some of the header to get the block_size from there */
93 if ( block_size == 0 ) {
94 char hdr[0x20];
96 if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
97 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
98 return -1;
101 returned = read( file->fd, hdr, 0x20 );
102 if ( (returned == -1) || (returned < 0x20) ) {
103 DEBUG(0,("read_block: failed to read in HBIN header. Is the file corrupt?\n"));
104 return -1;
107 /* make sure this is an hbin header */
109 if ( strncmp( hdr, "hbin", HBIN_HDR_SIZE ) != 0 ) {
110 DEBUG(0,("read_block: invalid block header!\n"));
111 return -1;
114 block_size = IVAL( hdr, 0x08 );
117 DEBUG(10,("read_block: block_size == 0x%x\n", block_size ));
119 /* set the offset, initialize the buffer, and read the block from disk */
121 if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
122 DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));
123 return -1;
126 if (!prs_init( ps, block_size, file->mem_ctx, UNMARSHALL )) {
127 DEBUG(0,("read_block: prs_init() failed! (%s)\n", strerror(errno) ));
128 return -1;
130 buffer = prs_data_p( ps );
131 bytes_read = returned = 0;
133 while ( bytes_read < block_size ) {
134 if ( (returned = read( file->fd, buffer+bytes_read, block_size-bytes_read )) == -1 ) {
135 DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno) ));
136 return False;
138 if ( (returned == 0) && (bytes_read < block_size) ) {
139 DEBUG(0,("read_block: not a vald registry file ?\n" ));
140 return False;
143 bytes_read += returned;
146 return bytes_read;
149 /*******************************************************************
150 *******************************************************************/
152 static bool write_hbin_block( REGF_FILE *file, REGF_HBIN *hbin )
154 if ( !hbin->dirty )
155 return True;
157 /* write free space record if any is available */
159 if ( hbin->free_off != REGF_OFFSET_NONE ) {
160 uint32 header = 0xffffffff;
162 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) )
163 return False;
164 if ( !prs_uint32( "free_size", &hbin->ps, 0, &hbin->free_size ) )
165 return False;
166 if ( !prs_uint32( "free_header", &hbin->ps, 0, &header ) )
167 return False;
170 hbin->dirty = (write_block( file, &hbin->ps, hbin->file_off ) != -1);
172 return hbin->dirty;
175 /*******************************************************************
176 *******************************************************************/
178 static bool hbin_block_close( REGF_FILE *file, REGF_HBIN *hbin )
180 REGF_HBIN *p;
182 /* remove the block from the open list and flush it to disk */
184 for ( p=file->block_list; p && p!=hbin; p=p->next )
187 if ( p == hbin ) {
188 DLIST_REMOVE( file->block_list, hbin );
190 else
191 DEBUG(0,("hbin_block_close: block not in open list!\n"));
193 if ( !write_hbin_block( file, hbin ) )
194 return False;
196 return True;
199 /*******************************************************************
200 *******************************************************************/
202 static bool prs_regf_block( const char *desc, prs_struct *ps, int depth, REGF_FILE *file )
204 prs_debug(ps, depth, desc, "prs_regf_block");
205 depth++;
207 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)file->header, sizeof( file->header )) )
208 return False;
210 /* yes, these values are always identical so store them only once */
212 if ( !prs_uint32( "unknown1", ps, depth, &file->unknown1 ))
213 return False;
214 if ( !prs_uint32( "unknown1 (again)", ps, depth, &file->unknown1 ))
215 return False;
217 /* get the modtime */
219 if ( !prs_set_offset( ps, 0x0c ) )
220 return False;
221 if ( !smb_io_time( "modtime", &file->mtime, ps, depth ) )
222 return False;
224 /* constants */
226 if ( !prs_uint32( "unknown2", ps, depth, &file->unknown2 ))
227 return False;
228 if ( !prs_uint32( "unknown3", ps, depth, &file->unknown3 ))
229 return False;
230 if ( !prs_uint32( "unknown4", ps, depth, &file->unknown4 ))
231 return False;
232 if ( !prs_uint32( "unknown5", ps, depth, &file->unknown5 ))
233 return False;
235 /* get file offsets */
237 if ( !prs_set_offset( ps, 0x24 ) )
238 return False;
239 if ( !prs_uint32( "data_offset", ps, depth, &file->data_offset ))
240 return False;
241 if ( !prs_uint32( "last_block", ps, depth, &file->last_block ))
242 return False;
244 /* one more constant */
246 if ( !prs_uint32( "unknown6", ps, depth, &file->unknown6 ))
247 return False;
249 /* get the checksum */
251 if ( !prs_set_offset( ps, 0x01fc ) )
252 return False;
253 if ( !prs_uint32( "checksum", ps, depth, &file->checksum ))
254 return False;
256 return True;
259 /*******************************************************************
260 *******************************************************************/
262 static bool prs_hbin_block( const char *desc, prs_struct *ps, int depth, REGF_HBIN *hbin )
264 uint32 block_size2;
266 prs_debug(ps, depth, desc, "prs_regf_block");
267 depth++;
269 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)hbin->header, sizeof( hbin->header )) )
270 return False;
272 if ( !prs_uint32( "first_hbin_off", ps, depth, &hbin->first_hbin_off ))
273 return False;
275 /* The dosreg.cpp comments say that the block size is at 0x1c.
276 According to a WINXP NTUSER.dat file, this is wrong. The block_size
277 is at 0x08 */
279 if ( !prs_uint32( "block_size", ps, depth, &hbin->block_size ))
280 return False;
282 block_size2 = hbin->block_size;
283 prs_set_offset( ps, 0x1c );
284 if ( !prs_uint32( "block_size2", ps, depth, &block_size2 ))
285 return False;
287 if ( MARSHALLING(ps) )
288 hbin->dirty = True;
291 return True;
294 /*******************************************************************
295 *******************************************************************/
297 static bool prs_nk_rec( const char *desc, prs_struct *ps, int depth, REGF_NK_REC *nk )
299 uint16 class_length, name_length;
300 uint32 start;
301 uint32 data_size, start_off, end_off;
302 uint32 unknown_off = REGF_OFFSET_NONE;
304 nk->hbin_off = prs_offset( ps );
305 start = nk->hbin_off;
307 prs_debug(ps, depth, desc, "prs_nk_rec");
308 depth++;
310 /* back up and get the data_size */
312 if ( !prs_set_offset( ps, prs_offset(ps)-sizeof(uint32)) )
313 return False;
314 start_off = prs_offset( ps );
315 if ( !prs_uint32( "rec_size", ps, depth, &nk->rec_size ))
316 return False;
318 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)nk->header, sizeof( nk->header )) )
319 return False;
321 if ( !prs_uint16( "key_type", ps, depth, &nk->key_type ))
322 return False;
323 if ( !smb_io_time( "mtime", &nk->mtime, ps, depth ))
324 return False;
326 if ( !prs_set_offset( ps, start+0x0010 ) )
327 return False;
328 if ( !prs_uint32( "parent_off", ps, depth, &nk->parent_off ))
329 return False;
330 if ( !prs_uint32( "num_subkeys", ps, depth, &nk->num_subkeys ))
331 return False;
333 if ( !prs_set_offset( ps, start+0x001c ) )
334 return False;
335 if ( !prs_uint32( "subkeys_off", ps, depth, &nk->subkeys_off ))
336 return False;
337 if ( !prs_uint32( "unknown_off", ps, depth, &unknown_off) )
338 return False;
340 if ( !prs_set_offset( ps, start+0x0024 ) )
341 return False;
342 if ( !prs_uint32( "num_values", ps, depth, &nk->num_values ))
343 return False;
344 if ( !prs_uint32( "values_off", ps, depth, &nk->values_off ))
345 return False;
346 if ( !prs_uint32( "sk_off", ps, depth, &nk->sk_off ))
347 return False;
348 if ( !prs_uint32( "classname_off", ps, depth, &nk->classname_off ))
349 return False;
351 if ( !prs_uint32( "max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname))
352 return False;
353 if ( !prs_uint32( "max_bytes_subkeyclassname", ps, depth, &nk->max_bytes_subkeyclassname))
354 return False;
355 if ( !prs_uint32( "max_bytes_valuename", ps, depth, &nk->max_bytes_valuename))
356 return False;
357 if ( !prs_uint32( "max_bytes_value", ps, depth, &nk->max_bytes_value))
358 return False;
359 if ( !prs_uint32( "unknown index", ps, depth, &nk->unk_index))
360 return False;
362 name_length = nk->keyname ? strlen(nk->keyname) : 0 ;
363 class_length = nk->classname ? strlen(nk->classname) : 0 ;
364 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
365 return False;
366 if ( !prs_uint16( "class_length", ps, depth, &class_length ))
367 return False;
369 if ( class_length ) {
373 if ( name_length ) {
374 if ( UNMARSHALLING(ps) ) {
375 if ( !(nk->keyname = PRS_ALLOC_MEM( ps, char, name_length+1 )) )
376 return False;
379 if ( !prs_uint8s( True, "name", ps, depth, (uint8*)nk->keyname, name_length) )
380 return False;
382 if ( UNMARSHALLING(ps) )
383 nk->keyname[name_length] = '\0';
386 end_off = prs_offset( ps );
388 /* data_size must be divisible by 8 and large enough to hold the original record */
390 data_size = ((start_off - end_off) & 0xfffffff8 );
391 if ( data_size > nk->rec_size )
392 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size));
394 if ( MARSHALLING(ps) )
395 nk->hbin->dirty = True;
397 return True;
400 /*******************************************************************
401 *******************************************************************/
403 static uint32 regf_block_checksum( prs_struct *ps )
405 char *buffer = prs_data_p( ps );
406 uint32 checksum, x;
407 int i;
409 /* XOR of all bytes 0x0000 - 0x01FB */
411 checksum = x = 0;
413 for ( i=0; i<0x01FB; i+=4 ) {
414 x = IVAL(buffer, i );
415 checksum ^= x;
418 return checksum;
421 /*******************************************************************
422 *******************************************************************/
424 static bool read_regf_block( REGF_FILE *file )
426 prs_struct ps;
427 uint32 checksum;
429 /* grab the first block from the file */
431 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) == -1 )
432 return False;
434 /* parse the block and verify the checksum */
436 if ( !prs_regf_block( "regf_header", &ps, 0, file ) )
437 return False;
439 checksum = regf_block_checksum( &ps );
441 prs_mem_free( &ps );
443 if ( file->checksum != checksum ) {
444 DEBUG(0,("read_regf_block: invalid checksum\n" ));
445 return False;
448 return True;
451 /*******************************************************************
452 *******************************************************************/
454 static REGF_HBIN* read_hbin_block( REGF_FILE *file, off_t offset )
456 REGF_HBIN *hbin;
457 uint32 record_size, curr_off, block_size, header;
459 if ( !(hbin = TALLOC_ZERO_P(file->mem_ctx, REGF_HBIN)) )
460 return NULL;
461 hbin->file_off = offset;
462 hbin->free_off = -1;
464 if ( read_block( file, &hbin->ps, offset, 0 ) == -1 )
465 return NULL;
467 if ( !prs_hbin_block( "hbin", &hbin->ps, 0, hbin ) )
468 return NULL;
470 /* this should be the same thing as hbin->block_size but just in case */
472 block_size = prs_data_size( &hbin->ps );
474 /* Find the available free space offset. Always at the end,
475 so walk the record list and stop when you get to the end.
476 The end is defined by a record header of 0xffffffff. The
477 previous 4 bytes contains the amount of free space remaining
478 in the hbin block. */
480 /* remember that the record_size is in the 4 bytes preceeding the record itself */
482 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE-sizeof(uint32) ) )
483 return False;
485 record_size = 0;
486 header = 0;
487 curr_off = prs_offset( &hbin->ps );
488 while ( header != 0xffffffff ) {
489 /* not done yet so reset the current offset to the
490 next record_size field */
492 curr_off = curr_off+record_size;
494 /* for some reason the record_size of the last record in
495 an hbin block can extend past the end of the block
496 even though the record fits within the remaining
497 space....aaarrrgggghhhhhh */
499 if ( curr_off >= block_size ) {
500 record_size = -1;
501 curr_off = -1;
502 break;
505 if ( !prs_set_offset( &hbin->ps, curr_off) )
506 return False;
508 if ( !prs_uint32( "rec_size", &hbin->ps, 0, &record_size ) )
509 return False;
510 if ( !prs_uint32( "header", &hbin->ps, 0, &header ) )
511 return False;
513 SMB_ASSERT( record_size != 0 );
515 if ( record_size & 0x80000000 ) {
516 /* absolute_value(record_size) */
517 record_size = (record_size ^ 0xffffffff) + 1;
521 /* save the free space offset */
523 if ( header == 0xffffffff ) {
525 /* account for the fact that the curr_off is 4 bytes behind the actual
526 record header */
528 hbin->free_off = curr_off + sizeof(uint32);
529 hbin->free_size = record_size;
532 DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin->free_off));
534 if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE ) )
535 return False;
537 return hbin;
540 /*******************************************************************
541 Input a random offset and receive the corresponding HBIN
542 block for it
543 *******************************************************************/
545 static bool hbin_contains_offset( REGF_HBIN *hbin, uint32 offset )
547 if ( !hbin )
548 return False;
550 if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) )
551 return True;
553 return False;
556 /*******************************************************************
557 Input a random offset and receive the corresponding HBIN
558 block for it
559 *******************************************************************/
561 static REGF_HBIN* lookup_hbin_block( REGF_FILE *file, uint32 offset )
563 REGF_HBIN *hbin = NULL;
564 uint32 block_off;
566 /* start with the open list */
568 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
569 DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%lx]\n", hbin->file_off, (unsigned long)hbin ));
570 if ( hbin_contains_offset( hbin, offset ) )
571 return hbin;
574 if ( !hbin ) {
575 /* start at the beginning */
577 block_off = REGF_BLOCKSIZE;
578 do {
579 /* cleanup before the next round */
580 if ( hbin )
581 prs_mem_free( &hbin->ps );
583 hbin = read_hbin_block( file, block_off );
585 if ( hbin )
586 block_off = hbin->file_off + hbin->block_size;
588 } while ( hbin && !hbin_contains_offset( hbin, offset ) );
591 if ( hbin )
592 DLIST_ADD( file->block_list, hbin );
594 return hbin;
597 /*******************************************************************
598 *******************************************************************/
600 static bool prs_hash_rec( const char *desc, prs_struct *ps, int depth, REGF_HASH_REC *hash )
602 prs_debug(ps, depth, desc, "prs_hash_rec");
603 depth++;
605 if ( !prs_uint32( "nk_off", ps, depth, &hash->nk_off ))
606 return False;
607 if ( !prs_uint8s( True, "keycheck", ps, depth, hash->keycheck, sizeof( hash->keycheck )) )
608 return False;
610 return True;
613 /*******************************************************************
614 *******************************************************************/
616 static bool hbin_prs_lf_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk )
618 int i;
619 REGF_LF_REC *lf = &nk->subkeys;
620 uint32 data_size, start_off, end_off;
622 prs_debug(&hbin->ps, depth, desc, "prs_lf_records");
623 depth++;
625 /* check if we have anything to do first */
627 if ( nk->num_subkeys == 0 )
628 return True;
630 /* move to the LF record */
632 if ( !prs_set_offset( &hbin->ps, nk->subkeys_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
633 return False;
635 /* backup and get the data_size */
637 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
638 return False;
639 start_off = prs_offset( &hbin->ps );
640 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &lf->rec_size ))
641 return False;
643 if ( !prs_uint8s( True, "header", &hbin->ps, depth, (uint8*)lf->header, sizeof( lf->header )) )
644 return False;
646 if ( !prs_uint16( "num_keys", &hbin->ps, depth, &lf->num_keys))
647 return False;
649 if ( UNMARSHALLING(&hbin->ps) ) {
650 if (lf->num_keys) {
651 if ( !(lf->hashes = PRS_ALLOC_MEM( &hbin->ps, REGF_HASH_REC, lf->num_keys )) )
652 return False;
653 } else {
654 lf->hashes = NULL;
658 for ( i=0; i<lf->num_keys; i++ ) {
659 if ( !prs_hash_rec( "hash_rec", &hbin->ps, depth, &lf->hashes[i] ) )
660 return False;
663 end_off = prs_offset( &hbin->ps );
665 /* data_size must be divisible by 8 and large enough to hold the original record */
667 data_size = ((start_off - end_off) & 0xfffffff8 );
668 if ( data_size > lf->rec_size )
669 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->rec_size));
671 if ( MARSHALLING(&hbin->ps) )
672 hbin->dirty = True;
674 return True;
677 /*******************************************************************
678 *******************************************************************/
680 static bool hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_SK_REC *sk )
682 prs_struct *ps = &hbin->ps;
683 uint16 tag = 0xFFFF;
684 uint32 data_size, start_off, end_off;
687 prs_debug(ps, depth, desc, "hbin_prs_sk_rec");
688 depth++;
690 if ( !prs_set_offset( &hbin->ps, sk->sk_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
691 return False;
693 /* backup and get the data_size */
695 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
696 return False;
697 start_off = prs_offset( &hbin->ps );
698 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &sk->rec_size ))
699 return False;
701 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)sk->header, sizeof( sk->header )) )
702 return False;
703 if ( !prs_uint16( "tag", ps, depth, &tag))
704 return False;
706 if ( !prs_uint32( "prev_sk_off", ps, depth, &sk->prev_sk_off))
707 return False;
708 if ( !prs_uint32( "next_sk_off", ps, depth, &sk->next_sk_off))
709 return False;
710 if ( !prs_uint32( "ref_count", ps, depth, &sk->ref_count))
711 return False;
712 if ( !prs_uint32( "size", ps, depth, &sk->size))
713 return False;
715 if ( !sec_io_desc( "sec_desc", &sk->sec_desc, ps, depth ))
716 return False;
718 end_off = prs_offset( &hbin->ps );
720 /* data_size must be divisible by 8 and large enough to hold the original record */
722 data_size = ((start_off - end_off) & 0xfffffff8 );
723 if ( data_size > sk->rec_size )
724 DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->rec_size));
726 if ( MARSHALLING(&hbin->ps) )
727 hbin->dirty = True;
729 return True;
732 /*******************************************************************
733 *******************************************************************/
735 static bool hbin_prs_vk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_VK_REC *vk, REGF_FILE *file )
737 uint32 offset;
738 uint16 name_length;
739 prs_struct *ps = &hbin->ps;
740 uint32 data_size, start_off, end_off;
742 prs_debug(ps, depth, desc, "prs_vk_rec");
743 depth++;
745 /* backup and get the data_size */
747 if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) )
748 return False;
749 start_off = prs_offset( &hbin->ps );
750 if ( !prs_uint32( "rec_size", &hbin->ps, depth, &vk->rec_size ))
751 return False;
753 if ( !prs_uint8s( True, "header", ps, depth, (uint8*)vk->header, sizeof( vk->header )) )
754 return False;
756 if ( MARSHALLING(&hbin->ps) )
757 name_length = strlen(vk->valuename);
759 if ( !prs_uint16( "name_length", ps, depth, &name_length ))
760 return False;
761 if ( !prs_uint32( "data_size", ps, depth, &vk->data_size ))
762 return False;
763 if ( !prs_uint32( "data_off", ps, depth, &vk->data_off ))
764 return False;
765 if ( !prs_uint32( "type", ps, depth, &vk->type))
766 return False;
767 if ( !prs_uint16( "flag", ps, depth, &vk->flag))
768 return False;
770 offset = prs_offset( ps );
771 offset += 2; /* skip 2 bytes */
772 prs_set_offset( ps, offset );
774 /* get the name */
776 if ( vk->flag&VK_FLAG_NAME_PRESENT ) {
778 if ( UNMARSHALLING(&hbin->ps) ) {
779 if ( !(vk->valuename = PRS_ALLOC_MEM( ps, char, name_length+1 )))
780 return False;
782 if ( !prs_uint8s( True, "name", ps, depth, (uint8*)vk->valuename, name_length ) )
783 return False;
786 end_off = prs_offset( &hbin->ps );
788 /* get the data if necessary */
790 if ( vk->data_size != 0 ) {
791 bool charmode = False;
793 if ( (vk->type == REG_SZ) || (vk->type == REG_MULTI_SZ) )
794 charmode = True;
796 /* the data is stored in the offset if the size <= 4 */
798 if ( !(vk->data_size & VK_DATA_IN_OFFSET) ) {
799 REGF_HBIN *hblock = hbin;
800 uint32 data_rec_size;
802 if ( UNMARSHALLING(&hbin->ps) ) {
803 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, vk->data_size) ) )
804 return False;
807 /* this data can be in another hbin */
808 if ( !hbin_contains_offset( hbin, vk->data_off ) ) {
809 if ( !(hblock = lookup_hbin_block( file, vk->data_off )) )
810 return False;
812 if ( !(prs_set_offset( &hblock->ps, (vk->data_off+HBIN_HDR_SIZE-hblock->first_hbin_off)-sizeof(uint32) )) )
813 return False;
815 if ( MARSHALLING(&hblock->ps) ) {
816 data_rec_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
817 data_rec_size = ( data_rec_size - 1 ) ^ 0xFFFFFFFF;
819 if ( !prs_uint32( "data_rec_size", &hblock->ps, depth, &data_rec_size ))
820 return False;
821 if ( !prs_uint8s( charmode, "data", &hblock->ps, depth, vk->data, vk->data_size) )
822 return False;
824 if ( MARSHALLING(&hblock->ps) )
825 hblock->dirty = True;
827 else {
828 if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, 4 ) ) )
829 return False;
830 SIVAL( vk->data, 0, vk->data_off );
835 /* data_size must be divisible by 8 and large enough to hold the original record */
837 data_size = ((start_off - end_off ) & 0xfffffff8 );
838 if ( data_size != vk->rec_size )
839 DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size, vk->rec_size));
841 if ( MARSHALLING(&hbin->ps) )
842 hbin->dirty = True;
844 return True;
847 /*******************************************************************
848 read a VK record which is contained in the HBIN block stored
849 in the prs_struct *ps.
850 *******************************************************************/
852 static bool hbin_prs_vk_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk, REGF_FILE *file )
854 int i;
855 uint32 record_size;
857 prs_debug(&hbin->ps, depth, desc, "prs_vk_records");
858 depth++;
860 /* check if we have anything to do first */
862 if ( nk->num_values == 0 )
863 return True;
865 if ( UNMARSHALLING(&hbin->ps) ) {
866 if ( !(nk->values = PRS_ALLOC_MEM( &hbin->ps, REGF_VK_REC, nk->num_values ) ) )
867 return False;
870 /* convert the offset to something relative to this HBIN block */
872 if ( !prs_set_offset( &hbin->ps, nk->values_off+HBIN_HDR_SIZE-hbin->first_hbin_off-sizeof(uint32)) )
873 return False;
875 if ( MARSHALLING( &hbin->ps) ) {
876 record_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
877 record_size = (record_size - 1) ^ 0xFFFFFFFF;
880 if ( !prs_uint32( "record_size", &hbin->ps, depth, &record_size ) )
881 return False;
883 for ( i=0; i<nk->num_values; i++ ) {
884 if ( !prs_uint32( "vk_off", &hbin->ps, depth, &nk->values[i].rec_off ) )
885 return False;
888 for ( i=0; i<nk->num_values; i++ ) {
889 REGF_HBIN *sub_hbin = hbin;
890 uint32 new_offset;
892 if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) ) {
893 sub_hbin = lookup_hbin_block( file, nk->values[i].rec_off );
894 if ( !sub_hbin ) {
895 DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n",
896 nk->values[i].hbin_off));
897 return False;
901 new_offset = nk->values[i].rec_off + HBIN_HDR_SIZE - sub_hbin->first_hbin_off;
902 if ( !prs_set_offset( &sub_hbin->ps, new_offset ) )
903 return False;
904 if ( !hbin_prs_vk_rec( "vk_rec", sub_hbin, depth, &nk->values[i], file ) )
905 return False;
908 if ( MARSHALLING(&hbin->ps) )
909 hbin->dirty = True;
912 return True;
916 /*******************************************************************
917 *******************************************************************/
919 static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32 offset )
921 REGF_SK_REC *p_sk;
923 for ( p_sk=file->sec_desc_list; p_sk; p_sk=p_sk->next ) {
924 if ( p_sk->sk_off == offset )
925 return p_sk;
928 return NULL;
931 /*******************************************************************
932 *******************************************************************/
934 static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, SEC_DESC *sd )
936 REGF_SK_REC *p;
938 for ( p=file->sec_desc_list; p; p=p->next ) {
939 if ( sec_desc_equal( p->sec_desc, sd ) )
940 return p;
943 /* failure */
945 return NULL;
948 /*******************************************************************
949 *******************************************************************/
951 static bool hbin_prs_key( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk )
953 int depth = 0;
954 REGF_HBIN *sub_hbin;
956 prs_debug(&hbin->ps, depth, "", "fetch_key");
957 depth++;
959 /* get the initial nk record */
961 if ( !prs_nk_rec( "nk_rec", &hbin->ps, depth, nk ))
962 return False;
964 /* fill in values */
966 if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) {
967 sub_hbin = hbin;
968 if ( !hbin_contains_offset( hbin, nk->values_off ) ) {
969 sub_hbin = lookup_hbin_block( file, nk->values_off );
970 if ( !sub_hbin ) {
971 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n",
972 nk->values_off));
973 return False;
977 if ( !hbin_prs_vk_records( "vk_rec", sub_hbin, depth, nk, file ))
978 return False;
981 /* now get subkeys */
983 if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) {
984 sub_hbin = hbin;
985 if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) {
986 sub_hbin = lookup_hbin_block( file, nk->subkeys_off );
987 if ( !sub_hbin ) {
988 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n",
989 nk->subkeys_off));
990 return False;
994 if ( !hbin_prs_lf_records( "lf_rec", sub_hbin, depth, nk ))
995 return False;
998 /* get the to the security descriptor. First look if we have already parsed it */
1000 if ( (nk->sk_off!=REGF_OFFSET_NONE) && !( nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )) ) {
1002 sub_hbin = hbin;
1003 if ( !hbin_contains_offset( hbin, nk->sk_off ) ) {
1004 sub_hbin = lookup_hbin_block( file, nk->sk_off );
1005 if ( !sub_hbin ) {
1006 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_offset [0x%x]\n",
1007 nk->subkeys_off));
1008 return False;
1012 if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) )
1013 return False;
1014 nk->sec_desc->sk_off = nk->sk_off;
1015 if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin, depth, nk->sec_desc ))
1016 return False;
1018 /* add to the list of security descriptors (ref_count has been read from the files) */
1020 nk->sec_desc->sk_off = nk->sk_off;
1021 DLIST_ADD( file->sec_desc_list, nk->sec_desc );
1024 return True;
1027 /*******************************************************************
1028 *******************************************************************/
1030 static bool next_record( REGF_HBIN *hbin, const char *hdr, bool *eob )
1032 uint8 header[REC_HDR_SIZE];
1033 uint32 record_size;
1034 uint32 curr_off, block_size;
1035 bool found = False;
1036 prs_struct *ps = &hbin->ps;
1038 curr_off = prs_offset( ps );
1039 if ( curr_off == 0 )
1040 prs_set_offset( ps, HBIN_HEADER_REC_SIZE );
1042 /* assume that the current offset is at the record header
1043 and we need to backup to read the record size */
1045 curr_off -= sizeof(uint32);
1047 block_size = prs_data_size( ps );
1048 record_size = 0;
1049 memset( header, 0x0, sizeof(uint8)*REC_HDR_SIZE );
1050 while ( !found ) {
1052 curr_off = curr_off+record_size;
1053 if ( curr_off >= block_size )
1054 break;
1056 if ( !prs_set_offset( &hbin->ps, curr_off) )
1057 return False;
1059 if ( !prs_uint32( "record_size", ps, 0, &record_size ) )
1060 return False;
1061 if ( !prs_uint8s( True, "header", ps, 0, header, REC_HDR_SIZE ) )
1062 return False;
1064 if ( record_size & 0x80000000 ) {
1065 /* absolute_value(record_size) */
1066 record_size = (record_size ^ 0xffffffff) + 1;
1069 if ( memcmp( header, hdr, REC_HDR_SIZE ) == 0 ) {
1070 found = True;
1071 curr_off += sizeof(uint32);
1075 /* mark prs_struct as done ( at end ) if no more SK records */
1076 /* mark end-of-block as True */
1078 if ( !found ) {
1079 prs_set_offset( &hbin->ps, prs_data_size(&hbin->ps) );
1080 *eob = True;
1081 return False;
1084 if ( !prs_set_offset( ps, curr_off ) )
1085 return False;
1087 return True;
1090 /*******************************************************************
1091 *******************************************************************/
1093 static bool next_nk_record( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk, bool *eob )
1095 if ( next_record( hbin, "nk", eob ) && hbin_prs_key( file, hbin, nk ) )
1096 return True;
1098 return False;
1101 /*******************************************************************
1102 Intialize the newly created REGF_BLOCK in *file and write the
1103 block header to disk
1104 *******************************************************************/
1106 static bool init_regf_block( REGF_FILE *file )
1108 prs_struct ps;
1109 bool result = True;
1111 if ( !prs_init( &ps, REGF_BLOCKSIZE, file->mem_ctx, MARSHALL ) )
1112 return False;
1114 memcpy( file->header, "regf", REGF_HDR_SIZE );
1115 file->data_offset = 0x20;
1116 file->last_block = 0x1000;
1118 /* set mod time */
1120 unix_to_nt_time( &file->mtime, time(NULL) );
1122 /* hard coded values...no diea what these are ... maybe in time */
1124 file->unknown1 = 0x2;
1125 file->unknown2 = 0x1;
1126 file->unknown3 = 0x3;
1127 file->unknown4 = 0x0;
1128 file->unknown5 = 0x1;
1129 file->unknown6 = 0x1;
1131 /* write header to the buffer */
1133 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1134 result = False;
1135 goto out;
1138 /* calculate the checksum, re-marshall data (to include the checksum)
1139 and write to disk */
1141 file->checksum = regf_block_checksum( &ps );
1142 prs_set_offset( &ps, 0 );
1143 if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) {
1144 result = False;
1145 goto out;
1148 if ( write_block( file, &ps, 0 ) == -1 ) {
1149 DEBUG(0,("init_regf_block: Failed to initialize registry header block!\n"));
1150 result = False;
1151 goto out;
1154 out:
1155 prs_mem_free( &ps );
1157 return result;
1159 /*******************************************************************
1160 Open the registry file and then read in the REGF block to get the
1161 first hbin offset.
1162 *******************************************************************/
1164 REGF_FILE* regfio_open( const char *filename, int flags, int mode )
1166 REGF_FILE *rb;
1168 if ( !(rb = SMB_MALLOC_P(REGF_FILE)) ) {
1169 DEBUG(0,("ERROR allocating memory\n"));
1170 return NULL;
1172 ZERO_STRUCTP( rb );
1173 rb->fd = -1;
1175 if ( !(rb->mem_ctx = talloc_init( "read_regf_block" )) ) {
1176 regfio_close( rb );
1177 return NULL;
1180 rb->open_flags = flags;
1182 /* open and existing file */
1184 if ( (rb->fd = open(filename, flags, mode)) == -1 ) {
1185 DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename, strerror(errno)));
1186 regfio_close( rb );
1187 return NULL;
1190 /* check if we are creating a new file or overwriting an existing one */
1192 if ( flags & (O_CREAT|O_TRUNC) ) {
1193 if ( !init_regf_block( rb ) ) {
1194 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1195 regfio_close( rb );
1196 return NULL;
1199 /* success */
1200 return rb;
1203 /* read in an existing file */
1205 if ( !read_regf_block( rb ) ) {
1206 DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
1207 regfio_close( rb );
1208 return NULL;
1211 /* success */
1213 return rb;
1216 /*******************************************************************
1217 *******************************************************************/
1219 static void regfio_mem_free( REGF_FILE *file )
1221 /* free any talloc()'d memory */
1223 if ( file && file->mem_ctx )
1224 talloc_destroy( file->mem_ctx );
1227 /*******************************************************************
1228 *******************************************************************/
1230 int regfio_close( REGF_FILE *file )
1232 int fd;
1234 /* cleanup for a file opened for write */
1236 if ((file->fd != -1) && (file->open_flags & (O_WRONLY|O_RDWR))) {
1237 prs_struct ps;
1238 REGF_SK_REC *sk;
1240 /* write of sd list */
1242 for ( sk=file->sec_desc_list; sk; sk=sk->next ) {
1243 hbin_prs_sk_rec( "sk_rec", sk->hbin, 0, sk );
1246 /* flush any dirty blocks */
1248 while ( file->block_list ) {
1249 hbin_block_close( file, file->block_list );
1252 ZERO_STRUCT( ps );
1254 unix_to_nt_time( &file->mtime, time(NULL) );
1256 if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) != -1 ) {
1257 /* now use for writing */
1258 prs_switch_type( &ps, MARSHALL );
1260 /* stream the block once, generate the checksum,
1261 and stream it again */
1262 prs_set_offset( &ps, 0 );
1263 prs_regf_block( "regf_blocK", &ps, 0, file );
1264 file->checksum = regf_block_checksum( &ps );
1265 prs_set_offset( &ps, 0 );
1266 prs_regf_block( "regf_blocK", &ps, 0, file );
1268 /* now we are ready to write it to disk */
1269 if ( write_block( file, &ps, 0 ) == -1 )
1270 DEBUG(0,("regfio_close: failed to update the regf header block!\n"));
1273 prs_mem_free( &ps );
1276 regfio_mem_free( file );
1278 /* nothing tdo do if there is no open file */
1280 if (file->fd == -1)
1281 return 0;
1283 fd = file->fd;
1284 file->fd = -1;
1285 SAFE_FREE( file );
1287 return close( fd );
1290 /*******************************************************************
1291 *******************************************************************/
1293 static void regfio_flush( REGF_FILE *file )
1295 REGF_HBIN *hbin;
1297 for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
1298 write_hbin_block( file, hbin );
1302 /*******************************************************************
1303 There should be only *one* root key in the registry file based
1304 on my experience. --jerry
1305 *******************************************************************/
1307 REGF_NK_REC* regfio_rootkey( REGF_FILE *file )
1309 REGF_NK_REC *nk;
1310 REGF_HBIN *hbin;
1311 uint32 offset = REGF_BLOCKSIZE;
1312 bool found = False;
1313 bool eob;
1315 if ( !file )
1316 return NULL;
1318 if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) ) {
1319 DEBUG(0,("regfio_rootkey: talloc() failed!\n"));
1320 return NULL;
1323 /* scan through the file on HBIN block at a time looking
1324 for an NK record with a type == 0x002c.
1325 Normally this is the first nk record in the first hbin
1326 block (but I'm not assuming that for now) */
1328 while ( (hbin = read_hbin_block( file, offset )) ) {
1329 eob = False;
1331 while ( !eob) {
1332 if ( next_nk_record( file, hbin, nk, &eob ) ) {
1333 if ( nk->key_type == NK_TYPE_ROOTKEY ) {
1334 found = True;
1335 break;
1338 prs_mem_free( &hbin->ps );
1341 if ( found )
1342 break;
1344 offset += hbin->block_size;
1347 if ( !found ) {
1348 DEBUG(0,("regfio_rootkey: corrupt registry file ? No root key record located\n"));
1349 return NULL;
1352 DLIST_ADD( file->block_list, hbin );
1354 return nk;
1357 /*******************************************************************
1358 This acts as an interator over the subkeys defined for a given
1359 NK record. Remember that offsets are from the *first* HBIN block.
1360 *******************************************************************/
1362 REGF_NK_REC* regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk )
1364 REGF_NK_REC *subkey;
1365 REGF_HBIN *hbin;
1366 uint32 nk_offset;
1368 /* see if there is anything left to report */
1370 if ( !nk || (nk->subkeys_off==REGF_OFFSET_NONE) || (nk->subkey_index >= nk->num_subkeys) )
1371 return NULL;
1373 /* find the HBIN block which should contain the nk record */
1375 if ( !(hbin = lookup_hbin_block( file, nk->subkeys.hashes[nk->subkey_index].nk_off )) ) {
1376 DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n",
1377 nk->subkeys.hashes[nk->subkey_index].nk_off));
1378 return NULL;
1381 nk_offset = nk->subkeys.hashes[nk->subkey_index].nk_off;
1382 if ( !prs_set_offset( &hbin->ps, (HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off) ) )
1383 return NULL;
1385 nk->subkey_index++;
1386 if ( !(subkey = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) )
1387 return NULL;
1389 if ( !hbin_prs_key( file, hbin, subkey ) )
1390 return NULL;
1392 return subkey;
1396 /*******************************************************************
1397 *******************************************************************/
1399 static REGF_HBIN* regf_hbin_allocate( REGF_FILE *file, uint32 block_size )
1401 REGF_HBIN *hbin;
1402 SMB_STRUCT_STAT sbuf;
1404 if ( !(hbin = TALLOC_ZERO_P( file->mem_ctx, REGF_HBIN )) )
1405 return NULL;
1407 memcpy( hbin->header, "hbin", sizeof(HBIN_HDR_SIZE) );
1410 if ( sys_fstat( file->fd, &sbuf ) ) {
1411 DEBUG(0,("regf_hbin_allocate: stat() failed! (%s)\n", strerror(errno)));
1412 return NULL;
1415 hbin->file_off = sbuf.st_size;
1417 hbin->free_off = HBIN_HEADER_REC_SIZE;
1418 hbin->free_size = block_size - hbin->free_off + sizeof(uint32);;
1420 hbin->block_size = block_size;
1421 hbin->first_hbin_off = hbin->file_off - REGF_BLOCKSIZE;
1423 if ( !prs_init( &hbin->ps, block_size, file->mem_ctx, MARSHALL ) )
1424 return NULL;
1426 if ( !prs_hbin_block( "new_hbin", &hbin->ps, 0, hbin ) )
1427 return NULL;
1429 if ( !write_hbin_block( file, hbin ) )
1430 return NULL;
1432 file->last_block = hbin->file_off;
1434 return hbin;
1437 /*******************************************************************
1438 *******************************************************************/
1440 static void update_free_space( REGF_HBIN *hbin, uint32 size_used )
1442 hbin->free_off += size_used;
1443 hbin->free_size -= size_used;
1445 if ( hbin->free_off >= hbin->block_size ) {
1446 hbin->free_off = REGF_OFFSET_NONE;
1449 return;
1452 /*******************************************************************
1453 *******************************************************************/
1455 static REGF_HBIN* find_free_space( REGF_FILE *file, uint32 size )
1457 REGF_HBIN *hbin, *p_hbin;
1458 uint32 block_off;
1459 bool cached;
1461 /* check open block list */
1463 for ( hbin=file->block_list; hbin!=NULL; hbin=hbin->next ) {
1464 /* only check blocks that actually have available space */
1466 if ( hbin->free_off == REGF_OFFSET_NONE )
1467 continue;
1469 /* check for a large enough available chunk */
1471 if ( (hbin->block_size - hbin->free_off) >= size ) {
1472 DLIST_PROMOTE( file->block_list, hbin );
1473 goto done;
1477 /* parse the file until we find a block with
1478 enough free space; save the last non-filled hbin */
1480 block_off = REGF_BLOCKSIZE;
1481 do {
1482 /* cleanup before the next round */
1483 cached = False;
1484 if ( hbin )
1485 prs_mem_free( &hbin->ps );
1487 hbin = read_hbin_block( file, block_off );
1489 if ( hbin ) {
1491 /* make sure that we don't already have this block in memory */
1493 for ( p_hbin=file->block_list; p_hbin!=NULL; p_hbin=p_hbin->next ) {
1494 if ( p_hbin->file_off == hbin->file_off ) {
1495 cached = True;
1496 break;
1500 block_off = hbin->file_off + hbin->block_size;
1502 if ( cached ) {
1503 prs_mem_free( &hbin->ps );
1504 hbin = NULL;
1505 continue;
1508 /* if (cached block or (new block and not enough free space)) then continue looping */
1509 } while ( cached || (hbin && (hbin->free_size < size)) );
1511 /* no free space; allocate a new one */
1513 if ( !hbin ) {
1514 uint32 alloc_size;
1516 /* allocate in multiples of REGF_ALLOC_BLOCK; make sure (size + hbin_header) fits */
1518 alloc_size = (((size+HBIN_HEADER_REC_SIZE) / REGF_ALLOC_BLOCK ) + 1 ) * REGF_ALLOC_BLOCK;
1520 if ( !(hbin = regf_hbin_allocate( file, alloc_size )) ) {
1521 DEBUG(0,("find_free_space: regf_hbin_allocate() failed!\n"));
1522 return NULL;
1524 DLIST_ADD( file->block_list, hbin );
1527 done:
1528 /* set the offset to be ready to write */
1530 if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) )
1531 return NULL;
1533 /* write the record size as a placeholder for now, it should be
1534 probably updated by the caller once it all of the data necessary
1535 for the record */
1537 if ( !prs_uint32("allocated_size", &hbin->ps, 0, &size) )
1538 return False;
1540 update_free_space( hbin, size );
1542 return hbin;
1545 /*******************************************************************
1546 *******************************************************************/
1548 static uint32 sk_record_data_size( SEC_DESC * sd )
1550 uint32 size, size_mod8;
1552 size_mod8 = 0;
1554 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1556 size = sizeof(uint32)*5 + ndr_size_security_descriptor(sd, 0) + sizeof(uint32);
1558 /* multiple of 8 */
1559 size_mod8 = size & 0xfffffff8;
1560 if ( size_mod8 < size )
1561 size_mod8 += 8;
1563 return size_mod8;
1566 /*******************************************************************
1567 *******************************************************************/
1569 static uint32 vk_record_data_size( REGF_VK_REC *vk )
1571 uint32 size, size_mod8;
1573 size_mod8 = 0;
1575 /* the record size is sizeof(hdr) + name + static members + data_size_field */
1577 size = REC_HDR_SIZE + (sizeof(uint16)*3) + (sizeof(uint32)*3) + sizeof(uint32);
1579 if ( vk->valuename )
1580 size += strlen(vk->valuename);
1582 /* multiple of 8 */
1583 size_mod8 = size & 0xfffffff8;
1584 if ( size_mod8 < size )
1585 size_mod8 += 8;
1587 return size_mod8;
1590 /*******************************************************************
1591 *******************************************************************/
1593 static uint32 lf_record_data_size( uint32 num_keys )
1595 uint32 size, size_mod8;
1597 size_mod8 = 0;
1599 /* the record size is sizeof(hdr) + num_keys + sizeof of hash_array + data_size_uint32 */
1601 size = REC_HDR_SIZE + sizeof(uint16) + (sizeof(REGF_HASH_REC) * num_keys) + sizeof(uint32);
1603 /* multiple of 8 */
1604 size_mod8 = size & 0xfffffff8;
1605 if ( size_mod8 < size )
1606 size_mod8 += 8;
1608 return size_mod8;
1611 /*******************************************************************
1612 *******************************************************************/
1614 static uint32 nk_record_data_size( REGF_NK_REC *nk )
1616 uint32 size, size_mod8;
1618 size_mod8 = 0;
1620 /* the record size is static + length_of_keyname + length_of_classname + data_size_uint32 */
1622 size = 0x4c + strlen(nk->keyname) + sizeof(uint32);
1624 if ( nk->classname )
1625 size += strlen( nk->classname );
1627 /* multiple of 8 */
1628 size_mod8 = size & 0xfffffff8;
1629 if ( size_mod8 < size )
1630 size_mod8 += 8;
1632 return size_mod8;
1635 /*******************************************************************
1636 *******************************************************************/
1638 static bool create_vk_record( REGF_FILE *file, REGF_VK_REC *vk, REGISTRY_VALUE *value )
1640 char *name = regval_name(value);
1641 REGF_HBIN *data_hbin;
1643 ZERO_STRUCTP( vk );
1645 memcpy( vk->header, "vk", REC_HDR_SIZE );
1647 if ( name ) {
1648 vk->valuename = talloc_strdup( file->mem_ctx, regval_name(value) );
1649 vk->flag = VK_FLAG_NAME_PRESENT;
1652 vk->data_size = regval_size( value );
1653 vk->type = regval_type( value );
1655 if ( vk->data_size > sizeof(uint32) ) {
1656 uint32 data_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
1658 vk->data = (uint8 *)TALLOC_MEMDUP( file->mem_ctx,
1659 regval_data_p(value),
1660 vk->data_size );
1661 if (vk->data == NULL) {
1662 return False;
1665 /* go ahead and store the offset....we'll pick this hbin block back up when
1666 we stream the data */
1668 if ((data_hbin = find_free_space(file, data_size )) == NULL) {
1669 return False;
1671 vk->data_off = prs_offset( &data_hbin->ps ) + data_hbin->first_hbin_off - HBIN_HDR_SIZE;
1673 else {
1674 /* make sure we don't try to copy from a NULL value pointer */
1676 if ( vk->data_size != 0 )
1677 memcpy( &vk->data_off, regval_data_p(value), sizeof(uint32) );
1678 vk->data_size |= VK_DATA_IN_OFFSET;
1681 return True;
1684 /*******************************************************************
1685 *******************************************************************/
1687 static int hashrec_cmp( REGF_HASH_REC *h1, REGF_HASH_REC *h2 )
1689 return StrCaseCmp( h1->fullname, h2->fullname );
1692 /*******************************************************************
1693 *******************************************************************/
1695 REGF_NK_REC* regfio_write_key( REGF_FILE *file, const char *name,
1696 REGVAL_CTR *values, struct regsubkey_ctr *subkeys,
1697 SEC_DESC *sec_desc, REGF_NK_REC *parent )
1699 REGF_NK_REC *nk;
1700 REGF_HBIN *vlist_hbin = NULL;
1701 uint32 size;
1703 if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) )
1704 return NULL;
1706 memcpy( nk->header, "nk", REC_HDR_SIZE );
1708 if ( !parent )
1709 nk->key_type = NK_TYPE_ROOTKEY;
1710 else
1711 nk->key_type = NK_TYPE_NORMALKEY;
1713 /* store the parent offset (or -1 if a the root key */
1715 nk->parent_off = parent ? (parent->hbin_off + parent->hbin->file_off - REGF_BLOCKSIZE - HBIN_HDR_SIZE ) : REGF_OFFSET_NONE;
1717 /* no classname currently */
1719 nk->classname_off = REGF_OFFSET_NONE;
1720 nk->classname = NULL;
1721 nk->keyname = talloc_strdup( file->mem_ctx, name );
1723 /* current modification time */
1725 unix_to_nt_time( &nk->mtime, time(NULL) );
1727 /* allocate the record on disk */
1729 size = nk_record_data_size( nk );
1730 nk->rec_size = ( size - 1 ) ^ 0XFFFFFFFF;
1731 if ((nk->hbin = find_free_space( file, size )) == NULL) {
1732 return NULL;
1734 nk->hbin_off = prs_offset( &nk->hbin->ps );
1736 /* Update the hash record in the parent */
1738 if ( parent ) {
1739 REGF_HASH_REC *hash = &parent->subkeys.hashes[parent->subkey_index];
1741 hash->nk_off = prs_offset( &nk->hbin->ps ) + nk->hbin->first_hbin_off - HBIN_HDR_SIZE;
1742 memcpy( hash->keycheck, name, sizeof(uint32) );
1743 hash->fullname = talloc_strdup( file->mem_ctx, name );
1744 parent->subkey_index++;
1746 /* sort the list by keyname */
1748 qsort( parent->subkeys.hashes, parent->subkey_index, sizeof(REGF_HASH_REC), QSORT_CAST hashrec_cmp );
1750 if ( !hbin_prs_lf_records( "lf_rec", parent->subkeys.hbin, 0, parent ) )
1751 return False;
1754 /* write the security descriptor */
1756 nk->sk_off = REGF_OFFSET_NONE;
1757 if ( sec_desc ) {
1758 uint32 sk_size = sk_record_data_size( sec_desc );
1759 REGF_HBIN *sk_hbin;
1761 /* search for it in the existing list of sd's */
1763 if ( (nk->sec_desc = find_sk_record_by_sec_desc( file, sec_desc )) == NULL ) {
1764 /* not found so add it to the list */
1766 if (!(sk_hbin = find_free_space( file, sk_size ))) {
1767 return NULL;
1770 if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) )
1771 return NULL;
1773 /* now we have to store the security descriptor in the list and
1774 update the offsets */
1776 memcpy( nk->sec_desc->header, "sk", REC_HDR_SIZE );
1777 nk->sec_desc->hbin = sk_hbin;
1778 nk->sec_desc->hbin_off = prs_offset( &sk_hbin->ps );
1779 nk->sec_desc->sk_off = prs_offset( &sk_hbin->ps ) + sk_hbin->first_hbin_off - HBIN_HDR_SIZE;
1780 nk->sec_desc->rec_size = (sk_size-1) ^ 0xFFFFFFFF;
1782 nk->sec_desc->sec_desc = sec_desc;
1783 nk->sec_desc->ref_count = 0;
1785 /* size value must be self-inclusive */
1786 nk->sec_desc->size = ndr_size_security_descriptor(sec_desc, 0)
1787 + sizeof(uint32);
1789 DLIST_ADD_END( file->sec_desc_list, nk->sec_desc, REGF_SK_REC *);
1791 /* update the offsets for us and the previous sd in the list.
1792 if this is the first record, then just set the next and prev
1793 offsets to ourself. */
1795 if ( nk->sec_desc->prev ) {
1796 REGF_SK_REC *prev = nk->sec_desc->prev;
1798 nk->sec_desc->prev_sk_off = prev->hbin_off + prev->hbin->first_hbin_off - HBIN_HDR_SIZE;
1799 prev->next_sk_off = nk->sec_desc->sk_off;
1801 /* the end must loop around to the front */
1802 nk->sec_desc->next_sk_off = file->sec_desc_list->sk_off;
1804 /* and first must loop around to the tail */
1805 file->sec_desc_list->prev_sk_off = nk->sec_desc->sk_off;
1806 } else {
1807 nk->sec_desc->prev_sk_off = nk->sec_desc->sk_off;
1808 nk->sec_desc->next_sk_off = nk->sec_desc->sk_off;
1812 /* bump the reference count +1 */
1814 nk->sk_off = nk->sec_desc->sk_off;
1815 nk->sec_desc->ref_count++;
1818 /* write the subkeys */
1820 nk->subkeys_off = REGF_OFFSET_NONE;
1821 if ( (nk->num_subkeys = regsubkey_ctr_numkeys( subkeys )) != 0 ) {
1822 uint32 lf_size = lf_record_data_size( nk->num_subkeys );
1823 uint32 namelen;
1824 int i;
1826 if (!(nk->subkeys.hbin = find_free_space( file, lf_size ))) {
1827 return NULL;
1829 nk->subkeys.hbin_off = prs_offset( &nk->subkeys.hbin->ps );
1830 nk->subkeys.rec_size = (lf_size-1) ^ 0xFFFFFFFF;
1831 nk->subkeys_off = prs_offset( &nk->subkeys.hbin->ps ) + nk->subkeys.hbin->first_hbin_off - HBIN_HDR_SIZE;
1833 memcpy( nk->subkeys.header, "lf", REC_HDR_SIZE );
1835 nk->subkeys.num_keys = nk->num_subkeys;
1836 if (nk->subkeys.num_keys) {
1837 if ( !(nk->subkeys.hashes = TALLOC_ZERO_ARRAY( file->mem_ctx, REGF_HASH_REC, nk->subkeys.num_keys )) )
1838 return NULL;
1839 } else {
1840 nk->subkeys.hashes = NULL;
1842 nk->subkey_index = 0;
1844 /* update the max_bytes_subkey{name,classname} fields */
1845 for ( i=0; i<nk->num_subkeys; i++ ) {
1846 namelen = strlen( regsubkey_ctr_specific_key(subkeys, i) );
1847 if ( namelen*2 > nk->max_bytes_subkeyname )
1848 nk->max_bytes_subkeyname = namelen * 2;
1852 /* write the values */
1854 nk->values_off = REGF_OFFSET_NONE;
1855 if ( (nk->num_values = regval_ctr_numvals( values )) != 0 ) {
1856 uint32 vlist_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
1857 int i;
1859 if (!(vlist_hbin = find_free_space( file, vlist_size ))) {
1860 return NULL;
1862 nk->values_off = prs_offset( &vlist_hbin->ps ) + vlist_hbin->first_hbin_off - HBIN_HDR_SIZE;
1864 if (nk->num_values) {
1865 if ( !(nk->values = TALLOC_ARRAY( file->mem_ctx, REGF_VK_REC, nk->num_values )) )
1866 return NULL;
1867 } else {
1868 nk->values = NULL;
1871 /* create the vk records */
1873 for ( i=0; i<nk->num_values; i++ ) {
1874 uint32 vk_size, namelen, datalen;
1875 REGISTRY_VALUE *r;
1877 r = regval_ctr_specific_value( values, i );
1878 create_vk_record( file, &nk->values[i], r );
1879 vk_size = vk_record_data_size( &nk->values[i] );
1880 nk->values[i].hbin = find_free_space( file, vk_size );
1881 nk->values[i].hbin_off = prs_offset( &nk->values[i].hbin->ps );
1882 nk->values[i].rec_size = ( vk_size - 1 ) ^ 0xFFFFFFFF;
1883 nk->values[i].rec_off = prs_offset( &nk->values[i].hbin->ps )
1884 + nk->values[i].hbin->first_hbin_off
1885 - HBIN_HDR_SIZE;
1887 /* update the max bytes fields if necessary */
1889 namelen = strlen( regval_name(r) );
1890 if ( namelen*2 > nk->max_bytes_valuename )
1891 nk->max_bytes_valuename = namelen * 2;
1893 datalen = regval_size( r );
1894 if ( datalen > nk->max_bytes_value )
1895 nk->max_bytes_value = datalen;
1899 /* stream the records */
1901 prs_set_offset( &nk->hbin->ps, nk->hbin_off );
1902 if ( !prs_nk_rec( "nk_rec", &nk->hbin->ps, 0, nk ) )
1903 return False;
1905 if ( nk->num_values ) {
1906 if ( !hbin_prs_vk_records( "vk_records", vlist_hbin, 0, nk, file ) )
1907 return False;
1911 regfio_flush( file );
1913 return nk;