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 _excval(): return SYS
.exc_info()[1]
46 ###--------------------------------------------------------------------------
50 prog
= re
.sub(r
'^.*[/\\]', '', argv
[0])
53 """Issue a warning message MSG."""
54 print >>stderr
, '%s: %s' %
(prog
, msg
)
57 """Report MSG as a fatal error, and exit."""
61 ###--------------------------------------------------------------------------
62 ### Subcommand implementations.
66 ## Default crypto-primitive selections.
67 cipher
= 'rijndael-cbc'
73 opts
, args
= getopt(av
, 'c:d:h:m:',
74 ['cipher=', 'database=', 'mac=', 'hash='])
79 if o
in ('-c', '--cipher'): cipher
= a
80 elif o
in ('-m', '--mac'): mac
= a
81 elif o
in ('-h', '--hash'): hash = a
82 elif o
in ('-d', '--database'): dbty
= a
83 else: raise Exception("barf")
84 if len(args
) > 2: return 1
85 if len(args
) >= 1: tag
= args
[0]
88 ## Set up the database.
89 if mac
is None: mac
= hash + '-hmac'
90 try: dbcls
= StorageBackend
.byname(dbty
)
91 except KeyError: die("Unknown database backend `%s'" % dbty
)
92 PW
.create(dbcls
, file, tag
,
93 C
.gcciphers
[cipher
], C
.gchashes
[hash], C
.gcmacs
[mac
])
96 if len(av
) != 0: return 1
97 with
PW(file, writep
= True) as pw
: pw
.changepp()
100 if len(av
) != 1: return 1
103 except KeyError: die("Password `%s' not found" %
_excval().args
[0])
106 if len(av
) < 1 or len(av
) > 2: return 1
108 with
PW(file, writep
= True) as pw
:
110 pp
= C
.getpass("Enter passphrase `%s': " % tag
)
111 vpp
= C
.getpass("Confirm passphrase `%s': " % tag
)
112 if pp
!= vpp
: die("passphrases don't match")
114 pp
= stdin
.readline().rstrip('\n')
120 if len(av
) < 1 or len(av
) > 2: return 1
121 with
PW(file) as pw_in
:
122 with
PW(av
[0], writep
= True) as pw_out
:
123 if len(av
) >= 3: pat
= av
[1]
126 if pat
is None or fnmatch(k
, pat
): pw_out
[k
] = pw_in
[k
]
129 if len(av
) > 1: return 1
131 if len(av
) >= 1: pat
= av
[0]
134 if pat
is None or fnmatch(k
, pat
): print k
137 if len(av
) > 2: return 1
141 for tag
in pw
: pix
.set(tag
, pw
[tag
])
144 if len(av
) >= 2: pptag
= av
[1]
146 pix
.set(pptag
, pw
[tag
])
149 if len(av
) != 1: return 1
150 with
PW(file, writep
= True) as pw
:
153 except KeyError: die("Password `%s' not found" %
_excval().args
[0])
157 ## Parse the command line.
158 try: opts
, args
= getopt(av
, 'd:', ['database='])
159 except GetoptError
: return 1
162 if o
in ('-d', '--database'): dbty
= a
163 else: raise Exception("barf")
164 if len(args
) != 1: return 1
165 try: dbcls
= StorageBackend
.byname(dbty
)
166 except KeyError: die("Unknown database backend `%s'" % dbty
)
168 ## Create the target database.
169 with StorageBackend
.open(file) as db_in
:
170 with dbcls
.create(args
[0]) as db_out
:
171 for k
, v
in db_in
.iter_meta(): db_out
.put_meta(k
, v
)
172 for k
, v
in db_in
.iter_passwds(): db_out
.put_passwd(k
, v
)
174 commands
= { 'create': [cmd_create
,
175 '[-c CIPHER] [-d DBTYPE] [-h HASH] [-m MAC] [PP-TAG]'],
176 'find' : [cmd_find
, 'LABEL'],
177 'store' : [cmd_store
, 'LABEL [VALUE]'],
178 'list' : [cmd_list
, '[GLOB-PATTERN]'],
179 'changepp' : [cmd_changepp
, ''],
180 'copy' : [cmd_copy
, 'DEST-FILE [GLOB-PATTERN]'],
181 'to-pixie' : [cmd_topixie
, '[TAG [PIXIE-TAG]]'],
182 'delete' : [cmd_del
, 'TAG'],
183 'xfer': [cmd_xfer
, '[-d DBTYPE] DEST-FILE'] }
185 ###--------------------------------------------------------------------------
186 ### Command-line handling and dispatch.
189 print '%s 1.0.0' % prog
190 print 'Backend types: %s' % \
191 ' '.join([c
.NAME
for c
in StorageBackend
.classes()])
194 print >>fp
, 'Usage: %s COMMAND [ARGS...]' % prog
201 Maintains passwords or other short secrets securely.
205 -h, --help Show this help text.
206 -v, --version Show program version number.
207 -u, --usage Show short usage message.
209 -f, --file=FILE Where to find the password-safe file.
213 for c
in sorted(commands
):
214 print '%s %s' %
(c
, commands
[c
][1])
216 ## Choose a default database file.
217 if 'PWSAFE' in environ
:
218 file = environ
['PWSAFE']
220 file = '%s/.pwsafe' % environ
['HOME']
222 ## Parse the command-line options.
224 opts
, argv
= getopt(argv
[1:], 'hvuf:',
225 ['help', 'version', 'usage', 'file='])
230 if o
in ('-h', '--help'):
233 elif o
in ('-v', '--version'):
236 elif o
in ('-u', '--usage'):
239 elif o
in ('-f', '--file'):
242 raise Exception("barf")
247 ## Dispatch to a command handler.
248 if argv
[0] in commands
:
254 if commands
[c
][0](argv
):
255 print >>stderr
, 'Usage: %s %s %s' %
(prog
, c
, commands
[c
][1])
258 die("decryption failure")
260 ###----- That's all, folks --------------------------------------------------