33. SMTP authentication
Author: The Exim Project. <https://www.exim.org/>
Description: Exim is a message transfer agent (MTA) developed at the University of Cambridge for use on Unix systems connected to the Internet.
Keywords: exim,smtp,mta,email

# Exim Internet Mailer

<-previous next->

## Chapter 33 - SMTP authentication

The “authenticators” section of Exim’s runtime configuration is concerned with SMTP authentication. This facility is an extension to the SMTP protocol, described in RFC 2554, which allows a client SMTP host to authenticate itself to a server. This is a common way for a server to recognize clients that are permitted to use it as a relay. SMTP authentication is not of relevance to the transfer of mail between servers that have no managerial connection with each other.

Very briefly, the way SMTP authentication works is as follows:

- The server advertises a number of authentication mechanisms in response to the client’s EHLO command.

- The client issues an AUTH command, naming a specific mechanism. The command may, optionally, contain some authentication data.

- The server may issue one or more challenges, to which the client must send appropriate responses. In simple authentication mechanisms, the challenges are just prompts for user names and passwords. The server does not have to issue any challenges – in some mechanisms the relevant data may all be transmitted with the AUTH command.

- The server either accepts or denies authentication.

- If authentication succeeds, the client may optionally make use of the AUTH option on the MAIL command to pass an authenticated sender in subsequent mail transactions. Authentication lasts for the remainder of the SMTP connection.

- If authentication fails, the client may give up, or it may try a different authentication mechanism, or it may try transferring mail over the unauthenticated connection.

If you are setting up a client, and want to know which authentication mechanisms the server supports, you can use Telnet to connect to port 25 (the SMTP port) on the server, and issue an EHLO command. The response to this includes the list of supported mechanisms. For example:

    $ telnet server.example 25
    Trying 192.168.34.25...
    Connected to server.example.
    Escape character is '^]'.
    220 server.example ESMTP Exim 4.20 ...
    ehlo client.example
    250-server.example Hello client.example [10.8.4.5]
    250-SIZE 52428800
    250-PIPELINING
    250-AUTH PLAIN
    250 HELP

The second-last line of this example output shows that the server supports authentication using the PLAIN mechanism. In Exim, the different authentication mechanisms are configured by specifying authenticator drivers. Like the routers and transports, which authenticators are included in the binary is controlled by build-time definitions. The following are currently available, included by setting

    AUTH_CRAM_MD5=yes
    AUTH_CYRUS_SASL=yes
    AUTH_DOVECOT=yes
    AUTH_EXTERNAL=yes
    AUTH_GSASL=yes
    AUTH_HEIMDAL_GSSAPI=yes
    AUTH_PLAINTEXT=yes
    AUTH_SPA=yes
    AUTH_TLS=yes

in Local/Makefile, respectively. The first of these supports the CRAM-MD5 authentication mechanism (RFC 2195), and the second provides an interface to the Cyrus SASL authentication library. The third is an interface to Dovecot’s authentication system, delegating the work via a socket interface. The fourth provides for negotiation of authentication done via non-SMTP means, as defined by RFC 4422 Appendix A. The fifth provides an interface to the GNU SASL authentication library, which provides mechanisms but typically not data sources. The sixth provides direct access to Heimdal GSSAPI, geared for Kerberos, but supporting setting a server keytab. The seventh can be configured to support the PLAIN authentication mechanism (RFC 2595) or the LOGIN mechanism, which is not formally documented, but used by several MUAs. The eighth authenticator supports Microsoft’s Secure Password Authentication mechanism. The last is an Exim authenticator but not an SMTP one; instead it can use information from a TLS negotiation.

The authenticators are configured using the same syntax as other drivers (see section 6.23). If no authenticators are required, no authentication section need be present in the configuration file. Each authenticator can in principle have both server and client functions. When Exim is receiving SMTP mail, it is acting as a server; when it is sending out messages over SMTP, it is acting as a client. Authenticator configuration options are provided for use in both these circumstances.

To make it clear which options apply to which situation, the prefixes server_ and client_ are used on option names that are specific to either the server or the client function, respectively. Server and client functions are disabled if none of their options are set. If an authenticator is to be used for both server and client functions, a single definition, using both sets of options, is required. For example:

    cram:
      driver = cram_md5
      public_name = CRAM-MD5
      server_secret = ${if eq{$auth1}{ph10}{secret1}fail}
      client_name = ph10
      client_secret = secret2

The server_ option is used when Exim is acting as a server, and the client_ options when it is acting as a client.

Descriptions of the individual authenticators are given in subsequent chapters. The remainder of this chapter covers the generic options for the authenticators, followed by general discussion of the way authentication works in Exim.

Beware: the meaning of $auth1, $auth2, ... varies on a per-driver and per-mechanism basis. Please read carefully to determine which variables hold account labels such as usercodes and which hold passwords or other authenticating data.

Note that some mechanisms support two different identifiers for accounts: the authentication id and the authorization id. The contractions authn and authz are commonly encountered. The American spelling is standard here. Conceptually, authentication data such as passwords are tied to the identifier used to authenticate; servers may have rules to permit one user to act as a second user, so that after login the session is treated as though that second user had logged in. That second user is the authorization id. A robust configuration might confirm that the authz field is empty or matches the authn field. Often this is just ignored. The authn can be considered as verified data, the authz as an unverified request which the server might choose to honour.

A realm is a text string, typically a domain name, presented by a server to a client to help it select an account and credentials to use. In some mechanisms, the client and server provably agree on the realm, but clients typically can not treat the realm as secure data to be blindly trusted.

### 1. Generic options for authenticators

client_condition | Use: authenticators | Type: string† | Default: unset

When Exim is authenticating as a client, it skips any authenticator whose client_condition expansion yields “0”, “no”, or “false”. This can be used, for example, to skip plain text authenticators when the connection is not encrypted by a setting such as:

    client_condition = ${if !eq{$tls_out_cipher}{}}

client_set_id | Use: authenticators | Type: string† | Default: unset

When client authentication succeeds, this condition is expanded; the result is used in the log lines for outbound messages. Typically it will be the user name used for authentication.

driver | Use: authenticators | Type: string | Default: unset

This option must always be set. It specifies which of the available authenticators is to be used.

public_name | Use: authenticators | Type: string | Default: unset

This option specifies the name of the authentication mechanism that the driver implements, and by which it is known to the outside world. These names should contain only upper case letters, digits, underscores, and hyphens (RFC 2222), but Exim in fact matches them caselessly. If public_name is not set, it defaults to the driver’s instance name.

server_advertise_condition | Use: authenticators | Type: string† | Default: unset

When a server is about to advertise an authentication mechanism, the condition is expanded. If it yields the empty string, “0”, “no”, or “false”, the mechanism is not advertised. If the expansion fails, the mechanism is not advertised. If the failure was not forced, and was not caused by a lookup defer, the incident is logged. See section 33.3 below for further discussion.

server_condition | Use: authenticators | Type: string† | Default: unset

This option must be set for a plaintext server authenticator, where it is used directly to control authentication. See section 34.3 for details.

For the gsasl authenticator, this option is required for various mechanisms; see chapter 38 for details.

For the other authenticators, server_condition can be used as an additional authentication or authorization mechanism that is applied after the other authenticator conditions succeed. If it is set, it is expanded when the authenticator would otherwise return a success code. If the expansion is forced to fail, authentication fails. Any other expansion failure causes a temporary error code to be returned. If the result of a successful expansion is an empty string, “0”, “no”, or “false”, authentication fails. If the result of the expansion is “1”, “yes”, or “true”, authentication succeeds. For any other result, a temporary error code is returned, with the expanded string as the error text.

server_debug_print | Use: authenticators | Type: string† | Default: unset

If this option is set and authentication debugging is enabled (see the -d command line option), the string is expanded and included in the debugging output when the authenticator is run as a server. This can help with checking out the values of variables. If expansion of the string fails, the error message is written to the debugging output, and Exim carries on processing.

server_set_id | Use: authenticators | Type: string† | Default: unset

When an Exim server successfully authenticates a client, this string is expanded using data from the authentication, and preserved for any incoming messages in the variable $authenticated_id. It is also included in the log lines for incoming messages. For example, a user/password authenticator configuration might preserve the user name that was used to authenticate, and refer to it subsequently during delivery of the message. On a failing authentication the expansion result is instead saved in the $authenticated_fail_id variable. If expansion fails, the option is ignored.

server_mail_auth_condition | Use: authenticators | Type: string† | Default: unset

This option allows a server to discard authenticated sender addresses supplied as part of MAIL commands in SMTP connections that are authenticated by the driver on which server_mail_auth_condition is set. The option is not used as part of the authentication process; instead its (unexpanded) value is remembered for later use. How it is used is described in the following section.

### 2. The AUTH parameter on MAIL commands

When a client supplied an AUTH= item on a MAIL command, Exim applies the following checks before accepting it as the authenticated sender of the message:

- If the connection is not using extended SMTP (that is, HELO was used rather than EHLO), the use of AUTH= is a syntax error.

- If the value of the AUTH= parameter is “<>”, it is ignored.

- If acl_smtp_mailauth is defined, the ACL it specifies is run. While it is running, the value of $authenticated_sender is set to the value obtained from the AUTH= parameter. If the ACL does not yield “accept”, the value of $authenticated_sender is deleted. The acl_smtp_mailauth ACL may not return “drop” or “discard”. If it defers, a temporary error code (451) is given for the MAIL command.

- If acl_smtp_mailauth is not defined, the value of the AUTH= parameter is accepted and placed in $authenticated_sender only if the client has authenticated.

- If the AUTH= value was accepted by either of the two previous rules, and the client has authenticated, and the authenticator has a setting for the server_mail_auth_condition, the condition is checked at this point. The valued that was saved from the authenticator is expanded. If the expansion fails, or yields an empty string, “0”, “no”, or “false”, the value of $authenticated_sender is deleted. If the expansion yields any other value, the value of $authenticated_sender is retained and passed on with the message.

When $authenticated_sender is set for a message, it is passed on to other hosts to which Exim authenticates as a client. Do not confuse this value with $authenticated_id, which is a string obtained from the authentication process, and which is not usually a complete email address.

Whenever an AUTH= value is ignored, the incident is logged. The ACL for MAIL, if defined, is run after AUTH= is accepted or ignored. It can therefore make use of $authenticated_sender. The converse is not true: the value of $sender_address is not yet set up when the acl_smtp_mailauth ACL is run.

### 3. Authentication on an Exim server

When Exim receives an EHLO command, it advertises the public names of those authenticators that are configured as servers, subject to the following conditions:

- The client host must match auth_advertise_hosts (default *).

- It the server_advertise_condition option is set, its expansion must not yield the empty string, “0”, “no”, or “false”.

The order in which the authenticators are defined controls the order in which the mechanisms are advertised.

Some mail clients (for example, some versions of Netscape) require the user to provide a name and password for authentication whenever AUTH is advertised, even though authentication may not in fact be needed (for example, Exim may be set up to allow unconditional relaying from the client by an IP address check). You can make such clients more friendly by not advertising AUTH to them. For example, if clients on the 10.9.8.0/24 network are permitted (by the ACL that runs for RCPT) to relay without authentication, you should set

    auth_advertise_hosts = ! 10.9.8.0/24

so that no authentication mechanisms are advertised to them.

The server_advertise_condition controls the advertisement of individual authentication mechanisms. For example, it can be used to restrict the advertisement of a particular mechanism to encrypted connections, by a setting such as:

    server_advertise_condition = ${if eq{$tls_in_cipher}{}{no}{yes}}

If the session is encrypted, $tls_in_cipher is not empty, and so the expansion yields “yes”, which allows the advertisement to happen.

When an Exim server receives an AUTH command from a client, it rejects it immediately if AUTH was not advertised in response to an earlier EHLO command. This is the case if

- The client host does not match auth_advertise_hosts; or

- No authenticators are configured with server options; or

- Expansion of server_advertise_condition blocked the advertising of all the server authenticators.

Otherwise, Exim runs the ACL specified by acl_smtp_auth in order to decide whether to accept the command. If acl_smtp_auth is not set, AUTH is accepted from any client host.

If AUTH is not rejected by the ACL, Exim searches its configuration for a server authentication mechanism that was advertised in response to EHLO and that matches the one named in the AUTH command. If it finds one, it runs the appropriate authentication protocol, and authentication either succeeds or fails. If there is no matching advertised mechanism, the AUTH command is rejected with a 504 error.

When a message is received from an authenticated host, the value of $received_protocol is set to “esmtpa” or “esmtpsa” instead of “esmtp” or “esmtps”, and $sender_host_authenticated contains the name (not the public name) of the authenticator driver that successfully authenticated the client from which the message was received. This variable is empty if there was no successful authentication.

Successful authentication sets up information used by the authresults expansion item.

### 4. Testing server authentication

Exim’s -bh option can be useful for testing server authentication configurations. The data for the AUTH command has to be sent using base64 encoding. A quick way to produce such data for testing is the following Perl script:

    use MIME::Base64;
    printf ("%s", encode_base64(eval "\"$ARGV[0]\""));

This interprets its argument as a Perl string, and then encodes it. The interpretation as a Perl string allows binary zeros, which are required for some kinds of authentication, to be included in the data. For example, a command line to run this script on such data might be

    encode '\0user\0password'

Note the use of single quotes to prevent the shell interpreting the backslashes, so that they can be interpreted by Perl to specify characters whose code value is zero.

Warning 1: If either of the user or password strings starts with an octal digit, you must use three zeros instead of one after the leading backslash. If you do not, the octal digit that starts your string will be incorrectly interpreted as part of the code for the first character.

Warning 2: If there are characters in the strings that Perl interprets specially, you must use a Perl escape to prevent them being misinterpreted. For example, a command such as

    encode '\0user@domain.com\0pas$$word'

gives an incorrect answer because of the unescaped “@” and “$” characters.

If you have the mimencode command installed, another way to do produce base64-encoded strings is to run the command

    echo -e -n `\0user\0password' | mimencode

The -e option of echo enables the interpretation of backslash escapes in the argument, and the -n option specifies no newline at the end of its output. However, not all versions of echo recognize these options, so you should check your version before relying on this suggestion.

### 5. Authentication by an Exim client

The smtp transport has two options called hosts_require_auth and hosts_try_auth. When the smtp transport connects to a server that announces support for authentication, and the host matches an entry in either of these options, Exim (as a client) tries to authenticate as follows:

- For each authenticator that is configured as a client, in the order in which they are defined in the configuration, it searches the authentication mechanisms announced by the server for one whose name matches the public name of the authenticator.

- When it finds one that matches, it runs the authenticator’s client code. The variables $host and $host_address are available for any string expansions that the client might do. They are set to the server’s name and IP address. If any expansion is forced to fail, the authentication attempt is abandoned, and Exim moves on to the next authenticator. Otherwise an expansion failure causes delivery to be deferred.

- If the result of the authentication attempt is a temporary error or a timeout, Exim abandons trying to send the message to the host for the moment. It will try again later. If there are any backup hosts available, they are tried in the usual way.

- If the response to authentication is a permanent error (5xx code), Exim carries on searching the list of authenticators and tries another one if possible. If all authentication attempts give permanent errors, or if there are no attempts because no mechanisms match (or option expansions force failure), what happens depends on whether the host matches hosts_require_auth or hosts_try_auth. In the first case, a temporary error is generated, and delivery is deferred. The error can be detected in the retry rules, and thereby turned into a permanent error if you wish. In the second case, Exim tries to deliver the message unauthenticated.

Note that the hostlist test for whether to do authentication can be confused if name-IP lookups change between the time the peer is decided upon and the time that the transport runs. For example, with a manualroute router given a host name, and with DNS "round-robin" used by that name: if the local resolver cache times out between the router and the transport running, the transport may get an IP for the name for its authentication check which does not match the connection peer IP. No authentication will then be done, despite the names being identical.

For such cases use a separate transport which always authenticates.

When Exim has authenticated itself to a remote server, it adds the AUTH parameter to the MAIL commands it sends, if it has an authenticated sender for the message. If the message came from a remote host, the authenticated sender is the one that was receiving on an incoming MAIL command, provided that the incoming connection was authenticated and the server_mail_auth condition allowed the authenticated sender to be retained. If a local process calls Exim to send a message, the sender address that is built from the login name and qualify_domain is treated as authenticated. However, if the authenticated_sender option is set on the smtp transport, it overrides the authenticated sender that was received with the message.

<-previous Table of Contents next->