3f7757adb288973773133fe2a5dd7e302b41a798
[people/pmueller/ipfire-2.x.git] / src / scripts / vpn-watch
1 #!/usr/bin/perl
2 ##################################################
3 ##### VPN-Watch.pl Version 0.4c #####
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
28 while ( $i == 0){
29 if ($debug){logger("We will wait 60 seconds before next action.");}
30 sleep(60);
31
32 if (open(FILE, "<${General::swroot}/vpn/config")) {
33 @vpnsettings = <FILE>;
34 close(FILE);
35 unless(@vpnsettings) {exit 1;}
36 }
37
38 my $status = `ipsec whack --status`;
39 foreach (@vpnsettings){
40 my @settings = split(/,/,$_);
41
42 if ($settings[27] ne 'RED'){next;}
43 if ($settings[4] ne 'net'){next;}
44 if ($settings[1] ne 'on'){next;}chomp($settings[29]);
45 if ($settings[29] ne 'on'){next;}
46
47 my $remotehostname = $settings[11];
48
49 if ($debug){logger("Checking connection to $remotehostname.");}
50
51 my $remoteip = `/usr/bin/ping -c 1 $remotehostname 2>/dev/null | head -n1 | awk '{print \$3}' | tr -d '()' | tr -d ':'`;chomp($remoteip);
52 if ($remoteip eq ""){next;if ($debug){logger("Unable to resolve $remotehostname.");}}
53 my $ipmatch= `echo "$status" | grep $remoteip | grep $settings[2]`;
54
55 if ( $ipmatch eq '' ){
56 logger("Remote IP for host $remotehostname-$remoteip has changed, restarting ipsec.");
57 system("/usr/local/bin/ipsecctrl S");
58 last; #all connections will reloaded
59 }
60 }
61 if ($debug){logger("All connections may be fine nothing was done.");}
62 }
63
64 sub logger {
65 my $log = shift;
66 system("logger -t vpnwatch \"$log\"");
67 }
68