Constantin Kaplinsky | de179d4 | 2006-04-16 06:53:44 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 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 | |
| 21 | using namespace rfb; |
| 22 | |
| 23 | IntParameter Blacklist::threshold("BlacklistThreshold", |
| 24 | "The number of unauthenticated connection attempts allowed from any " |
| 25 | "individual host before that host is black-listed", |
| 26 | 5); |
| 27 | IntParameter Blacklist::initialTimeout("BlacklistTimeout", |
| 28 | "The initial timeout applied when a host is first black-listed. " |
| 29 | "The host cannot re-attempt a connection until the timeout expires.", |
| 30 | 10); |
| 31 | |
| 32 | |
| 33 | Blacklist::Blacklist() { |
| 34 | } |
| 35 | |
| 36 | Blacklist::~Blacklist() { |
| 37 | // Free the map keys |
| 38 | BlacklistMap::iterator i; |
| 39 | for (i=blm.begin(); i!=blm.end(); i++) { |
| 40 | strFree((char*)(*i).first); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | bool Blacklist::isBlackmarked(const char* name) { |
| 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[strDup(name)] = bi; |
| 55 | i = blm.find(name); |
| 56 | } |
| 57 | |
| 58 | // Entry exists - has it reached the threshold yet? |
| 59 | if ((*i).second.marks >= threshold) { |
| 60 | // Yes - entry is blocked - has the timeout expired? |
| 61 | time_t now = time(0); |
| 62 | if (now >= (*i).second.blockUntil) { |
| 63 | // Timeout has expired. Reset timeout and allow |
| 64 | // a re-try. |
| 65 | (*i).second.blockUntil = now + (*i).second.blockTimeout; |
| 66 | (*i).second.blockTimeout = (*i).second.blockTimeout * 2; |
| 67 | return false; |
| 68 | } |
| 69 | // Blocked and timeout still in effect - reject! |
| 70 | return true; |
| 71 | } |
| 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 | |
| 80 | void Blacklist::clearBlackmark(const char* name) { |
| 81 | BlacklistMap::iterator i = blm.find(name); |
| 82 | if (i != blm.end()) { |
| 83 | strFree((char*)(*i).first); |
| 84 | blm.erase(i); |
| 85 | } |
| 86 | } |