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.

62 lines
2.7 KiB

  1. #!/bin/sh
  2. # send mail script for banIP notifications
  3. # Copyright (c) 2020-2021 Dirk Brenken (dev@brenken.org)
  4. # This is free software, licensed under the GNU General Public License v3.
  5. # (s)hellcheck exceptions
  6. # shellcheck disable=1091,3040
  7. # Please note: you have to setup the package 'msmtp' before using this script
  8. export LC_ALL=C
  9. export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  10. set -o pipefail
  11. . "/lib/functions.sh"
  12. ban_debug="$(uci_get banip global ban_debug "0")"
  13. ban_loglimit="$(uci_get banip global ban_loglimit "100")"
  14. ban_mailsender="$(uci_get banip global ban_mailsender "no-reply@banIP")"
  15. ban_mailreceiver="$(uci_get banip global ban_mailreceiver)"
  16. ban_mailtopic="$(uci_get banip global ban_mailtopic "banIP notification")"
  17. ban_mailprofile="$(uci_get banip global ban_mailprofile "ban_notify")"
  18. ban_mail="$(command -v msmtp)"
  19. ban_logger="$(command -v logger)"
  20. ban_logread="$(command -v logread)"
  21. if [ -z "${ban_mailreceiver}" ]; then
  22. f_log "err" "please set the mail receiver with the 'ban_mailreceiver' option"
  23. exit 1
  24. fi
  25. if [ "${ban_debug}" = "1" ]; then
  26. msmtp_debug="--debug"
  27. fi
  28. ban_mailhead="From: ${ban_mailsender}\nTo: ${ban_mailreceiver}\nSubject: ${ban_mailtopic}\nReply-to: ${ban_mailsender}\nMime-Version: 1.0\nContent-Type: text/html;charset=utf-8\nContent-Disposition: inline\n\n"
  29. # info preparation
  30. #
  31. sys_info="$(
  32. strings /etc/banner 2>/dev/null
  33. ubus call system board | awk 'BEGIN{FS="[{}\"]"}{if($2=="kernel"||$2=="hostname"||$2=="system"||$2=="model"||$2=="description")printf " + %-12s: %s\n",$2,$4}'
  34. )"
  35. ban_info="$(/etc/init.d/banip "status" 2>/dev/null)"
  36. rep_info="${1}"
  37. log_info="$("${ban_logread}" -l "${ban_loglimit}" -e "banIP-" 2>/dev/null | awk '{NR=1;max=120;if(length($0)>max+1)while($0){if(NR==1){print substr($0,1,max)}else{print substr($0,1,max)}{$0=substr($0,max+1);NR=NR+1}}else print}')"
  38. # mail body
  39. #
  40. ban_mailtext="<html><body><pre style='display:block;font-family:monospace;font-size:1rem;padding:20;background-color:#f3eee5;white-space:pre'>"
  41. ban_mailtext="${ban_mailtext}\n<strong>++\n++ System Information ++\n++</strong>\n${sys_info}"
  42. ban_mailtext="${ban_mailtext}\n\n<strong>++\n++ banIP Status ++\n++</strong>\n${ban_info}"
  43. if [ -n "${rep_info}" ]; then
  44. ban_mailtext="${ban_mailtext}\n\n<strong>++\n++ banIP Report ++\n++</strong>\n${rep_info}"
  45. fi
  46. ban_mailtext="${ban_mailtext}\n\n<strong>++\n++ Logfile Information ++\n++</strong>\n${log_info}"
  47. ban_mailtext="${ban_mailtext}</pre></body></html>"
  48. # send mail
  49. #
  50. printf "%b" "${ban_mailhead}${ban_mailtext}" 2>/dev/null | "${ban_mail}" ${msmtp_debug} -a "${ban_mailprofile}" "${ban_mailreceiver}" >/dev/null 2>&1
  51. "${ban_logger}" -p "info" -t "banIP-mail [${$}]" "mail sent to '${ban_mailreceiver}' with rc '${?}'" 2>/dev/null