source: trunk/include/regfi.h @ 107

Last change on this file since 107 was 107, checked in by tim, 16 years ago

removed redundant hbin parsing

added flags parameter to regfi_open

  • Property svn:keywords set to Id
File size: 11.8 KB
RevLine 
[30]1/*
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 *
5 * Unix SMB/CIFS implementation.
6 * Windows NT registry I/O library
7 *
[101]8 * Copyright (C) 2005-2008 Timothy D. Morgan
[30]9 * Copyright (C) 2005 Gerald (Jerry) Carter
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * $Id: regfi.h 107 2008-04-18 04:51:21Z tim $
25 */
26
27/************************************************************
28 * Most of this information was obtained from
29 * http://www.wednesday.demon.co.uk/dosreg.html
30 * Thanks Nigel!
31 ***********************************************************/
32
[78]33#ifndef _REGFI_H
34#define _REGFI_H
[30]35
[31]36#include <stdlib.h>
37#include <stdio.h>
[30]38#include <stdbool.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
48#include "smb_deps.h"
[78]49#include "void_stack.h"
[99]50#include "range_list.h"
[30]51
52/******************************************************************************/
53/* Macros */
54 
[32]55/* Registry data types */
56#define REG_NONE                       0
57#define REG_SZ                         1
58#define REG_EXPAND_SZ                  2
59#define REG_BINARY                     3
60#define REG_DWORD                      4
[54]61#define REG_DWORD_LE                   4  /* DWORD, little endian */
62#define REG_DWORD_BE                   5  /* DWORD, big endian */
[32]63#define REG_LINK                       6
64#define REG_MULTI_SZ                   7
65#define REG_RESOURCE_LIST              8
66#define REG_FULL_RESOURCE_DESCRIPTOR   9
67#define REG_RESOURCE_REQUIREMENTS_LIST 10
[72]68#define REG_QWORD                      11 /* 64-bit little endian */
69/* XXX: Has MS defined a REG_QWORD_BE? */
[32]70/* Not a real type in the registry */
[81]71#define REG_KEY                        0x7FFFFFFF
[32]72
[107]73#define REGFI_FLAG_NONE         0x0
74#define REGFI_FLAG_SAVE_UNALLOC 0x1
[32]75
[30]76#define REGF_BLOCKSIZE          0x1000
[97]77#define REGF_ALLOC_BLOCK        0x1000  /* Minimum allocation unit for hbins */
[78]78#define REGF_MAX_DEPTH          512
[30]79
80/* header sizes for various records */
81
[97]82#define REGF_MAGIC_SIZE         4
83#define HBIN_MAGIC_SIZE         4
84#define HBIN_HEADER_REC_SIZE    0x20
[30]85#define REC_HDR_SIZE            2
86
87#define REGF_OFFSET_NONE        0xffffffff
[101]88#define REGFI_NK_MIN_LENGTH     0x4C
89#define REGFI_VK_MIN_LENGTH     0x14
[102]90#define REGFI_SK_MIN_LENGTH     0x14
[104]91#define REGFI_HASH_LIST_MIN_LENGTH     0x4
[30]92
93/* Flags for the vk records */
94
95#define VK_FLAG_NAME_PRESENT    0x0001
96#define VK_DATA_IN_OFFSET       0x80000000
[77]97#define VK_MAX_DATA_LENGTH      1024*1024
[30]98
99/* NK record macros */
100
101#define NK_TYPE_LINKKEY         0x0010
102#define NK_TYPE_NORMALKEY       0x0020
103#define NK_TYPE_ROOTKEY         0x002c
[101]104  /* TODO: Unknown type that shows up in Vista registries */
105#define NK_TYPE_UNKNOWN1         0x1020
[30]106
[54]107#define HBIN_STORE_REF(x, y) { x->hbin = y; y->ref_count++ };
108/* if the count == 0; we can clean up */
109#define HBIN_REMOVE_REF(x, y){ x->hbin = NULL; y->ref_count-- };
[30]110
111
112/* HBIN block */
[97]113typedef struct regf_hbin
114{
[53]115  uint32 file_off;       /* my offset in the registry file */
[84]116  uint32 ref_count;      /* how many active records are pointing to this
[54]117                          * block (not used currently)
118                          */
[84]119 
[53]120  uint32 first_hbin_off; /* offset from first hbin block */
[97]121  uint32 block_size;     /* block size of this block
122                          * Should be a multiple of 4096 (0x1000)
[54]123                          */
[99]124  uint32 next_block;     /* relative offset to next block. 
125                          * NOTE: This value may be unreliable!
[97]126                          */
127
128  uint8 magic[HBIN_MAGIC_SIZE]; /* "hbin" */
[30]129} REGF_HBIN;
130
[97]131
132/* Hash List -- list of key offsets and hashed names for consistency */
133typedef struct 
134{
[53]135  uint32 nk_off;
[104]136  uint32 hash;
137} REGF_HASH_LIST_ELEM;
[30]138
[97]139
140typedef struct 
141{
[104]142  uint32 offset;        /* Real offset of this record's cell in the file */
143  uint32 cell_size;      /* ((start_offset - end_offset) & 0xfffffff8) */
[84]144  REGF_HBIN* hbin;       /* pointer to HBIN record (in memory) containing
[54]145                          * this nk record
146                          */
147  uint32 hbin_off;       /* offset from beginning of this hbin block */
[104]148  REGF_HASH_LIST_ELEM* hashes;
[53]149 
[104]150  uint8 magic[REC_HDR_SIZE];
[53]151  uint16 num_keys;
[104]152} REGF_HASH_LIST;
[30]153
[97]154
[30]155/* Key Value */
[97]156typedef struct 
157{
[101]158  uint32 offset;        /* Real offset of this record's cell in the file */
159  uint32 cell_size;     /* ((start_offset - end_offset) & 0xfffffff8) */
160
[84]161  REGF_HBIN* hbin;      /* pointer to HBIN record (in memory) containing
[54]162                         * this nk record
163                         */
[101]164  uint8* data;
165  uint16 name_length;
[84]166  char*  valuename;
[53]167  uint32 hbin_off;      /* offset from beginning of this hbin block */
168 
169  uint32 data_size;
[101]170  uint32 data_off;      /* offset of data cell (virtual) */
[53]171  uint32 type;
[101]172  uint8  magic[REC_HDR_SIZE];
[53]173  uint16 flag;
[101]174  uint16 unknown1;
[30]175} REGF_VK_REC;
176
177
178/* Key Security */
179struct _regf_sk_rec;
180
[97]181typedef struct _regf_sk_rec
182{
[84]183  struct _regf_sk_rec* next;
184  struct _regf_sk_rec* prev;
185  REGF_HBIN* hbin;      /* pointer to HBIN record (in memory) containing
[54]186                         * this nk record
187                         */
[84]188  SEC_DESC* sec_desc;
[53]189  uint32 hbin_off;      /* offset from beginning of this hbin block */
[99]190  uint32 cell_size;     /* ((start_offset - end_offset) & 0xfffffff8) */
[102]191  uint32 offset;        /* Real file offset of this record */
[53]192 
[54]193  uint32 sk_off;        /* offset parsed from NK record used as a key
194                         * to lookup reference to this SK record
195                         */
[53]196 
197  uint32 prev_sk_off;
198  uint32 next_sk_off;
199  uint32 ref_count;
[102]200  uint32 desc_size;     /* size of security descriptor */
201  uint16 unknown_tag;
202  uint8  magic[REC_HDR_SIZE];
[30]203} REGF_SK_REC;
204
[81]205
[104]206/* Key Name */
207typedef struct
[97]208{
[99]209  uint32 offset;        /* Real offset of this record's cell in the file */
210  uint32 cell_size;     /* Actual or estimated length of the cell. 
211                         * Always in multiples of 8.
212                         */
[84]213
214  /* link in the other records here */
[101]215  REGF_VK_REC** values;
[84]216  REGF_SK_REC* sec_desc;
[104]217  REGF_HASH_LIST* subkeys;
[53]218 
219  /* header information */
[84]220  /* XXX: should we be looking for types other than the root key type? */
221  uint16 key_type;     
[101]222  uint8  magic[REC_HDR_SIZE];
[53]223  NTTIME mtime;
[99]224  uint16 name_length;
225  uint16 classname_length;
[84]226  char* classname;
227  char* keyname;
[53]228  uint32 parent_off;    /* back pointer in registry hive */
229  uint32 classname_off; 
230 
231  /* max lengths */
[54]232  uint32 max_bytes_subkeyname;      /* max subkey name * 2 */
233  uint32 max_bytes_subkeyclassname; /* max subkey classname length (as if) */
234  uint32 max_bytes_valuename;       /* max valuename * 2 */
235  uint32 max_bytes_value;           /* max value data size */
[53]236 
237  /* unknowns */
[99]238  uint32 unknown1;
239  uint32 unknown2;
240  uint32 unknown3;
[54]241  uint32 unk_index;                 /* nigel says run time index ? */
[53]242 
243  /* children */
244  uint32 num_subkeys;
245  uint32 subkeys_off;   /* hash records that point to NK records */     
246  uint32 num_values;
247  uint32 values_off;    /* value lists which point to VK records */
[97]248  uint32 sk_off;        /* offset to SK record */ 
[30]249} REGF_NK_REC;
250
[81]251
[97]252
[30]253/* REGF block */
[97]254typedef struct 
255{
[53]256  /* run time information */
[54]257  int fd;         /* file descriptor */
[97]258  /* For sanity checking (not part of the registry header) */
259  uint32 file_length;
[84]260  void* mem_ctx;  /* memory context for run-time file access information */
[99]261
262  /* Experimental hbin lists */
263  range_list* hbins;
264  range_list* unalloc_cells;
265
[53]266  /* file format information */
[84]267  REGF_SK_REC* sec_desc_list;   /* list of security descriptors referenced
268                                 * by NK records
269                                 */
[53]270 
[97]271  uint8  magic[REGF_MAGIC_SIZE];/* "regf" */
[84]272  NTTIME mtime;
[54]273  uint32 data_offset;           /* offset to record in the first (or any?)
274                                 * hbin block
275                                 */
[53]276  uint32 last_block;            /* offset to last hbin block in file */
[97]277
278  uint32 checksum;              /* Stored checksum. */
279  uint32 computed_checksum;     /* Our own calculation of the checksum.
280                                 * (XOR of bytes 0x0000 - 0x01FB)
281                                 */
[53]282 
[97]283  /* unknown data structure values */
[53]284  uint32 unknown1;
285  uint32 unknown2;
286  uint32 unknown3;
287  uint32 unknown4;
288  uint32 unknown5;
289  uint32 unknown6;
[97]290  uint32 unknown7;
[53]291} REGF_FILE;
[30]292
293
[97]294
295typedef struct 
296{
[78]297  REGF_FILE* f;
[80]298  void_stack* key_positions;
299  REGF_NK_REC* cur_key;
[78]300  uint32 cur_subkey;
301  uint32 cur_value;
302} REGFI_ITERATOR;
303
[80]304
[97]305typedef struct 
306{
[80]307  REGF_NK_REC* nk;
308  uint32 cur_subkey;
309  /* We could store a cur_value here as well, but didn't see
310   * the use in it right now.
311   */
312} REGFI_ITER_POSITION;
313
314
[54]315/******************************************************************************/
[30]316/* Function Declarations */
[99]317/*  Main API */
[84]318const char*           regfi_type_val2str(unsigned int val);
319int                   regfi_type_str2val(const char* str);
[32]320
[84]321char*                 regfi_get_sacl(SEC_DESC* sec_desc);
322char*                 regfi_get_dacl(SEC_DESC* sec_desc);
323char*                 regfi_get_owner(SEC_DESC* sec_desc);
324char*                 regfi_get_group(SEC_DESC* sec_desc);
[53]325
[107]326REGF_FILE*            regfi_open(const char* filename, uint32 flags);
[84]327int                   regfi_close(REGF_FILE* r);
[30]328
[84]329REGFI_ITERATOR*       regfi_iterator_new(REGF_FILE* fh);
330void                  regfi_iterator_free(REGFI_ITERATOR* i);
331bool                  regfi_iterator_down(REGFI_ITERATOR* i);
332bool                  regfi_iterator_up(REGFI_ITERATOR* i);
333bool                  regfi_iterator_to_root(REGFI_ITERATOR* i);
[30]334
[84]335bool                  regfi_iterator_find_subkey(REGFI_ITERATOR* i, 
336                                                 const char* subkey_name);
337bool                  regfi_iterator_walk_path(REGFI_ITERATOR* i, 
338                                               const char** path);
339const REGF_NK_REC*    regfi_iterator_cur_key(REGFI_ITERATOR* i);
340const REGF_NK_REC*    regfi_iterator_first_subkey(REGFI_ITERATOR* i);
341const REGF_NK_REC*    regfi_iterator_cur_subkey(REGFI_ITERATOR* i);
342const REGF_NK_REC*    regfi_iterator_next_subkey(REGFI_ITERATOR* i);
[80]343
[84]344bool                  regfi_iterator_find_value(REGFI_ITERATOR* i, 
345                                                const char* value_name);
346const REGF_VK_REC*    regfi_iterator_first_value(REGFI_ITERATOR* i);
347const REGF_VK_REC*    regfi_iterator_cur_value(REGFI_ITERATOR* i);
348const REGF_VK_REC*    regfi_iterator_next_value(REGFI_ITERATOR* i);
[80]349
[99]350/************************************/
351/*  Low-layer data structure access */
352/************************************/
353REGF_FILE*            regfi_parse_regf(int fd, bool strict);
354REGF_HBIN*            regfi_parse_hbin(REGF_FILE* file, uint32 offset, 
355                                       bool strict, bool save_unalloc);
[80]356
[99]357
358/* regfi_parse_nk: Parses an NK record.
359 *
360 * Arguments:
361 *   f        -- the registry file structure
362 *   offset   -- the offset of the cell (not the record) to be parsed.
363 *   max_size -- the maximum size the NK cell could be. (for validation)
364 *   strict   -- if true, rejects any malformed records.  Otherwise,
365 *               tries to minimally validate integrity.
366 * Returns:
367 *   A newly allocated NK record structure, or NULL on failure.
368 */
369REGF_NK_REC*          regfi_parse_nk(REGF_FILE* file, uint32 offset, 
370                                     uint32 max_size, bool strict);
371
372
[84]373/* Private Functions */
374REGF_NK_REC*          regfi_rootkey(REGF_FILE* file);
375void                  regfi_key_free(REGF_NK_REC* nk);
[99]376uint32                regfi_read(int fd, uint8* buf, uint32* length);
[78]377
[84]378
[97]379
380/****************/
381/* Experimental */
382/****************/
[105]383REGF_NK_REC* regfi_load_key(REGF_FILE* file, uint32 offset, bool strict);
384
[104]385REGF_HASH_LIST* regfi_load_hashlist(REGF_FILE* file, uint32 offset, 
[105]386                                    uint32 num_keys, uint32 max_size, 
387                                    bool strict);
[97]388
[103]389REGF_VK_REC** regfi_load_valuelist(REGF_FILE* file, uint32 offset, 
[105]390                                   uint32 num_values, uint32 max_size, 
391                                   bool strict);
[103]392
[101]393REGF_VK_REC* regfi_parse_vk(REGF_FILE* file, uint32 offset, 
394                            uint32 max_size, bool strict);
[103]395
[101]396uint8* regfi_parse_data(REGF_FILE* file, uint32 offset, 
397                        uint32 length, bool strict);
[97]398
[101]399
[78]400#endif  /* _REGFI_H */
Note: See TracBrowser for help on using the repository browser.