-
Notifications
You must be signed in to change notification settings - Fork 0
/
amnesiafs.py
executable file
·375 lines (305 loc) · 11.9 KB
/
amnesiafs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/python2.6
"""
amnesia fuse bindings
(c) 2007 cartel research laboratories
"""
import os, sys
from errno import *
from stat import *
import fcntl
try:
import _find_fuse_parts
except ImportError:
pass
import fuse
from fuse import Fuse
from StringIO import StringIO
from amnesia import *
from getpass import getpass
import logger
from pbkdf import pbkdf
if not hasattr(fuse, '__version__'):
raise RuntimeError, \
"your fuse-py doesn't know of fuse.__version__, probably it's too old."
fuse.fuse_python_api = (0, 2)
fuse.feature_assert('stateful_files', 'has_init')
def flag2mode(flags):
md = {os.O_RDONLY: 'r', os.O_WRONLY: 'w', os.O_RDWR: 'w+'}
m = md[flags & (os.O_RDONLY | os.O_WRONLY | os.O_RDWR)]
if flags | os.O_APPEND:
m = m.replace('w', 'a', 1)
return m
class amnesiaFS(Fuse):
def __init__(self, *args, **kw):
Fuse.__init__(self, *args, **kw)
def begin(self, plaintext_keys):
global hyper_instance
logname = "/tmp/amnesia.log"
#logging.basicConfig(filename=logname, level=logging.DEBUG, filemode="w")
logger.debug("amnesia log starting")
print "mounting amnesiaFS from %s"%self.root
self.hyperblock = Hyperblock(open(self.root, "r+"))
hyper_instance = self.hyperblock
self.superblocks = self.hyperblock._superblocks
if len(plaintext_keys) == 0:
print "need at least one key"
raise SystemExit
for k in plaintext_keys:
self.hyperblock.merge(Superblock(pbkdf(k, "\xdd\x9a%.4j\x9a2\xb2\xa7\x1a\x9cnPi")))
del plaintext_keys
del k
print "total size: %s (%s blocks)"%(self.hyperblock.size, self.hyperblock.numblocks)
print "hyperblock has %s entries." %len(self.hyperblock)
print "%s keys known"%len(self.hyperblock.keys)
print "%s bytes free"%self.hyperblock.freespace()
# do stuff to set up your filesystem here, if you want
#import thread
#thread.start_new_thread(self.mythread, ())
#self.root = '/'
# def mythread(self):
#
# """
# The beauty of the FUSE python implementation is that with the python interp
# running in foreground, you can have threads
# """
# print "mythread: started"
# while 1:
# time.sleep(120)
# print "mythread: ticking"
def getattr(self, path):
print "getattr: %s"%path
print "stat: %s"%str(self.hyperblock.resolve(path).stat)
return self.hyperblock.resolve(path).stat
#return os.stat(".")
def readlink(self, path):
return self.hyperblock.resolve(path).reference
def readdir(self, path, offset):
print "readdir %s"%path
for e in self.hyperblock.ls(path):
print "got entry: %s"%e.name
yield fuse.Direntry(e.name)
def unlink(self, path):
self.hyperblock.unlink(path)
def rmdir(self, path):
self.hyperblock.unlink(path)
def symlink(self, path, path1):
tpath, terminus = os.path.split(path)
target = self.hyperblock.resolve(tpath)
target.append(Link(terminus, link=(0, path1)))
def rename(self, path, path1):
s = self.hyperblock.resolve(path)
tpath, terminus = os.path.split(path1)
s.name = terminus
def link(self, path, path1):
s = self.hyperblock.resolve(path)
tpath, terminus = os.path.split(path1)
target = self.hyperblock.resolve(tpath)
target.create(Link(terminus, link=(1, s)))
def chmod(self, path, mode):
print "changing mode: %s"%mode
self.hyperblock.resolve(path).stat.st_mode = mode
self.hyperblock.resolve(path).superblock.flush()
def chown(self, path, user, group):
s = self.hyperblock.resolve(path)
s.stat.st_uid = user
s.stat.st_gid = group
def truncate(self, path, len):
self.hyperblock.resolve(path).size = len
def mknod(self, path, mode, dev):
#os.mknod("." + path, mode, dev)
pass
def mkdir(self, path, mode):
searchpath, terminus = os.path.split(path)
print "parent resolved as: %s"%searchpath
t = self.hyperblock.resolve(searchpath)
t.superblock.mkdir(path)
t = self.hyperblock.resolve(path)
t.stat.st_mode = S_IFDIR | mode
t.superblock.flush()
def utime(self, path, times):
t = self.hyperblock.resolve(path)
print "setting time: %s, %s"%times
if times == None:
times = (time.time(), time.time())
t.stat.st_utime = times[0]
t.stat.st_mtime = times[1]
# The following utimens method would do the same as the above utime method.
# We can't make it better though as the Python stdlib doesn't know of
# subsecond preciseness in acces/modify times.
#
# def utimens(self, path, ts_acc, ts_mod):
# os.utime("." + path, (ts_acc.tv_sec, ts_mod.tv_sec))
def access(self, path, mode):
#if not os.access("." + path, mode):
# return -EACCES
pass
# This is how we could add stub extended attribute handlers...
# (We can't have ones which aptly delegate requests to the underlying fs
# because Python lacks a standard xattr interface.)
#
# def getxattr(self, path, name, size):
# val = name.swapcase() + '@' + path
# if size == 0:
# # We are asked for size of the value.
# return len(val)
# return val
#
# def listxattr(self, path, size):
# # We use the "user" namespace to please XFS utils
# aa = ["user." + a for a in ("foo", "bar")]
# if size == 0:
# # We are asked for size of the attr list, ie. joint size of attrs
# # plus null separators.
# return len("".join(aa)) + len(aa)
# return aa
def statfs(self):
"""
Should return an object with statvfs attributes (f_bsize, f_frsize...).
Eg., the return value of os.statvfs() is such a thing (since py 2.2).
If you are not reusing an existing statvfs object, start with
fuse.StatVFS(), and define the attributes.
To provide usable information (ie., you want sensible df(1)
output, you are suggested to specify the following attributes:
- f_bsize - preferred size of file blocks, in bytes
- f_frsize - fundamental size of file blcoks, in bytes
[if you have no idea, use the same as blocksize]
- f_blocks - total number of blocks in the filesystem
- f_bfree - number of free blocks
- f_files - total number of file inodes
- f_ffree - nunber of free file inodes
"""
return os.statvfs(".")
def fsinit(self):
#os.chdir(self.root)
pass
class AmnesiaFSFile(object):
def __init__(self, path, flags, *mode):
try:
self.File = hyper_instance.resolve(path)
self.file = StringIO(self.File.retr())
except IOError:
#print "flags, realmode: %s, %s, %s"%(flags, mode, flag2mode(flags))
if flag2mode(flags) in ["w", "w+", "a"]:
path, name = os.path.split(path)
if name == ".amnesiacmd":
self.File = CommandFileEntry(name)
else:
self.File = FileEntry(name)
#self.File.stat.st_mode = flags
hyper_instance.resolve(path).entries.append(self.File)
self.File.superblock = hyper_instance.resolve(path).superblock
self.File.parent = hyper_instance.resolve(path)
self.file = StringIO("")
else:
raise IOError(-2)
#self.fd = self.file.fileno()
def read(self, length, offset):
self.file.seek(offset)
return self.file.read(length)
def write(self, buf, offset):
self.file.seek(offset)
self.file.write(buf)
return len(buf)
def release(self, flags):
#self.File.writeplain(self.file.getvalue())
self.file.close()
def _fflush(self):
try:
self.file.flush()
except:
print "ohnoes"
pass
def fsync(self, isfsyncfile):
self._fflush()
#if isfsyncfile and hasattr(os, 'fdatasync'):
# os.fdatasync(self.fd)
#else:
print "key:%s"%self.File.superblock.key
self.File.store(self.file.getvalue())
def flush(self):
self._fflush()
# cf. xmp_flush() in fusexmp_fh.c
self.File.store(self.file.getvalue())
def fgetattr(self):
return self.File.stat
def ftruncate(self, len):
self.file.truncate(len)
def lock(self, cmd, owner, **kw):
# The code here is much rather just a demonstration of the locking
# API than something which actually was seen to be useful.
# Advisory file locking is pretty messy in Unix, and the Python
# interface to this doesn't make it better.
# We can't do fcntl(2)/F_GETLK from Python in a platfrom independent
# way. The following implementation *might* work under Linux.
#
# if cmd == fcntl.F_GETLK:
# import struct
#
# lockdata = struct.pack('hhQQi', kw['l_type'], os.SEEK_SET,
# kw['l_start'], kw['l_len'], kw['l_pid'])
# ld2 = fcntl.fcntl(self.fd, fcntl.F_GETLK, lockdata)
# flockfields = ('l_type', 'l_whence', 'l_start', 'l_len', 'l_pid')
# uld2 = struct.unpack('hhQQi', ld2)
# res = {}
# for i in xrange(len(uld2)):
# res[flockfields[i]] = uld2[i]
#
# return fuse.Flock(**res)
# Convert fcntl-ish lock parameters to Python's weird
# lockf(3)/flock(2) medley locking API...
#op = { fcntl.F_UNLCK : fcntl.LOCK_UN,
# fcntl.F_RDLCK : fcntl.LOCK_SH,
# fcntl.F_WRLCK : fcntl.LOCK_EX }[kw['l_type']]
#if cmd == fcntl.F_GETLK:
# return -EOPNOTSUPP
#elif cmd == fcntl.F_SETLK:
# if op != fcntl.LOCK_UN:
# op |= fcntl.LOCK_NB
#elif cmd == fcntl.F_SETLKW:
# pass
#else:
# return -EINVAL
#fcntl.lockf(self.fd, op, kw['l_start'], kw['l_len'])
pass
def main(self, *a, **kw):
self.file_class = self.AmnesiaFSFile
return Fuse.main(self, *a, **kw)
def main():
usage = """
Amnesia - a deniable filesystem.
""" + Fuse.fusage
server = amnesiaFS(version="%prog " + fuse.__version__,
usage=usage,
dash_s_do='setsingle')
server.parser.add_option(mountopt="root", metavar="DEVICE", default='/tmp/lulz',
help="mount amnesia from DEVICE [default: %default]")
server.parse(values=server, errex=1)
print server.parser
try:
if server.root:
pass
except:
print "missing root"
raise SystemExit
plaintext_keys=[]
while True:
k = getpass(prompt="enter key [none to continue]:")
if k != "":
plaintext_keys.append(k)
else:
break
logname = "/tmp/amnesia.log"
print "begin logging to %s"%logname
import logging
server.begin(plaintext_keys)
del plaintext_keys
print "server starting..."
server.main()
try:
if server.fuse_args.mount_expected():
os.chdir(server.root)
except OSError:
print >> sys.stderr, "can't enter root of underlying filesystem"
sys.exit(1)
if __name__ == '__main__':
main()