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 idmap database.
22 * @author Gregor Beck <gb@sernet.de>
26 #include "net_idmap_check.h"
28 #include "system/filesys.h"
29 #include "dbwrap/dbwrap.h"
30 #include "dbwrap/dbwrap_open.h"
31 #include "dbwrap/dbwrap_rbt.h"
33 #include "../libcli/security/dom_sid.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
);
46 /* record *********************************************************************/
50 DT_SID
, DT_UID
, DT_GID
,
51 DT_HWM
, DT_VER
, DT_SEQ
,
55 enum DT key_type
, val_type
;
61 static struct record
* parse_record(TALLOC_CTX
* ctx
, TDB_DATA key
, TDB_DATA val
);
62 static struct record
* reverse_record(struct record
* rec
);
64 static bool is_invalid(const struct record
* r
) {
65 return (r
->key_type
== DT_INV
) || (r
->val_type
== DT_INV
);
68 static bool is_map(const struct record
* r
) {
69 return (r
->key_type
== DT_SID
)
70 || (r
->key_type
== DT_UID
) || (r
->key_type
== DT_GID
);
73 /* action *********************************************************************/
75 typedef struct check_action
{
85 struct check_actions
{
86 check_action invalid_record
;
87 check_action missing_reverse
;
88 check_action invalid_mapping
;
89 check_action invalid_edit
;
90 check_action record_exists
;
91 check_action no_version
;
92 check_action wrong_version
;
93 check_action invalid_hwm
;
95 check_action valid_mapping
;
96 check_action valid_other
;
97 check_action invalid_diff
;
100 static struct check_actions
101 check_actions_init(const struct check_options
* opts
) {
102 struct check_actions ret
= {
103 .invalid_record
= (check_action
) {
104 .name
= "Invalid record",
105 .prompt
= "[e]dit/[d]elete/[D]elete all"
106 "/[s]kip/[S]kip all",
108 .default_action
= 'e',
111 .missing_reverse
= (check_action
) {
112 .name
= "Missing reverse mapping for",
113 .prompt
= "[f]ix/[F]ix all/[e]dit/[d]elete/[D]elete all"
114 "/[s]kip/[S]kip all",
116 .default_action
= 'f',
119 .invalid_mapping
= (check_action
) {
120 .fmt
= "%1$s: %2$s -> %3$s\n(%4$s <- %3$s)\n",
121 .name
= "Invalid mapping",
122 .prompt
= "[e]dit/[d]elete/[D]elete all"
123 "/[s]kip/[S]kip all",
125 .default_action
= 'd',
128 .invalid_edit
= (check_action
) {
129 .name
= "Invalid record",
130 .prompt
= "[e]dit/[d]elete/[D]elete all"
131 "/[s]kip/[S]kip all",
133 .default_action
= 'e',
136 .record_exists
= (check_action
) {
137 .fmt
= "%1$s: %2$s\n-%4$s\n+%3$s\n",
138 .name
= "Record exists",
139 .prompt
= "[o]verwrite/[O]verwrite all/[e]dit"
140 "/[d]elete/[D]elete all/[s]kip/[S]kip all",
142 .default_action
= 'o',
145 .no_version
= (check_action
) {
146 .prompt
= "[f]ix/[s]kip/[a]bort",
148 .default_action
= 'f',
150 .wrong_version
= (check_action
) {
151 .prompt
= "[f]ix/[s]kip/[a]bort",
153 .default_action
= 'a',
155 .invalid_hwm
= (check_action
) {
156 .prompt
= "[f]ix/[s]kip",
158 .default_action
= 'f',
160 .commit
= (check_action
) {
161 .prompt
= "[c]ommit/[l]ist/[s]kip",
163 .default_action
= 'l',
166 .valid_mapping
= (check_action
) {
167 .fmt
= "%1$s: %2$s <-> %3$s\n",
170 .verbose
= opts
->verbose
,
172 .valid_other
= (check_action
) {
175 .verbose
= opts
->verbose
,
177 .invalid_diff
= (check_action
) {
178 .prompt
= "[s]kip/[S]kip all/[c]ommit/[C]ommit all"
181 .default_action
= 's',
186 ret
.invalid_record
.auto_action
= 's';
187 ret
.missing_reverse
.auto_action
= 's';
188 ret
.invalid_mapping
.auto_action
= 's';
189 ret
.no_version
.auto_action
= 's';
190 ret
.wrong_version
.auto_action
= 's';
191 ret
.invalid_hwm
.auto_action
= 's';
192 ret
.commit
.auto_action
= 's';
195 if (opts
->automatic
) {
196 ret
.invalid_record
.auto_action
= 'd'; /* delete */
197 ret
.missing_reverse
.auto_action
= 'f'; /* fix */
198 ret
.invalid_mapping
.auto_action
= 'd'; /* delete */
199 ret
.no_version
.auto_action
= 'f'; /* fix */
200 ret
.wrong_version
.auto_action
= 'a'; /* abort */
201 ret
.invalid_hwm
.auto_action
= 'f'; /* fix */
202 ret
.commit
.auto_action
= 'c'; /* commit */
203 ret
.invalid_diff
.auto_action
= 'a'; /* abort */
205 ret
.wrong_version
.auto_action
= 'f'; /* fix */
206 ret
.invalid_diff
.auto_action
= 'c'; /* commit */
210 ret
.invalid_diff
.auto_action
= 'c'; /* commit */
211 /* ret.commit.auto_action = 'c';*/ /* commit */
217 static char get_action(struct check_action
* a
, struct record
* r
, TDB_DATA
* v
) {
219 if (a
->verbose
&& (r
!= NULL
)) {
221 d_printf("%s: %s ", a
->name
, print_data(r
, r
->key
));
223 d_printf("-> %s\n", print_data(r
, r
->val
));
224 } else if (r
->key_type
== DT_HWM
||
225 r
->key_type
== DT_VER
||
226 r
->key_type
== DT_SEQ
)
228 d_printf(": %ld\n", r
->id
);
233 d_printf(a
->fmt
, a
->name
,
234 print_data(r
, r
->key
),
235 print_data(r
, r
->val
),
236 (v
? print_data(r
, *v
) : ""));
240 if (a
->auto_action
!= '\0') {
241 return a
->auto_action
;
244 ret
= interact_prompt(a
->prompt
, a
->answers
, a
->default_action
);
248 a
->auto_action
= ret
;
250 a
->default_action
= ret
;
254 /* *************************************************************************/
260 static TDB_DATA
pack_diff(TDB_DATA_diff
* diff
) {
262 .dptr
= (uint8_t *)diff
,
263 .dsize
= sizeof(TDB_DATA_diff
),
267 static TDB_DATA_diff
unpack_diff(TDB_DATA data
) {
268 assert(data
.dsize
== sizeof(TDB_DATA_diff
));
269 return *(TDB_DATA_diff
*)data
.dptr
;
272 #define DEBUG_DIFF(LEV,MEM,MSG,KEY,OLD,NEW) \
273 DEBUG(LEV, ("%s: %s\n", MSG, print_data(MEM, KEY))); \
274 if (!tdb_data_is_empty(OLD)) { \
275 DEBUGADD(LEV, ("-%s\n", print_data(MEM, OLD))); \
277 if (!tdb_data_is_empty(NEW)) { \
278 DEBUGADD(LEV, ("+%s\n", print_data(MEM, NEW))); \
285 struct db_context
*db
;
286 struct db_context
*diff
;
287 struct check_actions action
;
292 unsigned n_invalid_record
;
293 unsigned n_missing_reverse
;
294 unsigned n_invalid_mappping
;
298 struct check_options opts
;
302 static void adjust_hwm(struct check_ctx
* ctx
, const struct record
* r
);
304 static int add_record(struct check_ctx
* ctx
, TDB_DATA key
, TDB_DATA value
)
308 TALLOC_CTX
* mem
= talloc_new(ctx
->diff
);
310 struct db_record
*rec
= dbwrap_fetch_locked(ctx
->diff
, mem
, key
);
316 recvalue
= dbwrap_record_get_value(rec
);
318 if (recvalue
.dptr
== 0) { /* first entry */
319 status
= dbwrap_fetch(ctx
->db
, ctx
->diff
, key
, &diff
.oval
);
320 if (!NT_STATUS_IS_OK(status
)) {
321 diff
.oval
= tdb_null
;
324 diff
= unpack_diff(recvalue
);
325 talloc_free(diff
.nval
.dptr
);
327 diff
.nval
= tdb_data_talloc_copy(ctx
->diff
, value
);
329 DEBUG_DIFF(2, mem
, "TDB DIFF", key
, diff
.oval
, diff
.nval
);
331 status
= dbwrap_record_store(rec
, pack_diff(&diff
), 0);
335 if (!NT_STATUS_IS_OK(status
)) {
336 DEBUG(0, ("could not store record %s\n", nt_errstr(status
)));
343 static int del_record(struct check_ctx
* ctx
, TDB_DATA key
) {
344 return add_record(ctx
, key
, tdb_null
);
348 fetch_record(struct check_ctx
* ctx
, TALLOC_CTX
* mem_ctx
, TDB_DATA key
)
353 status
= dbwrap_fetch(ctx
->diff
, mem_ctx
, key
, &tmp
);
355 if (NT_STATUS_IS_OK(status
)) {
356 TDB_DATA_diff diff
= unpack_diff(tmp
);
357 TDB_DATA ret
= tdb_data_talloc_copy(mem_ctx
, diff
.nval
);
358 talloc_free(tmp
.dptr
);
362 status
= dbwrap_fetch(ctx
->db
, mem_ctx
, key
, &tmp
);
363 if (!NT_STATUS_IS_OK(status
)) {
370 static void edit_record(struct record
* r
) {
371 TALLOC_CTX
* mem
= talloc_new(r
);
372 cbuf
* ost
= cbuf_new(mem
);
377 cbuf_printf(ost
, "%s %s\n",
378 print_data(mem
, r
->key
), print_data(mem
, r
->val
));
379 str
= interact_edit(mem
, cbuf_gets(ost
, 0));
380 key
= parse_data(mem
, &str
);
381 val
= parse_data(mem
, &str
);
382 nr
= parse_record(talloc_parent(r
), key
, val
);
389 static bool check_version(struct check_ctx
* ctx
) {
390 static const char* key
= "IDMAP_VERSION";
394 struct check_actions
* act
= &ctx
->action
;
396 status
= dbwrap_fetch_uint32_bystring(ctx
->db
, key
, &version
);
397 if (!NT_STATUS_IS_OK(status
)) {
398 d_printf("No version number, assume 2\n");
399 action
= get_action(&act
->no_version
, NULL
, NULL
);
400 } else if (version
!= 2) {
401 d_printf("Wrong version number %d, should be 2\n", version
);
402 action
= get_action(&act
->wrong_version
, NULL
, NULL
);
408 SIVAL(&version
, 0, 2);
409 add_record(ctx
, string_term_tdb_data(key
),
410 make_tdb_data((uint8_t *)&version
, sizeof(uint32_t)));
420 static void check_hwm(struct check_ctx
* ctx
, const char* key
, uint32_t target
) {
424 struct check_actions
* act
= &ctx
->action
;
426 status
= dbwrap_fetch_uint32_bystring(ctx
->db
, key
, &hwm
);
427 if (!NT_STATUS_IS_OK(status
)) {
428 d_printf("No %s should be %d\n", key
, target
);
429 action
= get_action(&act
->invalid_hwm
, NULL
, NULL
);
430 } else if (target
< hwm
) {
431 d_printf("Invalid %s %d: should be %d\n", key
, hwm
, target
);
432 action
= get_action(&act
->invalid_hwm
, NULL
, NULL
);
435 SIVAL(&hwm
, 0, target
);
436 add_record(ctx
, string_term_tdb_data(key
),
437 make_tdb_data((uint8_t *)&hwm
, sizeof(uint32_t)));
441 int traverse_check(struct db_record
*rec
, void* data
) {
442 struct check_ctx
* ctx
= (struct check_ctx
*)data
;
443 struct check_actions
* act
= &ctx
->action
;
444 TALLOC_CTX
* mem
= talloc_new(ctx
->diff
);
450 key
= dbwrap_record_get_key(rec
);
451 value
= dbwrap_record_get_value(rec
);
453 r
= parse_record(mem
, key
, value
);
456 action
= get_action(&act
->invalid_record
, r
, NULL
);
457 ctx
->n_invalid_record
++;
458 } else if (is_map(r
)) {
459 TDB_DATA back
= fetch_record(ctx
, mem
, r
->val
);
460 if (back
.dptr
== NULL
) {
461 action
= get_action(&act
->missing_reverse
, r
, NULL
);
462 ctx
->n_missing_reverse
++;
463 } else if (!tdb_data_equal(r
->key
, back
)) {
464 action
= get_action(&act
->invalid_mapping
, r
, &back
);
465 ctx
->n_invalid_mappping
++;
467 if (r
->key_type
== DT_SID
) {
468 action
= get_action(&act
->valid_mapping
, r
, NULL
);
471 action
= get_action(&act
->valid_mapping
, NULL
,
477 action
= get_action(&act
->valid_other
, r
, NULL
);
485 case 'd': /* delete */
486 del_record(ctx
, key
);
488 case 'f': /* add reverse mapping */
489 add_record(ctx
, value
, key
);
495 action
= get_action(&act
->invalid_edit
, r
,NULL
);
498 if (!tdb_data_equal(key
, r
->key
)) {
499 TDB_DATA oval
= fetch_record(ctx
, mem
, r
->key
);
500 if (!tdb_data_is_empty(oval
) &&
501 !tdb_data_equal(oval
, r
->val
))
503 action
= get_action(&act
->record_exists
,
511 TDB_DATA okey
= fetch_record(ctx
, mem
, r
->val
);
512 if (!tdb_data_is_empty(okey
) &&
513 !tdb_data_equal(okey
, r
->key
))
515 action
= get_action(&act
->record_exists
,
521 case 'o': /* overwrite */
523 if (!tdb_data_equal(key
, r
->key
)) {
524 del_record(ctx
, key
);
526 add_record(ctx
, r
->key
, r
->val
);
528 add_record(ctx
, r
->val
, r
->key
);
539 /******************************************************************************/
541 void adjust_hwm(struct check_ctx
* ctx
, const struct record
* r
) {
542 enum DT type
= (r
->key_type
== DT_SID
) ? r
->val_type
: r
->key_type
;
543 if (type
== DT_UID
) {
544 ctx
->uid_hwm
= MAX(ctx
->uid_hwm
, r
->id
);
545 } else if (type
== DT_GID
) {
546 ctx
->gid_hwm
= MAX(ctx
->gid_hwm
, r
->id
);
550 static bool is_cstr(TDB_DATA str
) {
551 return !tdb_data_is_empty(str
) && str
.dptr
[str
.dsize
-1] == '\0';
554 static bool parse_sid (TDB_DATA str
, enum DT
* type
, struct dom_sid
* sid
) {
556 const char* s
= (const char*)str
.dptr
;
557 if ((s
[0] == 'S') && string_to_sid(&tmp
, s
)) {
565 static bool parse_xid(TDB_DATA str
, enum DT
* type
, unsigned long* id
) {
568 if (sscanf((const char*)str
.dptr
, "%cID %lu%c", &c
, &tmp
, &t
) == 2) {
573 } else if (c
== 'G') {
584 parse_record(TALLOC_CTX
* mem_ctx
, TDB_DATA key
, TDB_DATA val
)
586 struct record
* ret
= talloc_zero(mem_ctx
, struct record
);
588 DEBUG(0, ("Out of memory.\n"));
591 ret
->key
= tdb_data_talloc_copy(ret
, key
);
592 ret
->val
= tdb_data_talloc_copy(ret
, val
);
593 if ((ret
->key
.dptr
== NULL
&& key
.dptr
!= NULL
) ||
594 (ret
->val
.dptr
== NULL
&& val
.dptr
!= NULL
))
597 DEBUG(0, ("Out of memory.\n"));
600 assert((ret
->key_type
== DT_INV
) && (ret
->val_type
== DT_INV
));
605 if (parse_sid(key
, &ret
->key_type
, &ret
->sid
)) {
606 parse_xid(val
, &ret
->val_type
, &ret
->id
);
607 } else if (parse_xid(key
, &ret
->key_type
, &ret
->id
)) {
609 parse_sid(val
, &ret
->val_type
, &ret
->sid
);
611 } else if (strcmp((const char*)key
.dptr
, "USER HWM") == 0) {
612 ret
->key_type
= DT_HWM
;
613 if (val
.dsize
== 4) {
614 ret
->id
= IVAL(val
.dptr
,0);
615 ret
->val_type
= DT_UID
;
617 } else if (strcmp((const char*)key
.dptr
, "GROUP HWM") == 0) {
618 ret
->key_type
= DT_HWM
;
619 if (val
.dsize
== 4) {
620 ret
->id
= IVAL(val
.dptr
,0);
621 ret
->val_type
= DT_GID
;
623 } else if (strcmp((const char*)key
.dptr
, "IDMAP_VERSION") == 0) {
624 ret
->key_type
= DT_VER
;
625 if (val
.dsize
== 4) {
626 ret
->id
= IVAL(val
.dptr
,0);
627 ret
->val_type
= DT_VER
;
629 } else if (strcmp((const char*)key
.dptr
, "__db_sequence_number__") == 0) {
630 ret
->key_type
= DT_SEQ
;
631 if (val
.dsize
== 8) {
632 ret
->id
= *(uint64_t*)val
.dptr
;
633 ret
->val_type
= DT_SEQ
;
640 struct record
* reverse_record(struct record
* in
)
642 return parse_record(talloc_parent(in
), in
->val
, in
->key
);
646 /******************************************************************************/
649 char* print_data(TALLOC_CTX
* mem_ctx
, TDB_DATA d
)
651 if (!tdb_data_is_empty(d
)) {
653 cbuf
* ost
= cbuf_new(mem_ctx
);
654 int len
= cbuf_print_quoted(ost
, (const char*)d
.dptr
, d
.dsize
);
656 cbuf_swapptr(ost
, &ret
, 0);
657 talloc_steal(mem_ctx
, ret
);
662 return talloc_strdup(mem_ctx
, "<NULL>");
666 TDB_DATA
parse_data(TALLOC_CTX
* mem_ctx
, const char** ptr
) {
667 cbuf
* ost
= cbuf_new(mem_ctx
);
668 TDB_DATA ret
= tdb_null
;
670 if (srprs_quoted(ptr
, ost
)) {
671 ret
.dsize
= cbuf_getpos(ost
);
672 ret
.dptr
= (uint8_t *)talloc_steal(mem_ctx
, cbuf_gets(ost
,0));
678 static int traverse_print_diff(struct db_record
*rec
, void* data
) {
679 struct check_ctx
* ctx
= (struct check_ctx
*)data
;
683 TALLOC_CTX
* mem
= talloc_new(ctx
->diff
);
685 key
= dbwrap_record_get_key(rec
);
686 value
= dbwrap_record_get_value(rec
);
687 diff
= unpack_diff(value
);
689 DEBUG_DIFF(0, mem
, "DIFF", key
, diff
.oval
, diff
.nval
);
696 static int traverse_commit(struct db_record
*diff_rec
, void* data
) {
697 struct check_ctx
* ctx
= (struct check_ctx
*)data
;
702 TALLOC_CTX
* mem
= talloc_new(ctx
->diff
);
705 struct check_actions
* act
= &ctx
->action
;
706 struct db_record
* rec
;
708 key
= dbwrap_record_get_key(diff_rec
);
709 diff_value
= dbwrap_record_get_value(diff_rec
);
710 diff
= unpack_diff(diff_value
);
712 rec
= dbwrap_fetch_locked(ctx
->db
, mem
, key
);
717 value
= dbwrap_record_get_value(rec
);
719 if (!tdb_data_equal(value
, diff
.oval
)) {
722 d_printf("Warning: record has changed: %s\n"
723 "expected: %s got %s\n", print_data(mem
, key
),
724 print_data(mem
, diff
.oval
),
725 print_data(mem
, value
));
727 action
= get_action(&act
->invalid_diff
, NULL
, NULL
);
731 } else if (action
== 'a') {
736 DEBUG_DIFF(0, mem
, "Commit", key
, diff
.oval
, diff
.nval
);
738 if (tdb_data_is_empty(diff
.nval
)) {
739 status
= dbwrap_record_delete(rec
);
741 status
= dbwrap_record_store(rec
, diff
.nval
, 0);
744 if (!NT_STATUS_IS_OK(status
)) {
745 DEBUG(0, ("could not store record %s\n", nt_errstr(status
)));
746 if (!ctx
->opts
.force
) {
756 static struct check_ctx
*
757 check_init(TALLOC_CTX
* mem_ctx
, const struct check_options
* o
)
759 struct check_ctx
* ctx
= talloc_zero(mem_ctx
, struct check_ctx
);
761 DEBUG(0, (_("No memory\n")));
765 ctx
->diff
= db_open_rbt(ctx
);
766 if (ctx
->diff
== NULL
) {
768 DEBUG(0, (_("No memory\n")));
772 ctx
->action
= check_actions_init(o
);
777 static bool check_open_db(struct check_ctx
* ctx
, const char* name
, int oflags
)
780 d_fprintf(stderr
, _("Error: name == NULL in check_open_db().\n"));
784 if (ctx
->db
!= NULL
) {
785 if ((ctx
->oflags
== oflags
) && (strcmp(ctx
->name
, name
))) {
788 TALLOC_FREE(ctx
->db
);
792 ctx
->db
= db_open(ctx
, name
, 0, TDB_DEFAULT
, oflags
, 0,
793 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
794 if (ctx
->db
== NULL
) {
796 _("Could not open idmap db (%s) for writing: %s\n"),
797 name
, strerror(errno
));
801 if (ctx
->name
!= name
) {
802 TALLOC_FREE(ctx
->name
);
803 ctx
->name
= talloc_strdup(ctx
, name
);
806 ctx
->oflags
= oflags
;
810 static bool check_do_checks(struct check_ctx
* ctx
)
814 if (!check_version(ctx
)) {
818 status
= dbwrap_traverse(ctx
->db
, traverse_check
, ctx
, NULL
);
820 if (!NT_STATUS_IS_OK(status
)) {
821 DEBUG(0, ("failed to traverse %s\n", ctx
->name
));
825 check_hwm(ctx
, "USER HWM", ctx
->uid_hwm
+ 1);
826 check_hwm(ctx
, "GROUP HWM", ctx
->gid_hwm
+ 1);
831 static void check_summary(const struct check_ctx
* ctx
)
833 d_printf("uid hwm: %d\ngid hwm: %d\n", ctx
->uid_hwm
, ctx
->gid_hwm
);
834 d_printf("mappings: %d\nother: %d\n", ctx
->n_map
, ctx
->n_other
);
835 d_printf("invalid records: %d\nmissing links: %d\ninvalid links: %d\n",
836 ctx
->n_invalid_record
, ctx
->n_missing_reverse
,
837 ctx
->n_invalid_mappping
);
838 d_printf("%u changes:\n", ctx
->n_diff
);
841 static bool check_transaction_start(struct check_ctx
* ctx
) {
842 return (dbwrap_transaction_start(ctx
->db
) == 0);
845 static bool check_transaction_commit(struct check_ctx
* ctx
) {
846 return (dbwrap_transaction_commit(ctx
->db
) == 0);
849 static bool check_transaction_cancel(struct check_ctx
* ctx
) {
850 return (dbwrap_transaction_cancel(ctx
->db
) == 0);
854 static void check_diff_list(struct check_ctx
* ctx
) {
855 NTSTATUS status
= dbwrap_traverse(ctx
->diff
, traverse_print_diff
, ctx
, NULL
);
857 if (!NT_STATUS_IS_OK(status
)) {
858 DEBUG(0, ("failed to traverse diff\n"));
863 static bool check_commit(struct check_ctx
* ctx
)
865 struct check_actions
* act
= &ctx
->action
;
867 NTSTATUS status
= NT_STATUS_OK
;
871 if (ctx
->n_diff
== 0) {
875 while ((action
= get_action(&act
->commit
, NULL
, NULL
)) == 'l') {
876 check_diff_list(ctx
);
881 assert(action
== 'c');
883 if (!check_open_db(ctx
, ctx
->name
, O_RDWR
)) {
887 if (!check_transaction_start(ctx
)) {
891 status
= dbwrap_traverse(ctx
->diff
, traverse_commit
, ctx
, NULL
);
893 if (!NT_STATUS_IS_OK(status
)) {
894 check_transaction_cancel(ctx
);
897 if (ctx
->opts
.test
) { /*get_action? */
898 return check_transaction_cancel(ctx
);
900 return check_transaction_commit(ctx
);
904 int net_idmap_check_db(const char* db
, const struct check_options
* o
)
907 TALLOC_CTX
* mem_ctx
= talloc_stackframe();
908 struct check_ctx
* ctx
= check_init(mem_ctx
, o
);
910 if (!o
->automatic
&& !isatty(STDIN_FILENO
)) {
911 DEBUG(0, ("Interactive use needs tty, use --auto\n"));
915 if (check_open_db(ctx
, db
, O_RDWR
)
916 && check_transaction_start(ctx
))
918 if ( check_do_checks(ctx
)
920 && check_transaction_commit(ctx
))
924 check_transaction_cancel(ctx
);
928 if (check_open_db(ctx
, db
, O_RDONLY
)
929 && check_do_checks(ctx
)
930 && check_commit(ctx
))
936 talloc_free(mem_ctx
);