#! /usr/bin/perl -w use strict; use GnuPG; use Getopt::Long; use IO::File; use POSIX qw(tmpnam); use Data::Dumper; use vars qw[ $temp_name $passphrase $gpg %tan_array $round ]; use vars qw[ $number $debug $multiple $free $keep $help ]; my $opt_result = GetOptions( 'n=i' => \$number, 'd' => \$debug, 'k' => \$keep, 'f' => \$free, 'm' => \$multiple, 'h' => \$help ); &help() unless( $opt_result ); &help() if( defined( $help )); my $temp_fh; # open temporary file to store decrypted TANs do { $temp_name = tmpnam() } until $temp_fh = IO::File->new($temp_name, O_RDWR|O_CREAT|O_EXCL); ### Initialise $gpg $gpg = new GnuPG(); # Delete temporary file automatically at the end END { if( defined( $temp_name )){ unlink($temp_name) or die "Couldn't unlink $temp_name : $!" } } ## check for argument my $arg_check = &check_args(); if( $arg_check ne 'true' ){ die "Error: ${arg_check}!\n"; } ## Get passphrase $passphrase = &password(); %tan_array = &decrypt(); if( defined( $free )){ # Calculate number of items in hash my $items = &num_tan( %tan_array ); print "\n$items iTANs in list!\n\n"; exit; } while( my @testarray = each( %tan_array )){ print "Index $testarray[0] has value $testarray[1]\n" if( defined( $debug )); } while(){ $number = &ask_number() unless( defined( $number )); last if( $number !~ /^\d+$/ ); if( defined( $tan_array{$number} )){ print "iTAN #$number is: $tan_array{$number}\n"; delete( $tan_array{$number} ) unless( defined( $keep )); }else{ print "No iTAN #$number\n"; } ## Break if multiple was not defined last unless( defined( $multiple )); undef( $number ); } ## After that loop, we need to write the unused iTANs back to the file ## if saving was not disabled ($keep is set). &write_tan( %tan_array ) unless( defined( $keep )); # Calculate number of items in hash my $items = &num_tan( %tan_array ); print "\n$items iTANs remaining!\n\n"; sub num_tan { my( %tans ) = @_; # Calculate number of items in hash my @items = values( %tans ); my $items = @items; return $items; } sub write_tan { my ( %tans ) = @_; die "No argument given! Expect hash of iTANs!\n" unless( defined( $_[0] )); open( WRITE, "> $temp_name" ) || die "Can't save unused iTANs to temporary file: $!\n"; while( my @testarray = each( %tan_array )){ print WRITE $testarray[0] . ',' . $testarray[1] . "\n"; } close( WRITE ); &encrypt_file(); } sub encrypt_file { $gpg->encrypt( plaintext => $temp_name, output => $ARGV[0], passphrase => $passphrase, recipient => 'to.c@gmx.net' ); return 1; } sub decrypt(){ my %temp_tan_array; $gpg->decrypt( ciphertext => $ARGV[0], output => $temp_name, passphrase => $passphrase ); open( READ, "< $temp_name" ) || die "Can't open decrypted file!\n"; while(){ chop; my ( $id, $tan ) = split/[-,\-\s]+/; $temp_tan_array{$id} = $tan; print "ID: $id - TAN $tan\n" if( defined( $debug )); } close( READ ); return %temp_tan_array; } sub password(){ my $password; ## Turn printing characters off qx(/bin/stty -echo); print "\nGnuPG Passphrase: "; $password = ; chop ( $password ); ## turn it on again qx(/bin/stty echo); print "\n\n"; return $password; } sub ask_number(){ my $number; print "Which iTAN do you want to see: "; $number = ; chop( $number ); return $number; } sub check_args(){ return "No argument to decrypt" unless( defined( $ARGV[0] )); return "No such file: $ARGV[0]" unless( -f $ARGV[0] ); return "File not readable" unless( -r $ARGV[0] ); return "true"; } sub help { print <