s3:utils: Call gfree_all() before exit in smbget
[samba.git] / source3 / utils / net_tdb.c
1 /*
2  * Samba Unix/Linux client library
3  * net tdb commands to query tdb record information
4  * Copyright (C) 2016, 2017 Christof Schmitt <cs@samba.org>
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 "locking/share_mode_lock.h"
23 #include "locking/proto.h"
24 #include "librpc/gen_ndr/open_files.h"
25 #include "librpc/gen_ndr/ndr_open_files.h"
26
27 static int net_tdb_locking(struct net_context *c, int argc, const char **argv)
28 {
29         TALLOC_CTX *mem_ctx = talloc_stackframe();
30         struct share_mode_lock *lock;
31         DATA_BLOB blob = { .data = NULL };
32         struct file_id id = { .inode = 0 };
33         int ret = -1;
34         bool ok;
35
36         if (argc < 1) {
37                 d_printf("Usage: net tdb locking <key> [ dump ]\n");
38                 goto out;
39         }
40
41         ok = locking_init_readonly();
42         if (!ok) {
43                 d_printf("locking_init_readonly failed\n");
44                 goto out;
45         }
46
47         blob = strhex_to_data_blob(mem_ctx, argv[0]);
48         if (blob.length != sizeof(struct file_id)) {
49                 d_printf("Invalid length %zu of key, expected %zu\n",
50                          blob.length,
51                          sizeof(struct file_id));
52                 goto out;
53         }
54
55         memcpy(&id, blob.data, blob.length);
56
57         lock = fetch_share_mode_unlocked(mem_ctx, id);
58         if (lock == NULL) {
59                 d_printf("Record with key %s not found.\n", argv[1]);
60                 goto out;
61         }
62
63         if (argc == 2 && strequal(argv[1], "dump")) {
64                 char *dump = share_mode_data_dump(mem_ctx, lock);
65                 d_printf("%s\n", dump);
66                 TALLOC_FREE(dump);
67         } else {
68                 NTSTATUS status;
69                 size_t num_share_modes = 0;
70
71                 status = share_mode_count_entries(id, &num_share_modes);
72                 if (!NT_STATUS_IS_OK(status)) {
73                         d_fprintf(stderr,
74                                   "Could not count share entries: %s\n",
75                                   nt_errstr(status));
76                         goto out;
77                 }
78
79                 d_printf("Share path:            %s\n",
80                          share_mode_servicepath(lock));
81                 d_printf("Name:                  %s\n",
82                          share_mode_filename(mem_ctx, lock));
83                 d_printf("Number of share modes: %zu\n", num_share_modes);
84         }
85
86         ret = 0;
87 out:
88         TALLOC_FREE(mem_ctx);
89         return ret;
90 }
91
92 int net_tdb(struct net_context *c, int argc, const char **argv)
93 {
94         struct functable func[] = {
95                 { "locking",
96                   net_tdb_locking,
97                   NET_TRANSPORT_LOCAL,
98                   N_("Show information for a record in locking.tdb"),
99                   N_("net tdb locking <key>")
100                 },
101                 {NULL, NULL, 0, NULL, NULL}
102         };
103
104         return net_run_function(c, argc, argv, "net tdb", func);
105 }