2011-02-10 11:22:11 +00:00
|
|
|
#!/bin/zsh
|
2010-08-22 13:04:19 +00:00
|
|
|
#
|
2011-01-13 13:37:52 +00:00
|
|
|
# Tomb, the Crypto Undertaker
|
2010-08-22 13:04:19 +00:00
|
|
|
#
|
2011-01-13 13:37:52 +00:00
|
|
|
# a tool to easily operate file encryption of private and secret data
|
2010-08-22 13:04:19 +00:00
|
|
|
#
|
2011-01-11 18:30:34 +00:00
|
|
|
# Copyleft (C) 2007-2011 Denis Roio <jaromil@dyne.org>
|
2010-08-22 13:04:19 +00:00
|
|
|
#
|
|
|
|
# This source code is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Public License as published by
|
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This source code 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.
|
|
|
|
# Please refer to the GNU Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Public License along with
|
|
|
|
# this source code; if not, write to:
|
|
|
|
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
2011-02-24 10:44:41 +00:00
|
|
|
VERSION=1.0
|
2011-02-03 16:11:08 +00:00
|
|
|
DATE=Feb/2011
|
2010-08-22 13:04:19 +00:00
|
|
|
|
2011-01-12 10:38:03 +00:00
|
|
|
# PATH=/usr/bin:/usr/sbin:/bin:/sbin
|
2010-08-29 12:56:53 +00:00
|
|
|
|
2010-08-22 13:04:19 +00:00
|
|
|
# standard output message routines
|
|
|
|
# it's always useful to wrap them, in case we change behaviour later
|
2011-02-14 09:24:31 +00:00
|
|
|
notice() { if ! [ $QUIET ]; then echo "[*] $1" >&2; fi }
|
|
|
|
act() { if ! [ $QUIET ]; then echo " . $1" >&2; fi }
|
|
|
|
error() { if ! [ $QUIET ]; then echo "[!] $1" >&2; fi }
|
|
|
|
func() { if [ $DEBUG ]; then echo "[D] $1" >&2; fi }
|
2010-08-22 13:04:19 +00:00
|
|
|
|
2010-08-29 12:56:53 +00:00
|
|
|
# which dd command to use
|
|
|
|
which dcfldd > /dev/null
|
|
|
|
if [ $? = 0 ]; then
|
|
|
|
DD="dcfldd"
|
|
|
|
else
|
|
|
|
DD=dd
|
|
|
|
fi
|
|
|
|
|
2011-01-11 18:27:30 +00:00
|
|
|
# which wipe command to use
|
|
|
|
which wipe > /dev/null
|
|
|
|
if [ $? = 0 ]; then
|
2011-01-16 22:43:45 +00:00
|
|
|
WIPE=(wipe -f -s -q)
|
2011-01-11 18:27:30 +00:00
|
|
|
else
|
2011-01-16 22:43:45 +00:00
|
|
|
WIPE=(rm -f)
|
2011-01-11 18:27:30 +00:00
|
|
|
fi
|
|
|
|
|
2011-01-10 19:41:28 +00:00
|
|
|
|
2011-02-14 09:24:31 +00:00
|
|
|
# we use pinentry now
|
|
|
|
# comes from gpg project and is much more secure
|
|
|
|
# it also conveniently uses the right toolkit
|
2010-08-22 13:04:19 +00:00
|
|
|
ask_password() {
|
|
|
|
|
2011-02-14 09:24:31 +00:00
|
|
|
# pinentry has no custom icon setting
|
|
|
|
# so we need to temporary modify the gtk theme
|
2011-02-24 23:10:09 +00:00
|
|
|
if [ -r /usr/local/share/themes/tomb/gtk-2.0-key/gtkrc ]; then
|
|
|
|
GTK2_RC=/usr/local/share/themes/tomb/gtk-2.0-key/gtkrc
|
|
|
|
elif [ -r /usr/share/themes/tomb/gtk-2.0-key/gtkrc ]; then
|
|
|
|
GTK2_RC=/usr/share/themes/tomb/gtk-2.0-key/gtkrc
|
2011-02-14 23:19:45 +00:00
|
|
|
fi
|
2011-02-20 13:59:30 +00:00
|
|
|
|
2011-02-24 23:10:09 +00:00
|
|
|
cat <<EOF | GTK2_RC_FILES=${GTK2_RC} pinentry | awk '/^D/ { print $2 }'
|
2011-02-20 13:59:30 +00:00
|
|
|
SETTITLE Insert tomb password
|
|
|
|
SETDESC Open tomb: $1
|
2011-02-14 09:24:31 +00:00
|
|
|
SETPROMPT Password:
|
|
|
|
GETPIN
|
|
|
|
EOF
|
|
|
|
|
2011-01-19 11:38:19 +00:00
|
|
|
}
|
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
|
2011-01-12 10:38:03 +00:00
|
|
|
# drop privileges
|
|
|
|
exec_as_user() {
|
2011-02-03 16:11:08 +00:00
|
|
|
|
|
|
|
if ! [ $SUDO_USER ]; then
|
|
|
|
exec $@[@]
|
|
|
|
return $?
|
|
|
|
fi
|
|
|
|
|
2011-02-07 10:27:53 +00:00
|
|
|
func "exec_as_user '$SUDO_USER': ${(f)@}"
|
2011-02-03 16:11:08 +00:00
|
|
|
which sudo > /dev/null
|
|
|
|
if [ $? = 0 ]; then
|
|
|
|
sudo -u $SUDO_USER "${@[@]}"
|
|
|
|
return $?
|
2011-02-03 19:42:46 +00:00
|
|
|
else
|
|
|
|
error "Tomb requires sudo. please install it."
|
|
|
|
return 1
|
2011-02-03 16:11:08 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# escalate privileges
|
|
|
|
check_priv() {
|
|
|
|
id | grep root > /dev/null
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
which sudo > /dev/null
|
2011-02-20 13:59:30 +00:00
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "Tomb requires sudo. please install it."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
func "Using sudo for root execution of 'tomb ${(f)ARGS}'"
|
2011-02-13 11:29:07 +00:00
|
|
|
# check if sudo has a timestamp active
|
2011-02-20 13:59:30 +00:00
|
|
|
sudok=false
|
|
|
|
sudo -n tomb 2> /dev/null
|
|
|
|
if [ $? != 0 ]; then # if not then ask a password
|
|
|
|
cat <<EOF | pinentry | awk '/^D/ { print $2 }' | sudo -S -v
|
2011-02-14 09:24:31 +00:00
|
|
|
SETTITLE Super user privileges required
|
|
|
|
SETDESC Sudo execution of Tomb ${ARGS[@]}
|
2011-02-13 11:29:07 +00:00
|
|
|
SETPROMPT Insert your USER password:
|
2011-02-14 09:24:31 +00:00
|
|
|
GETPIN
|
|
|
|
EOF
|
2011-02-20 13:59:30 +00:00
|
|
|
fi
|
|
|
|
sudo "tomb" ${(s: :)ARGS}
|
|
|
|
exit $?
|
2011-02-14 09:24:31 +00:00
|
|
|
fi # are we root already
|
2011-02-03 19:42:46 +00:00
|
|
|
return 0
|
2011-01-12 10:38:03 +00:00
|
|
|
}
|
2010-08-22 13:04:19 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
get_arg_tomb() {
|
|
|
|
# set up variables to be used by caller:
|
|
|
|
# tombfile - filename without path
|
|
|
|
# tombdir - directory where the tomb is
|
|
|
|
# tombname - name of the tomb (filename without extension)
|
|
|
|
# the full path is made with $tombdir/$tombfile
|
|
|
|
if [ -z $1 ]; then
|
|
|
|
error "internal: get_arg_tomb called without argument"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
arg=${1}
|
|
|
|
if ! [ -r ${arg} ]; then
|
|
|
|
error "file not found: $arg"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
tombfile=`basename $arg`
|
|
|
|
tombdir=`dirname $arg`
|
|
|
|
|
|
|
|
file ${tombdir}/${tombfile} | grep -i 'luks encrypted file' 2>&1 >/dev/null
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "$arg is not a valid tomb file, operation aborted"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
tombname=${tombfile%%\.*}
|
|
|
|
act "tomb found: ${tombdir}/${tombfile}"
|
|
|
|
# now check if the key is kept beside or in args
|
|
|
|
# we use the extension .key
|
|
|
|
|
|
|
|
# the problem with .tomb.gpg is that decoding by hand using gpg it
|
|
|
|
# can override the tomb contents if the key is in the same
|
|
|
|
# directory than the tomb
|
|
|
|
if [ $KEY ]; then
|
|
|
|
tombkey=$KEY # commandline -k flag
|
|
|
|
act "tomb key specified manually: $tombkey"
|
2011-02-20 19:10:08 +00:00
|
|
|
elif [ -r ${tombdir}/${tombname}.tomb.key ]; then
|
|
|
|
tombkey=${tombdir}/${tombname}.tomb.key
|
2011-02-20 13:59:30 +00:00
|
|
|
act "key found for tomb '${tombname}': ${tombkey}"
|
|
|
|
else
|
|
|
|
error "key not found for tomb '${tombname}'"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2010-08-22 13:04:19 +00:00
|
|
|
|
|
|
|
############################
|
|
|
|
### main()
|
|
|
|
###
|
|
|
|
|
2011-02-07 10:27:53 +00:00
|
|
|
echo $@ | grep '\-D' 2>&1 > /dev/null
|
|
|
|
if [ $? = 0 ]; then
|
|
|
|
fi
|
|
|
|
|
2011-02-03 16:11:08 +00:00
|
|
|
ARGS=$@[@]
|
2010-08-22 13:04:19 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
OPTS=`getopt -o hvqDs:k:n -n 'tomb' -- "$@"`
|
2010-08-22 13:04:19 +00:00
|
|
|
while true; do
|
|
|
|
case "$1" in
|
|
|
|
-h)
|
2011-02-20 13:59:30 +00:00
|
|
|
cat <<EOF
|
|
|
|
Tomb $VERSION - a strong and gentle undertaker for your secrets
|
2011-02-12 16:54:53 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
Copyright (C) 2007-2011 Dyne.org Foundation, License GNU GPL v3+
|
|
|
|
This is free software: you are free to change and redistribute it
|
|
|
|
The latest Tomb sourcecode is published on <http://tomb.dyne.org>
|
|
|
|
|
|
|
|
Syntax: tomb [options] command [file] [place]
|
|
|
|
|
|
|
|
Commands:
|
|
|
|
|
|
|
|
create create a new tomb FILE and its keys
|
|
|
|
open open an existing tomb FILE on PLACE
|
|
|
|
close closes the tomb open on PLACE
|
|
|
|
bury hide a tomb key FILE inside a jpeg PLACE
|
|
|
|
exhume extract a tomb key FILE from a jpeg PLACE
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-s size of the tomb file when creating one (in MB)
|
|
|
|
-k path to the key to use for opening a tomb
|
|
|
|
-n don't process the hooks found in tomb
|
|
|
|
|
|
|
|
-h print this help
|
|
|
|
-v version information for this tool
|
|
|
|
-q run quietly without printing informations
|
|
|
|
-D print debugging information at runtime
|
|
|
|
|
|
|
|
For more informations on Tomb read the manual: man tomb
|
|
|
|
Please report bugs on <http://bugs.dyne.org>.
|
|
|
|
EOF
|
|
|
|
exit 0 ;;
|
2010-08-22 13:04:19 +00:00
|
|
|
-v)
|
2011-02-20 13:59:30 +00:00
|
|
|
notice "Tomb - simple commandline tool for encrypted storage"
|
|
|
|
act "version $VERSION ($DATE) by Jaromil @ dyne.org"
|
2010-08-23 09:48:21 +00:00
|
|
|
# print out the GPL license in this file
|
2011-01-12 10:38:03 +00:00
|
|
|
act ""
|
2010-08-23 09:48:21 +00:00
|
|
|
cat $0 | awk '
|
|
|
|
BEGIN { license=0 }
|
|
|
|
/^# This source/ { license=1 }
|
|
|
|
{ if(license==1) print " " $0 }
|
|
|
|
/MA 02139, USA.$/ { license=0 }
|
|
|
|
'
|
|
|
|
act ""
|
|
|
|
exit 0 ;;
|
2011-02-03 21:17:06 +00:00
|
|
|
-q) QUIET=1; shift 1 ;;
|
2011-02-20 13:59:30 +00:00
|
|
|
-D)
|
|
|
|
echo "[D] Tomb invoked with args \"${(f)@}\" "
|
|
|
|
echo "[D] running on `date`"
|
|
|
|
DEBUG=1; shift 1 ;;
|
2010-08-22 13:04:19 +00:00
|
|
|
-s) SIZE=$2; shift 2 ;;
|
|
|
|
-k) KEY=$2; shift 2 ;;
|
2011-02-20 13:59:30 +00:00
|
|
|
-b) NOBIND=1; shift 1 ;;
|
2010-08-22 13:04:19 +00:00
|
|
|
--) shift; break ;;
|
2011-01-19 11:54:43 +00:00
|
|
|
*) CMD=$1;
|
|
|
|
FILE=$2; MOUNT=$3; # compat with old args
|
|
|
|
CMD2=${2}; CMD3=${3}; break ;;
|
2010-08-22 13:04:19 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2010-09-16 12:51:06 +00:00
|
|
|
|
2011-02-03 16:11:08 +00:00
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
if ! [ $CMD ]; then
|
2010-08-22 13:04:19 +00:00
|
|
|
error "first argument missing, use -h for help"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
func "Tomb command: $CMD $CMD2 $CMD3"
|
2011-01-13 13:37:52 +00:00
|
|
|
|
2010-08-22 14:44:35 +00:00
|
|
|
create_tomb() {
|
2010-08-22 13:04:19 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
if ! [ ${CMD2} ]; then
|
|
|
|
error "no tomb name specified for creation"
|
|
|
|
return 1
|
|
|
|
fi
|
2011-02-03 16:11:08 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
tombfile=`basename ${CMD2}`
|
|
|
|
tombdir=`dirname ${CMD2}`
|
|
|
|
# make sure the file has a .tomb extension
|
|
|
|
tombname=${tombfile%%\.*}
|
|
|
|
tombfile=${tombname}.tomb
|
|
|
|
|
|
|
|
if [ -e ${tombdir}/${tombfile} ]; then
|
|
|
|
error "tomb exists already. I'm not digging here:"
|
|
|
|
ls -lh ${tombdir}/${tombfile}
|
2011-02-03 19:42:46 +00:00
|
|
|
return 1
|
2011-01-30 22:25:01 +00:00
|
|
|
fi
|
2011-02-03 16:11:08 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
notice "Creating a new tomb in ${tombdir}/${tombfile}"
|
|
|
|
|
2010-08-22 13:04:19 +00:00
|
|
|
if [ -z $SIZE ]; then
|
2011-02-20 13:59:30 +00:00
|
|
|
if [ $CMD3 ]; then
|
|
|
|
tombsize=${CMD3}
|
2010-08-29 12:56:53 +00:00
|
|
|
else
|
2011-01-28 11:26:35 +00:00
|
|
|
act "No size specified, summoning the Tomb Undertaker to guide us in the creation."
|
2011-02-03 19:42:46 +00:00
|
|
|
tomb-open &!
|
|
|
|
return 0
|
2010-08-29 12:56:53 +00:00
|
|
|
fi
|
2011-02-20 13:59:30 +00:00
|
|
|
else
|
|
|
|
tombsize=${SIZE}
|
2010-08-22 13:04:19 +00:00
|
|
|
fi
|
2011-01-28 11:26:35 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
tombsize_4k=`expr $tombsize \* 1000 / 4`
|
|
|
|
act "Generating ${tombfile} of ${tombsize}Mb (${tombsize_4k} blocks of 4Kb)"
|
|
|
|
$DD if=/dev/urandom bs=4k count=${tombsize_4k} of=${tombdir}/${tombfile}
|
2010-08-22 13:04:19 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
if [ $? = 0 -a -e ${tombdir}/${tombfile} ]; then
|
|
|
|
act "OK: `ls -lh ${tombdir}/${tombfile}`"
|
2010-08-22 13:04:19 +00:00
|
|
|
else
|
2011-02-20 13:59:30 +00:00
|
|
|
error "Error creating the tomb ${tombdir}/${tombfile}, operation aborted."
|
2011-01-28 11:26:35 +00:00
|
|
|
exit 1
|
2010-08-22 13:04:19 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
nstloop=`losetup -f` # get the number for next loopback device
|
2011-02-20 13:59:30 +00:00
|
|
|
losetup -f ${tombdir}/${tombfile} # allocates the next loopback for our file
|
2011-02-03 16:11:08 +00:00
|
|
|
|
|
|
|
# create the keyfile in tmpfs so that we leave less traces in RAM
|
|
|
|
keytmp=`tempfile -p tomb`
|
|
|
|
rm -f $keytmp
|
|
|
|
mkdir -p $keytmp
|
|
|
|
mount tmpfs ${keytmp} -t tmpfs -o size=1m
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "cannot mount tmpfs filesystem in volatile memory"
|
|
|
|
error "operation aborted."
|
|
|
|
losetup -d $nstloop
|
|
|
|
rm -r $keytmp
|
|
|
|
exit 1
|
|
|
|
fi
|
2010-08-22 13:04:19 +00:00
|
|
|
act "Generating secret key..."
|
2011-01-13 13:37:52 +00:00
|
|
|
act "this operation takes time, keep using this computer on other tasks,"
|
|
|
|
act "once done you will be asked to choose a password for your tomb."
|
2011-02-03 16:11:08 +00:00
|
|
|
touch ${keytmp}/tomb.tmp
|
|
|
|
chmod 0600 ${keytmp}/tomb.tmp
|
2011-02-10 11:22:11 +00:00
|
|
|
$DD bs=1 count=256 if=/dev/random of=${keytmp}/tomb.tmp
|
2011-02-03 16:11:08 +00:00
|
|
|
if ! [ -r ${keytmp}/tomb.tmp ]; then
|
|
|
|
error "cannot generate encryption key, operation aborted."
|
|
|
|
umount ${keytmp}
|
|
|
|
losetup -d $nstloop
|
|
|
|
rm -r $keytmp
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2011-02-20 19:10:08 +00:00
|
|
|
notice "Setup your secret key file ${tombname}.tomb.key"
|
2011-02-03 16:11:08 +00:00
|
|
|
|
2010-08-29 12:56:53 +00:00
|
|
|
# here user is prompted for key password
|
2011-02-03 16:11:08 +00:00
|
|
|
for c in 1 2 3; do
|
|
|
|
# 3 tries to write two times a matching password
|
2011-02-20 13:59:30 +00:00
|
|
|
tombpass=`exec_as_user tomb askpass ${tombname}`
|
2011-02-14 09:24:31 +00:00
|
|
|
tombpasstmp=$tombpass
|
2011-02-20 13:59:30 +00:00
|
|
|
tombpass=`exec_as_user tomb askpass "${tombname} (again)"`
|
2011-02-14 09:24:31 +00:00
|
|
|
if [ "$tombpasstmp" = "$tombpass" ]; then
|
2011-02-03 16:11:08 +00:00
|
|
|
break;
|
|
|
|
fi
|
2011-02-14 09:24:31 +00:00
|
|
|
unset tombpasstmp
|
|
|
|
unset tombpass
|
2010-08-22 13:04:19 +00:00
|
|
|
done
|
2011-02-03 16:11:08 +00:00
|
|
|
|
2011-02-14 09:24:31 +00:00
|
|
|
if [ -z $tombpass ]; then
|
2011-02-03 16:11:08 +00:00
|
|
|
error "passwords don't match, aborting operation"
|
|
|
|
umount ${keytmp}
|
|
|
|
losetup -d $nstloop
|
|
|
|
rm -r $keytmp
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2011-02-22 17:30:42 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
echo "${tombpass}" | gpg \
|
2011-02-22 17:30:42 +00:00
|
|
|
--openpgp --batch --no-options --no-tty --passphrase-fd 0 2>/dev/null \
|
2011-02-20 19:10:08 +00:00
|
|
|
-o "${tombdir}/${tombname}.tomb.key" -c -a ${keytmp}/tomb.tmp
|
2011-02-14 09:24:31 +00:00
|
|
|
|
2011-02-22 17:30:42 +00:00
|
|
|
# if [ $? != 0 ]; then
|
|
|
|
# error "setting password failed: gnupg returns 2"
|
|
|
|
# umount ${keytmp}
|
|
|
|
# losetup -d $nstloop
|
|
|
|
# rm -r $keytmp
|
|
|
|
# exit 1
|
|
|
|
# fi
|
2010-08-22 13:04:19 +00:00
|
|
|
|
|
|
|
act "formatting Luks mapped device"
|
2011-02-03 16:11:08 +00:00
|
|
|
# we use aes-cbc-essiv with sha256
|
|
|
|
# for security, performance and compatibility
|
2010-08-29 12:56:53 +00:00
|
|
|
cryptsetup --batch-mode \
|
|
|
|
--cipher aes-cbc-essiv:sha256 --key-size 256 \
|
2011-02-03 16:11:08 +00:00
|
|
|
luksFormat ${nstloop} ${keytmp}/tomb.tmp
|
2010-08-29 12:56:53 +00:00
|
|
|
|
2010-08-22 13:04:19 +00:00
|
|
|
if ! [ $? = 0 ]; then
|
|
|
|
act "operation aborted."
|
|
|
|
exit 0
|
|
|
|
fi
|
2011-02-09 19:22:39 +00:00
|
|
|
|
2011-02-03 16:11:08 +00:00
|
|
|
cryptsetup --key-file ${keytmp}/tomb.tmp --cipher aes luksOpen ${nstloop} tomb.tmp
|
|
|
|
${WIPE[@]} ${keytmp}/tomb.tmp
|
|
|
|
umount ${keytmp}
|
|
|
|
rm -r ${keytmp}
|
2011-01-13 13:37:52 +00:00
|
|
|
|
2011-02-09 19:22:39 +00:00
|
|
|
# cryptsetup luksDump ${nstloop}
|
|
|
|
|
|
|
|
act "formatting your Tomb with Ext4 filesystem"
|
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
mkfs.ext4 -q -F -j -L ${tombname} /dev/mapper/tomb.tmp
|
2011-02-09 19:22:39 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "Tomb format returns error"
|
|
|
|
error "your tomb ${tombfile} maybe corrupt"
|
2011-02-09 19:22:39 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
sync
|
|
|
|
|
|
|
|
cryptsetup luksClose tomb.tmp
|
|
|
|
losetup -d ${nstloop}
|
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
act "done creating $tombname encrypted storage (using Luks dm-crypt AES/SHA256)"
|
2011-02-20 19:10:08 +00:00
|
|
|
notice "Your tomb is ready in ${tombdir}/${tombfile} and secured with key ${tombname}.tomb.key"
|
2010-08-22 13:04:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-08-22 14:44:35 +00:00
|
|
|
|
|
|
|
mount_tomb() {
|
2011-02-20 13:59:30 +00:00
|
|
|
get_arg_tomb $CMD2
|
2011-02-03 19:42:46 +00:00
|
|
|
if [ $? != 0 ]; then
|
2011-02-20 13:59:30 +00:00
|
|
|
error "operation aborted."
|
2011-02-03 19:42:46 +00:00
|
|
|
return 1
|
2010-08-22 13:04:19 +00:00
|
|
|
fi
|
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
if ! [ $CMD3 ]; then
|
2011-02-14 09:24:31 +00:00
|
|
|
tombmount=/media/${tombfile}
|
2011-02-03 19:42:46 +00:00
|
|
|
act "mountpoint not specified, using default: $tombmount"
|
|
|
|
elif ! [ -x $CMD3 ]; then
|
2011-02-11 16:10:56 +00:00
|
|
|
error "mountpoint $CMD3 doesn't exist, operation aborted."
|
2011-02-11 16:45:54 +00:00
|
|
|
if [ -n "$usbkey_mount" ]; then
|
|
|
|
umount $usbkey_mount
|
|
|
|
rmdir $usbkey_mount
|
2011-02-14 09:24:31 +00:00
|
|
|
unset usbkey_mount
|
2011-02-11 16:45:54 +00:00
|
|
|
fi
|
2011-02-03 19:42:46 +00:00
|
|
|
return 1
|
|
|
|
else
|
|
|
|
tombmount=$CMD3
|
|
|
|
fi
|
|
|
|
|
|
|
|
notice "mounting $tombfile on mountpoint $tombmount"
|
|
|
|
|
|
|
|
# we need root from here on
|
2011-02-24 11:26:48 +00:00
|
|
|
mkdir -p $tombmount
|
2011-01-11 09:49:44 +00:00
|
|
|
|
2010-08-22 13:04:19 +00:00
|
|
|
nstloop=`losetup -f`
|
2011-02-11 23:36:21 +00:00
|
|
|
losetup -f ${tombdir}/${tombfile}
|
2010-08-22 13:04:19 +00:00
|
|
|
|
|
|
|
act "check for a valid LUKS encrypted device"
|
|
|
|
cryptsetup isLuks ${nstloop}
|
2011-01-11 09:49:44 +00:00
|
|
|
if [ $? != 0 ]; then
|
|
|
|
# is it a LUKS encrypted nest? see cryptsetup(1)
|
2011-02-03 19:42:46 +00:00
|
|
|
error "$tombfile is not a valid Luks encrypted storage file"
|
2011-02-12 07:30:37 +00:00
|
|
|
$norm || rmdir $tombmount 2>/dev/null
|
2011-02-03 19:42:46 +00:00
|
|
|
return 1
|
2011-01-11 09:49:44 +00:00
|
|
|
fi
|
2011-02-22 17:30:42 +00:00
|
|
|
|
2011-01-11 09:49:44 +00:00
|
|
|
# save date of mount in minutes since 1970
|
|
|
|
mapdate="`date +%s`"
|
|
|
|
mapdate="`echo ${mapdate}/60 | bc -l | cut -d. -f1`"
|
|
|
|
|
2011-02-11 23:36:21 +00:00
|
|
|
mapper="tomb.${tombname}.${mapdate}.`basename $nstloop`"
|
2011-02-20 13:59:30 +00:00
|
|
|
|
2011-02-22 17:30:42 +00:00
|
|
|
keyname=`basename $tombkey | cut -d. -f1`
|
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
notice "Password is required for key ${keyname}"
|
2011-01-11 09:49:44 +00:00
|
|
|
for c in 1 2 3; do
|
2010-08-22 13:04:19 +00:00
|
|
|
|
2011-02-03 16:11:08 +00:00
|
|
|
if [ $c = 1 ]; then
|
2011-02-20 13:59:30 +00:00
|
|
|
tombpass=`exec_as_user tomb askpass ${keyname}`
|
2011-02-03 16:11:08 +00:00
|
|
|
else
|
2011-02-20 13:59:30 +00:00
|
|
|
tombpass=`exec_as_user tomb askpass "$keyname (retry $c)"`
|
2011-02-03 16:11:08 +00:00
|
|
|
fi
|
2011-02-14 09:24:31 +00:00
|
|
|
echo "${tombpass}" \
|
2011-02-03 16:11:08 +00:00
|
|
|
| gpg --batch --passphrase-fd 0 --no-tty --no-options \
|
2011-02-22 17:30:42 +00:00
|
|
|
-d "${tombkey}" 2> /dev/null \
|
2011-01-11 09:49:44 +00:00
|
|
|
| cryptsetup --key-file - luksOpen ${nstloop} ${mapper}
|
2010-08-22 13:04:19 +00:00
|
|
|
|
2011-02-14 09:24:31 +00:00
|
|
|
unset tombpass
|
2011-01-11 09:49:44 +00:00
|
|
|
|
|
|
|
if [ -r /dev/mapper/${mapper} ]; then
|
|
|
|
break; # password was correct
|
2010-08-22 13:04:19 +00:00
|
|
|
fi
|
|
|
|
|
2011-01-11 09:49:44 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
if ! [ -r /dev/mapper/${mapper} ]; then
|
|
|
|
error "failure mounting the encrypted file"
|
|
|
|
losetup -d ${nstloop}
|
2011-02-12 07:30:37 +00:00
|
|
|
$norm || rmdir ${tombmount} 2>/dev/null
|
2011-02-03 19:42:46 +00:00
|
|
|
return 1
|
2011-01-11 09:49:44 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
act "encrypted storage filesystem check"
|
2011-01-13 21:35:32 +00:00
|
|
|
fsck -p -C0 /dev/mapper/${mapper}
|
2011-02-11 23:36:21 +00:00
|
|
|
act "tomb engraved as $tombname"
|
2011-02-14 09:24:31 +00:00
|
|
|
tune2fs -L ${tombname} /dev/mapper/${mapper} > /dev/null
|
2011-02-11 23:36:21 +00:00
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
mount -o rw,noatime,nodev /dev/mapper/${mapper} ${tombmount}
|
2011-01-30 22:25:01 +00:00
|
|
|
|
2011-02-03 16:11:08 +00:00
|
|
|
# Ensure the user can write the disk - 10x Hellekin :)
|
2011-01-30 22:25:01 +00:00
|
|
|
ME=${SUDO_USER:-$(whoami)}
|
2011-02-03 19:42:46 +00:00
|
|
|
chmod 0750 ${tombmount}
|
|
|
|
chown $(id -u $ME):$(id -g $ME) ${tombmount}
|
2011-01-11 09:49:44 +00:00
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
notice "encrypted storage $tombfile succesfully mounted on $tombmount"
|
2011-02-12 11:38:59 +00:00
|
|
|
# exec_bind_hooks ${tombmount}
|
2011-02-20 13:59:30 +00:00
|
|
|
if ! [ $NOBIND ]; then
|
|
|
|
exec_safe_bind_hooks ${tombmount}
|
|
|
|
exec_post_hooks ${tombmount} open
|
|
|
|
fi
|
2011-02-03 19:42:46 +00:00
|
|
|
return 0
|
2010-08-22 13:04:19 +00:00
|
|
|
}
|
|
|
|
|
2011-02-11 23:36:21 +00:00
|
|
|
encode_key() {
|
|
|
|
tombkey=$CMD2
|
|
|
|
imagefile=$CMD3
|
|
|
|
|
|
|
|
file $tombkey | grep PGP > /dev/null
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "encode failed: $tombkey is not a tomb key"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
file $imagefile | grep JPEG > /dev/null
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "encode failed: $imagefile is not a jpeg image"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
notice "Encoding key $tombkey inside image $imagefile"
|
|
|
|
act "please choose a password for the encoding"
|
|
|
|
|
|
|
|
# here user is prompted for key password
|
|
|
|
for c in 1 2 3; do
|
|
|
|
# 3 tries to write two times a matching password
|
2011-02-20 13:59:30 +00:00
|
|
|
tombpass=`exec_as_user tomb askpass ${tombkey}`
|
2011-02-14 09:24:31 +00:00
|
|
|
tombpasstmp=$tombpass
|
2011-02-20 13:59:30 +00:00
|
|
|
tombpass=`exec_as_user tomb askpass "${tombkey} (again)"`
|
2011-02-14 09:24:31 +00:00
|
|
|
if [ "$tombpasstmp" = "$tombpass" ]; then
|
2011-02-11 23:36:21 +00:00
|
|
|
break;
|
|
|
|
fi
|
2011-02-14 09:24:31 +00:00
|
|
|
unset tombpasstmp
|
|
|
|
unset tombpass
|
2011-02-11 23:36:21 +00:00
|
|
|
done
|
|
|
|
|
2011-02-14 09:24:31 +00:00
|
|
|
if [ -z $tombpass ]; then
|
2011-02-11 23:36:21 +00:00
|
|
|
error "passwords don't match, aborting operation."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
awk '
|
|
|
|
/^-----/ {next}
|
|
|
|
/^Version/ {next}
|
|
|
|
{print $0}' ${tombkey} \
|
|
|
|
| steghide embed --embedfile - --coverfile ${imagefile} \
|
2011-02-14 09:24:31 +00:00
|
|
|
-p ${tombpass} -z 9 -e serpent cbc
|
2011-02-11 23:36:21 +00:00
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "encoding error: steghide reports problems"
|
|
|
|
res=1
|
|
|
|
else
|
|
|
|
notice "tomb key encoded succesfully into image ${imagefile}"
|
|
|
|
res=0
|
|
|
|
fi
|
|
|
|
|
2011-02-14 09:24:31 +00:00
|
|
|
unset tombpass
|
2011-02-11 23:36:21 +00:00
|
|
|
|
|
|
|
return $res
|
|
|
|
}
|
|
|
|
|
|
|
|
decode_key() {
|
|
|
|
tombname=$CMD2
|
|
|
|
imagefile=$CMD3
|
|
|
|
res=1
|
|
|
|
|
|
|
|
file $imagefile | grep JPEG > /dev/null
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "encode failed: $imagefile is not a jpeg image"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2011-02-20 19:10:08 +00:00
|
|
|
keyfile=${tombname%%\.*}.tomb.key
|
2011-02-20 13:59:30 +00:00
|
|
|
notice "Trying to exhume a key out of image $imagefile"
|
2011-02-11 23:36:21 +00:00
|
|
|
for c in 1 2 3; do
|
|
|
|
if [ $c = 1 ]; then
|
2011-02-20 13:59:30 +00:00
|
|
|
tombpass=`exec_as_user tomb askpass ${keyfile}`
|
2011-02-11 23:36:21 +00:00
|
|
|
else
|
2011-02-20 13:59:30 +00:00
|
|
|
tombpass=`exec_as_user tomb askpass "$keyfile (retry $c)"`
|
2011-02-11 23:36:21 +00:00
|
|
|
fi
|
2011-02-14 09:24:31 +00:00
|
|
|
steghide extract -sf ${imagefile} -p ${tombpass} -xf - \
|
2011-02-11 23:36:21 +00:00
|
|
|
| awk '
|
|
|
|
BEGIN {
|
|
|
|
print "-----BEGIN PGP MESSAGE-----"
|
|
|
|
}
|
|
|
|
{ print $0 }
|
|
|
|
END {
|
|
|
|
print "-----END PGP MESSAGE-----"
|
2011-02-20 13:59:30 +00:00
|
|
|
}' > ${keyfile}
|
2011-02-11 23:36:21 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
if [ "`cat ${keyfile} | wc -l`" != "3" ]; then
|
|
|
|
act "${keyfile} succesfully decoded"
|
2011-02-11 23:36:21 +00:00
|
|
|
res=0
|
|
|
|
break;
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2011-02-14 09:24:31 +00:00
|
|
|
unset tombpass
|
2011-02-11 23:36:21 +00:00
|
|
|
|
|
|
|
if [ $res != 0 ]; then
|
|
|
|
error "nothing found."
|
|
|
|
fi
|
|
|
|
|
|
|
|
return $res
|
|
|
|
}
|
|
|
|
|
2011-02-07 08:42:50 +00:00
|
|
|
exec_bind_hooks() {
|
|
|
|
mnt=$1 # first argument is where the tomb is mounted
|
|
|
|
if ! [ -r ${mnt}/bind-hooks ]; then return; fi
|
|
|
|
|
|
|
|
# if 'bind-hooks' is found inside the tomb, parse it
|
|
|
|
# every line contains two strings:
|
|
|
|
# the first is a directory existing inside the tomb
|
|
|
|
# the second is the place where it should be mounted (-o bind)
|
|
|
|
hook=`cat ${mnt}/bind-hooks | awk '
|
|
|
|
/^#/ { next }
|
|
|
|
{ if($1 && $2) print "mount -o bind \${mnt}/" $1 " " $2 "; " }
|
|
|
|
'`
|
|
|
|
# restore $HOME for the calling user
|
2011-02-20 13:59:30 +00:00
|
|
|
ME=${SUDO_USER:-$(whoami)}
|
|
|
|
HOME=$(grep $ME /etc/passwd | sed "s/^${ME}:.*:.*:.*:.*:\([\/a-z]*\):.*$/\1/" 2>/dev/null)
|
2011-02-07 08:42:50 +00:00
|
|
|
|
2011-02-11 16:10:56 +00:00
|
|
|
act "bind hooks found, mounting directories as requested"
|
2011-02-07 08:42:50 +00:00
|
|
|
# execute the mount commands
|
|
|
|
eval $hook
|
|
|
|
}
|
|
|
|
|
2011-02-12 11:38:59 +00:00
|
|
|
# FIXME: this should sanitize pathes!
|
|
|
|
exec_safe_bind_hooks() {
|
|
|
|
local MOUNTPOINT="${1}"
|
|
|
|
local ME=${SUDO_USER:-$(whoami)}
|
|
|
|
local HOME=$(grep $ME /etc/passwd | sed "s/^${ME}:.*:.*:.*:.*:\([\/a-z]*\):.*$/\1/" 2>/dev/null)
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
error "how pitiful! A tomb, and no HOME"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ -z "$MOUNTPOINT" -o ! -d "$MOUNTPOINT" ]; then
|
|
|
|
error "cannot exec bind hooks without a mounted tomb."
|
|
|
|
return 1
|
|
|
|
fi
|
2011-02-20 13:59:30 +00:00
|
|
|
if ! [ -r "$MOUNTPOINT/bind-hooks" ]; then
|
|
|
|
func "bind-hooks not found in $MOUNTPOINT"
|
|
|
|
return 1
|
2011-02-12 11:38:59 +00:00
|
|
|
fi
|
|
|
|
typeset -al created
|
|
|
|
typeset -al mounted
|
|
|
|
typeset -Al maps
|
|
|
|
maps=($(<"$MOUNTPOINT/bind-hooks"))
|
|
|
|
for dir in ${(k)maps}; do
|
|
|
|
if [ "${dir[1]}" = "/" -o "${dir[1,2]}" = ".." ]; then
|
|
|
|
error "bind-hooks map format: local/to/tomb local/to/\$HOME"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
if [ "${${maps[$dir]}[1]}" = "/" -o "${${maps[$dir]}[1,2]}" = ".." ]; then
|
|
|
|
error "bind-hooks map format: local/to/tomb local/to/\$HOME. Rolling back"
|
|
|
|
for dir in ${mounted}; do umount $dir; done
|
|
|
|
for dir in ${created}; do rmdir $dir; done
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ ! -d "$HOME/${maps[$dir]}" ]; then
|
2011-02-20 13:59:30 +00:00
|
|
|
act "creating $HOME/${maps[$dir]}"
|
2011-02-12 11:38:59 +00:00
|
|
|
mkdir -p $HOME/${maps[$dir]}
|
|
|
|
created+=("$HOME/${maps[$dir]}")
|
|
|
|
fi
|
|
|
|
mount --bind $MOUNTPOINT/$dir $HOME/${maps[$dir]}
|
|
|
|
mounted+=("$HOME/${maps[$dir]}")
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-02-07 10:56:11 +00:00
|
|
|
exec_post_hooks() {
|
|
|
|
mnt=$1 # first argument is where the tomb is mounted
|
|
|
|
if ! [ -x ${mnt}/post-hooks ]; then return; fi
|
|
|
|
# if 'post-hooks' is found inside the tomb, check it: if it is an
|
|
|
|
# executable, launch it as a user this might need a dialog for
|
|
|
|
# security on what is being run, however we expect you know well
|
|
|
|
# what is inside your tomb. this feature opens the possibility to
|
|
|
|
# make encrypted executables.
|
|
|
|
cat ${mnt}/post-hooks | head -n1 | grep '^#!/'
|
|
|
|
if [ $? = 0 ]; then
|
|
|
|
act "post hooks found, executing as user $SUDO_USER"
|
2011-02-12 11:38:59 +00:00
|
|
|
exec_as_user ${mnt}/post-hooks $2
|
2011-02-07 10:56:11 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2011-02-14 14:57:37 +00:00
|
|
|
|
|
|
|
backup_tomb() { # FIXME - duplicity asks passwords too often
|
|
|
|
# using duplicity
|
|
|
|
which duplicity > /dev/null
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "duplicity not found, can't operate backup"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
if [ -z $CMD3 ]; then
|
|
|
|
error "backup command needs 2 arguments: tomb and destination url"
|
|
|
|
error "please refer to tomb(1) and duplicity(1) manuals for more information"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# is it a tomb?
|
|
|
|
get_arg_tomb ${CMD2}
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "there is no tomb to backup, operation aborted."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# is it a url?
|
|
|
|
echo "${CMD3}" | grep -i -e '^.*:\/\/.*' 2>&1 > /dev/null
|
|
|
|
if ! [ $? = 0 ]; then
|
|
|
|
error "second argument is not a valid duplicity url."
|
|
|
|
error "read the tomb(1) and duplicity(1) manual for more information"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
bckurl=${CMD3}
|
|
|
|
|
|
|
|
# is it ssh?
|
|
|
|
protocol="`expr substr $bckurl 1 3`"
|
|
|
|
act "backup over protocol $protocol"
|
|
|
|
if [ "$protocol" = "ssh" ]; then
|
|
|
|
act "ssh connection requires a password"
|
|
|
|
FTP_PASSWORD="`exec_as_user tomb askpass $bckurl`"
|
|
|
|
dupopts="--ssh-askpass"
|
2011-02-20 13:59:30 +00:00
|
|
|
# TODO verify ssh access before duplicity does
|
|
|
|
# since it blocks the thing retrying 5 times and such crap
|
|
|
|
# i.e. try ssh true to sshurl="`echo $bckurl | sed -e 's/ssh:\/\///'`"
|
|
|
|
# --no-print-statistics
|
2011-02-14 14:57:37 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# duplicity works only on directories
|
|
|
|
# so we create a directory in tmpfs and bind the tomb inside it
|
|
|
|
# during backup the encrypted tomb will be exposed
|
|
|
|
# TODO: check that the tomb is not mounted and, if mounted
|
|
|
|
# remount it read-only so it doesn't gets modified during bck
|
|
|
|
bckname=${tombname}.bck
|
|
|
|
mkdir -p /dev/shm/${bckname}
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "cannot generate a temporary backup directory in /dev/shm, operation aborted."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
bcktmpdir=/dev/shm/${bckname}
|
|
|
|
# mmm, maybe we should mount our own tmpfs? we need root anyway for mount -o bind
|
|
|
|
# if we reach to eliminate this mount trick and upload only one file with duplicity
|
|
|
|
# then this function doesn't needs to be root to work.
|
|
|
|
touch ${bcktmpdir}/${tombfile}
|
|
|
|
mount -o bind ${tombdir}/${tombfile} ${bcktmpdir}/${tombfile}
|
|
|
|
bcklast=`exec_as_user duplicity \
|
|
|
|
${(s: :)dupopts} \
|
|
|
|
collection-status ${bckurl} \
|
|
|
|
| awk '/^Last full backup date:/ { print $5 }'`
|
|
|
|
# we detect if backup already exists or not so we can handle
|
|
|
|
# password prompt (choosing a password for full, inserting for incr)
|
|
|
|
if [ "$bcklast" = "none" ]; then
|
|
|
|
notice "Creating a backup of tomb $tombname on url $bckurl"
|
|
|
|
exec_as_user FTP_PASSWORD="$FTP_PASSWORD" duplicity ${(s: :)dupopts} \
|
|
|
|
full ${bcktmpdir} ${bckurl}
|
|
|
|
else
|
|
|
|
notice "Updating a backup of tomb $tombname on url $bckurl"
|
|
|
|
exec_as_user FTP_PASSWORD="$FTP_PASSWORD" duplicity ${(s: :)dupopts} \
|
|
|
|
incr ${bcktmpdir} ${bckurl}
|
|
|
|
fi
|
|
|
|
unset FTP_PASSWORD
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "duplicity reported error, operation aborted"
|
|
|
|
umount ${bcktmpdir}/${tombfile}
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
notice "Operation successful."
|
|
|
|
umount ${bcktmpdir}/${tombfile}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2010-08-22 14:44:35 +00:00
|
|
|
umount_tomb() {
|
2010-08-22 13:04:19 +00:00
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
if ! [ $1 ]; then
|
|
|
|
|
|
|
|
how_many_tombs="`find /dev/mapper -name 'tomb.*' | wc -w`"
|
|
|
|
if [ "$how_many_tombs" = "0" ]; then
|
2011-01-12 10:38:03 +00:00
|
|
|
error "there is no open tomb to be closed"
|
2011-02-03 19:42:46 +00:00
|
|
|
return 1
|
2011-02-24 21:59:54 +00:00
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
elif [ "$how_many_tombs" = "1" ]; then
|
|
|
|
mapper=`find /dev/mapper -name 'tomb.*'`
|
2011-02-24 21:59:54 +00:00
|
|
|
notice "trying to close $mapper"
|
|
|
|
umount_tomb ${mapper}
|
|
|
|
return 1
|
|
|
|
|
2010-08-25 18:17:53 +00:00
|
|
|
else
|
|
|
|
error "too many tombs mounted, please specify which to unmount:"
|
2011-01-30 22:25:01 +00:00
|
|
|
ls /dev/mapper/tomb.*
|
2011-02-03 19:42:46 +00:00
|
|
|
error "or issue the command 'tomb close all' to clos'em all."
|
|
|
|
return 1
|
2010-08-22 13:04:19 +00:00
|
|
|
fi
|
|
|
|
|
2011-02-03 16:11:08 +00:00
|
|
|
fi
|
2010-08-22 13:04:19 +00:00
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
if [ "$1" = "all" ]; then
|
|
|
|
tombs=`find /dev/mapper -name 'tomb.*'`
|
|
|
|
if ! [ $tombs ]; then
|
2011-02-20 13:59:30 +00:00
|
|
|
notice "Tombs are all closed, cemetery is quiet."
|
|
|
|
return 0
|
2011-02-03 19:42:46 +00:00
|
|
|
fi
|
|
|
|
for t in ${(f)tombs}; do
|
|
|
|
umount_tomb ${t}
|
|
|
|
done
|
|
|
|
return 0
|
2010-08-22 13:04:19 +00:00
|
|
|
fi
|
2011-02-03 19:42:46 +00:00
|
|
|
|
2011-02-24 21:59:54 +00:00
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
if [ -r "$1" ]; then # accepts relative and absolute path
|
|
|
|
mapper="$1"
|
|
|
|
elif [ -r /dev/mapper/${1} ]; then
|
|
|
|
mapper=/dev/mapper/${1}
|
|
|
|
else
|
|
|
|
error "tomb not found: $1"
|
2011-02-03 16:11:08 +00:00
|
|
|
error "please specify an existing /dev/mapper/tomb.*"
|
2011-02-03 19:42:46 +00:00
|
|
|
return 0
|
2011-02-03 16:11:08 +00:00
|
|
|
fi
|
|
|
|
|
2011-01-13 13:37:52 +00:00
|
|
|
basemap=`basename $mapper`
|
2011-01-13 21:35:32 +00:00
|
|
|
tombname=`echo ${basemap} | cut -d. -f2`
|
2011-02-07 08:42:50 +00:00
|
|
|
tombmount=`mount | grep $mapper | awk '{print $3}'`
|
|
|
|
|
|
|
|
# check if there are binded dirs and close them first
|
2011-02-12 11:48:16 +00:00
|
|
|
mount | grep "${tombmount}" 2>/dev/null | grep -v loop 2>&1 > /dev/null
|
2011-02-07 08:42:50 +00:00
|
|
|
if [ $? = 0 ]; then
|
2011-02-07 10:27:53 +00:00
|
|
|
act "closing bind hooks for tomb $tombname "
|
2011-02-07 08:42:50 +00:00
|
|
|
unbind=`mount | grep ${tombmount} | grep -v loop | awk '
|
|
|
|
{ print "umount " $3 "; " }
|
|
|
|
'`
|
|
|
|
eval $unbind
|
|
|
|
func "umount binded dirs:"
|
|
|
|
func "$unbind"
|
|
|
|
fi
|
2011-01-13 21:35:32 +00:00
|
|
|
|
2011-02-12 11:38:59 +00:00
|
|
|
# Execute post-hooks for eventual cleanup
|
2011-02-20 13:59:30 +00:00
|
|
|
if ! [ $NOBIND ]; then
|
|
|
|
exec_post_hooks ${tombmount} close
|
|
|
|
fi
|
2011-02-03 16:11:08 +00:00
|
|
|
|
2011-02-07 08:42:50 +00:00
|
|
|
act "closing tomb $tombname on dm-crypt $basemap"
|
2011-02-03 19:42:46 +00:00
|
|
|
mount | grep $mapper 2>&1 >/dev/null
|
2011-02-03 16:11:08 +00:00
|
|
|
if [ $? = 0 ]; then # still mounted
|
2011-02-07 10:27:53 +00:00
|
|
|
umount ${mapper}
|
2011-02-03 16:11:08 +00:00
|
|
|
if ! [ $? = 0 ]; then
|
2011-02-20 13:59:30 +00:00
|
|
|
# TODO: ask user if wanting to SLAM the tomb closed
|
|
|
|
# then kill all processes found using it with fuser and lsof
|
2011-02-03 19:42:46 +00:00
|
|
|
return 1
|
2011-02-03 16:11:08 +00:00
|
|
|
fi
|
2011-01-13 21:35:32 +00:00
|
|
|
fi
|
2010-08-22 13:04:19 +00:00
|
|
|
|
2011-01-13 13:37:52 +00:00
|
|
|
cryptsetup luksClose $basemap
|
2010-08-22 13:04:19 +00:00
|
|
|
if ! [ $? = 0 ]; then
|
2011-01-13 13:37:52 +00:00
|
|
|
error "error occurred in cryptsetup luksClose ${basemap}"
|
2011-02-03 19:42:46 +00:00
|
|
|
return 1
|
2010-08-22 13:04:19 +00:00
|
|
|
fi
|
|
|
|
|
2011-01-13 13:37:52 +00:00
|
|
|
losetup -d "/dev/`echo $basemap | cut -d. -f4`"
|
2010-08-22 14:44:35 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
notice "Tomb $tombname closed: your bones will rest in peace."
|
2011-02-03 19:42:46 +00:00
|
|
|
return 0
|
2010-08-22 13:04:19 +00:00
|
|
|
}
|
2011-01-12 16:02:19 +00:00
|
|
|
|
|
|
|
# install mime-types, bells and whistles for the desktop
|
|
|
|
# see http://developers.sun.com/solaris/articles/integrating_gnome.html
|
|
|
|
# and freedesktop specs
|
2011-02-03 16:11:08 +00:00
|
|
|
install_tomb() {
|
2011-01-19 11:38:19 +00:00
|
|
|
|
|
|
|
# TODO: distro package deps (for binary)
|
2011-02-20 13:59:30 +00:00
|
|
|
# debian: zsh, cryptsetup, sudo
|
2011-01-12 16:02:19 +00:00
|
|
|
act "updating mimetypes..."
|
|
|
|
cat <<EOF > /tmp/dyne-tomb.xml
|
|
|
|
<?xml version="1.0"?>
|
|
|
|
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
|
|
|
|
<mime-type type="application/x-tomb-volume">
|
|
|
|
<comment>Tomb encrypted volume</comment>
|
|
|
|
<glob pattern="*.tomb"/>
|
|
|
|
</mime-type>
|
|
|
|
<mime-type type="application/x-tomb-key">
|
|
|
|
<comment>Tomb crypto key</comment>
|
2011-02-20 19:10:08 +00:00
|
|
|
<glob pattern="*.tomb.key"/>
|
2011-01-12 16:02:19 +00:00
|
|
|
</mime-type>
|
|
|
|
</mime-info>
|
|
|
|
EOF
|
|
|
|
xdg-mime install /tmp/dyne-tomb.xml
|
|
|
|
xdg-icon-resource install --context mimetypes --size 32 monmort.xpm monmort
|
|
|
|
xdg-icon-resource install --size 32 monmort.xpm dyne-monmort
|
2011-01-13 13:37:52 +00:00
|
|
|
|
2011-01-12 16:02:19 +00:00
|
|
|
rm /tmp/dyne-tomb.xml
|
2011-01-13 13:37:52 +00:00
|
|
|
|
2011-01-12 16:02:19 +00:00
|
|
|
act "updating desktop..."
|
|
|
|
cat <<EOF > /usr/share/applications/tomb.desktop
|
|
|
|
[Desktop Entry]
|
|
|
|
Version=1.0
|
|
|
|
Type=Application
|
|
|
|
Name=Tomb crypto undertaker
|
|
|
|
GenericName=Crypto undertaker
|
|
|
|
Comment=Keep your bones safe
|
|
|
|
Exec=tomb-open %U
|
|
|
|
TryExec=tomb-open
|
|
|
|
Icon=monmort.xpm
|
2011-02-20 19:10:08 +00:00
|
|
|
Terminal=true
|
2011-01-12 16:02:19 +00:00
|
|
|
Categories=Utility;Security;Archiving;Filesystem;
|
|
|
|
MimeType=application/x-tomb-volume;
|
2011-01-29 13:45:03 +00:00
|
|
|
X-AppInstall-Package=tomb
|
2011-01-12 16:02:19 +00:00
|
|
|
EOF
|
2011-01-13 13:37:52 +00:00
|
|
|
update-desktop-database
|
2011-01-12 16:02:19 +00:00
|
|
|
|
|
|
|
act "updating menus..."
|
|
|
|
cat <<EOF > /etc/menu/tomb
|
2011-02-20 19:10:08 +00:00
|
|
|
?package(tomb):command="tomb" icon="/usr/share/pixmaps/monmort.xpm" needs="text" \
|
2011-01-12 16:02:19 +00:00
|
|
|
section="Applications/Accessories" title="Tomb" hints="Crypto" \
|
|
|
|
hotkey="Tomb"
|
|
|
|
EOF
|
|
|
|
update-menus
|
|
|
|
|
|
|
|
act "updating mime info..."
|
|
|
|
cat <<EOF > /usr/share/mime-info/tomb.keys
|
|
|
|
# actions for encrypted tomb storage
|
|
|
|
application/x-tomb-volume:
|
|
|
|
open=tomb-open %f
|
|
|
|
view=tomb-open %f
|
|
|
|
icon-filename=monmort.xpm
|
|
|
|
short_list_application_ids_for_novice_user_level=tomb
|
|
|
|
EOF
|
|
|
|
cat <<EOF > /usr/share/mime-info/tomb.mime
|
|
|
|
# mime type for encrypted tomb storage
|
|
|
|
application/x-tomb-volume
|
|
|
|
ext: tomb
|
|
|
|
|
|
|
|
application/x-tomb-key
|
2011-02-20 19:10:08 +00:00
|
|
|
ext: tomb.key
|
2011-01-12 16:02:19 +00:00
|
|
|
EOF
|
|
|
|
cat <<EOF > /usr/lib/mime/packages/tomb
|
2011-01-13 13:37:52 +00:00
|
|
|
application/x-tomb-volume; tomb-open '%s'; priority=8
|
2011-01-12 16:02:19 +00:00
|
|
|
EOF
|
|
|
|
update-mime
|
|
|
|
|
|
|
|
act "updating application entry..."
|
|
|
|
|
|
|
|
cat <<EOF > /usr/share/application-registry/tomb.applications
|
|
|
|
tomb
|
|
|
|
command=tomb-open
|
|
|
|
name=Tomb - Crypto Undertaker
|
|
|
|
can_open_multiple_files=false
|
|
|
|
expects_uris=false
|
2011-02-20 19:10:08 +00:00
|
|
|
requires_terminal=true
|
2011-01-12 16:02:19 +00:00
|
|
|
mime-types=application/x-tomb-volume,application/x-tomb-key
|
|
|
|
EOF
|
2011-01-13 13:37:52 +00:00
|
|
|
act "Tomb is now installed."
|
2011-01-12 16:02:19 +00:00
|
|
|
}
|
2011-02-03 16:11:08 +00:00
|
|
|
|
|
|
|
|
2010-08-22 13:04:19 +00:00
|
|
|
|
|
|
|
case "$CMD" in
|
2011-02-03 16:11:08 +00:00
|
|
|
create) check_priv ; create_tomb ;;
|
2011-01-12 16:02:19 +00:00
|
|
|
|
2011-02-03 16:11:08 +00:00
|
|
|
mount) check_priv ; mount_tomb ;;
|
|
|
|
open) check_priv ; mount_tomb ;;
|
2011-01-12 16:02:19 +00:00
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
umount) check_priv ; umount_tomb ${CMD2} ;;
|
|
|
|
unmount) check_priv ; umount_tomb ${CMD2} ;;
|
|
|
|
close) check_priv ; umount_tomb ${CMD2} ;;
|
2011-01-12 16:02:19 +00:00
|
|
|
|
2011-02-12 16:54:53 +00:00
|
|
|
bury) encode_key ${CMD2} ${CMD3} ;;
|
|
|
|
exhume) decode_key ${CMD2} ;;
|
2011-02-11 23:36:21 +00:00
|
|
|
|
2011-02-14 14:57:37 +00:00
|
|
|
backup) check_priv ; backup_tomb ${CMD2} ${CMD3} ;;
|
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
install) check_priv ; install_tomb ;;
|
2011-01-12 16:02:19 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
askpass) ask_password $CMD2 ;;
|
2011-01-12 16:02:19 +00:00
|
|
|
status) tomb-status ;;
|
|
|
|
|
2010-08-22 13:04:19 +00:00
|
|
|
*) error "command \"$CMD\" not recognized"
|
|
|
|
act "try -h for help"
|
2011-02-07 10:27:53 +00:00
|
|
|
return 1
|
2010-08-22 13:04:19 +00:00
|
|
|
;;
|
|
|
|
esac
|
2011-02-03 19:42:46 +00:00
|
|
|
# return codes from called functions
|
2011-02-14 09:24:31 +00:00
|
|
|
# return $?
|