OpenVPN: Added additional configuration for server and clients
[ipfire-2.x.git] / html / cgi-bin / ovpnmain.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 #                                                                             #
4 # IPFire.org - A linux based firewall                                         #
5 # Copyright (C) 2007-2014  IPFire Team  <info@ipfire.org>                     #
6 #                                                                             #
7 # This program is free software: you can redistribute it and/or modify        #
8 # it under the terms of the GNU General Public License as published by        #
9 # the Free Software Foundation, either version 3 of the License, or           #
10 # (at your option) any later version.                                         #
11 #                                                                             #
12 # This program is distributed in the hope that it will be useful,             #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of              #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
15 # GNU General Public License for more details.                                #
16 #                                                                             #
17 # You should have received a copy of the GNU General Public License           #
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
19 #                                                                             #
20 ###############################################################################
21 ###
22 # Based on IPFireCore 77
23 ###
24 use CGI;
25 use CGI qw/:standard/;
26 use Net::DNS;
27 use Net::Ping;
28 use Net::Telnet;
29 use File::Copy;
30 use File::Temp qw/ tempfile tempdir /;
31 use strict;
32 use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
33 use Sort::Naturally;
34 require '/var/ipfire/general-functions.pl';
35 require "${General::swroot}/lang.pl";
36 require "${General::swroot}/header.pl";
37 require "${General::swroot}/countries.pl";
38
39 # enable only the following on debugging purpose
40 #use warnings;
41 #use CGI::Carp 'fatalsToBrowser';
42 #workaround to suppress a warning when a variable is used only once
43 my @dummy = ( ${Header::colourgreen}, ${Header::colourblue} );
44 undef (@dummy);
45
46 my %color = ();
47 my %mainsettings = ();
48 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
49 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
50
51 ###
52 ### Initialize variables
53 ###
54 my %ccdconfhash=();
55 my %ccdroutehash=();
56 my %ccdroute2hash=();
57 my %netsettings=();
58 my %cgiparams=();
59 my %vpnsettings=();
60 my %checked=();
61 my %confighash=();
62 my %cahash=();
63 my %selected=();
64 my $warnmessage = '';
65 my $errormessage = '';
66 my %settings=();
67 my $routes_push_file = '';
68 my $confighost="${General::swroot}/fwhosts/customhosts";
69 my $configgrp="${General::swroot}/fwhosts/customgroups";
70 my $customnet="${General::swroot}/fwhosts/customnetworks";
71 my $name;
72 my $col="";
73 my $local_serverconf = "${General::swroot}/ovpn/scripts/server.conf.local";
74 my $local_clientconf = "${General::swroot}/ovpn/scripts/client.conf.local";
75
76 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
77 $cgiparams{'ENABLED'} = 'off';
78 $cgiparams{'ENABLED_BLUE'} = 'off';
79 $cgiparams{'ENABLED_ORANGE'} = 'off';
80 $cgiparams{'EDIT_ADVANCED'} = 'off';
81 $cgiparams{'NAT'} = 'off';
82 $cgiparams{'COMPRESSION'} = 'off';
83 $cgiparams{'ONLY_PROPOSED'} = 'off';
84 $cgiparams{'ACTION'} = '';
85 $cgiparams{'CA_NAME'} = '';
86 $cgiparams{'DH_NAME'} = 'dh1024.pem';
87 $cgiparams{'DHLENGHT'} = '';
88 $cgiparams{'DHCP_DOMAIN'} = '';
89 $cgiparams{'DHCP_DNS'} = '';
90 $cgiparams{'DHCP_WINS'} = '';
91 $cgiparams{'ROUTES_PUSH'} = '';
92 $cgiparams{'DCOMPLZO'} = 'off';
93 $cgiparams{'MSSFIX'} = '';
94 $cgiparams{'number'} = '';
95 $cgiparams{'PMTU_DISCOVERY'} = '';
96 $cgiparams{'DCIPHER'} = '';
97 $cgiparams{'DAUTH'} = '';
98 $cgiparams{'TLSAUTH'} = '';
99 $routes_push_file = "${General::swroot}/ovpn/routes_push";
100
101 # Add CCD files if not already presant
102 unless (-e $routes_push_file) {
103         open(RPF, ">$routes_push_file");
104         close(RPF);
105 }
106 unless (-e "${General::swroot}/ovpn/ccd.conf") {
107         open(CCDC, ">${General::swroot}/ovpn/ccd.conf");
108         close (CCDC);
109 }
110 unless (-e "${General::swroot}/ovpn/ccdroute") {
111         open(CCDR, ">${General::swroot}/ovpn/ccdroute");
112         close (CCDR);
113 }
114 unless (-e "${General::swroot}/ovpn/ccdroute2") {
115         open(CCDRT, ">${General::swroot}/ovpn/ccdroute2");
116         close (CCDRT);
117 }
118 # Add additional configs if not already presant
119 unless (-e "$local_serverconf") {
120         open(LSC, ">$local_serverconf");
121        close (LSC);
122 }
123 unless (-e "$local_clientconf") {
124        open(LCC, ">$local_clientconf");
125        close (LCC);
126 }
127
128 &Header::getcgihash(\%cgiparams, {'wantfile' => 1, 'filevar' => 'FH'});
129
130 # prepare openvpn config file
131 ###
132 ### Useful functions
133 ###
134 sub haveOrangeNet
135 {
136         if ($netsettings{'CONFIG_TYPE'} == 2) {return 1;}
137         if ($netsettings{'CONFIG_TYPE'} == 4) {return 1;}
138         return 0;
139 }
140
141 sub haveBlueNet
142 {
143         if ($netsettings{'CONFIG_TYPE'} == 3) {return 1;}
144         if ($netsettings{'CONFIG_TYPE'} == 4) {return 1;}
145         return 0;
146 }
147
148 sub sizeformat{
149     my $bytesize = shift;
150     my $i = 0;
151
152     while(abs($bytesize) >= 1024){
153         $bytesize=$bytesize/1024;
154         $i++;
155         last if($i==6);
156     }
157
158     my @units = ("Bytes","KB","MB","GB","TB","PB","EB");
159     my $newsize=(int($bytesize*100 +0.5))/100;
160     return("$newsize $units[$i]");
161 }
162
163 sub cleanssldatabase
164 {
165     if (open(FILE, ">${General::swroot}/ovpn/certs/serial")) {
166         print FILE "01";
167         close FILE;
168     }
169     if (open(FILE, ">${General::swroot}/ovpn/certs/index.txt")) {
170         print FILE "";
171         close FILE;
172     }
173     unlink ("${General::swroot}/ovpn/certs/index.txt.old");
174     unlink ("${General::swroot}/ovpn/certs/serial.old");
175     unlink ("${General::swroot}/ovpn/certs/01.pem");
176 }
177
178 sub newcleanssldatabase
179 {
180     if (! -s "${General::swroot}/ovpn/certs/serial" )  {
181         open(FILE, ">${General::swroot}(ovpn/certs/serial");
182         print FILE "01";
183         close FILE;
184     }
185     if (! -s ">${General::swroot}/ovpn/certs/index.txt") {
186         system ("touch ${General::swroot}/ovpn/certs/index.txt");
187     }
188     unlink ("${General::swroot}/ovpn/certs/index.txt.old");
189     unlink ("${General::swroot}/ovpn/certs/serial.old");
190 }
191
192 sub deletebackupcert
193 {
194         if (open(FILE, "${General::swroot}/ovpn/certs/serial.old")) {
195                 my $hexvalue = <FILE>;
196                 chomp $hexvalue;
197                 close FILE;
198                 unlink ("${General::swroot}/ovpn/certs/$hexvalue.pem");
199         }
200 }
201
202 sub writeserverconf {
203     my %sovpnsettings = ();  
204     my @temp = ();  
205     &General::readhash("${General::swroot}/ovpn/settings", \%sovpnsettings);
206     &read_routepushfile;
207     
208     open(CONF,    ">${General::swroot}/ovpn/server.conf") or die "Unable to open ${General::swroot}/ovpn/server.conf: $!";
209     flock CONF, 2;
210     print CONF "#OpenVPN Server conf\n";
211     print CONF "\n";
212     print CONF "daemon openvpnserver\n";
213     print CONF "writepid /var/run/openvpn.pid\n";
214     print CONF "#DAN prepare OpenVPN for listening on blue and orange\n";
215     print CONF ";local $sovpnsettings{'VPN_IP'}\n";
216     print CONF "dev $sovpnsettings{'DDEVICE'}\n";
217     print CONF "proto $sovpnsettings{'DPROTOCOL'}\n";
218     print CONF "port $sovpnsettings{'DDEST_PORT'}\n";
219     print CONF "script-security 3 system\n";
220     print CONF "ifconfig-pool-persist /var/ipfire/ovpn/ovpn-leases.db 3600\n";
221     print CONF "client-config-dir /var/ipfire/ovpn/ccd\n";
222     print CONF "tls-server\n";
223     print CONF "ca ${General::swroot}/ovpn/ca/cacert.pem\n";
224     print CONF "cert ${General::swroot}/ovpn/certs/servercert.pem\n";
225     print CONF "key ${General::swroot}/ovpn/certs/serverkey.pem\n";
226     print CONF "dh ${General::swroot}/ovpn/ca/$cgiparams{'DH_NAME'}\n";
227     my @tempovpnsubnet = split("\/",$sovpnsettings{'DOVPN_SUBNET'});
228     print CONF "server $tempovpnsubnet[0] $tempovpnsubnet[1]\n";
229     #print CONF "push \"route $netsettings{'GREEN_NETADDRESS'} $netsettings{'GREEN_NETMASK'}\"\n";
230
231     # Check if we are using mssfix, fragment or mtu-disc and set the corretct mtu of 1500.
232     # If we doesn't use one of them, we can use the configured mtu value.
233     if ($sovpnsettings{'MSSFIX'} eq 'on') 
234         { print CONF "$sovpnsettings{'DDEVICE'}-mtu 1500\n"; }
235     elsif ($sovpnsettings{'FRAGMENT'} ne '' && $sovpnsettings{'DPROTOCOL'} ne 'tcp') 
236         { print CONF "$sovpnsettings{'DDEVICE'}-mtu 1500\n"; }
237     elsif (($sovpnsettings{'PMTU_DISCOVERY'} eq 'yes') ||
238         ($sovpnsettings{'PMTU_DISCOVERY'} eq 'maybe') ||
239         ($sovpnsettings{'PMTU_DISCOVERY'} eq 'no' ))
240         { print CONF "$sovpnsettings{'DDEVICE'}-mtu 1500\n"; } 
241     else 
242         { print CONF "$sovpnsettings{'DDEVICE'}-mtu $sovpnsettings{'DMTU'}\n"; }
243
244     if ($vpnsettings{'ROUTES_PUSH'} ne '') {
245                 @temp = split(/\n/,$vpnsettings{'ROUTES_PUSH'});
246                 foreach (@temp)
247                 {
248                         @tempovpnsubnet = split("\/",&General::ipcidr2msk($_));
249                         print CONF "push \"route " . $tempovpnsubnet[0]. " " .  $tempovpnsubnet[1] . "\"\n";
250                 }
251         }
252 # a.marx ccd
253         my %ccdconfhash=();
254         &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
255         foreach my $key (keys %ccdconfhash) {
256                 my $a=$ccdconfhash{$key}[1];
257                 my ($b,$c) = split (/\//, $a);
258                 print CONF "route $b ".&General::cidrtosub($c)."\n";
259         }
260         my %ccdroutehash=();
261         &General::readhasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
262         foreach my $key (keys %ccdroutehash) {
263                 foreach my $i ( 1 .. $#{$ccdroutehash{$key}}){
264                         my ($a,$b)=split (/\//,$ccdroutehash{$key}[$i]);
265                         print CONF "route $a $b\n";
266                 }
267         }
268 # ccd end
269
270         if ($sovpnsettings{CLIENT2CLIENT} eq 'on') {
271         print CONF "client-to-client\n";
272     }
273     if ($sovpnsettings{MSSFIX} eq 'on') {
274                 print CONF "mssfix\n";
275     }
276     if ($sovpnsettings{FRAGMENT} ne '' && $sovpnsettings{'DPROTOCOL'} ne 'tcp') {
277                 print CONF "fragment $sovpnsettings{'FRAGMENT'}\n";
278     }
279
280     # Check if a valid operating mode has been choosen and use it.
281     if (($sovpnsettings{'PMTU_DISCOVERY'} eq 'yes') ||
282         ($sovpnsettings{'PMTU_DISCOVERY'} eq 'maybe') ||
283         ($sovpnsettings{'PMTU_DISCOVERY'} eq 'no' )) {
284                 print CONF "mtu-disc $sovpnsettings{'PMTU_DISCOVERY'}\n";
285     }
286
287     if ($sovpnsettings{KEEPALIVE_1} > 0 && $sovpnsettings{KEEPALIVE_2} > 0) {   
288         print CONF "keepalive $sovpnsettings{'KEEPALIVE_1'} $sovpnsettings{'KEEPALIVE_2'}\n";
289     }   
290     print CONF "status-version 1\n";
291     print CONF "status /var/log/ovpnserver.log 30\n";
292     print CONF "cipher $sovpnsettings{DCIPHER}\n";
293     if ($sovpnsettings{'DAUTH'} eq '') {
294         print CONF "";
295     } else {
296         print CONF "auth $sovpnsettings{'DAUTH'}\n";
297     }
298     if ($sovpnsettings{'TLSAUTH'} eq 'on') {
299         print CONF "tls-auth ${General::swroot}/ovpn/certs/ta.key\n";
300     }
301     if ($sovpnsettings{DCOMPLZO} eq 'on') {
302         print CONF "comp-lzo\n";
303     }
304     if ($sovpnsettings{REDIRECT_GW_DEF1} eq 'on') {
305         print CONF "push \"redirect-gateway def1\"\n";
306     }
307     if ($sovpnsettings{DHCP_DOMAIN} ne '') {
308         print CONF "push \"dhcp-option DOMAIN $sovpnsettings{DHCP_DOMAIN}\"\n";
309     }
310
311     if ($sovpnsettings{DHCP_DNS} ne '') {
312         print CONF "push \"dhcp-option DNS $sovpnsettings{DHCP_DNS}\"\n";
313     }
314
315     if ($sovpnsettings{DHCP_WINS} ne '') {
316         print CONF "push \"dhcp-option WINS $sovpnsettings{DHCP_WINS}\"\n";
317     }
318     
319     if ($sovpnsettings{DHCP_WINS} eq '') {
320         print CONF "max-clients 100\n";
321     }
322     if ($sovpnsettings{DHCP_WINS} ne '') {
323         print CONF "max-clients $sovpnsettings{MAX_CLIENTS}\n";
324     }   
325     print CONF "tls-verify /usr/lib/openvpn/verify\n";
326     print CONF "crl-verify /var/ipfire/ovpn/crls/cacrl.pem\n";
327     print CONF "user nobody\n";
328     print CONF "group nobody\n";
329     print CONF "persist-key\n";
330     print CONF "persist-tun\n";
331         if ($sovpnsettings{LOG_VERB} ne '') {
332                 print CONF "verb $sovpnsettings{LOG_VERB}\n";
333         } else {
334                 print CONF "verb 3\n";
335         }
336     # Print server.conf.local if entries exist to server.conf
337     if ( !-z $local_serverconf  && $sovpnsettings{'ADDITIONAL_CONFIGS'} eq 'on') {
338        open (LSC, "$local_serverconf");
339                print CONF "\n#---------------------------\n";
340                print CONF "# Start of custom directives\n";
341                print CONF "# from server.conf.local\n";
342                print CONF "#---------------------------\n\n";
343        while (<LSC>) {
344                print CONF $_;
345        }
346                print CONF "\n#-----------------------------\n";
347                print CONF "# End of custom directives\n";
348                print CONF "#-----------------------------\n";
349        close (LSC);
350     }
351     print CONF "\n";
352     
353     close(CONF);
354 }    
355
356 sub emptyserverlog{
357     if (open(FILE, ">/var/log/ovpnserver.log")) {
358         flock FILE, 2;
359         print FILE "";
360         close FILE;
361     }
362
363 }
364
365 sub delccdnet 
366 {
367         my %ccdconfhash = ();
368         my %ccdhash = ();
369         my $ccdnetname=$_[0];
370         if (-f "${General::swroot}/ovpn/ovpnconfig"){
371                 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%ccdhash);
372                 foreach my $key (keys %ccdhash) {
373                         if ($ccdhash{$key}[32] eq $ccdnetname) {
374                                 $errormessage=$Lang::tr{'ccd err hostinnet'};
375                                 return;
376                         }
377                 }
378         }
379         &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
380         foreach my $key (keys %ccdconfhash) {
381                         if ($ccdconfhash{$key}[0] eq $ccdnetname){
382                                 delete $ccdconfhash{$key};
383                         }
384         }
385         &General::writehasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
386         
387         &writeserverconf;
388         return 0;
389 }
390
391 sub addccdnet
392 {
393         my %ccdconfhash=();
394         my @ccdconf=();
395         my $ccdname=$_[0];
396         my $ccdnet=$_[1];
397         my $subcidr;
398         my @ip2=();
399         my $checkup;
400         my $ccdip;
401         my $baseaddress;
402         
403         
404         #check name     
405         if ($ccdname eq '') 
406         {
407                 $errormessage=$errormessage.$Lang::tr{'ccd err name'}."<br>";
408                 return
409         }
410         
411         if(!&General::validhostname($ccdname))
412         {
413                 $errormessage=$Lang::tr{'ccd err invalidname'};
414                 return;
415         }
416                 
417         ($ccdip,$subcidr) = split (/\//,$ccdnet);
418         $subcidr=&General::iporsubtocidr($subcidr);
419         #check subnet
420         if ($subcidr > 30)
421         {
422                 $errormessage=$Lang::tr{'ccd err invalidnet'};
423                 return;
424         }
425         #check ip
426         if (!&General::validipandmask($ccdnet)){
427                 $errormessage=$Lang::tr{'ccd err invalidnet'};
428                 return;
429         }
430         
431         $errormessage=&General::checksubnets($ccdname,$ccdnet);
432         
433                 
434         if (!$errormessage) {
435                 my %ccdconfhash=();
436                 $baseaddress=&General::getnetworkip($ccdip,$subcidr);
437                 &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
438                 my $key = &General::findhasharraykey (\%ccdconfhash);
439                 foreach my $i (0 .. 1) { $ccdconfhash{$key}[$i] = "";}
440                 $ccdconfhash{$key}[0] = $ccdname;
441                 $ccdconfhash{$key}[1] = $baseaddress."/".$subcidr;
442                 &General::writehasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
443                 &writeserverconf;
444                 $cgiparams{'ccdname'}='';
445                 $cgiparams{'ccdsubnet'}='';
446                 return 1;
447         }
448 }
449
450 sub modccdnet
451 {
452         
453         my $newname=$_[0];
454         my $oldname=$_[1];
455         my %ccdconfhash=();
456         my %ccdhash=();
457         &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
458         foreach my $key (keys %ccdconfhash) {
459                 if ($ccdconfhash{$key}[0] eq $oldname) {
460                         foreach my $key1 (keys %ccdconfhash) {
461                                 if ($ccdconfhash{$key1}[0] eq $newname){
462                                         $errormessage=$errormessage.$Lang::tr{'ccd err netadrexist'};
463                                         return;
464                                 }else{
465                                         $ccdconfhash{$key}[0]= $newname;
466                                         &General::writehasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
467                                         last;
468                                 }
469                         }
470                 }
471         }
472         
473         &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%ccdhash);
474                 foreach my $key (keys %ccdhash) {
475                         if ($ccdhash{$key}[32] eq $oldname) {
476                                 $ccdhash{$key}[32]=$newname;
477                                 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%ccdhash);
478                                 last;
479                         }
480                 }
481         
482         return 0;
483 }
484 sub ccdmaxclients
485 {
486         my $ccdnetwork=$_[0];
487         my @octets=();
488         my @subnet=();
489         @octets=split("\/",$ccdnetwork);
490         @subnet= split /\./, &General::cidrtosub($octets[1]);
491         my ($a,$b,$c,$d,$e);
492         $a=256-$subnet[0];
493         $b=256-$subnet[1];
494         $c=256-$subnet[2];
495         $d=256-$subnet[3];
496         $e=($a*$b*$c*$d)/4;
497         return $e-1;
498 }
499
500 sub getccdadresses 
501 {
502         my $ipin=$_[0];
503         my ($ip1,$ip2,$ip3,$ip4)=split  /\./, $ipin;
504         my $cidr=$_[1];
505         chomp($cidr);
506         my $count=$_[2];
507         my $hasip=$_[3];
508         chomp($hasip);
509         my @iprange=();
510         my %ccdhash=();
511         &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%ccdhash);
512         $iprange[0]=$ip1.".".$ip2.".".$ip3.".".($ip4+2);
513         for (my $i=1;$i<=$count;$i++) {
514                 my $tmpip=$iprange[$i-1];
515                 my $stepper=$i*4;
516                 $iprange[$i]= &General::getnextip($tmpip,4);
517         }
518         my $r=0;
519         foreach my $key (keys %ccdhash) {
520                 $r=0;
521                 foreach  my $tmp (@iprange){
522                         my ($net,$sub) = split (/\//,$ccdhash{$key}[33]);
523                         if ($net eq $tmp) {
524                                 if ( $hasip ne  $ccdhash{$key}[33] ){
525                                         splice (@iprange,$r,1);
526                                 }
527                         }
528                         $r++;
529                 }
530         }
531         return @iprange;
532 }
533
534 sub fillselectbox
535 {
536         my $boxname=$_[1];
537         my ($ccdip,$subcidr) = split("/",$_[0]); 
538         my $tz=$_[2];
539         my @allccdips=&getccdadresses($ccdip,$subcidr,&ccdmaxclients($ccdip."/".$subcidr),$tz);
540         print"<select name='$boxname' STYLE='font-family : arial; font-size : 9pt; width:130px;' >";
541         foreach (@allccdips) {
542                 my $ip=$_."/30";
543                 chomp($ip);
544                 print "<option value='$ip' ";
545                 if ( $ip eq $cgiparams{$boxname} ){
546                         print"selected";
547                 }
548                 print ">$ip</option>";
549         }
550         print "</select>";
551 }
552
553 sub hostsinnet
554 {
555         my $name=$_[0];
556         my %ccdhash=();
557         my $i=0;
558         &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%ccdhash);
559         foreach my $key (keys %ccdhash) {
560                 if ($ccdhash{$key}[32] eq $name){ $i++;}
561         }
562         return $i;
563 }
564
565 sub check_routes_push
566 {
567                         my $val=$_[0];
568                         my ($ip,$cidr) = split (/\//, $val);
569                         ##check for existing routes in routes_push
570                         if (-e "${General::swroot}/ovpn/routes_push") {
571                                 open(FILE,"${General::swroot}/ovpn/routes_push");
572                                 while (<FILE>) {
573                                         $_=~s/\s*$//g;
574                                         
575                                         my ($ip2,$cidr2) = split (/\//,"$_");
576                                         my $val2=$ip2."/".&General::iporsubtodec($cidr2);
577                                         
578                                         if($val eq $val2){
579                                                 return 0;
580                                         }
581                                         #subnetcheck
582                                         if (&General::IpInSubnet ($ip,$ip2,&General::iporsubtodec($cidr2))){
583                                                 return 0;
584                                         }
585                                 };
586                                 close(FILE);
587                         }
588         return 1;
589 }
590
591 sub check_ccdroute
592 {
593         my %ccdroutehash=();
594         my $val=$_[0];
595         my ($ip,$cidr) = split (/\//, $val);
596         #check for existing routes in ccdroute
597         &General::readhasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
598         foreach my $key (keys %ccdroutehash) {
599                 foreach my $i (1 .. $#{$ccdroutehash{$key}}) {
600                         if (&General::iporsubtodec($val) eq $ccdroutehash{$key}[$i] && $ccdroutehash{$key}[0] ne $cgiparams{'NAME'}){
601                                 return 0;
602                         }
603                         my ($ip2,$cidr2) = split (/\//,$ccdroutehash{$key}[$i]);
604                         #subnetcheck
605                         if (&General::IpInSubnet ($ip,$ip2,$cidr2)&& $ccdroutehash{$key}[0] ne $cgiparams{'NAME'} ){
606                                 return 0;
607                         }
608                 }
609         }
610         return 1;
611 }
612 sub check_ccdconf
613 {
614         my %ccdconfhash=();
615         my $val=$_[0];
616         my ($ip,$cidr) = split (/\//, $val);
617         #check for existing routes in ccdroute
618         &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
619         foreach my $key (keys %ccdconfhash) {
620                 if (&General::iporsubtocidr($val) eq $ccdconfhash{$key}[1]){
621                                 return 0;
622                         }
623                         my ($ip2,$cidr2) = split (/\//,$ccdconfhash{$key}[1]);
624                         #subnetcheck
625                         if (&General::IpInSubnet ($ip,$ip2,&General::cidrtosub($cidr2))){
626                                 return 0;
627                         }
628                 
629         }
630         return 1;
631 }
632
633 ###
634 # m.a.d net2net
635 ###
636
637 sub validdotmask
638 {
639         my $ipdotmask = $_[0];
640         if (&General::validip($ipdotmask)) { return 0; }
641         if (!($ipdotmask =~ /^(.*?)\/(.*?)$/)) {  }
642         my $mask = $2;
643         if (($mask =~ /\./ )) { return 0; }     
644   return 1;
645 }
646
647 # -------------------------------------------------------------------
648
649 sub write_routepushfile
650 {
651         open(FILE, ">$routes_push_file");
652         flock(FILE, 2);
653         if ($vpnsettings{'ROUTES_PUSH'} ne '') {
654                 print FILE $vpnsettings{'ROUTES_PUSH'};
655         }
656         close(FILE); 
657 }
658
659 sub read_routepushfile
660 {
661         if (-e "$routes_push_file") {
662                 open(FILE,"$routes_push_file");
663                 delete $vpnsettings{'ROUTES_PUSH'};
664                 while (<FILE>) { $vpnsettings{'ROUTES_PUSH'} .= $_ };
665                 close(FILE);
666                 $cgiparams{'ROUTES_PUSH'} = $vpnsettings{'ROUTES_PUSH'};
667                 
668         }
669 }
670
671
672 #hier die refresh page
673 if ( -e "${General::swroot}/ovpn/gencanow") {
674     my $refresh = '';
675     $refresh = "<meta http-equiv='refresh' content='15;' />";
676     &Header::showhttpheaders();
677     &Header::openpage($Lang::tr{'OVPN'}, 1, $refresh);
678     &Header::openbigbox('100%', 'center');
679     &Header::openbox('100%', 'left', "$Lang::tr{'generate root/host certificates'}:");
680     print "<tr>\n<td align='center'><img src='/images/clock.gif' alt='' /></td>\n";
681     print "<td colspan='2'><font color='red'>Please be patient this realy can take some time on older hardware...</font></td></tr>\n";
682     &Header::closebox();
683     &Header::closebigbox();
684     &Header::closepage();
685     exit (0);
686 }
687 ##hier die refresh page
688
689
690 ###
691 ### OpenVPN Server Control
692 ###
693 if ($cgiparams{'ACTION'} eq $Lang::tr{'start ovpn server'} ||
694     $cgiparams{'ACTION'} eq $Lang::tr{'stop ovpn server'} ||
695     $cgiparams{'ACTION'} eq $Lang::tr{'restart ovpn server'}) {
696     #start openvpn server
697     if ($cgiparams{'ACTION'} eq $Lang::tr{'start ovpn server'}){
698         &emptyserverlog();
699         system('/usr/local/bin/openvpnctrl', '-s');
700     }   
701     #stop openvpn server
702     if ($cgiparams{'ACTION'} eq $Lang::tr{'stop ovpn server'}){
703         system('/usr/local/bin/openvpnctrl', '-k');
704         &emptyserverlog();      
705     }   
706 #    #restart openvpn server
707 #    if ($cgiparams{'ACTION'} eq $Lang::tr{'restart ovpn server'}){
708 #workarund, till SIGHUP also works when running as nobody    
709 #       system('/usr/local/bin/openvpnctrl', '-r');     
710 #       &emptyserverlog();      
711 #    }       
712 }
713
714 ###
715 ### Save Advanced options
716 ###
717
718 if ($cgiparams{'ACTION'} eq $Lang::tr{'save-adv-options'}) {
719     &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
720     #DAN do we really need (to to check) this value? Besides if we listen on blue and orange too,
721     #DAN this value has to leave.
722 #new settings for daemon
723     $vpnsettings{'LOG_VERB'} = $cgiparams{'LOG_VERB'};
724     $vpnsettings{'KEEPALIVE_1'} = $cgiparams{'KEEPALIVE_1'};
725     $vpnsettings{'KEEPALIVE_2'} = $cgiparams{'KEEPALIVE_2'};
726     $vpnsettings{'MAX_CLIENTS'} = $cgiparams{'MAX_CLIENTS'};
727     $vpnsettings{'REDIRECT_GW_DEF1'} = $cgiparams{'REDIRECT_GW_DEF1'};
728     $vpnsettings{'CLIENT2CLIENT'} = $cgiparams{'CLIENT2CLIENT'};
729     $vpnsettings{'ADDITIONAL_CONFIGS'} = $cgiparams{'ADDITIONAL_CONFIGS'};
730     $vpnsettings{'DHCP_DOMAIN'} = $cgiparams{'DHCP_DOMAIN'};
731     $vpnsettings{'DHCP_DNS'} = $cgiparams{'DHCP_DNS'};
732     $vpnsettings{'DHCP_WINS'} = $cgiparams{'DHCP_WINS'};
733     $vpnsettings{'ROUTES_PUSH'} = $cgiparams{'ROUTES_PUSH'};
734     $vpnsettings{'PMTU_DISCOVERY'} = $cgiparams{'PMTU_DISCOVERY'};
735     $vpnsettings{'DAUTH'} = $cgiparams{'DAUTH'};
736     $vpnsettings{'TLSAUTH'} = $cgiparams{'TLSAUTH'};
737     my @temp=();
738     
739     if ($cgiparams{'FRAGMENT'} eq '') {
740         delete $vpnsettings{'FRAGMENT'};
741     } else {
742         if ($cgiparams{'FRAGMENT'} !~ /^[0-9]+$/) { 
743             $errormessage = "Incorrect value, please insert only numbers.";
744         goto ADV_ERROR;
745                 } else {
746                         $vpnsettings{'FRAGMENT'} = $cgiparams{'FRAGMENT'};
747         }
748     }
749
750     if ($cgiparams{'MSSFIX'} ne 'on') {
751         delete $vpnsettings{'MSSFIX'};
752     } else {
753         $vpnsettings{'MSSFIX'} = $cgiparams{'MSSFIX'};
754     }
755
756     if (($cgiparams{'PMTU_DISCOVERY'} eq 'yes') ||
757         ($cgiparams{'PMTU_DISCOVERY'} eq 'maybe') ||
758         ($cgiparams{'PMTU_DISCOVERY'} eq 'no' )) {
759
760         if (($cgiparams{'MSSFIX'} eq 'on') || ($cgiparams{'FRAGMENT'} ne '')) {
761                 $errormessage = $Lang::tr{'ovpn mtu-disc with mssfix or fragment'};
762                 goto ADV_ERROR;
763         }
764     }
765                 
766     if ($cgiparams{'DHCP_DOMAIN'} ne ''){
767         unless (&General::validdomainname($cgiparams{'DHCP_DOMAIN'}) || &General::validip($cgiparams{'DHCP_DOMAIN'})) {
768                 $errormessage = $Lang::tr{'invalid input for dhcp domain'};
769         goto ADV_ERROR;
770         }
771     }
772     if ($cgiparams{'DHCP_DNS'} ne ''){
773         unless (&General::validfqdn($cgiparams{'DHCP_DNS'}) || &General::validip($cgiparams{'DHCP_DNS'})) {
774                 $errormessage = $Lang::tr{'invalid input for dhcp dns'};
775         goto ADV_ERROR;
776         }
777     }
778     if ($cgiparams{'DHCP_WINS'} ne ''){
779         unless (&General::validfqdn($cgiparams{'DHCP_WINS'}) || &General::validip($cgiparams{'DHCP_WINS'})) {
780                 $errormessage = $Lang::tr{'invalid input for dhcp wins'};
781                 goto ADV_ERROR;
782         }
783     }
784     if ($cgiparams{'ROUTES_PUSH'} ne ''){
785         @temp = split(/\n/,$cgiparams{'ROUTES_PUSH'});
786         undef $vpnsettings{'ROUTES_PUSH'};
787         
788         foreach my $tmpip (@temp)
789         {
790                 s/^\s+//g; s/\s+$//g;
791                 
792                 if ($tmpip)
793                 {
794                         $tmpip=~s/\s*$//g; 
795                         unless (&General::validipandmask($tmpip)) {
796                                 $errormessage = "$tmpip ".$Lang::tr{'ovpn errmsg invalid ip or mask'};
797                                 goto ADV_ERROR;
798                         }
799                         my ($ip, $cidr) = split("\/",&General::ipcidr2msk($tmpip));
800                         
801                         if ($ip eq $netsettings{'GREEN_NETADDRESS'} && $cidr eq $netsettings{'GREEN_NETMASK'}) {
802                                 $errormessage = $Lang::tr{'ovpn errmsg green already pushed'};
803                                 goto ADV_ERROR;
804                         }
805 # a.marx ccd                    
806                         my %ccdroutehash=();
807                         &General::readhasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
808                         foreach my $key (keys %ccdroutehash) {
809                                 foreach my $i (1 .. $#{$ccdroutehash{$key}}) { 
810                                         if ( $ip."/".$cidr eq $ccdroutehash{$key}[$i] ){
811                                                 $errormessage="Route $ip\/$cidr ".$Lang::tr{'ccd err inuse'}." $ccdroutehash{$key}[0]" ;
812                                                 goto ADV_ERROR;
813                                         }
814                                         my ($ip2,$cidr2) = split(/\//,$ccdroutehash{$key}[$i]);
815                                         if (&General::IpInSubnet ($ip,$ip2,$cidr2)){
816                                                 $errormessage="Route $ip\/$cidr ".$Lang::tr{'ccd err inuse'}." $ccdroutehash{$key}[0]" ;
817                                                 goto ADV_ERROR;
818                                         }
819                                 }
820                         }
821                         
822 # ccd end
823                         
824                         $vpnsettings{'ROUTES_PUSH'} .= $tmpip."\n";
825                 }
826         }
827     &write_routepushfile;
828         undef $vpnsettings{'ROUTES_PUSH'};
829     }
830         else {
831         undef $vpnsettings{'ROUTES_PUSH'};
832         &write_routepushfile;
833     }
834     if ((length($cgiparams{'MAX_CLIENTS'}) == 0) || (($cgiparams{'MAX_CLIENTS'}) < 1 ) || (($cgiparams{'MAX_CLIENTS'}) > 255 )) {
835         $errormessage = $Lang::tr{'invalid input for max clients'};
836         goto ADV_ERROR;
837     }
838     if ($cgiparams{'KEEPALIVE_1'} ne '') {
839         if ($cgiparams{'KEEPALIVE_1'} !~ /^[0-9]+$/) { 
840             $errormessage = $Lang::tr{'invalid input for keepalive 1'};
841         goto ADV_ERROR;
842         }
843     }
844     if ($cgiparams{'KEEPALIVE_2'} ne ''){
845         if ($cgiparams{'KEEPALIVE_2'} !~ /^[0-9]+$/) { 
846             $errormessage = $Lang::tr{'invalid input for keepalive 2'};
847         goto ADV_ERROR;
848         }
849     }
850     if ($cgiparams{'KEEPALIVE_2'} < ($cgiparams{'KEEPALIVE_1'} * 2)){
851         $errormessage = $Lang::tr{'invalid input for keepalive 1:2'};
852         goto ADV_ERROR; 
853     }
854     # Create ta.key for tls-auth if not presant
855     if ($cgiparams{'TLSAUTH'} eq 'on') {
856         if ( ! -e "${General::swroot}/ovpn/certs/ta.key") {
857                 system('/usr/sbin/openvpn', '--genkey', '--secret', "${General::swroot}/ovpn/certs/ta.key");
858                 if ($?) {
859                 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
860         goto ADV_ERROR;
861                 }
862         }
863     }
864     
865     &General::writehash("${General::swroot}/ovpn/settings", \%vpnsettings);
866     &writeserverconf();#hier ok
867 }
868
869 ###
870 # m.a.d net2net
871 ###
872
873 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq 'net' && $cgiparams{'SIDE'} eq 'server')
874 {
875
876 my @remsubnet = split(/\//,$cgiparams{'REMOTE_SUBNET'});
877 my @ovsubnettemp =  split(/\./,$cgiparams{'OVPN_SUBNET'});
878 my $ovsubnet =  "$ovsubnettemp[0].$ovsubnettemp[1].$ovsubnettemp[2]";
879 my $tunmtu =  '';
880
881 unless(-d "${General::swroot}/ovpn/n2nconf/"){mkdir "${General::swroot}/ovpn/n2nconf", 0755 or die "Unable to create dir $!";}
882 unless(-d "${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}"){mkdir "${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}", 0770 or die "Unable to create dir $!";}   
883
884   open(SERVERCONF,    ">${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}/$cgiparams{'NAME'}.conf") or die "Unable to open ${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}/$cgiparams{'NAME'}.conf: $!";
885   
886   flock SERVERCONF, 2;
887   print SERVERCONF "# IPFire n2n Open VPN Server Config by ummeegge und m.a.d\n"; 
888   print SERVERCONF "\n"; 
889   print SERVERCONF "# User Security\n";
890   print SERVERCONF "user nobody\n";
891   print SERVERCONF "group nobody\n";
892   print SERVERCONF "persist-tun\n";
893   print SERVERCONF "persist-key\n";
894   print SERVERCONF "script-security 2\n";
895   print SERVERCONF "# IP/DNS for remote Server Gateway\n"; 
896
897   if ($cgiparams{'REMOTE'} ne '') {
898   print SERVERCONF "remote $cgiparams{'REMOTE'}\n";
899   }
900
901   print SERVERCONF "float\n";
902   print SERVERCONF "# IP adresses of the VPN Subnet\n"; 
903   print SERVERCONF "ifconfig $ovsubnet.1 $ovsubnet.2\n"; 
904   print SERVERCONF "# Client Gateway Network\n"; 
905   print SERVERCONF "route $remsubnet[0] $remsubnet[1]\n";
906   print SERVERCONF "# tun Device\n"; 
907   print SERVERCONF "dev tun\n"; 
908   print SERVERCONF "# Port and Protokol\n"; 
909   print SERVERCONF "port $cgiparams{'DEST_PORT'}\n"; 
910   
911   if ($cgiparams{'PROTOCOL'} eq 'tcp') {
912   print SERVERCONF "proto tcp-server\n";
913   print SERVERCONF "# Packet size\n";
914   if ($cgiparams{'MTU'} eq '') {$tunmtu = '1400'} else {$tunmtu = $cgiparams{'MTU'}};
915   print SERVERCONF "tun-mtu $tunmtu\n";
916   }
917   
918   if ($cgiparams{'PROTOCOL'} eq 'udp') {
919   print SERVERCONF "proto udp\n"; 
920   print SERVERCONF "# Paketsize\n";
921   if ($cgiparams{'MTU'} eq '') {$tunmtu = '1500'} else {$tunmtu = $cgiparams{'MTU'}};
922   print SERVERCONF "tun-mtu $tunmtu\n";
923   if ($cgiparams{'FRAGMENT'} ne '') {print SERVERCONF "fragment $cgiparams{'FRAGMENT'}\n";} 
924   if ($cgiparams{'MSSFIX'} eq 'on') {print SERVERCONF "mssfix\n"; }; 
925   }
926
927   # Check if a valid operating mode has been choosen and use it.
928   if (($cgiparams{'PMTU_DISCOVERY'} eq 'yes') ||
929       ($cgiparams{'PMTU_DISCOVERY'} eq 'maybe') ||
930       ($cgiparams{'PMTU_DISCOVERY'} eq 'no' )) {
931         if(($cgiparams{'MSSFIX'} ne 'on') || ($cgiparams{'FRAGMENT'} eq '')) {
932                 if($cgiparams{'MTU'} eq '1500') {
933                         print SERVERCONF "mtu-disc $cgiparams{'PMTU_DISCOVERY'}\n";
934                 }
935         }
936   }
937   print SERVERCONF "# Auth. Server\n"; 
938   print SERVERCONF "tls-server\n"; 
939   print SERVERCONF "ca ${General::swroot}/ovpn/ca/cacert.pem\n"; 
940   print SERVERCONF "cert ${General::swroot}/ovpn/certs/servercert.pem\n"; 
941   print SERVERCONF "key ${General::swroot}/ovpn/certs/serverkey.pem\n"; 
942   print SERVERCONF "dh ${General::swroot}/ovpn/ca/$cgiparams{'DH_NAME'}\n";
943   print SERVERCONF "# Cipher\n"; 
944   print SERVERCONF "cipher $cgiparams{'DCIPHER'}\n";
945   if ($cgiparams{'DAUTH'} eq '') {
946         print SERVERCONF "auth SHA1\n";
947   } else {
948         print SERVERCONF "# HMAC algorithm\n";
949         print SERVERCONF "auth $cgiparams{'DAUTH'}\n";
950   }
951   if ($cgiparams{'COMPLZO'} eq 'on') {
952    print SERVERCONF "# Enable Compression\n";
953    print SERVERCONF "comp-lzo\n";
954      }
955   print SERVERCONF "# Debug Level\n"; 
956   print SERVERCONF "verb 3\n"; 
957   print SERVERCONF "# Tunnel check\n"; 
958   print SERVERCONF "keepalive 10 60\n"; 
959   print SERVERCONF "# Start as daemon\n"; 
960   print SERVERCONF "daemon $cgiparams{'NAME'}n2n\n"; 
961   print SERVERCONF "writepid /var/run/$cgiparams{'NAME'}n2n.pid\n"; 
962   print SERVERCONF "# Activate Management Interface and Port\n"; 
963   if ($cgiparams{'OVPN_MGMT'} eq '') {print SERVERCONF "management localhost $cgiparams{'DEST_PORT'}\n"}
964   else {print SERVERCONF "management localhost $cgiparams{'OVPN_MGMT'}\n"};
965   close(SERVERCONF);
966
967 }
968
969 ###
970 # m.a.d net2net
971 ###
972
973 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq 'net' && $cgiparams{'SIDE'} eq 'client')
974 {
975
976         my @ovsubnettemp =  split(/\./,$cgiparams{'OVPN_SUBNET'});
977         my $ovsubnet =  "$ovsubnettemp[0].$ovsubnettemp[1].$ovsubnettemp[2]";
978         my @remsubnet =  split(/\//,$cgiparams{'REMOTE_SUBNET'});
979         my $tunmtu =  '';
980                    
981 unless(-d "${General::swroot}/ovpn/n2nconf/"){mkdir "${General::swroot}/ovpn/n2nconf", 0755 or die "Unable to create dir $!";}
982 unless(-d "${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}"){mkdir "${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}", 0770 or die "Unable to create dir $!";}
983   
984   open(CLIENTCONF,    ">${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}/$cgiparams{'NAME'}.conf") or die "Unable to open ${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}/$cgiparams{'NAME'}.conf: $!";
985   
986   flock CLIENTCONF, 2;
987   print CLIENTCONF "# IPFire rewritten n2n Open VPN Client Config by ummeegge und m.a.d\n";
988   print CLIENTCONF "#\n"; 
989   print CLIENTCONF "# User Security\n";
990   print CLIENTCONF "user nobody\n";
991   print CLIENTCONF "group nobody\n";
992   print CLIENTCONF "persist-tun\n";
993   print CLIENTCONF "persist-key\n";
994   print CLIENTCONF "script-security 2\n";
995   print CLIENTCONF "# IP/DNS for remote Server Gateway\n"; 
996   print CLIENTCONF "remote $cgiparams{'REMOTE'}\n";
997   print CLIENTCONF "float\n";
998   print CLIENTCONF "# IP adresses of the VPN Subnet\n"; 
999   print CLIENTCONF "ifconfig $ovsubnet.2 $ovsubnet.1\n"; 
1000   print CLIENTCONF "# Server Gateway Network\n"; 
1001   print CLIENTCONF "route $remsubnet[0] $remsubnet[1]\n"; 
1002   print CLIENTCONF "# tun Device\n"; 
1003   print CLIENTCONF "dev tun\n"; 
1004   print CLIENTCONF "# Port and Protokol\n"; 
1005   print CLIENTCONF "port $cgiparams{'DEST_PORT'}\n"; 
1006
1007   if ($cgiparams{'PROTOCOL'} eq 'tcp') {
1008   print CLIENTCONF "proto tcp-client\n";
1009   print CLIENTCONF "# Packet size\n";
1010   if ($cgiparams{'MTU'} eq '') {$tunmtu = '1400'} else {$tunmtu = $cgiparams{'MTU'}};
1011   print CLIENTCONF "tun-mtu $tunmtu\n";
1012   }
1013   
1014   if ($cgiparams{'PROTOCOL'} eq 'udp') {
1015   print CLIENTCONF "proto udp\n"; 
1016   print CLIENTCONF "# Paketsize\n";
1017   if ($cgiparams{'MTU'} eq '') {$tunmtu = '1500'} else {$tunmtu = $cgiparams{'MTU'}};
1018   print CLIENTCONF "tun-mtu $tunmtu\n";
1019   if ($cgiparams{'FRAGMENT'} ne '') {print CLIENTCONF "fragment $cgiparams{'FRAGMENT'}\n";}
1020   if ($cgiparams{'MSSFIX'} eq 'on') {print CLIENTCONF "mssfix\n"; }; 
1021   }
1022
1023   # Check if a valid operating mode has been choosen and use it.
1024   if (($cgiparams{'PMTU_DISCOVERY'} eq 'yes') ||
1025       ($cgiparams{'PMTU_DISCOVERY'} eq 'maybe') ||
1026       ($cgiparams{'PMTU_DISCOVERY'} eq 'no' )) {
1027         if(($cgiparams{'MSSFIX'} ne 'on') || ($cgiparams{'FRAGMENT'} eq '')) {
1028                 if ($cgiparams{'MTU'} eq '1500') {
1029                         print CLIENTCONF "mtu-disc $cgiparams{'PMTU_DISCOVERY'}\n";
1030                 }
1031         }
1032   }
1033  
1034   print CLIENTCONF "ns-cert-type server\n";   
1035   print CLIENTCONF "# Auth. Client\n"; 
1036   print CLIENTCONF "tls-client\n"; 
1037   print CLIENTCONF "# Cipher\n"; 
1038   print CLIENTCONF "cipher $cgiparams{'DCIPHER'}\n";
1039   print CLIENTCONF "pkcs12 ${General::swroot}/ovpn/certs/$cgiparams{'NAME'}.p12\r\n";
1040   if ($cgiparams{'DAUTH'} eq '') {
1041         print CLIENTCONF "auth SHA1\n";
1042   } else {
1043         print CLIENTCONF "# HMAC algorithm\n";
1044         print CLIENTCONF "auth $cgiparams{'DAUTH'}\n";
1045   }
1046   if ($cgiparams{'COMPLZO'} eq 'on') {
1047    print CLIENTCONF "# Enable Compression\n";
1048    print CLIENTCONF "comp-lzo\n";
1049   }
1050   print CLIENTCONF "# Debug Level\n"; 
1051   print CLIENTCONF "verb 3\n"; 
1052   print CLIENTCONF "# Tunnel check\n"; 
1053   print CLIENTCONF "keepalive 10 60\n"; 
1054   print CLIENTCONF "# Start as daemon\n"; 
1055   print CLIENTCONF "daemon $cgiparams{'NAME'}n2n\n";
1056   print CLIENTCONF "writepid /var/run/$cgiparams{'NAME'}n2n.pid\n"; 
1057   print CLIENTCONF "# Activate Management Interface and Port\n"; 
1058   if ($cgiparams{'OVPN_MGMT'} eq '') {print CLIENTCONF "management localhost $cgiparams{'DEST_PORT'}\n"}
1059   else {print CLIENTCONF "management localhost $cgiparams{'OVPN_MGMT'}\n"};
1060   close(CLIENTCONF);
1061
1062 }
1063   
1064 ###
1065 ### Save main settings
1066 ###
1067
1068 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq '' && $cgiparams{'KEY'} eq '') {
1069     &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1070     #DAN do we really need (to to check) this value? Besides if we listen on blue and orange too,
1071     #DAN this value has to leave.
1072     if ($cgiparams{'ENABLED'} eq 'on'){
1073         unless (&General::validfqdn($cgiparams{'VPN_IP'}) || &General::validip($cgiparams{'VPN_IP'})) {
1074                 $errormessage = $Lang::tr{'invalid input for hostname'};
1075         goto SETTINGS_ERROR;
1076         }
1077     }
1078
1079     if (! &General::validipandmask($cgiparams{'DOVPN_SUBNET'})) {
1080             $errormessage = $Lang::tr{'ovpn subnet is invalid'};
1081                         goto SETTINGS_ERROR;
1082     }
1083     my @tmpovpnsubnet = split("\/",$cgiparams{'DOVPN_SUBNET'});
1084     
1085     if (&General::IpInSubnet ( $netsettings{'RED_ADDRESS'}, 
1086         $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1087         $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire RED Network $netsettings{'RED_ADDRESS'}";
1088         goto SETTINGS_ERROR;
1089     }
1090     
1091     if (&General::IpInSubnet ( $netsettings{'GREEN_ADDRESS'}, 
1092         $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1093         $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire Green Network $netsettings{'GREEN_ADDRESS'}";
1094         goto SETTINGS_ERROR;
1095     }
1096
1097     if (&General::IpInSubnet ( $netsettings{'BLUE_ADDRESS'}, 
1098         $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1099         $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire Blue Network $netsettings{'BLUE_ADDRESS'}";
1100         goto SETTINGS_ERROR;
1101     }
1102     
1103     if (&General::IpInSubnet ( $netsettings{'ORANGE_ADDRESS'}, 
1104         $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1105         $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire Orange Network $netsettings{'ORANGE_ADDRESS'}";
1106         goto SETTINGS_ERROR;
1107     }
1108     open(ALIASES, "${General::swroot}/ethernet/aliases") or die 'Unable to open aliases file.';
1109     while (<ALIASES>)
1110     {
1111         chomp($_);
1112         my @tempalias = split(/\,/,$_);
1113         if ($tempalias[1] eq 'on') {
1114             if (&General::IpInSubnet ($tempalias[0] , 
1115                 $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1116                 $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire alias entry $tempalias[0]";
1117             }           
1118         }
1119     }
1120     close(ALIASES);
1121     if ($errormessage ne ''){
1122         goto SETTINGS_ERROR;
1123     }
1124     if ($cgiparams{'ENABLED'} !~ /^(on|off)$/) {
1125         $errormessage = $Lang::tr{'invalid input'};
1126         goto SETTINGS_ERROR;
1127     }
1128     if ((length($cgiparams{'DMTU'})==0) || (($cgiparams{'DMTU'}) < 1000 )) {
1129         $errormessage = $Lang::tr{'invalid mtu input'};
1130         goto SETTINGS_ERROR;
1131     }
1132     
1133     unless (&General::validport($cgiparams{'DDEST_PORT'})) {
1134         $errormessage = $Lang::tr{'invalid port'};
1135         goto SETTINGS_ERROR;
1136     }
1137
1138     $vpnsettings{'ENABLED_BLUE'} = $cgiparams{'ENABLED_BLUE'};
1139     $vpnsettings{'ENABLED_ORANGE'} =$cgiparams{'ENABLED_ORANGE'};
1140     $vpnsettings{'ENABLED'} = $cgiparams{'ENABLED'};
1141     $vpnsettings{'VPN_IP'} = $cgiparams{'VPN_IP'};
1142 #new settings for daemon
1143     $vpnsettings{'DOVPN_SUBNET'} = $cgiparams{'DOVPN_SUBNET'};
1144     $vpnsettings{'DDEVICE'} = $cgiparams{'DDEVICE'};
1145     $vpnsettings{'DPROTOCOL'} = $cgiparams{'DPROTOCOL'};
1146     $vpnsettings{'DDEST_PORT'} = $cgiparams{'DDEST_PORT'};
1147     $vpnsettings{'DMTU'} = $cgiparams{'DMTU'};
1148     $vpnsettings{'DCOMPLZO'} = $cgiparams{'DCOMPLZO'};
1149     $vpnsettings{'DCIPHER'} = $cgiparams{'DCIPHER'};
1150 #wrtie enable
1151
1152   if ( $vpnsettings{'ENABLED_BLUE'} eq 'on' ) {system("touch ${General::swroot}/ovpn/enable_blue 2>/dev/null");}else{system("unlink ${General::swroot}/ovpn/enable_blue 2>/dev/null");}
1153   if ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' ) {system("touch ${General::swroot}/ovpn/enable_orange 2>/dev/null");}else{system("unlink ${General::swroot}/ovpn/enable_orange 2>/dev/null");}
1154   if ( $vpnsettings{'ENABLED'} eq 'on' ) {system("touch ${General::swroot}/ovpn/enable 2>/dev/null");}else{system("unlink ${General::swroot}/ovpn/enable 2>/dev/null");}
1155 #new settings for daemon    
1156     &General::writehash("${General::swroot}/ovpn/settings", \%vpnsettings);
1157     &writeserverconf();#hier ok
1158 SETTINGS_ERROR:
1159 ###
1160 ### Reset all step 2
1161 ###
1162 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove x509'} && $cgiparams{'AREUSURE'} eq 'yes') {
1163     my $file = '';
1164     &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1165
1166     foreach my $key (keys %confighash) {
1167         if ($confighash{$key}[4] eq 'cert') {
1168             delete $confighash{$cgiparams{'$key'}};
1169         }
1170     }
1171     while ($file = glob("${General::swroot}/ovpn/ca/*")) {
1172         unlink $file;
1173     }
1174     while ($file = glob("${General::swroot}/ovpn/certs/*")) {
1175         unlink $file;
1176     }
1177     while ($file = glob("${General::swroot}/ovpn/crls/*")) {
1178         unlink $file;
1179     }
1180         &cleanssldatabase();
1181     if (open(FILE, ">${General::swroot}/ovpn/caconfig")) {
1182         print FILE "";
1183         close FILE;
1184     }
1185     if (open(FILE, ">${General::swroot}/ovpn/ccdroute")) {
1186         print FILE "";
1187         close FILE;
1188     }
1189     if (open(FILE, ">${General::swroot}/ovpn/ccdroute2")) {
1190         print FILE "";
1191         close FILE;
1192     }
1193     while ($file = glob("${General::swroot}/ovpn/ccd/*")) {
1194         unlink $file
1195     }
1196     if (open(FILE, ">${General::swroot}/ovpn/ovpn-leases.db")) {
1197         print FILE "";
1198         close FILE;
1199     }
1200     if (open(FILE, ">${General::swroot}/ovpn/ovpnconfig")) {
1201         print FILE "";
1202         close FILE;
1203     }
1204     while ($file = glob("${General::swroot}/ovpn/n2nconf/*")) {
1205         system ("rm -rf $file");
1206     }
1207
1208     #&writeserverconf();
1209 ###
1210 ### Reset all step 1
1211 ###
1212 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove x509'}) {
1213     &Header::showhttpheaders();
1214     &Header::openpage($Lang::tr{'ovpn'}, 1, '');
1215     &Header::openbigbox('100%', 'left', '', '');
1216     &Header::openbox('100%', 'left', $Lang::tr{'are you sure'});
1217     print <<END;
1218         <form method='post'>
1219                 <table width='100%'>
1220                         <tr>
1221                                 <td align='center'>
1222                                 <input type='hidden' name='AREUSURE' value='yes' />
1223                                 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>:
1224                                 $Lang::tr{'resetting the vpn configuration will remove the root ca, the host certificate and all certificate based connections'}</td>
1225                         </tr>
1226                         <tr>
1227                                 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'remove x509'}' />
1228                                 <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></td>
1229                         </tr>
1230                 </table>
1231         </form>
1232 END
1233     ;
1234     &Header::closebox();
1235     &Header::closebigbox();
1236     &Header::closepage();
1237     exit (0);
1238
1239 ###
1240 ### Generate DH key step 2
1241 ###
1242 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'generate dh key'} && $cgiparams{'AREUSURE'} eq 'yes') {
1243     # Delete if old key exists
1244     if (-f "${General::swroot}/ovpn/ca/$cgiparams{'DH_NAME'}") {
1245         unlink "${General::swroot}/ovpn/ca/$cgiparams{'DH_NAME'}";
1246         }
1247         # Create Diffie Hellmann Parameter
1248         system('/usr/bin/openssl', 'dhparam', '-rand', '/proc/interrupts:/proc/net/rt_cache',
1249         '-out', "${General::swroot}/ovpn/ca/dh1024.pem", "$cgiparams{'DHLENGHT'}");
1250         if ($?) {
1251                 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1252                 unlink ("${General::swroot}/ovpn/ca/dh1024.pem");
1253         }
1254
1255 ###
1256 ### Generate DH key step 1
1257 ###
1258 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'generate dh key'}) {
1259         &Header::showhttpheaders();
1260         &Header::openpage($Lang::tr{'ovpn'}, 1, '');
1261         &Header::openbigbox('100%', 'LEFT', '', '');
1262         &Header::openbox('100%', 'LEFT', "$Lang::tr{'gen dh'}:");
1263         print <<END;
1264         <table width='100%'>
1265         <tr>
1266                 <td width='20%'> </td> <td width='15%'></td> <td width='65%'></td>
1267         </tr>
1268         <tr>
1269                 <td class='base'>$Lang::tr{'ovpn dh'}:</td>
1270                 <td align='center'>
1271                 <form method='post'><input type='hidden' name='AREUSURE' value='yes' />
1272                 <input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />
1273                         <select name='DHLENGHT'>
1274                                 <option value='1024' $selected{'DHLENGHT'}{'1024'}>1024 $Lang::tr{'bit'}</option>
1275                                 <option value='2048' $selected{'DHLENGHT'}{'2048'}>2048 $Lang::tr{'bit'}</option>
1276                                 <option value='3072' $selected{'DHLENGHT'}{'3072'}>3072 $Lang::tr{'bit'}</option>
1277                                 <option value='4096' $selected{'DHLENGHT'}{'4096'}>4096 $Lang::tr{'bit'}</option>
1278                         </select>
1279                 </td>
1280         </tr>
1281         <tr><td colspan='4'><br></td></tr>
1282         </table>
1283         <table width='100%'>
1284         <tr>
1285                 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}: </font></b>$Lang::tr{'dh key warn'}
1286         </tr>
1287         <tr>
1288                 <td class='base'>$Lang::tr{'dh key warn1'}</td>
1289         </tr>
1290         <tr><td colspan='2'><br></td></tr>
1291         <tr>
1292                 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'generate dh key'}' /></td>
1293                 </form>
1294         </tr>
1295         </table>
1296
1297 END
1298         ;
1299         &Header::closebox();
1300         print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1301         &Header::closebigbox();
1302         &Header::closepage();
1303         exit (0);
1304
1305 ###
1306 ### Upload DH key
1307 ###
1308 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload dh key'}) {
1309     if (ref ($cgiparams{'FH'}) ne 'Fh') {
1310          $errormessage = $Lang::tr{'there was no file upload'};
1311          goto UPLOADCA_ERROR;
1312     }
1313     # Move uploaded dh key to a temporary file
1314     (my $fh, my $filename) = tempfile( );
1315     if (copy ($cgiparams{'FH'}, $fh) != 1) {
1316         $errormessage = $!;
1317         goto UPLOADCA_ERROR;
1318     }
1319     my $temp = `/usr/bin/openssl dhparam -text -in $filename`;
1320     if ($temp !~ /DH Parameters: \((1024|2048|3072|4096) bit\)/) {
1321         $errormessage = $Lang::tr{'not a valid dh key'};
1322         unlink ($filename);
1323         goto UPLOADCA_ERROR;
1324     } else {
1325     # Delete if old key exists
1326     if (-f "${General::swroot}/ovpn/ca/$cgiparams{'DH_NAME'}") {
1327         unlink "${General::swroot}/ovpn/ca/$cgiparams{'DH_NAME'}";
1328         }
1329     move($filename, "${General::swroot}/ovpn/ca/$cgiparams{'DH_NAME'}");
1330         if ($? ne 0) {
1331                 $errormessage = "$Lang::tr{'dh key move failed'}: $!";
1332                 unlink ($filename);
1333                 goto UPLOADCA_ERROR;
1334         }
1335     }
1336
1337 ###
1338 ### Upload CA Certificate
1339 ###
1340 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload ca certificate'}) {
1341     &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1342
1343     if ($cgiparams{'CA_NAME'} !~ /^[a-zA-Z0-9]+$/) {
1344         $errormessage = $Lang::tr{'name must only contain characters'};
1345         goto UPLOADCA_ERROR;
1346     }
1347
1348     if (length($cgiparams{'CA_NAME'}) >60) {
1349         $errormessage = $Lang::tr{'name too long'};
1350         goto VPNCONF_ERROR;
1351     }
1352
1353     if ($cgiparams{'CA_NAME'} eq 'ca') {
1354         $errormessage = $Lang::tr{'name is invalid'};
1355         goto UPLOADCA_ERROR;
1356     }
1357
1358     # Check if there is no other entry with this name
1359     foreach my $key (keys %cahash) {
1360         if ($cahash{$key}[0] eq $cgiparams{'CA_NAME'}) {
1361             $errormessage = $Lang::tr{'a ca certificate with this name already exists'};
1362             goto UPLOADCA_ERROR;
1363         }
1364     }
1365
1366     if (ref ($cgiparams{'FH'}) ne 'Fh') {
1367         $errormessage = $Lang::tr{'there was no file upload'};
1368         goto UPLOADCA_ERROR;
1369     }
1370     # Move uploaded ca to a temporary file
1371     (my $fh, my $filename) = tempfile( );
1372     if (copy ($cgiparams{'FH'}, $fh) != 1) {
1373         $errormessage = $!;
1374         goto UPLOADCA_ERROR;
1375     }
1376     my $temp = `/usr/bin/openssl x509 -text -in $filename`;
1377     if ($temp !~ /CA:TRUE/i) {
1378         $errormessage = $Lang::tr{'not a valid ca certificate'};
1379         unlink ($filename);
1380         goto UPLOADCA_ERROR;
1381     } else {
1382         move($filename, "${General::swroot}/ovpn/ca/$cgiparams{'CA_NAME'}cert.pem");
1383         if ($? ne 0) {
1384             $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1385             unlink ($filename);
1386             goto UPLOADCA_ERROR;
1387         }
1388     }
1389
1390     my $casubject = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/ca/$cgiparams{'CA_NAME'}cert.pem`;
1391     $casubject    =~ /Subject: (.*)[\n]/;
1392     $casubject    = $1;
1393     $casubject    =~ s+/Email+, E+;
1394     $casubject    =~ s/ ST=/ S=/;
1395     $casubject    = &Header::cleanhtml($casubject);
1396
1397     my $key = &General::findhasharraykey (\%cahash);
1398     $cahash{$key}[0] = $cgiparams{'CA_NAME'};
1399     $cahash{$key}[1] = $casubject;
1400     &General::writehasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1401 #    system('/usr/local/bin/ipsecctrl', 'R');
1402
1403     UPLOADCA_ERROR:
1404
1405 ###
1406 ### Display ca certificate
1407 ###
1408 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show ca certificate'}) {
1409     &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1410
1411     if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem") {
1412         &Header::showhttpheaders();
1413         &Header::openpage($Lang::tr{'ovpn'}, 1, '');
1414         &Header::openbigbox('100%', 'LEFT', '', $errormessage);
1415         &Header::openbox('100%', 'LEFT', "$Lang::tr{'ca certificate'}:");
1416         my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
1417         $output = &Header::cleanhtml($output,"y");
1418         print "<pre>$output</pre>\n";
1419         &Header::closebox();
1420         print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1421         &Header::closebigbox();
1422         &Header::closepage();
1423         exit(0);
1424     } else {
1425         $errormessage = $Lang::tr{'invalid key'};
1426     }
1427
1428 ###
1429 ### Download ca certificate
1430 ###
1431 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download ca certificate'}) {
1432     &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1433
1434     if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
1435         print "Content-Type: application/octet-stream\r\n";
1436         print "Content-Disposition: filename=$cahash{$cgiparams{'KEY'}}[0]cert.pem\r\n\r\n";
1437         print `/usr/bin/openssl x509 -in ${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
1438         exit(0);
1439     } else {
1440         $errormessage = $Lang::tr{'invalid key'};
1441     }
1442
1443 ###
1444 ### Remove ca certificate (step 2)
1445 ###
1446 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'} && $cgiparams{'AREUSURE'} eq 'yes') {
1447     &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1448     &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1449
1450     if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
1451         foreach my $key (keys %confighash) {
1452             my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem ${General::swroot}/ovpn/certs/$confighash{$key}[1]cert.pem`;
1453             if ($test =~ /: OK/) {
1454                 # Delete connection
1455 #               if ($vpnsettings{'ENABLED'} eq 'on' ||
1456 #                   $vpnsettings{'ENABLED_BLUE'} eq 'on') {
1457 #                   system('/usr/local/bin/ipsecctrl', 'D', $key);
1458 #               }
1459                 unlink ("${General::swroot}/ovpn//certs/$confighash{$key}[1]cert.pem");
1460                 unlink ("${General::swroot}/ovpn/certs/$confighash{$key}[1].p12");
1461                 delete $confighash{$key};
1462                 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1463 #               &writeipsecfiles();
1464             }
1465         }
1466         unlink ("${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
1467         delete $cahash{$cgiparams{'KEY'}};
1468         &General::writehasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1469 #       system('/usr/local/bin/ipsecctrl', 'R');
1470     } else {
1471         $errormessage = $Lang::tr{'invalid key'};
1472     }
1473 ###
1474 ### Remove ca certificate (step 1)
1475 ###
1476 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'}) {
1477     &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1478     &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1479
1480     my $assignedcerts = 0;
1481     if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
1482         foreach my $key (keys %confighash) {
1483             my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem ${General::swroot}/ovpn/certs/$confighash{$key}[1]cert.pem`;
1484             if ($test =~ /: OK/) {
1485                 $assignedcerts++;
1486             }
1487         }
1488         if ($assignedcerts) {
1489             &Header::showhttpheaders();
1490             &Header::openpage($Lang::tr{'ovpn'}, 1, '');
1491             &Header::openbigbox('100%', 'LEFT', '', $errormessage);
1492             &Header::openbox('100%', 'LEFT', $Lang::tr{'are you sure'});
1493             print <<END;
1494                 <table><form method='post'><input type='hidden' name='AREUSURE' value='yes' />
1495                        <input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />
1496                     <tr><td align='center'>
1497                         <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>: $assignedcerts
1498                         $Lang::tr{'connections are associated with this ca.  deleting the ca will delete these connections as well.'}
1499                     <tr><td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'remove ca certificate'}' />
1500                         <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></td></tr>
1501                 </form></table>
1502 END
1503             ;
1504             &Header::closebox();
1505             &Header::closebigbox();
1506             &Header::closepage();
1507             exit (0);
1508         } else {
1509             unlink ("${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
1510             delete $cahash{$cgiparams{'KEY'}};
1511             &General::writehasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1512 #           system('/usr/local/bin/ipsecctrl', 'R');
1513         }
1514     } else {
1515         $errormessage = $Lang::tr{'invalid key'};
1516     }
1517
1518 ###
1519 ### Display root certificate
1520 ###
1521 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'} ||
1522     $cgiparams{'ACTION'} eq $Lang::tr{'show host certificate'}) {
1523     my $output;
1524     &Header::showhttpheaders();
1525     &Header::openpage($Lang::tr{'ovpn'}, 1, '');
1526     &Header::openbigbox('100%', 'LEFT', '', '');
1527     if ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'}) {
1528         &Header::openbox('100%', 'LEFT', "$Lang::tr{'root certificate'}:");
1529         $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/ca/cacert.pem`;
1530     } else {
1531         &Header::openbox('100%', 'LEFT', "$Lang::tr{'host certificate'}:");
1532         $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/certs/servercert.pem`;
1533     }
1534     $output = &Header::cleanhtml($output,"y");
1535     print "<pre>$output</pre>\n";
1536     &Header::closebox();
1537     print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1538     &Header::closebigbox();
1539     &Header::closepage();
1540     exit(0);
1541
1542 ###
1543 ### Download root certificate
1544 ###
1545 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download root certificate'}) {
1546     if ( -f "${General::swroot}/ovpn/ca/cacert.pem" ) {
1547         print "Content-Type: application/octet-stream\r\n";
1548         print "Content-Disposition: filename=cacert.pem\r\n\r\n";
1549         print `/usr/bin/openssl x509 -in ${General::swroot}/ovpn/ca/cacert.pem`;
1550         exit(0);
1551     }
1552     
1553 ###
1554 ### Download host certificate
1555 ###
1556 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download host certificate'}) {
1557     if ( -f "${General::swroot}/ovpn/certs/servercert.pem" ) {
1558         print "Content-Type: application/octet-stream\r\n";
1559         print "Content-Disposition: filename=servercert.pem\r\n\r\n";
1560         print `/usr/bin/openssl x509 -in ${General::swroot}/ovpn/certs/servercert.pem`;
1561         exit(0);
1562     }
1563
1564 ###
1565 ### Download tls-auth key
1566 ###
1567 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download tls-auth key'}) {
1568     if ( -f "${General::swroot}/ovpn/certs/ta.key" ) {
1569         print "Content-Type: application/octet-stream\r\n";
1570         print "Content-Disposition: filename=ta.key\r\n\r\n";
1571         print `/bin/cat ${General::swroot}/ovpn/certs/ta.key`;
1572         exit(0);
1573     }
1574
1575 ###
1576 ### Form for generating a root certificate
1577 ###
1578 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'generate root/host certificates'} ||
1579          $cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
1580
1581     &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1582     if (-f "${General::swroot}/ovpn/ca/cacert.pem") {
1583         $errormessage = $Lang::tr{'valid root certificate already exists'};
1584         $cgiparams{'ACTION'} = '';
1585         goto ROOTCERT_ERROR;
1586     }
1587
1588     if (($cgiparams{'ROOTCERT_HOSTNAME'} eq '') && -e "${General::swroot}/red/active") {
1589         if (open(IPADDR, "${General::swroot}/red/local-ipaddress")) {
1590             my $ipaddr = <IPADDR>;
1591             close IPADDR;
1592             chomp ($ipaddr);
1593             $cgiparams{'ROOTCERT_HOSTNAME'} = (gethostbyaddr(pack("C4", split(/\./, $ipaddr)), 2))[0];
1594             if ($cgiparams{'ROOTCERT_HOSTNAME'} eq '') {
1595                 $cgiparams{'ROOTCERT_HOSTNAME'} = $ipaddr;
1596             }
1597         }
1598     } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
1599
1600         if (ref ($cgiparams{'FH'}) ne 'Fh') {
1601             $errormessage = $Lang::tr{'there was no file upload'};
1602             goto ROOTCERT_ERROR;
1603         }
1604
1605         # Move uploaded certificate request to a temporary file
1606         (my $fh, my $filename) = tempfile( );
1607         if (copy ($cgiparams{'FH'}, $fh) != 1) {
1608             $errormessage = $!;
1609             goto ROOTCERT_ERROR;
1610         }
1611
1612         # Create a temporary dirctory
1613         my $tempdir = tempdir( CLEANUP => 1 );
1614
1615         # Extract the CA certificate from the file
1616         my $pid = open(OPENSSL, "|-");
1617         $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1618         if ($pid) {     # parent
1619             if ($cgiparams{'P12_PASS'} ne '') {
1620                 print OPENSSL "$cgiparams{'P12_PASS'}\n";
1621             }
1622             close (OPENSSL);
1623             if ($?) {
1624                 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1625                 unlink ($filename);
1626                 goto ROOTCERT_ERROR;
1627             }
1628         } else {        # child
1629             unless (exec ('/usr/bin/openssl', 'pkcs12', '-cacerts', '-nokeys',
1630                     '-in', $filename,
1631                     '-out', "$tempdir/cacert.pem")) {
1632                 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1633                 unlink ($filename);
1634                 goto ROOTCERT_ERROR;
1635             }
1636         }
1637
1638         # Extract the Host certificate from the file
1639         $pid = open(OPENSSL, "|-");
1640         $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1641         if ($pid) {     # parent
1642             if ($cgiparams{'P12_PASS'} ne '') {
1643                 print OPENSSL "$cgiparams{'P12_PASS'}\n";
1644             }
1645             close (OPENSSL);
1646             if ($?) {
1647                 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1648                 unlink ($filename);
1649                 goto ROOTCERT_ERROR;
1650             }
1651         } else {        # child
1652             unless (exec ('/usr/bin/openssl', 'pkcs12', '-clcerts', '-nokeys',
1653                     '-in', $filename,
1654                     '-out', "$tempdir/hostcert.pem")) {
1655                 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1656                 unlink ($filename);
1657                 goto ROOTCERT_ERROR;
1658             }
1659         }
1660
1661         # Extract the Host key from the file
1662         $pid = open(OPENSSL, "|-");
1663         $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1664         if ($pid) {     # parent
1665             if ($cgiparams{'P12_PASS'} ne '') {
1666                 print OPENSSL "$cgiparams{'P12_PASS'}\n";
1667             }
1668             close (OPENSSL);
1669             if ($?) {
1670                 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1671                 unlink ($filename);
1672                 goto ROOTCERT_ERROR;
1673             }
1674         } else {        # child
1675             unless (exec ('/usr/bin/openssl', 'pkcs12', '-nocerts',
1676                     '-nodes',
1677                     '-in', $filename,
1678                     '-out', "$tempdir/serverkey.pem")) {
1679                 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1680                 unlink ($filename);
1681                 goto ROOTCERT_ERROR;
1682             }
1683         }
1684
1685         move("$tempdir/cacert.pem", "${General::swroot}/ovpn/ca/cacert.pem");
1686         if ($? ne 0) {
1687             $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1688             unlink ($filename);
1689             unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1690             unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1691             unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1692             goto ROOTCERT_ERROR;
1693         }
1694
1695         move("$tempdir/hostcert.pem", "${General::swroot}/ovpn/certs/servercert.pem");
1696         if ($? ne 0) {
1697             $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1698             unlink ($filename);
1699             unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1700             unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1701             unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1702             goto ROOTCERT_ERROR;
1703         }
1704
1705         move("$tempdir/serverkey.pem", "${General::swroot}/ovpn/certs/serverkey.pem");
1706         if ($? ne 0) {
1707             $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1708             unlink ($filename);
1709             unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1710             unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1711             unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1712             goto ROOTCERT_ERROR;
1713         }
1714
1715         goto ROOTCERT_SUCCESS;
1716
1717     } elsif ($cgiparams{'ROOTCERT_COUNTRY'} ne '') {
1718
1719         # Validate input since the form was submitted
1720         if ($cgiparams{'ROOTCERT_ORGANIZATION'} eq ''){
1721             $errormessage = $Lang::tr{'organization cant be empty'};
1722             goto ROOTCERT_ERROR;
1723         }
1724         if (length($cgiparams{'ROOTCERT_ORGANIZATION'}) >60) {
1725             $errormessage = $Lang::tr{'organization too long'};
1726             goto ROOTCERT_ERROR;
1727         }
1728         if ($cgiparams{'ROOTCERT_ORGANIZATION'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1729             $errormessage = $Lang::tr{'invalid input for organization'};
1730             goto ROOTCERT_ERROR;
1731         }
1732         if ($cgiparams{'ROOTCERT_HOSTNAME'} eq ''){
1733             $errormessage = $Lang::tr{'hostname cant be empty'};
1734             goto ROOTCERT_ERROR;
1735         }
1736         unless (&General::validfqdn($cgiparams{'ROOTCERT_HOSTNAME'}) || &General::validip($cgiparams{'ROOTCERT_HOSTNAME'})) {
1737             $errormessage = $Lang::tr{'invalid input for hostname'};
1738             goto ROOTCERT_ERROR;
1739         }
1740         if ($cgiparams{'ROOTCERT_EMAIL'} ne '' && (! &General::validemail($cgiparams{'ROOTCERT_EMAIL'}))) {
1741             $errormessage = $Lang::tr{'invalid input for e-mail address'};
1742             goto ROOTCERT_ERROR;
1743         }
1744         if (length($cgiparams{'ROOTCERT_EMAIL'}) > 40) {
1745             $errormessage = $Lang::tr{'e-mail address too long'};
1746             goto ROOTCERT_ERROR;
1747         }
1748         if ($cgiparams{'ROOTCERT_OU'} ne '' && $cgiparams{'ROOTCERT_OU'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1749             $errormessage = $Lang::tr{'invalid input for department'};
1750             goto ROOTCERT_ERROR;
1751         }
1752         if ($cgiparams{'ROOTCERT_CITY'} ne '' && $cgiparams{'ROOTCERT_CITY'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1753             $errormessage = $Lang::tr{'invalid input for city'};
1754             goto ROOTCERT_ERROR;
1755         }
1756         if ($cgiparams{'ROOTCERT_STATE'} ne '' && $cgiparams{'ROOTCERT_STATE'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1757             $errormessage = $Lang::tr{'invalid input for state or province'};
1758             goto ROOTCERT_ERROR;
1759         }
1760         if ($cgiparams{'ROOTCERT_COUNTRY'} !~ /^[A-Z]*$/) {
1761             $errormessage = $Lang::tr{'invalid input for country'};
1762             goto ROOTCERT_ERROR;
1763         }
1764
1765         # Copy the cgisettings to vpnsettings and save the configfile
1766         $vpnsettings{'ROOTCERT_ORGANIZATION'}   = $cgiparams{'ROOTCERT_ORGANIZATION'};
1767         $vpnsettings{'ROOTCERT_HOSTNAME'}       = $cgiparams{'ROOTCERT_HOSTNAME'};
1768         $vpnsettings{'ROOTCERT_EMAIL'}          = $cgiparams{'ROOTCERT_EMAIL'};
1769         $vpnsettings{'ROOTCERT_OU'}             = $cgiparams{'ROOTCERT_OU'};
1770         $vpnsettings{'ROOTCERT_CITY'}           = $cgiparams{'ROOTCERT_CITY'};
1771         $vpnsettings{'ROOTCERT_STATE'}          = $cgiparams{'ROOTCERT_STATE'};
1772         $vpnsettings{'ROOTCERT_COUNTRY'}        = $cgiparams{'ROOTCERT_COUNTRY'};
1773         &General::writehash("${General::swroot}/ovpn/settings", \%vpnsettings);
1774
1775         # Replace empty strings with a .
1776         (my $ou = $cgiparams{'ROOTCERT_OU'}) =~ s/^\s*$/\./;
1777         (my $city = $cgiparams{'ROOTCERT_CITY'}) =~ s/^\s*$/\./;
1778         (my $state = $cgiparams{'ROOTCERT_STATE'}) =~ s/^\s*$/\./;
1779
1780         # refresh
1781         #system ('/bin/touch', "${General::swroot}/ovpn/gencanow");
1782         
1783         # Create the CA certificate
1784         my $pid = open(OPENSSL, "|-");
1785         $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1786         if ($pid) {     # parent
1787             print OPENSSL "$cgiparams{'ROOTCERT_COUNTRY'}\n";
1788             print OPENSSL "$state\n";
1789             print OPENSSL "$city\n";
1790             print OPENSSL "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
1791             print OPENSSL "$ou\n";
1792             print OPENSSL "$cgiparams{'ROOTCERT_ORGANIZATION'} CA\n";
1793             print OPENSSL "$cgiparams{'ROOTCERT_EMAIL'}\n";
1794             close (OPENSSL);
1795             if ($?) {
1796                 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1797                 unlink ("${General::swroot}/ovpn/ca/cakey.pem");
1798                 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1799                 goto ROOTCERT_ERROR;
1800             }
1801         } else {        # child
1802             unless (exec ('/usr/bin/openssl', 'req', '-x509', '-nodes', '-rand', '/proc/interrupts:/proc/net/rt_cache',
1803                         '-days', '999999', '-newkey', 'rsa:4096', '-sha512',
1804                         '-keyout', "${General::swroot}/ovpn/ca/cakey.pem",
1805                         '-out', "${General::swroot}/ovpn/ca/cacert.pem",
1806                         '-config',"${General::swroot}/ovpn/openssl/ovpn.cnf")) {
1807                 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1808                 goto ROOTCERT_ERROR;
1809             }
1810         }
1811
1812         # Create the Host certificate request
1813         $pid = open(OPENSSL, "|-");
1814         $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1815         if ($pid) {     # parent
1816             print OPENSSL "$cgiparams{'ROOTCERT_COUNTRY'}\n";
1817             print OPENSSL "$state\n";
1818             print OPENSSL "$city\n";
1819             print OPENSSL "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
1820             print OPENSSL "$ou\n";
1821             print OPENSSL "$cgiparams{'ROOTCERT_HOSTNAME'}\n";
1822             print OPENSSL "$cgiparams{'ROOTCERT_EMAIL'}\n";
1823             print OPENSSL ".\n";
1824             print OPENSSL ".\n";
1825             close (OPENSSL);
1826             if ($?) {
1827                 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1828                 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1829                 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
1830                 goto ROOTCERT_ERROR;
1831             }
1832         } else {        # child
1833             unless (exec ('/usr/bin/openssl', 'req', '-nodes', '-rand', '/proc/interrupts:/proc/net/rt_cache',
1834                         '-newkey', 'rsa:2048',
1835                         '-keyout', "${General::swroot}/ovpn/certs/serverkey.pem",
1836                         '-out', "${General::swroot}/ovpn/certs/serverreq.pem",
1837                         '-extensions', 'server',
1838                         '-config', "${General::swroot}/ovpn/openssl/ovpn.cnf" )) {
1839                 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1840                 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1841                 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
1842                 unlink ("${General::swroot}/ovpn/ca/cakey.pem");
1843                 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1844                 goto ROOTCERT_ERROR;
1845             }
1846         }
1847         
1848         # Sign the host certificate request
1849         system('/usr/bin/openssl', 'ca', '-days', '999999',
1850                 '-batch', '-notext',
1851                 '-in',  "${General::swroot}/ovpn/certs/serverreq.pem",
1852                 '-out', "${General::swroot}/ovpn/certs/servercert.pem",
1853                 '-extensions', 'server',
1854                 '-config', "${General::swroot}/ovpn/openssl/ovpn.cnf");
1855         if ($?) {
1856             $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1857             unlink ("${General::swroot}/ovpn/ca/cakey.pem");
1858             unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1859             unlink ("${General::swroot}/ovpn/serverkey.pem");
1860             unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
1861             unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1862             &newcleanssldatabase();
1863             goto ROOTCERT_ERROR;
1864         } else {
1865             unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
1866             &deletebackupcert();
1867         }
1868
1869         # Create an empty CRL
1870         system('/usr/bin/openssl', 'ca', '-gencrl',
1871                 '-out', "${General::swroot}/ovpn/crls/cacrl.pem",
1872                 '-config', "${General::swroot}/ovpn/openssl/ovpn.cnf" );
1873         if ($?) {
1874             $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1875             unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1876             unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1877             unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1878             unlink ("${General::swroot}/ovpn/crls/cacrl.pem");      
1879             &cleanssldatabase();
1880             goto ROOTCERT_ERROR;
1881 #       } else {
1882 #           &cleanssldatabase();
1883         }
1884         # Create Diffie Hellmann Parameter
1885         system('/usr/bin/openssl', 'dhparam', '-rand', '/proc/interrupts:/proc/net/rt_cache',
1886                '-out', "${General::swroot}/ovpn/ca/dh1024.pem", "$cgiparams{'DHLENGHT'}");
1887         if ($?) {
1888             $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1889             unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1890             unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1891             unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1892             unlink ("${General::swroot}/ovpn/crls/cacrl.pem");
1893             unlink ("${General::swroot}/ovpn/ca/dh1024.pem");
1894             &cleanssldatabase();
1895             goto ROOTCERT_ERROR;
1896 #       } else {
1897 #           &cleanssldatabase();
1898         }
1899         # Create ta.key for tls-auth
1900         system('/usr/sbin/openvpn', '--genkey', '--secret', "${General::swroot}/ovpn/certs/ta.key");
1901         if ($?) {
1902             $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1903             &cleanssldatabase();
1904             goto ROOTCERT_ERROR;
1905         }
1906         goto ROOTCERT_SUCCESS;
1907     }
1908     ROOTCERT_ERROR:
1909     if ($cgiparams{'ACTION'} ne '') {
1910         &Header::showhttpheaders();
1911         &Header::openpage($Lang::tr{'ovpn'}, 1, '');
1912         &Header::openbigbox('100%', 'LEFT', '', '');
1913         if ($errormessage) {
1914             &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
1915             print "<class name='base'>$errormessage";
1916             print "&nbsp;</class>";
1917             &Header::closebox();
1918         }
1919         &Header::openbox('100%', 'LEFT', "$Lang::tr{'generate root/host certificates'}:");
1920         print <<END;
1921         <form method='post' enctype='multipart/form-data'>
1922         <table width='100%' border='0' cellspacing='1' cellpadding='0'>
1923         <tr><td width='30%' class='base'>$Lang::tr{'organization name'}:</td>
1924             <td width='35%' class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_ORGANIZATION' value='$cgiparams{'ROOTCERT_ORGANIZATION'}' size='32' /></td>
1925             <td width='35%' colspan='2'>&nbsp;</td></tr>
1926         <tr><td class='base'>$Lang::tr{'ipfires hostname'}:</td>
1927             <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_HOSTNAME' value='$cgiparams{'ROOTCERT_HOSTNAME'}' size='32' /></td>
1928             <td colspan='2'>&nbsp;</td></tr>
1929         <tr><td class='base'>$Lang::tr{'your e-mail'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
1930             <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_EMAIL' value='$cgiparams{'ROOTCERT_EMAIL'}' size='32' /></td>
1931             <td colspan='2'>&nbsp;</td></tr>
1932         <tr><td class='base'>$Lang::tr{'your department'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
1933             <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_OU' value='$cgiparams{'ROOTCERT_OU'}' size='32' /></td>
1934             <td colspan='2'>&nbsp;</td></tr>
1935         <tr><td class='base'>$Lang::tr{'city'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
1936             <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_CITY' value='$cgiparams{'ROOTCERT_CITY'}' size='32' /></td>
1937             <td colspan='2'>&nbsp;</td></tr>
1938         <tr><td class='base'>$Lang::tr{'state or province'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
1939             <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_STATE' value='$cgiparams{'ROOTCERT_STATE'}' size='32' /></td>
1940             <td colspan='2'>&nbsp;</td></tr>
1941         <tr><td class='base'>$Lang::tr{'country'}:</td>
1942             <td class='base'><select name='ROOTCERT_COUNTRY'> 
1943
1944 END
1945         ;
1946         foreach my $country (sort keys %{Countries::countries}) {
1947             print "<option value='$Countries::countries{$country}'";
1948             if ( $Countries::countries{$country} eq $cgiparams{'ROOTCERT_COUNTRY'} ) {
1949                 print " selected='selected'";
1950             }
1951             print ">$country</option>";
1952         }
1953         print <<END;
1954             </select></td>
1955         <tr><td class='base'>$Lang::tr{'ovpn dh'}:</td>
1956                 <td class='base'><select name='DHLENGHT'>
1957                                 <option value='1024' $selected{'DHLENGHT'}{'1024'}>1024 $Lang::tr{'bit'}</option>
1958                                 <option value='2048' $selected{'DHLENGHT'}{'2048'}>2048 $Lang::tr{'bit'}</option>
1959                                 <option value='3072' $selected{'DHLENGHT'}{'3072'}>3072 $Lang::tr{'bit'}</option>
1960                                 <option value='4096' $selected{'DHLENGHT'}{'4096'}>4096 $Lang::tr{'bit'}</option>
1961                         </select>
1962                 </td>
1963         </tr>
1964
1965         <tr><td>&nbsp;</td>
1966             <td><input type='submit' name='ACTION' value='$Lang::tr{'generate root/host certificates'}' /></td>
1967             <td>&nbsp;</td><td>&nbsp;</td></tr> 
1968         <tr><td class='base' colspan='4' align='left'>
1969             <img src='/blob.gif' valign='top' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td></tr>
1970         <tr><td colspan='2'><br></td></tr>
1971         <table width='100%'>
1972         <tr>
1973                 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}: </font></b>$Lang::tr{'ovpn generating the root and host certificates'}
1974                 <td class='base'>$Lang::tr{'dh key warn'}</td>
1975         </tr>
1976         <tr>
1977                 <td class='base'>$Lang::tr{'dh key warn1'}</td>
1978         </tr>
1979         <tr><td colspan='2'><br></td></tr>
1980         <tr>
1981         </table>
1982
1983         <table width='100%'>
1984         <tr><td colspan='4'><hr></td></tr>
1985         <tr><td class='base' nowrap='nowrap'>$Lang::tr{'upload p12 file'}:</td>
1986             <td nowrap='nowrap'><input type='file' name='FH' size='32'></td>
1987             <td colspan='2'>&nbsp;</td></tr>
1988         <tr><td class='base'>$Lang::tr{'pkcs12 file password'}:&nbsp;<img src='/blob.gif' alt='*' ></td>
1989             <td class='base' nowrap='nowrap'><input type='password' name='P12_PASS' value='$cgiparams{'P12_PASS'}' size='32' /></td>
1990             <td colspan='2'>&nbsp;</td></tr>
1991         <tr><td>&nbsp;</td>
1992             <td><input type='submit' name='ACTION' value='$Lang::tr{'upload p12 file'}' /></td>
1993             <td colspan='2'>&nbsp;</td></tr>
1994         <tr><td class='base' colspan='4' align='left'>
1995             <img src='/blob.gif' valign='top' al='*' >&nbsp;$Lang::tr{'this field may be blank'}</td>
1996         </tr>
1997         </form></table>
1998 END
1999         ;
2000         &Header::closebox();
2001         print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2002         &Header::closebigbox();
2003         &Header::closepage();
2004         exit(0)
2005     }
2006
2007     ROOTCERT_SUCCESS:
2008     system ("chmod 600 ${General::swroot}/ovpn/certs/serverkey.pem");
2009 #    if ($vpnsettings{'ENABLED'} eq 'on' ||
2010 #       $vpnsettings{'ENABLE_BLUE'} eq 'on') {
2011 #       system('/usr/local/bin/ipsecctrl', 'S');
2012 #    }
2013
2014 ###
2015 ### Enable/Disable connection
2016 ###
2017
2018 ###
2019 # m.a.d net2net
2020 ###
2021
2022 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
2023     
2024     &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
2025     &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2026 #    my $n2nactive = '';
2027     my $n2nactive = `/bin/ps ax|grep $confighash{$cgiparams{'KEY'}}[1]|grep -v grep|awk \'{print \$1}\'`;
2028     
2029     if ($confighash{$cgiparams{'KEY'}}) {
2030                 if ($confighash{$cgiparams{'KEY'}}[0] eq 'off') {
2031                         $confighash{$cgiparams{'KEY'}}[0] = 'on';
2032                         &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2033
2034                         if ($confighash{$cgiparams{'KEY'}}[3] eq 'net'){
2035                  system('/usr/local/bin/openvpnctrl', '-sn2n', $confighash{$cgiparams{'KEY'}}[1]);
2036                         }
2037                 } else {
2038
2039                         $confighash{$cgiparams{'KEY'}}[0] = 'off';
2040                         &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2041
2042                         if ($confighash{$cgiparams{'KEY'}}[3] eq 'net'){
2043                     if ($n2nactive ne ''){                              
2044                                                 system('/usr/local/bin/openvpnctrl', '-kn2n', $confighash{$cgiparams{'KEY'}}[1]);
2045                                         }
2046  
2047                         } else {
2048                   $errormessage = $Lang::tr{'invalid key'};
2049                         }
2050       }
2051   }
2052
2053 ###
2054 ### Download OpenVPN client package
2055 ###
2056
2057
2058 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'dl client arch'}) {
2059     &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
2060     &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2061     my $file = '';
2062     my $clientovpn = '';
2063     my @fileholder;
2064     my $tempdir = tempdir( CLEANUP => 1 );
2065     my $zippath = "$tempdir/";
2066
2067 ###
2068 # m.a.d net2net
2069 ###
2070
2071 if ($confighash{$cgiparams{'KEY'}}[3] eq 'net'){
2072         
2073         my $zipname = "$confighash{$cgiparams{'KEY'}}[1]-Client.zip";
2074         my $zippathname = "$zippath$zipname";
2075         $clientovpn = "$confighash{$cgiparams{'KEY'}}[1].conf";  
2076         my @ovsubnettemp =  split(/\./,$confighash{$cgiparams{'KEY'}}[27]);
2077         my $ovsubnet =  "$ovsubnettemp[0].$ovsubnettemp[1].$ovsubnettemp[2]";
2078         my $tunmtu = ''; 
2079         my @remsubnet = split(/\//,$confighash{$cgiparams{'KEY'}}[8]);
2080         my $n2nfragment = '';
2081         
2082     open(CLIENTCONF, ">$tempdir/$clientovpn") or die "Unable to open tempfile: $!";
2083     flock CLIENTCONF, 2;
2084     
2085     my $zip = Archive::Zip->new();
2086    print CLIENTCONF "# IPFire n2n Open VPN Client Config by ummeegge und m.a.d\n";
2087    print CLIENTCONF "# \n";
2088    print CLIENTCONF "# User Security\n";
2089    print CLIENTCONF "user nobody\n";
2090    print CLIENTCONF "group nobody\n";
2091    print CLIENTCONF "persist-tun\n";
2092    print CLIENTCONF "persist-key\n";
2093    print CLIENTCONF "script-security 2\n";
2094    print CLIENTCONF "# IP/DNS for remote Server Gateway\n"; 
2095    print CLIENTCONF "remote $vpnsettings{'VPN_IP'}\n";
2096    print CLIENTCONF "float\n";
2097    print CLIENTCONF "# IP adresses of the VPN Subnet\n"; 
2098    print CLIENTCONF "ifconfig $ovsubnet.2 $ovsubnet.1\n"; 
2099    print CLIENTCONF "# Server Gateway Network\n"; 
2100    print CLIENTCONF "route $remsubnet[0] $remsubnet[1]\n";
2101    print CLIENTCONF "# tun Device\n"; 
2102    print CLIENTCONF "dev $vpnsettings{'DDEVICE'}\n"; 
2103    print CLIENTCONF "# Port and Protokoll\n"; 
2104    print CLIENTCONF "port $confighash{$cgiparams{'KEY'}}[29]\n"; 
2105    
2106    if ($confighash{$cgiparams{'KEY'}}[28] eq 'tcp') {
2107    print CLIENTCONF "proto tcp-client\n";
2108    print CLIENTCONF "# Packet size\n";
2109    if ($confighash{$cgiparams{'KEY'}}[31] eq '') {$tunmtu = '1400'} else {$tunmtu = $confighash{$cgiparams{'KEY'}}[31]};
2110    print CLIENTCONF "tun-mtu $tunmtu\n";
2111    }
2112   
2113    if ($confighash{$cgiparams{'KEY'}}[28] eq 'udp') {
2114    print CLIENTCONF "proto udp\n"; 
2115    print CLIENTCONF "# Paketsize\n";
2116    if ($confighash{$cgiparams{'KEY'}}[31] eq '') {$tunmtu = '1500'} else {$tunmtu = $confighash{$cgiparams{'KEY'}}[31]};
2117    print CLIENTCONF "tun-mtu $tunmtu\n";
2118    if ($confighash{$cgiparams{'KEY'}}[24] ne '') {print CLIENTCONF "fragment $confighash{$cgiparams{'KEY'}}[24]\n";}
2119    if ($confighash{$cgiparams{'KEY'}}[23] eq 'on') {print CLIENTCONF "mssfix\n";}
2120    }
2121    if (($confighash{$cgiparams{'KEY'}}[38] eq 'yes') ||
2122        ($confighash{$cgiparams{'KEY'}}[38] eq 'maybe') ||
2123        ($confighash{$cgiparams{'KEY'}}[38] eq 'no' )) {
2124         if (($confighash{$cgiparams{'KEY'}}[23] ne 'on') || ($confighash{$cgiparams{'KEY'}}[24] eq '')) {
2125                 if ($tunmtu eq '1500' ) {
2126                         print CLIENTCONF "mtu-disc $confighash{$cgiparams{'KEY'}}[38]\n";
2127                 }
2128         }
2129    }
2130    print CLIENTCONF "ns-cert-type server\n";   
2131    print CLIENTCONF "# Auth. Client\n"; 
2132    print CLIENTCONF "tls-client\n"; 
2133    print CLIENTCONF "# Cipher\n";
2134    print CLIENTCONF "cipher $confighash{$cgiparams{'KEY'}}[40]\n";
2135     if ($confighash{$cgiparams{'KEY'}}[4] eq 'cert' && -f "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12") { 
2136          print CLIENTCONF "pkcs12 ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12\r\n";
2137      $zip->addFile( "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12", "$confighash{$cgiparams{'KEY'}}[1].p12") or die "Can't add file $confighash{$cgiparams{'KEY'}}[1].p12\n";
2138    }
2139    if ($confighash{$cgiparams{'KEY'}}[39] eq '') {
2140         print CLIENTCONF "# HMAC algorithm\n";
2141         print CLIENTCONF "auth SHA1\n";
2142    } else {
2143    print CLIENTCONF "# HMAC algorithm\n";
2144    print CLIENTCONF "auth $confighash{$cgiparams{'KEY'}}[39]\n";
2145    }
2146    if ($confighash{$cgiparams{'KEY'}}[30] eq 'on') {
2147    print CLIENTCONF "# Enable Compression\n";
2148    print CLIENTCONF "comp-lzo\n";
2149      }
2150    print CLIENTCONF "# Debug Level\n"; 
2151    print CLIENTCONF "verb 3\n"; 
2152    print CLIENTCONF "# Tunnel check\n"; 
2153    print CLIENTCONF "keepalive 10 60\n"; 
2154    print CLIENTCONF "# Start as daemon\n"; 
2155    print CLIENTCONF "daemon $confighash{$cgiparams{'KEY'}}[1]n2n\n"; 
2156    print CLIENTCONF "writepid /var/run/$confighash{$cgiparams{'KEY'}}[1]n2n.pid\n"; 
2157    print CLIENTCONF "# Activate Management Interface and Port\n"; 
2158    if ($confighash{$cgiparams{'KEY'}}[22] eq '') {print CLIENTCONF "management localhost $confighash{$cgiparams{'KEY'}}[29]\n"}
2159     else {print CLIENTCONF "management localhost $confighash{$cgiparams{'KEY'}}[22]\n"};
2160    print CLIENTCONF "# remsub $confighash{$cgiparams{'KEY'}}[11]\n";
2161   
2162
2163     close(CLIENTCONF);
2164         
2165     $zip->addFile( "$tempdir/$clientovpn", $clientovpn) or die "Can't add file $clientovpn\n";
2166     my $status = $zip->writeToFileNamed($zippathname);
2167
2168     open(DLFILE, "<$zippathname") or die "Unable to open $zippathname: $!";
2169     @fileholder = <DLFILE>;
2170     print "Content-Type:application/x-download\n";
2171     print "Content-Disposition:attachment;filename=$zipname\n\n";
2172     print @fileholder;
2173     exit (0);
2174 }
2175 else
2176 {
2177         my $zipname = "$confighash{$cgiparams{'KEY'}}[1]-TO-IPFire.zip";
2178         my $zippathname = "$zippath$zipname";
2179         $clientovpn = "$confighash{$cgiparams{'KEY'}}[1]-TO-IPFire.ovpn";
2180
2181 ###
2182 # m.a.d net2net
2183 ###
2184   
2185     open(CLIENTCONF, ">$tempdir/$clientovpn") or die "Unable to open tempfile: $!";
2186     flock CLIENTCONF, 2;
2187     
2188     my $zip = Archive::Zip->new();
2189     
2190     print CLIENTCONF "#OpenVPN Client conf\r\n";
2191     print CLIENTCONF "tls-client\r\n";
2192     print CLIENTCONF "client\r\n";
2193     print CLIENTCONF "nobind\r\n";
2194     print CLIENTCONF "dev $vpnsettings{'DDEVICE'}\r\n";
2195     print CLIENTCONF "proto $vpnsettings{'DPROTOCOL'}\r\n";
2196
2197     # Check if we are using fragment, mssfix or mtu-disc and set MTU to 1500
2198     # or use configured value.
2199     if ($vpnsettings{FRAGMENT} ne '' && $vpnsettings{DPROTOCOL} ne 'tcp' )
2200         { print CLIENTCONF "$vpnsettings{'DDEVICE'}-mtu 1500\r\n"; }
2201     elsif ($vpnsettings{MSSFIX} eq 'on')
2202         { print CLIENTCONF "$vpnsettings{'DDEVICE'}-mtu 1500\r\n"; }
2203     elsif (($vpnsettings{'PMTU_DISCOVERY'} eq 'yes') ||
2204            ($vpnsettings{'PMTU_DISCOVERY'} eq 'maybe') ||
2205            ($vpnsettings{'PMTU_DISCOVERY'} eq 'no' )) 
2206         { print CLIENTCONF "$vpnsettings{'DDEVICE'}-mtu 1500\r\n"; }
2207     else
2208         { print CLIENTCONF "$vpnsettings{'DDEVICE'}-mtu $vpnsettings{'DMTU'}\r\n"; }
2209
2210     if ( $vpnsettings{'ENABLED'} eq 'on'){
2211         print CLIENTCONF "remote $vpnsettings{'VPN_IP'} $vpnsettings{'DDEST_PORT'}\r\n";
2212         if ( $vpnsettings{'ENABLED_BLUE'} eq 'on' && (&haveBlueNet())){ 
2213             print CLIENTCONF "#Coment the above line and uncoment the next line, if you want to connect on the Blue interface\r\n";     
2214             print CLIENTCONF ";remote $netsettings{'BLUE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2215         }
2216         if ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' && (&haveOrangeNet())){
2217             print CLIENTCONF "#Coment the above line and uncoment the next line, if you want to connect on the Orange interface\r\n";           
2218             print CLIENTCONF ";remote $netsettings{'ORANGE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2219         }
2220     } elsif ( $vpnsettings{'ENABLED_BLUE'} eq 'on' && (&haveBlueNet())){
2221         print CLIENTCONF "remote $netsettings{'BLUE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2222         if ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' && (&haveOrangeNet())){
2223             print CLIENTCONF "#Coment the above line and uncoment the next line, if you want to connect on the Orange interface\r\n";           
2224             print CLIENTCONF ";remote $netsettings{'ORANGE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2225         }
2226     } elsif ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' && (&haveOrangeNet())){
2227         print CLIENTCONF "remote $netsettings{'ORANGE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2228     }
2229                         
2230     if ($confighash{$cgiparams{'KEY'}}[4] eq 'cert' && -f "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12") { 
2231         print CLIENTCONF "pkcs12 $confighash{$cgiparams{'KEY'}}[1].p12\r\n";
2232         $zip->addFile( "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12", "$confighash{$cgiparams{'KEY'}}[1].p12") or die "Can't add file $confighash{$cgiparams{'KEY'}}[1].p12\n";
2233     } else {
2234         print CLIENTCONF "ca cacert.pem\r\n";
2235         print CLIENTCONF "cert $confighash{$cgiparams{'KEY'}}[1]cert.pem\r\n";
2236         print CLIENTCONF "key $confighash{$cgiparams{'KEY'}}[1].key\r\n";
2237         $zip->addFile( "${General::swroot}/ovpn/ca/cacert.pem", "cacert.pem")  or die "Can't add file cacert.pem\n";
2238         $zip->addFile( "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem", "$confighash{$cgiparams{'KEY'}}[1]cert.pem") or die "Can't add file $confighash{$cgiparams{'KEY'}}[1]cert.pem\n";    
2239     }
2240     print CLIENTCONF "cipher $vpnsettings{DCIPHER}\r\n";
2241     if ($vpnsettings{'DAUTH'} eq '') {
2242         print CLIENTCONF "";
2243     } else {
2244         print CLIENTCONF "auth $vpnsettings{'DAUTH'}\r\n";
2245     }
2246     if ($vpnsettings{'TLSAUTH'} eq 'on') {
2247         print CLIENTCONF "tls-auth ta.key\r\n";
2248         $zip->addFile( "${General::swroot}/ovpn/certs/ta.key", "ta.key")  or die "Can't add file ta.key\n";
2249     }
2250     if ($vpnsettings{DCOMPLZO} eq 'on') {
2251         print CLIENTCONF "comp-lzo\r\n";
2252     }
2253     print CLIENTCONF "verb 3\r\n";
2254     print CLIENTCONF "ns-cert-type server\r\n";
2255     print CLIENTCONF "tls-remote $vpnsettings{ROOTCERT_HOSTNAME}\r\n"; 
2256     if ($vpnsettings{MSSFIX} eq 'on') {
2257         print CLIENTCONF "mssfix\r\n";
2258     }
2259     if ($vpnsettings{FRAGMENT} ne '' && $vpnsettings{DPROTOCOL} ne 'tcp' ) {
2260         print CLIENTCONF "fragment $vpnsettings{'FRAGMENT'}\r\n";
2261     }
2262
2263     # Check if a valid operating mode has been choosen and use it.
2264     if (($vpnsettings{'PMTU_DISCOVERY'} eq 'yes') ||
2265         ($vpnsettings{'PMTU_DISCOVERY'} eq 'maybe') ||
2266         ($vpnsettings{'PMTU_DISCOVERY'} eq 'no' )) {
2267         if(($vpnsettings{MSSFIX} ne 'on') || ($vpnsettings{FRAGMENT} eq '')) {
2268                 print CLIENTCONF "mtu-disc $vpnsettings{'PMTU_DISCOVERY'}\r\n";
2269         }
2270     }
2271     # Print client.conf.local if entries exist to client.ovpn
2272     if (!-z $local_clientconf && $vpnsettings{'ADDITIONAL_CONFIGS'} eq 'on') {
2273        open (LCC, "$local_clientconf");
2274                print CLIENTCONF "\n#---------------------------\n";
2275                print CLIENTCONF "# Start of custom directives\n";
2276                print CLIENTCONF "# from client.conf.local\n";
2277                print CLIENTCONF "#---------------------------\n\n";
2278        while (<LCC>) {
2279                print CLIENTCONF $_;
2280        }
2281                print CLIENTCONF "\n#---------------------------\n";
2282                print CLIENTCONF "# End of custom directives\n";
2283                print CLIENTCONF "#---------------------------\n\n";
2284        close (LCC);
2285     }
2286     close(CLIENTCONF);
2287         
2288     $zip->addFile( "$tempdir/$clientovpn", $clientovpn) or die "Can't add file $clientovpn\n";
2289     my $status = $zip->writeToFileNamed($zippathname);
2290
2291     open(DLFILE, "<$zippathname") or die "Unable to open $zippathname: $!";
2292     @fileholder = <DLFILE>;
2293     print "Content-Type:application/x-download\n";
2294     print "Content-Disposition:attachment;filename=$zipname\n\n";
2295     print @fileholder;
2296     exit (0);
2297    }
2298    
2299    
2300    
2301 ###
2302 ### Remove connection
2303 ###
2304
2305
2306 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove'}) {
2307     &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
2308     &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2309
2310     if ($confighash{$cgiparams{'KEY'}}) {
2311 #       if ($vpnsettings{'ENABLED'} eq 'on' ||
2312 #           $vpnsettings{'ENABLED_BLUE'} eq 'on') {
2313 #           system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'});
2314 #       }
2315 #
2316         my $temp = `/usr/bin/openssl ca -revoke ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem -config ${General::swroot}/ovpn/openssl/ovpn.cnf`;
2317
2318 ###
2319 # m.a.d net2net
2320 ###
2321
2322 if ($confighash{$cgiparams{'KEY'}}[3] eq 'net') {
2323         my $conffile = glob("${General::swroot}/ovpn/n2nconf/$confighash{$cgiparams{'KEY'}}[1]/$confighash{$cgiparams{'KEY'}}[1].conf");
2324         my $certfile = glob("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12");
2325         unlink ($certfile);
2326         unlink ($conffile);
2327
2328         if (-e "${General::swroot}/ovpn/n2nconf/$confighash{$cgiparams{'KEY'}}[1]") {
2329                 rmdir ("${General::swroot}/ovpn/n2nconf/$confighash{$cgiparams{'KEY'}}[1]") || die "Kann Verzeichnis nicht loeschen: $!";
2330         }
2331 }
2332
2333   unlink ("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem");
2334   unlink ("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12");
2335
2336 # A.Marx CCD delete ccd files and routes
2337
2338         
2339         if (-f "${General::swroot}/ovpn/ccd/$confighash{$cgiparams{'KEY'}}[2]")
2340         {
2341                 unlink "${General::swroot}/ovpn/ccd/$confighash{$cgiparams{'KEY'}}[2]";
2342         }
2343         
2344         &General::readhasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
2345         foreach my $key (keys %ccdroutehash) {
2346                 if ($ccdroutehash{$key}[0] eq $confighash{$cgiparams{'KEY'}}[1]){
2347                         delete $ccdroutehash{$key};
2348                 }
2349         }
2350         &General::writehasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
2351         
2352         &General::readhasharray("${General::swroot}/ovpn/ccdroute2", \%ccdroute2hash);
2353         foreach my $key (keys %ccdroute2hash) {
2354                 if ($ccdroute2hash{$key}[0] eq $confighash{$cgiparams{'KEY'}}[1]){
2355                         delete $ccdroute2hash{$key};
2356                 }
2357         }
2358         &General::writehasharray("${General::swroot}/ovpn/ccdroute2", \%ccdroute2hash);
2359         &writeserverconf;
2360         
2361         
2362 # CCD end 
2363
2364         
2365         delete $confighash{$cgiparams{'KEY'}};
2366         my $temp2 = `/usr/bin/openssl ca -gencrl -out ${General::swroot}/ovpn/crls/cacrl.pem -config ${General::swroot}/ovpn/openssl/ovpn.cnf`;
2367         &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2368
2369         #&writeserverconf();
2370     } else {
2371         $errormessage = $Lang::tr{'invalid key'};
2372     }
2373         &General::firewall_reload();
2374
2375 ###
2376 ### Download PKCS12 file
2377 ###
2378 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download pkcs12 file'}) {
2379     &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2380
2381     print "Content-Disposition: filename=" . $confighash{$cgiparams{'KEY'}}[1] . ".p12\r\n";
2382     print "Content-Type: application/octet-stream\r\n\r\n";
2383     print `/bin/cat ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12`;
2384     exit (0);
2385
2386 ###
2387 ### Display certificate
2388 ###
2389 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show certificate'}) {
2390     &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2391
2392     if ( -f "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem") {
2393         &Header::showhttpheaders();
2394         &Header::openpage($Lang::tr{'ovpn'}, 1, '');
2395         &Header::openbigbox('100%', 'LEFT', '', '');
2396         &Header::openbox('100%', 'LEFT', "$Lang::tr{'certificate'}:");
2397         my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
2398         $output = &Header::cleanhtml($output,"y");
2399         print "<pre>$output</pre>\n";
2400         &Header::closebox();
2401         print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2402         &Header::closebigbox();
2403         &Header::closepage();
2404         exit(0);
2405     }
2406
2407 ###
2408 ### Display Diffie-Hellman key
2409 ###
2410 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show dh'}) {
2411
2412     if (! -e "${General::swroot}/ovpn/ca/dh1024.pem") {
2413         $errormessage = $Lang::tr{'not present'};
2414         } else {
2415                 &Header::showhttpheaders();
2416                 &Header::openpage($Lang::tr{'ovpn'}, 1, '');
2417                 &Header::openbigbox('100%', 'LEFT', '', '');
2418                 &Header::openbox('100%', 'LEFT', "$Lang::tr{'dh'}:");
2419                 my $output = `/usr/bin/openssl dhparam -text -in ${General::swroot}/ovpn/ca/dh1024.pem`;
2420                 $output = &Header::cleanhtml($output,"y");
2421                 print "<pre>$output</pre>\n";
2422                 &Header::closebox();
2423                 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2424                 &Header::closebigbox();
2425                 &Header::closepage();
2426                 exit(0);
2427     }
2428
2429 ###
2430 ### Display tls-auth key
2431 ###
2432 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show tls-auth key'}) {
2433
2434     if (! -e "${General::swroot}/ovpn/certs/ta.key") {
2435         $errormessage = $Lang::tr{'not present'};
2436         } else {
2437                 &Header::showhttpheaders();
2438                 &Header::openpage($Lang::tr{'ovpn'}, 1, '');
2439                 &Header::openbigbox('100%', 'LEFT', '', '');
2440                 &Header::openbox('100%', 'LEFT', "$Lang::tr{'ta key'}:");
2441                 my $output = `/bin/cat ${General::swroot}/ovpn/certs/ta.key`;
2442                 $output = &Header::cleanhtml($output,"y");
2443                 print "<pre>$output</pre>\n";
2444                 &Header::closebox();
2445                 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2446                 &Header::closebigbox();
2447                 &Header::closepage();
2448                 exit(0);
2449     }
2450
2451 ###
2452 ### Display Certificate Revoke List
2453 ###
2454 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show crl'}) {
2455 #    &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2456
2457     if (! -e "${General::swroot}/ovpn/crls/cacrl.pem") {
2458         $errormessage = $Lang::tr{'not present'};
2459         } else {
2460         &Header::showhttpheaders();
2461         &Header::openpage($Lang::tr{'ovpn'}, 1, '');
2462         &Header::openbigbox('100%', 'LEFT', '', '');
2463         &Header::openbox('100%', 'LEFT', "$Lang::tr{'crl'}:");
2464         my $output = `/usr/bin/openssl crl -text -noout -in ${General::swroot}/ovpn/crls/cacrl.pem`;
2465         $output = &Header::cleanhtml($output,"y");
2466         print "<pre>$output</pre>\n";
2467         &Header::closebox();
2468         print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2469         &Header::closebigbox();
2470         &Header::closepage();
2471         exit(0);
2472     }
2473
2474 ###
2475 ### Advanced Server Settings
2476 ###
2477
2478 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'advanced server'}) {
2479     %cgiparams = ();
2480     %cahash = ();
2481     %confighash = ();
2482     my $disabled;
2483     &General::readhash("${General::swroot}/ovpn/settings", \%cgiparams);
2484     read_routepushfile;
2485         
2486         
2487 #    if ($cgiparams{'CLIENT2CLIENT'} eq '') {
2488 #       $cgiparams{'CLIENT2CLIENT'} =  'on';     
2489 #    }
2490 ADV_ERROR:
2491     if ($cgiparams{'MAX_CLIENTS'} eq '') {
2492                 $cgiparams{'MAX_CLIENTS'} =  '100';
2493     }
2494     if ($cgiparams{'KEEPALIVE_1'} eq '') {
2495                 $cgiparams{'KEEPALIVE_1'} =  '10';
2496     }
2497     if ($cgiparams{'KEEPALIVE_2'} eq '') {
2498                 $cgiparams{'KEEPALIVE_2'} =  '60';
2499     }
2500     if ($cgiparams{'LOG_VERB'} eq '') {
2501                 $cgiparams{'LOG_VERB'} =  '3';
2502     }
2503     if ($cgiparams{'PMTU_DISCOVERY'} eq '') {
2504                 $cgiparams{'PMTU_DISCOVERY'} = 'off';
2505     }
2506     if ($cgiparams{'DAUTH'} eq '') {
2507                 $cgiparams{'DAUTH'} = 'SHA1';
2508     }
2509     if ($cgiparams{'TLSAUTH'} eq '') {
2510                 $cgiparams{'TLSAUTH'} = 'off';
2511     }
2512     $checked{'CLIENT2CLIENT'}{'off'} = '';
2513     $checked{'CLIENT2CLIENT'}{'on'} = '';
2514     $checked{'CLIENT2CLIENT'}{$cgiparams{'CLIENT2CLIENT'}} = 'CHECKED';
2515     $checked{'REDIRECT_GW_DEF1'}{'off'} = '';
2516     $checked{'REDIRECT_GW_DEF1'}{'on'} = '';
2517     $checked{'REDIRECT_GW_DEF1'}{$cgiparams{'REDIRECT_GW_DEF1'}} = 'CHECKED';
2518     $checked{'ADDITIONAL_CONFIGS'}{'off'} = '';
2519     $checked{'ADDITIONAL_CONFIGS'}{'on'} = '';
2520     $checked{'ADDITIONAL_CONFIGS'}{$cgiparams{'ADDITIONAL_CONFIGS'}} = 'CHECKED';
2521     $checked{'MSSFIX'}{'off'} = '';
2522     $checked{'MSSFIX'}{'on'} = '';
2523     $checked{'MSSFIX'}{$cgiparams{'MSSFIX'}} = 'CHECKED';
2524     $checked{'PMTU_DISCOVERY'}{$cgiparams{'PMTU_DISCOVERY'}} = 'checked=\'checked\'';
2525     $selected{'LOG_VERB'}{'0'} = '';
2526     $selected{'LOG_VERB'}{'1'} = '';
2527     $selected{'LOG_VERB'}{'2'} = '';
2528     $selected{'LOG_VERB'}{'3'} = '';
2529     $selected{'LOG_VERB'}{'4'} = '';
2530     $selected{'LOG_VERB'}{'5'} = '';
2531     $selected{'LOG_VERB'}{'6'} = '';
2532     $selected{'LOG_VERB'}{'7'} = '';
2533     $selected{'LOG_VERB'}{'8'} = '';
2534     $selected{'LOG_VERB'}{'9'} = '';
2535     $selected{'LOG_VERB'}{'10'} = '';
2536     $selected{'LOG_VERB'}{'11'} = '';
2537     $selected{'LOG_VERB'}{$cgiparams{'LOG_VERB'}} = 'SELECTED';
2538     $selected{'DAUTH'}{'whirlpool'} = '';
2539     $selected{'DAUTH'}{'SHA512'} = '';
2540     $selected{'DAUTH'}{'SHA384'} = '';
2541     $selected{'DAUTH'}{'SHA256'} = '';
2542     $selected{'DAUTH'}{'SHA1'} = '';
2543     $selected{'DAUTH'}{$cgiparams{'DAUTH'}} = 'SELECTED';
2544     $checked{'TLSAUTH'}{'off'} = '';
2545     $checked{'TLSAUTH'}{'on'} = '';
2546     $checked{'TLSAUTH'}{$cgiparams{'TLSAUTH'}} = 'CHECKED';
2547    
2548     &Header::showhttpheaders();
2549     &Header::openpage($Lang::tr{'status ovpn'}, 1, '');
2550     &Header::openbigbox('100%', 'LEFT', '', $errormessage);    
2551     if ($errormessage) {
2552         &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
2553         print "<class name='base'>$errormessage\n";
2554         print "&nbsp;</class>\n";
2555         &Header::closebox();
2556     }
2557     &Header::openbox('100%', 'LEFT', $Lang::tr{'advanced server'});
2558     print <<END;
2559     <form method='post' enctype='multipart/form-data'>
2560 <table width='100%' border=0>
2561         <tr>
2562                 <td colspan='4'><b>$Lang::tr{'dhcp-options'}</b></td>
2563     </tr>
2564     <tr>
2565                 <td width='25%'></td> <td width='20%'> </td><td width='25%'> </td><td width='30%'></td>
2566     </tr>       
2567     <tr>                
2568                 <td class='base'>Domain</td>
2569         <td><input type='TEXT' name='DHCP_DOMAIN' value='$cgiparams{'DHCP_DOMAIN'}' size='30'  /></td>
2570     </tr>
2571     <tr>        
2572                 <td class='base'>DNS</td>
2573                 <td><input type='TEXT' name='DHCP_DNS' value='$cgiparams{'DHCP_DNS'}' size='30' /></td>
2574     </tr>       
2575     <tr>        
2576                 <td class='base'>WINS</td>
2577                 <td><input type='TEXT' name='DHCP_WINS' value='$cgiparams{'DHCP_WINS'}' size='30' /></td>
2578         </tr>
2579     <tr>
2580                 <td colspan='4'><b>$Lang::tr{'ovpn routes push options'}</b></td>
2581     </tr>
2582     <tr>        
2583                 <td class='base'>$Lang::tr{'ovpn routes push'}</td>
2584                 <td colspan='2'>
2585                 <textarea name='ROUTES_PUSH' cols='26' rows='6' wrap='off'>
2586 END
2587 ;
2588
2589 if ($cgiparams{'ROUTES_PUSH'} ne '')
2590 {
2591         print $cgiparams{'ROUTES_PUSH'};
2592 }
2593
2594 print <<END;
2595 </textarea></td>
2596 </tr>
2597     </tr>
2598 </table>
2599 <hr size='1'>
2600 <table width='100%'>
2601         <tr>
2602                 <td class'base'><b>$Lang::tr{'misc-options'}</b></td>
2603         </tr>
2604
2605         <tr>
2606                 <td width='20%'></td> <td width='15%'> </td><td width='15%'> </td><td width='15%'></td><td width='35%'></td>
2607         </tr>
2608
2609         <tr>
2610                 <td class='base'>Client-To-Client</td>
2611                 <td><input type='checkbox' name='CLIENT2CLIENT' $checked{'CLIENT2CLIENT'}{'on'} /></td>
2612         </tr>
2613
2614         <tr>
2615                 <td class='base'>Redirect-Gateway def1</td>
2616                 <td><input type='checkbox' name='REDIRECT_GW_DEF1' $checked{'REDIRECT_GW_DEF1'}{'on'} /></td>
2617         </tr>
2618
2619         <tr>
2620                 <td class='base'>$Lang::tr{'ovpn add conf'}</td>
2621                 <td><input type='checkbox' name='ADDITIONAL_CONFIGS' $checked{'ADDITIONAL_CONFIGS'}{'on'} /></td>
2622                 <td>$Lang::tr{'openvpn default'}: off</td>
2623         </tr>
2624
2625         <tr>
2626                 <td class='base'>mssfix</td>
2627                 <td><input type='checkbox' name='MSSFIX' $checked{'MSSFIX'}{'on'} /></td>
2628                 <td>$Lang::tr{'openvpn default'}: off</td>
2629         </tr>
2630
2631         <tr>
2632                 <td class='base'>fragment <br></td>
2633                 <td><input type='TEXT' name='FRAGMENT' value='$cgiparams{'FRAGMENT'}' size='10' /></td>
2634         </tr>
2635
2636
2637         <tr>
2638                 <td class='base'>Max-Clients</td>
2639                 <td><input type='text' name='MAX_CLIENTS' value='$cgiparams{'MAX_CLIENTS'}' size='10' /></td>
2640         </tr>
2641         <tr>
2642                 <td class='base'>Keepalive <br />
2643                 (ping/ping-restart)</td>
2644                 <td><input type='TEXT' name='KEEPALIVE_1' value='$cgiparams{'KEEPALIVE_1'}' size='10' /></td>
2645                 <td><input type='TEXT' name='KEEPALIVE_2' value='$cgiparams{'KEEPALIVE_2'}' size='10' /></td>
2646         </tr>
2647
2648         <tr>
2649                 <td class='base'>$Lang::tr{'ovpn mtu-disc'}</td>
2650                 <td><input type='radio' name='PMTU_DISCOVERY' value='yes' $checked{'PMTU_DISCOVERY'}{'yes'} /> $Lang::tr{'ovpn mtu-disc yes'}</td>
2651                 <td><input type='radio' name='PMTU_DISCOVERY' value='maybe' $checked{'PMTU_DISCOVERY'}{'maybe'} /> $Lang::tr{'ovpn mtu-disc maybe'}</td>
2652                 <td><input type='radio' name='PMTU_DISCOVERY' value='no' $checked{'PMTU_DISCOVERY'}{'no'} /> $Lang::tr{'ovpn mtu-disc no'}</td>
2653                 <td><input type='radio' name='PMTU_DISCOVERY' value='off' $checked{'PMTU_DISCOVERY'}{'off'} /> $Lang::tr{'ovpn mtu-disc off'}</td>
2654         </tr>
2655 </table>
2656
2657 <hr size='1'>
2658 <table width='100%'>
2659     <tr>
2660         <td class'base'><b>$Lang::tr{'log-options'}</b></td>
2661     </tr>
2662     <tr>
2663         <td width='20%'></td> <td width='30%'> </td><td width='25%'> </td><td width='25%'></td>
2664     </tr>
2665
2666     <tr><td class='base'>VERB</td>
2667         <td><select name='LOG_VERB'>
2668                         <option value='0'  $selected{'LOG_VERB'}{'0'}>0</option>
2669                         <option value='1'  $selected{'LOG_VERB'}{'1'}>1</option>
2670                         <option value='2'  $selected{'LOG_VERB'}{'2'}>2</option>
2671                         <option value='3'  $selected{'LOG_VERB'}{'3'}>3</option>
2672                         <option value='4'  $selected{'LOG_VERB'}{'4'}>4</option>
2673                         <option value='5'  $selected{'LOG_VERB'}{'5'}>5</option>
2674                         <option value='6'  $selected{'LOG_VERB'}{'6'}>6</option>
2675                         <option value='7'  $selected{'LOG_VERB'}{'7'}>7</option>
2676                         <option value='8'  $selected{'LOG_VERB'}{'8'}>8</option>
2677                         <option value='9'  $selected{'LOG_VERB'}{'9'}>9</option>
2678                         <option value='10' $selected{'LOG_VERB'}{'10'}>10</option>
2679                         <option value='11' $selected{'LOG_VERB'}{'11'}>11</option>
2680         </td></select>
2681     </table>
2682
2683 <hr size='1'>
2684 <table width='100%'>
2685     <tr>
2686                 <td class'base'><b>$Lang::tr{'ovpn crypt options'}</b></td>
2687         </tr>
2688         <tr>
2689                 <td width='20%'></td> <td width='30%'> </td><td width='25%'> </td><td width='25%'></td>
2690     </tr>       
2691     <tr><td class='base'>$Lang::tr{'ovpn ha'}</td>
2692                 <td><select name='DAUTH'>
2693                                 <option value='whirlpool'               $selected{'DAUTH'}{'whirlpool'}>Whirlpool (512 $Lang::tr{'bit'})</option>
2694                                 <option value='SHA512'                  $selected{'DAUTH'}{'SHA512'}>SHA2 (512 $Lang::tr{'bit'})</option>
2695                                 <option value='SHA384'                  $selected{'DAUTH'}{'SHA384'}>SHA2 (384 $Lang::tr{'bit'})</option>
2696                                 <option value='SHA256'                  $selected{'DAUTH'}{'SHA256'}>SHA2 (256 $Lang::tr{'bit'})</option>
2697                                 <option value='SHA1'                    $selected{'DAUTH'}{'SHA1'}>SHA1 (160 $Lang::tr{'bit'})</option>
2698                         </select>
2699                 </td>
2700                 <td>$Lang::tr{'openvpn default'}: <span class="base">SHA1 (160 $Lang::tr{'bit'})</span></td>
2701     </tr>
2702 </table>
2703
2704 <table width='100%'>
2705     <tr>
2706         <td width='20%'></td> <td width='15%'> </td><td width='15%'> </td><td width='15%'></td><td width='35%'></td>
2707     </tr>
2708
2709     <tr>
2710         <td class='base'>HMAC tls-auth</td>
2711         <td><input type='checkbox' name='TLSAUTH' $checked{'TLSAUTH'}{'on'} /></td>
2712     </tr>
2713     </table><hr>
2714 END
2715
2716 if ( -e "/var/run/openvpn.pid"){
2717 print"  <br><b><font color='#990000'>$Lang::tr{'attention'}:</b></font><br>
2718                 $Lang::tr{'server restart'}<br><br>
2719                 <hr>";
2720         print<<END;
2721 <table width='100%'>
2722 <tr>
2723     <td>&nbsp;</td>
2724     <td allign='center'><input type='submit' name='ACTION' value='$Lang::tr{'save-adv-options'}' disabled='disabled' /></td>
2725     <td allign='center'><input type='submit' name='ACTION' value='$Lang::tr{'cancel-adv-options'}' /></td>
2726     <td>&nbsp;</td>    
2727 </tr>
2728 </table>    
2729 </form>
2730 END
2731 ;               
2732                 
2733                 
2734 }else{
2735
2736         print<<END;
2737 <table width='100%'>
2738 <tr>
2739     <td>&nbsp;</td>
2740     <td allign='center'><input type='submit' name='ACTION' value='$Lang::tr{'save-adv-options'}' /></td>
2741     <td allign='center'><input type='submit' name='ACTION' value='$Lang::tr{'cancel-adv-options'}' /></td>
2742     <td>&nbsp;</td>    
2743 </tr>
2744 </table>    
2745 </form>
2746 END
2747 ;                                  
2748 }
2749     &Header::closebox();
2750 #    print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2751     &Header::closebigbox();
2752     &Header::closepage();
2753     exit(0);
2754         
2755
2756 # A.Marx CCD   Add,delete or edit CCD net
2757
2758 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'ccd net'} || 
2759                 $cgiparams{'ACTION'} eq $Lang::tr{'ccd add'} ||  
2760                 $cgiparams{'ACTION'} eq "kill" || 
2761                 $cgiparams{'ACTION'} eq "edit" ||
2762                 $cgiparams{'ACTION'} eq 'editsave'){
2763         &Header::showhttpheaders();
2764         &Header::openpage($Lang::tr{'ccd net'}, 1, '');
2765         &Header::openbigbox('100%', 'LEFT', '', '');
2766
2767         if ($cgiparams{'ACTION'} eq "kill"){
2768                 &delccdnet($cgiparams{'net'});
2769         }
2770         
2771         if ($cgiparams{'ACTION'} eq 'editsave'){
2772                 my ($a,$b) =split (/\|/,$cgiparams{'ccdname'});
2773                 if ( $a ne $b){ &modccdnet($a,$b);}
2774                 $cgiparams{'ccdname'}='';
2775                 $cgiparams{'ccdsubnet'}='';
2776         }
2777         
2778         if ($cgiparams{'ACTION'} eq $Lang::tr{'ccd add'}) {
2779                 &addccdnet($cgiparams{'ccdname'},$cgiparams{'ccdsubnet'});
2780         }
2781         if ($errormessage) {
2782             &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
2783             print "<class name='base'>$errormessage";
2784             print "&nbsp;</class>";
2785             &Header::closebox();                
2786         }
2787 if ($cgiparams{'ACTION'} eq "edit"){
2788         
2789         &Header::openbox('100%', 'LEFT', $Lang::tr{'ccd modify'});
2790
2791         print <<END;
2792     <table width='100%' border='0'>
2793     <tr><form method='post'>
2794         <td width='10%' nowrap='nowrap'>$Lang::tr{'ccd name'}:</td><td><input type='TEXT' name='ccdname' value='$cgiparams{'ccdname'}' /></td>
2795         <td width='8%'>$Lang::tr{'ccd subnet'}:</td><td><input type='TEXT' name='ccdsubnet' value='$cgiparams{'ccdsubnet'}' readonly='readonly' /></td></tr>
2796         <tr><td colspan='4' align='right'><hr><input type='submit' value='$Lang::tr{'save'}' /><input type='hidden' name='ACTION' value='editsave'/>
2797         <input type='hidden' name='ccdname' value='$cgiparams{'ccdname'}'/><input type='submit' value='$Lang::tr{'cancel'}' />
2798         </td></tr>
2799         </table></form>
2800 END
2801 ;
2802         &Header::closebox();
2803
2804         &Header::openbox('100%', 'LEFT',$Lang::tr{'ccd net'} );
2805         print <<END;
2806     <table width='100%' border='0'  cellpadding='0' cellspacing='1'>
2807     <tr>
2808         <td class='boldbase' align='center'><b>$Lang::tr{'ccd name'}</td><td class='boldbase' align='center'><b>$Lang::tr{'network'}</td><td class='boldbase' width='15%' align='center'><b>$Lang::tr{'ccd used'}</td><td width='3%'></td><td width='3%'></td></tr>
2809 END
2810 ;
2811 }
2812 else{
2813         if (! -e "/var/run/openvpn.pid"){
2814         &Header::openbox('100%', 'LEFT', $Lang::tr{'ccd add'});
2815         print <<END;
2816             <table width='100%' border='0'>
2817             <tr><form method='post'>
2818                 <td colspan='4'>$Lang::tr{'ccd hint'}<br><br></td></tr>
2819                 <tr>
2820                 <td width='10%' nowrap='nwrap'>$Lang::tr{'ccd name'}:</td><td><input type='TEXT' name='ccdname' value='$cgiparams{'ccdname'}' /></td>
2821                 <td width='8%'>$Lang::tr{'ccd subnet'}:</td><td><input type='TEXT' name='ccdsubnet' value='$cgiparams{'ccdsubnet'}' /></td></tr>
2822                 <tr><td colspan=4><hr /></td></tr><tr>
2823                 <td colspan='4' align='right'><input type='hidden' name='ACTION' value='$Lang::tr{'ccd add'}' /><input type='submit' value='$Lang::tr{'add'}' /><input type='hidden' name='DOVPN_SUBNET' value='$cgiparams{'DOVPN_SUBNET'}'/></td></tr>
2824                 </table></form>
2825 END
2826         
2827         &Header::closebox();
2828 }
2829         &Header::openbox('100%', 'LEFT',$Lang::tr{'ccd net'} );
2830         if ( -e "/var/run/openvpn.pid"){
2831                 print "<b>$Lang::tr{'attention'}:</b><br>";
2832                 print "$Lang::tr{'ccd noaddnet'}<br><hr>";
2833         }
2834         
2835     print <<END;
2836     <table width='100%' cellpadding='0' cellspacing='1'>
2837     <tr>
2838         <td class='boldbase' align='center' nowrap='nowrap' width='20%'><b>$Lang::tr{'ccd name'}</td><td class='boldbase' align='center' width='8%'><b>$Lang::tr{'network'}</td><td class='boldbase' width='8%' align='center' nowrap='nowrap'><b>$Lang::tr{'ccd used'}</td><td width='1%' align='center'></td><td width='1%' align='center'></td></tr>
2839 END
2840 ;
2841 }
2842         my %ccdconfhash=();     
2843         &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);     
2844         my @ccdconf=();
2845         my $count=0;
2846         foreach my $key (sort { uc($ccdconfhash{$a}[0]) cmp uc($ccdconfhash{$b}[0]) } keys %ccdconfhash) {
2847                 @ccdconf=($ccdconfhash{$key}[0],$ccdconfhash{$key}[1]);
2848                 $count++;
2849                 my $ccdhosts = &hostsinnet($ccdconf[0]);
2850                 if ($count % 2){ print" <tr bgcolor='$color{'color22'}'>";}
2851                 else{            print" <tr bgcolor='$color{'color20'}'>";}
2852                 print"<td>$ccdconf[0]</td><td align='center'>$ccdconf[1]</td><td align='center'>$ccdhosts/".(&ccdmaxclients($ccdconf[1])+1)."</td><td>";
2853         print <<END;
2854                 <form method='post' />
2855                 <input type='image' src='/images/edit.gif' align='middle' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
2856                 <input type='hidden' name='ACTION' value='edit'/>
2857                 <input type='hidden' name='ccdname' value='$ccdconf[0]' />
2858                 <input type='hidden' name='ccdsubnet' value='$ccdconf[1]' />
2859                 </form></td>
2860                 <form method='post' />
2861                 <td><input type='hidden' name='ACTION' value='kill'/>
2862                 <input type='hidden' name='number' value='$count' />
2863                 <input type='hidden' name='net' value='$ccdconf[0]' />
2864                 <input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' /></form></td></tr>
2865 END
2866 ;
2867         }       
2868         print "</table></form>";
2869         &Header::closebox();
2870         print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2871         &Header::closebigbox();
2872         &Header::closepage();
2873         exit(0);
2874         
2875 #END CCD
2876
2877 ###
2878 ### Openvpn Connections Statistics
2879 ###
2880 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'ovpn con stat'}) {
2881         &Header::showhttpheaders();
2882         &Header::openpage($Lang::tr{'ovpn con stat'}, 1, '');
2883         &Header::openbigbox('100%', 'LEFT', '', '');
2884     &Header::openbox('100%', 'LEFT', $Lang::tr{'ovpn con stat'});
2885
2886 #
2887 #       <td><b>$Lang::tr{'protocol'}</b></td>
2888 # protocol temp removed 
2889     print <<END;
2890     <table width='100%' cellpadding='2' cellspacing='0' class='tbl'>
2891     <tr>
2892         <th><b>$Lang::tr{'common name'}</b></th>
2893         <th><b>$Lang::tr{'real address'}</b></th>
2894         <th><b>$Lang::tr{'virtual address'}</b></th>
2895         <th><b>$Lang::tr{'loged in at'}</b></th>
2896         <th><b>$Lang::tr{'bytes sent'}</b></th>
2897         <th><b>$Lang::tr{'bytes received'}</b></th>
2898         <th><b>$Lang::tr{'last activity'}</b></th>
2899     </tr>
2900 END
2901 ;
2902         my $filename = "/var/log/ovpnserver.log";
2903         open(FILE, $filename) or die 'Unable to open config file.';
2904         my @current = <FILE>;
2905         close(FILE);
2906         my @users =();
2907         my $status;
2908         my $uid = 0;
2909         my $cn;
2910         my @match = ();
2911         my $proto = "udp";
2912         my $address;
2913         my %userlookup = ();
2914         foreach my $line (@current)
2915         {
2916             chomp($line);
2917             if ( $line =~ /^Updated,(.+)/){
2918                 @match = split( /^Updated,(.+)/, $line); 
2919                 $status = $match[1];
2920             }
2921 #gian       
2922             if ( $line =~ /^(.+),(\d+\.\d+\.\d+\.\d+\:\d+),(\d+),(\d+),(.+)/) {
2923                 @match = split(m/^(.+),(\d+\.\d+\.\d+\.\d+\:\d+),(\d+),(\d+),(.+)/, $line);
2924                 if ($match[1] ne "Common Name") {
2925                     $cn = $match[1];
2926                     $userlookup{$match[2]} = $uid;
2927                     $users[$uid]{'CommonName'} = $match[1];
2928                     $users[$uid]{'RealAddress'} = $match[2];
2929                     $users[$uid]{'BytesReceived'} = &sizeformat($match[3]);
2930                     $users[$uid]{'BytesSent'} = &sizeformat($match[4]);
2931                     $users[$uid]{'Since'} = $match[5];
2932                     $users[$uid]{'Proto'} = $proto;
2933                     $uid++;
2934                 }    
2935             }
2936             if ( $line =~ /^(\d+\.\d+\.\d+\.\d+),(.+),(\d+\.\d+\.\d+\.\d+\:\d+),(.+)/) {
2937                 @match = split(m/^(\d+\.\d+\.\d+\.\d+),(.+),(\d+\.\d+\.\d+\.\d+\:\d+),(.+)/, $line);
2938                 if ($match[1] ne "Virtual Address") {
2939                     $address = $match[3];
2940                     #find the uid in the lookup table
2941                     $uid = $userlookup{$address};
2942                     $users[$uid]{'VirtualAddress'} = $match[1];
2943                     $users[$uid]{'LastRef'} = $match[4];
2944                 }
2945             }
2946         }
2947         my $user2 = @users;
2948         if ($user2 >= 1){
2949                 for (my $idx = 1; $idx <= $user2; $idx++){
2950                                                 if ($idx % 2) {
2951                                                         print "<tr>";
2952                                                         $col="bgcolor='$color{'color22'}'";
2953                                                 } else {
2954                                                         print "<tr>";
2955                                                         $col="bgcolor='$color{'color20'}'";
2956                                                 }
2957                                                 print "<td align='left' $col>$users[$idx-1]{'CommonName'}</td>";
2958                                                 print "<td align='left' $col>$users[$idx-1]{'RealAddress'}</td>";
2959                                                 print "<td align='left' $col>$users[$idx-1]{'VirtualAddress'}</td>";
2960                                                 print "<td align='left' $col>$users[$idx-1]{'Since'}</td>";
2961                                                 print "<td align='left' $col>$users[$idx-1]{'BytesSent'}</td>";
2962                                                 print "<td align='left' $col>$users[$idx-1]{'BytesReceived'}</td>";
2963                                                 print "<td align='left' $col>$users[$idx-1]{'LastRef'}</td>";
2964                         }
2965         }
2966         
2967         print "</table>";
2968         print <<END;
2969         <table width='100%' border='0' cellpadding='2' cellspacing='0'>
2970         <tr><td></td></tr>
2971         <tr><td></td></tr>
2972         <tr><td></td></tr>
2973         <tr><td></td></tr>
2974         <tr><td align='center' >$Lang::tr{'the statistics were last updated at'} <b>$status</b></td></tr>
2975         </table>
2976 END
2977 ;       
2978         &Header::closebox();
2979         print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2980         &Header::closebigbox();
2981         &Header::closepage();
2982         exit(0);
2983
2984 ###
2985 ### Download Certificate
2986 ###
2987 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download certificate'}) {
2988     &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2989
2990     if ( -f "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem") {
2991         print "Content-Disposition: filename=" . $confighash{$cgiparams{'KEY'}}[1] . "cert.pem\r\n";
2992         print "Content-Type: application/octet-stream\r\n\r\n";
2993         print `/bin/cat ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
2994         exit (0);
2995     }
2996
2997 ###
2998 ### Enable/Disable connection
2999 ###
3000
3001 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
3002     
3003     &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
3004     &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
3005
3006     if ($confighash{$cgiparams{'KEY'}}) {
3007            if ($confighash{$cgiparams{'KEY'}}[0] eq 'off') {
3008             $confighash{$cgiparams{'KEY'}}[0] = 'on';
3009             &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
3010             #&writeserverconf();
3011 #           if ($vpnsettings{'ENABLED'} eq 'on' ||
3012 #               $vpnsettings{'ENABLED_BLUE'} eq 'on') {
3013 #               system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'});
3014 #           }
3015         } else {
3016             $confighash{$cgiparams{'KEY'}}[0] = 'off';
3017 #           if ($vpnsettings{'ENABLED'} eq 'on' ||
3018 #               $vpnsettings{'ENABLED_BLUE'} eq 'on') {
3019 #               system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'});
3020 #           }
3021             &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
3022             #&writeserverconf();
3023         }
3024     } else {
3025         $errormessage = $Lang::tr{'invalid key'};
3026     }
3027
3028 ###
3029 ### Restart connection
3030 ###
3031 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'restart'}) {
3032     &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
3033     &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
3034
3035     if ($confighash{$cgiparams{'KEY'}}) {
3036 #       if ($vpnsettings{'ENABLED'} eq 'on' ||
3037 #           $vpnsettings{'ENABLED_BLUE'} eq 'on') {
3038 #           system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'});
3039 #       }
3040     } else {
3041         $errormessage = $Lang::tr{'invalid key'};
3042     }
3043
3044 ###
3045 ### Remove connection
3046 ###
3047 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove'}) {
3048     &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
3049     &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
3050
3051     if ($confighash{$cgiparams{'KEY'}}) {
3052 #       if ($vpnsettings{'ENABLED'} eq 'on' ||
3053 #           $vpnsettings{'ENABLED_BLUE'} eq 'on') {
3054 #           system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'});
3055 #       }
3056         unlink ("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem");
3057         unlink ("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12");
3058         delete $confighash{$cgiparams{'KEY'}};
3059         &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
3060         #&writeserverconf();
3061     } else {
3062         $errormessage = $Lang::tr{'invalid key'};
3063     }
3064 #test33
3065
3066 ###
3067 ### Choose between adding a host-net or net-net connection
3068 ###
3069
3070 ###
3071 # m.a.d net2net
3072 ###
3073
3074 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'add'} && $cgiparams{'TYPE'} eq '') {
3075         &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
3076         &Header::showhttpheaders();
3077         &Header::openpage($Lang::tr{'ovpn'}, 1, '');
3078         &Header::openbigbox('100%', 'LEFT', '', '');
3079         &Header::openbox('100%', 'LEFT', $Lang::tr{'connection type'});
3080
3081 if ( -s "${General::swroot}/ovpn/settings") {
3082
3083         print <<END;
3084             <b>$Lang::tr{'connection type'}:</b><br />
3085             <table border='0' width='100%'><form method='post' ENCTYPE="multipart/form-data">
3086             <tr><td><input type='radio' name='TYPE' value='host' checked /></td>
3087                 <td class='base'>$Lang::tr{'host to net vpn'}</td></tr>
3088             <tr><td><input type='radio' name='TYPE' value='net' /></td>
3089                 <td class='base'>$Lang::tr{'net to net vpn'}</td></tr>
3090                 <tr><td><input type='radio' name='TYPE' value='net2net' /></td>         
3091                 <td class='base'>$Lang::tr{'net to net vpn'} (Upload Client Package)</td></tr>
3092           <tr><td>&nbsp;</td><td class='base'><input type='file' name='FH' size='30'></td></tr>
3093           <tr><td>&nbsp;</td><td>Import Connection Name <img src='/blob.gif' /></td></tr>
3094     <tr><td>&nbsp;</td><td class='base'><input type='text' name='n2nname' size='30'>$Lang::tr{'openvpn default'}: Client Packagename</td></tr>
3095           <tr><td colspan='3'><hr /></td></tr>
3096     <tr><td align='right' colspan='3'><input type='submit' name='ACTION' value='$Lang::tr{'add'}' /></td></tr>
3097           <tr><td class='base' colspan='3' align='left'><img src='/blob.gif' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td></tr>
3098             </form></table>
3099 END
3100         ;
3101         
3102
3103 } else {
3104         print <<END;
3105                     <b>$Lang::tr{'connection type'}:</b><br />
3106             <table border='0' width='100%'><form method='post' ENCTYPE="multipart/form-data">
3107             <tr><td><input type='radio' name='TYPE' value='host' checked /></td> <td class='base'>$Lang::tr{'host to net vpn'}</td></tr>
3108             <tr><td align='right' colspan'3'><input type='submit' name='ACTION' value='$Lang::tr{'add'}' /></td></tr>
3109             </form></table>
3110 END
3111         ;
3112
3113 }
3114
3115         &Header::closebox();
3116         print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
3117         &Header::closebigbox();
3118         &Header::closepage();
3119         exit (0);
3120
3121 ###
3122 # m.a.d net2net
3123 ###
3124
3125 }  elsif (($cgiparams{'ACTION'} eq $Lang::tr{'add'}) && ($cgiparams{'TYPE'} eq 'net2net')){
3126
3127         my @firen2nconf;
3128         my @confdetails;
3129         my $uplconffilename ='';
3130         my $uplconffilename2 ='';
3131         my $uplp12name = '';
3132         my $uplp12name2 = '';
3133         my @rem_subnet;
3134         my @rem_subnet2;
3135         my @tmposupnet3;        
3136         my $key;
3137         my @n2nname;
3138
3139         &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);    
3140
3141 # Check if a file is uploaded
3142
3143         if (ref ($cgiparams{'FH'}) ne 'Fh') {
3144                 $errormessage = $Lang::tr{'there was no file upload'};
3145                 goto N2N_ERROR;
3146     }
3147
3148 # Move uploaded IPfire n2n package to temporary file
3149
3150     (my $fh, my $filename) = tempfile( );
3151     if (copy ($cgiparams{'FH'}, $fh) != 1) {
3152                 $errormessage = $!;
3153                 goto N2N_ERROR;
3154     }
3155
3156         my $zip = Archive::Zip->new();
3157         my $zipName = $filename;
3158         my $status = $zip->read( $zipName );
3159         if ($status != AZ_OK) {   
3160                 $errormessage = "Read of $zipName failed\n";
3161                 goto N2N_ERROR;
3162         }
3163
3164         my $tempdir = tempdir( CLEANUP => 1 );
3165         my @files = $zip->memberNames();
3166         for(@files) {
3167         $zip->extractMemberWithoutPaths($_,"$tempdir/$_");
3168         }
3169         my $countfiles = @files;
3170
3171 # Check if we have not more then 2 files
3172
3173         if ( $countfiles == 2){
3174                 foreach (@files){
3175                         if ( $_ =~ /.conf$/){
3176                                 $uplconffilename = $_;
3177                         }
3178                         if ( $_ =~ /.p12$/){
3179                                 $uplp12name = $_;
3180                         }                       
3181                 }
3182                 if (($uplconffilename eq '') || ($uplp12name eq '')){
3183                         $errormessage = "Either no *.conf or no *.p12 file found\n";
3184                         goto N2N_ERROR;
3185                 }
3186
3187                 open(FILE, "$tempdir/$uplconffilename") or die 'Unable to open*.conf file';
3188                 @firen2nconf = <FILE>;
3189                 close (FILE);
3190                 chomp(@firen2nconf);
3191
3192         } else {
3193
3194                 $errormessage = "Filecount does not match only 2 files are allowed\n";
3195                 goto N2N_ERROR;
3196         }
3197
3198 ###
3199 # m.a.d net2net
3200 ###
3201   
3202  if ($cgiparams{'n2nname'} ne ''){
3203
3204   $uplconffilename2 = "$cgiparams{'n2nname'}.conf"; 
3205   $uplp12name2 = "$cgiparams{'n2nname'}.p12"; 
3206   $n2nname[0] = $cgiparams{'n2nname'};
3207   my @n2nname2 = split(/\./,$uplconffilename);
3208   $n2nname2[0] =~ s/\n|\r//g;
3209   my $input1 = "${General::swroot}/ovpn/certs/$uplp12name";
3210         my $output1 = "${General::swroot}/ovpn/certs/$uplp12name2";
3211         my $input2 = "$n2nname2[0]n2n";
3212   my $output2 = "$n2nname[0]n2n";
3213   my $filename = "$tempdir/$uplconffilename";
3214   open(FILE, "< $filename") or die 'Unable to open config file.';
3215         my @current = <FILE>;
3216         close(FILE);
3217         foreach (@current) {s/$input1/$output1/g;}
3218         foreach (@current) {s/$input2/$output2/g;}
3219   open (OUT, "> $filename") || die 'Unable to open config file.';
3220   print OUT @current;
3221   close OUT;
3222
3223     }else{
3224     $uplconffilename2 =  $uplconffilename;
3225     $uplp12name2 = $uplp12name;
3226     @n2nname = split(/\./,$uplconffilename);
3227     $n2nname[0] =~ s/\n|\r//g;
3228    } 
3229     unless(-d "${General::swroot}/ovpn/n2nconf/"){mkdir "${General::swroot}/ovpn/n2nconf", 0755 or die "Unable to create dir $!";}
3230     unless(-d "${General::swroot}/ovpn/n2nconf/$n2nname[0]"){mkdir "${General::swroot}/ovpn/n2nconf/$n2nname[0]", 0770 or die "Unable to create dir $!";}   
3231
3232         move("$tempdir/$uplconffilename", "${General::swroot}/ovpn/n2nconf/$n2nname[0]/$uplconffilename2");
3233
3234         if ($? ne 0) {
3235             $errormessage = "*.conf move failed: $!";
3236             unlink ($filename);
3237             goto N2N_ERROR;
3238         }
3239         
3240         move("$tempdir/$uplp12name", "${General::swroot}/ovpn/certs/$uplp12name2");
3241         chmod 0600, "${General::swroot}/ovpn/certs/$uplp12name";
3242         
3243         if ($? ne 0) {
3244             $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
3245             unlink ($filename);
3246             goto N2N_ERROR;
3247         }       
3248         
3249 my $complzoactive;
3250 my $mssfixactive;
3251 my $authactive;
3252 my $n2nfragment;
3253 my @n2nmtudisc = split(/ /, (grep { /^mtu-disc/ } @firen2nconf)[0]);
3254 my @n2nproto2 = split(/ /, (grep { /^proto/ } @firen2nconf)[0]);
3255 my @n2nproto = split(/-/, $n2nproto2[1]);
3256 my @n2nport = split(/ /, (grep { /^port/ } @firen2nconf)[0]);
3257 my @n2ntunmtu = split(/ /, (grep { /^tun-mtu/ } @firen2nconf)[0]);
3258 my @n2ncomplzo = grep { /^comp-lzo/ } @firen2nconf;
3259 if ($n2ncomplzo[0] =~ /comp-lzo/){$complzoactive = "on";} else {$complzoactive = "off";}        
3260 my @n2nmssfix  = grep { /^mssfix/ } @firen2nconf;
3261 if ($n2nmssfix[0] =~ /mssfix/){$mssfixactive = "on";} else {$mssfixactive = "off";}
3262 #my @n2nmssfix = split(/ /, (grep { /^mssfix/ } @firen2nconf)[0]);
3263 my @n2nfragment = split(/ /, (grep { /^fragment/ } @firen2nconf)[0]);
3264 my @n2nremote = split(/ /, (grep { /^remote/ } @firen2nconf)[0]);
3265 my @n2novpnsuball = split(/ /, (grep { /^ifconfig/ } @firen2nconf)[0]);
3266 my @n2novpnsub =  split(/\./,$n2novpnsuball[1]);
3267 my @n2nremsub = split(/ /, (grep { /^route/ } @firen2nconf)[0]);
3268 my @n2nmgmt =  split(/ /, (grep { /^management/ } @firen2nconf)[0]);
3269 my @n2nlocalsub  = split(/ /, (grep { /^# remsub/ } @firen2nconf)[0]);
3270 my @n2ncipher = split(/ /, (grep { /^cipher/ } @firen2nconf)[0]);
3271 my @n2nauth = split(/ /, (grep { /^auth/ } @firen2nconf)[0]);;
3272
3273 ###
3274 # m.a.d delete CR and LF from arrays for this chomp doesnt work
3275 ###
3276
3277 $n2nremote[1] =~ s/\n|\r//g;
3278 $n2novpnsub[0] =~ s/\n|\r//g;
3279 $n2novpnsub[1] =~ s/\n|\r//g;
3280 $n2novpnsub[2] =~ s/\n|\r//g;
3281 $n2nproto[0] =~ s/\n|\r//g;
3282 $n2nport[1] =~ s/\n|\r//g;
3283 $n2ntunmtu[1] =~ s/\n|\r//g;
3284 $n2nremsub[1] =~ s/\n|\r//g;
3285 $n2nremsub[2] =~ s/\n|\r//g;
3286 $n2nlocalsub[2] =~ s/\n|\r//g;
3287 $n2nfragment[1] =~ s/\n|\r//g;
3288 $n2nmgmt[2] =~ s/\n|\r//g;
3289 $n2nmtudisc[1] =~ s/\n|\r//g;
3290 $n2ncipher[1] =~ s/\n|\r//g;
3291 $n2nauth[1] =~ s/\n|\r//g;
3292 chomp ($complzoactive);
3293 chomp ($mssfixactive);
3294
3295 ###
3296 # m.a.d net2net