r25920: ndr: change NTSTAUS into enum ndr_err_code (samba4 callers)
[jelmer/samba4-debian.git] / source / lib / registry / regf.c
1 /*
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
6
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.
11
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.
16
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/>.  */
19
20 #include "includes.h"
21 #include "lib/registry/hive.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"
26 #include "librpc/gen_ndr/winreg.h"
27
28 static struct hive_operations reg_backend_regf;
29
30 /**
31  * There are several places on the web where the REGF format is explained;
32  *
33  * TODO: Links
34  */
35
36 /* TODO:
37  *  - Return error codes that make more sense
38  *  - Locking
39  *  - do more things in-memory
40  */
41
42 /*
43  * Read HBIN blocks into memory
44  */
45
46 struct regf_data {
47         int fd;
48         struct hbin_block **hbins;
49         struct regf_hdr *header;
50 };
51
52 static WERROR regf_save_hbin(struct regf_data *data);
53
54 struct regf_key_data {
55         struct hive_key key;
56         struct regf_data *hive;
57         uint32_t offset;
58         struct nk_block *nk;
59 };
60
61 static struct hbin_block *hbin_by_offset(const struct regf_data *data,
62                                          uint32_t offset, uint32_t *rel_offset)
63 {
64         int i;
65
66         for (i = 0; data->hbins[i]; i++) {
67                 if (offset >= data->hbins[i]->offset_from_first &&
68                         offset < data->hbins[i]->offset_from_first+
69                                          data->hbins[i]->offset_to_next) {
70                         if (rel_offset != NULL)
71                                 *rel_offset = offset - data->hbins[i]->offset_from_first - 0x20;
72                         return data->hbins[i];
73                 }
74         }
75
76         return NULL;
77 }
78
79 /**
80  * Validate a regf header
81  * For now, do nothing, but we should check the checksum
82  */
83 static uint32_t regf_hdr_checksum(const uint8_t *buffer)
84 {
85         uint32_t checksum = 0, x;
86         int i;
87
88         for (i = 0; i < 0x01FB; i+= 4) {
89                 x = IVAL(buffer, i);
90                 checksum ^= x;
91         }
92
93         return checksum;
94 }
95
96 /**
97  * Obtain the contents of a HBIN block
98  */
99 static DATA_BLOB hbin_get(const struct regf_data *data, uint32_t offset)
100 {
101         DATA_BLOB ret;
102         struct hbin_block *hbin;
103         uint32_t rel_offset;
104
105         ret.data = NULL;
106         ret.length = 0;
107
108         hbin = hbin_by_offset(data, offset, &rel_offset);
109
110         if (hbin == NULL) {
111                 DEBUG(1, ("Can't find HBIN containing 0x%04x\n", offset));
112                 return ret;
113         }
114
115         ret.length = IVAL(hbin->data, rel_offset);
116         if (!(ret.length & 0x80000000)) {
117                 DEBUG(0, ("Trying to use dirty block at 0x%04x\n", offset));
118                 return ret;
119         }
120
121         /* remove high bit */
122         ret.length = (ret.length ^ 0xffffffff) + 1;
123
124         ret.length -= 4; /* 4 bytes for the length... */
125         ret.data = hbin->data +
126                 (offset - hbin->offset_from_first - 0x20) + 4;
127
128         return ret;
129 }
130
131 static bool hbin_get_tdr(struct regf_data *regf, uint32_t offset,
132                          TALLOC_CTX *ctx, tdr_pull_fn_t pull_fn, void *p)
133 {
134         struct tdr_pull pull;
135
136         ZERO_STRUCT(pull);
137
138         pull.data = hbin_get(regf, offset);
139         if (!pull.data.data) {
140                 DEBUG(1, ("Unable to get data at 0x%04x\n", offset));
141                 return false;
142         }
143
144         if (NT_STATUS_IS_ERR(pull_fn(&pull, ctx, p))) {
145                 DEBUG(1, ("Error parsing record at 0x%04x using tdr\n",
146                         offset));
147                 return false;
148         }
149
150         return true;
151 }
152
153 /* Allocate some new data */
154 static DATA_BLOB hbin_alloc(struct regf_data *data, uint32_t size,
155                             uint32_t *offset)
156 {
157         DATA_BLOB ret;
158         uint32_t rel_offset = -1; /* Relative offset ! */
159         struct hbin_block *hbin = NULL;
160         int i;
161
162         *offset = 0;
163
164         if (size == 0)
165                 return data_blob(NULL, 0);
166
167         size += 4; /* Need to include int32 for the length */
168
169         /* Allocate as a multiple of 8 */
170         size = (size + 7) & ~7;
171
172         ret.data = NULL;
173         ret.length = 0;
174
175         for (i = 0; (hbin = data->hbins[i]); i++) {
176                 int j;
177                 int32_t my_size;
178                 for (j = 0; j < hbin->offset_to_next-0x20; j+= my_size) {
179                         my_size = IVALS(hbin->data, j);
180
181                         if (my_size == 0x0) {
182                                 DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
183                                 return ret;
184                         }
185
186                         if (my_size % 8 != 0) {
187                                 DEBUG(0, ("Encountered non-aligned block!\n"));
188                         }
189
190                         if (my_size < 0) { /* Used... */
191                                 my_size = -my_size;
192                         } else if (my_size == size) { /* exact match */
193                                 rel_offset = j;
194                                 DEBUG(4, ("Found free block of exact size %d in middle of HBIN\n",
195                                         size));
196                                 break;
197                         } else if (my_size > size) { /* data will remain */
198                                 rel_offset = j;
199                                 /* Split this block and mark the next block as free */
200                                 SIVAL(hbin->data, rel_offset+size, my_size-size);
201                                 DEBUG(4, ("Found free block of size %d (needing %d) in middle of HBIN\n",
202                                         my_size, size));
203                                 break;
204                         }
205                 }
206
207                 if (rel_offset != -1)
208                         break;
209         }
210
211         /* No space available in previous hbins,
212          * allocate new one */
213         if (data->hbins[i] == NULL) {
214                 DEBUG(4, ("No space available in other HBINs for block of size %d, allocating new HBIN\n",
215                         size));
216                 data->hbins = talloc_realloc(data, data->hbins,
217                                              struct hbin_block *, i+2);
218                 hbin = talloc(data->hbins, struct hbin_block);
219                 SMB_ASSERT(hbin != NULL);
220
221                 data->hbins[i] = hbin;
222                 data->hbins[i+1] = NULL;
223
224                 hbin->HBIN_ID = talloc_strdup(hbin, "hbin");
225                 hbin->offset_from_first = (i == 0?0:data->hbins[i-1]->offset_from_first+data->hbins[i-1]->offset_to_next);
226                 hbin->offset_to_next = 0x1000;
227                 hbin->unknown[0] = 0;
228                 hbin->unknown[0] = 0;
229                 unix_to_nt_time(&hbin->last_change, time(NULL));
230                 hbin->block_size = hbin->offset_to_next;
231                 hbin->data = talloc_zero_array(hbin, uint8_t, hbin->block_size - 0x20);
232
233                 rel_offset = 0x0;
234                 SIVAL(hbin->data, size, hbin->block_size - size - 0x20);
235         }
236
237         /* Set size and mark as used */
238         SIVAL(hbin->data, rel_offset, -size);
239
240         ret.data = hbin->data + rel_offset + 0x4; /* Skip past length */
241         ret.length = size - 0x4;
242         if (offset) {
243                 uint32_t new_rel_offset;
244                 *offset = hbin->offset_from_first + rel_offset + 0x20;
245                 SMB_ASSERT(hbin_by_offset(data, *offset, &new_rel_offset) == hbin);
246                 SMB_ASSERT(new_rel_offset == rel_offset);
247         }
248
249         return ret;
250 }
251
252 /* Store a data blob. Return the offset at which it was stored */
253 static uint32_t hbin_store (struct regf_data *data, DATA_BLOB blob)
254 {
255         uint32_t ret;
256         DATA_BLOB dest = hbin_alloc(data, blob.length, &ret);
257
258         memcpy(dest.data, blob.data, blob.length);
259
260         return ret;
261 }
262
263 static uint32_t hbin_store_tdr(struct regf_data *data,
264                                tdr_push_fn_t push_fn, void *p)
265 {
266         struct tdr_push *push = talloc_zero(data, struct tdr_push);
267         uint32_t ret;
268
269         if (NT_STATUS_IS_ERR(push_fn(push, p))) {
270                 DEBUG(0, ("Error during push\n"));
271                 return -1;
272         }
273
274         ret = hbin_store(data, push->data);
275
276         talloc_free(push);
277
278         return ret;
279 }
280
281
282 /* Free existing data */
283 static void hbin_free (struct regf_data *data, uint32_t offset)
284 {
285         int32_t size;
286         uint32_t rel_offset;
287         int32_t next_size;
288         struct hbin_block *hbin;
289
290         SMB_ASSERT (offset > 0);
291
292         hbin = hbin_by_offset(data, offset, &rel_offset);
293
294         if (hbin == NULL)
295                 return;
296
297         /* Get original size */
298         size = IVALS(hbin->data, rel_offset);
299
300         if (size > 0) {
301                 DEBUG(1, ("Trying to free already freed block at 0x%04x\n",
302                         offset));
303                 return;
304         }
305         /* Mark as unused */
306         size = -size;
307
308         /* If the next block is free, merge into big free block */
309         if (rel_offset + size < hbin->offset_to_next) {
310                 next_size = IVALS(hbin->data, rel_offset+size);
311                 if (next_size > 0) {
312                         size += next_size;
313                 }
314         }
315
316         /* Write block size */
317         SIVALS(hbin->data, rel_offset, size);
318 }
319
320 /**
321  * Store a data blob data was already stored, but has changed in size
322  * Will try to save it at the current location if possible, otherwise
323  * does a free + store */
324 static uint32_t hbin_store_resize(struct regf_data *data,
325                                   uint32_t orig_offset, DATA_BLOB blob)
326 {
327         uint32_t rel_offset;
328         struct hbin_block *hbin = hbin_by_offset(data, orig_offset,
329                                                  &rel_offset);
330         int32_t my_size;
331         int32_t orig_size;
332         int32_t needed_size;
333         int32_t possible_size;
334         int i;
335
336         SMB_ASSERT(orig_offset > 0);
337
338         if (!hbin)
339                 return hbin_store(data, blob);
340
341         /* Get original size */
342         orig_size = -IVALS(hbin->data, rel_offset);
343
344         needed_size = blob.length + 4; /* Add int32 containing length */
345         needed_size = (needed_size + 7) & ~7; /* Align */
346
347         /* Fits into current allocated block */
348         if (orig_size >= needed_size) {
349                 memcpy(hbin->data + rel_offset + 0x4, blob.data, blob.length);
350                 /* If the difference in size is greater than 0x4, split the block
351                  * and free/merge it */
352                 if (orig_size - needed_size > 0x4) {
353                         SIVALS(hbin->data, rel_offset, -needed_size);
354                         SIVALS(hbin->data, rel_offset + needed_size,
355                                needed_size-orig_size);
356                         hbin_free(data, orig_offset + needed_size);
357                 }
358                 return orig_offset;
359         }
360
361         possible_size = orig_size;
362
363         /* Check if it can be combined with the next few free records */
364         for (i = rel_offset; i < hbin->offset_to_next - 0x20; i += my_size) {
365                 if (IVALS(hbin->data, i) < 0) /* Used */
366                         break;
367
368                 my_size = IVALS(hbin->data, i);
369
370                 if (my_size == 0x0) {
371                         DEBUG(0, ("Invalid zero-length block! File is corrupt.\n"));
372                         break;
373                 } else {
374                         possible_size += my_size;
375                 }
376
377                 if (possible_size >= blob.length) {
378                         SIVAL(hbin->data, rel_offset, -possible_size);
379                         memcpy(hbin->data + rel_offset + 0x4,
380                                blob.data, blob.length);
381                         return orig_offset;
382                 }
383         }
384
385         hbin_free(data, orig_offset);
386         return hbin_store(data, blob);
387 }
388
389 static uint32_t hbin_store_tdr_resize(struct regf_data *regf,
390                                       tdr_push_fn_t push_fn,
391                                       uint32_t orig_offset, void *p)
392 {
393         struct tdr_push *push = talloc_zero(regf, struct tdr_push);
394         uint32_t ret;
395
396         if (NT_STATUS_IS_ERR(push_fn(push, p))) {
397                 DEBUG(0, ("Error during push\n"));
398                 return -1;
399         }
400
401         ret = hbin_store_resize(regf, orig_offset, push->data);
402
403         talloc_free(push);
404
405         return ret;
406 }
407
408 static uint32_t regf_create_lh_hash(const char *name)
409 {
410         char *hash_name;
411         uint32_t ret = 0;
412         uint16_t i;
413
414         hash_name = strupper_talloc(NULL, name);
415         for (i = 0; *(hash_name + i) != 0; i++) {
416                 ret *= 37;
417                 ret += *(hash_name + i);
418         }
419         talloc_free(hash_name);
420         return ret;
421 }
422
423 static WERROR regf_get_info(TALLOC_CTX *mem_ctx,
424                             const struct hive_key *key,
425                             const char **classname,
426                             uint32_t *num_subkeys,
427                             uint32_t *num_values,
428                             NTTIME *last_mod_time)
429 {
430         const struct regf_key_data *private_data =
431                 (const struct regf_key_data *)key;
432
433         if (num_subkeys != NULL)
434                 *num_subkeys = private_data->nk->num_subkeys;
435
436         if (num_values != NULL)
437                 *num_values = private_data->nk->num_values;
438
439         if (classname != NULL) {
440                 if (private_data->nk->clsname_offset != -1) {
441                         DATA_BLOB data = hbin_get(private_data->hive,
442                                                   private_data->nk->clsname_offset);
443                         *classname = talloc_strndup(mem_ctx,
444                                                     (char*)data.data,
445                                                     private_data->nk->clsname_length);
446                 } else
447                         *classname = NULL;
448         }
449
450         /* TODO: Last mod time */
451
452         return WERR_OK;
453 }
454
455 static struct regf_key_data *regf_get_key(TALLOC_CTX *ctx,
456                                           struct regf_data *regf,
457                                           uint32_t offset)
458 {
459         struct nk_block *nk;
460         struct regf_key_data *ret;
461
462         ret = talloc_zero(ctx, struct regf_key_data);
463         ret->key.ops = &reg_backend_regf;
464         ret->hive = talloc_reference(ret, regf);
465         ret->offset = offset;
466         nk = talloc(ret, struct nk_block);
467         if (nk == NULL)
468                 return NULL;
469
470         ret->nk = nk;
471
472         if (!hbin_get_tdr(regf, offset, nk,
473                           (tdr_pull_fn_t)tdr_pull_nk_block, nk)) {
474                 DEBUG(0, ("Unable to find HBIN data for offset %d\n", offset));
475                 return NULL;
476         }
477
478         if (strcmp(nk->header, "nk") != 0) {
479                 DEBUG(0, ("Expected nk record, got %s\n", nk->header));
480                 talloc_free(ret);
481                 return NULL;
482         }
483
484         return ret;
485 }
486
487
488 static WERROR regf_get_value(TALLOC_CTX *ctx, const struct hive_key *key,
489                              int idx, const char **name,
490                              uint32_t *data_type, DATA_BLOB *data)
491 {
492         const struct regf_key_data *private_data =
493                         (const struct regf_key_data *)key;
494         struct vk_block *vk;
495         struct regf_data *regf = private_data->hive;
496         uint32_t vk_offset;
497         DATA_BLOB tmp;
498
499         if (idx >= private_data->nk->num_values)
500                 return WERR_NO_MORE_ITEMS;
501
502         tmp = hbin_get(regf, private_data->nk->values_offset);
503         if (!tmp.data) {
504                 DEBUG(0, ("Unable to find value list\n"));
505                 return WERR_GENERAL_FAILURE;
506         }
507
508         if (tmp.length < private_data->nk->num_values * 4) {
509                 DEBUG(1, ("Value counts mismatch\n"));
510         }
511
512         vk_offset = IVAL(tmp.data, idx * 4);
513
514         vk = talloc(NULL, struct vk_block);
515         W_ERROR_HAVE_NO_MEMORY(vk);
516
517         if (!hbin_get_tdr(regf, vk_offset, vk,
518                           (tdr_pull_fn_t)tdr_pull_vk_block, vk)) {
519                 DEBUG(0, ("Unable to get VK block at %d\n", vk_offset));
520                 talloc_free(vk);
521                 return WERR_GENERAL_FAILURE;
522         }
523
524         /* FIXME: name character set ?*/
525         if (name != NULL)
526                 *name = talloc_strndup(ctx, vk->data_name, vk->name_length);
527
528         if (data_type != NULL)
529                 *data_type = vk->data_type;
530
531         if (vk->data_length & 0x80000000) {
532                 vk->data_length &=~0x80000000;
533                 data->data = (uint8_t *)&vk->data_offset;
534                 data->length = vk->data_length;
535         } else {
536                 *data = hbin_get(regf, vk->data_offset);
537         }
538
539         if (data->length < vk->data_length) {
540                 DEBUG(1, ("Read data less than indicated data length!\n"));
541         }
542
543         talloc_free(vk);
544
545         return WERR_OK;
546 }
547
548 static WERROR regf_get_value_by_name(TALLOC_CTX *mem_ctx,
549                                      struct hive_key *key, const char *name,
550                                      uint32_t *type, DATA_BLOB *data)
551 {
552         int i;
553         const char *vname;
554         WERROR error;
555
556         /* FIXME: Do binary search? Is this list sorted at all? */
557
558         for (i = 0; W_ERROR_IS_OK(error = regf_get_value(mem_ctx, key, i,
559                                                          &vname, type, data));
560                                                          i++) {
561                 if (!strcmp(vname, name))
562                         return WERR_OK;
563         }
564
565         if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
566                 return WERR_NOT_FOUND;
567
568         return error;
569 }
570
571
572 static WERROR regf_get_subkey_by_index(TALLOC_CTX *ctx,
573                                        const struct hive_key *key,
574                                        uint32_t idx, const char **name,
575                                        const char **classname,
576                                        NTTIME *last_mod_time)
577 {
578         DATA_BLOB data;
579         struct regf_key_data *ret;
580         const struct regf_key_data *private_data = (const struct regf_key_data *)key;
581         struct nk_block *nk = private_data->nk;
582         uint32_t key_off=0;
583
584         if (idx >= nk->num_subkeys)
585                 return WERR_NO_MORE_ITEMS;
586
587         data = hbin_get(private_data->hive, nk->subkeys_offset);
588         if (!data.data) {
589                 DEBUG(0, ("Unable to find subkey list\n"));
590                 return WERR_GENERAL_FAILURE;
591         }
592
593         if (!strncmp((char *)data.data, "li", 2)) {
594                 struct li_block li;
595                 struct tdr_pull pull;
596
597                 DEBUG(10, ("Subkeys in LI list\n"));
598                 ZERO_STRUCT(pull);
599                 pull.data = data;
600
601                 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, nk, &li))) {
602                         DEBUG(0, ("Error parsing LI list\n"));
603                         return WERR_GENERAL_FAILURE;
604                 }
605                 SMB_ASSERT(!strncmp(li.header, "li", 2));
606
607                 if (li.key_count != nk->num_subkeys) {
608                         DEBUG(0, ("Subkey counts don't match\n"));
609                         return WERR_GENERAL_FAILURE;
610                 }
611                 key_off = li.nk_offset[idx];
612
613         } else if (!strncmp((char *)data.data, "lf", 2)) {
614                 struct lf_block lf;
615                 struct tdr_pull pull;
616
617                 DEBUG(10, ("Subkeys in LF list\n"));
618                 ZERO_STRUCT(pull);
619                 pull.data = data;
620
621                 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(&pull, nk, &lf))) {
622                         DEBUG(0, ("Error parsing LF list\n"));
623                         return WERR_GENERAL_FAILURE;
624                 }
625                 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
626
627                 if (lf.key_count != nk->num_subkeys) {
628                         DEBUG(0, ("Subkey counts don't match\n"));
629                         return WERR_GENERAL_FAILURE;
630                 }
631
632                 key_off = lf.hr[idx].nk_offset;
633         } else if (!strncmp((char *)data.data, "lh", 2)) {
634                 struct lh_block lh;
635                 struct tdr_pull pull;
636
637                 DEBUG(10, ("Subkeys in LH list\n"));
638                 ZERO_STRUCT(pull);
639                 pull.data = data;
640
641                 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, nk, &lh))) {
642                         DEBUG(0, ("Error parsing LH list\n"));
643                         return WERR_GENERAL_FAILURE;
644                 }
645                 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
646
647                 if (lh.key_count != nk->num_subkeys) {
648                         DEBUG(0, ("Subkey counts don't match\n"));
649                         return WERR_GENERAL_FAILURE;
650                 }
651                 key_off = lh.hr[idx].nk_offset;
652         } else if (!strncmp((char *)data.data, "ri", 2)) {
653                 struct ri_block ri;
654                 struct tdr_pull pull;
655                 uint16_t i;
656                 uint16_t sublist_count = 0;
657
658                 DEBUG(10, ("Subkeys in RI list\n"));
659                 ZERO_STRUCT(pull);
660                 pull.data = data;
661
662                 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(&pull, nk, &ri))) {
663                         DEBUG(0, ("Error parsing RI list\n"));
664                         return WERR_GENERAL_FAILURE;
665                 }
666                 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
667
668                 for (i = 0; i < ri.key_count; i++) {
669                         DATA_BLOB list_data;
670
671                         /* Get sublist data blob */
672                         list_data = hbin_get(private_data->hive, ri.offset[i]);
673                         if (!list_data.data) {
674                                 DEBUG(0, ("Error getting RI list."));
675                                 return WERR_GENERAL_FAILURE;
676                         }
677
678                         ZERO_STRUCT(pull);
679                         pull.data = list_data;
680
681                         if (!strncmp((char *)list_data.data, "li", 2)) {
682                                 struct li_block li;
683
684                                 DEBUG(10, ("Subkeys in RI->LI list\n"));
685
686                                 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull,
687                                                                        nk,
688                                                                        &li))) {
689                                         DEBUG(0, ("Error parsing LI list from RI\n"));
690                                         return WERR_GENERAL_FAILURE;
691                                 }
692                                 SMB_ASSERT(!strncmp(li.header, "li", 2));
693
694                                 /* Advance to next sublist if necessary */
695                                 if (idx >= sublist_count + li.key_count) {
696                                         sublist_count += li.key_count;
697                                         continue;
698                                 }
699                                 key_off = li.nk_offset[idx - sublist_count];
700                                 sublist_count += li.key_count;
701                                 break;
702                         } else if (!strncmp((char *)list_data.data, "lh", 2)) {
703                                 struct lh_block lh;
704
705                                 DEBUG(10, ("Subkeys in RI->LH list\n"));
706
707                                 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull,
708                                                                        nk,
709                                                                        &lh))) {
710                                         DEBUG(0, ("Error parsing LH list from RI\n"));
711                                         return WERR_GENERAL_FAILURE;
712                                 }
713                                 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
714
715                                 /* Advance to next sublist if necessary */
716                                 if (idx >= sublist_count + lh.key_count) {
717                                         sublist_count += lh.key_count;
718                                         continue;
719                                 }
720                                 key_off = lh.hr[idx - sublist_count].nk_offset;
721                                 sublist_count += lh.key_count;
722                                 break;
723                         } else {
724                                 DEBUG(0,("Unknown sublist in ri block\n"));
725
726                                 return WERR_GENERAL_FAILURE;
727                         }
728
729                 }
730
731                 if (idx > sublist_count) {
732                         return WERR_NO_MORE_ITEMS;
733                 }
734
735         } else {
736                 DEBUG(0, ("Unknown type for subkey list (0x%04x): %c%c\n",
737                                   nk->subkeys_offset, data.data[0], data.data[1]));
738                 return WERR_GENERAL_FAILURE;
739         }
740
741         ret = regf_get_key (ctx, private_data->hive, key_off);
742
743         if (classname != NULL) {
744                 if (ret->nk->clsname_offset != -1) {
745                         DATA_BLOB db = hbin_get(ret->hive,
746                                                 ret->nk->clsname_offset);
747                         *classname = talloc_strndup(ctx,
748                                                     (char*)db.data,
749                                                     ret->nk->clsname_length);
750                 } else
751                         *classname = NULL;
752         }
753
754         if (last_mod_time != NULL)
755                 *last_mod_time = ret->nk->last_change;
756
757         if (name != NULL)
758                 *name = talloc_steal(ctx, ret->nk->key_name);
759
760         talloc_free(ret);
761
762         return WERR_OK;
763 }
764
765 static WERROR regf_match_subkey_by_name(TALLOC_CTX *ctx,
766                                         const struct hive_key *key,
767                                         uint32_t offset,
768                                         const char *name, uint32_t *ret)
769 {
770         DATA_BLOB subkey_data;
771         struct nk_block subkey;
772         struct tdr_pull pull;
773         const struct regf_key_data *private_data =
774                 (const struct regf_key_data *)key;
775
776         subkey_data = hbin_get(private_data->hive, offset);
777         if (!subkey_data.data) {
778                 DEBUG(0, ("Unable to retrieve subkey HBIN\n"));
779                 return WERR_GENERAL_FAILURE;
780         }
781
782         ZERO_STRUCT(pull);
783         pull.data = subkey_data;
784
785         if (NT_STATUS_IS_ERR(tdr_pull_nk_block(&pull, ctx, &subkey))) {
786                 DEBUG(0, ("Error parsing NK structure.\n"));
787                 return WERR_GENERAL_FAILURE;
788         }
789
790         if (strncmp(subkey.header, "nk", 2)) {
791                 DEBUG(0, ("Not an NK structure.\n"));
792                 return WERR_GENERAL_FAILURE;
793         }
794
795         if (!strcasecmp(subkey.key_name, name)) {
796                 *ret = offset;
797         } else {
798                 *ret = 0;
799         }
800         return WERR_OK;
801 }
802
803 static WERROR regf_get_subkey_by_name(TALLOC_CTX *ctx,
804                                       const struct hive_key *key,
805                                       const char *name,
806                                       struct hive_key **ret)
807 {
808         DATA_BLOB data;
809         const struct regf_key_data *private_data =
810                 (const struct regf_key_data *)key;
811         struct nk_block *nk = private_data->nk;
812         uint32_t key_off = 0;
813
814         data = hbin_get(private_data->hive, nk->subkeys_offset);
815         if (!data.data) {
816                 DEBUG(0, ("Unable to find subkey list\n"));
817                 return WERR_GENERAL_FAILURE;
818         }
819
820         if (!strncmp((char *)data.data, "li", 2)) {
821                 struct li_block li;
822                 struct tdr_pull pull;
823                 uint16_t i;
824
825                 DEBUG(10, ("Subkeys in LI list\n"));
826                 ZERO_STRUCT(pull);
827                 pull.data = data;
828
829                 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, nk, &li))) {
830                         DEBUG(0, ("Error parsing LI list\n"));
831                         return WERR_GENERAL_FAILURE;
832                 }
833                 SMB_ASSERT(!strncmp(li.header, "li", 2));
834
835                 if (li.key_count != nk->num_subkeys) {
836                         DEBUG(0, ("Subkey counts don't match\n"));
837                         return WERR_GENERAL_FAILURE;
838                 }
839
840                 for (i = 0; i < li.key_count; i++) {
841                         W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
842                                                                         li.nk_offset[i],
843                                                                         name,
844                                                                         &key_off));
845                         if (key_off != 0)
846                                 break;
847                 }
848                 if (key_off == 0)
849                         return WERR_NOT_FOUND;
850         } else if (!strncmp((char *)data.data, "lf", 2)) {
851                 struct lf_block lf;
852                 struct tdr_pull pull;
853                 uint16_t i;
854
855                 DEBUG(10, ("Subkeys in LF list\n"));
856                 ZERO_STRUCT(pull);
857                 pull.data = data;
858
859                 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(&pull, nk, &lf))) {
860                         DEBUG(0, ("Error parsing LF list\n"));
861                         return WERR_GENERAL_FAILURE;
862                 }
863                 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
864
865                 if (lf.key_count != nk->num_subkeys) {
866                         DEBUG(0, ("Subkey counts don't match\n"));
867                         return WERR_GENERAL_FAILURE;
868                 }
869
870                 for (i = 0; i < lf.key_count; i++) {
871                         if (strncmp(lf.hr[i].hash, name, 4)) {
872                                 continue;
873                         }
874                         W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
875                                                                         key,
876                                                                         lf.hr[i].nk_offset,
877                                                                         name,
878                                                                         &key_off));
879                         if (key_off != 0)
880                                 break;
881                 }
882                 if (key_off == 0)
883                         return WERR_NOT_FOUND;
884         } else if (!strncmp((char *)data.data, "lh", 2)) {
885                 struct lh_block lh;
886                 struct tdr_pull pull;
887                 uint16_t i;
888                 uint32_t hash;
889
890                 DEBUG(10, ("Subkeys in LH list\n"));
891                 ZERO_STRUCT(pull);
892                 pull.data = data;
893
894                 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, nk, &lh))) {
895                         DEBUG(0, ("Error parsing LH list\n"));
896                         return WERR_GENERAL_FAILURE;
897                 }
898                 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
899
900                 if (lh.key_count != nk->num_subkeys) {
901                         DEBUG(0, ("Subkey counts don't match\n"));
902                         return WERR_GENERAL_FAILURE;
903                 }
904
905                 hash = regf_create_lh_hash(name);
906                 for (i = 0; i < lh.key_count; i++) {
907                         if (lh.hr[i].base37 != hash) {
908                                 continue;
909                         }
910                         W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk,
911                                                                         key,
912                                                                         lh.hr[i].nk_offset,
913                                                                         name,
914                                                                         &key_off));
915                         if (key_off != 0)
916                                 break;
917                 }
918                 if (key_off == 0)
919                         return WERR_NOT_FOUND;
920         } else if (!strncmp((char *)data.data, "ri", 2)) {
921                 struct ri_block ri;
922                 struct tdr_pull pull;
923                 uint16_t i, j;
924
925                 DEBUG(10, ("Subkeys in RI list\n"));
926                 ZERO_STRUCT(pull);
927                 pull.data = data;
928
929                 if (NT_STATUS_IS_ERR(tdr_pull_ri_block(&pull, nk, &ri))) {
930                         DEBUG(0, ("Error parsing RI list\n"));
931                         return WERR_GENERAL_FAILURE;
932                 }
933                 SMB_ASSERT(!strncmp(ri.header, "ri", 2));
934
935                 for (i = 0; i < ri.key_count; i++) {
936                         DATA_BLOB list_data;
937
938                         /* Get sublist data blob */
939                         list_data = hbin_get(private_data->hive, ri.offset[i]);
940                         if (list_data.data == NULL) {
941                                 DEBUG(0, ("Error getting RI list."));
942                                 return WERR_GENERAL_FAILURE;
943                         }
944
945                         ZERO_STRUCT(pull);
946                         pull.data = list_data;
947
948                         if (!strncmp((char *)list_data.data, "li", 2)) {
949                                 struct li_block li;
950
951                                 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull,
952                                                                        nk,
953                                                                        &li))) {
954                                         DEBUG(0, ("Error parsing LI list from RI\n"));
955                                         return WERR_GENERAL_FAILURE;
956                                 }
957                                 SMB_ASSERT(!strncmp(li.header, "li", 2));
958
959                                 for (j = 0; j < li.key_count; j++) {
960                                         W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
961                                                                                         li.nk_offset[j],
962                                                                                         name,
963                                                                                         &key_off));
964                                         if (key_off)
965                                                 break;
966                                 }
967                         } else if (!strncmp((char *)list_data.data, "lh", 2)) {
968                                 struct lh_block lh;
969                                 uint32_t hash;
970
971                                 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull,
972                                                                        nk,
973                                                                        &lh))) {
974                                         DEBUG(0, ("Error parsing LH list from RI\n"));
975                                         return WERR_GENERAL_FAILURE;
976                                 }
977                                 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
978
979                                 hash = regf_create_lh_hash(name);
980                                 for (j = 0; j < lh.key_count; j++) {
981                                         if (lh.hr[j].base37 != hash) {
982                                                 continue;
983                                         }
984                                         W_ERROR_NOT_OK_RETURN(regf_match_subkey_by_name(nk, key,
985                                                                                         lh.hr[j].nk_offset,
986                                                                                         name,
987                                                                                         &key_off));
988                                         if (key_off)
989                                                 break;
990                                 }
991                         }
992                         if (key_off)
993                                 break;
994                 }
995                 if (!key_off)
996                         return WERR_NOT_FOUND;
997         } else {
998                 DEBUG(0, ("Unknown subkey list type.\n"));
999                 return WERR_GENERAL_FAILURE;
1000         }
1001
1002         *ret = (struct hive_key *)regf_get_key(ctx, private_data->hive,
1003                                                key_off);
1004         return WERR_OK;
1005 }
1006
1007 static WERROR regf_set_sec_desc(struct hive_key *key,
1008                                 const struct security_descriptor *sec_desc)
1009 {
1010         const struct regf_key_data *private_data =
1011                 (const struct regf_key_data *)key;
1012         struct sk_block cur_sk, sk, new_sk;
1013         struct regf_data *regf = private_data->hive;
1014         struct nk_block root;
1015         DATA_BLOB data;
1016         uint32_t sk_offset, cur_sk_offset;
1017         bool update_cur_sk = false;
1018
1019         /* Get the root nk */
1020         hbin_get_tdr(regf, regf->header->data_offset, regf,
1021                      (tdr_pull_fn_t) tdr_pull_nk_block, &root);
1022
1023         /* Push the security descriptor to a blob */
1024         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_push_struct_blob(&data, regf, sec_desc,
1025                              (ndr_push_flags_fn_t)ndr_push_security_descriptor))) {
1026                 DEBUG(0, ("Unable to push security descriptor\n"));
1027                 return WERR_GENERAL_FAILURE;
1028         }
1029
1030         /* Get the current security descriptor for the key */
1031         if (!hbin_get_tdr(regf, private_data->nk->sk_offset, regf,
1032                           (tdr_pull_fn_t) tdr_pull_sk_block, &cur_sk)) {
1033                 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1034                 return WERR_BADFILE;
1035         }
1036         /* If there's no change, change nothing. */
1037         if (memcmp(data.data, cur_sk.sec_desc,
1038                    MIN(data.length, cur_sk.rec_size)) == 0) {
1039                 return WERR_OK;
1040         }
1041
1042         /* Delete the current sk if only this key is using it */
1043         if (cur_sk.ref_cnt == 1) {
1044                 /* Get the previous security descriptor for the key */
1045                 if (!hbin_get_tdr(regf, cur_sk.prev_offset, regf,
1046                                   (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1047                         DEBUG(0, ("Unable to find prev security descriptor for current key\n"));
1048                         return WERR_BADFILE;
1049                 }
1050                 /* Change and store the previous security descriptor */
1051                 sk.next_offset = cur_sk.next_offset;
1052                 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
1053                                       cur_sk.prev_offset, &sk);
1054
1055                 /* Get the next security descriptor for the key */
1056                 if (!hbin_get_tdr(regf, cur_sk.next_offset, regf,
1057                                   (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1058                         DEBUG(0, ("Unable to find next security descriptor for current key\n"));
1059                         return WERR_BADFILE;
1060                 }
1061                 /* Change and store the next security descriptor */
1062                 sk.prev_offset = cur_sk.prev_offset;
1063                 hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_sk_block,
1064                                       cur_sk.next_offset, &sk);
1065
1066                 hbin_free(regf, private_data->nk->sk_offset);
1067         } else {
1068                 /* This key will no longer be referring to this sk */
1069                 cur_sk.ref_cnt--;
1070                 update_cur_sk = true;
1071         }
1072
1073         sk_offset = root.sk_offset;
1074
1075         do {
1076                 cur_sk_offset = sk_offset;
1077                 if (!hbin_get_tdr(regf, sk_offset, regf,
1078                                   (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1079                         DEBUG(0, ("Unable to find security descriptor\n"));
1080                         return WERR_BADFILE;
1081                 }
1082                 if (memcmp(data.data, sk.sec_desc, MIN(data.length, sk.rec_size)) == 0) {
1083                         private_data->nk->sk_offset = sk_offset;
1084                         sk.ref_cnt++;
1085                         hbin_store_tdr_resize(regf,
1086                                               (tdr_push_fn_t) tdr_push_sk_block,
1087                                               sk_offset, &sk);
1088                         hbin_store_tdr_resize(regf,
1089                                               (tdr_push_fn_t) tdr_push_nk_block,
1090                                               private_data->offset,
1091                                               private_data->nk);
1092                         return WERR_OK;
1093                 }
1094                 sk_offset = sk.next_offset;
1095         } while (sk_offset != root.sk_offset);
1096
1097         ZERO_STRUCT(new_sk);
1098         new_sk.header = "sk";
1099         new_sk.prev_offset = cur_sk_offset;
1100         new_sk.next_offset = root.sk_offset;
1101         new_sk.ref_cnt = 1;
1102         new_sk.rec_size = data.length;
1103         new_sk.sec_desc = data.data;
1104
1105         sk_offset = hbin_store_tdr(regf,
1106                                    (tdr_push_fn_t) tdr_push_sk_block,
1107                                    &new_sk);
1108         if (sk_offset == -1) {
1109                 DEBUG(0, ("Error storing sk block\n"));
1110                 return WERR_GENERAL_FAILURE;
1111         }
1112         private_data->nk->sk_offset = sk_offset;
1113
1114         if (update_cur_sk) {
1115                 hbin_store_tdr_resize(regf,
1116                                       (tdr_push_fn_t) tdr_push_sk_block,
1117                                       private_data->nk->sk_offset, &cur_sk);
1118         }
1119
1120         /* Get the previous security descriptor for the key */
1121         if (!hbin_get_tdr(regf, new_sk.prev_offset, regf,
1122                           (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1123                 DEBUG(0, ("Unable to find security descriptor for previous key\n"));
1124                 return WERR_BADFILE;
1125         }
1126         /* Change and store the previous security descriptor */
1127         sk.next_offset = sk_offset;
1128         hbin_store_tdr_resize(regf,
1129                               (tdr_push_fn_t) tdr_push_sk_block,
1130                               cur_sk.prev_offset, &sk);
1131
1132         /* Get the next security descriptor for the key (always root, as we append) */
1133         if (!hbin_get_tdr(regf, new_sk.next_offset, regf,
1134                           (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1135                 DEBUG(0, ("Unable to find security descriptor for current key\n"));
1136                 return WERR_BADFILE;
1137         }
1138         /* Change and store the next security descriptor (always root, as we append) */
1139         sk.prev_offset = sk_offset;
1140         hbin_store_tdr_resize(regf,
1141                               (tdr_push_fn_t) tdr_push_sk_block,
1142                               root.sk_offset, &sk);
1143
1144
1145         /* Store the nk. */
1146         hbin_store_tdr_resize(regf,
1147                               (tdr_push_fn_t) tdr_push_sk_block,
1148                               private_data->offset, private_data->nk);
1149         return WERR_OK;
1150 }
1151
1152 static WERROR regf_get_sec_desc(TALLOC_CTX *ctx, const struct hive_key *key,
1153                                 struct security_descriptor **sd)
1154 {
1155         const struct regf_key_data *private_data =
1156                 (const struct regf_key_data *)key;
1157         struct sk_block sk;
1158         struct regf_data *regf = private_data->hive;
1159         DATA_BLOB data;
1160
1161         if (!hbin_get_tdr(regf, private_data->nk->sk_offset, ctx,
1162                           (tdr_pull_fn_t) tdr_pull_sk_block, &sk)) {
1163                 DEBUG(0, ("Unable to find security descriptor\n"));
1164                 return WERR_GENERAL_FAILURE;
1165         }
1166
1167         if (strcmp(sk.header, "sk") != 0) {
1168                 DEBUG(0, ("Expected 'sk', got '%s'\n", sk.header));
1169                 return WERR_GENERAL_FAILURE;
1170         }
1171
1172         *sd = talloc(ctx, struct security_descriptor);
1173         W_ERROR_HAVE_NO_MEMORY(*sd);
1174
1175         data.data = sk.sec_desc;
1176         data.length = sk.rec_size;
1177         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_struct_blob(&data, ctx, *sd,
1178                                                   (ndr_pull_flags_fn_t)ndr_pull_security_descriptor))) {
1179                 DEBUG(0, ("Error parsing security descriptor\n"));
1180                 return WERR_GENERAL_FAILURE;
1181         }
1182
1183         return WERR_OK;
1184 }
1185
1186 static WERROR regf_sl_add_entry(struct regf_data *regf, uint32_t list_offset,
1187                                 const char *name,
1188                                 uint32_t key_offset, uint32_t *ret)
1189 {
1190         DATA_BLOB data;
1191
1192         /* Create a new key if necessary */
1193         if (list_offset == -1) {
1194                 if (regf->header->version.major != 1) {
1195                         DEBUG(0, ("Can't store keys in unknown registry format\n"));
1196                         return WERR_NOT_SUPPORTED;
1197                 }
1198                 if (regf->header->version.minor < 3) {
1199                         /* Store LI */
1200                         struct li_block li;
1201                         ZERO_STRUCT(li);
1202                         li.header = "li";
1203                         li.key_count = 1;
1204
1205                         li.nk_offset = talloc_array(regf, uint32_t, 1);
1206                         W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1207                         li.nk_offset[0] = key_offset;
1208
1209                         *ret = hbin_store_tdr(regf,
1210                                               (tdr_push_fn_t) tdr_push_li_block,
1211                                               &li);
1212
1213                         talloc_free(li.nk_offset);
1214                 } else if (regf->header->version.minor == 3 ||
1215                            regf->header->version.minor == 4) {
1216                         /* Store LF */
1217                         struct lf_block lf;
1218                         ZERO_STRUCT(lf);
1219                         lf.header = "lf";
1220                         lf.key_count = 1;
1221
1222                         lf.hr = talloc_array(regf, struct hash_record, 1);
1223                         W_ERROR_HAVE_NO_MEMORY(lf.hr);
1224                         lf.hr[0].nk_offset = key_offset;
1225                         lf.hr[0].hash = talloc_strndup(lf.hr, name, 4);
1226                         W_ERROR_HAVE_NO_MEMORY(lf.hr[0].hash);
1227
1228                         *ret = hbin_store_tdr(regf,
1229                                               (tdr_push_fn_t) tdr_push_lf_block,
1230                                               &lf);
1231
1232                         talloc_free(lf.hr);
1233                 } else if (regf->header->version.minor == 5) {
1234                         /* Store LH */
1235                         struct lh_block lh;
1236                         ZERO_STRUCT(lh);
1237                         lh.header = "lh";
1238                         lh.key_count = 1;
1239
1240                         lh.hr = talloc_array(regf, struct lh_hash, 1);
1241                         W_ERROR_HAVE_NO_MEMORY(lh.hr);
1242                         lh.hr[0].nk_offset = key_offset;
1243                         lh.hr[0].base37 = regf_create_lh_hash(name);
1244
1245                         *ret = hbin_store_tdr(regf,
1246                                               (tdr_push_fn_t) tdr_push_lh_block,
1247                                               &lh);
1248
1249                         talloc_free(lh.hr);
1250                 }
1251                 return WERR_OK;
1252         }
1253
1254         data = hbin_get(regf, list_offset);
1255         if (!data.data) {
1256                 DEBUG(0, ("Unable to find subkey list\n"));
1257                 return WERR_BADFILE;
1258         }
1259
1260         if (!strncmp((char *)data.data, "li", 2)) {
1261                 struct tdr_pull pull;
1262                 struct li_block li;
1263
1264                 ZERO_STRUCT(pull);
1265                 pull.data = data;
1266
1267                 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, regf, &li))) {
1268                         DEBUG(0, ("Error parsing LI list\n"));
1269                         return WERR_BADFILE;
1270                 }
1271
1272                 if (strncmp(li.header, "li", 2) != 0) {
1273                         abort();
1274                         DEBUG(0, ("LI header corrupt\n"));
1275                         return WERR_BADFILE;
1276                 }
1277
1278                 li.nk_offset = talloc_realloc(regf, li.nk_offset,
1279                                               uint32_t, li.key_count+1);
1280                 W_ERROR_HAVE_NO_MEMORY(li.nk_offset);
1281                 li.nk_offset[li.key_count] = key_offset;
1282                 li.key_count++;
1283                 *ret = hbin_store_tdr_resize(regf,
1284                                              (tdr_push_fn_t)tdr_push_li_block,
1285                                              list_offset, &li);
1286
1287                 talloc_free(li.nk_offset);
1288         } else if (!strncmp((char *)data.data, "lf", 2)) {
1289                 struct tdr_pull pull;
1290                 struct lf_block lf;
1291
1292                 ZERO_STRUCT(pull);
1293                 pull.data = data;
1294
1295                 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(&pull, regf, &lf))) {
1296                         DEBUG(0, ("Error parsing LF list\n"));
1297                         return WERR_BADFILE;
1298                 }
1299                 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1300
1301                 lf.hr = talloc_realloc(regf, lf.hr, struct hash_record,
1302                                        lf.key_count+1);
1303                 W_ERROR_HAVE_NO_MEMORY(lf.hr);
1304                 lf.hr[lf.key_count].nk_offset = key_offset;
1305                 lf.hr[lf.key_count].hash = talloc_strndup(lf.hr, name, 4);
1306                 W_ERROR_HAVE_NO_MEMORY(lf.hr[lf.key_count].hash);
1307                 lf.key_count++;
1308                 *ret = hbin_store_tdr_resize(regf,
1309                                              (tdr_push_fn_t)tdr_push_lf_block,
1310                                              list_offset, &lf);
1311
1312                 talloc_free(lf.hr);
1313         } else if (!strncmp((char *)data.data, "lh", 2)) {
1314                 struct tdr_pull pull;
1315                 struct lh_block lh;
1316
1317                 ZERO_STRUCT(pull);
1318                 pull.data = data;
1319
1320                 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, regf, &lh))) {
1321                         DEBUG(0, ("Error parsing LH list\n"));
1322                         return WERR_BADFILE;
1323                 }
1324                 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1325
1326                 lh.hr = talloc_realloc(regf, lh.hr, struct lh_hash,
1327                                        lh.key_count+1);
1328                 W_ERROR_HAVE_NO_MEMORY(lh.hr);
1329                 lh.hr[lh.key_count].nk_offset = key_offset;
1330                 lh.hr[lh.key_count].base37 = regf_create_lh_hash(name);
1331                 lh.key_count++;
1332                 *ret = hbin_store_tdr_resize(regf,
1333                                              (tdr_push_fn_t)tdr_push_lh_block,
1334                                              list_offset, &lh);
1335
1336                 talloc_free(lh.hr);
1337         } else if (!strncmp((char *)data.data, "ri", 2)) {
1338                 /* FIXME */
1339                 DEBUG(0, ("Adding to 'ri' subkey list is not supported yet.\n"));
1340                 return WERR_NOT_SUPPORTED;
1341         } else {
1342                 DEBUG(0, ("Cannot add to unknown subkey list\n"));
1343                 return WERR_BADFILE;
1344         }
1345
1346         return WERR_OK;
1347 }
1348
1349 static WERROR regf_sl_del_entry(struct regf_data *regf, uint32_t list_offset,
1350                                 uint32_t key_offset, uint32_t *ret)
1351 {
1352         DATA_BLOB data;
1353
1354         data = hbin_get(regf, list_offset);
1355         if (!data.data) {
1356                 DEBUG(0, ("Unable to find subkey list\n"));
1357                 return WERR_BADFILE;
1358         }
1359
1360         if (strncmp((char *)data.data, "li", 2) == 0) {
1361                 struct li_block li;
1362                 struct tdr_pull pull;
1363                 uint16_t i;
1364                 bool found_offset = false;
1365
1366                 DEBUG(10, ("Subkeys in LI list\n"));
1367
1368                 ZERO_STRUCT(pull);
1369                 pull.data = data;
1370
1371                 if (NT_STATUS_IS_ERR(tdr_pull_li_block(&pull, regf, &li))) {
1372                         DEBUG(0, ("Error parsing LI list\n"));
1373                         return WERR_BADFILE;
1374                 }
1375
1376                 SMB_ASSERT(!strncmp(li.header, "li", 2));
1377
1378                 for (i = 0; i < li.key_count; i++) {
1379                         if (found_offset) {
1380                                 li.nk_offset[i-1] = li.nk_offset[i];
1381                         }
1382                         if (li.nk_offset[i] == key_offset) {
1383                                 found_offset = true;
1384                                 continue;
1385                         }
1386                 }
1387                 if (!found_offset) {
1388                         DEBUG(2, ("Subkey not found\n"));
1389                         return WERR_NOT_FOUND;
1390                 }
1391                 li.key_count--;
1392
1393                 /* If the there are no entries left, free the subkey list */
1394                 if (li.key_count == 0) {
1395                         hbin_free(regf, list_offset);
1396                         *ret = -1;
1397                 }
1398
1399                 /* Store li block */
1400                 *ret = hbin_store_tdr_resize(regf,
1401                                              (tdr_push_fn_t) tdr_push_li_block,
1402                                              list_offset, &li);
1403         } else if (strncmp((char *)data.data, "lf", 2) == 0) {
1404                 struct lf_block lf;
1405                 struct tdr_pull pull;
1406                 uint16_t i;
1407                 bool found_offset = false;
1408
1409                 DEBUG(10, ("Subkeys in LF list\n"));
1410
1411                 ZERO_STRUCT(pull);
1412                 pull.data = data;
1413
1414                 if (NT_STATUS_IS_ERR(tdr_pull_lf_block(&pull, regf, &lf))) {
1415                         DEBUG(0, ("Error parsing LF list\n"));
1416                         return WERR_BADFILE;
1417                 }
1418
1419                 SMB_ASSERT(!strncmp(lf.header, "lf", 2));
1420
1421                 for (i = 0; i < lf.key_count; i++) {
1422                         if (found_offset) {
1423                                 lf.hr[i-1] = lf.hr[i];
1424                                 continue;
1425                         }
1426                         if (lf.hr[i].nk_offset == key_offset) {
1427                                 found_offset = 1;
1428                                 continue;
1429                         }
1430                 }
1431                 if (!found_offset) {
1432                         DEBUG(2, ("Subkey not found\n"));
1433                         return WERR_NOT_FOUND;
1434                 }
1435                 lf.key_count--;
1436
1437                 /* If the there are no entries left, free the subkey list */
1438                 if (lf.key_count == 0) {
1439                         hbin_free(regf, list_offset);
1440                         *ret = -1;
1441                         return WERR_OK;
1442                 }
1443
1444                 /* Store lf block */
1445                 *ret = hbin_store_tdr_resize(regf,
1446                                              (tdr_push_fn_t) tdr_push_lf_block,
1447                                              list_offset, &lf);
1448         } else if (strncmp((char *)data.data, "lh", 2) == 0) {
1449                 struct lh_block lh;
1450                 struct tdr_pull pull;
1451                 uint16_t i;
1452                 bool found_offset = false;
1453
1454                 DEBUG(10, ("Subkeys in LH list\n"));
1455
1456                 ZERO_STRUCT(pull);
1457                 pull.data = data;
1458
1459                 if (NT_STATUS_IS_ERR(tdr_pull_lh_block(&pull, regf, &lh))) {
1460                         DEBUG(0, ("Error parsing LF list\n"));
1461                         return WERR_BADFILE;
1462                 }
1463
1464                 SMB_ASSERT(!strncmp(lh.header, "lh", 2));
1465
1466                 for (i = 0; i < lh.key_count; i++) {
1467                         if (found_offset) {
1468                                 lh.hr[i-1] = lh.hr[i];
1469                                 continue;
1470                         }
1471                         if (lh.hr[i].nk_offset == key_offset) {
1472                                 found_offset = 1;
1473                                 continue;
1474                         }
1475                 }
1476                 if (!found_offset) {
1477                         DEBUG(0, ("Subkey not found\n"));
1478                         return WERR_NOT_FOUND;
1479                 }
1480                 lh.key_count--;
1481
1482                 /* If the there are no entries left, free the subkey list */
1483                 if (lh.key_count == 0) {
1484                         hbin_free(regf, list_offset);
1485                         *ret = -1;
1486                         return WERR_OK;
1487                 }
1488
1489                 /* Store lh block */
1490                 *ret = hbin_store_tdr_resize(regf,
1491                                              (tdr_push_fn_t) tdr_push_lh_block,
1492                                              list_offset, &lh);
1493         } else if (strncmp((char *)data.data, "ri", 2) == 0) {
1494                 /* FIXME */
1495                 DEBUG(0, ("Sorry, deletion from ri block is not supported yet.\n"));
1496                 return WERR_NOT_SUPPORTED;
1497         } else {
1498                 DEBUG (0, ("Unknown header found in subkey list.\n"));
1499                 return WERR_BADFILE;
1500         }
1501         return WERR_OK;
1502 }
1503
1504 static WERROR regf_del_value (struct hive_key *key, const char *name)
1505 {
1506         const struct regf_key_data *private_data =
1507                 (const struct regf_key_data *)key;
1508         struct regf_data *regf = private_data->hive;
1509         struct nk_block *nk = private_data->nk;
1510         struct vk_block vk;
1511         uint32_t vk_offset;
1512         bool found_offset = false;
1513         DATA_BLOB values;
1514         uint32_t i;
1515
1516         if (nk->values_offset == -1) {
1517                 return WERR_NOT_FOUND;
1518         }
1519
1520         values = hbin_get(regf, nk->values_offset);
1521
1522         for (i = 0; i < nk->num_values; i++) {
1523                 if (found_offset) {
1524                         ((uint32_t *)values.data)[i-1] = ((uint32_t *) values.data)[i];
1525                 } else {
1526                         vk_offset = IVAL(values.data, i * 4);
1527                         if (!hbin_get_tdr(regf, vk_offset, private_data,
1528                                           (tdr_pull_fn_t)tdr_pull_vk_block,
1529                                           &vk)) {
1530                                 DEBUG(0, ("Unable to get VK block at %d\n",
1531                                         vk_offset));
1532                                 return WERR_BADFILE;
1533                         }
1534                         if (strcmp(vk.data_name, name) == 0) {
1535                                 hbin_free(regf, vk_offset);
1536                                 found_offset = true;
1537                         }
1538                 }
1539         }
1540         if (!found_offset) {
1541                 return WERR_NOT_FOUND;
1542         } else {
1543                 nk->num_values--;
1544                 values.length = (nk->num_values)*4;
1545         }
1546
1547         /* Store values list and nk */
1548         if (nk->num_values == 0) {
1549                 hbin_free(regf, nk->values_offset);
1550                 nk->values_offset = -1;
1551         } else {
1552                 nk->values_offset = hbin_store_resize(regf,
1553                                                       nk->values_offset,
1554                                                       values);
1555         }
1556         hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
1557                               private_data->offset, nk);
1558
1559         return regf_save_hbin(private_data->hive);
1560 }
1561
1562
1563 static WERROR regf_del_key(const struct hive_key *parent, const char *name)
1564 {
1565         const struct regf_key_data *private_data =
1566                 (const struct regf_key_data *)parent;
1567         struct regf_key_data *key;
1568         struct nk_block *parent_nk;
1569         WERROR error;
1570
1571         SMB_ASSERT(private_data);
1572
1573         parent_nk = private_data->nk;
1574
1575         if (parent_nk->subkeys_offset == -1) {
1576                 DEBUG(4, ("Subkey list is empty, this key cannot contain subkeys.\n"));
1577                 return WERR_NOT_FOUND;
1578         }
1579
1580         /* Find the key */
1581         if (!W_ERROR_IS_OK(regf_get_subkey_by_name(parent_nk, parent, name,
1582                                                    (struct hive_key **)&key))) {
1583                 DEBUG(2, ("Key '%s' not found\n", name));
1584                 return WERR_NOT_FOUND;
1585         }
1586
1587         if (key->nk->subkeys_offset != -1 ||
1588                 key->nk->values_offset != -1) {
1589                 DEBUG(0, ("Key '%s' is not empty.\n", name));
1590                 return WERR_FILE_EXISTS;
1591         }
1592
1593         /* Delete it from the subkey list. */
1594         error = regf_sl_del_entry(private_data->hive, parent_nk->subkeys_offset,
1595                                   key->offset, &parent_nk->subkeys_offset);
1596         if (!W_ERROR_IS_OK(error)) {
1597                 DEBUG(0, ("Can't store new subkey list for parent key. Won't delete.\n"));
1598                 return error;
1599         }
1600
1601         /* Re-store parent key */
1602         parent_nk->num_subkeys--;
1603         hbin_store_tdr_resize(private_data->hive,
1604                               (tdr_push_fn_t) tdr_push_nk_block,
1605                               private_data->offset, parent_nk);
1606
1607         if (key->nk->clsname_offset != -1) {
1608                 hbin_free(private_data->hive, key->nk->clsname_offset);
1609         }
1610         hbin_free(private_data->hive, key->offset);
1611
1612         return regf_save_hbin(private_data->hive);
1613 }
1614
1615 static WERROR regf_add_key(TALLOC_CTX *ctx, const struct hive_key *parent,
1616                            const char *name, const char *classname,
1617                            struct security_descriptor *sec_desc,
1618                            struct hive_key **ret)
1619 {
1620         const struct regf_key_data *private_data =
1621                 (const struct regf_key_data *)parent;
1622         struct nk_block *parent_nk = private_data->nk, nk;
1623         struct nk_block *root;
1624         struct regf_data *regf = private_data->hive;
1625         uint32_t offset;
1626         WERROR error;
1627
1628         nk.header = "nk";
1629         nk.type = REG_SUB_KEY;
1630         unix_to_nt_time(&nk.last_change, time(NULL));
1631         nk.uk1 = 0;
1632         nk.parent_offset = private_data->offset;
1633         nk.num_subkeys = 0;
1634         nk.uk2 = 0;
1635         nk.subkeys_offset = -1;
1636         nk.unknown_offset = -1;
1637         nk.num_values = 0;
1638         nk.values_offset = -1;
1639         memset(nk.unk3, 0, 5);
1640         nk.clsname_offset = -1; /* FIXME: fill in */
1641         nk.clsname_length = 0;
1642         nk.key_name = name;
1643
1644         /* Get the security descriptor of the root key */
1645         root = talloc_zero(ctx, struct nk_block);
1646         W_ERROR_HAVE_NO_MEMORY(root);
1647
1648         if (!hbin_get_tdr(regf, regf->header->data_offset, root,
1649                           (tdr_pull_fn_t)tdr_pull_nk_block, root)) {
1650                 DEBUG(0, ("Unable to find HBIN data for offset %d\n", offset));
1651                 return WERR_GENERAL_FAILURE;
1652         }
1653         nk.sk_offset = root->sk_offset;
1654         talloc_free(root);
1655
1656         /* Store the new nk key */
1657         offset = hbin_store_tdr(regf, (tdr_push_fn_t) tdr_push_nk_block, &nk);
1658
1659         error = regf_sl_add_entry(regf, parent_nk->subkeys_offset, name, offset,
1660                                   &parent_nk->subkeys_offset);
1661         if (!W_ERROR_IS_OK(error)) {
1662                 hbin_free(regf, offset);
1663                 return error;
1664         }
1665
1666         parent_nk->num_subkeys++;
1667
1668         /* Since the subkey offset of the parent can change, store it again */
1669         hbin_store_tdr_resize(regf, (tdr_push_fn_t) tdr_push_nk_block,
1670                                                   nk.parent_offset, parent_nk);
1671
1672         *ret = (struct hive_key *)regf_get_key(ctx, regf, offset);
1673
1674         return regf_save_hbin(private_data->hive);
1675 }
1676
1677 static WERROR regf_set_value(struct hive_key *key, const char *name,
1678                              uint32_t type, const DATA_BLOB data)
1679 {
1680         const struct regf_key_data *private_data =
1681                 (const struct regf_key_data *)key;
1682         struct regf_data *regf = private_data->hive;
1683         struct nk_block *nk = private_data->nk;
1684         struct vk_block vk;
1685         uint32_t i;
1686         uint32_t tmp_vk_offset, vk_offset, old_vk_offset = -1;
1687         DATA_BLOB values;
1688
1689         ZERO_STRUCT(vk);
1690
1691         /* find the value offset, if it exists */
1692         if (nk->values_offset != -1) {
1693                 values = hbin_get(regf, nk->values_offset);
1694
1695                 for (i = 0; i < nk->num_values; i++) {
1696                         tmp_vk_offset = IVAL(values.data, i * 4);
1697                         if (!hbin_get_tdr(regf, tmp_vk_offset, private_data,
1698                                           (tdr_pull_fn_t)tdr_pull_vk_block,
1699                                           &vk)) {
1700                                 DEBUG(0, ("Unable to get VK block at %d\n",
1701                                         tmp_vk_offset));
1702                                 return WERR_GENERAL_FAILURE;
1703                         }
1704                         if (strcmp(vk.data_name, name) == 0) {
1705                                 old_vk_offset = tmp_vk_offset;
1706                                 break;
1707                         }
1708                 }
1709                 /* Free data, if any */
1710                 if (!(vk.data_length & 0x80000000)) {
1711                         hbin_free(regf, vk.data_offset);
1712                 }
1713         }
1714         if (old_vk_offset == -1) {
1715                 vk.header = "vk";
1716                 vk.name_length = strlen(name);
1717                 if (name != NULL && name[0] != 0) {
1718                         vk.flag = 1;
1719                         vk.data_name = name;
1720                 } else {
1721                         vk.data_name = NULL;
1722                         vk.flag = 0;
1723                 }
1724         }
1725         /* Set the type and data */
1726         vk.data_length = data.length;
1727         vk.data_type = type;
1728         if (type == REG_DWORD) {
1729                 vk.data_length |= 0x80000000;
1730                 vk.data_offset = *(uint32_t *)data.data;
1731         } else {
1732                 /* Store data somewhere */
1733                 vk.data_offset = hbin_store(regf, data);
1734         }
1735         if (old_vk_offset == -1) {
1736                 /* Store new vk */
1737                 vk_offset = hbin_store_tdr(regf,
1738                                            (tdr_push_fn_t) tdr_push_vk_block,
1739                                            &vk);
1740         } else {
1741                 /* Store vk at offset */
1742                 vk_offset = hbin_store_tdr_resize(regf,
1743                                                   (tdr_push_fn_t) tdr_push_vk_block,
1744                                                   old_vk_offset ,&vk);
1745         }
1746
1747         /* Re-allocate the value list */
1748         if (nk->values_offset == -1) {
1749                 nk->values_offset = hbin_store_tdr(regf,
1750                                                    (tdr_push_fn_t) tdr_push_uint32,
1751                                                    &vk_offset);
1752                 nk->num_values = 1;
1753         } else {
1754
1755                 /* Change if we're changing, otherwise we're adding the value */
1756                 if (old_vk_offset != -1) {
1757                         /* Find and overwrite the offset. */
1758                         for (i = 0; i < nk->num_values; i++) {
1759                                 if (IVAL(values.data, i * 4) == old_vk_offset) {
1760                                         SIVAL(values.data, i * 4, vk_offset);
1761                                         break;
1762                                 }
1763                         }
1764                 } else {
1765                         /* Create a new value list */
1766                         DATA_BLOB value_list;
1767
1768                         value_list.length = (nk->num_values+1)*4;
1769                         value_list.data = (uint8_t *)talloc_array(private_data,
1770                                                                   uint32_t,
1771                                                                   nk->num_values+1);
1772                         W_ERROR_HAVE_NO_MEMORY(value_list.data);
1773                         memcpy(value_list.data, values.data, nk->num_values * 4);
1774
1775                         SIVAL(value_list.data, nk->num_values * 4, vk_offset);
1776                         nk->num_values++;
1777                         nk->values_offset = hbin_store_resize(regf,
1778                                                               nk->values_offset,
1779                                                               value_list);
1780                 }
1781
1782         }
1783         hbin_store_tdr_resize(regf,
1784                               (tdr_push_fn_t) tdr_push_nk_block,
1785                               private_data->offset, nk);
1786         return regf_save_hbin(private_data->hive);
1787 }
1788
1789 static WERROR regf_save_hbin(struct regf_data *regf)
1790 {
1791         struct tdr_push *push = talloc_zero(regf, struct tdr_push);
1792         int i;
1793
1794         W_ERROR_HAVE_NO_MEMORY(push);
1795
1796         if (lseek(regf->fd, 0, SEEK_SET) == -1) {
1797                 DEBUG(0, ("Error lseeking in regf file\n"));
1798                 return WERR_GENERAL_FAILURE;
1799         }
1800
1801         /* Recompute checksum */
1802         if (NT_STATUS_IS_ERR(tdr_push_regf_hdr(push, regf->header))) {
1803                 DEBUG(0, ("Failed to push regf header\n"));
1804                 return WERR_GENERAL_FAILURE;
1805         }
1806         regf->header->chksum = regf_hdr_checksum(push->data.data);
1807         talloc_free(push);
1808
1809         if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
1810                                             (tdr_push_fn_t)tdr_push_regf_hdr,
1811                                             regf->header))) {
1812                 DEBUG(0, ("Error writing registry file header\n"));
1813                 return WERR_GENERAL_FAILURE;
1814         }
1815
1816         if (lseek(regf->fd, 0x1000, SEEK_SET) == -1) {
1817                 DEBUG(0, ("Error lseeking to 0x1000 in regf file\n"));
1818                 return WERR_GENERAL_FAILURE;
1819         }
1820
1821         for (i = 0; regf->hbins[i]; i++) {
1822                 if (NT_STATUS_IS_ERR(tdr_push_to_fd(regf->fd,
1823                                                     (tdr_push_fn_t)tdr_push_hbin_block,
1824                                                     regf->hbins[i]))) {
1825                         DEBUG(0, ("Error writing HBIN block\n"));
1826                         return WERR_GENERAL_FAILURE;
1827                 }
1828         }
1829
1830         return WERR_OK;
1831 }
1832
1833 WERROR reg_create_regf_file(TALLOC_CTX *parent_ctx, const char *location,
1834                             int minor_version, struct hive_key **key)
1835 {
1836         struct regf_data *regf;
1837         struct regf_hdr *regf_hdr;
1838         struct tdr_pull pull;
1839         int i;
1840         struct nk_block nk;
1841         WERROR error;
1842
1843         regf = (struct regf_data *)talloc_zero(NULL, struct regf_data);
1844
1845         W_ERROR_HAVE_NO_MEMORY(regf);
1846
1847         DEBUG(5, ("Attempting to create registry file\n"));
1848
1849         /* Get the header */
1850         regf->fd = creat(location, 0644);
1851
1852         if (regf->fd == -1) {
1853                 DEBUG(0,("Could not create file: %s, %s\n", location,
1854                                  strerror(errno)));
1855                 talloc_free(regf);
1856                 return WERR_GENERAL_FAILURE;
1857         }
1858
1859         regf_hdr = talloc_zero(regf, struct regf_hdr);
1860         W_ERROR_HAVE_NO_MEMORY(regf_hdr);
1861         regf_hdr->REGF_ID = "regf";
1862         unix_to_nt_time(&regf_hdr->modtime, time(NULL));
1863         regf_hdr->version.major = 1;
1864         regf_hdr->version.minor = minor_version;
1865         regf_hdr->last_block = 0x1000; /* Block size */
1866         regf_hdr->description = talloc_strdup(regf_hdr,
1867                                               "registry created by Samba 4");
1868         W_ERROR_HAVE_NO_MEMORY(regf_hdr->description);
1869         regf_hdr->chksum = 0;
1870
1871         regf->header = regf_hdr;
1872
1873         pull.offset = 0x1000;
1874
1875         i = 0;
1876         /* Create all hbin blocks */
1877         regf->hbins = talloc_array(regf, struct hbin_block *, 1);
1878         W_ERROR_HAVE_NO_MEMORY(regf->hbins);
1879         regf->hbins[0] = NULL;
1880
1881         regf_hdr->data_offset = -1; /* FIXME */
1882
1883         nk.header = "nk";
1884         nk.type = REG_SUB_KEY;
1885         unix_to_nt_time(&nk.last_change, time(NULL));
1886         nk.uk1 = 0;
1887         nk.parent_offset = -1;
1888         nk.num_subkeys = 0;
1889         nk.uk2 = 0;
1890         nk.subkeys_offset = -1;
1891         nk.unknown_offset = -1;
1892         nk.num_values = 0;
1893         nk.values_offset = -1;
1894         memset(nk.unk3, 0, 5);
1895         nk.clsname_offset = -1; /* FIXME: fill in */
1896         nk.clsname_length = 0;
1897         nk.key_name = "";
1898
1899         nk.sk_offset = -1; /* FIXME: fill in */
1900
1901         /* Store the new nk key */
1902         regf->header->data_offset = hbin_store_tdr(regf,
1903                                                    (tdr_push_fn_t)tdr_push_nk_block,
1904                                                    &nk);
1905
1906         *key = (struct hive_key *)regf_get_key(parent_ctx, regf,
1907                                                regf->header->data_offset);
1908
1909         /* We can drop our own reference now that *key will have created one */
1910         talloc_free(regf);
1911
1912         error = regf_save_hbin(regf);
1913         if (!W_ERROR_IS_OK(error)) {
1914                 return error;
1915         }
1916
1917         return WERR_OK;
1918 }
1919
1920 WERROR reg_open_regf_file(TALLOC_CTX *parent_ctx,
1921                           const char *location, struct hive_key **key)
1922 {
1923         struct regf_data *regf;
1924         struct regf_hdr *regf_hdr;
1925         struct tdr_pull pull;
1926         int i;
1927
1928         regf = (struct regf_data *)talloc_zero(NULL, struct regf_data);
1929
1930         W_ERROR_HAVE_NO_MEMORY(regf);
1931
1932         DEBUG(5, ("Attempting to load registry file\n"));
1933
1934         /* Get the header */
1935         regf->fd = open(location, O_RDWR);
1936
1937         if (regf->fd == -1) {
1938                 DEBUG(0,("Could not load file: %s, %s\n", location,
1939                                  strerror(errno)));
1940                 talloc_free(regf);
1941                 return WERR_GENERAL_FAILURE;
1942         }
1943
1944         ZERO_STRUCT(pull);
1945         pull.data.data = (uint8_t*)fd_load(regf->fd, &pull.data.length, regf);
1946
1947         if (pull.data.data == NULL) {
1948                 DEBUG(0, ("Error reading data\n"));
1949                 talloc_free(regf);
1950                 return WERR_GENERAL_FAILURE;
1951         }
1952
1953         regf_hdr = talloc(regf, struct regf_hdr);
1954         W_ERROR_HAVE_NO_MEMORY(regf_hdr);
1955
1956         if (NT_STATUS_IS_ERR(tdr_pull_regf_hdr(&pull, regf_hdr, regf_hdr))) {
1957                 talloc_free(regf);
1958                 return WERR_GENERAL_FAILURE;
1959         }
1960
1961         regf->header = regf_hdr;
1962
1963         if (strcmp(regf_hdr->REGF_ID, "regf") != 0) {
1964                 DEBUG(0, ("Unrecognized NT registry header id: %s, %s\n",
1965                         regf_hdr->REGF_ID, location));
1966                 talloc_free(regf);
1967                 return WERR_GENERAL_FAILURE;
1968         }
1969
1970         /* Validate the header ... */
1971         if (regf_hdr_checksum(pull.data.data) != regf_hdr->chksum) {
1972                 DEBUG(0, ("Registry file checksum error: %s: %d,%d\n",
1973                         location, regf_hdr->chksum,
1974                         regf_hdr_checksum(pull.data.data)));
1975                 talloc_free(regf);
1976                 return WERR_GENERAL_FAILURE;
1977         }
1978
1979         pull.offset = 0x1000;
1980
1981         i = 0;
1982         /* Read in all hbin blocks */
1983         regf->hbins = talloc_array(regf, struct hbin_block *, 1);
1984         W_ERROR_HAVE_NO_MEMORY(regf->hbins);
1985
1986         regf->hbins[0] = NULL;
1987
1988         while (pull.offset < pull.data.length &&
1989                pull.offset <= regf->header->last_block) {
1990                 struct hbin_block *hbin = talloc(regf->hbins,
1991                                                  struct hbin_block);
1992
1993                 W_ERROR_HAVE_NO_MEMORY(hbin);
1994
1995                 if (NT_STATUS_IS_ERR(tdr_pull_hbin_block(&pull, hbin, hbin))) {
1996                         DEBUG(0, ("[%d] Error parsing HBIN block\n", i));
1997                         talloc_free(regf);
1998                         return WERR_FOOBAR;
1999                 }
2000
2001                 if (strcmp(hbin->HBIN_ID, "hbin") != 0) {
2002                         DEBUG(0, ("[%d] Expected 'hbin', got '%s'\n",
2003                                 i, hbin->HBIN_ID));
2004                         talloc_free(regf);
2005                         return WERR_FOOBAR;
2006                 }
2007
2008                 regf->hbins[i] = hbin;
2009                 i++;
2010                 regf->hbins = talloc_realloc(regf, regf->hbins,
2011                                              struct hbin_block *, i+2);
2012                 regf->hbins[i] = NULL;
2013         }
2014
2015         DEBUG(1, ("%d HBIN blocks read\n", i));
2016
2017         *key = (struct hive_key *)regf_get_key(parent_ctx, regf,
2018                                                regf->header->data_offset);
2019
2020         /* We can drop our own reference now that *key will have created one */
2021         talloc_free(regf);
2022
2023         return WERR_OK;
2024 }
2025
2026 static struct hive_operations reg_backend_regf = {
2027         .name = "regf",
2028         .get_key_info = regf_get_info,
2029         .enum_key = regf_get_subkey_by_index,
2030         .get_key_by_name = regf_get_subkey_by_name,
2031         .get_value_by_name = regf_get_value_by_name,
2032         .enum_value = regf_get_value,
2033         .get_sec_desc = regf_get_sec_desc,
2034         .set_sec_desc = regf_set_sec_desc,
2035         .add_key = regf_add_key,
2036         .set_value = regf_set_value,
2037         .del_key = regf_del_key,
2038         .delete_value = regf_del_value,
2039 };