]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/mdb_load.c
ITS#8558 fix mdb_load with escaped plaintext
[openldap] / libraries / liblmdb / mdb_load.c
1 /* mdb_load.c - memory-mapped database load tool */
2 /*
3  * Copyright 2011-2016 Howard Chu, Symas Corp.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted only as authorized by the OpenLDAP
8  * Public License.
9  *
10  * A copy of this license is available in the file LICENSE in the
11  * top-level directory of the distribution or, alternatively, at
12  * <http://www.OpenLDAP.org/license.html>.
13  */
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <unistd.h>
20 #include "lmdb.h"
21
22 #define PRINT   1
23 #define NOHDR   2
24 static int mode;
25
26 static char *subname = NULL;
27
28 static mdb_size_t lineno;
29 static int version;
30
31 static int flags;
32
33 static char *prog;
34
35 static int Eof;
36
37 static MDB_envinfo info;
38
39 static MDB_val kbuf, dbuf;
40
41 #define Yu      MDB_PRIy(u)
42
43 #define STRLENOF(s)     (sizeof(s)-1)
44
45 typedef struct flagbit {
46         int bit;
47         char *name;
48         int len;
49 } flagbit;
50
51 #define S(s)    s, STRLENOF(s)
52
53 flagbit dbflags[] = {
54         { MDB_REVERSEKEY, S("reversekey") },
55         { MDB_DUPSORT, S("dupsort") },
56         { MDB_INTEGERKEY, S("integerkey") },
57         { MDB_DUPFIXED, S("dupfixed") },
58         { MDB_INTEGERDUP, S("integerdup") },
59         { MDB_REVERSEDUP, S("reversedup") },
60         { 0, NULL, 0 }
61 };
62
63 static void readhdr(void)
64 {
65         char *ptr;
66
67         while (fgets(dbuf.mv_data, dbuf.mv_size, stdin) != NULL) {
68                 lineno++;
69                 if (!strncmp(dbuf.mv_data, "VERSION=", STRLENOF("VERSION="))) {
70                         version=atoi((char *)dbuf.mv_data+STRLENOF("VERSION="));
71                         if (version > 3) {
72                                 fprintf(stderr, "%s: line %"Yu": unsupported VERSION %d\n",
73                                         prog, lineno, version);
74                                 exit(EXIT_FAILURE);
75                         }
76                 } else if (!strncmp(dbuf.mv_data, "HEADER=END", STRLENOF("HEADER=END"))) {
77                         break;
78                 } else if (!strncmp(dbuf.mv_data, "format=", STRLENOF("format="))) {
79                         if (!strncmp((char *)dbuf.mv_data+STRLENOF("FORMAT="), "print", STRLENOF("print")))
80                                 mode |= PRINT;
81                         else if (strncmp((char *)dbuf.mv_data+STRLENOF("FORMAT="), "bytevalue", STRLENOF("bytevalue"))) {
82                                 fprintf(stderr, "%s: line %"Yu": unsupported FORMAT %s\n",
83                                         prog, lineno, (char *)dbuf.mv_data+STRLENOF("FORMAT="));
84                                 exit(EXIT_FAILURE);
85                         }
86                 } else if (!strncmp(dbuf.mv_data, "database=", STRLENOF("database="))) {
87                         ptr = memchr(dbuf.mv_data, '\n', dbuf.mv_size);
88                         if (ptr) *ptr = '\0';
89                         if (subname) free(subname);
90                         subname = strdup((char *)dbuf.mv_data+STRLENOF("database="));
91                 } else if (!strncmp(dbuf.mv_data, "type=", STRLENOF("type="))) {
92                         if (strncmp((char *)dbuf.mv_data+STRLENOF("type="), "btree", STRLENOF("btree")))  {
93                                 fprintf(stderr, "%s: line %"Yu": unsupported type %s\n",
94                                         prog, lineno, (char *)dbuf.mv_data+STRLENOF("type="));
95                                 exit(EXIT_FAILURE);
96                         }
97                 } else if (!strncmp(dbuf.mv_data, "mapaddr=", STRLENOF("mapaddr="))) {
98                         int i;
99                         ptr = memchr(dbuf.mv_data, '\n', dbuf.mv_size);
100                         if (ptr) *ptr = '\0';
101                         i = sscanf((char *)dbuf.mv_data+STRLENOF("mapaddr="), "%p", &info.me_mapaddr);
102                         if (i != 1) {
103                                 fprintf(stderr, "%s: line %"Yu": invalid mapaddr %s\n",
104                                         prog, lineno, (char *)dbuf.mv_data+STRLENOF("mapaddr="));
105                                 exit(EXIT_FAILURE);
106                         }
107                 } else if (!strncmp(dbuf.mv_data, "mapsize=", STRLENOF("mapsize="))) {
108                         int i;
109                         ptr = memchr(dbuf.mv_data, '\n', dbuf.mv_size);
110                         if (ptr) *ptr = '\0';
111                         i = sscanf((char *)dbuf.mv_data+STRLENOF("mapsize="),
112                                 "%" MDB_SCNy(u), &info.me_mapsize);
113                         if (i != 1) {
114                                 fprintf(stderr, "%s: line %"Yu": invalid mapsize %s\n",
115                                         prog, lineno, (char *)dbuf.mv_data+STRLENOF("mapsize="));
116                                 exit(EXIT_FAILURE);
117                         }
118                 } else if (!strncmp(dbuf.mv_data, "maxreaders=", STRLENOF("maxreaders="))) {
119                         int i;
120                         ptr = memchr(dbuf.mv_data, '\n', dbuf.mv_size);
121                         if (ptr) *ptr = '\0';
122                         i = sscanf((char *)dbuf.mv_data+STRLENOF("maxreaders="), "%u", &info.me_maxreaders);
123                         if (i != 1) {
124                                 fprintf(stderr, "%s: line %"Yu": invalid maxreaders %s\n",
125                                         prog, lineno, (char *)dbuf.mv_data+STRLENOF("maxreaders="));
126                                 exit(EXIT_FAILURE);
127                         }
128                 } else {
129                         int i;
130                         for (i=0; dbflags[i].bit; i++) {
131                                 if (!strncmp(dbuf.mv_data, dbflags[i].name, dbflags[i].len) &&
132                                         ((char *)dbuf.mv_data)[dbflags[i].len] == '=') {
133                                         flags |= dbflags[i].bit;
134                                         break;
135                                 }
136                         }
137                         if (!dbflags[i].bit) {
138                                 ptr = memchr(dbuf.mv_data, '=', dbuf.mv_size);
139                                 if (!ptr) {
140                                         fprintf(stderr, "%s: line %"Yu": unexpected format\n",
141                                                 prog, lineno);
142                                         exit(EXIT_FAILURE);
143                                 } else {
144                                         *ptr = '\0';
145                                         fprintf(stderr, "%s: line %"Yu": unrecognized keyword ignored: %s\n",
146                                                 prog, lineno, (char *)dbuf.mv_data);
147                                 }
148                         }
149                 }
150         }
151 }
152
153 static void badend(void)
154 {
155         fprintf(stderr, "%s: line %"Yu": unexpected end of input\n",
156                 prog, lineno);
157 }
158
159 static int unhex(unsigned char *c2)
160 {
161         int x, c;
162         x = *c2++ & 0x4f;
163         if (x & 0x40)
164                 x -= 55;
165         c = x << 4;
166         x = *c2 & 0x4f;
167         if (x & 0x40)
168                 x -= 55;
169         c |= x;
170         return c;
171 }
172
173 static int readline(MDB_val *out, MDB_val *buf)
174 {
175         unsigned char *c1, *c2, *end;
176         size_t len, l2;
177         int c;
178
179         if (!(mode & NOHDR)) {
180                 c = fgetc(stdin);
181                 if (c == EOF) {
182                         Eof = 1;
183                         return EOF;
184                 }
185                 if (c != ' ') {
186                         lineno++;
187                         if (fgets(buf->mv_data, buf->mv_size, stdin) == NULL) {
188 badend:
189                                 Eof = 1;
190                                 badend();
191                                 return EOF;
192                         }
193                         if (c == 'D' && !strncmp(buf->mv_data, "ATA=END", STRLENOF("ATA=END")))
194                                 return EOF;
195                         goto badend;
196                 }
197         }
198         if (fgets(buf->mv_data, buf->mv_size, stdin) == NULL) {
199                 Eof = 1;
200                 return EOF;
201         }
202         lineno++;
203
204         c1 = buf->mv_data;
205         len = strlen((char *)c1);
206         l2 = len;
207
208         /* Is buffer too short? */
209         while (c1[len-1] != '\n') {
210                 buf->mv_data = realloc(buf->mv_data, buf->mv_size*2);
211                 if (!buf->mv_data) {
212                         Eof = 1;
213                         fprintf(stderr, "%s: line %"Yu": out of memory, line too long\n",
214                                 prog, lineno);
215                         return EOF;
216                 }
217                 c1 = buf->mv_data;
218                 c1 += l2;
219                 if (fgets((char *)c1, buf->mv_size+1, stdin) == NULL) {
220                         Eof = 1;
221                         badend();
222                         return EOF;
223                 }
224                 buf->mv_size *= 2;
225                 len = strlen((char *)c1);
226                 l2 += len;
227         }
228         c1 = c2 = buf->mv_data;
229         len = l2;
230         c1[--len] = '\0';
231         end = c1 + len;
232
233         if (mode & PRINT) {
234                 while (c2 < end) {
235                         if (*c2 == '\\') {
236                                 if (c2[1] == '\\') {
237                                         c1++; c2 += 2;
238                                 } else {
239                                         if (c2+3 > end || !isxdigit(c2[1]) || !isxdigit(c2[2])) {
240                                                 Eof = 1;
241                                                 badend();
242                                                 return EOF;
243                                         }
244                                         *c1++ = unhex(++c2);
245                                         c2 += 2;
246                                 }
247                         } else {
248                                 /* copies are redundant when no escapes were used */
249                                 *c1++ = *c2++;
250                         }
251                 }
252         } else {
253                 /* odd length not allowed */
254                 if (len & 1) {
255                         Eof = 1;
256                         badend();
257                         return EOF;
258                 }
259                 while (c2 < end) {
260                         if (!isxdigit(*c2) || !isxdigit(c2[1])) {
261                                 Eof = 1;
262                                 badend();
263                                 return EOF;
264                         }
265                         *c1++ = unhex(c2);
266                         c2 += 2;
267                 }
268         }
269         c2 = out->mv_data = buf->mv_data;
270         out->mv_size = c1 - c2;
271
272         return 0;
273 }
274
275 static void usage(void)
276 {
277         fprintf(stderr, "usage: %s [-V] [-f input] [-n] [-s name] [-N] [-T] dbpath\n", prog);
278         exit(EXIT_FAILURE);
279 }
280
281 int main(int argc, char *argv[])
282 {
283         int i, rc;
284         MDB_env *env;
285         MDB_txn *txn;
286         MDB_cursor *mc;
287         MDB_dbi dbi;
288         char *envname;
289         int envflags = 0, putflags = 0;
290         int dohdr = 0;
291
292         prog = argv[0];
293
294         if (argc < 2) {
295                 usage();
296         }
297
298         /* -f: load file instead of stdin
299          * -n: use NOSUBDIR flag on env_open
300          * -s: load into named subDB
301          * -N: use NOOVERWRITE on puts
302          * -T: read plaintext
303          * -V: print version and exit
304          */
305         while ((i = getopt(argc, argv, "f:ns:NTV")) != EOF) {
306                 switch(i) {
307                 case 'V':
308                         printf("%s\n", MDB_VERSION_STRING);
309                         exit(0);
310                         break;
311                 case 'f':
312                         if (freopen(optarg, "r", stdin) == NULL) {
313                                 fprintf(stderr, "%s: %s: reopen: %s\n",
314                                         prog, optarg, strerror(errno));
315                                 exit(EXIT_FAILURE);
316                         }
317                         break;
318                 case 'n':
319                         envflags |= MDB_NOSUBDIR;
320                         break;
321                 case 's':
322                         subname = strdup(optarg);
323                         break;
324                 case 'N':
325                         putflags = MDB_NOOVERWRITE|MDB_NODUPDATA;
326                         break;
327                 case 'T':
328                         mode |= NOHDR | PRINT;
329                         break;
330                 default:
331                         usage();
332                 }
333         }
334
335         if (optind != argc - 1)
336                 usage();
337
338         dbuf.mv_size = 4096;
339         dbuf.mv_data = malloc(dbuf.mv_size);
340
341         if (!(mode & NOHDR))
342                 readhdr();
343
344         envname = argv[optind];
345         rc = mdb_env_create(&env);
346         if (rc) {
347                 fprintf(stderr, "mdb_env_create failed, error %d %s\n", rc, mdb_strerror(rc));
348                 return EXIT_FAILURE;
349         }
350
351         mdb_env_set_maxdbs(env, 2);
352
353         if (info.me_maxreaders)
354                 mdb_env_set_maxreaders(env, info.me_maxreaders);
355
356         if (info.me_mapsize)
357                 mdb_env_set_mapsize(env, info.me_mapsize);
358
359         if (info.me_mapaddr)
360                 envflags |= MDB_FIXEDMAP;
361
362         rc = mdb_env_open(env, envname, envflags, 0664);
363         if (rc) {
364                 fprintf(stderr, "mdb_env_open failed, error %d %s\n", rc, mdb_strerror(rc));
365                 goto env_close;
366         }
367
368         kbuf.mv_size = mdb_env_get_maxkeysize(env) * 2 + 2;
369         kbuf.mv_data = malloc(kbuf.mv_size);
370
371         while(!Eof) {
372                 MDB_val key, data;
373                 int batch = 0;
374                 flags = 0;
375
376                 if (!dohdr) {
377                         dohdr = 1;
378                 } else if (!(mode & NOHDR))
379                         readhdr();
380                 
381                 rc = mdb_txn_begin(env, NULL, 0, &txn);
382                 if (rc) {
383                         fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
384                         goto env_close;
385                 }
386
387                 rc = mdb_open(txn, subname, flags|MDB_CREATE, &dbi);
388                 if (rc) {
389                         fprintf(stderr, "mdb_open failed, error %d %s\n", rc, mdb_strerror(rc));
390                         goto txn_abort;
391                 }
392
393                 rc = mdb_cursor_open(txn, dbi, &mc);
394                 if (rc) {
395                         fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
396                         goto txn_abort;
397                 }
398
399                 while(1) {
400                         rc = readline(&key, &kbuf);
401                         if (rc)  /* rc == EOF */
402                                 break;
403
404                         rc = readline(&data, &dbuf);
405                         if (rc) {
406                                 fprintf(stderr, "%s: line %"Yu": failed to read key value\n", prog, lineno);
407                                 goto txn_abort;
408                         }
409
410                         rc = mdb_cursor_put(mc, &key, &data, putflags);
411                         if (rc == MDB_KEYEXIST && putflags)
412                                 continue;
413                         if (rc) {
414                                 fprintf(stderr, "mdb_cursor_put failed, error %d %s\n", rc, mdb_strerror(rc));
415                                 goto txn_abort;
416                         }
417                         batch++;
418                         if (batch == 100) {
419                                 rc = mdb_txn_commit(txn);
420                                 if (rc) {
421                                         fprintf(stderr, "%s: line %"Yu": txn_commit: %s\n",
422                                                 prog, lineno, mdb_strerror(rc));
423                                         goto env_close;
424                                 }
425                                 rc = mdb_txn_begin(env, NULL, 0, &txn);
426                                 if (rc) {
427                                         fprintf(stderr, "mdb_txn_begin failed, error %d %s\n", rc, mdb_strerror(rc));
428                                         goto env_close;
429                                 }
430                                 rc = mdb_cursor_open(txn, dbi, &mc);
431                                 if (rc) {
432                                         fprintf(stderr, "mdb_cursor_open failed, error %d %s\n", rc, mdb_strerror(rc));
433                                         goto txn_abort;
434                                 }
435                                 batch = 0;
436                         }
437                 }
438                 rc = mdb_txn_commit(txn);
439                 txn = NULL;
440                 if (rc) {
441                         fprintf(stderr, "%s: line %"Yu": txn_commit: %s\n",
442                                 prog, lineno, mdb_strerror(rc));
443                         goto env_close;
444                 }
445                 mdb_dbi_close(env, dbi);
446         }
447
448 txn_abort:
449         mdb_txn_abort(txn);
450 env_close:
451         mdb_env_close(env);
452
453         return rc ? EXIT_FAILURE : EXIT_SUCCESS;
454 }