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