ttomber.py - tomb - the crypto undertaker
 (HTM) git clone git://parazyd.org/tomb.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       ttomber.py (4807B)
       ---
            1 # -*- coding: utf8 -*-
            2 
            3 """
            4 
            5 tomber - a python Tomb (the Crypto Undertaker) wrapper
            6 To use tomber you need to install Tomb (https://github.com/dyne/Tomb)
            7 Copyright © 2014, Federico reiven <reiven_at_gmail.com>
            8 Licensed under BSD License.
            9 See also LICENSE file
           10 
           11 """
           12 
           13 
           14 from subprocess import Popen, PIPE
           15 from tools import parser
           16 
           17 
           18 def get_message(stderr, type):
           19     """
           20     Helper to return exit messages from command execution
           21     """
           22     response = []
           23     for line in stderr.split('\n'):
           24         ret = parser.parse_line(line)
           25         if ret and ret['type'] == type:
           26             if not 'swaps' in ret['content']:
           27                 response.append(ret['content'])
           28     return response
           29 
           30 
           31 def execute(cmd):
           32     """
           33     Execute given cmd. return boolean based on exit status and error string
           34     """
           35     p = Popen(cmd.split(), stdout=PIPE, stderr=PIPE)
           36     stdout, stderr = p.communicate()
           37     p_status = p.wait()
           38     if p_status == 0:
           39         return True, get_message(stderr, 'success')
           40     else:
           41         return False, get_message(stderr, 'error')
           42 
           43 
           44 def sanitize_passphrase(passphrase):
           45     """
           46     Used to avoid errors with passphrases which include spaces
           47     """
           48     return ''.join(['"', passphrase, '"'])
           49 
           50 
           51 def tdig(tombfile, size, force=False):
           52     """
           53     Dig a tomb of given size
           54     """
           55     cmd = ' '.join(['tomb', 'dig', tombfile, '-s', str(size), '--no-color'])
           56     if force:
           57         cmd += " -f"
           58     return execute(cmd)
           59 
           60 
           61 def tforge(keyfile, passphrase, force=False):
           62     """
           63     Forge a key with given passphrase
           64     """
           65     cmd = ' '.join(['tomb',
           66         'forge',
           67         keyfile,
           68         '--unsafe',
           69         '--tomb-pwd',
           70         sanitize_passphrase(passphrase),
           71         '--no-color'])
           72     if force:
           73         cmd += " -f"
           74     return execute(cmd)
           75 
           76 
           77 def tlock(tombfile, keyfile, passphrase):
           78     """
           79     Lock a tomb file with given key and passphrase.
           80     """
           81     cmd = ' '.join(['tomb',
           82         'lock',
           83         tombfile,
           84         '-k',
           85         keyfile,
           86         '--unsafe',
           87         '--tomb-pwd',
           88         sanitize_passphrase(passphrase),
           89         '--no-color'])
           90     return execute(cmd)
           91 
           92 
           93 def topen(tombfile, keyfile, passphrase, mountpath=False):
           94     """
           95     Open (mount) a tomb.
           96     Keyfile and passphrase are needed, mountpoint is optional
           97     """
           98     if not mountpath:
           99         mountpath = ''
          100     cmd = ' '.join(['tomb',
          101         'open',
          102         tombfile,
          103         '-k',
          104         keyfile,
          105         '--unsafe',
          106         '--tomb-pwd',
          107         sanitize_passphrase(passphrase),
          108         '--no-color',
          109         mountpath])
          110     return execute(cmd)
          111 
          112 
          113 def tclose(tombfile):
          114     """
          115     Close (umount) a tomb
          116     """
          117     cmd = ' '.join(['tomb', 'close', tombfile, '--no-color'])
          118     return execute(cmd)
          119 
          120 
          121 def tresize(tombfile, keyfile, passphrase, newsize):
          122     """
          123     Resize a tomb.
          124     Keyfile, passphrase and new size are needed.
          125     """
          126     cmd = ' '.join(['tomb',
          127         'resize',
          128         tombfile,
          129         '-k',
          130         keyfile,
          131         '--unsafe',
          132         '--tomb-pwd',
          133         sanitize_passphrase(passphrase),
          134         '-s',
          135         str(newsize),
          136         '--no-color'])
          137     return execute(cmd)
          138 
          139 
          140 def tbury(keyfile, passphrase, imagefile):
          141     """
          142     Bury a key inside a jpg file
          143     """
          144     cmd = ' '.join(['tomb',
          145         'bury',
          146         '-k',
          147         keyfile,
          148         '--unsafe',
          149         '--tomb-pwd',
          150         sanitize_passphrase(passphrase),
          151         imagefile,
          152         '--no-color'])
          153     return execute(cmd)
          154 
          155 
          156 def texhume(keyfile, passphrase, imagefile):
          157     """
          158     Exhume (recover) key from jpg file. Passphrase for key is needed
          159     """
          160     cmd = ' '.join(['tomb',
          161         'exhume',
          162         '-k',
          163         keyfile,
          164         '--unsafe',
          165         '--tomb-pwd',
          166         sanitize_passphrase(passphrase),
          167         imagefile,
          168         '--no-color'])
          169     return execute(cmd)
          170 
          171 
          172 def tpasswd(keyfile, newpassphrase, oldpassphrase):
          173     """
          174     Change current passphrase from keyfile
          175     """
          176     cmd = ' '.join(['tomb',
          177         'passwd',
          178         '-k',
          179         keyfile,
          180         '--unsafe',
          181         '--tomb-pwd',
          182         sanitize_passphrase(newpassphrase),
          183         '--tomb-old-pwd',
          184         sanitize_passphrase(oldpassphrase),
          185         '--no-color'])
          186     return execute(cmd)
          187 
          188 
          189 def tsetkey(oldkeyfile, tombfile, newkeyfile, newpassphrase, oldpassphrase):
          190     """
          191     Change lock key for a tomb
          192     The old key+passphrase and new key+passphrase are needed
          193     """
          194     cmd = ' '.join(['tomb',
          195         'setkey',
          196         '-k',
          197         newkeyfile,
          198         oldkeyfile,
          199         tombfile,
          200         '--unsafe',
          201         '--tomb-old-pwd',
          202         sanitize_passphrase(newpassphrase),
          203         '--tomb-pwd',
          204         sanitize_passphrase(oldpassphrase),
          205         '--no-color'])
          206     return execute(cmd)
          207 
          208 
          209 def tslam():
          210     """
          211     Slam tombs, killing all programs using it
          212     """
          213     cmd = ' '.join(['tomb', 'slam'])
          214     return execute(cmd)