Skip to content

Commit

Permalink
Merge pull request #13 from ADORSYS-GIS/develop
Browse files Browse the repository at this point in the history
feat: add uninstall script
  • Loading branch information
Calebasah authored Dec 19, 2024
2 parents bd2b5ae + 03be316 commit 6fd1831
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 0 deletions.
45 changes: 45 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,51 @@ maybe_sudo() {
fi
}

# Function to validate the installation and configuration
validate_installation() {
info_message "Validating the installation..."

# Check if Snort is installed (Linux)
if [[ $OS_NAME == "Linux" ]]; then
if ! command -v snort &>/dev/null; then
error_message "Snort is not installed on this system. Please install it and rerun the script."
exit 1
else
success_message "Snort is installed on Linux."
fi
fi

# Check if Snort is installed (macOS)
if [[ $OS_NAME == "Darwin" ]]; then
if ! command -v snort &>/dev/null; then
error_message "Snort is not installed on this system. Please install it and rerun the script."
exit 1
else
success_message "Snort is installed on macOS."
fi
fi

# Validate Snort rules and directories
if [[ ! -d "/usr/local/etc/rules" ]] || [[ ! -f "/usr/local/etc/rules/local.rules" ]]; then
warn_message "Snort rules or directories are missing. Please check the configuration."
else
success_message "Snort rules and directories are properly configured."
fi

# Validate logging configuration for Snort
if [[ $OS_NAME == "Darwin" && ! -f "$SNORT_CONF_PATH" ]]; then
error_message "Snort configuration file not found at $SNORT_CONF_PATH. Please ensure Snort is installed properly."
exit 1
elif [[ $OS_NAME == "Linux" && ! -f "/etc/snort/snort.conf" ]]; then
error_message "Snort configuration file not found at /etc/snort/snort.conf. Please ensure Snort is installed properly."
exit 1
else
success_message "Snort configuration file is present."
fi

success_message "Validation completed successfully."
}

# Main logic: install Snort based on the operating system
case "$OS_NAME" in
Linux)
Expand Down
169 changes: 169 additions & 0 deletions scripts/uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#!/bin/bash

# Check if we're running in bash; if not, adjust behavior
if [ -n "$BASH_VERSION" ]; then
set -euo pipefail
else
set -eu
fi

# Determine OS-specific paths
OS_NAME=$(uname)
if [[ $OS_NAME == "Linux" ]]; then
OSSEC_CONF_PATH="/var/ossec/etc/ossec.conf"
elif [[ $OS_NAME == "Darwin" ]]; then
OSSEC_CONF_PATH="/Library/Ossec/etc/ossec.conf"
else
error_message "Unsupported operating system."
exit 1
fi

# Define text formatting
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
BOLD='\033[1m'
NORMAL='\033[0m'

# Function for logging with timestamp
log() {
local LEVEL="$1"
shift
local MESSAGE="$*"
local TIMESTAMP
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
echo -e "${TIMESTAMP} ${LEVEL} ${MESSAGE}"
}

# Logging helpers
info_message() {
log "${BLUE}${BOLD}[INFO]${NORMAL}" "$*"
}

warn_message() {
log "${YELLOW}${BOLD}[WARNING]${NORMAL}" "$*"
}

error_message() {
log "${RED}${BOLD}[ERROR]${NORMAL}" "$*"
}

success_message() {
log "${GREEN}${BOLD}[SUCCESS]${NORMAL}" "$*"
}

# Check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}

# Ensure root privileges, either directly or through sudo
maybe_sudo() {
if [ "$(id -u)" -ne 0 ]; then
if command_exists sudo; then
sudo "$@"
else
error_message "This script requires root privileges. Please run with sudo or as root."
exit 1
fi
else
"$@"
fi
}

sed_alternative() {
if command_exists gsed; then
maybe_sudo gsed "$@"
else
maybe_sudo sed "$@"
fi
}

# Function to remove directories and files
remove_snort_dirs_files() {
local dirs=("$@")
for dir in "${dirs[@]}"; do
if [ -d "$dir" ]; then
maybe_sudo rm -rf "$dir"
info_message "Removed directory $dir"
fi
done
}

remove_snort_files() {
local files=("$@")
for file in "${files[@]}"; do
if [ -f "$file" ]; then
maybe_sudo rm -f "$file"
info_message "Removed file $file"
fi
done
}

# Function to revert changes in ossec.conf
revert_ossec_conf() {
local ossec_conf="$1"
local snort_tag="<!-- snort -->"

if maybe_sudo [ -f "$ossec_conf" ]; then
if maybe_sudo grep -q "$snort_tag" "$ossec_conf"; then
sed_alternative -i "/$snort_tag/,/<\/localfile>/d" "$ossec_conf"
info_message "Reverted changes in $ossec_conf"
else
info_message "No Snort-related changes found in $ossec_conf. Skipping"
fi
else
warn_message "The file $ossec_conf no longer exists. Skipping"
fi
}

# Function to uninstall Snort on macOS
uninstall_snort_macos() {
info_message "Uninstalling Snort on macOS"
brew uninstall snort || warn_message "Snort was not installed via Homebrew."

remove_snort_dirs_files \
"/usr/local/etc/rules" \
"/usr/local/etc/so_rules" \
"/usr/local/etc/lists" \
"/var/log/snort"

remove_snort_files \
"/usr/local/etc/rules/local.rules" \
"/usr/local/etc/lists/default.blocklist"

revert_ossec_conf "$OSSEC_CONF_PATH"
success_message "Snort uninstalled on macOS"
}

# Function to uninstall Snort on Linux
uninstall_snort_linux() {
info_message "Uninstalling Snort on Linux"
if command -v apt >/dev/null 2>&1; then
sudo apt-get purge -y snort && sudo apt-get autoremove -y
else
warn_message "This script supports only Debian-based systems for uninstallation."
fi

remove_snort_dirs_files \
"/etc/snort/" \
"/var/log/snort"

revert_ossec_conf "$OSSEC_CONF_PATH"
success_message "Snort uninstalled on Linux"
}

# Main logic: uninstall Snort based on the operating system
case "$OS_NAME" in
Linux)
uninstall_snort_linux
;;
Darwin)
uninstall_snort_macos
;;
*)
error_message "Unsupported OS: $OS_NAME"
exit 1
;;
esac

0 comments on commit 6fd1831

Please sign in to comment.