From 52386f60f939ecc7d7ed2efbf01061f0e5976967 Mon Sep 17 00:00:00 2001 From: fer Date: Sun, 10 Mar 2024 11:28:41 +0100 Subject: [PATCH 1/6] checksmtp implementation --- bitwarden.sh | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/bitwarden.sh b/bitwarden.sh index cb9b317a..71044fd0 100755 --- a/bitwarden.sh +++ b/bitwarden.sh @@ -123,6 +123,53 @@ function checkOutputDirNotExists() { fi } +function checkSmtp() { + CONFIG_FILE="$1/env/global.override.env" + + # Check if CONFIG_FILE exists + if [ ! -f "$CONFIG_FILE" ]; then + echo "Configuration file not found at $CONFIG_FILE" + exit 1 + fi + + # Extract SMTP settings + host=$(grep 'globalSettings__mail__smtp__host=' "$CONFIG_FILE" | cut -d '=' -f2) + port=$(grep 'globalSettings__mail__smtp__port=' "$CONFIG_FILE" | cut -d '=' -f2) + ssl=$(grep 'globalSettings__mail__smtp__ssl=' "$CONFIG_FILE" | cut -d '=' -f2) + username=$(grep 'globalSettings__mail__smtp__username=' "$CONFIG_FILE" | cut -d '=' -f2) + password=$(grep 'globalSettings__mail__smtp__password=' "$CONFIG_FILE" | cut -d '=' -f2) + + # Determine if SSL is required + if [ "$ssl" == "true" ]; then + ssl_command="-ssl" + else + ssl_command="-starttls smtp" + fi + + # Perform the SMTP check and capture the output + SMTP_RESPONSE=$( + { + echo "EHLO localhost" + if [ "$ssl_command" != "-ssl" ]; then + echo "STARTTLS" + sleep 2 + echo "EHLO localhost" + fi + echo "AUTH LOGIN" + echo "$(echo -ne "$username" | base64)" + echo "$(echo -ne "$password" | base64)" + echo "QUIT" + } | openssl s_client -connect $host:$port $ssl_command -ign_eof 2>/dev/null + ) + + # Check for successful authentication in the SMTP response + if echo "$SMTP_RESPONSE" | grep -q "235 "; then + echo -e "SMTP settings are correct." + else + echo "SMTP authentication failed or connection error occurred." + fi +} + function listCommands() { cat << EOT Available commands: @@ -195,6 +242,9 @@ case $1 in checkOutputDirExists $SCRIPTS_DIR/run.sh uninstall $OUTPUT ;; + "checksmtp") + checkSmtp $OUTPUT + ;; "help") listCommands ;; From e5272d17045fa10a92ff2316555aa97a0ca65e1c Mon Sep 17 00:00:00 2001 From: fer Date: Sun, 10 Mar 2024 11:40:13 +0100 Subject: [PATCH 2/6] checkOutputDirExists before check is invoked --- bitwarden.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/bitwarden.sh b/bitwarden.sh index 71044fd0..68bd9ee6 100755 --- a/bitwarden.sh +++ b/bitwarden.sh @@ -243,6 +243,7 @@ case $1 in $SCRIPTS_DIR/run.sh uninstall $OUTPUT ;; "checksmtp") + checkOutputDirExists checkSmtp $OUTPUT ;; "help") From f2accc740aaab6a57a51a4504ae449ca86109597 Mon Sep 17 00:00:00 2001 From: fer Date: Sun, 10 Mar 2024 12:15:25 +0100 Subject: [PATCH 3/6] removed comments --- bitwarden.sh | 5 ----- 1 file changed, 5 deletions(-) diff --git a/bitwarden.sh b/bitwarden.sh index 68bd9ee6..fda69193 100755 --- a/bitwarden.sh +++ b/bitwarden.sh @@ -126,27 +126,23 @@ function checkOutputDirNotExists() { function checkSmtp() { CONFIG_FILE="$1/env/global.override.env" - # Check if CONFIG_FILE exists if [ ! -f "$CONFIG_FILE" ]; then echo "Configuration file not found at $CONFIG_FILE" exit 1 fi - # Extract SMTP settings host=$(grep 'globalSettings__mail__smtp__host=' "$CONFIG_FILE" | cut -d '=' -f2) port=$(grep 'globalSettings__mail__smtp__port=' "$CONFIG_FILE" | cut -d '=' -f2) ssl=$(grep 'globalSettings__mail__smtp__ssl=' "$CONFIG_FILE" | cut -d '=' -f2) username=$(grep 'globalSettings__mail__smtp__username=' "$CONFIG_FILE" | cut -d '=' -f2) password=$(grep 'globalSettings__mail__smtp__password=' "$CONFIG_FILE" | cut -d '=' -f2) - # Determine if SSL is required if [ "$ssl" == "true" ]; then ssl_command="-ssl" else ssl_command="-starttls smtp" fi - # Perform the SMTP check and capture the output SMTP_RESPONSE=$( { echo "EHLO localhost" @@ -162,7 +158,6 @@ function checkSmtp() { } | openssl s_client -connect $host:$port $ssl_command -ign_eof 2>/dev/null ) - # Check for successful authentication in the SMTP response if echo "$SMTP_RESPONSE" | grep -q "235 "; then echo -e "SMTP settings are correct." else From e3351fde82285366d02a1bf2af244f772f6066b4 Mon Sep 17 00:00:00 2001 From: fer Date: Mon, 18 Mar 2024 16:34:44 +0100 Subject: [PATCH 4/6] openssl check --- bitwarden.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bitwarden.sh b/bitwarden.sh index 40792c7c..91b70255 100755 --- a/bitwarden.sh +++ b/bitwarden.sh @@ -191,6 +191,11 @@ function checkSmtp() { exit 1 fi + if ! command -v openssl &> /dev/null; then + echo "OpenSSL is not available but is required for this check." + exit 1 + fi + host=$(grep 'globalSettings__mail__smtp__host=' "$CONFIG_FILE" | cut -d '=' -f2) port=$(grep 'globalSettings__mail__smtp__port=' "$CONFIG_FILE" | cut -d '=' -f2) ssl=$(grep 'globalSettings__mail__smtp__ssl=' "$CONFIG_FILE" | cut -d '=' -f2) @@ -303,7 +308,7 @@ case $1 in compressLogs $OUTPUT $2 $3 ;; "checksmtp") - checkOutputDirExists + # checkOutputDirExists checkSmtp $OUTPUT ;; "help") From 9acddea06e331f37b7d60321292ae2ef9165b981 Mon Sep 17 00:00:00 2001 From: fer Date: Mon, 18 Mar 2024 16:35:22 +0100 Subject: [PATCH 5/6] 2xx code regex check --- bitwarden.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitwarden.sh b/bitwarden.sh index 91b70255..41ff1728 100755 --- a/bitwarden.sh +++ b/bitwarden.sh @@ -223,7 +223,7 @@ function checkSmtp() { } | openssl s_client -connect $host:$port $ssl_command -ign_eof 2>/dev/null ) - if echo "$SMTP_RESPONSE" | grep -q "235 "; then + if echo "$SMTP_RESPONSE" | grep -q "^2[0-9][0-9] "; then echo -e "SMTP settings are correct." else echo "SMTP authentication failed or connection error occurred." From d121e3a55c18cb74a5daedead9731a8a7caf1c7d Mon Sep 17 00:00:00 2001 From: fer Date: Mon, 18 Mar 2024 16:43:37 +0100 Subject: [PATCH 6/6] only AUTH section if u/p exist --- bitwarden.sh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/bitwarden.sh b/bitwarden.sh index 41ff1728..ac8427a9 100755 --- a/bitwarden.sh +++ b/bitwarden.sh @@ -216,14 +216,17 @@ function checkSmtp() { sleep 2 echo "EHLO localhost" fi - echo "AUTH LOGIN" - echo "$(echo -ne "$username" | base64)" - echo "$(echo -ne "$password" | base64)" + # Check if username and password are set before proceeding + if [[ -n "$username" && -n "$password" ]]; then + echo "AUTH LOGIN" + echo "$(echo -ne "$username" | base64)" + echo "$(echo -ne "$password" | base64)" + fi echo "QUIT" } | openssl s_client -connect $host:$port $ssl_command -ign_eof 2>/dev/null ) - if echo "$SMTP_RESPONSE" | grep -q "^2[0-9][0-9] "; then + if echo "$SMTP_RESPONSE" | grep -q "^2[0-9][0-9] "; then echo -e "SMTP settings are correct." else echo "SMTP authentication failed or connection error occurred."