Recipe 6.9 Invoking Special Header Processing

Problem

You need to add customized header checks to the sendmail configuration.

Solution

Append your custom header processing to the end of the master configuration file using a LOCAL_CONFIG macro and a LOCAL_RULESETS macro. The LOCAL_CONFIG macro adds lines to the local information section of the sendmail.cf file, and, therefore, is used to define any macros, classes, or databases used by your customer header process. Use the LOCAL_RULESET macro to add your custom ruleset to the sendmail.cf file. The Discussion section provides an example of both of these macros.

Build the sendmail.cf file, copy it to /etc/mail/sendmail.cf , and restart sendmail, as described in Recipe 1.8.

Discussion

The knecht.mc file, which comes with the sendmail distribution, contains a variety of examples that illustrate different aspects of sendmail configuration. The following sample custom header check is taken from the knecht.mc file:

 LOCAL_CONFIG # #  Names that won't be allowed in a To: line (local-part and domains) # C{RejectToLocalparts}   friend you C{RejectToDomains}      public.com LOCAL_RULESETS HTo: $>CheckTo SCheckTo R$={RejectToLocalparts}@$*      $#error $: "553 Header error" R$*@$={RejectToDomains}         $#error $: "553 Header error" 

Custom header processing requires sendmail.cf code in addition to the normal m4 configuration. The LOCAL_CONFIG macro marks the beginning of lines that are added directly to the local information section of the sendmail.cf file. In the example, the two C lines that follow the LOCAL_CONFIG macro define two classes and load those classes with some values.

The LOCAL_RULESET macro indicates that a locally defined ruleset follows. The first line in the example that follows LOCAL_RULESET is the header command that calls the custom ruleset:

 HTo: $>CheckTo 

This H command calls the ruleset CheckTo whenever a To : header arrives in a mail stream from a remote system. The $> syntax is the standard way that rulesets are called from rewrite rules and header definitions.

The ruleset itself begins with an S command that defines a ruleset name CheckTo . The ruleset contains two rewrite rules. The first rule matches any To : header containing an address with a username found in the $={RejectToLocalparts} class. In the example, that would be any mail addressed to the username friend or the username you . The mail addressed to either of these usernames is rejected with the error message "553 Header error."

The second rewrite rule matches any To : header that addresses mail to a hostname found in the $={RejectToDomains} class. In the example, the class contains only the hostname public.com . The mail addressed to public.com is rejected with the error message "553 Header error."

The CheckTo ruleset is easily tested with sendmail -bt :

 #  sendmail -bt  ADDRESS TEST MODE (ruleset 3 NOT automatically invoked) Enter <ruleset> <address> >  CheckTo friend@wrotethebook.com  CheckTo            input: friend @ wrotethebook . com CheckTo          returns: $# error $: "553 Header error" >  CheckTo craig@public.com  CheckTo            input: craig @ public . com CheckTo          returns: $# error $: "553 Header error" >  CheckTo craig@wrotethebook.com  CheckTo            input: craig @ wrotethebook . com CheckTo          returns: craig @ wrotethebook . com >  /quit  

The ruleset is called and passed the contents of a sample To : header. Note that the ruleset is passed the contents of the header without the header name. This replicates the way that the ruleset will be called during an actual run. In all three tests, the CheckTo ruleset works as expected ”the username friend and the hostname public.com are rejected but the address craig@wrotethebook.com passes through the ruleset unscathed.

The CheckTo ruleset is a simplified example. Implementing a customer header processing ruleset that would be effective for fighting spam would be much more complex. However, the same H command syntax for calling a custom header ruleset, the same LOCAL_CONFIG macro, and the same LOCAL_RULESET macro used in this example would implement any custom header processing. Before you create a custom header process inside sendmail to battle spam, evaluate the alternatives, such as filtering the mail with a MILTER or procmail , and make sure your approach is the simplest and most effective to implement and maintain.

See Also

Recipe 6.10 also uses an example from knecht.mc . Recipe 6.7 and Recipe 6.8 cover procmail and Recipe 6.12 covers MILTER, which are alternatives for processing headers that should be evaluated before you write custom sendmail.cf rulesets. The sendmail book covers the LOCAL_RULESETS macro in Section 4.3.3.5 and the LOCAL_CONFIG macro in Section 4.3.3.1. See TCP/IP Network Administration , Third Edition, by Craig Hunt (O'Reilly), and Linux Sendmail Administration , by Craig Hunt (Sybex), for additional information about the sendmail.cf commands.



Sendmail Cookbook
sendmail Cookbook
ISBN: 0596004710
EAN: 2147483647
Year: 2005
Pages: 178
Authors: Craig Hunt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net