2011-11-17 13:47:40 +00:00
|
|
|
#!/bin/zsh
|
|
|
|
#
|
|
|
|
# Undertaker, auxiliary command for Tomb
|
|
|
|
#
|
|
|
|
# Tomb is a tool to operate file encryption of private and secret data
|
|
|
|
#
|
|
|
|
# Undertaker is a tool to retrieve tomb keys from various sources
|
|
|
|
#
|
2012-01-17 18:01:20 +00:00
|
|
|
# {{{ Copyleft (C) 2012 Dyne.org foundation
|
|
|
|
# 2011-2012 Denis Roio <jaromil@dyne.org>
|
2011-11-17 13:47:40 +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-12-01 20:39:10 +00:00
|
|
|
# }}}
|
2011-11-17 13:47:40 +00:00
|
|
|
|
2012-01-17 18:01:20 +00:00
|
|
|
# signal traps (special funcs in Zsh)
|
|
|
|
TRAPKILL() die "KILL signal caught, undertaker aborting."
|
|
|
|
TRAPSTOP() die "STOP signal caught, undertaker aborting."
|
|
|
|
|
2011-11-17 13:47:40 +00:00
|
|
|
# first of all source the tomb core functions
|
2012-08-11 22:57:13 +00:00
|
|
|
TOMBBIN=$(dirname $(readlink -f $0))/tomb
|
|
|
|
if ! [[ -x $TOMBBIN ]]; then
|
2012-01-29 15:16:06 +00:00
|
|
|
print "$fg[red][!]$fg[white] Tomb command not found, operation aborted." >&2; exit 1
|
2011-11-17 13:47:40 +00:00
|
|
|
fi
|
2012-08-11 22:57:13 +00:00
|
|
|
source $TOMBBIN ${tomb_opts[@]} source
|
|
|
|
TOMBEXEC=undertaker
|
2011-11-17 13:47:40 +00:00
|
|
|
|
2011-11-16 21:49:08 +00:00
|
|
|
key_found() {
|
|
|
|
# $1 is "url"
|
2012-01-28 03:15:23 +00:00
|
|
|
if option_is_set --batch; then
|
2012-01-29 15:16:06 +00:00
|
|
|
print -n '[m]' >&2
|
2011-11-16 21:49:08 +00:00
|
|
|
fi
|
2012-01-29 15:16:06 +00:00
|
|
|
print "$fg[white][found] $1" >&2
|
2011-11-16 21:49:08 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 18:01:20 +00:00
|
|
|
|
|
|
|
|
2011-11-15 14:50:09 +00:00
|
|
|
function undertaker_scheme() {
|
2012-01-28 03:15:23 +00:00
|
|
|
zparseopts -D -path=print_path
|
2011-11-17 13:47:40 +00:00
|
|
|
|
2011-11-16 21:49:08 +00:00
|
|
|
local scheme
|
2011-11-15 14:50:09 +00:00
|
|
|
scheme=$1
|
2011-11-16 21:49:08 +00:00
|
|
|
local keypath
|
2011-11-15 14:50:09 +00:00
|
|
|
keypath=$2
|
|
|
|
case $scheme in
|
2012-01-17 18:01:20 +00:00
|
|
|
|
|
|
|
|
2011-11-15 14:50:09 +00:00
|
|
|
bluetooth)
|
|
|
|
#TODO: support --print-path
|
|
|
|
act "access to bluetooth protocol requested"
|
|
|
|
which obexftp &> /dev/null
|
|
|
|
if [[ $? != 0 ]]; then
|
2012-01-28 03:15:23 +00:00
|
|
|
_warning "obexftp not found, needed for bluetooth: operation aborted."
|
2011-11-15 14:50:09 +00:00
|
|
|
return 64
|
|
|
|
fi
|
|
|
|
keytmp=`safe_dir undertaker`
|
|
|
|
cd $keytmp
|
|
|
|
# fetch key from bluetooth, url format: bluetooth://55:33:44:55:66/file/path
|
|
|
|
obexdevpath=${keypath#*//}
|
|
|
|
obexdev=${obexdevpath%%/*}
|
|
|
|
obexpath=${obexdevpath#*/}
|
|
|
|
act "obex device: $obexdev"
|
|
|
|
act "obex path: $obexpath"
|
2012-01-17 18:01:20 +00:00
|
|
|
obexftp -b $obexdev -g $obexpath
|
|
|
|
if [[ $? != 0 ]]; then
|
|
|
|
rmdir ${keytmp}
|
|
|
|
die "a problem occurred retreiving the key via bluetooth."
|
|
|
|
fi
|
2011-11-17 13:47:40 +00:00
|
|
|
# print out the key on stdout
|
2012-01-17 18:01:20 +00:00
|
|
|
if option_is_set --path; then
|
|
|
|
echo $obexpath
|
|
|
|
# up to the caller to delete the key
|
|
|
|
# TODO: --path should have the directory where to put the key
|
|
|
|
# as argument, instead of creating keytmp
|
|
|
|
else
|
|
|
|
cat `pwd`/$obexpath >&1
|
|
|
|
# wipe out the key
|
|
|
|
${WIPE[@]} $obexpath
|
|
|
|
cd -
|
|
|
|
rmdir ${keytmp}
|
|
|
|
fi
|
|
|
|
|
2011-11-15 14:50:09 +00:00
|
|
|
# tombkey="basename $obexpath"
|
|
|
|
;;
|
2012-01-17 18:01:20 +00:00
|
|
|
|
2011-11-15 14:50:09 +00:00
|
|
|
file)
|
|
|
|
if ! [[ -f $keypath ]]; then
|
2012-01-28 03:15:23 +00:00
|
|
|
_warning "Invalid path $keypath"
|
2011-11-15 14:50:09 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2012-01-28 03:15:23 +00:00
|
|
|
if option_is_set --path; then
|
2011-11-16 21:49:08 +00:00
|
|
|
key_found $scheme://$keypath;
|
2011-11-15 14:50:09 +00:00
|
|
|
else
|
|
|
|
< $keypath
|
|
|
|
r=$?
|
|
|
|
if [[ $r != 0 ]]; then return 1; fi
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
2011-11-16 21:49:08 +00:00
|
|
|
mounted)
|
|
|
|
for mountpoint in `cut -f2 /etc/mtab -d ' ' | sort -u`; do
|
|
|
|
undertaker_scheme ${print_path[@]} file ${mountpoint}/${keypath}
|
|
|
|
ret=$?
|
|
|
|
if [[ $ret == 0 ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
;;
|
2012-01-17 18:01:20 +00:00
|
|
|
|
2011-11-16 22:30:35 +00:00
|
|
|
udisks)
|
|
|
|
#It implements automounting using udisks; udisks is a (recently)
|
|
|
|
#new technology, so we can't rely on it being present
|
|
|
|
if ! which udisks &> /dev/null; then
|
2012-01-28 03:15:23 +00:00
|
|
|
_warning 'udisks not found'
|
2011-11-16 22:30:35 +00:00
|
|
|
exit 64
|
|
|
|
fi
|
|
|
|
while true; do
|
|
|
|
device=`udisks --monitor|egrep '/sd[a-z][0-9]' -o -m1`
|
|
|
|
device=/dev$device
|
|
|
|
udisks --mount $device
|
|
|
|
###get mountpoint for device
|
|
|
|
mountpoint=`egrep "^${device} " /etc/mtab|cut -d ' ' -f2`
|
|
|
|
undertaker_scheme ${print_path[@]} file ${mountpoint}/${keypath}
|
|
|
|
ret=$?
|
|
|
|
udisks --unmount $device
|
|
|
|
if [[ $ret == 0 ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
;;
|
2012-01-17 18:01:20 +00:00
|
|
|
|
2011-11-16 23:31:16 +00:00
|
|
|
near)
|
|
|
|
###Given the path to the tomb, search the key near to that
|
|
|
|
undertaker_scheme file ${keypath}.key
|
|
|
|
;;
|
2012-01-17 18:01:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-15 14:50:09 +00:00
|
|
|
*)
|
2011-11-16 20:52:57 +00:00
|
|
|
if ! which undertaker-$scheme &> /dev/null; then
|
2012-01-28 03:15:23 +00:00
|
|
|
_warning "url protocol not recognized: $scheme"
|
2011-11-16 20:52:57 +00:00
|
|
|
return 64
|
|
|
|
fi
|
2011-11-16 21:49:08 +00:00
|
|
|
undertaker-$scheme ${print_path[@]} ${scheme}://$keypath
|
2011-11-16 20:52:57 +00:00
|
|
|
return $?
|
2011-11-15 14:50:09 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
2012-01-17 18:01:20 +00:00
|
|
|
|
2011-11-15 14:50:09 +00:00
|
|
|
function main() {
|
2011-11-16 20:29:32 +00:00
|
|
|
typeset -A opts
|
2012-01-17 18:01:20 +00:00
|
|
|
zparseopts -M -E -D -Aopts -poll -path -batch
|
2011-11-15 14:50:09 +00:00
|
|
|
if ! [ $1 ] ; then
|
2012-01-29 15:16:06 +00:00
|
|
|
print "[W] an argument is missing, the undertaker is confused" >&2
|
|
|
|
print "usage: undertaker [options] url://host:path/to/tomb.key" >&2
|
2011-11-15 14:50:09 +00:00
|
|
|
exit 1;
|
|
|
|
fi
|
2011-11-16 20:29:32 +00:00
|
|
|
local -a tomb_opts
|
2012-01-17 18:01:20 +00:00
|
|
|
if [[ -n ${(k)opts[--batch]} ]]; then
|
2012-01-28 03:15:23 +00:00
|
|
|
tomb_opts+='--no-color'
|
|
|
|
tomb_opts+='--quiet'
|
2011-11-16 20:29:32 +00:00
|
|
|
fi
|
2011-11-16 21:49:08 +00:00
|
|
|
local -a under_opts
|
2012-01-17 18:01:20 +00:00
|
|
|
if [[ -n ${(k)opts[--path]} ]]; then
|
|
|
|
under_opts+='--path'
|
2011-11-16 21:49:08 +00:00
|
|
|
fi
|
2011-11-16 20:29:32 +00:00
|
|
|
local -A backupopts
|
2011-11-16 21:49:08 +00:00
|
|
|
for a in ${(k)opts}; do
|
|
|
|
backupopts[$a]=${opts[$a]}
|
|
|
|
done
|
2012-01-28 03:15:23 +00:00
|
|
|
source tomb ${tomb_opts[@]} source
|
|
|
|
TOMBEXEC=undertaker
|
2011-11-16 21:49:08 +00:00
|
|
|
for a in ${(k)backupopts}; do
|
|
|
|
opts[$a]=${backupopts[$a]}
|
|
|
|
done
|
2011-11-15 14:50:09 +00:00
|
|
|
check_bin
|
2011-11-17 13:47:40 +00:00
|
|
|
|
2012-01-28 03:15:23 +00:00
|
|
|
_success "Undertaker will look for $1"
|
2011-11-17 13:47:40 +00:00
|
|
|
|
2011-11-15 14:50:09 +00:00
|
|
|
ARG1=${1}
|
|
|
|
scheme=${ARG1%://*}
|
|
|
|
keypath=${ARG1#*//}
|
|
|
|
|
|
|
|
if [[ -n ${(k)opts[--poll]} ]]; then
|
|
|
|
while true; do
|
2011-11-16 20:29:32 +00:00
|
|
|
progress poll 0 search
|
2011-11-16 21:49:08 +00:00
|
|
|
undertaker_scheme ${under_opts[@]} $scheme $keypath
|
2011-11-15 14:50:09 +00:00
|
|
|
r=$?
|
|
|
|
if [[ $r == 64 ]]; then
|
|
|
|
exit 64
|
|
|
|
fi
|
2011-11-16 20:29:32 +00:00
|
|
|
progress poll 100 done
|
2011-11-15 14:50:09 +00:00
|
|
|
sleep 3
|
|
|
|
done
|
|
|
|
else
|
2011-11-16 21:49:08 +00:00
|
|
|
undertaker_scheme ${under_opts[@]} $scheme $keypath
|
2011-11-15 14:50:09 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
main $*
|
|
|
|
|
|
|
|
### Conventions and other comments:
|
|
|
|
#
|
|
|
|
# EXIT CODES FOR SCHEME HANDLERS
|
|
|
|
# 0 is for everything went fine
|
|
|
|
# 64 is for "not supported/the problem won't be solved by polling". This is for things like: unmet dependencies, not supported at all, etc
|
|
|
|
# everything else means just "error". Use 1, please. So other codes can be used if needed
|
|
|
|
#
|