PYTHON   18

checkdb py

Guest on 18th August 2022 01:12:04 AM

  1. #!/usr/bin/env python
  2. #
  3. # checkdbl.py - TCP lookup table for Postfix header_checks
  4. #
  5. # This script implements the same functionality as the Perl
  6. # version available here:
  7. #
  8. #       http://people.freebsd.org/~sahil/scripts/checkdbl.pl.txt
  9. #
  10. # To integrate in $config_directory/master.cf:
  11. #
  12. #  127.0.0.1:2526 inet  n       n       n       -       0      spawn
  13. #    user=nobody argv=/path/to/checkdbl.py
  14. #
  15. # And in $config_directory/main.cf:
  16. #
  17. #  header_checks = tcp:[127.0.0.1]:2526
  18. #  127.0.0.1:2526_time_limit = 3600s
  19. #
  20. # UPDATED: 2011-09-05
  21.  
  22. import os, re, socket, sys
  23.  
  24. #
  25. # Define the headers and DBL zones we care about, and
  26. # compile a repeatedly used regular expression.
  27. #
  28. headers = ['from', 'message-id', 'reply-to']
  29. zones   = ['dbl.spamhaus.org', 'multi.surbl.org', 'black.uribl.com']
  30. regx    = re.compile(r'([\w.-]+\.[^\W\d_]+)', re.IGNORECASE)
  31.  
  32. #
  33. # Autoflush the standard output stream.
  34. #
  35. sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
  36.  
  37. #
  38. # Verify that we have a domain, then query DBLs until
  39. # we get a hit.  Do not waste time checking additional
  40. # DBLs if a query suceeds.
  41. #
  42. def querybl(domain):
  43.         m = regx.search(domain)
  44.         if m is not None:
  45.                 domain = m.group(0)
  46.                 for zone in zones:
  47.                         try:
  48.                                 if socket.gethostbyname(domain + '.' + zone).startswith('127'):
  49.                                         return (domain, zone)
  50.                         except:
  51.                                 pass
  52.                        
  53. #
  54. # Parse input from Postfix, and do some basic sanity
  55. # checks; then, send the domain to query function.
  56. #
  57. def parse(request):
  58.         if request.startswith('get'):
  59.                 header = request.lstrip('get ').lower()
  60.                 if header.startswith(tuple(headers)):
  61.                         hsplit = header.rsplit('@',1)
  62.                         if len(hsplit) == 2 and hsplit[1] != '':
  63.                                 return querybl(hsplit[1])
  64.  
  65. while True:
  66.         rval = parse(raw_input())
  67.         try:
  68.                 print('200 REJECT ' + rval[0] + ' listed in ' + rval[1])
  69.         except:
  70.                 print('200 DUNNO')

Raw Paste


Login or Register to edit or fork this paste. It's free.