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.

84 lines
2.9 KiB

  1. #!/bin/sh
  2. # send mail script for banIP notifications
  3. # written by Dirk Brenken (dev@brenken.org)
  4. #
  5. # This is free software, licensed under the GNU General Public License v3.
  6. #
  7. # (s)hellcheck exceptions
  8. # shellcheck disable=1091,2030,2031,2034,2039,2086,2129,2140,2143,2154,2181,2183,2188
  9. export LC_ALL=C
  10. export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  11. set -o pipefail
  12. if [ -r "/lib/functions.sh" ]
  13. then
  14. . "/lib/functions.sh"
  15. ban_debug="$(uci_get banip global ban_debug "0")"
  16. ban_loglimit="$(uci_get banip global ban_loglimit "100")"
  17. ban_mailsender="$(uci_get banip global ban_mailsender "no-reply@banIP")"
  18. ban_mailreceiver="$(uci_get banip global ban_mailreceiver)"
  19. ban_mailtopic="$(uci_get banip global ban_mailtopic "banIP notification")"
  20. ban_mailprofile="$(uci_get banip global ban_mailprofile "ban_notify")"
  21. fi
  22. ban_ver="${1}"
  23. ban_mail="$(command -v msmtp)"
  24. ban_logger="$(command -v logger)"
  25. ban_logread="$(command -v logread)"
  26. ban_rc=1
  27. f_log()
  28. {
  29. local class="${1}" log_msg="${2}"
  30. if [ -x "${ban_logger}" ]
  31. then
  32. "${ban_logger}" -p "${class}" -t "banIP-${ban_ver%-*}[${$}]" "${log_msg}"
  33. else
  34. printf "%s %s %s\n" "${class}" "banIP-${ban_ver%-*}[${$}]" "${log_msg}"
  35. fi
  36. }
  37. if [ -z "${ban_mailreceiver}" ]
  38. then
  39. f_log "err" "please set the mail receiver with the 'ban_mailreceiver' option"
  40. exit ${ban_rc}
  41. fi
  42. if [ "${ban_debug}" = "1" ]
  43. then
  44. msmtp_debug="--debug"
  45. fi
  46. 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"
  47. # info preparation
  48. #
  49. sys_info="$(strings /etc/banner 2>/dev/null)"
  50. ban_info="$(/etc/init.d/banip "status" 2>/dev/null)"
  51. rep_info="${2}"
  52. 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}')"
  53. # mail body
  54. #
  55. ban_mailtext="<html><body><pre style='display:block;font-family:monospace;font-size:1rem;padding:20;background-color:#f3eee5;white-space:pre'>"
  56. ban_mailtext="${ban_mailtext}\n<strong>++\n++ System Information ++\n++</strong>\n${sys_info}"
  57. ban_mailtext="${ban_mailtext}\n\n<strong>++\n++ banIP Status ++\n++</strong>\n${ban_info}"
  58. if [ -n "${rep_info}" ]
  59. then
  60. ban_mailtext="${ban_mailtext}\n\n<strong>++\n++ banIP Report ++\n++</strong>\n${rep_info}"
  61. fi
  62. ban_mailtext="${ban_mailtext}\n\n<strong>++\n++ Logfile Information ++\n++</strong>\n${log_info}"
  63. ban_mailtext="${ban_mailtext}</pre></body></html>"
  64. # send mail
  65. #
  66. if [ -x "${ban_mail}" ]
  67. then
  68. printf "%b" "${ban_mailhead}${ban_mailtext}" 2>/dev/null | "${ban_mail}" ${msmtp_debug} -a "${ban_mailprofile}" "${ban_mailreceiver}" >/dev/null 2>&1
  69. ban_rc=${?}
  70. f_log "info" "mail sent to '${ban_mailreceiver}' with rc '${ban_rc}'"
  71. else
  72. f_log "err" "msmtp mail daemon not found"
  73. fi
  74. exit ${ban_rc}