คอนฟิกส่งเมล์ SMTP ด้วย TLS ใน postfix บน Fedora 17

โดยดีฟอลต์จากการติดตั้ง postfix ผู้ใช้จากเครื่องอื่น (ที่มี ip อยู่ใน network ระบุในคอนฟิก mynetworks) จะสามารถใช้เครื่องนี้เป็นเซิร์ฟเวอร์ส่งเมลผ่านทางพอร์ต smtp (TCP 25) แบบไม่มีการเข้ารหัส

บทความนี้จะแนะนำวิธีคอนฟิก postfix เพื่อรองรับเครื่อง client ให้สามารถส่งเมล์ผ่านทางพอร์ต 587 (submission) ซึ่งเป็นพอร์ตดีฟอลต์สำหรับการคอนฟิกเมล์ใน iPhone และ Android และจะเปิดใช้การเข้ารหัส TLS เพื่อความปลอดภัยของเมล์ที่ส่ง

ใช้คำสั่ง telnet เข้าพอร์ต 25 (smtp) แล้วใช้คำสั่ง EHLO เพื่อดูคอนฟิกที่รองรับของเมล์เซิร์ฟเวอร์

$ telnet 192.168.7.17 25
Trying 192.168.7.17...
Connected to 192.168.7.17.
Escape character is '^]'.
220 fc17-64a.example.com ESMTP Postfix
EHLO example.com
250-fc17-64a.example.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
QUIT
221 2.0.0 Bye
Connection closed by foreign host.

พิมพ์คำสั่ง QUIT เพื่อออกจาก telnet เข้าพอร์ต 25

 

เปิดพอร์ต 587 (submission)

โดยดีฟอลต์จากการติดตั้ง postfix บน Fedora 17 พอร์ต 587 หรือ submission ไม่ได้ถูกเปิดไว้ หากต้องการเปิดใช้งาน เพื่อรองรับอุปกรณ์ smart phone เช่น iPhone หรือ Android ให้ส่งเมล์ได้ ต้องแก้ไขไฟล์ master.cf

[root@fc17-64a ~]# vi /etc/postfix/master.cf
...
submission inet n - n - - smtpd
# -o syslog_name=postfix/submission
# -o smtpd_tls_security_level=encrypt
# -o smtpd_sasl_auth_enable=yes
# -o smtpd_client_restrictions=permit_sasl_authenticated,reject
# -o milter_macro_daemon_name=ORIGINATING
...

ทดสอบครั้งแรกให้เอาเครื่องหมาย # ออกเฉพาะบรรทัดที่มีคำว่า submission

รีสตาร์ตเซอร์วิสหลังการแก้ไขคอนฟิก

[root@fc17-64a ~]# systemctl restart postfix.service

ลองใช้คำสั่ง netstat -a เพื่อดูพอร์ตที่เปิด จะเห็นพอร์ตใหม่ submission (TCP: 587) รันเพิ่มขึ้นมา

[root@fc17-64a ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address      State
tcp        0      0 *:submission            *:*                  LISTEN
...

ทดสอบ telnet เข้าพอร์ต 587 แล้วใช้คำสั่ง EHLO เพื่อดูความสามารถของเมล์เซิร์ฟเวอร์ จะเห็นว่าผลลัพธ์เหมือนการ telnet เข้าพอร์ต 25

$ telnet 192.168.7.17 587
Trying 192.168.7.17...
Connected to 192.168.7.17.
Escape character is '^]'.
220 fc17-64a.example.com ESMTP Postfix
EHLO example.com
250-fc17-64a.example.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
QUIT
221 2.0.0 Bye
Connection closed by foreign host.

ยังไม่รองรับ TLS

 

สร้างไฟล์ private key, certificate สำหรับ postfix

ก่อนจะคอนฟิก TLS ใน postfix ต้องสร้างไฟล์ private key, certificate สำหรับใช้ใน postfix ก่อน

cd เข้าไปในไดเร็คทอรี /etc/pki/tls/

[root@fc17-64a ~]# cd /etc/pki/tls/

รันคำสั่ง openssl genrsa เพื่อสร้างไฟล์ private key

[root@fc17-64a tls]# openssl genrsa -out postfix.key
Generating RSA private key, 512 bit long modulus
..++++++++++++
....++++++++++++
e is 65537 (0x10001)

ใช้คำสั่ง openssl req เพื่อสร้าง self-sign certificate จากไฟล์ private key ที่สร้างไว้

[root@fc17-64a tls]# openssl req -new -x509 -key postfix.key -out postfix.crt
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]:TH
State or Province Name (full name) []:Bangkok
Locality Name (eg, city) [Default City]:Laksi
Organization Name (eg, company) [Default Company Ltd]:Example Company
Organizational Unit Name (eg, section) []:IT
Common Name (eg, your name or your server's hostname) []:smtp.example.com
Email Address []:

ย้ายไฟล์ private และ certificate ที่สร้างไปไว้ในไดเร็คทอรี private/ และ certs/ ตามลำดับ

[root@fc17-64a tls]# mv postfix.key private/
[root@fc17-64a tls]# mv postfix.crt certs/

แก้ไขไฟล์ main.cf เพื่อคอนฟิกที่อยู่ของไฟล์ private key และ certificate

[root@fc17-64a ~]# vi /etc/postfix/main.cf
...
smtpd_tls_key_file = /etc/pki/tls/private/postfix.key
smtpd_tls_cert_file = /etc/pki/tls/certs/postfix.crt
...

แก้ไขไฟล์ master เพื่อเปิดการใช้ tls ด้วย

[root@fc17-64a ~]# vi /etc/postfix/master.cf
...
submission inet n    -    n    -    -    smtpd
 -o syslog_name=postfix/submission
 -o smtpd_tls_security_level=encrypt
... 

รีสตาร์ตเซอร์วิส postfix เพื่อให้คอนฟิกใหม่มีผล

[root@fc17-64a ~]# systemctl restart postfix.service

ทดสอบ telnet เข้าพอร์ต 587 อีกครั้ง

$ telnet 192.168.7.17 587
Trying 192.168.7.17...
Connected to 192.168.7.17.
Escape character is '^]'.
220 fc17-64a.example.com ESMTP Postfix
EHLO example.com
250-fc17-64a.example.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
QUIT
221 2.0.0 Bye
Connection closed by foreign host.

จะเห็นคำว่า STARTTLS ที่แสดงว่าเมล์เซิร์ฟเวอร์เครื่องนี้รองรับการส่งเมล์ SMTP โดยใช้ TLS

 

ข้อมูลอ้างอิง

 

Leave a Reply

Your email address will not be published.