dsdb: Align integer types
[Samba.git] / source3 / utils / net_tdb.c
blob0b81acf18e55f91b62dd087bf4f01992bb3071e1
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>
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 "locking/proto.h"
23 #include "librpc/gen_ndr/open_files.h"
24 #include "librpc/gen_ndr/ndr_open_files.h"
26 static int net_tdb_locking_dump(TALLOC_CTX *mem_ctx,
27 struct share_mode_data *data)
29 struct ndr_print *ndr_print;
31 ndr_print = talloc_zero(mem_ctx, struct ndr_print);
32 if (ndr_print == NULL) {
33 d_printf("Could not allocate memory.\n");
34 return -1;
37 ndr_print->print = ndr_print_printf_helper;
38 ndr_print->depth = 1;
39 ndr_print_share_mode_data(ndr_print, "SHARE_MODE_DATA", data);
40 TALLOC_FREE(ndr_print);
42 return 0;
45 static int net_tdb_locking_fetch(TALLOC_CTX *mem_ctx, const char *hexkey,
46 struct share_mode_lock **lock)
48 DATA_BLOB blob;
49 struct file_id id;
50 bool ok;
52 blob = strhex_to_data_blob(mem_ctx, hexkey);
53 if (blob.length != sizeof(struct file_id)) {
54 d_printf("Invalid length %zu of key, expected %zu\n",
55 blob.length,
56 sizeof(struct file_id));
57 return -1;
60 id = *(struct file_id *)blob.data;
62 ok = locking_init_readonly();
63 if (!ok) {
64 d_printf("locking_init_readonly failed\n");
65 return -1;
68 *lock = fetch_share_mode_unlocked(mem_ctx, id);
70 if (*lock == NULL) {
71 d_printf("Record with key %s not found.\n", hexkey);
72 return -1;
75 return 0;
78 static int net_tdb_locking(struct net_context *c, int argc, const char **argv)
80 TALLOC_CTX *mem_ctx = talloc_stackframe();
81 struct share_mode_lock *lock;
82 int ret;
84 if (argc < 1) {
85 d_printf("Usage: net tdb locking <key> [ dump ]\n");
86 ret = -1;
87 goto out;
90 ret = net_tdb_locking_fetch(mem_ctx, argv[0], &lock);
91 if (ret != 0) {
92 goto out;
95 if (argc == 2 && strequal(argv[1], "dump")) {
96 ret = net_tdb_locking_dump(mem_ctx, lock->data);
97 } else {
98 NTSTATUS status;
99 size_t num_share_modes = 0;
101 status = share_mode_count_entries(
102 lock->data->id, &num_share_modes);
103 if (!NT_STATUS_IS_OK(status)) {
104 d_fprintf(stderr,
105 "Could not count share entries: %s\n",
106 nt_errstr(status));
109 d_printf("Share path: %s\n", lock->data->servicepath);
110 d_printf("Name: %s\n", lock->data->base_name);
111 d_printf("Number of share modes: %zu\n", num_share_modes);
114 out:
115 TALLOC_FREE(mem_ctx);
116 return ret;
119 int net_tdb(struct net_context *c, int argc, const char **argv)
121 struct functable func[] = {
122 { "locking",
123 net_tdb_locking,
124 NET_TRANSPORT_LOCAL,
125 N_("Show information for a record in locking.tdb"),
126 N_("net tdb locking <key>")
128 {NULL, NULL, 0, NULL, NULL}
131 return net_run_function(c, argc, argv, "net tdb", func);