2 Samba CIFS implementation
3 Registry backend for REGF files
4 Copyright (C) 2005-2007 Jelmer Vernooij, jelmer@samba.org
5 Copyright (C) 2006 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/>. */
21 #include "lib/registry/hive.h"
22 #include "system/filesys.h"
23 #include "system/time.h"
24 #include "lib/registry/tdr_regf.h"
25 #include "librpc/gen_ndr/ndr_security.h"
26 #include "librpc/gen_ndr/winreg.h"
27 #include "param/param.h"
29 static struct hive_operations reg_backend_regf;
32 * There are several places on the web where the REGF format is explained;
38 * - Return error codes that make more sense
40 * - do more things in-memory
44 * Read HBIN blocks into memory
49 struct hbin_block **hbins;
50 struct regf_hdr *header;
51 struct smb_iconv_convenience *iconv_convenience;
54 static WERROR regf_save_hbin(struct regf_data *data);
56 struct regf_key_data {
58 struct regf_data *hive;
63 static struct hbin_block *hbin_by_offset(const struct regf_data *data,
64 uint32_t offset, uint32_t *rel_offset)
68 for (i = 0; data->hbins[i]; i++) {
69 if (offset >= data->hbins[i]->offset_from_first &&
70 offset < data->hbins[i]->offset_from_first+
71 data->hbins[i]->offset_to_next) {
72 if (rel_offset != NULL)
73 *rel_offset = offset - data->hbins[i]->offset_from_first - 0x20;
74 return data->hbins[i];
82 * Validate a regf header
83 * For now, do nothing, but we should check the checksum
85 static uint32_t regf_hdr_checksum(const uint8_t *buffer)
87 uint32_t checksum = 0, x;
90 for (i = 0; i < 0x01FB; i+= 4) {
99 * Obtain the contents of a HBIN block
101 static DATA_BLOB hbin_get(const struct regf_data *data, uint32_t offset)
104 struct hbin_block *hbin;
110 hbin = hbin_by_offset(data, offset, &rel_offset);
113 DEBUG(1, ("Can't find HBIN containing 0x%04x\n", offset));
117 ret.length = IVAL(hbin->data, rel_offset);
118 if (!(ret.length & 0x80000000)) {
119 DEBUG(0, ("Trying to use dirty block at 0x%04x\n", offset));
123 /* remove high bit */
124 ret.length = (ret.length ^ 0xffffffff) + 1;
126 ret.length -= 4; /* 4 bytes for the length... */
127 ret.data = hbin->data +
128 (offset - hbin->offset_from_first - 0x20) + 4;
133 static bool hbin_get_tdr(struct regf_data *regf, uint32_t offset,
134 TALLOC_CTX *ctx, tdr_pull_fn_t pull_fn, void *p)
136 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
138 pull->data = hbin_get(regf, offset);
139 if (!pull->data.data) {
140 DEBUG(1, ("Unable to get data at 0x%04x\n", offset));
145 if (NT_STATUS_IS_ERR(pull_fn(pull, ctx, p))) {
146 DEBUG(1, ("Error parsing record at 0x%04x using tdr\n",
156 /* Allocate some new data */
157 static DATA_BLOB hbin_alloc(struct regf_data *data, uint32_t size,
161 uint32_t rel_offset = -1; /* Relative offset ! */
162 struct hbin_block *hbin = NULL;
168 return data_blob(NULL, 0);
170 size += 4; /* Need to include int32 for the length */
172 /* Allocate as a multiple of 8 */
173 size = (size + 7) & ~7;
178 for (i = 0; (hbin = data->hbins[i]); i++) {
181 for (j = 0; j < hbin->offset_to_next-0x20; j+= my_size) {
182 my_size = IVALS(hbin->data, j);
184 if (my_size == 0x0) {
185 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
189 if (my_size % 8 != 0) {
190 DEBUG(0, ("Encountered non-aligned block!\n"));
193 if (my_size < 0) { /* Used... */
195 } else if (my_size == size) { /* exact match */
197 DEBUG(4, ("Found free block of exact size %d in middle of HBIN\n",
200 } else if (my_size > size) { /* data will remain */
202 /* Split this block and mark the next block as free */
203 SIVAL(hbin->data, rel_offset+size, my_size-size);
204 DEBUG(4, ("Found free block of size %d (needing %d) in middle of HBIN\n",
210 if (rel_offset != -1)
214 /* No space available in previous hbins,
215 * allocate new one */
216 if (data->hbins[i] == NULL) {
217 DEBUG(4, ("No space available in other HBINs for block of size %d, allocating new HBIN\n",
219 data->hbins = talloc_realloc(data, data->hbins,
220 struct hbin_block *, i+2);
221 hbin = talloc(data->hbins, struct hbin_block);
222 SMB_ASSERT(hbin != NULL);
224 data->hbins[i] = hbin;
225 data->hbins[i+1] = NULL;
227 hbin->HBIN_ID = talloc_strdup(hbin, "hbin");
228 hbin->offset_from_first = (i == 0?0:data->hbins[i-1]->offset_from_first+data->hbins[i-1]->offset_to_next);
229 hbin->offset_to_next = 0x1000;
230 hbin->unknown[0] = 0;
231 hbin->unknown[0] = 0;
232 unix_to_nt_time(&hbin->last_change, time(NULL));
233 hbin->block_size = hbin->offset_to_next;
234 hbin->data = talloc_zero_array(hbin, uint8_t, hbin->block_size - 0x20);
237 SIVAL(hbin->data, size, hbin->block_size - size - 0x20);
240 /* Set size and mark as used */
241 SIVAL(hbin->data, rel_offset, -size);
243 ret.data = hbin->data + rel_offset + 0x4; /* Skip past length */
244 ret.length = size - 0x4;
246 uint32_t new_rel_offset;
247 *offset = hbin->offset_from_first + rel_offset + 0x20;
248 SMB_ASSERT(hbin_by_offset(data, *offset, &new_rel_offset) == hbin);
249 SMB_ASSERT(new_rel_offset == rel_offset);
255 /* Store a data blob. Return the offset at which it was stored */
256 static uint32_t hbin_store (struct regf_data *data, DATA_BLOB blob)
259 DATA_BLOB dest = hbin_alloc(data, blob.length, &ret);
261 memcpy(dest.data, blob.data, blob.length);
266 static uint32_t hbin_store_tdr(struct regf_data *data,
267 tdr_push_fn_t push_fn, void *p)
269 struct tdr_push *push = tdr_push_init(data, data->iconv_convenience);
272 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
273 DEBUG(0, ("Error during push\n"));
277 ret = hbin_store(data, push->data);
285 /* Free existing data */
286 static void hbin_free (struct regf_data *data, uint32_t offset)
291 struct hbin_block *hbin;
293 SMB_ASSERT (offset > 0);
295 hbin = hbin_by_offset(data, offset, &rel_offset);
300 /* Get original size */
301 size = IVALS(hbin->data, rel_offset);
304 DEBUG(1, ("Trying to free already freed block at 0x%04x\n",
311 /* If the next block is free, merge into big free block */
312 if (rel_offset + size < hbin->offset_to_next) {
313 next_size = IVALS(hbin->data, rel_offset+size);
319 /* Write block size */
320 SIVALS(hbin->data, rel_offset, size);
324 * Store a data blob data was already stored, but has changed in size
325 * Will try to save it at the current location if possible, otherwise
326 * does a free + store */
327 static uint32_t hbin_store_resize(struct regf_data *data,
328 uint32_t orig_offset, DATA_BLOB blob)
331 struct hbin_block *hbin = hbin_by_offset(data, orig_offset,
336 int32_t possible_size;
339 SMB_ASSERT(orig_offset > 0);
342 return hbin_store(data, blob);
344 /* Get original size */
345 orig_size = -IVALS(hbin->data, rel_offset);
347 needed_size = blob.length + 4; /* Add int32 containing length */
348 needed_size = (needed_size + 7) & ~7; /* Align */
350 /* Fits into current allocated block */
351 if (orig_size >= needed_size) {
352 memcpy(hbin->data + rel_offset + 0x4, blob.data, blob.length);
353 /* If the difference in size is greater than 0x4, split the block
354 * and free/merge it */
355 if (orig_size - needed_size > 0x4) {
356 SIVALS(hbin->data, rel_offset, -needed_size);
357 SIVALS(hbin->data, rel_offset + needed_size,
358 needed_size-orig_size);
359 hbin_free(data, orig_offset + needed_size);
364 possible_size = orig_size;
366 /* Check if it can be combined with the next few free records */
367 for (i = rel_offset; i < hbin->offset_to_next - 0x20; i += my_size) {
368 if (IVALS(hbin->data, i) < 0) /* Used */
371 my_size = IVALS(hbin->data, i);
373 if (my_size == 0x0) {
374 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
377 possible_size += my_size;
380 if (possible_size >= blob.length) {
381 SIVAL(hbin->data, rel_offset, -possible_size);
382 memcpy(hbin->data + rel_offset + 0x4,
383 blob.data, blob.length);
388 hbin_free(data, orig_offset);
389 return hbin_store(data, blob);
392 static uint32_t hbin_store_tdr_resize(struct regf_data *regf,
393 tdr_push_fn_t push_fn,
394 uint32_t orig_offset, void *p)
396 struct tdr_push *push = tdr_push_init(regf, regf->iconv_convenience);
399 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
400 DEBUG(0, ("Error during push\n"));
404 ret = hbin_store_resize(regf, orig_offset, push->data);
411 static uint32_t regf_create_lh_hash(const char *name)
417 hash_name = strupper_talloc(NULL, name);
418 for (i = 0; *(hash_name + i) != 0; i++) {
420 ret += *(hash_name + i);
422 talloc_free(hash_name);
426 static WERROR regf_get_info(TALLOC_CTX *mem_ctx,
427 const struct hive_key *key,
428 const char **classname,
429 uint32_t *num_subkeys,
430 uint32_t *num_values,
431 NTTIME *last_mod_time)
433 const struct regf_key_data *private_data =
434 (const struct regf_key_data *)key;
436 if (num_subkeys != NULL)
437 *num_subkeys = private_data->nk->num_subkeys;
439 if (num_values != NULL)
440 *num_values = private_data->nk->num_values;
442 if (classname != NULL) {
443 if (private_data->nk->clsname_offset != -1) {
444 DATA_BLOB data = hbin_get(private_data->hive,
445 private_data->nk->clsname_offset);
446 *classname = talloc_strndup(mem_ctx,
448 private_data->nk->clsname_length);
453 /* TODO: Last mod time */
458 static struct regf_key_data *regf_get_key(TALLOC_CTX *ctx,
459 struct regf_data *regf,
463 struct regf_key_data *ret;
465 ret = talloc_zero(ctx, struct regf_key_data);
466 ret->key.ops = ®_backend_regf;
467 ret->hive = talloc_reference(ret, regf);
468 ret->offset = offset;
469 nk = talloc(ret, struct nk_block);
475 if (!hbin_get_tdr(regf, offset, nk,
476 (tdr_pull_fn_t)tdr_pull_nk_block, nk)) {
477 DEBUG(0, ("Unable to find HBIN data for offset %d\n", offset));
481 if (strcmp(nk->header, "nk") != 0) {
482 DEBUG(0, ("Expected nk record, got %s\n", nk->header));
491 static WERROR regf_get_value(TALLOC_CTX *ctx, struct hive_key *key,
492 int idx, const char **name,
493 uint32_t *data_type, DATA_BLOB *data)
495 const struct regf_key_data *private_data =
496 (const struct regf_key_data *)key;
498 struct regf_data *regf = private_data->hive;
502 if (idx >= private_data->nk->num_values)
503 return WERR_NO_MORE_ITEMS;
505 tmp = hbin_get(regf, private_data->nk->values_offset);
507 DEBUG(0, ("Unable to find value list\n"));
508 return WERR_GENERAL_FAILURE;
511 if (tmp.length < private_data->nk->num_values * 4) {
512 DEBUG(1, ("Value counts mismatch\n"));
515 vk_offset = IVAL(tmp.data, idx * 4);
517 vk = talloc(NULL, struct vk_block);
518 W_ERROR_HAVE_NO_MEMORY(vk);
520 if (!hbin_get_tdr(regf, vk_offset, vk,
521 (tdr_pull_fn_t)tdr_pull_vk_block, vk)) {
522 DEBUG(0, ("Unable to get VK block at %d\n", vk_offset));
524 return WERR_GENERAL_FAILURE;
527 /* FIXME: name character set ?*/
529 *name = talloc_strndup(ctx, vk->data_name, vk->name_length);
531 if (data_type != NULL)
532 *data_type = vk->data_type;
534 if (vk->data_length & 0x80000000) {
535 vk->data_length &=~0x80000000;
536 data->data = (uint8_t *)&vk->data_offset;
537 data->length = vk->data_length;
539 *data = hbin_get(regf, vk->data_offset);
542 if (data->length < vk->data_length) {
543 DEBUG(1, ("Read data less than indicated data length!\n"));
551 static WERROR regf_get_value_by_name(TALLOC_CTX *mem_ctx,
552 struct hive_key *key, const char *name,
553 uint32_t *type, DATA_BLOB *data)
559 /* FIXME: Do binary search? Is this list sorted at all? */
561 for (i = 0; W_ERROR_IS_OK(error = regf_get_value(mem_ctx, key, i,
562 &vname, type, data));
564 if (!strcmp(vname, name))
568 if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
569 return WERR_NOT_FOUND;
575 static WERROR regf_get_subkey_by_index(TALLOC_CTX *ctx,
576 const struct hive_key *key,
577 uint32_t idx, const char **name,
578 const char **classname,
579 NTTIME *last_mod_time)
582 struct regf_key_data *ret;
583 const struct regf_key_data *private_data = (const struct regf_key_data *)key;
584 struct nk_block *nk = private_data->nk;
587 if (idx >= nk->num_subkeys)
588 return WERR_NO_MORE_ITEMS;
590 data = hbin_get(private_data->hive, nk->subkeys_offset);
592 DEBUG(0, ("Unable to find subkey list\n"));
593 return WERR_GENERAL_FAILURE;
596 if (!strncmp((char *)data.data, "li", 2)) {
598 struct tdr_pull *pull = tdr_pull_init(private_data->hive, private_data->hive->iconv_convenience);
600 DEBUG(10, ("Subkeys in LI list\n"));
603 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, nk, &li))) {
604 DEBUG(0, ("Error parsing LI list\n"));
606 return WERR_GENERAL_FAILURE;
609 SMB_ASSERT(!strncmp(li.header, "li", 2));
611 if (li.key_count != nk->num_subkeys) {
612 DEBUG(0, ("Subkey counts don't match\n"));
613 return WERR_GENERAL_FAILURE;
615 key_off = li.nk_offset[idx];
617 } else if (!strncmp((char *)data.data, "lf", 2)) {
619 struct tdr_pull *pull = tdr_pull_init(private_data->hive, private_data->hive->iconv_convenience);
621 DEBUG(10, ("Subkeys in LF list\n"));
624 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, nk, &lf))) {
625 DEBUG(0, ("Error parsing LF list\n"));
627 return WERR_GENERAL_FAILURE;
630 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
632 if (lf.key_count != nk->num_subkeys) {
633 DEBUG(0, ("Subkey counts don't match\n"));
634 return WERR_GENERAL_FAILURE;
637 key_off = lf.hr[idx].nk_offset;
638 } else if (!strncmp((char *)data.data, "lh", 2)) {
640 struct tdr_pull *pull = tdr_pull_init(private_data->hive, private_data->hive->iconv_convenience);
642 DEBUG(10, ("Subkeys in LH list\n"));
645 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, nk, &lh))) {
646 DEBUG(0, ("Error parsing LH list\n"));
648 return WERR_GENERAL_FAILURE;
651 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
653 if (lh.key_count != nk->num_subkeys) {
654 DEBUG(0, ("Subkey counts don't match\n"));
655 return WERR_GENERAL_FAILURE;
657 key_off = lh.hr[idx].nk_offset;
658 } else if (!strncmp((char *)data.data, "ri", 2)) {
660 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
662 uint16_t sublist_count = 0;
664 DEBUG(10, ("Subkeys in RI list\n"));
667 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull, nk, &ri))) {
668 DEBUG(0, ("Error parsing RI list\n"));
670 return WERR_GENERAL_FAILURE;
672 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
674 for (i = 0; i < ri.key_count; i++) {
677 /* Get sublist data blob */
678 list_data = hbin_get(private_data->hive, ri.offset[i]);
679 if (!list_data.data) {
680 DEBUG(0, ("Error getting RI list."));
682 return WERR_GENERAL_FAILURE;
685 pull->data = list_data;
687 if (!strncmp((char *)list_data.data, "li", 2)) {
690 DEBUG(10, ("Subkeys in RI->LI list\n"));
692 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull,
695 DEBUG(0, ("Error parsing LI list from RI\n"));
697 return WERR_GENERAL_FAILURE;
699 SMB_ASSERT(!strncmp(li.header, "li", 2));
701 /* Advance to next sublist if necessary */
702 if (idx >= sublist_count + li.key_count) {
703 sublist_count += li.key_count;
706 key_off = li.nk_offset[idx - sublist_count];
707 sublist_count += li.key_count;
709 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
712 DEBUG(10, ("Subkeys in RI->LH list\n"));
714 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull,
717 DEBUG(0, ("Error parsing LH list from RI\n"));
719 return WERR_GENERAL_FAILURE;
721 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
723 /* Advance to next sublist if necessary */
724 if (idx >= sublist_count + lh.key_count) {
725 sublist_count += lh.key_count;
728 key_off = lh.hr[idx - sublist_count].nk_offset;
729 sublist_count += lh.key_count;
732 DEBUG(0,("Unknown sublist in ri block\n"));
735 return WERR_GENERAL_FAILURE;
742 if (idx > sublist_count) {
743 return WERR_NO_MORE_ITEMS;
747 DEBUG(0, ("Unknown type for subkey list (0x%04x): %c%c\n",
748 nk->subkeys_offset, data.data[0], data.data[1]));
749 return WERR_GENERAL_FAILURE;
752 ret = regf_get_key (ctx, private_data->hive, key_off);
754 if (classname != NULL) {
755 if (ret->nk->clsname_offset != -1) {
756 DATA_BLOB db = hbin_get(ret->hive,
757 ret->nk->clsname_offset);
758 *classname = talloc_strndup(ctx,
760 ret->nk->clsname_length);
765 if (last_mod_time != NULL)
766 *last_mod_time = ret->nk->last_change;
769 *name = talloc_steal(ctx, ret->nk->key_name);
776 static WERROR regf_match_subkey_by_name(TALLOC_CTX *ctx,
777 const struct hive_key *key,
779 const char *name, uint32_t *ret)
781 DATA_BLOB subkey_data;
782 struct nk_block subkey;
783 struct tdr_pull *pull;
784 const struct regf_key_data *private_data =
785 (const struct regf_key_data *)key;
787 subkey_data = hbin_get(private_data->hive, offset);
788 if (!subkey_data.data) {
789 DEBUG(0, ("Unable to retrieve subkey HBIN\n"));
790 return WERR_GENERAL_FAILURE;
793 pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
795 pull->data = subkey_data;
797 if (NT_STATUS_IS_ERR(tdr_pull_nk_block(pull, ctx, &subkey))) {
798 DEBUG(0, ("Error parsing NK structure.\n"));
800 return WERR_GENERAL_FAILURE;
804 if (strncmp(subkey.header, "nk", 2)) {
805 DEBUG(0, ("Not an NK structure.\n"));
806 return WERR_GENERAL_FAILURE;
809 if (!strcasecmp(subkey.key_name, name)) {
817 static WERROR regf_get_subkey_by_name(TALLOC_CTX *ctx,
818 const struct hive_key *key,
820 struct hive_key **ret)
823 const struct regf_key_data *private_data =
824 (const struct regf_key_data *)key;
825 struct nk_block *nk = private_data->nk;
826 uint32_t key_off = 0;
828 data = hbin_get(private_data->hive, nk->subkeys_offset);
830 DEBUG(0, ("Unable to find subkey list\n"));
831 return WERR_GENERAL_FAILURE;
834 if (!strncmp((char *)data.data, "li", 2)) {
836 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
839 DEBUG(10, ("Subkeys in LI list\n"));
842 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, nk, &li))) {
843 DEBUG(0, ("Error parsing LI list\n"));
845 return WERR_GENERAL_FAILURE;
848 SMB_ASSERT(!strncmp(li.header, "li", 2));
850 if (li.key_count != nk->num_subkeys) {
851 DEBUG(0, ("Subkey counts don't match\n"));
852 return WERR_GENERAL_FAILURE;
855 for (i = 0; i < li.key_count; i++) {
856 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
864 return WERR_NOT_FOUND;
865 } else if (!strncmp((char *)data.data, "lf", 2)) {
867 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
870 DEBUG(10, ("Subkeys in LF list\n"));
873 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, nk, &lf))) {
874 DEBUG(0, ("Error parsing LF list\n"));
876 return WERR_GENERAL_FAILURE;
879 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
881 if (lf.key_count != nk->num_subkeys) {
882 DEBUG(0, ("Subkey counts don't match\n"));
883 return WERR_GENERAL_FAILURE;
886 for (i = 0; i < lf.key_count; i++) {
887 if (strncmp(lf.hr[i].hash, name, 4)) {
890 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
899 return WERR_NOT_FOUND;
900 } else if (!strncmp((char *)data.data, "lh", 2)) {
902 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
906 DEBUG(10, ("Subkeys in LH list\n"));
909 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, nk, &lh))) {
910 DEBUG(0, ("Error parsing LH list\n"));
912 return WERR_GENERAL_FAILURE;
915 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
917 if (lh.key_count != nk->num_subkeys) {
918 DEBUG(0, ("Subkey counts don't match\n"));
919 return WERR_GENERAL_FAILURE;
922 hash = regf_create_lh_hash(name);
923 for (i = 0; i < lh.key_count; i++) {
924 if (lh.hr[i].base37 != hash) {
927 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
936 return WERR_NOT_FOUND;
937 } else if (!strncmp((char *)data.data, "ri", 2)) {
939 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
942 DEBUG(10, ("Subkeys in RI list\n"));
945 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull, nk, &ri))) {
946 DEBUG(0, ("Error parsing RI list\n"));
948 return WERR_GENERAL_FAILURE;
950 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
952 for (i = 0; i < ri.key_count; i++) {
955 /* Get sublist data blob */
956 list_data = hbin_get(private_data->hive, ri.offset[i]);
957 if (list_data.data == NULL) {
958 DEBUG(0, ("Error getting RI list."));
960 return WERR_GENERAL_FAILURE;
963 pull->data = list_data;
965 if (!strncmp((char *)list_data.data, "li", 2)) {
968 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull,
971 DEBUG(0, ("Error parsing LI list from RI\n"));
973 return WERR_GENERAL_FAILURE;
975 SMB_ASSERT(!strncmp(li.header, "li", 2));
977 for (j = 0; j < li.key_count; j++) {
978 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
985 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
989 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull,
992 DEBUG(0, ("Error parsing LH list from RI\n"));
994 return WERR_GENERAL_FAILURE;
996 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
998 hash = regf_create_lh_hash(name);
999 for (j = 0; j < lh.key_count; j++) {
1000 if (lh.hr[j].base37 != hash) {
1003 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
1016 return WERR_NOT_FOUND;
1018 DEBUG(0, ("Unknown subkey list type.\n"));
1019 return WERR_GENERAL_FAILURE;
1022 *ret = (struct hive_key *)regf_get_key(ctx, private_data->hive,
1027 static WERROR regf_set_sec_desc(struct hive_key *key,
1028 const struct security_descriptor *sec_desc)
1030 const struct regf_key_data *private_data =
1031 (const struct regf_key_data *)key;
1032 struct sk_block cur_sk, sk, new_sk;
1033 struct regf_data *regf = private_data->hive;
1034 struct nk_block root;
1036 uint32_t sk_offset, cur_sk_offset;
1037 bool update_cur_sk = false;
1039 /* Get the root nk */
1040 hbin_get_tdr(regf, regf->header->data_offset, regf,
1041 (tdr_pull_fn_t) tdr_pull_nk_block, &root);
1043 /* Push the security descriptor to a blob */
1044 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf, NULL,
1045 sec_desc, (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
1046 DEBUG(0, ("Unable to push security descriptor\n"));
1047 return WERR_GENERAL_FAILURE;
1050 /* Get the current security descriptor for the key */
1051 if (!hbin_get_tdr(regf, private_data->nk->sk_offset, regf,
1052 (tdr_pull_fn_t) tdr_pull_sk_block, &cur_sk)) {
1053 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1054 return WERR_BADFILE;
1056 /* If there's no change, change nothing. */
1057 if (memcmp(data.data, cur_sk.sec_desc,
1058 MIN(data.length, cur_sk.rec_size)) == 0) {
1062 /* Delete the current sk if only this key is using it */
1063 if (cur_sk.ref_cnt == 1) {
1064 /* Get the previous security descriptor for the key */
1065 if (!hbin_get_tdr(regf, cur_sk.prev_offset, regf,
1066 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1067 DEBUG(0, ("Unable to find prev security descriptor for current key\n"));
1068 return WERR_BADFILE;
1070 /* Change and store the previous security descriptor */
1071 sk.next_offset = cur_sk.next_offset;
1072 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
1073 cur_sk.prev_offset, &sk);
1075 /* Get the next security descriptor for the key */
1076 if (!hbin_get_tdr(regf, cur_sk.next_offset, regf,
1077 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1078 DEBUG(0, ("Unable to find next security descriptor for current key\n"));
1079 return WERR_BADFILE;
1081 /* Change and store the next security descriptor */
1082 sk.prev_offset = cur_sk.prev_offset;
1083 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
1084 cur_sk.next_offset, &sk);
1086 hbin_free(regf, private_data->nk->sk_offset);
1088 /* This key will no longer be referring to this sk */
1090 update_cur_sk = true;
1093 sk_offset = root.sk_offset;
1096 cur_sk_offset = sk_offset;
1097 if (!hbin_get_tdr(regf, sk_offset, regf,
1098 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1099 DEBUG(0, ("Unable to find security descriptor\n"));
1100 return WERR_BADFILE;
1102 if (memcmp(data.data, sk.sec_desc, MIN(data.length, sk.rec_size)) == 0) {
1103 private_data->nk->sk_offset = sk_offset;
1105 hbin_store_tdr_resize(regf,
1106 (tdr_push_fn_t) tdr_push_sk_block,
1108 hbin_store_tdr_resize(regf,
1109 (tdr_push_fn_t) tdr_push_nk_block,
1110 private_data->offset,
1114 sk_offset = sk.next_offset;
1115 } while (sk_offset != root.sk_offset);
1117 ZERO_STRUCT(new_sk);
1118 new_sk.header = "sk";
1119 new_sk.prev_offset = cur_sk_offset;
1120 new_sk.next_offset = root.sk_offset;
1122 new_sk.rec_size = data.length;
1123 new_sk.sec_desc = data.data;
1125 sk_offset = hbin_store_tdr(regf,
1126 (tdr_push_fn_t) tdr_push_sk_block,
1128 if (sk_offset == -1) {
1129 DEBUG(0, ("Error storing sk block\n"));
1130 return WERR_GENERAL_FAILURE;
1132 private_data->nk->sk_offset = sk_offset;
1134 if (update_cur_sk) {
1135 hbin_store_tdr_resize(regf,
1136 (tdr_push_fn_t) tdr_push_sk_block,
1137 private_data->nk->sk_offset, &cur_sk);
1140 /* Get the previous security descriptor for the key */
1141 if (!hbin_get_tdr(regf, new_sk.prev_offset, regf,
1142 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1143 DEBUG(0, ("Unable to find security descriptor for previous key\n"));
1144 return WERR_BADFILE;
1146 /* Change and store the previous security descriptor */
1147 sk.next_offset = sk_offset;
1148 hbin_store_tdr_resize(regf,
1149 (tdr_push_fn_t) tdr_push_sk_block,
1150 cur_sk.prev_offset, &sk);
1152 /* Get the next security descriptor for the key (always root, as we append) */
1153 if (!hbin_get_tdr(regf, new_sk.next_offset, regf,
1154 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1155 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1156 return WERR_BADFILE;
1158 /* Change and store the next security descriptor (always root, as we append) */
1159 sk.prev_offset = sk_offset;
1160 hbin_store_tdr_resize(regf,
1161 (tdr_push_fn_t) tdr_push_sk_block,
1162 root.sk_offset, &sk);
1166 hbin_store_tdr_resize(regf,
1167 (tdr_push_fn_t) tdr_push_sk_block,
1168 private_data->offset, private_data->nk);
1172 static WERROR regf_get_sec_desc(TALLOC_CTX *ctx, const struct hive_key *key,
1173 struct security_descriptor **sd)
1175 const struct regf_key_data *private_data =
1176 (const struct regf_key_data *)key;
1178 struct regf_data *regf = private_data->hive;
1181 if (!hbin_get_tdr(regf, private_data->nk->sk_offset, ctx,
1182 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1183 DEBUG(0, ("Unable to find security descriptor\n"));
1184 return WERR_GENERAL_FAILURE;
1187 if (strcmp(sk.header, "sk") != 0) {
1188 DEBUG(0, ("Expected 'sk', got '%s'\n", sk.header));
1189 return WERR_GENERAL_FAILURE;
1192 *sd = talloc(ctx, struct security_descriptor);
1193 W_ERROR_HAVE_NO_MEMORY(*sd);
1195 data.data = sk.sec_desc;
1196 data.length = sk.rec_size;
1197 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_struct_blob(&data, ctx, NULL, *sd,
1198 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor))) {
1199 DEBUG(0, ("Error parsing security descriptor\n"));
1200 return WERR_GENERAL_FAILURE;
1206 static WERROR regf_sl_add_entry(struct regf_data *regf, uint32_t list_offset,
1208 uint32_t key_offset, uint32_t *ret)
1212 /* Create a new key if necessary */
1213 if (list_offset == -1) {
1214 if (regf->header->version.major != 1) {
1215 DEBUG(0, ("Can't store keys in unknown registry format\n"));
1216 return WERR_NOT_SUPPORTED;
1218 if (regf->header->version.minor < 3) {
1225 li.nk_offset = talloc_array(regf, uint32_t, 1);
1226 W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1227 li.nk_offset[0] = key_offset;
1229 *ret = hbin_store_tdr(regf,
1230 (tdr_push_fn_t) tdr_push_li_block,
1233 talloc_free(li.nk_offset);
1234 } else if (regf->header->version.minor == 3 ||
1235 regf->header->version.minor == 4) {
1242 lf.hr = talloc_array(regf, struct hash_record, 1);
1243 W_ERROR_HAVE_NO_MEMORY(lf.hr);
1244 lf.hr[0].nk_offset = key_offset;
1245 lf.hr[0].hash = talloc_strndup(lf.hr, name, 4);
1246 W_ERROR_HAVE_NO_MEMORY(lf.hr[0].hash);
1248 *ret = hbin_store_tdr(regf,
1249 (tdr_push_fn_t) tdr_push_lf_block,
1253 } else if (regf->header->version.minor == 5) {
1260 lh.hr = talloc_array(regf, struct lh_hash, 1);
1261 W_ERROR_HAVE_NO_MEMORY(lh.hr);
1262 lh.hr[0].nk_offset = key_offset;
1263 lh.hr[0].base37 = regf_create_lh_hash(name);
1265 *ret = hbin_store_tdr(regf,
1266 (tdr_push_fn_t) tdr_push_lh_block,
1274 data = hbin_get(regf, list_offset);
1276 DEBUG(0, ("Unable to find subkey list\n"));
1277 return WERR_BADFILE;
1280 if (!strncmp((char *)data.data, "li", 2)) {
1281 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
1286 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, regf, &li))) {
1287 DEBUG(0, ("Error parsing LI list\n"));
1289 return WERR_BADFILE;
1293 if (strncmp(li.header, "li", 2) != 0) {
1295 DEBUG(0, ("LI header corrupt\n"));
1296 return WERR_BADFILE;
1299 li.nk_offset = talloc_realloc(regf, li.nk_offset,
1300 uint32_t, li.key_count+1);
1301 W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1302 li.nk_offset[li.key_count] = key_offset;
1304 *ret = hbin_store_tdr_resize(regf,
1305 (tdr_push_fn_t)tdr_push_li_block,
1308 talloc_free(li.nk_offset);
1309 } else if (!strncmp((char *)data.data, "lf", 2)) {
1310 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
1315 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, regf, &lf))) {
1316 DEBUG(0, ("Error parsing LF list\n"));
1318 return WERR_BADFILE;
1321 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1323 lf.hr = talloc_realloc(regf, lf.hr, struct hash_record,
1325 W_ERROR_HAVE_NO_MEMORY(lf.hr);
1326 lf.hr[lf.key_count].nk_offset = key_offset;
1327 lf.hr[lf.key_count].hash = talloc_strndup(lf.hr, name, 4);
1328 W_ERROR_HAVE_NO_MEMORY(lf.hr[lf.key_count].hash);
1330 *ret = hbin_store_tdr_resize(regf,
1331 (tdr_push_fn_t)tdr_push_lf_block,
1335 } else if (!strncmp((char *)data.data, "lh", 2)) {
1336 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
1341 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, regf, &lh))) {
1342 DEBUG(0, ("Error parsing LH list\n"));
1344 return WERR_BADFILE;
1347 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1349 lh.hr = talloc_realloc(regf, lh.hr, struct lh_hash,
1351 W_ERROR_HAVE_NO_MEMORY(lh.hr);
1352 lh.hr[lh.key_count].nk_offset = key_offset;
1353 lh.hr[lh.key_count].base37 = regf_create_lh_hash(name);
1355 *ret = hbin_store_tdr_resize(regf,
1356 (tdr_push_fn_t)tdr_push_lh_block,
1360 } else if (!strncmp((char *)data.data, "ri", 2)) {
1362 DEBUG(0, ("Adding to 'ri' subkey list is not supported yet.\n"));
1363 return WERR_NOT_SUPPORTED;
1365 DEBUG(0, ("Cannot add to unknown subkey list\n"));
1366 return WERR_BADFILE;
1372 static WERROR regf_sl_del_entry(struct regf_data *regf, uint32_t list_offset,
1373 uint32_t key_offset, uint32_t *ret)
1377 data = hbin_get(regf, list_offset);
1379 DEBUG(0, ("Unable to find subkey list\n"));
1380 return WERR_BADFILE;
1383 if (strncmp((char *)data.data, "li", 2) == 0) {
1385 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
1387 bool found_offset = false;
1389 DEBUG(10, ("Subkeys in LI list\n"));
1393 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, regf, &li))) {
1394 DEBUG(0, ("Error parsing LI list\n"));
1396 return WERR_BADFILE;
1400 SMB_ASSERT(!strncmp(li.header, "li", 2));
1402 for (i = 0; i < li.key_count; i++) {
1404 li.nk_offset[i-1] = li.nk_offset[i];
1406 if (li.nk_offset[i] == key_offset) {
1407 found_offset = true;
1411 if (!found_offset) {
1412 DEBUG(2, ("Subkey not found\n"));
1413 return WERR_NOT_FOUND;
1417 /* If the there are no entries left, free the subkey list */
1418 if (li.key_count == 0) {
1419 hbin_free(regf, list_offset);
1423 /* Store li block */
1424 *ret = hbin_store_tdr_resize(regf,
1425 (tdr_push_fn_t) tdr_push_li_block,
1427 } else if (strncmp((char *)data.data, "lf", 2) == 0) {
1429 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
1431 bool found_offset = false;
1433 DEBUG(10, ("Subkeys in LF list\n"));
1437 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, regf, &lf))) {
1438 DEBUG(0, ("Error parsing LF list\n"));
1440 return WERR_BADFILE;
1444 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1446 for (i = 0; i < lf.key_count; i++) {
1448 lf.hr[i-1] = lf.hr[i];
1451 if (lf.hr[i].nk_offset == key_offset) {
1456 if (!found_offset) {
1457 DEBUG(2, ("Subkey not found\n"));
1458 return WERR_NOT_FOUND;
1462 /* If the there are no entries left, free the subkey list */
1463 if (lf.key_count == 0) {
1464 hbin_free(regf, list_offset);
1469 /* Store lf block */
1470 *ret = hbin_store_tdr_resize(regf,
1471 (tdr_push_fn_t) tdr_push_lf_block,
1473 } else if (strncmp((char *)data.data, "lh", 2) == 0) {
1475 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
1477 bool found_offset = false;
1479 DEBUG(10, ("Subkeys in LH list\n"));
1483 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, regf, &lh))) {
1484 DEBUG(0, ("Error parsing LF list\n"));
1486 return WERR_BADFILE;
1490 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1492 for (i = 0; i < lh.key_count; i++) {
1494 lh.hr[i-1] = lh.hr[i];
1497 if (lh.hr[i].nk_offset == key_offset) {
1502 if (!found_offset) {
1503 DEBUG(0, ("Subkey not found\n"));
1504 return WERR_NOT_FOUND;
1508 /* If the there are no entries left, free the subkey list */
1509 if (lh.key_count == 0) {
1510 hbin_free(regf, list_offset);
1515 /* Store lh block */
1516 *ret = hbin_store_tdr_resize(regf,
1517 (tdr_push_fn_t) tdr_push_lh_block,
1519 } else if (strncmp((char *)data.data, "ri", 2) == 0) {
1521 DEBUG(0, ("Sorry, deletion from ri block is not supported yet.\n"));
1522 return WERR_NOT_SUPPORTED;
1524 DEBUG (0, ("Unknown header found in subkey list.\n"));
1525 return WERR_BADFILE;
1530 static WERROR regf_del_value (struct hive_key *key, const char *name)
1532 struct regf_key_data *private_data = (struct regf_key_data *)key;
1533 struct regf_data *regf = private_data->hive;
1534 struct nk_block *nk = private_data->nk;
1537 bool found_offset = false;
1541 if (nk->values_offset == -1) {
1542 return WERR_NOT_FOUND;
1545 values = hbin_get(regf, nk->values_offset);
1547 for (i = 0; i < nk->num_values; i++) {
1549 ((uint32_t *)values.data)[i-1] = ((uint32_t *) values.data)[i];
1551 vk_offset = IVAL(values.data, i * 4);
1552 if (!hbin_get_tdr(regf, vk_offset, private_data,
1553 (tdr_pull_fn_t)tdr_pull_vk_block,
1555 DEBUG(0, ("Unable to get VK block at %d\n",
1557 return WERR_BADFILE;
1559 if (strcmp(vk.data_name, name) == 0) {
1560 hbin_free(regf, vk_offset);
1561 found_offset = true;
1565 if (!found_offset) {
1566 return WERR_NOT_FOUND;
1569 values.length = (nk->num_values)*4;
1572 /* Store values list and nk */
1573 if (nk->num_values == 0) {
1574 hbin_free(regf, nk->values_offset);
1575 nk->values_offset = -1;
1577 nk->values_offset = hbin_store_resize(regf,
1581 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
1582 private_data->offset, nk);
1584 return regf_save_hbin(private_data->hive);
1588 static WERROR regf_del_key(const struct hive_key *parent, const char *name)
1590 const struct regf_key_data *private_data =
1591 (const struct regf_key_data *)parent;
1592 struct regf_key_data *key;
1593 struct nk_block *parent_nk;
1596 SMB_ASSERT(private_data);
1598 parent_nk = private_data->nk;
1600 if (parent_nk->subkeys_offset == -1) {
1601 DEBUG(4, ("Subkey list is empty, this key cannot contain subkeys.\n"));
1602 return WERR_NOT_FOUND;
1606 if (!W_ERROR_IS_OK(regf_get_subkey_by_name(parent_nk, parent, name,
1607 (struct hive_key **)&key))) {
1608 DEBUG(2, ("Key '%s' not found\n", name));
1609 return WERR_NOT_FOUND;
1612 if (key->nk->subkeys_offset != -1 ||
1613 key->nk->values_offset != -1) {
1614 DEBUG(0, ("Key '%s' is not empty.\n", name));
1615 return WERR_FILE_EXISTS;
1618 /* Delete it from the subkey list. */
1619 error = regf_sl_del_entry(private_data->hive, parent_nk->subkeys_offset,
1620 key->offset, &parent_nk->subkeys_offset);
1621 if (!W_ERROR_IS_OK(error)) {
1622 DEBUG(0, ("Can't store new subkey list for parent key. Won't delete.\n"));
1626 /* Re-store parent key */
1627 parent_nk->num_subkeys--;
1628 hbin_store_tdr_resize(private_data->hive,
1629 (tdr_push_fn_t) tdr_push_nk_block,
1630 private_data->offset, parent_nk);
1632 if (key->nk->clsname_offset != -1) {
1633 hbin_free(private_data->hive, key->nk->clsname_offset);
1635 hbin_free(private_data->hive, key->offset);
1637 return regf_save_hbin(private_data->hive);
1640 static WERROR regf_add_key(TALLOC_CTX *ctx, const struct hive_key *parent,
1641 const char *name, const char *classname,
1642 struct security_descriptor *sec_desc,
1643 struct hive_key **ret)
1645 const struct regf_key_data *private_data =
1646 (const struct regf_key_data *)parent;
1647 struct nk_block *parent_nk = private_data->nk, nk;
1648 struct nk_block *root;
1649 struct regf_data *regf = private_data->hive;
1654 nk.type = REG_SUB_KEY;
1655 unix_to_nt_time(&nk.last_change, time(NULL));
1657 nk.parent_offset = private_data->offset;
1660 nk.subkeys_offset = -1;
1661 nk.unknown_offset = -1;
1663 nk.values_offset = -1;
1664 memset(nk.unk3, 0, 5);
1665 nk.clsname_offset = -1; /* FIXME: fill in */
1666 nk.clsname_length = 0;
1669 /* Get the security descriptor of the root key */
1670 root = talloc_zero(ctx, struct nk_block);
1671 W_ERROR_HAVE_NO_MEMORY(root);
1673 if (!hbin_get_tdr(regf, regf->header->data_offset, root,
1674 (tdr_pull_fn_t)tdr_pull_nk_block, root)) {
1675 DEBUG(0, ("Unable to find HBIN data for offset %d\n", offset));
1676 return WERR_GENERAL_FAILURE;
1678 nk.sk_offset = root->sk_offset;
1681 /* Store the new nk key */
1682 offset = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_nk_block, &nk);
1684 error = regf_sl_add_entry(regf, parent_nk->subkeys_offset, name, offset,
1685 &parent_nk->subkeys_offset);
1686 if (!W_ERROR_IS_OK(error)) {
1687 hbin_free(regf, offset);
1691 parent_nk->num_subkeys++;
1693 /* Since the subkey offset of the parent can change, store it again */
1694 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
1695 nk.parent_offset, parent_nk);
1697 *ret = (struct hive_key *)regf_get_key(ctx, regf, offset);
1699 return regf_save_hbin(private_data->hive);
1702 static WERROR regf_set_value(struct hive_key *key, const char *name,
1703 uint32_t type, const DATA_BLOB data)
1705 struct regf_key_data *private_data = (struct regf_key_data *)key;
1706 struct regf_data *regf = private_data->hive;
1707 struct nk_block *nk = private_data->nk;
1710 uint32_t tmp_vk_offset, vk_offset, old_vk_offset = -1;
1715 /* find the value offset, if it exists */
1716 if (nk->values_offset != -1) {
1717 values = hbin_get(regf, nk->values_offset);
1719 for (i = 0; i < nk->num_values; i++) {
1720 tmp_vk_offset = IVAL(values.data, i * 4);
1721 if (!hbin_get_tdr(regf, tmp_vk_offset, private_data,
1722 (tdr_pull_fn_t)tdr_pull_vk_block,
1724 DEBUG(0, ("Unable to get VK block at %d\n",
1726 return WERR_GENERAL_FAILURE;
1728 if (strcmp(vk.data_name, name) == 0) {
1729 old_vk_offset = tmp_vk_offset;
1733 /* Free data, if any */
1734 if (!(vk.data_length & 0x80000000)) {
1735 hbin_free(regf, vk.data_offset);
1738 if (old_vk_offset == -1) {
1740 vk.name_length = strlen(name);
1741 if (name != NULL && name[0] != 0) {
1743 vk.data_name = name;
1745 vk.data_name = NULL;
1749 /* Set the type and data */
1750 vk.data_length = data.length;
1751 vk.data_type = type;
1752 if (type == REG_DWORD) {
1753 vk.data_length |= 0x80000000;
1754 vk.data_offset = *(uint32_t *)data.data;
1756 /* Store data somewhere */
1757 vk.data_offset = hbin_store(regf, data);
1759 if (old_vk_offset == -1) {
1761 vk_offset = hbin_store_tdr(regf,
1762 (tdr_push_fn_t) tdr_push_vk_block,
1765 /* Store vk at offset */
1766 vk_offset = hbin_store_tdr_resize(regf,
1767 (tdr_push_fn_t) tdr_push_vk_block,
1768 old_vk_offset ,&vk);
1771 /* Re-allocate the value list */
1772 if (nk->values_offset == -1) {
1773 nk->values_offset = hbin_store_tdr(regf,
1774 (tdr_push_fn_t) tdr_push_uint32,
1779 /* Change if we're changing, otherwise we're adding the value */
1780 if (old_vk_offset != -1) {
1781 /* Find and overwrite the offset. */
1782 for (i = 0; i < nk->num_values; i++) {
1783 if (IVAL(values.data, i * 4) == old_vk_offset) {
1784 SIVAL(values.data, i * 4, vk_offset);
1789 /* Create a new value list */
1790 DATA_BLOB value_list;
1792 value_list.length = (nk->num_values+1)*4;
1793 value_list.data = (uint8_t *)talloc_array(private_data,
1796 W_ERROR_HAVE_NO_MEMORY(value_list.data);
1797 memcpy(value_list.data, values.data, nk->num_values * 4);
1799 SIVAL(value_list.data, nk->num_values * 4, vk_offset);
1801 nk->values_offset = hbin_store_resize(regf,
1807 hbin_store_tdr_resize(regf,
1808 (tdr_push_fn_t) tdr_push_nk_block,
1809 private_data->offset, nk);
1810 return regf_save_hbin(private_data->hive);
1813 static WERROR regf_save_hbin(struct regf_data *regf)
1815 struct tdr_push *push = tdr_push_init(regf, regf->iconv_convenience);
1818 W_ERROR_HAVE_NO_MEMORY(push);
1820 if (lseek(regf->fd, 0, SEEK_SET) == -1) {
1821 DEBUG(0, ("Error lseeking in regf file\n"));
1822 return WERR_GENERAL_FAILURE;
1825 /* Recompute checksum */
1826 if (NT_STATUS_IS_ERR(tdr_push_regf_hdr(push, regf->header))) {
1827 DEBUG(0, ("Failed to push regf header\n"));
1828 return WERR_GENERAL_FAILURE;
1830 regf->header->chksum = regf_hdr_checksum(push->data.data);
1833 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd, regf->iconv_convenience,
1834 (tdr_push_fn_t)tdr_push_regf_hdr,
1836 DEBUG(0, ("Error writing registry file header\n"));
1837 return WERR_GENERAL_FAILURE;
1840 if (lseek(regf->fd, 0x1000, SEEK_SET) == -1) {
1841 DEBUG(0, ("Error lseeking to 0x1000 in regf file\n"));
1842 return WERR_GENERAL_FAILURE;
1845 for (i = 0; regf->hbins[i]; i++) {
1846 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd, regf->iconv_convenience,
1847 (tdr_push_fn_t)tdr_push_hbin_block,
1849 DEBUG(0, ("Error writing HBIN block\n"));
1850 return WERR_GENERAL_FAILURE;
1857 WERROR reg_create_regf_file(TALLOC_CTX *parent_ctx, const char *location,
1858 int minor_version, struct hive_key **key)
1860 struct regf_data *regf;
1861 struct regf_hdr *regf_hdr;
1866 regf = (struct regf_data *)talloc_zero(NULL, struct regf_data);
1868 regf->iconv_convenience = lp_iconv_convenience(global_loadparm);
1870 W_ERROR_HAVE_NO_MEMORY(regf);
1872 DEBUG(5, ("Attempting to create registry file\n"));
1874 /* Get the header */
1875 regf->fd = creat(location, 0644);
1877 if (regf->fd == -1) {
1878 DEBUG(0,("Could not create file: %s, %s\n", location,
1881 return WERR_GENERAL_FAILURE;
1884 regf_hdr = talloc_zero(regf, struct regf_hdr);
1885 W_ERROR_HAVE_NO_MEMORY(regf_hdr);
1886 regf_hdr->REGF_ID = "regf";
1887 unix_to_nt_time(®f_hdr->modtime, time(NULL));
1888 regf_hdr->version.major = 1;
1889 regf_hdr->version.minor = minor_version;
1890 regf_hdr->last_block = 0x1000; /* Block size */
1891 regf_hdr->description = talloc_strdup(regf_hdr,
1892 "registry created by Samba 4");
1893 W_ERROR_HAVE_NO_MEMORY(regf_hdr->description);
1894 regf_hdr->chksum = 0;
1896 regf->header = regf_hdr;
1899 /* Create all hbin blocks */
1900 regf->hbins = talloc_array(regf, struct hbin_block *, 1);
1901 W_ERROR_HAVE_NO_MEMORY(regf->hbins);
1902 regf->hbins[0] = NULL;
1904 regf_hdr->data_offset = -1; /* FIXME */
1907 nk.type = REG_SUB_KEY;
1908 unix_to_nt_time(&nk.last_change, time(NULL));
1910 nk.parent_offset = -1;
1913 nk.subkeys_offset = -1;
1914 nk.unknown_offset = -1;
1916 nk.values_offset = -1;
1917 memset(nk.unk3, 0, 5);
1918 nk.clsname_offset = -1; /* FIXME: fill in */
1919 nk.clsname_length = 0;
1922 nk.sk_offset = -1; /* FIXME: fill in */
1924 /* Store the new nk key */
1925 regf->header->data_offset = hbin_store_tdr(regf,
1926 (tdr_push_fn_t)tdr_push_nk_block,
1929 *key = (struct hive_key *)regf_get_key(parent_ctx, regf,
1930 regf->header->data_offset);
1932 /* We can drop our own reference now that *key will have created one */
1935 error = regf_save_hbin(regf);
1936 if (!W_ERROR_IS_OK(error)) {
1943 WERROR reg_open_regf_file(TALLOC_CTX *parent_ctx, const char *location,
1944 struct loadparm_context *lp_ctx, struct hive_key **key)
1946 struct regf_data *regf;
1947 struct regf_hdr *regf_hdr;
1948 struct tdr_pull *pull;
1951 regf = (struct regf_data *)talloc_zero(NULL, struct regf_data);
1953 regf->iconv_convenience = lp_iconv_convenience(lp_ctx);
1955 W_ERROR_HAVE_NO_MEMORY(regf);
1957 DEBUG(5, ("Attempting to load registry file\n"));
1959 /* Get the header */
1960 regf->fd = open(location, O_RDWR);
1962 if (regf->fd == -1) {
1963 DEBUG(0,("Could not load file: %s, %s\n", location,
1966 return WERR_GENERAL_FAILURE;
1969 pull = tdr_pull_init(regf, regf->iconv_convenience);
1971 pull->data.data = (uint8_t*)fd_load(regf->fd, &pull->data.length, regf);
1973 if (pull->data.data == NULL) {
1974 DEBUG(0, ("Error reading data\n"));
1976 return WERR_GENERAL_FAILURE;
1979 regf_hdr = talloc(regf, struct regf_hdr);
1980 W_ERROR_HAVE_NO_MEMORY(regf_hdr);
1982 if (NT_STATUS_IS_ERR(tdr_pull_regf_hdr(pull, regf_hdr, regf_hdr))) {
1984 return WERR_GENERAL_FAILURE;
1987 regf->header = regf_hdr;
1989 if (strcmp(regf_hdr->REGF_ID, "regf") != 0) {
1990 DEBUG(0, ("Unrecognized NT registry header id: %s, %s\n",
1991 regf_hdr->REGF_ID, location));
1993 return WERR_GENERAL_FAILURE;
1996 /* Validate the header ... */
1997 if (regf_hdr_checksum(pull->data.data) != regf_hdr->chksum) {
1998 DEBUG(0, ("Registry file checksum error: %s: %d,%d\n",
1999 location, regf_hdr->chksum,
2000 regf_hdr_checksum(pull->data.data)));
2002 return WERR_GENERAL_FAILURE;
2005 pull->offset = 0x1000;
2008 /* Read in all hbin blocks */
2009 regf->hbins = talloc_array(regf, struct hbin_block *, 1);
2010 W_ERROR_HAVE_NO_MEMORY(regf->hbins);
2012 regf->hbins[0] = NULL;
2014 while (pull->offset < pull->data.length &&
2015 pull->offset <= regf->header->last_block) {
2016 struct hbin_block *hbin = talloc(regf->hbins,
2019 W_ERROR_HAVE_NO_MEMORY(hbin);
2021 if (NT_STATUS_IS_ERR(tdr_pull_hbin_block(pull, hbin, hbin))) {
2022 DEBUG(0, ("[%d] Error parsing HBIN block\n", i));
2027 if (strcmp(hbin->HBIN_ID, "hbin") != 0) {
2028 DEBUG(0, ("[%d] Expected 'hbin', got '%s'\n",
2034 regf->hbins[i] = hbin;
2036 regf->hbins = talloc_realloc(regf, regf->hbins,
2037 struct hbin_block *, i+2);
2038 regf->hbins[i] = NULL;
2043 DEBUG(1, ("%d HBIN blocks read\n", i));
2045 *key = (struct hive_key *)regf_get_key(parent_ctx, regf,
2046 regf->header->data_offset);
2048 /* We can drop our own reference now that *key will have created one */
2054 static struct hive_operations reg_backend_regf = {
2056 .get_key_info = regf_get_info,
2057 .enum_key = regf_get_subkey_by_index,
2058 .get_key_by_name = regf_get_subkey_by_name,
2059 .get_value_by_name = regf_get_value_by_name,
2060 .enum_value = regf_get_value,
2061 .get_sec_desc = regf_get_sec_desc,
2062 .set_sec_desc = regf_set_sec_desc,
2063 .add_key = regf_add_key,
2064 .set_value = regf_set_value,
2065 .del_key = regf_del_key,
2066 .delete_value = regf_del_value,