#!/bin/sh
# Copylefted by hip0.
# Registered under GPL 2.0 and above.
# Script Description:
# checks for successful logins in $maillog gets the ips
# and then checks against the /etc/tcp.smtp file.
# If there are successfully logged in users whom ip
# do not figurate in /etc/tcp.smtp then we add
# the missing ips to /etc/tcp.smtp and rewrite the tcp.smtp
# cdb detabase.
# So the new added users will be happily relayed by our mailserver.
# Put this script on crontab and have a fun time.
# Done on 11.03.2004. P.S. I'm sick i'm tired almost dead :[

maillog=/var/log/maillog;
tcp_f=/etc/tcp.smtp;
temp=/tmp/tcp.smtp.temp;
qml_ctl=/usr/bin/apachectl;

tcp_l_c=$(wc -l $tcp_f | awk '{ print $1 }');
tcp_l_c_m_o=$(( $tcp_l_c - 1 ));

head -n $tcp_l_c_m_o $tcp_f >$temp;
for f in `cat $maillog | grep success | \
awk '{ print $10 }' | sed -e 's/:/ /g' |\
awk '{ print $2 }' | sort | \
uniq`; do 

if [ "$f" != "`grep $f $tcp_f | awk -F: '{ print $1 }'`" ]; \
then

echo $f:allow,RELAYCLIENT=\"\">>$temp;
fi 
done
tail -n 1 $tcp_f >>$temp;
if [ "`cmp $tcp_f $temp`" ]; then
cp -f $tcp_f ${tcp_f}.bak
mv $temp $tcp_f
$qml_ctl cdb 2>&1 >/dev/null;
fi
