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