source: trunk/include/regfi.h @ 104

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

replaced lf (hash list) parsing code

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