2 ntdbrestore -- construct a ntdb from (n)tdbdump output.
3 Copyright (C) Rusty Russell 2012
4 Copyright (C) Volker Lendecke 2010
5 Copyright (C) Simon McVittie 2005
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/>.
26 static int read_linehead(FILE *f)
41 for (i=0; i<sizeof(prefix); i++) {
51 if (i == sizeof(prefix)) {
56 if (sscanf(prefix, "%d) = ", &num_bytes) != 1) {
62 static int read_hex(void) {
66 fprintf(stderr, "Unexpected EOF in data\n");
68 } else if (c == '"') {
69 fprintf(stderr, "Unexpected \\\" sequence\n");
71 } else if ('0' <= c && c <= '9') {
73 } else if ('A' <= c && c <= 'F') {
75 } else if ('a' <= c && c <= 'f') {
78 fprintf(stderr, "Invalid hex: %c\n", c);
83 static int read_data(FILE *f, NTDB_DATA *d, size_t size) {
87 d->dptr = (unsigned char *)malloc(size);
88 if (d->dptr == NULL) {
93 for (i=0; i<size; i++) {
96 fprintf(stderr, "Unexpected EOF in data\n");
98 } else if (c == '"') {
100 } else if (c == '\\') {
106 assert(high == (high & 0xf0));
111 assert(low == (low & 0x0f));
112 d->dptr[i] = (low|high);
120 static int swallow(FILE *f, const char *s, int *eof)
124 if (fgets(line, sizeof(line), f) == NULL) {
130 if (strcmp(line, s) != 0) {
136 static bool read_rec(FILE *f, struct ntdb_context *ntdb, int *eof)
146 if (swallow(f, "{\n", eof) == -1) {
149 length = read_linehead(f);
153 if (read_data(f, &key, length) == -1) {
156 if (swallow(f, "\"\n", NULL) == -1) {
159 length = read_linehead(f);
163 if (read_data(f, &data, length) == -1) {
166 if ((swallow(f, "\"\n", NULL) == -1)
167 || (swallow(f, "}\n", NULL) == -1)) {
170 e = ntdb_store(ntdb, key, data, NTDB_INSERT);
171 if (e != NTDB_SUCCESS) {
172 fprintf(stderr, "NTDB error: %s\n", ntdb_errorstr(e));
183 static int restore_ntdb(const char *fname, unsigned int hsize)
185 struct ntdb_context *ntdb;
186 union ntdb_attribute hashsize;
188 hashsize.base.attr = NTDB_ATTRIBUTE_HASHSIZE;
189 hashsize.base.next = NULL;
190 hashsize.hashsize.size = hsize;
192 ntdb = ntdb_open(fname, 0, O_RDWR|O_CREAT|O_EXCL, 0666,
193 hsize ? &hashsize : NULL);
196 fprintf(stderr, "Failed to open %s\n", fname);
202 if (!read_rec(stdin, ntdb, &eof)) {
209 if (ntdb_close(ntdb)) {
210 fprintf(stderr, "Error closing ntdb\n");
213 fprintf(stderr, "EOF\n");
217 int main(int argc, char *argv[])
219 unsigned int hsize = 0;
220 const char *execname = argv[0];
222 if (argv[1] && strcmp(argv[1], "-h") == 0) {
224 hsize = atoi(argv[2]);
227 fprintf(stderr, "-h requires a integer value"
228 " (eg. 128 or 131072)\n");
235 printf("Usage: %s [-h <hashsize>] dbname < tdbdump_output\n",
241 return restore_ntdb(argv[1], hsize);