procmail gpg——验证签名

procmail gpg——验证签名

有没有 procmail 的配方可以用来验证 pgp 签名?几乎就像 gpg --verify。我看过 pgpenvelope,但我不需要在任何类型的邮件客户端中检查它。

答案1

这个解决方案很有效。虽然有点旧,但没有任何问题。 https://www.w3.org/2004/07/pgp-whitelist.html

要求

要设置此白名单系统,您需要:

一个有效的 procmail 环境,设置为在与 procmail 相同的计算机上过滤您的传入电子邮件,这是 PGP 的一个实现;我们假设从现在开始它是 gnupg,但系统应该能够适应其他客户端以及您在 PGP 环境中导入的信任人员的密钥集;使用 gnupg,您可以使用 gpg --recv-key key_id 导入密钥,前提是您有一个配置好的 PGP 服务器来从中获取密钥设置

我们正在设置的系统执行以下操作:

对于收到的任何电子邮件,它都会检查发件人标题中给出的发件人是否在我们的受信任列表 (.pgp-whitelist) 中,如果是,它会检查邮件是否已签名,如果是,则检查签名是否正确 (使用 mailverify 脚本),如果是,它会在确保收到的邮件没有这样的标题后添加 X-Whitelist: Yes 标题。用于进行检查的 mailverify 脚本可以从 W3C CVS 公共服务器下载。如果您的 PGP 客户端不是以 gpg 形式调用的,则需要在此处更改调用。

.pgp-whitelist 假定位于您的主目录中;如果不是,请更改下面 PGP_WHITELIST 变量中的路径。其内容是您信任的电子邮件地址列表(每行一个),如果您收到正确签名的邮件,您希望将其列入白名单。

以下是要添加到您的 .procmailrc 配置文件的相关 procmail 规则;如果您也使用 procmail 进行识别垃圾邮件,则应该在设置之前添加它们。

# .procmailrc - Check whitelist 

PGP_WHITELIST=$HOME/.pgp-whitelist

#looking from spam, but blessing sender from my white list
# by setting a X-Whitelist header

# First, removing fake headers
:0 fwh
* ^X-Whitelist
| formail -IX-Whitelist

# checking for people with a trusted PGP key
FROM=`formail -XFrom: | formail -r -xTo: | tr -d ' '`
PGP_OK=`$HOME/mailverify 1>/dev/null && echo 1`
:0
* ? egrep -q "$FROM" $PGP_WHITELIST
* ? test -n "$PGP_OK"
{
   :0 fwh
   | formail -a"X-Whitelist: Yes"
   :0:
   $MAILDIR/good/
}

:0 fwh
* !^X-Whitelist: Yes
{
        :0:
        $MAILDIR/bounce/
}

Perl 脚本即 mailverify。相应地调整 MAILDIR 文件夹。

#!/usr/bin/env perl
# File Name: mailverify
# Maintainer: Moshe Kaminsky <[email protected]>
# Original Date: September 30, 2003
# Last Update: September 30, 2003
# http://mirror.hamakor.org.il/archives/linux-il/att-5615/mailverify
###########################################################

use warnings;
use integer;

BEGIN {
    our $VERSION = 0.1;

    # analyze command line
    use Getopt::Long qw(:config gnu_getopt);
    use Pod::Usage;

    our $opt_help;
    our $opt_man;
    our $opt_version;
    our $Gpg;
    our $Tolerant;
    GetOptions('gpg=s' => \$Gpg,
               'tolerant!' => \$Tolerant,
               'help', 'version', 'man');
    pod2usage(1) if $opt_help;
    pod2usage(-verbose => 2) if $opt_man;
    print "$0 version $VERSION\n" and exit if $opt_version;
    $Gpg = '/usr/bin/gpg --batch --verify' unless $Gpg;
}

use File::Temp qw( tempfile );

my $PrevField = '';

# process the header
while (<>) {
    next if /^From /o;
    last if /^$/o;
    if (/^([\w-]+): (.*)$/o) {
        $Header{$1} = $2;
        $PrevField = $1;
    } else {
        $Header{$PrevField} .= $_;
    }
}

# check that the message is signed
$Str = $Header{'Content-Type'};
@Parts = split /;\s+/, $Str if $Str;
if (not $Str or $Parts[0] ne 'multipart/signed') {
    # the message is not multipart/signed, but might still be cleartext 
    # signed. Depending on --tolerant, we may pass the rest of the message to 
    # gpg directly
    print "Message not signed\n" and exit -1 unless $Tolerant;
    open GPG, "|$Gpg" or die "Can't open pipe to gpg ($Gpg): $!";
    print GPG <>;
    close GPG;
    exit $? >> 8;
}

# the boundary string signals the boundary between two attachments
$Boundary = $1 if $Parts[3] =~ /^boundary="(.*)"$/o;
# go to the start of the message
while (<>) {
    last if $_ eq "--$Boundary\n";
}

# read the message, excluding the last (empty) line
while (<>) {
    last if $_ eq "--$Boundary\n";
    push @Message, $_;
}
pop @Message;
# read the sig
while (<>) {
    last if /^-----BEGIN PGP SIGNATURE-----$/o;
}
{
do {
    push @Sig, $_;
    last if /^-----END PGP SIGNATURE-----$/o;
} while (<>);
};

# here comes the funny part: replace \n by \r\n
$_ = join '', @Message;
s/(?<!\r)\n/\r\n/g;

# write everything to files
my ($MsgFH, $MsgFile) = tempfile;
print $MsgFH $_;
my $SigFile = "$MsgFile.asc";
open SIGFH, ">$SigFile" or die "can't open $SigFile: $!";
print SIGFH @Sig;
close $MsgFH;
close SIGFH;

# run gpg
print `$Gpg $SigFile`;

# clean up
unlink $MsgFile, $SigFile;

# exit with the status of gpg
exit $? >> 8;

__DATA__

# start of POD

=head1 NAME

mailverify - verify the pgp signature of a mime signed mail message

=head1 SYNOPSIS

B<mailverify> B<--help>|B<--man>|B<--version>

B<mailverify> [B<--gpg=I<gpg command>>] [B<--(no)tolerant>] [I<mail file>]

=head1 OPTIONS

=over 4

=item B<--gpg=I<gpg command>>

The command to run to do the actual checking. The default is 
S<C</usr/local/bin/gpg --batch --verify>>. It is called with one argument, 
which is the name of the file containing the signature. If B<--tolerant> is 
used, it may also be called with the whole message on the standard input.

=item B<--(no)tolerant>

Normally (with B<--notolerant>), if the Content-Type is not 
C<multipart/signed>, B<mailverify> decides that the message is not signed, 
and exits with status -1. With this switch, the message is passed to I<gpg> 
(or whatever was specified with the B<--gpg> option) as is. This way, 
clearsigned messages can be verified with the same command.

=item B<--help>

Give a short usage message and exit with status 1

=item B<--man>

Give the full description and exit with status 1

=item B<--version>

Print a line with the program name and exit with status 0

=back

=head1 ARGUMENTS

If an argument is given, it is treated a file containing an e-mail message to 
verify, but more common is to read the message from stdin.

=head1 DESCRIPTION

This script verifies the pgp signature of files whose signature appears as an 
attachment (Content-Type C<multipart/signed>). If B<--tolerant> is used, it 
will also verify clearsigned messages. The usage is basically to pipe the 
mail to this program.

=head1 EXIT STATUS

If the message is not of type C<multipart/signed>, and B<--tolerant> is not 
given, the exit status is -1. In any other case, it is the exit status of the 
I<gpg> command.

=head1 SEE ALSO

I<gpg(1)>

=head1 BUGS

Probably plenty, since I don't read any RFCs. Works with all cases I checked, 
though.

=head1 AUTHOR

Moshe Kaminsky <[email protected]> - Copyright (c) 2003

=head1 LICENSE

This program is free software. You may copy or 
redistribute it under the same terms as Perl itself.

=cut

向 Moshe 致敬。

相关内容