ch08 mail-systems
TRANSCRIPT
Mail Systems
Picture from wikipedia
http://upload.wikimedia.org/wikipedia/commons/thumb/7/72/Email.svg/400px-Email.svg.png
Mail Systems
● การรับ/ส่ง e-mail กระบวนคอ่นข้างซบัซ้อน– ถ้าสนใจลองอ่านจาก wikipedia: “Email”
● เราจะสนใจเฉพาะ ส่วนทีส่ำาคญั– Server สำาหรับการ รับ/ส่ง mail
(Computer ↔ Computer)
– Server สำาหรับติดต่อผ้้ใช้ (Computer ↔ User)
Mail Servers
● SMTP Server
– รับ mail message จากผ้ใ้ช้– รับ/ส่ง mail message จาก SMTP Server อ่ืน
● IMAP/POP3 Server
– รับ mail จาก mailbox/storage ให ้user
SMTP Server
● Simple Mail Transfer Protocol Server
(Simple!)
● มีหลายตัวเลอืก– postfix
– exim
– sendmail
– …
● Debian ใช้ default smtp server เป็น exim4
IMAP/POP3 Server
● Internet Message Access ProtocolPost Office Protocol (version 3)
● สำาหรับ mail reader เข้าถงึ mailbox ของผ้้ใช้● มีหลายตัวเลือกเช่นกัน
– uw-imap (Woshington Univ)
– cyrus-imapd (Cyrus Mail Systems)
– dovecot
● ใช้ Dovecot เพราะรองรับ Maildir format
Exim4
● ติดตั้งเป็น default smtp อย่้แล้ว– แต่ config ใหรั้บเฉพาะ local อย่างเดียว
● ทดสอบ$ echo test | mail s test `whoami`$ mail
หรือ$ mutt
Exim4 (cont)
● ด้หมายเลข IP ปัจจุบัน$ /sbin/ifconfig | grep 'inet ' | grep v 127.0.0.1
● กำาหนดค่าให้กับตัวแปรของ shell ช่ือ IP$ IP=`/sbin/ifconfig | grep 'inet ' | head 1 | cut f2 d: | cut f1 d' '`
● ในบรรทัดท่ีแล้ว คำาสั่งอย่้ในบรรทัดเดียว● ใช้ backtick (คีย์ ` ใต้ ESC ใต้อักษร '~')สำาหรบัคร่อมคำาสั่งท้ังหมด
Exim4 (cont)
● ทดสอบ$ nc $IP 25$ nc 127.0.0.1 25
● ใช้ $IP ควรจะติดต่อไม่ได้ เพราะยังไม่ได้ กำาหนด config ของ exim ให้เป็นแบบ internet
● ใช้ 127.0.0.1 จะใช้งานได้ เพราะ config เป็น local● ใช้ CTRL-C สำาหรับ exit จาก nc
Exim4 config
● ไฟล์ simplify config
/etc/exim4/update-exim4.conf.conf
ใช ้editor แก้ไข หรือ ใชค้ำาส่ัง$ sudo dpkgreconfigure exim4config
● config ท่ีต้องแก้dc_eximconfig_configtype='local'dc_local_interfaces='127.0.0.1 ; ::1'
● และ ควรจจะแก้dc_use_split_config='false'dc_localdelivery='mail_spool'
Exim4 config (cont)
● config ท่ีต้องแก้dc_eximconfig_configtype='internet'dc_local_interfaces=''dc_use_split_config='true'dc_localdelivery='maildir_home'
● config อื่นdc_other_hostnames='$YOURDOMAIN'dc_other_hostnames='cheshirecat2012.net'
Exim4 config ** เพิ่ม **(cont)
● สำาหรับ Exim4 Server ซึ่งมี ip address อย่ใ้น private address (10.0.0.0/8, 192.168.0.0/16,
172.16.0.0/12) จะต้องแก้ไข router config ในไฟล์/etc/exim4/conf.d/router/200_exim4config_primary
● ให ้search หา dnslookup ซึ่งจะมีลักษณะดังนี้
แก้ไข ignore_target_hosts จากdnslookup: debug_print = "R: dnslookup for $local_part@$domain" driver = dnslookup domains = ! +local_domains transport = remote_smtp same_domain_copy_routing = yes # ignore private rfc1918 and APIPA addresses ignore_target_hosts = 0.0.0.0 : 127.0.0.0/8 : 192.168.0.0/16 :\ 172.16.0.0/12 : 10.0.0.0/8 : 169.254.0.0/16 :\ 255.255.255.255 no_more
.endif
แก้ ignore_target_hosts เป็นdnslookup: debug_print = "R: dnslookup for $local_part@$domain" driver = dnslookup domains = ! +local_domains transport = remote_smtp same_domain_copy_routing = yes # ignore private rfc1918 and APIPA addresses ignore_target_hosts = 0.0.0.0 : 127.0.0.0/8 : 255.255.255.255 no_more
.endif
ทดสอบ new config
● restart exim4$ sudo service exim4 restart
● IP = หมายเลข IP ปัจจุบนั$ nc $IP 25$ nc 127.0.0.1 25
● ควรจะใช้งานได้ท้ังสองคำาสั่ง เพราะแก้ไข config ของ exim4 แลว้
● ตอ่ไปเป็นการทดสอบการสง่ mail
ทดสอบส่ง mail
$ nc 172.30.3.25 25220 lsa.coe.psu.ac.th ESMTP Exim 4.72 Wed, 12 Dec 2012 15:41:26 +0700mail from: [email protected] OKrcpt to: [email protected] Accepteddata354 Enter message, ending with "." on a line by itselfSubject: test
test.250 OK id=1TihtT00013zAQquit221 lsa.coe.psu.ac.th closing connection
ระบุชื่อ 'other_hostnames' ในส่วนของ email domain (cheshirecat2012.net)
ทดสอบ (ต่อ)
● ตัวอย่างใน slide ท่ีแลว้– ตัวอักษรสีดำาเป็น response ของคอมพิวเตอร์ / exim4
– ตัวอักษรสีนำ้าเงิน/แดง เป็นสิง่ท่ีเราจะต้องพิมพ์เข้าไป (ให้เปลี่ยน domain เป็นของตนเอง)
● email ผ้้รับ root@... อาจจะใช้ไม่ได้ ถ้าการ config user ยังไม่ถ้กต้อง ให้สร้าง user ธรรมดาข้ึนมารับ mail กอ่น
● การส่งให้ root ระบบจะ forward ต่อให้ user ธรรมดา (ท่ีทำาหน้าท่ี administrator) โดยการกำาหนดใน /etc/aliases
ทดสอบ (ต่อ)
● ใช้คำาส่ัง mutt สำาหรบัการอ่าน mail ทีส่่งมา แล้วเกบ็อย่ใ้นระบบแบบ Maildir format
– bsd mailx อ่าน Maildir format ไม่ได้$ mutt f $HOME/Maildir
● ด้ directory และ ไฟล์ใน $HOME/Maildir
$ ls -l Maildir/{cur,new,tmp}
ทดสอบ SMTP Operation
● การรับ/ส่ง email ระหว่าง SMTP Server กบั SMTP Server
● ต้องการ Name Server Config ที่ถก้ต้อง● SMTP Server จะส่ง mail ไปยัง [email protected]
จะต้องร้้ IP Address ของ host.domain.com
● SMTP Server จะส่ง mail ไปยัง [email protected]
จะต้องร้้ IP Address ของ Mail-Exchanger (MX) ของ domain.com
SMTP Operation (cont)
● IP Address ของ smtp.cheshirecat2012.net$ host t a smtp.cheshirecat2012.net
● IP Address ของ MX ของ cheshirecat2012.net$ host t mx cheshirecat2012.net$ host t a mx.cheshirecat2012.net
● จะต้อง config ใน DNS ให้ถ้กต้องและกำาหนด dnscache ให้ใช้ lsa-svr.coe.psu.ac.th
– สำาหรับกรณีทดลองจาก CoE'NEt (เท่านั้น)
zone new config
$TTL 240
...
$ORIGIN cheshirecat2012.net.IN NS lsasvr.coe.psu.ac.th.IN MX 5 mx.cheshirecat2012.net.IN MX 10 lsasvr.coe.psu.ac.th.
smtp 30 IN A $IPmx 30 IN A $IPimap 30 IN A $IPpop3 30 IN A $IP
zone new config (cont)
● เพิ่มข้อม้ลใน /var/cache/bind/master/$YOURDOMAIN
● $IP คอื IP Address ของเครื่อง● บรรทัด
IN MX 5 mx.$YOURDOMAININ MX 10 lsasvr.coe.psu.ac.th
● ค่า $YOURDOMAIN จะต้องน้อยกว่า ของ lsa-svr เพื่อให้ lsa-svr ส่งต่อไปยังปลายทางได้ถ้กต้อง
dnscache สำาหรับทดสอบ● ใช้ lsa-svr.coe.psu.ac.th เป็น dnscache
● file /etc/resolv.confdomain coe.psu.ac.thsearch coe.psu.ac.thnameserver 172.30.0.85
● ใช้สำาหรับทดสอบจากภายใน CoE'Net เท่านั้น
ทดลองสง่ mail
● ส่ง mail หา [email protected]$ echo “test” |\ mail s “This is a test” [email protected]
● ผลการทำางานของ exim4 ด้ใน log
/var/log/exim4/mainlog$ sudo tail f /var/log/exim4/mainlog$ sudo exigrep cheshirecat2012.net /var/log/exim4/mainlog
ทดลองสง่ mail (ต่อ)
● ทดลองส่ง mail ถึง user ท่ีนั่งโต๊ะข้างๆ เพ่ือความสะดวกให้สร้าง user ท่ีชื่อว่า mailacct เพ่ือง่ายต่อการทดสอบ
● สร้าง mail account บนเครื่องตัวเอง$ sudo useradd m c “Mail Account” mailacct$ echo “mailacct:********” | sudo chpasswd
● สง่ mail หา mailacct บนเครื่องตัวเอง และเครื่องข้างๆ$ echo “`date`” | mail s test mailacct@$MYDOMAIN$ echo “`date`” | mail s test mailacct@$OTHERDOMAIN
● Login โดยใช้ mailacct เพ่ืออ่าน mailหลงัจากใช้งานเสร็จแลว้ กค็วร lock account mailacct โดยการใช้คำาสั่ง$ sudo passwd l mailacct
aliases
● ไฟล์ /etc/aliases
● สำาหรับสร้าง aliases กำาหนด email address ท่ีจะส่งต่อ● กำาหนด aliases default => root
root: `whoami`
● สามารถ copy หรือ forward ไป account อื่นๆได้● หรือแม้จะส่งให้กับ program อื่นโดยเฉพาะ procmail
/etc/aliases
● e.g. บนเคร่ืองซ่ึงเป็น server ของ domain cheshirecat2012.net
root: cjsysadm: [email protected], [email protected]: cj, mailacct
● ส่ง email$ echo “test” | mail -s “Test” [email protected]ส่งถึง cj กับ mailacct บน cheshirecat2012
● ส่งถึง [email protected] จะถ้ก forward ต่อไปยงัcj, sysadmin ของ coe.psu.ac.th