2016年5月9日 星期一

在 CentOS7/RHEL7 上設定需要帳密登入的 Web站台

進階設定目標:
  • 在 Web 虚擬主機上,設置需要使用者帳密才可登入的網站!
注意事項:
  • 基本 Web 站台設定,請參考這一篇文章!
  • Web 虚擬主機設定,請參考第二篇文章!
  • https 連線加密的站台設定,請參考第三篇文章!
進階設定流程(一):
  1. 在 /etc/httpd/conf.d 目錄中,編寫之前的設定檔 vhosts.conf :
    #vim /etc/httpd/conf.d/vhosts.conf
    <VirtualHost *:80>
       ServerName test1.example.com
       DocumentRoot /var/www/vhosts/test1
       <Directory /var/www/vhosts/test1/sec>
           AuthType Basic
           AuthName "Restricted Area"
           AuthUserFile /var/www/vhosts/.htpasswd
           require valid-user
       </Directory>
    </VirtualHost>
    
  2. 新增 .htpasswd 檔案:
    #cd /var/www/vhosts/.htpasswd
    #htpasswd -c .htpasswd peter
    (記得要輸入密碼)
    
    (再增加使用者時,不用 -c 參數)
    #htpasswd .htpasswd eric
    
  3. 修改 .htpasswd 權限:
    #chmod 0411 .htpasswd
    
  4. 重新啟動 Apache :
    #systemctl restart httpd
    
參考資料:
  1. http://www.seas.upenn.edu/cets/answers/auth-htpasswd.html

在 CentOS7/RHEL7 上設定 Web 連線加密站台

進階設定目標:
  • 在同一 Web 主機上,設置加密與不加密兩類網站!
注意事項:
  • 基本 Web 站台設定,請參考這一篇文章!
  • Web 虚擬主機設定,請參考第二篇文章!
進階設定流程(一):
  1. 安裝 Apache 所需要的架密模組:
    #yum -y install mod_ssl openssl
    
  2. 產生一張自我簽署的憑證
    ## 產生私鑰
    #openssl genrsa -out ca.key 2048
    
    ## 產生 CSR
    #openssl req -new -key ca.key -out ca.csr
    
    ## 產生自我簽署的金鑰
    #openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt
    
    ## 複製檔案至正確位置
    #cp ca.crt /etc/pki/tls/certs
    #cp ca.key /etc/pki/tls/private/ca.key
    #cp ca.csr /etc/pki/tls/private/ca.csr
    
  3. 處理 SELiux 設定:
    #restorecon -RvF /etc/pki
    
  4. 設定成虚擬主機:
    #vim /etc/httpd/conf.d/ssl_vhosts.conf
    
    LoadModule ssl_module modules/mod_ssl.so
    
    #Listen 443
    <VirtualHost *:443>
        ServerName sec.example.com
        DocumentRoot /var/www/vhosts/sec
        SSLEngine on
        SSLCertificateFile "/etc/pki/tls/certs/ca.crt"
        SSLCertificateKeyFile "/etc/pki/tls/private/ca.key"
    </VirtualHost>
    
  5. 加密站台目錄與網頁的處理:
    #mkdir -p /var/www/vhosts/sec
    #cd /var/www/vhosts/sec
    #vim index.html
    Hi~~This is a secrete site !!
    
參考資料:
  1. https://wiki.centos.org/zh-tw/HowTos/Https
  2. https://httpd.apache.org/docs/2.4/ssl/ssl_howto.html
  3. http://linux.vbird.org/linux_server/0360apache.php#www_basic_pkg

2016年5月8日 星期日

在 CentOS7/RHEL7 上設定 Web 虚擬主機

進階設定目標:
  • 在同一 Web 主機上,設置不同網址網站!
  • 將 RoundCube 站台,變成 Webmail 站台
注意事項:
  • 基本 Web 站台設定,請參考這一篇文章!
進階設定流程(一):
  1. 在 /etc/httpd/conf.d 目錄中,自由新增檔案:
    #cd /etc/httpd/conf.d
    #vim vhosts.conf
    <VirtualHost _default_:80>
       ServerName www.example.com
       DocumentRoot /var/www/html
    </VirtualHost>
    <VirtualHost *:80>
       ServerName test1.example.com
       DocumentRoot /var/www/vhosts/test1
    </VirtualHost>
    
  2. 建立放置虚擬網站目錄:
    #mkdir -p /var/www/vhosts/test1
    
  3. 在虚擬網站目錄內,新增測試用網頁:
    #vim /var/www/vhosts/test1/index.html
    Hello!! Test1 Web Site !!
    
  4. 新增 DNS 記錄:
    #vim /var/named/example.zone
    (新增在最後一行)
    test1    IN  A  192.168.5.2
    
    注意事項:請參考這一篇的 DNS Server 設定!
  5. 重新啟動 DNS Server:
    #systemctl restart named
    
  6. 重新啟動 Web Server:
    #systemctl restart httpd
    
進階設定流程(二):
  1. 在 /etc/httpd/conf.d 目錄中,編輯 roundcubemail.conf 檔案:
    #vim /etc/httpd/conf.d/roundcubemail.conf
    Alias /roundcubemail /usr/share/roundcubemail
    (增加下列幾行:)
    <VirtualHost *:80>
       ServerName webmail.example.com
       DocumentRoot /usr/share/roundcubemail
    
    <Directory /usr/share/roundcubemail/bin/>
       :
       : (中間原來設定不動!)
       :
       :
    </Directory>
    (新增下列這行,為最後一行設定值)
    </VirtualHost>
    
  2. 新增 DNS 記錄:
    #vim /var/named/example.zone
    (新增在最後一行)
    webmail    IN  A  192.168.5.2
    
  3. 重新啟動 DNS Server:
    #systemctl restart named
    
  4. 重新啟動 Web Server:
    #systemctl restart httpd
    

在 CentOS7/RHEL7 上安裝設定 LDAP Server(一)

快速設定流程:
  1. 安裝 LDAP Server 套件:
    #yum install -y openldap openldap-clients openldap-servers migrationtools
    
  2. 先取得加密後的密碼:
    #slappasswd -s redhat -n > /etc/openldap/passwd
    
  3. 製作加密通訊用的金鑰組:
    #openssl req -new -x509 -nodes -out /etc/openldap/certs/cert.pem -keyout /etc/openldap/certs/priv.pem -days 3650
    Generating a 2048 bit RSA private key
    .....+++
    ..............+++
    writing new private key to '/etc/openldap/certs/priv.pem'
    -----
    You are about to be asked to enter information that will be incorporated
    into your certificate request.
    What you are about to enter is what is called a Distinguished Name or a DN.
    There are quite a few fields but you can leave some blank
    For some fields there will be a default value,
    If you enter '.', the field will be left blank.
    -----
    Country Name (2 letter code) [XX]:  ##輸入國別
    State or Province Name (full name) []: ##輸入省名
    Locality Name (eg, city) [Default City]: ##輸入城市名
    Organization Name (eg, company) [Default Company Ltd]: ##輸入組織名
    Organizational Unit Name (eg, section) []: ##輸入組織名
    Common Name (eg, your name or your server's hostname) []:instructor.example.com
    Email Address []: ##輸入 email 帳號
    
  4. 修改 /etc/openldap/certs 權限:
    # cd /etc/openldap/certs
    # chown ldap:ldap *
    # chmod 600 priv.pem
    
  5. 將 LDAP 資料庫範例檔,直接複製過來,方便架設與安裝:
    #cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
    #chown ldap /var/lib/ldap/DB_CONFIG
    
  6. 產生資料庫檔(不用管出現的錯誤):
    #slaptest
    
  7. 設定 /var/lib/ldap/ 目錄內,相關檔案權限:
    ## chown ldap:ldap /var/lib/ldap/*
    
  8. 防火牆設定(389/686 port):
    #firewall-cmd --permanent --add-service=ldap
    #firewall-cmd --permanent --add-service=ldaps
    #firewall-cmd --reload
    
  9. 啟動 slapd 服務:
    #systemctl enable slapd.service
    #systemctl start slapd.service
    
  10. 檢查啟動狀況:
    #ss -nlp | grep slapd
    
  11. 將 ldif 檔案導入系統中,例如: cosine 以及 nis 兩個 schemas:
    #cd /etc/openldap/schema
    #ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f cosine.ldif
    #ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f nis.ldif
    
  12. 編寫 changes.ldif 檔案:
    #vim /etc/openldap/changes.ldif
    dn: olcDatabase={2}hdb,cn=config
    changetype: modify
    replace: olcSuffix
    olcSuffix: dc=example,dc=com
    
    dn: olcDatabase={2}hdb,cn=config
    changetype: modify
    replace: olcRootDN
    olcRootDN: cn=Manager,dc=example,dc=com
    
    dn: olcDatabase={2}hdb,cn=config
    changetype: modify
    replace: olcRootPW
    olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx
    
    dn: cn=config
    changetype: modify
    replace: olcTLSCertificateFile
    olcTLSCertificateFile: /etc/openldap/certs/cert.pem
    
    dn: cn=config
    changetype: modify
    replace: olcTLSCertificateKeyFile
    olcTLSCertificateKeyFile: /etc/openldap/certs/priv.pem
    
    dn: cn=config
    changetype: modify
    replace: olcLogLevel
    olcLogLevel: -1
    
    dn: olcDatabase={1}monitor,cn=config
    changetype: modify
    replace: olcAccess
    olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read by dn.base="cn=Manager,dc=example,dc=com" read by * none
    
  13. 導入 changes.ldif 檔案到 LDAP Server 中:
    #ldapmodify -Y EXTERNAL -H ldapi:/// -f /etc/openldap/changes.ldif
    
  14. 可開始建立所須網域的 ldif 檔案:
    #vim /etc/openldap/base.ldif
    dn: dc=example,dc=com
    dc: example
    objectClass: top
    objectClass: domain
    
    dn: ou=People,dc=example,dc=com
    ou: People
    objectClass: top
    objectClass: organizationalUnit
    
    dn: ou=Group,dc=example,dc=com
    ou: Group
    objectClass: top
    objectClass: organizationalUnit
    
  15. 導入 base.ldif 檔案到 LDAP Server 中:
    #ldapadd -x -w redhat -D cn=Manager,dc=example,dc=com -f /etc/openldap/base.ldif
    
  16. 增加兩個系統使用者帳號來測試一下:
    # mkdir /home/guests
    # useradd -d /home/guests/ldapuser01 ldapuser01
    # passwd ldapuser01
    # useradd -d /home/guests/ldapuser02 ldapuser02
    # passwd ldapuser02
    
  17. 編寫 migrate_common.ph內容,將系統上的使用者,導入到 LDAP Server 中:
    #vim /usr/share/migrationtools/migrate_common.ph
    $DEFAULT_MAIL_DOMAIN = "example.com";
    $DEFAULT_BASE = "dc=example,dc=com";
    
  18. 在LDAP Server 中,建立目前的使用者與群組:
    # grep ":10[0-9][0-9]" /etc/passwd > passwd
    # ./migrate_passwd.pl passwd users.ldif
    # ldapadd -x -w redhat -D cn=Manager,dc=example,dc=com -f users.ldif
    # grep ":10[0-9][0-9]" /etc/group > group
    # ./migrate_group.pl group groups.ldif
    # ldapadd -x -w redhat -D cn=Manager,dc=example,dc=com -f groups.ldif
    
  19. 測試目前的使用者 ldapuser01:
    # ldapsearch -x cn=ldapuser01 -b dc=example,dc=com
    
  20. 可加入系統記錄服務:
    #vim /etc/rsyslog.conf
    local4.* /var/log/ldap.log
    
    (重新啟動系統記錄服務:)
    #systemctl restart rsyslog
    
參考資料網址:
  1. https://www.certdepot.net/rhel7-configure-ldap-directory-service-user-connection/
  2. http://www.server-world.info/en/note?os=CentOS_7&p=openldap
  3. http://jamyy.us.to/blog/2014/09/6704.html
  4. http://yu-li-liang.blogspot.tw/2014/04/linux-centos-ldap-server-24.html
  5. http://crashedbboy.blogspot.tw/2015/08/centos-7-open-ldap.html

2016年5月3日 星期二

在 CentOS7/RHEL7 上安裝設定 E-mail Server(四)

進階設定目標:
  • 大量建立 E-mail 使用者帳號,並使用 MySQL 進行管理!
進階設定流程:
  1. 追加安裝 dovecot-mysql 套件:
    #yum -y install dovecot-mysql
    
  2. 驗證 postfix 是否有與 mysql 以及 dovecot 連上:
    #postconf -m
    #postconf -a
    
  3. 建立一個真實使用者以及放置郵件目錄:
    #mkdir -p /var/www/mailbox/vmail
    #groupadd -g 5000 vmail
    #useradd -g 5000 -u 5000 -s /sbin/nologin -d /var/www/mailbox/vmail vmail
    #chown -R vmail:vmail /var/www/mailbox/
    #chmod -R 700 /var/www/mailbox/
    
  4. 修改 /etc/postfix/main.cf 基本設定:
    #vim /etc/postfix/main.cf
    #修改下列設定:
    mynetworks_style = host
    ##mynetworks = 127.0.0.0/8, 192.168.100.0/24 <== 可註解該行
    
    #追加下列設定:
    ## Vitual MailBox #####
    virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
    virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf
    virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
    virtual_uid_maps = static:5000
    virtual_gid_maps = static:5000
    virtual_mailbox_base = /var/www/mailbox/vmail
    
    
  5. 在 MySQL 中,建立資料表 domain, mailbox, alias, quota2:
    #mysql -u root -p
    MySQL(none)>create database MailBox;
    MySQL(none)>use MailBox;
    MySQL(MailBox)>CREATE TABLE domain (
                  >domain varchar(255) NOT NULL default '',
                  >description varchar(255) NOT NULL default '',
                  >aliases int(10) NOT NULL default '0',
                  >mailboxes int(10) NOT NULL default '0',
                  >maxquota int(10) NOT NULL default '0',
                  >transport varchar(255) default NULL,
                  >backupmx tinyint(1) NOT NULL default '0',
                  >created datetime NOT NULL default '0000-00-00 00:00:00',
                  >modified datetime NOT NULL default '0000-00-00 00:00:00',
                  >active tinyint(1) NOT NULL default '1',
                  >PRIMARY KEY (domain),
                  >KEY domain (domain)
                  >)ENGINE=MyISAM COMMENT='Virtual Domains';
    

                           
    MySQL(MailBox)>CREATE TABLE mailbox (
                  >username varchar(255) NOT NULL default '',
                  >password varchar(255) NOT NULL default '',
                  >name varchar(255) NOT NULL default '',
                  >maildir varchar(255) NOT NULL default '',
                  >quota int(10) NOT NULL default '0',
                  >domain varchar(255) NOT NULL default '',
                  >created datetime NOT NULL default '0000-00-00 00:00:00',
                  >modified datetime NOT NULL default '0000-00-00 00:00:00',
                  >active tinyint(1) NOT NULL default '1',
                  >PRIMARY KEY (username),
                  >KEY username (username)
                  >)ENGINE=MyISAM COMMENT='Virtual Mailboxes';
    

    MySQL(MailBox)>CREATE TABLE alias ( 
                  >address varchar(255) NOT NULL default '',
                  >goto text NOT NULL, 
                  >domain varchar(255) NOT NULL default '',
                  >created datetime NOT NULL default '0000-00-00 00:00:00',
                  >modified datetime NOT NULL default '0000-00-00 00:00:00',
                  >active tinyint(1) NOT NULL default '1',
                  >PRIMARY KEY (address),
                  >KEY address (address)
                  >)ENGINE=MyISAM COMMENT='Virtual Aliases';
    

    MySQL(MailBox)>CREATE TABLE IF NOT EXISTS `quota2` (
                  >username varchar(100) NOT NULL,
                  >bytes bigint(20) NOT NULL default '0',
                  >messages int(11) NOT NULL default '0',
                  >PRIMARY KEY  (`username`)
                  >)ENGINE=MyISAM DEFAULT CHARSET=latin1;
    
  6. 在 MySQL 中,新增使用者,並且授權:
    MySQL(MailBox)>use mysql;
                  >CREATE USER 'mailbox'@'localhost' IDENTIFIED BY 'mailbox@123';
                  >GRANT ALL PRIVILEGES ON MailBox.* TO 'mailbox'@'localhost';
    
  7. 將 postfix 連上 mysql :
    #vim /etc/postfix/mysql_virtual_alias_maps.cf
    user = mailbox
    password = mailbox@123
    hosts = localhost
    dbname = MailBox
    query = SELECT goto from alias WHERE address = '%s' AND active = '1'
    
    

    #vim /etc/postfix/mysql_virtual_domains_maps.cf
    user = mailbox
    password = mailbox@123
    hosts = localhost
    dbname = MailBox
    query = SELECT domain FROM domain WHERE domain = '%s' AND backupmx = '0' AND active = '1'
    
    

    #vim /etc/postfix/mysql_virtual_mailbox_maps.cf
    user = mailbox
    password = mailbox@123
    hosts = localhost
    dbname = MailBox
    query = SELECT maildir FROM mailbox WHERE username = '%s' AND active = '1'
    
    
  8. 設定 mailbox 大小:
    # vim /etc/postfix/main.cf
    #追加下列幾行
    virtual_create_maildirsize = yes
    virtual_maildir_extended = yes
    virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_mailbox_limit_maps.cf
    virtual_mailbox_limit_override = yes
    virtual_maildir_limit_message = "The user you are trying to reach is over quota."
    virtual_overquota_bounce = yes
    
  9. 設定相對應檔案內容/etc/postfix/mysql_virtual_mailbox_limit_maps.cf:
    user = mailbox
    password = mailbox@123
    hosts = localhost
    dbname = MailBox
    table = mailbox
    select_field = quota
    where_field = username
    additional_conditions = and active = '1'
    
    
  10. 修改 /etc/dovecot/dovecot.conf 檔案內容:
    # vim /etc/dovecot/dovecot.conf
    protocols = imap pop3 lmtp 
    
  11. 修改 /etc/dovecot/conf.d/10-auth.conf 檔案內容:
    # vim /etc/dovecot/conf.d/10-auth.conf
    disable_plaintext_auth = no
    
  12. 修改 /etc/dovecot/conf.d/10-mail.conf 檔案內容:
    # vim /etc/dovecot/conf.d/10-mail.conf
    mail_location = Maildir:/var/www/mailbox/vmail/%d/%n
    namespace {
               type = private
               separator = .
               prefix = INBOX.
               inbox = yes
               hidden = no
               }
    
  13. 設定 pop3 以及 imap 的 quota :
    #vim /etc/dovecot/conf.d/10-mail.conf
    mail_plugins = $mail_plugins quota
    
    #vim /etc/dovecot/conf.d/20-imap.conf
    mail_plugins = $mail_plugins imap_quota
    
    #vim /etc/dovecot/conf.d/20-pop3.conf
    pop3_uidl_format = %08Xu%08Xv
    mail_plugins = $mail_plugins quota
    
    #vim /etc/dovecot/conf.d/15-lda.conf
    postmaster_address = postmaster@localhost
    lda_mailbox_autocreate = yes
    lda_mailbox_autosubscribe = yes
    protocol lda {
    mail_plugins = $mail_plugins quota
    }
    
    #vim /etc/dovecot/conf.d/90-quota.conf 
    dict {
      quotadict = mysql:/etc/dovecot/dovecot-dict-quota.conf
    }
    plugin {
      quota = dict:user::proxy::quotadict
    }
    
    #vim /etc/dovecot/dovecot-dict-quota.conf
    connect = host=localhost dbname=MailBox user=mailbox password=mailbox@123
    map {
         pattern = priv/quota/storage
         table = quota2
         username_field = username
         value_field = bytes
        }
    map {
         pattern = priv/quota/messages
         table = quota2
         username_field = username
         value_field = messages
        }
    
    
  14. 追加 cram-md5 加密機制:
    #vim /etc/dovecot/conf.d/10-auth.conf
    #auth default {
                   auth_mechanisms = plain login cram-md5
    #}
    
  15. 設定 dovecot 帳密資料庫:
    # vim /etc/dovecot/conf.d/10-auth.conf
    !include auth-sql.conf.ext
    # vim /etc/dovecot/conf.d/auth-sql.conf.ext
    ###反註解
    userdb {
            driver = prefetch
    }
    
  16. 設定真實的唯一使用者 vmail:
    #vim /etc/dovecot/conf.d/10-master.conf
    ###反註解
    service auth {
        unix_listener auth-userdb {
           mode = 0600
           user = vmail
           group = vmail
      }
    }
    service dict {
        unix_listener dict {
            mode = 0600
            user = vmail
            group = vmail
        }
    }
    
    
  17. 編寫 dovecot 對 mysql 的設定檔:
    # vim /etc/dovecot/dovecot-sql.conf.ext
    driver = mysql
    connect = host=localhost dbname=MailBox  user=mailbox password=mailbox@123
    #default_pass_scheme = CRAM-MD5
    default_pass_scheme = PLAIN
    user_query = SELECT CONCAT('/var/www/mailbox/vmail/',domain,'/',name) AS home,5000 AS uid, \
    5000 AS gid, CONCAT('*:bytes=', quota) as quota_rule FROM mailbox WHERE username = '%u' AND active='1'
    password_query = SELECT username AS user, password, CONCAT('/var/www/mailbox/vmail/',domain,'/',name) \
    AS userdb_home, 5000 AS userdb_uid, 5000 AS userdb_gid,CONCAT('*:bytes=', quota) as userdb_quota_rule \
    FROM mailbox WHERE username = '%u' AND active='1'
    
  18. 設定 dovecot 連進 postfix:
    #vim /etc/postfix/main.cf
    virtual_transport = dovecot
    dovecot_destination_recipient_limit = 1
    # vim /etc/postfix/master.cf
    dovecot   unix  -       n       n       -       -   pipe flags=DRhu user=vmail:vmail \
    argv=/usr/libexec/dovecot/dovecot-lda -f ${sender} -d ${recipient}
    
  19. 重啟 dovecot 服務,測試是否有錯誤:
    #systemctl restart dovecot
    
  20. 設定 SASL 讓 postfix 可以使用 smtp-auth:
    #vim /etc/dovecot/conf.d/10-master.conf
     unix_listener /var/spool/postfix/private/auth {
        mode = 0666
        user = postfix
        group = postfix
      }
    
    
  21. 修改 /etc/postfix/main.cf 檔案內容:
    #vim /etc/postfix/main.cf
    ##追加下列項目:
    dovecot_destination_recipient_limit = 1
    smtpd_sasl_type = dovecot
    smtpd_sasl_path = private/auth
    smtpd_sasl_auth_enable = yes
    smtpd_sasl2_auth_enable = yes
    smtpd_sasl_security_options = noanonymous
    broken_sasl_auth_clients = yes
    smtpd_sasl_local_domain =
    
    ##修改下列項目,加入mysql 驗證機制:
    smtpd_recipient_restrictions = 
           permit_sasl_authenticated,
                :
                : (以下省略)
    
  22. 重新啟動 Postfix、dovecot 服務:
    #systemctl restart postfix
    #systemctl restart dovecot
    
  23. 設定 SELinux:
    # yum install setroubleshoot*
    # grep dovecot /var/log/audit/audit.log | audit2allow -M mypol
    # semodule -i mypol.pp
    
  24. 測試:
    #systemctl restart postfix
    #postmap -q test@example.com mysql:/etc/postfix/mysql_virtual_alias_maps.cf
    
  25. 檢查與驗證:
    #mail -s "first test" test@example.com
    (接著輸入下列內容:)
    Hello World
    .
    
    (以上的小黑點一定要打)
    
    #mailq
    #less /var/log/maillog
    #postmap -q test@example.com mysql:/etc/postfix/mysql_virtual_alias_maps.cf
    
  26. imap 的查驗方式:
    #telnet localhost imap
    a1 LOGIN 使用者帳號 使用者密碼
    a2 LIST "" "*"
    a3 EXAMINE INBOX
    a4 FETCH 1 BODY[]
    a5 LOGOUT
    

補充說明:
  • 解決一下 SELinux 的問題:
    #grep imap /var/log/audit/audit.log | audit2allow -M mypol
    #semodule -i mypol.pp
    #grep dovecot-lda /var/log/audit/audit.log | audit2allow -M lda
    #semodule -i lda.pp
    

參考資料:
  1. https://www.linode.com/docs/email/postfix/email-with-postfix-dovecot-and-mariadb-on-centos-7

在 CentOS7/RHEL7 上安裝設定 E-mail Server(三)

進階設定目標:
  • 過濾垃圾郵件主機或黑名單內的郵件信箱!
注意事項:

進階設定流程(一):
  1. 安裝 PostGrey 套件:
    #yum -y install postgrey
    
  2. 啟動 PostGrey 服務:
    #systemctl enable postgrey.service
    #systemctl start postgrey.service
    #netstat -anlp | grep postgrey
    
  3. 修改設定檔 /etc/postfix/main.cf:
    #vim /etc/postfix/main.cf
    
    #追加、修改下列設定;
    smtpd_recipient_restrictions =
         permit_mynetworks,
         permit_sasl_authenticated,
         reject_unknown_sender_domain,    ##拒絕來源不明的網域 (限制來源 MTA )
         reject_unknown_recipient_domain, ##拒絕不明的收件者 (限制目標 MTA)
         reject_unauth_destination,       ##拒絕不信任的目標
         check_policy_service unix:/var/spool/postfix/postgrey/socket
    
  4. 修改阻擋的秒數:
    #vim /etc/sysconfig/postgrey
    OPTIONS="--unix=/var/spool/postfix/postgrey/socket --delay=45"
    
  5. 重新啟動 postfix 服務:
    #systemctl restart postfix
    
  6. 重新啟動 PostGrey 服務:
    #systemctl restart postgrey
    
  7. 可額外設定 PostGrey 白名單:
    #vim /etc/postfix/postgrey_whitelist_clients
    加入可信任的MT郵件主機:
    station1.example.com
    
    ##設定完成後,請記得重新啟動 PostGrey 服務!
    

進階設定流程(二):
  1. 修改設定檔 /etc/postfix/main.cf,追加黑名單過濾:
    #vim /etc/postfix/main.cf
    
    #追加、修改下列設定;
    smtpd_recipient_restrictions =
         permit_mynetworks,
         permit_sasl_authenticated,
         reject_unknown_sender_domain,    
         reject_unknown_recipient_domain, 
         reject_unauth_destination,
         ##以下網址為提供黑名單查詢的網站     
         reject_rbl_client cbl.abuseat.org,
         reject_rbl_client bl.spamcop.net,
         reject_rbl_client cblless.anti-spam.org.cn,
         reject_rbl_client sbl-xbl.spamhaus.org,
         check_policy_service unix:/var/spool/postfix/postgrey/socket
    
    #過濾用戶端是黑名單內的成員:
    smtpd_client_restrictions =
         check_client_access hash:/etc/postfix/access,
         reject_rbl_client cbl.abuseat.org,
         reject_rbl_client bl.spamcop.net,
         reject_rbl_client cblless.anti-spam.org.cn,
         reject_rbl_client sbl-xbl.spamhaus.org
    
    #過濾不明的送件者網域主機
    smtpd_sender_restrictions = 
         reject_non_fqdn_sender,
         reject_unknown_sender_domain
    
  2. 重新啟動 postfix 服務:
    #systemctl restart postfix
    

進階設定流程(三):
  1. 修改設定檔 /etc/postfix/main.cf,追加針對郵件內容過濾:
    #vim /etc/postfix/main.cf
    #追加、修改下列設定:
    header_checks = regexp:/etc/postfix/header_checks
    body_checks = regexp:/etc/postfix/body_checks
    
  2. 編修/etc/postfix/header_checks、/etc/postfix/body_checks:
    #vim /etc/postfix/header_checks
    #vim /etc/postfix/body_checks
    
    檔案內的語法:
    /規則/   動作   顯示在登錄檔裡面的訊息
    
    例如:
    /^Subject:.*sex girls/   DISCARD  drop header deny
    
  3. 編修完上列兩個檔案後,可進行語言確認:
    #postmap -q - regexp:/etc/postfix/body_checks < /etc/postfix/body_checks
    

在 CentOS7/RHEL7 上安裝設定 E-mail Server(二)

進階設定目標:
  • 讓不在網段內的使用者,經由帳密驗證,亦可使用 Email Server 送信!
注意事項:請參考前一篇的設定:
進階設定流程:
  1. 安裝 Cyrus SASL 套件:
    #yum -y install cyrus-sasl-*
    
  2. 啟動 saslauthd 服務:
    #systemctl enable saslauthd.service
    #systemctl start saslauthd.service
    
  3. 修改設定檔 /etc/postfix/main.cf:
    #vim /etc/postfix/main.cf
    #追加下列設定:
    smtpd_sasl_auth_enable = yes
    smtpd_sasl_security_options = noanonymous
    broken_sasl_auth_clients = yes
    smtpd_recipient_restrictions =
         permit_mynetworks,
         permit_sasl_authenticated
    
  4. 重新啟動 postfix 服務:
    #systemctl restart postfix
    
  5. 測試 postfix 服務:
    #telnet localhost 25
    輸入下列指令:
    ehlo  localhost
    
    應該要出現下列兩行的資訊:
    250-AUTH LOGIN PLAIN
    250-AUTH=LOGIN PLAIN
    
    離開方式:
    quit