setup 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 IPTABLES command path
  24. ###############################################################
  25. IPTABLES="/sbin/iptables"
  26. IPTABLES_RESTORE="/sbin/iptables-restore"
  27. IPTABLES_RESTORE_6="/sbin/ip6tables-restore"
  28. IPTABLES_SAVE="/sbin/iptables-save"
  29. IPTABLES_SAVE_6="/sbin/ip6tables-save"
  30. IPSET=/usr/sbin/ipset
  31. ###############################################################
  32. # iptables action definition
  33. ###############################################################
  34. # -C
  35. export IPTABLES_CHECK=__iptables_check_action
  36. # -A
  37. export IPTABLES_ADD=__iptable_add_action
  38. # -I
  39. export IPTABLES_INSERT=__iptable_insert_action
  40. # -P
  41. export IPTABLES_SET_POLICY=__iptable_set_policy_action
  42. export IPTABLESexport IPSET
  43. export NETWORK_IF
  44. ###############################################################
  45. # File and folder paths
  46. ###############################################################
  47. CONFIGURATION_FILE=${CONFIGURATION_DIR}/lfirewall.conf
  48. CONFIGURATION_LOCAL_FILE=${CONFIGURATION_DIR}/lfirewall.conf.local
  49. USER_RULES_IPTABLES=${CONFIGURATION_DIR}/iptables-user.v4
  50. USER_RULES_IPTABLES_6=${CONFIGURATION_DIR}/iptables-user.v6
  51. POST_UP_DOWN_SCRIPTS_DIR=${CONFIGURATION_DIR}/post-up-down.d
  52. POST_START_STOP_SCRIPTS_DIR=${CONFIGURATION_DIR}/post-start-stop.d
  53. ###############################################################
  54. ###############################################################
  55. # Firewall log function definition
  56. ###############################################################
  57. log_action () {
  58. echo $*
  59. }
  60. do_not_log_action () {
  61. return 0
  62. }
  63. translate_iptables_rule(){
  64. echo $* | sed -e 's/'$IPTABLES_ADD'/-A/g' -e 's/'$IPTABLES_INSERT'/-I/g' -e 's/'$IPTABLES_SET_POLICY'/-P/g'
  65. }
  66. ###############################################################
  67. # Firewall actions function definition
  68. ###############################################################
  69. get_table_information(){
  70. if [ "$1" = "-t" ]
  71. then
  72. echo "$1 $2"
  73. else
  74. echo ""
  75. fi
  76. }
  77. get_action_information(){
  78. if [ "$1" = "-t" ]
  79. then
  80. echo "$3"
  81. else
  82. echo "$1"
  83. fi
  84. }
  85. get_command_information(){
  86. if [ "$1" = "-t" ]
  87. then
  88. shift 3
  89. else
  90. shift
  91. fi
  92. echo "$*"
  93. }
  94. get_chain_information(){
  95. if [ "$1" = "-t" ]
  96. then
  97. shift 3
  98. else
  99. shift
  100. fi
  101. echo "$1"
  102. }
  103. do_exec () {
  104. local it_action=`get_action_information $*`
  105. local it_table=`get_table_information $*`
  106. local it_command=`get_command_information $*`
  107. local it_option
  108. case ${it_action} in
  109. __iptable_add_action)
  110. shift
  111. it_option=-A
  112. ;;
  113. __iptable_insert_action)
  114. shift
  115. it_option=-I
  116. ;;
  117. __iptable_set_policy_action)
  118. shift
  119. it_option=-P
  120. ;;
  121. *)
  122. echo "Nothing to be done for $1"
  123. ;;
  124. esac
  125. if ! $IPTABLES $it_table -C $it_command > /dev/null 2>&1
  126. then
  127. if [ ${verbose} -ge 1 ] ; then
  128. echo $IPTABLES $it_table $it_option $it_command
  129. fi
  130. $IPTABLES $it_table $it_option $it_command
  131. fi
  132. }
  133. do_check () {
  134. if [ ${verbose} -ge 1 ] ; then
  135. echo "do_check params='$*'"
  136. fi
  137. local it_action=`get_action_information $*`
  138. local it_table=`get_table_information $*`
  139. local it_command=`get_command_information $*`
  140. local it_option
  141. case $it_action in
  142. __iptable_add_action)
  143. it_option=-A
  144. ;;
  145. __iptable_insert_action)
  146. it_option=-I
  147. ;;
  148. __iptable_set_policy_action)
  149. return 0
  150. ;;
  151. *)
  152. echo "Nothing to be done for $1"
  153. ;;
  154. esac
  155. default_option=-C
  156. if [ ${verbose} -ge 1 ] ; then
  157. echo "it_action=$it_action"
  158. echo "it_table=$it_table"
  159. echo "it_command=$it_command"
  160. echo $do_log "$it_action:" $IPTABLES $it_table -C $it_command
  161. echo $IPTABLES $it_table -C $it_command
  162. fi
  163. $do_log "$it_action:" $IPTABLES $it_table -C $it_command
  164. $IPTABLES $it_table -C $it_command
  165. global_status=$((global_status+$?))
  166. }
  167. do_delete () {
  168. local it_action=`get_action_information $*`
  169. local it_table=`get_table_information $*`
  170. local it_command=`get_command_information $*`
  171. local it_option
  172. if [ ${verbose} -gt 1 ] ; then
  173. $do_log "Trying to delete:" \
  174. $(translate_iptables_rule $IPTABLES $it_table $it_action $it_command)
  175. fi
  176. case $it_action in
  177. __iptable_add_action)
  178. it_option=-D
  179. ;;
  180. __iptable_insert_action)
  181. it_option=-D
  182. ;;
  183. __iptable_set_policy_action)
  184. CHAIN_NAME=`get_chain_information $*`
  185. $do_log "DELETING: $IPTABLES $it_table -P $CHAIN_NAME DROP"
  186. $IPTABLES $it_table -P $CHAIN_NAME ACCEPT
  187. return 0
  188. ;;
  189. *)
  190. echo "Nothing to be done for $1"
  191. ;;
  192. esac
  193. # Checks the rule then delete it, if it exists
  194. if $IPTABLES $it_table -C $it_command > /dev/null 2>&1
  195. then
  196. $IPTABLES $it_table $it_option $it_command || echo "DID NOT EXIST: "$IPTABLES $it_option $it_command
  197. $do_log "DELETING:" $IPTABLES $it_table $it_option $it_command
  198. else
  199. $do_log "NOT EXISTING:" $IPTABLES $it_table $it_option $it_command
  200. fi
  201. global_status=$((global_status+$?))
  202. }
  203. ###############################################################
  204. # Utility functions definition
  205. ###############################################################
  206. has_parent_process(){
  207. local parent_to_search
  208. local ppid
  209. parent_to_search="${1:-}"
  210. if [ -z "${parent_to_search:-}" ]
  211. then
  212. echo "ERROR: need parent process pid as first arg" >&2
  213. return 5
  214. fi
  215. local pid
  216. pid="${2:-}"
  217. if [ -z "${pid:-}" ]
  218. then
  219. pid=$$
  220. fi
  221. if [ $parent_to_search = $pid ]
  222. then
  223. echo ${parent_to_search}
  224. return 0
  225. else if [ $pid -gt 1 ]
  226. then
  227. ppid=$(ps --pid ${pid} -o ppid= | xargs) || echo "OUT OF RANGE PID=${pid}" >&2
  228. if [ -n "$ppid" ]
  229. then
  230. if [ $ppid = $pid ]
  231. then
  232. #echo "ERROR: pid=$pid is the same as ppid=$ppid" >&2
  233. echo -1
  234. else
  235. has_parent_process ${parent_to_search} ${ppid}
  236. fi
  237. else
  238. #echo "ERROR: pid='$pid' has ppid='$ppid'" >&2
  239. echo -2
  240. fi
  241. else
  242. #echo "NOT FOUND: ${parent_to_search}" >&2
  243. echo 1
  244. fi
  245. fi
  246. return 1
  247. }
  248. find_pid_user_of(){
  249. local used_file=$1
  250. local regex="$2"
  251. lsof ${used_file} | awk 'NR>1 && $1 ~ /'${regex}'/ && !($2 in a){a[$2]++; print $2}'
  252. }
  253. find_systemctl_pids(){
  254. local shell_pid
  255. local systemctl_pid
  256. ps -elf | grep 'systemctl' | grep -v grep | awk '{print $13}' | sort -u | while read term
  257. do
  258. #echo ${shell_pid} ${systemctl_pid} >&2
  259. if [ -z "${shell_pid:-}" ]
  260. then
  261. shell_pid=$(find_pid_user_of /dev/$term '.*sh$')
  262. fi
  263. if [ -z "${systemctl_pid:-}" ]
  264. then
  265. systemctl_pid=$(find_pid_user_of /dev/$term 'systemctl')
  266. fi
  267. echo ${shell_pid} ${systemctl_pid}
  268. done
  269. }