source: trunk/include/regfi.h @ 150

Last change on this file since 150 was 150, checked in by tim, 15 years ago

integrated talloc into most of the rest of the regfi library
fixed a length validation issue

  • Property svn:keywords set to Id
File size: 17.0 KB
RevLine 
[30]1/*
[134]2 * Branched from Samba project Subversion repository, version #6903:
[84]3 *   http://viewcvs.samba.org/cgi-bin/viewcvs.cgi/trunk/source/include/regfio.h?rev=6903&view=auto
[30]4 *
[134]5 * Windows NT (and later) registry parsing library
[30]6 *
[132]7 * Copyright (C) 2005-2009 Timothy D. Morgan
[30]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
[134]12 * the Free Software Foundation; version 3 of the License.
[30]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 150 2009-03-02 02:17:46Z 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
[78]32#ifndef _REGFI_H
33#define _REGFI_H
[30]34
[31]35#include <stdlib.h>
36#include <stdio.h>
[30]37#include <stdbool.h>
[136]38#include <stdarg.h>
[31]39#include <string.h>
[30]40#include <errno.h>
[31]41#include <time.h>
[30]42#include <fcntl.h>
43#include <sys/stat.h>
44#include <sys/types.h>
45#include <unistd.h>
46#include <assert.h>
47
[147]48#include "talloc.h"
[30]49#include "smb_deps.h"
[132]50#include "winsec.h"
[78]51#include "void_stack.h"
[99]52#include "range_list.h"
[108]53#include "lru_cache.h"
[30]54
55/******************************************************************************/
[138]56
57/* regfi library error message types */
58#define REGFI_MSG_INFO  0x0001
59#define REGFI_MSG_WARN  0x0004
60#define REGFI_MSG_ERROR 0x0010
61
[143]62/* Windows is lame */
63#ifdef O_BINARY
64#define REGFI_OPEN_FLAGS O_RDONLY|O_BINARY
65#else
66#define REGFI_OPEN_FLAGS O_RDONLY
67#endif
68
[32]69/* Registry data types */
70#define REG_NONE                       0
71#define REG_SZ                         1
72#define REG_EXPAND_SZ                  2
73#define REG_BINARY                     3
74#define REG_DWORD                      4
[54]75#define REG_DWORD_LE                   4  /* DWORD, little endian */
76#define REG_DWORD_BE                   5  /* DWORD, big endian */
[32]77#define REG_LINK                       6
78#define REG_MULTI_SZ                   7
79#define REG_RESOURCE_LIST              8
80#define REG_FULL_RESOURCE_DESCRIPTOR   9
81#define REG_RESOURCE_REQUIREMENTS_LIST 10
[72]82#define REG_QWORD                      11 /* 64-bit little endian */
83/* XXX: Has MS defined a REG_QWORD_BE? */
[32]84/* Not a real type in the registry */
[136]85#define REG_KEY                    0x7FFFFFFF
[32]86
[135]87#define REGFI_MAX_DEPTH            512
88#define REGFI_OFFSET_NONE          0xffffffff
[30]89
[139]90/* XXX: This is totally arbitrary right now. */
[149]91#define REGFI_MAX_SUBKEY_DEPTH     255
[139]92
[135]93/* Header sizes and magic number lengths for various records */
[147]94#define REGFI_HBIN_ALLOC           0x1000 /* Minimum allocation unit for HBINs */
95#define REGFI_REGF_SIZE            0x1000 /* "regf" header block size */
[135]96#define REGFI_REGF_MAGIC_SIZE      4
97#define REGFI_HBIN_MAGIC_SIZE      4
98#define REGFI_CELL_MAGIC_SIZE      2
99#define REGFI_HBIN_HEADER_SIZE     0x20
[116]100#define REGFI_NK_MIN_LENGTH        0x4C
101#define REGFI_VK_MIN_LENGTH        0x14
102#define REGFI_SK_MIN_LENGTH        0x14
[127]103#define REGFI_SUBKEY_LIST_MIN_LEN  0x4
[30]104
[135]105
[116]106/* Constants used for validation */
[125]107/* XXX: Can we add clock resolution validation as well as range?  It has
108 *      been reported that Windows timestamps are never more than a
109 *      certain granularity (250ms?), which could be used to help
[147]110 *      eliminate false positives.  Would need to verify this and
[125]111 *      perhaps conservatively implement a check.
112 */
[116]113 /* Minimum time is Jan 1, 1990 00:00:00 */
114#define REGFI_MTIME_MIN_HIGH       0x01B41E6D
115#define REGFI_MTIME_MIN_LOW        0x26F98000
116 /* Maximum time is Jan 1, 2290 00:00:00
117  * (We hope no one is using Windows by then...)
118  */
119#define REGFI_MTIME_MAX_HIGH       0x03047543
120#define REGFI_MTIME_MAX_LOW        0xC80A4000
[30]121
[116]122
[30]123/* Flags for the vk records */
[135]124#define REGFI_VK_FLAG_NAME_PRESENT 0x0001
125#define REGFI_VK_DATA_IN_OFFSET    0x80000000
126#define REGFI_VK_MAX_DATA_LENGTH   1024*1024
[30]127
[137]128
[116]129/* NK record types */
[137]130/* XXX: This is starting to look like this is a flags field. 
131 *      Need to decipher the meaning of each flag.
132 */
[135]133#define REGFI_NK_TYPE_LINKKEY      0x0010
134#define REGFI_NK_TYPE_NORMALKEY    0x0020
135 /* XXX: Unknown key type that shows up in Vista registries */
136#define REGFI_NK_TYPE_UNKNOWN1     0x1020
[137]137 /* XXX: Unknown key types that shows up in W2K3 registries */
138#define REGFI_NK_TYPE_UNKNOWN2     0x4020
139#define REGFI_NK_TYPE_UNKNOWN3     0x0000  /* XXX: This type seems to have UTF-16 names!!! */
[135]140#define REGFI_NK_TYPE_ROOTKEY1     0x002c
141 /* XXX: Unknown root key type that shows up in Vista registries */
142#define REGFI_NK_TYPE_ROOTKEY2     0x00ac
[30]143
[137]144#if 0
145/* Initial hypothesis of NK flags: */
[140]146/***********************************/
[137]147#define REGFI_NK_FLAG_LINK         0x0010
148/* The name will be in ASCII if this next bit is set, otherwise UTF-16LE */
149#define REGFI_NK_FLAG_ASCIINAME    0x0020
150/* These next two combine to form the "c" on both known root key types */
151#define REGFI_NK_FLAG_ROOT1        0x0008
152#define REGFI_NK_FLAG_ROOT2        0x0004
153/* These next two show up on normal-seeming keys in Vista and W2K3 registries */
154#define REGFI_NK_FLAG_UNKNOWN1     0x4000
155#define REGFI_NK_FLAG_UNKNOWN2     0x1000
156/* This next one shows up on root keys in some Vista "software" registries */
157#define REGFI_NK_FLAG_UNKNOWN3     0x0080
158#endif
[30]159
[137]160
[30]161/* HBIN block */
[135]162typedef struct _regfi_hbin
[97]163{
[53]164  uint32 file_off;       /* my offset in the registry file */
[84]165  uint32 ref_count;      /* how many active records are pointing to this
[54]166                          * block (not used currently)
167                          */
[84]168 
[53]169  uint32 first_hbin_off; /* offset from first hbin block */
[97]170  uint32 block_size;     /* block size of this block
171                          * Should be a multiple of 4096 (0x1000)
[54]172                          */
[99]173  uint32 next_block;     /* relative offset to next block. 
174                          * NOTE: This value may be unreliable!
[97]175                          */
176
[135]177  uint8 magic[REGFI_HBIN_MAGIC_SIZE]; /* "hbin" */
178} REGFI_HBIN;
[30]179
[97]180
[127]181/* Subkey List -- list of key offsets and hashed names for consistency */
[97]182typedef struct 
183{
[139]184  /* Virtual offset of NK record or additional subkey list,
185   * depending on this list's type.
186   */
187  uint32 offset;
188
[104]189  uint32 hash;
[135]190} REGFI_SUBKEY_LIST_ELEM;
[30]191
[97]192
[145]193typedef struct _regfi_subkey_list
[97]194{
[139]195  /* Real offset of this record's cell in the file */
196  uint32 offset;
197
198  uint32 cell_size;
199 
200  /* Number of immediate children */
201  uint32 num_children; 
202
203  /* Total number of keys referenced by this list and it's children */
204  uint32 num_keys;     
205
[135]206  REGFI_SUBKEY_LIST_ELEM* elements;
207  uint8 magic[REGFI_CELL_MAGIC_SIZE];
[139]208
209  /* Set if the magic indicates this subkey list points to child subkey lists */
210  bool recursive_type; 
[135]211} REGFI_SUBKEY_LIST;
[30]212
[97]213
[145]214typedef uint32 REGFI_VALUE_LIST_ELEM;
215typedef struct _regfi_value_list
216{
217  /* Actual number of values referenced by this list. 
218   * May differ from parent key's num_values if there were parsing errors.
219   */
220  uint32 num_values;
221
222  REGFI_VALUE_LIST_ELEM* elements;
223} REGFI_VALUE_LIST;
224
225
[150]226/* Value record */
[97]227typedef struct 
228{
[101]229  uint32 offset;        /* Real offset of this record's cell in the file */
230  uint32 cell_size;     /* ((start_offset - end_offset) & 0xfffffff8) */
231
232  uint8* data;
[150]233  char*  valuename;
[101]234  uint16 name_length;
[53]235  uint32 hbin_off;      /* offset from beginning of this hbin block */
236 
237  uint32 data_size;
[101]238  uint32 data_off;      /* offset of data cell (virtual) */
[53]239  uint32 type;
[135]240  uint8  magic[REGFI_CELL_MAGIC_SIZE];
[53]241  uint16 flag;
[101]242  uint16 unknown1;
[111]243  bool data_in_offset;
[135]244} REGFI_VK_REC;
[30]245
246
247/* Key Security */
[135]248struct _regfi_sk_rec;
[30]249
[135]250typedef struct _regfi_sk_rec
[97]251{
[111]252  uint32 offset;        /* Real file offset of this record */
253  uint32 cell_size;     /* ((start_offset - end_offset) & 0xfffffff8) */
254
[134]255  WINSEC_DESC* sec_desc;
[53]256  uint32 hbin_off;      /* offset from beginning of this hbin block */
257 
258  uint32 prev_sk_off;
259  uint32 next_sk_off;
260  uint32 ref_count;
[102]261  uint32 desc_size;     /* size of security descriptor */
262  uint16 unknown_tag;
[135]263  uint8  magic[REGFI_CELL_MAGIC_SIZE];
264} REGFI_SK_REC;
[30]265
[81]266
[104]267/* Key Name */
268typedef struct
[97]269{
[99]270  uint32 offset;        /* Real offset of this record's cell in the file */
271  uint32 cell_size;     /* Actual or estimated length of the cell. 
272                         * Always in multiples of 8.
273                         */
[84]274
275  /* link in the other records here */
[145]276  REGFI_VALUE_LIST* values;
[135]277  REGFI_SUBKEY_LIST* subkeys;
[53]278 
279  /* header information */
[112]280  uint16 key_type;
[135]281  uint8  magic[REGFI_CELL_MAGIC_SIZE];
[53]282  NTTIME mtime;
[99]283  uint16 name_length;
284  uint16 classname_length;
[84]285  char* classname;
286  char* keyname;
[125]287  uint32 parent_off;                /* pointer to parent key */
288  uint32 classname_off;
[53]289 
290  /* max lengths */
[54]291  uint32 max_bytes_subkeyname;      /* max subkey name * 2 */
292  uint32 max_bytes_subkeyclassname; /* max subkey classname length (as if) */
293  uint32 max_bytes_valuename;       /* max valuename * 2 */
294  uint32 max_bytes_value;           /* max value data size */
[53]295 
296  /* unknowns */
[99]297  uint32 unknown1;
298  uint32 unknown2;
299  uint32 unknown3;
[54]300  uint32 unk_index;                 /* nigel says run time index ? */
[53]301 
302  /* children */
303  uint32 num_subkeys;
[127]304  uint32 subkeys_off;   /* offset of subkey list that points to NK records */
[53]305  uint32 num_values;
306  uint32 values_off;    /* value lists which point to VK records */
[127]307  uint32 sk_off;        /* offset to SK record */
[135]308} REGFI_NK_REC;
[30]309
[81]310
[97]311
[30]312/* REGF block */
[97]313typedef struct 
314{
[135]315  /* Run-time information */
316  /************************/
[138]317  /* file descriptor */
318  int fd;
[99]319
[138]320  /* For sanity checking (not part of the registry header) */
321  uint32 file_length;
322
323  /* Metadata about hbins */
[99]324  range_list* hbins;
325
[146]326  /* SK record cached since they're repeatedly reused */
327  lru_cache* sk_cache;
328
[135]329  /* Error/warning/info messages returned by lower layer functions */
330  char* last_message;
331
[138]332  /* Mask for error message types that will be stored. */
333  uint16 msg_mask;
[135]334
335
336  /* Data parsed from file header */
337  /********************************/
338  uint8  magic[REGFI_REGF_MAGIC_SIZE];/* "regf" */
[84]339  NTTIME mtime;
[54]340  uint32 data_offset;           /* offset to record in the first (or any?)
341                                 * hbin block
342                                 */
[53]343  uint32 last_block;            /* offset to last hbin block in file */
[97]344
345  uint32 checksum;              /* Stored checksum. */
346  uint32 computed_checksum;     /* Our own calculation of the checksum.
347                                 * (XOR of bytes 0x0000 - 0x01FB)
348                                 */
[53]349 
[140]350  /* XXX: Some of these we have some clues about (major/minor version, etc).
351   *      Should verify and update names accordingly.
352   */
[97]353  /* unknown data structure values */
[53]354  uint32 unknown1;
355  uint32 unknown2;
356  uint32 unknown3;
357  uint32 unknown4;
358  uint32 unknown5;
359  uint32 unknown6;
[97]360  uint32 unknown7;
[135]361} REGFI_FILE;
[30]362
363
[140]364/* XXX: Should move all caching (SK records, HBINs, NKs, etc) to a single
365 *      structure, probably REGFI_FILE.  Once key caching is in place,
366 *      convert key_positions stack to store just key offsets rather than
367 *      whole keys.
368 */
[97]369typedef struct 
370{
[135]371  REGFI_FILE* f;
[80]372  void_stack* key_positions;
[135]373  REGFI_NK_REC* cur_key;
[78]374  uint32 cur_subkey;
375  uint32 cur_value;
376} REGFI_ITERATOR;
377
[80]378
[97]379typedef struct 
380{
[135]381  REGFI_NK_REC* nk;
[80]382  uint32 cur_subkey;
383  /* We could store a cur_value here as well, but didn't see
384   * the use in it right now.
385   */
386} REGFI_ITER_POSITION;
387
388
[54]389/******************************************************************************/
[135]390/*                         Main iterator API                                  */
391/******************************************************************************/
392REGFI_FILE*           regfi_open(const char* filename);
393int                   regfi_close(REGFI_FILE* r);
[32]394
[136]395/* regfi_get_messages: Get errors, warnings, and/or verbose information
396 *                     relating to processing of the given registry file.
[135]397 *
398 * Arguments:
399 *   file     -- the structure for the registry file
400 *
401 * Returns:
402 *   A newly allocated char* which must be free()d by the caller.
403 */
[136]404char*                 regfi_get_messages(REGFI_FILE* file);
[138]405void                  regfi_set_message_mask(REGFI_FILE* file, uint16 mask);
[53]406
[135]407REGFI_ITERATOR*       regfi_iterator_new(REGFI_FILE* fh);
[84]408void                  regfi_iterator_free(REGFI_ITERATOR* i);
409bool                  regfi_iterator_down(REGFI_ITERATOR* i);
410bool                  regfi_iterator_up(REGFI_ITERATOR* i);
411bool                  regfi_iterator_to_root(REGFI_ITERATOR* i);
[30]412
[84]413bool                  regfi_iterator_find_subkey(REGFI_ITERATOR* i, 
414                                                 const char* subkey_name);
415bool                  regfi_iterator_walk_path(REGFI_ITERATOR* i, 
416                                               const char** path);
[135]417const REGFI_NK_REC*   regfi_iterator_cur_key(REGFI_ITERATOR* i);
418const REGFI_SK_REC*   regfi_iterator_cur_sk(REGFI_ITERATOR* i);
[80]419
[150]420REGFI_NK_REC*         regfi_iterator_first_subkey(REGFI_ITERATOR* i);
421REGFI_NK_REC*         regfi_iterator_cur_subkey(REGFI_ITERATOR* i);
422REGFI_NK_REC*         regfi_iterator_next_subkey(REGFI_ITERATOR* i);
423
[84]424bool                  regfi_iterator_find_value(REGFI_ITERATOR* i, 
425                                                const char* value_name);
[150]426REGFI_VK_REC*         regfi_iterator_first_value(REGFI_ITERATOR* i);
427REGFI_VK_REC*         regfi_iterator_cur_value(REGFI_ITERATOR* i);
428REGFI_VK_REC*         regfi_iterator_next_value(REGFI_ITERATOR* i);
[80]429
[127]430
431/********************************************************/
[146]432/* Middle-layer structure loading, linking, and caching */
[127]433/********************************************************/
[135]434REGFI_NK_REC*         regfi_load_key(REGFI_FILE* file, uint32 offset, 
435                                     bool strict);
[145]436REGFI_VK_REC*         regfi_load_value(REGFI_FILE* file, uint32 offset, 
437                                       bool strict);
[146]438REGFI_SUBKEY_LIST*    regfi_load_subkeylist(REGFI_FILE* file, uint32 offset,
439                                            uint32 num_keys, uint32 max_size,
440                                            bool strict);
[145]441REGFI_VALUE_LIST*     regfi_load_valuelist(REGFI_FILE* file, uint32 offset, 
[146]442                                           uint32 num_values, uint32 max_size,
[135]443                                           bool strict);
[127]444
[146]445/* These are cached so return values don't need to be freed. */
446const REGFI_SK_REC*   regfi_load_sk(REGFI_FILE* file, uint32 offset,
447                                    bool strict);
448const REGFI_HBIN*     regfi_lookup_hbin(REGFI_FILE* file, uint32 voffset);
449
450
451
[99]452/************************************/
453/*  Low-layer data structure access */
454/************************************/
[135]455REGFI_FILE*           regfi_parse_regf(int fd, bool strict);
456REGFI_HBIN*           regfi_parse_hbin(REGFI_FILE* file, uint32 offset, 
[110]457                                       bool strict);
[80]458
[99]459
460/* regfi_parse_nk: Parses an NK record.
461 *
462 * Arguments:
463 *   f        -- the registry file structure
464 *   offset   -- the offset of the cell (not the record) to be parsed.
465 *   max_size -- the maximum size the NK cell could be. (for validation)
466 *   strict   -- if true, rejects any malformed records.  Otherwise,
467 *               tries to minimally validate integrity.
468 * Returns:
469 *   A newly allocated NK record structure, or NULL on failure.
470 */
[150]471REGFI_NK_REC*         regfi_parse_nk(REGFI_FILE* file, uint32 offset,
[99]472                                     uint32 max_size, bool strict);
473
[139]474REGFI_SUBKEY_LIST*    regfi_parse_subkeylist(REGFI_FILE* file, uint32 offset,
475                                             uint32 max_size, bool strict);
476
[135]477REGFI_VK_REC*         regfi_parse_vk(REGFI_FILE* file, uint32 offset, 
478                                     uint32 max_size, bool strict);
[103]479
[145]480uint8*                regfi_parse_data(REGFI_FILE* file, 
481                                       uint32 data_type, uint32 offset, 
[135]482                                       uint32 length, uint32 max_size, 
[145]483                                       bool data_in_offset, bool strict);
[97]484
[135]485REGFI_SK_REC*         regfi_parse_sk(REGFI_FILE* file, uint32 offset, 
486                                     uint32 max_size, bool strict);
[111]487
[135]488range_list*           regfi_parse_unalloc_cells(REGFI_FILE* file);
[101]489
[135]490bool                  regfi_parse_cell(int fd, uint32 offset, 
491                                       uint8* hdr, uint32 hdr_len,
492                                       uint32* cell_length, bool* unalloc);
[111]493
[135]494char*                 regfi_parse_classname(REGFI_FILE* file, uint32 offset,
495                                            uint16* name_length, 
496                                            uint32 max_size, bool strict);
[126]497
[150]498/* Dispose of previously parsed records */
499void                  regfi_free_key(REGFI_NK_REC* nk);
500void                  regfi_free_value(REGFI_VK_REC* vk);
[127]501
[150]502
503
[135]504/************************************/
505/*    Private Functions             */
506/************************************/
507REGFI_NK_REC*         regfi_rootkey(REGFI_FILE* file);
508void                  regfi_subkeylist_free(REGFI_SUBKEY_LIST* list);
[127]509uint32                regfi_read(int fd, uint8* buf, uint32* length);
510
[135]511const char*           regfi_type_val2str(unsigned int val);
512int                   regfi_type_str2val(const char* str);
[127]513
[135]514char*                 regfi_get_sacl(WINSEC_DESC* sec_desc);
515char*                 regfi_get_dacl(WINSEC_DESC* sec_desc);
516char*                 regfi_get_owner(WINSEC_DESC* sec_desc);
517char*                 regfi_get_group(WINSEC_DESC* sec_desc);
518
519REGFI_SUBKEY_LIST*    regfi_merge_subkeylists(uint16 num_lists, 
520                                              REGFI_SUBKEY_LIST** lists,
521                                              bool strict);
[139]522REGFI_SUBKEY_LIST*    regfi_load_subkeylist_aux(REGFI_FILE* file, uint32 offset,
523                                                uint32 max_size, bool strict,
524                                                uint8 depth_left);
[138]525void                  regfi_add_message(REGFI_FILE* file, uint16 msg_type, 
526                                        const char* fmt, ...);
[146]527REGFI_NK_REC*         regfi_copy_nk(const REGFI_NK_REC* nk);
528REGFI_VK_REC*         regfi_copy_vk(const REGFI_VK_REC* vk);
529
[78]530#endif  /* _REGFI_H */
Note: See TracBrowser for help on using the repository browser.