winbindd: as DC we should try to get the target_domain from @SOMETHING part of the...
[Samba.git] / lib / ldb / tools / ldbdump.c
blobc399b59eca47c503bffa761f70c81edea2cd4e90
1 /*
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/>.
21 #include "replace.h"
22 #include "system/locale.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "system/wait.h"
26 #include <tdb.h>
27 #include <ldb.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)
36 int ret, i, j;
37 struct ldb_dn *dn = state;
38 struct ldb_message *msg = talloc_zero(NULL, struct ldb_message);
39 struct ldb_val dbuf = {
40 .data = _dbuf.dptr,
41 .length = _dbuf.dsize,
43 struct ldb_ldif ldif = {
44 .msg = msg,
45 .changetype = LDB_CHANGETYPE_NONE
47 if (!msg) {
48 return -1;
51 ret = ldb_unpack_data(ldb, &dbuf, msg);
52 if (ret != 0) {
53 fprintf(stderr, "Failed to parse record %*.*s as an LDB record\n", (int)key.dsize, (int)key.dsize, (char *)key.dptr);
54 TALLOC_FREE(msg);
55 return 0;
58 if (dn && ldb_dn_compare(msg->dn, dn) != 0) {
59 TALLOC_FREE(msg);
60 return 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
72 TALLOC_FREE(msg);
73 return 0;
77 if (!validate_contents || ldb_dn_is_special(msg->dn)) {
78 ldb_ldif_write_file(ldb, stdout, &ldif);
79 TALLOC_FREE(msg);
80 return 0;
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++) {
88 struct ldb_val v;
89 ret = a->syntax->ldif_write_fn(ldb, msg, &msg->elements[i].values[j], &v);
90 if (ret != 0) {
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,
100 v.data);
101 TALLOC_FREE(msg);
102 return 0;
106 ldb_ldif_write_file(ldb, stdout, &ldif);
107 TALLOC_FREE(msg);
109 return 0;
112 static void log_stderr(struct tdb_context *tdb, enum tdb_debug_level level,
113 const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
115 static void log_stderr(struct tdb_context *tdb, enum tdb_debug_level level,
116 const char *fmt, ...)
118 va_list ap;
119 const char *name = tdb_name(tdb);
120 const char *prefix = "";
122 if (!name)
123 name = "unnamed";
125 switch (level) {
126 case TDB_DEBUG_ERROR:
127 prefix = "ERROR: ";
128 break;
129 case TDB_DEBUG_WARNING:
130 prefix = "WARNING: ";
131 break;
132 case TDB_DEBUG_TRACE:
133 return;
135 default:
136 case TDB_DEBUG_FATAL:
137 prefix = "FATAL: ";
138 break;
141 va_start(ap, fmt);
142 fprintf(stderr, "tdb(%s): %s", name, prefix);
143 vfprintf(stderr, fmt, ap);
144 va_end(ap);
147 static void emergency_walk(TDB_DATA key, TDB_DATA dbuf, void *keyname)
149 traverse_fn(NULL, key, dbuf, keyname);
152 static int dump_tdb(const char *fname, struct ldb_dn *dn, bool emergency)
154 TDB_CONTEXT *tdb;
155 struct tdb_logging_context logfn = { log_stderr };
157 tdb = tdb_open_ex(fname, 0, 0, O_RDONLY, 0, &logfn, NULL);
158 if (!tdb) {
159 fprintf(stderr, "Failed to open %s\n", fname);
160 return 1;
163 if (emergency) {
164 return tdb_rescue(tdb, emergency_walk, dn) == 0;
166 return tdb_traverse(tdb, traverse_fn, dn) == -1 ? 1 : 0;
169 static void usage( void)
171 printf( "Usage: ldbdump [options] <filename>\n\n");
172 printf( " -h this help message\n");
173 printf( " -d DN dumps DN only\n");
174 printf( " -e emergency dump, for corrupt databases\n");
175 printf( " -i include index and @BASEINFO records in dump\n");
176 printf( " -c validate contents of the records\n");
179 int main(int argc, char *argv[])
181 bool emergency = false;
182 int c, rc;
183 char *fname;
184 struct ldb_dn *dn = NULL;
186 ldb = ldb_init(NULL, NULL);
187 if (ldb == NULL) {
188 fprintf(stderr, "ldb: ldb_init failed()");
189 exit(1);
192 rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_PRECONNECT);
193 if (rc != LDB_SUCCESS) {
194 fprintf(stderr, "ldb: failed to run preconnect hooks (needed to get Samba LDIF handlers): %s\n", ldb_strerror(rc));
195 exit(1);
198 if (argc < 2) {
199 printf("Usage: ldbdump <fname>\n");
200 exit(1);
203 while ((c = getopt( argc, argv, "hd:eic")) != -1) {
204 switch (c) {
205 case 'h':
206 usage();
207 exit( 0);
208 case 'd':
209 dn = ldb_dn_new(ldb, ldb, optarg);
210 if (!dn) {
211 fprintf(stderr, "ldb failed to parse %s as a DN\n", optarg);
212 exit(1);
214 break;
215 case 'e':
216 emergency = true;
217 break;
218 case 'i':
219 show_index = true;
220 break;
221 case 'c':
222 validate_contents = true;
223 break;
224 default:
225 usage();
226 exit( 1);
230 fname = argv[optind];
232 return dump_tdb(fname, dn, emergency);