setup 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. get_table_information(){
  66. if [ "$1" = "-t" ]
  67. then
  68. echo "$1 $2"
  69. else
  70. echo ""
  71. fi
  72. }
  73. get_action_information(){
  74. if [ "$1" = "-t" ]
  75. then
  76. echo "$3"
  77. else
  78. echo "$1"
  79. fi
  80. }
  81. get_command_information(){
  82. if [ "$1" = "-t" ]
  83. then
  84. shift 3
  85. else
  86. shift
  87. fi
  88. echo "$*"
  89. }
  90. get_chain_information(){
  91. if [ "$1" = "-t" ]
  92. then
  93. shift 3
  94. else
  95. shift
  96. fi
  97. echo "$1"
  98. }
  99. do_exec () {
  100. local it_action=`get_action_information $*`
  101. local it_table=`get_table_information $*`
  102. local it_command=`get_command_information $*`
  103. local it_option
  104. case ${it_action} in
  105. __iptable_add_action)
  106. shift
  107. it_option=-A
  108. ;;
  109. __iptable_insert_action)
  110. shift
  111. it_option=-I
  112. ;;
  113. __iptable_set_policy_action)
  114. shift
  115. it_option=-P
  116. ;;
  117. *)
  118. echo "Nothing to be done for $1"
  119. ;;
  120. esac
  121. if [ ${verbose} -ge 1 ] ; then
  122. echo $IP_TABLES $it_table $it_option $it_command
  123. fi
  124. if ! $IP_TABLES $it_table -C $it_command > /dev/null 2>&1
  125. then
  126. $IP_TABLES $it_table $it_option $it_command
  127. fi
  128. }
  129. do_check () {
  130. local it_action=`get_action_information $*`
  131. local it_table=`get_table_information $*`
  132. local it_command=`get_command_information $*`
  133. local it_option
  134. case $it_action in
  135. __iptable_add_action)
  136. it_option=-A
  137. ;;
  138. __iptable_insert_action)
  139. it_option=-I
  140. ;;
  141. __iptable_set_policy_action)
  142. return 0
  143. ;;
  144. *)
  145. echo "Nothing to be done for $1"
  146. ;;
  147. esac
  148. default_option=-C
  149. if [ ${verbose} -ge 1 ] ; then
  150. echo $do_log "$it_action:" $IP_TABLES $it_table -C $it_command
  151. echo $IP_TABLES $it_table -C $it_command
  152. fi
  153. $do_log "$it_action:" $IP_TABLES -C $it_command
  154. $IP_TABLES $it_table -C $it_command
  155. global_status=$((global_status+$?))
  156. }
  157. do_delete () {
  158. local it_action=`get_action_information $*`
  159. local it_table=`get_table_information $*`
  160. local it_command=`get_command_information $*`
  161. local it_option
  162. if [ ${verbose} -gt 1 ] ; then
  163. $do_log "Trying to delete:" \
  164. $(translate_iptables_rule $IP_TABLES $it_table $it_action $it_command)
  165. fi
  166. case $it_action in
  167. __iptable_add_action)
  168. it_option=-D
  169. ;;
  170. __iptable_insert_action)
  171. it_option=-D
  172. ;;
  173. __iptable_set_policy_action)
  174. CHAIN_NAME=`get_chain_information $*`
  175. $do_log "DELETING: $IP_TABLES $it_table -P $CHAIN_NAME DROP"
  176. $IP_TABLES $it_table -P $CHAIN_NAME ACCEPT
  177. return 0
  178. ;;
  179. *)
  180. echo "Nothing to be done for $1"
  181. ;;
  182. esac
  183. # Checks the rule then delete it, if it exists
  184. if $IP_TABLES $it_table -C $it_command > /dev/null 2>&1
  185. then
  186. $IP_TABLES $it_table $it_option $it_command || echo "DID NOT EXIST: "$IP_TABLES $it_option $it_command
  187. $do_log "DELETING:" $IP_TABLES $it_table $it_option $it_command
  188. else
  189. $do_log "NOT EXISTING:" $IP_TABLES $it_table $it_option $it_command
  190. fi
  191. global_status=$((global_status+$?))
  192. }
  193. ###############################################################
  194. # Utility functions definition
  195. ###############################################################
  196. has_parent_process(){
  197. local parent_to_search
  198. local ppid
  199. parent_to_search="${1:-}"
  200. if [ -z "${parent_to_search:-}" ]
  201. then
  202. echo "ERROR: need parent process pid as first arg" >&2
  203. return 5
  204. fi
  205. local pid
  206. pid="${2:-}"
  207. if [ -z "${pid:-}" ]
  208. then
  209. pid=$$
  210. fi
  211. if [ $parent_to_search = $pid ]
  212. then
  213. echo ${parent_to_search}
  214. return 0
  215. else if [ $pid -gt 1 ]
  216. then
  217. ppid=$(ps --pid ${pid} -o ppid= | xargs) || echo "OUT OF RANGE PID=${pid}" >&2
  218. if [ -n "$ppid" ]
  219. then
  220. if [ $ppid = $pid ]
  221. then
  222. #echo "ERROR: pid=$pid is the same as ppid=$ppid" >&2
  223. echo -1
  224. else
  225. has_parent_process ${parent_to_search} ${ppid}
  226. fi
  227. else
  228. #echo "ERROR: pid='$pid' has ppid='$ppid'" >&2
  229. echo -2
  230. fi
  231. else
  232. #echo "NOT FOUND: ${parent_to_search}" >&2
  233. echo 1
  234. fi
  235. fi
  236. return 1
  237. }
  238. find_pid_user_of(){
  239. local used_file=$1
  240. local regex="$2"
  241. lsof ${used_file} | awk 'NR>1 && $1 ~ /'${regex}'/ && !($2 in a){a[$2]++; print $2}'
  242. }
  243. find_systemctl_pids(){
  244. local shell_pid
  245. local systemctl_pid
  246. ps -elf | grep 'systemctl' | grep -v grep | awk '{print $13}' | sort -u | while read term
  247. do
  248. #echo ${shell_pid} ${systemctl_pid} >&2
  249. if [ -z "${shell_pid:-}" ]
  250. then
  251. shell_pid=$(find_pid_user_of /dev/$term '.*sh$')
  252. fi
  253. if [ -z "${systemctl_pid:-}" ]
  254. then
  255. systemctl_pid=$(find_pid_user_of /dev/$term 'systemctl')
  256. fi
  257. echo ${shell_pid} ${systemctl_pid}
  258. done
  259. }