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'): cipher = a
79 elif o in ('-m', '--mac'): mac = a
80 elif o in ('-h', '--hash'): hash = a
82 if len(args) > 2: return 1
83 if len(args) >= 1: tag = args[0]
86 ## Set up the database.
87 if mac is None: mac = hash + '-hmac'
88 PW.create(file, C.gcciphers[cipher], C.gchashes[hash], C.gcmacs[mac], tag)
91 if len(av) != 0: return 1
92 pw = PW(file, writep = True)
96 if len(av) != 1: return 1
99 except KeyError, exc: die("Password `%s' not found." % exc.args[0])
102 if len(av) < 1 or len(av) > 2:
105 pw = PW(file, writep = True)
107 pp = C.getpass("Enter passphrase `%s': " % tag)
108 vpp = C.getpass("Confirm passphrase `%s': " % tag)
109 if pp != vpp: die("passphrases don't match")
111 pp = stdin.readline()
114 pw[av[0]] = chomp(pp)
117 if len(av) < 1 or len(av) > 2: return 1
119 pw_out = PW(av[0], 'w')
120 if len(av) >= 3: pat = av[1]
123 if pat is None or fnmatch(k, pat): pw_out[k] = pw_in[k]
126 if len(av) > 1: return 1
128 if len(av) >= 1: pat = av[0]
131 if pat is None or fnmatch(k, pat): print k
134 if len(av) > 2: return 1
138 for tag in pw: pix.set(tag, pw[tag])
141 if len(av) >= 2: pptag = av[1]
143 pix.set(pptag, pw[tag])
146 if len(av) != 1: return 1
147 pw = PW(file, writep = True)
150 except KeyError, exc: die("Password `%s' not found." % exc.args[0])
152 commands = { 'create': [cmd_create,
153 '[-c CIPHER] [-h HASH] [-m MAC] [PP-TAG]'],
154 'find' : [cmd_find, 'LABEL'],
155 'store' : [cmd_store, 'LABEL [VALUE]'],
156 'list' : [cmd_list, '[GLOB-PATTERN]'],
157 'changepp' : [cmd_changepp, ''],
158 'copy' : [cmd_copy, 'DEST-FILE [GLOB-PATTERN]'],
159 'to-pixie' : [cmd_topixie, '[TAG [PIXIE-TAG]]'],
160 'delete' : [cmd_del, 'TAG']}
162 ###--------------------------------------------------------------------------
163 ### Command-line handling and dispatch.
166 print '%s 1.0.0' % prog
169 print >>fp, 'Usage: %s COMMAND [ARGS...]' % prog
176 Maintains passwords or other short secrets securely.
180 -h, --help Show this help text.
181 -v, --version Show program version number.
182 -u, --usage Show short usage message.
184 -f, --file=FILE Where to find the password-safe file.
188 for c in sorted(commands):
189 print '%s %s' % (c, commands[c][1])
191 ## Choose a default database file.
192 if 'PWSAFE' in environ:
193 file = environ['PWSAFE']
195 file = '%s/.pwsafe' % environ['HOME']
197 ## Parse the command-line options.
199 opts, argv = getopt(argv[1:], 'hvuf:',
200 ['help', 'version', 'usage', 'file='])
205 if o in ('-h', '--help'):
208 elif o in ('-v', '--version'):
211 elif o in ('-u', '--usage'):
214 elif o in ('-f', '--file'):
222 ## Dispatch to a command handler.
223 if argv[0] in commands:
229 if commands[c][0](argv):
230 print >>stderr, 'Usage: %s %s %s' % (prog, c, commands[c][1])
233 die("decryption failure")
235 ###----- That's all, folks --------------------------------------------------