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 \s-1API\s0 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 \s-1SMTP\s0 connection, all in Perl.
It should be noted that PMilter 0.90 and later is \s-1NOT\s0 compatible with scripts written for PMilter 0.5 and earlier. The \s-1API\s0 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.
Returns the maximum number of interpreters passed to \*(C`main()\*(C'. This is only useful when called from within the dispatcher, as it is not set before \*(C`main()\*(C' is called.
Returns the maximum number of requests per interpreter passed to \*(C`main()\*(C'. This is only useful when called from within the dispatcher, as it is not set before \*(C`main()\*(C' is called.
This is the last method called in the main block of a milter program. If successful, this call never returns; the protocol engine is launched and begins accepting connections. \s-1MAXCHILDREN\s0 (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. \s-1MAXREQ\s0 (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 \*(C`die\*(C's will have its output sent to \*(C`warn\*(C', followed by a clean shutdown of the milter connection. To catch any warnings generated by the callbacks, and any error messages caused by a \*(C`die\*(C', set $SIG{_\|_WARN_\|_} to a user-defined subroutine. (See perlvar.)
Sets up the main milter loop configuration. \s-1NAME\s0 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. \s-1CALLBACKS\s0 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 \*(L"CALLBACK_callback\*(R", where \s-1CALLBACK\s0 is the name of the callback function. \s-1FLAGS\s0, if specified, is a bitmask of message modification actions (a bitwise \s-1OR\s0 of the SMFIF_* constants, or \s-1SMFI_CURR_ACTS\s0 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. \*(C`register()\*(C' 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.
Sets up the server socket with connection descriptor \s-1DESC\s0. This is identical to the descriptor syntax used by the \*(L"X\*(R" milter configuration lines in sendmail.cf (if using Sendmail). This should be one of the following:
A local (\*(L"\s-1UNIX\s0\*(R") socket on the filesystem, named \s-1PATH\s0. This has some smarts that will auto-delete the pathname if it seems that the milter is not currently running (but this currently contains a race condition that may not be fixable; at worst, there could be two milters running with one never receiving connections).
An IPv4 socket, bound to address \s-1HOST\s0 (default \s-1INADDR_ANY\s0), on port \s-1PORT\s0. It is not recommended to open milter engines to the world, so the @HOST part should be specified.
An IPv6 socket, bound to address \s-1HOST\s0 (default \s-1INADDR_ANY\s0), on port \s-1PORT\s0. This requires IPv6 support and the Perl \s-1INET6\s0 package to be installed. It is not recommended to open milter engines to the world, so the @HOST part should be specified.
Returns a true value on success, undef on failure.
Sets the dispatcher used to accept socket connections and hand them off to the protocol engine. This allows pluggable resource allocation so that the milter script may use fork, threads, or any other such means of handling milter connections. See \*(C`DISPATCHERS\*(C' below for more information. The subroutine (code) reference will be called by \*(C`main()\*(C' when the listening socket object is prepared and ready to accept connections. It will be passed the arguments: MILTER, LSOCKET, HANDLER \s-1MILTER\s0 is the milter object currently running. \s-1LSOCKET\s0 is a listening socket (an instance of \*(C`IO::Socket\*(C'), upon which \*(C`accept()\*(C' should be called. \s-1HANDLER\s0 is a subroutine reference which should be called, passing the socket object returned by \*(C`LSOCKET->accept()\*(C'. Note that the dispatcher may also be set from one of the off-the-shelf dispatchers noted in this document by setting the \s-1PMILTER_DISPATCHER\s0 environment variable. See \*(C`DISPATCHERS\*(C', below.
Set the socket listen backlog to \s-1BACKLOG\s0. The default is 5 connections if not set explicitly by this method. Only useful before calling \*(C`main()\*(C'.
Rather than calling \*(C`setconn()\*(C', this method may be called explicitly to set the \*(C`IO::Socket\*(C' instance used to accept inbound connections.
The following methods are only useful if Sendmail is the \s-1MTA\s0 connecting to this milter. Other MTAs likely don't use Sendmail's configuration file, so these methods would not be useful with them.
Returns the connection descriptor for milter \s-1NAME\s0 in Sendmail configuration file \s-1CONFIG\s0 (default \*(C`/etc/mail/sendmail.cf\*(C' or whatever was set by \*(C`set_sendmail_cf()\*(C'). This can then be passed to setconn(), below. Returns a true value on success, undef on failure.
Creates the server connection socket for milter \s-1NAME\s0 in Sendmail configuration file \s-1CONFIG\s0. Essentially, does: $milter->setconn($milter->auto_getconn(NAME, CONFIG)) Returns a true value on success, undef on failure.
Returns the pathname of the Sendmail configuration file set by \*(C`set_sendmail_cf()\*(C', else the default of \*(C`/etc/mail/sendmail.cf\*(C'.
Returns a list containing all members of the Sendmail class \s-1CLASS\s0, in Sendmail configuration file \s-1CONFIG\s0 (default \*(C`/etc/mail/sendmail.cf\*(C' or whatever is set by \*(C`set_sendmail_cf()\*(C'). Typically this is used to look up the entries in class \*(L"w\*(R", the local hostnames class.
Set the default filename used by \*(C`auto_getconn\*(C', \*(C`auto_setconn\*(C', and \*(C`sendmail_class\*(C' to find Sendmail-specific configuration data. If not explicitly set by this method, it defaults to \*(C`/etc/mail/sendmail.cf\*(C'.
Milter requests may be dispatched to the protocol handler in a pluggable manner (see the description for the \*(C`set_dispatcher()\*(C' method above). \*(C`Sendmail::PMilter\*(C' 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 \*(C`set_dispatcher()\*(C'.
The \*(C`ithread\*(C' dispatcher spins up a new thread upon each connection to the milter socket. This provides a thread-based model that may be more resource efficient than the similar \*(C`postfork\*(C' dispatcher. This requires that the Perl interpreter be compiled with \*(C`-Duseithreads\*(C', and uses the \*(C`threads\*(C' module (available on Perl 5.8 or later only).
The \*(C`prefork\*(C' dispatcher forks the main Perl process before accepting connections, and uses the main process to monitor the children. This should be appropriate for steady traffic flow sites. Note that if \s-1MAXINTERP\s0 is not set in the call to \*(C`main()\*(C' or in \s-1PARAMS\s0, an internal default of 10 processes will be used; similarly, if \s-1MAXREQ\s0 is not set, 100 requests will be served per child. 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. \s-1PARAMS\s0, if specified, is a hash of key-value pairs defining parameters for the dispatcher. The available parameters that may be set are:
subroutine reference that will be called after each child process is forked. It will be passed the \*(C`MILTER\*(C' object.
subroutine reference that will be called just before each child process terminates. It will be passed the \*(C`MILTER\*(C' object.
Maximum number of child processes active at any time. Equivalent to the \s-1MAXINTERP\s0 option to main() \*(-- if not set in the main() call, this value will be used.
Maximum number of requests a child process may service before being recycled. Equivalent to the \s-1MAXREQ\s0 option to main() \*(-- if not set in the main() call, this value will be used.
In this release, this is the default dispatcher for PMilter if no explicit dispatcher is set. The \*(C`postfork\*(C' 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.
The \*(C`sequential\*(C' dispatcher forces one request to be served at a time, making other requests wait on the socket for the next pass through the loop. This is not suitable for most production installations, but may be quite useful for milter debugging or other software development purposes. Note that, because the default socket backlog is 5 connections, it may be wise to increase this backlog by calling \*(C`set_listen()\*(C' before entering \*(C`main()\*(C' if using this dispatcher.
Each of these symbols may be imported explicitly, imported with tag \*(C`:all\*(C', or referenced as part of the \*(C`Sendmail::PMilter::\*(C' package.
Of these, \s-1SMFIS_CONTINUE\s0 will allow the milter to continue being called for the remainder of the message phases. All others will terminate processing of the current message and take the noted action. As a special exception, \s-1SMFIS_REJECT\s0 and \s-1SMFIS_TEMPFAIL\s0 in the \*(C`envrcpt\*(C' callback will reject only the current recipient, otherwise continuing message processing as if \s-1SMFIS_CONTINUE\s0 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
These values are bitmasks passed as the \s-1FLAGS\s0 argument to \*(C`register()\*(C'. Some MTAs may choose different methods of resource allocation, so keeping this list short may help the \s-1MTA\s0's memory usage. If the needed capabilities are not known, however, \*(C`SMFI_CURR_ACTS\*(C' should be used. 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.) [*] \s-1NOTE:\s0 \s-1SMFIF_SETSENDER\s0 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 \*(L"use\*(R" statements in your milter program; otherwise, setsender() will always fail when called: local $Sendmail::PMilter::enable_setsender = 1;
Running Perl as root is dangerous. Running \*(C`Sendmail::PMilter\*(C' as root may well be system-assisted suicide at this point. So don't do that. 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, <[email protected]> <[email protected]>
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 \s-1PAUSE\s0.
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:
Fork it & fix it on GitHub at <http://github.com/avar/sendmail-pmilter>
Send \s-1AVAR\s0 an E-Mail requesting upload permissions so you can upload the fixed version to the \s-1CPAN\s0.
Sendmail::PMilter::Context for a description of the arguments passed to each callback function
The project homepage: http://pmilter.sourceforge.net/
[email protected] - for the prefork mechanism idea