5 from catacomb
.pwsafe
import *
8 from sys
import argv
, exit
, stdin
, stdout
, stderr
9 from getopt
import getopt
, GetoptError
10 from fnmatch
import fnmatch
12 if 'PWSAFE' in environ
:
13 file = environ
['PWSAFE']
15 file = '%s/.pwsafe' % environ
['HOME']
18 cipher
= 'blowfish-cbc'
22 opts
, args
= getopt(av
, 'c:h:m:', ['cipher=', 'mac=', 'hash='])
26 if o
in ('-c', '--cipher'):
28 elif o
in ('-m', '--mac'):
30 elif o
in ('-h', '--hash'):
40 db
= G
.open(file, 'n', 0600)
41 pp
= C
.ppread(tag
, C
.PMODE_VERIFY
)
42 if not mac
: mac
= hash + '-hmac'
43 c
= C
.gcciphers
[cipher
]
46 ppk
= PW
.PPK(pp
, c
, h
, m
)
47 ck
= C
.rand
.block(c
.keysz
.default
)
48 mk
= C
.rand
.block(m
.keysz
.default
)
49 k
= Crypto(c
, h
, m
, ck
, mk
)
55 db
['key'] = ppk
.encrypt(wrapstr(ck
) + wrapstr(mk
))
56 db
['magic'] = k
.encrypt(C
.rand
.block(h
.hashsz
))
59 if len(pp
) > 0 and pp
[-1] == '\n':
76 if len(av
) < 1 or len(av
) > 2:
80 pp
= C
.getpass("Enter passphrase `%s': " % tag
)
81 vpp
= C
.getpass("Confirm passphrase `%s': " % tag
)
83 raise ValueError, "passphrases don't match"
92 if len(av
) < 1 or len(av
) > 2:
95 pw_out
= PW(av
[0], 'w')
101 if pat
is None or fnmatch(k
, pat
):
113 if pat
is None or fnmatch(k
, pat
):
123 pix
.set(tag
, pw
[tag
])
130 pix
.set(pptag
, pw
[tag
])
141 if ch
< ' ' or ch
> '~': return False
144 if asciip(s
): return s
145 return C
.ByteString(s
)
147 db
= gdbm
.open(file, 'r')
151 print '%r: %r' %
(present(k
), present(db
[k
]))
154 commands
= { 'create': [cmd_create
,
155 '[-c CIPHER] [-h HASH] [-m MAC] [PP-TAG]'],
156 'find' : [cmd_find
, 'LABEL'],
157 'store' : [cmd_store
, 'LABEL [VALUE]'],
158 'list' : [cmd_list
, '[GLOB-PATTERN]'],
159 'changepp' : [cmd_changepp
, ''],
160 'copy' : [cmd_copy
, 'DEST-FILE [GLOB-PATTERN]'],
161 'to-pixie' : [cmd_topixie
, '[TAG [PIXIE-TAG]]'],
162 'delete' : [cmd_del
, 'TAG'],
163 'dump' : [cmd_dump
, '']}
168 print >>fp
, 'Usage: pwsafe COMMAND [ARGS...]'
174 Maintains passwords or other short secrets securely.
178 -h, --help Show this help text.
179 -v, --version Show program version number.
180 -u, --usage Show short usage message.
185 print '%s %s' %
(c
, commands
[c
][1])
188 opts
, argv
= getopt(argv
[1:],
190 ['help', 'version', 'usage', 'file='])
195 if o
in ('-h', '--help'):
198 elif o
in ('-v', '--version'):
201 elif o
in ('-u', '--usage'):
204 elif o
in ('-f', '--file'):
212 if argv
[0] in commands
:
217 if commands
[c
][0](argv
):
218 print >>stderr
, 'Usage: pwsafe %s %s' %
(c
, commands
[c
][1])