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

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