Переглянути джерело

User is now warned in its terminal if needed

Laurent HUBERT 10 місяців тому
батько
коміт
56003da2a0
1 змінених файлів з 73 додано та 60 видалено
  1. 73 60
      scripts/lfirewall

+ 73 - 60
scripts/lfirewall

@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/dash
 ### BEGIN INIT INFO
 ### BEGIN INIT INFO
 # Provides:          firewall.sh
 # Provides:          firewall.sh
 # Required-Start:    $syslog $network
 # Required-Start:    $syslog $network
@@ -18,32 +18,41 @@
 # description: Activates/Deactivates the firewall at boot time
 # description: Activates/Deactivates the firewall at boot time
 #
 #
 
 
-
 has_parent_process(){
 has_parent_process(){
-	echo "${1} ${2}"
 	local parent_to_search
 	local parent_to_search
+	local ppid
 	parent_to_search="${1:-}"
 	parent_to_search="${1:-}"
-	if [[ -z "${parent_to_search:-}" ]]
+	if [ -z "${parent_to_search:-}" ]
 	then
 	then
-		echo "ERROR: need parent process pid as first arg"
+		echo "ERROR: need parent process pid as first arg" >&2
 		return 5
 		return 5
 	fi
 	fi
 	local pid
 	local pid
-	if [[ -z "${2:-}" ]]
+	pid="${2:-}"
+	if [ -z "${pid:-}" ]
 	then
 	then
 		pid=$$
 		pid=$$
-		#pid=$(ps --pid $$ -o ppid=  | xargs)
 	fi
 	fi
-	if [[ $parent_to_search -eq $pid ]]
+	if [ $parent_to_search = $pid ]
 	then
 	then
-		echo $parent_to_search
-	else if [[ $pid -gt 0 ]]
+		echo ${parent_to_search}
+		return 0
+	else if [ $pid > 1 ]
 		then
 		then
-			parent_process_terminal ${parent_to_search} $(ps --pid ${pid} -o ppid=  | xargs)
+			ppid=$(ps --pid ${pid} -o ppid= | xargs)
+			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
 		else
-			echo 0
+			echo "NOT FOUND: ${parent_to_search}" >&2
+			echo 1
 		fi
 		fi
 	fi
 	fi
+	return 1
 }
 }
 
 
 find_pid_user_of(){
 find_pid_user_of(){
@@ -52,46 +61,46 @@ find_pid_user_of(){
 	lsof ${used_file} | awk 'NR>1 && $1 ~ /'${regex}'/ && !($2 in a){a[$2]++; print $2}'
 	lsof ${used_file} | awk 'NR>1 && $1 ~ /'${regex}'/ && !($2 in a){a[$2]++; print $2}'
 }
 }
 
 
-echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
-declare -g shell_pid=""
-declare -g systemctl_pid=""
+find_systemctl_pids(){
+	ps -elf | grep 'systemctl' | grep -v grep | awk '{print $13}' | sort -u | while read term
+	do
+		#echo "$term ---"
+		#lsof /dev/$term
+		#lsof -F 'cp' /dev/$term
+		#echo "$term >>>"
+		#lsof /dev/$term | awk 'NR>1 && $1 ~ /.*sh$/ && !($2 in a){a[$2]++; print $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=$shell_pid" >&2
+		#echo "systemctl_pid=$systemctl_pid" >&2
+		echo ${shell_pid} ${systemctl_pid}
+		#echo "TEST:$term" > /dev/$term
+	done
+}
 
 
+#echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
+#declare -g shell_pid=""
+#declare -g systemctl_pid=""
 #ps -elf | grep 'systemctl' | grep -v grep | awk '{print $13}' | sort -u | while read term ; do echo "$term"; lsof /dev/$term ; echo "TEST:$term" > /dev/$term ; done
 #ps -elf | grep 'systemctl' | grep -v grep | awk '{print $13}' | sort -u | while read term ; do echo "$term"; lsof /dev/$term ; echo "TEST:$term" > /dev/$term ; done
-ps -elf | grep 'systemctl' | grep -v grep | awk '{print $13}' | sort -u | while read term
-do
-	echo "$term ---"
-	lsof /dev/$term
-	lsof -F 'cp' /dev/$term
-	echo "$term >>>"
-	#lsof /dev/$term | awk 'NR>1 && $1 ~ /.*sh$/ && !($2 in a){a[$2]++; print $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=$shell_pid"
-	echo "systemctl_pid=$systemctl_pid"
-	echo "TEST:$term" > /dev/$term
-done
-echo "shell_pid=$shell_pid"
-echo "systemctl_pid=$systemctl_pid"
-if [[ -z "${shell_pid:-}" ]]
+process_and_parent=`find_systemctl_pids`
+
+if has_parent_process ${process_and_parent}
 then
 then
-	echo has_parent_process ${shell_pid} ${systemctl_pid}
-	has_parent_process ${shell_pid} ${systemctl_pid}
-else
-	echo "SHELL PID NOT FOUND"
+	shell_process=$(echo ${process_and_parent} | awk '{print $1}')
+	parent_term=`readlink /proc/${shell_process}/fd/2`
 fi
 fi
-echo "Parent terminal:"
-parent_process_terminal
-echo "SSH_TTY: $SSH_TTY"
-echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
+
+echo "Parent terminal: ${parent_term}"
+#echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
 
 
 
 
-set -euo pipefail
+set -eu #o pipefail
 
 
 help_message_lfirewall(){
 help_message_lfirewall(){
 cat <<-EOF
 cat <<-EOF
@@ -129,7 +138,7 @@ EOF
 
 
 options=$(getopt -l "help,verbose,logging" -o "hvl" -- "$@")
 options=$(getopt -l "help,verbose,logging" -o "hvl" -- "$@")
 
 
-if [[ $? != 0 ]] ; then
+if [ $? != 0 ] ; then
 	help_message_lfirewall
 	help_message_lfirewall
 	exit 1
 	exit 1
 fi
 fi
@@ -146,7 +155,7 @@ logging=" "
 
 
 while :
 while :
 do
 do
-	if [[ ${verbose} = "1" ]] ; then
+	if [ ${verbose} = "1" ] ; then
 		echo "$@"
 		echo "$@"
 	fi
 	fi
 
 
@@ -157,11 +166,11 @@ do
 			;;
 			;;
 		-v|--verbose)
 		-v|--verbose)
 			verbose=$(( verbose + 1 ))
 			verbose=$(( verbose + 1 ))
-			if [[ ${verbose} -gt 1 ]]
+			if [ ${verbose} -gt 1 ]
 			then
 			then
 				set -x
 				set -x
 			fi
 			fi
-			if [[ ${verbose} -gt 2 ]]
+			if [ ${verbose} -gt 2 ]
 			then
 			then
 				set -v
 				set -v
 			fi
 			fi
@@ -275,7 +284,7 @@ do_exec () {
 			echo "Nothing to be done for $1"
 			echo "Nothing to be done for $1"
 		;;
 		;;
 	esac
 	esac
-	if [[ ${verbose} -ge 1 ]] ; then
+	if [ ${verbose} -ge 1 ] ; then
 		echo $IP_TABLES $iptables_option $*
 		echo $IP_TABLES $iptables_option $*
 	fi
 	fi
 	$IP_TABLES $iptables_option $*	
 	$IP_TABLES $iptables_option $*	
@@ -299,7 +308,7 @@ do_check () {
 		;;
 		;;
 	esac
 	esac
 	default_option=-C
 	default_option=-C
-	if [[ ${verbose} -ge 1 ]] ; then
+	if [ ${verbose} -ge 1 ] ; then
 		echo $do_log "$the_action:" $IP_TABLES -C $*
 		echo $do_log "$the_action:" $IP_TABLES -C $*
 		echo $IP_TABLES -C $*
 		echo $IP_TABLES -C $*
 	fi
 	fi
@@ -411,7 +420,7 @@ fw_execute () {
 			$do_action $IPTABLES_ADD $IT_OUTPUT -p udp --dport ${PORT} -j ACCEPT
 			$do_action $IPTABLES_ADD $IT_OUTPUT -p udp --dport ${PORT} -j ACCEPT
 		done
 		done
 	fi
 	fi
-	if [[ "${ACTIVATE_LOGGING:-1}" = 1 ]]
+	if [ "${ACTIVATE_LOGGING:-1}" = 1 ]
 	then
 	then
 		# All other connections are registered in system log's backend
 		# All other connections are registered in system log's backend
 		$do_action $IPTABLES_ADD $IT_INPUT -j LOG
 		$do_action $IPTABLES_ADD $IT_INPUT -j LOG
@@ -438,13 +447,13 @@ fw_network_protection(){
 	# Other network protections
 	# Other network protections
 	# (some will only work with some kernel versions)
 	# (some will only work with some kernel versions)
 	#**************************************************************************#
 	#**************************************************************************#
-	if [[ "${ALLOW_IP_FORWARDING:-0}" = 0 ]]
+	if [ "${ALLOW_IP_FORWARDING:-0}" = 0 ]
 	then
 	then
 		echo 0 > /proc/sys/net/ipv4/ip_forward
 		echo 0 > /proc/sys/net/ipv4/ip_forward
 	else
 	else
 		echo 1 > /proc/sys/net/ipv4/ip_forward
 		echo 1 > /proc/sys/net/ipv4/ip_forward
 	fi
 	fi
-	if [[ "${NETWORK_PROTECTION:-1}" = 1 ]]
+	if [ "${NETWORK_PROTECTION:-1}" = 1 ]
 	then
 	then
 		echo 1 > /proc/sys/net/ipv4/tcp_syncookies
 		echo 1 > /proc/sys/net/ipv4/tcp_syncookies
 		echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
 		echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
@@ -458,7 +467,7 @@ fw_network_protection(){
 }
 }
 
 
 do_this(){
 do_this(){
-    if [[ ${verbose} -ge 1 ]] ; then
+    if [ ${verbose} -ge 1 ] ; then
       echo $*
       echo $*
     fi
     fi
     $*
     $*
@@ -495,7 +504,7 @@ fw_restore_user(){
 do_delete () {
 do_delete () {
 	the_action=$1
 	the_action=$1
 	shift
 	shift
-	if [[ ${verbose} -gt 1 ]] ; then
+	if [ ${verbose} -gt 1 ] ; then
 		$do_log "Trying to delete:" $(translate_iptables_rule $IP_TABLES $the_action $*)
 		$do_log "Trying to delete:" $(translate_iptables_rule $IP_TABLES $the_action $*)
 	fi
 	fi
 	case $the_action in
 	case $the_action in
@@ -573,7 +582,7 @@ fw_clear () {
 ##########################
 ##########################
 
 
 fw_save () {
 fw_save () {
-	if [[ ${verbose} -ge 1 ]] ; then
+	if [ ${verbose} -ge 1 ] ; then
 	    echo "$IP_TABLES_SAVE > /etc/lfirewall/iptables.backup"
 	    echo "$IP_TABLES_SAVE > /etc/lfirewall/iptables.backup"
 	fi
 	fi
 
 
@@ -584,7 +593,7 @@ fw_restore () {
 	fw_clear
 	fw_clear
 	BACKUP_FILE=/etc/lfirewall/iptables.backup
 	BACKUP_FILE=/etc/lfirewall/iptables.backup
 	if [ -e $BACKUP_FILE ]; then
 	if [ -e $BACKUP_FILE ]; then
-	  if [[ ${verbose} -ge 1 ]] ; then
+	  if [ ${verbose} -ge 1 ] ; then
 	    echo "IP_TABLES_RESTORE > $BACKUP_FILE"
 	    echo "IP_TABLES_RESTORE > $BACKUP_FILE"
 	  fi
 	  fi
 
 
@@ -600,7 +609,7 @@ fw_test () {
 	wait $(jobs -p)
 	wait $(jobs -p)
 }
 }
 
 
-if [[ ${verbose} -gt 0 ]] ; then
+if [ ${verbose} -gt 0 ] ; then
 	do_log=log_action
 	do_log=log_action
 fi
 fi
 case "$1" in
 case "$1" in
@@ -650,6 +659,10 @@ case "$1" in
 		echo "Droping all connections !!!"
 		echo "Droping all connections !!!"
 		fw_dropall
 		fw_dropall
 		echo "done."
 		echo "done."
+		if [ -n "${parent_term}" ]
+		then
+			exec 2>${parent_term}
+		fi
 		echo "###############################################################" >&2
 		echo "###############################################################" >&2
 		echo "#                   IMPORTANT WARNING !!!                     #" >&2
 		echo "#                   IMPORTANT WARNING !!!                     #" >&2
 		echo "# From now any new SSH session or INPUT                       #" >&2
 		echo "# From now any new SSH session or INPUT                       #" >&2