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