2 Samba CIFS implementation
3 Registry backend for REGF files
4 Copyright (C) 2005-2007 Jelmer Vernooij, jelmer@samba.org
5 Copyright (C) 2006-2010 Wilco Baan Hofman, wilco@baanhofman.nl
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "system/filesys.h"
22 #include "system/time.h"
23 #include "lib/registry/tdr_regf.h"
24 #include "librpc/gen_ndr/ndr_security.h"
25 #include "librpc/gen_ndr/winreg.h"
26 #include "lib/registry/registry.h"
27 #include "libcli/security/security.h"
31 static struct hive_operations reg_backend_regf;
34 * There are several places on the web where the REGF format is explained;
40 * - Return error codes that make more sense
42 * - do more things in-memory
46 * Read HBIN blocks into memory
51 struct hbin_block **hbins;
52 struct regf_hdr *header;
56 static WERROR regf_save_hbin(struct regf_data *data, bool flush);
58 struct regf_key_data {
60 struct regf_data *hive;
65 static struct hbin_block *hbin_by_offset(const struct regf_data *data,
66 uint32_t offset, uint32_t *rel_offset)
70 for (i = 0; data->hbins[i]; i++) {
71 if (offset >= data->hbins[i]->offset_from_first &&
72 offset < data->hbins[i]->offset_from_first+
73 data->hbins[i]->offset_to_next) {
74 if (rel_offset != NULL)
75 *rel_offset = offset - data->hbins[i]->offset_from_first - 0x20;
76 return data->hbins[i];
84 * Validate a regf header
85 * For now, do nothing, but we should check the checksum
87 static uint32_t regf_hdr_checksum(const uint8_t *buffer)
89 uint32_t checksum = 0, x;
92 for (i = 0; i < 0x01FB; i+= 4) {
101 * Obtain the contents of a HBIN block
103 static DATA_BLOB hbin_get(const struct regf_data *data, uint32_t offset)
106 struct hbin_block *hbin;
112 hbin = hbin_by_offset(data, offset, &rel_offset);
115 DEBUG(1, ("Can't find HBIN at 0x%04x\n", offset));
119 ret.length = IVAL(hbin->data, rel_offset);
120 if (!(ret.length & 0x80000000)) {
121 DEBUG(0, ("Trying to use dirty block at 0x%04x\n", offset));
125 /* remove high bit */
126 ret.length = (ret.length ^ 0xffffffff) + 1;
128 ret.length -= 4; /* 4 bytes for the length... */
129 ret.data = hbin->data +
130 (offset - hbin->offset_from_first - 0x20) + 4;
135 static bool hbin_get_tdr(struct regf_data *regf, uint32_t offset,
136 TALLOC_CTX *ctx, tdr_pull_fn_t pull_fn, void *p)
138 struct tdr_pull *pull = tdr_pull_init(regf);
140 pull->data = hbin_get(regf, offset);
141 if (!pull->data.data) {
142 DEBUG(1, ("Unable to get data at 0x%04x\n", offset));
147 if (NT_STATUS_IS_ERR(pull_fn(pull, ctx, p))) {
148 DEBUG(1, ("Error parsing record at 0x%04x using tdr\n",
158 /* Allocate some new data */
159 static DATA_BLOB hbin_alloc(struct regf_data *data, uint32_t size,
163 uint32_t rel_offset = (uint32_t) -1; /* Relative offset ! */
164 struct hbin_block *hbin = NULL;
167 if (offset != NULL) {
172 return data_blob(NULL, 0);
174 size += 4; /* Need to include int32 for the length */
176 /* Allocate as a multiple of 8 */
177 size = (size + 7) & ~7;
182 for (i = 0; (hbin = data->hbins[i]); i++) {
185 for (j = 0; j < hbin->offset_to_next-0x20; j+= my_size) {
186 my_size = IVALS(hbin->data, j);
188 if (my_size == 0x0) {
189 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
193 if (my_size % 8 != 0) {
194 DEBUG(0, ("Encountered non-aligned block!\n"));
197 if (my_size < 0) { /* Used... */
199 } else if (my_size == size) { /* exact match */
201 DEBUG(4, ("Found free block of exact size %d in middle of HBIN\n",
204 } else if (my_size > size) { /* data will remain */
206 /* Split this block and mark the next block as free */
207 SIVAL(hbin->data, rel_offset+size, my_size-size);
208 DEBUG(4, ("Found free block of size %d (needing %d) in middle of HBIN\n",
214 if (rel_offset != -1)
218 /* No space available in previous hbins,
219 * allocate new one */
220 if (data->hbins[i] == NULL) {
221 DEBUG(4, ("No space available in other HBINs for block of size %d, allocating new HBIN\n",
224 /* Add extra hbin block */
225 data->hbins = talloc_realloc(data, data->hbins,
226 struct hbin_block *, i+2);
227 hbin = talloc(data->hbins, struct hbin_block);
228 SMB_ASSERT(hbin != NULL);
230 data->hbins[i] = hbin;
231 data->hbins[i+1] = NULL;
234 hbin->HBIN_ID = talloc_strdup(hbin, "hbin");
235 hbin->offset_from_first = (i == 0?0:data->hbins[i-1]->offset_from_first+data->hbins[i-1]->offset_to_next);
236 hbin->offset_to_next = 0x1000;
237 hbin->unknown[0] = 0;
238 hbin->unknown[1] = 0;
239 unix_to_nt_time(&hbin->last_change, time(NULL));
240 hbin->block_size = hbin->offset_to_next;
241 hbin->data = talloc_zero_array(hbin, uint8_t, hbin->block_size - 0x20);
242 /* Update the regf header */
243 data->header->last_block += hbin->offset_to_next;
245 /* Set the next block to it's proper size and set the
246 * rel_offset for this block */
247 SIVAL(hbin->data, size, hbin->block_size - size - 0x20);
251 /* Set size and mark as used */
252 SIVAL(hbin->data, rel_offset, -size);
254 ret.data = hbin->data + rel_offset + 0x4; /* Skip past length */
255 ret.length = size - 0x4;
257 uint32_t new_rel_offset = 0;
258 *offset = hbin->offset_from_first + rel_offset + 0x20;
259 SMB_ASSERT(hbin_by_offset(data, *offset, &new_rel_offset) == hbin);
260 SMB_ASSERT(new_rel_offset == rel_offset);
266 /* Store a data blob. Return the offset at which it was stored */
267 static uint32_t hbin_store (struct regf_data *data, DATA_BLOB blob)
270 DATA_BLOB dest = hbin_alloc(data, blob.length, &ret);
272 memcpy(dest.data, blob.data, blob.length);
274 /* Make sure that we have no tailing garbage in the block */
275 if (dest.length > blob.length) {
276 memset(dest.data + blob.length, 0, dest.length - blob.length);
282 static uint32_t hbin_store_tdr(struct regf_data *data,
283 tdr_push_fn_t push_fn, void *p)
285 struct tdr_push *push = tdr_push_init(data);
288 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
289 DEBUG(0, ("Error during push\n"));
293 ret = hbin_store(data, push->data);
301 /* Free existing data */
302 static void hbin_free (struct regf_data *data, uint32_t offset)
307 struct hbin_block *hbin;
309 SMB_ASSERT (offset > 0);
311 hbin = hbin_by_offset(data, offset, &rel_offset);
316 /* Get original size */
317 size = IVALS(hbin->data, rel_offset);
320 DEBUG(1, ("Trying to free already freed block at 0x%04x\n",
327 /* If the next block is free, merge into big free block */
328 if (rel_offset + size < hbin->offset_to_next - 0x20) {
329 next_size = IVALS(hbin->data, rel_offset+size);
335 /* Write block size */
336 SIVALS(hbin->data, rel_offset, size);
340 * Store a data blob data was already stored, but has changed in size
341 * Will try to save it at the current location if possible, otherwise
342 * does a free + store */
343 static uint32_t hbin_store_resize(struct regf_data *data,
344 uint32_t orig_offset, DATA_BLOB blob)
347 struct hbin_block *hbin = hbin_by_offset(data, orig_offset,
352 int32_t possible_size;
355 SMB_ASSERT(orig_offset > 0);
358 return hbin_store(data, blob);
360 /* Get original size */
361 orig_size = -IVALS(hbin->data, rel_offset);
363 needed_size = blob.length + 4; /* Add int32 containing length */
364 needed_size = (needed_size + 7) & ~7; /* Align */
366 /* Fits into current allocated block */
367 if (orig_size >= needed_size) {
368 memcpy(hbin->data + rel_offset + 0x4, blob.data, blob.length);
369 /* If the difference in size is greater than 0x4, split the block
370 * and free/merge it */
371 if (orig_size - needed_size > 0x4) {
372 SIVALS(hbin->data, rel_offset, -needed_size);
373 SIVALS(hbin->data, rel_offset + needed_size,
374 needed_size-orig_size);
375 hbin_free(data, orig_offset + needed_size);
380 possible_size = orig_size;
382 /* Check if it can be combined with the next few free records */
383 for (i = rel_offset; i < hbin->offset_to_next - 0x20; i += my_size) {
384 if (IVALS(hbin->data, i) < 0) /* Used */
387 my_size = IVALS(hbin->data, i);
389 if (my_size == 0x0) {
390 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
393 possible_size += my_size;
396 if (possible_size >= blob.length) {
397 SIVAL(hbin->data, rel_offset, -possible_size);
398 memcpy(hbin->data + rel_offset + 0x4,
399 blob.data, blob.length);
404 hbin_free(data, orig_offset);
405 return hbin_store(data, blob);
408 static uint32_t hbin_store_tdr_resize(struct regf_data *regf,
409 tdr_push_fn_t push_fn,
410 uint32_t orig_offset, void *p)
412 struct tdr_push *push = tdr_push_init(regf);
415 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
416 DEBUG(0, ("Error during push\n"));
420 ret = hbin_store_resize(regf, orig_offset, push->data);
427 static uint32_t regf_create_lh_hash(const char *name)
433 hash_name = strupper_talloc(NULL, name);
434 for (i = 0; *(hash_name + i) != 0; i++) {
436 ret += *(hash_name + i);
438 talloc_free(hash_name);
442 static WERROR regf_get_info(TALLOC_CTX *mem_ctx,
443 const struct hive_key *key,
444 const char **classname,
445 uint32_t *num_subkeys,
446 uint32_t *num_values,
447 NTTIME *last_mod_time,
448 uint32_t *max_subkeynamelen,
449 uint32_t *max_valnamelen,
450 uint32_t *max_valbufsize)
452 const struct regf_key_data *private_data =
453 (const struct regf_key_data *)key;
455 if (num_subkeys != NULL)
456 *num_subkeys = private_data->nk->num_subkeys;
458 if (num_values != NULL)
459 *num_values = private_data->nk->num_values;
461 if (classname != NULL) {
462 if (private_data->nk->clsname_offset != -1) {
463 DATA_BLOB data = hbin_get(private_data->hive,
464 private_data->nk->clsname_offset);
465 *classname = talloc_strndup(mem_ctx,
467 private_data->nk->clsname_length);
468 W_ERROR_HAVE_NO_MEMORY(*classname);
473 /* TODO: Last mod time */
475 /* TODO: max valnamelen */
477 /* TODO: max valbufsize */
479 /* TODO: max subkeynamelen */
484 static struct regf_key_data *regf_get_key(TALLOC_CTX *ctx,
485 struct regf_data *regf,
489 struct regf_key_data *ret;
491 ret = talloc_zero(ctx, struct regf_key_data);
492 ret->key.ops = ®_backend_regf;
493 ret->hive = talloc_reference(ret, regf);
494 ret->offset = offset;
495 nk = talloc(ret, struct nk_block);
501 if (!hbin_get_tdr(regf, offset, nk,
502 (tdr_pull_fn_t)tdr_pull_nk_block, nk)) {
503 DEBUG(0, ("Unable to find HBIN data for offset 0x%x\n", offset));
507 if (strcmp(nk->header, "nk") != 0) {
508 DEBUG(0, ("Expected nk record, got %s\n", nk->header));
517 static WERROR regf_get_value(TALLOC_CTX *ctx, struct hive_key *key,
518 uint32_t idx, const char **name,
519 uint32_t *data_type, DATA_BLOB *data)
521 const struct regf_key_data *private_data =
522 (const struct regf_key_data *)key;
524 struct regf_data *regf = private_data->hive;
528 if (idx >= private_data->nk->num_values)
529 return WERR_NO_MORE_ITEMS;
531 tmp = hbin_get(regf, private_data->nk->values_offset);
533 DEBUG(0, ("Unable to find value list at 0x%x\n",
534 private_data->nk->values_offset));
535 return WERR_GEN_FAILURE;
538 if (tmp.length < private_data->nk->num_values * 4) {
539 DEBUG(1, ("Value counts mismatch\n"));
542 vk_offset = IVAL(tmp.data, idx * 4);
544 vk = talloc(NULL, struct vk_block);
545 W_ERROR_HAVE_NO_MEMORY(vk);
547 if (!hbin_get_tdr(regf, vk_offset, vk,
548 (tdr_pull_fn_t)tdr_pull_vk_block, vk)) {
549 DEBUG(0, ("Unable to get VK block at 0x%x\n", vk_offset));
551 return WERR_GEN_FAILURE;
554 /* FIXME: name character set ?*/
556 *name = talloc_strndup(ctx, vk->data_name, vk->name_length);
557 W_ERROR_HAVE_NO_MEMORY(*name);
560 if (data_type != NULL)
561 *data_type = vk->data_type;
563 if (vk->data_length & 0x80000000) {
564 /* this is data of type "REG_DWORD" or "REG_DWORD_BIG_ENDIAN" */
565 data->data = talloc_size(ctx, sizeof(uint32_t));
566 W_ERROR_HAVE_NO_MEMORY(data->data);
567 SIVAL(data->data, 0, vk->data_offset);
568 data->length = sizeof(uint32_t);
570 *data = hbin_get(regf, vk->data_offset);
573 if (data->length < vk->data_length) {
574 DEBUG(1, ("Read data less than indicated data length!\n"));
582 static WERROR regf_get_value_by_name(TALLOC_CTX *mem_ctx,
583 struct hive_key *key, const char *name,
584 uint32_t *type, DATA_BLOB *data)
590 /* FIXME: Do binary search? Is this list sorted at all? */
592 for (i = 0; W_ERROR_IS_OK(error = regf_get_value(mem_ctx, key, i,
593 &vname, type, data));
595 if (!strcmp(vname, name))
599 if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
600 return WERR_FILE_NOT_FOUND;
606 static WERROR regf_get_subkey_by_index(TALLOC_CTX *ctx,
607 const struct hive_key *key,
608 uint32_t idx, const char **name,
609 const char **classname,
610 NTTIME *last_mod_time)
613 struct regf_key_data *ret;
614 const struct regf_key_data *private_data = (const struct regf_key_data *)key;
615 struct nk_block *nk = private_data->nk;
618 if (idx >= nk->num_subkeys)
619 return WERR_NO_MORE_ITEMS;
621 /* Make sure that we don't crash if the key is empty */
622 if (nk->subkeys_offset == -1) {
623 return WERR_NO_MORE_ITEMS;
626 data = hbin_get(private_data->hive, nk->subkeys_offset);
628 DEBUG(0, ("Unable to find subkey list at 0x%x\n",
629 nk->subkeys_offset));
630 return WERR_GEN_FAILURE;
633 if (!strncmp((char *)data.data, "li", 2)) {
635 struct tdr_pull *pull = tdr_pull_init(private_data->hive);
637 DEBUG(10, ("Subkeys in LI list\n"));
640 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, nk, &li))) {
641 DEBUG(0, ("Error parsing LI list\n"));
643 return WERR_GEN_FAILURE;
646 SMB_ASSERT(!strncmp(li.header, "li", 2));
648 if (li.key_count != nk->num_subkeys) {
649 DEBUG(0, ("Subkey counts don't match\n"));
650 return WERR_GEN_FAILURE;
652 key_off = li.nk_offset[idx];
654 } else if (!strncmp((char *)data.data, "lf", 2)) {
656 struct tdr_pull *pull = tdr_pull_init(private_data->hive);
658 DEBUG(10, ("Subkeys in LF list\n"));
661 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, nk, &lf))) {
662 DEBUG(0, ("Error parsing LF list\n"));
664 return WERR_GEN_FAILURE;
667 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
669 if (lf.key_count != nk->num_subkeys) {
670 DEBUG(0, ("Subkey counts don't match\n"));
671 return WERR_GEN_FAILURE;
674 key_off = lf.hr[idx].nk_offset;
675 } else if (!strncmp((char *)data.data, "lh", 2)) {
677 struct tdr_pull *pull = tdr_pull_init(private_data->hive);
679 DEBUG(10, ("Subkeys in LH list\n"));
682 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, nk, &lh))) {
683 DEBUG(0, ("Error parsing LH list\n"));
685 return WERR_GEN_FAILURE;
688 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
690 if (lh.key_count != nk->num_subkeys) {
691 DEBUG(0, ("Subkey counts don't match\n"));
692 return WERR_GEN_FAILURE;
694 key_off = lh.hr[idx].nk_offset;
695 } else if (!strncmp((char *)data.data, "ri", 2)) {
697 struct tdr_pull *pull = tdr_pull_init(ctx);
699 uint16_t sublist_count = 0;
701 DEBUG(10, ("Subkeys in RI list\n"));
704 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull, nk, &ri))) {
705 DEBUG(0, ("Error parsing RI list\n"));
707 return WERR_GEN_FAILURE;
709 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
711 for (i = 0; i < ri.key_count; i++) {
714 /* Get sublist data blob */
715 list_data = hbin_get(private_data->hive, ri.offset[i]);
716 if (!list_data.data) {
717 DEBUG(0, ("Error getting RI list."));
719 return WERR_GEN_FAILURE;
722 pull->data = list_data;
724 if (!strncmp((char *)list_data.data, "li", 2)) {
727 DEBUG(10, ("Subkeys in RI->LI list\n"));
729 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull,
732 DEBUG(0, ("Error parsing LI list from RI\n"));
734 return WERR_GEN_FAILURE;
736 SMB_ASSERT(!strncmp(li.header, "li", 2));
738 /* Advance to next sublist if necessary */
739 if (idx >= sublist_count + li.key_count) {
740 sublist_count += li.key_count;
743 key_off = li.nk_offset[idx - sublist_count];
744 sublist_count += li.key_count;
746 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
749 DEBUG(10, ("Subkeys in RI->LH list\n"));
751 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull,
754 DEBUG(0, ("Error parsing LH list from RI\n"));
756 return WERR_GEN_FAILURE;
758 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
760 /* Advance to next sublist if necessary */
761 if (idx >= sublist_count + lh.key_count) {
762 sublist_count += lh.key_count;
765 key_off = lh.hr[idx - sublist_count].nk_offset;
766 sublist_count += lh.key_count;
769 DEBUG(0,("Unknown sublist in ri block\n"));
772 return WERR_GEN_FAILURE;
779 if (idx > sublist_count) {
780 return WERR_NO_MORE_ITEMS;
784 DEBUG(0, ("Unknown type for subkey list (0x%04x): %c%c\n",
785 nk->subkeys_offset, data.data[0], data.data[1]));
786 return WERR_GEN_FAILURE;
789 ret = regf_get_key (ctx, private_data->hive, key_off);
791 if (classname != NULL) {
792 if (ret->nk->clsname_offset != -1) {
793 DATA_BLOB db = hbin_get(ret->hive,
794 ret->nk->clsname_offset);
795 *classname = talloc_strndup(ctx,
797 ret->nk->clsname_length);
798 W_ERROR_HAVE_NO_MEMORY(*classname);
803 if (last_mod_time != NULL)
804 *last_mod_time = ret->nk->last_change;
807 *name = talloc_steal(ctx, ret->nk->key_name);
814 static WERROR regf_match_subkey_by_name(TALLOC_CTX *ctx,
815 const struct hive_key *key,
817 const char *name, uint32_t *ret)
819 DATA_BLOB subkey_data;
820 struct nk_block subkey;
821 struct tdr_pull *pull;
822 const struct regf_key_data *private_data =
823 (const struct regf_key_data *)key;
825 subkey_data = hbin_get(private_data->hive, offset);
826 if (!subkey_data.data) {
827 DEBUG(0, ("Unable to retrieve subkey HBIN\n"));
828 return WERR_GEN_FAILURE;
831 pull = tdr_pull_init(ctx);
833 pull->data = subkey_data;
835 if (NT_STATUS_IS_ERR(tdr_pull_nk_block(pull, ctx, &subkey))) {
836 DEBUG(0, ("Error parsing NK structure.\n"));
838 return WERR_GEN_FAILURE;
842 if (strncmp(subkey.header, "nk", 2)) {
843 DEBUG(0, ("Not an NK structure.\n"));
844 return WERR_GEN_FAILURE;
847 if (!strcasecmp(subkey.key_name, name)) {
855 static WERROR regf_get_subkey_by_name(TALLOC_CTX *ctx,
856 const struct hive_key *key,
858 struct hive_key **ret)
861 const struct regf_key_data *private_data =
862 (const struct regf_key_data *)key;
863 struct nk_block *nk = private_data->nk;
864 uint32_t key_off = 0;
866 /* Make sure that we don't crash if the key is empty */
867 if (nk->subkeys_offset == -1) {
868 return WERR_FILE_NOT_FOUND;
871 data = hbin_get(private_data->hive, nk->subkeys_offset);
873 DEBUG(0, ("Unable to find subkey list\n"));
874 return WERR_GEN_FAILURE;
877 if (!strncmp((char *)data.data, "li", 2)) {
879 struct tdr_pull *pull = tdr_pull_init(ctx);
882 DEBUG(10, ("Subkeys in LI list\n"));
885 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, nk, &li))) {
886 DEBUG(0, ("Error parsing LI list\n"));
888 return WERR_GEN_FAILURE;
891 SMB_ASSERT(!strncmp(li.header, "li", 2));
893 if (li.key_count != nk->num_subkeys) {
894 DEBUG(0, ("Subkey counts don't match\n"));
895 return WERR_GEN_FAILURE;
898 for (i = 0; i < li.key_count; i++) {
899 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
907 return WERR_FILE_NOT_FOUND;
908 } else if (!strncmp((char *)data.data, "lf", 2)) {
910 struct tdr_pull *pull = tdr_pull_init(ctx);
913 DEBUG(10, ("Subkeys in LF list\n"));
916 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, nk, &lf))) {
917 DEBUG(0, ("Error parsing LF list\n"));
919 return WERR_GEN_FAILURE;
922 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
924 if (lf.key_count != nk->num_subkeys) {
925 DEBUG(0, ("Subkey counts don't match\n"));
926 return WERR_GEN_FAILURE;
929 for (i = 0; i < lf.key_count; i++) {
930 if (strncmp(lf.hr[i].hash, name, 4)) {
933 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
942 return WERR_FILE_NOT_FOUND;
943 } else if (!strncmp((char *)data.data, "lh", 2)) {
945 struct tdr_pull *pull = tdr_pull_init(ctx);
949 DEBUG(10, ("Subkeys in LH list\n"));
952 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, nk, &lh))) {
953 DEBUG(0, ("Error parsing LH list\n"));
955 return WERR_GEN_FAILURE;
958 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
960 if (lh.key_count != nk->num_subkeys) {
961 DEBUG(0, ("Subkey counts don't match\n"));
962 return WERR_GEN_FAILURE;
965 hash = regf_create_lh_hash(name);
966 for (i = 0; i < lh.key_count; i++) {
967 if (lh.hr[i].base37 != hash) {
970 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
979 return WERR_FILE_NOT_FOUND;
980 } else if (!strncmp((char *)data.data, "ri", 2)) {
982 struct tdr_pull *pull = tdr_pull_init(ctx);
985 DEBUG(10, ("Subkeys in RI list\n"));
988 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull, nk, &ri))) {
989 DEBUG(0, ("Error parsing RI list\n"));
991 return WERR_GEN_FAILURE;
993 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
995 for (i = 0; i < ri.key_count; i++) {
998 /* Get sublist data blob */
999 list_data = hbin_get(private_data->hive, ri.offset[i]);
1000 if (list_data.data == NULL) {
1001 DEBUG(0, ("Error getting RI list."));
1003 return WERR_GEN_FAILURE;
1006 pull->data = list_data;
1008 if (!strncmp((char *)list_data.data, "li", 2)) {
1011 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull,
1014 DEBUG(0, ("Error parsing LI list from RI\n"));
1016 return WERR_GEN_FAILURE;
1018 SMB_ASSERT(!strncmp(li.header, "li", 2));
1020 for (j = 0; j < li.key_count; j++) {
1021 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
1028 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
1032 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull,
1035 DEBUG(0, ("Error parsing LH list from RI\n"));
1037 return WERR_GEN_FAILURE;
1039 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1041 hash = regf_create_lh_hash(name);
1042 for (j = 0; j < lh.key_count; j++) {
1043 if (lh.hr[j].base37 != hash) {
1046 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
1059 return WERR_FILE_NOT_FOUND;
1061 DEBUG(0, ("Unknown subkey list type.\n"));
1062 return WERR_GEN_FAILURE;
1065 *ret = (struct hive_key *)regf_get_key(ctx, private_data->hive,
1070 static WERROR regf_set_sec_desc(struct hive_key *key,
1071 const struct security_descriptor *sec_desc)
1073 const struct regf_key_data *private_data =
1074 (const struct regf_key_data *)key;
1075 struct sk_block cur_sk, sk, new_sk;
1076 struct regf_data *regf = private_data->hive;
1077 struct nk_block root;
1079 uint32_t sk_offset, cur_sk_offset;
1080 bool update_cur_sk = false;
1082 /* Get the root nk */
1083 hbin_get_tdr(regf, regf->header->data_offset, regf,
1084 (tdr_pull_fn_t) tdr_pull_nk_block, &root);
1086 /* Push the security descriptor to a blob */
1087 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf,
1088 sec_desc, (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
1089 DEBUG(0, ("Unable to push security descriptor\n"));
1090 return WERR_GEN_FAILURE;
1093 /* Get the current security descriptor for the key */
1094 if (!hbin_get_tdr(regf, private_data->nk->sk_offset, regf,
1095 (tdr_pull_fn_t) tdr_pull_sk_block, &cur_sk)) {
1096 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1097 return WERR_FILE_NOT_FOUND;
1099 /* If there's no change, change nothing. */
1100 if (memcmp(data.data, cur_sk.sec_desc,
1101 MIN(data.length, cur_sk.rec_size)) == 0) {
1105 /* Delete the current sk if only this key is using it */
1106 if (cur_sk.ref_cnt == 1) {
1107 /* Get the previous security descriptor for the key */
1108 if (!hbin_get_tdr(regf, cur_sk.prev_offset, regf,
1109 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1110 DEBUG(0, ("Unable to find prev security descriptor for current key\n"));
1111 return WERR_FILE_NOT_FOUND;
1113 /* Change and store the previous security descriptor */
1114 sk.next_offset = cur_sk.next_offset;
1115 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
1116 cur_sk.prev_offset, &sk);
1118 /* Get the next security descriptor for the key */
1119 if (!hbin_get_tdr(regf, cur_sk.next_offset, regf,
1120 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1121 DEBUG(0, ("Unable to find next security descriptor for current key\n"));
1122 return WERR_FILE_NOT_FOUND;
1124 /* Change and store the next security descriptor */
1125 sk.prev_offset = cur_sk.prev_offset;
1126 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
1127 cur_sk.next_offset, &sk);
1129 hbin_free(regf, private_data->nk->sk_offset);
1131 /* This key will no longer be referring to this sk */
1133 update_cur_sk = true;
1136 sk_offset = root.sk_offset;
1139 cur_sk_offset = sk_offset;
1140 if (!hbin_get_tdr(regf, sk_offset, regf,
1141 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1142 DEBUG(0, ("Unable to find security descriptor\n"));
1143 return WERR_FILE_NOT_FOUND;
1145 if (memcmp(data.data, sk.sec_desc, MIN(data.length, sk.rec_size)) == 0) {
1146 private_data->nk->sk_offset = sk_offset;
1148 hbin_store_tdr_resize(regf,
1149 (tdr_push_fn_t) tdr_push_sk_block,
1151 hbin_store_tdr_resize(regf,
1152 (tdr_push_fn_t) tdr_push_nk_block,
1153 private_data->offset,
1157 sk_offset = sk.next_offset;
1158 } while (sk_offset != root.sk_offset);
1160 ZERO_STRUCT(new_sk);
1161 new_sk.header = "sk";
1162 new_sk.prev_offset = cur_sk_offset;
1163 new_sk.next_offset = root.sk_offset;
1165 new_sk.rec_size = data.length;
1166 new_sk.sec_desc = data.data;
1168 sk_offset = hbin_store_tdr(regf,
1169 (tdr_push_fn_t) tdr_push_sk_block,
1171 if (sk_offset == -1) {
1172 DEBUG(0, ("Error storing sk block\n"));
1173 return WERR_GEN_FAILURE;
1175 private_data->nk->sk_offset = sk_offset;
1177 if (update_cur_sk) {
1178 hbin_store_tdr_resize(regf,
1179 (tdr_push_fn_t) tdr_push_sk_block,
1180 private_data->nk->sk_offset, &cur_sk);
1183 /* Get the previous security descriptor for the key */
1184 if (!hbin_get_tdr(regf, new_sk.prev_offset, regf,
1185 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1186 DEBUG(0, ("Unable to find security descriptor for previous key\n"));
1187 return WERR_FILE_NOT_FOUND;
1189 /* Change and store the previous security descriptor */
1190 sk.next_offset = sk_offset;
1191 hbin_store_tdr_resize(regf,
1192 (tdr_push_fn_t) tdr_push_sk_block,
1193 cur_sk.prev_offset, &sk);
1195 /* Get the next security descriptor for the key (always root, as we append) */
1196 if (!hbin_get_tdr(regf, new_sk.next_offset, regf,
1197 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1198 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1199 return WERR_FILE_NOT_FOUND;
1201 /* Change and store the next security descriptor (always root, as we append) */
1202 sk.prev_offset = sk_offset;
1203 hbin_store_tdr_resize(regf,
1204 (tdr_push_fn_t) tdr_push_sk_block,
1205 root.sk_offset, &sk);
1209 hbin_store_tdr_resize(regf,
1210 (tdr_push_fn_t) tdr_push_sk_block,
1211 private_data->offset, private_data->nk);
1215 static WERROR regf_get_sec_desc(TALLOC_CTX *ctx, const struct hive_key *key,
1216 struct security_descriptor **sd)
1218 const struct regf_key_data *private_data =
1219 (const struct regf_key_data *)key;
1221 struct regf_data *regf = private_data->hive;
1224 if (!hbin_get_tdr(regf, private_data->nk->sk_offset, ctx,
1225 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1226 DEBUG(0, ("Unable to find security descriptor\n"));
1227 return WERR_GEN_FAILURE;
1230 if (strcmp(sk.header, "sk") != 0) {
1231 DEBUG(0, ("Expected 'sk', got '%s'\n", sk.header));
1232 return WERR_GEN_FAILURE;
1235 *sd = talloc(ctx, struct security_descriptor);
1236 W_ERROR_HAVE_NO_MEMORY(*sd);
1238 data.data = sk.sec_desc;
1239 data.length = sk.rec_size;
1240 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_struct_blob(&data, ctx, *sd,
1241 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor))) {
1242 DEBUG(0, ("Error parsing security descriptor\n"));
1243 return WERR_GEN_FAILURE;
1249 static WERROR regf_sl_add_entry(struct regf_data *regf, uint32_t list_offset,
1251 uint32_t key_offset, uint32_t *ret)
1255 /* Create a new key if necessary */
1256 if (list_offset == -1) {
1257 if (regf->header->version.major != 1) {
1258 DEBUG(0, ("Can't store keys in unknown registry format\n"));
1259 return WERR_NOT_SUPPORTED;
1261 if (regf->header->version.minor < 3) {
1268 li.nk_offset = talloc_array(regf, uint32_t, 1);
1269 W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1270 li.nk_offset[0] = key_offset;
1272 *ret = hbin_store_tdr(regf,
1273 (tdr_push_fn_t) tdr_push_li_block,
1276 talloc_free(li.nk_offset);
1277 } else if (regf->header->version.minor == 3 ||
1278 regf->header->version.minor == 4) {
1285 lf.hr = talloc_array(regf, struct hash_record, 1);
1286 W_ERROR_HAVE_NO_MEMORY(lf.hr);
1287 lf.hr[0].nk_offset = key_offset;
1288 lf.hr[0].hash = talloc_strndup(lf.hr, name, 4);
1289 W_ERROR_HAVE_NO_MEMORY(lf.hr[0].hash);
1291 *ret = hbin_store_tdr(regf,
1292 (tdr_push_fn_t) tdr_push_lf_block,
1296 } else if (regf->header->version.minor == 5) {
1303 lh.hr = talloc_array(regf, struct lh_hash, 1);
1304 W_ERROR_HAVE_NO_MEMORY(lh.hr);
1305 lh.hr[0].nk_offset = key_offset;
1306 lh.hr[0].base37 = regf_create_lh_hash(name);
1308 *ret = hbin_store_tdr(regf,
1309 (tdr_push_fn_t) tdr_push_lh_block,
1317 data = hbin_get(regf, list_offset);
1319 DEBUG(0, ("Unable to find subkey list\n"));
1320 return WERR_FILE_NOT_FOUND;
1323 if (!strncmp((char *)data.data, "li", 2)) {
1324 struct tdr_pull *pull = tdr_pull_init(regf);
1326 struct nk_block sub_nk;
1331 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, regf, &li))) {
1332 DEBUG(0, ("Error parsing LI list\n"));
1334 return WERR_FILE_NOT_FOUND;
1338 if (strncmp(li.header, "li", 2) != 0) {
1340 DEBUG(0, ("LI header corrupt\n"));
1341 return WERR_FILE_NOT_FOUND;
1345 * Find the position to store the pointer
1346 * Extensive testing reveils that at least on windows 7 subkeys
1347 * *MUST* be stored in alphabetical order
1349 for (i = 0; i < li.key_count; i++) {
1351 hbin_get_tdr(regf, li.nk_offset[i], regf,
1352 (tdr_pull_fn_t) tdr_pull_nk_block, &sub_nk);
1353 if (strcasecmp(name, sub_nk.key_name) < 0) {
1358 li.nk_offset = talloc_realloc(regf, li.nk_offset,
1359 uint32_t, li.key_count+1);
1360 W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1362 /* Move everything behind this offset */
1363 for (j = li.key_count - 1; j >= i; j--) {
1364 li.nk_offset[j+1] = li.nk_offset[j];
1367 li.nk_offset[i] = key_offset;
1369 *ret = hbin_store_tdr_resize(regf,
1370 (tdr_push_fn_t)tdr_push_li_block,
1373 talloc_free(li.nk_offset);
1374 } else if (!strncmp((char *)data.data, "lf", 2)) {
1375 struct tdr_pull *pull = tdr_pull_init(regf);
1377 struct nk_block sub_nk;
1382 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, regf, &lf))) {
1383 DEBUG(0, ("Error parsing LF list\n"));
1385 return WERR_FILE_NOT_FOUND;
1388 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1391 * Find the position to store the hash record
1392 * Extensive testing reveils that at least on windows 7 subkeys
1393 * *MUST* be stored in alphabetical order
1395 for (i = 0; i < lf.key_count; i++) {
1397 hbin_get_tdr(regf, lf.hr[i].nk_offset, regf,
1398 (tdr_pull_fn_t) tdr_pull_nk_block, &sub_nk);
1399 if (strcasecmp(name, sub_nk.key_name) < 0) {
1404 lf.hr = talloc_realloc(regf, lf.hr, struct hash_record,
1406 W_ERROR_HAVE_NO_MEMORY(lf.hr);
1408 /* Move everything behind this hash record */
1409 for (j = lf.key_count - 1; j >= i; j--) {
1410 lf.hr[j+1] = lf.hr[j];
1413 lf.hr[i].nk_offset = key_offset;
1414 lf.hr[i].hash = talloc_strndup(lf.hr, name, 4);
1415 W_ERROR_HAVE_NO_MEMORY(lf.hr[lf.key_count].hash);
1417 *ret = hbin_store_tdr_resize(regf,
1418 (tdr_push_fn_t)tdr_push_lf_block,
1422 } else if (!strncmp((char *)data.data, "lh", 2)) {
1423 struct tdr_pull *pull = tdr_pull_init(regf);
1425 struct nk_block sub_nk;
1430 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, regf, &lh))) {
1431 DEBUG(0, ("Error parsing LH list\n"));
1433 return WERR_FILE_NOT_FOUND;
1436 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1439 * Find the position to store the hash record
1440 * Extensive testing reveils that at least on windows 7 subkeys
1441 * *MUST* be stored in alphabetical order
1443 for (i = 0; i < lh.key_count; i++) {
1445 hbin_get_tdr(regf, lh.hr[i].nk_offset, regf,
1446 (tdr_pull_fn_t) tdr_pull_nk_block, &sub_nk);
1447 if (strcasecmp(name, sub_nk.key_name) < 0) {
1452 lh.hr = talloc_realloc(regf, lh.hr, struct lh_hash,
1454 W_ERROR_HAVE_NO_MEMORY(lh.hr);
1456 /* Move everything behind this hash record */
1457 for (j = lh.key_count - 1; j >= i; j--) {
1458 lh.hr[j+1] = lh.hr[j];
1461 lh.hr[i].nk_offset = key_offset;
1462 lh.hr[i].base37 = regf_create_lh_hash(name);
1464 *ret = hbin_store_tdr_resize(regf,
1465 (tdr_push_fn_t)tdr_push_lh_block,
1469 } else if (!strncmp((char *)data.data, "ri", 2)) {
1471 DEBUG(0, ("Adding to 'ri' subkey list is not supported yet.\n"));
1472 return WERR_NOT_SUPPORTED;
1474 DEBUG(0, ("Cannot add to unknown subkey list\n"));
1475 return WERR_FILE_NOT_FOUND;
1481 static WERROR regf_sl_del_entry(struct regf_data *regf, uint32_t list_offset,
1482 uint32_t key_offset, uint32_t *ret)
1486 data = hbin_get(regf, list_offset);
1488 DEBUG(0, ("Unable to find subkey list\n"));
1489 return WERR_FILE_NOT_FOUND;
1492 if (strncmp((char *)data.data, "li", 2) == 0) {
1494 struct tdr_pull *pull = tdr_pull_init(regf);
1496 bool found_offset = false;
1498 DEBUG(10, ("Subkeys in LI list\n"));
1502 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, regf, &li))) {
1503 DEBUG(0, ("Error parsing LI list\n"));
1505 return WERR_FILE_NOT_FOUND;
1509 SMB_ASSERT(!strncmp(li.header, "li", 2));
1511 for (i = 0; i < li.key_count; i++) {
1513 li.nk_offset[i-1] = li.nk_offset[i];
1515 if (li.nk_offset[i] == key_offset) {
1516 found_offset = true;
1520 if (!found_offset) {
1521 DEBUG(2, ("Subkey not found\n"));
1522 return WERR_FILE_NOT_FOUND;
1526 /* If the there are no entries left, free the subkey list */
1527 if (li.key_count == 0) {
1528 hbin_free(regf, list_offset);
1532 /* Store li block */
1533 *ret = hbin_store_tdr_resize(regf,
1534 (tdr_push_fn_t) tdr_push_li_block,
1536 } else if (strncmp((char *)data.data, "lf", 2) == 0) {
1538 struct tdr_pull *pull = tdr_pull_init(regf);
1540 bool found_offset = false;
1542 DEBUG(10, ("Subkeys in LF list\n"));
1546 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, regf, &lf))) {
1547 DEBUG(0, ("Error parsing LF list\n"));
1549 return WERR_FILE_NOT_FOUND;
1553 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1555 for (i = 0; i < lf.key_count; i++) {
1557 lf.hr[i-1] = lf.hr[i];
1560 if (lf.hr[i].nk_offset == key_offset) {
1565 if (!found_offset) {
1566 DEBUG(2, ("Subkey not found\n"));
1567 return WERR_FILE_NOT_FOUND;
1571 /* If the there are no entries left, free the subkey list */
1572 if (lf.key_count == 0) {
1573 hbin_free(regf, list_offset);
1578 /* Store lf block */
1579 *ret = hbin_store_tdr_resize(regf,
1580 (tdr_push_fn_t) tdr_push_lf_block,
1582 } else if (strncmp((char *)data.data, "lh", 2) == 0) {
1584 struct tdr_pull *pull = tdr_pull_init(regf);
1586 bool found_offset = false;
1588 DEBUG(10, ("Subkeys in LH list\n"));
1592 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, regf, &lh))) {
1593 DEBUG(0, ("Error parsing LF list\n"));
1595 return WERR_FILE_NOT_FOUND;
1599 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1601 for (i = 0; i < lh.key_count; i++) {
1603 lh.hr[i-1] = lh.hr[i];
1606 if (lh.hr[i].nk_offset == key_offset) {
1611 if (!found_offset) {
1612 DEBUG(0, ("Subkey not found\n"));
1613 return WERR_FILE_NOT_FOUND;
1617 /* If the there are no entries left, free the subkey list */
1618 if (lh.key_count == 0) {
1619 hbin_free(regf, list_offset);
1624 /* Store lh block */
1625 *ret = hbin_store_tdr_resize(regf,
1626 (tdr_push_fn_t) tdr_push_lh_block,
1628 } else if (strncmp((char *)data.data, "ri", 2) == 0) {
1630 DEBUG(0, ("Sorry, deletion from ri block is not supported yet.\n"));
1631 return WERR_NOT_SUPPORTED;
1633 DEBUG (0, ("Unknown header found in subkey list.\n"));
1634 return WERR_FILE_NOT_FOUND;
1639 static WERROR regf_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
1642 struct regf_key_data *private_data = (struct regf_key_data *)key;
1643 struct regf_data *regf = private_data->hive;
1644 struct nk_block *nk = private_data->nk;
1647 bool found_offset = false;
1651 if (nk->values_offset == -1) {
1652 return WERR_FILE_NOT_FOUND;
1655 values = hbin_get(regf, nk->values_offset);
1657 for (i = 0; i < nk->num_values; i++) {
1659 ((uint32_t *)values.data)[i-1] = ((uint32_t *) values.data)[i];
1661 vk_offset = IVAL(values.data, i * 4);
1662 if (!hbin_get_tdr(regf, vk_offset, private_data,
1663 (tdr_pull_fn_t)tdr_pull_vk_block,
1665 DEBUG(0, ("Unable to get VK block at %d\n",
1667 return WERR_FILE_NOT_FOUND;
1669 if (strcmp(vk.data_name, name) == 0) {
1670 hbin_free(regf, vk_offset);
1671 found_offset = true;
1675 if (!found_offset) {
1676 return WERR_FILE_NOT_FOUND;
1679 values.length = (nk->num_values)*4;
1682 /* Store values list and nk */
1683 if (nk->num_values == 0) {
1684 hbin_free(regf, nk->values_offset);
1685 nk->values_offset = -1;
1687 nk->values_offset = hbin_store_resize(regf,
1691 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
1692 private_data->offset, nk);
1694 return regf_save_hbin(private_data->hive, 0);
1698 static WERROR regf_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
1701 const struct regf_key_data *private_data =
1702 (const struct regf_key_data *)parent;
1703 struct regf_key_data *key;
1704 struct nk_block *parent_nk;
1707 SMB_ASSERT(private_data);
1709 parent_nk = private_data->nk;
1711 if (parent_nk->subkeys_offset == -1) {
1712 DEBUG(4, ("Subkey list is empty, this key cannot contain subkeys.\n"));
1713 return WERR_FILE_NOT_FOUND;
1717 if (!W_ERROR_IS_OK(regf_get_subkey_by_name(parent_nk, parent, name,
1718 (struct hive_key **)&key))) {
1719 DEBUG(2, ("Key '%s' not found\n", name));
1720 return WERR_FILE_NOT_FOUND;
1723 if (key->nk->subkeys_offset != -1) {
1724 struct hive_key *sk = (struct hive_key *)key;
1725 unsigned int i = key->nk->num_subkeys;
1728 const char *p = NULL;
1730 /* Get subkey information. */
1731 error = regf_get_subkey_by_index(parent_nk, sk, 0,
1734 if (!W_ERROR_IS_OK(error)) {
1735 DEBUG(0, ("Can't retrieve subkey by index.\n"));
1738 sk_name = discard_const_p(char, p);
1740 /* Delete subkey. */
1741 error = regf_del_key(NULL, sk, sk_name);
1742 if (!W_ERROR_IS_OK(error)) {
1743 DEBUG(0, ("Can't delete key '%s'.\n", sk_name));
1747 talloc_free(sk_name);
1751 if (key->nk->values_offset != -1) {
1752 struct hive_key *sk = (struct hive_key *)key;
1754 unsigned int i = key->nk->num_values;
1757 const char *p = NULL;
1759 /* Get value information. */
1760 error = regf_get_value(parent_nk, sk, 0,
1763 if (!W_ERROR_IS_OK(error)) {
1764 DEBUG(0, ("Can't retrieve value by index.\n"));
1767 val_name = discard_const_p(char, p);
1770 error = regf_del_value(NULL, sk, val_name);
1771 if (!W_ERROR_IS_OK(error)) {
1772 DEBUG(0, ("Can't delete value '%s'.\n", val_name));
1776 talloc_free(val_name);
1780 /* Delete it from the subkey list. */
1781 error = regf_sl_del_entry(private_data->hive, parent_nk->subkeys_offset,
1782 key->offset, &parent_nk->subkeys_offset);
1783 if (!W_ERROR_IS_OK(error)) {
1784 DEBUG(0, ("Can't store new subkey list for parent key. Won't delete.\n"));
1788 /* Re-store parent key */
1789 parent_nk->num_subkeys--;
1790 hbin_store_tdr_resize(private_data->hive,
1791 (tdr_push_fn_t) tdr_push_nk_block,
1792 private_data->offset, parent_nk);
1794 if (key->nk->clsname_offset != -1) {
1795 hbin_free(private_data->hive, key->nk->clsname_offset);
1797 hbin_free(private_data->hive, key->offset);
1799 return regf_save_hbin(private_data->hive, 0);
1802 static WERROR regf_add_key(TALLOC_CTX *ctx, const struct hive_key *parent,
1803 const char *name, const char *classname,
1804 struct security_descriptor *sec_desc,
1805 struct hive_key **ret)
1807 const struct regf_key_data *private_data =
1808 (const struct regf_key_data *)parent;
1809 struct nk_block *parent_nk = private_data->nk, nk;
1810 struct nk_block *root;
1811 struct regf_data *regf = private_data->hive;
1816 nk.type = REG_SUB_KEY;
1817 unix_to_nt_time(&nk.last_change, time(NULL));
1819 nk.parent_offset = private_data->offset;
1822 nk.subkeys_offset = -1;
1823 nk.unknown_offset = -1;
1825 nk.values_offset = -1;
1826 memset(nk.unk3, 0, sizeof(nk.unk3));
1827 nk.clsname_offset = -1; /* FIXME: fill in */
1828 nk.clsname_length = 0;
1831 /* Get the security descriptor of the root key */
1832 root = talloc_zero(ctx, struct nk_block);
1833 W_ERROR_HAVE_NO_MEMORY(root);
1835 if (!hbin_get_tdr(regf, regf->header->data_offset, root,
1836 (tdr_pull_fn_t)tdr_pull_nk_block, root)) {
1837 DEBUG(0, ("Unable to find HBIN data for offset 0x%x\n",
1838 regf->header->data_offset));
1839 return WERR_GEN_FAILURE;
1841 nk.sk_offset = root->sk_offset;
1844 /* Store the new nk key */
1845 offset = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_nk_block, &nk);
1847 error = regf_sl_add_entry(regf, parent_nk->subkeys_offset, name, offset,
1848 &parent_nk->subkeys_offset);
1849 if (!W_ERROR_IS_OK(error)) {
1850 hbin_free(regf, offset);
1854 parent_nk->num_subkeys++;
1856 /* Since the subkey offset of the parent can change, store it again */
1857 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
1858 nk.parent_offset, parent_nk);
1860 *ret = (struct hive_key *)regf_get_key(ctx, regf, offset);
1862 DEBUG(9, ("Storing key %s\n", name));
1863 return regf_save_hbin(private_data->hive, 0);
1866 static WERROR regf_set_value(struct hive_key *key, const char *name,
1867 uint32_t type, const DATA_BLOB data)
1869 struct regf_key_data *private_data = (struct regf_key_data *)key;
1870 struct regf_data *regf = private_data->hive;
1871 struct nk_block *nk = private_data->nk;
1874 uint32_t tmp_vk_offset, vk_offset, old_vk_offset = (uint32_t) -1;
1875 DATA_BLOB values = {0};
1879 /* find the value offset, if it exists */
1880 if (nk->values_offset != -1) {
1881 values = hbin_get(regf, nk->values_offset);
1883 for (i = 0; i < nk->num_values; i++) {
1884 tmp_vk_offset = IVAL(values.data, i * 4);
1885 if (!hbin_get_tdr(regf, tmp_vk_offset, private_data,
1886 (tdr_pull_fn_t)tdr_pull_vk_block,
1888 DEBUG(0, ("Unable to get VK block at 0x%x\n",
1890 return WERR_GEN_FAILURE;
1892 if (strcmp(vk.data_name, name) == 0) {
1893 old_vk_offset = tmp_vk_offset;
1899 /* If it's new, create the vk struct, if it's old, free the old data. */
1900 if (old_vk_offset == -1) {
1902 if (name != NULL && name[0] != '\0') {
1904 vk.data_name = name;
1905 vk.name_length = strlen(name);
1908 vk.data_name = NULL;
1912 /* Free data, if any */
1913 if (!(vk.data_length & 0x80000000)) {
1914 hbin_free(regf, vk.data_offset);
1918 /* Set the type and data */
1919 vk.data_length = data.length;
1920 vk.data_type = type;
1921 if ((type == REG_DWORD) || (type == REG_DWORD_BIG_ENDIAN)) {
1922 if (vk.data_length != sizeof(uint32_t)) {
1923 DEBUG(0, ("DWORD or DWORD_BIG_ENDIAN value with size other than 4 byte!\n"));
1924 return WERR_NOT_SUPPORTED;
1926 vk.data_length |= 0x80000000;
1927 vk.data_offset = IVAL(data.data, 0);
1929 /* Store data somewhere */
1930 vk.data_offset = hbin_store(regf, data);
1932 if (old_vk_offset == -1) {
1934 vk_offset = hbin_store_tdr(regf,
1935 (tdr_push_fn_t) tdr_push_vk_block,
1938 /* Store vk at offset */
1939 vk_offset = hbin_store_tdr_resize(regf,
1940 (tdr_push_fn_t) tdr_push_vk_block,
1941 old_vk_offset ,&vk);
1944 /* Re-allocate the value list */
1945 if (nk->values_offset == -1) {
1946 nk->values_offset = hbin_store_tdr(regf,
1947 (tdr_push_fn_t) tdr_push_uint32,
1952 /* Change if we're changing, otherwise we're adding the value */
1953 if (old_vk_offset != -1) {
1954 /* Find and overwrite the offset. */
1955 for (i = 0; i < nk->num_values; i++) {
1956 if (IVAL(values.data, i * 4) == old_vk_offset) {
1957 SIVAL(values.data, i * 4, vk_offset);
1962 /* Create a new value list */
1963 DATA_BLOB value_list;
1965 value_list.length = (nk->num_values+1)*4;
1966 value_list.data = (uint8_t *)talloc_array(private_data,
1969 W_ERROR_HAVE_NO_MEMORY(value_list.data);
1970 memcpy(value_list.data, values.data, nk->num_values * 4);
1972 SIVAL(value_list.data, nk->num_values * 4, vk_offset);
1974 nk->values_offset = hbin_store_resize(regf,
1980 hbin_store_tdr_resize(regf,
1981 (tdr_push_fn_t) tdr_push_nk_block,
1982 private_data->offset, nk);
1983 return regf_save_hbin(private_data->hive, 0);
1986 static WERROR regf_save_hbin(struct regf_data *regf, bool flush)
1988 struct tdr_push *push = tdr_push_init(regf);
1991 W_ERROR_HAVE_NO_MEMORY(push);
1993 /* Only write once every 5 seconds, or when flush is set */
1994 if (!flush && regf->last_write + 5 >= time(NULL)) {
1998 regf->last_write = time(NULL);
2000 if (lseek(regf->fd, 0, SEEK_SET) == -1) {
2001 DEBUG(0, ("Error lseeking in regf file\n"));
2002 return WERR_GEN_FAILURE;
2005 /* Recompute checksum */
2006 if (NT_STATUS_IS_ERR(tdr_push_regf_hdr(push, regf->header))) {
2007 DEBUG(0, ("Failed to push regf header\n"));
2008 return WERR_GEN_FAILURE;
2010 regf->header->chksum = regf_hdr_checksum(push->data.data);
2013 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
2014 (tdr_push_fn_t)tdr_push_regf_hdr,
2016 DEBUG(0, ("Error writing registry file header\n"));
2017 return WERR_GEN_FAILURE;
2020 if (lseek(regf->fd, 0x1000, SEEK_SET) == -1) {
2021 DEBUG(0, ("Error lseeking to 0x1000 in regf file\n"));
2022 return WERR_GEN_FAILURE;
2025 for (i = 0; regf->hbins[i]; i++) {
2026 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
2027 (tdr_push_fn_t)tdr_push_hbin_block,
2029 DEBUG(0, ("Error writing HBIN block\n"));
2030 return WERR_GEN_FAILURE;
2037 WERROR reg_create_regf_file(TALLOC_CTX *parent_ctx,
2038 const char *location,
2039 int minor_version, struct hive_key **key)
2041 struct regf_data *regf;
2042 struct regf_hdr *regf_hdr;
2047 struct security_descriptor *sd;
2050 regf = (struct regf_data *)talloc_zero(NULL, struct regf_data);
2052 W_ERROR_HAVE_NO_MEMORY(regf);
2054 DEBUG(5, ("Attempting to create registry file\n"));
2056 /* Get the header */
2057 regf->fd = creat(location, 0644);
2059 if (regf->fd == -1) {
2060 DEBUG(0,("Could not create file: %s, %s\n", location,
2063 return WERR_GEN_FAILURE;
2066 regf_hdr = talloc_zero(regf, struct regf_hdr);
2067 W_ERROR_HAVE_NO_MEMORY(regf_hdr);
2068 regf_hdr->REGF_ID = "regf";
2069 unix_to_nt_time(®f_hdr->modtime, time(NULL));
2070 regf_hdr->version.major = 1;
2071 regf_hdr->version.minor = minor_version;
2072 regf_hdr->last_block = 0x1000; /* Block size */
2073 regf_hdr->description = talloc_strdup(regf_hdr,
2074 "Registry created by Samba 4");
2075 W_ERROR_HAVE_NO_MEMORY(regf_hdr->description);
2076 regf_hdr->chksum = 0;
2078 regf->header = regf_hdr;
2080 /* Create all hbin blocks */
2081 regf->hbins = talloc_array(regf, struct hbin_block *, 1);
2082 W_ERROR_HAVE_NO_MEMORY(regf->hbins);
2083 regf->hbins[0] = NULL;
2086 nk.type = REG_ROOT_KEY;
2087 unix_to_nt_time(&nk.last_change, time(NULL));
2089 nk.parent_offset = -1;
2092 nk.subkeys_offset = -1;
2093 nk.unknown_offset = -1;
2095 nk.values_offset = -1;
2096 memset(nk.unk3, 0, 5 * sizeof(uint32_t));
2097 nk.clsname_offset = -1;
2098 nk.clsname_length = 0;
2099 nk.sk_offset = 0x80;
2100 nk.key_name = "SambaRootKey";
2103 * It should be noted that changing the key_name to something shorter
2104 * creates a shorter nk block, which makes the position of the sk block
2105 * change. All Windows registries I've seen have the sk at 0x80.
2106 * I therefore recommend that our regf files share that offset -- Wilco
2109 /* Create a security descriptor. */
2110 sd = security_descriptor_dacl_create(regf,
2113 SID_NT_AUTHENTICATED_USERS,
2114 SEC_ACE_TYPE_ACCESS_ALLOWED,
2116 SEC_ACE_FLAG_OBJECT_INHERIT,
2119 /* Push the security descriptor to a blob */
2120 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf,
2121 sd, (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
2122 DEBUG(0, ("Unable to push security descriptor\n"));
2123 return WERR_GEN_FAILURE;
2128 sk.prev_offset = 0x80;
2129 sk.next_offset = 0x80;
2131 sk.rec_size = data.length;
2132 sk.sec_desc = data.data;
2134 /* Store the new nk key */
2135 regf->header->data_offset = hbin_store_tdr(regf,
2136 (tdr_push_fn_t)tdr_push_nk_block,
2138 /* Store the sk block */
2139 sk_offset = hbin_store_tdr(regf,
2140 (tdr_push_fn_t) tdr_push_sk_block,
2142 if (sk_offset != 0x80) {
2143 DEBUG(0, ("Error storing sk block, should be at 0x80, stored at 0x%x\n", nk.sk_offset));
2144 return WERR_GEN_FAILURE;
2148 *key = (struct hive_key *)regf_get_key(parent_ctx, regf,
2149 regf->header->data_offset);
2151 error = regf_save_hbin(regf, 1);
2152 if (!W_ERROR_IS_OK(error)) {
2156 /* We can drop our own reference now that *key will have created one */
2157 talloc_unlink(NULL, regf);
2162 static WERROR regf_flush_key(struct hive_key *key)
2164 struct regf_key_data *private_data = (struct regf_key_data *)key;
2165 struct regf_data *regf = private_data->hive;
2168 error = regf_save_hbin(regf, 1);
2169 if (!W_ERROR_IS_OK(error)) {
2170 DEBUG(0, ("Failed to flush regf to disk\n"));
2177 static int regf_destruct(struct regf_data *regf)
2182 error = regf_save_hbin(regf, 1);
2183 if (!W_ERROR_IS_OK(error)) {
2184 DEBUG(0, ("Failed to flush registry to disk\n"));
2188 /* Close file descriptor */
2194 WERROR reg_open_regf_file(TALLOC_CTX *parent_ctx, const char *location,
2195 struct hive_key **key)
2197 struct regf_data *regf;
2198 struct regf_hdr *regf_hdr;
2199 struct tdr_pull *pull;
2202 regf = (struct regf_data *)talloc_zero(parent_ctx, struct regf_data);
2203 W_ERROR_HAVE_NO_MEMORY(regf);
2205 talloc_set_destructor(regf, regf_destruct);
2207 DEBUG(5, ("Attempting to load registry file\n"));
2209 /* Get the header */
2210 regf->fd = open(location, O_RDWR);
2212 if (regf->fd == -1) {
2213 DEBUG(0,("Could not load file: %s, %s\n", location,
2216 return WERR_GEN_FAILURE;
2219 pull = tdr_pull_init(regf);
2221 pull->data.data = (uint8_t*)fd_load(regf->fd, &pull->data.length, 0, regf);
2223 if (pull->data.data == NULL) {
2224 DEBUG(0, ("Error reading data from file: %s\n", location));
2226 return WERR_GEN_FAILURE;
2229 regf_hdr = talloc(regf, struct regf_hdr);
2230 W_ERROR_HAVE_NO_MEMORY(regf_hdr);
2232 if (NT_STATUS_IS_ERR(tdr_pull_regf_hdr(pull, regf_hdr, regf_hdr))) {
2233 DEBUG(0, ("Failed to pull regf header from file: %s\n", location));
2235 return WERR_GEN_FAILURE;
2238 regf->header = regf_hdr;
2240 if (strcmp(regf_hdr->REGF_ID, "regf") != 0) {
2241 DEBUG(0, ("Unrecognized NT registry header id: %s, %s\n",
2242 regf_hdr->REGF_ID, location));
2244 return WERR_GEN_FAILURE;
2247 /* Validate the header ... */
2248 if (regf_hdr_checksum(pull->data.data) != regf_hdr->chksum) {
2249 DEBUG(0, ("Registry file checksum error: %s: %d,%d\n",
2250 location, regf_hdr->chksum,
2251 regf_hdr_checksum(pull->data.data)));
2253 return WERR_GEN_FAILURE;
2256 pull->offset = 0x1000;
2259 /* Read in all hbin blocks */
2260 regf->hbins = talloc_array(regf, struct hbin_block *, 1);
2261 W_ERROR_HAVE_NO_MEMORY(regf->hbins);
2263 regf->hbins[0] = NULL;
2265 while (pull->offset < pull->data.length &&
2266 pull->offset <= regf->header->last_block) {
2267 struct hbin_block *hbin = talloc(regf->hbins,
2270 W_ERROR_HAVE_NO_MEMORY(hbin);
2272 if (NT_STATUS_IS_ERR(tdr_pull_hbin_block(pull, hbin, hbin))) {
2273 DEBUG(0, ("[%d] Error parsing HBIN block\n", i));
2278 if (strcmp(hbin->HBIN_ID, "hbin") != 0) {
2279 DEBUG(0, ("[%d] Expected 'hbin', got '%s'\n",
2285 regf->hbins[i] = hbin;
2287 regf->hbins = talloc_realloc(regf, regf->hbins,
2288 struct hbin_block *, i+2);
2289 regf->hbins[i] = NULL;
2294 DEBUG(1, ("%d HBIN blocks read\n", i));
2296 *key = (struct hive_key *)regf_get_key(parent_ctx, regf,
2297 regf->header->data_offset);
2299 /* We can drop our own reference now that *key will have created one */
2300 talloc_unlink(parent_ctx, regf);
2305 static struct hive_operations reg_backend_regf = {
2307 .get_key_info = regf_get_info,
2308 .enum_key = regf_get_subkey_by_index,
2309 .get_key_by_name = regf_get_subkey_by_name,
2310 .get_value_by_name = regf_get_value_by_name,
2311 .enum_value = regf_get_value,
2312 .get_sec_desc = regf_get_sec_desc,
2313 .set_sec_desc = regf_set_sec_desc,
2314 .add_key = regf_add_key,
2315 .set_value = regf_set_value,
2316 .del_key = regf_del_key,
2317 .delete_value = regf_del_value,
2318 .flush_key = regf_flush_key