3 from twisted.python import usage
4 from allmydata.scripts.common import BasedirMixin
6 class DumpOptions(usage.Options):
8 ["filename", "f", None, "which file to dump"],
11 def parseArgs(self, filename=None):
13 self['filename'] = filename
15 def postOptions(self):
16 if not self['filename']:
17 raise usage.UsageError("<filename> parameter is required")
19 class DumpRootDirnodeOptions(BasedirMixin, usage.Options):
21 ["basedir", "C", None, "the vdrive-server's base directory"],
24 class DumpDirnodeOptions(BasedirMixin, usage.Options):
26 ["uri", "u", None, "the URI of the dirnode to dump."],
27 ["basedir", "C", None, "which directory to create the introducer in"],
30 ["verbose", "v", "be extra noisy (show encrypted data)"],
32 def parseArgs(self, *args):
34 self['uri'] = args[-1]
36 BasedirMixin.parseArgs(self, *args)
38 def postOptions(self):
39 BasedirMixin.postOptions(self)
41 raise usage.UsageError("<uri> parameter is required")
43 def dump_uri_extension(config, out=sys.stdout, err=sys.stderr):
44 from allmydata import uri
46 filename = config['filename']
47 unpacked = uri.unpack_extension_readable(open(filename,"rb").read())
48 keys1 = ("size", "num_segments", "segment_size",
49 "needed_shares", "total_shares")
50 keys2 = ("codec_name", "codec_params", "tail_codec_params")
51 keys3 = ("plaintext_hash", "plaintext_root_hash",
52 "crypttext_hash", "crypttext_root_hash",
56 print >>out, "%19s: %s" % (k, unpacked[k])
60 print >>out, "%19s: %s" % (k, unpacked[k])
64 print >>out, "%19s: %s" % (k, unpacked[k])
66 leftover = set(unpacked.keys()) - set(keys1 + keys2 + keys3)
69 for k in sorted(leftover):
70 print >>out, "%s: %s" % (k, unpacked[k])
75 def dump_root_dirnode(config, out=sys.stdout, err=sys.stderr):
76 from allmydata import uri
78 basedir = config['basedirs'][0]
79 root_dirnode_file = os.path.join(basedir, "vdrive", "root")
81 f = open(root_dirnode_file, "rb")
83 rooturi = uri.pack_dirnode_uri("fakeFURL", key)
86 except EnvironmentError:
87 print >>out, "unable to read root dirnode file from %s" % \
91 def dump_directory_node(config, out=sys.stdout, err=sys.stderr):
92 from allmydata import uri, dirnode
93 from allmydata.util import hashutil, idlib
94 basedir = config['basedirs'][0]
95 dir_uri = config['uri']
96 verbose = config['verbose']
98 furl, key = uri.unpack_dirnode_uri(dir_uri)
99 if uri.is_mutable_dirnode_uri(dir_uri):
100 wk, we, rk, index = hashutil.generate_dirnode_keys_from_writekey(key)
102 wk, we, rk, index = hashutil.generate_dirnode_keys_from_readkey(key)
104 filename = os.path.join(basedir, "vdrive", idlib.b2a(index))
107 print >>out, "dirnode uri: %s" % dir_uri
108 print >>out, "filename : %s" % filename
109 print >>out, "index : %s" % idlib.b2a(index)
111 print >>out, "writekey : %s" % idlib.b2a(wk)
112 print >>out, "write_enabler: %s" % idlib.b2a(we)
114 print >>out, "writekey : None"
115 print >>out, "write_enabler: None"
116 print >>out, "readkey : %s" % idlib.b2a(rk)
120 vds = dirnode.VirtualDriveServer(os.path.join(basedir, "vdrive"), False)
121 data = vds._read_from_file(index)
124 print >>out, "ERROR: write_enabler does not match"
126 for (H_key, E_key, E_write, E_read) in data[1]:
128 print >>out, " H_key %s" % idlib.b2a(H_key)
129 print >>out, " E_key %s" % idlib.b2a(E_key)
130 print >>out, " E_write %s" % idlib.b2a(E_write)
131 print >>out, " E_read %s" % idlib.b2a(E_read)
132 key = dirnode.decrypt(rk, E_key)
133 print >>out, " key %s" % key
134 if hashutil.dir_name_hash(rk, key) != H_key:
135 print >>out, " ERROR: H_key does not match"
137 if len(E_write) < 14:
138 print >>out, " ERROR: write data is short:", idlib.b2a(E_write)
139 write = dirnode.decrypt(wk, E_write)
140 print >>out, " write: %s" % write
141 read = dirnode.decrypt(rk, E_read)
142 print >>out, " read: %s" % read
149 ["dump-uri-extension", None, DumpOptions,
150 "Unpack and display the contents of a uri_extension file."],
151 ["dump-root-dirnode", None, DumpRootDirnodeOptions,
152 "Compute most of the URI for the vdrive server's root dirnode."],
153 ["dump-dirnode", None, DumpDirnodeOptions,
154 "Unpack and display the contents of a vdrive DirectoryNode."],
158 "dump-uri-extension": dump_uri_extension,
159 "dump-root-dirnode": dump_root_dirnode,
160 "dump-dirnode": dump_directory_node,