source: trunk/include/regfi.h @ 250

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

added key caching
readded SK caching
fixed races and deadlocks

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