From 7df236127bda342dfe055a2a18d2029534a7eb3d Mon Sep 17 00:00:00 2001 From: "Stuart D. Gathman" Date: Sat, 4 Jul 2020 22:29:28 -0400 Subject: [PATCH] Add sendmail style MTA policy query module --- Milter/policy.py | 2 +- mime.py | 84 ------------------------------------------------ test.py | 2 ++ test/access | 9 ++++++ testmime.py | 31 ++---------------- testpolicy.py | 51 +++++++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 113 deletions(-) create mode 100644 test/access create mode 100644 testpolicy.py diff --git a/Milter/policy.py b/Milter/policy.py index 188a22d..25ed5db 100644 --- a/Milter/policy.py +++ b/Milter/policy.py @@ -42,7 +42,7 @@ class MTAPolicy(object): self.acf = dbmopen(self.access_file,'r') except: print('%s: Cannot open for reading'%self.access_file) - syslog.syslog('%s: Cannot open for reading'%self.access_file) + raise return self def __exit__(self,t,v,b): self.close() diff --git a/mime.py b/mime.py index 7a19059..2b1f6cc 100644 --- a/mime.py +++ b/mime.py @@ -1,87 +1,3 @@ -# $Log$ -# Revision 1.8 2011/11/05 15:51:03 customdesigned -# New example -# -# Revision 1.7 2009/06/13 21:15:12 customdesigned -# Doxygen updates. -# -# Revision 1.6 2009/06/09 03:13:13 customdesigned -# More doxygen docs. -# -# Revision 1.5 2005/07/20 14:49:43 customdesigned -# Handle corrupt and empty ZIP files. -# -# Revision 1.4 2005/06/17 01:49:39 customdesigned -# Handle zip within zip. -# -# Revision 1.3 2005/06/02 15:00:17 customdesigned -# Configure banned extensions. Scan zipfile option with test case. -# -# Revision 1.2 2005/06/02 04:18:55 customdesigned -# Update copyright notices after reading article on /. -# -# Revision 1.1.1.4 2005/05/31 18:23:49 customdesigned -# Development changes since 0.7.2 -# -# Revision 1.62 2005/02/14 22:31:17 stuart -# _parseparam replacement not needed for python2.4 -# -# Revision 1.61 2005/02/12 02:11:11 stuart -# Pass unit tests with python2.4. -# -# Revision 1.60 2005/02/11 18:34:14 stuart -# Handle garbage after quote in boundary. -# -# Revision 1.59 2005/02/10 01:10:59 stuart -# Fixed MimeMessage.ismodified() -# -# Revision 1.58 2005/02/10 00:56:49 stuart -# Runs with python2.4. Defang not working correctly - more work needed. -# -# Revision 1.57 2004/11/20 16:37:52 stuart -# fix regex for splitting header and body -# -# Revision 1.56 2004/11/09 20:33:51 stuart -# Recognize more dynamic PTR variations. -# -# Revision 1.55 2004/10/06 21:39:20 stuart -# Handle message attachments with boundary errors by not parsing them -# until needed. -# -# Revision 1.54 2004/08/18 01:59:46 stuart -# Handle mislabeled multipart messages -# -# Revision 1.53 2004/04/24 22:53:20 stuart -# Rename some local variables to avoid shadowing builtins -# -# Revision 1.52 2004/04/24 22:47:13 stuart -# Convert header values to str -# -# Revision 1.51 2004/03/25 03:19:10 stuart -# Correctly defang rfc822 attachments when boundary specified with -# content-type message/rfc822. -# -# Revision 1.50 2003/10/15 22:01:00 stuart -# Test for and work around email bug with encoded filenames. -# -# Revision 1.49 2003/09/04 18:48:13 stuart -# Support python-2.2.3 -# -# Revision 1.48 2003/09/02 00:27:27 stuart -# Should have full milter based dspam support working -# -# Revision 1.47 2003/08/26 06:08:18 stuart -# Use new python boolean since we now require 2.2.2 -# -# Revision 1.46 2003/08/26 05:01:38 stuart -# Release 0.6.0 -# -# Revision 1.45 2003/08/26 04:01:24 stuart -# Use new email module for parsing mail. Still need mime module to -# provide various bug fixes to email module, and maintain some compatibility -# with old milter code. -# - ## @package mime # This module provides a "defang" function to replace naughty attachments. # diff --git a/test.py b/test.py index 96c608c..b81f4db 100644 --- a/test.py +++ b/test.py @@ -4,6 +4,7 @@ import testsample import testutils import testgrey import testcfg +import testpolicy import os def suite(): @@ -13,6 +14,7 @@ def suite(): s.addTest(testutils.suite()) s.addTest(testgrey.suite()) s.addTest(testcfg.suite()) + s.addTest(testpolicy.suite()) return s if __name__ == '__main__': diff --git a/test/access b/test/access new file mode 100644 index 0000000..ac79cf8 --- /dev/null +++ b/test/access @@ -0,0 +1,9 @@ +SPF-Pass:example.com OK +SPF-Neutral:example.com REJECT +HELO-Neutral:example.com OK +SPF-Permerror:foo@bad.example.com OK +SPF-Permerror: REJECT +SMTP-Auth:good@example.com OK +SMTP-Auth:example.com REJECT +SMTP-Auth:bad@localhost.localdomain REJECT +SMTP-Test: REJECT diff --git a/testmime.py b/testmime.py index 56c041b..3b1f236 100644 --- a/testmime.py +++ b/testmime.py @@ -1,31 +1,6 @@ -# $Log$ -# Revision 1.5 2011/06/09 17:27:42 customdesigned -# Documentation updates. -# -# Revision 1.4 2005/07/20 14:49:44 customdesigned -# Handle corrupt and empty ZIP files. -# -# Revision 1.3 2005/06/17 01:49:39 customdesigned -# Handle zip within zip. -# -# Revision 1.2 2005/06/02 15:00:17 customdesigned -# Configure banned extensions. Scan zipfile option with test case. -# -# Revision 1.1.1.2 2005/05/31 18:23:49 customdesigned -# Development changes since 0.7.2 -# -# Revision 1.23 2005/02/11 18:34:14 stuart -# Handle garbage after quote in boundary. -# -# Revision 1.22 2005/02/10 01:10:59 stuart -# Fixed MimeMessage.ismodified() -# -# Revision 1.21 2005/02/10 00:56:49 stuart -# Runs with python2.4. Defang not working correctly - more work needed. -# -# Revision 1.20 2004/11/20 16:38:17 stuart -# Add rcs log -# +# @author Stuart D. Gathman +# Copyright 2005,2009,2020 Business Management Systems, Inc. +# This code is under the GNU General Public License. See COPYING for details. from __future__ import print_function import unittest import mime diff --git a/testpolicy.py b/testpolicy.py new file mode 100644 index 0000000..42b6b20 --- /dev/null +++ b/testpolicy.py @@ -0,0 +1,51 @@ +from __future__ import print_function +import unittest +import sys +import os +from Milter.policy import MTAPolicy + +class Config(object): + def __init__(self): + self.access_file='test/access.db' + self.access_file_nulls=True + +class PolicyTestCase(unittest.TestCase): + + def setUp(self): + self.config = Config() + if os.access('test/access',os.R_OK): + if not os.path.exists('test/access.db') or \ + os.path.getmtime('test/access') > os.path.getmtime('test/access.db'): + cmd = 'tr : ! = 2: + e,k = a[:2] + with MTAPolicy(e,conf=Config()) as p: + pol = p.getPolicy(k) + print(pol) + a = a[2:]