r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / source / lib / tdb / tools / tdbdump.c
blob4ceef2d60c10bc04d810a11af27efc90010cf65d
1 /*
2 Unix SMB/CIFS implementation.
3 simple tdb dump util
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "replace.h"
22 #include "system/locale.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "tdb.h"
27 static void print_data(TDB_DATA d)
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);
37 p++;
41 static int traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
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;
54 static int dump_tdb(const char *fname, const char *keyname)
56 TDB_CONTEXT *tdb;
57 TDB_DATA key, value;
59 tdb = tdb_open(fname, 0, 0, O_RDONLY, 0);
60 if (!tdb) {
61 printf("Failed to open %s\n", fname);
62 return 1;
65 if (!keyname) {
66 tdb_traverse(tdb, traverse_fn, NULL);
67 } else {
68 key.dptr = discard_const_p(uint8_t,keyname);
69 key.dsize = strlen( keyname);
70 value = tdb_fetch(tdb, key);
71 if (!value.dptr) {
72 return 1;
73 } else {
74 print_data(value);
75 free(value.dptr);
79 return 0;
82 static void usage( void)
84 printf( "Usage: tdbdump [options] <filename>\n\n");
85 printf( " -h this help message\n");
86 printf( " -k keyname dumps value of keyname\n");
89 int main(int argc, char *argv[])
91 char *fname, *keyname=NULL;
92 int c;
94 if (argc < 2) {
95 printf("Usage: tdbdump <fname>\n");
96 exit(1);
99 while ((c = getopt( argc, argv, "hk:")) != -1) {
100 switch (c) {
101 case 'h':
102 usage();
103 exit( 0);
104 case 'k':
105 keyname = optarg;
106 break;
107 default:
108 usage();
109 exit( 1);
113 fname = argv[optind];
115 return dump_tdb(fname, keyname);