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