#!/bin/bash #################################################################################################### #### author: SlickStack ############################################################################ #### link: https://slickstack.io ################################################################### #### mirror: https://mirrors.slickstack.io/bash/ss-install-ufw-config.txt ########################## #### path: /var/www/ss-install-ufw-config ########################################################## #### destination: n/a (not a boilerplate) ########################################################## #### purpose: Reinstalls the entire UFW firewall module for SlickStack servers (idempotent) ######## #### module version: UFW 0.36.x #################################################################### #### sourced by: ss-install ######################################################################## #### bash aliases: ss install ufw, ss install ufw firewall ######################################### #################################################################################################### ## SS-CONFIG MUST BE PROPERLY CONFIGURED AND ON CURRENT BUILD BEFORE RUNNING SS-INSTALL ## ## ENSURE YOUR SS-CONFIG BUILD REMAINS CURRENT BY RUNNING SS-UPDATE OCCASIONALLY ## ## source ss-config ## source /var/www/ss-config ## source ss-functions ## source /var/www/ss-functions ## BELOW THIS RELIES ON SS-CONFIG AND SS-FUNCTIONS #################################################################################################### #### TABLE OF CONTENTS (SS-Install-UFW-Config) ##################################################### #################################################################################################### ## this is a brief summary of the different code snippets you will find in this script ## ## each section should be commented so you understand what is being accomplished ## ## A. Touch Timestamp File ## B. Message (Begin Script) ## C. Force Disable UFW ## D. Configure UFW Settings ## E. Configure UFW User Rules ## F. Reset Permissions (UFW) #################################################################################################### #### A. SS-Install-UFW-Config: Touch Timestamp File ################################################ #################################################################################################### ## this is a dummy timestamp file that will remember the last time this script was run ## ## it can be useful for developer reference and is sometimes used by SlickStack ## ## script timestamp ## ss_touch "${TIMESTAMP_SS_INSTALL_UFW_CONFIG}" #################################################################################################### #### B. SS-Install-UFW-Config: Message (Begin Script) ############################################## #################################################################################################### ## this is a simple message that announces to the shell the purpose of this bash script ## ## it will only be noticed by sudo users who manually call ss core bash scripts ## ## echo message ## ss_echo "${COLOR_INFO}Running ss-install-ufw-config... ${COLOR_RESET}" #################################################################################################### #### C. SS-Install-UFW-Config: Force Disable UFW ################################################### #################################################################################################### ## disable UFW before proceeding with new install ## sudo ufw --force disable > /dev/null 2>&1 #################################################################################################### #### D. SS-Install-UFW-Config: Configure UFW Settings ############################################## #################################################################################################### # retrieve latest versions ## ss_wget /tmp/ufw "${MIRROR_UFW_UFW}" ss_wget /tmp/ufw.conf "${MIRROR_UFW_CONF}" ## copy files to their destinations ## ss_cp /tmp/ufw /etc/default/ufw ss_cp /tmp/ufw.conf /etc/ufw/ufw.conf #################################################################################################### #### E. SS-Install-UFW-Config: Configure UFW User Rules ############################################ #################################################################################################### # retrieve latest versions ## ss_wget /tmp/user.rules "${MIRROR_USER_RULES}" ss_wget /tmp/user6.rules "${MIRROR_USER6_RULES}" # sed -i "s/@SSH_PORT/${SSH_PORT}/g" /tmp/user.rules ## future maybe support custom port whitelist # sed -i "s/@SSH_PORT/${SSH_PORT}/g" /tmp/user6.rules ## future maybe support custom port whitelist ## copy files to their destinations ## ss_cp /tmp/user.rules /etc/ufw/user.rules ss_cp /tmp/user6.rules /etc/ufw/user6.rules #################################################################################################### #### F. SS-Install-UFW-Config: Reset Permissions (UFW) ############################################# #################################################################################################### ## run ss-perms-ufw-config ## source "${PATH_SS_PERMS_UFW_CONFIG}" #################################################################################################### #### SS-Install-UFW: Enable UFW #################################################################### #################################################################################################### ## MOVE THIS somewhere else ## ## CONSIDER not enabling ufw in this script and letting ss-worker do it or something cuz ## many times during re-installation of ss-install the shell session gets kicked off right after this step ## ## enable UFW because we are done configuring settings ## # sudo ufw --force enable > /dev/null 2>&1 #################################################################################################### #### PLACEHOLDER: Reset Permissions (SlickStack Scripts) ########################################### #################################################################################################### ## we include this permissions reset in all cron jobs and bash scripts for redundancy ## ## chmod 0700 means only the root/sudo users can execute any SlickStack scripts ## ## THIS SNIPPET DOES NOT RELY ON SS-CONFIG OR SS-FUNCTIONS ## SNIPPET: ss bash scripts, ss cron jobs ## UPDATED: 02JUL2022 chown root:root /var/www/ss* ## must be root:root chown root:root /var/www/crons/*cron* ## must be root:root chown root:root /var/www/crons/custom/*cron* ## must be root:root chmod 0700 /var/www/ss* ## 0700 means only root/sudo can execute chmod 0700 /var/www/crons/*cron* ## 0700 means only root/sudo can execute chmod 0700 /var/www/crons/custom/*cron* ## 0700 means only root/sudo can execute #################################################################################################### #### SlickStack: External References Used To Improve This Script (Thanks, Interwebz) ############### #################################################################################################### ## Ref: https://linux.die.net/man/8/apt-get ## Ref: https://wiki.ubuntu.com/UncomplicatedFirewall ## Ref: https://askubuntu.com/questions/1006834/ufw-rules-disappear-after-manually-adding-them-to-user-rules-ubuntu-16-04 ## Ref: https://serverfault.com/questions/198398/ubuntu-how-to-add-an-iptables-rule-that-ufw-cant-create ## Ref: https://askubuntu.com/questions/104899/make-apt-get-or-aptitude-run-with-y-but-not-prompt-for-replacement-of-configu ## Ref: https://linuxhint.com/apt_get_fix_missing_broken_packages/ ## Ref: https://bugs.launchpad.net/ubuntu/+source/apt/+bug/1341611 ## Ref: http://manpages.ubuntu.com/manpages/focal/man1/add-apt-repository.1.html ## SS_EOF