-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvintypasswd.py
executable file
·136 lines (129 loc) · 4.63 KB
/
vintypasswd.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
#!/usr/bin/python
## Revision history ############################################################
__author__ = 'Wouter Eerdekens <[email protected]>'
__date__ = '2011-08-12'
__version__ = 0.1
__history__ = """
2011-08-12 - Prepare for initial release <[email protected]>
2006-07-26 - initial version.
"""
################################################################################
## Imports #####################################################################
from getpass import getpass
from optparse import OptionParser
import commands, os, os.path, re, sys, tempfile
import pki, pki.util
## Functions ###################################################################
def error(msg):
sys.stderr.write('Error: ' + msg + '\n')
sys.exit(1)
# ------------------------------------------------------------------------------
def parse_argv(argv):
keyfile = ''
usage = """%prog --set KEY
%prog --remove KEY"""
version = '%prog ' + __version__
parser = OptionParser(usage=usage, version=version)
parser.add_option('--set', action='store_true', default=False,
metavar="KEY", help='add a passphrase to KEY')
parser.add_option('--remove', action='store_true', default=False,
metavar="KEY", help='remove the passphrase from KEY')
opt, arg = parser.parse_args(argv)
if opt.set and opt.remove:
parser.error('--set and --remove are mutually exclusive')
elif opt.set or opt.remove:
if len(arg) == 1:
keyfile = arg[0]
else:
parser.error('no keyfile given')
else:
parser.print_help()
sys.exit(1)
return opt, keyfile
# ------------------------------------------------------------------------------
def remove_passphrase(config, keyfile, remove_only=True):
if remove_only:
passwd = getpass()
else:
passwd = getpass('Old password: ')
tmpfile = keyfile + '.tmp'
try:
fd, fname = tempfile.mkstemp()
os.write(fd, passwd)
os.close(fd)
except:
sys.stderr.write('Problem creating secure temporary file...\n')
sys.exit(1)
else:
cmd = pki.OPENSSL_COMMANDS['del_passphrase'] \
% { 'openssl' : config.get('openssl', 'path'),
'pwfile' : fname,
'inkey' : keyfile,
'outkey' : tmpfile
}
status, output = commands.getstatusoutput(cmd)
os.remove(fname)
if status:
sys.stderr.write('Problem removing old passphrase:\n' + output)
sys.exit(1)
else:
try:
os.remove(keyfile)
os.rename(tmpfile, keyfile)
except:
sys.stderr.write('Problem storing new keyfile.\n')
# ------------------------------------------------------------------------------
def set_passphrase(config, keyfile):
if pki.util.is_encrypted(keyfile):
remove_passphrase(config, keyfile, remove_only=False)
pw1 = getpass()
pw2 = getpass('Retype password: ')
tmpfile = keyfile + '.tmp'
if pw1 != pw2:
sys.stderr.write('Password mismatch.\n')
sys.exit(1)
else:
try:
fd, fname = tempfile.mkstemp()
os.write(fd, pw1)
os.close(fd)
except:
sys.stderr.write('Problem creating secure temporary file...\n')
sys.exit(1)
else:
cmd = pki.OPENSSL_COMMANDS['set_passphrase'] \
% { 'openssl' : config.get('openssl', 'path'),
'pwfile' : fname,
'inkey' : keyfile,
'outkey' : tmpfile
}
status, output = commands.getstatusoutput(cmd)
os.remove(fname)
if status:
sys.stderr.write('Problem setting passphrase:\n' + output)
sys.exit(1)
else:
try:
os.remove(keyfile)
os.rename(tmpfile, keyfile)
except:
sys.stderr.write('Problem storing new keyfile.\n')
## We're being called as a script ##############################################
if __name__ == '__main__':
opt, keyfile = parse_argv(sys.argv[1:])
config = pki.util.parse_config()
if not os.path.isfile(keyfile):
error('Unable to open ' + keyfile + '.\n')
else:
if pki.util.is_private_key(keyfile):
if opt.set:
print 'Setting passphrase on ' + keyfile
set_passphrase(config, keyfile)
else:
print 'Removing passphrase from ' + keyfile
if pki.util.is_encrypted(keyfile):
remove_passphrase(config, keyfile)
else:
error(keyfile + ' has no passphrase set.')
else:
error(keyfile + ' is not a private key.')