forked from RF3/VMwareVMX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
221 lines (190 loc) · 7.15 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# main.py
#
version = '1.0.0'
import getopt
from getpass import getpass
import os
import re
import sys
from vmwarevmx import VMwareVMX
def initgetopt(name, options, files=''):
maxlen = max(len(long)
for (short, long, arg, text) in options)
optlong = list(long + '=' if arg else long
for (short, long, arg, text) in options)
optshort = ''.join(short + ':' if arg else short
for (short, long, arg, text) in options)
withoutarg = ''.join(short
for (short, long, arg, text) in options if not arg)
witharg = ''.join('[-{s} {a}] '.format(s=short, a=arg)
for (short, long, arg, text) in options if arg)
usage = '\n'.join(' -{s}, --{l:{len}s} {t}' \
.format(s=short, l=long, t=text, len=maxlen)
for (short, long, arg, text) in options)
usage = 'Usage: {n} [-{wo}] {w}{f}\n{u}' \
.format(n=name, wo=withoutarg, w=witharg, f=files, u=usage)
return optshort, optlong, usage
def main(argv):
add = False
addfilename = None
config = None
decrypt = False
displayname = None
encrypt = False
force = False
new = False
outfilename = None
password = None
remove = False
removefilename = None
options = [
('a', 'add', 'file',
'decrypt, add line(s) and encrypt in_file'),
('d', 'decrypt', '',
'decrypt in_file (default)'),
('D', 'displayname', 'name',
'set the displayname for encrypted configuration'),
('e', 'encrypt', '',
'encrypt in_file'),
('f', 'force', '',
'force overwriting out_file'),
('h', 'help', '',
'display this message'),
('n', 'new', '',
'after decrypt, use new parameters for encrypt'),
('p', 'password', 'password',
'set the password (default: ask for it)'),
('r', 'remove', 'file',
'decrypt, remove line(s) and encrypt in_file'),
('v', 'version', '',
'print the version string and exit'),
]
(optshort, optlong, usage) = initgetopt(argv[0], options,
'in_file [out_file]')
try:
(opts, args) = getopt.getopt(argv[1:], optshort, optlong)
except getopt.GetoptError as err:
sys.stderr.write(str(err) + '\n')
sys.exit(usage)
for (opt, arg) in opts:
if opt in ('-a', '--add'):
addfilename = arg
add, decrypt, encrypt = True, True, True
elif opt in ('-d', '--decrypt'):
decrypt = True
elif opt in ('-D', '--displayname'):
displayname = arg
elif opt in ('-e', '--encrypt'):
encrypt = True
elif opt in ('-f', '--force'):
force = True
elif opt in ('-h', '--help'):
print(usage)
sys.exit(0)
elif opt in ('-n', '--new'):
new = True
elif opt in ('-p', '--password'):
password = arg
elif opt in ('-r', '--remove'):
removefilename = arg
remove, decrypt, encrypt = True, True, True
elif opt in ('-v', '--version'):
print('VMwareVMX Crypto Tool v{}\n' \
'Copyright (C) 2018 Robert Federle'.format(version))
sys.exit(0)
else:
sys.exit(usage)
if len(args) == 0:
sys.exit('Error: No input file given')
if decrypt is False and encrypt is False:
decrypt = True
# Read the configuration file
infilename = args[0]
if len(args) == 2:
outfilename = args[1]
if os.path.abspath(infilename) == os.path.abspath(outfilename):
sys.exit('Error: Input and output files are the same')
elif len(args) > 2:
sys.exit('Error: More arguments after filenames found')
if password is None:
try:
password = getpass('Password:')
except (EOFError, KeyboardInterrupt):
sys.exit('\nError: Need a password')
if password == '':
sys.exit('Error: Empty password not allowed')
try:
with open(infilename, "r") as infile:
lines = infile.readlines()
except (OSError, IOError):
sys.exit('Error: Cannot read from file ' + infilename)
VMX = VMwareVMX.new()
# Decrypt the configuration
if decrypt:
keysafe = None
data = None
for line in lines:
if displayname is None:
match = re.match('displayName *= *"(.+)"\n', line)
if match:
displayname = match.group(1)
if 'encryption.keySafe' in line:
keysafe = line
elif 'encryption.data' in line:
data = line
if displayname is None or keysafe is None or data is None:
sys.exit('Error: File ' + infilename + ' is not a valid VMX file')
try:
config = VMX.decrypt(password, keysafe, data)
except ValueError as err:
sys.exit('Error: ' + str(err))
if config is None:
sys.exit('Error: Password is invalid')
# Remove lines from the configuration
if remove:
try:
removelist = open(removefilename, "r").read().split('\n')
except (OSError, IOError):
sys.exit('Error: Cannot read from file ' + removefilename)
lines = config.split('\n')
config = '\n'.join([x for x in lines if x not in removelist]) + '\n'
# Add lines to the configuration
if add:
try:
addconfig = open(addfilename, "r").read()
except (OSError, IOError):
sys.exit('Error: Cannot read from file ' + addfilename)
config += addconfig
# Use new parameters (identifier, salt, AES IV, AES keys) for encryption?
if new:
VMX.reinit()
# Encrypt the configuration
if encrypt:
if displayname is None:
sys.exit('Error: Displayname is missing')
if config is None:
config = ''.join(lines)
try:
(keysafe, data) = VMX.encrypt(password, config)
except ValueError as err:
sys.exit('Error: '+ str(err))
config = '.encoding = "UTF-8"\ndisplayName = "{n}"\n{k}\n{d}\n' \
.format(n=displayname, k=keysafe, d=data)
# Write to the configuration file or to stdout
if outfilename is None or outfilename == '-':
sys.stdout.write(config)
else:
if force is False and os.path.isfile(outfilename):
sys.exit('Error: File ' + outfilename + ' exists; ' \
'use --force to overwrite')
try:
open(outfilename, "w").write(config)
except (OSError, IOError):
sys.exit('Error: Cannot write to file ' + outfilename)
sys.exit(0)
if __name__ == '__main__':
main(sys.argv)
# vim:set ts=4 sw=4 sts=4 expandtab: