vfs: Make function pointer names consistent. They all end in _fn
[Samba/gebeck_regimport.git] / source3 / utils / net_registry_check.c
blob201dc5ebc16aaf47382e3243b5dde65f0d6ea3dc
1 /*
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/>.
20 /**
21 * @brief Check the registry database.
22 * @author Gregor Beck <gb@sernet.de>
23 * @date Mar 2011
26 #include "net_registry_check.h"
28 #include "includes.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"
33 #include "net.h"
34 #include "libcli/security/dom_sid.h"
35 #include "libcli/security/secdesc.h"
36 #include "cbuf.h"
37 #include "srprs.h"
38 #include <termios.h>
39 #include "util_tdb.h"
40 #include "registry/reg_db.h"
41 #include "libcli/registry/util_reg.h"
42 #include "registry/reg_parse_internal.h"
43 #include "interact.h"
46 check tree:
47 + every key has a subkeylist
48 + every key is referenced by the subkeylist of its parent
49 check path:
50 + starts with valid hive
51 + UTF-8 (option to convert ???)
52 + only uppercase
53 + separator ???
54 check value:
55 + REG_DWORD has size 4
56 + REG_QWORD has size 8
57 + STRINGS are zero terminated UTF-16
60 struct regval {
61 char *name;
62 uint32_t type;
63 DATA_BLOB data;
66 struct regkey {
67 char *name;
68 char *path;
69 bool has_subkeylist;
70 bool needs_update;
71 struct regkey *parent;
72 size_t nsubkeys;
73 struct regkey **subkeys;
74 size_t nvalues;
75 struct regval **values;
76 struct security_descriptor *sd;
79 struct check_ctx {
80 char *fname;
81 struct check_options opt;
83 uint32_t version;
84 char sep;
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;
92 bool transaction;
93 char auto_action;
94 char default_action;
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);
101 if (tmp == NULL) {
102 talloc_free(array);
103 return NULL;
105 tmp[size-1] = ptr;
106 tmp[size] = NULL;
107 return tmp;
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) {
115 key->nsubkeys++;
119 static struct regval* regval_copy(TALLOC_CTX *mem_ctx, const struct regval *val)
121 struct regval *ret = talloc_zero(mem_ctx, struct regval);
122 if (ret == NULL) {
123 goto fail;
126 ret->name = talloc_strdup(ret, val->name);
127 if (ret->name == NULL) {
128 goto fail;
131 ret->data = data_blob_dup_talloc(ret, val->data);
132 if (ret->data.data == NULL) {
133 goto fail;
136 ret->type = val->type;
138 return ret;
139 fail:
140 talloc_free(ret);
141 return NULL;
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) {
149 key->nvalues++;
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);
158 buf->dptr += len;
159 buf->dsize -= len;
160 return true;
162 return false;
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;
170 buf->dptr += len;
171 buf->dsize -= len;
172 return true;
174 return false;
177 static bool tdb_data_read_blob(TDB_DATA *buf, DATA_BLOB *result)
179 TDB_DATA tmp = *buf;
180 uint32_t len;
181 if (!tdb_data_read_uint32(&tmp, &len)) {
182 return false;
184 if (tmp.dsize >= len) {
185 *buf = tmp;
186 result->data = tmp.dptr;
187 result->length = len;
188 buf->dptr += len;
189 buf->dsize -= len;
190 return true;
192 return false;
195 static bool tdb_data_read_regval(TDB_DATA *buf, struct regval *result)
197 TDB_DATA tmp = *buf;
198 struct regval value;
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))
203 return false;
205 *buf = tmp;
206 *result = value;
207 return true;
210 static bool tdb_data_is_cstr(TDB_DATA d) {
211 if (tdb_data_is_empty(d) || (d.dptr[d.dsize-1] != '\0')) {
212 return false;
214 return strchr((char *)d.dptr, '\0') == (char *)&d.dptr[d.dsize-1];
217 static char* tdb_data_print(TALLOC_CTX *mem_ctx, TDB_DATA d)
219 if (!tdb_data_is_empty(d)) {
220 char *ret = NULL;
221 cbuf *ost = cbuf_new(mem_ctx);
222 int len = cbuf_print_quoted(ost, (const char*)d.dptr, d.dsize);
223 if (len != -1) {
224 cbuf_swapptr(ost, &ret, 0);
225 talloc_steal(mem_ctx, ret);
227 talloc_free(ost);
228 return ret;
230 return talloc_strdup(mem_ctx, "<NULL>");
234 static TDB_DATA cbuf_make_tdb_data(cbuf *b)
236 return make_tdb_data((void*)cbuf_gets(b, 0), cbuf_getpos(b));
239 static void remove_all(char *str, char c)
241 char *out=str;
242 while (*str) {
243 if (*str != c) {
244 *out = *str;
245 out++;
247 str++;
249 *out = '\0';
252 static char* parent_path(const char *path, char sep)
254 const char *p = strrchr(path, sep);
255 return p ? talloc_strndup(talloc_tos(), path, p-path) : NULL;
258 /* return the regkey corresponding to path, create if not yet existing */
259 static struct regkey*
260 check_ctx_lookup_key(struct check_ctx *ctx, const char *path) {
261 struct regkey *ret = NULL;
262 NTSTATUS status;
263 TDB_DATA val = tdb_null;
265 if ( path == NULL) {
266 return ctx->root;
269 status = dbwrap_fetch(ctx->reg, ctx, string_term_tdb_data(path), &val);
270 if (NT_STATUS_IS_OK(status)) {
271 if (ctx->opt.verbose) {
272 printf("Open: %s\n", path);
274 ret = *(struct regkey**)val.dptr;
275 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
276 /* not yet existing, create */
277 char *pp;
278 if (ctx->opt.verbose) {
279 printf("New: %s\n", path);
281 ret = talloc_zero(ctx, struct regkey);
282 if (ret == NULL) {
283 DEBUG(0, ("Out of memory!\n"));
284 goto done;
286 ret->path = talloc_strdup(ret, path);
288 pp = parent_path(path, ctx->sep);
289 ret->parent = check_ctx_lookup_key(ctx, pp);
290 regkey_add_subkey(ret->parent, ret);
291 TALLOC_FREE(pp);
293 /* the dummy root key has no subkeylist so set the name */
294 if (ret->parent == ctx->root) {
295 ret->name = talloc_strdup(ret, path);
298 dbwrap_store(ctx->reg, string_term_tdb_data(path),
299 make_tdb_data((void*)&ret, sizeof(ret)), 0);
300 } else {
301 DEBUG(0, ("lookup key: failed to fetch %s: %s\n", path,
302 nt_errstr(status)));
304 done:
305 talloc_free(val.dptr);
306 return ret;
309 static struct check_ctx* check_ctx_create(TALLOC_CTX *mem_ctx, const char *db,
310 const struct check_options *opt)
312 struct check_ctx *ctx = talloc_zero(mem_ctx, struct check_ctx);
314 ctx->opt = *opt;
315 ctx->reg = db_open_rbt(ctx);
316 ctx->del = db_open_rbt(ctx);
317 ctx->root = talloc_zero(ctx, struct regkey);
318 ctx->fname = talloc_strdup(ctx, db);
320 if (opt->automatic && (opt->output == NULL)) {
321 ctx->opt.repair = true;
322 ctx->opt.output = ctx->fname;
325 if (opt->repair) {
326 if (opt->output) {
327 d_fprintf(stderr, "You can not specify --output "
328 "with --repair\n");
329 goto fail;
330 } else {
331 ctx->opt.output = ctx->fname;
335 ctx->default_action = 'r';
336 return ctx;
337 fail:
338 talloc_free(ctx);
339 return NULL;
342 static bool check_ctx_open_output(struct check_ctx *ctx)
344 int oflags = O_RDWR | O_CREAT ;
346 if (ctx->opt.output == NULL) {
347 return true;
350 if (!ctx->opt.repair) {
351 if (!ctx->opt.wipe) {
352 oflags |= O_EXCL;
354 ctx->opt.wipe = true;
357 ctx->odb = db_open(ctx, ctx->opt.output, 0, TDB_DEFAULT, oflags, 0644);
358 if (ctx->odb == NULL) {
359 d_fprintf(stderr,
360 _("Could not open db (%s) for writing: %s\n"),
361 ctx->opt.output, strerror(errno));
362 return false;
364 return true;
368 static bool check_ctx_open_input(struct check_ctx *ctx) {
369 ctx->idb = db_open(ctx, ctx->fname, 0, TDB_DEFAULT, O_RDONLY, 0);
370 if (ctx->idb == NULL) {
371 d_fprintf(stderr,
372 _("Could not open db (%s) for reading: %s\n"),
373 ctx->fname, strerror(errno));
374 return false;
376 return true;
379 static bool check_ctx_transaction_start(struct check_ctx *ctx) {
380 if (ctx->odb == NULL) {
381 return true;
383 if (dbwrap_transaction_start(ctx->odb) != 0) {
384 DEBUG(0, ("transaction_start failed\n"));
385 return false;
387 ctx->transaction = true;
388 return true;
391 static void check_ctx_transaction_stop(struct check_ctx *ctx, bool ok) {
392 if (!ctx->transaction) {
393 return;
395 if (!ctx->opt.test && ok) {
396 d_printf("Commiting changes\n");
397 if (dbwrap_transaction_commit(ctx->odb) != 0) {
398 DEBUG(0, ("transaction_commit failed\n"));
400 } else {
401 d_printf("Discarding changes\n");
402 dbwrap_transaction_cancel(ctx->odb);
406 static bool read_info(struct check_ctx *ctx, const char *key, TDB_DATA val)
408 if (val.dsize==sizeof(uint32_t) && strcmp(key, "version")==0) {
409 uint32_t v = IVAL(val.dptr, 0);
410 printf("INFO: %s = %d\n", key, v);
411 return true;
413 printf("INFO: %s = <invalid>\n", key);
414 return false;
417 static bool is_all_upper(const char *str) {
418 bool ret;
419 char *tmp = talloc_strdup(talloc_tos(), str);
420 strupper_m(tmp);
421 ret = (strcmp(tmp, str) == 0);
422 talloc_free(tmp);
423 return ret;
426 static void move_to_back(struct regkey *key, struct regkey *subkey)
428 struct regkey **ptr;
429 size_t nidx;
431 DEBUG(5, ("Move to back subkey \"%s\" of \"%s\"\n",
432 subkey->path, key->path));
434 for (ptr=key->subkeys; *ptr != subkey; ptr++)
437 nidx = ptr + 1 - key->subkeys;
438 memmove(ptr, ptr+1, (key->nsubkeys - nidx) * sizeof(*ptr));
440 key->subkeys[key->nsubkeys-1] = subkey;
443 static void set_subkey_name(struct check_ctx *ctx, struct regkey *key,
444 const char *name, int nlen)
446 char *path = key->path;
447 TALLOC_CTX *mem_ctx = talloc_new(talloc_tos());
448 char *p;
449 struct regkey *subkey;
450 char *nname = talloc_strndup(mem_ctx, name, nlen);
451 remove_all(nname, ctx->sep);
453 if (strncmp(name, nname, nlen) != 0) {
454 /* XXX interaction: delete/edit */
455 printf("Warning: invalid name: \"%s\" replace with \"%s\"\n",
456 name, nname);
457 key->needs_update = true;
459 p = talloc_asprintf_strupper_m(mem_ctx, "%s%c%s",
460 path, ctx->sep, nname);
461 subkey = check_ctx_lookup_key(ctx, p);
462 if (subkey->name) {
463 bool do_replace = false;
465 if (strcmp(subkey->name, nname) != 0) {
466 int action;
467 char default_action;
469 if (is_all_upper(nname)) {
470 default_action = 'o';
471 } else {
472 default_action = 'n';
475 printf("Conflicting subkey names of [%s]: "
476 "old: \"%s\", new: \"%s\"\n",
477 key->path, subkey->name, nname);
479 if (ctx->opt.output == NULL || ctx->opt.automatic) {
480 action = default_action;
481 } else {
482 do {
483 action = interact_prompt(
484 "choose spelling [o]ld, [n]ew,"
485 "or [e]dit", "one",
486 default_action);
487 if (action == 'e') {
488 printf("Sorry, edit is not yet "
489 "implemented here...\n");
491 } while (action == 'e');
494 if (action == 'n') {
495 do_replace = true;
499 if (do_replace) {
500 if (ctx->opt.verbose) {
501 printf("Replacing name: %s: \"%s\""
502 " -> \"%s\"\n", path,
503 subkey->name, nname);
505 TALLOC_FREE(subkey->name);
506 subkey->name = talloc_steal(subkey, nname);
507 key->needs_update = true;
509 } else {
510 if (ctx->opt.verbose) {
511 printf("Set name: %s: \"%s\"\n", path, nname);
513 subkey->name = talloc_steal(subkey, nname);
516 move_to_back(key, subkey);
517 TALLOC_FREE(mem_ctx);
520 static void
521 read_subkeys(struct check_ctx *ctx, const char *path, TDB_DATA val, bool update)
523 uint32_t num_items, found_items = 0;
524 char *subkey;
525 struct regkey *key = check_ctx_lookup_key(ctx, path);
527 key->needs_update |= update;
529 /* printf("SUBKEYS: %s\n", path); */
530 if (key->has_subkeylist) {
531 printf("Duplicate subkeylist \"%s\"\n",
532 path);
533 found_items = key->nsubkeys;
536 /* exists as defined by regdb_key_exists() */
537 key->has_subkeylist = true;
539 /* name is set if a key is referenced by the */
540 /* subkeylist of its parent. */
542 if (!tdb_data_read_uint32(&val, &num_items) ) {
543 printf("Invalid subkeylist: \"%s\"\n", path);
544 return;
547 while (tdb_data_read_cstr(&val, &subkey)) {
548 /* printf(" SUBKEY: %s\n", subkey); */
549 set_subkey_name(ctx, key, subkey, strlen(subkey));
550 found_items++;
553 if (val.dsize != 0) {
554 printf("Subkeylist of \"%s\": trailing: \"%.*s\"\n",
555 path, (int)val.dsize, val.dptr);
556 /* ask: best effort, delete or edit?*/
557 set_subkey_name(ctx, key, (char*)val.dptr, val.dsize);
558 found_items++;
559 key->needs_update = true;
562 if (num_items != found_items) {
563 printf("Subkeylist of \"%s\": invalid number of subkeys, "
564 "expected: %d got: %d\n", path, num_items, found_items);
565 key->needs_update = true;
570 static void read_values(struct check_ctx *ctx, const char *path, TDB_DATA val)
572 struct regkey *key = check_ctx_lookup_key(ctx, path);
573 uint32_t num_items, found_items;
574 struct regval value;
576 /* printf("VALUES: %s\n", path); */
578 if (!tdb_data_read_uint32(&val, &num_items) ) {
579 printf("Invalid valuelist: \"%s\"\n", path);
580 return;
583 found_items=0;
584 while (tdb_data_read_regval(&val, &value)) {
585 /* printf(" VAL: %s type: %s(%d) length: %d\n", value.name, */
586 /* str_regtype(value.type), value.type, */
587 /* (int)value.data.length); */
588 regkey_add_regval(key, regval_copy(key, &value));
589 found_items++;
592 if (num_items != found_items) {
593 printf("Valuelist of \"%s\": invalid number of values, "
594 "expected: %d got: %d\n", path, num_items, found_items);
595 key->needs_update = true;
598 if (val.dsize != 0) {
599 printf("Valuelist of \"%s\": trailing: \"%*s\"\n", path,
600 (int)val.dsize, val.dptr);
601 key->needs_update = true;
602 /* XXX best effort ??? */
603 /* ZERO_STRUCT(value); */
604 /* if (tdb_data_read_cstr(&val, &value.name) */
605 /* && tdb_data_read_uint32(&val, &value.type)) */
606 /* { */
607 /* uint32_t len = -1; */
608 /* tdb_data_read_uint32(&val, &len); */
609 /* ... */
610 /* found_items ++; */
611 /* regkey_add_regval(key, regval_copy(key, value)); */
612 /* } */
614 if (found_items == 0) {
615 printf("Valuelist of \"%s\" empty\n", path);
616 key->needs_update = true;
620 static bool read_sorted(struct check_ctx *ctx, const char *path, TDB_DATA val)
622 if (ctx->version >= 3) {
623 return false;
626 if ((val.dptr == NULL) || (val.dsize<4)) {
627 return false;
630 /* ToDo: check */
631 /* struct regkey *key = check_ctx_lookup_key(ctx, path); */
632 /* printf("SORTED: %s\n", path); */
633 return true;
636 static bool read_sd(struct check_ctx *ctx, const char *path, TDB_DATA val)
638 NTSTATUS status;
639 struct regkey *key = check_ctx_lookup_key(ctx, path);
640 /* printf("SD: %s\n", path); */
642 status = unmarshall_sec_desc(key, val.dptr, val.dsize, &key->sd);
643 if (!NT_STATUS_IS_OK(status)) {
644 DEBUG(0, ("Failed to read SD of %s: %s\n",
645 path, nt_errstr(status)));
647 return true;
650 static bool srprs_path(const char **ptr, const char* prefix, char sep,
651 const char **ppath)
653 const char *path, *pos = *ptr;
654 if (prefix != NULL) {
655 if (!srprs_str(&pos, prefix, -1) || !srprs_char(&pos, sep) ) {
656 return false;
659 path = pos;
660 if ( !srprs_hive(&pos, NULL) ) {
661 return false;
663 if ( !srprs_eos(&pos) && !srprs_char(&pos, sep) ) {
664 return false;
666 *ppath = path;
667 *ptr = strchr(pos, '\0');
668 return true;
671 /* Fixme: this dosn't work in the general multibyte char case.
672 see string_replace()
674 static bool normalize_path_internal(char* path, char sep) {
675 size_t len = strlen(path);
676 const char *orig = talloc_strndup(talloc_tos(), path, len);
677 char *optr = path, *iptr = path;
678 bool changed;
680 while (*iptr == sep ) {
681 iptr++;
683 while (*iptr) {
684 *optr = *iptr;
685 if (*iptr == sep) {
686 while (*iptr == sep) {
687 iptr++;
689 if (*iptr) {
690 optr++;
692 } else {
693 iptr++;
694 optr++;
697 *optr = '\0';
699 strupper_m(path);
700 changed = (strcmp(orig, path) != 0);
701 talloc_free(discard_const(orig));
702 return changed;
705 static bool normalize_path(char* path, char sep) {
706 static const char* SEPS = "\\/";
707 char* firstsep = strpbrk(path, SEPS);
708 bool wrong_sep = (firstsep && (*firstsep != sep));
710 assert (strchr(SEPS, sep));
712 if (wrong_sep) {
713 string_replace(path, *firstsep, sep);
715 return normalize_path_internal(path, sep) || wrong_sep;
718 static int check_tdb_action(struct db_record *rec, void *check_ctx)
720 struct check_ctx *ctx = (struct check_ctx*)check_ctx;
721 TALLOC_CTX *frame = talloc_stackframe();
722 TDB_DATA val = dbwrap_record_get_value(rec);
723 TDB_DATA rec_key = dbwrap_record_get_key(rec);
724 char *key;
725 bool invalid_path = false;
726 bool once_more;
727 bool first_iter = true;
729 if (!tdb_data_is_cstr(rec_key)) {
730 printf("Key is not zero terminated: \"%.*s\"\ntry to go on.\n",
731 (int)rec_key.dsize, rec_key.dptr);
732 invalid_path = true;
734 key = talloc_strndup(frame, (char*)rec_key.dptr, rec_key.dsize);
736 do {
737 const char *path, *pos = key;
738 once_more = false;
740 if (srprs_str(&pos, "INFO/", -1)) {
741 if ( read_info(ctx, pos, val) ) {
742 break;
744 invalid_path = true;
745 /* ask: mark invalid */
746 } else if (srprs_str(&pos, "__db_sequence_number__", -1)) {
747 printf("Skip key: \"%.*s\"\n",
748 (int)rec_key.dsize, rec_key.dptr);
749 /* skip: do nothing + break */
750 break;
752 } else if (normalize_path(key, ctx->sep)) {
753 printf("Unnormal key: \"%.*s\"\n",
754 (int)rec_key.dsize, rec_key.dptr);
755 printf("Normalize to: \"%s\"\n", key);
756 invalid_path = true;
757 } else if (srprs_path(&pos, NULL,
758 ctx->sep, &path))
760 read_subkeys(ctx, path, val, invalid_path);
761 break;
762 } else if (srprs_path(&pos, REG_VALUE_PREFIX,
763 ctx->sep, &path))
765 read_values(ctx, path, val);
766 break;
767 } else if (srprs_path(&pos, REG_SECDESC_PREFIX,
768 ctx->sep, &path))
770 read_sd(ctx, path, val);
771 break;
772 } else if (srprs_path(&pos, REG_SORTED_SUBKEYS_PREFIX,
773 ctx->sep, &path))
775 if (!read_sorted(ctx, path, val)) {
776 /* delete: mark invalid + break */
777 printf("Invalid sorted subkeys for: \"%s\"\n", path);
778 invalid_path = true;
779 key = NULL;
781 break;
782 } else {
783 printf("Unrecognized key: \"%.*s\"\n",
784 (int)rec_key.dsize, rec_key.dptr);
785 invalid_path = true;
788 if (invalid_path) {
789 int action;
790 if (ctx->opt.output == NULL) {
791 action = first_iter ? 'r' : 's';
792 } else if (ctx->opt.automatic) {
793 action = first_iter ? 'r' : 'd';
794 } else if (ctx->auto_action != '\0') {
795 action = ctx->auto_action;
796 } else {
797 action = interact_prompt("[s]kip,[S]kip all,"
798 "[d]elete,[D]elete all"
799 ",[e]dit,[r]etry"
800 , "sder",
801 ctx->default_action);
803 if (isupper(action)) {
804 action = tolower(action);
805 ctx->auto_action = action;
807 ctx->default_action = action;
808 switch (action) {
809 case 's': /* skip */
810 invalid_path = false;
811 break;
812 case 'd': /* delete */
813 invalid_path = true;
814 key = NULL;
815 break;
816 case 'e': /* edit */ {
817 char *p = interact_edit(frame, key);
818 if (p) {
819 talloc_free(key);
820 key = p;
822 } /* fall through */
823 case 'r': /* retry */
824 once_more = true;
825 break;
828 first_iter = false;
829 } while (once_more);
831 if (invalid_path) {
832 dbwrap_store(ctx->del, rec_key, string_term_tdb_data(key), 0);
835 talloc_free(frame);
836 return 0;
839 static bool get_version(struct check_ctx *ctx) {
840 static const uint32_t curr_version = REGDB_CODE_VERSION;
841 uint32_t version = ctx->opt.version ? ctx->opt.version : curr_version;
842 uint32_t info_version = 0;
843 NTSTATUS status;
845 status = dbwrap_fetch_uint32(ctx->idb, "INFO/version", &info_version);
846 if (!NT_STATUS_IS_OK(status)) {
847 printf("Warning: no INFO/version found!\n");
848 /* info_version = guess_version(ctx); */
851 if (ctx->opt.version) {
852 version = ctx->opt.version;
853 } else if (ctx->opt.implicit_db) {
854 version = curr_version;
855 } else {
856 version = info_version;
859 if (!version) {
860 printf("Couldn't determine registry format version, "
861 "specify with --reg-version\n");
862 return false;
866 if ( version != info_version ) {
867 if (ctx->opt.force || !ctx->opt.repair) {
868 printf("Warning: overwrite registry format "
869 "version %d with %d\n", info_version, version);
870 } else {
871 printf("Warning: found registry format version %d but "
872 "expected %d, use --force to proceed.\n", info_version, version);
873 return false;
877 ctx->version = version;
878 ctx->sep = (version > 1) ? '\\' : '/';
880 return true;
883 static bool
884 dbwrap_store_verbose(struct db_context *db, const char *key, TDB_DATA nval)
886 TALLOC_CTX *mem_ctx = talloc_new(talloc_tos());
887 TDB_DATA oval;
888 NTSTATUS status;
890 status = dbwrap_fetch_bystring(db, mem_ctx, key, &oval);
891 if (NT_STATUS_IS_OK(status)) {
892 if (tdb_data_equal(nval, oval)) {
893 goto done;
895 printf("store %s:\n overwrite: %s\n with: %s\n", key,
896 tdb_data_print(mem_ctx, oval),
897 tdb_data_print(mem_ctx, nval));
899 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
900 printf("store %s:\n write: %s\n", key,
901 tdb_data_print(mem_ctx, nval));
902 } else {
903 printf ("store %s:\n failed to fetch old value: %s\n", key,
904 nt_errstr(status));
905 goto done;
908 status = dbwrap_store_bystring(db, key, nval, 0);
909 if (!NT_STATUS_IS_OK(status)) {
910 printf ("store %s failed: %s\n", key, nt_errstr(status));
913 done:
914 talloc_free(mem_ctx);
915 return NT_STATUS_IS_OK(status);
918 static bool
919 dbwrap_store_uint32_verbose(struct db_context *db, const char *key, uint32_t nval)
921 uint32_t oval;
922 NTSTATUS status;
924 status = dbwrap_fetch_uint32(db, key, &oval);
925 if (NT_STATUS_IS_OK(status)) {
926 if (nval == oval) {
927 goto done;
929 printf("store %s:\n overwrite: %d\n with: %d\n", key,
930 (int)oval, (int)nval);
932 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
933 printf("store %s:\n write: %d\n", key, (int)nval);
934 } else {
935 printf ("store %s:\n failed to fetch old value: %s\n", key,
936 nt_errstr(status));
937 goto done;
940 status = dbwrap_store_uint32(db, key, nval);
941 if (!NT_STATUS_IS_OK(status)) {
942 printf ("store %s failed: %s\n", key, nt_errstr(status));
945 done:
946 return NT_STATUS_IS_OK(status);
949 static int cmp_keynames(char **p1, char **p2)
951 return strcasecmp_m(*p1, *p2);
954 static bool
955 write_subkeylist(struct db_context *db, struct regkey *key, char sep)
957 cbuf *buf = cbuf_new(talloc_tos());
958 int i;
959 bool ret;
961 cbuf_putdw(buf, key->nsubkeys);
963 for (i=0; i < key->nsubkeys; i++) {
964 struct regkey *subkey = key->subkeys[i];
965 const char *name = subkey->name;
966 if (name == NULL) {
967 printf("Warning: no explicite name for key %s\n",
968 subkey->path);
969 name = strrchr_m(subkey->path, sep);
970 assert(name);
971 name ++;
973 cbuf_puts(buf, name, -1);
974 cbuf_putc(buf, '\0');
977 ret = dbwrap_store_verbose(db, key->path, cbuf_make_tdb_data(buf));
979 talloc_free(buf);
980 return ret;
983 static bool write_sorted(struct db_context *db, struct regkey *key, char sep)
985 cbuf *buf = cbuf_new(talloc_tos());
986 char *path;
987 int i;
988 bool ret = false;
989 char **sorted = talloc_zero_array(buf, char*, key->nsubkeys);
990 int offset = (1 + key->nsubkeys) * sizeof(uint32_t);
992 for (i=0; i < key->nsubkeys; i++) {
993 sorted[i] = talloc_strdup_upper(sorted, key->subkeys[i]->name);
995 TYPESAFE_QSORT(sorted, key->nsubkeys, cmp_keynames);
997 cbuf_putdw(buf, key->nsubkeys);
998 for (i=0; i < key->nsubkeys; i++) {
999 cbuf_putdw(buf, offset);
1000 offset += strlen(sorted[i]) + 1;
1002 for (i=0; i < key->nsubkeys; i++) {
1003 cbuf_puts(buf, sorted[i], -1);
1004 cbuf_putc(buf, '\0');
1007 path = talloc_asprintf(buf, "%s%c%s", REG_SORTED_SUBKEYS_PREFIX, sep,
1008 key->path);
1009 if (path == NULL) {
1010 DEBUG(0, ("Out of memory!\n"));
1011 goto done;
1014 ret = dbwrap_store_verbose(db, path, cbuf_make_tdb_data(buf));
1015 done:
1016 talloc_free(buf);
1017 return ret;
1020 static bool write_values(struct db_context *db, struct regkey *key, char sep)
1022 cbuf *buf = cbuf_new(talloc_tos());
1023 char *path;
1024 int i;
1025 bool ret = false;
1027 cbuf_putdw(buf, key->nvalues);
1028 for (i=0; i < key->nvalues; i++) {
1029 struct regval *val = key->values[i];
1030 cbuf_puts(buf, val->name, -1);
1031 cbuf_putc(buf, '\0');
1032 cbuf_putdw(buf, val->type);
1033 cbuf_putdw(buf, val->data.length);
1034 cbuf_puts(buf, (void*)val->data.data, val->data.length);
1037 path = talloc_asprintf(buf, "%s%c%s", REG_VALUE_PREFIX, sep, key->path);
1038 if (path == NULL) {
1039 DEBUG(0, ("Out of memory!\n"));
1040 goto done;
1043 ret = dbwrap_store_verbose(db, path, cbuf_make_tdb_data(buf));
1044 done:
1045 talloc_free(buf);
1046 return ret;
1049 static bool write_sd(struct db_context *db, struct regkey *key, char sep)
1051 TDB_DATA sd;
1052 NTSTATUS status;
1053 char *path;
1054 bool ret = false;
1055 TALLOC_CTX *mem_ctx = talloc_new(talloc_tos());
1057 status = marshall_sec_desc(mem_ctx, key->sd, &sd.dptr, &sd.dsize);
1058 if (!NT_STATUS_IS_OK(status)) {
1059 printf("marshall sec desc %s failed: %s\n",
1060 key->path, nt_errstr(status));
1061 goto done;
1063 path = talloc_asprintf(mem_ctx, "%s%c%s", REG_SECDESC_PREFIX,
1064 sep, key->path);
1065 if (path == NULL) {
1066 DEBUG(0, ("Out of memory!\n"));
1067 goto done;
1070 ret = dbwrap_store_verbose(db, path, sd);
1071 done:
1072 talloc_free(mem_ctx);
1073 return ret;
1077 static int check_write_db_action(struct db_record *rec, void *check_ctx)
1079 struct check_ctx *ctx = (struct check_ctx*)check_ctx;
1080 TDB_DATA rec_val = dbwrap_record_get_value(rec);
1081 struct regkey *key = *(struct regkey**)rec_val.dptr;
1082 TALLOC_CTX *frame = talloc_stackframe();
1084 /* write subkeylist */
1085 if ((ctx->version > 2) || (key->nsubkeys > 0) || (key->has_subkeylist)) {
1086 write_subkeylist(ctx->odb, key, ctx->sep);
1089 /* write sorted subkeys */
1090 if ((ctx->version < 3) && (key->nsubkeys > 0)) {
1091 write_sorted(ctx->odb, key, ctx->sep);
1094 /* write value list */
1095 if (key->nvalues > 0) {
1096 write_values(ctx->odb, key, ctx->sep);
1099 /* write sd */
1100 if (key->sd) {
1101 write_sd(ctx->odb, key, ctx->sep);
1104 talloc_free(frame);
1105 return 0;
1108 static int fix_tree_action(struct db_record *rec, void *check_ctx)
1110 struct check_ctx *ctx = (struct check_ctx*)check_ctx;
1111 TDB_DATA rec_key = dbwrap_record_get_key(rec);
1112 TDB_DATA rec_val = dbwrap_record_get_value(rec);
1113 struct regkey* key = *(struct regkey**)rec_val.dptr;
1114 if (ctx->opt.verbose) {
1115 printf("Check Tree: %s\n", key->path);
1118 assert (strncmp(key->path, (char*)rec_key.dptr, rec_key.dsize) == 0);
1120 /* assert(dbwrap_exists(ctx->db, string_term_tdb_data(key->path)) */
1121 /* == key->exists); */
1123 if (key->needs_update) {
1124 printf("Update key: \"%s\"\n", key->path);
1125 if ((ctx->version > 2) || (key->nsubkeys > 0)) {
1126 write_subkeylist(ctx->odb, key, ctx->sep);
1128 if ((ctx->version <= 2) && (key->nsubkeys > 0)) {
1129 write_sorted(ctx->odb, key, ctx->sep);
1131 if (key->nvalues > 0) {
1132 write_values(ctx->odb, key, ctx->sep);
1134 if (key->sd) {
1135 write_sd(ctx->odb, key, ctx->sep);
1137 } else if (!key->has_subkeylist) {
1138 if ((ctx->version > 2) || (key->nsubkeys > 0)) {
1139 printf("Missing subkeylist: %s\n", key->path);
1140 write_subkeylist(ctx->odb, key, ctx->sep);
1144 if (key->name == NULL && key->parent->has_subkeylist) {
1145 printf("Key not referenced by the its parents subkeylist: %s\n",
1146 key->path);
1147 write_subkeylist(ctx->odb, key->parent, ctx->sep);
1150 /* XXX check that upcase(name) matches last part of path ??? */
1152 return 0;
1156 /* give the same warnings as fix_tree_action */
1157 static int check_tree_action(struct db_record *rec, void *check_ctx)
1159 struct check_ctx *ctx = (struct check_ctx*)check_ctx;
1160 TDB_DATA rec_key = dbwrap_record_get_key(rec);
1161 TDB_DATA rec_val = dbwrap_record_get_value(rec);
1162 struct regkey* key = *(struct regkey**)rec_val.dptr;
1163 if (ctx->opt.verbose) {
1164 printf("Check Tree: %s\n", key->path);
1167 assert (strncmp(key->path, (char*)rec_key.dptr, rec_key.dsize) == 0);
1169 if (!key->has_subkeylist) {
1170 if ((ctx->version > 2) || (key->nsubkeys > 0)) {
1171 printf("Missing subkeylist: %s\n", key->path);
1175 if (key->name == NULL && key->parent->has_subkeylist) {
1176 printf("Key not referenced by the its parents subkeylist: %s\n",
1177 key->path);
1180 return 0;
1183 static int delete_invalid_action(struct db_record *rec, void* check_ctx)
1185 NTSTATUS status;
1186 struct check_ctx *ctx = (struct check_ctx*)check_ctx;
1187 TDB_DATA rec_key = dbwrap_record_get_key(rec);
1188 TDB_DATA rec_val = dbwrap_record_get_value(rec);
1191 printf("Delete key: \"%.*s\"",(int)rec_key.dsize, rec_key.dptr);
1192 if (rec_val.dsize > 0) {
1193 printf(" in favour of \"%s\"\n", rec_val.dptr);
1194 } else {
1195 putc('\n', stdout);
1198 status = dbwrap_delete(ctx->odb, rec_key);
1199 if (!NT_STATUS_IS_OK(status)) {
1200 d_printf("delete key \"%.*s\" failed!\n",
1201 (int)rec_key.dsize, rec_key.dptr);
1202 return -1;
1204 return 0;
1207 static bool check_ctx_check_tree(struct check_ctx *ctx) {
1208 NTSTATUS status;
1210 status = dbwrap_traverse(ctx->reg, check_tree_action, ctx, NULL);
1211 if (!NT_STATUS_IS_OK(status)) {
1212 DEBUG(0, ("check traverse failed: %s\n",
1213 nt_errstr(status)));
1214 return false;
1216 return true;
1218 static bool check_ctx_fix_inplace(struct check_ctx *ctx) {
1219 NTSTATUS status;
1220 status = dbwrap_traverse(ctx->reg, fix_tree_action, ctx, NULL);
1221 if (!NT_STATUS_IS_OK(status)) {
1222 DEBUG(0, ("fix traverse failed: %s\n", nt_errstr(status)));
1223 return false;
1226 status = dbwrap_traverse(ctx->del, delete_invalid_action, ctx, NULL);
1227 if (!NT_STATUS_IS_OK(status)) {
1228 DEBUG(0, ("delete traverse failed: %s\n", nt_errstr(status)));
1229 return false;
1232 if (!dbwrap_store_uint32_verbose(ctx->odb, "INFO/version", ctx->version)) {
1233 DEBUG(0, ("storing version failed: %s\n", nt_errstr(status)));
1234 return false;
1237 return true;
1240 static bool check_ctx_write_new_db(struct check_ctx *ctx) {
1241 NTSTATUS status;
1243 assert(ctx->odb);
1245 if (ctx->opt.wipe) {
1246 int ret = dbwrap_wipe(ctx->odb);
1247 if (ret != 0) {
1248 DEBUG(0, ("wiping %s failed\n", ctx->opt.output));
1249 return false;
1253 status = dbwrap_traverse(ctx->reg, check_write_db_action, ctx, NULL);
1254 if (!NT_STATUS_IS_OK(status)) {
1255 DEBUG(0, ("traverse2 failed: %s\n", nt_errstr(status)));
1256 return false;
1259 status = dbwrap_store_uint32(ctx->odb,
1260 "INFO/version", ctx->version);
1261 if (!NT_STATUS_IS_OK(status)) {
1262 DEBUG(0, ("write version failed: %s\n", nt_errstr(status)));
1263 return false;
1265 return true;
1268 int net_registry_check_db(const char *name, const struct check_options *opt)
1270 NTSTATUS status;
1271 int ret = -1;
1272 struct check_ctx *ctx = check_ctx_create(talloc_tos(), name, opt);
1273 if (ctx==NULL) {
1274 goto done;
1277 d_printf("Check database: %s\n", name);
1279 /* 1. open output RW */
1280 if (!check_ctx_open_output(ctx)) {
1281 goto done;
1284 /* 2. open input RO */
1285 if (!check_ctx_open_input(ctx)) {
1286 goto done;
1289 if (opt->lock && !check_ctx_transaction_start(ctx)) {
1290 goto done;
1293 if (!get_version(ctx)) {
1294 goto done;
1297 status = dbwrap_traverse_read(ctx->idb, check_tdb_action, ctx, NULL);
1298 if (!NT_STATUS_IS_OK(status)) {
1299 DEBUG(0, ("check traverse failed: %s\n", nt_errstr(status)));
1300 goto done;
1303 if (!opt->lock && !check_ctx_transaction_start(ctx)) {
1304 goto done;
1307 if (ctx->opt.repair && !ctx->opt.wipe) {
1308 if (!check_ctx_fix_inplace(ctx)) {
1309 goto done;
1311 } else {
1312 if (!check_ctx_check_tree(ctx)) {
1313 goto done;
1315 if (ctx->odb) {
1316 if (!check_ctx_write_new_db(ctx)) {
1317 goto done;
1321 ret = 0;
1322 done:
1323 check_ctx_transaction_stop(ctx, ret == 0);
1325 talloc_free(ctx);
1326 return ret;
1329 /*Local Variables:*/
1330 /*mode: c*/
1331 /*End:*/