winbind: Keep "force_reauth" in invalidate_cm_connection
[Samba.git] / source3 / utils / net_serverid.c
blobc826f95cc0dae26d2c7f80a8beed70c3cf33c49c
1 /*
2 Samba Unix/Linux SMB client library
3 net serverid commands
4 Copyright (C) Volker Lendecke 2010
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 #include "includes.h"
21 #include "utils/net.h"
22 #include "lib/util/server_id.h"
23 #include "dbwrap/dbwrap.h"
24 #include "dbwrap/dbwrap_rbt.h"
25 #include "serverid.h"
26 #include "session.h"
27 #include "lib/conn_tdb.h"
28 #include "smbd/globals.h"
29 #include "util_tdb.h"
30 #include "librpc/gen_ndr/ndr_open_files.h"
32 struct wipedbs_record_marker {
33 struct wipedbs_record_marker *prev, *next;
34 TDB_DATA key, val;
35 const char *desc;
38 struct wipedbs_server_data {
39 struct server_id server_id;
40 const char *server_id_str;
41 bool exists;
42 struct wipedbs_record_marker *session_records;
43 struct wipedbs_record_marker *tcon_records;
44 struct wipedbs_record_marker *open_records;
47 struct wipedbs_state {
48 struct db_context *id2server_data;
49 struct {
50 struct {
51 int total;
52 int existing;
53 int disconnected;
54 } server;
55 struct {
56 int total;
57 int disconnected;
58 int todelete;
59 int failure;
60 } session, tcon, open;
61 int open_timed_out;
62 } stat;
63 struct server_id *server_ids;
64 bool *server_exists;
65 int idx;
66 struct db_context *session_db;
67 struct db_context *tcon_db;
68 struct db_context *open_db;
69 struct timeval now;
70 bool testmode;
71 bool verbose;
74 static struct wipedbs_server_data *get_server_data(struct wipedbs_state *state,
75 const struct server_id *id)
77 struct wipedbs_server_data *ret = NULL;
78 TDB_DATA key, val = tdb_null;
79 NTSTATUS status;
81 key = make_tdb_data((const void*)&id->unique_id, sizeof(id->unique_id));
82 status = dbwrap_fetch(state->id2server_data, talloc_tos(), key, &val);
83 if (NT_STATUS_IS_OK(status)) {
84 ret = *(struct wipedbs_server_data**) val.dptr;
85 TALLOC_FREE(val.dptr);
86 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
87 struct server_id_buf idbuf;
89 server_id_str_buf(*id, &idbuf);
91 ret = talloc_zero(state->id2server_data,
92 struct wipedbs_server_data);
93 if (ret == NULL) {
94 DEBUG(0, ("Failed to allocate server entry for %s\n",
95 idbuf.buf));
96 goto done;
98 ret->server_id = *id;
99 ret->server_id_str = talloc_strdup(ret, idbuf.buf);
100 ret->exists = true;
101 val = make_tdb_data((const void*)&ret, sizeof(ret));
102 status = dbwrap_store(state->id2server_data,
103 key, val, TDB_INSERT);
104 if (!NT_STATUS_IS_OK(status)) {
105 DEBUG(0, ("Failed to store server entry for %s: %s\n",
106 idbuf.buf, nt_errstr(status)));
108 goto done;
109 } else {
110 struct server_id_buf idbuf;
111 DEBUG(0, ("Failed to fetch server entry for %s: %s\n",
112 server_id_str_buf(*id, &idbuf), nt_errstr(status)));
113 goto done;
115 if (!server_id_equal(id, &ret->server_id)) {
116 struct server_id_buf idbuf1, idbuf2;
117 DEBUG(0, ("uniq id collision for %s and %s\n",
118 server_id_str_buf(*id, &idbuf1),
119 server_id_str_buf(ret->server_id, &idbuf2)));
120 smb_panic("server_id->unique_id not unique!");
122 done:
123 return ret;
126 static int wipedbs_traverse_sessions(struct smbXsrv_session_global0 *session,
127 void *wipedbs_state)
129 struct wipedbs_state *state =
130 talloc_get_type_abort(wipedbs_state,
131 struct wipedbs_state);
132 struct wipedbs_server_data *sd;
133 struct wipedbs_record_marker *rec;
134 TDB_DATA tmp;
135 int ret = -1;
137 assert(session->num_channels == 1);
139 state->stat.session.total++;
141 sd = get_server_data(state, &session->channels[0].server_id);
142 if (sd == NULL) {
143 goto done;
146 if (server_id_is_disconnected(&sd->server_id)) {
147 state->stat.session.disconnected++;
150 rec = talloc_zero(sd, struct wipedbs_record_marker);
151 if (rec == NULL) {
152 DEBUG(0, ("Out of memory!\n"));
153 goto done;
156 tmp = dbwrap_record_get_key(session->db_rec);
157 rec->key = tdb_data_talloc_copy(rec, tmp);
158 tmp = dbwrap_record_get_value(session->db_rec);
159 rec->val = tdb_data_talloc_copy(rec, tmp);
161 rec->desc = talloc_asprintf(
162 rec, "session[global: %u wire: %llu]",
163 session->session_global_id,
164 (long long unsigned)session->session_wire_id);
166 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
167 (rec->desc == NULL))
169 DEBUG(0, ("Out of memory!\n"));
170 goto done;
173 state->session_db = dbwrap_record_get_db(session->db_rec);
175 DLIST_ADD(sd->session_records, rec);
176 ret = 0;
177 done:
178 return ret;
181 static int wipedbs_traverse_tcon(struct smbXsrv_tcon_global0 *tcon,
182 void *wipedbs_state)
184 struct wipedbs_state *state =
185 talloc_get_type_abort(wipedbs_state,
186 struct wipedbs_state);
187 struct wipedbs_server_data *sd;
188 struct wipedbs_record_marker *rec;
189 TDB_DATA tmp;
190 int ret = -1;
192 state->stat.tcon.total++;
194 sd = get_server_data(state, &tcon->server_id);
195 if (sd == NULL) {
196 goto done;
199 if (server_id_is_disconnected(&sd->server_id)) {
200 state->stat.tcon.disconnected++;
203 rec = talloc_zero(sd, struct wipedbs_record_marker);
204 if (rec == NULL) {
205 DEBUG(0, ("Out of memory!\n"));
206 goto done;
209 tmp = dbwrap_record_get_key(tcon->db_rec);
210 rec->key = tdb_data_talloc_copy(rec, tmp);
211 tmp = dbwrap_record_get_value(tcon->db_rec);
212 rec->val = tdb_data_talloc_copy(rec, tmp);
214 rec->desc = talloc_asprintf(
215 rec, "tcon[global: %u wire: %u session: %u share: %s]",
216 tcon->tcon_global_id, tcon->tcon_wire_id,
217 tcon->session_global_id, tcon->share_name);
219 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
220 (rec->desc == NULL))
222 DEBUG(0, ("Out of memory!\n"));
223 goto done;
226 state->tcon_db = dbwrap_record_get_db(tcon->db_rec);
228 DLIST_ADD(sd->tcon_records, rec);
229 ret = 0;
231 done:
232 return ret;
235 static int wipedbs_traverse_open(struct smbXsrv_open_global0 *open,
236 void *wipedbs_state)
238 struct wipedbs_state *state =
239 talloc_get_type_abort(wipedbs_state,
240 struct wipedbs_state);
241 struct wipedbs_server_data *sd;
242 struct wipedbs_record_marker *rec;
243 TDB_DATA tmp;
244 int ret = -1;
246 state->stat.open.total++;
248 sd = get_server_data(state, &open->server_id);
249 if (sd == NULL) {
250 goto done;
253 if (server_id_is_disconnected(&sd->server_id)) {
254 struct timeval disconnect_time;
255 int64_t tdiff;
256 bool reached;
258 state->stat.open.disconnected++;
260 nttime_to_timeval(&disconnect_time, open->disconnect_time);
261 tdiff = usec_time_diff(&state->now, &disconnect_time);
262 reached = (tdiff >= 1000*open->durable_timeout_msec);
264 if (state->verbose) {
265 TALLOC_CTX *mem_ctx = talloc_new(talloc_tos());
266 enum ndr_err_code ndr_err;
267 struct vfs_default_durable_cookie cookie;
269 ndr_err = ndr_pull_struct_blob(
270 &open->backend_cookie, mem_ctx, &cookie,
271 (ndr_pull_flags_fn_t)ndr_pull_vfs_default_durable_cookie);
272 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
273 d_printf("ndr_pull_struct_blob failed\n");
274 ret = -1;
275 goto done;
278 d_printf("open[%s/%s id: 0x%" PRIx32 "] disconnected at "
279 "[%s] %us ago with timeout of %us "
280 "-%s reached\n",
281 cookie.servicepath, cookie.base_name,
282 open->open_global_id,
283 nt_time_string(mem_ctx, open->disconnect_time),
284 (unsigned)(tdiff/1000000),
285 open->durable_timeout_msec / 1000,
286 reached ? "" : " not");
287 talloc_free(mem_ctx);
290 if (!reached) {
291 ret = 0;
292 goto done;
294 state->stat.open_timed_out++;
297 rec = talloc_zero(sd, struct wipedbs_record_marker);
298 if (rec == NULL) {
299 DEBUG(0, ("Out of memory!\n"));
300 goto done;
303 tmp = dbwrap_record_get_key(open->db_rec);
304 rec->key = tdb_data_talloc_copy(rec, tmp);
305 tmp = dbwrap_record_get_value(open->db_rec);
306 rec->val = tdb_data_talloc_copy(rec, tmp);
308 rec->desc = talloc_asprintf(
309 rec, "open[global: %u persistent: %llu volatile: %llu]",
310 open->open_global_id,
311 (long long unsigned)open->open_persistent_id,
312 (long long unsigned)open->open_volatile_id);
314 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
315 (rec->desc == NULL))
317 DEBUG(0, ("Out of memory!\n"));
318 goto done;
321 state->open_db = dbwrap_record_get_db(open->db_rec);
323 DLIST_ADD(sd->open_records, rec);
324 ret = 0;
326 done:
327 return ret;
330 static int wipedbs_traverse_nop(struct db_record *rec, void *private_data)
332 return 0;
335 static int wipedbs_traverse_fill_ids(struct db_record *rec, void *wipedbs_state)
337 struct wipedbs_state *state = talloc_get_type_abort(
338 wipedbs_state, struct wipedbs_state);
340 TDB_DATA val = dbwrap_record_get_value(rec);
342 struct wipedbs_server_data *sd = talloc_get_type_abort(
343 *(void**)val.dptr, struct wipedbs_server_data);
345 state->server_ids[state->idx] = sd->server_id;
346 state->idx++;
347 return 0;
350 static int wipedbs_traverse_set_exists(struct db_record *rec,
351 void *wipedbs_state)
353 struct wipedbs_state *state = talloc_get_type_abort(
354 wipedbs_state, struct wipedbs_state);
356 TDB_DATA val = dbwrap_record_get_value(rec);
358 struct wipedbs_server_data *sd = talloc_get_type_abort(
359 *(void**)val.dptr, struct wipedbs_server_data);
361 /* assume a stable traverse order for rbt */
362 SMB_ASSERT(server_id_equal(&state->server_ids[state->idx],
363 &sd->server_id));
364 sd->exists = state->server_exists[state->idx];
366 if (sd->exists) {
367 state->stat.server.existing++;
369 if (server_id_is_disconnected(&sd->server_id)) {
370 state->stat.server.disconnected++;
373 state->idx++;
374 return 0;
377 static bool serverids_exist(const struct server_id *ids, int num_ids,
378 bool *results)
380 int i;
382 for (i=0; i<num_ids; i++) {
383 results[i] = serverid_exists(&ids[i]);
386 return true;
390 static NTSTATUS wipedbs_check_server_exists(struct wipedbs_state *state)
392 NTSTATUS status;
393 bool ok;
394 int num_servers;
396 status = dbwrap_traverse_read(state->id2server_data,
397 wipedbs_traverse_nop, NULL, &num_servers);
398 if (!NT_STATUS_IS_OK(status)) {
399 DEBUG(0, ("Failed to traverse temporary database\n"));
400 goto done;
402 state->stat.server.total = num_servers;
404 state->server_ids = talloc_array(state, struct server_id, num_servers);
405 state->server_exists = talloc_array(state, bool, num_servers);
406 if (state->server_ids == NULL || state->server_exists == NULL) {
407 DEBUG(0, ("Out of memory\n"));
408 goto done;
411 state->idx = 0;
412 status = dbwrap_traverse_read(state->id2server_data,
413 wipedbs_traverse_fill_ids,
414 state, NULL);
415 if (!NT_STATUS_IS_OK(status)) {
416 DEBUG(0, ("Failed to traverse temporary database\n"));
417 goto done;
420 ok = serverids_exist(state->server_ids, num_servers, state->server_exists);
421 if (!ok) {
422 DEBUG(0, ("Calling serverids_exist failed\n"));
423 status = NT_STATUS_UNSUCCESSFUL;
424 goto done;
427 state->idx = 0;
428 status = dbwrap_traverse_read(state->id2server_data,
429 wipedbs_traverse_set_exists, state, NULL);
430 if (!NT_STATUS_IS_OK(status)) {
431 DEBUG(0, ("Failed to traverse temporary database\n"));
432 goto done;
434 done:
435 TALLOC_FREE(state->server_ids);
436 TALLOC_FREE(state->server_exists);
437 return status;
440 static int wipedbs_delete_records(struct db_context *db,
441 struct wipedbs_record_marker *records,
442 bool dry_run, bool verbose, int *count)
444 struct wipedbs_record_marker *cur;
445 struct db_record *rec;
446 TDB_DATA val;
447 NTSTATUS status;
448 unsigned num=0, total=0;
450 if (db == NULL) {
451 return 0;
454 for (cur = records; cur != NULL; cur = cur->next) {
455 total++;
456 rec = dbwrap_fetch_locked(db, talloc_tos(), cur->key);
457 if (rec == NULL) {
458 DEBUG(0, ("Failed to fetch record <%s> from %s",
459 cur->desc, dbwrap_name(db)));
460 continue;
462 val = dbwrap_record_get_value(rec);
463 if (tdb_data_equal(val, cur->val)) {
464 if (dry_run) {
465 status = NT_STATUS_OK;
466 } else {
467 status = dbwrap_record_delete(rec);
469 if (NT_STATUS_IS_OK(status)) {
470 num ++;
471 } else {
472 DEBUG(0, ("Failed to delete record <%s> from %s"
473 ": %s\n", cur->desc, dbwrap_name(db),
474 nt_errstr(status)));
476 } else {
477 DEBUG(0, ("Warning: record <%s> from %s changed"
478 ", skip record!\n",
479 cur->desc, dbwrap_name(db)));
481 if (verbose) {
482 d_printf("deleting %s\n", cur->desc);
484 TALLOC_FREE(rec);
487 if (verbose) {
488 d_printf("Deleted %u of %u records from %s\n",
489 num, total, dbwrap_name(db));
492 if (count) {
493 *count += total;
496 return total - num;
499 static int wipedbs_traverse_server_data(struct db_record *rec,
500 void *wipedbs_state)
502 struct wipedbs_state *state = talloc_get_type_abort(
503 wipedbs_state, struct wipedbs_state);
504 bool dry_run = state->testmode;
505 TDB_DATA val = dbwrap_record_get_value(rec);
506 int ret;
507 struct wipedbs_server_data *sd = talloc_get_type_abort(
508 *(void**)val.dptr, struct wipedbs_server_data);
510 if (state->verbose) {
511 d_printf("Server: '%s' %s\n", sd->server_id_str,
512 sd->exists ?
513 "exists" :
514 "does not exist, cleaning up...");
517 if (sd->exists) {
518 return 0;
521 ret = wipedbs_delete_records(state->session_db, sd->session_records,
522 dry_run, state->verbose,
523 &state->stat.session.todelete);
524 state->stat.session.failure += ret;
526 ret = wipedbs_delete_records(state->tcon_db, sd->tcon_records,
527 dry_run, state->verbose,
528 &state->stat.tcon.todelete);
529 state->stat.tcon.failure += ret;
531 ret = wipedbs_delete_records(state->open_db, sd->open_records,
532 dry_run, state->verbose,
533 &state->stat.open.todelete);
534 state->stat.open.failure += ret;
536 return 0;
539 static int net_serverid_wipedbs(struct net_context *c, int argc,
540 const char **argv)
542 int ret = -1;
543 NTSTATUS status;
544 struct wipedbs_state *state = talloc_zero(talloc_tos(),
545 struct wipedbs_state);
547 if (c->display_usage) {
548 d_printf("%s\n%s",
549 _("Usage:"),
550 _("net serverid wipedbs [--test] [--verbose]\n"));
551 d_printf("%s\n%s",
552 _("Example:"),
553 _("net serverid wipedbs -v\n"));
554 return -1;
557 state->now = timeval_current();
558 state->testmode = c->opt_testmode;
559 state->verbose = c->opt_verbose;
561 state->id2server_data = db_open_rbt(state);
562 if (state->id2server_data == NULL) {
563 DEBUG(0, ("Failed to open temporary database\n"));
564 goto done;
567 status = smbXsrv_session_global_traverse(wipedbs_traverse_sessions,
568 state);
569 if (!NT_STATUS_IS_OK(status)) {
570 goto done;
573 status = smbXsrv_tcon_global_traverse(wipedbs_traverse_tcon, state);
574 if (!NT_STATUS_IS_OK(status)) {
575 goto done;
578 status = smbXsrv_open_global_traverse(wipedbs_traverse_open, state);
579 if (!NT_STATUS_IS_OK(status)) {
580 goto done;
583 status = wipedbs_check_server_exists(state);
584 if (!NT_STATUS_IS_OK(status)) {
585 goto done;
588 status = dbwrap_traverse_read(state->id2server_data,
589 wipedbs_traverse_server_data,
590 state, NULL);
591 if (!NT_STATUS_IS_OK(status)) {
592 DEBUG(0, ("Failed to traverse db: %s\n", nt_errstr(status)));
593 goto done;
596 d_printf("Found %d serverids, %d alive and %d disconnected\n",
597 state->stat.server.total,
598 state->stat.server.existing,
599 state->stat.server.disconnected);
600 d_printf("Found %d sessions, %d alive and %d disconnected"
601 ", cleaned up %d of %d entries\n",
602 state->stat.session.total,
603 state->stat.session.total - state->stat.session.todelete,
604 state->stat.session.disconnected,
605 state->stat.session.todelete - state->stat.session.failure,
606 state->stat.session.todelete);
607 d_printf("Found %d tcons, %d alive and %d disconnected"
608 ", cleaned up %d of %d entries\n",
609 state->stat.tcon.total,
610 state->stat.tcon.total - state->stat.tcon.todelete,
611 state->stat.tcon.disconnected,
612 state->stat.tcon.todelete - state->stat.tcon.failure,
613 state->stat.tcon.todelete);
614 d_printf("Found %d opens, %d alive, %d disconnected and %d timed out"
615 ", cleaned up %d of %d entries\n",
616 state->stat.open.total,
617 state->stat.open.total - state->stat.open.todelete
618 - (state->stat.open.disconnected - state->stat.open_timed_out),
619 state->stat.open.disconnected,
620 state->stat.open_timed_out,
621 state->stat.open.todelete - state->stat.open.failure,
622 state->stat.open.todelete);
624 ret = 0;
625 done:
626 talloc_free(state);
627 return ret;
630 static int net_serverid_exists(struct net_context *c, int argc,
631 const char **argv)
633 struct server_id pid;
634 bool ok;
636 if ((argc != 1) || (c->display_usage)) {
637 d_printf("Usage:\n"
638 "net serverid exists <serverid>\n");
639 return -1;
642 pid = server_id_from_string(get_my_vnn(), argv[0]);
643 ok = serverid_exists(&pid);
645 if (ok) {
646 d_printf("%s exists\n", argv[0]);
647 } else {
648 d_printf("%s does not exist\n", argv[0]);
651 return 0;
654 int net_serverid(struct net_context *c, int argc, const char **argv)
656 struct functable func[] = {
658 "wipedbs",
659 net_serverid_wipedbs,
660 NET_TRANSPORT_LOCAL,
661 N_("Clean dead entries from temporary databases"),
662 N_("net serverid wipedbs\n"
663 " Clean dead entries from temporary databases")
666 "exists",
667 net_serverid_exists,
668 NET_TRANSPORT_LOCAL,
669 N_("Show existence of a serverid"),
670 N_("net serverid exists <id>")
672 {NULL, NULL, 0, NULL, NULL}
675 return net_run_function(c, argc, argv, "net serverid", func);