libads: abstract out SASL wrapping code
[Samba.git] / source3 / utils / net_serverid.c
blob942f7a4902411451b6a71c34ea756157c7e7a28d
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 static int net_serverid_list_fn(const struct server_id *id,
33 uint32_t msg_flags, void *priv)
35 struct server_id_buf idbuf;
36 d_printf("%s %llu 0x%x\n", server_id_str_buf(*id, &idbuf),
37 (unsigned long long)id->unique_id,
38 (unsigned int)msg_flags);
39 return 0;
42 static int net_serverid_list(struct net_context *c, int argc,
43 const char **argv)
45 d_printf("pid unique_id msg_flags\n");
46 return serverid_traverse_read(net_serverid_list_fn, NULL) ? 0 : -1;
49 static int net_serverid_wipe_fn(struct db_record *rec,
50 const struct server_id *id,
51 uint32_t msg_flags, void *private_data)
53 NTSTATUS status;
55 if (!procid_is_local(id)) {
56 return 0;
58 status = dbwrap_record_delete(rec);
59 if (!NT_STATUS_IS_OK(status)) {
60 struct server_id_buf idbuf;
61 DEBUG(1, ("Could not delete serverid.tdb record %s: %s\n",
62 server_id_str_buf(*id, &idbuf), nt_errstr(status)));
64 return 0;
67 static int net_serverid_wipe(struct net_context *c, int argc,
68 const char **argv)
70 return serverid_traverse(net_serverid_wipe_fn, NULL) ? 0 : -1;
74 struct wipedbs_record_marker {
75 struct wipedbs_record_marker *prev, *next;
76 TDB_DATA key, val;
77 const char *desc;
80 struct wipedbs_server_data {
81 struct server_id server_id;
82 const char *server_id_str;
83 bool exists;
84 struct wipedbs_record_marker *session_records;
85 struct wipedbs_record_marker *tcon_records;
86 struct wipedbs_record_marker *open_records;
89 struct wipedbs_state {
90 struct db_context *id2server_data;
91 struct {
92 struct {
93 int total;
94 int existing;
95 int disconnected;
96 } server;
97 struct {
98 int total;
99 int disconnected;
100 int todelete;
101 int failure;
102 } session, tcon, open;
103 int open_timed_out;
104 } stat;
105 struct server_id *server_ids;
106 bool *server_exists;
107 int idx;
108 struct db_context *session_db;
109 struct db_context *tcon_db;
110 struct db_context *open_db;
111 struct timeval now;
112 bool testmode;
113 bool verbose;
116 static struct wipedbs_server_data *get_server_data(struct wipedbs_state *state,
117 const struct server_id *id)
119 struct wipedbs_server_data *ret = NULL;
120 TDB_DATA key, val = tdb_null;
121 NTSTATUS status;
123 key = make_tdb_data((const void*)&id->unique_id, sizeof(id->unique_id));
124 status = dbwrap_fetch(state->id2server_data, talloc_tos(), key, &val);
125 if (NT_STATUS_IS_OK(status)) {
126 ret = *(struct wipedbs_server_data**) val.dptr;
127 TALLOC_FREE(val.dptr);
128 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
129 struct server_id_buf idbuf;
131 server_id_str_buf(*id, &idbuf);
133 ret = talloc_zero(state->id2server_data,
134 struct wipedbs_server_data);
135 if (ret == NULL) {
136 DEBUG(0, ("Failed to allocate server entry for %s\n",
137 idbuf.buf));
138 goto done;
140 ret->server_id = *id;
141 ret->server_id_str = talloc_strdup(ret, idbuf.buf);
142 ret->exists = true;
143 val = make_tdb_data((const void*)&ret, sizeof(ret));
144 status = dbwrap_store(state->id2server_data,
145 key, val, TDB_INSERT);
146 if (!NT_STATUS_IS_OK(status)) {
147 DEBUG(0, ("Failed to store server entry for %s: %s\n",
148 idbuf.buf, nt_errstr(status)));
150 goto done;
151 } else {
152 struct server_id_buf idbuf;
153 DEBUG(0, ("Failed to fetch server entry for %s: %s\n",
154 server_id_str_buf(*id, &idbuf), nt_errstr(status)));
155 goto done;
157 if (!server_id_equal(id, &ret->server_id)) {
158 struct server_id_buf idbuf1, idbuf2;
159 DEBUG(0, ("uniq id collision for %s and %s\n",
160 server_id_str_buf(*id, &idbuf1),
161 server_id_str_buf(ret->server_id, &idbuf2)));
162 smb_panic("server_id->unique_id not unique!");
164 done:
165 return ret;
168 static int wipedbs_traverse_sessions(struct smbXsrv_session_global0 *session,
169 void *wipedbs_state)
171 struct wipedbs_state *state =
172 talloc_get_type_abort(wipedbs_state,
173 struct wipedbs_state);
174 struct wipedbs_server_data *sd;
175 struct wipedbs_record_marker *rec;
176 TDB_DATA tmp;
177 int ret = -1;
179 assert(session->num_channels == 1);
181 state->stat.session.total++;
183 sd = get_server_data(state, &session->channels[0].server_id);
184 if (sd == NULL) {
185 goto done;
188 if (server_id_is_disconnected(&sd->server_id)) {
189 state->stat.session.disconnected++;
192 rec = talloc_zero(sd, struct wipedbs_record_marker);
193 if (rec == NULL) {
194 DEBUG(0, ("Out of memory!\n"));
195 goto done;
198 tmp = dbwrap_record_get_key(session->db_rec);
199 rec->key = tdb_data_talloc_copy(rec, tmp);
200 tmp = dbwrap_record_get_value(session->db_rec);
201 rec->val = tdb_data_talloc_copy(rec, tmp);
203 rec->desc = talloc_asprintf(
204 rec, "session[global: %u wire: %llu]",
205 session->session_global_id,
206 (long long unsigned)session->session_wire_id);
208 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
209 (rec->desc == NULL))
211 DEBUG(0, ("Out of memory!\n"));
212 goto done;
215 state->session_db = dbwrap_record_get_db(session->db_rec);
217 DLIST_ADD(sd->session_records, rec);
218 ret = 0;
219 done:
220 return ret;
223 static int wipedbs_traverse_tcon(struct smbXsrv_tcon_global0 *tcon,
224 void *wipedbs_state)
226 struct wipedbs_state *state =
227 talloc_get_type_abort(wipedbs_state,
228 struct wipedbs_state);
229 struct wipedbs_server_data *sd;
230 struct wipedbs_record_marker *rec;
231 TDB_DATA tmp;
232 int ret = -1;
234 state->stat.tcon.total++;
236 sd = get_server_data(state, &tcon->server_id);
237 if (sd == NULL) {
238 goto done;
241 if (server_id_is_disconnected(&sd->server_id)) {
242 state->stat.tcon.disconnected++;
245 rec = talloc_zero(sd, struct wipedbs_record_marker);
246 if (rec == NULL) {
247 DEBUG(0, ("Out of memory!\n"));
248 goto done;
251 tmp = dbwrap_record_get_key(tcon->db_rec);
252 rec->key = tdb_data_talloc_copy(rec, tmp);
253 tmp = dbwrap_record_get_value(tcon->db_rec);
254 rec->val = tdb_data_talloc_copy(rec, tmp);
256 rec->desc = talloc_asprintf(
257 rec, "tcon[global: %u wire: %u session: %u share: %s]",
258 tcon->tcon_global_id, tcon->tcon_wire_id,
259 tcon->session_global_id, tcon->share_name);
261 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
262 (rec->desc == NULL))
264 DEBUG(0, ("Out of memory!\n"));
265 goto done;
268 state->tcon_db = dbwrap_record_get_db(tcon->db_rec);
270 DLIST_ADD(sd->tcon_records, rec);
271 ret = 0;
273 done:
274 return ret;
277 static int wipedbs_traverse_open(struct smbXsrv_open_global0 *open,
278 void *wipedbs_state)
280 struct wipedbs_state *state =
281 talloc_get_type_abort(wipedbs_state,
282 struct wipedbs_state);
283 struct wipedbs_server_data *sd;
284 struct wipedbs_record_marker *rec;
285 TDB_DATA tmp;
286 int ret = -1;
288 state->stat.open.total++;
290 sd = get_server_data(state, &open->server_id);
291 if (sd == NULL) {
292 goto done;
295 if (server_id_is_disconnected(&sd->server_id)) {
296 struct timeval disconnect_time;
297 int64_t tdiff;
298 bool reached;
300 state->stat.open.disconnected++;
302 nttime_to_timeval(&disconnect_time, open->disconnect_time);
303 tdiff = usec_time_diff(&state->now, &disconnect_time);
304 reached = (tdiff >= 1000*open->durable_timeout_msec);
306 if (state->verbose) {
307 TALLOC_CTX *mem_ctx = talloc_new(talloc_tos());
308 enum ndr_err_code ndr_err;
309 struct vfs_default_durable_cookie cookie;
311 ndr_err = ndr_pull_struct_blob(
312 &open->backend_cookie, mem_ctx, &cookie,
313 (ndr_pull_flags_fn_t)ndr_pull_vfs_default_durable_cookie);
314 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
315 d_printf("ndr_pull_struct_blob failed\n");
316 ret = -1;
317 goto done;
320 d_printf("open[%s/%s id: 0x%" PRIx32 "] disconnected at "
321 "[%s] %us ago with timeout of %us "
322 "-%s reached\n",
323 cookie.servicepath, cookie.base_name,
324 open->open_global_id,
325 nt_time_string(mem_ctx, open->disconnect_time),
326 (unsigned)(tdiff/1000000),
327 open->durable_timeout_msec / 1000,
328 reached ? "" : " not");
329 talloc_free(mem_ctx);
332 if (!reached) {
333 ret = 0;
334 goto done;
336 state->stat.open_timed_out++;
339 rec = talloc_zero(sd, struct wipedbs_record_marker);
340 if (rec == NULL) {
341 DEBUG(0, ("Out of memory!\n"));
342 goto done;
345 tmp = dbwrap_record_get_key(open->db_rec);
346 rec->key = tdb_data_talloc_copy(rec, tmp);
347 tmp = dbwrap_record_get_value(open->db_rec);
348 rec->val = tdb_data_talloc_copy(rec, tmp);
350 rec->desc = talloc_asprintf(
351 rec, "open[global: %u persistent: %llu volatile: %llu]",
352 open->open_global_id,
353 (long long unsigned)open->open_persistent_id,
354 (long long unsigned)open->open_volatile_id);
356 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
357 (rec->desc == NULL))
359 DEBUG(0, ("Out of memory!\n"));
360 goto done;
363 state->open_db = dbwrap_record_get_db(open->db_rec);
365 DLIST_ADD(sd->open_records, rec);
366 ret = 0;
368 done:
369 return ret;
372 static int wipedbs_traverse_nop(struct db_record *rec, void *private_data)
374 return 0;
377 static int wipedbs_traverse_fill_ids(struct db_record *rec, void *wipedbs_state)
379 struct wipedbs_state *state = talloc_get_type_abort(
380 wipedbs_state, struct wipedbs_state);
382 TDB_DATA val = dbwrap_record_get_value(rec);
384 struct wipedbs_server_data *sd = talloc_get_type_abort(
385 *(void**)val.dptr, struct wipedbs_server_data);
387 state->server_ids[state->idx] = sd->server_id;
388 state->idx++;
389 return 0;
392 static int wipedbs_traverse_set_exists(struct db_record *rec,
393 void *wipedbs_state)
395 struct wipedbs_state *state = talloc_get_type_abort(
396 wipedbs_state, struct wipedbs_state);
398 TDB_DATA val = dbwrap_record_get_value(rec);
400 struct wipedbs_server_data *sd = talloc_get_type_abort(
401 *(void**)val.dptr, struct wipedbs_server_data);
403 /* assume a stable traverse order for rbt */
404 SMB_ASSERT(server_id_equal(&state->server_ids[state->idx],
405 &sd->server_id));
406 sd->exists = state->server_exists[state->idx];
408 if (sd->exists) {
409 state->stat.server.existing++;
411 if (server_id_is_disconnected(&sd->server_id)) {
412 state->stat.server.disconnected++;
415 state->idx++;
416 return 0;
419 static bool serverids_exist(const struct server_id *ids, int num_ids,
420 bool *results)
422 int i;
424 for (i=0; i<num_ids; i++) {
425 results[i] = serverid_exists(&ids[i]);
428 return true;
432 static NTSTATUS wipedbs_check_server_exists(struct wipedbs_state *state)
434 NTSTATUS status;
435 bool ok;
436 int num_servers;
438 status = dbwrap_traverse_read(state->id2server_data,
439 wipedbs_traverse_nop, NULL, &num_servers);
440 if (!NT_STATUS_IS_OK(status)) {
441 DEBUG(0, ("Failed to traverse temporary database\n"));
442 goto done;
444 state->stat.server.total = num_servers;
446 state->server_ids = talloc_array(state, struct server_id, num_servers);
447 state->server_exists = talloc_array(state, bool, num_servers);
448 if (state->server_ids == NULL || state->server_exists == NULL) {
449 DEBUG(0, ("Out of memory\n"));
450 goto done;
453 state->idx = 0;
454 status = dbwrap_traverse_read(state->id2server_data,
455 wipedbs_traverse_fill_ids,
456 state, NULL);
457 if (!NT_STATUS_IS_OK(status)) {
458 DEBUG(0, ("Failed to traverse temporary database\n"));
459 goto done;
462 ok = serverids_exist(state->server_ids, num_servers, state->server_exists);
463 if (!ok) {
464 DEBUG(0, ("Calling serverids_exist failed\n"));
465 status = NT_STATUS_UNSUCCESSFUL;
466 goto done;
469 state->idx = 0;
470 status = dbwrap_traverse_read(state->id2server_data,
471 wipedbs_traverse_set_exists, state, NULL);
472 if (!NT_STATUS_IS_OK(status)) {
473 DEBUG(0, ("Failed to traverse temporary database\n"));
474 goto done;
476 done:
477 TALLOC_FREE(state->server_ids);
478 TALLOC_FREE(state->server_exists);
479 return status;
482 static int wipedbs_delete_records(struct db_context *db,
483 struct wipedbs_record_marker *records,
484 bool dry_run, bool verbose, int *count)
486 struct wipedbs_record_marker *cur;
487 struct db_record *rec;
488 TDB_DATA val;
489 NTSTATUS status;
490 unsigned num=0, total=0;
492 if (db == NULL) {
493 return 0;
496 for (cur = records; cur != NULL; cur = cur->next) {
497 total++;
498 rec = dbwrap_fetch_locked(db, talloc_tos(), cur->key);
499 if (rec == NULL) {
500 DEBUG(0, ("Failed to fetch record <%s> from %s",
501 cur->desc, dbwrap_name(db)));
502 continue;
504 val = dbwrap_record_get_value(rec);
505 if (tdb_data_equal(val, cur->val)) {
506 if (dry_run) {
507 status = NT_STATUS_OK;
508 } else {
509 status = dbwrap_record_delete(rec);
511 if (NT_STATUS_IS_OK(status)) {
512 num ++;
513 } else {
514 DEBUG(0, ("Failed to delete record <%s> from %s"
515 ": %s\n", cur->desc, dbwrap_name(db),
516 nt_errstr(status)));
518 } else {
519 DEBUG(0, ("Warning: record <%s> from %s changed"
520 ", skip record!\n",
521 cur->desc, dbwrap_name(db)));
523 if (verbose) {
524 d_printf("deleting %s\n", cur->desc);
526 TALLOC_FREE(rec);
529 if (verbose) {
530 d_printf("Deleted %u of %u records from %s\n",
531 num, total, dbwrap_name(db));
534 if (count) {
535 *count += total;
538 return total - num;
541 static int wipedbs_traverse_server_data(struct db_record *rec,
542 void *wipedbs_state)
544 struct wipedbs_state *state = talloc_get_type_abort(
545 wipedbs_state, struct wipedbs_state);
546 bool dry_run = state->testmode;
547 TDB_DATA val = dbwrap_record_get_value(rec);
548 int ret;
549 struct wipedbs_server_data *sd = talloc_get_type_abort(
550 *(void**)val.dptr, struct wipedbs_server_data);
552 if (state->verbose) {
553 d_printf("Server: '%s' %s\n", sd->server_id_str,
554 sd->exists ?
555 "exists" :
556 "does not exist, cleaning up...");
559 if (sd->exists) {
560 return 0;
563 ret = wipedbs_delete_records(state->session_db, sd->session_records,
564 dry_run, state->verbose,
565 &state->stat.session.todelete);
566 state->stat.session.failure += ret;
568 ret = wipedbs_delete_records(state->tcon_db, sd->tcon_records,
569 dry_run, state->verbose,
570 &state->stat.tcon.todelete);
571 state->stat.tcon.failure += ret;
573 ret = wipedbs_delete_records(state->open_db, sd->open_records,
574 dry_run, state->verbose,
575 &state->stat.open.todelete);
576 state->stat.open.failure += ret;
578 return 0;
581 static int net_serverid_wipedbs(struct net_context *c, int argc,
582 const char **argv)
584 int ret = -1;
585 NTSTATUS status;
586 struct wipedbs_state *state = talloc_zero(talloc_tos(),
587 struct wipedbs_state);
589 if (c->display_usage) {
590 d_printf("%s\n%s",
591 _("Usage:"),
592 _("net serverid wipedbs [--test] [--verbose]\n"));
593 d_printf("%s\n%s",
594 _("Example:"),
595 _("net serverid wipedbs -v\n"));
596 return -1;
599 state->now = timeval_current();
600 state->testmode = c->opt_testmode;
601 state->verbose = c->opt_verbose;
603 state->id2server_data = db_open_rbt(state);
604 if (state->id2server_data == NULL) {
605 DEBUG(0, ("Failed to open temporary database\n"));
606 goto done;
609 status = smbXsrv_session_global_traverse(wipedbs_traverse_sessions,
610 state);
611 if (!NT_STATUS_IS_OK(status)) {
612 goto done;
615 status = smbXsrv_tcon_global_traverse(wipedbs_traverse_tcon, state);
616 if (!NT_STATUS_IS_OK(status)) {
617 goto done;
620 status = smbXsrv_open_global_traverse(wipedbs_traverse_open, state);
621 if (!NT_STATUS_IS_OK(status)) {
622 goto done;
625 status = wipedbs_check_server_exists(state);
626 if (!NT_STATUS_IS_OK(status)) {
627 goto done;
630 status = dbwrap_traverse_read(state->id2server_data,
631 wipedbs_traverse_server_data,
632 state, NULL);
633 if (!NT_STATUS_IS_OK(status)) {
634 DEBUG(0, ("Failed to traverse db: %s\n", nt_errstr(status)));
635 goto done;
638 d_printf("Found %d serverids, %d alive and %d disconnected\n",
639 state->stat.server.total,
640 state->stat.server.existing,
641 state->stat.server.disconnected);
642 d_printf("Found %d sessions, %d alive and %d disconnected"
643 ", cleaned up %d of %d entries\n",
644 state->stat.session.total,
645 state->stat.session.total - state->stat.session.todelete,
646 state->stat.session.disconnected,
647 state->stat.session.todelete - state->stat.session.failure,
648 state->stat.session.todelete);
649 d_printf("Found %d tcons, %d alive and %d disconnected"
650 ", cleaned up %d of %d entries\n",
651 state->stat.tcon.total,
652 state->stat.tcon.total - state->stat.tcon.todelete,
653 state->stat.tcon.disconnected,
654 state->stat.tcon.todelete - state->stat.tcon.failure,
655 state->stat.tcon.todelete);
656 d_printf("Found %d opens, %d alive, %d disconnected and %d timed out"
657 ", cleaned up %d of %d entries\n",
658 state->stat.open.total,
659 state->stat.open.total - state->stat.open.todelete
660 - (state->stat.open.disconnected - state->stat.open_timed_out),
661 state->stat.open.disconnected,
662 state->stat.open_timed_out,
663 state->stat.open.todelete - state->stat.open.failure,
664 state->stat.open.todelete);
666 ret = 0;
667 done:
668 talloc_free(state);
669 return ret;
672 static int net_serverid_exists(struct net_context *c, int argc,
673 const char **argv)
675 struct server_id pid;
676 bool ok;
678 if ((argc != 1) || (c->display_usage)) {
679 d_printf("Usage:\n"
680 "net serverid exists <serverid>\n");
681 return -1;
684 pid = server_id_from_string(get_my_vnn(), argv[0]);
685 ok = serverid_exists(&pid);
687 if (ok) {
688 d_printf("%s exists\n", argv[0]);
689 } else {
690 d_printf("%s does not exist\n", argv[0]);
693 return 0;
696 int net_serverid(struct net_context *c, int argc, const char **argv)
698 struct functable func[] = {
700 "list",
701 net_serverid_list,
702 NET_TRANSPORT_LOCAL,
703 N_("List all entries from serverid.tdb"),
704 N_("net serverid list\n"
705 " List all entries from serverid.tdb")
708 "wipe",
709 net_serverid_wipe,
710 NET_TRANSPORT_LOCAL,
711 N_("Wipe the serverid.tdb for the current node"),
712 N_("net serverid wipe\n"
713 " Wipe the serverid.tdb for the current node")
716 "wipedbs",
717 net_serverid_wipedbs,
718 NET_TRANSPORT_LOCAL,
719 N_("Clean dead entries from temporary databases"),
720 N_("net serverid wipedbs\n"
721 " Clean dead entries from temporary databases")
724 "exists",
725 net_serverid_exists,
726 NET_TRANSPORT_LOCAL,
727 N_("Show existence of a serverid"),
728 N_("net serverid exists <id>")
730 {NULL, NULL, 0, NULL, NULL}
733 return net_run_function(c, argc, argv, "net serverid", func);