netlogon_creds_cli: Protect netlogon_creds_cli_auth by _lck
[Samba.git] / lib / tdb / tools / tdbrestore.c
blob81c986c60038b3490c6113175ffad394153cce51
1 /*
2 tdbrestore -- construct a tdb from tdbdump output.
3 Copyright (C) Volker Lendecke 2010
4 Copyright (C) Simon McVittie 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "replace.h"
21 #include <assert.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"
28 static int read_linehead(FILE *f)
30 int i, c;
31 int num_bytes;
32 char prefix[128];
34 while (1) {
35 c = getc(f);
36 if (c == EOF) {
37 return -1;
39 if (c == '(') {
40 break;
43 for (i=0; i<sizeof(prefix); i++) {
44 c = getc(f);
45 if (c == EOF) {
46 return -1;
48 prefix[i] = c;
49 if (c == '"') {
50 break;
53 if (i == sizeof(prefix)) {
54 return -1;
56 prefix[i] = '\0';
58 if (sscanf(prefix, "%d) = ", &num_bytes) != 1) {
59 return -1;
61 return num_bytes;
64 static int read_hex(void) {
65 int c;
66 c = getchar();
67 if (c == EOF) {
68 fprintf(stderr, "Unexpected EOF in data\n");
69 return -1;
70 } else if (c == '"') {
71 fprintf(stderr, "Unexpected \\\" sequence\n");
72 return -1;
73 } else if ('0' <= c && c <= '9') {
74 return c - '0';
75 } else if ('A' <= c && c <= 'F') {
76 return c - 'A' + 10;
77 } else if ('a' <= c && c <= 'f') {
78 return c - 'a' + 10;
79 } else {
80 fprintf(stderr, "Invalid hex: %c\n", c);
81 return -1;
85 static int read_data(FILE *f, TDB_DATA *d, size_t size) {
86 int c, low, high;
87 int i;
89 d->dptr = (unsigned char *)malloc(size);
90 if (d->dptr == NULL) {
91 return -1;
93 d->dsize = size;
95 for (i=0; i<size; i++) {
96 c = getc(f);
97 if (c == EOF) {
98 fprintf(stderr, "Unexpected EOF in data\n");
99 return 1;
100 } else if (c == '"') {
101 return 0;
102 } else if (c == '\\') {
103 high = read_hex();
104 if (high < 0) {
105 return -1;
107 high = high << 4;
108 assert(high == (high & 0xf0));
109 low = read_hex();
110 if (low < 0) {
111 return -1;
113 assert(low == (low & 0x0f));
114 d->dptr[i] = (low|high);
115 } else {
116 d->dptr[i] = c;
119 return 0;
122 static int swallow(FILE *f, const char *s, int *eof)
124 char line[128];
126 if (fgets(line, sizeof(line), f) == NULL) {
127 if (eof != NULL) {
128 *eof = 1;
130 return -1;
132 if (strcmp(line, s) != 0) {
133 return -1;
135 return 0;
138 static int read_rec(FILE *f, TDB_CONTEXT *tdb, int *eof)
140 int length;
141 TDB_DATA key, data;
142 int ret = -1;
144 key.dptr = NULL;
145 data.dptr = NULL;
147 if (swallow(f, "{\n", eof) == -1) {
148 goto fail;
150 length = read_linehead(f);
151 if (length == -1) {
152 goto fail;
154 if (read_data(f, &key, length) == -1) {
155 goto fail;
157 if (swallow(f, "\"\n", NULL) == -1) {
158 goto fail;
160 length = read_linehead(f);
161 if (length == -1) {
162 goto fail;
164 if (read_data(f, &data, length) == -1) {
165 goto fail;
167 if ((swallow(f, "\"\n", NULL) == -1)
168 || (swallow(f, "}\n", NULL) == -1)) {
169 goto fail;
171 if (tdb_store(tdb, key, data, TDB_INSERT) != 0) {
172 fprintf(stderr, "TDB error: %s\n", tdb_errorstr(tdb));
173 goto fail;
176 ret = 0;
177 fail:
178 free(key.dptr);
179 free(data.dptr);
180 return ret;
183 static int restore_tdb(const char *fname)
185 TDB_CONTEXT *tdb;
187 tdb = tdb_open(fname, 0, 0, O_RDWR|O_CREAT|O_EXCL, 0666);
188 if (!tdb) {
189 perror("tdb_open");
190 fprintf(stderr, "Failed to open %s\n", fname);
191 return 1;
194 while (1) {
195 int eof = 0;
196 if (read_rec(stdin, tdb, &eof) == -1) {
197 if (eof) {
198 break;
200 return 1;
203 if (tdb_close(tdb)) {
204 fprintf(stderr, "Error closing tdb\n");
205 return 1;
207 return 0;
210 int main(int argc, char *argv[])
212 char *fname;
214 if (argc < 2) {
215 printf("Usage: %s dbname < tdbdump_output\n", argv[0]);
216 exit(1);
219 fname = argv[1];
221 return restore_tdb(fname);