-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwm.py
executable file
·195 lines (156 loc) · 5.93 KB
/
pwm.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
#!/usr/bin/env python
import sys
import os
import re
import GnuPGInterface
import getpass
# sudo apt-get install python-gnupginterface
__version__ = "0.1"
gnupg = GnuPGInterface.GnuPG()
gnupg.options.armor = 1
gnupg.options.meta_interactive = 0
gnupg.options.extra_args.append('--no-secmem-warning')
pwd = re.compile("[Pp]assword")
color = False
def read_db(pwddb):
try:
f = open(pwddb)
except IOError:
print >>sys.stderr, "Cannot open", pwddb
sys.exit(1)
proc = gnupg.run(['--decrypt'], create_fhs=['stdout', 'passphrase'], attach_fhs={'stdin': f})
pwd = getpass.getpass("GPG Password:")
proc.handles['passphrase'].write(pwd)
proc.handles['passphrase'].close()
cleartext = proc.handles['stdout'].read()
proc.handles['stdout'].close()
proc.wait()
f.close()
#return cleartext.strip()
return cleartext
def write_db(pwddb, keyid, cleartext):
try:
f = open(pwddb, 'w')
except IOError:
print >>sys.stderr, "Cannot open", pwddb
sys.exit(1)
gnupg.options.recipients = [keyid]
proc = gnupg.run(['--encrypt'], create_fhs=['stdin'], attach_fhs={'stdout': f})
proc.handles['stdin'].write(cleartext)
proc.handles['stdin'].close()
proc.wait()
f.close()
print "Successfully wrote encrypted database"
def find(text, regex):
p = re.compile(regex)
first = True
for line in text:
if first:
captions = line.strip().split('\t')
first = False
else:
if p.search(line):
print
entry = line.strip().split('\t')
for i, cap in enumerate(captions):
if color:
if pwd.match(cap):
print cap.ljust(23), ":", "\033[0;31;41m%s\033[m" % entry[i]
else:
print cap.ljust(23), ":", "\033[1m%s\033[m" % entry[i]
else:
print cap.ljust(23), ":", entry[i]
def main():
global color
from optparse import OptionParser
parser = OptionParser(usage="%prog "+"[options] "+"[regex]...", version="%prog "+__version__)
parser.add_option("-c", "--color",
action="store_true", dest="use_colors",
help="use colors for output")
parser.add_option("-d", "--dump",
action="store_true", dest="dump_db",
help="dump database to plain text")
parser.add_option("-i", "--init",
action="store_true", dest="init_db",
help="initialize/create new database")
parser.add_option("-f", "--file", dest="pwddb",
metavar="FILE", help="use FILE as password database")
parser.add_option("-t", "--text", dest="cleartextdb",
metavar="FILE", help="generate password database from cleartext file")
parser.add_option("-k", "--key", dest="keyid", help="specify gpg key")
parser.add_option("-a", "--add", action="store_true", dest="add", help="add entry")
(options, args) = parser.parse_args()
pwddb = options.pwddb if options.pwddb else False
keyid = options.keyid if options.keyid else False
color = True if options.use_colors else False
dump = True if options.dump_db else False
init = True if options.init_db else False
add = True if options.add else False
importfile = options.cleartextdb if options.cleartextdb else False
# simple config parser
default_config = os.getenv("HOME")+"/.pwm.conf"
if not os.path.exists(default_config): # writing defaults
try:
fh = open(default_config, 'w')
except IOError:
print >>sys.stderr, "Cannot open", default_config
sys.exit(1)
print "Creating default config in:", default_config
print "Please edit values in", default_config
print "Additional help/hints can be found in the config file"
fh.write("KEYID=123456\n")
fh.write("PWDFILE=%s/.pwmdb.gpg\n" % os.getenv("HOME"))
fh.write("COLOR=False\n")
help = "# Hint: vim can edit .gpg files with the plugin from:\n"
help += "# http://www.vim.org/scripts/script.php?script_id=661"
fh.write(help)
fh.close
sys.exit(1)
else: # parsing values, commandline options overwrite them
for line in open(default_config):
if line.startswith("KEYID") and not keyid:
keyid = line.strip().split('=')[-1]
if line.startswith("PWDFILE") and not pwddb:
pwddb = line.strip().split('=')[-1]
if line.startswith("COLOR") and not color:
color = line.strip().split('=')[-1]
color = True if color == "True" else False
cmndlineoptions = add or dump or init or importfile
if len(args) == 0 and not cmndlineoptions:
print >>sys.stderr, "Specify at least one regex"
sys.exit(1)
if dump:
cleartext = read_db(pwddb)
print cleartext,
sys.exit(0)
if importfile:
try:
fh = open(importfile)
cleartext = fh.read()
fh.close()
except IOError:
print >>sys.stderr, "Cannot open", importfile
sys.exit(1)
write_db(pwddb, keyid, cleartext)
sys.exit(0)
if add and init: add = False # init wins
if add or init:
if add:
cleartext = read_db(pwddb)
text = "data"
if init:
cleartext = ""
text = "columns"
print "\nInput your %s (separated by <Tab>):" % text
if add: print cleartext.split('\n')[0]
input = raw_input()
cleartext += input + '\n'
write_db(pwddb, keyid, cleartext)
sys.exit(0)
#find regex in DB
cleartext = read_db(pwddb)
text_splitted = cleartext.split('\n')
for regex in args:
find(text_splitted, regex)
if __name__ == "__main__":
main()