ntdbrestore.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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
  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.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "config.h"
  18. #include "ntdb.h"
  19. #include "private.h"
  20. #include <assert.h>
  21. static int read_linehead(FILE *f)
  22. {
  23. int i, c;
  24. int num_bytes;
  25. char prefix[128];
  26. while (1) {
  27. c = getc(f);
  28. if (c == EOF) {
  29. return -1;
  30. }
  31. if (c == '(') {
  32. break;
  33. }
  34. }
  35. for (i=0; i<sizeof(prefix); i++) {
  36. c = getc(f);
  37. if (c == EOF) {
  38. return -1;
  39. }
  40. prefix[i] = c;
  41. if (c == '"') {
  42. break;
  43. }
  44. }
  45. if (i == sizeof(prefix)) {
  46. return -1;
  47. }
  48. prefix[i] = '\0';
  49. if (sscanf(prefix, "%d) = ", &num_bytes) != 1) {
  50. return -1;
  51. }
  52. return num_bytes;
  53. }
  54. static int read_hex(void) {
  55. int c;
  56. c = getchar();
  57. if (c == EOF) {
  58. fprintf(stderr, "Unexpected EOF in data\n");
  59. return -1;
  60. } else if (c == '"') {
  61. fprintf(stderr, "Unexpected \\\" sequence\n");
  62. return -1;
  63. } else if ('0' <= c && c <= '9') {
  64. return c - '0';
  65. } else if ('A' <= c && c <= 'F') {
  66. return c - 'A' + 10;
  67. } else if ('a' <= c && c <= 'f') {
  68. return c - 'a' + 10;
  69. } else {
  70. fprintf(stderr, "Invalid hex: %c\n", c);
  71. return -1;
  72. }
  73. }
  74. static int read_data(FILE *f, NTDB_DATA *d, size_t size) {
  75. int c, low, high;
  76. int i;
  77. d->dptr = (unsigned char *)malloc(size);
  78. if (d->dptr == NULL) {
  79. return -1;
  80. }
  81. d->dsize = size;
  82. for (i=0; i<size; i++) {
  83. c = getc(f);
  84. if (c == EOF) {
  85. fprintf(stderr, "Unexpected EOF in data\n");
  86. return 1;
  87. } else if (c == '"') {
  88. return 0;
  89. } else if (c == '\\') {
  90. high = read_hex();
  91. if (high < 0) {
  92. return -1;
  93. }
  94. high = high << 4;
  95. assert(high == (high & 0xf0));
  96. low = read_hex();
  97. if (low < 0) {
  98. return -1;
  99. }
  100. assert(low == (low & 0x0f));
  101. d->dptr[i] = (low|high);
  102. } else {
  103. d->dptr[i] = c;
  104. }
  105. }
  106. return 0;
  107. }
  108. static int swallow(FILE *f, const char *s, int *eof)
  109. {
  110. char line[128];
  111. if (fgets(line, sizeof(line), f) == NULL) {
  112. if (eof != NULL) {
  113. *eof = 1;
  114. }
  115. return -1;
  116. }
  117. if (strcmp(line, s) != 0) {
  118. return -1;
  119. }
  120. return 0;
  121. }
  122. static bool read_rec(FILE *f, struct ntdb_context *ntdb, int *eof)
  123. {
  124. int length;
  125. NTDB_DATA key, data;
  126. bool ret = false;
  127. enum NTDB_ERROR e;
  128. key.dptr = NULL;
  129. data.dptr = NULL;
  130. if (swallow(f, "{\n", eof) == -1) {
  131. goto fail;
  132. }
  133. length = read_linehead(f);
  134. if (length == -1) {
  135. goto fail;
  136. }
  137. if (read_data(f, &key, length) == -1) {
  138. goto fail;
  139. }
  140. if (swallow(f, "\"\n", NULL) == -1) {
  141. goto fail;
  142. }
  143. length = read_linehead(f);
  144. if (length == -1) {
  145. goto fail;
  146. }
  147. if (read_data(f, &data, length) == -1) {
  148. goto fail;
  149. }
  150. if ((swallow(f, "\"\n", NULL) == -1)
  151. || (swallow(f, "}\n", NULL) == -1)) {
  152. goto fail;
  153. }
  154. e = ntdb_store(ntdb, key, data, NTDB_INSERT);
  155. if (e != NTDB_SUCCESS) {
  156. fprintf(stderr, "NTDB error: %s\n", ntdb_errorstr(e));
  157. goto fail;
  158. }
  159. ret = true;
  160. fail:
  161. free(key.dptr);
  162. free(data.dptr);
  163. return ret;
  164. }
  165. static int restore_ntdb(const char *fname, unsigned int hsize)
  166. {
  167. struct ntdb_context *ntdb;
  168. union ntdb_attribute hashsize;
  169. hashsize.base.attr = NTDB_ATTRIBUTE_HASHSIZE;
  170. hashsize.base.next = NULL;
  171. hashsize.hashsize.size = hsize;
  172. ntdb = ntdb_open(fname, 0, O_RDWR|O_CREAT|O_EXCL, 0666,
  173. hsize ? &hashsize : NULL);
  174. if (!ntdb) {
  175. perror("ntdb_open");
  176. fprintf(stderr, "Failed to open %s\n", fname);
  177. return 1;
  178. }
  179. while (1) {
  180. int eof = 0;
  181. if (!read_rec(stdin, ntdb, &eof)) {
  182. if (eof) {
  183. break;
  184. }
  185. return 1;
  186. }
  187. }
  188. if (ntdb_close(ntdb)) {
  189. fprintf(stderr, "Error closing ntdb\n");
  190. return 1;
  191. }
  192. fprintf(stderr, "EOF\n");
  193. return 0;
  194. }
  195. int main(int argc, char *argv[])
  196. {
  197. unsigned int hsize = 0;
  198. const char *execname = argv[0];
  199. if (argv[1] && strcmp(argv[1], "-h") == 0) {
  200. if (argv[2]) {
  201. hsize = atoi(argv[2]);
  202. }
  203. if (hsize == 0) {
  204. fprintf(stderr, "-h requires a integer value"
  205. " (eg. 128 or 131072)\n");
  206. exit(1);
  207. }
  208. argv += 2;
  209. argc -= 2;
  210. }
  211. if (argc != 2) {
  212. printf("Usage: %s [-h <hashsize>] dbname < tdbdump_output\n",
  213. execname);
  214. exit(1);
  215. }
  216. return restore_ntdb(argv[1], hsize);
  217. }