summaryrefslogtreecommitdiffstats
path: root/source3/registry/regfio.h
blob: 3c2b6560bfa6954c873fcf134f2fb0fd0b07c125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Unix SMB/CIFS implementation.
 * Windows NT registry I/O library
 * Copyright (c) Gerald (Jerry) Carter               2005
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

/************************************************************
 * Most of this information was obtained from 
 * http://www.wednesday.demon.co.uk/dosreg.html
 * Thanks Nigel!
 ***********************************************************/

#include "registry/reg_parse_prs.h"
#include "registry/reg_objects.h"

#ifndef _REGFIO_H
#define _REGFIO_H

struct regsubkey_ctr;

/* Macros */
 
#define REGF_BLOCKSIZE		0x1000
#define REGF_ALLOC_BLOCK	0x1000

/* header sizes for various records */

#define REGF_HDR_SIZE		4
#define HBIN_HDR_SIZE		4
#define HBIN_HEADER_REC_SIZE	0x24
#define REC_HDR_SIZE		2

#define REGF_OFFSET_NONE	0xffffffff

/* Flags for the vk records */

#define VK_FLAG_NAME_PRESENT	0x0001
#define VK_DATA_IN_OFFSET	0x80000000

/* NK record macros */

#define NK_TYPE_LINKKEY		0x0010
#define NK_TYPE_NORMALKEY	0x0020
#define NK_TYPE_ROOTKEY		0x002c

#define HBIN_STORE_REF(x, y)	{ x->hbin = y; y->ref_count++ };
#define HBIN_REMOVE_REF(x, y)	{ x->hbin = NULL; y->ref_count-- /* if the count == 0; we can clean up */ };


/* HBIN block */
struct regf_hbin;
typedef struct regf_hbin {
	struct regf_hbin *prev, *next;
	uint32 file_off;		/* my offset in the registry file */
	uint32 free_off;		/* offset to free space within the hbin record */
	uint32 free_size;		/* amount of data left in the block */
	int ref_count;			/* how many active records are pointing to this block (not used currently) */
	
	char   header[HBIN_HDR_SIZE];	/* "hbin" */
	uint32 first_hbin_off;		/* offset from first hbin block */
	uint32 block_size;		/* block size of this blockually a multiple of 4096Kb) */

	prs_struct ps;			/* data */

	bool dirty;			/* has this hbin block been modified? */
} REGF_HBIN;

/* ??? List -- list of key offsets and hashed names for consistency */

typedef struct {
	uint32 nk_off;
	uint8 keycheck[sizeof(uint32)];
	char *fullname;
} REGF_HASH_REC;

typedef struct {
	REGF_HBIN *hbin;	/* pointer to HBIN record (in memory) containing this nk record */
	uint32 hbin_off;	/* offset from beginning of this hbin block */
	uint32 rec_size;	/* ((start_offset - end_offset) & 0xfffffff8) */

	char header[REC_HDR_SIZE];
	uint16 num_keys;
	REGF_HASH_REC *hashes;
} REGF_LF_REC;

/* Key Value */

typedef struct {
	REGF_HBIN *hbin;	/* pointer to HBIN record (in memory) containing this nk record */
	uint32 hbin_off;	/* offset from beginning of this hbin block */
	uint32 rec_size;	/* ((start_offset - end_offset) & 0xfffffff8) */
	uint32 rec_off;		/* offset stored in the value list */
	
	char header[REC_HDR_SIZE];
	char *valuename;
	uint32 data_size;
	uint32 data_off;
	uint8  *data;
	uint32 type;
	uint16 flag;
} REGF_VK_REC;


/* Key Security */
struct _regf_sk_rec;

typedef struct _regf_sk_rec {
	struct _regf_sk_rec *next, *prev;
	REGF_HBIN *hbin;	/* pointer to HBIN record (in memory) containing this nk record */
	uint32 hbin_off;	/* offset from beginning of this hbin block */
	uint32 rec_size;	/* ((start_offset - end_offset) & 0xfffffff8) */

	uint32 sk_off;		/* offset parsed from NK record used as a key
				   to lookup reference to this SK record */

	char header[REC_HDR_SIZE];
	uint32 prev_sk_off;
	uint32 next_sk_off;
	uint32 ref_count;
	uint32 size;
	struct security_descriptor *sec_desc;
} REGF_SK_REC;

/* Key Name */ 

typedef struct {
	REGF_HBIN *hbin;	/* pointer to HBIN record (in memory) containing this nk record */
	uint32 hbin_off;	/* offset from beginning of this hbin block */
	uint32 subkey_index;	/* index to next subkey record to return */
	uint32 rec_size;	/* ((start_offset - end_offset) & 0xfffffff8) */
	
	/* header information */
	
	char header[REC_HDR_SIZE];
	uint16 key_type;
	NTTIME mtime;
	uint32 parent_off;	/* back pointer in registry hive */
	uint32 classname_off;	
	char *classname;
	char *keyname;

	/* max lengths */

	uint32 max_bytes_subkeyname;		/* max subkey name * 2 */
	uint32 max_bytes_subkeyclassname;	/* max subkey classname length (as if) */
	uint32 max_bytes_valuename;		/* max valuename * 2 */
	uint32 max_bytes_value;			/* max value data size */

	/* unknowns */

	uint32 unk_index;			/* nigel says run time index ? */
	
	/* children */
	
	uint32 num_subkeys;
	uint32 subkeys_off;	/* hash records that point to NK records */	
	uint32 num_values;
	uint32 values_off;	/* value lists which point to VK records */
	uint32 sk_off;		/* offset to SK record */
	
	/* link in the other records here */
	
	REGF_LF_REC subkeys;
	REGF_VK_REC *values;
	REGF_SK_REC *sec_desc;
	
} REGF_NK_REC;

/* REGF block */
 
typedef struct {
	/* run time information */

	int fd;				/* file descriptor */
	int open_flags;			/* flags passed to the open() call */
	TALLOC_CTX *mem_ctx;		/* memory context for run-time file access information */
	REGF_HBIN *block_list;		/* list of open hbin blocks */

	/* file format information */

	char   header[REGF_HDR_SIZE];	/* "regf" */
	uint32 data_offset;		/* offset to record in the first (or any?) hbin block */
	uint32 last_block;		/* offset to last hbin block in file */
	uint32 checksum;		/* XOR of bytes 0x0000 - 0x01FB */
	NTTIME mtime;
	
	REGF_SK_REC *sec_desc_list;	/* list of security descriptors referenced by NK records */

	/* unknowns used to simply writing */
	
	uint32 unknown1;
	uint32 unknown2;
	uint32 unknown3;
	uint32 unknown4;
	uint32 unknown5;
	uint32 unknown6;
	
} REGF_FILE;

/* Function Declarations */
 
REGF_FILE*    regfio_open( const char *filename, int flags, int mode );
int           regfio_close( REGF_FILE *r );

REGF_NK_REC*  regfio_rootkey( REGF_FILE *file );
REGF_NK_REC*  regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk );
REGF_NK_REC*  regfio_write_key ( REGF_FILE *file, const char *name,
                                 struct regval_ctr *values, struct regsubkey_ctr *subkeys,
                                 struct security_descriptor *sec_desc, REGF_NK_REC *parent );


#endif	/* _REGFIO_H */