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.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include <rfb/Blacklist.h>
  19. #include <rfb/Configuration.h>
  20. using namespace rfb;
  21. IntParameter threshold("BlacklistThreshold",
  22. "The number of unauthenticated connection attempts "
  23. "allowed from any individual host before that host "
  24. "is black-listed",
  25. 5);
  26. IntParameter initialTimeout("BlacklistTimeout",
  27. "The initial timeout applied when a host is "
  28. "first black-listed. The host cannot re-attempt "
  29. "a connection until the timeout expires.",
  30. 10);
  31. Blacklist::Blacklist() {
  32. }
  33. Blacklist::~Blacklist() {
  34. // Free the map keys
  35. BlacklistMap::iterator i;
  36. for (i=blm.begin(); i!=blm.end(); i++) {
  37. strFree((char*)(*i).first);
  38. }
  39. }
  40. bool Blacklist::isBlackmarked(const char* name) {
  41. BlacklistMap::iterator i = blm.find(name);
  42. if (i == blm.end()) {
  43. // Entry is not already black-marked.
  44. // Create the entry unmarked, unblocked,
  45. // with suitable defaults set.
  46. BlacklistInfo bi;
  47. bi.marks = 1;
  48. bi.blockUntil = 0;
  49. bi.blockTimeout = initialTimeout;
  50. blm[strDup(name)] = bi;
  51. i = blm.find(name);
  52. }
  53. // Entry exists - has it reached the threshold yet?
  54. if ((*i).second.marks >= threshold) {
  55. // Yes - entry is blocked - has the timeout expired?
  56. time_t now = time(0);
  57. if (now >= (*i).second.blockUntil) {
  58. // Timeout has expired. Reset timeout and allow
  59. // a re-try.
  60. (*i).second.blockUntil = now + (*i).second.blockTimeout;
  61. (*i).second.blockTimeout = (*i).second.blockTimeout * 2;
  62. return false;
  63. }
  64. // Blocked and timeout still in effect - reject!
  65. return true;
  66. }
  67. // We haven't reached the threshold yet.
  68. // Increment the black-mark counter but allow
  69. // the entry to pass.
  70. (*i).second.marks++;
  71. return false;
  72. }
  73. void Blacklist::clearBlackmark(const char* name) {
  74. BlacklistMap::iterator i = blm.find(name);
  75. if (i != blm.end()) {
  76. strFree((char*)(*i).first);
  77. blm.erase(i);
  78. }
  79. }