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"
30 static struct hive_operations reg_backend_regf;
33 * There are several places on the web where the REGF format is explained;
39 * - Return error codes that make more sense
41 * - do more things in-memory
45 * Read HBIN blocks into memory
50 struct hbin_block **hbins;
51 struct regf_hdr *header;
55 static WERROR regf_save_hbin(struct regf_data *data, int flush);
57 struct regf_key_data {
59 struct regf_data *hive;
64 static struct hbin_block *hbin_by_offset(const struct regf_data *data,
65 uint32_t offset, uint32_t *rel_offset)
69 for (i = 0; data->hbins[i]; i++) {
70 if (offset >= data->hbins[i]->offset_from_first &&
71 offset < data->hbins[i]->offset_from_first+
72 data->hbins[i]->offset_to_next) {
73 if (rel_offset != NULL)
74 *rel_offset = offset - data->hbins[i]->offset_from_first - 0x20;
75 return data->hbins[i];
83 * Validate a regf header
84 * For now, do nothing, but we should check the checksum
86 static uint32_t regf_hdr_checksum(const uint8_t *buffer)
88 uint32_t checksum = 0, x;
91 for (i = 0; i < 0x01FB; i+= 4) {
100 * Obtain the contents of a HBIN block
102 static DATA_BLOB hbin_get(const struct regf_data *data, uint32_t offset)
105 struct hbin_block *hbin;
111 hbin = hbin_by_offset(data, offset, &rel_offset);
114 DEBUG(1, ("Can't find HBIN at 0x%04x\n", offset));
118 ret.length = IVAL(hbin->data, rel_offset);
119 if (!(ret.length & 0x80000000)) {
120 DEBUG(0, ("Trying to use dirty block at 0x%04x\n", offset));
124 /* remove high bit */
125 ret.length = (ret.length ^ 0xffffffff) + 1;
127 ret.length -= 4; /* 4 bytes for the length... */
128 ret.data = hbin->data +
129 (offset - hbin->offset_from_first - 0x20) + 4;
134 static bool hbin_get_tdr(struct regf_data *regf, uint32_t offset,
135 TALLOC_CTX *ctx, tdr_pull_fn_t pull_fn, void *p)
137 struct tdr_pull *pull = tdr_pull_init(regf);
139 pull->data = hbin_get(regf, offset);
140 if (!pull->data.data) {
141 DEBUG(1, ("Unable to get data at 0x%04x\n", offset));
146 if (NT_STATUS_IS_ERR(pull_fn(pull, ctx, p))) {
147 DEBUG(1, ("Error parsing record at 0x%04x using tdr\n",
157 /* Allocate some new data */
158 static DATA_BLOB hbin_alloc(struct regf_data *data, uint32_t size,
162 uint32_t rel_offset = (uint32_t) -1; /* Relative offset ! */
163 struct hbin_block *hbin = NULL;
169 return data_blob(NULL, 0);
171 size += 4; /* Need to include int32 for the length */
173 /* Allocate as a multiple of 8 */
174 size = (size + 7) & ~7;
179 for (i = 0; (hbin = data->hbins[i]); i++) {
182 for (j = 0; j < hbin->offset_to_next-0x20; j+= my_size) {
183 my_size = IVALS(hbin->data, j);
185 if (my_size == 0x0) {
186 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
190 if (my_size % 8 != 0) {
191 DEBUG(0, ("Encountered non-aligned block!\n"));
194 if (my_size < 0) { /* Used... */
196 } else if (my_size == size) { /* exact match */
198 DEBUG(4, ("Found free block of exact size %d in middle of HBIN\n",
201 } else if (my_size > size) { /* data will remain */
203 /* Split this block and mark the next block as free */
204 SIVAL(hbin->data, rel_offset+size, my_size-size);
205 DEBUG(4, ("Found free block of size %d (needing %d) in middle of HBIN\n",
211 if (rel_offset != -1)
215 /* No space available in previous hbins,
216 * allocate new one */
217 if (data->hbins[i] == NULL) {
218 DEBUG(4, ("No space available in other HBINs for block of size %d, allocating new HBIN\n",
221 /* Add extra hbin block */
222 data->hbins = talloc_realloc(data, data->hbins,
223 struct hbin_block *, i+2);
224 hbin = talloc(data->hbins, struct hbin_block);
225 SMB_ASSERT(hbin != NULL);
227 data->hbins[i] = hbin;
228 data->hbins[i+1] = NULL;
231 hbin->HBIN_ID = talloc_strdup(hbin, "hbin");
232 hbin->offset_from_first = (i == 0?0:data->hbins[i-1]->offset_from_first+data->hbins[i-1]->offset_to_next);
233 hbin->offset_to_next = 0x1000;
234 hbin->unknown[0] = 0;
235 hbin->unknown[1] = 0;
236 unix_to_nt_time(&hbin->last_change, time(NULL));
237 hbin->block_size = hbin->offset_to_next;
238 hbin->data = talloc_zero_array(hbin, uint8_t, hbin->block_size - 0x20);
239 /* Update the regf header */
240 data->header->last_block += hbin->offset_to_next;
242 /* Set the next block to it's proper size and set the
243 * rel_offset for this block */
244 SIVAL(hbin->data, size, hbin->block_size - size - 0x20);
248 /* Set size and mark as used */
249 SIVAL(hbin->data, rel_offset, -size);
251 ret.data = hbin->data + rel_offset + 0x4; /* Skip past length */
252 ret.length = size - 0x4;
254 uint32_t new_rel_offset;
255 *offset = hbin->offset_from_first + rel_offset + 0x20;
256 SMB_ASSERT(hbin_by_offset(data, *offset, &new_rel_offset) == hbin);
257 SMB_ASSERT(new_rel_offset == rel_offset);
263 /* Store a data blob. Return the offset at which it was stored */
264 static uint32_t hbin_store (struct regf_data *data, DATA_BLOB blob)
267 DATA_BLOB dest = hbin_alloc(data, blob.length, &ret);
269 memcpy(dest.data, blob.data, blob.length);
271 /* Make sure that we have no tailing garbage in the block */
272 if (dest.length > blob.length) {
273 memset(dest.data + blob.length, 0, dest.length - blob.length);
279 static uint32_t hbin_store_tdr(struct regf_data *data,
280 tdr_push_fn_t push_fn, void *p)
282 struct tdr_push *push = tdr_push_init(data);
285 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
286 DEBUG(0, ("Error during push\n"));
290 ret = hbin_store(data, push->data);
298 /* Free existing data */
299 static void hbin_free (struct regf_data *data, uint32_t offset)
304 struct hbin_block *hbin;
306 SMB_ASSERT (offset > 0);
308 hbin = hbin_by_offset(data, offset, &rel_offset);
313 /* Get original size */
314 size = IVALS(hbin->data, rel_offset);
317 DEBUG(1, ("Trying to free already freed block at 0x%04x\n",
324 /* If the next block is free, merge into big free block */
325 if (rel_offset + size < hbin->offset_to_next - 0x20) {
326 next_size = IVALS(hbin->data, rel_offset+size);
332 /* Write block size */
333 SIVALS(hbin->data, rel_offset, size);
337 * Store a data blob data was already stored, but has changed in size
338 * Will try to save it at the current location if possible, otherwise
339 * does a free + store */
340 static uint32_t hbin_store_resize(struct regf_data *data,
341 uint32_t orig_offset, DATA_BLOB blob)
344 struct hbin_block *hbin = hbin_by_offset(data, orig_offset,
349 int32_t possible_size;
352 SMB_ASSERT(orig_offset > 0);
355 return hbin_store(data, blob);
357 /* Get original size */
358 orig_size = -IVALS(hbin->data, rel_offset);
360 needed_size = blob.length + 4; /* Add int32 containing length */
361 needed_size = (needed_size + 7) & ~7; /* Align */
363 /* Fits into current allocated block */
364 if (orig_size >= needed_size) {
365 memcpy(hbin->data + rel_offset + 0x4, blob.data, blob.length);
366 /* If the difference in size is greater than 0x4, split the block
367 * and free/merge it */
368 if (orig_size - needed_size > 0x4) {
369 SIVALS(hbin->data, rel_offset, -needed_size);
370 SIVALS(hbin->data, rel_offset + needed_size,
371 needed_size-orig_size);
372 hbin_free(data, orig_offset + needed_size);
377 possible_size = orig_size;
379 /* Check if it can be combined with the next few free records */
380 for (i = rel_offset; i < hbin->offset_to_next - 0x20; i += my_size) {
381 if (IVALS(hbin->data, i) < 0) /* Used */
384 my_size = IVALS(hbin->data, i);
386 if (my_size == 0x0) {
387 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
390 possible_size += my_size;
393 if (possible_size >= blob.length) {
394 SIVAL(hbin->data, rel_offset, -possible_size);
395 memcpy(hbin->data + rel_offset + 0x4,
396 blob.data, blob.length);
401 hbin_free(data, orig_offset);
402 return hbin_store(data, blob);
405 static uint32_t hbin_store_tdr_resize(struct regf_data *regf,
406 tdr_push_fn_t push_fn,
407 uint32_t orig_offset, void *p)
409 struct tdr_push *push = tdr_push_init(regf);
412 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
413 DEBUG(0, ("Error during push\n"));
417 ret = hbin_store_resize(regf, orig_offset, push->data);
424 static uint32_t regf_create_lh_hash(const char *name)
430 hash_name = strupper_talloc(NULL, name);
431 for (i = 0; *(hash_name + i) != 0; i++) {
433 ret += *(hash_name + i);
435 talloc_free(hash_name);
439 static WERROR regf_get_info(TALLOC_CTX *mem_ctx,
440 const struct hive_key *key,
441 const char **classname,
442 uint32_t *num_subkeys,
443 uint32_t *num_values,
444 NTTIME *last_mod_time,
445 uint32_t *max_subkeynamelen,
446 uint32_t *max_valnamelen,
447 uint32_t *max_valbufsize)
449 const struct regf_key_data *private_data =
450 (const struct regf_key_data *)key;
452 if (num_subkeys != NULL)
453 *num_subkeys = private_data->nk->num_subkeys;
455 if (num_values != NULL)
456 *num_values = private_data->nk->num_values;
458 if (classname != NULL) {
459 if (private_data->nk->clsname_offset != -1) {
460 DATA_BLOB data = hbin_get(private_data->hive,
461 private_data->nk->clsname_offset);
462 *classname = talloc_strndup(mem_ctx,
464 private_data->nk->clsname_length);
465 W_ERROR_HAVE_NO_MEMORY(*classname);
470 /* TODO: Last mod time */
472 /* TODO: max valnamelen */
474 /* TODO: max valbufsize */
476 /* TODO: max subkeynamelen */
481 static struct regf_key_data *regf_get_key(TALLOC_CTX *ctx,
482 struct regf_data *regf,
486 struct regf_key_data *ret;
488 ret = talloc_zero(ctx, struct regf_key_data);
489 ret->key.ops = ®_backend_regf;
490 ret->hive = talloc_reference(ret, regf);
491 ret->offset = offset;
492 nk = talloc(ret, struct nk_block);
498 if (!hbin_get_tdr(regf, offset, nk,
499 (tdr_pull_fn_t)tdr_pull_nk_block, nk)) {
500 DEBUG(0, ("Unable to find HBIN data for offset 0x%x\n", offset));
504 if (strcmp(nk->header, "nk") != 0) {
505 DEBUG(0, ("Expected nk record, got %s\n", nk->header));
514 static WERROR regf_get_value(TALLOC_CTX *ctx, struct hive_key *key,
515 uint32_t idx, const char **name,
516 uint32_t *data_type, DATA_BLOB *data)
518 const struct regf_key_data *private_data =
519 (const struct regf_key_data *)key;
521 struct regf_data *regf = private_data->hive;
525 if (idx >= private_data->nk->num_values)
526 return WERR_NO_MORE_ITEMS;
528 tmp = hbin_get(regf, private_data->nk->values_offset);
530 DEBUG(0, ("Unable to find value list at 0x%x\n",
531 private_data->nk->values_offset));
532 return WERR_GENERAL_FAILURE;
535 if (tmp.length < private_data->nk->num_values * 4) {
536 DEBUG(1, ("Value counts mismatch\n"));
539 vk_offset = IVAL(tmp.data, idx * 4);
541 vk = talloc(NULL, struct vk_block);
542 W_ERROR_HAVE_NO_MEMORY(vk);
544 if (!hbin_get_tdr(regf, vk_offset, vk,
545 (tdr_pull_fn_t)tdr_pull_vk_block, vk)) {
546 DEBUG(0, ("Unable to get VK block at 0x%x\n", vk_offset));
548 return WERR_GENERAL_FAILURE;
551 /* FIXME: name character set ?*/
553 *name = talloc_strndup(ctx, vk->data_name, vk->name_length);
554 W_ERROR_HAVE_NO_MEMORY(*name);
557 if (data_type != NULL)
558 *data_type = vk->data_type;
560 if (vk->data_length & 0x80000000) {
561 /* this is data of type "REG_DWORD" or "REG_DWORD_BIG_ENDIAN" */
562 data->data = talloc_size(ctx, sizeof(uint32_t));
563 W_ERROR_HAVE_NO_MEMORY(data->data);
564 SIVAL(data->data, 0, vk->data_offset);
565 data->length = sizeof(uint32_t);
567 *data = hbin_get(regf, vk->data_offset);
570 if (data->length < vk->data_length) {
571 DEBUG(1, ("Read data less than indicated data length!\n"));
579 static WERROR regf_get_value_by_name(TALLOC_CTX *mem_ctx,
580 struct hive_key *key, const char *name,
581 uint32_t *type, DATA_BLOB *data)
587 /* FIXME: Do binary search? Is this list sorted at all? */
589 for (i = 0; W_ERROR_IS_OK(error = regf_get_value(mem_ctx, key, i,
590 &vname, type, data));
592 if (!strcmp(vname, name))
596 if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
603 static WERROR regf_get_subkey_by_index(TALLOC_CTX *ctx,
604 const struct hive_key *key,
605 uint32_t idx, const char **name,
606 const char **classname,
607 NTTIME *last_mod_time)
610 struct regf_key_data *ret;
611 const struct regf_key_data *private_data = (const struct regf_key_data *)key;
612 struct nk_block *nk = private_data->nk;
615 if (idx >= nk->num_subkeys)
616 return WERR_NO_MORE_ITEMS;
618 /* Make sure that we don't crash if the key is empty */
619 if (nk->subkeys_offset == -1) {
620 return WERR_NO_MORE_ITEMS;
623 data = hbin_get(private_data->hive, nk->subkeys_offset);
625 DEBUG(0, ("Unable to find subkey list at 0x%x\n",
626 nk->subkeys_offset));
627 return WERR_GENERAL_FAILURE;
630 if (!strncmp((char *)data.data, "li", 2)) {
632 struct tdr_pull *pull = tdr_pull_init(private_data->hive);
634 DEBUG(10, ("Subkeys in LI list\n"));
637 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, nk, &li))) {
638 DEBUG(0, ("Error parsing LI list\n"));
640 return WERR_GENERAL_FAILURE;
643 SMB_ASSERT(!strncmp(li.header, "li", 2));
645 if (li.key_count != nk->num_subkeys) {
646 DEBUG(0, ("Subkey counts don't match\n"));
647 return WERR_GENERAL_FAILURE;
649 key_off = li.nk_offset[idx];
651 } else if (!strncmp((char *)data.data, "lf", 2)) {
653 struct tdr_pull *pull = tdr_pull_init(private_data->hive);
655 DEBUG(10, ("Subkeys in LF list\n"));
658 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, nk, &lf))) {
659 DEBUG(0, ("Error parsing LF list\n"));
661 return WERR_GENERAL_FAILURE;
664 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
666 if (lf.key_count != nk->num_subkeys) {
667 DEBUG(0, ("Subkey counts don't match\n"));
668 return WERR_GENERAL_FAILURE;
671 key_off = lf.hr[idx].nk_offset;
672 } else if (!strncmp((char *)data.data, "lh", 2)) {
674 struct tdr_pull *pull = tdr_pull_init(private_data->hive);
676 DEBUG(10, ("Subkeys in LH list\n"));
679 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, nk, &lh))) {
680 DEBUG(0, ("Error parsing LH list\n"));
682 return WERR_GENERAL_FAILURE;
685 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
687 if (lh.key_count != nk->num_subkeys) {
688 DEBUG(0, ("Subkey counts don't match\n"));
689 return WERR_GENERAL_FAILURE;
691 key_off = lh.hr[idx].nk_offset;
692 } else if (!strncmp((char *)data.data, "ri", 2)) {
694 struct tdr_pull *pull = tdr_pull_init(ctx);
696 uint16_t sublist_count = 0;
698 DEBUG(10, ("Subkeys in RI list\n"));
701 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull, nk, &ri))) {
702 DEBUG(0, ("Error parsing RI list\n"));
704 return WERR_GENERAL_FAILURE;
706 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
708 for (i = 0; i < ri.key_count; i++) {
711 /* Get sublist data blob */
712 list_data = hbin_get(private_data->hive, ri.offset[i]);
713 if (!list_data.data) {
714 DEBUG(0, ("Error getting RI list."));
716 return WERR_GENERAL_FAILURE;
719 pull->data = list_data;
721 if (!strncmp((char *)list_data.data, "li", 2)) {
724 DEBUG(10, ("Subkeys in RI->LI list\n"));
726 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull,
729 DEBUG(0, ("Error parsing LI list from RI\n"));
731 return WERR_GENERAL_FAILURE;
733 SMB_ASSERT(!strncmp(li.header, "li", 2));
735 /* Advance to next sublist if necessary */
736 if (idx >= sublist_count + li.key_count) {
737 sublist_count += li.key_count;
740 key_off = li.nk_offset[idx - sublist_count];
741 sublist_count += li.key_count;
743 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
746 DEBUG(10, ("Subkeys in RI->LH list\n"));
748 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull,
751 DEBUG(0, ("Error parsing LH list from RI\n"));
753 return WERR_GENERAL_FAILURE;
755 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
757 /* Advance to next sublist if necessary */
758 if (idx >= sublist_count + lh.key_count) {
759 sublist_count += lh.key_count;
762 key_off = lh.hr[idx - sublist_count].nk_offset;
763 sublist_count += lh.key_count;
766 DEBUG(0,("Unknown sublist in ri block\n"));
769 return WERR_GENERAL_FAILURE;
776 if (idx > sublist_count) {
777 return WERR_NO_MORE_ITEMS;
781 DEBUG(0, ("Unknown type for subkey list (0x%04x): %c%c\n",
782 nk->subkeys_offset, data.data[0], data.data[1]));
783 return WERR_GENERAL_FAILURE;
786 ret = regf_get_key (ctx, private_data->hive, key_off);
788 if (classname != NULL) {
789 if (ret->nk->clsname_offset != -1) {
790 DATA_BLOB db = hbin_get(ret->hive,
791 ret->nk->clsname_offset);
792 *classname = talloc_strndup(ctx,
794 ret->nk->clsname_length);
795 W_ERROR_HAVE_NO_MEMORY(*classname);
800 if (last_mod_time != NULL)
801 *last_mod_time = ret->nk->last_change;
804 *name = talloc_steal(ctx, ret->nk->key_name);
811 static WERROR regf_match_subkey_by_name(TALLOC_CTX *ctx,
812 const struct hive_key *key,
814 const char *name, uint32_t *ret)
816 DATA_BLOB subkey_data;
817 struct nk_block subkey;
818 struct tdr_pull *pull;
819 const struct regf_key_data *private_data =
820 (const struct regf_key_data *)key;
822 subkey_data = hbin_get(private_data->hive, offset);
823 if (!subkey_data.data) {
824 DEBUG(0, ("Unable to retrieve subkey HBIN\n"));
825 return WERR_GENERAL_FAILURE;
828 pull = tdr_pull_init(ctx);
830 pull->data = subkey_data;
832 if (NT_STATUS_IS_ERR(tdr_pull_nk_block(pull, ctx, &subkey))) {
833 DEBUG(0, ("Error parsing NK structure.\n"));
835 return WERR_GENERAL_FAILURE;
839 if (strncmp(subkey.header, "nk", 2)) {
840 DEBUG(0, ("Not an NK structure.\n"));
841 return WERR_GENERAL_FAILURE;
844 if (!strcasecmp(subkey.key_name, name)) {
852 static WERROR regf_get_subkey_by_name(TALLOC_CTX *ctx,
853 const struct hive_key *key,
855 struct hive_key **ret)
858 const struct regf_key_data *private_data =
859 (const struct regf_key_data *)key;
860 struct nk_block *nk = private_data->nk;
861 uint32_t key_off = 0;
863 /* Make sure that we don't crash if the key is empty */
864 if (nk->subkeys_offset == -1) {
868 data = hbin_get(private_data->hive, nk->subkeys_offset);
870 DEBUG(0, ("Unable to find subkey list\n"));
871 return WERR_GENERAL_FAILURE;
874 if (!strncmp((char *)data.data, "li", 2)) {
876 struct tdr_pull *pull = tdr_pull_init(ctx);
879 DEBUG(10, ("Subkeys in LI list\n"));
882 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, nk, &li))) {
883 DEBUG(0, ("Error parsing LI list\n"));
885 return WERR_GENERAL_FAILURE;
888 SMB_ASSERT(!strncmp(li.header, "li", 2));
890 if (li.key_count != nk->num_subkeys) {
891 DEBUG(0, ("Subkey counts don't match\n"));
892 return WERR_GENERAL_FAILURE;
895 for (i = 0; i < li.key_count; i++) {
896 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
905 } else if (!strncmp((char *)data.data, "lf", 2)) {
907 struct tdr_pull *pull = tdr_pull_init(ctx);
910 DEBUG(10, ("Subkeys in LF list\n"));
913 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, nk, &lf))) {
914 DEBUG(0, ("Error parsing LF list\n"));
916 return WERR_GENERAL_FAILURE;
919 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
921 if (lf.key_count != nk->num_subkeys) {
922 DEBUG(0, ("Subkey counts don't match\n"));
923 return WERR_GENERAL_FAILURE;
926 for (i = 0; i < lf.key_count; i++) {
927 if (strncmp(lf.hr[i].hash, name, 4)) {
930 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
940 } else if (!strncmp((char *)data.data, "lh", 2)) {
942 struct tdr_pull *pull = tdr_pull_init(ctx);
946 DEBUG(10, ("Subkeys in LH list\n"));
949 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, nk, &lh))) {
950 DEBUG(0, ("Error parsing LH list\n"));
952 return WERR_GENERAL_FAILURE;
955 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
957 if (lh.key_count != nk->num_subkeys) {
958 DEBUG(0, ("Subkey counts don't match\n"));
959 return WERR_GENERAL_FAILURE;
962 hash = regf_create_lh_hash(name);
963 for (i = 0; i < lh.key_count; i++) {
964 if (lh.hr[i].base37 != hash) {
967 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
977 } else if (!strncmp((char *)data.data, "ri", 2)) {
979 struct tdr_pull *pull = tdr_pull_init(ctx);
982 DEBUG(10, ("Subkeys in RI list\n"));
985 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull, nk, &ri))) {
986 DEBUG(0, ("Error parsing RI list\n"));
988 return WERR_GENERAL_FAILURE;
990 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
992 for (i = 0; i < ri.key_count; i++) {
995 /* Get sublist data blob */
996 list_data = hbin_get(private_data->hive, ri.offset[i]);
997 if (list_data.data == NULL) {
998 DEBUG(0, ("Error getting RI list."));
1000 return WERR_GENERAL_FAILURE;
1003 pull->data = list_data;
1005 if (!strncmp((char *)list_data.data, "li", 2)) {
1008 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull,
1011 DEBUG(0, ("Error parsing LI list from RI\n"));
1013 return WERR_GENERAL_FAILURE;
1015 SMB_ASSERT(!strncmp(li.header, "li", 2));
1017 for (j = 0; j < li.key_count; j++) {
1018 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
1025 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
1029 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull,
1032 DEBUG(0, ("Error parsing LH list from RI\n"));
1034 return WERR_GENERAL_FAILURE;
1036 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1038 hash = regf_create_lh_hash(name);
1039 for (j = 0; j < lh.key_count; j++) {
1040 if (lh.hr[j].base37 != hash) {
1043 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
1056 return WERR_BADFILE;
1058 DEBUG(0, ("Unknown subkey list type.\n"));
1059 return WERR_GENERAL_FAILURE;
1062 *ret = (struct hive_key *)regf_get_key(ctx, private_data->hive,
1067 static WERROR regf_set_sec_desc(struct hive_key *key,
1068 const struct security_descriptor *sec_desc)
1070 const struct regf_key_data *private_data =
1071 (const struct regf_key_data *)key;
1072 struct sk_block cur_sk, sk, new_sk;
1073 struct regf_data *regf = private_data->hive;
1074 struct nk_block root;
1076 uint32_t sk_offset, cur_sk_offset;
1077 bool update_cur_sk = false;
1079 /* Get the root nk */
1080 hbin_get_tdr(regf, regf->header->data_offset, regf,
1081 (tdr_pull_fn_t) tdr_pull_nk_block, &root);
1083 /* Push the security descriptor to a blob */
1084 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf,
1085 sec_desc, (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
1086 DEBUG(0, ("Unable to push security descriptor\n"));
1087 return WERR_GENERAL_FAILURE;
1090 /* Get the current security descriptor for the key */
1091 if (!hbin_get_tdr(regf, private_data->nk->sk_offset, regf,
1092 (tdr_pull_fn_t) tdr_pull_sk_block, &cur_sk)) {
1093 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1094 return WERR_BADFILE;
1096 /* If there's no change, change nothing. */
1097 if (memcmp(data.data, cur_sk.sec_desc,
1098 MIN(data.length, cur_sk.rec_size)) == 0) {
1102 /* Delete the current sk if only this key is using it */
1103 if (cur_sk.ref_cnt == 1) {
1104 /* Get the previous security descriptor for the key */
1105 if (!hbin_get_tdr(regf, cur_sk.prev_offset, regf,
1106 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1107 DEBUG(0, ("Unable to find prev security descriptor for current key\n"));
1108 return WERR_BADFILE;
1110 /* Change and store the previous security descriptor */
1111 sk.next_offset = cur_sk.next_offset;
1112 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
1113 cur_sk.prev_offset, &sk);
1115 /* Get the next security descriptor for the key */
1116 if (!hbin_get_tdr(regf, cur_sk.next_offset, regf,
1117 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1118 DEBUG(0, ("Unable to find next security descriptor for current key\n"));
1119 return WERR_BADFILE;
1121 /* Change and store the next security descriptor */
1122 sk.prev_offset = cur_sk.prev_offset;
1123 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
1124 cur_sk.next_offset, &sk);
1126 hbin_free(regf, private_data->nk->sk_offset);
1128 /* This key will no longer be referring to this sk */
1130 update_cur_sk = true;
1133 sk_offset = root.sk_offset;
1136 cur_sk_offset = sk_offset;
1137 if (!hbin_get_tdr(regf, sk_offset, regf,
1138 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1139 DEBUG(0, ("Unable to find security descriptor\n"));
1140 return WERR_BADFILE;
1142 if (memcmp(data.data, sk.sec_desc, MIN(data.length, sk.rec_size)) == 0) {
1143 private_data->nk->sk_offset = sk_offset;
1145 hbin_store_tdr_resize(regf,
1146 (tdr_push_fn_t) tdr_push_sk_block,
1148 hbin_store_tdr_resize(regf,
1149 (tdr_push_fn_t) tdr_push_nk_block,
1150 private_data->offset,
1154 sk_offset = sk.next_offset;
1155 } while (sk_offset != root.sk_offset);
1157 ZERO_STRUCT(new_sk);
1158 new_sk.header = "sk";
1159 new_sk.prev_offset = cur_sk_offset;
1160 new_sk.next_offset = root.sk_offset;
1162 new_sk.rec_size = data.length;
1163 new_sk.sec_desc = data.data;
1165 sk_offset = hbin_store_tdr(regf,
1166 (tdr_push_fn_t) tdr_push_sk_block,
1168 if (sk_offset == -1) {
1169 DEBUG(0, ("Error storing sk block\n"));
1170 return WERR_GENERAL_FAILURE;
1172 private_data->nk->sk_offset = sk_offset;
1174 if (update_cur_sk) {
1175 hbin_store_tdr_resize(regf,
1176 (tdr_push_fn_t) tdr_push_sk_block,
1177 private_data->nk->sk_offset, &cur_sk);
1180 /* Get the previous security descriptor for the key */
1181 if (!hbin_get_tdr(regf, new_sk.prev_offset, regf,
1182 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1183 DEBUG(0, ("Unable to find security descriptor for previous key\n"));
1184 return WERR_BADFILE;
1186 /* Change and store the previous security descriptor */
1187 sk.next_offset = sk_offset;
1188 hbin_store_tdr_resize(regf,
1189 (tdr_push_fn_t) tdr_push_sk_block,
1190 cur_sk.prev_offset, &sk);
1192 /* Get the next security descriptor for the key (always root, as we append) */
1193 if (!hbin_get_tdr(regf, new_sk.next_offset, regf,
1194 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1195 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1196 return WERR_BADFILE;
1198 /* Change and store the next security descriptor (always root, as we append) */
1199 sk.prev_offset = sk_offset;
1200 hbin_store_tdr_resize(regf,
1201 (tdr_push_fn_t) tdr_push_sk_block,
1202 root.sk_offset, &sk);
1206 hbin_store_tdr_resize(regf,
1207 (tdr_push_fn_t) tdr_push_sk_block,
1208 private_data->offset, private_data->nk);
1212 static WERROR regf_get_sec_desc(TALLOC_CTX *ctx, const struct hive_key *key,
1213 struct security_descriptor **sd)
1215 const struct regf_key_data *private_data =
1216 (const struct regf_key_data *)key;
1218 struct regf_data *regf = private_data->hive;
1221 if (!hbin_get_tdr(regf, private_data->nk->sk_offset, ctx,
1222 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1223 DEBUG(0, ("Unable to find security descriptor\n"));
1224 return WERR_GENERAL_FAILURE;
1227 if (strcmp(sk.header, "sk") != 0) {
1228 DEBUG(0, ("Expected 'sk', got '%s'\n", sk.header));
1229 return WERR_GENERAL_FAILURE;
1232 *sd = talloc(ctx, struct security_descriptor);
1233 W_ERROR_HAVE_NO_MEMORY(*sd);
1235 data.data = sk.sec_desc;
1236 data.length = sk.rec_size;
1237 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_struct_blob(&data, ctx, *sd,
1238 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor))) {
1239 DEBUG(0, ("Error parsing security descriptor\n"));
1240 return WERR_GENERAL_FAILURE;
1246 static WERROR regf_sl_add_entry(struct regf_data *regf, uint32_t list_offset,
1248 uint32_t key_offset, uint32_t *ret)
1252 /* Create a new key if necessary */
1253 if (list_offset == -1) {
1254 if (regf->header->version.major != 1) {
1255 DEBUG(0, ("Can't store keys in unknown registry format\n"));
1256 return WERR_NOT_SUPPORTED;
1258 if (regf->header->version.minor < 3) {
1265 li.nk_offset = talloc_array(regf, uint32_t, 1);
1266 W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1267 li.nk_offset[0] = key_offset;
1269 *ret = hbin_store_tdr(regf,
1270 (tdr_push_fn_t) tdr_push_li_block,
1273 talloc_free(li.nk_offset);
1274 } else if (regf->header->version.minor == 3 ||
1275 regf->header->version.minor == 4) {
1282 lf.hr = talloc_array(regf, struct hash_record, 1);
1283 W_ERROR_HAVE_NO_MEMORY(lf.hr);
1284 lf.hr[0].nk_offset = key_offset;
1285 lf.hr[0].hash = talloc_strndup(lf.hr, name, 4);
1286 W_ERROR_HAVE_NO_MEMORY(lf.hr[0].hash);
1288 *ret = hbin_store_tdr(regf,
1289 (tdr_push_fn_t) tdr_push_lf_block,
1293 } else if (regf->header->version.minor == 5) {
1300 lh.hr = talloc_array(regf, struct lh_hash, 1);
1301 W_ERROR_HAVE_NO_MEMORY(lh.hr);
1302 lh.hr[0].nk_offset = key_offset;
1303 lh.hr[0].base37 = regf_create_lh_hash(name);
1305 *ret = hbin_store_tdr(regf,
1306 (tdr_push_fn_t) tdr_push_lh_block,
1314 data = hbin_get(regf, list_offset);
1316 DEBUG(0, ("Unable to find subkey list\n"));
1317 return WERR_BADFILE;
1320 if (!strncmp((char *)data.data, "li", 2)) {
1321 struct tdr_pull *pull = tdr_pull_init(regf);
1326 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, regf, &li))) {
1327 DEBUG(0, ("Error parsing LI list\n"));
1329 return WERR_BADFILE;
1333 if (strncmp(li.header, "li", 2) != 0) {
1335 DEBUG(0, ("LI header corrupt\n"));
1336 return WERR_BADFILE;
1339 li.nk_offset = talloc_realloc(regf, li.nk_offset,
1340 uint32_t, li.key_count+1);
1341 W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1342 li.nk_offset[li.key_count] = key_offset;
1344 *ret = hbin_store_tdr_resize(regf,
1345 (tdr_push_fn_t)tdr_push_li_block,
1348 talloc_free(li.nk_offset);
1349 } else if (!strncmp((char *)data.data, "lf", 2)) {
1350 struct tdr_pull *pull = tdr_pull_init(regf);
1355 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, regf, &lf))) {
1356 DEBUG(0, ("Error parsing LF list\n"));
1358 return WERR_BADFILE;
1361 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1363 lf.hr = talloc_realloc(regf, lf.hr, struct hash_record,
1365 W_ERROR_HAVE_NO_MEMORY(lf.hr);
1366 lf.hr[lf.key_count].nk_offset = key_offset;
1367 lf.hr[lf.key_count].hash = talloc_strndup(lf.hr, name, 4);
1368 W_ERROR_HAVE_NO_MEMORY(lf.hr[lf.key_count].hash);
1370 *ret = hbin_store_tdr_resize(regf,
1371 (tdr_push_fn_t)tdr_push_lf_block,
1375 } else if (!strncmp((char *)data.data, "lh", 2)) {
1376 struct tdr_pull *pull = tdr_pull_init(regf);
1381 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, regf, &lh))) {
1382 DEBUG(0, ("Error parsing LH list\n"));
1384 return WERR_BADFILE;
1387 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1389 lh.hr = talloc_realloc(regf, lh.hr, struct lh_hash,
1391 W_ERROR_HAVE_NO_MEMORY(lh.hr);
1392 lh.hr[lh.key_count].nk_offset = key_offset;
1393 lh.hr[lh.key_count].base37 = regf_create_lh_hash(name);
1395 *ret = hbin_store_tdr_resize(regf,
1396 (tdr_push_fn_t)tdr_push_lh_block,
1400 } else if (!strncmp((char *)data.data, "ri", 2)) {
1402 DEBUG(0, ("Adding to 'ri' subkey list is not supported yet.\n"));
1403 return WERR_NOT_SUPPORTED;
1405 DEBUG(0, ("Cannot add to unknown subkey list\n"));
1406 return WERR_BADFILE;
1412 static WERROR regf_sl_del_entry(struct regf_data *regf, uint32_t list_offset,
1413 uint32_t key_offset, uint32_t *ret)
1417 data = hbin_get(regf, list_offset);
1419 DEBUG(0, ("Unable to find subkey list\n"));
1420 return WERR_BADFILE;
1423 if (strncmp((char *)data.data, "li", 2) == 0) {
1425 struct tdr_pull *pull = tdr_pull_init(regf);
1427 bool found_offset = false;
1429 DEBUG(10, ("Subkeys in LI list\n"));
1433 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, regf, &li))) {
1434 DEBUG(0, ("Error parsing LI list\n"));
1436 return WERR_BADFILE;
1440 SMB_ASSERT(!strncmp(li.header, "li", 2));
1442 for (i = 0; i < li.key_count; i++) {
1444 li.nk_offset[i-1] = li.nk_offset[i];
1446 if (li.nk_offset[i] == key_offset) {
1447 found_offset = true;
1451 if (!found_offset) {
1452 DEBUG(2, ("Subkey not found\n"));
1453 return WERR_BADFILE;
1457 /* If the there are no entries left, free the subkey list */
1458 if (li.key_count == 0) {
1459 hbin_free(regf, list_offset);
1463 /* Store li block */
1464 *ret = hbin_store_tdr_resize(regf,
1465 (tdr_push_fn_t) tdr_push_li_block,
1467 } else if (strncmp((char *)data.data, "lf", 2) == 0) {
1469 struct tdr_pull *pull = tdr_pull_init(regf);
1471 bool found_offset = false;
1473 DEBUG(10, ("Subkeys in LF list\n"));
1477 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, regf, &lf))) {
1478 DEBUG(0, ("Error parsing LF list\n"));
1480 return WERR_BADFILE;
1484 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1486 for (i = 0; i < lf.key_count; i++) {
1488 lf.hr[i-1] = lf.hr[i];
1491 if (lf.hr[i].nk_offset == key_offset) {
1496 if (!found_offset) {
1497 DEBUG(2, ("Subkey not found\n"));
1498 return WERR_BADFILE;
1502 /* If the there are no entries left, free the subkey list */
1503 if (lf.key_count == 0) {
1504 hbin_free(regf, list_offset);
1509 /* Store lf block */
1510 *ret = hbin_store_tdr_resize(regf,
1511 (tdr_push_fn_t) tdr_push_lf_block,
1513 } else if (strncmp((char *)data.data, "lh", 2) == 0) {
1515 struct tdr_pull *pull = tdr_pull_init(regf);
1517 bool found_offset = false;
1519 DEBUG(10, ("Subkeys in LH list\n"));
1523 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, regf, &lh))) {
1524 DEBUG(0, ("Error parsing LF list\n"));
1526 return WERR_BADFILE;
1530 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1532 for (i = 0; i < lh.key_count; i++) {
1534 lh.hr[i-1] = lh.hr[i];
1537 if (lh.hr[i].nk_offset == key_offset) {
1542 if (!found_offset) {
1543 DEBUG(0, ("Subkey not found\n"));
1544 return WERR_BADFILE;
1548 /* If the there are no entries left, free the subkey list */
1549 if (lh.key_count == 0) {
1550 hbin_free(regf, list_offset);
1555 /* Store lh block */
1556 *ret = hbin_store_tdr_resize(regf,
1557 (tdr_push_fn_t) tdr_push_lh_block,
1559 } else if (strncmp((char *)data.data, "ri", 2) == 0) {
1561 DEBUG(0, ("Sorry, deletion from ri block is not supported yet.\n"));
1562 return WERR_NOT_SUPPORTED;
1564 DEBUG (0, ("Unknown header found in subkey list.\n"));
1565 return WERR_BADFILE;
1570 static WERROR regf_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
1573 struct regf_key_data *private_data = (struct regf_key_data *)key;
1574 struct regf_data *regf = private_data->hive;
1575 struct nk_block *nk = private_data->nk;
1578 bool found_offset = false;
1582 if (nk->values_offset == -1) {
1583 return WERR_BADFILE;
1586 values = hbin_get(regf, nk->values_offset);
1588 for (i = 0; i < nk->num_values; i++) {
1590 ((uint32_t *)values.data)[i-1] = ((uint32_t *) values.data)[i];
1592 vk_offset = IVAL(values.data, i * 4);
1593 if (!hbin_get_tdr(regf, vk_offset, private_data,
1594 (tdr_pull_fn_t)tdr_pull_vk_block,
1596 DEBUG(0, ("Unable to get VK block at %d\n",
1598 return WERR_BADFILE;
1600 if (strcmp(vk.data_name, name) == 0) {
1601 hbin_free(regf, vk_offset);
1602 found_offset = true;
1606 if (!found_offset) {
1607 return WERR_BADFILE;
1610 values.length = (nk->num_values)*4;
1613 /* Store values list and nk */
1614 if (nk->num_values == 0) {
1615 hbin_free(regf, nk->values_offset);
1616 nk->values_offset = -1;
1618 nk->values_offset = hbin_store_resize(regf,
1622 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
1623 private_data->offset, nk);
1625 return regf_save_hbin(private_data->hive, 0);
1629 static WERROR regf_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
1632 const struct regf_key_data *private_data =
1633 (const struct regf_key_data *)parent;
1634 struct regf_key_data *key;
1635 struct nk_block *parent_nk;
1638 SMB_ASSERT(private_data);
1640 parent_nk = private_data->nk;
1642 if (parent_nk->subkeys_offset == -1) {
1643 DEBUG(4, ("Subkey list is empty, this key cannot contain subkeys.\n"));
1644 return WERR_BADFILE;
1648 if (!W_ERROR_IS_OK(regf_get_subkey_by_name(parent_nk, parent, name,
1649 (struct hive_key **)&key))) {
1650 DEBUG(2, ("Key '%s' not found\n", name));
1651 return WERR_BADFILE;
1654 if (key->nk->subkeys_offset != -1) {
1656 struct hive_key *sk = (struct hive_key *)key;
1657 unsigned int i = key->nk->num_subkeys;
1659 /* Get subkey information. */
1660 error = regf_get_subkey_by_index(parent_nk, sk, 0,
1661 (const char **)&sk_name,
1663 if (!W_ERROR_IS_OK(error)) {
1664 DEBUG(0, ("Can't retrieve subkey by index.\n"));
1668 /* Delete subkey. */
1669 error = regf_del_key(NULL, sk, sk_name);
1670 if (!W_ERROR_IS_OK(error)) {
1671 DEBUG(0, ("Can't delete key '%s'.\n", sk_name));
1675 talloc_free(sk_name);
1679 if (key->nk->values_offset != -1) {
1681 struct hive_key *sk = (struct hive_key *)key;
1683 unsigned int i = key->nk->num_values;
1685 /* Get value information. */
1686 error = regf_get_value(parent_nk, sk, 0,
1687 (const char **)&val_name,
1689 if (!W_ERROR_IS_OK(error)) {
1690 DEBUG(0, ("Can't retrieve value by index.\n"));
1695 error = regf_del_value(NULL, sk, val_name);
1696 if (!W_ERROR_IS_OK(error)) {
1697 DEBUG(0, ("Can't delete value '%s'.\n", val_name));
1701 talloc_free(val_name);
1705 /* Delete it from the subkey list. */
1706 error = regf_sl_del_entry(private_data->hive, parent_nk->subkeys_offset,
1707 key->offset, &parent_nk->subkeys_offset);
1708 if (!W_ERROR_IS_OK(error)) {
1709 DEBUG(0, ("Can't store new subkey list for parent key. Won't delete.\n"));
1713 /* Re-store parent key */
1714 parent_nk->num_subkeys--;
1715 hbin_store_tdr_resize(private_data->hive,
1716 (tdr_push_fn_t) tdr_push_nk_block,
1717 private_data->offset, parent_nk);
1719 if (key->nk->clsname_offset != -1) {
1720 hbin_free(private_data->hive, key->nk->clsname_offset);
1722 hbin_free(private_data->hive, key->offset);
1724 return regf_save_hbin(private_data->hive, 0);
1727 static WERROR regf_add_key(TALLOC_CTX *ctx, const struct hive_key *parent,
1728 const char *name, const char *classname,
1729 struct security_descriptor *sec_desc,
1730 struct hive_key **ret)
1732 const struct regf_key_data *private_data =
1733 (const struct regf_key_data *)parent;
1734 struct nk_block *parent_nk = private_data->nk, nk;
1735 struct nk_block *root;
1736 struct regf_data *regf = private_data->hive;
1741 nk.type = REG_SUB_KEY;
1742 unix_to_nt_time(&nk.last_change, time(NULL));
1744 nk.parent_offset = private_data->offset;
1747 nk.subkeys_offset = -1;
1748 nk.unknown_offset = -1;
1750 nk.values_offset = -1;
1751 memset(nk.unk3, 0, sizeof(nk.unk3));
1752 nk.clsname_offset = -1; /* FIXME: fill in */
1753 nk.clsname_length = 0;
1756 /* Get the security descriptor of the root key */
1757 root = talloc_zero(ctx, struct nk_block);
1758 W_ERROR_HAVE_NO_MEMORY(root);
1760 if (!hbin_get_tdr(regf, regf->header->data_offset, root,
1761 (tdr_pull_fn_t)tdr_pull_nk_block, root)) {
1762 DEBUG(0, ("Unable to find HBIN data for offset 0x%x\n",
1763 regf->header->data_offset));
1764 return WERR_GENERAL_FAILURE;
1766 nk.sk_offset = root->sk_offset;
1769 /* Store the new nk key */
1770 offset = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_nk_block, &nk);
1772 error = regf_sl_add_entry(regf, parent_nk->subkeys_offset, name, offset,
1773 &parent_nk->subkeys_offset);
1774 if (!W_ERROR_IS_OK(error)) {
1775 hbin_free(regf, offset);
1779 parent_nk->num_subkeys++;
1781 /* Since the subkey offset of the parent can change, store it again */
1782 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
1783 nk.parent_offset, parent_nk);
1785 *ret = (struct hive_key *)regf_get_key(ctx, regf, offset);
1787 DEBUG(9, ("Storing key %s\n", name));
1788 return regf_save_hbin(private_data->hive, 0);
1791 static WERROR regf_set_value(struct hive_key *key, const char *name,
1792 uint32_t type, const DATA_BLOB data)
1794 struct regf_key_data *private_data = (struct regf_key_data *)key;
1795 struct regf_data *regf = private_data->hive;
1796 struct nk_block *nk = private_data->nk;
1799 uint32_t tmp_vk_offset, vk_offset, old_vk_offset = (uint32_t) -1;
1804 /* find the value offset, if it exists */
1805 if (nk->values_offset != -1) {
1806 values = hbin_get(regf, nk->values_offset);
1808 for (i = 0; i < nk->num_values; i++) {
1809 tmp_vk_offset = IVAL(values.data, i * 4);
1810 if (!hbin_get_tdr(regf, tmp_vk_offset, private_data,
1811 (tdr_pull_fn_t)tdr_pull_vk_block,
1813 DEBUG(0, ("Unable to get VK block at 0x%x\n",
1815 return WERR_GENERAL_FAILURE;
1817 if (strcmp(vk.data_name, name) == 0) {
1818 old_vk_offset = tmp_vk_offset;
1824 /* If it's new, create the vk struct, if it's old, free the old data. */
1825 if (old_vk_offset == -1) {
1827 vk.name_length = strlen(name);
1828 if (name != NULL && name[0] != 0) {
1830 vk.data_name = name;
1832 vk.data_name = NULL;
1836 /* Free data, if any */
1837 if (!(vk.data_length & 0x80000000)) {
1838 hbin_free(regf, vk.data_offset);
1842 /* Set the type and data */
1843 vk.data_length = data.length;
1844 vk.data_type = type;
1845 if ((type == REG_DWORD) || (type == REG_DWORD_BIG_ENDIAN)) {
1846 if (vk.data_length != sizeof(uint32_t)) {
1847 DEBUG(0, ("DWORD or DWORD_BIG_ENDIAN value with size other than 4 byte!\n"));
1848 return WERR_NOT_SUPPORTED;
1850 vk.data_length |= 0x80000000;
1851 vk.data_offset = IVAL(data.data, 0);
1853 /* Store data somewhere */
1854 vk.data_offset = hbin_store(regf, data);
1856 if (old_vk_offset == -1) {
1858 vk_offset = hbin_store_tdr(regf,
1859 (tdr_push_fn_t) tdr_push_vk_block,
1862 /* Store vk at offset */
1863 vk_offset = hbin_store_tdr_resize(regf,
1864 (tdr_push_fn_t) tdr_push_vk_block,
1865 old_vk_offset ,&vk);
1868 /* Re-allocate the value list */
1869 if (nk->values_offset == -1) {
1870 nk->values_offset = hbin_store_tdr(regf,
1871 (tdr_push_fn_t) tdr_push_uint32,
1876 /* Change if we're changing, otherwise we're adding the value */
1877 if (old_vk_offset != -1) {
1878 /* Find and overwrite the offset. */
1879 for (i = 0; i < nk->num_values; i++) {
1880 if (IVAL(values.data, i * 4) == old_vk_offset) {
1881 SIVAL(values.data, i * 4, vk_offset);
1886 /* Create a new value list */
1887 DATA_BLOB value_list;
1889 value_list.length = (nk->num_values+1)*4;
1890 value_list.data = (uint8_t *)talloc_array(private_data,
1893 W_ERROR_HAVE_NO_MEMORY(value_list.data);
1894 memcpy(value_list.data, values.data, nk->num_values * 4);
1896 SIVAL(value_list.data, nk->num_values * 4, vk_offset);
1898 nk->values_offset = hbin_store_resize(regf,
1904 hbin_store_tdr_resize(regf,
1905 (tdr_push_fn_t) tdr_push_nk_block,
1906 private_data->offset, nk);
1907 return regf_save_hbin(private_data->hive, 0);
1910 static WERROR regf_save_hbin(struct regf_data *regf, int flush)
1912 struct tdr_push *push = tdr_push_init(regf);
1915 W_ERROR_HAVE_NO_MEMORY(push);
1917 /* Only write once every 5 seconds, or when flush is set */
1918 if (!flush && regf->last_write + 5 >= time(NULL)) {
1922 regf->last_write = time(NULL);
1924 if (lseek(regf->fd, 0, SEEK_SET) == -1) {
1925 DEBUG(0, ("Error lseeking in regf file\n"));
1926 return WERR_GENERAL_FAILURE;
1929 /* Recompute checksum */
1930 if (NT_STATUS_IS_ERR(tdr_push_regf_hdr(push, regf->header))) {
1931 DEBUG(0, ("Failed to push regf header\n"));
1932 return WERR_GENERAL_FAILURE;
1934 regf->header->chksum = regf_hdr_checksum(push->data.data);
1937 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
1938 (tdr_push_fn_t)tdr_push_regf_hdr,
1940 DEBUG(0, ("Error writing registry file header\n"));
1941 return WERR_GENERAL_FAILURE;
1944 if (lseek(regf->fd, 0x1000, SEEK_SET) == -1) {
1945 DEBUG(0, ("Error lseeking to 0x1000 in regf file\n"));
1946 return WERR_GENERAL_FAILURE;
1949 for (i = 0; regf->hbins[i]; i++) {
1950 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
1951 (tdr_push_fn_t)tdr_push_hbin_block,
1953 DEBUG(0, ("Error writing HBIN block\n"));
1954 return WERR_GENERAL_FAILURE;
1961 WERROR reg_create_regf_file(TALLOC_CTX *parent_ctx,
1962 const char *location,
1963 int minor_version, struct hive_key **key)
1965 struct regf_data *regf;
1966 struct regf_hdr *regf_hdr;
1971 struct security_descriptor *sd;
1974 regf = (struct regf_data *)talloc_zero(NULL, struct regf_data);
1976 W_ERROR_HAVE_NO_MEMORY(regf);
1978 DEBUG(5, ("Attempting to create registry file\n"));
1980 /* Get the header */
1981 regf->fd = creat(location, 0644);
1983 if (regf->fd == -1) {
1984 DEBUG(0,("Could not create file: %s, %s\n", location,
1987 return WERR_GENERAL_FAILURE;
1990 regf_hdr = talloc_zero(regf, struct regf_hdr);
1991 W_ERROR_HAVE_NO_MEMORY(regf_hdr);
1992 regf_hdr->REGF_ID = "regf";
1993 unix_to_nt_time(®f_hdr->modtime, time(NULL));
1994 regf_hdr->version.major = 1;
1995 regf_hdr->version.minor = minor_version;
1996 regf_hdr->last_block = 0x1000; /* Block size */
1997 regf_hdr->description = talloc_strdup(regf_hdr,
1998 "Registry created by Samba 4");
1999 W_ERROR_HAVE_NO_MEMORY(regf_hdr->description);
2000 regf_hdr->chksum = 0;
2002 regf->header = regf_hdr;
2004 /* Create all hbin blocks */
2005 regf->hbins = talloc_array(regf, struct hbin_block *, 1);
2006 W_ERROR_HAVE_NO_MEMORY(regf->hbins);
2007 regf->hbins[0] = NULL;
2010 nk.type = REG_ROOT_KEY;
2011 unix_to_nt_time(&nk.last_change, time(NULL));
2013 nk.parent_offset = -1;
2016 nk.subkeys_offset = -1;
2017 nk.unknown_offset = -1;
2019 nk.values_offset = -1;
2020 memset(nk.unk3, 0, 5);
2021 nk.clsname_offset = -1;
2022 nk.clsname_length = 0;
2023 nk.sk_offset = 0x80;
2024 nk.key_name = "SambaRootKey";
2027 * It should be noted that changing the key_name to something shorter
2028 * creates a shorter nk block, which makes the position of the sk block
2029 * change. All Windows registries I've seen have the sk at 0x80.
2030 * I therefore recommend that our regf files share that offset -- Wilco
2033 /* Create a security descriptor. */
2034 sd = security_descriptor_dacl_create(regf,
2037 SID_NT_AUTHENTICATED_USERS,
2038 SEC_ACE_TYPE_ACCESS_ALLOWED,
2040 SEC_ACE_FLAG_OBJECT_INHERIT,
2043 /* Push the security descriptor to a blob */
2044 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf,
2045 sd, (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
2046 DEBUG(0, ("Unable to push security descriptor\n"));
2047 return WERR_GENERAL_FAILURE;
2052 sk.prev_offset = 0x80;
2053 sk.next_offset = 0x80;
2055 sk.rec_size = data.length;
2056 sk.sec_desc = data.data;
2058 /* Store the new nk key */
2059 regf->header->data_offset = hbin_store_tdr(regf,
2060 (tdr_push_fn_t)tdr_push_nk_block,
2062 /* Store the sk block */
2063 sk_offset = hbin_store_tdr(regf,
2064 (tdr_push_fn_t) tdr_push_sk_block,
2066 if (sk_offset != 0x80) {
2067 DEBUG(0, ("Error storing sk block, should be at 0x80, stored at 0x%x\n", nk.sk_offset));
2068 return WERR_GENERAL_FAILURE;
2072 *key = (struct hive_key *)regf_get_key(parent_ctx, regf,
2073 regf->header->data_offset);
2075 error = regf_save_hbin(regf, 1);
2076 if (!W_ERROR_IS_OK(error)) {
2080 /* We can drop our own reference now that *key will have created one */
2081 talloc_unlink(NULL, regf);
2086 static WERROR regf_flush_key(struct hive_key *key)
2088 struct regf_key_data *private_data = (struct regf_key_data *)key;
2089 struct regf_data *regf = private_data->hive;
2092 error = regf_save_hbin(regf, 1);
2093 if (!W_ERROR_IS_OK(error)) {
2094 DEBUG(0, ("Failed to flush regf to disk\n"));
2101 static int regf_destruct(struct regf_data *regf)
2106 error = regf_save_hbin(regf, 1);
2107 if (!W_ERROR_IS_OK(error)) {
2108 DEBUG(0, ("Failed to flush registry to disk\n"));
2112 /* Close file descriptor */
2118 WERROR reg_open_regf_file(TALLOC_CTX *parent_ctx, const char *location,
2119 struct hive_key **key)
2121 struct regf_data *regf;
2122 struct regf_hdr *regf_hdr;
2123 struct tdr_pull *pull;
2126 regf = (struct regf_data *)talloc_zero(parent_ctx, struct regf_data);
2127 W_ERROR_HAVE_NO_MEMORY(regf);
2129 talloc_set_destructor(regf, regf_destruct);
2131 DEBUG(5, ("Attempting to load registry file\n"));
2133 /* Get the header */
2134 regf->fd = open(location, O_RDWR);
2136 if (regf->fd == -1) {
2137 DEBUG(0,("Could not load file: %s, %s\n", location,
2140 return WERR_GENERAL_FAILURE;
2143 pull = tdr_pull_init(regf);
2145 pull->data.data = (uint8_t*)fd_load(regf->fd, &pull->data.length, 0, regf);
2147 if (pull->data.data == NULL) {
2148 DEBUG(0, ("Error reading data\n"));
2150 return WERR_GENERAL_FAILURE;
2153 regf_hdr = talloc(regf, struct regf_hdr);
2154 W_ERROR_HAVE_NO_MEMORY(regf_hdr);
2156 if (NT_STATUS_IS_ERR(tdr_pull_regf_hdr(pull, regf_hdr, regf_hdr))) {
2158 return WERR_GENERAL_FAILURE;
2161 regf->header = regf_hdr;
2163 if (strcmp(regf_hdr->REGF_ID, "regf") != 0) {
2164 DEBUG(0, ("Unrecognized NT registry header id: %s, %s\n",
2165 regf_hdr->REGF_ID, location));
2167 return WERR_GENERAL_FAILURE;
2170 /* Validate the header ... */
2171 if (regf_hdr_checksum(pull->data.data) != regf_hdr->chksum) {
2172 DEBUG(0, ("Registry file checksum error: %s: %d,%d\n",
2173 location, regf_hdr->chksum,
2174 regf_hdr_checksum(pull->data.data)));
2176 return WERR_GENERAL_FAILURE;
2179 pull->offset = 0x1000;
2182 /* Read in all hbin blocks */
2183 regf->hbins = talloc_array(regf, struct hbin_block *, 1);
2184 W_ERROR_HAVE_NO_MEMORY(regf->hbins);
2186 regf->hbins[0] = NULL;
2188 while (pull->offset < pull->data.length &&
2189 pull->offset <= regf->header->last_block) {
2190 struct hbin_block *hbin = talloc(regf->hbins,
2193 W_ERROR_HAVE_NO_MEMORY(hbin);
2195 if (NT_STATUS_IS_ERR(tdr_pull_hbin_block(pull, hbin, hbin))) {
2196 DEBUG(0, ("[%d] Error parsing HBIN block\n", i));
2201 if (strcmp(hbin->HBIN_ID, "hbin") != 0) {
2202 DEBUG(0, ("[%d] Expected 'hbin', got '%s'\n",
2208 regf->hbins[i] = hbin;
2210 regf->hbins = talloc_realloc(regf, regf->hbins,
2211 struct hbin_block *, i+2);
2212 regf->hbins[i] = NULL;
2217 DEBUG(1, ("%d HBIN blocks read\n", i));
2219 *key = (struct hive_key *)regf_get_key(parent_ctx, regf,
2220 regf->header->data_offset);
2222 /* We can drop our own reference now that *key will have created one */
2223 talloc_unlink(parent_ctx, regf);
2228 static struct hive_operations reg_backend_regf = {
2230 .get_key_info = regf_get_info,
2231 .enum_key = regf_get_subkey_by_index,
2232 .get_key_by_name = regf_get_subkey_by_name,
2233 .get_value_by_name = regf_get_value_by_name,
2234 .enum_value = regf_get_value,
2235 .get_sec_desc = regf_get_sec_desc,
2236 .set_sec_desc = regf_set_sec_desc,
2237 .add_key = regf_add_key,
2238 .set_value = regf_set_value,
2239 .del_key = regf_del_key,
2240 .delete_value = regf_del_value,
2241 .flush_key = regf_flush_key