]> git.ipfire.org Git - ipfire-2.x.git/blame - src/wio/wio-lib.pl
WIO - shutdown function removed, adjustments to IPsec status display
[ipfire-2.x.git] / src / wio / wio-lib.pl
CommitLineData
0d6cc79d
SF
1#!/usr/bin/perl
2#
3###############################################################################
4# #
5# IPFire.org - A linux based firewall #
eebbe981 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#
9755fdf9 24# Version: 2020/05/25 19:39:23
0d6cc79d 25#
9755fdf9 26# This wio-lib.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
9755fdf9 31# Co-Autor: Frank Mainz (for some code for the IPCop WIO Addon)
0d6cc79d
SF
32#
33
34package WIO;
35
36# enable only the following on debugging purpose
37#use warnings;
38
39use strict;
40use Socket;
41use Time::Local;
42use MIME::Lite;
43
44require '/var/ipfire/general-functions.pl';
45require '/var/ipfire/header.pl';
46require '/var/ipfire/lang.pl';
47
9755fdf9 48my %mailsettings = ();
0d6cc79d 49
9755fdf9 50&General::readhash('/var/ipfire/dma/mail.conf', \%mailsettings);
0d6cc79d
SF
51
52############################################################################################################################
53
54sub getdyndnsip {
55 my $ipadr = $_[0];
56 my $host = $_[1];
57 my @fetchip = ();
58
eebbe981 59 if ( -e "/var/ipfire/red/active" ) {
0d6cc79d
SF
60 @fetchip = gethostbyname($host);
61
62 if ( defined($fetchip[0]) ) {
63 @fetchip = map ( &Socket::inet_ntoa($_), @fetchip[4..$#fetchip]);
64 return ($fetchip[0], $Lang::tr{'wio_dyndns_success'});
65 }
66 }
67 else {
68 return ($ipadr, $Lang::tr{'wio_dyndns_info'});
69 }
70}
71
72############################################################################################################################
73
74sub contime {
eebbe981
SF
75 chomp(my $str = $_[0]);
76 chomp(my $vpn = $_[1]);
77
0d6cc79d
SF
78 my %m = ();
79 @m{qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/} = (0 .. 11);
80
eebbe981
SF
81 my $totalsecs = '';
82
83 if ( $vpn eq 'ipsec' ) {
84 my @temp = split (/ /, $str);
85
86 if ( $temp[1] eq 'seconds' ) {
87 $totalsecs = $temp[0];
88 }
89
90 if ( $temp[1] eq 'minutes' ) {
91 $totalsecs = $temp[0] * 60;
92 }
9755fdf9
SF
93
94 if ( $temp[1] eq 'hours' ) {
95 $totalsecs = $temp[0] * 3600;
96 }
97
98 if ( $temp[1] eq 'days' ) {
99 $totalsecs = $temp[0] * 86400;
100 }
eebbe981 101 }
0d6cc79d 102
eebbe981
SF
103 if ( $vpn eq 'ovpn' ) {
104 if ( $str =~ /^\w{3}\ ([a-zA-Z]+)\ (\d{1,2})\ (\d{2})\:(\d{2})\:(\d{2}) (\d{4})$/ ||
105 $str =~ /^\w{3}\ ([a-zA-Z]+)\ (\d{1})\ (\d{2})\:(\d{2})\:(\d{2}) (\d{4})$/ )
106 {
107 my $past = timelocal($5, $4, $3, $2, $m{$1}, $6);
108 my $now = time;
109 $totalsecs = $now - $past;
110 }
111 }
112
113 if ( $totalsecs ne '' ) {
0d6cc79d
SF
114 my $days = int($totalsecs / 86400);
115 my $totalhours = int($totalsecs / 3600);
116 my $hours = $totalhours % 24;
117 my $totalmins = int($totalsecs / 60);
118 my $mins = $totalmins % 60;
119 my $secs = $totalsecs % 60;
120
121 return "${days}d ${hours}h ${mins}m ${secs}s";
122 }
123 else {
124 return;
125 }
126}
127
128############################################################################################################################
129
130sub statustime {
131 my $str = $_[0];
132 my ( $day, $mon ) = '';
133
134 my %m = qw ( Jan 01 Feb 02 Mar 03 Apr 04 May 05 Jun 06 Jul 07 Aug 08 Sep 09 Oct 10 Nov 11 Dec 12 );
135
136 if ( $str =~ /^\w{3}\ ([a-zA-Z]+)\ (\d{1,2})\ (\d{2})\:(\d{2})\:(\d{2}) (\d{4})$/ ||
137 $str =~ /^\w{3}\ ([a-zA-Z]+)\ (\d{1})\ (\d{2})\:(\d{2})\:(\d{2}) (\d{4})$/ )
138 {
139 $mon = $m{$1};
140
141 if (length $2 < 2) { $day = "0$2"; }
142 else { $day = $2; }
143
144 return "$day.$mon.$6 - $3:$4:$5";
145 }
146 else {
147 return;
148 }
149}
150
151############################################################################################################################
152
153sub mailsender {
eebbe981
SF
154 my $msg = '';
155
0d6cc79d 156 $msg = MIME::Lite->new(
9755fdf9
SF
157 From => $mailsettings{'SENDER'},
158 To => $mailsettings{'RECIPIENT'},
0d6cc79d
SF
159 Subject => $_[0],
160 Type => 'multipart/mixed'
161 );
162
163 $msg->attach(
164 Type => 'TEXT',
165 Data => $_[1]
166 );
167
168 $msg->send_by_sendmail;
169}
170
171############################################################################################################################
172
173sub checkinto {
174 my ($checkip, $checkhost, @checkfile) = @_;
175
176 if ( $checkip ne '' ) {
177 foreach (@checkfile) {
178 chomp;
179 if ( (split (/\,/, $_))[2] eq $checkip ) { return $Lang::tr{'wio_ip_exists'}; last; }
180 }
181 }
182
183 if ( $checkhost ne '' ) {
184 foreach (@checkfile) {
185 chomp;
186 if ( (split (/\,/, $_))[3] eq $checkhost ) {
187 if ( $checkip ne '' ) {
188 my $fileip = (split (/\,/, $_))[2];
189
190 $fileip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
191
192 my $fileip1 = $1;
193 my $fileip2 = $2;
194 my $fileip3 = $3;
195 my $fileip4 = $4;
196
197 $checkip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
198
199 if ( $fileip1 == $1 && $fileip2 == $2 && $fileip3 == $3 ) {
200 return $Lang::tr{'wio_host_exists'}; last; }
201 }
202 else { return $Lang::tr{'wio_host_exists'}; last; }
203 }
204 }
205 }
206
207 return;
208}
209
210############################################################################################################################
211
212sub clearfile {
213 my $file = $_[0];
214
215 open(FILE, "> $file");
216 close(FILE);
217}
218
219############################################################################################################################
220
221sub color_devices() {
222 my $output = shift;
223
224 if ( uc($output) eq "GREEN0" ) { $output = "<b><font color ='$Header::colourgreen'>$output</b>";}
225 elsif ( uc($output) eq "BLUE0" ) { $output = "<b><font color ='$Header::colourblue'>$output</b>"; }
226 elsif ( uc($output) eq "ORANGE0" ) { $output = "<b><font color ='$Header::colourorange'>$output</b>"; }
227 elsif ( uc($output) eq "RED0" ) { $output = "<b><font color ='$Header::colourred'>$output</b>"; }
228 else { return $output = "<b><font color ='#696565'>$output</b>"; }
229
230 return $output;
231}
232
233return 1;