2017年1月17日 星期二

Domain Name Server 設定(BIND DNS)

有時為了減少硬體設備或其他原因,將 DNS Service 內、外 ZONE 都放在同一台 DNS 主機上。
這個時候可利用 VIEW 的功能,讓內網和外網網域在同一台 DNS 伺服器上提供解析網域。

yum install bind bind-chroot bind-utils

named.conf 內容:
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { none; };
version "None"; //當別人查詢您的BIND版本時顯示的內容
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { any; };
allow-recursion { localhost; 192.168.1.0/24; };
forwarders { 8.8.4.4; 8.8.8.8; 168.95.192.1; 168.95.1.1; };
notify yes;

dnssec-enable yes;
dnssec-validation yes;
dnssec-lookaside auto;
bindkeys-file "/etc/named.iscdlv.key";

managed-keys-directory "/var/named/dynamic";

pid-file "/run/named/named.pid";
session-keyfile "/run/named/session.key";

};

logging {
category lame-servers { null; };
channel security_file {
file "/var/log/named/security.log" versions 3 size 30m;
severity dynamic;
print-time yes;
};
category security {
security_file;
};
};


acl "lan" { 192.168.1.0/24; localhost; };

view "internal" {
match-clients { lan; };
zone "." {
type hint;
file "/etc/named.ca";
};

include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";

zone "example.com" {
type master;
file "/var/named/internal/zone_example.com";
allow-transfer { none; };
};

zone "1.168.192.in-addr.arpa" {
type master;
file "/var/named/internal/192.168.1.rev";
allow-transfer { none; };
};
};

view "external" {
match-clients { any; };

zone "." {
type hint;
file "/etc/named.ca";
};

include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";

zone "example.com" {
type master;
file "/var/named/zone_example.com";
allow-transfer { none; };
};
};

依內、外網域 IP 修改:
zone_example.com 內容:
$TTL 38400
@ IN SOA dns.example.com. root.example.com. (
2017010101
600
3600
604800
86400 )
@ IN NS dns.example.com.
@ IN MX 5 mail.example.com.
dns.example.com. IN A 192.168.1.1
example.com.     IN    TXT     "v=spf1 a mx include:mail.example.com ~all"
mail.example.com. IN A 192.168.1.2
www.example.com. IN A 192.168.1.3

192.168.1.rev 內容:
$TTL 38400
@ IN SOA dns.example.com. root.example.com. (
2017010101
1800
3600
604800
38400 )
@ IN NS dns.example.com.
3 IN PTR www.example.com.

修改後重啟 dns service:
systemctl restart named-chroot.service
systemctl enable named-chroot.service



.htaccess 網頁認證的方式

1.建立 .htaccess
在要限制的網頁目錄裡(例: /var/www/html/xxxxx),建立一個 .htaccess 的檔案
.htaccess 內容:
AuthName "User Account Login"    #網頁上顯示的文字
AuthUserFile /var/www/.htpasswd    #.htaccess 密碼檔存放位置,儘量不要放在 www 目錄下
AuthType Basic
require valid-user

2. 產生帳號密碼檔
利用 htpasswd 指令來產生帳號密碼檔

1.新建一個登入帳號 為 test,
如 /var/www/.htpasswd 帳號密碼檔還不存在,則需要輸入下列指令:

       htpasswd -c /var/www/.htpasswd test

(需特別注意的是,如果 /var/www/.htpasswd 已經存在的話,不要再使用 -c 參數,因為它會覆蓋掉原本/var/www/.htpasswd 的內容)

2.再新建一個登入帳號 為 admin,而這個時候 /var/www/.htpasswd 已經存在,則只需要輸入下列指令,

       htpasswd  /var/www/.htpasswd admin

輸入此使用者的密碼,輸入完畢後,在 /var/www/.htpasswd 就會看到剛剛新建的使用者帳號及密碼的資料了。


CentOS 6 Apache 設定方式 .htaccess:
apache 預設應開是開啟此功能的,在 http.conf 裡
確認 .htaccess 是可生效的
AccessFileName .htaccess
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy All
</Files>
新增保護的網頁:
<Directory "/var/www/html/xxxxx">
    Options FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>


CentOS 7 Apache 設定方式 .htaccess:
新增保護的網頁 /etc/httpd/conf.d/xxxx.conf:
<Directory "/var/www/html/xxxxx">
    AllowOverride All
    Require all granted
</Directory>





2017年1月4日 星期三

Ubuntu 16.04 安裝 iRedMail (Postfix) 整合 Samba DC

資料來源:
iRedMail 整合 Windows Active Directory 官網設定:
熱心的 Alan 把 iRedMail 英文官網翻譯成中文網頁:

會寫這篇是因為官網上的設定是針對 Windows Active Directory 的設定。
但我是使用 Samba 來當 DC 伺服器(沒有 Windows 系統),所以寫這篇當作是自己備忘錄用。
roundcubemail 的 config 檔案也有些微的調整,因為我的使用者是歸屬到 OU 裡,所以不調整會抓不到使用者名單及群組。
再強調一次,所有的設定都是延用官網提供的資料,只有些微的調整以符合我自己的網域環境。

先更新系統:
apt-get update && apt-get upgrade && apt-get dist-upgrade

下載安裝 iRedMail 套件,它會線上下載安裝所有所需的套件:

解壓縮 iRedMail :
tar -xvf iRedMail-0.9.5-1.tar.bz2
cd iRedMail-0.9.5-1
bash iRedMail.sh

iRedMail 固定的設定畫面,依它的設定步驟提供各項資料設定(增加版面的截圖):

郵件的儲存路徑:

要採用 Nginx 或 apache 作為 WEB Server:

這裡我選擇 OpenLDAP 作為使用者帳號管理(測試整合 Samba DC 帳號,後續再修改 LDAP 設定):
iRedMail 是一個支援多網域的郵件整合套件,這裡先輸入一個網域,但要整合 DC 伺服器的話,多網域功能將無法使用(這裡不允許輸入與本機相同的網域名稱):

輸入管理者密碼:

安裝額外的套件(WEB 管理介面、Web Mail、Fail2ban) :

最後,確認一下所有的設定項目,沒想修改的就按下 y 完成 iRedMail 的安裝:

按下 y 後開始下載及安裝各項套件,會跑一下子。安裝過程中還會再詢問是否要在防火牆設定開啟 SSH Port
我是依照提示字元 [Y|n] 哪個是大寫就選哪個,亂選!!!

完成後的訊息畫面:

重新啟動伺服器,啟動所有相關的服務功能:
reboot

登入 iRedMail 管理網頁:

先建一個使用者帳號給 LDAP search 清單時使用:
可利用微軟提供的 RSAT 管理工具連線 Samba DC 建立使用者帳號及管理;
遠端伺服器管理工具(RSAT) 連線管理 DC 伺服器:
下載連結位址: https://www.microsoft.com/zh-TW/download/details.aspx?id=45520
建一個【vmail】使用者帳號
想要練一下 Samba DC 指令建帳號也可以:
samba-tool user add -h    #查詢用法
samba-tool user list     #顯示目前使用者帳號
samba-tool user add username --given-name=小明 --surname= --mail-address=username@tw.example

測試 LDAP 是否可以搜尋到資料:
DNS IP 是指向 Samba Domain 的 DNS 主機才可用 -h DomainName 做測試
ldapsearch -x -h tw.example -D 'tw\vmail' -W -b 'cn=users,dc=tw,dc=example'

如果出現下列訊息:
則必須在 smb.conf 加入:
ldap server require strong auth = No    #關閉使用複雜的身份驗證

自已備忘用:
測試時用的是 dynamic dns 方式架設 iRedMail,所以如果出現下列訊息,
這時還沒修改 LDAP 設定整合 Samba DC:
do not list BOTH mydestination and virtual_mailbox_domains
修改 /etc/postfix/main.cf 下列三個參數:
不需修改 mydestination、virtual_mailbox_domains 這兩個參數


修改 Postfix 各項設定,整合 Samba DC :
將 LDAP 帳號驗證轉移到 Samba DC 後,iRedMail 本機的網域就無法登入使用了。

vi /etc/postfix/main.cf
修改下列參數值:
virtual_alias_maps =
    proxy:ldap:/etc/postfix/ldap/virtual_alias_maps.cf
    proxy:ldap:/etc/postfix/ldap/virtual_group_maps.cf
    proxy:ldap:/etc/postfix/ldap/virtual_group_members_maps.cf
    proxy:ldap:/etc/postfix/ldap/catchall_maps.cf
change to:
virtual_alias_maps =          #設為空值

sender_bcc_maps =
    proxy:ldap:/etc/postfix/ldap/sender_bcc_maps_user.cf
    proxy:ldap:/etc/postfix/ldap/sender_bcc_maps_domain.cf
change to:
sender_bcc_maps=         #設為空值

recipient_bcc_maps =
    proxy:ldap:/etc/postfix/ldap/recipient_bcc_maps_user.cf
    proxy:ldap:/etc/postfix/ldap/recipient_bcc_maps_domain.cf
change to:
recipient_bcc_maps=         #設為空值

relay_domains =
    $mydestination
    proxy:ldap:/etc/postfix/ldap/relay_domains.cf
change to:
relay_domains=         #設為空值

relay_recipient_maps=         #設為空值

virtual_mailbox_domains =
   proxy:ldap:/etc/postfix/ldap/virtual_mailbox_domains.cf
change to:
virtual_mailbox_domains = tw.example    #E-Mail Domain Name

smtpd_sasl_local_domain = tw.example    #E-Mail Domain Name

修改 transport 設定:
transport_maps =
    proxy:ldap:/etc/postfix/ldap/transport_maps_user.cf
    proxy:ldap:/etc/postfix/ldap/transport_maps_domain.cf
change to:
transport_maps = hash:/etc/postfix/transport

SMTP 寄件者設定:
smtpd_sender_login_maps =
   proxy:ldap:/etc/postfix/ldap/sender_login_maps.cf
change to:
smtpd_sender_login_maps =
   proxy:ldap:/etc/postfix/ldap/ad_sender_login_maps.cf

mail 使用者:
virtual_mailbox_maps =
   proxy:ldap:/etc/postfix/ldap/virtual_mailbox_maps.cf
change to:
virtual_mailbox_maps =
   proxy:ldap:/etc/postfix/ldap/ad_virtual_mailbox_maps.cf

mail 群組:
virtual_alias_maps =
    proxy:ldap:/etc/postfix/ldap/virtual_group_maps.cf
change to:
virtual_alias_maps =
   proxy:ldap:/etc/postfix/ldap/ad_virtual_group_maps.cf

建立 transport 檔案:
vi /etc/postfix/transport
內容:
tw.example dovecot

hash transport 檔案產生 transport.db:
postmap hash:/etc/postfix/transport

網友 squawell huang 提供:
在 DC 網域與使用者 E-Mail 網域不同的情況下,必須修改下列四個檔案:
1.ad_sender_login_maps.cf
2.ad_virtual_mailbox_maps.cf
3.ad_virtual_group_maps.cf
4.dovecot-ldap.conf
將【userPrincipalName】修改為【mail】
我自己的網域是 tw.example 但我的 E-Mail 是用 hansen@example.ddns.net ,

所以我登入 webmail 的帳號要輸入 hansen@example.ddns.net ,我測試從 Gmail 寄給 hansen@example.ddns.net 是可以收到信件的。

建立 ad_sender_login_maps.cf :
vi /etc/postfix/ldap/ad_sender_login_maps.cf
內容:
server_host     = tw.example    #DNS IP 指向 AD 主機時可這樣設定,指定 IP 也可以
server_port     = 389
version         = 3
bind            = yes
start_tls       = no
bind_dn         = tw\vmail
bind_pw         = vmail_password
search_base     = cn=users,dc=tw,dc=example
scope           = sub
#query_filter    = (&(userPrincipalName=%s)(objectClass=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))
#result_attribute= userPrincipalName
query_filter    = (&(mail=%s)(objectClass=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))
result_attribute= mail
debuglevel      = 0

建立 ad_virtual_mailbox_maps.cf :
vi /etc/postfix/ldap/ad_virtual_mailbox_maps.cf
內容:
server_host     = tw.example    #DNS IP 指向 AD 主機時可這樣設定,指定 IP 也可以
server_port     = 389
version         = 3
bind            = yes
start_tls       = no
bind_dn         = tw\vmail
bind_pw         = vmail_password
search_base     = cn=users,dc=tw,dc=example
scope           = sub
#query_filter    = (&(objectclass=person)(userPrincipalName=%s))
#result_attribute= userPrincipalName
query_filter    = (&(objectclass=person)(mail=%s))
result_attribute= mail
result_format   = %d/%u/Maildir/
debuglevel      = 0

建立 ad_virtual_group_maps.cf :
vi /etc/postfix/ldap/ad_virtual_group_maps.cf
內容:
server_host     = tw.example    #DNS IP 指向 AD 主機時可這樣設定,指定 IP 也可以
server_port     = 389
version         = 3
bind            = yes
start_tls       = no
bind_dn         = tw\vmail
bind_pw         = vmail_password
search_base     = cn=users,dc=tw,dc=example
scope           = sub
query_filter    = (&(objectClass=group)(mail=%s))
special_result_attribute = member
leaf_result_attribute = mail
#result_attribute= userPrincipalName
result_attribute=mail
debuglevel      = 0

Alan 翻譯的注意事項: 
1.AD 的使用者資訊在 "mail " 及 "userPrincipalName"中皆有設定, 將取回重複的兩筆 郵件地址, 註解掉 'leaf_result_attribute' 即可
2.若群組帳號未傳回正確的 'mail' 及 'userPrincipalName' 資料,修改query_filter 的設定: query_filter = (&(objectClass=group)(sAMAccountName=%u))

移除 Postfix 中 iRedAPD 相關設定:
vi /etc/postfix/main.cf
移除下列這段文字:
check_policy_service inet:127.0.0.1:7777  (有兩處要刪除: smtpd_recipient_restrictions、smtpd_end_of_data_restrictions)

測試是否可提取 AD 資料,有回覆訊息表示正常運作:
postmap -q vmail@tw.example ldap:/etc/postfix/ldap/ad_virtual_mailbox_maps.cf
應回覆訊息:
tw.example/vmail/Maildir/

postmap -q vmail@tw.example ldap:/etc/postfix/ldap/ad_sender_login_maps.cf
應回覆訊息:
vmail@tw.example

postmap -q hr@tw.example ldap:/etc/postfix/ldap/ad_virtual_group_maps.cf
應回覆 HR 群組所有成員 E-Mail 清單。


AD 整合 Dovecot 功能(dovecot 的 LDAP 查詢轉向至 Samba DC 伺服器):
建立 dovecot-ldap.conf :
vi /etc/dovecot/dovecot-ldap.conf
內容:
hosts           = tw.example:389    #DNS IP 指向 AD 主機時可這樣設定,不然指定 IP 也可以
ldap_version    = 3
auth_bind       = yes
dn              = tw\vmail
dnpass          = vmail_password
base            = cn=users,dc=tw,dc=example
scope           = subtree
deref           = never
#user_filter     = (&(userPrincipalName=%u)(objectClass=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))
#pass_filter     = (&(userPrincipalName=%u)(objectClass=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))
user_filter     = (&(mail=%u)(objectClass=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))
pass_filter     = (&(mail=%u)(objectClass=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))
pass_attrs      = userPassword=password
default_pass_scheme = CRYPT
user_attrs      = =home=/var/vmail/vmail1/%Ld/%Ln/Maildir/,=mail=maildir:/var/vmail/vmail1/%Ld/%Ln/Maildir/

設定 user quota:
vi /etc/dovecot/dovecot.conf
找到修改:
quota_rule = *:storage=1G

重啟 Dovecot Service :
systemctl restart dovecot.service

測試 Dovecot 是否正常運作:
telnet localhost 143
出現 * OK [...] Dovecot ready.
表示正常可登入
測試指令:
. login vmail@tw.example user_password


設定 roundcubemail 可讀取 AD 使用者帳號:
iRedMail 將 roundcubemail 安裝在:
/opt/www/roundcubemail

修改 roundcubemail config.inc.php 設定檔:
vi /opt/www/roundcubemail/config/config.inc.php

修改的內容:
# Global LDAP Address Book with AD.
#
$config['ldap_public']["global_ldap_abook"] = array(
    'name'          => 'AD 通訊錄',
    'hosts'         => array("tw.example"),      //設定 Samba DC 主機名稱或者主機 IP
    'port'          => 389,
    'use_tls'       => false,   //設定 LDAP 使用 TLS
    'ldap_version'  => '3',
    'network_timeout' => 10,
    'user_specific' => false,

    'base_dn'       => "dc=tw,dc=example",    
     // <- 設定 base dn,因為 Samba DC 人員採用 OU 方式, cn=users,dc=tw,dc=example 無法提取清單,所以改成 dc=tw,dc=example

    'bind_dn'       => "tw\\vmail",             // <- 搜尋網域清單時 bind_dn 要加 "\\"
    'bind_pass'     => "vmail_password",
    'writable'      => false,               // <- 不允許使用者回寫資料到 Samba DC

    'search_fields' => array('mail', 'cn', 'sAMAccountName', 'displayname', 'sn', 'givenName'),

    // mapping of contact fields to directory attributes
    'fieldmap' => array(
        'name'        => 'cn',
        //'surname'     => 'sn',
        //'firstname'   => 'givenName',
        'title'       => 'title',
        'email'       => 'mail:*',
        'phone:work'  => 'telephoneNumber',
        'phone:mobile' => 'mobile',
        'phone:workfax' => 'facsimileTelephoneNumber',
        'street'      => 'street',
        'zipcode'     => 'postalCode',
        'locality'    => 'l',
        'department'  => 'departmentNumber',
        'notes'       => 'description',
        'photo'       => 'jpegPhoto',
    ),
    'sort'          => 'cn',
    'scope'         => 'sub',

//因為人員採用 OU 方式,會跑出一些不該出現的帳號,所以在 filter 時過濾掉不該出現的帳號
    //'filter'        => "(&(objectclass=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))",
    'filter'        => '(&(objectClass=Person)(!(objectClass=computer))(!(cn=dns*))(!(cn=Admin*))(!(cn=Guest*))(!(cn=krbtgt)))',    //應該有更簡單的過濾方式,目前先這樣用
    'fuzzy_search'  => true,
    'vlv'           => false,
    'sizelimit'     => '0',
    'timelimit'     => '0',
    'referrals'     => false,

'group_filters' => array(
        'departments' => array(
            'name'    => 'AD 群組',
            'scope'   => 'sub',
            'base_dn' => 'dc=tw,dc=example',
            //'filter'  => '(&(|(objectclass=mailList)(objectClass=mailAlias))(accountStatus=active)(enabledService=displayedInGlobalAddressBook))',
            'filter'  => '(&(|(cn=HRGroup)(cn=RDGroup)))',    //不知道怎樣才能過濾掉一大堆 DC 伺服器的群組,乾脆直接取出固定的群組清單,反正修改也是一下子的時間而已
            'name_attr' => 'cn',
            'email'     => 'mail',
        ),
    ),
);

roundcube 通訊錄:


搞定,後續觀察是否有其他問題。

補充:
syslog 出現訊息:
vmail roundcube: <clji923d> PHP Error: LDAP: ldap_search failed for dn=.........................

使用者在郵件地址欄位每輸入一個字符,Roundcube ldap 便立即查詢,但無法找到相符的資料,所以會出現此訊息。這個訊息是可以忽略。



ManageEngine EventLog Analyzer版本更新步驟

更新檔下載位址: Upgrade to Latest Version of EventLog Analyzer Build (manageengine.com) 先將下載的更新檔上傳到主機裡,再進行下列動作。 切換到 ManageEngine EventLog Analyzer ...