#!/bin/sh # send mail script for banIP notifications # written by Dirk Brenken (dev@brenken.org) # # This is free software, licensed under the GNU General Public License v3. # # (s)hellcheck exceptions # shellcheck disable=1091,2030,2031,2034,2039,2086,2129,2140,2143,2154,2181,2183,2188 export LC_ALL=C export PATH="/usr/sbin:/usr/bin:/sbin:/bin" set -o pipefail if [ -r "/lib/functions.sh" ] then . "/lib/functions.sh" ban_debug="$(uci_get banip global ban_debug "0")" ban_loglimit="$(uci_get banip global ban_loglimit "100")" ban_mailsender="$(uci_get banip global ban_mailsender "no-reply@banIP")" ban_mailreceiver="$(uci_get banip global ban_mailreceiver)" ban_mailtopic="$(uci_get banip global ban_mailtopic "banIP notification")" ban_mailprofile="$(uci_get banip global ban_mailprofile "ban_notify")" fi ban_ver="${1}" ban_mail="$(command -v msmtp)" ban_logger="$(command -v logger)" ban_logread="$(command -v logread)" ban_rc=1 f_log() { local class="${1}" log_msg="${2}" if [ -x "${ban_logger}" ] then "${ban_logger}" -p "${class}" -t "banIP-${ban_ver%-*}[${$}]" "${log_msg}" else printf "%s %s %s\n" "${class}" "banIP-${ban_ver%-*}[${$}]" "${log_msg}" fi } if [ -z "${ban_mailreceiver}" ] then f_log "err" "please set the mail receiver with the 'ban_mailreceiver' option" exit ${ban_rc} fi if [ "${ban_debug}" = "1" ] then msmtp_debug="--debug" fi 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" # info preparation # sys_info="$(strings /etc/banner 2>/dev/null)" ban_info="$(/etc/init.d/banip "status" 2>/dev/null)" rep_info="${2}" 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}')" # mail body # ban_mailtext="
"
ban_mailtext="${ban_mailtext}\n++\n++ System Information ++\n++\n${sys_info}"
ban_mailtext="${ban_mailtext}\n\n++\n++ banIP Status ++\n++\n${ban_info}"
if [ -n "${rep_info}" ]
then
	ban_mailtext="${ban_mailtext}\n\n++\n++ banIP Report ++\n++\n${rep_info}"
fi
ban_mailtext="${ban_mailtext}\n\n++\n++ Logfile Information ++\n++\n${log_info}"
ban_mailtext="${ban_mailtext}
" # send mail # if [ -x "${ban_mail}" ] then printf "%b" "${ban_mailhead}${ban_mailtext}" 2>/dev/null | "${ban_mail}" ${msmtp_debug} -a "${ban_mailprofile}" "${ban_mailreceiver}" >/dev/null 2>&1 ban_rc=${?} f_log "info" "mail sent to '${ban_mailreceiver}' with rc '${ban_rc}'" else f_log "err" "msmtp mail daemon not found" fi exit ${ban_rc}