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-05-26 11:16:02 +00:00
|
|
|
VERSION=1.1
|
2011-05-09 08:32:08 +00:00
|
|
|
DATE=May/2011
|
2011-03-31 17:42:05 +00:00
|
|
|
TOMBEXEC=$0
|
2011-04-10 19:38:01 +00:00
|
|
|
TOMBOPENEXEC="tomb-open"
|
2011-08-02 00:10:31 +00:00
|
|
|
typeset -a OLDARGS
|
|
|
|
for arg in ${argv}; do OLDARGS+=($arg); done
|
2011-04-10 19:38:01 +00:00
|
|
|
STEGHIDE=1
|
2011-05-24 10:04:18 +00:00
|
|
|
MOUNTOPTS="rw,noatime,nodev"
|
2010-08-22 13:04:19 +00:00
|
|
|
|
2011-07-14 13:47:20 +00:00
|
|
|
#declare global variables
|
|
|
|
QUIET=0
|
|
|
|
DEBUG=0
|
|
|
|
typeset -A global_opts
|
|
|
|
typeset -A opts
|
|
|
|
|
2011-04-28 10:14:37 +00:00
|
|
|
# PATH=/usr/bin:/usr/sbin:/bin:/sbin
|
2010-08-29 12:56:53 +00:00
|
|
|
|
2011-05-09 08:32:08 +00:00
|
|
|
autoload colors; colors
|
|
|
|
|
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-07-14 13:47:20 +00:00
|
|
|
notice() { if [[ $QUIET == 0 ]]; then print "$fg_bold[green][*]$fg_no_bold[white] $1" >&2; fi }
|
|
|
|
error() { if [[ $QUIET == 0 ]]; then print "$fg[red][!]$fg[white] $1" >&2; fi }
|
|
|
|
func() { if [[ $DEBUG == 1 ]]; then print "$fg[blue][D]$fg[white] $1" >&2; fi }
|
2011-05-26 10:24:17 +00:00
|
|
|
act() {
|
2011-07-14 13:47:20 +00:00
|
|
|
if [[ $QUIET == 0 ]]; then
|
2011-05-26 10:24:17 +00:00
|
|
|
if [ "$1" = "-n" ]; then
|
|
|
|
print -n "$fg_bold[white] . $fg_no_bold[white] $2" >&2;
|
|
|
|
else
|
|
|
|
print "$fg_bold[white] . $fg_no_bold[white] $1" >&2;
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
2011-04-10 19:38:01 +00:00
|
|
|
|
|
|
|
check_bin() {
|
|
|
|
# which dd command to use
|
|
|
|
which dcfldd > /dev/null
|
|
|
|
if [ $? = 0 ]; then
|
2011-04-28 10:14:37 +00:00
|
|
|
DD="dcfldd"
|
2011-04-10 19:38:01 +00:00
|
|
|
else
|
2011-04-28 10:14:37 +00:00
|
|
|
DD=dd
|
2011-04-10 19:38:01 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# which wipe command to use
|
|
|
|
which wipe > /dev/null
|
|
|
|
if [ $? = 0 ]; then
|
2011-04-28 10:14:37 +00:00
|
|
|
WIPE=(wipe -f -s)
|
2011-04-10 19:38:01 +00:00
|
|
|
else
|
2011-04-28 10:14:37 +00:00
|
|
|
WIPE=(rm -f)
|
2011-04-10 19:38:01 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# check for filesystem creation progs
|
|
|
|
which mkfs.ext4 > /dev/null
|
|
|
|
if [ $? = 0 ]; then
|
2011-04-28 10:14:37 +00:00
|
|
|
MKFS=(mkfs.ext4 -q -F -j -L)
|
2011-04-10 19:38:01 +00:00
|
|
|
else
|
2011-04-28 10:14:37 +00:00
|
|
|
MKFS=(mkfs.ext3 -q -F -j -L)
|
2011-04-10 19:38:01 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# check for sudo
|
|
|
|
which sudo > /dev/null
|
|
|
|
if [ $? != 0 ]; then
|
2011-04-28 10:14:37 +00:00
|
|
|
error "Cannot find sudo. Please install it"
|
|
|
|
exit 1
|
2011-04-10 19:38:01 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# check for steghide
|
|
|
|
which steghide > /dev/null
|
|
|
|
if [ $? != 0 ]; then
|
2011-04-28 10:14:37 +00:00
|
|
|
STEGHIDE=0
|
2011-04-10 19:38:01 +00:00
|
|
|
fi
|
|
|
|
|
2011-08-01 20:14:23 +00:00
|
|
|
which cryptsetup > /dev/null
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "Cannot find cryptsetup. Please install it."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
which pinentry > /dev/null
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "Cannot find pinentry. Please install it."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2011-04-10 19:38:01 +00:00
|
|
|
# check for tomb-open script
|
|
|
|
if [ "$0" = "./tomb" ]; then
|
2011-04-28 10:14:37 +00:00
|
|
|
TOMBOPENEXEC="./tomb-open"
|
2011-04-10 19:38:01 +00:00
|
|
|
elif [ "$0" != "tomb" ]; then
|
|
|
|
TOMBOPENEXEC="`dirname $0`/tomb-open"
|
|
|
|
fi
|
|
|
|
}
|
2011-03-31 17:42:05 +00:00
|
|
|
|
|
|
|
# safe dir creation function
|
|
|
|
safe_dir() {
|
2011-05-06 22:19:05 +00:00
|
|
|
which mktemp &> /dev/null
|
2011-04-28 21:48:37 +00:00
|
|
|
if [[ $? = 0 ]]; then
|
2011-09-03 10:39:17 +00:00
|
|
|
mktemp -d /dev/shm/$1.$$.XXXXXXX
|
2011-04-28 21:48:37 +00:00
|
|
|
return
|
|
|
|
fi
|
2011-09-03 10:39:17 +00:00
|
|
|
dir="/dev/shm/$1.$$.$RANDOM.$RANDOM"
|
2011-05-09 09:00:47 +00:00
|
|
|
(umask 077 && mkdir "$dir") || print "-1"
|
|
|
|
print "$dir"
|
2011-03-31 17:42:05 +00:00
|
|
|
}
|
2011-01-10 19:41:28 +00:00
|
|
|
|
2011-07-10 20:11:10 +00:00
|
|
|
#check if there is swap activated
|
|
|
|
check_swap() {
|
|
|
|
# Return 0 if NO swap is used, 1 if swap is used
|
|
|
|
# TODO: it should return 2 if swap is used, but encrypted
|
|
|
|
nlines=$(wc -l /proc/swaps|cut -f1 -d ' ')
|
|
|
|
if [[ $nlines -gt 1 ]]; then
|
|
|
|
r=1
|
|
|
|
else
|
|
|
|
#and return 2
|
|
|
|
r=0
|
|
|
|
fi
|
|
|
|
if [[ $1 == out ]]; then
|
|
|
|
echo $r;
|
|
|
|
fi
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
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-08-29 22:40:04 +00:00
|
|
|
cat <<EOF | GTK2_RC_FILES=${GTK2_RC} pinentry 2>/dev/null | awk '/^D / { sub(/^D /, ""); print }'
|
2011-04-13 14:56:57 +00:00
|
|
|
OPTION ttyname=$TTY
|
|
|
|
OPTION lc-ctype=$LANG
|
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-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-04-28 10:14:37 +00:00
|
|
|
|
2011-02-07 10:27:53 +00:00
|
|
|
func "exec_as_user '$SUDO_USER': ${(f)@}"
|
2011-03-31 17:42:05 +00:00
|
|
|
sudo -u $SUDO_USER "${@[@]}"
|
|
|
|
return $?
|
2011-02-03 16:11:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# escalate privileges
|
|
|
|
check_priv() {
|
2011-04-10 19:38:01 +00:00
|
|
|
if [ $UID != 0 ]; then
|
2011-07-14 13:47:20 +00:00
|
|
|
func "Using sudo for root execution of 'tomb ${(f)OLDARGS}'"
|
2011-04-28 10:14:37 +00:00
|
|
|
# check if sudo has a timestamp active
|
2011-02-20 13:59:30 +00:00
|
|
|
sudok=false
|
2011-08-20 17:25:11 +00:00
|
|
|
sudo -n ${TOMBEXEC} &> /dev/null
|
2011-02-20 13:59:30 +00:00
|
|
|
if [ $? != 0 ]; then # if not then ask a password
|
2011-08-29 22:40:04 +00:00
|
|
|
cat <<EOF | pinentry 2>/dev/null | awk '/^D / { sub(/^D /, ""); print }' | sudo -S -v
|
2011-04-13 14:56:57 +00:00
|
|
|
OPTION ttyname=$TTY
|
|
|
|
OPTION lc-ctype=$LANG
|
2011-02-14 09:24:31 +00:00
|
|
|
SETTITLE Super user privileges required
|
2011-07-14 13:47:20 +00:00
|
|
|
SETDESC Sudo execution of Tomb ${OLDARGS[@]}
|
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
|
2011-08-02 00:10:31 +00:00
|
|
|
sudo "${TOMBEXEC}" "${(@)OLDARGS}"
|
2011-02-20 13:59:30 +00:00
|
|
|
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-03-31 17:42:05 +00:00
|
|
|
usage() {
|
|
|
|
cat <<EOF
|
2011-02-20 13:59:30 +00:00
|
|
|
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
|
2011-05-09 08:32:08 +00:00
|
|
|
list list all open tombs or the one called FILE
|
2011-04-27 21:19:06 +00:00
|
|
|
close close the open tomb called FILE (or all)
|
|
|
|
slam close tomb FILE and kill all pids using it
|
2011-05-09 09:00:47 +00:00
|
|
|
EOF
|
|
|
|
if [ "$STEGHIDE" = 1 ]; then
|
|
|
|
cat <<EOF
|
2011-02-20 13:59:30 +00:00
|
|
|
bury hide a tomb key FILE inside a jpeg PLACE
|
2011-09-26 09:33:07 +00:00
|
|
|
exhume extract a tomb key FILE from a jpeg PLACE
|
2011-05-09 09:00:47 +00:00
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
cat <<EOF
|
2011-02-20 13:59:30 +00:00
|
|
|
|
|
|
|
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
|
2011-05-24 10:04:18 +00:00
|
|
|
-o mount options used to open (default: rw,noatime,nodev)
|
2011-02-20 13:59:30 +00:00
|
|
|
|
|
|
|
-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
|
2011-03-31 17:42:05 +00:00
|
|
|
}
|
|
|
|
|
2011-05-09 11:26:36 +00:00
|
|
|
generate_translatable_strings() {
|
|
|
|
cat <<EOF
|
|
|
|
# Tomb - The Crypto Undertaker.
|
|
|
|
# Copyright (C) 2007-2011 Dyne.org Foundation
|
|
|
|
# Denis Roio <jaromil@dyne.org>, 2011.
|
|
|
|
#
|
|
|
|
#, fuzzy
|
|
|
|
msgid ""
|
|
|
|
msgstr ""
|
|
|
|
"Project-Id-Version: Tomb $VERSION\n"
|
|
|
|
"PO-Revision-Date: `date`\n"
|
|
|
|
"Last-Translator: Denis Roio <jaromil@dyne.org>\n"
|
|
|
|
"Language-Team: Tomb developers <crypto@lists.dyne.org>\n"
|
|
|
|
"MIME-Version: 1.0\n"
|
|
|
|
"Content-Type: text/plain; charset=CHARSET\n"
|
|
|
|
"Content-Transfer-Encoding: 8bit\n"
|
|
|
|
|
|
|
|
#
|
|
|
|
#: commandline help
|
|
|
|
#
|
|
|
|
|
|
|
|
msgid ""
|
|
|
|
EOF
|
|
|
|
|
|
|
|
usage | awk '
|
|
|
|
{ print "\"" $0 "\"" }'
|
|
|
|
cat <<EOF
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
#
|
|
|
|
# tomb internal messages
|
|
|
|
#
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat $TOMBEXEC | awk '
|
|
|
|
/notice ".*"$/ { sub( /notice/ , "");
|
|
|
|
print "#: notice"; print "msgid " $0; print "msgstr \"\"\n" }
|
|
|
|
|
|
|
|
/act ".*"$/ { sub( /act/ , "");
|
|
|
|
print "#: act"; print "msgid " $0; print "msgstr \"\"\n" }
|
|
|
|
'
|
|
|
|
}
|
|
|
|
|
2010-08-22 14:44:35 +00:00
|
|
|
create_tomb() {
|
2011-08-17 17:36:45 +00:00
|
|
|
if ! option_is_set --ignore-swap && [[ `check_swap out` == 1 ]]; then
|
2011-07-10 20:11:10 +00:00
|
|
|
error "You have swap activated; use --ignore-swap if you want to skip this check"
|
|
|
|
act "Using encryption with swap activated is very bad, because some files, or even your secret key, could be written on hard disk."
|
|
|
|
act "However, it could be that your swap is encrypted. If this is case, this is ok. Then, use --ignore-swap to skip this check"
|
|
|
|
exit 1
|
|
|
|
fi
|
2011-02-20 13:59:30 +00:00
|
|
|
if ! [ ${CMD2} ]; then
|
2011-07-14 13:47:20 +00:00
|
|
|
error "no tomb name specified for creation"
|
|
|
|
return 1
|
2011-02-20 13:59:30 +00:00
|
|
|
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
|
2011-08-29 21:52:50 +00:00
|
|
|
tombsize=$opts[-s]
|
2011-08-28 14:54:00 +00:00
|
|
|
|
|
|
|
if [[ $tombsize != <-> ]]; then
|
|
|
|
error "Size is not an integer"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
if [ -e ${tombdir}/${tombfile} ]; then
|
2011-08-01 20:11:57 +00:00
|
|
|
error "tomb exists already. I'm not digging here:"
|
|
|
|
ls -lh ${tombdir}/${tombfile}
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2011-08-28 18:21:10 +00:00
|
|
|
if option_is_set -k; then
|
2011-08-29 21:52:50 +00:00
|
|
|
tombkey="`option_value -k`.tomb.key"
|
2011-08-28 18:21:10 +00:00
|
|
|
else
|
|
|
|
tombkey="${tombdir}/${tombfile}.key"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -e "${tombkey}" ]; then
|
2011-08-01 20:11:57 +00:00
|
|
|
error "tomb key already exists. Quitting."
|
2011-08-28 18:21:10 +00:00
|
|
|
ls -lh ${tombkey}
|
2011-08-01 20:11:57 +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}"
|
|
|
|
|
2011-07-14 13:47:20 +00:00
|
|
|
if [ -z $tombsize ]; then
|
|
|
|
act "No size specified, summoning the Tomb Undertaker to guide us in the creation."
|
|
|
|
"$TOMBOPENEXEC" &
|
|
|
|
wait $!
|
|
|
|
return 0
|
2010-08-22 13:04:19 +00:00
|
|
|
fi
|
2011-01-28 11:26:35 +00:00
|
|
|
|
2011-05-10 13:44:03 +00:00
|
|
|
tombsize_4k=`expr $tombsize \* 1024 / 4`
|
2011-02-20 13:59:30 +00:00
|
|
|
act "Generating ${tombfile} of ${tombsize}Mb (${tombsize_4k} blocks of 4Kb)"
|
|
|
|
$DD if=/dev/urandom bs=4k count=${tombsize_4k} of=${tombdir}/${tombfile}
|
2011-04-28 10:14:37 +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
|
2011-03-31 17:42:05 +00:00
|
|
|
keytmp=`safe_dir tomb`
|
|
|
|
if [ "$keytmp" = "-1" ]; then
|
|
|
|
error "error creating temp dir"
|
2011-04-28 10:14:37 +00:00
|
|
|
exit 1
|
2011-03-31 17:42:05 +00:00
|
|
|
fi
|
|
|
|
#rm -f $keytmp
|
|
|
|
# ?????? creo, cancello e ricreo ??????
|
|
|
|
#mkdir -p $keytmp
|
|
|
|
mount tmpfs "${keytmp}" -t tmpfs -o size=1m
|
2011-02-03 16:11:08 +00:00
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "cannot mount tmpfs filesystem in volatile memory"
|
|
|
|
error "operation aborted."
|
|
|
|
losetup -d $nstloop
|
2011-03-31 17:42:05 +00:00
|
|
|
rm -r "${keytmp}"
|
2011-02-03 16:11:08 +00:00
|
|
|
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-04-28 21:48:37 +00:00
|
|
|
act "To make it faster you can move the mouse around"
|
2011-02-03 16:11:08 +00:00
|
|
|
touch ${keytmp}/tomb.tmp
|
|
|
|
chmod 0600 ${keytmp}/tomb.tmp
|
2011-04-28 21:32:09 +00:00
|
|
|
if [[ $DD = "dcfldd" ]]; then
|
|
|
|
$DD bs=1 count=256 if=/dev/random of=${keytmp}/tomb.tmp statusinterval=1
|
|
|
|
else
|
|
|
|
$DD bs=1 count=256 if=/dev/random of=${keytmp}/tomb.tmp
|
|
|
|
fi
|
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-04-28 10:14:37 +00:00
|
|
|
|
2011-08-29 21:52:50 +00:00
|
|
|
notice "Setup your secret key file ${tombkey}"
|
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-03-31 17:42:05 +00:00
|
|
|
tombpass=`exec_as_user ${TOMBEXEC} askpass ${tombname}`
|
2011-02-14 09:24:31 +00:00
|
|
|
tombpasstmp=$tombpass
|
2011-03-31 17:42:05 +00:00
|
|
|
tombpass=`exec_as_user ${TOMBEXEC} 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-04-28 10:14:37 +00:00
|
|
|
|
2011-09-16 23:28:00 +00:00
|
|
|
gpg \
|
2011-02-22 17:30:42 +00:00
|
|
|
--openpgp --batch --no-options --no-tty --passphrase-fd 0 2>/dev/null \
|
2011-09-16 23:28:00 +00:00
|
|
|
-o "${tombkey}" -c -a ${keytmp}/tomb.tmp <<< ${tombpass}
|
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
|
2011-04-28 10:14:37 +00:00
|
|
|
|
2010-08-22 13:04:19 +00:00
|
|
|
act "formatting Luks mapped device"
|
2011-04-28 10:14:37 +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-04-28 10:14:37 +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-04-10 19:38:01 +00:00
|
|
|
# cryptsetup luksDump ${nstloop}
|
2011-02-09 19:22:39 +00:00
|
|
|
|
2011-03-31 17:42:05 +00:00
|
|
|
act "formatting your Tomb with Ext3/Ext4 filesystem"
|
|
|
|
${MKFS} ${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-04-28 18:42:45 +00:00
|
|
|
# set permissions on the tomb
|
2011-05-24 09:17:33 +00:00
|
|
|
ME=${SUDO_USER:-$(whoami)}
|
2011-08-23 14:55:46 +00:00
|
|
|
chmod 0600 "${tombdir}/${tombfile}"
|
|
|
|
chown $(id -u $ME):$(id -g $ME) "${tombdir}/${tombfile}"
|
2011-04-28 18:42:45 +00:00
|
|
|
|
2011-02-20 13:59:30 +00:00
|
|
|
act "done creating $tombname encrypted storage (using Luks dm-crypt AES/SHA256)"
|
2011-08-28 18:21:10 +00:00
|
|
|
notice "Your tomb is ready in ${tombdir}/${tombfile} and secured with key ${tombkey}"
|
2010-08-22 13:04:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-08-22 14:44:35 +00:00
|
|
|
mount_tomb() {
|
2011-05-26 11:15:03 +00:00
|
|
|
notice "Commanded to open tomb $CMD2"
|
2011-08-17 17:36:45 +00:00
|
|
|
if ! option_is_set --ignore-swap && [[ `check_swap out` == 1 ]]; then
|
2011-07-10 20:11:10 +00:00
|
|
|
error "You have swap activated; use --ignore-swap if you want to skip this check"
|
|
|
|
act "Using encryption with swap activated is very bad, because some files, or even your secret key, could be written on hard disk."
|
|
|
|
act "However, it could be that your swap is encrypted. If this is case, this is ok. Then, use --ignore-swap to skip this check"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if ! [ ${CMD2} ]; then
|
|
|
|
error "no tomb name specified for creation"
|
|
|
|
return 1
|
|
|
|
fi
|
2011-08-28 17:28:47 +00:00
|
|
|
|
2011-09-26 09:33:07 +00:00
|
|
|
# set up variables to be used
|
|
|
|
# the full path is made with $tombdir/$tombfile
|
2011-08-02 00:10:31 +00:00
|
|
|
local tombkey
|
2011-09-26 09:33:07 +00:00
|
|
|
local tombfile
|
|
|
|
local tombdir
|
|
|
|
local tombname
|
|
|
|
tombfile=`basename ${CMD2}`
|
|
|
|
tombdir=`dirname ${CMD2}`
|
|
|
|
# check file type (if its a Luks fs)
|
|
|
|
file ${tombdir}/${tombfile} | grep -i 'luks encrypted file' 2>&1 >/dev/null
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "$CMD2 is not a valid tomb file, operation aborted"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
tombname=${tombfile%%\.*}
|
|
|
|
func "tomb found: ${tombdir}/${tombfile}"
|
|
|
|
|
2011-08-02 00:10:31 +00:00
|
|
|
if option_is_set -k ; then
|
2011-08-28 17:28:47 +00:00
|
|
|
if [[ "`option_value -k`" == "-" ]]; then
|
|
|
|
# take key from stdin
|
|
|
|
local tombkeydir
|
|
|
|
tombkeydir=`safe_dir`
|
|
|
|
cat > ${tombkeydir}/stdin.tmp
|
|
|
|
tombkey=${tombkeydir}/stdin.tmp
|
|
|
|
else
|
|
|
|
# take key from a file
|
|
|
|
tombkey=`option_value -k`
|
|
|
|
fi
|
2011-08-02 00:10:31 +00:00
|
|
|
else
|
2011-08-28 17:28:47 +00:00
|
|
|
# guess key as lying besides the tomb
|
2011-09-26 09:33:07 +00:00
|
|
|
tombkey=${tombdir}/${tombfile}.key
|
2011-08-02 00:10:31 +00:00
|
|
|
fi
|
2011-09-26 09:33:07 +00:00
|
|
|
if ! [ -r ${tombkey} ]; then
|
|
|
|
error "key file not found: ${tombkey}"
|
|
|
|
error "operation aborted."
|
|
|
|
return 1
|
2010-08-22 13:04:19 +00:00
|
|
|
fi
|
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
if ! [ $CMD3 ]; then
|
2011-07-14 13:47:20 +00:00
|
|
|
tombmount=/media/${tombfile}
|
|
|
|
act "mountpoint not specified, using default: $tombmount"
|
2011-02-03 19:42:46 +00:00
|
|
|
elif ! [ -x $CMD3 ]; then
|
2011-07-14 13:47:20 +00:00
|
|
|
error "mountpoint $CMD3 doesn't exist, operation aborted."
|
|
|
|
return 1
|
2011-02-03 19:42:46 +00:00
|
|
|
else
|
2011-09-26 09:33:07 +00:00
|
|
|
tombmount=${CMD3}
|
2011-02-03 19:42:46 +00:00
|
|
|
fi
|
2011-09-26 09:33:07 +00:00
|
|
|
|
2011-05-26 11:15:03 +00:00
|
|
|
# check if its already open
|
2011-09-26 09:33:07 +00:00
|
|
|
mount -l | grep "${tombfile}.*\[$tombname\]$" 2>&1 > /dev/null
|
2011-05-26 11:15:03 +00:00
|
|
|
if [ $? = 0 ]; then
|
2011-09-26 09:33:07 +00:00
|
|
|
error "$tombname is already open on $tombmount"
|
|
|
|
act "here below its status is reported:"
|
|
|
|
list_tombs ${tombname}
|
|
|
|
return 1
|
2011-05-26 11:15:03 +00:00
|
|
|
fi
|
2011-09-26 09:33:07 +00:00
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
notice "mounting $tombfile on mountpoint $tombmount"
|
|
|
|
|
|
|
|
# we need root from here on
|
2011-02-24 11:26:48 +00:00
|
|
|
mkdir -p $tombmount
|
2011-04-28 10:14:37 +00:00
|
|
|
|
2010-08-22 13:04:19 +00:00
|
|
|
nstloop=`losetup -f`
|
2011-03-31 17:42:05 +00:00
|
|
|
if [ $? = 255 ]; then
|
2011-07-14 13:47:20 +00:00
|
|
|
error "too many tomb opened. Please close any of them to open another tomb"
|
|
|
|
exit 1
|
2011-03-31 17:42:05 +00:00
|
|
|
fi
|
2011-02-11 23:36:21 +00:00
|
|
|
losetup -f ${tombdir}/${tombfile}
|
2011-04-28 10:14:37 +00:00
|
|
|
|
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
|
2011-07-14 13:47:20 +00:00
|
|
|
# is it a LUKS encrypted nest? see cryptsetup(1)
|
|
|
|
error "$tombfile is not a valid Luks encrypted storage file"
|
|
|
|
$norm || rmdir $tombmount 2>/dev/null
|
|
|
|
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
|
2011-04-28 18:42:45 +00:00
|
|
|
mapdate=`date +%s`
|
2011-04-28 10:14:37 +00:00
|
|
|
|
2011-02-11 23:36:21 +00:00
|
|
|
mapper="tomb.${tombname}.${mapdate}.`basename $nstloop`"
|
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
|
2011-02-03 16:11:08 +00:00
|
|
|
if [ $c = 1 ]; then
|
2011-03-31 17:42:05 +00:00
|
|
|
tombpass=`exec_as_user ${TOMBEXEC} askpass ${keyname}`
|
2011-02-03 16:11:08 +00:00
|
|
|
else
|
2011-03-31 17:42:05 +00:00
|
|
|
tombpass=`exec_as_user ${TOMBEXEC} askpass "$keyname (retry $c)"`
|
2011-02-03 16:11:08 +00:00
|
|
|
fi
|
2011-09-16 23:28:00 +00:00
|
|
|
(gpg --batch --passphrase-fd 0 --no-tty --no-options \
|
|
|
|
-d "${tombkey}" 2> /dev/null <<< ${tombpass} ) \
|
|
|
|
| cryptsetup --key-file - luksOpen ${nstloop} ${mapper}
|
2011-02-14 09:24:31 +00:00
|
|
|
unset tombpass
|
2011-04-28 10:14:37 +00:00
|
|
|
|
2011-08-28 17:28:47 +00:00
|
|
|
# if key was from stdin delete temp file and dir
|
|
|
|
if [ $tombkeydir ]; then
|
|
|
|
${WIPE[@]} ${tombkey}
|
|
|
|
rmdir $tombkeydir
|
|
|
|
fi
|
|
|
|
|
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
|
2011-04-28 10:14:37 +00:00
|
|
|
|
2011-01-11 09:49:44 +00:00
|
|
|
if ! [ -r /dev/mapper/${mapper} ]; then
|
2011-07-14 13:47:20 +00:00
|
|
|
error "failure mounting the encrypted file"
|
|
|
|
losetup -d ${nstloop}
|
|
|
|
$norm || rmdir ${tombmount} 2>/dev/null
|
|
|
|
return 1
|
2011-01-11 09:49:44 +00:00
|
|
|
fi
|
2011-04-28 10:14:37 +00:00
|
|
|
|
2011-01-11 09:49:44 +00:00
|
|
|
act "encrypted storage filesystem check"
|
2011-01-13 21:35:32 +00:00
|
|
|
fsck -p -C0 /dev/mapper/${mapper}
|
2011-09-26 09:33:07 +00:00
|
|
|
func "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-05-24 10:04:18 +00:00
|
|
|
mount -o $MOUNTOPTS /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-04-28 10:14:37 +00:00
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
notice "encrypted storage $tombfile succesfully mounted on $tombmount"
|
2011-10-25 21:52:59 +00:00
|
|
|
if ! option_is_set -n ; then
|
2011-07-14 13:47:20 +00:00
|
|
|
exec_safe_bind_hooks ${tombmount}
|
|
|
|
exec_safe_post_hooks ${tombmount} open
|
2011-02-20 13:59:30 +00:00
|
|
|
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
|
2011-04-28 10:14:37 +00:00
|
|
|
error "encode failed: $tombkey is not a tomb key"
|
2011-02-11 23:36:21 +00:00
|
|
|
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-04-10 19:38:01 +00:00
|
|
|
tombpass=`exec_as_user ${TOMBEXEC} askpass ${tombkey}`
|
2011-02-14 09:24:31 +00:00
|
|
|
tombpasstmp=$tombpass
|
2011-04-10 19:38:01 +00:00
|
|
|
tombpass=`exec_as_user ${TOMBEXEC} 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-09-04 14:41:33 +00:00
|
|
|
if [[ -e "$keyfile" ]]; then
|
|
|
|
error "Key file $keyfile already exist."
|
|
|
|
return 1
|
|
|
|
fi
|
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-04-10 19:38:01 +00:00
|
|
|
tombpass=`exec_as_user ${TOMBEXEC} askpass ${keyfile}`
|
2011-02-11 23:36:21 +00:00
|
|
|
else
|
2011-04-10 19:38:01 +00:00
|
|
|
tombpass=`exec_as_user ${TOMBEXEC} 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-04-28 10:14:37 +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-12 11:38:59 +00:00
|
|
|
exec_safe_bind_hooks() {
|
2011-07-14 13:47:20 +00:00
|
|
|
if [[ -n ${(k)opts[-o]} ]]; then
|
|
|
|
MOUNTOPTS=${opts[-o]}
|
|
|
|
fi
|
2011-02-12 11:38:59 +00:00
|
|
|
local MOUNTPOINT="${1}"
|
|
|
|
local ME=${SUDO_USER:-$(whoami)}
|
2011-10-25 17:55:48 +00:00
|
|
|
local HOME=$(awk -v a="$ME" -F ':' '{if ($1 == a) print $6}' /etc/passwd 2>/dev/null)
|
2011-02-12 11:38:59 +00:00
|
|
|
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 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
|
2011-04-28 10:14:37 +00:00
|
|
|
error "bind-hooks map format: local/to/tomb local/to/\$HOME. Rolling back"
|
|
|
|
for dir in ${mounted}; do umount $dir; done
|
2011-02-12 11:38:59 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2011-02-25 08:58:45 +00:00
|
|
|
if [ ! -r "$HOME/${maps[$dir]}" ]; then
|
2011-04-28 10:14:37 +00:00
|
|
|
error "bind-hook target not existent, skipping $HOME/${maps[$dir]}"
|
2011-02-25 08:58:45 +00:00
|
|
|
elif [ ! -r "$MOUNTPOINT/$dir" ]; then
|
|
|
|
error "bind-hook source not found in tomb, skipping ${MOUNTPOINT}/${dir}"
|
|
|
|
else
|
2011-05-24 10:04:18 +00:00
|
|
|
mount -o bind,$MOUNTOPTS $MOUNTPOINT/$dir $HOME/${maps[$dir]}
|
2011-02-25 08:58:45 +00:00
|
|
|
mounted+=("$HOME/${maps[$dir]}")
|
2011-02-12 11:38:59 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-04-10 19:38:01 +00:00
|
|
|
exec_safe_post_hooks() {
|
|
|
|
local mnt=$1 # first argument is where the tomb is mounted
|
|
|
|
local ME=${SUDO_USER:-$(whoami)}
|
2011-02-07 10:56:11 +00:00
|
|
|
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-08-16 00:52:01 +00:00
|
|
|
kill_tomb() {
|
|
|
|
# $1 = pids to kill
|
|
|
|
# $2 = type of kill
|
2011-08-17 22:16:00 +00:00
|
|
|
local e p
|
|
|
|
# substitute the \n with space
|
|
|
|
e=${1//$'\n'/ }
|
|
|
|
# split the string at space delim
|
|
|
|
# so we can get a for loop honestly
|
|
|
|
e=(${(s: :)e})
|
|
|
|
for p in $e; do
|
2011-08-16 00:52:01 +00:00
|
|
|
func "killing PID $p..."
|
|
|
|
if [[ "$2" == "soft" ]]; then
|
|
|
|
kill -USR1 $p
|
|
|
|
elif [[ "$2" == "hard" ]]; then
|
|
|
|
kill -TERM $p
|
|
|
|
elif [[ "$2" == "must die" ]]; then
|
2011-08-17 22:16:00 +00:00
|
|
|
kill -KILL $p
|
2011-08-16 00:52:01 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-08-16 00:43:15 +00:00
|
|
|
slam_tomb() {
|
|
|
|
# $1 = tomb mount point
|
2011-08-16 00:52:01 +00:00
|
|
|
local pidk
|
|
|
|
|
2011-08-16 00:43:15 +00:00
|
|
|
pidk=`lsof -t "$1"`
|
|
|
|
if [[ ! -z "$pidk" ]]; then
|
2011-08-16 00:52:01 +00:00
|
|
|
kill_tomb "$pidk" "soft"
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# if there are remaining pids
|
|
|
|
# we need to play hard
|
|
|
|
pidk=`lsof -t "$1"`
|
|
|
|
if [[ -z "$pidk" ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2011-08-18 09:01:17 +00:00
|
|
|
# if we set the -f (force) option
|
|
|
|
# don't wait, just kill
|
|
|
|
option_is_set -f || sleep 3
|
2011-08-16 00:52:01 +00:00
|
|
|
kill_tomb "$pidk" "hard"
|
|
|
|
pidk=`lsof -t "$1"`
|
|
|
|
if [[ -z "$pidk" ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# if there are still some pids around
|
|
|
|
# we have to kill 'em all
|
2011-08-18 09:01:17 +00:00
|
|
|
option_is_set -f || sleep 3
|
2011-08-16 00:52:01 +00:00
|
|
|
kill_tomb "$pidk" "must die"
|
|
|
|
pidk=`lsof -t "$1"`
|
|
|
|
if [[ -z "$pidk" ]]; then
|
|
|
|
return 0
|
2011-08-16 00:43:15 +00:00
|
|
|
fi
|
2011-08-16 00:52:01 +00:00
|
|
|
|
|
|
|
# what PITA!
|
|
|
|
return 1
|
2011-08-16 00:43:15 +00:00
|
|
|
}
|
|
|
|
|
2010-08-22 14:44:35 +00:00
|
|
|
umount_tomb() {
|
2011-04-10 19:38:01 +00:00
|
|
|
local tombs how_many_tombs
|
|
|
|
local pathmap mapper tombname tombmount loopdev
|
|
|
|
local ans pidk pname
|
2010-08-22 13:04:19 +00:00
|
|
|
|
2011-02-03 19:42:46 +00:00
|
|
|
if ! [ $1 ]; then
|
2011-08-16 00:52:01 +00:00
|
|
|
tombs=`find /dev/mapper -name 'tomb.*'`
|
|
|
|
how_many_tombs=`wc -w <<< "$tombs"`
|
|
|
|
if [[ "$how_many_tombs" == "0" ]]; then
|
|
|
|
error "There is no open tomb to be closed"
|
|
|
|
return 1
|
|
|
|
elif [[ "$how_many_tombs" == "1" ]]; then
|
|
|
|
#mapper=`find /dev/mapper -name 'tomb.*'`
|
|
|
|
func "closing mapper $tombs"
|
|
|
|
umount_tomb ${tombs}
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
error "Too many tombs mounted, please specify which to unmount:"
|
|
|
|
ls /dev/mapper/tomb.*
|
|
|
|
error "or issue the command 'tomb close all' to clos'em all."
|
|
|
|
return 1
|
|
|
|
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
|
2011-08-16 00:52:01 +00:00
|
|
|
tombs=`find /dev/mapper -name 'tomb.*'`
|
|
|
|
if ! [ $tombs ]; then
|
|
|
|
notice "Tombs are all closed, cemetery is quiet."
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
for t in ${(f)tombs}; do
|
|
|
|
umount_tomb ${t}
|
|
|
|
done
|
2011-02-20 13:59:30 +00:00
|
|
|
return 0
|
2010-08-22 13:04:19 +00:00
|
|
|
fi
|
2011-02-03 19:42:46 +00:00
|
|
|
|
2011-04-28 10:14:37 +00:00
|
|
|
|
2011-04-28 10:13:46 +00:00
|
|
|
# tomb close argument deduction
|
2011-03-31 17:42:05 +00:00
|
|
|
pathmap=`dirname "$1"`
|
2011-04-28 10:13:46 +00:00
|
|
|
|
2011-04-28 10:14:37 +00:00
|
|
|
if [ "${pathmap}" = "/dev/mapper" ]; then
|
2011-08-16 00:43:15 +00:00
|
|
|
mapper="$1" # argument is the mapper (or none which autofills mapper)
|
|
|
|
tombname="`print $mapper | cut -d. -f2`"
|
|
|
|
tombmount=`mount -l | \
|
2011-04-28 10:13:46 +00:00
|
|
|
awk -vtomb="[$tombname]" '/^\/dev\/mapper\/tomb/ { if($7==tomb) print $3 } '`
|
|
|
|
elif [ "$pathmap" = "." ]; then
|
2011-08-16 00:43:15 +00:00
|
|
|
tombname="$1" # argument is the name
|
|
|
|
mapper=`mount -l | \
|
2011-04-28 10:13:46 +00:00
|
|
|
awk -vtomb="[$tombname]" '/^\/dev\/mapper\/tomb/ { if($7==tomb) print $1 } '`
|
2011-08-16 00:43:15 +00:00
|
|
|
tombmount=`mount -l | \
|
2011-04-28 10:13:46 +00:00
|
|
|
awk -vtomb="[$tombname]" '/^\/dev\/mapper\/tomb/ { if($7==tomb) print $3 } '`
|
2011-03-31 17:42:05 +00:00
|
|
|
else
|
2011-08-16 00:43:15 +00:00
|
|
|
tombmount="$1" # argument should be the mount
|
2011-04-28 10:13:46 +00:00
|
|
|
mapper=`mount | awk -vmnt="$tombmount" '/^\/dev\/mapper\/tomb/ { if($3==mnt) print $1 }'`
|
2011-08-16 00:43:15 +00:00
|
|
|
tombname="`print $mapper | cut -d. -f2`"
|
2011-04-28 10:13:46 +00:00
|
|
|
fi
|
2011-04-28 10:14:37 +00:00
|
|
|
|
2011-05-23 20:28:22 +00:00
|
|
|
# avoid block when the same tomb is mounted, take only the first
|
|
|
|
for tm in ${(f)tombmount}; do tombmount=${tm}; break; done
|
|
|
|
|
2011-04-28 10:13:46 +00:00
|
|
|
func "tomb close argument: $1"
|
|
|
|
func "name:\t$tombname"
|
|
|
|
func "mount:\t$tombmount"
|
|
|
|
func "mapper:\t$mapper"
|
|
|
|
|
|
|
|
if ! [ -e "$mapper" ]; then
|
|
|
|
error "Tomb not found: $1"
|
|
|
|
error "Please specify an existing tomb."
|
|
|
|
return 0
|
2011-03-31 17:42:05 +00:00
|
|
|
fi
|
2011-02-07 08:42:50 +00:00
|
|
|
|
2011-04-28 10:13:46 +00:00
|
|
|
if [ $SLAM ]; then
|
2011-08-16 00:43:15 +00:00
|
|
|
notice "Slamming tomb $tombname mounted on $tombmount"
|
|
|
|
act "Kill all processes busy inside the tomb"
|
|
|
|
slam_tomb "$tombmount"
|
2011-08-16 00:52:01 +00:00
|
|
|
if [[ $? == 1 ]]; then
|
|
|
|
error "Cannot slam the tomb $tombname"
|
|
|
|
return 1
|
|
|
|
fi
|
2011-04-28 10:13:46 +00:00
|
|
|
else
|
2011-08-16 00:43:15 +00:00
|
|
|
notice "Closing tomb $tombname mounted on $tombmount"
|
2011-02-07 08:42:50 +00:00
|
|
|
fi
|
2011-05-15 18:03:11 +00:00
|
|
|
|
|
|
|
# check if there are binded dirs and close them
|
2011-05-09 07:11:18 +00:00
|
|
|
tombmount_esc=`sed 's:\/:\\\/:g' <<< $tombmount `
|
2011-04-28 10:13:46 +00:00
|
|
|
unbind=`mount | awk "/^$tombmount_esc.*bind/"' { print $3 }'`
|
|
|
|
for b in ${(f)unbind}; do
|
2011-08-16 00:43:15 +00:00
|
|
|
hook="`basename $b`"
|
|
|
|
act "closing tomb hook: $hook"
|
|
|
|
umount $b
|
2011-08-16 00:52:01 +00:00
|
|
|
if [[ $? != 0 ]]; then
|
2011-08-16 00:43:15 +00:00
|
|
|
if [ $SLAM ]; then
|
|
|
|
notice "Slamming tomb: killing all processes using this hook"
|
|
|
|
slam_tomb "$b"
|
2011-08-16 00:52:01 +00:00
|
|
|
if [[ $? == 1 ]]; then
|
|
|
|
error "Cannot slam the tomb $b"
|
|
|
|
return 1
|
|
|
|
fi
|
2011-08-16 00:43:15 +00:00
|
|
|
umount $b
|
|
|
|
else
|
|
|
|
error "Tomb hook is busy, cannot close tomb."
|
|
|
|
return 1
|
|
|
|
fi
|
2011-04-28 10:13:46 +00:00
|
|
|
fi
|
|
|
|
done
|
2011-01-13 21:35:32 +00:00
|
|
|
|
2011-02-12 11:38:59 +00:00
|
|
|
# Execute post-hooks for eventual cleanup
|
2011-10-25 21:52:59 +00:00
|
|
|
if ! option_is_set -n ; then
|
2011-08-16 00:43:15 +00:00
|
|
|
exec_safe_post_hooks ${tombmount%%/} close
|
2011-02-20 13:59:30 +00:00
|
|
|
fi
|
2011-02-03 16:11:08 +00:00
|
|
|
|
2011-04-28 10:13:46 +00:00
|
|
|
if [ $tombmount ]; then # tomb is actively mounted
|
2011-05-26 16:38:36 +00:00
|
|
|
func "performing umount of $tombmount"
|
2011-09-03 10:43:21 +00:00
|
|
|
umount ${tombmount}
|
2011-04-28 10:13:46 +00:00
|
|
|
if ! [ $? = 0 ]; then
|
2011-04-10 19:38:01 +00:00
|
|
|
error "Tomb is busy, cannot umount!"
|
2011-10-25 17:37:28 +00:00
|
|
|
else
|
|
|
|
# this means we used a "default" mount point
|
|
|
|
if [ "${tombmount}" = "/media/${tombname}.tomb" ]; then
|
|
|
|
rmdir ${tombmount}
|
|
|
|
fi
|
2011-04-28 10:13:46 +00:00
|
|
|
fi
|
2011-04-10 19:38:01 +00:00
|
|
|
fi
|
2011-04-28 10:14:37 +00:00
|
|
|
|
2011-04-28 10:13:46 +00:00
|
|
|
cryptsetup luksClose $mapper
|
2010-08-22 13:04:19 +00:00
|
|
|
if ! [ $? = 0 ]; then
|
2011-03-31 17:42:05 +00:00
|
|
|
error "error occurred in cryptsetup luksClose ${mapper}"
|
2011-02-03 19:42:46 +00:00
|
|
|
return 1
|
2010-08-22 13:04:19 +00:00
|
|
|
fi
|
|
|
|
|
2011-04-28 10:14:37 +00:00
|
|
|
loopdev=`cut -d '.' -f4 <<< "$mapper"`
|
2011-03-31 17:42:05 +00:00
|
|
|
losetup -d "/dev/$loopdev"
|
2010-08-22 14:44:35 +00:00
|
|
|
|
2011-05-15 18:11:01 +00:00
|
|
|
# kill the status tray widget if still present
|
|
|
|
# this makes the widget disappear when closing tomb from cli
|
|
|
|
awkmapper=`sed 's:\/:\\\/:g' <<< $mapper`
|
|
|
|
statustray_pid=`ps ax | awk "/tomb-status $awkmapper/"' {print $1} '`
|
2011-05-15 18:12:15 +00:00
|
|
|
if [ ${statustray_pid} ]; then
|
|
|
|
kill ${statustray_pid}
|
|
|
|
fi
|
2011-05-15 18:11:01 +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
|
|
|
|
2011-05-09 07:11:18 +00:00
|
|
|
# list all tombs mounted in a readable format
|
|
|
|
list_tombs() {
|
|
|
|
if [ $1 ]; then
|
|
|
|
# list a specific tomb
|
|
|
|
mounted_tombs=`mount -l |
|
|
|
|
awk -vtomb="[$1]" '/^\/dev\/mapper\/tomb/ { if($7==tomb) print $1 ";" $3 ";" $5 ";" $6 ";" $7 }'`
|
|
|
|
else
|
|
|
|
# list all open tombs
|
|
|
|
mounted_tombs=`mount -l |
|
|
|
|
awk '/^\/dev\/mapper\/tomb/ { print $1 ";" $3 ";" $5 ";" $6 ";" $7 }'`
|
|
|
|
fi
|
|
|
|
# 1 = full mapper path
|
|
|
|
# 2 = mountpont
|
|
|
|
# 3 = filesystem
|
|
|
|
# 4 = mount options
|
|
|
|
# 5 = name
|
|
|
|
|
|
|
|
if ! [ $mounted_tombs ]; then
|
|
|
|
if [ $1 ]; then
|
2011-05-09 08:32:08 +00:00
|
|
|
error "There seems to be no open tomb engraved as [${1}]"
|
2011-05-09 07:11:18 +00:00
|
|
|
else
|
2011-05-09 08:32:08 +00:00
|
|
|
error "I can't see any open tomb, may they all rest in peace."
|
2011-05-09 07:11:18 +00:00
|
|
|
fi
|
2011-05-09 08:32:08 +00:00
|
|
|
exit 1
|
2011-05-09 07:11:18 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
for t in ${(f)mounted_tombs}; do
|
|
|
|
mapper=`basename ${t[(ws:;:)1]}`
|
|
|
|
|
|
|
|
tombname=${t[(ws:;:)5]}
|
|
|
|
tombmount=${t[(ws:;:)2]}
|
|
|
|
tombfs=${t[(ws:;:)3]}
|
|
|
|
tombfsopts=${t[(ws:;:)4]}
|
|
|
|
|
2011-05-26 10:24:17 +00:00
|
|
|
# calculate tomb size
|
|
|
|
ts=`df -hP /dev/mapper/$mapper |
|
|
|
|
awk "/mapper/"' { print $2 ";" $3 ";" $4 ";" $5 }'`
|
|
|
|
tombtot=${ts[(ws:;:)1]}
|
|
|
|
tombused=${ts[(ws:;:)2]}
|
|
|
|
tombavail=${ts[(ws:;:)3]}
|
|
|
|
tombpercent=${ts[(ws:;:)4]}
|
2011-09-26 09:33:07 +00:00
|
|
|
tombp=${tombpercent%%%}
|
2011-05-09 07:11:18 +00:00
|
|
|
tombsince=`date --date=@${mapper[(ws:.:)3]} +%c`
|
2011-05-26 10:24:17 +00:00
|
|
|
|
2011-05-09 07:11:18 +00:00
|
|
|
# breaking up such strings is good for translation
|
2011-05-09 08:32:08 +00:00
|
|
|
print -n "$fg[green]$tombname"
|
|
|
|
print -n "$fg[white] open on "
|
|
|
|
print -n "$fg_bold[white]$tombmount"
|
|
|
|
print -n "$fg_no_bold[white] using "
|
2011-05-10 07:26:07 +00:00
|
|
|
print "$fg_bold[white]$tombfs $tombfsopts"
|
2011-05-26 10:24:17 +00:00
|
|
|
|
2011-09-26 09:33:07 +00:00
|
|
|
print -n "$fg_no_bold[green]$tombname"
|
|
|
|
print -n "$fg_no_bold[white] open since "
|
|
|
|
print "$fg_bold[white]$tombsince$fg_no_bold[white]"
|
|
|
|
|
2011-05-26 10:24:17 +00:00
|
|
|
print -n "$fg_no_bold[green]$tombname"
|
|
|
|
print -n "$fg[white] size "
|
|
|
|
print -n "$fg_bold[white]$tombtot"
|
|
|
|
print -n "$fg_no_bold[white] of which "
|
|
|
|
print -n "$fg_bold[white]$tombused"
|
2011-09-26 09:33:07 +00:00
|
|
|
print -n "$fg_no_bold[white] used: "
|
2011-05-26 10:24:17 +00:00
|
|
|
print -n "$fg_bold[white]$tombavail"
|
|
|
|
print -n "$fg_no_bold[white] free ("
|
|
|
|
print -n "$fg_bold[white]$tombpercent"
|
|
|
|
print "$fg_no_bold[white] full)"
|
2011-08-19 13:25:19 +00:00
|
|
|
|
2011-09-26 09:33:07 +00:00
|
|
|
if [[ ${tombp} -ge 90 ]]; then
|
|
|
|
print -n "$fg_no_bold[green]$tombname"
|
|
|
|
print "$fg_bold[red] Your tomb is almost full!"
|
|
|
|
fi
|
2011-05-26 10:24:17 +00:00
|
|
|
|
2011-05-09 07:11:18 +00:00
|
|
|
|
|
|
|
# now check hooks
|
|
|
|
mtomb=`sed 's:\/:\\\/:g' <<< $tombmount`
|
|
|
|
mounted_hooks=`mount | awk "/^$mtomb/"' {print $1 ";" $3}'`
|
|
|
|
for h in ${(f)mounted_hooks}; do
|
2011-09-26 09:33:07 +00:00
|
|
|
print -n "$fg_no_bold[green]$tombname"
|
2011-05-09 08:32:08 +00:00
|
|
|
print -n "$fg_no_bold[white] hooks "
|
|
|
|
print -n "$fg_bold[white]`basename ${h[(ws:;:)1]}`"
|
|
|
|
print -n "$fg_no_bold[white] on "
|
|
|
|
print "$fg_bold[white]${h[(ws:;:)2]}$fg_no_bold[white]"
|
2011-05-09 07:11:18 +00:00
|
|
|
done
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-09-04 07:36:40 +00:00
|
|
|
launch_status() {
|
|
|
|
# calculates the correct arguments to launch tomb-status tray
|
|
|
|
# applet; it takes the tomb name as an argument and can be
|
|
|
|
# launched after a successful tomb mount.
|
|
|
|
|
|
|
|
which tomb-status > /dev/null
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "Cannot find tomb-status binary, operation aborted."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [ $DISPLAY ]; then
|
|
|
|
error "No active X display found, operation aborted."
|
|
|
|
error "Status launches a graphical tray applet, you need X running."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check argument. use single tomb mounted if no argument
|
|
|
|
# abort if more than one tomb is mounted.
|
|
|
|
if ! [ $1 ]; then
|
|
|
|
tombs=`find /dev/mapper -name 'tomb.*'`
|
|
|
|
how_many_tombs=`wc -w <<< "$tombs"`
|
|
|
|
if [[ "$how_many_tombs" == "0" ]]; then
|
|
|
|
error "There is no open tomb, status cannot be launched"
|
|
|
|
return 1
|
|
|
|
elif [[ "$how_many_tombs" == "1" ]]; then
|
|
|
|
#mapper=`find /dev/mapper -name 'tomb.*'`
|
|
|
|
tombname=`find /dev/mapper -name "tomb.*"`
|
|
|
|
tombname=`basename $tombname | cut -d. -f2`
|
|
|
|
notice "launching status for tomb $tombname"
|
|
|
|
else
|
|
|
|
error "Too many tombs mounted, please specify which one"
|
|
|
|
list_tombs
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# name was specified on command line
|
|
|
|
tombname=$1
|
|
|
|
ls /dev/mapper | grep "^tomb.${tombname}.*" > /dev/null
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
error "Cannot find any tomb named $tombname being open, operation aborted."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# finally we launch
|
|
|
|
tombmap=`mount -l | awk "/\[${tombname}\]$/"' { print $1 } '`
|
|
|
|
tombmount=`mount -l | awk "/\[${tombname}\]$/"' { print $3 } '`
|
|
|
|
tomb-status $tombmap $tombname $tombmount &!
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
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-04-28 10:14:37 +00:00
|
|
|
act "updating mimetypes..."
|
2011-01-12 16:02:19 +00:00
|
|
|
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-04-28 10:14:37 +00:00
|
|
|
|
2011-01-12 16:02:19 +00:00
|
|
|
rm /tmp/dyne-tomb.xml
|
2011-04-28 10:14:37 +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
|
2011-04-10 19:38:01 +00:00
|
|
|
Exec="${TOMBOPENEXEC}" %U
|
2011-01-12 16:02:19 +00:00
|
|
|
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
|
2011-04-28 10:14:37 +00:00
|
|
|
|
2011-01-12 16:02:19 +00:00
|
|
|
act "updating mime info..."
|
|
|
|
cat <<EOF > /usr/share/mime-info/tomb.keys
|
|
|
|
# actions for encrypted tomb storage
|
|
|
|
application/x-tomb-volume:
|
2011-04-10 19:38:01 +00:00
|
|
|
open="${TOMBOPENEXEC}" %f
|
2011-01-12 16:02:19 +00:00
|
|
|
view=tomb-open %f
|
|
|
|
icon-filename=monmort.xpm
|
2011-04-28 10:14:37 +00:00
|
|
|
short_list_application_ids_for_novice_user_level=tomb
|
2011-01-12 16:02:19 +00:00
|
|
|
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-04-28 10:14:37 +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
|
2011-04-28 10:14:37 +00:00
|
|
|
command=tomb-open
|
|
|
|
name=Tomb - Crypto Undertaker
|
|
|
|
can_open_multiple_files=false
|
|
|
|
expects_uris=false
|
|
|
|
requires_terminal=true
|
|
|
|
mime-types=application/x-tomb-volume,application/x-tomb-key
|
2011-01-12 16:02:19 +00:00
|
|
|
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
|
|
|
|
2011-08-02 00:10:31 +00:00
|
|
|
option_is_set() {
|
|
|
|
#First argument, the option (something like "-s")
|
|
|
|
#Second (optional) argument: if it's "out", command will print it out 'set'/'unset'
|
|
|
|
# This is useful for if conditions
|
|
|
|
#Return 0 if is set, 1 otherwise
|
|
|
|
[[ -n ${(k)opts[$1]} ]];
|
|
|
|
r=$?
|
|
|
|
if [[ $2 == out ]]; then
|
|
|
|
if [[ $r == 0 ]]; then
|
|
|
|
echo 'set'
|
|
|
|
else
|
|
|
|
echo 'unset'
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
option_value() {
|
|
|
|
#First argument, the option (something like "-s")
|
2011-08-21 15:43:32 +00:00
|
|
|
<<< ${opts[$1]}
|
2011-08-02 00:10:31 +00:00
|
|
|
}
|
|
|
|
|
[Optparsing] Support options in whatever position
However, it has a precondition that developers MUST respect:
An option CAN'T have differente meanings/behaviour in different subcommands.
For example, "-s" means "size" and accept an argument. If you are tempted to add
an option "-s" (that means, for example "silent", and doesn't accept an argument)
This, however, make sense even from an usability point of view.
Also, option with optional argument are prohibited.
The technique is a "double scan": first, we scan options passing
_every_ possible subcommand options to zparseopts.
This way, we can distinguish between option argument and parameters.
So, we recognize which is the subcommand.
Then, parse again
2011-07-24 23:00:25 +00:00
|
|
|
main() {
|
|
|
|
local -A subcommands_opts
|
2011-07-19 13:42:49 +00:00
|
|
|
### Options configuration
|
[Optparsing] Support options in whatever position
However, it has a precondition that developers MUST respect:
An option CAN'T have differente meanings/behaviour in different subcommands.
For example, "-s" means "size" and accept an argument. If you are tempted to add
an option "-s" (that means, for example "silent", and doesn't accept an argument)
This, however, make sense even from an usability point of view.
Also, option with optional argument are prohibited.
The technique is a "double scan": first, we scan options passing
_every_ possible subcommand options to zparseopts.
This way, we can distinguish between option argument and parameters.
So, we recognize which is the subcommand.
Then, parse again
2011-07-24 23:00:25 +00:00
|
|
|
#Hi, dear developer! Are you trying to add a new subcommand, or to add some options?
|
|
|
|
#Well, keep in mind that:
|
|
|
|
# 1. An option CAN'T have differente meanings/behaviour in different subcommands.
|
|
|
|
# For example, "-s" means "size" and accept an argument. If you are tempted to add
|
|
|
|
# an option "-s" (that means, for example "silent", and doesn't accept an argument)
|
|
|
|
# DON'T DO IT!
|
|
|
|
# There are two reasons for that:
|
|
|
|
# I. usability; user expect that "-s" is "size
|
|
|
|
# II. Option parsing WILL EXPLODE if you do this kind of bad things
|
|
|
|
# (it will say "option defined more than once, and he's right)
|
2011-08-16 18:51:59 +00:00
|
|
|
main_opts=(q -quiet=q D -debug=D h -help=h v -version=v)
|
2011-08-16 18:44:18 +00:00
|
|
|
subcommands_opts[__default]=""
|
2011-07-10 20:11:10 +00:00
|
|
|
subcommands_opts[open]="n -nohook=n k: -key=k o: -mount-options=o -ignore-swap"
|
2011-07-19 23:15:31 +00:00
|
|
|
subcommands_opts[mount]=${subcommands_opts[open]}
|
2011-08-28 18:21:10 +00:00
|
|
|
subcommands_opts[create]="s: -size=s -ignore-swap k: -key=k"
|
2011-07-19 13:42:49 +00:00
|
|
|
subcommands_opts[close]=""
|
|
|
|
subcommands_opts[help]=""
|
[Optparsing] Support options in whatever position
However, it has a precondition that developers MUST respect:
An option CAN'T have differente meanings/behaviour in different subcommands.
For example, "-s" means "size" and accept an argument. If you are tempted to add
an option "-s" (that means, for example "silent", and doesn't accept an argument)
This, however, make sense even from an usability point of view.
Also, option with optional argument are prohibited.
The technique is a "double scan": first, we scan options passing
_every_ possible subcommand options to zparseopts.
This way, we can distinguish between option argument and parameters.
So, we recognize which is the subcommand.
Then, parse again
2011-07-24 23:00:25 +00:00
|
|
|
subcommands_opts[slam]=""
|
|
|
|
subcommands_opts[list]=""
|
|
|
|
subcommands_opts[help]=""
|
|
|
|
subcommands_opts[bury]=""
|
|
|
|
subcommands_opts[exhume]=""
|
|
|
|
subcommands_opts[decompose]=""
|
|
|
|
subcommands_opts[recompose]=""
|
|
|
|
subcommands_opts[install]=""
|
|
|
|
subcommands_opts[askpass]=""
|
|
|
|
subcommands_opts[mktemp]=""
|
2011-08-20 16:45:00 +00:00
|
|
|
subcommands_opts[source]=""
|
2011-09-04 07:36:40 +00:00
|
|
|
subcommands_opts[status]=""
|
[Optparsing] Support options in whatever position
However, it has a precondition that developers MUST respect:
An option CAN'T have differente meanings/behaviour in different subcommands.
For example, "-s" means "size" and accept an argument. If you are tempted to add
an option "-s" (that means, for example "silent", and doesn't accept an argument)
This, however, make sense even from an usability point of view.
Also, option with optional argument are prohibited.
The technique is a "double scan": first, we scan options passing
_every_ possible subcommand options to zparseopts.
This way, we can distinguish between option argument and parameters.
So, we recognize which is the subcommand.
Then, parse again
2011-07-24 23:00:25 +00:00
|
|
|
### Detect subcommand
|
|
|
|
local -aU every_opts #every_opts behave like a set; that is, an array with unique elements
|
|
|
|
for optspec in $subcommands_opts$main_opts; do
|
|
|
|
for opt in ${=optspec}; do
|
|
|
|
every_opts+=${opt}
|
|
|
|
done
|
|
|
|
done
|
|
|
|
local -a oldstar
|
|
|
|
oldstar=($argv)
|
|
|
|
zparseopts -M -E -D -Adiscardme ${every_opts}
|
|
|
|
unset discardme
|
|
|
|
subcommand=$1
|
2011-08-16 18:44:18 +00:00
|
|
|
if [[ -z $subcommand ]]; then
|
|
|
|
subcommand="__default"
|
|
|
|
fi
|
[Optparsing] Support options in whatever position
However, it has a precondition that developers MUST respect:
An option CAN'T have differente meanings/behaviour in different subcommands.
For example, "-s" means "size" and accept an argument. If you are tempted to add
an option "-s" (that means, for example "silent", and doesn't accept an argument)
This, however, make sense even from an usability point of view.
Also, option with optional argument are prohibited.
The technique is a "double scan": first, we scan options passing
_every_ possible subcommand options to zparseopts.
This way, we can distinguish between option argument and parameters.
So, we recognize which is the subcommand.
Then, parse again
2011-07-24 23:00:25 +00:00
|
|
|
if [[ -z ${(k)subcommands_opts[$subcommand]} ]]; then #there's no such subcommand
|
|
|
|
error "Subcommand '$subcommand' doesn't exist"
|
|
|
|
exit 127
|
2011-04-10 19:38:01 +00:00
|
|
|
fi
|
[Optparsing] Support options in whatever position
However, it has a precondition that developers MUST respect:
An option CAN'T have differente meanings/behaviour in different subcommands.
For example, "-s" means "size" and accept an argument. If you are tempted to add
an option "-s" (that means, for example "silent", and doesn't accept an argument)
This, however, make sense even from an usability point of view.
Also, option with optional argument are prohibited.
The technique is a "double scan": first, we scan options passing
_every_ possible subcommand options to zparseopts.
This way, we can distinguish between option argument and parameters.
So, we recognize which is the subcommand.
Then, parse again
2011-07-24 23:00:25 +00:00
|
|
|
argv=(${oldstar})
|
|
|
|
unset oldstar
|
2011-02-14 14:57:37 +00:00
|
|
|
|
[Optparsing] Support options in whatever position
However, it has a precondition that developers MUST respect:
An option CAN'T have differente meanings/behaviour in different subcommands.
For example, "-s" means "size" and accept an argument. If you are tempted to add
an option "-s" (that means, for example "silent", and doesn't accept an argument)
This, however, make sense even from an usability point of view.
Also, option with optional argument are prohibited.
The technique is a "double scan": first, we scan options passing
_every_ possible subcommand options to zparseopts.
This way, we can distinguish between option argument and parameters.
So, we recognize which is the subcommand.
Then, parse again
2011-07-24 23:00:25 +00:00
|
|
|
### Parsing global + command-specific options
|
2011-07-14 13:47:20 +00:00
|
|
|
# zsh magic: ${=string} will split to multiple arguments when spaces occur
|
[Optparsing] Support options in whatever position
However, it has a precondition that developers MUST respect:
An option CAN'T have differente meanings/behaviour in different subcommands.
For example, "-s" means "size" and accept an argument. If you are tempted to add
an option "-s" (that means, for example "silent", and doesn't accept an argument)
This, however, make sense even from an usability point of view.
Also, option with optional argument are prohibited.
The technique is a "double scan": first, we scan options passing
_every_ possible subcommand options to zparseopts.
This way, we can distinguish between option argument and parameters.
So, we recognize which is the subcommand.
Then, parse again
2011-07-24 23:00:25 +00:00
|
|
|
set -A cmd_opts ${main_opts} ${=subcommands_opts[$subcommand]}
|
|
|
|
if [[ -n $cmd_opts ]]; then #if there is no option, we don't need parsing
|
2011-07-14 13:47:20 +00:00
|
|
|
zparseopts -M -E -D -Aopts ${cmd_opts}
|
|
|
|
if [[ $? != 0 ]]; then
|
|
|
|
error "Some error occurred during option processing. See \"tomb help\" for more info"
|
|
|
|
exit 127
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
#build PARAM (array of arguments) and check if there are unrecognized options
|
|
|
|
ok=0
|
|
|
|
PARAM=()
|
|
|
|
for arg in $*; do
|
|
|
|
if [[ $arg == '--' || $arg == '-' ]]; then
|
|
|
|
ok=1
|
|
|
|
continue #it shouldnt be appended to PARAM
|
|
|
|
elif [[ $arg[1] == '-' ]]; then
|
|
|
|
if [[ $ok == 0 ]]; then
|
[Optparsing] Support options in whatever position
However, it has a precondition that developers MUST respect:
An option CAN'T have differente meanings/behaviour in different subcommands.
For example, "-s" means "size" and accept an argument. If you are tempted to add
an option "-s" (that means, for example "silent", and doesn't accept an argument)
This, however, make sense even from an usability point of view.
Also, option with optional argument are prohibited.
The technique is a "double scan": first, we scan options passing
_every_ possible subcommand options to zparseopts.
This way, we can distinguish between option argument and parameters.
So, we recognize which is the subcommand.
Then, parse again
2011-07-24 23:00:25 +00:00
|
|
|
error "unrecognized option $arg"
|
2011-07-14 13:47:20 +00:00
|
|
|
exit 127
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
PARAM+=$arg
|
|
|
|
done
|
2011-07-27 08:52:26 +00:00
|
|
|
#first parameter actually is the subcommand: delete it and shift
|
2011-08-16 18:44:18 +00:00
|
|
|
if [[ $subcommand != '__default' ]]; then
|
|
|
|
PARAM[1]=()
|
|
|
|
shift
|
|
|
|
fi
|
2011-07-14 13:47:20 +00:00
|
|
|
### End parsing command-specific options
|
|
|
|
|
|
|
|
### Set global options (useless, but for code retro-compatibility)
|
|
|
|
for opt in ${(k)global_opts}; do
|
|
|
|
if [[ $opt == '-q' ]]; then
|
|
|
|
QUIET=1
|
|
|
|
elif [[ $opt == '-D' ]]; then
|
|
|
|
DEBUG=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
CMD=$subcommand
|
|
|
|
CMD2=$PARAM[1]
|
|
|
|
CMD3=$PARAM[2]
|
2011-01-12 16:02:19 +00:00
|
|
|
|
2011-04-10 19:38:01 +00:00
|
|
|
func "Tomb command: $CMD $CMD2 $CMD3"
|
2011-04-28 10:14:37 +00:00
|
|
|
|
2011-07-14 13:47:20 +00:00
|
|
|
case "$subcommand" in
|
2011-04-28 10:14:37 +00:00
|
|
|
create) check_priv ; create_tomb ;;
|
|
|
|
mount) check_priv ; mount_tomb ;;
|
|
|
|
open) check_priv ; mount_tomb ;;
|
|
|
|
umount) check_priv ; umount_tomb ${CMD2} ;;
|
|
|
|
close) check_priv ; umount_tomb ${CMD2} ;;
|
2011-04-28 18:42:45 +00:00
|
|
|
slam) check_priv ; SLAM=1; umount_tomb ${CMD2} ;;
|
2011-05-09 07:11:18 +00:00
|
|
|
list) list_tombs ${CMD2} ;;
|
2011-09-04 07:36:40 +00:00
|
|
|
status) launch_status ${CMD2} ;;
|
2011-05-09 08:32:08 +00:00
|
|
|
help) usage ;;
|
2011-04-28 10:14:37 +00:00
|
|
|
bury) if [ "$STEGHIDE" = 0 ]; then
|
2011-04-10 19:38:01 +00:00
|
|
|
error "steghide not installed. Cannot bury your key"
|
|
|
|
return 1
|
2011-04-28 10:14:37 +00:00
|
|
|
fi
|
2011-04-10 19:38:01 +00:00
|
|
|
encode_key ${CMD2} ${CMD3} ;;
|
2011-04-28 10:14:37 +00:00
|
|
|
exhume) if [ "$STEGHIDE" = 0 ]; then
|
2011-04-10 19:38:01 +00:00
|
|
|
error "steghide not installed. Cannot exhume your key"
|
|
|
|
return 1
|
2011-04-28 10:14:37 +00:00
|
|
|
fi
|
2011-04-10 19:38:01 +00:00
|
|
|
decode_key ${CMD2} ;;
|
2011-05-26 10:24:17 +00:00
|
|
|
# internal commands useful to developers
|
|
|
|
'source') return 0 ;;
|
2011-04-28 10:14:37 +00:00
|
|
|
install) check_priv ; install_tomb ;;
|
|
|
|
askpass) ask_password $CMD2 ;;
|
2011-04-28 18:42:45 +00:00
|
|
|
mktemp) safe_dir ${CMD2} ;;
|
2011-05-09 11:26:36 +00:00
|
|
|
translate) generate_translatable_strings ;;
|
2011-08-16 18:51:59 +00:00
|
|
|
__default)
|
|
|
|
if option_is_set -v; then
|
|
|
|
echo Tomb - $VERSION
|
|
|
|
else
|
|
|
|
usage
|
|
|
|
fi;;
|
2011-04-28 10:14:37 +00:00
|
|
|
*) error "command \"$CMD\" not recognized"
|
|
|
|
act "try -h for help"
|
|
|
|
return 1
|
|
|
|
;;
|
2011-04-10 19:38:01 +00:00
|
|
|
esac
|
2011-10-24 19:30:10 +00:00
|
|
|
return $?
|
2011-04-10 19:38:01 +00:00
|
|
|
}
|
2011-01-12 16:02:19 +00:00
|
|
|
|
2011-04-10 19:38:01 +00:00
|
|
|
check_bin
|
|
|
|
main $@
|
2011-10-24 19:30:10 +00:00
|
|
|
exit $?
|