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