setup 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. # File and folder paths
  32. ###############################################################
  33. CONFIGURATION_FILE=${CONFIGURATION_DIR}/lfirewall.conf
  34. CONFIGURATION_LOCAL_FILE=${CONFIGURATION_DIR}/lfirewall.conf.local
  35. USER_RULES_IPTABLES=${CONFIGURATION_DIR}/iptables-user.v4
  36. USER_RULES_IPTABLES_6=${CONFIGURATION_DIR}/iptables-user.v6
  37. ###############################################################
  38. ###############################################################
  39. # Utility functions definition
  40. ###############################################################
  41. has_parent_process(){
  42. local parent_to_search
  43. local ppid
  44. parent_to_search="${1:-}"
  45. if [ -z "${parent_to_search:-}" ]
  46. then
  47. echo "ERROR: need parent process pid as first arg" >&2
  48. return 5
  49. fi
  50. local pid
  51. pid="${2:-}"
  52. if [ -z "${pid:-}" ]
  53. then
  54. pid=$$
  55. fi
  56. if [ $parent_to_search = $pid ]
  57. then
  58. echo ${parent_to_search}
  59. return 0
  60. else if [ $pid -gt 1 ]
  61. then
  62. ppid=$(ps --pid ${pid} -o ppid= | xargs) || echo "OUT OF RANGE PID=${pid}" >&2
  63. if [ -n "$ppid" ]
  64. then
  65. if [ $ppid = $pid ]
  66. then
  67. #echo "ERROR: pid=$pid is the same as ppid=$ppid" >&2
  68. echo -1
  69. else
  70. has_parent_process ${parent_to_search} ${ppid}
  71. fi
  72. else
  73. #echo "ERROR: pid='$pid' has ppid='$ppid'" >&2
  74. echo -2
  75. fi
  76. else
  77. #echo "NOT FOUND: ${parent_to_search}" >&2
  78. echo 1
  79. fi
  80. fi
  81. return 1
  82. }
  83. find_pid_user_of(){
  84. local used_file=$1
  85. local regex="$2"
  86. lsof ${used_file} | awk 'NR>1 && $1 ~ /'${regex}'/ && !($2 in a){a[$2]++; print $2}'
  87. }
  88. find_systemctl_pids(){
  89. local shell_pid
  90. local systemctl_pid
  91. ps -elf | grep 'systemctl' | grep -v grep | awk '{print $13}' | sort -u | while read term
  92. do
  93. #echo ${shell_pid} ${systemctl_pid} >&2
  94. if [ -z "${shell_pid:-}" ]
  95. then
  96. shell_pid=$(find_pid_user_of /dev/$term '.*sh$')
  97. fi
  98. if [ -z "${systemctl_pid:-}" ]
  99. then
  100. systemctl_pid=$(find_pid_user_of /dev/$term 'systemctl')
  101. fi
  102. echo ${shell_pid} ${systemctl_pid}
  103. done
  104. }