vi /home/cross/.procmailrc
# chmod o+w /var/log/procmail.log
LOGFILE=/var/log/procmail.log
LOGABSTRACT=all
VERBOSE=onPATH=$PATH
SHELL=/bin/sh
#LANG=zh_TW.Big5
#LC_ALL=zh_TW.Big5#USERHOME=$HOME
#USER=`echo ${USERHOME##*/}`
SUBJ=`formail -zxSubject:`
SUBJ=`formail -zxSubject: | sed -e "s/['|"|$]//g"`
#SUBJ=`formail -zxSubject: | sed 's/=?.*?[bB]?(.*)=?=/1/'`
# Need to Compile mmencode
#SUBJ=`formail -zxSubject: | sed 's/=?.*?[bB]?(.*)=?=/1/'| mmencode -u `
#SUBJ=`echo $SUBJ | mmencode -u`
SUBJ=`perl /home/alex2/mail_parser $SUBJ`
FROM=`formail -zxFrom:`
FROM=`echo ${FROM##*<}`
FROM=`echo ${FROM%%>}`
TO=`formail -zxTo:|tr ',' '
'| sed 's/(.*) (.*)/2/'`
TO=`echo ${TO##*<}`
TO=`echo ${TO%%>}`
USER=`echo ${TO%%@*}`
HOME=/home/$USER
# 此設定檔可放到/etc/procmailrc底下全部有效, 或者使用者 $HOME/.procmailrc個別受限FGREP=/bin/fgrep
DEFAULTBOX=/var/mail/$USER
BLACKLISTBOX=$HOME/blacklistbox
TIME=`date +%F'.'%T'.'%N`
# chmod 777 /var/mail/quarantine
QUARANTINE=/var/mail/quarantine/
FILE=$TIME-$USER.eml
#TMP=`formail > $QUARANTINE$FILE`
DATE=`date +%F' '%T`DBHOST=lab100
DBNAME=procmail_test
DBUSER=procmail
DBPASS=procmail123456# 如果.whistlist是空白的,信件會放到 blacklistbox裡
WHITELIST =`echo "select sender from maillist where recipient='$TO'" | mysql -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME | sed '/sender/d'>$HOME/.whitelist`:0E
* ? (echo "$FROM" | $FGREP -i -f $HOME/.whitelist)
${DEFAULTBOX}# 只有沒列在 whitelist裡的信會被寫到 /var/mail/quarantine/xxx.eml (每一封信存一檔案), 並記錄到資料庫
:0E
*!? (echo "$FROM" | $FGREP -i -f $HOME/.whitelist)
`echo "insert into blacklistbox(timestamp,recipient,sender,subject,file) values ('$DATE','$TO','$FROM','$SUBJ','$FILE');" | mysql -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME` `formail > $QUARANTINE$FILE` /dev/null
# ${BLACKLISTBOX}
# $HOME/blacklistbox# create database
#
# CREATE DATABASE procmail_test;
#
# —
# — whitelist
# —
# CREATE TABLE `maillist` (
# `id` int(11) NOT NULL auto_increment,
# `recipient` varchar(128) NOT NULL default '',
# `sender` varchar(128) NOT NULL default '',
# PRIMARY KEY (`id`)
# ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
#
# —
# — blacklist recored
# —
# CREATE TABLE `blacklistbox` (
# `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
# `recipient` varchar(128) NOT NULL default '',
# `sender` varchar(128) NOT NULL default '',
# `subject` text NOT NULL,
# `file` text NOT NULL
# ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
vi /home/cross/mail_parser
#!/usr/bin/perl
$sub=$ARGV[0];
if ($sub=~ /=?S+?l(S)?/) {
if ($1 =~ /[Qq]/) {
$sub=decode_qp($sub);
}
elsif ($1 =~ /[Bb]/) {
$sub=decode_base64($sub);
}
}
elsif ($sub=~ /=[a-fA-F0-9][a-fA-F0-9]/) {
$sub=decode_qp($sub);
}
$subject.=$sub;
print $subject;
sub decode_qp {
my($string) = @_;
@buffer=split(/?/,$string);
$string = $buffer[3] if ($buffer[3] ne "");
$string =~ s/=([da-fA-F]{2})/pack("C", hex($1))/ge;
$string =~ /?=(.*)/;
$string =~ tr/_/ /;
$buffer[4]=~s/^=//;
$buffer[0]=~s/=$//;
$string="$buffer[0]$string$buffer[4]";
return($string);
}sub decode_base64 {
my($string) = @_;
my($string2);
@buffer=split(/?/,$string);
$string = $buffer[3] if ($buffer[3] ne "");
$string =~ s/=+$//;
$string =~ tr|A-Za-z0-9+/| -_|;
while($string =~ /(.{1,60})/gs) {
my($string3) = chr(32+length($1)*3/4);
$string2 .= unpack("u",$string3 . $1 );
}
$buffer[4]=~s/^=//;
$buffer[0]=~s/=$//;
$string2="$buffer[0]$string2$buffer[4]";
return($string2);
}
留言