-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_shell.py
executable file
·143 lines (109 loc) · 4.1 KB
/
admin_shell.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
#!/bin/env python3
import base64
import cmd
import inspect
import json
import os.path
import shlex
from itertools import zip_longest
from server import Server
from user import User, UserStore
def printable(v):
return v >= 32 and v <= 126
def print_raw_output(data):
for i in range(0, len(data), 16):
line = data[i:i+16]
line_hex = [ '{:02x}'.format(x) for x in line ] + [' '] * (16 - len(line))
print('{:08x}: {} {}'.format(
i,
' '.join(x + y for x, y in zip(line_hex[::2], line_hex[1::2])),
''.join(chr(x) if printable(x) else '.' for x in line)))
class EFSRepl(cmd.Cmd):
intro = 'Welcome to the EFS REPL. Type help or ? for command help.'
prompt = 'EFS> '
def __init__(self, path, user_path):
super().__init__()
self.server = Server(path)
self.user_store = UserStore(user_path)
def do_help(self, argline):
print('Welcome to the EFS REPL. Type any of these commands to get')
print('more detailed command information.')
for k, v in inspect.getmembers(self, predicate=inspect.ismethod):
if k.startswith('do_'):
name = k[3:]
if name not in [ 'EOF' ]:
if v.__doc__ is None:
doc = '<no docstring>'
else:
lines = [ l.strip() for l in v.__doc__.split('\n') ]
doc = '\n'.join([lines[0]] +
[ '{}{}'.format(' ' * (2 + 12 + 3), l) for l in lines[1:]])
print(' {:>12} - {}'.format(name, doc))
def do_raw_read(self, argline):
""" Reads the raw contents associated with the provided filename. """
args = shlex.split(argline)
if len(args) != 1:
print('usage: raw_read <filename>')
elif not self.server.has_file(args[0]):
print('{}: file not found'.format(args[0]))
else:
print_raw_output(self.server.read_file(args[0]))
def do_raw_set(self, argline):
""" Sets the raw contents associated with the provided filename. """
args = shlex.split(argline)
if len(args) != 2:
print('usage: raw_put <filename> <data>')
else:
self.server.write_file(args[0], args[1].encode('latin-1'))
def do_raw_put(self, argline):
""" Sets the raw contents associated with the provided filename to
be the content of the provided file. """
args = shlex.split(argline)
if len(args) != 2:
print('usage: raw_put <filename> <source>')
elif not os.path.isfile(args[1]):
print('{}: file not found'.format(args[1]))
else:
with open(args[1], 'rb') as f:
self.server.write_file(args[0], f.read())
def do_raw_cp(self, argline):
args = shlex.split(argline)
if len(args) != 2:
print('usage: raw_cp <source> <dest>')
else:
data = self.server.read_file(args[0])
self.server.write_file(args[1], data)
def do_rm(self, argline):
""" Deletes the provided file. """
args = shlex.split(argline)
for arg in args:
if not self.server.has_file(arg):
print('{}: file not found'.format(arg))
else:
self.server.remove_file(arg)
def do_user_new(self, argline):
args = shlex.split(argline)
if len(args) != 1:
print('usage: user_new <name>')
else:
self.user_store.add_user(args[0], User.generate())
def do_exit(self, arg):
print('goodbye')
return True
do_EOF = do_exit
if __name__ == '__main__':
import sys
if len(sys.argv) != 3:
print('usage: {} <fs> <user_store>'.format(sys.argv[0]))
sys.exit(1)
while True:
repl = EFSRepl(sys.argv[1], sys.argv[2])
try:
repl.cmdloop()
break
except:
import traceback
traceback.print_exc()
print()
print('**Caught exception. Restarting.**')
print()