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
, bool 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
;
166 if (offset
!= NULL
) {
171 return data_blob(NULL
, 0);
173 size
+= 4; /* Need to include int32 for the length */
175 /* Allocate as a multiple of 8 */
176 size
= (size
+ 7) & ~7;
181 for (i
= 0; (hbin
= data
->hbins
[i
]); i
++) {
184 for (j
= 0; j
< hbin
->offset_to_next
-0x20; j
+= my_size
) {
185 my_size
= IVALS(hbin
->data
, j
);
187 if (my_size
== 0x0) {
188 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
192 if (my_size
% 8 != 0) {
193 DEBUG(0, ("Encountered non-aligned block!\n"));
196 if (my_size
< 0) { /* Used... */
198 } else if (my_size
== size
) { /* exact match */
200 DEBUG(4, ("Found free block of exact size %d in middle of HBIN\n",
203 } else if (my_size
> size
) { /* data will remain */
205 /* Split this block and mark the next block as free */
206 SIVAL(hbin
->data
, rel_offset
+size
, my_size
-size
);
207 DEBUG(4, ("Found free block of size %d (needing %d) in middle of HBIN\n",
213 if (rel_offset
!= -1)
217 /* No space available in previous hbins,
218 * allocate new one */
219 if (data
->hbins
[i
] == NULL
) {
220 DEBUG(4, ("No space available in other HBINs for block of size %d, allocating new HBIN\n",
223 /* Add extra hbin block */
224 data
->hbins
= talloc_realloc(data
, data
->hbins
,
225 struct hbin_block
*, i
+2);
226 hbin
= talloc(data
->hbins
, struct hbin_block
);
227 SMB_ASSERT(hbin
!= NULL
);
229 data
->hbins
[i
] = hbin
;
230 data
->hbins
[i
+1] = NULL
;
233 hbin
->HBIN_ID
= talloc_strdup(hbin
, "hbin");
234 hbin
->offset_from_first
= (i
== 0?0:data
->hbins
[i
-1]->offset_from_first
+data
->hbins
[i
-1]->offset_to_next
);
235 hbin
->offset_to_next
= 0x1000;
236 hbin
->unknown
[0] = 0;
237 hbin
->unknown
[1] = 0;
238 unix_to_nt_time(&hbin
->last_change
, time(NULL
));
239 hbin
->block_size
= hbin
->offset_to_next
;
240 hbin
->data
= talloc_zero_array(hbin
, uint8_t, hbin
->block_size
- 0x20);
241 /* Update the regf header */
242 data
->header
->last_block
+= hbin
->offset_to_next
;
244 /* Set the next block to it's proper size and set the
245 * rel_offset for this block */
246 SIVAL(hbin
->data
, size
, hbin
->block_size
- size
- 0x20);
250 /* Set size and mark as used */
251 SIVAL(hbin
->data
, rel_offset
, -size
);
253 ret
.data
= hbin
->data
+ rel_offset
+ 0x4; /* Skip past length */
254 ret
.length
= size
- 0x4;
256 uint32_t new_rel_offset
;
257 *offset
= hbin
->offset_from_first
+ rel_offset
+ 0x20;
258 SMB_ASSERT(hbin_by_offset(data
, *offset
, &new_rel_offset
) == hbin
);
259 SMB_ASSERT(new_rel_offset
== rel_offset
);
265 /* Store a data blob. Return the offset at which it was stored */
266 static uint32_t hbin_store (struct regf_data
*data
, DATA_BLOB blob
)
269 DATA_BLOB dest
= hbin_alloc(data
, blob
.length
, &ret
);
271 memcpy(dest
.data
, blob
.data
, blob
.length
);
273 /* Make sure that we have no tailing garbage in the block */
274 if (dest
.length
> blob
.length
) {
275 memset(dest
.data
+ blob
.length
, 0, dest
.length
- blob
.length
);
281 static uint32_t hbin_store_tdr(struct regf_data
*data
,
282 tdr_push_fn_t push_fn
, void *p
)
284 struct tdr_push
*push
= tdr_push_init(data
);
287 if (NT_STATUS_IS_ERR(push_fn(push
, p
))) {
288 DEBUG(0, ("Error during push\n"));
292 ret
= hbin_store(data
, push
->data
);
300 /* Free existing data */
301 static void hbin_free (struct regf_data
*data
, uint32_t offset
)
306 struct hbin_block
*hbin
;
308 SMB_ASSERT (offset
> 0);
310 hbin
= hbin_by_offset(data
, offset
, &rel_offset
);
315 /* Get original size */
316 size
= IVALS(hbin
->data
, rel_offset
);
319 DEBUG(1, ("Trying to free already freed block at 0x%04x\n",
326 /* If the next block is free, merge into big free block */
327 if (rel_offset
+ size
< hbin
->offset_to_next
- 0x20) {
328 next_size
= IVALS(hbin
->data
, rel_offset
+size
);
334 /* Write block size */
335 SIVALS(hbin
->data
, rel_offset
, size
);
339 * Store a data blob data was already stored, but has changed in size
340 * Will try to save it at the current location if possible, otherwise
341 * does a free + store */
342 static uint32_t hbin_store_resize(struct regf_data
*data
,
343 uint32_t orig_offset
, DATA_BLOB blob
)
346 struct hbin_block
*hbin
= hbin_by_offset(data
, orig_offset
,
351 int32_t possible_size
;
354 SMB_ASSERT(orig_offset
> 0);
357 return hbin_store(data
, blob
);
359 /* Get original size */
360 orig_size
= -IVALS(hbin
->data
, rel_offset
);
362 needed_size
= blob
.length
+ 4; /* Add int32 containing length */
363 needed_size
= (needed_size
+ 7) & ~7; /* Align */
365 /* Fits into current allocated block */
366 if (orig_size
>= needed_size
) {
367 memcpy(hbin
->data
+ rel_offset
+ 0x4, blob
.data
, blob
.length
);
368 /* If the difference in size is greater than 0x4, split the block
369 * and free/merge it */
370 if (orig_size
- needed_size
> 0x4) {
371 SIVALS(hbin
->data
, rel_offset
, -needed_size
);
372 SIVALS(hbin
->data
, rel_offset
+ needed_size
,
373 needed_size
-orig_size
);
374 hbin_free(data
, orig_offset
+ needed_size
);
379 possible_size
= orig_size
;
381 /* Check if it can be combined with the next few free records */
382 for (i
= rel_offset
; i
< hbin
->offset_to_next
- 0x20; i
+= my_size
) {
383 if (IVALS(hbin
->data
, i
) < 0) /* Used */
386 my_size
= IVALS(hbin
->data
, i
);
388 if (my_size
== 0x0) {
389 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
392 possible_size
+= my_size
;
395 if (possible_size
>= blob
.length
) {
396 SIVAL(hbin
->data
, rel_offset
, -possible_size
);
397 memcpy(hbin
->data
+ rel_offset
+ 0x4,
398 blob
.data
, blob
.length
);
403 hbin_free(data
, orig_offset
);
404 return hbin_store(data
, blob
);
407 static uint32_t hbin_store_tdr_resize(struct regf_data
*regf
,
408 tdr_push_fn_t push_fn
,
409 uint32_t orig_offset
, void *p
)
411 struct tdr_push
*push
= tdr_push_init(regf
);
414 if (NT_STATUS_IS_ERR(push_fn(push
, p
))) {
415 DEBUG(0, ("Error during push\n"));
419 ret
= hbin_store_resize(regf
, orig_offset
, push
->data
);
426 static uint32_t regf_create_lh_hash(const char *name
)
432 hash_name
= strupper_talloc(NULL
, name
);
433 for (i
= 0; *(hash_name
+ i
) != 0; i
++) {
435 ret
+= *(hash_name
+ i
);
437 talloc_free(hash_name
);
441 static WERROR
regf_get_info(TALLOC_CTX
*mem_ctx
,
442 const struct hive_key
*key
,
443 const char **classname
,
444 uint32_t *num_subkeys
,
445 uint32_t *num_values
,
446 NTTIME
*last_mod_time
,
447 uint32_t *max_subkeynamelen
,
448 uint32_t *max_valnamelen
,
449 uint32_t *max_valbufsize
)
451 const struct regf_key_data
*private_data
=
452 (const struct regf_key_data
*)key
;
454 if (num_subkeys
!= NULL
)
455 *num_subkeys
= private_data
->nk
->num_subkeys
;
457 if (num_values
!= NULL
)
458 *num_values
= private_data
->nk
->num_values
;
460 if (classname
!= NULL
) {
461 if (private_data
->nk
->clsname_offset
!= -1) {
462 DATA_BLOB data
= hbin_get(private_data
->hive
,
463 private_data
->nk
->clsname_offset
);
464 *classname
= talloc_strndup(mem_ctx
,
466 private_data
->nk
->clsname_length
);
467 W_ERROR_HAVE_NO_MEMORY(*classname
);
472 /* TODO: Last mod time */
474 /* TODO: max valnamelen */
476 /* TODO: max valbufsize */
478 /* TODO: max subkeynamelen */
483 static struct regf_key_data
*regf_get_key(TALLOC_CTX
*ctx
,
484 struct regf_data
*regf
,
488 struct regf_key_data
*ret
;
490 ret
= talloc_zero(ctx
, struct regf_key_data
);
491 ret
->key
.ops
= ®_backend_regf
;
492 ret
->hive
= talloc_reference(ret
, regf
);
493 ret
->offset
= offset
;
494 nk
= talloc(ret
, struct nk_block
);
500 if (!hbin_get_tdr(regf
, offset
, nk
,
501 (tdr_pull_fn_t
)tdr_pull_nk_block
, nk
)) {
502 DEBUG(0, ("Unable to find HBIN data for offset 0x%x\n", offset
));
506 if (strcmp(nk
->header
, "nk") != 0) {
507 DEBUG(0, ("Expected nk record, got %s\n", nk
->header
));
516 static WERROR
regf_get_value(TALLOC_CTX
*ctx
, struct hive_key
*key
,
517 uint32_t idx
, const char **name
,
518 uint32_t *data_type
, DATA_BLOB
*data
)
520 const struct regf_key_data
*private_data
=
521 (const struct regf_key_data
*)key
;
523 struct regf_data
*regf
= private_data
->hive
;
527 if (idx
>= private_data
->nk
->num_values
)
528 return WERR_NO_MORE_ITEMS
;
530 tmp
= hbin_get(regf
, private_data
->nk
->values_offset
);
532 DEBUG(0, ("Unable to find value list at 0x%x\n",
533 private_data
->nk
->values_offset
));
534 return WERR_GENERAL_FAILURE
;
537 if (tmp
.length
< private_data
->nk
->num_values
* 4) {
538 DEBUG(1, ("Value counts mismatch\n"));
541 vk_offset
= IVAL(tmp
.data
, idx
* 4);
543 vk
= talloc(NULL
, struct vk_block
);
544 W_ERROR_HAVE_NO_MEMORY(vk
);
546 if (!hbin_get_tdr(regf
, vk_offset
, vk
,
547 (tdr_pull_fn_t
)tdr_pull_vk_block
, vk
)) {
548 DEBUG(0, ("Unable to get VK block at 0x%x\n", vk_offset
));
550 return WERR_GENERAL_FAILURE
;
553 /* FIXME: name character set ?*/
555 *name
= talloc_strndup(ctx
, vk
->data_name
, vk
->name_length
);
556 W_ERROR_HAVE_NO_MEMORY(*name
);
559 if (data_type
!= NULL
)
560 *data_type
= vk
->data_type
;
562 if (vk
->data_length
& 0x80000000) {
563 /* this is data of type "REG_DWORD" or "REG_DWORD_BIG_ENDIAN" */
564 data
->data
= talloc_size(ctx
, sizeof(uint32_t));
565 W_ERROR_HAVE_NO_MEMORY(data
->data
);
566 SIVAL(data
->data
, 0, vk
->data_offset
);
567 data
->length
= sizeof(uint32_t);
569 *data
= hbin_get(regf
, vk
->data_offset
);
572 if (data
->length
< vk
->data_length
) {
573 DEBUG(1, ("Read data less than indicated data length!\n"));
581 static WERROR
regf_get_value_by_name(TALLOC_CTX
*mem_ctx
,
582 struct hive_key
*key
, const char *name
,
583 uint32_t *type
, DATA_BLOB
*data
)
589 /* FIXME: Do binary search? Is this list sorted at all? */
591 for (i
= 0; W_ERROR_IS_OK(error
= regf_get_value(mem_ctx
, key
, i
,
592 &vname
, type
, data
));
594 if (!strcmp(vname
, name
))
598 if (W_ERROR_EQUAL(error
, WERR_NO_MORE_ITEMS
))
605 static WERROR
regf_get_subkey_by_index(TALLOC_CTX
*ctx
,
606 const struct hive_key
*key
,
607 uint32_t idx
, const char **name
,
608 const char **classname
,
609 NTTIME
*last_mod_time
)
612 struct regf_key_data
*ret
;
613 const struct regf_key_data
*private_data
= (const struct regf_key_data
*)key
;
614 struct nk_block
*nk
= private_data
->nk
;
617 if (idx
>= nk
->num_subkeys
)
618 return WERR_NO_MORE_ITEMS
;
620 /* Make sure that we don't crash if the key is empty */
621 if (nk
->subkeys_offset
== -1) {
622 return WERR_NO_MORE_ITEMS
;
625 data
= hbin_get(private_data
->hive
, nk
->subkeys_offset
);
627 DEBUG(0, ("Unable to find subkey list at 0x%x\n",
628 nk
->subkeys_offset
));
629 return WERR_GENERAL_FAILURE
;
632 if (!strncmp((char *)data
.data
, "li", 2)) {
634 struct tdr_pull
*pull
= tdr_pull_init(private_data
->hive
);
636 DEBUG(10, ("Subkeys in LI list\n"));
639 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull
, nk
, &li
))) {
640 DEBUG(0, ("Error parsing LI list\n"));
642 return WERR_GENERAL_FAILURE
;
645 SMB_ASSERT(!strncmp(li
.header
, "li", 2));
647 if (li
.key_count
!= nk
->num_subkeys
) {
648 DEBUG(0, ("Subkey counts don't match\n"));
649 return WERR_GENERAL_FAILURE
;
651 key_off
= li
.nk_offset
[idx
];
653 } else if (!strncmp((char *)data
.data
, "lf", 2)) {
655 struct tdr_pull
*pull
= tdr_pull_init(private_data
->hive
);
657 DEBUG(10, ("Subkeys in LF list\n"));
660 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull
, nk
, &lf
))) {
661 DEBUG(0, ("Error parsing LF list\n"));
663 return WERR_GENERAL_FAILURE
;
666 SMB_ASSERT(!strncmp(lf
.header
, "lf", 2));
668 if (lf
.key_count
!= nk
->num_subkeys
) {
669 DEBUG(0, ("Subkey counts don't match\n"));
670 return WERR_GENERAL_FAILURE
;
673 key_off
= lf
.hr
[idx
].nk_offset
;
674 } else if (!strncmp((char *)data
.data
, "lh", 2)) {
676 struct tdr_pull
*pull
= tdr_pull_init(private_data
->hive
);
678 DEBUG(10, ("Subkeys in LH list\n"));
681 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull
, nk
, &lh
))) {
682 DEBUG(0, ("Error parsing LH list\n"));
684 return WERR_GENERAL_FAILURE
;
687 SMB_ASSERT(!strncmp(lh
.header
, "lh", 2));
689 if (lh
.key_count
!= nk
->num_subkeys
) {
690 DEBUG(0, ("Subkey counts don't match\n"));
691 return WERR_GENERAL_FAILURE
;
693 key_off
= lh
.hr
[idx
].nk_offset
;
694 } else if (!strncmp((char *)data
.data
, "ri", 2)) {
696 struct tdr_pull
*pull
= tdr_pull_init(ctx
);
698 uint16_t sublist_count
= 0;
700 DEBUG(10, ("Subkeys in RI list\n"));
703 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull
, nk
, &ri
))) {
704 DEBUG(0, ("Error parsing RI list\n"));
706 return WERR_GENERAL_FAILURE
;
708 SMB_ASSERT(!strncmp(ri
.header
, "ri", 2));
710 for (i
= 0; i
< ri
.key_count
; i
++) {
713 /* Get sublist data blob */
714 list_data
= hbin_get(private_data
->hive
, ri
.offset
[i
]);
715 if (!list_data
.data
) {
716 DEBUG(0, ("Error getting RI list."));
718 return WERR_GENERAL_FAILURE
;
721 pull
->data
= list_data
;
723 if (!strncmp((char *)list_data
.data
, "li", 2)) {
726 DEBUG(10, ("Subkeys in RI->LI list\n"));
728 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull
,
731 DEBUG(0, ("Error parsing LI list from RI\n"));
733 return WERR_GENERAL_FAILURE
;
735 SMB_ASSERT(!strncmp(li
.header
, "li", 2));
737 /* Advance to next sublist if necessary */
738 if (idx
>= sublist_count
+ li
.key_count
) {
739 sublist_count
+= li
.key_count
;
742 key_off
= li
.nk_offset
[idx
- sublist_count
];
743 sublist_count
+= li
.key_count
;
745 } else if (!strncmp((char *)list_data
.data
, "lh", 2)) {
748 DEBUG(10, ("Subkeys in RI->LH list\n"));
750 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull
,
753 DEBUG(0, ("Error parsing LH list from RI\n"));
755 return WERR_GENERAL_FAILURE
;
757 SMB_ASSERT(!strncmp(lh
.header
, "lh", 2));
759 /* Advance to next sublist if necessary */
760 if (idx
>= sublist_count
+ lh
.key_count
) {
761 sublist_count
+= lh
.key_count
;
764 key_off
= lh
.hr
[idx
- sublist_count
].nk_offset
;
765 sublist_count
+= lh
.key_count
;
768 DEBUG(0,("Unknown sublist in ri block\n"));
771 return WERR_GENERAL_FAILURE
;
778 if (idx
> sublist_count
) {
779 return WERR_NO_MORE_ITEMS
;
783 DEBUG(0, ("Unknown type for subkey list (0x%04x): %c%c\n",
784 nk
->subkeys_offset
, data
.data
[0], data
.data
[1]));
785 return WERR_GENERAL_FAILURE
;
788 ret
= regf_get_key (ctx
, private_data
->hive
, key_off
);
790 if (classname
!= NULL
) {
791 if (ret
->nk
->clsname_offset
!= -1) {
792 DATA_BLOB db
= hbin_get(ret
->hive
,
793 ret
->nk
->clsname_offset
);
794 *classname
= talloc_strndup(ctx
,
796 ret
->nk
->clsname_length
);
797 W_ERROR_HAVE_NO_MEMORY(*classname
);
802 if (last_mod_time
!= NULL
)
803 *last_mod_time
= ret
->nk
->last_change
;
806 *name
= talloc_steal(ctx
, ret
->nk
->key_name
);
813 static WERROR
regf_match_subkey_by_name(TALLOC_CTX
*ctx
,
814 const struct hive_key
*key
,
816 const char *name
, uint32_t *ret
)
818 DATA_BLOB subkey_data
;
819 struct nk_block subkey
;
820 struct tdr_pull
*pull
;
821 const struct regf_key_data
*private_data
=
822 (const struct regf_key_data
*)key
;
824 subkey_data
= hbin_get(private_data
->hive
, offset
);
825 if (!subkey_data
.data
) {
826 DEBUG(0, ("Unable to retrieve subkey HBIN\n"));
827 return WERR_GENERAL_FAILURE
;
830 pull
= tdr_pull_init(ctx
);
832 pull
->data
= subkey_data
;
834 if (NT_STATUS_IS_ERR(tdr_pull_nk_block(pull
, ctx
, &subkey
))) {
835 DEBUG(0, ("Error parsing NK structure.\n"));
837 return WERR_GENERAL_FAILURE
;
841 if (strncmp(subkey
.header
, "nk", 2)) {
842 DEBUG(0, ("Not an NK structure.\n"));
843 return WERR_GENERAL_FAILURE
;
846 if (!strcasecmp(subkey
.key_name
, name
)) {
854 static WERROR
regf_get_subkey_by_name(TALLOC_CTX
*ctx
,
855 const struct hive_key
*key
,
857 struct hive_key
**ret
)
860 const struct regf_key_data
*private_data
=
861 (const struct regf_key_data
*)key
;
862 struct nk_block
*nk
= private_data
->nk
;
863 uint32_t key_off
= 0;
865 /* Make sure that we don't crash if the key is empty */
866 if (nk
->subkeys_offset
== -1) {
870 data
= hbin_get(private_data
->hive
, nk
->subkeys_offset
);
872 DEBUG(0, ("Unable to find subkey list\n"));
873 return WERR_GENERAL_FAILURE
;
876 if (!strncmp((char *)data
.data
, "li", 2)) {
878 struct tdr_pull
*pull
= tdr_pull_init(ctx
);
881 DEBUG(10, ("Subkeys in LI list\n"));
884 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull
, nk
, &li
))) {
885 DEBUG(0, ("Error parsing LI list\n"));
887 return WERR_GENERAL_FAILURE
;
890 SMB_ASSERT(!strncmp(li
.header
, "li", 2));
892 if (li
.key_count
!= nk
->num_subkeys
) {
893 DEBUG(0, ("Subkey counts don't match\n"));
894 return WERR_GENERAL_FAILURE
;
897 for (i
= 0; i
< li
.key_count
; i
++) {
898 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk
, key
,
907 } else if (!strncmp((char *)data
.data
, "lf", 2)) {
909 struct tdr_pull
*pull
= tdr_pull_init(ctx
);
912 DEBUG(10, ("Subkeys in LF list\n"));
915 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull
, nk
, &lf
))) {
916 DEBUG(0, ("Error parsing LF list\n"));
918 return WERR_GENERAL_FAILURE
;
921 SMB_ASSERT(!strncmp(lf
.header
, "lf", 2));
923 if (lf
.key_count
!= nk
->num_subkeys
) {
924 DEBUG(0, ("Subkey counts don't match\n"));
925 return WERR_GENERAL_FAILURE
;
928 for (i
= 0; i
< lf
.key_count
; i
++) {
929 if (strncmp(lf
.hr
[i
].hash
, name
, 4)) {
932 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk
,
942 } else if (!strncmp((char *)data
.data
, "lh", 2)) {
944 struct tdr_pull
*pull
= tdr_pull_init(ctx
);
948 DEBUG(10, ("Subkeys in LH list\n"));
951 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull
, nk
, &lh
))) {
952 DEBUG(0, ("Error parsing LH list\n"));
954 return WERR_GENERAL_FAILURE
;
957 SMB_ASSERT(!strncmp(lh
.header
, "lh", 2));
959 if (lh
.key_count
!= nk
->num_subkeys
) {
960 DEBUG(0, ("Subkey counts don't match\n"));
961 return WERR_GENERAL_FAILURE
;
964 hash
= regf_create_lh_hash(name
);
965 for (i
= 0; i
< lh
.key_count
; i
++) {
966 if (lh
.hr
[i
].base37
!= hash
) {
969 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk
,
979 } else if (!strncmp((char *)data
.data
, "ri", 2)) {
981 struct tdr_pull
*pull
= tdr_pull_init(ctx
);
984 DEBUG(10, ("Subkeys in RI list\n"));
987 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(pull
, nk
, &ri
))) {
988 DEBUG(0, ("Error parsing RI list\n"));
990 return WERR_GENERAL_FAILURE
;
992 SMB_ASSERT(!strncmp(ri
.header
, "ri", 2));
994 for (i
= 0; i
< ri
.key_count
; i
++) {
997 /* Get sublist data blob */
998 list_data
= hbin_get(private_data
->hive
, ri
.offset
[i
]);
999 if (list_data
.data
== NULL
) {
1000 DEBUG(0, ("Error getting RI list."));
1002 return WERR_GENERAL_FAILURE
;
1005 pull
->data
= list_data
;
1007 if (!strncmp((char *)list_data
.data
, "li", 2)) {
1010 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull
,
1013 DEBUG(0, ("Error parsing LI list from RI\n"));
1015 return WERR_GENERAL_FAILURE
;
1017 SMB_ASSERT(!strncmp(li
.header
, "li", 2));
1019 for (j
= 0; j
< li
.key_count
; j
++) {
1020 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk
, key
,
1027 } else if (!strncmp((char *)list_data
.data
, "lh", 2)) {
1031 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull
,
1034 DEBUG(0, ("Error parsing LH list from RI\n"));
1036 return WERR_GENERAL_FAILURE
;
1038 SMB_ASSERT(!strncmp(lh
.header
, "lh", 2));
1040 hash
= regf_create_lh_hash(name
);
1041 for (j
= 0; j
< lh
.key_count
; j
++) {
1042 if (lh
.hr
[j
].base37
!= hash
) {
1045 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk
, key
,
1058 return WERR_BADFILE
;
1060 DEBUG(0, ("Unknown subkey list type.\n"));
1061 return WERR_GENERAL_FAILURE
;
1064 *ret
= (struct hive_key
*)regf_get_key(ctx
, private_data
->hive
,
1069 static WERROR
regf_set_sec_desc(struct hive_key
*key
,
1070 const struct security_descriptor
*sec_desc
)
1072 const struct regf_key_data
*private_data
=
1073 (const struct regf_key_data
*)key
;
1074 struct sk_block cur_sk
, sk
, new_sk
;
1075 struct regf_data
*regf
= private_data
->hive
;
1076 struct nk_block root
;
1078 uint32_t sk_offset
, cur_sk_offset
;
1079 bool update_cur_sk
= false;
1081 /* Get the root nk */
1082 hbin_get_tdr(regf
, regf
->header
->data_offset
, regf
,
1083 (tdr_pull_fn_t
) tdr_pull_nk_block
, &root
);
1085 /* Push the security descriptor to a blob */
1086 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data
, regf
,
1087 sec_desc
, (ndr_push_flags_fn_t
)ndr_push_security_descriptor
))) {
1088 DEBUG(0, ("Unable to push security descriptor\n"));
1089 return WERR_GENERAL_FAILURE
;
1092 /* Get the current security descriptor for the key */
1093 if (!hbin_get_tdr(regf
, private_data
->nk
->sk_offset
, regf
,
1094 (tdr_pull_fn_t
) tdr_pull_sk_block
, &cur_sk
)) {
1095 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1096 return WERR_BADFILE
;
1098 /* If there's no change, change nothing. */
1099 if (memcmp(data
.data
, cur_sk
.sec_desc
,
1100 MIN(data
.length
, cur_sk
.rec_size
)) == 0) {
1104 /* Delete the current sk if only this key is using it */
1105 if (cur_sk
.ref_cnt
== 1) {
1106 /* Get the previous security descriptor for the key */
1107 if (!hbin_get_tdr(regf
, cur_sk
.prev_offset
, regf
,
1108 (tdr_pull_fn_t
) tdr_pull_sk_block
, &sk
)) {
1109 DEBUG(0, ("Unable to find prev security descriptor for current key\n"));
1110 return WERR_BADFILE
;
1112 /* Change and store the previous security descriptor */
1113 sk
.next_offset
= cur_sk
.next_offset
;
1114 hbin_store_tdr_resize(regf
, (tdr_push_fn_t
) tdr_push_sk_block
,
1115 cur_sk
.prev_offset
, &sk
);
1117 /* Get the next security descriptor for the key */
1118 if (!hbin_get_tdr(regf
, cur_sk
.next_offset
, regf
,
1119 (tdr_pull_fn_t
) tdr_pull_sk_block
, &sk
)) {
1120 DEBUG(0, ("Unable to find next security descriptor for current key\n"));
1121 return WERR_BADFILE
;
1123 /* Change and store the next security descriptor */
1124 sk
.prev_offset
= cur_sk
.prev_offset
;
1125 hbin_store_tdr_resize(regf
, (tdr_push_fn_t
) tdr_push_sk_block
,
1126 cur_sk
.next_offset
, &sk
);
1128 hbin_free(regf
, private_data
->nk
->sk_offset
);
1130 /* This key will no longer be referring to this sk */
1132 update_cur_sk
= true;
1135 sk_offset
= root
.sk_offset
;
1138 cur_sk_offset
= sk_offset
;
1139 if (!hbin_get_tdr(regf
, sk_offset
, regf
,
1140 (tdr_pull_fn_t
) tdr_pull_sk_block
, &sk
)) {
1141 DEBUG(0, ("Unable to find security descriptor\n"));
1142 return WERR_BADFILE
;
1144 if (memcmp(data
.data
, sk
.sec_desc
, MIN(data
.length
, sk
.rec_size
)) == 0) {
1145 private_data
->nk
->sk_offset
= sk_offset
;
1147 hbin_store_tdr_resize(regf
,
1148 (tdr_push_fn_t
) tdr_push_sk_block
,
1150 hbin_store_tdr_resize(regf
,
1151 (tdr_push_fn_t
) tdr_push_nk_block
,
1152 private_data
->offset
,
1156 sk_offset
= sk
.next_offset
;
1157 } while (sk_offset
!= root
.sk_offset
);
1159 ZERO_STRUCT(new_sk
);
1160 new_sk
.header
= "sk";
1161 new_sk
.prev_offset
= cur_sk_offset
;
1162 new_sk
.next_offset
= root
.sk_offset
;
1164 new_sk
.rec_size
= data
.length
;
1165 new_sk
.sec_desc
= data
.data
;
1167 sk_offset
= hbin_store_tdr(regf
,
1168 (tdr_push_fn_t
) tdr_push_sk_block
,
1170 if (sk_offset
== -1) {
1171 DEBUG(0, ("Error storing sk block\n"));
1172 return WERR_GENERAL_FAILURE
;
1174 private_data
->nk
->sk_offset
= sk_offset
;
1176 if (update_cur_sk
) {
1177 hbin_store_tdr_resize(regf
,
1178 (tdr_push_fn_t
) tdr_push_sk_block
,
1179 private_data
->nk
->sk_offset
, &cur_sk
);
1182 /* Get the previous security descriptor for the key */
1183 if (!hbin_get_tdr(regf
, new_sk
.prev_offset
, regf
,
1184 (tdr_pull_fn_t
) tdr_pull_sk_block
, &sk
)) {
1185 DEBUG(0, ("Unable to find security descriptor for previous key\n"));
1186 return WERR_BADFILE
;
1188 /* Change and store the previous security descriptor */
1189 sk
.next_offset
= sk_offset
;
1190 hbin_store_tdr_resize(regf
,
1191 (tdr_push_fn_t
) tdr_push_sk_block
,
1192 cur_sk
.prev_offset
, &sk
);
1194 /* Get the next security descriptor for the key (always root, as we append) */
1195 if (!hbin_get_tdr(regf
, new_sk
.next_offset
, regf
,
1196 (tdr_pull_fn_t
) tdr_pull_sk_block
, &sk
)) {
1197 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1198 return WERR_BADFILE
;
1200 /* Change and store the next security descriptor (always root, as we append) */
1201 sk
.prev_offset
= sk_offset
;
1202 hbin_store_tdr_resize(regf
,
1203 (tdr_push_fn_t
) tdr_push_sk_block
,
1204 root
.sk_offset
, &sk
);
1208 hbin_store_tdr_resize(regf
,
1209 (tdr_push_fn_t
) tdr_push_sk_block
,
1210 private_data
->offset
, private_data
->nk
);
1214 static WERROR
regf_get_sec_desc(TALLOC_CTX
*ctx
, const struct hive_key
*key
,
1215 struct security_descriptor
**sd
)
1217 const struct regf_key_data
*private_data
=
1218 (const struct regf_key_data
*)key
;
1220 struct regf_data
*regf
= private_data
->hive
;
1223 if (!hbin_get_tdr(regf
, private_data
->nk
->sk_offset
, ctx
,
1224 (tdr_pull_fn_t
) tdr_pull_sk_block
, &sk
)) {
1225 DEBUG(0, ("Unable to find security descriptor\n"));
1226 return WERR_GENERAL_FAILURE
;
1229 if (strcmp(sk
.header
, "sk") != 0) {
1230 DEBUG(0, ("Expected 'sk', got '%s'\n", sk
.header
));
1231 return WERR_GENERAL_FAILURE
;
1234 *sd
= talloc(ctx
, struct security_descriptor
);
1235 W_ERROR_HAVE_NO_MEMORY(*sd
);
1237 data
.data
= sk
.sec_desc
;
1238 data
.length
= sk
.rec_size
;
1239 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_struct_blob(&data
, ctx
, *sd
,
1240 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
))) {
1241 DEBUG(0, ("Error parsing security descriptor\n"));
1242 return WERR_GENERAL_FAILURE
;
1248 static WERROR
regf_sl_add_entry(struct regf_data
*regf
, uint32_t list_offset
,
1250 uint32_t key_offset
, uint32_t *ret
)
1254 /* Create a new key if necessary */
1255 if (list_offset
== -1) {
1256 if (regf
->header
->version
.major
!= 1) {
1257 DEBUG(0, ("Can't store keys in unknown registry format\n"));
1258 return WERR_NOT_SUPPORTED
;
1260 if (regf
->header
->version
.minor
< 3) {
1267 li
.nk_offset
= talloc_array(regf
, uint32_t, 1);
1268 W_ERROR_HAVE_NO_MEMORY(li
.nk_offset
);
1269 li
.nk_offset
[0] = key_offset
;
1271 *ret
= hbin_store_tdr(regf
,
1272 (tdr_push_fn_t
) tdr_push_li_block
,
1275 talloc_free(li
.nk_offset
);
1276 } else if (regf
->header
->version
.minor
== 3 ||
1277 regf
->header
->version
.minor
== 4) {
1284 lf
.hr
= talloc_array(regf
, struct hash_record
, 1);
1285 W_ERROR_HAVE_NO_MEMORY(lf
.hr
);
1286 lf
.hr
[0].nk_offset
= key_offset
;
1287 lf
.hr
[0].hash
= talloc_strndup(lf
.hr
, name
, 4);
1288 W_ERROR_HAVE_NO_MEMORY(lf
.hr
[0].hash
);
1290 *ret
= hbin_store_tdr(regf
,
1291 (tdr_push_fn_t
) tdr_push_lf_block
,
1295 } else if (regf
->header
->version
.minor
== 5) {
1302 lh
.hr
= talloc_array(regf
, struct lh_hash
, 1);
1303 W_ERROR_HAVE_NO_MEMORY(lh
.hr
);
1304 lh
.hr
[0].nk_offset
= key_offset
;
1305 lh
.hr
[0].base37
= regf_create_lh_hash(name
);
1307 *ret
= hbin_store_tdr(regf
,
1308 (tdr_push_fn_t
) tdr_push_lh_block
,
1316 data
= hbin_get(regf
, list_offset
);
1318 DEBUG(0, ("Unable to find subkey list\n"));
1319 return WERR_BADFILE
;
1322 if (!strncmp((char *)data
.data
, "li", 2)) {
1323 struct tdr_pull
*pull
= tdr_pull_init(regf
);
1325 struct nk_block sub_nk
;
1330 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull
, regf
, &li
))) {
1331 DEBUG(0, ("Error parsing LI list\n"));
1333 return WERR_BADFILE
;
1337 if (strncmp(li
.header
, "li", 2) != 0) {
1339 DEBUG(0, ("LI header corrupt\n"));
1340 return WERR_BADFILE
;
1344 * Find the position to store the pointer
1345 * Extensive testing reveils that at least on windows 7 subkeys
1346 * *MUST* be stored in alphabetical order
1348 for (i
= 0; i
< li
.key_count
; i
++) {
1350 hbin_get_tdr(regf
, li
.nk_offset
[i
], regf
,
1351 (tdr_pull_fn_t
) tdr_pull_nk_block
, &sub_nk
);
1352 if (strcasecmp(name
, sub_nk
.key_name
) < 0) {
1357 li
.nk_offset
= talloc_realloc(regf
, li
.nk_offset
,
1358 uint32_t, li
.key_count
+1);
1359 W_ERROR_HAVE_NO_MEMORY(li
.nk_offset
);
1361 /* Move everything behind this offset */
1362 for (j
= li
.key_count
- 1; j
>= i
; j
--) {
1363 li
.nk_offset
[j
+1] = li
.nk_offset
[j
];
1366 li
.nk_offset
[i
] = key_offset
;
1368 *ret
= hbin_store_tdr_resize(regf
,
1369 (tdr_push_fn_t
)tdr_push_li_block
,
1372 talloc_free(li
.nk_offset
);
1373 } else if (!strncmp((char *)data
.data
, "lf", 2)) {
1374 struct tdr_pull
*pull
= tdr_pull_init(regf
);
1376 struct nk_block sub_nk
;
1381 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull
, regf
, &lf
))) {
1382 DEBUG(0, ("Error parsing LF list\n"));
1384 return WERR_BADFILE
;
1387 SMB_ASSERT(!strncmp(lf
.header
, "lf", 2));
1390 * Find the position to store the hash record
1391 * Extensive testing reveils that at least on windows 7 subkeys
1392 * *MUST* be stored in alphabetical order
1394 for (i
= 0; i
< lf
.key_count
; i
++) {
1396 hbin_get_tdr(regf
, lf
.hr
[i
].nk_offset
, regf
,
1397 (tdr_pull_fn_t
) tdr_pull_nk_block
, &sub_nk
);
1398 if (strcasecmp(name
, sub_nk
.key_name
) < 0) {
1403 lf
.hr
= talloc_realloc(regf
, lf
.hr
, struct hash_record
,
1405 W_ERROR_HAVE_NO_MEMORY(lf
.hr
);
1407 /* Move everything behind this hash record */
1408 for (j
= lf
.key_count
- 1; j
>= i
; j
--) {
1409 lf
.hr
[j
+1] = lf
.hr
[j
];
1412 lf
.hr
[i
].nk_offset
= key_offset
;
1413 lf
.hr
[i
].hash
= talloc_strndup(lf
.hr
, name
, 4);
1414 W_ERROR_HAVE_NO_MEMORY(lf
.hr
[lf
.key_count
].hash
);
1416 *ret
= hbin_store_tdr_resize(regf
,
1417 (tdr_push_fn_t
)tdr_push_lf_block
,
1421 } else if (!strncmp((char *)data
.data
, "lh", 2)) {
1422 struct tdr_pull
*pull
= tdr_pull_init(regf
);
1424 struct nk_block sub_nk
;
1429 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull
, regf
, &lh
))) {
1430 DEBUG(0, ("Error parsing LH list\n"));
1432 return WERR_BADFILE
;
1435 SMB_ASSERT(!strncmp(lh
.header
, "lh", 2));
1438 * Find the position to store the hash record
1439 * Extensive testing reveils that at least on windows 7 subkeys
1440 * *MUST* be stored in alphabetical order
1442 for (i
= 0; i
< lh
.key_count
; i
++) {
1444 hbin_get_tdr(regf
, lh
.hr
[i
].nk_offset
, regf
,
1445 (tdr_pull_fn_t
) tdr_pull_nk_block
, &sub_nk
);
1446 if (strcasecmp(name
, sub_nk
.key_name
) < 0) {
1451 lh
.hr
= talloc_realloc(regf
, lh
.hr
, struct lh_hash
,
1453 W_ERROR_HAVE_NO_MEMORY(lh
.hr
);
1455 /* Move everything behind this hash record */
1456 for (j
= lh
.key_count
- 1; j
>= i
; j
--) {
1457 lh
.hr
[j
+1] = lh
.hr
[j
];
1460 lh
.hr
[i
].nk_offset
= key_offset
;
1461 lh
.hr
[i
].base37
= regf_create_lh_hash(name
);
1463 *ret
= hbin_store_tdr_resize(regf
,
1464 (tdr_push_fn_t
)tdr_push_lh_block
,
1468 } else if (!strncmp((char *)data
.data
, "ri", 2)) {
1470 DEBUG(0, ("Adding to 'ri' subkey list is not supported yet.\n"));
1471 return WERR_NOT_SUPPORTED
;
1473 DEBUG(0, ("Cannot add to unknown subkey list\n"));
1474 return WERR_BADFILE
;
1480 static WERROR
regf_sl_del_entry(struct regf_data
*regf
, uint32_t list_offset
,
1481 uint32_t key_offset
, uint32_t *ret
)
1485 data
= hbin_get(regf
, list_offset
);
1487 DEBUG(0, ("Unable to find subkey list\n"));
1488 return WERR_BADFILE
;
1491 if (strncmp((char *)data
.data
, "li", 2) == 0) {
1493 struct tdr_pull
*pull
= tdr_pull_init(regf
);
1495 bool found_offset
= false;
1497 DEBUG(10, ("Subkeys in LI list\n"));
1501 if (NT_STATUS_IS_ERR(tdr_pull_li_block(pull
, regf
, &li
))) {
1502 DEBUG(0, ("Error parsing LI list\n"));
1504 return WERR_BADFILE
;
1508 SMB_ASSERT(!strncmp(li
.header
, "li", 2));
1510 for (i
= 0; i
< li
.key_count
; i
++) {
1512 li
.nk_offset
[i
-1] = li
.nk_offset
[i
];
1514 if (li
.nk_offset
[i
] == key_offset
) {
1515 found_offset
= true;
1519 if (!found_offset
) {
1520 DEBUG(2, ("Subkey not found\n"));
1521 return WERR_BADFILE
;
1525 /* If the there are no entries left, free the subkey list */
1526 if (li
.key_count
== 0) {
1527 hbin_free(regf
, list_offset
);
1531 /* Store li block */
1532 *ret
= hbin_store_tdr_resize(regf
,
1533 (tdr_push_fn_t
) tdr_push_li_block
,
1535 } else if (strncmp((char *)data
.data
, "lf", 2) == 0) {
1537 struct tdr_pull
*pull
= tdr_pull_init(regf
);
1539 bool found_offset
= false;
1541 DEBUG(10, ("Subkeys in LF list\n"));
1545 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(pull
, regf
, &lf
))) {
1546 DEBUG(0, ("Error parsing LF list\n"));
1548 return WERR_BADFILE
;
1552 SMB_ASSERT(!strncmp(lf
.header
, "lf", 2));
1554 for (i
= 0; i
< lf
.key_count
; i
++) {
1556 lf
.hr
[i
-1] = lf
.hr
[i
];
1559 if (lf
.hr
[i
].nk_offset
== key_offset
) {
1564 if (!found_offset
) {
1565 DEBUG(2, ("Subkey not found\n"));
1566 return WERR_BADFILE
;
1570 /* If the there are no entries left, free the subkey list */
1571 if (lf
.key_count
== 0) {
1572 hbin_free(regf
, list_offset
);
1577 /* Store lf block */
1578 *ret
= hbin_store_tdr_resize(regf
,
1579 (tdr_push_fn_t
) tdr_push_lf_block
,
1581 } else if (strncmp((char *)data
.data
, "lh", 2) == 0) {
1583 struct tdr_pull
*pull
= tdr_pull_init(regf
);
1585 bool found_offset
= false;
1587 DEBUG(10, ("Subkeys in LH list\n"));
1591 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(pull
, regf
, &lh
))) {
1592 DEBUG(0, ("Error parsing LF list\n"));
1594 return WERR_BADFILE
;
1598 SMB_ASSERT(!strncmp(lh
.header
, "lh", 2));
1600 for (i
= 0; i
< lh
.key_count
; i
++) {
1602 lh
.hr
[i
-1] = lh
.hr
[i
];
1605 if (lh
.hr
[i
].nk_offset
== key_offset
) {
1610 if (!found_offset
) {
1611 DEBUG(0, ("Subkey not found\n"));
1612 return WERR_BADFILE
;
1616 /* If the there are no entries left, free the subkey list */
1617 if (lh
.key_count
== 0) {
1618 hbin_free(regf
, list_offset
);
1623 /* Store lh block */
1624 *ret
= hbin_store_tdr_resize(regf
,
1625 (tdr_push_fn_t
) tdr_push_lh_block
,
1627 } else if (strncmp((char *)data
.data
, "ri", 2) == 0) {
1629 DEBUG(0, ("Sorry, deletion from ri block is not supported yet.\n"));
1630 return WERR_NOT_SUPPORTED
;
1632 DEBUG (0, ("Unknown header found in subkey list.\n"));
1633 return WERR_BADFILE
;
1638 static WERROR
regf_del_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*key
,
1641 struct regf_key_data
*private_data
= (struct regf_key_data
*)key
;
1642 struct regf_data
*regf
= private_data
->hive
;
1643 struct nk_block
*nk
= private_data
->nk
;
1646 bool found_offset
= false;
1650 if (nk
->values_offset
== -1) {
1651 return WERR_BADFILE
;
1654 values
= hbin_get(regf
, nk
->values_offset
);
1656 for (i
= 0; i
< nk
->num_values
; i
++) {
1658 ((uint32_t *)values
.data
)[i
-1] = ((uint32_t *) values
.data
)[i
];
1660 vk_offset
= IVAL(values
.data
, i
* 4);
1661 if (!hbin_get_tdr(regf
, vk_offset
, private_data
,
1662 (tdr_pull_fn_t
)tdr_pull_vk_block
,
1664 DEBUG(0, ("Unable to get VK block at %d\n",
1666 return WERR_BADFILE
;
1668 if (strcmp(vk
.data_name
, name
) == 0) {
1669 hbin_free(regf
, vk_offset
);
1670 found_offset
= true;
1674 if (!found_offset
) {
1675 return WERR_BADFILE
;
1678 values
.length
= (nk
->num_values
)*4;
1681 /* Store values list and nk */
1682 if (nk
->num_values
== 0) {
1683 hbin_free(regf
, nk
->values_offset
);
1684 nk
->values_offset
= -1;
1686 nk
->values_offset
= hbin_store_resize(regf
,
1690 hbin_store_tdr_resize(regf
, (tdr_push_fn_t
) tdr_push_nk_block
,
1691 private_data
->offset
, nk
);
1693 return regf_save_hbin(private_data
->hive
, 0);
1697 static WERROR
regf_del_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*parent
,
1700 const struct regf_key_data
*private_data
=
1701 (const struct regf_key_data
*)parent
;
1702 struct regf_key_data
*key
;
1703 struct nk_block
*parent_nk
;
1706 SMB_ASSERT(private_data
);
1708 parent_nk
= private_data
->nk
;
1710 if (parent_nk
->subkeys_offset
== -1) {
1711 DEBUG(4, ("Subkey list is empty, this key cannot contain subkeys.\n"));
1712 return WERR_BADFILE
;
1716 if (!W_ERROR_IS_OK(regf_get_subkey_by_name(parent_nk
, parent
, name
,
1717 (struct hive_key
**)&key
))) {
1718 DEBUG(2, ("Key '%s' not found\n", name
));
1719 return WERR_BADFILE
;
1722 if (key
->nk
->subkeys_offset
!= -1) {
1724 struct hive_key
*sk
= (struct hive_key
*)key
;
1725 unsigned int i
= key
->nk
->num_subkeys
;
1727 /* Get subkey information. */
1728 error
= regf_get_subkey_by_index(parent_nk
, sk
, 0,
1729 (const char **)&sk_name
,
1731 if (!W_ERROR_IS_OK(error
)) {
1732 DEBUG(0, ("Can't retrieve subkey by index.\n"));
1736 /* Delete subkey. */
1737 error
= regf_del_key(NULL
, sk
, sk_name
);
1738 if (!W_ERROR_IS_OK(error
)) {
1739 DEBUG(0, ("Can't delete key '%s'.\n", sk_name
));
1743 talloc_free(sk_name
);
1747 if (key
->nk
->values_offset
!= -1) {
1749 struct hive_key
*sk
= (struct hive_key
*)key
;
1751 unsigned int i
= key
->nk
->num_values
;
1753 /* Get value information. */
1754 error
= regf_get_value(parent_nk
, sk
, 0,
1755 (const char **)&val_name
,
1757 if (!W_ERROR_IS_OK(error
)) {
1758 DEBUG(0, ("Can't retrieve value by index.\n"));
1763 error
= regf_del_value(NULL
, sk
, val_name
);
1764 if (!W_ERROR_IS_OK(error
)) {
1765 DEBUG(0, ("Can't delete value '%s'.\n", val_name
));
1769 talloc_free(val_name
);
1773 /* Delete it from the subkey list. */
1774 error
= regf_sl_del_entry(private_data
->hive
, parent_nk
->subkeys_offset
,
1775 key
->offset
, &parent_nk
->subkeys_offset
);
1776 if (!W_ERROR_IS_OK(error
)) {
1777 DEBUG(0, ("Can't store new subkey list for parent key. Won't delete.\n"));
1781 /* Re-store parent key */
1782 parent_nk
->num_subkeys
--;
1783 hbin_store_tdr_resize(private_data
->hive
,
1784 (tdr_push_fn_t
) tdr_push_nk_block
,
1785 private_data
->offset
, parent_nk
);
1787 if (key
->nk
->clsname_offset
!= -1) {
1788 hbin_free(private_data
->hive
, key
->nk
->clsname_offset
);
1790 hbin_free(private_data
->hive
, key
->offset
);
1792 return regf_save_hbin(private_data
->hive
, 0);
1795 static WERROR
regf_add_key(TALLOC_CTX
*ctx
, const struct hive_key
*parent
,
1796 const char *name
, const char *classname
,
1797 struct security_descriptor
*sec_desc
,
1798 struct hive_key
**ret
)
1800 const struct regf_key_data
*private_data
=
1801 (const struct regf_key_data
*)parent
;
1802 struct nk_block
*parent_nk
= private_data
->nk
, nk
;
1803 struct nk_block
*root
;
1804 struct regf_data
*regf
= private_data
->hive
;
1809 nk
.type
= REG_SUB_KEY
;
1810 unix_to_nt_time(&nk
.last_change
, time(NULL
));
1812 nk
.parent_offset
= private_data
->offset
;
1815 nk
.subkeys_offset
= -1;
1816 nk
.unknown_offset
= -1;
1818 nk
.values_offset
= -1;
1819 memset(nk
.unk3
, 0, sizeof(nk
.unk3
));
1820 nk
.clsname_offset
= -1; /* FIXME: fill in */
1821 nk
.clsname_length
= 0;
1824 /* Get the security descriptor of the root key */
1825 root
= talloc_zero(ctx
, struct nk_block
);
1826 W_ERROR_HAVE_NO_MEMORY(root
);
1828 if (!hbin_get_tdr(regf
, regf
->header
->data_offset
, root
,
1829 (tdr_pull_fn_t
)tdr_pull_nk_block
, root
)) {
1830 DEBUG(0, ("Unable to find HBIN data for offset 0x%x\n",
1831 regf
->header
->data_offset
));
1832 return WERR_GENERAL_FAILURE
;
1834 nk
.sk_offset
= root
->sk_offset
;
1837 /* Store the new nk key */
1838 offset
= hbin_store_tdr(regf
, (tdr_push_fn_t
) tdr_push_nk_block
, &nk
);
1840 error
= regf_sl_add_entry(regf
, parent_nk
->subkeys_offset
, name
, offset
,
1841 &parent_nk
->subkeys_offset
);
1842 if (!W_ERROR_IS_OK(error
)) {
1843 hbin_free(regf
, offset
);
1847 parent_nk
->num_subkeys
++;
1849 /* Since the subkey offset of the parent can change, store it again */
1850 hbin_store_tdr_resize(regf
, (tdr_push_fn_t
) tdr_push_nk_block
,
1851 nk
.parent_offset
, parent_nk
);
1853 *ret
= (struct hive_key
*)regf_get_key(ctx
, regf
, offset
);
1855 DEBUG(9, ("Storing key %s\n", name
));
1856 return regf_save_hbin(private_data
->hive
, 0);
1859 static WERROR
regf_set_value(struct hive_key
*key
, const char *name
,
1860 uint32_t type
, const DATA_BLOB data
)
1862 struct regf_key_data
*private_data
= (struct regf_key_data
*)key
;
1863 struct regf_data
*regf
= private_data
->hive
;
1864 struct nk_block
*nk
= private_data
->nk
;
1867 uint32_t tmp_vk_offset
, vk_offset
, old_vk_offset
= (uint32_t) -1;
1872 /* find the value offset, if it exists */
1873 if (nk
->values_offset
!= -1) {
1874 values
= hbin_get(regf
, nk
->values_offset
);
1876 for (i
= 0; i
< nk
->num_values
; i
++) {
1877 tmp_vk_offset
= IVAL(values
.data
, i
* 4);
1878 if (!hbin_get_tdr(regf
, tmp_vk_offset
, private_data
,
1879 (tdr_pull_fn_t
)tdr_pull_vk_block
,
1881 DEBUG(0, ("Unable to get VK block at 0x%x\n",
1883 return WERR_GENERAL_FAILURE
;
1885 if (strcmp(vk
.data_name
, name
) == 0) {
1886 old_vk_offset
= tmp_vk_offset
;
1892 /* If it's new, create the vk struct, if it's old, free the old data. */
1893 if (old_vk_offset
== -1) {
1895 if (name
!= NULL
&& name
[0] != '\0') {
1897 vk
.data_name
= name
;
1898 vk
.name_length
= strlen(name
);
1901 vk
.data_name
= NULL
;
1905 /* Free data, if any */
1906 if (!(vk
.data_length
& 0x80000000)) {
1907 hbin_free(regf
, vk
.data_offset
);
1911 /* Set the type and data */
1912 vk
.data_length
= data
.length
;
1913 vk
.data_type
= type
;
1914 if ((type
== REG_DWORD
) || (type
== REG_DWORD_BIG_ENDIAN
)) {
1915 if (vk
.data_length
!= sizeof(uint32_t)) {
1916 DEBUG(0, ("DWORD or DWORD_BIG_ENDIAN value with size other than 4 byte!\n"));
1917 return WERR_NOT_SUPPORTED
;
1919 vk
.data_length
|= 0x80000000;
1920 vk
.data_offset
= IVAL(data
.data
, 0);
1922 /* Store data somewhere */
1923 vk
.data_offset
= hbin_store(regf
, data
);
1925 if (old_vk_offset
== -1) {
1927 vk_offset
= hbin_store_tdr(regf
,
1928 (tdr_push_fn_t
) tdr_push_vk_block
,
1931 /* Store vk at offset */
1932 vk_offset
= hbin_store_tdr_resize(regf
,
1933 (tdr_push_fn_t
) tdr_push_vk_block
,
1934 old_vk_offset
,&vk
);
1937 /* Re-allocate the value list */
1938 if (nk
->values_offset
== -1) {
1939 nk
->values_offset
= hbin_store_tdr(regf
,
1940 (tdr_push_fn_t
) tdr_push_uint32
,
1945 /* Change if we're changing, otherwise we're adding the value */
1946 if (old_vk_offset
!= -1) {
1947 /* Find and overwrite the offset. */
1948 for (i
= 0; i
< nk
->num_values
; i
++) {
1949 if (IVAL(values
.data
, i
* 4) == old_vk_offset
) {
1950 SIVAL(values
.data
, i
* 4, vk_offset
);
1955 /* Create a new value list */
1956 DATA_BLOB value_list
;
1958 value_list
.length
= (nk
->num_values
+1)*4;
1959 value_list
.data
= (uint8_t *)talloc_array(private_data
,
1962 W_ERROR_HAVE_NO_MEMORY(value_list
.data
);
1963 memcpy(value_list
.data
, values
.data
, nk
->num_values
* 4);
1965 SIVAL(value_list
.data
, nk
->num_values
* 4, vk_offset
);
1967 nk
->values_offset
= hbin_store_resize(regf
,
1973 hbin_store_tdr_resize(regf
,
1974 (tdr_push_fn_t
) tdr_push_nk_block
,
1975 private_data
->offset
, nk
);
1976 return regf_save_hbin(private_data
->hive
, 0);
1979 static WERROR
regf_save_hbin(struct regf_data
*regf
, bool flush
)
1981 struct tdr_push
*push
= tdr_push_init(regf
);
1984 W_ERROR_HAVE_NO_MEMORY(push
);
1986 /* Only write once every 5 seconds, or when flush is set */
1987 if (!flush
&& regf
->last_write
+ 5 >= time(NULL
)) {
1991 regf
->last_write
= time(NULL
);
1993 if (lseek(regf
->fd
, 0, SEEK_SET
) == -1) {
1994 DEBUG(0, ("Error lseeking in regf file\n"));
1995 return WERR_GENERAL_FAILURE
;
1998 /* Recompute checksum */
1999 if (NT_STATUS_IS_ERR(tdr_push_regf_hdr(push
, regf
->header
))) {
2000 DEBUG(0, ("Failed to push regf header\n"));
2001 return WERR_GENERAL_FAILURE
;
2003 regf
->header
->chksum
= regf_hdr_checksum(push
->data
.data
);
2006 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf
->fd
,
2007 (tdr_push_fn_t
)tdr_push_regf_hdr
,
2009 DEBUG(0, ("Error writing registry file header\n"));
2010 return WERR_GENERAL_FAILURE
;
2013 if (lseek(regf
->fd
, 0x1000, SEEK_SET
) == -1) {
2014 DEBUG(0, ("Error lseeking to 0x1000 in regf file\n"));
2015 return WERR_GENERAL_FAILURE
;
2018 for (i
= 0; regf
->hbins
[i
]; i
++) {
2019 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf
->fd
,
2020 (tdr_push_fn_t
)tdr_push_hbin_block
,
2022 DEBUG(0, ("Error writing HBIN block\n"));
2023 return WERR_GENERAL_FAILURE
;
2030 WERROR
reg_create_regf_file(TALLOC_CTX
*parent_ctx
,
2031 const char *location
,
2032 int minor_version
, struct hive_key
**key
)
2034 struct regf_data
*regf
;
2035 struct regf_hdr
*regf_hdr
;
2040 struct security_descriptor
*sd
;
2043 regf
= (struct regf_data
*)talloc_zero(NULL
, struct regf_data
);
2045 W_ERROR_HAVE_NO_MEMORY(regf
);
2047 DEBUG(5, ("Attempting to create registry file\n"));
2049 /* Get the header */
2050 regf
->fd
= creat(location
, 0644);
2052 if (regf
->fd
== -1) {
2053 DEBUG(0,("Could not create file: %s, %s\n", location
,
2056 return WERR_GENERAL_FAILURE
;
2059 regf_hdr
= talloc_zero(regf
, struct regf_hdr
);
2060 W_ERROR_HAVE_NO_MEMORY(regf_hdr
);
2061 regf_hdr
->REGF_ID
= "regf";
2062 unix_to_nt_time(®f_hdr
->modtime
, time(NULL
));
2063 regf_hdr
->version
.major
= 1;
2064 regf_hdr
->version
.minor
= minor_version
;
2065 regf_hdr
->last_block
= 0x1000; /* Block size */
2066 regf_hdr
->description
= talloc_strdup(regf_hdr
,
2067 "Registry created by Samba 4");
2068 W_ERROR_HAVE_NO_MEMORY(regf_hdr
->description
);
2069 regf_hdr
->chksum
= 0;
2071 regf
->header
= regf_hdr
;
2073 /* Create all hbin blocks */
2074 regf
->hbins
= talloc_array(regf
, struct hbin_block
*, 1);
2075 W_ERROR_HAVE_NO_MEMORY(regf
->hbins
);
2076 regf
->hbins
[0] = NULL
;
2079 nk
.type
= REG_ROOT_KEY
;
2080 unix_to_nt_time(&nk
.last_change
, time(NULL
));
2082 nk
.parent_offset
= -1;
2085 nk
.subkeys_offset
= -1;
2086 nk
.unknown_offset
= -1;
2088 nk
.values_offset
= -1;
2089 memset(nk
.unk3
, 0, 5);
2090 nk
.clsname_offset
= -1;
2091 nk
.clsname_length
= 0;
2092 nk
.sk_offset
= 0x80;
2093 nk
.key_name
= "SambaRootKey";
2096 * It should be noted that changing the key_name to something shorter
2097 * creates a shorter nk block, which makes the position of the sk block
2098 * change. All Windows registries I've seen have the sk at 0x80.
2099 * I therefore recommend that our regf files share that offset -- Wilco
2102 /* Create a security descriptor. */
2103 sd
= security_descriptor_dacl_create(regf
,
2106 SID_NT_AUTHENTICATED_USERS
,
2107 SEC_ACE_TYPE_ACCESS_ALLOWED
,
2109 SEC_ACE_FLAG_OBJECT_INHERIT
,
2112 /* Push the security descriptor to a blob */
2113 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data
, regf
,
2114 sd
, (ndr_push_flags_fn_t
)ndr_push_security_descriptor
))) {
2115 DEBUG(0, ("Unable to push security descriptor\n"));
2116 return WERR_GENERAL_FAILURE
;
2121 sk
.prev_offset
= 0x80;
2122 sk
.next_offset
= 0x80;
2124 sk
.rec_size
= data
.length
;
2125 sk
.sec_desc
= data
.data
;
2127 /* Store the new nk key */
2128 regf
->header
->data_offset
= hbin_store_tdr(regf
,
2129 (tdr_push_fn_t
)tdr_push_nk_block
,
2131 /* Store the sk block */
2132 sk_offset
= hbin_store_tdr(regf
,
2133 (tdr_push_fn_t
) tdr_push_sk_block
,
2135 if (sk_offset
!= 0x80) {
2136 DEBUG(0, ("Error storing sk block, should be at 0x80, stored at 0x%x\n", nk
.sk_offset
));
2137 return WERR_GENERAL_FAILURE
;
2141 *key
= (struct hive_key
*)regf_get_key(parent_ctx
, regf
,
2142 regf
->header
->data_offset
);
2144 error
= regf_save_hbin(regf
, 1);
2145 if (!W_ERROR_IS_OK(error
)) {
2149 /* We can drop our own reference now that *key will have created one */
2150 talloc_unlink(NULL
, regf
);
2155 static WERROR
regf_flush_key(struct hive_key
*key
)
2157 struct regf_key_data
*private_data
= (struct regf_key_data
*)key
;
2158 struct regf_data
*regf
= private_data
->hive
;
2161 error
= regf_save_hbin(regf
, 1);
2162 if (!W_ERROR_IS_OK(error
)) {
2163 DEBUG(0, ("Failed to flush regf to disk\n"));
2170 static int regf_destruct(struct regf_data
*regf
)
2175 error
= regf_save_hbin(regf
, 1);
2176 if (!W_ERROR_IS_OK(error
)) {
2177 DEBUG(0, ("Failed to flush registry to disk\n"));
2181 /* Close file descriptor */
2187 WERROR
reg_open_regf_file(TALLOC_CTX
*parent_ctx
, const char *location
,
2188 struct hive_key
**key
)
2190 struct regf_data
*regf
;
2191 struct regf_hdr
*regf_hdr
;
2192 struct tdr_pull
*pull
;
2195 regf
= (struct regf_data
*)talloc_zero(parent_ctx
, struct regf_data
);
2196 W_ERROR_HAVE_NO_MEMORY(regf
);
2198 talloc_set_destructor(regf
, regf_destruct
);
2200 DEBUG(5, ("Attempting to load registry file\n"));
2202 /* Get the header */
2203 regf
->fd
= open(location
, O_RDWR
);
2205 if (regf
->fd
== -1) {
2206 DEBUG(0,("Could not load file: %s, %s\n", location
,
2209 return WERR_GENERAL_FAILURE
;
2212 pull
= tdr_pull_init(regf
);
2214 pull
->data
.data
= (uint8_t*)fd_load(regf
->fd
, &pull
->data
.length
, 0, regf
);
2216 if (pull
->data
.data
== NULL
) {
2217 DEBUG(0, ("Error reading data from file: %s\n", location
));
2219 return WERR_GENERAL_FAILURE
;
2222 regf_hdr
= talloc(regf
, struct regf_hdr
);
2223 W_ERROR_HAVE_NO_MEMORY(regf_hdr
);
2225 if (NT_STATUS_IS_ERR(tdr_pull_regf_hdr(pull
, regf_hdr
, regf_hdr
))) {
2226 DEBUG(0, ("Failed to pull regf header from file: %s\n", location
));
2228 return WERR_GENERAL_FAILURE
;
2231 regf
->header
= regf_hdr
;
2233 if (strcmp(regf_hdr
->REGF_ID
, "regf") != 0) {
2234 DEBUG(0, ("Unrecognized NT registry header id: %s, %s\n",
2235 regf_hdr
->REGF_ID
, location
));
2237 return WERR_GENERAL_FAILURE
;
2240 /* Validate the header ... */
2241 if (regf_hdr_checksum(pull
->data
.data
) != regf_hdr
->chksum
) {
2242 DEBUG(0, ("Registry file checksum error: %s: %d,%d\n",
2243 location
, regf_hdr
->chksum
,
2244 regf_hdr_checksum(pull
->data
.data
)));
2246 return WERR_GENERAL_FAILURE
;
2249 pull
->offset
= 0x1000;
2252 /* Read in all hbin blocks */
2253 regf
->hbins
= talloc_array(regf
, struct hbin_block
*, 1);
2254 W_ERROR_HAVE_NO_MEMORY(regf
->hbins
);
2256 regf
->hbins
[0] = NULL
;
2258 while (pull
->offset
< pull
->data
.length
&&
2259 pull
->offset
<= regf
->header
->last_block
) {
2260 struct hbin_block
*hbin
= talloc(regf
->hbins
,
2263 W_ERROR_HAVE_NO_MEMORY(hbin
);
2265 if (NT_STATUS_IS_ERR(tdr_pull_hbin_block(pull
, hbin
, hbin
))) {
2266 DEBUG(0, ("[%d] Error parsing HBIN block\n", i
));
2271 if (strcmp(hbin
->HBIN_ID
, "hbin") != 0) {
2272 DEBUG(0, ("[%d] Expected 'hbin', got '%s'\n",
2278 regf
->hbins
[i
] = hbin
;
2280 regf
->hbins
= talloc_realloc(regf
, regf
->hbins
,
2281 struct hbin_block
*, i
+2);
2282 regf
->hbins
[i
] = NULL
;
2287 DEBUG(1, ("%d HBIN blocks read\n", i
));
2289 *key
= (struct hive_key
*)regf_get_key(parent_ctx
, regf
,
2290 regf
->header
->data_offset
);
2292 /* We can drop our own reference now that *key will have created one */
2293 talloc_unlink(parent_ctx
, regf
);
2298 static struct hive_operations reg_backend_regf
= {
2300 .get_key_info
= regf_get_info
,
2301 .enum_key
= regf_get_subkey_by_index
,
2302 .get_key_by_name
= regf_get_subkey_by_name
,
2303 .get_value_by_name
= regf_get_value_by_name
,
2304 .enum_value
= regf_get_value
,
2305 .get_sec_desc
= regf_get_sec_desc
,
2306 .set_sec_desc
= regf_set_sec_desc
,
2307 .add_key
= regf_add_key
,
2308 .set_value
= regf_set_value
,
2309 .del_key
= regf_del_key
,
2310 .delete_value
= regf_del_value
,
2311 .flush_key
= regf_flush_key