d8fc8932767c0473b8a612a124ad2a87f0b434b1
[people/teissler/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-2013 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 55
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 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
74 $cgiparams{'ENABLED'} = 'off';
75 $cgiparams{'ENABLED_BLUE'} = 'off';
76 $cgiparams{'ENABLED_ORANGE'} = 'off';
77 $cgiparams{'EDIT_ADVANCED'} = 'off';
78 $cgiparams{'NAT'} = 'off';
79 $cgiparams{'COMPRESSION'} = 'off';
80 $cgiparams{'ONLY_PROPOSED'} = 'off';
81 $cgiparams{'ACTION'} = '';
82 $cgiparams{'CA_NAME'} = '';
83 $cgiparams{'DHCP_DOMAIN'} = '';
84 $cgiparams{'DHCP_DNS'} = '';
85 $cgiparams{'DHCP_WINS'} = '';
86 $cgiparams{'ROUTES_PUSH'} = '';
87 $cgiparams{'DCOMPLZO'} = 'off';
88 $cgiparams{'MSSFIX'} = '';
89 $cgiparams{'number'} = '';
90 $cgiparams{'PMTU_DISCOVERY'} = '';
91 $routes_push_file = "${General::swroot}/ovpn/routes_push";
92 unless (-e $routes_push_file) { system("touch $routes_push_file"); }
93 unless (-e "${General::swroot}/ovpn/ccd.conf") { system("touch ${General::swroot}/ovpn/ccd.conf"); }
94 unless (-e "${General::swroot}/ovpn/ccdroute") { system("touch ${General::swroot}/ovpn/ccdroute"); }
95 unless (-e "${General::swroot}/ovpn/ccdroute2") { system("touch ${General::swroot}/ovpn/ccdroute2"); }
96
97 &Header::getcgihash(\%cgiparams, {'wantfile' => 1, 'filevar' => 'FH'});
98
99 # prepare openvpn config file
100 ###
101 ### Useful functions
102 ###
103 sub haveOrangeNet
104 {
105 if ($netsettings{'CONFIG_TYPE'} == 2) {return 1;}
106 if ($netsettings{'CONFIG_TYPE'} == 4) {return 1;}
107 return 0;
108 }
109
110 sub haveBlueNet
111 {
112 if ($netsettings{'CONFIG_TYPE'} == 3) {return 1;}
113 if ($netsettings{'CONFIG_TYPE'} == 4) {return 1;}
114 return 0;
115 }
116
117 sub sizeformat{
118 my $bytesize = shift;
119 my $i = 0;
120
121 while(abs($bytesize) >= 1024){
122 $bytesize=$bytesize/1024;
123 $i++;
124 last if($i==6);
125 }
126
127 my @units = ("Bytes","KB","MB","GB","TB","PB","EB");
128 my $newsize=(int($bytesize*100 +0.5))/100;
129 return("$newsize $units[$i]");
130 }
131
132 sub cleanssldatabase
133 {
134 if (open(FILE, ">${General::swroot}/ovpn/certs/serial")) {
135 print FILE "01";
136 close FILE;
137 }
138 if (open(FILE, ">${General::swroot}/ovpn/certs/index.txt")) {
139 print FILE "";
140 close FILE;
141 }
142 unlink ("${General::swroot}/ovpn/certs/index.txt.old");
143 unlink ("${General::swroot}/ovpn/certs/serial.old");
144 unlink ("${General::swroot}/ovpn/certs/01.pem");
145 }
146
147 sub newcleanssldatabase
148 {
149 if (! -s "${General::swroot}/ovpn/certs/serial" ) {
150 open(FILE, ">${General::swroot}(ovpn/certs/serial");
151 print FILE "01";
152 close FILE;
153 }
154 if (! -s ">${General::swroot}/ovpn/certs/index.txt") {
155 system ("touch ${General::swroot}/ovpn/certs/index.txt");
156 }
157 unlink ("${General::swroot}/ovpn/certs/index.txt.old");
158 unlink ("${General::swroot}/ovpn/certs/serial.old");
159 }
160
161 sub deletebackupcert
162 {
163 if (open(FILE, "${General::swroot}/ovpn/certs/serial.old")) {
164 my $hexvalue = <FILE>;
165 chomp $hexvalue;
166 close FILE;
167 unlink ("${General::swroot}/ovpn/certs/$hexvalue.pem");
168 }
169 }
170 sub checkportfw {
171 my $DPORT = shift;
172 my $DPROT = shift;
173 my %natconfig =();
174 my $confignat = "${General::swroot}/firewall/config";
175 $DPROT= uc ($DPROT);
176 &General::readhasharray($confignat, \%natconfig);
177 foreach my $key (sort keys %natconfig){
178 my @portarray = split (/\|/,$natconfig{$key}[30]);
179 foreach my $value (@portarray){
180 if ($value =~ /:/i){
181 my ($a,$b) = split (":",$value);
182 if ($DPROT eq $natconfig{$key}[12] && $DPORT gt $a && $DPORT lt $b){
183 $errormessage= "$Lang::tr{'source port in use'} $DPORT";
184 }
185 }else{
186 if ($DPROT eq $natconfig{$key}[12] && $DPORT eq $value){
187 $errormessage= "$Lang::tr{'source port in use'} $DPORT";
188 }
189 }
190 }
191 }
192 return;
193 }
194
195 sub checkportoverlap
196 {
197 my $portrange1 = $_[0]; # New port range
198 my $portrange2 = $_[1]; # existing port range
199 my @tempr1 = split(/\:/,$portrange1);
200 my @tempr2 = split(/\:/,$portrange2);
201
202 unless (&checkportinc($tempr1[0], $portrange2)){ return 0;}
203 unless (&checkportinc($tempr1[1], $portrange2)){ return 0;}
204
205 unless (&checkportinc($tempr2[0], $portrange1)){ return 0;}
206 unless (&checkportinc($tempr2[1], $portrange1)){ return 0;}
207
208 return 1; # Everything checks out!
209 }
210
211 # Darren Critchley - we want to make sure that a port entry is not within an already existing range
212 sub checkportinc
213 {
214 my $port1 = $_[0]; # Port
215 my $portrange2 = $_[1]; # Port range
216 my @tempr1 = split(/\:/,$portrange2);
217
218 if ($port1 < $tempr1[0] || $port1 > $tempr1[1]) {
219 return 1;
220 } else {
221 return 0;
222 }
223 }
224
225 sub writeserverconf {
226 my %sovpnsettings = ();
227 my @temp = ();
228 &General::readhash("${General::swroot}/ovpn/settings", \%sovpnsettings);
229 &read_routepushfile;
230
231 open(CONF, ">${General::swroot}/ovpn/server.conf") or die "Unable to open ${General::swroot}/ovpn/server.conf: $!";
232 flock CONF, 2;
233 print CONF "#OpenVPN Server conf\n";
234 print CONF "\n";
235 print CONF "daemon openvpnserver\n";
236 print CONF "writepid /var/run/openvpn.pid\n";
237 print CONF "#DAN prepare OpenVPN for listening on blue and orange\n";
238 print CONF ";local $sovpnsettings{'VPN_IP'}\n";
239 print CONF "dev $sovpnsettings{'DDEVICE'}\n";
240 print CONF "proto $sovpnsettings{'DPROTOCOL'}\n";
241 print CONF "port $sovpnsettings{'DDEST_PORT'}\n";
242 print CONF "script-security 3 system\n";
243 print CONF "ifconfig-pool-persist /var/ipfire/ovpn/ovpn-leases.db 3600\n";
244 print CONF "client-config-dir /var/ipfire/ovpn/ccd\n";
245 print CONF "tls-server\n";
246 print CONF "ca /var/ipfire/ovpn/ca/cacert.pem\n";
247 print CONF "cert /var/ipfire/ovpn/certs/servercert.pem\n";
248 print CONF "key /var/ipfire/ovpn/certs/serverkey.pem\n";
249 print CONF "dh /var/ipfire/ovpn/ca/dh1024.pem\n";
250 my @tempovpnsubnet = split("\/",$sovpnsettings{'DOVPN_SUBNET'});
251 print CONF "server $tempovpnsubnet[0] $tempovpnsubnet[1]\n";
252 #print CONF "push \"route $netsettings{'GREEN_NETADDRESS'} $netsettings{'GREEN_NETMASK'}\"\n";
253
254 # Check if we are using mssfix, fragment or mtu-disc and set the corretct mtu of 1500.
255 # If we doesn't use one of them, we can use the configured mtu value.
256 if ($sovpnsettings{'MSSFIX'} eq 'on')
257 { print CONF "$sovpnsettings{'DDEVICE'}-mtu 1500\n"; }
258 elsif ($sovpnsettings{'FRAGMENT'} ne '' && $sovpnsettings{'DPROTOCOL'} ne 'tcp')
259 { print CONF "$sovpnsettings{'DDEVICE'}-mtu 1500\n"; }
260 elsif (($sovpnsettings{'PMTU_DISCOVERY'} eq 'yes') ||
261 ($sovpnsettings{'PMTU_DISCOVERY'} eq 'maybe') ||
262 ($sovpnsettings{'PMTU_DISCOVERY'} eq 'no' ))
263 { print CONF "$sovpnsettings{'DDEVICE'}-mtu 1500\n"; }
264 else
265 { print CONF "$sovpnsettings{'DDEVICE'}-mtu $sovpnsettings{'DMTU'}\n"; }
266
267 if ($vpnsettings{'ROUTES_PUSH'} ne '') {
268 @temp = split(/\n/,$vpnsettings{'ROUTES_PUSH'});
269 foreach (@temp)
270 {
271 @tempovpnsubnet = split("\/",&General::ipcidr2msk($_));
272 print CONF "push \"route " . $tempovpnsubnet[0]. " " . $tempovpnsubnet[1] . "\"\n";
273 }
274 }
275 # a.marx ccd
276 my %ccdconfhash=();
277 &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
278 foreach my $key (keys %ccdconfhash) {
279 my $a=$ccdconfhash{$key}[1];
280 my ($b,$c) = split (/\//, $a);
281 print CONF "route $b ".&General::cidrtosub($c)."\n";
282 }
283 my %ccdroutehash=();
284 &General::readhasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
285 foreach my $key (keys %ccdroutehash) {
286 foreach my $i ( 1 .. $#{$ccdroutehash{$key}}){
287 my ($a,$b)=split (/\//,$ccdroutehash{$key}[$i]);
288 print CONF "route $a $b\n";
289 }
290 }
291 # ccd end
292
293 if ($sovpnsettings{CLIENT2CLIENT} eq 'on') {
294 print CONF "client-to-client\n";
295 }
296 if ($sovpnsettings{MSSFIX} eq 'on') {
297 print CONF "mssfix\n";
298 }
299 if ($sovpnsettings{FRAGMENT} ne '' && $sovpnsettings{'DPROTOCOL'} ne 'tcp') {
300 print CONF "fragment $sovpnsettings{'FRAGMENT'}\n";
301 }
302
303 # Check if a valid operating mode has been choosen and use it.
304 if (($sovpnsettings{'PMTU_DISCOVERY'} eq 'yes') ||
305 ($sovpnsettings{'PMTU_DISCOVERY'} eq 'maybe') ||
306 ($sovpnsettings{'PMTU_DISCOVERY'} eq 'no' )) {
307 print CONF "mtu-disc $sovpnsettings{'PMTU_DISCOVERY'}\n";
308 }
309
310 if ($sovpnsettings{KEEPALIVE_1} > 0 && $sovpnsettings{KEEPALIVE_2} > 0) {
311 print CONF "keepalive $sovpnsettings{'KEEPALIVE_1'} $sovpnsettings{'KEEPALIVE_2'}\n";
312 }
313 print CONF "status-version 1\n";
314 print CONF "status /var/log/ovpnserver.log 30\n";
315 print CONF "cipher $sovpnsettings{DCIPHER}\n";
316 if ($sovpnsettings{DCOMPLZO} eq 'on') {
317 print CONF "comp-lzo\n";
318 }
319 if ($sovpnsettings{REDIRECT_GW_DEF1} eq 'on') {
320 print CONF "push \"redirect-gateway def1\"\n";
321 }
322 if ($sovpnsettings{DHCP_DOMAIN} ne '') {
323 print CONF "push \"dhcp-option DOMAIN $sovpnsettings{DHCP_DOMAIN}\"\n";
324 }
325
326 if ($sovpnsettings{DHCP_DNS} ne '') {
327 print CONF "push \"dhcp-option DNS $sovpnsettings{DHCP_DNS}\"\n";
328 }
329
330 if ($sovpnsettings{DHCP_WINS} ne '') {
331 print CONF "push \"dhcp-option WINS $sovpnsettings{DHCP_WINS}\"\n";
332 }
333
334 if ($sovpnsettings{DHCP_WINS} eq '') {
335 print CONF "max-clients 100\n";
336 }
337 if ($sovpnsettings{DHCP_WINS} ne '') {
338 print CONF "max-clients $sovpnsettings{MAX_CLIENTS}\n";
339 }
340 print CONF "tls-verify /usr/lib/openvpn/verify\n";
341 print CONF "crl-verify /var/ipfire/ovpn/crls/cacrl.pem\n";
342 print CONF "user nobody\n";
343 print CONF "group nobody\n";
344 print CONF "persist-key\n";
345 print CONF "persist-tun\n";
346 if ($sovpnsettings{LOG_VERB} ne '') {
347 print CONF "verb $sovpnsettings{LOG_VERB}\n";
348 } else {
349 print CONF "verb 3\n";
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{'DHCP_DOMAIN'} = $cgiparams{'DHCP_DOMAIN'};
730 $vpnsettings{'DHCP_DNS'} = $cgiparams{'DHCP_DNS'};
731 $vpnsettings{'DHCP_WINS'} = $cgiparams{'DHCP_WINS'};
732 $vpnsettings{'ROUTES_PUSH'} = $cgiparams{'ROUTES_PUSH'};
733 $vpnsettings{'PMTU_DISCOVERY'} = $cgiparams{'PMTU_DISCOVERY'};
734 my @temp=();
735
736 if ($cgiparams{'FRAGMENT'} eq '') {
737 delete $vpnsettings{'FRAGMENT'};
738 } else {
739 if ($cgiparams{'FRAGMENT'} !~ /^[0-9]+$/) {
740 $errormessage = "Incorrect value, please insert only numbers.";
741 goto ADV_ERROR;
742 } else {
743 $vpnsettings{'FRAGMENT'} = $cgiparams{'FRAGMENT'};
744 }
745 }
746 if ($cgiparams{'MSSFIX'} ne 'on') {
747 delete $vpnsettings{'MSSFIX'};
748 } else {
749 $vpnsettings{'MSSFIX'} = $cgiparams{'MSSFIX'};
750 }
751
752 if (($cgiparams{'PMTU_DISCOVERY'} eq 'yes') ||
753 ($cgiparams{'PMTU_DISCOVERY'} eq 'maybe') ||
754 ($cgiparams{'PMTU_DISCOVERY'} eq 'no' )) {
755
756 if (($cgiparams{'MSSFIX'} eq 'on') || ($cgiparams{'FRAGMENT'} ne '')) {
757 $errormessage = $Lang::tr{'ovpn mtu-disc with mssfix or fragment'};
758 goto ADV_ERROR;
759 }
760 }
761
762 if ($cgiparams{'DHCP_DOMAIN'} ne ''){
763 unless (&General::validdomainname($cgiparams{'DHCP_DOMAIN'}) || &General::validip($cgiparams{'DHCP_DOMAIN'})) {
764 $errormessage = $Lang::tr{'invalid input for dhcp domain'};
765 goto ADV_ERROR;
766 }
767 }
768 if ($cgiparams{'DHCP_DNS'} ne ''){
769 unless (&General::validfqdn($cgiparams{'DHCP_DNS'}) || &General::validip($cgiparams{'DHCP_DNS'})) {
770 $errormessage = $Lang::tr{'invalid input for dhcp dns'};
771 goto ADV_ERROR;
772 }
773 }
774 if ($cgiparams{'DHCP_WINS'} ne ''){
775 unless (&General::validfqdn($cgiparams{'DHCP_WINS'}) || &General::validip($cgiparams{'DHCP_WINS'})) {
776 $errormessage = $Lang::tr{'invalid input for dhcp wins'};
777 goto ADV_ERROR;
778 }
779 }
780 if ($cgiparams{'ROUTES_PUSH'} ne ''){
781 @temp = split(/\n/,$cgiparams{'ROUTES_PUSH'});
782 undef $vpnsettings{'ROUTES_PUSH'};
783
784 foreach my $tmpip (@temp)
785 {
786 s/^\s+//g; s/\s+$//g;
787
788 if ($tmpip)
789 {
790 $tmpip=~s/\s*$//g;
791 unless (&General::validipandmask($tmpip)) {
792 $errormessage = "$tmpip ".$Lang::tr{'ovpn errmsg invalid ip or mask'};
793 goto ADV_ERROR;
794 }
795 my ($ip, $cidr) = split("\/",&General::ipcidr2msk($tmpip));
796
797 if ($ip eq $netsettings{'GREEN_NETADDRESS'} && $cidr eq $netsettings{'GREEN_NETMASK'}) {
798 $errormessage = $Lang::tr{'ovpn errmsg green already pushed'};
799 goto ADV_ERROR;
800 }
801 # a.marx ccd
802 my %ccdroutehash=();
803 &General::readhasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
804 foreach my $key (keys %ccdroutehash) {
805 foreach my $i (1 .. $#{$ccdroutehash{$key}}) {
806 if ( $ip."/".$cidr eq $ccdroutehash{$key}[$i] ){
807 $errormessage="Route $ip\/$cidr ".$Lang::tr{'ccd err inuse'}." $ccdroutehash{$key}[0]" ;
808 goto ADV_ERROR;
809 }
810 my ($ip2,$cidr2) = split(/\//,$ccdroutehash{$key}[$i]);
811 if (&General::IpInSubnet ($ip,$ip2,$cidr2)){
812 $errormessage="Route $ip\/$cidr ".$Lang::tr{'ccd err inuse'}." $ccdroutehash{$key}[0]" ;
813 goto ADV_ERROR;
814 }
815 }
816 }
817
818 # ccd end
819
820 $vpnsettings{'ROUTES_PUSH'} .= $tmpip."\n";
821 }
822 }
823 &write_routepushfile;
824 undef $vpnsettings{'ROUTES_PUSH'};
825 }
826 else {
827 undef $vpnsettings{'ROUTES_PUSH'};
828 &write_routepushfile;
829 }
830 if ((length($cgiparams{'MAX_CLIENTS'}) == 0) || (($cgiparams{'MAX_CLIENTS'}) < 1 ) || (($cgiparams{'MAX_CLIENTS'}) > 255 )) {
831 $errormessage = $Lang::tr{'invalid input for max clients'};
832 goto ADV_ERROR;
833 }
834 if ($cgiparams{'KEEPALIVE_1'} ne '') {
835 if ($cgiparams{'KEEPALIVE_1'} !~ /^[0-9]+$/) {
836 $errormessage = $Lang::tr{'invalid input for keepalive 1'};
837 goto ADV_ERROR;
838 }
839 }
840 if ($cgiparams{'KEEPALIVE_2'} ne ''){
841 if ($cgiparams{'KEEPALIVE_2'} !~ /^[0-9]+$/) {
842 $errormessage = $Lang::tr{'invalid input for keepalive 2'};
843 goto ADV_ERROR;
844 }
845 }
846 if ($cgiparams{'KEEPALIVE_2'} < ($cgiparams{'KEEPALIVE_1'} * 2)){
847 $errormessage = $Lang::tr{'invalid input for keepalive 1:2'};
848 goto ADV_ERROR;
849 }
850
851 &General::writehash("${General::swroot}/ovpn/settings", \%vpnsettings);
852 &writeserverconf();#hier ok
853 }
854
855 ###
856 # m.a.d net2net
857 ###
858
859 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq 'net' && $cgiparams{'SIDE'} eq 'server')
860 {
861
862 my @remsubnet = split(/\//,$cgiparams{'REMOTE_SUBNET'});
863 my @ovsubnettemp = split(/\./,$cgiparams{'OVPN_SUBNET'});
864 my $ovsubnet = "$ovsubnettemp[0].$ovsubnettemp[1].$ovsubnettemp[2]";
865 my $tunmtu = '';
866
867 unless(-d "${General::swroot}/ovpn/n2nconf/"){mkdir "${General::swroot}/ovpn/n2nconf", 0755 or die "Unable to create dir $!";}
868 unless(-d "${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}"){mkdir "${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}", 0770 or die "Unable to create dir $!";}
869
870 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: $!";
871
872 flock SERVERCONF, 2;
873 print SERVERCONF "# IPFire n2n Open VPN Server Config by ummeegge und m.a.d\n";
874 print SERVERCONF "\n";
875 print SERVERCONF "# User Security\n";
876 print SERVERCONF "user nobody\n";
877 print SERVERCONF "group nobody\n";
878 print SERVERCONF "persist-tun\n";
879 print SERVERCONF "persist-key\n";
880 print SERVERCONF "script-security 2\n";
881 print SERVERCONF "# IP/DNS for remote Server Gateway\n";
882
883 if ($cgiparams{'REMOTE'} ne '') {
884 print SERVERCONF "remote $cgiparams{'REMOTE'}\n";
885 }
886
887 print SERVERCONF "float\n";
888 print SERVERCONF "# IP adresses of the VPN Subnet\n";
889 print SERVERCONF "ifconfig $ovsubnet.1 $ovsubnet.2\n";
890 print SERVERCONF "# Client Gateway Network\n";
891 print SERVERCONF "route $remsubnet[0] $remsubnet[1]\n";
892 print SERVERCONF "# tun Device\n";
893 print SERVERCONF "dev tun\n";
894 print SERVERCONF "# Port and Protokol\n";
895 print SERVERCONF "port $cgiparams{'DEST_PORT'}\n";
896
897 if ($cgiparams{'PROTOCOL'} eq 'tcp') {
898 print SERVERCONF "proto tcp-server\n";
899 print SERVERCONF "# Packet size\n";
900 if ($cgiparams{'MTU'} eq '') {$tunmtu = '1400'} else {$tunmtu = $cgiparams{'MTU'}};
901 print SERVERCONF "tun-mtu $tunmtu\n";
902 }
903
904 if ($cgiparams{'PROTOCOL'} eq 'udp') {
905 print SERVERCONF "proto udp\n";
906 print SERVERCONF "# Paketsize\n";
907 if ($cgiparams{'MTU'} eq '') {$tunmtu = '1500'} else {$tunmtu = $cgiparams{'MTU'}};
908 print SERVERCONF "tun-mtu $tunmtu\n";
909 if ($cgiparams{'FRAGMENT'} ne '') {print SERVERCONF "fragment $cgiparams{'FRAGMENT'}\n";}
910 if ($cgiparams{'MSSFIX'} eq 'on') {print SERVERCONF "mssfix\n"; };
911 }
912
913 # Check if a valid operating mode has been choosen and use it.
914 if (($cgiparams{'PMTU_DISCOVERY'} eq 'yes') ||
915 ($cgiparams{'PMTU_DISCOVERY'} eq 'maybe') ||
916 ($cgiparams{'PMTU_DISCOVERY'} eq 'no' )) {
917 if(($cgiparams{'MSSFIX'} ne 'on') || ($cgiparams{'FRAGMENT'} eq '')) {
918 if($cgiparams{'MTU'} eq '1500') {
919 print SERVERCONF "mtu-disc $cgiparams{'PMTU_DISCOVERY'}\n";
920 }
921 }
922 }
923 print SERVERCONF "# Auth. Server\n";
924 print SERVERCONF "tls-server\n";
925 print SERVERCONF "ca ${General::swroot}/ovpn/ca/cacert.pem\n";
926 print SERVERCONF "cert ${General::swroot}/ovpn/certs/servercert.pem\n";
927 print SERVERCONF "key ${General::swroot}/ovpn/certs/serverkey.pem\n";
928 print SERVERCONF "dh ${General::swroot}/ovpn/ca/dh1024.pem\n";
929 print SERVERCONF "# Cipher\n";
930 print SERVERCONF "cipher AES-256-CBC\n";
931 if ($cgiparams{'COMPLZO'} eq 'on') {
932 print SERVERCONF "# Enable Compression\n";
933 print SERVERCONF "comp-lzo\r\n";
934 }
935 print SERVERCONF "# Debug Level\n";
936 print SERVERCONF "verb 3\n";
937 print SERVERCONF "# Tunnel check\n";
938 print SERVERCONF "keepalive 10 60\n";
939 print SERVERCONF "# Start as daemon\n";
940 print SERVERCONF "daemon $cgiparams{'NAME'}n2n\n";
941 print SERVERCONF "writepid /var/run/$cgiparams{'NAME'}n2n.pid\n";
942 print SERVERCONF "# Activate Management Interface and Port\n";
943 if ($cgiparams{'OVPN_MGMT'} eq '') {print SERVERCONF "management localhost $cgiparams{'DEST_PORT'}\n"}
944 else {print SERVERCONF "management localhost $cgiparams{'OVPN_MGMT'}\n"};
945 close(SERVERCONF);
946
947 }
948
949 ###
950 # m.a.d net2net
951 ###
952
953 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq 'net' && $cgiparams{'SIDE'} eq 'client')
954 {
955 my @ovsubnettemp = split(/\./,$cgiparams{'OVPN_SUBNET'});
956 my $ovsubnet = "$ovsubnettemp[0].$ovsubnettemp[1].$ovsubnettemp[2]";
957 my @remsubnet = split(/\//,$cgiparams{'REMOTE_SUBNET'});
958 my $tunmtu = '';
959
960 unless(-d "${General::swroot}/ovpn/n2nconf/"){mkdir "${General::swroot}/ovpn/n2nconf", 0755 or die "Unable to create dir $!";}
961 unless(-d "${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}"){mkdir "${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}", 0770 or die "Unable to create dir $!";}
962
963 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: $!";
964
965 flock CLIENTCONF, 2;
966 print CLIENTCONF "# IPFire rewritten n2n Open VPN Client Config by ummeegge und m.a.d\n";
967 print CLIENTCONF "#\n";
968 print CLIENTCONF "# User Security\n";
969 print CLIENTCONF "user nobody\n";
970 print CLIENTCONF "group nobody\n";
971 print CLIENTCONF "persist-tun\n";
972 print CLIENTCONF "persist-key\n";
973 print CLIENTCONF "script-security 2\n";
974 print CLIENTCONF "# IP/DNS for remote Server Gateway\n";
975 print CLIENTCONF "remote $cgiparams{'REMOTE'}\n";
976 print CLIENTCONF "float\n";
977 print CLIENTCONF "# IP adresses of the VPN Subnet\n";
978 print CLIENTCONF "ifconfig $ovsubnet.2 $ovsubnet.1\n";
979 print CLIENTCONF "# Server Gateway Network\n";
980 print CLIENTCONF "route $remsubnet[0] $remsubnet[1]\n";
981 print CLIENTCONF "# tun Device\n";
982 print CLIENTCONF "dev tun\n";
983 print CLIENTCONF "# Port and Protokol\n";
984 print CLIENTCONF "port $cgiparams{'DEST_PORT'}\n";
985
986 if ($cgiparams{'PROTOCOL'} eq 'tcp') {
987 print CLIENTCONF "proto tcp-client\n";
988 print CLIENTCONF "# Packet size\n";
989 if ($cgiparams{'MTU'} eq '') {$tunmtu = '1400'} else {$tunmtu = $cgiparams{'MTU'}};
990 print CLIENTCONF "tun-mtu $tunmtu\n";
991 }
992
993 if ($cgiparams{'PROTOCOL'} eq 'udp') {
994 print CLIENTCONF "proto udp\n";
995 print CLIENTCONF "# Paketsize\n";
996 if ($cgiparams{'MTU'} eq '') {$tunmtu = '1500'} else {$tunmtu = $cgiparams{'MTU'}};
997 print CLIENTCONF "tun-mtu $tunmtu\n";
998 if ($cgiparams{'FRAGMENT'} ne '') {print CLIENTCONF "fragment $cgiparams{'FRAGMENT'}\n";}
999 if ($cgiparams{'MSSFIX'} eq 'on') {print CLIENTCONF "mssfix\n"; };
1000 }
1001
1002 # Check if a valid operating mode has been choosen and use it.
1003 if (($cgiparams{'PMTU_DISCOVERY'} eq 'yes') ||
1004 ($cgiparams{'PMTU_DISCOVERY'} eq 'maybe') ||
1005 ($cgiparams{'PMTU_DISCOVERY'} eq 'no' )) {
1006 if(($cgiparams{'MSSFIX'} ne 'on') || ($cgiparams{'FRAGMENT'} eq '')) {
1007 if ($cgiparams{'MTU'} eq '1500') {
1008 print CLIENTCONF "mtu-disc $cgiparams{'PMTU_DISCOVERY'}\n";
1009 }
1010 }
1011 }
1012
1013 print CLIENTCONF "ns-cert-type server\n";
1014 print CLIENTCONF "# Auth. Client\n";
1015 print CLIENTCONF "tls-client\n";
1016 print CLIENTCONF "# Cipher\n";
1017 print CLIENTCONF "cipher AES-256-CBC\n";
1018 print CLIENTCONF "pkcs12 ${General::swroot}/ovpn/certs/$cgiparams{'NAME'}.p12\r\n";
1019 if ($cgiparams{'COMPLZO'} eq 'on') {
1020 print CLIENTCONF "# Enable Compression\n";
1021 print CLIENTCONF "comp-lzo\r\n";
1022 }
1023 print CLIENTCONF "# Debug Level\n";
1024 print CLIENTCONF "verb 3\n";
1025 print CLIENTCONF "# Tunnel check\n";
1026 print CLIENTCONF "keepalive 10 60\n";
1027 print CLIENTCONF "# Start as daemon\n";
1028 print CLIENTCONF "daemon $cgiparams{'NAME'}n2n\n";
1029 print CLIENTCONF "writepid /var/run/$cgiparams{'NAME'}n2n.pid\n";
1030 print CLIENTCONF "# Activate Management Interface and Port\n";
1031 if ($cgiparams{'OVPN_MGMT'} eq '') {print CLIENTCONF "management localhost $cgiparams{'DEST_PORT'}\n"}
1032 else {print CLIENTCONF "management localhost $cgiparams{'OVPN_MGMT'}\n"};
1033 close(CLIENTCONF);
1034
1035 }
1036
1037 ###
1038 ### Save main settings
1039 ###
1040
1041
1042 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq '' && $cgiparams{'KEY'} eq '') {
1043 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1044 #DAN do we really need (to to check) this value? Besides if we listen on blue and orange too,
1045 #DAN this value has to leave.
1046 if ($cgiparams{'ENABLED'} eq 'on'){
1047 unless (&General::validfqdn($cgiparams{'VPN_IP'}) || &General::validip($cgiparams{'VPN_IP'})) {
1048 $errormessage = $Lang::tr{'invalid input for hostname'};
1049 goto SETTINGS_ERROR;
1050 }
1051 }
1052 if ($errormessage) { goto SETTINGS_ERROR; }
1053
1054 if ($cgiparams{'ENABLED'} eq 'on'){
1055 &checkportfw($cgiparams{'DDEST_PORT'},$cgiparams{'DPROTOCOL'});
1056 }
1057 if ($errormessage) { goto SETTINGS_ERROR; }
1058
1059 if (! &General::validipandmask($cgiparams{'DOVPN_SUBNET'})) {
1060 $errormessage = $Lang::tr{'ovpn subnet is invalid'};
1061 goto SETTINGS_ERROR;
1062 }
1063 my @tmpovpnsubnet = split("\/",$cgiparams{'DOVPN_SUBNET'});
1064
1065 if (&General::IpInSubnet ( $netsettings{'RED_ADDRESS'},
1066 $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1067 $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire RED Network $netsettings{'RED_ADDRESS'}";
1068 goto SETTINGS_ERROR;
1069 }
1070
1071 if (&General::IpInSubnet ( $netsettings{'GREEN_ADDRESS'},
1072 $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1073 $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire Green Network $netsettings{'GREEN_ADDRESS'}";
1074 goto SETTINGS_ERROR;
1075 }
1076
1077 if (&General::IpInSubnet ( $netsettings{'BLUE_ADDRESS'},
1078 $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1079 $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire Blue Network $netsettings{'BLUE_ADDRESS'}";
1080 goto SETTINGS_ERROR;
1081 }
1082
1083 if (&General::IpInSubnet ( $netsettings{'ORANGE_ADDRESS'},
1084 $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1085 $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire Orange Network $netsettings{'ORANGE_ADDRESS'}";
1086 goto SETTINGS_ERROR;
1087 }
1088 open(ALIASES, "${General::swroot}/ethernet/aliases") or die 'Unable to open aliases file.';
1089 while (<ALIASES>)
1090 {
1091 chomp($_);
1092 my @tempalias = split(/\,/,$_);
1093 if ($tempalias[1] eq 'on') {
1094 if (&General::IpInSubnet ($tempalias[0] ,
1095 $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1096 $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire alias entry $tempalias[0]";
1097 }
1098 }
1099 }
1100 close(ALIASES);
1101 if ($errormessage ne ''){
1102 goto SETTINGS_ERROR;
1103 }
1104 if ($cgiparams{'ENABLED'} !~ /^(on|off)$/) {
1105 $errormessage = $Lang::tr{'invalid input'};
1106 goto SETTINGS_ERROR;
1107 }
1108 if ((length($cgiparams{'DMTU'})==0) || (($cgiparams{'DMTU'}) < 1000 )) {
1109 $errormessage = $Lang::tr{'invalid mtu input'};
1110 goto SETTINGS_ERROR;
1111 }
1112
1113 unless (&General::validport($cgiparams{'DDEST_PORT'})) {
1114 $errormessage = $Lang::tr{'invalid port'};
1115 goto SETTINGS_ERROR;
1116 }
1117
1118 if ($cgiparams{'DDEST_PORT'} <= 1023) {
1119 $errormessage = $Lang::tr{'ovpn port in root range'};
1120 goto SETTINGS_ERROR;
1121 }
1122
1123 $vpnsettings{'ENABLED_BLUE'} = $cgiparams{'ENABLED_BLUE'};
1124 $vpnsettings{'ENABLED_ORANGE'} =$cgiparams{'ENABLED_ORANGE'};
1125 $vpnsettings{'ENABLED'} = $cgiparams{'ENABLED'};
1126 $vpnsettings{'VPN_IP'} = $cgiparams{'VPN_IP'};
1127 #new settings for daemon
1128 $vpnsettings{'DOVPN_SUBNET'} = $cgiparams{'DOVPN_SUBNET'};
1129 $vpnsettings{'DDEVICE'} = $cgiparams{'DDEVICE'};
1130 $vpnsettings{'DPROTOCOL'} = $cgiparams{'DPROTOCOL'};
1131 $vpnsettings{'DDEST_PORT'} = $cgiparams{'DDEST_PORT'};
1132 $vpnsettings{'DMTU'} = $cgiparams{'DMTU'};
1133 $vpnsettings{'DCOMPLZO'} = $cgiparams{'DCOMPLZO'};
1134 $vpnsettings{'DCIPHER'} = $cgiparams{'DCIPHER'};
1135 #wrtie enable
1136
1137 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");}
1138 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");}
1139 if ( $vpnsettings{'ENABLED'} eq 'on' ) {system("touch ${General::swroot}/ovpn/enable 2>/dev/null");}else{system("unlink ${General::swroot}/ovpn/enable 2>/dev/null");}
1140 #new settings for daemon
1141 &General::writehash("${General::swroot}/ovpn/settings", \%vpnsettings);
1142 &writeserverconf();#hier ok
1143 SETTINGS_ERROR:
1144 ###
1145 ### Reset all step 2
1146 ###
1147 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'reset'} && $cgiparams{'AREUSURE'} eq 'yes') {
1148 my $file = '';
1149 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1150
1151 foreach my $key (keys %confighash) {
1152 if ($confighash{$key}[4] eq 'cert') {
1153 delete $confighash{$cgiparams{'$key'}};
1154 }
1155 }
1156 while ($file = glob("${General::swroot}/ovpn/ca/*")) {
1157 unlink $file
1158 }
1159 while ($file = glob("${General::swroot}/ovpn/certs/*")) {
1160 unlink $file
1161 }
1162 while ($file = glob("${General::swroot}/ovpn/crls/*")) {
1163 unlink $file
1164 }
1165 &cleanssldatabase();
1166 if (open(FILE, ">${General::swroot}/ovpn/caconfig")) {
1167 print FILE "";
1168 close FILE;
1169 }
1170 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1171 #&writeserverconf();
1172 ###
1173 ### Reset all step 1
1174 ###
1175 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'reset'}) {
1176 &Header::showhttpheaders();
1177 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1178 &Header::openbigbox('100%', 'LEFT', '', '');
1179 &Header::openbox('100%', 'LEFT', $Lang::tr{'are you sure'});
1180 print <<END
1181 <table><form method='post'><input type='hidden' name='AREUSURE' value='yes' />
1182 <tr><td align='center'>
1183 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>:
1184 $Lang::tr{'resetting the vpn configuration will remove the root ca, the host certificate and all certificate based connections'}
1185 <tr><td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reset'}' />
1186 <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></td></tr>
1187 </form></table>
1188 END
1189 ;
1190 &Header::closebox();
1191 &Header::closebigbox();
1192 &Header::closepage();
1193 exit (0);
1194
1195 ###
1196 ### Upload CA Certificate
1197 ###
1198 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload ca certificate'}) {
1199 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1200
1201 if ($cgiparams{'CA_NAME'} !~ /^[a-zA-Z0-9]+$/) {
1202 $errormessage = $Lang::tr{'name must only contain characters'};
1203 goto UPLOADCA_ERROR;
1204 }
1205
1206 if (length($cgiparams{'CA_NAME'}) >60) {
1207 $errormessage = $Lang::tr{'name too long'};
1208 goto VPNCONF_ERROR;
1209 }
1210
1211 if ($cgiparams{'CA_NAME'} eq 'ca') {
1212 $errormessage = $Lang::tr{'name is invalid'};
1213 goto UPLOAD_CA_ERROR;
1214 }
1215
1216 # Check if there is no other entry with this name
1217 foreach my $key (keys %cahash) {
1218 if ($cahash{$key}[0] eq $cgiparams{'CA_NAME'}) {
1219 $errormessage = $Lang::tr{'a ca certificate with this name already exists'};
1220 goto UPLOADCA_ERROR;
1221 }
1222 }
1223
1224 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1225 $errormessage = $Lang::tr{'there was no file upload'};
1226 goto UPLOADCA_ERROR;
1227 }
1228 # Move uploaded ca to a temporary file
1229 (my $fh, my $filename) = tempfile( );
1230 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1231 $errormessage = $!;
1232 goto UPLOADCA_ERROR;
1233 }
1234 my $temp = `/usr/bin/openssl x509 -text -in $filename`;
1235 if ($temp !~ /CA:TRUE/i) {
1236 $errormessage = $Lang::tr{'not a valid ca certificate'};
1237 unlink ($filename);
1238 goto UPLOADCA_ERROR;
1239 } else {
1240 move($filename, "${General::swroot}/ovpn/ca/$cgiparams{'CA_NAME'}cert.pem");
1241 if ($? ne 0) {
1242 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1243 unlink ($filename);
1244 goto UPLOADCA_ERROR;
1245 }
1246 }
1247
1248 my $casubject = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/ca/$cgiparams{'CA_NAME'}cert.pem`;
1249 $casubject =~ /Subject: (.*)[\n]/;
1250 $casubject = $1;
1251 $casubject =~ s+/Email+, E+;
1252 $casubject =~ s/ ST=/ S=/;
1253 $casubject = &Header::cleanhtml($casubject);
1254
1255 my $key = &General::findhasharraykey (\%cahash);
1256 $cahash{$key}[0] = $cgiparams{'CA_NAME'};
1257 $cahash{$key}[1] = $casubject;
1258 &General::writehasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1259 # system('/usr/local/bin/ipsecctrl', 'R');
1260
1261 UPLOADCA_ERROR:
1262
1263 ###
1264 ### Display ca certificate
1265 ###
1266 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show ca certificate'}) {
1267 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1268
1269 if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem") {
1270 &Header::showhttpheaders();
1271 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1272 &Header::openbigbox('100%', 'LEFT', '', $errormessage);
1273 &Header::openbox('100%', 'LEFT', "$Lang::tr{'ca certificate'}:");
1274 my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
1275 $output = &Header::cleanhtml($output,"y");
1276 print "<pre>$output</pre>\n";
1277 &Header::closebox();
1278 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1279 &Header::closebigbox();
1280 &Header::closepage();
1281 exit(0);
1282 } else {
1283 $errormessage = $Lang::tr{'invalid key'};
1284 }
1285
1286 ###
1287 ### Download ca certificate
1288 ###
1289 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download ca certificate'}) {
1290 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1291
1292 if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
1293 print "Content-Type: application/octet-stream\r\n";
1294 print "Content-Disposition: filename=$cahash{$cgiparams{'KEY'}}[0]cert.pem\r\n\r\n";
1295 print `/usr/bin/openssl x509 -in ${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
1296 exit(0);
1297 } else {
1298 $errormessage = $Lang::tr{'invalid key'};
1299 }
1300
1301 ###
1302 ### Remove ca certificate (step 2)
1303 ###
1304 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'} && $cgiparams{'AREUSURE'} eq 'yes') {
1305 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1306 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1307
1308 if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
1309 foreach my $key (keys %confighash) {
1310 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`;
1311 if ($test =~ /: OK/) {
1312 # Delete connection
1313 # if ($vpnsettings{'ENABLED'} eq 'on' ||
1314 # $vpnsettings{'ENABLED_BLUE'} eq 'on') {
1315 # system('/usr/local/bin/ipsecctrl', 'D', $key);
1316 # }
1317 unlink ("${General::swroot}/ovpn//certs/$confighash{$key}[1]cert.pem");
1318 unlink ("${General::swroot}/ovpn/certs/$confighash{$key}[1].p12");
1319 delete $confighash{$key};
1320 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1321 # &writeipsecfiles();
1322 }
1323 }
1324 unlink ("${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
1325 delete $cahash{$cgiparams{'KEY'}};
1326 &General::writehasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1327 # system('/usr/local/bin/ipsecctrl', 'R');
1328 } else {
1329 $errormessage = $Lang::tr{'invalid key'};
1330 }
1331 ###
1332 ### Remove ca certificate (step 1)
1333 ###
1334 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'}) {
1335 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1336 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1337
1338 my $assignedcerts = 0;
1339 if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
1340 foreach my $key (keys %confighash) {
1341 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`;
1342 if ($test =~ /: OK/) {
1343 $assignedcerts++;
1344 }
1345 }
1346 if ($assignedcerts) {
1347 &Header::showhttpheaders();
1348 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1349 &Header::openbigbox('100%', 'LEFT', '', $errormessage);
1350 &Header::openbox('100%', 'LEFT', $Lang::tr{'are you sure'});
1351 print <<END
1352 <table><form method='post'><input type='hidden' name='AREUSURE' value='yes' />
1353 <input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />
1354 <tr><td align='center'>
1355 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>: $assignedcerts
1356 $Lang::tr{'connections are associated with this ca. deleting the ca will delete these connections as well.'}
1357 <tr><td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'remove ca certificate'}' />
1358 <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></td></tr>
1359 </form></table>
1360 END
1361 ;
1362 &Header::closebox();
1363 &Header::closebigbox();
1364 &Header::closepage();
1365 exit (0);
1366 } else {
1367 unlink ("${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
1368 delete $cahash{$cgiparams{'KEY'}};
1369 &General::writehasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1370 # system('/usr/local/bin/ipsecctrl', 'R');
1371 }
1372 } else {
1373 $errormessage = $Lang::tr{'invalid key'};
1374 }
1375
1376 ###
1377 ### Display root certificate
1378 ###
1379 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'} ||
1380 $cgiparams{'ACTION'} eq $Lang::tr{'show host certificate'}) {
1381 my $output;
1382 &Header::showhttpheaders();
1383 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1384 &Header::openbigbox('100%', 'LEFT', '', '');
1385 if ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'}) {
1386 &Header::openbox('100%', 'LEFT', "$Lang::tr{'root certificate'}:");
1387 $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/ca/cacert.pem`;
1388 } else {
1389 &Header::openbox('100%', 'LEFT', "$Lang::tr{'host certificate'}:");
1390 $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/certs/servercert.pem`;
1391 }
1392 $output = &Header::cleanhtml($output,"y");
1393 print "<pre>$output</pre>\n";
1394 &Header::closebox();
1395 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1396 &Header::closebigbox();
1397 &Header::closepage();
1398 exit(0);
1399
1400 ###
1401 ### Download root certificate
1402 ###
1403 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download root certificate'}) {
1404 if ( -f "${General::swroot}/ovpn/ca/cacert.pem" ) {
1405 print "Content-Type: application/octet-stream\r\n";
1406 print "Content-Disposition: filename=cacert.pem\r\n\r\n";
1407 print `/usr/bin/openssl x509 -in ${General::swroot}/ovpn/ca/cacert.pem`;
1408 exit(0);
1409 }
1410
1411 ###
1412 ### Download host certificate
1413 ###
1414 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download host certificate'}) {
1415 if ( -f "${General::swroot}/ovpn/certs/servercert.pem" ) {
1416 print "Content-Type: application/octet-stream\r\n";
1417 print "Content-Disposition: filename=servercert.pem\r\n\r\n";
1418 print `/usr/bin/openssl x509 -in ${General::swroot}/ovpn/certs/servercert.pem`;
1419 exit(0);
1420 }
1421 ###
1422 ### Form for generating a root certificate
1423 ###
1424 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'generate root/host certificates'} ||
1425 $cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
1426
1427 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1428 if (-f "${General::swroot}/ovpn/ca/cacert.pem") {
1429 $errormessage = $Lang::tr{'valid root certificate already exists'};
1430 $cgiparams{'ACTION'} = '';
1431 goto ROOTCERT_ERROR;
1432 }
1433
1434 if (($cgiparams{'ROOTCERT_HOSTNAME'} eq '') && -e "${General::swroot}/red/active") {
1435 if (open(IPADDR, "${General::swroot}/red/local-ipaddress")) {
1436 my $ipaddr = <IPADDR>;
1437 close IPADDR;
1438 chomp ($ipaddr);
1439 $cgiparams{'ROOTCERT_HOSTNAME'} = (gethostbyaddr(pack("C4", split(/\./, $ipaddr)), 2))[0];
1440 if ($cgiparams{'ROOTCERT_HOSTNAME'} eq '') {
1441 $cgiparams{'ROOTCERT_HOSTNAME'} = $ipaddr;
1442 }
1443 }
1444 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
1445
1446 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1447 $errormessage = $Lang::tr{'there was no file upload'};
1448 goto ROOTCERT_ERROR;
1449 }
1450
1451 # Move uploaded certificate request to a temporary file
1452 (my $fh, my $filename) = tempfile( );
1453 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1454 $errormessage = $!;
1455 goto ROOTCERT_ERROR;
1456 }
1457
1458 # Create a temporary dirctory
1459 my $tempdir = tempdir( CLEANUP => 1 );
1460
1461 # Extract the CA certificate from the file
1462 my $pid = open(OPENSSL, "|-");
1463 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1464 if ($pid) { # parent
1465 if ($cgiparams{'P12_PASS'} ne '') {
1466 print OPENSSL "$cgiparams{'P12_PASS'}\n";
1467 }
1468 close (OPENSSL);
1469 if ($?) {
1470 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1471 unlink ($filename);
1472 goto ROOTCERT_ERROR;
1473 }
1474 } else { # child
1475 unless (exec ('/usr/bin/openssl', 'pkcs12', '-cacerts', '-nokeys',
1476 '-in', $filename,
1477 '-out', "$tempdir/cacert.pem")) {
1478 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1479 unlink ($filename);
1480 goto ROOTCERT_ERROR;
1481 }
1482 }
1483
1484 # Extract the Host certificate from the file
1485 $pid = open(OPENSSL, "|-");
1486 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1487 if ($pid) { # parent
1488 if ($cgiparams{'P12_PASS'} ne '') {
1489 print OPENSSL "$cgiparams{'P12_PASS'}\n";
1490 }
1491 close (OPENSSL);
1492 if ($?) {
1493 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1494 unlink ($filename);
1495 goto ROOTCERT_ERROR;
1496 }
1497 } else { # child
1498 unless (exec ('/usr/bin/openssl', 'pkcs12', '-clcerts', '-nokeys',
1499 '-in', $filename,
1500 '-out', "$tempdir/hostcert.pem")) {
1501 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1502 unlink ($filename);
1503 goto ROOTCERT_ERROR;
1504 }
1505 }
1506
1507 # Extract the Host key from the file
1508 $pid = open(OPENSSL, "|-");
1509 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1510 if ($pid) { # parent
1511 if ($cgiparams{'P12_PASS'} ne '') {
1512 print OPENSSL "$cgiparams{'P12_PASS'}\n";
1513 }
1514 close (OPENSSL);
1515 if ($?) {
1516 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1517 unlink ($filename);
1518 goto ROOTCERT_ERROR;
1519 }
1520 } else { # child
1521 unless (exec ('/usr/bin/openssl', 'pkcs12', '-nocerts',
1522 '-nodes',
1523 '-in', $filename,
1524 '-out', "$tempdir/serverkey.pem")) {
1525 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1526 unlink ($filename);
1527 goto ROOTCERT_ERROR;
1528 }
1529 }
1530
1531 move("$tempdir/cacert.pem", "${General::swroot}/ovpn/ca/cacert.pem");
1532 if ($? ne 0) {
1533 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1534 unlink ($filename);
1535 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1536 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1537 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1538 goto ROOTCERT_ERROR;
1539 }
1540
1541 move("$tempdir/hostcert.pem", "${General::swroot}/ovpn/certs/servercert.pem");
1542 if ($? ne 0) {
1543 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1544 unlink ($filename);
1545 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1546 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1547 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1548 goto ROOTCERT_ERROR;
1549 }
1550
1551 move("$tempdir/serverkey.pem", "${General::swroot}/ovpn/certs/serverkey.pem");
1552 if ($? ne 0) {
1553 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1554 unlink ($filename);
1555 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1556 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1557 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1558 goto ROOTCERT_ERROR;
1559 }
1560
1561 goto ROOTCERT_SUCCESS;
1562
1563 } elsif ($cgiparams{'ROOTCERT_COUNTRY'} ne '') {
1564
1565 # Validate input since the form was submitted
1566 if ($cgiparams{'ROOTCERT_ORGANIZATION'} eq ''){
1567 $errormessage = $Lang::tr{'organization cant be empty'};
1568 goto ROOTCERT_ERROR;
1569 }
1570 if (length($cgiparams{'ROOTCERT_ORGANIZATION'}) >60) {
1571 $errormessage = $Lang::tr{'organization too long'};
1572 goto ROOTCERT_ERROR;
1573 }
1574 if ($cgiparams{'ROOTCERT_ORGANIZATION'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1575 $errormessage = $Lang::tr{'invalid input for organization'};
1576 goto ROOTCERT_ERROR;
1577 }
1578 if ($cgiparams{'ROOTCERT_HOSTNAME'} eq ''){
1579 $errormessage = $Lang::tr{'hostname cant be empty'};
1580 goto ROOTCERT_ERROR;
1581 }
1582 unless (&General::validfqdn($cgiparams{'ROOTCERT_HOSTNAME'}) || &General::validip($cgiparams{'ROOTCERT_HOSTNAME'})) {
1583 $errormessage = $Lang::tr{'invalid input for hostname'};
1584 goto ROOTCERT_ERROR;
1585 }
1586 if ($cgiparams{'ROOTCERT_EMAIL'} ne '' && (! &General::validemail($cgiparams{'ROOTCERT_EMAIL'}))) {
1587 $errormessage = $Lang::tr{'invalid input for e-mail address'};
1588 goto ROOTCERT_ERROR;
1589 }
1590 if (length($cgiparams{'ROOTCERT_EMAIL'}) > 40) {
1591 $errormessage = $Lang::tr{'e-mail address too long'};
1592 goto ROOTCERT_ERROR;
1593 }
1594 if ($cgiparams{'ROOTCERT_OU'} ne '' && $cgiparams{'ROOTCERT_OU'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1595 $errormessage = $Lang::tr{'invalid input for department'};
1596 goto ROOTCERT_ERROR;
1597 }
1598 if ($cgiparams{'ROOTCERT_CITY'} ne '' && $cgiparams{'ROOTCERT_CITY'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1599 $errormessage = $Lang::tr{'invalid input for city'};
1600 goto ROOTCERT_ERROR;
1601 }
1602 if ($cgiparams{'ROOTCERT_STATE'} ne '' && $cgiparams{'ROOTCERT_STATE'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1603 $errormessage = $Lang::tr{'invalid input for state or province'};
1604 goto ROOTCERT_ERROR;
1605 }
1606 if ($cgiparams{'ROOTCERT_COUNTRY'} !~ /^[A-Z]*$/) {
1607 $errormessage = $Lang::tr{'invalid input for country'};
1608 goto ROOTCERT_ERROR;
1609 }
1610
1611 # Copy the cgisettings to vpnsettings and save the configfile
1612 $vpnsettings{'ROOTCERT_ORGANIZATION'} = $cgiparams{'ROOTCERT_ORGANIZATION'};
1613 $vpnsettings{'ROOTCERT_HOSTNAME'} = $cgiparams{'ROOTCERT_HOSTNAME'};
1614 $vpnsettings{'ROOTCERT_EMAIL'} = $cgiparams{'ROOTCERT_EMAIL'};
1615 $vpnsettings{'ROOTCERT_OU'} = $cgiparams{'ROOTCERT_OU'};
1616 $vpnsettings{'ROOTCERT_CITY'} = $cgiparams{'ROOTCERT_CITY'};
1617 $vpnsettings{'ROOTCERT_STATE'} = $cgiparams{'ROOTCERT_STATE'};
1618 $vpnsettings{'ROOTCERT_COUNTRY'} = $cgiparams{'ROOTCERT_COUNTRY'};
1619 &General::writehash("${General::swroot}/ovpn/settings", \%vpnsettings);
1620
1621 # Replace empty strings with a .
1622 (my $ou = $cgiparams{'ROOTCERT_OU'}) =~ s/^\s*$/\./;
1623 (my $city = $cgiparams{'ROOTCERT_CITY'}) =~ s/^\s*$/\./;
1624 (my $state = $cgiparams{'ROOTCERT_STATE'}) =~ s/^\s*$/\./;
1625
1626 # refresh
1627 #system ('/bin/touch', "${General::swroot}/ovpn/gencanow");
1628
1629 # Create the CA certificate
1630 my $pid = open(OPENSSL, "|-");
1631 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1632 if ($pid) { # parent
1633 print OPENSSL "$cgiparams{'ROOTCERT_COUNTRY'}\n";
1634 print OPENSSL "$state\n";
1635 print OPENSSL "$city\n";
1636 print OPENSSL "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
1637 print OPENSSL "$ou\n";
1638 print OPENSSL "$cgiparams{'ROOTCERT_ORGANIZATION'} CA\n";
1639 print OPENSSL "$cgiparams{'ROOTCERT_EMAIL'}\n";
1640 close (OPENSSL);
1641 if ($?) {
1642 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1643 unlink ("${General::swroot}/ovpn/ca/cakey.pem");
1644 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1645 goto ROOTCERT_ERROR;
1646 }
1647 } else { # child
1648 unless (exec ('/usr/bin/openssl', 'req', '-x509', '-nodes', '-rand', '/proc/interrupts:/proc/net/rt_cache',
1649 '-days', '999999', '-newkey', 'rsa:2048',
1650 '-keyout', "${General::swroot}/ovpn/ca/cakey.pem",
1651 '-out', "${General::swroot}/ovpn/ca/cacert.pem",
1652 '-config',"${General::swroot}/ovpn/openssl/ovpn.cnf")) {
1653 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1654 goto ROOTCERT_ERROR;
1655 }
1656 }
1657
1658 # Create the Host certificate request
1659 $pid = open(OPENSSL, "|-");
1660 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1661 if ($pid) { # parent
1662 print OPENSSL "$cgiparams{'ROOTCERT_COUNTRY'}\n";
1663 print OPENSSL "$state\n";
1664 print OPENSSL "$city\n";
1665 print OPENSSL "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
1666 print OPENSSL "$ou\n";
1667 print OPENSSL "$cgiparams{'ROOTCERT_HOSTNAME'}\n";
1668 print OPENSSL "$cgiparams{'ROOTCERT_EMAIL'}\n";
1669 print OPENSSL ".\n";
1670 print OPENSSL ".\n";
1671 close (OPENSSL);
1672 if ($?) {
1673 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1674 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1675 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
1676 goto ROOTCERT_ERROR;
1677 }
1678 } else { # child
1679 unless (exec ('/usr/bin/openssl', 'req', '-nodes', '-rand', '/proc/interrupts:/proc/net/rt_cache',
1680 '-newkey', 'rsa:1024',
1681 '-keyout', "${General::swroot}/ovpn/certs/serverkey.pem",
1682 '-out', "${General::swroot}/ovpn/certs/serverreq.pem",
1683 '-extensions', 'server',
1684 '-config', "${General::swroot}/ovpn/openssl/ovpn.cnf" )) {
1685 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1686 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1687 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
1688 unlink ("${General::swroot}/ovpn/ca/cakey.pem");
1689 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1690 goto ROOTCERT_ERROR;
1691 }
1692 }
1693
1694 # Sign the host certificate request
1695 system('/usr/bin/openssl', 'ca', '-days', '999999',
1696 '-batch', '-notext',
1697 '-in', "${General::swroot}/ovpn/certs/serverreq.pem",
1698 '-out', "${General::swroot}/ovpn/certs/servercert.pem",
1699 '-extensions', 'server',
1700 '-config', "${General::swroot}/ovpn/openssl/ovpn.cnf");
1701 if ($?) {
1702 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1703 unlink ("${General::swroot}/ovpn/ca/cakey.pem");
1704 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1705 unlink ("${General::swroot}/ovpn/serverkey.pem");
1706 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
1707 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1708 &newcleanssldatabase();
1709 goto ROOTCERT_ERROR;
1710 } else {
1711 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
1712 &deletebackupcert();
1713 }
1714
1715 # Create an empty CRL
1716 system('/usr/bin/openssl', 'ca', '-gencrl',
1717 '-out', "${General::swroot}/ovpn/crls/cacrl.pem",
1718 '-config', "${General::swroot}/ovpn/openssl/ovpn.cnf" );
1719 if ($?) {
1720 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1721 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1722 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1723 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1724 unlink ("${General::swroot}/ovpn/crls/cacrl.pem");
1725 &cleanssldatabase();
1726 goto ROOTCERT_ERROR;
1727 # } else {
1728 # &cleanssldatabase();
1729 }
1730 # Create Diffie Hellmann Parameter
1731 system('/usr/bin/openssl', 'dhparam', '-rand', '/proc/interrupts:/proc/net/rt_cache',
1732 '-out', "${General::swroot}/ovpn/ca/dh1024.pem",
1733 '1024' );
1734 if ($?) {
1735 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1736 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1737 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1738 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1739 unlink ("${General::swroot}/ovpn/crls/cacrl.pem");
1740 unlink ("${General::swroot}/ovpn/ca/dh1024.pem");
1741 &cleanssldatabase();
1742 goto ROOTCERT_ERROR;
1743 # } else {
1744 # &cleanssldatabase();
1745 }
1746 goto ROOTCERT_SUCCESS;
1747 }
1748 ROOTCERT_ERROR:
1749 if ($cgiparams{'ACTION'} ne '') {
1750 &Header::showhttpheaders();
1751 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1752 &Header::openbigbox('100%', 'LEFT', '', '');
1753 if ($errormessage) {
1754 &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
1755 print "<class name='base'>$errormessage";
1756 print "&nbsp;</class>";
1757 &Header::closebox();
1758 }
1759 &Header::openbox('100%', 'LEFT', "$Lang::tr{'generate root/host certificates'}:");
1760 print <<END
1761 <form method='post' enctype='multipart/form-data'>
1762 <table width='100%' border='0' cellspacing='1' cellpadding='0'>
1763 <tr><td width='30%' class='base'>$Lang::tr{'organization name'}:</td>
1764 <td width='35%' class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_ORGANIZATION' value='$cgiparams{'ROOTCERT_ORGANIZATION'}' size='32' /></td>
1765 <td width='35%' colspan='2'>&nbsp;</td></tr>
1766 <tr><td class='base'>$Lang::tr{'ipfires hostname'}:</td>
1767 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_HOSTNAME' value='$cgiparams{'ROOTCERT_HOSTNAME'}' size='32' /></td>
1768 <td colspan='2'>&nbsp;</td></tr>
1769 <tr><td class='base'>$Lang::tr{'your e-mail'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
1770 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_EMAIL' value='$cgiparams{'ROOTCERT_EMAIL'}' size='32' /></td>
1771 <td colspan='2'>&nbsp;</td></tr>
1772 <tr><td class='base'>$Lang::tr{'your department'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
1773 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_OU' value='$cgiparams{'ROOTCERT_OU'}' size='32' /></td>
1774 <td colspan='2'>&nbsp;</td></tr>
1775 <tr><td class='base'>$Lang::tr{'city'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
1776 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_CITY' value='$cgiparams{'ROOTCERT_CITY'}' size='32' /></td>
1777 <td colspan='2'>&nbsp;</td></tr>
1778 <tr><td class='base'>$Lang::tr{'state or province'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
1779 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_STATE' value='$cgiparams{'ROOTCERT_STATE'}' size='32' /></td>
1780 <td colspan='2'>&nbsp;</td></tr>
1781 <tr><td class='base'>$Lang::tr{'country'}:</td>
1782 <td class='base'><select name='ROOTCERT_COUNTRY'>
1783
1784 END
1785 ;
1786 foreach my $country (sort keys %{Countries::countries}) {
1787 print "<option value='$Countries::countries{$country}'";
1788 if ( $Countries::countries{$country} eq $cgiparams{'ROOTCERT_COUNTRY'} ) {
1789 print " selected='selected'";
1790 }
1791 print ">$country</option>";
1792 }
1793 print <<END
1794 </select></td>
1795 <td colspan='2'>&nbsp;</td></tr>
1796 <tr><td>&nbsp;</td>
1797 <td><input type='submit' name='ACTION' value='$Lang::tr{'generate root/host certificates'}' /></td>
1798 <td>&nbsp;</td><td>&nbsp;</td></tr>
1799 <tr><td class='base' colspan='4' align='left'>
1800 <img src='/blob.gif' valign='top' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td></tr>
1801 <tr><td class='base' colspan='4' align='left'>
1802 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>:
1803 $Lang::tr{'generating the root and host certificates may take a long time. it can take up to several minutes on older hardware. please be patient'}
1804 </td></tr>
1805 <tr><td colspan='4' bgcolor='#000000'><img src='/images/null.gif' width='1' height='1' border='0' /></td></tr>
1806 <tr><td class='base' nowrap='nowrap'>$Lang::tr{'upload p12 file'}:</td>
1807 <td nowrap='nowrap'><input type='file' name='FH' size='32'></td>
1808 <td colspan='2'>&nbsp;</td></tr>
1809 <tr><td class='base'>$Lang::tr{'pkcs12 file password'}:&nbsp;<img src='/blob.gif' alt='*' ></td>
1810 <td class='base' nowrap='nowrap'><input type='password' name='P12_PASS' value='$cgiparams{'P12_PASS'}' size='32' /></td>
1811 <td colspan='2'>&nbsp;</td></tr>
1812 <tr><td>&nbsp;</td>
1813 <td><input type='submit' name='ACTION' value='$Lang::tr{'upload p12 file'}' /></td>
1814 <td colspan='2'>&nbsp;</td></tr>
1815 <tr><td class='base' colspan='4' align='left'>
1816 <img src='/blob.gif' valign='top' al='*' >&nbsp;$Lang::tr{'this field may be blank'}</td></tr>
1817 </form></table>
1818 END
1819 ;
1820 &Header::closebox();
1821
1822 &Header::closebigbox();
1823 &Header::closepage();
1824 exit(0)
1825 }
1826
1827 ROOTCERT_SUCCESS:
1828 system ("chmod 600 ${General::swroot}/ovpn/certs/serverkey.pem");
1829 # if ($vpnsettings{'ENABLED'} eq 'on' ||
1830 # $vpnsettings{'ENABLE_BLUE'} eq 'on') {
1831 # system('/usr/local/bin/ipsecctrl', 'S');
1832 # }
1833
1834 ###
1835 ### Enable/Disable connection
1836 ###
1837
1838 ###
1839 # m.a.d net2net
1840 ###
1841
1842 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
1843
1844 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1845 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1846 # my $n2nactive = '';
1847 my $n2nactive = `/bin/ps ax|grep $confighash{$cgiparams{'KEY'}}[1]|grep -v grep|awk \'{print \$1}\'`;
1848
1849 if ($confighash{$cgiparams{'KEY'}}) {
1850 if ($confighash{$cgiparams{'KEY'}}[0] eq 'off') {
1851 $confighash{$cgiparams{'KEY'}}[0] = 'on';
1852 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1853
1854 if ($confighash{$cgiparams{'KEY'}}[3] eq 'net'){
1855 system('/usr/local/bin/openvpnctrl', '-sn2n', $confighash{$cgiparams{'KEY'}}[1]);
1856 }
1857 } else {
1858
1859 $confighash{$cgiparams{'KEY'}}[0] = 'off';
1860 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1861
1862 if ($confighash{$cgiparams{'KEY'}}[3] eq 'net'){
1863 if ($n2nactive ne ''){
1864 system('/usr/local/bin/openvpnctrl', '-kn2n', $confighash{$cgiparams{'KEY'}}[1]);
1865 }
1866
1867 } else {
1868 $errormessage = $Lang::tr{'invalid key'};
1869 }
1870 }
1871 }
1872
1873 ###
1874 ### Download OpenVPN client package
1875 ###
1876
1877
1878 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'dl client arch'}) {
1879 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1880 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1881 my $file = '';
1882 my $clientovpn = '';
1883 my @fileholder;
1884 my $tempdir = tempdir( CLEANUP => 1 );
1885 my $zippath = "$tempdir/";
1886
1887 ###
1888 # m.a.d net2net
1889 ###
1890
1891 if ($confighash{$cgiparams{'KEY'}}[3] eq 'net'){
1892
1893 my $zipname = "$confighash{$cgiparams{'KEY'}}[1]-Client.zip";
1894 my $zippathname = "$zippath$zipname";
1895 $clientovpn = "$confighash{$cgiparams{'KEY'}}[1].conf";
1896 my @ovsubnettemp = split(/\./,$confighash{$cgiparams{'KEY'}}[27]);
1897 my $ovsubnet = "$ovsubnettemp[0].$ovsubnettemp[1].$ovsubnettemp[2]";
1898 my $tunmtu = '';
1899 my @remsubnet = split(/\//,$confighash{$cgiparams{'KEY'}}[8]);
1900 my $n2nfragment = '';
1901
1902 open(CLIENTCONF, ">$tempdir/$clientovpn") or die "Unable to open tempfile: $!";
1903 flock CLIENTCONF, 2;
1904
1905 my $zip = Archive::Zip->new();
1906 print CLIENTCONF "# IPFire n2n Open VPN Client Config by ummeegge und m.a.d\n";
1907 print CLIENTCONF "# \n";
1908 print CLIENTCONF "# User Security\n";
1909 print CLIENTCONF "user nobody\n";
1910 print CLIENTCONF "group nobody\n";
1911 print CLIENTCONF "persist-tun\n";
1912 print CLIENTCONF "persist-key\n";
1913 print CLIENTCONF "script-security 2\n";
1914 print CLIENTCONF "# IP/DNS for remote Server Gateway\n";
1915 print CLIENTCONF "remote $vpnsettings{'VPN_IP'}\n";
1916 print CLIENTCONF "float\n";
1917 print CLIENTCONF "# IP adresses of the VPN Subnet\n";
1918 print CLIENTCONF "ifconfig $ovsubnet.2 $ovsubnet.1\n";
1919 print CLIENTCONF "# Server Gateway Network\n";
1920 print CLIENTCONF "route $remsubnet[0] $remsubnet[1]\n";
1921 print CLIENTCONF "# tun Device\n";
1922 print CLIENTCONF "dev $vpnsettings{'DDEVICE'}\n";
1923 print CLIENTCONF "# Port and Protokoll\n";
1924 print CLIENTCONF "port $confighash{$cgiparams{'KEY'}}[29]\n";
1925
1926 if ($confighash{$cgiparams{'KEY'}}[28] eq 'tcp') {
1927 print CLIENTCONF "proto tcp-client\n";
1928 print CLIENTCONF "# Packet size\n";
1929 if ($confighash{$cgiparams{'KEY'}}[31] eq '') {$tunmtu = '1400'} else {$tunmtu = $confighash{$cgiparams{'KEY'}}[31]};
1930 print CLIENTCONF "tun-mtu $tunmtu\n";
1931 }
1932
1933 if ($confighash{$cgiparams{'KEY'}}[28] eq 'udp') {
1934 print CLIENTCONF "proto udp\n";
1935 print CLIENTCONF "# Paketsize\n";
1936 if ($confighash{$cgiparams{'KEY'}}[31] eq '') {$tunmtu = '1500'} else {$tunmtu = $confighash{$cgiparams{'KEY'}}[31]};
1937 print CLIENTCONF "tun-mtu $tunmtu\n";
1938 if ($confighash{$cgiparams{'KEY'}}[24] ne '') {print CLIENTCONF "fragment $confighash{$cgiparams{'KEY'}}[24]\n";}
1939 if ($confighash{$cgiparams{'KEY'}}[23] eq 'on') {print CLIENTCONF "mssfix\n";}
1940 }
1941 if (($confighash{$cgiparams{'KEY'}}[38] eq 'yes') ||
1942 ($confighash{$cgiparams{'KEY'}}[38] eq 'maybe') ||
1943 ($confighash{$cgiparams{'KEY'}}[38] eq 'no' )) {
1944 if (($confighash{$cgiparams{'KEY'}}[23] ne 'on') || ($confighash{$cgiparams{'KEY'}}[24] eq '')) {
1945 if ($tunmtu eq '1500' ) {
1946 print CLIENTCONF "mtu-disc $confighash{$cgiparams{'KEY'}}[38]\n";
1947 }
1948 }
1949 }
1950 print CLIENTCONF "ns-cert-type server\n";
1951 print CLIENTCONF "# Auth. Client\n";
1952 print CLIENTCONF "tls-client\n";
1953 print CLIENTCONF "# Cipher\n";
1954 print CLIENTCONF "cipher AES-256-CBC\n";
1955 if ($confighash{$cgiparams{'KEY'}}[4] eq 'cert' && -f "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12") {
1956 print CLIENTCONF "pkcs12 ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12\r\n";
1957 $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";
1958 }
1959 if ($confighash{$cgiparams{'KEY'}}[30] eq 'on') {
1960 print CLIENTCONF "# Enable Compression\n";
1961 print CLIENTCONF "comp-lzo\r\n";
1962 }
1963 print CLIENTCONF "# Debug Level\n";
1964 print CLIENTCONF "verb 3\n";
1965 print CLIENTCONF "# Tunnel check\n";
1966 print CLIENTCONF "keepalive 10 60\n";
1967 print CLIENTCONF "# Start as daemon\n";
1968 print CLIENTCONF "daemon $confighash{$cgiparams{'KEY'}}[1]n2n\n";
1969 print CLIENTCONF "writepid /var/run/$confighash{$cgiparams{'KEY'}}[1]n2n.pid\n";
1970 print CLIENTCONF "# Activate Management Interface and Port\n";
1971 if ($confighash{$cgiparams{'KEY'}}[22] eq '') {print CLIENTCONF "management localhost $confighash{$cgiparams{'KEY'}}[29]\n"}
1972 else {print CLIENTCONF "management localhost $confighash{$cgiparams{'KEY'}}[22]\n"};
1973 print CLIENTCONF "# remsub $confighash{$cgiparams{'KEY'}}[11]\n";
1974
1975
1976 close(CLIENTCONF);
1977
1978 $zip->addFile( "$tempdir/$clientovpn", $clientovpn) or die "Can't add file $clientovpn\n";
1979 my $status = $zip->writeToFileNamed($zippathname);
1980
1981 open(DLFILE, "<$zippathname") or die "Unable to open $zippathname: $!";
1982 @fileholder = <DLFILE>;
1983 print "Content-Type:application/x-download\n";
1984 print "Content-Disposition:attachment;filename=$zipname\n\n";
1985 print @fileholder;
1986 exit (0);
1987 }
1988 else
1989 {
1990 my $zipname = "$confighash{$cgiparams{'KEY'}}[1]-TO-IPFire.zip";
1991 my $zippathname = "$zippath$zipname";
1992 $clientovpn = "$confighash{$cgiparams{'KEY'}}[1]-TO-IPFire.ovpn";
1993
1994 ###
1995 # m.a.d net2net
1996 ###
1997
1998 open(CLIENTCONF, ">$tempdir/$clientovpn") or die "Unable to open tempfile: $!";
1999 flock CLIENTCONF, 2;
2000
2001 my $zip = Archive::Zip->new();
2002
2003 print CLIENTCONF "#OpenVPN Client conf\r\n";
2004 print CLIENTCONF "tls-client\r\n";
2005 print CLIENTCONF "client\r\n";
2006 print CLIENTCONF "nobind\r\n";
2007 print CLIENTCONF "dev $vpnsettings{'DDEVICE'}\r\n";
2008 print CLIENTCONF "proto $vpnsettings{'DPROTOCOL'}\r\n";
2009
2010 # Check if we are using fragment, mssfix or mtu-disc and set MTU to 1500
2011 # or use configured value.
2012 if ($vpnsettings{FRAGMENT} ne '' && $vpnsettings{DPROTOCOL} ne 'tcp' )
2013 { print CLIENTCONF "$vpnsettings{'DDEVICE'}-mtu 1500\r\n"; }
2014 elsif ($vpnsettings{MSSFIX} eq 'on')
2015 { print CLIENTCONF "$vpnsettings{'DDEVICE'}-mtu 1500\r\n"; }
2016 elsif (($vpnsettings{'PMTU_DISCOVERY'} eq 'yes') ||
2017 ($vpnsettings{'PMTU_DISCOVERY'} eq 'maybe') ||
2018 ($vpnsettings{'PMTU_DISCOVERY'} eq 'no' ))
2019 { print CLIENTCONF "$vpnsettings{'DDEVICE'}-mtu 1500\r\n"; }
2020 else
2021 { print CLIENTCONF "$vpnsettings{'DDEVICE'}-mtu $vpnsettings{'DMTU'}\r\n"; }
2022
2023 if ( $vpnsettings{'ENABLED'} eq 'on'){
2024 print CLIENTCONF "remote $vpnsettings{'VPN_IP'} $vpnsettings{'DDEST_PORT'}\r\n";
2025 if ( $vpnsettings{'ENABLED_BLUE'} eq 'on' && (&haveBlueNet())){
2026 print CLIENTCONF "#Coment the above line and uncoment the next line, if you want to connect on the Blue interface\r\n";
2027 print CLIENTCONF ";remote $netsettings{'BLUE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2028 }
2029 if ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' && (&haveOrangeNet())){
2030 print CLIENTCONF "#Coment the above line and uncoment the next line, if you want to connect on the Orange interface\r\n";
2031 print CLIENTCONF ";remote $netsettings{'ORANGE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2032 }
2033 } elsif ( $vpnsettings{'ENABLED_BLUE'} eq 'on' && (&haveBlueNet())){
2034 print CLIENTCONF "remote $netsettings{'BLUE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2035 if ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' && (&haveOrangeNet())){
2036 print CLIENTCONF "#Coment the above line and uncoment the next line, if you want to connect on the Orange interface\r\n";
2037 print CLIENTCONF ";remote $netsettings{'ORANGE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2038 }
2039 } elsif ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' && (&haveOrangeNet())){
2040 print CLIENTCONF "remote $netsettings{'ORANGE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2041 }
2042
2043 if ($confighash{$cgiparams{'KEY'}}[4] eq 'cert' && -f "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12") {
2044 print CLIENTCONF "pkcs12 $confighash{$cgiparams{'KEY'}}[1].p12\r\n";
2045 $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";
2046 } else {
2047 print CLIENTCONF "ca cacert.pem\r\n";
2048 print CLIENTCONF "cert $confighash{$cgiparams{'KEY'}}[1]cert.pem\r\n";
2049 print CLIENTCONF "key $confighash{$cgiparams{'KEY'}}[1].key\r\n";
2050 $zip->addFile( "${General::swroot}/ovpn/ca/cacert.pem", "cacert.pem") or die "Can't add file cacert.pem\n";
2051 $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";
2052 }
2053 print CLIENTCONF "cipher $vpnsettings{DCIPHER}\r\n";
2054 if ($vpnsettings{DCOMPLZO} eq 'on') {
2055 print CLIENTCONF "comp-lzo\r\n";
2056 }
2057 print CLIENTCONF "verb 3\r\n";
2058 print CLIENTCONF "ns-cert-type server\r\n";
2059 print CLIENTCONF "tls-remote $vpnsettings{ROOTCERT_HOSTNAME}\r\n";
2060 if ($vpnsettings{MSSFIX} eq 'on') {
2061 print CLIENTCONF "mssfix\r\n";
2062 }
2063 if ($vpnsettings{FRAGMENT} ne '' && $vpnsettings{DPROTOCOL} ne 'tcp' ) {
2064 print CLIENTCONF "fragment $vpnsettings{'FRAGMENT'}\r\n";
2065 }
2066
2067 # Check if a valid operating mode has been choosen and use it.
2068 if (($vpnsettings{'PMTU_DISCOVERY'} eq 'yes') ||
2069 ($vpnsettings{'PMTU_DISCOVERY'} eq 'maybe') ||
2070 ($vpnsettings{'PMTU_DISCOVERY'} eq 'no' )) {
2071 if(($vpnsettings{MSSFIX} ne 'on') || ($vpnsettings{FRAGMENT} eq '')) {
2072 print CLIENTCONF "mtu-disc $vpnsettings{'PMTU_DISCOVERY'}\r\n";
2073 }
2074 }
2075 close(CLIENTCONF);
2076
2077 $zip->addFile( "$tempdir/$clientovpn", $clientovpn) or die "Can't add file $clientovpn\n";
2078 my $status = $zip->writeToFileNamed($zippathname);
2079
2080 open(DLFILE, "<$zippathname") or die "Unable to open $zippathname: $!";
2081 @fileholder = <DLFILE>;
2082 print "Content-Type:application/x-download\n";
2083 print "Content-Disposition:attachment;filename=$zipname\n\n";
2084 print @fileholder;
2085 exit (0);
2086 }
2087
2088
2089
2090 ###
2091 ### Remove connection
2092 ###
2093
2094
2095 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove'}) {
2096 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
2097 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2098
2099 if ($confighash{$cgiparams{'KEY'}}) {
2100 # if ($vpnsettings{'ENABLED'} eq 'on' ||
2101 # $vpnsettings{'ENABLED_BLUE'} eq 'on') {
2102 # system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'});
2103 # }
2104 #
2105 my $temp = `/usr/bin/openssl ca -revoke ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem -config ${General::swroot}/ovpn/openssl/ovpn.cnf`;
2106
2107 ###
2108 # m.a.d net2net
2109 ###
2110
2111 if ($confighash{$cgiparams{'KEY'}}[3] eq 'net') {
2112 my $conffile = glob("${General::swroot}/ovpn/n2nconf/$confighash{$cgiparams{'KEY'}}[1]/$confighash{$cgiparams{'KEY'}}[1].conf");
2113 my $certfile = glob("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12");
2114 unlink ($certfile);
2115 unlink ($conffile);
2116
2117 if (-e "${General::swroot}/ovpn/n2nconf/$confighash{$cgiparams{'KEY'}}[1]") {
2118 rmdir ("${General::swroot}/ovpn/n2nconf/$confighash{$cgiparams{'KEY'}}[1]") || die "Kann Verzeichnis nicht loeschen: $!";
2119 }
2120 }
2121
2122 unlink ("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem");
2123 unlink ("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12");
2124
2125 # A.Marx CCD delete ccd files and routes
2126
2127
2128 if (-f "${General::swroot}/ovpn/ccd/$confighash{$cgiparams{'KEY'}}[2]")
2129 {
2130 unlink "${General::swroot}/ovpn/ccd/$confighash{$cgiparams{'KEY'}}[2]";
2131 }
2132
2133 &General::readhasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
2134 foreach my $key (keys %ccdroutehash) {
2135 if ($ccdroutehash{$key}[0] eq $confighash{$cgiparams{'KEY'}}[1]){
2136 delete $ccdroutehash{$key};
2137 }
2138 }
2139 &General::writehasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
2140
2141 &General::readhasharray("${General::swroot}/ovpn/ccdroute2", \%ccdroute2hash);
2142 foreach my $key (keys %ccdroute2hash) {
2143 if ($ccdroute2hash{$key}[0] eq $confighash{$cgiparams{'KEY'}}[1]){
2144 delete $ccdroute2hash{$key};
2145 }
2146 }
2147 &General::writehasharray("${General::swroot}/ovpn/ccdroute2", \%ccdroute2hash);
2148 &writeserverconf;
2149
2150
2151 # CCD end
2152
2153
2154 delete $confighash{$cgiparams{'KEY'}};
2155 my $temp2 = `/usr/bin/openssl ca -gencrl -out ${General::swroot}/ovpn/crls/cacrl.pem -config ${General::swroot}/ovpn/openssl/ovpn.cnf`;
2156 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2157
2158 #&writeserverconf();
2159 } else {
2160 $errormessage = $Lang::tr{'invalid key'};
2161 }
2162 &General::firewall_reload();
2163
2164 ###
2165 ### Download PKCS12 file
2166 ###
2167 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download pkcs12 file'}) {
2168 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2169
2170 print "Content-Disposition: filename=" . $confighash{$cgiparams{'KEY'}}[1] . ".p12\r\n";
2171 print "Content-Type: application/octet-stream\r\n\r\n";
2172 print `/bin/cat ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12`;
2173 exit (0);
2174
2175 ###
2176 ### Display certificate
2177 ###
2178 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show certificate'}) {
2179 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2180
2181 if ( -f "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem") {
2182 &Header::showhttpheaders();
2183 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
2184 &Header::openbigbox('100%', 'LEFT', '', '');
2185 &Header::openbox('100%', 'LEFT', "$Lang::tr{'certificate'}:");
2186 my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
2187 $output = &Header::cleanhtml($output,"y");
2188 print "<pre>$output</pre>\n";
2189 &Header::closebox();
2190 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2191 &Header::closebigbox();
2192 &Header::closepage();
2193 exit(0);
2194 }
2195 ###
2196 ### Display Certificate Revoke List
2197 ###
2198 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show crl'}) {
2199 # &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2200
2201 if ( -f "${General::swroot}/ovpn/crls/cacrl.pem") {
2202 &Header::showhttpheaders();
2203 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
2204 &Header::openbigbox('100%', 'LEFT', '', '');
2205 &Header::openbox('100%', 'LEFT', "$Lang::tr{'crl'}:");
2206 my $output = `/usr/bin/openssl crl -text -noout -in ${General::swroot}/ovpn/crls/cacrl.pem`;
2207 $output = &Header::cleanhtml($output,"y");
2208 print "<pre>$output</pre>\n";
2209 &Header::closebox();
2210 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2211 &Header::closebigbox();
2212 &Header::closepage();
2213 exit(0);
2214 }
2215
2216 ###
2217 ### Advanced Server Settings
2218 ###
2219
2220 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'advanced server'}) {
2221 %cgiparams = ();
2222 %cahash = ();
2223 %confighash = ();
2224 my $disabled;
2225 &General::readhash("${General::swroot}/ovpn/settings", \%cgiparams);
2226 read_routepushfile;
2227
2228
2229 # if ($cgiparams{'CLIENT2CLIENT'} eq '') {
2230 # $cgiparams{'CLIENT2CLIENT'} = 'on';
2231 # }
2232 ADV_ERROR:
2233 if ($cgiparams{'MAX_CLIENTS'} eq '') {
2234 $cgiparams{'MAX_CLIENTS'} = '100';
2235 }
2236 if ($cgiparams{'KEEPALIVE_1'} eq '') {
2237 $cgiparams{'KEEPALIVE_1'} = '10';
2238 }
2239 if ($cgiparams{'KEEPALIVE_2'} eq '') {
2240 $cgiparams{'KEEPALIVE_2'} = '60';
2241 }
2242 if ($cgiparams{'LOG_VERB'} eq '') {
2243 $cgiparams{'LOG_VERB'} = '3';
2244 }
2245 if ($cgiparams{'PMTU_DISCOVERY'} eq '') {
2246 $cgiparams{'PMTU_DISCOVERY'} = 'off';
2247 }
2248 $checked{'CLIENT2CLIENT'}{'off'} = '';
2249 $checked{'CLIENT2CLIENT'}{'on'} = '';
2250 $checked{'CLIENT2CLIENT'}{$cgiparams{'CLIENT2CLIENT'}} = 'CHECKED';
2251 $checked{'REDIRECT_GW_DEF1'}{'off'} = '';
2252 $checked{'REDIRECT_GW_DEF1'}{'on'} = '';
2253 $checked{'REDIRECT_GW_DEF1'}{$cgiparams{'REDIRECT_GW_DEF1'}} = 'CHECKED';
2254 $selected{'ENGINES'}{$cgiparams{'ENGINES'}} = 'SELECTED';
2255 $checked{'MSSFIX'}{'off'} = '';
2256 $checked{'MSSFIX'}{'on'} = '';
2257 $checked{'MSSFIX'}{$cgiparams{'MSSFIX'}} = 'CHECKED';
2258 $checked{'PMTU_DISCOVERY'}{$cgiparams{'PMTU_DISCOVERY'}} = 'checked=\'checked\'';
2259 $selected{'LOG_VERB'}{'1'} = '';
2260 $selected{'LOG_VERB'}{'2'} = '';
2261 $selected{'LOG_VERB'}{'3'} = '';
2262 $selected{'LOG_VERB'}{'4'} = '';
2263 $selected{'LOG_VERB'}{'5'} = '';
2264 $selected{'LOG_VERB'}{'6'} = '';
2265 $selected{'LOG_VERB'}{'7'} = '';
2266 $selected{'LOG_VERB'}{'8'} = '';
2267 $selected{'LOG_VERB'}{'9'} = '';
2268 $selected{'LOG_VERB'}{'10'} = '';
2269 $selected{'LOG_VERB'}{'11'} = '';
2270 $selected{'LOG_VERB'}{'0'} = '';
2271 $selected{'LOG_VERB'}{$cgiparams{'LOG_VERB'}} = 'SELECTED';
2272
2273 &Header::showhttpheaders();
2274 &Header::openpage($Lang::tr{'status ovpn'}, 1, '');
2275 &Header::openbigbox('100%', 'LEFT', '', $errormessage);
2276 if ($errormessage) {
2277 &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
2278 print "<class name='base'>$errormessage\n";
2279 print "&nbsp;</class>\n";
2280 &Header::closebox();
2281 }
2282 &Header::openbox('100%', 'LEFT', $Lang::tr{'advanced server'});
2283 print <<END
2284 <form method='post' enctype='multipart/form-data'>
2285 <table width='100%' border='0'>
2286 <tr>
2287 <td colspan='4'><b>$Lang::tr{'dhcp-options'}</b></td>
2288 </tr>
2289 <tr>
2290 <td width='25%'></td> <td width='20%'> </td><td width='25%'> </td><td width='30%'></td>
2291 </tr>
2292 <tr>
2293 <td class='base'>Domain</td>
2294 <td><input type='TEXT' name='DHCP_DOMAIN' value='$cgiparams{'DHCP_DOMAIN'}' size='30' /></td>
2295 </tr>
2296 <tr>
2297 <td class='base'>DNS</td>
2298 <td><input type='TEXT' name='DHCP_DNS' value='$cgiparams{'DHCP_DNS'}' size='30' /></td>
2299 </tr>
2300 <tr>
2301 <td class='base'>WINS</td>
2302 <td><input type='TEXT' name='DHCP_WINS' value='$cgiparams{'DHCP_WINS'}' size='30' /></td>
2303 </tr>
2304 <tr>
2305 <td colspan='4'><b>$Lang::tr{'ovpn routes push options'}</b></td>
2306 </tr>
2307 <tr>
2308 <td class='base'>$Lang::tr{'ovpn routes push'}</td>
2309 <td colspan='2'>
2310 <textarea name='ROUTES_PUSH' cols='26' rows='6' wrap='off'>
2311 END
2312 ;
2313
2314 if ($cgiparams{'ROUTES_PUSH'} ne '')
2315 {
2316 print $cgiparams{'ROUTES_PUSH'};
2317 }
2318
2319 print <<END;
2320 </textarea></td>
2321 </tr>
2322 </tr>
2323 </table>
2324 <hr size='1'>
2325 <table width='100%'>
2326 <tr>
2327 <td class'base'><b>$Lang::tr{'misc-options'}</b></td>
2328 </tr>
2329 <tr>
2330 <td width='20%'></td> <td width='15%'> </td><td width='15%'> </td><td width='15%'></td><td width='35%'></td>
2331 </tr>
2332 <tr>
2333 <td class='base'>Client-To-Client</td>
2334 <td><input type='checkbox' name='CLIENT2CLIENT' $checked{'CLIENT2CLIENT'}{'on'} /></td>
2335 </tr>
2336 <tr>
2337 <td class='base'>Redirect-Gateway def1</td>
2338 <td><input type='checkbox' name='REDIRECT_GW_DEF1' $checked{'REDIRECT_GW_DEF1'}{'on'} /></td>
2339 </tr>
2340 <tr>
2341 <td class='base'>Max-Clients</td>
2342 <td><input type='text' name='MAX_CLIENTS' value='$cgiparams{'MAX_CLIENTS'}' size='10' /></td>
2343 </tr>
2344 <tr>
2345 <td class='base'>Keepalive <br />
2346 (ping/ping-restart)</td>
2347 <td><input type='TEXT' name='KEEPALIVE_1' value='$cgiparams{'KEEPALIVE_1'}' size='10' /></td>
2348 <td><input type='TEXT' name='KEEPALIVE_2' value='$cgiparams{'KEEPALIVE_2'}' size='10' /></td>
2349 </tr>
2350 <tr>
2351 <td class='base'>fragment <br></td>
2352 <td><input type='TEXT' name='FRAGMENT' value='$cgiparams{'FRAGMENT'}' size='10' /></td>
2353 <td>$Lang::tr{'openvpn default'}: <span class="base">1300</span></td>
2354 </tr>
2355 <tr>
2356 <td class='base'>mssfix</td>
2357 <td><input type='checkbox' name='MSSFIX' $checked{'MSSFIX'}{'on'} /></td>
2358 <td>$Lang::tr{'openvpn default'}: on</td>
2359 </tr>
2360
2361 <tr>
2362 <td class='base'>$Lang::tr{'ovpn mtu-disc'}</td>
2363 <td><input type='radio' name='PMTU_DISCOVERY' value='yes' $checked{'PMTU_DISCOVERY'}{'yes'} /> $Lang::tr{'ovpn mtu-disc yes'}</td>
2364 <td><input type='radio' name='PMTU_DISCOVERY' value='maybe' $checked{'PMTU_DISCOVERY'}{'maybe'} /> $Lang::tr{'ovpn mtu-disc maybe'}</td>
2365 <td><input type='radio' name='PMTU_DISCOVERY' value='no' $checked{'PMTU_DISCOVERY'}{'no'} /> $Lang::tr{'ovpn mtu-disc no'}</td>
2366 <td><input type='radio' name='PMTU_DISCOVERY' value='off' $checked{'PMTU_DISCOVERY'}{'off'} /> $Lang::tr{'ovpn mtu-disc off'}</td>
2367 </tr>
2368 </table>
2369
2370 <!--
2371 <hr size='1'>
2372 <table width='100%'>
2373 <tr>
2374 <td class'base'><b>Crypto-Engines</b></td>
2375 </tr>
2376 <tr>
2377 <td width='15%'></td> <td width='30%'> </td><td width='25%'> </td><td width='30%'></td>
2378 </tr>
2379 <tr><td class='base'>Engines:</td>
2380 <td><select name='ENGINES'><option value="none" $selected{'ENGINES'}{'none'}>none</option>
2381 <option value="cryptodev" $selected{'ENGINES'}{'cryptodev'}>cryptodev</option>
2382 <option value="padlock" $selected{'ENGINES'}{'padlock'}>padlock</option>
2383 </select>
2384 </td>
2385 </table>
2386 -->
2387 <hr size='1'>
2388 <table width='100%'>
2389 <tr>
2390 <td class'base'><b>$Lang::tr{'log-options'}</b></td>
2391 </tr>
2392 <tr>
2393 <td width='15%'></td> <td width='30%'> </td><td width='25%'> </td><td width='30%'></td>
2394 </tr>
2395
2396 <tr><td class='base'>VERB</td>
2397 <td><select name='LOG_VERB'><option value='1' $selected{'LOG_VERB'}{'1'}>1</option>
2398 <option value='2' $selected{'LOG_VERB'}{'2'}>2</option>
2399 <option value='3' $selected{'LOG_VERB'}{'3'}>3</option>
2400 <option value='4' $selected{'LOG_VERB'}{'4'}>4</option>
2401 <option value='5' $selected{'LOG_VERB'}{'5'}>5</option>
2402 <option value='6' $selected{'LOG_VERB'}{'6'}>6</option>
2403 <option value='7' $selected{'LOG_VERB'}{'7'}>7</option>
2404 <option value='8' $selected{'LOG_VERB'}{'8'}>8</option>
2405 <option value='9' $selected{'LOG_VERB'}{'9'}>9</option>
2406 <option value='10' $selected{'LOG_VERB'}{'10'}>10</option>
2407 <option value='11' $selected{'LOG_VERB'}{'11'}>11</option>
2408 <option value='0' $selected{'LOG_VERB'}{'0'}>0</option></select></td>
2409 </table><hr>
2410 END
2411
2412 if ( -e "/var/run/openvpn.pid"){
2413 print" <br><b><font color='#990000'>$Lang::tr{'attention'}:</b></font><br>
2414 $Lang::tr{'server restart'}<br><br>
2415 <hr>";
2416 print<<END
2417 <table width='100%'>
2418 <tr>
2419 <td>&nbsp;</td>
2420 <td allign='center'><input type='submit' name='ACTION' value='$Lang::tr{'save-adv-options'}' disabled='disabled' /></td>
2421 <td allign='center'><input type='submit' name='ACTION' value='$Lang::tr{'cancel-adv-options'}' /></td>
2422 <td>&nbsp;</td>
2423 </tr>
2424 </table>
2425 </form>
2426 END
2427 ;
2428
2429
2430 }else{
2431
2432 print<<END
2433 <table width='100%'>
2434 <tr>
2435 <td>&nbsp;</td>
2436 <td allign='center'><input type='submit' name='ACTION' value='$Lang::tr{'save-adv-options'}' /></td>
2437 <td allign='center'><input type='submit' name='ACTION' value='$Lang::tr{'cancel-adv-options'}' /></td>
2438 <td>&nbsp;</td>
2439 </tr>
2440 </table>
2441 </form>
2442 END
2443 ;
2444 }
2445 &Header::closebox();
2446 # print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2447 &Header::closebigbox();
2448 &Header::closepage();
2449 exit(0);
2450
2451
2452 # A.Marx CCD Add,delete or edit CCD net
2453
2454 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'ccd net'} ||
2455 $cgiparams{'ACTION'} eq $Lang::tr{'ccd add'} ||
2456 $cgiparams{'ACTION'} eq "kill" ||
2457 $cgiparams{'ACTION'} eq "edit" ||
2458 $cgiparams{'ACTION'} eq 'editsave'){
2459 &Header::showhttpheaders();
2460 &Header::openpage($Lang::tr{'ccd net'}, 1, '');
2461 &Header::openbigbox('100%', 'LEFT', '', '');
2462
2463 if ($cgiparams{'ACTION'} eq "kill"){
2464 &delccdnet($cgiparams{'net'});
2465 }
2466
2467 if ($cgiparams{'ACTION'} eq 'editsave'){
2468 my ($a,$b) =split (/\|/,$cgiparams{'ccdname'});
2469 if ( $a ne $b){ &modccdnet($a,$b);}
2470 $cgiparams{'ccdname'}='';
2471 $cgiparams{'ccdsubnet'}='';
2472 }
2473
2474 if ($cgiparams{'ACTION'} eq $Lang::tr{'ccd add'}) {
2475 &addccdnet($cgiparams{'ccdname'},$cgiparams{'ccdsubnet'});
2476 }
2477 if ($errormessage) {
2478 &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
2479 print "<class name='base'>$errormessage";
2480 print "&nbsp;</class>";
2481 &Header::closebox();
2482 }
2483 if ($cgiparams{'ACTION'} eq "edit"){
2484
2485 &Header::openbox('100%', 'LEFT', $Lang::tr{'ccd modify'});
2486
2487 print <<END
2488 <table width='100%' border='0'>
2489 <tr><form method='post'>
2490 <td width='10%' nowrap='nowrap'>$Lang::tr{'ccd name'}:</td><td><input type='TEXT' name='ccdname' value='$cgiparams{'ccdname'}' /></td>
2491 <td width='8%'>$Lang::tr{'ccd subnet'}:</td><td><input type='TEXT' name='ccdsubnet' value='$cgiparams{'ccdsubnet'}' readonly='readonly' /></td></tr>
2492 <tr><td colspan='4' align='right'><hr><input type='submit' value='$Lang::tr{'save'}' /><input type='hidden' name='ACTION' value='editsave'/>
2493 <input type='hidden' name='ccdname' value='$cgiparams{'ccdname'}'/><input type='submit' value='$Lang::tr{'cancel'}' />
2494 </td></tr>
2495 </table></form>
2496 END
2497 ;
2498 &Header::closebox();
2499
2500 &Header::openbox('100%', 'LEFT',$Lang::tr{'ccd net'} );
2501 print <<END
2502 <table width='100%' border='0' cellpadding='0' cellspacing='1'>
2503 <tr>
2504 <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>
2505 END
2506 ;
2507 }
2508 else{
2509 if (! -e "/var/run/openvpn.pid"){
2510 &Header::openbox('100%', 'LEFT', $Lang::tr{'ccd add'});
2511 print <<END;
2512 <table width='100%' border='0'>
2513 <tr><form method='post'>
2514 <td colspan='4'>$Lang::tr{'ccd hint'}<br><br></td></tr>
2515 <tr>
2516 <td width='10%' nowrap='nwrap'>$Lang::tr{'ccd name'}:</td><td><input type='TEXT' name='ccdname' value='$cgiparams{'ccdname'}' /></td>
2517 <td width='8%'>$Lang::tr{'ccd subnet'}:</td><td><input type='TEXT' name='ccdsubnet' value='$cgiparams{'ccdsubnet'}' /></td></tr>
2518 <tr><td colspan=4><hr /></td></tr><tr>
2519 <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>
2520 </table></form>
2521 END
2522
2523 &Header::closebox();
2524 }
2525 &Header::openbox('100%', 'LEFT',$Lang::tr{'ccd net'} );
2526 if ( -e "/var/run/openvpn.pid"){
2527 print "<b>$Lang::tr{'attention'}:</b><br>";
2528 print "$Lang::tr{'ccd noaddnet'}<br><hr>";
2529 }
2530
2531 print <<END
2532 <table width='100%' cellpadding='0' cellspacing='1'>
2533 <tr>
2534 <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>
2535 END
2536 ;
2537 }
2538 my %ccdconfhash=();
2539 &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
2540 my @ccdconf=();
2541 my $count=0;
2542 foreach my $key (sort { uc($ccdconfhash{$a}[0]) cmp uc($ccdconfhash{$b}[0]) } keys %ccdconfhash) {
2543 @ccdconf=($ccdconfhash{$key}[0],$ccdconfhash{$key}[1]);
2544 $count++;
2545 my $ccdhosts = &hostsinnet($ccdconf[0]);
2546 if ($count % 2){ print" <tr bgcolor='$color{'color22'}'>";}
2547 else{ print" <tr bgcolor='$color{'color20'}'>";}
2548 print"<td>$ccdconf[0]</td><td align='center'>$ccdconf[1]</td><td align='center'>$ccdhosts/".(&ccdmaxc