2 Samba Unix/Linux SMB client utility libeditreg.c
3 Copyright (C) 2004 Jelmer Vernooij, jelmer@samba.org
5 Backend for Windows '95 registry files. Explanation of file format
6 comes from http://www.cs.mun.ca/~michael/regutils/.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "lib/registry/common/registry.h"
26 * The registry starts with a header that contains pointers to
29 * After the main header follows the RGKN header (key index table).
30 * The RGKN keys are listed after each other. They are put into
31 * blocks, the first having a length of 0x2000 bytes, the others
32 * being 0x1000 bytes long.
34 * After the RGKN header follow one or more RGDB blocks. These blocks
35 * contain keys. A key is followed by its name and its values.
37 * Values are followed by their name and then their data.
39 * Basically the idea is that the RGKN contains the associations between
40 * the keys and the RGDB contains the actual data.
43 typedef unsigned int DWORD;
44 typedef unsigned short WORD;
46 typedef struct creg_block {
47 DWORD CREG_ID; /* CREG */
58 typedef struct rgkn_block {
59 DWORD RGKN_ID; /* RGKN */
69 typedef struct reg_id {
74 typedef struct rgkn_key {
75 DWORD type; /* 0x00000000 = normal key, 0x80000000 = free block */
76 DWORD hash; /* Contains either hash or size of free blocks that follows */
79 DWORD first_child_offset;
85 typedef struct rgdb_block {
86 DWORD RGDB_ID; /* RGDB */
91 DWORD free_offset; /* -1 if there is no free space */
98 typedef struct rgdb_key {
107 typedef struct rgdb_value {
114 typedef struct creg_struct_s {
121 RGDB_KEY ***rgdb_keys;
124 #define RGKN_START_SIZE 0x2000
125 #define RGKN_INC_SIZE 0x1000
127 #define LOCN_RGKN(creg, o) ((RGKN_KEY *)((creg)->base + sizeof(CREG_HDR) + o))
128 #define LOCN_RGDB_BLOCK(creg, o) (((creg)->base + (creg)->creg_hdr->rgdb_offset + o))
129 #define LOCN_RGDB_KEY(creg, rgdb, id) ((RGDB_KEY *)((creg)->rgdb_keys[(rgdb)][(id)]))
131 static DWORD str_to_dword(const char *a) {
133 unsigned long ret = 0;
134 for(i = strlen(a)-1; i >= 0; i--) {
135 ret = ret * 0x100 + a[i];
142 static DWORD calc_hash(const char *str) {
145 for(i = 0; str[i] && str[i] != '\\'; i++) {
146 ret+=toupper(str[i]);
151 static void parse_rgkn_block(CREG *creg, off_t start_off, off_t end_off)
154 for(i = start_off; end_off - i > sizeof(RGKN_KEY); i+= sizeof(RGKN_KEY)) {
155 RGKN_KEY *key = (RGKN_KEY *)LOCN_RGKN(creg, i);
157 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));
158 } else if(key->type == 0x80000000) {
162 DEBUG(0,("Invalid key type in RGKN: %0X\n", key->type));
169 static void parse_rgdb_block(CREG *creg, RGDB_HDR *rgdb_hdr)
171 DWORD used_size = rgdb_hdr->size - rgdb_hdr->unused_size;
174 while(offset < used_size) {
175 RGDB_KEY *key = (RGDB_KEY *)(((char *)rgdb_hdr) + sizeof(RGDB_HDR) + offset);
177 if(!(key->id.id == 0xFFFF && key->id.rgdb == 0xFFFF))creg->rgdb_keys[key->id.rgdb][key->id.id] = key;
182 static WERROR w95_open_root (REG_HANDLE *h, REG_KEY **key)
184 CREG *creg = h->backend_data;
186 /* First element in rgkn should be root key */
187 *key = reg_key_new_abs("\\", h, LOCN_RGKN(creg, sizeof(RGKN_HDR)));
192 static WERROR w95_get_subkey_by_index (REG_KEY *parent, int n, REG_KEY **key)
194 CREG *creg = parent->handle->backend_data;
195 RGKN_KEY *rgkn_key = parent->backend_data;
200 /* Get id of first child */
201 child_offset = rgkn_key->first_child_offset;
203 while(child_offset != 0xFFFFFFFF) {
204 child = LOCN_RGKN(creg, child_offset);
206 /* n == cur ? return! */
210 rgdb_key = LOCN_RGDB_KEY(creg, child->id.rgdb, child->id.id);
212 DEBUG(0, ("Can't find %d,%d in RGDB table!\n", child->id.rgdb, child->id.id));
215 name = strndup((char *)rgdb_key + sizeof(RGDB_KEY), rgdb_key->name_len);
216 *key = reg_key_new_rel(name, parent, child);
223 child_offset = child->next_offset;
226 return WERR_NO_MORE_ITEMS;
229 static WERROR w95_open_reg (REG_HANDLE *h, const char *location, const char *credentials)
232 DWORD creg_id, rgkn_id;
236 creg = talloc_p(h->mem_ctx, CREG);
237 memset(creg, 0, sizeof(CREG));
238 h->backend_data = creg;
240 if((creg->fd = open(location, O_RDONLY, 0000)) < 0) {
244 if (fstat(creg->fd, &creg->sbuf) < 0) {
248 creg->base = mmap(0, creg->sbuf.st_size, PROT_READ, MAP_SHARED, creg->fd, 0);
250 if ((int)creg->base == 1) {
251 DEBUG(0,("Could not mmap file: %s, %s\n", location, strerror(errno)));
255 creg->creg_hdr = (CREG_HDR *)creg->base;
257 if ((creg_id = IVAL(&creg->creg_hdr->CREG_ID,0)) != str_to_dword("CREG")) {
258 DEBUG(0, ("Unrecognized Windows 95 registry header id: 0x%0X, %s\n",
263 creg->rgkn_hdr = (RGKN_HDR *)LOCN_RGKN(creg, 0);
265 if ((rgkn_id = IVAL(&creg->rgkn_hdr->RGKN_ID,0)) != str_to_dword("RGKN")) {
266 DEBUG(0, ("Unrecognized Windows 95 registry key index id: 0x%0X, %s\n",
272 /* If'ed out because we only need to parse this stuff when allocating new
273 * entries (which we don't do at the moment */
274 /* First parse the 0x2000 long block */
275 parse_rgkn_block(creg, sizeof(RGKN_HDR), 0x2000);
277 /* Then parse the other 0x1000 length blocks */
278 for(offset = 0x2000; offset < creg->rgkn_hdr->size; offset+=0x1000) {
279 parse_rgkn_block(creg, offset, offset+0x1000);
283 creg->rgdb_keys = talloc_array_p(h->mem_ctx, RGDB_KEY **, creg->creg_hdr->num_rgdb);
286 DEBUG(3, ("Reading %d rgdb entries\n", creg->creg_hdr->num_rgdb));
287 for(i = 0; i < creg->creg_hdr->num_rgdb; i++) {
288 RGDB_HDR *rgdb_hdr = (RGDB_HDR *)LOCN_RGDB_BLOCK(creg, offset);
290 if(strncmp((char *)&(rgdb_hdr->RGDB_ID), "RGDB", 4)) {
291 DEBUG(0, ("unrecognized rgdb entry: %4d, %s\n",
292 rgdb_hdr->RGDB_ID, location));
295 DEBUG(3, ("Valid rgdb entry, first free id: %d, max id: %d\n", rgdb_hdr->first_free_id, rgdb_hdr->max_id));
299 creg->rgdb_keys[i] = talloc_array_p(h->mem_ctx, RGDB_KEY *, rgdb_hdr->max_id+1);
300 memset(creg->rgdb_keys[i], 0, sizeof(RGDB_KEY *) * (rgdb_hdr->max_id+1));
302 parse_rgdb_block(creg, rgdb_hdr);
304 offset+=rgdb_hdr->size;
311 static WERROR w95_close_reg(REG_HANDLE *h)
313 CREG *creg = h->backend_data;
314 if (creg->base) munmap(creg->base, creg->sbuf.st_size);
321 static WERROR w95_fetch_values(REG_KEY *k, int *count, REG_VAL ***values)
323 RGKN_KEY *rgkn_key = k->backend_data;
326 RGDB_KEY *rgdb_key = LOCN_RGDB_KEY((CREG *)k->handle->backend_data, rgkn_key->id.rgdb, rgkn_key->id.id);
328 if(!rgdb_key) return WERR_FOOBAR;
330 *count = rgdb_key->num_values;
332 if((*count) == 0) return WERR_OK;
334 (*values) = talloc_array_p(k->mem_ctx, REG_VAL *, (*count)+1);
335 for(i = 0; i < rgdb_key->num_values; i++) {
336 RGDB_VALUE *val = (RGDB_VALUE *)(((char *)rgdb_key) + sizeof(RGDB_KEY) + rgdb_key->name_len + offset);
337 (*values)[i] = reg_val_new(k, val);
340 (*values)[i]->name = talloc_strndup(k->mem_ctx, (char *)val+sizeof(RGDB_VALUE), val->name_len);
343 (*values)[i]->data_len = val->data_len;
344 (*values)[i]->data_blk = talloc_memdup((*values)[i]->mem_ctx, (char *)val+sizeof(RGDB_VALUE)+val->name_len, val->data_len);
347 (*values)[i]->data_type = val->type;
349 offset+=sizeof(RGDB_VALUE) + val->name_len + val->data_len;
355 static struct registry_ops reg_backend_w95 = {
357 .open_registry = w95_open_reg,
358 .close_registry = w95_close_reg,
359 .open_root_key = w95_open_root,
360 .fetch_values = w95_fetch_values,
361 .get_subkey_by_index = w95_get_subkey_by_index,
364 NTSTATUS reg_w95_init(void)
366 return register_backend("registry", ®_backend_w95);