mirror of
				https://github.com/eworm-de/routeros-scripts.git
				synced 2024-05-11 05:55:19 +00:00 
			
		
		
		
	Copyright (C) 2013-2020 Christian Hesse <mail@eworm.de> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. https://www.gnu.org/licenses/#GPL https://www.gnu.org/licenses/gpl.html https://www.gnu.org/licenses/gpl.md
		
			
				
	
	
		
			126 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			126 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
#!rsc
 | 
						|
# RouterOS script: check-certificates
 | 
						|
# Copyright (c) 2013-2020 Christian Hesse <mail@eworm.de>
 | 
						|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
 | 
						|
#
 | 
						|
# check for certificate validity
 | 
						|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/check-certificates.md
 | 
						|
 | 
						|
:global CertRenewPass;
 | 
						|
:global CertRenewUrl;
 | 
						|
:global Identity;
 | 
						|
 | 
						|
:global CertificateAvailable
 | 
						|
:global CertificateNameByCN;
 | 
						|
:global LogPrintExit;
 | 
						|
:global ParseKeyValueStore;
 | 
						|
:global SendNotification;
 | 
						|
:global TimeIsSync;
 | 
						|
:global UrlEncode;
 | 
						|
:global WaitForFile;
 | 
						|
 | 
						|
:local FormatExpire do={
 | 
						|
  :global CharacterReplace;
 | 
						|
  :return [ $CharacterReplace [ $CharacterReplace [ :tostr $1 ] "w" "w " ] "d" "d " ];
 | 
						|
}
 | 
						|
 | 
						|
:if ($TimeIsSync = false) do={
 | 
						|
  $LogPrintExit warning ("Time is not yet synchronized.") true;
 | 
						|
}
 | 
						|
 | 
						|
:foreach Cert in=[ / certificate find where !revoked !ca !scep-url expires-after<3w ] do={
 | 
						|
  :local CertVal [ / certificate get $Cert ];
 | 
						|
 | 
						|
  :do {
 | 
						|
    :if ([ :len $CertRenewUrl ] = 0) do={
 | 
						|
      $LogPrintExit warning ("No CertRenewUrl given.") true;
 | 
						|
    }
 | 
						|
 | 
						|
    :foreach Type in={ ".pem"; ".p12" } do={
 | 
						|
      :local CertFileName ([ $UrlEncode ($CertVal->"common-name") ] . $Type);
 | 
						|
      :do {
 | 
						|
        / tool fetch check-certificate=yes-without-crl \
 | 
						|
            ($CertRenewUrl . $CertFileName) dst-path=$CertFileName;
 | 
						|
        $WaitForFile $CertFileName;
 | 
						|
        :foreach PassPhrase in=$CertRenewPass do={
 | 
						|
          / certificate import file-name=$CertFileName passphrase=$PassPhrase;
 | 
						|
        }
 | 
						|
        / file remove [ find where name=$CertFileName ];
 | 
						|
 | 
						|
        :foreach CertInChain in=[ / certificate find where name~("^" . $CertFileName . "_[0-9]+\$") common-name!=($CertVal->"common-name") ] do={
 | 
						|
          $CertificateNameByCN [ / certificate get $CertInChain common-name ];
 | 
						|
        }
 | 
						|
      } on-error={
 | 
						|
        $LogPrintExit debug ("Could not download certificate file " . $CertFileName) false;
 | 
						|
      }
 | 
						|
    }
 | 
						|
 | 
						|
    :local CertNew [ / certificate find where common-name=($CertVal->"common-name") fingerprint!=[ :tostr ($CertVal->"fingerprint") ] expires-after>3w ];
 | 
						|
    :local CertNewVal [ / certificate get $CertNew ];
 | 
						|
 | 
						|
    :if ([ $CertificateAvailable ([ $ParseKeyValueStore ($CertNewVal->"issuer") ]->"CN") ] = false) do={
 | 
						|
      $LogPrintExit warning ("The certificate chain is not available!") false;
 | 
						|
    }
 | 
						|
 | 
						|
    :if ($Cert != $CertNew) do={
 | 
						|
      $LogPrintExit debug ("Certificate '" . $CertVal->"name" . "' was not updated, but replaced.") false;
 | 
						|
 | 
						|
      / ip service set certificate=($CertNewVal->"name") [ find where certificate=($CertVal->"name") ];
 | 
						|
 | 
						|
      :do {
 | 
						|
        / ip ipsec identity set certificate=($CertNewVal->"name") [ / ip ipsec identity find where certificate=($CertVal->"name") ];
 | 
						|
        / ip ipsec identity set remote-certificate=($CertNewVal->"name") [ / ip ipsec identity find where remote-certificate=($CertVal->"name") ];
 | 
						|
      } on-error={
 | 
						|
        $LogPrintExit debug ("Setting IPSEC certificates failed. Package 'security' not installed?") false;
 | 
						|
      }
 | 
						|
 | 
						|
      :do {
 | 
						|
        / ip hotspot profile set ssl-certificate=($CertNewVal->"name") [ / ip hotspot profile find where ssl-certificate=($CertVal->"name") ];
 | 
						|
      } on-error={
 | 
						|
        $LogPrintExit debug ("Setting hotspot certificates failed. Package 'hotspot' not installed?") false;
 | 
						|
      }
 | 
						|
 | 
						|
      / certificate remove $Cert;
 | 
						|
      / certificate set $CertNew name=($CertVal->"name");
 | 
						|
    }
 | 
						|
 | 
						|
    $SendNotification ("Certificate renewed") \
 | 
						|
      ("A certificate on " . $Identity . " has been renewed.\n\n" . \
 | 
						|
        "Name:        " . ($CertVal->"name") . "\n" . \
 | 
						|
        "CommonName:  " . ($CertNewVal->"common-name") . "\n" . \
 | 
						|
        "Fingerprint: " . ($CertNewVal->"fingerprint") . "\n" . \
 | 
						|
        "Issuer:      " . ([ $ParseKeyValueStore ($CertNewVal->"issuer") ]->"CN") . "\n" . \
 | 
						|
        "Validity:    " . ($CertNewVal->"invalid-before") . " to " . ($CertNewVal->"invalid-after") . "\n" . \
 | 
						|
        "Expires in:  " . [ $FormatExpire ($CertNewVal->"expires-after") ]) "" "true";
 | 
						|
    $LogPrintExit info ("The certificate " . ($CertVal->"name") . " has been renewed.") false;
 | 
						|
  } on-error={
 | 
						|
    $LogPrintExit debug ("Could not renew certificate " . ($CertVal->"name") . ".") false;
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
:foreach Cert in=[ / certificate find where !revoked !scep-url expires-after<2w fingerprint~"." ] do={
 | 
						|
  :local CertVal [ / certificate get $Cert ];
 | 
						|
 | 
						|
  :if ([ / certificate scep-server print count-only where ca-cert=($CertVal->"ca") ] > 0) do={
 | 
						|
    $LogPrintExit debug ("Certificate \"" . ($CertVal->"name") . "\" is handled by SCEP, skipping.") false;
 | 
						|
  } else={
 | 
						|
    :local ExpiresAfter [ $FormatExpire ($CertVal->"expires-after") ];
 | 
						|
    :local State "is about to expire";
 | 
						|
    :if (($CertVal->"expired") = true) do={
 | 
						|
      :set ExpiresAfter "expired";
 | 
						|
      :set State "expired";
 | 
						|
    }
 | 
						|
 | 
						|
    $SendNotification ("Certificate warning!") \
 | 
						|
      ("A certificate on " . $Identity . " " . $State . ".\n\n" . \
 | 
						|
        "Name:        " . ($CertVal->"name") . "\n" . \
 | 
						|
        "CommonName:  " . ($CertVal->"common-name") . "\n" . \
 | 
						|
        "Fingerprint: " . ($CertVal->"fingerprint") . "\n" . \
 | 
						|
        "Issuer:      " . ($CertVal->"ca") . ([ $ParseKeyValueStore ($CertVal->"issuer") ]->"CN") . "\n" . \
 | 
						|
        "Validity:    " . ($CertVal->"invalid-before") . " to " . ($CertVal->"invalid-after") . "\n" . \
 | 
						|
        "Expires in:  " . $ExpiresAfter);
 | 
						|
    $LogPrintExit warning ("The certificate " . ($CertVal->"name") . " " . $State . \
 | 
						|
        ", it is invalid after " . ($CertVal->"invalid-after") . ".") false;
 | 
						|
  }
 | 
						|
}
 |