setup 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. ###############################################################
  2. # iptables chain names
  3. ###############################################################
  4. IT_INPUT=INPUT
  5. IT_INPUT_LOG=LOGINPUT
  6. IT_OUTPUT=OUTPUT
  7. IT_OUTPUT_LOG=LOGOUTPUT
  8. IT_POSTROUTING=POSTROUTING
  9. IT_PREROUTING=PREROUTING
  10. PATH=/bin:/sbin:/usr/bin:/usr/sbin
  11. #Defautl network interface
  12. NETWORK_IF=eth0
  13. # Services that the system will offer to the network
  14. TCP_SERVICES="22" # SSH only
  15. UDP_SERVICES=""
  16. # Services the system will use from the network
  17. REMOTE_TCP_SERVICES="80 443" # web browsing
  18. REMOTE_UDP_SERVICES="53" # DNS
  19. # Port used for the SSH service, define this is you have setup a
  20. # management network but remove it from TCP_SERVICES
  21. SSH_PORT="22"
  22. ###############################################################
  23. # Default IP_TABLES command path
  24. ###############################################################
  25. IP_TABLES="/sbin/iptables"
  26. IP_TABLES_RESTORE="/sbin/iptables-restore"
  27. IP_TABLES_RESTORE_6="/sbin/ip6tables-restore"
  28. IP_TABLES_SAVE="/sbin/iptables-save"
  29. IP_TABLES_SAVE_6="/sbin/ip6tables-save"
  30. ###############################################################
  31. # iptables action definition
  32. ###############################################################
  33. # -C
  34. export IPTABLES_CHECK=__iptables_check_action
  35. # -A
  36. export IPTABLES_ADD=__iptable_add_action
  37. # -I
  38. export IPTABLES_INSERT=__iptable_insert_action
  39. # -P
  40. export IPTABLES_SET_POLICY=__iptable_set_policy_action
  41. export IP_TABLES
  42. export NETWORK_IF
  43. ###############################################################
  44. # File and folder paths
  45. ###############################################################
  46. CONFIGURATION_FILE=${CONFIGURATION_DIR}/lfirewall.conf
  47. CONFIGURATION_LOCAL_FILE=${CONFIGURATION_DIR}/lfirewall.conf.local
  48. USER_RULES_IPTABLES=${CONFIGURATION_DIR}/iptables-user.v4
  49. USER_RULES_IPTABLES_6=${CONFIGURATION_DIR}/iptables-user.v6
  50. POST_UP_DOWN_SCRIPTS_DIR=${CONFIGURATION_DIR}/post-up-down.d
  51. POST_START_STOP_SCRIPTS_DIR=${CONFIGURATION_DIR}/post-start-stop.d
  52. ###############################################################
  53. ###############################################################
  54. # Firewall log function definition
  55. ###############################################################
  56. log_action () {
  57. echo $*
  58. }
  59. do_not_log_action () {
  60. return 0
  61. }
  62. ###############################################################
  63. # Firewall actions function definition
  64. ###############################################################
  65. do_exec () {
  66. case $1 in
  67. __iptable_add_action)
  68. shift
  69. iptables_option=-A
  70. ;;
  71. __iptable_insert_action)
  72. shift
  73. iptables_option=-I
  74. ;;
  75. __iptable_set_policy_action)
  76. shift
  77. iptables_option=-P
  78. ;;
  79. *)
  80. echo "Nothing to be done for $1"
  81. ;;
  82. esac
  83. if [ ${verbose} -ge 1 ] ; then
  84. echo $IP_TABLES $iptables_option $*
  85. fi
  86. if ! $IP_TABLES -C $* > /dev/null 2>&1
  87. then
  88. $IP_TABLES $iptables_option $*
  89. fi
  90. }
  91. do_check () {
  92. local the_action
  93. the_action=$1
  94. shift
  95. case $the_action in
  96. __iptable_add_action)
  97. iptables_option=-A
  98. ;;
  99. __iptable_insert_action)
  100. iptables_option=-I
  101. ;;
  102. __iptable_set_policy_action)
  103. return 0
  104. ;;
  105. *)
  106. echo "Nothing to be done for $1"
  107. ;;
  108. esac
  109. default_option=-C
  110. if [ ${verbose} -ge 1 ] ; then
  111. echo $do_log "$the_action:" $IP_TABLES -C $*
  112. echo $IP_TABLES -C $*
  113. fi
  114. $do_log "$the_action:" $IP_TABLES -C $*
  115. $IP_TABLES -C $*
  116. global_status=$((global_status+$?))
  117. }
  118. do_delete () {
  119. local the_action
  120. the_action=$1
  121. shift
  122. if [ ${verbose} -gt 1 ] ; then
  123. $do_log "Trying to delete:" $(translate_iptables_rule $IP_TABLES $the_action $*)
  124. fi
  125. case $the_action in
  126. __iptable_add_action)
  127. iptables_option=-D
  128. ;;
  129. __iptable_insert_action)
  130. iptables_option=-D
  131. ;;
  132. __iptable_set_policy_action)
  133. CHAIN_NAME="$1"
  134. $do_log "DELETING: $IP_TABLES -P $CHAIN_NAME DROP"
  135. $IP_TABLES -P $CHAIN_NAME ACCEPT
  136. return 0
  137. ;;
  138. *)
  139. echo "Nothing to be done for $1"
  140. ;;
  141. esac
  142. # Checks the rule then delete it, if it exists
  143. if $IP_TABLES -C $* > /dev/null 2>&1
  144. then
  145. $IP_TABLES $iptables_option $* || echo "DID NOT EXIST: "$IP_TABLES $iptables_option $*
  146. $do_log "DELETING:" $IP_TABLES $iptables_option $*
  147. else
  148. $do_log "NOT EXISTING:" $IP_TABLES $iptables_option $*
  149. fi
  150. global_status=$((global_status+$?))
  151. }
  152. ###############################################################
  153. # Utility functions definition
  154. ###############################################################
  155. has_parent_process(){
  156. local parent_to_search
  157. local ppid
  158. parent_to_search="${1:-}"
  159. if [ -z "${parent_to_search:-}" ]
  160. then
  161. echo "ERROR: need parent process pid as first arg" >&2
  162. return 5
  163. fi
  164. local pid
  165. pid="${2:-}"
  166. if [ -z "${pid:-}" ]
  167. then
  168. pid=$$
  169. fi
  170. if [ $parent_to_search = $pid ]
  171. then
  172. echo ${parent_to_search}
  173. return 0
  174. else if [ $pid -gt 1 ]
  175. then
  176. ppid=$(ps --pid ${pid} -o ppid= | xargs) || echo "OUT OF RANGE PID=${pid}" >&2
  177. if [ -n "$ppid" ]
  178. then
  179. if [ $ppid = $pid ]
  180. then
  181. #echo "ERROR: pid=$pid is the same as ppid=$ppid" >&2
  182. echo -1
  183. else
  184. has_parent_process ${parent_to_search} ${ppid}
  185. fi
  186. else
  187. #echo "ERROR: pid='$pid' has ppid='$ppid'" >&2
  188. echo -2
  189. fi
  190. else
  191. #echo "NOT FOUND: ${parent_to_search}" >&2
  192. echo 1
  193. fi
  194. fi
  195. return 1
  196. }
  197. find_pid_user_of(){
  198. local used_file=$1
  199. local regex="$2"
  200. lsof ${used_file} | awk 'NR>1 && $1 ~ /'${regex}'/ && !($2 in a){a[$2]++; print $2}'
  201. }
  202. find_systemctl_pids(){
  203. local shell_pid
  204. local systemctl_pid
  205. ps -elf | grep 'systemctl' | grep -v grep | awk '{print $13}' | sort -u | while read term
  206. do
  207. #echo ${shell_pid} ${systemctl_pid} >&2
  208. if [ -z "${shell_pid:-}" ]
  209. then
  210. shell_pid=$(find_pid_user_of /dev/$term '.*sh$')
  211. fi
  212. if [ -z "${systemctl_pid:-}" ]
  213. then
  214. systemctl_pid=$(find_pid_user_of /dev/$term 'systemctl')
  215. fi
  216. echo ${shell_pid} ${systemctl_pid}
  217. done
  218. }