-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcmount.py
executable file
·282 lines (253 loc) · 10.8 KB
/
tcmount.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
#!/usr/bin/env python
########################################################################
# tcmount.py: TrueCrypt/VeraCrypt Device Mounter
#
# Description:
# This script is designed to automate the mounting and unmounting of
# TrueCrypt and VeraCrypt encrypted devices. It checks for the presence
# of the TrueCrypt and VeraCrypt commands and supports a variety of devices,
# including options for different file systems and encoding types. This version
# allows for specific device mounting and unmounting by specifying the device
# name as an argument and choosing between TrueCrypt and VeraCrypt.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: LGPLv3 (Details: https://www.gnu.org/licenses/lgpl-3.0.html)
# Contact: [email protected]
#
# Version History:
# v4.3 2025-03-05
# Added sudo privilege check when --sudo option is specified.
# v4.2 2024-01-26
# Updated documentation to include notes on custom return codes.
# v4.1 2023-12-17
# Modified is_truecrypt_installed and is_veracrypt_installed functions for compatibility
# with Python versions below 3.3, replacing DEVNULL with os.devnull.
# v4.0 2023-12-15
# Added support for VeraCrypt with the -v (--veracrypt) and -t (--tc-compat) options.
# Improved error handling for systems where only TrueCrypt or VeraCrypt is installed.
# Reversed the behavior of the -u (--utf8) option. Now, by default,
# the filesystem is mounted with UTF-8 encoding, and the -u option
# is used to disable this setting.
# Refactored command construction to improve testability.
# Renamed the -e (--expansion) option to -e (--external) and updated the path to
# the container file to '~/mnt/external/container.tc' for generalizing external HDD support.
# [Further version history truncated for brevity]
# v1.0 2010-08-06
# First release.
#
# Usage:
# To use this script, ensure you have TrueCrypt or VeraCrypt installed and
# run the script with appropriate privileges. You can specify the device
# and other mount options as arguments. For example:
#
# python tcmount.py [device] [options]
#
# Specific device mounting:
# python tcmount.py sdb
# This will mount the device /dev/sdb using TrueCrypt or VeraCrypt based on the options.
#
# Specific device unmounting:
# python tcmount.py sdb unmount
# python tcmount.py sdb umount
# These commands will unmount the device /dev/sdb using TrueCrypt or VeraCrypt.
#
# Options:
# -v, --veracrypt Use VeraCrypt instead of TrueCrypt for mounting and unmounting.
# -t, --tc-compat Use VeraCrypt in TrueCrypt compatibility mode.
# -u, --no-utf8 Do not use UTF-8 encoding for the mounted filesystem.
# -r, --readonly Mount the filesystem in read-only mode.
# -a, --all Mount all available devices.
# -e, --external Mount the container file of an external drive to a specified device.
#
# Note on Custom Return Codes:
# This script uses custom return codes to indicate specific error conditions:
# - 0: Success. The operation completed without any errors.
# - 1: Neither TrueCrypt nor VeraCrypt is installed. The script requires one of them to be installed to function.
# - 11: TrueCrypt is not installed. This is returned when TrueCrypt is required but not found.
# - 12: VeraCrypt is not installed but specified for use. This occurs when VeraCrypt is selected but not installed.
# - 13: VeraCrypt compatibility mode is specified but VeraCrypt is not installed.
#
# Refer to the TrueCrypt and VeraCrypt documentation for more detailed information
# on mount options and device specifications.
#
########################################################################
import os
import subprocess
import sys
from optparse import OptionParser
def check_sudo():
"""Check if the user has sudo privileges (password may be required)."""
try:
with open(os.devnull, 'w') as devnull:
result = subprocess.call(["sudo", "-v"], stdout=devnull, stderr=devnull)
if result != 0:
print("Error: This script requires sudo privileges. Please run as a user with sudo access.", file=sys.stderr)
sys.exit(1)
except Exception as e:
print("Error: Failed to check sudo privileges: {}".format(e), file=sys.stderr)
sys.exit(1)
def os_exec(cmd):
"""
Executes a system command using subprocess.
"""
subprocess.call(cmd, shell=True)
def is_truecrypt_installed():
"""
Checks if TrueCrypt is installed by searching for its command in the system path.
"""
with open(os.devnull, 'w') as devnull:
return subprocess.call(['which', 'truecrypt'], stdout=devnull, stderr=devnull) == 0
def is_veracrypt_installed():
"""
Checks if VeraCrypt is installed by searching for its command in the system path.
"""
with open(os.devnull, 'w') as devnull:
return subprocess.call(['which', 'veracrypt'], stdout=devnull, stderr=devnull) == 0
def get_truecrypt_version():
"""
Retrieves the version information of TrueCrypt.
"""
try:
output = subprocess.check_output(
["truecrypt", "--version"], stderr=subprocess.STDOUT)
return output.decode().strip()
except subprocess.CalledProcessError:
return "Unknown"
def get_veracrypt_version():
"""
Retrieves the version information of VeraCrypt.
"""
try:
output = subprocess.check_output(
["veracrypt", "--version"], stderr=subprocess.STDOUT)
return output.decode().strip()
except subprocess.CalledProcessError:
return "Unknown"
def build_mount_command(device, mount_options):
"""
Builds the command to mount a TrueCrypt volume on a specific drive.
"""
return 'test -b /dev/{0} && sudo truecrypt -t -k "" --protect-hidden=no --fs-options={1} /dev/{0} ~/mnt/{0}'.format(device, mount_options)
def build_unmount_command(device):
"""
Builds the command to unmount a specified device.
"""
return 'sudo truecrypt -d ~/mnt/{0}'.format(device)
def build_mount_all_command(mount_options):
"""
Builds the commands to mount all devices from sdc to sdz.
"""
commands = []
for device_suffix in range(ord('c'), ord('z') + 1):
commands.append(build_mount_command(
'sd' + chr(device_suffix), mount_options))
return commands
def build_mount_external_command(device, mount_options):
"""
Builds the command to mount the container file of external to the specified device.
"""
external_file = '~/mnt/external/container.tc'
mount_point = os.path.join('~/mnt', device)
return 'test -f {0} && sudo truecrypt -t -k "" --protect-hidden=no --fs-options={1} {0} {2}'.format(external_file, mount_options, mount_point)
def process_mounting(options, args):
"""
Processes the mounting based on the provided options and arguments.
"""
mount_options = []
if not options.no_utf8:
mount_options.append('utf8')
if options.readonly:
mount_options.append('ro')
mount_options_str = ','.join(mount_options)
commands = []
if options.external:
cmd = build_mount_external_command(
options.external, mount_options_str)
if cmd:
commands.append(cmd)
else:
if args:
device = args[0]
if len(args) > 1 and args[1] in ['unmount', 'umount']:
commands.append(build_unmount_command(device))
else:
commands.append(build_mount_command(device, mount_options_str))
else:
commands.append(build_mount_command('sdb', mount_options_str))
if options.all:
commands.extend(build_mount_all_command(mount_options_str))
if options.tc_compat:
if not is_veracrypt_installed():
print("Error: VeraCrypt is not installed, but '-t' option was specified. Please use TrueCrypt or install VeraCrypt and try again.")
sys.exit(13)
encryption_tool = "veracrypt -tc"
unmount_cmd = "veracrypt"
elif options.veracrypt:
if not is_veracrypt_installed():
print("Error: VeraCrypt is not installed, but '-v' option was specified. Please use TrueCrypt or install VeraCrypt and try again.")
sys.exit(12)
encryption_tool = "veracrypt"
unmount_cmd = "veracrypt"
else:
if not is_truecrypt_installed():
print(
"Error: TrueCrypt is not installed. Please use VeraCrypt or install TrueCrypt and try again.")
sys.exit(11)
encryption_tool = "truecrypt"
unmount_cmd = "truecrypt"
for cmd in commands:
if 'unmount' in cmd or 'umount' in cmd:
cmd = cmd.replace('truecrypt', unmount_cmd)
else:
cmd = cmd.replace('truecrypt', encryption_tool)
os_exec(cmd)
def main():
"""
Main function to handle the mounting process based on user inputs.
"""
tcmount_version = "4.3"
versions = []
if is_truecrypt_installed():
versions.append(get_truecrypt_version())
if is_veracrypt_installed():
versions.append(get_veracrypt_version())
if not versions:
print("Error: Neither TrueCrypt nor VeraCrypt is installed. Please install one of them and try again.")
sys.exit(1)
version_message = "tcmount.py {} - This script operates with {}.".format(
tcmount_version, " / ".join(versions))
parser = OptionParser(version=version_message)
parser.add_option("-v", "--veracrypt",
dest="veracrypt",
help="use VeraCrypt instead of TrueCrypt",
action="store_true")
parser.add_option("-t", "--tc-compat",
dest="tc_compat",
help="use VeraCrypt in TrueCrypt compatibility mode",
action="store_true")
parser.add_option("-u", "--no-utf8",
dest="no_utf8",
help="do not use UTF-8 as the mount filesystem type",
action="store_true")
parser.add_option("-r", "--readonly",
dest="readonly",
help="mount filesystem as read-only",
action="store_true")
parser.add_option("-a", "--all",
dest="all",
help="mount all available devices",
action="store_true")
parser.add_option("-e", "--external",
dest="external",
help="mount the specified device with an external drive",
action="store",
type="string")
(options, args) = parser.parse_args()
if "-h" in sys.argv or "--help" in sys.argv or "-V" in sys.argv or "--version" in sys.argv:
parser.print_help()
sys.exit(0)
check_sudo()
process_mounting(options, args)
if __name__ == "__main__":
main()