forked from jseppanen/gpgfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpgfs.py
executable file
·407 lines (366 loc) · 13.1 KB
/
gpgfs.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env python
from fuse import FUSE, FuseOSError, Operations
import errno
import stat
import os
import sys
import logging
import struct
import time
from cStringIO import StringIO
import gpgstore
from contextlib import contextmanager
from threading import Lock
magic = 'GPGFS1\n'
log = logging.getLogger('gpgfs')
class Entry:
'''
Filesystem object, either file or directory.
'''
def __init__(self, **kwargs):
for k,v in kwargs.iteritems():
setattr(self, k, v)
def read_index(store, path):
if not store.exists(path):
now = time.time()
root = Entry(children={}, nlink=3, size=0,
mode=stat.S_IFDIR | 0755,
mtime=now, ctime=now)
write_index(store, path, root)
log.info('created %s', path)
return root
data = store.get(path, format=gpgstore.FMT_GPG)
buf = StringIO(data)
if buf.read(len(magic)) != magic:
raise IOError, 'index parse error: %s' % path
read_atom(buf)
root = Entry(**read_dict(buf))
return root
def write_index(store, path, root):
buf = StringIO()
buf.write(magic)
header = ''
write_atom(buf, header)
write_dict(buf, root)
store.put(buf.getvalue(), path=path, format=gpgstore.FMT_GPG)
def write_dict(fd, dct):
# breadth-first
children = []
buf = StringIO()
if not isinstance(dct, dict):
dct = dct.__dict__
for key in dct:
write_atom(buf, key.encode('utf8'))
val = dct[key]
if isinstance(val, dict):
buf.write('D')
children.append(val)
elif isinstance(val, Entry):
buf.write('E')
children.append(val)
elif isinstance(val, (int, long)):
if val < 2**32:
buf.write('I')
buf.write(struct.pack('<I', val))
else:
buf.write('L')
buf.write(struct.pack('<Q', val))
elif isinstance(val, float):
buf.write('F')
buf.write(struct.pack('<d', val))
elif isinstance(val, str):
buf.write('B')
write_atom(buf, val)
elif isinstance(val, unicode):
buf.write('S')
write_atom(buf, val.encode('utf8'))
else:
raise TypeError, type(val)
write_atom(fd, buf.getvalue())
for c in children:
write_dict(fd, c)
def read_dict(fd):
dct = {}
buf = read_atom(fd)
buflen = len(buf)
buf = StringIO(buf)
while buf.tell() < buflen:
key = read_atom(buf).decode('utf8')
tag = buf.read(1)
if tag == 'D': val = read_dict(fd)
elif tag == 'E': val = Entry(**read_dict(fd))
elif tag == 'I': val = struct.unpack('<I', buf.read(4))[0]
elif tag == 'L': val = struct.unpack('<Q', buf.read(8))[0]
elif tag == 'F': val = struct.unpack('<d', buf.read(8))[0]
elif tag == 'B': val = read_atom(buf)
elif tag == 'S': val = read_atom(buf).decode('utf8')
else: raise TypeError, tag
dct[key] = val
return dct
def write_atom(fd, atom):
assert isinstance(atom, str)
fd.write(struct.pack('<I', len(atom)))
fd.write(atom)
def read_atom(fd):
return fd.read(struct.unpack('<I', fd.read(4))[0])
class LoggingMixIn:
def __call__(self, op, path, *args):
if op=='write':
atxt = ' '.join([repr(args[0])[:10], repr(args[1]), repr(args[2])])
else:
atxt = ' '.join(map(repr, args))
log.debug('-> %s %s %s', op, repr(path), atxt)
ret = '[Unhandled Exception]'
try:
ret = getattr(self, op)(path, *args)
return ret
except OSError, e:
ret = str(e)
raise
except:
log.exception('unhandled error in %s:', op)
raise
finally:
rtxt = repr(ret)
if op=='read':
rtxt = rtxt[:10]
log.debug('<- %s %s', op, rtxt)
class GpgFs(LoggingMixIn, Operations):
#class GpgFs(Operations):
def __init__(self, encroot, keyid):
'''
:param encroot: Encrypted root directory
'''
self.encroot = encroot.rstrip('/')
assert os.path.exists(self.encroot)
assert os.path.isdir(self.encroot)
#self.cache = cache
self.store = gpgstore.GpgStore(self.encroot, keyid)
self.index_path = 'index'
self.root = read_index(self.store, self.index_path)
self.txlock = Lock()
self.fd = 0
self._clear_write_cache()
def _find(self, path, parent=False):
assert path.startswith('/')
if path == '/':
return self.root
node = self.root
path = path[1:].split('/')
if parent:
basename = path[-1]
path = path[:-1]
for name in path:
if name not in node.children:
raise FuseOSError(errno.ENOENT)
node = node.children[name]
if parent:
return node, basename
return node
def _clear_write_cache(self):
self.write_path = None
self.write_buf = []
self.write_len = 0
self.write_dirty = False
@contextmanager
def transaction(self):
paths = {'old': None, 'new': None}
def putx(data, old_path = None):
paths['new'] = self.store.put(data)
paths['old'] = old_path
return paths['new']
with self.txlock:
try:
yield putx
# commit
write_index(self.store, self.index_path, self.root)
except:
# rollback
try:
log.warning('starting rollback')
self.root = read_index(self.store, self.index_path)
if paths['new']:
self.store.delete(paths['new'])
log.warning('rollback done')
except:
log.exception('rollback failed')
raise
if paths['old']:
self.store.delete(paths['old'])
def chmod(self, path, mode):
# sanitize mode (clear setuid/gid/sticky bits)
mode &= 0777
with self.transaction():
ent = self._find(path)
ent.mode = mode | (ent.mode & 0170000)
def chown(self, path, uid, gid):
raise FuseOSError(errno.ENOSYS)
def create(self, path, mode):
mode &= 0777
mode |= stat.S_IFREG
with self.transaction() as putx:
parent, name = self._find(path, parent=True)
if name in parent.children:
raise FuseOSError(errno.EEXIST)
now = time.time()
encpath = putx('')
parent.children[name] = Entry(mode=mode, encpath=encpath, size=0,
nlink=1, ctime=now, mtime=now,
encformat=gpgstore.FMT_GPG)
parent.mtime = now
log.debug('new path %s => %s', path, encpath)
self.fd += 1
return self.fd
def flush(self, path, fh):
if not self.write_dirty:
log.debug('nothing to flush')
return 0
with self.transaction() as putx:
buf = ''.join(self.write_buf)
self.write_buf = [buf]
ent = self._find(self.write_path)
ent.size = len(buf)
ent.encpath = putx(buf, ent.encpath)
self.write_dirty = False
log.debug('flushed %d bytes to %s', len(buf), self.write_path)
return 0
def fsync(self, path, datasync, fh):
self.flush(path, fh)
return 0
def getattr(self, path, fh = None):
# don't do full blown transaction
with self.txlock:
ent = self._find(path)
return dict(st_mode = ent.mode, st_size = ent.size,
st_ctime = ent.ctime, st_mtime = ent.mtime,
st_atime = 0, st_nlink = ent.nlink)
def getxattr(self, path, name, position = 0):
raise FuseOSError(errno.ENODATA) # ENOATTR
def listxattr(self, path):
return []
def mkdir(self, path, mode):
mode &= 0777
mode |= stat.S_IFDIR
with self.transaction():
parent, name = self._find(path, parent=True)
if name in parent.children:
raise FuseOSError(errno.EEXIST)
now = time.time()
parent.children[name] = Entry(children={}, mode=mode, nlink=2,
size=0, mtime=now, ctime=now)
parent.mtime = now
def open(self, path, flags):
return 0
def read(self, path, size, offset, fh):
self.flush(path, 0)
ent = self._find(path)
assert ent.mode & stat.S_IFREG
data = self.store.get(ent.encpath, format=ent.encformat)
return data[offset:offset + size]
def readdir(self, path, fh):
dirent = self._find(path)
return ['.', '..'] + list(dirent.children)
def readlink(self, path):
raise FuseOSError(errno.ENOSYS)
def removexattr(self, path, name):
raise FuseOSError(errno.ENOSYS)
def rename(self, old, new):
self.flush(old, 0)
self._clear_write_cache()
if new.startswith(old):
raise FuseOSError(errno.EINVAL)
with self.transaction():
old_dir, old_name = self._find(old, parent=True)
if old_name not in old_dir.children:
raise FuseOSError(errno.ENOENT)
new_dir, new_name = self._find(new, parent=True)
old_ent = old_dir.children[old_name]
new_ent = new_dir.children.get(new_name)
if new_ent:
if new_ent.mode & stat.S_IFDIR:
if not old_ent.mode & stat.S_IFDIR:
raise FuseOSError(errno.EISDIR)
if new_ent.children:
raise FuseOSError(errno.ENOTEMPTY)
elif old_ent.mode & stat.S_IFDIR:
raise FuseOSError(errno.ENOTDIR)
new_dir.children[new_name] = old_dir.children.pop(old_name)
old_dir.mtime = new_dir.mtime = time.time()
if new_ent != None and new_ent.mode & stat.S_IFREG:
self.store.delete(new_ent.encpath)
def rmdir(self, path):
with self.transaction():
parent, name = self._find(path, parent=True)
if name not in parent.children:
raise FuseOSError(errno.ENOENT)
ent = parent.children[name]
if not ent.mode & stat.S_IFDIR:
raise FuseOSError(errno.ENOTDIR)
if ent.children:
raise FuseOSError(errno.ENOTEMPTY)
del parent.children[name]
parent.mtime = time.time()
def setxattr(self, path, name, value, options, position = 0):
raise FuseOSError(errno.ENOSYS)
def statfs(self, path):
raise FuseOSError(errno.ENOSYS)
def symlink(self, target, source):
raise FuseOSError(errno.ENOSYS)
def truncate(self, path, length, fh = None):
self.flush(path, 0)
self._clear_write_cache()
with self.transaction() as putx:
ent = self._find(path)
if length == 0:
buf = ''
else:
buf = self.store.get(ent.encpath, format=ent.encformat)
buf = buf[:length]
ent.encpath = putx(buf, ent.encpath)
ent.size = length
def unlink(self, path):
with self.transaction():
if self.write_path == path:
# no need to flush afterwards
self._clear_write_cache()
parent, name = self._find(path, parent=True)
if name not in parent.children:
raise FuseOSError(errno.ENOENT)
ent = parent.children.pop(name)
parent.mtime = time.time()
self.store.delete(ent.encpath)
def utimens(self, path, times = None):
if times is None:
mtime = time.time()
else:
mtime = times[1]
with self.transaction():
ent = self._find(path)
ent.mtime = mtime
def write(self, path, data, offset, fh):
if path != self.write_path:
self.flush(self.write_path, None)
ent = self._find(path)
buf = self.store.get(ent.encpath, format=ent.encformat)
self.write_buf = [buf]
self.write_len = len(buf)
self.write_path = path
if offset == self.write_len:
self.write_buf.append(data)
self.write_len += len(data)
else:
buf = ''.join(self.write_buf)
buf = buf[:offset] + data + buf[offset + len(data):]
self.write_buf = [buf]
self.write_len = len(buf)
self.write_dirty = True
return len(data)
if __name__ == '__main__':
if len(sys.argv) != 4:
sys.stderr.write('Usage: gpgfs <gpg_keyid> <encrypted_root> <mountpoint>\n')
sys.exit(1)
logpath = os.path.join(os.path.dirname(__file__), 'gpgfs.log')
log.addHandler(logging.FileHandler(logpath, 'w'))
log.setLevel(logging.DEBUG)
fs = GpgFs(sys.argv[2], sys.argv[1])
FUSE(fs, sys.argv[3], foreground=True)