4 ### Tool for maintaining a secure-ish password database
6 ### (c) 2005 Straylight/Edgeware
9 ###----- Licensing notice ---------------------------------------------------
11 ### This file is part of the Python interface to Catacomb.
13 ### Catacomb/Python is free software; you can redistribute it and/or modify
14 ### it under the terms of the GNU General Public License as published by
15 ### the Free Software Foundation; either version 2 of the License, or
16 ### (at your option) any later version.
18 ### Catacomb/Python is distributed in the hope that it will be useful,
19 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ### GNU General Public License for more details.
23 ### You should have received a copy of the GNU General Public License
24 ### along with Catacomb/Python; if not, write to the Free Software Foundation,
25 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ###--------------------------------------------------------------------------
30 from __future__
import with_statement
32 from os
import environ
33 from sys
import argv
, exit
, stdin
, stdout
, stderr
34 from getopt
import getopt
, GetoptError
35 from fnmatch
import fnmatch
39 from catacomb
.pwsafe
import *
41 ###--------------------------------------------------------------------------
42 ### Python version portability.
44 def _text(bin
): return bin
45 def _bin(text
): return text
47 def _excval(): return SYS
.exc_info()[1]
49 ###--------------------------------------------------------------------------
53 prog
= re
.sub(r
'^.*[/\\]', '', argv
[0])
56 """Issue a warning message MSG."""
57 stderr
.write('%s: %s\n' %
(prog
, msg
))
60 """Report MSG as a fatal error, and exit."""
64 ###--------------------------------------------------------------------------
65 ### Subcommand implementations.
69 ## Default crypto-primitive selections.
70 cipher
= 'rijndael-cbc'
76 opts
, args
= getopt(av
, 'c:d:h:m:',
77 ['cipher=', 'database=', 'mac=', 'hash='])
82 if o
in ('-c', '--cipher'): cipher
= a
83 elif o
in ('-m', '--mac'): mac
= a
84 elif o
in ('-h', '--hash'): hash = a
85 elif o
in ('-d', '--database'): dbty
= a
86 else: raise Exception("barf")
87 if len(args
) > 2: return 1
88 if len(args
) >= 1: tag
= args
[0]
91 ## Set up the database.
92 if mac
is None: mac
= hash + '-hmac'
93 try: dbcls
= StorageBackend
.byname(dbty
)
94 except KeyError: die("Unknown database backend `%s'" % dbty
)
95 PW
.create(dbcls
, file, tag
,
96 C
.gcciphers
[cipher
], C
.gchashes
[hash], C
.gcmacs
[mac
])
99 if len(av
) != 0: return 1
100 with
PW(file, writep
= True) as pw
: pw
.changepp()
103 if len(av
) != 1: return 1
105 try: print(_text(pw
[_bin(av
[0])]))
106 except KeyError: die("Password `%s' not found" %
_excval().args
[0])
109 if len(av
) < 1 or len(av
) > 2: return 1
111 with
PW(file, writep
= True) as pw
:
113 pp
= C
.getpass("Enter passphrase `%s': " % tag
)
114 vpp
= C
.getpass("Confirm passphrase `%s': " % tag
)
115 if pp
!= vpp
: die("passphrases don't match")
117 pp
= _bin(stdin
.readline().rstrip('\n'))
123 if len(av
) < 1 or len(av
) > 2: return 1
124 with
PW(file) as pw_in
:
125 with
PW(av
[0], writep
= True) as pw_out
:
126 if len(av
) >= 3: pat
= av
[1]
130 if pat
is None or fnmatch(ktext
, pat
): pw_out
[k
] = pw_in
[k
]
133 if len(av
) > 1: return 1
135 if len(av
) >= 1: pat
= av
[0]
139 if pat
is None or fnmatch(ktext
, pat
): print(ktext
)
142 if len(av
) > 2: return 1
146 for tag
in pw
: pix
.set(tag
, pw
[_bin(tag
)])
149 if len(av
) >= 2: pptag
= av
[1]
151 pix
.set(pptag
, pw
[tag
])
154 if len(av
) != 1: return 1
155 with
PW(file, writep
= True) as pw
:
158 except KeyError: die("Password `%s' not found" %
_excval().args
[0])
162 ## Parse the command line.
163 try: opts
, args
= getopt(av
, 'd:', ['database='])
164 except GetoptError
: return 1
167 if o
in ('-d', '--database'): dbty
= a
168 else: raise Exception("barf")
169 if len(args
) != 1: return 1
170 try: dbcls
= StorageBackend
.byname(dbty
)
171 except KeyError: die("Unknown database backend `%s'" % dbty
)
173 ## Create the target database.
174 with StorageBackend
.open(file) as db_in
:
175 with dbcls
.create(args
[0]) as db_out
:
176 for k
, v
in db_in
.iter_meta(): db_out
.put_meta(k
, v
)
177 for k
, v
in db_in
.iter_passwds(): db_out
.put_passwd(k
, v
)
179 commands
= { 'create': [cmd_create
,
180 '[-c CIPHER] [-d DBTYPE] [-h HASH] [-m MAC] [PP-TAG]'],
181 'find' : [cmd_find
, 'LABEL'],
182 'store' : [cmd_store
, 'LABEL [VALUE]'],
183 'list' : [cmd_list
, '[GLOB-PATTERN]'],
184 'changepp' : [cmd_changepp
, ''],
185 'copy' : [cmd_copy
, 'DEST-FILE [GLOB-PATTERN]'],
186 'to-pixie' : [cmd_topixie
, '[TAG [PIXIE-TAG]]'],
187 'delete' : [cmd_del
, 'TAG'],
188 'xfer': [cmd_xfer
, '[-d DBTYPE] DEST-FILE'] }
190 ###--------------------------------------------------------------------------
191 ### Command-line handling and dispatch.
194 print('%s 1.0.0' % prog
)
195 print('Backend types: %s' %
196 ' '.join([c
.NAME
for c
in StorageBackend
.classes()]))
199 fp
.write('Usage: %s COMMAND [ARGS...]\n' % prog
)
206 Maintains passwords or other short secrets securely.
210 -h, --help Show this help text.
211 -v, --version Show program version number.
212 -u, --usage Show short usage message.
214 -f, --file=FILE Where to find the password-safe file.
218 for c
in sorted(commands
):
219 print('%s %s' %
(c
, commands
[c
][1]))
221 ## Choose a default database file.
222 if 'PWSAFE' in environ
:
223 file = environ
['PWSAFE']
225 file = '%s/.pwsafe' % environ
['HOME']
227 ## Parse the command-line options.
229 opts
, argv
= getopt(argv
[1:], 'hvuf:',
230 ['help', 'version', 'usage', 'file='])
235 if o
in ('-h', '--help'):
238 elif o
in ('-v', '--version'):
241 elif o
in ('-u', '--usage'):
244 elif o
in ('-f', '--file'):
247 raise Exception("barf")
252 ## Dispatch to a command handler.
253 if argv
[0] in commands
:
259 if commands
[c
][0](argv
):
260 stderr
.write('Usage: %s %s %s\n' %
(prog
, c
, commands
[c
][1]))
263 die("decryption failure")
265 ###----- That's all, folks --------------------------------------------------