]>
Commit | Line | Data |
---|---|---|
0d6cc79d SF |
1 | #!/usr/bin/perl |
2 | # | |
3 | ############################################################################### | |
4 | # # | |
5 | # IPFire.org - A linux based firewall # | |
6 | # Copyright (C) 2017 Stephan Feddersen <addons@h-loit.de> # | |
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 | # id: wio-lib.pl, v1.3.1 2017/07/11 21:31:16 sfeddersen | |
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 | &General::readhash($mailfile, \%mail); | |
51 | ||
52 | my $redactive = "/var/ipfire/red/active"; | |
53 | my $msg = ''; | |
54 | ||
55 | ############################################################################################################################ | |
56 | ||
57 | sub getdyndnsip { | |
58 | my $ipadr = $_[0]; | |
59 | my $host = $_[1]; | |
60 | my @fetchip = (); | |
61 | ||
62 | if ( -e $redactive ) { | |
63 | @fetchip = gethostbyname($host); | |
64 | ||
65 | if ( defined($fetchip[0]) ) { | |
66 | @fetchip = map ( &Socket::inet_ntoa($_), @fetchip[4..$#fetchip]); | |
67 | return ($fetchip[0], $Lang::tr{'wio_dyndns_success'}); | |
68 | } | |
69 | } | |
70 | else { | |
71 | return ($ipadr, $Lang::tr{'wio_dyndns_info'}); | |
72 | } | |
73 | } | |
74 | ||
75 | ############################################################################################################################ | |
76 | ||
77 | sub contime { | |
78 | my $str = $_[0]; | |
79 | my %m = (); | |
80 | @m{qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/} = (0 .. 11); | |
81 | ||
82 | if ( $str =~ /^\w{3}\ ([a-zA-Z]+)\ (\d{1,2})\ (\d{2})\:(\d{2})\:(\d{2}) (\d{4})$/ || | |
83 | $str =~ /^\w{3}\ ([a-zA-Z]+)\ (\d{1})\ (\d{2})\:(\d{2})\:(\d{2}) (\d{4})$/ ) | |
84 | { | |
85 | my $past = timelocal($5, $4, $3, $2, $m{$1}, $6); | |
86 | my $now = time; | |
87 | ||
88 | my $totalsecs = $now - $past; | |
89 | my $days = int($totalsecs / 86400); | |
90 | my $totalhours = int($totalsecs / 3600); | |
91 | my $hours = $totalhours % 24; | |
92 | my $totalmins = int($totalsecs / 60); | |
93 | my $mins = $totalmins % 60; | |
94 | my $secs = $totalsecs % 60; | |
95 | ||
96 | return "${days}d ${hours}h ${mins}m ${secs}s"; | |
97 | } | |
98 | else { | |
99 | return; | |
100 | } | |
101 | } | |
102 | ||
103 | ############################################################################################################################ | |
104 | ||
105 | sub statustime { | |
106 | my $str = $_[0]; | |
107 | my ( $day, $mon ) = ''; | |
108 | ||
109 | 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 ); | |
110 | ||
111 | if ( $str =~ /^\w{3}\ ([a-zA-Z]+)\ (\d{1,2})\ (\d{2})\:(\d{2})\:(\d{2}) (\d{4})$/ || | |
112 | $str =~ /^\w{3}\ ([a-zA-Z]+)\ (\d{1})\ (\d{2})\:(\d{2})\:(\d{2}) (\d{4})$/ ) | |
113 | { | |
114 | $mon = $m{$1}; | |
115 | ||
116 | if (length $2 < 2) { $day = "0$2"; } | |
117 | else { $day = $2; } | |
118 | ||
119 | return "$day.$mon.$6 - $3:$4:$5"; | |
120 | } | |
121 | else { | |
122 | return; | |
123 | } | |
124 | } | |
125 | ||
126 | ############################################################################################################################ | |
127 | ||
128 | sub mailsender { | |
129 | $msg = MIME::Lite->new( | |
130 | From => $mail{'SENDER'}, | |
131 | To => $mail{'RECIPIENT'}, | |
132 | Subject => $_[0], | |
133 | Type => 'multipart/mixed' | |
134 | ); | |
135 | ||
136 | $msg->attach( | |
137 | Type => 'TEXT', | |
138 | Data => $_[1] | |
139 | ); | |
140 | ||
141 | $msg->send_by_sendmail; | |
142 | } | |
143 | ||
144 | ############################################################################################################################ | |
145 | ||
146 | sub checkinto { | |
147 | my ($checkip, $checkhost, @checkfile) = @_; | |
148 | ||
149 | if ( $checkip ne '' ) { | |
150 | foreach (@checkfile) { | |
151 | chomp; | |
152 | if ( (split (/\,/, $_))[2] eq $checkip ) { return $Lang::tr{'wio_ip_exists'}; last; } | |
153 | } | |
154 | } | |
155 | ||
156 | if ( $checkhost ne '' ) { | |
157 | foreach (@checkfile) { | |
158 | chomp; | |
159 | if ( (split (/\,/, $_))[3] eq $checkhost ) { | |
160 | if ( $checkip ne '' ) { | |
161 | my $fileip = (split (/\,/, $_))[2]; | |
162 | ||
163 | $fileip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/; | |
164 | ||
165 | my $fileip1 = $1; | |
166 | my $fileip2 = $2; | |
167 | my $fileip3 = $3; | |
168 | my $fileip4 = $4; | |
169 | ||
170 | $checkip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/; | |
171 | ||
172 | if ( $fileip1 == $1 && $fileip2 == $2 && $fileip3 == $3 ) { | |
173 | return $Lang::tr{'wio_host_exists'}; last; } | |
174 | } | |
175 | else { return $Lang::tr{'wio_host_exists'}; last; } | |
176 | } | |
177 | } | |
178 | } | |
179 | ||
180 | return; | |
181 | } | |
182 | ||
183 | ############################################################################################################################ | |
184 | ||
185 | sub clearfile { | |
186 | my $file = $_[0]; | |
187 | ||
188 | open(FILE, "> $file"); | |
189 | close(FILE); | |
190 | } | |
191 | ||
192 | ############################################################################################################################ | |
193 | ||
194 | sub color_devices() { | |
195 | my $output = shift; | |
196 | ||
197 | if ( uc($output) eq "GREEN0" ) { $output = "<b><font color ='$Header::colourgreen'>$output</b>";} | |
198 | elsif ( uc($output) eq "BLUE0" ) { $output = "<b><font color ='$Header::colourblue'>$output</b>"; } | |
199 | elsif ( uc($output) eq "ORANGE0" ) { $output = "<b><font color ='$Header::colourorange'>$output</b>"; } | |
200 | elsif ( uc($output) eq "RED0" ) { $output = "<b><font color ='$Header::colourred'>$output</b>"; } | |
201 | else { return $output = "<b><font color ='#696565'>$output</b>"; } | |
202 | ||
203 | return $output; | |
204 | } | |
205 | ||
206 | return 1; |