You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Blacklist.cxx 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <rfb/Blacklist.h>
  22. #include <rfb/Configuration.h>
  23. using namespace rfb;
  24. BoolParameter enabled("UseBlacklist",
  25. "Temporarily reject connections from a host if it "
  26. "repeatedly fails to authenticate.",
  27. true);
  28. IntParameter threshold("BlacklistThreshold",
  29. "The number of unauthenticated connection attempts "
  30. "allowed from any individual host before that host "
  31. "is black-listed",
  32. 5);
  33. IntParameter initialTimeout("BlacklistTimeout",
  34. "The initial timeout applied when a host is "
  35. "first black-listed. The host cannot re-attempt "
  36. "a connection until the timeout expires.",
  37. 10);
  38. Blacklist::Blacklist() {
  39. }
  40. Blacklist::~Blacklist() {
  41. }
  42. bool Blacklist::isBlackmarked(const char* name) {
  43. if (!enabled)
  44. return false;
  45. BlacklistMap::iterator i = blm.find(name);
  46. if (i == blm.end()) {
  47. // Entry is not already black-marked.
  48. // Create the entry unmarked, unblocked,
  49. // with suitable defaults set.
  50. BlacklistInfo bi;
  51. bi.marks = 1;
  52. bi.blockUntil = 0;
  53. bi.blockTimeout = initialTimeout;
  54. blm[name] = bi;
  55. i = blm.find(name);
  56. }
  57. // Entry exists - has it reached the threshold yet?
  58. if ((*i).second.marks >= threshold) {
  59. // Yes - entry is blocked - has the timeout expired?
  60. time_t now = time(0);
  61. if (now >= (*i).second.blockUntil) {
  62. // Timeout has expired. Reset timeout and allow
  63. // a re-try.
  64. (*i).second.blockUntil = now + (*i).second.blockTimeout;
  65. (*i).second.blockTimeout = (*i).second.blockTimeout * 2;
  66. return false;
  67. }
  68. // Blocked and timeout still in effect - reject!
  69. return true;
  70. }
  71. // We haven't reached the threshold yet.
  72. // Increment the black-mark counter but allow
  73. // the entry to pass.
  74. (*i).second.marks++;
  75. return false;
  76. }
  77. void Blacklist::clearBlackmark(const char* name) {
  78. blm.erase(name);
  79. }