This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.py
138 lines (103 loc) · 3.58 KB
/
load.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
#!/usr/bin/python
#
# . [IN USB]
# crypto.fs
# lost+found/
# backup_dev.rip
from cmtab import cmtab
from os import path, system, getegid, mkdir, remove, walk, removedirs, getlogin
from sys import argv
try:
from utils import move_file, copy_file, sys
except:
print(argv[0] + ' m [Mount to] | Mount Encrypted drive\n' + argv[0] + ' u | umount encrypted drive')
#print('Needs root privs')
exit(6)
TARGET = path.dirname(path.abspath(__file__))
CMTAB_FP = '/etc/cryptmount/cmtab'
ENCRYPTED_DEV = path.join(TARGET, 'crypto.fs')
INFO_FILE = path.join(TARGET, 'backup_dev.rip')
try:
assert getegid() == 0
except AssertionError:
print('Needs root privleges.')
exit(2)
with open(INFO_FILE) as f:
NAME = f.read().strip(' \n')
MOUNTED_TO = None
try:
assert path.isfile(INFO_FILE)
except AssertionError:
print('Needs backup_dev.rip info file | Otherwise not a BACKUP_DEV')
exit(1)
if path.isfile(path.join(TARGET, 'tempSave.tmp')):
with open(path.join(TARGET, 'tempSave.tmp')) as f:
MOUNTED_TO = f.read().strip(' \n')
def mount(mountTo):
try:
assert path.isdir('/etc/cryptmount/keys/')
except Exception as e:
print('You might need to install the actual program and convert this USB to load it.')
exit()
key_fp = path.join('/etc/cryptmount/keys/', NAME+'.key')
try:
assert path.isfile(key_fp)
except Exception as e:
print('No key Found.')
raise e
if not path.isdir(mountTo):
mkdir(mountTo)
copy_file(CMTAB_FP, path.join(TARGET, 'cmtab.bak'))
system('echo "%s" > /etc/cryptmount/cmtab' % cmtab(mountTo, ENCRYPTED_DEV, NAME, key_fp).make())
system('chown -R %s:%s %s' % (getlogin(), getlogin(), mountTo))
system('chmod 0700 %s' % mountTo)
with open(path.join(TARGET, 'tempSave.tmp'), 'w') as f:
f.write(mountTo)
assert system('cryptmount '+NAME) == 0
return True
def umount():
if not path.isfile(CMTAB_FP):
print('Fatal Error of losing cmtab - Attempting recovery...')
try:
assert system('echo "%s" > /etc/cryptmount/cmtab' % cmtab(
MOUNTED_TO, ENCRYPTED_DEV, NAME,
path.join('/etc/cryptmount/keys/', NAME+'.key')
).make()) == 0
except AssertionError:
raise AssertionError('Could not umount encrypted fs')
mountData = sys('cryptmount -u ' + NAME + ' && sync')
if 'is not recognized' in mountData:
assert system('umount /dev/mapper/'+NAME) == 0
else: print mountData
del mountData
print('unmounted '+NAME+' from '+str(MOUNTED_TO))
print('Cleaning up...')
data = []
for root, dirs, files in walk(MOUNTED_TO):
data.extend(files)
data.extend(dirs)
break
if len(data) == 0:
removedirs(MOUNTED_TO)
if path.isfile(path.join(TARGET, 'tempSave.tmp')):
remove(path.join(TARGET, 'tempSave.tmp'))
try:
if path.isfile(path.join(TARGET, 'cmtab.bak')):
move_file(path.join(TARGET, 'cmtab.bak'), CMTAB_FP, True)
except:
raise OSError('Couldn\'t write original cmtab into place, manually move it.')
print('done.')
if __name__ == '__main__':
try:
action = argv[1]
if action == 'm':
mount(argv[2])
elif action == 'u':
umount()
elif action == '--help' or action == '-h':
print(argv[0]+' m [Mount to] | Mount Encrypted drive\n'+argv[0]+' u | umount encrypted drive')
else:
print(argv[0] + ' m [Mount to] | Mount Encrypted drive\n' + argv[0] + ' u | umount encrypted drive')
except Exception as e:
print e.message
print(argv[0] + ' m [Mount to] | Mount Encrypted drive\n' + argv[0] + ' u | umount encrypted drive')