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