]> git.ipfire.org Git - ipfire-2.x.git/blob - src/wio/main/wiovpn.pl
suricata: Scan outgoing traffic, too
[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-2018 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: 2018/01/05 12:32: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
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 chomp;
98
99 if ( $_ =~ "server" ) { next; }
100
101 ( $name, $remark ) = (split (/\,/, $_))[3, 26];
102
103 $status = 'off';
104
105 unless ( grep (/$name/, @ovpncache) ) { push (@ovpncache, "$name,$remark,$status\n"); }
106 }
107
108 foreach (@ovpncache) {
109 chomp;
110
111 ( $name, $remark, $status ) = split (/\,/, $_);
112
113 if ( grep (/,$name,/, @ovpncfg) ) { push (@ovpnarray, "$name,$remark,$status\n"); }
114 }
115
116 foreach (@ovpnarray) {
117 chomp;
118
119 ( $name, $remark, $status ) = split (/\,/, $_);
120
121 if ( $name =~ m/_/ ) { $nameul = $name; }
122 else { ($nameul = $name) =~ s/ /_/g; }
123
124 if ( grep (/$name/, @ovpnstatus) || grep (/$nameul/, @ovpnstatus) ) {
125 foreach (@ovpnstatus) {
126 chomp;
127
128 if ( $_ =~ "ROUTING TABLE" ) { last; }
129
130 @ovpnmatch = split (/\,/, $_);
131
132 if ( @ovpnmatch != 5 || $_ =~ "Common Name" ) { next; }
133
134 ( $ovpnclt, $realipadr, undef, undef, $connected ) = @ovpnmatch;
135
136 ( $ovpncltip, undef ) = split (/:/, $realipadr);
137
138 $ovpnrwlogin = &WIO::statustime($connected);
139
140 if ( $nameul eq $ovpnclt || $name eq $ovpnclt ) {
141 $ovpnrwstatus = "$Lang::tr{'wio up'}";
142 $togglestat = ( $status ne 'on' ) ? 1 : 0;
143 $status = 'on';
144 }
145
146 if ( ! $name =~ m/_/ ) { $ovpnclt =~ s/_/ /g; }
147
148 if ( $nameul eq $ovpnclt || $name eq $ovpnclt ) { push (@ovpnwrite, "$name,$remark,$status\n"); }
149
150 if ( $togglestat == 1 && ($name eq $ovpnclt || $nameul eq $ovpnclt) ) {
151 $ovpnmailsub = "WIO OVPN - $name - $ovpnrwstatus - $now";
152 $logmsg = "Client: WIO OVPN $name - IP: $ovpncltip - Status: $ovpnrwstatus";
153 $ovpnmailmsg = "Client : $name\nLogin : $ovpnrwlogin\nIP : $ovpncltip\nStatus : $ovpnrwstatus\n";
154
155 if ( $mailremark eq 'on' ) {
156 $ovpnmailmsg .= "Remark : $remark\n\n";
157 }
158
159 &WIO::mailsender($ovpnmailsub, $ovpnmailmsg);
160 if ( $logging eq 'on' ) { &General::log("wio","$logmsg"); }
161 undef ($ovpnmailsub);
162 undef ($ovpnmailmsg);
163 $togglestat = 0;
164 }
165 }
166 }
167 else {
168 if ( $status eq 'on' ) {
169 $ovpnrwstatus = "$Lang::tr{'wio down'}";
170 $status = 'off';
171 $ovpnmailsub = "WIO OVPN - $name - $ovpnrwstatus - $now";
172 $logmsg = "Client: WIO OVPN $name - Status: $ovpnrwstatus";
173 $ovpnmailmsg = "Client : $name\nLogout : $now\nStatus : $ovpnrwstatus\n";
174
175 if ( $mailremark eq 'on' ) { $ovpnmailmsg .= "Remark : $remark\n\n"; }
176
177 &WIO::mailsender($ovpnmailsub, $ovpnmailmsg);
178
179 if ( $logging eq 'on' ) { &General::log("wio","$logmsg"); }
180 undef ($ovpnmailsub);
181 undef ($ovpnmailmsg);
182 }
183
184 push (@ovpnwrite, "$name,$remark,$status\n");
185 }
186 }
187
188 open( FILE, "> $ovpncache" );
189 print FILE @ovpnwrite;
190 close(FILE);
191
192 }
193
194 if ( ! -e "$vpnpid" ) {
195 unlink "$vpncache";
196 }
197 else {
198
199 if ( -e "$vpnpid" ) {
200 @vpnstatus = `/usr/local/bin/ipsecctrl I`;
201 }
202
203 open(FILE, "$vpnconfig");
204 @vpncfg = <FILE>;
205 close (FILE);
206
207 if ( ! -e "$vpncache" ) {
208 open(FILE, ">$vpncache");
209 close (FILE);
210 }
211 else {
212 open(FILE, "$vpncache");
213 @vpncache = <FILE>;
214 close (FILE);
215 }
216
217 foreach (@vpncfg) {
218 chomp;
219
220 ( $activ, $name, $remark ) = (split (/\,/, $_))[1, 2, 26];
221
222 if ( $remark eq 'off' ) { $remark = '-'; }
223
224 $status = 'off';
225
226 if ( $activ eq "off" ) { next; }
227
228 unless ( grep (/$name/, @vpncache) ) { push (@vpncache, "$name,$remark,$status\n"); }
229 }
230
231 foreach (@vpncache) {
232 chomp;
233
234 ( $name, $remark, $status ) = split (/\,/, $_);
235
236 if ( grep (/,$name,/, @vpncfg) ) { push (@vpnarray, "$name,$remark,$status\n"); }
237 }
238
239 foreach (@vpnarray) {
240 chomp;
241
242 ( $name, $remark, $status ) = split (/\,/, $_);
243
244 if ( grep (/$name\{.*INSTALLED/ , @vpnstatus) ) {
245 $vpnrwstatus = "$Lang::tr{'wio up'}";
246 $togglestat = ( $status ne 'on' ) ? 1 : 0;
247 $status = 'on';
248 }
249 else {
250 $vpnrwstatus = "$Lang::tr{'wio down'}";
251 $togglestat = ( $status ne 'off' ) ? 1 : 0;
252 $status = 'off';
253 }
254
255 push (@vpnwrite, "$name,$remark,$status\n");
256
257 if ( $togglestat == 1 ) {
258 $vpnmailsub = "WIO VPN - $name - $vpnrwstatus - $now";
259 $logmsg = "Client: WIO VPN $name - Status: $vpnrwstatus $now";
260 $vpnmailmsg = "Client : $name\n";
261
262 if ( $status eq 'on' ) {
263 $vpnmailmsg .= "Login : $now\n";
264 }
265 else {
266 $vpnmailmsg .= "Logout : $now\n";
267 }
268
269 $vpnmailmsg .= "Status : $vpnrwstatus\n";
270
271 if ( $mailremark eq 'on' ) { $vpnmailmsg .= "Remark : $remark\n\n"; }
272
273 &WIO::mailsender($vpnmailsub, $vpnmailmsg);
274
275 if ( $logging eq 'on' ) { &General::log("wio","$logmsg"); }
276 undef ($vpnmailsub);
277 undef ($vpnmailmsg);
278 $togglestat = 0;
279 }
280 }
281
282 open( FILE, "> $vpncache" );
283 print FILE @vpnwrite;
284 close(FILE);
285
286 }