Skip to content

Commit 14699b3

Browse files
ggtrdggtrd
authored andcommitted
1.1.0 starting firewall + improve config file
1 parent e64002d commit 14699b3

File tree

5 files changed

+235
-53
lines changed

5 files changed

+235
-53
lines changed

bashpack.sh

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@
2525

2626

2727

28-
export VERSION="1.0.12"
28+
export VERSION="1.1.0"
2929

3030
export NAME="Bashpack"
3131
export NAME_LOWERCASE=$(echo "$NAME" | tr A-Z a-z)
32+
export NAME_UPPERCASE=$(echo "$NAME" | tr a-z A-Z)
3233
export NAME_ALIAS="bp"
3334

3435
BASE_URL="https://api.github.com/repos/bashpack-project"
3536

3637
USAGE="Usage: sudo $NAME_ALIAS [COMMAND] [OPTION]..."$'\n'"$NAME_ALIAS --help"
3738

3839
export yes="@(yes|Yes|yEs|yeS|YEs|YeS|yES|YES|y|Y)"
40+
export continue_question="Do you want to continue? [y/N] "
3941

4042

4143

@@ -176,6 +178,12 @@ get_config_value() {
176178
local parameter=${2}
177179

178180
while read -r line; do
181+
182+
# Avoid reading comments
183+
if [[ $line =~ ^"${#}" ]]; then
184+
break
185+
fi
186+
179187
if [[ $line =~ ^([^=]+)[[:space:]]([^=]+)$ ]]; then
180188
# Test first word (= parameter name)...
181189
if [[ $parameter = ${BASH_REMATCH[1]} ]]; then
@@ -264,11 +272,13 @@ if [[ $0 = "./$NAME_LOWERCASE.sh" ]] && [[ -d "commands" ]]; then
264272
echo ""
265273
COMMAND_UPDATE="commands/update.sh"
266274
COMMAND_MAN="commands/man.sh"
267-
COMMAND_VERIFY_INTALLATION="commands/tests.sh"
275+
COMMAND_VERIFY="commands/tests.sh"
276+
COMMAND_FIREWALL="commands/firewall.sh"
268277
else
269278
COMMAND_UPDATE="$dir_src/update.sh"
270279
COMMAND_MAN="$dir_src/man.sh"
271-
COMMAND_VERIFY_INTALLATION="$dir_src/tests.sh"
280+
COMMAND_VERIFY="$dir_src/tests.sh"
281+
COMMAND_FIREWALL="$dir_src/firewall.sh"
272282
fi
273283
COMMAND_SYSTEMD_LOGS="journalctl -e _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value $file_systemd_update.service`"
274284
COMMAND_SYSTEMD_STATUS="systemctl status $file_systemd_update.timer"
@@ -713,13 +723,24 @@ case "$1" in
713723
man) $COMMAND_MAN ;;
714724
verify)
715725
if [[ -z "$2" ]]; then
716-
export function_to_launch="check_all" && exec $COMMAND_VERIFY_INTALLATION
726+
export function_to_launch="check_all" && exec $COMMAND_VERIFY
727+
else
728+
case "$2" in
729+
-f|--files) export function_to_launch="check_files" && exec $COMMAND_VERIFY ;;
730+
-d|--download) export function_to_launch="check_download" && exec $COMMAND_VERIFY ;;
731+
-r|--repository-reachability) export function_to_launch="check_repository_reachability" && exec $COMMAND_VERIFY ;;
732+
*) echo "Error: unknown [$1] option '$2'."$'\n'"$USAGE" && exit ;;
733+
esac
734+
fi ;;
735+
firewall)
736+
if [[ -z "$2" ]]; then
737+
exec $COMMAND_FIREWALL
717738
else
718739
case "$2" in
719-
-f|--files) export function_to_launch="check_files" && exec $COMMAND_VERIFY_INTALLATION ;;
720-
-d|--download) export function_to_launch="check_download" && exec $COMMAND_VERIFY_INTALLATION ;;
721-
-r|--repository-reachability) export function_to_launch="check_repository_reachability" && exec $COMMAND_VERIFY_INTALLATION ;;
722-
*) echo "Error: unknown [verify] option '$2'."$'\n'"$USAGE" && exit ;;
740+
-o|--open-inbound-port) exec $COMMAND_FIREWALL ;;
741+
-e|--enable) exec $COMMAND_FIREWALL ;;
742+
--disable) exec $COMMAND_FIREWALL ;;
743+
*) echo "Error: unknown [$1] option '$2'."$'\n'"$USAGE" && exit ;;
723744
esac
724745
fi ;;
725746
update)
@@ -732,7 +753,7 @@ case "$1" in
732753
--ask) read -p "Do you want to automatically accept installations during the process? [y/N] " install_confirmation && export install_confirmation && exec $COMMAND_UPDATE ;;
733754
--when) $COMMAND_SYSTEMD_STATUS | grep Trigger: | awk '$1=$1' ;;
734755
--get-logs) $COMMAND_SYSTEMD_LOGS ;;
735-
*) echo "Error: unknown [update] option '$2'."$'\n'"$USAGE" && exit ;;
756+
*) echo "Error: unknown [$1] option '$2'."$'\n'"$USAGE" && exit ;;
736757
esac
737758
# done
738759
fi ;;

commands/firewall.sh

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/bin/bash
2+
3+
# MIT License
4+
5+
# Copyright (c) 2024 Geoffrey Gontard
6+
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
25+
26+
27+
28+
disable_firewall() {
29+
systemctl stop nftables.service
30+
}
31+
32+
33+
34+
35+
# This script is based on nftables, it can't work without it.
36+
# Testing if nftables is installed on the system, try to install it if not, or exit.
37+
install_firewall() {
38+
if [[ $(exists_command "nft") != "exists" ]]; then
39+
echo "Error: nftables not found on the system but is required to configure your firewall with $NAME."
40+
41+
if [[ $(exists_command "apt") = "exists" ]]; then
42+
echo "Installing nftables with APT..."
43+
echo "Warning: if iptables is installed, nftables will replace it. "
44+
echo "Warning: be sure to keep a copy of your currents firewall rulesets."
45+
read -p "$continue_question" install_confirmation_nftables
46+
47+
if [[ $install_confirmation_nftables = $yes ]]; then
48+
apt install -y nftables
49+
systemctl enable nftables.service
50+
systemctl restart nftables.service
51+
else
52+
# Exit to avoid doing anything else with this script without nftables installed
53+
exit
54+
fi
55+
56+
else
57+
echo "Error: nftables could not been installed with APT."
58+
59+
# Exit to avoid doing anything else with this script without nftables installed
60+
exit
61+
fi
62+
63+
fi
64+
}
65+
66+
67+
68+
69+
# Configure the firewall
70+
create_firewall() {
71+
72+
now=$(date +%y-%m-%d_%H-%M-%S)
73+
nftables_file="/etc/nftables.conf"
74+
nftables_dir="/etc/bashpack/firewall/"
75+
nftables_file_backup=$nftables_dir"nftables.conf_backup_$now"
76+
77+
78+
# # Making sure to use nftables
79+
# apt install -y nftables
80+
# systemctl enable nftables.service
81+
# systemctl restart nftables.service
82+
83+
# Making sure the nftables.conf file exists
84+
nft list ruleset > $nftables_file
85+
86+
# Making a backup of your current nftables ruleset
87+
mkdir -p $nftables_dir
88+
chmod 755 $nftables_dir
89+
cp $nftables_file $nftables_file_backup
90+
91+
echo ""
92+
echo "A backup of your current nftables firewall ruleset has been saved to "$nftables_file_backup"."
93+
echo ""
94+
95+
# --------------------------
96+
# Here is what this script will erase and recreate :
97+
#
98+
# table inet filter
99+
# chain $NAME_UPPERCASE-PREROUTING type filter hook prerouting priority -199; policy drop;
100+
# ct state related,established counter accept
101+
# iifname lo counter accept
102+
# chain $NAME_UPPERCASE-OUTPUT
103+
# type filter hook output priority -300; policy accept;
104+
#
105+
# Documentation here: https://wiki.nftables.org/wiki-nftables/index.php/Netfilter_hooks
106+
# --------------------------
107+
108+
# Deleting current ruleset to get a clean firewall and avoid any issues
109+
nft flush ruleset
110+
111+
# Adding the inet filter table to the current firewall
112+
nft add table inet filter
113+
114+
# Creating $NAME_UPPERCASE-PREROUTING chain
115+
# Eveything is closed inbound by default
116+
nft add chain inet filter $NAME_UPPERCASE-PREROUTING { type filter hook prerouting priority -199\; policy drop\; }
117+
nft add rule inet filter $NAME_UPPERCASE-PREROUTING ct state related,established counter accept
118+
nft add rule inet filter $NAME_UPPERCASE-PREROUTING iifname lo counter accept
119+
# Allowing Docker containers to see each others & to reach internet (works with or without Docker bridges) (this rule is created even if Docker is not installed to anticipate any futures Docker installations)
120+
nft add rule inet filter $NAME_UPPERCASE-PREROUTING ip saddr 172.0.0.0/8 counter accept
121+
# Inbound customs rules below
122+
#nft add rule inet filter $NAME_UPPERCASE-PREROUTING tcp dport <PORT> counter accept
123+
124+
# Creating $NAME_UPPERCASE-POSTROUTING
125+
# Eveything is open outbound by default
126+
nft add chain inet filter $NAME_UPPERCASE-POSTROUTING { type filter hook postrouting priority -300\; policy accept\; }
127+
128+
129+
# Saving the new nftables ruleset
130+
nft list ruleset > $nftables_file
131+
132+
# Restarting firewall
133+
systemctl restart nftables.service
134+
135+
# Restarting Docker (if Docker is installed) to force it using nftables instead of iptables
136+
if [[ $(exists_command "docker") = "exists" ]]; then
137+
systemctl restart docker.service
138+
fi
139+
140+
141+
echo "Success! New firewall configured."
142+
143+
144+
}
145+
146+
147+
148+
install_firewall
149+
create_firewall

commands/man.sh

100644100755
File mode changed.

commands/update.sh

100644100755
Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,67 +25,67 @@
2525

2626

2727

28-
continue_question="Do you want to continue? [y/N] "
28+
# continue_question="Do you want to continue? [y/N] "
2929

3030

3131

3232

33-
# Usage : text_error_cant_install <manager>
34-
install_package_error() {
35-
echo "Error: could not be installed with ${1}."
36-
}
33+
# # Usage : text_error_cant_install <manager>
34+
# install_package_error() {
35+
# echo "Error: could not be installed with ${1}."
36+
# }
3737

3838

3939

4040

41-
# Function to install packages on the system (and package managers also, because package managers are packages themselves).
42-
# Package manager order to search packages candidates: Apt -> Snapcraft -> Error "not found"
43-
# Usage : install_package <manager> <package>
44-
install_package() {
45-
local manager=${1}
46-
local package=${2}
41+
# # Function to install packages on the system (and package managers also, because package managers are packages themselves).
42+
# # Package manager order to search packages candidates: Apt -> Snapcraft -> Error "not found"
43+
# # Usage : install_package <manager> <package>
44+
# install_package() {
45+
# local manager=${1}
46+
# local package=${2}
4747

48-
echo ""
49-
echo "Installing $package with $manager... "
50-
echo ""
48+
# echo ""
49+
# echo "Installing $package with $manager... "
50+
# echo ""
5151

52-
if ([[ $manager = "apt" ]] && [[ $(exists_command "apt") = "exists" ]]) || [[ $(exists_command "apt") = "exists" ]]; then
53-
apt install -y $package
54-
elif ([[ $manager = "snap" ]] && [[ $(exists_command "snap") = "exists" ]]) || [[ $(exists_command "snap") = "exists" ]]; then
55-
snap install $package
56-
else
57-
echo "$package: Error: package not found."
58-
fi
52+
# if ([[ $manager = "apt" ]] && [[ $(exists_command "apt") = "exists" ]]) || [[ $(exists_command "apt") = "exists" ]]; then
53+
# apt install -y $package
54+
# elif ([[ $manager = "snap" ]] && [[ $(exists_command "snap") = "exists" ]]) || [[ $(exists_command "snap") = "exists" ]]; then
55+
# snap install $package
56+
# else
57+
# echo "$package: Error: package not found."
58+
# fi
5959

60-
echo ""
61-
}
60+
# echo ""
61+
# }
6262

6363

6464

6565

66-
# Function to delete packages on the system.
67-
# Package manager order to search packages candidates: Apt -> Snapcraft -> Error "not found"
68-
# Usage : delete_package $package <$manager> <yes>
69-
delete_package() {
70-
local package=${1}
71-
local manager=${2}
66+
# # Function to delete packages on the system.
67+
# # Package manager order to search packages candidates: Apt -> Snapcraft -> Error "not found"
68+
# # Usage : delete_package $package <$manager> <yes>
69+
# delete_package() {
70+
# local package=${1}
71+
# local manager=${2}
7272

73-
echo ""
74-
if [[ $manager != "" ]]; then
75-
echo "Uninstalling $package with $manager... "
76-
else
77-
echo "Uninstalling $package with the default system manager... "
78-
fi
79-
echo ""
73+
# echo ""
74+
# if [[ $manager != "" ]]; then
75+
# echo "Uninstalling $package with $manager... "
76+
# else
77+
# echo "Uninstalling $package with the default system manager... "
78+
# fi
79+
# echo ""
8080

81-
if ([[ $manager = "apt" ]] && [[ $(exists_command "apt") = "exists" ]]) || [[ $(exists_command "apt") = "exists" ]]; then
82-
apt remove -y $package
83-
elif ([[ $manager = "snap" ]] && [[ $(exists_command "snap") = "exists" ]]) || [[ $(exists_command "snap") = "exists" ]]; then
84-
snap remove $package
85-
else
86-
echo "$package: Error: package not found."
87-
fi
88-
}
81+
# if ([[ $manager = "apt" ]] && [[ $(exists_command "apt") = "exists" ]]) || [[ $(exists_command "apt") = "exists" ]]; then
82+
# apt remove -y $package
83+
# elif ([[ $manager = "snap" ]] && [[ $(exists_command "snap") = "exists" ]]) || [[ $(exists_command "snap") = "exists" ]]; then
84+
# snap remove $package
85+
# else
86+
# echo "$package: Error: package not found."
87+
# fi
88+
# }
8989

9090

9191

config/bashpack_config

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1+
# How to use this file ?
2+
# A single space is necessary to match options with their values:
3+
# <option> <value>
4+
5+
# publication can be "main", "unstable" or "dev"
6+
# - Production systems should always be configured with "main"
7+
# - "dev" and "unstable" are meant for development use only, you should never use them.
18
publication main
9+
10+
# firewall can be "auto" or "disable"
11+
# auto = automatically install & configure firewall at each update
12+
# disable = do not
13+
firewall auto

0 commit comments

Comments
 (0)