ntdbdump.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. simple ntdb dump util
  3. Copyright (C) Andrew Tridgell 2001
  4. Copyright (C) Rusty Russell 2011
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "config.h"
  17. #include "ntdb.h"
  18. #include "private.h"
  19. static void print_data(NTDB_DATA d)
  20. {
  21. unsigned char *p = (unsigned char *)d.dptr;
  22. int len = d.dsize;
  23. while (len--) {
  24. if (isprint(*p) && !strchr("\"\\", *p)) {
  25. fputc(*p, stdout);
  26. } else {
  27. printf("\\%02X", *p);
  28. }
  29. p++;
  30. }
  31. }
  32. static int traverse_fn(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *state)
  33. {
  34. printf("{\n");
  35. printf("key(%d) = \"", (int)key.dsize);
  36. print_data(key);
  37. printf("\"\n");
  38. printf("data(%d) = \"", (int)dbuf.dsize);
  39. print_data(dbuf);
  40. printf("\"\n");
  41. printf("}\n");
  42. return 0;
  43. }
  44. static int dump_ntdb(const char *fname, const char *keyname)
  45. {
  46. struct ntdb_context *ntdb;
  47. NTDB_DATA key, value;
  48. ntdb = ntdb_open(fname, 0, O_RDONLY, 0, NULL);
  49. if (!ntdb) {
  50. printf("Failed to open %s\n", fname);
  51. return 1;
  52. }
  53. if (!keyname) {
  54. ntdb_traverse(ntdb, traverse_fn, NULL);
  55. } else {
  56. key = ntdb_mkdata(keyname, strlen(keyname));
  57. if (ntdb_fetch(ntdb, key, &value) != 0) {
  58. return 1;
  59. } else {
  60. print_data(value);
  61. free(value.dptr);
  62. }
  63. }
  64. return 0;
  65. }
  66. static void usage( void)
  67. {
  68. printf( "Usage: ntdbdump [options] <filename>\n\n");
  69. printf( " -h this help message\n");
  70. printf( " -k keyname dumps value of keyname\n");
  71. }
  72. int main(int argc, char *argv[])
  73. {
  74. char *fname, *keyname=NULL;
  75. int c;
  76. if (argc < 2) {
  77. printf("Usage: ntdbdump <fname>\n");
  78. exit(1);
  79. }
  80. while ((c = getopt( argc, argv, "hk:")) != -1) {
  81. switch (c) {
  82. case 'h':
  83. usage();
  84. exit( 0);
  85. case 'k':
  86. keyname = optarg;
  87. break;
  88. default:
  89. usage();
  90. exit( 1);
  91. }
  92. }
  93. fname = argv[optind];
  94. return dump_ntdb(fname, keyname);
  95. }