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