setup 3.4 KB

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