s3:rpc_client: implement bind time feature negotiation
[metze/samba-autobuild/.git] / source3 / utils / net_serverid.c
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 "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"
30
31 struct wipedbs_record_marker {
32         struct wipedbs_record_marker *prev, *next;
33         TDB_DATA key, val;
34         const char *desc;
35 };
36
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;
44 };
45
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;
71 };
72
73 static struct wipedbs_server_data *get_server_data(struct wipedbs_state *state,
74                                                    const struct server_id *id)
75 {
76         struct wipedbs_server_data *ret = NULL;
77         TDB_DATA key, val = tdb_null;
78         NTSTATUS status;
79
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;
87
88                 server_id_str_buf(*id, &idbuf);
89
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;
96                 }
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)));
106                 }
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;
113         }
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!");
120         }
121 done:
122         return ret;
123 }
124
125 static int wipedbs_traverse_sessions(struct smbXsrv_session_global0 *session,
126                                      void *wipedbs_state)
127 {
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;
135
136         assert(session->num_channels == 1);
137
138         state->stat.session.total++;
139
140         sd = get_server_data(state, &session->channels[0].server_id);
141         if (sd == NULL) {
142                 goto done;
143         }
144
145         if (server_id_is_disconnected(&sd->server_id)) {
146                 state->stat.session.disconnected++;
147         }
148
149         rec = talloc_zero(sd, struct wipedbs_record_marker);
150         if (rec == NULL) {
151                 DEBUG(0, ("Out of memory!\n"));
152                 goto done;
153         }
154
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);
159
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);
164
165         if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
166             (rec->desc == NULL))
167         {
168                 DEBUG(0, ("Out of memory!\n"));
169                 goto done;
170         }
171
172         state->session_db = dbwrap_record_get_db(session->db_rec);
173
174         DLIST_ADD(sd->session_records, rec);
175         ret = 0;
176 done:
177         return ret;
178 }
179
180 static int wipedbs_traverse_tcon(struct smbXsrv_tcon_global0 *tcon,
181                                  void *wipedbs_state)
182 {
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;
190
191         state->stat.tcon.total++;
192
193         sd = get_server_data(state, &tcon->server_id);
194         if (sd == NULL) {
195                 goto done;
196         }
197
198         if (server_id_is_disconnected(&sd->server_id)) {
199                 state->stat.tcon.disconnected++;
200         }
201
202         rec = talloc_zero(sd, struct wipedbs_record_marker);
203         if (rec == NULL) {
204                 DEBUG(0, ("Out of memory!\n"));
205                 goto done;
206         }
207
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);
212
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);
217
218         if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
219             (rec->desc == NULL))
220         {
221                 DEBUG(0, ("Out of memory!\n"));
222                 goto done;
223         }
224
225         state->tcon_db = dbwrap_record_get_db(tcon->db_rec);
226
227         DLIST_ADD(sd->tcon_records, rec);
228         ret = 0;
229
230 done:
231         return ret;
232 }
233
234 static int wipedbs_traverse_open(struct smbXsrv_open_global0 *open,
235                                  void *wipedbs_state)
236 {
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;
244
245         state->stat.open.total++;
246
247         sd = get_server_data(state, &open->server_id);
248         if (sd == NULL) {
249                 goto done;
250         }
251
252         if (server_id_is_disconnected(&sd->server_id)) {
253                 struct timeval disconnect_time;
254                 int64_t tdiff;
255                 bool reached;
256
257                 state->stat.open.disconnected++;
258
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);
262
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;
267
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;
275                         }
276
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);
287                 }
288
289                 if (!reached) {
290                         ret = 0;
291                         goto done;
292                 }
293                 state->stat.open_timed_out++;
294         }
295
296         rec = talloc_zero(sd, struct wipedbs_record_marker);
297         if (rec == NULL) {
298                 DEBUG(0, ("Out of memory!\n"));
299                 goto done;
300         }
301
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);
306
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);
312
313         if ((rec->key.dptr == NULL) || (rec->val.dptr == NULL) ||
314             (rec->desc == NULL))
315         {
316                 DEBUG(0, ("Out of memory!\n"));
317                 goto done;
318         }
319
320         state->open_db = dbwrap_record_get_db(open->db_rec);
321
322         DLIST_ADD(sd->open_records, rec);
323         ret = 0;
324
325 done:
326         return ret;
327 }
328
329 static int wipedbs_traverse_nop(struct db_record *rec, void *private_data)
330 {
331         return 0;
332 }
333
334 static int wipedbs_traverse_fill_ids(struct db_record *rec, void *wipedbs_state)
335 {
336         struct wipedbs_state *state = talloc_get_type_abort(
337                 wipedbs_state, struct wipedbs_state);
338
339         TDB_DATA val = dbwrap_record_get_value(rec);
340
341         struct wipedbs_server_data *sd = talloc_get_type_abort(
342                 *(void**)val.dptr, struct wipedbs_server_data);
343
344         state->server_ids[state->idx] = sd->server_id;
345         state->idx++;
346         return 0;
347 }
348
349 static int wipedbs_traverse_set_exists(struct db_record *rec,
350                                        void *wipedbs_state)
351 {
352         struct wipedbs_state *state = talloc_get_type_abort(
353                 wipedbs_state, struct wipedbs_state);
354
355         TDB_DATA val = dbwrap_record_get_value(rec);
356
357         struct wipedbs_server_data *sd = talloc_get_type_abort(
358                 *(void**)val.dptr, struct wipedbs_server_data);
359
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];
364
365         if (sd->exists) {
366                 state->stat.server.existing++;
367         }
368         if (server_id_is_disconnected(&sd->server_id)) {
369                 state->stat.server.disconnected++;
370         }
371
372         state->idx++;
373         return 0;
374 }
375
376 static bool serverids_exist(const struct server_id *ids, int num_ids,
377                             bool *results)
378 {
379         int i;
380
381         for (i=0; i<num_ids; i++) {
382                 results[i] = serverid_exists(&ids[i]);
383         }
384
385         return true;
386 }
387
388
389 static NTSTATUS wipedbs_check_server_exists(struct wipedbs_state *state)
390 {
391         NTSTATUS status;
392         bool ok;
393         int num_servers;
394
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;
400         }
401         state->stat.server.total = num_servers;
402
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;
408         }
409
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;
417         }
418
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;
424         }
425
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;
432         }
433 done:
434         TALLOC_FREE(state->server_ids);
435         TALLOC_FREE(state->server_exists);
436         return status;
437 }
438
439 static int wipedbs_delete_records(struct db_context *db,
440                                   struct wipedbs_record_marker *records,
441                                   bool dry_run, bool verbose, int *count)
442 {
443         struct wipedbs_record_marker *cur;
444         struct db_record *rec;
445         TDB_DATA val;
446         NTSTATUS status;
447         unsigned num=0, total=0;
448
449         if (db == NULL) {
450                 return 0;
451         }
452
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;
460                 }
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);
467                         }
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)));
474                         }
475                 } else {
476                         DEBUG(0, ("Warning: record <%s> from %s changed"
477                                   ", skip record!\n",
478                                   cur->desc, dbwrap_name(db)));
479                 }
480                 if (verbose) {
481                         d_printf("deleting %s\n", cur->desc);
482                 }
483                 TALLOC_FREE(rec);
484         }
485
486         if (verbose) {
487                 d_printf("Deleted %u of %u records from %s\n",
488                          num, total, dbwrap_name(db));
489         }
490
491         if (count) {
492                 *count += total;
493         }
494
495         return total - num;
496 }
497
498 static int wipedbs_traverse_server_data(struct db_record *rec,
499                                         void *wipedbs_state)
500 {
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);
508
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...");
514         }
515
516         if (sd->exists) {
517                 return 0;
518         }
519
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;
524
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;
529
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;
534
535         return 0;
536 }
537
538 static int net_serverid_wipedbs(struct net_context *c, int argc,
539                                 const char **argv)
540 {
541         int ret = -1;
542         NTSTATUS status;
543         struct wipedbs_state *state = talloc_zero(talloc_tos(),
544                                                   struct wipedbs_state);
545
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;
554         }
555
556         state->now = timeval_current();
557         state->testmode = c->opt_testmode;
558         state->verbose = c->opt_verbose;
559
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;
564         }
565
566         status = smbXsrv_session_global_traverse(wipedbs_traverse_sessions,
567                                                  state);
568         if (!NT_STATUS_IS_OK(status)) {
569                 goto done;
570         }
571
572         status = smbXsrv_tcon_global_traverse(wipedbs_traverse_tcon, state);
573         if (!NT_STATUS_IS_OK(status)) {
574                 goto done;
575         }
576
577         status = smbXsrv_open_global_traverse(wipedbs_traverse_open, state);
578         if (!NT_STATUS_IS_OK(status)) {
579                 goto done;
580         }
581
582         status = wipedbs_check_server_exists(state);
583         if (!NT_STATUS_IS_OK(status)) {
584                 goto done;
585         }
586
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;
593         }
594
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);
622
623         ret = 0;
624 done:
625         talloc_free(state);
626         return ret;
627 }
628
629 static int net_serverid_exists(struct net_context *c, int argc,
630                                const char **argv)
631 {
632         struct server_id pid;
633         bool ok;
634
635         if ((argc != 1) || (c->display_usage)) {
636                 d_printf("Usage:\n"
637                          "net serverid exists <serverid>\n");
638                 return -1;
639         }
640
641         pid = server_id_from_string(get_my_vnn(), argv[0]);
642         ok = serverid_exists(&pid);
643
644         if (ok) {
645                 d_printf("%s exists\n", argv[0]);
646         } else {
647                 d_printf("%s does not exist\n", argv[0]);
648         }
649
650         return 0;
651 }
652
653 int net_serverid(struct net_context *c, int argc, const char **argv)
654 {
655         struct functable func[] = {
656                 {
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")
663                 },
664                 {
665                         "exists",
666                         net_serverid_exists,
667                         NET_TRANSPORT_LOCAL,
668                         N_("Show existence of a serverid"),
669                         N_("net serverid exists <id>")
670                 },
671                 {NULL, NULL, 0, NULL, NULL}
672         };
673
674         return net_run_function(c, argc, argv, "net serverid", func);
675 }