functions.fail2ban.inc.php 3.3 KB

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