<a id="module-auth"></a>

<a id="auth"></a>

# AUTH

The AUTH script allows trusted SMTP clients to authenticate. The SASL mechanisms *LOGIN* and *PLAIN* are implemented in the SMTP engine and will populate the `username` and `password` arguments. If you add support for custom authentication mechanisms you will need to use the `mechanism`, `state` and `response` arguments instead.

<a id="variables"></a>

## Variables

These are the pre-defined variables available.

| Variable                                                    | Type   | Read-only   | Description                                  |
|-------------------------------------------------------------|--------|-------------|----------------------------------------------|
| [$arguments](#v-a3)   | array  | yes         | Context/hook arguments                       |
| [$connection](#v-c3)  | array  | yes         | Connection/session bound                     |
| [$transaction](#v-t3) | array  | yes         | Transaction bound                            |
| $context                                                    | any    | no          | Connection bound user-defined (default none) |

<a id="v-a3"></a>

<a id="arguments"></a>

### Arguments

| Array item   | Type   | Example      | Description                                                          |
|--------------|--------|--------------|----------------------------------------------------------------------|
| username     | string | `"mailuser"` | SASL username (only available with LOGIN or PLAIN)                   |
| password     | string | `"secret"`   | SASL password (only available with LOGIN or PLAIN)                   |
| mechanism    | string | `"PLAIN"`    | SASL mechanism (always in uppercase)                                 |
| state        | number | `0`          | SASL state, incremeted per Reply (not available with LOGIN or PLAIN) |
| response     | string | `none`       | SASL response (not available with LOGIN or PLAIN)                    |

<a id="v-c3"></a>

<a id="connection"></a>

### Connection

| Array item                                          | Type   | Example                     | Description                                              |
|-----------------------------------------------------|--------|-----------------------------|----------------------------------------------------------|
| id                                                  | string | `"18c190a3-93f-47d7-bd..."` | ID of the connection                                     |
| priority                                            | number | `1`                         | The connection priority                                  |
| remoteip                                            | string | `"192.168.1.11"`            | IP address of the connected client                       |
| remoteport                                          | number | `41666`                     | TCP port of connected client                             |
| remoteptr                                           | string | `"mail.example.org"`        | Reverse DNS (FCrDNS) for remoteip (not always available) |
| remoteuid                                           | number | `106`                       | UNIX socket remote user id (not always available)        |
| localip                                             | string | `"10.0.0.1"`                | IP address of the server                                 |
| localport                                           | number | `25`                        | TCP port of the server                                   |
| serverid                                            | string | `"inbound"`                 | ID of the server                                         |
| [helo](#he3) | array  |                             | HELO information (not always available)                  |
| [tls](#tls3)  | array  |                             | TLS information (if TLS was started)                     |

<a id="he3"></a>

<a id="helo"></a>

#### HELO

| Array item   | Type   | Example              | Description          |
|--------------|--------|----------------------|----------------------|
| verb         | string | `"EHLO"`             | HELO or EHLO command |
| host         | string | `"mail.example.com"` | HELO hostname        |

<a id="tls3"></a>

<a id="tls"></a>

#### TLS

| Array item                                              | Type   | Example                     | Description                                      |
|---------------------------------------------------------|--------|-----------------------------|--------------------------------------------------|
| protocol                                                | string | `"TLSv1.3"`                 | The protocol                                     |
| cipher                                                  | string | `"ECDHE-RSA-AES256-SHA384"` | The cipher                                       |
| keysize                                                 | number | `256`                       | The keysize                                      |
| sni                                                     | string | `"example.com"`             | The SNI hostname (not always available)          |
| [peercert](#p3) | array  |                             | The peer certificate (if provided by the client) |

<a id="p3"></a>

<a id="peercert"></a>

##### Peercert

| Array item   | Type         | Example   | Description                                                                                          |
|--------------|--------------|-----------|------------------------------------------------------------------------------------------------------|
| x509         | X509Resource |           | An X509Resource to be used with the [`X509`](functions.html.md#X509) class |
| chain        | array        |           | An array of X509Resource (the chain)                                                                 |
| error        | number       | `18`      | The peer certificate validation error (see OpenSSLs SSL_get_verify_result(3))                        |

<a id="v-t3"></a>

<a id="transaction"></a>

### Transaction

| Array item   | Type   | Example                     | Description           |
|--------------|--------|-----------------------------|-----------------------|
| id           | string | `"18c190a3-93f-47d7-bd..."` | ID of the transaction |

<a id="functions"></a>

## Functions

<a id="Accept"></a>

<a id="auth.Accept"></a>

### Accept([options])

Authorize the login request.

* **Parameters:**
  **options** (*array*) – an options array
* **Returns:**
  doesn’t return, script is terminated

The following options are available in the options array.

> * **username** (string) Set or change the username. The default is the `username` argument (if available).
> * **reason** (string or array) The reason to report. The default is a system generated message.
> * **reply_codes** (array) The associative array may contain the properties *code* (number) and *enhanced* (array of three numbers). To remove the *enhanced* code set it to `[0,0,0]`. The default *code* is `235` and *enhanced* is `[2,7,0]`.
> * **priority** (number) Change the connection priority (0 normal, 1 high).

<a id="Reject"></a>

<a id="auth.Reject"></a>

### Reject([reason[, options]])

Reject the login request with a permanent (535) error.

* **Parameters:**
  * **reason** (*string* *or* *array*) – reject message with reason
  * **options** (*array*) – an options array
* **Returns:**
  doesn’t return, script is terminated

The following options are available in the options array.

> * **disconnect** (boolean) Disconnect the client. The default is `false`.
> * **reply_codes** (array) The associative array may contain the properties *code* (number) and *enhanced* (array of three numbers). To remove the *enhanced* code set it to `[0,0,0]`. The default *code* is `535` and *enhanced* is `[5,7,8]`.
> * **priority** (number) Change the connection priority (0 normal, 1 high).

<a id="Defer"></a>

<a id="auth.Defer"></a>

### Defer([reason[, options]])

Defer the login request with a temporary (454) error.

* **Parameters:**
  * **reason** (*string* *or* *array*) – defer message with reason
  * **options** (*array*) – an options array
* **Returns:**
  doesn’t return, script is terminated

The following options are available in the options array.

> * **disconnect** (boolean) Disconnect the client. The default is `false`.
> * **reply_codes** (array) The associative array may contain the properties *code* (number) and *enhanced* (array of three numbers). To remove the *enhanced* code set it to `[0,0,0]`. The default *code* is `454` and *enhanced* is `[4,7,0]`.
> * **priority** (number) Change the connection priority (0 normal, 1 high).

<a id="Reply"></a>

<a id="auth.Reply"></a>

### Reply([reply[, options]])

Send a reply (334) message. The reply will be base64 encoded before sent to the client. This function is used to implement custom authentication mechanisms.

* **Parameters:**
  * **reply** (*string*) – the reply message
  * **options** (*array*) – an options array
* **Increments:**
  `state` argument
* **Returns:**
  doesn’t return, script is terminated

The following options are available in the options array.

> * **disconnect** (boolean) Disconnect the client. The default is `false`.
> * **reply_codes** (array) The associative array may contain the properties *code* (number) and *enhanced* (array of three numbers). To remove the *enhanced* code set it to `[0,0,0]`. The default *code* is `334` and *enhanced* is not used.
> * **priority** (number) Change the connection priority (0 normal, 1 high).

<a id="on-script-error"></a>

## On script error

On script error [`Defer()`](#auth.Defer) is called.

<a id="on-implicit-termination"></a>

## On implicit termination

If not explicitly terminated then [`Reject()`](#auth.Reject) is called.

<a id="authentication-diagram"></a>

## Authentication diagram

A flow chart diagram of how custom authentication is implemented:

```default
             +--------------+
             | AUTH request |
             +--------------+
                    |
                    |
                    v
+---------------------------------------+
|   state = 0                           |
+---------------------------------------+      Accept()      +-------------------+
| > AUTH mechanism [response]           | ---- Reject() ---> | AUTH request done |
+---------------------------------------+      Defer()       +-------------------+
                    |                             ^
                    |                             |
                  Reply() <------------------+    |
                    |                        |    |
                    |                        |    |
                    v                        |    |
+---------------------------------------+    |    |
|   state += 1                          |    |    |
+---------------------------------------+    |    |
| > response                            | ---+----+
+---------------------------------------+    |
                    |                        |
                    |                        |
                    +------------------------+
```
