docs-xml: fix reference from "server min protocol" to "server max protocol"
[Samba/gebeck_regimport.git] / source4 / lib / registry / regf.c
blobb6a586cf78bd6edad3909761c43edbd219683583
1 /*
2 Samba CIFS implementation
3 Registry backend for REGF files
4 Copyright (C) 2005-2007 Jelmer Vernooij, jelmer@samba.org
5 Copyright (C) 2006-2010 Wilco Baan Hofman, wilco@baanhofman.nl
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "system/time.h"
23 #include "lib/registry/tdr_regf.h"
24 #include "librpc/gen_ndr/ndr_security.h"
25 #include "librpc/gen_ndr/winreg.h"
26 #include "lib/registry/registry.h"
27 #include "libcli/security/security.h"
30 static struct hive_operations reg_backend_regf;
32 /**
33 * There are several places on the web where the REGF format is explained;
35 * TODO: Links
38 /* TODO:
39 * - Return error codes that make more sense
40 * - Locking
41 * - do more things in-memory
45 * Read HBIN blocks into memory
48 struct regf_data {
49 int fd;
50 struct hbin_block **hbins;
51 struct regf_hdr *header;
52 time_t last_write;
55 static WERROR regf_save_hbin(struct regf_data *data, bool flush);
57 struct regf_key_data {
58 struct hive_key key;
59 struct regf_data *hive;
60 uint32_t offset;
61 struct nk_block *nk;
64 static struct hbin_block *hbin_by_offset(const struct regf_data *data,
65 uint32_t offset, uint32_t *rel_offset)
67 unsigned int i;
69 for (i = 0; data->hbins[i]; i++) {
70 if (offset >= data->hbins[i]->offset_from_first &&
71 offset < data->hbins[i]->offset_from_first+
72 data->hbins[i]->offset_to_next) {
73 if (rel_offset != NULL)
74 *rel_offset = offset - data->hbins[i]->offset_from_first - 0x20;
75 return data->hbins[i];
79 return NULL;
82 /**
83 * Validate a regf header
84 * For now, do nothing, but we should check the checksum
86 static uint32_t regf_hdr_checksum(const uint8_t *buffer)
88 uint32_t checksum = 0, x;
89 unsigned int i;
91 for (i = 0; i < 0x01FB; i+= 4) {
92 x = IVAL(buffer, i);
93 checksum ^= x;
96 return checksum;
99 /**
100 * Obtain the contents of a HBIN block
102 static DATA_BLOB hbin_get(const struct regf_data *data, uint32_t offset)
104 DATA_BLOB ret;
105 struct hbin_block *hbin;
106 uint32_t rel_offset;
108 ret.data = NULL;
109 ret.length = 0;
111 hbin = hbin_by_offset(data, offset, &rel_offset);
113 if (hbin == NULL) {
114 DEBUG(1, ("Can't find HBIN at 0x%04x\n", offset));
115 return ret;
118 ret.length = IVAL(hbin->data, rel_offset);
119 if (!(ret.length & 0x80000000)) {
120 DEBUG(0, ("Trying to use dirty block at 0x%04x\n", offset));
121 return ret;
124 /* remove high bit */
125 ret.length = (ret.length ^ 0xffffffff) + 1;
127 ret.length -= 4; /* 4 bytes for the length... */
128 ret.data = hbin->data +
129 (offset - hbin->offset_from_first - 0x20) + 4;
131 return ret;
134 static bool hbin_get_tdr(struct regf_data *regf, uint32_t offset,
135 TALLOC_CTX *ctx, tdr_pull_fn_t pull_fn, void *p)
137 struct tdr_pull *pull = tdr_pull_init(regf);
139 pull->data = hbin_get(regf, offset);
140 if (!pull->data.data) {
141 DEBUG(1, ("Unable to get data at 0x%04x\n", offset));
142 talloc_free(pull);
143 return false;
146 if (NT_STATUS_IS_ERR(pull_fn(pull, ctx, p))) {
147 DEBUG(1, ("Error parsing record at 0x%04x using tdr\n",
148 offset));
149 talloc_free(pull);
150 return false;
152 talloc_free(pull);
154 return true;
157 /* Allocate some new data */
158 static DATA_BLOB hbin_alloc(struct regf_data *data, uint32_t size,
159 uint32_t *offset)
161 DATA_BLOB ret;
162 uint32_t rel_offset = (uint32_t) -1; /* Relative offset ! */
163 struct hbin_block *hbin = NULL;
164 unsigned int i;
166 *offset = 0;
168 if (size == 0)
169 return data_blob(NULL, 0);
171 size += 4; /* Need to include int32 for the length */
173 /* Allocate as a multiple of 8 */
174 size = (size + 7) & ~7;
176 ret.data = NULL;
177 ret.length = 0;
179 for (i = 0; (hbin = data->hbins[i]); i++) {
180 int j;
181 int32_t my_size;
182 for (j = 0; j < hbin->offset_to_next-0x20; j+= my_size) {
183 my_size = IVALS(hbin->data, j);
185 if (my_size == 0x0) {
186 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
187 return ret;
190 if (my_size % 8 != 0) {
191 DEBUG(0, ("Encountered non-aligned block!\n"));
194 if (my_size < 0) { /* Used... */
195 my_size = -my_size;
196 } else if (my_size == size) { /* exact match */
197 rel_offset = j;
198 DEBUG(4, ("Found free block of exact size %d in middle of HBIN\n",
199 size));
200 break;
201 } else if (my_size > size) { /* data will remain */
202 rel_offset = j;
203 /* Split this block and mark the next block as free */
204 SIVAL(hbin->data, rel_offset+size, my_size-size);
205 DEBUG(4, ("Found free block of size %d (needing %d) in middle of HBIN\n",
206 my_size, size));
207 break;
211 if (rel_offset != -1)
212 break;
215 /* No space available in previous hbins,
216 * allocate new one */
217 if (data->hbins[i] == NULL) {
218 DEBUG(4, ("No space available in other HBINs for block of size %d, allocating new HBIN\n",
219 size));
221 /* Add extra hbin block */
222 data->hbins = talloc_realloc(data, data->hbins,
223 struct hbin_block *, i+2);
224 hbin = talloc(data->hbins, struct hbin_block);
225 SMB_ASSERT(hbin != NULL);
227 data->hbins[i] = hbin;
228 data->hbins[i+1] = NULL;
230 /* Set hbin data */
231 hbin->HBIN_ID = talloc_strdup(hbin, "hbin");
232 hbin->offset_from_first = (i == 0?0:data->hbins[i-1]->offset_from_first+data->hbins[i-1]->offset_to_next);
233 hbin->offset_to_next = 0x1000;
234 hbin->unknown[0] = 0;
235 hbin->unknown[1] = 0;
236 unix_to_nt_time(&hbin->last_change, time(NULL));
237 hbin->block_size = hbin->offset_to_next;
238 hbin->data = talloc_zero_array(hbin, uint8_t, hbin->block_size - 0x20);
239 /* Update the regf header */
240 data->header->last_block += hbin->offset_to_next;
242 /* Set the next block to it's proper size and set the
243 * rel_offset for this block */
244 SIVAL(hbin->data, size, hbin->block_size - size - 0x20);
245 rel_offset = 0x0;
248 /* Set size and mark as used */
249 SIVAL(hbin->data, rel_offset, -size);
251 ret.data = hbin->data + rel_offset + 0x4; /* Skip past length */
252 ret.length = size - 0x4;
253 if (offset) {
254 uint32_t new_rel_offset;
255 *offset = hbin->offset_from_first + rel_offset + 0x20;
256 SMB_ASSERT(hbin_by_offset(data, *offset, &new_rel_offset) == hbin);
257 SMB_ASSERT(new_rel_offset == rel_offset);
260 return ret;
263 /* Store a data blob. Return the offset at which it was stored */
264 static uint32_t hbin_store (struct regf_data *data, DATA_BLOB blob)
266 uint32_t ret;
267 DATA_BLOB dest = hbin_alloc(data, blob.length, &ret);
269 memcpy(dest.data, blob.data, blob.length);
271 /* Make sure that we have no tailing garbage in the block */
272 if (dest.length > blob.length) {
273 memset(dest.data + blob.length, 0, dest.length - blob.length);
276 return ret;
279 static uint32_t hbin_store_tdr(struct regf_data *data,
280 tdr_push_fn_t push_fn, void *p)
282 struct tdr_push *push = tdr_push_init(data);
283 uint32_t ret;
285 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
286 DEBUG(0, ("Error during push\n"));
287 return -1;
290 ret = hbin_store(data, push->data);
292 talloc_free(push);
294 return ret;
298 /* Free existing data */
299 static void hbin_free (struct regf_data *data, uint32_t offset)
301 int32_t size;
302 uint32_t rel_offset;
303 int32_t next_size;
304 struct hbin_block *hbin;
306 SMB_ASSERT (offset > 0);
308 hbin = hbin_by_offset(data, offset, &rel_offset);
310 if (hbin == NULL)
311 return;
313 /* Get original size */
314 size = IVALS(hbin->data, rel_offset);
316 if (size > 0) {
317 DEBUG(1, ("Trying to free already freed block at 0x%04x\n",
318 offset));
319 return;
321 /* Mark as unused */
322 size = -size;
324 /* If the next block is free, merge into big free block */
325 if (rel_offset + size < hbin->offset_to_next - 0x20) {
326 next_size = IVALS(hbin->data, rel_offset+size);
327 if (next_size > 0) {
328 size += next_size;
332 /* Write block size */
333 SIVALS(hbin->data, rel_offset, size);
337 * Store a data blob data was already stored, but has changed in size
338 * Will try to save it at the current location if possible, otherwise
339 * does a free + store */
340 static uint32_t hbin_store_resize(struct regf_data *data,
341 uint32_t orig_offset, DATA_BLOB blob)
343 uint32_t rel_offset;
344 struct hbin_block *hbin = hbin_by_offset(data, orig_offset,
345 &rel_offset);
346 int32_t my_size;
347 int32_t orig_size;
348 int32_t needed_size;
349 int32_t possible_size;
350 unsigned int i;
352 SMB_ASSERT(orig_offset > 0);
354 if (!hbin)
355 return hbin_store(data, blob);
357 /* Get original size */
358 orig_size = -IVALS(hbin->data, rel_offset);
360 needed_size = blob.length + 4; /* Add int32 containing length */
361 needed_size = (needed_size + 7) & ~7; /* Align */
363 /* Fits into current allocated block */
364 if (orig_size >= needed_size) {
365 memcpy(hbin->data + rel_offset + 0x4, blob.data, blob.length);
366 /* If the difference in size is greater than 0x4, split the block
367 * and free/merge it */
368 if (orig_size - needed_size > 0x4) {
369 SIVALS(hbin->data, rel_offset, -needed_size);
370 SIVALS(hbin->data, rel_offset + needed_size,
371 needed_size-orig_size);
372 hbin_free(data, orig_offset + needed_size);
374 return orig_offset;
377 possible_size = orig_size;
379 /* Check if it can be combined with the next few free records */
380 for (i = rel_offset; i < hbin->offset_to_next - 0x20; i += my_size) {
381 if (IVALS(hbin->data, i) < 0) /* Used */
382 break;
384 my_size = IVALS(hbin->data, i);
386 if (my_size == 0x0) {
387 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
388 break;
389 } else {
390 possible_size += my_size;
393 if (possible_size >= blob.length) {
394 SIVAL(hbin->data, rel_offset, -possible_size);
395 memcpy(hbin->data + rel_offset + 0x4,
396 blob.data, blob.length);
397 return orig_offset;
401 hbin_free(data, orig_offset);
402 return hbin_store(data, blob);
405 static uint32_t hbin_store_tdr_resize(struct regf_data *regf,
406 tdr_push_fn_t push_fn,
407 uint32_t orig_offset, void *p)
409 struct tdr_push *push = tdr_push_init(regf);
410 uint32_t ret;
412 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
413 DEBUG(0, ("Error during push\n"));
414 return -1;
417 ret = hbin_store_resize(regf, orig_offset, push->data);
419 talloc_free(push);
421 return ret;
424 static uint32_t regf_create_lh_hash(const char *name)
426 char *hash_name;
427 uint32_t ret = 0;
428 uint16_t i;
430 hash_name = strupper_talloc(NULL, name);
431 for (i = 0; *(hash_name + i) != 0; i++) {
432 ret *= 37;
433 ret += *(hash_name + i);
435 talloc_free(hash_name);
436 return ret;
439 static WERROR regf_get_info(TALLOC_CTX *mem_ctx,
440 const struct hive_key *key,
441 const char **classname,
442 uint32_t *num_subkeys,
443 uint32_t *num_values,
444 NTTIME *last_mod_time,
445 uint32_t *max_subkeynamelen,
446 uint32_t *max_valnamelen,
447 uint32_t *max_valbufsize)
449 const struct regf_key_data *private_data =
450 (const struct regf_key_data *)key;
452 if (num_subkeys != NULL)
453 *num_subkeys = private_data->nk->num_subkeys;
455 if (num_values != NULL)
456 *num_values = private_data->nk->num_values;
458 if (classname != NULL) {
459 if (private_data->nk->clsname_offset != -1) {
460 DATA_BLOB data = hbin_get(private_data->hive,
461 private_data->nk->clsname_offset);
462 *classname = talloc_strndup(mem_ctx,
463 (char*)data.data,
464 private_data->nk->clsname_length);
465 W_ERROR_HAVE_NO_MEMORY(*classname);
466 } else
467 *classname = NULL;
470 /* TODO: Last mod time */
472 /* TODO: max valnamelen */
474 /* TODO: max valbufsize */
476 /* TODO: max subkeynamelen */
478 return WERR_OK;
481 static struct regf_key_data *regf_get_key(TALLOC_CTX *ctx,
482 struct regf_data *regf,
483 uint32_t offset)
485 struct nk_block *nk;
486 struct regf_key_data *ret;
488 ret = talloc_zero(ctx, struct regf_key_data);
489 ret->key.ops = &reg_backend_regf;
490 ret->hive = talloc_reference(ret, regf);
491 ret->offset = offset;
492 nk = talloc(ret, struct nk_block);
493 if (nk == NULL)
494 return NULL;
496 ret->nk = nk;
498 if (!hbin_get_tdr(regf, offset, nk,
499 (tdr_pull_fn_t)tdr_pull_nk_block, nk)) {
500 DEBUG(0, ("Unable to find HBIN data for offset 0x%x\n", offset));
501 return NULL;
504 if (strcmp(nk->header, "nk") != 0) {
505 DEBUG(0, ("Expected nk record, got %s\n", nk->header));
506 talloc_free(ret);
507 return NULL;
510 return ret;
514 static WERROR regf_get_value(TALLOC_CTX *ctx, struct hive_key *key,
515 uint32_t idx, const char **name,
516 uint32_t *data_type, DATA_BLOB *data)
518 const struct regf_key_data *private_data =
519 (const struct regf_key_data *)key;
520 struct vk_block *vk;
521 struct regf_data *regf = private_data->hive;
522 uint32_t vk_offset;
523 DATA_BLOB tmp;
525 if (idx >= private_data->nk->num_values)
526 return WERR_NO_MORE_ITEMS;
528 tmp = hbin_get(regf, private_data->nk->values_offset);
529 if (!tmp.data) {
530 DEBUG(0, ("Unable to find value list at 0x%x\n",
531 private_data->nk->values_offset));
532 return WERR_GENERAL_FAILURE;
535 if (tmp.length < private_data->nk->num_values * 4) {
536 DEBUG(1, ("Value counts mismatch\n"));
539 vk_offset = IVAL(tmp.data, idx * 4);
541 vk = talloc(NULL, struct vk_block);
542 W_ERROR_HAVE_NO_MEMORY(vk);
544 if (!hbin_get_tdr(regf, vk_offset, vk,
545 (tdr_pull_fn_t)tdr_pull_vk_block, vk)) {
546 DEBUG(0, ("Unable to get VK block at 0x%x\n", vk_offset));
547 talloc_free(vk);
548 return WERR_GENERAL_FAILURE;
551 /* FIXME: name character set ?*/
552 if (name != NULL) {
553 *name = talloc_strndup(ctx, vk->data_name, vk->name_length);
554 W_ERROR_HAVE_NO_MEMORY(*name);
557 if (data_type != NULL)
558 *data_type = vk->data_type;
560 if (vk->data_length & 0x80000000) {
561 /* this is data of type "REG_DWORD" or "REG_DWORD_BIG_ENDIAN" */
562 data->data = talloc_size(ctx, sizeof(uint32_t));
563 W_ERROR_HAVE_NO_MEMORY(data->data);
564 SIVAL(data->data, 0, vk->data_offset);
565 data->length = sizeof(uint32_t);
566 } else {
567 *data = hbin_get(regf, vk->data_offset);
570 if (data->length < vk->data_length) {
571 DEBUG(1, ("Read data less than indicated data length!\n"));
574 talloc_free(vk);
576 return WERR_OK;
579 static WERROR regf_get_value_by_name(TALLOC_CTX *mem_ctx,
580 struct hive_key *key, const char *name,
581 uint32_t *type, DATA_BLOB *data)
583 unsigned int i;
584 const char *vname;
585 WERROR error;
587 /* FIXME: Do binary search? Is this list sorted at all? */
589 for (i = 0; W_ERROR_IS_OK(error = regf_get_value(mem_ctx, key, i,
590 &vname, type, data));
591 i++) {
592 if (!strcmp(vname, name))
593 return WERR_OK;
596 if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
597 return WERR_BADFILE;
599 return error;
603 static WERROR regf_get_subkey_by_index(TALLOC_CTX *ctx,
604 const struct hive_key *key,
605 uint32_t idx, const char **name,
606 const char **classname,
607 NTTIME *last_mod_time)
609 DATA_BLOB data;
610 struct regf_key_data *ret;
611 const struct regf_key_data *private_data = (const struct regf_key_data *)key;
612 struct nk_block *nk = private_data->nk;
613 uint32_t key_off=0;
615 if (idx >= nk->num_subkeys)
616 return WERR_NO_MORE_ITEMS;
618 /* Make sure that we don't crash if the key is empty */
619 if (nk->subkeys_offset == -1) {
620 return WERR_NO_MORE_ITEMS;
623 data = hbin_get(private_data->hive, nk->subkeys_offset);
624 if (!data.data) {
625 DEBUG(0, ("Unable to find subkey list at 0x%x\n",
626 nk->subkeys_offset));
627 return WERR_GENERAL_FAILURE;
630 if (!strncmp((char *)data.data, "li", 2)) {
631 struct li_block li;
632 struct tdr_pull *pull = tdr_pull_init(private_data->hive);
634 DEBUG(10, ("Subkeys in LI list\n"));
635 pull->data = data;
637 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, nk, &li))) {
638 DEBUG(0, ("Error parsing LI list\n"));
639 talloc_free(pull);
640 return WERR_GENERAL_FAILURE;
642 talloc_free(pull);
643 SMB_ASSERT(!strncmp(li.header, "li", 2));
645 if (li.key_count != nk->num_subkeys) {
646 DEBUG(0, ("Subkey counts don't match\n"));
647 return WERR_GENERAL_FAILURE;
649 key_off = li.nk_offset[idx];
651 } else if (!strncmp((char *)data.data, "lf", 2)) {
652 struct lf_block lf;
653 struct tdr_pull *pull = tdr_pull_init(private_data->hive);
655 DEBUG(10, ("Subkeys in LF list\n"));
656 pull->data = data;
658 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, nk, &lf))) {
659 DEBUG(0, ("Error parsing LF list\n"));
660 talloc_free(pull);
661 return WERR_GENERAL_FAILURE;
663 talloc_free(pull);
664 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
666 if (lf.key_count != nk->num_subkeys) {
667 DEBUG(0, ("Subkey counts don't match\n"));
668 return WERR_GENERAL_FAILURE;
671 key_off = lf.hr[idx].nk_offset;
672 } else if (!strncmp((char *)data.data, "lh", 2)) {
673 struct lh_block lh;
674 struct tdr_pull *pull = tdr_pull_init(private_data->hive);
676 DEBUG(10, ("Subkeys in LH list\n"));
677 pull->data = data;
679 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, nk, &lh))) {
680 DEBUG(0, ("Error parsing LH list\n"));
681 talloc_free(pull);
682 return WERR_GENERAL_FAILURE;
684 talloc_free(pull);
685 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
687 if (lh.key_count != nk->num_subkeys) {
688 DEBUG(0, ("Subkey counts don't match\n"));
689 return WERR_GENERAL_FAILURE;
691 key_off = lh.hr[idx].nk_offset;
692 } else if (!strncmp((char *)data.data, "ri", 2)) {
693 struct ri_block ri;
694 struct tdr_pull *pull = tdr_pull_init(ctx);
695 uint16_t i;
696 uint16_t sublist_count = 0;
698 DEBUG(10, ("Subkeys in RI list\n"));
699 pull->data = data;
701 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull, nk, &ri))) {
702 DEBUG(0, ("Error parsing RI list\n"));
703 talloc_free(pull);
704 return WERR_GENERAL_FAILURE;
706 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
708 for (i = 0; i < ri.key_count; i++) {
709 DATA_BLOB list_data;
711 /* Get sublist data blob */
712 list_data = hbin_get(private_data->hive, ri.offset[i]);
713 if (!list_data.data) {
714 DEBUG(0, ("Error getting RI list."));
715 talloc_free(pull);
716 return WERR_GENERAL_FAILURE;
719 pull->data = list_data;
721 if (!strncmp((char *)list_data.data, "li", 2)) {
722 struct li_block li;
724 DEBUG(10, ("Subkeys in RI->LI list\n"));
726 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull,
728 &li))) {
729 DEBUG(0, ("Error parsing LI list from RI\n"));
730 talloc_free(pull);
731 return WERR_GENERAL_FAILURE;
733 SMB_ASSERT(!strncmp(li.header, "li", 2));
735 /* Advance to next sublist if necessary */
736 if (idx >= sublist_count + li.key_count) {
737 sublist_count += li.key_count;
738 continue;
740 key_off = li.nk_offset[idx - sublist_count];
741 sublist_count += li.key_count;
742 break;
743 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
744 struct lh_block lh;
746 DEBUG(10, ("Subkeys in RI->LH list\n"));
748 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull,
750 &lh))) {
751 DEBUG(0, ("Error parsing LH list from RI\n"));
752 talloc_free(pull);
753 return WERR_GENERAL_FAILURE;
755 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
757 /* Advance to next sublist if necessary */
758 if (idx >= sublist_count + lh.key_count) {
759 sublist_count += lh.key_count;
760 continue;
762 key_off = lh.hr[idx - sublist_count].nk_offset;
763 sublist_count += lh.key_count;
764 break;
765 } else {
766 DEBUG(0,("Unknown sublist in ri block\n"));
767 talloc_free(pull);
769 return WERR_GENERAL_FAILURE;
773 talloc_free(pull);
776 if (idx > sublist_count) {
777 return WERR_NO_MORE_ITEMS;
780 } else {
781 DEBUG(0, ("Unknown type for subkey list (0x%04x): %c%c\n",
782 nk->subkeys_offset, data.data[0], data.data[1]));
783 return WERR_GENERAL_FAILURE;
786 ret = regf_get_key (ctx, private_data->hive, key_off);
788 if (classname != NULL) {
789 if (ret->nk->clsname_offset != -1) {
790 DATA_BLOB db = hbin_get(ret->hive,
791 ret->nk->clsname_offset);
792 *classname = talloc_strndup(ctx,
793 (char*)db.data,
794 ret->nk->clsname_length);
795 W_ERROR_HAVE_NO_MEMORY(*classname);
796 } else
797 *classname = NULL;
800 if (last_mod_time != NULL)
801 *last_mod_time = ret->nk->last_change;
803 if (name != NULL)
804 *name = talloc_steal(ctx, ret->nk->key_name);
806 talloc_free(ret);
808 return WERR_OK;
811 static WERROR regf_match_subkey_by_name(TALLOC_CTX *ctx,
812 const struct hive_key *key,
813 uint32_t offset,
814 const char *name, uint32_t *ret)
816 DATA_BLOB subkey_data;
817 struct nk_block subkey;
818 struct tdr_pull *pull;
819 const struct regf_key_data *private_data =
820 (const struct regf_key_data *)key;
822 subkey_data = hbin_get(private_data->hive, offset);
823 if (!subkey_data.data) {
824 DEBUG(0, ("Unable to retrieve subkey HBIN\n"));
825 return WERR_GENERAL_FAILURE;
828 pull = tdr_pull_init(ctx);
830 pull->data = subkey_data;
832 if (NT_STATUS_IS_ERR(tdr_pull_nk_block(pull, ctx, &subkey))) {
833 DEBUG(0, ("Error parsing NK structure.\n"));
834 talloc_free(pull);
835 return WERR_GENERAL_FAILURE;
837 talloc_free(pull);
839 if (strncmp(subkey.header, "nk", 2)) {
840 DEBUG(0, ("Not an NK structure.\n"));
841 return WERR_GENERAL_FAILURE;
844 if (!strcasecmp(subkey.key_name, name)) {
845 *ret = offset;
846 } else {
847 *ret = 0;
849 return WERR_OK;
852 static WERROR regf_get_subkey_by_name(TALLOC_CTX *ctx,
853 const struct hive_key *key,
854 const char *name,
855 struct hive_key **ret)
857 DATA_BLOB data;
858 const struct regf_key_data *private_data =
859 (const struct regf_key_data *)key;
860 struct nk_block *nk = private_data->nk;
861 uint32_t key_off = 0;
863 /* Make sure that we don't crash if the key is empty */
864 if (nk->subkeys_offset == -1) {
865 return WERR_BADFILE;
868 data = hbin_get(private_data->hive, nk->subkeys_offset);
869 if (!data.data) {
870 DEBUG(0, ("Unable to find subkey list\n"));
871 return WERR_GENERAL_FAILURE;
874 if (!strncmp((char *)data.data, "li", 2)) {
875 struct li_block li;
876 struct tdr_pull *pull = tdr_pull_init(ctx);
877 uint16_t i;
879 DEBUG(10, ("Subkeys in LI list\n"));
880 pull->data = data;
882 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, nk, &li))) {
883 DEBUG(0, ("Error parsing LI list\n"));
884 talloc_free(pull);
885 return WERR_GENERAL_FAILURE;
887 talloc_free(pull);
888 SMB_ASSERT(!strncmp(li.header, "li", 2));
890 if (li.key_count != nk->num_subkeys) {
891 DEBUG(0, ("Subkey counts don't match\n"));
892 return WERR_GENERAL_FAILURE;
895 for (i = 0; i < li.key_count; i++) {
896 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
897 li.nk_offset[i],
898 name,
899 &key_off));
900 if (key_off != 0)
901 break;
903 if (key_off == 0)
904 return WERR_BADFILE;
905 } else if (!strncmp((char *)data.data, "lf", 2)) {
906 struct lf_block lf;
907 struct tdr_pull *pull = tdr_pull_init(ctx);
908 uint16_t i;
910 DEBUG(10, ("Subkeys in LF list\n"));
911 pull->data = data;
913 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, nk, &lf))) {
914 DEBUG(0, ("Error parsing LF list\n"));
915 talloc_free(pull);
916 return WERR_GENERAL_FAILURE;
918 talloc_free(pull);
919 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
921 if (lf.key_count != nk->num_subkeys) {
922 DEBUG(0, ("Subkey counts don't match\n"));
923 return WERR_GENERAL_FAILURE;
926 for (i = 0; i < lf.key_count; i++) {
927 if (strncmp(lf.hr[i].hash, name, 4)) {
928 continue;
930 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
931 key,
932 lf.hr[i].nk_offset,
933 name,
934 &key_off));
935 if (key_off != 0)
936 break;
938 if (key_off == 0)
939 return WERR_BADFILE;
940 } else if (!strncmp((char *)data.data, "lh", 2)) {
941 struct lh_block lh;
942 struct tdr_pull *pull = tdr_pull_init(ctx);
943 uint16_t i;
944 uint32_t hash;
946 DEBUG(10, ("Subkeys in LH list\n"));
947 pull->data = data;
949 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, nk, &lh))) {
950 DEBUG(0, ("Error parsing LH list\n"));
951 talloc_free(pull);
952 return WERR_GENERAL_FAILURE;
954 talloc_free(pull);
955 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
957 if (lh.key_count != nk->num_subkeys) {
958 DEBUG(0, ("Subkey counts don't match\n"));
959 return WERR_GENERAL_FAILURE;
962 hash = regf_create_lh_hash(name);
963 for (i = 0; i < lh.key_count; i++) {
964 if (lh.hr[i].base37 != hash) {
965 continue;
967 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
968 key,
969 lh.hr[i].nk_offset,
970 name,
971 &key_off));
972 if (key_off != 0)
973 break;
975 if (key_off == 0)
976 return WERR_BADFILE;
977 } else if (!strncmp((char *)data.data, "ri", 2)) {
978 struct ri_block ri;
979 struct tdr_pull *pull = tdr_pull_init(ctx);
980 uint16_t i, j;
982 DEBUG(10, ("Subkeys in RI list\n"));
983 pull->data = data;
985 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull, nk, &ri))) {
986 DEBUG(0, ("Error parsing RI list\n"));
987 talloc_free(pull);
988 return WERR_GENERAL_FAILURE;
990 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
992 for (i = 0; i < ri.key_count; i++) {
993 DATA_BLOB list_data;
995 /* Get sublist data blob */
996 list_data = hbin_get(private_data->hive, ri.offset[i]);
997 if (list_data.data == NULL) {
998 DEBUG(0, ("Error getting RI list."));
999 talloc_free(pull);
1000 return WERR_GENERAL_FAILURE;
1003 pull->data = list_data;
1005 if (!strncmp((char *)list_data.data, "li", 2)) {
1006 struct li_block li;
1008 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull,
1010 &li))) {
1011 DEBUG(0, ("Error parsing LI list from RI\n"));
1012 talloc_free(pull);
1013 return WERR_GENERAL_FAILURE;
1015 SMB_ASSERT(!strncmp(li.header, "li", 2));
1017 for (j = 0; j < li.key_count; j++) {
1018 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
1019 li.nk_offset[j],
1020 name,
1021 &key_off));
1022 if (key_off)
1023 break;
1025 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
1026 struct lh_block lh;
1027 uint32_t hash;
1029 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull,
1031 &lh))) {
1032 DEBUG(0, ("Error parsing LH list from RI\n"));
1033 talloc_free(pull);
1034 return WERR_GENERAL_FAILURE;
1036 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1038 hash = regf_create_lh_hash(name);
1039 for (j = 0; j < lh.key_count; j++) {
1040 if (lh.hr[j].base37 != hash) {
1041 continue;
1043 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
1044 lh.hr[j].nk_offset,
1045 name,
1046 &key_off));
1047 if (key_off)
1048 break;
1051 if (key_off)
1052 break;
1054 talloc_free(pull);
1055 if (!key_off)
1056 return WERR_BADFILE;
1057 } else {
1058 DEBUG(0, ("Unknown subkey list type.\n"));
1059 return WERR_GENERAL_FAILURE;
1062 *ret = (struct hive_key *)regf_get_key(ctx, private_data->hive,
1063 key_off);
1064 return WERR_OK;
1067 static WERROR regf_set_sec_desc(struct hive_key *key,
1068 const struct security_descriptor *sec_desc)
1070 const struct regf_key_data *private_data =
1071 (const struct regf_key_data *)key;
1072 struct sk_block cur_sk, sk, new_sk;
1073 struct regf_data *regf = private_data->hive;
1074 struct nk_block root;
1075 DATA_BLOB data;
1076 uint32_t sk_offset, cur_sk_offset;
1077 bool update_cur_sk = false;
1079 /* Get the root nk */
1080 hbin_get_tdr(regf, regf->header->data_offset, regf,
1081 (tdr_pull_fn_t) tdr_pull_nk_block, &root);
1083 /* Push the security descriptor to a blob */
1084 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf,
1085 sec_desc, (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
1086 DEBUG(0, ("Unable to push security descriptor\n"));
1087 return WERR_GENERAL_FAILURE;
1090 /* Get the current security descriptor for the key */
1091 if (!hbin_get_tdr(regf, private_data->nk->sk_offset, regf,
1092 (tdr_pull_fn_t) tdr_pull_sk_block, &cur_sk)) {
1093 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1094 return WERR_BADFILE;
1096 /* If there's no change, change nothing. */
1097 if (memcmp(data.data, cur_sk.sec_desc,
1098 MIN(data.length, cur_sk.rec_size)) == 0) {
1099 return WERR_OK;
1102 /* Delete the current sk if only this key is using it */
1103 if (cur_sk.ref_cnt == 1) {
1104 /* Get the previous security descriptor for the key */
1105 if (!hbin_get_tdr(regf, cur_sk.prev_offset, regf,
1106 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1107 DEBUG(0, ("Unable to find prev security descriptor for current key\n"));
1108 return WERR_BADFILE;
1110 /* Change and store the previous security descriptor */
1111 sk.next_offset = cur_sk.next_offset;
1112 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
1113 cur_sk.prev_offset, &sk);
1115 /* Get the next security descriptor for the key */
1116 if (!hbin_get_tdr(regf, cur_sk.next_offset, regf,
1117 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1118 DEBUG(0, ("Unable to find next security descriptor for current key\n"));
1119 return WERR_BADFILE;
1121 /* Change and store the next security descriptor */
1122 sk.prev_offset = cur_sk.prev_offset;
1123 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
1124 cur_sk.next_offset, &sk);
1126 hbin_free(regf, private_data->nk->sk_offset);
1127 } else {
1128 /* This key will no longer be referring to this sk */
1129 cur_sk.ref_cnt--;
1130 update_cur_sk = true;
1133 sk_offset = root.sk_offset;
1135 do {
1136 cur_sk_offset = sk_offset;
1137 if (!hbin_get_tdr(regf, sk_offset, regf,
1138 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1139 DEBUG(0, ("Unable to find security descriptor\n"));
1140 return WERR_BADFILE;
1142 if (memcmp(data.data, sk.sec_desc, MIN(data.length, sk.rec_size)) == 0) {
1143 private_data->nk->sk_offset = sk_offset;
1144 sk.ref_cnt++;
1145 hbin_store_tdr_resize(regf,
1146 (tdr_push_fn_t) tdr_push_sk_block,
1147 sk_offset, &sk);
1148 hbin_store_tdr_resize(regf,
1149 (tdr_push_fn_t) tdr_push_nk_block,
1150 private_data->offset,
1151 private_data->nk);
1152 return WERR_OK;
1154 sk_offset = sk.next_offset;
1155 } while (sk_offset != root.sk_offset);
1157 ZERO_STRUCT(new_sk);
1158 new_sk.header = "sk";
1159 new_sk.prev_offset = cur_sk_offset;
1160 new_sk.next_offset = root.sk_offset;
1161 new_sk.ref_cnt = 1;
1162 new_sk.rec_size = data.length;
1163 new_sk.sec_desc = data.data;
1165 sk_offset = hbin_store_tdr(regf,
1166 (tdr_push_fn_t) tdr_push_sk_block,
1167 &new_sk);
1168 if (sk_offset == -1) {
1169 DEBUG(0, ("Error storing sk block\n"));
1170 return WERR_GENERAL_FAILURE;
1172 private_data->nk->sk_offset = sk_offset;
1174 if (update_cur_sk) {
1175 hbin_store_tdr_resize(regf,
1176 (tdr_push_fn_t) tdr_push_sk_block,
1177 private_data->nk->sk_offset, &cur_sk);
1180 /* Get the previous security descriptor for the key */
1181 if (!hbin_get_tdr(regf, new_sk.prev_offset, regf,
1182 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1183 DEBUG(0, ("Unable to find security descriptor for previous key\n"));
1184 return WERR_BADFILE;
1186 /* Change and store the previous security descriptor */
1187 sk.next_offset = sk_offset;
1188 hbin_store_tdr_resize(regf,
1189 (tdr_push_fn_t) tdr_push_sk_block,
1190 cur_sk.prev_offset, &sk);
1192 /* Get the next security descriptor for the key (always root, as we append) */
1193 if (!hbin_get_tdr(regf, new_sk.next_offset, regf,
1194 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1195 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1196 return WERR_BADFILE;
1198 /* Change and store the next security descriptor (always root, as we append) */
1199 sk.prev_offset = sk_offset;
1200 hbin_store_tdr_resize(regf,
1201 (tdr_push_fn_t) tdr_push_sk_block,
1202 root.sk_offset, &sk);
1205 /* Store the nk. */
1206 hbin_store_tdr_resize(regf,
1207 (tdr_push_fn_t) tdr_push_sk_block,
1208 private_data->offset, private_data->nk);
1209 return WERR_OK;
1212 static WERROR regf_get_sec_desc(TALLOC_CTX *ctx, const struct hive_key *key,
1213 struct security_descriptor **sd)
1215 const struct regf_key_data *private_data =
1216 (const struct regf_key_data *)key;
1217 struct sk_block sk;
1218 struct regf_data *regf = private_data->hive;
1219 DATA_BLOB data;
1221 if (!hbin_get_tdr(regf, private_data->nk->sk_offset, ctx,
1222 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1223 DEBUG(0, ("Unable to find security descriptor\n"));
1224 return WERR_GENERAL_FAILURE;
1227 if (strcmp(sk.header, "sk") != 0) {
1228 DEBUG(0, ("Expected 'sk', got '%s'\n", sk.header));
1229 return WERR_GENERAL_FAILURE;
1232 *sd = talloc(ctx, struct security_descriptor);
1233 W_ERROR_HAVE_NO_MEMORY(*sd);
1235 data.data = sk.sec_desc;
1236 data.length = sk.rec_size;
1237 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_struct_blob(&data, ctx, *sd,
1238 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor))) {
1239 DEBUG(0, ("Error parsing security descriptor\n"));
1240 return WERR_GENERAL_FAILURE;
1243 return WERR_OK;
1246 static WERROR regf_sl_add_entry(struct regf_data *regf, uint32_t list_offset,
1247 const char *name,
1248 uint32_t key_offset, uint32_t *ret)
1250 DATA_BLOB data;
1252 /* Create a new key if necessary */
1253 if (list_offset == -1) {
1254 if (regf->header->version.major != 1) {
1255 DEBUG(0, ("Can't store keys in unknown registry format\n"));
1256 return WERR_NOT_SUPPORTED;
1258 if (regf->header->version.minor < 3) {
1259 /* Store LI */
1260 struct li_block li;
1261 ZERO_STRUCT(li);
1262 li.header = "li";
1263 li.key_count = 1;
1265 li.nk_offset = talloc_array(regf, uint32_t, 1);
1266 W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1267 li.nk_offset[0] = key_offset;
1269 *ret = hbin_store_tdr(regf,
1270 (tdr_push_fn_t) tdr_push_li_block,
1271 &li);
1273 talloc_free(li.nk_offset);
1274 } else if (regf->header->version.minor == 3 ||
1275 regf->header->version.minor == 4) {
1276 /* Store LF */
1277 struct lf_block lf;
1278 ZERO_STRUCT(lf);
1279 lf.header = "lf";
1280 lf.key_count = 1;
1282 lf.hr = talloc_array(regf, struct hash_record, 1);
1283 W_ERROR_HAVE_NO_MEMORY(lf.hr);
1284 lf.hr[0].nk_offset = key_offset;
1285 lf.hr[0].hash = talloc_strndup(lf.hr, name, 4);
1286 W_ERROR_HAVE_NO_MEMORY(lf.hr[0].hash);
1288 *ret = hbin_store_tdr(regf,
1289 (tdr_push_fn_t) tdr_push_lf_block,
1290 &lf);
1292 talloc_free(lf.hr);
1293 } else if (regf->header->version.minor == 5) {
1294 /* Store LH */
1295 struct lh_block lh;
1296 ZERO_STRUCT(lh);
1297 lh.header = "lh";
1298 lh.key_count = 1;
1300 lh.hr = talloc_array(regf, struct lh_hash, 1);
1301 W_ERROR_HAVE_NO_MEMORY(lh.hr);
1302 lh.hr[0].nk_offset = key_offset;
1303 lh.hr[0].base37 = regf_create_lh_hash(name);
1305 *ret = hbin_store_tdr(regf,
1306 (tdr_push_fn_t) tdr_push_lh_block,
1307 &lh);
1309 talloc_free(lh.hr);
1311 return WERR_OK;
1314 data = hbin_get(regf, list_offset);
1315 if (!data.data) {
1316 DEBUG(0, ("Unable to find subkey list\n"));
1317 return WERR_BADFILE;
1320 if (!strncmp((char *)data.data, "li", 2)) {
1321 struct tdr_pull *pull = tdr_pull_init(regf);
1322 struct li_block li;
1323 struct nk_block sub_nk;
1324 int32_t i, j;
1326 pull->data = data;
1328 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, regf, &li))) {
1329 DEBUG(0, ("Error parsing LI list\n"));
1330 talloc_free(pull);
1331 return WERR_BADFILE;
1333 talloc_free(pull);
1335 if (strncmp(li.header, "li", 2) != 0) {
1336 abort();
1337 DEBUG(0, ("LI header corrupt\n"));
1338 return WERR_BADFILE;
1342 * Find the position to store the pointer
1343 * Extensive testing reveils that at least on windows 7 subkeys
1344 * *MUST* be stored in alphabetical order
1346 for (i = 0; i < li.key_count; i++) {
1347 /* Get the nk */
1348 hbin_get_tdr(regf, li.nk_offset[i], regf,
1349 (tdr_pull_fn_t) tdr_pull_nk_block, &sub_nk);
1350 if (strcasecmp(name, sub_nk.key_name) < 0) {
1351 break;
1355 li.nk_offset = talloc_realloc(regf, li.nk_offset,
1356 uint32_t, li.key_count+1);
1357 W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1359 /* Move everything behind this offset */
1360 for (j = li.key_count - 1; j >= i; j--) {
1361 li.nk_offset[j+1] = li.nk_offset[j];
1364 li.nk_offset[i] = key_offset;
1365 li.key_count++;
1366 *ret = hbin_store_tdr_resize(regf,
1367 (tdr_push_fn_t)tdr_push_li_block,
1368 list_offset, &li);
1370 talloc_free(li.nk_offset);
1371 } else if (!strncmp((char *)data.data, "lf", 2)) {
1372 struct tdr_pull *pull = tdr_pull_init(regf);
1373 struct lf_block lf;
1374 struct nk_block sub_nk;
1375 int32_t i, j;
1377 pull->data = data;
1379 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, regf, &lf))) {
1380 DEBUG(0, ("Error parsing LF list\n"));
1381 talloc_free(pull);
1382 return WERR_BADFILE;
1384 talloc_free(pull);
1385 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1388 * Find the position to store the hash record
1389 * Extensive testing reveils that at least on windows 7 subkeys
1390 * *MUST* be stored in alphabetical order
1392 for (i = 0; i < lf.key_count; i++) {
1393 /* Get the nk */
1394 hbin_get_tdr(regf, lf.hr[i].nk_offset, regf,
1395 (tdr_pull_fn_t) tdr_pull_nk_block, &sub_nk);
1396 if (strcasecmp(name, sub_nk.key_name) < 0) {
1397 break;
1401 lf.hr = talloc_realloc(regf, lf.hr, struct hash_record,
1402 lf.key_count+1);
1403 W_ERROR_HAVE_NO_MEMORY(lf.hr);
1405 /* Move everything behind this hash record */
1406 for (j = lf.key_count - 1; j >= i; j--) {
1407 lf.hr[j+1] = lf.hr[j];
1410 lf.hr[i].nk_offset = key_offset;
1411 lf.hr[i].hash = talloc_strndup(lf.hr, name, 4);
1412 W_ERROR_HAVE_NO_MEMORY(lf.hr[lf.key_count].hash);
1413 lf.key_count++;
1414 *ret = hbin_store_tdr_resize(regf,
1415 (tdr_push_fn_t)tdr_push_lf_block,
1416 list_offset, &lf);
1418 talloc_free(lf.hr);
1419 } else if (!strncmp((char *)data.data, "lh", 2)) {
1420 struct tdr_pull *pull = tdr_pull_init(regf);
1421 struct lh_block lh;
1422 struct nk_block sub_nk;
1423 int32_t i, j;
1425 pull->data = data;
1427 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, regf, &lh))) {
1428 DEBUG(0, ("Error parsing LH list\n"));
1429 talloc_free(pull);
1430 return WERR_BADFILE;
1432 talloc_free(pull);
1433 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1436 * Find the position to store the hash record
1437 * Extensive testing reveils that at least on windows 7 subkeys
1438 * *MUST* be stored in alphabetical order
1440 for (i = 0; i < lh.key_count; i++) {
1441 /* Get the nk */
1442 hbin_get_tdr(regf, lh.hr[i].nk_offset, regf,
1443 (tdr_pull_fn_t) tdr_pull_nk_block, &sub_nk);
1444 if (strcasecmp(name, sub_nk.key_name) < 0) {
1445 break;
1449 lh.hr = talloc_realloc(regf, lh.hr, struct lh_hash,
1450 lh.key_count+1);
1451 W_ERROR_HAVE_NO_MEMORY(lh.hr);
1453 /* Move everything behind this hash record */
1454 for (j = lh.key_count - 1; j >= i; j--) {
1455 lh.hr[j+1] = lh.hr[j];
1458 lh.hr[i].nk_offset = key_offset;
1459 lh.hr[i].base37 = regf_create_lh_hash(name);
1460 lh.key_count++;
1461 *ret = hbin_store_tdr_resize(regf,
1462 (tdr_push_fn_t)tdr_push_lh_block,
1463 list_offset, &lh);
1465 talloc_free(lh.hr);
1466 } else if (!strncmp((char *)data.data, "ri", 2)) {
1467 /* FIXME */
1468 DEBUG(0, ("Adding to 'ri' subkey list is not supported yet.\n"));
1469 return WERR_NOT_SUPPORTED;
1470 } else {
1471 DEBUG(0, ("Cannot add to unknown subkey list\n"));
1472 return WERR_BADFILE;
1475 return WERR_OK;
1478 static WERROR regf_sl_del_entry(struct regf_data *regf, uint32_t list_offset,
1479 uint32_t key_offset, uint32_t *ret)
1481 DATA_BLOB data;
1483 data = hbin_get(regf, list_offset);
1484 if (!data.data) {
1485 DEBUG(0, ("Unable to find subkey list\n"));
1486 return WERR_BADFILE;
1489 if (strncmp((char *)data.data, "li", 2) == 0) {
1490 struct li_block li;
1491 struct tdr_pull *pull = tdr_pull_init(regf);
1492 uint16_t i;
1493 bool found_offset = false;
1495 DEBUG(10, ("Subkeys in LI list\n"));
1497 pull->data = data;
1499 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, regf, &li))) {
1500 DEBUG(0, ("Error parsing LI list\n"));
1501 talloc_free(pull);
1502 return WERR_BADFILE;
1504 talloc_free(pull);
1506 SMB_ASSERT(!strncmp(li.header, "li", 2));
1508 for (i = 0; i < li.key_count; i++) {
1509 if (found_offset) {
1510 li.nk_offset[i-1] = li.nk_offset[i];
1512 if (li.nk_offset[i] == key_offset) {
1513 found_offset = true;
1514 continue;
1517 if (!found_offset) {
1518 DEBUG(2, ("Subkey not found\n"));
1519 return WERR_BADFILE;
1521 li.key_count--;
1523 /* If the there are no entries left, free the subkey list */
1524 if (li.key_count == 0) {
1525 hbin_free(regf, list_offset);
1526 *ret = -1;
1529 /* Store li block */
1530 *ret = hbin_store_tdr_resize(regf,
1531 (tdr_push_fn_t) tdr_push_li_block,
1532 list_offset, &li);
1533 } else if (strncmp((char *)data.data, "lf", 2) == 0) {
1534 struct lf_block lf;
1535 struct tdr_pull *pull = tdr_pull_init(regf);
1536 uint16_t i;
1537 bool found_offset = false;
1539 DEBUG(10, ("Subkeys in LF list\n"));
1541 pull->data = data;
1543 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, regf, &lf))) {
1544 DEBUG(0, ("Error parsing LF list\n"));
1545 talloc_free(pull);
1546 return WERR_BADFILE;
1548 talloc_free(pull);
1550 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1552 for (i = 0; i < lf.key_count; i++) {
1553 if (found_offset) {
1554 lf.hr[i-1] = lf.hr[i];
1555 continue;
1557 if (lf.hr[i].nk_offset == key_offset) {
1558 found_offset = 1;
1559 continue;
1562 if (!found_offset) {
1563 DEBUG(2, ("Subkey not found\n"));
1564 return WERR_BADFILE;
1566 lf.key_count--;
1568 /* If the there are no entries left, free the subkey list */
1569 if (lf.key_count == 0) {
1570 hbin_free(regf, list_offset);
1571 *ret = -1;
1572 return WERR_OK;
1575 /* Store lf block */
1576 *ret = hbin_store_tdr_resize(regf,
1577 (tdr_push_fn_t) tdr_push_lf_block,
1578 list_offset, &lf);
1579 } else if (strncmp((char *)data.data, "lh", 2) == 0) {
1580 struct lh_block lh;
1581 struct tdr_pull *pull = tdr_pull_init(regf);
1582 uint16_t i;
1583 bool found_offset = false;
1585 DEBUG(10, ("Subkeys in LH list\n"));
1587 pull->data = data;
1589 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, regf, &lh))) {
1590 DEBUG(0, ("Error parsing LF list\n"));
1591 talloc_free(pull);
1592 return WERR_BADFILE;
1594 talloc_free(pull);
1596 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1598 for (i = 0; i < lh.key_count; i++) {
1599 if (found_offset) {
1600 lh.hr[i-1] = lh.hr[i];
1601 continue;
1603 if (lh.hr[i].nk_offset == key_offset) {
1604 found_offset = 1;
1605 continue;
1608 if (!found_offset) {
1609 DEBUG(0, ("Subkey not found\n"));
1610 return WERR_BADFILE;
1612 lh.key_count--;
1614 /* If the there are no entries left, free the subkey list */
1615 if (lh.key_count == 0) {
1616 hbin_free(regf, list_offset);
1617 *ret = -1;
1618 return WERR_OK;
1621 /* Store lh block */
1622 *ret = hbin_store_tdr_resize(regf,
1623 (tdr_push_fn_t) tdr_push_lh_block,
1624 list_offset, &lh);
1625 } else if (strncmp((char *)data.data, "ri", 2) == 0) {
1626 /* FIXME */
1627 DEBUG(0, ("Sorry, deletion from ri block is not supported yet.\n"));
1628 return WERR_NOT_SUPPORTED;
1629 } else {
1630 DEBUG (0, ("Unknown header found in subkey list.\n"));
1631 return WERR_BADFILE;
1633 return WERR_OK;
1636 static WERROR regf_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
1637 const char *name)
1639 struct regf_key_data *private_data = (struct regf_key_data *)key;
1640 struct regf_data *regf = private_data->hive;
1641 struct nk_block *nk = private_data->nk;
1642 struct vk_block vk;
1643 uint32_t vk_offset;
1644 bool found_offset = false;
1645 DATA_BLOB values;
1646 unsigned int i;
1648 if (nk->values_offset == -1) {
1649 return WERR_BADFILE;
1652 values = hbin_get(regf, nk->values_offset);
1654 for (i = 0; i < nk->num_values; i++) {
1655 if (found_offset) {
1656 ((uint32_t *)values.data)[i-1] = ((uint32_t *) values.data)[i];
1657 } else {
1658 vk_offset = IVAL(values.data, i * 4);
1659 if (!hbin_get_tdr(regf, vk_offset, private_data,
1660 (tdr_pull_fn_t)tdr_pull_vk_block,
1661 &vk)) {
1662 DEBUG(0, ("Unable to get VK block at %d\n",
1663 vk_offset));
1664 return WERR_BADFILE;
1666 if (strcmp(vk.data_name, name) == 0) {
1667 hbin_free(regf, vk_offset);
1668 found_offset = true;
1672 if (!found_offset) {
1673 return WERR_BADFILE;
1674 } else {
1675 nk->num_values--;
1676 values.length = (nk->num_values)*4;
1679 /* Store values list and nk */
1680 if (nk->num_values == 0) {
1681 hbin_free(regf, nk->values_offset);
1682 nk->values_offset = -1;
1683 } else {
1684 nk->values_offset = hbin_store_resize(regf,
1685 nk->values_offset,
1686 values);
1688 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
1689 private_data->offset, nk);
1691 return regf_save_hbin(private_data->hive, 0);
1695 static WERROR regf_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
1696 const char *name)
1698 const struct regf_key_data *private_data =
1699 (const struct regf_key_data *)parent;
1700 struct regf_key_data *key;
1701 struct nk_block *parent_nk;
1702 WERROR error;
1704 SMB_ASSERT(private_data);
1706 parent_nk = private_data->nk;
1708 if (parent_nk->subkeys_offset == -1) {
1709 DEBUG(4, ("Subkey list is empty, this key cannot contain subkeys.\n"));
1710 return WERR_BADFILE;
1713 /* Find the key */
1714 if (!W_ERROR_IS_OK(regf_get_subkey_by_name(parent_nk, parent, name,
1715 (struct hive_key **)&key))) {
1716 DEBUG(2, ("Key '%s' not found\n", name));
1717 return WERR_BADFILE;
1720 if (key->nk->subkeys_offset != -1) {
1721 char *sk_name;
1722 struct hive_key *sk = (struct hive_key *)key;
1723 unsigned int i = key->nk->num_subkeys;
1724 while (i--) {
1725 /* Get subkey information. */
1726 error = regf_get_subkey_by_index(parent_nk, sk, 0,
1727 (const char **)&sk_name,
1728 NULL, NULL);
1729 if (!W_ERROR_IS_OK(error)) {
1730 DEBUG(0, ("Can't retrieve subkey by index.\n"));
1731 return error;
1734 /* Delete subkey. */
1735 error = regf_del_key(NULL, sk, sk_name);
1736 if (!W_ERROR_IS_OK(error)) {
1737 DEBUG(0, ("Can't delete key '%s'.\n", sk_name));
1738 return error;
1741 talloc_free(sk_name);
1745 if (key->nk->values_offset != -1) {
1746 char *val_name;
1747 struct hive_key *sk = (struct hive_key *)key;
1748 DATA_BLOB data;
1749 unsigned int i = key->nk->num_values;
1750 while (i--) {
1751 /* Get value information. */
1752 error = regf_get_value(parent_nk, sk, 0,
1753 (const char **)&val_name,
1754 NULL, &data);
1755 if (!W_ERROR_IS_OK(error)) {
1756 DEBUG(0, ("Can't retrieve value by index.\n"));
1757 return error;
1760 /* Delete value. */
1761 error = regf_del_value(NULL, sk, val_name);
1762 if (!W_ERROR_IS_OK(error)) {
1763 DEBUG(0, ("Can't delete value '%s'.\n", val_name));
1764 return error;
1767 talloc_free(val_name);
1771 /* Delete it from the subkey list. */
1772 error = regf_sl_del_entry(private_data->hive, parent_nk->subkeys_offset,
1773 key->offset, &parent_nk->subkeys_offset);
1774 if (!W_ERROR_IS_OK(error)) {
1775 DEBUG(0, ("Can't store new subkey list for parent key. Won't delete.\n"));
1776 return error;
1779 /* Re-store parent key */
1780 parent_nk->num_subkeys--;
1781 hbin_store_tdr_resize(private_data->hive,
1782 (tdr_push_fn_t) tdr_push_nk_block,
1783 private_data->offset, parent_nk);
1785 if (key->nk->clsname_offset != -1) {
1786 hbin_free(private_data->hive, key->nk->clsname_offset);
1788 hbin_free(private_data->hive, key->offset);
1790 return regf_save_hbin(private_data->hive, 0);
1793 static WERROR regf_add_key(TALLOC_CTX *ctx, const struct hive_key *parent,
1794 const char *name, const char *classname,
1795 struct security_descriptor *sec_desc,
1796 struct hive_key **ret)
1798 const struct regf_key_data *private_data =
1799 (const struct regf_key_data *)parent;
1800 struct nk_block *parent_nk = private_data->nk, nk;
1801 struct nk_block *root;
1802 struct regf_data *regf = private_data->hive;
1803 uint32_t offset;
1804 WERROR error;
1806 nk.header = "nk";
1807 nk.type = REG_SUB_KEY;
1808 unix_to_nt_time(&nk.last_change, time(NULL));
1809 nk.uk1 = 0;
1810 nk.parent_offset = private_data->offset;
1811 nk.num_subkeys = 0;
1812 nk.uk2 = 0;
1813 nk.subkeys_offset = -1;
1814 nk.unknown_offset = -1;
1815 nk.num_values = 0;
1816 nk.values_offset = -1;
1817 memset(nk.unk3, 0, sizeof(nk.unk3));
1818 nk.clsname_offset = -1; /* FIXME: fill in */
1819 nk.clsname_length = 0;
1820 nk.key_name = name;
1822 /* Get the security descriptor of the root key */
1823 root = talloc_zero(ctx, struct nk_block);
1824 W_ERROR_HAVE_NO_MEMORY(root);
1826 if (!hbin_get_tdr(regf, regf->header->data_offset, root,
1827 (tdr_pull_fn_t)tdr_pull_nk_block, root)) {
1828 DEBUG(0, ("Unable to find HBIN data for offset 0x%x\n",
1829 regf->header->data_offset));
1830 return WERR_GENERAL_FAILURE;
1832 nk.sk_offset = root->sk_offset;
1833 talloc_free(root);
1835 /* Store the new nk key */
1836 offset = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_nk_block, &nk);
1838 error = regf_sl_add_entry(regf, parent_nk->subkeys_offset, name, offset,
1839 &parent_nk->subkeys_offset);
1840 if (!W_ERROR_IS_OK(error)) {
1841 hbin_free(regf, offset);
1842 return error;
1845 parent_nk->num_subkeys++;
1847 /* Since the subkey offset of the parent can change, store it again */
1848 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
1849 nk.parent_offset, parent_nk);
1851 *ret = (struct hive_key *)regf_get_key(ctx, regf, offset);
1853 DEBUG(9, ("Storing key %s\n", name));
1854 return regf_save_hbin(private_data->hive, 0);
1857 static WERROR regf_set_value(struct hive_key *key, const char *name,
1858 uint32_t type, const DATA_BLOB data)
1860 struct regf_key_data *private_data = (struct regf_key_data *)key;
1861 struct regf_data *regf = private_data->hive;
1862 struct nk_block *nk = private_data->nk;
1863 struct vk_block vk;
1864 uint32_t i;
1865 uint32_t tmp_vk_offset, vk_offset, old_vk_offset = (uint32_t) -1;
1866 DATA_BLOB values;
1868 ZERO_STRUCT(vk);
1870 /* find the value offset, if it exists */
1871 if (nk->values_offset != -1) {
1872 values = hbin_get(regf, nk->values_offset);
1874 for (i = 0; i < nk->num_values; i++) {
1875 tmp_vk_offset = IVAL(values.data, i * 4);
1876 if (!hbin_get_tdr(regf, tmp_vk_offset, private_data,
1877 (tdr_pull_fn_t)tdr_pull_vk_block,
1878 &vk)) {
1879 DEBUG(0, ("Unable to get VK block at 0x%x\n",
1880 tmp_vk_offset));
1881 return WERR_GENERAL_FAILURE;
1883 if (strcmp(vk.data_name, name) == 0) {
1884 old_vk_offset = tmp_vk_offset;
1885 break;
1890 /* If it's new, create the vk struct, if it's old, free the old data. */
1891 if (old_vk_offset == -1) {
1892 vk.header = "vk";
1893 vk.name_length = strlen(name);
1894 if (name != NULL && name[0] != 0) {
1895 vk.flag = 1;
1896 vk.data_name = name;
1897 } else {
1898 vk.data_name = NULL;
1899 vk.flag = 0;
1901 } else {
1902 /* Free data, if any */
1903 if (!(vk.data_length & 0x80000000)) {
1904 hbin_free(regf, vk.data_offset);
1908 /* Set the type and data */
1909 vk.data_length = data.length;
1910 vk.data_type = type;
1911 if ((type == REG_DWORD) || (type == REG_DWORD_BIG_ENDIAN)) {
1912 if (vk.data_length != sizeof(uint32_t)) {
1913 DEBUG(0, ("DWORD or DWORD_BIG_ENDIAN value with size other than 4 byte!\n"));
1914 return WERR_NOT_SUPPORTED;
1916 vk.data_length |= 0x80000000;
1917 vk.data_offset = IVAL(data.data, 0);
1918 } else {
1919 /* Store data somewhere */
1920 vk.data_offset = hbin_store(regf, data);
1922 if (old_vk_offset == -1) {
1923 /* Store new vk */
1924 vk_offset = hbin_store_tdr(regf,
1925 (tdr_push_fn_t) tdr_push_vk_block,
1926 &vk);
1927 } else {
1928 /* Store vk at offset */
1929 vk_offset = hbin_store_tdr_resize(regf,
1930 (tdr_push_fn_t) tdr_push_vk_block,
1931 old_vk_offset ,&vk);
1934 /* Re-allocate the value list */
1935 if (nk->values_offset == -1) {
1936 nk->values_offset = hbin_store_tdr(regf,
1937 (tdr_push_fn_t) tdr_push_uint32,
1938 &vk_offset);
1939 nk->num_values = 1;
1940 } else {
1942 /* Change if we're changing, otherwise we're adding the value */
1943 if (old_vk_offset != -1) {
1944 /* Find and overwrite the offset. */
1945 for (i = 0; i < nk->num_values; i++) {
1946 if (IVAL(values.data, i * 4) == old_vk_offset) {
1947 SIVAL(values.data, i * 4, vk_offset);
1948 break;
1951 } else {
1952 /* Create a new value list */
1953 DATA_BLOB value_list;
1955 value_list.length = (nk->num_values+1)*4;
1956 value_list.data = (uint8_t *)talloc_array(private_data,
1957 uint32_t,
1958 nk->num_values+1);
1959 W_ERROR_HAVE_NO_MEMORY(value_list.data);
1960 memcpy(value_list.data, values.data, nk->num_values * 4);
1962 SIVAL(value_list.data, nk->num_values * 4, vk_offset);
1963 nk->num_values++;
1964 nk->values_offset = hbin_store_resize(regf,
1965 nk->values_offset,
1966 value_list);
1970 hbin_store_tdr_resize(regf,
1971 (tdr_push_fn_t) tdr_push_nk_block,
1972 private_data->offset, nk);
1973 return regf_save_hbin(private_data->hive, 0);
1976 static WERROR regf_save_hbin(struct regf_data *regf, bool flush)
1978 struct tdr_push *push = tdr_push_init(regf);
1979 unsigned int i;
1981 W_ERROR_HAVE_NO_MEMORY(push);
1983 /* Only write once every 5 seconds, or when flush is set */
1984 if (!flush && regf->last_write + 5 >= time(NULL)) {
1985 return WERR_OK;
1988 regf->last_write = time(NULL);
1990 if (lseek(regf->fd, 0, SEEK_SET) == -1) {
1991 DEBUG(0, ("Error lseeking in regf file\n"));
1992 return WERR_GENERAL_FAILURE;
1995 /* Recompute checksum */
1996 if (NT_STATUS_IS_ERR(tdr_push_regf_hdr(push, regf->header))) {
1997 DEBUG(0, ("Failed to push regf header\n"));
1998 return WERR_GENERAL_FAILURE;
2000 regf->header->chksum = regf_hdr_checksum(push->data.data);
2001 talloc_free(push);
2003 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
2004 (tdr_push_fn_t)tdr_push_regf_hdr,
2005 regf->header))) {
2006 DEBUG(0, ("Error writing registry file header\n"));
2007 return WERR_GENERAL_FAILURE;
2010 if (lseek(regf->fd, 0x1000, SEEK_SET) == -1) {
2011 DEBUG(0, ("Error lseeking to 0x1000 in regf file\n"));
2012 return WERR_GENERAL_FAILURE;
2015 for (i = 0; regf->hbins[i]; i++) {
2016 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
2017 (tdr_push_fn_t)tdr_push_hbin_block,
2018 regf->hbins[i]))) {
2019 DEBUG(0, ("Error writing HBIN block\n"));
2020 return WERR_GENERAL_FAILURE;
2024 return WERR_OK;
2027 WERROR reg_create_regf_file(TALLOC_CTX *parent_ctx,
2028 const char *location,
2029 int minor_version, struct hive_key **key)
2031 struct regf_data *regf;
2032 struct regf_hdr *regf_hdr;
2033 struct nk_block nk;
2034 struct sk_block sk;
2035 WERROR error;
2036 DATA_BLOB data;
2037 struct security_descriptor *sd;
2038 uint32_t sk_offset;
2040 regf = (struct regf_data *)talloc_zero(NULL, struct regf_data);
2042 W_ERROR_HAVE_NO_MEMORY(regf);
2044 DEBUG(5, ("Attempting to create registry file\n"));
2046 /* Get the header */
2047 regf->fd = creat(location, 0644);
2049 if (regf->fd == -1) {
2050 DEBUG(0,("Could not create file: %s, %s\n", location,
2051 strerror(errno)));
2052 talloc_free(regf);
2053 return WERR_GENERAL_FAILURE;
2056 regf_hdr = talloc_zero(regf, struct regf_hdr);
2057 W_ERROR_HAVE_NO_MEMORY(regf_hdr);
2058 regf_hdr->REGF_ID = "regf";
2059 unix_to_nt_time(&regf_hdr->modtime, time(NULL));
2060 regf_hdr->version.major = 1;
2061 regf_hdr->version.minor = minor_version;
2062 regf_hdr->last_block = 0x1000; /* Block size */
2063 regf_hdr->description = talloc_strdup(regf_hdr,
2064 "Registry created by Samba 4");
2065 W_ERROR_HAVE_NO_MEMORY(regf_hdr->description);
2066 regf_hdr->chksum = 0;
2068 regf->header = regf_hdr;
2070 /* Create all hbin blocks */
2071 regf->hbins = talloc_array(regf, struct hbin_block *, 1);
2072 W_ERROR_HAVE_NO_MEMORY(regf->hbins);
2073 regf->hbins[0] = NULL;
2075 nk.header = "nk";
2076 nk.type = REG_ROOT_KEY;
2077 unix_to_nt_time(&nk.last_change, time(NULL));
2078 nk.uk1 = 0;
2079 nk.parent_offset = -1;
2080 nk.num_subkeys = 0;
2081 nk.uk2 = 0;
2082 nk.subkeys_offset = -1;
2083 nk.unknown_offset = -1;
2084 nk.num_values = 0;
2085 nk.values_offset = -1;
2086 memset(nk.unk3, 0, 5);
2087 nk.clsname_offset = -1;
2088 nk.clsname_length = 0;
2089 nk.sk_offset = 0x80;
2090 nk.key_name = "SambaRootKey";
2093 * It should be noted that changing the key_name to something shorter
2094 * creates a shorter nk block, which makes the position of the sk block
2095 * change. All Windows registries I've seen have the sk at 0x80.
2096 * I therefore recommend that our regf files share that offset -- Wilco
2099 /* Create a security descriptor. */
2100 sd = security_descriptor_dacl_create(regf,
2102 NULL, NULL,
2103 SID_NT_AUTHENTICATED_USERS,
2104 SEC_ACE_TYPE_ACCESS_ALLOWED,
2105 SEC_GENERIC_ALL,
2106 SEC_ACE_FLAG_OBJECT_INHERIT,
2107 NULL);
2109 /* Push the security descriptor to a blob */
2110 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf,
2111 sd, (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
2112 DEBUG(0, ("Unable to push security descriptor\n"));
2113 return WERR_GENERAL_FAILURE;
2116 ZERO_STRUCT(sk);
2117 sk.header = "sk";
2118 sk.prev_offset = 0x80;
2119 sk.next_offset = 0x80;
2120 sk.ref_cnt = 1;
2121 sk.rec_size = data.length;
2122 sk.sec_desc = data.data;
2124 /* Store the new nk key */
2125 regf->header->data_offset = hbin_store_tdr(regf,
2126 (tdr_push_fn_t)tdr_push_nk_block,
2127 &nk);
2128 /* Store the sk block */
2129 sk_offset = hbin_store_tdr(regf,
2130 (tdr_push_fn_t) tdr_push_sk_block,
2131 &sk);
2132 if (sk_offset != 0x80) {
2133 DEBUG(0, ("Error storing sk block, should be at 0x80, stored at 0x%x\n", nk.sk_offset));
2134 return WERR_GENERAL_FAILURE;
2138 *key = (struct hive_key *)regf_get_key(parent_ctx, regf,
2139 regf->header->data_offset);
2141 error = regf_save_hbin(regf, 1);
2142 if (!W_ERROR_IS_OK(error)) {
2143 return error;
2146 /* We can drop our own reference now that *key will have created one */
2147 talloc_unlink(NULL, regf);
2149 return WERR_OK;
2152 static WERROR regf_flush_key(struct hive_key *key)
2154 struct regf_key_data *private_data = (struct regf_key_data *)key;
2155 struct regf_data *regf = private_data->hive;
2156 WERROR error;
2158 error = regf_save_hbin(regf, 1);
2159 if (!W_ERROR_IS_OK(error)) {
2160 DEBUG(0, ("Failed to flush regf to disk\n"));
2161 return error;
2164 return WERR_OK;
2167 static int regf_destruct(struct regf_data *regf)
2169 WERROR error;
2171 /* Write to disk */
2172 error = regf_save_hbin(regf, 1);
2173 if (!W_ERROR_IS_OK(error)) {
2174 DEBUG(0, ("Failed to flush registry to disk\n"));
2175 return -1;
2178 /* Close file descriptor */
2179 close(regf->fd);
2181 return 0;
2184 WERROR reg_open_regf_file(TALLOC_CTX *parent_ctx, const char *location,
2185 struct hive_key **key)
2187 struct regf_data *regf;
2188 struct regf_hdr *regf_hdr;
2189 struct tdr_pull *pull;
2190 unsigned int i;
2192 regf = (struct regf_data *)talloc_zero(parent_ctx, struct regf_data);
2193 W_ERROR_HAVE_NO_MEMORY(regf);
2195 talloc_set_destructor(regf, regf_destruct);
2197 DEBUG(5, ("Attempting to load registry file\n"));
2199 /* Get the header */
2200 regf->fd = open(location, O_RDWR);
2202 if (regf->fd == -1) {
2203 DEBUG(0,("Could not load file: %s, %s\n", location,
2204 strerror(errno)));
2205 talloc_free(regf);
2206 return WERR_GENERAL_FAILURE;
2209 pull = tdr_pull_init(regf);
2211 pull->data.data = (uint8_t*)fd_load(regf->fd, &pull->data.length, 0, regf);
2213 if (pull->data.data == NULL) {
2214 DEBUG(0, ("Error reading data from file: %s\n", location));
2215 talloc_free(regf);
2216 return WERR_GENERAL_FAILURE;
2219 regf_hdr = talloc(regf, struct regf_hdr);
2220 W_ERROR_HAVE_NO_MEMORY(regf_hdr);
2222 if (NT_STATUS_IS_ERR(tdr_pull_regf_hdr(pull, regf_hdr, regf_hdr))) {
2223 DEBUG(0, ("Failed to pull regf header from file: %s\n", location));
2224 talloc_free(regf);
2225 return WERR_GENERAL_FAILURE;
2228 regf->header = regf_hdr;
2230 if (strcmp(regf_hdr->REGF_ID, "regf") != 0) {
2231 DEBUG(0, ("Unrecognized NT registry header id: %s, %s\n",
2232 regf_hdr->REGF_ID, location));
2233 talloc_free(regf);
2234 return WERR_GENERAL_FAILURE;
2237 /* Validate the header ... */
2238 if (regf_hdr_checksum(pull->data.data) != regf_hdr->chksum) {
2239 DEBUG(0, ("Registry file checksum error: %s: %d,%d\n",
2240 location, regf_hdr->chksum,
2241 regf_hdr_checksum(pull->data.data)));
2242 talloc_free(regf);
2243 return WERR_GENERAL_FAILURE;
2246 pull->offset = 0x1000;
2248 i = 0;
2249 /* Read in all hbin blocks */
2250 regf->hbins = talloc_array(regf, struct hbin_block *, 1);
2251 W_ERROR_HAVE_NO_MEMORY(regf->hbins);
2253 regf->hbins[0] = NULL;
2255 while (pull->offset < pull->data.length &&
2256 pull->offset <= regf->header->last_block) {
2257 struct hbin_block *hbin = talloc(regf->hbins,
2258 struct hbin_block);
2260 W_ERROR_HAVE_NO_MEMORY(hbin);
2262 if (NT_STATUS_IS_ERR(tdr_pull_hbin_block(pull, hbin, hbin))) {
2263 DEBUG(0, ("[%d] Error parsing HBIN block\n", i));
2264 talloc_free(regf);
2265 return WERR_FOOBAR;
2268 if (strcmp(hbin->HBIN_ID, "hbin") != 0) {
2269 DEBUG(0, ("[%d] Expected 'hbin', got '%s'\n",
2270 i, hbin->HBIN_ID));
2271 talloc_free(regf);
2272 return WERR_FOOBAR;
2275 regf->hbins[i] = hbin;
2276 i++;
2277 regf->hbins = talloc_realloc(regf, regf->hbins,
2278 struct hbin_block *, i+2);
2279 regf->hbins[i] = NULL;
2282 talloc_free(pull);
2284 DEBUG(1, ("%d HBIN blocks read\n", i));
2286 *key = (struct hive_key *)regf_get_key(parent_ctx, regf,
2287 regf->header->data_offset);
2289 /* We can drop our own reference now that *key will have created one */
2290 talloc_unlink(parent_ctx, regf);
2292 return WERR_OK;
2295 static struct hive_operations reg_backend_regf = {
2296 .name = "regf",
2297 .get_key_info = regf_get_info,
2298 .enum_key = regf_get_subkey_by_index,
2299 .get_key_by_name = regf_get_subkey_by_name,
2300 .get_value_by_name = regf_get_value_by_name,
2301 .enum_value = regf_get_value,
2302 .get_sec_desc = regf_get_sec_desc,
2303 .set_sec_desc = regf_set_sec_desc,
2304 .add_key = regf_add_key,
2305 .set_value = regf_set_value,
2306 .del_key = regf_del_key,
2307 .delete_value = regf_del_value,
2308 .flush_key = regf_flush_key