aboutsummaryrefslogtreecommitdiffstats
path: root/check-certificates
blob: f0a466e7aa8b1c3f71b207a4bfbf469d30d9aae4 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!rsc
# RouterOS script: check-certificates
# Copyright (c) 2013-2019 Christian Hesse <mail@eworm.de>
#
# check for certificate validity

:global Identity;
:global CertRenewUrl;
:global CertRenewPass;

:global SendNotification;

:foreach Cert in=[ / certificate find where !revoked ] do={
  :local CertName [ / certificate get $Cert name ];
  :local ExpiresAfter [ / certificate get $Cert expires-after ];

  :if ([ :typeof $ExpiresAfter ] = "time") do={
    :if ($ExpiresAfter < 2w) do={
      :local CommonName [ / certificate get $Cert common-name ];
      :local FingerPrint [ / certificate get $Cert fingerprint ];

      :do {
        :if ([ :len $CertRenewUrl ] = 0) do={
          :error "No CertRenewUrl given.";
        }

        / tool fetch mode=https check-certificate=yes-without-crl url=($CertRenewUrl . $CommonName . ".pem");
        / certificate import file-name=($CommonName . ".pem") passphrase=$CertRenewPass;
        / file remove [ find where name=($CommonName . ".pem") ];

        :local CertNew [ / certificate find where common-name=$CommonName fingerprint!=$FingerPrint ];
        :local CertNameNew [ / certificate get $CertNew name ];

        :foreach IpService in=[ / ip service find where certificate=$CertName ] do={
          / ip service set $IpService certificate=$CertNameNew;
        }

        :do {
          :foreach Hotspot in=[ / ip hotspot profile find where ssl-certificate=$CertName ] do={
            / ip hotspot profile set $Hotspot ssl-certificate=$CertNameNew;
          }
        } on-error={
          :log debug ("Setting hotspot certificates failed. Hotspot package not installed?");
        }

        / certificate remove $Cert;
        / certificate set $CertNew name=$CertName;

        :local InvalidBefore [ / certificate get $CertNew invalid-before ];
        :local InvalidAfter [ / certificate get $CertNew invalid-after ];
        :local Issuer [ / certificate get $CertNew issuer ];
        :set CommonName [ / certificate get $CertNew common-name ];
        :set FingerPrint [ / certificate get $CertNew fingerprint ];

        $SendNotification ("Certificate renewed") \
          ("A certificate on " . $Identity . " has been renewed.\n\n" . \
            "Certificate Name: " . $CertName . "\n" . \
            "Common Name:      " . $CommonName . "\n" . \
            "Fingerprint:      " . $FingerPrint . "\n" . \
            "Issuer:           " . $Issuer . "\n" . \
            "Validity:         " . $InvalidBefore . " to " . $InvalidAfter);
        :log info ("The certificate " . $CertName . " has been renewed.");
      } on-error={
        :local InvalidBefore [ / certificate get $Cert invalid-before ];
        :local InvalidAfter [ / certificate get $Cert invalid-after ];
        :local Issuer [ / certificate get $Cert issuer ];

        $SendNotification ("Certificate warning!") \
          ("A certificate on " . $Identity . " is about to expire.\n\n" . \
            "Certificate Name: " . $CertName . "\n" . \
            "Common Name:      " . $CommonName . "\n" . \
            "Fingerprint:      " . $FingerPrint . "\n" . \
            "Issuer:           " . $Issuer . "\n" . \
            "Validity:         " . $InvalidBefore . " to " . $InvalidAfter);
        :log warning ("The certificate " . $CertName . " is about to expire in " . $ExpiresAfter . ".");
      }
    } else={
      :log debug ("The certificate " . $CertName . " expires in " . $ExpiresAfter . ".");
    }
  } else={
    :log debug ("The certificate " . $CertName . " is just a template.");
  }
}