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"
29 #include "librpc/gen_ndr/ndr_open_files.h"
31 struct wipedbs_record_marker
{
32 struct wipedbs_record_marker
*prev
, *next
;
37 struct wipedbs_server_data
{
38 struct server_id server_id
;
39 const char *server_id_str
;
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
;
59 } session
, tcon
, open
;
62 struct server_id
*server_ids
;
65 struct db_context
*session_db
;
66 struct db_context
*tcon_db
;
67 struct db_context
*open_db
;
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
;
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
);
93 DEBUG(0, ("Failed to allocate server entry for %s\n",
98 ret
->server_id_str
= talloc_strdup(ret
, idbuf
.buf
);
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
)));
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
)));
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!");
125 static int wipedbs_traverse_sessions(struct smbXsrv_session_global0
*session
,
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
;
136 assert(session
->num_channels
== 1);
138 state
->stat
.session
.total
++;
140 sd
= get_server_data(state
, &session
->channels
[0].server_id
);
145 if (server_id_is_disconnected(&sd
->server_id
)) {
146 state
->stat
.session
.disconnected
++;
149 rec
= talloc_zero(sd
, struct wipedbs_record_marker
);
151 DEBUG(0, ("Out of memory!\n"));
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
) ||
168 DEBUG(0, ("Out of memory!\n"));
172 state
->session_db
= dbwrap_record_get_db(session
->db_rec
);
174 DLIST_ADD(sd
->session_records
, rec
);
180 static int wipedbs_traverse_tcon(struct smbXsrv_tcon_global0
*tcon
,
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
;
191 state
->stat
.tcon
.total
++;
193 sd
= get_server_data(state
, &tcon
->server_id
);
198 if (server_id_is_disconnected(&sd
->server_id
)) {
199 state
->stat
.tcon
.disconnected
++;
202 rec
= talloc_zero(sd
, struct wipedbs_record_marker
);
204 DEBUG(0, ("Out of memory!\n"));
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
) ||
221 DEBUG(0, ("Out of memory!\n"));
225 state
->tcon_db
= dbwrap_record_get_db(tcon
->db_rec
);
227 DLIST_ADD(sd
->tcon_records
, rec
);
234 static int wipedbs_traverse_open(struct smbXsrv_open_global0
*open
,
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
;
245 state
->stat
.open
.total
++;
247 sd
= get_server_data(state
, &open
->server_id
);
252 if (server_id_is_disconnected(&sd
->server_id
)) {
253 struct timeval disconnect_time
;
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");
277 d_printf("open[%s/%s id: 0x%" PRIx32
"] disconnected at "
278 "[%s] %us ago with timeout of %us "
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
);
293 state
->stat
.open_timed_out
++;
296 rec
= talloc_zero(sd
, struct wipedbs_record_marker
);
298 DEBUG(0, ("Out of memory!\n"));
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
) ||
316 DEBUG(0, ("Out of memory!\n"));
320 state
->open_db
= dbwrap_record_get_db(open
->db_rec
);
322 DLIST_ADD(sd
->open_records
, rec
);
329 static int wipedbs_traverse_nop(struct db_record
*rec
, void *private_data
)
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
;
349 static int wipedbs_traverse_set_exists(struct db_record
*rec
,
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
],
363 sd
->exists
= state
->server_exists
[state
->idx
];
366 state
->stat
.server
.existing
++;
368 if (server_id_is_disconnected(&sd
->server_id
)) {
369 state
->stat
.server
.disconnected
++;
376 static bool serverids_exist(const struct server_id
*ids
, int num_ids
,
381 for (i
=0; i
<num_ids
; i
++) {
382 results
[i
] = serverid_exists(&ids
[i
]);
389 static NTSTATUS
wipedbs_check_server_exists(struct wipedbs_state
*state
)
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"));
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"));
411 status
= dbwrap_traverse_read(state
->id2server_data
,
412 wipedbs_traverse_fill_ids
,
414 if (!NT_STATUS_IS_OK(status
)) {
415 DEBUG(0, ("Failed to traverse temporary database\n"));
419 ok
= serverids_exist(state
->server_ids
, num_servers
, state
->server_exists
);
421 DEBUG(0, ("Calling serverids_exist failed\n"));
422 status
= NT_STATUS_UNSUCCESSFUL
;
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"));
434 TALLOC_FREE(state
->server_ids
);
435 TALLOC_FREE(state
->server_exists
);
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
;
447 unsigned num
=0, total
=0;
453 for (cur
= records
; cur
!= NULL
; cur
= cur
->next
) {
455 rec
= dbwrap_fetch_locked(db
, talloc_tos(), cur
->key
);
457 DEBUG(0, ("Failed to fetch record <%s> from %s",
458 cur
->desc
, dbwrap_name(db
)));
461 val
= dbwrap_record_get_value(rec
);
462 if (tdb_data_equal(val
, cur
->val
)) {
464 status
= NT_STATUS_OK
;
466 status
= dbwrap_record_delete(rec
);
468 if (NT_STATUS_IS_OK(status
)) {
471 DEBUG(0, ("Failed to delete record <%s> from %s"
472 ": %s\n", cur
->desc
, dbwrap_name(db
),
476 DEBUG(0, ("Warning: record <%s> from %s changed"
478 cur
->desc
, dbwrap_name(db
)));
481 d_printf("deleting %s\n", cur
->desc
);
487 d_printf("Deleted %u of %u records from %s\n",
488 num
, total
, dbwrap_name(db
));
498 static int wipedbs_traverse_server_data(struct db_record
*rec
,
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
);
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
,
513 "does not exist, cleaning up...");
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
;
538 static int net_serverid_wipedbs(struct net_context
*c
, int argc
,
543 struct wipedbs_state
*state
= talloc_zero(talloc_tos(),
544 struct wipedbs_state
);
546 if (c
->display_usage
) {
549 _("net serverid wipedbs [--test] [--verbose]\n"));
552 _("net serverid wipedbs -v\n"));
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"));
566 status
= smbXsrv_session_global_traverse(wipedbs_traverse_sessions
,
568 if (!NT_STATUS_IS_OK(status
)) {
572 status
= smbXsrv_tcon_global_traverse(wipedbs_traverse_tcon
, state
);
573 if (!NT_STATUS_IS_OK(status
)) {
577 status
= smbXsrv_open_global_traverse(wipedbs_traverse_open
, state
);
578 if (!NT_STATUS_IS_OK(status
)) {
582 status
= wipedbs_check_server_exists(state
);
583 if (!NT_STATUS_IS_OK(status
)) {
587 status
= dbwrap_traverse_read(state
->id2server_data
,
588 wipedbs_traverse_server_data
,
590 if (!NT_STATUS_IS_OK(status
)) {
591 DEBUG(0, ("Failed to traverse db: %s\n", nt_errstr(status
)));
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
);
629 static int net_serverid_exists(struct net_context
*c
, int argc
,
632 struct server_id pid
;
635 if ((argc
!= 1) || (c
->display_usage
)) {
637 "net serverid exists <serverid>\n");
641 pid
= server_id_from_string(get_my_vnn(), argv
[0]);
642 ok
= serverid_exists(&pid
);
645 d_printf("%s exists\n", argv
[0]);
647 d_printf("%s does not exist\n", argv
[0]);
653 int net_serverid(struct net_context
*c
, int argc
, const char **argv
)
655 struct functable func
[] = {
658 net_serverid_wipedbs
,
660 N_("Clean dead entries from temporary databases"),
661 N_("net serverid wipedbs\n"
662 " Clean dead entries from temporary databases")
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
);