functions.fail2ban.inc.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. if (F2B == 1) {
  3. function fail2ban($_action, $_data = null) {
  4. global $redis;
  5. global $lang;
  6. switch ($_action) {
  7. case 'get':
  8. $data = array();
  9. if ($_SESSION['mailcow_cc_role'] != "admin") {
  10. return false;
  11. }
  12. try {
  13. $data['ban_time'] = $redis->Get('F2B_BAN_TIME');
  14. $data['max_attempts'] = $redis->Get('F2B_MAX_ATTEMPTS');
  15. $data['retry_window'] = $redis->Get('F2B_RETRY_WINDOW');
  16. $wl = $redis->hGetAll('F2B_WHITELIST');
  17. if (is_array($wl)) {
  18. foreach ($wl as $key => $value) {
  19. $tmp_data[] = $key;
  20. }
  21. if (isset($tmp_data)) {
  22. $data['whitelist'] = implode(PHP_EOL, $tmp_data);
  23. }
  24. else {
  25. $data['whitelist'] = "";
  26. }
  27. }
  28. else {
  29. $data['whitelist'] = "";
  30. }
  31. }
  32. catch (RedisException $e) {
  33. $_SESSION['return'] = array(
  34. 'type' => 'danger',
  35. 'msg' => 'Redis: '.$e
  36. );
  37. return false;
  38. }
  39. return $data;
  40. break;
  41. case 'edit':
  42. if ($_SESSION['mailcow_cc_role'] != "admin") {
  43. $_SESSION['return'] = array(
  44. 'type' => 'danger',
  45. 'msg' => sprintf($lang['danger']['access_denied'])
  46. );
  47. return false;
  48. }
  49. $is_now = fail2ban('get');
  50. if (!empty($is_now)) {
  51. $ban_time = intval((isset($_data['ban_time'])) ? $_data['ban_time'] : $is_now['ban_time']);
  52. $max_attempts = intval((isset($_data['max_attempts'])) ? $_data['max_attempts'] : $is_now['active_int']);
  53. $retry_window = intval((isset($_data['retry_window'])) ? $_data['retry_window'] : $is_now['retry_window']);
  54. }
  55. else {
  56. $_SESSION['return'] = array(
  57. 'type' => 'danger',
  58. 'msg' => sprintf($lang['danger']['access_denied'])
  59. );
  60. return false;
  61. }
  62. $wl = $_data['whitelist'];
  63. $ban_time = ($ban_time < 60) ? 60 : $ban_time;
  64. $max_attempts = ($max_attempts < 1) ? 1 : $max_attempts;
  65. $retry_window = ($retry_window < 1) ? 1 : $retry_window;
  66. try {
  67. $redis->Set('F2B_BAN_TIME', $ban_time);
  68. $redis->Set('F2B_MAX_ATTEMPTS', $max_attempts);
  69. $redis->Set('F2B_RETRY_WINDOW', $retry_window);
  70. $redis->Del('F2B_WHITELIST');
  71. if(!empty($wl)) {
  72. $wl_array = array_map('trim', preg_split( "/( |,|;|\n)/", $wl));
  73. if (is_array($wl_array)) {
  74. foreach ($wl_array as $wl_item) {
  75. $cidr = explode('/', $wl_item);
  76. if (filter_var($cidr[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) && (!isset($cidr[1]) || ($cidr[1] >= 0 && $cidr[1] <= 32))) {
  77. $redis->hSet('F2B_WHITELIST', $wl_item, 1);
  78. }
  79. elseif (filter_var($cidr[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && (!isset($cidr[1]) || ($cidr[1] >= 0 && $cidr[1] <= 128))) {
  80. $redis->hSet('F2B_WHITELIST', $wl_item, 1);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. catch (RedisException $e) {
  87. $_SESSION['return'] = array(
  88. 'type' => 'danger',
  89. 'msg' => 'Redis: '.$e
  90. );
  91. return false;
  92. }
  93. $_SESSION['return'] = array(
  94. 'type' => 'success',
  95. 'msg' => sprintf($lang['success']['f2b_modified'])
  96. );
  97. break;
  98. }
  99. }
  100. }