ページへ戻る

− Links

 印刷 

XMail Server の SPF(Sender Policy Framework) 対応 :: XOOPS マニア

UsersWiki:nao-pon/blog/2008-04-09


2008 4月 9 (水)
 
ページ内コンテンツ
    • XMail Server の SPF(Sender Policy Framework) 対応
    • Mail::SPF::Query が必要です
    • フィルター定義
      • プログラムソース

XMail Server の SPF(Sender Policy Framework) 対応 anchor.png[5]

Tag: 自宅サーバー[6] SPF[7] メールサーバー[8] プログラミング[9]

xpWiki[10] のプラグイン moblog では、メールによってページを作成することができますが、認証判断は送信元メールアドレスのみで行っています。

できるだけ簡単に投稿できるようにしたかったので、敢えて Subject や本文にパスワードを入れたりする仕様にしなかったのですが、他人の成りすましによる偽投稿が可能なので、なんとか防がなければいけません。

その、動作検証するのに SPF 環境が必要なので、自宅サーバーの XMail Server (Win32) に SPF フィルターを導入しました。

調べてみると、XMail の Web サイトに、xm-spf.pl という perl のフィルターがありましたので、導入してみましたが上手く動きません。早速、ソースを眺めてみましたが、久々の perl なので、結構手間取りましたが、数箇所おかしなところがあったので、修正し、Mail Header に "Received-SPF:" を付け加えるフィルターも書いてみました。

Mail::SPF::Query のレスポンスが "fail" だった場合、XMail に終了コード(4)を返しますので、問答無用で受け取り拒否します。

Page Top

Mail::SPF::Query が必要です anchor.png[11]

サーバー機のコマンドプロンプトで

>cpan
>install Mail::SPF::Query

として、インストールして置いてください。

Page Top

フィルター定義 anchor.png[12]

  • SMTP RCPT 後処理 フィルター (Mail::SPF::Query を使用して判定します)
    • filespf_rcpt.pl[13]
    • フィルター定義
      起動コマンド最初の引数その他の引数
      C:\Perl\bin\perl.exe[絶対パス]/spf_rcpt.pl--file $(FILE) --tmp c:/winnt/temp
      C:\Perl\bin は環境に合わせる--tmp の引数は、システムのテンポラリディレクトリ
  • SMTPセッション終了後処理 (Mail Header に "Received-SPF: [判定結果]" を書き加えます。)
    • filespf_in.pl[14]
    • フィルター定義
      送信者アドレスあて先アドレスリモートIPローカルIP起動ファイル起動コマンドと引数
      **0.0.0.0/00.0.0.0/0spf_in.tab"C:\Perl\bin\perl.exe" "[絶対パス]/spf_in.pl" "$(FILE)" "c:/winnt/temp"
      C:\Perl\bin は環境に合わせる
      c:/winnt/temp は SMTP RCPT 後処理 フィルターと合わせる
    • SpamAssassin を利用しているなら、spf_in.tab を SpamAssassin の定義の前に持ってくると、SpamAssassin で Header 判定できるようになります。
Page Top

プログラムソース anchor.png[15]

filespf_rcpt.pl[16]
Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
-
|
!
-
|
!
!
 
-
|
!
 
-
|
|
|
|
-
|
|
!
-
!
|
|
-
!
-
!
|
-
!
|
|
-
!
-
|
|
-
|
!
!
 
-
|
!
 
 
-
|
-
-
|
!
|
!
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
-
-
|
|
|
|
!
!
!
 
-
|
|
!
 
 
#!/usr/bin/perl
#
# spf_rcpt.pl based on xm-spf.pl by nao-pon[3] (hypweb.net)
#
# xm-spf.pl by Davide Libenzi ( XMail Perl filter for SPF )
# Copyright (C) 2004  Davide Libenzi
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation[17]; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation[17], Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Davide Libenzi <davidel@xmailserver.org>
#
 
use strict;
use Mail::SPF::Query;
 
my $opt_file = undef;
my $opt_ip = undef;
my $opt_sender = undef;
my $opt_helo = undef;
my $opt_rcpt_to = undef;
my $opt_local = undef;
my $opt_trusted = undef;
my $opt_guess = undef;
my $opt_exp = undef;
my $opt_max_lookup = undef;
my $opt_sanitize = undef;
my $opt_name = "spfquery";
my $opt_debug = 0;
my $opt_keep_comments = 0;
my $opt_fallback = undef;
my $opt_override = undef;
my $opt_dns = undef;
my $opt_help = undef;
my $opt_pcode = 0;
my $opt_fcode = 3;
my @opt_bypass = ();
 
push(@opt_bypass, "^127\\.0\\.0\\.1\$");
my $mid;
my $opt_tmp = undef;
my $i;
 
for ($i = 0; $i <= $#ARGV; $i++) {
    if ($ARGV[$i] eq "--file") {
        if (++$i <= $#ARGV) {
            $opt_file = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--tmp") {
        if (++$i <= $#ARGV) {
            $opt_tmp = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--ip") {
        if (++$i <= $#ARGV) {
            $opt_ip = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--sender") {
        if (++$i <= $#ARGV) {
            $opt_sender = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--helo") {
        if (++$i <= $#ARGV) {
            $opt_helo = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--rcpt-to") {
        if (++$i <= $#ARGV) {
            $opt_rcpt_to = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--debug") {
        if (++$i <= $#ARGV) {
            $opt_debug = int($ARGV[$i]);
        }
    } elsif ($ARGV[$i] eq "--local") {
        if (++$i <= $#ARGV) {
            $opt_local = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--trusted") {
        if (++$i <= $#ARGV) {
            $opt_trusted = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--guess") {
        if (++$i <= $#ARGV) {
            $opt_guess = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--expl") {
        if (++$i <= $#ARGV) {
            $opt_exp = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--max-lookup") {
        if (++$i <= $#ARGV) {
            $opt_max_lookup = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--sanitize") {
        if (++$i <= $#ARGV) {
            $opt_sanitize = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--name") {
        if (++$i <= $#ARGV) {
            $opt_name = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--fallback") {
        if (++$i <= $#ARGV) {
            $opt_fallback = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--override") {
        if (++$i <= $#ARGV) {
            $opt_override = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--dns") {
        if (++$i <= $#ARGV) {
            $opt_dns = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--pcode") {
        if (++$i <= $#ARGV) {
            $opt_pcode = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--fcode") {
        if (++$i <= $#ARGV) {
            $opt_fcode = $ARGV[$i];
        }
    } elsif ($ARGV[$i] eq "--bypass") {
        if (++$i <= $#ARGV) {
            push(@opt_bypass, $ARGV[$i]);
        }
    } elsif ($ARGV[$i] eq "--help[18]") {
        $opt_help = 1;
    }
}
 
if ($opt_help) {
    exit($opt_pcode);
}
 
if (defined($opt_file)) {
 
    my $ln;
    my @hvars;
 
    if (!open(MFIL, "$opt_file")) {
        print STDERR "unable to open ${opt_file}\n";
        exit($opt_pcode);
    }
# Read message info
    $ln = <MFIL>;
    chomp($ln);
    @hvars = split(/;/, $ln);
# Discard SMTP domain
    $ln = <MFIL>;
# Discard message ID
    $mid = <MFIL>;
    chomp($mid);
# Read MAIL FROM
    $ln = <MFIL>;
    chomp($ln);
    close(MFIL);
# Handle both cases of early message file and spool file
    ($opt_sender = $ln) =~ s/[^<]+<([^>]*)>.*/$1/;
    if ($#hvars > 3) {
        $opt_helo = $hvars[0];
        ($opt_ip = $hvars[1]) =~ s/\[([^:]+)\]:[0-9]+/$1/;
    } else {
        ($opt_ip = $hvars[0]) =~ s/\[([^:]+)\]:[0-9]+/$1/;
    }
}
 
if (!defined($opt_helo) && ($opt_sender =~ /\@/)) {
    ($opt_helo = $opt_sender) =~ s/.*\@(.*)/$1/;
}
 
my $reg;
for ($i = 0; $i <= $#opt_bypass; $i++) {
    $reg = $opt_bypass[$i];
    if ($opt_ip =~ /$reg/) {
        if ($opt_debug) {
            print "whitelist ip ${opt_ip}\n";
        }
        exit($opt_pcode);
    }
}
 
my $query = new Mail::SPF::Query(ipv4       => $opt_ip,
                 sender     => $opt_sender,
                 helo       => $opt_helo,
                 local      => $opt_local,
                 trusted    => $opt_trusted,
                 guess      => $opt_guess,
                 default_explanation => $opt_exp,
                 max_lookup => $opt_max_lookup,
                 sanitize   => $opt_sanitize,
                 myhostname => $opt_name,
                 fallback   => $opt_fallback,
                 override   => $opt_override,
                 debug      => $opt_debug,
                 );
my ($result, $smtp_comment, $header_comment, $spf_record, $detail) = $query->result();
my $rcode = ($result eq "fail") ? $opt_fcode: $opt_pcode;
 
if (defined($opt_file)) {
    if ($rcode eq 0) {
        if (defined($opt_tmp) && $mid){
            open(FILE,">${opt_tmp}/${mid}.SPF");
                flock(FILE,2);
                print FILE "Received-SPF: ".$result." ( $header_comment )";
            close(FILE);
        }
    }
}
 
if ($opt_debug) {
    print $query->result . "\n";
    print $rcode;
}
 
exit($rcode);
filespf_in.pl[19]
Everything is expanded.Everything is shortened.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
-
|
|
|
|
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
-
|
!
-
|
|
|
|
|
|
|
|
|
|
!
|
!
 
 
 
 
 
 
 
#!/usr/bin/perl
#
# spf_in.pl by nao-pon[3] (hypweb.net)
# License: GPL
#
 
use strict;
 
my $file = $ARGV[0];
my $temp = $ARGV[1];
 
open(FILE, $file) || exit(0);
my @tmp = <FILE>;
close(FILE);
 
if (!@tmp) {exit(0);}
 
my @dat = ();
my $check = 0;
my $i = 0;
my $mid;
my $spf_file;
my $spf;
foreach(@tmp){
    $i++;
    chomp;
    if ($i eq 3) {
        $mid = $_;
    }
    if (!$check && /^<<MAIL\-DATA>>$/) {
        $check = 1;
        push(@dat, "$_\n");
        $spf_file = "${temp}/${mid}.SPF";
        open(FILE, $spf_file) || exit(0);
        $spf = <FILE>;
        close(FILE);
        unlink($spf_file);
        chomp($spf);
        push(@dat, "$spf\n");
        next;
    }
    push(@dat, "$_\n");
}
 
open(FILE,">$file");
    flock(FILE,2);
    print FILE @dat;
close(FILE);
 
exit(7);


Last-modified: 2016-06-24 (金) 11:07:42 (JST) (2863d) by nao-pon