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 ###--------------------------------------------------------------------------
45 prog = re.sub(r'^.*[/\\]', '', argv[0])
48 """Issue a warning message MSG."""
49 print >>stderr, '%s: %s' % (prog, msg)
52 """Report MSG as a fatal error, and exit."""
57 """Return the string PP, without its trailing newline if it has one."""
58 if len(pp) > 0 and pp[-1] == '\n':
62 ###--------------------------------------------------------------------------
63 ### Subcommand implementations.
67 ## Default crypto-primitive selections.
68 cipher = 'blowfish-cbc'
74 opts, args = getopt(av, 'c:h:m:', ['cipher=', 'mac=', 'hash='])
78 if o in ('-c', '--cipher'):
80 elif o in ('-m', '--mac'):
82 elif o in ('-h', '--hash'):
93 ## Set up the database.
94 if mac is None: mac = hash + '-hmac'
95 PW.create(file, C.gcciphers[cipher], C.gchashes[hash], C.gcmacs[mac], tag)
109 except KeyError, exc:
110 die('Password `%s\' not found.' % exc.args[0])
113 if len(av) < 1 or len(av) > 2:
117 pp = C.getpass("Enter passphrase `%s': " % tag)
118 vpp = C.getpass("Confirm passphrase `%s': " % tag)
120 raise ValueError, "passphrases don't match"
122 pp = stdin.readline()
126 pw[av[0]] = chomp(pp)
129 if len(av) < 1 or len(av) > 2:
132 pw_out = PW(av[0], 'w')
138 if pat is None or fnmatch(k, pat):
150 if pat is None or fnmatch(k, pat):
160 pix.set(tag, pw[tag])
167 pix.set(pptag, pw[tag])
176 except KeyError, exc:
177 die('Password `%s\' not found.' % exc.args[0])
179 commands = { 'create': [cmd_create,
180 '[-c CIPHER] [-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']}
189 ###--------------------------------------------------------------------------
190 ### Command-line handling and dispatch.
193 print '%s 1.0.0' % prog
196 print >>fp, 'Usage: %s COMMAND [ARGS...]' % prog
203 Maintains passwords or other short secrets securely.
207 -h, --help Show this help text.
208 -v, --version Show program version number.
209 -u, --usage Show short usage message.
211 -f, --file=FILE Where to find the password-safe file.
215 for c in sorted(commands):
216 print '%s %s' % (c, commands[c][1])
218 ## Choose a default database file.
219 if 'PWSAFE' in environ:
220 file = environ['PWSAFE']
222 file = '%s/.pwsafe' % environ['HOME']
224 ## Parse the command-line options.
226 opts, argv = getopt(argv[1:], 'hvuf:',
227 ['help', 'version', 'usage', 'file='])
232 if o in ('-h', '--help'):
235 elif o in ('-v', '--version'):
238 elif o in ('-u', '--usage'):
241 elif o in ('-f', '--file'):
249 ## Dispatch to a command handler.
250 if argv[0] in commands:
256 if commands[c][0](argv):
257 print >>stderr, 'Usage: %s %s %s' % (prog, c, commands[c][1])
260 die("decryption failure")
262 ###----- That's all, folks --------------------------------------------------