s3:net: adapt idmap check to new dbwrap_fetch behavior not to return success if not...
[Samba/gebeck_regimport.git] / source3 / utils / net_idmap_check.c
blobb662a85792a81912c4f08738e889cca23d21c7fa
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 idmap database.
22 * @author Gregor Beck <gb@sernet.de>
23 * @date Mar 2011
26 #include "net_idmap_check.h"
27 #include "includes.h"
28 #include "system/filesys.h"
29 #include "dbwrap/dbwrap.h"
30 #include "dbwrap/dbwrap_open.h"
31 #include "dbwrap/dbwrap_rbt.h"
32 #include "net.h"
33 #include "../libcli/security/dom_sid.h"
34 #include "cbuf.h"
35 #include "srprs.h"
36 #include "util_tdb.h"
37 #include "interact.h"
39 static int traverse_commit(struct db_record *diff_rec, void* data);
40 static int traverse_check(struct db_record *rec, void* data);
42 /* TDB_DATA *******************************************************************/
43 static char* print_data(TALLOC_CTX* mem_ctx, TDB_DATA d);
44 static TDB_DATA parse_data(TALLOC_CTX* mem_ctx, const char** ptr);
45 static TDB_DATA talloc_copy(TALLOC_CTX* mem_ctx, TDB_DATA data);
46 static bool is_empty(TDB_DATA data) {
47 return (data.dsize == 0) || (data.dptr == NULL);
50 /* record *********************************************************************/
52 enum DT {
53 DT_INV = 0,
54 DT_SID, DT_UID, DT_GID,
55 DT_HWM, DT_VER, DT_SEQ,
58 struct record {
59 enum DT key_type, val_type;
60 TDB_DATA key, val;
61 struct dom_sid sid;
62 long unsigned id;
65 static struct record* parse_record(TALLOC_CTX* ctx, TDB_DATA key, TDB_DATA val);
66 static struct record* reverse_record(struct record* rec);
68 static bool is_invalid(const struct record* r) {
69 return (r->key_type == DT_INV) || (r->val_type == DT_INV);
72 static bool is_map(const struct record* r) {
73 return (r->key_type == DT_SID)
74 || (r->key_type == DT_UID) || (r->key_type == DT_GID);
77 /* action *********************************************************************/
79 typedef struct check_action {
80 const char* fmt;
81 const char* name;
82 const char* prompt;
83 const char* answers;
84 char auto_action;
85 char default_action;
86 bool verbose;
87 } check_action;
89 struct check_actions {
90 check_action invalid_record;
91 check_action missing_reverse;
92 check_action invalid_mapping;
93 check_action invalid_edit;
94 check_action record_exists;
95 check_action no_version;
96 check_action wrong_version;
97 check_action invalid_hwm;
98 check_action commit;
99 check_action valid_mapping;
100 check_action valid_other;
101 check_action invalid_diff;
104 static struct check_actions
105 check_actions_init(const struct check_options* opts) {
106 struct check_actions ret = {
107 .invalid_record = (check_action) {
108 .name = "Invalid record",
109 .prompt = "[e]dit/[d]elete/[D]elete all"
110 "/[s]kip/[S]kip all",
111 .answers = "eds",
112 .default_action = 'e',
113 .verbose = true,
115 .missing_reverse = (check_action) {
116 .name = "Missing reverse mapping for",
117 .prompt = "[f]ix/[F]ix all/[e]dit/[d]elete/[D]elete all"
118 "/[s]kip/[S]kip all",
119 .answers = "feds",
120 .default_action = 'f',
121 .verbose = true,
123 .invalid_mapping = (check_action) {
124 .fmt = "%1$s: %2$s -> %3$s\n(%4$s <- %3$s)\n",
125 .name = "Invalid mapping",
126 .prompt = "[e]dit/[d]elete/[D]elete all"
127 "/[s]kip/[S]kip all",
128 .answers = "eds",
129 .default_action = 'd',
130 .verbose = true,
132 .invalid_edit = (check_action) {
133 .name = "Invalid record",
134 .prompt = "[e]dit/[d]elete/[D]elete all"
135 "/[s]kip/[S]kip all",
136 .answers = "eds",
137 .default_action = 'e',
138 .verbose = true,
140 .record_exists = (check_action) {
141 .fmt = "%1$s: %2$s\n-%4$s\n+%3$s\n",
142 .name = "Record exists",
143 .prompt = "[o]verwrite/[O]verwrite all/[e]dit"
144 "/[d]elete/[D]elete all/[s]kip/[S]kip all",
145 .answers = "oeds",
146 .default_action = 'o',
147 .verbose = true,
149 .no_version = (check_action) {
150 .prompt = "[f]ix/[s]kip/[a]bort",
151 .answers = "fsa",
152 .default_action = 'f',
154 .wrong_version = (check_action) {
155 .prompt = "[f]ix/[s]kip/[a]bort",
156 .answers = "fsa",
157 .default_action = 'a',
159 .invalid_hwm = (check_action) {
160 .prompt = "[f]ix/[s]kip",
161 .answers = "fs",
162 .default_action = 'f',
164 .commit = (check_action) {
165 .prompt = "[c]ommit/[l]ist/[s]kip",
166 .answers = "cls",
167 .default_action = 'l',
168 .verbose = true,
170 .valid_mapping = (check_action) {
171 .fmt = "%1$s: %2$s <-> %3$s\n",
172 .name = "Mapping",
173 .auto_action = 's',
174 .verbose = opts->verbose,
176 .valid_other = (check_action) {
177 .name = "Other",
178 .auto_action = 's',
179 .verbose = opts->verbose,
181 .invalid_diff = (check_action) {
182 .prompt = "[s]kip/[S]kip all/[c]ommit/[C]ommit all"
183 "/[a]bort",
184 .answers = "sca",
185 .default_action = 's',
189 if (!opts->repair) {
190 ret.invalid_record.auto_action = 's';
191 ret.missing_reverse.auto_action = 's';
192 ret.invalid_mapping.auto_action = 's';
193 ret.no_version.auto_action = 's';
194 ret.wrong_version.auto_action = 's';
195 ret.invalid_hwm.auto_action = 's';
196 ret.commit.auto_action = 's';
199 if (opts->automatic) {
200 ret.invalid_record.auto_action = 'd'; /* delete */
201 ret.missing_reverse.auto_action = 'f'; /* fix */
202 ret.invalid_mapping.auto_action = 'd'; /* delete */
203 ret.no_version.auto_action = 'f'; /* fix */
204 ret.wrong_version.auto_action = 'a'; /* abort */
205 ret.invalid_hwm.auto_action = 'f'; /* fix */
206 ret.commit.auto_action = 'c'; /* commit */
207 ret.invalid_diff.auto_action = 'a'; /* abort */
208 if (opts->force) {
209 ret.wrong_version.auto_action = 'f'; /* fix */
210 ret.invalid_diff.auto_action = 'c'; /* commit */
213 if (opts->test) {
214 ret.invalid_diff.auto_action = 'c'; /* commit */
215 /* ret.commit.auto_action = 'c';*/ /* commit */
218 return ret;
221 static char get_action(struct check_action* a, struct record* r, TDB_DATA* v) {
222 char ret;
223 if (a->verbose && (r != NULL)) {
224 if (!a->fmt) {
225 d_printf("%s: %s ", a->name, print_data(r, r->key));
226 if (is_map(r)) {
227 d_printf("-> %s\n", print_data(r, r->val));
228 } else if (r->key_type == DT_HWM ||
229 r->key_type == DT_VER ||
230 r->key_type == DT_SEQ)
232 d_printf(": %ld\n", r->id);
233 } else {
234 d_printf("\n");
236 } else {
237 d_printf(a->fmt, a->name,
238 print_data(r, r->key),
239 print_data(r, r->val),
240 (v ? print_data(r, *v) : ""));
244 if (a->auto_action != '\0') {
245 return a->auto_action;
248 ret = interact_prompt(a->prompt, a->answers, a->default_action);
250 if (isupper(ret)) {
251 ret = tolower(ret);
252 a->auto_action = ret;
254 a->default_action = ret;
255 return ret;
258 /* *************************************************************************/
260 typedef struct {
261 TDB_DATA oval, nval;
262 } TDB_DATA_diff;
264 static TDB_DATA pack_diff(TDB_DATA_diff* diff) {
265 return (TDB_DATA) {
266 .dptr = (uint8_t *)diff,
267 .dsize = sizeof(TDB_DATA_diff),
271 static TDB_DATA_diff unpack_diff(TDB_DATA data) {
272 assert(data.dsize == sizeof(TDB_DATA_diff));
273 return *(TDB_DATA_diff*)data.dptr;
276 #define DEBUG_DIFF(LEV,MEM,MSG,KEY,OLD,NEW) \
277 DEBUG(LEV, ("%s: %s\n", MSG, print_data(MEM, KEY))); \
278 if (!is_empty(OLD)) { \
279 DEBUGADD(LEV, ("-%s\n", print_data(MEM, OLD))); \
281 if (!is_empty(NEW)) { \
282 DEBUGADD(LEV, ("+%s\n", print_data(MEM, NEW))); \
285 struct check_ctx {
286 int oflags;
287 char* name;
288 bool transaction;
289 struct db_context *db;
290 struct db_context *diff;
291 struct check_actions action;
293 uint32_t uid_hwm;
294 uint32_t gid_hwm;
296 unsigned n_invalid_record;
297 unsigned n_missing_reverse;
298 unsigned n_invalid_mappping;
299 unsigned n_map;
300 unsigned n_other;
301 unsigned n_diff;
302 struct check_options opts;
306 static void adjust_hwm(struct check_ctx* ctx, const struct record* r);
308 static int add_record(struct check_ctx* ctx, TDB_DATA key, TDB_DATA value)
310 NTSTATUS status;
311 TDB_DATA_diff diff;
312 TALLOC_CTX* mem = talloc_new(ctx->diff);
313 TDB_DATA recvalue;
314 struct db_record *rec = dbwrap_fetch_locked(ctx->diff, mem, key);
316 if (rec == NULL) {
317 return -1;
320 recvalue = dbwrap_record_get_value(rec);
322 if (recvalue.dptr == 0) { /* first entry */
323 status = dbwrap_fetch(ctx->db, ctx->diff, key, &diff.oval);
324 if (!NT_STATUS_IS_OK(status)) {
325 diff.oval = tdb_null;
327 } else {
328 diff = unpack_diff(recvalue);
329 talloc_free(diff.nval.dptr);
331 diff.nval = talloc_copy(ctx->diff, value);
333 DEBUG_DIFF(2, mem, "TDB DIFF", key, diff.oval, diff.nval);
335 status = dbwrap_record_store(rec, pack_diff(&diff), 0);
337 talloc_free(mem);
339 if (!NT_STATUS_IS_OK(status)) {
340 DEBUG(0, ("could not store record %s\n", nt_errstr(status)));
341 return -1;
343 ctx->n_diff ++;
344 return 0;
347 static int del_record(struct check_ctx* ctx, TDB_DATA key) {
348 return add_record(ctx, key, tdb_null);
351 static TDB_DATA
352 fetch_record(struct check_ctx* ctx, TALLOC_CTX* mem_ctx, TDB_DATA key)
354 TDB_DATA tmp;
355 NTSTATUS status;
357 status = dbwrap_fetch(ctx->diff, mem_ctx, key, &tmp);
359 if (NT_STATUS_IS_OK(status)) {
360 TDB_DATA_diff diff = unpack_diff(tmp);
361 TDB_DATA ret = talloc_copy(mem_ctx, diff.nval);
362 talloc_free(tmp.dptr);
363 return ret;
366 status = dbwrap_fetch(ctx->db, mem_ctx, key, &tmp);
367 if (!NT_STATUS_IS_OK(status)) {
368 return tdb_null;
371 return tmp;
374 static void edit_record(struct record* r) {
375 TALLOC_CTX* mem = talloc_new(r);
376 cbuf* ost = cbuf_new(mem);
377 const char* str;
378 struct record* nr;
379 TDB_DATA key;
380 TDB_DATA val;
381 cbuf_printf(ost, "%s %s\n",
382 print_data(mem, r->key), print_data(mem, r->val));
383 str = interact_edit(mem, cbuf_gets(ost, 0));
384 key = parse_data(mem, &str);
385 val = parse_data(mem, &str);
386 nr = parse_record(talloc_parent(r), key, val);
387 if (nr != NULL) {
388 *r = *nr;
390 talloc_free(mem);
393 static bool check_version(struct check_ctx* ctx) {
394 static const char* key = "IDMAP_VERSION";
395 uint32_t version;
396 NTSTATUS status;
397 char action = 's';
398 struct check_actions* act = &ctx->action;
400 status = dbwrap_fetch_uint32(ctx->db, key, &version);
401 if (!NT_STATUS_IS_OK(status)) {
402 d_printf("No version number, assume 2\n");
403 action = get_action(&act->no_version, NULL, NULL);
404 } else if (version != 2) {
405 d_printf("Wrong version number %d, should be 2\n", version);
406 action = get_action(&act->wrong_version, NULL, NULL);
408 switch (action) {
409 case 's':
410 break;
411 case 'f':
412 SIVAL(&version, 0, 2);
413 add_record(ctx, string_term_tdb_data(key),
414 make_tdb_data((uint8_t *)&version, sizeof(uint32_t)));
415 break;
416 case 'a':
417 return false;
418 default:
419 assert(false);
421 return true;
424 static void check_hwm(struct check_ctx* ctx, const char* key, uint32_t target) {
425 uint32_t hwm;
426 char action = 's';
427 NTSTATUS status;
428 struct check_actions* act = &ctx->action;
430 status = dbwrap_fetch_uint32(ctx->db, key, &hwm);
431 if (!NT_STATUS_IS_OK(status)) {
432 d_printf("No %s should be %d\n", key, target);
433 action = get_action(&act->invalid_hwm, NULL, NULL);
434 } else if (target < hwm) {
435 d_printf("Invalid %s %d: should be %d\n", key, hwm, target);
436 action = get_action(&act->invalid_hwm, NULL, NULL);
438 if (action == 'f') {
439 SIVAL(&hwm, 0, target);
440 add_record(ctx, string_term_tdb_data(key),
441 make_tdb_data((uint8_t *)&hwm, sizeof(uint32_t)));
445 int traverse_check(struct db_record *rec, void* data) {
446 struct check_ctx* ctx = (struct check_ctx*)data;
447 struct check_actions* act = &ctx->action;
448 TALLOC_CTX* mem = talloc_new(ctx->diff);
449 TDB_DATA key;
450 TDB_DATA value;
451 struct record *r;
452 char action = 's';
454 key = dbwrap_record_get_key(rec);
455 value = dbwrap_record_get_value(rec);
457 r = parse_record(mem, key, value);
459 if (is_invalid(r)) {
460 action = get_action(&act->invalid_record, r, NULL);
461 ctx->n_invalid_record++;
462 } else if (is_map(r)) {
463 TDB_DATA back = fetch_record(ctx, mem, r->val);
464 if (back.dptr == NULL) {
465 action = get_action(&act->missing_reverse, r, NULL);
466 ctx->n_missing_reverse++;
467 } else if (!tdb_data_equal(r->key, back)) {
468 action = get_action(&act->invalid_mapping, r, &back);
469 ctx->n_invalid_mappping++;
470 } else {
471 if (r->key_type == DT_SID) {
472 action = get_action(&act->valid_mapping, r, NULL);
473 ctx->n_map++;
474 } else {
475 action = get_action(&act->valid_mapping, NULL,
476 NULL);
479 adjust_hwm(ctx, r);
480 } else {
481 action = get_action(&act->valid_other, r, NULL);
482 ctx->n_other++;
485 while (action) {
486 switch (action) {
487 case 's': /* skip */
488 break;
489 case 'd': /* delete */
490 del_record(ctx, key);
491 break;
492 case 'f': /* add reverse mapping */
493 add_record(ctx, value, key);
494 break;
495 case 'e': /* edit */
496 edit_record(r);
497 action = 'o';
498 if (is_invalid(r)) {
499 action = get_action(&act->invalid_edit, r,NULL);
500 continue;
502 if (!tdb_data_equal(key, r->key)) {
503 TDB_DATA oval = fetch_record(ctx, mem, r->key);
504 if (!is_empty(oval) &&
505 !tdb_data_equal(oval, r->val))
507 action = get_action(&act->record_exists,
508 r, &oval);
509 if (action != 'o') {
510 continue;
514 if (is_map(r)) {
515 TDB_DATA okey = fetch_record(ctx, mem, r->val);
516 if (!is_empty(okey) &&
517 !tdb_data_equal(okey, r->key))
519 action = get_action(&act->record_exists,
520 reverse_record(r),
521 &okey);
524 continue;
525 case 'o': /* overwrite */
526 adjust_hwm(ctx, r);
527 if (!tdb_data_equal(key, r->key)) {
528 del_record(ctx, key);
530 add_record(ctx, r->key, r->val);
531 if (is_map(r)) {
532 add_record(ctx, r->val, r->key);
535 action = '\0';
538 talloc_free(mem);
540 return 0;
543 /******************************************************************************/
545 void adjust_hwm(struct check_ctx* ctx, const struct record* r) {
546 enum DT type = (r->key_type == DT_SID) ? r->val_type : r->key_type;
547 if (type == DT_UID) {
548 ctx->uid_hwm = MAX(ctx->uid_hwm, r->id);
549 } else if (type == DT_GID) {
550 ctx->gid_hwm = MAX(ctx->gid_hwm, r->id);
554 TDB_DATA talloc_copy(TALLOC_CTX* mem_ctx, TDB_DATA data) {
555 TDB_DATA ret = {
556 .dptr = (uint8_t *)talloc_size(mem_ctx, data.dsize+1),
557 .dsize = data.dsize
559 if (ret.dptr == NULL) {
560 ret.dsize = 0;
561 } else {
562 memcpy(ret.dptr, data.dptr, data.dsize);
563 ret.dptr[ret.dsize] = '\0';
565 return ret;
568 static bool is_cstr(TDB_DATA str) {
569 return !is_empty(str) && str.dptr[str.dsize-1] == '\0';
572 static bool parse_sid (TDB_DATA str, enum DT* type, struct dom_sid* sid) {
573 struct dom_sid tmp;
574 const char* s = (const char*)str.dptr;
575 if ((s[0] == 'S') && string_to_sid(&tmp, s)) {
576 *sid = tmp;
577 *type = DT_SID;
578 return true;
580 return false;
583 static bool parse_xid(TDB_DATA str, enum DT* type, unsigned long* id) {
584 char c, t;
585 unsigned long tmp;
586 if (sscanf((const char*)str.dptr, "%cID %lu%c", &c, &tmp, &t) == 2) {
587 if (c == 'U') {
588 *id = tmp;
589 *type = DT_UID;
590 return true;
591 } else if (c == 'G') {
592 *id = tmp;
593 *type = DT_GID;
594 return true;
597 return false;
601 struct record*
602 parse_record(TALLOC_CTX* mem_ctx, TDB_DATA key, TDB_DATA val)
604 struct record* ret = talloc_zero(mem_ctx, struct record);
605 if (ret == NULL) {
606 DEBUG(0, ("Out of memory.\n"));
607 return NULL;
609 ret->key = talloc_copy(ret, key);
610 ret->val = talloc_copy(ret, val);
611 if ((ret->key.dptr == NULL && key.dptr != NULL) ||
612 (ret->val.dptr == NULL && val.dptr != NULL))
614 talloc_free(ret);
615 DEBUG(0, ("Out of memory.\n"));
616 return NULL;
618 assert((ret->key_type == DT_INV) && (ret->val_type == DT_INV));
620 if (!is_cstr(key)) {
621 return ret;
623 if (parse_sid(key, &ret->key_type, &ret->sid)) {
624 parse_xid(val, &ret->val_type, &ret->id);
625 } else if (parse_xid(key, &ret->key_type, &ret->id)) {
626 if (is_cstr(val)) {
627 parse_sid(val, &ret->val_type, &ret->sid);
629 } else if (strcmp((const char*)key.dptr, "USER HWM") == 0) {
630 ret->key_type = DT_HWM;
631 if (val.dsize == 4) {
632 ret->id = IVAL(val.dptr,0);
633 ret->val_type = DT_UID;
635 } else if (strcmp((const char*)key.dptr, "GROUP HWM") == 0) {
636 ret->key_type = DT_HWM;
637 if (val.dsize == 4) {
638 ret->id = IVAL(val.dptr,0);
639 ret->val_type = DT_GID;
641 } else if (strcmp((const char*)key.dptr, "IDMAP_VERSION") == 0) {
642 ret->key_type = DT_VER;
643 if (val.dsize == 4) {
644 ret->id = IVAL(val.dptr,0);
645 ret->val_type = DT_VER;
647 } else if (strcmp((const char*)key.dptr, "__db_sequence_number__") == 0) {
648 ret->key_type = DT_SEQ;
649 if (val.dsize == 8) {
650 ret->id = *(uint64_t*)val.dptr;
651 ret->val_type = DT_SEQ;
655 return ret;
658 struct record* reverse_record(struct record* in)
660 return parse_record(talloc_parent(in), in->val, in->key);
664 /******************************************************************************/
667 char* print_data(TALLOC_CTX* mem_ctx, TDB_DATA d)
669 if (!is_empty(d)) {
670 char* ret = NULL;
671 cbuf* ost = cbuf_new(mem_ctx);
672 int len = cbuf_print_quoted(ost, (const char*)d.dptr, d.dsize);
673 if (len != -1) {
674 cbuf_swapptr(ost, &ret, 0);
675 talloc_steal(mem_ctx, ret);
677 talloc_free(ost);
678 return ret;
680 return talloc_strdup(mem_ctx, "<NULL>");
684 TDB_DATA parse_data(TALLOC_CTX* mem_ctx, const char** ptr) {
685 cbuf* ost = cbuf_new(mem_ctx);
686 TDB_DATA ret = tdb_null;
687 srprs_skipws(ptr);
688 if (srprs_quoted(ptr, ost)) {
689 ret.dsize = cbuf_getpos(ost);
690 ret.dptr = (uint8_t *)talloc_steal(mem_ctx, cbuf_gets(ost,0));
692 talloc_free(ost);
693 return ret;
696 static int traverse_print_diff(struct db_record *rec, void* data) {
697 struct check_ctx* ctx = (struct check_ctx*)data;
698 TDB_DATA key;
699 TDB_DATA value;
700 TDB_DATA_diff diff;
701 TALLOC_CTX* mem = talloc_new(ctx->diff);
703 key = dbwrap_record_get_key(rec);
704 value = dbwrap_record_get_value(rec);
705 diff = unpack_diff(value);
707 DEBUG_DIFF(0, mem, "DIFF", key, diff.oval, diff.nval);
709 talloc_free(mem);
710 return 0;
714 static int traverse_commit(struct db_record *diff_rec, void* data) {
715 struct check_ctx* ctx = (struct check_ctx*)data;
716 TDB_DATA key;
717 TDB_DATA diff_value;
718 TDB_DATA_diff diff;
719 TDB_DATA value;
720 TALLOC_CTX* mem = talloc_new(ctx->diff);
721 int ret = -1;
722 NTSTATUS status;
723 struct check_actions* act = &ctx->action;
724 struct db_record* rec;
726 key = dbwrap_record_get_key(diff_rec);
727 diff_value = dbwrap_record_get_value(diff_rec);
728 diff = unpack_diff(diff_value);
730 rec = dbwrap_fetch_locked(ctx->db, mem, key);
731 if (rec == NULL) {
732 goto done;
735 value = dbwrap_record_get_value(rec);
737 if (!tdb_data_equal(value, diff.oval)) {
738 char action;
740 d_printf("Warning: record has changed: %s\n"
741 "expected: %s got %s\n", print_data(mem, key),
742 print_data(mem, diff.oval),
743 print_data(mem, value));
745 action = get_action(&act->invalid_diff, NULL, NULL);
746 if (action == 's') {
747 ret = 0;
748 goto done;
749 } else if (action == 'a') {
750 goto done;
754 DEBUG_DIFF(0, mem, "Commit", key, diff.oval, diff.nval);
756 if (is_empty(diff.nval)) {
757 status = dbwrap_record_delete(rec);
758 } else {
759 status = dbwrap_record_store(rec, diff.nval, 0);
762 if (!NT_STATUS_IS_OK(status)) {
763 DEBUG(0, ("could not store record %s\n", nt_errstr(status)));
764 if (!ctx->opts.force) {
765 goto done;
768 ret = 0;
769 done:
770 talloc_free(mem);
771 return ret;
774 static struct check_ctx*
775 check_init(TALLOC_CTX* mem_ctx, const struct check_options* o)
777 struct check_ctx* ctx = talloc_zero(mem_ctx, struct check_ctx);
778 if (ctx == NULL) {
779 DEBUG(0, (_("No memory\n")));
780 return NULL;
783 ctx->diff = db_open_rbt(ctx);
784 if (ctx->diff == NULL) {
785 talloc_free(ctx);
786 DEBUG(0, (_("No memory\n")));
787 return NULL;
790 ctx->action = check_actions_init(o);
791 ctx->opts = *o;
792 return ctx;
795 static bool check_open_db(struct check_ctx* ctx, const char* name, int oflags)
797 if (name == NULL) {
798 d_fprintf(stderr, _("Error: name == NULL in check_open_db().\n"));
799 return false;
802 if (ctx->db != NULL) {
803 if ((ctx->oflags == oflags) && (strcmp(ctx->name, name))) {
804 return true;
805 } else {
806 TALLOC_FREE(ctx->db);
810 ctx->db = db_open(ctx, name, 0, TDB_DEFAULT, oflags, 0);
811 if (ctx->db == NULL) {
812 d_fprintf(stderr,
813 _("Could not open idmap db (%s) for writing: %s\n"),
814 name, strerror(errno));
815 return false;
818 if (ctx->name != name) {
819 TALLOC_FREE(ctx->name);
820 ctx->name = talloc_strdup(ctx, name);
823 ctx->oflags = oflags;
824 return true;
827 static bool check_do_checks(struct check_ctx* ctx)
829 NTSTATUS status;
831 if (!check_version(ctx)) {
832 return false;
835 status = dbwrap_traverse(ctx->db, traverse_check, ctx, NULL);
837 if (!NT_STATUS_IS_OK(status)) {
838 DEBUG(0, ("failed to traverse %s\n", ctx->name));
839 return false;
842 check_hwm(ctx, "USER HWM", ctx->uid_hwm + 1);
843 check_hwm(ctx, "GROUP HWM", ctx->gid_hwm + 1);
845 return true;
848 static void check_summary(const struct check_ctx* ctx)
850 d_printf("uid hwm: %d\ngid hwm: %d\n", ctx->uid_hwm, ctx->gid_hwm);
851 d_printf("mappings: %d\nother: %d\n", ctx->n_map, ctx->n_other);
852 d_printf("invalid records: %d\nmissing links: %d\ninvalid links: %d\n",
853 ctx->n_invalid_record, ctx->n_missing_reverse,
854 ctx->n_invalid_mappping);
855 d_printf("%u changes:\n", ctx->n_diff);
858 static bool check_transaction_start(struct check_ctx* ctx) {
859 return (dbwrap_transaction_start(ctx->db) == 0);
862 static bool check_transaction_commit(struct check_ctx* ctx) {
863 return (dbwrap_transaction_commit(ctx->db) == 0);
866 static bool check_transaction_cancel(struct check_ctx* ctx) {
867 return (dbwrap_transaction_cancel(ctx->db) == 0);
871 static void check_diff_list(struct check_ctx* ctx) {
872 NTSTATUS status = dbwrap_traverse(ctx->diff, traverse_print_diff, ctx, NULL);
874 if (!NT_STATUS_IS_OK(status)) {
875 DEBUG(0, ("failed to traverse diff\n"));
880 static bool check_commit(struct check_ctx* ctx)
882 struct check_actions* act = &ctx->action;
883 char action;
884 NTSTATUS status = NT_STATUS_OK;
886 check_summary(ctx);
888 if (ctx->n_diff == 0) {
889 return true;
892 while ((action = get_action(&act->commit, NULL, NULL)) == 'l') {
893 check_diff_list(ctx);
895 if (action == 's') {
896 return true;
898 assert(action == 'c');
900 if (!check_open_db(ctx, ctx->name, O_RDWR)) {
901 return false;
904 if (!check_transaction_start(ctx)) {
905 return false;
908 status = dbwrap_traverse(ctx->diff, traverse_commit, ctx, NULL);
910 if (!NT_STATUS_IS_OK(status)) {
911 check_transaction_cancel(ctx);
912 return false;
914 if (ctx->opts.test) { /*get_action? */
915 return check_transaction_cancel(ctx);
916 } else {
917 return check_transaction_commit(ctx);
921 int net_idmap_check_db(const char* db, const struct check_options* o)
923 int ret = -1;
924 TALLOC_CTX* mem_ctx = talloc_stackframe();
925 struct check_ctx* ctx = check_init(mem_ctx, o);
927 if (!o->automatic && !isatty(STDIN_FILENO)) {
928 DEBUG(0, ("Interactive use needs tty, use --auto\n"));
929 goto done;
931 if (o->lock) {
932 if (check_open_db(ctx, db, O_RDWR)
933 && check_transaction_start(ctx))
935 if ( check_do_checks(ctx)
936 && check_commit(ctx)
937 && check_transaction_commit(ctx))
939 ret = 0;
940 } else {
941 check_transaction_cancel(ctx);
944 } else {
945 if (check_open_db(ctx, db, O_RDONLY)
946 && check_do_checks(ctx)
947 && check_commit(ctx))
949 ret = 0;
952 done:
953 talloc_free(mem_ctx);
954 return ret;
958 /*Local Variables:*/
959 /*mode: c*/
960 /*End:*/