openvpn ccd: Multiple installation fixes.
[people/teissler/ipfire-2.x.git] / src / scripts / ovpn-ccd-convert
1 #!/usr/bin/perl
2 # Converter script for old openvpn clients
3 my %net=();
4 my %ovpnconfig=();
5 my @serverconf=();
6 my $greennet;
7 my $greensubnet;
8 my $running='off';
9
10 require '/var/ipfire/general-functions.pl';
11 unless (-d "${General::swroot}/ovpn/ccd") { system("mkdir ${General::swroot}/ovpn/ccd"); }
12 system ("chown nobody.nobody ${General::swroot}/ovpn/ccd");
13 if ( -e "/var/run/openvpn.pid"){
14 $running='on';
15 system('/usr/local/bin/openvpnctrl', '-k');
16 }
17
18 &General::readhash("/var/ipfire/ethernet/settings", \%net);
19 $greennet=$net{'GREEN_NETADDRESS'};
20 $greensubnet=$net{'GREEN_NETMASK'};
21 open(FILE,"/var/ipfire/ovpn/server.conf");
22 while (<FILE>) {
23 $_=~s/\s*$//g;
24 if ($_ ne "route $greennet $greensubnet"){
25 push (@serverconf,$_."\n");
26 }else{
27 print"\nFound ROUTE >>route $greennet $greensubnet<< in server.conf.. Deleted!";
28 }
29 }
30
31 &General::readhasharray("/var/ipfire/ovpn/ovpnconfig", \%ovpnconfig);
32 foreach my $key (keys %ovpnconfig){
33 if($ovpnconfig{$key}[32] eq '' && $ovpnconfig{$key}[3] eq 'host'){
34 open ( CCDRWCONF,'>',"/var/ipfire/ovpn/ccd/$ovpnconfig{$key}[2]") or die "Unable to create clientconfigfile $!";
35 print CCDRWCONF "# OpenVPN Clientconfig from CCD extension by Copymaster#\n\n";
36 print CCDRWCONF "#This client uses the dynamic pool\n\n";
37 print CCDRWCONF "\n#Client gets routes to these Networks (behind IPFIRE)\n";
38 print CCDRWCONF "push \"route $greennet $greensubnet\"\n";
39 close CCDRWCONF;
40 print"Client $ovpnconfig{$key}[2] converted! \n";
41 }else{
42 print "Client $ovpnconfig{$key}[2] NOT converted!\n";
43 }
44 $ovpnconfig{$key}[32] = 'dynamic';
45 }
46 &General::writehasharray("/var/ipfire/ovpn/ovpnconfig", \%ovpnconfig);
47 if ($running eq 'on')
48 {
49 system('/usr/local/bin/openvpnctrl', '-s');
50 }
51 system ("chown nobody:nobody /var/ipfire/ovpn/ccd/*");