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