/
ripecheck.tcl
975 lines (876 loc) · 43.2 KB
/
ripecheck.tcl
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
#
# ripecheck.tcl Version: 3.1 Author: Stefan Wold <ratler@stderr.eu>
###
# Info:
# This script check unresolved ip addresses against a RIPE database
# and ban the user if the country match your configured top domains.
# Features:
# * Configuration through dcc console
# * Per channel settings
# * Can handle top domain banning for name based hosts
# * Custom bantime
# * Support extra resolving for domains like info, com, net, org
# to find hosts that actually have an ip from a country
# you wish to ban.
# * Custom ban messages
# * Builtin help pages, see .help ripecheck
###
# Require / Depends:
# tcllib >= 1.8 (http://www.tcl.tk/software/tcllib/)
###
# Usage:
# Load the script and change the topdomains you
# wish to ban.
#
# You can test ip addresses from dcc console
# against your current settings by enabling debug output
# (.console +d) and running .testripecheck <channel> <host>
#
# chanset <channel> <+/->ripecheck
# This will either enable (+) or disable (-) the script for the
# specified channel
#
# chanset <channel> ripecheck.bantime <number>
# For how long should the ban be active in minutes
#
# chanset <channel> <+/->ripecheck.topchk
# Enable (+) or disable (-) top domain resolve check
#
# chanset <channel> <+/->ripecheck.topban
# Enable (+) or disable (-) top domain banning based on
# the topdomain list
#
# chanset <channel> <+/->ripecheck.pubcmd
# Enable (+) or disable (-) public commands (!ripecheck)
#
# +ripetopresolv <channel> <topdomain|*>
# Add a top domain or regexp pattern that you want to resolve for
# further ripe checking. It's possible that domains like com, info, org
# could be from a country that is banned in the top domain list.
# Example (match .com): .+ripetopresolv #channel com
# Example (match everything): .+ripetopresolv #channel *
#
# -ripetopresolv <channel> <topdomain|*>
# Remove a top resolve domain or regexp pattern from the channel that
# you no longer wish to resolve.
#
# +ripetopdom <channel> <topdomain>
# Add a top domain for the channel that you wish to ban
# Example: .+ripetopdom #channel ro
#
# -ripetopdom <channel> <topdomain>
# Remove a top domain from the channel that you no longer
# wish to ban
#
# ripesettings
# List current channel settings
#
# ripeconfig <option> [value]"
# Options:"
# banreason [string] : Set custom ban reason, support substitutional keywords, see below"
# bantopreason [string] : Set custom TLD ban reason, support substitutional keywords, see below"
# msgcmds [on|off] : Enable or Disable commands through private message"
# fallback [on|off] : EXPERIMENTAL!!! Use with caution!
# This function will _try_ to detect country for an host where the whois server
# only return a few NET-XXX-XXX-XXX-XXX entries.
# The intention is to replace lastResortMask.
# Examples:
# TLD ban reason: .ripeconfig bantopreason Hello %nick%, TLD '%tld%' is not allowed here"
# Ban reason: .ripeconfig banreason Sorry %country%(%tld%) is not allowed in here"
# Enable msgcmds: .ripeconfig msgcmds on"
# Disable msgcmds: .ripeconfig msgcmds off"
# Substitutional keywords, current keywords are:"
# %tld% = Top level domain, ie .us, .se, .no"
# %country% = Country name"
# %nick% = Nickname of the user being banned"
# *NOTE*:"
# To completely remove an option from the configuration leave \[value\] blank, ie .ripeconfig msgcmds"
#
# help ripecheck
# View ripecheck command help page through dcc console
#
# Public channel commands:
# !ripecheck <host>
# !ripeinfo <host>
#
# Private msg commands:
# !ripecheck <host>
# !ripeinfo <host>
###
# Tested:
# eggdrop v1.6.19 GNU/Linux with tcl 8.5 and tcllib 1.10
###
# BUGS?!
# If you discover any problems please send an e-mail
# to ratler@stderr.eu with as detailed information as possible
# on how to reproduce the issue.
###
# LICENSE:
# Copyright (C) 2006 - 2010 Stefan Wold <ratler@stderr.eu>
#
# This code comes with ABSOLUTELY NO WARRANTY
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# RIPE Country Checker
if {[namespace exists ::ripecheck]} {namespace delete ::ripecheck}
namespace eval ::ripecheck {
# --- Settings ---
# RIPE query timeout setting, default 5 seconds
variable rtimeout 5
# Set console output flag, for debug purpose (default d, ie .console +d)
variable conflag d
# Path to netmask file
variable iplistfile "scripts/iplist.txt"
# Path to channel settings file
variable chanfile "ripecheckchan.dat"
# Path to tld country list file
variable tldfile "scripts/tld_country_list.txt"
}
# ---- Only edit stuff below this line if you know what you are doing ----
# Channel flags
setudef flag ripecheck
setudef flag ripecheck.topchk
setudef flag ripecheck.topban
setudef flag ripecheck.pubcmd
setudef int ripecheck.bantime
# Packages
package require ip
# Bindings
bind join - *!*@* ::ripecheck::onJoin
bind dcc -|- testripecheck ::ripecheck::test
bind dcc m|ov +ripetopdom ::ripecheck::addTopDom
bind dcc m|ov -ripetopdom ::ripecheck::delTopDom
bind dcc m|ov +ripetopresolv ::ripecheck::addTopResolve
bind dcc m|ov -ripetopresolv ::ripecheck::delTopResolve
bind dcc m|ov ripeconfig ::ripecheck::config
bind dcc -|- ripesettings ::ripecheck::settings
bind dcc -|- help ::ripecheck::help
bind pub -|- !ripecheck ::ripecheck::pubRipeCheck
bind msg -|- !ripecheck ::ripecheck::msgRipeCheck
bind pub -|- !ripeinfo ::ripecheck::pubRipeInfo
bind msg -|- !ripeinfo ::ripecheck::msgRipeInfo
namespace eval ::ripecheck {
# Global variables
variable version "3.1"
variable maskarray
variable chanarr
variable topresolv
variable config
variable constate
variable tldtocountry
# Parse ip list file
if {[file exists $::ripecheck::iplistfile]} {
set fid [open $::ripecheck::iplistfile r]
while { ![eof $fid] } {
gets $fid line
if {[regexp {^[0-9]} $line]} {
regexp -nocase {^([0-9\.\/]+)[[:space:]]+([a-z0-9\.]+)} $line dummy mask whoisdb
lappend ::ripecheck::maskarray $mask
set ::ripecheck::maskhash($mask) $whoisdb
}
}
close $fid
# These two variables should _ALWAYS_ be of the same size, otherwise something is wrong
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - IP file loaded with [llength $::ripecheck::maskarray] netmask(s)"
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - IP file loaded with [array size ::ripecheck::maskhash] whois entries"
}
# Read settings - only at startup
if {[file exists $::ripecheck::chanfile]} {
set fchan [open $::ripecheck::chanfile r]
while { ![eof $fchan] } {
gets $fchan line
if {[regexp {^\#} $line]} {
set ::ripecheck::chanarr([string tolower [lindex [split $line :] 0]]) [split [lindex [split $line :] 1] ,]
} elseif {[regexp {^topresolv} $line]} {
set ::ripecheck::topresolv([string tolower [lindex [split $line :] 1]]) [split [lindex [split $line :] 2] ,]
} elseif {[regexp {^config} $line]} {
set ::ripecheck::config([lindex [split $line :] 1]) [lindex [split $line :] 2]
}
}
close $fchan
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Channel file loaded with settings for [array size ::ripecheck::chanarr] channel(s)"
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Top resolv domains loaded for [array size ::ripecheck::topresolv] channel(s)"
}
# Read tld_country_list
if {[file exists $::ripecheck::tldfile]} {
set ftld [open $::ripecheck::tldfile r]
while { ![eof $ftld] } {
gets $ftld line
if {[regexp {^[a-z]} $line]} {
regexp -nocase {^([a-z]{2,6})[[:space:]]+(.*)} $line -> tld country
set ::ripecheck::tldtocountry($tld) $country
}
}
close $ftld
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - TLD country list loaded with [array size ::ripecheck::tldtocountry] entries"
}
# Functions
proc onJoin { nick host handle channel } {
# Lower case channel
set channel [string tolower $channel]
# Only run if channel is defined
if {![channel get $channel ripecheck]} { return 1 }
# Exclude ops, voice, friends
if {[matchattr $handle fov|fov $channel]} {
putloglev $::ripecheck::conflag * "ripecheck: $nick is on exempt list"
return 1
}
# Check if channel has a domain list or complain about it and then abort
if {![info exists ::ripecheck::chanarr($channel)]} {
putlog "ripecheck: Ripecheck is enabled but '$channel' has no domain list!"
return 1
}
# Get IP/Host part
regexp ".+@(.+)" $host matches iphost
# Top domain ban if enabled
if {[channel get $channel ripecheck.topban]} {
set htopdom [lindex [split $iphost "."] end]
if {[lsearch -exact $::ripecheck::chanarr($channel) $htopdom] != -1} {
set country [::ripecheck::getCountry $htopdom]
set template [list %nick% $nick \
%domain% $htopdom \
%tld% $htopdom \
%country% $country]
set bantime [channel get $channel ripecheck.bantime]
if {[info exists ::ripecheck::config(bantopreason)]} {
set banreason [::ripecheck::templateReplace $::ripecheck::config(bantopreason) $template]
} else {
set banreason "RIPE Country Check: Top domain .$htopdom is banned."
}
putlog "ripecheck: Matched top domain '$htopdom' banning *!*@*.$htopdom for $bantime minute(s)"
newchanban $channel "*!*@*.$htopdom" ripecheck $banreason $bantime
return 1
}
}
# Only run RIPE check on numeric IP unless ripecheck.topchk is enabled
if {[regexp {[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$} $iphost]} {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Found numeric IP $iphost ... scanning"
::ripecheck::whoisFindServer $iphost $iphost 1 $nick $channel $host ripecheck
} elseif {[channel get $channel ripecheck.topchk]} {
# Check if channel has a resolve domain list or complain about it and then abort
if {![info exists ::ripecheck::topresolv($channel)]} {
putlog "ripecheck: Ripecheck is enabled but '$channel' has no resolve domain list!"
return 0
}
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Checking if host match the top resolve list..."
set htopdom [lindex [split $iphost "."] end]
if {[lsearch -exact $::ripecheck::topresolv($channel) "*"] != -1 || [lsearch -exact $::ripecheck::topresolv($channel) $htopdom] != -1} {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Matched top resolve domain '$htopdom' for host '$iphost'"
dnslookup $iphost ::ripecheck::whoisFindServer $nick $channel $host ripecheck
}
}
}
proc notifySender { nick channel rtype msg } {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG: Entering notifySender()"
if {$rtype == "pubRipeCheck"} {
puthelp "PRIVMSG $channel :$nick: \[ripecheck\] $msg"
} elseif {$rtype == "pubRipeInfo" || $rtype == "msgRipeCheck"} {
puthelp "PRIVMSG $nick :ripecheck: $msg"
}
}
proc ripecheck { ip host nick channel orghost ripe } {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Entering ripecheck()"
set bantime [channel get $channel ripecheck.bantime]
if {[lsearch -exact $::ripecheck::chanarr($channel) $ripe] != -1} {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - ripecheck() matched '$ripe'"
set country [::ripecheck::getCountry $ripe]
set template [list %nick% $nick \
%ripe% $ripe \
%tld% $ripe \
%country% $country]
if {[info exists ::ripecheck::config(banreason)]} {
set banreason [::ripecheck::templateReplace $::ripecheck::config(banreason) $template]
} else {
set banreason "RIPE Country Check: Matched $country \[$ripe\]"
}
putlog "ripecheck: Matched country $country \[$ripe\] banning $nick!$orghost for $bantime minute(s)"
newchanban $channel "*!*@$host" ripecheck $banreason $bantime
}
}
proc test { nick idx arg } {
if {[llength [split $arg]] != 2} {
::ripecheck::help $nick $idx testripecheck; return 0
}
foreach {channel ip} $arg {break}
set ip [string tolower $ip]
set channel [string tolower $channel]
if {[validchan $channel]} {
# First we check if topban is enabled
if {[channel get $channel ripecheck.topban]} {
set htopdom [lindex [split $ip "."] end]
if {[lsearch -exact $::ripecheck::chanarr($channel) $htopdom] != -1} {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Topban matched '$htopdom' for host '$ip', host would get banned!"
return 1
}
}
if {[regexp {[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$} $ip]} {
::ripecheck::whoisFindServer $ip "" "" $nick $channel "" testRipeCheck
} else {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Resolving..."
set htopdom [lindex [split $ip "."] end]
if {[lsearch -exact $::ripecheck::topresolv($channel) "*"] != -1 || [lsearch -exact $::ripecheck::topresolv($channel) $htopdom] != -1} {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Matched top resolve domain '$htopdom' for host '$ip' on '$channel'"
dnslookup $ip ::ripecheck::whoisFindServer $nick $channel "" testRipeCheck
} else {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Host '$ip' did not match one of the top resolve domains"
}
}
} else {
putdcc $idx "\002RIPECHECK\002: Invalid channel $channel"
}
}
proc ripeInfo { nick inetnum netname mntby country descr } {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Entering ripeInfo()"
set countryname [::ripecheck::getCountry $country]
puthelp "PRIVMSG $nick :Inetnum: $inetnum"
puthelp "PRIVMSG $nick :Netname: $netname"
puthelp "PRIVMSG $nick :mnt-by: $mntby"
if {$countryname == ""} {
puthelp "PRIVMSG $nick :Country: $country"
} else {
puthelp "PRIVMSG $nick :Country: $countryname \[[string toupper $country]\]"
}
puthelp "PRIVMSG $nick :Description: $descr"
}
proc testripecheck { ip host channel ripe } {
if {[lsearch -exact $::ripecheck::chanarr($channel) $ripe] != -1} {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Testripecheck matched country '$ripe' for host '$host ($ip)' on channel '$channel', host would get banned!"
} else {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Testripecheck host '$host ($ip)' would _not_ get banned!"
}
}
proc pubParseIp { nick host handle channel ip rtype } {
if {[regexp {[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$} $ip]} {
set iptype [::ip::type $ip]
if {$iptype != "normal"} {
::ripecheck::notifySender $nick $channel $rtype "Sorry but '$ip' is from a '$iptype' range"
} else {
::ripecheck::whoisFindServer $ip $ip "" $nick $channel "" $rtype
}
} else {
dnslookup $ip ::ripecheck::whoisFindServer $nick $channel "" $rtype
}
}
proc pubRipeCheck { nick host handle channel ip } {
set channel [string tolower $channel]
if {![channel get $channel ripecheck.pubcmd]} { return 0 }
::ripecheck::pubParseIp $nick $host $handle $channel $ip pubRipeCheck
}
proc msgRipeCheck { nick host handle ip } {
# Check if msgcmds is enabled
if {[::ripecheck::isConfigEnabled msgcmds]} {
::ripecheck::pubParseIp $nick $host $handle "" $ip msgRipeCheck
}
return 0
}
proc pubRipeInfo { nick host handle channel ip } {
set channel [string tolower $channel]
if {![channel get $channel ripecheck.pubcmd]} { return 0 }
::ripecheck::pubParseIp $nick $host $handle $channel $ip pubRipeInfo
}
proc msgRipeInfo { nick host handle ip } {
# Check if msgcmds is enabled
if {[::ripecheck::isConfigEnabled msgcmds]} {
::ripecheck::pubParseIp $nick $host $handle "" $ip pubRipeInfo
}
return 0
}
# Lookup which whois server to query and call whois_connect
proc whoisFindServer { ip host status nick channel orghost rtype } {
if {$status == 0} {
::ripecheck::notifySender $nick $channel $rtype "Failed to resolve '$host'!"
putlog "ripecheck: Couldn't resolve '$host'. No further action taken."
return 0
}
# Abort if we stumble upon a private or reserved net range
set iptype [::ip::type $ip]
if {$iptype != "normal"} {
putlog "ripecheck: '$ip' is from a '$iptype' range. No further action taken."
return 0
}
set matchmask [::ip::longestPrefixMatch $ip $::ripecheck::maskarray]
set whoisdb [string tolower $::ripecheck::maskhash($matchmask)]
set whoisport 43
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Matching mask $matchmask using whois DB: $whoisdb"
if {$whoisdb == "unallocated"} {
::ripecheck::notifySender $nick $channel $rtype "Unallocated netmask!"
putlog "ripecheck: Unallocated netmask, bailing out!"
return 0
}
::ripecheck::whoisConnect $ip $host $nick $channel $orghost $whoisdb $whoisport $rtype
}
proc whoisConnect { ip host nick channel orghost whoisdb whoisport rtype } {
# Setup timeout
after $::ripecheck::rtimeout * 1000 set ::ripecheck::constate "timeout"
if {[catch {socket -async $whoisdb $whoisport} sock]} {
::ripecheck::notifySender $nick $channel $rtype "ERROR: Failed to connect to '$whoisdb'!"
putlog "ripecheck: ERROR: Failed to connect to server $whoisdb!" ; return -1
}
fconfigure $sock -buffering line
fileevent $sock writable [list ::ripecheck::whoisCallback $ip $host $nick $channel $orghost $sock $whoisdb $rtype]
vwait ::ripecheck::constate
if { $::ripecheck::constate == "timeout" } {
::ripecheck::notifySender $nick $channel $rtype "ERROR: Connection timeout using '$whoisdb'!"
putlog "ripecheck: ERROR: Connection timeout against $whoisdb"; return -1
}
}
proc whoisCallback { ip host nick channel orghost sock whoisdb rtype } {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Entering whois_callback..."
set whoisdata(inetnum) "No info"
set whoisdata(netname) "No info"
set whoisdata(mntby) "No info"
set whoisdata(descr) "No info"
if {[string equal {} [fconfigure $sock -error]]} {
puts $sock $ip
flush $sock
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - State 'connected' with '$whoisdb'"
set ::ripecheck::constate "connected"
while {![eof $sock]} {
set row [gets $sock]
if {[regexp -line -nocase {referralserver:\s*(.*)} $row -> referral]} {
set referral [string tolower $referral]
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Found whois referral server: $referral"
# Extract the whois server from $referral
if {[regexp -line -nocase {^whois://(.*[^/])/?} $referral -> referral]} {
foreach {referral whoisport} [split $referral :] { break }
# Set default port if empty
if {$whoisport == ""} {
set whoisport 43
}
# Close socket, don't want to many sockets open simultaneously
close $sock
# Time for some recursive looping ;)
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Following referral server, new server is '$referral', port '$whoisport'"
::ripecheck::whoisConnect $ip $host $nick $channel $orghost $referral $whoisport $rtype
return 1
} elseif {[regexp -line -nocase {^rwhois://.*} $referral]} {
# Ignore rwhois for now
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Ignoring rwhois referral"
continue
} else {
putlog "ripecheck: ERROR: Unknown referral type from '$whoisdb' for ip '$ip', please bug report this line."
close $sock; return 0
}
} elseif {[regexp -line -nocase {country:\s*([a-z]{2,6})} $row -> data]} {
set whoisdata(country) [string tolower $data]
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - $whoisdb answer: $whoisdata(country)"
} elseif {[regexp -line -nocase {netname:\s*(.*)} $row -> data]} {
set whoisdata(netname) $data
} elseif {[regexp -line -nocase {descr:\s*(.*)} $row -> data] && ![info exists whoisdata(descr)]} {
set whoisdata(descr) $data
} elseif {[regexp -line -nocase {mnt-by:\s*(.*)} $row -> data]} {
set whoisdata(mntby) $data
} elseif {[regexp -line -nocase {inetnum:\s*(.*)} $row -> data]} {
set whoisdata(inetnum) $data
} elseif {[regexp -line {.*\((NET-[0-9]{1,3}-[0-9]{1,3}-[0-9]{1,3}.*)\)} $row -> data]} {
set whoisdata(fallback) $data
}
}
close $sock
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - End of while-loop in whois_callback"
# Experimental feature that might replace lastResortMasks in the future
if {[::ripecheck::isConfigEnabled fallback] && ![info exists whoisdata(country)] && [info exists whoisdata(fallback)]} {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Using fallback method (EXPERIMENTAL) for '$whoisdata(fallback)' original host was $host ($ip)"
::ripecheck::whoisConnect $whoisdata(fallback) $host $nick $channel $orghost $whoisdb 43 $rtype
return 1
}
# Last resort, check if we get a match from hardcoded netmasks
if {![info exists whoisdata(country)] && [::ripecheck::lastResortMasks $ip] != ""} {
set whoisdata(country) [::ripecheck::lastResortMasks $ip]
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Got '$whoisdata(country)' from lastResortMasks"
}
if {[info exists whoisdata(country)]} {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - Running mode: '$rtype' for country: $whoisdata(country)"
set country [::ripecheck::getCountry $whoisdata(country)]
switch -- $rtype {
ripecheck {
::ripecheck::ripecheck $ip $host $nick $channel $orghost $whoisdata(country)
}
testRipeCheck {
::ripecheck::testripecheck $ip $host $channel $whoisdata(country)
}
pubRipeCheck {
::ripecheck::notifySender $nick $channel $rtype "$host is located in $country\[[string toupper $whoisdata(country)]\]"
}
msgRipeCheck {
::ripecheck::notifySender $nick $channel $rtype "$host is located in $country\[[string toupper $whoisdata(country)]\]"
}
pubRipeInfo {
::ripecheck::ripeInfo $nick $whoisdata(inetnum) $whoisdata(netname) $whoisdata(mntby) $whoisdata(country) $whoisdata(descr)
}
default {
::ripecheck::ripecheck $ip $host $nick $channel $orghost $whoisdata(country)
}
}
} else {
# Respond that something went wrong
::ripecheck::notifySender $nick $channel $rtype "Whois query failed for '$host'!"
putlog "ripecheck: No country found for '$ip'. No further action taken. (Possible bug?)"
}
} else {
set ::ripecheck::constate "timeout"
}
}
# Add top resolv domain for channel and write settings to file
proc addTopResolve { nick idx arg } {
if {[llength [split $arg]] != 2} {
::ripecheck::help $nick $idx +ripetopresolv; return 0
}
foreach {channel topdom} $arg {break}
set channel [string tolower $channel]
set topdom [string tolower $topdom]
if {[validchan $channel]} {
# It's pointless to set a resolv domain if no domains have been added for banning on the
# current channel.
if {[info exists ::ripecheck::chanarr($channel)]} {
# If data exist extract into a list
if {[info exists ::ripecheck::topresolv($channel)]} {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - topresolv exists"
set dlist $::ripecheck::topresolv($channel)
# top domain doesn't exist so lets add it
if {[lsearch -exact $dlist $topdom] == -1 } {
lappend dlist $topdom
set ::ripecheck::topresolv($channel) $dlist
} else {
putdcc $idx "\002RIPECHECK\002: Resolve domain '$topdom' already exist on $channel"; return 0
}
} else {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - topresolv doesn't exist"
set dlist [list $topdom]
set ::ripecheck::topresolv($channel) $dlist
}
# Write to the ripecheck channel file
::ripecheck::writeSettings
putdcc $idx "\002RIPECHECK\002: Top resolve domain '$topdom' successfully added to $channel."
} else {
putdcc $idx "\002RIPECHECK\002: You need to add a top domain for $channel before adding a resolve domain."
}
} else {
putdcc $idx "\002RIPECHECK\002: Invalid channel: $channel"
}
}
# Remove resolve domain from channel and write settings to file
proc delTopResolve { nick idx arg } {
if {[llength [split $arg]] != 2} {
::ripecheck::help $nick $idx -ripetopresolv; return 0
}
foreach {channel topdom} $arg {break}
set channel [string tolower $channel]
set topdom [string tolower $topdom]
if {[validchan $channel]} {
if {[info exists ::ripecheck::topresolv($channel)]} {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - topresolv($channel) exists"
set dlist $::ripecheck::topresolv($channel)
# resolve domain exist so lets remove it
set dlist_index [lsearch -exact $dlist $topdom]
if {$dlist_index != -1 } {
set dlist [lreplace $dlist $dlist_index $dlist_index]
# More magic, lets clear array if the list is empty
if {[llength $dlist] > 0} {
set ::ripecheck::topresolv($channel) $dlist
} else {
unset ::ripecheck::topresolv($channel)
}
} else {
putdcc $idx "\002RIPECHECK\002: Resolve domain '$topdom' doesn't exist on $channel"; return 0
}
} else {
putdcc $idx "\002RIPECHECK\002: Nothing to do, no settings found for $channel."; return 0
}
# Write to the ripecheck channel file
::ripecheck::writeSettings
putdcc $idx "\002RIPECHECK\002: Resolve domain '$topdom' successfully removed from $channel."
} else {
putdcc $idx "\002RIPECHECK\002: Invalid channel: $channel"
}
}
# List channel and top resolv domains
proc settings { nick idx arg } {
putdcc $idx "### \002Settings\002 - Ripecheck v$::ripecheck::version by Ratler ###"
if {[array size ::ripecheck::chanarr] > 0} {
foreach channel [array names ::ripecheck::chanarr] {
putdcc $idx "### \002Channel:\002 $channel"
putdcc $idx " \002Banned domains:\002 [join $::ripecheck::chanarr($channel) ", "]"
if {[info exists ::ripecheck::topresolv($channel)]} {
putdcc $idx " \002Resolve domains:\002 [join $::ripecheck::topresolv($channel) ", "]"
}
}
} else {
putdcc $idx "### No channel settings exist."
}
foreach option [array names ::ripecheck::config] {
putdcc $idx "### \002$option:\002 $::ripecheck::config($option)"
}
}
# Function to set general options
proc config { nick idx arg } {
if {!([llength [split $arg]] > 0)} {
::ripecheck::help $nick $idx ripeconfig; return 0
}
# Allowed string options
set allowed_str_opts [list banreason bantopreason]
# Allowed boolean options
set allowed_bool_opts [list msgcmds fallback]
set option [string tolower [lindex [split $arg] 0]]
set value [join [lrange [split $arg] 1 end]]
# Check option type
if {[lsearch -exact $allowed_str_opts $option] != -1} {
if {$value != ""} {
set ::ripecheck::config($option) $value
putdcc $idx "\002RIPECHECK\002: Option '$option' set with the value '$value'"
} else {
if {[info exists ::ripecheck::config($option)]} {
unset ::ripecheck::config($option)
}
# Always output unset msg for convenience
putdcc $idx "\002RIPECHECK\002: Option '$option' unset"
}
} elseif {[lsearch -exact $allowed_bool_opts $option] != -1} {
set value [string tolower $value]
if {[string is boolean $value] && $value != ""} {
set ::ripecheck::config($option) $value
putdcc $idx "\002RIPECHECK\002: Option '$option' set with the value '$value'"
} elseif { $value == "" } {
if {[info exists ::ripecheck::config($option)]} {
unset ::ripecheck::config($option)
}
# Always output unset msg for convenience
putdcc $idx "\002RIPECHECK\002: Option '$option' unset"
} else {
putdcc $idx "\002RIPECHECK\002: Value '$value' is not a boolean, should be true|false|on|off|1|0"
return 0
}
} else {
putdcc $idx "\002RIPECHECK\002: Invalid option '$option', valid options are: [join $allowed_str_opts ", "], [join $allowed_bool_opts ", "]"
return 0
}
::ripecheck::writeSettings
}
# Add top domain to channel and write settings to file
proc addTopDom { nick idx arg } {
if {[llength [split $arg]] != 2} {
::ripecheck::help $nick $idx +ripetopdom; return 0
}
foreach {channel topdom} $arg {break}
set channel [string tolower $channel]
set topdom [string tolower $topdom]
if {[validchan $channel]} {
# If data exist extract into a list
if {[info exists ::ripecheck::chanarr($channel)]} {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - chanarr exists"
set dlist $::ripecheck::chanarr($channel)
# top domain doesn't exist so lets add it
if {[lsearch -exact $dlist $topdom] == -1 } {
lappend dlist $topdom
set ::ripecheck::chanarr($channel) $dlist
} else {
putdcc $idx "\002RIPECHECK\002: Domain '$topdom' already exist on $channel"; return 0
}
} else {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - chanarr doesn't exist"
set dlist [list $topdom]
set ::ripecheck::chanarr($channel) $dlist
}
# Write to the ripecheck channel file
::ripecheck::writeSettings
putdcc $idx "\002RIPECHECK\002: Top domain '$topdom' successfully added to $channel."
} else {
putdcc $idx "\002RIPECHECK\002: Invalid channel: $channel"
}
}
# Remove top domain for channel and write settings to file
proc delTopDom { nick idx arg } {
if {[llength [split $arg]] != 2} {
::ripecheck::help $nick $idx -ripetopdom; return 0
}
foreach {channel topdom} $arg {break}
set channel [string tolower $channel]
set topdom [string tolower $topdom]
if {[validchan $channel]} {
if {[info exists ::ripecheck::chanarr($channel)]} {
putloglev $::ripecheck::conflag * "ripecheck: DEBUG - chanarr($channel) exists"
set dlist $::ripecheck::chanarr($channel)
# top domain doesn't exist so lets add it
set dlist_index [lsearch -exact $dlist $topdom]
if {$dlist_index != -1 } {
set dlist [lreplace $dlist $dlist_index $dlist_index]
# More magic, clear array if list is empty
if {[llength $dlist] > 0} {
set ::ripecheck::chanarr($channel) $dlist
} else {
unset ::ripecheck::chanarr($channel)
}
} else {
putdcc $idx "\002RIPECHECK\002: Domain '$topdom' doesn't exist on $channel"; return 0
}
} else {
putdcc $idx "\002RIPECHECK\002: Nothing to do, no settings found for $channel."; return 0
}
# Write to the ripecheck channel file
::ripecheck::writeSettings
putdcc $idx "\002RIPECHECK\002: Top domain '$topdom' successfully removed from $channel."
} else {
putdcc $idx "\002RIPECHECK\002: Invalid channel: $channel"
}
}
# Define whois overrides for netmasks with incomplete records
proc lastResortMasks { ip } {
set masks(24.16.0.0/13) "us"
set masks(24.239.32.0/19) "us"
set masks(208.151.241.0/24) "us"
set masks(208.151.242.0/23) "us"
set masks(208.151.244.0/22) "us"
set masks(208.151.248.0/21) "us"
# Create a list from the masks array
foreach mask [array names masks] {
lappend masklist $mask
}
set matchmask [::ip::longestPrefixMatch $ip $masklist]
if {$matchmask != ""} {
return $masks($matchmask)
}
}
# Return a country based on tld or return "" if no country is found
proc getCountry { tld } {
if {[array size ::ripecheck::tldtocountry] > 0} {
set country $::ripecheck::tldtocountry($tld)
if {$country != ""} {
return $country
}
}
return ""
}
# Return modified text based on template list
proc templateReplace { text subs } {
foreach {arg1 arg2} $subs {
regsub -all -- $arg1 $text $arg2 text
}
return $text
}
proc isConfigEnabled { option } {
if {[info exists ::ripecheck::config($option)] && [string is boolean $::ripecheck::config($option)] && [string is true $::ripecheck::config($option)]} {
return true
}
return false
}
proc writeSettings { } {
# Backup file in case something goes wrong
if {[file exists $::ripecheck::chanfile]} {
# Don't backup a zero byte file
if {[file size $::ripecheck::chanfile] > 0} {
file copy -force $::ripecheck::chanfile $::ripecheck::chanfile.bak
}
}
set fp [open $::ripecheck::chanfile w]
foreach key [array names ::ripecheck::chanarr] {
puts $fp "$key:[join $::ripecheck::chanarr($key) ,]"
}
foreach key [array names ::ripecheck::topresolv] {
puts $fp "topresolv:$key:[join $::ripecheck::topresolv($key) ,]"
}
foreach key [array names ::ripecheck::config] {
puts $fp "config:$key:[join $::ripecheck::config($key)]"
}
close $fp
}
proc help { hand idx arg } {
switch -- $arg {
ripecheck {
putidx $idx "### \002ripecheck v$::ripecheck::version\002 by Ratler ###"; putidx $idx ""
putidx $idx "### \002chanset <channel> <+/->ripecheck\002"
putidx $idx " Enable (+) or disable (-) the script for specified channel"
putidx $idx "### \002chanset <channel> ripecheck.bantime <minutes>\002"
putidx $idx " For how long should the ban be active in minutes"
putidx $idx "### \002chanset <channel> <+/->ripecheck.topchk\002"
putidx $idx " Enable (+) or disable (-) top domain resolve check"
putidx $idx "### \002chanset <channel> <+/->ripecheck.topban\002"
putidx $idx " Enable (+) or disable (-) top domain banning based on the topdomain list"
putidx $idx "### \002chanset <channel> <+/->ripecheck.pubcmd\002"
putidx $idx " Enable (+) or disable (-) public commands (!ripecheck)"
::ripecheck::help $hand $idx +ripetopresolv
::ripecheck::help $hand $idx -ripetopresolv
::ripecheck::help $hand $idx +ripetopdom
::ripecheck::help $hand $idx -ripetopdom
::ripecheck::help $hand $idx ripesettings
::ripecheck::help $hand $idx ripeconfig
::ripecheck::help $hand $idx testripecheck
putidx $idx "### \002help ripecheck\002"
putidx $idx " This help page you're currently viewing"
}
+ripetopresolv {
putidx $idx "### \002+ripetopresolv <channel> <topdomain|*>\002"
putidx $idx " Add a top domain or regexp pattern that you want to resolve for"
putidx $idx " further ripe checking. It's possible that domains like com, info, org"
putidx $idx " could be from a country that is banned in the top domain list."
putidx $idx " Example (match .com): .+ripetopresolv #channel com"
putidx $idx " Example (match everything): .+ripetopresolv #channel *"
}
-ripetopresolv {
putidx $idx "### \002-ripetopresolv <channel> <topdomain|*>\002"
putidx $idx " Remove a top resolve domain or * from the channel that"
putidx $idx " you no longer wish to resolve."
}
+ripetopdom {
putidx $idx "### \002+ripetopdom <channel> <topdomain>\002"
putidx $idx " Add a top domain for the channel that you wish to ban"
putidx $idx " Example: .+ripetopdom #channel ro"
}
-ripetopdom {
putidx $idx "### \002-ripetopdom <channel> <topdomain>\002"
putidx $idx " Remove a top domain from the channel that you no longer"
putidx $idx " wish to ban"
}
ripeconfig {
putidx $idx "### \002ripeconfig <option> \[value\]\002"
putidx $idx " \002Options\002:"
putidx $idx " banreason \[string\] : Set custom ban reason, support substitutional keywords, see below"
putidx $idx " bantopreason \[string\] : Set custom TLD ban reason, support substitutional keywords, see below"
putidx $idx " msgcmds \[on|off\] : Enable or Disable commands through private message"
putidx $idx " fallback \[on|off\] : \002EXPERIMENTAL!!! Use with caution!\002"
putidx $idx " This function will _try_ to detect country for an host where the whois server"
putidx $idx " only return a few NET-XXX-XXX-XXX-XXX entries."
putidx $idx " The intention is to replace lastResortMask."
putidx $idx " \002Examples\002:"
putidx $idx " TLD ban reason: .ripeconfig bantopreason Hello %nick%, TLD '%tld%' is not allowed here"
putidx $idx " Ban reason: .ripeconfig banreason Sorry %country%(%tld%) is not allowed in here"
putidx $idx " Enable msgcmds: .ripeconfig msgcmds on"
putidx $idx " Disable msgcmds: .ripeconfig msgcmds off"
putidx $idx " \002Substitutional keywords, current keywords are\002:"
putidx $idx " %tld% = Top level domain, ie .us, .se, .no"
putidx $idx " %country% = Country name"
putidx $idx " %nick% = Nickname of the user being banned"
putidx $idx " \002*NOTE*\002:"
putidx $idx " To completely remove an option from the configuration leave \[value\] blank, ie .ripeconfig msgcmds"
}
ripesettings {
putidx $idx "### \002ripesettings\002"
putidx $idx " View current settings"
}
testripecheck {
putidx $idx "### \002testripecheck <channel> <host>\002"
}
default {
*dcc:help $hand $idx [join $arg]
if {[llength [split $arg]] == 0} {
putidx $idx "\n\nripecheck v$::ripecheck::version commands:"
putidx $idx " \002+ripetopresolv -ripetopresolv +ripetopdom -ripetopdom\002"
putidx $idx " \002ripesettings ripeconfig testripecheck\002"
}
return 1
}
}
}
}
putlog "\002Ripecheck v$::ripecheck::version\002 by Ratler loaded"