v1.0.0
This commit is contained in:
parent
08b7356179
commit
ed2bbbbe0c
40
README.md
40
README.md
@ -1,21 +1,37 @@
|
|||||||
# Project Name
|
# Project Name
|
||||||
TODO: Write a project description
|
This script checks the validity of a certificate and renews it if necessary
|
||||||
## Requirements
|
## Requirements
|
||||||
TODO: Requirments
|
A pki on <pki_path>/\<domain>
|
||||||
## Installation
|
## Installation
|
||||||
TODO: Describe the installation process
|
```bash
|
||||||
|
sudo ./install.sh
|
||||||
|
```
|
||||||
|
A cron file is created
|
||||||
|
## Parameters
|
||||||
|
See `/etc/renew-cert/renew-cert.conf`
|
||||||
## Usage
|
## Usage
|
||||||
TODO: Write usage instructions
|
```bash
|
||||||
|
./renew-cert.sh --help
|
||||||
|
```
|
||||||
|
```bash
|
||||||
|
Usage: ./renew-cert.sh options
|
||||||
|
Renew wilcard certificat
|
||||||
|
Options:
|
||||||
|
-d, --domain - domain name
|
||||||
|
-p, --param - display parameters
|
||||||
|
-h, --help - display this help
|
||||||
|
-v, --version - display script version
|
||||||
|
|
||||||
|
Example
|
||||||
|
Renew wildcard certificat for domain gmolab.net
|
||||||
|
./renew-cert.sh --domain gmolab.net
|
||||||
|
```
|
||||||
|
|
||||||
### Changelog
|
### Changelog
|
||||||
### [1.0.0] - 2025-09-06
|
### [1.0.0] - 2025-09-11
|
||||||
#### Added
|
#### Added
|
||||||
- New features and functionality.
|
- check expiration date certificate
|
||||||
#### Modified
|
- renew certification
|
||||||
- Changes to existing functionality (backwards compatible).
|
- send mail
|
||||||
#### Fixed
|
|
||||||
- Bug fixes.
|
|
||||||
#### Removed
|
|
||||||
- Deprecated or removed features (breaking changes).
|
|
||||||
#### Project initialization
|
#### Project initialization
|
||||||
- initialization by [GMo](mailto:gilles.mouchet@gmail.com)
|
- initialization by [GMo](mailto:gilles.mouchet@gmail.com)
|
||||||
|
|||||||
59
install.sh
59
install.sh
@ -37,17 +37,21 @@ installPackage() {
|
|||||||
#-----------------------------------------------------------
|
#-----------------------------------------------------------
|
||||||
# variables
|
# variables
|
||||||
|
|
||||||
|
user=gilles
|
||||||
|
group=gilles
|
||||||
fullScriptName=renew-cert.sh
|
fullScriptName=renew-cert.sh
|
||||||
shortScriptName=`echo $fullScriptName | sed -e 's|.*/||g' | cut -f1 -d.`
|
shortScriptName=`echo $fullScriptName | sed -e 's|.*/||g' | cut -f1 -d.`
|
||||||
destPath=/usr/local/bin/
|
destPath=/usr/local/bin/gmotools
|
||||||
configFile=$shortScriptName.conf
|
configFile=$shortScriptName.conf
|
||||||
configFilePath=/etc/$shortScriptName/
|
configFilePath=/etc/$shortScriptName/
|
||||||
logRotateFile=$shortScriptName
|
logRotateFile=$shortScriptName
|
||||||
logRotateFilePath=/etc/logrotate.d/$logRotateFile
|
logRotateFilePath=/etc/logrotate.d/$logRotateFile
|
||||||
|
cronFile=$shortScriptName.cron
|
||||||
|
|
||||||
|
|
||||||
# check if the effective user ID is 0 (root)
|
# check if the effective user ID is 0 (root)
|
||||||
if [[ $EUID -ne 0 ]]; then
|
if [[ $EUID -ne 0 ]]; then
|
||||||
echo "This script must be run as root or with sudo."
|
echo "[ERROR] - this script must be run as root or with sudo."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -58,18 +62,18 @@ if command -v apt &> /dev/null; then
|
|||||||
elif command -v dnf &> /dev/null; then
|
elif command -v dnf &> /dev/null; then
|
||||||
packageManager="dnf"
|
packageManager="dnf"
|
||||||
else
|
else
|
||||||
echo "Erreur : No supported package managers (apt, dnf) were found."
|
echo "[ERROR] - no supported package managers (apt, dnf) were found."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "Package manager detected: $packageManager"
|
echo "[INFO] - package manager detected: $packageManager"
|
||||||
|
|
||||||
# check if logPath exist
|
# check if logPath exist
|
||||||
if [ ! -d $configFilePath ]; then
|
if [ ! -d $configFilePath ]; then
|
||||||
mkdir $configFilePath &> /dev/null
|
mkdir $configFilePath &> /dev/null
|
||||||
rc=$?
|
rc=$?
|
||||||
if [ "$rc" != "0" ];then
|
if [ "$rc" != "0" ];then
|
||||||
echo "[ERROR] - An error occurred while creating $configFilePath ($rc)"
|
echo "[ERROR] - an error occurred while creating $configFilePath ($rc)"
|
||||||
else echo "[SUCCESS] - The folder $configFilePath was created successfully."
|
else echo "[SUCCESS] - the folder $configFilePath was created successfully."
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
# install package (example)
|
# install package (example)
|
||||||
@ -79,22 +83,51 @@ installPackage mutt &> /dev/null
|
|||||||
cp $fullScriptName $destPath/. &> /dev/null
|
cp $fullScriptName $destPath/. &> /dev/null
|
||||||
rc=$?
|
rc=$?
|
||||||
if [ "$rc" != "0" ];then
|
if [ "$rc" != "0" ];then
|
||||||
echo "[ERROR] - An error occurred while copying $fullScriptName to $destPath ($rc)"
|
echo "[ERROR] - an error occurred while copying $fullScriptName to $destPath ($rc)"
|
||||||
else echo "[SUCCESS] - The script ${fullScriptName} to $destPath was copied successfully."
|
exit 1
|
||||||
|
else echo "[SUCCESS] - the script ${fullScriptName} to $destPath was copied successfully."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# copy config file
|
# copy config file
|
||||||
cp $configFile $configFilePath &> /dev/null
|
cp $configFile $configFilePath &> /dev/null
|
||||||
rc=$?
|
rc=$?
|
||||||
if [ "$rc" != "0" ];then
|
if [ "$rc" != "0" ];then
|
||||||
echo "[ERROR] - An error occurred while copying $configFile to $configFilePath ($rc)"
|
echo "[ERROR] - an error occurred while copying $configFile to $configFilePath ($rc)"
|
||||||
else echo "[SUCCESS] - The script $configFile to $configFilepath was copied successfully."
|
exit 1
|
||||||
|
else echo "[SUCCESS] - the script $configFile to $configFilepath was copied successfully."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cp $logRotateFile.logrotate $logRotateFilePath &> /dev/null
|
cp $logRotateFile.logrotate $logRotateFilePath &> /dev/null
|
||||||
rc=$?
|
rc=$?
|
||||||
if [ "$rc" != "0" ];then
|
if [ "$rc" != "0" ];then
|
||||||
echo "[ERROR] - An error occurred while copying $logRotateFile.logrotate to $logRotateFilePath ($rc)"
|
echo "[ERROR] - an error occurred while copying $logRotateFile.logrotate to $logRotateFilePath ($rc)"
|
||||||
else echo "[SUCCESS] - The script $logRotateFile.logrotate to $logRotateFilePath was copied successfully."
|
exit 1
|
||||||
|
else echo "[SUCCESS] - the script $logRotateFile.logrotate to $logRotateFilePath was copied successfully."
|
||||||
fi
|
fi
|
||||||
echo "Installation completed."
|
|
||||||
|
cp $shortScriptName.cron /etc/cron.d/$shortScriptName &> /dev/null
|
||||||
|
rc=$?
|
||||||
|
if [ "$rc" != "0" ];then
|
||||||
|
echo "[ERROR] - n error occurred while copying $shortScriptName.cron to /etc/cron.d/$shortScriptName ($rc)"
|
||||||
|
exit 1
|
||||||
|
else echo "[SUCCESS] - $shortScriptName.cron to /etc/cron.d/$shortScriptName was copied successfully."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# create log file if not exist
|
||||||
|
if [ ! -d /var/log/$shortScriptName ]; then
|
||||||
|
mkdir /var/log/$shortScriptName &> /dev/null
|
||||||
|
rc=$?
|
||||||
|
if [ "$rc" != "0" ];then
|
||||||
|
echo "[ERROR] - An error occurred while creating /var/log/$shortScriptName ($rc)"
|
||||||
|
exit 1
|
||||||
|
else echo "[SUCCESS] - /var/log/$shortScriptName was created successfully."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
sudo chown $user:$group /var/log/$shortScriptName
|
||||||
|
rc=$?
|
||||||
|
if [ "$rc" != "0" ];then
|
||||||
|
echo "[ERROR] - An error occurred while applying user ($user) and group ($group) owner /var/log/$shortScriptName ($rc)"
|
||||||
|
exit 1
|
||||||
|
else echo "[SUCCESS] - User ($user) and group ($group) owner on /var/log/$shortScriptName was applied successfully."
|
||||||
|
fi
|
||||||
|
echo "Installation completed"
|
||||||
|
|||||||
@ -1,3 +1,47 @@
|
|||||||
|
#onScreen mode - display message log on screen
|
||||||
|
# 0 no display log on screen
|
||||||
|
# 1 display log entry on screen
|
||||||
|
onScreen=0
|
||||||
|
|
||||||
|
# debug mode - display more var value
|
||||||
|
#. 0 disable debug mode
|
||||||
|
# 1 enable debug mode
|
||||||
|
debug=0
|
||||||
|
|
||||||
|
# folder where the root of the domain PKI is located
|
||||||
|
pkiRootPath=/home/gilles/certs
|
||||||
|
|
||||||
|
# credentials file for the domain certificate .key file
|
||||||
|
credFilePath=/home/gilles
|
||||||
|
|
||||||
|
# number of days the certificate is valid upon renewal
|
||||||
|
certDaysValidity=30
|
||||||
|
|
||||||
|
# warning message sent x days
|
||||||
|
# before the end of the certificate validity
|
||||||
|
warningDays=7
|
||||||
|
|
||||||
|
# critical message sent x days
|
||||||
|
# before the end of the certificate validity
|
||||||
|
criticalDays=3
|
||||||
|
|
||||||
|
# certificate renewal x days before
|
||||||
|
# before the end of the certificate validity
|
||||||
|
renewDays=1
|
||||||
|
|
||||||
|
# send warning mail
|
||||||
|
warningMail=1
|
||||||
|
|
||||||
|
# send critical mail
|
||||||
|
criticalMail=1
|
||||||
|
|
||||||
|
# send renewcert mail
|
||||||
|
renewMail=1
|
||||||
|
|
||||||
|
# send info mail
|
||||||
|
#if 1 a message is sent each time the file is executed
|
||||||
|
#even if there is no error
|
||||||
|
infoMail=0
|
||||||
|
|
||||||
# mail recipient
|
# mail recipient
|
||||||
msgRecipient=exploit.gmotech@gmail.com
|
msgRecipient=exploit.gmotech@gmail.com
|
||||||
|
|
||||||
|
|||||||
3
renew-cert.cron
Normal file
3
renew-cert.cron
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# there are no rediect /dev/null. It's normal
|
||||||
|
# if redirect not input in log file
|
||||||
|
01 00 * * * gilles /usr/local/bin/gmotools/renew-cert.sh -d gmolab.net
|
||||||
@ -1,8 +1,8 @@
|
|||||||
/var/log/template/template.log {
|
/var/log/renew-log/renew-log.log {
|
||||||
daily
|
daily
|
||||||
rotate 7
|
rotate 7
|
||||||
compress
|
compress
|
||||||
missingok
|
missingok
|
||||||
notifempty
|
notifempty
|
||||||
create 644 root root
|
create 644 gilles gilles
|
||||||
}
|
}
|
||||||
331
renew-cert.sh
331
renew-cert.sh
@ -1,8 +1,8 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
############################################################
|
############################################################
|
||||||
# Decription: Template script
|
# Decription: Renew certificate
|
||||||
# Author: Gilles Mouchet (gilles.mouchet@gmail.com)
|
# Author: Gilles Mouchet (gilles.mouchet@gmail.com)
|
||||||
# Creation Date: 06-Sep-2025
|
# Creation Date: 11-Sep-2025
|
||||||
# Version: 1.0.0
|
# Version: 1.0.0
|
||||||
#
|
#
|
||||||
# Changelog:
|
# Changelog:
|
||||||
@ -11,16 +11,23 @@
|
|||||||
# - Creation of script from scratch
|
# - Creation of script from scratch
|
||||||
#
|
#
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
#-----------------------------------------------------------------
|
#-----------------------------------------------------------------
|
||||||
# DON'T CHANGE ANYTHING FROM HERE
|
# DON'T CHANGE ANYTHING FROM HERE
|
||||||
#-----------------------------------------------------------------
|
#-----------------------------------------------------------------
|
||||||
|
|
||||||
version="1.0.0"
|
version="1.0.0"
|
||||||
|
# config
|
||||||
|
progName=`echo $0 | sed -e 's|.*/||g' | cut -f1 -d.`
|
||||||
|
confDir=/etc/$progName
|
||||||
|
cfgFile=$confDir/$progName.conf
|
||||||
|
logPath=/var/log/$progName
|
||||||
|
logFile=$logPath/$progName.log
|
||||||
|
# mail
|
||||||
mailSubject="[SUCCESS] - script result on `hostname`"
|
mailSubject="[SUCCESS] - script result on `hostname`"
|
||||||
mailHeader="my_hdr From: GMO Check System <exploit.gmotech@gmail.com>"
|
mailHeader="my_hdr From: GMO Check System <exploit.gmotech@gmail.com>"
|
||||||
mailBody=""
|
mailBody=""
|
||||||
mailFooter="\n\nTemplate script $version by Exploit GMoTech"
|
mailFooter="\n\n$progName script V$version on `hostname` by Exploit GMoTech"
|
||||||
tmpFile=/tmp/list.txt
|
tmpFile=/tmp/$progName.log
|
||||||
|
|
||||||
#-----------------------------------------------------------
|
#-----------------------------------------------------------
|
||||||
# FUNCTIONS
|
# FUNCTIONS
|
||||||
@ -28,14 +35,64 @@ tmpFile=/tmp/list.txt
|
|||||||
function usage() {
|
function usage() {
|
||||||
cat << EOF
|
cat << EOF
|
||||||
Usage: ./$(basename "$0") options
|
Usage: ./$(basename "$0") options
|
||||||
Template script
|
Renew wilcard certificat
|
||||||
Options:
|
Options:
|
||||||
|
-d, --domain - domain name
|
||||||
-p, --param - display parameters
|
-p, --param - display parameters
|
||||||
-h, --help - display this help
|
-h, --help - display this help
|
||||||
-v, --version - display script version
|
-v, --version - display script version
|
||||||
|
|
||||||
|
Example
|
||||||
|
Renew wildcard certificat for domain gmolab.net
|
||||||
|
./$(basename "$0") --domain gmolab.net
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
#-----------------------------------------------------------
|
#-----------------------------------------------------------
|
||||||
|
function renewCertificate(){
|
||||||
|
log I "renew certificate"
|
||||||
|
log D "ca key file use $fullpkiRootPath/$caKeyFile"
|
||||||
|
log D "ca crt file use $fullpkiRootPath/$caCrtFile"
|
||||||
|
log D "ca csr file use $fullpkiRootPath/$certDomain.csr"
|
||||||
|
log D "bakup path file use $fullpkiRootPath/backup/"
|
||||||
|
# backup crt file
|
||||||
|
log I "copy $certFile to $fullpkiRootPath/backup/$certFile"
|
||||||
|
cp $fullpkiRootPath/$certFile $fullpkiRootPath/backup/$certFile-$(date +"%Y-%m-%d-%H-%M-%S") > $tmpFile
|
||||||
|
rc=$?
|
||||||
|
if [ "$rc" != "0" ];then
|
||||||
|
log I "send error message"
|
||||||
|
log E "a problem occurred while copying $certDomain certificate on $fullpkiRootPath/backup/$certFile ($rc)"
|
||||||
|
mailSubject="[ERROR] - a problem occurred while copying $certDomain certificate on $fullpkiRootPath/backup/$certFile ($rc)"
|
||||||
|
mailBody="Bad new!\n A problem occurred while copying $certDomain certificate on $fullpkiRootPath/backup/$certFile ($rc).\nHave a good day"
|
||||||
|
sendMail
|
||||||
|
endScript
|
||||||
|
fi
|
||||||
|
# renewal certificate
|
||||||
|
openssl x509 -req -in $fullpkiRootPath/$certDomain.csr \
|
||||||
|
-passin file:$caKeyCredFile \
|
||||||
|
-CA $fullpkiRootPath/$caCrtFile \
|
||||||
|
-CAkey $fullpkiRootPath/$caKeyFile \
|
||||||
|
-CAcreateserial \
|
||||||
|
-extfile $fullpkiRootPath/wilcard.cnf -out $fullpkiRootPath/$certFile \
|
||||||
|
-days $certDaysValidity -sha256 > $tmpFile 2>&1
|
||||||
|
rc=$?
|
||||||
|
if [ "$rc" != "0" ]; then
|
||||||
|
log I "send error message"
|
||||||
|
log E "a problem occurred while renewing $certDomain certificate ($rc)"
|
||||||
|
mailSubject="[ERROR] - a problem occurred while renewing $certDomain certificate ($rc)"
|
||||||
|
mailBody="Bad new!\n A problem occurred while renewing $certDomain certificate ($rc).\nHave a good day"
|
||||||
|
sendMail
|
||||||
|
rm -rf $tmpFile
|
||||||
|
else
|
||||||
|
endDateFo=$(openssl x509 -in $fullpkiRootPath/$certFile -noout -enddate | awk -F'=' '{print $2}' | xargs -I {} date -d "{}" '+%Y-%m-%d')
|
||||||
|
log I "certificate successfully renewed. New expiry date: $endDateFo"
|
||||||
|
log I "send a success mesaage"
|
||||||
|
mailSubject="[SUCCESS] - certificate for $certDomain successfully renewed. New expiry date: $endDateFo"
|
||||||
|
mailBody="Bad new!\n Certificate or $certDomain successfully renewed. New expiry date: $endDateFo.\nHave a good day"
|
||||||
|
sendMail
|
||||||
|
rm -rf $tmpFile
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
#-----------------------------------------------------------
|
||||||
function sendMail() {
|
function sendMail() {
|
||||||
if [ -f "$tmpFile" ];then
|
if [ -f "$tmpFile" ];then
|
||||||
echo -e "$mailBody $mailFooter" | mutt -e "$mailHeader" -s "${mailSubject}" $msgRecipient -a $tmpFile
|
echo -e "$mailBody $mailFooter" | mutt -e "$mailHeader" -s "${mailSubject}" $msgRecipient -a $tmpFile
|
||||||
@ -48,12 +105,7 @@ function sendMail() {
|
|||||||
# parameter
|
# parameter
|
||||||
# $1 define entry type (info, warning, error)
|
# $1 define entry type (info, warning, error)
|
||||||
# $2 define text
|
# $2 define text
|
||||||
# $3 define display on screen or not (nothing=no, 1=yes)
|
|
||||||
function log() {
|
function log() {
|
||||||
if [ -z "$3" ]; then
|
|
||||||
displayScreen=0
|
|
||||||
else displayScreen=1
|
|
||||||
fi
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
I)
|
I)
|
||||||
logType="[info]"
|
logType="[info]"
|
||||||
@ -64,33 +116,47 @@ function log() {
|
|||||||
E)
|
E)
|
||||||
logType="[error]"
|
logType="[error]"
|
||||||
;;
|
;;
|
||||||
|
C)
|
||||||
|
logType="[critical]"
|
||||||
|
;;
|
||||||
|
D)
|
||||||
|
logType="[debug]"
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
# on screen and logfile
|
|
||||||
#echo "$(date "+%Y-%m-%d")-$(date "+%H:%M:%S") - $logType - $2" | tee -a "$logFile"
|
# write all entries except [debug]
|
||||||
#echo "[$(date "+%Y-%m-%d")-$(date "+%H:%M:%S")] - $logType - $2" >> "$logFile"
|
if [ "$logType" != "[debug]" ]; then
|
||||||
# true to display screen to
|
|
||||||
if [ "${displayScreen}" -eq 1 ];then
|
|
||||||
echo "[$(date "+%Y-%m-%d")-$(date "+%H:%M:%S")] - $logType - $2" | tee -a "$logFile"
|
|
||||||
else
|
|
||||||
echo "[$(date "+%Y-%m-%d")-$(date "+%H:%M:%S")] - $logType - $2" >> "$logFile"
|
echo "[$(date "+%Y-%m-%d")-$(date "+%H:%M:%S")] - $logType - $2" >> "$logFile"
|
||||||
|
# display log entry on screen if onScreen is enabled
|
||||||
|
if [ "$onScreen" -eq 1 ];then
|
||||||
|
echo "[$(date "+%Y-%m-%d")-$(date "+%H:%M:%S")] - $logType - $2"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# write [debug] entries only if debug is enabled
|
||||||
|
if [ "$logType" == "[debug]" ] && [ "$debug" -eq 1 ]; then
|
||||||
|
echo "[$(date "+%Y-%m-%d")-$(date "+%H:%M:%S")] - $logType - $2" >> "$logFile"
|
||||||
|
# display log entry on screen if onScreen is enabled
|
||||||
|
if [ "$onScreen" -eq 1 ];then
|
||||||
|
echo "[$(date "+%Y-%m-%d")-$(date "+%H:%M:%S")] - $logType - $2"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
}
|
||||||
|
#-----------------------------------------------------------
|
||||||
|
function endScript(){
|
||||||
|
log I "----- end script -----"
|
||||||
|
exit
|
||||||
}
|
}
|
||||||
#-----------------------------------------------------------
|
#-----------------------------------------------------------
|
||||||
# MAIN
|
# MAIN
|
||||||
#-----------------------------------------------------------
|
#-----------------------------------------------------------
|
||||||
|
|
||||||
# check if the effective user ID is 0 (root)
|
# check if the effective user ID is 0 (root)
|
||||||
if [[ $EUID -ne 0 ]]; then
|
#if [[ $EUID -ne 0 ]]; then
|
||||||
echo "This script must be run as root or with sudo."
|
# echo "This script must be run as root or with sudo."
|
||||||
exit 1
|
# exit 1
|
||||||
fi
|
#fi
|
||||||
|
|
||||||
# config
|
|
||||||
progName=`echo $0 | sed -e 's|.*/||g' | cut -f1 -d.`
|
|
||||||
confDir=/etc/$progName
|
|
||||||
cfgFile=$confDir/$progName.conf
|
|
||||||
logPath=/var/log/$progName
|
|
||||||
logFile=$logPath/$progName.log
|
|
||||||
|
|
||||||
# check if conf file or passphrase file exist
|
# check if conf file or passphrase file exist
|
||||||
if [ ! -f $cfgFile ]; then
|
if [ ! -f $cfgFile ]; then
|
||||||
@ -98,24 +164,38 @@ if [ ! -f $cfgFile ]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# read config file
|
# read config file
|
||||||
. $cfgFile
|
. $cfgFile
|
||||||
log I "script start" 1
|
|
||||||
|
log I "----- script start -----"
|
||||||
|
|
||||||
# check if logPath exist
|
# check if logPath exist
|
||||||
if [ ! -d $logPath ]; then
|
if [ ! -d $logPath ]; then
|
||||||
mkdir $logPath
|
echo "$progName not installed correctly. Please run install.sh script"
|
||||||
|
endScript
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# check param exist. Uncomment if your script need parameters
|
# check param exist. Uncomment if your script need parameters
|
||||||
#if [ -z "$1" ]; then
|
if [ -z "$1" ]; then
|
||||||
# usage
|
usage
|
||||||
# exit
|
endScript
|
||||||
#fi
|
fi
|
||||||
|
|
||||||
while [[ "$#" -gt 0 ]]; do
|
while [[ "$#" -gt 0 ]]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
|
-d|--domain)
|
||||||
|
if [ -z "$2" ]; then
|
||||||
|
usage
|
||||||
|
endScript
|
||||||
|
else
|
||||||
|
certFile=$2.crt
|
||||||
|
certDomain=$2
|
||||||
|
log I "cert file $certFile"
|
||||||
|
fi
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
-p|--param)
|
-p|--param)
|
||||||
|
log I "script run with -p or --param option"
|
||||||
cat << EOF
|
cat << EOF
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
Parameters
|
Parameters
|
||||||
@ -130,46 +210,175 @@ Defined in script
|
|||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
Defined in $cfgFile
|
Defined in $cfgFile
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
message recipient: $msgRecipient
|
message recipient: $msgRecipient
|
||||||
|
pki path: $pkiRootPath
|
||||||
|
display log on screen: $onScreen
|
||||||
|
debug mode: $debug
|
||||||
|
|
||||||
|
send warning mail
|
||||||
|
x days before
|
||||||
|
the end of validity: $warningDays
|
||||||
|
|
||||||
|
send critical mail
|
||||||
|
x days before
|
||||||
|
the end of validity: $criticalDays
|
||||||
|
|
||||||
|
certificate renewal
|
||||||
|
x days before
|
||||||
|
before the end of
|
||||||
|
the certificate validity: $renewDays
|
||||||
|
|
||||||
|
number of days
|
||||||
|
the certificate
|
||||||
|
is valid upon renewal: $certDaysValidity
|
||||||
|
|
||||||
|
sending an infor email: $infoMail
|
||||||
|
sending a warning email: $warningMail
|
||||||
|
sending a critical email: $criticalMail
|
||||||
|
sending a renewal email: $renewMail
|
||||||
EOF
|
EOF
|
||||||
exit
|
endScript
|
||||||
;;
|
;;
|
||||||
version|-v|--version)
|
-v|--version)
|
||||||
|
log I "script run with -v or --verion option"
|
||||||
cat << EOF
|
cat << EOF
|
||||||
$(basename "$0") v$version (c) 1990 - $(date +%Y) by Gilles Mouchet
|
$(basename "$0") v$version (c) 1990 - $(date +%Y) by Gilles Mouchet
|
||||||
Non-Commercial Use License – See LICENSE for details
|
Non-Commercial Use License – See LICENSE for details
|
||||||
EOF
|
EOF
|
||||||
exit
|
endScript
|
||||||
;;
|
;;
|
||||||
# must be in the last block of the case because of *
|
# must be in the last block of the case because of *
|
||||||
*|help|-h|--help)
|
*|help|-h|--help)
|
||||||
|
log I "script run with -h, --help option"
|
||||||
usage
|
usage
|
||||||
exit
|
endScript
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
|
||||||
# success message
|
# check if pki folder and pki files for domain
|
||||||
log I "send a success message" 1
|
fullpkiRootPath="$pkiRootPath/`echo $certFile | sed -e 's|.*/||g' | cut -f1 -d.`"
|
||||||
mailSubject="[SUCCESS] - script result on `hostname`"
|
|
||||||
mailBody=" This is a success test mail\nHave a good day"
|
|
||||||
sendMail
|
|
||||||
|
|
||||||
# warnig message
|
# check if backupcrtpath exist
|
||||||
log W "send a warning message" 1
|
if [ ! -d "$fullpkiRootPath/backup" ]; then
|
||||||
mailSubject="[WARNING] - script result on `hostname`"
|
mkdir $fullpkiRootPath/backup
|
||||||
mailBody=" This is a warning test mail\nHave a good day"
|
fi
|
||||||
sendMail
|
|
||||||
|
|
||||||
# error message
|
# set credFile and check if exist
|
||||||
log E "send an error message" 1
|
caKeyCredFile=$credFilePath/.$certDomain
|
||||||
cat << EOF > $tmpFile
|
if [ ! -e "$caKeyCredFile" ]; then
|
||||||
This file contain the description error
|
log E "$caKeyCredFile doesn't exist !"
|
||||||
or log file
|
log I "send error message"
|
||||||
EOF
|
mailSubject="[ERROR] - $caKeyCredFile doesn't exist !"
|
||||||
mailSubject="[ERROR] - script result on `hostname`"
|
mailBody="Oups !\n$caKeyCredFile doesn't exist !.\nHave a good day"
|
||||||
mailBody=" This is a warning test mail\nHave a good day"
|
sendMail
|
||||||
sendMail
|
endScript
|
||||||
rm -rf $tmpFile
|
fi
|
||||||
|
log I "cred file $caKeyCredFile"
|
||||||
|
|
||||||
|
# set CA file name
|
||||||
|
caCrtFile="`echo $certFile | sed -e 's|.*/||g' | cut -f1 -d.`CA.crt"
|
||||||
|
caKeyFile="`echo $certFile | sed -e 's|.*/||g' | cut -f1 -d.`CA.key"
|
||||||
|
log I "ca files $caCrtFile and $caKeyFile are used"
|
||||||
|
# check if cert exist
|
||||||
|
if [ ! -f "$fullpkiRootPath/$certFile" ] || [ ! -f "$fullpkiRootPath/$caCrtFile" ] || [ ! -f "$fullpkiRootPath/$caKeyFile" ]; then
|
||||||
|
log E "$certFile, $caCrtFile or $caKeyFile on $fullpkiRootPath doesn't exist."
|
||||||
|
log I "send error message"
|
||||||
|
mailSubject="[ERROR] - $certFile, $caCrtFile or $caKeyFile on $fullpkiRootPath doesn't exist"
|
||||||
|
mailBody="Problem !\nT$certFile, $caCrtFile or $caKeyFile on $fullpkiRootPath doesn't exist.\nHave a good day"
|
||||||
|
sendMail
|
||||||
|
endScript
|
||||||
|
fi
|
||||||
|
log I "all files are present on $fullpkiRootPath"
|
||||||
|
|
||||||
|
# set end cert expiration date an convert in timestamp
|
||||||
|
endDateFo=$(openssl x509 -in $fullpkiRootPath/$certFile -noout -enddate | awk -F'=' '{print $2}' | xargs -I {} date -d "{}" '+%Y-%m-%d')
|
||||||
|
endDateTs=$(date -d "$endDateFo" +%s)
|
||||||
|
|
||||||
|
# set current date and convert it in timestamp
|
||||||
|
currentDateFo=$(date "+%Y-%m-%d")
|
||||||
|
currentDateTs=$(date -d "$currentDateFo" +%s)
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------------------------------------------------------
|
||||||
|
#FOR DEV
|
||||||
|
# set debug=1 and onScreen=1 in /etc/renew-cert/renew-cert.conf
|
||||||
|
#endDateFo="2025-11-14"
|
||||||
|
#endDateTs=$(date -d "$endDateFo" +%s)
|
||||||
|
#currentDateFo="2025-10-10"
|
||||||
|
#currentDateTs=$(date -d "$currentDateFo" +%s)
|
||||||
|
# END FOR DEV
|
||||||
|
#--------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# set the low and the hight threshold for warning message
|
||||||
|
warningMsgLowDateTs=$((endDateTs - (warningDays * 86400)))
|
||||||
|
warningMsgLowDateFo=$(date -d "@$warningMsgLowDateTs" "+%Y-%m-%d")
|
||||||
|
warningMsgHighDateTs=$((endDateTs - (criticalDays * 86400)- 86400))
|
||||||
|
warningMsgHighDateFo=$(date -d "@$warningMsgHighDateTs" "+%Y-%m-%d")
|
||||||
|
|
||||||
|
# set the low and the hight threshold for critical message
|
||||||
|
criticalMsgLowDateTs=$((endDateTs - (criticalDays * 86400)))
|
||||||
|
criticalMsgLowDateFo=$(date -d "@$criticalMsgLowDateTs" "+%Y-%m-%d")
|
||||||
|
criticalMsgHighDateTs=$((endDateTs - (renewDays * 86400)))
|
||||||
|
criticalMsgHighDateFo=$(date -d "@$criticalMsgHighDateTs" "+%Y-%m-%d")
|
||||||
|
|
||||||
|
# set the renewal dat
|
||||||
|
tmpNbrDaysTd=$((renewDays * 86400 ))
|
||||||
|
renewDateTs=$((endDateTs-tmpNbrDaysTd))
|
||||||
|
renewDateFo=$(date -d "@$renewDateTs" "+%Y-%m-%d")
|
||||||
|
|
||||||
|
# set number days before end today and expiration date
|
||||||
|
daysLeftTs=$((endDateTs-currentDateTs))
|
||||||
|
daysLeftFo=$((daysLeftTs / 86400))
|
||||||
|
|
||||||
|
daysPassTs=$((currentDateTs-endDateTs))
|
||||||
|
daysPassFo=$((daysPassTs / 86400))
|
||||||
|
|
||||||
|
log D "current date : $currentDateFo ($currentDateTs)" $onScreen
|
||||||
|
log D "certificate expiration date : $endDateFo ($endDateTs)" $onScreen
|
||||||
|
log D "nbr. of days before cert expires : $daysLeftFo ($daysLeftTs)" $onScreen
|
||||||
|
log D "warning mail will be send from $warningMsgLowDateFo ($warningMsgLowDateTs) to $warningMsgHighDateFo ($warningMsgHighDateTs)" $onScreen
|
||||||
|
log D "critical mail will be send from $criticalMsgLowDateFo ($criticalMsgLowDateTs) to $criticalMsgHighDateFo ($criticalMsgHighDateTs)" $onScreen
|
||||||
|
log D "certificate renew date to $renewDateFo ($renewDateTs)" $onScreen
|
||||||
|
|
||||||
|
# actions to do based on the number of days remaining
|
||||||
|
# Warning
|
||||||
|
if [ "$daysLeftFo" -le "$warningDays" ] && [ "$daysLeftFo" -gt "$criticalDays" ]; then
|
||||||
|
log W "the $certDomain certificate will expire in $daysLeftFo days ($endDateFo)."
|
||||||
|
if [ "$warningMail" -eq 1 ]; then
|
||||||
|
log I "send warning message"
|
||||||
|
mailSubject="[WARNING] - the $certDomain certificate will expire in $daysLeftFo days ($endDateFo)."
|
||||||
|
mailBody="Demands your attention!\nThe $certDomain certificate will expire in $daysLeftFo days ($endDateFo).\nHave a good day"
|
||||||
|
sendMail
|
||||||
|
fi
|
||||||
|
# critical
|
||||||
|
elif [ "$daysLeftFo" -le "$criticalDays" ] && [ "$daysLeftFo" -gt "$renewDays" ]; then
|
||||||
|
log C "the $certDomain certificate will expire in $daysLeftFo days ($endDateFo). Urgent renewal required."
|
||||||
|
if [ "$criticalMail" -eq 1 ]; then
|
||||||
|
log I "send critical message"
|
||||||
|
mailSubject="[CRITICAL] - the $certDomain certificate will expire in $daysLeftFo days ($endDateFo). Urgent renewal required."
|
||||||
|
mailBody="ATTENTION!\nThe $certDomain certificate will expire in $daysLeftFo days ($endDateFo). Urgent renewal required.\nHave a good day"
|
||||||
|
sendMail
|
||||||
|
fi
|
||||||
|
# renew
|
||||||
|
elif [ "$daysLeftFo" -eq "$renewDays" ]; then
|
||||||
|
log C "the '$certDomain' certificate expires in $daysLeftFo ($endDateFo) day. Renew immediately!"
|
||||||
|
renewCertificate
|
||||||
|
# expired
|
||||||
|
elif [ "$daysLeftFo" -le 0 ]; then
|
||||||
|
log C "the '$certDomain' certificate expired $daysPassFo ($endDateFo) days ago."
|
||||||
|
log I "send error message"
|
||||||
|
mailSubject="[CRITICAL] - the '$certDomain' certificate expired $daysPassFo ($endDateFo) days ago."
|
||||||
|
mailBody="Bad new !\nThe '$certDomain' certificate expired $daysPassFo ($endDateFo) days ago. \nHave a good day"
|
||||||
|
sendMail
|
||||||
|
else
|
||||||
|
# certificat ok
|
||||||
|
log I "the '$certDomain' certificate is valid. There are $daysLeftFo days left before expiration ($endDateFo)."
|
||||||
|
if [ "$infoMail" -eq 1 ]; then
|
||||||
|
log I "send info message"
|
||||||
|
mailSubject="[INFO] - the '$certDomain' certificate is valid. There are $daysLeftFo days left before expiration ($endDateFo)."
|
||||||
|
mailBody="All is ok!\nThe '$certDomain' certificate is valid. There are $daysLeftFo days left before expiration ($endDateFo).\nHave a good day"
|
||||||
|
sendMail
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
log I "----- end script -----"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user