Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ###########################################################################
- ############### A quick way to securely erase a list of files
- ###########################################################################
- ww="which wipe >/dev/null"
- eval $ww
- if [ "$?" != "0" ]; then echo "!! You need to install wipe to use this tool !!"; unset ww; exit 1; fi
- ####
- declare -i ctstat #files deleted
- declare -i ctfail #files failed
- declare -i ctignore #files ignored (already removed)
- cfd=""
- cff=""
- cfi=""
- ctstat=0
- ctfail=0
- ctignore=0
- laststat=""
- devseed="/dev/random"
- tmprnd="./.rmprnd"
- _retval=""
- wforce=""
- wrecurse=""
- wkeep=""
- wcomopts="-Sr -R $devseed -Ma -Q3 -iq$wforce$wrecurse$wkeep"
- wcom="wipe $wcomopts"
- bvb="n"
- ###
- # this is the only way to get aliasing to work
- shopt -s expand_aliases
- alias echo='echo -e' #gets removed in quit
- #####
- #functions
- usage() {
- echo "usage: $0 [options] <file1 [\"file2\" \'file3\' ...]>"
- echo "usage: $0 -h|--help\t\tshow this"
- echo "usage: $0 -f|--force\t\tforce wipe, do not prompt"
- echo "usage: $0 -r|--recurse\t\tallow the removal of the entire directory tree"
- echo "usage: $0 -k\t\tdo not delete the file after wiping it"
- echo "usage: $0 quickwipe may encounter problems with gathering random data (\"low entropy\")\n\t\t\tif this happens, the program will try to gather random data from 3 other sources before giving up\n\t\t\tdata sources (in order): /dev/random, /dev/urandom/, 'openssl rand' output"
- quit 0
- }
- quit() {
- unalias echo
- shopt -u expand_aliases
- unset devseed
- rm -f $tmprnd
- unset tmprnd
- unset _retval
- unset wforce
- unset wrecurse
- unset wcom
- unset wcomopts
- unset wkeep
- unset gencrstemp
- unset arg1
- unset arg2
- unset shorti
- unset ctstat
- unset ctfail
- unset ctignore
- unset laststat
- unset cfd
- unset cff
- unset cfi
- unset bvb
- unset ww
- echo "(exitting with code $1)"
- exit $1
- }
- _reseed() {
- #devseed must be updated prior to calling this function
- orand="openssl rand -out $devseed 10000000" #10mb of random data
- eval $orand
- return $?
- }
- execwipe() {
- #devseed must be updated prior to calling this function
- wcomopts="-Sr -R $devseed -Ma -Q3 -iq$wforce$wrecurse$wkeep"
- wcom="wipe $wcomopts $1 2>/dev/null"
- eval $wcom
- return $?
- }
- genshorti() {
- shorti=""
- arg1="$1"
- arg1=${#arg1}
- if [ "$arg1" -gt "7" ]; then shorti="${i:0:7}..."; else shorti="$i"; fi
- }
- _ks() {
- #cfd files del
- #cff file fail
- #cfi file ignore
- _arg1="$1"
- # file was forced/failed
- if [ "$#" == "2" ]; then _arg2="$2"
- else _arg2=""; fi
- if [ "$_arg2" == "f" ]; then cff="$cff $_arg1"; ((ctfail++)); return 0; fi
- # check file existence
- if [ ! -f "$_arg1" ]; then cfd="$cfd $_arg1"; ((ctstat++)); return 0
- else cff="$cff $_arg1"; ((ctfail++)); fi
- }
- keepstat() {
- #gather some stats (only works if --keep is NOT enabled)
- if [ "$wkeep" == "k" ]; then return 0; fi
- arg1="$1"
- # get 2nd var
- if [ "$#" == "2" ]; then arg2="$2"
- else arg2=""; fi
- # file was ignored (i = ignore flag)
- if [ "$arg2" == "i" ]; then cfi="$cfi $arg1"; ((ctignore++)); laststat="$arg1"; return 0; fi
- # duplicate call
- if [ "$laststat" == "$arg1" ]; then
- # only run _ks if the force flag it given
- if [ "$arg2" == "f" ]; then
- _ks "$arg1" "f"
- laststat="$arg1"
- fi
- else
- _ks "$arg1"
- laststat="$arg1"
- #echo "ls $laststat || curr $arg1"
- fi
- }
- # https://gist.github.com/cosimo/3760587
- OPTS=`getopt -o hfrkv --long help,force,recurse,keep,verbose -n 'parse-options' -- "$@"`
- if [ $? != 0 ]; then echo "Failed parsing options..."; quit 1; fi
- eval set -- "$OPTS"
- # parse options
- while true; do
- case "$1" in
- ( -h | --help )
- usage
- ;;
- ( -f | --force )
- wforce="f"
- ;;
- ( -r | --recurse )
- wrecurse="r"
- ;;
- ( -k | --keep )
- wkeep="k"
- ;;
- ( -v | --verbose )
- bvb="y"
- ;;
- ( -- ) shift; break ;;
- ( -* ) echo "$0: error - unrecognized option $1" 1>&2; quit 1;;
- ( * ) break ;;
- esac
- shift
- done
- # if there are no more arguments, show usage and quit
- if [ "$#" -lt "1" ]; then usage; quit 1; fi
- # parse files
- for i in "$@"; do
- # check existence of file
- if [ ! -f "$i" ]; then echo "wipe: file not found"; keepstat "$i" "i"; shift; continue; fi
- # try wipe with /dev/random
- devseed="/dev/random"; genshorti $i
- echo -n "trying quickwipe($devseed) on \"$shorti\" --> "
- execwipe $i
- # WIPEFAIL on /dev/random
- if [ "$?" != "0" ]; then echo "failed!"
- else keepstat "$i"; shift; continue; fi
- # try wipe with /dev/urandom
- devseed="/dev/urandom"; genshorti $i
- echo -n "\ttrying quickwipe($devseed) on \"$shorti\" --> "
- execwipe $i
- # WIPEFAIL on /dev/urandom
- if [ "$?" != "0" ]; then echo "failed!"
- else keepstat "$i"; shift; continue; fi
- # try wiping with $tmprnd
- devseed="$tmprnd"
- echo -n "\ttrying to seed $devseed... "
- _reseed
- # SEEDFAIL on $tmprnd
- if [ "$?" != "0" ]; then echo "failed!"
- else
- genshorti $i
- echo -n "\ttrying quickwipe($devseed) on \"$shorti\" --> "
- # seeding $tmprnd succeeded, try wiping
- devseed="$tmprnd"
- execwipe $i
- _retval="$?"
- fi
- # WIPEFAIL on $tmprnd
- if [ "$_retval" != "0" ]; then echo "failed!"
- else keepstat "$i"; shift; continue; fi
- # try seeding /dev/random
- devseed="/dev/random"
- echo -n "\ttrying to seed $devseed... "
- _reseed
- # SEEDFAIL on /dev/random
- if [ "$?" != "0" ]; then echo "failed!"
- else
- genshorti $i
- echo -n "\ttrying quickwipe($devseed) on \"$shorti\" --> "
- #seeding /dev/random succeeded, try wipping
- devseed="/dev/random"
- execwipe $i
- _retval="$?"
- fi
- # WIPEFAIL on /dev/random
- if [ "$_retval" != "0" ]; then echo "failed!"
- else keepstat "$i"; shift; continue; fi
- # try seeding /dev/urandom
- devseed="/dev/urandom"
- echo -n "\ttrying to seed $devseed... "
- _reseed
- # final stop ... if wipe fails, shift & continue
- # SEEDFAIL on /dev/urandom
- if [ "$?" != "0" ]; then genshorti $i; echo "failed!\n!!\tout of options! quickwipe on \"$shorti...\" FAILED"; keepstat "$i" "f"; shift; continue
- else
- genshorti $i
- echo -n "trying quickwipe($devseed) on \"$shorti\" --> "
- #seeding /dev/urandom succeeded, try wipping
- devseed="/dev/urandom"
- execwipe $i
- _retval="$?"
- fi
- # WIPEFAIL on /dev/urandom
- if [ "$_retval" != "0" ]; then genshorti $i; echo "failed!\n!!\tout of options! quickwipe on \"$shorti...\" FAILED"; keepstat "$i" "f"; shift; continue
- else keepstat "$i"; shift; continue; fi
- # move to next argument
- shift
- done
- if [ "$wkeep" == "" ]; then echo "\nfinished:\n$ctstat files wiped\n$ctfail files failed to be wiped\n$ctignore files ignored"; fi
- if [ "$bvb" == "y" ]; then echo "\nfiles wiped: $cfd\nfiles ignored: $cfi\nfiles failed: $cff"; fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement