rpcclient: abort shadow-copy set on commit failure
[Samba.git] / lib / ntdb / tools / ntdbrestore.c
blob695af79cb2f8bd01ab6fbd26d76130bd604d3595
1 /*
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/>.
21 #include "config.h"
22 #include "ntdb.h"
23 #include "private.h"
24 #include <assert.h>
26 static int read_linehead(FILE *f)
28 int i, c;
29 int num_bytes;
30 char prefix[128];
32 while (1) {
33 c = getc(f);
34 if (c == EOF) {
35 return -1;
37 if (c == '(') {
38 break;
41 for (i=0; i<sizeof(prefix); i++) {
42 c = getc(f);
43 if (c == EOF) {
44 return -1;
46 prefix[i] = c;
47 if (c == '"') {
48 break;
51 if (i == sizeof(prefix)) {
52 return -1;
54 prefix[i] = '\0';
56 if (sscanf(prefix, "%d) = ", &num_bytes) != 1) {
57 return -1;
59 return num_bytes;
62 static int read_hex(void) {
63 int c;
64 c = getchar();
65 if (c == EOF) {
66 fprintf(stderr, "Unexpected EOF in data\n");
67 return -1;
68 } else if (c == '"') {
69 fprintf(stderr, "Unexpected \\\" sequence\n");
70 return -1;
71 } else if ('0' <= c && c <= '9') {
72 return c - '0';
73 } else if ('A' <= c && c <= 'F') {
74 return c - 'A' + 10;
75 } else if ('a' <= c && c <= 'f') {
76 return c - 'a' + 10;
77 } else {
78 fprintf(stderr, "Invalid hex: %c\n", c);
79 return -1;
83 static int read_data(FILE *f, NTDB_DATA *d, size_t size) {
84 int c, low, high;
85 int i;
87 d->dptr = (unsigned char *)malloc(size);
88 if (d->dptr == NULL) {
89 return -1;
91 d->dsize = size;
93 for (i=0; i<size; i++) {
94 c = getc(f);
95 if (c == EOF) {
96 fprintf(stderr, "Unexpected EOF in data\n");
97 return 1;
98 } else if (c == '"') {
99 return 0;
100 } else if (c == '\\') {
101 high = read_hex();
102 if (high < 0) {
103 return -1;
105 high = high << 4;
106 assert(high == (high & 0xf0));
107 low = read_hex();
108 if (low < 0) {
109 return -1;
111 assert(low == (low & 0x0f));
112 d->dptr[i] = (low|high);
113 } else {
114 d->dptr[i] = c;
117 return 0;
120 static int swallow(FILE *f, const char *s, int *eof)
122 char line[128];
124 if (fgets(line, sizeof(line), f) == NULL) {
125 if (eof != NULL) {
126 *eof = 1;
128 return -1;
130 if (strcmp(line, s) != 0) {
131 return -1;
133 return 0;
136 static bool read_rec(FILE *f, struct ntdb_context *ntdb, int *eof)
138 int length;
139 NTDB_DATA key, data;
140 bool ret = false;
141 enum NTDB_ERROR e;
143 key.dptr = NULL;
144 data.dptr = NULL;
146 if (swallow(f, "{\n", eof) == -1) {
147 goto fail;
149 length = read_linehead(f);
150 if (length == -1) {
151 goto fail;
153 if (read_data(f, &key, length) == -1) {
154 goto fail;
156 if (swallow(f, "\"\n", NULL) == -1) {
157 goto fail;
159 length = read_linehead(f);
160 if (length == -1) {
161 goto fail;
163 if (read_data(f, &data, length) == -1) {
164 goto fail;
166 if ((swallow(f, "\"\n", NULL) == -1)
167 || (swallow(f, "}\n", NULL) == -1)) {
168 goto fail;
170 e = ntdb_store(ntdb, key, data, NTDB_INSERT);
171 if (e != NTDB_SUCCESS) {
172 fprintf(stderr, "NTDB error: %s\n", ntdb_errorstr(e));
173 goto fail;
176 ret = true;
177 fail:
178 free(key.dptr);
179 free(data.dptr);
180 return ret;
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);
194 if (!ntdb) {
195 perror("ntdb_open");
196 fprintf(stderr, "Failed to open %s\n", fname);
197 return 1;
200 while (1) {
201 int eof = 0;
202 if (!read_rec(stdin, ntdb, &eof)) {
203 if (eof) {
204 break;
206 return 1;
209 if (ntdb_close(ntdb)) {
210 fprintf(stderr, "Error closing ntdb\n");
211 return 1;
213 fprintf(stderr, "EOF\n");
214 return 0;
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) {
223 if (argv[2]) {
224 hsize = atoi(argv[2]);
226 if (hsize == 0) {
227 fprintf(stderr, "-h requires a integer value"
228 " (eg. 128 or 131072)\n");
229 exit(1);
231 argv += 2;
232 argc -= 2;
234 if (argc != 2) {
235 printf("Usage: %s [-h <hashsize>] dbname < tdbdump_output\n",
236 execname);
237 exit(1);
241 return restore_ntdb(argv[1], hsize);