net: Use dom_sid_str_buf
[garming/samba-autobuild/.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/proto.h"
23 #include "librpc/gen_ndr/open_files.h"
24 #include "librpc/gen_ndr/ndr_open_files.h"
25
26 static int net_tdb_locking_dump(TALLOC_CTX *mem_ctx,
27                                 struct share_mode_data *data)
28 {
29         struct ndr_print *ndr_print;
30
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;
35         }
36
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);
41
42         return 0;
43 }
44
45 static int net_tdb_locking_fetch(TALLOC_CTX *mem_ctx, const char *hexkey,
46                                  struct share_mode_lock **lock)
47 {
48         DATA_BLOB blob;
49         struct file_id id;
50         bool ok;
51
52         blob = strhex_to_data_blob(mem_ctx, hexkey);
53         if (blob.length != sizeof(struct file_id)) {
54                 d_printf("Invalid length of key\n");
55                 return -1;
56         }
57
58         id = *(struct file_id *)blob.data;
59
60         ok = locking_init_readonly();
61         if (!ok) {
62                 d_printf("locking_init_readonly failed\n");
63                 return -1;
64         }
65
66         *lock = fetch_share_mode_unlocked(mem_ctx, id);
67
68         if (*lock == NULL) {
69                 d_printf("Record with key %s not found.\n", hexkey);
70                 return -1;
71         }
72
73         return 0;
74 }
75
76 static int net_tdb_locking(struct net_context *c, int argc, const char **argv)
77 {
78         TALLOC_CTX *mem_ctx = talloc_stackframe();
79         struct share_mode_lock *lock;
80         int ret;
81
82         if (argc < 1) {
83                 d_printf("Usage: net tdb locking <key> [ dump ]\n");
84                 ret = -1;
85                 goto out;
86         }
87
88         ret = net_tdb_locking_fetch(mem_ctx, argv[0], &lock);
89         if (ret != 0) {
90                 goto out;
91         }
92
93         if (argc == 2 && strequal(argv[1], "dump")) {
94                 ret = net_tdb_locking_dump(mem_ctx, lock->data);
95         } else {
96                 d_printf("Share path:            %s\n", lock->data->servicepath);
97                 d_printf("Name:                  %s\n", lock->data->base_name);
98                 d_printf("Number of share modes: %" PRIu32 "\n",
99                          lock->data->num_share_modes);
100         }
101
102 out:
103         TALLOC_FREE(mem_ctx);
104         return ret;
105 }
106
107 int net_tdb(struct net_context *c, int argc, const char **argv)
108 {
109         struct functable func[] = {
110                 { "locking",
111                   net_tdb_locking,
112                   NET_TRANSPORT_LOCAL,
113                   N_("Show information for a record in locking.tdb"),
114                   N_("net tdb locking <key>")
115                 },
116                 {NULL, NULL, 0, NULL, NULL}
117         };
118
119         return net_run_function(c, argc, argv, "net tdb", func);
120 }