2 * Samba Unix/Linux SMB client library
4 * Copyright (C) Gregor Beck 2011
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @brief Check the registry database.
22 * @author Gregor Beck <gb@sernet.de>
26 #include "net_registry_check.h"
29 #include "system/filesys.h"
30 #include "lib/dbwrap/dbwrap.h"
31 #include "lib/dbwrap/dbwrap_open.h"
32 #include "lib/dbwrap/dbwrap_rbt.h"
34 #include "libcli/security/dom_sid.h"
35 #include "libcli/security/secdesc.h"
40 #include "registry/reg_db.h"
41 #include "libcli/registry/util_reg.h"
42 #include "registry/reg_parse_internal.h"
47 + every key has a subkeylist
48 + every key is referenced by the subkeylist of its parent
50 + starts with valid hive
51 + UTF-8 (option to convert ???)
55 + REG_DWORD has size 4
56 + REG_QWORD has size 8
57 + STRINGS are zero terminated UTF-16
71 struct regkey
*parent
;
73 struct regkey
**subkeys
;
75 struct regval
**values
;
76 struct security_descriptor
*sd
;
81 struct check_options opt
;
85 struct db_context
*idb
;
86 struct db_context
*odb
;
88 struct regkey
*root
; /*dummy key to hold all basekeys*/
89 struct db_context
*reg
;
90 struct db_context
*del
;
97 static void* talloc_array_append(void *mem_ctx
, void* array
[], void *ptr
)
99 size_t size
= array
? talloc_array_length(array
) : 1;
100 void **tmp
= talloc_realloc(mem_ctx
, array
, void*, size
+ 1);
110 static void regkey_add_subkey(struct regkey
*key
, struct regkey
*subkey
)
112 key
->subkeys
= (struct regkey
**)
113 talloc_array_append(key
, (void**)key
->subkeys
, subkey
);
114 if (key
->subkeys
!= NULL
) {
119 static struct regval
* regval_copy(TALLOC_CTX
*mem_ctx
, const struct regval
*val
)
121 struct regval
*ret
= talloc_zero(mem_ctx
, struct regval
);
126 ret
->name
= talloc_strdup(ret
, val
->name
);
127 if (ret
->name
== NULL
) {
131 ret
->data
= data_blob_dup_talloc(ret
, val
->data
);
132 if (ret
->data
.data
== NULL
) {
136 ret
->type
= val
->type
;
144 static void regkey_add_regval(struct regkey
*key
, struct regval
*val
)
146 key
->values
= (struct regval
**)
147 talloc_array_append(key
, (void**)key
->values
, val
);
148 if (key
->values
!= NULL
) {
153 static bool tdb_data_read_uint32(TDB_DATA
*buf
, uint32_t *result
)
155 const size_t len
= sizeof(uint32_t);
156 if (buf
->dsize
>= len
) {
157 *result
= IVAL(buf
->dptr
, 0);
165 static bool tdb_data_read_cstr(TDB_DATA
*buf
, char **result
)
167 const size_t len
= strnlen((char*)buf
->dptr
, buf
->dsize
) + 1;
168 if (buf
->dsize
>= len
) {
169 *result
= (char*)buf
->dptr
;
177 static bool tdb_data_read_blob(TDB_DATA
*buf
, DATA_BLOB
*result
)
181 if (!tdb_data_read_uint32(&tmp
, &len
)) {
184 if (tmp
.dsize
>= len
) {
186 result
->data
= tmp
.dptr
;
187 result
->length
= len
;
195 static bool tdb_data_read_regval(TDB_DATA
*buf
, struct regval
*result
)
199 if (!tdb_data_read_cstr(&tmp
, &value
.name
)
200 || !tdb_data_read_uint32(&tmp
, &value
.type
)
201 || !tdb_data_read_blob(&tmp
, &value
.data
))
210 static bool tdb_data_is_cstr(TDB_DATA d
) {
211 if (tdb_data_is_empty(d
) || (d
.dptr
[d
.dsize
-1] != '\0')) {
214 return strlen((char *)d
.dptr
) == d
.dsize
-1;
217 static TDB_DATA
cbuf_make_tdb_data(cbuf
*b
)
219 return make_tdb_data((void*)cbuf_gets(b
, 0), cbuf_getpos(b
));
222 static void remove_all(char *str
, char c
)
235 static char* parent_path(const char *path
, char sep
)
237 const char *p
= strrchr(path
, sep
);
238 return p
? talloc_strndup(talloc_tos(), path
, p
-path
) : NULL
;
241 /* return the regkey corresponding to path, create if not yet existing */
242 static struct regkey
*
243 check_ctx_lookup_key(struct check_ctx
*ctx
, const char *path
) {
244 struct regkey
*ret
= NULL
;
246 TDB_DATA val
= tdb_null
;
252 status
= dbwrap_fetch(ctx
->reg
, ctx
, string_term_tdb_data(path
), &val
);
253 if (NT_STATUS_IS_OK(status
)) {
254 if (ctx
->opt
.verbose
) {
255 printf("Open: %s\n", path
);
257 ret
= *(struct regkey
**)val
.dptr
;
258 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
259 /* not yet existing, create */
261 if (ctx
->opt
.verbose
) {
262 printf("New: %s\n", path
);
264 ret
= talloc_zero(ctx
, struct regkey
);
266 DEBUG(0, ("Out of memory!\n"));
269 ret
->path
= talloc_strdup(ret
, path
);
271 pp
= parent_path(path
, ctx
->sep
);
272 ret
->parent
= check_ctx_lookup_key(ctx
, pp
);
273 regkey_add_subkey(ret
->parent
, ret
);
276 /* the dummy root key has no subkeylist so set the name */
277 if (ret
->parent
== ctx
->root
) {
278 ret
->name
= talloc_strdup(ret
, path
);
281 dbwrap_store(ctx
->reg
, string_term_tdb_data(path
),
282 make_tdb_data((void*)&ret
, sizeof(ret
)), 0);
284 DEBUG(0, ("lookup key: failed to fetch %s: %s\n", path
,
288 talloc_free(val
.dptr
);
292 static struct check_ctx
* check_ctx_create(TALLOC_CTX
*mem_ctx
, const char *db
,
293 const struct check_options
*opt
)
295 struct check_ctx
*ctx
= talloc_zero(mem_ctx
, struct check_ctx
);
298 ctx
->reg
= db_open_rbt(ctx
);
299 ctx
->del
= db_open_rbt(ctx
);
300 ctx
->root
= talloc_zero(ctx
, struct regkey
);
301 ctx
->fname
= talloc_strdup(ctx
, db
);
303 if (opt
->automatic
&& (opt
->output
== NULL
)) {
304 ctx
->opt
.repair
= true;
305 ctx
->opt
.output
= ctx
->fname
;
310 d_fprintf(stderr
, "You can not specify --output "
314 ctx
->opt
.output
= ctx
->fname
;
318 ctx
->default_action
= 'r';
325 static bool check_ctx_open_output(struct check_ctx
*ctx
)
327 int oflags
= O_RDWR
| O_CREAT
;
329 if (ctx
->opt
.output
== NULL
) {
333 if (!ctx
->opt
.repair
) {
334 if (!ctx
->opt
.wipe
) {
337 ctx
->opt
.wipe
= true;
340 ctx
->odb
= db_open(ctx
, ctx
->opt
.output
, 0, TDB_DEFAULT
, oflags
, 0644,
341 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
342 if (ctx
->odb
== NULL
) {
344 _("Could not open db (%s) for writing: %s\n"),
345 ctx
->opt
.output
, strerror(errno
));
352 static bool check_ctx_open_input(struct check_ctx
*ctx
) {
353 ctx
->idb
= db_open(ctx
, ctx
->fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0,
354 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
355 if (ctx
->idb
== NULL
) {
357 _("Could not open db (%s) for reading: %s\n"),
358 ctx
->fname
, strerror(errno
));
364 static bool check_ctx_transaction_start(struct check_ctx
*ctx
) {
365 if (ctx
->odb
== NULL
) {
368 if (dbwrap_transaction_start(ctx
->odb
) != 0) {
369 DEBUG(0, ("transaction_start failed\n"));
372 ctx
->transaction
= true;
376 static void check_ctx_transaction_stop(struct check_ctx
*ctx
, bool ok
) {
377 if (!ctx
->transaction
) {
380 if (!ctx
->opt
.test
&& ok
) {
381 d_printf("Committing changes\n");
382 if (dbwrap_transaction_commit(ctx
->odb
) != 0) {
383 DEBUG(0, ("transaction_commit failed\n"));
386 d_printf("Discarding changes\n");
387 dbwrap_transaction_cancel(ctx
->odb
);
391 static bool read_info(struct check_ctx
*ctx
, const char *key
, TDB_DATA val
)
393 if (val
.dsize
==sizeof(uint32_t) && strcmp(key
, "version")==0) {
394 uint32_t v
= IVAL(val
.dptr
, 0);
395 printf("INFO: %s = %d\n", key
, v
);
398 printf("INFO: %s = <invalid>\n", key
);
402 static bool is_all_upper(const char *str
) {
404 char *tmp
= talloc_strdup(talloc_tos(), str
);
405 if (!strupper_m(tmp
)) {
409 ret
= (strcmp(tmp
, str
) == 0);
414 static void move_to_back(struct regkey
*key
, struct regkey
*subkey
)
419 DEBUG(5, ("Move to back subkey \"%s\" of \"%s\"\n",
420 subkey
->path
, key
->path
));
422 for (ptr
=key
->subkeys
; *ptr
!= subkey
; ptr
++)
425 nidx
= ptr
+ 1 - key
->subkeys
;
426 memmove(ptr
, ptr
+1, (key
->nsubkeys
- nidx
) * sizeof(*ptr
));
428 key
->subkeys
[key
->nsubkeys
-1] = subkey
;
431 static void set_subkey_name(struct check_ctx
*ctx
, struct regkey
*key
,
432 const char *name
, int nlen
)
434 char *path
= key
->path
;
435 TALLOC_CTX
*mem_ctx
= talloc_new(talloc_tos());
437 struct regkey
*subkey
;
438 char *nname
= talloc_strndup(mem_ctx
, name
, nlen
);
439 remove_all(nname
, ctx
->sep
);
441 if (strncmp(name
, nname
, nlen
) != 0) {
442 /* XXX interaction: delete/edit */
443 printf("Warning: invalid name: \"%s\" replace with \"%s\"\n",
445 key
->needs_update
= true;
447 p
= talloc_asprintf_strupper_m(mem_ctx
, "%s%c%s",
448 path
, ctx
->sep
, nname
);
449 subkey
= check_ctx_lookup_key(ctx
, p
);
451 bool do_replace
= false;
453 if (strcmp(subkey
->name
, nname
) != 0) {
457 if (is_all_upper(nname
)) {
458 default_action
= 'o';
460 default_action
= 'n';
463 printf("Conflicting subkey names of [%s]: "
464 "old: \"%s\", new: \"%s\"\n",
465 key
->path
, subkey
->name
, nname
);
467 if (ctx
->opt
.output
== NULL
|| ctx
->opt
.automatic
) {
468 action
= default_action
;
471 action
= interact_prompt(
472 "choose spelling [o]ld, [n]ew,"
476 printf("Sorry, edit is not yet "
477 "implemented here...\n");
479 } while (action
== 'e');
488 if (ctx
->opt
.verbose
) {
489 printf("Replacing name: %s: \"%s\""
490 " -> \"%s\"\n", path
,
491 subkey
->name
, nname
);
493 TALLOC_FREE(subkey
->name
);
494 subkey
->name
= talloc_steal(subkey
, nname
);
495 key
->needs_update
= true;
498 if (ctx
->opt
.verbose
) {
499 printf("Set name: %s: \"%s\"\n", path
, nname
);
501 subkey
->name
= talloc_steal(subkey
, nname
);
504 move_to_back(key
, subkey
);
505 TALLOC_FREE(mem_ctx
);
509 read_subkeys(struct check_ctx
*ctx
, const char *path
, TDB_DATA val
, bool update
)
511 uint32_t num_items
, found_items
= 0;
513 struct regkey
*key
= check_ctx_lookup_key(ctx
, path
);
515 key
->needs_update
|= update
;
517 /* printf("SUBKEYS: %s\n", path); */
518 if (key
->has_subkeylist
) {
519 printf("Duplicate subkeylist \"%s\"\n",
521 found_items
= key
->nsubkeys
;
524 /* exists as defined by regdb_key_exists() */
525 key
->has_subkeylist
= true;
527 /* name is set if a key is referenced by the */
528 /* subkeylist of its parent. */
530 if (!tdb_data_read_uint32(&val
, &num_items
) ) {
531 printf("Invalid subkeylist: \"%s\"\n", path
);
535 while (tdb_data_read_cstr(&val
, &subkey
)) {
536 /* printf(" SUBKEY: %s\n", subkey); */
537 set_subkey_name(ctx
, key
, subkey
, strlen(subkey
));
541 if (val
.dsize
!= 0) {
542 printf("Subkeylist of \"%s\": trailing: \"%.*s\"\n",
543 path
, (int)val
.dsize
, val
.dptr
);
544 /* ask: best effort, delete or edit?*/
545 set_subkey_name(ctx
, key
, (char*)val
.dptr
, val
.dsize
);
547 key
->needs_update
= true;
550 if (num_items
!= found_items
) {
551 printf("Subkeylist of \"%s\": invalid number of subkeys, "
552 "expected: %d got: %d\n", path
, num_items
, found_items
);
553 key
->needs_update
= true;
558 static void read_values(struct check_ctx
*ctx
, const char *path
, TDB_DATA val
)
560 struct regkey
*key
= check_ctx_lookup_key(ctx
, path
);
561 uint32_t num_items
, found_items
;
564 /* printf("VALUES: %s\n", path); */
566 if (!tdb_data_read_uint32(&val
, &num_items
) ) {
567 printf("Invalid valuelist: \"%s\"\n", path
);
572 while (tdb_data_read_regval(&val
, &value
)) {
573 /* printf(" VAL: %s type: %s(%d) length: %d\n", value.name, */
574 /* str_regtype(value.type), value.type, */
575 /* (int)value.data.length); */
576 regkey_add_regval(key
, regval_copy(key
, &value
));
580 if (num_items
!= found_items
) {
581 printf("Valuelist of \"%s\": invalid number of values, "
582 "expected: %d got: %d\n", path
, num_items
, found_items
);
583 key
->needs_update
= true;
586 if (val
.dsize
!= 0) {
587 printf("Valuelist of \"%s\": trailing: \"%*s\"\n", path
,
588 (int)val
.dsize
, val
.dptr
);
589 key
->needs_update
= true;
590 /* XXX best effort ??? */
591 /* ZERO_STRUCT(value); */
592 /* if (tdb_data_read_cstr(&val, &value.name) */
593 /* && tdb_data_read_uint32(&val, &value.type)) */
595 /* uint32_t len = -1; */
596 /* tdb_data_read_uint32(&val, &len); */
598 /* found_items ++; */
599 /* regkey_add_regval(key, regval_copy(key, value)); */
602 if (found_items
== 0) {
603 printf("Valuelist of \"%s\" empty\n", path
);
604 key
->needs_update
= true;
608 static bool read_sorted(struct check_ctx
*ctx
, const char *path
, TDB_DATA val
)
610 if (ctx
->version
>= 3) {
614 if ((val
.dptr
== NULL
) || (val
.dsize
<4)) {
619 /* struct regkey *key = check_ctx_lookup_key(ctx, path); */
620 /* printf("SORTED: %s\n", path); */
624 static bool read_sd(struct check_ctx
*ctx
, const char *path
, TDB_DATA val
)
627 struct regkey
*key
= check_ctx_lookup_key(ctx
, path
);
628 /* printf("SD: %s\n", path); */
630 status
= unmarshall_sec_desc(key
, val
.dptr
, val
.dsize
, &key
->sd
);
631 if (!NT_STATUS_IS_OK(status
)) {
632 DEBUG(0, ("Failed to read SD of %s: %s\n",
633 path
, nt_errstr(status
)));
638 static bool srprs_path(const char **ptr
, const char* prefix
, char sep
,
641 const char *path
, *pos
= *ptr
;
642 if (prefix
!= NULL
) {
643 if (!srprs_str(&pos
, prefix
, -1) || !srprs_char(&pos
, sep
) ) {
648 if ( !srprs_hive(&pos
, NULL
) ) {
651 if ( !srprs_eos(&pos
) && !srprs_char(&pos
, sep
) ) {
655 *ptr
= strchr(pos
, '\0');
659 /* Fixme: this doesn't work in the general multibyte char case.
662 static bool normalize_path_internal(char* path
, char sep
) {
663 size_t len
= strlen(path
);
664 const char *orig
= talloc_strndup(talloc_tos(), path
, len
);
665 char *optr
= path
, *iptr
= path
;
668 while (*iptr
== sep
) {
674 while (*iptr
== sep
) {
687 if (!strupper_m(path
)) {
688 talloc_free(discard_const(orig
));
691 changed
= (strcmp(orig
, path
) != 0);
692 talloc_free(discard_const(orig
));
696 static bool normalize_path(char* path
, char sep
) {
697 static const char* SEPS
= "\\/";
698 char* firstsep
= strpbrk(path
, SEPS
);
699 bool wrong_sep
= (firstsep
&& (*firstsep
!= sep
));
701 assert (strchr(SEPS
, sep
));
704 string_replace(path
, *firstsep
, sep
);
706 return normalize_path_internal(path
, sep
) || wrong_sep
;
709 static int check_tdb_action(struct db_record
*rec
, void *check_ctx
)
711 struct check_ctx
*ctx
= (struct check_ctx
*)check_ctx
;
712 TALLOC_CTX
*frame
= talloc_stackframe();
713 TDB_DATA val
= dbwrap_record_get_value(rec
);
714 TDB_DATA rec_key
= dbwrap_record_get_key(rec
);
716 bool invalid_path
= false;
718 bool first_iter
= true;
720 if (!tdb_data_is_cstr(rec_key
)) {
721 printf("Key is not zero terminated: \"%.*s\"\ntry to go on.\n",
722 (int)rec_key
.dsize
, rec_key
.dptr
);
725 key
= talloc_strndup(frame
, (char*)rec_key
.dptr
, rec_key
.dsize
);
728 const char *path
, *pos
= key
;
731 if (srprs_str(&pos
, "INFO/", -1)) {
732 if ( read_info(ctx
, pos
, val
) ) {
736 /* ask: mark invalid */
737 } else if (srprs_str(&pos
, "__db_sequence_number__", -1)) {
738 printf("Skip key: \"%.*s\"\n",
739 (int)rec_key
.dsize
, rec_key
.dptr
);
740 /* skip: do nothing + break */
743 } else if (normalize_path(key
, ctx
->sep
)) {
744 printf("Unnormal key: \"%.*s\"\n",
745 (int)rec_key
.dsize
, rec_key
.dptr
);
746 printf("Normalize to: \"%s\"\n", key
);
748 } else if (srprs_path(&pos
, NULL
,
751 read_subkeys(ctx
, path
, val
, invalid_path
);
753 } else if (srprs_path(&pos
, REG_VALUE_PREFIX
,
756 read_values(ctx
, path
, val
);
758 } else if (srprs_path(&pos
, REG_SECDESC_PREFIX
,
761 read_sd(ctx
, path
, val
);
763 } else if (srprs_path(&pos
, REG_SORTED_SUBKEYS_PREFIX
,
766 if (!read_sorted(ctx
, path
, val
)) {
767 /* delete: mark invalid + break */
768 printf("Invalid sorted subkeys for: \"%s\"\n", path
);
774 printf("Unrecognized key: \"%.*s\"\n",
775 (int)rec_key
.dsize
, rec_key
.dptr
);
780 unsigned char action
;
781 if (ctx
->opt
.output
== NULL
) {
782 action
= first_iter
? 'r' : 's';
783 } else if (ctx
->opt
.automatic
) {
784 action
= first_iter
? 'r' : 'd';
785 } else if (ctx
->auto_action
!= '\0') {
786 action
= ctx
->auto_action
;
788 action
= interact_prompt("[s]kip,[S]kip all,"
789 "[d]elete,[D]elete all"
792 ctx
->default_action
);
794 if (isupper(action
)) {
795 action
= tolower(action
);
796 ctx
->auto_action
= action
;
798 ctx
->default_action
= action
;
801 invalid_path
= false;
803 case 'd': /* delete */
807 case 'e': /* edit */ {
808 char *p
= interact_edit(frame
, key
);
815 case 'r': /* retry */
824 dbwrap_store(ctx
->del
, rec_key
, string_term_tdb_data(key
), 0);
831 static bool get_version(struct check_ctx
*ctx
) {
832 static const uint32_t curr_version
= REGDB_CODE_VERSION
;
833 uint32_t version
= ctx
->opt
.version
? ctx
->opt
.version
: curr_version
;
834 uint32_t info_version
= 0;
837 status
= dbwrap_fetch_uint32_bystring(ctx
->idb
, "INFO/version",
839 if (!NT_STATUS_IS_OK(status
)) {
840 printf("Warning: no INFO/version found!\n");
841 /* info_version = guess_version(ctx); */
844 if (ctx
->opt
.version
) {
845 version
= ctx
->opt
.version
;
846 } else if (ctx
->opt
.implicit_db
) {
847 version
= curr_version
;
849 version
= info_version
;
853 printf("Couldn't determine registry format version, "
854 "specify with --reg-version\n");
859 if ( version
!= info_version
) {
860 if (ctx
->opt
.force
|| !ctx
->opt
.repair
) {
861 printf("Warning: overwrite registry format "
862 "version %d with %d\n", info_version
, version
);
864 printf("Warning: found registry format version %d but "
865 "expected %d, use --force to proceed.\n", info_version
, version
);
870 ctx
->version
= version
;
871 ctx
->sep
= (version
> 1) ? '\\' : '/';
877 dbwrap_store_verbose(struct db_context
*db
, const char *key
, TDB_DATA nval
)
879 TALLOC_CTX
*mem_ctx
= talloc_new(talloc_tos());
883 status
= dbwrap_fetch_bystring(db
, mem_ctx
, key
, &oval
);
884 if (NT_STATUS_IS_OK(status
)) {
885 if (tdb_data_equal(nval
, oval
)) {
888 printf("store %s:\n overwrite: %s\n with: %s\n", key
,
889 tdb_data_string(mem_ctx
, oval
),
890 tdb_data_string(mem_ctx
, nval
));
892 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
893 printf("store %s:\n write: %s\n", key
,
894 tdb_data_string(mem_ctx
, nval
));
896 printf ("store %s:\n failed to fetch old value: %s\n", key
,
901 status
= dbwrap_store_bystring(db
, key
, nval
, 0);
902 if (!NT_STATUS_IS_OK(status
)) {
903 printf ("store %s failed: %s\n", key
, nt_errstr(status
));
907 talloc_free(mem_ctx
);
908 return NT_STATUS_IS_OK(status
);
912 dbwrap_store_uint32_verbose(struct db_context
*db
, const char *key
, uint32_t nval
)
917 status
= dbwrap_fetch_uint32_bystring(db
, key
, &oval
);
918 if (NT_STATUS_IS_OK(status
)) {
922 printf("store %s:\n overwrite: %d\n with: %d\n", key
,
923 (int)oval
, (int)nval
);
925 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
926 printf("store %s:\n write: %d\n", key
, (int)nval
);
928 printf ("store %s:\n failed to fetch old value: %s\n", key
,
933 status
= dbwrap_store_uint32_bystring(db
, key
, nval
);
934 if (!NT_STATUS_IS_OK(status
)) {
935 printf ("store %s failed: %s\n", key
, nt_errstr(status
));
939 return NT_STATUS_IS_OK(status
);
942 static int cmp_keynames(char **p1
, char **p2
)
944 return strcasecmp_m(*p1
, *p2
);
948 write_subkeylist(struct db_context
*db
, struct regkey
*key
, char sep
)
950 cbuf
*buf
= cbuf_new(talloc_tos());
954 cbuf_putdw(buf
, key
->nsubkeys
);
956 for (i
=0; i
< key
->nsubkeys
; i
++) {
957 struct regkey
*subkey
= key
->subkeys
[i
];
958 const char *name
= subkey
->name
;
960 printf("Warning: no explicit name for key %s\n",
962 name
= strrchr_m(subkey
->path
, sep
);
966 cbuf_puts(buf
, name
, -1);
967 cbuf_putc(buf
, '\0');
970 ret
= dbwrap_store_verbose(db
, key
->path
, cbuf_make_tdb_data(buf
));
976 static bool write_sorted(struct db_context
*db
, struct regkey
*key
, char sep
)
978 cbuf
*buf
= cbuf_new(talloc_tos());
982 char **sorted
= talloc_zero_array(buf
, char*, key
->nsubkeys
);
983 int offset
= (1 + key
->nsubkeys
) * sizeof(uint32_t);
985 for (i
=0; i
< key
->nsubkeys
; i
++) {
986 sorted
[i
] = talloc_strdup_upper(sorted
, key
->subkeys
[i
]->name
);
988 TYPESAFE_QSORT(sorted
, key
->nsubkeys
, cmp_keynames
);
990 cbuf_putdw(buf
, key
->nsubkeys
);
991 for (i
=0; i
< key
->nsubkeys
; i
++) {
992 cbuf_putdw(buf
, offset
);
993 offset
+= strlen(sorted
[i
]) + 1;
995 for (i
=0; i
< key
->nsubkeys
; i
++) {
996 cbuf_puts(buf
, sorted
[i
], -1);
997 cbuf_putc(buf
, '\0');
1000 path
= talloc_asprintf(buf
, "%s%c%s", REG_SORTED_SUBKEYS_PREFIX
, sep
,
1003 DEBUG(0, ("Out of memory!\n"));
1007 ret
= dbwrap_store_verbose(db
, path
, cbuf_make_tdb_data(buf
));
1013 static bool write_values(struct db_context
*db
, struct regkey
*key
, char sep
)
1015 cbuf
*buf
= cbuf_new(talloc_tos());
1020 cbuf_putdw(buf
, key
->nvalues
);
1021 for (i
=0; i
< key
->nvalues
; i
++) {
1022 struct regval
*val
= key
->values
[i
];
1023 cbuf_puts(buf
, val
->name
, -1);
1024 cbuf_putc(buf
, '\0');
1025 cbuf_putdw(buf
, val
->type
);
1026 cbuf_putdw(buf
, val
->data
.length
);
1027 cbuf_puts(buf
, (void*)val
->data
.data
, val
->data
.length
);
1030 path
= talloc_asprintf(buf
, "%s%c%s", REG_VALUE_PREFIX
, sep
, key
->path
);
1032 DEBUG(0, ("Out of memory!\n"));
1036 ret
= dbwrap_store_verbose(db
, path
, cbuf_make_tdb_data(buf
));
1042 static bool write_sd(struct db_context
*db
, struct regkey
*key
, char sep
)
1048 TALLOC_CTX
*mem_ctx
= talloc_new(talloc_tos());
1050 status
= marshall_sec_desc(mem_ctx
, key
->sd
, &sd
.dptr
, &sd
.dsize
);
1051 if (!NT_STATUS_IS_OK(status
)) {
1052 printf("marshall sec desc %s failed: %s\n",
1053 key
->path
, nt_errstr(status
));
1056 path
= talloc_asprintf(mem_ctx
, "%s%c%s", REG_SECDESC_PREFIX
,
1059 DEBUG(0, ("Out of memory!\n"));
1063 ret
= dbwrap_store_verbose(db
, path
, sd
);
1065 talloc_free(mem_ctx
);
1070 static int check_write_db_action(struct db_record
*rec
, void *check_ctx
)
1072 struct check_ctx
*ctx
= (struct check_ctx
*)check_ctx
;
1073 TDB_DATA rec_val
= dbwrap_record_get_value(rec
);
1074 struct regkey
*key
= *(struct regkey
**)rec_val
.dptr
;
1075 TALLOC_CTX
*frame
= talloc_stackframe();
1077 /* write subkeylist */
1078 if ((ctx
->version
> 2) || (key
->nsubkeys
> 0) || (key
->has_subkeylist
)) {
1079 write_subkeylist(ctx
->odb
, key
, ctx
->sep
);
1082 /* write sorted subkeys */
1083 if ((ctx
->version
< 3) && (key
->nsubkeys
> 0)) {
1084 write_sorted(ctx
->odb
, key
, ctx
->sep
);
1087 /* write value list */
1088 if (key
->nvalues
> 0) {
1089 write_values(ctx
->odb
, key
, ctx
->sep
);
1094 write_sd(ctx
->odb
, key
, ctx
->sep
);
1101 static int fix_tree_action(struct db_record
*rec
, void *check_ctx
)
1103 struct check_ctx
*ctx
= (struct check_ctx
*)check_ctx
;
1104 TDB_DATA rec_key
= dbwrap_record_get_key(rec
);
1105 TDB_DATA rec_val
= dbwrap_record_get_value(rec
);
1106 struct regkey
* key
= *(struct regkey
**)rec_val
.dptr
;
1107 if (ctx
->opt
.verbose
) {
1108 printf("Check Tree: %s\n", key
->path
);
1111 assert (strncmp(key
->path
, (char*)rec_key
.dptr
, rec_key
.dsize
) == 0);
1113 /* assert(dbwrap_exists(ctx->db, string_term_tdb_data(key->path)) */
1114 /* == key->exists); */
1116 if (key
->needs_update
) {
1117 printf("Update key: \"%s\"\n", key
->path
);
1118 if ((ctx
->version
> 2) || (key
->nsubkeys
> 0)) {
1119 write_subkeylist(ctx
->odb
, key
, ctx
->sep
);
1121 if ((ctx
->version
<= 2) && (key
->nsubkeys
> 0)) {
1122 write_sorted(ctx
->odb
, key
, ctx
->sep
);
1124 if (key
->nvalues
> 0) {
1125 write_values(ctx
->odb
, key
, ctx
->sep
);
1128 write_sd(ctx
->odb
, key
, ctx
->sep
);
1130 } else if (!key
->has_subkeylist
) {
1131 if ((ctx
->version
> 2) || (key
->nsubkeys
> 0)) {
1132 printf("Missing subkeylist: %s\n", key
->path
);
1133 write_subkeylist(ctx
->odb
, key
, ctx
->sep
);
1137 if (key
->name
== NULL
&& key
->parent
->has_subkeylist
) {
1138 printf("Key not referenced by the its parents subkeylist: %s\n",
1140 write_subkeylist(ctx
->odb
, key
->parent
, ctx
->sep
);
1143 /* XXX check that upcase(name) matches last part of path ??? */
1149 /* give the same warnings as fix_tree_action */
1150 static int check_tree_action(struct db_record
*rec
, void *check_ctx
)
1152 struct check_ctx
*ctx
= (struct check_ctx
*)check_ctx
;
1153 TDB_DATA rec_key
= dbwrap_record_get_key(rec
);
1154 TDB_DATA rec_val
= dbwrap_record_get_value(rec
);
1155 struct regkey
* key
= *(struct regkey
**)rec_val
.dptr
;
1156 if (ctx
->opt
.verbose
) {
1157 printf("Check Tree: %s\n", key
->path
);
1160 assert (strncmp(key
->path
, (char*)rec_key
.dptr
, rec_key
.dsize
) == 0);
1162 if (!key
->has_subkeylist
) {
1163 if ((ctx
->version
> 2) || (key
->nsubkeys
> 0)) {
1164 printf("Missing subkeylist: %s\n", key
->path
);
1168 if (key
->name
== NULL
&& key
->parent
->has_subkeylist
) {
1169 printf("Key not referenced by the its parents subkeylist: %s\n",
1176 static int delete_invalid_action(struct db_record
*rec
, void* check_ctx
)
1179 struct check_ctx
*ctx
= (struct check_ctx
*)check_ctx
;
1180 TDB_DATA rec_key
= dbwrap_record_get_key(rec
);
1181 TDB_DATA rec_val
= dbwrap_record_get_value(rec
);
1184 printf("Delete key: \"%.*s\"",(int)rec_key
.dsize
, rec_key
.dptr
);
1185 if (rec_val
.dsize
> 0) {
1186 printf(" in favour of \"%s\"\n", rec_val
.dptr
);
1191 status
= dbwrap_delete(ctx
->odb
, rec_key
);
1192 if (!NT_STATUS_IS_OK(status
)) {
1193 d_printf("delete key \"%.*s\" failed!\n",
1194 (int)rec_key
.dsize
, rec_key
.dptr
);
1200 static bool check_ctx_check_tree(struct check_ctx
*ctx
) {
1203 status
= dbwrap_traverse(ctx
->reg
, check_tree_action
, ctx
, NULL
);
1204 if (!NT_STATUS_IS_OK(status
)) {
1205 DEBUG(0, ("check traverse failed: %s\n",
1206 nt_errstr(status
)));
1211 static bool check_ctx_fix_inplace(struct check_ctx
*ctx
) {
1213 status
= dbwrap_traverse(ctx
->reg
, fix_tree_action
, ctx
, NULL
);
1214 if (!NT_STATUS_IS_OK(status
)) {
1215 DEBUG(0, ("fix traverse failed: %s\n", nt_errstr(status
)));
1219 status
= dbwrap_traverse(ctx
->del
, delete_invalid_action
, ctx
, NULL
);
1220 if (!NT_STATUS_IS_OK(status
)) {
1221 DEBUG(0, ("delete traverse failed: %s\n", nt_errstr(status
)));
1225 if (!dbwrap_store_uint32_verbose(ctx
->odb
, "INFO/version", ctx
->version
)) {
1226 DEBUG(0, ("storing version failed: %s\n", nt_errstr(status
)));
1233 static bool check_ctx_write_new_db(struct check_ctx
*ctx
) {
1238 if (ctx
->opt
.wipe
) {
1239 int ret
= dbwrap_wipe(ctx
->odb
);
1241 DEBUG(0, ("wiping %s failed\n", ctx
->opt
.output
));
1246 status
= dbwrap_traverse(ctx
->reg
, check_write_db_action
, ctx
, NULL
);
1247 if (!NT_STATUS_IS_OK(status
)) {
1248 DEBUG(0, ("traverse2 failed: %s\n", nt_errstr(status
)));
1252 status
= dbwrap_store_uint32_bystring(ctx
->odb
, "INFO/version",
1254 if (!NT_STATUS_IS_OK(status
)) {
1255 DEBUG(0, ("write version failed: %s\n", nt_errstr(status
)));
1261 int net_registry_check_db(const char *name
, const struct check_options
*opt
)
1265 struct check_ctx
*ctx
= check_ctx_create(talloc_tos(), name
, opt
);
1270 d_printf("Check database: %s\n", name
);
1272 /* 1. open output RW */
1273 if (!check_ctx_open_output(ctx
)) {
1277 /* 2. open input RO */
1278 if (!check_ctx_open_input(ctx
)) {
1282 if (opt
->lock
&& !check_ctx_transaction_start(ctx
)) {
1286 if (!get_version(ctx
)) {
1290 status
= dbwrap_traverse_read(ctx
->idb
, check_tdb_action
, ctx
, NULL
);
1291 if (!NT_STATUS_IS_OK(status
)) {
1292 DEBUG(0, ("check traverse failed: %s\n", nt_errstr(status
)));
1296 if (!opt
->lock
&& !check_ctx_transaction_start(ctx
)) {
1300 if (ctx
->opt
.repair
&& !ctx
->opt
.wipe
) {
1301 if (!check_ctx_fix_inplace(ctx
)) {
1305 if (!check_ctx_check_tree(ctx
)) {
1309 if (!check_ctx_write_new_db(ctx
)) {
1316 check_ctx_transaction_stop(ctx
, ret
== 0);
1322 /*Local Variables:*/