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