source: branches/samba-3.5.x/lib/tdb/tools/tdbdump.c

Last change on this file was 832, checked in by Herwig Bauernfeind, 12 years ago

Samba 3.5: Update Samba core to 3.5.22

File size: 2.6 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 int dump_tdb(const char *fname, const char *keyname)
55{
56 TDB_CONTEXT *tdb;
57 TDB_DATA key, value;
58 tdb = tdb_open(fname, 0, 0, O_RDONLY, 0);
59 if (!tdb) {
60 printf("Failed to open %s\n", fname);
61 return 1;
62 }
63
64 if (!keyname) {
65 tdb_traverse(tdb, traverse_fn, NULL);
66 } else {
67 key.dptr = discard_const_p(uint8_t, keyname);
68 key.dsize = strlen(keyname);
69 value = tdb_fetch(tdb, key);
70 if (!value.dptr) {
71 return 1;
72 } else {
73 print_data(value);
74 free(value.dptr);
75 }
76 }
77
78 return 0;
79}
80
81static void usage( void)
82{
83 printf( "Usage: tdbdump [options] <filename>\n\n");
84 printf( " -h this help message\n");
85 printf( " -k keyname dumps value of keyname\n");
86}
87
88 int main(int argc, char *argv[])
89{
90 char *fname, *keyname=NULL;
91 int c;
92
93 if (argc < 2) {
94 printf("Usage: tdbdump <fname>\n");
95 exit(1);
96 }
97
98 while ((c = getopt( argc, argv, "hk:")) != -1) {
99 switch (c) {
100 case 'h':
101 usage();
102 exit( 0);
103 case 'k':
104 keyname = optarg;
105 break;
106 default:
107 usage();
108 exit( 1);
109 }
110 }
111
112 fname = argv[optind];
113
114 return dump_tdb(fname, keyname);
115}
Note: See TracBrowser for help on using the repository browser.