debian31r2 postfix automatic setup script


关键词

debian31r2 postfix automatic setup script

摘要

#!/bin/bash

################################################################################################
# Somthing to say
#
# Author:sosogh
#
# Date:2007.2.10
#
# The script will ask you some questions(it is the software asking you question in fact) ,
#  so "donot" go anywhere when excute this script
#
# This script  is just to implement the setup of the mail system . It is not to show the usage
# of every software.The usage of every software  leaves to you.
#  
# I have test the scrpit on my vmware and my real pc,it worked well.If u want to use this script,
#
# MAKE SURE THAT TEST IT ON YOUR VMWARE BEFORE YOU INSTALL IT ON YOUR REAL PC.
#
# AND I DON'T  GUARANTEE THAT IT WILL WORK WELL ON YOUR PC
#
################################################################################################

#!/bin/bash

################################################################################################
# Somthing to say
#
# Author:sosogh
#
# Date:2007.2.10
#
# The script will ask you some questions(it is the software asking you question in fact) ,
#  so "donot" go anywhere when excute this script
#
# This script  is just to implement the setup of the mail system . It is not to show the usage
# of every software.The usage of every software  leaves to you.
#  
# I have test the scrpit on my vmware and my real pc,it worked well.If u want to use this script,
#
# MAKE SURE THAT TEST IT ON YOUR VMWARE BEFORE YOU INSTALL IT ON YOUR REAL PC.
#
# AND I DON'T  GUARANTEE THAT IT WILL WORK WELL ON YOUR PC
#
################################################################################################

###########################################################################################
#
# I leave it up to you for any other issues. Absolutely no warranty of any kind.
# Use entirely at your own risk.
# The author accepts no responsibility for any loss or damage caused by the use, lack of use,
# or misuse, of information contained in this script
#
#############################################################################################


############################################################################################      
#
#             postfix2.1.5  cyrus-sasl couirer(auth imap pop maildrop)  postfixadmin  
#                     Amavis-new  Spamassasin  Clamav  squirrelmail
#
#############################################################################################

######################
# Some notes:
######################

##############################################################################################
#
# The course of SMTP authentication is illustrated as follow:
#
# client pc ----> postfix -----> sasl ----> authdaemon  --------> authmysql ----------> Mysql
#                                        
#
###############################################################################################

###############################################################################################
#
# The course of POP3 authentication is illustrated as follow:
#
# client pc -----> POP3 -----> authdaemon  --------> authmysql ----------> Mysql
#                            
#
###############################################################################################

###########################################################
#  
#  My source list is :
#  deb http://debian.cn99.com/debian/ stable main
#
###########################################################

##<---------- SECTION 1  prepation --------->##

##############
# prepation
##############
sed -i '/^deb http:\/\/security.debian.org/s/\(.*\)/#\1/'  /etc/apt/sources.list
apt-get update
apt-get -y install sudo
groupadd  -g 108 vmail
useradd  -g 108 -u 108  vmail

##<---------- SECTION 2  pop3Server --------->##

###################################
# Install and set the pop3 server
###################################

apt-get -y install courier-pop courier-authmysql
sed -i 's/^authmodulelist=.*/authmodulelist="authmysql"/' /etc/courier/authdaemonrc
chmod +x /var/run/courier/authdaemon
mkdir  /vmail
chown vmail.vmail  /vmail

###########################
# Configure the Authmysqlrc
###########################

mv /etc/courier/authmysqlrc  /etc/courier/authmysqlrc.bakup
echo -e 'MYSQL_SERVER\tlocalhost' >> /etc/courier/authmysqlrc
echo -e 'MYSQL_USERNAME\tpostfixadmin' >> /etc/courier/authmysqlrc
echo -e 'MYSQL_PASSWORD\tpostfixadmin' >> /etc/courier/authmysqlrc
echo -e 'MYSQL_SOCKET\t/var/run/mysqld/mysqld.sock' >> /etc/courier/authmysqlrc
echo -e 'MYSQL_OPT\t0' >> /etc/courier/authmysqlrc
echo -e 'MYSQL_DATABASE\tpostfix' >> /etc/courier/authmysqlrc
echo -e 'MYSQL_USER_TABLE\tmailbox' >> /etc/courier/authmysqlrc
echo -e 'MYSQL_CRYPT_PWFIELD\tpassword' >> /etc/courier/authmysqlrc
echo -e 'MYSQL_UID_FIELD\t108' >> /etc/courier/authmysqlrc
echo -e 'MYSQL_GID_FIELD\t108' >> /etc/courier/authmysqlrc
echo -e 'MYSQL_LOGIN_FIELD\tusername' >> /etc/courier/authmysqlrc
echo -e "MYSQL_HOME_FIELD\t'/vmail/'" >> /etc/courier/authmysqlrc
echo -e 'MYSQL_NAME_FIELD\tname' >> /etc/courier/authmysqlrc
echo -e 'MYSQL_MAILDIR_FIELD\tmaildir' >> /etc/courier/authmysqlrc
echo -e "MYSQL_WHERE_CLAUSE\tactive='1'" >> /etc/courier/authmysqlrc

##<---------- SECTION 3  apache--------->##

###########################
# Install apache2
###########################
apt-get -y install apache2  apache2-mpm-prefork

####################################
# Install PHP4 Support For Apache2
####################################
apt-get -y install libapache2-mod-php4 php4-cli php4-common php4-cgi


#####################################
# Configure apache2 for PHP4 Support
#####################################
mkdir /etc/apache2/modules
cp /usr/lib/apache2/modules/libphp4.so  /etc/apache2/modules/
echo 'AddType application/x-httpd-php .php' > /etc/apache2/conf.d/php4.conf

##<---------- SECTION 4 Mysql --------->##

####################################
# Install Mysql-Server-4.1
####################################
apt-get -y install mysql-server-4.1

#######################################
# Preption for Mysql and Postfixadmin
#######################################
m='/usr/bin/mysql'
pa='postfixadmin'

##################################################################
# Init Mysql and set a root account for Mysql password 123456
##################################################################
/usr/bin/mysql_install_db
/usr/bin/mysqladmin -u root password '123456'

################################################
# Install and Configure Mysql Module for PHP4
################################################
apt-get -y install php4-mysql
sed -i 's#^\(extension=\)\(mysql.so\)#\1/usr/lib/php4/20020429/\2#' /etc/php4/apache2/php.ini
echo 'extension_dir = "./"' >> /etc/php4/apache2/php.ini
/etc/init.d/apache2  restart

##<---------- SECTION 5  postfixadmin --------->##

####################################
# Download postfixadmin-2.1.0
####################################
cd /var/www/
wget -c http://high5.net/page7_files/postfixadmin-2.1.0.tgz
tar zxf postfixadmin-2.1.0.tgz
mv postfixadmin-2.1.0  pa

##########################################
# Import the Mailbox infor into Mysql
##########################################
/usr/bin/mysql -uroot -p123456  <  /var/www/pa/DATABASE_MYSQL.TXT

####################################
# Change some permissions
####################################
chown  -R www-data.www-data /var/www/pa
cd /var/www/pa/
chmod 640 *.php *.css
cd /var/www/pa/admin/
chmod 640 *.php .ht*
cd /var/www/pa/images/
chmod 640  *.png
cd /var/www/pa/languages/
chmod 640 *.lang
cd /var/www/pa/templates/
chmod 640 *.tpl
cd /var/www/pa/users/
chmod 640 *.php

####################################
# Init Postfixadmin
####################################
cd /var/www/pa/
cp config.inc.php.sample  config.inc.php
/usr/bin/php4 /var/www/pa/setup.php > /dev/null && echo 'postfixadmin Setup Done'

############################################
# Issues after Initting  Postfixadmin
############################################
rm -f /var/www/pa/setup.php
mv /var/www/pa/motd-admin.txt  /var/www/pa/motd-admin.txt.bakup
cp  /var/www/pa/admin/create-mailbox.php /var/www/pa/admin/create-mailbox.php.bakup


##############################################################################################
#
# When using postfixadmin creat a user,it just add the user infor into database(Mysql).
# It does not creat the Maildir for that user,so we change a little bit code of
# the creat-mailbox.php to make postfixadmin create the Maildir for a user when creating it  
#
##############################################################################################

cm_dir='/var/www/pa/admin/create-mailbox.php'

toadd1='$fullmaildir = "/vmail/" . $maildir;'
toadd2='$userhome = "/vmail/" . $fDomain . "/" . $myusername;'
toadd3='system("sudo -u vmail mkdir -p $userhome");'
toadd4='system("sudo -u vmail maildirmake  $fullmaildir");'
sed -i "176s#\(.*\)#\1\n$toadd1\n\n$toadd2\n$toadd3\n$toadd4#g"  $cm_dir
sed -i '60s/\(.*\)/$myusername = escape_string ($_POST['fUsername']);\n\1/g' $cm_dir
sed -i '149s/.*/$maildir = $fDomain . \"\/\" . $myusername . \"\/\" . \"Maildir\/\";/g' $cm_dir


##############################################################################################
#
# When using postfixadmin DELETE a user,it just DELETE the user infor from database(Mysql).
# It does not DELETE the Maildir for that user,so we change a little bit code of
# the delete.php to make postfixadmin DELETE the Maildir for a user when DELETing it  
#
##############################################################################################

toaddone='$pieces = explode("@", $fDelete);'
toaddtwo='$todel = "/vmail/" . $fDomain . "/" . $pieces[0];'
toaddthree='system("sudo -u vmail  rm -rf $todel");'
sed -i "93s#\(.*\)#\1\n$toaddone\n$toaddtwo\n$toaddthree#g"  /var/www/pa/admin/delete.php


##########################################################################################
#
# create the sudoers entry:
# to make apache has permistion to excute the cmd (maildirmake and  mkdir )as Vmail ID
#
##########################################################################################

echo "www-data   ALL=(vmail)NOPASSWD: ALL"   >> /etc/sudoers

##<----------- SECTION 6  postfix ----------->##

###########################
# Install the postfix deb
###########################
apt-get -y install postfix postfix-mysql


##<---------- SECTION 7  Virtual Domain Users--------->##

#################################################
# Set the postfix Virtual Domain Users infor
#################################################
sed  -i  '/^smtp.*smtpd/s/.*/smtp inet n  -  n  -  -  smtpd/' /etc/postfix/master.cf
m='/usr/bin/mysql -uroot -p123456 -e'
$m 'GRANT ALL PRIVILEGES ON postfix.* TO "postfix"@"127.0.0.1" identified by "postfix";'


postconf -e 'virtual_mailbox_domains = mysql:/etc/postfix/mysql-vd.cf'
postconf -e 'virtual_mailbox_maps = mysql:/etc/postfix/mysql-vm.cf'
postconf -e 'virtual_mailbox_base = /vmail'
postconf -e 'virtual_uid_maps = static:108'
postconf -e 'virtual_gid_maps = static:108'


echo 'user = postfix' >> /etc/postfix/mysql-vd.cf
echo 'password = postfix' >>  /etc/postfix/mysql-vd.cf
echo 'dbname = postfix' >>  /etc/postfix/mysql-vd.cf
echo 'table = mailbox' >>  /etc/postfix/mysql-vd.cf
echo 'select_field = domain' >>  /etc/postfix/mysql-vd.cf
echo 'where_field = domain' >>  /etc/postfix/mysql-vd.cf
echo 'hosts = 127.0.0.1'  >> /etc/postfix/mysql-vd.cf

echo 'user = postfix' >> /etc/postfix/mysql-vm.cf
echo 'password = postfix' >>  /etc/postfix/mysql-vm.cf
echo 'dbname = postfix' >>  /etc/postfix/mysql-vm.cf
echo 'table = mailbox' >>  /etc/postfix/mysql-vm.cf
echo 'select_field = maildir' >>  /etc/postfix/mysql-vm.cf
echo 'where_field = username' >>  /etc/postfix/mysql-vm.cf
echo 'hosts = 127.0.0.1'  >> /etc/postfix/mysql-vm.cf

##<---------- SECTION 8 Maildrop  -------->##

##################################################################
# Install Maildrop and Configrue it
# This version of maildrop.deb support Mysql.Thanks to andot
##################################################################
cd /tmp
wget -c http://download.coolcode.cn/maildrop_1.5.3-1.1sarge1_i386.deb
dpkg -i /tmp/maildrop_1.5.3-1.1sarge1_i386.deb
postconf -e  "virtual_transport = maildrop"
postconf -e  "maildrop_destination_recipient_limit = 1"
postconf -e  "maildrop_destination_concurrency_limit = 2"
sed -i '/^maildrop/{n;s/local\///;s/\(-d.*\)/-w 90 \1/}'  /etc/postfix/master.cf


echo "hostname 127.0.0.1" >> /etc/maildropmysql.config
echo "port 3306" >> /etc/maildropmysql.config
echo "database postfix" >> /etc/maildropmysql.config
echo "dbuser postfix " >> /etc/maildropmysql.config
echo "dbpw postfix" >> /etc/maildropmysql.config
echo "dbtable mailbox" >> /etc/maildropmysql.config
echo "default_uidnumber  108" >> /etc/maildropmysql.config
echo "default_gidnumber  108" >> /etc/maildropmysql.config
echo "uidnumber_field 108" >> /etc/maildropmysql.config
echo "gidnumber_field 108" >> /etc/maildropmysql.config
echo "uid_field username" >> /etc/maildropmysql.config
echo "homedirectory_field '/vmail/' " >> /etc/maildropmysql.config
echo "maildir_field concat('/vmail/',maildir)" >> /etc/maildropmysql.config
echo "quota_field quota" >> /etc/maildropmysql.config
echo "mailstatus_field active" >> /etc/maildropmysql.config


##<---------- SECTION 9  sasl for postfix  -------->##

#########################
# Install and set sasl
#########################

apt-get -y install  postfix-tls sasl2-bin libsasl2-modules
mkdir -p /etc/postfix/sasl
echo "pwcheck_method: authdaemond" >> /etc/postfix/sasl/smtpd.conf
echo "log_level: 3" >> /etc/postfix/sasl/smtpd.conf
echo "mech_list:  LOGIN" >> /etc/postfix/sasl/smtpd.conf
echo "authdaemond_path:/var/run/courier/authdaemon/socket" >> /etc/postfix/sasl/smtpd.conf


postconf -e  "smtpd_sasl_auth_enable = yes"
postconf -e  "broken_sasl_auth_clients = yes"
postconf -e  "smtpd_sasl_security_options = noanonymous"


echo "smtpd_recipient_restrictions =" >> /etc/postfix/main.cf
echo "  permit_mynetworks," >> /etc/postfix/main.cf
echo "  permit_sasl_authenticated," >> /etc/postfix/main.cf
echo "  reject_non_fqdn_hostname," >> /etc/postfix/main.cf
echo "  reject_non_fqdn_sender," >> /etc/postfix/main.cf
echo "  reject_non_fqdn_recipient," >> /etc/postfix/main.cf
echo "  reject_unauth_destination," >> /etc/postfix/main.cf
echo "  reject_unauth_pipelining," >> /etc/postfix/main.cf
echo "  reject_invalid_hostname" >> /etc/postfix/main.cf

##<---------- SECTION 10 Anti-Spam and Anti-Virus  -------->##

#############################################
# Install Amavis-new  Spamassasin Clamav
#############################################

url='deb http://ftp2.de.debian.org/debian-volatile/ sarge/volatile main'
echo "$url" >> /etc/apt/sources.list
apt-get update
apt-get install -y  zoo unzip unarj  bzip2   arc lzop
apt-get install -y amavisd-new spamassassin clamav clamav-base clamav-daemon clamav-freshclam
sed  -i  "/#\$forward_method = 'smtp:/s/^.//g"   /etc/amavis/amavisd.conf
sed -i 's/^ENABLED=0/ENABLED=1/g' /etc/default/spamassassin
gpasswd -a clamav amavis
sed -i 's/User clamav/User amavis/g' /etc/clamav/clamd.conf


chown -R amavis:amavis /var/run/clamav
chown -R amavis:amavis /var/lib/clamav
chown -R amavis:amavis /var/log/clamav


sed -i  '/DatabaseOwner/s/clamav/amavis/g'  /etc/clamav/freshclam.conf
sed -i  '/create 640/s/clamav/amavis/g' /etc/logrotate.d/clamav-daemon
sed -i '/create 640/s/clamav/amavis/g'  /etc/logrotate.d/clamav-freshclam


############################################################################################
#
# I just want:
# mails whose recipients are email addresses on our server(virtual or local) to be checked
#
# you can also do it in another way:
# set the relevant parameter in the amavis config file
#
############################################################################################
postconf -e  "virtual_transport = amavis:[127.0.0.1]:10024"


echo  "amavis unix - - n - 2 smtp"  >> /etc/postfix/master.cf
echo  "     -o smtp_data_done_timeout=1200"  >> /etc/postfix/master.cf
echo  "     -o smtp_send_xforward_command=yes"  >> /etc/postfix/master.cf
echo  "     -o disable_dns_lookups=yes"  >> /etc/postfix/master.cf


m_dir='/etc/postfix/master.cf'
csam_dir='/etc/postfix/check_sender_access_maps'
echo  "127.0.0.1:10025 inet n - y - - smtpd"  >> /etc/postfix/master.cf
echo  "      -o local_recipient_maps="  >> /etc/postfix/master.cf
echo  "      -o relay_recipient_maps="  >> /etc/postfix/master.cf
echo  "      -o smtpd_restriction_classes="  >> /etc/postfix/master.cf
echo  "      -o smtpd_client_restrictions="  >> /etc/postfix/master.cf
echo  "      -o smtpd_helo_restrictions="  >> /etc/postfix/master.cf
echo  "      -o smtpd_sender_restrictions=check_sender_access,regexp:$csam_dir"  >> $m_dir
echo  "      -o smtpd_recipient_restrictions=permit_mynetworks,reject" >> $m_dir
echo  "      -o mynetworks=127.0.0.0/8 "  >> /etc/postfix/master.cf
echo  "      -o strict_rfc821_envelopes=yes "  >> /etc/postfix/master.cf
echo  "      -o smtpd_error_sleep_time=0 "  >> /etc/postfix/master.cf
echo  "      -o smtpd_soft_error_limit=1001 "  >> /etc/postfix/master.cf
echo  "      -o smtpd_hard_error_limit=1000 "  >> /etc/postfix/master.cf
echo "/.*/   FILTER maildrop:"  > $csam_dir
postmap  /etc/postfix/check_sender_access_maps

##<---------- SECTION 11 imap  -------->##

##############################
# Install courier-imap
##############################

apt-get -y install   courier-imap

##<---------- SECTION 12 squirrelmail  -------->##

############################################
# Install squirrelmail  squirrelmail-locales
############################################

apt-get -y install squirrelmail squirrelmail-locales



###############################
# Configure squirrelmail
###############################
ln -s /etc/squirrelmail/apache.conf /etc/apache2/conf.d/squirrelmail.conf
sed -i '/RedirectMatch/s/apache2-default/squirrelmail/' /etc/apache2/sites-available/default


#############################################################
# Install and set Spam Locker: A before queue Anti-Spam tool
#############################################################

apt-get -y install libmd5-perl  libdigest-sha1-perl libdigest-hmac-perl
apt-get -y install libnet-ip-perl  libnet-dns-perl  
cd /usr/local/
wget -c http://blog.chinaunix.net/upfile/070204215603.gz
tar zxf 070204215603.gz
mv slockd-0.2beta1 /usr/local/slockd
sed -i '/setsid/s/# //g'  /usr/local/slockd/config/main.cf
cps='check_policy_service inet:127.0.0.1:10030'
sed -i "/invalid_hostname/s/\(.*\)/\1\n  $cps/g"  /etc/postfix/main.cf
ln -s /usr/local/slockd/slockd-init  /etc/rc2.d/S20slockd
ln -s /usr/local/slockd/slockd-init  /etc/init.d/slockd

/etc/init.d/slockd  start


###########################
# Restart STH.
###########################
/etc/init.d/courier-authdaemon restart
/etc/init.d/postfix  restart
/etc/init.d/apache2 restart
/etc/init.d/clamav-daemon  restart
/etc/init.d/clamav-freshclam  restart
/etc/init.d/amavis restart


echo "Finish,Enjoy it!"


#################################################################################
#
#  All the mails are stored in the directory /vmail
#  
#################################################################################


#################################################################################
#
#  After this setup,you can access postfixadmin (it is superuser) via this URL:
#  http://yourserver.com/pa/admin
#  It does not require any password and it is WORLD-ACCESSABLE.
#  So you must setup the access permisions for this resource correctly
#  through the Apache config files , And it leaves to you
#
##################################################################################


#######################################################################
#
#  You can find more information via the following websites:
#
#  http://www200.pair.com/mecham/spam/clamav-amavisd-new.html
#  http://www.linuxnarede.com.br/artigos/fullnews.php?id=84
#  http://www.fatofthelan.com/articles/articles.php?pid=22
#  http://www.coolcode.cn/?p=235&pp=12#pp0
#  http://wiki.debian.org.tw/index.php/Postfix-SASL
#  http://www.xnote.com/howto/postfix_mysql_courier.html
#  http://www.debianhelp.co.uk/apache2.htm
#  http://www.debian-administration.org/articles/357
#
#######################################################################

 

要饭二维码

洪哥写文章很苦逼,如果本文对您略有帮助,可以扫描下方二维码支持洪哥!金额随意,先行谢过!大家的支持是我前进的动力!

文章的版权

本文属于“洪哥笔记”原创文章,转载请注明来源地址:debian31r2 postfix automatic setup script:http://www.splaybow.com/post/130521240120082519.html

如果您在服务器运维、网络管理、网站或系统开发过程有需要提供收费服务,请加QQ:115085382!十年运维经验,帮您省钱、让您放心!
亲,如果有需要,先存起来,方便以后再看啊!加入收藏夹的话,按Ctrl+D

« Postfix 日志脚本 postfix限制特定用户收发邮件的高级访问控制方法 »

相关文章:

腾讯企业邮箱POP,SMTP分别是什么  (2014/9/4 8:23:19)

邮件服务器架设  (2014/3/9 13:27:43)

winwebmail邮局附件大小  (2014/3/8 10:13:12)

Exchange服务器快速恢复的步骤  (2013/10/24 14:04:36)

使域名DNS解析支持DKIM  (2013/5/27 16:45:56)

邮局使用SPF  (2013/5/24 12:27:46)

用telnet来理解SMTP协议  (2013/5/24 12:23:38)

qmail+vpopmail+squirrelmail 安装经验谈  (2013/9/6 10:59:41)

LINUX中的QMail邮件安全问题  (2013/9/5 10:59:12)

用qmail/vpopmail/courier-imap/qmail-scanner/igenus打造邮件系统  (2013/8/31 10:59:13)