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_empty(TDB_DATA d
) {
211 return (d
.dptr
== NULL
) || (d
.dsize
== 0);
214 static bool tdb_data_is_cstr(TDB_DATA d
) {
215 if (tdb_data_is_empty(d
)) {
218 return (d
.dptr
[d
.dsize
-1] == '\0');
221 static char* tdb_data_print(TALLOC_CTX
*mem_ctx
, TDB_DATA d
)
223 if (!tdb_data_is_empty(d
)) {
225 cbuf
*ost
= cbuf_new(mem_ctx
);
226 int len
= cbuf_print_quoted(ost
, (const char*)d
.dptr
, d
.dsize
);
228 cbuf_swapptr(ost
, &ret
, 0);
229 talloc_steal(mem_ctx
, ret
);
234 return talloc_strdup(mem_ctx
, "<NULL>");
238 static TDB_DATA
cbuf_make_tdb_data(cbuf
*b
)
240 return make_tdb_data((void*)cbuf_gets(b
, 0), cbuf_getpos(b
));
243 static void remove_all(char *str
, char c
)
256 static char* parent_path(const char *path
, char sep
)
258 const char *p
= strrchr(path
, sep
);
259 return p
? talloc_strndup(talloc_tos(), path
, p
-path
) : NULL
;
262 /* return the regkey corresponding to path, create if not yet existing */
263 static struct regkey
*
264 check_ctx_lookup_key(struct check_ctx
*ctx
, const char *path
) {
265 struct regkey
*ret
= NULL
;
273 status
= dbwrap_fetch(ctx
->reg
, ctx
, string_term_tdb_data(path
), &val
);
274 if (!NT_STATUS_IS_OK(status
)) {
277 if (val
.dptr
!= NULL
) {
278 if (ctx
->opt
.verbose
) {
279 printf("Open: %s\n", path
);
281 ret
= *(struct regkey
**)val
.dptr
;
283 /* not yet existing, create */
285 if (ctx
->opt
.verbose
) {
286 printf("New: %s\n", path
);
288 ret
= talloc_zero(ctx
, struct regkey
);
290 DEBUG(0, ("Out of memory!\n"));
293 ret
->path
= talloc_strdup(ret
, path
);
295 pp
= parent_path(path
, ctx
->sep
);
296 ret
->parent
= check_ctx_lookup_key(ctx
, pp
);
297 regkey_add_subkey(ret
->parent
, ret
);
300 /* the dummy root key has no subkeylist so set the name */
301 if (ret
->parent
== ctx
->root
) {
302 ret
->name
= talloc_strdup(ret
, path
);
305 dbwrap_store(ctx
->reg
, string_term_tdb_data(path
),
306 make_tdb_data((void*)&ret
, sizeof(ret
)), 0);
309 talloc_free(val
.dptr
);
313 static struct check_ctx
* check_ctx_create(TALLOC_CTX
*mem_ctx
, const char *db
,
314 const struct check_options
*opt
)
316 struct check_ctx
*ctx
= talloc_zero(mem_ctx
, struct check_ctx
);
319 ctx
->reg
= db_open_rbt(ctx
);
320 ctx
->del
= db_open_rbt(ctx
);
321 ctx
->root
= talloc_zero(ctx
, struct regkey
);
322 ctx
->fname
= talloc_strdup(ctx
, db
);
324 if (opt
->automatic
&& (opt
->output
== NULL
)) {
325 ctx
->opt
.repair
= true;
326 ctx
->opt
.output
= ctx
->fname
;
331 d_fprintf(stderr
, "You can not specify --output "
335 ctx
->opt
.output
= ctx
->fname
;
339 ctx
->default_action
= 'r';
346 static bool check_ctx_open_output(struct check_ctx
*ctx
)
348 int oflags
= O_RDWR
| O_CREAT
;
350 if (ctx
->opt
.output
== NULL
) {
354 if (!ctx
->opt
.repair
) {
355 if (!ctx
->opt
.wipe
) {
358 ctx
->opt
.wipe
= true;
361 ctx
->odb
= db_open(ctx
, ctx
->opt
.output
, 0, TDB_DEFAULT
, oflags
, 0644);
362 if (ctx
->odb
== NULL
) {
364 _("Could not open db (%s) for writing: %s\n"),
365 ctx
->opt
.output
, strerror(errno
));
372 static bool check_ctx_open_input(struct check_ctx
*ctx
) {
373 ctx
->idb
= db_open(ctx
, ctx
->fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0);
374 if (ctx
->idb
== NULL
) {
376 _("Could not open db (%s) for reading: %s\n"),
377 ctx
->fname
, strerror(errno
));
383 static bool check_ctx_transaction_start(struct check_ctx
*ctx
) {
384 if (ctx
->odb
== NULL
) {
387 if (dbwrap_transaction_start(ctx
->odb
) != 0) {
388 DEBUG(0, ("transaction_start failed\n"));
391 ctx
->transaction
= true;
395 static void check_ctx_transaction_stop(struct check_ctx
*ctx
, bool ok
) {
396 if (!ctx
->transaction
) {
399 if (!ctx
->opt
.test
&& ok
) {
400 d_printf("Commiting changes\n");
401 if (dbwrap_transaction_commit(ctx
->odb
) != 0) {
402 DEBUG(0, ("transaction_commit failed\n"));
405 d_printf("Discarding changes\n");
406 dbwrap_transaction_cancel(ctx
->odb
);
410 static bool read_info(struct check_ctx
*ctx
, const char *key
, TDB_DATA val
)
412 if (val
.dsize
==sizeof(uint32_t) && strcmp(key
, "version")==0) {
413 uint32_t v
= IVAL(val
.dptr
, 0);
414 printf("INFO: %s = %d\n", key
, v
);
417 printf("INFO: %s = <invalid>\n", key
);
421 static bool is_all_upper(const char *str
) {
423 char *tmp
= talloc_strdup(talloc_tos(), str
);
425 ret
= (strcmp(tmp
, str
) == 0);
430 static void move_to_back(struct regkey
*key
, struct regkey
*subkey
)
435 DEBUG(5, ("Move to back subkey \"%s\" of \"%s\"\n",
436 subkey
->path
, key
->path
));
438 for (ptr
=key
->subkeys
; *ptr
!= subkey
; ptr
++)
441 nidx
= ptr
+ 1 - key
->subkeys
;
442 memmove(ptr
, ptr
+1, (key
->nsubkeys
- nidx
) * sizeof(*ptr
));
444 key
->subkeys
[key
->nsubkeys
-1] = subkey
;
447 static void set_subkey_name(struct check_ctx
*ctx
, struct regkey
*key
,
448 const char *name
, int nlen
)
450 char *path
= key
->path
;
451 TALLOC_CTX
*mem_ctx
= talloc_new(talloc_tos());
453 struct regkey
*subkey
;
454 char *nname
= talloc_strndup(mem_ctx
, name
, nlen
);
455 remove_all(nname
, ctx
->sep
);
457 if (strncmp(name
, nname
, nlen
) != 0) {
458 /* XXX interaction: delete/edit */
459 printf("Warning: invalid name: \"%s\" replace with \"%s\"\n",
461 key
->needs_update
= true;
463 p
= talloc_asprintf_strupper_m(mem_ctx
, "%s%c%s",
464 path
, ctx
->sep
, nname
);
465 subkey
= check_ctx_lookup_key(ctx
, p
);
467 bool do_replace
= false;
469 if (strcmp(subkey
->name
, nname
) != 0) {
473 if (is_all_upper(nname
)) {
474 default_action
= 'o';
476 default_action
= 'n';
479 printf("Conflicting subkey names of [%s]: "
480 "old: \"%s\", new: \"%s\"\n",
481 key
->path
, subkey
->name
, nname
);
483 if (ctx
->opt
.output
== NULL
|| ctx
->opt
.automatic
) {
484 action
= default_action
;
487 action
= interact_prompt(
488 "choose spelling [o]ld, [n]ew,"
492 printf("Sorry, edit is not yet "
493 "implemented here...\n");
495 } while (action
== 'e');
504 if (ctx
->opt
.verbose
) {
505 printf("Replacing name: %s: \"%s\""
506 " -> \"%s\"\n", path
,
507 subkey
->name
, nname
);
509 TALLOC_FREE(subkey
->name
);
510 subkey
->name
= talloc_steal(subkey
, nname
);
511 key
->needs_update
= true;
514 if (ctx
->opt
.verbose
) {
515 printf("Set name: %s: \"%s\"\n", path
, nname
);
517 subkey
->name
= talloc_steal(subkey
, nname
);
520 move_to_back(key
, subkey
);
521 TALLOC_FREE(mem_ctx
);
525 read_subkeys(struct check_ctx
*ctx
, const char *path
, TDB_DATA val
, bool update
)
527 uint32_t num_items
, found_items
= 0;
529 struct regkey
*key
= check_ctx_lookup_key(ctx
, path
);
531 key
->needs_update
|= update
;
533 /* printf("SUBKEYS: %s\n", path); */
534 if (key
->has_subkeylist
) {
535 printf("Duplicate subkeylist \"%s\"\n",
537 found_items
= key
->nsubkeys
;
540 /* exists as defined by regdb_key_exists() */
541 key
->has_subkeylist
= true;
543 /* name is set if a key is referenced by the */
544 /* subkeylist of its parent. */
546 if (!tdb_data_read_uint32(&val
, &num_items
) ) {
547 printf("Invalid subkeylist: \"%s\"\n", path
);
551 while (tdb_data_read_cstr(&val
, &subkey
)) {
552 /* printf(" SUBKEY: %s\n", subkey); */
553 set_subkey_name(ctx
, key
, subkey
, strlen(subkey
));
557 if (val
.dsize
!= 0) {
558 printf("Subkeylist of \"%s\": trailing: \"%.*s\"\n",
559 path
, (int)val
.dsize
, val
.dptr
);
560 /* ask: best effort, delete or edit?*/
561 set_subkey_name(ctx
, key
, (char*)val
.dptr
, val
.dsize
);
563 key
->needs_update
= true;
566 if (num_items
!= found_items
) {
567 printf("Subkeylist of \"%s\": invalid number of subkeys, "
568 "expected: %d got: %d\n", path
, num_items
, found_items
);
569 key
->needs_update
= true;
574 static void read_values(struct check_ctx
*ctx
, const char *path
, TDB_DATA val
)
576 struct regkey
*key
= check_ctx_lookup_key(ctx
, path
);
577 uint32_t num_items
, found_items
;
580 /* printf("VALUES: %s\n", path); */
582 if (!tdb_data_read_uint32(&val
, &num_items
) ) {
583 printf("Invalid valuelist: \"%s\"\n", path
);
588 while (tdb_data_read_regval(&val
, &value
)) {
589 /* printf(" VAL: %s type: %s(%d) length: %d\n", value.name, */
590 /* str_regtype(value.type), value.type, */
591 /* (int)value.data.length); */
592 regkey_add_regval(key
, regval_copy(key
, &value
));
596 if (num_items
!= found_items
) {
597 printf("Valuelist of \"%s\": invalid number of values, "
598 "expected: %d got: %d\n", path
, num_items
, found_items
);
599 key
->needs_update
= true;
602 if (val
.dsize
!= 0) {
603 printf("Valuelist of \"%s\": trailing: \"%*s\"\n", path
,
604 (int)val
.dsize
, val
.dptr
);
605 key
->needs_update
= true;
606 /* XXX best effort ??? */
607 /* ZERO_STRUCT(value); */
608 /* if (tdb_data_read_cstr(&val, &value.name) */
609 /* && tdb_data_read_uint32(&val, &value.type)) */
611 /* uint32_t len = -1; */
612 /* tdb_data_read_uint32(&val, &len); */
614 /* found_items ++; */
615 /* regkey_add_regval(key, regval_copy(key, value)); */
618 if (found_items
== 0) {
619 printf("Valuelist of \"%s\" empty\n", path
);
620 key
->needs_update
= true;
624 static bool read_sorted(struct check_ctx
*ctx
, const char *path
, TDB_DATA val
)
626 if (ctx
->version
>= 3) {
630 if ((val
.dptr
== NULL
) || (val
.dsize
<4)) {
635 /* struct regkey *key = check_ctx_lookup_key(ctx, path); */
636 /* printf("SORTED: %s\n", path); */
640 static bool read_sd(struct check_ctx
*ctx
, const char *path
, TDB_DATA val
)
643 struct regkey
*key
= check_ctx_lookup_key(ctx
, path
);
644 /* printf("SD: %s\n", path); */
646 status
= unmarshall_sec_desc(key
, val
.dptr
, val
.dsize
, &key
->sd
);
647 if (!NT_STATUS_IS_OK(status
)) {
648 DEBUG(0, ("Failed to read SD of %s: %s\n",
649 path
, nt_errstr(status
)));
654 static bool srprs_path(const char **ptr
, const char* prefix
, char sep
,
657 const char *path
, *pos
= *ptr
;
658 if (prefix
!= NULL
) {
659 if (!srprs_str(&pos
, prefix
, -1) || !srprs_char(&pos
, sep
) ) {
664 if ( !srprs_hive(&pos
, NULL
) ) {
667 if ( !srprs_eos(&pos
) && !srprs_char(&pos
, sep
) ) {
671 /* We know pos ends in '\0'. */
672 *ptr
= &pos
[strlen(pos
)];
676 /* Fixme: this dosn't work in the general multibyte char case.
679 static bool normalize_path_internal(char* path
, char sep
) {
680 size_t len
= strlen(path
);
681 const char *orig
= talloc_strndup(talloc_tos(), path
, len
);
682 char *optr
= path
, *iptr
= path
;
685 while (*iptr
== sep
) {
691 while (*iptr
== sep
) {
705 changed
= (strcmp(orig
, path
) != 0);
706 talloc_free(discard_const(orig
));
710 static bool normalize_path(char* path
, char sep
) {
711 static const char* SEPS
= "\\/";
712 char* firstsep
= strpbrk(path
, SEPS
);
713 bool wrong_sep
= (firstsep
&& (*firstsep
!= sep
));
715 assert (strchr(SEPS
, sep
));
718 string_replace(path
, *firstsep
, sep
);
720 return normalize_path_internal(path
, sep
) || wrong_sep
;
723 static int check_tdb_action(struct db_record
*rec
, void *check_ctx
)
725 struct check_ctx
*ctx
= (struct check_ctx
*)check_ctx
;
726 TALLOC_CTX
*frame
= talloc_stackframe();
727 TDB_DATA val
= dbwrap_record_get_value(rec
);
728 TDB_DATA rec_key
= dbwrap_record_get_key(rec
);
730 bool invalid_path
= false;
732 bool first_iter
= true;
734 if (!tdb_data_is_cstr(rec_key
)) {
735 printf("Key is not zero terminated: \"%.*s\"\ntry to go on.\n",
736 (int)rec_key
.dsize
, rec_key
.dptr
);
739 key
= talloc_strndup(frame
, (char*)rec_key
.dptr
, rec_key
.dsize
);
742 const char *path
, *pos
= key
;
745 if (srprs_str(&pos
, "INFO/", -1)) {
746 if ( read_info(ctx
, pos
, val
) ) {
750 /* ask: mark invalid */
751 } else if (srprs_str(&pos
, "__db_sequence_number__", -1)) {
752 printf("Skip key: \"%.*s\"\n",
753 (int)rec_key
.dsize
, rec_key
.dptr
);
754 /* skip: do nothing + break */
757 } else if (normalize_path(key
, ctx
->sep
)) {
758 printf("Unnormal key: \"%.*s\"\n",
759 (int)rec_key
.dsize
, rec_key
.dptr
);
760 printf("Normalize to: \"%s\"\n", key
);
762 } else if (srprs_path(&pos
, NULL
,
765 read_subkeys(ctx
, path
, val
, invalid_path
);
767 } else if (srprs_path(&pos
, REG_VALUE_PREFIX
,
770 read_values(ctx
, path
, val
);
772 } else if (srprs_path(&pos
, REG_SECDESC_PREFIX
,
775 read_sd(ctx
, path
, val
);
777 } else if (srprs_path(&pos
, REG_SORTED_SUBKEYS_PREFIX
,
780 if (!read_sorted(ctx
, path
, val
)) {
781 /* delete: mark invalid + break */
782 printf("Invalid sorted subkeys for: \"%s\"\n", path
);
788 printf("Unrecognized key: \"%.*s\"\n",
789 (int)rec_key
.dsize
, rec_key
.dptr
);
795 if (ctx
->opt
.output
== NULL
) {
796 action
= first_iter
? 'r' : 's';
797 } else if (ctx
->opt
.automatic
) {
798 action
= first_iter
? 'r' : 'd';
799 } else if (ctx
->auto_action
!= '\0') {
800 action
= ctx
->auto_action
;
802 action
= interact_prompt("[s]kip,[S]kip all,"
803 "[d]elete,[D]elete all"
806 ctx
->default_action
);
808 if (isupper(action
)) {
809 action
= tolower(action
);
810 ctx
->auto_action
= action
;
812 ctx
->default_action
= action
;
815 invalid_path
= false;
817 case 'd': /* delete */
821 case 'e': /* edit */ {
822 char *p
= interact_edit(frame
, key
);
828 case 'r': /* retry */
837 dbwrap_store(ctx
->del
, rec_key
, string_term_tdb_data(key
), 0);
844 static bool get_version(struct check_ctx
*ctx
) {
845 static const uint32_t curr_version
= REGDB_CODE_VERSION
;
846 uint32_t version
= ctx
->opt
.version
? ctx
->opt
.version
: curr_version
;
847 uint32_t info_version
= 0;
850 status
= dbwrap_fetch_uint32(ctx
->idb
, "INFO/version", &info_version
);
851 if (!NT_STATUS_IS_OK(status
)) {
852 printf("Warning: no INFO/version found!\n");
853 /* info_version = guess_version(ctx); */
856 if (ctx
->opt
.version
) {
857 version
= ctx
->opt
.version
;
858 } else if (ctx
->opt
.implicit_db
) {
859 version
= curr_version
;
861 version
= info_version
;
865 printf("Couldn't determine registry format version, "
866 "specify with --reg-version\n");
871 if ( version
!= info_version
) {
872 if (ctx
->opt
.force
|| !ctx
->opt
.repair
) {
873 printf("Warning: overwrite registry format "
874 "version %d with %d\n", info_version
, version
);
876 printf("Warning: found registry format version %d but "
877 "expected %d, use --force to proceed.\n", info_version
, version
);
882 ctx
->version
= version
;
883 ctx
->sep
= (version
> 1) ? '\\' : '/';
889 dbwrap_store_verbose(struct db_context
*db
, const char *key
, TDB_DATA nval
)
891 TALLOC_CTX
*mem_ctx
= talloc_new(talloc_tos());
895 status
= dbwrap_fetch_bystring(db
, mem_ctx
, key
, &oval
);
896 if (!NT_STATUS_IS_OK(status
)) {
897 printf ("store %s failed to fetch old value: %s\n", key
,
902 if (!tdb_data_is_empty(oval
) && !tdb_data_equal(nval
, oval
)) {
906 key
, tdb_data_print(mem_ctx
, oval
),
907 tdb_data_print(mem_ctx
, nval
));
910 status
= dbwrap_store_bystring(db
, key
, nval
, 0);
911 if (!NT_STATUS_IS_OK(status
)) {
912 printf ("store %s failed: %s\n", key
, nt_errstr(status
));
916 talloc_free(mem_ctx
);
917 return NT_STATUS_IS_OK(status
);
921 static int cmp_keynames(char **p1
, char **p2
)
923 return strcasecmp_m(*p1
, *p2
);
927 write_subkeylist(struct db_context
*db
, struct regkey
*key
, char sep
)
929 cbuf
*buf
= cbuf_new(talloc_tos());
933 cbuf_putdw(buf
, key
->nsubkeys
);
935 for (i
=0; i
< key
->nsubkeys
; i
++) {
936 struct regkey
*subkey
= key
->subkeys
[i
];
937 const char *name
= subkey
->name
;
939 printf("Warning: no explicite name for key %s\n",
941 name
= strrchr_m(subkey
->path
, sep
);
945 cbuf_puts(buf
, name
, -1);
946 cbuf_putc(buf
, '\0');
949 ret
= dbwrap_store_verbose(db
, key
->path
, cbuf_make_tdb_data(buf
));
955 static bool write_sorted(struct db_context
*db
, struct regkey
*key
, char sep
)
957 cbuf
*buf
= cbuf_new(talloc_tos());
961 char **sorted
= talloc_zero_array(buf
, char*, key
->nsubkeys
);
962 int offset
= (1 + key
->nsubkeys
) * sizeof(uint32_t);
964 for (i
=0; i
< key
->nsubkeys
; i
++) {
965 sorted
[i
] = talloc_strdup_upper(sorted
, key
->subkeys
[i
]->name
);
967 TYPESAFE_QSORT(sorted
, key
->nsubkeys
, cmp_keynames
);
969 cbuf_putdw(buf
, key
->nsubkeys
);
970 for (i
=0; i
< key
->nsubkeys
; i
++) {
971 cbuf_putdw(buf
, offset
);
972 offset
+= strlen(sorted
[i
]) + 1;
974 for (i
=0; i
< key
->nsubkeys
; i
++) {
975 cbuf_puts(buf
, sorted
[i
], -1);
976 cbuf_putc(buf
, '\0');
979 path
= talloc_asprintf(buf
, "%s%c%s", REG_SORTED_SUBKEYS_PREFIX
, sep
,
982 DEBUG(0, ("Out of memory!\n"));
986 ret
= dbwrap_store_verbose(db
, path
, cbuf_make_tdb_data(buf
));
992 static bool write_values(struct db_context
*db
, struct regkey
*key
, char sep
)
994 cbuf
*buf
= cbuf_new(talloc_tos());
999 cbuf_putdw(buf
, key
->nvalues
);
1000 for (i
=0; i
< key
->nvalues
; i
++) {
1001 struct regval
*val
= key
->values
[i
];
1002 cbuf_puts(buf
, val
->name
, -1);
1003 cbuf_putc(buf
, '\0');
1004 cbuf_putdw(buf
, val
->type
);
1005 cbuf_putdw(buf
, val
->data
.length
);
1006 cbuf_puts(buf
, (void*)val
->data
.data
, val
->data
.length
);
1009 path
= talloc_asprintf(buf
, "%s%c%s", REG_VALUE_PREFIX
, sep
, key
->path
);
1011 DEBUG(0, ("Out of memory!\n"));
1015 ret
= dbwrap_store_verbose(db
, path
, cbuf_make_tdb_data(buf
));
1021 static bool write_sd(struct db_context
*db
, struct regkey
*key
, char sep
)
1027 TALLOC_CTX
*mem_ctx
= talloc_new(talloc_tos());
1029 status
= marshall_sec_desc(mem_ctx
, key
->sd
, &sd
.dptr
, &sd
.dsize
);
1030 if (!NT_STATUS_IS_OK(status
)) {
1031 printf("marshall sec desc %s failed: %s\n",
1032 key
->path
, nt_errstr(status
));
1035 path
= talloc_asprintf(mem_ctx
, "%s%c%s", REG_SECDESC_PREFIX
,
1038 DEBUG(0, ("Out of memory!\n"));
1042 ret
= dbwrap_store_verbose(db
, path
, sd
);
1044 talloc_free(mem_ctx
);
1049 static int check_write_db_action(struct db_record
*rec
, void *check_ctx
)
1051 struct check_ctx
*ctx
= (struct check_ctx
*)check_ctx
;
1052 TDB_DATA rec_val
= dbwrap_record_get_value(rec
);
1053 struct regkey
*key
= *(struct regkey
**)rec_val
.dptr
;
1054 TALLOC_CTX
*frame
= talloc_stackframe();
1056 /* write subkeylist */
1057 if ((ctx
->version
> 2) || (key
->nsubkeys
> 0) || (key
->has_subkeylist
)) {
1058 write_subkeylist(ctx
->odb
, key
, ctx
->sep
);
1061 /* write sorted subkeys */
1062 if ((ctx
->version
< 3) && (key
->nsubkeys
> 0)) {
1063 write_sorted(ctx
->odb
, key
, ctx
->sep
);
1066 /* write value list */
1067 if (key
->nvalues
> 0) {
1068 write_values(ctx
->odb
, key
, ctx
->sep
);
1073 write_sd(ctx
->odb
, key
, ctx
->sep
);
1080 static int fix_tree_action(struct db_record
*rec
, void *check_ctx
)
1082 struct check_ctx
*ctx
= (struct check_ctx
*)check_ctx
;
1083 TDB_DATA rec_key
= dbwrap_record_get_key(rec
);
1084 TDB_DATA rec_val
= dbwrap_record_get_value(rec
);
1085 struct regkey
* key
= *(struct regkey
**)rec_val
.dptr
;
1086 if (ctx
->opt
.verbose
) {
1087 printf("Check Tree: %s\n", key
->path
);
1090 assert (strncmp(key
->path
, (char*)rec_key
.dptr
, rec_key
.dsize
) == 0);
1092 /* assert(dbwrap_exists(ctx->db, string_term_tdb_data(key->path)) */
1093 /* == key->exists); */
1095 if (key
->needs_update
) {
1096 printf("Update key: \"%s\"\n", key
->path
);
1097 if ((ctx
->version
> 2) || (key
->nsubkeys
> 0)) {
1098 write_subkeylist(ctx
->odb
, key
, ctx
->sep
);
1100 if ((ctx
->version
<= 2) && (key
->nsubkeys
> 0)) {
1101 write_sorted(ctx
->odb
, key
, ctx
->sep
);
1103 if (key
->nvalues
> 0) {
1104 write_values(ctx
->odb
, key
, ctx
->sep
);
1107 write_sd(ctx
->odb
, key
, ctx
->sep
);
1109 } else if (!key
->has_subkeylist
) {
1110 if ((ctx
->version
> 2) || (key
->nsubkeys
> 0)) {
1111 printf("Missing subkeylist: %s\n", key
->path
);
1112 write_subkeylist(ctx
->odb
, key
, ctx
->sep
);
1116 if (key
->name
== NULL
&& key
->parent
->has_subkeylist
) {
1117 printf("Key not referenced by the its parents subkeylist: %s\n",
1119 write_subkeylist(ctx
->odb
, key
->parent
, ctx
->sep
);
1122 /* XXX check that upcase(name) matches last part of path ??? */
1128 /* give the same warnings as fix_tree_action */
1129 static int check_tree_action(struct db_record
*rec
, void *check_ctx
)
1131 struct check_ctx
*ctx
= (struct check_ctx
*)check_ctx
;
1132 TDB_DATA rec_key
= dbwrap_record_get_key(rec
);
1133 TDB_DATA rec_val
= dbwrap_record_get_value(rec
);
1134 struct regkey
* key
= *(struct regkey
**)rec_val
.dptr
;
1135 if (ctx
->opt
.verbose
) {
1136 printf("Check Tree: %s\n", key
->path
);
1139 assert (strncmp(key
->path
, (char*)rec_key
.dptr
, rec_key
.dsize
) == 0);
1141 if (!key
->has_subkeylist
) {
1142 if ((ctx
->version
> 2) || (key
->nsubkeys
> 0)) {
1143 printf("Missing subkeylist: %s\n", key
->path
);
1147 if (key
->name
== NULL
&& key
->parent
->has_subkeylist
) {
1148 printf("Key not referenced by the its parents subkeylist: %s\n",
1155 static int delete_invalid_action(struct db_record
*rec
, void* check_ctx
)
1158 struct check_ctx
*ctx
= (struct check_ctx
*)check_ctx
;
1159 TDB_DATA rec_key
= dbwrap_record_get_key(rec
);
1160 TDB_DATA rec_val
= dbwrap_record_get_value(rec
);
1163 printf("Delete key: \"%.*s\"",(int)rec_key
.dsize
, rec_key
.dptr
);
1164 if (rec_val
.dsize
> 0) {
1165 printf(" in favour of \"%s\"\n", rec_val
.dptr
);
1170 status
= dbwrap_delete(ctx
->odb
, rec_key
);
1171 if (!NT_STATUS_IS_OK(status
)) {
1172 d_printf("delete key \"%.*s\" failed!\n",
1173 (int)rec_key
.dsize
, rec_key
.dptr
);
1179 static bool check_ctx_check_tree(struct check_ctx
*ctx
) {
1182 status
= dbwrap_traverse(ctx
->reg
, check_tree_action
, ctx
, NULL
);
1183 if (!NT_STATUS_IS_OK(status
)) {
1184 DEBUG(0, ("check traverse failed: %s\n",
1185 nt_errstr(status
)));
1190 static bool check_ctx_fix_inplace(struct check_ctx
*ctx
) {
1192 status
= dbwrap_traverse(ctx
->reg
, fix_tree_action
, ctx
, NULL
);
1193 if (!NT_STATUS_IS_OK(status
)) {
1194 DEBUG(0, ("fix traverse failed: %s\n", nt_errstr(status
)));
1198 status
= dbwrap_traverse(ctx
->del
, delete_invalid_action
, ctx
, NULL
);
1199 if (!NT_STATUS_IS_OK(status
)) {
1200 DEBUG(0, ("delete traverse failed: %s\n", nt_errstr(status
)));
1206 static bool check_ctx_write_new_db(struct check_ctx
*ctx
) {
1211 if (ctx
->opt
.wipe
) {
1212 int ret
= dbwrap_wipe(ctx
->odb
);
1214 DEBUG(0, ("wiping %s failed\n", ctx
->opt
.output
));
1219 status
= dbwrap_traverse(ctx
->reg
, check_write_db_action
, ctx
, NULL
);
1220 if (!NT_STATUS_IS_OK(status
)) {
1221 DEBUG(0, ("traverse2 failed: %s\n", nt_errstr(status
)));
1225 status
= dbwrap_store_uint32(ctx
->odb
,
1226 "INFO/version", ctx
->version
);
1227 if (!NT_STATUS_IS_OK(status
)) {
1228 DEBUG(0, ("write version failed: %s\n", nt_errstr(status
)));
1234 int net_registry_check_db(const char *name
, const struct check_options
*opt
)
1238 struct check_ctx
*ctx
= check_ctx_create(talloc_tos(), name
, opt
);
1243 d_printf("Check database: %s\n", name
);
1245 /* 1. open output RW */
1246 if (!check_ctx_open_output(ctx
)) {
1250 /* 2. open input RO */
1251 if (!check_ctx_open_input(ctx
)) {
1255 if (opt
->lock
&& !check_ctx_transaction_start(ctx
)) {
1259 if (!get_version(ctx
)) {
1263 status
= dbwrap_traverse_read(ctx
->idb
, check_tdb_action
, ctx
, NULL
);
1264 if (!NT_STATUS_IS_OK(status
)) {
1265 DEBUG(0, ("check traverse failed: %s\n", nt_errstr(status
)));
1269 if (!opt
->lock
&& !check_ctx_transaction_start(ctx
)) {
1273 if (ctx
->opt
.repair
&& !ctx
->opt
.wipe
) {
1274 if (!check_ctx_fix_inplace(ctx
)) {
1278 if (!check_ctx_check_tree(ctx
)) {
1282 if (!check_ctx_write_new_db(ctx
)) {
1289 check_ctx_transaction_stop(ctx
, ret
== 0);
1295 /*Local Variables:*/