-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtruecrypt.py
74 lines (62 loc) · 1.95 KB
/
truecrypt.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
#!/bin/python
#!/usr/bin/env python3
#
# exitcode = 0 - For password OK, initial OK
# exitcode = 1 - For password not OK, or initial not OK
# exitcode >= 2 - For fatal error, or initial not OK
#
import os
import sys
import argparse
import logging
import subprocess
log=logging.getLogger(__name__)
def do_initialize(cfg):
log.info("Unmouting drive %s...", cfg.letter)
ret = subprocess.call(["truecrypt",
'/s', '/q',
'/d', '/l', cfg.letter,
])
if ret !=0:
log.error("Unable to execute truecrypt with error code of %s", ret)
sys.exit(1)
if os.path.isdir(cfg.letter + ":\\"):
log.error("Unable to unmount drive %s", cfg.letter)
sys.exit(1)
def do_checkpass(cfg):
""" Check password """
ret = subprocess.call(['truecrypt',
'/s', '/q',
'/v', cfg.volume,
'/l', cfg.letter,
'/p', cfg.password,
])
if ret !=0:
log.warn("truecrypt return %s", ret)
sys.exit(1)
if not os.path.isdir(cfg.letter + ":\\"):
sys.exit(1)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('password', help="path to a TrueCrypt volume"
, nargs='?')
parser.add_argument("-l", '--letter'
, help="driver letter to mount the volume as (X for example)."
, required=True )
parser.add_argument("-v", '--volume'
, help="path to a TrueCrypt volume, such as \Device\Harddisk1\Partition1"
, required=True )
parser.add_argument("-c", '--checkdrive'
, help="check driver letter that should always exist")
args = parser.parse_args()
if not os.path.isdir(args.checkdrive + ":\\"):
log.error("Cannot found drive %s", args.checkdrive)
sys.exit(2)
if args.password is None:
return do_initialize(args)
else:
return do_checkpass(args)
if __name__ == "__main__":
FORMAT= '%(asctime)s [%(filename)15s:%(lineno)-4d] %(levelname)7s - %(message)s'
logging.basicConfig(format=FORMAT, level=logging.WARN)
main()