source: trunk/include/regfi.h @ 168

Last change on this file since 168 was 168, checked in by tim, 14 years ago

merged remaining smb_deps items into regfi

began formatting API comments for use with doxygen

  • Property svn:keywords set to Id
File size: 35.4 KB
Line 
1/** @file
2 * Windows NT (and later) registry parsing library
3 *
4 * Branched from Samba project Subversion repository, version #6903:
5 *   http://viewcvs.samba.org/cgi-bin/viewcvs.cgi/trunk/source/include/regfio.h?rev=6903&view=auto
6 *
7 * Copyright (C) 2005-2010 Timothy D. Morgan
8 * Copyright (C) 2005 Gerald (Jerry) Carter
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * $Id: regfi.h 168 2010-03-03 00:08:42Z tim $
24 */
25
26/************************************************************
27 * Most of this information was obtained from
28 * http://www.wednesday.demon.co.uk/dosreg.html
29 * Thanks Nigel!
30 ***********************************************************/
31
32/**
33 * @mainpage Home
34 *
35 * See \ref regfiTopLayer
36 */
37
38#ifndef _REGFI_H
39#define _REGFI_H
40
41#include <stdlib.h>
42#include <stdio.h>
43#include <stdbool.h>
44#include <stdarg.h>
45#include <string.h>
46#include <errno.h>
47#include <time.h>
48#include <fcntl.h>
49#include <sys/stat.h>
50#include <sys/types.h>
51#include <unistd.h>
52#include <assert.h>
53#include <iconv.h>
54
55#include "byteorder.h"
56#include "talloc.h"
57#include "winsec.h"
58#include "void_stack.h"
59#include "range_list.h"
60#include "lru_cache.h"
61
62/******************************************************************************/
63
64/* regfi library error message types */
65#define REGFI_MSG_INFO  0x0001
66#define REGFI_MSG_WARN  0x0004
67#define REGFI_MSG_ERROR 0x0010
68
69typedef uint8_t REGFI_ENCODING;
70/* regfi library supported character encodings */
71#define REGFI_ENCODING_ASCII   0
72#define REGFI_ENCODING_UTF8    1
73#define REGFI_ENCODING_DEFAULT REGFI_ENCODING_ASCII
74/* UTF16LE is not supported for output */
75#define REGFI_ENCODING_UTF16LE 2
76
77#define REGFI_NUM_ENCODINGS    3
78
79/* Windows is lame */
80#ifdef O_BINARY
81#define REGFI_OPEN_FLAGS O_RDONLY|O_BINARY
82#else
83#define REGFI_OPEN_FLAGS O_RDONLY
84#endif
85
86/* Registry data types */
87#define REG_NONE                       0
88#define REG_SZ                         1
89#define REG_EXPAND_SZ                  2
90#define REG_BINARY                     3
91#define REG_DWORD                      4
92#define REG_DWORD_LE                   4  /* DWORD, little endian */
93#define REG_DWORD_BE                   5  /* DWORD, big endian */
94#define REG_LINK                       6
95#define REG_MULTI_SZ                   7
96#define REG_RESOURCE_LIST              8
97#define REG_FULL_RESOURCE_DESCRIPTOR   9
98#define REG_RESOURCE_REQUIREMENTS_LIST 10
99#define REG_QWORD                      11 /* 64-bit little endian */
100/* XXX: Has MS defined a REG_QWORD_BE? */
101/* Not a real type in the registry */
102#define REG_KEY                    0x7FFFFFFF
103
104#define REGFI_OFFSET_NONE          0xffffffff
105
106
107/* This maximum depth is described here:
108 * http://msdn.microsoft.com/en-us/library/ms724872%28VS.85%29.aspx
109 */
110#define REGFI_MAX_DEPTH            512
111
112/* This limit defines the maximum number of levels deep that ri subkey list
113 * trees can go.
114 */
115/* XXX: This is totally arbitrary right now.
116 *      The actual limit may need to be discovered by experimentation.
117 */
118#define REGFI_MAX_SUBKEY_DEPTH     255
119
120
121/* Header sizes and magic number lengths for various records */
122#define REGFI_HBIN_ALLOC           0x1000 /* Minimum allocation unit for HBINs */
123#define REGFI_REGF_SIZE            0x1000 /* "regf" header block size */
124#define REGFI_REGF_MAGIC_SIZE      4
125#define REGFI_REGF_NAME_SIZE       64
126#define REGFI_REGF_RESERVED1_SIZE  340
127#define REGFI_REGF_RESERVED2_SIZE  3528
128#define REGFI_HBIN_MAGIC_SIZE      4
129#define REGFI_CELL_MAGIC_SIZE      2
130#define REGFI_HBIN_HEADER_SIZE     0x20
131#define REGFI_NK_MIN_LENGTH        0x4C
132#define REGFI_VK_MIN_LENGTH        0x14
133#define REGFI_SK_MIN_LENGTH        0x14
134#define REGFI_SUBKEY_LIST_MIN_LEN  0x4
135#define REGFI_BIG_DATA_MIN_LENGTH  0xC
136
137
138/* Constants used for validation */
139/* XXX: Can we add clock resolution validation as well as range?  It has
140 *      been reported that Windows timestamps are never more than a
141 *      certain granularity (250ms?), which could be used to help
142 *      eliminate false positives.  Would need to verify this and
143 *      perhaps conservatively implement a check.
144 */
145 /* Minimum time is Jan 1, 1990 00:00:00 */
146#define REGFI_MTIME_MIN_HIGH       0x01B41E6D
147#define REGFI_MTIME_MIN_LOW        0x26F98000
148 /* Maximum time is Jan 1, 2290 00:00:00
149  * (We hope no one is using Windows by then...)
150  */
151#define REGFI_MTIME_MAX_HIGH       0x03047543
152#define REGFI_MTIME_MAX_LOW        0xC80A4000
153
154
155/* Flags for the vk records */
156#define REGFI_VK_FLAG_ASCIINAME    0x0001
157#define REGFI_VK_DATA_IN_OFFSET    0x80000000
158#define REGFI_VK_MAX_DATA_LENGTH   1024*1024  /* XXX: This is arbitrary */
159
160
161/* Known key flags */
162/*******************/
163/* These next two show up on normal-seeming keys in Vista and W2K3 registries */
164#define REGFI_NK_FLAG_UNKNOWN1     0x4000
165#define REGFI_NK_FLAG_UNKNOWN2     0x1000
166
167/* This next one shows up in some Vista "software" registries */
168/* XXX: This shows up in the following two SOFTWARE keys in Vista:
169 *   /Wow6432Node/Microsoft
170 *   /Wow6432Node/Microsoft/Cryptography
171 * 
172 * It comes along with UNKNOWN2 and ASCIINAME for a total flags value of 0x10A0
173 */
174#define REGFI_NK_FLAG_UNKNOWN3     0x0080
175
176/* Predefined handle.  Rumor has it that the valuelist count for this key is
177 * where the handle is stored.
178 * http://msdn.microsoft.com/en-us/library/ms724836(VS.85).aspx
179 */
180#define REGFI_NK_FLAG_PREDEF_KEY   0x0040
181
182/* The name will be in ASCII if this next bit is set, otherwise UTF-16LE */
183#define REGFI_NK_FLAG_ASCIINAME    0x0020
184
185/* Symlink key. 
186 * See: http://www.codeproject.com/KB/system/regsymlink.aspx
187 */
188#define REGFI_NK_FLAG_LINK         0x0010
189
190/* This key cannot be deleted */
191#define REGFI_NK_FLAG_NO_RM        0x0008
192
193/* Root of a hive */
194#define REGFI_NK_FLAG_ROOT         0x0004
195
196/* Mount point of another hive.  NULL/(default) value indicates which hive
197 * and where in the hive it points to.
198 */
199#define REGFI_NK_FLAG_HIVE_LINK    0x0002
200
201/* These keys shouldn't be stored on disk, according to:
202 * http://geekswithblogs.net/sdorman/archive/2007/12/24/volatile-registry-keys.aspx
203 */
204#define REGFI_NK_FLAG_VOLATILE     0x0001
205
206/* Useful for identifying unknown flag types */
207#define REGFI_NK_KNOWN_FLAGS       (REGFI_NK_FLAG_PREDEF_KEY\
208                                    | REGFI_NK_FLAG_ASCIINAME\
209                                    | REGFI_NK_FLAG_LINK\
210                                    | REGFI_NK_FLAG_NO_RM\
211                                    | REGFI_NK_FLAG_ROOT\
212                                    | REGFI_NK_FLAG_HIVE_LINK\
213                                    | REGFI_NK_FLAG_VOLATILE\
214                                    | REGFI_NK_FLAG_UNKNOWN1\
215                                    | REGFI_NK_FLAG_UNKNOWN2\
216                                    | REGFI_NK_FLAG_UNKNOWN3)
217
218
219#define CHAR_BIT 8
220#define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
221                    : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
222#define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
223#define TIME_FIXUP_CONSTANT (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
224
225typedef struct _regfi_nttime
226{
227  uint32_t low;
228  uint32_t high;
229} REGFI_NTTIME;
230
231
232/* HBIN block */
233typedef struct _regfi_hbin
234{
235  uint32_t file_off;       /* my offset in the registry file */
236  uint32_t ref_count;      /* how many active records are pointing to this
237                          * block (not used currently)
238                          */
239 
240  uint32_t first_hbin_off; /* offset from first hbin block */
241  uint32_t block_size;     /* block size of this block
242                          * Should be a multiple of 4096 (0x1000)
243                          */
244  uint32_t next_block;     /* relative offset to next block. 
245                          * NOTE: This value may be unreliable!
246                          */
247
248  uint8_t magic[REGFI_HBIN_MAGIC_SIZE]; /* "hbin" */
249} REGFI_HBIN;
250
251
252/* Subkey List -- list of key offsets and hashed names for consistency */
253typedef struct 
254{
255  /* Virtual offset of NK record or additional subkey list,
256   * depending on this list's type.
257   */
258  uint32_t offset;
259
260  uint32_t hash;
261} REGFI_SUBKEY_LIST_ELEM;
262
263
264typedef struct _regfi_subkey_list
265{
266  /* Real offset of this record's cell in the file */
267  uint32_t offset;
268
269  uint32_t cell_size;
270 
271  /* Number of immediate children */
272  uint32_t num_children; 
273
274  /* Total number of keys referenced by this list and it's children */
275  uint32_t num_keys;     
276
277  REGFI_SUBKEY_LIST_ELEM* elements;
278  uint8_t magic[REGFI_CELL_MAGIC_SIZE];
279
280  /* Set if the magic indicates this subkey list points to child subkey lists */
281  bool recursive_type; 
282} REGFI_SUBKEY_LIST;
283
284
285typedef uint32_t REGFI_VALUE_LIST_ELEM;
286typedef struct _regfi_value_list
287{
288  /* Actual number of values referenced by this list. 
289   * May differ from parent key's num_values if there were parsing errors.
290   */
291  uint32_t num_values;
292
293  REGFI_VALUE_LIST_ELEM* elements;
294} REGFI_VALUE_LIST;
295
296
297typedef struct _regfi_classname
298{
299  /* As converted to requested character encoding. */
300  char* interpreted;
301
302  /* Represents raw buffer read from classname cell. */
303  uint8_t* raw;
304
305  /* Length of the raw data. May be shorter than that indicated by parent key.*/
306  uint16_t size;
307} REGFI_CLASSNAME;
308
309
310typedef struct _regfi_data
311{
312  uint32_t type;
313
314  /* Length of the raw data. */
315  uint32_t size;
316
317  /* This is always present, representing the raw data cell contents. */
318  uint8_t* raw;
319
320  /* Represents the length of the interpreted value. Meaning is type-specific.*/
321  uint32_t interpreted_size;
322
323  /* These items represent interpreted versions of the raw attribute above.
324   * Only use the appropriate member according to the type field. 
325   * In the event of an unknown type, use only the raw field.
326   */
327  union _regfi_data_interpreted
328  {
329    uint8_t* none; /* */
330    uint8_t* string;
331    uint8_t* expand_string;
332    uint8_t* binary; /* */
333    uint32_t dword;
334    uint32_t dword_be;
335    uint8_t* link;
336    uint8_t** multiple_string;
337    uint64_t qword;
338
339    /* The following are treated as binary currently, but this may change in
340     * the future as the formats become better understood.
341     */
342    uint8_t* resource_list;
343    uint8_t* full_resource_descriptor;
344    uint8_t* resource_requirements_list;
345  } interpreted;
346} REGFI_DATA;
347
348
349/* Value record */
350typedef struct 
351{
352  uint32_t offset;      /* Real offset of this record's cell in the file */
353  uint32_t cell_size;   /* ((start_offset - end_offset) & 0xfffffff8) */
354
355  REGFI_DATA* data;     /* XXX: deprecated */
356
357  char*  valuename;
358  uint8_t* valuename_raw;
359  uint16_t name_length;
360  uint32_t hbin_off;    /* offset from beginning of this hbin block */
361 
362  uint32_t data_size;     /* As reported in the VK record.  May be different than
363                         * That obtained while parsing the data cell itself. */
364  uint32_t data_off;      /* Offset of data cell (virtual) */
365  uint32_t type;
366  uint8_t  magic[REGFI_CELL_MAGIC_SIZE];
367  uint16_t flags;
368  uint16_t unknown1;
369  bool data_in_offset;
370} REGFI_VK_REC;
371
372
373/* Key Security */
374struct _regfi_sk_rec;
375
376typedef struct _regfi_sk_rec
377{
378  uint32_t offset;        /* Real file offset of this record */
379  uint32_t cell_size;   /* ((start_offset - end_offset) & 0xfffffff8) */
380
381  WINSEC_DESC* sec_desc;
382  uint32_t hbin_off;    /* offset from beginning of this hbin block */
383 
384  uint32_t prev_sk_off;
385  uint32_t next_sk_off;
386  uint32_t ref_count;
387  uint32_t desc_size;     /* size of security descriptor */
388  uint16_t unknown_tag;
389  uint8_t  magic[REGFI_CELL_MAGIC_SIZE];
390} REGFI_SK_REC;
391
392
393/* Key Name */
394typedef struct
395{
396  uint32_t offset;      /* Real offset of this record's cell in the file */
397  uint32_t cell_size;   /* Actual or estimated length of the cell. 
398                         * Always in multiples of 8.
399                         */
400
401  /* link in the other records here */
402  REGFI_VALUE_LIST* values;
403  REGFI_SUBKEY_LIST* subkeys;
404 
405  /* header information */
406  uint16_t flags;
407  uint8_t  magic[REGFI_CELL_MAGIC_SIZE];
408  REGFI_NTTIME mtime;
409  uint16_t name_length;
410  uint16_t classname_length;
411  char* keyname;
412  uint8_t* keyname_raw;
413  uint32_t parent_off;              /* pointer to parent key */
414  uint32_t classname_off;
415 
416  /* max lengths */
417  uint32_t max_bytes_subkeyname;            /* max subkey name * 2 */
418  uint32_t max_bytes_subkeyclassname; /* max subkey classname length (as if) */
419  uint32_t max_bytes_valuename;     /* max valuename * 2 */
420  uint32_t max_bytes_value;           /* max value data size */
421 
422  /* unknowns */
423  uint32_t unknown1;
424  uint32_t unknown2;
425  uint32_t unknown3;
426  uint32_t unk_index;               /* nigel says run time index ? */
427 
428  /* children */
429  uint32_t num_subkeys;
430  uint32_t subkeys_off; /* offset of subkey list that points to NK records */
431  uint32_t num_values;
432  uint32_t values_off;  /* value lists which point to VK records */
433  uint32_t sk_off;      /* offset to SK record */
434} REGFI_NK_REC;
435
436
437
438/* REGF block */
439typedef struct 
440{
441  /* Run-time information */
442  /************************/
443  /* file descriptor */
444  int fd;
445
446  /* For sanity checking (not part of the registry header) */
447  uint32_t file_length;
448
449  /* Metadata about hbins */
450  range_list* hbins;
451
452  /* SK record cached since they're repeatedly reused */
453  lru_cache* sk_cache;
454
455  /* Error/warning/info messages returned by lower layer functions */
456  char* last_message;
457
458  /* Mask for error message types that will be stored. */
459  uint16_t msg_mask;
460
461
462  /* Data parsed from file header */
463  /********************************/
464  uint8_t  magic[REGFI_REGF_MAGIC_SIZE];/* "regf" */
465
466 /* These sequence numbers should match if
467  * the hive was properly synced to disk.
468  */
469  uint32_t sequence1;           
470  uint32_t sequence2;
471
472  REGFI_NTTIME mtime;
473  uint32_t major_version;  /* Set to 1 in all known hives */
474  uint32_t minor_version;  /* Set to 3 or 5 in all known hives */
475  uint32_t type;           /* XXX: Unverified.  Set to 0 in all known hives */
476  uint32_t format;         /* XXX: Unverified.  Set to 1 in all known hives */
477
478  uint32_t root_cell;  /* Offset to root cell in the first (or any?) hbin block */
479  uint32_t last_block; /* Offset to last hbin block in file */
480
481  uint32_t cluster;    /* XXX: Unverified. Set to 1 in all known hives */
482
483  /* Matches hive's base file name. Stored in UTF-16LE */
484  uint8_t file_name[REGFI_REGF_NAME_SIZE];
485
486  WINSEC_UUID* rm_id;       /* XXX: Unverified. */
487  WINSEC_UUID* log_id;      /* XXX: Unverified. */
488  WINSEC_UUID* tm_id;       /* XXX: Unverified. */
489  uint32_t flags;             /* XXX: Unverified. */
490  uint32_t guid_signature;    /* XXX: Unverified. */
491
492  uint32_t checksum;          /* Stored checksum from file */
493  uint32_t computed_checksum; /* Our own calculation of the checksum.
494                             * (XOR of bytes 0x0000 - 0x01FB) */
495
496  WINSEC_UUID* thaw_tm_id;  /* XXX: Unverified. */
497  WINSEC_UUID* thaw_rm_id;  /* XXX: Unverified. */
498  WINSEC_UUID* thaw_log_id; /* XXX: Unverified. */
499  uint32_t boot_type;         /* XXX: Unverified. */
500  uint32_t boot_recover;      /* XXX: Unverified. */
501
502  /* This seems to include random junk.  Possibly unsanitized memory left over
503   * from when header block was written.  For instance, chunks of nk records
504   * can be found, though often it's all 0s. */
505  uint8_t reserved1[REGFI_REGF_RESERVED1_SIZE];
506
507  /* This is likely reserved and unusued currently.  (Should be all 0s.)
508   * Included here for easier access in looking for hidden data
509   * or doing research. */
510  uint8_t reserved2[REGFI_REGF_RESERVED2_SIZE];
511
512} REGFI_FILE;
513
514
515typedef struct _regfi_iterator
516{
517  REGFI_FILE* f;
518  void_stack* key_positions;
519  REGFI_NK_REC* cur_key;
520  REGFI_ENCODING string_encoding;
521  uint32_t cur_subkey;
522  uint32_t cur_value;
523} REGFI_ITERATOR;
524
525
526typedef struct _regfi_iter_position
527{
528  REGFI_NK_REC* nk;
529  uint32_t cur_subkey;
530  /* We could store a cur_value here as well, but didn't see
531   * the use in it right now.
532   */
533} REGFI_ITER_POSITION;
534
535
536typedef struct _regfi_buffer
537{
538  uint8_t* buf;
539  uint32_t len;
540} REGFI_BUFFER;
541
542
543/******************************************************************************/
544/**
545 * @defgroup regfiBase Base Functions: Usable with Any Layer
546 *
547 * These functions don't fit particularly well in any of the other layers or
548 * are intended for use with any of the API layers.
549 */
550/******************************************************************************/
551
552
553/** Attempts to open a registry hive and allocate related data structures.
554 *
555 * @param filename A string containing the relative or absolute path of the
556 *               registry hive to be opened.
557 *
558 * @return A reference to a newly allocated REGFI_FILE structure,
559 *         if successful;  NULL on error.
560 *
561 * @ingroup regfiBase
562 */
563REGFI_FILE*           regfi_open(const char* filename);
564
565
566/** Parses file headers of an already open registry hive file and
567 *  allocates related structures for further parsing.
568 *
569 * @param fd A file descriptor of an already open file.  Must be seekable.
570 *
571 * @return A reference to a newly allocated REGFI_FILE structure, if successful;
572 *         NULL on error.
573 *
574 * @ingroup regfiBase
575 */
576REGFI_FILE*           regfi_alloc(int fd);
577
578
579/** Closes and frees an open registry hive.
580 *
581 * @param file The registry structure to close.
582 *
583 * @return 0 on success, -1 on failure with errno set. 
584 *         errno codes are similar to those of close(2).
585 *
586 * @ingroup regfiBase
587 */
588int                   regfi_close(REGFI_FILE* file);
589
590
591/** Frees a hive's data structures without closing the underlying file.
592 *
593 * @param file The registry structure to free.
594 *
595 * @ingroup regfiBase
596 */
597void                  regfi_free(REGFI_FILE* file);
598
599
600/** Get errors, warnings, and/or verbose information relating to processing of
601 *  the given registry file.
602 *
603 * @param file the structure for the registry file
604 *
605 * @return A newly allocated char* which must be free()d by the caller.
606 *
607 * @ingroup regfiBase
608 */
609char*                 regfi_get_messages(REGFI_FILE* file);
610
611
612/** Set the verbosity level of errors and warnings generated by the library
613 *  (as accessible via regfi_get_messages).
614 *
615 * This may be called at any time and will take effect immediately.
616 *
617 * @param file   the structure for the registry file
618 *
619 * @param mask   an integer representing the types of messages desired.
620 *               Acceptable values are created through bitwise ORs of
621 *               REGFI_MSG_* values.  For instance, if only errors and
622 *               informational messages were desired (but not warnings),
623 *               then one would specify: REGFI_MSG_ERROR|REGFI_MSG_INFO
624 *               New REGFI_FILE structures are created with:
625 *                REGFI_MSG_ERROR|REGFI_MSG_WARN
626 *               Note that error and warning messages will continue to
627 *               accumulate in memory if they are not fetched using
628 *               regfi_get_messages and then freed by the caller.
629 *               To disable error messages entirely, supply 0, which
630 *               will prevent message accumulation. 
631 *
632 * @ingroup regfiBase
633 */
634void                  regfi_set_message_mask(REGFI_FILE* file, uint16_t mask);
635
636
637/* Dispose of previously parsed records */
638
639/** Frees a key structure previously returned by one of the API functions
640 *
641 * XXX: finish documenting
642 *
643 * @ingroup regfiBase
644 */
645void                  regfi_free_key(REGFI_NK_REC* nk);
646
647
648/** Frees a value structure previously returned by one of the API functions
649 *
650 * XXX: finish documenting
651 *
652 * @ingroup regfiBase
653 */
654void                  regfi_free_value(REGFI_VK_REC* vk);
655
656
657
658/******************************************************************************/
659/**
660 * @defgroup regfiTopLayer Top Layer: Primary regfi Library Interface
661 *
662 * This top layer of API functions provides an iterator interface which makes
663 * traversing registry data structures easy in both single-threaded and
664 * multi-threaded scenarios.
665 */
666/******************************************************************************/
667
668/** Creates a new iterator for the provided registry file.
669 *
670 * @param file The opened registry file the iterator should be created for.
671 *
672 * @param output_encoding Character encoding that strings should be returned in.
673 *                        Only supply the REGFI_ENCODING_* constants, as others
674 *                        will be rejected.
675 *                        The following values are currently accepted:
676 *                        REGFI_ENCODING_DEFAULT (currently REGFI_ENCODING_ASCII)
677 *                        REGFI_ENCODING_ASCII
678 *                        REGFI_ENCODING_UTF8
679 *
680 * @return A newly allocated REGFI_ITERATOR.
681 *         Must be free()d with regfi_iterator_free.
682 *
683 * @ingroup regfiTopLayer
684 */
685REGFI_ITERATOR*       regfi_iterator_new(REGFI_FILE* file,
686                                         REGFI_ENCODING output_encoding);
687
688
689/** Frees a registry file iterator previously created by regfi_iterator_new.
690 *
691 * This does not affect the underlying registry file's allocation status.
692 *
693 * @param i the iterator to be freed
694 *
695 * @ingroup regfiTopLayer
696 */
697void                  regfi_iterator_free(REGFI_ITERATOR* i);
698
699
700/** Traverse deeper into the registry tree at the current subkey.
701 *
702 * @param i the iterator
703 *
704 * @return  true on success, false on failure. 
705 *          Note that subkey and value indexes are preserved.  That is, if a
706 *          regfi_iterator_up call occurs later (reversing the effect of this
707 *          call) then the subkey and value referenced prior to the
708 *          regfi_iterator_down call will still be referenced.  This  makes
709 *          depth-first iteration particularly easy.
710 *
711 * @ingroup regfiTopLayer
712 */
713bool                  regfi_iterator_down(REGFI_ITERATOR* i);
714
715
716/** Traverse up to the current key's parent key.
717 *
718 * @param i the iterator
719 *
720 * @return  true on success, false on failure.  Any subkey or value state
721 *          associated with the current key is lost.
722 *
723 * @ingroup regfiTopLayer
724 */
725bool                  regfi_iterator_up(REGFI_ITERATOR* i);
726
727
728/** Traverse up to the root key of the hive.
729 *
730 * @param i the iterator
731 *
732 * @return true on success, false on failure.
733 *
734 * @ingroup regfiTopLayer
735 */
736bool                  regfi_iterator_to_root(REGFI_ITERATOR* i);
737
738
739/** Traverse down multiple levels in the registry hive.
740 *
741 * XXX: This currently only accepts ASCII key names.  Need to look into
742 *      accepting other encodings.
743 *
744 * @param i    the iterator
745 * @param path a list of key names representing the path.  This list must
746 *             contain NUL terminated strings.  The list itself is
747 *             terminated with a NULL pointer.  All path elements must be
748 *             keys; value names are not accepted (even as the last
749 *             element).
750 *
751 * @return true on success, false on failure.  If any element of path is not
752 *                 found, false will be returned and the iterator will remain
753 *                 in its original position.
754 *
755 * @ingroup regfiTopLayer
756 */
757bool regfi_iterator_walk_path(REGFI_ITERATOR* i, const char** path);
758
759
760/** Returns the currently referenced key.
761 *
762 * @param i the iterator
763 *
764 * @return A read-only key structure for the current key, or NULL on failure.
765 *
766 * @ingroup regfiTopLayer
767 */
768const REGFI_NK_REC*   regfi_iterator_cur_key(REGFI_ITERATOR* i);
769
770
771/** Returns the SK (security) record referenced by the current key.
772 *
773 * @param i the iterator
774 *
775 * @return A read-only SK structure, or NULL on failure.
776 *
777 * @ingroup regfiTopLayer
778 */
779const REGFI_SK_REC*   regfi_iterator_cur_sk(REGFI_ITERATOR* i);
780
781
782/** Sets the internal subkey index to the first subkey referenced by the current
783 *  key and returns that key.
784 *
785 * @param i the iterator
786 *
787 * @return A newly allocated key structure for the newly referenced first
788 *         subkey, or NULL on failure.  Failure may be due to a lack of any
789 *         subkeys or other errors.  Newly allocated keys must be freed with
790 *         regfi_free_key.
791 *
792 * @ingroup regfiTopLayer
793 */
794REGFI_NK_REC*         regfi_iterator_first_subkey(REGFI_ITERATOR* i);
795
796
797/** Returns the currently indexed subkey.
798 *
799 * @param i the iterator
800 *
801 * @return A newly allocated key structure for the currently referenced subkey,
802 *         or NULL on failure.  Newly allocated keys must be freed with
803 *         regfi_free_key.
804 *
805 * @ingroup regfiTopLayer
806 */
807REGFI_NK_REC*         regfi_iterator_cur_subkey(REGFI_ITERATOR* i);
808
809
810/** Increments the internal subkey index to the next key in the subkey-list and
811 *  returns the subkey for that index.
812 *
813 * @param i the iterator
814 *
815 * @return A newly allocated key structure for the next subkey or NULL on
816 *         failure.  Newly allocated keys must be freed with regfi_free_key.
817 *
818 * @ingroup regfiTopLayer
819 */
820REGFI_NK_REC*         regfi_iterator_next_subkey(REGFI_ITERATOR* i);
821
822
823/** Searches for a subkey with a given name under the current key.
824 *
825 * @param i           the iterator
826 * @param subkey_name subkey name to search for
827 *
828 * @return True if such a subkey was found, false otherwise.  If a subkey is
829 *         found, the current subkey index is set to that subkey.  Otherwise,
830 *         the subkey index remains at the same location as before the call.
831 *
832 * @ingroup regfiTopLayer
833 */
834bool                  regfi_iterator_find_subkey(REGFI_ITERATOR* i, 
835                                                 const char* subkey_name);
836
837/** Sets the internal value index to the first value referenced by the current
838 *  key and returns that value.
839 *
840 * @param i the iterator
841 *
842 * @return  A newly allocated value structure for the newly referenced first
843 *          value, or NULL on failure.  Failure may be due to a lack of any
844 *          values or other errors.  Newly allocated keys must be freed with
845 *          regfi_free_value.
846 *
847 * @ingroup regfiTopLayer
848 */
849REGFI_VK_REC*         regfi_iterator_first_value(REGFI_ITERATOR* i);
850
851
852/** Returns the currently indexed value.
853 *
854 * @param i the iterator
855 *
856 * @return A newly allocated value structure for the currently referenced value,
857 *         or NULL on failure.  Newly allocated values must be freed with
858 *         regfi_free_value.
859 *
860 * @ingroup regfiTopLayer
861 */
862REGFI_VK_REC*         regfi_iterator_cur_value(REGFI_ITERATOR* i);
863
864
865/** Increments the internal value index to the next value in the value-list and
866 *  returns the value for that index.
867 *
868 * @param i the iterator
869 *
870 * @return  A newly allocated key structure for the next value or NULL on
871 *          failure.  Newly allocated keys must be freed with regfi_free_value.
872 *
873 * @ingroup regfiTopLayer
874 */
875REGFI_VK_REC*         regfi_iterator_next_value(REGFI_ITERATOR* i);
876
877
878/** Searches for a value with a given name under the current key.
879 *
880 * @param i          the iterator
881 * @param value_name value name to search for
882 *
883 * @return True if such a value was found, false otherwise.  If a value is
884 *         found, the current value index is set to that value.  Otherwise,
885 *         the value index remains at the same location as before the call.
886 *
887 * @ingroup regfiTopLayer
888 */
889bool                  regfi_iterator_find_value(REGFI_ITERATOR* i, 
890                                                const char* value_name);
891
892/** Retrieves classname for a given key.
893 *
894 * @param i   the iterator
895 * @param key the key whose classname is desired
896 *
897 * @return Returns a newly allocated classname structure, or NULL on failure.
898 *         Classname structures must be freed with regfi_free_classname.
899 *
900 * @ingroup regfiTopLayer
901 */
902REGFI_CLASSNAME*      regfi_iterator_fetch_classname(REGFI_ITERATOR* i, 
903                                                     const REGFI_NK_REC* key);
904
905
906/** Retrieves data for a given value.
907 *
908 * @param i     the iterator
909 * @param value the value whose data is desired
910 *
911 * @return Returns a newly allocated data structure, or NULL on failure.
912 *         Data structures must be freed with regfi_free_data.
913 *
914 * @ingroup regfiTopLayer
915 */
916REGFI_DATA*           regfi_iterator_fetch_data(REGFI_ITERATOR* i, 
917                                                const REGFI_VK_REC* value);
918
919
920
921/******************************************************************************/
922/**
923 * @defgroup regfiMiddleLayer Middle Layer: Logical Data Structure Loading
924 */
925/******************************************************************************/
926
927/** Loads a key at a given file offset along with associated data structures.
928 *
929 * XXX: finish documenting
930 *
931 * @ingroup regfiMiddleLayer
932 */
933REGFI_NK_REC*         regfi_load_key(REGFI_FILE* file, uint32_t offset, 
934                                     REGFI_ENCODING output_encoding, 
935                                     bool strict);
936
937
938/** Loads a value at a given file offset alng with associated data structures.
939 *
940 * XXX: finish documenting
941 *
942 * @ingroup regfiMiddleLayer
943 */
944REGFI_VK_REC*         regfi_load_value(REGFI_FILE* file, uint32_t offset, 
945                                       REGFI_ENCODING output_encoding, 
946                                       bool strict);
947
948
949/** Loads a logical subkey list in its entirety which may span multiple records.
950 *
951 * XXX: finish documenting
952 *
953 * @ingroup regfiMiddleLayer
954 */
955REGFI_SUBKEY_LIST*    regfi_load_subkeylist(REGFI_FILE* file, uint32_t offset,
956                                            uint32_t num_keys, uint32_t max_size,
957                                            bool strict);
958
959
960/** Loads a valuelist.
961 *
962 * XXX: finish documenting
963 *
964 * @ingroup regfiMiddleLayer
965 */
966REGFI_VALUE_LIST*     regfi_load_valuelist(REGFI_FILE* file, uint32_t offset, 
967                                           uint32_t num_values, uint32_t max_size,
968                                           bool strict);
969
970
971/** Loads a data record which may be contained in the virtual offset, in a
972 *  single cell, or in multiple cells through big data records.
973 *
974 * XXX: finish documenting
975 *
976 * @ingroup regfiMiddleLayer
977 */
978REGFI_BUFFER          regfi_load_data(REGFI_FILE* file, uint32_t voffset,
979                                      uint32_t length, bool data_in_offset,
980                                      bool strict);
981
982
983/** Loads the data associated with a big data record at the specified offset.
984 *
985 * XXX: finish documenting
986 *
987 * @ingroup regfiMiddleLayer
988 */
989REGFI_BUFFER          regfi_load_big_data(REGFI_FILE* file, uint32_t offset, 
990                                          uint32_t data_length,uint32_t cell_length,
991                                          range_list* used_ranges,
992                                          bool strict);
993
994
995/** Given raw data, attempts to interpret the data based on a specified registry
996 *  data type.
997 *
998 * XXX: finish documenting
999 *
1000 * @ingroup regfiMiddleLayer
1001 */
1002bool                  regfi_interpret_data(REGFI_FILE* file, 
1003                                           REGFI_ENCODING string_encoding,
1004                                           uint32_t type, REGFI_DATA* data);
1005
1006
1007/** Frees the memory associated with a REGFI_CLASSNAME data structure.
1008 *
1009 * XXX: finish documenting
1010 *
1011 * @ingroup regfiMiddleLayer
1012 */
1013void                  regfi_free_classname(REGFI_CLASSNAME* classname);
1014
1015
1016/** Frees the memory associated with a REGFI_DATA data structure.
1017 *
1018 * XXX: finish documenting
1019 *
1020 * @ingroup regfiMiddleLayer
1021 */
1022void                  regfi_free_data(REGFI_DATA* data);
1023
1024
1025/* These are cached so return values don't need to be freed. */
1026
1027/** Loads an "sk" security record at the specified offset.
1028 *
1029 * XXX: finish documenting
1030 *
1031 * @ingroup regfiMiddleLayer
1032 */
1033const REGFI_SK_REC*   regfi_load_sk(REGFI_FILE* file, uint32_t offset,
1034                                    bool strict);
1035
1036
1037/** Retrieves the HBIN data structure stored at the specified offset.
1038 *
1039 * XXX: finish documenting
1040 *
1041 * @ingroup regfiMiddleLayer
1042 */
1043const REGFI_HBIN*     regfi_lookup_hbin(REGFI_FILE* file, uint32_t offset);
1044
1045
1046
1047/******************************************************************************/
1048/**
1049 * @defgroup regfiBottomLayer Bottom Layer: Direct Data Structure Access
1050 */
1051/******************************************************************************/
1052
1053REGFI_FILE*           regfi_parse_regf(int fd, bool strict);
1054REGFI_HBIN*           regfi_parse_hbin(REGFI_FILE* file, uint32_t offset, 
1055                                       bool strict);
1056
1057
1058/** Parses an NK record at the specified offset
1059 *
1060 * @param file     the registry file structure
1061 * @param offset   the offset of the cell (not the record) to be parsed.
1062 * @param max_size the maximum size the NK cell could be. (for validation)
1063 * @param strict   if true, rejects any malformed records.  Otherwise,
1064 *                 tries to minimally validate integrity.
1065 *
1066 * @return A newly allocated NK record structure, or NULL on failure.
1067 *
1068 * @ingroup regfiBottomLayer
1069 */
1070REGFI_NK_REC*         regfi_parse_nk(REGFI_FILE* file, uint32_t offset,
1071                                     uint32_t max_size, bool strict);
1072
1073
1074/** Parses a single cell containing a subkey-list record.
1075 *
1076 * XXX: finish documenting
1077 *
1078 * @ingroup regfiBottomLayer
1079 */
1080REGFI_SUBKEY_LIST*    regfi_parse_subkeylist(REGFI_FILE* file, uint32_t offset,
1081                                             uint32_t max_size, bool strict);
1082
1083
1084/** Parses a VK (value) record at the specified offset
1085 *
1086 * XXX: finish documenting
1087 *
1088 * @ingroup regfiBottomLayer
1089 */
1090REGFI_VK_REC*         regfi_parse_vk(REGFI_FILE* file, uint32_t offset, 
1091                                     uint32_t max_size, bool strict);
1092
1093
1094/** Parses an SK (security) record at the specified offset
1095 *
1096 * XXX: finish documenting
1097 *
1098 * @ingroup regfiBottomLayer
1099 */
1100REGFI_SK_REC*         regfi_parse_sk(REGFI_FILE* file, uint32_t offset, 
1101                                     uint32_t max_size, bool strict);
1102
1103
1104/** Retrieves information on all cells in the registry hive which are
1105 *  currently in the unallocated status. 
1106 *
1107 * The unallocated status is determined based soley on the cell length sign.
1108 *
1109 * XXX: finish documenting
1110 *
1111 * @ingroup regfiBottomLayer
1112 */
1113range_list*           regfi_parse_unalloc_cells(REGFI_FILE* file);
1114
1115
1116/** Helper function to parse a cell
1117 *
1118 * XXX: finish documenting
1119 *
1120 * @ingroup regfiBottomLayer
1121 */
1122bool                  regfi_parse_cell(int fd, uint32_t offset, 
1123                                       uint8_t* hdr, uint32_t hdr_len,
1124                                       uint32_t* cell_length, bool* unalloc);
1125
1126
1127/** Parses a classname cell
1128 *
1129 * XXX: finish documenting
1130 *
1131 * @ingroup regfiBottomLayer
1132 */
1133uint8_t*                regfi_parse_classname(REGFI_FILE* file, uint32_t offset,
1134                                            uint16_t* name_length, 
1135                                            uint32_t max_size, bool strict);
1136
1137
1138/** Parses a single-cell data record
1139 *
1140 * XXX: finish documenting
1141 *
1142 * @ingroup regfiBottomLayer
1143 */
1144REGFI_BUFFER          regfi_parse_data(REGFI_FILE* file, uint32_t offset,
1145                                       uint32_t length, bool strict);
1146
1147
1148/** Parses a "little data" record which is stored entirely within the
1149 *  provided virtual offset.
1150 *
1151 * XXX: finish documenting
1152 *
1153 * @ingroup regfiBottomLayer
1154 */
1155REGFI_BUFFER          regfi_parse_little_data(REGFI_FILE* file, uint32_t voffset, 
1156                                              uint32_t length, bool strict);
1157
1158
1159/******************************************************************************/
1160/*    Private Functions                                                       */
1161/******************************************************************************/
1162REGFI_NK_REC*         regfi_rootkey(REGFI_FILE* file, 
1163                                    REGFI_ENCODING output_encoding);
1164void                  regfi_subkeylist_free(REGFI_SUBKEY_LIST* list);
1165uint32_t                regfi_read(int fd, uint8_t* buf, uint32_t* length);
1166
1167const char*           regfi_type_val2str(unsigned int val);
1168int                   regfi_type_str2val(const char* str);
1169
1170char*                 regfi_get_sacl(WINSEC_DESC* sec_desc);
1171char*                 regfi_get_dacl(WINSEC_DESC* sec_desc);
1172char*                 regfi_get_owner(WINSEC_DESC* sec_desc);
1173char*                 regfi_get_group(WINSEC_DESC* sec_desc);
1174
1175REGFI_SUBKEY_LIST*    regfi_merge_subkeylists(uint16_t num_lists, 
1176                                              REGFI_SUBKEY_LIST** lists,
1177                                              bool strict);
1178REGFI_SUBKEY_LIST*    regfi_load_subkeylist_aux(REGFI_FILE* file, uint32_t offset,
1179                                                uint32_t max_size, bool strict,
1180                                                uint8_t depth_left);
1181void                  regfi_add_message(REGFI_FILE* file, uint16_t msg_type, 
1182                                        const char* fmt, ...);
1183REGFI_NK_REC*         regfi_copy_nk(const REGFI_NK_REC* nk);
1184REGFI_VK_REC*         regfi_copy_vk(const REGFI_VK_REC* vk);
1185int32_t               regfi_calc_maxsize(REGFI_FILE* file, uint32_t offset);
1186int32_t               regfi_conv_charset(const char* input_charset, 
1187                                         const char* output_charset,
1188                                         uint8_t* input, char* output, 
1189                                         uint32_t input_len, uint32_t output_max);
1190REGFI_DATA*           regfi_buffer_to_data(REGFI_BUFFER raw_data);
1191
1192/* XXX: move to base API and document */
1193void                  regfi_unix2nt_time(REGFI_NTTIME* nt, time_t t);
1194time_t                regfi_nt2unix_time(const REGFI_NTTIME* nt);
1195
1196
1197#endif  /* _REGFI_H */
Note: See TracBrowser for help on using the repository browser.