source: trunk/include/regfi.h @ 167

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

added more regfi API documentation
minor consistency change

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