# amavisd-new documentation bits and pieces

The most recent version of this document is available at http://www.ijs.si/software/amavisd/amavisd-new-docs.html

- performing mail checks
- acting on mail checks results
- tag, tag2 and kill levels
- quarantine
- releasing from a quarantine
- redirecting malware to a different mailbox -- plus addressing
- hard black- and whitelisting senders regarding spam
- soft black- and whitelisting senders regarding spam -- @score_sender_maps
- configuration variables
- policy banks
- putting policy banks to good use -- examples
- $max_requests
- setting up DKIM and DomainKeys mail signing and verification

## Performing mail checks

The following checks on mail are available

- mail header validity checks
- banned names and types checks
- virus checks
- spam checks
- is sender white- or blacklisted (regarding spam)

Although checks are presently not performed in parallel, it is best to consider the order of their evaluation unspecified (unknown). Besides possible future parallel implementation, another reason is the caching of results, where subsequent mail with the same contents may benefit from earlier checks if validity of these check results has not yet expired -- so a check result may be instantly available, regardless of whether it has been asked for or not.

Using configuration variables @bypass_virus_checks_maps, @bypass_banned_checks_maps, @bypass_header_checks_maps and @bypass_spam_checks_maps each recipient (or administrator on their behalf) may suggest that certain tests are not needed, primarily for performance reasons. Although the @bypass_*_checks_maps pertain to individual recipients, a mail check is an operation done on the whole message, regardless of the number of recipients and their individual preferences. Suggestion by some of the recipients that certain check is not needed (is to be bypassed) does not guarantee the test will not be performed.

Similarly the (hard) blacklisting or whitelisting of sender address may make running spam check unnecessary, but it does not guarantee the spam check result will not be available for subsequent decisions.

There are two primary reasons why a check result may still be available despite the bypass hint or a sender being black- or whitelisted:

- a check result from some previous mail with the same contents has been cached and is still valid;
- when mail has multiple recipients and not all of them agree that a check should be bypassed.

The amavisd-new program is allowed to skip some check for performance reasons if all recipients agree that a check is not necessary (that it may be bypassed), or if the outcome of a check to be skipped could not influence further mail processing and delivery/non-delivery of the message (as is the case of a sender being black- or whitelisted regarding spam check).

For example spam checks may be skipped if it is already known that a mail is infected. This is an implementation and optimization issue, and no guarantee is given about interdependency of checks. Future version may use a different strategy of performing checks (e.g. some checks may be performed in parallel), as long as a change does not affect the final outcome.

## Acting on mail checks results

Based on the outcome of mail checks performed during mail analysis or cached from previous mail with the same contents, and based on global settings and individual recipient preferences, the program now decides what action to perform next. As described in the previous section, not all results of checks are necessarily known (e.g. if all recipients voted for some check to be bypassed). For the purpose of deciding further actions, unknown results of a check are considered equivalent to negative (false) results, i.e. skipped virus check is treated the same as non-infected mail, bypassed spam check is equivalent to low spam score (ham).

The following decisions are made at this stage:

- whether a mail should be quarantined and how;
- whether an administrator (and which administrator) should receive a notification (and which notification);
- whether recipients should receive a notification;

and regarding mail delivery and/or sender (non)delivery notifications:

- whether a mail should be delivered to each recipient or not;
- whether delivered mail should be modified (header edits, defanging);
- whether a sender should receive a (non)delivery notification (bounce);
- what should be the final status code returned to the mailer (reject/pass).

For the purpose of deciding on these actions, a mail is classified based on all available checks results. It is quite possible that more than one check results would be positive (e.g. virus and banned and bad header, or spam and bad header, or virus and spam), yet a mail is considered to be only in one category. The logic is currently hard-wired into the program and can not be influenced by configuration variables. The following order is used, the first condition met decides the outcome:

1. a virus is detected: mail is considered infected;
2. contains banned name or type: mail is considered banned;
3. spam level is above kill level for at least one recipient, or a sender is blacklisted: mail is considered spam;
4. bad (invalid) headers: mail is considered as having a bad header.

This decision order explains why amavisd-new is not free to skip (to optimize away) virus checks if a presence of a banned name or a bad header is already known or can easily be determined. The order was chosen with the intention that a more informative or a stronger assertion is the one to base further mail delivery on, and to be quoted in notifications and in the log. Even at the expense of possibly longer processing time, it is more important to declare a mail infected than complain about a bad header, a banned executable or spamy contents.

The determined mail category now governs further action. Administrators are notified if enabled for the category, mail is quarantined if quarantining if enabled for the category, recipients are notified if enabled for the category.

Next a mail delivery is attempted. A decision to deliver depends on mail category and on global and individual recipient preferences. The global setting $final_*_destiny=D_PASS or a per-recipient setting @*_lovers_maps ensure mail delivery for corresponding mail category even if mail would otherwise be blocked for being infected or banned or spam or having a bad header.

A mail that is decided to be passed to an individual recipient undergoes some simple header editing which happens on-the-fly during mail forwarding. Certain mail header fields may be inserted or removed, or an existing header field (e.g. Subject) may be modified. This header editing may be different for each recipient even in multi-recipient messages. If necessary, a multi-recipient mail is split into more than one forwarding transaction, grouping (clustering) recipients with same settings into one SMTP transaction.

Based on decisions to forward or to block mail to each recipient, and on the global setting for the mail category ($final_*_destiny=D_BOUNCE or D_REJECT), the sender (non)delivery notification is now prepared in case of D_BOUNCE, and MTA receives a 2xx status (success); or in case of D_REJECT the MTA receives a 5xx (reject) status and preparing sender notifications is thus delegated to MTA (not recommended in post-queue or dual-MTA content filtering setup).

Even in cases of mail non-delivery when a (non-)delivery status notification (DSN) for the sender should have been prepared and sent, there are certain exceptions where the DSN is suppressed, which makes mail effectively lost as far as the sender and the recipient are concerned (but quarantining is not affected):

- when $final_*_destiny=D_DISCARD;
- when mail is infected and the detected virus name matches the @viruses_that_fake_sender_maps (unconditionally true by default);
- when spam score exceeds level determined by @spam_dsn_cutoff_level_maps for all recipients;
- when mail is coming from a mailing list, as determined by examining a mail header Precedence: for containing string 'bulk' or 'list' or 'junk';

## tag, tag2 and kill levels

When SpamAssassin is called upon to analyze a mail message, it returns a spam score (spam level, hits), which is a numeric representation of spaminess. The higher the number, the more spamy the message is considered. Small numbers near zero or negative indicate a clean message, colloquially called ham. Spam score is a characteristic of the whole message, and does not depend on recipient preferences. SpamAssassin is called only once for each message regardless of the number of recipients.

To determine further course of action, amavisd-new compares the spam score to three numeric values: tag level, tag2 level and kill level. These values may be different for each recipient, and further actions may be different for each recipient. If necessary, the mail forwarding is split into more than one transaction to cater for different recipient preferences.

tag level
  if spam score is at or above tag level, spam-related header fields (X-Spam-Status, X-Spam-Level) are inserted for local recipients; undefined (unknown) spam score is interpreted as lower than any spam score;
tag2 level
  if spam score is at or above tag2 level, spam-related header fields (X-Spam-Status, X-Spam-Level, X-Spam-Flag and X-Spam-Report) are inserted for local recipients, and X-Spam-Flag and X-Spam-Status bear a YES; also recipient address extension (if enabled) is tacked onto recipient address for local recipients; for these actions to have any effect, mail must be allowed to be delivered to a recipient;
kill level
  if spam score is at or above kill level, mail is blocked; and sender receives a nondelivery notification unless spam score exceeds dsn cutoff level.

The general idea is that kill level is what controls the main actions as far as MTA and amavisd-new is concerned (regardless of what recipients' MUA later does with the mail).

Reaching kill level for at least one recipient controls the following:

- mail gets quarantined (unless disabled)
- spam administrator gets a notification (unless disabled)
- ContentSpamMsgs counter is incremented
- sender gets a notification if warnspamsender is true and $final_spam_destiny is D_PASS
- if message is not delivered, sender gets a nondelivery notification (suppressed under certain conditions)
- the main log entry says: Passed/Blocked SPAM.

On the other hand the tag2 level just adds some mark to the passed mail (only for local recipients), which recipient or his MUA may decide to act on or not. Specifically:

- Subject header field is modified (unless disabled)
- X-Spam-Flag and X-Spam-Status header field get a Yes
- address extension for spam gets tacked on the recipient address
- spam defanging is done (unless disabled)
- the main log entry says: Passed/Blocked SPAMMY.

For mail below kill level, if a recipient (or his MUA) decides to discard a message based on tag2 marking, there is no way to retrieve it later from a quarantine, the sender is never notified, spam administrator is never notified. As far as the MTA and amavisd-new are concerned, the message was successfully delivered. Whatever MUA does with the mail is entirely the responsibility and jurisdiction of the recipient and his LDA and MUA.

## Quarantine

Mail quarantining is attempted when enabled for a given contents category, which usually includes infected or banned or spam mail with score for at least one of its recipients at or above his kill level. It is also possible to enable quarantining of clean messages for archiving or troubleshooting purposes. The *quarantine_to for each recipient (when nonempty), along with a global corresponding *_quarantine_method, determines where the quarantine location should be.

The *_quarantine_method can be considered a static and a site-wide setting, generally controlling a format and location of the quarantine on the system. The *quarantine_to can be considered a dynamic part of the quarantine location, possibly affected by per-recipient settings and the class of malware (contents category). It serves to fully specify the final location, e.g. a file or a mailbox.

Depending on mail contents category (type of malware), the following variables specify the quarantine method: `$virus_quarantine_method`, `$spam_quarantine_method`, `$banned_files_quarantine_method`, and `$bad_header_quarantine_method`. One way to globally disable quarantine is to specify undef or an empty string as a value of these variables. A nonempty string should follow a syntax:

- `local:`filename-template
- `bsmtp:`filename-template
- `smtp:`hostname`:`port
- `smtp:[`ip-address-or-hostname`]:`port
- `pipe:`argv=command args...
- `sql:`anything

The `local:`, `bsmtp:` and `sql:` methods are the usual methods for quarantining. The `smtp:` method is only useful for quarantining if quarantine location is some dedicated mailbox instead of a local file or directory. The `smtp:` and `pipe:` methods are more often used for forwarding and notifications.

Depending on the method specified (local/bsmtp/smtp/sql) a per-recipient setting *quarantine_to adopts different semantics and syntax, possibly modified by the configuration variable `$QUARANTINEDIR`.

method | quarantine_to | `$QUARANTINEDIR` | effect
--- | --- | --- | ---
anything | empty or undef | anything | not quarantined
empty or undef | anything | anything | not quarantined
`local:` | pseudo-alias mapped through %local_delivery_aliases | directory | stored as an individual file below the directory `$QUARANTINEDIR`, file name comes from the template specified in the *_quarantine_method
`local:` | pseudo-alias mapped through %local_delivery_aliases | filename of a mailbox | appended to a file `$QUARANTINEDIR` in mbox format
`local:` | pseudo-alias mapped through %local_delivery_aliases | empty or undef | not quarantined
`local:` | e-mail address containing '@'-sign | anything | sent via SMTP to a mailer for storage, uses $notify_method to specify how to deliver to MTA; much like a newer '`smtp:`' entry below
`smtp:` | e-mail address | anything | sent via SMTP to a mailer for storage, uses the specified IP address and port for delivery; formerly a '`local:`' method was used for this purpose
`bsmtp:` | anything (nonempty) | anything | stored in a file specified in the *_quarantine_method in BSMTP format (if file name is absolute, i.e. starts with a "/")
`bsmtp:` | anything (nonempty) | directory | stored in a file specified in the *_quarantine_method in BSMTP format (file name relative to `$QUARANTINEDIR`)
`sql:` | anything (nonempty) | anything | stored into SQL database specified by @storage_sql_dsn
`sql:` | empty or undef | anything | not quarantined

The *quarantine_to is currently quite limited in functionality, it is often used only to turn off the quarantining for some user or local subdomain. The reason for this limited functionality is a more vulnerable nature of this value, as it may come from SQL or LDAP lookups where non-careful access controls to these databases might permit users to enter any value in the *quarantine_to field, which is why we do not let it control the directory or the exact file name of the quarantine file. This may be somewhat relaxed in the future.

In common setups the quarantine location (e.g. a directory or a dedicated mailbox) is the same for all recipients. If at least one recipient specifies a nonempty *quarantine_to specifying this location, the message is quarantined (stored) there once, regardless of the number of recipients.

The general algorithm is: the *quarantine_to value associated with each recipient is looked up. Empty or undef values are ignored and duplicates are discarded. A mail to be quarantined is then stored/sent to each unique location remaining on the list.

The "bsmtp:" quarantine method is somewhat special in that the quarantine file location is entirely determined by the *_quarantine_method setting, and the value of per-recipient *quarantine_to settings do not influence the quarantine location, as long as this value is nonempty.

When using the "bsmtp:" quarantine method and versions of amavisd-new earlier than 2.2.0, the *_quarantine_to was completely ignored, which made it impossible to turn off quarantining selectively for certain users by specifying an empty or undef value. Since 2.2.0, an empty *_quarantine_to turns off quarantine for a recipient regardless of the quarantine method. A nonempty string in *_quarantine_to (the exact value is ignored) must now be used even with "bsmtp:" to enable quarantining.

## Releasing from a quarantine

The utility amavisd-release tells the amavisd daemon to fetch a mail from a local quarantine, and send it to MTA through its regular channels ($notify_method), bypassing re-checking.

By default it connects to socket /var/amavis/amavisd.sock, on which amavisd should be listening for AM.PDP protocol, but one can use inet socket instead of a Unix socket if there is a need to run amavisd-release from a remote host.

In the amavisd.conf the following should be added:

    $unix_socketname = "$MYHOME/amavisd.sock";  # listen on Unix socket

    # alternatively (less common):
    # $inet_socket_port = [10024, 9998];  # listen on listed inet tcp ports

    # apply policy bank AM.PDP-SOCK on a Unix socket:
    #  (note that this precludes the use of old amavis-milter
    #   helper program (with sendmail) on the same socket)
    $interface_policy{'SOCK'} = 'AM.PDP-SOCK';

    # apply policy bank AM.PDP-INET to some inet tcp socket, e.g. tcp port 9998:
    $interface_policy{'9998'} = 'AM.PDP-INET';

    $policy_bank{'AM.PDP-SOCK'} = {
      protocol => 'AM.PDP',  # select Amavis policy delegation protocol
      auth_required_release => 0,  # don't require secret_id for amavisd-release
    };
    $policy_bank{'AM.PDP-INET'} = {
      protocol => 'AM.PDP',  # select Amavis policy delegation protocol
      inet_acl => [qw( 127.0.0.1 [::1] )],  # restrict access to these IP addresses
    # auth_required_release => 0,  # don't require secret_id for amavisd-release
    };

Setting of $auth_required_release decides whether the requestor needs to specify secret_id in addition to mail_id to authorize a mail release. The secret_id is stored in SQL table msgs when logging to SQL is enabled, otherwise this information is not accessible.

Note that turning off $auth_required_release check is safe as long as access to the socket is restricted, like with file protections on a Unix socket, or restricted with inet_acl to specific IP addresses. Enabling or disabling $auth_required_release is a management / setup decision and convenience.

To release a mail message an exact quarantine location from a log file should be specified as an argument to amavisd-release, e.g.:

    amavis[29297]: (29297-01-6) Blocked SPAM,
      ... <xxx> -> <yyy>,
      quarantine: spam/U/UM3XM3XDbN52.gz,
      Message-ID:<...>, mail_id: UM3XM3XDbN52, Hits: 13.365,

    $ amavisd-release spam/U/UM3XM3XDbN52.gz
    250 2.6.0 Ok, id=rel-UM3XM3XDbN52,
      from MTA([193.2.4.66]:10025): 250 2.0.0 Ok: queued as F137717B88B

The amavisd-release utility also accepts mail_id from STDIN if releasing more than one message in one go is more convenient:

    $ amavisd-release -
    spam/U/UM3XM3XDbN52.gz
    spam/g/gnwKVFKiuey3.gz
    spam/X/Xpkj9mLLBHTR.gz

## Redirecting malware to a different mailbox -- plus addressing

Amavisd-new can tag passed malware by appending an address extension to a recipient address. An address extension is usually a short string (such as 'spam') appended to the local part of the recipient address, delimited from it by a single character delimiter, often a '+' (or sometimes a '-'). This is why address extensions are also known as "plus addressing". Examples of such mail addresses belonging to user jim@example.com are: jim+spam@example.com, jim+cooking@example.com, jim+health@example.com, jim+postfix@example.com.

Most mailers (MTA), including Postfix and sendmail, have some provision to put address extensions to good use. Similarly, local delivery agents (LDA) such as Cyrus or LDAs that come with MTA, can be configured to recognize and make use of address extensions.

The most common application for address extensions is to provide additional information to LDA to store mail into a separate mail folder. Users may for example choose to use this feature to let LDA automatically file messages from mailing lists to a dedicated subfolder, or to file spam to a spam folder, just by letting LDA simply and quickly examine the envelope recipient address, without having to parse mail header or having to configure and run filters such as procmail or Sieve.

Mailers (MTA and LDA) usually attempt first to examine (to check for validity, to lookup in virtual or aliases maps) a full unmodified recipient address. If the attempt is unsuccessful, they strip away the extension part, and try again. This way a presence of some unknown address extension is simply ignored. For example, a delivery for jim+health@example.com would deliver the mail to the main Jim's inbox if he hasn't provided a subfolder health in his mailbox.

For this fallback to work (to ignore unknown extensions), it is important that all components that need to deal with address extensions (MTA, LDA, content filters) have the same notion of the delimiter in use on the system. For Postfix the configuration option is `recipient_delimiter=+` (see also propagate_unmatched_extensions), for amavisd-new the option is `$recipient_delimiter='+';` for Cyrus the delimiter is hardcoded as '+', see Cyrus IMAP FAQ -> plus addressing.

The amavisd-new configuration options for adding address extensions are @addr_extension_virus_maps, @addr_extension_spam_maps, @addr_extension_banned_maps, @addr_extension_bad_header_maps. The configuration must also ensure the malware mail is to be delivered, otherwise there is nothing to tack an address extension on -- either by setting kill level sufficiently high, or by declaring spam lovers, or by `$final_spam_destiny=D_PASS`; an example:

    $recipient_delimiter = '+';
    @addr_extension_spam_maps = ('spam');
    $sa_tag2_level_deflt = 6.7 ;    # score above which spam extension is added
    $sa_kill_level_deflt = 15;      # block higher score entirely
    $final_spam_destiny=D_DISCARD;  # junk all above kill level

or provide extension string more selectively for certain users or subdomains:

    @addr_extension_spam_maps = (
      { '.sub1.example.com' => 'spam',     # an entire subdomain
        'user1@example.com' => 'spam',     # a particular user
        'user2@example.com' => 'malware',  # another user wants a different ext.
        '.'                 => '' }  # all the rest do not receive an extension
    );

If one is considering using a quarantine mechanism but wants a per-user (or perhaps per-subdomain) quarantines, this is not such a good idea, because quarantined files are not supposed to be directly visible or handled by recipients: to protect the privacy of the sender, some header pre-processing must be performed on a quarantined file before handing it over to a recipient.

The cleanest way to achieve per-user quarantine which may be directly accessible and/or manipulated by recipients is to turn on adding address extensions, and configure MTA and/or LDA to store such mail wherever necessary, either to a user's dedicated subfolder, or perhaps to some centralized dedicated set of malware mailboxes (per-user or perhaps per-subdomain).

If it is desired to reroute extension-tagged mail to some mailbox away from the usual LDA, the virtual alias mapping by MTA is the tool for the job. With Postfix, a pcre-based virtual map can specify for example:

    /^(.*)\+spam@([^@]*)\.example\.com$/   spam-$2-box@example.com

which will collect all spam into one mailbox for each subdomain.

For the Postfix local(8) LDA, a presence of a file $HOME/.forward+spam can redirect mail for user+spam to some dedicated file. For the Postfix virtual(8) LDA, a virtual_mailbox_maps may contain entries like:

    user1         mbxfile1
    user1+spam    mbxspamfile1
    user2         mbxfile2
    user2+spam    mbxspamfile2

## Hard black- and whitelisting senders regarding spam

The blacklisting and the whitelisting are ways of telling that we already know that a message is spam or is ham (non-spam) just by examining the envelope sender address and comparing it to lists of known spammers or to lists of known legitimate senders of ham. It is a quick check, potentially saving us the trouble of examining the mail contents. It has a big drawback however in that the sender mail address can be (and often is) faked and there is no guarantee that the claimed sender address represents the actual sender.

The sender address is usually faked for spam messages, so whitelisting some sender address is a of questionable value, and often lets in far more spam than it does good by approving legitimate mail. For a reliable way of permitting certain sending clients to send spamy mail see policy banks.

Blacklisting however is still useful: spammer has no desire to pretend to be some blacklisted sending address, when he can choose any other address. Genuine sender that is intentionally blacklisted can only avoid being blocked by falsifying his address (joining spammers in his methods) and sending non-spamy mail, the later being our objective anyway. Although amavisd-new does provide blacklisting, it is functionally equivalent but more effective to blacklist senders at the MTA, preventing such mail from even entering the mail system.

It should be emphasized that whitelisting (and blacklisting) only affects spam checks. It has no influence on other checks such as virus, banned or header checks. Infected mail from whitelisted sender would still be blocked if our policy is to block viruses.

Another point to bear in mind is that the sender address examined is the one from the SMTP protocol, exactly as provide by MTA to amavisd-new. It is known as the envelope sender address or return path. This address does not necessarily match the mail author's address from the mail header (From:) or the sender's address from the header (Sender:). This is most obvious with mail from mailing lists, where the envelope sender address is usually the address of a mailing list management service, while the author's address (From:) is the address of a person sending the message. Using the envelope sender address in most cases makes it easier to black- or whitelist mail from mailing lists, compared to guessing a sender address by parsing mail header.

To avoid surprises, whitelisted sender suppresses inserting/editing the tag2-level header fields (X-Spam-*, Subject), appending spam address extension, and quarantining, even if we know the message is spam (e.g. because the spam check result on the same mail contents has been cached from some earlier mail or known from check on behalf of another recipient).

For mail from blacklisted senders, the effect is as if the spam level were artificially pushed high, resulting in 'X-Spam-Flag: YES', high 'X-Spam-Level' bar and other usual reactions to spam, including possible rejection. If the message nevertheless still passes (e.g. for spam loving recipients), it is tagged as BLACKLISTED in the 'X-Spam-Status' header field, but the reported spam value and set of tests in this report header field is not adjusted (if available from SpamAssassin, which may or may not have been called)

If all recipients of a message either white- or blacklist the sender, amavisd is free to skip spam scanning (calling the SpamAssassin), saving on time. There is no guarantee however that spam scanning will actually and always be skipped.

The following variables (lists of lookup tables) are available, with the semantics and syntax as specified in README.lookups: @whitelist_sender_maps, @blacklist_sender_maps, which implement global policy applicable to all recipients. Similarly there are $per_recip_blacklist_sender_lookup_tables and $per_recip_whitelist_sender_lookup_tables, which make possible for each recipient or subdomain to specify its own set of black- or whitelisted senders. The per-recipient tables take precedence over global tables.

For SQL lookups, amavisd-new will first lookup the recipient in table users in order of descending priority, e.g. user@sub.domain.org, user, @.sub.domain.org, @.domain.org, @.org, and @. (which can be considered a catchall). Each matching recipient record may have a list of senders associated (through join on field users.id and wblist.rid). The sender address is then looked up in the associated list of senders (wblist) in order of descending priority, e.g. sender@sub.example.com, @.sub.example.com, @.example.com, @.com, and @. . This search stops at the first matching sender record with a non-NULL field wblist.wb. The value of a field wblist.wb from the matched record determines if the sender is considered whitelisted ('W'), blacklisted ('B') or neutral (' ') for this recipient.

The neutral value is there just as a way to explicitly stop the search, which may be used by a recipient to overrule site-wide or static white- or blacklisting defaults for some specific sender, and to explicitly neither whitelist nor blacklist the sender, letting the normal spam check determine the spaminess of a mail.

For recipient user@sub.domain.com and sender sender@sub.example.com the following search is performed:

    user@sub.domain.org
      sender@sub.example.com @.sub.example.com @.example.com @.com @.

    user
      sender@sub.example.com @.sub.example.com @.example.com @.com @.

    @.sub.domain.org
      sender@sub.example.com @.sub.example.com @.example.com @.com @.

    @.domain.org
      sender@sub.example.com @.sub.example.com @.example.com @.com @.

    @.org
      sender@sub.example.com @.sub.example.com @.example.com @.com @.

    @.
      sender@sub.example.com @.sub.example.com @.example.com @.com @.

## Soft black- and whitelisting senders regarding spam -- @score_sender_maps

Instead of hard black- or whitelisting a sender address (unconditionally considering mail spam or ham solely based on sender address regardless of mail contents), a more gentle approach is to add score points (penalties) to the spam score for mail from certain senders or sending domains. Positive points lean towards blacklisting, negative towards whitelisting. This is much like adding SpamAssassin rules or using its white/blacklisting, except that here only envelope sender addresses are considered (not addresses in a mail header), and that score points can be assigned per-recipient (or per-domain or globally), and that the assigned penalties are customarily much lower than the default SpamAssassin white/blacklisting score.

The table structure of @score_sender_maps is similar to $per_recip_blacklist_sender_lookup_tables i.e. the first level key is recipient address, pointing to by-sender lookup tables. The essential difference is that scores from all matching by-recipient lookups (not just the first that matches) are summed to give the final score boost. That means that both the site and domain administrators, as well as the recipient can have a say on the final score.

For SQL lookups, the mechanism is much like the one described for hard black- or whitelisting, with the following differences:

- the field wblist.wb is numeric, representing score points, instead of containing a character W or B or space;
- the search through matching recipients does not stop at the first match, but traverses all matching recipients, summing up the corresponding wblist.wb field values.

Namely, amavisd will lookup the recipient, e.g. user@sub.domain.org, user, @.sub.domain.org, @.domain.org, @.org, and @. . Since the search will not stop at the first recipient match, the search order in this case is unimportant, although it is actually the same descending-priority order as with hard b/w listing. Each matching recipient record may have a list of senders associated (through join on field users.id and wblist.rid). The sender address is then looked up in the associated list of senders (wblist) in order of descending priority, e.g. sender@sub.example.com, @.sub.example.com, @.example.com, @.com, and @. . This search stops at the first matching sender record with a non-NULL field wblist.wb, but this does not terminate the outer recipients search. Numeric values of a field wblist.wb from matched records are summed up across all matching recipients tables, and the result is added to the spam score as produced by SpamAssassin.

Unlike static tables, where hard and soft w/b-listing use separate tables, the SQL-based hard and soft w/b-listing uses the same SQL tables and the same field wblist.wb. Mixing the 'W', 'B' with numeric values is somewhat frowned upon, but is supported to facilitate transition. The search goes like described above as long as only numeric field values are encountered, summing up the values and adding the accumulated sum to the final score. If a non-numeric value of field wblist.wb is encountered during this search, its value (W or B or space) is interpreted as described for hard w/b listing, and the search stops at this point.

## Configuration variables

The behaviour of the amavisd-new is controlled by a set of configuration variables, which are just normal module-global Perl variables (in package Amavis::Conf). At daemon startup time these variables are first assigned an initial value (often just an undefined value, the undef). The default values of configuration variables are documented in file amavisd.conf-defaults, which lists all configuration variables.

Next a configuration file amavisd.conf (or other file as specified by option -c) is read and interpreted by the Perl interpreter itself. The amavisd.conf is just a normal Perl program, and can in principle do whatever and however it pleases, but its main purpose is to assign values to configuration variables.

After execution of amavisd.conf is done, the daemon may correct some configuration variable values (mainly to maintain backwards compatibility with earlier version of configuration file), and may assign a default value to certain variables which are still undefined -- these variables and their default values are marked "after-defaults" in the documentation file amavisd.conf-defaults. The main reason for existence of the "after-defaults" concept is that some default values depend on other configuration variables and can not be computed before the amavisd.conf is finished. To force such variables to an off/false/disabled state, one needs to assign some false but defined value to them, such as '' (an empty string) or a 0 for booleans.

Perl variables always start with a character $, @ or % to indicate a type of variable. This leading character is part of the variable name for all practical purposes.

$ (dollar character)
  indicates a scalar variable (a string, a number, a reference)
@ (at sign)
  indicates an array variable (a list)
% (percent character)
  indicates an associative array (also known as hash), which maps keys to values

A couple of Perl syntactical elements deserve mention at this point, as they are often used in the amavisd.conf configuration file.

"...", a double-quoted string
  is a string; variables within are evaluated, e.g. "$MYHOME/tmp"
'...', a single-quoted string
  is a string; variables within are not evaluated, the $ and @ loose their special meaning, e.g. 'user@example.com'
(...)
  is a list of comma-separated expressions, e.g. (1,2,"test"); a list is normally assigned to an array variable
qw(string)
  is an operator that interprets its argument as a single string, splits it on whitespace to words, and returns a list of words (strings); it is a convenience to avoid some typing, e.g. qw(user@example.com .example.net .org) is exactly equivalent to ('user@example.com', '.example.net', '.org');
[...]
  is a reference to an anonymous list of comma-separated expressions, e.g. [1,2,"test"]; (note: a reference is a scalar)
{...}
  is a reference to an anonymous associative array, e.g. {'alfa'=>1, 'beta'=>99, 'other'=>'test'}; (note: a reference is a scalar)
\variable
  is a reference to a variable, e.g. \$virus_admin, \@mynetworks, \%whitelist_sender; (note: a reference is a scalar)

Historically amavisd-new accessed all configuration variables directly with their name, e.g. %spam_lovers, @spam_lovers_acl, $spam_lovers_re. Later it became apparent that certain groups of variables (lookups) are always used together in the same way, so new array variables like @spam_lovers_maps were introduced. The program now never accesses old lookup table variables directly, but always through higher level lists. The solution is fully backwards compatible, as the default value for the new lists references the old variables, e.g.:

    @spam_lovers_maps = (\%spam_lovers, \@spam_lovers_acl, \$spam_lovers_re);

Administrator is free to modify or replace the lists in variables like @spam_lovers_maps, perhaps rearranging the order or loosing all references to legacy variables, and replacing them with other variables, often anonymous arrays/lists or anonymous associative maps (hashes), or constants which can serve as a convenient catchall default value when used last in the list.

Since amavisd-new version 2.0, there is one further generalization step in the way a program accesses configuration variables. More than a hundred configuration variables which control amavisd-new operation on a by-message level (as opposed to by-recipient and truly global settings) are now grouped in associative array called a policy bank. These configuration variables are no longer accessed directly by their variable name by the program, but always through a currently installed policy bank. Administrator is free to modify the policy bank, normally by providing replacement policy banks and specifying under what conditions the replacement policy bank is to be automatically installed.

## Policy banks

Policy banks hold sets of configuration variables controlling most of per-message settings, including: static lookup tables, IP interface access rules, forwarding address, log level, templates, administrator addresses, spam trigger levels, quarantine rules, lists of anti-virus scanner entries (or just a subset), banned names rules, defang settings, etc. The whole set of these settings may be replaced with another predefined set based on incoming port number, making it possible for one amavisd daemon to cope with more diverse needs of served user communities which could so far only be implemented by running more than one instance of the amavisd daemon, each with its own configuration file.

This mechanism brings new potentials for the future: in principle policy banks could be swapped not only based on port number or SMTP client IP address, but on any characteristics pertaining to a mail message as a whole (not specific to each of its recipients), or to characteristics of a connection from a mailer (e.g. the interface address or protocol);

Until a better mechanism is available, a policy bank named 'MYNETS' has special semantics: this policy bank is loaded (if it exists) whenever MTA supplies a SMTP client's IP address (through Postfix XFORWARD extension to the SMTP protocol, or via a new AM.PDP protocol) and that address matches the @mynetworks list (actually: the list referenced by 'mynetworks_maps' key in the currently installed policy map).

An associative array %interface_policy is a current mechanism of assigning a policy bank to an incoming TCP port number (port must be in the list @$inet_socket_port, otherwise amavisd will not listen on that port). Whenever a connection from MTA is received, first a built-in policy bank with an empty name -- the $policy_bank{''} gets loaded, which brings in all the global/legacy settings. Then it is overlaid by whatever configuration settings are in the bank named in the $interface_policy{$port} if any, and finally the policy bank named 'MYNETS' (i.e. settings from $policy_bank{'MYNETS'}) is overlaid if such policy bank exists and the SMTP client IP address is known (by XFORWARD SMTP extension command from MTA) and it matches the current mynetworks_maps. See amavisd.conf-sample for examples.

When a new policy bank is overlaid over an existing set of configuration variables, the variables not present in the new policy bank retain their value. This makes it possible to specify new policy banks which carry only a minimal set of settings that need to be changed.

The built-in policy bank (with empty name) is predefined, and includes references to most other variables (the dynamic config variables), which are accessed only indirectly through the currently installed policy bank. Overlaying a policy bank with another policy bank may bring in references to entirely different variables, possibly unnamed, and may remove references to legacy variables if it so chooses.

Configuration variables are referenced from a policy bank (which is implemented as a perl associative array, i.e. a hash) by keys of the same name, e.g. { log_level => \$log_level, inet_acl => \@inet_acl, ...}. For scalars one level of indirection is allowed, e.g. a policy bank { log_level => \$log_level }; $log_level=2; is equivalent to { log_level => $log_level } or to { log_level => 2 }, but in the first example with an indirect reference, the $log_level may be assigned to even _after_ the policy bank has already been formed.

A word of caution: the syntax of entries within a policy bank hash is slightly different from assignments to configuration variables. This is because entries within policy bank are not assignments, but key=>value pairs as in any Perl associative array. And these pairs are delimited by commas, unlike statements, which are delimited by semicolons. Value is separated from its key by '=>' (or by a comma), whereas the assignment operator is '='. Keys of a policy bank are without leading $ or @ or %, unlike variable names. Values of an associative array can only be scalars (e.g. strings or numbers or references to arrays or references to associative array).

Compare:

- value of a policy bank is a reference to a Perl associative array, e.g.:

          { log_level => 3,
            forward_method => 'smtp:[127.0.0.1]:10025',
            spam_admin_maps => ["spamalert\@$mydomain"],
            blacklist_sender_maps => [ [qw(.example.org .example.net)] ],
          }

- normal assignments look like:

            $log_level = 3;
            $forward_method = 'smtp:[127.0.0.1]:10025';
            @spam_admin_maps = ("spamalert\@$mydomain");
            @blacklist_sender_maps = ( [qw(.example.org .example.net)] );

And a final note: Perl can detect and report typing mistakes in variable names, but mistyped key is just some unused associative array entry lurking in a hash, never used and never reported as mistyped/useless.

## Putting policy banks to good use -- examples

The sender address can be faked, so comparing envelope sender address to @local_domains_maps or some other lookup table to base some important decisions on would not be trustworthy. The only reliable information is the recipient's e-mail address and information about client SMTP session, such as the IP address of the sending SMTP client and the server port number or the interface address. Such information can be made available by MTA to amavisd-new through a feeding protocol (e.g. XFORWARD extension or via AM.PDP), or separate MTA paths can be set up for mail that needs to be treated differently, such as internally originating and externally originating mail, or perhaps separating authenticated mail from the rest.

Amavisd-new has two ways of receiving such extra information from MTA:

- it can listen on more than one TCP port and apply different policy banks to each port (applicable to any dual-MTA setup including Postfix), and/or
- it can accept SMTP client's IP address from MTA by XFORWARD extension to the SMTP protocol (available in Postfix only), which can control loading of policy bank MYNETS. This is in addition to loading policy banks based on TCP port number. The MYNETS policy bank is loaded (if applicable) after (on top of the) the port-assigned policy bank.

The following examples illustrate several ways of distinguishing between different mail origins. For most common purposes the only distinction that really matters is separating internally originating mail from the rest, and for this purpose the use of policy bank MYNETS and a sufficiently recent version of Postfix supporting XFORWARD suffices -- the complication with multiple ports and multiple interfaces is needed only for more demanding sites which prefer maximum flexibility.

### Example 1

As stated earlier, a policy bank named 'MYNETS' is loaded (if it exists) whenever MTA supplies an original SMTP client's IP address (e.g. via the Postfix XFORWARD extension) and that address matches the @mynetworks list. This covers most common needs to distinguish internally-originating mail from the rest, and allows them to be treated differently, as illustrated by the following example:

    $policy_bank{'MYNETS'} = {  # mail originating from @mynetworks
      virus_admin_maps => ["security\@$mydomain"], # alert of infected local hosts
      spam_admin_maps  => ["abuse\@$mydomain"],    # alert of internal spam
      spam_kill_level_maps => [7.0],  # slightly more permissive spam kill level
      spam_dsn_cutoff_level_maps => [15],
      banned_filename_maps => [
        new_RE(
        # block double extensions in names:
          qr'\.[^./]*\.(exe|vbs|pif|scr|bat|cmd|com|cpl|dll)\.?$'i,
        # allow any name or type (except viruses) within an archive:
          [ qr'^\.(Z|gz|bz2|rpm|cpio|tar|zip|rar|arc|arj|zoo)$' => 0],
        # blocks MS executable file(1) types, unless allowed above:
          qr'^\.(exe-ms)$',
        ),
      ],
    };

### Example 2

In the following example some of the external mail is coming in via fetchmail, the rest of the externally originating mail is coming in via normal SMTP at tcp port 25, and all internally originating mail is coming to MTA via mail submission port 587 reserved for that purpose, or via dedicated IP address accessible only from inside, or through a Postfix pickup service. We'll use Postfix in this example, although it does not rely on any particular Postfix capability that wouldn't be available in any general purpose MTA in some form or another.

Only the specifics of this setup are described here. Missing bits like the MTA re-entry port 10025 and other options are described in README.postfix and are assumed here. Specifying additional smtpd restrictions and options may be desired, and is omitted here for brevity.

To let amavisd-new be able to distinguish between all four mail entry routes, we let amavisd listen on four TCP ports (the fifth is for good measure, to be used in the next example): `$inet_socket_port = [10040,10041,10042,10043,10044];` (any unused non-privileged TCP ports can be used)

In Postfix configuration file master.cf we attach different content_filter options to each of the Postfix services receiving mail. We'll assume the MTA host has two IP addresses 192.0.2.1 and 192.0.2.2 assigned (IP aliases or separate physical interfaces), which makes it easier to distinguish between internally originating mail and the rest even if XFORWARD can not be used (older Postfix versions or some other MTA):

    # regular incoming mail, originating from anywhere (usually from outside)
    # the MX record (or backup mailers) should point to this IP address
    192.0.2.1:smtp inet  n  -  n  -  -  smtpd
      -o content_filter=smtp-amavis:[127.0.0.1]:10040

    # incoming mail from fetchmail, considered externally originating
    # (add 'smtphost localhost/2345' to the poll section in .fetchmailrc)
    127.0.0.1:2345 inet  n  -  n  -  -  smtpd
      -o content_filter=smtp-amavis:[127.0.0.1]:10041
      -o smtpd_client_restrictions=permit_mynetworks,reject
      -o mynetworks=127.0.0.0/8

    # IP address to be used by internal hosts for mail submission
    192.0.2.2:smtp inet  n  -  n  -  -  smtpd
      -o content_filter=smtp-amavis:[127.0.0.1]:10042
      -o smtpd_client_restrictions=permit_mynetworks,reject

    # or, tcp port 587 to be used by internal hosts for mail submission
    submission inet  n  -  n  -  -  smtpd
      -o content_filter=smtp-amavis:[127.0.0.1]:10042
      -o smtpd_client_restrictions=permit_mynetworks,reject

    # locally originating mail submitted on this host through a sendmail binary
    pickup     fifo  n  -  n  60  1  pickup
      -o content_filter=smtp-amavis:[127.0.0.1]:10043

A global option content_filter in file main.cf could provide a convenient default, only services that need a different setting would then need to override it.

Now let's make up names for policy banks which will cover all four cases. We'll pick names EXT, EXT-FM, INT, INT-HOST for policy banks. The amavisd needs to be told to load corresponding policy when a request comes in on each of the listening ports:

      $interface_policy{'10040'} = 'EXT';
      $interface_policy{'10041'} = 'EXT-FM';
      $interface_policy{'10042'} = 'INT';
      $interface_policy{'10043'} = 'INT-HOST';
      $interface_policy{'10044'} = 'AUTH';  # to be used in the next example

Next we'll prepare each policy and specify there the options which should be different from global options. Note that the following policies serve mostly as an example and to provide ideas -- they should not be considered a recommendation. For example:

    # regular incoming mail, originating from anywhere (usually from outside)
    $policy_bank{'EXT'} = {
      # just use global settings, no special overrides
    };

    # incoming mail from fetchmail, considered externally originating
    $policy_bank{'EXT-FM'} = {
      log_level => 2,
        # no bounces for spam, not even for score below spam_dsn_cutoff_level_maps:
      final_spam_destiny => D_DISCARD,
    };

    # locally originating mail guaranteed to be from inside
    $policy_bank{'INT'} = {
        # enable/redirect admin notifications for locally originating malware:
      virus_admin_maps => ["virusalert\@$mydomain"],
      spam_admin_maps  => ["virusalert\@$mydomain"],
        # be slightly more permissive on spam levels for mail from our hosts:
      spam_kill_level_maps => [7.0],
      spam_dsn_cutoff_level_maps => [15],
      final_virus_destiny => D_BOUNCE,  # (unless in viruses_that_fake_sender_maps)
      final_spam_destiny  => D_BOUNCE,  # (unless above spam_dsn_cutoff_level_maps)
      bypass_banned_checks_maps => [ 1 ],  # allow sending any file type or name
        # provide customized sender notifications for spam from our users:
      notify_spam_sender_templ => read_text("$MYHOME/notify_spam_sender.txt"),
    };

    # mail locally submitted on the host on which MTA runs
    $policy_bank{'INT-HOST'} = {
        # NOTE: this is just an example; ignoring internally generated spam
        # may not be such a good idea, consider zombified infected local PCs
      bypass_spam_checks_maps   => [ 1 ],
      bypass_banned_checks_maps => [ 1 ],
      final_spam_destiny   => D_PASS,
      final_banned_destiny => D_PASS,
    };

    # authenticated mail (used by the next example)
    $policy_bank{'AUTH'} = {
        # enable admin notifications for malware originating from our users:
      virus_admin_maps => ["virusalert\@$mydomain"],
      spam_admin_maps  => ["virusalert\@$mydomain"],
        # be slightly more permissive on spam levels for mail from our users:
      spam_kill_level_maps => 7.0,
      spam_dsn_cutoff_level_maps => 15,
      bypass_banned_checks_maps => 1,  # allow sending any file type or name
      final_bad_header_destiny => D_BOUNCE;  # block invalid headers
    };

If not all four cases need to be distinguished, the same policy bank name (or none at all) can be assigned to more than one port. Also the MTA configuration can use the same amavisd port for more than one of its incoming services if there is no need for different settings.

### Example 3

Besides setting different content_filter options for different Postfix services, one may use the option FILTER in Postfix lookup tables, as described in Postfix man pages access(5) and header_checks(5), to specify different content_filter settings based on various conditions, such as sender domain name or IP address, mail header fields, etc.

Consider the next example which uses the FILTER settings to distinguish from internally originating, authenticated external mail and the rest.

    # global default:
    content_filter=smtp-amavis:[127.0.0.1]:10044

    smtpd_recipient_restrictions =
      reject_unauth_pipelining, reject_non_fqdn_recipient, reject_non_fqdn_sender,
      reject_unknown_recipient_domain, reject_unknown_sender_domain,
      check_client_access cidr:/etc/postfix/filter-mynets.cidr,
      permit_sasl_authenticated, permit_tls_clientcerts,
      reject_unauth_destination,
      check_sender_access regexp:/etc/postfix/filter-catchall.regexp

The `check_client_access cidr:/etc/postfix/filter-mynets.cidr` takes the place of the more usual permit_mynetworks, and it serves to be able to override the global content_filter setting by the use of FILTER for each of the networks (presumably internal) listed in filter-mynets.cidr. The final effect is that mail matching networks listed in filter-mynets.cidr will be sent for content filtering to tcp port 10042 (the FILTER setting in access map), authenticated non-local mail will be sent for content filtering to port 10044 (the global setting), while all the rest will be sent to port 10040 (as specified in catchall filter). If there are any other overrides in master.cf like in the previous example, they take precedence over the global settings, but the FILTER rules take the ultimate precedence.

/etc/postfix/filter-mynets.cidr :

    127.0.0.0/8    FILTER smtp-amavis:[127.0.0.1]:10042
    10.0.0.0/8     FILTER smtp-amavis:[127.0.0.1]:10042
    172.16.0.0/12  FILTER smtp-amavis:[127.0.0.1]:10042
    192.168.0.0/16 FILTER smtp-amavis:[127.0.0.1]:10042

/etc/postfix/filter-catchall.regexp:

    /^/            FILTER smtp-amavis:[127.0.0.1]:10040

Note that in place of the last catchall entry: `check_sender_access regexp:/etc/postfix/filter-catchall.regexp` one would be tempted to do: `check_sender_access static:FILTER smtp-amavis:[127.0.0.1]:10040`, but unfortunately spaces are not allowed within an option value in master.cf, so we have to resort to a lookup table.

## $max_requests

Amavisd-new runs under process control of Net::Server. This is a pre-forked environment where $max_servers child processes are constantly kept alive and ready to accept new tasks (mail messages to be checked). Each amavisd child process is able to handle several tasks in a row, which helps to reduce startup (fork) costs. In case of SMTP or LMTP protocol, each session may consist of several SMTP/LMTP transactions. Each SMTP/LMTP transaction is counted a one task, regardless of whether it came in from the same SMTP/LMTP client in a multi-transaction session, or as separate sessions, possibly from different SMTP/LMTP clients.

A configuration variable $max_requests (default value 10) controls the approximate number of tasks each child process is willing to handle. After that the child process terminates and Net::Server provides a new child process to take its place.

The exact value of $max_requests is not critical. There are two opposing needs, and some in-between value should be chosen.

On the low side, the number should not be too small in order for the startup cost to be averaged out / sufficiently diluted over an entire child lifetime. A value above 5 or 10 meets this goal in most amavisd-new configurations.

On the high side, the value depends on the amavisd-new configuration. The amavisd daemon itself is conservative in its use of dynamically allocated memory and does not load mail into memory, but keeps mail being processed and its components on files. Similarly, most of the called external virus scanners and decoders are rational in their use of memory (a notable exception is Archive::Tar which is used if the pax or cpio command is not available). Unfortunately this is not true for Perl module Mail::SpamAssassin, which expect to have an entire decoded mail in memory in order to be able to run its large set of rules on it in reasonable time. This is a design decision of SpamAssassin.

When amavisd-new is not configured to use SpamAssassin, the value of $max_requests can be quite high without any known or expected problems. For general sanity reasons, an upper limit could be a 100 for example, although anything above 20 or so would not bring measurable benefit to the maximum sustained mail throughput.

When amavisd-new is configured to use SpamAssassin however, the slurping of entire mail in memory and decoding it may have implications, depending on the $sa_mail_body_size_limit value, on the maximum mail size allowed at the MTA (e.g. Postfix setting for message_size_limit) and on the mail compression factor. Even though the allocated memory is reclaimed by Perl after mail processing, and is reused for subsequent processing, the process virtual memory footprint never shrinks, it can only expand as needed.

With a default value of message_size_limit near 10 MB this is not a serious problem, and $max_requests can be fairly large, although since the additional performance gain is negligible for values beyond 20 or so, there is no good reason to choose much larger value than that.

Some sites however chose not to limit mail size, or increase the maximum mail size limit substantially. If a large mail arrives at such site, the virtual memory of amavisd child process is extended to accommodate the message. For the rest of its lifetime the child process that processed the mail stays at its high virtual memory size. If this happens frequently, host resources may become scarce. Limiting the number of tasks each child is supposed to process is very much desirable on such systems.

The default value of 10 for $max_servers was chosen as a reasonable compromise between averaging-out the startup costs and not wasting too much resources on hosts with high message size limit and SpamAssassin enabled.

In the setup with Postfix where its lmtp client is chosen to feed amavisd-new, this client tries to keep LMTP session open and submit several mail messages in multiple transactions. With recent Postfix versions its SMTP client is capable and willing of using multiple transaction sessions as well, although it seems to be less persistent than the LMTP client.

According to SMTP and LMTP protocol specifications, dropping the session on the server side is considered rude and should be used only as a last resort. In order to respect the $max_requests setting (which is not strictly enforced by amavisd, and is considered an advisory value), the client side should preferably be configured with a comparable limit. Starting amavisd-new-2.2.0 the amavisd daemon is more strict in enforcing the limit and drops the SMTP or LMTP session after $max_servers is exceeded by one. This was a recommendation from the Postfix community, as the option of reducing Postfix max_use setting is considered less appropriate.

Nevertheless, Postfix doesn't take session dropping lightly, it backs off a while after content filter forcibly drops the session, which is undesired. Better behaviour is achieved when Postfix voluntarily terminates a SMTP session before amavisd would reach its $max_requests limit. This can be achieved by applying max_use to the Postfix smtp service feeding a content filter (typically this entry in master.cf is named 'smtp-amavis').

## Setting up DKIM and DomainKeys mail signing and verification

The goals of DKIM and DomainKeys are:

- assurance of sender identities
- protection against message tampering.

A DKIM draft states the following, which applies to its predecessor DomainKeys as well:

> DomainKeys Identified Mail (DKIM) defines a mechanism by which email messages can be cryptographically signed, permitting a signing domain to claim responsibility for the introduction of a message into the mail stream. Message recipients can verify the signature by querying the signer's domain directly to retrieve the appropriate public key, and thereby confirm that the message was attested to by a party in possession of the private key for the signing domain.

A gentle introduction and deployment guide is available at: http://antispam.yahoo.com/domainkeys. Except for some minor details, it applies to DKIM system as well.

With added support in Postfix 2.3 for a milter protocol, it became possible to use with Postfix many of existing milters (mail filters) that were originally developed with sendmail in mind. It was hoped that a widespread use of milters with sendmail offered a fertile ground for software development, producing software of sufficient quality to be able to use it with Postfix. It remains to be seen whether quality of freely available milters comes anywhere close to high standards we are accustomed to with Postfix, but with a bit of luck and reasonable expectations, some of it can be put to good use.

Two of such milters are dkim-milter offering support for DomainKeys Identified Mail (DKIM) Signatures, and dk-milter, offering support for Domain-based Email Authentication (DomainKeys). The DomainKeys (DK) is a predecessor of DKIM, as recognized by draft-delany-domainkeys-base-06:

> The DomainKeys specification was a primary source from which the DomainKeys Identified Mail [DKIM] specification has been derived. The purpose in submitting this document is as an historical reference for deployed implementations written prior to the DKIM specification.

At the time of this writing it appears the dkim-milter is more reliable and better maintained than dk-milter, which is slowly fading into oblivion. Similar holds true in the world of Perl modules: there are modules Mail::DomainKeys and Mail::DKIM, both of which can be used by SpamAssassin. Again the Mail::DKIM (by Jason Long and Anthony D. Urso) seems to be of higher quality than the older Mail::DomainKeys. SpamAssassin makes it very easy to use each or both of them (for verification only), just by enabling the already provided plugins.

Despite DomainKeys slowly giving grounds to DKIM, the DomainKeys is currently still in use by several large players in the Internet world, so this section will describe how to integrate both of them with Postfix and amavisd-new (an after-queue content filter) into a mail system.

Mail signing and verification is a two-part job: signing of originating mail (or mail being redistributed) from our domain, and verifying signatures of incoming mail. Both tasks can be done by the same program, or they can be performed by separate entities. Traditionally with sendmail, both tasks are performed by the same milter, which may be easier to maintain, but has certain disadvantages.

Verifying signatures should be performed early, before any local mail transformations get a chance of invalidating signature, e.g. by performing MIME conversions to quote-printable, by fixing syntactically invalid mail header, by editing/inserting/removing certain header fields, or by a local mailing list modifying mail text, e.g. by appending footnotes.

Signing outgoing mail should be performed late, after mail sanitation, after conversion to 7-bit characters (to avoid later uncontrollable changes by a relaying or receiving MTA), and after adding header fields by a content filter. Similar applies to local mailing lists, which may be rewriting messages, requiring them to be re-signed by the domain hosting a mailing list, just before being sent out.

Since SpamAssassin only provides signature verification but not signing, one obvious choice for signing is to use dkim-milter and dk-milter in signing-only mode, invoked by a Postfix smtpd service which is receiving content-checked mail from a content filter such as amavisd-new. As this second-stage smtpd service does not reliably know how a given message came into a mail system and whether it is supposed to be signed or not, a clean solution is to provide two parallel paths through a content filter, one used for mail to be signed (originating mail), the other for all the rest:

                  +------+
                  |verify|            (verify)
                  +--+---+          SpamAssassin
                    ^^^                  ^v
    incoming:       |||             +----++-----+
      MX ---->  25 smtpd ---> 10024 >           >---> 10025 smtpd -->
    submission:      ||             |           |
      SASL -->  25 smtpd \          |  amavisd  | (notifications)
                      |   +->       |           >--->_
      mynets->  25 smtpd ---> 10026 >ORIGINATING>---> 10027 smtpd -->
                          +->       +-----------+            |
           --> 587 smtpd /  :                                v
                           (convert to 7-bit)              +----+
                                                           |sign|
                                                           +----+

There are other benefits to providing two parallal paths: a content filter may be configured to apply different rules and settings to mail that is known to be originating from our users, compared to the rest. Some suggestions: apply less strict banning rules, enable spam administrator notifications for internally originating spam and viruses, letting SpamAssassin rules be conditionalized based on amavisd-new policy banks loaded, etc.

For verification there are two choices: either a SpamAssassin plugin can do it by calling Perl modules, or a milter in verification-only mode can be invoked by the incoming Postfix smtpd service. In the setup described, SpamAssassin sees almost the same message as a milter on the incoming smtpd would (just few header fields including a Received are prepended, no other changes), so there is no concern that a signature would become invalid.

Invoking signature verification by SpamAssassin has an advantage that DKIM-based or DomainKeys-based whitelisting or scoring can be used, but has a disadvantage that possibly not all mail is checked (e.g. large mail may be exempt from spam checks).

On the other hand, invoking signature verification by calling a milter from incoming smtpd service can insert a header field reporting a result of verification, which may be used by MUA or by SpamAssassin rules, although currently no such rules are provided ready-made.

For the purpose of gathering experience, both methods for signature verification are described. It turns out that one or the other implementation (in C or in Perl) may be buggy, and being able to compare both results of signature verification can help with troubleshooting.

Let's begin by starting both milters, each in two instances, one dedicated for signing, the other for verification. For security reasons all milters should be run under a dedicated username, certainly not as root, not as user amavis and not as user postfix:

dk, verifying:

      dk-filter   -u dkfilter -b v -H \
        -l -p inet:4442@127.0.0.1 -P /var/run/dk-filter-v.pid

dkim, verifying:

      dkim-filter -u dkfilter -b v \
        -l -p inet:4443@127.0.0.1 -P /var/run/dkim-filter-v.pid

dk, signing:

      dk-filter   -u dkfilter -b s -m ORIGINATING \
        -H -c nofws \
        -d example.com -S myselector -s /var/db/dkim/mykey.pem \
        -l -p inet:4444@127.0.0.1 -P /var/run/dk-filter-s.pid

dkim, signing:

      dkim-filter -u dkfilter -b s -m ORIGINATING \
        -c relaxed/simple -S rsa-sha1 \
        -d example.com -s myselector -k /var/db/dkim/mykey.pem \
        -l -p inet:4445@127.0.0.1 -P /var/run/dkim-filter-s.pid

Note that both signing milters may (but need not) use the same signing key and the same selector. Generating a pair of public and private key and publishing a public key and a policy in DNS is described in the documentation of each milter and also in DKIM and DomainKeys drafts.

We are not specifying option -i to milters, the default of -i 127.0.0.1 suits our setup just fine, as mail to be signed is coming from a content filter, usually on a loopback interface from IP address 127.0.0.1.

Now we can tie both verifying milters to a Postfix smtpd service listening for incoming mail:

master.cf:

      smtp inet n - n - 300 smtpd
        -o milter_default_action=accept
        -o milter_macro_daemon_name=MTA
        -o smtpd_milters=inet:127.0.0.1:4442,inet:127.0.0.1:4443

and tie both signing milters to a Postfix smtpd service that is receiving checked mail from amavisd, intended to be signed:

master.cf:

      # mail return from a content filter (non-signing)
      10025 inet n - n - - smtpd
        -o content_filter=
        ... (other options, mail not to be signed) ...

      # mail from our users returning from a content filter (DKIM or DK signing)
      10027 inet n - n - - smtpd
        -o content_filter=
        ... (other options, mail to be signed) ...
        -o milter_default_action=accept
        -o milter_macro_daemon_name=ORIGINATING
        -o smtpd_milters=inet:127.0.0.1:4444,inet:127.0.0.1:4445

Note the order for signing milters: dk-milter is invoked first, and dkim-milter second, so that DK signature will be protected by a later DKIM signature. The reason is that DomainKeys specification does not protect its own signature in its signed data, but the newer DKIM fixes this omission and does protect its own signature.

As a sidenote, attaching milters to sendmail would use the same order of invocations: signature verifying milters first, content filters next, and signing milters last, for example:

      dnl Verifiers:
      INPUT_MAIL_FILTER(`dk-filter-v',   `S=inet:4442@127.0.0.1, T=R:2m')
      INPUT_MAIL_FILTER(`dkim-filter-v', `S=inet:4443@127.0.0.1, T=R:2m')

      dnl Content filter:
      INPUT_MAIL_FILTER(`amavisd-milter',
        `S=unix:/var/amavis/amavisd-milter.sock, F=T, T=S:10m;R:10m;E:10m')

      dnl Signers:
      INPUT_MAIL_FILTER(`dk-filter-s',   `S=inet:4444@127.0.0.1, T=R:2m')
      INPUT_MAIL_FILTER(`dkim-filter-s', `S=inet:4445@127.0.0.1, T=R:2m')

Now back to Postfix setup (or any dual-MTA setup). Default content filter is to be amavisd, listening on port 10026 (intended for signing). Locally submited or SASL-authenticated mail will go to a content filter on this default port 10026 and will be signed on its way out. All other mail (incoming) will be diverted to port 10024 for content filtering by a final catchall FILTER, and will never hit the signing milter:

main.cf:

      content_filter = smtp-amavis:[127.0.0.1]:10026

      smtpd_recipient_restrictions =
        ...
        permit_mynetworks
        permit_sasl_authenticated
        reject_unauth_destination
        ...
        check_sender_access regexp:/etc/postfix/filter_10024_catchall
        permit

/etc/postfix/filter_10024_catchall:

      /^/  FILTER smtp-amavis:[127.0.0.1]:10024

In SpamAssassin all that is necessary is to add (or uncomment) lines in any of the .pre files (e.g. in local.pre, or in init.pre and v312.pre):

      loadplugin Mail::SpamAssassin::Plugin::DomainKeys
      loadplugin Mail::SpamAssassin::Plugin::DKIM

Perl modules Mail::DomainKeys (version 0.88 or better) and Mail::DKIM (0.19 or better) need to be installed. Note that Mail::DKIM starting with version 0.20 also recognizes DomainKeys signatures, so that Plugin::DomainKeys is not needed any longer. There is a small bug in 0.20 canonicalization of DK messages, so it is advisable to skip 0.20 and go for 0.21 or 0.22 or later.

The following SpamAssassin rules (in local.cf) work fairly well, giving verified mail a little bit of advantage and slightly favourize verified mail from some popular domains, encouraging people to start signing their mail. Possible signed spam can be counterbalanced by other measures.

      score DK_VERIFIED -1.5
      score DK_POLICY_SIGNSOME 0
      score DK_POLICY_TESTING  0

      score DKIM_VERIFIED -1.5
      score DKIM_POLICY_TESTING 0

      # DKIM and DK-based whitelisting may be used reliably:
      score USER_IN_DKIM_WHITELIST -3.0
      score USER_IN_DK_WHITELIST   -3.0
      whitelist_from_dkim *@friends.example.com
      whitelist_from_dk   *@friends.example.com

Another suggestions - penalize mail claiming to be from Yahoo or Gmail but was not signed by their official mailers:

      header __L_ML1       Precedence =~ m{\b(list|bulk)\b}i
      header __L_ML2       exists:List-Id
      header __L_ML3       exists:List-Post
      header __L_ML4       exists:Mailing-List
      header __L_HAS_SNDR  exists:Sender
      meta   __L_VIA_ML    __L_ML1 || __L_ML2 || __L_ML3 || __L_ML4 || __L_HAS_SNDR
      header __L_FROM_Y1   From:addr =~ m{[@.]yahoo\.com$}i
      header __L_FROM_Y2   From:addr =~ m{\@yahoo\.com\.(ar|br|cn|hk|my|sg)$}i
      header __L_FROM_Y3   From:addr =~ m{\@yahoo\.co\.(id|in|jp|nz|uk)$}i
      header __L_FROM_Y4   From:addr =~ m{\@yahoo\.(ca|de|dk|es|fr|gr|ie|it|pl|se)$}i
      meta   __L_FROM_YAHOO __L_FROM_Y1 || __L_FROM_Y2 || __L_FROM_Y3 || __L_FROM_Y4
      header __L_FROM_GMAIL From:addr =~ m{\@gmail\.com$}i
      meta     L_UNVERIFIED_YAHOO  !DK_VERIFIED && __L_FROM_YAHOO && !__L_VIA_ML
      priority L_UNVERIFIED_YAHOO  500
      score    L_UNVERIFIED_YAHOO  2.5
      meta     L_UNVERIFIED_GMAIL  !DK_VERIFIED && __L_FROM_GMAIL && !__L_VIA_ML
      priority L_UNVERIFIED_GMAIL  500
      score    L_UNVERIFIED_GMAIL  2.5

In amavisd.conf two parallel paths need to be provided, one receiving on port 10024 and forwarding to 10025, the other receiving on port 10026 and forwarding to 10027.

      $inet_socket_port = [10024,10026];  # listen on two ports

The 10024/10025 path will be controlled by a default policy bank, the other, dedicated to mail needing to be signed, will use a policy bank (arbitrarily) named ORIGINATING:

      $forward_method = 'smtp:[127.0.0.1]:10025';  # MTA with non-signing service
      $notify_method  = 'smtp:[127.0.0.1]:10027';  # MTA with signing service

      # switch policy bank to 'ORIGINATING' for mail received on port 10026:
      $interface_policy{'10026'} = 'ORIGINATING';

      $policy_bank{'ORIGINATING'} = {  # mail originating from our users
        # force MTA to convert mail to 7-bit before DKIM signing
        # to avoid later conversions which could destroy signature:
        smtpd_discard_ehlo_keywords => ['8BITMIME'],
        #
        # forward to a smtpd service providing DKIM/DomainKeys signing service:
        forward_method => 'smtp:[127.0.0.1]:10027',
        #
        # other special treatment of locally originating mail, e.g.:
        spam_admin_maps  => ["spamalert\@$mydomain"],  # warn of spam from us
        virus_admin_maps => ["virusalert\@$mydomain"],
        banned_filename_maps => ['ALT-RULES'],         # more relaxed rules
        spam_quarantine_cutoff_level_maps => undef,    # quarantine all spam
        spam_dsn_cutoff_level_maps => undef,
        spam_dsn_cutoff_level_bysender_maps => # bounce to local senders only
          [ { lc(".$mydomain") => undef,  '.' => 15 } ],
      };

The `smtpd_discard_ehlo_keywords=>['8BITMIME']` serves to persuade Postfix to convert mail to 7-bit quoted-printable before submitting it to content filtering and signing (this configuration variable was introduced with version 2.4.3 of amavisd-new). Avoiding 8-bit characters in mail body makes signatures less susceptible to breaking by some relaying or receiving MTA over which we have no control. Note that the same effect (making Postfix convert outgoing mail to 7-bits before DKIM signing) could be achieved by a Postfix setting `smtp_discard_ehlo_keywords=8bitmime` on a smtp service feeding mail to be signed to amavisd, but this would require setting up two such services, one with the option and one without.

Note that 8-bit to 7-bit conversion may break SMIME or PGP signature, so if mail signing is in use, it may not be desirable to let Postfix do the conversion, and it may be acceptable to take a risk that a remote MTA will clobber signatures if it decides to convert mail text to 7-bits.

The following text from the Postfix documentation file MILTER_README should be disregarded -- amavisd is 8-bit clean, and we want Postfix to do conversion to 7-bits on the signing path but not on the other path: Content filters may break domain key etc. signatures. If you use an SMTP-based content filter, then you should add a line to master.cf with "-o disable_mime_output_conversion=yes", as described in the advanced content filter example.

While testing how the configured system plays with some mailing lists (such as postfix-users or SpamAssassin users list), one has to keep in mind that amavisd-new caches spam checking results of recently seen message bodies: a mail going out to a mailing list is not yet signed as it reaches a content filter, but the SpamAssassin verdict is remembered at that point (claiming the message is not signed). When this message with unchanged body comes back from a mailing list, this time signed in the header by our domain, the signature should prove correct, yet the cached result from a minute ago still claims the message is not signed. If this is of concern, one can turn off caching of spam checking results for ham by setting: `$spam_check_negative_ttl = 0;`

### Some experience with DKIM and DomainKeys

Recent versions of software components must be used to avoid bugs and interoperability problems with earlier versions:

- use Postfix 2.3.3 or later (fixing minor 2.3 problems with milter);
- amavisd-new 2.4.3 polished some corner issues on modifying mail header when releasing from a quarantine and defanging, and added some goodies affecting DKIM and DomainKeys to facilitate integration;
- Mail::DomainKeys 0.88 or later must be used, the last couple of versions squashed several bugs, one at a time. Version 0.84 dropped the use of unreliable Email::Address (which could cause deep recursion in evaluating regular expressions, bringing processing to a halt). Nevertheless, one patch is still needed ( http://www.ijs.si/software/amavisd/Mail-DomainKeys-0.88-mark.patch ), hopefully to be included with the next version;
- Mail::DKIM 0.19 is very solid, it is the only component where no bugs or design flaws were found during experimenting; in fact, this module helped to discover bugs in other components;
- SpamAssassin 3.1.5 added whitelisting based on verified DomainKeys signatures, similar to what was already available for DKIM whitelisting in earlier versions of SA;
- dk-milter 0.4.1 needs a patch as described in the Postfix documentation file MILTER_README;
- the dk-milter 0.4.1 seems to be neglected lately and is rather buggy compared to dkim-milter. Nevertheless, with command options as given in the above example, it does its job sufficiently well in a described Postfix + amavisd-new setup, so that it may be deployed for a production use; see its bug tracking system for details;
- dkim-milter 0.5.2 is already compatible with Postfix, no patch is required; the 0.5.2 also adds a missing body hash check (omitted in 0.5.1 and earlier versions) and fixes "relaxed" body canonicalization algorithm; because the other end may still be running 0.5.1 or older, it is best to avoid "relaxed" body canonicalization, so choose -c relaxed/simple.

Instead of signing mail with dkim-milter, the same can be achieved by using Jason Long's DKIM Proxy, which is a Perl program calling a Perl module Mail::DKIM, i.e. the same modules as used by a SpamAssassin DKIM plugin. As the Mail::DKIM turned out to be a reliable and quite efficient module, this may be a good alternative to dkim-milter (which is also quite good).

On the other hand there exist a dkfilter SMTP-proxy by the same author, which calls a Perl module Mail::DomainKays, which in turn is not recommended for processing of entire messages because of its design limitation which requires loading the whole message into memory. The use of Mail::DomainKays from within SpamAssassin does not represent such a problem, as messages checked by SpamAssassin are already limited in size.

Under FreeBSD just install both milters (mail/dkim-milter and mail/dk-milter) from freshly updated ports, and add the following to `/etc/rc.conf`:

      milterdk_enable="YES"
      milterdk_uid='dkimfilter'
      milterdk_profiles="verifier signer"
      milterdk_verifier_socket='inet:4442@127.0.0.1'
      milterdk_verifier_flags='-b v -H'
      milterdk_signer_socket='inet:4444@127.0.0.1'
      milterdk_signer_flags='-b s -H -c nofws -m ORIGINATING
        -d example.org -S mysel -s /var/db/domainkeys/mysel.key.pem'

      milterdkim_enable="YES"
      milterdkim_uid='dkimfilter'
      milterdkim_profiles="verifier signer"
      milterdkim_verifier_socket='inet:4443@127.0.0.1'
      milterdkim_verifier_flags='-b v'
      milterdkim_signer_socket='inet:4445@127.0.0.1'
      milterdkim_signer_flags='-b s -c relaxed/simple -m ORIGINATING
        -d example.org -s mysel -k /var/db/domainkeys/mysel.key.pem -S rsa-sha1'

Recent startup scripts supplied by both FreeBSD ports already ensure that both deamons run under their dedicated usernames, supplying a default value for option -u. The mail/dk-milter port also already includes a patch to make it compatible with Postfix; (the mail/dkim-milter doesn't need such patch, 0.5.2 is already compatible with Postfix).

Mail transformations as performed by some mailing lists are probably the most challenging problem facing DKIM and DomainKeys deployment (and to other schemes as well). Nevertheless, mailing lists can be configured to either avoid transformations which invalidate mail signatures, or can re-sign fan-out mail. Examples of mailing lists which work very well with DKIM and DomainKeys, preserving existing signatures provided by posters, are the postfix-users ( postfix-users@postfix.org ) and the SpamAssassin users list ( users@spamassassin.apache.org ). Example of re-signing mailing lists are Yahoo groups. A representative of another type of mailing lists is currently Mailman, which modifies mail body, but at least it is stripping original signatures and adding a Sender header, so that original signatures do not appear to be broken, they are just missing, and mail may be re-signed - not too good, but could be worse.

Several big players are already signing mail from their users or employees: Yahoo! (worldwide), Gmail, eBay, Earthlink, google.com, Cisco, etc. (not to mention porcupine.org :)

When signatures are missing on mail from domains which are known to be signing all their mail (yahoo.com, gmail.com), the most common reason is that a sender submitted his mail through some other provider, but supplied his Yahoo or gmail e-mail address in the From header field. Similar to other schemes designed to prevent faking of sending address, the DKIM (and the DomainKeys) encourages mail submission only through a domain which is used in the From address - although there are other possibilities for roving users, especially in the DKIM system. People will need to become aware that their best choice is to submit mail through their native domain to prevent their messages being treated as second-class or appear suspicious.

Note that some spam is also being signed by DomainKeys or DKIM lately, which is a good thing -- it indicates the sender owns (or ownz) a domain they are sending mail from. This either shows sender's sincere interest of not hiding behind a faked sender mail address (in which case such mail can be easily filtered if desired), or they are using a short-lived temporary domain (perhaps through domain kiting), which can be counteracted by black lists of few-days old freshly registered domains. Adding a small negative spam score to successfully verified mail will encourage people to start signing their mail, benefiting legitimate senders and recipients, while signed spam can be counterbalanced by other measures. Signing and verifying mail is a good mechanism for companies to reliably whitelist mail from their partner companies or frequent clients.

Some references:

- Postfix before-queue Milter support (original Postfix documentation)
- DomainKeys: Proving and Protecting Email Sender Identity (original DK Yahoo! document)
- IETF charter: Domain Keys Identified Mail (DKIM)
- DKIM Deployment Reports (at Mutual Internet Practices Association - MIPA)

mm
Last updated: 2007-01-29