s3:winbindd: make use of samba_tevent_context_init()
[Samba/gebeck_regimport.git] / source3 / utils / net_serverid.c
blob90448c158a19c2242d8cb295d2502683c746b914
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 "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_rbt.h"
24 #include "serverid.h"
25 #include "session.h"
26 #include "lib/conn_tdb.h"
27 #include "smbd/globals.h"
28 #include "util_tdb.h"
30 static int net_serverid_list_fn(const struct server_id *id,
31 uint32_t msg_flags, void *priv)
33 char *str = server_id_str(talloc_tos(), id);
34 d_printf("%s %llu 0x%x\n", str, (unsigned long long)id->unique_id,
35 (unsigned int)msg_flags);
36 TALLOC_FREE(str);
37 return 0;
40 static int net_serverid_list(struct net_context *c, int argc,
41 const char **argv)
43 d_printf("pid unique_id msg_flags\n");
44 return serverid_traverse_read(net_serverid_list_fn, NULL) ? 0 : -1;
47 static int net_serverid_wipe_fn(struct db_record *rec,
48 const struct server_id *id,
49 uint32_t msg_flags, void *private_data)
51 NTSTATUS status;
53 if (id->vnn != get_my_vnn()) {
54 return 0;
56 status = dbwrap_record_delete(rec);
57 if (!NT_STATUS_IS_OK(status)) {
58 char *str = server_id_str(talloc_tos(), id);
59 DEBUG(1, ("Could not delete serverid.tdb record %s: %s\n",
60 str, nt_errstr(status)));
61 TALLOC_FREE(str);
63 return 0;
66 static int net_serverid_wipe(struct net_context *c, int argc,
67 const char **argv)
69 return serverid_traverse(net_serverid_wipe_fn, NULL) ? 0 : -1;
73 struct wipedbs_record_marker {
74 struct wipedbs_record_marker *prev, *next;
75 TDB_DATA key, val;
76 const char *desc;
79 struct wipedbs_server_data {
80 struct server_id server_id;
81 const char *server_id_str;
82 bool exists;
83 struct wipedbs_record_marker *session_records;
84 struct wipedbs_record_marker *tcon_records;
85 struct wipedbs_record_marker *open_records;
88 struct wipedbs_state {
89 struct db_context *id2server_data;
90 struct {
91 struct {
92 int total;
93 int existing;
94 int disconnected;
95 } server;
96 struct {
97 int total;
98 int disconnected;
99 int todelete;
100 int failure;
101 } session, tcon, open;
102 int open_timed_out;
103 } stat;
104 struct server_id *server_ids;
105 bool *server_exists;
106 int idx;
107 struct db_context *session_db;
108 struct db_context *tcon_db;
109 struct db_context *open_db;
110 struct timeval now;
111 bool testmode;
112 bool verbose;
115 static struct wipedbs_server_data *get_server_data(struct wipedbs_state *state,
116 const struct server_id *id)
118 struct wipedbs_server_data *ret = NULL;
119 TDB_DATA key, val = tdb_null;
120 NTSTATUS status;
122 key = make_tdb_data((const void*)&id->unique_id, sizeof(id->unique_id));
123 status = dbwrap_fetch(state->id2server_data, talloc_tos(), key, &val);
124 if (NT_STATUS_IS_OK(status)) {
125 ret = *(struct wipedbs_server_data**) val.dptr;
126 TALLOC_FREE(val.dptr);
127 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
128 ret = talloc_zero(state->id2server_data,
129 struct wipedbs_server_data);
130 if (ret == NULL) {
131 DEBUG(0, ("Failed to allocate server entry for %s\n",
132 server_id_str(talloc_tos(), id)));
133 goto done;
135 ret->server_id = *id;
136 ret->server_id_str = server_id_str(ret, id);
137 ret->exists = true;
138 val = make_tdb_data((const void*)&ret, sizeof(ret));
139 status = dbwrap_store(state->id2server_data,
140 key, val, TDB_INSERT);
141 if (!NT_STATUS_IS_OK(status)) {
142 DEBUG(0, ("Failed to store server entry for %s: %s\n",
143 server_id_str(talloc_tos(), id),
144 nt_errstr(status)));
146 goto done;
147 } else {
148 DEBUG(0, ("Failed to fetch server entry for %s: %s\n",
149 server_id_str(talloc_tos(), id), nt_errstr(status)));
150 goto done;
152 if (!server_id_equal(id, &ret->server_id)) {
153 DEBUG(0, ("uniq id collision for %s and %s\n",
154 server_id_str(talloc_tos(), id),
155 server_id_str(talloc_tos(), &ret->server_id)));
156 smb_panic("server_id->unique_id not unique!");
158 done:
159 return ret;
162 static int wipedbs_traverse_sessions(struct smbXsrv_session_global0 *session,
163 void *wipedbs_state)
165 struct wipedbs_state *state =
166 talloc_get_type_abort(wipedbs_state,
167 struct wipedbs_state);
168 struct wipedbs_server_data *sd;
169 struct wipedbs_record_marker *rec;
170 TDB_DATA tmp;
171 int ret = -1;
173 assert(session->num_channels == 1);
175 state->stat.session.total++;
177 sd = get_server_data(state, &session->channels[0].server_id);
178 if (sd == NULL) {
179 goto done;
182 if (server_id_is_disconnected(&sd->server_id)) {
183 state->stat.session.disconnected++;
186 rec = talloc_zero(sd, struct wipedbs_record_marker);
187 if (rec == NULL) {
188 DEBUG(0, ("Out of memory!\n"));
189 goto done;
192 tmp = dbwrap_record_get_key(session->db_rec);
193 rec->key = tdb_data_talloc_copy(rec, tmp);
194 tmp = dbwrap_record_get_value(session->db_rec);
195 rec->val = tdb_data_talloc_copy(rec, tmp);
197 rec->desc = talloc_asprintf(
198 rec, "session[global: %u wire: %lu]",
199 session->session_global_id, session->session_wire_id);
201 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
202 (rec->desc == NULL))
204 DEBUG(0, ("Out of memory!\n"));
205 goto done;
208 state->session_db = dbwrap_record_get_db(session->db_rec);
210 DLIST_ADD(sd->session_records, rec);
211 ret = 0;
212 done:
213 return ret;
216 static int wipedbs_traverse_tcon(struct smbXsrv_tcon_global0 *tcon,
217 void *wipedbs_state)
219 struct wipedbs_state *state =
220 talloc_get_type_abort(wipedbs_state,
221 struct wipedbs_state);
222 struct wipedbs_server_data *sd;
223 struct wipedbs_record_marker *rec;
224 TDB_DATA tmp;
225 int ret = -1;
227 state->stat.tcon.total++;
229 sd = get_server_data(state, &tcon->server_id);
230 if (sd == NULL) {
231 goto done;
234 if (server_id_is_disconnected(&sd->server_id)) {
235 state->stat.tcon.disconnected++;
238 rec = talloc_zero(sd, struct wipedbs_record_marker);
239 if (rec == NULL) {
240 DEBUG(0, ("Out of memory!\n"));
241 goto done;
244 tmp = dbwrap_record_get_key(tcon->db_rec);
245 rec->key = tdb_data_talloc_copy(rec, tmp);
246 tmp = dbwrap_record_get_value(tcon->db_rec);
247 rec->val = tdb_data_talloc_copy(rec, tmp);
249 rec->desc = talloc_asprintf(
250 rec, "tcon[global: %u wire: %u session: %u share: %s]",
251 tcon->tcon_global_id, tcon->tcon_wire_id,
252 tcon->session_global_id, tcon->share_name);
254 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
255 (rec->desc == NULL))
257 DEBUG(0, ("Out of memory!\n"));
258 goto done;
261 state->tcon_db = dbwrap_record_get_db(tcon->db_rec);
263 DLIST_ADD(sd->tcon_records, rec);
264 ret = 0;
266 done:
267 return ret;
270 static int wipedbs_traverse_open(struct smbXsrv_open_global0 *open,
271 void *wipedbs_state)
273 struct wipedbs_state *state =
274 talloc_get_type_abort(wipedbs_state,
275 struct wipedbs_state);
276 struct wipedbs_server_data *sd;
277 struct wipedbs_record_marker *rec;
278 TDB_DATA tmp;
279 int ret = -1;
281 state->stat.open.total++;
283 sd = get_server_data(state, &open->server_id);
284 if (sd == NULL) {
285 goto done;
288 if (server_id_is_disconnected(&sd->server_id)) {
289 struct timeval disconnect_time;
290 int64_t tdiff;
291 bool reached;
293 state->stat.open.disconnected++;
295 nttime_to_timeval(&disconnect_time, open->disconnect_time);
296 tdiff = usec_time_diff(&state->now, &disconnect_time);
297 reached = (tdiff >= 1000*open->durable_timeout_msec);
299 if (state->verbose) {
300 TALLOC_CTX *mem_ctx = talloc_new(talloc_tos());
301 d_printf("open[global: %u] disconnected at "
302 "[%s] %us ago with timeout of %us "
303 "-%s reached\n",
304 open->open_global_id,
305 nt_time_string(mem_ctx, open->disconnect_time),
306 (unsigned)(tdiff/1000000),
307 open->durable_timeout_msec / 1000,
308 reached ? "" : " not");
309 talloc_free(mem_ctx);
312 if (!reached) {
313 ret = 0;
314 goto done;
316 state->stat.open_timed_out++;
319 rec = talloc_zero(sd, struct wipedbs_record_marker);
320 if (rec == NULL) {
321 DEBUG(0, ("Out of memory!\n"));
322 goto done;
325 tmp = dbwrap_record_get_key(open->db_rec);
326 rec->key = tdb_data_talloc_copy(rec, tmp);
327 tmp = dbwrap_record_get_value(open->db_rec);
328 rec->val = tdb_data_talloc_copy(rec, tmp);
330 rec->desc = talloc_asprintf(
331 rec, "open[global: %u persistent: %lu volatile: %lu]",
332 open->open_global_id, open->open_persistent_id,
333 open->open_volatile_id);
335 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
336 (rec->desc == NULL))
338 DEBUG(0, ("Out of memory!\n"));
339 goto done;
342 state->open_db = dbwrap_record_get_db(open->db_rec);
344 DLIST_ADD(sd->open_records, rec);
345 ret = 0;
347 done:
348 return ret;
351 static int wipedbs_traverse_nop(struct db_record *rec, void *private_data)
353 return 0;
356 static int wipedbs_traverse_fill_ids(struct db_record *rec, void *wipedbs_state)
358 struct wipedbs_state *state = talloc_get_type_abort(
359 wipedbs_state, struct wipedbs_state);
361 TDB_DATA val = dbwrap_record_get_value(rec);
363 struct wipedbs_server_data *sd = talloc_get_type_abort(
364 *(void**)val.dptr, struct wipedbs_server_data);
366 state->server_ids[state->idx] = sd->server_id;
367 state->idx++;
368 return 0;
371 static int wipedbs_traverse_set_exists(struct db_record *rec,
372 void *wipedbs_state)
374 struct wipedbs_state *state = talloc_get_type_abort(
375 wipedbs_state, struct wipedbs_state);
377 TDB_DATA val = dbwrap_record_get_value(rec);
379 struct wipedbs_server_data *sd = talloc_get_type_abort(
380 *(void**)val.dptr, struct wipedbs_server_data);
382 /* assume a stable traverse order for rbt */
383 SMB_ASSERT(server_id_equal(&state->server_ids[state->idx],
384 &sd->server_id));
385 sd->exists = state->server_exists[state->idx];
387 if (sd->exists) {
388 state->stat.server.existing++;
390 if (server_id_is_disconnected(&sd->server_id)) {
391 state->stat.server.disconnected++;
394 state->idx++;
395 return 0;
398 static NTSTATUS wipedbs_check_server_exists(struct wipedbs_state *state)
400 NTSTATUS status;
401 bool ok;
402 int num_servers;
404 status = dbwrap_traverse_read(state->id2server_data,
405 wipedbs_traverse_nop, NULL, &num_servers);
406 if (!NT_STATUS_IS_OK(status)) {
407 DEBUG(0, ("Failed to traverse temporary database\n"));
408 goto done;
410 state->stat.server.total = num_servers;
412 state->server_ids = talloc_array(state, struct server_id, num_servers);
413 state->server_exists = talloc_array(state, bool, num_servers);
414 if (state->server_ids == NULL || state->server_exists == NULL) {
415 DEBUG(0, ("Out of memory\n"));
416 goto done;
419 state->idx = 0;
420 status = dbwrap_traverse_read(state->id2server_data,
421 wipedbs_traverse_fill_ids,
422 state, NULL);
423 if (!NT_STATUS_IS_OK(status)) {
424 DEBUG(0, ("Failed to traverse temporary database\n"));
425 goto done;
428 ok = serverids_exist(state->server_ids, num_servers, state->server_exists);
429 if (!ok) {
430 DEBUG(0, ("Calling serverids_exist failed\n"));
431 status = NT_STATUS_UNSUCCESSFUL;
432 goto done;
435 state->idx = 0;
436 status = dbwrap_traverse_read(state->id2server_data,
437 wipedbs_traverse_set_exists, state, NULL);
438 if (!NT_STATUS_IS_OK(status)) {
439 DEBUG(0, ("Failed to traverse temporary database\n"));
440 goto done;
442 done:
443 TALLOC_FREE(state->server_ids);
444 TALLOC_FREE(state->server_exists);
445 return status;
448 static int wipedbs_delete_records(struct db_context *db,
449 struct wipedbs_record_marker *records,
450 bool dry_run, bool verbose, int *count)
452 struct wipedbs_record_marker *cur;
453 struct db_record *rec;
454 TDB_DATA val;
455 NTSTATUS status;
456 unsigned num=0, total=0;
458 if (db == NULL) {
459 return 0;
462 for (cur = records; cur != NULL; cur = cur->next) {
463 total++;
464 rec = dbwrap_fetch_locked(db, talloc_tos(), cur->key);
465 if (rec == NULL) {
466 DEBUG(0, ("Failed to fetch record <%s> from %s",
467 cur->desc, dbwrap_name(db)));
468 continue;
470 val = dbwrap_record_get_value(rec);
471 if (tdb_data_equal(val, cur->val)) {
472 if (dry_run) {
473 status = NT_STATUS_OK;
474 } else {
475 status = dbwrap_record_delete(rec);
477 if (NT_STATUS_IS_OK(status)) {
478 num ++;
479 } else {
480 DEBUG(0, ("Failed to delete record <%s> from %s"
481 ": %s\n", cur->desc, dbwrap_name(db),
482 nt_errstr(status)));
484 } else {
485 DEBUG(0, ("Warning: record <%s> from %s changed"
486 ", skip record!\n",
487 cur->desc, dbwrap_name(db)));
489 if (verbose) {
490 d_printf("deleting %s\n", cur->desc);
492 TALLOC_FREE(rec);
495 if (verbose) {
496 d_printf("Deleted %u of %u records from %s\n",
497 num, total, dbwrap_name(db));
500 if (count) {
501 *count += total;
504 return total - num;
507 static int wipedbs_traverse_server_data(struct db_record *rec,
508 void *wipedbs_state)
510 struct wipedbs_state *state = talloc_get_type_abort(
511 wipedbs_state, struct wipedbs_state);
512 bool dry_run = state->testmode;
513 TDB_DATA val = dbwrap_record_get_value(rec);
514 int ret;
515 struct wipedbs_server_data *sd = talloc_get_type_abort(
516 *(void**)val.dptr, struct wipedbs_server_data);
518 if (state->verbose) {
519 d_printf("Server: '%s' %s\n", sd->server_id_str,
520 sd->exists ?
521 "exists" :
522 "does not exist, cleaning up...");
525 if (sd->exists) {
526 return 0;
529 ret = wipedbs_delete_records(state->session_db, sd->session_records,
530 dry_run, state->verbose,
531 &state->stat.session.todelete);
532 state->stat.session.failure += ret;
534 ret = wipedbs_delete_records(state->tcon_db, sd->tcon_records,
535 dry_run, state->verbose,
536 &state->stat.tcon.todelete);
537 state->stat.tcon.failure += ret;
539 ret = wipedbs_delete_records(state->open_db, sd->open_records,
540 dry_run, state->verbose,
541 &state->stat.open.todelete);
542 state->stat.open.failure += ret;
544 return 0;
547 static int net_serverid_wipedbs(struct net_context *c, int argc,
548 const char **argv)
550 int ret = -1;
551 NTSTATUS status;
552 struct wipedbs_state *state = talloc_zero(talloc_tos(),
553 struct wipedbs_state);
555 if (c->display_usage) {
556 d_printf("%s\n%s",
557 _("Usage:"),
558 _("net serverid wipedbs [--test] [--verbose]\n"));
559 d_printf("%s\n%s",
560 _("Example:"),
561 _("net serverid wipedbs -v\n"));
562 return -1;
565 state->now = timeval_current();
566 state->testmode = c->opt_testmode;
567 state->verbose = c->opt_verbose;
569 state->id2server_data = db_open_rbt(state);
570 if (state->id2server_data == NULL) {
571 DEBUG(0, ("Failed to open temporary database\n"));
572 goto done;
575 status = smbXsrv_session_global_traverse(wipedbs_traverse_sessions,
576 state);
577 if (!NT_STATUS_IS_OK(status)) {
578 goto done;
581 status = smbXsrv_tcon_global_traverse(wipedbs_traverse_tcon, state);
582 if (!NT_STATUS_IS_OK(status)) {
583 goto done;
586 status = smbXsrv_open_global_traverse(wipedbs_traverse_open, state);
587 if (!NT_STATUS_IS_OK(status)) {
588 goto done;
591 status = wipedbs_check_server_exists(state);
592 if (!NT_STATUS_IS_OK(status)) {
593 goto done;
596 status = dbwrap_traverse_read(state->id2server_data,
597 wipedbs_traverse_server_data,
598 state, NULL);
599 if (!NT_STATUS_IS_OK(status)) {
600 DEBUG(0, ("Failed to traverse db: %s\n", nt_errstr(status)));
601 goto done;
604 d_printf("Found %d serverids, %d alive and %d disconnected\n",
605 state->stat.server.total,
606 state->stat.server.existing,
607 state->stat.server.disconnected);
608 d_printf("Found %d sessions, %d alive and %d disconnected"
609 ", cleaned up %d of %d entries\n",
610 state->stat.session.total,
611 state->stat.session.total - state->stat.session.todelete,
612 state->stat.session.disconnected,
613 state->stat.session.todelete - state->stat.session.failure,
614 state->stat.session.todelete);
615 d_printf("Found %d tcons, %d alive and %d disconnected"
616 ", cleaned up %d of %d entries\n",
617 state->stat.tcon.total,
618 state->stat.tcon.total - state->stat.tcon.todelete,
619 state->stat.tcon.disconnected,
620 state->stat.tcon.todelete - state->stat.tcon.failure,
621 state->stat.tcon.todelete);
622 d_printf("Found %d opens, %d alive, %d disconnected and %d timed out"
623 ", cleaned up %d of %d entries\n",
624 state->stat.open.total,
625 state->stat.open.total - state->stat.open.todelete
626 - (state->stat.open.disconnected - state->stat.open_timed_out),
627 state->stat.open.disconnected,
628 state->stat.open_timed_out,
629 state->stat.open.todelete - state->stat.open.failure,
630 state->stat.open.todelete);
632 ret = 0;
633 done:
634 talloc_free(state);
635 return ret;
638 int net_serverid(struct net_context *c, int argc, const char **argv)
640 struct functable func[] = {
642 "list",
643 net_serverid_list,
644 NET_TRANSPORT_LOCAL,
645 N_("List all entries from serverid.tdb"),
646 N_("net serverid list\n"
647 " List all entries from serverid.tdb")
650 "wipe",
651 net_serverid_wipe,
652 NET_TRANSPORT_LOCAL,
653 N_("Wipe the serverid.tdb for the current node"),
654 N_("net serverid wipe\n"
655 " Wipe the serverid.tdb for the current node")
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")
665 {NULL, NULL, 0, NULL, NULL}
668 return net_run_function(c, argc, argv, "net serverid", func);