lost and found ( for me ? )

Ubuntu 10.10 : Postfix , Courier-pop/imap Basic configuration( SMTP , POP3 , IMAP )

基本的なメールの設定。メールの送受信をできるようにする。
root@ubuntu-7:~# tail -1 /etc/lsb-release
DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS"


[ SMTPの設定: Postfix ]

・Postfixをインストール
root@ubuntu-7:~# apt-get install postfix mailutils

root@ubuntu-7:~# dpkg -l postfix | tail -1
ii  postfix                           2.7.0-1                    High-performance mail transport agent


・テスト用ユーザを作成
root@ubuntu-7:/home# useradd -m -s /bin/bash test01
root@ubuntu-7:/home# passwd test01


・Postfixのコンフィグ。mydestination を自分のホスト名、ドメイン名にする。
root@ubuntu-7:/home# egrep -v ^# /etc/postfix/main.cf | egrep -v ^$
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
append_dot_mydomain = no
readme_directory = no
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
myhostname = ubuntu-7.example.jp
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = ubuntu-7.example.jp, localhost.example.jp, example.jp, localhost
relayhost =
mynetworks = 192.168.11.0/24 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all


・BINDの設定

テスト用ドメイン example.jp の MX をひけるようにする。
root@ubuntu-7:~# cat /var/cache/bind/example.jp.zone
$TTL    3600
@       IN      SOA     example.jp. root.example.jp.  (
                               2010122402      ; Serial
                               3600            ; Refresh
                               900             ; Retry
                               3600000         ; Expire
                               3600 )          ; Minimum
       IN      NS      ubuntu-7.example.jp.
       IN      MX      0 ubuntu-7.example.jp.
ubuntu-7        IN      A       192.168.11.150

root@ubuntu-7:~# dig @127.1 example.jp mx

; <<>> DiG 9.7.0-P1 <<>> @127.1 example.jp mx
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23181
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:
;example.jp.                    IN      MX

;; ANSWER SECTION:
example.jp.             3600    IN      MX      0 ubuntu-7.example.jp.

;; AUTHORITY SECTION:
example.jp.             3600    IN      NS      ubuntu-7.example.jp.

;; ADDITIONAL SECTION:
ubuntu-7.example.jp.    3600    IN      A       192.168.11.150


・Postfix起動
root@ubuntu-7:~# /etc/init.d/postfix start
* Starting Postfix Mail Transport Agent postfix
  ...done.

root@ubuntu-7:~# postfix status
postfix/postfix-script: the Postfix mail system is running: PID: 3034


設定は postconf で確認できる。
root@ubuntu-7:~# postconf | head -5
2bounce_notice_recipient = postmaster
access_map_defer_code = 450
access_map_reject_code = 554
address_verify_cache_cleanup_interval = 12h
address_verify_default_transport = $default_transport


・メール送信
root@ubuntu-7:~# telnet 127.1 25
Trying 127.0.0.1...
Connected to 127.1.
Escape character is '^]'.
220 ubuntu-7.example.jp ESMTP Postfix (Ubuntu)
ehlo localhost
250-ubuntu-7.example.jp
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail from:root@localhost
250 2.1.0 Ok
rcpt to:test01@example.jp
250 2.1.5 Ok
data
354 End data with .
subject: test
hello!
.
250 2.0.0 Ok: queued as E6B3B2036E
quit
221 2.0.0 Bye
Connection closed by foreign host.


・ユーザ test01 になってメールをチェック
root@ubuntu-7:~# su - test01


mail コマンドでメッセージを確認。
test01@ubuntu-7:~$ mail
"/var/mail/test01": 1 message 1 new
>N   1 root@localhost     火 12月 28 04:  13/443   test
? 1
Return-Path:
X-Original-To: test01@example.jp
Delivered-To: test01@example.jp
Received: from localhost (localhost [127.0.0.1])
       by ubuntu-7.example.jp (Postfix) with ESMTP id E6B3B2036E
       for ; Tue, 28 Dec 2010 04:26:43 +0000 (UTC)
subject: test
Message-Id: <20101228042651.E6B3B2036E@ubuntu-7.example.jp>
Date: Tue, 28 Dec 2010 04:26:43 +0000 (UTC)
From: root@localhost
To: undisclosed-recipients:;

hello!
? quit
Saved 1 message in /home/test01/mbox
Held 0 messages in /var/mail/test01


/var/mail/test01 のメールの内容が /home/test01/mbox に書きだされる。
test01@ubuntu-7:~$ cat /home/test01/mbox
From root@localhost  Tue Dec 28 04:27:05 2010
Return-Path:
X-Original-To: test01@example.jp
Delivered-To: test01@example.jp
Received: from localhost (localhost [127.0.0.1])
       by ubuntu-7.example.jp (Postfix) with ESMTP id E6B3B2036E
       for ; Tue, 28 Dec 2010 04:26:43 +0000 (UTC)
subject: test
Message-Id: <20101228042651.E6B3B2036E@ubuntu-7.example.jp>
Date: Tue, 28 Dec 2010 04:26:43 +0000 (UTC)
From: root@localhost
To: undisclosed-recipients:;

hello!


・メール保存をスプールからディレクトリ方式に変更

今はスプール形式
root@ubuntu-7:~# postconf | grep mail_spool
mail_spool_directory = /var/mail


ひとつのファイルにメールが保存される。
root@ubuntu-7:~# cat /var/mail/test01
From root@example.jp  Tue Dec 28 04:44:15 2010
Return-Path:
X-Original-To: test01@example.jp
Delivered-To: test01@example.jp
Received: from localhsot (localhost [127.0.0.1])
       by ubuntu-7.example.jp (Postfix) with ESMTP id 54BD42036E
       for ; Tue, 28 Dec 2010 04:44:01 +0000 (UTC)
Message-Id: <20101228044408.54BD42036E@ubuntu-7.example.jp>
Date: Tue, 28 Dec 2010 04:44:01 +0000 (UTC)
From: root@example.jp
To: undisclosed-recipients:;

aaa
/

From root@example.jp  Tue Dec 28 04:44:39 2010
Return-Path:
X-Original-To: test01@example.jp
Delivered-To: test01@example.jp
Received: from localhsot (localhost [127.0.0.1])
       by ubuntu-7.example.jp (Postfix) with ESMTP id 8A4362036E
       for ; Tue, 28 Dec 2010 04:44:25 +0000 (UTC)
Message-Id: <20101228044433.8A4362036E@ubuntu-7.example.jp>
Date: Tue, 28 Dec 2010 04:44:25 +0000 (UTC)
From: root@example.jp
To: undisclosed-recipients:;

bbbb
zz

root@ubuntu-7:~#


Maildir形式に変更
root@ubuntu-7:~# postconf -e "home_mailbox = Maildir/"
root@ubuntu-7:~# postconf -e "mailbox_command = "


変更後のコンフィグ
root@ubuntu-7:~# egrep -v ^# /etc/postfix/main.cf | egrep -v ^$
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
append_dot_mydomain = no
readme_directory = no
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
myhostname = ubuntu-7.example.jp
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = ubuntu-7.example.jp, localhost.example.jp, example.jp, localhost
relayhost =
mynetworks = 192.168.11.0/24 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
home_mailbox = Maildir/
mailbox_command =


Postfixをリロード
root@ubuntu-7:~# postfix reload
postfix/postfix-script: refreshing the Postfix mail system


/home/ユーザ/Maildir ディレクトリ下にメール毎にファイルが作成される。
test01@ubuntu-7:~$ cat Maildir/new/12935121
1293512106.V801I40a7dM892581.ubuntu-7  1293512159.V801I40a84M99355.ubuntu-7
test01@ubuntu-7:~$ cat Maildir/new/12935121*
Return-Path:
X-Original-To: test01@example.jp
Delivered-To: test01@example.jp
Received: from local (localhost [127.0.0.1])
       by ubuntu-7.example.jp (Postfix) with ESMTP id 73CF12036E
       for ; Tue, 28 Dec 2010 04:54:51 +0000 (UTC)
Message-Id: <20101228045500.73CF12036E@ubuntu-7.example.jp>
Date: Tue, 28 Dec 2010 04:54:51 +0000 (UTC)
From: root@example.jp
To: undisclosed-recipients:;

mail 1
Return-Path:
X-Original-To: test01@example.jp
Delivered-To: test01@example.jp
Received: from local (localhost [127.0.0.1])
       by ubuntu-7.example.jp (Postfix) with ESMTP id 6EDAE2036E;
       Tue, 28 Dec 2010 04:55:14 +0000 (UTC)
Message-Id: <20101228045518.6EDAE2036E@ubuntu-7.example.jp>
Date: Tue, 28 Dec 2010 04:55:14 +0000 (UTC)
From: root@example.jp
To: undisclosed-recipients:;

mail 2
test01@ubuntu-7:~$


[ IMAP 、POP3の設定: courier-pop , courier-imap ]

root@ubuntu-7:~# apt-get install courier-pop courier-imap


設定ファイル ( authdamonrc , imapd , pop3d ) 。デフォルトのまま。
root@ubuntu-7:/etc/courier# pwd
/etc/courier
root@ubuntu-7:/etc/courier# ls
authdaemonrc  imapd  pop3d  shared


デーモン起動
root@ubuntu-7:~# /etc/init.d/courier-pop start
root@ubuntu-7:~# /etc/init.d/courier-imap start
root@ubuntu-7:~# /etc/init.d/courier-authdaemon start ←これ起動しないと認証できない


POP3(TCP 110)で認証、メールチェックができることを確認
test01@ubuntu-7:~$ netcat localhost 110
+OK Hello there.
user test01
+OK Password required.
pass test01
+OK logged in.
LIST
+OK POP3 clients that break here, they violate STD53.
1 439
2 415
.
RETR 1
+OK 439 octets follow.
Return-Path:
X-Original-To: test01@example.jp
Delivered-To: test01@example.jp
Received: from local (localhost [127.0.0.1])
       by ubuntu-7.example.jp (Postfix) with ESMTP id 73CF12036E
       for ; Tue, 28 Dec 2010 04:54:51 +0000 (UTC)
Message-Id: <20101228045500.73CF12036E@ubuntu-7.example.jp>
Date: Tue, 28 Dec 2010 04:54:51 +0000 (UTC)
From: root@example.jp
To: undisclosed-recipients:;

mail 1
.
quit
+OK Bye-bye.
test01@ubuntu-7:~$


IMAPもチェック
ログイン確認だけ。(赤字はタイプしたところ)
test01@ubuntu-7:~$ netcat localhost 143
* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA IDLE ACL ACL2=UNION] Courier-IMAP ready. Copyright 1998-2008 Double Precision, Inc.  See COPYING for distribution information.
a login test01 test01  ← a login ユーザ名 パスワード
a OK LOGIN Ok.
a logout ←ログアウト
* BYE Courier-IMAP server shutting down
a OK LOGOUT completed
test01@ubuntu-7:~$


・Aliasの設定

alias の参照を有効にする
root@ubuntu-7:/etc/courier# postconf alias_maps
alias_maps = hash:/etc/aliases


テスト用ユーザ test02 を作成
root@ubuntu-7:~# useradd -m -s /bin/bash test02
root@ubuntu-7:~# passwd test02


root@ubuntu-7:~# egrep -v ^# /etc/aliases
postmaster:    root ←もとから
test01: test02 ←追加


/etc/alias を更新
root@ubuntu-7:~# newaliases


test01 にメールを送信。test02におくられないな。。
test02のドメインが example.jp じゃなくて、defaultdomain になってる。
Dec 28 06:16:49 ubuntu-7 postfix/local[3992]: 6608E2036C: to=, relay=local, delay=12, delays=12/0.01/0/0.01, dsn=2.0.0, status=sent (forwarded as C14392036D)
Dec 28 06:16:49 ubuntu-7 postfix/qmgr[3912]: 6608E2036C: removed
Dec 28 06:16:49 ubuntu-7 postfix/smtp[3993]: C14392036D: to=, orig_to=, relay=none, delay=0.03, delays=0.01/0.01/0/0, dsn=5.4.4, status=bounced (Host or domain name not found. Name service error for name=ubuntu-7.defaultdomain type=A: Host not found)


これが原因かな。/etc/mailnameの defaultdomain を example.jp に変更。
root@ubuntu-7:~# cat /etc/mailname
ubuntu-7.defaultdomain

root@ubuntu-7:~# hostname --fqdn > /etc/mailname

root@ubuntu-7:~# cat /etc/mailname
ubuntu-7.example.jp

root@ubuntu-7:~# postfix reload


再度トライ。
test02にメールがとどいた。
root@ubuntu-7:~# cat /home/test02/Maildir/new/1293519285.V801I424cdM23390.ubuntu-7
Return-Path:
X-Original-To: test01@example.jp
Delivered-To: test01@example.jp
Received: from localhost (localhost [127.0.0.1])
       by ubuntu-7.example.jp (Postfix) with ESMTP id 103F52044F
       for ; Tue, 28 Dec 2010 06:54:31 +0000 (UTC)
Message-Id: <20101228065440.103F52044F@ubuntu-7.example.jp>
Date: Tue, 28 Dec 2010 06:54:31 +0000 (UTC)
From: root@example.jp
To: undisclosed-recipients:;

hello
bye

Dec 28 06:54:19 ubuntu-7 postfix/smtpd[4202]: connect from localhost[127.0.0.1]
Dec 28 06:54:40 ubuntu-7 postfix/smtpd[4202]: 103F52044F: client=localhost[127.0.0.1]
Dec 28 06:54:44 ubuntu-7 postfix/cleanup[4207]: 103F52044F: message-id=<20101228065440.103F52044F@ubuntu-7.example.jp>
Dec 28 06:54:45 ubuntu-7 postfix/qmgr[4175]: 103F52044F: from=, size=348, nrcpt=1 (queue active)
Dec 28 06:54:45 ubuntu-7 postfix/local[4208]: 103F52044F: to=, orig_to=, relay=local, delay=13, delays=13/0.02/0/0.01, dsn=2.0.0, status=sent (delivered to maildir)


・ユーザ単位でフォワード .forward ファイル
test03@ubuntu-7:~$ whoami
test03


test03@example.jp あては test04@example.jp にフォワードされる。
test03@ubuntu-7:~$ cat .forward
test04@example.jp


test03にメールを送信。test04にフォワードされた。
root@ubuntu-7:~# cat /home/test04/Maildir/new/1293520445.V801I617a8M707371.ubuntu-7
Return-Path:
X-Original-To: test03@example.jp
Delivered-To: test04@example.jp
Received: by ubuntu-7.example.jp (Postfix)
       id A8E9520469; Tue, 28 Dec 2010 07:14:05 +0000 (UTC)
Delivered-To: test03@example.jp
Received: from localhsot (localhost [127.0.0.1])
       by ubuntu-7.example.jp (Postfix) with ESMTP id 718C02044F
       for ; Tue, 28 Dec 2010 07:13:51 +0000 (UTC)
Message-Id: <20101228071400.718C02044F@ubuntu-7.example.jp>
Date: Tue, 28 Dec 2010 07:13:51 +0000 (UTC)
From: test@fsdafsd.jp
To: undisclosed-recipients:;

have fun!

Dec 28 07:14:05 ubuntu-7 postfix/qmgr[4175]: A8E9520469: from=, size=479, nrcpt=1 (queue active)
Dec 28 07:14:05 ubuntu-7 postfix/local[4287]: 718C02044F: to=, relay=local, delay=14, delays=14/0.01/0/0.02, dsn=2.0.0, status=sent (forwarded as A8E9520469)
Dec 28 07:14:05 ubuntu-7 postfix/qmgr[4175]: 718C02044F: removed
Dec 28 07:14:05 ubuntu-7 postfix/local[4287]: A8E9520469: to=, orig_to=, relay=local, delay=0.03, delays=0.01/0/0/0.02, dsn=2.0.0, status=sent (delivered to maildir)


・バーチャルドメインの設定

1台のマシンで複数のドメインを扱う。
example.jp
example2.jp

example2.jp のMXもひけるようにBINDを設定
root@ubuntu-7:~# dig example2.jp mx

; <<>> DiG 9.7.0-P1 <<>> example2.jp mx
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31391
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:
;example2.jp.                   IN      MX

;; ANSWER SECTION:
example2.jp.            3600    IN      MX      0 ubuntu-7.example2.jp.

;; AUTHORITY SECTION:
example2.jp.            3600    IN      NS      ubuntu-7.example2.jp.

;; ADDITIONAL SECTION:
ubuntu-7.example2.jp.   3600    IN      A       192.168.11.150


main.cf を編集。
root@ubuntu-7:~# postconf -e "virtual_alias_domains = example2.jp"
root@ubuntu-7:~# postconf -e "virtual_alias_maps = hash:/etc/postfix/virtual"
root@ubuntu-7:~#

root@ubuntu-7:~# postconf virtual_alias_domains
virtual_alias_domains = example2.jp
root@ubuntu-7:~# postconf virtual_alias_maps
virtual_alias_maps = hash:/etc/postfix/virtual


info@example2.jp は test04 へ。
root@ubuntu-7:~# cat /etc/postfix/virtual
info@example2.jp        test04


MAP DBを作成
root@ubuntu-7:~# postmap /etc/postfix/virtual


/etc/postfix/virtual を /etc/postfix/virtual.db ファイルに変換する。
root@ubuntu-7:~# file /etc/postfix/virtual.db
/etc/postfix/virtual.db: Berkeley DB (Hash, version 9, native byte-order)

root@ubuntu-7:~# postfix reload


info@example2.jp あてにメールを送信。 test04 に送信される。
Dec 28 07:56:56 ubuntu-7 postfix/qmgr[4387]: 09FA02044F: from=, size=353, nrcpt=1 (queue active)
Dec 28 07:56:56 ubuntu-7 postfix/local[4400]: 09FA02044F: to=, orig_to=, relay=local, delay=16, delays=16/0.01/0/0.02, dsn=2.0.0, status=sent (delivered to maildir)

root@ubuntu-7:~# cat /home/test04/Maildir/new/1293523016.V801I617b3M731511.ubuntu-7
Return-Path:
X-Original-To: info@example2.jp
Delivered-To: test04@ubuntu-7.example.jp
Received: from localhost (localhost [127.0.0.1])
       by ubuntu-7.example.jp (Postfix) with ESMTP id 09FA02044F
       for ; Tue, 28 Dec 2010 07:56:40 +0000 (UTC)
Message-Id: <20101228075650.09FA02044F@ubuntu-7.example.jp>
Date: Tue, 28 Dec 2010 07:56:40 +0000 (UTC)
From: root@example.jp
To: undisclosed-recipients:;

have a nice day!