# Pairing a DMZ

A [DMZ server](DMZ.md) does what its server tells it: it takes the licence, the certificates and the settings from it, and passes every login to it. So each must be sure of the other. **Pairing** ties the two together by their own keys, once, with a code that an administrator carries from one to the other.

In CrushMFT 27 a DMZ is always paired. A DMZ that is not paired serves nobody, and a server links to no DMZ it is not paired with.

## What you need

- The DMZ started on its machine: see [Starting a DMZ](DMZ.md#starting-a-dmz).
- A DMZ port for it on the server, set to version 6: see [The DMZ port](DMZ.md#2-the-dmz-port).
- A way to read the DMZ's console or log, to get the code.
- The link between them encrypted, as it is unless somebody turned that off: without TLS a DMZ can be neither paired nor used.

## The code the DMZ shows

A DMZ that is not paired makes a one-time code when it starts and prints it, to its console and its log:

```
This DMZ is not paired. Any server that can reach port 9000 can control it.
To pair it, open the admin page of your CrushMFT server: Cluster, Pair a DMZ, and enter
    Code:         CFE1-7K2M9Q-RT41XB-0PGW3N-C8DZ5H-M6JA2V-KQ7E9S-B4XN1T-W0R8FY-3GCD
    Fingerprint:  3F9A-01C2-77B0-E4D5
The code works once and until 14:32. It is also in cluster/pairing_code.txt.
```

- The code is good for 60 minutes and for one use.
- It is also in the file `cluster/pairing_code.txt` in the DMZ's folder, readable by the DMZ's account only.
- For a fresh code without a restart, make an empty file `cluster/new_code` in the DMZ's folder. The DMZ sees it, deletes it, and prints a new code.
- After five wrong tries the code is made void, and a new one is needed.

While it is not paired, the DMZ accepts a pairing and nothing else.

## Pairing

On the server: **Server Admin, Cluster, Pair a DMZ**.

> Pairing pins this server's link to the DMZ's own key, so nothing in between can pose as either side. A DMZ that is not paired prints a one-time code and its fingerprint when it starts; the code is also in cluster/pairing_code.txt on its machine.

1. **Which DMZ port**: choose the port. Each is listed with its state: "Not pinned, not running", or "Pinned to dmz1 (7387-…)".
2. Enter the **Code**. The dialog reads the fingerprint out of it: "This code was made by the DMZ with fingerprint …. Compare it with the one on its screen." Do compare.
3. **Pair with the code**.

What happens: the server connects to the DMZ, checks that the key it meets is the one the code names, and both prove to each other that they know the code, without sending it. The DMZ stores which servers may control it; the server adds the DMZ to its member list and ties the port to it. The port starts, and the link comes up.

If the server was in no cluster, it becomes a cluster of one for this.

With no DMZ port on the server, the dialog says where to make one: "This server has no DMZ port. Add one of the type DMZ in Preferences, IP/servers, then pair it here."

From [Preferences, IP / Servers](Ports.md), **Pair in Server Admin** on a DMZ port leads straight to this dialog, with the port chosen.

## Afterwards

A paired DMZ says so at every start:

```
This DMZ is paired with cluster "Production". Fingerprint: 3F9A-01C2-77B0-E4D5
It accepts these servers:  web1 (A1B2-...)   web2 (77F0-...)
```

It accepts a connection only from a server whose key it holds, and only the current kind of link. A stranger who reaches its port, a server that was removed from the cluster, a server with the standard key of an old version: all are refused.

On the server, the port's line reads "Pinned to dmz1 (7387-…)", and the DMZ is on the Cluster page as a node.

Restarts of either side need nothing: the link comes back by the keys.

## A second server for the same DMZ

When the DMZ is paired with one server of a cluster, the other servers need no code. The DMZ already holds the keys of all servers of the cluster: the list is sent to it whenever it changes.

On the second server, **Pair a DMZ** offers for its port: **Link to a DMZ paired already**.

> When this DMZ is paired with another server of the cluster already, link this port to it; no code is needed.

The second server then stands by for that DMZ: see [The DMZ server](DMZ.md#more-than-one).

## Comparing fingerprints

A member that came in without a code is marked unverified, and for a DMZ the page offers **Compare fingerprints**:

> Read the fingerprint on the DMZ itself: it prints it when it starts, and it is in its log.

**They match** clears the mark. If they do not, the dialog says what to do: remove it, and look at the network path between the two machines.

## Undoing a pairing

Only on the DMZ's own machine. In the DMZ's folder:

```
java -jar CrushMFT.jar -dmz_unpair
```

and start the DMZ again. It has forgotten its servers, kept its key, and prints a new code.

Nothing that arrives over the network can unpair a DMZ. That is deliberate: a DMZ that could be talked out of its pairing from outside would be no safer than one that was never paired.

On the server's side, **Remove** in the DMZ's node detail takes it out of the cluster: the servers then refuse it. See [Changing a key, removing a server](Cluster-Changes.md#removing-a-node).

To move a DMZ to another cluster: unpair it on its machine, remove it from the old cluster, pair it with a server of the new one.

## When it does not pair

| Sign | Look at |
|---|---|
| "The code was mistyped." | the code has a check built in: type it again |
| the fingerprint in the dialog is not the one on the DMZ's console | you are not talking to that DMZ. Check the port's address. Do not go on. |
| the code is refused | it was used, is older than 60 minutes, or was made void by wrong tries: make a new one with `cluster/new_code` |
| the server cannot connect | the firewall: from the server to the DMZ's port |
| "Version 5 cannot be pinned. Change the port to DMZ version 6 in Preferences, IP/servers, and start the DMZ with this build first." | the port's version |
| the DMZ is of an older version | update it first |

## Things to know

- **One code, one DMZ.** Each DMZ makes its own.
- **The DMZ's key stays on the DMZ.** A new machine for a DMZ is a new DMZ: pair it, and remove the old one.
- **The alert Cluster Enrolment Failed** fires when codes were made void by wrong tries. Somebody is trying.
- **What was tried**, when the cluster was built: a DMZ printed its code; pairing worked; the paired DMZ refused a stranger, the standard certificate of old versions, and old kinds of link; a removed server was refused by the DMZ; the link came back after restarts with no code; `-dmz_unpair`; five wrong codes voided the code; the trigger file made a new one. These were run through the server's commands on one machine; pairing through the page with a real code, and a second server linking a paired DMZ, were not run.
