Sendmail::PMilter(3pm) | User Contributed Perl Documentation | Sendmail::PMilter(3pm) |
Sendmail::PMilter - Perl binding of Sendmail Milter protocol
use Sendmail::PMilter; my $milter = new Sendmail::PMilter; $milter->auto_setconn(NAME); $milter->register(NAME, { CALLBACKS }, FLAGS); $milter->main();
Sendmail::PMilter is a mail filtering API implementing the Sendmail milter protocol in pure Perl. This allows Sendmail servers (and perhaps other MTAs implementing milter) to filter and modify mail in transit during the SMTP connection, all in Perl.
It should be noted that PMilter 0.90 and later is NOT compatible with scripts written for PMilter 0.5 and earlier. The API has been reworked significantly, and the enhanced APIs and rule logic provided by PMilter 0.5 and earlier has been factored out for inclusion in a separate package to be called Mail::Milter.
MAXCHILDREN (default 0, meaning unlimited) specifies the maximum number of connections that may be serviced simultaneously. If a connection arrives with the number of active connections above this limit, the milter will immediately return a temporary failure condition and close the connection.
MAXREQ (default 0, meaning unlimited) is the maximum number of requests that a child may service before being recycled. It is not guaranteed that the interpreter will service this many requests, only that it will not go over the limit.
Any callback which "die"s will have its output sent to "warn", followed by a clean shutdown of the milter connection. To catch any warnings generated by the callbacks, and any error messages caused by a "die", set $SIG{__WARN__} to a user-defined subroutine. (See perlvar.)
NAME is the name of the milter. For compatibility with the official Sendmail::Milter distribution, this should be the same name as passed to auto_getconn() or auto_setconn(), but this PMilter implementation does not enforce this.
CALLBACKS is a hash reference containing one or more callback subroutines. If a callback is not named in this hashref, the caller's package will be searched for subroutines named "CALLBACK_callback", where CALLBACK is the name of the callback function.
FLAGS, if specified, is a bitmask of message modification actions (a bitwise OR of the SMFIF_* constants, or SMFI_CURR_ACTS to ask for all capabilities) that are requested by the callback object for use during message processing. If any bit is not set in this mask, its corresponding action will not be allowed during message processing.
"register()" must be called successfully exactly once. If called a second time, the previously registered callbacks will be erased.
Returns a true value on success, undef on failure.
Returns a true value on success, undef on failure.
The subroutine (code) reference will be called by "main()" when the listening socket object is prepared and ready to accept connections. It will be passed the arguments:
MILTER, LSOCKET, HANDLER
MILTER is the milter object currently running. LSOCKET is a listening socket (an instance of "IO::Socket"), upon which "accept()" should be called. HANDLER is a subroutine reference which should be called, passing the socket object returned by "LSOCKET->accept()".
Note that the dispatcher may also be set from one of the off-the-shelf dispatchers noted in this document by setting the PMILTER_DISPATCHER environment variable. See "DISPATCHERS", below.
The following methods are only useful if Sendmail is the MTA connecting to this milter. Other MTAs likely don't use Sendmail's configuration file, so these methods would not be useful with them.
Returns a true value on success, undef on failure.
Essentially, does:
$milter->setconn($milter->auto_getconn(NAME, CONFIG))
Returns a true value on success, undef on failure.
Milter requests may be dispatched to the protocol handler in a pluggable manner (see the description for the "set_dispatcher()" method above). "Sendmail::PMilter" offers some off-the-shelf dispatchers that use different methods of resource allocation.
Each of these is referenced as a non-object function, and return a value that may be passed directly to "set_dispatcher()".
Currently the child process pool is fixed-size: discarded children will be immediately replaced. This may change to use a dynamic sizing method in the future, more like the Apache webserver's fork-based model.
PARAMS, if specified, is a hash of key-value pairs defining parameters for the dispatcher. The available parameters that may be set are:
The "postfork" dispatcher forks the main Perl process upon each connection to the milter socket. This is adequate for machines that get bursty but otherwise mostly idle mail traffic, as the idle-time resource consumption is very low.
Note that, because the default socket backlog is 5 connections, it may be wise to increase this backlog by calling "set_listen()" before entering "main()" if using this dispatcher.
Each of these symbols may be imported explicitly, imported with tag ":all", or referenced as part of the "Sendmail::PMilter::" package.
As a special exception, SMFIS_REJECT and SMFIS_TEMPFAIL in the "envrcpt" callback will reject only the current recipient, otherwise continuing message processing as if SMFIS_CONTINUE were returned.
SMFIS_CONTINUE - continue processing the message SMFIS_REJECT - reject the message with a 5xx error SMFIS_DISCARD - accept, but discard the message SMFIS_ACCEPT - accept the whole message as-is SMFIS_TEMPFAIL - reject the message with a 4xx error
SMFIF_ADDHDRS - allow $ctx->addheader() SMFIF_CHGBODY - allow $ctx->replacebody() SMFIF_MODBODY - (compatibility synonym for SMFIF_CHGBODY) SMFIF_ADDRCPT - allow $ctx->addrcpt() SMFIF_DELRCPT - allow $ctx->delrcpt() SMFIF_CHGHDRS - allow $ctx->chgheader() SMFIF_QUARANTINE - allow $ctx->quarantine() (requires Sendmail 8.13; not defined in Sendmail::Milter) SMFIF_SETSENDER - allow $ctx->setsender() (requires special Sendmail patch; see below[*]) SMFI_V1_ACTS - SMFIF_ADDHDRS through SMFIF_DELRCPT (Sendmail 8.11 _FFR_MILTER capabilities) SMFI_V2_ACTS - SMFIF_ADDHDRS through SMFIF_CHGHDRS SMFI_CURR_ACTS - (compatibility synonym for SMFI_V2_ACTS) (Sendmail 8.12 capabilities) (Currently no combined macro includes SMFIF_QUARANTINE or SMFIF_SETSENDER.)
[*] NOTE: SMFIF_SETSENDER is not official as of Sendmail 8.13.x. To enable this flag, Sendmail must be patched with the diff available from:
C<http://www.sourceforge.net/projects/mlfi-setsender>
Additionally, the following statement must appear after the "use" statements in your milter program; otherwise, setsender() will always fail when called:
local $Sendmail::PMilter::enable_setsender = 1;
More specifically, though, it is possible to run a milter frontend as root, in order to gain access to network resources (such as a filesystem socket in /var/run), and then drop privileges before accepting connections. To do this, insert drop-privileges code between calls to setconn/auto_setconn and main; for instance:
$milter->auto_setconn('pmilter'); $> = 65534; # drop root privileges $milter->main();
The semantics of properly dropping system administrator privileges in Perl are, unfortunately, somewhat OS-specific, so this process is not described in detail here.
Todd Vierling, <tv@duh.org> <tv@pobox.com>
Since 0.96 Sendmail::Pmilter is no longer maintained on sourceforge.net, cpan:AVAR took it over in version 0.96 to fix a minor bug and currently owns the module in PAUSE.
However this module is effectively orphaned and looking for a new maintainer. The current maintainer doesn't use Sendmail and probably never will again. If this code is important to you and you find a bug in it or want something new implemented please:
Sendmail::PMilter::Context for a description of the arguments passed to each callback function
The project homepage: http://pmilter.sourceforge.net/
rob.casey@bluebottle.com - for the prefork mechanism idea
2021-12-26 | perl v5.32.1 |