You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.5 KiB

#!/bin/bash
#
# Handle DIDAFF commands
if [ -z "LOCK" ] ; then
exec flock $0 env LOCK=yes $0 $*
exit 1
fi
cd $(dirname $0)
. ./didaff.conf
usage() {
cat <<EOF >&2
Local commands
add [ <ip> ]* = add baddies
del [ <ip> ]* = remove baddies
list = clean database to default period and print it
clean [ <period> ] = remove baddies older than $period seconds
server = service the buddy port for remote commands
Remote commands:
tell add [ <ip> ]*
tell del [ <ip> ]*
EOF
}
############################################################
clean_baddies() {
OLD=$(date +"%s-${1-$TIMEOUT}"|bc -l)
echo "delete from baddies where time<=$OLD;" | sqlite $DB
}
case "$1" in
add)
shift
TIME=$(date +%s)
for b in $* ; do
[ "$b" = "#" ] && break
echo "insert into baddies values('$b','$TIME');"
done | sqlite $DB
;;
del)
shift
B="$(for b in $* ; do printf ",'%s'" $b ; done)"
echo "delete from baddies where ip in (${B:1});" | sqlite $DB
;;
list)
clean_baddies
WHAT=who
[ "$2" = all ] && WHAT=who,time
echo "select who,time from baddies;" | sqlite -separator ' ' $DB
;;
clean)
clean_baddies $2
;;
setup)
echo "create table baddies(who,time);" | sqlite $DB
;;
server)
socat UDP-RECVFROM:$PORT,fork exec:"$0 read"
;;
read)
D="$(openssl enc -aes-256-ctr -d -pbkdf2 -a -pass file:$PSK)"
case "${D%% *}" in
list|add|del)
eval $0 $D
;;
esac
;;
tell)
shift
echo "$*" | openssl enc -aes-256-ctr -pbkdf2 -a -pass file:$PSK | \
socat - UDP:$REMOTE:$PORT
;;
*)
usage
esac
true