SHARE
TWEET

Untitled

a guest Sep 8th, 2011 872 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. # Copyright (c) 2011, Stephan Chenette and David Saunders
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without modification,
  6. # are permitted provided that the following conditions are met:
  7. #
  8. #   * Redistributions of source code must retain the above copyright notice,
  9. #    this list of conditions and the following disclaimer.
  10. #   * Redistributions in binary form must reproduce the above copyright notice,
  11. #    this list of conditions and the following disclaimer in the documentation
  12. #    and/or other materials provided with the distribution.
  13. #
  14. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. # IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  18. # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  19. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  22. # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. # THE POSSIBILITY OF SUCH DAMAGE.
  24.  
  25. use warnings;
  26. use strict;
  27.  
  28. use Mail::DKIM::Verifier;
  29.  
  30. my $DEBUG = 1;
  31.  
  32. my $file = $ARGV[0];
  33.  
  34. # copy the original email to a file, remove all headers above the DKIM signature
  35. if ( !defined $file ) {
  36.     print STDERR "Usage: $0 FILE\n";
  37.     exit 1;
  38. }
  39.  
  40. print "file: " . $file . "\n" if ($DEBUG);
  41.  
  42. open( my $fd, "<", $file );
  43. my @text = <$fd>;
  44. close $fd;
  45.  
  46. if( validate_dkim(\@text) ) {
  47.     printf "$ARGV[0] passed DKIM validation\n";
  48. } else {
  49.     printf "$ARGV[0] failed DKIM validation\n";
  50. }
  51.  
  52. #############################################################################
  53. # Validate DKIM Header
  54. # Returns 1 if valid, otherwise 0
  55. #############################################################################
  56. sub validate_dkim {
  57.  
  58.     my ($text_arr_ref) = @_;
  59.  
  60.     my $dkim = Mail::DKIM::Verifier->new();
  61.  
  62.     my @lines = @{$text_arr_ref};
  63.  
  64.     my $text = join( q{}, @lines );
  65.  
  66.     my $dkim_line;
  67.     foreach my $line (@lines) {
  68.  
  69.         # remove local line terminators
  70.         chomp $line;
  71.         $line =~ s/\015$//;
  72.  
  73.         if($line =~ /^DKIM/) {
  74.             $dkim_line = $line;
  75.         }
  76.  
  77.         # use SMTP line terminators
  78.         $dkim->PRINT("$line\015\012");
  79.     }
  80.  
  81.     $dkim->CLOSE;
  82.  
  83.     my $result = $dkim->result;
  84.  
  85.     print $dkim->result_detail."\n" if ($DEBUG || $result ne "pass");
  86.  
  87.     if($result eq "pass") {
  88.         return 1;
  89.     } else {
  90.         print "$result\n";
  91.     }
  92.  
  93.     return 0;
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top