✏️ 编辑:sendmail
路径:
/usr/lib/sendmail
大小:4.05 KB · 修改:2023-12-28 05:40:20 · 权限:0755 · 只读(保存会失败)
← 返回目录
👁 查看
⬇ 下载
#!/bin/bash SENDMAIL='/var/qmail/bin/sendmail.qmail' USER=$(id -un) MIN_CUSTOMER_UID=10003 #BASEDIR=/tmp/mail-abuse BASEDIR=/var/cache/mail-abuse HISTORY_DB="${BASEDIR}/sendmail_history.${USER}.sqlite3" MAX_SEND_MAIL=500 MAX_SEND_MAIL_TRIAL=100 TIME_PERIOD='-60 minutes' KEEP_HISTORY='-7 days' TRIAL_DAYS=7 USER_HOME_BASE='/virtual' TIMESTAMP=$(date --date "${TRIAL_DAYS} days ago" +'%Y%m%d%H%M') CHECKFILE="${BASEDIR}/trialcheck.${USER}" # white list ids 202312 WHITELIST_UIDS=(10001 10002 10003) # Example UIDs in whitelist # Get the hostname HOSTNAME=$(hostname) # Adjust MAX_SEND_MAIL based on hostname patterns 202312 if [[ $HOSTNAME == *s*.xrea.com* ]]; then MAX_SEND_MAIL=1000 elif [[ $HOSTNAME == *m*.xrea.com* ]]; then MAX_SEND_MAIL=100 elif [[ $HOSTNAME == *s*.coreserver.jp* ]]; then MAX_SEND_MAIL=2000 elif [[ $HOSTNAME == *m*.coreserver.jp* ]]; then MAX_SEND_MAIL=1000 elif [[ $HOSTNAME == *b*.coreserver.jp* ]]; then MAX_SEND_MAIL=3000 elif [[ $HOSTNAME == *c*.coreserver.jp* ]]; then MAX_SEND_MAIL=10000 elif [[ $HOSTNAME == *s*.valueserver.jp* ]]; then MAX_SEND_MAIL=2000 elif [[ $HOSTNAME == *e*.valueserver.jp* ]]; then MAX_SEND_MAIL=1000 elif [[ $HOSTNAME == *b*.valueserver.jp* ]]; then MAX_SEND_MAIL=5000 fi init_sqlite3(){ echo "CREATE TABLE IF NOT EXISTS history ( id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, pwd TEXT, count INTEGER, created_at DATETIME );" | sqlite3 ${HISTORY_DB} chmod 700 ${HISTORY_DB} } count_recipients(){ echo -e "${STDIN}" | while read line do if [[ $line = '' ]];then echo "${RCPT_SUM}" break fi if [[ $line =~ ^[Tt][Oo] ]] || [[ $line =~ ^[Bb]?[Cc][Cc] ]];then for rcpt in `echo $line | tr ',' '\n'` do if [[ ${rcpt} =~ @ ]];then RCPT_SUM=$(( ${RCPT_SUM=} + 1 )) fi done fi done } add_history(){ echo "INSERT INTO history (user,pwd,count,created_at) values ('${USER}','$1',$2,datetime('now', 'localtime'));" | sqlite3 ${HISTORY_DB} } count_recent(){ echo "SELECT sum(count) FROM history WHERE user = '${USER}' AND created_at > datetime('now','localtime','${TIME_PERIOD}');" | sqlite3 ${HISTORY_DB} } clear_old_history(){ echo "DELETE FROM history WHERE created_at < datetime('now','localtime','${KEEP_HISTORY}');" | sqlite3 ${HISTORY_DB} } is_user_account(){ ## user account's UID is begin from MIN_CUSTOMER_UID if [[ ${UID} -gt ${MIN_CUSTOMER_UID} ]];then echo "1" fi } get_sendlimit(){ touch -t ${TIMESTAMP} ${CHECKFILE} chmod 700 ${CHECKFILE} user_home=$(echo $1 | cut -d/ -f1-3) if [[ "${user_home}" =~ "${USER_HOME_BASE}" && ${user_home} -nt ${CHECKFILE} ]];then echo ${MAX_SEND_MAIL_TRIAL} else echo ${MAX_SEND_MAIL} fi rm -f ${CHECKFILE} } # Whitelist implementation if [[ " ${WHITELIST_UIDS[@]} " =~ " ${UID} " ]]; then STDIN=$(cat -) echo "${STDIN}" | ${SENDMAIL} $* exit 0 fi ## log caller information logger -t "sendmail_wrapper" -p mail.info "PWD=${PWD}" ## check and create working dir if [[ -f ${BASEDIR} ]];then rm -f ${BASEDIR} fi if [[ ! -d ${BASEDIR} ]];then mkdir ${BASEDIR} chmod 1777 ${BASEDIR} fi ## init sqlite3 db file for history if [[ ! -f ${HISTORY_DB} ]];then init_sqlite3 fi ## exit sendmail if this user sent more than limit ## system account can send mail anyway IS_USER_ACCOUNT=$( is_user_account ) RECENT=$( count_recent ) SEND_LIMIT=$( get_sendlimit ${PWD} ) if [[ ${IS_USER_ACCOUNT} ]] && [[ ${RECENT} -gt ${SEND_LIMIT} ]];then echo "sendmail is not allowed. You sent too many mails" exit 1 fi ## run sendmail first. STDIN=$(cat -) echo "${STDIN}" | ${SENDMAIL} $* ## if non-user(system) account, don't have to record send history if [[ ! ${IS_USER_ACCOUNT} ]]; then exit 0 fi ## count recipients RCPTS=$( count_recipients ) if [[ ! -n "${RCPTS}" ]];then RCPTS=0 fi ## store to sendmail history add_history ${PWD} ${RCPTS} ## suppress sqlite database grow too large clear_old_history
💾 保存
取消
保存为 UTF-8,换行统一为 LF