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.

80 lines
1.5 KiB

  1. #!/bin/bash
  2. #
  3. # Handle DIDAFF commands
  4. if [ -z "LOCK" ] ; then
  5. exec flock $0 env LOCK=yes $0 $*
  6. exit 1
  7. fi
  8. cd $(dirname $0)
  9. . ./didaff.conf
  10. usage() {
  11. cat <<EOF >&2
  12. Local commands
  13. add [ <ip> ]* = add baddies
  14. del [ <ip> ]* = remove baddies
  15. list = clean database to default period and print it
  16. clean [ <period> ] = remove baddies older than $period seconds
  17. server = service the buddy port for remote commands
  18. Remote commands:
  19. tell add [ <ip> ]*
  20. tell del [ <ip> ]*
  21. EOF
  22. }
  23. ############################################################
  24. clean_baddies() {
  25. OLD=$(date +"%s-${1-$TIMEOUT}"|bc -l)
  26. echo "delete from baddies where time<=$OLD;" | sqlite $DB
  27. }
  28. case "$1" in
  29. add)
  30. shift
  31. TIME=$(date +%s)
  32. for b in $* ; do
  33. [ "$b" = "#" ] && break
  34. echo "insert into baddies values('$b','$TIME');"
  35. done | sqlite $DB
  36. ;;
  37. del)
  38. shift
  39. B="$(for b in $* ; do printf ",'%s'" $b ; done)"
  40. echo "delete from baddies where ip in (${B:1});" | sqlite $DB
  41. ;;
  42. list)
  43. clean_baddies
  44. WHAT=who
  45. [ "$2" = all ] && WHAT=who,time
  46. echo "select who,time from baddies;" | sqlite -separator ' ' $DB
  47. ;;
  48. clean)
  49. clean_baddies $2
  50. ;;
  51. setup)
  52. echo "create table baddies(who,time);" | sqlite $DB
  53. ;;
  54. server)
  55. socat UDP-RECVFROM:$PORT,fork exec:"$0 read"
  56. ;;
  57. read)
  58. D="$(openssl enc -aes-256-ctr -d -pbkdf2 -a -pass file:$PSK)"
  59. case "${D%% *}" in
  60. list|add|del)
  61. eval $0 $D
  62. ;;
  63. esac
  64. ;;
  65. tell)
  66. shift
  67. echo "$*" | openssl enc -aes-256-ctr -pbkdf2 -a -pass file:$PSK | \
  68. socat - UDP:$REMOTE:$PORT
  69. ;;
  70. *)
  71. usage
  72. esac
  73. true