s4-rpc_server: limit allowed transports for samr_ValidatePassword().
[Samba/gebeck_regimport.git] / lib / ldb / tools / ldbdump.c
blobedf7b5efb57236b3db3523052de28837558c9208
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_ldif ldif = {
40 .msg = msg,
41 .changetype = LDB_CHANGETYPE_NONE
43 if (!msg) {
44 return -1;
47 ret = ldb_unpack_data(ldb, &dbuf, msg);
48 if (ret != 0) {
49 fprintf(stderr, "Failed to parse record %*.*s as an LDB record\n", (int)key.dsize, (int)key.dsize, (char *)key.dptr);
50 TALLOC_FREE(msg);
51 return 0;
54 if (dn && ldb_dn_compare(msg->dn, dn) != 0) {
55 TALLOC_FREE(msg);
56 return 0;
59 if (!show_index && ldb_dn_is_special(msg->dn)) {
60 const char *dn_lin = ldb_dn_get_linearized(msg->dn);
61 if ((strcmp(dn_lin, "@BASEINFO") == 0) || (strncmp(dn_lin, "@INDEX:", strlen("@INDEX:")) == 0)) {
63 the user has asked not to show index
64 records. Also exclude BASEINFO as it
65 contains meta-data which will be re-created
66 if this database is restored
68 TALLOC_FREE(msg);
69 return 0;
73 if (!validate_contents || ldb_dn_is_special(msg->dn)) {
74 ldb_ldif_write_file(ldb, stdout, &ldif);
75 TALLOC_FREE(msg);
76 return 0;
79 for (i=0;i<msg->num_elements;i++) {
80 const struct ldb_schema_attribute *a;
82 a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);
83 for (j=0;j<msg->elements[i].num_values;j++) {
84 struct ldb_val v;
85 ret = a->syntax->ldif_write_fn(ldb, msg, &msg->elements[i].values[j], &v);
86 if (ret != 0) {
87 v = msg->elements[i].values[j];
88 if (ldb_should_b64_encode(ldb, &v)) {
89 v.data = (uint8_t *)ldb_base64_encode(ldb, (char *)v.data, v.length);
90 v.length = strlen((char *)v.data);
92 fprintf(stderr, "On %s element %s value %d (%*.*s) failed to convert to LDIF correctly, skipping possibly corrupt record\n",
93 ldb_dn_get_linearized(msg->dn),
94 msg->elements[i].name,
95 j, (int)v.length, (int)v.length,
96 v.data);
97 TALLOC_FREE(msg);
98 return 0;
102 ldb_ldif_write_file(ldb, stdout, &ldif);
103 TALLOC_FREE(msg);
105 return 0;
108 static void log_stderr(struct tdb_context *tdb, enum tdb_debug_level level,
109 const char *fmt, ...)
111 va_list ap;
112 const char *name = tdb_name(tdb);
113 const char *prefix = "";
115 if (!name)
116 name = "unnamed";
118 switch (level) {
119 case TDB_DEBUG_ERROR:
120 prefix = "ERROR: ";
121 break;
122 case TDB_DEBUG_WARNING:
123 prefix = "WARNING: ";
124 break;
125 case TDB_DEBUG_TRACE:
126 return;
128 default:
129 case TDB_DEBUG_FATAL:
130 prefix = "FATAL: ";
131 break;
134 va_start(ap, fmt);
135 fprintf(stderr, "tdb(%s): %s", name, prefix);
136 vfprintf(stderr, fmt, ap);
137 va_end(ap);
140 static void emergency_walk(TDB_DATA key, TDB_DATA dbuf, void *keyname)
142 traverse_fn(NULL, key, dbuf, keyname);
145 static int dump_tdb(const char *fname, struct ldb_dn *dn, bool emergency)
147 TDB_CONTEXT *tdb;
148 struct tdb_logging_context logfn = { log_stderr };
150 tdb = tdb_open_ex(fname, 0, 0, O_RDONLY, 0, &logfn, NULL);
151 if (!tdb) {
152 fprintf(stderr, "Failed to open %s\n", fname);
153 return 1;
156 if (emergency) {
157 return tdb_rescue(tdb, emergency_walk, dn) == 0;
159 return tdb_traverse(tdb, traverse_fn, dn) == -1 ? 1 : 0;
162 static void usage( void)
164 printf( "Usage: ldbdump [options] <filename>\n\n");
165 printf( " -h this help message\n");
166 printf( " -d DN dumps DN only\n");
167 printf( " -e emergency dump, for corrupt databases\n");
168 printf( " -i include index and @BASEINFO records in dump\n");
169 printf( " -c validate contents of the records\n");
172 int main(int argc, char *argv[])
174 bool emergency = false;
175 int c, rc;
176 char *fname;
177 struct ldb_dn *dn = NULL;
179 ldb = ldb_init(NULL, NULL);
180 if (ldb == NULL) {
181 fprintf(stderr, "ldb: ldb_init failed()");
182 exit(1);
185 rc = ldb_modules_hook(ldb, LDB_MODULE_HOOK_CMDLINE_PRECONNECT);
186 if (rc != LDB_SUCCESS) {
187 fprintf(stderr, "ldb: failed to run preconnect hooks (needed to get Samba LDIF handlers): %s\n", ldb_strerror(rc));
188 exit(1);
191 if (argc < 2) {
192 printf("Usage: ldbdump <fname>\n");
193 exit(1);
196 while ((c = getopt( argc, argv, "hd:ec")) != -1) {
197 switch (c) {
198 case 'h':
199 usage();
200 exit( 0);
201 case 'd':
202 dn = ldb_dn_new(ldb, ldb, optarg);
203 if (!dn) {
204 fprintf(stderr, "ldb failed to parse %s as a DN\n", optarg);
205 exit(1);
207 break;
208 case 'e':
209 emergency = true;
210 break;
211 case 'i':
212 show_index = true;
213 break;
214 case 'c':
215 validate_contents = true;
216 break;
217 default:
218 usage();
219 exit( 1);
223 fname = argv[optind];
225 return dump_tdb(fname, dn, emergency);