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