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

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