source: vendor/current/lib/tdb/tools/tdbdump.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: 3.8 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 simple tdb dump util
4 Copyright (C) Andrew Tridgell 2001
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 "replace.h"
21#include "system/locale.h"
22#include "system/time.h"
23#include "system/filesys.h"
24#include "system/wait.h"
25#include "tdb.h"
26
27static void print_data(TDB_DATA d)
28{
29 unsigned char *p = (unsigned char *)d.dptr;
30 int len = d.dsize;
31 while (len--) {
32 if (isprint(*p) && !strchr("\"\\", *p)) {
33 fputc(*p, stdout);
34 } else {
35 printf("\\%02X", *p);
36 }
37 p++;
38 }
39}
40
41static int traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
42{
43 printf("{\n");
44 printf("key(%d) = \"", (int)key.dsize);
45 print_data(key);
46 printf("\"\n");
47 printf("data(%d) = \"", (int)dbuf.dsize);
48 print_data(dbuf);
49 printf("\"\n");
50 printf("}\n");
51 return 0;
52}
53
54static void log_stderr(struct tdb_context *tdb, enum tdb_debug_level level,
55 const char *fmt, ...)
56{
57 va_list ap;
58 const char *name = tdb_name(tdb);
59 const char *prefix = "";
60
61 if (!name)
62 name = "unnamed";
63
64 switch (level) {
65 case TDB_DEBUG_ERROR:
66 prefix = "ERROR: ";
67 break;
68 case TDB_DEBUG_WARNING:
69 prefix = "WARNING: ";
70 break;
71 case TDB_DEBUG_TRACE:
72 return;
73
74 default:
75 case TDB_DEBUG_FATAL:
76 prefix = "FATAL: ";
77 break;
78 }
79
80 va_start(ap, fmt);
81 fprintf(stderr, "tdb(%s): %s", name, prefix);
82 vfprintf(stderr, fmt, ap);
83 va_end(ap);
84}
85
86static void emergency_walk(TDB_DATA key, TDB_DATA dbuf, void *keyname)
87{
88 if (keyname) {
89 if (key.dsize != strlen(keyname))
90 return;
91 if (memcmp(key.dptr, keyname, key.dsize) != 0)
92 return;
93 }
94 traverse_fn(NULL, key, dbuf, NULL);
95}
96
97static int dump_tdb(const char *fname, const char *keyname, bool emergency)
98{
99 TDB_CONTEXT *tdb;
100 TDB_DATA key, value;
101 struct tdb_logging_context logfn = { log_stderr };
102 int tdb_flags = TDB_DEFAULT;
103
104 /*
105 * Note: that O_RDONLY implies TDB_NOLOCK, but we want to make it
106 * explicit as it's important when working on databases which were
107 * created with mutex locking.
108 */
109 tdb_flags |= TDB_NOLOCK;
110
111 tdb = tdb_open_ex(fname, 0, tdb_flags, O_RDONLY, 0, &logfn, NULL);
112 if (!tdb) {
113 printf("Failed to open %s\n", fname);
114 return 1;
115 }
116
117 if (emergency) {
118 return tdb_rescue(tdb, emergency_walk, discard_const(keyname)) == 0;
119 }
120 if (!keyname) {
121 return tdb_traverse(tdb, traverse_fn, NULL) == -1 ? 1 : 0;
122 } else {
123 key.dptr = discard_const_p(uint8_t, keyname);
124 key.dsize = strlen(keyname);
125 value = tdb_fetch(tdb, key);
126 if (!value.dptr) {
127 return 1;
128 } else {
129 print_data(value);
130 free(value.dptr);
131 }
132 }
133
134 return 0;
135}
136
137static void usage( void)
138{
139 printf( "Usage: tdbdump [options] <filename>\n\n");
140 printf( " -h this help message\n");
141 printf( " -k keyname dumps value of keyname\n");
142 printf( " -e emergency dump, for corrupt databases\n");
143}
144
145 int main(int argc, char *argv[])
146{
147 char *fname, *keyname=NULL;
148 bool emergency = false;
149 int c;
150
151 if (argc < 2) {
152 printf("Usage: tdbdump <fname>\n");
153 exit(1);
154 }
155
156 while ((c = getopt( argc, argv, "hk:e")) != -1) {
157 switch (c) {
158 case 'h':
159 usage();
160 exit( 0);
161 case 'k':
162 keyname = optarg;
163 break;
164 case 'e':
165 emergency = true;
166 break;
167 default:
168 usage();
169 exit( 1);
170 }
171 }
172
173 fname = argv[optind];
174
175 return dump_tdb(fname, keyname, emergency);
176}
Note: See TracBrowser for help on using the repository browser.