Compare commits
44 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1b5db35ace | |||
| f357be1e99 | |||
| 84eeecf9a6 | |||
| a180b212c6 | |||
| bd0df5d77a | |||
| 34746823f7 | |||
| baeddd9fa5 | |||
| 4854f95b59 | |||
| 242f2fa78f | |||
| 1e0324399b | |||
| 078d9f2078 | |||
| ff06b5f1b4 | |||
| dd581f5d9a | |||
| 3fb9beb5c0 | |||
| b12c4c9746 | |||
| f3fbb1c99d | |||
| 27887daf3f | |||
| 23defb880b | |||
| 7502c29e47 | |||
| 594d3ad365 | |||
| b2e0b2ebc6 | |||
| 04a241f1e9 | |||
| 16bfe5d4da | |||
| 70d19001c0 | |||
| 0d001dd8e9 | |||
| 8f4a82794c | |||
| de0ec3430d | |||
| c9e32e4b06 | |||
| 83a1762515 | |||
| feb6526cb8 | |||
| 3a3add814e | |||
| 1ba522e501 | |||
| a43649f2ce | |||
| de679b1514 | |||
| b946759857 | |||
| f6702e39dd | |||
| 5a8aaf85d7 | |||
| 720db3d7bd | |||
| a46627959c | |||
| 4e0d3da07d | |||
| 53c7519922 | |||
| b3d6328167 | |||
| 2133942c19 | |||
| eef3cde27e |
@@ -31,7 +31,7 @@ PROJECT_NAME = pymilter
|
|||||||
# This could be handy for archiving the generated documentation or
|
# This could be handy for archiving the generated documentation or
|
||||||
# if some version control system is used.
|
# if some version control system is used.
|
||||||
|
|
||||||
PROJECT_NUMBER = 0.9.3
|
PROJECT_NUMBER = 0.9.6
|
||||||
|
|
||||||
# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute)
|
# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute)
|
||||||
# base path where the generated documentation will be put.
|
# base path where the generated documentation will be put.
|
||||||
@@ -814,7 +814,7 @@ DOCSET_FEEDNAME = "Doxygen generated docs"
|
|||||||
# reverse domain-name style string, e.g. com.mycompany.MyDocSet. Doxygen
|
# reverse domain-name style string, e.g. com.mycompany.MyDocSet. Doxygen
|
||||||
# will append .docset to the name.
|
# will append .docset to the name.
|
||||||
|
|
||||||
DOCSET_BUNDLE_ID = org.doxygen.Project
|
DOCSET_BUNDLE_ID = com.bmsi.pymilter
|
||||||
|
|
||||||
# If the GENERATE_HTMLHELP tag is set to YES, additional index files
|
# If the GENERATE_HTMLHELP tag is set to YES, additional index files
|
||||||
# will be generated that can be used as input for tools like the
|
# will be generated that can be used as input for tools like the
|
||||||
|
|||||||
+254
-61
@@ -8,19 +8,27 @@
|
|||||||
# Copyright 2001,2009 Business Management Systems, Inc.
|
# Copyright 2001,2009 Business Management Systems, Inc.
|
||||||
# This code is under the GNU General Public License. See COPYING for details.
|
# This code is under the GNU General Public License. See COPYING for details.
|
||||||
|
|
||||||
__version__ = '0.9.3'
|
__version__ = '0.9.8'
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import milter
|
import milter
|
||||||
import thread
|
import thread
|
||||||
|
|
||||||
from milter import *
|
from milter import *
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
_seq_lock = thread.allocate_lock()
|
_seq_lock = thread.allocate_lock()
|
||||||
_seq = 0
|
_seq = 0
|
||||||
|
|
||||||
|
## @fn set_flags(flags)
|
||||||
|
# @brief Enable optional %milter actions.
|
||||||
|
# Certain %milter actions need to be enabled before calling milter.runmilter()
|
||||||
|
# or they throw an exception.
|
||||||
|
# @param flags Bit ored mask of optional actions to enable
|
||||||
|
|
||||||
def uniqueID():
|
def uniqueID():
|
||||||
"""Return a sequence number unique to this process.
|
"""Return a unique sequence number (incremented on each call).
|
||||||
"""
|
"""
|
||||||
global _seq
|
global _seq
|
||||||
_seq_lock.acquire()
|
_seq_lock.acquire()
|
||||||
@@ -28,6 +36,7 @@ def uniqueID():
|
|||||||
_seq_lock.release()
|
_seq_lock.release()
|
||||||
return seqno
|
return seqno
|
||||||
|
|
||||||
|
## @private
|
||||||
OPTIONAL_CALLBACKS = {
|
OPTIONAL_CALLBACKS = {
|
||||||
'connect':(P_NR_CONN,P_NOCONNECT),
|
'connect':(P_NR_CONN,P_NOCONNECT),
|
||||||
'hello':(P_NR_HELO,P_NOHELO),
|
'hello':(P_NR_HELO,P_NOHELO),
|
||||||
@@ -40,6 +49,10 @@ OPTIONAL_CALLBACKS = {
|
|||||||
'header':(P_NR_HDR,P_NOHDRS)
|
'header':(P_NR_HDR,P_NOHDRS)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## @private
|
||||||
|
R = re.compile(r'%+')
|
||||||
|
|
||||||
|
## @private
|
||||||
def decode_mask(bits,names):
|
def decode_mask(bits,names):
|
||||||
t = [ (s,getattr(milter,s)) for s in names]
|
t = [ (s,getattr(milter,s)) for s in names]
|
||||||
nms = [s for s,m in t if bits & m]
|
nms = [s for s,m in t if bits & m]
|
||||||
@@ -49,16 +62,17 @@ def decode_mask(bits,names):
|
|||||||
|
|
||||||
## Class decorator to enable optional protocol steps.
|
## Class decorator to enable optional protocol steps.
|
||||||
# P_SKIP is enabled by default when supported, but
|
# P_SKIP is enabled by default when supported, but
|
||||||
# milter applications may wish to enable P_HDR_LEADSPC
|
# applications may wish to enable P_HDR_LEADSPC
|
||||||
# to send and receive the leading space of header continuation
|
# to send and receive the leading space of header continuation
|
||||||
# lines unchanged, and/or P_RCPT_REJ to have recipients
|
# lines unchanged, and/or P_RCPT_REJ to have recipients
|
||||||
# detected as invalid by the MTA passed to the envcrpt callback.
|
# detected as invalid by the MTA passed to the envcrpt callback.
|
||||||
#
|
#
|
||||||
# Applications may want to check whether the protocol is actually
|
# Applications may want to check whether the protocol is actually
|
||||||
# supported by the MTA in use. The <code>_protocol</code>
|
# supported by the MTA in use. Base._protocol
|
||||||
# member is a bitmask of protocol options negotiated. So,
|
# is a bitmask of protocol options negotiated. So,
|
||||||
# for instance, if <code>self._protocol & Milter.P_RCPT_REJ</code>
|
# for instance, if <code>self._protocol & Milter.P_RCPT_REJ</code>
|
||||||
# is true, then that feature was successfully negotiated with the MTA.
|
# is true, then that feature was successfully negotiated with the MTA
|
||||||
|
# and the application will see recipients the MTA has flagged as invalid.
|
||||||
#
|
#
|
||||||
# Sample use:
|
# Sample use:
|
||||||
# <pre>
|
# <pre>
|
||||||
@@ -68,21 +82,59 @@ def decode_mask(bits,names):
|
|||||||
# myMilter = Milter.enable_protocols(myMilter,Milter.P_RCPT_REJ)
|
# myMilter = Milter.enable_protocols(myMilter,Milter.P_RCPT_REJ)
|
||||||
# </pre>
|
# </pre>
|
||||||
# @since 0.9.3
|
# @since 0.9.3
|
||||||
# @param klass the milter application class to modify
|
# @param klass the %milter application class to modify
|
||||||
# @param mask a bitmask of protocol steps to enable
|
# @param mask a bitmask of protocol steps to enable
|
||||||
# @return the modified milter class
|
# @return the modified %milter class
|
||||||
def enable_protocols(klass,mask):
|
def enable_protocols(klass,mask):
|
||||||
klass._protocol_mask = klass.protocol_mask() & ~mask
|
klass._protocol_mask = klass.protocol_mask() & ~mask
|
||||||
return klass
|
return klass
|
||||||
|
|
||||||
|
## Milter rejected recipients. A class decorator that calls
|
||||||
|
# enable_protocols() with the P_RCPT_REJ flag. By default, the MTA
|
||||||
|
# does not pass recipients that it knows are invalid on to the milter.
|
||||||
|
# This decorator enables a %milter app to see all recipients if supported
|
||||||
|
# by the MTA. Use like this with python-2.6 and later:
|
||||||
|
# <pre>
|
||||||
|
# @@Milter.rejected_recipients
|
||||||
|
# class myMilter(Milter.Base):
|
||||||
|
# def envrcpt(self,to,*params):
|
||||||
|
# return Milter.CONTINUE
|
||||||
|
# </pre>
|
||||||
|
# @since 0.9.5
|
||||||
|
# @param klass the %milter application class to modify
|
||||||
|
# @return the modified %milter class
|
||||||
|
def rejected_recipients(klass):
|
||||||
|
return enable_protocols(klass,P_RCPT_REJ)
|
||||||
|
|
||||||
|
## Milter leading space on headers. A class decorator that calls
|
||||||
|
# enable_protocols() with the P_HEAD_LEADSPC flag. By default,
|
||||||
|
# header continuation lines are collected and joined before getting
|
||||||
|
# sent to a milter. Headers modified or added by the milter are
|
||||||
|
# folded by the MTA as necessary according to its own standards.
|
||||||
|
# With this flag, header continuation lines are preserved
|
||||||
|
# with their newlines and leading space. In addition, header folding
|
||||||
|
# done by the milter is preserved as well.
|
||||||
|
# Use like this with python-2.6 and later:
|
||||||
|
# <pre>
|
||||||
|
# @@Milter.header_leading_space
|
||||||
|
# class myMilter(Milter.Base):
|
||||||
|
# def header(self,hname,value):
|
||||||
|
# return Milter.CONTINUE
|
||||||
|
# </pre>
|
||||||
|
# @since 0.9.5
|
||||||
|
# @param klass the %milter application class to modify
|
||||||
|
# @return the modified %milter class
|
||||||
|
def header_leading_space(klass):
|
||||||
|
return enable_protocols(klass,P_HEAD_LEADSPC)
|
||||||
|
|
||||||
## Function decorator to disable callback methods.
|
## Function decorator to disable callback methods.
|
||||||
# If the MTA supports it, tells the MTA not to call this callback,
|
# If the MTA supports it, tells the MTA not to invoke this callback,
|
||||||
# increasing efficiency. All the callbacks (except negotiate)
|
# increasing efficiency. All the callbacks (except negotiate)
|
||||||
# are disabled in Milter.Base, and overriding them reenables the
|
# are disabled in Milter.Base, and overriding them reenables the
|
||||||
# callback. An application may need to use @@nocallback when it extends
|
# callback. An application may need to use @@nocallback when it extends
|
||||||
# another milter and wants to disable a callback again.
|
# another %milter and wants to disable a callback again.
|
||||||
# The disabled method should still return Milter.CONTINUE, in case the MTA does
|
# The disabled method should still return Milter.CONTINUE, in case the MTA does
|
||||||
# not support protocol negotiation.
|
# not support protocol negotiation, and for when called from a test harness.
|
||||||
# @since 0.9.2
|
# @since 0.9.2
|
||||||
def nocallback(func):
|
def nocallback(func):
|
||||||
try:
|
try:
|
||||||
@@ -90,7 +142,12 @@ def nocallback(func):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
'@nocallback applied to non-optional method: '+func.__name__)
|
'@nocallback applied to non-optional method: '+func.__name__)
|
||||||
return func
|
def wrapper(self,*args):
|
||||||
|
if func(self,*args) != CONTINUE:
|
||||||
|
raise RuntimeError('%s return code must be CONTINUE with @nocallback'
|
||||||
|
% func.__name__)
|
||||||
|
return CONTINUE
|
||||||
|
return wrapper
|
||||||
|
|
||||||
## Function decorator to disable callback reply.
|
## Function decorator to disable callback reply.
|
||||||
# If the MTA supports it, tells the MTA not to wait for a reply from
|
# If the MTA supports it, tells the MTA not to wait for a reply from
|
||||||
@@ -105,9 +162,14 @@ def noreply(func):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
'@noreply applied to non-optional method: '+func.__name__)
|
'@noreply applied to non-optional method: '+func.__name__)
|
||||||
|
@wraps(func)
|
||||||
def wrapper(self,*args):
|
def wrapper(self,*args):
|
||||||
rc = func(self,*args)
|
rc = func(self,*args)
|
||||||
if self._protocol & nr_mask: return NOREPLY
|
if self._protocol & nr_mask:
|
||||||
|
if rc != CONTINUE:
|
||||||
|
raise RuntimeError('%s return code must be CONTINUE with @noreply'
|
||||||
|
% func.__name__)
|
||||||
|
return NOREPLY
|
||||||
return rc
|
return rc
|
||||||
wrapper.milter_protocol = nr_mask
|
wrapper.milter_protocol = nr_mask
|
||||||
return wrapper
|
return wrapper
|
||||||
@@ -122,49 +184,87 @@ def noreply(func):
|
|||||||
class DisabledAction(RuntimeError):
|
class DisabledAction(RuntimeError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
## A do "nothing" Milter base class.
|
## A do "nothing" Milter base class representing an SMTP connection.
|
||||||
|
#
|
||||||
# Python milters should derive from this class
|
# Python milters should derive from this class
|
||||||
# unless they are using the low lever milter module directly.
|
# unless they are using the low level milter module directly.
|
||||||
# All optional callbacks are disabled, and automatically
|
#
|
||||||
# reenabled when overridden.
|
# Most of the methods are either "actions" or "callbacks". Callbacks
|
||||||
|
# are invoked by the MTA at certain points in the SMTP protocol. For
|
||||||
|
# instance when the HELO command is seen, the MTA calls the helo
|
||||||
|
# callback before returning a response code. All callbacks must
|
||||||
|
# return one of these constants: CONTINUE, TEMPFAIL, REJECT, ACCEPT,
|
||||||
|
# DISCARD, SKIP. The NOREPLY response is supplied automatically by
|
||||||
|
# the @@noreply decorator if negotiation with the MTA is successful.
|
||||||
|
# @@noreply and @@nocallback methods should return CONTINUE for two reasons:
|
||||||
|
# the MTA may not support negotiation, and the class may be running in a test
|
||||||
|
# harness.
|
||||||
|
#
|
||||||
|
# Optional callbacks are disabled with the @@nocallback decorator, and
|
||||||
|
# automatically reenabled when overridden. Disabled callbacks should
|
||||||
|
# still return CONTINUE for testing and MTAs that do not support
|
||||||
|
# negotiation.
|
||||||
|
|
||||||
|
# Each SMTP connection to the MTA calls the factory method you provide to
|
||||||
|
# create an instance derived from this class. This is typically the
|
||||||
|
# constructor for a class derived from Base. The _setctx() method attaches
|
||||||
|
# the instance to the low level milter.milterContext object. When the SMTP
|
||||||
|
# connection terminates, the close callback is called, the low level connection
|
||||||
|
# object is destroyed, and this normally causes instances of this class to be
|
||||||
|
# garbage collected as well. The close() method should release any global
|
||||||
|
# resources held by instances.
|
||||||
# @since 0.9.2
|
# @since 0.9.2
|
||||||
class Base(object):
|
class Base(object):
|
||||||
"The core class interface to the milter module."
|
"The core class interface to the %milter module."
|
||||||
|
|
||||||
## Attach this Milter to the low level milter.milterContext object.
|
## Attach this Milter to the low level milter.milterContext object.
|
||||||
def _setctx(self,ctx):
|
def _setctx(self,ctx):
|
||||||
|
## The low level @ref milter.milterContext object.
|
||||||
self._ctx = ctx
|
self._ctx = ctx
|
||||||
|
## A bitmask of actions this connection has negotiated to use.
|
||||||
|
# By default, all actions are enabled. High throughput milters
|
||||||
|
# may want to disable unused actions to increase efficiency.
|
||||||
|
# Some optional actions may be disabled by calling milter.set_flags(), or
|
||||||
|
# by overriding the negotiate callback. The bits include:
|
||||||
|
# <code>ADDHDRS,CHGBODY,MODBODY,ADDRCPT,ADDRCPT_PAR,DELRCPT
|
||||||
|
# CHGHDRS,QUARANTINE,CHGFROM,SETSYMLIST</code>.
|
||||||
|
# The <code>Milter.CURR_ACTS</code> bitmask is all actions
|
||||||
|
# known when the milter module was compiled.
|
||||||
|
# Application code can also inspect this field to determine
|
||||||
|
# which actions are available. This is especially useful in
|
||||||
|
# generic library code designed to work in multiple milters.
|
||||||
|
# @since 0.9.2
|
||||||
|
#
|
||||||
self._actions = CURR_ACTS # all actions enabled by default
|
self._actions = CURR_ACTS # all actions enabled by default
|
||||||
|
## A bitmask of protocol options this connection has negotiated.
|
||||||
|
# An application may inspect this
|
||||||
|
# variable to determine which protocol steps are supported. Options
|
||||||
|
# of interest to applications: the SKIP result code is allowed
|
||||||
|
# only if the P_SKIP bit is set, rejected recipients are passed to the
|
||||||
|
# %milter application only if the P_RCPT_REJ bit is set, and
|
||||||
|
# header values are sent and received with leading spaces (in the
|
||||||
|
# continuation lines) intact if the P_HDR_LEADSPC bit is set (so
|
||||||
|
# that the application can customize indenting).
|
||||||
|
#
|
||||||
|
# The P_N* bits should be negotiated via the @@noreply and @@nocallback
|
||||||
|
# method decorators, and P_RCPT_REJ, P_HDR_LEADSPC should
|
||||||
|
# be enabled using the enable_protocols class decorator.
|
||||||
|
#
|
||||||
|
# The bits include: <code>
|
||||||
|
# P_RCPT_REJ P_NR_CONN P_NR_HELO P_NR_MAIL P_NR_RCPT P_NR_DATA P_NR_UNKN
|
||||||
|
# P_NR_EOH P_NR_BODY P_NR_HDR P_NOCONNECT P_NOHELO P_NOMAIL P_NORCPT
|
||||||
|
# P_NODATA P_NOUNKNOWN P_NOEOH P_NOBODY P_NOHDRS P_HDR_LEADSPC P_SKIP
|
||||||
|
# </code> (all under the Milter namespace).
|
||||||
|
# @since 0.9.2
|
||||||
self._protocol = 0 # no protocol options by default
|
self._protocol = 0 # no protocol options by default
|
||||||
if ctx:
|
if ctx:
|
||||||
ctx.setpriv(self)
|
ctx.setpriv(self)
|
||||||
## @var _actions
|
|
||||||
# A bitmask of actions this milter has negotiated to use.
|
|
||||||
# By default, all actions are enabled. This may be changed
|
|
||||||
# by calling <code>milter.set_flags</code>, or by overriding
|
|
||||||
# the negotiate callback. The bits include:
|
|
||||||
# <code>ADDHDRS,CHGBODY,MODBODY,ADDRCPT,ADDRCPT_PAR,DELRCPT
|
|
||||||
# CHGHDRS,QUARANTINE,CHGFROM,SETSMLIST</code>.
|
|
||||||
# The <code>Milter.CURR_ACTS</code> bitmask is all actions
|
|
||||||
# known when the milter module was compiled.
|
|
||||||
# @since 0.9.2
|
|
||||||
#
|
|
||||||
|
|
||||||
## @var _protocol
|
|
||||||
# A bitmask of protocol options this milter has negotiated.
|
|
||||||
# The bits generally indicate that a particular step should be
|
|
||||||
# skipped, since previous versions of the milter protocol had
|
|
||||||
# no provision for skipping steps.
|
|
||||||
# The bits include: <code>
|
|
||||||
# P_RCPT_REJ P_NR_CONN P_NR_HELO P_NR_MAIL P_NR_RCPT P_NR_DATA P_NR_UNKN
|
|
||||||
# P_NR_EOH P_NR_BODY P_NR_HDR P_NOCONNECT P_NOHELO P_NOMAIL P_NORCPT
|
|
||||||
# P_NODATA P_NOUNKNOWN P_NOEOH P_NOBODY P_NOHDRS P_HDR_LEADSPC P_SKIP
|
|
||||||
# </code> (all under the Milter namespace).
|
|
||||||
# @since 0.9.2
|
|
||||||
|
|
||||||
## Defined by subclasses to write log messages.
|
## Defined by subclasses to write log messages.
|
||||||
def log(self,*msg): pass
|
def log(self,*msg): pass
|
||||||
## Called for each connection to the MTA.
|
## Called for each connection to the MTA. Called by the
|
||||||
|
# <a href="https://www.milter.org/developers/api/xxfi_connect">
|
||||||
|
# xxfi_connect</a> callback.
|
||||||
# The <code>hostname</code> provided by the local MTA is either
|
# The <code>hostname</code> provided by the local MTA is either
|
||||||
# the PTR name or the IP in the form "[1.2.3.4]" if no PTR is available.
|
# the PTR name or the IP in the form "[1.2.3.4]" if no PTR is available.
|
||||||
# The format of hostaddr depends on the socket family:
|
# The format of hostaddr depends on the socket family:
|
||||||
@@ -177,6 +277,17 @@ class Base(object):
|
|||||||
# <dt><code>socket.AF_UNIX</code>
|
# <dt><code>socket.AF_UNIX</code>
|
||||||
# <dd>A string with the socketname
|
# <dd>A string with the socketname
|
||||||
# </dl>
|
# </dl>
|
||||||
|
# To vary behavior based on what port the client connected to,
|
||||||
|
# for example skipping blacklist checks for port 587 (which must
|
||||||
|
# be authenticated), use @link #getsymval getsymval('{daemon_port}') @endlink.
|
||||||
|
# The <code>{daemon_port}</code> macro must be enabled in sendmail.cf
|
||||||
|
# <pre>
|
||||||
|
# O Milter.macros.connect=j, _, {daemon_name}, {daemon_port}, {if_name}, {if_addr}
|
||||||
|
# </pre>
|
||||||
|
# or sendmail.mc
|
||||||
|
# <pre>
|
||||||
|
# define(`confMILTER_MACROS_CONNECT', ``j, _, {daemon_name}, {daemon_port}, {if_name}, {if_addr}'')dnl
|
||||||
|
# </pre>
|
||||||
# @param hostname the PTR name or bracketed IP of the SMTP client
|
# @param hostname the PTR name or bracketed IP of the SMTP client
|
||||||
# @param family <code>socket.AF_INET</code>, <code>socket.AF_INET6</code>,
|
# @param family <code>socket.AF_INET</code>, <code>socket.AF_INET6</code>,
|
||||||
# or <code>socket.AF_UNIX</code>
|
# or <code>socket.AF_UNIX</code>
|
||||||
@@ -188,12 +299,26 @@ class Base(object):
|
|||||||
# this almost always results in terminating the connection.
|
# this almost always results in terminating the connection.
|
||||||
@nocallback
|
@nocallback
|
||||||
def hello(self,hostname): return CONTINUE
|
def hello(self,hostname): return CONTINUE
|
||||||
## Called when the SMTP client says MAIL FROM.
|
## Called when the SMTP client says MAIL FROM. Called by the
|
||||||
|
# <a href="https://www.milter.org/developers/api/xxfi_envfrom">
|
||||||
|
# xxfi_envfrom</a> callback.
|
||||||
# Returning REJECT rejects the message, but not the connection.
|
# Returning REJECT rejects the message, but not the connection.
|
||||||
|
# The sender is the "envelope" from as defined by
|
||||||
|
# <a href="http://tools.ietf.org/html/rfc5321">RFC 5321</a>.
|
||||||
|
# For the From: header (author) defined in
|
||||||
|
# <a href="http://tools.ietf.org/html/rfc5322">RFC 5322</a>,
|
||||||
|
# see @link #header the header callback @endlink.
|
||||||
@nocallback
|
@nocallback
|
||||||
def envfrom(self,f,*str): return CONTINUE
|
def envfrom(self,f,*str): return CONTINUE
|
||||||
## Called when the SMTP client says RCPT TO.
|
## Called when the SMTP client says RCPT TO. Called by the
|
||||||
|
# <a href="https://www.milter.org/developers/api/xxfi_envrcpt">
|
||||||
|
# xxfi_envrcpt</a> callback.
|
||||||
# Returning REJECT rejects the current recipient, not the entire message.
|
# Returning REJECT rejects the current recipient, not the entire message.
|
||||||
|
# The recipient is the "envelope" recipient as defined by
|
||||||
|
# <a href="http://tools.ietf.org/html/rfc5321">RFC 5321</a>.
|
||||||
|
# For recipients defined in
|
||||||
|
# <a href="http://tools.ietf.org/html/rfc5322">RFC 5322</a>,
|
||||||
|
# for example To: or Cc:, see @link #header the header callback @endlink.
|
||||||
@nocallback
|
@nocallback
|
||||||
def envrcpt(self,to,*str): return CONTINUE
|
def envrcpt(self,to,*str): return CONTINUE
|
||||||
## Called when the SMTP client says DATA.
|
## Called when the SMTP client says DATA.
|
||||||
@@ -227,7 +352,7 @@ class Base(object):
|
|||||||
## Called when the connection is closed.
|
## Called when the connection is closed.
|
||||||
def close(self): return CONTINUE
|
def close(self): return CONTINUE
|
||||||
|
|
||||||
## Return mask of SMFIP_N.. protocol option bits to clear for this class
|
## Return mask of SMFIP_N* protocol option bits to clear for this class
|
||||||
# The @@nocallback and @@noreply decorators set the
|
# The @@nocallback and @@noreply decorators set the
|
||||||
# <code>milter_protocol</code> function attribute to the protocol mask bit to
|
# <code>milter_protocol</code> function attribute to the protocol mask bit to
|
||||||
# pass to libmilter, causing that callback or its reply to be skipped.
|
# pass to libmilter, causing that callback or its reply to be skipped.
|
||||||
@@ -251,10 +376,20 @@ class Base(object):
|
|||||||
klass._protocol_mask = p
|
klass._protocol_mask = p
|
||||||
return p
|
return p
|
||||||
|
|
||||||
## Negotiate milter protocol options.
|
## Negotiate milter protocol options. Called by the
|
||||||
|
# <a href="https://www.milter.org/developers/api/xxfi_negotiate">
|
||||||
|
# xffi_negotiate</a> callback. This is an advanced callback,
|
||||||
|
# do not override unless you know what you are doing. Most
|
||||||
|
# negotiation can be done simply by using the supplied
|
||||||
|
# class and function decorators.
|
||||||
|
# Options are passed as
|
||||||
|
# a list of 4 32-bit ints which can be modified and are passed
|
||||||
|
# back to libmilter on return.
|
||||||
# Default negotiation sets P_NO* and P_NR* for callbacks
|
# Default negotiation sets P_NO* and P_NR* for callbacks
|
||||||
# marked @@nocallback and @@noreply respectively, leaves all
|
# marked @@nocallback and @@noreply respectively, leaves all
|
||||||
# actions enabled, and enables Milter.SKIP.
|
# actions enabled, and enables Milter.SKIP. The @@enable_protocols
|
||||||
|
# class decorator can customize which protocol steps are implemented.
|
||||||
|
# @param opts a modifiable list of 4 ints with negotiated options
|
||||||
# @since 0.9.2
|
# @since 0.9.2
|
||||||
def negotiate(self,opts):
|
def negotiate(self,opts):
|
||||||
try:
|
try:
|
||||||
@@ -273,54 +408,93 @@ class Base(object):
|
|||||||
## Return the value of an MTA macro. Sendmail macro names
|
## Return the value of an MTA macro. Sendmail macro names
|
||||||
# are either single chars (e.g. "j") or multiple chars enclosed
|
# are either single chars (e.g. "j") or multiple chars enclosed
|
||||||
# in braces (e.g. "{auth_type}"). Macro names are MTA dependent.
|
# in braces (e.g. "{auth_type}"). Macro names are MTA dependent.
|
||||||
|
# See <a href="https://www.milter.org/developers/api/smfi_getsymval">
|
||||||
|
# smfi_getsymval</a> for default sendmail macros.
|
||||||
# @param sym the macro name
|
# @param sym the macro name
|
||||||
def getsymval(self,sym):
|
def getsymval(self,sym):
|
||||||
return self._ctx.getsymval(sym)
|
return self._ctx.getsymval(sym)
|
||||||
|
|
||||||
## Set the SMTP reply code and message.
|
## Set the SMTP reply code and message.
|
||||||
# If the MTA does not support setmlreply, then only the
|
# If the MTA does not support setmlreply, then only the
|
||||||
# first msg line is used.
|
# first msg line is used. Any '%%' in a message line
|
||||||
|
# must be doubled, or libmilter will silently ignore the setreply.
|
||||||
|
# Beginning with 0.9.6, we test for that case and throw ValueError to avoid
|
||||||
|
# head scratching. What will <i>really</i> irritate you, however,
|
||||||
|
# is that if you carefully double any '%%', your message will be
|
||||||
|
# sent - but with the '%%' still doubled!
|
||||||
|
# See <a href="https://www.milter.org/developers/api/smfi_setreply">
|
||||||
|
# smfi_setreply</a> for more information.
|
||||||
|
# @param rcode The three-digit (RFC 821/2821) SMTP reply code as a string.
|
||||||
|
# rcode cannot be None, and <b>must be a valid 4XX or 5XX reply code</b>.
|
||||||
|
# @param xcode The extended (RFC 1893/2034) reply code. If xcode is None,
|
||||||
|
# no extended code is used. Otherwise, xcode must conform to RFC 1893/2034.
|
||||||
|
# @param msg The text part of the SMTP reply. If msg is None,
|
||||||
|
# an empty message is used.
|
||||||
|
# @param ml Optional additional message lines.
|
||||||
def setreply(self,rcode,xcode=None,msg=None,*ml):
|
def setreply(self,rcode,xcode=None,msg=None,*ml):
|
||||||
|
for m in (msg,)+ml:
|
||||||
|
if 1 in [len(s)&1 for s in R.findall(m)]:
|
||||||
|
raise ValueError("'%' must be doubled: "+m)
|
||||||
return self._ctx.setreply(rcode,xcode,msg,*ml)
|
return self._ctx.setreply(rcode,xcode,msg,*ml)
|
||||||
|
|
||||||
## Tell the MTA which macro names will be used.
|
## Tell the MTA which macro names will be used.
|
||||||
# The <code>Milter.SETSMLIST</code> action flag must be set.
|
# This information can reduce the size of messages received from sendmail,
|
||||||
|
# and hence could reduce bandwidth between sendmail and your milter where
|
||||||
|
# that is a factor. The <code>Milter.SETSYMLIST</code> action flag must be
|
||||||
|
# set. The protocol stages are M_CONNECT, M_HELO, M_ENVFROM, M_ENVRCPT,
|
||||||
|
# M_DATA, M_EOM, M_EOH.
|
||||||
#
|
#
|
||||||
# May only be called from negotiate callback.
|
# May only be called from negotiate callback.
|
||||||
# @since 0.9.2
|
# @since 0.9.8, previous version was misspelled!
|
||||||
# @param stage the protocol stage to set to macro list for
|
# @param stage the protocol stage to set to macro list for,
|
||||||
# @param macros a string with a space delimited list of macros
|
# one of the M_* constants defined in Milter
|
||||||
def setsmlist(self,stage,macros):
|
# @param macros space separated and/or lists of strings
|
||||||
if not self._actions & SETSMLIST: raise DisabledAction("SETSMLIST")
|
def setsymlist(self,stage,*macros):
|
||||||
if type(macros) in (list,tuple):
|
if not self._actions & SETSYMLIST: raise DisabledAction("SETSYMLIST")
|
||||||
macros = ' '.join(macros)
|
a = []
|
||||||
return self._ctx.setsmlist(stage,macros)
|
for m in macros:
|
||||||
|
try:
|
||||||
|
m = m.encode('utf8')
|
||||||
|
except: pass
|
||||||
|
try:
|
||||||
|
m = m.split(' ')
|
||||||
|
except: pass
|
||||||
|
a += m
|
||||||
|
return self._ctx.setsmlist(stage,' '.join(a))
|
||||||
|
|
||||||
# Milter methods which can only be called from eom callback.
|
# Milter methods which can only be called from eom callback.
|
||||||
|
|
||||||
## Add a mail header field.
|
## Add a mail header field.
|
||||||
|
# Calls <a href="https://www.milter.org/developers/api/smfi_addheader">
|
||||||
|
# smfi_addheader</a>.
|
||||||
# The <code>Milter.ADDHDRS</code> action flag must be set.
|
# The <code>Milter.ADDHDRS</code> action flag must be set.
|
||||||
#
|
#
|
||||||
# May be called from eom callback only.
|
# May be called from eom callback only.
|
||||||
# @param field the header field name
|
# @param field the header field name
|
||||||
# @param value the header field value
|
# @param value the header field value
|
||||||
# @param idx header field index from the top of the message to insert at
|
# @param idx header field index from the top of the message to insert at
|
||||||
|
# @throws DisabledAction if ADDHDRS is not enabled
|
||||||
def addheader(self,field,value,idx=-1):
|
def addheader(self,field,value,idx=-1):
|
||||||
if not self._actions & ADDHDRS: raise DisabledAction("ADDHDRS")
|
if not self._actions & ADDHDRS: raise DisabledAction("ADDHDRS")
|
||||||
return self._ctx.addheader(field,value,idx)
|
return self._ctx.addheader(field,value,idx)
|
||||||
|
|
||||||
## Change the value of a mail header field.
|
## Change the value of a mail header field.
|
||||||
|
# Calls <a href="https://www.milter.org/developers/api/smfi_chgheader">
|
||||||
|
# smfi_chgheader</a>.
|
||||||
# The <code>Milter.CHGHDRS</code> action flag must be set.
|
# The <code>Milter.CHGHDRS</code> action flag must be set.
|
||||||
#
|
#
|
||||||
# May be called from eom callback only.
|
# May be called from eom callback only.
|
||||||
# @param field the name of the field to change
|
# @param field the name of the field to change
|
||||||
# @param idx index of the field to change when there are multiple instances
|
# @param idx index of the field to change when there are multiple instances
|
||||||
# @param value the new value of the field
|
# @param value the new value of the field
|
||||||
|
# @throws DisabledAction if CHGHDRS is not enabled
|
||||||
def chgheader(self,field,idx,value):
|
def chgheader(self,field,idx,value):
|
||||||
if not self._actions & CHGHDRS: raise DisabledAction("CHGHDRS")
|
if not self._actions & CHGHDRS: raise DisabledAction("CHGHDRS")
|
||||||
return self._ctx.chgheader(field,idx,value)
|
return self._ctx.chgheader(field,idx,value)
|
||||||
|
|
||||||
## Add a recipient to the message.
|
## Add a recipient to the message.
|
||||||
|
# Calls <a href="https://www.milter.org/developers/api/smfi_addrcpt">
|
||||||
|
# smfi_addrcpt</a>.
|
||||||
# If no corresponding mail header is added, this is like a Bcc.
|
# If no corresponding mail header is added, this is like a Bcc.
|
||||||
# The syntax of the recipient is the same as used in the SMTP
|
# The syntax of the recipient is the same as used in the SMTP
|
||||||
# RCPT TO command (and as delivered to the envrcpt callback), for example
|
# RCPT TO command (and as delivered to the envrcpt callback), for example
|
||||||
@@ -332,33 +506,42 @@ class Base(object):
|
|||||||
# May be called from eom callback only.
|
# May be called from eom callback only.
|
||||||
# @param rcpt the message recipient
|
# @param rcpt the message recipient
|
||||||
# @param params an optional list of ESMTP parameters
|
# @param params an optional list of ESMTP parameters
|
||||||
|
# @throws DisabledAction if ADDRCPT or ADDRCPT_PAR is not enabled
|
||||||
def addrcpt(self,rcpt,params=None):
|
def addrcpt(self,rcpt,params=None):
|
||||||
if not self._actions & ADDRCPT: raise DisabledAction("ADDRCPT")
|
if not self._actions & ADDRCPT: raise DisabledAction("ADDRCPT")
|
||||||
if params and not self._actions & ADDRCPT_PAR:
|
if params and not self._actions & ADDRCPT_PAR:
|
||||||
raise DisabledAction("ADDRCPT_PAR")
|
raise DisabledAction("ADDRCPT_PAR")
|
||||||
return self._ctx.addrcpt(rcpt,params)
|
return self._ctx.addrcpt(rcpt,params)
|
||||||
## Delete a recipient from the message.
|
## Delete a recipient from the message.
|
||||||
|
# Calls <a href="https://www.milter.org/developers/api/smfi_delrcpt">
|
||||||
|
# smfi_delrcpt</a>.
|
||||||
# The recipient should match one passed to the envrcpt callback.
|
# The recipient should match one passed to the envrcpt callback.
|
||||||
# The <code>Milter.DELRCPT</code> action flag must be set.
|
# The <code>Milter.DELRCPT</code> action flag must be set.
|
||||||
#
|
#
|
||||||
# May be called from eom callback only.
|
# May be called from eom callback only.
|
||||||
# @param rcpt the message recipient to delete
|
# @param rcpt the message recipient to delete
|
||||||
|
# @throws DisabledAction if DELRCPT is not enabled
|
||||||
def delrcpt(self,rcpt):
|
def delrcpt(self,rcpt):
|
||||||
if not self._actions & DELRCPT: raise DisabledAction("DELRCPT")
|
if not self._actions & DELRCPT: raise DisabledAction("DELRCPT")
|
||||||
return self._ctx.delrcpt(rcpt)
|
return self._ctx.delrcpt(rcpt)
|
||||||
|
|
||||||
## Replace the message body.
|
## Replace the message body.
|
||||||
|
# Calls <a href="https://www.milter.org/developers/api/smfi_replacebody">
|
||||||
|
# smfi_replacebody</a>.
|
||||||
# The entire message body must be replaced.
|
# The entire message body must be replaced.
|
||||||
# Call repeatedly with blocks of data until the entire body is transferred.
|
# Call repeatedly with blocks of data until the entire body is transferred.
|
||||||
# The <code>Milter.MODBODY</code> action flag must be set.
|
# The <code>Milter.MODBODY</code> action flag must be set.
|
||||||
#
|
#
|
||||||
# May be called from eom callback only.
|
# May be called from eom callback only.
|
||||||
# @param body a chunk of body data
|
# @param body a chunk of body data
|
||||||
|
# @throws DisabledAction if MODBODY is not enabled
|
||||||
def replacebody(self,body):
|
def replacebody(self,body):
|
||||||
if not self._actions & MODBODY: raise DisabledAction("MODBODY")
|
if not self._actions & MODBODY: raise DisabledAction("MODBODY")
|
||||||
return self._ctx.replacebody(body)
|
return self._ctx.replacebody(body)
|
||||||
|
|
||||||
## Change the SMTP envelope sender address.
|
## Change the SMTP envelope sender address.
|
||||||
|
# Calls <a href="https://www.milter.org/developers/api/smfi_chgfrom">
|
||||||
|
# smfi_chgfrom</a>.
|
||||||
# The syntax of the sender is that same as used in the SMTP
|
# The syntax of the sender is that same as used in the SMTP
|
||||||
# MAIL FROM command (and as delivered to the envfrom callback),
|
# MAIL FROM command (and as delivered to the envfrom callback),
|
||||||
# for example <code>self.chgfrom('<bar@example.com>')</code>.
|
# for example <code>self.chgfrom('<bar@example.com>')</code>.
|
||||||
@@ -368,22 +551,28 @@ class Base(object):
|
|||||||
# @since 0.9.1
|
# @since 0.9.1
|
||||||
# @param sender the new sender address
|
# @param sender the new sender address
|
||||||
# @param params an optional list of ESMTP parameters
|
# @param params an optional list of ESMTP parameters
|
||||||
|
# @throws DisabledAction if CHGFROM is not enabled
|
||||||
def chgfrom(self,sender,params=None):
|
def chgfrom(self,sender,params=None):
|
||||||
if not self._actions & CHGFROM: raise DisabledAction("CHGFROM")
|
if not self._actions & CHGFROM: raise DisabledAction("CHGFROM")
|
||||||
return self._ctx.chgfrom(sender,params)
|
return self._ctx.chgfrom(sender,params)
|
||||||
|
|
||||||
## Quarantine the message.
|
## Quarantine the message.
|
||||||
|
# Calls <a href="https://www.milter.org/developers/api/smfi_quarantine">
|
||||||
|
# smfi_quarantine</a>.
|
||||||
# When quarantined, a message goes into the mailq as if to be delivered,
|
# When quarantined, a message goes into the mailq as if to be delivered,
|
||||||
# but delivery is deferred until the message is unquarantined.
|
# but delivery is deferred until the message is unquarantined.
|
||||||
# The <code>Milter.QUARANTINE</code> action flag must be set.
|
# The <code>Milter.QUARANTINE</code> action flag must be set.
|
||||||
#
|
#
|
||||||
# May be called from eom callback only.
|
# May be called from eom callback only.
|
||||||
# @param reason a string describing the reason for quarantine
|
# @param reason a string describing the reason for quarantine
|
||||||
|
# @throws DisabledAction if QUARANTINE is not enabled
|
||||||
def quarantine(self,reason):
|
def quarantine(self,reason):
|
||||||
if not self._actions & QUARANTINE: raise DisabledAction("QUARANTINE")
|
if not self._actions & QUARANTINE: raise DisabledAction("QUARANTINE")
|
||||||
return self._ctx.quarantine(reason)
|
return self._ctx.quarantine(reason)
|
||||||
|
|
||||||
## Tell the MTA to wait a bit longer.
|
## Tell the MTA to wait a bit longer.
|
||||||
|
# Calls <a href="https://www.milter.org/developers/api/smfi_progress">
|
||||||
|
# smfi_progress</a>.
|
||||||
# Resets timeouts in the MTA that detect a "hung" milter.
|
# Resets timeouts in the MTA that detect a "hung" milter.
|
||||||
def progress(self):
|
def progress(self):
|
||||||
return self._ctx.progress()
|
return self._ctx.progress()
|
||||||
@@ -465,12 +654,14 @@ class Milter(Base):
|
|||||||
factory = Milter
|
factory = Milter
|
||||||
|
|
||||||
## @private
|
## @private
|
||||||
|
# @brief Connect context to connection instance and return enabled callbacks.
|
||||||
def negotiate_callback(ctx,opts):
|
def negotiate_callback(ctx,opts):
|
||||||
m = factory()
|
m = factory()
|
||||||
m._setctx(ctx)
|
m._setctx(ctx)
|
||||||
return m.negotiate(opts)
|
return m.negotiate(opts)
|
||||||
|
|
||||||
## @private
|
## @private
|
||||||
|
# @brief Connect context if needed and invoke connect method.
|
||||||
def connect_callback(ctx,hostname,family,hostaddr,nr_mask=P_NR_CONN):
|
def connect_callback(ctx,hostname,family,hostaddr,nr_mask=P_NR_CONN):
|
||||||
m = ctx.getpriv()
|
m = ctx.getpriv()
|
||||||
if not m:
|
if not m:
|
||||||
@@ -481,6 +672,7 @@ def connect_callback(ctx,hostname,family,hostaddr,nr_mask=P_NR_CONN):
|
|||||||
return m.connect(hostname,family,hostaddr)
|
return m.connect(hostname,family,hostaddr)
|
||||||
|
|
||||||
## @private
|
## @private
|
||||||
|
# @brief Disconnect milterContext and call close method.
|
||||||
def close_callback(ctx):
|
def close_callback(ctx):
|
||||||
m = ctx.getpriv()
|
m = ctx.getpriv()
|
||||||
if not m: return CONTINUE
|
if not m: return CONTINUE
|
||||||
@@ -527,11 +719,11 @@ def envcallback(c,args):
|
|||||||
pargs.append(s)
|
pargs.append(s)
|
||||||
return c(*pargs,**kw)
|
return c(*pargs,**kw)
|
||||||
|
|
||||||
## Run the milter.
|
## Run the %milter.
|
||||||
# @param name the name of the milter known by the MTA
|
# @param name the name of the %milter known to the MTA
|
||||||
# @param socketname the socket to be passed to <code>milter.setconn</code>
|
# @param socketname the socket to be passed to milter.setconn()
|
||||||
# @param timeout the time in secs the MTA should wait for a response before
|
# @param timeout the time in secs the MTA should wait for a response before
|
||||||
# considering this milter dead
|
# considering this %milter dead
|
||||||
def runmilter(name,socketname,timeout = 0):
|
def runmilter(name,socketname,timeout = 0):
|
||||||
# This bit is here on the assumption that you will be starting this filter
|
# This bit is here on the assumption that you will be starting this filter
|
||||||
# before sendmail. If sendmail is not running and the socket already exists,
|
# before sendmail. If sendmail is not running and the socket already exists,
|
||||||
@@ -598,4 +790,5 @@ for priv in ('os','milter','thread','factory','_seq','_seq_lock','__version__'):
|
|||||||
__all__ = __all__.keys()
|
__all__ = __all__.keys()
|
||||||
|
|
||||||
## @example milter-template.py
|
## @example milter-template.py
|
||||||
|
## @example milter-nomix.py
|
||||||
#
|
#
|
||||||
|
|||||||
+12
-9
@@ -10,6 +10,9 @@
|
|||||||
# CBV results.
|
# CBV results.
|
||||||
#
|
#
|
||||||
# $Log$
|
# $Log$
|
||||||
|
# Revision 1.9 2008/05/08 21:35:57 customdesigned
|
||||||
|
# Allow explicitly whitelisted email from banned_users.
|
||||||
|
#
|
||||||
# Revision 1.8 2007/09/03 16:18:45 customdesigned
|
# Revision 1.8 2007/09/03 16:18:45 customdesigned
|
||||||
# Delete unparseable timestamps when loading address cache. These have
|
# Delete unparseable timestamps when loading address cache. These have
|
||||||
# arisen because of failure to parse MAIL FROM properly. Will have to
|
# arisen because of failure to parse MAIL FROM properly. Will have to
|
||||||
@@ -72,8 +75,8 @@ class AddrCache(object):
|
|||||||
except OSError:
|
except OSError:
|
||||||
fp = ()
|
fp = ()
|
||||||
for ln in fp:
|
for ln in fp:
|
||||||
try:
|
try:
|
||||||
rcpt,ts = ln.strip().split(None,1)
|
rcpt,ts = ln.strip().split(None,1)
|
||||||
try:
|
try:
|
||||||
l = time.strptime(ts,AddrCache.time_format)
|
l = time.strptime(ts,AddrCache.time_format)
|
||||||
t = time.mktime(l)
|
t = time.mktime(l)
|
||||||
@@ -84,11 +87,11 @@ class AddrCache(object):
|
|||||||
except: # unparsable timestamp - likely garbage
|
except: # unparsable timestamp - likely garbage
|
||||||
changed = True
|
changed = True
|
||||||
continue
|
continue
|
||||||
except: # manual entry (no timestamp)
|
except: # manual entry (no timestamp)
|
||||||
cache[ln.strip().lower()] = (now,None)
|
cache[ln.strip().lower()] = (now,None)
|
||||||
wfp.write(ln)
|
wfp.write(ln)
|
||||||
if changed:
|
if changed:
|
||||||
lock.commit(self.fname+'.old')
|
lock.commit(self.fname+'.old')
|
||||||
else:
|
else:
|
||||||
lock.unlock()
|
lock.unlock()
|
||||||
except IOError:
|
except IOError:
|
||||||
@@ -126,13 +129,13 @@ class AddrCache(object):
|
|||||||
ts,res = self.cache[lsender]
|
ts,res = self.cache[lsender]
|
||||||
too_old = time.time() - self.age*24*60*60 # max age in days
|
too_old = time.time() - self.age*24*60*60 # max age in days
|
||||||
if not ts or ts > too_old:
|
if not ts or ts > too_old:
|
||||||
return res
|
return res
|
||||||
del self.cache[lsender]
|
del self.cache[lsender]
|
||||||
raise KeyError, sender
|
raise KeyError, sender
|
||||||
except KeyError,x:
|
except KeyError,x:
|
||||||
try:
|
try:
|
||||||
user,host = sender.split('@',1)
|
user,host = sender.split('@',1)
|
||||||
return self.__getitem__(host)
|
return self.__getitem__(host)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise x
|
raise x
|
||||||
|
|
||||||
|
|||||||
+11
-11
@@ -29,10 +29,10 @@ class MilterConfigParser(ConfigParser):
|
|||||||
q = q.strip()
|
q = q.strip()
|
||||||
if q.startswith('file:'):
|
if q.startswith('file:'):
|
||||||
domain = q[5:].lower()
|
domain = q[5:].lower()
|
||||||
d[domain] = d.setdefault(domain,[]) + open(domain,'r').read().split()
|
d[domain] = d.setdefault(domain,[]) + open(domain,'r').read().split()
|
||||||
else:
|
else:
|
||||||
user,domain = q.split('@')
|
user,domain = q.split('@')
|
||||||
d.setdefault(domain.lower(),[]).append(user)
|
d.setdefault(domain.lower(),[]).append(user)
|
||||||
return d
|
return d
|
||||||
|
|
||||||
def getaddrdict(self,sect,opt):
|
def getaddrdict(self,sect,opt):
|
||||||
@@ -43,14 +43,14 @@ class MilterConfigParser(ConfigParser):
|
|||||||
q = q.strip()
|
q = q.strip()
|
||||||
if self.has_option(sect,q):
|
if self.has_option(sect,q):
|
||||||
l = self.get(sect,q)
|
l = self.get(sect,q)
|
||||||
for addr in l.split(','):
|
for addr in l.split(','):
|
||||||
addr = addr.strip()
|
addr = addr.strip()
|
||||||
if addr.startswith('file:'):
|
if addr.startswith('file:'):
|
||||||
fname = addr[5:]
|
fname = addr[5:]
|
||||||
for a in open(fname,'r').read().split():
|
for a in open(fname,'r').read().split():
|
||||||
d[a] = q
|
d[a] = q
|
||||||
else:
|
else:
|
||||||
d[addr] = q
|
d[addr] = q
|
||||||
return d
|
return d
|
||||||
|
|
||||||
def getdefault(self,sect,opt,default=None):
|
def getdefault(self,sect,opt,default=None):
|
||||||
|
|||||||
+24
-4
@@ -70,15 +70,23 @@ class Session(object):
|
|||||||
pre: qtype in ['A', 'AAAA', 'MX', 'PTR', 'TXT', 'SPF']
|
pre: qtype in ['A', 'AAAA', 'MX', 'PTR', 'TXT', 'SPF']
|
||||||
post: isinstance(__return__, types.ListType)
|
post: isinstance(__return__, types.ListType)
|
||||||
"""
|
"""
|
||||||
|
if name.endswith('.'): name = name[:-1]
|
||||||
|
if not reduce(lambda x,y:x and 0 < len(y) < 64, name.split('.'),True):
|
||||||
|
return [] # invalid DNS name (too long or empty)
|
||||||
result = self.cache.get( (name, qtype) )
|
result = self.cache.get( (name, qtype) )
|
||||||
cname = None
|
cname = None
|
||||||
|
if result: return result
|
||||||
|
cnamek = (name,'CNAME')
|
||||||
|
cname = self.cache.get( cnamek )
|
||||||
|
|
||||||
if not result:
|
if cname:
|
||||||
|
cname = cname[0]
|
||||||
|
else:
|
||||||
safe2cache = Session.SAFE2CACHE
|
safe2cache = Session.SAFE2CACHE
|
||||||
for k, v in DNSLookup(name, qtype):
|
for k, v in DNSLookup(name, qtype):
|
||||||
if k == (name, 'CNAME'):
|
if k == cnamek:
|
||||||
cname = v
|
cname = v
|
||||||
if (qtype,k[1]) in safe2cache:
|
if k[1] == 'CNAME' or (qtype,k[1]) in safe2cache:
|
||||||
self.cache.setdefault(k, []).append(v)
|
self.cache.setdefault(k, []).append(v)
|
||||||
result = self.cache.get( (name, qtype), [])
|
result = self.cache.get( (name, qtype), [])
|
||||||
if not result and cname:
|
if not result and cname:
|
||||||
@@ -89,10 +97,22 @@ class Session(object):
|
|||||||
raise DNSError('Length of CNAME chain exceeds %d' % MAX_CNAME)
|
raise DNSError('Length of CNAME chain exceeds %d' % MAX_CNAME)
|
||||||
cnames[name] = cname
|
cnames[name] = cname
|
||||||
if cname in cnames:
|
if cname in cnames:
|
||||||
raise DNSError, 'CNAME loop'
|
raise DNSError('CNAME loop')
|
||||||
result = self.dns(cname, qtype, cnames=cnames)
|
result = self.dns(cname, qtype, cnames=cnames)
|
||||||
|
if result:
|
||||||
|
self.cache[(name,qtype)] = result
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def dns_txt(self, domainname, enc='ascii'):
|
||||||
|
"Get a list of TXT records for a domain name."
|
||||||
|
if domainname:
|
||||||
|
try:
|
||||||
|
return [''.join(s.decode(enc) for s in a)
|
||||||
|
for a in self.dns(domainname, 'TXT')]
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
raise DNSError('Non-ascii character in SPF TXT record.')
|
||||||
|
return []
|
||||||
|
|
||||||
DNS.DiscoverNameServers()
|
DNS.DiscoverNameServers()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
+15
-9
@@ -5,6 +5,12 @@
|
|||||||
# Send DSNs, do call back verification,
|
# Send DSNs, do call back verification,
|
||||||
# and generate DSN messages from a template
|
# and generate DSN messages from a template
|
||||||
# $Log$
|
# $Log$
|
||||||
|
# Revision 1.22 2011/03/18 20:41:31 customdesigned
|
||||||
|
# Python2.6 SMTP.close() fails when instance never connected.
|
||||||
|
#
|
||||||
|
# Revision 1.21 2011/03/03 05:11:58 customdesigned
|
||||||
|
# Release 0.9.4
|
||||||
|
#
|
||||||
# Revision 1.20 2010/10/11 00:29:47 customdesigned
|
# Revision 1.20 2010/10/11 00:29:47 customdesigned
|
||||||
# Handle multiple recipients. For CBV or auto whitelist of multiple emails.
|
# Handle multiple recipients. For CBV or auto whitelist of multiple emails.
|
||||||
#
|
#
|
||||||
@@ -111,17 +117,17 @@ def send_dsn(mailfrom,receiver,msg=None,timeout=600,session=None,ourfrom=''):
|
|||||||
if a[0] == receiver:
|
if a[0] == receiver:
|
||||||
return (553,'Fraudulent MX for %s: %s' % (domain,host))
|
return (553,'Fraudulent MX for %s: %s' % (domain,host))
|
||||||
if not (200 <= code <= 299):
|
if not (200 <= code <= 299):
|
||||||
raise smtplib.SMTPHeloError(code, resp)
|
raise smtplib.SMTPHeloError(code, resp)
|
||||||
if msg:
|
if msg:
|
||||||
try:
|
try:
|
||||||
smtp.sendmail('<%s>'%ourfrom,mailfrom,msg)
|
smtp.sendmail('<%s>'%ourfrom,mailfrom,msg)
|
||||||
except smtplib.SMTPSenderRefused:
|
except smtplib.SMTPSenderRefused:
|
||||||
# does not accept DSN, try postmaster (at the risk of mail loops)
|
# does not accept DSN, try postmaster (at the risk of mail loops)
|
||||||
smtp.sendmail('<postmaster@%s>'%receiver,mailfrom,msg)
|
smtp.sendmail('<postmaster@%s>'%receiver,mailfrom,msg)
|
||||||
else: # CBV
|
else: # CBV
|
||||||
code,resp = smtp.docmd('MAIL FROM: <%s>'%ourfrom)
|
code,resp = smtp.docmd('MAIL FROM: <%s>'%ourfrom)
|
||||||
if code != 250:
|
if code != 250:
|
||||||
raise smtplib.SMTPSenderRefused(code, resp, '<%s>'%ourfrom)
|
raise smtplib.SMTPSenderRefused(code, resp, '<%s>'%ourfrom)
|
||||||
if isinstance(mailfrom,basestring):
|
if isinstance(mailfrom,basestring):
|
||||||
mailfrom = [mailfrom]
|
mailfrom = [mailfrom]
|
||||||
badrcpts = {}
|
badrcpts = {}
|
||||||
@@ -129,7 +135,7 @@ def send_dsn(mailfrom,receiver,msg=None,timeout=600,session=None,ourfrom=''):
|
|||||||
code,resp = smtp.rcpt(rcpt)
|
code,resp = smtp.rcpt(rcpt)
|
||||||
if code not in (250,251):
|
if code not in (250,251):
|
||||||
badrcpts[rcpt] = (code,resp)# permanent error
|
badrcpts[rcpt] = (code,resp)# permanent error
|
||||||
smtp.quit()
|
smtp.quit()
|
||||||
if len(badrcpts) == 1:
|
if len(badrcpts) == 1:
|
||||||
return badrcpts.values()[0] # permanent error
|
return badrcpts.values()[0] # permanent error
|
||||||
if badrcpts:
|
if badrcpts:
|
||||||
@@ -149,7 +155,7 @@ def send_dsn(mailfrom,receiver,msg=None,timeout=600,session=None,ourfrom=''):
|
|||||||
pass # MX didn't accept connections, try next one
|
pass # MX didn't accept connections, try next one
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
pass # MX too slow, try next one
|
pass # MX too slow, try next one
|
||||||
smtp.close()
|
if hasattr(smtp,'sock'): smtp.close()
|
||||||
if time.time() > toolate:
|
if time.time() > toolate:
|
||||||
return (450,'No MX response within %f minutes'%(timeout/60.0))
|
return (450,'No MX response within %f minutes'%(timeout/60.0))
|
||||||
return (450,'No MX servers available') # temp error
|
return (450,'No MX servers available') # temp error
|
||||||
|
|||||||
+5
-4
@@ -48,9 +48,10 @@ def is_dynip(host,addr):
|
|||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
if host.startswith('[') and host.endswith(']'):
|
if host.startswith('[') and host.endswith(']'):
|
||||||
return True
|
return True # no ptr
|
||||||
if addr:
|
if addr:
|
||||||
if host.find(addr) >= 0: return True
|
if host.find(addr) >= 0: return True
|
||||||
|
if addr.find(':') >= 0: return False # IP6
|
||||||
a = addr.split('.')
|
a = addr.split('.')
|
||||||
ia = map(int,a)
|
ia = map(int,a)
|
||||||
h = host
|
h = host
|
||||||
@@ -67,8 +68,8 @@ def is_dynip(host,addr):
|
|||||||
if ia[2:] in (g[:2],g[-2:]): return True
|
if ia[2:] in (g[:2],g[-2:]): return True
|
||||||
for m in ip3.finditer(host):
|
for m in ip3.finditer(host):
|
||||||
if int(m.group()) == ia[3]:
|
if int(m.group()) == ia[3]:
|
||||||
h = host[:m.start()] + '<3>' + host[m.end():]
|
h = host[:m.start()] + '<3>' + host[m.end():]
|
||||||
break
|
break
|
||||||
if rehmac.search(h): return True
|
if rehmac.search(h): return True
|
||||||
if host.find(''.join(a[:3])) >= 0: return True
|
if host.find(''.join(a[:3])) >= 0: return True
|
||||||
if host.find(''.join(a[1:])) >= 0: return True
|
if host.find(''.join(a[1:])) >= 0: return True
|
||||||
@@ -85,7 +86,7 @@ if __name__ == '__main__':
|
|||||||
if a[3:5] == ['connect','from']:
|
if a[3:5] == ['connect','from']:
|
||||||
host = a[5]
|
host = a[5]
|
||||||
if host.startswith('[') and host.endswith(']'):
|
if host.startswith('[') and host.endswith(']'):
|
||||||
continue # no PTR
|
continue # no PTR
|
||||||
ip = a[7][2:-2]
|
ip = a[7][2:-2]
|
||||||
if ip in seen: continue
|
if ip in seen: continue
|
||||||
seen.add(ip)
|
seen.add(ip)
|
||||||
|
|||||||
+37
-9
@@ -18,13 +18,19 @@ def quoteAddress(s):
|
|||||||
class Record(object):
|
class Record(object):
|
||||||
__slots__ = ( 'firstseen', 'lastseen', 'umis', 'cnt' )
|
__slots__ = ( 'firstseen', 'lastseen', 'umis', 'cnt' )
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self,timeinc=0):
|
||||||
now = time.time()
|
now = time.time() + timeinc
|
||||||
self.firstseen = now
|
self.firstseen = now
|
||||||
self.lastseen = now
|
self.lastseen = now
|
||||||
self.cnt = 0
|
self.cnt = 0
|
||||||
self.umis = None
|
self.umis = None
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Grey[%s:%s:%s:%d]" % (
|
||||||
|
time.ctime(self.firstseen),time.ctime(self.lastseen),
|
||||||
|
self.umis,self.cnt
|
||||||
|
)
|
||||||
|
|
||||||
class Greylist(object):
|
class Greylist(object):
|
||||||
|
|
||||||
def __init__(self,dbname,grey_time=10,grey_expire=4,grey_retain=36):
|
def __init__(self,dbname,grey_time=10,grey_expire=4,grey_retain=36):
|
||||||
@@ -35,7 +41,26 @@ class Greylist(object):
|
|||||||
self.dbp = shelve.open(dbname,'c',protocol=2)
|
self.dbp = shelve.open(dbname,'c',protocol=2)
|
||||||
self.lock = thread.allocate_lock()
|
self.lock = thread.allocate_lock()
|
||||||
|
|
||||||
def check(self,ip,sender,recipient):
|
def clean(self,timeinc=0):
|
||||||
|
"Delete records past the retention limit."
|
||||||
|
now = time.time() + timeinc
|
||||||
|
cnt = 0
|
||||||
|
dbp = self.dbp
|
||||||
|
for key, r in dbp.iteritems():
|
||||||
|
#print key,r,time.ctime(now)
|
||||||
|
if now > r.lastseen + self.greylist_retain:
|
||||||
|
self.lock.acquire()
|
||||||
|
try:
|
||||||
|
r = dbp[key]
|
||||||
|
now = time.time() + timeinc
|
||||||
|
if now > r.lastseen + self.greylist_retain:
|
||||||
|
del dbp[key]
|
||||||
|
cnt += 1
|
||||||
|
finally:
|
||||||
|
self.lock.release()
|
||||||
|
return cnt
|
||||||
|
|
||||||
|
def check(self,ip,sender,recipient,timeinc=0):
|
||||||
"Return number of allowed messages for greylist triple."
|
"Return number of allowed messages for greylist triple."
|
||||||
sender = quoteAddress(sender)
|
sender = quoteAddress(sender)
|
||||||
recipient = quoteAddress(recipient)
|
recipient = quoteAddress(recipient)
|
||||||
@@ -45,15 +70,15 @@ class Greylist(object):
|
|||||||
dbp = self.dbp
|
dbp = self.dbp
|
||||||
try:
|
try:
|
||||||
r = dbp[key]
|
r = dbp[key]
|
||||||
now = time.time()
|
now = time.time() + timeinc
|
||||||
if now > r.lastseen + self.greylist_retain:
|
if now > r.lastseen + self.greylist_retain:
|
||||||
# expired
|
# expired
|
||||||
log.debug('Expired greylist: %s',key)
|
log.debug('Expired greylist: %s',key)
|
||||||
r = Record()
|
r = Record(timeinc)
|
||||||
elif now < r.firstseen + self.greylist_time:
|
elif now < r.firstseen + self.greylist_time + 5:
|
||||||
# still greylisted
|
# still greylisted
|
||||||
log.debug('Early greylist: %s',key)
|
log.debug('Early greylist: %s',key)
|
||||||
#r = Record()
|
#r = Record(timeinc)
|
||||||
r.lastseen = now
|
r.lastseen = now
|
||||||
elif r.cnt or now < r.firstseen + self.greylist_expire:
|
elif r.cnt or now < r.firstseen + self.greylist_expire:
|
||||||
# in greylist window or active
|
# in greylist window or active
|
||||||
@@ -63,12 +88,15 @@ class Greylist(object):
|
|||||||
else:
|
else:
|
||||||
# passed greylist window
|
# passed greylist window
|
||||||
log.debug('Late greylist: %s',key)
|
log.debug('Late greylist: %s',key)
|
||||||
r = Record()
|
r = Record(timeinc)
|
||||||
dbp[key] = r
|
dbp[key] = r
|
||||||
except:
|
except:
|
||||||
r = Record()
|
r = Record(timeinc)
|
||||||
dbp[key] = r
|
dbp[key] = r
|
||||||
dbp.sync()
|
dbp.sync()
|
||||||
finally:
|
finally:
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
return r.cnt
|
return r.cnt
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.dbp.close()
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
import time
|
||||||
|
import logging
|
||||||
|
import urllib
|
||||||
|
import sqlite3
|
||||||
|
import thread
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
log = logging.getLogger('milter.greylist')
|
||||||
|
|
||||||
|
_db_lock = thread.allocate_lock()
|
||||||
|
|
||||||
|
class Greylist(object):
|
||||||
|
|
||||||
|
def __init__(self,dbname,grey_time=10,grey_expire=4,grey_retain=36):
|
||||||
|
self.ignoreLastByte = False
|
||||||
|
self.greylist_time = grey_time * 60 # minutes
|
||||||
|
self.greylist_expire = grey_expire * 3600 # hours
|
||||||
|
self.greylist_retain = grey_retain * 24 * 3600 # days
|
||||||
|
self.conn = sqlite3.connect(dbname)
|
||||||
|
self.conn.row_factory = sqlite3.Row
|
||||||
|
try:
|
||||||
|
self.conn.execute('''create table greylist(
|
||||||
|
ip text , sender text, recipient text,
|
||||||
|
firstseen timestamp, lastseen timestamp, cnt integer, umis text,
|
||||||
|
primary key (ip,sender,recipient))''')
|
||||||
|
except: pass
|
||||||
|
|
||||||
|
def clean(self,timeinc=0):
|
||||||
|
"Delete records past the retention limit."
|
||||||
|
now = time.time() + timeinc - self.greylist_retain
|
||||||
|
cur = self.conn.cursor()
|
||||||
|
try:
|
||||||
|
cur.execute('delete from greylist where lastseen < ?',(now,))
|
||||||
|
cnt = cur.rowcount
|
||||||
|
self.conn.commit()
|
||||||
|
finally: cur.close()
|
||||||
|
return cnt
|
||||||
|
|
||||||
|
def check(self,ip,sender,recipient,timeinc=0):
|
||||||
|
"Return number of allowed messages for greylist triple."
|
||||||
|
_db_lock.acquire()
|
||||||
|
cur = self.conn.execute('begin immediate')
|
||||||
|
try:
|
||||||
|
cur.execute('''select firstseen,lastseen,cnt,umis from greylist where
|
||||||
|
ip=? and sender=? and recipient=?''',(ip,sender,recipient))
|
||||||
|
r = cur.fetchone()
|
||||||
|
now = time.time() + timeinc
|
||||||
|
cnt = 0
|
||||||
|
if not r:
|
||||||
|
cur.execute('''insert into
|
||||||
|
greylist(ip,sender,recipient,firstseen,lastseen,cnt,umis)
|
||||||
|
values(?,?,?,?,?,?,?)''', (ip,sender,recipient,now,now,0,None))
|
||||||
|
elif now > r['lastseen'] + self.greylist_retain:
|
||||||
|
# expired
|
||||||
|
log.debug('Expired greylist: %s:%s:%s',ip,sender,recipient)
|
||||||
|
cur.execute('''update greylist set firstseen=?,lastseen=?,cnt=?,umis=?
|
||||||
|
where ip=? and sender=? and recipient=?''',
|
||||||
|
(now,now,0,None,ip,sender,recipient))
|
||||||
|
elif now < r['firstseen'] + self.greylist_time + 5:
|
||||||
|
# still greylisted
|
||||||
|
log.debug('Early greylist: %s:%s:%s',ip,sender,recipient)
|
||||||
|
#r = Record()
|
||||||
|
cur.execute('''update greylist set lastseen=?
|
||||||
|
where ip=? and sender=? and recipient=?''',
|
||||||
|
(now,ip,sender,recipient))
|
||||||
|
elif r['cnt'] or now < r['firstseen'] + self.greylist_expire:
|
||||||
|
# in greylist window or active
|
||||||
|
cnt = r['cnt'] + 1
|
||||||
|
cur.execute('''update greylist set lastseen=?,cnt=?
|
||||||
|
where ip=? and sender=? and recipient=?''',
|
||||||
|
(now,cnt,ip,sender,recipient))
|
||||||
|
log.debug('Active greylist(%d): %s:%s:%s',cnt,ip,sender,recipient)
|
||||||
|
else:
|
||||||
|
# passed greylist window
|
||||||
|
log.debug('Late greylist: %s:%s:%s',ip,sender,recipient)
|
||||||
|
cur.execute('''update greylist set firstseen=?,lastseen=?,cnt=?,umis=?
|
||||||
|
where ip=? and sender=? and recipient=?''',
|
||||||
|
(now,now,0,None,ip,sender,recipient))
|
||||||
|
self.conn.commit()
|
||||||
|
finally:
|
||||||
|
cur.close()
|
||||||
|
_db_lock.release()
|
||||||
|
return cnt
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.conn.close()
|
||||||
+3
-3
@@ -31,8 +31,8 @@ class PLock(object):
|
|||||||
os.chown(self.lockname,-1,st.st_gid)
|
os.chown(self.lockname,-1,st.st_gid)
|
||||||
except:
|
except:
|
||||||
if strict_perms:
|
if strict_perms:
|
||||||
self.unlock()
|
self.unlock()
|
||||||
raise
|
raise
|
||||||
return self.fp
|
return self.fp
|
||||||
|
|
||||||
def wlock(self,lockname=None):
|
def wlock(self,lockname=None):
|
||||||
@@ -51,7 +51,7 @@ class PLock(object):
|
|||||||
self.fp = None
|
self.fp = None
|
||||||
if backname:
|
if backname:
|
||||||
try:
|
try:
|
||||||
os.remove(backname)
|
os.remove(backname)
|
||||||
except OSError: pass
|
except OSError: pass
|
||||||
os.link(self.basename,backname)
|
os.link(self.basename,backname)
|
||||||
os.rename(self.lockname,self.basename)
|
os.rename(self.lockname,self.basename)
|
||||||
|
|||||||
+21
-21
@@ -48,11 +48,11 @@ def inet_ntop(s):
|
|||||||
e = n[:l]
|
e = n[:l]
|
||||||
for i in range(9-l):
|
for i in range(9-l):
|
||||||
if a[i:i+l] == e:
|
if a[i:i+l] == e:
|
||||||
if i == 0:
|
if i == 0:
|
||||||
return ':'+':%x'*(8-l) % a[l:]
|
return ':'+':%x'*(8-l) % a[l:]
|
||||||
if i == 8 - l:
|
if i == 8 - l:
|
||||||
return '%x:'*(8-l) % a[:-l] + ':'
|
return '%x:'*(8-l) % a[:-l] + ':'
|
||||||
return '%x:'*i % a[:i] + ':%x'*(8-l-i) % a[i+l:]
|
return '%x:'*i % a[:i] + ':%x'*(8-l-i) % a[i+l:]
|
||||||
return "%x:%x:%x:%x:%x:%x:%x:%x" % a
|
return "%x:%x:%x:%x:%x:%x:%x:%x" % a
|
||||||
|
|
||||||
def inet_pton(p):
|
def inet_pton(p):
|
||||||
@@ -89,29 +89,29 @@ def inet_pton(p):
|
|||||||
m = RE_IP4.search(s)
|
m = RE_IP4.search(s)
|
||||||
try:
|
try:
|
||||||
if m:
|
if m:
|
||||||
pos = m.start()
|
pos = m.start()
|
||||||
ip4 = [int(i) for i in s[pos:].split('.')]
|
ip4 = [int(i) for i in s[pos:].split('.')]
|
||||||
if not pos:
|
if not pos:
|
||||||
return struct.pack('!QLBBBB',0,65535,*ip4)
|
return struct.pack('!QLBBBB',0,65535,*ip4)
|
||||||
s = s[:pos]+'%x%02x:%x%02x'%tuple(ip4)
|
s = s[:pos]+'%x%02x:%x%02x'%tuple(ip4)
|
||||||
a = s.split('::')
|
a = s.split('::')
|
||||||
if len(a) == 2:
|
if len(a) == 2:
|
||||||
l,r = a
|
l,r = a
|
||||||
if not l:
|
if not l:
|
||||||
r = r.split(':')
|
r = r.split(':')
|
||||||
return struct.pack('!HHHHHHHH',
|
return struct.pack('!HHHHHHHH',
|
||||||
*[0]*(8-len(r)) + [int(s,16) for s in r])
|
*[0]*(8-len(r)) + [int(s,16) for s in r])
|
||||||
if not r:
|
if not r:
|
||||||
l = l.split(':')
|
l = l.split(':')
|
||||||
return struct.pack('!HHHHHHHH',
|
return struct.pack('!HHHHHHHH',
|
||||||
*[int(s,16) for s in l] + [0]*(8-len(l)))
|
*[int(s,16) for s in l] + [0]*(8-len(l)))
|
||||||
l = l.split(':')
|
l = l.split(':')
|
||||||
r = r.split(':')
|
r = r.split(':')
|
||||||
return struct.pack('!HHHHHHHH',
|
return struct.pack('!HHHHHHHH',
|
||||||
*[int(s,16) for s in l] + [0]*(8-len(l)-len(r))
|
*[int(s,16) for s in l] + [0]*(8-len(l)-len(r))
|
||||||
+ [int(s,16) for s in r])
|
+ [int(s,16) for s in r])
|
||||||
if len(a) == 1:
|
if len(a) == 1:
|
||||||
return struct.pack('!HHHHHHHH',
|
return struct.pack('!HHHHHHHH',
|
||||||
*[int(s,16) for s in a[0].split(':')])
|
*[int(s,16) for s in a[0].split(':')])
|
||||||
except ValueError: pass
|
except ValueError: pass
|
||||||
raise ValueError,p
|
raise ValueError,p
|
||||||
|
|||||||
+192
@@ -0,0 +1,192 @@
|
|||||||
|
## @package Milter.test
|
||||||
|
# A test framework for milters
|
||||||
|
|
||||||
|
import rfc822
|
||||||
|
import StringIO
|
||||||
|
import Milter
|
||||||
|
|
||||||
|
Milter.NOREPLY = Milter.CONTINUE
|
||||||
|
|
||||||
|
## Test mixin for unit testing milter applications.
|
||||||
|
# This mixin overrides many Milter.MilterBase methods
|
||||||
|
# with stub versions that simply record what was done.
|
||||||
|
# @since 0.9.8
|
||||||
|
class TestBase(object):
|
||||||
|
|
||||||
|
def __init__(self,logfile='test/milter.log'):
|
||||||
|
self._protocol = 0
|
||||||
|
self.logfp = open(logfile,"a")
|
||||||
|
## List of recipients deleted
|
||||||
|
self._delrcpt = []
|
||||||
|
## List of recipients added
|
||||||
|
self._addrcpt = []
|
||||||
|
## Macros defined
|
||||||
|
self._macros = { }
|
||||||
|
## The message body.
|
||||||
|
self._body = None
|
||||||
|
## True if the milter replaced the message body.
|
||||||
|
self._bodyreplaced = False
|
||||||
|
## True if the milter changed any headers.
|
||||||
|
self._headerschanged = False
|
||||||
|
## Reply codes and messages set by milter
|
||||||
|
self._reply = None
|
||||||
|
## The rfc822 message object for the current email being fed to the milter.
|
||||||
|
self._msg = None
|
||||||
|
self._symlist = [ None, None, None, None, None, None, None ]
|
||||||
|
|
||||||
|
def log(self,*msg):
|
||||||
|
for i in msg: print >>self.logfp, i,
|
||||||
|
print >>self.logfp
|
||||||
|
|
||||||
|
## Set a macro value.
|
||||||
|
# These are retrieved by the milter with getsymval.
|
||||||
|
# @param name the macro name, as passed to getsymval
|
||||||
|
# @param val the macro value
|
||||||
|
def setsymval(self,name,val):
|
||||||
|
self._macros[name] = val
|
||||||
|
|
||||||
|
def getsymval(self,name):
|
||||||
|
# FIXME: track stage, and use _symlist
|
||||||
|
return self._macros.get(name,'')
|
||||||
|
|
||||||
|
def replacebody(self,chunk):
|
||||||
|
if self._body:
|
||||||
|
self._body.write(chunk)
|
||||||
|
self._bodyreplaced = True
|
||||||
|
else:
|
||||||
|
raise IOError,"replacebody not called from eom()"
|
||||||
|
|
||||||
|
# FIXME: rfc822 indexing does not really reflect the way chg/add header
|
||||||
|
# work for a milter
|
||||||
|
def chgheader(self,field,idx,value):
|
||||||
|
if not self._body:
|
||||||
|
raise IOError,"chgheader not called from eom()"
|
||||||
|
self.log('chgheader: %s[%d]=%s' % (field,idx,value))
|
||||||
|
if value == '':
|
||||||
|
del self._msg[field]
|
||||||
|
else:
|
||||||
|
self._msg[field] = value
|
||||||
|
self._headerschanged = True
|
||||||
|
|
||||||
|
def addheader(self,field,value,idx=-1):
|
||||||
|
if not self._body:
|
||||||
|
raise IOError,"addheader not called from eom()"
|
||||||
|
self.log('addheader: %s=%s' % (field,value))
|
||||||
|
self._msg[field] = value
|
||||||
|
self._headerschanged = True
|
||||||
|
|
||||||
|
def delrcpt(self,rcpt):
|
||||||
|
if not self._body:
|
||||||
|
raise IOError,"delrcpt not called from eom()"
|
||||||
|
self._delrcpt.append(rcpt)
|
||||||
|
|
||||||
|
def addrcpt(self,rcpt):
|
||||||
|
if not self._body:
|
||||||
|
raise IOError,"addrcpt not called from eom()"
|
||||||
|
self._addrcpt.append(rcpt)
|
||||||
|
|
||||||
|
## Save the reply codes and messages in self._reply.
|
||||||
|
def setreply(self,rcode,xcode,*msg):
|
||||||
|
self._reply = (rcode,xcode) + msg
|
||||||
|
|
||||||
|
def setsymlist(self,stage,macros):
|
||||||
|
if not self._actions & SETSYMLIST: raise DisabledAction("SETSYMLIST")
|
||||||
|
# not used yet, but just for grins we save the data
|
||||||
|
a = []
|
||||||
|
for m in macros:
|
||||||
|
try:
|
||||||
|
m = m.encode('utf8')
|
||||||
|
except: pass
|
||||||
|
try:
|
||||||
|
m = m.split(' ')
|
||||||
|
except: pass
|
||||||
|
a += m
|
||||||
|
self._symlist[stage] = set(a)
|
||||||
|
|
||||||
|
## Feed a file like object to the milter. Calls envfrom, envrcpt for
|
||||||
|
# each recipient, header for each header field, body for each body
|
||||||
|
# block, and finally eom. A return code from the milter other than
|
||||||
|
# CONTINUE returns immediately with that return code.
|
||||||
|
#
|
||||||
|
# This is a convenience method, a test could invoke the callbacks
|
||||||
|
# in sequence on its own - and for some complex tests, this may
|
||||||
|
# be necessary.
|
||||||
|
# @param fp the file with rfc2822 message stream
|
||||||
|
# @param sender the MAIL FROM
|
||||||
|
# @param rcpt RCPT TO - additional recipients may follow
|
||||||
|
def feedFile(self,fp,sender="spam@adv.com",rcpt="victim@lamb.com",*rcpts):
|
||||||
|
self._body = None
|
||||||
|
self._bodyreplaced = False
|
||||||
|
self._headerschanged = False
|
||||||
|
self._reply = None
|
||||||
|
msg = rfc822.Message(fp)
|
||||||
|
rc = self.envfrom('<%s>'%sender)
|
||||||
|
if rc != Milter.CONTINUE: return rc
|
||||||
|
for rcpt in (rcpt,) + rcpts:
|
||||||
|
rc = self.envrcpt('<%s>'%rcpt)
|
||||||
|
if rc != Milter.CONTINUE: return rc
|
||||||
|
line = None
|
||||||
|
for h in msg.headers:
|
||||||
|
if h[:1].isspace():
|
||||||
|
line = line + h
|
||||||
|
continue
|
||||||
|
if not line:
|
||||||
|
line = h
|
||||||
|
continue
|
||||||
|
s = line.split(': ',1)
|
||||||
|
if len(s) > 1: val = s[1].strip()
|
||||||
|
else: val = ''
|
||||||
|
rc = self.header(s[0],val)
|
||||||
|
if rc != Milter.CONTINUE: return rc
|
||||||
|
line = h
|
||||||
|
if line:
|
||||||
|
s = line.split(': ',1)
|
||||||
|
rc = self.header(s[0],s[1])
|
||||||
|
if rc != Milter.CONTINUE: return rc
|
||||||
|
rc = self.eoh()
|
||||||
|
if rc != Milter.CONTINUE: return rc
|
||||||
|
while 1:
|
||||||
|
buf = fp.read(8192)
|
||||||
|
if len(buf) == 0: break
|
||||||
|
rc = self.body(buf)
|
||||||
|
if rc != Milter.CONTINUE: return rc
|
||||||
|
self._msg = msg
|
||||||
|
self._body = StringIO.StringIO()
|
||||||
|
rc = self.eom()
|
||||||
|
if self._bodyreplaced:
|
||||||
|
body = self._body.getvalue()
|
||||||
|
else:
|
||||||
|
msg.rewindbody()
|
||||||
|
body = msg.fp.read()
|
||||||
|
self._body = StringIO.StringIO()
|
||||||
|
self._body.writelines(msg.headers)
|
||||||
|
self._body.write('\n')
|
||||||
|
self._body.write(body)
|
||||||
|
return rc
|
||||||
|
|
||||||
|
## Feed an email contained in a file to the milter.
|
||||||
|
# This is a convenience method that invokes @link #feedFile feedFile @endlink.
|
||||||
|
# @param sender MAIL FROM
|
||||||
|
# @param rcpts RCPT TO, multiple recipients may be supplied
|
||||||
|
def feedMsg(self,fname,sender="spam@adv.com",*rcpts):
|
||||||
|
with open('test/'+fname,'r') as fp:
|
||||||
|
return self.feedFile(fp,sender,*rcpts)
|
||||||
|
|
||||||
|
## Call the connect and helo callbacks.
|
||||||
|
# The helo callback is not called if connect does not return CONTINUE.
|
||||||
|
# @param host the hostname passed to the connect callback
|
||||||
|
# @param helo the hostname passed to the helo callback
|
||||||
|
# @param ip the IP address passed to the connect callback
|
||||||
|
def connect(self,host='localhost',helo='spamrelay',ip='1.2.3.4'):
|
||||||
|
self._body = None
|
||||||
|
self._bodyreplaced = False
|
||||||
|
opts = [ Milter.CURR_ACTS,~0,0,0 ]
|
||||||
|
rc = self.negotiate(opts)
|
||||||
|
rc = super(TestBase,self).connect(host,1,(ip,1234))
|
||||||
|
if rc != Milter.CONTINUE:
|
||||||
|
self.close()
|
||||||
|
return rc
|
||||||
|
rc = self.hello(helo)
|
||||||
|
if rc != Milter.CONTINUE:
|
||||||
|
self.close()
|
||||||
|
return rc
|
||||||
+19
-13
@@ -28,23 +28,28 @@ ip6re = re.compile( '(?:%(hex4)s:){6}%(ls32)s$'
|
|||||||
}, re.IGNORECASE)
|
}, re.IGNORECASE)
|
||||||
|
|
||||||
# from spf.py
|
# from spf.py
|
||||||
def addr2bin(str):
|
def addr2bin(s):
|
||||||
"""Convert a string IPv4 address into an unsigned integer."""
|
"""Convert a string IPv4 address into an unsigned integer."""
|
||||||
|
if s.find(':') >= 0:
|
||||||
|
try:
|
||||||
|
return bin2long6(inet_pton(s))
|
||||||
|
except:
|
||||||
|
raise socket.error("Invalid IP6 address: "+s)
|
||||||
try:
|
try:
|
||||||
return struct.unpack("!L", socket.inet_aton(str))[0]
|
return struct.unpack("!L", socket.inet_aton(s))[0]
|
||||||
except socket.error:
|
except socket.error:
|
||||||
raise socket.error("Invalid IP4 address: "+str)
|
raise socket.error("Invalid IP4 address: "+s)
|
||||||
|
|
||||||
def bin2long6(str):
|
def bin2long6(s):
|
||||||
"""Convert binary IP6 address into an unsigned Python long integer."""
|
"""Convert binary IP6 address into an unsigned Python long integer."""
|
||||||
h, l = struct.unpack("!QQ", str)
|
h, l = struct.unpack("!QQ", s)
|
||||||
return h << 64 | l
|
return h << 64 | l
|
||||||
|
|
||||||
if hasattr(socket,'has_ipv6') and socket.has_ipv6:
|
if hasattr(socket,'has_ipv6') and socket.has_ipv6:
|
||||||
def inet_ntop(s):
|
def inet_ntop(s):
|
||||||
return socket.inet_ntop(socket.AF_INET6,s)
|
return socket.inet_ntop(socket.AF_INET6,s)
|
||||||
def inet_pton(s):
|
def inet_pton(s):
|
||||||
return socket.inet_pton(socket.AF_INET6,s)
|
return socket.inet_pton(socket.AF_INET6,s.strip())
|
||||||
else:
|
else:
|
||||||
from pyip6 import inet_ntop, inet_pton
|
from pyip6 import inet_ntop, inet_pton
|
||||||
|
|
||||||
@@ -79,14 +84,14 @@ def iniplist(ipaddr,iplist):
|
|||||||
p = pat.split('/',1)
|
p = pat.split('/',1)
|
||||||
if ip4re.match(p[0]):
|
if ip4re.match(p[0]):
|
||||||
if len(p) > 1:
|
if len(p) > 1:
|
||||||
n = int(p[1])
|
n = int(p[1])
|
||||||
else:
|
else:
|
||||||
n = 32
|
n = 32
|
||||||
if cidr(addr2bin(p[0]),n) == cidr(ipnum,n):
|
if cidr(addr2bin(p[0]),n) == cidr(ipnum,n):
|
||||||
return True
|
return True
|
||||||
elif ip6re.match(p[0]):
|
elif ip6re.match(p[0]):
|
||||||
if len(p) > 1:
|
if len(p) > 1:
|
||||||
n = int(p[1])
|
n = int(p[1])
|
||||||
else:
|
else:
|
||||||
n = 128
|
n = 128
|
||||||
if cidr(bin2long6(inet_pton(p[0])),n,MASK6) == cidr(ipnum,n,MASK6):
|
if cidr(bin2long6(inet_pton(p[0])),n,MASK6) == cidr(ipnum,n,MASK6):
|
||||||
@@ -180,17 +185,18 @@ def parse_header(val):
|
|||||||
for s,enc in h:
|
for s,enc in h:
|
||||||
if enc:
|
if enc:
|
||||||
try:
|
try:
|
||||||
u.append(unicode(s,enc))
|
u.append(unicode(s,enc,'replace'))
|
||||||
except LookupError:
|
except LookupError:
|
||||||
u.append(unicode(s))
|
u.append(unicode(s))
|
||||||
else:
|
else:
|
||||||
u.append(unicode(s))
|
u.append(unicode(s))
|
||||||
u = ''.join(u)
|
u = ''.join(u)
|
||||||
for enc in ('us-ascii','iso-8859-1','utf8'):
|
for enc in ('us-ascii','iso-8859-1','utf8'):
|
||||||
try:
|
try:
|
||||||
return u.encode(enc)
|
return u.encode(enc)
|
||||||
except UnicodeError: continue
|
except UnicodeError: continue
|
||||||
except UnicodeDecodeError: pass
|
except UnicodeDecodeError: pass
|
||||||
except LookupError: pass
|
except LookupError: pass
|
||||||
|
except ValueError: pass
|
||||||
except email.Errors.HeaderParseError: pass
|
except email.Errors.HeaderParseError: pass
|
||||||
return val
|
return val
|
||||||
|
|||||||
@@ -69,8 +69,7 @@ Not-so-quick Installation
|
|||||||
First install Sendmail. Make sure you read libmilter/README in the Sendmail
|
First install Sendmail. Make sure you read libmilter/README in the Sendmail
|
||||||
source directory, and make sure you enable libmilter before you build. The
|
source directory, and make sure you enable libmilter before you build. The
|
||||||
8.11 series had libmilter marked as FFR (For Future Release); 8.12
|
8.11 series had libmilter marked as FFR (For Future Release); 8.12
|
||||||
officially
|
officially supports libmilter, but it's still not built by default.
|
||||||
supports libmilter, but it's still not built by default.
|
|
||||||
|
|
||||||
Install Python, and enable threading in Modules/Setup.
|
Install Python, and enable threading in Modules/Setup.
|
||||||
|
|
||||||
|
|||||||
+18
-1
@@ -1,6 +1,5 @@
|
|||||||
## @mainpage Writing Milters in Python
|
## @mainpage Writing Milters in Python
|
||||||
#
|
#
|
||||||
#
|
|
||||||
# At the lowest level, the <code>milter</code> module provides a thin wrapper
|
# At the lowest level, the <code>milter</code> module provides a thin wrapper
|
||||||
# around the <a href="https://www.milter.org/developers/api/index"> sendmail
|
# around the <a href="https://www.milter.org/developers/api/index"> sendmail
|
||||||
# libmilter API</a>. This API lets you register callbacks for a number of
|
# libmilter API</a>. This API lets you register callbacks for a number of
|
||||||
@@ -34,3 +33,21 @@
|
|||||||
# The <code>mime</code> module provides a wrapper for the Python email package
|
# The <code>mime</code> module provides a wrapper for the Python email package
|
||||||
# that fixes some bugs, and simplifies modifying selected parts of a MIME
|
# that fixes some bugs, and simplifies modifying selected parts of a MIME
|
||||||
# message.
|
# message.
|
||||||
|
#
|
||||||
|
# @section threading
|
||||||
|
#
|
||||||
|
# The libmilter library which pymilter wraps
|
||||||
|
# <a href="https://www.milter.org/developers/overview#SignalHandling">handles
|
||||||
|
# all signals</a> itself, and expects to be called from a single main thread.
|
||||||
|
# It handles SIGTERM, SIGHUP, and SIGINT, mapping the first two to
|
||||||
|
# <a href="https://www.milter.org/developers/api/smfi_stop">smfi_stop</a>
|
||||||
|
# and the last to an internal ABORT.
|
||||||
|
#
|
||||||
|
# If you use python threads or threading modules, then signal handling gets
|
||||||
|
# confused. Threads may still be useful, but you may need to provide an
|
||||||
|
# alternate means of causing graceful shutdown.
|
||||||
|
#
|
||||||
|
# You may find the
|
||||||
|
# <a href="http://docs.python.org/release/2.6.6/library/multiprocessing.html">
|
||||||
|
# multiprocessing</a> module useful. It can be a drop-in
|
||||||
|
# replacement for threading as illustrated in @ref milter-template.py.
|
||||||
|
|||||||
+80
-3
@@ -20,23 +20,57 @@
|
|||||||
# and converts function callbacks to instance method invocations.
|
# and converts function callbacks to instance method invocations.
|
||||||
#
|
#
|
||||||
class milterContext(object):
|
class milterContext(object):
|
||||||
|
## Calls <a href="https://www.milter.org/developers/api/smfi_getsymval">smfi_getsymval</a>.
|
||||||
def getsymval(self,sym): pass
|
def getsymval(self,sym): pass
|
||||||
|
## Calls <a href="https://www.milter.org/developers/api/smfi_setreply">
|
||||||
|
# smfi_setreply</a> or
|
||||||
|
# <a href="https://www.milter.org/developers/api/smfi_setmlreply">
|
||||||
|
# smfi_setmlreply</a>.
|
||||||
|
# @param rcode SMTP response code
|
||||||
|
# @param xcode extended SMTP response code
|
||||||
|
# @param msg one or more message lines. If the MTA does not support
|
||||||
|
# multiline messages, only the first is used.
|
||||||
def setreply(self,rcode,xcode,*msg): pass
|
def setreply(self,rcode,xcode,*msg): pass
|
||||||
|
## Calls <a href="https://www.milter.org/developers/api/smfi_addheader">smfi_addheader</a>.
|
||||||
def addheader(self,name,value,idx=-1): pass
|
def addheader(self,name,value,idx=-1): pass
|
||||||
|
## Calls <a href="https://www.milter.org/developers/api/smfi_chgheader">smfi_chgheader</a>.
|
||||||
def chgheader(self,name,idx,value): pass
|
def chgheader(self,name,idx,value): pass
|
||||||
|
## Calls <a href="https://www.milter.org/developers/api/smfi_addrcpt">smfi_addrcpt</a>.
|
||||||
def addrcpt(self,rcpt,params=None): pass
|
def addrcpt(self,rcpt,params=None): pass
|
||||||
|
## Calls <a href="https://www.milter.org/developers/api/smfi_delrcpt">smfi_delrcpt</a>.
|
||||||
def delrcpt(self,rcpt): pass
|
def delrcpt(self,rcpt): pass
|
||||||
|
## Calls <a href="https://www.milter.org/developers/api/smfi_replacebody">smfi_replacebody</a>.
|
||||||
def replacebody(self,data): pass
|
def replacebody(self,data): pass
|
||||||
|
## Attach a Python object to this connection context.
|
||||||
|
# @return the old value or None
|
||||||
def setpriv(self,priv): pass
|
def setpriv(self,priv): pass
|
||||||
|
## Return the Python object attached to this connection context.
|
||||||
def getpriv(self): pass
|
def getpriv(self): pass
|
||||||
|
## Calls <a href="https://www.milter.org/developers/api/smfi_quarantine">smfi_quarantine</a>.
|
||||||
def quarantine(self,reason): pass
|
def quarantine(self,reason): pass
|
||||||
|
## Calls <a href="https://www.milter.org/developers/api/smfi_progress">smfi_progress</a>.
|
||||||
def progress(self): pass
|
def progress(self): pass
|
||||||
|
## Calls <a href="https://www.milter.org/developers/api/smfi_chgfrom">smfi_chgfrom</a>.
|
||||||
def chgfrom(self,sender,param=None): pass
|
def chgfrom(self,sender,param=None): pass
|
||||||
def setsmlist(self,stage,macrolist): pass
|
## Tell the MTA which macro values we are interested in for a given stage.
|
||||||
|
# Of interest only when you need to squeeze a few more bytes of bandwidth.
|
||||||
|
# It may only be called from the negotiate callback.
|
||||||
|
# The protocol stages are
|
||||||
|
# M_CONNECT, M_HELO, M_ENVFROM, M_ENVRCPT, M_DATA, M_EOM, M_EOH.
|
||||||
|
# Calls <a href="https://www.milter.org/developers/api/smfi_setsymlist">smfi_setsymlist</a>.
|
||||||
|
# @param stage protocol stage in which the macro list should be used
|
||||||
|
def setsymlist(self,stage,macrolist): pass
|
||||||
|
|
||||||
class error(Exception): pass
|
class error(Exception): pass
|
||||||
|
|
||||||
|
## Enable optional milter actions.
|
||||||
|
# Certain milter actions need to be enabled before calling main()
|
||||||
|
# or they throw an exception. Pymilter enables them all by
|
||||||
|
# default (since 0.9.2), but you may wish to disable unneeded
|
||||||
|
# actions as an optimization.
|
||||||
|
# @param flags Bit or mask of optional actions to enable
|
||||||
def set_flags(flags): pass
|
def set_flags(flags): pass
|
||||||
|
|
||||||
def set_connect_callback(cb): pass
|
def set_connect_callback(cb): pass
|
||||||
def set_helo_callback(cb): pass
|
def set_helo_callback(cb): pass
|
||||||
def set_envfrom_callback(cb): pass
|
def set_envfrom_callback(cb): pass
|
||||||
@@ -46,19 +80,62 @@ def set_eoh_callback(cb): pass
|
|||||||
def set_body_callback(cb): pass
|
def set_body_callback(cb): pass
|
||||||
def set_abort_callback(cb): pass
|
def set_abort_callback(cb): pass
|
||||||
def set_close_callback(cb): pass
|
def set_close_callback(cb): pass
|
||||||
|
|
||||||
|
## Sets the return code for untrapped Python exceptions during a callback.
|
||||||
|
# Must be one of TEMPFAIL,REJECT,CONTINUE. The default is TEMPFAIL.
|
||||||
|
# You should not depend on this handler. Your application should
|
||||||
|
# have its own top level exception handler for each callback. You can
|
||||||
|
# then choose your own reply message, log the stack track were you please,
|
||||||
|
# and so on. However, if you miss one, this last ditch handler will
|
||||||
|
# print a standard stack trace to sys.stderr, and return to sendmail.
|
||||||
def set_exception_policy(code): pass
|
def set_exception_policy(code): pass
|
||||||
|
|
||||||
|
## Register python milter with libmilter.
|
||||||
|
# The name we pass is used to identify the milter in the MTA configuration.
|
||||||
|
# Callback functions must be set using the set_*_callback() functions before
|
||||||
|
# registering the milter.
|
||||||
|
# Three additional callbacks are specified as keyword parameters. These
|
||||||
|
# were added by recent versions of libmilter. The keyword parameters is
|
||||||
|
# a nicer way to do it, I think, since it makes clear that you have to do
|
||||||
|
# it before registering. I may move all the callbacks
|
||||||
|
# in the future (perhaps keeping the set functions for compatibility).
|
||||||
|
# @param name the milter name by which the MTA finds us
|
||||||
|
# @param negotiate the
|
||||||
|
# <a href="https://www.milter.org/developers/api/xxfi_negotiate">
|
||||||
|
# xxfi_negotiate</a> callback, called to negotiate supported
|
||||||
|
# actions, callbacks, and protocol steps.
|
||||||
|
# @param unknown the
|
||||||
|
# <a href="https://www.milter.org/developers/api/xxfi_unknown">
|
||||||
|
# xxfi_unknown</a> callback, called when for SMTP commands
|
||||||
|
# not recognized by the MTA. (Extend SMTP in your milter!)
|
||||||
|
# @param data the
|
||||||
|
# <a href="https://www.milter.org/developers/api/xxfi_data">
|
||||||
|
# xxfi_data</a> callback, called when the DATA
|
||||||
|
# SMTP command is received.
|
||||||
def register(name,negotiate=None,unknown=None,data=None): pass
|
def register(name,negotiate=None,unknown=None,data=None): pass
|
||||||
def opensocket(rmsock): pass
|
def opensocket(rmsock): pass
|
||||||
|
|
||||||
|
## Transfer control to libmilter.
|
||||||
|
# Calls <a href="https://www.milter.org/developers/api/smfi_main">
|
||||||
|
# smfi_main</a>.
|
||||||
def main(): pass
|
def main(): pass
|
||||||
|
|
||||||
## Set the libmilter debugging level.
|
## Set the libmilter debugging level.
|
||||||
# smfi_setdbg sets the milter library's internal debugging level to a new level
|
# <a href="https://www.milter.org/developers/api/smfi_setdbg">smfi_setdbg</a>
|
||||||
|
# sets the milter library's internal debugging level to a new level
|
||||||
# so that code details may be traced. A level of zero turns off debugging. The
|
# so that code details may be traced. A level of zero turns off debugging. The
|
||||||
# greater (more positive) the level the more detailed the debugging. Six is the
|
# greater (more positive) the level the more detailed the debugging. Six is the
|
||||||
# current, highest, useful value.
|
# current, highest, useful value. Must be called before calling main().
|
||||||
def setdbg(lev): pass
|
def setdbg(lev): pass
|
||||||
|
|
||||||
|
## Set timeout for MTA communication.
|
||||||
|
# Calls <a href="https://www.milter.org/developers/api/smfi_settimeout">
|
||||||
|
# smfi_settimeout</a>. Must be called before calling main().
|
||||||
def settimeout(secs): pass
|
def settimeout(secs): pass
|
||||||
|
|
||||||
|
## Set socket backlog.
|
||||||
|
# Calls <a href="https://www.milter.org/developers/api/smfi_setbacklog">
|
||||||
|
# smfi_setbacklog</a>. Must be called before calling main().
|
||||||
def setbacklog(n): pass
|
def setbacklog(n): pass
|
||||||
|
|
||||||
## Set the socket used to communicate with the MTA.
|
## Set the socket used to communicate with the MTA.
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ web:
|
|||||||
doxygen
|
doxygen
|
||||||
rsync -ravK doc/html/ spidey2.bmsi.com:/Public/pymilter
|
rsync -ravK doc/html/ spidey2.bmsi.com:/Public/pymilter
|
||||||
|
|
||||||
VERSION=0.9.4
|
VERSION=0.9.8
|
||||||
CVSTAG=pymilter-0_9_4
|
CVSTAG=pymilter-0_9_8
|
||||||
PKG=pymilter-$(VERSION)
|
PKG=pymilter-$(VERSION)
|
||||||
SRCTAR=$(PKG).tar.gz
|
SRCTAR=$(PKG).tar.gz
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
## A very simple milter to prevent mixing of internal and external mail.
|
||||||
|
# Internal is defined as using one of a list of internal top level domains.
|
||||||
|
# This code is open-source on the same terms as Python.
|
||||||
|
|
||||||
|
import Milter
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
from Milter.utils import parse_addr
|
||||||
|
|
||||||
|
internal_tlds = ["corp", "personal"]
|
||||||
|
|
||||||
|
## Determine if a hostname is internal or not.
|
||||||
|
# True if internal, False otherwise
|
||||||
|
def is_internal(hostname):
|
||||||
|
components = hostname.split(".")
|
||||||
|
return components.pop() in internal_tlds:
|
||||||
|
|
||||||
|
# Determine if internal and external hosts are mixed based on a list
|
||||||
|
# of hostnames
|
||||||
|
def are_mixed(hostnames):
|
||||||
|
hostnames_mapped = map(is_internal, hostnames)
|
||||||
|
|
||||||
|
# Num internals
|
||||||
|
num_internal_hosts = hostnames_mapped.count(True)
|
||||||
|
|
||||||
|
# Num externals
|
||||||
|
num_external_hosts = hostnames_mapped.count(False)
|
||||||
|
|
||||||
|
return num_external_hosts >= 1 and num_internal_hosts >= 1
|
||||||
|
|
||||||
|
class NoMixMilter(Milter.Base):
|
||||||
|
|
||||||
|
def __init__(self): # A new instance with each new connection.
|
||||||
|
self.id = Milter.uniqueID() # Integer incremented with each call.
|
||||||
|
|
||||||
|
|
||||||
|
## def envfrom(self,f,*str):
|
||||||
|
@Milter.noreply
|
||||||
|
def envfrom(self, mailfrom, *str):
|
||||||
|
self.mailfrom = mailfrom
|
||||||
|
self.domains = []
|
||||||
|
t = parse_addr(mailfrom)
|
||||||
|
if len(t) > 1:
|
||||||
|
self.domains.append(t[1])
|
||||||
|
else:
|
||||||
|
self.domains.append('local')
|
||||||
|
self.internal = False
|
||||||
|
return Milter.CONTINUE
|
||||||
|
|
||||||
|
## def envrcpt(self, to, *str):
|
||||||
|
def envrcpt(self, to, *str):
|
||||||
|
self.R.append(to)
|
||||||
|
t = parse_addr(to)
|
||||||
|
if len(t) > 1:
|
||||||
|
self.domains.append(t[1])
|
||||||
|
else:
|
||||||
|
self.domains.append('local')
|
||||||
|
|
||||||
|
if are_mixed(self.domains):
|
||||||
|
# FIXME: log recipients collected in self.mailfrom and self.R
|
||||||
|
self.setreply('550','5.7.1','Mixing internal and external TLDs')
|
||||||
|
return Milter.REJECT
|
||||||
|
|
||||||
|
return Milter.CONTINUE
|
||||||
|
|
||||||
|
def main():
|
||||||
|
socketname = "/var/run/nomixsock"
|
||||||
|
timeout = 600
|
||||||
|
# Register to have the Milter factory create instances of your class:
|
||||||
|
Milter.factory = NoMixMilter
|
||||||
|
print "%s milter startup" % time.strftime('%Y%b%d %H:%M:%S')
|
||||||
|
sys.stdout.flush()
|
||||||
|
Milter.runmilter("nomixfilter",socketname,timeout)
|
||||||
|
logq.put(None)
|
||||||
|
bt.join()
|
||||||
|
print "%s nomix milter shutdown" % time.strftime('%Y%b%d %H:%M:%S')
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
+28
-10
@@ -11,9 +11,16 @@ import Milter
|
|||||||
import StringIO
|
import StringIO
|
||||||
import time
|
import time
|
||||||
import email
|
import email
|
||||||
|
import sys
|
||||||
from socket import AF_INET, AF_INET6
|
from socket import AF_INET, AF_INET6
|
||||||
from Milter import parse_addr
|
from Milter.utils import parse_addr
|
||||||
|
if True:
|
||||||
|
from multiprocessing import Process as Thread, Queue
|
||||||
|
else:
|
||||||
|
from threading import Thread
|
||||||
|
from Queue import Queue
|
||||||
|
|
||||||
|
logq = Queue(maxsize=4)
|
||||||
|
|
||||||
class myMilter(Milter.Base):
|
class myMilter(Milter.Base):
|
||||||
|
|
||||||
@@ -23,7 +30,7 @@ class myMilter(Milter.Base):
|
|||||||
# each connection runs in its own thread and has its own myMilter
|
# each connection runs in its own thread and has its own myMilter
|
||||||
# instance. Python code must be thread safe. This is trivial if only stuff
|
# instance. Python code must be thread safe. This is trivial if only stuff
|
||||||
# in myMilter instances is referenced.
|
# in myMilter instances is referenced.
|
||||||
@noreply
|
@Milter.noreply
|
||||||
def connect(self, IPname, family, hostaddr):
|
def connect(self, IPname, family, hostaddr):
|
||||||
# (self, 'ip068.subnet71.example.com', AF_INET, ('215.183.71.68', 4720) )
|
# (self, 'ip068.subnet71.example.com', AF_INET, ('215.183.71.68', 4720) )
|
||||||
# (self, 'ip6.mxout.example.com', AF_INET6,
|
# (self, 'ip6.mxout.example.com', AF_INET6,
|
||||||
@@ -71,25 +78,25 @@ class myMilter(Milter.Base):
|
|||||||
|
|
||||||
|
|
||||||
## def envrcpt(self, to, *str):
|
## def envrcpt(self, to, *str):
|
||||||
@noreply
|
@Milter.noreply
|
||||||
def envrcpt(self, recipient, *str):
|
def envrcpt(self, to, *str):
|
||||||
rcptinfo = to,Milter.dictfromlist(str)
|
rcptinfo = to,Milter.dictfromlist(str)
|
||||||
self.R.append(rcptinfo)
|
self.R.append(rcptinfo)
|
||||||
|
|
||||||
return Milter.CONTINUE
|
return Milter.CONTINUE
|
||||||
|
|
||||||
|
|
||||||
@noreply
|
@Milter.noreply
|
||||||
def header(self, name, hval):
|
def header(self, name, hval):
|
||||||
self.fp.write("%s: %s\n" % (name,hval)) # add header to buffer
|
self.fp.write("%s: %s\n" % (name,hval)) # add header to buffer
|
||||||
return Milter.CONTINUE
|
return Milter.CONTINUE
|
||||||
|
|
||||||
@noreply
|
@Milter.noreply
|
||||||
def eoh(self):
|
def eoh(self):
|
||||||
self.fp.write("\n") # terminate headers
|
self.fp.write("\n") # terminate headers
|
||||||
return Milter.CONTINUE
|
return Milter.CONTINUE
|
||||||
|
|
||||||
@noreply
|
@Milter.noreply
|
||||||
def body(self, chunk):
|
def body(self, chunk):
|
||||||
self.fp.write(chunk)
|
self.fp.write(chunk)
|
||||||
return Milter.CONTINUE
|
return Milter.CONTINUE
|
||||||
@@ -103,7 +110,6 @@ class myMilter(Milter.Base):
|
|||||||
self.addrcpt('<%s>' % 'spy@example.com')
|
self.addrcpt('<%s>' % 'spy@example.com')
|
||||||
return Milter.ACCEPT
|
return Milter.ACCEPT
|
||||||
|
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
# always called, even when abort is called. Clean up
|
# always called, even when abort is called. Clean up
|
||||||
# any external resources here.
|
# any external resources here.
|
||||||
@@ -116,15 +122,25 @@ class myMilter(Milter.Base):
|
|||||||
## === Support Functions ===
|
## === Support Functions ===
|
||||||
|
|
||||||
def log(self,*msg):
|
def log(self,*msg):
|
||||||
print "%s [%d]" % (time.strftime('%Y%b%d %H:%M:%S'),self.id),
|
logq.put((msg,self.id,time.time()))
|
||||||
|
|
||||||
|
def background():
|
||||||
|
while True:
|
||||||
|
t = logq.get()
|
||||||
|
if not t: break
|
||||||
|
msg,id,ts = t
|
||||||
|
print "%s [%d]" % (time.strftime('%Y%b%d %H:%M:%S',time.localtime(ts)),id),
|
||||||
# 2005Oct13 02:34:11 [1] msg1 msg2 msg3 ...
|
# 2005Oct13 02:34:11 [1] msg1 msg2 msg3 ...
|
||||||
for i in msg: print i,
|
for i in msg: print i,
|
||||||
print
|
print
|
||||||
|
|
||||||
|
|
||||||
## ===
|
## ===
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
bt = Thread(target=background)
|
||||||
|
bt.start()
|
||||||
|
socketname = "/home/stuart/pythonsock"
|
||||||
|
timeout = 600
|
||||||
# Register to have the Milter factory create instances of your class:
|
# Register to have the Milter factory create instances of your class:
|
||||||
Milter.factory = myMilter
|
Milter.factory = myMilter
|
||||||
flags = Milter.CHGBODY + Milter.CHGHDRS + Milter.ADDHDRS
|
flags = Milter.CHGBODY + Milter.CHGHDRS + Milter.ADDHDRS
|
||||||
@@ -134,6 +150,8 @@ def main():
|
|||||||
print "%s milter startup" % time.strftime('%Y%b%d %H:%M:%S')
|
print "%s milter startup" % time.strftime('%Y%b%d %H:%M:%S')
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
Milter.runmilter("pythonfilter",socketname,timeout)
|
Milter.runmilter("pythonfilter",socketname,timeout)
|
||||||
|
logq.put(None)
|
||||||
|
bt.join()
|
||||||
print "%s bms milter shutdown" % time.strftime('%Y%b%d %H:%M:%S')
|
print "%s bms milter shutdown" % time.strftime('%Y%b%d %H:%M:%S')
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
+146
-67
@@ -35,6 +35,31 @@ $ python setup.py help
|
|||||||
libraries=["milter","smutil","resolv"]
|
libraries=["milter","smutil","resolv"]
|
||||||
|
|
||||||
* $Log$
|
* $Log$
|
||||||
|
* Revision 1.34 2013/03/09 05:42:14 customdesigned
|
||||||
|
* Make TestBase members private, fix getsymlist misspelling.
|
||||||
|
*
|
||||||
|
* Revision 1.33 2013/03/09 00:25:23 customdesigned
|
||||||
|
* Better untrapped exception message. const char for doc comments.
|
||||||
|
*
|
||||||
|
* Revision 1.32 2013/01/13 01:46:16 customdesigned
|
||||||
|
* Doc updates.
|
||||||
|
*
|
||||||
|
* Revision 1.31 2012/04/12 23:32:50 customdesigned
|
||||||
|
* Replace redundant callback array with macros. If this doesn't break anything,
|
||||||
|
* macros can be eliminated with code changes.
|
||||||
|
*
|
||||||
|
* Revision 1.30 2012/04/12 23:08:06 customdesigned
|
||||||
|
* Support RFC2553 on BSD
|
||||||
|
*
|
||||||
|
* Revision 1.29 2011/06/09 15:45:27 customdesigned
|
||||||
|
* Print callback name for non-int return error.
|
||||||
|
*
|
||||||
|
* Revision 1.28 2011/06/08 23:13:48 customdesigned
|
||||||
|
* Generate special exception when callback return not int.
|
||||||
|
*
|
||||||
|
* Revision 1.27 2009/07/28 21:45:54 customdesigned
|
||||||
|
* Add getversion() to return runtime version.
|
||||||
|
*
|
||||||
* Revision 1.26 2009/07/28 21:08:20 customdesigned
|
* Revision 1.26 2009/07/28 21:08:20 customdesigned
|
||||||
* Increment del count.
|
* Increment del count.
|
||||||
*
|
*
|
||||||
@@ -251,10 +276,10 @@ $ python setup.py help
|
|||||||
#define HAVE_IPV6_SUPPORT /* use this for #ifdef's later on */
|
#define HAVE_IPV6_SUPPORT /* use this for #ifdef's later on */
|
||||||
/* Now see if it supports the RFC-2553 socket's API spec. Early
|
/* Now see if it supports the RFC-2553 socket's API spec. Early
|
||||||
* IPv6 "prototype" implementations existed before the RFC was
|
* IPv6 "prototype" implementations existed before the RFC was
|
||||||
* published. Unfortunately I know of now good way to do this
|
* published. Unfortunately I know of no good way to do this
|
||||||
* other than with OS-specific tests.
|
* other than with OS-specific tests.
|
||||||
*/
|
*/
|
||||||
#ifdef linux
|
#if defined(__FreeBSD_kernel__) || defined(__linux__)
|
||||||
#define HAVE_IPV6_RFC2553
|
#define HAVE_IPV6_RFC2553
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -268,19 +293,52 @@ $ python setup.py help
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
enum callbacks {
|
||||||
|
CONNECT,HELO,ENVFROM,ENVRCPT,HEADER,EOH,BODY,EOM,ABORT,CLOSE,
|
||||||
|
#ifdef SMFIS_ALL_OPTS
|
||||||
|
UNKNOWN,DATA,NEGOTIATE,
|
||||||
|
#endif
|
||||||
|
NUMCALLBACKS
|
||||||
|
};
|
||||||
|
|
||||||
/* Yes, these are static. If you need multiple different callbacks, */
|
#define connect_callback callback[CONNECT].cb
|
||||||
/* it's cleaner to use multiple filters, or convert to OO method calls. */
|
#define helo_callback callback[HELO].cb
|
||||||
static PyObject *connect_callback = NULL;
|
#define envfrom_callback callback[ENVFROM].cb
|
||||||
static PyObject *helo_callback = NULL;
|
#define envrcpt_callback callback[ENVRCPT].cb
|
||||||
static PyObject *envfrom_callback = NULL;
|
#define header_callback callback[HEADER].cb
|
||||||
static PyObject *envrcpt_callback = NULL;
|
#define eoh_callback callback[EOH].cb
|
||||||
static PyObject *header_callback = NULL;
|
#define body_callback callback[BODY].cb
|
||||||
static PyObject *eoh_callback = NULL;
|
#define eom_callback callback[EOM].cb
|
||||||
static PyObject *body_callback = NULL;
|
#define abort_callback callback[ABORT].cb
|
||||||
static PyObject *eom_callback = NULL;
|
#define close_callback callback[CLOSE].cb
|
||||||
static PyObject *abort_callback = NULL;
|
#define unknown_callback callback[UNKNOWN].cb
|
||||||
static PyObject *close_callback = NULL;
|
#define data_callback callback[DATA].cb
|
||||||
|
#define negotiate_callback callback[NEGOTIATE].cb
|
||||||
|
|
||||||
|
/* Yes, these are static. If you need multiple different callbacks,
|
||||||
|
it's cleaner to use multiple filters, or convert to OO method calls. */
|
||||||
|
|
||||||
|
static struct MilterCallback {
|
||||||
|
PyObject *cb;
|
||||||
|
const char *name;
|
||||||
|
} callback[NUMCALLBACKS+1] = {
|
||||||
|
{ NULL ,"connect" },
|
||||||
|
{ NULL ,"helo" },
|
||||||
|
{ NULL ,"envfrom" },
|
||||||
|
{ NULL ,"envrcpt" },
|
||||||
|
{ NULL ,"header" },
|
||||||
|
{ NULL ,"eoh" },
|
||||||
|
{ NULL ,"body" },
|
||||||
|
{ NULL ,"eom" },
|
||||||
|
{ NULL ,"abort" },
|
||||||
|
{ NULL ,"close" },
|
||||||
|
#ifdef SMFIS_ALL_OPTS
|
||||||
|
{ NULL ,"unknown" },
|
||||||
|
{ NULL ,"data" },
|
||||||
|
{ NULL ,"negotiate" },
|
||||||
|
#endif
|
||||||
|
{ NULL , NULL }
|
||||||
|
};
|
||||||
|
|
||||||
staticforward struct smfiDesc description; /* forward declaration */
|
staticforward struct smfiDesc description; /* forward declaration */
|
||||||
|
|
||||||
@@ -391,7 +449,7 @@ _thread_return(PyThreadState *t,int val,char *errstr) {
|
|||||||
return _generic_return(val,errstr);
|
return _generic_return(val,errstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_set_flags__doc__[] =
|
static const char milter_set_flags__doc__[] =
|
||||||
"set_flags(int) -> None\n\
|
"set_flags(int) -> None\n\
|
||||||
Set flags for filter capabilities; OR of one or more of:\n\
|
Set flags for filter capabilities; OR of one or more of:\n\
|
||||||
ADDHDRS - filter may add headers\n\
|
ADDHDRS - filter may add headers\n\
|
||||||
@@ -432,7 +490,7 @@ generic_set_callback(PyObject *args,char *t,PyObject **cb) {
|
|||||||
return Py_None;
|
return Py_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_set_connect_callback__doc__[] =
|
static const char milter_set_connect_callback__doc__[] =
|
||||||
"set_connect_callback(Function) -> None\n\
|
"set_connect_callback(Function) -> None\n\
|
||||||
Sets the Python function invoked when a connection is made to sendmail.\n\
|
Sets the Python function invoked when a connection is made to sendmail.\n\
|
||||||
Function takes args (ctx, hostname, integer, hostaddr) -> int\n\
|
Function takes args (ctx, hostname, integer, hostaddr) -> int\n\
|
||||||
@@ -459,7 +517,7 @@ milter_set_connect_callback(PyObject *self, PyObject *args) {
|
|||||||
"O:set_connect_callback", &connect_callback);
|
"O:set_connect_callback", &connect_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_set_helo_callback__doc__[] =
|
static const char milter_set_helo_callback__doc__[] =
|
||||||
"set_helo_callback(Function) -> None\n\
|
"set_helo_callback(Function) -> None\n\
|
||||||
Sets the Python function invoked upon SMTP HELO.\n\
|
Sets the Python function invoked upon SMTP HELO.\n\
|
||||||
Function takes args (ctx, hostname) -> int\n\
|
Function takes args (ctx, hostname) -> int\n\
|
||||||
@@ -470,7 +528,7 @@ milter_set_helo_callback(PyObject *self, PyObject *args) {
|
|||||||
return generic_set_callback(args, "O:set_helo_callback", &helo_callback);
|
return generic_set_callback(args, "O:set_helo_callback", &helo_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_set_envfrom_callback__doc__[] =
|
static const char milter_set_envfrom_callback__doc__[] =
|
||||||
"set_envfrom_callback(Function) -> None\n\
|
"set_envfrom_callback(Function) -> None\n\
|
||||||
Sets the Python function invoked on envelope from.\n\
|
Sets the Python function invoked on envelope from.\n\
|
||||||
Function takes args (ctx, from, *str) -> int\n\
|
Function takes args (ctx, from, *str) -> int\n\
|
||||||
@@ -483,7 +541,7 @@ milter_set_envfrom_callback(PyObject *self, PyObject *args) {
|
|||||||
&envfrom_callback);
|
&envfrom_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_set_envrcpt_callback__doc__[] =
|
static const char milter_set_envrcpt_callback__doc__[] =
|
||||||
"set_envrcpt_callback(Function) -> None\n\
|
"set_envrcpt_callback(Function) -> None\n\
|
||||||
Sets the Python function invoked on each envelope recipient.\n\
|
Sets the Python function invoked on each envelope recipient.\n\
|
||||||
Function takes args (ctx, rcpt, *str) -> int\n\
|
Function takes args (ctx, rcpt, *str) -> int\n\
|
||||||
@@ -496,7 +554,7 @@ milter_set_envrcpt_callback(PyObject *self, PyObject *args) {
|
|||||||
&envrcpt_callback);
|
&envrcpt_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_set_header_callback__doc__[] =
|
static const char milter_set_header_callback__doc__[] =
|
||||||
"set_header_callback(Function) -> None\n\
|
"set_header_callback(Function) -> None\n\
|
||||||
Sets the Python function invoked on each message header.\n\
|
Sets the Python function invoked on each message header.\n\
|
||||||
Function takes args (ctx, field, value) ->int\n\
|
Function takes args (ctx, field, value) ->int\n\
|
||||||
@@ -509,7 +567,7 @@ milter_set_header_callback(PyObject *self, PyObject *args) {
|
|||||||
&header_callback);
|
&header_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_set_eoh_callback__doc__[] =
|
static const char milter_set_eoh_callback__doc__[] =
|
||||||
"set_eoh_callback(Function) -> None\n\
|
"set_eoh_callback(Function) -> None\n\
|
||||||
Sets the Python function invoked at end of header.\n\
|
Sets the Python function invoked at end of header.\n\
|
||||||
Function takes args (ctx) -> int";
|
Function takes args (ctx) -> int";
|
||||||
@@ -519,7 +577,7 @@ milter_set_eoh_callback(PyObject *self, PyObject *args) {
|
|||||||
return generic_set_callback(args, "O:set_eoh_callback", &eoh_callback);
|
return generic_set_callback(args, "O:set_eoh_callback", &eoh_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_set_body_callback__doc__[] =
|
static const char milter_set_body_callback__doc__[] =
|
||||||
"set_body_callback(Function) -> None\n\
|
"set_body_callback(Function) -> None\n\
|
||||||
Sets the Python function invoked for each body chunk. There may\n\
|
Sets the Python function invoked for each body chunk. There may\n\
|
||||||
be multiple body chunks passed to the filter. End-of-lines are\n\
|
be multiple body chunks passed to the filter. End-of-lines are\n\
|
||||||
@@ -532,7 +590,7 @@ milter_set_body_callback(PyObject *self, PyObject *args) {
|
|||||||
return generic_set_callback(args, "O:set_body_callback", &body_callback);
|
return generic_set_callback(args, "O:set_body_callback", &body_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_set_eom_callback__doc__[] =
|
static const char milter_set_eom_callback__doc__[] =
|
||||||
"set_eom_callback(Function) -> None\n\
|
"set_eom_callback(Function) -> None\n\
|
||||||
Sets the Python function invoked at end of message.\n\
|
Sets the Python function invoked at end of message.\n\
|
||||||
This routine is the only place where special operations\n\
|
This routine is the only place where special operations\n\
|
||||||
@@ -545,7 +603,7 @@ milter_set_eom_callback(PyObject *self, PyObject *args) {
|
|||||||
return generic_set_callback(args, "O:set_eom_callback", &eom_callback);
|
return generic_set_callback(args, "O:set_eom_callback", &eom_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_set_abort_callback__doc__[] =
|
static const char milter_set_abort_callback__doc__[] =
|
||||||
"set_abort_callback(Function) -> None\n\
|
"set_abort_callback(Function) -> None\n\
|
||||||
Sets the Python function invoked if message is aborted\n\
|
Sets the Python function invoked if message is aborted\n\
|
||||||
outside of the control of the filter, for example,\n\
|
outside of the control of the filter, for example,\n\
|
||||||
@@ -559,7 +617,7 @@ milter_set_abort_callback(PyObject *self, PyObject *args) {
|
|||||||
return generic_set_callback(args, "O:set_abort_callback", &abort_callback);
|
return generic_set_callback(args, "O:set_abort_callback", &abort_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_set_close_callback__doc__[] =
|
static const char milter_set_close_callback__doc__[] =
|
||||||
"set_close_callback(Function) -> None\n\
|
"set_close_callback(Function) -> None\n\
|
||||||
Sets the Python function invoked at end of the connection. This\n\
|
Sets the Python function invoked at end of the connection. This\n\
|
||||||
is called on close even if the previous mail transaction was aborted.\n\
|
is called on close even if the previous mail transaction was aborted.\n\
|
||||||
@@ -572,7 +630,7 @@ milter_set_close_callback(PyObject *self, PyObject *args) {
|
|||||||
|
|
||||||
static int exception_policy = SMFIS_TEMPFAIL;
|
static int exception_policy = SMFIS_TEMPFAIL;
|
||||||
|
|
||||||
static char milter_set_exception_policy__doc__[] =
|
static const char milter_set_exception_policy__doc__[] =
|
||||||
"set_exception_policy(i) -> None\n\
|
"set_exception_policy(i) -> None\n\
|
||||||
Sets the policy for untrapped Python exceptions during a callback.\n\
|
Sets the policy for untrapped Python exceptions during a callback.\n\
|
||||||
Must be one of TEMPFAIL,REJECT,CONTINUE";
|
Must be one of TEMPFAIL,REJECT,CONTINUE";
|
||||||
@@ -598,19 +656,23 @@ _release_thread(PyThreadState *t) {
|
|||||||
PyEval_ReleaseThread(t);
|
PyEval_ReleaseThread(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Report and clear any python exception before returning to libmilter.
|
/** Report and clear any python exception before returning to libmilter.
|
||||||
The interpreter is locked when we are called, and we unlock it. */
|
The interpreter is locked when we are called, and we unlock it. */
|
||||||
static int _report_exception(milter_ContextObject *self) {
|
static int _report_exception(milter_ContextObject *self) {
|
||||||
|
char untrapped_msg[80];
|
||||||
|
sprintf(untrapped_msg,"pymilter: untrapped exception in %.40s",
|
||||||
|
description.xxfi_name);
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
PyErr_Clear(); /* must clear since not returning to python */
|
PyErr_Clear(); /* must clear since not returning to python */
|
||||||
_release_thread(self->t);
|
_release_thread(self->t);
|
||||||
switch (exception_policy) {
|
switch (exception_policy) {
|
||||||
case SMFIS_REJECT:
|
case SMFIS_REJECT:
|
||||||
smfi_setreply(self->ctx, "554", "5.3.0", "Filter failure");
|
smfi_setreply(self->ctx, "554", "5.3.0", untrapped_msg);
|
||||||
return SMFIS_REJECT;
|
return SMFIS_REJECT;
|
||||||
case SMFIS_TEMPFAIL:
|
case SMFIS_TEMPFAIL:
|
||||||
smfi_setreply(self->ctx, "451", "4.3.0", "Filter failure");
|
smfi_setreply(self->ctx, "451", "4.3.0", untrapped_msg);
|
||||||
return SMFIS_TEMPFAIL;
|
return SMFIS_TEMPFAIL;
|
||||||
}
|
}
|
||||||
return SMFIS_CONTINUE;
|
return SMFIS_CONTINUE;
|
||||||
@@ -631,9 +693,23 @@ _generic_wrapper(milter_ContextObject *self, PyObject *cb, PyObject *arglist) {
|
|||||||
result = PyEval_CallObject(cb, arglist);
|
result = PyEval_CallObject(cb, arglist);
|
||||||
Py_DECREF(arglist);
|
Py_DECREF(arglist);
|
||||||
if (result == NULL) return _report_exception(self);
|
if (result == NULL) return _report_exception(self);
|
||||||
retval = PyInt_AsLong(result);
|
if (!PyInt_Check(result)) {
|
||||||
|
const struct MilterCallback *p;
|
||||||
|
const char *cbname = "milter";
|
||||||
|
char buf[40];
|
||||||
|
Py_DECREF(result);
|
||||||
|
for (p = callback; p->name; ++p) {
|
||||||
|
if (cb == p->cb) {
|
||||||
|
cbname = p->name;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sprintf(buf,"The %s callback must return int",cbname);
|
||||||
|
PyErr_SetString(MilterError,buf);
|
||||||
|
return _report_exception(self);
|
||||||
|
}
|
||||||
|
retval = PyInt_AS_LONG(result);
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
if (PyErr_Occurred()) return _report_exception(self);
|
|
||||||
_release_thread(self->t);
|
_release_thread(self->t);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
@@ -822,10 +898,6 @@ milter_wrap_abort(SMFICTX *ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SMFIS_ALL_OPTS
|
#ifdef SMFIS_ALL_OPTS
|
||||||
static PyObject *unknown_callback = NULL;
|
|
||||||
static PyObject *data_callback = NULL;
|
|
||||||
static PyObject *negotiate_callback = NULL;
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
milter_wrap_unknown(SMFICTX *ctx, const char *cmd) {
|
milter_wrap_unknown(SMFICTX *ctx, const char *cmd) {
|
||||||
PyObject *arglist;
|
PyObject *arglist;
|
||||||
@@ -932,7 +1004,7 @@ milter_wrap_close(SMFICTX *ctx) {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_register__doc__[] =
|
static const char milter_register__doc__[] =
|
||||||
"register(name,unknown=,data=,negotiate=) -> None\n\
|
"register(name,unknown=,data=,negotiate=) -> None\n\
|
||||||
Registers the milter name with current callbacks, and flags.\n\
|
Registers the milter name with current callbacks, and flags.\n\
|
||||||
Required before main() is called.";
|
Required before main() is called.";
|
||||||
@@ -977,7 +1049,7 @@ milter_register(PyObject *self, PyObject *args, PyObject *kwds) {
|
|||||||
return _generic_return(smfi_register(description), "cannot register");
|
return _generic_return(smfi_register(description), "cannot register");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_opensocket__doc__[] =
|
static const char milter_opensocket__doc__[] =
|
||||||
"opensocket(rmsock) -> None\n\
|
"opensocket(rmsock) -> None\n\
|
||||||
Attempts to create and open the socket provided with setconn.\n\
|
Attempts to create and open the socket provided with setconn.\n\
|
||||||
Removes the socket first if rmsock is True.";
|
Removes the socket first if rmsock is True.";
|
||||||
@@ -990,7 +1062,7 @@ milter_opensocket(PyObject *self, PyObject *args) {
|
|||||||
return _generic_return(smfi_opensocket(rmsock), "cannot opensocket");
|
return _generic_return(smfi_opensocket(rmsock), "cannot opensocket");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_main__doc__[] =
|
static const char milter_main__doc__[] =
|
||||||
"main() -> None\n\
|
"main() -> None\n\
|
||||||
Main milter routine. Set any callbacks, and flags desired, then call\n\
|
Main milter routine. Set any callbacks, and flags desired, then call\n\
|
||||||
setconn(), then call register(name), and finally call main().";
|
setconn(), then call register(name), and finally call main().";
|
||||||
@@ -1014,7 +1086,7 @@ milter_main(PyObject *self, PyObject *args) {
|
|||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_setdbg__doc__[] =
|
static const char milter_setdbg__doc__[] =
|
||||||
"setdbg(int) -> None\n\
|
"setdbg(int) -> None\n\
|
||||||
Sets debug level in sendmail/libmilter source. Dubious usefulness.";
|
Sets debug level in sendmail/libmilter source. Dubious usefulness.";
|
||||||
|
|
||||||
@@ -1025,7 +1097,7 @@ milter_setdbg(PyObject *self, PyObject *args) {
|
|||||||
return _generic_return(smfi_setdbg(val), "cannot set debug value");
|
return _generic_return(smfi_setdbg(val), "cannot set debug value");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_setbacklog__doc__[] =
|
static const char milter_setbacklog__doc__[] =
|
||||||
"setbacklog(int) -> None\n\
|
"setbacklog(int) -> None\n\
|
||||||
Set the TCP connection queue size for the milter socket.";
|
Set the TCP connection queue size for the milter socket.";
|
||||||
|
|
||||||
@@ -1037,7 +1109,7 @@ milter_setbacklog(PyObject *self, PyObject *args) {
|
|||||||
return _generic_return(smfi_setbacklog(val), "cannot set backlog");
|
return _generic_return(smfi_setbacklog(val), "cannot set backlog");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_settimeout__doc__[] =
|
static const char milter_settimeout__doc__[] =
|
||||||
"settimeout(int) -> None\n\
|
"settimeout(int) -> None\n\
|
||||||
Set the time (in seconds) that sendmail will wait before\n\
|
Set the time (in seconds) that sendmail will wait before\n\
|
||||||
considering this filter dead.";
|
considering this filter dead.";
|
||||||
@@ -1050,7 +1122,7 @@ milter_settimeout(PyObject *self, PyObject *args) {
|
|||||||
return _generic_return(smfi_settimeout(val), "cannot set timeout");
|
return _generic_return(smfi_settimeout(val), "cannot set timeout");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_setconn__doc__[] =
|
static const char milter_setconn__doc__[] =
|
||||||
"setconn(filename) -> None\n\
|
"setconn(filename) -> None\n\
|
||||||
Sets the pathname to the unix, inet, or inet6 socket that\n\
|
Sets the pathname to the unix, inet, or inet6 socket that\n\
|
||||||
sendmail will use to communicate with this filter. By default,\n\
|
sendmail will use to communicate with this filter. By default,\n\
|
||||||
@@ -1070,7 +1142,7 @@ milter_setconn(PyObject *self, PyObject *args) {
|
|||||||
return _generic_return(smfi_setconn(str), "cannot set connection");
|
return _generic_return(smfi_setconn(str), "cannot set connection");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_stop__doc__[] =
|
static const char milter_stop__doc__[] =
|
||||||
"stop() -> None\n\
|
"stop() -> None\n\
|
||||||
This function appears to be a controlled method to tell sendmail to\n\
|
This function appears to be a controlled method to tell sendmail to\n\
|
||||||
stop using this filter. It will close the socket.";
|
stop using this filter. It will close the socket.";
|
||||||
@@ -1083,7 +1155,7 @@ milter_stop(PyObject *self, PyObject *args) {
|
|||||||
return _thread_return(t,smfi_stop(), "cannot stop");
|
return _thread_return(t,smfi_stop(), "cannot stop");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_getdiag__doc__[] =
|
static const char milter_getdiag__doc__[] =
|
||||||
"getdiag() -> tuple\n\
|
"getdiag() -> tuple\n\
|
||||||
Return a tuple of diagnostic data. The first two items are context new\n\
|
Return a tuple of diagnostic data. The first two items are context new\n\
|
||||||
count and context del count. The rest are yet to be defined.";
|
count and context del count. The rest are yet to be defined.";
|
||||||
@@ -1093,7 +1165,7 @@ milter_getdiag(PyObject *self, PyObject *args) {
|
|||||||
return Py_BuildValue("(kk)", diag.contextNew,diag.contextDel);
|
return Py_BuildValue("(kk)", diag.contextNew,diag.contextDel);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_getversion__doc__[] =
|
static const char milter_getversion__doc__[] =
|
||||||
"getversion() -> tuple\n\
|
"getversion() -> tuple\n\
|
||||||
Return runtime libmilter version as a tuple of major,minor,patchlevel.";
|
Return runtime libmilter version as a tuple of major,minor,patchlevel.";
|
||||||
static PyObject *
|
static PyObject *
|
||||||
@@ -1107,7 +1179,7 @@ milter_getversion(PyObject *self, PyObject *args) {
|
|||||||
return Py_BuildValue("(kkk)", major,minor,patch);
|
return Py_BuildValue("(kkk)", major,minor,patch);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_getsymval__doc__[] =
|
static const char milter_getsymval__doc__[] =
|
||||||
"getsymval(String) -> String\n\
|
"getsymval(String) -> String\n\
|
||||||
Returns a symbol's value. Context-dependent, and unclear from the dox.";
|
Returns a symbol's value. Context-dependent, and unclear from the dox.";
|
||||||
|
|
||||||
@@ -1122,7 +1194,7 @@ milter_getsymval(PyObject *self, PyObject *args) {
|
|||||||
return Py_BuildValue("s", smfi_getsymval(ctx, str));
|
return Py_BuildValue("s", smfi_getsymval(ctx, str));
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_setreply__doc__[] =
|
static const char milter_setreply__doc__[] =
|
||||||
"setreply(rcode, xcode, message) -> None\n\
|
"setreply(rcode, xcode, message) -> None\n\
|
||||||
Sets the specific reply code to be used in response\n\
|
Sets the specific reply code to be used in response\n\
|
||||||
to the active command.\n\
|
to the active command.\n\
|
||||||
@@ -1186,7 +1258,7 @@ milter_setreply(PyObject *self, PyObject *args) {
|
|||||||
"cannot set reply");
|
"cannot set reply");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_addheader__doc__[] =
|
static const char milter_addheader__doc__[] =
|
||||||
"addheader(field, value, idx=-1) -> None\n\
|
"addheader(field, value, idx=-1) -> None\n\
|
||||||
Add a header to the message. This header is not passed to other\n\
|
Add a header to the message. This header is not passed to other\n\
|
||||||
filters. It is not checked for standards compliance;\n\
|
filters. It is not checked for standards compliance;\n\
|
||||||
@@ -1223,7 +1295,7 @@ milter_addheader(PyObject *self, PyObject *args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SMFIF_CHGFROM
|
#ifdef SMFIF_CHGFROM
|
||||||
static char milter_chgfrom__doc__[] =
|
static const char milter_chgfrom__doc__[] =
|
||||||
"chgfrom(sender,params) -> None\n\
|
"chgfrom(sender,params) -> None\n\
|
||||||
Change the envelope sender (MAIL From) of the current message.\n\
|
Change the envelope sender (MAIL From) of the current message.\n\
|
||||||
A filter which calls smfi_chgfrom must have set the CHGFROM flag\n\
|
A filter which calls smfi_chgfrom must have set the CHGFROM flag\n\
|
||||||
@@ -1246,7 +1318,7 @@ milter_chgfrom(PyObject *self, PyObject *args) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static char milter_chgheader__doc__[] =
|
static const char milter_chgheader__doc__[] =
|
||||||
"chgheader(field, int, value) -> None\n\
|
"chgheader(field, int, value) -> None\n\
|
||||||
Change/delete a header in the message. \n\
|
Change/delete a header in the message. \n\
|
||||||
It is not checked for standards compliance; the mail filter\n\
|
It is not checked for standards compliance; the mail filter\n\
|
||||||
@@ -1274,7 +1346,7 @@ milter_chgheader(PyObject *self, PyObject *args) {
|
|||||||
"cannot change header");
|
"cannot change header");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_addrcpt__doc__[] =
|
static const char milter_addrcpt__doc__[] =
|
||||||
"addrcpt(string,params=None) -> None\n\
|
"addrcpt(string,params=None) -> None\n\
|
||||||
Add a recipient to the envelope. It must be in the same format\n\
|
Add a recipient to the envelope. It must be in the same format\n\
|
||||||
as is passed to the envrcpt callback in the first tuple element.\n\
|
as is passed to the envrcpt callback in the first tuple element.\n\
|
||||||
@@ -1304,7 +1376,7 @@ milter_addrcpt(PyObject *self, PyObject *args) {
|
|||||||
return _thread_return(t,rc, "cannot add recipient");
|
return _thread_return(t,rc, "cannot add recipient");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_delrcpt__doc__[] =
|
static const char milter_delrcpt__doc__[] =
|
||||||
"delrcpt(string) -> None\n\
|
"delrcpt(string) -> None\n\
|
||||||
Delete a recipient from the envelope.\n\
|
Delete a recipient from the envelope.\n\
|
||||||
This function can only be called from the EOM callback.";
|
This function can only be called from the EOM callback.";
|
||||||
@@ -1322,7 +1394,7 @@ milter_delrcpt(PyObject *self, PyObject *args) {
|
|||||||
return _thread_return(t,smfi_delrcpt(ctx, rcpt), "cannot delete recipient");
|
return _thread_return(t,smfi_delrcpt(ctx, rcpt), "cannot delete recipient");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_replacebody__doc__[] =
|
static const char milter_replacebody__doc__[] =
|
||||||
"replacebody(string) -> None\n\
|
"replacebody(string) -> None\n\
|
||||||
Replace the body of the message. This routine may be called multiple\n\
|
Replace the body of the message. This routine may be called multiple\n\
|
||||||
times if the body is longer than convenient to send in one call. End of\n\
|
times if the body is longer than convenient to send in one call. End of\n\
|
||||||
@@ -1344,7 +1416,7 @@ milter_replacebody(PyObject *self, PyObject *args) {
|
|||||||
(unsigned char *)bodyp, bodylen), "cannot replace message body");
|
(unsigned char *)bodyp, bodylen), "cannot replace message body");
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_setpriv__doc__[] =
|
static const char milter_setpriv__doc__[] =
|
||||||
"setpriv(object) -> object\n\
|
"setpriv(object) -> object\n\
|
||||||
Associates any Python object with this context, and returns\n\
|
Associates any Python object with this context, and returns\n\
|
||||||
the old value or None. Use this to\n\
|
the old value or None. Use this to\n\
|
||||||
@@ -1370,7 +1442,7 @@ milter_setpriv(PyObject *self, PyObject *args) {
|
|||||||
return old;
|
return old;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char milter_getpriv__doc__[] =
|
static const char milter_getpriv__doc__[] =
|
||||||
"getpriv() -> None\n\
|
"getpriv() -> None\n\
|
||||||
Returns the Python object associated with the current context (if any).\n\
|
Returns the Python object associated with the current context (if any).\n\
|
||||||
Use this in conjunction with setpriv to keep track of data in a thread-safe\n\
|
Use this in conjunction with setpriv to keep track of data in a thread-safe\n\
|
||||||
@@ -1388,7 +1460,7 @@ milter_getpriv(PyObject *self, PyObject *args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SMFIF_QUARANTINE
|
#ifdef SMFIF_QUARANTINE
|
||||||
static char milter_quarantine__doc__[] =
|
static const char milter_quarantine__doc__[] =
|
||||||
"quarantine(string) -> None\n\
|
"quarantine(string) -> None\n\
|
||||||
Place the message in quarantine. A string with a description of the reason\n\
|
Place the message in quarantine. A string with a description of the reason\n\
|
||||||
is the only argument.";
|
is the only argument.";
|
||||||
@@ -1409,7 +1481,7 @@ milter_quarantine(PyObject *self, PyObject *args) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SMFIR_PROGRESS
|
#ifdef SMFIR_PROGRESS
|
||||||
static char milter_progress__doc__[] =
|
static const char milter_progress__doc__[] =
|
||||||
"progress() -> None\n\
|
"progress() -> None\n\
|
||||||
Notify the MTA that we are working on a message so it will reset timeouts.";
|
Notify the MTA that we are working on a message so it will reset timeouts.";
|
||||||
|
|
||||||
@@ -1426,23 +1498,23 @@ milter_progress(PyObject *self, PyObject *args) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SMFIF_SETSMLIST
|
#ifdef SMFIF_SETSYMLIST
|
||||||
static char milter_setsmlist__doc__[] =
|
static const char milter_setsymlist__doc__[] =
|
||||||
"setsmlist(stage,macrolist) -> None\n\
|
"setsymlist(stage,macrolist) -> None\n\
|
||||||
Tell the MTA which macro values we are interested in for a given stage";
|
Tell the MTA which macro values we are interested in for a given stage";
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
milter_setsmlist(PyObject *self, PyObject *args) {
|
milter_setsymlist(PyObject *self, PyObject *args) {
|
||||||
SMFICTX *ctx;
|
SMFICTX *ctx;
|
||||||
PyThreadState *t;
|
PyThreadState *t;
|
||||||
int stage = 0;
|
int stage = 0;
|
||||||
char *smlist = 0;
|
char *smlist = 0;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "is:setsmlist",&stage, &smlist)) return NULL;
|
if (!PyArg_ParseTuple(args, "is:setsymlist",&stage, &smlist)) return NULL;
|
||||||
ctx = _find_context(self);
|
ctx = _find_context(self);
|
||||||
if (ctx == NULL) return NULL;
|
if (ctx == NULL) return NULL;
|
||||||
t = PyEval_SaveThread();
|
t = PyEval_SaveThread();
|
||||||
return _thread_return(t,smfi_setsmlist(ctx,stage,smlist),
|
return _thread_return(t,smfi_setsymlist(ctx,stage,smlist),
|
||||||
"cannot set macro list");
|
"cannot set macro list");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -1466,8 +1538,8 @@ static PyMethodDef context_methods[] = {
|
|||||||
#ifdef SMFIF_CHGFROM
|
#ifdef SMFIF_CHGFROM
|
||||||
{ "chgfrom", milter_chgfrom, METH_VARARGS, milter_chgfrom__doc__},
|
{ "chgfrom", milter_chgfrom, METH_VARARGS, milter_chgfrom__doc__},
|
||||||
#endif
|
#endif
|
||||||
#ifdef SMFIF_SETSMLIST
|
#ifdef SMFIF_SETSYMLIST
|
||||||
{ "setsmlist", milter_setsmlist, METH_VARARGS, milter_setsmlist__doc__},
|
{ "setsymlist", milter_setsymlist, METH_VARARGS, milter_setsymlist__doc__},
|
||||||
#endif
|
#endif
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
@@ -1548,7 +1620,7 @@ static PyTypeObject milter_ContextType = {
|
|||||||
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
||||||
};
|
};
|
||||||
|
|
||||||
static char milter_documentation[] =
|
static const char milter_documentation[] =
|
||||||
"This module interfaces with Sendmail's libmilter functionality,\n\
|
"This module interfaces with Sendmail's libmilter functionality,\n\
|
||||||
allowing one to write email filters directly in Python.\n\
|
allowing one to write email filters directly in Python.\n\
|
||||||
Libmilter is currently marked FFR, and needs to be explicitly installed.\n\
|
Libmilter is currently marked FFR, and needs to be explicitly installed.\n\
|
||||||
@@ -1590,8 +1662,15 @@ initmilter(void) {
|
|||||||
#ifdef SMFIF_CHGFROM
|
#ifdef SMFIF_CHGFROM
|
||||||
setitem(d,"CHGFROM",SMFIF_CHGFROM);
|
setitem(d,"CHGFROM",SMFIF_CHGFROM);
|
||||||
#endif
|
#endif
|
||||||
#ifdef SMFIF_SETSMLIST
|
#ifdef SMFIF_SETSYMLIST
|
||||||
setitem(d,"SETSMLIST",SMFIF_SETSMLIST);
|
setitem(d,"SETSYMLIST",SMFIF_SETSYMLIST);
|
||||||
|
setitem(d,"M_CONNECT",SMFIM_CONNECT);/* connect */
|
||||||
|
setitem(d,"M_HELO",SMFIM_HELO); /* HELO/EHLO */
|
||||||
|
setitem(d,"M_ENVFROM",SMFIM_ENVFROM);/* MAIL From */
|
||||||
|
setitem(d,"M_ENVRCPT",SMFIM_ENVRCPT);/* RCPT To */
|
||||||
|
setitem(d,"M_DATA",SMFIM_DATA); /* DATA */
|
||||||
|
setitem(d,"M_EOM",SMFIM_EOM); /* end of message (final dot) */
|
||||||
|
setitem(d,"M_EOH",SMFIM_EOH); /* end of header */
|
||||||
#endif
|
#endif
|
||||||
#ifdef SMFIS_ALL_OPTS
|
#ifdef SMFIS_ALL_OPTS
|
||||||
setitem(d,"P_RCPT_REJ",SMFIP_RCPT_REJ);
|
setitem(d,"P_RCPT_REJ",SMFIP_RCPT_REJ);
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
# $Log$
|
# $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
|
# Revision 1.6 2009/06/09 03:13:13 customdesigned
|
||||||
# More doxygen docs.
|
# More doxygen docs.
|
||||||
#
|
#
|
||||||
@@ -124,24 +130,24 @@ class MimeGenerator(Generator):
|
|||||||
# full MIME type, then dispatch to self._handle_<maintype>(). If
|
# full MIME type, then dispatch to self._handle_<maintype>(). If
|
||||||
# that's missing too, then dispatch to self._writeBody().
|
# that's missing too, then dispatch to self._writeBody().
|
||||||
main = msg.get_content_maintype()
|
main = msg.get_content_maintype()
|
||||||
if msg.is_multipart() and main.lower() != 'multipart':
|
if msg.is_multipart() and main.lower() != 'multipart':
|
||||||
self._handle_multipart(msg)
|
self._handle_multipart(msg)
|
||||||
else:
|
else:
|
||||||
Generator._dispatch(self,msg)
|
Generator._dispatch(self,msg)
|
||||||
|
|
||||||
def unquote(s):
|
def unquote(s):
|
||||||
"""Remove quotes from a string."""
|
"""Remove quotes from a string."""
|
||||||
if len(s) > 1:
|
if len(s) > 1:
|
||||||
if s.startswith('"'):
|
if s.startswith('"'):
|
||||||
if s.endswith('"'):
|
if s.endswith('"'):
|
||||||
s = s[1:-1]
|
s = s[1:-1]
|
||||||
else: # remove garbage after trailing quote
|
else: # remove garbage after trailing quote
|
||||||
try: s = s[1:s[1:].index('"')+1]
|
try: s = s[1:s[1:].index('"')+1]
|
||||||
except:
|
except:
|
||||||
return s
|
return s
|
||||||
return s.replace('\\\\', '\\').replace('\\"', '"')
|
return s.replace('\\\\', '\\').replace('\\"', '"')
|
||||||
if s.startswith('<') and s.endswith('>'):
|
if s.startswith('<') and s.endswith('>'):
|
||||||
return s[1:-1]
|
return s[1:-1]
|
||||||
return s
|
return s
|
||||||
|
|
||||||
from types import TupleType
|
from types import TupleType
|
||||||
@@ -165,15 +171,14 @@ class MimeMessage(Message):
|
|||||||
"""
|
"""
|
||||||
def __init__(self,fp=None,seekable=1):
|
def __init__(self,fp=None,seekable=1):
|
||||||
Message.__init__(self)
|
Message.__init__(self)
|
||||||
self.headerchange = None
|
|
||||||
self.submsg = None
|
self.submsg = None
|
||||||
self.modified = False
|
self.modified = False
|
||||||
|
|
||||||
## @var headerchange
|
## @var headerchange
|
||||||
# Provide a headerchange event for integration with Milter.
|
# Provide a headerchange event for integration with Milter.
|
||||||
# The headerchange attribute can be assigned a function to be called when
|
# The headerchange attribute can be assigned a function to be called when
|
||||||
# changing headers. The signature is:
|
# changing headers. The signature is:
|
||||||
# headerchange(msg,name,value) -> None
|
# headerchange(msg,name,value) -> None
|
||||||
|
self.headerchange = None
|
||||||
|
|
||||||
def get_param(self, param, failobj=None, header='content-type', unquote=True):
|
def get_param(self, param, failobj=None, header='content-type', unquote=True):
|
||||||
val = Message.get_param(self,param,failobj,header,unquote)
|
val = Message.get_param(self,param,failobj,header,unquote)
|
||||||
@@ -200,21 +205,21 @@ class MimeMessage(Message):
|
|||||||
for attr,val in self._get_params_preserve([],'content-type'):
|
for attr,val in self._get_params_preserve([],'content-type'):
|
||||||
if isinstance(val, TupleType):
|
if isinstance(val, TupleType):
|
||||||
# It's an RFC 2231 encoded parameter
|
# It's an RFC 2231 encoded parameter
|
||||||
newvalue = _unquotevalue(val)
|
newvalue = _unquotevalue(val)
|
||||||
if val[0]:
|
if val[0]:
|
||||||
val = unicode(newvalue[2], newvalue[0])
|
val = unicode(newvalue[2], newvalue[0])
|
||||||
else:
|
else:
|
||||||
val = unicode(newvalue[2])
|
val = unicode(newvalue[2])
|
||||||
else:
|
else:
|
||||||
val = _unquotevalue(val.strip())
|
val = _unquotevalue(val.strip())
|
||||||
names.append((attr,val))
|
names.append((attr,val))
|
||||||
names += [("filename",self.get_filename())]
|
names += [("filename",self.get_filename())]
|
||||||
if scan_zip:
|
if scan_zip:
|
||||||
for key,name in tuple(names): # copy by converting to tuple
|
for key,name in tuple(names): # copy by converting to tuple
|
||||||
if name and name.lower().endswith('.zip'):
|
if name and name.lower().endswith('.zip'):
|
||||||
txt = self.get_payload(decode=True)
|
txt = self.get_payload(decode=True)
|
||||||
if txt.strip():
|
if txt.strip():
|
||||||
names += zipnames(txt)
|
names += zipnames(txt)
|
||||||
return names
|
return names
|
||||||
|
|
||||||
def ismodified(self):
|
def ismodified(self):
|
||||||
@@ -285,13 +290,13 @@ class MimeMessage(Message):
|
|||||||
if t == 'message/rfc822' or t.startswith('multipart/'):
|
if t == 'message/rfc822' or t.startswith('multipart/'):
|
||||||
if not self.submsg:
|
if not self.submsg:
|
||||||
txt = self.get_payload()
|
txt = self.get_payload()
|
||||||
if type(txt) == str:
|
if type(txt) == str:
|
||||||
txt = self.get_payload(decode=True)
|
txt = self.get_payload(decode=True)
|
||||||
self.submsg = email.message_from_string(txt,MimeMessage)
|
self.submsg = email.message_from_string(txt,MimeMessage)
|
||||||
for part in self.submsg.walk():
|
for part in self.submsg.walk():
|
||||||
part.modified = False
|
part.modified = False
|
||||||
else:
|
else:
|
||||||
self.submsg = txt[0]
|
self.submsg = txt[0]
|
||||||
return self.submsg
|
return self.submsg
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@@ -331,7 +336,7 @@ def check_name(msg,savname=None,ckname=check_ext,scan_zip=False):
|
|||||||
if badname:
|
if badname:
|
||||||
if key == 'zipname':
|
if key == 'zipname':
|
||||||
badname = msg.get_filename()
|
badname = msg.get_filename()
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
return Milter.CONTINUE
|
return Milter.CONTINUE
|
||||||
except zipfile.BadZipfile:
|
except zipfile.BadZipfile:
|
||||||
@@ -378,7 +383,7 @@ class _defang:
|
|||||||
return rc
|
return rc
|
||||||
|
|
||||||
def __call__(self,msg,savname=None,check=check_ext,scan_rfc822=True,
|
def __call__(self,msg,savname=None,check=check_ext,scan_rfc822=True,
|
||||||
scan_zip=False):
|
scan_zip=False):
|
||||||
"""Compatible entry point.
|
"""Compatible entry point.
|
||||||
Replace all attachments with dangerous names."""
|
Replace all attachments with dangerous names."""
|
||||||
self._savname = savname
|
self._savname = savname
|
||||||
@@ -448,25 +453,25 @@ class SGMLFilter(sgmllib.SGMLParser):
|
|||||||
n = len(rawdata)
|
n = len(rawdata)
|
||||||
j = i + 2
|
j = i + 2
|
||||||
while j < n:
|
while j < n:
|
||||||
c = rawdata[j]
|
c = rawdata[j]
|
||||||
if c == ">":
|
if c == ">":
|
||||||
# end of declaration syntax
|
# end of declaration syntax
|
||||||
self.handle_special(rawdata[i+2:j])
|
self.handle_special(rawdata[i+2:j])
|
||||||
return j + 1
|
return j + 1
|
||||||
if c in "\"'":
|
if c in "\"'":
|
||||||
m = declstringlit.match(rawdata, j)
|
m = declstringlit.match(rawdata, j)
|
||||||
if not m:
|
if not m:
|
||||||
# incomplete or an error?
|
# incomplete or an error?
|
||||||
return -1
|
return -1
|
||||||
j = m.end()
|
j = m.end()
|
||||||
elif c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
|
elif c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
|
||||||
m = declname.match(rawdata, j)
|
m = declname.match(rawdata, j)
|
||||||
if not m:
|
if not m:
|
||||||
# incomplete or an error?
|
# incomplete or an error?
|
||||||
return -1
|
return -1
|
||||||
j = m.end()
|
j = m.end()
|
||||||
else:
|
else:
|
||||||
j += 1
|
j += 1
|
||||||
# end of buffer between tokens
|
# end of buffer between tokens
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
@@ -495,7 +500,7 @@ def check_html(msg,savname=None):
|
|||||||
if msgtype == 'application/octet-stream':
|
if msgtype == 'application/octet-stream':
|
||||||
for (attr,name) in msg.getnames():
|
for (attr,name) in msg.getnames():
|
||||||
if name and name.lower().endswith(".htm"):
|
if name and name.lower().endswith(".htm"):
|
||||||
msgtype = 'text/html'
|
msgtype = 'text/html'
|
||||||
if msgtype == 'text/html':
|
if msgtype == 'text/html':
|
||||||
out = StringIO.StringIO()
|
out = StringIO.StringIO()
|
||||||
htmlfilter = HTMLScriptFilter(out)
|
htmlfilter = HTMLScriptFilter(out)
|
||||||
|
|||||||
+26
-4
@@ -1,20 +1,21 @@
|
|||||||
%define __python python2.6
|
%define __python python2.6
|
||||||
|
%define pythonbase python
|
||||||
|
|
||||||
%define libdir %{_libdir}/pymilter
|
%define libdir %{_libdir}/pymilter
|
||||||
%{!?python_sitearch: %define python_sitearch %(%{__python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(1)")}
|
%{!?python_sitearch: %define python_sitearch %(%{__python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib(1)")}
|
||||||
%define pythonbase python26
|
|
||||||
|
|
||||||
Summary: Python interface to sendmail milter API
|
Summary: Python interface to sendmail milter API
|
||||||
Name: %{pythonbase}-pymilter
|
Name: %{pythonbase}-pymilter
|
||||||
Version: 0.9.4
|
Version: 0.9.8
|
||||||
Release: 1%{dist}
|
Release: 1%{dist}
|
||||||
Source: http://downloads.sourceforge.net/pymilter/pymilter-%{version}.tar.gz
|
Source: http://downloads.sourceforge.net/pymilter/pymilter-%{version}.tar.gz
|
||||||
License: GPLv2+
|
License: GPLv2+
|
||||||
Group: Development/Libraries
|
Group: Development/Libraries
|
||||||
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
|
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
|
||||||
Url: http://www.bmsi.com/python/milter.html
|
Url: http://www.bmsi.com/python/milter.html
|
||||||
Requires: %{pythonbase}, sendmail >= 8.13
|
# python-2.6.4 gets RuntimeError: not holding the import lock
|
||||||
# Need python2.4 specific pydns, not the version for system python
|
Requires: %{pythonbase} >= 2.6.5, sendmail >= 8.13
|
||||||
|
# Need python2.6 specific pydns, not the version for system python
|
||||||
Requires: %{pythonbase}-pydns
|
Requires: %{pythonbase}-pydns
|
||||||
# Needed for callbacks, not a core function but highly useful for milters
|
# Needed for callbacks, not a core function but highly useful for milters
|
||||||
BuildRequires: ed, %{pythonbase}-devel, sendmail-devel >= 8.13
|
BuildRequires: ed, %{pythonbase}-devel, sendmail-devel >= 8.13
|
||||||
@@ -74,6 +75,27 @@ chmod a+x $RPM_BUILD_ROOT%{libdir}/start.sh
|
|||||||
rm -rf $RPM_BUILD_ROOT
|
rm -rf $RPM_BUILD_ROOT
|
||||||
|
|
||||||
%changelog
|
%changelog
|
||||||
|
* Sat Mar 9 2013 Stuart Gathman <stuart@bmsi.com> 0.9.8-1
|
||||||
|
- Add Milter.test module for unit testing milters.
|
||||||
|
- Fix typo that prevented setsymlist from being active.
|
||||||
|
- Change untrapped exception message to:
|
||||||
|
- "pymilter: untrapped exception in milter app"
|
||||||
|
|
||||||
|
* Sat Feb 25 2012 Stuart Gathman <stuart@bmsi.com> 0.9.7-1
|
||||||
|
- Raise RuntimeError when result != CONTINUE for @noreply and @nocallback
|
||||||
|
- Remove redundant table in miltermodule
|
||||||
|
- Fix CNAME chain duplicating TXT records in Milter.dns (from pyspf).
|
||||||
|
|
||||||
|
* Sat Feb 25 2012 Stuart Gathman <stuart@bmsi.com> 0.9.6-1
|
||||||
|
- Raise ValueError on unescaped '%' passed to setreply
|
||||||
|
- Grace time at end of Greylist window
|
||||||
|
|
||||||
|
* Fri Aug 19 2011 Stuart Gathman <stuart@bmsi.com> 0.9.5-1
|
||||||
|
- Print milter.error for invalid callback return type.
|
||||||
|
(Since stacktrace is empty, the TypeError exception is confusing.)
|
||||||
|
- Fix milter-template.py
|
||||||
|
- Tweak Milter.utils.addr2bin and Milter.dynip to handle IP6
|
||||||
|
|
||||||
* Wed Mar 02 2010 Stuart Gathman <stuart@bmsi.com> 0.9.4-1
|
* Wed Mar 02 2010 Stuart Gathman <stuart@bmsi.com> 0.9.4-1
|
||||||
- Handle IP6 in Milter.utils.iniplist()
|
- Handle IP6 in Milter.utils.iniplist()
|
||||||
- python-2.6
|
- python-2.6
|
||||||
|
|||||||
@@ -35,7 +35,9 @@ class sampleMilter(Milter.Milter):
|
|||||||
# multiple messages can be received on a single connection
|
# multiple messages can be received on a single connection
|
||||||
# envfrom (MAIL FROM in the SMTP protocol) seems to mark the start
|
# envfrom (MAIL FROM in the SMTP protocol) seems to mark the start
|
||||||
# of each message.
|
# of each message.
|
||||||
|
@Milter.noreply
|
||||||
def envfrom(self,f,*str):
|
def envfrom(self,f,*str):
|
||||||
|
"start of MAIL transaction"
|
||||||
self.log("mail from",f,str)
|
self.log("mail from",f,str)
|
||||||
self.fp = StringIO.StringIO()
|
self.fp = StringIO.StringIO()
|
||||||
self.tempname = None
|
self.tempname = None
|
||||||
@@ -58,23 +60,23 @@ class sampleMilter(Milter.Milter):
|
|||||||
# even if we wanted the Taiwanese spam, we can't read Chinese
|
# even if we wanted the Taiwanese spam, we can't read Chinese
|
||||||
# (delete if you read chinese mail)
|
# (delete if you read chinese mail)
|
||||||
if val.startswith('=?big5') or val.startswith('=?ISO-2022-JP'):
|
if val.startswith('=?big5') or val.startswith('=?ISO-2022-JP'):
|
||||||
self.log('REJECT: %s: %s' % (name,val))
|
self.log('REJECT: %s: %s' % (name,val))
|
||||||
#self.setreply('550','','Go away spammer')
|
#self.setreply('550','','Go away spammer')
|
||||||
return Milter.REJECT
|
return Milter.REJECT
|
||||||
|
|
||||||
# check for common spam keywords
|
# check for common spam keywords
|
||||||
if val.find("$$$") >= 0 or val.find("XXX") >= 0 \
|
if val.find("$$$") >= 0 or val.find("XXX") >= 0 \
|
||||||
or val.find("!!!") >= 0 or val.find("FREE") >= 0:
|
or val.find("!!!") >= 0 or val.find("FREE") >= 0:
|
||||||
self.log('REJECT: %s: %s' % (name,val))
|
self.log('REJECT: %s: %s' % (name,val))
|
||||||
#self.setreply('550','','Go away spammer')
|
#self.setreply('550','','Go away spammer')
|
||||||
return Milter.REJECT
|
return Milter.REJECT
|
||||||
|
|
||||||
# check for spam that pretends to be legal
|
# check for spam that pretends to be legal
|
||||||
lval = val.lower()
|
lval = val.lower()
|
||||||
if lval.startswith("adv:") or lval.startswith("adv.") \
|
if lval.startswith("adv:") or lval.startswith("adv.") \
|
||||||
or lval.find('viagra') >= 0:
|
or lval.find('viagra') >= 0:
|
||||||
self.log('REJECT: %s: %s' % (name,val))
|
self.log('REJECT: %s: %s' % (name,val))
|
||||||
return Milter.REJECT
|
return Milter.REJECT
|
||||||
|
|
||||||
# check for invalid message id
|
# check for invalid message id
|
||||||
if lname == 'message-id' and len(val) < 4:
|
if lname == 'message-id' and len(val) < 4:
|
||||||
@@ -84,7 +86,7 @@ class sampleMilter(Milter.Milter):
|
|||||||
|
|
||||||
# check for common bulk mailers
|
# check for common bulk mailers
|
||||||
if lname == 'x-mailer' and \
|
if lname == 'x-mailer' and \
|
||||||
val.lower() in ('direct email','calypso','mail bomber'):
|
val.lower() in ('direct email','calypso','mail bomber'):
|
||||||
self.log('REJECT: %s: %s' % (name,val))
|
self.log('REJECT: %s: %s' % (name,val))
|
||||||
#self.setreply('550','','Go away spammer')
|
#self.setreply('550','','Go away spammer')
|
||||||
return Milter.REJECT
|
return Milter.REJECT
|
||||||
@@ -121,7 +123,7 @@ class sampleMilter(Milter.Milter):
|
|||||||
h = msg.getheaders(name)
|
h = msg.getheaders(name)
|
||||||
cnt = len(h)
|
cnt = len(h)
|
||||||
for i in range(cnt,0,-1):
|
for i in range(cnt,0,-1):
|
||||||
self.chgheader(name,i-1,'')
|
self.chgheader(name,i-1,'')
|
||||||
|
|
||||||
def eom(self):
|
def eom(self):
|
||||||
if not self.fp: return Milter.ACCEPT
|
if not self.fp: return Milter.ACCEPT
|
||||||
@@ -143,9 +145,9 @@ class sampleMilter(Milter.Milter):
|
|||||||
msg = rfc822.Message(out)
|
msg = rfc822.Message(out)
|
||||||
msg.rewindbody()
|
msg.rewindbody()
|
||||||
while 1:
|
while 1:
|
||||||
buf = out.read(8192)
|
buf = out.read(8192)
|
||||||
if len(buf) == 0: break
|
if len(buf) == 0: break
|
||||||
self.replacebody(buf) # feed modified message to sendmail
|
self.replacebody(buf) # feed modified message to sendmail
|
||||||
return Milter.ACCEPT # ACCEPT modified message
|
return Milter.ACCEPT # ACCEPT modified message
|
||||||
finally:
|
finally:
|
||||||
out.close()
|
out.close()
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
from distutils.core import setup, Extension
|
from distutils.core import setup, Extension
|
||||||
|
|
||||||
|
if sys.version < '2.6.5':
|
||||||
|
sys.exit('ERROR: Sorry, python 2.6.5 is required for this module.')
|
||||||
|
|
||||||
# FIXME: on some versions of sendmail, smutil is renamed to sm.
|
# FIXME: on some versions of sendmail, smutil is renamed to sm.
|
||||||
# On slackware and debian, leave it out entirely. It depends
|
# On slackware and debian, leave it out entirely. It depends
|
||||||
# on how libmilter was built by the sendmail package.
|
# on how libmilter was built by the sendmail package.
|
||||||
@@ -9,15 +12,8 @@ from distutils.core import setup, Extension
|
|||||||
libs = ["milter"]
|
libs = ["milter"]
|
||||||
libdirs = ["/usr/lib/libmilter"] # needed for Debian
|
libdirs = ["/usr/lib/libmilter"] # needed for Debian
|
||||||
|
|
||||||
# patch distutils if it can't cope with the "classifiers" or
|
|
||||||
# "download_url" keywords
|
|
||||||
if sys.version < '2.2.3':
|
|
||||||
from distutils.dist import DistributionMetadata
|
|
||||||
DistributionMetadata.classifiers = None
|
|
||||||
DistributionMetadata.download_url = None
|
|
||||||
|
|
||||||
# NOTE: importing Milter to obtain version fails when milter.so not built
|
# NOTE: importing Milter to obtain version fails when milter.so not built
|
||||||
setup(name = "pymilter", version = '0.9.4',
|
setup(name = "pymilter", version = '0.9.8',
|
||||||
description="Python interface to sendmail milter API",
|
description="Python interface to sendmail milter API",
|
||||||
long_description="""\
|
long_description="""\
|
||||||
This is a python extension module to enable python scripts to
|
This is a python extension module to enable python scripts to
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
appname="$1"
|
appname="$1"
|
||||||
script="${2:-${appname}}"
|
script="${2:-${appname}}"
|
||||||
datadir="/var/log/milter"
|
datadir="/var/lib/milter"
|
||||||
|
logdir="/var/log/milter"
|
||||||
piddir="/var/run/milter"
|
piddir="/var/run/milter"
|
||||||
libdir="/usr/lib/pymilter"
|
libdir="/usr/lib/pymilter"
|
||||||
python="python2.4"
|
python="python2.4"
|
||||||
exec >>${datadir}/${appname}.log 2>&1
|
exec >>${logdir}/${appname}.log 2>&1
|
||||||
if test -s ${datadir}/${script}.py; then
|
if test -s ${datadir}/${script}.py; then
|
||||||
cd ${datadir} # use version in log dir if it exists for debugging
|
cd ${datadir} # use version in data dir if it exists for debugging
|
||||||
|
elif test -s ${logdir}/${script}.py; then
|
||||||
|
cd ${logdir} # use version in log dir if it exists for debugging
|
||||||
else
|
else
|
||||||
cd ${libdir}
|
cd ${libdir}
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import unittest
|
|||||||
import testmime
|
import testmime
|
||||||
import testsample
|
import testsample
|
||||||
import testutils
|
import testutils
|
||||||
|
import testgrey
|
||||||
import os
|
import os
|
||||||
|
|
||||||
def suite():
|
def suite():
|
||||||
@@ -9,6 +10,7 @@ def suite():
|
|||||||
s.addTest(testmime.suite())
|
s.addTest(testmime.suite())
|
||||||
s.addTest(testsample.suite())
|
s.addTest(testsample.suite())
|
||||||
s.addTest(testutils.suite())
|
s.addTest(testutils.suite())
|
||||||
|
s.addTest(testgrey.suite())
|
||||||
return s
|
return s
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
+18587
File diff suppressed because it is too large
Load Diff
+55
@@ -0,0 +1,55 @@
|
|||||||
|
import unittest
|
||||||
|
import doctest
|
||||||
|
import os
|
||||||
|
#from Milter.greylist import Greylist
|
||||||
|
from Milter.greysql import Greylist
|
||||||
|
|
||||||
|
class GreylistTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.fname = 'test.db'
|
||||||
|
os.remove(self.fname)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
#os.remove(self.fname)
|
||||||
|
pass
|
||||||
|
|
||||||
|
def testGrey(self):
|
||||||
|
grey = Greylist(self.fname)
|
||||||
|
# first time
|
||||||
|
rc = grey.check('1.2.3.4','foo@bar.com','baz@spat.com')
|
||||||
|
self.assertEqual(rc,0)
|
||||||
|
# not in window yet
|
||||||
|
rc = grey.check('1.2.3.4','foo@bar.com','baz@spat.com',timeinc=5*60)
|
||||||
|
self.assertEqual(rc,0)
|
||||||
|
# within window
|
||||||
|
rc = grey.check('1.2.3.4','foo@bar.com','baz@spat.com',timeinc=15*60)
|
||||||
|
self.assertEqual(rc,1)
|
||||||
|
# new triple
|
||||||
|
rc = grey.check('1.2.3.5','foo@bar.com','baz@spat.com',timeinc=15*60)
|
||||||
|
self.assertEqual(rc,0)
|
||||||
|
# seen again
|
||||||
|
rc = grey.check('1.2.3.4','foo@bar.com','baz@spat.com',timeinc=5*3600)
|
||||||
|
self.assertEqual(rc,2)
|
||||||
|
# new one past expire
|
||||||
|
rc = grey.check('1.2.3.5','foo@bar.com','baz@spat.com',timeinc=6*3600)
|
||||||
|
self.assertEqual(rc,0)
|
||||||
|
# original past retain
|
||||||
|
rc = grey.check('1.2.3.4','foo@bar.com','baz@spat.com',timeinc=37*24*3600)
|
||||||
|
self.assertEqual(rc,0)
|
||||||
|
# new one for testing expire
|
||||||
|
rc = grey.check('1.2.3.5','flub@bar.com','baz@spat.com',timeinc=20*24*3600)
|
||||||
|
self.assertEqual(rc,0)
|
||||||
|
grey.close()
|
||||||
|
# test cleanup
|
||||||
|
grey = Greylist(self.fname)
|
||||||
|
rc = grey.clean(timeinc=37*24*3600)
|
||||||
|
self.assertEqual(rc,1)
|
||||||
|
grey.close()
|
||||||
|
|
||||||
|
def suite():
|
||||||
|
s = unittest.makeSuite(GreylistTestCase,'test')
|
||||||
|
return s
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.TextTestRunner().run(suite())
|
||||||
+36
-4
@@ -1,4 +1,10 @@
|
|||||||
# $Log$
|
# $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
|
# Revision 1.3 2005/06/17 01:49:39 customdesigned
|
||||||
# Handle zip within zip.
|
# Handle zip within zip.
|
||||||
#
|
#
|
||||||
@@ -26,6 +32,7 @@ import socket
|
|||||||
import StringIO
|
import StringIO
|
||||||
import email
|
import email
|
||||||
import sys
|
import sys
|
||||||
|
import Milter
|
||||||
from email import Errors
|
from email import Errors
|
||||||
|
|
||||||
samp1_txt1 = """Dear Agent 1
|
samp1_txt1 = """Dear Agent 1
|
||||||
@@ -65,12 +72,12 @@ class MimeTestCase(unittest.TestCase):
|
|||||||
# python 2.4 doesn't get exceptions on missing boundaries, and
|
# python 2.4 doesn't get exceptions on missing boundaries, and
|
||||||
# if message is modified, output is readable by mail clients
|
# if message is modified, output is readable by mail clients
|
||||||
if sys.hexversion < 0x02040000:
|
if sys.hexversion < 0x02040000:
|
||||||
self.fail('should get boundary error parsing bad rfc822 attachment')
|
self.fail('should get boundary error parsing bad rfc822 attachment')
|
||||||
except Errors.BoundaryError:
|
except Errors.BoundaryError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def testDefang(self,vname='virus1',part=1,
|
def testDefang(self,vname='virus1',part=1,
|
||||||
fname='LOVE-LETTER-FOR-YOU.TXT.vbs'):
|
fname='LOVE-LETTER-FOR-YOU.TXT.vbs'):
|
||||||
msg = mime.message_from_file(open('test/'+vname,"r"))
|
msg = mime.message_from_file(open('test/'+vname,"r"))
|
||||||
mime.defang(msg,scan_zip=True)
|
mime.defang(msg,scan_zip=True)
|
||||||
self.failUnless(msg.ismodified(),"virus not removed")
|
self.failUnless(msg.ismodified(),"virus not removed")
|
||||||
@@ -104,7 +111,7 @@ class MimeTestCase(unittest.TestCase):
|
|||||||
self.failIf(msg.ismultipart())
|
self.failIf(msg.ismultipart())
|
||||||
txt2 = msg.get_payload()
|
txt2 = msg.get_payload()
|
||||||
self.failUnless(txt2 == mime.virus_msg % \
|
self.failUnless(txt2 == mime.virus_msg % \
|
||||||
(fname,hostname,None),txt2)
|
(fname,hostname,None),txt2)
|
||||||
|
|
||||||
# honey virus has a sneaky ASP payload which is parsed correctly
|
# honey virus has a sneaky ASP payload which is parsed correctly
|
||||||
# by email package in python-2.2.2, but not by mime.MimeMessage or 2.2.1
|
# by email package in python-2.2.2, but not by mime.MimeMessage or 2.2.1
|
||||||
@@ -118,7 +125,7 @@ class MimeTestCase(unittest.TestCase):
|
|||||||
txt2 = parts[1].get_payload()
|
txt2 = parts[1].get_payload()
|
||||||
txt3 = parts[2].get_payload()
|
txt3 = parts[2].get_payload()
|
||||||
self.failUnless(txt2.rstrip()+'\n' == mime.virus_msg % \
|
self.failUnless(txt2.rstrip()+'\n' == mime.virus_msg % \
|
||||||
(fname,hostname,None),txt2)
|
(fname,hostname,None),txt2)
|
||||||
if txt3 != '':
|
if txt3 != '':
|
||||||
self.failUnless(txt3.rstrip()+'\n' == mime.virus_msg % \
|
self.failUnless(txt3.rstrip()+'\n' == mime.virus_msg % \
|
||||||
('story[1].asp',hostname,None),txt3)
|
('story[1].asp',hostname,None),txt3)
|
||||||
@@ -146,6 +153,31 @@ class MimeTestCase(unittest.TestCase):
|
|||||||
# test zip within zip
|
# test zip within zip
|
||||||
self.testDefang('ziploop',1,'stuart@bmsi.com.zip')
|
self.testDefang('ziploop',1,'stuart@bmsi.com.zip')
|
||||||
|
|
||||||
|
def _chk_name(self,name):
|
||||||
|
self.filename = name
|
||||||
|
|
||||||
|
def _chk_attach(self,msg):
|
||||||
|
"Filter attachments by content."
|
||||||
|
# check for bad extensions
|
||||||
|
mime.check_name(msg,ckname=self._chk_name,scan_zip=True)
|
||||||
|
# remove scripts from HTML
|
||||||
|
mime.check_html(msg)
|
||||||
|
# don't let a tricky virus slip one past us
|
||||||
|
msg = msg.get_submsg()
|
||||||
|
if isinstance(msg,email.Message.Message):
|
||||||
|
return mime.check_attachments(msg,self._chk_attach)
|
||||||
|
return Milter.CONTINUE
|
||||||
|
|
||||||
|
def testCheckAttach(self,fname="test1"):
|
||||||
|
# test1 contains a very long filename
|
||||||
|
msg = mime.message_from_file(open('test/'+fname,'r'))
|
||||||
|
mime.defang(msg,scan_zip=True)
|
||||||
|
self.failIf(msg.ismodified())
|
||||||
|
msg = mime.message_from_file(open('test/test2','r'))
|
||||||
|
rc = mime.check_attachments(msg,self._chk_attach)
|
||||||
|
self.assertEquals(self.filename,"7501'S FOR TWO GOLDEN SOURCES SHIPMENTS FOR TAX & DUTY PURPOSES ONLY.PDF")
|
||||||
|
self.assertEquals(rc,Milter.CONTINUE)
|
||||||
|
|
||||||
def testHTML(self,fname=""):
|
def testHTML(self,fname=""):
|
||||||
result = StringIO.StringIO()
|
result = StringIO.StringIO()
|
||||||
filter = mime.HTMLScriptFilter(result)
|
filter = mime.HTMLScriptFilter(result)
|
||||||
|
|||||||
+10
-95
@@ -4,97 +4,12 @@ import sample
|
|||||||
import mime
|
import mime
|
||||||
import rfc822
|
import rfc822
|
||||||
import StringIO
|
import StringIO
|
||||||
|
from Milter.test import TestBase
|
||||||
|
|
||||||
class TestMilter(sample.sampleMilter):
|
class TestMilter(TestBase,sample.sampleMilter):
|
||||||
|
|
||||||
_protocol = 0
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.logfp = open("test/milter.log","a")
|
TestBase.__init__(self)
|
||||||
|
sample.sampleMilter.__init__(self)
|
||||||
def log(self,*msg):
|
|
||||||
for i in msg: print >>self.logfp, i,
|
|
||||||
print >>self.logfp
|
|
||||||
|
|
||||||
def replacebody(self,chunk):
|
|
||||||
if self._body:
|
|
||||||
self._body.write(chunk)
|
|
||||||
self.bodyreplaced = True
|
|
||||||
else:
|
|
||||||
raise IOError,"replacebody not called from eom()"
|
|
||||||
|
|
||||||
# FIXME: rfc822 indexing does not really reflect the way chg/add header
|
|
||||||
# work for a milter
|
|
||||||
def chgheader(self,field,idx,value):
|
|
||||||
self.log('chgheader: %s[%d]=%s' % (field,idx,value))
|
|
||||||
if value == '':
|
|
||||||
del self._msg[field]
|
|
||||||
else:
|
|
||||||
self._msg[field] = value
|
|
||||||
self.headerschanged = True
|
|
||||||
|
|
||||||
def addheader(self,field,value):
|
|
||||||
self.log('addheader: %s=%s' % (field,value))
|
|
||||||
self._msg[field] = value
|
|
||||||
self.headerschanged = True
|
|
||||||
|
|
||||||
def feedMsg(self,fname):
|
|
||||||
self._body = None
|
|
||||||
self.bodyreplaced = False
|
|
||||||
self.headerschanged = 0
|
|
||||||
fp = open('test/'+fname,'r')
|
|
||||||
msg = rfc822.Message(fp)
|
|
||||||
rc = self.envfrom('<spam@advertisements.com>')
|
|
||||||
if rc != Milter.CONTINUE: return rc
|
|
||||||
rc = self.envrcpt('<victim@lamb.com>')
|
|
||||||
if rc != Milter.CONTINUE: return rc
|
|
||||||
line = None
|
|
||||||
for h in msg.headers:
|
|
||||||
if h[:1].isspace():
|
|
||||||
line = line + h
|
|
||||||
continue
|
|
||||||
if not line:
|
|
||||||
line = h
|
|
||||||
continue
|
|
||||||
s = line.split(': ',1)
|
|
||||||
rc = self.header(s[0],s[1].strip())
|
|
||||||
if rc != Milter.CONTINUE: return rc
|
|
||||||
line = h
|
|
||||||
if line:
|
|
||||||
s = line.split(': ',1)
|
|
||||||
rc = self.header(s[0],s[1])
|
|
||||||
if rc != Milter.CONTINUE: return rc
|
|
||||||
rc = self.eoh()
|
|
||||||
if rc != Milter.CONTINUE: return rc
|
|
||||||
while 1:
|
|
||||||
buf = fp.read(8192)
|
|
||||||
if len(buf) == 0: break
|
|
||||||
rc = self.body(buf)
|
|
||||||
if rc != Milter.CONTINUE: return rc
|
|
||||||
self._msg = msg
|
|
||||||
self._body = StringIO.StringIO()
|
|
||||||
rc = self.eom()
|
|
||||||
if self.bodyreplaced:
|
|
||||||
body = self._body.getvalue()
|
|
||||||
else:
|
|
||||||
msg.rewindbody()
|
|
||||||
body = msg.fp.read()
|
|
||||||
self._body = StringIO.StringIO()
|
|
||||||
self._body.writelines(msg.headers)
|
|
||||||
self._body.write('\n')
|
|
||||||
self._body.write(body)
|
|
||||||
return rc
|
|
||||||
|
|
||||||
def connect(self,host='localhost'):
|
|
||||||
self._body = None
|
|
||||||
self.bodyreplaced = False
|
|
||||||
rc = sample.sampleMilter.connect(self,host,1,0)
|
|
||||||
if rc != Milter.CONTINUE and rc != Milter.ACCEPT:
|
|
||||||
self.close()
|
|
||||||
return rc
|
|
||||||
rc = self.hello('spamrelay')
|
|
||||||
if rc != Milter.CONTINUE:
|
|
||||||
self.close()
|
|
||||||
return rc
|
|
||||||
|
|
||||||
class BMSMilterTestCase(unittest.TestCase):
|
class BMSMilterTestCase(unittest.TestCase):
|
||||||
|
|
||||||
@@ -104,7 +19,7 @@ class BMSMilterTestCase(unittest.TestCase):
|
|||||||
self.failUnless(rc == Milter.CONTINUE)
|
self.failUnless(rc == Milter.CONTINUE)
|
||||||
rc = milter.feedMsg(fname)
|
rc = milter.feedMsg(fname)
|
||||||
self.failUnless(rc == Milter.ACCEPT)
|
self.failUnless(rc == Milter.ACCEPT)
|
||||||
self.failUnless(milter.bodyreplaced,"Message body not replaced")
|
self.failUnless(milter._bodyreplaced,"Message body not replaced")
|
||||||
fp = milter._body
|
fp = milter._body
|
||||||
open('test/'+fname+".tstout","w").write(fp.getvalue())
|
open('test/'+fname+".tstout","w").write(fp.getvalue())
|
||||||
#self.failUnless(fp.getvalue() == open("test/virus1.out","r").read())
|
#self.failUnless(fp.getvalue() == open("test/virus1.out","r").read())
|
||||||
@@ -119,7 +34,7 @@ class BMSMilterTestCase(unittest.TestCase):
|
|||||||
milter.connect('somehost')
|
milter.connect('somehost')
|
||||||
rc = milter.feedMsg(fname)
|
rc = milter.feedMsg(fname)
|
||||||
self.failUnless(rc == Milter.ACCEPT)
|
self.failUnless(rc == Milter.ACCEPT)
|
||||||
self.failIf(milter.bodyreplaced,"Milter needlessly replaced body.")
|
self.failIf(milter._bodyreplaced,"Milter needlessly replaced body.")
|
||||||
fp = milter._body
|
fp = milter._body
|
||||||
open('test/'+fname+".tstout","w").write(fp.getvalue())
|
open('test/'+fname+".tstout","w").write(fp.getvalue())
|
||||||
milter.close()
|
milter.close()
|
||||||
@@ -129,17 +44,17 @@ class BMSMilterTestCase(unittest.TestCase):
|
|||||||
milter.connect('somehost')
|
milter.connect('somehost')
|
||||||
rc = milter.feedMsg('samp1')
|
rc = milter.feedMsg('samp1')
|
||||||
self.failUnless(rc == Milter.ACCEPT)
|
self.failUnless(rc == Milter.ACCEPT)
|
||||||
self.failIf(milter.bodyreplaced,"Milter needlessly replaced body.")
|
self.failIf(milter._bodyreplaced,"Milter needlessly replaced body.")
|
||||||
rc = milter.feedMsg("virus3")
|
rc = milter.feedMsg("virus3")
|
||||||
self.failUnless(rc == Milter.ACCEPT)
|
self.failUnless(rc == Milter.ACCEPT)
|
||||||
self.failUnless(milter.bodyreplaced,"Message body not replaced")
|
self.failUnless(milter._bodyreplaced,"Message body not replaced")
|
||||||
fp = milter._body
|
fp = milter._body
|
||||||
open("test/virus3.tstout","w").write(fp.getvalue())
|
open("test/virus3.tstout","w").write(fp.getvalue())
|
||||||
#self.failUnless(fp.getvalue() == open("test/virus3.out","r").read())
|
#self.failUnless(fp.getvalue() == open("test/virus3.out","r").read())
|
||||||
rc = milter.feedMsg("virus6")
|
rc = milter.feedMsg("virus6")
|
||||||
self.failUnless(rc == Milter.ACCEPT)
|
self.failUnless(rc == Milter.ACCEPT)
|
||||||
self.failUnless(milter.bodyreplaced,"Message body not replaced")
|
self.failUnless(milter._bodyreplaced,"Message body not replaced")
|
||||||
self.failUnless(milter.headerschanged,"Message headers not adjusted")
|
self.failUnless(milter._headerschanged,"Message headers not adjusted")
|
||||||
fp = milter._body
|
fp = milter._body
|
||||||
open("test/virus6.tstout","w").write(fp.getvalue())
|
open("test/virus6.tstout","w").write(fp.getvalue())
|
||||||
milter.close()
|
milter.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user