ユーザ用ツール

サイト用ツール


メールサーバ_postfix

postfixのインストール

postfix で postfix-policyd-spf-python ( or postfix-policyd-spf-perl ) を使ってSPF認証する(Debian/Ubuntu編)

DomainKeys Identified Mail (DKIM) の仕組み

debian stretchにpostfixとclamav、Spamassassinをamavisで連携してみる。

SSLメールサーバ構築メモ Let's Encrypt+Postfix+Dovecot

[SOLVED] How to DKIM sign emails by amavisd-new

DMARCと送信ドメイン認証( SPF, DKIM) を理解して、実際に設定してみよう

Linux/Mail/OpenDMARC

Gmailに学ぶ「セキュリティに配慮したメールサーバを構築する方法」

How to configure OpenDKIM with Postfix on Debian 12 (bookworm)

$ sudo apt-get install postfix

「Postfix Configuration」で、<Ok>を選択しenterキー
「Postfix Configuration」のGeneral type of configuration?
 で、Internet Siteを選択しenterキー
「Postfix Configuration」のWhere should mail for root go
 で、管理者用ユーザIDを入力しenterキー
「Postfix Configuration」のMail name? で、ドメイン名を入力しenterキー
「Postfix Configuration」のOther destinations to accept mail for? 
 で、そのままenterキー
「Postfix Configuration」のForce synchronous updates on mail queue?
 で、 <No>を選択しenterキー

設定の再読み込み

$ sudo /etc/init.d/postfix reload

ローカルから直接telnetでテストメールを送信

$ telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.localdomain.
Escape character is '^]'.
220 localhost.localdomain ESMTP Postfix (Debian/GNU)
helo test
250 localhost.localdomain
mail from: test@localhost
250 Ok
rcpt to: xxxx@yyyy.zzz
 250 Ok
data
354 End data with <CR><LF>.<CR><LF>
 mail test
xxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyy
.
250 Ok: queued as DF41F6D0053
quit
221 Bye
Connection closed by foreign host.

テストメールを読む

$ mail
Mail version 8.1.2 01/15/2001.  Type ? for help.
"/var/mail/xxxx": 1 message 1 new
>N  1 test@localhost     Sun Dec 10 22:56   16/536
& 1
Message 1:
From test@localhost  Sun Dec 10 22:56:53 2006
X-Original-To: xxxx@yyyy.zzz
Date: Sun, 10 Dec 2006 22:56:03 +0900 (JST)
From: test@localhost
To: undisclosed-recipients:;
 
mail test
xxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyy

& q
Saved 1 message in /home/xxxx/mbox
 
SASLでSMTP-AUTH (DIGEST-MD5 & CRAM-MD5)

$ sudo apt-get install postfix-tls libsasl2 libsasl2-modules sasl2-bin 

$ sudo emacs /etc/default/saslauthd
で、
START=no
を
START=yes

$ sudo vi /etc/postfix/main.cf

で、以下を追加

smtpd_sasl_auth_enable=yes
smtpd_sasl_local_domain=$myhostname
smtpd_recipient_restrictions=
  permit_mynetworks,
  permit_sasl_authenticated,
  reject_unauth_destination
smtpd_sasl_security_options=noanonymous

パスワードチェックの設定ファイル作成

$ sudo vi /etc/postfix/sasl/smtpd.conf 

で新規作成して、以下の設定を書く。

pwcheck_method: auxprop
mech_list: cram-md5 digest-md5

SASLパスワード設定 
 
$ sudo saslpasswd2 -c -u ドメイン名 smtpユーザ名

設定ユーザ確認は、
$ sudo sasldblistusers2
 
設定削除
$ sudo saslpasswd2 -d -u ドメイン名 smtpユーザ名

パスワードデータベースsasldb2の設定
$ sudo chgrp postfix /etc/sasldb2
$ sudo chmod 640 /etc/sasldb2
$ sudo ln /etc/sasldb2 /var/spool/postfix/etc

$ sudo postfix check  で、チェックし何も表示されなければ
$ sudo /etc/init.d/postfix restart

SMTP-AUTH の確認

$ telnet localhost 25
Trying 127.0.0.1...
Connected to ホスト名.
Escape character is '^]'.
220 myhostname名 ESMTP Postfix (Debian/GNU)
ehlo localhost   <== 入力
250-myhostname名
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH DIGEST-MD5 CRAM-MD5
250 8BITMIME
quit   <== 入力
221 Bye
Connection closed by foreign host.

Evolution(メールソフト)での確認

メニューの[アプリケーション]-[オフィス]-[Evolution]で起動
「メール送信」タグの認証で、種別を変えてテスト
CRAM-MD5またはDIGEST-MD5の時にOKとなる

不正中継のチェック(WEBサイトでチェック)

不正中継を行わないように設定されているかどうかを確認。 下記のサイトにアクセスして、使用しているSMTPサーバ名あるいはIPアドレスを指定。

スパムの踏み台対策 (メールの不正中継対策) のチェックをする

POPクライアント(qpopper)のインストール

$ sudo apt-get install qpopper

ユーザの追加

$ sudo useradd ユーザ名

ユーザの削除

$ sudo userdel ユーザ名

APOPの設定

最初の一回だけ以下を実行。

$ sudo popauth -init

APOPを利用するアカウントを登録

$ sudo popauth -user アカウント名
Adding only APOP password for アカウント名.
New password:          <== パスワード
Retype new password:   <== 再度パスワード

メールを受け取るユーザがAPOPのパスワードを設定をしていなければ、 
認証に失敗するように設定

$ sudo vi /etc/qpopper.conf
で、
set clear-text-password      = never
に変更

mbox型をMaildir型へ変更

$ sudo vi /etc/postfix/main.cf
#mailbox_command = procmail -a "$EXTENSION"
home_mailbox = Maildir/

$ sudo postfix check  で、チェックし何も表示されなければ
$ sudo /etc/init.d/postfix reload

mboxをMaildirに変換
$ sudo apt-get install mb2md
spool内のメールを変換(ユーザ毎に行う)
$ mb2md -m 
mboxを変換(ユーザ毎に行う)
$ mb2md -s mbox 
 
ユーザ追加時にMaildirを自動生成するように設定
$ sudo apt-get install maildrop
$ sudo maildirmake /etc/skel/Maildir

QpopperからCourier-popに変更(Maildir対応)
$ sudo apt-get remove qpopper
$ sudo apt-get install courier-pop

TLS/SSL設定への対応

postfix-tlsのインストール
$ sudo apt-get install postfix postfix-tls postfix-doc openssl

「Postfix Configuration」で、<Ok>を選択しenterキー
「Postfix Configuration」のGeneral type of configuration?
 で、Internet Siteを選択しenterキー
「Postfix Configuration」のWhere should mail for root go
 で、管理者用ユーザIDを入力しenterキー
「Postfix Configuration」のMail name? で、ドメイン名を入力しenterキー
「Postfix Configuration」のOther destinations to accept mail for? 
で、そのままenterキー
「Postfix Configuration」のForce synchronous updates on mail queue?
で、 <No>を選択しenterキー

ローカルから直接telnetでテストメールを送信
$ telnet localhost 25
Trying 127.0.0.1...
Connected to hoge02.
Escape character is '^]'.
220 hoge02 ESMTP Postfix (Debian/GNU)
helo localhost
250 hoge02
mail from: test@localhost.localdomain
250 Ok
rcpt to: hoge02@hogehoge.net
250 Ok
data
354 End data with <CR><LF>.<CR><LF>
test mail
aaaaaaaaa
.
250 Ok: queued as 812324101B6
quit
221 Bye
Connection closed by foreign host.
You have new mail in /var/mail/hoge02

送信結果の確認
$ mail
Mail version 8.1.2 01/15/2001.  Type ? for help.
"/var/mail/hoge02": 1 message 1 new
>N  1 test@localhost.lo  Wed Jan 31 04:52   15/503
& 1
Message 1:
From test@localhost.localdomain  Wed Jan 31 04:52:52 2007
X-Original-To: hoge02@hogehoge.net
Date: Wed, 31 Jan 2007 04:52:15 +0000 (UTC)
From: test@localhost.localdomain
To: undisclosed-recipients:;

test mail
aaaaaaaaa

& q
Saved 1 message in /home/hoge02/mbox

SASLでSMTP-AUTH (DIGEST-MD5 & CRAM-MD5)
$ sudo apt-get install libsasl2 libsasl2-modules sasl2-bin 

$ sudo vi /etc/postfix/main.cf
で、以下を追加
smtpd_sasl_auth_enable=yes
smtpd_sasl_local_domain=$myhostname
smtpd_recipient_restrictions=
  permit_mynetworks,
  permit_sasl_authenticated,
  reject_unauth_destination
smtpd_sasl_security_options=noanonymous

パスワードチェックの設定ファイル作成
$ sudo vi /etc/postfix/sasl/smtpd.conf 
で新規作成して、以下の設定を書く。
pwcheck_method: auxprop
mech_list: cram-md5 digest-md5

SASLパスワード設定
$ sudo saslpasswd2 -c -u ドメイン名 smtpユーザ名
設定ユーザ確認は、
$ sudo sasldblistusers2
設定削除
$ sudo saslpasswd2 -d -u ドメイン名 smtpユーザ名
パスワードデータベースsasldb2の設定
$ sudo chgrp postfix /etc/sasldb2
$ sudo chmod 640 /etc/sasldb2
$ sudo ln /etc/sasldb2 /var/spool/postfix/etc
$ sudo postfix check  で、チェックし何も表示されなければ
$ sudo /etc/init.d/postfix reload
SMTP-AUTH の確認
$ telnet localhost 25
Trying 127.0.0.1...
Connected to ホスト名.
Escape character is '^]'.
220 myhostname名 ESMTP Postfix (Debian/GNU)
ehlo localhost   <== 入力
250-myhostname名
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH DIGEST-MD5 CRAM-MD5
250 8BITMIME
quit   <== 入力
221 Bye
Connection closed by foreign host.
 
自己認証局(CA)の構築
(玄箱Apache2のSSL対応: https://kazz7.air-nifty.com/tekito/2005/04/apache2ssl_1068.html 参照)
(SSLサーバ証明書を作る https://raijin.ddo.jp/pukiwiki/pukiwiki.php?SSL%A5%B5%A1%BC%A5%D0%BE%DA%CC%C0%BD%F1%A4%F2%BA%EE%A4%EB 参照)
$ sudo mv /etc/ssl/openssl.cnf /etc/ssl/openssl.cnf.org
$ sudo cp /etc/ssl/openssl.cnf.org /etc/ssl/openssl.cnf
$ sudo emacs /etc/ssl/openssl.cnf

<変更前>
# nsCertType                    = server
<変更後>
nsCertType                    = server

<変更前>
# nsCertType = sslCA, emailCA
<変更後>
nsCertType = sslCA, emailCA

CA用秘密鍵(cakey.pem)とCA用証明書(cacert.pem)の作成
$ cd /usr/local/certs
$ sudo /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) [AU]:JP
State or Province Name (full name) [Some-State]:Saitama
Locality Name (eg, city) []:xxxxxxxx
Organization Name (eg, company) [Internet Widgits Pty Ltd]:hogehoge.net_CA
Organizational Unit Name (eg, section) []:Admin
Common Name (eg, YOUR name) []:hogehoge.net
Email Address []:root@hogehoge.net
 
ブラウザ配布用のファイル(ca.der)作成
$ sudo openssl x509 -inform pem -in ./demoCA/cacert.pem -outform der -out ./demoCA/ca.der

サーバ用秘密鍵の作成
$ sudo openssl genrsa -out server.key 1024

サーバ用公開鍵の作成
$ sudo openssl req -new -key server.key -out server.csr
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) [AU]:JP
State or Province Name (full name) [Some-State]:Saitama
Locality Name (eg, city) []:xxxxxx
Organization Name (eg, company) [Internet Widgits Pty Ltd]:xxx.co.jp
Organizational Unit Name (eg, section) []:Admin
Common Name (eg, YOUR name) []:hogehoge.net
Email Address []:root@hogehoge.net

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:  <== enterキー
An optional company name []:  <== enterキー

サーバ用証明書の作成
$ su
# echo 01 > ./demoCA/ca-cert.srl  <== シリアルナンバーファイルを作成
# exit
$ sudo openssl x509 -CA ./demoCA/cacert.pem -CAkey ./demoCA/private/cakey.pem -CAserial ./demoCA/ca-cert.srl -req -days 365 -in server.csr -out server.crt
Signature ok
subject=/C=JP/ST=Saitama/L=xxxxxx/O=hogehoge.net/OU=Admin/CN=hogehoge.net/emailAddress=root@hogehoge.net
Getting CA Private Key
Enter pass phrase for ./demoCA/private/cakey.pem:  <== CA用のパスフレーズを入力

$ ls -l /usr/local/certs
drwxr-xr-x  6 root root 4096 Jan 31 06:31 demoCA
-rw-r--r--  1 root root  977 Jan 31 06:36 server.crt <== サーバ用証明書
-rw-r--r--  1 root root  712 Jan 31 06:24 server.csr <== 証明書のリクエストファイル
-rw-r--r--  1 root root  887 Jan 31 06:21 server.key <== サーバ用秘密鍵

$ cd /usr/local/certs
$ sudo su
# cat server.key server.crt >> apache.pem  <== apcahe2,courier,imap用証明書
# exit
$ sudo cp apache.pem /etc/apache2/ssl

(Postfix + Courier-IMAP + OpenLDAPによるメールサーバ構築(Part.6)
https://park15.wakwak.com/~unixlife/practical/postfix2-7.html 参照)
$ sudo emacs /etc/postfix/master.cf
tlsmgr fifo - - n 300 1 tlsmgr  <== コメント解除
smtps inet n - n - - smtpd -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes  <== コメント解除

$ sudo emacs /etc/postfix/main.cf で、以下を追加
home_mailbox = Maildir/
smtpd_recipient_restrictions=
  permit_mynetworks
  permit_auth_destination
  reject
smtpd_tls_CAfile = /usr/local/certs/demoCA/cacert.pem
smtpd_tls_cert_file = /etc/apache2/ssl/apache.pem
smtpd_tls_key_file = /etc/apache2/ssl/apache.pem
smtpd_tls_session_cache_database = sdbm:/etc/postfix/smtpd_scache
smtpd_use_tls = yes

mboxをMaildirに変換
$ sudo apt-get install mb2md
spool内のメールを変換(ユーザ毎に行う)
$ mb2md -m 
mboxを変換(ユーザ毎に行う)
$ mb2md -s mbox 

ユーザ追加時にMaildirを自動生成するように設定
$ sudo apt-get install maildrop
$ sudo maildirmake /etc/skel/Maildir
 
$ sudo /etc/init.d/postfix reload
 
Courier-pop-sslのインストール
$ sudo apt-get install courier-pop-ssl
「Create directories for web-based administration ?」で、<No> を選択しenterキー
「SSL certificate required」で、enterキー

$ sudo emacs /etc/courier/pop3d-ssl
で
TLS_CERTFILE=/etc/courier/pop3d.pem
を
#TLS_CERTFILE=/etc/courier/pop3d.pem
TLS_CERTFILE=/etc/apache2/ssl/apache.pem
に変更

一旦停止してから起動
$ sudo /etc/init.d/courier-pop-ssl stop
$ sudo /etc/init.d/courier-authdaemon stop

Courier-popの自動起動をしない様にする
$ sudo update-rc.d -f courier-pop remove

$ sudo /etc/init.d/courier-pop-ssl start
$ sudo /etc/init.d/courier-authdaemon start

Thundebirdでアクセスすると表示される警告エラーの対応策は、
Thundebirdの[編集]-[設定]で「詳細」の「証明書を表示」をクリックし、
「証明書マネージャ」ウィンドウの「サイト証明書」タグをクリックし、「インポート」ボタンをクリック、
/usr/local/certs/server.crt を指定し、「開く」をクリック
用途が<不明>のサイトを選択し「設定」ボタンをクリック後、「この証明書が本物であると信用する」を選択する。

暗号化されているかの確認
$ sudo apt-get install ngrep
$ sudo ngrep | grep 本文の文字列
(例: $ sudo ngrep | grep hogehoge)
まずセキュリティ設定でTLSやSSLを指定しないで送信と受信を行うと
指定した「本文の文字列」を含んだ部分が表示される。
次に、セキュリティ設定でTLSやSSLを指定して送信と受信を行うと何も表示されない。 

Postfixのログを解析

$ sudo apt-get install pflogsumm

auth.logのエラー対応

Postfix / smtpd : sql_select option missing auxpropfunc error no mechanism available error and solution 参照

auth.logに下記エラーが表示されている場合は 

postfix/smtpd[16592]: sql_select option missing
postfix/smtpd[16592]: auxpropfunc error no mechanism available
postfix/smtpd[16592]: _sasl_plugin_load failed on sasl_auxprop_plug_init for plugin: sql

$ sudo apt-get remove libsasl2-modules-sql

スパム対策(postgreyのインストール)

SPAMメール対策ツールPostgrey(Postfix Greylisting Policy Server)」参照

$ sudo emacs /etc/apt/sources.list
で、
deb https://ftp.jp.debian.org/debian-volatile sarge/volatile main
を追加(etchの場合は、追加不要)
$ sudo apt-get update
$ sudo apt-get install postgrey

postfix側の設定
$ sudo emacs /etc/postfix/main.cf
で、smtpd_client_restrictionsとsmtpd_recipient_restrictionsに
check_policy_service inet:127.0.0.1:60000
を追加

$ sudo /etc/init.d/postfix reload

動作テスト

yahooメールなどから自ドメイン宛にテストメールを送信し

$ sudo tail /var/log/mail.log
で、
Apr 17 13:40:41 localhost postfix/smtpd[19084]: NOQUEUE: reject:
RCPT from smtp23.mail.bbt.yahoo.co.jp[202.93.85.138]: 450
<smtp23.mail.bbt.yahoo.co.jp[202.93.85.138]>: Client host
rejected: Greylisted for 300 seconds
(seehttps://isg.ee.ethz.ch/tools/postgrey/help/yyyy.html);
from=<zzzz@yahoo.co.jp> to=<xxx@yyyy>
proto=SMTP helo=<smtp23.mail.bbt.yahoo.co.jp>
Apr 17 13:40:41 localhost postfix/smtpd[19084]:
disconnect from smtp23.mail.bbt.yahoo.co.jp[202.93.85.138]
などの様に
450 <smtp23.mail.bbt.yahoo.co.jp[202.93.85.138]>: Client host rejected:
Greylisted for 300 seconds
が表示されている事を確認

また、自ドメインで受信したテストメールのヘッダーに
X-Greylist: delayed 399 seconds by postgrey-1.21 at xxxx; Tue, 17 Apr 2007 11:56:47 JST
の様に表示されている事を確認

$ sudo pflogsumm -d today /var/log/mail.info | less
で、
message reject detail
---------------------
  RCPT
    Client host rejected: Greylisted for 300 seconds (see
https://isg.ee.ethz.ch/tools/postgrey/help/xxxx.html)
(total: 1)
          1   yahoo.co.jp
の様に表示される

Postfixのログ解析結果をメール

$ crontab -e
で、
10 0 * * * /usr/sbin/pflogsumm -d yesterday /var/log/mail.log 2>&1 |/usr/bin/mailx -s "`uname -n` daily mail stats" postmaster
を追加

amavisd-new + clamavによるウィルスチェック

Debian Lennyにclamavを導入。EICARチェック」参照

Postfixの環境にClam AntiVirusを導入する」参照

Debian/Sarge でメールサーバ構築」参照 」参照

Debian 9 (Stretch) - アンチウィルスソフト導入!」参照

$ sudo hostname -f
hostname: Unknown host
と表示されたら
$ sudo hostname fqdn.hostname.example.com
でhostnameを設定
 
$ sudo apt-get install amavisd-new clamav clamav-daemon

$ sudo freshclam -d -c 24  <= ウィルスデータベースを1日24回更新

$ sudo emacs /etc/group
で、
clamav:x:110:
amavis:x:111:
を
clamav:x:110:amavis
amavis:x:111:clamav
に変更

$ sudo cp /usr/share/doc/amavisd-new/examples/amavisd.conf-sample.gz /etc/amavis/
$ sudo gunzip -d /etc/amavis/amavisd.conf-sample.gz
$ cd /etc/amavis
$ sudo mv amavisd.conf-sample amavisd.conf
$ sudo emacs /etc/amavis/amavisd.conf
で、以下を変更
$mydomain を自分のドメイン名に
$hdr_encoding と $bdy_encoding を 'iso-2022-jp' に
$undecipherable_subject_tag をコメントアウト

$ sudo emacs /etc/postfix/main.cf
で、以下を追加
content_filter = smtp:localhost:10024

$ sudo emacs /etc/postfix/master.cf
で、以下を追加
127.0.0.1:10025 inet n - n - - smtpd
 -o content_filter=
 -o local_recipient_maps=
 -o myhostname=localhost

$ sudo /etc/init.d/amavis restart
$ sudo /etc/init.d/postfix reload

送信先の受信メールヘッダに、amavis のレコードがある事を確認
また、自ドメインの受信メールヘッダに、amavis のレコードがある事を確認

ウイルスが正しく検出できるかをテストして見る
まず、
https://www.eicar.org/anti_virus_test_file.htm から
eicar.comという無害のウイルスをダウンロードして、メールに添付し送信
/var/log/mail.log を見ると

May  5 14:21:10 xxxx amavis[31044]: (31044-10) Blocked BANNED
(multipart/mixed | application/x-msdos-program,.asc,eicar.com), [aaa.bbb.ccc.ddd]  
<yyy@xxxx.net> -> <yyy@xxxx.net>, quarantine: banned-UyVRMnDyXGAb,
Message-ID: <481E9945.4060200@xxxx.net>, mail_id: UyVRMnDyXGAb, Hits: -, 391 ms

が記録され、
タイトル名 BANNED message from you (multipart/mixed | application/x-msdos-program,.asc,eicar.com)
のメールが送信されて来る


X-Virus-Scanned: ヘッダーから Debian の文字列を削除する方法

$ sudo emacs /etc/amavis/conf.d/20-debian_defaults

# Set to empty ("") to add no header
#$X_HEADER_LINE = "Debian $myproduct_name at $mydomain";
$X_HEADER_LINE = "$myproduct_name at $mydomain";

$ sudo systemctl restart amavis clamav-daemon
メールサーバ_postfix.txt · 最終更新: 2024/05/06 05:32 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki