-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecuritymanager.cpp
More file actions
2640 lines (2163 loc) · 57.5 KB
/
securitymanager.cpp
File metadata and controls
2640 lines (2163 loc) · 57.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** securitymanager.cpp
**
** Copyright © Quazaa Development Team, 2009-2014.
** This file is part of the Quazaa Security Library (quazaa.sourceforge.net)
**
** The Quazaa Security Library is free software; this file may be used under the terms of the GNU
** General Public License version 3.0 or later as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** The Quazaa Security Library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**
** Please review the following information to ensure the GNU General Public
** License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** You should have received a copy of the GNU General Public License version
** 3.0 along with the Quazaa Security Library; if not, write to the Free Software Foundation,
** Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <QDir>
#include <QDateTime>
#include <QMetaType>
#include <QDataStream>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include "useragent.h"
#include "securitymanager.h"
#include "debug_new.h"
const QString Security::Manager::sXMLNameSpace = "http://www.shareaza.com/schemas/Security.xsd";
Security::Manager securityManager;
using namespace Security;
Manager::Manager() :
m_bEnableCountries( false ),
m_bLogIPCheckHits( false ),
m_tRuleExpiryInterval( 0 ),
m_bUnsaved( false ),
m_bShutDown( false ),
m_bExpiryRequested( false ),
m_bDenyPrivateIPs( false ),
m_bDenyPolicy( false )
{
// QApplication hasn't been started when the global definition creates this object, so
// no qt specific calls (for example connect() or emit signal) may be used over here.
// See initialize() for that kind of initializations.
}
Manager::~Manager()
{
}
Manager::RuleVectorPos Manager::count() const
{
return m_vRules.size();
}
bool Manager::denyPolicy() const
{
return m_bDenyPolicy;
}
void Manager::setDenyPolicy( bool bDenyPolicy )
{
m_oRWLock.lockForWrite();
if ( m_bDenyPolicy != bDenyPolicy )
{
m_bDenyPolicy = bDenyPolicy;
m_bUnsaved = true;
}
m_oRWLock.unlock();
}
bool Manager::check( const Rule* const pRule ) const
{
m_oRWLock.lockForRead();
bool bReturn = pRule && find( pRule->m_idUUID ) != m_vRules.size();
m_oRWLock.unlock();
return bReturn;
}
bool Manager::add( Rule* pRule , bool bDoSanityCheck )
{
if ( !pRule )
{
return false;
}
QWriteLocker writeLock( &m_oRWLock );
RuleType::Type nType = pRule->type();
RuleAction::Action nAction = pRule->m_nAction;
// check for invalid rules
Q_ASSERT( nType > 0 && nType < RuleType::NoOfTypes &&
nAction >= 0 && nAction < RuleAction::NoOfActions );
Q_ASSERT( !pRule->m_idUUID.isNull() );
const RuleVectorPos nExRule = find( pRule->m_idUUID );
if ( nExRule != m_vRules.size() )
{
// we do not allow 2 rules by the same UUID
remove( nExRule );
}
// REMOVE for beta 1
#ifdef _DEBUG
for ( RuleVectorPos i = 0; i < m_vRules.size(); ++i )
{
Q_ASSERT( m_vRules[i] );
Rule* pTestRule = m_vRules[i];
if ( pTestRule->type() <= 0 ||
pTestRule->type() >= RuleType::NoOfTypes ||
pTestRule->totalCount() < 0 )
{
Q_ASSERT( pTestRule->type() > 0 && pTestRule->type() < RuleType::NoOfTypes );
Q_ASSERT( pTestRule->totalCount() >= 0 );
}
}
#endif
bool bNewAddress = false;
bool bNewHit = false;
// Special treatment for the different types of rules
switch ( nType )
{
case RuleType::IPAddress:
{
const quint32 nIPHash = m_oIPHasher( ( ( IPRule* )pRule )->IP() );
IPMap::iterator it = m_lmIPs.find( nIPHash );
if ( it != m_lmIPs.end() ) // there is a conflicting rule in our map
{
pRule->mergeInto( ( *it ).second );
delete pRule;
pRule = NULL;
}
else
{
m_lmIPs[ nIPHash ] = ( IPRule* )pRule;
bNewAddress = true;
}
}
break;
case RuleType::IPAddressRange:
{
IPRangeRule* pRange = ( IPRangeRule* )pRule;
insertRange( pRange );
pRule = pRange; // set pRule to NULL if pRange has been set to NULL
bNewAddress = pRule; // evaluates to false if the previous method sets pRule to NULL
}
break;
#if SECURITY_ENABLE_GEOIP
case RuleType::Country:
{
const quint32 nCountryHash = m_oCountryHasher( pRule->contentString() );
CountryMap::iterator it = m_lmCountries.find( nCountryHash );
if ( it != m_lmCountries.end() ) // there is a conflicting rule in our map
{
pRule->mergeInto( ( *it ).second );
delete pRule;
pRule = NULL;
}
else
{
m_lmCountries[ nCountryHash ] = ( CountryRule* )pRule;
bNewAddress = true;
}
// not all stl implementation guarantee this to have constant time...
m_bEnableCountries = m_lmCountries.size();
}
break;
#endif // SECURITY_ENABLE_GEOIP
case RuleType::Hash:
{
const HashSet& vHashes = ( ( HashRule* )pRule )->getHashes();
RuleVectorPos nPos = find( vHashes );
if ( nPos != m_vRules.size() )
{
pRule->mergeInto( m_vRules[nPos] );
// there is no point on adding a rule for the same content twice,
// as that content is already blocked.
delete pRule;
pRule = NULL;
}
else
{
// If there isn't a rule for this content or there is a rule for
// similar but not 100% identical content, add hashes to map.
for ( quint8 i = 0, nSize = vHashes.size(); i < nSize; ++i )
{
if ( vHashes[i] )
{
uint nKey = qHash( vHashes[i]->rawValue() );
m_lmmHashes.insert( HashPair( nKey, ( HashRule* )pRule ) );
}
}
bNewHit = true;
}
}
break;
case RuleType::RegularExpression:
{
const RegExpVectorPos nSize = m_vRegularExpressions.size();
if ( nSize )
{
RegularExpressionRule* const * const pRegExpRules = &m_vRegularExpressions[0];
for ( RegExpVectorPos i = 0; i < nSize; ++i )
{
if ( pRegExpRules[i]->contentString() == pRule->contentString() )
{
pRule->mergeInto( pRegExpRules[i] );
delete pRule;
pRule = NULL;
break;
}
}
}
if ( pRule )
{
m_vRegularExpressions.push_back( ( RegularExpressionRule* )pRule );
bNewHit = true;
}
}
break;
case RuleType::Content:
{
const ContentVectorPos nSize = m_vContents.size();
if ( nSize )
{
ContentRule* const * const pContentRules = &m_vContents[0];
for ( ContentVectorPos i = 0; i < nSize; ++i )
{
if ( pContentRules[i]->contentString() == pRule->contentString() &&
pContentRules[i]->getAll() == ( ( ContentRule* )pRule )->getAll() )
{
pRule->mergeInto( pContentRules[i] );
delete pRule;
pRule = NULL;
break;
}
}
}
if ( pRule )
{
m_vContents.push_back( ( ContentRule* )pRule );
bNewHit = true;
}
}
break;
case RuleType::UserAgent:
{
const UserAgentVectorPos nSize = m_vUserAgents.size();
if ( nSize )
{
UserAgentRule* const * const pUserAgentRules = &m_vUserAgents[0];
for ( UserAgentVectorPos i = 0; i < nSize; ++i )
{
if ( pUserAgentRules[i]->contentString() == pRule->contentString() )
{
pRule->mergeInto( pUserAgentRules[i] );
delete pRule;
pRule = NULL;
break;
}
}
}
if ( pRule )
{
m_vUserAgents.push_back( ( UserAgentRule* )pRule );
}
}
break;
default:
#if SECURITY_ENABLE_GEOIP
Q_ASSERT( false );
#else
Q_ASSERT( type == RuleType::Country );
#endif // SECURITY_ENABLE_GEOIP
}
// a rule has been added and we might require saving
m_bUnsaved = true;
if ( pRule )
{
if ( bNewAddress )
{
if ( nType == RuleType::IPAddress )
{
m_oMissCache.erase( ( ( IPRule* )pRule )->IP() );
}
else
{
m_oMissCache.clear();
}
m_oMissCache.evaluateUsage();
m_oSanity.push( pRule );
}
else if ( bNewHit )
{
m_oSanity.push( pRule );
}
// add rule to vector containing all rules sorted by GUID
insert( pRule );
bool bSave = !pRule->m_bAutomatic;
// Inform SecurityTableModel about new rule.
emit ruleAdded( pRule );
if ( bDoSanityCheck )
{
// Unlock mutex before performing system wide security check.
writeLock.unlock();
// In case we are currently loading rules from file,
// this is done uppon completion of the entire process.
m_oSanity.sanityCheck();
if ( bSave )
{
save();
}
}
}
else
{
postLogMessage( LogSeverity::Security,
tr( "A new security rule has been merged into an existing one." ) );
}
// REMOVE for beta 1
#ifdef _DEBUG
for ( RuleVectorPos i = 0; i < m_vRules.size(); ++i )
{
Q_ASSERT( m_vRules[i] );
Rule* pTestRule = m_vRules[i];
if ( pTestRule->type() <= 0 ||
pTestRule->type() >= RuleType::NoOfTypes ||
pTestRule->totalCount() < 0 )
{
Q_ASSERT( pTestRule->type() > 0 && pTestRule->type() < RuleType::NoOfTypes );
Q_ASSERT( pTestRule->totalCount() >= 0 );
}
}
#endif
return pRule; // Evaluates to false if pRule has been set to NULL.
}
void Manager::remove( const Rule* const pRule )
{
if ( !pRule )
{
return;
}
m_oRWLock.lockForWrite();
const RuleVectorPos nPos = find( pRule->m_idUUID );
#ifdef _DEBUG
Q_ASSERT( nPos != m_vRules.size() );
Q_ASSERT( m_vRules[nPos] == pRule );
#endif
remove( nPos );
m_oRWLock.unlock();
}
void Manager::clear()
{
m_oRWLock.lockForWrite();
qDeleteAll( m_vRules );
m_vRules.clear();
if ( !m_bShutDown )
{
m_lmIPs.clear();
m_vIPRanges.clear();
#if SECURITY_ENABLE_GEOIP
m_lmCountries.clear();
#endif // SECURITY_ENABLE_GEOIP
m_lmmHashes.clear();
m_vRegularExpressions.clear();
m_vContents.clear();
m_vUserAgents.clear();
m_oMissCache.clear();
// saving might be required :)
m_bUnsaved = true;
m_oRWLock.unlock();
emit cleared();
// refresh settings
settingsChanged(); // refresh settings from securitySettigs
}
else
{
// Note: On shutdown, the containers are cleared anyway. No need to do that twice.
m_oRWLock.unlock();
}
}
void Manager::ban( const QHostAddress& oAddress, RuleTime::Time nBanLength,
bool bMessage, const QString& sComment, bool bAutomatic
#if SECURITY_LOG_BAN_SOURCES
, const QString& sSender
#endif // SECURITY_LOG_BAN_SOURCES
)
{
#ifdef _DEBUG
if ( oAddress.isNull() )
{
qDebug() << "You've just tried to ban a Null IP: " << oAddress.toString();
Q_ASSERT( false ); // if this happens, make sure to fix the caller... :)
return;
}
#endif // _DEBUG
#if SECURITY_LOG_BAN_SOURCES
qDebug() << "[Security] Manager::ban() invoked by: " << sSender.toLocal8Bit().data();
#endif // SECURITY_LOG_BAN_SOURCES
const quint32 tNow = common::getTNowUTC();
IPRule* pIPRule = new IPRule();
if ( !pIPRule->parseContent( oAddress.toString() ) )
{
qDebug() << "[Security] Unable to ban (invalid address): " << oAddress.toString();
delete pIPRule;
return;
}
pIPRule->m_bAutomatic = bAutomatic;
pIPRule->setExpiryTime( tNow + nBanLength );
QString sUntil;
switch ( nBanLength )
{
case RuleTime::FiveMinutes:
pIPRule->m_sComment = tr( "Temp Ignore (5 min)" );
break;
case RuleTime::ThirtyMinutes:
pIPRule->m_sComment = tr( "Temp Ignore (30 min)" );
break;
case RuleTime::TwoHours:
pIPRule->m_sComment = tr( "Temp Ignore (2 h)" );
break;
case RuleTime::SixHours:
pIPRule->m_sComment = tr( "Temp Ignore (2 h)" );
break;
case RuleTime::TwelveHours:
pIPRule->m_sComment = tr( "Temp Ignore (2 h)" );
break;
case RuleTime::Day:
pIPRule->m_sComment = tr( "Temp Ignore (1 d)" );
break;
case RuleTime::Week:
pIPRule->m_sComment = tr( "Client Block (1 week)" );
break;
case RuleTime::Month:
pIPRule->m_sComment = tr( "Quick IP Block (1 month)" );
break;
case RuleTime::Session:
pIPRule->setExpiryTime( RuleTime::Session );
pIPRule->m_sComment = tr( "Session Ban" );
sUntil = tr( "until the end of the current session" );
break;
case RuleTime::Forever:
pIPRule->setExpiryTime( RuleTime::Forever );
pIPRule->m_sComment = tr( "Indefinite Ban" );
sUntil = tr( "for an indefinite time" );
break;
default: // allows for ban lengths not defined in RuleTime::Time
pIPRule->m_sComment = tr( "Auto Ban" );
}
if ( !( sComment.isEmpty() ) )
{
pIPRule->m_sComment = sComment;
}
Rule* pRule = pIPRule;
// This also merges any existing rules in case the same IP is added twice.
if ( add( pRule ) )
{
pRule->count( tNow );
if ( bMessage )
{
if ( sUntil.isEmpty() )
sUntil = tr( "until " ) +
QDateTime::fromTime_t( pIPRule->expiryTime() ).toString();
postLogMessage( LogSeverity::Security,
tr( "Banned %1 %2." ).arg( oAddress.toString(), sUntil ) );
}
}
else
{
qDebug() << "No rule added for: " << oAddress.toString();
}
}
void Manager::ban( const QueryHit* const pHit, RuleTime::Time nBanLength, quint8 nMaxHashes,
const QString& sComment )
{
if ( !pHit || !pHit->isValid() || pHit->m_vHashes.empty() )
{
postLogMessage( LogSeverity::Security, tr( "Error: Could not ban invalid file." ) );
return;
}
m_oRWLock.lockForRead();
bool bAlreadyBlocked = ( find( pHit->m_vHashes ) != m_vRules.size() );
m_oRWLock.unlock();
if ( bAlreadyBlocked )
{
postLogMessage( LogSeverity::Security,
tr( "Error: Could not ban already banned file." ) );
}
else
{
const quint32 tNow = common::getTNowUTC();
HashRule* pRule = new HashRule();
pRule->setExpiryTime( tNow + nBanLength );
QString sUntil;
switch ( nBanLength )
{
case RuleTime::FiveMinutes:
pRule->m_sComment = tr( "Temp Ignore (5 min)" );
break;
case RuleTime::ThirtyMinutes:
pRule->m_sComment = tr( "Temp Ignore (30 min)" );
break;
case RuleTime::TwoHours:
pRule->m_sComment = tr( "Temp Ignore (2 h)" );
break;
case RuleTime::SixHours:
pRule->m_sComment = tr( "Temp Ignore (2 h)" );
break;
case RuleTime::TwelveHours:
pRule->m_sComment = tr( "Temp Ignore (2 h)" );
break;
case RuleTime::Day:
pRule->m_sComment = tr( "Temp Ignore (1 d)" );
break;
case RuleTime::Week:
pRule->m_sComment = tr( "Client Block (1 week)" );
break;
case RuleTime::Month:
pRule->m_sComment = tr( "Quick Block (1 month)" );
break;
case RuleTime::Session:
pRule->setExpiryTime( RuleTime::Session );
pRule->m_sComment = tr( "Session Ban" );
sUntil = tr( "until the end of the current session" );
break;
case RuleTime::Forever:
pRule->setExpiryTime( RuleTime::Forever );
pRule->m_sComment = tr( "Indefinite Ban" );
sUntil = tr( "for an indefinite time" );
break;
default: // allows for ban lengths not defined in RuleTime::Time
pRule->m_sComment = tr( "Auto Ban" );
}
if ( !( sComment.isEmpty() ) )
{
pRule->m_sComment = sComment;
}
pRule->setHashes( pHit->m_vHashes );
pRule->simplifyByHashPriority( nMaxHashes );
if ( add( pRule ) )
{
pRule->count( tNow );
}
postLogMessage( LogSeverity::Security,
tr( "Banned file: " ) + pHit->m_sDescriptiveName );
}
}
bool Manager::isDenied( const EndPoint& oAddress )
{
if ( oAddress.isNull() )
{
return false;
}
QReadLocker readLock( &m_oRWLock );
const quint32 tNow = common::getTNowUTC();
// First, check the miss cache if the IP is not included in the list of rules.
// If the address is in cache, it is a miss and no further lookup is needed.
if ( m_oMissCache.check( oAddress ) )
{
if ( m_bLogIPCheckHits )
{
postLogMessage( LogSeverity::Security,
tr( "Skipped repeat IP security check for %1 (%2 IPs cached)."
).arg( oAddress.toString(),
QString::number( m_oMissCache.size() ) ) );
}
return m_bDenyPolicy;
}
if ( m_bLogIPCheckHits )
{
postLogMessage( LogSeverity::Security,
tr( "Called first-time IP security check for %1."
).arg( oAddress.toString() ) );
}
// Second, if quazaa local/private blocking is turned on, check if the IP is local/private
if ( m_bDenyPrivateIPs )
{
if ( isPrivate( oAddress ) )
{
postLogMessage( LogSeverity::Security,
tr( "Local/Private IP denied: %1" ).arg( oAddress.toString() ) );
return true;
}
}
// Third, look up the IP in our country rule map.
#if SECURITY_ENABLE_GEOIP
if ( m_bEnableCountries )
{
CountryMap::const_iterator itCountries;
itCountries = m_lmCountries.find( m_oCountryHasher( oAddress.country() ) );
if ( itCountries != m_lmCountries.end() )
{
CountryRule* pCountryRule = ( *itCountries ).second;
if ( pCountryRule->isExpired( tNow ) )
{
expireLater();
}
else if ( pCountryRule->match( oAddress ) )
{
hit( pCountryRule );
if ( pCountryRule->m_nAction == RuleAction::Deny )
{
return true;
}
else if ( pCountryRule->m_nAction == RuleAction::Accept )
{
return false;
}
}
}
}
#endif // SECURITY_ENABLE_GEOIP
// Fourth, check whether the IP is contained within one of the IP range rules.
{
IPRangeVectorPos foo;
IPRangeRule* pRangeRule = findRangeMatch( oAddress, foo );
if ( pRangeRule )
{
Q_ASSERT( pRangeRule->match( oAddress ) );
if ( pRangeRule->isExpired( tNow ) )
{
expireLater();
}
else
{
hit( pRangeRule );
if ( pRangeRule->m_nAction == RuleAction::Deny )
{
return true;
}
else if ( pRangeRule->m_nAction == RuleAction::Accept )
{
return false;
}
}
}
}
// Fifth, check the IP rules lookup map.
{
IPMap::const_iterator itIPs;
itIPs = m_lmIPs.find( m_oIPHasher( oAddress ) );
if ( itIPs != m_lmIPs.end() )
{
IPRule* pIPRule = ( *itIPs ).second;
if ( pIPRule->isExpired( tNow ) )
{
expireLater();
}
else if ( pIPRule->match( oAddress ) )
{
if ( pIPRule->m_bAutomatic )
{
// Add 30 seconds to the rule time for every hit.
pIPRule->addExpiryTime( 30 );
}
hit( pIPRule );
if ( pIPRule->m_nAction == RuleAction::Deny )
{
return true;
}
else if ( pIPRule->m_nAction == RuleAction::Accept )
{
return false;
}
}
}
}
// If the IP is not within the rules (and we're using the cache),
// add the IP to the miss cache.
m_oMissCache.insert( oAddress, tNow );
// In this case, return our default policy
return m_bDenyPolicy;
}
bool Manager::isDenied( const QueryHit* const pHit, const QList<QString>& lQuery )
{
bool bReturn;
m_oRWLock.lockForRead();
bReturn = isDenied( pHit ) || // test hashes, file size and extension
isDenied( lQuery, pHit->m_sDescriptiveName ); // test regex
m_oRWLock.unlock();
return bReturn;
}
bool Manager::isClientBad( const QString& sUserAgent ) const
{
// No user agent - assume bad - They allowed to connect but no searches were performed
if ( sUserAgent.isEmpty() )
{
return true;
}
UserAgent oUserAgent( sUserAgent );
static const UserAgent oShareaza( "Shareaza" );
if ( oUserAgent == oShareaza )
{
static const UserAgent oShareaza2520( "Shareaza 2.5.2.0" );
static const UserAgent oShareaza3000( "Shareaza 3.0.0.0" );
// old and bad versions
if ( oUserAgent <= oShareaza2520 )
{
return true;
}
// fake versions
if ( oUserAgent >= oShareaza3000 )
{
return true;
}
}
if ( sUserAgent.startsWith( "Shareaza Pro", Qt::CaseInsensitive ) )
{
return true;
}
else
{
return false;
}
QString sSubStr;
// Dianlei: Shareaza rip-off
// add only based on alpha code, need verification for others
if ( sUserAgent.startsWith( "Dianlei", Qt::CaseInsensitive ) )
{
sSubStr = sUserAgent.mid( 7 );
if ( sSubStr.startsWith( " 1." ) )
{
return true;
}
if ( sSubStr.startsWith( " 0." ) )
{
return true;
}
return false;
}
// BearShare
if ( sUserAgent.startsWith( "BearShare", Qt::CaseInsensitive ) )
{
sSubStr = sUserAgent.mid( 9 );
if ( sSubStr.startsWith( " Lite" ) )
{
return true;
}
if ( sSubStr.startsWith( " Pro" ) )
{
return true;
}
if ( sSubStr.startsWith( " MP3" ) )
{
return true; // GPL breaker
}
if ( sSubStr.startsWith( " Music" ) )
{
return true; // GPL breaker
}
if ( sSubStr.startsWith( " 6." ) )
{
return true; // iMesh
}
return false;
}
// Fastload.TV
if ( sUserAgent.startsWith( "Fastload.TV", Qt::CaseInsensitive ) )
{
return true;
}
// Fildelarprogram
if ( sUserAgent.startsWith( "Fildelarprogram", Qt::CaseInsensitive ) )
{
return true;
}
// Gnutella Turbo (Look into this client some more)
if ( sUserAgent.startsWith( "Gnutella Turbo", Qt::CaseInsensitive ) )
{
return true;
}
// Identified Shareaza Leecher Mod
if ( sUserAgent.startsWith( "eMule mod (4)", Qt::CaseInsensitive ) )
{
return true;
}
// iMesh
if ( sUserAgent.startsWith( "iMesh", Qt::CaseInsensitive ) )
{
return true;
}
// Mastermax File Sharing
if ( sUserAgent.startsWith( "Mastermax File Sharing", Qt::CaseInsensitive ) )
{
return true;
}
// Trilix
if ( sUserAgent.startsWith( "Trilix", Qt::CaseInsensitive ) )
{
return true;
}
// Wru (bad GuncDNA based client)
if ( sUserAgent.startsWith( "Wru", Qt::CaseInsensitive ) )
{
return true;
}
// GPL breakers- Clients violating the GPL
// See http://www.gnu.org/copyleft/gpl.html
// Some other breakers outside the list
if ( sUserAgent.startsWith( "C -3.0.1", Qt::CaseInsensitive ) )
{
return true;
}
// outdated rip-off
if ( sUserAgent.startsWith( "eTomi", Qt::CaseInsensitive ) )
{
return true;
}
// Shareaza rip-off / GPL violator
if ( sUserAgent.startsWith( "FreeTorrentViewer", Qt::CaseInsensitive ) )
{
return true;
}
// Is it bad?
if ( sUserAgent.startsWith( "K-Lite", Qt::CaseInsensitive ) )
{
return true;
}
// Leechers, do not allow to connect
if ( sUserAgent.startsWith( "mxie", Qt::CaseInsensitive ) )
{
return true;
}
// ShareZilla (bad Shareaza clone)
if ( sUserAgent.startsWith( "ShareZilla", Qt::CaseInsensitive ) )
{
return true;
}
// Shareaza rip-off / GPL violator
if ( sUserAgent.startsWith( "P2P Rocket", Qt::CaseInsensitive ) )
{
return true;
}
// Rip-off with bad tweaks
if ( sUserAgent.startsWith( "SlingerX", Qt::CaseInsensitive ) )
{
return true;
}
// Not clear why it's bad