libcli/smb: Allow smb2cli_validate_negotiate_info_done() to ignore NT_STATUS_INVALID_...
[Samba.git] / source3 / utils / net_serverid.c
blob92892e5a4fa0f1a3f46d931984c05d9de9a48924
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 "smbd/globals.h"
28 #include "util_tdb.h"
29 #include "librpc/gen_ndr/ndr_open_files.h"
31 struct wipedbs_record_marker {
32 struct wipedbs_record_marker *prev, *next;
33 TDB_DATA key, val;
34 const char *desc;
37 struct wipedbs_server_data {
38 struct server_id server_id;
39 const char *server_id_str;
40 bool exists;
41 struct wipedbs_record_marker *session_records;
42 struct wipedbs_record_marker *tcon_records;
43 struct wipedbs_record_marker *open_records;
46 struct wipedbs_state {
47 struct db_context *id2server_data;
48 struct {
49 struct {
50 int total;
51 int existing;
52 int disconnected;
53 } server;
54 struct {
55 int total;
56 int disconnected;
57 int todelete;
58 int failure;
59 } session, tcon, open;
60 int open_timed_out;
61 } stat;
62 struct server_id *server_ids;
63 bool *server_exists;
64 int idx;
65 struct db_context *session_db;
66 struct db_context *tcon_db;
67 struct db_context *open_db;
68 struct timeval now;
69 bool testmode;
70 bool verbose;
73 static struct wipedbs_server_data *get_server_data(struct wipedbs_state *state,
74 const struct server_id *id)
76 struct wipedbs_server_data *ret = NULL;
77 TDB_DATA key, val = tdb_null;
78 NTSTATUS status;
80 key = make_tdb_data((const void*)&id->unique_id, sizeof(id->unique_id));
81 status = dbwrap_fetch(state->id2server_data, talloc_tos(), key, &val);
82 if (NT_STATUS_IS_OK(status)) {
83 ret = *(struct wipedbs_server_data**) val.dptr;
84 TALLOC_FREE(val.dptr);
85 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
86 struct server_id_buf idbuf;
88 server_id_str_buf(*id, &idbuf);
90 ret = talloc_zero(state->id2server_data,
91 struct wipedbs_server_data);
92 if (ret == NULL) {
93 DEBUG(0, ("Failed to allocate server entry for %s\n",
94 idbuf.buf));
95 goto done;
97 ret->server_id = *id;
98 ret->server_id_str = talloc_strdup(ret, idbuf.buf);
99 ret->exists = true;
100 val = make_tdb_data((const void*)&ret, sizeof(ret));
101 status = dbwrap_store(state->id2server_data,
102 key, val, TDB_INSERT);
103 if (!NT_STATUS_IS_OK(status)) {
104 DEBUG(0, ("Failed to store server entry for %s: %s\n",
105 idbuf.buf, nt_errstr(status)));
107 goto done;
108 } else {
109 struct server_id_buf idbuf;
110 DEBUG(0, ("Failed to fetch server entry for %s: %s\n",
111 server_id_str_buf(*id, &idbuf), nt_errstr(status)));
112 goto done;
114 if (!server_id_equal(id, &ret->server_id)) {
115 struct server_id_buf idbuf1, idbuf2;
116 DEBUG(0, ("uniq id collision for %s and %s\n",
117 server_id_str_buf(*id, &idbuf1),
118 server_id_str_buf(ret->server_id, &idbuf2)));
119 smb_panic("server_id->unique_id not unique!");
121 done:
122 return ret;
125 static int wipedbs_traverse_sessions(struct smbXsrv_session_global0 *session,
126 void *wipedbs_state)
128 struct wipedbs_state *state =
129 talloc_get_type_abort(wipedbs_state,
130 struct wipedbs_state);
131 struct wipedbs_server_data *sd;
132 struct wipedbs_record_marker *rec;
133 TDB_DATA tmp;
134 int ret = -1;
136 assert(session->num_channels == 1);
138 state->stat.session.total++;
140 sd = get_server_data(state, &session->channels[0].server_id);
141 if (sd == NULL) {
142 goto done;
145 if (server_id_is_disconnected(&sd->server_id)) {
146 state->stat.session.disconnected++;
149 rec = talloc_zero(sd, struct wipedbs_record_marker);
150 if (rec == NULL) {
151 DEBUG(0, ("Out of memory!\n"));
152 goto done;
155 tmp = dbwrap_record_get_key(session->db_rec);
156 rec->key = tdb_data_talloc_copy(rec, tmp);
157 tmp = dbwrap_record_get_value(session->db_rec);
158 rec->val = tdb_data_talloc_copy(rec, tmp);
160 rec->desc = talloc_asprintf(
161 rec, "session[global: %u wire: %llu]",
162 session->session_global_id,
163 (long long unsigned)session->session_wire_id);
165 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
166 (rec->desc == NULL))
168 DEBUG(0, ("Out of memory!\n"));
169 goto done;
172 state->session_db = dbwrap_record_get_db(session->db_rec);
174 DLIST_ADD(sd->session_records, rec);
175 ret = 0;
176 done:
177 return ret;
180 static int wipedbs_traverse_tcon(struct smbXsrv_tcon_global0 *tcon,
181 void *wipedbs_state)
183 struct wipedbs_state *state =
184 talloc_get_type_abort(wipedbs_state,
185 struct wipedbs_state);
186 struct wipedbs_server_data *sd;
187 struct wipedbs_record_marker *rec;
188 TDB_DATA tmp;
189 int ret = -1;
191 state->stat.tcon.total++;
193 sd = get_server_data(state, &tcon->server_id);
194 if (sd == NULL) {
195 goto done;
198 if (server_id_is_disconnected(&sd->server_id)) {
199 state->stat.tcon.disconnected++;
202 rec = talloc_zero(sd, struct wipedbs_record_marker);
203 if (rec == NULL) {
204 DEBUG(0, ("Out of memory!\n"));
205 goto done;
208 tmp = dbwrap_record_get_key(tcon->db_rec);
209 rec->key = tdb_data_talloc_copy(rec, tmp);
210 tmp = dbwrap_record_get_value(tcon->db_rec);
211 rec->val = tdb_data_talloc_copy(rec, tmp);
213 rec->desc = talloc_asprintf(
214 rec, "tcon[global: %u wire: %u session: %u share: %s]",
215 tcon->tcon_global_id, tcon->tcon_wire_id,
216 tcon->session_global_id, tcon->share_name);
218 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
219 (rec->desc == NULL))
221 DEBUG(0, ("Out of memory!\n"));
222 goto done;
225 state->tcon_db = dbwrap_record_get_db(tcon->db_rec);
227 DLIST_ADD(sd->tcon_records, rec);
228 ret = 0;
230 done:
231 return ret;
234 static int wipedbs_traverse_open(struct smbXsrv_open_global0 *open,
235 void *wipedbs_state)
237 struct wipedbs_state *state =
238 talloc_get_type_abort(wipedbs_state,
239 struct wipedbs_state);
240 struct wipedbs_server_data *sd;
241 struct wipedbs_record_marker *rec;
242 TDB_DATA tmp;
243 int ret = -1;
245 state->stat.open.total++;
247 sd = get_server_data(state, &open->server_id);
248 if (sd == NULL) {
249 goto done;
252 if (server_id_is_disconnected(&sd->server_id)) {
253 struct timeval disconnect_time;
254 int64_t tdiff;
255 bool reached;
257 state->stat.open.disconnected++;
259 nttime_to_timeval(&disconnect_time, open->disconnect_time);
260 tdiff = usec_time_diff(&state->now, &disconnect_time);
261 reached = (tdiff >= 1000*open->durable_timeout_msec);
263 if (state->verbose) {
264 TALLOC_CTX *mem_ctx = talloc_new(talloc_tos());
265 enum ndr_err_code ndr_err;
266 struct vfs_default_durable_cookie cookie;
268 ndr_err = ndr_pull_struct_blob(
269 &open->backend_cookie, mem_ctx, &cookie,
270 (ndr_pull_flags_fn_t)ndr_pull_vfs_default_durable_cookie);
271 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
272 d_printf("ndr_pull_struct_blob failed\n");
273 ret = -1;
274 goto done;
277 d_printf("open[%s/%s id: 0x%" PRIx32 "] disconnected at "
278 "[%s] %us ago with timeout of %us "
279 "-%s reached\n",
280 cookie.servicepath, cookie.base_name,
281 open->open_global_id,
282 nt_time_string(mem_ctx, open->disconnect_time),
283 (unsigned)(tdiff/1000000),
284 open->durable_timeout_msec / 1000,
285 reached ? "" : " not");
286 talloc_free(mem_ctx);
289 if (!reached) {
290 ret = 0;
291 goto done;
293 state->stat.open_timed_out++;
296 rec = talloc_zero(sd, struct wipedbs_record_marker);
297 if (rec == NULL) {
298 DEBUG(0, ("Out of memory!\n"));
299 goto done;
302 tmp = dbwrap_record_get_key(open->db_rec);
303 rec->key = tdb_data_talloc_copy(rec, tmp);
304 tmp = dbwrap_record_get_value(open->db_rec);
305 rec->val = tdb_data_talloc_copy(rec, tmp);
307 rec->desc = talloc_asprintf(
308 rec, "open[global: %u persistent: %llu volatile: %llu]",
309 open->open_global_id,
310 (long long unsigned)open->open_persistent_id,
311 (long long unsigned)open->open_volatile_id);
313 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
314 (rec->desc == NULL))
316 DEBUG(0, ("Out of memory!\n"));
317 goto done;
320 state->open_db = dbwrap_record_get_db(open->db_rec);
322 DLIST_ADD(sd->open_records, rec);
323 ret = 0;
325 done:
326 return ret;
329 static int wipedbs_traverse_nop(struct db_record *rec, void *private_data)
331 return 0;
334 static int wipedbs_traverse_fill_ids(struct db_record *rec, void *wipedbs_state)
336 struct wipedbs_state *state = talloc_get_type_abort(
337 wipedbs_state, struct wipedbs_state);
339 TDB_DATA val = dbwrap_record_get_value(rec);
341 struct wipedbs_server_data *sd = talloc_get_type_abort(
342 *(void**)val.dptr, struct wipedbs_server_data);
344 state->server_ids[state->idx] = sd->server_id;
345 state->idx++;
346 return 0;
349 static int wipedbs_traverse_set_exists(struct db_record *rec,
350 void *wipedbs_state)
352 struct wipedbs_state *state = talloc_get_type_abort(
353 wipedbs_state, struct wipedbs_state);
355 TDB_DATA val = dbwrap_record_get_value(rec);
357 struct wipedbs_server_data *sd = talloc_get_type_abort(
358 *(void**)val.dptr, struct wipedbs_server_data);
360 /* assume a stable traverse order for rbt */
361 SMB_ASSERT(server_id_equal(&state->server_ids[state->idx],
362 &sd->server_id));
363 sd->exists = state->server_exists[state->idx];
365 if (sd->exists) {
366 state->stat.server.existing++;
368 if (server_id_is_disconnected(&sd->server_id)) {
369 state->stat.server.disconnected++;
372 state->idx++;
373 return 0;
376 static bool serverids_exist(const struct server_id *ids, int num_ids,
377 bool *results)
379 int i;
381 for (i=0; i<num_ids; i++) {
382 results[i] = serverid_exists(&ids[i]);
385 return true;
389 static NTSTATUS wipedbs_check_server_exists(struct wipedbs_state *state)
391 NTSTATUS status;
392 bool ok;
393 int num_servers;
395 status = dbwrap_traverse_read(state->id2server_data,
396 wipedbs_traverse_nop, NULL, &num_servers);
397 if (!NT_STATUS_IS_OK(status)) {
398 DEBUG(0, ("Failed to traverse temporary database\n"));
399 goto done;
401 state->stat.server.total = num_servers;
403 state->server_ids = talloc_array(state, struct server_id, num_servers);
404 state->server_exists = talloc_array(state, bool, num_servers);
405 if (state->server_ids == NULL || state->server_exists == NULL) {
406 DEBUG(0, ("Out of memory\n"));
407 goto done;
410 state->idx = 0;
411 status = dbwrap_traverse_read(state->id2server_data,
412 wipedbs_traverse_fill_ids,
413 state, NULL);
414 if (!NT_STATUS_IS_OK(status)) {
415 DEBUG(0, ("Failed to traverse temporary database\n"));
416 goto done;
419 ok = serverids_exist(state->server_ids, num_servers, state->server_exists);
420 if (!ok) {
421 DEBUG(0, ("Calling serverids_exist failed\n"));
422 status = NT_STATUS_UNSUCCESSFUL;
423 goto done;
426 state->idx = 0;
427 status = dbwrap_traverse_read(state->id2server_data,
428 wipedbs_traverse_set_exists, state, NULL);
429 if (!NT_STATUS_IS_OK(status)) {
430 DEBUG(0, ("Failed to traverse temporary database\n"));
431 goto done;
433 done:
434 TALLOC_FREE(state->server_ids);
435 TALLOC_FREE(state->server_exists);
436 return status;
439 static int wipedbs_delete_records(struct db_context *db,
440 struct wipedbs_record_marker *records,
441 bool dry_run, bool verbose, int *count)
443 struct wipedbs_record_marker *cur;
444 struct db_record *rec;
445 TDB_DATA val;
446 NTSTATUS status;
447 unsigned num=0, total=0;
449 if (db == NULL) {
450 return 0;
453 for (cur = records; cur != NULL; cur = cur->next) {
454 total++;
455 rec = dbwrap_fetch_locked(db, talloc_tos(), cur->key);
456 if (rec == NULL) {
457 DEBUG(0, ("Failed to fetch record <%s> from %s",
458 cur->desc, dbwrap_name(db)));
459 continue;
461 val = dbwrap_record_get_value(rec);
462 if (tdb_data_equal(val, cur->val)) {
463 if (dry_run) {
464 status = NT_STATUS_OK;
465 } else {
466 status = dbwrap_record_delete(rec);
468 if (NT_STATUS_IS_OK(status)) {
469 num ++;
470 } else {
471 DEBUG(0, ("Failed to delete record <%s> from %s"
472 ": %s\n", cur->desc, dbwrap_name(db),
473 nt_errstr(status)));
475 } else {
476 DEBUG(0, ("Warning: record <%s> from %s changed"
477 ", skip record!\n",
478 cur->desc, dbwrap_name(db)));
480 if (verbose) {
481 d_printf("deleting %s\n", cur->desc);
483 TALLOC_FREE(rec);
486 if (verbose) {
487 d_printf("Deleted %u of %u records from %s\n",
488 num, total, dbwrap_name(db));
491 if (count) {
492 *count += total;
495 return total - num;
498 static int wipedbs_traverse_server_data(struct db_record *rec,
499 void *wipedbs_state)
501 struct wipedbs_state *state = talloc_get_type_abort(
502 wipedbs_state, struct wipedbs_state);
503 bool dry_run = state->testmode;
504 TDB_DATA val = dbwrap_record_get_value(rec);
505 int ret;
506 struct wipedbs_server_data *sd = talloc_get_type_abort(
507 *(void**)val.dptr, struct wipedbs_server_data);
509 if (state->verbose) {
510 d_printf("Server: '%s' %s\n", sd->server_id_str,
511 sd->exists ?
512 "exists" :
513 "does not exist, cleaning up...");
516 if (sd->exists) {
517 return 0;
520 ret = wipedbs_delete_records(state->session_db, sd->session_records,
521 dry_run, state->verbose,
522 &state->stat.session.todelete);
523 state->stat.session.failure += ret;
525 ret = wipedbs_delete_records(state->tcon_db, sd->tcon_records,
526 dry_run, state->verbose,
527 &state->stat.tcon.todelete);
528 state->stat.tcon.failure += ret;
530 ret = wipedbs_delete_records(state->open_db, sd->open_records,
531 dry_run, state->verbose,
532 &state->stat.open.todelete);
533 state->stat.open.failure += ret;
535 return 0;
538 static int net_serverid_wipedbs(struct net_context *c, int argc,
539 const char **argv)
541 int ret = -1;
542 NTSTATUS status;
543 struct wipedbs_state *state = talloc_zero(talloc_tos(),
544 struct wipedbs_state);
546 if (c->display_usage) {
547 d_printf("%s\n%s",
548 _("Usage:"),
549 _("net serverid wipedbs [--test] [--verbose]\n"));
550 d_printf("%s\n%s",
551 _("Example:"),
552 _("net serverid wipedbs -v\n"));
553 return -1;
556 state->now = timeval_current();
557 state->testmode = c->opt_testmode;
558 state->verbose = c->opt_verbose;
560 state->id2server_data = db_open_rbt(state);
561 if (state->id2server_data == NULL) {
562 DEBUG(0, ("Failed to open temporary database\n"));
563 goto done;
566 status = smbXsrv_session_global_traverse(wipedbs_traverse_sessions,
567 state);
568 if (!NT_STATUS_IS_OK(status)) {
569 goto done;
572 status = smbXsrv_tcon_global_traverse(wipedbs_traverse_tcon, state);
573 if (!NT_STATUS_IS_OK(status)) {
574 goto done;
577 status = smbXsrv_open_global_traverse(wipedbs_traverse_open, state);
578 if (!NT_STATUS_IS_OK(status)) {
579 goto done;
582 status = wipedbs_check_server_exists(state);
583 if (!NT_STATUS_IS_OK(status)) {
584 goto done;
587 status = dbwrap_traverse_read(state->id2server_data,
588 wipedbs_traverse_server_data,
589 state, NULL);
590 if (!NT_STATUS_IS_OK(status)) {
591 DEBUG(0, ("Failed to traverse db: %s\n", nt_errstr(status)));
592 goto done;
595 d_printf("Found %d serverids, %d alive and %d disconnected\n",
596 state->stat.server.total,
597 state->stat.server.existing,
598 state->stat.server.disconnected);
599 d_printf("Found %d sessions, %d alive and %d disconnected"
600 ", cleaned up %d of %d entries\n",
601 state->stat.session.total,
602 state->stat.session.total - state->stat.session.todelete,
603 state->stat.session.disconnected,
604 state->stat.session.todelete - state->stat.session.failure,
605 state->stat.session.todelete);
606 d_printf("Found %d tcons, %d alive and %d disconnected"
607 ", cleaned up %d of %d entries\n",
608 state->stat.tcon.total,
609 state->stat.tcon.total - state->stat.tcon.todelete,
610 state->stat.tcon.disconnected,
611 state->stat.tcon.todelete - state->stat.tcon.failure,
612 state->stat.tcon.todelete);
613 d_printf("Found %d opens, %d alive, %d disconnected and %d timed out"
614 ", cleaned up %d of %d entries\n",
615 state->stat.open.total,
616 state->stat.open.total - state->stat.open.todelete
617 - (state->stat.open.disconnected - state->stat.open_timed_out),
618 state->stat.open.disconnected,
619 state->stat.open_timed_out,
620 state->stat.open.todelete - state->stat.open.failure,
621 state->stat.open.todelete);
623 ret = 0;
624 done:
625 talloc_free(state);
626 return ret;
629 static int net_serverid_exists(struct net_context *c, int argc,
630 const char **argv)
632 struct server_id pid;
633 bool ok;
635 if ((argc != 1) || (c->display_usage)) {
636 d_printf("Usage:\n"
637 "net serverid exists <serverid>\n");
638 return -1;
641 pid = server_id_from_string(get_my_vnn(), argv[0]);
642 ok = serverid_exists(&pid);
644 if (ok) {
645 d_printf("%s exists\n", argv[0]);
646 } else {
647 d_printf("%s does not exist\n", argv[0]);
650 return 0;
653 int net_serverid(struct net_context *c, int argc, const char **argv)
655 struct functable func[] = {
657 "wipedbs",
658 net_serverid_wipedbs,
659 NET_TRANSPORT_LOCAL,
660 N_("Clean dead entries from temporary databases"),
661 N_("net serverid wipedbs\n"
662 " Clean dead entries from temporary databases")
665 "exists",
666 net_serverid_exists,
667 NET_TRANSPORT_LOCAL,
668 N_("Show existence of a serverid"),
669 N_("net serverid exists <id>")
671 {NULL, NULL, 0, NULL, NULL}
674 return net_run_function(c, argc, argv, "net serverid", func);