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