2 Unix SMB/CIFS implementation.
3 simple ldb tdb dump util
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Andrew Bartlett 2012
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/locale.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "system/wait.h"
28 #include <ldb_private.h>
30 static struct ldb_context
*ldb
;
31 bool show_index
= false;
32 bool validate_contents
= false;
34 static int traverse_fn(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA _dbuf
, void *state
)
37 struct ldb_dn
*dn
= state
;
38 struct ldb_message
*msg
= talloc_zero(NULL
, struct ldb_message
);
39 struct ldb_val dbuf
= {
41 .length
= _dbuf
.dsize
,
43 struct ldb_ldif ldif
= {
45 .changetype
= LDB_CHANGETYPE_NONE
51 ret
= ldb_unpack_data(ldb
, &dbuf
, msg
);
53 fprintf(stderr
, "Failed to parse record %*.*s as an LDB record\n", (int)key
.dsize
, (int)key
.dsize
, (char *)key
.dptr
);
58 if (dn
&& ldb_dn_compare(msg
->dn
, dn
) != 0) {
63 if (!show_index
&& ldb_dn_is_special(msg
->dn
)) {
64 const char *dn_lin
= ldb_dn_get_linearized(msg
->dn
);
65 if ((strcmp(dn_lin
, "@BASEINFO") == 0) || (strncmp(dn_lin
, "@INDEX:", strlen("@INDEX:")) == 0)) {
67 the user has asked not to show index
68 records. Also exclude BASEINFO as it
69 contains meta-data which will be re-created
70 if this database is restored
77 if (!validate_contents
|| ldb_dn_is_special(msg
->dn
)) {
78 ldb_ldif_write_file(ldb
, stdout
, &ldif
);
83 for (i
=0;i
<msg
->num_elements
;i
++) {
84 const struct ldb_schema_attribute
*a
;
86 a
= ldb_schema_attribute_by_name(ldb
, msg
->elements
[i
].name
);
87 for (j
=0;j
<msg
->elements
[i
].num_values
;j
++) {
89 ret
= a
->syntax
->ldif_write_fn(ldb
, msg
, &msg
->elements
[i
].values
[j
], &v
);
91 v
= msg
->elements
[i
].values
[j
];
92 if (ldb_should_b64_encode(ldb
, &v
)) {
93 v
.data
= (uint8_t *)ldb_base64_encode(ldb
, (char *)v
.data
, v
.length
);
94 v
.length
= strlen((char *)v
.data
);
96 fprintf(stderr
, "On %s element %s value %d (%*.*s) failed to convert to LDIF correctly, skipping possibly corrupt record\n",
97 ldb_dn_get_linearized(msg
->dn
),
98 msg
->elements
[i
].name
,
99 j
, (int)v
.length
, (int)v
.length
,
106 ldb_ldif_write_file(ldb
, stdout
, &ldif
);
112 static void log_stderr(struct tdb_context
*tdb
, enum tdb_debug_level level
,
113 const char *fmt
, ...)
116 const char *name
= tdb_name(tdb
);
117 const char *prefix
= "";
123 case TDB_DEBUG_ERROR
:
126 case TDB_DEBUG_WARNING
:
127 prefix
= "WARNING: ";
129 case TDB_DEBUG_TRACE
:
133 case TDB_DEBUG_FATAL
:
139 fprintf(stderr
, "tdb(%s): %s", name
, prefix
);
140 vfprintf(stderr
, fmt
, ap
);
144 static void emergency_walk(TDB_DATA key
, TDB_DATA dbuf
, void *keyname
)
146 traverse_fn(NULL
, key
, dbuf
, keyname
);
149 static int dump_tdb(const char *fname
, struct ldb_dn
*dn
, bool emergency
)
152 struct tdb_logging_context logfn
= { log_stderr
};
154 tdb
= tdb_open_ex(fname
, 0, 0, O_RDONLY
, 0, &logfn
, NULL
);
156 fprintf(stderr
, "Failed to open %s\n", fname
);
161 return tdb_rescue(tdb
, emergency_walk
, dn
) == 0;
163 return tdb_traverse(tdb
, traverse_fn
, dn
) == -1 ? 1 : 0;
166 static void usage( void)
168 printf( "Usage: ldbdump [options] <filename>\n\n");
169 printf( " -h this help message\n");
170 printf( " -d DN dumps DN only\n");
171 printf( " -e emergency dump, for corrupt databases\n");
172 printf( " -i include index and @BASEINFO records in dump\n");
173 printf( " -c validate contents of the records\n");
176 int main(int argc
, char *argv
[])
178 bool emergency
= false;
181 struct ldb_dn
*dn
= NULL
;
183 ldb
= ldb_init(NULL
, NULL
);
185 fprintf(stderr
, "ldb: ldb_init failed()");
189 rc
= ldb_modules_hook(ldb
, LDB_MODULE_HOOK_CMDLINE_PRECONNECT
);
190 if (rc
!= LDB_SUCCESS
) {
191 fprintf(stderr
, "ldb: failed to run preconnect hooks (needed to get Samba LDIF handlers): %s\n", ldb_strerror(rc
));
196 printf("Usage: ldbdump <fname>\n");
200 while ((c
= getopt( argc
, argv
, "hd:ec")) != -1) {
206 dn
= ldb_dn_new(ldb
, ldb
, optarg
);
208 fprintf(stderr
, "ldb failed to parse %s as a DN\n", optarg
);
219 validate_contents
= true;
227 fname
= argv
[optind
];
229 return dump_tdb(fname
, dn
, emergency
);