#!/bin/sh # send mail script for banIP notifications # Copyright (c) 2020-2021 Dirk Brenken (dev@brenken.org) # This is free software, licensed under the GNU General Public License v3. # (s)hellcheck exceptions # shellcheck disable=1091,3040 # Please note: you have to setup the package 'msmtp' before using this script export LC_ALL=C export PATH="/usr/sbin:/usr/bin:/sbin:/bin" set -o pipefail . "/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")" ban_mail="$(command -v msmtp)" ban_logger="$(command -v logger)" ban_logread="$(command -v logread)" if [ -z "${ban_mailreceiver}" ]; then f_log "err" "please set the mail receiver with the 'ban_mailreceiver' option" exit 1 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 ubus call system board | awk 'BEGIN{FS="[{}\"]"}{if($2=="kernel"||$2=="hostname"||$2=="system"||$2=="model"||$2=="description")printf " + %-12s: %s\n",$2,$4}' )" ban_info="$(/etc/init.d/banip "status" 2>/dev/null)" rep_info="${1}" 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 # printf "%b" "${ban_mailhead}${ban_mailtext}" 2>/dev/null | "${ban_mail}" ${msmtp_debug} -a "${ban_mailprofile}" "${ban_mailreceiver}" >/dev/null 2>&1 "${ban_logger}" -p "info" -t "banIP-mail [${$}]" "mail sent to '${ban_mailreceiver}' with rc '${?}'" 2>/dev/null