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