]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/ipinfo.cgi
Adjust CGI files to work with latest location-function.pl changes.
[ipfire-2.x.git] / html / cgi-bin / ipinfo.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use CGI;
23 use IO::Socket;
24 use strict;
25
26 # enable only the following on debugging purpose
27 #use warnings;
28 #use CGI::Carp 'fatalsToBrowser';
29
30 require '/var/ipfire/general-functions.pl';
31 require "${General::swroot}/lang.pl";
32 require "${General::swroot}/header.pl";
33 require "${General::swroot}/location-functions.pl";
34
35 my %cgiparams=();
36
37 &Header::showhttpheaders();
38
39 &Header::openpage($Lang::tr{'ip info'}, 1, '');
40 &Header::openbigbox('100%', 'left');
41 my @lines=();
42 my $extraquery='';
43
44 # Hash which contains the whois servers from
45 # the responisible RIR of the continent.
46 my %whois_servers_by_continent = (
47 "AF" => "whois.afrinic.net",
48 "AS" => "whois.apnic.net",
49 "EU" => "whois.ripe.net",
50 "NA" => "whois.arin.net",
51 "SA" => "whois.lacnic.net"
52 );
53
54 # Default whois server if no continent could be determined.
55 my $whois_server = "whois.arin.net";
56
57 my $addr = CGI::param("ip") || "";
58
59 if (&General::validip($addr)) {
60 my $iaddr = inet_aton($addr);
61 my $hostname = gethostbyaddr($iaddr, AF_INET);
62 if (!$hostname) { $hostname = $Lang::tr{'lookup failed'}; }
63
64 # enumerate location information for IP address...
65 my $ccode = &Location::Functions::lookup_country_code($addr);
66 my @network_flags = &Location::Functions::address_has_flags($addr);
67
68 # Try to get the continent of the country code.
69 my $continent = &Location::Functions::get_continent_code($ccode);
70
71 # Check if a whois server for the continent is known.
72 if($whois_servers_by_continent{$continent}) {
73 # Use it.
74 $whois_server = $whois_servers_by_continent{$continent};
75 }
76
77 my $flag_icon = &Location::Functions::get_flag_icon($ccode);
78
79 my $sock = new IO::Socket::INET ( PeerAddr => $whois_server, PeerPort => 43, Proto => 'tcp');
80 if ($sock)
81 {
82 print $sock "$addr\n";
83 while (<$sock>) {
84 $extraquery = $1 if (/ReferralServer: whois:\/\/(\S+)\s+/);
85 push(@lines,$_);
86 }
87 close($sock);
88 if ($extraquery) {
89 undef (@lines);
90 $whois_server = $extraquery;
91 my $sock = new IO::Socket::INET ( PeerAddr => $whois_server, PeerPort => 43, Proto => 'tcp');
92 if ($sock)
93 {
94 print $sock "$addr\n";
95 while (<$sock>) {
96 push(@lines,$_);
97 }
98 }
99 else
100 {
101 @lines = ( "$Lang::tr{'unable to contact'} $whois_server" );
102 }
103 }
104 }
105 else
106 {
107 @lines = ( "$Lang::tr{'unable to contact'} $whois_server" );
108 }
109
110 &Header::openbox('100%', 'left', $addr . " <a href='country.cgi#$ccode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode' title='$ccode' /></a> (" . $hostname . ') : '.$whois_server);
111
112 # Check if the address has a flag.
113 if (@network_flags) {
114 # Get amount of flags for this network.
115 my $flags_amount = @network_flags;
116 my $processed_flags;
117
118 # The message string which will be displayed.
119 my $message_string = "This address is marked as";
120
121 # Loop through the array of network_flags.
122 foreach my $network_flag (@network_flags) {
123 # Increment value of processed flags.
124 $processed_flags++;
125
126 # Get the network flag name.
127 my $network_flag_name = &Location::Functions::get_full_country_name($network_flag);
128
129 # Add the flag name to the message string.
130 $message_string = "$message_string" . " $network_flag_name";
131
132 # Check if multiple flags are set for this network.
133 if ($flags_amount gt "1") {
134 # Check if the the current flag is the next-to-last one.
135 if ($processed_flags eq $flags_amount - 1) {
136 $message_string = "$message_string" . " and ";
137
138 # Check if the current flag it the last one.
139 } elsif ($processed_flags eq $flags_amount) {
140 # The message is finished add a dot for ending the sentence.
141 $message_string = "$message_string" . ".";
142
143 # Otherwise add a simple comma to the message string.
144 } else {
145 $message_string = "$message_string" . ", ";
146 }
147 } else {
148 # Nothing special to do, simple add a dot to finish the sentence.
149 $message_string = "$message_string" . ".";
150 }
151 }
152
153 # Display the generated notice.
154 print "<h3>$message_string</h3>\n";
155 print "<br>\n";
156 }
157
158 print "<pre>\n";
159 foreach my $line (@lines) {
160 print &Header::cleanhtml($line,"y");
161 }
162 print "</pre>\n";
163 &Header::closebox();
164 } else {
165 &Header::openbox('100%', 'left', $Lang::tr{'invalid ip'});
166 print <<EOF;
167 <p style="text-align: center;">
168 $Lang::tr{'invalid ip'}
169 </p>
170 EOF
171 &Header::closebox();
172 }
173
174 print <<END
175 <div align='center'>
176 <table width='80%'>
177 <tr>
178 <td align='center'><a href='$ENV{'HTTP_REFERER'}'><img src='/images/back.png' alt='$Lang::tr{'back'}' title='$Lang::tr{'back'}' /></a></td>
179 </tr>
180 </table>
181 </div>
182 END
183 ;
184
185 &Header::closebigbox();
186
187 &Header::closepage();