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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // Free the map keys
  42. BlacklistMap::iterator i;
  43. for (i=blm.begin(); i!=blm.end(); i++) {
  44. strFree((char*)(*i).first);
  45. }
  46. }
  47. bool Blacklist::isBlackmarked(const char* name) {
  48. if (!enabled)
  49. return false;
  50. BlacklistMap::iterator i = blm.find(name);
  51. if (i == blm.end()) {
  52. // Entry is not already black-marked.
  53. // Create the entry unmarked, unblocked,
  54. // with suitable defaults set.
  55. BlacklistInfo bi;
  56. bi.marks = 1;
  57. bi.blockUntil = 0;
  58. bi.blockTimeout = initialTimeout;
  59. blm[strDup(name)] = bi;
  60. i = blm.find(name);
  61. }
  62. // Entry exists - has it reached the threshold yet?
  63. if ((*i).second.marks >= threshold) {
  64. // Yes - entry is blocked - has the timeout expired?
  65. time_t now = time(0);
  66. if (now >= (*i).second.blockUntil) {
  67. // Timeout has expired. Reset timeout and allow
  68. // a re-try.
  69. (*i).second.blockUntil = now + (*i).second.blockTimeout;
  70. (*i).second.blockTimeout = (*i).second.blockTimeout * 2;
  71. return false;
  72. }
  73. // Blocked and timeout still in effect - reject!
  74. return true;
  75. }
  76. // We haven't reached the threshold yet.
  77. // Increment the black-mark counter but allow
  78. // the entry to pass.
  79. (*i).second.marks++;
  80. return false;
  81. }
  82. void Blacklist::clearBlackmark(const char* name) {
  83. BlacklistMap::iterator i = blm.find(name);
  84. if (i != blm.end()) {
  85. strFree((char*)(*i).first);
  86. blm.erase(i);
  87. }
  88. }