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