# Receiving

The server takes AS2 messages on its web ports. A partner posts a signed and encrypted file; the server decrypts it, checks it, stores the file, and answers with a receipt.

This page describes receiving with version 1, which is what the admin pages set up, and then what is different in version 2. For the two engines, see [AS2 overview](AS2.md#two-engines).

## Receiving with version 1

### What to set up

1. **A secure web port** that the partner can reach: usually 443. See [the ports of the server](Ports.md).
2. **A user for the partner**, with one folder they may upload into, your decryption key and their certificate: [Trading partners](AS2-Partners.md#for-receiving-a-user).

That is all. There is no AS2 switch: any message that arrives with AS2 headers on a web port is treated as AS2.

### What to tell the partner

| They ask for | Give |
|---|---|
| the address | `https://files.example.com/`, with the folder the files should land in after it: `https://files.example.com/inbox/` |
| your AS2 name | the user's name and password, joined by `-_-`: see below |
| your certificate | the certificate of your decryption key |
| receipts | at once or later, signed: both work |

### The AS2 name carries the login

Version 1 has no separate login for AS2. The partner signs in by what they put into the `AS2-To` header, your "AS2 name":

```text
as2_acme-_-Blue-Harbor-71
```

That is the user name, the three characters `-_-`, and the user's password. The server signs the partner in as that user for the one message.

Consequences:

- **The password is in a header of every message**, and it comes back in the receipt, which names the recipient. Over HTTPS nobody on the way reads it, but it is in the partner's system, in their logs, and in receipts they file. Give the AS2 user a password used nowhere else, and no rights beyond its one folder.
- **Changing the password changes your AS2 name**: the partner must change it on their side at the same moment.
- **The server-wide settings** `as2_from_as_to_for_username` and `as2_prepend_as2_username` change how the user is found from the headers, for partners whose systems cannot send such a name. They are among the settings without a screen: [Preference keys](Prefs.md).

This is the main reason version 2 exists. There the partner is known by their signature and no password travels.

### What happens to a message

1. The message arrives. The user named in it is signed in.
2. The body is decrypted with the user's **AS2/3 Decryption Key**.
3. If the user has an **AS2/3 Signature Verification Key**, the signature is checked against it.
4. The file is stored in the user's folder, at the path of the address the partner posted to, under the name the partner gave it. With **Unique AS2 filenames**, an id is added to the name.
5. A copy of the receipt is stored beside it, as a file ending in `.out.mdn`.
6. The receipt is sent: in the answer, or later to the address the partner asked for. See [Receipts](AS2-MDN.md).
7. The user's upload [events](Users-Events.md) run for the file, as for any upload. That is how received documents are passed on: a job that moves them, converts them, or tells somebody.

The whole message is held in memory while this happens. A file of a few hundred megabytes needs a heap several times its size.

### Looking at what came in

| Where | Shows |
|---|---|
| the partner user's folder | the files, and the receipts sent for them |
| the [log](Admin-Log.md) | each message, with its headers. The password part of the AS2 name is masked on the lines that name it. |
| [Reports](Reports.md) | uploads by user, like any others |

### When a message is refused

| The partner sees | Look at |
|---|---|
| a login failure, 401 or 403 | the AS2 name: user, `-_-`, password, exactly. Protocols and addresses allowed for the user. |
| a receipt that says decryption failed | they encrypted for another certificate than the one of the user's decryption key. Send them the right certificate; check the alias with **Test Certificate**. |
| a receipt that says the signature failed | the user's verification key is not the certificate they sign with |
| a size error, 413 | the file is too large for the server's memory |
| nothing, a timeout | the port, the firewall, the address |

## Receiving with version 2

Version 2 takes messages at a path of its own, `/as2v2/` unless changed, on the web ports you allow it on.

| | Version 1 | Version 2 |
|---|---|---|
| the address | any path of a web port | `https://files.example.com/as2v2/` |
| your AS2 name | user, `-_-`, password | the plain AS2 name of your station |
| who the sender is | whoever knows the password | whoever signs with a certificate pinned to the partner |
| before the body is read | nothing | optionally: allowed addresses, a name and password for the connection, or the partner's TLS certificate |
| the file while it is checked | in memory | on disk, in a quarantine folder of the server |
| stored when | at once | only after the signature, the checksum and the rules have passed |
| stored where | the folder of the user who signed in | where the partner's **routes** say: the folders of a user you name, written as that user would upload, so that permissions, quotas and events apply |
| the same message twice | stored twice | stored once; the first receipt is sent again |
| too many failures from one address | handled by the server's usual [banning](Prefs-Banning.md) | 20 failures in 10 minutes block the address for 30 minutes, unless changed |

A port takes version 2 messages when its entry in the server's settings has `as2v2_allowed`, or `as2v2_only`, which closes the port to everything else. Without TLS it must be allowed expressly, and then only signed and encrypted messages are taken.

A partner can be moved without changing anything on their side: version 2 can take over the address and AS2 name a partner uses today, password form included. That is what the import tool sets up: [Moving from AS2 version 1](AS2-Upgrade-from-v1.md).

Version 2 runs on the internal server only. With a DMZ server in front, the port for it is passed inward as a plain forward: see [DMZ](DMZ.md).

## Things to know

- **A partner's first message usually fails.** Ask them to send a small test file, and read both the receipt they got and your log.
- **Receipts are part of the record.** Keep the `.out.mdn` files as long as the documents: they are your proof of what you acknowledged.
- **AS3**, AS2's sibling over FTP, is taken by version 1 for users with **Process AS3 Files**.
