source: trunk/include/regfi.h @ 253

Last change on this file since 253 was 253, checked in by tim, 13 years ago

added preliminary interface to security descriptors in pyregfi
misc bug fixes

  • Property svn:keywords set to Id
File size: 49.9 KB
Line 
1/*
2 * Copyright (C) 2005-2010 Timothy D. Morgan
3 * Copyright (C) 2010 Michael Cohen
4 * Copyright (C) 2005 Gerald (Jerry) Carter
5 *
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; version 3 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * $Id: regfi.h 253 2011-06-13 02:27:42Z tim $
20 */
21
22/**
23 * @file
24 * Windows NT (and later) read-only registry library
25 *
26 * This library is intended for use in digital forensics investigations, but
27 * is likely useful in other applications.
28 *
29 * Branched from Samba project Subversion repository, version #6903:
30 *   http://viewcvs.samba.org/cgi-bin/viewcvs.cgi/trunk/source/include/regfio.h?rev=6903&view=auto
31 *
32 * Since then, it has been heavily rewritten, simplified, and improved.
33 */
34
35/**
36 * @mainpage Home
37 *
38 * The regfi library is a read-only NT registry library which serves as the main
39 * engine behind the reglookup tool.  It is designed with digital forensic
40 * analysis in mind, but it should also be useful in other tools which need to
41 * efficiently traverse and query registry data structures.
42 *
43 * The library is broken down into four main parts, the
44 * @ref regfiBase "Base Layer", which any code dependent on the library will
45 * likely need to rely on, as well as three main functional layers:
46 * @li @ref regfiIteratorLayer
47 * @li @ref regfiGlueLayer
48 * @li @ref regfiParseLayer
49 *
50 * Most users will find that a combination of the Base Layer and the Iterator Layer
51 * will be sufficient for accessing registry hive files.  Those who are willing
52 * to dive deep into registry data structures, for instance to recover deleted
53 * data structures or to research Windows registry behavior in detail, will
54 * find the Parse Layer to be quite useful.
55 */
56
57
58#ifndef _REGFI_H
59#define _REGFI_H
60
61#include <stdlib.h>
62#include <stdio.h>
63#include <stdbool.h>
64#include <string.h>
65#include <errno.h>
66#include <time.h>
67#include <fcntl.h>
68#include <sys/stat.h>
69#include <sys/types.h>
70#include <unistd.h>
71#include <iconv.h>
72#include <pthread.h>
73#include <talloc.h>
74
75/* regfi headers */
76#include "compat.h"
77#include "byteorder.h"
78#include "winsec.h"
79#include "void_stack.h"
80#include "range_list.h"
81#include "lru_cache.h"
82
83/******************************************************************************/
84/* Constants for use while interacting with the library                       */
85/******************************************************************************/
86
87/* regfi library error message types */
88#define REGFI_LOG_INFO  0x0001
89#define REGFI_LOG_WARN  0x0004
90#define REGFI_LOG_ERROR 0x0010
91#define REGFI_DEFAULT_LOG_MASK REGFI_LOG_ERROR|REGFI_LOG_WARN
92
93/* regfi library supported character encodings */
94/* UTF16LE is not supported for output */
95typedef enum {
96  REGFI_ENCODING_DEFAULT  = 0,
97  REGFI_ENCODING_ASCII =   0,
98  REGFI_ENCODING_UTF8  =  1,
99  REGFI_ENCODING_UTF16LE = 2,
100  REGFI_NUM_ENCODINGS  =  3
101} REGFI_ENCODING;
102
103/* Registry data types */
104typedef enum {
105  REG_NONE                   =    0,
106  REG_SZ                     =    1,
107  REG_EXPAND_SZ              =    2,
108  REG_BINARY                 =    3,
109  REG_DWORD                  =    4,
110  REG_DWORD_LE               =    4 , /* DWORD, little endian */
111  REG_DWORD_BE               =    5 , /* DWORD, big endian */
112  REG_LINK                   =    6,
113  REG_MULTI_SZ               =    7,
114  REG_RESOURCE_LIST          =    8,
115  REG_FULL_RESOURCE_DESCRIPTOR=   9,
116  REG_RESOURCE_REQUIREMENTS_LIST= 10,
117  REG_QWORD                     = 11, /* 64-bit little endian */
118/* XXX: Has MS defined a REG_QWORD_BE? */
119/* Not a real type in the registry */
120  REG_KEY                 =   0x7FFFFFFF
121} REGFI_DATA_TYPE;
122#define REGFI_OFFSET_NONE          0xffffffff
123
124
125
126/******************************************************************************/
127/* Various resource limits and related constants                              */
128/******************************************************************************/
129
130/* Flags determining how many records to cache internally */
131#define REGFI_CACHE_SK_MAX         64
132#define REGFI_CACHE_NK_MAX         1024
133
134/* This maximum depth is described here:
135 * http://msdn.microsoft.com/en-us/library/ms724872%28VS.85%29.aspx
136 */
137#define REGFI_MAX_DEPTH            512
138
139/* This limit defines the maximum number of levels deep that ri subkey list
140 * trees can go.
141 */
142/* XXX: This is totally arbitrary right now.
143 *      The actual limit may need to be discovered by experimentation.
144 */
145#define REGFI_MAX_SUBKEY_DEPTH     255
146
147
148/******************************************************************************/
149/* Symbols for internal use                                                   */
150/******************************************************************************/
151
152/* Global thread-local storage key */
153pthread_key_t regfi_log_key;
154
155/* Header sizes and magic number lengths for various records */
156#define REGFI_HBIN_ALLOC           0x1000 /* Minimum allocation unit for HBINs */
157#define REGFI_REGF_SIZE            0x1000 /* "regf" header block size */
158#define REGFI_REGF_MAGIC_SIZE      4
159#define REGFI_REGF_NAME_SIZE       64
160#define REGFI_REGF_RESERVED1_SIZE  340
161#define REGFI_REGF_RESERVED2_SIZE  3528
162#define REGFI_HBIN_MAGIC_SIZE      4
163#define REGFI_CELL_MAGIC_SIZE      2
164#define REGFI_HBIN_HEADER_SIZE     0x20
165#define REGFI_NK_MIN_LENGTH        0x4C
166#define REGFI_VK_MIN_LENGTH        0x14
167#define REGFI_SK_MIN_LENGTH        0x14
168#define REGFI_SUBKEY_LIST_MIN_LEN  0x4
169#define REGFI_BIG_DATA_MIN_LENGTH  0xC
170
171
172/* Constants used for validation */
173/* XXX: Can we add clock resolution validation as well as range?  It has
174 *      been reported that Windows timestamps are never more than a
175 *      certain granularity (250ms?), which could be used to help
176 *      eliminate false positives.  Would need to verify this and
177 *      perhaps conservatively implement a check.
178 */
179 /* Minimum time is Jan 1, 1990 00:00:00 */
180#define REGFI_MTIME_MIN            0x01B41E6D00000000L
181
182 /* Maximum time is Jan 1, 2290 00:00:00
183  * (We hope no one is using Windows by then...)
184  */
185#define REGFI_MTIME_MAX            0x0304754300000000L
186
187
188/* Flags for the vk records */
189#define REGFI_VK_FLAG_ASCIINAME    0x0001
190#define REGFI_VK_DATA_IN_OFFSET    0x80000000
191#define REGFI_VK_MAX_DATA_LENGTH   1024*1024  /* XXX: This is arbitrary */
192
193
194/* Known key flags */
195/*******************/
196/* These next two show up on normal-seeming keys in Vista and W2K3 registries */
197#define REGFI_NK_FLAG_UNKNOWN1     0x4000
198#define REGFI_NK_FLAG_UNKNOWN2     0x1000
199
200/* This next one shows up in some Vista "software" registries */
201/* XXX: This shows up in the following two SOFTWARE keys in Vista:
202 *   /Wow6432Node/Microsoft
203 *   /Wow6432Node/Microsoft/Cryptography
204 * 
205 * It comes along with UNKNOWN2 and ASCIINAME for a total flags value of 0x10A0
206 */
207#define REGFI_NK_FLAG_UNKNOWN3     0x0080
208
209/* Predefined handle.  Rumor has it that the valuelist count for this key is
210 * where the handle is stored.
211 * http://msdn.microsoft.com/en-us/library/ms724836(VS.85).aspx
212 */
213#define REGFI_NK_FLAG_PREDEF_KEY   0x0040
214
215/* The name will be in ASCII if this next bit is set, otherwise UTF-16LE */
216#define REGFI_NK_FLAG_ASCIINAME    0x0020
217
218/* Symlink key. 
219 * See: http://www.codeproject.com/KB/system/regsymlink.aspx
220 */
221#define REGFI_NK_FLAG_LINK         0x0010
222
223/* This key cannot be deleted */
224#define REGFI_NK_FLAG_NO_RM        0x0008
225
226/* Root of a hive */
227#define REGFI_NK_FLAG_ROOT         0x0004
228
229/* Mount point of another hive.  NULL/(default) value indicates which hive
230 * and where in the hive it points to.
231 */
232#define REGFI_NK_FLAG_HIVE_LINK    0x0002
233
234/* These keys shouldn't be stored on disk, according to:
235 * http://geekswithblogs.net/sdorman/archive/2007/12/24/volatile-registry-keys.aspx
236 */
237#define REGFI_NK_FLAG_VOLATILE     0x0001
238
239/* Useful for identifying unknown flag types */
240#define REGFI_NK_KNOWN_FLAGS       (REGFI_NK_FLAG_PREDEF_KEY\
241                                    | REGFI_NK_FLAG_ASCIINAME\
242                                    | REGFI_NK_FLAG_LINK\
243                                    | REGFI_NK_FLAG_NO_RM\
244                                    | REGFI_NK_FLAG_ROOT\
245                                    | REGFI_NK_FLAG_HIVE_LINK\
246                                    | REGFI_NK_FLAG_VOLATILE\
247                                    | REGFI_NK_FLAG_UNKNOWN1\
248                                    | REGFI_NK_FLAG_UNKNOWN2\
249                                    | REGFI_NK_FLAG_UNKNOWN3)
250
251
252#ifndef CHAR_BIT
253#define CHAR_BIT 8
254#endif
255
256#define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
257                    : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
258#define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
259#define REGFI_TIME_FIXUP (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
260
261
262
263/******************************************************************************/
264/* Structures                                                                 */
265/******************************************************************************/
266
267typedef uint64_t REGFI_NTTIME;
268
269typedef struct _regfi_log
270{
271  /* Error/warning/info messages returned by lower layer functions */
272  char* messages;
273
274  /* Mask for error message types that will be stored. */
275  uint16_t msg_mask;
276
277} REGFI_LOG;
278
279
280/** HBIN block information
281 * @ingroup regfiMiddleLayer
282 */
283typedef struct _regfi_hbin
284{
285  /** Offset of this HBIN in the registry file */
286  uint32_t file_off;
287
288  /** Number of active records pointing to this block (not used currently) */
289  uint32_t ref_count;
290
291  /** Offset from first hbin block */
292  uint32_t first_hbin_off;
293
294  /** Block size of this block Should be a multiple of 4096 (0x1000) */
295  uint32_t block_size;
296
297  /** Relative offset to next block. 
298   *
299   * @note This value may be unreliable!
300   */
301  uint32_t next_block;
302
303  /** Magic number for the HBIN (should be "hbin"). */
304  uint8_t magic[REGFI_HBIN_MAGIC_SIZE];
305} REGFI_HBIN;
306
307
308/* Subkey List -- list of key offsets and hashed names for consistency */
309typedef struct 
310{
311  /* Virtual offset of NK record or additional subkey list,
312   * depending on this list's type.
313   */
314  uint32_t offset;
315
316  uint32_t hash;
317} REGFI_SUBKEY_LIST_ELEM;
318
319
320/** Subkey-list structure
321 * @ingroup regfiMiddleLayer
322 */
323typedef struct _regfi_subkey_list
324{
325  /* Real offset of this record's cell in the file */
326  uint32_t offset;
327
328  uint32_t cell_size;
329 
330  /* Number of immediate children */
331  uint32_t num_children;
332
333  /* Total number of keys referenced by this list and its children */
334  uint32_t num_keys;
335
336  REGFI_SUBKEY_LIST_ELEM* elements;
337  uint8_t magic[REGFI_CELL_MAGIC_SIZE];
338
339  /* Set if the magic indicates this subkey list points to child subkey lists */
340  bool recursive_type;
341} REGFI_SUBKEY_LIST;
342
343
344typedef uint32_t REGFI_VALUE_LIST_ELEM;
345/** Value-list structure
346 * @ingroup regfiMiddleLayer
347 */
348typedef struct _regfi_value_list
349{
350  /* Real offset of this record's cell in the file */
351  uint32_t offset;
352
353  uint32_t cell_size;
354
355  /* Actual number of values referenced by this list. 
356   * May differ from parent key's num_values if there were parsing errors.
357   */
358  uint32_t num_values;
359
360  REGFI_VALUE_LIST_ELEM* elements;
361} REGFI_VALUE_LIST;
362
363
364/** Class name structure (used in storing SysKeys)
365 * @ingroup regfiBase
366 */
367typedef struct _regfi_classname
368{
369  /** Real offset of this record's cell in the file */
370  uint32_t offset;
371
372  /** As converted to requested REGFI_ENCODING */
373  char* interpreted;
374
375  /** Represents raw buffer read from classname cell.
376   *
377   * Length of this item is specified in the size field.
378   */
379  uint8_t* raw;
380
381  /** Length of the raw data.
382   *
383   * May be shorter than that indicated by parent key.
384   */
385  uint16_t size;
386} REGFI_CLASSNAME;
387
388
389/** Data record structure
390 * @ingroup regfiBase
391 */
392typedef struct _regfi_data
393{
394  /* XXX: this isn't populated yet. Should set it to start of data cell
395   *      or big data cell.
396   */
397  uint32_t offset;
398
399  /** Data type of this data, as indicated by the referencing VK record. */
400  REGFI_DATA_TYPE type;
401
402  /** Length of the raw data. */
403  uint32_t size;
404
405  /** This is always present, representing the raw data cell contents. */
406  uint8_t* raw;
407
408  /** Represents the length of the interpreted value. Meaning is type-specific.
409   *  Will be 0 if interpretation failed for any reason.
410   */
411  uint32_t interpreted_size;
412
413  /** These items represent interpreted versions of the REGFI_DATA::raw field.
414   *
415   * Only use the appropriate member according to the REGFI_DATA::type field.
416   * In the event of an unknown type, use only the REGFI_DATA::raw field.
417   */
418  union _regfi_data_interpreted
419  {
420    /** REG_NONE
421     *
422     * Stored as a raw buffer.  Use REGFI_DATA::interpreted_size to determine
423     * length.
424     */
425    uint8_t* none; 
426
427    /** REG_SZ
428     *
429     * Stored as a NUL terminated string.  Converted to the specified
430     * REGFI_ENCODING.
431     */
432    uint8_t* string;
433
434    /** REG_EXPAND_SZ
435     *
436     * Stored as a NUL terminated string.  Converted to the specified
437     * REGFI_ENCODING.
438     */
439    uint8_t* expand_string;
440
441    /** REG_BINARY
442     *
443     * Stored as a raw buffer.  Use REGFI_DATA::interpreted_size to determine
444     * length.
445     */
446    uint8_t* binary;
447
448    /** REG_DWORD */
449    uint32_t dword;
450
451    /** REG_DWORD_BE */
452    uint32_t dword_be;
453
454    /** REG_LINK
455     *
456     * Stored as a NUL terminated string.  Converted to the specified
457     * REGFI_ENCODING.
458     */
459    uint8_t* link;
460
461    /** REG_MULTI_SZ
462     *
463     * Stored as a list of uint8_t* pointers, terminated with a NULL pointer.
464     * Each string element in the list is NUL terminated, and the character set
465     * is determined by the specified REGFI_ENCODING.
466     */
467    uint8_t** multiple_string;
468
469    /** REG_QWORD */
470    uint64_t qword;
471
472    /* The following are treated as binary currently, but this may change in
473     * the future as the formats become better understood.
474     */
475
476    /** REG_RESOURCE_LIST
477     *
478     * Stored as a raw buffer.  Use REGFI_DATA::interpreted_size to determine
479     * length.
480     */
481    uint8_t* resource_list;
482
483    /** REG_FULL_RESOURCE_DESCRIPTOR
484     *
485     * Stored as a raw buffer.  Use REGFI_DATA::interpreted_size to determine
486     * length.
487     */
488    uint8_t* full_resource_descriptor;
489
490    /** REG_RESOURCE_REQUIREMENTS_LIST
491     *
492     * Stored as a raw buffer.  Use REGFI_DATA::interpreted_size to determine
493     * length.
494     */
495    uint8_t* resource_requirements_list;
496  } interpreted;
497} REGFI_DATA;
498
499
500/** Value structure
501 * @ingroup regfiBase
502 */
503typedef struct _regfi_vk
504{
505  /** Real offset of this record's cell in the file */
506  uint32_t offset;     
507
508  /** ((start_offset - end_offset) & 0xfffffff8) */
509  uint32_t cell_size;
510
511  /** The name of this value converted to desired REGFI_ENCODING. 
512   *
513   * This conversion typically occurs automatically through REGFI_ITERATOR
514   * settings.  String is NUL terminated.
515   */
516  char* name;
517
518  /** The raw value name
519   *
520   * Length of the buffer is stored in name_length.
521   */
522  uint8_t* name_raw;
523
524  /** Length of name_raw */
525  uint16_t name_length;
526
527  /** Offset from beginning of this hbin block */
528  uint32_t hbin_off;
529 
530  /** Size of the value's data as reported in the VK record.
531   *
532   * May be different than that obtained while parsing the data cell itself.
533   */
534  uint32_t data_size;
535
536  /** Virtual offset of data cell */
537  uint32_t data_off;
538
539  /** Value's data type */
540  REGFI_DATA_TYPE type;
541
542  /** VK record's magic number (should be "vk") */
543  uint8_t  magic[REGFI_CELL_MAGIC_SIZE];
544
545  /** VK record flags */
546  uint16_t flags;
547
548  /* XXX: A 2-byte field of unknown purpose stored in the VK record */
549  uint16_t unknown1;
550
551  /** Whether or not the data record is stored in the VK record's data_off field.
552   *
553   * This information is derived from the high bit of the raw data size field.
554   */
555  bool     data_in_offset;
556
557  /* XXX: deprecated */
558  REGFI_DATA* data;
559
560} REGFI_VK;
561
562
563/* Key Security */
564struct _regfi_sk;
565
566/** Security structure
567 * @ingroup regfiBase
568 */
569typedef struct _regfi_sk
570{
571  /** Real file offset of this record */
572  uint32_t offset;
573
574  /** ((start_offset - end_offset) & 0xfffffff8) */
575  uint32_t cell_size;
576
577  /** The stored Windows security descriptor for this SK record */
578  WINSEC_DESC* sec_desc;
579
580  /** Offset of this record from beginning of this hbin block */
581  uint32_t hbin_off;
582 
583  /** Offset of the previous SK record in the linked list of SK records */
584  uint32_t prev_sk_off;
585
586  /** Offset of the next SK record in the linked list of SK records */
587  uint32_t next_sk_off;
588
589  /** Number of keys referencing this SK record */
590  uint32_t ref_count;
591
592  /** Size of security descriptor (sec_desc) */
593  uint32_t desc_size;
594
595  /* XXX: A 2-byte field of unknown purpose */
596  uint16_t unknown_tag;
597
598  /** The magic number for this record (should be "sk") */
599  uint8_t  magic[REGFI_CELL_MAGIC_SIZE];
600} REGFI_SK;
601
602
603/** Key structure
604 * @ingroup regfiBase
605 */
606typedef struct _regfi_nk
607{
608  /** Real offset of this record's cell in the file */
609  uint32_t offset;
610
611  /** Actual or estimated length of the cell. 
612   * Always in multiples of 8.
613   */
614  uint32_t cell_size;
615
616  /** Preloaded value-list for this key.
617   * This element is loaded automatically when using the iterator interface and
618   * possibly some lower layer interfaces.
619   */
620  REGFI_VALUE_LIST* values;
621
622
623  /** Preloaded subkey-list for this key.
624   * This element is loaded automatically when using the iterator interface and
625   * possibly some lower layer interfaces.
626   */
627  REGFI_SUBKEY_LIST* subkeys;
628 
629  /** Key flags */
630  uint16_t flags;
631
632  /** Magic number of key (should be "nk") */
633  uint8_t  magic[REGFI_CELL_MAGIC_SIZE];
634
635  /** Key's last modification time */
636  REGFI_NTTIME mtime;
637
638  /** Length of name_raw */
639  uint16_t name_length;
640
641  /** Length of referenced classname */
642  uint16_t classname_length;
643
644  /** The name of this key converted to desired REGFI_ENCODING. 
645   *
646   * This conversion typically occurs automatically through REGFI_ITERATOR
647   * settings.  String is NUL terminated.
648   */
649  char* name;
650
651  /** The raw key name
652   *
653   * Length of the buffer is stored in name_length.
654   */
655  uint8_t* name_raw;
656
657  /** Virtual offset of parent key */
658  uint32_t parent_off;
659
660  /** Virtual offset of classname key */
661  uint32_t classname_off;
662 
663  /* XXX: max subkey name * 2 */
664  uint32_t max_bytes_subkeyname;
665
666  /* XXX: max subkey classname length (as if) */
667  uint32_t max_bytes_subkeyclassname;
668
669  /* XXX: max value name * 2 */
670  uint32_t max_bytes_valuename;
671
672  /* XXX: max value data size */
673  uint32_t max_bytes_value;
674 
675  /* XXX: Fields of unknown purpose */
676  uint32_t unknown1;
677  uint32_t unknown2;
678  uint32_t unknown3;
679  uint32_t unk_index;               /* nigel says run time index ? */
680 
681  /** Number of subkeys */
682  uint32_t num_subkeys;
683
684  /** Virtual offset of subkey-list */
685  uint32_t subkeys_off;
686
687  /** Number of values for this key */
688  uint32_t num_values;
689
690  /** Virtual offset of value-list */
691  uint32_t values_off;
692
693  /** Virtual offset of SK record */
694  uint32_t sk_off;
695} REGFI_NK;
696
697
698typedef struct _regfi_raw_file
699{
700  int64_t  (* seek)(); /* (REGFI_RAW_FILE* self, uint64_t offset, int whence) */
701  ssize_t  (* read)(); /* (REGFI_RAW_FILE* self, void* buf, size_t count) */
702
703  uint64_t cur_off;
704  uint64_t size;
705  void*    state;
706} REGFI_RAW_FILE;
707
708
709/** Registry hive file data structure
710 *
711 * This essential structure stores run-time information about a single open
712 * registry hive as well as file header (REGF block) data.  This structure
713 * also stores a list of warnings and error messages generated while parsing
714 * the registry hive.  These can be tuned using @ref regfi_log_set_mask. 
715 * Messages may be retrieved using @ref regfi_log_get_str.
716 *
717 * @note If the message mask is set to record any messages, dependent code
718 *       must use @ref regfi_log_get_str periodically to clear the message
719 *       queue. Otherwise, this structure will grow in size over time as
720 *       messages queue up.
721 *
722 * @ingroup regfiBase
723 */ 
724typedef struct _regfi_file
725{
726  /* Data parsed from file header */
727  /********************************/
728  uint8_t  magic[REGFI_REGF_MAGIC_SIZE];/* "regf" */
729
730 /* These sequence numbers should match if
731  * the hive was properly synced to disk.
732  */
733  uint32_t sequence1;           
734  uint32_t sequence2;
735
736  REGFI_NTTIME mtime;
737  uint32_t major_version;  /* Set to 1 in all known hives */
738  uint32_t minor_version;  /* Set to 3 or 5 in all known hives */
739  uint32_t type;           /* XXX: Unverified.  Set to 0 in all known hives */
740  uint32_t format;         /* XXX: Unverified.  Set to 1 in all known hives */
741
742  uint32_t root_cell;  /* Offset to root cell in the first (or any?) hbin block */
743  uint32_t last_block; /* Offset to last hbin block in file */
744
745  uint32_t cluster;    /* XXX: Unverified. Set to 1 in all known hives */
746
747  /* Matches hive's base file name. Stored in UTF-16LE */
748  uint8_t file_name[REGFI_REGF_NAME_SIZE];
749
750  WINSEC_UUID* rm_id;       /* XXX: Unverified. */
751  WINSEC_UUID* log_id;      /* XXX: Unverified. */
752  WINSEC_UUID* tm_id;       /* XXX: Unverified. */
753  uint32_t flags;             /* XXX: Unverified. */
754  uint32_t guid_signature;    /* XXX: Unverified. */
755
756  uint32_t checksum;          /* Stored checksum from file */
757  uint32_t computed_checksum; /* Our own calculation of the checksum.
758                             * (XOR of bytes 0x0000 - 0x01FB) */
759
760  WINSEC_UUID* thaw_tm_id;  /* XXX: Unverified. */
761  WINSEC_UUID* thaw_rm_id;  /* XXX: Unverified. */
762  WINSEC_UUID* thaw_log_id; /* XXX: Unverified. */
763  uint32_t boot_type;         /* XXX: Unverified. */
764  uint32_t boot_recover;      /* XXX: Unverified. */
765
766  /* This seems to include random junk.  Possibly unsanitized memory left over
767   * from when header block was written.  For instance, chunks of nk records
768   * can be found, though often it's all 0s. */
769  uint8_t reserved1[REGFI_REGF_RESERVED1_SIZE];
770
771  /* This is likely reserved and unusued currently.  (Should be all 0s.)
772   * Included here for easier access in looking for hidden data
773   * or doing research. */
774  uint8_t reserved2[REGFI_REGF_RESERVED2_SIZE];
775
776
777  /* Run-time information */
778  /************************/
779  /* For sanity checking (not part of the registry header) */
780  uint32_t file_length;
781
782  /** The encoding that all strings are converted to during interpretation.
783   */
784  REGFI_ENCODING string_encoding;
785
786  /* Functions for accessing the file */
787  REGFI_RAW_FILE* cb;
788
789  /* Mutex for all cb access.  This is done to prevent one thread from moving
790   * the file offset while another thread is in the middle of a multi-read
791   * parsing transaction */
792  pthread_mutex_t cb_lock;
793
794  /* Metadata about hbins */
795  range_list* hbins;
796
797  /* Multiple read access allowed, write access is exclusive */
798  pthread_rwlock_t hbins_lock;
799
800  /* Small number of SK records cached */
801  lru_cache* sk_cache;
802
803  /* Need exclusive access for LRUs, since lookups make changes */
804  pthread_mutex_t sk_lock;
805
806  /* Limited number of keys cached */
807  lru_cache* nk_cache;
808
809  /* Need exclusive access for LRUs, since lookups make changes */
810  pthread_mutex_t nk_lock;
811
812  /* Needed to protect various talloc calls */
813  pthread_mutex_t mem_lock;
814
815} REGFI_FILE;
816
817
818typedef struct _regfi_iter_position
819{
820  /* key offset */
821  uint32_t offset;
822
823  /* Index of the current subkey */
824  uint32_t cur_subkey;
825
826  /* Index of the current value */
827  uint32_t cur_value;
828
829  /* The number of subkeys of this key */
830  uint32_t num_subkeys;
831
832  /* The number of values of this key */
833  uint32_t num_values;
834
835} REGFI_ITER_POSITION;
836
837
838/** Registry hive iterator
839 * @ingroup regfiIteratorLayer
840 */
841typedef struct _regfi_iterator
842{
843  /** The registry hive this iterator is associated with */
844  REGFI_FILE* f;
845
846  /** All current parent keys and associated iterator positions */
847  void_stack* key_positions;
848
849  REGFI_ITER_POSITION* cur;
850} REGFI_ITERATOR;
851
852
853
854/** General purpose buffer with stored length
855 * @ingroup regfiBottomLayer
856 */
857typedef struct _regfi_buffer
858{
859  uint8_t* buf;
860  uint32_t len;
861} REGFI_BUFFER;
862
863
864
865/******************************************************************************/
866/**
867 * @defgroup regfiBase Base Layer: Essential Functions and Data Structures
868 *
869 * These functions are either necessary for normal use of the regfi API or just
870 * don't fit particularly well in any of the other layers.
871 */
872/******************************************************************************/
873
874
875
876/** Returns the current regfi library version
877 *
878 * @return A string indicating the version.
879 *
880 * @ingroup regfiBase
881 */
882_EXPORT()
883const char* regfi_version();
884
885
886/** Parses file headers of an already open registry hive file and
887 *  allocates related structures for further parsing.
888 *
889 * @param fd A file descriptor of an already open file.  Must be seekable.
890 *
891 * @return A reference to a newly allocated REGFI_FILE structure, if successful;
892 *         NULL on error.  Use regfi_free to free the returned REGFI_FILE.
893 *
894 * @ingroup regfiBase
895 */
896_EXPORT()
897REGFI_FILE* regfi_alloc(int fd, REGFI_ENCODING output_encoding);
898
899
900/** Parses file headers returned by supplied callback functions.
901 *
902 * This interface is useful if you have a registry hive in memory
903 * or have some other reason to emulate a real file.
904 *
905 * @param file_cb A structure defining the callback functions needed to access the file.
906 *
907 * @return A reference to a newly allocated REGFI_FILE structure, if successful;
908 *         NULL on error.  Use regfi_free to free the returned REGFI_FILE.
909 *
910 * @ingroup regfiBase
911 */
912_EXPORT()
913REGFI_FILE* regfi_alloc_cb(REGFI_RAW_FILE* file_cb,
914                           REGFI_ENCODING output_encoding);
915
916
917/** Frees a hive's data structures without closing the underlying file.
918 *
919 * @param file The registry structure to free.
920 *
921 * @ingroup regfiBase
922 */
923_EXPORT()
924void regfi_free(REGFI_FILE* file);
925
926
927/** Get errors, warnings, and/or verbose information relating to processing of
928 *  the given registry file.
929 *
930 * @return A newly allocated char* which must be free()d by the caller.
931 *
932 * @ingroup regfiBase
933 */
934_EXPORT()
935char* regfi_log_get_str();
936
937
938/** Set the verbosity level of messages generated by the library for the
939 *  current thread.
940 *
941 * @param mask   An integer representing the types of messages desired.
942 *               Acceptable values are created through bitwise ORs of
943 *               REGFI_LOG_* values.  For instance, if only errors and
944 *               informational messages were desired (but not warnings),
945 *               then one would specify: REGFI_LOG_ERROR|REGFI_LOG_INFO
946 *               By default the message mask is: REGFI_LOG_ERROR|REGFI_LOG_WARN.
947 *
948 * @return       true on success and false on failure.  Failure occurs if
949 *               underlying pthread functions fail.  errno is set in this case.
950 *
951 * Message masks are set in a thread-specific way.  If one were to set a message
952 * mask in one thread and then spawn a new thread, then the new thread will have
953 * it's message mask reset to the default.  This function may be called at any
954 * time and will take effect immediately for the current thread.
955 *
956 * @note When a non-zero message mask is set, messages will
957 *       accumulate in memory without limit if they are not fetched using
958 *       @ref regfi_get_log_str and subsequently freed by the caller.  It is
959 *       recommended that messsages be fetched after each regfi API call in
960 *       order to provide the most context.
961 *
962 * @ingroup regfiBase
963 */
964_EXPORT()
965bool regfi_log_set_mask(uint16_t mask);
966
967
968/** Fetches a hive's root key.
969 *
970 * @return Returns the root key or NULL on failure.  Key must be freed using
971 *         @ref regfi_free_record.
972 *
973 * @ingroup regfiBase
974 */
975_EXPORT()
976const REGFI_NK*       regfi_get_rootkey(REGFI_FILE* file);
977
978
979/** Frees a record previously returned by one of the API functions.
980 *
981 * @param file The file from which the record originated. 
982 *             (This is needed for memory management reasons.)
983 *
984 * @param record Any of the following record types: REGFI_NK, REGFI_VK,
985 *        REGFI_SK, REGFI_DATA, and REGFI_CLASSNAME records.
986 *
987 * @note The "const" in the record data type is a bit misleading and is there just for
988 * convenience.  Since records returned previously must not be modified by users
989 * of the API due to internal caching, these are returned as const, so this
990 * function is const to make passing those records back easy.
991 *
992 * @ingroup regfiBase
993 */
994_EXPORT()
995void regfi_free_record(REGFI_FILE* file, const void* record);
996
997
998/** Increments reference count on record
999 *
1000 * Adds an extra internal reference to specified record, making it necessary to
1001 * call regfi_free_record on it an additional time before it is freed.  This is
1002 * useful in cases where multiple threads/structures need access to a shared record,
1003 * without requiring them to be in sync with when it is freed.
1004 *
1005 * @param file The file from which the record originated. 
1006 *             (This is needed for memory management reasons.)
1007 *
1008 * @param record Any of the following record types: REGFI_NK, REGFI_VK,
1009 *        REGFI_SK, REGFI_DATA, and REGFI_CLASSNAME records.
1010 *
1011 * @return Updated record pointer on success, NULL otherwise
1012 *
1013 * @note Be sure to use the returned record for further access to the structure
1014 *       instead of the previous version of the pointer.  E.g.:
1015 *       @code
1016 *       myKey = (const REGFI_NK*)regfi_reference_record(myFile, myKey);
1017 *       @endcode
1018 *
1019 * @ingroup regfiBase
1020 */
1021_EXPORT()
1022const void* regfi_reference_record(REGFI_FILE* file, const void* record);
1023
1024
1025/** Retrieves number of subkeys referenced by this key.
1026 *
1027 * Number of subkeyss in key structure and subkey list structure could differ,
1028 * so this provides a standard/sane way of determining the number.
1029 *
1030 * @param key  the key whose number of subkeys is desired
1031 *
1032 * @return Returns the number of subkeys referenced by this key.
1033 *
1034 * @ingroup regfiBase
1035 */
1036_EXPORT()
1037uint32_t regfi_fetch_num_subkeys(const REGFI_NK* key);
1038
1039
1040/** Retrieves number of values referenced by this key.
1041 *
1042 * Number of values in key structure and value list structure could differ,
1043 * so this provides a standard/sane way of determining the number.
1044 *
1045 * @param key  the key whose number of values is desired
1046 *
1047 * @return Returns the number of values referenced by this key.
1048 *
1049 * @ingroup regfiBase
1050 */
1051_EXPORT()
1052uint32_t regfi_fetch_num_values(const REGFI_NK* key);
1053
1054
1055/** Retrieves classname for a given key.
1056 *
1057 * @param file the file from which key is derived
1058 * @param key the key whose classname is desired
1059 *
1060 * @return Returns a newly allocated classname structure, or NULL on failure.
1061 *         Classname structures must be freed with @ref regfi_free_record.
1062 *
1063 * @ingroup regfiBase
1064 */
1065_EXPORT()
1066const REGFI_CLASSNAME* regfi_fetch_classname(REGFI_FILE* file, 
1067                                             const REGFI_NK* key);
1068
1069
1070/** Returns the SK (security) record referenced by the supplied key.
1071 *
1072 * @param file the file from which key is derived
1073 * @param key  the key whose SK record is desired
1074 *
1075 * @return A read-only SK structure, or NULL on failure.
1076 *
1077 * @ingroup regfiBase
1078 */
1079_EXPORT()
1080const REGFI_SK* regfi_fetch_sk(REGFI_FILE* file, const REGFI_NK* key);
1081
1082
1083/** Returns the next SK (security) record referenced by the supplied SK record
1084 *
1085 * @param file the file from which sk is derived
1086 * @param sk   the SK record whose next sibling SK record is desired
1087 *
1088 * @return A read-only SK structure, or NULL on failure.
1089 *
1090 * @note
1091 * SK records are included in a circular, doubly-linked list.
1092 * To iterate over all SK records, be sure to check for the repetition of
1093 * the SK record you started with to determine when all have been traversed.
1094 *
1095 * @ingroup regfiBase
1096 */
1097_EXPORT()
1098const REGFI_SK* regfi_next_sk(REGFI_FILE* file, const REGFI_SK* sk);
1099
1100
1101/** Returns the previous SK (security) record referenced by the supplied SK record
1102 *
1103 * @param file the file from which sk is derived
1104 * @param sk   the SK record whose previous sibling SK record is desired
1105 *
1106 * @return A read-only SK structure, or NULL on failure.
1107 *
1108 * @note
1109 * SK records are included in a circular, doubly-linked list.
1110 * To iterate over all SK records, be sure to check for the repetition of
1111 * the SK record you started with to determine when all have been traversed.
1112 *
1113 * @ingroup regfiBase
1114 */
1115_EXPORT()
1116const REGFI_SK* regfi_prev_sk(REGFI_FILE* file, const REGFI_SK* sk);
1117
1118
1119/** Retrieves data for a given value.
1120 *
1121 * @param file the file from which value is derived
1122 * @param value the value whose data is desired
1123 *
1124 * @return Returns a newly allocated data structure, or NULL on failure.
1125 *         Data structures must be freed with @ref regfi_free_record.
1126 *
1127 * @ingroup regfiBase
1128 */
1129_EXPORT()
1130const REGFI_DATA* regfi_fetch_data(REGFI_FILE* file,
1131                                   const REGFI_VK* value);
1132
1133
1134/** Locates a specific subkey of a given key.
1135 *
1136 * @param file  the file from which key is derived
1137 * @param key   the key whose subkey is desired
1138 * @param name  name of the desired subkey
1139 * @param index a return value: the index of the desired subkey.
1140 *              undefined on error
1141 *
1142 * @return true if the subkey is found, false if an error occurred or if the
1143 *         specified name could not be found. If an error occurs, messages
1144 *         will be written explaining the issue. (See regfi_log_get_str.)
1145 *
1146 * @ingroup regfiBase
1147 */
1148_EXPORT()
1149bool regfi_find_subkey(REGFI_FILE* file, const REGFI_NK* key, 
1150                       const char* name, uint32_t* index);
1151
1152
1153/** Locates a specific value of a given key.
1154 *
1155 * @param file  the file from which key is derived
1156 * @param key   the key whose value is desired
1157 * @param name  name of the desired value
1158 * @param index a return value: the index of the desired value. 
1159 *              undefined on error
1160 *
1161 * @return true if the value is found, false if an error occurred or if the
1162 *         specified name could not be found. If an error occurs, messages
1163 *         will be written explaining the issue. (See regfi_log_get_str.)
1164 *
1165 * @ingroup regfiBase
1166 */
1167_EXPORT()
1168bool regfi_find_value(REGFI_FILE* file, const REGFI_NK* key,
1169                      const char* name, uint32_t* index);
1170
1171
1172/** Retrieves a specific subkey of a given key.
1173 *
1174 * @param file  the file from which key is derived
1175 * @param key   the key whose subkey is desired
1176 * @param index the index of the desired subkey
1177 *
1178 * @return the requested subkey or NULL on error.
1179 *
1180 * @ingroup regfiBase
1181 */
1182_EXPORT()
1183const REGFI_NK* regfi_get_subkey(REGFI_FILE* file, const REGFI_NK* key, 
1184                                 uint32_t index);
1185
1186
1187/** Retrieves a specific value of a given key.
1188 *
1189 * @param file  the file from which key is derived
1190 * @param key   the key whose value is desired
1191 * @param index the index of the desired value
1192 *
1193 * @return the requested value or NULL on error.
1194 *
1195 * @ingroup regfiBase
1196 */
1197_EXPORT()
1198const REGFI_VK* regfi_get_value(REGFI_FILE* file, const REGFI_NK* key, 
1199                                uint32_t index);
1200
1201
1202
1203/** Uses a key's parent_off reference to retrieve it's parent.
1204 *
1205 * @param file  the file from which key is derived
1206 * @param key   the key whose parent is desired
1207 *
1208 * @return the requested subkey or NULL on error.
1209 *
1210 * @ingroup regfiBase
1211 */
1212_EXPORT()
1213const REGFI_NK* regfi_get_parentkey(REGFI_FILE* file, const REGFI_NK* key);
1214
1215
1216/******************************************************************************/
1217/**
1218 * @defgroup regfiIteratorLayer Iterator Layer: Primary regfi Library Interface
1219 *
1220 * This top layer of API functions provides an iterator interface which makes
1221 * traversing registry data structures easy in both single-threaded and
1222 * multi-threaded scenarios.
1223 */
1224/******************************************************************************/
1225
1226/** Creates a new iterator for the provided registry file.
1227 *
1228 * @param file The opened registry file the iterator should be created for.
1229 *
1230 * @param output_encoding Character encoding that strings should be returned in.
1231 *                        Only supply the REGFI_ENCODING_* constants, as others
1232 *                        will be rejected.
1233 *                        The following values are currently accepted:
1234 *                        REGFI_ENCODING_DEFAULT (currently REGFI_ENCODING_ASCII)
1235 *                        REGFI_ENCODING_ASCII
1236 *                        REGFI_ENCODING_UTF8
1237 *
1238 * @return A newly allocated REGFI_ITERATOR.
1239 *         Must be free()d with regfi_iterator_free.
1240 *
1241 * @ingroup regfiIteratorLayer
1242 */
1243_EXPORT()
1244REGFI_ITERATOR* regfi_iterator_new(REGFI_FILE* file);
1245
1246
1247/** Frees a registry file iterator previously created by regfi_iterator_new.
1248 *
1249 * This does not affect the underlying registry file's allocation status.
1250 *
1251 * @param i the iterator to be freed
1252 *
1253 * @ingroup regfiIteratorLayer
1254 */
1255_EXPORT()
1256void regfi_iterator_free(REGFI_ITERATOR* i);
1257
1258
1259/** Traverse deeper into the registry tree at the current subkey.
1260 *
1261 * @param i the iterator
1262 *
1263 * @return  true on success, false on failure. 
1264 *          Note that subkey and value indexes are preserved.  That is, if a
1265 *          regfi_iterator_up call occurs later (reversing the effect of this
1266 *          call) then the subkey and value referenced prior to the
1267 *          regfi_iterator_down call will still be referenced.  This  makes
1268 *          depth-first iteration particularly easy.
1269 *
1270 * @ingroup regfiIteratorLayer
1271 */
1272_EXPORT()
1273bool regfi_iterator_down(REGFI_ITERATOR* i);
1274
1275
1276/** Traverse up to the current key's parent key.
1277 *
1278 * @param i the iterator
1279 *
1280 * @return  true on success, false on failure.  Any subkey or value state
1281 *          associated with the current key is lost.
1282 *
1283 * @ingroup regfiIteratorLayer
1284 */
1285_EXPORT()
1286bool regfi_iterator_up(REGFI_ITERATOR* i);
1287
1288
1289/** Traverse up to the root key of the hive.
1290 *
1291 * @param i the iterator
1292 *
1293 * @return true on success, false on failure.
1294 *
1295 * @ingroup regfiIteratorLayer
1296 */
1297_EXPORT()
1298bool regfi_iterator_to_root(REGFI_ITERATOR* i);
1299
1300
1301/** Traverse down multiple levels in the registry hive.
1302 *
1303 * XXX: This currently only accepts ASCII key names.  Need to look into
1304 *      accepting other encodings.
1305 *
1306 * @param i    the iterator
1307 * @param path a list of key names representing the path.  This list must
1308 *             contain NUL terminated strings.  The list itself is
1309 *             terminated with a NULL pointer.  All path elements must be
1310 *             keys; value names are not accepted (even as the last
1311 *             element).
1312 *
1313 * @return true on success, false on failure.  If any element of path is not
1314 *                 found, false will be returned and the iterator will remain
1315 *                 in its original position.
1316 *
1317 * @ingroup regfiIteratorLayer
1318 */
1319_EXPORT()
1320bool regfi_iterator_descend(REGFI_ITERATOR* i, const char** path);
1321
1322
1323/** Returns the currently referenced key.
1324 *
1325 * @param i the iterator
1326 *
1327 * @return A read-only key structure for the current key, or NULL on failure.
1328 *         Data structures must be freed with @ref regfi_free_record.
1329 *
1330 * @ingroup regfiIteratorLayer
1331 */
1332_EXPORT()
1333const REGFI_NK* regfi_iterator_cur_key(REGFI_ITERATOR* i);
1334
1335
1336/** Sets the internal subkey index to the first subkey referenced by the current
1337 *  key.
1338 *
1339 * @param i the iterator
1340 *
1341 * @return True if the current key has any subkeys, false otherwise.
1342 *
1343 * @ingroup regfiIteratorLayer
1344 */
1345_EXPORT()
1346bool regfi_iterator_first_subkey(REGFI_ITERATOR* i);
1347
1348
1349/** Returns the currently indexed subkey.
1350 *
1351 * @param i the iterator
1352 *
1353 * @return A newly allocated key structure for the currently referenced subkey,
1354 *         or NULL on failure.  Newly allocated keys must be freed with
1355 *         @ref regfi_free_record.
1356 *
1357 * @ingroup regfiIteratorLayer
1358 */
1359_EXPORT()
1360const REGFI_NK* regfi_iterator_cur_subkey(REGFI_ITERATOR* i);
1361
1362
1363/** Increments the internal subkey index to the next key in the subkey-list.
1364 *
1365 * @param i the iterator
1366 *
1367 * @return True if another subkey should exist, false otherwise.
1368 *
1369 * @ingroup regfiIteratorLayer
1370 */
1371_EXPORT()
1372bool regfi_iterator_next_subkey(REGFI_ITERATOR* i);
1373
1374
1375/** Searches for a subkey with a given name under the current key.
1376 *
1377 * @param i     the iterator
1378 * @param name  subkey name to search for
1379 *
1380 * @return True if such a subkey was found, false otherwise.  If a subkey is
1381 *         found, the current subkey index is set to that subkey.  Otherwise,
1382 *         the subkey index remains at the same location as before the call.
1383 *
1384 * @ingroup regfiIteratorLayer
1385 */
1386_EXPORT()
1387bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* name);
1388
1389
1390/** Sets the internal value index to the first value referenced by the current
1391 *  key.
1392 *
1393 * @param i the iterator
1394 *
1395 * @return True if the current key has any values, false otherwise.
1396 *
1397 * @ingroup regfiIteratorLayer
1398 */
1399_EXPORT()
1400bool regfi_iterator_first_value(REGFI_ITERATOR* i);
1401
1402
1403/** Returns the currently indexed value.
1404 *
1405 * @param i the iterator
1406 *
1407 * @return A newly allocated value structure for the currently referenced value,
1408 *         or NULL on failure.  Newly allocated values must be freed with
1409 *         @ref regfi_free_record.
1410 *
1411 * @ingroup regfiIteratorLayer
1412 */
1413_EXPORT()
1414const REGFI_VK* regfi_iterator_cur_value(REGFI_ITERATOR* i);
1415
1416
1417/** Increments the internal value index to the next value in the value-list.
1418 *
1419 * @param i the iterator
1420 *
1421 * @return True if another value should exist, false otherwise.
1422 *
1423 * @ingroup regfiIteratorLayer
1424 */
1425_EXPORT()
1426bool regfi_iterator_next_value(REGFI_ITERATOR* i);
1427
1428
1429/** Searches for a value with a given name under the current key.
1430 *
1431 * @param i     the iterator
1432 * @param name  value name to search for
1433 *
1434 * @return True if such a value was found, false otherwise.  If a value is
1435 *         found, the current value index is set to that value.  Otherwise,
1436 *         the value index remains at the same location as before the call.
1437 *
1438 * @ingroup regfiIteratorLayer
1439 */
1440_EXPORT()
1441bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* name);
1442
1443
1444/** Returns the current key and all parent keys as a list of NK records
1445 *
1446 * @param i     the iterator
1447 *
1448 * @return An array of NK record pointers terminated by a NULL pointer. 
1449 *         This array may be passed directly to regfi_free_record to free
1450 *         the entire array.
1451 *
1452 * @note In order to use an element of the array independently from the
1453 *       array (that is, to hold a pointer to an individual NK record while
1454 *       freeing the remaining array), callers must first use
1455 *       regfi_reference_record on the elements to be kept.
1456 *
1457 * @ingroup regfiIteratorLayer
1458 */
1459_EXPORT()
1460const REGFI_NK** regfi_iterator_ancestry(REGFI_ITERATOR* i);
1461
1462
1463/******************************************************************************/
1464/**
1465 * @defgroup regfiGlueLayer Glue Layer: Logical Data Structure Loading
1466 */
1467/******************************************************************************/
1468
1469/** Loads a key and associated data structures given a file offset.
1470 *
1471 * XXX: finish documenting
1472 *
1473 * @ingroup regfiGlueLayer
1474 */
1475_EXPORT()
1476REGFI_NK* regfi_load_key(REGFI_FILE* file, uint32_t offset, 
1477                         REGFI_ENCODING output_encoding, 
1478                         bool strict);
1479
1480
1481/** Loads a value at a given file offset alng with associated data structures.
1482 *
1483 * XXX: finish documenting
1484 *
1485 * @ingroup regfiGlueLayer
1486 */
1487_EXPORT()
1488REGFI_VK* regfi_load_value(REGFI_FILE* file, uint32_t offset, 
1489                               REGFI_ENCODING output_encoding, 
1490                               bool strict);
1491
1492
1493/** Loads a logical subkey list in its entirety which may span multiple records.
1494 *
1495 * XXX: finish documenting
1496 *
1497 * @ingroup regfiGlueLayer
1498 */
1499_EXPORT()
1500REGFI_SUBKEY_LIST* regfi_load_subkeylist(REGFI_FILE* file, uint32_t offset,
1501                                         uint32_t num_keys, uint32_t max_size,
1502                                         bool strict);
1503
1504
1505/** Loads a valuelist.
1506 *
1507 * XXX: finish documenting
1508 *
1509 * @ingroup regfiGlueLayer
1510 */
1511_EXPORT()
1512REGFI_VALUE_LIST* regfi_load_valuelist(REGFI_FILE* file, uint32_t offset, 
1513                                       uint32_t num_values, uint32_t max_size,
1514                                       bool strict);
1515
1516
1517/** Loads a data record which may be contained in the virtual offset, in a
1518 *  single cell, or in multiple cells through big data records.
1519 *
1520 * XXX: finish documenting
1521 *
1522 * @ingroup regfiGlueLayer
1523 */
1524_EXPORT()
1525REGFI_BUFFER regfi_load_data(REGFI_FILE* file, uint32_t voffset,
1526                             uint32_t length, bool data_in_offset,
1527                             bool strict);
1528
1529
1530/** Loads the data associated with a big data record at the specified offset.
1531 *
1532 * XXX: finish documenting
1533 *
1534 * @ingroup regfiGlueLayer
1535 */
1536_EXPORT()
1537REGFI_BUFFER regfi_load_big_data(REGFI_FILE* file, uint32_t offset, 
1538                                 uint32_t data_length,uint32_t cell_length,
1539                                 range_list* used_ranges,
1540                                 bool strict);
1541
1542
1543/** Given raw data, attempts to interpret the data based on a specified registry
1544 *  data type.
1545 *
1546 * XXX: finish documenting
1547 *
1548 * @ingroup regfiGlueLayer
1549 */
1550_EXPORT()
1551bool regfi_interpret_data(REGFI_FILE* file, 
1552                          REGFI_ENCODING string_encoding,
1553                          uint32_t type, REGFI_DATA* data);
1554
1555
1556
1557/* These are cached so return values don't need to be freed. */
1558
1559/** Loads an "sk" security record at the specified offset.
1560 *
1561 * XXX: finish documenting
1562 *
1563 * @ingroup regfiGlueLayer
1564 */
1565_EXPORT()
1566const REGFI_SK* regfi_load_sk(REGFI_FILE* file, uint32_t offset,
1567                              bool strict);
1568
1569
1570
1571
1572/** Retrieves the HBIN data structure stored at the specified offset.
1573 *
1574 * XXX: finish documenting
1575 *
1576 * @ingroup regfiGlueLayer
1577 */
1578_EXPORT()
1579const REGFI_HBIN* regfi_lookup_hbin(REGFI_FILE* file, uint32_t offset);
1580
1581
1582
1583/******************************************************************************/
1584/**
1585 * @defgroup regfiParseLayer Parsing Layer: Direct Data Structure Access
1586 */
1587/******************************************************************************/
1588
1589_EXPORT()
1590REGFI_FILE* regfi_parse_regf(REGFI_RAW_FILE* file_cb, bool strict);
1591
1592_EXPORT()
1593REGFI_HBIN* regfi_parse_hbin(REGFI_FILE* file, uint32_t offset, 
1594                             bool strict);
1595
1596
1597/** Parses an NK record at the specified offset
1598 *
1599 * @param file     the registry file structure
1600 * @param offset   the offset of the cell (not the record) to be parsed.
1601 * @param max_size the maximum size the NK cell could be. (for validation)
1602 * @param strict   if true, rejects any malformed records.  Otherwise,
1603 *                 tries to minimally validate integrity.
1604 *
1605 * @return A newly allocated NK record structure, or NULL on failure.
1606 *
1607 * @ingroup regfiParseLayer
1608 */
1609_EXPORT()
1610REGFI_NK* regfi_parse_nk(REGFI_FILE* file, uint32_t offset,
1611                             uint32_t max_size, bool strict);
1612
1613
1614/** Parses a single cell containing a subkey-list record.
1615 *
1616 * XXX: finish documenting
1617 *
1618 * @ingroup regfiParseLayer
1619 */
1620_EXPORT()
1621REGFI_SUBKEY_LIST* regfi_parse_subkeylist(REGFI_FILE* file, uint32_t offset,
1622                                          uint32_t max_size, bool strict);
1623
1624
1625/** Parses a VK (value) record at the specified offset
1626 *
1627 * XXX: finish documenting
1628 *
1629 * @ingroup regfiParseLayer
1630 */
1631_EXPORT()
1632REGFI_VK* regfi_parse_vk(REGFI_FILE* file, uint32_t offset, 
1633                             uint32_t max_size, bool strict);
1634
1635
1636/** Parses an SK (security) record at the specified offset
1637 *
1638 * XXX: finish documenting
1639 *
1640 * @ingroup regfiParseLayer
1641 */
1642_EXPORT()
1643REGFI_SK* regfi_parse_sk(REGFI_FILE* file, uint32_t offset, 
1644                             uint32_t max_size, bool strict);
1645
1646
1647/** Retrieves information on all cells in the registry hive which are
1648 *  currently in the unallocated status. 
1649 *
1650 * The unallocated status is determined based soley on the cell length sign.
1651 *
1652 * XXX: finish documenting
1653 *
1654 * @ingroup regfiParseLayer
1655 */
1656_EXPORT()
1657range_list* regfi_parse_unalloc_cells(REGFI_FILE* file);
1658
1659
1660/** Helper function to parse a cell
1661 *
1662 * XXX: finish documenting
1663 *
1664 * @ingroup regfiParseLayer
1665 */
1666_EXPORT()
1667bool regfi_parse_cell(REGFI_RAW_FILE* file_cb, uint32_t offset,
1668                      uint8_t* hdr, uint32_t hdr_len,
1669                      uint32_t* cell_length, bool* unalloc);
1670
1671
1672/** Parses a classname cell
1673 *
1674 * XXX: finish documenting
1675 *
1676 * @ingroup regfiParseLayer
1677 */
1678_EXPORT()
1679uint8_t* regfi_parse_classname(REGFI_FILE* file, uint32_t offset,
1680                               uint16_t* name_length, 
1681                               uint32_t max_size, bool strict);
1682
1683
1684/** Parses a single-cell data record
1685 *
1686 * XXX: finish documenting
1687 *
1688 * @ingroup regfiParseLayer
1689 */
1690_EXPORT()
1691REGFI_BUFFER regfi_parse_data(REGFI_FILE* file, uint32_t offset,
1692                              uint32_t length, bool strict);
1693
1694
1695/** Parses a "little data" record which is stored entirely within the
1696 *  provided virtual offset.
1697 *
1698 * XXX: finish documenting
1699 *
1700 * @ingroup regfiParseLayer
1701 */
1702_EXPORT()
1703REGFI_BUFFER regfi_parse_little_data(REGFI_FILE* file, uint32_t voffset, 
1704                                     uint32_t length, bool strict);
1705
1706
1707/******************************************************************************/
1708/*    Private (and undocumented) Functions                                    */
1709/******************************************************************************/
1710int64_t               regfi_raw_seek(REGFI_RAW_FILE* self, 
1711                                     uint64_t offset, int whence);
1712ssize_t               regfi_raw_read(REGFI_RAW_FILE* self, 
1713                                     void* buf, size_t count);
1714_EXPORT()
1715uint64_t              regfi_seek(REGFI_RAW_FILE* file_cb, 
1716                                 uint64_t offset, int whence);
1717_EXPORT()
1718uint32_t              regfi_read(REGFI_RAW_FILE* file_cb, 
1719                                 uint8_t* buf, uint32_t* length);
1720
1721_EXPORT()
1722const char*           regfi_type_val2str(unsigned int val);
1723_EXPORT()
1724int                   regfi_type_str2val(const char* str);
1725
1726_EXPORT()
1727char*                 regfi_get_sacl(WINSEC_DESC* sec_desc);
1728_EXPORT()
1729char*                 regfi_get_dacl(WINSEC_DESC* sec_desc);
1730_EXPORT()
1731char*                 regfi_get_owner(WINSEC_DESC* sec_desc);
1732_EXPORT()
1733char*                 regfi_get_group(WINSEC_DESC* sec_desc);
1734
1735REGFI_SUBKEY_LIST*    regfi_merge_subkeylists(uint16_t num_lists, 
1736                                              REGFI_SUBKEY_LIST** lists,
1737                                              bool strict);
1738REGFI_SUBKEY_LIST*    regfi_load_subkeylist_aux(REGFI_FILE* file, uint32_t offset,
1739                                                uint32_t max_size, bool strict,
1740                                                uint8_t depth_left);
1741void                  regfi_add_message(REGFI_FILE* file, uint16_t msg_type, 
1742                                        const char* fmt, ...);
1743REGFI_NK*             regfi_copy_nk(const REGFI_NK* nk);
1744REGFI_VK*             regfi_copy_vk(const REGFI_VK* vk);
1745_EXPORT()
1746int32_t               regfi_calc_maxsize(REGFI_FILE* file, uint32_t offset);
1747int32_t               regfi_conv_charset(const char* input_charset, 
1748                                         const char* output_charset,
1749                                         uint8_t* input, char* output, 
1750                                         uint32_t input_len, uint32_t output_max);
1751_EXPORT()
1752REGFI_DATA*           regfi_buffer_to_data(REGFI_BUFFER raw_data);
1753
1754/* XXX: move to base API and document */
1755_EXPORT()
1756REGFI_NTTIME          regfi_unix2nt_time(time_t t);
1757_EXPORT()
1758double                regfi_nt2unix_time(REGFI_NTTIME nt);
1759
1760
1761_EXPORT()
1762void regfi_interpret_keyname(REGFI_FILE* file, REGFI_NK* nk, 
1763                             REGFI_ENCODING output_encoding, bool strict);
1764_EXPORT()
1765void regfi_interpret_valuename(REGFI_FILE* file, REGFI_VK* vk, 
1766                               REGFI_ENCODING output_encoding, bool strict);
1767
1768_EXPORT()
1769void regfi_init();
1770
1771
1772#endif  /* _REGFI_H */
Note: See TracBrowser for help on using the repository browser.