]>
Commit | Line | Data |
---|---|---|
0d6cc79d SF |
1 | #!/usr/bin/perl |
2 | # | |
3 | ############################################################################### | |
4 | # # | |
5 | # IPFire.org - A linux based firewall # | |
9c5dbb24 | 6 | # Copyright (C) 2017-2020 Stephan Feddersen <sfeddersen@ipfire.org> # |
0d6cc79d SF |
7 | # All Rights Reserved. # |
8 | # # | |
9 | # This program is free software: you can redistribute it and/or modify # | |
10 | # it under the terms of the GNU General Public License as published by # | |
11 | # the Free Software Foundation, either version 3 of the License, or # | |
12 | # (at your option) any later version. # | |
13 | # # | |
14 | # This program is distributed in the hope that it will be useful, # | |
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
17 | # GNU General Public License for more details. # | |
18 | # # | |
19 | # You should have received a copy of the GNU General Public License # | |
20 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
21 | # # | |
22 | ############################################################################### | |
23 | # | |
9c5dbb24 | 24 | # Version: 2020/06/01 13:29:23 |
0d6cc79d | 25 | # |
9c5dbb24 | 26 | # This wio.pl is based on the code from the IPCop WIO Addon |
0d6cc79d SF |
27 | # and is extremly adapted to work with IPFire. |
28 | # | |
29 | # Autor: Stephan Feddersen | |
30 | # Co-Autor: Alexander Marx | |
9c5dbb24 | 31 | # Co-Autor: Frank Mainz (for some code for the IPCop WIO Addon) |
0d6cc79d SF |
32 | # |
33 | ||
34 | # enable only the following on debugging purpose | |
35 | #use warnings; | |
36 | ||
37 | use strict; | |
38 | use POSIX qw(strftime); | |
39 | use Time::HiRes qw(gettimeofday tv_interval); | |
40 | use Net::Ping; | |
41 | use RRDs; | |
42 | use Fatal qw/ open /; | |
43 | ||
44 | require '/var/ipfire/general-functions.pl'; | |
45 | require '/var/ipfire/lang.pl'; | |
46 | require '/usr/lib/wio/wio-lib.pl'; | |
47 | ||
48 | my ( $debug, $i, $t, $ib, $tb, $ivpn, $tvpn ) = ''; | |
0d6cc79d SF |
49 | my $owner = getpwnam "nobody"; |
50 | my $group = getgrnam "nobody"; | |
9c5dbb24 | 51 | my $ipadrfile = "/var/log/wio/wioips"; |
0d6cc79d SF |
52 | |
53 | unless ( -e $ipadrfile ) { print ( "The file $ipadrfile doesn't exist!\n" ); exit; } | |
54 | ||
55 | foreach (@ARGV) { | |
56 | if ( $_ eq '-d' || $_ eq '--debug') { $debug = 1; } | |
57 | if ( $_ eq '-h' || $_ eq '--help' ) { die help(); } | |
58 | } | |
59 | ||
60 | my ( %wiosettings, %mainsettings, %mailsettings, %netsettings ) = (); | |
61 | ||
62 | &General::readhash('/var/ipfire/main/settings', \%mainsettings); | |
63 | &General::readhash('/var/ipfire/ethernet/settings', \%netsettings); | |
64 | &General::readhash('/var/ipfire/dma/mail.conf', \%mailsettings); | |
65 | &General::readhash("/var/ipfire/wio/wio.conf", \%wiosettings); | |
66 | ||
67 | my $now = strftime "%a, %d.%m.%Y %H:%M:%S", localtime; | |
68 | my $logging = $wiosettings{'LOGGING'}; | |
69 | my $mailstyle = $wiosettings{'MAILSTYLE'}; | |
70 | my $mailremark = $wiosettings{'MAILREMARK'}; | |
71 | my $timeout = $wiosettings{'TIMEOUT'}; | |
0d6cc79d | 72 | my $rrddir = "/var/log/rrd/wio"; |
9c5dbb24 | 73 | my $onoffip = "/var/log/wio/wioscip"; |
0d6cc79d SF |
74 | my $hostname = "$mainsettings{'HOSTNAME'}.$mainsettings{'DOMAINNAME'}"; |
75 | my $redactive = "/var/ipfire/red/active"; | |
76 | my $rediface = "/var/ipfire/red/iface"; | |
77 | my $reddev = ''; | |
78 | ||
79 | if ( -e $rediface ) { | |
80 | $reddev = &General::get_red_interface; | |
81 | } | |
82 | ||
83 | my $redip = $hostname; | |
84 | my $vpnpid = ( -e "/var/run/charon.pid" ? `awk '{print $1}' /var/run/charon.pid`: ''); | |
85 | my $ovpnpid = ( -e "/var/run/openvpn.pid" ? `awk '{print $1}' /var/run/openvpn.pid`: ''); | |
86 | ||
2a1c1735 SF |
87 | my $steptime = $wiosettings{'CRON'} *= 60; |
88 | my $i_ping = 'icmp'; | |
89 | my $t_ping = 'tcp'; | |
0d6cc79d SF |
90 | |
91 | my $nr = 1; | |
0d6cc79d SF |
92 | |
93 | my ( $togglestat, $arp, $time, $start, $timestamp ) = 0; | |
94 | my ( $id, $ipadr, $ipadrnew, $host, $hostnew, $enable, $remark, $dyndns, $dyndnsip ) = ''; | |
95 | my ( $mail, $mailon, $mailoff, $ping, $on, $httphost, $mailen ) = ''; | |
96 | my ( $msg, $logmsg, $mailmsg, $smailtxt, $infomsg, $client, $mode, $onbak, $arpclient ) = ''; | |
97 | my ( $ping_i, $ping_t, $ping_ib, $ping_tb, $ping_iv, $ping_tv, $pingmode ) = ''; | |
98 | my ( @tmp, @arptmp, @myarray, @status, @arpclients ) = ''; | |
99 | my @ifaces = ('GREEN','BLUE','ORANGE'); | |
100 | ||
9c5dbb24 | 101 | |
0d6cc79d SF |
102 | if ( $mailsettings{'USEMAIL'} eq 'on' ) { $mailen = 'on'; } |
103 | else { $mailen = 'off'; } | |
104 | ||
105 | if ( -e $redactive ) { | |
106 | open(IPADDR, "/var/ipfire/red/local-ipaddress"); | |
107 | $redip = <IPADDR>; | |
108 | close IPADDR; | |
109 | chomp($redip); | |
110 | } | |
111 | ||
112 | if ($debug) { | |
113 | $start = [gettimeofday]; | |
114 | startdebug(); | |
115 | } | |
116 | ||
117 | foreach (@ifaces) { | |
118 | if ( $netsettings{"${_}_DEV"} ne '' && $netsettings{"${_}_DEV"} ne 'disabled' ) { | |
119 | my $output = `ifconfig $netsettings{"${_}_DEV"}`; | |
120 | ||
121 | if ( grep (/RX bytes:0/, $output) ) { next; } | |
122 | else { | |
123 | @arptmp = `/usr/local/bin/wioscan -s $netsettings{"${_}_DEV"}`; | |
124 | ||
125 | foreach $arpclient (@arptmp) { | |
126 | push (@arpclients, (split (/\,/,$arpclient))[1]); | |
127 | } | |
128 | } | |
129 | $output = ''; | |
130 | undef(@arptmp); | |
131 | } | |
132 | } | |
133 | ||
134 | if ( -e "$onoffip" ) { open( FILE, "< $onoffip" ); } | |
135 | else { open( FILE, "< $ipadrfile" ); } | |
136 | @myarray = <FILE>; | |
137 | close(FILE); | |
138 | ||
139 | # ping all clients | |
140 | ||
141 | foreach (@myarray) { | |
142 | chomp; | |
143 | @tmp = split( /\,/, $_ ); | |
144 | ||
145 | ($id,$timestamp,$ipadr,$host,$enable,$remark,$dyndns,$mailon,$mailoff,$ping,$on,$httphost) = @tmp; | |
146 | ||
147 | $timestamp = strftime "%d.%m.%Y - %H:%M:%S", localtime; | |
148 | ||
149 | if ( $enable ne 'on' ) { | |
150 | push (@status, "$id,$timestamp,$ipadr,$host,$enable,$remark,$dyndns,$mailon,$mailoff,$ping,$on,$httphost\n"); | |
151 | next; | |
152 | } | |
153 | ||
154 | if ( defined($dyndns) && ( $dyndns =~ 'on' ) ) { | |
155 | ($dyndnsip, $infomsg) = &WIO::getdyndnsip($host, @myarray); | |
156 | if ($dyndnsip ne $ipadr) { $ipadr = $dyndnsip; } | |
157 | } | |
158 | ||
159 | $ping_i = $ping_t = $ping_ib = $ping_tb = $ping_iv = $ping_tv = $pingmode = $arp = 0; | |
160 | ||
161 | foreach (@arpclients) { | |
162 | chomp; | |
163 | unless ( $ipadr eq $_ ) | |
164 | { | |
165 | $i = Net::Ping->new( $i_ping, $timeout ); | |
166 | unless ( defined $i ) { die "Can't create Net::Ping object $!"; } | |
167 | ||
168 | $t = Net::Ping->new( $t_ping, $timeout ); | |
169 | unless ( defined $t ) { die "Can't create Net::Ping object $!"; } | |
170 | ||
171 | $ib = Net::Ping->new( $i_ping, $timeout ); | |
172 | unless ( defined $ib ) { die "Can't create Net::Ping object $!"; } | |
173 | $ib->bind($redip); | |
174 | ||
175 | $tb = Net::Ping->new( $t_ping, $timeout ); | |
176 | unless ( defined $tb ) { die "Can't create Net::Ping object $!"; } | |
177 | $tb->bind($redip); | |
178 | ||
179 | if ($ovpnpid || $vpnpid) | |
180 | { | |
181 | $ivpn = Net::Ping->new( $i_ping, $timeout ); | |
182 | unless ( defined $ivpn ) { die "Can't create Net::Ping object $!"; } | |
183 | $ivpn->bind($hostname); | |
184 | ||
185 | $tvpn = Net::Ping->new( $t_ping, $timeout ); | |
186 | unless ( defined $tvpn ) { die "Can't create Net::Ping object $!"; } | |
187 | $tvpn->bind($hostname); | |
188 | } | |
189 | } | |
190 | else { $arp = 1 } | |
191 | } | |
192 | ||
193 | $client = ( ( $dyndns eq 'on' || $ping eq 'fqdn' ) ? $host : $ipadr ); | |
194 | ||
195 | if ($debug) { | |
196 | printf "%2s %15s", $nr++, ($client ne $ipadr ? $ipadr : $client ); | |
197 | $time = [gettimeofday]; | |
198 | } | |
199 | ||
200 | if ( $arp == 1 | |
201 | || ($ping_i = $i->ping($client)) | |
202 | || ($ping_t = $t->ping($client)) | |
203 | || ($ping_ib = $ib->ping($client)) | |
204 | || ($ping_tb = $tb->ping($client)) | |
205 | || ($ovpnpid?($ping_iv = $ivpn->ping($client)) : 0) | |
206 | || ($vpnpid?($ping_tv = $tvpn->ping($client)) : 0) ) | |
207 | { | |
208 | $mode = 100; | |
209 | $msg = "$Lang::tr{'wio up'}"; | |
210 | $onbak = $on; | |
211 | $togglestat = ( $on ne 'on' ) ? 1 : 0; | |
212 | $on = 'on'; | |
213 | } | |
214 | else { | |
215 | $mode = 0; | |
216 | $msg = "$Lang::tr{'wio down'}"; | |
217 | $onbak = $on; | |
218 | $togglestat = ( $on ne 'off' ) ? 1 : 0; | |
219 | $on = 'off'; | |
220 | } | |
221 | ||
222 | push (@status, "$id,$timestamp,$ipadr,$host,$enable,$remark,$dyndns,$mailon,$mailoff,$ping,$on,$httphost\n"); | |
223 | ||
224 | if ($debug) { | |
225 | $mail = '----'; | |
226 | if ( $mailon eq 'on' && $togglestat == 1 && $mode == 100 ) { $mail = 'Online'; } | |
227 | if ( $mailoff eq 'on' && $togglestat == 1 && $mode == 0 ) { $mail = 'Offline'; } | |
228 | if ( $dyndns ne 'on' ) { $dyndns = 'off'; } | |
229 | ||
230 | $pingmode = $arp ? 'ARPSCAN' : $ping_i ? 'ICMP' : $ping_t ? 'TCP' : $ping_ib ? 'ICMP+BIND' : $ping_tb ? 'TCP+BIND' : $ping_iv ? 'VPN ICMP' : $ping_tv ? 'VPN TCP' : 'NO ECHO'; | |
231 | printf "%7s%8s%9s%10s %.4f sek%12s\n", $ping, $dyndns, $msg, $mail, tv_interval($time), $pingmode; | |
232 | } | |
233 | ||
234 | if ( $host eq '' ) { $hostnew = 'n/a'; } else { $hostnew = $host; } | |
235 | if ( $ipadr eq '' ) { $ipadrnew = 'n/a'; } else { $ipadrnew = $ipadr; } | |
236 | ||
237 | if ( $logging eq 'on' ) { | |
238 | $logmsg = "Client: $hostnew - IP: $ipadrnew - Status: $msg"; | |
239 | &General::log("wio","$logmsg"); | |
240 | } | |
241 | ||
242 | if ( $mailen eq 'on' && $togglestat == 1 && ($mailon eq 'on' || $mailoff eq 'on')) { | |
243 | ||
244 | if ( $mailstyle eq 'email' || ($mailstyle eq 'smail' && $smailtxt eq '') ) { $mailmsg .= "Date\t : $now\n\n"; } | |
245 | ||
246 | $mailmsg .= "Client\t : $hostnew\nIP\t : $ipadrnew\nStatus\t : $msg\n"; | |
247 | ||
248 | if ( $mailremark eq 'on' && $remark ne '' ) { | |
249 | $mailmsg .= "Remark : $remark\n\n"; | |
250 | } | |
251 | ||
252 | if ( $mailstyle eq 'email' ) { | |
253 | &WIO::mailsender("WIO - $host - $msg", $mailmsg); | |
254 | undef ($mailmsg); | |
255 | } | |
256 | elsif ( $mailstyle eq 'smail' ) { | |
257 | $smailtxt .= $mailmsg."\n"; | |
258 | undef ($mailmsg); | |
259 | } | |
260 | } | |
261 | ||
262 | if ( $ping ne 'fqdn' ) { $client = $host; } | |
263 | if ( $host eq '' ) { $client = $ipadr; } | |
264 | ||
265 | updatewiodata("$id"); | |
266 | ||
267 | if ( $arp == 0 ) { | |
268 | $i->close(); | |
269 | $t->close(); | |
270 | $ib->close(); | |
271 | $tb->close(); | |
272 | } | |
273 | ||
274 | if ( ( -e $ovpnpid || -e $vpnpid ) && $arp == 0 ) { | |
275 | $ivpn->close(); | |
276 | $tvpn->close(); | |
277 | } | |
278 | } | |
279 | ||
9c5dbb24 | 280 | # write ipadressfile new |
0d6cc79d SF |
281 | |
282 | if ( !-e $onoffip ) { | |
283 | open( FILE, "> $ipadrfile" ); | |
284 | print FILE @status; | |
285 | close(FILE); | |
286 | } | |
287 | else { | |
288 | system("/bin/sed -i 's#$tmp[0],$tmp[1],$tmp[2],$tmp[3],$tmp[4],$tmp[5],$tmp[6],$tmp[7],$tmp[8],$tmp[9],$tmp[10],$tmp[11]#$id,$timestamp,$ipadr,$host,$enable,$remark,$dyndns,$mailon,$mailoff,$ping,$on,$httphost#g' $ipadrfile"); | |
289 | chmod ( 0644, $ipadrfile ); | |
290 | chown ( $owner, $group, $ipadrfile ); | |
291 | } | |
292 | ||
293 | if ($debug) { | |
294 | printf ("\n$Lang::tr{'wio_scriptruntime'}: %.4f $Lang::tr{'age ssecond'}\n\n", tv_interval($start)); | |
295 | } | |
296 | ||
297 | if ( $smailtxt ne '' ) { &WIO::mailsender($Lang::tr{'wio_sub'}, $smailtxt); } | |
298 | ||
0d6cc79d SF |
299 | undef (@tmp); |
300 | undef (@myarray); | |
301 | undef (@status); | |
302 | undef (@arptmp); | |
303 | undef (@arpclients); | |
304 | ||
305 | if ( -e $onoffip ) { unlink($onoffip); } | |
306 | ||
307 | sub updatewiodata { | |
308 | my $id = $_[0]; | |
309 | ||
310 | if ( !-e "$rrddir/$id.rrd" ) { | |
311 | RRDs::create( | |
312 | "$rrddir/$id.rrd", "--step=$steptime", | |
313 | "DS:mode:GAUGE:3600:0:100", "RRA:AVERAGE:0.5:1:576", | |
314 | "RRA:AVERAGE:0.5:6:672", "RRA:AVERAGE:0.5:24:732", | |
315 | "RRA:AVERAGE:0.5:144:1460" | |
316 | ); | |
317 | my $ERROR = RRDs::error; | |
318 | print "Error in RRD::create for Who Is Online: $ERROR\n" if $ERROR; | |
319 | } | |
320 | ||
321 | RRDs::update( "$rrddir/$id.rrd", "-t", "mode", "N:$mode" ); | |
322 | ||
323 | my $error = RRDs::error; | |
324 | ||
325 | if ($error) { &General::log("wio","$error"); } | |
326 | } | |
327 | ||
328 | sub startdebug { | |
0d6cc79d SF |
329 | printf " |
330 | HOSTNAME : $hostname | |
331 | TIMEOUT : $timeout $Lang::tr{'age ssecond'} | |
332 | MAILSTYLE : $mailstyle | |
333 | RED TYPE : $netsettings{'RED_TYPE'} | |
334 | RED DEVICE : $reddev | |
335 | RED ADDRESS : $redip | |
0d6cc79d | 336 | "; |
9c5dbb24 SF |
337 | |
338 | if ($ovpnpid) {printf "OpenVPN PID : $ovpnpid"} | |
339 | if ($vpnpid) {printf "IPsec PID : $vpnpid"} | |
340 | ||
0d6cc79d SF |
341 | printf " |
342 | $Lang::tr{'wio_search'} | |
343 | ||
344 | %3s%17s%7s%8s%9s%10s%15s%12s | |
345 | --------------------------------------------------------------------------------- | |
346 | ","ID ", "$Lang::tr{'wio ipadress'}", "Ping", "DynDNS", "Status", "Mail", "$Lang::tr{'wio_answer_time'}", "$Lang::tr{'wio_answer'}"; | |
347 | } | |
348 | ||
349 | sub help { | |
350 | return " | |
351 | Who Is Online? for IPFire | |
352 | ||
353 | use option -d for debugging | |
354 | use option -h for help\n\n"; | |
355 | } |