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>
32 #endif /* ifdef HAVE_LMDB */
35 static struct ldb_context *ldb;
36 bool show_index = false;
37 bool validate_contents = false;
39 static int traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA _dbuf, void *state)
42 struct ldb_dn *dn = state;
43 struct ldb_message *msg = talloc_zero(NULL, struct ldb_message);
44 struct ldb_val dbuf = {
46 .length = _dbuf.dsize,
48 struct ldb_ldif ldif = {
50 .changetype = LDB_CHANGETYPE_NONE
56 ret = ldb_unpack_data(ldb, &dbuf, msg);
58 fprintf(stderr, "Failed to parse record %*.*s as an LDB record\n", (int)key.dsize, (int)key.dsize, (char *)key.dptr);
63 if (dn && ldb_dn_compare(msg->dn, dn) != 0) {
68 if (!show_index && ldb_dn_is_special(msg->dn)) {
69 const char *dn_lin = ldb_dn_get_linearized(msg->dn);
70 if ((strcmp(dn_lin, "@BASEINFO") == 0) || (strncmp(dn_lin, "@INDEX:", strlen("@INDEX:")) == 0)) {
72 the user has asked not to show index
73 records. Also exclude BASEINFO as it
74 contains meta-data which will be re-created
75 if this database is restored
82 if (!validate_contents || ldb_dn_is_special(msg->dn)) {
83 ldb_ldif_write_file(ldb, stdout, &ldif);
88 for (i=0;i<msg->num_elements;i++) {
89 const struct ldb_schema_attribute *a;
91 a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);
92 for (j=0;j<msg->elements[i].num_values;j++) {
94 ret = a->syntax->ldif_write_fn(ldb, msg, &msg->elements[i].values[j], &v);
96 v = msg->elements[i].values[j];
97 if (ldb_should_b64_encode(ldb, &v)) {
98 v.data = (uint8_t *)ldb_base64_encode(ldb, (char *)v.data, v.length);
99 v.length = strlen((char *)v.data);
101 fprintf(stderr, "On %s element %s value %d (%*.*s) failed to convert to LDIF correctly, skipping possibly corrupt record\n",
102 ldb_dn_get_linearized(msg->dn),
103 msg->elements[i].name,
104 j, (int)v.length, (int)v.length,
111 ldb_ldif_write_file(ldb, stdout, &ldif);
117 static void log_stderr(struct tdb_context *tdb, enum tdb_debug_level level,
118 const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
120 static void log_stderr(struct tdb_context *tdb, enum tdb_debug_level level,
121 const char *fmt, ...)
124 const char *name = tdb_name(tdb);
125 const char *prefix = "";
131 case TDB_DEBUG_ERROR:
134 case TDB_DEBUG_WARNING:
135 prefix = "WARNING: ";
137 case TDB_DEBUG_TRACE:
141 case TDB_DEBUG_FATAL:
147 fprintf(stderr, "tdb(%s): %s", name, prefix);
148 vfprintf(stderr, fmt, ap);
152 static void emergency_walk(TDB_DATA key, TDB_DATA dbuf, void *keyname)
154 traverse_fn(NULL, key, dbuf, keyname);
157 static int dump_tdb(const char *fname, struct ldb_dn *dn, bool emergency)
160 struct tdb_logging_context logfn = { log_stderr };
162 tdb = tdb_open_ex(fname, 0, 0, O_RDONLY, 0, &logfn, NULL);
164 fprintf(stderr, "Failed to open %s\n", fname);
169 return tdb_rescue(tdb, emergency_walk, dn) == 0;
171 return tdb_traverse(tdb, traverse_fn, dn) == -1 ? 1 : 0;
175 static int dump_lmdb(const char *fname, struct ldb_dn *dn, bool emergency)
178 struct MDB_env *env = NULL;
179 struct MDB_txn *txn = NULL;
181 struct MDB_cursor *cursor = NULL;
185 ret = mdb_env_create(&env);
188 "Could not create MDB environment: (%d) %s\n",
194 ret = mdb_env_open(env,
196 MDB_NOSUBDIR|MDB_NOTLS|MDB_RDONLY,
200 "Could not open environment for %s: (%d) %s\n",
207 ret = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
210 "Could not start transaction: (%d) %s\n",
216 ret = mdb_dbi_open(txn, NULL, 0, &dbi);
219 "Could not open database: (%d) %s\n",
225 ret = mdb_cursor_open(txn, dbi, &cursor);
228 "Could not open cursor: (%d) %s\n",
234 ret = mdb_cursor_get(cursor, &key, &data, MDB_FIRST);
235 if (ret != 0 && ret != MDB_NOTFOUND) {
237 "Could not find first record: (%d) %s\n",
242 while (ret != MDB_NOTFOUND) {
243 struct TDB_DATA tkey = {
247 struct TDB_DATA tdata = {
248 .dptr = data.mv_data,
249 .dsize = data.mv_size
251 traverse_fn(NULL, tkey, tdata, dn);
252 ret = mdb_cursor_get(cursor, &key, &data, MDB_NEXT);
253 if (ret != 0 && ret != MDB_NOTFOUND) {
255 "Could not read next record: (%d) %s\n",
264 mdb_cursor_close(cursor);
277 static int dump_lmdb(const char *fname, struct ldb_dn *dn, bool emergency)
279 /* not built with lmdb support */
282 #endif /* #ifdef HAVE_LMDB */
284 static void usage( void)
286 printf( "Usage: ldbdump [options] <filename>\n\n");
287 printf( " -h this help message\n");
288 printf( " -d DN dumps DN only\n");
289 printf( " -e emergency dump, for corrupt databases\n");
290 printf( " -i include index and @BASEINFO records in dump\n");
291 printf( " -c validate contents of the records\n");
294 int main(int argc, char *argv[])
296 bool emergency = false;
299 struct ldb_dn *dn = NULL;
301 ldb = ldb_init(NULL, NULL);
303 fprintf(stderr, "ldb: ldb_init failed()");
307 rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_PRECONNECT);
308 if (rc != LDB_SUCCESS) {
309 fprintf(stderr, "ldb: failed to run preconnect hooks (needed to get Samba LDIF handlers): %s\n", ldb_strerror(rc));
314 printf("Usage: ldbdump <fname>\n");
318 while ((c = getopt( argc, argv, "hd:eic")) != -1) {
324 dn = ldb_dn_new(ldb, ldb, optarg);
326 fprintf(stderr, "ldb failed to parse %s as a DN\n", optarg);
337 validate_contents = true;
345 fname = argv[optind];
347 rc = dump_lmdb(fname, dn, emergency);
349 rc = dump_tdb(fname, dn, emergency);
351 fprintf(stderr, "Failed to open %s\n", fname);