Subversion Repositories vgwhois

Rev

Rev 13 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 daniel-mar 1
#!/usr/bin/perl
2
 
3
#
4
#  VGWhoIs (ViaThinkSoft Global WhoIs, a fork of generic Whois / gwhois)
5
#  Main program
6
#
7
#  (c) 2010-2019 by Daniel Marschall, ViaThinkSoft <info@daniel-marschall.de>
8
#  based on the code (c) 1998-2010 by Juliane Holzt <debian@kju.de>
9
#  Some early parts by Lutz Donnerhacke <Lutz.Donnerhacke@Jena.Thur.de>
10
#
11
#  License: https://www.gnu.org/licenses/gpl-2.0.html (GPL version 2)
12
#
13
 
14
# TODO: print whois parameters at "querying..."
15
# TODO: lynx injection sicherheitslücke? => quotemeta()
16
# TODO: regularly check https://bugs.debian.org/cgi-bin/pkgreport.cgi?src=gwhois
17
 
18
# TODO: "%" am Anfang jeder Meldung ausgeben
19
 
20
# TODO: lynx wird manchmal auch ausgeführt ohne -L ...
21
# TODO: Alle "!!" entfernen
22
# TODO: print -> $result .= ?
23
 
24
use warnings;
25
use strict;
26
 
27
use FindBin;
28
use lib "$FindBin::RealBin/lib/";
29
 
30
use VGWhoIs::Core;
31
use VGWhoIs::Utils;
32
use VGWhoIs::IPv4;
33
use VGWhoIs::IPv6;
34
use VGWhoIs::OID;
35
 
36
# install with "cpan Net::IP" or "aptitude install libnet-ip-perl"
37
use Net::IP;
38
 
39
use Net::LibIDN;
40
use Encode;
41
# use Encode::Detect::Detector; # requires Debian package libencode-detect-perl
42
 
43
#use encoding ':locale';
44
 
45
#use utf8;
46
 
47
 
48
# Examples for output of the different hosts:
49
# -------------------------------------------------------------
50
# Host                            Example    Output         BOM
51
# -------------------------------------------------------------
52
# whois.viathinksoft.de           oid:2.999  UTF-8          if required (existing BOMs will be removed)
53
# cnnic.cn                        cnnic.cn   UTF-8          no
54
# whois.ati.tn                    ati.tn     UTF-8          no
55
# whois.kr                        whois.kr   UTF-8          no
56
# whois.denic.de                  denic.de   ISO-8859-1     no
57
# oldwhois.kisa.or.kr (obsolete)  whois.kr   EUC-KR         no
58
# whois.nic.ch                    domian.ch  UTF-8          no
59
# vgwhois                                    UTF-8          yes (existing BOMs will be removed?)
60
# gwhois                                     (like server)  (like server)
61
# -------------------------------------------------------------
62
 
63
 
64
# TODO: for this diagram: check if existing BOMs will be removed, e.g. by LWP.
65
# TODO: how to stop LWP's auto-detect magic?
66
# TODO: only output bom if required? doesn't work, otherwise we would need to buffer stderr and stdout, and then their order is wrong again.
67
 
68
 
69
$ENV{'HOME'}='/var/home/whois' unless defined $ENV{'HOME'};
70
 
71
# Nicht nach VGWhoIs::Core auslagern
72
# TODO: die $version auch von den .pm Modulen anzeigen?
73
my $version = '20190521';
74
my $fixwhoishost;
75
my $rawoutput = 0;
76
 
77
$| = 1; # buffer flushing = autoflush
78
 
79
while ($ARGV[0]) {
80
	if ($ARGV[0] eq '--help' || $ARGV[0] eq '-?') {
81
		print "VGWhoIs - ViaThinkSoft Global WhoIs\n",
82
		"Version $version\n\n",
83
		"Usage: vgwhois {options} [query]\n",
84
		" Try find information about the query (might be multiple words).\n",
85
		" If no query is given, use the first line from stdin\n\n",
86
		" Options:\n",
87
		"   -h host                 Selecting a fixed whois server for this query\n",
88
		"   -m method:host mirror   Defining a mirror for a given method and host.\n",
89
		"   -L                      Use lynx -source instead of LWP::Simple\n",
90
		"   -e                      Do not protect eMail addresses\n",
91
		"   -c                      Do not try to convert to UTF-8. Output server's stream.\n",
92
		"   -v                      Output version of pattern table(s)\n",
93
		"   -?, --help              Printing this text\n\n";
94
		exit;
95
	} elsif ($ARGV[0] eq '-c') {
96
		shift;
97
		$rawoutput = 1;
98
		$VGWhoIs::Core::useLWP = 0; # TODO: geht irgendwie nicht anders
99
	} elsif ($ARGV[0] eq '-h') {
100
		shift;
101
		$fixwhoishost = shift;
102
	} elsif ($ARGV[0] eq '-L') {
103
		shift;
104
		$VGWhoIs::Core::useLWP = 0;
105
	} elsif ($ARGV[0] eq '-m') {
106
		shift;
107
		$_ = shift;
108
		s/://;
109
		$VGWhoIs::Core::mirror{$_} = shift;
110
	} elsif ($ARGV[0] eq '-e') {
111
		shift;
112
		$VGWhoIs::Core::antispam = 0;
113
	} elsif ($ARGV[0] eq '-v') {
114
		print "VGWhoIs - ViaThinkSoft Global WhoIs\n\n",
115
		"program version:  $version\n",
116
		"pattern tables:   ";
117
		foreach my $patternfile (VGWhoIs::Core::getpatternfiles()) {
118
			if (!open(PATTERN,"<$patternfile")) {
119
				warn "Cannot open $patternfile. STOP.\n";
120
				exit 1;
121
			}
122
 
123
			my $line = <PATTERN>;
124
			close(PATTERN);
125
 
126
			my $patternversion;
127
			if (defined($line)) {
128
				($patternversion) = $line =~ /#:\s+version\s+(\S+)/;
129
				$patternversion = 'unknown' if !defined($patternversion);
130
			} else {
131
				$patternversion = 'unknown';
132
			}
133
			print "$patternversion\t($patternfile)\n                  ";
134
		}
135
		print "\n";
136
		exit 0;
137
	} elsif ($ARGV[0] eq '--') {
138
		shift;
139
		last;
140
	} else {
141
		last;
142
	}
143
}
144
 
145
if ($rawoutput) {
146
	binmode(STDOUT, ":bytes");
147
	binmode(STDERR, ":bytes");
148
} else {
149
	binmode(STDOUT, ":utf8");
150
	binmode(STDERR, ":utf8");
151
}
152
 
153
if (defined $ARGV[0]) {
154
	$_ = join(' ', @ARGV);
155
} else {
156
	# If no parameter is given, await an input from STDIN
157
	$_ = <>;
158
	chomp;
159
}
160
 
161
print "\x{FEFF}" if !$rawoutput; # BOM
162
exit main($_);
163
 
164
# -----------------------------------------------------------------------------------------
165
 
166
sub main {
167
	my $query = shift;
168
 
169
	$query = '' if !defined $query;
170
 
171
	if (VGWhoIs::Utils::is_utf8($query)) {
172
		$query = Encode::decode('utf8', $query);
173
	}
174
	$query = VGWhoIs::Utils::trim($query);
175
 
176
	if ($query eq '') {
177
		warn "Query is empty.\n";
178
		exit 2;
179
	}
180
 
181
	my ($method,$host,$additional);
182
 
183
	my $query_utf8 = VGWhoIs::Utils::enforce_utf8($query);
184
	print "Process query: '$query_utf8'\n\n";
185
 
186
	if ( $fixwhoishost ) {
187
		# QUE: soll das immer gelten, oder nur, wenn ermittelt wurde, dass whois benötigt wird (nicht aber cgi, etc?)
188
		($method,$host,$additional) = ('whois',$fixwhoishost,'');
189
	} else {
190
		# if ($query !~ /[^0-9\.]/) { # ipv4
191
		if ($query =~ /^[0-9\.]*$/) {
192
			my ($a, $b, $c, $d, $e) = $query =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)(.*)/;
193
			$a = 256 if !defined $a;
194
			$b = 256 if !defined $b;
195
			$c = 256 if !defined $c;
196
			$d = 256 if !defined $d;
197
			$e = ''  if !defined $e;
198
			if ($a > 255 || $b > 255 || $c > 255 || $d > 255 || $e ne '') {
199
				warn "'$query' is no valid IP address, ASN, OID or domain name.\n";
200
				exit 2;
201
			}
202
			print "Query recognized as IPv4.\n";
203
 
204
			($method,$host,$additional) = VGWhoIs::IPv4::getmethodv4($a,$b,$c,$d);
205
		# } elsif ( lc($query) !~ /[^0-9a-f:]/i ) { # ipv6
206
		# } elsif ( $query !~ /[0-9a-f:]*/ ) {
207
		} elsif (($query =~ /:/ ) && ( Net::IP::ip_expand_address($query, 6) =~ /^[0-9a-f:]*:[0-9a-f:]*$/ )) { # at least one ":" so that e.g. "ac" is recognized as TLD and not as IPv6
208
			# check and correct v6 address
209
			if ( $query =~ /[0-9a-f]{5}/ || $query =~ /:::/ ) {
210
				warn "'$query' is an invalid IPv6 address.\n";
211
				exit 2;
212
			}
213
 
214
			my $orig_query = $query;
215
			#$query =~ s/:?$/::/ if ( $query !~ /(.*:){7}/ && $query !~ /::/ );
216
			$query = Net::IP::ip_expand_address($query, 6);
217
 
218
			print "Query recognized as IPv6.\n";
219
			print "Address expanded to '$query'.\n" if $orig_query ne $query;
220
 
221
			($method,$host,$additional) = VGWhoIs::IPv6::getmethodv6($query);
222
		} elsif ($query =~ /^(urn:){0,1}oid:/i ) { # OID
223
			print "Query recognized as OID.\n";
224
 
225
			# preliminarily remove urn: and oid: from query
226
			# we need a dot so that we can use "oid:." in our patternfile too
227
			$query = VGWhoIs::OID::normalize_oid($query);
228
 
229
			my @arcs = split(/\./, $query); # TODO: warum geht split('.',$oid) nicht?
230
 
231
			($method,$host,$additional) = VGWhoIs::OID::getmethodoid(@arcs);
232
 
233
			# Whois OID query syntax definition by ViaThinkSoft (TODO: Apply for RFC):
234
			# - urn:oid:2.999 or oid:2.999
235
			# - Case insensitive
236
			# - Leading dot should be tolerated (urn:oid:.2.999)
237
			# - Leading zeros should be tolerated (urn:oid:.002.00999)
238
			# Idea: Should "oid:" be optional? Since 2.999 cannot be an IP ... But 1.2.3.4 could be one ...
239
 
240
			# There are many possibilities. We choose "oid:.2.999"
241
			$query = 'oid:' . VGWhoIs::OID::normalize_oid($query);
242
		} else {
243
			# Last resort: Query is probably a TLD, domain or handle, but we are not sure!
244
			# print "Query recognized as domain.\n";
245
 
246
			# Dot exists?	Type?	Punycode?	Filtering?
247
			# ------------------------------------------------
248
			# Yes		Domain	Yes		Yes
249
			# No		TLD	Yes		Yes
250
			# No		Handle	No*		Maybe
251
			# ------------------------------------------------
252
			# * = but it is unlikely that a handle contains non-latin characters
253
 
254
			# Filtering
255
			$query =~ y/[\x{00A0}-\x{FFFF}]a-zA-Z0-9:.,+_ -//cd;
256
			$query =~ s/\.$//;
257
			my $query_utf8_filtered = VGWhoIs::Utils::enforce_utf8($query);
258
			if ( $query_utf8 ne $query_utf8_filtered ) {
259
				# QUE: warn or print?
260
				warn "Attention: Query was filtered to '$query_utf8_filtered'.\n\n";
261
			}
262
 
263
			# Punycode decoding
264
			# my $ascii_query = Net::LibIDN::idn_to_ascii($query, 'utf-8')
265
			# We separate between spaces, so that "tld <unicode>" can be processed
266
			my @query_split = split(' ', $query);
267
			@query_split = map { Net::LibIDN::idn_to_ascii($_, 'utf-8') || '' } @query_split;
268
			my $ascii_query = join(' ', @query_split);
269
 
270
			# Query valid?
271
			if (!$ascii_query) { # e.g. $query = ".x"
272
				warn "'$query_utf8' is an invalid domain name.\n";
273
				return 2;
274
			}
275
 
276
			# Just information for the user
277
			if (index($query, ".") != -1) {
278
				print "Query recognized as domain.\n\n"; # TODO: aber wenn kein IDN?
279
			} else {
280
				print "Query is probably a handle or TLD.\n\n";
281
			}
282
 
283
			($method,$host,$additional) = VGWhoIs::Core::getmethodother($ascii_query);
284
		}
285
	}
286
 
287
	if ($method eq '') {
288
		warn "I don't know where to query that.\n";
289
		warn "If this is a valid domainname or handle, please file a bug report.\n";
290
		return 1;
291
	}
292
 
293
	# Wird in getmethod*() bereits ausgeführt.
294
	# Grund: Dann kann auch bei redirectwhois() dementsprechend in jedem Zwischenschritt gehandelt werden.
295
	# $host = $VGWhoIs::Core::mirror{$method.$host} if defined $VGWhoIs::Core::mirror{$method.$host};
296
 
297
	my ($result, $exitcode) = VGWhoIs::Core::doquery($query,$method,$host,$additional);
298
	$result = '' if !defined $result; # should not happen!
299
 
300
	my $antispam_replacements = 0;
301
	if ($VGWhoIs::Core::antispam) {
302
		# Protect email addresses (to allow e.g. "RIPE -B" for public services)
303
		# Note: eMail addresses have a much more complex structure, see http://code.google.com/p/isemail/
304
		# But this Regex should still prevent spammers from filtering eMail addresses,
305
		# even if e.g. the "wrong" (e.g. escaped) "@" is protected.
306
		$antispam_replacements = $result =~ s/(\S+)@(\S+)\.([^.\s]+)/$1 (at) $2 (dot) $3/g;
307
		# Alternative solution:
308
		# $antispam_replacements = $result =~ s/(\S+)@(\S+)\.([^.\s]+)/$1&$2.$3/g;
309
	}
310
 
311
	# We try to get $result to wide-string. Functions like LWP::Simple automatically convert UTF-8 into Unicode
312
	# (even without BOM sent through the whois gopher channel!), while subprograms and other methods are providing
313
	# raw UTF-8 data.
314
	$result = Encode::decode('utf8', VGWhoIs::Utils::trim($result), Encode::FB_CROAK) if !$rawoutput && VGWhoIs::Utils::is_utf8($result);
315
 
316
	# Don't allow DOS format
317
	$result =~ s/(\012|\015\012?)/\n/g;
318
 
319
	# Output everything
320
	print VGWhoIs::Utils::trim($result), "\n\n";
321
 
322
	if ($antispam_replacements > 0) {
323
		print "Note: The output has been modified by VGWhoIs.\n";
324
		print "$antispam_replacements eMail addresses have been anti-spam protected.\n";
325
		print "(Disable protection with \"vgwhois -e\")\n";
326
		print "\n";
327
	}
328
 
329
	# Footer
330
	print "--\n  To resolve one of the above handles:";
331
 
332
	if ($method =~ /whois/) {
333
		print "\n     whois -h $host";
334
		print ":$1" if ( $additional =~ /port=(\d+)/ );
335
		print " -- HANDLE\n";
336
	}
337
	elsif ($method eq "cgipost") {
338
		print "\n     POST $host\n";
339
		print "     $additional\n";
340
	}
341
	elsif ($method eq "cgi") {
342
		print "\n     $host\n";
343
	}
344
	elsif ($method eq "program") {
345
		print "\n     $host HANDLE\n";
346
	}
347
	# elsif ($method eq "wwwgrep") {
348
	else {
349
		# todo: add cgipostcurl etc
350
		print "\n     hmm. not sure (method = $method).\n";
351
	}
352
 
353
	print "  OTOH globally unique handles should be recognised directly by VGWhoIs.\n";
354
	print "  Please report errors or misfits via the Debian bug tracking system.\n";
355
 
356
	return $exitcode;
357
}