2 Samba CIFS implementation
3 Registry backend for REGF files
4 Copyright (C) 2005 Jelmer Vernooij, jelmer@samba.org
5 Copyright (C) 2006 Wilco Baan Hofman, wilco@baanhofman.nl
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "lib/registry/registry.h"
22 #include "system/filesys.h"
23 #include "system/time.h"
24 #include "lib/registry/tdr_regf.h"
25 #include "librpc/gen_ndr/ndr_security.h"
28 * - Return error codes that make more sense
33 * Read HBIN blocks into memory
38 struct hbin_block **hbins;
39 struct regf_hdr *header;
42 static struct hbin_block *hbin_by_offset (const struct regf_data *data, uint32_t offset, uint32_t *rel_offset)
46 for (i = 0; data->hbins[i]; i++) {
47 if (offset >= data->hbins[i]->offset_from_first &&
48 offset < data->hbins[i]->offset_from_first+
49 data->hbins[i]->offset_to_next) {
51 *rel_offset = offset - data->hbins[i]->offset_from_first - 0x20;
52 return data->hbins[i];
60 * Validate a regf header
61 * For now, do nothing, but we should check the checksum
63 static uint32_t regf_hdr_checksum(const uint8_t *buffer)
65 uint32_t checksum = 0, x;
68 for (i = 0; i < 0x01FB; i+= 4) {
76 static DATA_BLOB hbin_get(const struct regf_data *data, uint32_t offset)
79 struct hbin_block *hbin;
84 hbin = hbin_by_offset(data, offset, &rel_offset);
87 DEBUG(1, ("Can't find HBIN containing 0x%04x\n", offset));
91 ret.length = IVAL(hbin->data, rel_offset);
92 if (!(ret.length & 0x80000000)) {
93 DEBUG(0, ("Trying to use dirty block at 0x%04x\n", offset));
98 ret.length = (ret.length ^ 0xffffffff) + 1;
100 ret.length -= 4; /* 4 bytes for the length... */
101 ret.data = hbin->data +
102 (offset - hbin->offset_from_first - 0x20) + 4;
107 static BOOL hbin_get_tdr (struct regf_data *regf, uint32_t offset, TALLOC_CTX *ctx, tdr_pull_fn_t pull_fn, void *p)
109 struct tdr_pull pull;
113 pull.data = hbin_get(regf, offset);
114 if (!pull.data.data) {
115 DEBUG(1, ("Unable to get data at 0x%04x\n", offset));
119 if (NT_STATUS_IS_ERR(pull_fn(&pull, ctx, p))) {
120 DEBUG(1, ("Error parsing record at 0x%04x using tdr\n", offset));
127 /* Allocate some new data */
128 static DATA_BLOB hbin_alloc (struct regf_data *data, uint32_t size, uint32_t *offset)
131 uint32_t rel_offset = -1; /* Relative offset ! */
132 struct hbin_block *hbin = NULL;
138 return data_blob(NULL, 0);
140 size += 4; /* Need to include uint32 for the length */
142 /* Allocate as a multiple of 8 */
143 size = (size + 7) & ~7;
148 for (i = 0; (hbin = data->hbins[i]); i++) {
151 for (j = 0; j < hbin->offset_to_next-0x20; j+= my_size) {
152 uint32_t header = IVAL(hbin->data, j + 4);
153 my_size = IVAL(hbin->data, j);
155 if (my_size == 0x0) {
156 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
160 if (my_size % 8 != 0) {
161 DEBUG(0, ("Encountered non-aligned block!\n"));
164 if (my_size & 0x80000000) { /* Used... */
165 my_size = (my_size ^ 0xffffffff) + 1;
166 } else if (my_size == size) { /* exact match */
168 DEBUG(4, ("Found free block of exact size %d in middle of HBIN\n", size));
170 } else if (my_size > size) { /* data will remain */
172 SIVAL(hbin->data, rel_offset+size, my_size-size);
173 DEBUG(4, ("Found free block of size %d (needing %d) in middle of HBIN\n", my_size, size));
177 if (header == 0xffffffff &&
178 hbin->offset_to_next-rel_offset >= size) {
181 DEBUG(4, ("Found free block of size %d at end of HBIN\n", size));
182 /* Mark new free block size */
183 SIVAL(hbin->data, rel_offset+size,hbin->offset_to_next - rel_offset - size - 0x20);
184 SIVAL(hbin->data, rel_offset+size+0x4, 0xffffffff);
188 if (header == 0xffffffff) {
193 if (rel_offset != -1)
197 /* No space available in previous hbins,
198 * allocate new one */
199 if (data->hbins[i] == NULL) {
200 DEBUG(4, ("No space available in other HBINs for block of size %d, allocating new HBIN\n", size));
201 data->hbins = talloc_realloc(data, data->hbins, struct hbin_block *, i+2);
202 hbin = talloc(data->hbins, struct hbin_block);
203 data->hbins[i] = hbin;
204 data->hbins[i+1] = NULL;
206 hbin->HBIN_ID = talloc_strdup(hbin, "hbin");
207 hbin->offset_from_first = (i == 0?0:data->hbins[i-1]->offset_from_first+data->hbins[i-1]->offset_to_next);
208 hbin->offset_to_next = 0x1000;
209 hbin->unknown[0] = 0;
210 hbin->unknown[0] = 0;
211 unix_to_nt_time(&hbin->last_change, time(NULL));
212 hbin->block_size = hbin->offset_to_next;
213 hbin->data = talloc_zero_array(hbin, uint8_t, hbin->block_size - 0x20);
216 SIVAL(hbin->data, size, hbin->block_size - size - 0x20);
217 SIVAL(hbin->data, size + 0x4, 0xffffffff);
220 /* Set size and mark as used */
221 SIVAL(hbin->data, rel_offset, size | 0x80000000);
223 ret.data = hbin->data + rel_offset + 0x4; /* Skip past length */
224 ret.length = size - 0x4;
226 uint32_t new_rel_offset;
227 *offset = hbin->offset_from_first + rel_offset + 0x20;
228 SMB_ASSERT(hbin_by_offset(data, *offset, &new_rel_offset) == hbin);
229 SMB_ASSERT(new_rel_offset == rel_offset);
235 /* Store a data blob. Return the offset at which it was stored */
236 static uint32_t hbin_store (struct regf_data *data, DATA_BLOB blob)
239 DATA_BLOB dest = hbin_alloc(data, blob.length, &ret);
241 memcpy(dest.data, blob.data, blob.length);
246 static uint32_t hbin_store_tdr (struct regf_data *data, tdr_push_fn_t push_fn, void *p)
248 struct tdr_push *push = talloc_zero(data, struct tdr_push);
251 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
252 DEBUG(0, ("Error during push\n"));
256 ret = hbin_store(data, push->data);
264 /* Free existing data */
265 static void hbin_free (struct regf_data *data, uint32_t offset)
269 struct hbin_block *hbin;
271 SMB_ASSERT (offset > 0);
273 hbin = hbin_by_offset(data, offset, &rel_offset);
278 /* Get original size */
279 size = IVAL(hbin->data, rel_offset);
281 if (!(size & 0x80000000)) {
282 DEBUG(1, ("Trying to free already freed block at 0x%04x\n", offset));
286 /* Mark block as free */
287 SIVAL(hbin->data, rel_offset, size &~ 0x80000000);
290 /* Store a data blob data was already stored, but hsa changed in size
291 * Will try to save it at the current location if possible, otherwise
292 * does a free + store */
293 static uint32_t hbin_store_resize (struct regf_data *data, uint32_t orig_offset, DATA_BLOB blob)
296 struct hbin_block *hbin = hbin_by_offset(data, orig_offset, &rel_offset);
299 uint32_t needed_size;
300 uint32_t possible_size;
303 SMB_ASSERT(orig_offset > 0);
306 return hbin_store(data, blob);
308 /* Get original size */
309 orig_size = IVAL(hbin->data, rel_offset);
311 needed_size = blob.length + 4; /* Add uint32 containing length */
312 needed_size = (needed_size + 7) & ~7; /* Align */
314 /* Fits into current allocated block */
315 if (orig_size >= needed_size) {
316 memcpy(hbin->data + rel_offset + 0x4, blob.data, blob.length);
320 possible_size = orig_size;
322 /* Check if it can be combined with the next few free records */
324 i < hbin->offset_to_next - 0x20;
327 if (IVAL(hbin->data, i) & 0x80000000) /* Used */
330 my_size = IVAL(hbin->data, i);
331 header = IVAL(hbin->data, i + 4);
332 if (header == 0xffffffff) {
333 possible_size = hbin->offset_to_next - 0x20 - rel_offset;
334 } else if (my_size == 0x0) {
335 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
338 possible_size += my_size;
341 if (possible_size >= blob.length) {
342 SIVAL(hbin->data, rel_offset, possible_size);
343 memcpy(hbin->data + rel_offset + 0x4, blob.data, blob.length);
347 if (header == 0xffffffff)
351 hbin_free(data, orig_offset);
352 return hbin_store(data, blob);
355 static uint32_t hbin_store_tdr_resize (struct regf_data *regf, tdr_push_fn_t push_fn, uint32_t orig_offset, void *p)
357 struct tdr_push *push = talloc_zero(regf, struct tdr_push);
360 if (NT_STATUS_IS_ERR(push_fn(push, p))) {
361 DEBUG(0, ("Error during push\n"));
365 ret = hbin_store_resize(regf, orig_offset, push->data);
372 static WERROR regf_num_subkeys (const struct registry_key *key, uint32_t *count)
374 struct nk_block *nk = key->backend_data;
376 *count = nk->num_subkeys;
381 static WERROR regf_num_values (const struct registry_key *key, uint32_t *count)
383 struct nk_block *nk = key->backend_data;
385 *count = nk->num_values;
390 static struct registry_key *regf_get_key (TALLOC_CTX *ctx, struct regf_data *regf, uint32_t offset)
392 struct registry_key *ret;
395 ret = talloc_zero(ctx, struct registry_key);
396 nk = talloc(ret, struct nk_block);
397 if (!hbin_get_tdr(regf, offset, nk, (tdr_pull_fn_t)tdr_pull_nk_block, nk)) {
398 DEBUG(0, ("Unable to find HBIN data for offset %d\n", offset));
402 if (strcmp(nk->header, "nk") != 0) {
403 DEBUG(0, ("Expected nk record, got %s\n", nk->header));
408 ret->name = talloc_steal(ret, nk->key_name);
409 ret->last_mod = nk->last_change;
411 if (nk->clsname_offset != -1) {
412 DATA_BLOB data = hbin_get(regf, nk->clsname_offset);
413 ret->class_name = talloc_strndup(ret, (char*)data.data, nk->clsname_length);
415 ret->backend_data = nk;
420 static WERROR regf_get_value (TALLOC_CTX *ctx, const struct registry_key *key, int idx, struct registry_value **ret)
422 struct nk_block *nk = key->backend_data;
424 struct regf_data *regf = key->hive->backend_data;
428 if (idx >= nk->num_values)
429 return WERR_NO_MORE_ITEMS;
431 data = hbin_get(regf, nk->values_offset);
433 DEBUG(0, ("Unable to find value list\n"));
434 return WERR_GENERAL_FAILURE;
437 if (data.length < nk->num_values * 4) {
438 DEBUG(1, ("Value counts mismatch\n"));
441 vk_offset = IVAL(data.data, idx * 4);
443 *ret = talloc_zero(ctx, struct registry_value);
447 vk = talloc(*ret, struct vk_block);
451 if (!hbin_get_tdr(regf, vk_offset, vk, (tdr_pull_fn_t)tdr_pull_vk_block, vk)) {
452 DEBUG(0, ("Unable to get VK block at %d\n", vk_offset));
453 return WERR_GENERAL_FAILURE;
456 (*ret)->name = talloc_steal(*ret, vk->data_name);
457 (*ret)->data_type = vk->data_type;
458 if (vk->data_length & 0x80000000) {
459 vk->data_length &=~0x80000000;
460 (*ret)->data.data = (uint8_t *)&vk->data_offset;
461 (*ret)->data.length = vk->data_length;
463 (*ret)->data = hbin_get(regf, vk->data_offset);
466 if ((*ret)->data.length < vk->data_length) {
467 DEBUG(1, ("Read data less than indicated data length!\n"));
473 static WERROR regf_get_subkey_by_index (TALLOC_CTX *ctx, const struct registry_key *key, int idx, struct registry_key **ret)
476 struct nk_block *nk = key->backend_data;
479 if (idx >= nk->num_subkeys)
480 return WERR_NO_MORE_ITEMS;
482 data = hbin_get(key->hive->backend_data, nk->subkeys_offset);
484 DEBUG(0, ("Unable to find subkey list\n"));
485 return WERR_GENERAL_FAILURE;
488 if (!strncmp((char *)data.data, "li", 2)) {
490 struct tdr_pull pull;
492 DEBUG(10, ("Subkeys in LI list\n"));
496 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, nk, &li))) {
497 DEBUG(0, ("Error parsing LI list\n"));
498 return WERR_GENERAL_FAILURE;
500 SMB_ASSERT(!strncmp(li.header, "li",2));
502 if (li.key_count != nk->num_subkeys) {
503 DEBUG(0, ("Subkey counts don't match\n"));
504 return WERR_GENERAL_FAILURE;
506 key_off = li.nk_offset[idx];
508 } else if (!strncmp((char *)data.data, "lf", 2)) {
510 struct tdr_pull pull;
512 DEBUG(10, ("Subkeys in LF list\n"));
516 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(&pull, nk, &lf))) {
517 DEBUG(0, ("Error parsing LF list\n"));
518 return WERR_GENERAL_FAILURE;
520 SMB_ASSERT(!strncmp(lf.header, "lf",2));
522 if (lf.key_count != nk->num_subkeys) {
523 DEBUG(0, ("Subkey counts don't match\n"));
524 return WERR_GENERAL_FAILURE;
527 key_off = lf.hr[idx].nk_offset;
528 } else if (!strncmp((char *)data.data, "lh", 2)) {
530 struct tdr_pull pull;
532 DEBUG(10, ("Subkeys in LH list"));
536 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, nk, &lh))) {
537 DEBUG(0, ("Error parsing LH list\n"));
538 return WERR_GENERAL_FAILURE;
540 SMB_ASSERT(!strncmp(lh.header, "lh",2));
542 if (lh.key_count != nk->num_subkeys) {
543 DEBUG(0, ("Subkey counts don't match\n"));
544 return WERR_GENERAL_FAILURE;
546 key_off = lh.hr[idx].nk_offset;
547 } else if (!strncmp((char *)data.data, "ri", 2)) {
549 struct tdr_pull pull;
551 uint16_t sublist_count = 0;
556 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(&pull, nk, &ri))) {
557 DEBUG(0, ("Error parsing RI list\n"));
558 return WERR_GENERAL_FAILURE;
560 SMB_ASSERT(!strncmp(ri.header, "ri",2));
562 for (i = 0; i < ri.key_count; i++) {
565 /* Get sublist data blob */
566 list_data = hbin_get(key->hive->backend_data, ri.offset[i]);
567 if (!list_data.data) {
568 DEBUG(0, ("Error getting RI list."));
569 return WERR_GENERAL_FAILURE;
573 pull.data = list_data;
575 if (!strncmp((char *)list_data.data, "li", 2)) {
578 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, nk, &li))) {
579 DEBUG(0, ("Error parsing LI list from RI\n"));
580 return WERR_GENERAL_FAILURE;
582 SMB_ASSERT(!strncmp(li.header, "li",2));
584 /* Advance to next sublist if necessary */
585 if (idx >= sublist_count + li.key_count) {
586 sublist_count += li.key_count;
589 key_off = li.nk_offset[idx - sublist_count];
590 sublist_count += li.key_count;
592 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
595 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, nk, &lh))) {
596 DEBUG(0, ("Error parsing LH list from RI\n"));
597 return WERR_GENERAL_FAILURE;
599 SMB_ASSERT(!strncmp(lh.header, "lh",2));
602 /* Advance to next sublist if necessary */
603 if (idx >= sublist_count + lh.key_count) {
604 sublist_count += lh.key_count;
607 key_off = lh.hr[idx - sublist_count].nk_offset;
608 sublist_count += lh.key_count;
611 DEBUG(0,("Unknown sublist in ri block\n"));
617 if (idx > sublist_count) {
618 return WERR_NO_MORE_ITEMS;
622 DEBUG(0, ("Unknown type for subkey list (0x%04x): %c%c\n", nk->subkeys_offset, data.data[0], data.data[1]));
623 return WERR_GENERAL_FAILURE;
626 *ret = regf_get_key (ctx, key->hive->backend_data, key_off);
631 static WERROR regf_match_subkey_by_name (TALLOC_CTX *ctx, const struct registry_key *key, uint32_t offset, const char *name, uint32_t *ret)
633 DATA_BLOB subkey_data;
634 struct nk_block subkey;
635 struct tdr_pull pull;
637 subkey_data = hbin_get(key->hive->backend_data, offset);
638 if (!subkey_data.data) {
639 DEBUG(0, ("Unable to retrieve subkey HBIN\n"));
640 return WERR_GENERAL_FAILURE;
644 pull.data = subkey_data;
646 if (NT_STATUS_IS_ERR(tdr_pull_nk_block(&pull, ctx, &subkey))) {
647 DEBUG(0, ("Error parsing NK structure.\n"));
648 return WERR_GENERAL_FAILURE;
650 if (strncmp(subkey.header, "nk", 2)) {
651 DEBUG(0, ("Not an NK structure.\n"));
652 return WERR_GENERAL_FAILURE;
654 if (!strcasecmp(subkey.key_name, name)) {
662 static WERROR regf_get_subkey_by_name (TALLOC_CTX *ctx, const struct registry_key *key, const char *name, struct registry_key **ret)
665 struct nk_block *nk = key->backend_data;
666 uint32_t key_off = 0;
668 data = hbin_get(key->hive->backend_data, nk->subkeys_offset);
670 DEBUG(0, ("Unable to find subkey list\n"));
671 return WERR_GENERAL_FAILURE;
674 if (!strncmp((char *)data.data, "li",2)) {
676 struct tdr_pull pull;
679 DEBUG(10, ("Subkeys in LI list\n"));
683 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, nk, &li))) {
684 DEBUG(0, ("Error parsing LI list\n"));
685 return WERR_GENERAL_FAILURE;
687 SMB_ASSERT(!strncmp(li.header, "li",2));
689 if (li.key_count != nk->num_subkeys) {
690 DEBUG(0, ("Subkey counts don't match\n"));
691 return WERR_GENERAL_FAILURE;
694 for (i = 0; i < li.key_count; i++) {
695 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key, li.nk_offset[i], name, &key_off));
701 return WERR_DEST_NOT_FOUND;
703 } else if (!strncmp((char *)data.data, "lf",2)) {
705 struct tdr_pull pull;
708 DEBUG(10, ("Subkeys in LF list\n"));
712 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(&pull, nk, &lf))) {
713 DEBUG(0, ("Error parsing LF list\n"));
714 return WERR_GENERAL_FAILURE;
716 SMB_ASSERT(!strncmp(lf.header, "lf",2));
718 if (lf.key_count != nk->num_subkeys) {
719 DEBUG(0, ("Subkey counts don't match\n"));
720 return WERR_GENERAL_FAILURE;
723 for (i = 0; i < lf.key_count; i++) {
724 if (strncmp(lf.hr[i].hash, name, 4)) {
727 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key, lf.hr[i].nk_offset, name, &key_off));
733 return WERR_DEST_NOT_FOUND;
735 } else if (!strncmp((char *)data.data, "lh",2)) {
737 struct tdr_pull pull;
742 DEBUG(10, ("Subkeys in LH list\n"));
746 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, nk, &lh))) {
747 DEBUG(0, ("Error parsing LH list\n"));
748 return WERR_GENERAL_FAILURE;
750 SMB_ASSERT(!strncmp(lh.header, "lh",2));
752 if (lh.key_count != nk->num_subkeys) {
753 DEBUG(0, ("Subkey counts don't match\n"));
754 return WERR_GENERAL_FAILURE;
757 /* Compute hash for the name */
758 hash_name = strupper_talloc(nk, name);
759 for (i = 0; *(hash_name + i) != 0; i++) {
761 hash += *(hash_name + i);
763 for (i = 0; i < lh.key_count; i++) {
764 if (lh.hr[i].base37 != hash) {
767 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key, lh.hr[i].nk_offset, name, &key_off));
773 return WERR_DEST_NOT_FOUND;
775 } else if (!strncmp((char *)data.data, "ri", 2)) {
777 struct tdr_pull pull;
780 DEBUG(10, ("Subkeys in RI list\n"));
784 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(&pull, nk, &ri))) {
785 DEBUG(0, ("Error parsing RI list\n"));
786 return WERR_GENERAL_FAILURE;
788 SMB_ASSERT(!strncmp(ri.header, "ri",2));
791 for (i = 0; i < ri.key_count; i++) {
794 /* Get sublist data blob */
795 list_data = hbin_get(key->hive->backend_data, ri.offset[i]);
796 if (!list_data.data) {
797 DEBUG(0, ("Error getting RI list."));
798 return WERR_GENERAL_FAILURE;
802 pull.data = list_data;
804 if (!strncmp((char *)list_data.data, "li", 2)) {
807 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, nk, &li))) {
808 DEBUG(0, ("Error parsing LI list from RI\n"));
809 return WERR_GENERAL_FAILURE;
811 SMB_ASSERT(!strncmp(li.header, "li",2));
813 for (j = 0; j < li.key_count; j++) {
814 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
815 li.nk_offset[j], name, &key_off));
820 } else if (!strncmp((char *)list_data.data, "lh", 2)) {
825 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, nk, &lh))) {
826 DEBUG(0, ("Error parsing LH list from RI\n"));
827 return WERR_GENERAL_FAILURE;
829 SMB_ASSERT(!strncmp(lh.header, "lh",2));
831 /* Compute hash for the name */
832 hash_name = strupper_talloc(nk, name);
833 for (j = 0; *(hash_name + j) != 0; j++) {
835 hash += *(hash_name + j);
837 for (j = 0; j < lh.key_count; j++) {
838 if (lh.hr[j].base37 != hash) {
841 W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
842 lh.hr[j].nk_offset, name, &key_off));
854 return WERR_DEST_NOT_FOUND;
857 DEBUG(0, ("Unknown subkey list type.\n"));
858 return WERR_GENERAL_FAILURE;
861 *ret = regf_get_key (ctx, key->hive->backend_data, key_off);
865 static WERROR regf_set_sec_desc (const struct registry_key *key, const struct security_descriptor *sec_desc)
868 return WERR_NOT_SUPPORTED;
871 static WERROR regf_get_sec_desc(TALLOC_CTX *ctx, const struct registry_key *key, struct security_descriptor **sd)
873 struct nk_block *nk = key->backend_data;
875 struct regf_data *regf = key->hive->backend_data;
878 if (!hbin_get_tdr(regf, nk->sk_offset, ctx, (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
879 DEBUG(0, ("Unable to find security descriptor\n"));
880 return WERR_GENERAL_FAILURE;
883 if (strcmp(sk.header, "sk") != 0) {
884 DEBUG(0, ("Expected 'sk', got '%s'\n", sk.header));
885 return WERR_GENERAL_FAILURE;
888 *sd = talloc(ctx, struct security_descriptor);
892 data.data = sk.sec_desc;
893 data.length = sk.rec_size;
894 if (NT_STATUS_IS_ERR(ndr_pull_struct_blob(&data, ctx, *sd, (ndr_pull_flags_fn_t)ndr_pull_security_descriptor))) {
895 DEBUG(0, ("Error parsing security descriptor\n"));
896 return WERR_GENERAL_FAILURE;
902 static uint32_t lf_add_entry (struct regf_data *regf, uint32_t list_offset, const char *name, uint32_t key_offset)
909 /* Add to subkeys list */
910 if (list_offset == -1) { /* Need to create subkeys list */
913 if (!hbin_get_tdr(regf, list_offset, regf, (tdr_pull_fn_t)tdr_pull_lf_block, &lf)) {
914 DEBUG(0, ("Can't get subkeys list\n"));
919 lf.hr = talloc_realloc(regf, lf.hr, struct hash_record, lf.key_count+1);
920 lf.hr[lf.key_count].nk_offset = key_offset;
921 lf.hr[lf.key_count].hash = talloc_strndup(regf, name, 4);
924 ret = hbin_store_tdr_resize(regf, (tdr_push_fn_t)tdr_push_lf_block, list_offset, &lf);
931 static WERROR regf_del_value (const struct registry_key *parent, const char *name)
934 return WERR_NOT_SUPPORTED;
938 static WERROR regf_del_key (const struct registry_key *parent, const char *name)
940 struct nk_block *nk = parent->backend_data;
944 if (nk->subkeys_offset == -1)
949 return WERR_NOT_SUPPORTED;
952 static WERROR regf_add_key (TALLOC_CTX *ctx, const struct registry_key *parent, const char *name, uint32_t access_mask, struct security_descriptor *sec_desc, struct registry_key **ret)
954 struct nk_block *parent_nk = parent->backend_data, nk;
955 struct regf_data *regf = parent->hive->backend_data;
959 nk.type = REG_SUB_KEY;
960 unix_to_nt_time(&nk.last_change, time(NULL));
962 nk.parent_offset = 0; /* FIXME */
965 nk.subkeys_offset = -1;
966 nk.unknown_offset = -1;
969 memset(nk.unk3, 0, 5);
970 nk.clsname_offset = -1;
971 nk.clsname_length = 0;
974 offset = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_nk_block, &nk);
976 parent_nk->subkeys_offset = lf_add_entry(regf, parent_nk->subkeys_offset, name, nk.parent_offset);
978 parent_nk->num_subkeys++;
980 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block, nk.parent_offset, parent_nk);
982 *ret = regf_get_key(ctx, regf, offset);
984 /* FIXME: Set sec desc ! */
988 static WERROR regf_set_value (const struct registry_key *key, const char *name, uint32_t type, const DATA_BLOB data)
992 return WERR_NOT_SUPPORTED;
997 static WERROR regf_save_hbin(struct registry_hive *hive, struct hbin_block *hbin)
999 struct regf_data *regf = hive->backend_data;
1001 /* go to right offset */
1002 if (lseek(regf->fd, SEEK_SET, regf->header->data_offset + hbin->offset_from_first) == -1) {
1003 DEBUG(0, ("Error lseeking in regf file\n"));
1004 return WERR_GENERAL_FAILURE;
1007 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd, (tdr_push_fn_t)tdr_push_hbin_block, hbin))) {
1008 DEBUG(0, ("Error writing HBIN block\n"));
1009 return WERR_GENERAL_FAILURE;
1017 static WERROR nt_open_hive (struct registry_hive *h, struct registry_key **key)
1019 struct regf_data *regf;
1020 struct regf_hdr *regf_hdr;
1021 struct tdr_pull pull;
1024 regf = (struct regf_data *)talloc_zero(h, struct regf_data);
1025 h->backend_data = regf;
1027 DEBUG(5, ("Attempting to load registry file\n"));
1029 /* Get the header */
1030 regf->fd = open(h->location, O_RDWR);
1032 if (regf->fd == -1) {
1033 DEBUG(0,("Could not load file: %s, %s\n", h->location,
1035 return WERR_GENERAL_FAILURE;
1039 pull.data.data = (uint8_t*)fd_load(regf->fd, &pull.data.length, regf);
1041 if (pull.data.data == NULL) {
1042 DEBUG(0, ("Error reading data\n"));
1043 return WERR_GENERAL_FAILURE;
1046 regf_hdr = talloc(regf, struct regf_hdr);
1047 if (NT_STATUS_IS_ERR(tdr_pull_regf_hdr(&pull, regf_hdr, regf_hdr))) {
1048 return WERR_GENERAL_FAILURE;
1051 regf->header = regf_hdr;
1053 if (strcmp(regf_hdr->REGF_ID, "regf") != 0) {
1054 DEBUG(0, ("Unrecognized NT registry header id: %s, %s\n",
1055 regf_hdr->REGF_ID, h->location));
1058 DEBUG(1, ("Registry '%s' read. Version %d.%d.%d.%d\n",
1059 regf_hdr->description, regf_hdr->version.major,
1060 regf_hdr->version.minor, regf_hdr->version.release,
1061 regf_hdr->version.build));
1064 * Validate the header ...
1066 if (regf_hdr_checksum(pull.data.data) != regf_hdr->chksum) {
1067 DEBUG(0, ("Registry file checksum error: %s: %d,%d\n",
1068 h->location, regf_hdr->chksum, regf_hdr_checksum(pull.data.data)));
1069 return WERR_GENERAL_FAILURE;
1072 pull.offset = 0x1000;
1075 /* Read in all hbin blocks */
1076 regf->hbins = talloc_array(regf, struct hbin_block *, 1);
1077 regf->hbins[0] = NULL;
1079 while (pull.offset < pull.data.length && pull.offset < regf->header->last_block) {
1080 struct hbin_block *hbin = talloc(regf->hbins, struct hbin_block);
1082 if (NT_STATUS_IS_ERR(tdr_pull_hbin_block(&pull, hbin, hbin))) {
1083 DEBUG(0, ("[%d] Error parsing HBIN block\n", i));
1087 if (strcmp(hbin->HBIN_ID, "hbin") != 0) {
1088 DEBUG(0, ("[%d] Expected 'hbin', got '%s'\n", i, hbin->HBIN_ID));
1092 regf->hbins[i] = hbin;
1094 regf->hbins = talloc_realloc(regf, regf->hbins, struct hbin_block *, i+2);
1095 regf->hbins[i] = NULL;
1098 DEBUG(1, ("%d HBIN blocks read\n", i));
1100 *key = regf_get_key(h, regf, 0x20);
1105 static struct hive_operations reg_backend_nt4 = {
1107 .open_hive = nt_open_hive,
1108 .num_subkeys = regf_num_subkeys,
1109 .num_values = regf_num_values,
1110 .get_subkey_by_index = regf_get_subkey_by_index,
1111 .get_subkey_by_name = regf_get_subkey_by_name,
1112 .get_value_by_index = regf_get_value,
1113 .key_get_sec_desc = regf_get_sec_desc,
1114 .key_set_sec_desc = regf_set_sec_desc,
1115 .add_key = regf_add_key,
1116 .set_value = regf_set_value,
1117 .del_key = regf_del_key,
1118 .del_value = regf_del_value,
1121 NTSTATUS registry_nt4_init(void)
1123 return registry_register(®_backend_nt4);