1 | /* Simple program to dump out all records of TDB */ |
---|
2 | #include <stdint.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <fcntl.h> |
---|
5 | #include <stdio.h> |
---|
6 | #include <stdarg.h> |
---|
7 | |
---|
8 | #include "xs_lib.h" |
---|
9 | #include "tdb.h" |
---|
10 | #include "talloc.h" |
---|
11 | #include "utils.h" |
---|
12 | |
---|
13 | struct record_hdr { |
---|
14 | uint32_t num_perms; |
---|
15 | uint32_t datalen; |
---|
16 | uint32_t childlen; |
---|
17 | struct xs_permissions perms[0]; |
---|
18 | }; |
---|
19 | |
---|
20 | static uint32_t total_size(struct record_hdr *hdr) |
---|
21 | { |
---|
22 | return sizeof(*hdr) + hdr->num_perms * sizeof(struct xs_permissions) |
---|
23 | + hdr->datalen + hdr->childlen; |
---|
24 | } |
---|
25 | |
---|
26 | static char perm_to_char(enum xs_perm_type perm) |
---|
27 | { |
---|
28 | return perm == XS_PERM_READ ? 'r' : |
---|
29 | perm == XS_PERM_WRITE ? 'w' : |
---|
30 | perm == XS_PERM_NONE ? '-' : |
---|
31 | perm == (XS_PERM_READ|XS_PERM_WRITE) ? 'b' : |
---|
32 | '?'; |
---|
33 | } |
---|
34 | |
---|
35 | int main(int argc, char *argv[]) |
---|
36 | { |
---|
37 | TDB_DATA key; |
---|
38 | TDB_CONTEXT *tdb; |
---|
39 | |
---|
40 | if (argc != 2) |
---|
41 | barf("Usage: xs_tdb_dump <tdbfile>"); |
---|
42 | |
---|
43 | tdb = tdb_open(talloc_strdup(NULL, argv[1]), 0, 0, O_RDONLY, 0); |
---|
44 | if (!tdb) |
---|
45 | barf_perror("Could not open %s", argv[1]); |
---|
46 | |
---|
47 | key = tdb_firstkey(tdb); |
---|
48 | while (key.dptr) { |
---|
49 | TDB_DATA data; |
---|
50 | struct record_hdr *hdr; |
---|
51 | |
---|
52 | data = tdb_fetch(tdb, key); |
---|
53 | hdr = (void *)data.dptr; |
---|
54 | if (data.dsize < sizeof(*hdr)) |
---|
55 | fprintf(stderr, "%.*s: BAD truncated\n", |
---|
56 | (int)key.dsize, key.dptr); |
---|
57 | else if (data.dsize != total_size(hdr)) |
---|
58 | fprintf(stderr, "%.*s: BAD length %i for %i/%i/%i (%i)\n", |
---|
59 | (int)key.dsize, key.dptr, (int)data.dsize, |
---|
60 | hdr->num_perms, hdr->datalen, |
---|
61 | hdr->childlen, total_size(hdr)); |
---|
62 | else { |
---|
63 | unsigned int i; |
---|
64 | char *p; |
---|
65 | |
---|
66 | printf("%.*s: ", (int)key.dsize, key.dptr); |
---|
67 | for (i = 0; i < hdr->num_perms; i++) |
---|
68 | printf("%s%c%i", |
---|
69 | i == 0 ? "" : ",", |
---|
70 | perm_to_char(hdr->perms[i].perms), |
---|
71 | hdr->perms[i].id); |
---|
72 | p = (void *)&hdr->perms[hdr->num_perms]; |
---|
73 | printf(" %.*s\n", hdr->datalen, p); |
---|
74 | p += hdr->datalen; |
---|
75 | for (i = 0; i < hdr->childlen; i += strlen(p+i)+1) |
---|
76 | printf("\t-> %s\n", p+i); |
---|
77 | } |
---|
78 | key = tdb_nextkey(tdb, key); |
---|
79 | } |
---|
80 | return 0; |
---|
81 | } |
---|
82 | |
---|