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