setup 7.1 KB

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