IPsec: Set default inactivity timeout to half an hour
[ipfire-2.x.git] / html / cgi-bin / vpnmain.cgi
CommitLineData
ac1cfefa 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
4a29f854 5# Copyright (C) 2007-2013 IPFire Team info@ipfire.org #
70df8302
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
ac1cfefa
MT
21
22use Net::DNS;
23use File::Copy;
24use File::Temp qw/ tempfile tempdir /;
25use strict;
eff2dbf8 26use Sort::Naturally;
ac1cfefa 27# enable only the following on debugging purpose
cb5e9c6c
CS
28#use warnings;
29#use CGI::Carp 'fatalsToBrowser';
ac1cfefa 30
986e08d9 31require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
32require "${General::swroot}/lang.pl";
33require "${General::swroot}/header.pl";
ac1cfefa
MT
34require "${General::swroot}/countries.pl";
35
36#workaround to suppress a warning when a variable is used only once
ed84e8b8 37my @dummy = ( ${Header::colourgreen}, ${Header::colourblue} );
ac1cfefa
MT
38undef (@dummy);
39
40###
41### Initialize variables
42###
624615ee 43my $sleepDelay = 4; # after a call to ipsecctrl S or R, wait this delay (seconds) before reading status (let the ipsec do its job)
ac1cfefa 44my %netsettings=();
ed84e8b8
MT
45our %cgiparams=();
46our %vpnsettings=();
ac1cfefa
MT
47my %checked=();
48my %confighash=();
49my %cahash=();
50my %selected=();
51my $warnmessage = '';
52my $errormessage = '';
ed84e8b8 53
f2fdd0c1
CS
54my %color = ();
55my %mainsettings = ();
56&General::readhash("${General::swroot}/main/settings", \%mainsettings);
57&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
58
ac1cfefa 59&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
e897bfeb 60
af183eeb
MT
61my %INACTIVITY_TIMEOUTS = (
62 300 => $Lang::tr{'five minutes'},
63 600 => $Lang::tr{'ten minutes'},
64 900 => $Lang::tr{'fifteen minutes'},
65 1800 => $Lang::tr{'thirty minutes'},
66 3600 => $Lang::tr{'one hour'},
67 43200 => $Lang::tr{'twelve hours'},
68 86400 => $Lang::tr{'24 hours'},
69 0 => "- $Lang::tr{'unlimited'} -",
70);
71
e9850821
AM
72my $col="";
73
ac1cfefa 74$cgiparams{'ENABLED'} = 'off';
ac1cfefa 75$cgiparams{'EDIT_ADVANCED'} = 'off';
ac1cfefa
MT
76$cgiparams{'ACTION'} = '';
77$cgiparams{'CA_NAME'} = '';
ed84e8b8
MT
78$cgiparams{'KEY'} = '';
79$cgiparams{'TYPE'} = '';
80$cgiparams{'ADVANCED'} = '';
ed84e8b8
MT
81$cgiparams{'NAME'} = '';
82$cgiparams{'LOCAL_SUBNET'} = '';
83$cgiparams{'REMOTE_SUBNET'} = '';
84$cgiparams{'REMOTE'} = '';
85$cgiparams{'LOCAL_ID'} = '';
86$cgiparams{'REMOTE_ID'} = '';
87$cgiparams{'REMARK'} = '';
88$cgiparams{'PSK'} = '';
89$cgiparams{'CERT_NAME'} = '';
90$cgiparams{'CERT_EMAIL'} = '';
91$cgiparams{'CERT_OU'} = '';
92$cgiparams{'CERT_ORGANIZATION'} = '';
93$cgiparams{'CERT_CITY'} = '';
94$cgiparams{'CERT_STATE'} = '';
95$cgiparams{'CERT_COUNTRY'} = '';
96$cgiparams{'SUBJECTALTNAME'} = '';
97$cgiparams{'CERT_PASS1'} = '';
98$cgiparams{'CERT_PASS2'} = '';
99$cgiparams{'ROOTCERT_HOSTNAME'} = '';
100$cgiparams{'ROOTCERT_COUNTRY'} = '';
101$cgiparams{'P12_PASS'} = '';
102$cgiparams{'ROOTCERT_ORGANIZATION'} = '';
103$cgiparams{'ROOTCERT_HOSTNAME'} = '';
104$cgiparams{'ROOTCERT_EMAIL'} = '';
105$cgiparams{'ROOTCERT_OU'} = '';
106$cgiparams{'ROOTCERT_CITY'} = '';
107$cgiparams{'ROOTCERT_STATE'} = '';
9d85ac3b 108$cgiparams{'RW_NET'} = '';
4e156911
AM
109$cgiparams{'DPD_DELAY'} = '30';
110$cgiparams{'DPD_TIMEOUT'} = '120';
f6529a04 111$cgiparams{'FORCE_MOBIKE'} = 'off';
1e9457ac 112$cgiparams{'START_ACTION'} = 'route';
8ebe7254 113$cgiparams{'INACTIVITY_TIMEOUT'} = 1800;
29f5e0e2 114$cgiparams{'MODE'} = "tunnel";
cae1f4a7 115$cgiparams{'INTERFACE_MODE'} = "";
ac1cfefa
MT
116&Header::getcgihash(\%cgiparams, {'wantfile' => 1, 'filevar' => 'FH'});
117
118###
119### Useful functions
120###
121sub valid_dns_host {
122 my $hostname = $_[0];
123 unless ($hostname) { return "No hostname"};
124 my $res = new Net::DNS::Resolver;
125 my $query = $res->search("$hostname");
126 if ($query) {
127 foreach my $rr ($query->answer) {
128 ## Potential bug - we are only looking at A records:
129 return 0 if $rr->type eq "A";
130 }
131 } else {
132 return $res->errorstring;
133 }
134}
ed84e8b8
MT
135###
136### Just return true is one interface is vpn enabled
137###
138sub vpnenabled {
624615ee 139 return ($vpnsettings{'ENABLED'} eq 'on');
ed84e8b8
MT
140}
141###
624615ee
LS
142### old version: maintain serial number to one, without explication.
143### this: let the counter go, so that each cert is numbered.
ed84e8b8 144###
624615ee
LS
145sub cleanssldatabase {
146 if (open(FILE, ">${General::swroot}/certs/serial")) {
147 print FILE "01";
148 close FILE;
149 }
150 if (open(FILE, ">${General::swroot}/certs/index.txt")) {
151 print FILE "";
152 close FILE;
153 }
e6f7f8e7
EK
154 if (open(FILE, ">${General::swroot}/certs/index.txt.attr")) {
155 print FILE "";
156 close FILE;
157 }
624615ee 158 unlink ("${General::swroot}/certs/index.txt.old");
e6f7f8e7 159 unlink ("${General::swroot}/certs/index.txt.attr.old");
624615ee
LS
160 unlink ("${General::swroot}/certs/serial.old");
161 unlink ("${General::swroot}/certs/01.pem");
ac1cfefa 162}
624615ee
LS
163sub newcleanssldatabase {
164 if (! -s "${General::swroot}/certs/serial" ) {
165 open(FILE, ">${General::swroot}/certs/serial");
166 print FILE "01";
167 close FILE;
168 }
169 if (! -s ">${General::swroot}/certs/index.txt") {
170 system ("touch ${General::swroot}/certs/index.txt");
171 }
e6f7f8e7
EK
172 if (! -s ">${General::swroot}/certs/index.txt.attr") {
173 system ("touch ${General::swroot}/certs/index.txt.attr");
174 }
624615ee 175 unlink ("${General::swroot}/certs/index.txt.old");
e6f7f8e7 176 unlink ("${General::swroot}/certs/index.txt.attr.old");
624615ee
LS
177 unlink ("${General::swroot}/certs/serial.old");
178# unlink ("${General::swroot}/certs/01.pem"); numbering evolves. Wrong place to delete
ac1cfefa 179}
ed84e8b8
MT
180
181###
182### Call openssl and return errormessage if any
183###
184sub callssl ($) {
624615ee
LS
185 my $opt = shift;
186 my $retssl = `/usr/bin/openssl $opt 2>&1`; #redirect stderr
187 my $ret = '';
188 foreach my $line (split (/\n/, $retssl)) {
189 &General::log("ipsec", "$line") if (0); # 1 for verbose logging
190 $ret .= '<br>'.$line if ( $line =~ /error|unknown/ );
191 }
192 if ($ret) {
193 $ret= &Header::cleanhtml($ret);
194 }
195 return $ret ? "$Lang::tr{'openssl produced an error'}: $ret" : '' ;
ed84e8b8
MT
196}
197###
198### Obtain a CN from given cert
199###
200sub getCNfromcert ($) {
624615ee
LS
201 #&General::log("ipsec", "Extracting name from $_[0]...");
202 my $temp = `/usr/bin/openssl x509 -text -in $_[0]`;
568a227b 203 $temp =~ /Subject:.*CN = (.*)[\n]/;
624615ee
LS
204 $temp = $1;
205 $temp =~ s+/Email+, E+;
568a227b 206 $temp =~ s/ ST = / S = /;
624615ee
LS
207 $temp =~ s/,//g;
208 $temp =~ s/\'//g;
209 return $temp;
ed84e8b8
MT
210}
211###
212### Obtain Subject from given cert
213###
214sub getsubjectfromcert ($) {
624615ee
LS
215 #&General::log("ipsec", "Extracting subject from $_[0]...");
216 my $temp = `/usr/bin/openssl x509 -text -in $_[0]`;
217 $temp =~ /Subject: (.*)[\n]/;
218 $temp = $1;
219 $temp =~ s+/Email+, E+;
568a227b 220 $temp =~ s/ ST = / S = /;
624615ee 221 return $temp;
ed84e8b8
MT
222}
223###
624615ee 224### Combine local subnet and connection name to make a unique name for each connection section
ed84e8b8
MT
225### (this sub is not used now)
226###
227sub makeconnname ($) {
624615ee
LS
228 my $conn = shift;
229 my $subnet = shift;
230
231 $subnet =~ /^(.*?)\/(.*?)$/; # $1=IP $2=mask
232 my $ip = unpack('N', &Socket::inet_aton($1));
233 if (length ($2) > 2) {
234 my $mm = unpack('N', &Socket::inet_aton($2));
235 while ( ($mm & 1)==0 ) {
236 $ip >>= 1;
237 $mm >>= 1;
238 };
239 } else {
240 $ip >>= (32 - $2);
241 }
242 return sprintf ("%s-%X", $conn, $ip);
ed84e8b8
MT
243}
244###
245### Write a config file.
246###
247###Type=Host : GUI can choose the interface used (RED,GREEN,BLUE) and
248### the side is always defined as 'left'.
ed84e8b8 249###
ed84e8b8 250
ac1cfefa 251sub writeipsecfiles {
624615ee
LS
252 my %lconfighash = ();
253 my %lvpnsettings = ();
254 &General::readhasharray("${General::swroot}/vpn/config", \%lconfighash);
255 &General::readhash("${General::swroot}/vpn/settings", \%lvpnsettings);
256
257 open(CONF, ">${General::swroot}/vpn/ipsec.conf") or die "Unable to open ${General::swroot}/vpn/ipsec.conf: $!";
258 open(SECRETS, ">${General::swroot}/vpn/ipsec.secrets") or die "Unable to open ${General::swroot}/vpn/ipsec.secrets: $!";
259 flock CONF, 2;
260 flock SECRETS, 2;
261 print CONF "version 2\n\n";
262 print CONF "conn %default\n";
263 print CONF "\tkeyingtries=%forever\n";
264 print CONF "\n";
265
266 # Add user includes to config file
267 if (-e "/etc/ipsec.user.conf") {
268 print CONF "include /etc/ipsec.user.conf\n";
269 print CONF "\n";
ed84e8b8 270 }
e8b3bb0e 271
624615ee 272 print SECRETS "include /etc/ipsec.user.secrets\n";
4b02b404 273
624615ee
LS
274 if (-f "${General::swroot}/certs/hostkey.pem") {
275 print SECRETS ": RSA ${General::swroot}/certs/hostkey.pem\n"
276 }
277 my $last_secrets = ''; # old the less specifics connections
278
279 foreach my $key (keys %lconfighash) {
280 next if ($lconfighash{$key}[0] ne 'on');
281
282 #remote peer is not set? => use '%any'
283 $lconfighash{$key}[10] = '%any' if ($lconfighash{$key}[10] eq '');
284
285 my $localside;
286 if ($lconfighash{$key}[26] eq 'BLUE') {
287 $localside = $netsettings{'BLUE_ADDRESS'};
288 } elsif ($lconfighash{$key}[26] eq 'GREEN') {
289 $localside = $netsettings{'GREEN_ADDRESS'};
290 } elsif ($lconfighash{$key}[26] eq 'ORANGE') {
291 $localside = $netsettings{'ORANGE_ADDRESS'};
292 } else { # it is RED
293 $localside = $lvpnsettings{'VPN_IP'};
4b02b404 294 }
e8b3bb0e 295
624615ee
LS
296 print CONF "conn $lconfighash{$key}[1]\n";
297 print CONF "\tleft=$localside\n";
b1881251 298 print CONF "\tleftsubnet=" . &make_subnets($lconfighash{$key}[8]) . "\n";
624615ee
LS
299 print CONF "\tleftfirewall=yes\n";
300 print CONF "\tlefthostaccess=yes\n";
301 print CONF "\tright=$lconfighash{$key}[10]\n";
302
303 if ($lconfighash{$key}[3] eq 'net') {
b1881251 304 print CONF "\trightsubnet=" . &make_subnets($lconfighash{$key}[11]) . "\n";
624615ee 305 }
e8b3bb0e 306
624615ee
LS
307 # Local Cert and Remote Cert (unless auth is DN dn-auth)
308 if ($lconfighash{$key}[4] eq 'cert') {
309 print CONF "\tleftcert=${General::swroot}/certs/hostcert.pem\n";
310 print CONF "\trightcert=${General::swroot}/certs/$lconfighash{$key}[1]cert.pem\n" if ($lconfighash{$key}[2] ne '%auth-dn');
ed84e8b8 311 }
ed84e8b8 312
624615ee
LS
313 # Local and Remote IDs
314 print CONF "\tleftid=\"$lconfighash{$key}[7]\"\n" if ($lconfighash{$key}[7]);
315 print CONF "\trightid=\"$lconfighash{$key}[9]\"\n" if ($lconfighash{$key}[9]);
126246a8 316
326728d5
MT
317 # Set mode
318 if ($lconfighash{$key}[35] eq "transport") {
319 print CONF "\ttype=transport\n";
320 } else {
321 print CONF "\ttype=tunnel\n";
322 }
323
624615ee
LS
324 # Is PFS enabled?
325 my $pfs = $lconfighash{$key}[28] eq 'on' ? 'on' : 'off';
ed84e8b8 326
624615ee
LS
327 # Algorithms
328 if ($lconfighash{$key}[18] && $lconfighash{$key}[19] && $lconfighash{$key}[20]) {
329 my @encs = split('\|', $lconfighash{$key}[18]);
330 my @ints = split('\|', $lconfighash{$key}[19]);
331 my @groups = split('\|', $lconfighash{$key}[20]);
ed84e8b8 332
624615ee
LS
333 my @algos = &make_algos("ike", \@encs, \@ints, \@groups, 1);
334 print CONF "\tike=" . join(",", @algos);
335
336 if ($lconfighash{$key}[24] eq 'on') { #only proposed algorythms?
337 print CONF "!\n";
338 } else {
339 print CONF "\n";
340 }
341 }
342
343 if ($lconfighash{$key}[21] && $lconfighash{$key}[22]) {
344 my @encs = split('\|', $lconfighash{$key}[21]);
345 my @ints = split('\|', $lconfighash{$key}[22]);
346 my @groups = split('\|', $lconfighash{$key}[23]);
347
348 # Use IKE grouptype if no ESP group type has been selected
349 # (for backwards compatibility)
350 if ($lconfighash{$key}[23] eq "") {
351 @groups = split('\|', $lconfighash{$key}[20]);
352 }
f6529a04 353
624615ee
LS
354 my @algos = &make_algos("esp", \@encs, \@ints, \@groups, ($pfs eq "on"));
355 print CONF "\tesp=" . join(",", @algos);
afd5d8f7 356
624615ee
LS
357 if ($lconfighash{$key}[24] eq 'on') { #only proposed algorythms?
358 print CONF "!\n";
359 } else {
360 print CONF "\n";
361 }
afd5d8f7 362 }
ac1cfefa 363
624615ee
LS
364 # IKE V1 or V2
365 if (! $lconfighash{$key}[29]) {
366 $lconfighash{$key}[29] = "ikev1";
367 }
a4737620 368
624615ee 369 print CONF "\tkeyexchange=$lconfighash{$key}[29]\n";
a4737620 370
624615ee
LS
371 # Lifetimes
372 print CONF "\tikelifetime=$lconfighash{$key}[16]h\n" if ($lconfighash{$key}[16]);
373 print CONF "\tkeylife=$lconfighash{$key}[17]h\n" if ($lconfighash{$key}[17]);
374
375 # Compression
376 print CONF "\tcompress=yes\n" if ($lconfighash{$key}[13] eq 'on');
377
378 # Force MOBIKE?
379 if (($lconfighash{$key}[29] eq "ikev2") && ($lconfighash{$key}[32] eq 'on')) {
380 print CONF "\tmobike=yes\n";
381 }
382
383 # Dead Peer Detection
384 my $dpdaction = $lconfighash{$key}[27];
385 print CONF "\tdpdaction=$dpdaction\n";
386
387 # If the dead peer detection is disabled and IKEv2 is used,
388 # dpddelay must be set to zero, too.
389 if ($dpdaction eq "none") {
390 if ($lconfighash{$key}[29] eq "ikev2") {
391 print CONF "\tdpddelay=0\n";
392 }
393 } else {
394 my $dpddelay = $lconfighash{$key}[31];
395 if (!$dpddelay) {
396 $dpddelay = 30;
397 }
398 print CONF "\tdpddelay=$dpddelay\n";
399 my $dpdtimeout = $lconfighash{$key}[30];
400 if (!$dpdtimeout) {
401 $dpdtimeout = 120;
402 }
403 print CONF "\tdpdtimeout=$dpdtimeout\n";
404 }
405
406 # Build Authentication details: LEFTid RIGHTid : PSK psk
407 my $psk_line;
408 if ($lconfighash{$key}[4] eq 'psk') {
409 $psk_line = ($lconfighash{$key}[7] ? $lconfighash{$key}[7] : $localside) . " " ;
410 $psk_line .= $lconfighash{$key}[9] ? $lconfighash{$key}[9] : $lconfighash{$key}[10]; #remoteid or remote address?
411 $psk_line .= " : PSK '$lconfighash{$key}[5]'\n";
412 # if the line contains %any, it is less specific than two IP or ID, so move it at end of file.
413 if ($psk_line =~ /%any/) {
414 $last_secrets .= $psk_line;
415 } else {
416 print SECRETS $psk_line;
417 }
418 print CONF "\tauthby=secret\n";
419 } else {
420 print CONF "\tauthby=rsasig\n";
421 print CONF "\tleftrsasigkey=%cert\n";
422 print CONF "\trightrsasigkey=%cert\n";
423 }
424
dcb406cc
MT
425 my $start_action = $lconfighash{$key}[33];
426 if (!$start_action) {
427 $start_action = "start";
428 }
429
af183eeb
MT
430 my $inactivity_timeout = $lconfighash{$key}[34];
431 if ($inactivity_timeout eq "") {
432 $inactivity_timeout = 900;
433 }
434
624615ee
LS
435 # Automatically start only if a net-to-net connection
436 if ($lconfighash{$key}[3] eq 'host') {
437 print CONF "\tauto=add\n";
438 print CONF "\trightsourceip=$lvpnsettings{'RW_NET'}\n";
439 } else {
dcb406cc 440 print CONF "\tauto=$start_action\n";
1ee1666e
MT
441
442 # If in on-demand mode, we terminate the tunnel
443 # after 15 min of no traffic
af183eeb
MT
444 if ($start_action eq 'route' && $inactivity_timeout > 0) {
445 print CONF "\tinactivity=$inactivity_timeout\n";
1ee1666e 446 }
624615ee
LS
447 }
448
449 # Fragmentation
450 print CONF "\tfragmentation=yes\n";
451
452 print CONF "\n";
453 } #foreach key
454
455 # Add post user includes to config file
456 # After the GUI-connections allows to patch connections.
457 if (-e "/etc/ipsec.user-post.conf") {
458 print CONF "include /etc/ipsec.user-post.conf\n";
459 print CONF "\n";
460 }
461
462 print SECRETS $last_secrets if ($last_secrets);
463 close(CONF);
464 close(SECRETS);
ac1cfefa
MT
465}
466
ae2782ba
MT
467# Hook to regenerate the configuration files.
468if ($ENV{"REMOTE_ADDR"} eq "") {
26dfc86a 469 writeipsecfiles();
ae2782ba
MT
470 exit(0);
471}
472
ac1cfefa
MT
473###
474### Save main settings
475###
476if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq '' && $cgiparams{'KEY'} eq '') {
624615ee
LS
477 &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
478
479 unless (&General::validfqdn($cgiparams{'VPN_IP'}) || &General::validip($cgiparams{'VPN_IP'})
480 || $cgiparams{'VPN_IP'} eq '%defaultroute' ) {
481 $errormessage = $Lang::tr{'invalid input for hostname'};
482 goto SAVE_ERROR;
483 }
484
485 unless ($cgiparams{'VPN_DELAYED_START'} =~ /^[0-9]{1,3}$/ ) { #allow 0-999 seconds !
486 $errormessage = $Lang::tr{'invalid time period'};
487 goto SAVE_ERROR;
488 }
489
490 if ( $cgiparams{'RW_NET'} ne '' and !&General::validipandmask($cgiparams{'RW_NET'}) ) {
491 $errormessage = $Lang::tr{'urlfilter invalid ip or mask error'};
492 goto SAVE_ERROR;
493 }
494
495 $vpnsettings{'ENABLED'} = $cgiparams{'ENABLED'};
496 $vpnsettings{'VPN_IP'} = $cgiparams{'VPN_IP'};
497 $vpnsettings{'VPN_DELAYED_START'} = $cgiparams{'VPN_DELAYED_START'};
498 $vpnsettings{'RW_NET'} = $cgiparams{'RW_NET'};
499 &General::writehash("${General::swroot}/vpn/settings", \%vpnsettings);
500 &writeipsecfiles();
501 if (&vpnenabled) {
502 system('/usr/local/bin/ipsecctrl', 'S');
503 } else {
504 system('/usr/local/bin/ipsecctrl', 'D');
505 }
506 sleep $sleepDelay;
507 SAVE_ERROR:
ac1cfefa
MT
508###
509### Reset all step 2
510###
ed84e8b8 511} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove x509'} && $cgiparams{'AREUSURE'} eq 'yes') {
624615ee
LS
512 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
513
514 foreach my $key (keys %confighash) {
515 if ($confighash{$key}[4] eq 'cert') {
516 delete $confighash{$key};
517 }
518 }
519 while (my $file = glob("${General::swroot}/{ca,certs,crls,private}/*")) {
520 unlink $file
521 }
522 &cleanssldatabase();
523 if (open(FILE, ">${General::swroot}/vpn/caconfig")) {
524 print FILE "";
525 close FILE;
526 }
527 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
528 &writeipsecfiles();
529 system('/usr/local/bin/ipsecctrl', 'R');
530 sleep $sleepDelay;
ac1cfefa
MT
531
532###
533### Reset all step 1
534###
ed84e8b8 535} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove x509'}) {
624615ee
LS
536 &Header::showhttpheaders();
537 &Header::openpage($Lang::tr{'ipsec'}, 1, '');
538 &Header::openbigbox('100%', 'left', '', '');
539 &Header::openbox('100%', 'left', $Lang::tr{'are you sure'});
540 print <<END
ed84e8b8 541 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
624615ee
LS
542 <table width='100%'>
543 <tr>
544 <td align='center'>
545 <input type='hidden' name='AREUSURE' value='yes' />
546 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>:&nbsp;$Lang::tr{'resetting the vpn configuration will remove the root ca, the host certificate and all certificate based connections'}
547 </td>
548 </tr><tr>
549 <td align='center'>
550 <input type='submit' name='ACTION' value='$Lang::tr{'remove x509'}' />
ed84e8b8 551 <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></td>
624615ee
LS
552 </tr>
553 </table>
ed84e8b8 554 </form>
ac1cfefa 555END
624615ee
LS
556;
557 &Header::closebox();
558 &Header::closebigbox();
559 &Header::closepage();
560 exit (0);
ac1cfefa
MT
561
562###
563### Upload CA Certificate
564###
565} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload ca certificate'}) {
624615ee
LS
566 &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
567
568 if ($cgiparams{'CA_NAME'} !~ /^[a-zA-Z0-9]+$/) {
569 $errormessage = $Lang::tr{'name must only contain characters'};
570 goto UPLOADCA_ERROR;
571 }
572
573 if (length($cgiparams{'CA_NAME'}) >60) {
574 $errormessage = $Lang::tr{'name too long'};
575 goto VPNCONF_ERROR;
576 }
577
578 if ($cgiparams{'CA_NAME'} eq 'ca') {
579 $errormessage = $Lang::tr{'name is invalid'};
580 goto UPLOAD_CA_ERROR;
581 }
582
583 # Check if there is no other entry with this name
584 foreach my $key (keys %cahash) {
585 if ($cahash{$key}[0] eq $cgiparams{'CA_NAME'}) {
586 $errormessage = $Lang::tr{'a ca certificate with this name already exists'};
587 goto UPLOADCA_ERROR;
588 }
589 }
590
591 if (ref ($cgiparams{'FH'}) ne 'Fh') {
592 $errormessage = $Lang::tr{'there was no file upload'};
593 goto UPLOADCA_ERROR;
594 }
595 # Move uploaded ca to a temporary file
596 (my $fh, my $filename) = tempfile( );
597 if (copy ($cgiparams{'FH'}, $fh) != 1) {
598 $errormessage = $!;
599 goto UPLOADCA_ERROR;
600 }
601 my $temp = `/usr/bin/openssl x509 -text -in $filename`;
602 if ($temp !~ /CA:TRUE/i) {
603 $errormessage = $Lang::tr{'not a valid ca certificate'};
604 unlink ($filename);
605 goto UPLOADCA_ERROR;
606 } else {
607 move($filename, "${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem");
608 if ($? ne 0) {
609 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
610 unlink ($filename);
611 goto UPLOADCA_ERROR;
612 }
613 }
614
615 my $key = &General::findhasharraykey (\%cahash);
616 $cahash{$key}[0] = $cgiparams{'CA_NAME'};
617 $cahash{$key}[1] = &Header::cleanhtml(getsubjectfromcert ("${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem"));
618 &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
619
620 system('/usr/local/bin/ipsecctrl', 'R');
621 sleep $sleepDelay;
622
623 UPLOADCA_ERROR:
ac1cfefa
MT
624
625###
626### Display ca certificate
627###
628} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show ca certificate'}) {
624615ee
LS
629 &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
630
631 if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem") {
632 &Header::showhttpheaders();
633 &Header::openpage($Lang::tr{'ipsec'}, 1, '');
634 &Header::openbigbox('100%', 'left', '', '');
635 &Header::openbox('100%', 'left', "$Lang::tr{'ca certificate'}:");
636 my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
637 $output = &Header::cleanhtml($output,"y");
638 print "<pre>$output</pre>\n";
639 &Header::closebox();
640 print "<div align='center'><a href='/cgi-bin/vpnmain.cgi'>$Lang::tr{'back'}</a></div>";
641 &Header::closebigbox();
642 &Header::closepage();
643 exit(0);
644 } else {
645 $errormessage = $Lang::tr{'invalid key'};
646 }
ac1cfefa
MT
647
648###
ed84e8b8 649### Export ca certificate to browser
ac1cfefa
MT
650###
651} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download ca certificate'}) {
624615ee
LS
652 &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
653
654 if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
655 print "Content-Type: application/force-download\n";
656 print "Content-Type: application/octet-stream\r\n";
657 print "Content-Disposition: attachment; filename=$cahash{$cgiparams{'KEY'}}[0]cert.pem\r\n\r\n";
658 print `/usr/bin/openssl x509 -in ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
659 exit(0);
660 } else {
661 $errormessage = $Lang::tr{'invalid key'};
662 }
ac1cfefa
MT
663
664###
665### Remove ca certificate (step 2)
666###
667} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'} && $cgiparams{'AREUSURE'} eq 'yes') {
624615ee
LS
668 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
669 &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
670
671 if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
672 foreach my $key (keys %confighash) {
673 my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem ${General::swroot}/certs/$confighash{$key}[1]cert.pem`;
674 if ($test =~ /: OK/) {
675 # Delete connection
676 system('/usr/local/bin/ipsecctrl', 'D', $key) if (&vpnenabled);
677 unlink ("${General::swroot}/certs/$confighash{$key}[1]cert.pem");
678 unlink ("${General::swroot}/certs/$confighash{$key}[1].p12");
679 delete $confighash{$key};
680 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
681 &writeipsecfiles();
682 }
683 }
684 unlink ("${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
685 delete $cahash{$cgiparams{'KEY'}};
686 &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
687 system('/usr/local/bin/ipsecctrl', 'R');
688 sleep $sleepDelay;
689 } else {
690 $errormessage = $Lang::tr{'invalid key'};
ac1cfefa 691 }
ac1cfefa
MT
692###
693### Remove ca certificate (step 1)
694###
695} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'}) {
624615ee
LS
696 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
697 &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
698
699 my $assignedcerts = 0;
700 if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
701 foreach my $key (keys %confighash) {
702 my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem ${General::swroot}/certs/$confighash{$key}[1]cert.pem`;
703 if ($test =~ /: OK/) {
704 $assignedcerts++;
705 }
706 }
707 if ($assignedcerts) {
708 &Header::showhttpheaders();
709 &Header::openpage($Lang::tr{'ipsec'}, 1, '');
710 &Header::openbigbox('100%', 'left', '', '');
711 &Header::openbox('100%', 'left', $Lang::tr{'are you sure'});
712 print <<END
713 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
714 <table width='100%'>
715 <tr>
716 <td align='center'>
717 <input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />
718 <input type='hidden' name='AREUSURE' value='yes' /></td>
719 </tr><tr>
720 <td align='center'>
721 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>&nbsp;$Lang::tr{'connections are associated with this ca. deleting the ca will delete these connections as well.'}</td>
722 </tr><tr>
723 <td align='center'>
724 <input type='submit' name='ACTION' value='$Lang::tr{'remove ca certificate'}' />
725 <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></td>
726 </tr>
727 </table>
728 </form>
ac1cfefa 729END
624615ee
LS
730;
731 &Header::closebox();
732 &Header::closebigbox();
733 &Header::closepage();
734 exit (0);
735 } else {
736 unlink ("${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
737 delete $cahash{$cgiparams{'KEY'}};
738 &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
739 system('/usr/local/bin/ipsecctrl', 'R');
740 sleep $sleepDelay;
741 }
ac1cfefa 742 } else {
624615ee 743 $errormessage = $Lang::tr{'invalid key'};
ac1cfefa 744 }
ac1cfefa
MT
745
746###
747### Display root certificate
748###
749} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'} ||
750 $cgiparams{'ACTION'} eq $Lang::tr{'show host certificate'}) {
624615ee
LS
751 my $output;
752 &Header::showhttpheaders();
753 &Header::openpage($Lang::tr{'ipsec'}, 1, '');
754 &Header::openbigbox('100%', 'left', '', '');
755 if ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'}) {
756 &Header::openbox('100%', 'left', "$Lang::tr{'root certificate'}:");
757 $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ca/cacert.pem`;
758 } else {
759 &Header::openbox('100%', 'left', "$Lang::tr{'host certificate'}:");
760 $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/certs/hostcert.pem`;
761 }
762 $output = &Header::cleanhtml($output,"y");
763 print "<pre>$output</pre>\n";
764 &Header::closebox();
765 print "<div align='center'><a href='/cgi-bin/vpnmain.cgi'>$Lang::tr{'back'}</a></div>";
766 &Header::closebigbox();
767 &Header::closepage();
768 exit(0);
ac1cfefa
MT
769
770###
ed84e8b8 771### Export root certificate to browser
ac1cfefa
MT
772###
773} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download root certificate'}) {
624615ee
LS
774 if ( -f "${General::swroot}/ca/cacert.pem" ) {
775 print "Content-Type: application/force-download\n";
776 print "Content-Disposition: attachment; filename=cacert.pem\r\n\r\n";
777 print `/usr/bin/openssl x509 -in ${General::swroot}/ca/cacert.pem`;
778 exit(0);
779 }
ac1cfefa 780###
ed84e8b8 781### Export host certificate to browser
ac1cfefa
MT
782###
783} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download host certificate'}) {
624615ee
LS
784 if ( -f "${General::swroot}/certs/hostcert.pem" ) {
785 print "Content-Type: application/force-download\n";
786 print "Content-Disposition: attachment; filename=hostcert.pem\r\n\r\n";
787 print `/usr/bin/openssl x509 -in ${General::swroot}/certs/hostcert.pem`;
788 exit(0);
789 }
ac1cfefa 790###
ed84e8b8 791### Form for generating/importing the caroot+host certificate
ac1cfefa
MT
792###
793} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'generate root/host certificates'} ||
624615ee
LS
794 $cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
795
796 if (-f "${General::swroot}/ca/cacert.pem") {
797 $errormessage = $Lang::tr{'valid root certificate already exists'};
798 goto ROOTCERT_SKIP;
799 }
800
801 &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
802 # fill in initial values
803 if ($cgiparams{'ROOTCERT_HOSTNAME'} eq '') {
804 if (-e "${General::swroot}/red/active" && open(IPADDR, "${General::swroot}/red/local-ipaddress")) {
805 my $ipaddr = <IPADDR>;
806 close IPADDR;
807 chomp ($ipaddr);
808 $cgiparams{'ROOTCERT_HOSTNAME'} = (gethostbyaddr(pack("C4", split(/\./, $ipaddr)), 2))[0];
809 if ($cgiparams{'ROOTCERT_HOSTNAME'} eq '') {
810 $cgiparams{'ROOTCERT_HOSTNAME'} = $ipaddr;
811 }
812 }
813 $cgiparams{'ROOTCERT_COUNTRY'} = $vpnsettings{'ROOTCERT_COUNTRY'} if (!$cgiparams{'ROOTCERT_COUNTRY'});
814 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
815 &General::log("ipsec", "Importing from p12...");
ac1cfefa 816
624615ee
LS
817 if (ref ($cgiparams{'FH'}) ne 'Fh') {
818 $errormessage = $Lang::tr{'there was no file upload'};
819 goto ROOTCERT_ERROR;
820 }
ac1cfefa 821
624615ee
LS
822 # Move uploaded certificate request to a temporary file
823 (my $fh, my $filename) = tempfile( );
824 if (copy ($cgiparams{'FH'}, $fh) != 1) {
825 $errormessage = $!;
826 goto ROOTCERT_ERROR;
827 }
ac1cfefa 828
624615ee
LS
829 # Extract the CA certificate from the file
830 &General::log("ipsec", "Extracting caroot from p12...");
831 if (open(STDIN, "-|")) {
832 my $opt = " pkcs12 -cacerts -nokeys";
833 $opt .= " -in $filename";
834 $opt .= " -out /tmp/newcacert";
835 $errormessage = &callssl ($opt);
836 } else { #child
837 print "$cgiparams{'P12_PASS'}\n";
838 exit (0);
839 }
ac1cfefa 840
624615ee
LS
841 # Extract the Host certificate from the file
842 if (!$errormessage) {
843 &General::log("ipsec", "Extracting host cert from p12...");
844 if (open(STDIN, "-|")) {
845 my $opt = " pkcs12 -clcerts -nokeys";
846 $opt .= " -in $filename";
847 $opt .= " -out /tmp/newhostcert";
848 $errormessage = &callssl ($opt);
849 } else { #child
850 print "$cgiparams{'P12_PASS'}\n";
851 exit (0);
852 }
853 }
ed84e8b8 854
624615ee
LS
855 # Extract the Host key from the file
856 if (!$errormessage) {
857 &General::log("ipsec", "Extracting private key from p12...");
858 if (open(STDIN, "-|")) {
859 my $opt = " pkcs12 -nocerts -nodes";
860 $opt .= " -in $filename";
861 $opt .= " -out /tmp/newhostkey";
862 $errormessage = &callssl ($opt);
863 } else { #child
864 print "$cgiparams{'P12_PASS'}\n";
865 exit (0);
866 }
867 }
ac1cfefa 868
624615ee
LS
869 if (!$errormessage) {
870 &General::log("ipsec", "Moving cacert...");
871 move("/tmp/newcacert", "${General::swroot}/ca/cacert.pem");
872 $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
873 }
ed84e8b8 874
624615ee
LS
875 if (!$errormessage) {
876 &General::log("ipsec", "Moving host cert...");
877 move("/tmp/newhostcert", "${General::swroot}/certs/hostcert.pem");
878 $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
879 }
ed84e8b8 880
624615ee
LS
881 if (!$errormessage) {
882 &General::log("ipsec", "Moving private key...");
883 move("/tmp/newhostkey", "${General::swroot}/certs/hostkey.pem");
884 $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
885 }
886
887 #cleanup temp files
888 unlink ($filename);
889 unlink ('/tmp/newcacert');
890 unlink ('/tmp/newhostcert');
891 unlink ('/tmp/newhostkey');
892 if ($errormessage) {
893 unlink ("${General::swroot}/ca/cacert.pem");
894 unlink ("${General::swroot}/certs/hostcert.pem");
895 unlink ("${General::swroot}/certs/hostkey.pem");
896 goto ROOTCERT_ERROR;
897 }
898
899 # Create empty CRL cannot be done because we don't have
900 # the private key for this CAROOT
901 # IPFire can only import certificates
902
903 &General::log("ipsec", "p12 import completed!");
904 &cleanssldatabase();
905 goto ROOTCERT_SUCCESS;
906
907 } elsif ($cgiparams{'ROOTCERT_COUNTRY'} ne '') {
908
909 # Validate input since the form was submitted
910 if ($cgiparams{'ROOTCERT_ORGANIZATION'} eq ''){
911 $errormessage = $Lang::tr{'organization cant be empty'};
912 goto ROOTCERT_ERROR;
913 }
914 if (length($cgiparams{'ROOTCERT_ORGANIZATION'}) >60) {
915 $errormessage = $Lang::tr{'organization too long'};
916 goto ROOTCERT_ERROR;
917 }
918 if ($cgiparams{'ROOTCERT_ORGANIZATION'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
919 $errormessage = $Lang::tr{'invalid input for organization'};
920 goto ROOTCERT_ERROR;
921 }
922 if ($cgiparams{'ROOTCERT_HOSTNAME'} eq ''){
923 $errormessage = $Lang::tr{'hostname cant be empty'};
924 goto ROOTCERT_ERROR;
925 }
926 unless (&General::validfqdn($cgiparams{'ROOTCERT_HOSTNAME'}) || &General::validip($cgiparams{'ROOTCERT_HOSTNAME'})) {
927 $errormessage = $Lang::tr{'invalid input for hostname'};
928 goto ROOTCERT_ERROR;
929 }
930 if ($cgiparams{'ROOTCERT_EMAIL'} ne '' && (! &General::validemail($cgiparams{'ROOTCERT_EMAIL'}))) {
931 $errormessage = $Lang::tr{'invalid input for e-mail address'};
932 goto ROOTCERT_ERROR;
933 }
934 if (length($cgiparams{'ROOTCERT_EMAIL'}) > 40) {
935 $errormessage = $Lang::tr{'e-mail address too long'};
936 goto ROOTCERT_ERROR;
937 }
938 if ($cgiparams{'ROOTCERT_OU'} ne '' && $cgiparams{'ROOTCERT_OU'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
939 $errormessage = $Lang::tr{'invalid input for department'};
940 goto ROOTCERT_ERROR;
941 }
942 if ($cgiparams{'ROOTCERT_CITY'} ne '' && $cgiparams{'ROOTCERT_CITY'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
943 $errormessage = $Lang::tr{'invalid input for city'};
944 goto ROOTCERT_ERROR;
945 }
946 if ($cgiparams{'ROOTCERT_STATE'} ne '' && $cgiparams{'ROOTCERT_STATE'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
947 $errormessage = $Lang::tr{'invalid input for state or province'};
948 goto ROOTCERT_ERROR;
949 }
950 if ($cgiparams{'ROOTCERT_COUNTRY'} !~ /^[A-Z]*$/) {
951 $errormessage = $Lang::tr{'invalid input for country'};
952 goto ROOTCERT_ERROR;
953 }
954 #the exact syntax is a list comma separated of
955 # email:any-validemail
956 # URI: a uniform resource indicator
957 # DNS: a DNS domain name
958 # RID: a registered OBJECT IDENTIFIER
959 # IP: an IP address
960 # example: email:franck@foo.com,IP:10.0.0.10,DNS:franck.foo.com
961
962 if ($cgiparams{'SUBJECTALTNAME'} ne '' && $cgiparams{'SUBJECTALTNAME'} !~ /^(email|URI|DNS|RID|IP):[a-zA-Z0-9 :\/,\.\-_@]*$/) {
963 $errormessage = $Lang::tr{'vpn altname syntax'};
964 goto VPNCONF_ERROR;
965 }
966
967 # Copy the cgisettings to vpnsettings and save the configfile
968 $vpnsettings{'ROOTCERT_ORGANIZATION'} = $cgiparams{'ROOTCERT_ORGANIZATION'};
969 $vpnsettings{'ROOTCERT_HOSTNAME'} = $cgiparams{'ROOTCERT_HOSTNAME'};
970 $vpnsettings{'ROOTCERT_EMAIL'} = $cgiparams{'ROOTCERT_EMAIL'};
971 $vpnsettings{'ROOTCERT_OU'} = $cgiparams{'ROOTCERT_OU'};
972 $vpnsettings{'ROOTCERT_CITY'} = $cgiparams{'ROOTCERT_CITY'};
973 $vpnsettings{'ROOTCERT_STATE'} = $cgiparams{'ROOTCERT_STATE'};
974 $vpnsettings{'ROOTCERT_COUNTRY'} = $cgiparams{'ROOTCERT_COUNTRY'};
975 &General::writehash("${General::swroot}/vpn/settings", \%vpnsettings);
976
977 # Replace empty strings with a .
978 (my $ou = $cgiparams{'ROOTCERT_OU'}) =~ s/^\s*$/\./;
979 (my $city = $cgiparams{'ROOTCERT_CITY'}) =~ s/^\s*$/\./;
980 (my $state = $cgiparams{'ROOTCERT_STATE'}) =~ s/^\s*$/\./;
981
982 # Create the CA certificate
983 if (!$errormessage) {
984 &General::log("ipsec", "Creating cacert...");
985 if (open(STDIN, "-|")) {
986 my $opt = " req -x509 -sha256 -nodes";
987 $opt .= " -days 999999";
988 $opt .= " -newkey rsa:4096";
989 $opt .= " -keyout ${General::swroot}/private/cakey.pem";
990 $opt .= " -out ${General::swroot}/ca/cacert.pem";
991
992 $errormessage = &callssl ($opt);
993 } else { #child
994 print "$cgiparams{'ROOTCERT_COUNTRY'}\n";
995 print "$state\n";
996 print "$city\n";
997 print "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
998 print "$ou\n";
999 print "$cgiparams{'ROOTCERT_ORGANIZATION'} CA\n";
1000 print "$cgiparams{'ROOTCERT_EMAIL'}\n";
1001 exit (0);
1002 }
1003 }
1004
1005 # Create the Host certificate request
1006 if (!$errormessage) {
1007 &General::log("ipsec", "Creating host cert...");
1008 if (open(STDIN, "-|")) {
1009 my $opt = " req -sha256 -nodes";
1010 $opt .= " -newkey rsa:2048";
1011 $opt .= " -keyout ${General::swroot}/certs/hostkey.pem";
1012 $opt .= " -out ${General::swroot}/certs/hostreq.pem";
1013 $errormessage = &callssl ($opt);
1014 } else { #child
1015 print "$cgiparams{'ROOTCERT_COUNTRY'}\n";
1016 print "$state\n";
1017 print "$city\n";
1018 print "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
1019 print "$ou\n";
1020 print "$cgiparams{'ROOTCERT_HOSTNAME'}\n";
1021 print "$cgiparams{'ROOTCERT_EMAIL'}\n";
1022 print ".\n";
1023 print ".\n";
1024 exit (0);
1025 }
1026 }
1027
1028 # Sign the host certificate request
1029 if (!$errormessage) {
1030 &General::log("ipsec", "Self signing host cert...");
1031
1032 #No easy way for specifying the contain of subjectAltName without writing a config file...
1033 my ($fh, $v3extname) = tempfile ('/tmp/XXXXXXXX');
1034 print $fh <<END
1035 basicConstraints=CA:FALSE
1036 nsComment="OpenSSL Generated Certificate"
1037 subjectKeyIdentifier=hash
1038 authorityKeyIdentifier=keyid,issuer:always
1039 extendedKeyUsage = serverAuth
ed84e8b8
MT
1040END
1041;
624615ee
LS
1042 print $fh "subjectAltName=$cgiparams{'SUBJECTALTNAME'}" if ($cgiparams{'SUBJECTALTNAME'});
1043 close ($fh);
1044
1045 my $opt = " ca -md sha256 -days 999999";
1046 $opt .= " -batch -notext";
1047 $opt .= " -in ${General::swroot}/certs/hostreq.pem";
1048 $opt .= " -out ${General::swroot}/certs/hostcert.pem";
1049 $opt .= " -extfile $v3extname";
1050 $errormessage = &callssl ($opt);
1051 unlink ("${General::swroot}/certs/hostreq.pem"); #no more needed
1052 unlink ($v3extname);
1053 }
1054
1055 # Create an empty CRL
1056 if (!$errormessage) {
1057 &General::log("ipsec", "Creating emptycrl...");
1058 my $opt = " ca -gencrl";
1059 $opt .= " -out ${General::swroot}/crls/cacrl.pem";
1060 $errormessage = &callssl ($opt);
1061 }
1062
1063 # Successfully build CA / CERT!
1064 if (!$errormessage) {
1065 &cleanssldatabase();
1066 goto ROOTCERT_SUCCESS;
1067 }
1068
1069 #Cleanup
1070 unlink ("${General::swroot}/ca/cacert.pem");
1071 unlink ("${General::swroot}/certs/hostkey.pem");
1072 unlink ("${General::swroot}/certs/hostcert.pem");
1073 unlink ("${General::swroot}/crls/cacrl.pem");
1074 &cleanssldatabase();
1075 }
1076
1077 ROOTCERT_ERROR:
1078 &Header::showhttpheaders();
1079 &Header::openpage($Lang::tr{'ipsec'}, 1, '');
1080 &Header::openbigbox('100%', 'left', '', $errormessage);
1081 if ($errormessage) {
1082 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
1083 print "<class name='base'>$errormessage";
1084 print "&nbsp;</class>";
1085 &Header::closebox();
1086 }
1087 &Header::openbox('100%', 'left', "$Lang::tr{'generate root/host certificates'}:");
1088 print <<END
1089 <form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'>
1090 <table width='100%' border='0' cellspacing='1' cellpadding='0'>
1091 <tr><td width='40%' class='base'>$Lang::tr{'organization name'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
1092 <td width='60%' class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_ORGANIZATION' value='$cgiparams{'ROOTCERT_ORGANIZATION'}' size='32' /></td></tr>
1093 <tr><td class='base'>$Lang::tr{'ipfires hostname'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
1094 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_HOSTNAME' value='$cgiparams{'ROOTCERT_HOSTNAME'}' size='32' /></td></tr>
1095 <tr><td class='base'>$Lang::tr{'your e-mail'}:</td>
1096 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_EMAIL' value='$cgiparams{'ROOTCERT_EMAIL'}' size='32' /></td></tr>
1097 <tr><td class='base'>$Lang::tr{'your department'}:</td>
1098 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_OU' value='$cgiparams{'ROOTCERT_OU'}' size='32' /></td></tr>
1099 <tr><td class='base'>$Lang::tr{'city'}:</td>
1100 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_CITY' value='$cgiparams{'ROOTCERT_CITY'}' size='32' /></td></tr>
1101 <tr><td class='base'>$Lang::tr{'state or province'}:</td>
1102 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_STATE' value='$cgiparams{'ROOTCERT_STATE'}' size='32' /></td></tr>
1103 <tr><td class='base'>$Lang::tr{'country'}:</td>
1104 <td class='base'><select name='ROOTCERT_COUNTRY'>
ac1cfefa 1105END
624615ee
LS
1106;
1107 foreach my $country (sort keys %{Countries::countries}) {
1108 print "<option value='$Countries::countries{$country}'";
1109 if ( $Countries::countries{$country} eq $cgiparams{'ROOTCERT_COUNTRY'} ) {
1110 print " selected='selected'";
1111 }
1112 print ">$country</option>";
1113 }
1114 print <<END
1115 </select></td></tr>
1116 <tr><td class='base'>$Lang::tr{'vpn subjectaltname'} (subjectAltName=email:*,URI:*,DNS:*,RID:*)</td>
ed84e8b8 1117 <td class='base' nowrap='nowrap'><input type='text' name='SUBJECTALTNAME' value='$cgiparams{'SUBJECTALTNAME'}' size='32' /></td></tr>
624615ee
LS
1118 <tr><td>&nbsp;</td>
1119 <td><br /><input type='submit' name='ACTION' value='$Lang::tr{'generate root/host certificates'}' /><br /><br /></td></tr>
1120 <tr><td class='base' colspan='2' align='left'>
1121 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>:
1122 $Lang::tr{'generating the root and host certificates may take a long time. it can take up to several minutes on older hardware. please be patient'}
1123 </td></tr>
1124 <tr><td colspan='2'><hr></td></tr>
1125 <tr><td class='base' nowrap='nowrap'>$Lang::tr{'upload p12 file'}:</td>
1126 <td nowrap='nowrap'><input type='file' name='FH' size='32' /></td></tr>
1127 <tr><td class='base'>$Lang::tr{'pkcs12 file password'}:</td>
1128 <td class='base' nowrap='nowrap'><input type='password' name='P12_PASS' value='$cgiparams{'P12_PASS'}' size='32' /></td></tr>
1129 <tr><td>&nbsp;</td>
1130 <td><input type='submit' name='ACTION' value='$Lang::tr{'upload p12 file'}' /></td></tr>
1131 <tr><td class='base' colspan='2' align='left'>
1132 <img src='/blob.gif' alt='*' />&nbsp;$Lang::tr{'required field'}</td></tr>
1133 </table></form>
ed84e8b8 1134END
624615ee
LS
1135;
1136 &Header::closebox();
1137 &Header::closebigbox();
1138 &Header::closepage();
1139 exit(0);
1140
1141 ROOTCERT_SUCCESS:
1142 if (&vpnenabled) {
1143 system('/usr/local/bin/ipsecctrl', 'S');
1144 sleep $sleepDelay;
1145 }
1146 ROOTCERT_SKIP:
ac1cfefa 1147###
ed84e8b8 1148### Export PKCS12 file to browser
ac1cfefa
MT
1149###
1150} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download pkcs12 file'}) {
624615ee
LS
1151 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1152 print "Content-Type: application/force-download\n";
1153 print "Content-Disposition: attachment; filename=" . $confighash{$cgiparams{'KEY'}}[1] . ".p12\r\n";
1154 print "Content-Type: application/octet-stream\r\n\r\n";
1155 print `/bin/cat ${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1].p12`;
1156 exit (0);
ac1cfefa
MT
1157
1158###
1159### Display certificate
1160###
1161} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show certificate'}) {
624615ee
LS
1162 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1163
1164 if ( -f "${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem") {
1165 &Header::showhttpheaders();
1166 &Header::openpage($Lang::tr{'ipsec'}, 1, '');
1167 &Header::openbigbox('100%', 'left', '', '');
1168 &Header::openbox('100%', 'left', "$Lang::tr{'cert'}:");
1169 my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
1170 $output = &Header::cleanhtml($output,"y");
1171 print "<pre>$output</pre>\n";
1172 &Header::closebox();
1173 print "<div align='center'><a href='/cgi-bin/vpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1174 &Header::closebigbox();
1175 &Header::closepage();
1176 exit(0);
1177 }
ac1cfefa
MT
1178
1179###
ed84e8b8 1180### Export Certificate to browser
ac1cfefa
MT
1181###
1182} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download certificate'}) {
624615ee 1183 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
ac1cfefa 1184
624615ee
LS
1185 if ( -f "${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem") {
1186 print "Content-Type: application/force-download\n";
1187 print "Content-Disposition: attachment; filename=" . $confighash{$cgiparams{'KEY'}}[1] . "cert.pem\n\n";
1188 print `/bin/cat ${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
1189 exit (0);
1190 }
ac1cfefa
MT
1191
1192###
1193### Enable/Disable connection
1194###
1195} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
624615ee
LS
1196
1197 &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
1198 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1199
1200 if ($confighash{$cgiparams{'KEY'}}) {
1201 if ($confighash{$cgiparams{'KEY'}}[0] eq 'off') {
1202 $confighash{$cgiparams{'KEY'}}[0] = 'on';
1203 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
1204 &writeipsecfiles();
1205 system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'}) if (&vpnenabled);
1206 } else {
1207 system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'}) if (&vpnenabled);
1208 $confighash{$cgiparams{'KEY'}}[0] = 'off';
1209 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
1210 &writeipsecfiles();
1211 }
1212 sleep $sleepDelay;
ac1cfefa 1213 } else {
624615ee 1214 $errormessage = $Lang::tr{'invalid key'};
ac1cfefa 1215 }
ac1cfefa
MT
1216
1217###
1218### Restart connection
1219###
1220} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'restart'}) {
624615ee
LS
1221 &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
1222 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
ac1cfefa 1223
624615ee
LS
1224 if ($confighash{$cgiparams{'KEY'}}) {
1225 if (&vpnenabled) {
1226 system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'});
1227 sleep $sleepDelay;
1228 }
1229 } else {
1230 $errormessage = $Lang::tr{'invalid key'};
ac1cfefa 1231 }
ac1cfefa
MT
1232
1233###
1234### Remove connection
1235###
1236} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove'}) {
624615ee
LS
1237 &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
1238 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1239
1240 if ($confighash{$cgiparams{'KEY'}}) {
1241 system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'}) if (&vpnenabled);
1242 unlink ("${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem");
1243 unlink ("${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1].p12");
1244 delete $confighash{$cgiparams{'KEY'}};
1245 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
1246 &writeipsecfiles();
1247 } else {
1248 $errormessage = $Lang::tr{'invalid key'};
1249 }
c6df357f 1250 &General::firewall_reload();
ac1cfefa
MT
1251###
1252### Choose between adding a host-net or net-net connection
1253###
1254} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'add'} && $cgiparams{'TYPE'} eq '') {
ac1cfefa 1255 &Header::showhttpheaders();
7d44bfee 1256 &Header::openpage($Lang::tr{'ipsec'}, 1, '');
ed84e8b8
MT
1257 &Header::openbigbox('100%', 'left', '', '');
1258 &Header::openbox('100%', 'left', $Lang::tr{'connection type'});
ac1cfefa 1259 print <<END
624615ee
LS
1260 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1261 <b>$Lang::tr{'connection type'}:</b><br />
1262 <table>
1263 <tr><td><input type='radio' name='TYPE' value='host' checked='checked' /></td>
ed84e8b8 1264 <td class='base'>$Lang::tr{'host to net vpn'}</td>
624615ee 1265 </tr><tr>
ed84e8b8
MT
1266 <td><input type='radio' name='TYPE' value='net' /></td>
1267 <td class='base'>$Lang::tr{'net to net vpn'}</td>
624615ee 1268 </tr><tr>
ed84e8b8 1269 <td align='center' colspan='2'><input type='submit' name='ACTION' value='$Lang::tr{'add'}' /></td>
624615ee
LS
1270 </tr>
1271 </table></form>
ac1cfefa 1272END
624615ee 1273;
ac1cfefa
MT
1274 &Header::closebox();
1275 &Header::closebigbox();
1276 &Header::closepage();
1277 exit (0);
1278###
ed1d0fbd 1279### Adding/Editing/Saving a connection
ac1cfefa
MT
1280###
1281} elsif (($cgiparams{'ACTION'} eq $Lang::tr{'add'}) ||
624615ee
LS
1282 ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) ||
1283 ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'ADVANCED'} eq '')) {
ac1cfefa 1284
624615ee
LS
1285 &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
1286 &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
1287 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
cbb3a8f9 1288
624615ee
LS
1289 if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) {
1290 if (! $confighash{$cgiparams{'KEY'}}[0]) {
1291 $errormessage = $Lang::tr{'invalid key'};
1292 goto VPNCONF_END;
1293 }
1294 $cgiparams{'ENABLED'} = $confighash{$cgiparams{'KEY'}}[0];
1295 $cgiparams{'NAME'} = $confighash{$cgiparams{'KEY'}}[1];
1296 $cgiparams{'TYPE'} = $confighash{$cgiparams{'KEY'}}[3];
1297 $cgiparams{'AUTH'} = $confighash{$cgiparams{'KEY'}}[4];
1298 $cgiparams{'PSK'} = $confighash{$cgiparams{'KEY'}}[5];
1299 #$cgiparams{'free'} = $confighash{$cgiparams{'KEY'}}[6];
1300 $cgiparams{'LOCAL_ID'} = $confighash{$cgiparams{'KEY'}}[7];
b1881251
MT
1301 my @local_subnets = split(",", $confighash{$cgiparams{'KEY'}}[8]);
1302 $cgiparams{'LOCAL_SUBNET'} = join(/\|/, @local_subnets);
624615ee
LS
1303 $cgiparams{'REMOTE_ID'} = $confighash{$cgiparams{'KEY'}}[9];
1304 $cgiparams{'REMOTE'} = $confighash{$cgiparams{'KEY'}}[10];
b1881251
MT
1305 my @remote_subnets = split(",", $confighash{$cgiparams{'KEY'}}[11]);
1306 $cgiparams{'REMOTE_SUBNET'} = join(/\|/, @remote_subnets);
624615ee
LS
1307 $cgiparams{'REMARK'} = $confighash{$cgiparams{'KEY'}}[25];
1308 $cgiparams{'DPD_ACTION'} = $confighash{$cgiparams{'KEY'}}[27];
1309 $cgiparams{'IKE_VERSION'} = $confighash{$cgiparams{'KEY'}}[29];
1310 $cgiparams{'IKE_ENCRYPTION'} = $confighash{$cgiparams{'KEY'}}[18];
1311 $cgiparams{'IKE_INTEGRITY'} = $confighash{$cgiparams{'KEY'}}[19];
1312 $cgiparams{'IKE_GROUPTYPE'} = $confighash{$cgiparams{'KEY'}}[20];
1313 $cgiparams{'IKE_LIFETIME'} = $confighash{$cgiparams{'KEY'}}[16];
1314 $cgiparams{'ESP_ENCRYPTION'} = $confighash{$cgiparams{'KEY'}}[21];
1315 $cgiparams{'ESP_INTEGRITY'} = $confighash{$cgiparams{'KEY'}}[22];
1316 $cgiparams{'ESP_GROUPTYPE'} = $confighash{$cgiparams{'KEY'}}[23];
1317 if ($cgiparams{'ESP_GROUPTYPE'} eq "") {
1318 $cgiparams{'ESP_GROUPTYPE'} = $cgiparams{'IKE_GROUPTYPE'};
1319 }
1320 $cgiparams{'ESP_KEYLIFE'} = $confighash{$cgiparams{'KEY'}}[17];
1321 $cgiparams{'COMPRESSION'} = $confighash{$cgiparams{'KEY'}}[13];
1322 $cgiparams{'ONLY_PROPOSED'} = $confighash{$cgiparams{'KEY'}}[24];
1323 $cgiparams{'PFS'} = $confighash{$cgiparams{'KEY'}}[28];
1324 $cgiparams{'DPD_TIMEOUT'} = $confighash{$cgiparams{'KEY'}}[30];
1325 $cgiparams{'DPD_DELAY'} = $confighash{$cgiparams{'KEY'}}[31];
1326 $cgiparams{'FORCE_MOBIKE'} = $confighash{$cgiparams{'KEY'}}[32];
af183eeb 1327 $cgiparams{'INACTIVITY_TIMEOUT'} = $confighash{$cgiparams{'KEY'}}[34];
29f5e0e2 1328 $cgiparams{'MODE'} = $confighash{$cgiparams{'KEY'}}[35];
cae1f4a7 1329 $cgiparams{'INTERFACE_MODE'} = $confighash{$cgiparams{'KEY'}}[36];
624615ee
LS
1330
1331 if (!$cgiparams{'DPD_DELAY'}) {
1332 $cgiparams{'DPD_DELAY'} = 30;
1333 }
cbb3a8f9 1334
624615ee
LS
1335 if (!$cgiparams{'DPD_TIMEOUT'}) {
1336 $cgiparams{'DPD_TIMEOUT'} = 120;
1337 }
ac1cfefa 1338
af183eeb
MT
1339 if ($cgiparams{'INACTIVITY_TIMEOUT'} eq "") {
1340 $cgiparams{'INACTIVITY_TIMEOUT'} = 900;
1341 }
1342
29f5e0e2
MT
1343 if ($cgiparams{'MODE'} eq "") {
1344 $cgiparams{'MODE'} = "tunnel";
1345 }
1346
624615ee
LS
1347 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
1348 $cgiparams{'REMARK'} = &Header::cleanhtml($cgiparams{'REMARK'});
1349 if ($cgiparams{'TYPE'} !~ /^(host|net)$/) {
1350 $errormessage = $Lang::tr{'connection type is invalid'};
1351 goto VPNCONF_ERROR;
1352 }
ac1cfefa 1353
624615ee
LS
1354 if ($cgiparams{'NAME'} !~ /^[a-zA-Z0-9]+$/) {
1355 $errormessage = $Lang::tr{'name must only contain characters'};
1356 goto VPNCONF_ERROR;
1357 }
ac1cfefa 1358
624615ee
LS
1359 if ($cgiparams{'NAME'} =~ /^(host|01|block|private|clear|packetdefault)$/) {
1360 $errormessage = $Lang::tr{'name is invalid'};
1361 goto VPNCONF_ERROR;
1362 }
ac1cfefa 1363
624615ee
LS
1364 if (length($cgiparams{'NAME'}) >60) {
1365 $errormessage = $Lang::tr{'name too long'};
1366 goto VPNCONF_ERROR;
ac1cfefa 1367 }
ac1cfefa 1368
624615ee
LS
1369 # Check if there is no other entry with this name
1370 if (! $cgiparams{'KEY'}) { #only for add
1371 foreach my $key (keys %confighash) {
1372 if ($confighash{$key}[1] eq $cgiparams{'NAME'}) {
1373 $errormessage = $Lang::tr{'a connection with this name already exists'};
1374 goto VPNCONF_ERROR;
1375 }
1376 }
1377 }
ac1cfefa 1378
624615ee
LS
1379 if (($cgiparams{'TYPE'} eq 'net') && (! $cgiparams{'REMOTE'})) {
1380 $errormessage = $Lang::tr{'invalid input for remote host/ip'};
1381 goto VPNCONF_ERROR;
ac1cfefa 1382 }
ac1cfefa 1383
624615ee
LS
1384 if ($cgiparams{'REMOTE'}) {
1385 if (($cgiparams{'REMOTE'} ne '%any') && (! &General::validip($cgiparams{'REMOTE'}))) {
1386 if (! &General::validfqdn ($cgiparams{'REMOTE'})) {
1387 $errormessage = $Lang::tr{'invalid input for remote host/ip'};
1388 goto VPNCONF_ERROR;
1389 } else {
1390 if (&valid_dns_host($cgiparams{'REMOTE'})) {
1391 $warnmessage = "$Lang::tr{'check vpn lr'} $cgiparams{'REMOTE'}. $Lang::tr{'dns check failed'}";
1392 }
1393 }
1394 }
1395 }
ac1cfefa 1396
b1881251
MT
1397 my @local_subnets = split(",", $cgiparams{'LOCAL_SUBNET'});
1398 foreach my $subnet (@local_subnets) {
8792caad 1399 unless (&Network::check_subnet($subnet)) {
b1881251 1400 $errormessage = $Lang::tr{'local subnet is invalid'};
8792caad
MT
1401 goto VPNCONF_ERROR;
1402 }
ac1cfefa 1403 }
ac1cfefa 1404
624615ee
LS
1405 # Allow only one roadwarrior/psk without remote IP-address
1406 if ($cgiparams{'REMOTE'} eq '' && $cgiparams{'AUTH'} eq 'psk') {
1407 foreach my $key (keys %confighash) {
1408 if ( ($cgiparams{'KEY'} ne $key) &&
1409 ($confighash{$key}[4] eq 'psk') &&
1410 ($confighash{$key}[10] eq '') ) {
1411 $errormessage = $Lang::tr{'you can only define one roadwarrior connection when using pre-shared key authentication'};
1412 goto VPNCONF_ERROR;
1413 }
1414 }
1415 }
ac1cfefa 1416
b1881251
MT
1417 if ($cgiparams{'TYPE'} eq 'net') {
1418 my @remote_subnets = split(",", $cgiparams{'REMOTE_SUBNET'});
1419 foreach my $subnet (@remote_subnets) {
1420 unless (&Network::check_subnet($subnet)) {
1421 $errormessage = $Lang::tr{'remote subnet is invalid'};
1422 goto VPNCONF_ERROR;
1423 }
1424 }
624615ee 1425 }
ac1cfefa 1426
624615ee
LS
1427 if ($cgiparams{'ENABLED'} !~ /^(on|off)$/) {
1428 $errormessage = $Lang::tr{'invalid input'};
1429 goto VPNCONF_ERROR;
1430 }
1431 if ($cgiparams{'EDIT_ADVANCED'} !~ /^(on|off)$/) {
1432 $errormessage = $Lang::tr{'invalid input'};
1433 goto VPNCONF_ERROR;
1434 }
ed84e8b8 1435
624615ee
LS
1436 # Allow nothing or a string (DN,FDQN,) beginning with @
1437 # with no comma but slashes between RID eg @O=FR/C=Paris/OU=myhome/CN=franck
1438 if ( ($cgiparams{'LOCAL_ID'} !~ /^(|[\w.-]*@[\w. =*\/-]+|\d+\.\d+\.\d+\.\d+)$/) ||
1439 ($cgiparams{'REMOTE_ID'} !~ /^(|[\w.-]*@[\w. =*\/-]+|\d+\.\d+\.\d+\.\d+)$/) ||
1440 (($cgiparams{'REMOTE_ID'} eq $cgiparams{'LOCAL_ID'}) && ($cgiparams{'LOCAL_ID'} ne ''))
1441 ) {
1442 $errormessage = $Lang::tr{'invalid local-remote id'} . '<br />' .
1443 'DER_ASN1_DN: @c=FR/ou=Paris/ou=Home/cn=*<br />' .
1444 'FQDN: @ipfire.org<br />' .
1445 'USER_FQDN: info@ipfire.org<br />' .
1446 'IPV4_ADDR: 123.123.123.123';
1447 goto VPNCONF_ERROR;
1448 }
1449 # If Auth is DN, verify existance of Remote ID.
1450 if ( $cgiparams{'REMOTE_ID'} eq '' && (
1451 $cgiparams{'AUTH'} eq 'auth-dn'|| # while creation
1452 $confighash{$cgiparams{'KEY'}}[2] eq '%auth-dn')){ # while editing
1453 $errormessage = $Lang::tr{'vpn missing remote id'};
1454 goto VPNCONF_ERROR;
4d81e0f3 1455 }
4d81e0f3 1456
624615ee
LS
1457 if ($cgiparams{'TYPE'} eq 'net'){
1458 $warnmessage=&General::checksubnets('',$cgiparams{'REMOTE_SUBNET'},'ipsec');
1459 if ($warnmessage ne ''){
1460 $warnmessage=$Lang::tr{'remote subnet'}." ($cgiparams{'REMOTE_SUBNET'}) <br>".$warnmessage;
1461 }
1462 }
1463
1464 if ($cgiparams{'AUTH'} eq 'psk') {
1465 if (! length($cgiparams{'PSK'}) ) {
1466 $errormessage = $Lang::tr{'pre-shared key is too short'};
1467 goto VPNCONF_ERROR;
1468 }
1469 if ($cgiparams{'PSK'} =~ /'/) {
1470 $cgiparams{'PSK'} =~ tr/'/ /;
1471 $errormessage = $Lang::tr{'invalid characters found in pre-shared key'};
1472 goto VPNCONF_ERROR;
1473 }
ac1cfefa 1474 } elsif ($cgiparams{'AUTH'} eq 'certreq') {
624615ee
LS
1475 if ($cgiparams{'KEY'}) {
1476 $errormessage = $Lang::tr{'cant change certificates'};
1477 goto VPNCONF_ERROR;
1478 }
1479 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1480 $errormessage = $Lang::tr{'there was no file upload'};
1481 goto VPNCONF_ERROR;
1482 }
ac1cfefa 1483
624615ee
LS
1484 # Move uploaded certificate request to a temporary file
1485 (my $fh, my $filename) = tempfile( );
1486 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1487 $errormessage = $!;
1488 goto VPNCONF_ERROR;
1489 }
ac1cfefa 1490
624615ee
LS
1491 # Sign the certificate request
1492 &General::log("ipsec", "Signing your cert $cgiparams{'NAME'}...");
1493 my $opt = " ca -md sha256 -days 999999";
ed84e8b8
MT
1494 $opt .= " -batch -notext";
1495 $opt .= " -in $filename";
1496 $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}cert.pem";
1497
624615ee
LS
1498 if ( $errormessage = &callssl ($opt) ) {
1499 unlink ($filename);
1500 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1501 &cleanssldatabase();
1502 goto VPNCONF_ERROR;
1503 } else {
1504 unlink ($filename);
1505 &cleanssldatabase();
1506 }
1507
1508 $cgiparams{'CERT_NAME'} = getCNfromcert ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1509 if ($cgiparams{'CERT_NAME'} eq '') {
1510 $errormessage = $Lang::tr{'could not retrieve common name from certificate'};
1511 goto VPNCONF_ERROR;
1512 }
ed84e8b8
MT
1513 } elsif ($cgiparams{'AUTH'} eq 'pkcs12') {
1514 &General::log("ipsec", "Importing from p12...");
1515
1516 if (ref ($cgiparams{'FH'}) ne 'Fh') {
624615ee
LS
1517 $errormessage = $Lang::tr{'there was no file upload'};
1518 goto ROOTCERT_ERROR;
ed84e8b8
MT
1519 }
1520
1521 # Move uploaded certificate request to a temporary file
1522 (my $fh, my $filename) = tempfile( );
1523 if (copy ($cgiparams{'FH'}, $fh) != 1) {
624615ee
LS
1524 $errormessage = $!;
1525 goto ROOTCERT_ERROR;
ed84e8b8
MT
1526 }
1527
1528 # Extract the CA certificate from the file
1529 &General::log("ipsec", "Extracting caroot from p12...");
1530 if (open(STDIN, "-|")) {
624615ee 1531 my $opt = " pkcs12 -cacerts -nokeys";
ed84e8b8
MT
1532 $opt .= " -in $filename";
1533 $opt .= " -out /tmp/newcacert";
ed84e8b8 1534 $errormessage = &callssl ($opt);
624615ee 1535 } else { #child
ed84e8b8
MT
1536 print "$cgiparams{'P12_PASS'}\n";
1537 exit (0);
624615ee
LS
1538 }
1539
1540 # Extract the Host certificate from the file
1541 if (!$errormessage) {
1542 &General::log("ipsec", "Extracting host cert from p12...");
1543 if (open(STDIN, "-|")) {
1544 my $opt = " pkcs12 -clcerts -nokeys";
1545 $opt .= " -in $filename";
1546 $opt .= " -out /tmp/newhostcert";
1547 $errormessage = &callssl ($opt);
1548 } else { #child
1549 print "$cgiparams{'P12_PASS'}\n";
1550 exit (0);
1551 }
1552 }
1553
1554 if (!$errormessage) {
1555 &General::log("ipsec", "Moving cacert...");
1556 #If CA have new subject, add it to our list of CA
1557 my $casubject = &Header::cleanhtml(getsubjectfromcert ('/tmp/newcacert'));
1558 my @names;
1559 foreach my $x (keys %cahash) {
1560 $casubject='' if ($cahash{$x}[1] eq $casubject);
1561 unshift (@names,$cahash{$x}[0]);
1562 }
1563 if ($casubject) { # a new one!
1564 my $temp = `/usr/bin/openssl x509 -text -in /tmp/newcacert`;
1565 if ($temp !~ /CA:TRUE/i) {
1566 $errormessage = $Lang::tr{'not a valid ca certificate'};
1567 } else {
1568 #compute a name for it
1569 my $idx=0;
1570 while (grep(/Imported-$idx/, @names) ) {$idx++};
1571 $cgiparams{'CA_NAME'}="Imported-$idx";
1572 $cgiparams{'CERT_NAME'}=&Header::cleanhtml(getCNfromcert ('/tmp/newhostcert'));
1573 move("/tmp/newcacert", "${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem");
1574 $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
1575 if (!$errormessage) {
1576 my $key = &General::findhasharraykey (\%cahash);
1577 $cahash{$key}[0] = $cgiparams{'CA_NAME'};
1578 $cahash{$key}[1] = $casubject;
1579 &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
1580 system('/usr/local/bin/ipsecctrl', 'R');
1581 }
1582 }
1583 }
ed84e8b8
MT
1584 }
1585 if (!$errormessage) {
624615ee
LS
1586 &General::log("ipsec", "Moving host cert...");
1587 move("/tmp/newhostcert", "${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1588 $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
1589 }
ed84e8b8
MT
1590
1591 #cleanup temp files
1592 unlink ($filename);
1593 unlink ('/tmp/newcacert');
1594 unlink ('/tmp/newhostcert');
1595 if ($errormessage) {
624615ee
LS
1596 unlink ("${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem");
1597 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1598 goto VPNCONF_ERROR;
ed84e8b8
MT
1599 }
1600 &General::log("ipsec", "p12 import completed!");
ac1cfefa 1601 } elsif ($cgiparams{'AUTH'} eq 'certfile') {
624615ee
LS
1602 if ($cgiparams{'KEY'}) {
1603 $errormessage = $Lang::tr{'cant change certificates'};
1604 goto VPNCONF_ERROR;
1605 }
1606 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1607 $errormessage = $Lang::tr{'there was no file upload'};
1608 goto VPNCONF_ERROR;
1609 }
1610 # Move uploaded certificate to a temporary file
1611 (my $fh, my $filename) = tempfile( );
1612 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1613 $errormessage = $!;
1614 goto VPNCONF_ERROR;
ac1cfefa 1615 }
ac1cfefa 1616
624615ee
LS
1617 # Verify the certificate has a valid CA and move it
1618 &General::log("ipsec", "Validating imported cert against our known CA...");
1619 my $validca = 1; #assume ok
1620 my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/cacert.pem $filename`;
1621 if ($test !~ /: OK/) {
1622 my $validca = 0;
1623 foreach my $key (keys %cahash) {
1624 $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/$cahash{$key}[0]cert.pem $filename`;
1625 if ($test =~ /: OK/) {
1626 $validca = 1;
1627 last;
1628 }
1629 }
1630 }
1631 if (! $validca) {
1632 $errormessage = $Lang::tr{'certificate does not have a valid ca associated with it'};
1633 unlink ($filename);
1634 goto VPNCONF_ERROR;
1635 } else {
1636 move($filename, "${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1637 if ($? ne 0) {
1638 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1639 unlink ($filename);
1640 goto VPNCONF_ERROR;
1641 }
1642 }
1643
1644 $cgiparams{'CERT_NAME'} = getCNfromcert ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1645 if ($cgiparams{'CERT_NAME'} eq '') {
1646 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1647 $errormessage = $Lang::tr{'could not retrieve common name from certificate'};
1648 goto VPNCONF_ERROR;
1649 }
ac1cfefa 1650 } elsif ($cgiparams{'AUTH'} eq 'certgen') {
624615ee
LS
1651 if ($cgiparams{'KEY'}) {
1652 $errormessage = $Lang::tr{'cant change certificates'};
1653 goto VPNCONF_ERROR;
1654 }
1655 # Validate input since the form was submitted
1656 if (length($cgiparams{'CERT_NAME'}) >60) {
1657 $errormessage = $Lang::tr{'name too long'};
1658 goto VPNCONF_ERROR;
1659 }
1660 if ($cgiparams{'CERT_NAME'} !~ /^[a-zA-Z0-9 ,\.\-_]+$/) {
1661 $errormessage = $Lang::tr{'invalid input for name'};
1662 goto VPNCONF_ERROR;
1663 }
1664 if ($cgiparams{'CERT_EMAIL'} ne '' && (! &General::validemail($cgiparams{'CERT_EMAIL'}))) {
1665 $errormessage = $Lang::tr{'invalid input for e-mail address'};
1666 goto VPNCONF_ERROR;
1667 }
1668 if (length($cgiparams{'CERT_EMAIL'}) > 40) {
1669 $errormessage = $Lang::tr{'e-mail address too long'};
1670 goto VPNCONF_ERROR;
1671 }
1672 if ($cgiparams{'CERT_OU'} ne '' && $cgiparams{'CERT_OU'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1673 $errormessage = $Lang::tr{'invalid input for department'};
1674 goto VPNCONF_ERROR;
1675 }
1676 if (length($cgiparams{'CERT_ORGANIZATION'}) >60) {
1677 $errormessage = $Lang::tr{'organization too long'};
1678 goto VPNCONF_ERROR;
1679 }
1680 if ($cgiparams{'CERT_ORGANIZATION'} !~ /^[a-zA-Z0-9 ,\.\-_]+$/) {
1681 $errormessage = $Lang::tr{'invalid input for organization'};
1682 goto VPNCONF_ERROR;
1683 }
1684 if ($cgiparams{'CERT_CITY'} ne '' && $cgiparams{'CERT_CITY'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1685 $errormessage = $Lang::tr{'invalid input for city'};
1686 goto VPNCONF_ERROR;
1687 }
1688 if ($cgiparams{'CERT_STATE'} ne '' && $cgiparams{'CERT_STATE'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1689 $errormessage = $Lang::tr{'invalid input for state or province'};
1690 goto VPNCONF_ERROR;
1691 }
1692 if ($cgiparams{'CERT_COUNTRY'} !~ /^[A-Z]*$/) {
1693 $errormessage = $Lang::tr{'invalid input for country'};
1694 goto VPNCONF_ERROR;
1695 }
1696 #the exact syntax is a list comma separated of
1697 # email:any-validemail
1698 # URI: a uniform resource indicator
1699 # DNS: a DNS domain name
1700 # RID: a registered OBJECT IDENTIFIER
1701 # IP: an IP address
1702 # example: email:franck@foo.com,IP:10.0.0.10,DNS:franck.foo.com
1703
1704 if ($cgiparams{'SUBJECTALTNAME'} ne '' && $cgiparams{'SUBJECTALTNAME'} !~ /^(email|URI|DNS|RID|IP):[a-zA-Z0-9 :\/,\.\-_@]*$/) {
1705 $errormessage = $Lang::tr{'vpn altname syntax'};
1706 goto VPNCONF_ERROR;
1707 }
ed84e8b8 1708
624615ee
LS
1709 if (length($cgiparams{'CERT_PASS1'}) < 5) {
1710 $errormessage = $Lang::tr{'password too short'};
1711 goto VPNCONF_ERROR;
1712 }
1713 if ($cgiparams{'CERT_PASS1'} ne $cgiparams{'CERT_PASS2'}) {
1714 $errormessage = $Lang::tr{'passwords do not match'};
1715 goto VPNCONF_ERROR;
1716 }
ac1cfefa 1717
624615ee
LS
1718 # Replace empty strings with a .
1719 (my $ou = $cgiparams{'CERT_OU'}) =~ s/^\s*$/\./;
1720 (my $city = $cgiparams{'CERT_CITY'}) =~ s/^\s*$/\./;
1721 (my $state = $cgiparams{'CERT_STATE'}) =~ s/^\s*$/\./;
ac1cfefa 1722
624615ee
LS
1723 # Create the Client certificate request
1724 &General::log("ipsec", "Creating a cert...");
ed84e8b8 1725
624615ee
LS
1726 if (open(STDIN, "-|")) {
1727 my $opt = " req -nodes -rand /proc/interrupts:/proc/net/rt_cache";
1728 $opt .= " -newkey rsa:2048";
1729 $opt .= " -keyout ${General::swroot}/certs/$cgiparams{'NAME'}key.pem";
1730 $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}req.pem";
1731
1732 if ( $errormessage = &callssl ($opt) ) {
1733 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}key.pem");
1734 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}req.pem");
1735 goto VPNCONF_ERROR;
1736 }
1737 } else { #child
1738 print "$cgiparams{'CERT_COUNTRY'}\n";
1739 print "$state\n";
1740 print "$city\n";
1741 print "$cgiparams{'CERT_ORGANIZATION'}\n";
1742 print "$ou\n";
1743 print "$cgiparams{'CERT_NAME'}\n";
1744 print "$cgiparams{'CERT_EMAIL'}\n";
1745 print ".\n";
1746 print ".\n";
1747 exit (0);
1748 }
ed84e8b8 1749
624615ee
LS
1750 # Sign the client certificate request
1751 &General::log("ipsec", "Signing the cert $cgiparams{'NAME'}...");
1752
1753 #No easy way for specifying the contain of subjectAltName without writing a config file...
1754 my ($fh, $v3extname) = tempfile ('/tmp/XXXXXXXX');
1755 print $fh <<END
1756 basicConstraints=CA:FALSE
1757 nsComment="OpenSSL Generated Certificate"
1758 subjectKeyIdentifier=hash
1759 extendedKeyUsage=clientAuth
1760 authorityKeyIdentifier=keyid,issuer:always
ed84e8b8
MT
1761END
1762;
624615ee
LS
1763 print $fh "subjectAltName=$cgiparams{'SUBJECTALTNAME'}" if ($cgiparams{'SUBJECTALTNAME'});
1764 close ($fh);
1765
1766 my $opt = " ca -md sha256 -days 999999 -batch -notext";
1767 $opt .= " -in ${General::swroot}/certs/$cgiparams{'NAME'}req.pem";
1768 $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}cert.pem";
1769 $opt .= " -extfile $v3extname";
1770
1771 if ( $errormessage = &callssl ($opt) ) {
1772 unlink ($v3extname);
1773 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}key.pem");
1774 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}req.pem");
1775 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1776 &cleanssldatabase();
1777 goto VPNCONF_ERROR;
1778 } else {
1779 unlink ($v3extname);
1780 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}req.pem");
1781 &cleanssldatabase();
1782 }
1783
1784 # Create the pkcs12 file
1785 &General::log("ipsec", "Packing a pkcs12 file...");
1786 $opt = " pkcs12 -export";
1787 $opt .= " -inkey ${General::swroot}/certs/$cgiparams{'NAME'}key.pem";
1788 $opt .= " -in ${General::swroot}/certs/$cgiparams{'NAME'}cert.pem";
1789 $opt .= " -name \"$cgiparams{'NAME'}\"";
1790 $opt .= " -passout pass:$cgiparams{'CERT_PASS1'}";
1791 $opt .= " -certfile ${General::swroot}/ca/cacert.pem";
1792 $opt .= " -caname \"$vpnsettings{'ROOTCERT_ORGANIZATION'} CA\"";
1793 $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}.p12";
1794
1795 if ( $errormessage = &callssl ($opt) ) {
1796 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}key.pem");
1797 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1798 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}.p12");
1799 goto VPNCONF_ERROR;
1800 } else {
1801 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}key.pem");
1802 }
ac1cfefa 1803 } elsif ($cgiparams{'AUTH'} eq 'cert') {
624615ee 1804 ;# Nothing, just editing
ed84e8b8 1805 } elsif ($cgiparams{'AUTH'} eq 'auth-dn') {
624615ee 1806 $cgiparams{'CERT_NAME'} = '%auth-dn'; # a special value saying 'no cert file'
ac1cfefa 1807 } else {
624615ee
LS
1808 $errormessage = $Lang::tr{'invalid input for authentication method'};
1809 goto VPNCONF_ERROR;
ac1cfefa
MT
1810 }
1811
ed84e8b8
MT
1812 # 1)Error message here is not accurate.
1813 # 2)Test is superfluous, openswan can reference same cert multiple times
1814 # 3)Present since initial version (1.3.2.11), it isn't a bug correction
1815 # Check if there is no other entry with this certificate name
1816 #if ((! $cgiparams{'KEY'}) && ($cgiparams{'AUTH'} ne 'psk') && ($cgiparams{'AUTH'} ne 'auth-dn')) {
624615ee 1817 # foreach my $key (keys %confighash) {
ed84e8b8 1818 # if ($confighash{$key}[2] eq $cgiparams{'CERT_NAME'}) {
624615ee
LS
1819 # $errormessage = $Lang::tr{'a connection with this common name already exists'};
1820 # goto VPNCONF_ERROR;
1821 # }
ed84e8b8 1822 # }
ed84e8b8 1823 #}
624615ee 1824 # Save the config
ed84e8b8 1825
ac1cfefa
MT
1826 my $key = $cgiparams{'KEY'};
1827 if (! $key) {
624615ee 1828 $key = &General::findhasharraykey (\%confighash);
cae1f4a7 1829 foreach my $i (0 .. 36) { $confighash{$key}[$i] = "";}
ac1cfefa
MT
1830 }
1831 $confighash{$key}[0] = $cgiparams{'ENABLED'};
1832 $confighash{$key}[1] = $cgiparams{'NAME'};
1833 if ((! $cgiparams{'KEY'}) && $cgiparams{'AUTH'} ne 'psk') {
624615ee 1834 $confighash{$key}[2] = $cgiparams{'CERT_NAME'};
ac1cfefa
MT
1835 }
1836 $confighash{$key}[3] = $cgiparams{'TYPE'};
1837 if ($cgiparams{'AUTH'} eq 'psk') {
624615ee
LS
1838 $confighash{$key}[4] = 'psk';
1839 $confighash{$key}[5] = $cgiparams{'PSK'};
ac1cfefa 1840 } else {
624615ee 1841 $confighash{$key}[4] = 'cert';
ac1cfefa
MT
1842 }
1843 if ($cgiparams{'TYPE'} eq 'net') {
b1881251
MT
1844 my @remote_subnets = split(",", $cgiparams{'REMOTE_SUBNET'});
1845 $confighash{$key}[11] = join('|', @remote_subnets);
ac1cfefa
MT
1846 }
1847 $confighash{$key}[7] = $cgiparams{'LOCAL_ID'};
8792caad
MT
1848 my @local_subnets = split(",", $cgiparams{'LOCAL_SUBNET'});
1849 $confighash{$key}[8] = join('|', @local_subnets);
ac1cfefa
MT
1850 $confighash{$key}[9] = $cgiparams{'REMOTE_ID'};
1851 $confighash{$key}[10] = $cgiparams{'REMOTE'};
1852 $confighash{$key}[25] = $cgiparams{'REMARK'};
ae2782ba 1853 $confighash{$key}[26] = ""; # Formerly INTERFACE
ac1cfefa 1854 $confighash{$key}[27] = $cgiparams{'DPD_ACTION'};
e2e4ed01 1855 $confighash{$key}[29] = $cgiparams{'IKE_VERSION'};
ac1cfefa 1856
624615ee 1857 # don't forget advanced value
ed84e8b8
MT
1858 $confighash{$key}[18] = $cgiparams{'IKE_ENCRYPTION'};
1859 $confighash{$key}[19] = $cgiparams{'IKE_INTEGRITY'};
1860 $confighash{$key}[20] = $cgiparams{'IKE_GROUPTYPE'};
1861 $confighash{$key}[16] = $cgiparams{'IKE_LIFETIME'};
1862 $confighash{$key}[21] = $cgiparams{'ESP_ENCRYPTION'};
1863 $confighash{$key}[22] = $cgiparams{'ESP_INTEGRITY'};
1864 $confighash{$key}[23] = $cgiparams{'ESP_GROUPTYPE'};
1865 $confighash{$key}[17] = $cgiparams{'ESP_KEYLIFE'};
451a2f68 1866 $confighash{$key}[12] = 'off'; # $cgiparams{'AGGRMODE'};
ed84e8b8
MT
1867 $confighash{$key}[13] = $cgiparams{'COMPRESSION'};
1868 $confighash{$key}[24] = $cgiparams{'ONLY_PROPOSED'};
1869 $confighash{$key}[28] = $cgiparams{'PFS'};
4e156911
AM
1870 $confighash{$key}[30] = $cgiparams{'DPD_TIMEOUT'};
1871 $confighash{$key}[31] = $cgiparams{'DPD_DELAY'};
f6529a04 1872 $confighash{$key}[32] = $cgiparams{'FORCE_MOBIKE'};
af183eeb 1873 $confighash{$key}[34] = $cgiparams{'INACTIVITY_TIMEOUT'};
29f5e0e2 1874 $confighash{$key}[35] = $cgiparams{'MODE'};
cae1f4a7 1875 $confighash{$key}[36] = $cgiparams{'INTERFACE_MODE'};
ac1cfefa 1876
624615ee 1877 # free unused fields!
5fd30232 1878 $confighash{$key}[6] = 'off';
ed84e8b8 1879 $confighash{$key}[15] = 'off';
ac1cfefa
MT
1880
1881 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
1882 &writeipsecfiles();
ed84e8b8 1883 if (&vpnenabled) {
624615ee
LS
1884 system('/usr/local/bin/ipsecctrl', 'S', $key);
1885 sleep $sleepDelay;
ac1cfefa
MT
1886 }
1887 if ($cgiparams{'EDIT_ADVANCED'} eq 'on') {
624615ee
LS
1888 $cgiparams{'KEY'} = $key;
1889 $cgiparams{'ACTION'} = $Lang::tr{'advanced'};
ac1cfefa
MT
1890 }
1891 goto VPNCONF_END;
624615ee
LS
1892} else { # add new connection
1893 $cgiparams{'ENABLED'} = 'on';
ac1cfefa 1894 if ( ! -f "${General::swroot}/private/cakey.pem" ) {
624615ee 1895 $cgiparams{'AUTH'} = 'psk';
ac1cfefa 1896 } elsif ( ! -f "${General::swroot}/ca/cacert.pem") {
624615ee 1897 $cgiparams{'AUTH'} = 'certfile';
ac1cfefa 1898 } else {
624615ee 1899 $cgiparams{'AUTH'} = 'certgen';
ac1cfefa 1900 }
624615ee
LS
1901 $cgiparams{'LOCAL_SUBNET'} = "$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}";
1902 $cgiparams{'CERT_EMAIL'} = $vpnsettings{'ROOTCERT_EMAIL'};
1903 $cgiparams{'CERT_OU'} = $vpnsettings{'ROOTCERT_OU'};
1904 $cgiparams{'CERT_ORGANIZATION'} = $vpnsettings{'ROOTCERT_ORGANIZATION'};
1905 $cgiparams{'CERT_CITY'} = $vpnsettings{'ROOTCERT_CITY'};
1906 $cgiparams{'CERT_STATE'} = $vpnsettings{'ROOTCERT_STATE'};
1907 $cgiparams{'CERT_COUNTRY'} = $vpnsettings{'ROOTCERT_COUNTRY'};
ac1cfefa 1908
624615ee 1909 # choose appropriate dpd action
ac1cfefa 1910 if ($cgiparams{'TYPE'} eq 'host') {
afd5d8f7 1911 $cgiparams{'DPD_ACTION'} = 'clear';
ac1cfefa 1912 } else {
afd5d8f7 1913 $cgiparams{'DPD_ACTION'} = 'restart';
ac1cfefa
MT
1914 }
1915
cbb3a8f9
MT
1916 if (!$cgiparams{'DPD_DELAY'}) {
1917 $cgiparams{'DPD_DELAY'} = 30;
1918 }
1919
1920 if (!$cgiparams{'DPD_TIMEOUT'}) {
1921 $cgiparams{'DPD_TIMEOUT'} = 120;
1922 }
1923
f6529a04
MT
1924 if (!$cgiparams{'FORCE_MOBIKE'}) {
1925 $cgiparams{'FORCE_MOBIKE'} = 'no';
1926 }
1927
ae2782ba
MT
1928 # Default IKE Version to v2
1929 if (!$cgiparams{'IKE_VERSION'}) {
624615ee 1930 $cgiparams{'IKE_VERSION'} = 'ikev2';
e2e4ed01
AF
1931 }
1932
ac1cfefa 1933 # ID are empty
624615ee 1934 $cgiparams{'LOCAL_ID'} = '';
ac1cfefa 1935 $cgiparams{'REMOTE_ID'} = '';
ed84e8b8
MT
1936
1937 #use default advanced value
05375f12 1938 $cgiparams{'IKE_ENCRYPTION'} = 'chacha20poly1305|aes256gcm128|aes256gcm96|aes256gcm64|aes256|aes192gcm128|aes192gcm96|aes192gcm64|aes192|aes128gcm128|aes128gcm96|aes128gcm64|aes128'; #[18];
570d54fd 1939 $cgiparams{'IKE_INTEGRITY'} = 'sha2_512|sha2_256'; #[19];
9bc2e596 1940 $cgiparams{'IKE_GROUPTYPE'} = 'curve25519|4096|3072|2048'; #[20];
624615ee 1941 $cgiparams{'IKE_LIFETIME'} = '3'; #[16];
05375f12 1942 $cgiparams{'ESP_ENCRYPTION'} = 'chacha20poly1305|aes256gcm128|aes256gcm96|aes256gcm64|aes256|aes192gcm128|aes192gcm96|aes192gcm64|aes192|aes128gcm128|aes128gcm96|aes128gcm64|aes128'; #[21];
570d54fd 1943 $cgiparams{'ESP_INTEGRITY'} = 'sha2_512|sha2_256'; #[22];
9bc2e596 1944 $cgiparams{'ESP_GROUPTYPE'} = 'curve25519|4096|3072|2048'; #[23];
624615ee 1945 $cgiparams{'ESP_KEYLIFE'} = '1'; #[17];
120d77b3 1946 $cgiparams{'COMPRESSION'} = 'off'; #[13];
570d54fd 1947 $cgiparams{'ONLY_PROPOSED'} = 'on'; #[24];
624615ee 1948 $cgiparams{'PFS'} = 'on'; #[28];
af183eeb 1949 $cgiparams{'INACTIVITY_TIMEOUT'} = 900;
29f5e0e2 1950 $cgiparams{'MODE'} = "tunnel";
cae1f4a7 1951 $cgiparams{'INTERFACE_MODE'} = "";
624615ee 1952}
ac1cfefa 1953
624615ee
LS
1954VPNCONF_ERROR:
1955 $checked{'ENABLED'}{'off'} = '';
1956 $checked{'ENABLED'}{'on'} = '';
1957 $checked{'ENABLED'}{$cgiparams{'ENABLED'}} = "checked='checked'";
1958
1959 $checked{'EDIT_ADVANCED'}{'off'} = '';
1960 $checked{'EDIT_ADVANCED'}{'on'} = '';
1961 $checked{'EDIT_ADVANCED'}{$cgiparams{'EDIT_ADVANCED'}} = "checked='checked'";
1962
1963 $checked{'AUTH'}{'psk'} = '';
1964 $checked{'AUTH'}{'certreq'} = '';
1965 $checked{'AUTH'}{'certgen'} = '';
1966 $checked{'AUTH'}{'certfile'} = '';
1967 $checked{'AUTH'}{'pkcs12'} = '';
1968 $checked{'AUTH'}{'auth-dn'} = '';
1969 $checked{'AUTH'}{$cgiparams{'AUTH'}} = "checked='checked'";
1970
1971 &Header::showhttpheaders();
1972 &Header::openpage($Lang::tr{'ipsec'}, 1, '');
1973 &Header::openbigbox('100%', 'left', '', $errormessage);
1974 if ($errormessage) {
1975 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
1976 print "<class name='base'>$errormessage";
1977 print "&nbsp;</class>";
1978 &Header::closebox();
1979 }
1980
1981 if ($warnmessage) {
1982 &Header::openbox('100%', 'left', "$Lang::tr{'warning messages'}:");
1983 print "<class name='base'>$warnmessage";
1984 print "&nbsp;</class>";
1985 &Header::closebox();
1986 }
ac1cfefa 1987
624615ee
LS
1988 print "<form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'>";
1989 print<<END
ed84e8b8 1990 <input type='hidden' name='TYPE' value='$cgiparams{'TYPE'}' />
4ad0b5b6 1991 <input type='hidden' name='IKE_VERSION' value='$cgiparams{'IKE_VERSION'}' />
ed84e8b8
MT
1992 <input type='hidden' name='IKE_ENCRYPTION' value='$cgiparams{'IKE_ENCRYPTION'}' />
1993 <input type='hidden' name='IKE_INTEGRITY' value='$cgiparams{'IKE_INTEGRITY'}' />
1994 <input type='hidden' name='IKE_GROUPTYPE' value='$cgiparams{'IKE_GROUPTYPE'}' />
1995 <input type='hidden' name='IKE_LIFETIME' value='$cgiparams{'IKE_LIFETIME'}' />
1996 <input type='hidden' name='ESP_ENCRYPTION' value='$cgiparams{'ESP_ENCRYPTION'}' />
1997 <input type='hidden' name='ESP_INTEGRITY' value='$cgiparams{'ESP_INTEGRITY'}' />
1998 <input type='hidden' name='ESP_GROUPTYPE' value='$cgiparams{'ESP_GROUPTYPE'}' />
1999 <input type='hidden' name='ESP_KEYLIFE' value='$cgiparams{'ESP_KEYLIFE'}' />
ed84e8b8
MT
2000 <input type='hidden' name='COMPRESSION' value='$cgiparams{'COMPRESSION'}' />
2001 <input type='hidden' name='ONLY_PROPOSED' value='$cgiparams{'ONLY_PROPOSED'}' />
2002 <input type='hidden' name='PFS' value='$cgiparams{'PFS'}' />
cbb3a8f9
MT
2003 <input type='hidden' name='DPD_ACTION' value='$cgiparams{'DPD_ACTION'}' />
2004 <input type='hidden' name='DPD_DELAY' value='$cgiparams{'DPD_DELAY'}' />
2005 <input type='hidden' name='DPD_TIMEOUT' value='$cgiparams{'DPD_TIMEOUT'}' />
f6529a04 2006 <input type='hidden' name='FORCE_MOBIKE' value='$cgiparams{'FORCE_MOBIKE'}' />
5e6fa03e 2007 <input type='hidden' name='INACTIVITY_TIMEOUT' value='$cgiparams{'INACTIVITY_TIMEOUT'}' />
29f5e0e2 2008 <input type='hidden' name='MODE' value='$cgiparams{'MODE'}' />
cae1f4a7 2009 <input type='hidden' name='INTERFACE_MODE' value='$cgiparams{'INTERFACE_MODE'}' />
ed84e8b8 2010END
624615ee
LS
2011;
2012 if ($cgiparams{'KEY'}) {
2013 print "<input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />";
2014 print "<input type='hidden' name='NAME' value='$cgiparams{'NAME'}' />";
2015 print "<input type='hidden' name='AUTH' value='$cgiparams{'AUTH'}' />";
2016 }
2017
2018 &Header::openbox('100%', 'left', "$Lang::tr{'connection'}: $cgiparams{'NAME'}");
2019 print "<table width='100%'>";
2020 if (!$cgiparams{'KEY'}) {
2021 print <<EOF;
2022 <tr>
2023 <td width='20%'>$Lang::tr{'name'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
2024 <td width='30%'>
2025 <input type='text' name='NAME' value='$cgiparams{'NAME'}' size='25' />
2026 </td>
2027 <td colspan="2"></td>
2028 </tr>
d2d87f2c 2029EOF
624615ee 2030 }
ac1cfefa 2031
624615ee
LS
2032 my $disabled;
2033 my $blob;
2034 if ($cgiparams{'TYPE'} eq 'host') {
e3edceeb 2035 $disabled = "disabled='disabled'";
624615ee 2036 } elsif ($cgiparams{'TYPE'} eq 'net') {
e3edceeb 2037 $blob = "<img src='/blob.gif' alt='*' />";
624615ee 2038 };
5fd30232 2039
b1881251
MT
2040 my @local_subnets = split(/\|/, $cgiparams{'LOCAL_SUBNET'});
2041 my $local_subnets = join(",", @local_subnets);
8792caad 2042
b1881251
MT
2043 my @remote_subnets = split(/\|/, $cgiparams{'REMOTE_SUBNET'});
2044 my $remote_subnets = join(",", @remote_subnets);
8792caad 2045
624615ee 2046 print <<END
ae2782ba 2047 <tr>
d2d87f2c
MT
2048 <td width='20%'>$Lang::tr{'enabled'}</td>
2049 <td width='30%'>
2050 <input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} />
2051 </td>
624615ee
LS
2052 <td class='boldbase' nowrap='nowrap' width='20%'>$Lang::tr{'local subnet'}&nbsp;<img src='/blob.gif' alt='*' /></td>
2053 <td width='30%'>
b1881251 2054 <input type='text' name='LOCAL_SUBNET' value='$local_subnets' />
624615ee 2055 </td>
d2d87f2c
MT
2056 </tr>
2057 <tr>
624615ee
LS
2058 <td class='boldbase' width='20%'>$Lang::tr{'remote host/ip'}:&nbsp;$blob</td>
2059 <td width='30%'>
2060 <input type='text' name='REMOTE' value='$cgiparams{'REMOTE'}' size="25" />
2061 </td>
2062 <td class='boldbase' nowrap='nowrap' width='20%'>$Lang::tr{'remote subnet'}&nbsp;$blob</td>
2063 <td width='30%'>
b1881251 2064 <input $disabled type='text' name='REMOTE_SUBNET' value='$remote_subnets' />
624615ee 2065 </td>
ae2782ba
MT
2066 </tr>
2067 <tr>
624615ee
LS
2068 <td class='boldbase' width='20%'>$Lang::tr{'vpn local id'}:</td>
2069 <td width='30%'>
2070 <input type='text' name='LOCAL_ID' value='$cgiparams{'LOCAL_ID'}' size="25" />
2071 </td>
2072 <td class='boldbase' width='20%'>$Lang::tr{'vpn remote id'}:</td>
2073 <td width='30%'>
2074 <input type='text' name='REMOTE_ID' value='$cgiparams{'REMOTE_ID'}' size="25" />
2075 </td>
ae2782ba 2076 </tr>
d2d87f2c 2077 <tr><td colspan="4"><br /></td></tr>
ae2782ba 2078 <tr>
624615ee
LS
2079 <td class='boldbase' width='20%'>$Lang::tr{'remark title'}</td>
2080 <td colspan='3'>
2081 <input type='text' name='REMARK' value='$cgiparams{'REMARK'}' maxlength='50' size="73" />
2082 </td>
ed84e8b8 2083 </tr>
ac1cfefa 2084END
624615ee
LS
2085;
2086 if (!$cgiparams{'KEY'}) {
2087 print "<tr><td colspan='3'><input type='checkbox' name='EDIT_ADVANCED' $checked{'EDIT_ADVANCED'}{'on'} /> $Lang::tr{'edit advanced settings when done'}</td></tr>";
2088 }
2089 print "</table>";
ed84e8b8 2090 &Header::closebox();
ed84e8b8 2091
624615ee
LS
2092 if ($cgiparams{'KEY'} && $cgiparams{'AUTH'} eq 'psk') {
2093 &Header::openbox('100%', 'left', $Lang::tr{'authentication'});
2094 print <<END
2095 <table width='100%' cellpadding='0' cellspacing='5' border='0'>
2096 <tr><td class='base' width='50%'>$Lang::tr{'use a pre-shared key'}</td>
2097 <td class='base' width='50%'><input type='password' name='PSK' size='30' value='$cgiparams{'PSK'}' /></td>
2098 </tr>
2099 </table>
ac1cfefa 2100END
624615ee
LS
2101;
2102 &Header::closebox();
2103 } elsif (! $cgiparams{'KEY'}) {
2104 my $cakeydisabled = ( ! -f "${General::swroot}/private/cakey.pem" ) ? "disabled='disabled'" : '';
2105 $cgiparams{'CERT_NAME'} = $Lang::tr{'vpn no full pki'} if ($cakeydisabled);
2106 my $cacrtdisabled = ( ! -f "${General::swroot}/ca/cacert.pem" ) ? "disabled='disabled'" : '';
2107
2108 &Header::openbox('100%', 'left', $Lang::tr{'authentication'});
2109 print <<END
2110 <table width='100%' cellpadding='0' cellspacing='5' border='0'>
2111 <tr><td width='5%'><input type='radio' name='AUTH' value='psk' $checked{'AUTH'}{'psk'} /></td>
2112 <td class='base' width='55%'>$Lang::tr{'use a pre-shared key'}</td>
2113 <td class='base' width='40%'><input type='password' name='PSK' size='30' value='$cgiparams{'PSK'}' /></td></tr>
2114 <tr><td colspan='3' bgcolor='#000000'></td></tr>
2115 <tr><td><input type='radio' name='AUTH' value='certreq' $checked{'AUTH'}{'certreq'} $cakeydisabled /></td>
2116 <td class='base'><hr />$Lang::tr{'upload a certificate request'}</td>
2117 <td class='base' rowspan='3' valign='middle'><input type='file' name='FH' size='30' $cacrtdisabled /></td></tr>
2118 <tr><td><input type='radio' name='AUTH' value='certfile' $checked{'AUTH'}{'certfile'} $cacrtdisabled /></td>
2119 <td class='base'>$Lang::tr{'upload a certificate'}</td></tr>
2120 <tr><td><input type='radio' name='AUTH' value='pkcs12' $cacrtdisabled /></td>
2121 <td class='base'>$Lang::tr{'upload p12 file'} $Lang::tr{'pkcs12 file password'}:<input type='password' name='P12_PASS'/></td></tr>
2122 <tr><td><input type='radio' name='AUTH' value='auth-dn' $checked{'AUTH'}{'auth-dn'} $cacrtdisabled /></td>
2123 <td class='base'><hr />$Lang::tr{'vpn auth-dn'}</td></tr>
2124 <tr><td colspan='3' bgcolor='#000000'></td></tr>
2125 <tr><td><input type='radio' name='AUTH' value='certgen' $checked{'AUTH'}{'certgen'} $cakeydisabled /></td>
2126 <td class='base'><hr />$Lang::tr{'generate a certificate'}</td><td>&nbsp;</td></tr>
2127 <tr><td>&nbsp;</td>
2128 <td class='base'>$Lang::tr{'users fullname or system hostname'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
2129 <td class='base' nowrap='nowrap'><input type='text' name='CERT_NAME' value='$cgiparams{'CERT_NAME'}' size='32' $cakeydisabled /></td></tr>
2130 <tr><td>&nbsp;</td>
2131 <td class='base'>$Lang::tr{'users email'}:</td>
2132 <td class='base' nowrap='nowrap'><input type='text' name='CERT_EMAIL' value='$cgiparams{'CERT_EMAIL'}' size='32' $cakeydisabled /></td></tr>
2133 <tr><td>&nbsp;</td>
2134 <td class='base'>$Lang::tr{'users department'}:</td>
2135 <td class='base' nowrap='nowrap'><input type='text' name='CERT_OU' value='$cgiparams{'CERT_OU'}' size='32' $cakeydisabled /></td></tr>
2136 <tr><td>&nbsp;</td>
2158e11b 2137 <td class='base'>$Lang::tr{'organization name'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
624615ee
LS
2138 <td class='base' nowrap='nowrap'><input type='text' name='CERT_ORGANIZATION' value='$cgiparams{'CERT_ORGANIZATION'}' size='32' $cakeydisabled /></td></tr>
2139 <tr><td>&nbsp;</td>
2140 <td class='base'>$Lang::tr{'city'}:</td>
2141 <td class='base' nowrap='nowrap'><input type='text' name='CERT_CITY' value='$cgiparams{'CERT_CITY'}' size='32' $cakeydisabled /></td></tr>
2142 <tr><td>&nbsp;</td>
2143 <td class='base'>$Lang::tr{'state or province'}:</td>
2144 <td class='base' nowrap='nowrap'><input type='text' name='CERT_STATE' value='$cgiparams{'CERT_STATE'}' size='32' $cakeydisabled /></td></tr>
2145 <tr><td>&nbsp;</td>
2146 <td class='base'>$Lang::tr{'country'}:</td>
2147 <td class='base'><select name='CERT_COUNTRY' $cakeydisabled>
2148END
2149;
2150 foreach my $country (sort keys %{Countries::countries}) {
2151 print "\t\t\t<option value='$Countries::countries{$country}'";
2152 if ( $Countries::countries{$country} eq $cgiparams{'CERT_COUNTRY'} ) {
2153 print " selected='selected'";
2154 }
2155 print ">$country</option>\n";
2156 }
2157 print <<END
2158 </select></td></tr>
2159
2160 <tr><td>&nbsp;</td><td class='base'>$Lang::tr{'vpn subjectaltname'} (subjectAltName=email:*,URI:*,DNS:*,RID:*)</td>
2161 <td class='base' nowrap='nowrap'><input type='text' name='SUBJECTALTNAME' value='$cgiparams{'SUBJECTALTNAME'}' size='32' $cakeydisabled /></td></tr>
2162 <tr><td>&nbsp;</td>
2163 <td class='base'>$Lang::tr{'pkcs12 file password'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
2164 <td class='base' nowrap='nowrap'><input type='password' name='CERT_PASS1' value='$cgiparams{'CERT_PASS1'}' size='32' $cakeydisabled /></td></tr>
2165 <tr><td>&nbsp;</td><td class='base'>$Lang::tr{'pkcs12 file password'}&nbsp;($Lang::tr{'confirmation'}):&nbsp;<img src='/blob.gif' alt='*' /></td>
2166 <td class='base' nowrap='nowrap'><input type='password' name='CERT_PASS2' value='$cgiparams{'CERT_PASS2'}' size='32' $cakeydisabled /></td></tr>
2167 </table>
2168END
2169;
2170 &Header::closebox();
ac1cfefa
MT
2171 }
2172
624615ee
LS
2173 print "<div align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' />";
2174 if ($cgiparams{'KEY'}) {
2175 print "<input type='submit' name='ACTION' value='$Lang::tr{'advanced'}' />";
2176 }
2177 print "<input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></div></form>";
2178 &Header::closebigbox();
2179 &Header::closepage();
2180 exit (0);
2181
2182 VPNCONF_END:
ac1cfefa
MT
2183}
2184
2185###
2186### Advanced settings
2187###
2188if(($cgiparams{'ACTION'} eq $Lang::tr{'advanced'}) ||
2189 ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'ADVANCED'} eq 'yes')) {
624615ee
LS
2190 &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
2191 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
2192 if (! $confighash{$cgiparams{'KEY'}}) {
2193 $errormessage = $Lang::tr{'invalid key'};
2194 goto ADVANCED_END;
2195 }
2196
2197 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
2198 my @temp = split('\|', $cgiparams{'IKE_ENCRYPTION'});
2199 if ($#temp < 0) {
2200 $errormessage = $Lang::tr{'invalid input'};
2201 goto ADVANCED_ERROR;
2202 }
2203 foreach my $val (@temp) {
05375f12 2204 if ($val !~ /^(aes(256|192|128)(gcm(128|96|64))?|3des|chacha20poly1305|camellia(256|192|128))$/) {
624615ee
LS
2205 $errormessage = $Lang::tr{'invalid input'};
2206 goto ADVANCED_ERROR;
2207 }
2208 }
2209 @temp = split('\|', $cgiparams{'IKE_INTEGRITY'});
2210 if ($#temp < 0) {
2211 $errormessage = $Lang::tr{'invalid input'};
2212 goto ADVANCED_ERROR;
2213 }
2214 foreach my $val (@temp) {
2215 if ($val !~ /^(sha2_(512|384|256)|sha|md5|aesxcbc)$/) {
2216 $errormessage = $Lang::tr{'invalid input'};
2217 goto ADVANCED_ERROR;
2218 }
2219 }
2220 @temp = split('\|', $cgiparams{'IKE_GROUPTYPE'});
2221 if ($#temp < 0) {
2222 $errormessage = $Lang::tr{'invalid input'};
2223 goto ADVANCED_ERROR;
2224 }
2225 foreach my $val (@temp) {
8c6b02e7 2226 if ($val !~ /^(curve25519|e521|e384|e256|e224|e192|e512bp|e384bp|e256bp|e224bp|768|1024|1536|2048|3072|4096|6144|8192)$/) {
624615ee
LS
2227 $errormessage = $Lang::tr{'invalid input'};
2228 goto ADVANCED_ERROR;
2229 }
2230 }
2231 if ($cgiparams{'IKE_LIFETIME'} !~ /^\d+$/) {
2232 $errormessage = $Lang::tr{'invalid input for ike lifetime'};
2233 goto ADVANCED_ERROR;
2234 }
2235 if ($cgiparams{'IKE_LIFETIME'} < 1 || $cgiparams{'IKE_LIFETIME'} > 8) {
2236 $errormessage = $Lang::tr{'ike lifetime should be between 1 and 8 hours'};
2237 goto ADVANCED_ERROR;
2238 }
2239 @temp = split('\|', $cgiparams{'ESP_ENCRYPTION'});
2240 if ($#temp < 0) {
2241 $errormessage = $Lang::tr{'invalid input'};
2242 goto ADVANCED_ERROR;
2243 }
2244 foreach my $val (@temp) {
05375f12 2245 if ($val !~ /^(aes(256|192|128)(gcm(128|96|64))?|3des|chacha20poly1305|camellia(256|192|128))$/) {
624615ee
LS
2246 $errormessage = $Lang::tr{'invalid input'};
2247 goto ADVANCED_ERROR;
2248 }
2249 }
2250 @temp = split('\|', $cgiparams{'ESP_INTEGRITY'});
2251 if ($#temp < 0) {
2252 $errormessage = $Lang::tr{'invalid input'};
2253 goto ADVANCED_ERROR;
2254 }
2255 foreach my $val (@temp) {
2256 if ($val !~ /^(sha2_(512|384|256)|sha1|md5|aesxcbc)$/) {
2257 $errormessage = $Lang::tr{'invalid input'};
2258 goto ADVANCED_ERROR;
2259 }
2260 }
2261 @temp = split('\|', $cgiparams{'ESP_GROUPTYPE'});
2262 if ($#temp < 0) {
2263 $errormessage = $Lang::tr{'invalid input'};
2264 goto ADVANCED_ERROR;
2265 }
2266 foreach my $val (@temp) {
8c6b02e7 2267 if ($val !~ /^(curve25519|e521|e384|e256|e224|e192|e512bp|e384bp|e256bp|e224bp|768|1024|1536|2048|3072|4096|6144|8192|none)$/) {
624615ee
LS
2268 $errormessage = $Lang::tr{'invalid input'};
2269 goto ADVANCED_ERROR;
2270 }
2271 }
2272 if ($cgiparams{'ESP_KEYLIFE'} !~ /^\d+$/) {
2273 $errormessage = $Lang::tr{'invalid input for esp keylife'};
2274 goto ADVANCED_ERROR;
2275 }
2276 if ($cgiparams{'ESP_KEYLIFE'} < 1 || $cgiparams{'ESP_KEYLIFE'} > 24) {
2277 $errormessage = $Lang::tr{'esp keylife should be between 1 and 24 hours'};
2278 goto ADVANCED_ERROR;
2279 }
2280
2281 if (($cgiparams{'COMPRESSION'} !~ /^(|on|off)$/) ||
2282 ($cgiparams{'FORCE_MOBIKE'} !~ /^(|on|off)$/) ||
2283 ($cgiparams{'ONLY_PROPOSED'} !~ /^(|on|off)$/) ||
2284 ($cgiparams{'PFS'} !~ /^(|on|off)$/)) {
2285 $errormessage = $Lang::tr{'invalid input'};
2286 goto ADVANCED_ERROR;
2287 }
2288
2289 if ($cgiparams{'DPD_DELAY'} !~ /^\d+$/) {
2290 $errormessage = $Lang::tr{'invalid input for dpd delay'};
2291 goto ADVANCED_ERROR;
2292 }
2293
2294 if ($cgiparams{'DPD_TIMEOUT'} !~ /^\d+$/) {
2295 $errormessage = $Lang::tr{'invalid input for dpd timeout'};
2296 goto ADVANCED_ERROR;
2297 }
2298
af183eeb
MT
2299 if ($cgiparams{'INACTIVITY_TIMEOUT'} !~ /^\d+$/) {
2300 $errormessage = $Lang::tr{'invalid input for inactivity timeout'};
2301 goto ADVANCED_ERROR;
2302 }
2303
29f5e0e2
MT
2304 if ($cgiparams{'MODE'} !~ /^(tunnel|transport)$/) {
2305 $errormessage = $Lang::tr{'invalid input for mode'};
2306 goto ADVANCED_ERROR;
2307 }
2308
cae1f4a7
MT
2309 if ($cgiparams{'INTERFACE_MODE'} !~ /^(|gre|vti)$/) {
2310 $errormessage = $Lang::tr{'invalid input for interface mode'};
2311 goto ADVANCED_ERROR;
2312 }
2313
624615ee
LS
2314 $confighash{$cgiparams{'KEY'}}[29] = $cgiparams{'IKE_VERSION'};
2315 $confighash{$cgiparams{'KEY'}}[18] = $cgiparams{'IKE_ENCRYPTION'};
2316 $confighash{$cgiparams{'KEY'}}[19] = $cgiparams{'IKE_INTEGRITY'};
2317 $confighash{$cgiparams{'KEY'}}[20] = $cgiparams{'IKE_GROUPTYPE'};
2318 $confighash{$cgiparams{'KEY'}}[16] = $cgiparams{'IKE_LIFETIME'};
2319 $confighash{$cgiparams{'KEY'}}[21] = $cgiparams{'ESP_ENCRYPTION'};
2320 $confighash{$cgiparams{'KEY'}}[22] = $cgiparams{'ESP_INTEGRITY'};
2321 $confighash{$cgiparams{'KEY'}}[23] = $cgiparams{'ESP_GROUPTYPE'};
2322 $confighash{$cgiparams{'KEY'}}[17] = $cgiparams{'ESP_KEYLIFE'};
2323 $confighash{$cgiparams{'KEY'}}[12] = 'off'; #$cgiparams{'AGGRMODE'};
2324 $confighash{$cgiparams{'KEY'}}[13] = $cgiparams{'COMPRESSION'};
2325 $confighash{$cgiparams{'KEY'}}[24] = $cgiparams{'ONLY_PROPOSED'};
2326 $confighash{$cgiparams{'KEY'}}[28] = $cgiparams{'PFS'};
2327 $confighash{$cgiparams{'KEY'}}[27] = $cgiparams{'DPD_ACTION'};
2328 $confighash{$cgiparams{'KEY'}}[30] = $cgiparams{'DPD_TIMEOUT'};
2329 $confighash{$cgiparams{'KEY'}}[31] = $cgiparams{'DPD_DELAY'};
2330 $confighash{$cgiparams{'KEY'}}[32] = $cgiparams{'FORCE_MOBIKE'};
dcb406cc 2331 $confighash{$cgiparams{'KEY'}}[33] = $cgiparams{'START_ACTION'};
af183eeb 2332 $confighash{$cgiparams{'KEY'}}[34] = $cgiparams{'INACTIVITY_TIMEOUT'};
29f5e0e2 2333 $confighash{$cgiparams{'KEY'}}[35] = $cgiparams{'MODE'};
cae1f4a7 2334 $confighash{$cgiparams{'KEY'}}[36] = $cgiparams{'INTERFACE_MODE'};
624615ee
LS
2335 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
2336 &writeipsecfiles();
2337 if (&vpnenabled) {
2338 system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'});
2339 sleep $sleepDelay;
2340 }
2341 goto ADVANCED_END;
2342 } else {
2343 $cgiparams{'IKE_VERSION'} = $confighash{$cgiparams{'KEY'}}[29];
2344 $cgiparams{'IKE_ENCRYPTION'} = $confighash{$cgiparams{'KEY'}}[18];
2345 $cgiparams{'IKE_INTEGRITY'} = $confighash{$cgiparams{'KEY'}}[19];
2346 $cgiparams{'IKE_GROUPTYPE'} = $confighash{$cgiparams{'KEY'}}[20];
2347 $cgiparams{'IKE_LIFETIME'} = $confighash{$cgiparams{'KEY'}}[16];
2348 $cgiparams{'ESP_ENCRYPTION'} = $confighash{$cgiparams{'KEY'}}[21];
2349 $cgiparams{'ESP_INTEGRITY'} = $confighash{$cgiparams{'KEY'}}[22];
2350 $cgiparams{'ESP_GROUPTYPE'} = $confighash{$cgiparams{'KEY'}}[23];
2351 if ($cgiparams{'ESP_GROUPTYPE'} eq "") {
2352 $cgiparams{'ESP_GROUPTYPE'} = $cgiparams{'IKE_GROUPTYPE'};
2353 }
2354 $cgiparams{'ESP_KEYLIFE'} = $confighash{$cgiparams{'KEY'}}[17];
2355 $cgiparams{'COMPRESSION'} = $confighash{$cgiparams{'KEY'}}[13];
2356 $cgiparams{'ONLY_PROPOSED'} = $confighash{$cgiparams{'KEY'}}[24];
2357 $cgiparams{'PFS'} = $confighash{$cgiparams{'KEY'}}[28];
2358 $cgiparams{'DPD_ACTION'} = $confighash{$cgiparams{'KEY'}}[27];
2359 $cgiparams{'DPD_TIMEOUT'} = $confighash{$cgiparams{'KEY'}}[30];
2360 $cgiparams{'DPD_DELAY'} = $confighash{$cgiparams{'KEY'}}[31];
2361 $cgiparams{'FORCE_MOBIKE'} = $confighash{$cgiparams{'KEY'}}[32];
dcb406cc 2362 $cgiparams{'START_ACTION'} = $confighash{$cgiparams{'KEY'}}[33];
af183eeb 2363 $cgiparams{'INACTIVITY_TIMEOUT'} = $confighash{$cgiparams{'KEY'}}[34];
29f5e0e2 2364 $cgiparams{'MODE'} = $confighash{$cgiparams{'KEY'}}[35];
cae1f4a7 2365 $cgiparams{'INTERFACE_MODE'} = $confighash{$cgiparams{'KEY'}}[36];
624615ee
LS
2366
2367 if (!$cgiparams{'DPD_DELAY'}) {
2368 $cgiparams{'DPD_DELAY'} = 30;
2369 }
2370
2371 if (!$cgiparams{'DPD_TIMEOUT'}) {
2372 $cgiparams{'DPD_TIMEOUT'} = 120;
2373 }
dcb406cc
MT
2374
2375 if (!$cgiparams{'START_ACTION'}) {
2376 $cgiparams{'START_ACTION'} = "start";
2377 }
af183eeb
MT
2378
2379 if ($cgiparams{'INACTIVITY_TIMEOUT'} eq "") {
2380 $cgiparams{'INACTIVITY_TIMEOUT'} = 900; # 15 min
2381 }
29f5e0e2
MT
2382
2383 if ($cgiparams{'MODE'} eq "") {
2384 $cgiparams{'MODE'} = "tunnel";
2385 }
ac1cfefa 2386 }
624615ee
LS
2387
2388 ADVANCED_ERROR:
05375f12 2389 $checked{'IKE_ENCRYPTION'}{'chacha20poly1305'} = '';
624615ee
LS
2390 $checked{'IKE_ENCRYPTION'}{'aes256'} = '';
2391 $checked{'IKE_ENCRYPTION'}{'aes192'} = '';
2392 $checked{'IKE_ENCRYPTION'}{'aes128'} = '';
2393 $checked{'IKE_ENCRYPTION'}{'aes256gcm128'} = '';
2394 $checked{'IKE_ENCRYPTION'}{'aes192gcm128'} = '';
2395 $checked{'IKE_ENCRYPTION'}{'aes128gcm128'} = '';
2396 $checked{'IKE_ENCRYPTION'}{'aes256gcm96'} = '';
2397 $checked{'IKE_ENCRYPTION'}{'aes192gcm96'} = '';
2398 $checked{'IKE_ENCRYPTION'}{'aes128gcm96'} = '';
2399 $checked{'IKE_ENCRYPTION'}{'aes256gcm64'} = '';
2400 $checked{'IKE_ENCRYPTION'}{'aes192gcm64'} = '';
2401 $checked{'IKE_ENCRYPTION'}{'aes128gcm64'} = '';
2402 $checked{'IKE_ENCRYPTION'}{'3des'} = '';
2403 $checked{'IKE_ENCRYPTION'}{'camellia256'} = '';
2404 $checked{'IKE_ENCRYPTION'}{'camellia192'} = '';
2405 $checked{'IKE_ENCRYPTION'}{'camellia128'} = '';
2406 my @temp = split('\|', $cgiparams{'IKE_ENCRYPTION'});
2407 foreach my $key (@temp) {$checked{'IKE_ENCRYPTION'}{$key} = "selected='selected'"; }
2408 $checked{'IKE_INTEGRITY'}{'sha2_512'} = '';
2409 $checked{'IKE_INTEGRITY'}{'sha2_384'} = '';
2410 $checked{'IKE_INTEGRITY'}{'sha2_256'} = '';
2411 $checked{'IKE_INTEGRITY'}{'sha'} = '';
2412 $checked{'IKE_INTEGRITY'}{'md5'} = '';
2413 $checked{'IKE_INTEGRITY'}{'aesxcbc'} = '';
ac1cfefa 2414 @temp = split('\|', $cgiparams{'IKE_INTEGRITY'});
624615ee 2415 foreach my $key (@temp) {$checked{'IKE_INTEGRITY'}{$key} = "selected='selected'"; }
64056cae 2416 $checked{'IKE_GROUPTYPE'}{'curve25519'} = '';
624615ee
LS
2417 $checked{'IKE_GROUPTYPE'}{'768'} = '';
2418 $checked{'IKE_GROUPTYPE'}{'1024'} = '';
2419 $checked{'IKE_GROUPTYPE'}{'1536'} = '';
2420 $checked{'IKE_GROUPTYPE'}{'2048'} = '';
2421 $checked{'IKE_GROUPTYPE'}{'3072'} = '';
2422 $checked{'IKE_GROUPTYPE'}{'4096'} = '';
2423 $checked{'IKE_GROUPTYPE'}{'6144'} = '';
2424 $checked{'IKE_GROUPTYPE'}{'8192'} = '';
ac1cfefa 2425 @temp = split('\|', $cgiparams{'IKE_GROUPTYPE'});
624615ee
LS
2426 foreach my $key (@temp) {$checked{'IKE_GROUPTYPE'}{$key} = "selected='selected'"; }
2427
05375f12 2428 $checked{'ESP_ENCRYPTION'}{'chacha20poly1305'} = '';
624615ee
LS
2429 $checked{'ESP_ENCRYPTION'}{'aes256'} = '';
2430 $checked{'ESP_ENCRYPTION'}{'aes192'} = '';
2431 $checked{'ESP_ENCRYPTION'}{'aes128'} = '';
2432 $checked{'ESP_ENCRYPTION'}{'aes256gcm128'} = '';
2433 $checked{'ESP_ENCRYPTION'}{'aes192gcm128'} = '';
2434 $checked{'ESP_ENCRYPTION'}{'aes128gcm128'} = '';
2435 $checked{'ESP_ENCRYPTION'}{'aes256gcm96'} = '';
2436 $checked{'ESP_ENCRYPTION'}{'aes192gcm96'} = '';
2437 $checked{'ESP_ENCRYPTION'}{'aes128gcm96'} = '';
2438 $checked{'ESP_ENCRYPTION'}{'aes256gcm64'} = '';
2439 $checked{'ESP_ENCRYPTION'}{'aes192gcm64'} = '';
2440 $checked{'ESP_ENCRYPTION'}{'aes128gcm64'} = '';
2441 $checked{'ESP_ENCRYPTION'}{'3des'} = '';
2442 $checked{'ESP_ENCRYPTION'}{'camellia256'} = '';
2443 $checked{'ESP_ENCRYPTION'}{'camellia192'} = '';
2444 $checked{'ESP_ENCRYPTION'}{'camellia128'} = '';
ac1cfefa 2445 @temp = split('\|', $cgiparams{'ESP_ENCRYPTION'});
624615ee
LS
2446 foreach my $key (@temp) {$checked{'ESP_ENCRYPTION'}{$key} = "selected='selected'"; }
2447 $checked{'ESP_INTEGRITY'}{'sha2_512'} = '';
2448 $checked{'ESP_INTEGRITY'}{'sha2_384'} = '';
2449 $checked{'ESP_INTEGRITY'}{'sha2_256'} = '';
2450 $checked{'ESP_INTEGRITY'}{'sha1'} = '';
2451 $checked{'ESP_INTEGRITY'}{'md5'} = '';
2452 $checked{'ESP_INTEGRITY'}{'aesxcbc'} = '';
ac1cfefa 2453 @temp = split('\|', $cgiparams{'ESP_INTEGRITY'});
624615ee 2454 foreach my $key (@temp) {$checked{'ESP_INTEGRITY'}{$key} = "selected='selected'"; }
64056cae 2455 $checked{'ESP_GROUPTYPE'}{'curve25519'} = '';
624615ee
LS
2456 $checked{'ESP_GROUPTYPE'}{'768'} = '';
2457 $checked{'ESP_GROUPTYPE'}{'1024'} = '';
2458 $checked{'ESP_GROUPTYPE'}{'1536'} = '';
2459 $checked{'ESP_GROUPTYPE'}{'2048'} = '';
2460 $checked{'ESP_GROUPTYPE'}{'3072'} = '';
2461 $checked{'ESP_GROUPTYPE'}{'4096'} = '';
2462 $checked{'ESP_GROUPTYPE'}{'6144'} = '';
2463 $checked{'ESP_GROUPTYPE'}{'8192'} = '';
2464 $checked{'ESP_GROUPTYPE'}{'none'} = '';
4b02b404 2465 @temp = split('\|', $cgiparams{'ESP_GROUPTYPE'});
624615ee 2466 foreach my $key (@temp) {$checked{'ESP_GROUPTYPE'}{$key} = "selected='selected'"; }
ed84e8b8 2467
624615ee
LS
2468 $checked{'COMPRESSION'} = $cgiparams{'COMPRESSION'} eq 'on' ? "checked='checked'" : '' ;
2469 $checked{'FORCE_MOBIKE'} = $cgiparams{'FORCE_MOBIKE'} eq 'on' ? "checked='checked'" : '' ;
2470 $checked{'ONLY_PROPOSED'} = $cgiparams{'ONLY_PROPOSED'} eq 'on' ? "checked='checked'" : '' ;
2471 $checked{'PFS'} = $cgiparams{'PFS'} eq 'on' ? "checked='checked'" : '' ;
cbb3a8f9 2472
624615ee
LS
2473 $selected{'IKE_VERSION'}{'ikev1'} = '';
2474 $selected{'IKE_VERSION'}{'ikev2'} = '';
2475 $selected{'IKE_VERSION'}{$cgiparams{'IKE_VERSION'}} = "selected='selected'";
cbb3a8f9 2476
624615ee
LS
2477 $selected{'DPD_ACTION'}{'clear'} = '';
2478 $selected{'DPD_ACTION'}{'hold'} = '';
2479 $selected{'DPD_ACTION'}{'restart'} = '';
2480 $selected{'DPD_ACTION'}{'none'} = '';
2481 $selected{'DPD_ACTION'}{$cgiparams{'DPD_ACTION'}} = "selected='selected'";
ac1cfefa 2482
237f3ab7 2483 $selected{'START_ACTION'}{'add'} = '';
dcb406cc
MT
2484 $selected{'START_ACTION'}{'route'} = '';
2485 $selected{'START_ACTION'}{'start'} = '';
2486 $selected{'START_ACTION'}{$cgiparams{'START_ACTION'}} = "selected='selected'";
2487
af183eeb
MT
2488 $selected{'INACTIVITY_TIMEOUT'} = ();
2489 foreach my $timeout (keys %INACTIVITY_TIMEOUTS) {
2490 $selected{'INACTIVITY_TIMEOUT'}{$timeout} = "";
2491 }
2492 $selected{'INACTIVITY_TIMEOUT'}{$cgiparams{'INACTIVITY_TIMEOUT'}} = "selected";
2493
29f5e0e2
MT
2494 $selected{'MODE'}{'tunnel'} = '';
2495 $selected{'MODE'}{'transport'} = '';
2496 $selected{'MODE'}{$cgiparams{'MODE'}} = "selected='selected'";
2497
cae1f4a7
MT
2498 $selected{'INTERFACE_MODE'}{''} = '';
2499 $selected{'INTERFACE_MODE'}{'gre'} = '';
2500 $selected{'INTERFACE_MODE'}{'vti'} = '';
2501 $selected{'INTERFACE_MODE'}{$cgiparams{'INTERFACE_MODE'}} = "selected='selected'";
2502
624615ee
LS
2503 &Header::showhttpheaders();
2504 &Header::openpage($Lang::tr{'ipsec'}, 1, '');
2505 &Header::openbigbox('100%', 'left', '', $errormessage);
2506
2507 if ($errormessage) {
2508 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
2509 print "<class name='base'>$errormessage";
2510 print "&nbsp;</class>";
2511 &Header::closebox();
2512 }
2513
2514 if ($warnmessage) {
2515 &Header::openbox('100%', 'left', $Lang::tr{'warning messages'});
2516 print "<class name='base'>$warnmessage";
2517 print "&nbsp;</class>";
2518 &Header::closebox();
2519 }
ac1cfefa 2520
624615ee 2521 &Header::openbox('100%', 'left', "$Lang::tr{'advanced'}:");
dcb406cc 2522 print <<EOF;
624615ee
LS
2523 <form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'>
2524 <input type='hidden' name='ADVANCED' value='yes' />
2525 <input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />
ac1cfefa 2526
29f5e0e2
MT
2527 <table width="100%">
2528 <tbody>
2529 <tr>
2530 <td width="15%">$Lang::tr{'mode'}:</td>
2531 <td>
2532 <select name='MODE'>
2533 <option value='tunnel' $selected{'MODE'}{'tunnel'}>$Lang::tr{'ipsec mode tunnel'}</option>
2534 <option value='transport' $selected{'MODE'}{'transport'}>$Lang::tr{'ipsec mode transport'}</option>
2535 </select>
2536 </td>
2537 </tr>
cae1f4a7
MT
2538
2539 <tr>
2540 <td width="15%">$Lang::tr{'interface mode'}:</td>
2541 <td>
2542 <select name='INTERFACE_MODE'>
2543 <option value='' $selected{'INTERFACE_MODE'}{''}>$Lang::tr{'ipsec interface mode none'}</option>
2544 <option value='gre' $selected{'INTERFACE_MODE'}{'gre'}>$Lang::tr{'ipsec interface mode gre'}</option>
2545 <option value='vti' $selected{'INTERFACE_MODE'}{'vti'}>$Lang::tr{'ipsec interface mode vti'}</option>
2546 </select>
2547 </td>
2548 </tr>
29f5e0e2
MT
2549 </tbody>
2550 </table>
2551
2552 <br><br>
2553
2554 <h2>$Lang::tr{'cryptographic settings'}</h2>
2555
624615ee 2556 <table width='100%'>
63e3da59
MT
2557 <thead>
2558 <tr>
cbb3a8f9 2559 <th width="15%"></th>
63e3da59
MT
2560 <th>IKE</th>
2561 <th>ESP</th>
2562 </tr>
2563 </thead>
2564 <tbody>
4ad0b5b6
MT
2565 <tr>
2566 <td>$Lang::tr{'vpn keyexchange'}:</td>
2567 <td>
2568 <select name='IKE_VERSION'>
2569 <option value='ikev2' $selected{'IKE_VERSION'}{'ikev2'}>IKEv2</option>
2570 <option value='ikev1' $selected{'IKE_VERSION'}{'ikev1'}>IKEv1</option>
2571 </select>
2572 </td>
2573 <td></td>
2574 </tr>
63e3da59 2575 <tr>
cbb3a8f9 2576 <td class='boldbase' width="15%">$Lang::tr{'encryption'}</td>
63e3da59
MT
2577 <td class='boldbase'>
2578 <select name='IKE_ENCRYPTION' multiple='multiple' size='6' style='width: 100%'>
05375f12 2579 <option value='chacha20poly1305' $checked{'IKE_ENCRYPTION'}{'chacha20poly1305'}>256 bit ChaCha20-Poly1305/128 bit ICV</option>
dfea4f86 2580 <option value='aes256gcm128' $checked{'IKE_ENCRYPTION'}{'aes256gcm128'}>256 bit AES-GCM/128 bit ICV</option>
dfea4f86 2581 <option value='aes256gcm96' $checked{'IKE_ENCRYPTION'}{'aes256gcm96'}>256 bit AES-GCM/96 bit ICV</option>
dfea4f86 2582 <option value='aes256gcm64' $checked{'IKE_ENCRYPTION'}{'aes256gcm64'}>256 bit AES-GCM/64 bit ICV</option>
a4d24f90 2583 <option value='aes256' $checked{'IKE_ENCRYPTION'}{'aes256'}>256 bit AES-CBC</option>
dfea4f86 2584 <option value='camellia256' $checked{'IKE_ENCRYPTION'}{'camellia256'}>256 bit Camellia-CBC</option>
a4d24f90
MT
2585 <option value='aes192gcm128' $checked{'IKE_ENCRYPTION'}{'aes192gcm128'}>192 bit AES-GCM/128 bit ICV</option>
2586 <option value='aes192gcm96' $checked{'IKE_ENCRYPTION'}{'aes192gcm96'}>192 bit AES-GCM/96 bit ICV</option>
2587 <option value='aes192gcm64' $checked{'IKE_ENCRYPTION'}{'aes192gcm64'}>192 bit AES-GCM/64 bit ICV</option>
2588 <option value='aes192' $checked{'IKE_ENCRYPTION'}{'aes192'}>192 bit AES-CBC</option>
dfea4f86 2589 <option value='camellia192' $checked{'IKE_ENCRYPTION'}{'camellia192'}>192 bit Camellia-CBC</option>
a4d24f90
MT
2590 <option value='aes128gcm128' $checked{'IKE_ENCRYPTION'}{'aes128gcm128'}>128 bit AES-GCM/128 bit ICV</option>
2591 <option value='aes128gcm96' $checked{'IKE_ENCRYPTION'}{'aes128gcm96'}>128 bit AES-GCM/96 bit ICV</option>
2592 <option value='aes128gcm64' $checked{'IKE_ENCRYPTION'}{'aes128gcm64'}>128 bit AES-GCM/64 bit ICV</option>
2593 <option value='aes128' $checked{'IKE_ENCRYPTION'}{'aes128'}>128 bit AES-CBC</option>
dfea4f86 2594 <option value='camellia128' $checked{'IKE_ENCRYPTION'}{'camellia128'}>128 bit Camellia-CBC</option>
6fc0f5eb 2595 <option value='3des' $checked{'IKE_ENCRYPTION'}{'3des'}>168 bit 3DES-EDE-CBC ($Lang::tr{'vpn weak'})</option>
63e3da59
MT
2596 </select>
2597 </td>
2598 <td class='boldbase'>
2599 <select name='ESP_ENCRYPTION' multiple='multiple' size='6' style='width: 100%'>
05375f12 2600 <option value='chacha20poly1305' $checked{'ESP_ENCRYPTION'}{'chacha20poly1305'}>256 bit ChaCha20-Poly1305/128 bit ICV</option>
dfea4f86 2601 <option value='aes256gcm128' $checked{'ESP_ENCRYPTION'}{'aes256gcm128'}>256 bit AES-GCM/128 bit ICV</option>
dfea4f86 2602 <option value='aes256gcm96' $checked{'ESP_ENCRYPTION'}{'aes256gcm96'}>256 bit AES-GCM/96 bit ICV</option>
dfea4f86 2603 <option value='aes256gcm64' $checked{'ESP_ENCRYPTION'}{'aes256gcm64'}>256 bit AES-GCM/64 bit ICV</option>
a4d24f90 2604 <option value='aes256' $checked{'ESP_ENCRYPTION'}{'aes256'}>256 bit AES-CBC</option>
dfea4f86 2605 <option value='camellia256' $checked{'ESP_ENCRYPTION'}{'camellia256'}>256 bit Camellia-CBC</option>
a4d24f90
MT
2606 <option value='aes192gcm128' $checked{'ESP_ENCRYPTION'}{'aes192gcm128'}>192 bit AES-GCM/128 bit ICV</option>
2607 <option value='aes192gcm96' $checked{'ESP_ENCRYPTION'}{'aes192gcm96'}>192 bit AES-GCM/96 bit ICV</option>
2608 <option value='aes192gcm64' $checked{'ESP_ENCRYPTION'}{'aes192gcm64'}>192 bit AES-GCM/64 bit ICV</option>
2609 <option value='aes192' $checked{'ESP_ENCRYPTION'}{'aes192'}>192 bit AES-CBC</option>
dfea4f86 2610 <option value='camellia192' $checked{'ESP_ENCRYPTION'}{'camellia192'}>192 bit Camellia-CBC</option>
a4d24f90
MT
2611 <option value='aes128gcm128' $checked{'ESP_ENCRYPTION'}{'aes128gcm128'}>128 bit AES-GCM/128 bit ICV</option>
2612 <option value='aes128gcm96' $checked{'ESP_ENCRYPTION'}{'aes128gcm96'}>128 bit AES-GCM/96 bit ICV</option>
2613 <option value='aes128gcm64' $checked{'ESP_ENCRYPTION'}{'aes128gcm64'}>128 bit AES-GCM/64 bit ICV</option>
2614 <option value='aes128' $checked{'ESP_ENCRYPTION'}{'aes128'}>128 bit AES-CBC</option>
dfea4f86 2615 <option value='camellia128' $checked{'ESP_ENCRYPTION'}{'camellia128'}>128 bit Camellia-CBC</option>
6fc0f5eb 2616 <option value='3des' $checked{'ESP_ENCRYPTION'}{'3des'}>168 bit 3DES-EDE-CBC ($Lang::tr{'vpn weak'})</option>
63e3da59
MT
2617 </select>
2618 </td>
2619 </tr>
ed84e8b8 2620
63e3da59 2621 <tr>
cbb3a8f9 2622 <td class='boldbase' width="15%">$Lang::tr{'integrity'}</td>
63e3da59
MT
2623 <td class='boldbase'>
2624 <select name='IKE_INTEGRITY' multiple='multiple' size='6' style='width: 100%'>
2625 <option value='sha2_512' $checked{'IKE_INTEGRITY'}{'sha2_512'}>SHA2 512 bit</option>
2626 <option value='sha2_384' $checked{'IKE_INTEGRITY'}{'sha2_384'}>SHA2 384 bit</option>
2627 <option value='sha2_256' $checked{'IKE_INTEGRITY'}{'sha2_256'}>SHA2 256 bit</option>
a4d24f90 2628 <option value='aesxcbc' $checked{'IKE_INTEGRITY'}{'aesxcbc'}>AES XCBC</option>
c94d1976 2629 <option value='sha' $checked{'IKE_INTEGRITY'}{'sha'}>SHA1 ($Lang::tr{'vpn weak'})</option>
86282bdc 2630 <option value='md5' $checked{'IKE_INTEGRITY'}{'md5'}>MD5 ($Lang::tr{'vpn broken'})</option>
63e3da59
MT
2631 </select>
2632 </td>
2633 <td class='boldbase'>
2634 <select name='ESP_INTEGRITY' multiple='multiple' size='6' style='width: 100%'>
2635 <option value='sha2_512' $checked{'ESP_INTEGRITY'}{'sha2_512'}>SHA2 512 bit</option>
2636 <option value='sha2_384' $checked{'ESP_INTEGRITY'}{'sha2_384'}>SHA2 384 bit</option>
2637 <option value='sha2_256' $checked{'ESP_INTEGRITY'}{'sha2_256'}>SHA2 256 bit</option>
a4d24f90 2638 <option value='aesxcbc' $checked{'ESP_INTEGRITY'}{'aesxcbc'}>AES XCBC</option>
c94d1976
MT
2639 <option value='sha1' $checked{'ESP_INTEGRITY'}{'sha1'}>SHA1 ($Lang::tr{'vpn weak'})</option>
2640 <option value='md5' $checked{'ESP_INTEGRITY'}{'md5'}>MD5 ($Lang::tr{'vpn broken'})</option>
63e3da59
MT
2641 </select>
2642 </td>
2643 </tr>
2644 <tr>
e3edceeb 2645 <td class='boldbase' width="15%">$Lang::tr{'lifetime'}&nbsp;<img src='/blob.gif' alt='*' /></td>
63e3da59
MT
2646 <td class='boldbase'>
2647 <input type='text' name='IKE_LIFETIME' value='$cgiparams{'IKE_LIFETIME'}' size='5' /> $Lang::tr{'hours'}
2648 </td>
2649 <td class='boldbase'>
2650 <input type='text' name='ESP_KEYLIFE' value='$cgiparams{'ESP_KEYLIFE'}' size='5' /> $Lang::tr{'hours'}
2651 </td>
2652 </tr>
2653 <tr>
cbb3a8f9 2654 <td class='boldbase' width="15%">$Lang::tr{'grouptype'}</td>
63e3da59
MT
2655 <td class='boldbase'>
2656 <select name='IKE_GROUPTYPE' multiple='multiple' size='6' style='width: 100%'>
64056cae 2657 <option value='curve25519' $checked{'IKE_GROUPTYPE'}{'curve25519'}>Curve 25519 (256 bit)</option>
63e3da59 2658 <option value='e521' $checked{'IKE_GROUPTYPE'}{'e521'}>ECP-521 (NIST)</option>
63e3da59 2659 <option value='e512bp' $checked{'IKE_GROUPTYPE'}{'e512bp'}>ECP-512 (Brainpool)</option>
a4d24f90 2660 <option value='e384' $checked{'IKE_GROUPTYPE'}{'e384'}>ECP-384 (NIST)</option>
63e3da59 2661 <option value='e384bp' $checked{'IKE_GROUPTYPE'}{'e384bp'}>ECP-384 (Brainpool)</option>
a4d24f90 2662 <option value='e256' $checked{'IKE_GROUPTYPE'}{'e256'}>ECP-256 (NIST)</option>
63e3da59 2663 <option value='e256bp' $checked{'IKE_GROUPTYPE'}{'e256bp'}>ECP-256 (Brainpool)</option>
a4d24f90 2664 <option value='e224' $checked{'IKE_GROUPTYPE'}{'e224'}>ECP-224 (NIST)</option>
63e3da59 2665 <option value='e224bp' $checked{'IKE_GROUPTYPE'}{'e224bp'}>ECP-224 (Brainpool)</option>
a4d24f90 2666 <option value='e192' $checked{'IKE_GROUPTYPE'}{'e192'}>ECP-192 (NIST)</option>
63e3da59
MT
2667 <option value='8192' $checked{'IKE_GROUPTYPE'}{'8192'}>MODP-8192</option>
2668 <option value='6144' $checked{'IKE_GROUPTYPE'}{'6144'}>MODP-6144</option>
2669 <option value='4096' $checked{'IKE_GROUPTYPE'}{'4096'}>MODP-4096</option>
2670 <option value='3072' $checked{'IKE_GROUPTYPE'}{'3072'}>MODP-3072</option>
63e3da59
MT
2671 <option value='2048' $checked{'IKE_GROUPTYPE'}{'2048'}>MODP-2048</option>
2672 <option value='1536' $checked{'IKE_GROUPTYPE'}{'1536'}>MODP-1536</option>
c94d1976
MT
2673 <option value='1024' $checked{'IKE_GROUPTYPE'}{'1024'}>MODP-1024 ($Lang::tr{'vpn broken'})</option>
2674 <option value='768' $checked{'IKE_GROUPTYPE'}{'768'}>MODP-768 ($Lang::tr{'vpn broken'})</option>
63e3da59
MT
2675 </select>
2676 </td>
4b02b404
MT
2677 <td class='boldbase'>
2678 <select name='ESP_GROUPTYPE' multiple='multiple' size='6' style='width: 100%'>
64056cae 2679 <option value='curve25519' $checked{'ESP_GROUPTYPE'}{'curve25519'}>Curve 25519 (256 bit)</option>
4b02b404
MT
2680 <option value='e521' $checked{'ESP_GROUPTYPE'}{'e521'}>ECP-521 (NIST)</option>
2681 <option value='e512bp' $checked{'ESP_GROUPTYPE'}{'e512bp'}>ECP-512 (Brainpool)</option>
2682 <option value='e384' $checked{'ESP_GROUPTYPE'}{'e384'}>ECP-384 (NIST)</option>
2683 <option value='e384bp' $checked{'ESP_GROUPTYPE'}{'e384bp'}>ECP-384 (Brainpool)</option>
2684 <option value='e256' $checked{'ESP_GROUPTYPE'}{'e256'}>ECP-256 (NIST)</option>
2685 <option value='e256bp' $checked{'ESP_GROUPTYPE'}{'e256bp'}>ECP-256 (Brainpool)</option>
2686 <option value='e224' $checked{'ESP_GROUPTYPE'}{'e224'}>ECP-224 (NIST)</option>
2687 <option value='e224bp' $checked{'ESP_GROUPTYPE'}{'e224bp'}>ECP-224 (Brainpool)</option>
2688 <option value='e192' $checked{'ESP_GROUPTYPE'}{'e192'}>ECP-192 (NIST)</option>
2689 <option value='8192' $checked{'ESP_GROUPTYPE'}{'8192'}>MODP-8192</option>
2690 <option value='6144' $checked{'ESP_GROUPTYPE'}{'6144'}>MODP-6144</option>
2691 <option value='4096' $checked{'ESP_GROUPTYPE'}{'4096'}>MODP-4096</option>
2692 <option value='3072' $checked{'ESP_GROUPTYPE'}{'3072'}>MODP-3072</option>
4b02b404
MT
2693 <option value='2048' $checked{'ESP_GROUPTYPE'}{'2048'}>MODP-2048</option>
2694 <option value='1536' $checked{'ESP_GROUPTYPE'}{'1536'}>MODP-1536</option>
c94d1976
MT
2695 <option value='1024' $checked{'ESP_GROUPTYPE'}{'1024'}>MODP-1024 ($Lang::tr{'vpn broken'})</option>
2696 <option value='768' $checked{'ESP_GROUPTYPE'}{'768'}>MODP-768 ($Lang::tr{'vpn broken'})</option>
4b02b404
MT
2697 <option value='none' $checked{'ESP_GROUPTYPE'}{'none'}>- $Lang::tr{'none'} -</option>
2698 </select>
2699 </td>
63e3da59
MT
2700 </tr>
2701 </tbody>
624615ee 2702 </table>
63e3da59 2703
cbb3a8f9
MT
2704 <br><br>
2705
2706 <h2>$Lang::tr{'dead peer detection'}</h2>
2707
624615ee
LS
2708 <table width="100%">
2709 <tr>
cbb3a8f9
MT
2710 <td width="15%">$Lang::tr{'dpd action'}:</td>
2711 <td>
2712 <select name='DPD_ACTION'>
afd5d8f7 2713 <option value='none' $selected{'DPD_ACTION'}{'none'}>- $Lang::tr{'disabled'} -</option>
cbb3a8f9
MT
2714 <option value='clear' $selected{'DPD_ACTION'}{'clear'}>clear</option>
2715 <option value='hold' $selected{'DPD_ACTION'}{'hold'}>hold</option>
2716 <option value='restart' $selected{'DPD_ACTION'}{'restart'}>restart</option>
2717 </select>
2718 </td>
2719 </tr>
2720 <tr>
e3edceeb 2721 <td width="15%">$Lang::tr{'dpd timeout'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
cbb3a8f9
MT
2722 <td>
2723 <input type='text' name='DPD_TIMEOUT' size='5' value='$cgiparams{'DPD_TIMEOUT'}' />
2724 </td>
2725 </tr>
2726 <tr>
e3edceeb 2727 <td width="15%">$Lang::tr{'dpd delay'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
cbb3a8f9
MT
2728 <td>
2729 <input type='text' name='DPD_DELAY' size='5' value='$cgiparams{'DPD_DELAY'}' />
2730 </td>
2731 </tr>
624615ee 2732 </table>
cbb3a8f9 2733
624615ee 2734 <hr>
63e3da59 2735
624615ee 2736 <table width="100%">
63e3da59 2737 <tr>
cbb3a8f9 2738 <td>
63e3da59
MT
2739 <label>
2740 <input type='checkbox' name='ONLY_PROPOSED' $checked{'ONLY_PROPOSED'} />
cbb88df1 2741 IKE+ESP: $Lang::tr{'use only proposed settings'}
63e3da59
MT
2742 </label>
2743 </td>
dcb406cc
MT
2744 <td>
2745 <label>$Lang::tr{'vpn start action'}</label>
2746 <select name="START_ACTION">
2747 <option value="route" $selected{'START_ACTION'}{'route'}>$Lang::tr{'vpn start action route'}</option>
2748 <option value="start" $selected{'START_ACTION'}{'start'}>$Lang::tr{'vpn start action start'}</option>
237f3ab7 2749 <option value="add" $selected{'START_ACTION'}{'add'} >$Lang::tr{'vpn start action add'}</option>
dcb406cc
MT
2750 </select>
2751 </td>
63e3da59
MT
2752 </tr>
2753 <tr>
af183eeb 2754 <td>
63e3da59
MT
2755 <label>
2756 <input type='checkbox' name='PFS' $checked{'PFS'} />
2757 $Lang::tr{'pfs yes no'}
2758 </label>
2759 </td>
af183eeb
MT
2760 <td>
2761 <label>$Lang::tr{'vpn inactivity timeout'}</label>
2762 <select name="INACTIVITY_TIMEOUT">
2763EOF
2764 foreach my $t (sort { $a <=> $b } keys %INACTIVITY_TIMEOUTS) {
2765 print "<option value=\"$t\" $selected{'INACTIVITY_TIMEOUT'}{$t}>$INACTIVITY_TIMEOUTS{$t}</option>\n";
2766 }
2767
2768 print <<EOF;
2769
2770 </select>
2771 </td>
63e3da59
MT
2772 </tr>
2773 <tr>
dcb406cc 2774 <td colspan="2">
63e3da59
MT
2775 <label>
2776 <input type='checkbox' name='COMPRESSION' $checked{'COMPRESSION'} />
2777 $Lang::tr{'vpn payload compression'}
2778 </label>
2779 </td>
ed84e8b8 2780 </tr>
f6529a04 2781 <tr>
dcb406cc 2782 <td colspan="2">
f6529a04
MT
2783 <label>
2784 <input type='checkbox' name='FORCE_MOBIKE' $checked{'FORCE_MOBIKE'} />
2785 $Lang::tr{'vpn force mobike'}
2786 </label>
2787 </td>
2788 </tr>
63e3da59 2789 <tr>
dcb406cc
MT
2790 <td align='left'><img src='/blob.gif' align='top' alt='*' />&nbsp;$Lang::tr{'required field'}</td>
2791 <td align='right'>
63e3da59
MT
2792 <input type='submit' name='ACTION' value='$Lang::tr{'save'}' />
2793 <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' />
2794 </td>
2795 </tr>
624615ee 2796 </table></form>
63e3da59
MT
2797EOF
2798
624615ee
LS
2799 &Header::closebox();
2800 &Header::closebigbox();
2801 &Header::closepage();
2802 exit(0);
ac1cfefa 2803
624615ee 2804 ADVANCED_END:
ac1cfefa
MT
2805}
2806
2807###
2808### Default status page
2809###
624615ee
LS
2810 %cgiparams = ();
2811 %cahash = ();
2812 %confighash = ();
2813 &General::readhash("${General::swroot}/vpn/settings", \%cgiparams);
2814 &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
2815 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
2816 $cgiparams{'CA_NAME'} = '';
2817
2818 my @status = `/usr/local/bin/ipsecctrl I 2>/dev/null`;
2819
2820 # suggest a default name for this side
2821 if ($cgiparams{'VPN_IP'} eq '' && -e "${General::swroot}/red/active") {
2822 if (open(IPADDR, "${General::swroot}/red/local-ipaddress")) {
2823 my $ipaddr = <IPADDR>;
2824 close IPADDR;
2825 chomp ($ipaddr);
2826 $cgiparams{'VPN_IP'} = (gethostbyaddr(pack("C4", split(/\./, $ipaddr)), 2))[0];
2827 if ($cgiparams{'VPN_IP'} eq '') {
2828 $cgiparams{'VPN_IP'} = $ipaddr;
2829 }
2830 }
2831 }
2832 # no IP found, use %defaultroute
2833 $cgiparams{'VPN_IP'} ='%defaultroute' if ($cgiparams{'VPN_IP'} eq '');
2834
2835 $cgiparams{'VPN_DELAYED_START'} = 0 if (! defined ($cgiparams{'VPN_DELAYED_START'}));
2836 $checked{'ENABLED'} = $cgiparams{'ENABLED'} eq 'on' ? "checked='checked'" : '';
2837
2838 &Header::showhttpheaders();
2839 &Header::openpage($Lang::tr{'ipsec'}, 1, '');
2840 &Header::openbigbox('100%', 'left', '', $errormessage);
2841
2842 if ($errormessage) {
2843 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
2844 print "<class name='base'>$errormessage\n";
2845 print "&nbsp;</class>\n";
2846 &Header::closebox();
2847 }
ac1cfefa 2848
4d81e0f3
AM
2849 if ($warnmessage) {
2850 &Header::openbox('100%', 'left', $Lang::tr{'warning messages'});
2851 print "$warnmessage<br>";
2852 print "$Lang::tr{'fwdfw warn1'}<br>";
2853 &Header::closebox();
03b08c08 2854 print"<center><form method='post'><input type='submit' name='ACTION' value='$Lang::tr{'ok'}' style='width: 5em;'></form>";
4d81e0f3
AM
2855 &Header::closepage();
2856 exit 0;
2857 }
2858
624615ee
LS
2859 &Header::openbox('100%', 'left', $Lang::tr{'global settings'});
2860 print <<END
2861 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
2862 <table width='100%'>
2863 <tr>
e3edceeb 2864 <td width='20%' class='base' nowrap='nowrap'>$Lang::tr{'vpn red name'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
ed84e8b8
MT
2865 <td width='20%'><input type='text' name='VPN_IP' value='$cgiparams{'VPN_IP'}' /></td>
2866 <td width='20%' class='base'>$Lang::tr{'enabled'}<input type='checkbox' name='ENABLED' $checked{'ENABLED'} /></td>
624615ee 2867 </tr>
ac1cfefa 2868END
624615ee 2869;
ac1cfefa 2870print <<END
624615ee
LS
2871 <tr>
2872 <td class='base' nowrap='nowrap'>$Lang::tr{'vpn delayed start'}:&nbsp;<img src='/blob.gif' alt='*' /><img src='/blob.gif' alt='*' /></td>
ed84e8b8 2873 <td ><input type='text' name='VPN_DELAYED_START' value='$cgiparams{'VPN_DELAYED_START'}' /></td>
624615ee
LS
2874 </tr>
2875 <tr>
2876 <td class='base' nowrap='nowrap'>$Lang::tr{'host to net vpn'}:</td>
9d85ac3b 2877 <td ><input type='text' name='RW_NET' value='$cgiparams{'RW_NET'}' /></td>
624615ee 2878 </tr>
0afd8493
AM
2879</table>
2880<br>
ac1cfefa
MT
2881<hr />
2882<table width='100%'>
2883<tr>
624615ee
LS
2884 <td class='base' valign='top'><img src='/blob.gif' alt='*' /></td>
2885 <td width='70%' class='base' valign='top'>$Lang::tr{'required field'}</td><td width='30%' align='right' class='base'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
ed84e8b8
MT
2886</tr>
2887<tr>
624615ee
LS
2888 <td class='base' valign='top' nowrap='nowrap'><img src='/blob.gif' alt='*' /><img src='/blob.gif' alt='*' />&nbsp;</td>
2889 <td class='base'> <font class='base'>$Lang::tr{'vpn delayed start help'}</font></td>
2890 <td></td>
ac1cfefa
MT
2891</tr>
2892</table>
2893END
624615ee
LS
2894;
2895 print "</form>";
2896 &Header::closebox();
2897
2898 &Header::openbox('100%', 'left', $Lang::tr{'connection status and controlc'});
2899 print <<END
2900 <table width='100%' cellspacing='1' cellpadding='0' class='tbl'>
2901 <tr>
e9850821
AM
2902 <th width='10%' class='boldbase' align='center'><b>$Lang::tr{'name'}</b></th>
2903 <th width='22%' class='boldbase' align='center'><b>$Lang::tr{'type'}</b></th>
2904 <th width='23%' class='boldbase' align='center'><b>$Lang::tr{'common name'}</b></th>
2905 <th width='30%' class='boldbase' align='center'><b>$Lang::tr{'remark'}</b></th>
2906 <th width='10%' class='boldbase' align='center'><b>$Lang::tr{'status'}</b></th>
2907 <th class='boldbase' align='center' colspan='6'><b>$Lang::tr{'action'}</b></th>
624615ee 2908 </tr>
ac1cfefa 2909END
624615ee
LS
2910;
2911 my $id = 0;
2912 my $gif;
2913 foreach my $key (sort { ncmp ($confighash{$a}[1],$confighash{$b}[1]) } keys %confighash) {
ac1cfefa
MT
2914 if ($confighash{$key}[0] eq 'on') { $gif = 'on.gif'; } else { $gif = 'off.gif'; }
2915
2916 if ($id % 2) {
e9850821
AM
2917 print "<tr>";
2918 $col="bgcolor='$color{'color20'}'";
ac1cfefa 2919 } else {
e9850821
AM
2920 print "<tr>";
2921 $col="bgcolor='$color{'color22'}'";
ac1cfefa 2922 }
e9850821
AM
2923 print "<td align='center' nowrap='nowrap' $col>$confighash{$key}[1]</td>";
2924 print "<td align='center' nowrap='nowrap' $col>" . $Lang::tr{"$confighash{$key}[3]"} . " (" . $Lang::tr{"$confighash{$key}[4]"} . ") $confighash{$key}[29]</td>";
ed84e8b8 2925 if ($confighash{$key}[2] eq '%auth-dn') {
624615ee 2926 print "<td align='left' nowrap='nowrap' $col>$confighash{$key}[9]</td>";
ed84e8b8 2927 } elsif ($confighash{$key}[4] eq 'cert') {
624615ee 2928 print "<td align='left' nowrap='nowrap' $col>$confighash{$key}[2]</td>";
ac1cfefa 2929 } else {
624615ee 2930 print "<td align='left' $col>&nbsp;</td>";
ac1cfefa 2931 }
e9850821 2932 print "<td align='center' $col>$confighash{$key}[25]</td>";
0afd8493 2933 my $col1="bgcolor='${Header::colourred}'";
0afd8493 2934 my $active = "<b><font color='#FFFFFF'>$Lang::tr{'capsclosed'}</font></b>";
aec1925b
MT
2935 if ($confighash{$key}[33] eq "add") {
2936 $col1="bgcolor='${Header::colourorange}'";
2937 $active = "<b><font color='#FFFFFF'>$Lang::tr{'vpn wait'}</font></b>";
2938 }
5fd30232 2939 foreach my $line (@status) {
624615ee
LS
2940 if (($line =~ /\"$confighash{$key}[1]\".*IPsec SA established/) ||
2941 ($line =~ /$confighash{$key}[1]\{.*INSTALLED/)) {
2942 $col1="bgcolor='${Header::colourgreen}'";
2943 $active = "<b><font color='#FFFFFF'>$Lang::tr{'capsopen'}</font></b>";
1fab4edf
MT
2944 } elsif ($line =~ /$confighash{$key}[1]\[.*CONNECTING/) {
2945 $col1="bgcolor='${Header::colourorange}'";
2946 $active = "<b><font color='#FFFFFF'>$Lang::tr{'vpn connecting'}</font></b>";
8057ab15
MT
2947 } elsif ($line =~ /$confighash{$key}[1]\{.*ROUTED/) {
2948 $col1="bgcolor='${Header::colourorange}'";
2949 $active = "<b><font color='#FFFFFF'>$Lang::tr{'vpn on-demand'}</font></b>";
624615ee
LS
2950 }
2951 }
2952 # move to blue if really down
0afd8493
AM
2953 if ($confighash{$key}[0] eq 'off' && $col1 =~ /${Header::colourred}/ ) {
2954 $col1="bgcolor='${Header::colourblue}'";
624615ee 2955 $active = "<b><font color='#FFFFFF'>$Lang::tr{'capsclosed'}</font></b>";
5fd30232 2956 }
ac1cfefa 2957 print <<END
0afd8493 2958 <td align='center' $col1>$active</td>
e9850821 2959 <td align='center' $col>
624615ee
LS
2960 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
2961 <input type='image' name='$Lang::tr{'restart'}' src='/images/reload.gif' alt='$Lang::tr{'restart'}' title='$Lang::tr{'restart'}' />
2962 <input type='hidden' name='ACTION' value='$Lang::tr{'restart'}' />
2963 <input type='hidden' name='KEY' value='$key' />
2964 </form>
ed84e8b8 2965 </td>
ac1cfefa 2966END
624615ee 2967;
ed84e8b8 2968 if (($confighash{$key}[4] eq 'cert') && ($confighash{$key}[2] ne '%auth-dn')) {
624615ee
LS
2969 print <<END
2970 <td align='center' $col>
2971 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
ed84e8b8 2972 <input type='image' name='$Lang::tr{'show certificate'}' src='/images/info.gif' alt='$Lang::tr{'show certificate'}' title='$Lang::tr{'show certificate'}' />
ac1cfefa
MT
2973 <input type='hidden' name='ACTION' value='$Lang::tr{'show certificate'}' />
2974 <input type='hidden' name='KEY' value='$key' />
624615ee
LS
2975 </form>
2976 </td>
ac1cfefa 2977END
624615ee
LS
2978;
2979 } else {
2980 print "<td width='2%' $col>&nbsp;</td>";
ac1cfefa 2981 }
624615ee
LS
2982 if ($confighash{$key}[4] eq 'cert' && -f "${General::swroot}/certs/$confighash{$key}[1].p12") {
2983 print <<END
2984 <td align='center' $col>
2985 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
ed84e8b8 2986 <input type='image' name='$Lang::tr{'download pkcs12 file'}' src='/images/floppy.gif' alt='$Lang::tr{'download pkcs12 file'}' title='$Lang::tr{'download pkcs12 file'}' />
ac1cfefa
MT
2987 <input type='hidden' name='ACTION' value='$Lang::tr{'download pkcs12 file'}' />
2988 <input type='hidden' name='KEY' value='$key' />
624615ee 2989 </form>
ed84e8b8 2990 </td>
ac1cfefa 2991END
624615ee
LS
2992;
2993 } elsif (($confighash{$key}[4] eq 'cert') && ($confighash{$key}[2] ne '%auth-dn')) {
2994 print <<END
2995 <td align='center' $col>
2996 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
ed84e8b8 2997 <input type='image' name='$Lang::tr{'download certificate'}' src='/images/floppy.gif' alt='$Lang::tr{'download certificate'}' title='$Lang::tr{'download certificate'}' />
ac1cfefa
MT
2998 <input type='hidden' name='ACTION' value='$Lang::tr{'download certificate'}' />
2999 <input type='hidden' name='KEY' value='$key' />
624615ee 3000 </form>
ed84e8b8 3001 </td>
ac1cfefa 3002END
624615ee
LS
3003;
3004 } else {
3005 print "<td width='2%' $col>&nbsp;</td>";
ac1cfefa
MT
3006 }
3007 print <<END
e9850821 3008 <td align='center' $col>
624615ee
LS
3009 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
3010 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$Lang::tr{'toggle enable disable'}' title='$Lang::tr{'toggle enable disable'}' />
3011 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
3012 <input type='hidden' name='KEY' value='$key' />
3013 </form>
ed84e8b8 3014 </td>
ac1cfefa 3015
e9850821 3016 <td align='center' $col>
624615ee
LS
3017 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
3018 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
3019 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
3020 <input type='hidden' name='KEY' value='$key' />
3021 </form>
ed84e8b8 3022 </td>
e9850821 3023 <td align='center' $col>
624615ee
LS
3024 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
3025 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
3026 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
3027 <input type='hidden' name='KEY' value='$key' />
3028 </form>
ed84e8b8 3029 </td>
ac1cfefa
MT
3030 </tr>
3031END
624615ee 3032;
ac1cfefa 3033 $id++;
624615ee
LS
3034 }
3035 print "</table>";
3036
3037 # If the config file contains entries, print Key to action icons
3038 if ( $id ) {
3039 print <<END
3040 <table>
3041 <tr>
3042 <td class='boldbase'>&nbsp; <b>$Lang::tr{'legend'}:</b></td>
3043 <td>&nbsp; <img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
3044 <td class='base'>$Lang::tr{'click to disable'}</td>
3045 <td>&nbsp; &nbsp; <img src='/images/info.gif' alt='$Lang::tr{'show certificate'}' /></td>
3046 <td class='base'>$Lang::tr{'show certificate'}</td>
3047 <td>&nbsp; &nbsp; <img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
3048 <td class='base'>$Lang::tr{'edit'}</td>
3049 <td>&nbsp; &nbsp; <img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
3050 <td class='base'>$Lang::tr{'remove'}</td>
3051 </tr>
3052 <tr>
3053 <td>&nbsp; </td>
3054 <td>&nbsp; <img src='/images/off.gif' alt='?OFF' /></td>
3055 <td class='base'>$Lang::tr{'click to enable'}</td>
3056 <td>&nbsp; &nbsp; <img src='/images/floppy.gif' alt='?FLOPPY' /></td>
3057 <td class='base'>$Lang::tr{'download certificate'}</td>
3058 <td>&nbsp; &nbsp; <img src='/images/reload.gif' alt='?RELOAD'/></td>
3059 <td class='base'>$Lang::tr{'restart'}</td>
3060 </tr>
3061 </table>
ac1cfefa 3062END
624615ee
LS
3063;
3064 }
ac1cfefa 3065
624615ee
LS
3066 print <<END
3067 <table width='100%'>
3068 <tr><td align='right' colspan='9'>
ed84e8b8
MT
3069 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
3070 <input type='submit' name='ACTION' value='$Lang::tr{'add'}' />
3071 </form>
624615ee
LS
3072 </td></tr>
3073 </table>
ac1cfefa 3074END
624615ee
LS
3075;
3076 &Header::closebox();
ac1cfefa 3077
624615ee
LS
3078 &Header::openbox('100%', 'left', "$Lang::tr{'certificate authorities'}");
3079 print <<EOF
3080 <table width='100%' cellspacing='1' cellpadding='0' class='tbl'>
3081 <tr>
e9850821
AM
3082 <th width='25%' class='boldbase' align='center'><b>$Lang::tr{'name'}</b></th>
3083 <th width='65%' class='boldbase' align='center'><b>$Lang::tr{'subject'}</b></th>
3084 <th width='10%' class='boldbase' colspan='3' align='center'><b>$Lang::tr{'action'}</b></th>
624615ee 3085 </tr>
ac1cfefa 3086EOF
624615ee
LS
3087;
3088 my $col1="bgcolor='$color{'color22'}'";
e9850821 3089 my $col2="bgcolor='$color{'color20'}'";
624615ee
LS
3090 if (-f "${General::swroot}/ca/cacert.pem") {
3091 my $casubject = &Header::cleanhtml(getsubjectfromcert ("${General::swroot}/ca/cacert.pem"));
3092 print <<END
3093 <tr>
3094 <td class='base' $col1>$Lang::tr{'root certificate'}</td>
3095 <td class='base' $col1>$casubject</td>
3096 <td width='3%' align='center' $col1>
3097 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
3098 <input type='hidden' name='ACTION' value='$Lang::tr{'show root certificate'}' />
3099 <input type='image' name='$Lang::tr{'edit'}' src='/images/info.gif' alt='$Lang::tr{'show root certificate'}' title='$Lang::tr{'show root certificate'}' />
3100 </form>
3101 </td>
3102 <td width='3%' align='center' $col1>
3103 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
3104 <input type='image' name='$Lang::tr{'download root certificate'}' src='/images/floppy.gif' alt='$Lang::tr{'download root certificate'}' title='$Lang::tr{'download root certificate'}' />
3105 <input type='hidden' name='ACTION' value='$Lang::tr{'download root certificate'}' />
3106 </form>
3107 </td>
3108 <td width='4%' $col1>&nbsp;</td></tr>
ac1cfefa 3109END
624615ee
LS
3110;
3111 } else {
3112 # display rootcert generation buttons
3113 print <<END
3114 <tr>
3115 <td class='base' $col1>$Lang::tr{'root certificate'}:</td>
3116 <td class='base' $col1>$Lang::tr{'not present'}</td>
3117 <td colspan='3' $col1>&nbsp;</td></tr>
ac1cfefa 3118END
624615ee
LS
3119;
3120 }
ac1cfefa 3121
624615ee
LS
3122 if (-f "${General::swroot}/certs/hostcert.pem") {
3123 my $hostsubject = &Header::cleanhtml(getsubjectfromcert ("${General::swroot}/certs/hostcert.pem"));
ac1cfefa 3124
624615ee
LS
3125 print <<END
3126 <tr>
3127 <td class='base' $col2>$Lang::tr{'host certificate'}</td>
3128 <td class='base' $col2>$hostsubject</td>
3129 <td width='3%' align='center' $col2>
3130 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
3131 <input type='hidden' name='ACTION' value='$Lang::tr{'show host certificate'}' />
3132 <input type='image' name='$Lang::tr{'show host certificate'}' src='/images/info.gif' alt='$Lang::tr{'show host certificate'}' title='$Lang::tr{'show host certificate'}' />
3133 </form>
3134 </td>
3135 <td width='3%' align='center' $col2>
3136 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
3137 <input type='image' name="$Lang::tr{'download host certificate'}" src='/images/floppy.gif' alt="$Lang::tr{'download host certificate'}" title="$Lang::tr{'download host certificate'}" />
3138 <input type='hidden' name='ACTION' value="$Lang::tr{'download host certificate'}" />
3139 </form>
3140 </td>
3141 <td width='4%' $col2>&nbsp;</td></tr>
ac1cfefa 3142END
624615ee
LS
3143;
3144 } else {
3145 # Nothing
3146 print <<END
3147 <tr>
3148 <td width='25%' class='base' $col2>$Lang::tr{'host certificate'}:</td>
3149 <td class='base' $col2>$Lang::tr{'not present'}</td>
3150 <td colspan='3' $col2>&nbsp;</td></tr>
ac1cfefa 3151END
624615ee
LS
3152;
3153 }
3154
e9850821
AM
3155 my $rowcolor = 0;
3156 if (keys %cahash > 0) {
3157 foreach my $key (keys %cahash) {
624615ee
LS
3158 if ($rowcolor++ % 2) {
3159 print "<tr>";
3160 $col="bgcolor='$color{'color20'}'";
3161 } else {
3162 print "<tr>";
3163 $col="bgcolor='$color{'color22'}'";
3164 }
3165 print "<td class='base' $col>$cahash{$key}[0]</td>\n";
3166 print "<td class='base' $col>$cahash{$key}[1]</td>\n";
3167 print <<END
3168 <td align='center' $col>
3169 <form method='post' name='cafrm${key}a' action='$ENV{'SCRIPT_NAME'}'>
3170 <input type='image' name='$Lang::tr{'show ca certificate'}' src='/images/info.gif' alt='$Lang::tr{'show ca certificate'}' title='$Lang::tr{'show ca certificate'}' />
3171 <input type='hidden' name='ACTION' value='$Lang::tr{'show ca certificate'}' />
3172 <input type='hidden' name='KEY' value='$key' />
3173 </form>
3174 </td>
3175 <td align='center' $col>
3176 <form method='post' name='cafrm${key}b' action='$ENV{'SCRIPT_NAME'}'>
3177 <input type='image' name='$Lang::tr{'download ca certificate'}' src='/images/floppy.gif' alt='$Lang::tr{'download ca certificate'}' title='$Lang::tr{'download ca certificate'}' />
3178 <input type='hidden' name='ACTION' value='$Lang::tr{'download ca certificate'}' />
3179 <input type='hidden' name='KEY' value='$key' />
3180 </form>
3181 </td>
3182 <td align='center' $col>
3183 <form method='post' name='cafrm${key}c' action='$ENV{'SCRIPT_NAME'}'>
3184 <input type='hidden' name='ACTION' value='$Lang::tr{'remove ca certificate'}' />
3185 <input type='image' name='$Lang::tr{'remove ca certificate'}' src='/images/delete.gif' alt='$Lang::tr{'remove ca certificate'}' title='$Lang::tr{'remove ca certificate'}' />
3186 <input type='hidden' name='KEY' value='$key' />
3187 </form>
3188 </td>
3189 </tr>
3190END
3191;
3192 }
3193 }
3194 print "</table>";
3195
3196 # If the file contains entries, print Key to action icons
3197 if ( -f "${General::swroot}/ca/cacert.pem") {
3198 print <<END
3199 <table><tr>
3200 <td class='boldbase'>&nbsp; <b>$Lang::tr{'legend'}:</b></td>
3201 <td>&nbsp; &nbsp; <img src='/images/info.gif' alt='$Lang::tr{'show certificate'}' /></td>
3202 <td class='base'>$Lang::tr{'show certificate'}</td>
3203 <td>&nbsp; &nbsp; <img src='/images/floppy.gif' alt='$Lang::tr{'download certificate'}' /></td>
3204 <td class='base'>$Lang::tr{'download certificate'}</td>
3205 </tr></table>
ac1cfefa 3206END
624615ee 3207;
ac1cfefa 3208 }
624615ee 3209 my $createCA = -f "${General::swroot}/ca/cacert.pem" ? '' : "<tr><td colspan='3'></td><td><input type='submit' name='ACTION' value='$Lang::tr{'generate root/host certificates'}' /></td></tr>";
ed84e8b8 3210 print <<END
624615ee
LS
3211 <br>
3212 <hr />
3213 <form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'>
3214 <table width='100%' border='0' cellspacing='1' cellpadding='0'>
3215 $createCA
3216 <tr>
e3edceeb 3217 <td class='base' nowrap='nowrap'>$Lang::tr{'ca name'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
ed84e8b8
MT
3218 <td nowrap='nowrap'><input type='text' name='CA_NAME' value='$cgiparams{'CA_NAME'}' size='15' /> </td>
3219 <td nowrap='nowrap'><input type='file' name='FH' size='30' /></td>
3220 <td nowrap='nowrap'><input type='submit' name='ACTION' value='$Lang::tr{'upload ca certificate'}' /></td>
624615ee
LS
3221 </tr>
3222 <tr>
ed84e8b8 3223 <td colspan='3'>$Lang::tr{'resetting the vpn configuration will remove the root ca, the host certificate and all certificate based connections'}:</td>
0afd8493 3224 <td align='right'><input type='submit' name='ACTION' value='$Lang::tr{'remove x509'}' /></td>
624615ee
LS
3225 </tr>
3226 </table>
3227 </form>
ac1cfefa 3228END
624615ee
LS
3229;
3230 &Header::closebox();
3231 &Header::closebigbox();
3232 &Header::closepage();
e8b3bb0e
MT
3233
3234sub array_unique($) {
3235 my $array = shift;
3236 my @unique = ();
3237
3238 my %seen = ();
3239 foreach my $e (@$array) {
3240 next if $seen{$e}++;
3241 push(@unique, $e);
3242 }
3243
3244 return @unique;
3245}
3246
3247sub make_algos($$$$$) {
3248 my ($mode, $encs, $ints, $grps, $pfs) = @_;
3249 my @algos = ();
3250
3251 foreach my $enc (@$encs) {
3252 foreach my $int (@$ints) {
3253 foreach my $grp (@$grps) {
3254 my @algo = ($enc);
3255
78039c15 3256 if ($mode eq "ike") {
e8b3bb0e 3257 push(@algo, $int);
e8b3bb0e 3258
2c531c21 3259 if ($grp =~ m/^e(.*)$/) {
e8b3bb0e 3260 push(@algo, "ecp$1");
e34e72b6
PM
3261 } elsif ($grp =~ m/curve25519/) {
3262 push(@algo, "$grp");
e8b3bb0e
MT
3263 } else {
3264 push(@algo, "modp$grp");
3265 }
e8b3bb0e 3266
78039c15
MT
3267 } elsif ($mode eq "esp" && $pfs) {
3268 my $is_aead = ($enc =~ m/[cg]cm/);
3269
3270 if (!$is_aead) {
3271 push(@algo, $int);
3272 }
4b02b404
MT
3273
3274 if ($grp eq "none") {
3275 # noop
3276 } elsif ($grp =~ m/^e(.*)$/) {
3277 push(@algo, "ecp$1");
e34e72b6
PM
3278 } elsif ($grp =~ m/curve25519/) {
3279 push(@algo, "$grp");
4b02b404
MT
3280 } else {
3281 push(@algo, "modp$grp");
3282 }
e8b3bb0e
MT
3283 }
3284
3285 push(@algos, join("-", @algo));
3286 }
3287 }
3288 }
3289
3290 return &array_unique(\@algos);
3291}
8792caad
MT
3292
3293sub make_subnets($) {
3294 my $subnets = shift;
3295
3296 my @nets = split(/\|/, $subnets);
3297 my @cidr_nets = ();
3298 foreach my $net (@nets) {