Author Topic: set dkim on directadmin os=freebsd,debian exim.conf  (Read 13280 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
set dkim on directadmin os=freebsd,debian exim.conf
« on: ตุลาคม 12, 2013, 11:25:30 AM »
สำหรับ ผู้ที่ใช้งาน directadmin(da) เป็น control panel การตั้งค่า dkim ก็ไม่ยากครับ ลองดูตามนี้ นะครับ
MTA= exim , OS=FreeBSD หรือ CentOS หรือ Debian
ทำการเชคก่อนว่า exim compile "dkim" module ไว้แล้วหรือยัง

#ก่อนอื่นต้องอธิบายก่อนว่า DKIM คืออะไร ฮ่าๆ
=>DKIM ย่อมาจากคำว่า Domain Keys Identified Mail ซึ่งตัว feature นี้จะเอาไว้คล้ายๆ เซ็นต์ลายเซ็นต์ของอีเมลเรา แล้วส่งออกไป เพื่อป้องกันการปลอมแปลงอีเมลในขาส่งออก โดยทางเซิฟเวอร์ปลายทางจะทำการตรวจสอบได้ โดยอาศัยการใช้ ลายเซ็นต์สาธารณะ ที่เราแนบไว้ใน zone file ที่เป็น txt record ในการตรวจสอบเพื่อถอดรหัสลายเซ็นต์ว่าถูกต้องหรือเปล่า ถ้าลายเซ็นต์ถูกต้องก็จะทำให้อีเมลฉบับนั้นลดความเป็นสแปมเมล และสามารถผ่านไปยัง ระบบเมลของปลายทางได้ง่ายยิ่งขึ้น ครับผม

ส่วน SPF TXT คืออะไร
=> แน่นอนถ้ามีคำว่า TXT ด้วยไม่ใช่ค่ากันแดดแน่นอนครับ ฮ่าๆ SPF มาจาก (Sender Policy Framework) แต่เป็นค่าที่บ่งบอกว่า อีเมลที่ส่งนั้นจะส่งออกมาด้วย กลุ่ม IP อะไรบ้าง และเป็นค่าที่กำหนดไว้ใน zone file ของ domain นั้นๆ โดยที่จะบอก ว่า IP ที่ใช้ส่งเมลได้ต้องมีอยู่กลุ่ม IP ใน list นี้เท่านั้น ถ้าส่งไปด้วยกลุ่ม ip อื่นนอกเหนือจาก ip ที่ระบุไว้จะไม่ใช่ของโดเมนนี้ แล้ว และปลายทางสามารถ drop ไปได้เลย

ส่วน DMARC คืออะไร
=> สำหรับ DMARC นั้นย่อมาจาก Domain-based Message Authentication , Reporting and Conformance
หน้าที่หลักๆคือเป็นส่วนของรายงานผล และยืนยันว่าเมลฉบับนั้นๆ มีการเข้ารหัสการส่งเมลที่ถูกต้อง ทั้งนี้ยังป้องกันการปลอมแปลง domain เพื่อส่งอีเมลออกอีกด้วย โดยส่วนของ DMARC นั้นจะทำการ configure เพิ่มลงในระบบเมลและzonefile เพื่อไป on top ส่วนของ feature SPF และ DKIM ที่อธิบายไปก่อนหน้านี้

ซึ่งทั้งสามส่วนนั้นทางบริษัท Packetlove ก็รับ implement ให้ลูกค้าที่สนใจทั้งหมดครับ
แต่ในวันนี้จะมาพูดถึงวิธีการเตรียมระบบเพื่อให้รองรับการทำงานของ DKIM ก่อนนะครับ

[root@es5 ~]# /usr/sbin/exim -bV | grep 'Support for'
Support for: crypteq IPv6 Perl OpenSSL move_frozen_messages Content_Scanning DKIM Old_Demime PRDR OCSP
ถ้าเจอคำว่า DKIM ด้วยแล้วก็จัดการต่อเลย แต่ถ้ายังไม่มีก็ compile ใหม่ครับผม
compile ใหม่ => http://help.directadmin.com/item.php?id=125

ถ้ามี DKIM แล้วใน module ข้ามมาขั้นตอนนี้ได้เลยครับ
cd /etc
wget -O exim.dkim.conf http://files.directadmin.com/services/exim.dkim.conf

เข้าไปแก้ไขไฟล์  /etc/exim.conf, และค้นหา code ดังนี้:
remote_smtp:
 driver = smtp

เปลี่ยนให้เหมือนดังนี้
remote_smtp:
 driver = smtp
.include_if_exists /etc/exim.dkim.conf

ทำการ restart exim
#/etc/init.d/exim restart

3) ทำการเปิดค่า DKIM ใน DirectAdmin.
cd /usr/local/directadmin
cp -f conf/directadmin.conf conf/directadmin.conf.backup
echo 'dkim=1' >> conf/directadmin.conf

making absolutely sure that you're using two >> characters, else you'll empty your directadmin.conf.
And confirm it's set, and restart DA:
[root@es5 directadmin]# ./directadmin c | grep dkim
dkim=1
[root@es5 directadmin]# /etc/init.d/directadmin restart
Stopping DirectAdmin:                                      [  OK  ]
Starting DirectAdmin:                                      [  OK  ]

4) At this point, any domain created after the change should have the DKIM keys created, and dns zones updated.
For existing domains, you can either enable it individually for each domain, one-by-one:
cd /usr/local/directadmin/scripts
./dkim_create.sh domain.com

5) or you can enable it for all of your domains like this:
echo "action=rewrite&value=dkim" >> /usr/local/directadmin/data/task.queue

###### Finished ###############

#####ถ้าเป็น Directadmin version เก่ากว่า 1.5

ค้นหา สามบันทัดนี้

# These options specify the Access Control Lists (ACLs) that
# are used for incoming SMTP messages - after the RCPT and DATA
# commands, respectively.

แล้วเพิ่ม ต่อข้างล่างลงไป
acl_smtp_dkim = acl_check_dkim

ต่อไปค้นหา check_helo แล้วทำการเพิ่ม

acl_check_dkim:

        warn    add_header      = X-DKIM-Status: $dkim_verify_status [($dkim_cur_signer) - $sender_host_address]
                sender_domains  = $sender_address_domain:$dkim_signers
                dkim_signers    = $sender_address_domain:$dkim_signers
                dkim_status     = invalid
                condition       = ${if eq {${lc:$dkim_verify_status}{invalid}{true}{false}}}

        warn    add_header      = X-DKIM-Status: $dkim_verify_status [($dkim_cur_signer) - $sender_host_address]
                sender_domains  = $sender_address_domain:$dkim_signers
                dkim_signers    = $sender_address_domain:$dkim_signers
                dkim_status     = fail
                condition       = ${if eq {${lc:$dkim_verify_status}{fail}{true}{false}}}

        warn    add_header      = X-DKIM-Status: $dkim_verify_status [($dkim_cur_signer) - $sender_host_address]
                sender_domains  = $sender_address_domain:$dkim_signers
                dkim_signers    = $sender_address_domain:$dkim_signers
                dkim_status     = none
                condition       = ${if eq {${lc:$dkim_verify_status}{none}{true}{false}}}

        warn    add_header      = X-DKIM-Status: $dkim_verify_status [($dkim_cur_signer) - $sender_host_address]
                sender_domains  = $sender_address_domain:$dkim_signers
                dkim_signers    = $sender_address_domain:$dkim_signers
                dkim_status     = pass
                condition       = ${if eq {${lc:$dkim_verify_status}{pass}{true}{false}}}
        accept 

ต่อไปทำการค้นหา
driver = smtp
 
แล้วเพิ่ม .include_if_exists /etc/exim.dkim.conf
ในบันทัดใหม่

ภายใน /etc/exim.dkim.conf ใส่ configure ดังนี้

  dkim_domain = ${sender_address_domain}
  dkim_selector = x
  dkim_private_key = /etc/virtual/${lookup{$sender_address_domain}lsearch{/etc/virtual/domains}{$sender_address_domain}{ERROR}}/dkim.private.key
  dkim_canon = relaxed

เสร็จแล้วทำการ reload exim 1 รอบ
#/etc/init.d/exim restart

ทำการปรับ configure ใน da ให้รองรับ dkim ด้วย
#cd /usr/local/directadmin
#cp -f conf/directadmin.conf conf/directadmin.conf.backup
#echo 'dkim=1' >> conf/directadmin.conf

ทำการ restart directadmin daemon
[root@es5 directadmin]# ./directadmin c | grep dkim
dkim=1
[root@es5 directadmin]# /etc/init.d/directadmin restart
Stopping DirectAdmin:                                      [  OK  ]
Starting DirectAdmin:                                      [  OK  ]

ต่อไปทำการสร้าง key ของ domain ที่อยู่ในเครื่องเราครับ สามารถทำได้สองวิธีโดย
วิธี1. เข้าไปใช้ script ของ da สร้างได้เลยโดยระบุโดเมนไปครับผม วิธีนี้จะสร้าง 1 key ต่อการรันชื่อ domain 1 ครั้ง/command
#cd /usr/local/directadmin/scripts
./dkim_create.sh domain.com
รันเสร็จแล้ว รอให้ระบบอัฟเดทประมาณ 2 นาทีครับก็เข้าไปเชคผ่าน zone file domain นั้นๆได้เลย : )

วิธีที่เขียน shell script ข้ึนมาใหม่ โดยสคิปนี้จะไปอ่าน domain ทั้งหมดในระบบที่มีอยู่แล้วทำการ gen key ให้ครับ
1. gen openssl private_key , public_key ผมใช้ private_key เป็น 2048 bit นะครับตามมาตรฐานใหม่

#!/bin/bash
openssl genrsa -out /etc/virtual/$1/dkim.private.key 2048
openssl rsa -in /etc/virtual/$1/dkim.private.key -out /etc/virtual/$1/dkim.public.key -pubout -outform PEM
chown mail:mail /etc/virtual/$1/*.key
echo
echo "x._domainkey.$1.       14400   IN      TXT     \"v=DKIM1; k=rsa; p=`cat /etc/virtual/$1/dkim.public.key|grep -v "\-\-\-\-\-"|sed ':a;N;$!ba;s/\n//g'`\"">>/var/named/etc/namedb/$1.db
echo Domain $1, has been configured for DKIM signing.

เมื่อรัน shell script จะได้ดังนี้ ครับผม
add_dkim [domain]
      e.g. add_dkim packetlove.com


หลังจากนั้นถ้าคุณมีหลาย domain ก็รันอีกคำสั่งนะครับ
#ls -d */|xargs -n1|cut -d"/" -f1|xargs -n1 add_dkim $1

ทำการ restart exim
#/usr/local/etc/rc.d/exim restart

****ขั้นตอนสำคัญอีกอย่างทำการ ปรับค่า zonefile ของ domain ผมอิงจาก config ของ Bind นะครับผม

x._domainkey.thaiherchern.com.       300   IN      TXT    "v=DKIM1; k=rsa; p=MIG.....AQAB"

ตรง ..... คือค่า public_key นะครับผม
เสร็จแล้วทำการ reload record ใน zonefile ดังนี้
#rndc reload

เท่านี้ ก็เรียบร้อยครับผม

ลองเชคจากเว็บได้ นะครับ
http://dkimcore.org

ถ้ามีข้อสงสัยลองอ่านจาก RFC ดูนะครับ
http://www.opendkim.org/opendkim-README

ลองดู ตัวอย่าง Header ได้นะครับ อันนี้ผมเทสส่งจาก packetlove.com ไป gmail.com ครับผม

Received-SPF: pass (google.com: domain of ปปป@packetlove.com designates 103.246.17.224 as permitted sender) client-ip=103.246.17.224;
Authentication-Results: mx.google.com;
       spf=pass (google.com: domain of ปปปป@packetlove.com designates 103.246.17.224 as permitted sender) smtp.mail=ปปปป@packetlove.com;
       dkim=pass header.i=@packetlove.com
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=packetlove.com; s=x;
   h=Message-ID:Subject:To:From:Date:Content-Transfer-Encoding:Content-Type:MIME-Version; bh=Vjj5jVfyLn8vAO/R8F3Cd5FPihmbk4Y9GtNoCP2XRdg=;
« Last Edit: กุมภาพันธ์ 09, 2018, 08:40:42 PM by golfreeze »

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: set dkim on directadmin os=freebsd,debian exim.conf
« Reply #1 on: สิงหาคม 24, 2016, 07:50:11 PM »
สำหรับใน debian การ สร้าง dkim ของแต่ละ domain ทำตามขั้นตอนได้ง่ายคือ

#cd /usr/local/directadmin/scripts
./dkim_create.sh domain.com

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: set dkim on directadmin os=freebsd,debian exim.conf
« Reply #2 on: สิงหาคม 24, 2016, 08:59:26 PM »
เว็บที่สามารถเชค dkim record ได้คือ

https://www.mail-tester.com/spf-dkim-check
ใส่ชื่อโดเมน กับ selector ให้ถูกต้องครับผม ถ้าเจนจากสคิปใน da selector = x นะครับ

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
Re: set dkim on directadmin os=freebsd,debian exim.conf
« Reply #3 on: สิงหาคม 24, 2016, 09:46:15 PM »
ถ้าดูใน header mail ของ gmail.com ก็จะขึ้นแบบนี้ครับผม ก็แสดงว่า configure ได้ถูกต้องครับ dkim=pass

Received-SPF: pass (google.com: domain of กอฟ@pkl.com designates 103.242.1.12 as permitted sender) client-ip=103.242.1.12;
Authentication-Results: mx.google.com;
       dkim=pass header.i=@packetlove.com;
       spf=pass (google.com: domain of กอฟ@pkl.com designates 103.242.1.12 as permitted sender) smtp.mailfrom=กอฟ@pkl.com