source: vendor/current/source3/utils/net_serverid.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 18.3 KB
Line 
1/*
2 Samba Unix/Linux SMB client library
3 net serverid commands
4 Copyright (C) Volker Lendecke 2010
5
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.
10
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.
15
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/>.
18*/
19
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"
29#include "librpc/gen_ndr/ndr_open_files.h"
30
31static int net_serverid_list_fn(const struct server_id *id,
32 uint32_t msg_flags, void *priv)
33{
34 struct server_id_buf idbuf;
35 d_printf("%s %llu 0x%x\n", server_id_str_buf(*id, &idbuf),
36 (unsigned long long)id->unique_id,
37 (unsigned int)msg_flags);
38 return 0;
39}
40
41static int net_serverid_list(struct net_context *c, int argc,
42 const char **argv)
43{
44 d_printf("pid unique_id msg_flags\n");
45 return serverid_traverse_read(net_serverid_list_fn, NULL) ? 0 : -1;
46}
47
48static int net_serverid_wipe_fn(struct db_record *rec,
49 const struct server_id *id,
50 uint32_t msg_flags, void *private_data)
51{
52 NTSTATUS status;
53
54 if (!procid_is_local(id)) {
55 return 0;
56 }
57 status = dbwrap_record_delete(rec);
58 if (!NT_STATUS_IS_OK(status)) {
59 struct server_id_buf idbuf;
60 DEBUG(1, ("Could not delete serverid.tdb record %s: %s\n",
61 server_id_str_buf(*id, &idbuf), nt_errstr(status)));
62 }
63 return 0;
64}
65
66static int net_serverid_wipe(struct net_context *c, int argc,
67 const char **argv)
68{
69 return serverid_traverse(net_serverid_wipe_fn, NULL) ? 0 : -1;
70}
71
72
73struct wipedbs_record_marker {
74 struct wipedbs_record_marker *prev, *next;
75 TDB_DATA key, val;
76 const char *desc;
77};
78
79struct 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;
86};
87
88struct 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;
113};
114
115static struct wipedbs_server_data *get_server_data(struct wipedbs_state *state,
116 const struct server_id *id)
117{
118 struct wipedbs_server_data *ret = NULL;
119 TDB_DATA key, val = tdb_null;
120 NTSTATUS status;
121
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 struct server_id_buf idbuf;
129
130 server_id_str_buf(*id, &idbuf);
131
132 ret = talloc_zero(state->id2server_data,
133 struct wipedbs_server_data);
134 if (ret == NULL) {
135 DEBUG(0, ("Failed to allocate server entry for %s\n",
136 idbuf.buf));
137 goto done;
138 }
139 ret->server_id = *id;
140 ret->server_id_str = talloc_strdup(ret, idbuf.buf);
141 ret->exists = true;
142 val = make_tdb_data((const void*)&ret, sizeof(ret));
143 status = dbwrap_store(state->id2server_data,
144 key, val, TDB_INSERT);
145 if (!NT_STATUS_IS_OK(status)) {
146 DEBUG(0, ("Failed to store server entry for %s: %s\n",
147 idbuf.buf, nt_errstr(status)));
148 }
149 goto done;
150 } else {
151 struct server_id_buf idbuf;
152 DEBUG(0, ("Failed to fetch server entry for %s: %s\n",
153 server_id_str_buf(*id, &idbuf), nt_errstr(status)));
154 goto done;
155 }
156 if (!server_id_equal(id, &ret->server_id)) {
157 struct server_id_buf idbuf1, idbuf2;
158 DEBUG(0, ("uniq id collision for %s and %s\n",
159 server_id_str_buf(*id, &idbuf1),
160 server_id_str_buf(ret->server_id, &idbuf2)));
161 smb_panic("server_id->unique_id not unique!");
162 }
163done:
164 return ret;
165}
166
167static int wipedbs_traverse_sessions(struct smbXsrv_session_global0 *session,
168 void *wipedbs_state)
169{
170 struct wipedbs_state *state =
171 talloc_get_type_abort(wipedbs_state,
172 struct wipedbs_state);
173 struct wipedbs_server_data *sd;
174 struct wipedbs_record_marker *rec;
175 TDB_DATA tmp;
176 int ret = -1;
177
178 assert(session->num_channels == 1);
179
180 state->stat.session.total++;
181
182 sd = get_server_data(state, &session->channels[0].server_id);
183 if (sd == NULL) {
184 goto done;
185 }
186
187 if (server_id_is_disconnected(&sd->server_id)) {
188 state->stat.session.disconnected++;
189 }
190
191 rec = talloc_zero(sd, struct wipedbs_record_marker);
192 if (rec == NULL) {
193 DEBUG(0, ("Out of memory!\n"));
194 goto done;
195 }
196
197 tmp = dbwrap_record_get_key(session->db_rec);
198 rec->key = tdb_data_talloc_copy(rec, tmp);
199 tmp = dbwrap_record_get_value(session->db_rec);
200 rec->val = tdb_data_talloc_copy(rec, tmp);
201
202 rec->desc = talloc_asprintf(
203 rec, "session[global: %u wire: %llu]",
204 session->session_global_id,
205 (long long unsigned)session->session_wire_id);
206
207 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
208 (rec->desc == NULL))
209 {
210 DEBUG(0, ("Out of memory!\n"));
211 goto done;
212 }
213
214 state->session_db = dbwrap_record_get_db(session->db_rec);
215
216 DLIST_ADD(sd->session_records, rec);
217 ret = 0;
218done:
219 return ret;
220}
221
222static int wipedbs_traverse_tcon(struct smbXsrv_tcon_global0 *tcon,
223 void *wipedbs_state)
224{
225 struct wipedbs_state *state =
226 talloc_get_type_abort(wipedbs_state,
227 struct wipedbs_state);
228 struct wipedbs_server_data *sd;
229 struct wipedbs_record_marker *rec;
230 TDB_DATA tmp;
231 int ret = -1;
232
233 state->stat.tcon.total++;
234
235 sd = get_server_data(state, &tcon->server_id);
236 if (sd == NULL) {
237 goto done;
238 }
239
240 if (server_id_is_disconnected(&sd->server_id)) {
241 state->stat.tcon.disconnected++;
242 }
243
244 rec = talloc_zero(sd, struct wipedbs_record_marker);
245 if (rec == NULL) {
246 DEBUG(0, ("Out of memory!\n"));
247 goto done;
248 }
249
250 tmp = dbwrap_record_get_key(tcon->db_rec);
251 rec->key = tdb_data_talloc_copy(rec, tmp);
252 tmp = dbwrap_record_get_value(tcon->db_rec);
253 rec->val = tdb_data_talloc_copy(rec, tmp);
254
255 rec->desc = talloc_asprintf(
256 rec, "tcon[global: %u wire: %u session: %u share: %s]",
257 tcon->tcon_global_id, tcon->tcon_wire_id,
258 tcon->session_global_id, tcon->share_name);
259
260 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
261 (rec->desc == NULL))
262 {
263 DEBUG(0, ("Out of memory!\n"));
264 goto done;
265 }
266
267 state->tcon_db = dbwrap_record_get_db(tcon->db_rec);
268
269 DLIST_ADD(sd->tcon_records, rec);
270 ret = 0;
271
272done:
273 return ret;
274}
275
276static int wipedbs_traverse_open(struct smbXsrv_open_global0 *open,
277 void *wipedbs_state)
278{
279 struct wipedbs_state *state =
280 talloc_get_type_abort(wipedbs_state,
281 struct wipedbs_state);
282 struct wipedbs_server_data *sd;
283 struct wipedbs_record_marker *rec;
284 TDB_DATA tmp;
285 int ret = -1;
286
287 state->stat.open.total++;
288
289 sd = get_server_data(state, &open->server_id);
290 if (sd == NULL) {
291 goto done;
292 }
293
294 if (server_id_is_disconnected(&sd->server_id)) {
295 struct timeval disconnect_time;
296 int64_t tdiff;
297 bool reached;
298
299 state->stat.open.disconnected++;
300
301 nttime_to_timeval(&disconnect_time, open->disconnect_time);
302 tdiff = usec_time_diff(&state->now, &disconnect_time);
303 reached = (tdiff >= 1000*open->durable_timeout_msec);
304
305 if (state->verbose) {
306 TALLOC_CTX *mem_ctx = talloc_new(talloc_tos());
307 enum ndr_err_code ndr_err;
308 struct vfs_default_durable_cookie cookie;
309
310 ndr_err = ndr_pull_struct_blob(
311 &open->backend_cookie, mem_ctx, &cookie,
312 (ndr_pull_flags_fn_t)ndr_pull_vfs_default_durable_cookie);
313 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
314 d_printf("ndr_pull_struct_blob failed\n");
315 ret = -1;
316 goto done;
317 }
318
319 d_printf("open[%s/%s id: 0x%" PRIx32 "] disconnected at "
320 "[%s] %us ago with timeout of %us "
321 "-%s reached\n",
322 cookie.servicepath, cookie.base_name,
323 open->open_global_id,
324 nt_time_string(mem_ctx, open->disconnect_time),
325 (unsigned)(tdiff/1000000),
326 open->durable_timeout_msec / 1000,
327 reached ? "" : " not");
328 talloc_free(mem_ctx);
329 }
330
331 if (!reached) {
332 ret = 0;
333 goto done;
334 }
335 state->stat.open_timed_out++;
336 }
337
338 rec = talloc_zero(sd, struct wipedbs_record_marker);
339 if (rec == NULL) {
340 DEBUG(0, ("Out of memory!\n"));
341 goto done;
342 }
343
344 tmp = dbwrap_record_get_key(open->db_rec);
345 rec->key = tdb_data_talloc_copy(rec, tmp);
346 tmp = dbwrap_record_get_value(open->db_rec);
347 rec->val = tdb_data_talloc_copy(rec, tmp);
348
349 rec->desc = talloc_asprintf(
350 rec, "open[global: %u persistent: %llu volatile: %llu]",
351 open->open_global_id,
352 (long long unsigned)open->open_persistent_id,
353 (long long unsigned)open->open_volatile_id);
354
355 if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
356 (rec->desc == NULL))
357 {
358 DEBUG(0, ("Out of memory!\n"));
359 goto done;
360 }
361
362 state->open_db = dbwrap_record_get_db(open->db_rec);
363
364 DLIST_ADD(sd->open_records, rec);
365 ret = 0;
366
367done:
368 return ret;
369}
370
371static int wipedbs_traverse_nop(struct db_record *rec, void *private_data)
372{
373 return 0;
374}
375
376static int wipedbs_traverse_fill_ids(struct db_record *rec, void *wipedbs_state)
377{
378 struct wipedbs_state *state = talloc_get_type_abort(
379 wipedbs_state, struct wipedbs_state);
380
381 TDB_DATA val = dbwrap_record_get_value(rec);
382
383 struct wipedbs_server_data *sd = talloc_get_type_abort(
384 *(void**)val.dptr, struct wipedbs_server_data);
385
386 state->server_ids[state->idx] = sd->server_id;
387 state->idx++;
388 return 0;
389}
390
391static int wipedbs_traverse_set_exists(struct db_record *rec,
392 void *wipedbs_state)
393{
394 struct wipedbs_state *state = talloc_get_type_abort(
395 wipedbs_state, struct wipedbs_state);
396
397 TDB_DATA val = dbwrap_record_get_value(rec);
398
399 struct wipedbs_server_data *sd = talloc_get_type_abort(
400 *(void**)val.dptr, struct wipedbs_server_data);
401
402 /* assume a stable traverse order for rbt */
403 SMB_ASSERT(server_id_equal(&state->server_ids[state->idx],
404 &sd->server_id));
405 sd->exists = state->server_exists[state->idx];
406
407 if (sd->exists) {
408 state->stat.server.existing++;
409 }
410 if (server_id_is_disconnected(&sd->server_id)) {
411 state->stat.server.disconnected++;
412 }
413
414 state->idx++;
415 return 0;
416}
417
418static bool serverids_exist(const struct server_id *ids, int num_ids,
419 bool *results)
420{
421 int i;
422
423 for (i=0; i<num_ids; i++) {
424 results[i] = serverid_exists(&ids[i]);
425 }
426
427 return true;
428}
429
430
431static NTSTATUS wipedbs_check_server_exists(struct wipedbs_state *state)
432{
433 NTSTATUS status;
434 bool ok;
435 int num_servers;
436
437 status = dbwrap_traverse_read(state->id2server_data,
438 wipedbs_traverse_nop, NULL, &num_servers);
439 if (!NT_STATUS_IS_OK(status)) {
440 DEBUG(0, ("Failed to traverse temporary database\n"));
441 goto done;
442 }
443 state->stat.server.total = num_servers;
444
445 state->server_ids = talloc_array(state, struct server_id, num_servers);
446 state->server_exists = talloc_array(state, bool, num_servers);
447 if (state->server_ids == NULL || state->server_exists == NULL) {
448 DEBUG(0, ("Out of memory\n"));
449 goto done;
450 }
451
452 state->idx = 0;
453 status = dbwrap_traverse_read(state->id2server_data,
454 wipedbs_traverse_fill_ids,
455 state, NULL);
456 if (!NT_STATUS_IS_OK(status)) {
457 DEBUG(0, ("Failed to traverse temporary database\n"));
458 goto done;
459 }
460
461 ok = serverids_exist(state->server_ids, num_servers, state->server_exists);
462 if (!ok) {
463 DEBUG(0, ("Calling serverids_exist failed\n"));
464 status = NT_STATUS_UNSUCCESSFUL;
465 goto done;
466 }
467
468 state->idx = 0;
469 status = dbwrap_traverse_read(state->id2server_data,
470 wipedbs_traverse_set_exists, state, NULL);
471 if (!NT_STATUS_IS_OK(status)) {
472 DEBUG(0, ("Failed to traverse temporary database\n"));
473 goto done;
474 }
475done:
476 TALLOC_FREE(state->server_ids);
477 TALLOC_FREE(state->server_exists);
478 return status;
479}
480
481static int wipedbs_delete_records(struct db_context *db,
482 struct wipedbs_record_marker *records,
483 bool dry_run, bool verbose, int *count)
484{
485 struct wipedbs_record_marker *cur;
486 struct db_record *rec;
487 TDB_DATA val;
488 NTSTATUS status;
489 unsigned num=0, total=0;
490
491 if (db == NULL) {
492 return 0;
493 }
494
495 for (cur = records; cur != NULL; cur = cur->next) {
496 total++;
497 rec = dbwrap_fetch_locked(db, talloc_tos(), cur->key);
498 if (rec == NULL) {
499 DEBUG(0, ("Failed to fetch record <%s> from %s",
500 cur->desc, dbwrap_name(db)));
501 continue;
502 }
503 val = dbwrap_record_get_value(rec);
504 if (tdb_data_equal(val, cur->val)) {
505 if (dry_run) {
506 status = NT_STATUS_OK;
507 } else {
508 status = dbwrap_record_delete(rec);
509 }
510 if (NT_STATUS_IS_OK(status)) {
511 num ++;
512 } else {
513 DEBUG(0, ("Failed to delete record <%s> from %s"
514 ": %s\n", cur->desc, dbwrap_name(db),
515 nt_errstr(status)));
516 }
517 } else {
518 DEBUG(0, ("Warning: record <%s> from %s changed"
519 ", skip record!\n",
520 cur->desc, dbwrap_name(db)));
521 }
522 if (verbose) {
523 d_printf("deleting %s\n", cur->desc);
524 }
525 TALLOC_FREE(rec);
526 }
527
528 if (verbose) {
529 d_printf("Deleted %u of %u records from %s\n",
530 num, total, dbwrap_name(db));
531 }
532
533 if (count) {
534 *count += total;
535 }
536
537 return total - num;
538}
539
540static int wipedbs_traverse_server_data(struct db_record *rec,
541 void *wipedbs_state)
542{
543 struct wipedbs_state *state = talloc_get_type_abort(
544 wipedbs_state, struct wipedbs_state);
545 bool dry_run = state->testmode;
546 TDB_DATA val = dbwrap_record_get_value(rec);
547 int ret;
548 struct wipedbs_server_data *sd = talloc_get_type_abort(
549 *(void**)val.dptr, struct wipedbs_server_data);
550
551 if (state->verbose) {
552 d_printf("Server: '%s' %s\n", sd->server_id_str,
553 sd->exists ?
554 "exists" :
555 "does not exist, cleaning up...");
556 }
557
558 if (sd->exists) {
559 return 0;
560 }
561
562 ret = wipedbs_delete_records(state->session_db, sd->session_records,
563 dry_run, state->verbose,
564 &state->stat.session.todelete);
565 state->stat.session.failure += ret;
566
567 ret = wipedbs_delete_records(state->tcon_db, sd->tcon_records,
568 dry_run, state->verbose,
569 &state->stat.tcon.todelete);
570 state->stat.tcon.failure += ret;
571
572 ret = wipedbs_delete_records(state->open_db, sd->open_records,
573 dry_run, state->verbose,
574 &state->stat.open.todelete);
575 state->stat.open.failure += ret;
576
577 return 0;
578}
579
580static int net_serverid_wipedbs(struct net_context *c, int argc,
581 const char **argv)
582{
583 int ret = -1;
584 NTSTATUS status;
585 struct wipedbs_state *state = talloc_zero(talloc_tos(),
586 struct wipedbs_state);
587
588 if (c->display_usage) {
589 d_printf("%s\n%s",
590 _("Usage:"),
591 _("net serverid wipedbs [--test] [--verbose]\n"));
592 d_printf("%s\n%s",
593 _("Example:"),
594 _("net serverid wipedbs -v\n"));
595 return -1;
596 }
597
598 state->now = timeval_current();
599 state->testmode = c->opt_testmode;
600 state->verbose = c->opt_verbose;
601
602 state->id2server_data = db_open_rbt(state);
603 if (state->id2server_data == NULL) {
604 DEBUG(0, ("Failed to open temporary database\n"));
605 goto done;
606 }
607
608 status = smbXsrv_session_global_traverse(wipedbs_traverse_sessions,
609 state);
610 if (!NT_STATUS_IS_OK(status)) {
611 goto done;
612 }
613
614 status = smbXsrv_tcon_global_traverse(wipedbs_traverse_tcon, state);
615 if (!NT_STATUS_IS_OK(status)) {
616 goto done;
617 }
618
619 status = smbXsrv_open_global_traverse(wipedbs_traverse_open, state);
620 if (!NT_STATUS_IS_OK(status)) {
621 goto done;
622 }
623
624 status = wipedbs_check_server_exists(state);
625 if (!NT_STATUS_IS_OK(status)) {
626 goto done;
627 }
628
629 status = dbwrap_traverse_read(state->id2server_data,
630 wipedbs_traverse_server_data,
631 state, NULL);
632 if (!NT_STATUS_IS_OK(status)) {
633 DEBUG(0, ("Failed to traverse db: %s\n", nt_errstr(status)));
634 goto done;
635 }
636
637 d_printf("Found %d serverids, %d alive and %d disconnected\n",
638 state->stat.server.total,
639 state->stat.server.existing,
640 state->stat.server.disconnected);
641 d_printf("Found %d sessions, %d alive and %d disconnected"
642 ", cleaned up %d of %d entries\n",
643 state->stat.session.total,
644 state->stat.session.total - state->stat.session.todelete,
645 state->stat.session.disconnected,
646 state->stat.session.todelete - state->stat.session.failure,
647 state->stat.session.todelete);
648 d_printf("Found %d tcons, %d alive and %d disconnected"
649 ", cleaned up %d of %d entries\n",
650 state->stat.tcon.total,
651 state->stat.tcon.total - state->stat.tcon.todelete,
652 state->stat.tcon.disconnected,
653 state->stat.tcon.todelete - state->stat.tcon.failure,
654 state->stat.tcon.todelete);
655 d_printf("Found %d opens, %d alive, %d disconnected and %d timed out"
656 ", cleaned up %d of %d entries\n",
657 state->stat.open.total,
658 state->stat.open.total - state->stat.open.todelete
659 - (state->stat.open.disconnected - state->stat.open_timed_out),
660 state->stat.open.disconnected,
661 state->stat.open_timed_out,
662 state->stat.open.todelete - state->stat.open.failure,
663 state->stat.open.todelete);
664
665 ret = 0;
666done:
667 talloc_free(state);
668 return ret;
669}
670
671static int net_serverid_exists(struct net_context *c, int argc,
672 const char **argv)
673{
674 struct server_id pid;
675 bool ok;
676
677 if ((argc != 1) || (c->display_usage)) {
678 d_printf("Usage:\n"
679 "net serverid exists <serverid>\n");
680 return -1;
681 }
682
683 pid = server_id_from_string(get_my_vnn(), argv[0]);
684 ok = serverid_exists(&pid);
685
686 if (ok) {
687 d_printf("%s exists\n", argv[0]);
688 } else {
689 d_printf("%s does not exist\n", argv[0]);
690 }
691
692 return 0;
693}
694
695int net_serverid(struct net_context *c, int argc, const char **argv)
696{
697 struct functable func[] = {
698 {
699 "list",
700 net_serverid_list,
701 NET_TRANSPORT_LOCAL,
702 N_("List all entries from serverid.tdb"),
703 N_("net serverid list\n"
704 " List all entries from serverid.tdb")
705 },
706 {
707 "wipe",
708 net_serverid_wipe,
709 NET_TRANSPORT_LOCAL,
710 N_("Wipe the serverid.tdb for the current node"),
711 N_("net serverid wipe\n"
712 " Wipe the serverid.tdb for the current node")
713 },
714 {
715 "wipedbs",
716 net_serverid_wipedbs,
717 NET_TRANSPORT_LOCAL,
718 N_("Clean dead entries from temporary databases"),
719 N_("net serverid wipedbs\n"
720 " Clean dead entries from temporary databases")
721 },
722 {
723 "exists",
724 net_serverid_exists,
725 NET_TRANSPORT_LOCAL,
726 N_("Show existence of a serverid"),
727 N_("net serverid exists <id>")
728 },
729 {NULL, NULL, 0, NULL, NULL}
730 };
731
732 return net_run_function(c, argc, argv, "net serverid", func);
733}
Note: See TracBrowser for help on using the repository browser.