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"
28 static struct hive_operations reg_backend_regf;
31 * There are several places on the web where the REGF format is explained;
37 * - Return error codes that make more sense
39 * - do more things in-memory
43 * Read HBIN blocks into memory
48 struct hbin_block **hbins;
49 struct regf_hdr *header;
52 static WERROR regf_save_hbin(struct regf_data *data);
54 struct regf_key_data {
56 struct regf_data *hive;
61 static struct hbin_block *hbin_by_offset(const struct regf_data *data,
62 uint32_t offset, uint32_t *rel_offset)
66 for (i = 0; data->hbins[i]; i++) {
67 if (offset >= data->hbins[i]->offset_from_first &&
68 offset < data->hbins[i]->offset_from_first+
69 data->hbins[i]->offset_to_next) {
70 if (rel_offset != NULL)
71 *rel_offset = offset - data->hbins[i]->offset_from_first - 0x20;
72 return data->hbins[i];
80 * Validate a regf header
81 * For now, do nothing, but we should check the checksum
83 static uint32_t regf_hdr_checksum(const uint8_t *buffer)
85 uint32_t checksum = 0, x;
88 for (i = 0; i < 0x01FB; i+= 4) {
97 * Obtain the contents of a HBIN block
99 static DATA_BLOB hbin_get(const struct regf_data *data, uint32_t offset)
102 struct hbin_block *hbin;
108 hbin = hbin_by_offset(data, offset, &rel_offset);
111 DEBUG(1, ("Can't find HBIN containing 0x%04x\n", offset));
115 ret.length = IVAL(hbin->data, rel_offset);
116 if (!(ret.length & 0x80000000)) {
117 DEBUG(0, ("Trying to use dirty block at 0x%04x\n", offset));
121 /* remove high bit */
122 ret.length = (ret.length ^ 0xffffffff) + 1;
124 ret.length -= 4; /* 4 bytes for the length... */
125 ret.data = hbin->data +
126 (offset - hbin->offset_from_first - 0x20) + 4;
131 static bool hbin_get_tdr (struct regf_data *regf, uint32_t offset,
132 TALLOC_CTX *ctx, tdr_pull_fn_t pull_fn, void *p)
134 struct tdr_pull pull;
138 pull.data = hbin_get(regf, offset);
139 if (!pull.data.data) {
140 DEBUG(1, ("Unable to get data at 0x%04x\n", offset));
144 if (NT_STATUS_IS_ERR(pull_fn(&pull, ctx, p))) {
145 DEBUG(1, ("Error parsing record at 0x%04x using tdr\n", offset));
152 /* Allocate some new data */
153 static DATA_BLOB hbin_alloc(struct regf_data *data, uint32_t size,
157 uint32_t rel_offset = -1; /* Relative offset ! */
158 struct hbin_block *hbin = NULL;
164 return data_blob(NULL, 0);
166 size += 4; /* Need to include int32 for the length */
168 /* Allocate as a multiple of 8 */
169 size = (size + 7) & ~7;
174 for (i = 0; (hbin = data->hbins[i]); i++) {
177 for (j = 0; j < hbin->offset_to_next-0x20; j+= my_size) {
178 my_size = IVALS(hbin->data, j);
180 if (my_size == 0x0) {
181 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
185 if (my_size % 8 != 0) {
186 DEBUG(0, ("Encountered non-aligned block!\n"));
189 if (my_size < 0) { /* Used... */
191 } else if (my_size == size) { /* exact match */
193 DEBUG(4, ("Found free block of exact size %d in middle of HBIN\n", size));
195 } else if (my_size > size) { /* data will remain */
197 /* Split this block and mark the next block as free */
198 SIVAL(hbin->data, rel_offset+size, my_size-size);
199 DEBUG(4, ("Found free block of size %d (needing %d) in middle of HBIN\n", my_size, size));
204 if (rel_offset != -1)
208 /* No space available in previous hbins,
209 * allocate new one */
210 if (data->hbins[i] == NULL) {
211 DEBUG(4, ("No space available in other HBINs for block of size %d, allocating new HBIN\n", size));
212 data->hbins = talloc_realloc(data, data->hbins, struct hbin_block *, i+2);
213 hbin = talloc(data->hbins, struct hbin_block);
214 SMB_ASSERT(hbin != NULL);
216 data->hbins[i] = hbin;
217 data->hbins[i+1] = NULL;
219 hbin->HBIN_ID = talloc_strdup(hbin, "hbin");
220 hbin->offset_from_first = (i == 0?0:data->hbins[i-1]->offset_from_first+data->hbins[i-1]->offset_to_next);
221 hbin->offset_to_next = 0x1000;
222 hbin->unknown[0] = 0;
223 hbin->unknown[0] = 0;
224 unix_to_nt_time(&hbin->last_change, time(NULL));
225 hbin->block_size = hbin->offset_to_next;
226 hbin->data = talloc_zero_array(hbin, uint8_t, hbin->block_size - 0x20);
229 SIVAL(hbin->data, size, hbin->block_size - size - 0x20);
232 /* Set size and mark as used */
233 SIVAL(hbin->data, rel_offset, -size);
235 ret.data = hbin->data + rel_offset + 0x4; /* Skip past length */
236 ret.length = size - 0x4;
238 uint32_t new_rel_offset;
239 *offset = hbin->offset_from_first + rel_offset + 0x20;
240 SMB_ASSERT(hbin_by_offset(data, *offset, &new_rel_offset) == hbin);
241 SMB_ASSERT(new_rel_offset == rel_offset);
247 /* Store a data blob. Return the offset at which it was stored */
248 static uint32_t hbin_store (struct regf_data *data, DATA_BLOB blob)
251 DATA_BLOB dest = hbin_alloc(data, blob.length, &ret);
253 memcpy(dest.data, blob.data, blob.length);
258 static uint32_t hbin_store_tdr (struct regf_data *data, tdr_push_fn_t push_fn, void *p)
260 struct tdr_push *push = talloc_zero(data, struct tdr_push);
263 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
264 DEBUG(0, ("Error during push\n"));
268 ret = hbin_store(data, push->data);
276 /* Free existing data */
277 static void hbin_free (struct regf_data *data, uint32_t offset)
282 struct hbin_block *hbin;
284 SMB_ASSERT (offset > 0);
286 hbin = hbin_by_offset(data, offset, &rel_offset);
291 /* Get original size */
292 size = IVALS(hbin->data, rel_offset);
295 DEBUG(1, ("Trying to free already freed block at 0x%04x\n", offset));
301 /* If the next block is free, merge into big free block */
302 if (rel_offset + size < hbin->offset_to_next) {
303 next_size = IVALS(hbin->data, rel_offset+size);
309 /* Write block size */
310 SIVALS(hbin->data, rel_offset, size);
314 * Store a data blob data was already stored, but has changed in size
315 * Will try to save it at the current location if possible, otherwise
316 * does a free + store */
317 static uint32_t hbin_store_resize(struct regf_data *data,
318 uint32_t orig_offset, DATA_BLOB blob)
321 struct hbin_block *hbin = hbin_by_offset(data, orig_offset, &rel_offset);
325 int32_t possible_size;
328 SMB_ASSERT(orig_offset > 0);
331 return hbin_store(data, blob);
333 /* Get original size */
334 orig_size = -IVALS(hbin->data, rel_offset);
336 needed_size = blob.length + 4; /* Add int32 containing length */
337 needed_size = (needed_size + 7) & ~7; /* Align */
339 /* Fits into current allocated block */
340 if (orig_size >= needed_size) {
341 memcpy(hbin->data + rel_offset + 0x4, blob.data, blob.length);
342 /* If the difference in size is greater than 0x4, split the block
343 * and free/merge it */
344 if (orig_size - needed_size > 0x4) {
345 SIVALS(hbin->data, rel_offset, -needed_size);
346 SIVALS(hbin->data, rel_offset + needed_size, needed_size-orig_size);
347 hbin_free(data, orig_offset + needed_size);
352 possible_size = orig_size;
354 /* Check if it can be combined with the next few free records */
355 for (i = rel_offset; i < hbin->offset_to_next - 0x20; i += my_size) {
356 if (IVALS(hbin->data, i) < 0) /* Used */
359 my_size = IVALS(hbin->data, i);
361 if (my_size == 0x0) {
362 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
365 possible_size += my_size;
368 if (possible_size >= blob.length) {
369 SIVAL(hbin->data, rel_offset, -possible_size);
370 memcpy(hbin->data + rel_offset + 0x4, blob.data, blob.length);
375 hbin_free(data, orig_offset);
376 return hbin_store(data, blob);
379 static uint32_t hbin_store_tdr_resize (struct regf_data *regf, tdr_push_fn_t push_fn,
380 uint32_t orig_offset, void *p)
382 struct tdr_push *push = talloc_zero(regf, struct tdr_push);
385 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
386 DEBUG(0, ("Error during push\n"));
390 ret = hbin_store_resize(regf, orig_offset, push->data);
397 static uint32_t regf_create_lh_hash(const char *name)
403 hash_name = strupper_talloc(NULL, name);
404 for (i = 0; *(hash_name + i) != 0; i++) {
406 ret += *(hash_name + i);
408 talloc_free(hash_name);
412 static WERROR regf_get_info (TALLOC_CTX *mem_ctx,
413 const struct hive_key *key,
414 const char **classname,
415 uint32_t *num_subkeys,
416 uint32_t *num_values,
417 NTTIME *last_mod_time)
419 const struct regf_key_data *private_data =
420 (const struct regf_key_data *)key;
422 if (num_subkeys != NULL)
423 *num_subkeys = private_data->nk->num_subkeys;
425 if (num_values != NULL)
426 *num_values = private_data->nk->num_values;
428 if (classname != NULL) {
429 if (private_data->nk->clsname_offset != -1) {
430 DATA_BLOB data = hbin_get(private_data->hive,
431 private_data->nk->clsname_offset);
432 *classname = talloc_strndup(mem_ctx,
433 (char*)data.data, private_data->nk->clsname_length);
438 /* TODO: Last mod time */
443 static struct regf_key_data *regf_get_key(TALLOC_CTX *ctx,
444 struct regf_data *regf,
448 struct regf_key_data *ret;
450 ret = talloc_zero(ctx, struct regf_key_data);
451 ret->key.ops = ®_backend_regf;
452 ret->hive = talloc_reference(ret, regf);
453 ret->offset = offset;
454 nk = talloc(ret, struct nk_block);
460 if (!hbin_get_tdr(regf, offset, nk, (tdr_pull_fn_t)tdr_pull_nk_block, nk)) {
461 DEBUG(0, ("Unable to find HBIN data for offset %d\n", offset));
465 if (strcmp(nk->header, "nk") != 0) {
466 DEBUG(0, ("Expected nk record, got %s\n", nk->header));
475 static WERROR regf_get_value(TALLOC_CTX *ctx, const struct hive_key *key,
476 int idx, const char **name,
477 uint32_t *data_type, DATA_BLOB *data)
479 const struct regf_key_data *private_data =
480 (const struct regf_key_data *)key;
482 struct regf_data *regf = private_data->hive;
486 if (idx >= private_data->nk->num_values)
487 return WERR_NO_MORE_ITEMS;
489 tmp = hbin_get(regf, private_data->nk->values_offset);
491 DEBUG(0, ("Unable to find value list\n"));
492 return WERR_GENERAL_FAILURE;
495 if (tmp.length < private_data->nk->num_values * 4) {
496 DEBUG(1, ("Value counts mismatch\n"));
499 vk_offset = IVAL(tmp.data, idx * 4);
501 vk = talloc(NULL, struct vk_block);
502 W_ERROR_HAVE_NO_MEMORY(vk);
504 if (!hbin_get_tdr(regf, vk_offset, vk, (tdr_pull_fn_t)tdr_pull_vk_block, vk)) {
505 DEBUG(0, ("Unable to get VK block at %d\n", vk_offset));
507 return WERR_GENERAL_FAILURE;
510 /* FIXME: name character set ?*/
512 *name = talloc_strndup(ctx, vk->data_name, vk->name_length);
514 if (data_type != NULL)
515 *data_type = vk->data_type;
517 if (vk->data_length & 0x80000000) {
518 vk->data_length &=~0x80000000;
519 data->data = (uint8_t *)&vk->data_offset;
520 data->length = vk->data_length;
522 *data = hbin_get(regf, vk->data_offset);
525 if (data->length < vk->data_length) {
526 DEBUG(1, ("Read data less than indicated data length!\n"));
534 static WERROR regf_get_value_by_name (TALLOC_CTX *mem_ctx,
535 struct hive_key *key, const char *name,
536 uint32_t *type, DATA_BLOB *data)
542 /* FIXME: Do binary search? Is this list sorted at all? */
544 for (i = 0; W_ERROR_IS_OK(error = regf_get_value(mem_ctx, key, i,
545 &vname, type, data)); i++) {
546 if (!strcmp(vname, name))
550 if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
551 return WERR_NOT_FOUND;
557 static WERROR regf_get_subkey_by_index (TALLOC_CTX *ctx,
558 const struct hive_key *key,
559 uint32_t idx, const char **name,
560 const char **classname,
561 NTTIME *last_mod_time)
564 struct regf_key_data *ret;
565 const struct regf_key_data *private_data = (const struct regf_key_data *)key;
566 struct nk_block *nk = private_data->nk;
569 if (idx >= nk->num_subkeys)
570 return WERR_NO_MORE_ITEMS;
572 data = hbin_get(private_data->hive, nk->subkeys_offset);
574 DEBUG(0, ("Unable to find subkey list\n"));
575 return WERR_GENERAL_FAILURE;
578 if (!strncmp((char *)data.data, "li", 2)) {
580 struct tdr_pull pull;
582 DEBUG(10, ("Subkeys in LI list\n"));
586 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, nk, &li))) {
587 DEBUG(0, ("Error parsing LI list\n"));
588 return WERR_GENERAL_FAILURE;
590 SMB_ASSERT(!strncmp(li.header, "li", 2));
592 if (li.key_count != nk->num_subkeys) {
593 DEBUG(0, ("Subkey counts don't match\n"));
594 return WERR_GENERAL_FAILURE;
596 key_off = li.nk_offset[idx];
598 } else if (!strncmp((char *)data.data, "lf", 2)) {
600 struct tdr_pull pull;
602 DEBUG(10, ("Subkeys in LF list\n"));
606 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(&pull, nk, &lf))) {
607 DEBUG(0, ("Error parsing LF list\n"));
608 return WERR_GENERAL_FAILURE;
610 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
612 if (lf.key_count != nk->num_subkeys) {
613 DEBUG(0, ("Subkey counts don't match\n"));
614 return WERR_GENERAL_FAILURE;
617 key_off = lf.hr[idx].nk_offset;
618 } else if (!strncmp((char *)data.data, "lh", 2)) {
620 struct tdr_pull pull;
622 DEBUG(10, ("Subkeys in LH list\n"));
626 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, nk, &lh))) {
627 DEBUG(0, ("Error parsing LH list\n"));
628 return WERR_GENERAL_FAILURE;
630 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
632 if (lh.key_count != nk->num_subkeys) {
633 DEBUG(0, ("Subkey counts don't match\n"));
634 return WERR_GENERAL_FAILURE;
636 key_off = lh.hr[idx].nk_offset;
637 } else if (!strncmp((char *)data.data, "ri", 2)) {
639 struct tdr_pull pull;
641 uint16_t sublist_count = 0;
643 DEBUG(10, ("Subkeys in RI list\n"));
647 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(&pull, nk, &ri))) {
648 DEBUG(0, ("Error parsing RI list\n"));
649 return WERR_GENERAL_FAILURE;
651 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
653 for (i = 0; i < ri.key_count; i++) {
656 /* Get sublist data blob */
657 list_data = hbin_get(private_data->hive, ri.offset[i]);
658 if (!list_data.data) {
659 DEBUG(0, ("Error getting RI list."));
660 return WERR_GENERAL_FAILURE;
664 pull.data = list_data;
666 if (!strncmp((char *)list_data.data, "li", 2)) {
669 DEBUG(10, ("Subkeys in RI->LI list\n"));
671 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, nk, &li))) {
672 DEBUG(0, ("Error parsing LI list from RI\n"));
673 return WERR_GENERAL_FAILURE;
675 SMB_ASSERT(!strncmp(li.header, "li", 2));
677 /* Advance to next sublist if necessary */
678 if (idx >= sublist_count + li.key_count) {
679 sublist_count += li.key_count;
682 key_off = li.nk_offset[idx - sublist_count];
683 sublist_count += li.key_count;
685 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
688 DEBUG(10, ("Subkeys in RI->LH list\n"));
690 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, nk, &lh))) {
691 DEBUG(0, ("Error parsing LH list from RI\n"));
692 return WERR_GENERAL_FAILURE;
694 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
696 /* Advance to next sublist if necessary */
697 if (idx >= sublist_count + lh.key_count) {
698 sublist_count += lh.key_count;
701 key_off = lh.hr[idx - sublist_count].nk_offset;
702 sublist_count += lh.key_count;
705 DEBUG(0,("Unknown sublist in ri block\n"));
707 return WERR_GENERAL_FAILURE;
712 if (idx > sublist_count) {
713 return WERR_NO_MORE_ITEMS;
717 DEBUG(0, ("Unknown type for subkey list (0x%04x): %c%c\n",
718 nk->subkeys_offset, data.data[0], data.data[1]));
719 return WERR_GENERAL_FAILURE;
722 ret = regf_get_key (ctx, private_data->hive, key_off);
724 if (classname != NULL) {
725 if (ret->nk->clsname_offset != -1) {
726 DATA_BLOB db = hbin_get(ret->hive,
727 ret->nk->clsname_offset);
728 *classname = talloc_strndup(ctx,
729 (char*)db.data, ret->nk->clsname_length);
734 if (last_mod_time != NULL)
735 *last_mod_time = ret->nk->last_change;
738 *name = talloc_steal(ctx, ret->nk->key_name);
745 static WERROR regf_match_subkey_by_name(TALLOC_CTX *ctx,
746 const struct hive_key *key, uint32_t offset,
747 const char *name, uint32_t *ret)
749 DATA_BLOB subkey_data;
750 struct nk_block subkey;
751 struct tdr_pull pull;
752 const struct regf_key_data *private_data =
753 (const struct regf_key_data *)key;
755 subkey_data = hbin_get(private_data->hive, offset);
756 if (!subkey_data.data) {
757 DEBUG(0, ("Unable to retrieve subkey HBIN\n"));
758 return WERR_GENERAL_FAILURE;
762 pull.data = subkey_data;
764 if (NT_STATUS_IS_ERR(tdr_pull_nk_block(&pull, ctx, &subkey))) {
765 DEBUG(0, ("Error parsing NK structure.\n"));
766 return WERR_GENERAL_FAILURE;
769 if (strncmp(subkey.header, "nk", 2)) {
770 DEBUG(0, ("Not an NK structure.\n"));
771 return WERR_GENERAL_FAILURE;
774 if (!strcasecmp(subkey.key_name, name)) {
782 static WERROR regf_get_subkey_by_name(TALLOC_CTX *ctx,
783 const struct hive_key *key,
785 struct hive_key **ret)
788 const struct regf_key_data *private_data =
789 (const struct regf_key_data *)key;
790 struct nk_block *nk = private_data->nk;
791 uint32_t key_off = 0;
793 data = hbin_get(private_data->hive, nk->subkeys_offset);
795 DEBUG(0, ("Unable to find subkey list\n"));
796 return WERR_GENERAL_FAILURE;
799 if (!strncmp((char *)data.data, "li", 2)) {
801 struct tdr_pull pull;
804 DEBUG(10, ("Subkeys in LI list\n"));
808 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, nk, &li))) {
809 DEBUG(0, ("Error parsing LI list\n"));
810 return WERR_GENERAL_FAILURE;
812 SMB_ASSERT(!strncmp(li.header, "li", 2));
814 if (li.key_count != nk->num_subkeys) {
815 DEBUG(0, ("Subkey counts don't match\n"));
816 return WERR_GENERAL_FAILURE;
819 for (i = 0; i < li.key_count; i++) {
820 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key, li.nk_offset[i], name, &key_off));
825 return WERR_NOT_FOUND;
826 } else if (!strncmp((char *)data.data, "lf", 2)) {
828 struct tdr_pull pull;
831 DEBUG(10, ("Subkeys in LF list\n"));
835 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(&pull, nk, &lf))) {
836 DEBUG(0, ("Error parsing LF list\n"));
837 return WERR_GENERAL_FAILURE;
839 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
841 if (lf.key_count != nk->num_subkeys) {
842 DEBUG(0, ("Subkey counts don't match\n"));
843 return WERR_GENERAL_FAILURE;
846 for (i = 0; i < lf.key_count; i++) {
847 if (strncmp(lf.hr[i].hash, name, 4)) {
850 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key, lf.hr[i].nk_offset, name, &key_off));
855 return WERR_NOT_FOUND;
856 } else if (!strncmp((char *)data.data, "lh", 2)) {
858 struct tdr_pull pull;
862 DEBUG(10, ("Subkeys in LH list\n"));
866 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, nk, &lh))) {
867 DEBUG(0, ("Error parsing LH list\n"));
868 return WERR_GENERAL_FAILURE;
870 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
872 if (lh.key_count != nk->num_subkeys) {
873 DEBUG(0, ("Subkey counts don't match\n"));
874 return WERR_GENERAL_FAILURE;
877 hash = regf_create_lh_hash(name);
878 for (i = 0; i < lh.key_count; i++) {
879 if (lh.hr[i].base37 != hash) {
882 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key, lh.hr[i].nk_offset, name, &key_off));
887 return WERR_NOT_FOUND;
888 } else if (!strncmp((char *)data.data, "ri", 2)) {
890 struct tdr_pull pull;
893 DEBUG(10, ("Subkeys in RI list\n"));
897 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(&pull, nk, &ri))) {
898 DEBUG(0, ("Error parsing RI list\n"));
899 return WERR_GENERAL_FAILURE;
901 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
903 for (i = 0; i < ri.key_count; i++) {
906 /* Get sublist data blob */
907 list_data = hbin_get(private_data->hive, ri.offset[i]);
908 if (list_data.data == NULL) {
909 DEBUG(0, ("Error getting RI list."));
910 return WERR_GENERAL_FAILURE;
914 pull.data = list_data;
916 if (!strncmp((char *)list_data.data, "li", 2)) {
919 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, nk, &li))) {
920 DEBUG(0, ("Error parsing LI list from RI\n"));
921 return WERR_GENERAL_FAILURE;
923 SMB_ASSERT(!strncmp(li.header, "li", 2));
925 for (j = 0; j < li.key_count; j++) {
926 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
927 li.nk_offset[j], name, &key_off));
931 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
935 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, nk, &lh))) {
936 DEBUG(0, ("Error parsing LH list from RI\n"));
937 return WERR_GENERAL_FAILURE;
939 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
941 hash = regf_create_lh_hash(name);
942 for (j = 0; j < lh.key_count; j++) {
943 if (lh.hr[j].base37 != hash) {
946 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
947 lh.hr[j].nk_offset, name, &key_off));
956 return WERR_NOT_FOUND;
958 DEBUG(0, ("Unknown subkey list type.\n"));
959 return WERR_GENERAL_FAILURE;
962 *ret = (struct hive_key *)regf_get_key (ctx, private_data->hive, key_off);
966 static WERROR regf_set_sec_desc (struct hive_key *key,
967 const struct security_descriptor *sec_desc)
969 const struct regf_key_data *private_data =
970 (const struct regf_key_data *)key;
971 struct sk_block cur_sk, sk, new_sk;
972 struct regf_data *regf = private_data->hive;
973 struct nk_block root;
975 uint32_t sk_offset, cur_sk_offset;
976 bool update_cur_sk = false;
978 /* Get the root nk */
979 hbin_get_tdr(regf, regf->header->data_offset, regf,
980 (tdr_pull_fn_t) tdr_pull_nk_block, &root);
982 /* Push the security descriptor to a blob */
983 if (NT_STATUS_IS_ERR(ndr_push_struct_blob(&data, regf, sec_desc,
984 (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
985 DEBUG(0, ("Unable to push security descriptor\n"));
986 return WERR_GENERAL_FAILURE;
989 /* Get the current security descriptor for the key */
990 if (!hbin_get_tdr(regf, private_data->nk->sk_offset, regf,
991 (tdr_pull_fn_t) tdr_pull_sk_block, &cur_sk)) {
992 DEBUG(0, ("Unable to find security descriptor for current key\n"));
995 /* If there's no change, change nothing. */
996 if (memcmp(data.data, cur_sk.sec_desc, MIN(data.length, cur_sk.rec_size)) == 0) {
1000 /* Delete the current sk if only this key is using it */
1001 if (cur_sk.ref_cnt == 1) {
1002 /* Get the previous security descriptor for the key */
1003 if (!hbin_get_tdr(regf, cur_sk.prev_offset, regf,
1004 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1005 DEBUG(0, ("Unable to find prev security descriptor for current key\n"));
1006 return WERR_BADFILE;
1008 /* Change and store the previous security descriptor */
1009 sk.next_offset = cur_sk.next_offset;
1010 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block, cur_sk.prev_offset, &sk);
1012 /* Get the next security descriptor for the key */
1013 if (!hbin_get_tdr(regf, cur_sk.next_offset, regf, (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1014 DEBUG(0, ("Unable to find next security descriptor for current key\n"));
1015 return WERR_BADFILE;
1017 /* Change and store the next security descriptor */
1018 sk.prev_offset = cur_sk.prev_offset;
1019 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block, cur_sk.next_offset, &sk);
1021 hbin_free(regf, private_data->nk->sk_offset);
1023 /* This key will no longer be referring to this sk */
1025 update_cur_sk = true;
1028 sk_offset = root.sk_offset;
1031 cur_sk_offset = sk_offset;
1032 if (!hbin_get_tdr(regf, sk_offset, regf, (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1033 DEBUG(0, ("Unable to find security descriptor\n"));
1034 return WERR_BADFILE;
1036 if (memcmp(data.data, sk.sec_desc, MIN(data.length, sk.rec_size)) == 0) {
1037 private_data->nk->sk_offset = sk_offset;
1039 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block, sk_offset, &sk);
1040 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block, private_data->offset, private_data->nk);
1043 sk_offset = sk.next_offset;
1044 } while (sk_offset != root.sk_offset);
1046 ZERO_STRUCT(new_sk);
1047 new_sk.header = "sk";
1048 new_sk.prev_offset = cur_sk_offset;
1049 new_sk.next_offset = root.sk_offset;
1051 new_sk.rec_size = data.length;
1052 new_sk.sec_desc = data.data;
1054 sk_offset = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_sk_block, &new_sk);
1055 if (sk_offset == -1) {
1056 DEBUG(0, ("Error storing sk block\n"));
1057 return WERR_GENERAL_FAILURE;
1059 private_data->nk->sk_offset = sk_offset;
1061 if (update_cur_sk) {
1062 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block, private_data->nk->sk_offset, &cur_sk);
1065 /* Get the previous security descriptor for the key */
1066 if (!hbin_get_tdr(regf, new_sk.prev_offset, regf, (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1067 DEBUG(0, ("Unable to find security descriptor for previous key\n"));
1068 return WERR_BADFILE;
1070 /* Change and store the previous security descriptor */
1071 sk.next_offset = sk_offset;
1072 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block, cur_sk.prev_offset, &sk);
1074 /* Get the next security descriptor for the key (always root, as we append) */
1075 if (!hbin_get_tdr(regf, new_sk.next_offset, regf, (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1076 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1077 return WERR_BADFILE;
1079 /* Change and store the next security descriptor (always root, as we append) */
1080 sk.prev_offset = sk_offset;
1081 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block, root.sk_offset, &sk);
1085 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block, private_data->offset, private_data->nk);
1089 static WERROR regf_get_sec_desc(TALLOC_CTX *ctx, const struct hive_key *key,
1090 struct security_descriptor **sd)
1092 const struct regf_key_data *private_data =
1093 (const struct regf_key_data *)key;
1095 struct regf_data *regf = private_data->hive;
1098 if (!hbin_get_tdr(regf, private_data->nk->sk_offset, ctx, (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1099 DEBUG(0, ("Unable to find security descriptor\n"));
1100 return WERR_GENERAL_FAILURE;
1103 if (strcmp(sk.header, "sk") != 0) {
1104 DEBUG(0, ("Expected 'sk', got '%s'\n", sk.header));
1105 return WERR_GENERAL_FAILURE;
1108 *sd = talloc(ctx, struct security_descriptor);
1109 W_ERROR_HAVE_NO_MEMORY(*sd);
1111 data.data = sk.sec_desc;
1112 data.length = sk.rec_size;
1113 if (NT_STATUS_IS_ERR(ndr_pull_struct_blob(&data, ctx, *sd, (ndr_pull_flags_fn_t)ndr_pull_security_descriptor))) {
1114 DEBUG(0, ("Error parsing security descriptor\n"));
1115 return WERR_GENERAL_FAILURE;
1121 static WERROR regf_sl_add_entry(struct regf_data *regf, uint32_t list_offset,
1122 const char *name, uint32_t key_offset, uint32_t *ret)
1126 /* Create a new key if necessary */
1127 if (list_offset == -1) {
1128 if (regf->header->version.major != 1) {
1129 DEBUG(0, ("Can't store keys in unknown registry format\n"));
1130 return WERR_NOT_SUPPORTED;
1132 if (regf->header->version.minor < 3) {
1139 li.nk_offset = talloc_array(regf, uint32_t, 1);
1140 W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1141 li.nk_offset[0] = key_offset;
1143 *ret = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_li_block, &li);
1145 talloc_free(li.nk_offset);
1146 } else if (regf->header->version.minor == 3 || regf->header->version.minor == 4) {
1153 lf.hr = talloc_array(regf, struct hash_record, 1);
1154 W_ERROR_HAVE_NO_MEMORY(lf.hr);
1155 lf.hr[0].nk_offset = key_offset;
1156 lf.hr[0].hash = talloc_strndup(lf.hr, name, 4);
1157 W_ERROR_HAVE_NO_MEMORY(lf.hr[0].hash);
1159 *ret = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_lf_block, &lf);
1162 } else if (regf->header->version.minor == 5) {
1169 lh.hr = talloc_array(regf, struct lh_hash, 1);
1170 W_ERROR_HAVE_NO_MEMORY(lh.hr);
1171 lh.hr[0].nk_offset = key_offset;
1172 lh.hr[0].base37 = regf_create_lh_hash(name);
1174 *ret = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_lh_block, &lh);
1181 data = hbin_get(regf, list_offset);
1183 DEBUG(0, ("Unable to find subkey list\n"));
1184 return WERR_BADFILE;
1187 if (!strncmp((char *)data.data, "li", 2)) {
1188 struct tdr_pull pull;
1194 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, regf, &li))) {
1195 DEBUG(0, ("Error parsing LI list\n"));
1196 return WERR_BADFILE;
1199 if (strncmp(li.header, "li", 2) != 0) {
1201 DEBUG(0, ("LI header corrupt\n"));
1202 return WERR_BADFILE;
1205 li.nk_offset = talloc_realloc(regf, li.nk_offset, uint32_t, li.key_count+1);
1206 W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1207 li.nk_offset[li.key_count] = key_offset;
1209 *ret = hbin_store_tdr_resize(regf, (tdr_push_fn_t)tdr_push_li_block, list_offset, &li);
1211 talloc_free(li.nk_offset);
1212 } else if (!strncmp((char *)data.data, "lf", 2)) {
1213 struct tdr_pull pull;
1219 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(&pull, regf, &lf))) {
1220 DEBUG(0, ("Error parsing LF list\n"));
1221 return WERR_BADFILE;
1223 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1225 lf.hr = talloc_realloc(regf, lf.hr, struct hash_record, lf.key_count+1);
1226 W_ERROR_HAVE_NO_MEMORY(lf.hr);
1227 lf.hr[lf.key_count].nk_offset = key_offset;
1228 lf.hr[lf.key_count].hash = talloc_strndup(lf.hr, name, 4);
1229 W_ERROR_HAVE_NO_MEMORY(lf.hr[lf.key_count].hash);
1231 *ret = hbin_store_tdr_resize(regf, (tdr_push_fn_t)tdr_push_lf_block, list_offset, &lf);
1234 } else if (!strncmp((char *)data.data, "lh", 2)) {
1235 struct tdr_pull pull;
1241 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, regf, &lh))) {
1242 DEBUG(0, ("Error parsing LH list\n"));
1243 return WERR_BADFILE;
1245 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1247 lh.hr = talloc_realloc(regf, lh.hr, struct lh_hash, lh.key_count+1);
1248 W_ERROR_HAVE_NO_MEMORY(lh.hr);
1249 lh.hr[lh.key_count].nk_offset = key_offset;
1250 lh.hr[lh.key_count].base37 = regf_create_lh_hash(name);
1252 *ret = hbin_store_tdr_resize(regf, (tdr_push_fn_t)tdr_push_lh_block, list_offset, &lh);
1255 } else if (!strncmp((char *)data.data, "ri", 2)) {
1257 DEBUG(0, ("Adding to 'ri' subkey list is not supported yet.\n"));
1258 return WERR_NOT_SUPPORTED;
1260 DEBUG(0, ("Cannot add to unknown subkey list\n"));
1261 return WERR_BADFILE;
1267 static WERROR regf_sl_del_entry(struct regf_data *regf, uint32_t list_offset,
1268 uint32_t key_offset, uint32_t *ret)
1272 data = hbin_get(regf, list_offset);
1274 DEBUG(0, ("Unable to find subkey list\n"));
1275 return WERR_BADFILE;
1278 if (strncmp((char *)data.data, "li", 2) == 0) {
1280 struct tdr_pull pull;
1282 bool found_offset = false;
1284 DEBUG(10, ("Subkeys in LI list\n"));
1289 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, regf, &li))) {
1290 DEBUG(0, ("Error parsing LI list\n"));
1291 return WERR_BADFILE;
1294 SMB_ASSERT(!strncmp(li.header, "li", 2));
1296 for (i = 0; i < li.key_count; i++) {
1298 li.nk_offset[i-1] = li.nk_offset[i];
1300 if (li.nk_offset[i] == key_offset) {
1301 found_offset = true;
1305 if (!found_offset) {
1306 DEBUG(2, ("Subkey not found\n"));
1307 return WERR_NOT_FOUND;
1311 /* If the there are no entries left, free the subkey list */
1312 if (li.key_count == 0) {
1313 hbin_free(regf, list_offset);
1317 /* Store li block */
1318 *ret = hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_li_block, list_offset, &li);
1319 } else if (strncmp((char *)data.data, "lf", 2) == 0) {
1321 struct tdr_pull pull;
1323 bool found_offset = false;
1325 DEBUG(10, ("Subkeys in LF list\n"));
1330 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(&pull, regf, &lf))) {
1331 DEBUG(0, ("Error parsing LF list\n"));
1332 return WERR_BADFILE;
1335 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1337 for (i = 0; i < lf.key_count; i++) {
1339 lf.hr[i-1] = lf.hr[i];
1342 if (lf.hr[i].nk_offset == key_offset) {
1347 if (!found_offset) {
1348 DEBUG(2, ("Subkey not found\n"));
1349 return WERR_NOT_FOUND;
1353 /* If the there are no entries left, free the subkey list */
1354 if (lf.key_count == 0) {
1355 hbin_free(regf, list_offset);
1360 /* Store lf block */
1361 *ret = hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_lf_block, list_offset, &lf);
1362 } else if (strncmp((char *)data.data, "lh", 2) == 0) {
1364 struct tdr_pull pull;
1366 bool found_offset = false;
1368 DEBUG(10, ("Subkeys in LH list\n"));
1373 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, regf, &lh))) {
1374 DEBUG(0, ("Error parsing LF list\n"));
1375 return WERR_BADFILE;
1378 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1380 for (i = 0; i < lh.key_count; i++) {
1382 lh.hr[i-1] = lh.hr[i];
1385 if (lh.hr[i].nk_offset == key_offset) {
1390 if (!found_offset) {
1391 DEBUG(0, ("Subkey not found\n"));
1392 return WERR_NOT_FOUND;
1396 /* If the there are no entries left, free the subkey list */
1397 if (lh.key_count == 0) {
1398 hbin_free(regf, list_offset);
1403 /* Store lh block */
1404 *ret = hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_lh_block, list_offset, &lh);
1405 } else if (strncmp((char *)data.data, "ri", 2) == 0) {
1407 DEBUG(0, ("Sorry, deletion from ri block is not supported yet.\n"));
1408 return WERR_NOT_SUPPORTED;
1410 DEBUG (0, ("Unknown header found in subkey list.\n"));
1411 return WERR_BADFILE;
1416 static WERROR regf_del_value (struct hive_key *key, const char *name)
1418 const struct regf_key_data *private_data =
1419 (const struct regf_key_data *)key;
1420 struct regf_data *regf = private_data->hive;
1421 struct nk_block *nk = private_data->nk;
1424 bool found_offset = false;
1428 if (nk->values_offset == -1) {
1429 return WERR_NOT_FOUND;
1432 values = hbin_get(regf, nk->values_offset);
1434 for (i = 0; i < nk->num_values; i++) {
1436 ((uint32_t *)values.data)[i-1] = ((uint32_t *) values.data)[i];
1438 vk_offset = IVAL(values.data, i * 4);
1439 if (!hbin_get_tdr(regf, vk_offset, private_data,
1440 (tdr_pull_fn_t)tdr_pull_vk_block, &vk)) {
1441 DEBUG(0, ("Unable to get VK block at %d\n", vk_offset));
1442 return WERR_BADFILE;
1444 if (strcmp(vk.data_name, name) == 0) {
1445 hbin_free(regf, vk_offset);
1446 found_offset = true;
1450 if (!found_offset) {
1451 return WERR_NOT_FOUND;
1454 values.length = (nk->num_values)*4;
1457 /* Store values list and nk */
1458 if (nk->num_values == 0) {
1459 hbin_free(regf, nk->values_offset);
1460 nk->values_offset = -1;
1462 nk->values_offset = hbin_store_resize(regf, nk->values_offset, values);
1464 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block, private_data->offset, nk);
1466 return regf_save_hbin(private_data->hive);
1470 static WERROR regf_del_key(const struct hive_key *parent, const char *name)
1472 const struct regf_key_data *private_data =
1473 (const struct regf_key_data *)parent;
1474 struct regf_key_data *key;
1475 struct nk_block *parent_nk;
1478 SMB_ASSERT(private_data);
1480 parent_nk = private_data->nk;
1482 if (parent_nk->subkeys_offset == -1) {
1483 DEBUG(4, ("Subkey list is empty, this key cannot contain subkeys.\n"));
1484 return WERR_NOT_FOUND;
1488 if (!W_ERROR_IS_OK(regf_get_subkey_by_name(parent_nk, parent, name,
1489 (struct hive_key **)&key))) {
1490 DEBUG(2, ("Key '%s' not found\n", name));
1491 return WERR_NOT_FOUND;
1494 if (key->nk->subkeys_offset != -1 ||
1495 key->nk->values_offset != -1) {
1496 DEBUG(0, ("Key '%s' is not empty.\n", name));
1497 return WERR_FILE_EXISTS;
1500 /* Delete it from the subkey list. */
1501 error = regf_sl_del_entry(private_data->hive, parent_nk->subkeys_offset,
1502 key->offset, &parent_nk->subkeys_offset);
1503 if (!W_ERROR_IS_OK(error)) {
1504 DEBUG(0, ("Can't store new subkey list for parent key. Won't delete.\n"));
1508 /* Re-store parent key */
1509 parent_nk->num_subkeys--;
1510 hbin_store_tdr_resize(private_data->hive,
1511 (tdr_push_fn_t) tdr_push_nk_block,
1512 private_data->offset, parent_nk);
1514 if (key->nk->clsname_offset != -1) {
1515 hbin_free(private_data->hive, key->nk->clsname_offset);
1517 hbin_free(private_data->hive, key->offset);
1519 return regf_save_hbin(private_data->hive);
1522 static WERROR regf_add_key(TALLOC_CTX *ctx, const struct hive_key *parent,
1523 const char *name, const char *classname,
1524 struct security_descriptor *sec_desc,
1525 struct hive_key **ret)
1527 const struct regf_key_data *private_data =
1528 (const struct regf_key_data *)parent;
1529 struct nk_block *parent_nk = private_data->nk, nk;
1530 struct nk_block *root;
1531 struct regf_data *regf = private_data->hive;
1536 nk.type = REG_SUB_KEY;
1537 unix_to_nt_time(&nk.last_change, time(NULL));
1539 nk.parent_offset = private_data->offset;
1542 nk.subkeys_offset = -1;
1543 nk.unknown_offset = -1;
1545 nk.values_offset = -1;
1546 memset(nk.unk3, 0, 5);
1547 nk.clsname_offset = -1; /* FIXME: fill in */
1548 nk.clsname_length = 0;
1551 /* Get the security descriptor of the root key */
1552 root = talloc_zero(ctx, struct nk_block);
1553 W_ERROR_HAVE_NO_MEMORY(root);
1555 if (!hbin_get_tdr(regf, regf->header->data_offset, root,
1556 (tdr_pull_fn_t)tdr_pull_nk_block, root)) {
1557 DEBUG(0, ("Unable to find HBIN data for offset %d\n", offset));
1558 return WERR_GENERAL_FAILURE;
1560 nk.sk_offset = root->sk_offset;
1563 /* Store the new nk key */
1564 offset = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_nk_block, &nk);
1566 error = regf_sl_add_entry(regf, parent_nk->subkeys_offset, name, offset, &parent_nk->subkeys_offset);
1567 if (!W_ERROR_IS_OK(error)) {
1568 hbin_free(regf, offset);
1572 parent_nk->num_subkeys++;
1574 /* Since the subkey offset of the parent can change, store it again */
1575 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
1576 nk.parent_offset, parent_nk);
1578 *ret = (struct hive_key *)regf_get_key(ctx, regf, offset);
1580 return regf_save_hbin(private_data->hive);
1583 static WERROR regf_set_value(struct hive_key *key, const char *name,
1584 uint32_t type, const DATA_BLOB data)
1586 const struct regf_key_data *private_data =
1587 (const struct regf_key_data *)key;
1588 struct regf_data *regf = private_data->hive;
1589 struct nk_block *nk = private_data->nk;
1592 uint32_t tmp_vk_offset, vk_offset, old_vk_offset = -1;
1597 /* find the value offset, if it exists */
1598 if (nk->values_offset != -1) {
1599 values = hbin_get(regf, nk->values_offset);
1601 for (i = 0; i < nk->num_values; i++) {
1602 tmp_vk_offset = IVAL(values.data, i * 4);
1603 if (!hbin_get_tdr(regf, tmp_vk_offset, private_data,
1604 (tdr_pull_fn_t)tdr_pull_vk_block, &vk)) {
1605 DEBUG(0, ("Unable to get VK block at %d\n", tmp_vk_offset));
1606 return WERR_GENERAL_FAILURE;
1608 if (strcmp(vk.data_name, name) == 0) {
1609 old_vk_offset = tmp_vk_offset;
1613 /* Free data, if any */
1614 if (!(vk.data_length & 0x80000000)) {
1615 hbin_free(regf, vk.data_offset);
1618 if (old_vk_offset == -1) {
1620 vk.name_length = strlen(name);
1621 if (name != NULL && name[0] != 0) {
1623 vk.data_name = name;
1625 vk.data_name = NULL;
1629 /* Set the type and data */
1630 vk.data_length = data.length;
1631 vk.data_type = type;
1632 if (type == REG_DWORD) {
1633 vk.data_length |= 0x80000000;
1634 vk.data_offset = *(uint32_t *)data.data;
1636 /* Store data somewhere */
1637 vk.data_offset = hbin_store(regf, data);
1639 if (old_vk_offset == -1) {
1641 vk_offset = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_vk_block, &vk);
1643 /* Store vk at offset */
1644 vk_offset = hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_vk_block, old_vk_offset ,&vk);
1647 /* Re-allocate the value list */
1648 if (nk->values_offset == -1) {
1649 nk->values_offset = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_uint32, &vk_offset);
1653 /* Change if we're changing, otherwise we're adding the value */
1654 if (old_vk_offset != -1) {
1655 /* Find and overwrite the offset. */
1656 for (i = 0; i < nk->num_values; i++) {
1657 if (IVAL(values.data, i * 4) == old_vk_offset) {
1658 SIVAL(values.data, i * 4, vk_offset);
1663 /* Create a new value list */
1664 DATA_BLOB value_list;
1666 value_list.length = (nk->num_values+1)*4;
1667 value_list.data = talloc_array(private_data, uint32_t, nk->num_values+1);
1668 W_ERROR_HAVE_NO_MEMORY(value_list.data);
1669 memcpy(value_list.data, values.data, nk->num_values * 4);
1671 SIVAL(value_list.data, nk->num_values * 4, vk_offset);
1673 nk->values_offset = hbin_store_resize(regf, nk->values_offset, value_list);
1677 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block, private_data->offset, nk);
1678 return regf_save_hbin(private_data->hive);
1681 static WERROR regf_save_hbin(struct regf_data *regf)
1683 struct tdr_push *push = talloc_zero(regf, struct tdr_push);
1686 W_ERROR_HAVE_NO_MEMORY(push);
1688 if (lseek(regf->fd, 0, SEEK_SET) == -1) {
1689 DEBUG(0, ("Error lseeking in regf file\n"));
1690 return WERR_GENERAL_FAILURE;
1693 /* Recompute checksum */
1694 if (NT_STATUS_IS_ERR(tdr_push_regf_hdr(push, regf->header))) {
1695 DEBUG(0, ("Failed to push regf header\n"));
1696 return WERR_GENERAL_FAILURE;
1698 regf->header->chksum = regf_hdr_checksum(push->data.data);
1701 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
1702 (tdr_push_fn_t)tdr_push_regf_hdr, regf->header))) {
1703 DEBUG(0, ("Error writing registry file header\n"));
1704 return WERR_GENERAL_FAILURE;
1707 if (lseek(regf->fd, 0x1000, SEEK_SET) == -1) {
1708 DEBUG(0, ("Error lseeking to 0x1000 in regf file\n"));
1709 return WERR_GENERAL_FAILURE;
1712 for (i = 0; regf->hbins[i]; i++) {
1713 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
1714 (tdr_push_fn_t)tdr_push_hbin_block,
1716 DEBUG(0, ("Error writing HBIN block\n"));
1717 return WERR_GENERAL_FAILURE;
1724 WERROR reg_create_regf_file(TALLOC_CTX *parent_ctx, const char *location,
1725 int minor_version, struct hive_key **key)
1727 struct regf_data *regf;
1728 struct regf_hdr *regf_hdr;
1729 struct tdr_pull pull;
1734 regf = (struct regf_data *)talloc_zero(NULL, struct regf_data);
1736 W_ERROR_HAVE_NO_MEMORY(regf);
1738 DEBUG(5, ("Attempting to create registry file\n"));
1740 /* Get the header */
1741 regf->fd = creat(location, 0644);
1743 if (regf->fd == -1) {
1744 DEBUG(0,("Could not create file: %s, %s\n", location,
1747 return WERR_GENERAL_FAILURE;
1750 regf_hdr = talloc_zero(regf, struct regf_hdr);
1751 W_ERROR_HAVE_NO_MEMORY(regf_hdr);
1752 regf_hdr->REGF_ID = "regf";
1753 unix_to_nt_time(®f_hdr->modtime, time(NULL));
1754 regf_hdr->version.major = 1;
1755 regf_hdr->version.minor = minor_version;
1756 regf_hdr->last_block = 0x1000; /* Block size */
1757 regf_hdr->description = talloc_strdup(regf_hdr, "registry created by Samba 4");
1758 W_ERROR_HAVE_NO_MEMORY(regf_hdr->description);
1759 regf_hdr->chksum = 0;
1761 regf->header = regf_hdr;
1763 pull.offset = 0x1000;
1766 /* Create all hbin blocks */
1767 regf->hbins = talloc_array(regf, struct hbin_block *, 1);
1768 W_ERROR_HAVE_NO_MEMORY(regf->hbins);
1769 regf->hbins[0] = NULL;
1771 regf_hdr->data_offset = -1; /* FIXME */
1774 nk.type = REG_SUB_KEY;
1775 unix_to_nt_time(&nk.last_change, time(NULL));
1777 nk.parent_offset = -1;
1780 nk.subkeys_offset = -1;
1781 nk.unknown_offset = -1;
1783 nk.values_offset = -1;
1784 memset(nk.unk3, 0, 5);
1785 nk.clsname_offset = -1; /* FIXME: fill in */
1786 nk.clsname_length = 0;
1789 nk.sk_offset = -1; /* FIXME: fill in */
1791 /* Store the new nk key */
1792 regf->header->data_offset = hbin_store_tdr(regf,
1793 (tdr_push_fn_t)tdr_push_nk_block, &nk);
1795 *key = (struct hive_key *)regf_get_key(parent_ctx, regf, regf->header->data_offset);
1797 /* We can drop our own reference now that *key will have created one */
1800 error = regf_save_hbin(regf);
1801 if (!W_ERROR_IS_OK(error)) {
1808 WERROR reg_open_regf_file(TALLOC_CTX *parent_ctx,
1809 const char *location, struct hive_key **key)
1811 struct regf_data *regf;
1812 struct regf_hdr *regf_hdr;
1813 struct tdr_pull pull;
1816 regf = (struct regf_data *)talloc_zero(NULL, struct regf_data);
1818 W_ERROR_HAVE_NO_MEMORY(regf);
1820 DEBUG(5, ("Attempting to load registry file\n"));
1822 /* Get the header */
1823 regf->fd = open(location, O_RDWR);
1825 if (regf->fd == -1) {
1826 DEBUG(0,("Could not load file: %s, %s\n", location,
1829 return WERR_GENERAL_FAILURE;
1833 pull.data.data = (uint8_t*)fd_load(regf->fd, &pull.data.length, regf);
1835 if (pull.data.data == NULL) {
1836 DEBUG(0, ("Error reading data\n"));
1838 return WERR_GENERAL_FAILURE;
1841 regf_hdr = talloc(regf, struct regf_hdr);
1842 W_ERROR_HAVE_NO_MEMORY(regf_hdr);
1844 if (NT_STATUS_IS_ERR(tdr_pull_regf_hdr(&pull, regf_hdr, regf_hdr))) {
1846 return WERR_GENERAL_FAILURE;
1849 regf->header = regf_hdr;
1851 if (strcmp(regf_hdr->REGF_ID, "regf") != 0) {
1852 DEBUG(0, ("Unrecognized NT registry header id: %s, %s\n",
1853 regf_hdr->REGF_ID, location));
1855 return WERR_GENERAL_FAILURE;
1858 /* Validate the header ... */
1859 if (regf_hdr_checksum(pull.data.data) != regf_hdr->chksum) {
1860 DEBUG(0, ("Registry file checksum error: %s: %d,%d\n",
1861 location, regf_hdr->chksum,
1862 regf_hdr_checksum(pull.data.data)));
1864 return WERR_GENERAL_FAILURE;
1867 pull.offset = 0x1000;
1870 /* Read in all hbin blocks */
1871 regf->hbins = talloc_array(regf, struct hbin_block *, 1);
1872 W_ERROR_HAVE_NO_MEMORY(regf->hbins);
1874 regf->hbins[0] = NULL;
1876 while (pull.offset < pull.data.length && pull.offset <= regf->header->last_block) {
1877 struct hbin_block *hbin = talloc(regf->hbins, struct hbin_block);
1879 W_ERROR_HAVE_NO_MEMORY(hbin);
1881 if (NT_STATUS_IS_ERR(tdr_pull_hbin_block(&pull, hbin, hbin))) {
1882 DEBUG(0, ("[%d] Error parsing HBIN block\n", i));
1887 if (strcmp(hbin->HBIN_ID, "hbin") != 0) {
1888 DEBUG(0, ("[%d] Expected 'hbin', got '%s'\n", i, hbin->HBIN_ID));
1893 regf->hbins[i] = hbin;
1895 regf->hbins = talloc_realloc(regf, regf->hbins, struct hbin_block *, i+2);
1896 regf->hbins[i] = NULL;
1899 DEBUG(1, ("%d HBIN blocks read\n", i));
1901 *key = (struct hive_key *)regf_get_key(parent_ctx, regf,
1902 regf->header->data_offset);
1904 /* We can drop our own reference now that *key will have created one */
1910 static struct hive_operations reg_backend_regf = {
1912 .get_key_info = regf_get_info,
1913 .enum_key = regf_get_subkey_by_index,
1914 .get_key_by_name = regf_get_subkey_by_name,
1915 .get_value_by_name = regf_get_value_by_name,
1916 .enum_value = regf_get_value,
1917 .get_sec_desc = regf_get_sec_desc,
1918 .set_sec_desc = regf_set_sec_desc,
1919 .add_key = regf_add_key,
1920 .set_value = regf_set_value,
1921 .del_key = regf_del_key,
1922 .delete_value = regf_del_value,