Free ACME scripts / Linux
FortiMail (Linux) certificate renewal script for lego
Installs the certificate that lego keeps on this server on a FortiMail, over SSH, and keeps trying until it is installed. Reports every result when it has a report address.
The scripts are made for simple-acme and lego, which FairSSL, the company behind sslbrain, sponsors.
- Version
- 1.2.0
- Language
- bash
- SHA-256
- 8c89447919b8677e0e19d669cfbe1cb26f713a22fc809f19c6f196053383aa4b
- Licence
- Apache-2.0, copyright FairSSL A/S
Download it on the server, check the SHA-256 and keep it as /usr/local/bin/fairssl-fortimail, so a newer version later replaces it without a change to your renewal:
curl -fsSL -o /tmp/fortimail-linux-1.2.0.sh 'https://sslbrain.com/free-acme-scripts/files/fortimail-linux-1.2.0.sh'
echo '8c89447919b8677e0e19d669cfbe1cb26f713a22fc809f19c6f196053383aa4b /tmp/fortimail-linux-1.2.0.sh' | sha256sum -c -
sudo install -m 0755 /tmp/fortimail-linux-1.2.0.sh /usr/local/bin/fairssl-fortimail What it does
Used by FairSSL's guide "FortiMail (Linux Agent)" and, with any ACME service, as lego's deploy hook.
fairssl-fortimail setup --name NAME --host ADDRESS [--port 22] [--user admin] [--report-url URL] Asks for the FortiMail password and keeps it in /etc/fairssl-fortimail/NAME.password (root only, 0600), pins FortiMail's SSH host key in /etc/fairssl-fortimail/known_hosts, logs in once and shows what it found. fairssl-fortimail deploy NAME Installs the certificate now and shows the result. If the attempt fails, a background worker keeps trying. fairssl-fortimail hook NAME lego's --deploy-hook: starts the background worker and returns at once. fairssl-fortimail resume NAME At boot: starts the worker again when a restart interrupted a deployment. fairssl-fortimail status NAME Shows the settings (never the password), the last result and the log.
What the worker does, each attempt: it reads the certificate and key lego saved, logs in to FortiMail with the password from the root-only file (handed to ssh by this script as its askpass program, never in a command line, the environment or a log), uploads the certificate with its intermediates as a local certificate named fssl-<6 hex of the certificate name>-<12 hex of the certificate's SHA-256>, makes it FortiMail's default certificate (HTTPS, webmail and mail over TLS), reloads httpd and smtpd, and looks with TLS handshakes on HTTPS and SMTP (STARTTLS) at what it serves; the default certificate read back over SSH decides. If a listener that answered before stops answering, the previous certificate is put back. It keeps the previous certificate object and deletes older objects of its own. A failed attempt is reported and tried again after 1, 2, 5, 15, 30 and then every 60 minutes, for as long as it takes, also after a restart of this server. Every result is sent to the report address (a failure that repeats unchanged once a day); a report the receiver has not confirmed waits in /var/lib/fairssl-fortimail/outbox and is sent again.
Requires: bash 4, OpenSSH client, openssl, curl, flock and setsid (util-linux), run as root.
Requirements
- A Linux server with bash, run as root
- lego 5.0.4 or newer, which runs the script after each issuance and renewal
Parameters
| --name | setup: the certificate's name, as lego knows it (--cert.name). The other commands take it as NAME. |
|---|---|
| --host | setup: FortiMail's IP address or host name. |
| --port | setup: FortiMail's SSH port. Defaults to 22. |
| --user | setup: the FortiMail administrator the script logs in as. Defaults to admin. |
| --https-port | setup: FortiMail's HTTPS port for the TLS check. Defaults to 443. |
| --smtp-port | setup: FortiMail's SMTP port for the TLS check (STARTTLS). Defaults to 25. |
| --report-url | setup: where each result is reported, an https address. Leave it out: the address set up before is kept, otherwise DEFAULT_REPORT_URL below is used; when that is empty too, nothing is sent. |
| --lego-path | setup: lego's --path. Defaults to /etc/ssl/fortimail. |
Get a certificate and run the script
Made for lego; works with certbot and acme.sh too.
lego orders the certificate, keeps it and its key under certificates/ in the --path folder,
and runs the script from --deploy-hook after the first issuance and each renewal. Replace
www.example.com, the e-mail address and the script's arguments (see
Parameters). The HTTP challenge is answered from /var/www/html;
use your web server's document root.
sudo /usr/local/bin/lego run --domains "www.example.com" \
--server "https://acme-v02.api.letsencrypt.org/directory" \
--accept-tos --email "you@example.com" \
--http --http.webroot /var/www/html \
--path /etc/ssl/fortimail \
--deploy-hook "/usr/local/bin/fairssl-fortimail <arguments>"
Renew from cron, for example daily at 03:30 in /etc/cron.d/lego-fortimail-linux, with the same
--server, --path and --email and without the EAB options,
so lego reuses its account and renews when the certificate is due:
30 3 * * * root /usr/local/bin/lego run --domains "www.example.com" --server "https://acme-v02.api.letsencrypt.org/directory" --accept-tos --email "you@example.com" --http --http.webroot /var/www/html --path /etc/ssl/fortimail --renew-days 7 --deploy-hook "/usr/local/bin/fairssl-fortimail <arguments>" Choose the ACME server
The command uses Let's Encrypt. For another ACME server, put its options in place of
--server "…".
| Let's Encrypt | --server "https://acme-v02.api.letsencrypt.org/directory" No account to create: the client registers one with your e-mail address. Let's Encrypt documentation |
|---|---|
| Google Trust Services | --server "https://dv.acme-v02.api.pki.goog/directory" --eab --eab.kid "EAB_KID" --eab.hmac "EAB_HMAC" Create an EAB key in a Google Cloud project (gcloud publicca external-account-keys create). Put the key's id and HMAC in place of EAB_KID and EAB_HMAC. Google Public CA tutorial |
| ZeroSSL | --server "https://acme.zerossl.com/v2/DV90" --eab --eab.kid "EAB_KID" --eab.hmac "EAB_HMAC" Copy the EAB credentials from the Developer section of your ZeroSSL account. Put the key's id and HMAC in place of EAB_KID and EAB_HMAC. ZeroSSL ACME documentation |
| acme.sslbrain.com | --server "https://acme.sslbrain.com/acme/directory" --eab --eab.kid "EAB_KID" --eab.hmac "EAB_HMAC" Create an EAB key in sslbrain Cloud (Basic or higher) under ACME clients, Connect a client. sslbrain Cloud validates the domain through a CNAME redirect, a DNS API or pre-validation, not your ACME client. Put the key's id and HMAC in place of EAB_KID and EAB_HMAC. sslbrain plans |
Report each result to your own receiver
The script can send the result of every installation to an HTTPS address of yours, so one place shows the status, errors
and expiry of all your servers. It sends nothing until you give it the address: pass
--report-url https://reports.example.com/acme or set the report address in the variable at the top of the script.
Each report is a JSON POST; your receiver answers {"ok": true}, and a report without that answer
is kept and sent again. The fields are in the report format.
Licence and credits
Copyright FairSSL A/S, licensed under the Apache License 2.0: use it, change it and share it, also commercially.
Writing a guide or answering a question? Link to this page instead of copying the script: it always has the newest version and its SHA-256.
[FortiMail (Linux) certificate renewal script for lego](https://sslbrain.com/free-acme-scripts/fortimail-linux/) <a href="https://sslbrain.com/free-acme-scripts/fortimail-linux/">FortiMail (Linux) certificate renewal script for lego</a> One script per server, or sslbrain does it for all your servers without scripts.
Feedback and improvements are welcome: info@fairssl.dk