2 Samba CIFS implementation
3 Registry backend for REGF files
4 Copyright (C) 2005-2007 Jelmer Vernooij, jelmer@samba.org
5 Copyright (C) 2006 Wilco Baan Hofman, wilco@baanhofman.nl
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "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 "param/param.h"
27 #include "lib/registry/registry.h"
28 #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;
53 struct smb_iconv_convenience *iconv_convenience;
56 static WERROR regf_save_hbin(struct regf_data *data);
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 containing 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, regf->iconv_convenience);
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 = -1; /* Relative offset ! */
164 struct hbin_block *hbin = NULL;
170 return data_blob(NULL, 0);
172 size += 4; /* Need to include int32 for the length */
174 /* Allocate as a multiple of 8 */
175 size = (size + 7) & ~7;
180 for (i = 0; (hbin = data->hbins[i]); i++) {
183 for (j = 0; j < hbin->offset_to_next-0x20; j+= my_size) {
184 my_size = IVALS(hbin->data, j);
186 if (my_size == 0x0) {
187 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
191 if (my_size % 8 != 0) {
192 DEBUG(0, ("Encountered non-aligned block!\n"));
195 if (my_size < 0) { /* Used... */
197 } else if (my_size == size) { /* exact match */
199 DEBUG(4, ("Found free block of exact size %d in middle of HBIN\n",
202 } else if (my_size > size) { /* data will remain */
204 /* Split this block and mark the next block as free */
205 SIVAL(hbin->data, rel_offset+size, my_size-size);
206 DEBUG(4, ("Found free block of size %d (needing %d) in middle of HBIN\n",
212 if (rel_offset != -1)
216 /* No space available in previous hbins,
217 * allocate new one */
218 if (data->hbins[i] == NULL) {
219 DEBUG(4, ("No space available in other HBINs for block of size %d, allocating new HBIN\n",
221 data->hbins = talloc_realloc(data, data->hbins,
222 struct hbin_block *, i+2);
223 hbin = talloc(data->hbins, struct hbin_block);
224 SMB_ASSERT(hbin != NULL);
226 data->hbins[i] = hbin;
227 data->hbins[i+1] = NULL;
229 hbin->HBIN_ID = talloc_strdup(hbin, "hbin");
230 hbin->offset_from_first = (i == 0?0:data->hbins[i-1]->offset_from_first+data->hbins[i-1]->offset_to_next);
231 hbin->offset_to_next = 0x1000;
232 hbin->unknown[0] = 0;
233 hbin->unknown[0] = 0;
234 unix_to_nt_time(&hbin->last_change, time(NULL));
235 hbin->block_size = hbin->offset_to_next;
236 hbin->data = talloc_zero_array(hbin, uint8_t, hbin->block_size - 0x20);
239 SIVAL(hbin->data, size, hbin->block_size - size - 0x20);
242 /* Set size and mark as used */
243 SIVAL(hbin->data, rel_offset, -size);
245 ret.data = hbin->data + rel_offset + 0x4; /* Skip past length */
246 ret.length = size - 0x4;
248 uint32_t new_rel_offset;
249 *offset = hbin->offset_from_first + rel_offset + 0x20;
250 SMB_ASSERT(hbin_by_offset(data, *offset, &new_rel_offset) == hbin);
251 SMB_ASSERT(new_rel_offset == rel_offset);
257 /* Store a data blob. Return the offset at which it was stored */
258 static uint32_t hbin_store (struct regf_data *data, DATA_BLOB blob)
261 DATA_BLOB dest = hbin_alloc(data, blob.length, &ret);
263 memcpy(dest.data, blob.data, blob.length);
268 static uint32_t hbin_store_tdr(struct regf_data *data,
269 tdr_push_fn_t push_fn, void *p)
271 struct tdr_push *push = tdr_push_init(data, data->iconv_convenience);
274 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
275 DEBUG(0, ("Error during push\n"));
279 ret = hbin_store(data, push->data);
287 /* Free existing data */
288 static void hbin_free (struct regf_data *data, uint32_t offset)
293 struct hbin_block *hbin;
295 SMB_ASSERT (offset > 0);
297 hbin = hbin_by_offset(data, offset, &rel_offset);
302 /* Get original size */
303 size = IVALS(hbin->data, rel_offset);
306 DEBUG(1, ("Trying to free already freed block at 0x%04x\n",
313 /* If the next block is free, merge into big free block */
314 if (rel_offset + size < hbin->offset_to_next) {
315 next_size = IVALS(hbin->data, rel_offset+size);
321 /* Write block size */
322 SIVALS(hbin->data, rel_offset, size);
326 * Store a data blob data was already stored, but has changed in size
327 * Will try to save it at the current location if possible, otherwise
328 * does a free + store */
329 static uint32_t hbin_store_resize(struct regf_data *data,
330 uint32_t orig_offset, DATA_BLOB blob)
333 struct hbin_block *hbin = hbin_by_offset(data, orig_offset,
338 int32_t possible_size;
341 SMB_ASSERT(orig_offset > 0);
344 return hbin_store(data, blob);
346 /* Get original size */
347 orig_size = -IVALS(hbin->data, rel_offset);
349 needed_size = blob.length + 4; /* Add int32 containing length */
350 needed_size = (needed_size + 7) & ~7; /* Align */
352 /* Fits into current allocated block */
353 if (orig_size >= needed_size) {
354 memcpy(hbin->data + rel_offset + 0x4, blob.data, blob.length);
355 /* If the difference in size is greater than 0x4, split the block
356 * and free/merge it */
357 if (orig_size - needed_size > 0x4) {
358 SIVALS(hbin->data, rel_offset, -needed_size);
359 SIVALS(hbin->data, rel_offset + needed_size,
360 needed_size-orig_size);
361 hbin_free(data, orig_offset + needed_size);
366 possible_size = orig_size;
368 /* Check if it can be combined with the next few free records */
369 for (i = rel_offset; i < hbin->offset_to_next - 0x20; i += my_size) {
370 if (IVALS(hbin->data, i) < 0) /* Used */
373 my_size = IVALS(hbin->data, i);
375 if (my_size == 0x0) {
376 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
379 possible_size += my_size;
382 if (possible_size >= blob.length) {
383 SIVAL(hbin->data, rel_offset, -possible_size);
384 memcpy(hbin->data + rel_offset + 0x4,
385 blob.data, blob.length);
390 hbin_free(data, orig_offset);
391 return hbin_store(data, blob);
394 static uint32_t hbin_store_tdr_resize(struct regf_data *regf,
395 tdr_push_fn_t push_fn,
396 uint32_t orig_offset, void *p)
398 struct tdr_push *push = tdr_push_init(regf, regf->iconv_convenience);
401 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
402 DEBUG(0, ("Error during push\n"));
406 ret = hbin_store_resize(regf, orig_offset, push->data);
413 static uint32_t regf_create_lh_hash(const char *name)
419 hash_name = strupper_talloc(NULL, name);
420 for (i = 0; *(hash_name + i) != 0; i++) {
422 ret += *(hash_name + i);
424 talloc_free(hash_name);
428 static WERROR regf_get_info(TALLOC_CTX *mem_ctx,
429 const struct hive_key *key,
430 const char **classname,
431 uint32_t *num_subkeys,
432 uint32_t *num_values,
433 NTTIME *last_mod_time,
434 uint32_t *max_subkeynamelen,
435 uint32_t *max_valnamelen,
436 uint32_t *max_valbufsize)
438 const struct regf_key_data *private_data =
439 (const struct regf_key_data *)key;
441 if (num_subkeys != NULL)
442 *num_subkeys = private_data->nk->num_subkeys;
444 if (num_values != NULL)
445 *num_values = private_data->nk->num_values;
447 if (classname != NULL) {
448 if (private_data->nk->clsname_offset != -1) {
449 DATA_BLOB data = hbin_get(private_data->hive,
450 private_data->nk->clsname_offset);
451 *classname = talloc_strndup(mem_ctx,
453 private_data->nk->clsname_length);
458 /* TODO: Last mod time */
460 /* TODO: max valnamelen */
462 /* TODO: max valbufsize */
464 /* TODO: max subkeynamelen */
469 static struct regf_key_data *regf_get_key(TALLOC_CTX *ctx,
470 struct regf_data *regf,
474 struct regf_key_data *ret;
476 ret = talloc_zero(ctx, struct regf_key_data);
477 ret->key.ops = ®_backend_regf;
478 ret->hive = talloc_reference(ret, regf);
479 ret->offset = offset;
480 nk = talloc(ret, struct nk_block);
486 if (!hbin_get_tdr(regf, offset, nk,
487 (tdr_pull_fn_t)tdr_pull_nk_block, nk)) {
488 DEBUG(0, ("Unable to find HBIN data for offset %d\n", offset));
492 if (strcmp(nk->header, "nk") != 0) {
493 DEBUG(0, ("Expected nk record, got %s\n", nk->header));
502 static WERROR regf_get_value(TALLOC_CTX *ctx, struct hive_key *key,
503 int idx, const char **name,
504 uint32_t *data_type, DATA_BLOB *data)
506 const struct regf_key_data *private_data =
507 (const struct regf_key_data *)key;
509 struct regf_data *regf = private_data->hive;
513 if (idx >= private_data->nk->num_values)
514 return WERR_NO_MORE_ITEMS;
516 tmp = hbin_get(regf, private_data->nk->values_offset);
518 DEBUG(0, ("Unable to find value list\n"));
519 return WERR_GENERAL_FAILURE;
522 if (tmp.length < private_data->nk->num_values * 4) {
523 DEBUG(1, ("Value counts mismatch\n"));
526 vk_offset = IVAL(tmp.data, idx * 4);
528 vk = talloc(NULL, struct vk_block);
529 W_ERROR_HAVE_NO_MEMORY(vk);
531 if (!hbin_get_tdr(regf, vk_offset, vk,
532 (tdr_pull_fn_t)tdr_pull_vk_block, vk)) {
533 DEBUG(0, ("Unable to get VK block at %d\n", vk_offset));
535 return WERR_GENERAL_FAILURE;
538 /* FIXME: name character set ?*/
540 *name = talloc_strndup(ctx, vk->data_name, vk->name_length);
542 if (data_type != NULL)
543 *data_type = vk->data_type;
545 if (vk->data_length & 0x80000000) {
546 vk->data_length &=~0x80000000;
547 data->data = (uint8_t *)&vk->data_offset;
548 data->length = vk->data_length;
550 *data = hbin_get(regf, vk->data_offset);
553 if (data->length < vk->data_length) {
554 DEBUG(1, ("Read data less than indicated data length!\n"));
562 static WERROR regf_get_value_by_name(TALLOC_CTX *mem_ctx,
563 struct hive_key *key, const char *name,
564 uint32_t *type, DATA_BLOB *data)
570 /* FIXME: Do binary search? Is this list sorted at all? */
572 for (i = 0; W_ERROR_IS_OK(error = regf_get_value(mem_ctx, key, i,
573 &vname, type, data));
575 if (!strcmp(vname, name))
579 if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
586 static WERROR regf_get_subkey_by_index(TALLOC_CTX *ctx,
587 const struct hive_key *key,
588 uint32_t idx, const char **name,
589 const char **classname,
590 NTTIME *last_mod_time)
593 struct regf_key_data *ret;
594 const struct regf_key_data *private_data = (const struct regf_key_data *)key;
595 struct nk_block *nk = private_data->nk;
598 if (idx >= nk->num_subkeys)
599 return WERR_NO_MORE_ITEMS;
601 data = hbin_get(private_data->hive, nk->subkeys_offset);
603 DEBUG(0, ("Unable to find subkey list\n"));
604 return WERR_GENERAL_FAILURE;
607 if (!strncmp((char *)data.data, "li", 2)) {
609 struct tdr_pull *pull = tdr_pull_init(private_data->hive, private_data->hive->iconv_convenience);
611 DEBUG(10, ("Subkeys in LI list\n"));
614 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, nk, &li))) {
615 DEBUG(0, ("Error parsing LI list\n"));
617 return WERR_GENERAL_FAILURE;
620 SMB_ASSERT(!strncmp(li.header, "li", 2));
622 if (li.key_count != nk->num_subkeys) {
623 DEBUG(0, ("Subkey counts don't match\n"));
624 return WERR_GENERAL_FAILURE;
626 key_off = li.nk_offset[idx];
628 } else if (!strncmp((char *)data.data, "lf", 2)) {
630 struct tdr_pull *pull = tdr_pull_init(private_data->hive, private_data->hive->iconv_convenience);
632 DEBUG(10, ("Subkeys in LF list\n"));
635 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, nk, &lf))) {
636 DEBUG(0, ("Error parsing LF list\n"));
638 return WERR_GENERAL_FAILURE;
641 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
643 if (lf.key_count != nk->num_subkeys) {
644 DEBUG(0, ("Subkey counts don't match\n"));
645 return WERR_GENERAL_FAILURE;
648 key_off = lf.hr[idx].nk_offset;
649 } else if (!strncmp((char *)data.data, "lh", 2)) {
651 struct tdr_pull *pull = tdr_pull_init(private_data->hive, private_data->hive->iconv_convenience);
653 DEBUG(10, ("Subkeys in LH list\n"));
656 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, nk, &lh))) {
657 DEBUG(0, ("Error parsing LH list\n"));
659 return WERR_GENERAL_FAILURE;
662 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
664 if (lh.key_count != nk->num_subkeys) {
665 DEBUG(0, ("Subkey counts don't match\n"));
666 return WERR_GENERAL_FAILURE;
668 key_off = lh.hr[idx].nk_offset;
669 } else if (!strncmp((char *)data.data, "ri", 2)) {
671 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
673 uint16_t sublist_count = 0;
675 DEBUG(10, ("Subkeys in RI list\n"));
678 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull, nk, &ri))) {
679 DEBUG(0, ("Error parsing RI list\n"));
681 return WERR_GENERAL_FAILURE;
683 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
685 for (i = 0; i < ri.key_count; i++) {
688 /* Get sublist data blob */
689 list_data = hbin_get(private_data->hive, ri.offset[i]);
690 if (!list_data.data) {
691 DEBUG(0, ("Error getting RI list."));
693 return WERR_GENERAL_FAILURE;
696 pull->data = list_data;
698 if (!strncmp((char *)list_data.data, "li", 2)) {
701 DEBUG(10, ("Subkeys in RI->LI list\n"));
703 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull,
706 DEBUG(0, ("Error parsing LI list from RI\n"));
708 return WERR_GENERAL_FAILURE;
710 SMB_ASSERT(!strncmp(li.header, "li", 2));
712 /* Advance to next sublist if necessary */
713 if (idx >= sublist_count + li.key_count) {
714 sublist_count += li.key_count;
717 key_off = li.nk_offset[idx - sublist_count];
718 sublist_count += li.key_count;
720 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
723 DEBUG(10, ("Subkeys in RI->LH list\n"));
725 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull,
728 DEBUG(0, ("Error parsing LH list from RI\n"));
730 return WERR_GENERAL_FAILURE;
732 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
734 /* Advance to next sublist if necessary */
735 if (idx >= sublist_count + lh.key_count) {
736 sublist_count += lh.key_count;
739 key_off = lh.hr[idx - sublist_count].nk_offset;
740 sublist_count += lh.key_count;
743 DEBUG(0,("Unknown sublist in ri block\n"));
746 return WERR_GENERAL_FAILURE;
753 if (idx > sublist_count) {
754 return WERR_NO_MORE_ITEMS;
758 DEBUG(0, ("Unknown type for subkey list (0x%04x): %c%c\n",
759 nk->subkeys_offset, data.data[0], data.data[1]));
760 return WERR_GENERAL_FAILURE;
763 ret = regf_get_key (ctx, private_data->hive, key_off);
765 if (classname != NULL) {
766 if (ret->nk->clsname_offset != -1) {
767 DATA_BLOB db = hbin_get(ret->hive,
768 ret->nk->clsname_offset);
769 *classname = talloc_strndup(ctx,
771 ret->nk->clsname_length);
776 if (last_mod_time != NULL)
777 *last_mod_time = ret->nk->last_change;
780 *name = talloc_steal(ctx, ret->nk->key_name);
787 static WERROR regf_match_subkey_by_name(TALLOC_CTX *ctx,
788 const struct hive_key *key,
790 const char *name, uint32_t *ret)
792 DATA_BLOB subkey_data;
793 struct nk_block subkey;
794 struct tdr_pull *pull;
795 const struct regf_key_data *private_data =
796 (const struct regf_key_data *)key;
798 subkey_data = hbin_get(private_data->hive, offset);
799 if (!subkey_data.data) {
800 DEBUG(0, ("Unable to retrieve subkey HBIN\n"));
801 return WERR_GENERAL_FAILURE;
804 pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
806 pull->data = subkey_data;
808 if (NT_STATUS_IS_ERR(tdr_pull_nk_block(pull, ctx, &subkey))) {
809 DEBUG(0, ("Error parsing NK structure.\n"));
811 return WERR_GENERAL_FAILURE;
815 if (strncmp(subkey.header, "nk", 2)) {
816 DEBUG(0, ("Not an NK structure.\n"));
817 return WERR_GENERAL_FAILURE;
820 if (!strcasecmp(subkey.key_name, name)) {
828 static WERROR regf_get_subkey_by_name(TALLOC_CTX *ctx,
829 const struct hive_key *key,
831 struct hive_key **ret)
834 const struct regf_key_data *private_data =
835 (const struct regf_key_data *)key;
836 struct nk_block *nk = private_data->nk;
837 uint32_t key_off = 0;
839 data = hbin_get(private_data->hive, nk->subkeys_offset);
841 DEBUG(0, ("Unable to find subkey list\n"));
842 return WERR_GENERAL_FAILURE;
845 if (!strncmp((char *)data.data, "li", 2)) {
847 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
850 DEBUG(10, ("Subkeys in LI list\n"));
853 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, nk, &li))) {
854 DEBUG(0, ("Error parsing LI list\n"));
856 return WERR_GENERAL_FAILURE;
859 SMB_ASSERT(!strncmp(li.header, "li", 2));
861 if (li.key_count != nk->num_subkeys) {
862 DEBUG(0, ("Subkey counts don't match\n"));
863 return WERR_GENERAL_FAILURE;
866 for (i = 0; i < li.key_count; i++) {
867 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
876 } else if (!strncmp((char *)data.data, "lf", 2)) {
878 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
881 DEBUG(10, ("Subkeys in LF list\n"));
884 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, nk, &lf))) {
885 DEBUG(0, ("Error parsing LF list\n"));
887 return WERR_GENERAL_FAILURE;
890 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
892 if (lf.key_count != nk->num_subkeys) {
893 DEBUG(0, ("Subkey counts don't match\n"));
894 return WERR_GENERAL_FAILURE;
897 for (i = 0; i < lf.key_count; i++) {
898 if (strncmp(lf.hr[i].hash, name, 4)) {
901 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
911 } else if (!strncmp((char *)data.data, "lh", 2)) {
913 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
917 DEBUG(10, ("Subkeys in LH list\n"));
920 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, nk, &lh))) {
921 DEBUG(0, ("Error parsing LH list\n"));
923 return WERR_GENERAL_FAILURE;
926 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
928 if (lh.key_count != nk->num_subkeys) {
929 DEBUG(0, ("Subkey counts don't match\n"));
930 return WERR_GENERAL_FAILURE;
933 hash = regf_create_lh_hash(name);
934 for (i = 0; i < lh.key_count; i++) {
935 if (lh.hr[i].base37 != hash) {
938 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
948 } else if (!strncmp((char *)data.data, "ri", 2)) {
950 struct tdr_pull *pull = tdr_pull_init(ctx, private_data->hive->iconv_convenience);
953 DEBUG(10, ("Subkeys in RI list\n"));
956 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull, nk, &ri))) {
957 DEBUG(0, ("Error parsing RI list\n"));
959 return WERR_GENERAL_FAILURE;
961 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
963 for (i = 0; i < ri.key_count; i++) {
966 /* Get sublist data blob */
967 list_data = hbin_get(private_data->hive, ri.offset[i]);
968 if (list_data.data == NULL) {
969 DEBUG(0, ("Error getting RI list."));
971 return WERR_GENERAL_FAILURE;
974 pull->data = list_data;
976 if (!strncmp((char *)list_data.data, "li", 2)) {
979 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull,
982 DEBUG(0, ("Error parsing LI list from RI\n"));
984 return WERR_GENERAL_FAILURE;
986 SMB_ASSERT(!strncmp(li.header, "li", 2));
988 for (j = 0; j < li.key_count; j++) {
989 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
996 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
1000 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull,
1003 DEBUG(0, ("Error parsing LH list from RI\n"));
1005 return WERR_GENERAL_FAILURE;
1007 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1009 hash = regf_create_lh_hash(name);
1010 for (j = 0; j < lh.key_count; j++) {
1011 if (lh.hr[j].base37 != hash) {
1014 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
1027 return WERR_BADFILE;
1029 DEBUG(0, ("Unknown subkey list type.\n"));
1030 return WERR_GENERAL_FAILURE;
1033 *ret = (struct hive_key *)regf_get_key(ctx, private_data->hive,
1038 static WERROR regf_set_sec_desc(struct hive_key *key,
1039 const struct security_descriptor *sec_desc)
1041 const struct regf_key_data *private_data =
1042 (const struct regf_key_data *)key;
1043 struct sk_block cur_sk, sk, new_sk;
1044 struct regf_data *regf = private_data->hive;
1045 struct nk_block root;
1047 uint32_t sk_offset, cur_sk_offset;
1048 bool update_cur_sk = false;
1050 /* Get the root nk */
1051 hbin_get_tdr(regf, regf->header->data_offset, regf,
1052 (tdr_pull_fn_t) tdr_pull_nk_block, &root);
1054 /* Push the security descriptor to a blob */
1055 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf, NULL,
1056 sec_desc, (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
1057 DEBUG(0, ("Unable to push security descriptor\n"));
1058 return WERR_GENERAL_FAILURE;
1061 /* Get the current security descriptor for the key */
1062 if (!hbin_get_tdr(regf, private_data->nk->sk_offset, regf,
1063 (tdr_pull_fn_t) tdr_pull_sk_block, &cur_sk)) {
1064 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1065 return WERR_BADFILE;
1067 /* If there's no change, change nothing. */
1068 if (memcmp(data.data, cur_sk.sec_desc,
1069 MIN(data.length, cur_sk.rec_size)) == 0) {
1073 /* Delete the current sk if only this key is using it */
1074 if (cur_sk.ref_cnt == 1) {
1075 /* Get the previous security descriptor for the key */
1076 if (!hbin_get_tdr(regf, cur_sk.prev_offset, regf,
1077 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1078 DEBUG(0, ("Unable to find prev security descriptor for current key\n"));
1079 return WERR_BADFILE;
1081 /* Change and store the previous security descriptor */
1082 sk.next_offset = cur_sk.next_offset;
1083 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
1084 cur_sk.prev_offset, &sk);
1086 /* Get the next security descriptor for the key */
1087 if (!hbin_get_tdr(regf, cur_sk.next_offset, regf,
1088 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1089 DEBUG(0, ("Unable to find next security descriptor for current key\n"));
1090 return WERR_BADFILE;
1092 /* Change and store the next security descriptor */
1093 sk.prev_offset = cur_sk.prev_offset;
1094 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
1095 cur_sk.next_offset, &sk);
1097 hbin_free(regf, private_data->nk->sk_offset);
1099 /* This key will no longer be referring to this sk */
1101 update_cur_sk = true;
1104 sk_offset = root.sk_offset;
1107 cur_sk_offset = sk_offset;
1108 if (!hbin_get_tdr(regf, sk_offset, regf,
1109 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1110 DEBUG(0, ("Unable to find security descriptor\n"));
1111 return WERR_BADFILE;
1113 if (memcmp(data.data, sk.sec_desc, MIN(data.length, sk.rec_size)) == 0) {
1114 private_data->nk->sk_offset = sk_offset;
1116 hbin_store_tdr_resize(regf,
1117 (tdr_push_fn_t) tdr_push_sk_block,
1119 hbin_store_tdr_resize(regf,
1120 (tdr_push_fn_t) tdr_push_nk_block,
1121 private_data->offset,
1125 sk_offset = sk.next_offset;
1126 } while (sk_offset != root.sk_offset);
1128 ZERO_STRUCT(new_sk);
1129 new_sk.header = "sk";
1130 new_sk.prev_offset = cur_sk_offset;
1131 new_sk.next_offset = root.sk_offset;
1133 new_sk.rec_size = data.length;
1134 new_sk.sec_desc = data.data;
1136 sk_offset = hbin_store_tdr(regf,
1137 (tdr_push_fn_t) tdr_push_sk_block,
1139 if (sk_offset == -1) {
1140 DEBUG(0, ("Error storing sk block\n"));
1141 return WERR_GENERAL_FAILURE;
1143 private_data->nk->sk_offset = sk_offset;
1145 if (update_cur_sk) {
1146 hbin_store_tdr_resize(regf,
1147 (tdr_push_fn_t) tdr_push_sk_block,
1148 private_data->nk->sk_offset, &cur_sk);
1151 /* Get the previous security descriptor for the key */
1152 if (!hbin_get_tdr(regf, new_sk.prev_offset, regf,
1153 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1154 DEBUG(0, ("Unable to find security descriptor for previous key\n"));
1155 return WERR_BADFILE;
1157 /* Change and store the previous security descriptor */
1158 sk.next_offset = sk_offset;
1159 hbin_store_tdr_resize(regf,
1160 (tdr_push_fn_t) tdr_push_sk_block,
1161 cur_sk.prev_offset, &sk);
1163 /* Get the next security descriptor for the key (always root, as we append) */
1164 if (!hbin_get_tdr(regf, new_sk.next_offset, regf,
1165 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1166 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1167 return WERR_BADFILE;
1169 /* Change and store the next security descriptor (always root, as we append) */
1170 sk.prev_offset = sk_offset;
1171 hbin_store_tdr_resize(regf,
1172 (tdr_push_fn_t) tdr_push_sk_block,
1173 root.sk_offset, &sk);
1177 hbin_store_tdr_resize(regf,
1178 (tdr_push_fn_t) tdr_push_sk_block,
1179 private_data->offset, private_data->nk);
1183 static WERROR regf_get_sec_desc(TALLOC_CTX *ctx, const struct hive_key *key,
1184 struct security_descriptor **sd)
1186 const struct regf_key_data *private_data =
1187 (const struct regf_key_data *)key;
1189 struct regf_data *regf = private_data->hive;
1192 if (!hbin_get_tdr(regf, private_data->nk->sk_offset, ctx,
1193 (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1194 DEBUG(0, ("Unable to find security descriptor\n"));
1195 return WERR_GENERAL_FAILURE;
1198 if (strcmp(sk.header, "sk") != 0) {
1199 DEBUG(0, ("Expected 'sk', got '%s'\n", sk.header));
1200 return WERR_GENERAL_FAILURE;
1203 *sd = talloc(ctx, struct security_descriptor);
1204 W_ERROR_HAVE_NO_MEMORY(*sd);
1206 data.data = sk.sec_desc;
1207 data.length = sk.rec_size;
1208 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_struct_blob(&data, ctx, NULL, *sd,
1209 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor))) {
1210 DEBUG(0, ("Error parsing security descriptor\n"));
1211 return WERR_GENERAL_FAILURE;
1217 static WERROR regf_sl_add_entry(struct regf_data *regf, uint32_t list_offset,
1219 uint32_t key_offset, uint32_t *ret)
1223 /* Create a new key if necessary */
1224 if (list_offset == -1) {
1225 if (regf->header->version.major != 1) {
1226 DEBUG(0, ("Can't store keys in unknown registry format\n"));
1227 return WERR_NOT_SUPPORTED;
1229 if (regf->header->version.minor < 3) {
1236 li.nk_offset = talloc_array(regf, uint32_t, 1);
1237 W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1238 li.nk_offset[0] = key_offset;
1240 *ret = hbin_store_tdr(regf,
1241 (tdr_push_fn_t) tdr_push_li_block,
1244 talloc_free(li.nk_offset);
1245 } else if (regf->header->version.minor == 3 ||
1246 regf->header->version.minor == 4) {
1253 lf.hr = talloc_array(regf, struct hash_record, 1);
1254 W_ERROR_HAVE_NO_MEMORY(lf.hr);
1255 lf.hr[0].nk_offset = key_offset;
1256 lf.hr[0].hash = talloc_strndup(lf.hr, name, 4);
1257 W_ERROR_HAVE_NO_MEMORY(lf.hr[0].hash);
1259 *ret = hbin_store_tdr(regf,
1260 (tdr_push_fn_t) tdr_push_lf_block,
1264 } else if (regf->header->version.minor == 5) {
1271 lh.hr = talloc_array(regf, struct lh_hash, 1);
1272 W_ERROR_HAVE_NO_MEMORY(lh.hr);
1273 lh.hr[0].nk_offset = key_offset;
1274 lh.hr[0].base37 = regf_create_lh_hash(name);
1276 *ret = hbin_store_tdr(regf,
1277 (tdr_push_fn_t) tdr_push_lh_block,
1285 data = hbin_get(regf, list_offset);
1287 DEBUG(0, ("Unable to find subkey list\n"));
1288 return WERR_BADFILE;
1291 if (!strncmp((char *)data.data, "li", 2)) {
1292 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
1297 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, regf, &li))) {
1298 DEBUG(0, ("Error parsing LI list\n"));
1300 return WERR_BADFILE;
1304 if (strncmp(li.header, "li", 2) != 0) {
1306 DEBUG(0, ("LI header corrupt\n"));
1307 return WERR_BADFILE;
1310 li.nk_offset = talloc_realloc(regf, li.nk_offset,
1311 uint32_t, li.key_count+1);
1312 W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1313 li.nk_offset[li.key_count] = key_offset;
1315 *ret = hbin_store_tdr_resize(regf,
1316 (tdr_push_fn_t)tdr_push_li_block,
1319 talloc_free(li.nk_offset);
1320 } else if (!strncmp((char *)data.data, "lf", 2)) {
1321 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
1326 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, regf, &lf))) {
1327 DEBUG(0, ("Error parsing LF list\n"));
1329 return WERR_BADFILE;
1332 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1334 lf.hr = talloc_realloc(regf, lf.hr, struct hash_record,
1336 W_ERROR_HAVE_NO_MEMORY(lf.hr);
1337 lf.hr[lf.key_count].nk_offset = key_offset;
1338 lf.hr[lf.key_count].hash = talloc_strndup(lf.hr, name, 4);
1339 W_ERROR_HAVE_NO_MEMORY(lf.hr[lf.key_count].hash);
1341 *ret = hbin_store_tdr_resize(regf,
1342 (tdr_push_fn_t)tdr_push_lf_block,
1346 } else if (!strncmp((char *)data.data, "lh", 2)) {
1347 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
1352 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, regf, &lh))) {
1353 DEBUG(0, ("Error parsing LH list\n"));
1355 return WERR_BADFILE;
1358 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1360 lh.hr = talloc_realloc(regf, lh.hr, struct lh_hash,
1362 W_ERROR_HAVE_NO_MEMORY(lh.hr);
1363 lh.hr[lh.key_count].nk_offset = key_offset;
1364 lh.hr[lh.key_count].base37 = regf_create_lh_hash(name);
1366 *ret = hbin_store_tdr_resize(regf,
1367 (tdr_push_fn_t)tdr_push_lh_block,
1371 } else if (!strncmp((char *)data.data, "ri", 2)) {
1373 DEBUG(0, ("Adding to 'ri' subkey list is not supported yet.\n"));
1374 return WERR_NOT_SUPPORTED;
1376 DEBUG(0, ("Cannot add to unknown subkey list\n"));
1377 return WERR_BADFILE;
1383 static WERROR regf_sl_del_entry(struct regf_data *regf, uint32_t list_offset,
1384 uint32_t key_offset, uint32_t *ret)
1388 data = hbin_get(regf, list_offset);
1390 DEBUG(0, ("Unable to find subkey list\n"));
1391 return WERR_BADFILE;
1394 if (strncmp((char *)data.data, "li", 2) == 0) {
1396 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
1398 bool found_offset = false;
1400 DEBUG(10, ("Subkeys in LI list\n"));
1404 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull, regf, &li))) {
1405 DEBUG(0, ("Error parsing LI list\n"));
1407 return WERR_BADFILE;
1411 SMB_ASSERT(!strncmp(li.header, "li", 2));
1413 for (i = 0; i < li.key_count; i++) {
1415 li.nk_offset[i-1] = li.nk_offset[i];
1417 if (li.nk_offset[i] == key_offset) {
1418 found_offset = true;
1422 if (!found_offset) {
1423 DEBUG(2, ("Subkey not found\n"));
1424 return WERR_BADFILE;
1428 /* If the there are no entries left, free the subkey list */
1429 if (li.key_count == 0) {
1430 hbin_free(regf, list_offset);
1434 /* Store li block */
1435 *ret = hbin_store_tdr_resize(regf,
1436 (tdr_push_fn_t) tdr_push_li_block,
1438 } else if (strncmp((char *)data.data, "lf", 2) == 0) {
1440 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
1442 bool found_offset = false;
1444 DEBUG(10, ("Subkeys in LF list\n"));
1448 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull, regf, &lf))) {
1449 DEBUG(0, ("Error parsing LF list\n"));
1451 return WERR_BADFILE;
1455 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1457 for (i = 0; i < lf.key_count; i++) {
1459 lf.hr[i-1] = lf.hr[i];
1462 if (lf.hr[i].nk_offset == key_offset) {
1467 if (!found_offset) {
1468 DEBUG(2, ("Subkey not found\n"));
1469 return WERR_BADFILE;
1473 /* If the there are no entries left, free the subkey list */
1474 if (lf.key_count == 0) {
1475 hbin_free(regf, list_offset);
1480 /* Store lf block */
1481 *ret = hbin_store_tdr_resize(regf,
1482 (tdr_push_fn_t) tdr_push_lf_block,
1484 } else if (strncmp((char *)data.data, "lh", 2) == 0) {
1486 struct tdr_pull *pull = tdr_pull_init(regf, regf->iconv_convenience);
1488 bool found_offset = false;
1490 DEBUG(10, ("Subkeys in LH list\n"));
1494 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull, regf, &lh))) {
1495 DEBUG(0, ("Error parsing LF list\n"));
1497 return WERR_BADFILE;
1501 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1503 for (i = 0; i < lh.key_count; i++) {
1505 lh.hr[i-1] = lh.hr[i];
1508 if (lh.hr[i].nk_offset == key_offset) {
1513 if (!found_offset) {
1514 DEBUG(0, ("Subkey not found\n"));
1515 return WERR_BADFILE;
1519 /* If the there are no entries left, free the subkey list */
1520 if (lh.key_count == 0) {
1521 hbin_free(regf, list_offset);
1526 /* Store lh block */
1527 *ret = hbin_store_tdr_resize(regf,
1528 (tdr_push_fn_t) tdr_push_lh_block,
1530 } else if (strncmp((char *)data.data, "ri", 2) == 0) {
1532 DEBUG(0, ("Sorry, deletion from ri block is not supported yet.\n"));
1533 return WERR_NOT_SUPPORTED;
1535 DEBUG (0, ("Unknown header found in subkey list.\n"));
1536 return WERR_BADFILE;
1541 static WERROR regf_del_value (struct hive_key *key, const char *name)
1543 struct regf_key_data *private_data = (struct regf_key_data *)key;
1544 struct regf_data *regf = private_data->hive;
1545 struct nk_block *nk = private_data->nk;
1548 bool found_offset = false;
1552 if (nk->values_offset == -1) {
1553 return WERR_BADFILE;
1556 values = hbin_get(regf, nk->values_offset);
1558 for (i = 0; i < nk->num_values; i++) {
1560 ((uint32_t *)values.data)[i-1] = ((uint32_t *) values.data)[i];
1562 vk_offset = IVAL(values.data, i * 4);
1563 if (!hbin_get_tdr(regf, vk_offset, private_data,
1564 (tdr_pull_fn_t)tdr_pull_vk_block,
1566 DEBUG(0, ("Unable to get VK block at %d\n",
1568 return WERR_BADFILE;
1570 if (strcmp(vk.data_name, name) == 0) {
1571 hbin_free(regf, vk_offset);
1572 found_offset = true;
1576 if (!found_offset) {
1577 return WERR_BADFILE;
1580 values.length = (nk->num_values)*4;
1583 /* Store values list and nk */
1584 if (nk->num_values == 0) {
1585 hbin_free(regf, nk->values_offset);
1586 nk->values_offset = -1;
1588 nk->values_offset = hbin_store_resize(regf,
1592 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
1593 private_data->offset, nk);
1595 return regf_save_hbin(private_data->hive);
1599 static WERROR regf_del_key(const struct hive_key *parent, const char *name)
1601 const struct regf_key_data *private_data =
1602 (const struct regf_key_data *)parent;
1603 struct regf_key_data *key;
1604 struct nk_block *parent_nk;
1607 SMB_ASSERT(private_data);
1609 parent_nk = private_data->nk;
1611 if (parent_nk->subkeys_offset == -1) {
1612 DEBUG(4, ("Subkey list is empty, this key cannot contain subkeys.\n"));
1613 return WERR_BADFILE;
1617 if (!W_ERROR_IS_OK(regf_get_subkey_by_name(parent_nk, parent, name,
1618 (struct hive_key **)&key))) {
1619 DEBUG(2, ("Key '%s' not found\n", name));
1620 return WERR_BADFILE;
1623 if (key->nk->subkeys_offset != -1) {
1625 struct hive_key *sk = (struct hive_key *)key;
1626 int i = key->nk->num_subkeys;
1628 /* Get subkey information. */
1629 error = regf_get_subkey_by_index(parent_nk, sk, 0,
1630 (const char **)&sk_name,
1632 if (!W_ERROR_IS_OK(error)) {
1633 DEBUG(0, ("Can't retrieve subkey by index.\n"));
1637 /* Delete subkey. */
1638 error = regf_del_key(sk, sk_name);
1639 if (!W_ERROR_IS_OK(error)) {
1640 DEBUG(0, ("Can't delete key '%s'.\n", sk_name));
1644 talloc_free(sk_name);
1648 if (key->nk->values_offset != -1) {
1650 struct hive_key *sk = (struct hive_key *)key;
1652 int i = key->nk->num_values;
1654 /* Get value information. */
1655 error = regf_get_value(parent_nk, sk, 0,
1656 (const char **)&val_name,
1658 if (!W_ERROR_IS_OK(error)) {
1659 DEBUG(0, ("Can't retrieve value by index.\n"));
1664 error = regf_del_value(sk, val_name);
1665 if (!W_ERROR_IS_OK(error)) {
1666 DEBUG(0, ("Can't delete value '%s'.\n", val_name));
1670 talloc_free(val_name);
1674 /* Delete it from the subkey list. */
1675 error = regf_sl_del_entry(private_data->hive, parent_nk->subkeys_offset,
1676 key->offset, &parent_nk->subkeys_offset);
1677 if (!W_ERROR_IS_OK(error)) {
1678 DEBUG(0, ("Can't store new subkey list for parent key. Won't delete.\n"));
1682 /* Re-store parent key */
1683 parent_nk->num_subkeys--;
1684 hbin_store_tdr_resize(private_data->hive,
1685 (tdr_push_fn_t) tdr_push_nk_block,
1686 private_data->offset, parent_nk);
1688 if (key->nk->clsname_offset != -1) {
1689 hbin_free(private_data->hive, key->nk->clsname_offset);
1691 hbin_free(private_data->hive, key->offset);
1693 return regf_save_hbin(private_data->hive);
1696 static WERROR regf_add_key(TALLOC_CTX *ctx, const struct hive_key *parent,
1697 const char *name, const char *classname,
1698 struct security_descriptor *sec_desc,
1699 struct hive_key **ret)
1701 const struct regf_key_data *private_data =
1702 (const struct regf_key_data *)parent;
1703 struct nk_block *parent_nk = private_data->nk, nk;
1704 struct nk_block *root;
1705 struct regf_data *regf = private_data->hive;
1710 nk.type = REG_SUB_KEY;
1711 unix_to_nt_time(&nk.last_change, time(NULL));
1713 nk.parent_offset = private_data->offset;
1716 nk.subkeys_offset = -1;
1717 nk.unknown_offset = -1;
1719 nk.values_offset = -1;
1720 memset(nk.unk3, 0, 5);
1721 nk.clsname_offset = -1; /* FIXME: fill in */
1722 nk.clsname_length = 0;
1725 /* Get the security descriptor of the root key */
1726 root = talloc_zero(ctx, struct nk_block);
1727 W_ERROR_HAVE_NO_MEMORY(root);
1729 if (!hbin_get_tdr(regf, regf->header->data_offset, root,
1730 (tdr_pull_fn_t)tdr_pull_nk_block, root)) {
1731 DEBUG(0, ("Unable to find HBIN data for offset %d\n", offset));
1732 return WERR_GENERAL_FAILURE;
1734 nk.sk_offset = root->sk_offset;
1737 /* Store the new nk key */
1738 offset = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_nk_block, &nk);
1740 error = regf_sl_add_entry(regf, parent_nk->subkeys_offset, name, offset,
1741 &parent_nk->subkeys_offset);
1742 if (!W_ERROR_IS_OK(error)) {
1743 hbin_free(regf, offset);
1747 parent_nk->num_subkeys++;
1749 /* Since the subkey offset of the parent can change, store it again */
1750 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
1751 nk.parent_offset, parent_nk);
1753 *ret = (struct hive_key *)regf_get_key(ctx, regf, offset);
1755 return regf_save_hbin(private_data->hive);
1758 static WERROR regf_set_value(struct hive_key *key, const char *name,
1759 uint32_t type, const DATA_BLOB data)
1761 struct regf_key_data *private_data = (struct regf_key_data *)key;
1762 struct regf_data *regf = private_data->hive;
1763 struct nk_block *nk = private_data->nk;
1766 uint32_t tmp_vk_offset, vk_offset, old_vk_offset = -1;
1771 /* find the value offset, if it exists */
1772 if (nk->values_offset != -1) {
1773 values = hbin_get(regf, nk->values_offset);
1775 for (i = 0; i < nk->num_values; i++) {
1776 tmp_vk_offset = IVAL(values.data, i * 4);
1777 if (!hbin_get_tdr(regf, tmp_vk_offset, private_data,
1778 (tdr_pull_fn_t)tdr_pull_vk_block,
1780 DEBUG(0, ("Unable to get VK block at %d\n",
1782 return WERR_GENERAL_FAILURE;
1784 if (strcmp(vk.data_name, name) == 0) {
1785 old_vk_offset = tmp_vk_offset;
1789 /* Free data, if any */
1790 if (!(vk.data_length & 0x80000000)) {
1791 hbin_free(regf, vk.data_offset);
1794 if (old_vk_offset == -1) {
1796 vk.name_length = strlen(name);
1797 if (name != NULL && name[0] != 0) {
1799 vk.data_name = name;
1801 vk.data_name = NULL;
1805 /* Set the type and data */
1806 vk.data_length = data.length;
1807 vk.data_type = type;
1808 if (type == REG_DWORD) {
1809 vk.data_length |= 0x80000000;
1810 vk.data_offset = *(uint32_t *)data.data;
1812 /* Store data somewhere */
1813 vk.data_offset = hbin_store(regf, data);
1815 if (old_vk_offset == -1) {
1817 vk_offset = hbin_store_tdr(regf,
1818 (tdr_push_fn_t) tdr_push_vk_block,
1821 /* Store vk at offset */
1822 vk_offset = hbin_store_tdr_resize(regf,
1823 (tdr_push_fn_t) tdr_push_vk_block,
1824 old_vk_offset ,&vk);
1827 /* Re-allocate the value list */
1828 if (nk->values_offset == -1) {
1829 nk->values_offset = hbin_store_tdr(regf,
1830 (tdr_push_fn_t) tdr_push_uint32,
1835 /* Change if we're changing, otherwise we're adding the value */
1836 if (old_vk_offset != -1) {
1837 /* Find and overwrite the offset. */
1838 for (i = 0; i < nk->num_values; i++) {
1839 if (IVAL(values.data, i * 4) == old_vk_offset) {
1840 SIVAL(values.data, i * 4, vk_offset);
1845 /* Create a new value list */
1846 DATA_BLOB value_list;
1848 value_list.length = (nk->num_values+1)*4;
1849 value_list.data = (uint8_t *)talloc_array(private_data,
1852 W_ERROR_HAVE_NO_MEMORY(value_list.data);
1853 memcpy(value_list.data, values.data, nk->num_values * 4);
1855 SIVAL(value_list.data, nk->num_values * 4, vk_offset);
1857 nk->values_offset = hbin_store_resize(regf,
1863 hbin_store_tdr_resize(regf,
1864 (tdr_push_fn_t) tdr_push_nk_block,
1865 private_data->offset, nk);
1866 return regf_save_hbin(private_data->hive);
1869 static WERROR regf_save_hbin(struct regf_data *regf)
1871 struct tdr_push *push = tdr_push_init(regf, regf->iconv_convenience);
1874 W_ERROR_HAVE_NO_MEMORY(push);
1876 if (lseek(regf->fd, 0, SEEK_SET) == -1) {
1877 DEBUG(0, ("Error lseeking in regf file\n"));
1878 return WERR_GENERAL_FAILURE;
1881 /* Recompute checksum */
1882 if (NT_STATUS_IS_ERR(tdr_push_regf_hdr(push, regf->header))) {
1883 DEBUG(0, ("Failed to push regf header\n"));
1884 return WERR_GENERAL_FAILURE;
1886 regf->header->chksum = regf_hdr_checksum(push->data.data);
1889 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd, regf->iconv_convenience,
1890 (tdr_push_fn_t)tdr_push_regf_hdr,
1892 DEBUG(0, ("Error writing registry file header\n"));
1893 return WERR_GENERAL_FAILURE;
1896 if (lseek(regf->fd, 0x1000, SEEK_SET) == -1) {
1897 DEBUG(0, ("Error lseeking to 0x1000 in regf file\n"));
1898 return WERR_GENERAL_FAILURE;
1901 for (i = 0; regf->hbins[i]; i++) {
1902 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd, regf->iconv_convenience,
1903 (tdr_push_fn_t)tdr_push_hbin_block,
1905 DEBUG(0, ("Error writing HBIN block\n"));
1906 return WERR_GENERAL_FAILURE;
1913 WERROR reg_create_regf_file(TALLOC_CTX *parent_ctx,
1914 struct smb_iconv_convenience *iconv_convenience,
1915 const char *location,
1916 int minor_version, struct hive_key **key)
1918 struct regf_data *regf;
1919 struct regf_hdr *regf_hdr;
1924 struct security_descriptor *sd;
1927 regf = (struct regf_data *)talloc_zero(NULL, struct regf_data);
1929 regf->iconv_convenience = iconv_convenience;
1931 W_ERROR_HAVE_NO_MEMORY(regf);
1933 DEBUG(5, ("Attempting to create registry file\n"));
1935 /* Get the header */
1936 regf->fd = creat(location, 0644);
1938 if (regf->fd == -1) {
1939 DEBUG(0,("Could not create file: %s, %s\n", location,
1942 return WERR_GENERAL_FAILURE;
1945 regf_hdr = talloc_zero(regf, struct regf_hdr);
1946 W_ERROR_HAVE_NO_MEMORY(regf_hdr);
1947 regf_hdr->REGF_ID = "regf";
1948 unix_to_nt_time(®f_hdr->modtime, time(NULL));
1949 regf_hdr->version.major = 1;
1950 regf_hdr->version.minor = minor_version;
1951 regf_hdr->last_block = 0x1000; /* Block size */
1952 regf_hdr->description = talloc_strdup(regf_hdr,
1953 "Registry created by Samba 4");
1954 W_ERROR_HAVE_NO_MEMORY(regf_hdr->description);
1955 regf_hdr->chksum = 0;
1957 regf->header = regf_hdr;
1959 /* Create all hbin blocks */
1960 regf->hbins = talloc_array(regf, struct hbin_block *, 1);
1961 W_ERROR_HAVE_NO_MEMORY(regf->hbins);
1962 regf->hbins[0] = NULL;
1965 nk.type = REG_SUB_KEY;
1966 unix_to_nt_time(&nk.last_change, time(NULL));
1968 nk.parent_offset = -1;
1971 nk.subkeys_offset = -1;
1972 nk.unknown_offset = -1;
1974 nk.values_offset = -1;
1975 memset(nk.unk3, 0, 5);
1976 nk.clsname_offset = -1;
1977 nk.clsname_length = 0;
1978 nk.sk_offset = 0x80;
1979 nk.key_name = "SambaRootKey";
1982 * It should be noted that changing the key_name to something shorter
1983 * creates a shorter nk block, which makes the position of the sk block
1984 * change. All Windows registries I've seen have the sk at 0x80.
1985 * I therefore recommend that our regf files share that offset -- Wilco
1988 /* Create a security descriptor. */
1989 sd = security_descriptor_dacl_create(regf,
1992 SID_NT_AUTHENTICATED_USERS,
1993 SEC_ACE_TYPE_ACCESS_ALLOWED,
1995 SEC_ACE_FLAG_OBJECT_INHERIT,
1998 /* Push the security descriptor to a blob */
1999 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf, NULL,
2000 sd, (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
2001 DEBUG(0, ("Unable to push security descriptor\n"));
2002 return WERR_GENERAL_FAILURE;
2007 sk.prev_offset = 0x80;
2008 sk.next_offset = 0x80;
2010 sk.rec_size = data.length;
2011 sk.sec_desc = data.data;
2013 /* Store the new nk key */
2014 regf->header->data_offset = hbin_store_tdr(regf,
2015 (tdr_push_fn_t)tdr_push_nk_block,
2017 /* Store the sk block */
2018 sk_offset = hbin_store_tdr(regf,
2019 (tdr_push_fn_t) tdr_push_sk_block,
2021 if (sk_offset != 0x80) {
2022 DEBUG(0, ("Error storing sk block, should be at 0x80, stored at 0x%x\n", nk.sk_offset));
2023 return WERR_GENERAL_FAILURE;
2027 *key = (struct hive_key *)regf_get_key(parent_ctx, regf,
2028 regf->header->data_offset);
2030 error = regf_save_hbin(regf);
2031 if (!W_ERROR_IS_OK(error)) {
2035 /* We can drop our own reference now that *key will have created one */
2041 WERROR reg_open_regf_file(TALLOC_CTX *parent_ctx, const char *location,
2042 struct smb_iconv_convenience *iconv_convenience, struct hive_key **key)
2044 struct regf_data *regf;
2045 struct regf_hdr *regf_hdr;
2046 struct tdr_pull *pull;
2049 regf = (struct regf_data *)talloc_zero(NULL, struct regf_data);
2051 regf->iconv_convenience = iconv_convenience;
2053 W_ERROR_HAVE_NO_MEMORY(regf);
2055 DEBUG(5, ("Attempting to load registry file\n"));
2057 /* Get the header */
2058 regf->fd = open(location, O_RDWR);
2060 if (regf->fd == -1) {
2061 DEBUG(0,("Could not load file: %s, %s\n", location,
2064 return WERR_GENERAL_FAILURE;
2067 pull = tdr_pull_init(regf, regf->iconv_convenience);
2069 pull->data.data = (uint8_t*)fd_load(regf->fd, &pull->data.length, 0, regf);
2071 if (pull->data.data == NULL) {
2072 DEBUG(0, ("Error reading data\n"));
2074 return WERR_GENERAL_FAILURE;
2077 regf_hdr = talloc(regf, struct regf_hdr);
2078 W_ERROR_HAVE_NO_MEMORY(regf_hdr);
2080 if (NT_STATUS_IS_ERR(tdr_pull_regf_hdr(pull, regf_hdr, regf_hdr))) {
2082 return WERR_GENERAL_FAILURE;
2085 regf->header = regf_hdr;
2087 if (strcmp(regf_hdr->REGF_ID, "regf") != 0) {
2088 DEBUG(0, ("Unrecognized NT registry header id: %s, %s\n",
2089 regf_hdr->REGF_ID, location));
2091 return WERR_GENERAL_FAILURE;
2094 /* Validate the header ... */
2095 if (regf_hdr_checksum(pull->data.data) != regf_hdr->chksum) {
2096 DEBUG(0, ("Registry file checksum error: %s: %d,%d\n",
2097 location, regf_hdr->chksum,
2098 regf_hdr_checksum(pull->data.data)));
2100 return WERR_GENERAL_FAILURE;
2103 pull->offset = 0x1000;
2106 /* Read in all hbin blocks */
2107 regf->hbins = talloc_array(regf, struct hbin_block *, 1);
2108 W_ERROR_HAVE_NO_MEMORY(regf->hbins);
2110 regf->hbins[0] = NULL;
2112 while (pull->offset < pull->data.length &&
2113 pull->offset <= regf->header->last_block) {
2114 struct hbin_block *hbin = talloc(regf->hbins,
2117 W_ERROR_HAVE_NO_MEMORY(hbin);
2119 if (NT_STATUS_IS_ERR(tdr_pull_hbin_block(pull, hbin, hbin))) {
2120 DEBUG(0, ("[%d] Error parsing HBIN block\n", i));
2125 if (strcmp(hbin->HBIN_ID, "hbin") != 0) {
2126 DEBUG(0, ("[%d] Expected 'hbin', got '%s'\n",
2132 regf->hbins[i] = hbin;
2134 regf->hbins = talloc_realloc(regf, regf->hbins,
2135 struct hbin_block *, i+2);
2136 regf->hbins[i] = NULL;
2141 DEBUG(1, ("%d HBIN blocks read\n", i));
2143 *key = (struct hive_key *)regf_get_key(parent_ctx, regf,
2144 regf->header->data_offset);
2146 /* We can drop our own reference now that *key will have created one */
2152 static struct hive_operations reg_backend_regf = {
2154 .get_key_info = regf_get_info,
2155 .enum_key = regf_get_subkey_by_index,
2156 .get_key_by_name = regf_get_subkey_by_name,
2157 .get_value_by_name = regf_get_value_by_name,
2158 .enum_value = regf_get_value,
2159 .get_sec_desc = regf_get_sec_desc,
2160 .set_sec_desc = regf_set_sec_desc,
2161 .add_key = regf_add_key,
2162 .set_value = regf_set_value,
2163 .del_key = regf_del_key,
2164 .delete_value = regf_del_value,