|
|
@@ -17,80 +17,17 @@
|
|
|
# chkconfig: 2345 9 91
|
|
|
# description: Activates/Deactivates the firewall at boot time
|
|
|
#
|
|
|
+CONFIGURATION_DIR=/etc/lfirewall
|
|
|
|
|
|
-has_parent_process(){
|
|
|
- local parent_to_search
|
|
|
- local ppid
|
|
|
- parent_to_search="${1:-}"
|
|
|
- if [ -z "${parent_to_search:-}" ]
|
|
|
- then
|
|
|
- echo "ERROR: need parent process pid as first arg" >&2
|
|
|
- return 5
|
|
|
- fi
|
|
|
- local pid
|
|
|
- pid="${2:-}"
|
|
|
- if [ -z "${pid:-}" ]
|
|
|
- then
|
|
|
- pid=$$
|
|
|
- fi
|
|
|
- if [ $parent_to_search = $pid ]
|
|
|
- then
|
|
|
- echo ${parent_to_search}
|
|
|
- return 0
|
|
|
- else if [ $pid -gt 1 ]
|
|
|
- then
|
|
|
- ppid=$(ps --pid ${pid} -o ppid= | xargs) || echo "OUT OF RANGE PID=${pid}" >&2
|
|
|
- if [ -n "$ppid" ]
|
|
|
- then
|
|
|
- if [ $ppid = $pid ]
|
|
|
- then
|
|
|
- #echo "ERROR: pid=$pid is the same as ppid=$ppid" >&2
|
|
|
- echo -1
|
|
|
- else
|
|
|
- has_parent_process ${parent_to_search} ${ppid}
|
|
|
- fi
|
|
|
- else
|
|
|
- #echo "ERROR: pid='$pid' has ppid='$ppid'" >&2
|
|
|
- echo -2
|
|
|
- fi
|
|
|
- else
|
|
|
- #echo "NOT FOUND: ${parent_to_search}" >&2
|
|
|
- echo 1
|
|
|
- fi
|
|
|
- fi
|
|
|
- return 1
|
|
|
-}
|
|
|
-
|
|
|
-find_pid_user_of(){
|
|
|
- local used_file=$1
|
|
|
- local regex="$2"
|
|
|
- lsof ${used_file} | awk 'NR>1 && $1 ~ /'${regex}'/ && !($2 in a){a[$2]++; print $2}'
|
|
|
-}
|
|
|
+. ${CONFIGURATION_DIR}/setup
|
|
|
|
|
|
-find_systemctl_pids(){
|
|
|
- local shell_pid
|
|
|
- local systemctl_pid
|
|
|
- ps -elf | grep 'systemctl' | grep -v grep | awk '{print $13}' | sort -u | while read term
|
|
|
- do
|
|
|
- #echo ${shell_pid} ${systemctl_pid} >&2
|
|
|
- if [ -z "${shell_pid:-}" ]
|
|
|
- then
|
|
|
- shell_pid=$(find_pid_user_of /dev/$term '.*sh$')
|
|
|
- fi
|
|
|
- if [ -z "${systemctl_pid:-}" ]
|
|
|
- then
|
|
|
- systemctl_pid=$(find_pid_user_of /dev/$term 'systemctl')
|
|
|
- fi
|
|
|
- echo ${shell_pid} ${systemctl_pid}
|
|
|
- done
|
|
|
-}
|
|
|
process_and_parent=`find_systemctl_pids`
|
|
|
|
|
|
restart_mode=no
|
|
|
|
|
|
if [ -n "${process_and_parent}" ]
|
|
|
then
|
|
|
- if has_parent_process ${process_and_parent}
|
|
|
+ if has_parent_process ${process_and_parent} > /dev/null
|
|
|
then
|
|
|
shell_process=`echo ${process_and_parent} | awk '{print $1}'`
|
|
|
parent_term=`readlink /proc/${shell_process}/fd/2`
|
|
|
@@ -99,9 +36,11 @@ then
|
|
|
# Find if systemctl is in restart mode
|
|
|
for process_id in ${process_and_parent}
|
|
|
do
|
|
|
- if [ `ps -o comm= -p $process_id` = "systemctl" ]
|
|
|
+ if [ `ps -o comm= -p $process_id` = "systemctl" ] \
|
|
|
+ && ps -o args -p $process_id | grep -v COMMAND | grep restart > /dev/null
|
|
|
then
|
|
|
- restart_mode=`ps -o args -p $process_id | grep -v COMMAND | grep restart && echo yes || echo no`
|
|
|
+ restart_mode=yes
|
|
|
+ break
|
|
|
fi
|
|
|
done
|
|
|
fi
|
|
|
@@ -151,11 +90,6 @@ fi
|
|
|
|
|
|
eval set -- "$options"
|
|
|
|
|
|
-IT_INPUT=INPUT
|
|
|
-IT_INPUT_LOG=LOGINPUT
|
|
|
-IT_OUTPUT=OUTPUT
|
|
|
-IT_OUTPUT_LOG=LOGOUTPUT
|
|
|
-
|
|
|
verbose=0
|
|
|
logging=" "
|
|
|
|
|
|
@@ -194,35 +128,6 @@ do
|
|
|
shift
|
|
|
done
|
|
|
|
|
|
-PATH=/bin:/sbin:/usr/bin:/usr/sbin
|
|
|
-
|
|
|
-#Defautl network interface
|
|
|
-NETWORK_IF=eth0
|
|
|
-
|
|
|
-# Services that the system will offer to the network
|
|
|
-TCP_SERVICES="22" # SSH only
|
|
|
-UDP_SERVICES=""
|
|
|
-# Services the system will use from the network
|
|
|
-REMOTE_TCP_SERVICES="80 443" # web browsing
|
|
|
-REMOTE_UDP_SERVICES="53" # DNS
|
|
|
-
|
|
|
-
|
|
|
-# Network that will be used for remote mgmt
|
|
|
-# (if undefined, no rules will be setup)
|
|
|
-# NETWORK_MGMT=192.168.0.0/24
|
|
|
-
|
|
|
-# Port used for the SSH service, define this is you have setup a
|
|
|
-# management network but remove it from TCP_SERVICES
|
|
|
-SSH_PORT="22"
|
|
|
-
|
|
|
-# Default IP_TABLES command path
|
|
|
-IP_TABLES="/sbin/iptables"
|
|
|
-IP_TABLES_RESTORE="/sbin/iptables-restore"
|
|
|
-IP_TABLES_RESTORE_6="/sbin/ip6tables-restore"
|
|
|
-IP_TABLES_SAVE="/sbin/iptables-save"
|
|
|
-IP_TABLES_SAVE_6="/sbin/ip6tables-save"
|
|
|
-
|
|
|
-
|
|
|
if ! [ -x $IP_TABLES ]; then
|
|
|
echo "$IP_TABLES is not executable or not present" >&2
|
|
|
exit 1
|
|
|
@@ -238,13 +143,6 @@ if ! [ -x $IP_TABLES_RESTORE_6 ]; then
|
|
|
exit 6
|
|
|
fi
|
|
|
|
|
|
-
|
|
|
-CONFIGURATION_DIR=/etc/lfirewall
|
|
|
-CONFIGURATION_FILE=${CONFIGURATION_DIR}/lfirewall.conf
|
|
|
-CONFIGURATION_LOCAL_FILE=${CONFIGURATION_DIR}/lfirewall.conf.local
|
|
|
-USER_RULES_IPTABLES=${CONFIGURATION_DIR}/iptables-user.v4
|
|
|
-USER_RULES_IPTABLES_6=${CONFIGURATION_DIR}/iptables-user.v6
|
|
|
-
|
|
|
if [ -f $CONFIGURATION_FILE ] ; then
|
|
|
set +u
|
|
|
. $CONFIGURATION_FILE
|
|
|
@@ -264,79 +162,10 @@ then
|
|
|
exit 2
|
|
|
fi
|
|
|
|
|
|
-export IPTABLES_CHECK=__iptables_check_action
|
|
|
-export IPTABLES_ADD=__iptable_add_action
|
|
|
-export IPTABLES_INSERT=__iptable_insert_action
|
|
|
-export IPTABLES_SET_POLICY=__iptable_set_policy_action
|
|
|
-
|
|
|
-export IP_TABLES
|
|
|
-export NETWORK_IF
|
|
|
-
|
|
|
-
|
|
|
-do_exec () {
|
|
|
- case $1 in
|
|
|
- __iptable_add_action)
|
|
|
- shift
|
|
|
- iptables_option=-A
|
|
|
- ;;
|
|
|
- __iptable_insert_action)
|
|
|
- shift
|
|
|
- iptables_option=-I
|
|
|
- ;;
|
|
|
- __iptable_set_policy_action)
|
|
|
- shift
|
|
|
- iptables_option=-P
|
|
|
- ;;
|
|
|
- *)
|
|
|
- echo "Nothing to be done for $1"
|
|
|
- ;;
|
|
|
- esac
|
|
|
- if [ ${verbose} -ge 1 ] ; then
|
|
|
- echo $IP_TABLES $iptables_option $*
|
|
|
- fi
|
|
|
- if ! $IP_TABLES -C $* > /dev/null 2>&1
|
|
|
- then
|
|
|
- $IP_TABLES $iptables_option $*
|
|
|
- fi
|
|
|
-}
|
|
|
-
|
|
|
-do_check () {
|
|
|
- the_action=$1
|
|
|
- shift
|
|
|
- case $the_action in
|
|
|
- __iptable_add_action)
|
|
|
- iptables_option=-A
|
|
|
- ;;
|
|
|
- __iptable_insert_action)
|
|
|
- iptables_option=-I
|
|
|
- ;;
|
|
|
- __iptable_set_policy_action)
|
|
|
- return 0
|
|
|
- ;;
|
|
|
- *)
|
|
|
- echo "Nothing to be done for $1"
|
|
|
- ;;
|
|
|
- esac
|
|
|
- default_option=-C
|
|
|
- if [ ${verbose} -ge 1 ] ; then
|
|
|
- echo $do_log "$the_action:" $IP_TABLES -C $*
|
|
|
- echo $IP_TABLES -C $*
|
|
|
- fi
|
|
|
- $do_log "$the_action:" $IP_TABLES -C $*
|
|
|
- $IP_TABLES -C $*
|
|
|
- global_status=$((global_status+$?))
|
|
|
-}
|
|
|
-
|
|
|
-log_action () {
|
|
|
- echo $*
|
|
|
-}
|
|
|
-
|
|
|
-do_not_log_action () {
|
|
|
- return 0
|
|
|
-}
|
|
|
-
|
|
|
do_log=do_not_log_action
|
|
|
|
|
|
+export verbose
|
|
|
+
|
|
|
##########################
|
|
|
# Drops INPUT
|
|
|
##########################
|
|
|
@@ -456,12 +285,34 @@ fw_execute () {
|
|
|
#**************************************************************************#
|
|
|
fw_blacklist $do_action
|
|
|
|
|
|
+ fw_execute_post_up_down $do_action
|
|
|
+ fw_execute_post_start_stop $do_action
|
|
|
+
|
|
|
#**************************************************************************#
|
|
|
# whitelist action
|
|
|
#**************************************************************************#
|
|
|
fw_whitelist $do_action
|
|
|
}
|
|
|
|
|
|
+EXECUTE_LFIREWALL_DIR=`dirname $0`/execute_lfirewall_dir
|
|
|
+
|
|
|
+fw_execute_post_up_down(){
|
|
|
+ local do_action
|
|
|
+ do_action=$1
|
|
|
+ set +eu
|
|
|
+ ${EXECUTE_LFIREWALL_DIR} -f ${do_action} ${POST_UP_DOWN_SCRIPTS_DIR}
|
|
|
+ set -eu
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+fw_execute_post_start_stop(){
|
|
|
+ local do_action
|
|
|
+ do_action=$1
|
|
|
+ set +eu
|
|
|
+ ${EXECUTE_LFIREWALL_DIR} ${do_action} ${POST_START_STOP_SCRIPTS_DIR}
|
|
|
+ set -eu
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
warn_user_missing_ban_list() {
|
|
|
local ban_list_name
|
|
|
@@ -575,10 +426,6 @@ do_this(){
|
|
|
$*
|
|
|
}
|
|
|
|
|
|
-translate_iptables_rule(){
|
|
|
- echo $* | sed -e 's/'$IPTABLES_ADD'/-A/g' -e 's/'$IPTABLES_INSERT'/-I/g' -e 's/'$IPTABLES_SET_POLICY'/-P/g'
|
|
|
-}
|
|
|
-
|
|
|
##########################
|
|
|
# Backups user rules
|
|
|
##########################
|
|
|
@@ -603,42 +450,6 @@ fw_restore_user(){
|
|
|
# Stop the Firewall rules
|
|
|
##########################
|
|
|
|
|
|
-do_delete () {
|
|
|
- the_action=$1
|
|
|
- shift
|
|
|
- if [ ${verbose} -gt 1 ] ; then
|
|
|
- $do_log "Trying to delete:" $(translate_iptables_rule $IP_TABLES $the_action $*)
|
|
|
- fi
|
|
|
- case $the_action in
|
|
|
- __iptable_add_action)
|
|
|
- iptables_option=-D
|
|
|
- ;;
|
|
|
- __iptable_insert_action)
|
|
|
- iptables_option=-D
|
|
|
- ;;
|
|
|
- __iptable_set_policy_action)
|
|
|
- CHAIN_NAME="$1"
|
|
|
- $do_log "DELETING: $IP_TABLES -P $CHAIN_NAME DROP"
|
|
|
- $IP_TABLES -P $CHAIN_NAME ACCEPT
|
|
|
- return 0
|
|
|
- ;;
|
|
|
- *)
|
|
|
- echo "Nothing to be done for $1"
|
|
|
- ;;
|
|
|
- esac
|
|
|
-
|
|
|
-
|
|
|
- # Checks the rule then delete it, if it exists
|
|
|
- if $IP_TABLES -C $* > /dev/null 2>&1
|
|
|
- then
|
|
|
- $IP_TABLES $iptables_option $* || echo "DID NOT EXIST: "$IP_TABLES $iptables_option $*
|
|
|
- $do_log "DELETING:" $IP_TABLES $iptables_option $*
|
|
|
- else
|
|
|
- $do_log "NOT EXISTING:" $IP_TABLES $iptables_option $*
|
|
|
- fi
|
|
|
- global_status=$((global_status+$?))
|
|
|
-}
|
|
|
-
|
|
|
fw_stop () {
|
|
|
global_status=0
|
|
|
# Start will not really start but exec the "do_delete" action
|
|
|
@@ -714,6 +525,8 @@ fw_test () {
|
|
|
if [ ${verbose} -gt 0 ] ; then
|
|
|
do_log=log_action
|
|
|
fi
|
|
|
+export do_log
|
|
|
+
|
|
|
case "$1" in
|
|
|
start|restart)
|
|
|
echo -n "Starting firewall.."
|