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