]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/ipinfo.cgi
memory.cgi: Use perl mechanism to check if the rrd file for the swap exists.
[people/pmueller/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) 2007-2020 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 # Load colours for current theme...
36 my %color = ();
37 my %mainsettings = ();
38 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
39 &General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
40
41 my %cgiparams=();
42
43 &Header::showhttpheaders();
44
45 my @lines=();
46 my $extraquery='';
47
48 # Hash which contains the whois servers from
49 # the responisible RIR of the continent.
50 my %whois_servers_by_continent = (
51 "AF" => "whois.afrinic.net",
52 "AS" => "whois.apnic.net",
53 "EU" => "whois.ripe.net",
54 "NA" => "whois.arin.net",
55 "SA" => "whois.lacnic.net"
56 );
57
58 # Default whois server if no continent could be determined.
59 my $whois_server = "whois.arin.net";
60
61 my $addr = CGI::param("ip") || "";
62
63 if (&General::validip($addr)) {
64 # Write HTML page header...
65 &Header::openpage($Lang::tr{'ip info for'} . ' ' . $addr, 1, '');
66 &Header::openbigbox('100%', 'left');
67
68 my $iaddr = inet_aton($addr);
69 my $hostname = gethostbyaddr($iaddr, AF_INET);
70 if (!$hostname) { $hostname = $Lang::tr{'ptr lookup failed'}; }
71
72 # Enumerate location information for IP address...
73 my $ccode = &Location::Functions::lookup_country_code($addr);
74 my $cname = &Location::Functions::get_full_country_name($ccode);
75 my @network_flags = &Location::Functions::address_has_flags($addr);
76
77 # Try to get the continent of the country code.
78 my $continent = &Location::Functions::get_continent_code($ccode);
79
80 # Enumerate Autonomous System details for IP address...
81 my $asn = &Location::Functions::lookup_asn($addr);
82 my $as_name;
83 if ($asn) {
84 $as_name = &Location::Functions::get_as_name($asn);
85
86 # In case we have found an AS name, make output more readable...
87 if ($as_name) {
88 $as_name = "- " . $as_name;
89 }
90 $asn = "AS" . $asn;
91 } else {
92 $asn = $Lang::tr{'asn lookup failed'};
93 }
94
95 # Check if a whois server for the continent is known.
96 if($whois_servers_by_continent{$continent}) {
97 # Use it.
98 $whois_server = $whois_servers_by_continent{$continent};
99 }
100
101 my $flag_icon = &Location::Functions::get_flag_icon($ccode);
102
103 &Header::openbox('100%', 'left', $Lang::tr{'ip basic info'});
104
105 print <<END;
106 <center>
107 <table class="tbl" width='100%'>
108 <tr>
109 <td bgcolor='$color{'color22'}'><strong>$Lang::tr{'country'}</strong></td>
110 <td bgcolor='$color{'color22'}'>$cname <a href='country.cgi#$ccode'><img src="$flag_icon" border="0" align="absmiddle" alt="$cname" title="$cname" /></td>
111 </tr>
112 <tr>
113 <td bgcolor='$color{'color20'}'><strong>$Lang::tr{'ptr'}</strong></td>
114 <td bgcolor='$color{'color20'}'>$hostname</td>
115 </tr>
116 <tr>
117 <td bgcolor='$color{'color22'}'><strong>$Lang::tr{'autonomous system'}</strong></td>
118 <td bgcolor='$color{'color22'}'>$asn $as_name</td>
119 </tr>
120 END
121
122 # Check if the address has a flag.
123 if (@network_flags) {
124 # Get amount of flags for this network.
125 my $flags_amount = @network_flags;
126 my $processed_flags;
127
128 # Loop through the array of network_flags.
129 foreach my $network_flag (@network_flags) {
130 # Increment value of processed flags.
131 $processed_flags++;
132
133 # Get the network flag name.
134 my $network_flag_name = &Location::Functions::get_full_country_name($network_flag);
135
136 # Colorize columns.
137 my $col;
138 if ($processed_flags % 2) {
139 $col = "bgcolor='$color{'color20'}'"; }
140 else {
141 $col = "bgcolor='$color{'color22'}'";
142 }
143
144 # Write table row...
145 print <<END;
146 <tr>
147 <td $col><strong>$network_flag_name</strong></td>
148 <td $col>$Lang::tr{'yes'}</td>
149 </tr>
150 END
151 }
152 }
153
154 print " </table>\n";
155 print " </center>\n";
156
157 &Header::closebox();
158
159 my $sock = new IO::Socket::INET ( PeerAddr => $whois_server, PeerPort => 43, Proto => 'tcp');
160 if ($sock)
161 {
162 print $sock "$addr\n";
163 while (<$sock>) {
164 $extraquery = $1 if (/ReferralServer: whois:\/\/(\S+)\s+/);
165 push(@lines,$_);
166 }
167 close($sock);
168 if ($extraquery) {
169 undef (@lines);
170 $whois_server = $extraquery;
171 my $sock = new IO::Socket::INET ( PeerAddr => $whois_server, PeerPort => 43, Proto => 'tcp');
172 if ($sock)
173 {
174 print $sock "$addr\n";
175 while (<$sock>) {
176 push(@lines,$_);
177 }
178 }
179 else
180 {
181 @lines = ( "$Lang::tr{'unable to contact'} $whois_server" );
182 }
183 }
184 }
185 else
186 {
187 @lines = ( "$Lang::tr{'unable to contact'} $whois_server" );
188 }
189
190 &Header::openbox('100%', 'left', $Lang::tr{'whois results from'} . " " . $whois_server);
191
192 print "<pre>\n";
193 foreach my $line (@lines) {
194 print &Header::cleanhtml($line,"y");
195 }
196 print "</pre>\n";
197 &Header::closebox();
198 } else {
199 # Open HTML page header in case of invalid IP addresses
200 &Header::openpage($Lang::tr{'ip info'}, 1, '');
201 &Header::openbigbox('100%', 'left');
202
203 &Header::openbox('100%', 'left', $Lang::tr{'invalid ip'});
204 print <<EOF;
205 <p style="text-align: center;">
206 $Lang::tr{'invalid ip'}
207 </p>
208 EOF
209 &Header::closebox();
210 }
211
212 print <<END
213 <div align='center'>
214 <table width='80%'>
215 <tr>
216 <td align='center'><a href='$ENV{'HTTP_REFERER'}'><img src='/images/back.png' alt='$Lang::tr{'back'}' title='$Lang::tr{'back'}' /></a></td>
217 </tr>
218 </table>
219 </div>
220 END
221 ;
222
223 &Header::closebigbox();
224
225 &Header::closepage();