]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/scripts/vpn-watch
ipsec: fix vpn-watch ikev2 skipping.
[people/pmueller/ipfire-2.x.git] / src / scripts / vpn-watch
CommitLineData
dced81b2 1#!/usr/bin/perl
0edf2f61 2##################################################
843f29e9 3##### VPN-Watch.pl Version 0.6a #####
0edf2f61
CS
4##################################################
5# #
6# VPN-Watch is part of the IPFire Firewall #
7# #
8##################################################
c6c9630e 9
dced81b2 10use strict;
c6c9630e 11
dced81b2
CS
12require '/var/ipfire/general-functions.pl';
13my @vpnsettings;
14my $i = 0;
15my $file = "/var/run/vpn-watch.pid";
ad60e3ea
CS
16my $debug = 0;
17
a2d5130f
CS
18if ( -e $file ){
19 logger("There my be another vpn-watch runnning because $file exists, vpn-watch will try kill the process.");
20 open(FILE, "<$file");
21 my $PID = <FILE>;
22 close(FILE);
23 system("kill -9 $PID");
24 }
c6c9630e 25
dced81b2 26system("echo $$ > $file");
44b5666b 27my $round=0;
dced81b2 28while ( $i == 0){
64bd20c5
AF
29 if ($debug){logger("We will wait 60 seconds before next action.");}
30 sleep(60);
44b5666b
AF
31
32 $round++;
33
843f29e9
AF
34 # Reset roundcounter after 60 min. To do established check.
35 if ($round > 59) { $round=0; }
44b5666b
AF
36
37 if (open(FILE, "<${General::swroot}/vpn/config")) { @vpnsettings = <FILE>;
dced81b2
CS
38 close(FILE);
39 unless(@vpnsettings) {exit 1;}
40 }
41
64bd20c5 42my $status = `ipsec whack --status`;
dced81b2
CS
43foreach (@vpnsettings){
44 my @settings = split(/,/,$_);
641baa86
AF
45
46 chomp($settings[30]);
e2e4ed01 47 if ($settings[30] eq 'ikev2'){next;}
dced81b2
CS
48 if ($settings[27] ne 'RED'){next;}
49 if ($settings[4] ne 'net'){next;}
50 if ($settings[1] ne 'on'){next;}chomp($settings[29]);
51 if ($settings[29] ne 'on'){next;}
52
53 my $remotehostname = $settings[11];
ad60e3ea
CS
54
55 if ($debug){logger("Checking connection to $remotehostname.");}
56
dced81b2 57 my $remoteip = `/usr/bin/ping -c 1 $remotehostname 2>/dev/null | head -n1 | awk '{print \$3}' | tr -d '()' | tr -d ':'`;chomp($remoteip);
0edf2f61 58 if ($remoteip eq ""){next;if ($debug){logger("Unable to resolve $remotehostname.");}}
44b5666b 59 my $ipmatch= `echo "$status" | grep '$remoteip' | grep '$settings[2]'`;
bb893dcd 60 my $established= `echo "$status" | grep '$settings[2]' | grep 'erouted;'`;
843f29e9
AF
61 my $known= `echo "$status" | grep '$settings[2]'`;
62
63 if ( $ipmatch eq '' && $known ne '' ){
44b5666b
AF
64 logger("Remote IP for host $remotehostname($remoteip) has changed, restarting ipsec.");
65 system("/usr/local/bin/ipsecctrl S $settings[0]");
bb893dcd 66 $round=0;
98ccaf67 67 last; #all connections will reloaded
44b5666b
AF
68 #remove this if ipsecctrl can restart single con again
69 }
bb893dcd
CS
70
71 if ($debug){logger("Round=".$round." and established=".$established);}
72
73 if ( ($round == 0) && ($established eq '')) {
44b5666b
AF
74 logger("Connection to $remotehostname($remoteip) not erouted, restarting ipsec.");
75 system("/usr/local/bin/ipsecctrl S $settings[0]");
bb893dcd 76 $round=0;
44b5666b
AF
77 last; #all connections will reloaded
78 #remove this if ipsecctrl can restart single con again
79
dced81b2 80 }
ad60e3ea 81 }
af0e9924 82 if ($debug){logger("All connections may be fine nothing was done.");}
dced81b2
CS
83}
84
85sub logger {
86 my $log = shift;
87 system("logger -t vpnwatch \"$log\"");
88}