2 Samba Unix/Linux SMB client library
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/>.
21 #include "utils/net.h"
22 #include "lib/util/server_id.h"
23 #include "dbwrap/dbwrap.h"
24 #include "dbwrap/dbwrap_rbt.h"
27 #include "smbd/globals.h"
28 #include "source3/smbd/smbXsrv_session.h"
29 #include "smbd/smbXsrv_open.h"
31 #include "librpc/gen_ndr/ndr_open_files.h"
33 struct wipedbs_record_marker
{
34 struct wipedbs_record_marker
*prev
, *next
;
39 struct wipedbs_server_data
{
40 struct server_id server_id
;
41 const char *server_id_str
;
43 struct wipedbs_record_marker
*session_records
;
44 struct wipedbs_record_marker
*tcon_records
;
45 struct wipedbs_record_marker
*open_records
;
48 struct wipedbs_state
{
49 struct db_context
*id2server_data
;
61 } session
, tcon
, open
;
64 struct server_id
*server_ids
;
67 struct db_context
*session_db
;
68 struct db_context
*tcon_db
;
69 struct db_context
*open_db
;
75 static struct wipedbs_server_data
*get_server_data(struct wipedbs_state
*state
,
76 const struct server_id
*id
)
78 struct wipedbs_server_data
*ret
= NULL
;
79 TDB_DATA key
, val
= tdb_null
;
82 key
= make_tdb_data((const void*)&id
->unique_id
, sizeof(id
->unique_id
));
83 status
= dbwrap_fetch(state
->id2server_data
, talloc_tos(), key
, &val
);
84 if (NT_STATUS_IS_OK(status
)) {
85 ret
= *(struct wipedbs_server_data
**) val
.dptr
;
86 TALLOC_FREE(val
.dptr
);
87 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
88 struct server_id_buf idbuf
;
90 server_id_str_buf(*id
, &idbuf
);
92 ret
= talloc_zero(state
->id2server_data
,
93 struct wipedbs_server_data
);
95 DEBUG(0, ("Failed to allocate server entry for %s\n",
100 ret
->server_id_str
= talloc_strdup(ret
, idbuf
.buf
);
102 val
= make_tdb_data((const void*)&ret
, sizeof(ret
));
103 status
= dbwrap_store(state
->id2server_data
,
104 key
, val
, TDB_INSERT
);
105 if (!NT_STATUS_IS_OK(status
)) {
106 DEBUG(0, ("Failed to store server entry for %s: %s\n",
107 idbuf
.buf
, nt_errstr(status
)));
111 struct server_id_buf idbuf
;
112 DEBUG(0, ("Failed to fetch server entry for %s: %s\n",
113 server_id_str_buf(*id
, &idbuf
), nt_errstr(status
)));
116 if (!server_id_equal(id
, &ret
->server_id
)) {
117 struct server_id_buf idbuf1
, idbuf2
;
118 DEBUG(0, ("uniq id collision for %s and %s\n",
119 server_id_str_buf(*id
, &idbuf1
),
120 server_id_str_buf(ret
->server_id
, &idbuf2
)));
121 smb_panic("server_id->unique_id not unique!");
127 static int wipedbs_traverse_sessions(struct smbXsrv_session_global0
*session
,
130 struct wipedbs_state
*state
=
131 talloc_get_type_abort(wipedbs_state
,
132 struct wipedbs_state
);
133 struct wipedbs_server_data
*sd
;
134 struct wipedbs_record_marker
*rec
;
138 assert(session
->num_channels
== 1);
140 state
->stat
.session
.total
++;
142 sd
= get_server_data(state
, &session
->channels
[0].server_id
);
147 if (server_id_is_disconnected(&sd
->server_id
)) {
148 state
->stat
.session
.disconnected
++;
151 rec
= talloc_zero(sd
, struct wipedbs_record_marker
);
153 DEBUG(0, ("Out of memory!\n"));
157 tmp
= dbwrap_record_get_key(session
->db_rec
);
158 rec
->key
= tdb_data_talloc_copy(rec
, tmp
);
159 tmp
= dbwrap_record_get_value(session
->db_rec
);
160 rec
->val
= tdb_data_talloc_copy(rec
, tmp
);
162 rec
->desc
= talloc_asprintf(
163 rec
, "session[global: %u wire: %llu]",
164 session
->session_global_id
,
165 (long long unsigned)session
->session_wire_id
);
167 if ((rec
->key
.dptr
== NULL
) || (rec
->val
.dptr
== NULL
) ||
170 DEBUG(0, ("Out of memory!\n"));
174 state
->session_db
= dbwrap_record_get_db(session
->db_rec
);
176 DLIST_ADD(sd
->session_records
, rec
);
182 static int wipedbs_traverse_tcon(struct smbXsrv_tcon_global0
*tcon
,
185 struct wipedbs_state
*state
=
186 talloc_get_type_abort(wipedbs_state
,
187 struct wipedbs_state
);
188 struct wipedbs_server_data
*sd
;
189 struct wipedbs_record_marker
*rec
;
193 state
->stat
.tcon
.total
++;
195 sd
= get_server_data(state
, &tcon
->server_id
);
200 if (server_id_is_disconnected(&sd
->server_id
)) {
201 state
->stat
.tcon
.disconnected
++;
204 rec
= talloc_zero(sd
, struct wipedbs_record_marker
);
206 DEBUG(0, ("Out of memory!\n"));
210 tmp
= dbwrap_record_get_key(tcon
->db_rec
);
211 rec
->key
= tdb_data_talloc_copy(rec
, tmp
);
212 tmp
= dbwrap_record_get_value(tcon
->db_rec
);
213 rec
->val
= tdb_data_talloc_copy(rec
, tmp
);
215 rec
->desc
= talloc_asprintf(
216 rec
, "tcon[global: %u wire: %u session: %u share: %s]",
217 tcon
->tcon_global_id
, tcon
->tcon_wire_id
,
218 tcon
->session_global_id
, tcon
->share_name
);
220 if ((rec
->key
.dptr
== NULL
) || (rec
->val
.dptr
== NULL
) ||
223 DEBUG(0, ("Out of memory!\n"));
227 state
->tcon_db
= dbwrap_record_get_db(tcon
->db_rec
);
229 DLIST_ADD(sd
->tcon_records
, rec
);
236 static int wipedbs_traverse_open(struct db_record
*db_rec
,
237 struct smbXsrv_open_global0
*open
,
240 struct wipedbs_state
*state
=
241 talloc_get_type_abort(wipedbs_state
,
242 struct wipedbs_state
);
243 struct wipedbs_server_data
*sd
;
244 struct wipedbs_record_marker
*rec
;
248 state
->stat
.open
.total
++;
250 sd
= get_server_data(state
, &open
->server_id
);
255 if (server_id_is_disconnected(&sd
->server_id
)) {
256 struct timeval disconnect_time
;
260 state
->stat
.open
.disconnected
++;
262 nttime_to_timeval(&disconnect_time
, open
->disconnect_time
);
263 tdiff
= usec_time_diff(&state
->now
, &disconnect_time
);
264 reached
= (tdiff
>= INT64_C(1000)*open
->durable_timeout_msec
);
266 if (state
->verbose
) {
267 TALLOC_CTX
*mem_ctx
= talloc_new(talloc_tos());
268 enum ndr_err_code ndr_err
;
269 struct vfs_default_durable_cookie cookie
;
271 ndr_err
= ndr_pull_struct_blob(
272 &open
->backend_cookie
, mem_ctx
, &cookie
,
273 (ndr_pull_flags_fn_t
)ndr_pull_vfs_default_durable_cookie
);
274 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
275 d_printf("ndr_pull_struct_blob failed\n");
280 d_printf("open[%s/%s id: 0x%" PRIx32
"] disconnected at "
281 "[%s] %us ago with timeout of %us "
283 cookie
.servicepath
, cookie
.base_name
,
284 open
->open_global_id
,
285 nt_time_string(mem_ctx
, open
->disconnect_time
),
286 (unsigned)(tdiff
/1000000),
287 open
->durable_timeout_msec
/ 1000,
288 reached
? "" : " not");
289 talloc_free(mem_ctx
);
296 state
->stat
.open_timed_out
++;
299 rec
= talloc_zero(sd
, struct wipedbs_record_marker
);
301 DEBUG(0, ("Out of memory!\n"));
305 tmp
= dbwrap_record_get_key(db_rec
);
306 rec
->key
= tdb_data_talloc_copy(rec
, tmp
);
307 tmp
= dbwrap_record_get_value(db_rec
);
308 rec
->val
= tdb_data_talloc_copy(rec
, tmp
);
310 rec
->desc
= talloc_asprintf(
311 rec
, "open[global: %u persistent: %llu volatile: %llu]",
312 open
->open_global_id
,
313 (long long unsigned)open
->open_persistent_id
,
314 (long long unsigned)open
->open_volatile_id
);
316 if ((rec
->key
.dptr
== NULL
) || (rec
->val
.dptr
== NULL
) ||
319 DEBUG(0, ("Out of memory!\n"));
323 state
->open_db
= dbwrap_record_get_db(db_rec
);
325 DLIST_ADD(sd
->open_records
, rec
);
332 static int wipedbs_traverse_nop(struct db_record
*rec
, void *private_data
)
337 static int wipedbs_traverse_fill_ids(struct db_record
*rec
, void *wipedbs_state
)
339 struct wipedbs_state
*state
= talloc_get_type_abort(
340 wipedbs_state
, struct wipedbs_state
);
342 TDB_DATA val
= dbwrap_record_get_value(rec
);
344 struct wipedbs_server_data
*sd
= talloc_get_type_abort(
345 *(void**)val
.dptr
, struct wipedbs_server_data
);
347 state
->server_ids
[state
->idx
] = sd
->server_id
;
352 static int wipedbs_traverse_set_exists(struct db_record
*rec
,
355 struct wipedbs_state
*state
= talloc_get_type_abort(
356 wipedbs_state
, struct wipedbs_state
);
358 TDB_DATA val
= dbwrap_record_get_value(rec
);
360 struct wipedbs_server_data
*sd
= talloc_get_type_abort(
361 *(void**)val
.dptr
, struct wipedbs_server_data
);
363 /* assume a stable traverse order for rbt */
364 SMB_ASSERT(server_id_equal(&state
->server_ids
[state
->idx
],
366 sd
->exists
= state
->server_exists
[state
->idx
];
369 state
->stat
.server
.existing
++;
371 if (server_id_is_disconnected(&sd
->server_id
)) {
372 state
->stat
.server
.disconnected
++;
379 static bool serverids_exist(const struct server_id
*ids
, int num_ids
,
384 for (i
=0; i
<num_ids
; i
++) {
385 results
[i
] = serverid_exists(&ids
[i
]);
392 static NTSTATUS
wipedbs_check_server_exists(struct wipedbs_state
*state
)
398 status
= dbwrap_traverse_read(state
->id2server_data
,
399 wipedbs_traverse_nop
, NULL
, &num_servers
);
400 if (!NT_STATUS_IS_OK(status
)) {
401 DEBUG(0, ("Failed to traverse temporary database\n"));
404 state
->stat
.server
.total
= num_servers
;
406 state
->server_ids
= talloc_array(state
, struct server_id
, num_servers
);
407 state
->server_exists
= talloc_array(state
, bool, num_servers
);
408 if (state
->server_ids
== NULL
|| state
->server_exists
== NULL
) {
409 DEBUG(0, ("Out of memory\n"));
414 status
= dbwrap_traverse_read(state
->id2server_data
,
415 wipedbs_traverse_fill_ids
,
417 if (!NT_STATUS_IS_OK(status
)) {
418 DEBUG(0, ("Failed to traverse temporary database\n"));
422 ok
= serverids_exist(state
->server_ids
, num_servers
, state
->server_exists
);
424 DEBUG(0, ("Calling serverids_exist failed\n"));
425 status
= NT_STATUS_UNSUCCESSFUL
;
430 status
= dbwrap_traverse_read(state
->id2server_data
,
431 wipedbs_traverse_set_exists
, state
, NULL
);
432 if (!NT_STATUS_IS_OK(status
)) {
433 DEBUG(0, ("Failed to traverse temporary database\n"));
437 TALLOC_FREE(state
->server_ids
);
438 TALLOC_FREE(state
->server_exists
);
442 struct wipedbs_delete_state
{
443 struct wipedbs_record_marker
*cur
;
450 static void wipedbs_delete_fn(
451 struct db_record
*rec
, TDB_DATA value
, void *private_data
)
453 struct db_context
*db
= dbwrap_record_get_db(rec
);
454 struct wipedbs_delete_state
*state
= private_data
;
455 struct wipedbs_record_marker
*cur
= state
->cur
;
456 NTSTATUS status
= NT_STATUS_OK
;
460 if (!tdb_data_equal(value
, cur
->val
)) {
461 DBG_ERR("Warning: record <%s> from %s changed,"
463 cur
->desc
, dbwrap_name(db
));
467 if (state
->verbose
) {
468 d_printf("deleting %s\n", cur
->desc
);
471 if (!state
->dry_run
) {
472 status
= dbwrap_record_delete(rec
);
475 if (!NT_STATUS_IS_OK(status
)) {
476 DBG_ERR("Failed to delete record <%s> from %s: %s\n",
486 static int wipedbs_delete_records(struct db_context
*db
,
487 struct wipedbs_record_marker
*records
,
488 bool dry_run
, bool verbose
, int *count
)
490 struct wipedbs_delete_state state
= {
491 .verbose
= verbose
, .dry_run
= dry_run
,
498 for (state
.cur
= records
;
500 state
.cur
= state
.cur
->next
) {
502 NTSTATUS status
= dbwrap_do_locked(
503 db
, state
.cur
->key
, wipedbs_delete_fn
, &state
);
505 if (!NT_STATUS_IS_OK(status
)) {
506 DBG_ERR("dbwrap_do_locked failed for record <%s> "
514 d_printf("Deleted %zu of %zu records from %s\n",
521 *count
+= state
.total
;
524 return state
.total
- state
.num
;
527 static int wipedbs_traverse_server_data(struct db_record
*rec
,
530 struct wipedbs_state
*state
= talloc_get_type_abort(
531 wipedbs_state
, struct wipedbs_state
);
532 bool dry_run
= state
->testmode
;
533 TDB_DATA val
= dbwrap_record_get_value(rec
);
535 struct wipedbs_server_data
*sd
= talloc_get_type_abort(
536 *(void**)val
.dptr
, struct wipedbs_server_data
);
538 if (state
->verbose
) {
539 d_printf("Server: '%s' %s\n", sd
->server_id_str
,
542 "does not exist, cleaning up...");
549 ret
= wipedbs_delete_records(state
->session_db
, sd
->session_records
,
550 dry_run
, state
->verbose
,
551 &state
->stat
.session
.todelete
);
552 state
->stat
.session
.failure
+= ret
;
554 ret
= wipedbs_delete_records(state
->tcon_db
, sd
->tcon_records
,
555 dry_run
, state
->verbose
,
556 &state
->stat
.tcon
.todelete
);
557 state
->stat
.tcon
.failure
+= ret
;
559 ret
= wipedbs_delete_records(state
->open_db
, sd
->open_records
,
560 dry_run
, state
->verbose
,
561 &state
->stat
.open
.todelete
);
562 state
->stat
.open
.failure
+= ret
;
567 static int net_serverid_wipedbs(struct net_context
*c
, int argc
,
572 struct wipedbs_state
*state
= talloc_zero(talloc_tos(),
573 struct wipedbs_state
);
575 if (c
->display_usage
) {
578 _("net serverid wipedbs [--test] [--verbose]\n"));
581 _("net serverid wipedbs -v\n"));
585 state
->now
= timeval_current();
586 state
->testmode
= c
->opt_testmode
;
587 state
->verbose
= c
->opt_verbose
;
589 state
->id2server_data
= db_open_rbt(state
);
590 if (state
->id2server_data
== NULL
) {
591 DEBUG(0, ("Failed to open temporary database\n"));
595 status
= smbXsrv_session_global_traverse(wipedbs_traverse_sessions
,
597 if (!NT_STATUS_IS_OK(status
)) {
601 status
= smbXsrv_tcon_global_traverse(wipedbs_traverse_tcon
, state
);
602 if (!NT_STATUS_IS_OK(status
)) {
606 status
= smbXsrv_open_global_traverse(wipedbs_traverse_open
, state
);
607 if (!NT_STATUS_IS_OK(status
)) {
611 status
= wipedbs_check_server_exists(state
);
612 if (!NT_STATUS_IS_OK(status
)) {
616 status
= dbwrap_traverse_read(state
->id2server_data
,
617 wipedbs_traverse_server_data
,
619 if (!NT_STATUS_IS_OK(status
)) {
620 DEBUG(0, ("Failed to traverse db: %s\n", nt_errstr(status
)));
624 d_printf("Found %d serverids, %d alive and %d disconnected\n",
625 state
->stat
.server
.total
,
626 state
->stat
.server
.existing
,
627 state
->stat
.server
.disconnected
);
628 d_printf("Found %d sessions, %d alive and %d disconnected"
629 ", cleaned up %d of %d entries\n",
630 state
->stat
.session
.total
,
631 state
->stat
.session
.total
- state
->stat
.session
.todelete
,
632 state
->stat
.session
.disconnected
,
633 state
->stat
.session
.todelete
- state
->stat
.session
.failure
,
634 state
->stat
.session
.todelete
);
635 d_printf("Found %d tcons, %d alive and %d disconnected"
636 ", cleaned up %d of %d entries\n",
637 state
->stat
.tcon
.total
,
638 state
->stat
.tcon
.total
- state
->stat
.tcon
.todelete
,
639 state
->stat
.tcon
.disconnected
,
640 state
->stat
.tcon
.todelete
- state
->stat
.tcon
.failure
,
641 state
->stat
.tcon
.todelete
);
642 d_printf("Found %d opens, %d alive, %d disconnected and %d timed out"
643 ", cleaned up %d of %d entries\n",
644 state
->stat
.open
.total
,
645 state
->stat
.open
.total
- state
->stat
.open
.todelete
646 - (state
->stat
.open
.disconnected
- state
->stat
.open_timed_out
),
647 state
->stat
.open
.disconnected
,
648 state
->stat
.open_timed_out
,
649 state
->stat
.open
.todelete
- state
->stat
.open
.failure
,
650 state
->stat
.open
.todelete
);
658 static int net_serverid_exists(struct net_context
*c
, int argc
,
661 struct server_id pid
;
664 if ((argc
!= 1) || (c
->display_usage
)) {
666 "net serverid exists <serverid>\n");
670 pid
= server_id_from_string(get_my_vnn(), argv
[0]);
671 ok
= serverid_exists(&pid
);
674 d_printf("%s exists\n", argv
[0]);
676 d_printf("%s does not exist\n", argv
[0]);
682 int net_serverid(struct net_context
*c
, int argc
, const char **argv
)
684 struct functable func
[] = {
687 net_serverid_wipedbs
,
689 N_("Clean dead entries from temporary databases"),
690 N_("net serverid wipedbs\n"
691 " Clean dead entries from temporary databases")
697 N_("Show existence of a serverid"),
698 N_("net serverid exists <id>")
700 {NULL
, NULL
, 0, NULL
, NULL
}
703 return net_run_function(c
, argc
, argv
, "net serverid", func
);