source: trunk/include/regfi.h @ 262

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

changed regfi_conv_charset to handle memory allocation
tweaked test cases
corrected some documentation

  • Property svn:keywords set to Id
File size: 50.4 KB
RevLine 
[169]1/*
[262]2 * Copyright (C) 2005-2011 Timothy D. Morgan
[193]3 * Copyright (C) 2010 Michael Cohen
[30]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
[134]8 * the Free Software Foundation; version 3 of the License.
[30]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 262 2011-06-17 17:51:31Z tim $
20 */
21
[169]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 */
[30]34
[168]35/**
36 * @mainpage Home
37 *
[169]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
[185]51 * will be sufficient for accessing registry hive files.  Those who are willing
[169]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.
[168]55 */
56
[169]57
[78]58#ifndef _REGFI_H
59#define _REGFI_H
[30]60
[31]61#include <stdlib.h>
62#include <stdio.h>
[30]63#include <stdbool.h>
[31]64#include <string.h>
[30]65#include <errno.h>
[31]66#include <time.h>
[30]67#include <fcntl.h>
68#include <sys/stat.h>
69#include <sys/types.h>
70#include <unistd.h>
[159]71#include <iconv.h>
[180]72#include <pthread.h>
[201]73#include <talloc.h>
[30]74
[193]75/* regfi headers */
[253]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"
[30]82
83/******************************************************************************/
[184]84/* Constants for use while interacting with the library                       */
85/******************************************************************************/
[138]86
87/* regfi library error message types */
[182]88#define REGFI_LOG_INFO  0x0001
89#define REGFI_LOG_WARN  0x0004
90#define REGFI_LOG_ERROR 0x0010
[185]91#define REGFI_DEFAULT_LOG_MASK REGFI_LOG_ERROR|REGFI_LOG_WARN
[138]92
[161]93/* regfi library supported character encodings */
94/* UTF16LE is not supported for output */
[193]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;
[161]102
[32]103/* Registry data types */
[193]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 */
[72]118/* XXX: Has MS defined a REG_QWORD_BE? */
[32]119/* Not a real type in the registry */
[193]120  REG_KEY                 =   0x7FFFFFFF
121} REGFI_DATA_TYPE;
[135]122#define REGFI_OFFSET_NONE          0xffffffff
[30]123
[165]124
[184]125
126/******************************************************************************/
127/* Various resource limits and related constants                              */
128/******************************************************************************/
129
[250]130/* Flags determining how many records to cache internally */
131#define REGFI_CACHE_SK_MAX         64
132#define REGFI_CACHE_NK_MAX         1024
[184]133
[165]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 */
[149]145#define REGFI_MAX_SUBKEY_DEPTH     255
[139]146
[165]147
[184]148/******************************************************************************/
149/* Symbols for internal use                                                   */
150/******************************************************************************/
151
152/* Global thread-local storage key */
[185]153pthread_key_t regfi_log_key;
[184]154
[135]155/* Header sizes and magic number lengths for various records */
[147]156#define REGFI_HBIN_ALLOC           0x1000 /* Minimum allocation unit for HBINs */
157#define REGFI_REGF_SIZE            0x1000 /* "regf" header block size */
[135]158#define REGFI_REGF_MAGIC_SIZE      4
[151]159#define REGFI_REGF_NAME_SIZE       64
160#define REGFI_REGF_RESERVED1_SIZE  340
161#define REGFI_REGF_RESERVED2_SIZE  3528
[135]162#define REGFI_HBIN_MAGIC_SIZE      4
163#define REGFI_CELL_MAGIC_SIZE      2
164#define REGFI_HBIN_HEADER_SIZE     0x20
[116]165#define REGFI_NK_MIN_LENGTH        0x4C
166#define REGFI_VK_MIN_LENGTH        0x14
167#define REGFI_SK_MIN_LENGTH        0x14
[127]168#define REGFI_SUBKEY_LIST_MIN_LEN  0x4
[157]169#define REGFI_BIG_DATA_MIN_LENGTH  0xC
[30]170
[135]171
[116]172/* Constants used for validation */
[125]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
[147]176 *      eliminate false positives.  Would need to verify this and
[125]177 *      perhaps conservatively implement a check.
178 */
[116]179 /* Minimum time is Jan 1, 1990 00:00:00 */
[251]180#define REGFI_MTIME_MIN            0x01B41E6D00000000L
[178]181
[116]182 /* Maximum time is Jan 1, 2290 00:00:00
183  * (We hope no one is using Windows by then...)
184  */
[251]185#define REGFI_MTIME_MAX            0x0304754300000000L
[30]186
[116]187
[30]188/* Flags for the vk records */
[162]189#define REGFI_VK_FLAG_ASCIINAME    0x0001
[135]190#define REGFI_VK_DATA_IN_OFFSET    0x80000000
[152]191#define REGFI_VK_MAX_DATA_LENGTH   1024*1024  /* XXX: This is arbitrary */
[30]192
[137]193
[152]194/* Known key flags */
195/*******************/
[137]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
[152]199
[167]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 */
[137]207#define REGFI_NK_FLAG_UNKNOWN3     0x0080
[30]208
[152]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
[137]214
[152]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\
[167]248                                    | REGFI_NK_FLAG_UNKNOWN2\
249                                    | REGFI_NK_FLAG_UNKNOWN3)
[152]250
[168]251
[193]252#ifndef CHAR_BIT
[168]253#define CHAR_BIT 8
[193]254#endif
255
[168]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)
[251]259#define REGFI_TIME_FIXUP (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
[168]260
[184]261
262
263/******************************************************************************/
264/* Structures                                                                 */
265/******************************************************************************/
266
[251]267typedef uint64_t REGFI_NTTIME;
[168]268
[182]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
[169]280/** HBIN block information
281 * @ingroup regfiMiddleLayer
282 */
[135]283typedef struct _regfi_hbin
[97]284{
[169]285  /** Offset of this HBIN in the registry file */
286  uint32_t file_off;
[97]287
[169]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];
[135]305} REGFI_HBIN;
[30]306
[97]307
[127]308/* Subkey List -- list of key offsets and hashed names for consistency */
[97]309typedef struct 
310{
[139]311  /* Virtual offset of NK record or additional subkey list,
312   * depending on this list's type.
313   */
[168]314  uint32_t offset;
[139]315
[168]316  uint32_t hash;
[135]317} REGFI_SUBKEY_LIST_ELEM;
[30]318
[97]319
[169]320/** Subkey-list structure
321 * @ingroup regfiMiddleLayer
322 */
[145]323typedef struct _regfi_subkey_list
[97]324{
[139]325  /* Real offset of this record's cell in the file */
[168]326  uint32_t offset;
[139]327
[168]328  uint32_t cell_size;
[139]329 
330  /* Number of immediate children */
[202]331  uint32_t num_children;
[139]332
[202]333  /* Total number of keys referenced by this list and its children */
334  uint32_t num_keys;
[139]335
[135]336  REGFI_SUBKEY_LIST_ELEM* elements;
[168]337  uint8_t magic[REGFI_CELL_MAGIC_SIZE];
[139]338
339  /* Set if the magic indicates this subkey list points to child subkey lists */
[202]340  bool recursive_type;
[135]341} REGFI_SUBKEY_LIST;
[30]342
[97]343
[168]344typedef uint32_t REGFI_VALUE_LIST_ELEM;
[169]345/** Value-list structure
346 * @ingroup regfiMiddleLayer
347 */
[145]348typedef struct _regfi_value_list
349{
[206]350  /* Real offset of this record's cell in the file */
351  uint32_t offset;
352
353  uint32_t cell_size;
354
[145]355  /* Actual number of values referenced by this list. 
356   * May differ from parent key's num_values if there were parsing errors.
357   */
[168]358  uint32_t num_values;
[145]359
360  REGFI_VALUE_LIST_ELEM* elements;
361} REGFI_VALUE_LIST;
362
363
[169]364/** Class name structure (used in storing SysKeys)
365 * @ingroup regfiBase
366 */
[160]367typedef struct _regfi_classname
368{
[206]369  /** Real offset of this record's cell in the file */
370  uint32_t offset;
371
[169]372  /** As converted to requested REGFI_ENCODING */
[160]373  char* interpreted;
374
[169]375  /** Represents raw buffer read from classname cell.
376   *
377   * Length of this item is specified in the size field.
378   */
[168]379  uint8_t* raw;
[160]380
[169]381  /** Length of the raw data.
382   *
383   * May be shorter than that indicated by parent key.
384   */
[168]385  uint16_t size;
[160]386} REGFI_CLASSNAME;
387
388
[169]389/** Data record structure
390 * @ingroup regfiBase
391 */
[159]392typedef struct _regfi_data
393{
[209]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
[169]399  /** Data type of this data, as indicated by the referencing VK record. */
[193]400  REGFI_DATA_TYPE type;
[159]401
[169]402  /** Length of the raw data. */
[168]403  uint32_t size;
[159]404
[169]405  /** This is always present, representing the raw data cell contents. */
[168]406  uint8_t* raw;
[159]407
[209]408  /** Represents the length of the interpreted value. Meaning is type-specific.
409   *  Will be 0 if interpretation failed for any reason.
410   */
[168]411  uint32_t interpreted_size;
[159]412
[169]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.
[159]417   */
418  union _regfi_data_interpreted
419  {
[169]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     */
[168]432    uint8_t* string;
[169]433
434    /** REG_EXPAND_SZ
435     *
436     * Stored as a NUL terminated string.  Converted to the specified
437     * REGFI_ENCODING.
438     */
[168]439    uint8_t* expand_string;
[169]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 */
[168]449    uint32_t dword;
[169]450
451    /** REG_DWORD_BE */
[168]452    uint32_t dword_be;
[169]453
454    /** REG_LINK
455     *
456     * Stored as a NUL terminated string.  Converted to the specified
457     * REGFI_ENCODING.
458     */
[168]459    uint8_t* link;
[169]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     */
[168]467    uint8_t** multiple_string;
[169]468
469    /** REG_QWORD */
[168]470    uint64_t qword;
[159]471
472    /* The following are treated as binary currently, but this may change in
473     * the future as the formats become better understood.
474     */
[169]475
476    /** REG_RESOURCE_LIST
477     *
478     * Stored as a raw buffer.  Use REGFI_DATA::interpreted_size to determine
479     * length.
480     */
[168]481    uint8_t* resource_list;
[169]482
483    /** REG_FULL_RESOURCE_DESCRIPTOR
484     *
485     * Stored as a raw buffer.  Use REGFI_DATA::interpreted_size to determine
486     * length.
487     */
[168]488    uint8_t* full_resource_descriptor;
[169]489
490    /** REG_RESOURCE_REQUIREMENTS_LIST
491     *
492     * Stored as a raw buffer.  Use REGFI_DATA::interpreted_size to determine
493     * length.
494     */
[168]495    uint8_t* resource_requirements_list;
[159]496  } interpreted;
497} REGFI_DATA;
498
499
[169]500/** Value structure
501 * @ingroup regfiBase
502 */
[206]503typedef struct _regfi_vk
[97]504{
[169]505  /** Real offset of this record's cell in the file */
506  uint32_t offset;     
[101]507
[169]508  /** ((start_offset - end_offset) & 0xfffffff8) */
509  uint32_t cell_size;
[159]510
[169]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   */
[206]516  char* name;
[169]517
518  /** The raw value name
519   *
520   * Length of the buffer is stored in name_length.
521   */
[206]522  uint8_t* name_raw;
[169]523
[206]524  /** Length of name_raw */
[168]525  uint16_t name_length;
[169]526
527  /** Offset from beginning of this hbin block */
528  uint32_t hbin_off;
[53]529 
[169]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 */
[193]540  REGFI_DATA_TYPE type;
[169]541
542  /** VK record's magic number (should be "vk") */
[168]543  uint8_t  magic[REGFI_CELL_MAGIC_SIZE];
[169]544
545  /** VK record flags */
[168]546  uint16_t flags;
[169]547
548  /* XXX: A 2-byte field of unknown purpose stored in the VK record */
[168]549  uint16_t unknown1;
[169]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;
[206]556
557  /* XXX: deprecated */
558  REGFI_DATA* data;
559
[203]560} REGFI_VK;
[30]561
562
563/* Key Security */
[206]564struct _regfi_sk;
[30]565
[169]566/** Security structure
567 * @ingroup regfiBase
568 */
[206]569typedef struct _regfi_sk
[97]570{
[169]571  /** Real file offset of this record */
572  uint32_t offset;
[111]573
[169]574  /** ((start_offset - end_offset) & 0xfffffff8) */
575  uint32_t cell_size;
576
577  /** The stored Windows security descriptor for this SK record */
[134]578  WINSEC_DESC* sec_desc;
[169]579
580  /** Offset of this record from beginning of this hbin block */
581  uint32_t hbin_off;
[53]582 
[169]583  /** Offset of the previous SK record in the linked list of SK records */
[168]584  uint32_t prev_sk_off;
[169]585
586  /** Offset of the next SK record in the linked list of SK records */
[168]587  uint32_t next_sk_off;
[169]588
589  /** Number of keys referencing this SK record */
[168]590  uint32_t ref_count;
[169]591
592  /** Size of security descriptor (sec_desc) */
593  uint32_t desc_size;
594
595  /* XXX: A 2-byte field of unknown purpose */
[168]596  uint16_t unknown_tag;
[169]597
598  /** The magic number for this record (should be "sk") */
[168]599  uint8_t  magic[REGFI_CELL_MAGIC_SIZE];
[203]600} REGFI_SK;
[30]601
[81]602
[169]603/** Key structure
604 * @ingroup regfiBase
605 */
[206]606typedef struct _regfi_nk
[97]607{
[169]608  /** Real offset of this record's cell in the file */
609  uint32_t offset;
[84]610
[169]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   */
[145]620  REGFI_VALUE_LIST* values;
[169]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   */
[135]627  REGFI_SUBKEY_LIST* subkeys;
[53]628 
[169]629  /** Key flags */
[168]630  uint16_t flags;
[169]631
632  /** Magic number of key (should be "nk") */
[168]633  uint8_t  magic[REGFI_CELL_MAGIC_SIZE];
[169]634
635  /** Key's last modification time */
[168]636  REGFI_NTTIME mtime;
[169]637
[249]638  /** Length of name_raw */
[168]639  uint16_t name_length;
[169]640
641  /** Length of referenced classname */
[168]642  uint16_t classname_length;
[169]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   */
[206]649  char* name;
[169]650
651  /** The raw key name
652   *
653   * Length of the buffer is stored in name_length.
654   */
[206]655  uint8_t* name_raw;
[169]656
[215]657  /** Virtual offset of parent key */
[169]658  uint32_t parent_off;
659
[215]660  /** Virtual offset of classname key */
[168]661  uint32_t classname_off;
[53]662 
[169]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
[206]669  /* XXX: max value name * 2 */
[169]670  uint32_t max_bytes_valuename;
671
672  /* XXX: max value data size */
673  uint32_t max_bytes_value;
[53]674 
[169]675  /* XXX: Fields of unknown purpose */
[168]676  uint32_t unknown1;
677  uint32_t unknown2;
678  uint32_t unknown3;
679  uint32_t unk_index;               /* nigel says run time index ? */
[53]680 
[169]681  /** Number of subkeys */
[168]682  uint32_t num_subkeys;
[169]683
684  /** Virtual offset of subkey-list */
685  uint32_t subkeys_off;
686
687  /** Number of values for this key */
[168]688  uint32_t num_values;
[169]689
690  /** Virtual offset of value-list */
691  uint32_t values_off;
692
693  /** Virtual offset of SK record */
694  uint32_t sk_off;
[203]695} REGFI_NK;
[30]696
[81]697
[178]698typedef struct _regfi_raw_file
699{
[226]700  int64_t  (* seek)(); /* (REGFI_RAW_FILE* self, uint64_t offset, int whence) */
[178]701  ssize_t  (* read)(); /* (REGFI_RAW_FILE* self, void* buf, size_t count) */
[97]702
[178]703  uint64_t cur_off;
704  uint64_t size;
705  void*    state;
706} REGFI_RAW_FILE;
707
708
[169]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
[238]714 * the registry hive.  These can be tuned using @ref regfi_log_set_mask. 
[202]715 * Messages may be retrieved using @ref regfi_log_get_str.
[169]716 *
717 * @note If the message mask is set to record any messages, dependent code
[202]718 *       must use @ref regfi_log_get_str periodically to clear the message
[169]719 *       queue. Otherwise, this structure will grow in size over time as
720 *       messages queue up.
721 *
722 * @ingroup regfiBase
723 */ 
[178]724typedef struct _regfi_file
[97]725{
[135]726  /* Data parsed from file header */
727  /********************************/
[168]728  uint8_t  magic[REGFI_REGF_MAGIC_SIZE];/* "regf" */
[151]729
730 /* These sequence numbers should match if
731  * the hive was properly synced to disk.
732  */
[168]733  uint32_t sequence1;           
734  uint32_t sequence2;
[151]735
[168]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 */
[97]741
[168]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 */
[151]744
[168]745  uint32_t cluster;    /* XXX: Unverified. Set to 1 in all known hives */
[151]746
747  /* Matches hive's base file name. Stored in UTF-16LE */
[168]748  uint8_t file_name[REGFI_REGF_NAME_SIZE];
[151]749
750  WINSEC_UUID* rm_id;       /* XXX: Unverified. */
751  WINSEC_UUID* log_id;      /* XXX: Unverified. */
752  WINSEC_UUID* tm_id;       /* XXX: Unverified. */
[168]753  uint32_t flags;             /* XXX: Unverified. */
754  uint32_t guid_signature;    /* XXX: Unverified. */
[151]755
[168]756  uint32_t checksum;          /* Stored checksum from file */
757  uint32_t computed_checksum; /* Our own calculation of the checksum.
[151]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. */
[168]763  uint32_t boot_type;         /* XXX: Unverified. */
764  uint32_t boot_recover;      /* XXX: Unverified. */
[151]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. */
[168]769  uint8_t reserved1[REGFI_REGF_RESERVED1_SIZE];
[151]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. */
[168]774  uint8_t reserved2[REGFI_REGF_RESERVED2_SIZE];
[151]775
[203]776
777  /* Run-time information */
778  /************************/
[206]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
[203]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
[250]800  /* Small number of SK records cached */
[203]801  lru_cache* sk_cache;
802
803  /* Need exclusive access for LRUs, since lookups make changes */
804  pthread_mutex_t sk_lock;
805
[250]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
[228]812  /* Needed to protect various talloc calls */
813  pthread_mutex_t mem_lock;
814
[135]815} REGFI_FILE;
[30]816
817
[249]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
[169]838/** Registry hive iterator
839 * @ingroup regfiIteratorLayer
840 */
[151]841typedef struct _regfi_iterator
[97]842{
[169]843  /** The registry hive this iterator is associated with */
[135]844  REGFI_FILE* f;
[169]845
846  /** All current parent keys and associated iterator positions */
[80]847  void_stack* key_positions;
[169]848
[249]849  REGFI_ITER_POSITION* cur;
[78]850} REGFI_ITERATOR;
851
[80]852
853
[169]854/** General purpose buffer with stored length
855 * @ingroup regfiBottomLayer
856 */
[151]857typedef struct _regfi_buffer
858{
[168]859  uint8_t* buf;
[151]860  uint32_t len;
861} REGFI_BUFFER;
862
863
[169]864
[54]865/******************************************************************************/
[168]866/**
[169]867 * @defgroup regfiBase Base Layer: Essential Functions and Data Structures
[168]868 *
[169]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.
[168]871 */
[135]872/******************************************************************************/
[166]873
[233]874
[238]875
[233]876/** Returns the current regfi library version
877 *
878 * @return A string indicating the version.
879 *
880 * @ingroup regfiBase
881 */
[238]882_EXPORT()
[233]883const char* regfi_version();
884
885
[168]886/** Parses file headers of an already open registry hive file and
887 *  allocates related structures for further parsing.
[166]888 *
[168]889 * @param fd A file descriptor of an already open file.  Must be seekable.
[166]890 *
[258]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 *
[168]899 * @return A reference to a newly allocated REGFI_FILE structure, if successful;
[184]900 *         NULL on error.  Use regfi_free to free the returned REGFI_FILE.
[168]901 *
902 * @ingroup regfiBase
[166]903 */
[238]904_EXPORT()
[206]905REGFI_FILE* regfi_alloc(int fd, REGFI_ENCODING output_encoding);
[166]906
907
[178]908/** Parses file headers returned by supplied callback functions.
[166]909 *
[178]910 * This interface is useful if you have a registry hive in memory
911 * or have some other reason to emulate a real file.
[166]912 *
[178]913 * @param file_cb A structure defining the callback functions needed to access the file.
[168]914 *
[258]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 *
[178]923 * @return A reference to a newly allocated REGFI_FILE structure, if successful;
[184]924 *         NULL on error.  Use regfi_free to free the returned REGFI_FILE.
[178]925 *
[168]926 * @ingroup regfiBase
[166]927 */
[238]928_EXPORT()
[206]929REGFI_FILE* regfi_alloc_cb(REGFI_RAW_FILE* file_cb,
930                           REGFI_ENCODING output_encoding);
[166]931
932
[168]933/** Frees a hive's data structures without closing the underlying file.
[166]934 *
[168]935 * @param file The registry structure to free.
936 *
937 * @ingroup regfiBase
[166]938 */
[238]939_EXPORT()
[201]940void regfi_free(REGFI_FILE* file);
[166]941
942
[168]943/** Get errors, warnings, and/or verbose information relating to processing of
944 *  the given registry file.
[135]945 *
[168]946 * @return A newly allocated char* which must be free()d by the caller.
947 *
948 * @ingroup regfiBase
[135]949 */
[238]950_EXPORT()
[182]951char* regfi_log_get_str();
[159]952
[166]953
[185]954/** Set the verbosity level of messages generated by the library for the
955 *  current thread.
[166]956 *
[185]957 * @param mask   An integer representing the types of messages desired.
[166]958 *               Acceptable values are created through bitwise ORs of
[185]959 *               REGFI_LOG_* values.  For instance, if only errors and
[166]960 *               informational messages were desired (but not warnings),
[185]961 *               then one would specify: REGFI_LOG_ERROR|REGFI_LOG_INFO
962 *               By default the message mask is: REGFI_LOG_ERROR|REGFI_LOG_WARN.
[166]963 *
[185]964 * @return       true on success and false on failure.  Failure occurs if
965 *               underlying pthread functions fail.  errno is set in this case.
[182]966 *
[185]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.
[182]971 *
[185]972 * @note When a non-zero message mask is set, messages will
973 *       accumulate in memory without limit if they are not fetched using
[258]974 *       @ref regfi_log_get_str and subsequently freed by the caller.  It is
[185]975 *       recommended that messsages be fetched after each regfi API call in
976 *       order to provide the most context.
977 *
[182]978 * @ingroup regfiBase
979 */
[238]980_EXPORT()
[185]981bool regfi_log_set_mask(uint16_t mask);
[182]982
983
[215]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 */
[238]991_EXPORT()
[215]992const REGFI_NK*       regfi_get_rootkey(REGFI_FILE* file);
[168]993
[215]994
[184]995/** Frees a record previously returned by one of the API functions.
[159]996 *
[228]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.
[159]1002 *
[228]1003 * @note The "const" in the record data type is a bit misleading and is there just for
[184]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
[215]1006 * function is const to make passing those records back easy.
[168]1007 *
1008 * @ingroup regfiBase
1009 */
[238]1010_EXPORT()
[228]1011void regfi_free_record(REGFI_FILE* file, const void* record);
[168]1012
1013
[224]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
[252]1018 * useful in cases where multiple threads/structures need access to a shared record,
[224]1019 * without requiring them to be in sync with when it is freed.
1020 *
[228]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.
[224]1026 *
[252]1027 * @return Updated record pointer on success, NULL otherwise
[224]1028 *
[252]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 *
[224]1035 * @ingroup regfiBase
1036 */
[238]1037_EXPORT()
[252]1038const void* regfi_reference_record(REGFI_FILE* file, const void* record);
[224]1039
1040
[207]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 */
[238]1052_EXPORT()
[207]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 */
[238]1067_EXPORT()
[207]1068uint32_t regfi_fetch_num_values(const REGFI_NK* key);
1069
1070
[206]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 */
[238]1081_EXPORT()
[206]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 */
[238]1095_EXPORT()
[206]1096const REGFI_SK* regfi_fetch_sk(REGFI_FILE* file, const REGFI_NK* key);
1097
1098
[253]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
[206]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 */
[238]1145_EXPORT()
[206]1146const REGFI_DATA* regfi_fetch_data(REGFI_FILE* file,
1147                                   const REGFI_VK* value);
1148
1149
[207]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
[262]1154 * @param name  name of the desired subkey (case-insensitive)
[207]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 */
[238]1164_EXPORT()
[207]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
[262]1173 * @param name  name of the desired value (case-insensitive)
[207]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 */
[238]1183_EXPORT()
[207]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 */
[238]1198_EXPORT()
[207]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 */
[238]1213_EXPORT()
[207]1214const REGFI_VK* regfi_get_value(REGFI_FILE* file, const REGFI_NK* key, 
1215                                uint32_t index);
1216
1217
1218
[215]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 */
[238]1228_EXPORT()
[215]1229const REGFI_NK* regfi_get_parentkey(REGFI_FILE* file, const REGFI_NK* key);
1230
1231
[168]1232/******************************************************************************/
1233/**
[169]1234 * @defgroup regfiIteratorLayer Iterator Layer: Primary regfi Library Interface
[168]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 *
[169]1249 * @ingroup regfiIteratorLayer
[168]1250 */
[238]1251_EXPORT()
[206]1252REGFI_ITERATOR* regfi_iterator_new(REGFI_FILE* file);
[166]1253
1254
[168]1255/** Frees a registry file iterator previously created by regfi_iterator_new.
[166]1256 *
1257 * This does not affect the underlying registry file's allocation status.
1258 *
[168]1259 * @param i the iterator to be freed
1260 *
[169]1261 * @ingroup regfiIteratorLayer
[166]1262 */
[238]1263_EXPORT()
[201]1264void regfi_iterator_free(REGFI_ITERATOR* i);
[166]1265
1266
[168]1267/** Traverse deeper into the registry tree at the current subkey.
[166]1268 *
[168]1269 * @param i the iterator
[166]1270 *
[168]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 *
[169]1278 * @ingroup regfiIteratorLayer
[166]1279 */
[238]1280_EXPORT()
[201]1281bool regfi_iterator_down(REGFI_ITERATOR* i);
[166]1282
1283
[168]1284/** Traverse up to the current key's parent key.
[166]1285 *
[168]1286 * @param i the iterator
[166]1287 *
[168]1288 * @return  true on success, false on failure.  Any subkey or value state
1289 *          associated with the current key is lost.
1290 *
[169]1291 * @ingroup regfiIteratorLayer
[166]1292 */
[238]1293_EXPORT()
[201]1294bool regfi_iterator_up(REGFI_ITERATOR* i);
[166]1295
1296
[168]1297/** Traverse up to the root key of the hive.
[166]1298 *
[168]1299 * @param i the iterator
[166]1300 *
[168]1301 * @return true on success, false on failure.
1302 *
[169]1303 * @ingroup regfiIteratorLayer
[166]1304 */
[238]1305_EXPORT()
[201]1306bool regfi_iterator_to_root(REGFI_ITERATOR* i);
[30]1307
[166]1308
[168]1309/** Traverse down multiple levels in the registry hive.
[166]1310 *
1311 * XXX: This currently only accepts ASCII key names.  Need to look into
1312 *      accepting other encodings.
1313 *
[168]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 *
[169]1325 * @ingroup regfiIteratorLayer
[166]1326 */
[238]1327_EXPORT()
[252]1328bool regfi_iterator_descend(REGFI_ITERATOR* i, const char** path);
[166]1329
1330
[168]1331/** Returns the currently referenced key.
[166]1332 *
[168]1333 * @param i the iterator
[166]1334 *
[168]1335 * @return A read-only key structure for the current key, or NULL on failure.
[221]1336 *         Data structures must be freed with @ref regfi_free_record.
[168]1337 *
[169]1338 * @ingroup regfiIteratorLayer
[166]1339 */
[238]1340_EXPORT()
[203]1341const REGFI_NK* regfi_iterator_cur_key(REGFI_ITERATOR* i);
[166]1342
1343
[168]1344/** Sets the internal subkey index to the first subkey referenced by the current
[200]1345 *  key.
[166]1346 *
[168]1347 * @param i the iterator
[166]1348 *
[200]1349 * @return True if the current key has any subkeys, false otherwise.
[168]1350 *
[169]1351 * @ingroup regfiIteratorLayer
[166]1352 */
[238]1353_EXPORT()
[199]1354bool regfi_iterator_first_subkey(REGFI_ITERATOR* i);
[166]1355
1356
[168]1357/** Returns the currently indexed subkey.
[166]1358 *
[168]1359 * @param i the iterator
[166]1360 *
[168]1361 * @return A newly allocated key structure for the currently referenced subkey,
1362 *         or NULL on failure.  Newly allocated keys must be freed with
[185]1363 *         @ref regfi_free_record.
[168]1364 *
[169]1365 * @ingroup regfiIteratorLayer
[166]1366 */
[238]1367_EXPORT()
[203]1368const REGFI_NK* regfi_iterator_cur_subkey(REGFI_ITERATOR* i);
[167]1369
1370
[200]1371/** Increments the internal subkey index to the next key in the subkey-list.
[167]1372 *
[168]1373 * @param i the iterator
[167]1374 *
[200]1375 * @return True if another subkey should exist, false otherwise.
[168]1376 *
[169]1377 * @ingroup regfiIteratorLayer
[167]1378 */
[238]1379_EXPORT()
[199]1380bool regfi_iterator_next_subkey(REGFI_ITERATOR* i);
[167]1381
1382
[168]1383/** Searches for a subkey with a given name under the current key.
[167]1384 *
[207]1385 * @param i     the iterator
1386 * @param name  subkey name to search for
[167]1387 *
[168]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 *
[169]1392 * @ingroup regfiIteratorLayer
[167]1393 */
[238]1394_EXPORT()
[207]1395bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* name);
[150]1396
[184]1397
[168]1398/** Sets the internal value index to the first value referenced by the current
[200]1399 *  key.
[167]1400 *
[168]1401 * @param i the iterator
[167]1402 *
[200]1403 * @return True if the current key has any values, false otherwise.
[168]1404 *
[169]1405 * @ingroup regfiIteratorLayer
[167]1406 */
[238]1407_EXPORT()
[199]1408bool regfi_iterator_first_value(REGFI_ITERATOR* i);
[167]1409
1410
[168]1411/** Returns the currently indexed value.
[167]1412 *
[168]1413 * @param i the iterator
[167]1414 *
[168]1415 * @return A newly allocated value structure for the currently referenced value,
1416 *         or NULL on failure.  Newly allocated values must be freed with
[185]1417 *         @ref regfi_free_record.
[168]1418 *
[169]1419 * @ingroup regfiIteratorLayer
[167]1420 */
[238]1421_EXPORT()
[203]1422const REGFI_VK* regfi_iterator_cur_value(REGFI_ITERATOR* i);
[167]1423
1424
[200]1425/** Increments the internal value index to the next value in the value-list.
[167]1426 *
[168]1427 * @param i the iterator
[167]1428 *
[200]1429 * @return True if another value should exist, false otherwise.
[168]1430 *
[169]1431 * @ingroup regfiIteratorLayer
[167]1432 */
[238]1433_EXPORT()
[199]1434bool regfi_iterator_next_value(REGFI_ITERATOR* i);
[167]1435
1436
[168]1437/** Searches for a value with a given name under the current key.
[167]1438 *
[207]1439 * @param i     the iterator
1440 * @param name  value name to search for
[167]1441 *
[168]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 *
[169]1446 * @ingroup regfiIteratorLayer
[167]1447 */
[238]1448_EXPORT()
[207]1449bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* name);
[80]1450
[167]1451
[252]1452/** Returns the current key and all parent keys as a list of NK records
[249]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
[252]1458 *         the entire array.
[249]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()
[252]1468const REGFI_NK** regfi_iterator_ancestry(REGFI_ITERATOR* i);
[249]1469
1470
[168]1471/******************************************************************************/
1472/**
[169]1473 * @defgroup regfiGlueLayer Glue Layer: Logical Data Structure Loading
[168]1474 */
1475/******************************************************************************/
1476
[215]1477/** Loads a key and associated data structures given a file offset.
[168]1478 *
1479 * XXX: finish documenting
1480 *
[169]1481 * @ingroup regfiGlueLayer
[168]1482 */
[238]1483_EXPORT()
[203]1484REGFI_NK* regfi_load_key(REGFI_FILE* file, uint32_t offset, 
[215]1485                         REGFI_ENCODING output_encoding, 
1486                         bool strict);
[168]1487
1488
1489/** Loads a value at a given file offset alng with associated data structures.
1490 *
1491 * XXX: finish documenting
1492 *
[169]1493 * @ingroup regfiGlueLayer
[168]1494 */
[238]1495_EXPORT()
[203]1496REGFI_VK* regfi_load_value(REGFI_FILE* file, uint32_t offset, 
[258]1497                           REGFI_ENCODING output_encoding, 
1498                           bool strict);
[168]1499
1500
1501/** Loads a logical subkey list in its entirety which may span multiple records.
1502 *
1503 * XXX: finish documenting
1504 *
[169]1505 * @ingroup regfiGlueLayer
[168]1506 */
[238]1507_EXPORT()
[201]1508REGFI_SUBKEY_LIST* regfi_load_subkeylist(REGFI_FILE* file, uint32_t offset,
1509                                         uint32_t num_keys, uint32_t max_size,
1510                                         bool strict);
[168]1511
1512
1513/** Loads a valuelist.
1514 *
1515 * XXX: finish documenting
1516 *
[169]1517 * @ingroup regfiGlueLayer
[168]1518 */
[238]1519_EXPORT()
[201]1520REGFI_VALUE_LIST* regfi_load_valuelist(REGFI_FILE* file, uint32_t offset, 
1521                                       uint32_t num_values, uint32_t max_size,
1522                                       bool strict);
[127]1523
[168]1524
1525/** Loads a data record which may be contained in the virtual offset, in a
1526 *  single cell, or in multiple cells through big data records.
1527 *
1528 * XXX: finish documenting
1529 *
[169]1530 * @ingroup regfiGlueLayer
[168]1531 */
[238]1532_EXPORT()
[201]1533REGFI_BUFFER regfi_load_data(REGFI_FILE* file, uint32_t voffset,
1534                             uint32_t length, bool data_in_offset,
1535                             bool strict);
[157]1536
[168]1537
1538/** Loads the data associated with a big data record at the specified offset.
1539 *
1540 * XXX: finish documenting
1541 *
[169]1542 * @ingroup regfiGlueLayer
[168]1543 */
[238]1544_EXPORT()
[201]1545REGFI_BUFFER regfi_load_big_data(REGFI_FILE* file, uint32_t offset, 
1546                                 uint32_t data_length,uint32_t cell_length,
1547                                 range_list* used_ranges,
1548                                 bool strict);
[168]1549
1550
1551/** Given raw data, attempts to interpret the data based on a specified registry
1552 *  data type.
1553 *
1554 * XXX: finish documenting
1555 *
[169]1556 * @ingroup regfiGlueLayer
[168]1557 */
[238]1558_EXPORT()
[201]1559bool regfi_interpret_data(REGFI_FILE* file, 
1560                          REGFI_ENCODING string_encoding,
1561                          uint32_t type, REGFI_DATA* data);
[168]1562
1563
1564
[146]1565/* These are cached so return values don't need to be freed. */
[168]1566
1567/** Loads an "sk" security record at the specified offset.
1568 *
1569 * XXX: finish documenting
1570 *
[169]1571 * @ingroup regfiGlueLayer
[168]1572 */
[238]1573_EXPORT()
[203]1574const REGFI_SK* regfi_load_sk(REGFI_FILE* file, uint32_t offset,
[253]1575                              bool strict);
[146]1576
1577
[206]1578
1579
[168]1580/** Retrieves the HBIN data structure stored at the specified offset.
1581 *
1582 * XXX: finish documenting
1583 *
[169]1584 * @ingroup regfiGlueLayer
[168]1585 */
[238]1586_EXPORT()
[201]1587const REGFI_HBIN* regfi_lookup_hbin(REGFI_FILE* file, uint32_t offset);
[168]1588
1589
1590
1591/******************************************************************************/
1592/**
[169]1593 * @defgroup regfiParseLayer Parsing Layer: Direct Data Structure Access
[168]1594 */
1595/******************************************************************************/
1596
[238]1597_EXPORT()
[201]1598REGFI_FILE* regfi_parse_regf(REGFI_RAW_FILE* file_cb, bool strict);
[80]1599
[238]1600_EXPORT()
[201]1601REGFI_HBIN* regfi_parse_hbin(REGFI_FILE* file, uint32_t offset, 
1602                             bool strict);
[99]1603
[201]1604
[168]1605/** Parses an NK record at the specified offset
[99]1606 *
[168]1607 * @param file     the registry file structure
1608 * @param offset   the offset of the cell (not the record) to be parsed.
1609 * @param max_size the maximum size the NK cell could be. (for validation)
1610 * @param strict   if true, rejects any malformed records.  Otherwise,
1611 *                 tries to minimally validate integrity.
1612 *
1613 * @return A newly allocated NK record structure, or NULL on failure.
1614 *
[169]1615 * @ingroup regfiParseLayer
[99]1616 */
[238]1617_EXPORT()
[203]1618REGFI_NK* regfi_parse_nk(REGFI_FILE* file, uint32_t offset,
[201]1619                             uint32_t max_size, bool strict);
[99]1620
[139]1621
[168]1622/** Parses a single cell containing a subkey-list record.
1623 *
1624 * XXX: finish documenting
1625 *
[169]1626 * @ingroup regfiParseLayer
[168]1627 */
[238]1628_EXPORT()
[201]1629REGFI_SUBKEY_LIST* regfi_parse_subkeylist(REGFI_FILE* file, uint32_t offset,
1630                                          uint32_t max_size, bool strict);
[103]1631
[111]1632
[168]1633/** Parses a VK (value) record at the specified offset
1634 *
1635 * XXX: finish documenting
1636 *
[169]1637 * @ingroup regfiParseLayer
[168]1638 */
[238]1639_EXPORT()
[203]1640REGFI_VK* regfi_parse_vk(REGFI_FILE* file, uint32_t offset, 
[201]1641                             uint32_t max_size, bool strict);
[168]1642
1643
1644/** Parses an SK (security) record at the specified offset
1645 *
1646 * XXX: finish documenting
1647 *
[169]1648 * @ingroup regfiParseLayer
[168]1649 */
[238]1650_EXPORT()
[203]1651REGFI_SK* regfi_parse_sk(REGFI_FILE* file, uint32_t offset, 
[201]1652                             uint32_t max_size, bool strict);
[168]1653
1654
1655/** Retrieves information on all cells in the registry hive which are
1656 *  currently in the unallocated status. 
1657 *
1658 * The unallocated status is determined based soley on the cell length sign.
1659 *
1660 * XXX: finish documenting
1661 *
[169]1662 * @ingroup regfiParseLayer
[168]1663 */
[238]1664_EXPORT()
[201]1665range_list* regfi_parse_unalloc_cells(REGFI_FILE* file);
[101]1666
[111]1667
[168]1668/** Helper function to parse a cell
1669 *
1670 * XXX: finish documenting
1671 *
[169]1672 * @ingroup regfiParseLayer
[168]1673 */
[238]1674_EXPORT()
[201]1675bool regfi_parse_cell(REGFI_RAW_FILE* file_cb, uint32_t offset,
1676                      uint8_t* hdr, uint32_t hdr_len,
1677                      uint32_t* cell_length, bool* unalloc);
[126]1678
[157]1679
[168]1680/** Parses a classname cell
1681 *
1682 * XXX: finish documenting
1683 *
[169]1684 * @ingroup regfiParseLayer
[168]1685 */
[238]1686_EXPORT()
[201]1687uint8_t* regfi_parse_classname(REGFI_FILE* file, uint32_t offset,
1688                               uint16_t* name_length, 
1689                               uint32_t max_size, bool strict);
[157]1690
1691
[168]1692/** Parses a single-cell data record
1693 *
1694 * XXX: finish documenting
1695 *
[169]1696 * @ingroup regfiParseLayer
[168]1697 */
[238]1698_EXPORT()
[201]1699REGFI_BUFFER regfi_parse_data(REGFI_FILE* file, uint32_t offset,
1700                              uint32_t length, bool strict);
[127]1701
[150]1702
[168]1703/** Parses a "little data" record which is stored entirely within the
1704 *  provided virtual offset.
1705 *
1706 * XXX: finish documenting
1707 *
[169]1708 * @ingroup regfiParseLayer
[168]1709 */
[238]1710_EXPORT()
[201]1711REGFI_BUFFER regfi_parse_little_data(REGFI_FILE* file, uint32_t voffset, 
1712                                     uint32_t length, bool strict);
[150]1713
[168]1714
1715/******************************************************************************/
[215]1716/*    Private (and undocumented) Functions                                    */
[168]1717/******************************************************************************/
[226]1718int64_t               regfi_raw_seek(REGFI_RAW_FILE* self, 
1719                                     uint64_t offset, int whence);
[178]1720ssize_t               regfi_raw_read(REGFI_RAW_FILE* self, 
1721                                     void* buf, size_t count);
[238]1722_EXPORT()
[226]1723uint64_t              regfi_seek(REGFI_RAW_FILE* file_cb, 
1724                                 uint64_t offset, int whence);
[238]1725_EXPORT()
[178]1726uint32_t              regfi_read(REGFI_RAW_FILE* file_cb, 
1727                                 uint8_t* buf, uint32_t* length);
1728
[238]1729_EXPORT()
[135]1730const char*           regfi_type_val2str(unsigned int val);
[238]1731_EXPORT()
[135]1732int                   regfi_type_str2val(const char* str);
[127]1733
[238]1734_EXPORT()
[135]1735char*                 regfi_get_sacl(WINSEC_DESC* sec_desc);
[238]1736_EXPORT()
[135]1737char*                 regfi_get_dacl(WINSEC_DESC* sec_desc);
[238]1738_EXPORT()
[135]1739char*                 regfi_get_owner(WINSEC_DESC* sec_desc);
[238]1740_EXPORT()
[135]1741char*                 regfi_get_group(WINSEC_DESC* sec_desc);
1742
[168]1743REGFI_SUBKEY_LIST*    regfi_merge_subkeylists(uint16_t num_lists, 
[135]1744                                              REGFI_SUBKEY_LIST** lists,
1745                                              bool strict);
[168]1746REGFI_SUBKEY_LIST*    regfi_load_subkeylist_aux(REGFI_FILE* file, uint32_t offset,
1747                                                uint32_t max_size, bool strict,
1748                                                uint8_t depth_left);
1749void                  regfi_add_message(REGFI_FILE* file, uint16_t msg_type, 
[138]1750                                        const char* fmt, ...);
[206]1751REGFI_NK*             regfi_copy_nk(const REGFI_NK* nk);
1752REGFI_VK*             regfi_copy_vk(const REGFI_VK* vk);
[238]1753_EXPORT()
[168]1754int32_t               regfi_calc_maxsize(REGFI_FILE* file, uint32_t offset);
[262]1755REGFI_BUFFER          regfi_conv_charset(const char* input_charset, const char* output_charset,
1756                                         uint8_t* input, uint32_t input_len);
[238]1757_EXPORT()
[159]1758REGFI_DATA*           regfi_buffer_to_data(REGFI_BUFFER raw_data);
[146]1759
[168]1760/* XXX: move to base API and document */
[238]1761_EXPORT()
[251]1762REGFI_NTTIME          regfi_unix2nt_time(time_t t);
[238]1763_EXPORT()
[251]1764double                regfi_nt2unix_time(REGFI_NTTIME nt);
[168]1765
1766
[238]1767_EXPORT()
[203]1768void regfi_interpret_keyname(REGFI_FILE* file, REGFI_NK* nk, 
[172]1769                             REGFI_ENCODING output_encoding, bool strict);
[238]1770_EXPORT()
[203]1771void regfi_interpret_valuename(REGFI_FILE* file, REGFI_VK* vk, 
[172]1772                               REGFI_ENCODING output_encoding, bool strict);
1773
[238]1774_EXPORT()
[202]1775void regfi_init();
[172]1776
[202]1777
[78]1778#endif  /* _REGFI_H */
Note: See TracBrowser for help on using the repository browser.