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