1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | simple ldb tdb dump util
|
---|
4 | Copyright (C) Andrew Tridgell 2001
|
---|
5 | Copyright (C) Andrew Bartlett 2012
|
---|
6 |
|
---|
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.
|
---|
11 |
|
---|
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.
|
---|
16 |
|
---|
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/>.
|
---|
19 | */
|
---|
20 |
|
---|
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>
|
---|
29 |
|
---|
30 | static struct ldb_context *ldb;
|
---|
31 | bool show_index = false;
|
---|
32 | bool validate_contents = false;
|
---|
33 |
|
---|
34 | static int traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA _dbuf, void *state)
|
---|
35 | {
|
---|
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,
|
---|
42 | };
|
---|
43 | struct ldb_ldif ldif = {
|
---|
44 | .msg = msg,
|
---|
45 | .changetype = LDB_CHANGETYPE_NONE
|
---|
46 | };
|
---|
47 | if (!msg) {
|
---|
48 | return -1;
|
---|
49 | }
|
---|
50 |
|
---|
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;
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (dn && ldb_dn_compare(msg->dn, dn) != 0) {
|
---|
59 | TALLOC_FREE(msg);
|
---|
60 | return 0;
|
---|
61 | }
|
---|
62 |
|
---|
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)) {
|
---|
66 | /*
|
---|
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
|
---|
71 | */
|
---|
72 | TALLOC_FREE(msg);
|
---|
73 | return 0;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
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;
|
---|
81 | }
|
---|
82 |
|
---|
83 | for (i=0;i<msg->num_elements;i++) {
|
---|
84 | const struct ldb_schema_attribute *a;
|
---|
85 |
|
---|
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);
|
---|
95 | }
|
---|
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;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 | ldb_ldif_write_file(ldb, stdout, &ldif);
|
---|
107 | TALLOC_FREE(msg);
|
---|
108 |
|
---|
109 | return 0;
|
---|
110 | }
|
---|
111 |
|
---|
112 | static void log_stderr(struct tdb_context *tdb, enum tdb_debug_level level,
|
---|
113 | const char *fmt, ...)
|
---|
114 | {
|
---|
115 | va_list ap;
|
---|
116 | const char *name = tdb_name(tdb);
|
---|
117 | const char *prefix = "";
|
---|
118 |
|
---|
119 | if (!name)
|
---|
120 | name = "unnamed";
|
---|
121 |
|
---|
122 | switch (level) {
|
---|
123 | case TDB_DEBUG_ERROR:
|
---|
124 | prefix = "ERROR: ";
|
---|
125 | break;
|
---|
126 | case TDB_DEBUG_WARNING:
|
---|
127 | prefix = "WARNING: ";
|
---|
128 | break;
|
---|
129 | case TDB_DEBUG_TRACE:
|
---|
130 | return;
|
---|
131 |
|
---|
132 | default:
|
---|
133 | case TDB_DEBUG_FATAL:
|
---|
134 | prefix = "FATAL: ";
|
---|
135 | break;
|
---|
136 | }
|
---|
137 |
|
---|
138 | va_start(ap, fmt);
|
---|
139 | fprintf(stderr, "tdb(%s): %s", name, prefix);
|
---|
140 | vfprintf(stderr, fmt, ap);
|
---|
141 | va_end(ap);
|
---|
142 | }
|
---|
143 |
|
---|
144 | static void emergency_walk(TDB_DATA key, TDB_DATA dbuf, void *keyname)
|
---|
145 | {
|
---|
146 | traverse_fn(NULL, key, dbuf, keyname);
|
---|
147 | }
|
---|
148 |
|
---|
149 | static int dump_tdb(const char *fname, struct ldb_dn *dn, bool emergency)
|
---|
150 | {
|
---|
151 | TDB_CONTEXT *tdb;
|
---|
152 | struct tdb_logging_context logfn = { log_stderr };
|
---|
153 |
|
---|
154 | tdb = tdb_open_ex(fname, 0, 0, O_RDONLY, 0, &logfn, NULL);
|
---|
155 | if (!tdb) {
|
---|
156 | fprintf(stderr, "Failed to open %s\n", fname);
|
---|
157 | return 1;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (emergency) {
|
---|
161 | return tdb_rescue(tdb, emergency_walk, dn) == 0;
|
---|
162 | }
|
---|
163 | return tdb_traverse(tdb, traverse_fn, dn) == -1 ? 1 : 0;
|
---|
164 | }
|
---|
165 |
|
---|
166 | static void usage( void)
|
---|
167 | {
|
---|
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");
|
---|
174 | }
|
---|
175 |
|
---|
176 | int main(int argc, char *argv[])
|
---|
177 | {
|
---|
178 | bool emergency = false;
|
---|
179 | int c, rc;
|
---|
180 | char *fname;
|
---|
181 | struct ldb_dn *dn = NULL;
|
---|
182 |
|
---|
183 | ldb = ldb_init(NULL, NULL);
|
---|
184 | if (ldb == NULL) {
|
---|
185 | fprintf(stderr, "ldb: ldb_init failed()");
|
---|
186 | exit(1);
|
---|
187 | }
|
---|
188 |
|
---|
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));
|
---|
192 | exit(1);
|
---|
193 | }
|
---|
194 |
|
---|
195 | if (argc < 2) {
|
---|
196 | printf("Usage: ldbdump <fname>\n");
|
---|
197 | exit(1);
|
---|
198 | }
|
---|
199 |
|
---|
200 | while ((c = getopt( argc, argv, "hd:ec")) != -1) {
|
---|
201 | switch (c) {
|
---|
202 | case 'h':
|
---|
203 | usage();
|
---|
204 | exit( 0);
|
---|
205 | case 'd':
|
---|
206 | dn = ldb_dn_new(ldb, ldb, optarg);
|
---|
207 | if (!dn) {
|
---|
208 | fprintf(stderr, "ldb failed to parse %s as a DN\n", optarg);
|
---|
209 | exit(1);
|
---|
210 | }
|
---|
211 | break;
|
---|
212 | case 'e':
|
---|
213 | emergency = true;
|
---|
214 | break;
|
---|
215 | case 'i':
|
---|
216 | show_index = true;
|
---|
217 | break;
|
---|
218 | case 'c':
|
---|
219 | validate_contents = true;
|
---|
220 | break;
|
---|
221 | default:
|
---|
222 | usage();
|
---|
223 | exit( 1);
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | fname = argv[optind];
|
---|
228 |
|
---|
229 | return dump_tdb(fname, dn, emergency);
|
---|
230 | }
|
---|