setup 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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-up-down.d
  52. ###############################################################
  53. ###############################################################
  54. # Utility functions definition
  55. ###############################################################
  56. has_parent_process(){
  57. local parent_to_search
  58. local ppid
  59. parent_to_search="${1:-}"
  60. if [ -z "${parent_to_search:-}" ]
  61. then
  62. echo "ERROR: need parent process pid as first arg" >&2
  63. return 5
  64. fi
  65. local pid
  66. pid="${2:-}"
  67. if [ -z "${pid:-}" ]
  68. then
  69. pid=$$
  70. fi
  71. if [ $parent_to_search = $pid ]
  72. then
  73. echo ${parent_to_search}
  74. return 0
  75. else if [ $pid -gt 1 ]
  76. then
  77. ppid=$(ps --pid ${pid} -o ppid= | xargs) || echo "OUT OF RANGE PID=${pid}" >&2
  78. if [ -n "$ppid" ]
  79. then
  80. if [ $ppid = $pid ]
  81. then
  82. #echo "ERROR: pid=$pid is the same as ppid=$ppid" >&2
  83. echo -1
  84. else
  85. has_parent_process ${parent_to_search} ${ppid}
  86. fi
  87. else
  88. #echo "ERROR: pid='$pid' has ppid='$ppid'" >&2
  89. echo -2
  90. fi
  91. else
  92. #echo "NOT FOUND: ${parent_to_search}" >&2
  93. echo 1
  94. fi
  95. fi
  96. return 1
  97. }
  98. find_pid_user_of(){
  99. local used_file=$1
  100. local regex="$2"
  101. lsof ${used_file} | awk 'NR>1 && $1 ~ /'${regex}'/ && !($2 in a){a[$2]++; print $2}'
  102. }
  103. find_systemctl_pids(){
  104. local shell_pid
  105. local systemctl_pid
  106. ps -elf | grep 'systemctl' | grep -v grep | awk '{print $13}' | sort -u | while read term
  107. do
  108. #echo ${shell_pid} ${systemctl_pid} >&2
  109. if [ -z "${shell_pid:-}" ]
  110. then
  111. shell_pid=$(find_pid_user_of /dev/$term '.*sh$')
  112. fi
  113. if [ -z "${systemctl_pid:-}" ]
  114. then
  115. systemctl_pid=$(find_pid_user_of /dev/$term 'systemctl')
  116. fi
  117. echo ${shell_pid} ${systemctl_pid}
  118. done
  119. }