同サイトのページだが探しにくいのでメモ。
SPF(Sender Policy Framework) : 迷惑メール対策委員会
SPFの歴史的背景や、SPF設定時のプロパティの解説や記述例。
間違いから学ぶSPFレコードの正しい書き方 : 迷惑メール対策委員会
SPFレコードを設定する際に間違いやすい例を参考に解説。
同サイトのページだが探しにくいのでメモ。
SPF(Sender Policy Framework) : 迷惑メール対策委員会
SPFの歴史的背景や、SPF設定時のプロパティの解説や記述例。
間違いから学ぶSPFレコードの正しい書き方 : 迷惑メール対策委員会
SPFレコードを設定する際に間違いやすい例を参考に解説。
SpamAssasinのみをインストールしたくなったのでメールサーバ再構築のついでに試してみる。メールサーバ構築のまとめ でPostfix+MySQL+postfixadminは構築済みとして想定しています。そちらではウィルスチェックも導入しているため今回はスパムチェックのみの導入。
また、学習機能があるようなので試してみる。スパム判定の精度が高まるみたい。
コレに合わせて postfixadmin +procmailな環境でメールディレクトリの自動作成・削除 のメールディレクトリ作成スクリプトに .Spam を作るコマンドを追加。
yum install spamassassin
/etc/procmailrc を新規作成する。SpamAssassinの起動条件は参考ブログより引用。
# vi /etc/procmailrc SHELL=/bin/bash PATH=/usr/bin:/bin DEFAULT=/mail/$DOMAIN/$USER\@$DOMAIN/ LOCKFILE=/mail/procmail.lock LOGFILE=/mail/procmail.log # ログ出力先 #VERBOSE=ON # 詳細ログ出力 # メールヘッダー中に「 X-Spam-*** 」の記述がなければ spamassassin を起動します :0fw *!^X-Spam.* |spamassassin # メールヘッダー中に「 X-Spam-Status: Yes 」の記述があれば、「 .Spam 」ディレクトリにメールを格納します :0 * ^X-Spam-Status: Yes $DEFAULT/.Spam/
ログの肥大化を防ぐため、ログローテーション設定ファイル新規作成する。
# vi /etc/logrotate.d/procmail /mail/procmail.log { missingok nocreate notifempty }
main.cf の設定
# vi /etc/postfix/main.cf ....最終行付近 # virtual_transport = virtual ←コメントアウト virtual_transport = procmail ←追記 procmail_destination_recipient_limit = 1 ←追記
master.cf の設定
# vi /etc/postfix/master.cf 最終行に追記 procmail unix - n n - - pipe flags=R user=vuser argv=/usr/bin/procmail -t -m USER=${user} DOMAIN=${nexthop} /etc/procmailrc
SpamAssassin起動
# service spamassassin start
SpamAssassin自動起動設定
# chkconfig spamassassin on
Postfixを再起動して設定を有効化
# /etc/rc.d/init.d/postfix restart
スパムメールの判定制度を上げるために学習する事が出来る。また、迷惑メールではないメールの学習も可能。
# /usr/bin/sa-learn --spam /mail/*/*/.Spam/cur
# /usr/bin/sa-learn --ham /mail/*/*/cur
参考ブログではcronに設定していたけど、Spamメールを確認してから手動でコマンドを実行するようにする。~/binにコマンドスクリプトを作成してsudoで実行できるようにしておく。
$ mkdir ~/bin $ vi ~/bin/spam-learn #!/bin/sh /usr/bin/sa-learn --spam /mail/*/*/.Spam/cur # スパムメール保存ディレクトリの中身を強制的に消去してよいのなら以下の記述を追加 /bin/rm -f /mail/*/*/.Spam/cur/* echo 'All Spam Mail Deleted.' $ chmod 700 ~/bin/spam-learn $ sudo chown root:root ~/bin/spam-learn
メーラーでSpamフォルダに迷惑メールしか無いと確認してから以下のコマンドで学習&スパムメール削除
$ sudo spam-learn
入れなおしたらかなりの確率でスパムメールを排除してくれる!
参考:
スマートフォンでpush通知出来るようにIMAP-IDLE対応のdovecot2をインストールする。
基本的には メールサーバ構築のまとめ(複数のバーチャルドメイン+Postfix+MySQL+Dovecot+procmail+Clam AntiVirus+spamassassin+postfixadmin) のdovecotの部分をdovecot2にするための記事。
yumでインストールできるようにするため、ATrpmsリポジトリを追加する。
# vi /etc/yum.repos.d/atrpms.repo [atrpms] name=Redhat Enterprise Linux $releasever - $basearch - ATrpms baseurl=http://dl.atrpms.net/el$releasever-$basearch/atrpms/stable enabled=0 gpgcheck=1 gpgkey=http://atrpms.net/RPM-GPG-KEY.atrpms
# yum install dovecot --enablerepo=atrpms Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base: www.ftp.ne.jp * centosplus: www.ftp.ne.jp * epel: ftp.kddilabs.jp * extras: www.ftp.ne.jp * rpmforge: ftp.riken.jp * updates: www.ftp.ne.jp Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package dovecot.i386 1:2.1.1-2_132.el5 set to be updated --> Processing Dependency: libpq.so.5 for package: dovecot --> Running transaction check ---> Package postgresql84-libs.i386 0:8.4.13-1.el5_8 set to be updated --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Updating: dovecot i386 1:2.1.1-2_132.el5 atrpms 2.5 M Installing for dependencies: postgresql84-libs i386 8.4.13-1.el5_8 base 213 k Transaction Summary ================================================================================ Install 1 Package(s) Upgrade 1 Package(s) Total download size: 2.7 M Is this ok [y/N]: y ... Running Transaction Installing : postgresql84-libs 1/3 Updating : dovecot 2/3 Cleanup : dovecot 3/3 warning: /etc/dovecot.conf saved as /etc/dovecot.conf.rpmsave Dependency Installed: postgresql84-libs.i386 0:8.4.13-1.el5_8 Updated: dovecot.i386 1:2.1.1-2_132.el5 Complete!
アップデートした場合、設定ファイルはに変更があるのでスクリプトがあるみたいだけど最初から設定することにする。
http://wiki2.dovecot.org/Upgrading
dovecot2では設定ファイルの書式が変わり、諸々の設定は conf.d/以下に置かれている。
・ オリジナルをコピーしておく # cd /etc/dovecot # cp -a dovecot.conf dovecot.conf.org ・ dovecot.confを編集 # vi /etc/dovecot/dovecot.conf # Protocols we want to be serving. #protocols = imap pop3 lmtp protocols = imap pop3 ←追記
# cd /etc/dovecot/conf.d/ # vi /etc/dovecot/conf.d/10-master.conf service imap-login { inet_listener imap { port = 143 ←コメント削除 } inet_listener imaps { port = 993 ←コメント削除 ssl = yes ←コメント削除 } ... service pop3-login { inet_listener pop3 { port = 110 ←コメント削除 } inet_listener pop3s { port = 995 ←コメント削除 ssl = yes ←コメント削除 } } .... service auth { unix_listener auth-userdb { #mode = 0666 #user = #group = } # Postfix smtp-auth unix_listener /var/spool/postfix/private/auth { ←コメント削除 # mode = 0666 mode = 0660 ←追記 user = postfix ←追記 group = postfix ←追記 } ←コメント削除 # Auth process is run as this user. #user = $default_internal_user }
# vi /etc/dovecot/conf.d/10-mail.conf # 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 #mail_location = mail_location = maildir:/mail/%d/%u ← 追記 ... # Valid UID range for users, defaults to 500 and above. This is mostly # to make sure that users can't log in as daemons or other system users. # Note that denying root logins is hardcoded to dovecot binary and can't # be done even if first_valid_uid is set to 0. #first_valid_uid = 500 #last_valid_uid = 0 first_valid_uid = 10000 ← 追記 # Valid GID range for users, defaults to non-root/wheel. Users having # non-valid GID as primary group ID aren't allowed to log in. If user # belongs to supplementary groups with non-valid GIDs, those groups are # not set. #first_valid_gid = 1 #last_valid_gid = 0 first_valid_gid = 10000 ← 追記
# vi /etc/dovecot/conf.d/10-auth.conf # 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 digest-md5 cram-md5 ←追加 .... #!include auth-deny.conf.ext #!include auth-master.conf.ext !include auth-system.conf.ext !include auth-sql.conf.ext ←コメント削除 #!include auth-ldap.conf.ext #!include auth-passwdfile.conf.ext #!include auth-checkpassword.conf.ext #!include auth-vpopmail.conf.ext #!include auth-static.conf.ext
# vi /etc/dovecot/conf.d/10-ssl.conf #ssl_cert =MySQLを使うための設定
※自分はunixユーザでメールを使わないので下記設定で問題ないが、以前頂いたコメントによると問題がある模様。参考まで。
auth-mysql.conf.ext を新規作成
# vi /etc/dovecot/conf.d/auth-mysql.conf.ext driver = mysql default_pass_scheme = PLAIN connect = dbname=postfix host=localhost user=postfix password=passwd password_query = SELECT password FROM mailbox WHERE username = '%u' AND active = '1' user_query = SELECT concat('/mail/', maildir) as home, 10000 as uid, 10000 as gid FROM mailbox WHERE username = '%u' AND active = '1'auth-sql.conf.extを修正
# vi /etc/dovecot/conf.d/auth-sql.conf.ext passdb { driver = sql # Path for SQL configuration file, see example-config/dovecot-sql.conf.ext #args = /etc/dovecot/dovecot-sql.conf.ext args = /etc/dovecot/conf.d/auth-mysql.conf.ext ←追記 } ... userdb { driver = sql #args = /etc/dovecot/dovecot-sql.conf.ext args = /etc/dovecot/conf.d/auth-mysql.conf.ext ←追記 }参考
メールサーバー間通信内容暗号化(OpenSSL+Postfix+Dovecot) - CentOSで自宅サーバー構築 を参考にサーバー証明書を作成
# cd /etc/pki/tls/certs/ # make mail.pem umask 77 ; \ PEM1=`/bin/mktemp /tmp/openssl.XXXXXX` ; \ PEM2=`/bin/mktemp /tmp/openssl.XXXXXX` ; \ /usr/bin/openssl req -utf8 -newkey rsa:2048 -keyout $PEM1 -nodes -x509 -days 365 -out $PEM2 -set_serial 0 ; \ cat $PEM1 > mail.pem ; \ echo "" >> mail.pem ; \ cat $PEM2 >> mail.pem ; \ rm -f $PEM1 $PEM2 Generating a 2048 bit RSA private key ......................................................................................................+++ ............+++ writing new private key to '/tmp/openssl.h27273' ----- 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) [GB]:JP State or Province Name (full name) [Berkshire]:Gunma Locality Name (eg, city) [Newbury]:Maebashi Organization Name (eg, company) [My Company Ltd]:clicktxcloud.com Organizational Unit Name (eg, section) []: Common Name (eg, your name or your server's hostname) []:mail.clicktxcloud.com Email Address []:mail@clicktxcloud.com
smtpd_use_tls = yes #最終行に追記 smtpd_tls_cert_file = /etc/pki/tls/certs/mail.pem smtpd_tls_key_file = /etc/pki/tls/certs/mail.pem smtpd_tls_session_cache_database = btree:/etc/postfix/smtpd_scache
submission inet n - n - - smtpd ←コメント削除 -o smtpd_enforce_tls=yes ←コメント削除 -o smtpd_sasl_auth_enable=yes ←コメント削除 -o smtpd_client_restrictions=permit_sasl_authenticated,reject ←コメント削除 smtps inet n - n - - smtpd ←コメント削除 -o smtpd_tls_wrappermode=yes ←コメント削除 -o smtpd_sasl_auth_enable=yes ←コメント削除 -o smtpd_client_restrictions=permit_sasl_authenticated,reject #628 inet n - n - - qmqpd pickup fifo n - n 60 1 pickup cleanup unix n - n - 0 cleanup qmgr fifo n - n 300 1 qmgr #qmgr fifo n - n 300 1 oqmgr tlsmgr unix - - n 1000? 1 tlsmgr ←コメント削除
$ openssl s_client -starttls smtp -crlf -connect xxx.xxx.xxx.xxx:587 CONNECTED(00000003) ... SSL-Session: Protocol : TLSv1 Cipher : DHE-RSA-AES256-SHA Session-ID: 9AE082BD79AE4029D5AD48BCCA63C3E6970FE3D3617C2EE98586AB7B331A4332 Session-ID-ctx: Master-Key: 64F761C1EA5B4261A67DDBB625A2D04AD88586CD21B0D35F4E62C06C1FDE481EA21BEED6EDCD5548B4DCBEF0A6399881 Key-Arg : None PSK identity: None PSK identity hint: None SRP username: None Compression: 1 (zlib compression) Start Time: 1394449493 Timeout : 300 (sec) Verify return code: 10 (certificate has expired) --- 250 DSN
# Disable SSL/TLS support. ssl_disable = no ←コメント削除 # PEM encoded X.509 SSL/TLS certificate and private key. They're opened before # dropping root privileges, so keep the key file unreadable by anyone but # root. Included doc/mkcert.sh can be used to easily generate self-signed # certificate, just make sure to update the domains in dovecot-openssl.cnf #ssl_cert_file = /etc/pki/dovecot/certs/dovecot.pem #ssl_key_file = /etc/pki/dovecot/private/dovecot.pem ssl_cert_file = /etc/pki/tls/certs/mail.pem ←追記 ssl_key_file = /etc/pki/tls/certs/mail.pem ←追記
Postfix TLS サポート
Postfix+MySQL+Postfix AdminによるバーチャルSMTPサーバの構築(CentOS4.x編)
とりあえずwebアプリ制作において開発環境からメール送信できればいいのでそれなりの設定です。
本格的なメールサーバーとしては運用できませんのであしからず。
主要プロバイダを使っている場合はほぼ対策しなくてはメールが送れません。なぜならプロバイダが25番ポートでの接続を遮断してしまうからです。25番ポートを使った通信が出来ません。
対策としては契約しているプロバイダ経由でメールを送ります。
OP25Bの確認
OP25B(Outbound Port 25 Blocking)対策 - CentOSで自宅サーバー構築
$ telnet smtp.gmail.com 25 Trying 74.125.25.108... telnet: connect to address 74.125.25.108: Operation timed out Trying 74.125.25.109...
こうなったらOP25Bの影響を受けています。
⌘(command)+. あるいは control+c で止めましょう。
587番ポートなら接続可能です。
$ telnet smtp.gmail.com 587 Trying 74.125.25.108... Connected to gmail-smtp-msa.l.google.com. Escape character is '^]'. 220 mx.google.com ESMTP n11sm6774215pby.67 quit
プロバイダの...と書いていますが、自サーバのメールサーバ使った設定にしてしまいます。
※プロバイダ経由の場合の設定ははココが参考になると思います。
$ sudo vi /etc/postfix/sasl_passwd [smtp.clicktx.jp]:587 myname@clicktx.jp:password $ sudo postmap /etc/postfix/sasl_passwd
※ /etc/postfix/sasl_passwd.db というファイルが出来ます。
postfixの設定ファイルmain.cfを編集します。
$ sudo vi /etc/postfix/main.cf
該当箇所に追記or修正
relayhost = [smtp.clicktx.jp]:587
最終行に追記
# Enable SASL authentication in the Postfix SMTP client. smtp_sasl_auth_enable=yes smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd smtp_sasl_security_options= smtp_sasl_mechanism_filter = plain
postfixの起動またはリロード
$ sudo postfix start
$ sudo postfix reload
$ mail xxx@hoge.com(宛先) Subject: テストメール テストメール .
参考