]>
Commit | Line | Data |
---|---|---|
dced81b2 | 1 | #!/usr/bin/perl |
0edf2f61 CS |
2 | ################################################## |
3 | ##### VPN-Watch.pl Version 0.4 ##### | |
4 | ################################################## | |
5 | # # | |
6 | # VPN-Watch is part of the IPFire Firewall # | |
7 | # # | |
8 | ################################################## | |
c6c9630e | 9 | |
dced81b2 | 10 | use strict; |
c6c9630e | 11 | |
dced81b2 CS |
12 | require '/var/ipfire/general-functions.pl'; |
13 | my @vpnsettings; | |
14 | my $i = 0; | |
15 | my $file = "/var/run/vpn-watch.pid"; | |
ad60e3ea CS |
16 | my $debug = 0; |
17 | ||
a2d5130f CS |
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 | } | |
c6c9630e | 25 | |
dced81b2 | 26 | system("echo $$ > $file"); |
c6c9630e | 27 | |
dced81b2 | 28 | while ( $i == 0){ |
ad60e3ea | 29 | if ($debug){logger("We will wait 300 seconds before next action.");} |
f515baeb | 30 | sleep(300); |
ad60e3ea | 31 | |
a2d5130f | 32 | if (open(FILE, "<${General::swroot}/vpn/config")) { |
dced81b2 CS |
33 | @vpnsettings = <FILE>; |
34 | close(FILE); | |
35 | unless(@vpnsettings) {exit 1;} | |
36 | } | |
37 | ||
38 | foreach (@vpnsettings){ | |
39 | my @settings = split(/,/,$_); | |
40 | ||
41 | if ($settings[27] ne 'RED'){next;} | |
42 | if ($settings[4] ne 'net'){next;} | |
43 | if ($settings[1] ne 'on'){next;}chomp($settings[29]); | |
44 | if ($settings[29] ne 'on'){next;} | |
45 | ||
46 | my $remotehostname = $settings[11]; | |
ad60e3ea CS |
47 | |
48 | if ($debug){logger("Checking connection to $remotehostname.");} | |
49 | ||
dced81b2 | 50 | my $remoteip = `/usr/bin/ping -c 1 $remotehostname 2>/dev/null | head -n1 | awk '{print \$3}' | tr -d '()' | tr -d ':'`;chomp($remoteip); |
0edf2f61 | 51 | if ($remoteip eq ""){next;if ($debug){logger("Unable to resolve $remotehostname.");}} |
f515baeb | 52 | my $connected= `ipsec whack --status | grep $remoteip`; |
0fe5c268 | 53 | my $established= `ipsec whack --status | grep '$settings[2]' | grep 'IPsec SA established'`; |
0edf2f61 | 54 | |
f515baeb | 55 | if ( $established eq '' || $connected eq '' ){ |
0edf2f61 | 56 | logger("Remote IP for host $remotehostname has changed or no connection is established, restarting connection to $remoteip."); |
dced81b2 CS |
57 | system("/usr/local/bin/ipsecctrl S $settings[0]"); |
58 | next; | |
59 | } | |
ad60e3ea CS |
60 | if ($debug){logger("All connections may be fine nothing was done.");} |
61 | } | |
dced81b2 CS |
62 | } |
63 | ||
64 | sub logger { | |
65 | my $log = shift; | |
66 | system("logger -t vpnwatch \"$log\""); | |
67 | } | |
0edf2f61 | 68 |