歡迎您光臨本站 註冊首頁

postfix+dovecot+postfixadmin+mysql認證

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

  前段時間在弄postfix郵局,碰到了很多問題,最後原因都是由於看的那些教程沒有把細結部分寫出來,導致初學問題多多,這裡就把我的過程寫出來,本文是通過結合了CU上各位前輩的一些文章總結配置成功而寫的,供需要學習的同學參考
 
 首先先裝好apache+php+mysql
 
 步驟這裡不提了
 需要安裝以下軟體:postfix(一款MTA),postfixadmin(管理郵局帳號),cyrus-sasl(安全認證軟體),dovecot(一款安全pop軟體)
 
 wget http://dovecot.org/releases/1.2/dovecot-1.2.9.tar.gz
 wget http://downloads.sourceforg[…]_2.3.tar.gz?use_mirror=nchc
 wget ftp://ftp.andrew.cmu.edu/[…]/cyrus-sasl-2.1.23.tar.gz
 wget ftp://ftp.cuhk.edu.hk/[…]/postfix-2.6.5.tar.gz
 
 
 (1)安裝postfixadmin
 
 tar zxf postfixadmin_2.3.tar.gz
 mv postfixadmin_2.3 /usr/local/wwwroot/postfix
 cd /usr/local/wwwroot/postfix
 cp config.inc.php.sample config.inc.php
 vi config.inc.php
 
 找這行$CONF['default_language'] = 'en';
 改為$CONF['default_language'] = 'cn'; //讓postfixadmin 支持中文
 找到 $CONF['domain_in_mailbox'] = 'YES';
 改為$CONF['domain_in_mailbox'] = 'NO'; // If you don't want to have the domain in your mailbox set this to 'NO'.
 找到此行:$CONF['encrypt'] =
 改為CONF['encrypt'] = 'cleartext'; //明文存入mysql
 
 配置apache訪問postfixadmin
 
 vi /usr/local/apache/conf/httpd.conf
 
 


  DocumentRoot /usr/local/wwwroot/postfix
 
   Options FollowSymLinks
   DirectoryIndex index.php
   AllowOverride None
   AuthType Basic
   authname Private
   authuserfile /var/run/admin
 require valid-user
   Order allow,deny
   Allow from all
 

 


 
 生成認證文件
 
 /usr/local/apache/bin/htpasswd -c /var/run/admin admin //其中/var/run/admin為文件名,最後的admin為用戶名
 ew password:
 Re-type new password:
 Adding password for user admin
 輸入兩次密碼後生成用戶admin
 
 添加mysql用戶
 
 create database postfix;
 GRANT all privileges on postfix.* TO postfix@localhost IDENTIFIED BY 'PostFIX_1228';
 flush privileges;
 
 運行安裝嚮導前需要安裝php的imap擴展
 
 先安裝依賴包,否則會報錯
 
 yum -y install openssl openssl-devel pam pam-devel
 wget ftp://ftp.cac.washington.edu/mail/imap.tar.Z
 tar zxf imap.tar.z
 cd imap-2007a
 make lr5 PASSWDTYPE=std SSLTYPE=unix.nopwd IP6=4
 echo "set disable-plaintext nil" > /etc/c-client.cf
 mkdir /usr/local/imap-2007a
 mkdir /usr/local/imap-2007a/include/
 mkdir /usr/local/imap-2007a/lib/
 chmod -R 077 /usr/local/imap-2007a
 cp imapd/imapd /usr/sbin/
 cp c-client/*.h /usr/local/imap-2007a/include/
 cp c-client/*.c /usr/local/imap-2007a/lib/
 cp c-client/c-client.a /usr/local/imap-2007a/lib/libc-client.a
 cd /usr/local/src/php-5.2.10/ext/imap
 /usr/local/php/bin/phpize
 ./configure --with-php-config=/usr/local/php-config --with-imap-ssl --with-imap=/usr/local/imap-2007a
 make && make install
 
 然後在php.ini中加入 extension = "imap.so"
 重啟apache
 打開http://yourip/setup.php就可以開始postfixadmin的安裝了,安裝完后可以進行添加域和用戶的操作,這裡添加了域mail.abc.com,用戶名lsm@mail.abc.com
 
 (2)安裝cyrus-sasl
 
 tar zxf cyrus-sasl-2.1.23.tar.gz
 cd cyrus-sasl
 ./configure --enable-login --enable-sql --with-mysql=/usr/local/mysql/
 make && make install
 ln -s /usr/local/lib/sasl2 /usr/lib/sasl2
 
 (3)安裝postfix
 
 停用sendmail
 
 service sendmail stop
 chkconfig --level 345 sendmail off
 mv /usr/bin/newaliases /usr/bin/newaliases.orig
 mv /usr/bin/mailq /usr/bin/mailq.orig
 mv /usr/sbin/sendmail /usr/sbin/sendmail.orig
 groupadd -g 12345 postfix
 useradd -u 12345 -g 12345 -c postfix -d /dev/null -s /bin/false postfix
 groupadd -g 54321 postdrop
 
 tar zxf postfix-2.6.5.tar.gz
 cd postfix-2.6.5
 make -f Makefile.init makefiles \
 'CCARGS=-DHAS_MYSQL -I/usr/local/mysql/include \
 -DUSE_SASL_AUTH -DDEF_SERVER_SASL_TYPE=\"dovecot\"' \
 'AUXLIBS=-L/usr/local/mysql/lib -lmysqlclient -lz -lm'
 
 make && make install
 
 然後下一步下一步默認就可以
 
 echo postfix: root >>/etc/aliases
 newaliases
 
 (4)安裝配置dovecot
 
 mkdir -p /data/mailbox
 chown -R postfix.postfix /data/mailbox
 useradd -d /dev/null -s /sbin/nologin dovecot
 tar zxf dovecot-1.2.9.tar.gz
 cd dovecot-1.2.9
 ./configure --with-sql --with-sql-drivers --with-mysql
 make && make install
 
 
 vi /usr/local/etc/dovecot.conf
 protocols=pop3
 listen=*
 disable_plaintext_auth = no
 ssl_disable = yes
 auth_debug = yes
 log_path = /var/log/pop3.log
 mail_location = maildir:/data/mailbox/%u
 pop3_uidl_format=%08Xu%08Xv
 mail_uid = 12345
 mail_gid = 12345
 first_valid_uid = 12345
 auth default {
     mechanisms = PLAIN LOGIN CRAM-MD5 DIGEST-MD5
     passdb sql {
         args = /usr/local/etc/dovecot-sql.conf
     }
     userdb sql {
         args = /usr/local/etc/dovecot-sql.conf
     }
     socket listen {
         client {
             path = /var/run/dovecot/auth-client
             mode = 0660
             user = postfix
             group = postfix
         }
     }
 }
 
 vi /usr/local/etc/dovecot-sql.conf
 
 driver = mysql
 connect = host=localhost dbname=postfix user=postfix password=PostFIX_1228
 default_pass_scheme = MD5
 password_query = SELECT password, maildir as userdb_home FROM mailbox WHERE username = '%u'
 user_query = select maildir as home,12345 as uid ,12345 as gid from mailbox where username='%u' And active='1'
 
 
 (5)配置postfix
 
 vi /etc/postfix/main.cf
 
 command_directory = /usr/sbin
 config_directory = /etc/postfix
 daemon_directory = /usr/libexec/postfix
 data_directory = /var/lib/postfix
 debug_peer_level = 2
 html_directory = no
 inet_interfaces = all
 mail_owner = postfix
 mailbox_size_limit = 209715200 #容量大小
 mailq_path = /usr/bin/mailq
 manpage_directory = /usr/local/man
 message_size_limit = 52428800 #限制附件大小
 mydomain = mail.abc.com #域名
 myhostname = mail #主機名
 mynetworks = 0.0.0.0/0 #配置這一項使用用戶可在任意地發送郵件
 mynetworks_style = subnet
 myorigin = $mydomain
 newaliases_path = /usr/bin/newaliases
 queue_directory = /var/spool/postfix
 readme_directory = no
 sample_directory = /etc/postfix
 sendmail_path = /usr/sbin/sendmail
 setgid_group = postdrop
 
 #配置使用dovecot的sql認證
 smtpd_banner = $myhostname ESMTP
 smtpd_recipient_restrictions = permit_mynetworks, reject_invalid_hostname, reject_non_fqdn_hostname, reject_unknown_sender_domain, reject_non_fqdn_sender, reject_non_fqdn_recipient, reject_unknown_recipient_domain, reject_unauth_pipelining, reject_unauth_destination
 
 #上面這個smtpd_recipient_restrictions後面的項目每個逗號后都要有一個空格,否則會有問題
 
 smtpd_sasl_auth_enable = yes
 smtpd_sasl_local_domain =
 smtpd_sasl_path = /var/run/dovecot/auth-client
 smtpd_sasl_security_options = noanonymous
 smtpd_sasl_type = dovecot
 
 unknown_local_recipient_reject_code = 550
 
 #虛擬用戶設定
 virtual_alias_domains =
 virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
 virtual_gid_maps = static:12345
 virtual_mailbox_base = /data/mailbox/
 virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf
 virtual_mailbox_limit = 209715200
 virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
 virtual_transport = virtual
 virtual_uid_maps = static:12345
 
 
 vi /etc/postfix/mysql_virtual_alias_maps.cf
 
 user = postfix
 password = PostFIX_1228
 hosts = localhost
 dbname = postfix
 table = alias
 select_field = goto
 where_field = address
 
 vi /etc/postfix/mysql_virtual_domains_maps.cf
 
 user = postfix
 password = PostFIX_1228
 hosts = localhost
 dbname = postfix
 table = domain
 select_field = description
 where_field = domain
 
 vi /etc/postfix/mysql_virtual_mailbox_maps.cf
 
 user = postfix
 password = PostFIX_1228
 hosts = localhost
 dbname = postfix
 table = mailbox
 select_field = maildir
 where_field = username
 
 (6)運行測試
 
 在測試前需要在DNS伺服器上建一個MX記錄解析到本機
 
 postfix start
 dovecot
 
 然後用netstat -putln查看25和110埠是否啟動
 
 測試認證
 
 telnet localhost 25
 
 ehlo localhost # 手工輸入返回下列信息表示postfix運行良好
 250-mail
 250-PIPELINING
 250-SIZE 52428800
 250-VRFY
 250-ETRN
 250-AUTH PLAIN LOGIN CRAM-MD5 DIGEST-MD5
 250-ENHANCEDSTATUSCODES
 250-8BITMIME
 250 DSN
 auth login                                         #手工輸入進行認證
 334 VXNlcm5hbWU6
 bHNtQG1haWwuYWJjLmNvbQ==     #手工輸入用戶名的base64編碼
 334 UGFzc3dvcmQ6
 MTIzNDU2                                        #手工輸入密碼的base64編碼
 334 UGFzc3dvcmQ6
 235 2.7.0 Authentication successful       #返回該信息表示認證成功
 mail from:lsm@mail.abc.com #發件人地址
 250 2.1.0 Ok
 rcpt to:lsm@163.com #收件人地址
 data
 sdfasdfsdfsf #輸入發信內容
 . #結束輸入發信內容
 subject:test email #發信主題
 
 將字元轉換為base64編碼的方法
 
 vi a.php
 <?php
 echo base64_encode("lsm@mail.abc.com");
 echo "

;";
 echo base64_encode("123456");
 ?>;
 
 運行/usr/local/php/bin/php a.php就可以看到轉換的base64編碼
 
 測試收信
 
 telnet localhost 110
 
 user lsm@mail.abc.com #輸入用戶名
 +OK
 pass 123456 #輸入密碼
 +OK Logged in.
 list #列出新郵件
 +OK 1 messages: #有一封新郵件
 1 1608
 
 整個過程完成了,你可以在outlook或者foxmail上配置看能不能正常收發郵件,注意用戶名是完整的,如lsm@mail.abc.com,這個郵件地址就是用戶名



[火星人 ] postfix+dovecot+postfixadmin+mysql認證已經有613次圍觀

http://coctec.com/docs/service/show-post-13395.html