2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2001
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/>.
21 #include "system/locale.h"
22 #include "system/time.h"
23 #include "system/filesys.h"
24 #include "system/wait.h"
27 static void print_data(TDB_DATA d)
29 unsigned char *p = (unsigned char *)d.dptr;
32 if (isprint(*p) && !strchr("\"\\", *p)) {
41 static int traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
44 printf("key(%zu) = \"", key.dsize);
47 printf("data(%zu) = \"", dbuf.dsize);
54 static void log_stderr(struct tdb_context *tdb, enum tdb_debug_level level,
55 const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
57 static void log_stderr(struct tdb_context *tdb, enum tdb_debug_level level,
61 const char *name = tdb_name(tdb);
62 const char *prefix = "";
71 case TDB_DEBUG_WARNING:
84 fprintf(stderr, "tdb(%s): %s", name, prefix);
85 vfprintf(stderr, fmt, ap);
89 static void emergency_walk(TDB_DATA key, TDB_DATA dbuf, void *keyname)
92 if (key.dsize != strlen(keyname))
94 if (memcmp(key.dptr, keyname, key.dsize) != 0)
97 traverse_fn(NULL, key, dbuf, NULL);
100 static int dump_tdb(const char *fname, const char *keyname, bool emergency)
104 struct tdb_logging_context logfn = {
105 .log_fn = log_stderr,
107 int tdb_flags = TDB_DEFAULT;
110 * Note: that O_RDONLY implies TDB_NOLOCK, but we want to make it
111 * explicit as it's important when working on databases which were
112 * created with mutex locking.
114 tdb_flags |= TDB_NOLOCK;
116 tdb = tdb_open_ex(fname, 0, tdb_flags, O_RDONLY, 0, &logfn, NULL);
118 printf("Failed to open %s\n", fname);
123 return tdb_rescue(tdb, emergency_walk, discard_const(keyname)) == 0;
126 return tdb_traverse(tdb, traverse_fn, NULL) == -1 ? 1 : 0;
128 key.dptr = discard_const_p(uint8_t, keyname);
129 key.dsize = strlen(keyname);
130 value = tdb_fetch(tdb, key);
142 static void usage( void)
144 printf( "Usage: tdbdump [options] <filename>\n\n");
145 printf( " -h this help message\n");
146 printf( " -k keyname dumps value of keyname\n");
147 printf( " -e emergency dump, for corrupt databases\n");
150 int main(int argc, char *argv[])
152 char *fname, *keyname=NULL;
153 bool emergency = false;
157 printf("Usage: tdbdump <fname>\n");
161 while ((c = getopt( argc, argv, "hk:e")) != -1) {
178 fname = argv[optind];
180 return dump_tdb(fname, keyname, emergency);