summaryrefslogtreecommitdiff
path: root/source3/utils/net_serverid.c
blob: 115147586eda273f465314d49a8d4cf867436167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/*
   Samba Unix/Linux SMB client library
   net serverid commands
   Copyright (C) Volker Lendecke 2010

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "utils/net.h"
#include "lib/util/server_id.h"
#include "dbwrap/dbwrap.h"
#include "dbwrap/dbwrap_rbt.h"
#include "serverid.h"
#include "session.h"
#include "smbd/globals.h"
#include "source3/smbd/smbXsrv_session.h"
#include "smbd/smbXsrv_open.h"
#include "util_tdb.h"
#include "librpc/gen_ndr/ndr_open_files.h"
#include "librpc/gen_ndr/ndr_smbXsrv.h"

struct wipedbs_record_marker {
	struct wipedbs_record_marker *prev, *next;
	TDB_DATA key, val;
	const char *desc;
};

struct wipedbs_server_data {
	struct server_id server_id;
	const char *server_id_str;
	bool exists;
	struct wipedbs_record_marker *session_records;
	struct wipedbs_record_marker *tcon_records;
	struct wipedbs_record_marker *open_records;
};

struct wipedbs_state {
	struct db_context *id2server_data;
	struct db_context *open_records_db;
	struct db_context *replay_records_db;
	struct {
		struct {
			int total;
			int existing;
			int disconnected;
		} server;
		struct {
			int total;
			int disconnected;
			int todelete;
			int failure;
		} session, tcon, open, replay;
		int open_timed_out;
	} stat;
	struct server_id *server_ids;
	bool *server_exists;
	int idx;
	struct db_context *session_db;
	struct db_context *tcon_db;
	struct db_context *open_db;
	struct timeval now;
	bool testmode;
	bool verbose;
};

static struct wipedbs_server_data *get_server_data(struct wipedbs_state *state,
						   const struct server_id *id)
{
	struct wipedbs_server_data *ret = NULL;
	TDB_DATA key, val = tdb_null;
	NTSTATUS status;

	key = make_tdb_data((const void*)&id->unique_id, sizeof(id->unique_id));
	status = dbwrap_fetch(state->id2server_data, talloc_tos(), key, &val);
	if (NT_STATUS_IS_OK(status)) {
		ret = *(struct wipedbs_server_data**) val.dptr;
		TALLOC_FREE(val.dptr);
	} else if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
		struct server_id_buf idbuf;

		server_id_str_buf(*id, &idbuf);

		ret = talloc_zero(state->id2server_data,
				  struct wipedbs_server_data);
		if (ret == NULL) {
			DEBUG(0, ("Failed to allocate server entry for %s\n",
				  idbuf.buf));
			goto done;
		}
		ret->server_id = *id;
		ret->server_id_str = talloc_strdup(ret, idbuf.buf);
		ret->exists = true;
		val = make_tdb_data((const void*)&ret, sizeof(ret));
		status = dbwrap_store(state->id2server_data,
				      key, val, TDB_INSERT);
		if (!NT_STATUS_IS_OK(status)) {
			DEBUG(0, ("Failed to store server entry for %s: %s\n",
				  idbuf.buf, nt_errstr(status)));
		}
		goto done;
	} else {
		struct server_id_buf idbuf;
		DEBUG(0, ("Failed to fetch server entry for %s: %s\n",
			  server_id_str_buf(*id, &idbuf), nt_errstr(status)));
		goto done;
	}
	if (!server_id_equal(id, &ret->server_id)) {
		struct server_id_buf idbuf1, idbuf2;
		DEBUG(0, ("uniq id collision for %s and %s\n",
			  server_id_str_buf(*id, &idbuf1),
			  server_id_str_buf(ret->server_id, &idbuf2)));
		smb_panic("server_id->unique_id not unique!");
	}
done:
	return ret;
}

static int wipedbs_traverse_sessions(struct smbXsrv_session_global0 *session,
				     void *wipedbs_state)
{
	struct wipedbs_state *state =
		talloc_get_type_abort(wipedbs_state,
		struct wipedbs_state);
	struct wipedbs_server_data *sd;
	struct wipedbs_record_marker *rec;
	TDB_DATA tmp;
	int ret = -1;

	assert(session->num_channels == 1);

	state->stat.session.total++;

	sd = get_server_data(state, &session->channels[0].server_id);
	if (sd == NULL) {
		goto done;
	}

	if (server_id_is_disconnected(&sd->server_id)) {
		state->stat.session.disconnected++;
	}

	rec = talloc_zero(sd, struct wipedbs_record_marker);
	if (rec == NULL) {
		DEBUG(0, ("Out of memory!\n"));
		goto done;
	}

	tmp = dbwrap_record_get_key(session->db_rec);
	rec->key = tdb_data_talloc_copy(rec, tmp);
	tmp = dbwrap_record_get_value(session->db_rec);
	rec->val = tdb_data_talloc_copy(rec, tmp);

	rec->desc = talloc_asprintf(
		rec, "session[global: %u wire: %llu]",
		session->session_global_id,
		(long long unsigned)session->session_wire_id);

	if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
	    (rec->desc == NULL))
	{
		DEBUG(0, ("Out of memory!\n"));
		goto done;
	}

	state->session_db = dbwrap_record_get_db(session->db_rec);

	DLIST_ADD(sd->session_records, rec);
	ret = 0;
done:
	return ret;
}

static int wipedbs_traverse_tcon(struct smbXsrv_tcon_global0 *tcon,
				 void *wipedbs_state)
{
	struct wipedbs_state *state =
		talloc_get_type_abort(wipedbs_state,
		struct wipedbs_state);
	struct wipedbs_server_data *sd;
	struct wipedbs_record_marker *rec;
	TDB_DATA tmp;
	int ret = -1;

	state->stat.tcon.total++;

	sd = get_server_data(state, &tcon->server_id);
	if (sd == NULL) {
		goto done;
	}

	if (server_id_is_disconnected(&sd->server_id)) {
		state->stat.tcon.disconnected++;
	}

	rec = talloc_zero(sd, struct wipedbs_record_marker);
	if (rec == NULL) {
		DEBUG(0, ("Out of memory!\n"));
		goto done;
	}

	tmp = dbwrap_record_get_key(tcon->db_rec);
	rec->key = tdb_data_talloc_copy(rec, tmp);
	tmp = dbwrap_record_get_value(tcon->db_rec);
	rec->val = tdb_data_talloc_copy(rec, tmp);

	rec->desc = talloc_asprintf(
		rec, "tcon[global: %u wire: %u session: %u share: %s]",
		tcon->tcon_global_id, tcon->tcon_wire_id,
		tcon->session_global_id, tcon->share_name);

	if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
	    (rec->desc == NULL))
	{
		DEBUG(0, ("Out of memory!\n"));
		goto done;
	}

	state->tcon_db = dbwrap_record_get_db(tcon->db_rec);

	DLIST_ADD(sd->tcon_records, rec);
	ret = 0;

done:
	return ret;
}

static int wipedbs_traverse_open_replay(struct db_record *db_rec,
					TDB_DATA *rc_open_global_key,
					struct wipedbs_state *state)
{
	struct wipedbs_record_marker *rec = NULL;
	TDB_DATA tmp;
	NTSTATUS status;
	int ret = -1;

	state->stat.replay.total++;

	rec = talloc_zero(state, struct wipedbs_record_marker);
	if (rec == NULL) {
		DBG_ERR("Out of memory!\n");
		goto done;
	}

	tmp = dbwrap_record_get_key(db_rec);
	rec->key = tdb_data_talloc_copy(rec, tmp);
	tmp = dbwrap_record_get_value(db_rec);
	rec->val = tdb_data_talloc_copy(rec, tmp);

	rec->desc = talloc_asprintf(
		rec,
		"replay rec [global key: %s]",
		hex_encode_talloc(rec,
				  rc_open_global_key->dptr,
				  rc_open_global_key->dsize));

	if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
	    (rec->desc == NULL))
	{
		DEBUG(0, ("Out of memory!\n"));
		goto done;
	}

	tmp = make_tdb_data((const void*)&rec, sizeof(rec));

	status = dbwrap_store(state->replay_records_db,
			      rec->key,
			      tmp,
			      TDB_INSERT);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("Failed to store replay record for %s: %s\n",
			rec->desc, nt_errstr(status));
		ret = -1;
		goto done;
	}
	ret = 0;

	state->open_db = dbwrap_record_get_db(db_rec);

done:
	return ret;
}

static int wipedbs_traverse_open(struct db_record *db_rec,
				 struct smbXsrv_open_global0 *open,
				 TDB_DATA *rc_open_global_key,
				 void *wipedbs_state)
{
	struct wipedbs_state *state =
		talloc_get_type_abort(wipedbs_state,
		struct wipedbs_state);
	struct wipedbs_server_data *sd;
	struct wipedbs_record_marker *rec;
	TDB_DATA val;
	TDB_DATA tmp;
	NTSTATUS status;
	int ret = -1;

	if (rc_open_global_key != NULL) {
		return wipedbs_traverse_open_replay(db_rec,
						    rc_open_global_key,
						    state);
	}

	state->stat.open.total++;

	sd = get_server_data(state, &open->server_id);
	if (sd == NULL) {
		goto done;
	}

	if (server_id_is_disconnected(&sd->server_id)) {
		struct timeval disconnect_time;
		int64_t tdiff;
		bool reached;

		state->stat.open.disconnected++;

		nttime_to_timeval(&disconnect_time, open->disconnect_time);
		tdiff = usec_time_diff(&state->now, &disconnect_time);
		reached = (tdiff >= INT64_C(1000)*open->durable_timeout_msec);

		if (state->verbose) {
			TALLOC_CTX *mem_ctx = talloc_new(talloc_tos());
			enum ndr_err_code ndr_err;
			struct vfs_default_durable_cookie cookie;

			ndr_err = ndr_pull_struct_blob(
				&open->backend_cookie, mem_ctx, &cookie,
				(ndr_pull_flags_fn_t)ndr_pull_vfs_default_durable_cookie);
			if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
				d_printf("ndr_pull_struct_blob failed\n");
				ret = -1;
				goto done;
			}

			d_printf("open[%s/%s id: 0x%" PRIx32 "] disconnected at "
				 "[%s] %us ago with timeout of %us "
				 "-%s reached\n",
				 cookie.servicepath, cookie.base_name,
				 open->open_global_id,
				 nt_time_string(mem_ctx, open->disconnect_time),
				 (unsigned)(tdiff/1000000),
				 open->durable_timeout_msec / 1000,
				 reached ? "" : " not");
			talloc_free(mem_ctx);
		}

		if (!reached) {
			ret = 0;
			goto done;
		}
		state->stat.open_timed_out++;
	}

	rec = talloc_zero(sd, struct wipedbs_record_marker);
	if (rec == NULL) {
		DEBUG(0, ("Out of memory!\n"));
		goto done;
	}

	tmp = dbwrap_record_get_key(db_rec);
	rec->key = tdb_data_talloc_copy(rec, tmp);
	tmp = dbwrap_record_get_value(db_rec);
	rec->val = tdb_data_talloc_copy(rec, tmp);

	rec->desc = talloc_asprintf(
		rec, "open[global: %u persistent: %llu volatile: %llu]",
		open->open_global_id,
		(long long unsigned)open->open_persistent_id,
		(long long unsigned)open->open_volatile_id);

	if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
	    (rec->desc == NULL))
	{
		DEBUG(0, ("Out of memory!\n"));
		goto done;
	}

	state->open_db = dbwrap_record_get_db(db_rec);

	DLIST_ADD(sd->open_records, rec);

	val = make_tdb_data((const void*)&rec, sizeof(rec));

	status = dbwrap_store(state->open_records_db, rec->key, val, TDB_INSERT);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("Failed to store server entry for %s: %s\n",
			rec->desc, nt_errstr(status));
		ret = -1;
		goto done;
	}
	ret = 0;

done:
	return ret;
}

static int wipedbs_traverse_nop(struct db_record *rec, void *private_data)
{
	return 0;
}

static int wipedbs_traverse_fill_ids(struct db_record *rec, void *wipedbs_state)
{
	struct wipedbs_state *state = talloc_get_type_abort(
		wipedbs_state, struct wipedbs_state);

	TDB_DATA val = dbwrap_record_get_value(rec);

	struct wipedbs_server_data *sd = talloc_get_type_abort(
		*(void**)val.dptr, struct wipedbs_server_data);

	state->server_ids[state->idx] = sd->server_id;
	state->idx++;
	return 0;
}

static int wipedbs_traverse_set_exists(struct db_record *rec,
				       void *wipedbs_state)
{
	struct wipedbs_state *state = talloc_get_type_abort(
		wipedbs_state, struct wipedbs_state);

	TDB_DATA val = dbwrap_record_get_value(rec);

	struct wipedbs_server_data *sd = talloc_get_type_abort(
		*(void**)val.dptr, struct wipedbs_server_data);

	/* assume a stable traverse order for rbt */
	SMB_ASSERT(server_id_equal(&state->server_ids[state->idx],
				   &sd->server_id));
	sd->exists = state->server_exists[state->idx];

	if (sd->exists) {
		state->stat.server.existing++;
	}
	if (server_id_is_disconnected(&sd->server_id)) {
		state->stat.server.disconnected++;
	}

	state->idx++;
	return 0;
}

static bool serverids_exist(const struct server_id *ids, int num_ids,
			    bool *results)
{
	int i;

	for (i=0; i<num_ids; i++) {
		results[i] = serverid_exists(&ids[i]);
	}

	return true;
}


static NTSTATUS wipedbs_check_server_exists(struct wipedbs_state *state)
{
	NTSTATUS status;
	bool ok;
	int num_servers;

	status = dbwrap_traverse_read(state->id2server_data,
				      wipedbs_traverse_nop, NULL, &num_servers);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("Failed to traverse temporary database\n"));
		goto done;
	}
	state->stat.server.total = num_servers;

	state->server_ids = talloc_array(state, struct server_id, num_servers);
	state->server_exists = talloc_array(state, bool, num_servers);
	if (state->server_ids == NULL || state->server_exists == NULL) {
		DEBUG(0, ("Out of memory\n"));
		goto done;
	}

	state->idx = 0;
	status = dbwrap_traverse_read(state->id2server_data,
				      wipedbs_traverse_fill_ids,
				      state, NULL);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("Failed to traverse temporary database\n"));
		goto done;
	}

	ok = serverids_exist(state->server_ids, num_servers, state->server_exists);
	if (!ok) {
		DEBUG(0, ("Calling serverids_exist failed\n"));
		status = NT_STATUS_UNSUCCESSFUL;
		goto done;
	}

	state->idx = 0;
	status = dbwrap_traverse_read(state->id2server_data,
				      wipedbs_traverse_set_exists, state, NULL);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("Failed to traverse temporary database\n"));
		goto done;
	}
done:
	TALLOC_FREE(state->server_ids);
	TALLOC_FREE(state->server_exists);
	return status;
}

struct wipedbs_delete_state {
	struct wipedbs_state *state;
	struct wipedbs_record_marker *cur;
	bool verbose;
	bool dry_run;
	size_t total;
	size_t num;
};

static void wipedbs_delete_fn(
	struct db_record *rec, TDB_DATA value, void *private_data)
{
	struct db_context *db = dbwrap_record_get_db(rec);
	struct wipedbs_delete_state *state = private_data;
	struct wipedbs_record_marker *cur = state->cur;
	NTSTATUS status = NT_STATUS_OK;

	state->total += 1;

	if (!tdb_data_equal(value, cur->val)) {
		DBG_ERR("Warning: record <%s> from %s changed,"
			"skip record!\n",
			cur->desc, dbwrap_name(db));
		return;
	}

	if (state->verbose) {
		d_printf("deleting %s\n", cur->desc);
	}

	if (!state->dry_run) {
		status = dbwrap_record_delete(rec);
		if (!NT_STATUS_IS_OK(status)) {
			DBG_ERR("Failed to delete record <%s> from %s: %s\n",
				cur->desc,
				dbwrap_name(db),
				nt_errstr(status));
			return;
		}
	}

	state->num += 1;
}

static void wipedbs_delete_replay_record(
	struct wipedbs_delete_state *delete_state)
{
	TALLOC_CTX *mem_ctx = NULL;
	struct wipedbs_record_marker *cur = delete_state->cur;
	DATA_BLOB blob = data_blob_const(cur->val.dptr, cur->val.dsize);
	struct smbXsrv_open_globalB global_blob;
	enum ndr_err_code ndr_err;
	NTSTATUS status;

	/* Deleting open records, also remove associated replay record */

	mem_ctx = talloc_new(delete_state->state);
	if (mem_ctx == NULL) {
		DBG_ERR("talloc_new failed\n");
		return;
	}

	ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &global_blob,
			(ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_globalB);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		DBG_ERR("Invalid record in smbXsrv_open_global.tdb:"
			"key '%s' ndr_pull_struct_blob - %s\n%s\n",
			tdb_data_dbg(cur->key),
			ndr_errstr(ndr_err),
			tdb_data_dbg(cur->val));
		return;
	}

	if (global_blob.version != SMBXSRV_VERSION_0) {
		DBG_ERR("Unsupported open record version %u\n",
			global_blob.version);
		TALLOC_FREE(mem_ctx);
		return;
	}

	status = smbXsrv_replay_cleanup(&global_blob.info.info0->client_guid,
					&global_blob.info.info0->create_guid);
	TALLOC_FREE(mem_ctx);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("Replay record cleanup failed\n");
		return;
	}
}

static int wipedbs_delete_records(struct wipedbs_state *state,
				  struct db_context *db,
				  struct wipedbs_record_marker *records,
				  bool dry_run, bool verbose, int *count)
{
	struct wipedbs_delete_state delete_state = {
		.state = state, .verbose = verbose, .dry_run = dry_run,
	};

	if (db == NULL) {
		return 0;
	}

	for (delete_state.cur = records;
	     delete_state.cur != NULL;
	     delete_state.cur = delete_state.cur->next) {

		NTSTATUS status = dbwrap_do_locked(
			db, delete_state.cur->key, wipedbs_delete_fn, &delete_state);

		if (!NT_STATUS_IS_OK(status)) {
			DBG_ERR("dbwrap_do_locked failed for record <%s> "
				"from %s\n",
				delete_state.cur->desc,
				dbwrap_name(db));
		}

		if (state->open_db != db) {
			continue;
		}
		wipedbs_delete_replay_record(&delete_state);
	}

	if (verbose) {
		d_printf("Deleted %zu of %zu records from %s\n",
			 delete_state.num,
			 delete_state.total,
			 dbwrap_name(db));
	}

	if (count) {
		*count += delete_state.total;
	}

	return delete_state.total - delete_state.num;
}

static int wipedbs_traverse_server_data(struct db_record *rec,
					void *wipedbs_state)
{
	struct wipedbs_state *state = talloc_get_type_abort(
		wipedbs_state, struct wipedbs_state);
	bool dry_run = state->testmode;
	TDB_DATA val = dbwrap_record_get_value(rec);
	int ret;
	struct wipedbs_server_data *sd = talloc_get_type_abort(
		*(void**)val.dptr, struct wipedbs_server_data);

	if (state->verbose) {
		d_printf("Server: '%s' %s\n", sd->server_id_str,
			 sd->exists ?
			 "exists" :
			 "does not exist, cleaning up...");
	}

	if (sd->exists) {
		return 0;
	}

	ret = wipedbs_delete_records(state, state->session_db, sd->session_records,
				     dry_run, state->verbose,
				     &state->stat.session.todelete);
	state->stat.session.failure += ret;

	ret = wipedbs_delete_records(state, state->tcon_db, sd->tcon_records,
				     dry_run, state->verbose,
				     &state->stat.tcon.todelete);
	state->stat.tcon.failure += ret;

	ret = wipedbs_delete_records(state, state->open_db, sd->open_records,
				     dry_run, state->verbose,
				     &state->stat.open.todelete);
	state->stat.open.failure += ret;

	return 0;
}

static int wipedbs_traverse_replay_records(struct db_record *rec,
					   void *wipedbs_state)
{
	struct wipedbs_state *state = talloc_get_type_abort(
		wipedbs_state, struct wipedbs_state);
	bool dry_run = state->testmode;
	TDB_DATA key = dbwrap_record_get_key(rec);
	TDB_DATA val = dbwrap_record_get_value(rec);
	struct wipedbs_record_marker *m = talloc_get_type_abort(
		*(void **)val.dptr, struct wipedbs_record_marker);
	TDB_DATA open_key;
	bool exists;
	NTSTATUS status;

	exists = dbwrap_exists(state->open_db, key);
	if (!exists) {
		/*
		 * Already properly removed when removing the
		 * open record.
		 */
		return 0;
	}

	open_key = m->val;
	exists = dbwrap_exists(state->open_records_db, open_key);
	if (exists) {
		return 0;
	}

	state->stat.replay.todelete++;

	if (!dry_run) {
		status = dbwrap_purge(state->open_db, key);
		if (!NT_STATUS_IS_OK(status)) {
			state->stat.replay.failure++;
			DBG_ERR("dbwrap_purge failed\n");
			return -1;
		}
	}

	return 0;
}

int net_serverid_wipedbs(struct net_context *c, int argc, const char **argv)
{
	int ret = -1;
	NTSTATUS status;
	struct wipedbs_state *state = talloc_zero(talloc_tos(),
						  struct wipedbs_state);

	if (c->display_usage) {
		d_printf("%s\n%s",
			 _("Usage:"),
			 _("net serverid wipedbs [--test] [--verbose]\n"));
		d_printf("%s\n%s",
			 _("Example:"),
			 _("net serverid wipedbs -v\n"));
		return -1;
	}

	state->now = timeval_current();
	state->testmode = c->opt_testmode;
	state->verbose = c->opt_verbose;

	state->id2server_data = db_open_rbt(state);
	if (state->id2server_data == NULL) {
		DEBUG(0, ("Failed to open temporary database\n"));
		goto done;
	}

	state->open_records_db = db_open_rbt(state);
	if (state->open_records_db == NULL) {
		DBG_ERR("Failed to open temporary database\n");
		goto done;
	}

	state->replay_records_db = db_open_rbt(state);
	if (state->replay_records_db == NULL) {
		DBG_ERR("Failed to open temporary database\n");
		goto done;
	}

	status = smbXsrv_session_global_traverse(wipedbs_traverse_sessions,
						 state);
	if (!NT_STATUS_IS_OK(status)) {
		goto done;
	}

	status = smbXsrv_tcon_global_traverse(wipedbs_traverse_tcon, state);
	if (!NT_STATUS_IS_OK(status)) {
		goto done;
	}

	status = smbXsrv_open_global_traverse(wipedbs_traverse_open, state);
	if (!NT_STATUS_IS_OK(status)) {
		goto done;
	}

	status = wipedbs_check_server_exists(state);
	if (!NT_STATUS_IS_OK(status)) {
		goto done;
	}

	status = dbwrap_traverse_read(state->id2server_data,
				      wipedbs_traverse_server_data,
				      state, NULL);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("Failed to traverse db: %s\n", nt_errstr(status)));
		goto done;
	}

	status = dbwrap_traverse_read(state->replay_records_db,
				      wipedbs_traverse_replay_records,
				      state, NULL);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("Failed to replay records: %s\n", nt_errstr(status));
		goto done;
	}

	d_printf("Found %d serverids, %d alive and %d disconnected\n",
		 state->stat.server.total,
		 state->stat.server.existing,
		 state->stat.server.disconnected);
	d_printf("Found %d sessions, %d alive and %d disconnected"
		 ", cleaned up %d of %d entries\n",
		 state->stat.session.total,
		 state->stat.session.total - state->stat.session.todelete,
		 state->stat.session.disconnected,
		 state->stat.session.todelete - state->stat.session.failure,
		 state->stat.session.todelete);
	d_printf("Found %d tcons, %d alive and %d disconnected"
		 ", cleaned up %d of %d entries\n",
		 state->stat.tcon.total,
		 state->stat.tcon.total - state->stat.tcon.todelete,
		 state->stat.tcon.disconnected,
		 state->stat.tcon.todelete - state->stat.tcon.failure,
		 state->stat.tcon.todelete);
	d_printf("Found %d opens, %d alive, %d disconnected and %d timed out"
		 ", cleaned up %d of %d entries\n",
		 state->stat.open.total,
		 state->stat.open.total - state->stat.open.todelete
		 - (state->stat.open.disconnected - state->stat.open_timed_out),
		 state->stat.open.disconnected,
		 state->stat.open_timed_out,
		 state->stat.open.todelete - state->stat.open.failure,
		 state->stat.open.todelete);

	d_printf("Found %d replay records, %d alive, "
		 "cleaned up %d entries, %d failed\n",
		 state->stat.replay.total,
		 state->stat.replay.total - state->stat.replay.todelete,
		 state->stat.replay.todelete,
		 state->stat.replay.failure);

	ret = 0;
done:
	talloc_free(state);
	return ret;
}

static int net_serverid_exists(struct net_context *c, int argc,
			       const char **argv)
{
	struct server_id pid;
	bool ok;

	if ((argc != 1) || (c->display_usage)) {
		d_printf("Usage:\n"
			 "net serverid exists <serverid>\n");
		return -1;
	}

	pid = server_id_from_string(get_my_vnn(), argv[0]);
	ok = serverid_exists(&pid);

	if (ok) {
		d_printf("%s exists\n", argv[0]);
	} else {
		d_printf("%s does not exist\n", argv[0]);
	}

	return 0;
}

int net_serverid(struct net_context *c, int argc, const char **argv)
{
	struct functable func[] = {
		{
			"wipedbs",
			net_serverid_wipedbs,
			NET_TRANSPORT_LOCAL,
			N_("Clean dead entries from temporary databases"),
			N_("net serverid wipedbs\n"
			   "    Clean dead entries from temporary databases")
		},
		{
			"exists",
			net_serverid_exists,
			NET_TRANSPORT_LOCAL,
			N_("Show existence of a serverid"),
			N_("net serverid exists <id>")
		},
		{NULL, NULL, 0, NULL, NULL}
	};

	return net_run_function(c, argc, argv, "net serverid", func);
}