From 73c3a06e295aaa920adff9672fe6278d1b05dd9f Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Wed, 1 Sep 2021 16:56:47 +0000 Subject: [PATCH] util.py: add check_binary(), shred_file() --- mmgen/main_autosign.py | 11 ++--------- mmgen/main_wallet.py | 9 +++------ mmgen/util.py | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/mmgen/main_autosign.py b/mmgen/main_autosign.py index c806a9e7..208dcbaf 100755 --- a/mmgen/main_autosign.py +++ b/mmgen/main_autosign.py @@ -322,8 +322,8 @@ def wipe_existing_key(): try: os.stat(fn) except: pass else: - msg(f'\nWiping existing key {fn!r}') - run(['wipe','-cf',fn],check=True) + msg(f'\nShredding existing key {fn!r}') + shred_file( fn, verbose=opt.verbose ) def create_key(): kdata = os.urandom(32).hex() @@ -372,12 +372,6 @@ def get_insert_status(): except: return False else: return True -def check_wipe_present(): - try: - run(['wipe','-v'],stdout=DEVNULL,stderr=DEVNULL,check=True) - except: - die(2,"The 'wipe' utility must be installed before running this program") - async def do_loop(): n,prev_status = 0,False if not opt.stealth_led: @@ -406,7 +400,6 @@ if len(cmd_args) == 1: elif cmd != 'wait': die(1,f'{cmd!r}: unrecognized command') -check_wipe_present() wfs = get_wallet_files() def at_exit(exit_val,message='\nCleaning up...'): diff --git a/mmgen/main_wallet.py b/mmgen/main_wallet.py index d2cc3e13..ca4fa457 100755 --- a/mmgen/main_wallet.py +++ b/mmgen/main_wallet.py @@ -224,12 +224,9 @@ if invoked_as == 'passchg' and ss_in.infile.dirname == g.data_dir: confirm_or_raise(m1,m2,exit_msg='Password not changed') ss_out.write_to_file(desc='New wallet',outdir=g.data_dir) bmsg('Securely deleting old wallet') - from subprocess import run - run( - ['shred','--iterations=30','--zero','--remove=wipesync'] - + (['--verbose'] if opt.verbose else []) - + [ss_in.infile.name], - check=True ) + shred_file( + ss_in.infile.name, + verbose = opt.verbose ) else: try: assert invoked_as == 'gen', 'dw' diff --git a/mmgen/util.py b/mmgen/util.py index 41942119..2bcd12cf 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -21,7 +21,7 @@ util.py: Low-level routines imported by other modules in the MMGen suite """ import sys,os,time,stat,re -from subprocess import run +from subprocess import run,PIPE,DEVNULL from hashlib import sha256 from string import hexdigits,digits from .color import * @@ -487,6 +487,20 @@ def compare_or_die(val1, desc1, val2, desc2, e='Error'): dmsg('{} OK ({})'.format(capfirst(desc2),val2)) return True +def check_binary(args): + try: + run(args,stdout=DEVNULL,stderr=DEVNULL,check=True) + except: + rdie(2,f'{args[0]!r} binary missing, not in path, or not executable') + +def shred_file(fn,verbose=False): + check_binary(['shred','--version']) + run( + ['shred','--force','--iterations=30','--zero','--remove=wipesync'] + + (['--verbose'] if verbose else []) + + [fn], + check=True ) + def open_file_or_exit(filename,mode,silent=False): try: return open(filename, mode)