บันทึกติดตั้ง postfix/SASL/TLS

 

Postfix เป็นแพกเกจ Mail Server ที่ได้รับความนิยมมากกว่าแพกเกจของเดเบียนเอง (Exim)
ดูจาก debian-administration: Mail Server Poll

ผมแทบไม่รู้อะไรเกี่ยวกับ Postfix SASL และ TLS เลย
แต่ขออนุญาตบันทึกไว้รอศึกษาครับ

เอามาจาก tribulaciones: Postfix/SASL/TLS HowTo for Debian Sid and Sarge

งาน Server
ติดตั้งแพกเกจ
# aptitude install postfix postfix-tls libsasl2-modules sasl2-bin openssl

ปรับตั้ง Postfix ให้ใช้ SASL
# vi /etc/postfix/main.cf

...
# SASL SUPPORT FOR CLIENTS
#
# The following options set parameters needed by Postfix to enable
# Cyrus-SASL support for authentication of mail clients.
#
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
broken_sasl_auth_clients = yes
... 
smtpd_recipient_restrictions = permit_sasl_authenticated,
 permit_mynetworks, 
 reject_unauth_destination
...

ปรับตั้ง SASL

    แก้คอนฟิกของ SASL
    # vi /etc/postfix/sasl/smtpd.conf
...
pwcheck_method: auxprop
mech_list: digest-md5 cram-md5
...

สมมุติว่าเซิร์ฟเวอร์เราชื่อ mail.example.com เราจะสร้างผู้ใช้ชื่อ test
# saslpasswd2 -c -u mail.example.com -a smtpauth test
Password: <<<--- ใส่รหัสผ่านของ test
Again (for verification): <<<--- ใส่รหัสผ่านของ test

คำสั่งนี้จะสร้างไฟล์รหัสผ่านชือ /etc/sasldb2
เราต้องเปลี่ยนเจ้าของให้มีกรุ๊ปเป็น sasl
# chgrp sasl /etc/sasldb2
(เดเบียนเปลี่ยนให้เอง)

และเปลี่ยนผู้ใช้ postfix ให้มาอยู่ในกรุ๊ป sasl
# usermod -G sasl postfix

และเริ่ม postfix ใหม่
# /etc/init.d/postfix reload

ตรวจสอบได้โดยการใช้ telnet ตัวอย่างคือ

[root@mail root]# telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.example.com ESMTP Postfix (Debian/GNU)
EHLO client.example.com
250-mail.example.com
250-PIPELINING
250-SIZE 20480000
250-ETRN
250-AUTH DIGEST-MD5 CRAM-MD5
250-AUTH=DIGEST-MD5 CRAM-MD5
250-XVERP
250 8BITMIME
QUIT
221 Bye
Connection closed by foreign host.

ปรับตั้ง TLS

    แก้ไขไฟล์ /etc/postfix/main.cf
## TLS
#
smtpd_use_tls = yes
smtpd_tls_auth_only = yes
smtpd_tls_key_file = /etc/postfix/postfix-key.pem
smtpd_tls_cert_file = /etc/postfix/postfix-cert.pem
smtpd_tls_CAfile = /etc/postfix/cacert.pem
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
tls_random_source = dev:/dev/urandom

ตั้งค่าเริ่มต้นให้ openssl
# vi /etc/ssl/openssl.cnf

...
countryName_default          = TH
stateOrProvinceName_default  = Bangkok
localityName_default         = Bangkok
0.organizationName_default   = example.com
organizationalUnitName_default  = admin
...

เอาสคริปต์ตัวอย่างมาใช้สร้างคีย์
# /usr/lib/ssl/misc/CA.sh -newca
CA certificate filename (or enter to create)
<<<--- [ENTER]
Making CA certificate ...
Generating a 1024 bit RSA private key
.......................................++++++
...++++++
writing new private key to './demoCA/private/./cakey.pem'
Enter PEM pass phrase: <<<--- ใส่รหัสผ่าน
Verifying - Enter PEM pass phrase: <<<--- ใส่รหัสผ่านอีกครั้ง
-----
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) [TH]: <<<--- [ENTER]
State or Province Name (full name) [Bangkok]: <<<--- [ENTER]
Locality Name (eg, city) [Bangkok]: <<<--- [ENTER]
Organization Name (eg, company) [example.com]: <<<--- ใส่ชื่อร้านหรือหน่วยงาน
Organizational Unit Name (eg, section) [admin]: <<<--- ชื่อย่อหน่วยงานย่อย
Common Name (eg, YOUR name) []: <<<--- ชื่อหน่วยงานปกติ
Email Address []: <<<--- อีเมลเรา
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: <<<--- [ENTER]
An optional company name []: <<<--- [ENTER]
Using configuration from /usr/lib/ssl/openssl.cnf
Enter pass phrase for ./demoCA/private/./cakey.pem: <<<--- รหัสผ่านให้เหมือน PEM
...
Write out database with 1 new entries
Data Base Updated

ไฟล์กุญแจ CA certificate จะอยู่ที่ /etc/ssl/demoCA/private/cakey.pem

เอาไฟล์ตัวอย่าง CA.sh มาปรับแก้
# cd /etc/ssl
# cp /usr/lib/ssl/misc/CA.sh CA-nodes.sh

เราจะแพตช์ให้ใช้กับของเรา ด้วยการสร้างไฟล์ชื่อ CA.patch
# vi CA.patch

--- /usr/lib/ssl/misc/CA.sh     2006-11-16 17:57:32.000000000 +0700
+++ /etc/ssl/CA-nodes.sh        2006-11-14:16:28:39.000000000 +0700
@@ -53,13 +53,13 @@ case $i in
     ;;
 -newcert)
     # create a certificate
-    $REQ -new -x509 -keyout newkey.pem -out newcert.pem $DAYS
+    $REQ -new -nodes -x509 -keyout newkey.pem -out newcert.pem $DAYS
     RET=$?
     echo "Certificate is in newcert.pem, private key is in newkey.pem"
     ;;
 -newreq)
     # create a certificate request
-    $REQ -new -keyout newkey.pem -out newreq.pem $DAYS
+    $REQ -new -nodes -keyout newkey.pem -out newreq.pem $DAYS
     RET=$?
     echo "Request is in newreq.pem, private key is in newkey.pem"
     ;;

สั่งแพตช์
# patch -p3 < CA.patch

สร้างกุญแจจากไฟล์ที่แพตช์แล้ว
# ./CA-nodes.sh -newreq
Generating a 1024 bit RSA private key
........................++++++
...........++++++
writing new private key to 'newkey.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) [TH]: <<<--- [ENTER]
State or Province Name (full name) [Bangkok]: <<<--- [ENTER]
Locality Name (eg, city) [Bangkok]: <<<--- [ENTER]
Organization Name (eg, company) [example.com]: <<<--- ใส่ชื่อร้านหรือหน่วยงาน
Organizational Unit Name (eg, section) [admin]: <<<--- ชื่อย่อหน่วยงานย่อย
Common Name (eg, YOUR name) []: <<<--- ชื่อหน่วยงานปกติ
Email Address []: <<<--- อีเมลเรา
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: <<<--- [ENTER]
An optional company name []: <<<--- [ENTER]
Request is in newreq.pem, private key is in newkey.pem

สั่งใช้กุญแจที่สร้าง
# ./CA-nodes.sh -sign

คัดลอกไฟล์กุญแจที่สร้างไว้ไปที่ /etc/postfix
# cp demoCA/cacert.pem /etc/postfix
# cp newreq.pem /etc/postfix/postfix-key.pem
# cp newcert.pem /etc/postfix/postfix-cert.pem
# cd /etc/postfix
# chown root:root postfix-key.pem ; chmod 400 postfix-key.pem

เริ่ม postfix ใหม่อีกครั้ง
# /etc/init.d/postfix reload
ต้องไม่มีรายงานข้อผิดพลาด

งาน Client
ติดตั้งแพกเกจ
# aptitude install postfix postfix-tls libsasl2-modules openssl

ปรับตั้ง Postfix ให้ใช้ SASL
# vi /etc/postfix/main.cf

...
# SASL for client side
#
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options =
...

ปรับแก้ให้ไฟล์รหัสผ่าน อ่านได้จาก root อย่างเดียว
# chown root:root /etc/postfix/sasl_passwd && chmod 600 /etc/postfix/sasl_passwd

สร้างไฟล์ข้อมูล sasl_passwd DB ด้วยคำสั่ง postmap
# postmap hash:/etc/postfix/sasl_passwd

ปรับตั้ง TLS
แก้ /etc/postfix/main.cf
# vi /etc/postfix/main.cf

...
# Use TLS client side
smtp_use_tls = yes
...
# If we want also the client to issue a certificate, these lines are needed:
smtp_tls_key_file = /etc/postfix/postfix-key.pem
smtp_tls_cert_file = /etc/postfix/postfix-cert.pem
smtp_tls_CAfile = /etc/postfix/cacert.pem
...

เสร็จแล้วด้วยความมึนงง :)

 

Syndicate

Subscribe to Syndicate

Who's online

There are currently 0 users online.