Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checksmtp functionality for SMTP configuration verification #231

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
55 changes: 55 additions & 0 deletions bitwarden.sh
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,57 @@ function compressLogs() {
rm $tempfile
}

function checkSmtp() {
CONFIG_FILE="$1/env/global.override.env"

if [ ! -f "$CONFIG_FILE" ]; then
echo "Configuration file not found at $CONFIG_FILE"
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)
username=$(grep 'globalSettings__mail__smtp__username=' "$CONFIG_FILE" | cut -d '=' -f2)
password=$(grep 'globalSettings__mail__smtp__password=' "$CONFIG_FILE" | cut -d '=' -f2)

if [ "$ssl" == "true" ]; then
ssl_command="-ssl"
else
ssl_command="-starttls smtp"
fi

set +e
SMTP_RESPONSE=$(
{
echo "EHLO localhost"
if [ "$ssl_command" != "-ssl" ]; then
echo "STARTTLS"
sleep 2
echo "EHLO localhost"
fi
# 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
echo -e "SMTP settings are correct."
else
echo "SMTP authentication failed or connection error occurred."
fi
}

function listCommands() {
cat << EOT
Available commands:
Expand Down Expand Up @@ -267,6 +318,10 @@ case $1 in
checkOutputDirExists
compressLogs $OUTPUT $2 $3
;;
"checksmtp")
checkOutputDirExists
checkSmtp $OUTPUT
;;
"help")
listCommands
;;
Expand Down
Loading