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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
Samba Unix/Linux SMB client utility libeditreg.c
Copyright (C) 2004 Jelmer Vernooij, jelmer@samba.org
Backend for Windows '95 registry files. Explanation of file format
comes from http://www.cs.mun.ca/~michael/regutils/.
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "includes.h"
#include "registry.h"
#include "system/filesys.h"
#include "system/shmem.h"
/**
* The registry starts with a header that contains pointers to
* the rgdb.
*
* After the main header follows the RGKN header (key index table).
* The RGKN keys are listed after each other. They are put into
* blocks, the first having a length of 0x2000 bytes, the others
* being 0x1000 bytes long.
*
* After the RGKN header follow one or more RGDB blocks. These blocks
* contain keys. A key is followed by its name and its values.
*
* Values are followed by their name and then their data.
*
* Basically the idea is that the RGKN contains the associations between
* the keys and the RGDB contains the actual data.
*/
typedef uint_t DWORD;
typedef unsigned short WORD;
typedef struct creg_block {
DWORD CREG_ID; /* CREG */
DWORD uk1;
DWORD rgdb_offset;
DWORD chksum;
WORD num_rgdb;
WORD flags;
DWORD uk2;
DWORD uk3;
DWORD uk4;
} CREG_HDR;
typedef struct rgkn_block {
DWORD RGKN_ID; /* RGKN */
DWORD size;
DWORD root_offset;
DWORD free_offset;
DWORD flags;
DWORD chksum;
DWORD uk1;
DWORD uk2;
} RGKN_HDR;
typedef struct reg_id {
WORD id;
WORD rgdb;
} REG_ID;
typedef struct rgkn_key {
DWORD type; /* 0x00000000 = normal key, 0x80000000 = free block */
DWORD hash; /* Contains either hash or size of free blocks that follows */
DWORD next_free;
DWORD parent_offset;
DWORD first_child_offset;
DWORD next_offset;
REG_ID id;
} RGKN_KEY;
typedef struct rgdb_block {
DWORD RGDB_ID; /* RGDB */
DWORD size;
DWORD unused_size;
WORD flags;
WORD section;
DWORD free_offset; /* -1 if there is no free space */
WORD max_id;
WORD first_free_id;
DWORD uk1;
DWORD chksum;
} RGDB_HDR;
typedef struct rgdb_key {
DWORD size;
REG_ID id;
DWORD used_size;
WORD name_len;
WORD num_values;
DWORD uk1;
} RGDB_KEY;
typedef struct rgdb_value {
DWORD type;
DWORD uk1;
WORD name_len;
WORD data_len;
} RGDB_VALUE;
typedef struct creg_struct_s {
int fd;
BOOL modified;
char *base;
struct stat sbuf;
CREG_HDR *creg_hdr;
RGKN_HDR *rgkn_hdr;
RGDB_KEY ***rgdb_keys;
} CREG;
#define RGKN_START_SIZE 0x2000
#define RGKN_INC_SIZE 0x1000
#define LOCN_RGKN(creg, o) ((RGKN_KEY *)((creg)->base + sizeof(CREG_HDR) + o))
#define LOCN_RGDB_BLOCK(creg, o) (((creg)->base + (creg)->creg_hdr->rgdb_offset + o))
#define LOCN_RGDB_KEY(creg, rgdb, id) ((RGDB_KEY *)((creg)->rgdb_keys[(rgdb)][(id)]))
static DWORD str_to_dword(const char *a) {
int i;
unsigned long ret = 0;
for(i = strlen(a)-1; i >= 0; i--) {
ret = ret * 0x100 + a[i];
}
return ret;
}
#if 0 /* unused */
static DWORD calc_hash(const char *str) {
DWORD ret = 0;
int i;
for(i = 0; str[i] && str[i] != '\\'; i++) {
ret+=toupper(str[i]);
}
return ret;
}
static void parse_rgkn_block(CREG *creg, off_t start_off, off_t end_off)
{
off_t i;
for(i = start_off; end_off - i > sizeof(RGKN_KEY); i+= sizeof(RGKN_KEY)) {
RGKN_KEY *key = (RGKN_KEY *)LOCN_RGKN(creg, i);
if(key->type == 0) {
DEBUG(4,("Regular, id: %d, %d, parent: %x, firstchild: %x, next: %x hash: %lX\n", key->id.id, key->id.rgdb, key->parent_offset, key->first_child_offset, key->next_offset, (long)key->hash));
} else if(key->type == 0x80000000) {
DEBUG(3,("free\n"));
i += key->hash;
} else {
DEBUG(0,("Invalid key type in RGKN: %0X\n", key->type));
}
}
}
#endif
static void parse_rgdb_block(CREG *creg, RGDB_HDR *rgdb_hdr)
{
DWORD used_size = rgdb_hdr->size - rgdb_hdr->unused_size;
DWORD offset = 0;
while(offset < used_size) {
RGDB_KEY *key = (RGDB_KEY *)(((char *)rgdb_hdr) + sizeof(RGDB_HDR) + offset);
if(!(key->id.id == 0xFFFF && key->id.rgdb == 0xFFFF))creg->rgdb_keys[key->id.rgdb][key->id.id] = key;
offset += key->size;
}
}
static WERROR w95_open_reg (struct registry_hive *h, struct registry_key **root)
{
CREG *creg;
DWORD creg_id, rgkn_id;
DWORD i;
DWORD offset;
creg = talloc(h, CREG);
memset(creg, 0, sizeof(CREG));
h->backend_data = creg;
if((creg->fd = open(h->location, O_RDONLY, 0000)) < 0) {
return WERR_FOOBAR;
}
if (fstat(creg->fd, &creg->sbuf) < 0) {
return WERR_FOOBAR;
}
creg->base = mmap(0, creg->sbuf.st_size, PROT_READ, MAP_SHARED, creg->fd, 0);
if ((int)creg->base == 1) {
DEBUG(0,("Could not mmap file: %s, %s\n", h->location, strerror(errno)));
return WERR_FOOBAR;
}
creg->creg_hdr = (CREG_HDR *)creg->base;
if ((creg_id = IVAL(&creg->creg_hdr->CREG_ID,0)) != str_to_dword("CREG")) {
DEBUG(0, ("Unrecognized Windows 95 registry header id: 0x%0X, %s\n",
creg_id, h->location));
return WERR_FOOBAR;
}
creg->rgkn_hdr = (RGKN_HDR *)LOCN_RGKN(creg, 0);
if ((rgkn_id = IVAL(&creg->rgkn_hdr->RGKN_ID,0)) != str_to_dword("RGKN")) {
DEBUG(0, ("Unrecognized Windows 95 registry key index id: 0x%0X, %s\n",
rgkn_id, h->location));
return WERR_FOOBAR;
}
#if 0
/* If'ed out because we only need to parse this stuff when allocating new
* entries (which we don't do at the moment */
/* First parse the 0x2000 long block */
parse_rgkn_block(creg, sizeof(RGKN_HDR), 0x2000);
/* Then parse the other 0x1000 length blocks */
for(offset = 0x2000; offset < creg->rgkn_hdr->size; offset+=0x1000) {
parse_rgkn_block(creg, offset, offset+0x1000);
}
#endif
creg->rgdb_keys = talloc_array(h, RGDB_KEY **, creg->creg_hdr->num_rgdb);
offset = 0;
DEBUG(3, ("Reading %d rgdb entries\n", creg->creg_hdr->num_rgdb));
for(i = 0; i < creg->creg_hdr->num_rgdb; i++) {
RGDB_HDR *rgdb_hdr = (RGDB_HDR *)LOCN_RGDB_BLOCK(creg, offset);
if(strncmp((char *)&(rgdb_hdr->RGDB_ID), "RGDB", 4)) {
DEBUG(0, ("unrecognized rgdb entry: %4d, %s\n",
rgdb_hdr->RGDB_ID, h->location));
return WERR_FOOBAR;
} else {
DEBUG(3, ("Valid rgdb entry, first free id: %d, max id: %d\n", rgdb_hdr->first_free_id, rgdb_hdr->max_id));
}
creg->rgdb_keys[i] = talloc_array(h, RGDB_KEY *, rgdb_hdr->max_id+1);
memset(creg->rgdb_keys[i], 0, sizeof(RGDB_KEY *) * (rgdb_hdr->max_id+1));
parse_rgdb_block(creg, rgdb_hdr);
offset+=rgdb_hdr->size;
}
/* First element in rgkn should be root key */
*root = talloc(h, struct registry_key);
(*root)->name = NULL;
(*root)->backend_data = LOCN_RGKN(creg, sizeof(RGKN_HDR));
return WERR_OK;
}
static WERROR w95_get_subkey_by_index (TALLOC_CTX *mem_ctx, const struct registry_key *parent, int n, struct registry_key **key)
{
CREG *creg = parent->hive->backend_data;
RGKN_KEY *rgkn_key = parent->backend_data;
RGKN_KEY *child;
DWORD child_offset;
DWORD cur = 0;
/* Get id of first child */
child_offset = rgkn_key->first_child_offset;
while(child_offset != 0xFFFFFFFF) {
child = LOCN_RGKN(creg, child_offset);
/* n == cur ? return! */
if(cur == n) {
RGDB_KEY *rgdb_key;
rgdb_key = LOCN_RGDB_KEY(creg, child->id.rgdb, child->id.id);
if(!rgdb_key) {
DEBUG(0, ("Can't find %d,%d in RGDB table!\n", child->id.rgdb, child->id.id));
return WERR_FOOBAR;
}
*key = talloc(mem_ctx, struct registry_key);
(*key)->backend_data = child;
(*key)->name = talloc_strndup(mem_ctx, (char *)rgdb_key + sizeof(RGDB_KEY), rgdb_key->name_len);
return WERR_OK;
}
cur++;
child_offset = child->next_offset;
}
return WERR_NO_MORE_ITEMS;
}
static WERROR w95_num_values(const struct registry_key *k, uint32_t *count)
{
RGKN_KEY *rgkn_key = k->backend_data;
RGDB_KEY *rgdb_key = LOCN_RGDB_KEY((CREG *)k->hive->backend_data, rgkn_key->id.rgdb, rgkn_key->id.id);
if(!rgdb_key) return WERR_FOOBAR;
*count = rgdb_key->num_values;
return WERR_OK;
}
static WERROR w95_get_value_by_id(TALLOC_CTX *mem_ctx, const struct registry_key *k, int idx, struct registry_value **value)
{
RGKN_KEY *rgkn_key = k->backend_data;
DWORD i;
DWORD offset = 0;
RGDB_KEY *rgdb_key = LOCN_RGDB_KEY((CREG *)k->hive->backend_data, rgkn_key->id.rgdb, rgkn_key->id.id);
RGDB_VALUE *curval = NULL;
if(!rgdb_key) return WERR_FOOBAR;
if(idx >= rgdb_key->num_values) return WERR_NO_MORE_ITEMS;
for(i = 0; i < idx; i++) {
curval = (RGDB_VALUE *)(((char *)rgdb_key) + sizeof(RGDB_KEY) + rgdb_key->name_len + offset);
offset+=sizeof(RGDB_VALUE) + curval->name_len + curval->data_len;
}
*value = talloc(mem_ctx, struct registry_value);
(*value)->name = talloc_strndup(mem_ctx, (char *)curval+sizeof(RGDB_VALUE), curval->name_len);
(*value)->data = data_blob_talloc(mem_ctx, curval+sizeof(RGDB_VALUE)+curval->name_len, curval->data_len);
(*value)->data_type = curval->type;
return WERR_OK;
}
static struct hive_operations reg_backend_w95 = {
.name = "w95",
.open_hive = w95_open_reg,
.get_value_by_index = w95_get_value_by_id,
.num_values = w95_num_values,
.get_subkey_by_index = w95_get_subkey_by_index,
};
NTSTATUS registry_w95_init(void)
{
return registry_register(®_backend_w95);
}
|