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