chiark / gitweb /
Merge remote-tracking branch 'origin/HEAD'
[catacomb-python] / pwsafe
diff --git a/pwsafe b/pwsafe
index 5f2e75d1e7ae22f488c6d14f772ec9491c01e9df..c12f856bd5e7dee42bfec95c67dd254c2b16219c 100644 (file)
--- a/pwsafe
+++ b/pwsafe
@@ -53,12 +53,6 @@ def die(msg):
   moan(msg)
   exit(1)
 
-def chomp(pp):
-  """Return the string PP, without its trailing newline if it has one."""
-  if len(pp) > 0 and pp[-1] == '\n':
-    pp = pp[:-1]
-  return pp
-
 ###--------------------------------------------------------------------------
 ### Subcommand implementations.
 
@@ -71,13 +65,16 @@ def cmd_create(av):
 
   ## Parse the options.
   try:
-    opts, args = getopt(av, 'c:h:m:', ['cipher=', 'mac=', 'hash='])
+    opts, args = getopt(av, 'c:d:h:m:',
+                        ['cipher=', 'database=', 'mac=', 'hash='])
   except GetoptError:
     return 1
+  dbty = 'flat'
   for o, a in opts:
     if o in ('-c', '--cipher'): cipher = a
     elif o in ('-m', '--mac'): mac = a
     elif o in ('-h', '--hash'): hash = a
+    elif o in ('-d', '--database'): dbty = a
     else: raise 'Barf!'
   if len(args) > 2: return 1
   if len(args) >= 1: tag = args[0]
@@ -85,7 +82,10 @@ def cmd_create(av):
 
   ## Set up the database.
   if mac is None: mac = hash + '-hmac'
-  PW.create(file, C.gcciphers[cipher], C.gchashes[hash], C.gcmacs[mac], tag)
+  try: dbcls = StorageBackend.byname(dbty)
+  except KeyError: die("Unknown database backend `%s'" % dbty)
+  PW.create(dbcls, file, tag,
+            C.gcciphers[cipher], C.gchashes[hash], C.gcmacs[mac])
 
 def cmd_changepp(av):
   if len(av) != 0: return 1
@@ -106,10 +106,10 @@ def cmd_store(av):
       vpp = C.getpass("Confirm passphrase `%s': " % tag)
       if pp != vpp: die("passphrases don't match")
     elif av[1] == '-':
-      pp = stdin.readline()
+      pp = stdin.readline().rstrip('\n')
     else:
       pp = av[1]
-    pw[av[0]] = chomp(pp)
+    pw[av[0]] = pp
 
 def cmd_copy(av):
   if len(av) < 1 or len(av) > 2: return 1
@@ -147,21 +147,43 @@ def cmd_del(av):
     try: del pw[tag]
     except KeyError, exc: die("Password `%s' not found" % exc.args[0])
 
+def cmd_xfer(av):
+
+  ## Parse the command line.
+  try: opts, args = getopt(av, 'd:', ['database='])
+  except GetoptError: return 1
+  dbty = 'flat'
+  for o, a in opts:
+    if o in ('-d', '--database'): dbty = a
+    else: raise 'Barf!'
+  if len(args) != 1: return 1
+  try: dbcls = StorageBackend.byname(dbty)
+  except KeyError: die("Unknown database backend `%s'" % dbty)
+
+  ## Create the target database.
+  with StorageBackend.open(file) as db_in:
+    with dbcls.create(args[0]) as db_out:
+      for k, v in db_in.iter_meta(): db_out.put_meta(k, v)
+      for k, v in db_in.iter_passwds(): db_out.put_passwd(k, v)
+
 commands = { 'create': [cmd_create,
-                        '[-c CIPHER] [-h HASH] [-m MAC] [PP-TAG]'],
+                        '[-c CIPHER] [-d DBTYPE] [-h HASH] [-m MAC] [PP-TAG]'],
              'find' : [cmd_find, 'LABEL'],
              'store' : [cmd_store, 'LABEL [VALUE]'],
              'list' : [cmd_list, '[GLOB-PATTERN]'],
              'changepp' : [cmd_changepp, ''],
              'copy' : [cmd_copy, 'DEST-FILE [GLOB-PATTERN]'],
              'to-pixie' : [cmd_topixie, '[TAG [PIXIE-TAG]]'],
-             'delete' : [cmd_del, 'TAG']}
+             'delete' : [cmd_del, 'TAG'],
+             'xfer': [cmd_xfer, '[-d DBTYPE] DEST-FILE'] }
 
 ###--------------------------------------------------------------------------
 ### Command-line handling and dispatch.
 
 def version():
   print '%s 1.0.0' % prog
+  print 'Backend types: %s' % \
+      ' '.join([c.NAME for c in StorageBackend.classes()])
 
 def usage(fp):
   print >>fp, 'Usage: %s COMMAND [ARGS...]' % prog