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