]> git.ipfire.org Git - ipfire-2.x.git/blob - config/wio/wiovpn.pl
Core Update 169: Drop entropy.cgi
[ipfire-2.x.git] / config / wio / wiovpn.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/05/04 12:02:23
25 #
26 # This wioovpn.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 (for some code for the IPCop WIO Addon)
32 #
33
34 # enable only the following on debugging purpose
35 #use warnings;
36
37 use strict;
38 use POSIX qw(strftime);
39
40 require '/var/ipfire/general-functions.pl';
41 require '/var/ipfire/lang.pl';
42 require '/usr/lib/wio/wio-lib.pl';
43
44 my %wiosettings = ();
45
46 &General::readhash( "/var/ipfire/wio/wio.conf", \%wiosettings );
47
48 my $mailremark = $wiosettings{'MAILREMARK'};
49 my $logging = $wiosettings{'LOGGING'};
50
51 my ( @ovpnstatus, @ovpncfg, @ovpncache, @ovpnarray, @ovpnmatch, @ovpnwrite );
52
53 my $now = strftime "%a, %d.%m.%Y %H:%M:%S", localtime;
54 my $ovpnpid = "/var/run/openvpn.pid";
55 my $ovpnmailmsg = '';
56 my $ovpncache = "/var/log/wio/.ovpncache";
57 my $ovpnconfig = "/var/ipfire/ovpn/ovpnconfig";
58
59 my ( $name, $nameul, $ovpnclt, $ovpncltip, $realipadr, $connected ) = '';
60 my ( $ovpnmailsub, $ovpnrwlogin, $ovpnrwstatus, $status, $remark, $logmsg ) = '';
61
62 my ( @vpnstatus, @vpncfg, @vpncache, @vpnarray, @vpnwrite );
63
64 my $vpnpid = "/var/run/charon.pid";
65 my $vpnmailmsg = '';
66 my $vpncache = "/var/log/wio/.vpncache";
67 my $vpnconfig = "/var/ipfire/vpn/config";
68
69 my ( $vpnmailsub, $vpnrwstatus ) = '';
70
71 my $togglestat = 0;
72
73 if ( ! -e "$ovpnpid" ) {
74 unlink "$ovpncache";
75 }
76 else {
77
78 @ovpnstatus = `cat /var/run/ovpnserver.log`;
79
80 open(FILE, "$ovpnconfig");
81 @ovpncfg = <FILE>;
82 close (FILE);
83
84 unless ( -e "$ovpncache" ) {
85 open(FILE, ">$ovpncache");
86 close (FILE);
87 }
88 else {
89 open(FILE, "$ovpncache");
90 @ovpncache = <FILE>;
91 close (FILE);
92 }
93
94 foreach (@ovpncfg) {
95 chomp;
96
97 if ( $_ =~ "server" ) { next; }
98
99 ( $name, $remark ) = (split (/\,/, $_))[3, 26];
100
101 unless ( grep (/$name/, @ovpncache) ) { push (@ovpncache, "$name,$remark,off\n"); }
102 }
103
104 foreach (@ovpncache) {
105 chomp;
106
107 ( $name, $remark, $status ) = split (/\,/, $_);
108
109 if ( grep (/$name/, @ovpncfg) ) { push (@ovpnarray, "$name,$remark,$status\n"); }
110 }
111
112 foreach (@ovpnarray) {
113 chomp;
114
115 ( $name, $remark, $status ) = split (/\,/, $_);
116
117 $remark = `/bin/cat $ovpnconfig | grep '$name' | cut -d "," -f 27`;
118 chomp ($remark);
119
120 if ( $name =~ m/_/ ) { $nameul = $name; }
121 else { ($nameul = $name) =~ s/ /_/g; }
122
123 if ( grep (/$name/, @ovpnstatus) || grep (/$nameul/, @ovpnstatus) ) {
124 foreach (@ovpnstatus) {
125 chomp;
126
127 if ( $_ =~ "ROUTING TABLE" ) { last; }
128
129 @ovpnmatch = split (/\,/, $_);
130
131 if ( @ovpnmatch != 5 || $_ =~ "Common Name" ) { next; }
132
133 ( $ovpnclt, $realipadr, undef, undef, $connected ) = @ovpnmatch;
134
135 ( $ovpncltip, undef ) = split (/:/, $realipadr);
136
137 $ovpnrwlogin = &WIO::statustime($connected);
138
139 if ( $nameul eq $ovpnclt || $name eq $ovpnclt ) {
140 $ovpnrwstatus = "$Lang::tr{'wio up'}";
141 $togglestat = ( $status ne 'on' ) ? 1 : 0;
142 $status = 'on';
143 }
144
145 if ( ! $name =~ m/_/ ) { $ovpnclt =~ s/_/ /g; }
146
147 if ( $nameul eq $ovpnclt || $name eq $ovpnclt ) { push (@ovpnwrite, "$name,$remark,$status\n"); }
148
149 if ( $togglestat == 1 && ($name eq $ovpnclt || $nameul eq $ovpnclt) ) {
150 $ovpnmailsub = "WIO OVPN - $name - $ovpnrwstatus - $now";
151 $logmsg = "Client: WIO OVPN $name - IP: $ovpncltip - Status: $ovpnrwstatus";
152 $ovpnmailmsg = "Client : $name\nLogin : $ovpnrwlogin\nIP : $ovpncltip\nStatus : $ovpnrwstatus\n";
153
154 if ( $mailremark eq 'on' ) {
155 $ovpnmailmsg .= "Remark : $remark\n\n";
156 }
157
158 &WIO::mailsender($ovpnmailsub, $ovpnmailmsg);
159 if ( $logging eq 'on' ) { &General::log("wio","$logmsg"); }
160 undef ($ovpnmailsub);
161 undef ($ovpnmailmsg);
162 $togglestat = 0;
163 }
164 }
165 }
166 else {
167 if ( $status eq 'on' ) {
168 $ovpnrwstatus = "$Lang::tr{'wio down'}";
169 $status = 'off';
170 $ovpnmailsub = "WIO OVPN - $name - $ovpnrwstatus - $now";
171 $logmsg = "Client: WIO OVPN $name - Status: $ovpnrwstatus";
172 $ovpnmailmsg = "Client : $name\nLogout : $now\nStatus : $ovpnrwstatus\n";
173
174 if ( $mailremark eq 'on' ) { $ovpnmailmsg .= "Remark : $remark\n\n"; }
175
176 &WIO::mailsender($ovpnmailsub, $ovpnmailmsg);
177
178 if ( $logging eq 'on' ) { &General::log("wio","$logmsg"); }
179 undef ($ovpnmailsub);
180 undef ($ovpnmailmsg);
181 }
182
183 push (@ovpnwrite, "$name,$remark,$status\n");
184 }
185 }
186
187 open( FILE, "> $ovpncache" );
188 print FILE @ovpnwrite;
189 close(FILE);
190
191 }
192
193 if ( ! -e "$vpnpid" ) {
194 unlink "$vpncache";
195 }
196 else {
197
198 @vpnstatus = `/usr/local/bin/ipsecctrl I`;
199
200 open(FILE, "$vpnconfig");
201 @vpncfg = <FILE>;
202 close (FILE);
203
204 unless ( -e "$vpncache" ) {
205 open(FILE, ">$vpncache");
206 close (FILE);
207 }
208 else {
209 open(FILE, "$vpncache");
210 @vpncache = <FILE>;
211 close (FILE);
212 }
213
214 foreach (@vpncfg) {
215 chomp;
216
217 ( $name, $remark ) = (split (/\,/, $_))[2, 26];
218
219 unless ( grep (/$name/, @vpncache) ) { push (@vpncache, "$name,$remark,off\n"); }
220 }
221
222 foreach (@vpncache) {
223 chomp;
224
225 ( $name, $remark, $status ) = split (/\,/, $_);
226
227 if ( grep (/$name/, @vpncfg) ) { push (@vpnarray, "$name,$remark,$status\n"); }
228 }
229
230 foreach (@vpnarray) {
231 chomp;
232
233 ( $name, $remark, $status ) = split (/\,/, $_);
234
235 $remark = `/bin/cat $vpnconfig | grep '$name' | cut -d "," -f 27`;
236 chomp ($remark);
237
238 if ( grep (/$name\{.*INSTALLED/ , @vpnstatus) ) {
239 $vpnrwstatus = "$Lang::tr{'wio up'}";
240 $togglestat = ( $status ne 'on' ) ? 1 : 0;
241 $status = 'on';
242 }
243 else {
244 $vpnrwstatus = "$Lang::tr{'wio down'}";
245 $togglestat = ( $status ne 'off' ) ? 1 : 0;
246 $status = 'off';
247 }
248
249 push (@vpnwrite, "$name,$remark,$status\n");
250
251 if ( $togglestat == 1 ) {
252 $vpnmailsub = "WIO IPsec - $name - $vpnrwstatus - $now";
253 $logmsg = "Client: WIO IPSec $name - Status: $vpnrwstatus $now";
254 $vpnmailmsg = "Client : $name\n";
255
256 if ( $status eq 'on' ) {
257 $vpnmailmsg .= "Login : $now\n";
258 }
259 else {
260 $vpnmailmsg .= "Logout : $now\n";
261 }
262
263 $vpnmailmsg .= "Status : $vpnrwstatus\n";
264
265 if ( $mailremark eq 'on' ) { $vpnmailmsg .= "Remark : $remark\n\n"; }
266
267 &WIO::mailsender($vpnmailsub, $vpnmailmsg);
268
269 if ( $logging eq 'on' ) { &General::log("wio","$logmsg"); }
270 undef ($vpnmailsub);
271 undef ($vpnmailmsg);
272 $togglestat = 0;
273 }
274 }
275
276 open( FILE, "> $vpncache" );
277 print FILE @vpnwrite;
278 close(FILE);
279
280 }