This commit is contained in:
Brian Zalewski 2024-05-13 04:01:29 +00:00
parent 6b201206dc
commit 6b857207a0
2 changed files with 44 additions and 49 deletions

View file

@ -11,42 +11,35 @@
#
# * [`fail2ban` configuration folder](https://github.com/megabyte-labs/install.doctor/tree/master/home/private_dot_ssh/fail2ban)
### Notify of script start
logg info 'Configuring fail2ban'
### Restart fail2ban
function restartFailToBan() {
if [ -d /Applications ] && [ -d /System ]; then
### macOS
logg info 'Enabling the fail2ban Homebrew service'
brew services restart fail2ban
if command -v fail2ban-client > /dev/null; then
if [[ ! "$(test -d /proc && grep Microsoft /proc/version > /dev/null)" ]]; then
if [ -f "$HOME/.ssh/fail2ban/jail.local" ]; then
### Linux
FAIL2BAN_CONFIG=/etc/fail2ban
if [ -d /Applications ] && [ -d /System ]; then
### macOS
FAIL2BAN_CONFIG=/usr/local/etc/fail2ban
fi
sudo mkdir -p "$FAIL2BAN_CONFIG"
sudo cp -f "$HOME/.ssh/fail2ban/jail.local" "$FAIL2BAN_CONFIG/jail.local"
if [ -d "${HOMEBREW_PREFIX:-/opt/homebrew}/etc/fail2ban" ] && [ ! -f "${HOMEBREW_PREFIX:-/opt/homebrew}/etc/fail2ban/jail.local" ]; then
logg info "Symlinking $FAIL2BAN_CONFIG/jail.local to ${HOMEBREW_PREFIX:-/opt/homebrew}/etc/fail2ban/jail.local"
ln -s "$FAIL2BAN_CONFIG/jail.local" "${HOMEBREW_PREFIX:-/opt/homebrew}/etc/fail2ban/jail.local"
fi
if [ -d /Applications ] && [ -d /System ]; then
### macOS
logg info 'Enabling the fail2ban Homebrew service' && brew services restart fail2ban
else
### Linux
logg info 'Enabling the fail2ban service' && sudo systemctl enable fail2ban
logg info 'Restarting the fail2ban service' && sudo systemctl restart fail2ban
fi
else
logg info "The $HOME/.ssh/fail2ban/jail.local configuration is missing so fail2ban will not be set up"
fi
else
# Linux
logg info 'Enabling the fail2ban service'
sudo systemctl enable fail2ban
logg info 'Restarting the fail2ban service'
sudo systemctl restart fail2ban
fi
}
### Update the jail.local file if environment is not WSL
logg info 'Checking if script is being run in WSL environment'
if [[ ! "$(test -d /proc && grep Microsoft /proc/version > /dev/null)" ]]; then
if [ -d /etc/fail2ban ]; then
logg info 'Copying ~/.ssh/fail2ban/jail.local to /etc/fail2ban/jail.local'
sudo cp -f "$HOME/.ssh/fail2ban/jail.local" /etc/fail2ban/jail.local
restartFailToBan
elif [ -d /usr/local/etc/fail2ban ]; then
logg info 'Copying ~/.ssh/fail2ban/jail.local to /usr/local/etc/fail2ban/jail.local'
sudo cp -f "$HOME/.ssh/fail2ban/jail.local" /usr/local/etc/fail2ban/jail.local
restartFailToBan
elif [ -d "${HOMEBREW_PREFIX:-/opt/homebrew}/etc/fail2ban" ]; then
logg info "Copying ~/.ssh/fail2ban/jail.local to ${HOMEBREW_PREFIX:-/opt/homebrew}/etc/fail2ban/jail.local"
sudo cp -f "$HOME/.ssh/fail2ban/jail.local" "${HOMEBREW_PREFIX:-/opt/homebrew}/etc/fail2ban/jail.local"
restartFailToBan
else
logg warn 'The /etc/fail2ban (Linux), the /usr/local/etc/fail2ban, and the ${HOMEBREW_PREFIX:-/opt/homebrew}/etc/fail2ban (macOS) folder do not exist'
logg info 'The environment is a WSL environment so the fail2ban sshd_config will be skipped'
fi
else
logg info 'Skipping sshd_config application since environment is WSL'
fi
logg info 'The fail2ban-client executable is not available on the system so fail2ban configuration will be skipped'
fi

View file

@ -16,36 +16,38 @@
### Configure variables
if [ -d /Applications ] && [ -d /System ]; then
### macOS
if [ -d "/usr/local/etc/privoxy" ]; then
PRIVOXY_CONFIG_DIR=/usr/local/etc/privoxy
elif [ -d "${HOMEBREW_PREFIX:-/opt/homebrew}/etc/privoxy" ]; then
PRIVOXY_CONFIG_DIR="${HOMEBREW_PREFIX:-/opt/homebrew}/etc/privoxy"
else
logg warn 'Unable to detect Privoxy configuration directory'
fi
PRIVOXY_CONFIG_DIR=/usr/local/etc/privoxy
else
### Linux
PRIVOXY_CONFIG_DIR=/etc/privoxy
fi
PRIVOXY_CONFIG="$PRIVOXY_CONFIG_DIR/config"
### Copy Privoxy configuration stored at `${XDG_CONFIG_HOME:-HOME/.config}/privoxy/config` to the system location
if command -v privoxy > /dev/null; then
if [ -d "$PRIVOXY_CONFIG_DIR" ]; then
if [ -f "${XDG_CONFIG_HOME:-HOME/.config}/privoxy/config" ]; then
sudo mkdir -p "PRIVOXY_CONFIG_DIR"
logg info "Copying ${XDG_CONFIG_HOME:-HOME/.config}/privoxy/config to $PRIVOXY_CONFIG"
sudo cp -f "${XDG_CONFIG_HOME:-HOME/.config}/privoxy/config" "$PRIVOXY_CONFIG"
logg info "Running sudo chmod 600 $PRIVOXY_CONFIG"
sudo chmod 600 "$PRIVOXY_CONFIG"
if command -v add-usergroup > /dev/null; then
sudo add-usergroup "$USER" privoxy
fi
logg info 'Applying proper permissions to Privoxy configuration'
sudo chown privoxy:privoxy "$PRIVOXY_CONFIG" 2> /dev/null || sudo chown privoxy:$(id -g -n) "$PRIVOXY_CONFIG"
if [ -d "${HOMEBREW_PREFIX:-/opt/homebrew}/etc/privoxy" ] && [ ! -f "${HOMEBREW_PREFIX:-/opt/homebrew}/etc/privoxy/config" ]; then
logg info "Symlinking $PRIVOXY_CONFIG to ${HOMEBREW_PREFIX:-/opt/homebrew}/etc/privoxy/config"
ln -s "$PRIVOXY_CONFIG" "${HOMEBREW_PREFIX:-/opt/homebrew}/etc/privoxy/config"
fi
### Restart Privoxy after configuration is applied
if [ -d /Applications ] && [ -d /System ]; then
### macOS
logg info 'Running brew services restart privoxy'
brew services restart privoxy
else
### Linux
if [[ ! "$(test -d /proc && grep Microsoft /proc/version > /dev/null)" ]]; then
### Linux
logg info 'Running sudo systemctl enable / restart privoxy'
sudo systemctl enable privoxy
sudo systemctl restart privoxy
else
@ -53,8 +55,8 @@ if command -v privoxy > /dev/null; then
fi
fi
else
logg warn 'The '"$PRIVOXY_CONFIG_DIR"' directory is missing'
logg info "${XDG_CONFIG_HOME:-HOME/.config}/privoxy/config is missing so skipping set up of Privoxy"
fi
else
logg logg 'privoxy is missing from the PATH - skipping configuration'
logg info 'privoxy is not installed or not available in the PATH'
fi