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."""
56 ###--------------------------------------------------------------------------
57 ### Subcommand implementations.
61 ## Default crypto-primitive selections.
62 cipher = 'blowfish-cbc'
68 opts, args = getopt(av, 'c:h:m:', ['cipher=', 'mac=', 'hash='])
72 if o in ('-c', '--cipher'): cipher = a
73 elif o in ('-m', '--mac'): mac = a
74 elif o in ('-h', '--hash'): hash = a
76 if len(args) > 2: return 1
77 if len(args) >= 1: tag = args[0]
80 ## Set up the database.
81 if mac is None: mac = hash + '-hmac'
82 PW.create(file, C.gcciphers[cipher], C.gchashes[hash], C.gcmacs[mac], tag)
85 if len(av) != 0: return 1
86 with PW(file, writep = True) as pw: pw.changepp()
89 if len(av) != 1: return 1
92 except KeyError, exc: die("Password `%s' not found" % exc.args[0])
95 if len(av) < 1 or len(av) > 2: return 1
97 with PW(file, writep = True) as pw:
99 pp = C.getpass("Enter passphrase `%s': " % tag)
100 vpp = C.getpass("Confirm passphrase `%s': " % tag)
101 if pp != vpp: die("passphrases don't match")
103 pp = stdin.readline().rstrip('\n')
109 if len(av) < 1 or len(av) > 2: return 1
110 with PW(file) as pw_in:
111 with PW(av[0], writep = True) as pw_out:
112 if len(av) >= 3: pat = av[1]
115 if pat is None or fnmatch(k, pat): pw_out[k] = pw_in[k]
118 if len(av) > 1: return 1
120 if len(av) >= 1: pat = av[0]
123 if pat is None or fnmatch(k, pat): print k
126 if len(av) > 2: return 1
130 for tag in pw: pix.set(tag, pw[tag])
133 if len(av) >= 2: pptag = av[1]
135 pix.set(pptag, pw[tag])
138 if len(av) != 1: return 1
139 with PW(file, writep = True) as pw:
142 except KeyError, exc: die("Password `%s' not found" % exc.args[0])
144 commands = { 'create': [cmd_create,
145 '[-c CIPHER] [-h HASH] [-m MAC] [PP-TAG]'],
146 'find' : [cmd_find, 'LABEL'],
147 'store' : [cmd_store, 'LABEL [VALUE]'],
148 'list' : [cmd_list, '[GLOB-PATTERN]'],
149 'changepp' : [cmd_changepp, ''],
150 'copy' : [cmd_copy, 'DEST-FILE [GLOB-PATTERN]'],
151 'to-pixie' : [cmd_topixie, '[TAG [PIXIE-TAG]]'],
152 'delete' : [cmd_del, 'TAG']}
154 ###--------------------------------------------------------------------------
155 ### Command-line handling and dispatch.
158 print '%s 1.0.0' % prog
161 print >>fp, 'Usage: %s COMMAND [ARGS...]' % prog
168 Maintains passwords or other short secrets securely.
172 -h, --help Show this help text.
173 -v, --version Show program version number.
174 -u, --usage Show short usage message.
176 -f, --file=FILE Where to find the password-safe file.
180 for c in sorted(commands):
181 print '%s %s' % (c, commands[c][1])
183 ## Choose a default database file.
184 if 'PWSAFE' in environ:
185 file = environ['PWSAFE']
187 file = '%s/.pwsafe' % environ['HOME']
189 ## Parse the command-line options.
191 opts, argv = getopt(argv[1:], 'hvuf:',
192 ['help', 'version', 'usage', 'file='])
197 if o in ('-h', '--help'):
200 elif o in ('-v', '--version'):
203 elif o in ('-u', '--usage'):
206 elif o in ('-f', '--file'):
214 ## Dispatch to a command handler.
215 if argv[0] in commands:
221 if commands[c][0](argv):
222 print >>stderr, 'Usage: %s %s %s' % (prog, c, commands[c][1])
225 die("decryption failure")
227 ###----- That's all, folks --------------------------------------------------