#!/bin/bash


LANG=C


# dovecot-2.2.5.tar.gz

# courier-authlib-0.63.0.tar.bz2

# extman-1.1.tar.gz 

# extmail-1.2.tar.gz 

# Unix-Syslog-1.1.tar.gz 

# DBI-1.607.tar.gz 

# DBD-mysql-4.011.tar.gz 

# GD-2.41.tar.gz 

# File-Tail-0.99.3.tar.gz 


INST_DIR=/usr/src

sed -i '/SELINUX=/s/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

setenforce 0

iptables -F

service iptables save

chkconfig iptables off

yum clean all;yum list

grep postfix /etc/rc.local || echo '/usr/sbin/postfix start' >> /etc/rc.local

ldconfig

useradd -M -s /sbin/nologin dovecot

useradd -M -s /sbin/nologin dovenull

yum  -y install pam-devel

cd $INST_DIR

tar zxf dovecot-2.2.5.tar.gz -C /usr/src

cd /usr/src/dovecot-2.2.5/

./configure --sysconfdir=/etc --with-mysql --with-pam

make

make install

/bin/cp -rf doc/dovecot-initd.sh /etc/init.d/dovecot

chmod +x /etc/init.d/dovecot

chkconfig --add dovecot

/bin/cp -rpf doc/example-config/* /etc/dovecot/

auth   required pam_nologin.so

auth   include system-auth

account   include system-auth

session   include system-auth' > /etc/pam.d/dovecot

chown -R postfix /var/lib/postfix

postfix start

chown -R postfix /var/spool/postfix/private/

chown -R postfix /var/spool/postfix/public/

chown -R :postdrop /var/spool/postfix/public/

yum -y install libtool-ltdl-devel expect

cd $INST_DIR

tar jxf courier-authlib-0.63.0.tar.bz2 -C /usr/src/

cd /usr/src/courier-authlib-0.63.0/

./configure --prefix=/usr/local/authd --without-stdheaderdir --with-mysql-libs=/usr/local/mysql/lib/mysql --with-mysql-includes=/usr/local/mysql/include/mysql --with-mailuser=postfix --with-mailgroup=postfix --without-authpam --without-authldap --without-authpgsql --without-authcustom --without-authpipe --without-authuserdb --without-authpwd --without-authshadow 2> $INST_DIR/authlib.error1

make 2> $INST_DIR/authlib.error2

make install 2> $INST_DIR/authlib.error3

make install-configure 2> $INST_DIR/authlib.error4

echo '/usr/local/authd/lib/courier-authlib' > /etc/ld.so.conf.d/courier-authlib.conf

ldconfig

/bin/cp -f /usr/src/courier-authlib-0.63.0/courier-authlib.sysvinit /etc/init.d/authd

chmod +x /etc/init.d/authd

chkconfig --add authd

cd $INST_DIR

tar zxf extman-1.1.tar.gz -C /usr/src/

mysql -u root -p < /usr/src/extman-1.1/docs/extmail.sql

mysql -u root -p < /usr/src/extman-1.1/docs/init.sql

mkdir -p /mxdir/extmail.org/postmaster/Maildir/

chown -R postfix:postfix /mxdir/

cd  /usr/src/extman-1.1/docs 

cp mysql_virtual_*.cf  /etc/postfix/

chown  -R  :postfix  /etc/postfix/mysql_virtual_*.cf

chmod o-r  /etc/postfix/mysql_virtual_*.cf

sed -i '76a myhostname  = mail.wgl.com' /etc/postfix/main.cf

sed -i '84a mydomain  = wgl.com' /etc/postfix/main.cf

sed -i '99a myorigin  =  $mydomain' /etc/postfix/main.cf

sed -i '263a mynetworks  =  127.0.0.1' /etc/postfix/main.cf

echo "smtpd_sasl_auth_enable  = yes     

smtpd_sasl_security_options  = noanonymous

smtpd_recipient_restrictions  = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination 

virtual_mailbox_base  =  /mxdir

virtual_alias_maps  = mysql:/etc/postfix/mysql_virtual_alias_maps.cf

virtual_mailbox_maps  = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf

virtual_mailbox_domains  = mysql:/etc/postfix/mysql_virtual_domains_maps.cf

virtual_uid_maps  = static:1000

virtual_gid_maps  = static:1000

SYS_G_ABOOK_FILE_PATH = /usr/local/httpd/htdocs/extmail/globabook.cf

alias_maps = hash:/etc/aliases" >> /etc/postfix/main.cf

echo 'pwcheck_method:authdaemond

authdaemond_path:/usr/local/authd/var/spool/authdaemon/socket' > /etc/sasl2/smtpd.conf

chmod -R 755 /usr/local/authd/var/spool/authdaemon/

FILE=/usr/local/authd/etc/authlib/authmysqlrc

B=`find / -name mysql.sock`

sed -i 's/mysql.example.com/localhost/' $FILE

sed -i 's/admin/extmail/' $FILE

sed -i 's/var\/mysql\/mysql.sock\//tmp\/mysql.sock/' $FILE

sed -i '/MYSQL_DATABASE/s/mysql/extmail/' $FILE

sed -i '/MYSQL_USER_TABLE/s/passwd/mailbox/' $FILE

sed -i '/MYSQL_CRYPT_PWFIELD/s/crypt/password/' $FILE

sed -i '/MYSQL_UID_FIELD/s/uid/uidnumber/' $FILE

sed -i '/MYSQL_GID_FIELD/s/gid/gidnumber/' $FILE

sed -i '/YSQL_LOGIN_FIELD/s/id/username/' $FILE

sed -i "/MYSQL_HOME_FIELD/s/home/concat('\/mxdir\/',homedir)/" $FILE

sed -i "150a MYSQL_MAILDIR_FIELD    concat('\/mxdir\/',maildir)" $FILE

sed -i 's/\/var\/lib\/mysql\/mysql.sock/\/tmp\/mysql.sock/' $FILE

AUTH=/usr/local/authd/etc/authlib/authdaemonrc

sed -i '35a authmodulelist="authmysql"' $AUTH

sed -i '35a authmodulelistorig="authmysql" ' $AUTH

/etc/init.d/authd restart

postfix stop &> /dev/null ; postfix start

/etc/init.d/dovecot restart

/etc/init.d/authd restart

echo "mail_location  = maildir:/mxdir/%d/%n/Maildir" >> /etc/dovecot/conf.d/10-mail.conf 

cd  /etc/dovecot/conf.d/

cp auth-sql.conf.ext  10-auth-sql.conf

SQL=/etc/dovecot/dovecot-sql.conf.ext

sed -i '70a connect  = host=localhost dbname=extmail user=extmail password=extmail ' $SQL

sed -i  "116a password_query  =   SELECT username,password FROM mailbox WHERE username='%u'" $SQL

sed -i '70a default_pass_scheme  = CRYPT' $SQL

sed -i "125a user_query  = SELECT maildir,uidnumber,gidnumber FROM mailbox WHERE username='%u'" $SQL

/etc/init.d/dovecot restart

/usr/local/authd/sbin/authtest -s smtp [email protected] extmail | grep succeeded  &> /dev/null&& echo "authlib 认证成功" || echo "authlib 认证失败"

echo 'NameVirtualHost *:80

    ServerName mail.wgl.com

    DocumentRoot "/usr/local/httpd/htdocs/extmail/html"

    ScriptAlias /extmail/cgi/ /usr/local/httpd/htdocs/extmail/cgi/

    Alias /extmail  /usr/local/httpd/htdocs/extmail/html/

    ScriptAlias /extman/cgi/ /usr/local/httpd/htdocs/extman/cgi/

    Alias /extman /usr/local/httpd/htdocs/extman/html/

    SuexecUserGroup postfix postfix

' > /usr/local/httpd/conf/extra/httpd-vhosts.conf

grep CharsetDefault /usr/local/httpd/conf/httpd.conf || echo 'CharsetDefault utf8' >> /usr/loca

l/httpd/conf/httpd.conf

/etc/init.d/httpd restart

cd $INST_DIR

tar -zxf extman-1.1.tar.gz -C /usr/src/

mv extman-1.1 /usr/local/httpd/htdocs/extman

chown -R postfix:postfix /usr/local/httpd/htdocs/extman/cgi/

cd /usr/local/httpd/htdocs/extman/

cp webman.cf.default webman.cf

CONf=/usr/local/httpd/htdocs/extman/webman.cf

sed -i 's/SYS_CONFIG.*/SYS_CONFIG  =  \/usr\/local\/httpd\/htdocs\/extman/' $CONf

sed -i 's/SYS_LANGDIR.*/SYS_LANGDIR  =  \/usr\/local\/httpd\/htdocs\/extman\/lang/' $CONf

sed -i 's/SYS_TEMPLDIR.*/SYS_TEMPLDIR  = \/usr\/local\/httpd\/htdocs\/extman\/html/' $CONf

sed -i 's/SYS_MAILDIR_BASE.*/SYS_MAILDIR_BASE  =  \/mxdir/' $CONf

sed -i 's/SYS_SESS_DIR.*/SYS_SESS_DIR  =  \/tmp\/extman/' $CONf

sed -i '/SYS_CAPTCHA_ON/s/1/0/' $CONf

sed -i '/SYS_MYSQL_USER/s/db_user/webmail/' $CONf

sed -i '/SYS_MYSQL_PASS/s/db_pass/webmail/' $CONf 

sed -i '/SYS_MYSQL_SOCKET/s/\/var\/lib\/mysql\/mysql.sock/\/tmp\/mysql.sock/' $CONf

mkdir -p /tmp/extman

chown -R postfix:postfix /tmp/extman

[ -f /var/lib/mysql/ ] || mkdir /var/lib/mysql ; ln -s /tmp/mysql.sock /var/lib/mysql/

cd $INST_DIR

tar -zxf extmail-1.2.tar.gz -C /usr/src/

mv /usr/src/extmail-1.2 /usr/local/httpd/htdocs/extmail

cd /usr/local/httpd/htdocs/extmail/

cp webmail.cf.default webmail.cf 

chown -R postfix:postfix cgi/

CONF=/usr/local/httpd/htdocs/extmail/webmail.cf

sed -i 's/SYS_CONFIG.*/SYS_CONFIG  =  \/usr\/local\/httpd\/htdocs\/extmail/' $CONF

sed -i 's/SYS_LANGDIR.*/SYS_LANGDIR  =  \/usr\/local\/httpd\/htdocs\/extmail\/lang/' $CONF

sed -i 's/SYS_TEMPLDIR.*/SYS_TEMPLDIR  = \/usr\/local\/httpd\/htdocs\/extmail\/html/' $CONF

sed -i 's/SYS_MAILDIR_BASE.*/SYS_MAILDIR_BASE  =  \/mxdir/' $CONF

sed -i 's/SYS_SESS_DIR.*/SYS_SESS_DIR  =  \/tmp\/extman/' $CONF

sed -i '/SYS_CAPTCHA_ON/s/1/0/' $CONF

sed -i '/SYS_MYSQL_USER/s/db_user/extmail/' $CONF

sed -i '/SYS_MYSQL_PASS/s/db_pass/extmail/' $CONF 

sed -i '/SYS_MYSQL_SOCKET/s/\/var\/lib\/mysql\/mysql.sock/\/tmp\/mysql.sock/' $CONF

sed -i '/SYS_AUTHLIB_SOCKET/s/SYS.*/SYS_AUTHLIB_SOCKET  =  \/usr\/local\/authd\/var\/spool\/authdaemon\/socket/' $CONF

sed -i '/SYS_G_ABOOK_FILE_PATH/s/SYS.*/SYS_G_ABOOK_FILE_PATH  =  \/usr\/local\/httpd\/htdocs\/extmail\/globabook.cf/' $CONF

cd $INST_DIR

tar zxf Unix-Syslog-1.1.tar.gz -C /usr/src/

tar zxf DBI-1.607.tar.gz -C /usr/src/

tar zxf DBD-mysql-4.011.tar.gz -C /usr/src/

tar zxf GD-2.41.tar.gz -C /usr/src/

tar zxf File-Tail-0.99.3.tar.gz -C /usr/src/

export LANG='zh_CN.UTF-8'

cd /usr/src/Unix-Syslog-1.1/

perl Makefile.PL 2> $INST_DIR/Unix-Syslog.error

make 2>> $INST_DIR/Unix-Syslog.error

make install 2>> $INST_DIR/Unix-Syslog.error

cd /usr/src/DBI-1.607/

perl Makefile.PL 2> $INST_DIR/DBI.error

make 2>> $INST_DIR/DBI.error

make install 2>> $INST_DIR/DBI.error

cd /usr/src/DBD-mysql-4.011

perl Makefile.PL 2> $INST_DIR/DBD-mysql.error

make 2>> $INST_DIR/DBD-mysql.error

make install 2>> $INST_DIR/DBD-mysql.error

cd /usr/src/GD-2.41

perl Makefile.PL 2> $INST_DIR/DBD-mysql.error

make 2>> $INST_DIR/DBD-mysql.error

make install 2>> $INST_DIR/DBD-mysql.error

cd /usr/src/File-Tail-0.99.3

perl Makefile.PL ; make && make install

yum -y install rrdtool rrdtool-devel rrdtool-perl

yum -y install gd-devel

cd $INST_DIR

tar -zxf extman-1.1.tar.gz -C /usr/src/

/bin/cp -rf /usr/src/extman-1.1/addon/mailgraph_ext/ /usr/local/

/bin/cp -f /usr/local/mailgraph_ext/mailgraph-init /etc/init.d/mailgraphd

chkconfig --add mailgraphd

/etc/init.d/mailgraphd restart

pgrep -l cmdserver || /usr/local/httpd/htdocs/extman/daemon/cmdserver -d

grep cmdserver /etc/rc.local || echo '/usr/local/httpd/htdocs/extman/daemon/cmdserver -d' >> /etc/rc.local 

echo "数据库帐号/密码:extmail/extmail

                   webman/extman

邮箱帐号/密码:[email protected]/extmail

管理帐号/密码:[email protected]/extmail*123*

登陆mysql对extmail授权:grant all privileges on extmail.* to 'extmail'@'localhost' identified by 'extmail';

         对extweb授权: grant all privileges on extmail.* to 'webmail'@'localhost' identified by 'webmail';"

echo "查看所有邮箱用户:  select * from extmail.mailbox\G;"