#!/usr/bin/perl # Apollia's TrueCrypt Volume Dismounter # (has drag and drop capabilities) # # Version 1.0 # # by Apollia of Astroblahhh.Com. http://astroblahhh.com/ # # # Completed and released April 29, 2014. # # # Available here: # # http://astroblahhh.com/software/perl/apollias-truecrypt-volume-dismounter-v1.txt # # ################################################################################ # # # Description # # This script simply dismounts any TrueCrypt volume which is dragged and dropped onto it, # or whose filepath is sent to this script as an argument. # # Or, you can optionally put a list of filepaths of TrueCrypt volumes which should be # dismounted when the script is run without drag and drop or without arguments. # # You can configure the script to ignore dragged and dropped items and arguments. # You can also configure this script to be incapable of running unless it is # provided either dragged and dropped items, or filepaths as arguments. # # # This script definitely works with TrueCrypt 7.1 in Lucid Puppy Linux 5.2.8 version 004. # # # I don't know if it will all work properly with other Linuxes or other TrueCrypt # versions. And the stuff involving the RXVT terminal program definitely won't, # if your Linux doesn't have that. # # So, please be very careful. # # # There are a few config settings in this file in the "Some stuff you can change" # section. # # # ----- # # # Except for parts not by Apollia: # # Copyright (C) 2014 Apollia # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see http://www.gnu.org/licenses/ . # # # Contact info: http://astroblahhh.com/contact-apollia.shtml ################################################################ # # Some stuff you should leave alone # # This gets the script's location. use Cwd 'abs_path'; use File::Basename qw( dirname fileparse ); $this_script_path=abs_path($0); $this_script_parentdirs=dirname( $this_script_path ); print "Running $this_script_path...\n\n\n"; $this_escaped_script_path=Escape_Quotes($this_script_path); sub Escape_Quotes { $string=$_[0]; $result=$string; $result=~s/"/\\"/g; return $result; } ################ # # Constants for readability # use constant false => 0; use constant true => 1; $dollar_sign="\$"; # # End of Constants for readability # ################ # # # End of Some stuff you should leave alone # ################################################################ ####################################################################### # # Some stuff you can change # $should_accept_drag_and_drop_items=true; # true|false # # If false, will abort if drag/dropped items are detected, # or if arguments are given to this script. $should_be_capable_of_running_without_drag_and_drop = true; # true|false # # If false, will abort if this script is run without drag # and drop or without arguments. $num_of_seconds_before_autoclose=5; $should_autoclose_at_end_of_script=false; @things_to_dismount_if_script_is_run_without_drag_and_drop = ( # "/root/00-ApWorkspace/example nonexistent file" ); # End of stuff you can change. # # # Below this point, nothing else in this script needs to be changed. # # ####################################################################### sub Make_Greppable_String { $string=$_[0]; if ( String_Contains_Space($string) ) { $result = "\": '$string' \""; } else { $result = "\": $string \""; } return $result; } sub String_Contains_Space { $string=$_[0]; $space_position = index($string, " "); if ($space_position==-1) { return false; } return true; } ################ # # Here, we open an RXVT window so the user can see some output. $First_Argv_Arg=$ARGV[0]; if ( $First_Argv_Arg != 1) # If the RXVT window hasn't been opened yet, # then, the first parameter won't be 1. { $all_argv_args = ""; foreach $argv (@ARGV) { $argv=Escape_Quotes($argv); $all_argv_args = $all_argv_args . " \"$argv\" "; } #print "\n\nAll_argv_args: $all_argv_args"; @args_for_exec=("rxvt -rv +sb -sl 999 -e perl \"$this_escaped_script_path\" 1 $all_argv_args"); #print "\n\nArgs for exec: @args_for_exec"; exec (@args_for_exec); die; # # End of section where we open an RXVT window. # # ################ } #elsif ( $First_Argv_Arg == 1) # Then an RXVT window is already open. #{ @dragged_and_dropped_things_to_dismount = @ARGV; shift @dragged_and_dropped_things_to_dismount; # Gets rid of the first argv arg - just a 1, # leaving only the paths of the files/folders # that were dragged/dropped onto the script. if ($should_accept_drag_and_drop_items==false) { if (@dragged_and_dropped_things_to_dismount) { print "\n\nCurrently configured not to accept drag and dropped items! I assume you didn't actually want to run this script, so, aborting.\n\n"; End_Script(); } } else # Can accept drag and drop items. { @things_to_dismount=@dragged_and_dropped_things_to_dismount; } if ($should_be_capable_of_running_without_drag_and_drop) { if (!@dragged_and_dropped_things_to_dismount) { if (@things_to_dismount_if_script_is_run_without_drag_and_drop) { @things_to_dismount=@things_to_dismount_if_script_is_run_without_drag_and_drop; print "Files to dismount (listed in config settings inside this script):"; } } else # There are dragged and dropped files. { print "Files that you dragged and dropped:"; } } else # Can't run without drag and drop. { if (!@dragged_and_dropped_things_to_dismount) { print "\n\nCurrently configured not to do anything if this script is run without drag and dropping items onto it. Aborting.\n\n"; End_Script(); } } if (!@things_to_dismount) { print "Nothing to dismount - nothing dragged and dropped, and no files listed even in the config settings! Aborting.\n\n"; End_Script(); } print "\n\n"; foreach $thing_to_dismount (@things_to_dismount) { print "$thing_to_dismount \n"; } print "\n\n---"; foreach $thing_to_dismount (@things_to_dismount) { print "\n\n\n\nSeeing if this volume is already mounted..."; $thing_to_dismount_with_escaped_quotes=Escape_Quotes($thing_to_dismount); $thing_to_dismount_for_grep = Make_Greppable_String($thing_to_dismount_with_escaped_quotes); # First, we see if the file to dismount already exists in the list of mounted TrueCrypt volumes. $command_line="truecrypt --text --list | grep $thing_to_dismount_for_grep"; print "\n\n\n# $command_line"; $result=`$command_line`; print "\n\nResult:\n$result"; if (!$result) { print "\n\n\nApparently this wasn't even mounted in the first place: $thing_to_dismount So, skipping the attempt to dismount it. --- "; next; } else { print "\n\nVolume definitely mounted, so we can proceed with dismounting it!"; } push @things_we_attempted_to_dismount, $thing_to_dismount; # If we've reached this point, it's definitely mounted, so we attempt to dismount it. $command_line="truecrypt --text --dismount \"$thing_to_dismount_with_escaped_quotes\" 2>&1"; # The 2>&1 part makes it so output from STDERR gets merged with output from STDOUT. # This is to make it so both STDERR and STDOUT messages can be printed. # # http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html print "\n\n# $command_line"; $result=`$command_line`; print "\n\n"; print "Result:\n$result"; chomp($result); if (!$result) # If it worked properly, there will be no output. { # But, we check to make sure it's really dismounted anyway. print "\n\nChecking to make sure it's dismounted now..."; $command_line="truecrypt --text --list | grep $thing_to_dismount_for_grep"; print "\n\n# $command_line"; $result=`$command_line`; print "\n\nResult: $result"; if (!$result) { print "\n\nDismount succeeded!"; push @things_we_succeeded_in_dismounting, $thing_to_dismount; } } print "\n\n\n---"; } #} print "\n\n\n\nVolumes this script attempted to dismount:"; if ( @things_we_attempted_to_dismount>0) { foreach $item (@things_we_attempted_to_dismount) { print "\n$item"; } } else { print " None."; } print "\n\nVolumes this script succeeded at dismounting:"; if ( @things_we_succeeded_in_dismounting>0) { foreach $item (@things_we_succeeded_in_dismounting) { print "\n$item"; } } else { print " None."; } print "\n\n\nAll remaining mounted volumes:"; $command_line="truecrypt --text --list | sort -n"; print "\n\n# $command_line"; $result=`$command_line`; print "\n\n$result"; print "\n\n"; End_Script(); sub End_Script() { if ($should_autoclose_at_end_of_script ) { End_Script_with_Autoclose(); die; } else { End_Script_and_Wait_for_Return(); die; } } sub End_Script_with_Autoclose() { print "\n\nScript finished!\n"; print "\n\nThis window will automatically close in $num_of_seconds_before_autoclose seconds.\n\n\n"; sleep $num_of_seconds_before_autoclose; die; } sub End_Script_and_Wait_for_Return() { print "\n\nScript finished!\n"; print "\nYou can press Return (or Enter) to close this window.\n"; $wait_for_return = ; die; }