forked from jridgewell/Unlock
-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.py
executable file
·70 lines (55 loc) · 2.14 KB
/
install.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
#!/usr/bin/env python3
"""
Installer of Unlock, a daemon to decrypt CoreStorage (HFS+) and APFS volumes automatically when macOS starts.
"""
import sys
import os
import pathlib
import shutil
import argparse
script_name = "com.juanjonol.unlock.py"
plist_name = "com.juanjonol.unlock.plist"
script_folder = "/Library/PrivilegedHelperTools/"
plist_folder = "/Library/LaunchDaemons/"
passwords_folder = "Generated_Files/"
# Parse the arguments given by the user.
def parse_args():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--version', action='version', version='2.2.2')
parser.add_argument("-u", "--uninstall", help='Uninstall Unlock.', action='store_true')
return parser.parse_args()
def main(argv=None):
if not sys.platform == 'darwin':
raise NotImplementedError("This program only works in OS X")
if os.getuid() != 0:
raise PermissionError("This program must be executed as root.")
args = parse_args()
if args.uninstall == True:
uninstaller()
else:
installer(argv)
def installer(argv):
# Generates /Library/PrivilegedHelperTools, owned by root
script_folder_path = pathlib.Path(script_folder)
if not script_folder_path.is_dir():
script_folder_path.mkdir()
os.chown(script_folder, 0, 0)
folder_path = pathlib.Path(argv[0]).parent
# Copies the script to /Library/PrivilegedHelperTools, being only writable by root.
script_current_path = pathlib.Path(str(folder_path) + "/" + script_name)
script_new = shutil.copy(str(script_current_path), script_folder)
os.chown(script_new, 0, 0)
os.chmod(script_new, 0o755)
# Copies the LaunchDaemon plist to /Library/LaunchDaemon, being only writable by root.
plist_current_path = pathlib.Path(str(folder_path) + "/" + plist_name)
plist_new = shutil.copy(str(plist_current_path), plist_folder)
os.chown(plist_new, 0, 0)
os.chmod(plist_new, 0o644)
print('Installation finished.')
print('To add a disk, use the following command: "%s%s add"' % (script_folder, script_name))
def uninstaller():
os.remove(plist_folder + plist_name)
os.remove(script_folder + script_name)
shutil.rmtree(script_folder + passwords_folder)
if __name__ == '__main__':
sys.exit(main(sys.argv))