lfirewall 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. #!/bin/dash
  2. ### BEGIN INIT INFO
  3. # Provides: firewall.sh
  4. # Required-Start: $syslog $network
  5. # Required-Stop: $syslog $network
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Start firewall daemon at boot time
  9. # Description: Custom Firewall scrip.
  10. ### END INIT INFO
  11. #
  12. # Light Firewall configuration.
  13. #
  14. # Original author : Nicolargo
  15. #
  16. # chkconfig: 2345 9 91
  17. # description: Activates/Deactivates the firewall at boot time
  18. #
  19. has_parent_process(){
  20. local parent_to_search
  21. local ppid
  22. parent_to_search="${1:-}"
  23. if [ -z "${parent_to_search:-}" ]
  24. then
  25. echo "ERROR: need parent process pid as first arg" >&2
  26. return 5
  27. fi
  28. local pid
  29. pid="${2:-}"
  30. if [ -z "${pid:-}" ]
  31. then
  32. pid=$$
  33. fi
  34. if [ $parent_to_search = $pid ]
  35. then
  36. echo ${parent_to_search}
  37. return 0
  38. else if [ $pid > 1 ]
  39. then
  40. ppid=$(ps --pid ${pid} -o ppid= | xargs)
  41. if [ $ppid = $pid ]
  42. then
  43. echo "ERROR: pid=$pid is the same as ppid=$ppid" >&2
  44. echo -1
  45. else
  46. has_parent_process ${parent_to_search} ${ppid}
  47. fi
  48. else
  49. echo "NOT FOUND: ${parent_to_search}" >&2
  50. echo 1
  51. fi
  52. fi
  53. return 1
  54. }
  55. find_pid_user_of(){
  56. local used_file=$1
  57. local regex="$2"
  58. lsof ${used_file} | awk 'NR>1 && $1 ~ /'${regex}'/ && !($2 in a){a[$2]++; print $2}'
  59. }
  60. find_systemctl_pids(){
  61. ps -elf | grep 'systemctl' | grep -v grep | awk '{print $13}' | sort -u | while read term
  62. do
  63. #echo "$term ---"
  64. #lsof /dev/$term
  65. #lsof -F 'cp' /dev/$term
  66. #echo "$term >>>"
  67. #lsof /dev/$term | awk 'NR>1 && $1 ~ /.*sh$/ && !($2 in a){a[$2]++; print $2}'
  68. if [ -z "${shell_pid:-}" ]
  69. then
  70. shell_pid=$(find_pid_user_of /dev/$term '.*sh$')
  71. fi
  72. if [ -z "${systemctl_pid:-}" ]
  73. then
  74. systemctl_pid=$(find_pid_user_of /dev/$term 'systemctl')
  75. fi
  76. #echo "shell_pid=$shell_pid" >&2
  77. #echo "systemctl_pid=$systemctl_pid" >&2
  78. echo ${shell_pid} ${systemctl_pid}
  79. #echo "TEST:$term" > /dev/$term
  80. done
  81. }
  82. #echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
  83. #declare -g shell_pid=""
  84. #declare -g systemctl_pid=""
  85. #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
  86. process_and_parent=`find_systemctl_pids`
  87. if has_parent_process ${process_and_parent}
  88. then
  89. shell_process=$(echo ${process_and_parent} | awk '{print $1}')
  90. parent_term=`readlink /proc/${shell_process}/fd/2`
  91. fi
  92. echo "Parent terminal: ${parent_term}"
  93. #echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
  94. set -eu #o pipefail
  95. help_message_lfirewall(){
  96. cat <<-EOF
  97. Light Firewall configuration script.
  98. $(basename ${0}) [OPTIONS...] COMMAND
  99. COMMANDS
  100. start : starts the firewall
  101. stop : stops the firewall (removes rules set by this script)
  102. clear : clears the firewall (removes all IPTABLES rules and let all connections work)
  103. dropall : drops all network connections (USE with CAUTION)
  104. test : tests the existing rules for 30 seconds
  105. saveuser : backup of the user (custom) rules
  106. OPTIONS
  107. -v --verbose : verbose mode
  108. -h --help : display this help message
  109. -l --logging : enables logging (prefix: iptables-logging)
  110. BACKING UP USER RULES
  111. WARNING: PLEASE FOLLOW CAREFULLY THE ACTIONS BELOW
  112. 1. Before setting any rule: clear the firewall
  113. 2. Setup your rules
  114. 3. run:
  115. $(basename ${0}) saveuser
  116. AUTHOR
  117. Author: Laurent Hubert
  118. Idea by: Nicolargo
  119. EOF
  120. }
  121. options=$(getopt -l "help,verbose,logging" -o "hvl" -- "$@")
  122. if [ $? != 0 ] ; then
  123. help_message_lfirewall
  124. exit 1
  125. fi
  126. eval set -- "$options"
  127. IT_INPUT=INPUT
  128. IT_INPUT_LOG=LOGINPUT
  129. IT_OUTPUT=OUTPUT
  130. IT_OUTPUT_LOG=LOGOUTPUT
  131. verbose=0
  132. logging=" "
  133. while :
  134. do
  135. if [ ${verbose} = "1" ] ; then
  136. echo "$@"
  137. fi
  138. case "$1" in
  139. -h|--help)
  140. help_message_lfirewall
  141. exit 0
  142. ;;
  143. -v|--verbose)
  144. verbose=$(( verbose + 1 ))
  145. if [ ${verbose} -gt 1 ]
  146. then
  147. set -x
  148. fi
  149. if [ ${verbose} -gt 2 ]
  150. then
  151. set -v
  152. fi
  153. ;;
  154. -l|--logging)
  155. logging="-j LOG --log-prefix 'iptables-logging'"
  156. IT_INPUT=${IT_INPUT_LOG}
  157. IT_OUTPUT=${IT_OUTPUT_LOG}
  158. ;;
  159. --)
  160. shift
  161. break
  162. ;;
  163. esac
  164. shift
  165. done
  166. PATH=/bin:/sbin:/usr/bin:/usr/sbin
  167. #Defautl network interface
  168. NETWORK_IF=eth0
  169. # Services that the system will offer to the network
  170. TCP_SERVICES="22" # SSH only
  171. UDP_SERVICES=""
  172. # Services the system will use from the network
  173. REMOTE_TCP_SERVICES="80 443" # web browsing
  174. REMOTE_UDP_SERVICES="53" # DNS
  175. # Network that will be used for remote mgmt
  176. # (if undefined, no rules will be setup)
  177. # NETWORK_MGMT=192.168.0.0/24
  178. # Port used for the SSH service, define this is you have setup a
  179. # management network but remove it from TCP_SERVICES
  180. SSH_PORT="22"
  181. # Default IP_TABLES command path
  182. IP_TABLES="/sbin/iptables"
  183. IP_TABLES_RESTORE="/sbin/iptables-restore"
  184. IP_TABLES_RESTORE_6="/sbin/ip6tables-restore"
  185. IP_TABLES_SAVE="/sbin/iptables-save"
  186. IP_TABLES_SAVE_6="/sbin/ip6tables-save"
  187. if ! [ -x $IP_TABLES ]; then
  188. echo "$IP_TABLES is not executable or not present" >&2
  189. exit 1
  190. fi
  191. if ! [ -x $IP_TABLES_RESTORE ]; then
  192. echo "$IP_TABLES_RESTORE is not executable or not present" >&2
  193. exit 3
  194. fi
  195. if ! [ -x $IP_TABLES_RESTORE_6 ]; then
  196. echo "$IP_TABLES_RESTORE_6 is not executable or not present" >&2
  197. exit 6
  198. fi
  199. CONFIGURATION_DIR=/etc/lfirewall
  200. CONFIGURATION_FILE=${CONFIGURATION_DIR}/lfirewall.conf
  201. CONFIGURATION_LOCAL_FILE=${CONFIGURATION_DIR}/lfirewall.conf.local
  202. USER_RULES_IPTABLES=${CONFIGURATION_DIR}/iptables-user.v4
  203. USER_RULES_IPTABLES_6=${CONFIGURATION_DIR}/iptables-user.v6
  204. if [ -f $CONFIGURATION_FILE ] ; then
  205. set +u
  206. . $CONFIGURATION_FILE
  207. set -u
  208. fi
  209. if [ -f $CONFIGURATION_LOCAL_FILE ] ; then
  210. set +u
  211. . $CONFIGURATION_LOCAL_FILE
  212. set -u
  213. fi
  214. if ! /usr/sbin/ifup --no-act $NETWORK_IF > /dev/null 2>&1
  215. then
  216. echo "Network interface '$NETWORK_IF' is not present or configured" >&2
  217. exit 2
  218. fi
  219. IPTABLES_CHECK=__iptables_check_action
  220. IPTABLES_ADD=__iptable_add_action
  221. IPTABLES_INSERT=__iptable_insert_action
  222. IPTABLES_SET_POLICY=__iptable_set_policy_action
  223. export IP_TABLES
  224. export NETWORK_IF
  225. do_exec () {
  226. case $1 in
  227. __iptable_add_action)
  228. shift
  229. iptables_option=-A
  230. ;;
  231. __iptable_insert_action)
  232. shift
  233. iptables_option=-I
  234. ;;
  235. __iptable_set_policy_action)
  236. shift
  237. iptables_option=-P
  238. ;;
  239. *)
  240. echo "Nothing to be done for $1"
  241. ;;
  242. esac
  243. if [ ${verbose} -ge 1 ] ; then
  244. echo $IP_TABLES $iptables_option $*
  245. fi
  246. $IP_TABLES $iptables_option $*
  247. }
  248. do_check () {
  249. the_action=$1
  250. shift
  251. case $the_action in
  252. __iptable_add_action)
  253. iptables_option=-A
  254. ;;
  255. __iptable_insert_action)
  256. iptables_option=-I
  257. ;;
  258. __iptable_set_policy_action)
  259. return 0
  260. ;;
  261. *)
  262. echo "Nothing to be done for $1"
  263. ;;
  264. esac
  265. default_option=-C
  266. if [ ${verbose} -ge 1 ] ; then
  267. echo $do_log "$the_action:" $IP_TABLES -C $*
  268. echo $IP_TABLES -C $*
  269. fi
  270. $do_log "$the_action:" $IP_TABLES -C $*
  271. $IP_TABLES -C $*
  272. global_status=$((global_status+$?))
  273. }
  274. log_action () {
  275. echo $*
  276. }
  277. do_not_log_action () {
  278. return 0
  279. }
  280. do_log=do_not_log_action
  281. ##########################
  282. # Drops INPUT
  283. ##########################
  284. fw_exec_basic_input_rules(){
  285. local do_action
  286. do_action=$1
  287. ### Blocks any incoming
  288. $do_action $IPTABLES_SET_POLICY $IT_INPUT DROP
  289. ### Keep existing connections
  290. $do_action $IPTABLES_ADD $IT_INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  291. # Remote testing
  292. ### Allows PING
  293. $do_action $IPTABLES_ADD $IT_INPUT -p icmp -j ACCEPT
  294. ### Allows LOOPBACK
  295. $do_action $IPTABLES_ADD $IT_INPUT -i lo -j ACCEPT
  296. }
  297. ##########################
  298. # Executes the Firewall rules
  299. ##########################
  300. fw_execute () {
  301. local do_action
  302. do_action=$1
  303. #**************************************************************************#
  304. # Input traffic:
  305. #**************************************************************************#
  306. fw_exec_basic_input_rules $do_action
  307. # Services
  308. if [ -n "$TCP_SERVICES" ] ; then
  309. for PORT in $TCP_SERVICES; do
  310. $do_action $IPTABLES_ADD $IT_INPUT -p tcp --dport ${PORT} -j ACCEPT
  311. done
  312. fi
  313. if [ -n "$UDP_SERVICES" ] ; then
  314. for PORT in $UDP_SERVICES; do
  315. $do_action $IPTABLES_ADD $IT_INPUT -p udp --dport ${PORT} -j ACCEPT
  316. done
  317. fi
  318. # Remote management
  319. if [ "${NETWORK_MGMT:=UNBOUND_VARIABLE}" != "UNBOUND_VARIABLE" ] ; then
  320. $do_action $IPTABLES_ADD $IT_INPUT -p tcp --src ${NETWORK_MGMT} --dport ${SSH_PORT} -j ACCEPT
  321. else
  322. $do_action $IPTABLES_ADD $IT_INPUT -p tcp --dport ${SSH_PORT} -j ACCEPT
  323. fi
  324. # SSH
  325. if [ ! -z "${SSH_PORT:-}" ] ; then
  326. $do_action $IPTABLES_ADD $IT_OUTPUT -t filter -p tcp --dport "$SSH_PORT" -j ACCEPT
  327. $do_action $IPTABLES_ADD $IT_INPUT -t filter -p tcp --dport "$SSH_PORT" -j ACCEPT
  328. else
  329. $do_action $IPTABLES_ADD $IT_OUTPUT -t filter -p tcp --dport "22" -j ACCEPT
  330. $do_action $IPTABLES_ADD $IT_INPUT -t filter -p tcp --dport "22" -j ACCEPT
  331. fi
  332. #**************************************************************************#
  333. # NGINX
  334. #**************************************************************************#
  335. $do_action $IPTABLES_ADD $IT_INPUT -i lo -s localhost -d localhost -j ACCEPT
  336. $do_action $IPTABLES_ADD $IT_OUTPUT -o lo -s localhost -d localhost -j ACCEPT
  337. #**************************************************************************#
  338. # Output:
  339. #**************************************************************************#
  340. ### Allows LOOPBACK
  341. $do_action $IPTABLES_ADD $IT_OUTPUT -j ACCEPT -o lo
  342. ###
  343. $do_action $IPTABLES_ADD $IT_OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  344. # ICMP is permitted:
  345. ### Allows ping:
  346. $do_action $IPTABLES_ADD $IT_OUTPUT -p icmp -j ACCEPT
  347. # As well as the services we have defined:
  348. if [ -n "$REMOTE_TCP_SERVICES" ] ; then
  349. for PORT in $REMOTE_TCP_SERVICES; do
  350. $do_action $IPTABLES_ADD $IT_OUTPUT -p tcp --dport ${PORT} -j ACCEPT
  351. done
  352. fi
  353. if [ -n "$REMOTE_UDP_SERVICES" ] ; then
  354. for PORT in $REMOTE_UDP_SERVICES; do
  355. $do_action $IPTABLES_ADD $IT_OUTPUT -p udp --dport ${PORT} -j ACCEPT
  356. done
  357. fi
  358. if [ "${ACTIVATE_LOGGING:-1}" = 1 ]
  359. then
  360. # All other connections are registered in system log's backend
  361. $do_action $IPTABLES_ADD $IT_INPUT -j LOG
  362. $do_action $IPTABLES_ADD $IT_OUTPUT -j LOG
  363. $do_action $IPTABLES_ADD $IT_OUTPUT -j REJECT
  364. $do_action $IPTABLES_ADD FORWARD -j LOG
  365. fi
  366. $do_action $IPTABLES_SET_POLICY $IT_OUTPUT DROP
  367. #**************************************************************************#
  368. # DOS attack protection
  369. #**************************************************************************#
  370. # See http://rockdio.org/ayudatech/how-to-stop-small-ddos-attacks-some-basic-security-advice/
  371. #
  372. $do_action $IPTABLES_INSERT $IT_INPUT -p tcp --dport 80 -i $NETWORK_IF -m state --state NEW -m recent --set
  373. $do_action $IPTABLES_INSERT $IT_INPUT -p tcp --dport 80 -i $NETWORK_IF -m state --state NEW -m recent --update --seconds 30 --hitcount 20 -j DROP
  374. $do_action $IPTABLES_INSERT $IT_INPUT -p tcp --dport 443 -i $NETWORK_IF -m state --state NEW -m recent --set
  375. $do_action $IPTABLES_INSERT $IT_INPUT -p tcp --dport 443 -i $NETWORK_IF -m state --state NEW -m recent --update --seconds 30 --hitcount 20 -j DROP
  376. }
  377. fw_network_protection(){
  378. #**************************************************************************#
  379. # Other network protections
  380. # (some will only work with some kernel versions)
  381. #**************************************************************************#
  382. if [ "${ALLOW_IP_FORWARDING:-0}" = 0 ]
  383. then
  384. echo 0 > /proc/sys/net/ipv4/ip_forward
  385. else
  386. echo 1 > /proc/sys/net/ipv4/ip_forward
  387. fi
  388. if [ "${NETWORK_PROTECTION:-1}" = 1 ]
  389. then
  390. echo 1 > /proc/sys/net/ipv4/tcp_syncookies
  391. echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  392. echo 1 > /proc/sys/net/ipv4/conf/all/log_martians
  393. echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
  394. echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter
  395. echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
  396. echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route
  397. echo "Firewall network protection enhancement set"
  398. fi
  399. }
  400. do_this(){
  401. if [ ${verbose} -ge 1 ] ; then
  402. echo $*
  403. fi
  404. $*
  405. }
  406. translate_iptables_rule(){
  407. echo $* | sed -e 's/'$IPTABLES_ADD'/-A/g' -e 's/'$IPTABLES_INSERT'/-I/g' -e 's/'$IPTABLES_SET_POLICY'/-P/g'
  408. }
  409. ##########################
  410. # Backups user rules
  411. ##########################
  412. fw_backup_user(){
  413. $IP_TABLES_SAVE > ${USER_RULES_IPTABLES}
  414. $IP_TABLES_SAVE_6 > ${USER_RULES_IPTABLES_6}
  415. }
  416. ##########################
  417. # Restores user rules
  418. ##########################
  419. fw_restore_user(){
  420. if [ -f $USER_RULES_IPTABLES ] ; then
  421. $IP_TABLES_RESTORE < ${USER_RULES_IPTABLES}
  422. fi
  423. if [ -f $USER_RULES_IPTABLES_6 ] ; then
  424. $IP_TABLES_RESTORE_6 < ${USER_RULES_IPTABLES_6}
  425. fi
  426. }
  427. ##########################
  428. # Stop the Firewall rules
  429. ##########################
  430. do_delete () {
  431. the_action=$1
  432. shift
  433. if [ ${verbose} -gt 1 ] ; then
  434. $do_log "Trying to delete:" $(translate_iptables_rule $IP_TABLES $the_action $*)
  435. fi
  436. case $the_action in
  437. __iptable_add_action)
  438. iptables_option=-D
  439. ;;
  440. __iptable_insert_action)
  441. iptables_option=-D
  442. ;;
  443. __iptable_set_policy_action)
  444. CHAIN_NAME="$1"
  445. $do_log "DELETING: $IP_TABLES -P $CHAIN_NAME DROP"
  446. $IP_TABLES -P $CHAIN_NAME ACCEPT
  447. return 0
  448. ;;
  449. *)
  450. echo "Nothing to be done for $1"
  451. ;;
  452. esac
  453. # Checks the rule then delete it, if it exists
  454. if $IP_TABLES -C $* > /dev/null 2>&1
  455. then
  456. $IP_TABLES $iptables_option $* || echo "DID NOT EXIST: "$IP_TABLES $iptables_option $*
  457. $do_log "DELETING:" $IP_TABLES $iptables_option $*
  458. else
  459. $do_log "NOT EXISTING:" $IP_TABLES $iptables_option $*
  460. fi
  461. global_status=$((global_status+$?))
  462. }
  463. fw_stop () {
  464. global_status=0
  465. # Start will not really start but exec the "do_delete" action
  466. fw_execute do_delete
  467. if [ 0 -eq "$global_status" ] ; then
  468. echo "Firewall rules removed"
  469. return 0
  470. else
  471. echo "Some firewall rules were not removed !!!"
  472. return $global_status
  473. fi
  474. }
  475. ##########################
  476. # Drop any incoming
  477. # but keep existing ones
  478. ##########################
  479. fw_dropall(){
  480. do_this $IP_TABLES -F
  481. do_this $IP_TABLES -t nat -F
  482. do_this $IP_TABLES -t mangle -F
  483. fw_exec_basic_input_rules do_exec
  484. do_this $IP_TABLES -P FORWARD ACCEPT
  485. do_this $IP_TABLES -P OUTPUT ACCEPT
  486. }
  487. ##########################
  488. # Clear the Firewall rules
  489. ##########################
  490. fw_clear () {
  491. do_this $IP_TABLES -F
  492. do_this $IP_TABLES -X
  493. do_this $IP_TABLES -P INPUT ACCEPT
  494. do_this $IP_TABLES -P FORWARD ACCEPT
  495. do_this $IP_TABLES -P OUTPUT ACCEPT
  496. }
  497. ##########################
  498. # Test the Firewall rules
  499. ##########################
  500. fw_save () {
  501. if [ ${verbose} -ge 1 ] ; then
  502. echo "$IP_TABLES_SAVE > /etc/lfirewall/iptables.backup"
  503. fi
  504. $IP_TABLES_SAVE > /etc/lfirewall/iptables.backup
  505. }
  506. fw_restore () {
  507. fw_clear
  508. BACKUP_FILE=/etc/lfirewall/iptables.backup
  509. if [ -e $BACKUP_FILE ]; then
  510. if [ ${verbose} -ge 1 ] ; then
  511. echo "IP_TABLES_RESTORE > $BACKUP_FILE"
  512. fi
  513. $IP_TABLES_RESTORE < $BACKUP_FILE
  514. fi
  515. }
  516. fw_test () {
  517. fw_save
  518. sleep 30 && echo "Restoring previous Firewall rules" && fw_restore && echo "Done" &
  519. fw_stop
  520. fw_execute do_exec
  521. wait $(jobs -p)
  522. }
  523. if [ ${verbose} -gt 0 ] ; then
  524. do_log=log_action
  525. fi
  526. case "$1" in
  527. start|restart)
  528. echo -n "Starting firewall.."
  529. fw_stop
  530. fw_restore_user
  531. fw_execute do_exec
  532. echo "Firewall rules added"
  533. fw_network_protection
  534. echo "done."
  535. ;;
  536. saveuser)
  537. echo "###############################################################"
  538. echo "# Backing up user rules to /etc/lfirewall/iptables-user.v* #"
  539. echo "# #"
  540. echo "# Other iptables rules should not be removed unless you use #"
  541. echo "# the 'clear' command (which deletes every rule) #"
  542. echo "# If you used the 'clear' command, you can restore them by #"
  543. echo "# using the 'start' command to restore them #"
  544. echo "###############################################################"
  545. echo -n "Backing up rules"
  546. fw_backup_user
  547. echo "done."
  548. ;;
  549. stop)
  550. echo "###############################################################"
  551. echo "# Removing rules set by this tool #"
  552. echo "# #"
  553. echo "# Other iptables rules should not be removed unless you use #"
  554. echo "# the 'clear' command #"
  555. echo "# Use 'dropall' to stop any traffic and block everything. #"
  556. echo "###############################################################"
  557. fw_stop
  558. ;;
  559. clear)
  560. echo "###############################################################"
  561. echo "# Clearing any rule and let the firewall pass any packet #"
  562. echo "# #"
  563. echo "# You can restore them by using the 'start' command #"
  564. echo "###############################################################"
  565. echo -n "Clearing firewall rules.."
  566. fw_clear
  567. echo "done."
  568. ;;
  569. dropall)
  570. echo "Droping all connections !!!"
  571. fw_dropall
  572. echo "done."
  573. if [ -n "${parent_term}" ]
  574. then
  575. exec 2>${parent_term}
  576. fi
  577. echo "###############################################################" >&2
  578. echo "# IMPORTANT WARNING !!! #" >&2
  579. echo "# From now any new SSH session or INPUT #" >&2
  580. echo "# WILL BE REFUSED #" >&2
  581. echo "# If you NEED to continue working remotely, #" >&2
  582. echo "# run one of the 'lfirewall clear', 'lfirewall stop' #" >&2
  583. echo "# 'lfirewall start' commands #" >&2
  584. echo "# NOW !!! #" >&2
  585. echo "###############################################################" >&2
  586. ;;
  587. test)
  588. echo "Test Firewall rules..."
  589. echo "Previous configuration will be restore in 30 seconds"
  590. fw_test
  591. ;;
  592. status)
  593. global_status=0
  594. # Start will not really start but exec the "check" action
  595. fw_execute do_check
  596. if [ 0 -eq "$global_status" ] ; then
  597. echo "Firewall rules match configuration"
  598. exit 0
  599. else
  600. echo "Some firewall rules are not set correctly"
  601. exit $global_status
  602. fi
  603. ;;
  604. *)
  605. echo "Usage: $0 {start|dropall|stop|restart|clear|test}"
  606. echo "###############################################################"
  607. echo "# Be aware that 'dropall' will block #"
  608. echo "# all incoming/outgoing traffic !!! #"
  609. echo "###############################################################"
  610. echo "Use start option to restore all rules."
  611. echo "Use clear option to remove all traffic."
  612. echo "Use stop option to allow all traffic."
  613. exit 1
  614. ;;
  615. esac