| 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # This software is provided 'as-is', without any express or implied |
| 4 | # warranty. In no event will the author be held liable for any damages |
| 5 | # arising from the use of this software. |
| 6 | # |
| 7 | # Permission is granted to anyone to use this software for any purpose, |
| 8 | # including commercial applications, and to alter it and redistribute it |
| 9 | # freely, subject to the following restrictions: |
| 10 | # |
| 11 | # 1. The origin of this software must not be misrepresented; you must not |
| 12 | # claim that you wrote the original software. If you use this software |
| 13 | # in a product, an acknowledgment in the product documentation would be |
| 14 | # appreciated but is not required. |
| 15 | # 2. Altered source versions must be plainly marked as such, and must not be |
| 16 | # misrepresented as being the original software. |
| 17 | # 3. This notice may not be removed or altered from any source distribution. |
| 18 | # |
| 19 | # Copyright (c) 2008 Greg Hewgill http://hewgill.com |
| 20 | # |
| 21 | # This has been modified from the original software. |
| 22 | # Copyright (c) 2011 William Grant <[email protected]> |
| 23 | |
| 24 | from __future__ import print_function |
| 25 | |
| 26 | import sys |
| 27 | import argparse |
| 28 | |
| 29 | import dkim |
| 30 | |
| 31 | def main(): |
| 32 | parser = argparse.ArgumentParser( |
| 33 | description='Verify DKIM signature for email messages.', |
| 34 | epilog="message to be verified follows commands on stdin") |
| 35 | parser.add_argument('--index', metavar='N', type=int, default=0, |
| 36 | help='Index of DKIM signature header to verify: default=0') |
| 37 | args=parser.parse_args() |
| 38 | if sys.version_info[0] >= 3: |
| 39 | # Make sys.stdin a binary stream. |
| 40 | sys.stdin = sys.stdin.detach() |
| 41 | |
| 42 | message = sys.stdin.read() |
| 43 | verbose = '-v' in sys.argv |
| 44 | if verbose: |
| 45 | import logging |
| 46 | d = dkim.DKIM(message, logger=logging) |
| 47 | else: |
| 48 | d = dkim.DKIM(message) |
| 49 | res = d.verify(args.index) |
| 50 | if not res: |
| 51 | print("signature verification failed") |
| 52 | sys.exit(1) |
| 53 | print("signature ok") |
| 54 | |
| 55 | |
| 56 | if __name__ == "__main__": |
| 57 | main() |