]> git.ipfire.org Git - ipfire-2.x.git/blame - src/wio/main/wio.pl
WIO - shutdown function removed, adjustments to IPsec status display
[ipfire-2.x.git] / src / wio / main / wio.pl
CommitLineData
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
37use strict;
38use POSIX qw(strftime);
39use Time::HiRes qw(gettimeofday tv_interval);
40use Net::Ping;
41use RRDs;
42use Fatal qw/ open /;
43
44require '/var/ipfire/general-functions.pl';
45require '/var/ipfire/lang.pl';
46require '/usr/lib/wio/wio-lib.pl';
47
48my ( $debug, $i, $t, $ib, $tb, $ivpn, $tvpn ) = '';
0d6cc79d
SF
49my $owner = getpwnam "nobody";
50my $group = getgrnam "nobody";
9c5dbb24 51my $ipadrfile = "/var/log/wio/wioips";
0d6cc79d
SF
52
53unless ( -e $ipadrfile ) { print ( "The file $ipadrfile doesn't exist!\n" ); exit; }
54
55foreach (@ARGV) {
56 if ( $_ eq '-d' || $_ eq '--debug') { $debug = 1; }
57 if ( $_ eq '-h' || $_ eq '--help' ) { die help(); }
58}
59
60my ( %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
67my $now = strftime "%a, %d.%m.%Y %H:%M:%S", localtime;
68my $logging = $wiosettings{'LOGGING'};
69my $mailstyle = $wiosettings{'MAILSTYLE'};
70my $mailremark = $wiosettings{'MAILREMARK'};
71my $timeout = $wiosettings{'TIMEOUT'};
0d6cc79d 72my $rrddir = "/var/log/rrd/wio";
9c5dbb24 73my $onoffip = "/var/log/wio/wioscip";
0d6cc79d
SF
74my $hostname = "$mainsettings{'HOSTNAME'}.$mainsettings{'DOMAINNAME'}";
75my $redactive = "/var/ipfire/red/active";
76my $rediface = "/var/ipfire/red/iface";
77my $reddev = '';
78
79if ( -e $rediface ) {
80 $reddev = &General::get_red_interface;
81}
82
83my $redip = $hostname;
84my $vpnpid = ( -e "/var/run/charon.pid" ? `awk '{print $1}' /var/run/charon.pid`: '');
85my $ovpnpid = ( -e "/var/run/openvpn.pid" ? `awk '{print $1}' /var/run/openvpn.pid`: '');
86
2a1c1735
SF
87my $steptime = $wiosettings{'CRON'} *= 60;
88my $i_ping = 'icmp';
89my $t_ping = 'tcp';
0d6cc79d
SF
90
91my $nr = 1;
0d6cc79d
SF
92
93my ( $togglestat, $arp, $time, $start, $timestamp ) = 0;
94my ( $id, $ipadr, $ipadrnew, $host, $hostnew, $enable, $remark, $dyndns, $dyndnsip ) = '';
95my ( $mail, $mailon, $mailoff, $ping, $on, $httphost, $mailen ) = '';
96my ( $msg, $logmsg, $mailmsg, $smailtxt, $infomsg, $client, $mode, $onbak, $arpclient ) = '';
97my ( $ping_i, $ping_t, $ping_ib, $ping_tb, $ping_iv, $ping_tv, $pingmode ) = '';
98my ( @tmp, @arptmp, @myarray, @status, @arpclients ) = '';
99my @ifaces = ('GREEN','BLUE','ORANGE');
100
9c5dbb24
SF
101if ( $netsettings{'RED_TYPE'} eq 'STATIC' || $netsettings{'RED_TYPE'} eq 'DHCP' ) {
102 push (@ifaces, "RED");
103}
104
0d6cc79d
SF
105if ( $mailsettings{'USEMAIL'} eq 'on' ) { $mailen = 'on'; }
106else { $mailen = 'off'; }
107
108if ( -e $redactive ) {
109 open(IPADDR, "/var/ipfire/red/local-ipaddress");
110 $redip = <IPADDR>;
111 close IPADDR;
112 chomp($redip);
113}
114
115if ($debug) {
116 $start = [gettimeofday];
117 startdebug();
118}
119
120foreach (@ifaces) {
121 if ( $netsettings{"${_}_DEV"} ne '' && $netsettings{"${_}_DEV"} ne 'disabled' ) {
122 my $output = `ifconfig $netsettings{"${_}_DEV"}`;
123
124 if ( grep (/RX bytes:0/, $output) ) { next; }
125 else {
126 @arptmp = `/usr/local/bin/wioscan -s $netsettings{"${_}_DEV"}`;
127
128 foreach $arpclient (@arptmp) {
129 push (@arpclients, (split (/\,/,$arpclient))[1]);
130 }
131 }
132 $output = '';
133 undef(@arptmp);
134 }
135}
136
137if ( -e "$onoffip" ) { open( FILE, "< $onoffip" ); }
138else { open( FILE, "< $ipadrfile" ); }
139@myarray = <FILE>;
140close(FILE);
141
142# ping all clients
143
144foreach (@myarray) {
145 chomp;
146 @tmp = split( /\,/, $_ );
147
148 ($id,$timestamp,$ipadr,$host,$enable,$remark,$dyndns,$mailon,$mailoff,$ping,$on,$httphost) = @tmp;
149
150 $timestamp = strftime "%d.%m.%Y - %H:%M:%S", localtime;
151
152 if ( $enable ne 'on' ) {
153 push (@status, "$id,$timestamp,$ipadr,$host,$enable,$remark,$dyndns,$mailon,$mailoff,$ping,$on,$httphost\n");
154 next;
155 }
156
157 if ( defined($dyndns) && ( $dyndns =~ 'on' ) ) {
158 ($dyndnsip, $infomsg) = &WIO::getdyndnsip($host, @myarray);
159 if ($dyndnsip ne $ipadr) { $ipadr = $dyndnsip; }
160 }
161
162 $ping_i = $ping_t = $ping_ib = $ping_tb = $ping_iv = $ping_tv = $pingmode = $arp = 0;
163
164 foreach (@arpclients) {
165 chomp;
166 unless ( $ipadr eq $_ )
167 {
168 $i = Net::Ping->new( $i_ping, $timeout );
169 unless ( defined $i ) { die "Can't create Net::Ping object $!"; }
170
171 $t = Net::Ping->new( $t_ping, $timeout );
172 unless ( defined $t ) { die "Can't create Net::Ping object $!"; }
173
174 $ib = Net::Ping->new( $i_ping, $timeout );
175 unless ( defined $ib ) { die "Can't create Net::Ping object $!"; }
176 $ib->bind($redip);
177
178 $tb = Net::Ping->new( $t_ping, $timeout );
179 unless ( defined $tb ) { die "Can't create Net::Ping object $!"; }
180 $tb->bind($redip);
181
182 if ($ovpnpid || $vpnpid)
183 {
184 $ivpn = Net::Ping->new( $i_ping, $timeout );
185 unless ( defined $ivpn ) { die "Can't create Net::Ping object $!"; }
186 $ivpn->bind($hostname);
187
188 $tvpn = Net::Ping->new( $t_ping, $timeout );
189 unless ( defined $tvpn ) { die "Can't create Net::Ping object $!"; }
190 $tvpn->bind($hostname);
191 }
192 }
193 else { $arp = 1 }
194 }
195
196 $client = ( ( $dyndns eq 'on' || $ping eq 'fqdn' ) ? $host : $ipadr );
197
198 if ($debug) {
199 printf "%2s %15s", $nr++, ($client ne $ipadr ? $ipadr : $client );
200 $time = [gettimeofday];
201 }
202
203 if ( $arp == 1
204 || ($ping_i = $i->ping($client))
205 || ($ping_t = $t->ping($client))
206 || ($ping_ib = $ib->ping($client))
207 || ($ping_tb = $tb->ping($client))
208 || ($ovpnpid?($ping_iv = $ivpn->ping($client)) : 0)
209 || ($vpnpid?($ping_tv = $tvpn->ping($client)) : 0) )
210 {
211 $mode = 100;
212 $msg = "$Lang::tr{'wio up'}";
213 $onbak = $on;
214 $togglestat = ( $on ne 'on' ) ? 1 : 0;
215 $on = 'on';
216 }
217 else {
218 $mode = 0;
219 $msg = "$Lang::tr{'wio down'}";
220 $onbak = $on;
221 $togglestat = ( $on ne 'off' ) ? 1 : 0;
222 $on = 'off';
223 }
224
225 push (@status, "$id,$timestamp,$ipadr,$host,$enable,$remark,$dyndns,$mailon,$mailoff,$ping,$on,$httphost\n");
226
227 if ($debug) {
228 $mail = '----';
229 if ( $mailon eq 'on' && $togglestat == 1 && $mode == 100 ) { $mail = 'Online'; }
230 if ( $mailoff eq 'on' && $togglestat == 1 && $mode == 0 ) { $mail = 'Offline'; }
231 if ( $dyndns ne 'on' ) { $dyndns = 'off'; }
232
233 $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';
234 printf "%7s%8s%9s%10s %.4f sek%12s\n", $ping, $dyndns, $msg, $mail, tv_interval($time), $pingmode;
235 }
236
237 if ( $host eq '' ) { $hostnew = 'n/a'; } else { $hostnew = $host; }
238 if ( $ipadr eq '' ) { $ipadrnew = 'n/a'; } else { $ipadrnew = $ipadr; }
239
240 if ( $logging eq 'on' ) {
241 $logmsg = "Client: $hostnew - IP: $ipadrnew - Status: $msg";
242 &General::log("wio","$logmsg");
243 }
244
245 if ( $mailen eq 'on' && $togglestat == 1 && ($mailon eq 'on' || $mailoff eq 'on')) {
246
247 if ( $mailstyle eq 'email' || ($mailstyle eq 'smail' && $smailtxt eq '') ) { $mailmsg .= "Date\t : $now\n\n"; }
248
249 $mailmsg .= "Client\t : $hostnew\nIP\t : $ipadrnew\nStatus\t : $msg\n";
250
251 if ( $mailremark eq 'on' && $remark ne '' ) {
252 $mailmsg .= "Remark : $remark\n\n";
253 }
254
255 if ( $mailstyle eq 'email' ) {
256 &WIO::mailsender("WIO - $host - $msg", $mailmsg);
257 undef ($mailmsg);
258 }
259 elsif ( $mailstyle eq 'smail' ) {
260 $smailtxt .= $mailmsg."\n";
261 undef ($mailmsg);
262 }
263 }
264
265 if ( $ping ne 'fqdn' ) { $client = $host; }
266 if ( $host eq '' ) { $client = $ipadr; }
267
268 updatewiodata("$id");
269
270 if ( $arp == 0 ) {
271 $i->close();
272 $t->close();
273 $ib->close();
274 $tb->close();
275 }
276
277 if ( ( -e $ovpnpid || -e $vpnpid ) && $arp == 0 ) {
278 $ivpn->close();
279 $tvpn->close();
280 }
281}
282
9c5dbb24 283# write ipadressfile new
0d6cc79d
SF
284
285if ( !-e $onoffip ) {
286 open( FILE, "> $ipadrfile" );
287 print FILE @status;
288 close(FILE);
289}
290else {
291 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");
292 chmod ( 0644, $ipadrfile );
293 chown ( $owner, $group, $ipadrfile );
294}
295
296if ($debug) {
297 printf ("\n$Lang::tr{'wio_scriptruntime'}: %.4f $Lang::tr{'age ssecond'}\n\n", tv_interval($start));
298}
299
300if ( $smailtxt ne '' ) { &WIO::mailsender($Lang::tr{'wio_sub'}, $smailtxt); }
301
0d6cc79d
SF
302undef (@tmp);
303undef (@myarray);
304undef (@status);
305undef (@arptmp);
306undef (@arpclients);
307
308if ( -e $onoffip ) { unlink($onoffip); }
309
310sub updatewiodata {
311 my $id = $_[0];
312
313 if ( !-e "$rrddir/$id.rrd" ) {
314 RRDs::create(
315 "$rrddir/$id.rrd", "--step=$steptime",
316 "DS:mode:GAUGE:3600:0:100", "RRA:AVERAGE:0.5:1:576",
317 "RRA:AVERAGE:0.5:6:672", "RRA:AVERAGE:0.5:24:732",
318 "RRA:AVERAGE:0.5:144:1460"
319 );
320 my $ERROR = RRDs::error;
321 print "Error in RRD::create for Who Is Online: $ERROR\n" if $ERROR;
322 }
323
324 RRDs::update( "$rrddir/$id.rrd", "-t", "mode", "N:$mode" );
325
326 my $error = RRDs::error;
327
328 if ($error) { &General::log("wio","$error"); }
329}
330
331sub startdebug {
0d6cc79d
SF
332printf "
333HOSTNAME : $hostname
334TIMEOUT : $timeout $Lang::tr{'age ssecond'}
335MAILSTYLE : $mailstyle
336RED TYPE : $netsettings{'RED_TYPE'}
337RED DEVICE : $reddev
338RED ADDRESS : $redip
0d6cc79d 339";
9c5dbb24
SF
340
341if ($ovpnpid) {printf "OpenVPN PID : $ovpnpid"}
342if ($vpnpid) {printf "IPsec PID : $vpnpid"}
343
0d6cc79d
SF
344printf "
345$Lang::tr{'wio_search'}
346
347%3s%17s%7s%8s%9s%10s%15s%12s
348---------------------------------------------------------------------------------
349","ID ", "$Lang::tr{'wio ipadress'}", "Ping", "DynDNS", "Status", "Mail", "$Lang::tr{'wio_answer_time'}", "$Lang::tr{'wio_answer'}";
350}
351
352sub help {
353 return "
354Who Is Online? for IPFire
355
356use option -d for debugging
357use option -h for help\n\n";
358}