blob: 4c4f95b259822ea9cf0ac69d294abca44f0f2936 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2003 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
21using namespace rfb;
22
23IntParameter Blacklist::threshold("BlacklistThreshold",
24 "The number of unauthenticated connection attempts allowed from any "
25 "individual host before that host is black-listed",
26 5);
27IntParameter 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
33Blacklist::Blacklist() {
34}
35
36Blacklist::~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
44bool 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
80void 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}