source: trunk/include/regfi.h @ 295

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

simplified string encoding argument passing throughout regfi API

  • Property svn:keywords set to Id
File size: 50.2 KB
Line 
1/*
2 * Copyright (C) 2005-2011 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 263 2011-06-18 00:06:51Z 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 * @param output_encoding Character encoding that strings should be returned in.
892 *                        Only supply the REGFI_ENCODING_* constants, as others
893 *                        will be rejected.
894 *                        The following values are currently accepted:
895 *                        REGFI_ENCODING_DEFAULT (currently REGFI_ENCODING_ASCII)
896 *                        REGFI_ENCODING_ASCII
897 *                        REGFI_ENCODING_UTF8
898 *
899 * @return A reference to a newly allocated REGFI_FILE structure, if successful;
900 *         NULL on error.  Use regfi_free to free the returned REGFI_FILE.
901 *
902 * @ingroup regfiBase
903 */
904_EXPORT()
905REGFI_FILE* regfi_alloc(int fd, REGFI_ENCODING output_encoding);
906
907
908/** Parses file headers returned by supplied callback functions.
909 *
910 * This interface is useful if you have a registry hive in memory
911 * or have some other reason to emulate a real file.
912 *
913 * @param file_cb A structure defining the callback functions needed to access the file.
914 *
915 * @param output_encoding Character encoding that strings should be returned in.
916 *                        Only supply the REGFI_ENCODING_* constants, as others
917 *                        will be rejected.
918 *                        The following values are currently accepted:
919 *                        REGFI_ENCODING_DEFAULT (currently REGFI_ENCODING_ASCII)
920 *                        REGFI_ENCODING_ASCII
921 *                        REGFI_ENCODING_UTF8
922 *
923 * @return A reference to a newly allocated REGFI_FILE structure, if successful;
924 *         NULL on error.  Use regfi_free to free the returned REGFI_FILE.
925 *
926 * @ingroup regfiBase
927 */
928_EXPORT()
929REGFI_FILE* regfi_alloc_cb(REGFI_RAW_FILE* file_cb,
930                           REGFI_ENCODING output_encoding);
931
932
933/** Frees a hive's data structures without closing the underlying file.
934 *
935 * @param file The registry structure to free.
936 *
937 * @ingroup regfiBase
938 */
939_EXPORT()
940void regfi_free(REGFI_FILE* file);
941
942
943/** Get errors, warnings, and/or verbose information relating to processing of
944 *  the given registry file.
945 *
946 * @return A newly allocated char* which must be free()d by the caller.
947 *
948 * @ingroup regfiBase
949 */
950_EXPORT()
951char* regfi_log_get_str();
952
953
954/** Set the verbosity level of messages generated by the library for the
955 *  current thread.
956 *
957 * @param mask   An integer representing the types of messages desired.
958 *               Acceptable values are created through bitwise ORs of
959 *               REGFI_LOG_* values.  For instance, if only errors and
960 *               informational messages were desired (but not warnings),
961 *               then one would specify: REGFI_LOG_ERROR|REGFI_LOG_INFO
962 *               By default the message mask is: REGFI_LOG_ERROR|REGFI_LOG_WARN.
963 *
964 * @return       true on success and false on failure.  Failure occurs if
965 *               underlying pthread functions fail.  errno is set in this case.
966 *
967 * Message masks are set in a thread-specific way.  If one were to set a message
968 * mask in one thread and then spawn a new thread, then the new thread will have
969 * it's message mask reset to the default.  This function may be called at any
970 * time and will take effect immediately for the current thread.
971 *
972 * @note When a non-zero message mask is set, messages will
973 *       accumulate in memory without limit if they are not fetched using
974 *       @ref regfi_log_get_str and subsequently freed by the caller.  It is
975 *       recommended that messsages be fetched after each regfi API call in
976 *       order to provide the most context.
977 *
978 * @ingroup regfiBase
979 */
980_EXPORT()
981bool regfi_log_set_mask(uint16_t mask);
982
983
984/** Fetches a hive's root key.
985 *
986 * @return Returns the root key or NULL on failure.  Key must be freed using
987 *         @ref regfi_free_record.
988 *
989 * @ingroup regfiBase
990 */
991_EXPORT()
992const REGFI_NK*       regfi_get_rootkey(REGFI_FILE* file);
993
994
995/** Frees a record previously returned by one of the API functions.
996 *
997 * @param file The file from which the record originated. 
998 *             (This is needed for memory management reasons.)
999 *
1000 * @param record Any of the following record types: REGFI_NK, REGFI_VK,
1001 *        REGFI_SK, REGFI_DATA, and REGFI_CLASSNAME records.
1002 *
1003 * @note The "const" in the record data type is a bit misleading and is there just for
1004 * convenience.  Since records returned previously must not be modified by users
1005 * of the API due to internal caching, these are returned as const, so this
1006 * function is const to make passing those records back easy.
1007 *
1008 * @ingroup regfiBase
1009 */
1010_EXPORT()
1011void regfi_free_record(REGFI_FILE* file, const void* record);
1012
1013
1014/** Increments reference count on record
1015 *
1016 * Adds an extra internal reference to specified record, making it necessary to
1017 * call regfi_free_record on it an additional time before it is freed.  This is
1018 * useful in cases where multiple threads/structures need access to a shared record,
1019 * without requiring them to be in sync with when it is freed.
1020 *
1021 * @param file The file from which the record originated. 
1022 *             (This is needed for memory management reasons.)
1023 *
1024 * @param record Any of the following record types: REGFI_NK, REGFI_VK,
1025 *        REGFI_SK, REGFI_DATA, and REGFI_CLASSNAME records.
1026 *
1027 * @return Updated record pointer on success, NULL otherwise
1028 *
1029 * @note Be sure to use the returned record for further access to the structure
1030 *       instead of the previous version of the pointer.  E.g.:
1031 *       @code
1032 *       myKey = (const REGFI_NK*)regfi_reference_record(myFile, myKey);
1033 *       @endcode
1034 *
1035 * @ingroup regfiBase
1036 */
1037_EXPORT()
1038const void* regfi_reference_record(REGFI_FILE* file, const void* record);
1039
1040
1041/** Retrieves number of subkeys referenced by this key.
1042 *
1043 * Number of subkeyss in key structure and subkey list structure could differ,
1044 * so this provides a standard/sane way of determining the number.
1045 *
1046 * @param key  the key whose number of subkeys is desired
1047 *
1048 * @return Returns the number of subkeys referenced by this key.
1049 *
1050 * @ingroup regfiBase
1051 */
1052_EXPORT()
1053uint32_t regfi_fetch_num_subkeys(const REGFI_NK* key);
1054
1055
1056/** Retrieves number of values referenced by this key.
1057 *
1058 * Number of values in key structure and value list structure could differ,
1059 * so this provides a standard/sane way of determining the number.
1060 *
1061 * @param key  the key whose number of values is desired
1062 *
1063 * @return Returns the number of values referenced by this key.
1064 *
1065 * @ingroup regfiBase
1066 */
1067_EXPORT()
1068uint32_t regfi_fetch_num_values(const REGFI_NK* key);
1069
1070
1071/** Retrieves classname for a given key.
1072 *
1073 * @param file the file from which key is derived
1074 * @param key the key whose classname is desired
1075 *
1076 * @return Returns a newly allocated classname structure, or NULL on failure.
1077 *         Classname structures must be freed with @ref regfi_free_record.
1078 *
1079 * @ingroup regfiBase
1080 */
1081_EXPORT()
1082const REGFI_CLASSNAME* regfi_fetch_classname(REGFI_FILE* file, 
1083                                             const REGFI_NK* key);
1084
1085
1086/** Returns the SK (security) record referenced by the supplied key.
1087 *
1088 * @param file the file from which key is derived
1089 * @param key  the key whose SK record is desired
1090 *
1091 * @return A read-only SK structure, or NULL on failure.
1092 *
1093 * @ingroup regfiBase
1094 */
1095_EXPORT()
1096const REGFI_SK* regfi_fetch_sk(REGFI_FILE* file, const REGFI_NK* key);
1097
1098
1099/** Returns the next SK (security) record referenced by the supplied SK record
1100 *
1101 * @param file the file from which sk is derived
1102 * @param sk   the SK record whose next sibling SK record is desired
1103 *
1104 * @return A read-only SK structure, or NULL on failure.
1105 *
1106 * @note
1107 * SK records are included in a circular, doubly-linked list.
1108 * To iterate over all SK records, be sure to check for the repetition of
1109 * the SK record you started with to determine when all have been traversed.
1110 *
1111 * @ingroup regfiBase
1112 */
1113_EXPORT()
1114const REGFI_SK* regfi_next_sk(REGFI_FILE* file, const REGFI_SK* sk);
1115
1116
1117/** Returns the previous SK (security) record referenced by the supplied SK record
1118 *
1119 * @param file the file from which sk is derived
1120 * @param sk   the SK record whose previous sibling SK record is desired
1121 *
1122 * @return A read-only SK structure, or NULL on failure.
1123 *
1124 * @note
1125 * SK records are included in a circular, doubly-linked list.
1126 * To iterate over all SK records, be sure to check for the repetition of
1127 * the SK record you started with to determine when all have been traversed.
1128 *
1129 * @ingroup regfiBase
1130 */
1131_EXPORT()
1132const REGFI_SK* regfi_prev_sk(REGFI_FILE* file, const REGFI_SK* sk);
1133
1134
1135/** Retrieves data for a given value.
1136 *
1137 * @param file the file from which value is derived
1138 * @param value the value whose data is desired
1139 *
1140 * @return Returns a newly allocated data structure, or NULL on failure.
1141 *         Data structures must be freed with @ref regfi_free_record.
1142 *
1143 * @ingroup regfiBase
1144 */
1145_EXPORT()
1146const REGFI_DATA* regfi_fetch_data(REGFI_FILE* file,
1147                                   const REGFI_VK* value);
1148
1149
1150/** Locates a specific subkey of a given key.
1151 *
1152 * @param file  the file from which key is derived
1153 * @param key   the key whose subkey is desired
1154 * @param name  name of the desired subkey (case-insensitive)
1155 * @param index a return value: the index of the desired subkey.
1156 *              undefined on error
1157 *
1158 * @return true if the subkey is found, false if an error occurred or if the
1159 *         specified name could not be found. If an error occurs, messages
1160 *         will be written explaining the issue. (See regfi_log_get_str.)
1161 *
1162 * @ingroup regfiBase
1163 */
1164_EXPORT()
1165bool regfi_find_subkey(REGFI_FILE* file, const REGFI_NK* key, 
1166                       const char* name, uint32_t* index);
1167
1168
1169/** Locates a specific value of a given key.
1170 *
1171 * @param file  the file from which key is derived
1172 * @param key   the key whose value is desired
1173 * @param name  name of the desired value (case-insensitive)
1174 * @param index a return value: the index of the desired value. 
1175 *              undefined on error
1176 *
1177 * @return true if the value is found, false if an error occurred or if the
1178 *         specified name could not be found. If an error occurs, messages
1179 *         will be written explaining the issue. (See regfi_log_get_str.)
1180 *
1181 * @ingroup regfiBase
1182 */
1183_EXPORT()
1184bool regfi_find_value(REGFI_FILE* file, const REGFI_NK* key,
1185                      const char* name, uint32_t* index);
1186
1187
1188/** Retrieves a specific subkey of a given key.
1189 *
1190 * @param file  the file from which key is derived
1191 * @param key   the key whose subkey is desired
1192 * @param index the index of the desired subkey
1193 *
1194 * @return the requested subkey or NULL on error.
1195 *
1196 * @ingroup regfiBase
1197 */
1198_EXPORT()
1199const REGFI_NK* regfi_get_subkey(REGFI_FILE* file, const REGFI_NK* key, 
1200                                 uint32_t index);
1201
1202
1203/** Retrieves a specific value of a given key.
1204 *
1205 * @param file  the file from which key is derived
1206 * @param key   the key whose value is desired
1207 * @param index the index of the desired value
1208 *
1209 * @return the requested value or NULL on error.
1210 *
1211 * @ingroup regfiBase
1212 */
1213_EXPORT()
1214const REGFI_VK* regfi_get_value(REGFI_FILE* file, const REGFI_NK* key, 
1215                                uint32_t index);
1216
1217
1218
1219/** Uses a key's parent_off reference to retrieve it's parent.
1220 *
1221 * @param file  the file from which key is derived
1222 * @param key   the key whose parent is desired
1223 *
1224 * @return the requested subkey or NULL on error.
1225 *
1226 * @ingroup regfiBase
1227 */
1228_EXPORT()
1229const REGFI_NK* regfi_get_parentkey(REGFI_FILE* file, const REGFI_NK* key);
1230
1231
1232/******************************************************************************/
1233/**
1234 * @defgroup regfiIteratorLayer Iterator Layer: Primary regfi Library Interface
1235 *
1236 * This top layer of API functions provides an iterator interface which makes
1237 * traversing registry data structures easy in both single-threaded and
1238 * multi-threaded scenarios.
1239 */
1240/******************************************************************************/
1241
1242/** Creates a new iterator for the provided registry file.
1243 *
1244 * @param file The opened registry file the iterator should be created for.
1245 *
1246 * @return A newly allocated REGFI_ITERATOR.
1247 *         Must be free()d with regfi_iterator_free.
1248 *
1249 * @ingroup regfiIteratorLayer
1250 */
1251_EXPORT()
1252REGFI_ITERATOR* regfi_iterator_new(REGFI_FILE* file);
1253
1254
1255/** Frees a registry file iterator previously created by regfi_iterator_new.
1256 *
1257 * This does not affect the underlying registry file's allocation status.
1258 *
1259 * @param i the iterator to be freed
1260 *
1261 * @ingroup regfiIteratorLayer
1262 */
1263_EXPORT()
1264void regfi_iterator_free(REGFI_ITERATOR* i);
1265
1266
1267/** Traverse deeper into the registry tree at the current subkey.
1268 *
1269 * @param i the iterator
1270 *
1271 * @return  true on success, false on failure. 
1272 *          Note that subkey and value indexes are preserved.  That is, if a
1273 *          regfi_iterator_up call occurs later (reversing the effect of this
1274 *          call) then the subkey and value referenced prior to the
1275 *          regfi_iterator_down call will still be referenced.  This  makes
1276 *          depth-first iteration particularly easy.
1277 *
1278 * @ingroup regfiIteratorLayer
1279 */
1280_EXPORT()
1281bool regfi_iterator_down(REGFI_ITERATOR* i);
1282
1283
1284/** Traverse up to the current key's parent key.
1285 *
1286 * @param i the iterator
1287 *
1288 * @return  true on success, false on failure.  Any subkey or value state
1289 *          associated with the current key is lost.
1290 *
1291 * @ingroup regfiIteratorLayer
1292 */
1293_EXPORT()
1294bool regfi_iterator_up(REGFI_ITERATOR* i);
1295
1296
1297/** Traverse up to the root key of the hive.
1298 *
1299 * @param i the iterator
1300 *
1301 * @return true on success, false on failure.
1302 *
1303 * @ingroup regfiIteratorLayer
1304 */
1305_EXPORT()
1306bool regfi_iterator_to_root(REGFI_ITERATOR* i);
1307
1308
1309/** Traverse down multiple levels in the registry hive.
1310 *
1311 * XXX: This currently only accepts ASCII key names.  Need to look into
1312 *      accepting other encodings.
1313 *
1314 * @param i    the iterator
1315 * @param path a list of key names representing the path.  This list must
1316 *             contain NUL terminated strings.  The list itself is
1317 *             terminated with a NULL pointer.  All path elements must be
1318 *             keys; value names are not accepted (even as the last
1319 *             element).
1320 *
1321 * @return true on success, false on failure.  If any element of path is not
1322 *                 found, false will be returned and the iterator will remain
1323 *                 in its original position.
1324 *
1325 * @ingroup regfiIteratorLayer
1326 */
1327_EXPORT()
1328bool regfi_iterator_descend(REGFI_ITERATOR* i, const char** path);
1329
1330
1331/** Returns the currently referenced key.
1332 *
1333 * @param i the iterator
1334 *
1335 * @return A read-only key structure for the current key, or NULL on failure.
1336 *         Data structures must be freed with @ref regfi_free_record.
1337 *
1338 * @ingroup regfiIteratorLayer
1339 */
1340_EXPORT()
1341const REGFI_NK* regfi_iterator_cur_key(REGFI_ITERATOR* i);
1342
1343
1344/** Sets the internal subkey index to the first subkey referenced by the current
1345 *  key.
1346 *
1347 * @param i the iterator
1348 *
1349 * @return True if the current key has any subkeys, false otherwise.
1350 *
1351 * @ingroup regfiIteratorLayer
1352 */
1353_EXPORT()
1354bool regfi_iterator_first_subkey(REGFI_ITERATOR* i);
1355
1356
1357/** Returns the currently indexed subkey.
1358 *
1359 * @param i the iterator
1360 *
1361 * @return A newly allocated key structure for the currently referenced subkey,
1362 *         or NULL on failure.  Newly allocated keys must be freed with
1363 *         @ref regfi_free_record.
1364 *
1365 * @ingroup regfiIteratorLayer
1366 */
1367_EXPORT()
1368const REGFI_NK* regfi_iterator_cur_subkey(REGFI_ITERATOR* i);
1369
1370
1371/** Increments the internal subkey index to the next key in the subkey-list.
1372 *
1373 * @param i the iterator
1374 *
1375 * @return True if another subkey should exist, false otherwise.
1376 *
1377 * @ingroup regfiIteratorLayer
1378 */
1379_EXPORT()
1380bool regfi_iterator_next_subkey(REGFI_ITERATOR* i);
1381
1382
1383/** Searches for a subkey with a given name under the current key.
1384 *
1385 * @param i     the iterator
1386 * @param name  subkey name to search for
1387 *
1388 * @return True if such a subkey was found, false otherwise.  If a subkey is
1389 *         found, the current subkey index is set to that subkey.  Otherwise,
1390 *         the subkey index remains at the same location as before the call.
1391 *
1392 * @ingroup regfiIteratorLayer
1393 */
1394_EXPORT()
1395bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* name);
1396
1397
1398/** Sets the internal value index to the first value referenced by the current
1399 *  key.
1400 *
1401 * @param i the iterator
1402 *
1403 * @return True if the current key has any values, false otherwise.
1404 *
1405 * @ingroup regfiIteratorLayer
1406 */
1407_EXPORT()
1408bool regfi_iterator_first_value(REGFI_ITERATOR* i);
1409
1410
1411/** Returns the currently indexed value.
1412 *
1413 * @param i the iterator
1414 *
1415 * @return A newly allocated value structure for the currently referenced value,
1416 *         or NULL on failure.  Newly allocated values must be freed with
1417 *         @ref regfi_free_record.
1418 *
1419 * @ingroup regfiIteratorLayer
1420 */
1421_EXPORT()
1422const REGFI_VK* regfi_iterator_cur_value(REGFI_ITERATOR* i);
1423
1424
1425/** Increments the internal value index to the next value in the value-list.
1426 *
1427 * @param i the iterator
1428 *
1429 * @return True if another value should exist, false otherwise.
1430 *
1431 * @ingroup regfiIteratorLayer
1432 */
1433_EXPORT()
1434bool regfi_iterator_next_value(REGFI_ITERATOR* i);
1435
1436
1437/** Searches for a value with a given name under the current key.
1438 *
1439 * @param i     the iterator
1440 * @param name  value name to search for
1441 *
1442 * @return True if such a value was found, false otherwise.  If a value is
1443 *         found, the current value index is set to that value.  Otherwise,
1444 *         the value index remains at the same location as before the call.
1445 *
1446 * @ingroup regfiIteratorLayer
1447 */
1448_EXPORT()
1449bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* name);
1450
1451
1452/** Returns the current key and all parent keys as a list of NK records
1453 *
1454 * @param i     the iterator
1455 *
1456 * @return An array of NK record pointers terminated by a NULL pointer. 
1457 *         This array may be passed directly to regfi_free_record to free
1458 *         the entire array.
1459 *
1460 * @note In order to use an element of the array independently from the
1461 *       array (that is, to hold a pointer to an individual NK record while
1462 *       freeing the remaining array), callers must first use
1463 *       regfi_reference_record on the elements to be kept.
1464 *
1465 * @ingroup regfiIteratorLayer
1466 */
1467_EXPORT()
1468const REGFI_NK** regfi_iterator_ancestry(REGFI_ITERATOR* i);
1469
1470
1471/******************************************************************************/
1472/**
1473 * @defgroup regfiGlueLayer Glue Layer: Logical Data Structure Loading
1474 */
1475/******************************************************************************/
1476
1477/** Loads a key and associated data structures given a file offset.
1478 *
1479 * XXX: finish documenting
1480 *
1481 * @ingroup regfiGlueLayer
1482 */
1483_EXPORT()
1484REGFI_NK* regfi_load_key(REGFI_FILE* file, uint32_t offset, 
1485                         bool strict);
1486
1487
1488/** Loads a value at a given file offset alng with associated data structures.
1489 *
1490 * XXX: finish documenting
1491 *
1492 * @ingroup regfiGlueLayer
1493 */
1494_EXPORT()
1495REGFI_VK* regfi_load_value(REGFI_FILE* file, uint32_t offset, 
1496                           bool strict);
1497
1498
1499/** Loads a logical subkey list in its entirety which may span multiple records.
1500 *
1501 * XXX: finish documenting
1502 *
1503 * @ingroup regfiGlueLayer
1504 */
1505_EXPORT()
1506REGFI_SUBKEY_LIST* regfi_load_subkeylist(REGFI_FILE* file, uint32_t offset,
1507                                         uint32_t num_keys, uint32_t max_size,
1508                                         bool strict);
1509
1510
1511/** Loads a valuelist.
1512 *
1513 * XXX: finish documenting
1514 *
1515 * @ingroup regfiGlueLayer
1516 */
1517_EXPORT()
1518REGFI_VALUE_LIST* regfi_load_valuelist(REGFI_FILE* file, uint32_t offset, 
1519                                       uint32_t num_values, uint32_t max_size,
1520                                       bool strict);
1521
1522
1523/** Loads a data record which may be contained in the virtual offset, in a
1524 *  single cell, or in multiple cells through big data records.
1525 *
1526 * XXX: finish documenting
1527 *
1528 * @ingroup regfiGlueLayer
1529 */
1530_EXPORT()
1531REGFI_BUFFER regfi_load_data(REGFI_FILE* file, uint32_t voffset,
1532                             uint32_t length, bool data_in_offset,
1533                             bool strict);
1534
1535
1536/** Loads the data associated with a big data record at the specified offset.
1537 *
1538 * XXX: finish documenting
1539 *
1540 * @ingroup regfiGlueLayer
1541 */
1542_EXPORT()
1543REGFI_BUFFER regfi_load_big_data(REGFI_FILE* file, uint32_t offset, 
1544                                 uint32_t data_length,uint32_t cell_length,
1545                                 range_list* used_ranges,
1546                                 bool strict);
1547
1548
1549/** Given raw data, attempts to interpret the data based on a specified registry
1550 *  data type.
1551 *
1552 * XXX: finish documenting
1553 *
1554 * @ingroup regfiGlueLayer
1555 */
1556_EXPORT()
1557bool regfi_interpret_data(REGFI_FILE* file, 
1558                          uint32_t type, REGFI_DATA* data);
1559
1560
1561
1562/* These are cached so return values don't need to be freed. */
1563
1564/** Loads an "sk" security record at the specified offset.
1565 *
1566 * XXX: finish documenting
1567 *
1568 * @ingroup regfiGlueLayer
1569 */
1570_EXPORT()
1571const REGFI_SK* regfi_load_sk(REGFI_FILE* file, uint32_t offset,
1572                              bool strict);
1573
1574
1575
1576
1577/** Retrieves the HBIN data structure stored at the specified offset.
1578 *
1579 * XXX: finish documenting
1580 *
1581 * @ingroup regfiGlueLayer
1582 */
1583_EXPORT()
1584const REGFI_HBIN* regfi_lookup_hbin(REGFI_FILE* file, uint32_t offset);
1585
1586
1587
1588/******************************************************************************/
1589/**
1590 * @defgroup regfiParseLayer Parsing Layer: Direct Data Structure Access
1591 */
1592/******************************************************************************/
1593
1594_EXPORT()
1595REGFI_FILE* regfi_parse_regf(REGFI_RAW_FILE* file_cb, bool strict);
1596
1597_EXPORT()
1598REGFI_HBIN* regfi_parse_hbin(REGFI_FILE* file, uint32_t offset, 
1599                             bool strict);
1600
1601
1602/** Parses an NK record at the specified offset
1603 *
1604 * @param file     the registry file structure
1605 * @param offset   the offset of the cell (not the record) to be parsed.
1606 * @param max_size the maximum size the NK cell could be. (for validation)
1607 * @param strict   if true, rejects any malformed records.  Otherwise,
1608 *                 tries to minimally validate integrity.
1609 *
1610 * @return A newly allocated NK record structure, or NULL on failure.
1611 *
1612 * @ingroup regfiParseLayer
1613 */
1614_EXPORT()
1615REGFI_NK* regfi_parse_nk(REGFI_FILE* file, uint32_t offset,
1616                             uint32_t max_size, bool strict);
1617
1618
1619/** Parses a single cell containing a subkey-list record.
1620 *
1621 * XXX: finish documenting
1622 *
1623 * @ingroup regfiParseLayer
1624 */
1625_EXPORT()
1626REGFI_SUBKEY_LIST* regfi_parse_subkeylist(REGFI_FILE* file, uint32_t offset,
1627                                          uint32_t max_size, bool strict);
1628
1629
1630/** Parses a VK (value) record at the specified offset
1631 *
1632 * XXX: finish documenting
1633 *
1634 * @ingroup regfiParseLayer
1635 */
1636_EXPORT()
1637REGFI_VK* regfi_parse_vk(REGFI_FILE* file, uint32_t offset, 
1638                             uint32_t max_size, bool strict);
1639
1640
1641/** Parses an SK (security) record at the specified offset
1642 *
1643 * XXX: finish documenting
1644 *
1645 * @ingroup regfiParseLayer
1646 */
1647_EXPORT()
1648REGFI_SK* regfi_parse_sk(REGFI_FILE* file, uint32_t offset, 
1649                             uint32_t max_size, bool strict);
1650
1651
1652/** Retrieves information on all cells in the registry hive which are
1653 *  currently in the unallocated status. 
1654 *
1655 * The unallocated status is determined based soley on the cell length sign.
1656 *
1657 * XXX: finish documenting
1658 *
1659 * @ingroup regfiParseLayer
1660 */
1661_EXPORT()
1662range_list* regfi_parse_unalloc_cells(REGFI_FILE* file);
1663
1664
1665/** Helper function to parse a cell
1666 *
1667 * XXX: finish documenting
1668 *
1669 * @ingroup regfiParseLayer
1670 */
1671_EXPORT()
1672bool regfi_parse_cell(REGFI_RAW_FILE* file_cb, uint32_t offset,
1673                      uint8_t* hdr, uint32_t hdr_len,
1674                      uint32_t* cell_length, bool* unalloc);
1675
1676
1677/** Parses a classname cell
1678 *
1679 * XXX: finish documenting
1680 *
1681 * @ingroup regfiParseLayer
1682 */
1683_EXPORT()
1684uint8_t* regfi_parse_classname(REGFI_FILE* file, uint32_t offset,
1685                               uint16_t* name_length, 
1686                               uint32_t max_size, bool strict);
1687
1688
1689/** Parses a single-cell data record
1690 *
1691 * XXX: finish documenting
1692 *
1693 * @ingroup regfiParseLayer
1694 */
1695_EXPORT()
1696REGFI_BUFFER regfi_parse_data(REGFI_FILE* file, uint32_t offset,
1697                              uint32_t length, bool strict);
1698
1699
1700/** Parses a "little data" record which is stored entirely within the
1701 *  provided virtual offset.
1702 *
1703 * XXX: finish documenting
1704 *
1705 * @ingroup regfiParseLayer
1706 */
1707_EXPORT()
1708REGFI_BUFFER regfi_parse_little_data(REGFI_FILE* file, uint32_t voffset, 
1709                                     uint32_t length, bool strict);
1710
1711
1712/******************************************************************************/
1713/*    Private (and undocumented) Functions                                    */
1714/******************************************************************************/
1715int64_t               regfi_raw_seek(REGFI_RAW_FILE* self, 
1716                                     uint64_t offset, int whence);
1717ssize_t               regfi_raw_read(REGFI_RAW_FILE* self, 
1718                                     void* buf, size_t count);
1719_EXPORT()
1720uint64_t              regfi_seek(REGFI_RAW_FILE* file_cb, 
1721                                 uint64_t offset, int whence);
1722_EXPORT()
1723uint32_t              regfi_read(REGFI_RAW_FILE* file_cb, 
1724                                 uint8_t* buf, uint32_t* length);
1725
1726_EXPORT()
1727const char*           regfi_type_val2str(unsigned int val);
1728_EXPORT()
1729int                   regfi_type_str2val(const char* str);
1730
1731_EXPORT()
1732char*                 regfi_get_sacl(WINSEC_DESC* sec_desc);
1733_EXPORT()
1734char*                 regfi_get_dacl(WINSEC_DESC* sec_desc);
1735_EXPORT()
1736char*                 regfi_get_owner(WINSEC_DESC* sec_desc);
1737_EXPORT()
1738char*                 regfi_get_group(WINSEC_DESC* sec_desc);
1739
1740REGFI_SUBKEY_LIST*    regfi_merge_subkeylists(uint16_t num_lists, 
1741                                              REGFI_SUBKEY_LIST** lists,
1742                                              bool strict);
1743REGFI_SUBKEY_LIST*    regfi_load_subkeylist_aux(REGFI_FILE* file, uint32_t offset,
1744                                                uint32_t max_size, bool strict,
1745                                                uint8_t depth_left);
1746void                  regfi_add_message(REGFI_FILE* file, uint16_t msg_type, 
1747                                        const char* fmt, ...);
1748REGFI_NK*             regfi_copy_nk(const REGFI_NK* nk);
1749REGFI_VK*             regfi_copy_vk(const REGFI_VK* vk);
1750_EXPORT()
1751int32_t               regfi_calc_maxsize(REGFI_FILE* file, uint32_t offset);
1752REGFI_BUFFER          regfi_conv_charset(const char* input_charset, const char* output_charset,
1753                                         uint8_t* input, uint32_t input_len);
1754_EXPORT()
1755REGFI_DATA*           regfi_buffer_to_data(REGFI_BUFFER raw_data);
1756
1757/* XXX: move to base API and document */
1758_EXPORT()
1759REGFI_NTTIME          regfi_unix2nt_time(time_t t);
1760_EXPORT()
1761double                regfi_nt2unix_time(REGFI_NTTIME nt);
1762
1763
1764_EXPORT()
1765void regfi_interpret_keyname(REGFI_FILE* file, REGFI_NK* nk, bool strict);
1766_EXPORT()
1767void regfi_interpret_valuename(REGFI_FILE* file, REGFI_VK* vk, bool strict);
1768
1769_EXPORT()
1770void regfi_init();
1771
1772
1773#endif  /* _REGFI_H */
Note: See TracBrowser for help on using the repository browser.