1 views
実現する機能
自宅にメールサーバーを構築するメリットは、メールアドレスを自由に決められる、添付ファイルの制限も大幅に緩和できる、yahoo、gmail、hotmailすべてを自宅サーバーに転送させて一気に確認できることです。
構築するサーバーは、送信メールサーバー(SMTPサーバー)と受信メールサーバー(POP/IMAPサーバー)です。送信メールサーバーにはPostfix、受信メールサーバーにはDovecotを採用しています。
ただ、自宅のメールサーバーは迷惑メールの不正中継に利用されやすいので、インターネットを使って送信できないようになっています。そこで、どこにでも自由にメールを送信できるようにするため、SMTP-Auth機能※をもたせました。
また、DovecotはPOP/IMAPサーバーとして構築し、ユーザがPOPまたはIMAPを選択できるようにしました。※POPとIMAPの違い
※SMTP-Auth機能とは、メール送信時にインターネットプロバイダのユーザ名とパスワードで認証を行なう機能で、迷惑メールの不正中継に利用されないためのものです。auのひかりちゅらをプロバイダで使っていますが、問題なく外部へメール送信ができます。これで内外への送受信が集中管理できるので便利です。
Postfixの導入手順
(1)Postfix設定
[root@ufuso ~]# vi /etc/postfix/main.cf ← Postfix設定ファイル編集 # INTERNET HOST AND DOMAIN NAMES # # The myhostname parameter specifies the internet hostname of this # mail system. The default is to use the fully-qualified domain name # from gethostname(). $myhostname is used as a default value for many # other configuration parameters. # #myhostname = host.domain.tld #myhostname = virtual.domain.tld myhostname = ufuso.dip.jp ← 追加(自FQDN名を指定) # The mydomain parameter specifies the local internet domain name. # The default is to use $myhostname minus the first component. # $mydomain is used as a default value for many other configuration # parameters. # #mydomain = domain.tld mydomain = ufuso.dip.jp ← 追加(自ドメイン名を指定) # SENDING MAIL # # The myorigin parameter specifies the domain that locally-posted # mail appears to come from. The default is to append $myhostname, # which is fine for small sites. If you run a domain with multiple # machines, you should (1) change this to $mydomain and (2) set up # a domain-wide alias database that aliases each user to # user@that.users.mailhost. # # For the sake of consistency between sender and recipient addresses, # myorigin also specifies the default domain name that is appended # to recipient addresses that have no @domain part. # #myorigin = $myhostname myorigin = $mydomain ← 追加(自ドメイン名を指定) # RECEIVING MAIL # The inet_interfaces parameter specifies the network interface # addresses that this mail system receives mail on. By default, # the software claims all active interfaces on the machine. The # parameter also controls delivery of mail to user@[ip.address]. # # See also the proxy_interfaces parameter, for network addresses that # are forwarded to us via a proxy or network address translator. # # Note: you need to stop/start Postfix when this parameter changes. # inet_interfaces = all ← 行頭の#を削除(外部からのメール受信を許可) #inet_interfaces = $myhostname #inet_interfaces = $myhostname, localhost # Enable IPv4, and IPv6 if supported #inet_protocols = all inet_protocols = ipv4 ← IPv4のみ使用 # STANDARD_CONFIGURATION_README). # # The local machine is always the final destination for mail addressed # to user@[the.net.work.address] of an interface that the mail system # receives mail on (see the inet_interfaces parameter). # # Specify a list of host or domain names, /file/name or type:table # patterns, separated by commas and/or whitespace. A /file/name # pattern is replaced by its contents; a type:table is matched when # a name matches a lookup key (the right-hand side is ignored). # Continue long lines by starting the next line with whitespace. # # See also below, section "REJECTING MAIL FOR UNKNOWN LOCAL USERS". # #mydestination = $myhostname, localhost.$mydomain, localhost ← 行頭に#を付加して無効にする mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain ← 行頭の#を削除(自ドメイン宛メールを受信できるようにする) #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, # mail.$mydomain, www.$mydomain, ftp.$mydomain # DELIVERY TO MAILBOX # # The home_mailbox parameter specifies the optional pathname of a # mailbox file relative to a user's home directory. The default # mailbox file is /var/spool/mail/user or /var/mail/user. Specify # "Maildir/" for qmail-style delivery (the / is required). # #home_mailbox = Mailbox home_mailbox = Maildir/ ← 行頭の#を削除(メールボックス形式をMaildir形式にする) # SHOW SOFTWARE VERSION OR NOT # # The smtpd_banner parameter specifies the text that follows the 220 # code in the SMTP server's greeting banner. Some people like to see # the mail version advertised. By default, Postfix shows no version. # # You MUST specify $myhostname at the start of the text. That is an # RFC requirement. Postfix itself does not care. # #smtpd_banner = $myhostname ESMTP $mail_name #smtpd_banner = $myhostname ESMTP $mail_name ($mail_version) smtpd_banner = $myhostname ESMTP unknown ← 追加(メールサーバーソフト名の隠蔽化) 以下を最終行へ追加(SMTP-Auth設定) smtpd_sasl_auth_enable = yes smtpd_sasl_local_domain = $myhostname smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated reject_unauth_destination 以下を最終行へ追加(受信メールサイズ制限設定) message_size_limit = 20971520 ← 受信メールサイズを20MBに制限設定 |
(2)SMTP-Auth設定(SMTP-Auth用ユーザ名、パスワードにシステムのユーザ名、パスワードを使用する場合)
[root@ufuso ~]# /etc/rc.d/init.d/saslauthd start ← saslauthd起動 saslauthd を起動中: [ OK ] [root@ufuso ~]# chkconfig saslauthd on ← saslauthd自動起動設定 |
(3)Maildir形式メールボックス作成
Postfixのメール格納形式は共有ディレクトリ形式(「/var/spool/mail/ユーザ名」というファイルに全てのメールが蓄積されていく形式)ですが、アクセス性能改善及びセキュリティ強化の観点からMaildir形式へ移行します。
【新規ユーザ対処】
新規ユーザ追加時に自動でホームディレクトリにMaildir形式のメールボックスが作成されるようにします。
[root@ufuso ~]# mkdir -p /etc/skel/Maildir/{new,cur,tmp} ← 新規ユーザ追加時に自動で Maildir形式メールボックス作成 [root@ufuso ~]# chmod -R 700 /etc/skel/Maildir/ ← 所有者以外読み書き不可に設定 |
(4)未承諾広告メールの削除
未承諾広告メール(件名に「未承諾広告※」と表示されているメール)は受信せずに削除するようにする。
[root@ufuso ~]# vi /etc/postfix/main.cf ← Postfix設定ファイル編集 # IF YOU USE THIS TO DELIVER MAIL SYSTEM-WIDE, YOU MUST SET UP AN # ALIAS THAT FORWARDS MAIL FOR ROOT TO A REAL USER. # #mailbox_command = /some/where/procmail #mailbox_command = /some/where/procmail -a "$EXTENSION" mailbox_command = /usr/bin/procmail ← Procmailと連携 [root@ufuso ~]# vi /etc/procmailrc ← procmail設定ファイル作成 SHELL=/bin/bash PATH=/usr/bin:/bin DROPPRIVS=yes MAILDIR=$HOME/Maildir DEFAULT=$MAILDIR/ LOGFILE=$MAILDIR/.procmail.log # ログ出力先 #VERBOSE=ON # 詳細ログ出力 # 未承諾広告メール削除 :0 * ^Subject:.*iso-2022-jp * ^Subject:/.* * ? echo "$MATCH" | nkf -mwZ2 | sed 's/[[:space:]]//g' | egrep '未承諾広告※' /dev/null # 未承諾広告メール削除※システムの文字コードがEUCの場合 :0 * ^Subject:.*iso-2022-jp * ^Subject:/.* * ? echo "$MATCH" | nkf -meZ2 | sed 's/[[:space:]]//g' | egrep '未承諾広告※' /dev/null # SpamAssassinによるスパムチェック :0fw |/usr/bin/spamc # SpamAssassinにより判定されたSpam-Levelが一定値(ここでは20)以上の場合は削除 # ※必要なメールが削除されてしまう可能性があることに留意すること :0 * ^X-Spam-Level: ******************** /dev/null # SpamAssassinがスパム判定したメールはスパム専用メールボックスへ配送 :0 *^X-Spam-Flag: YES $SPAM [root@scientific ~]# vi /etc/logrotate.d/procmail ← procmailログローテーション設定 ファイル作成 /home/*/Maildir/.procmail.log { missingok nocreate notifempty } |
(5)Postfix再起動
[root@ufuso ~]# /etc/rc.d/init.d/postfix restart ← Postfix再起動 postfix を停止中: [ OK ] postfix を起動中: [ OK ] |
Dovecotの導入手順
(1)Dovecotインストール
[root@ufuso ~]# yum -y install dovecot ← Dovecotインストール Installed: dovecot.x86_64 1:2.0.9-2.el6_1.1 Complete! [root@ufuso ~]# vi /etc/dovecot/dovecot.conf ← Dovecotの設定 # A comma separated list of IPs or hosts where to listen in for connections. # "*" listens in all IPv4 interfaces, "::" listens in all IPv6 interfaces. # If you want to specify non-default ports or anything more complex, # edit conf.d/master.conf. #listen = *, :: listen = * ← 追記。IPv6を無効化 [root@ufuso ~]# vi /etc/dovecot/conf.d/10-auth.conf ← 認証の設定 # Disable LOGIN command and all other plaintext authentications unless # SSL/TLS is used (LOGINDISABLED capability). Note that if the remote IP # matches the local IP (ie. you're connecting from the same computer), the # connection is considered secure and plaintext authentication is allowed. #disable_plaintext_auth = yes disable_plaintext_auth = no ← 追記。プレーンテキスト認証も許可 # Space separated list of wanted authentication mechanisms: # plain login digest-md5 cram-md5 ntlm rpa apop anonymous gssapi otp skey # gss-spnego # NOTE: See also disable_plaintext_auth setting. auth_mechanisms = plain login ← ログイン認証も許可 [root@ufuso ~]# vi /etc/dovecot/conf.d/10-mail.conf ← Dovecotの設定 # See doc/wiki/Variables.txt for full list. Some examples: # # mail_location = maildir:~/Maildir # mail_location = mbox:~/mail:INBOX=/var/mail/%u # mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n # # # See doc/wiki/Variables.txt for full list. Some examples: # # mail_location = maildir:~/Maildir # mail_location = mbox:~/mail:INBOX=/var/mail/%u # mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n # # <doc/wiki/MailLocation.txt> # #mail_location = mail_location = maildir:~/Maildir ← メールボックス形式をMaildir形式に [root@ufuso ~]# vi /etc/dovecot/conf.d/10-master.conf ← Dovecotの設定 # Postfix smtp-auth unix_listener /var/spool/postfix/private/auth { mode = 0666 user = postfix ← 追記 group = postfix ← 追記 } [root@ufuso ~]# /etc/rc.d/init.d/dovecot start ← Dovecot起動 Dovecot Imapを起動中: [ OK ] [root@ufuso ~]# chkconfig dovecot on ← Dovecot自動起動設定 |
(2)ポート110番(POPの場合)または143番(IMAPの場合)のOPEN
ルーター側の設定でポート110番(POPの場合)または143番(IMAPの場合)をOPENにします。
※ルーターの設定は各ルーターのマニュアルまたはメーカー別ルーターポート開放手順を参照
ポートチェック【外部からポート開放確認】で「host名」にサーバー名(例:ufuso.jp)、「port番号」に110(POPの場合)または143(IMAPの場合)と入力して「ポートチェック」ボタン押下し、「ホスト=ufuso.jp ポート=110(POPの場合)または143(IMAPの場合) にアクセスできました。」と表示されることを確認。
OP25B対策(SMTP-Auth機能)
(1)Postfix設定
[root@ufuso ~]# vi /etc/postfix/main.cf ← メイン設定の編集 # INTERNET OR INTRANET # The relayhost parameter specifies the default host to send mail to # when no entry is matched in the optional transport(5) table. When # no relayhost is given, mail is routed directly to the destination. # # On an intranet, specify the organizational domain name. If your # internal DNS uses no MX records, specify the name of the intranet # gateway host instead. # # In the case of SMTP, specify a domain, host, host:port, [host]:port, # [address] or [address]:port; the form [host] turns off MX lookups. # # If you're connected via UUCP, see also the default_transport parameter. # #relayhost = $mydomain #relayhost = [gateway.my.domain] #relayhost = [mailserver.isp.tld] #relayhost = uucphost #relayhost = [an.ip.add.ress] relayhost = [auth.au-hikari.ne.jp]:587 ← 行頭の#を外しプロバイダのSMTPサーバーを指定 以下を最終行へ追記 smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/authinfo smtp_sasl_security_options = noanonymous smtp_sasl_mechanism_filter = LOGIN |
(2)SMTP認証情報設定
[root@ ~]# echo [auth.au-hikari.ne.jp]:587 ユーザー名@at.au-hikari.ne.jp:パスワード > /etc/postfix/authinfo ← SMTP認証情報設定 SMTPサーバー名・・・プロバイダのSMTPサーバー名 ユーザー名・・・プロバイダのメールアカウント名 @at.au-hikari.ne.jp・・・ご自分のメールアカウント名の@以降の部分 パスワード・・・プロバイダのメールパスワード [root@ ~]# chmod 640 /etc/postfix/authinfo ← root以外参照できないようにパーミッション変更 [root@ ~]# postmap /etc/postfix/authinfo ← authinfo.db作成 |
(3)Postfix設定反映
[root@ ~]# /etc/rc.d/init.d/postfix reload ← Postfix設定反映 postfix を再読み込み中: [ OK ] |
コメントを残す