IPsec: Update ipsec.conf for GRE/VTI changes
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / vpnmain.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 #                                                                             #
4 # IPFire.org - A linux based firewall                                         #
5 # Copyright (C) 2007-2013  IPFire Team  info@ipfire.org                       #
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 ###############################################################################
21
22 use Net::DNS;
23 use File::Copy;
24 use File::Temp qw/ tempfile tempdir /;
25 use strict;
26 use Sort::Naturally;
27 # enable only the following on debugging purpose
28 #use warnings;
29 #use CGI::Carp 'fatalsToBrowser';
30
31 require '/var/ipfire/general-functions.pl';
32 require "${General::swroot}/lang.pl";
33 require "${General::swroot}/header.pl";
34 require "${General::swroot}/countries.pl";
35
36 #workaround to suppress a warning when a variable is used only once
37 my @dummy = ( ${Header::colourgreen}, ${Header::colourblue} );
38 undef (@dummy);
39
40 ###
41 ### Initialize variables
42 ###
43 my $sleepDelay = 4; # after a call to ipsecctrl S or R, wait this delay (seconds) before reading status (let the ipsec do its job)
44 my %netsettings=();
45 our %cgiparams=();
46 our %vpnsettings=();
47 my %checked=();
48 my %confighash=();
49 my %cahash=();
50 my %selected=();
51 my $warnmessage = '';
52 my $errormessage = '';
53
54 my %color = ();
55 my %mainsettings = ();
56 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
57 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
58
59 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
60
61 my %INACTIVITY_TIMEOUTS = (
62         300             => $Lang::tr{'five minutes'},
63         600             => $Lang::tr{'ten minutes'},
64         900             => $Lang::tr{'fifteen minutes'},
65         1800            => $Lang::tr{'thirty minutes'},
66         3600            => $Lang::tr{'one hour'},
67         43200           => $Lang::tr{'twelve hours'},
68         86400           => $Lang::tr{'24 hours'},
69         0               => "- $Lang::tr{'unlimited'} -",
70 );
71
72 my $col="";
73
74 $cgiparams{'ENABLED'} = 'off';
75 $cgiparams{'EDIT_ADVANCED'} = 'off';
76 $cgiparams{'ACTION'} = '';
77 $cgiparams{'CA_NAME'} = '';
78 $cgiparams{'KEY'} = '';
79 $cgiparams{'TYPE'} = '';
80 $cgiparams{'ADVANCED'} = '';
81 $cgiparams{'NAME'} = '';
82 $cgiparams{'LOCAL_SUBNET'} = '';
83 $cgiparams{'REMOTE_SUBNET'} = '';
84 $cgiparams{'REMOTE'} = '';
85 $cgiparams{'LOCAL_ID'} = '';
86 $cgiparams{'REMOTE_ID'} = '';
87 $cgiparams{'REMARK'} = '';
88 $cgiparams{'PSK'} = '';
89 $cgiparams{'CERT_NAME'} = '';
90 $cgiparams{'CERT_EMAIL'} = '';
91 $cgiparams{'CERT_OU'} = '';
92 $cgiparams{'CERT_ORGANIZATION'} = '';
93 $cgiparams{'CERT_CITY'} = '';
94 $cgiparams{'CERT_STATE'} = '';
95 $cgiparams{'CERT_COUNTRY'} = '';
96 $cgiparams{'SUBJECTALTNAME'} = '';
97 $cgiparams{'CERT_PASS1'} = '';
98 $cgiparams{'CERT_PASS2'} = '';
99 $cgiparams{'ROOTCERT_HOSTNAME'} = '';
100 $cgiparams{'ROOTCERT_COUNTRY'} = '';
101 $cgiparams{'P12_PASS'} = '';
102 $cgiparams{'ROOTCERT_ORGANIZATION'} = '';
103 $cgiparams{'ROOTCERT_HOSTNAME'} = '';
104 $cgiparams{'ROOTCERT_EMAIL'} = '';
105 $cgiparams{'ROOTCERT_OU'} = '';
106 $cgiparams{'ROOTCERT_CITY'} = '';
107 $cgiparams{'ROOTCERT_STATE'} = '';
108 $cgiparams{'RW_NET'} = '';
109 $cgiparams{'DPD_DELAY'} = '30';
110 $cgiparams{'DPD_TIMEOUT'} = '120';
111 $cgiparams{'FORCE_MOBIKE'} = 'off';
112 $cgiparams{'START_ACTION'} = 'route';
113 $cgiparams{'INACTIVITY_TIMEOUT'} = 1800;
114 $cgiparams{'MODE'} = "tunnel";
115 $cgiparams{'INTERFACE_MODE'} = "";
116 $cgiparams{'INTERFACE_ADDRESS'} = "";
117 $cgiparams{'INTERFACE_MTU'} = 1500;
118 &Header::getcgihash(\%cgiparams, {'wantfile' => 1, 'filevar' => 'FH'});
119
120 ###
121 ### Useful functions
122 ###
123 sub valid_dns_host {
124         my $hostname = $_[0];
125         unless ($hostname) { return "No hostname"};
126         my $res = new Net::DNS::Resolver;
127         my $query = $res->search("$hostname");
128         if ($query) {
129                 foreach my $rr ($query->answer) {
130                         ## Potential bug - we are only looking at A records:
131                         return 0 if $rr->type eq "A";
132                 }
133         } else {
134                 return $res->errorstring;
135         }
136 }
137 ###
138 ### Just return true is one interface is vpn enabled
139 ###
140 sub vpnenabled {
141         return ($vpnsettings{'ENABLED'} eq 'on');
142 }
143 ###
144 ### old version: maintain serial number to one, without explication.
145 ### this: let the counter go, so that each cert is numbered.
146 ###
147 sub cleanssldatabase {
148         if (open(FILE, ">${General::swroot}/certs/serial")) {
149                 print FILE "01";
150                 close FILE;
151         }
152         if (open(FILE, ">${General::swroot}/certs/index.txt")) {
153                 print FILE "";
154                 close FILE;
155         }
156         if (open(FILE, ">${General::swroot}/certs/index.txt.attr")) {
157                 print FILE "";
158                 close FILE;
159         }
160         unlink ("${General::swroot}/certs/index.txt.old");
161         unlink ("${General::swroot}/certs/index.txt.attr.old");
162         unlink ("${General::swroot}/certs/serial.old");
163         unlink ("${General::swroot}/certs/01.pem");
164 }
165 sub newcleanssldatabase {
166         if (! -s "${General::swroot}/certs/serial" ) {
167                 open(FILE, ">${General::swroot}/certs/serial");
168                 print FILE "01";
169                 close FILE;
170         }
171         if (! -s ">${General::swroot}/certs/index.txt") {
172                 system ("touch ${General::swroot}/certs/index.txt");
173         }
174         if (! -s ">${General::swroot}/certs/index.txt.attr") {
175                 system ("touch ${General::swroot}/certs/index.txt.attr");
176         }
177         unlink ("${General::swroot}/certs/index.txt.old");
178         unlink ("${General::swroot}/certs/index.txt.attr.old");
179         unlink ("${General::swroot}/certs/serial.old");
180 #       unlink ("${General::swroot}/certs/01.pem");             numbering evolves. Wrong place to delete
181 }
182
183 ###
184 ### Call openssl and return errormessage if any
185 ###
186 sub callssl ($) {
187         my $opt = shift;
188         my $retssl = `/usr/bin/openssl $opt 2>&1`; #redirect stderr
189         my $ret = '';
190         foreach my $line (split (/\n/, $retssl)) {
191                 &General::log("ipsec", "$line") if (0); # 1 for verbose logging
192                 $ret .= '<br>'.$line if ( $line =~ /error|unknown/ );
193         }
194         if ($ret) {
195                 $ret= &Header::cleanhtml($ret);
196         }
197         return $ret ? "$Lang::tr{'openssl produced an error'}: $ret" : '' ;
198 }
199 ###
200 ### Obtain a CN from given cert
201 ###
202 sub getCNfromcert ($) {
203         #&General::log("ipsec", "Extracting name from $_[0]...");
204         my $temp = `/usr/bin/openssl x509 -text -in $_[0]`;
205         $temp =~ /Subject:.*CN = (.*)[\n]/;
206         $temp = $1;
207         $temp =~ s+/Email+, E+;
208         $temp =~ s/ ST = / S = /;
209         $temp =~ s/,//g;
210         $temp =~ s/\'//g;
211         return $temp;
212 }
213 ###
214 ### Obtain Subject from given cert
215 ###
216 sub getsubjectfromcert ($) {
217         #&General::log("ipsec", "Extracting subject from $_[0]...");
218         my $temp = `/usr/bin/openssl x509 -text -in $_[0]`;
219         $temp =~ /Subject: (.*)[\n]/;
220         $temp = $1;
221         $temp =~ s+/Email+, E+;
222         $temp =~ s/ ST = / S = /;
223         return $temp;
224 }
225 ###
226 ### Combine local subnet and connection name to make a unique name for each connection section
227 ### (this sub is not used now)
228 ###
229 sub makeconnname ($) {
230         my $conn = shift;
231         my $subnet = shift;
232
233         $subnet =~ /^(.*?)\/(.*?)$/; # $1=IP $2=mask
234         my $ip = unpack('N', &Socket::inet_aton($1));
235         if (length ($2) > 2) {
236                 my $mm = unpack('N', &Socket::inet_aton($2));
237                 while ( ($mm & 1)==0 ) {
238                         $ip >>= 1;
239                         $mm >>= 1;
240                 };
241         } else {
242                 $ip >>= (32 - $2);
243         }
244         return sprintf ("%s-%X", $conn, $ip);
245 }
246 ###
247 ### Write a config file.
248 ###
249 ###Type=Host : GUI can choose the interface used (RED,GREEN,BLUE) and
250 ###             the side is always defined as 'left'.
251 ###
252
253 sub writeipsecfiles {
254         my %lconfighash = ();
255         my %lvpnsettings = ();
256         &General::readhasharray("${General::swroot}/vpn/config", \%lconfighash);
257         &General::readhash("${General::swroot}/vpn/settings", \%lvpnsettings);
258
259         open(CONF, ">${General::swroot}/vpn/ipsec.conf") or die "Unable to open ${General::swroot}/vpn/ipsec.conf: $!";
260         open(SECRETS, ">${General::swroot}/vpn/ipsec.secrets") or die "Unable to open ${General::swroot}/vpn/ipsec.secrets: $!";
261         flock CONF, 2;
262         flock SECRETS, 2;
263         print CONF "version 2\n\n";
264         print CONF "conn %default\n";
265         print CONF "\tkeyingtries=%forever\n";
266         print CONF "\n";
267
268         # Add user includes to config file
269         if (-e "/etc/ipsec.user.conf") {
270                 print CONF "include /etc/ipsec.user.conf\n";
271                 print CONF "\n";
272         }
273
274         print SECRETS "include /etc/ipsec.user.secrets\n";
275
276         if (-f "${General::swroot}/certs/hostkey.pem") {
277                 print SECRETS ": RSA ${General::swroot}/certs/hostkey.pem\n"
278         }
279         my $last_secrets = ''; # old the less specifics connections
280
281         foreach my $key (keys %lconfighash) {
282                 next if ($lconfighash{$key}[0] ne 'on');
283
284                 #remote peer is not set? => use '%any'
285                 $lconfighash{$key}[10] = '%any' if ($lconfighash{$key}[10] eq '');
286
287                 my $localside;
288                 if ($lconfighash{$key}[26] eq 'BLUE') {
289                         $localside = $netsettings{'BLUE_ADDRESS'};
290                 } elsif ($lconfighash{$key}[26] eq 'GREEN') {
291                         $localside = $netsettings{'GREEN_ADDRESS'};
292                 } elsif ($lconfighash{$key}[26] eq 'ORANGE') {
293                         $localside = $netsettings{'ORANGE_ADDRESS'};
294                 } else { # it is RED
295                         $localside = $lvpnsettings{'VPN_IP'};
296                 }
297
298                 my $interface_mode = $lconfighash{$key}[36];
299
300                 print CONF "conn $lconfighash{$key}[1]\n";
301                 print CONF "\tleft=$localside\n";
302
303                 if ($interface_mode eq "gre") {
304                         print CONF "\tleftsubnet=%dynamic[gre]\n";
305                 } elsif ($interface_mode eq "vti") {
306                         print CONF "\tleftsubnet=0.0.0.0/0\n";
307                 } else {
308                         print CONF "\tleftsubnet=" . &make_subnets($lconfighash{$key}[8]) . "\n";
309                 }
310
311                 print CONF "\tleftfirewall=yes\n";
312                 print CONF "\tlefthostaccess=yes\n";
313                 print CONF "\tright=$lconfighash{$key}[10]\n";
314
315                 if ($lconfighash{$key}[3] eq 'net') {
316                         if ($interface_mode eq "gre") {
317                                 print CONF "\trightsubnet=%dynamic[gre]\n";
318                         } elsif ($interface_mode eq "vti") {
319                                 print CONF "\trightsubnet=0.0.0.0/0\n";
320                         } else {
321                                 print CONF "\trightsubnet=" . &make_subnets($lconfighash{$key}[11]) . "\n";
322                         }
323                 }
324
325                 # Local Cert and Remote Cert (unless auth is DN dn-auth)
326                 if ($lconfighash{$key}[4] eq 'cert') {
327                         print CONF "\tleftcert=${General::swroot}/certs/hostcert.pem\n";
328                         print CONF "\trightcert=${General::swroot}/certs/$lconfighash{$key}[1]cert.pem\n" if ($lconfighash{$key}[2] ne '%auth-dn');
329                 }
330
331                 # Local and Remote IDs
332                 print CONF "\tleftid=\"$lconfighash{$key}[7]\"\n" if ($lconfighash{$key}[7]);
333                 print CONF "\trightid=\"$lconfighash{$key}[9]\"\n" if ($lconfighash{$key}[9]);
334
335                 # Set mode
336                 if ($lconfighash{$key}[35] eq "transport") {
337                         print CONF "\ttype=transport\n";
338                 } else {
339                         print CONF "\ttype=tunnel\n";
340                 }
341
342                 # Add mark for VTI
343                 if ($interface_mode eq "vti") {
344                         print CONF "\tmark=$key\n";
345                 }
346
347                 # Is PFS enabled?
348                 my $pfs = $lconfighash{$key}[28] eq 'on' ? 'on' : 'off';
349
350                 # Algorithms
351                 if ($lconfighash{$key}[18] && $lconfighash{$key}[19] && $lconfighash{$key}[20]) {
352                         my @encs        = split('\|', $lconfighash{$key}[18]);
353                         my @ints        = split('\|', $lconfighash{$key}[19]);
354                         my @groups      = split('\|', $lconfighash{$key}[20]);
355
356                         my @algos = &make_algos("ike", \@encs, \@ints, \@groups, 1);
357                         print CONF "\tike=" . join(",", @algos);
358
359                         if ($lconfighash{$key}[24] eq 'on') { #only proposed algorythms?
360                                 print CONF "!\n";
361                         } else {
362                                 print CONF "\n";
363                         }
364                 }
365
366                 if ($lconfighash{$key}[21] && $lconfighash{$key}[22]) {
367                         my @encs        = split('\|', $lconfighash{$key}[21]);
368                         my @ints        = split('\|', $lconfighash{$key}[22]);
369                         my @groups      = split('\|', $lconfighash{$key}[23]);
370
371                         # Use IKE grouptype if no ESP group type has been selected
372                         # (for backwards compatibility)
373                         if ($lconfighash{$key}[23] eq "") {
374                                 @groups = split('\|', $lconfighash{$key}[20]);
375                         }
376
377                         my @algos = &make_algos("esp", \@encs, \@ints, \@groups, ($pfs eq "on"));
378                         print CONF "\tesp=" . join(",", @algos);
379
380                         if ($lconfighash{$key}[24] eq 'on') { #only proposed algorythms?
381                                 print CONF "!\n";
382                         } else {
383                                 print CONF "\n";
384                         }
385                 }
386
387                 # IKE V1 or V2
388                 if (! $lconfighash{$key}[29]) {
389                         $lconfighash{$key}[29] = "ikev1";
390                 }
391
392                 print CONF "\tkeyexchange=$lconfighash{$key}[29]\n";
393
394                 # Lifetimes
395                 print CONF "\tikelifetime=$lconfighash{$key}[16]h\n" if ($lconfighash{$key}[16]);
396                 print CONF "\tkeylife=$lconfighash{$key}[17]h\n" if ($lconfighash{$key}[17]);
397
398                 # Compression
399                 print CONF "\tcompress=yes\n" if ($lconfighash{$key}[13] eq 'on');
400
401                 # Force MOBIKE?
402                 if (($lconfighash{$key}[29] eq "ikev2") && ($lconfighash{$key}[32] eq 'on')) {
403                         print CONF "\tmobike=yes\n";
404                 }
405
406                 # Dead Peer Detection
407                 my $dpdaction = $lconfighash{$key}[27];
408                 print CONF "\tdpdaction=$dpdaction\n";
409
410                 # If the dead peer detection is disabled and IKEv2 is used,
411                 # dpddelay must be set to zero, too.
412                 if ($dpdaction eq "none") {
413                         if ($lconfighash{$key}[29] eq "ikev2") {
414                                 print CONF "\tdpddelay=0\n";
415                         }
416                 } else {
417                         my $dpddelay = $lconfighash{$key}[31];
418                         if (!$dpddelay) {
419                                 $dpddelay = 30;
420                         }
421                         print CONF "\tdpddelay=$dpddelay\n";
422                         my $dpdtimeout = $lconfighash{$key}[30];
423                         if (!$dpdtimeout) {
424                                 $dpdtimeout = 120;
425                         }
426                         print CONF "\tdpdtimeout=$dpdtimeout\n";
427                 }
428
429                 # Build Authentication details: LEFTid RIGHTid : PSK psk
430                 my $psk_line;
431                 if ($lconfighash{$key}[4] eq 'psk') {
432                         $psk_line = ($lconfighash{$key}[7] ? $lconfighash{$key}[7] : $localside) . " " ;
433                         $psk_line .= $lconfighash{$key}[9] ? $lconfighash{$key}[9] : $lconfighash{$key}[10]; #remoteid or remote address?
434                         $psk_line .= " : PSK '$lconfighash{$key}[5]'\n";
435                         # if the line contains %any, it is less specific than two IP or ID, so move it at end of file.
436                         if ($psk_line =~ /%any/) {
437                                 $last_secrets .= $psk_line;
438                         } else {
439                                 print SECRETS $psk_line;
440                         }
441                         print CONF "\tauthby=secret\n";
442                 } else {
443                         print CONF "\tauthby=rsasig\n";
444                         print CONF "\tleftrsasigkey=%cert\n";
445                         print CONF "\trightrsasigkey=%cert\n";
446                 }
447
448                 my $start_action = $lconfighash{$key}[33];
449                 if (!$start_action) {
450                         $start_action = "start";
451                 }
452
453                 my $inactivity_timeout = $lconfighash{$key}[34];
454                 if ($inactivity_timeout eq "") {
455                         $inactivity_timeout = 900;
456                 }
457
458                 # Automatically start only if a net-to-net connection
459                 if ($lconfighash{$key}[3] eq 'host') {
460                         print CONF "\tauto=add\n";
461                         print CONF "\trightsourceip=$lvpnsettings{'RW_NET'}\n";
462                 } else {
463                         print CONF "\tauto=$start_action\n";
464
465                         # If in on-demand mode, we terminate the tunnel
466                         # after 15 min of no traffic
467                         if ($start_action eq 'route' && $inactivity_timeout > 0) {
468                                 print CONF "\tinactivity=$inactivity_timeout\n";
469                         }
470                 }
471
472                 # Fragmentation
473                 print CONF "\tfragmentation=yes\n";
474
475                 print CONF "\n";
476         } #foreach key
477
478         # Add post user includes to config file
479         # After the GUI-connections allows to patch connections.
480         if (-e "/etc/ipsec.user-post.conf") {
481                 print CONF "include /etc/ipsec.user-post.conf\n";
482                 print CONF "\n";
483         }
484
485         print SECRETS $last_secrets if ($last_secrets);
486         close(CONF);
487         close(SECRETS);
488 }
489
490 # Hook to regenerate the configuration files.
491 if ($ENV{"REMOTE_ADDR"} eq "") {
492         writeipsecfiles();
493         exit(0);
494 }
495
496 ###
497 ### Save main settings
498 ###
499 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq '' && $cgiparams{'KEY'} eq '') {
500         &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
501
502         unless (&General::validfqdn($cgiparams{'VPN_IP'}) || &General::validip($cgiparams{'VPN_IP'})
503         || $cgiparams{'VPN_IP'} eq '%defaultroute' ) {
504                 $errormessage = $Lang::tr{'invalid input for hostname'};
505                 goto SAVE_ERROR;
506         }
507
508         unless ($cgiparams{'VPN_DELAYED_START'} =~ /^[0-9]{1,3}$/ ) { #allow 0-999 seconds !
509                 $errormessage = $Lang::tr{'invalid time period'};
510                 goto SAVE_ERROR;
511         }
512
513         if ( $cgiparams{'RW_NET'} ne '' and !&General::validipandmask($cgiparams{'RW_NET'}) ) {
514                 $errormessage = $Lang::tr{'urlfilter invalid ip or mask error'};
515                 goto SAVE_ERROR;
516         }
517
518         $vpnsettings{'ENABLED'} = $cgiparams{'ENABLED'};
519         $vpnsettings{'VPN_IP'} = $cgiparams{'VPN_IP'};
520         $vpnsettings{'VPN_DELAYED_START'} = $cgiparams{'VPN_DELAYED_START'};
521         $vpnsettings{'RW_NET'} = $cgiparams{'RW_NET'};
522         &General::writehash("${General::swroot}/vpn/settings", \%vpnsettings);
523         &writeipsecfiles();
524         if (&vpnenabled) {
525                 system('/usr/local/bin/ipsecctrl', 'S');
526         } else {
527                 system('/usr/local/bin/ipsecctrl', 'D');
528         }
529         sleep $sleepDelay;
530         SAVE_ERROR:
531 ###
532 ### Reset all step 2
533 ###
534 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove x509'} && $cgiparams{'AREUSURE'} eq 'yes') {
535         &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
536
537         foreach my $key (keys %confighash) {
538                 if ($confighash{$key}[4] eq 'cert') {
539                         delete $confighash{$key};
540                 }
541         }
542         while (my $file = glob("${General::swroot}/{ca,certs,crls,private}/*")) {
543                 unlink $file
544         }
545         &cleanssldatabase();
546         if (open(FILE, ">${General::swroot}/vpn/caconfig")) {
547                 print FILE "";
548                 close FILE;
549         }
550         &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
551         &writeipsecfiles();
552         system('/usr/local/bin/ipsecctrl', 'R');
553         sleep $sleepDelay;
554
555 ###
556 ### Reset all step 1
557 ###
558 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove x509'}) {
559         &Header::showhttpheaders();
560         &Header::openpage($Lang::tr{'ipsec'}, 1, '');
561         &Header::openbigbox('100%', 'left', '', '');
562         &Header::openbox('100%', 'left', $Lang::tr{'are you sure'});
563         print <<END
564         <form method='post' action='$ENV{'SCRIPT_NAME'}'>
565                 <table width='100%'>
566                         <tr>
567                                 <td align='center'>
568                                         <input type='hidden' name='AREUSURE' value='yes' />
569                                         <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'}
570                                 </td>
571                         </tr><tr>
572                                 <td align='center'>
573                                         <input type='submit' name='ACTION' value='$Lang::tr{'remove x509'}' />
574                 <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></td>
575                         </tr>
576                 </table>
577         </form>
578 END
579 ;
580         &Header::closebox();
581         &Header::closebigbox();
582         &Header::closepage();
583         exit (0);
584
585 ###
586 ### Upload CA Certificate
587 ###
588 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload ca certificate'}) {
589         &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
590
591         if ($cgiparams{'CA_NAME'} !~ /^[a-zA-Z0-9]+$/) {
592                 $errormessage = $Lang::tr{'name must only contain characters'};
593                 goto UPLOADCA_ERROR;
594         }
595
596         if (length($cgiparams{'CA_NAME'}) >60) {
597                 $errormessage = $Lang::tr{'name too long'};
598                 goto VPNCONF_ERROR;
599         }
600
601         if ($cgiparams{'CA_NAME'} eq 'ca') {
602                 $errormessage = $Lang::tr{'name is invalid'};
603                 goto UPLOAD_CA_ERROR;
604         }
605
606         # Check if there is no other entry with this name
607         foreach my $key (keys %cahash) {
608                 if ($cahash{$key}[0] eq $cgiparams{'CA_NAME'}) {
609                         $errormessage = $Lang::tr{'a ca certificate with this name already exists'};
610                         goto UPLOADCA_ERROR;
611                 }
612         }
613
614         if (ref ($cgiparams{'FH'}) ne 'Fh') {
615                 $errormessage = $Lang::tr{'there was no file upload'};
616                 goto UPLOADCA_ERROR;
617         }
618         # Move uploaded ca to a temporary file
619         (my $fh, my $filename) = tempfile( );
620         if (copy ($cgiparams{'FH'}, $fh) != 1) {
621                 $errormessage = $!;
622                 goto UPLOADCA_ERROR;
623         }
624         my $temp = `/usr/bin/openssl x509 -text -in $filename`;
625         if ($temp !~ /CA:TRUE/i) {
626                 $errormessage = $Lang::tr{'not a valid ca certificate'};
627                 unlink ($filename);
628                 goto UPLOADCA_ERROR;
629         } else {
630                 move($filename, "${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem");
631                 if ($? ne 0) {
632                         $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
633                         unlink ($filename);
634                         goto UPLOADCA_ERROR;
635                 }
636         }
637
638         my $key = &General::findhasharraykey (\%cahash);
639         $cahash{$key}[0] = $cgiparams{'CA_NAME'};
640         $cahash{$key}[1] = &Header::cleanhtml(getsubjectfromcert ("${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem"));
641         &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
642
643         system('/usr/local/bin/ipsecctrl', 'R');
644         sleep $sleepDelay;
645
646         UPLOADCA_ERROR:
647
648 ###
649 ### Display ca certificate
650 ###
651 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show ca certificate'}) {
652         &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
653
654         if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem") {
655                 &Header::showhttpheaders();
656                 &Header::openpage($Lang::tr{'ipsec'}, 1, '');
657                 &Header::openbigbox('100%', 'left', '', '');
658                 &Header::openbox('100%', 'left', "$Lang::tr{'ca certificate'}:");
659                 my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
660                 $output = &Header::cleanhtml($output,"y");
661                 print "<pre>$output</pre>\n";
662                 &Header::closebox();
663                 print "<div align='center'><a href='/cgi-bin/vpnmain.cgi'>$Lang::tr{'back'}</a></div>";
664                 &Header::closebigbox();
665                 &Header::closepage();
666                 exit(0);
667         } else {
668                 $errormessage = $Lang::tr{'invalid key'};
669         }
670
671 ###
672 ### Export ca certificate to browser
673 ###
674 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download ca certificate'}) {
675         &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
676
677         if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
678                 print "Content-Type: application/force-download\n";
679                 print "Content-Type: application/octet-stream\r\n";
680                 print "Content-Disposition: attachment; filename=$cahash{$cgiparams{'KEY'}}[0]cert.pem\r\n\r\n";
681                 print `/usr/bin/openssl x509 -in ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
682                 exit(0);
683         } else {
684                 $errormessage = $Lang::tr{'invalid key'};
685         }
686
687 ###
688 ### Remove ca certificate (step 2)
689 ###
690 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'} && $cgiparams{'AREUSURE'} eq 'yes') {
691         &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
692         &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
693
694         if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
695                 foreach my $key (keys %confighash) {
696                         my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem ${General::swroot}/certs/$confighash{$key}[1]cert.pem`;
697                         if ($test =~ /: OK/) {
698                                 # Delete connection
699                                 system('/usr/local/bin/ipsecctrl', 'D', $key) if (&vpnenabled);
700                                 unlink ("${General::swroot}/certs/$confighash{$key}[1]cert.pem");
701                                 unlink ("${General::swroot}/certs/$confighash{$key}[1].p12");
702                                 delete $confighash{$key};
703                                 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
704                                 &writeipsecfiles();
705                         }
706                 }
707                 unlink ("${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
708                 delete $cahash{$cgiparams{'KEY'}};
709                 &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
710                 system('/usr/local/bin/ipsecctrl', 'R');
711                 sleep $sleepDelay;
712         } else {
713                 $errormessage = $Lang::tr{'invalid key'};
714         }
715 ###
716 ### Remove ca certificate (step 1)
717 ###
718 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'}) {
719         &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
720         &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
721
722         my $assignedcerts = 0;
723         if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
724                 foreach my $key (keys %confighash) {
725                         my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem ${General::swroot}/certs/$confighash{$key}[1]cert.pem`;
726                         if ($test =~ /: OK/) {
727                                 $assignedcerts++;
728                         }
729                 }
730                 if ($assignedcerts) {
731                         &Header::showhttpheaders();
732                         &Header::openpage($Lang::tr{'ipsec'}, 1, '');
733                         &Header::openbigbox('100%', 'left', '', '');
734                         &Header::openbox('100%', 'left', $Lang::tr{'are you sure'});
735                         print <<END
736                         <form method='post' action='$ENV{'SCRIPT_NAME'}'>
737                                 <table width='100%'>
738                                         <tr>
739                                                 <td align='center'>
740                                                         <input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />
741                         <input type='hidden' name='AREUSURE' value='yes' /></td>
742                                         </tr><tr>
743                                                 <td align='center'>
744                                                         <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>
745                                         </tr><tr>
746                                                 <td align='center'>
747                                                         <input type='submit' name='ACTION' value='$Lang::tr{'remove ca certificate'}' />
748                         <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></td>
749                                         </tr>
750                                 </table>
751                         </form>
752 END
753 ;
754                         &Header::closebox();
755                         &Header::closebigbox();
756                         &Header::closepage();
757                         exit (0);
758                 } else {
759                         unlink ("${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
760                         delete $cahash{$cgiparams{'KEY'}};
761                         &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
762                         system('/usr/local/bin/ipsecctrl', 'R');
763                         sleep $sleepDelay;
764                 }
765         } else {
766                 $errormessage = $Lang::tr{'invalid key'};
767         }
768
769 ###
770 ### Display root certificate
771 ###
772 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'} ||
773         $cgiparams{'ACTION'} eq $Lang::tr{'show host certificate'}) {
774         my $output;
775         &Header::showhttpheaders();
776         &Header::openpage($Lang::tr{'ipsec'}, 1, '');
777         &Header::openbigbox('100%', 'left', '', '');
778         if ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'}) {
779                 &Header::openbox('100%', 'left', "$Lang::tr{'root certificate'}:");
780                 $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ca/cacert.pem`;
781         } else {
782                 &Header::openbox('100%', 'left', "$Lang::tr{'host certificate'}:");
783                 $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/certs/hostcert.pem`;
784         }
785         $output = &Header::cleanhtml($output,"y");
786         print "<pre>$output</pre>\n";
787         &Header::closebox();
788         print "<div align='center'><a href='/cgi-bin/vpnmain.cgi'>$Lang::tr{'back'}</a></div>";
789         &Header::closebigbox();
790         &Header::closepage();
791         exit(0);
792
793 ###
794 ### Export root certificate to browser
795 ###
796 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download root certificate'}) {
797         if ( -f "${General::swroot}/ca/cacert.pem" ) {
798                 print "Content-Type: application/force-download\n";
799                 print "Content-Disposition: attachment; filename=cacert.pem\r\n\r\n";
800                 print `/usr/bin/openssl x509 -in ${General::swroot}/ca/cacert.pem`;
801                 exit(0);
802         }
803 ###
804 ### Export host certificate to browser
805 ###
806 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download host certificate'}) {
807         if ( -f "${General::swroot}/certs/hostcert.pem" ) {
808                 print "Content-Type: application/force-download\n";
809                 print "Content-Disposition: attachment; filename=hostcert.pem\r\n\r\n";
810                 print `/usr/bin/openssl x509 -in ${General::swroot}/certs/hostcert.pem`;
811                 exit(0);
812         }
813 ###
814 ### Form for generating/importing the caroot+host certificate
815 ###
816 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'generate root/host certificates'} ||
817         $cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
818
819         if (-f "${General::swroot}/ca/cacert.pem") {
820                 $errormessage = $Lang::tr{'valid root certificate already exists'};
821                 goto ROOTCERT_SKIP;
822         }
823
824         &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
825         # fill in initial values
826         if ($cgiparams{'ROOTCERT_HOSTNAME'} eq '') {
827                 if (-e "${General::swroot}/red/active" && open(IPADDR, "${General::swroot}/red/local-ipaddress")) {
828                         my $ipaddr = <IPADDR>;
829                         close IPADDR;
830                         chomp ($ipaddr);
831                         $cgiparams{'ROOTCERT_HOSTNAME'} = (gethostbyaddr(pack("C4", split(/\./, $ipaddr)), 2))[0];
832                         if ($cgiparams{'ROOTCERT_HOSTNAME'} eq '') {
833                                 $cgiparams{'ROOTCERT_HOSTNAME'} = $ipaddr;
834                         }
835                 }
836                 $cgiparams{'ROOTCERT_COUNTRY'} = $vpnsettings{'ROOTCERT_COUNTRY'} if (!$cgiparams{'ROOTCERT_COUNTRY'});
837         } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
838                 &General::log("ipsec", "Importing from p12...");
839
840                 if (ref ($cgiparams{'FH'}) ne 'Fh') {
841                         $errormessage = $Lang::tr{'there was no file upload'};
842                         goto ROOTCERT_ERROR;
843                 }
844
845                 # Move uploaded certificate request to a temporary file
846                 (my $fh, my $filename) = tempfile( );
847                 if (copy ($cgiparams{'FH'}, $fh) != 1) {
848                         $errormessage = $!;
849                         goto ROOTCERT_ERROR;
850                 }
851
852                 # Extract the CA certificate from the file
853                 &General::log("ipsec", "Extracting caroot from p12...");
854                 if (open(STDIN, "-|")) {
855                         my $opt = " pkcs12 -cacerts -nokeys";
856                         $opt .= " -in $filename";
857                         $opt .= " -out /tmp/newcacert";
858                         $errormessage = &callssl ($opt);
859                 } else { #child
860                         print "$cgiparams{'P12_PASS'}\n";
861                         exit (0);
862                 }
863
864                 # Extract the Host certificate from the file
865                 if (!$errormessage) {
866                         &General::log("ipsec", "Extracting host cert from p12...");
867                         if (open(STDIN, "-|")) {
868                                 my $opt = " pkcs12 -clcerts -nokeys";
869                                 $opt .= " -in $filename";
870                                 $opt .= " -out /tmp/newhostcert";
871                                 $errormessage = &callssl ($opt);
872                         } else { #child
873                                 print "$cgiparams{'P12_PASS'}\n";
874                                 exit (0);
875                         }
876                 }
877
878                 # Extract the Host key from the file
879                 if (!$errormessage) {
880                         &General::log("ipsec", "Extracting private key from p12...");
881                         if (open(STDIN, "-|")) {
882                                 my $opt = " pkcs12 -nocerts -nodes";
883                                 $opt .= " -in $filename";
884                                 $opt .= " -out /tmp/newhostkey";
885                                 $errormessage = &callssl ($opt);
886                         } else { #child
887                                 print "$cgiparams{'P12_PASS'}\n";
888                                 exit (0);
889                         }
890                 }
891
892                 if (!$errormessage) {
893                         &General::log("ipsec", "Moving cacert...");
894                         move("/tmp/newcacert", "${General::swroot}/ca/cacert.pem");
895                         $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
896                 }
897
898                 if (!$errormessage) {
899                         &General::log("ipsec", "Moving host cert...");
900                         move("/tmp/newhostcert", "${General::swroot}/certs/hostcert.pem");
901                         $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
902                 }
903
904                 if (!$errormessage) {
905                         &General::log("ipsec", "Moving private key...");
906                         move("/tmp/newhostkey", "${General::swroot}/certs/hostkey.pem");
907                         $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
908                 }
909
910                 #cleanup temp files
911                 unlink ($filename);
912                 unlink ('/tmp/newcacert');
913                 unlink ('/tmp/newhostcert');
914                 unlink ('/tmp/newhostkey');
915                 if ($errormessage) {
916                         unlink ("${General::swroot}/ca/cacert.pem");
917                         unlink ("${General::swroot}/certs/hostcert.pem");
918                         unlink ("${General::swroot}/certs/hostkey.pem");
919                         goto ROOTCERT_ERROR;
920                 }
921
922                 # Create empty CRL cannot be done because we don't have
923                 # the private key for this CAROOT
924                 # IPFire can only import certificates
925
926                 &General::log("ipsec", "p12 import completed!");
927                 &cleanssldatabase();
928                 goto ROOTCERT_SUCCESS;
929
930         } elsif ($cgiparams{'ROOTCERT_COUNTRY'} ne '') {
931
932                 # Validate input since the form was submitted
933                 if ($cgiparams{'ROOTCERT_ORGANIZATION'} eq ''){
934                         $errormessage = $Lang::tr{'organization cant be empty'};
935                         goto ROOTCERT_ERROR;
936                 }
937                 if (length($cgiparams{'ROOTCERT_ORGANIZATION'}) >60) {
938                         $errormessage = $Lang::tr{'organization too long'};
939                         goto ROOTCERT_ERROR;
940                 }
941                 if ($cgiparams{'ROOTCERT_ORGANIZATION'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
942                         $errormessage = $Lang::tr{'invalid input for organization'};
943                         goto ROOTCERT_ERROR;
944                 }
945                 if ($cgiparams{'ROOTCERT_HOSTNAME'} eq ''){
946                         $errormessage = $Lang::tr{'hostname cant be empty'};
947                         goto ROOTCERT_ERROR;
948                 }
949                 unless (&General::validfqdn($cgiparams{'ROOTCERT_HOSTNAME'}) || &General::validip($cgiparams{'ROOTCERT_HOSTNAME'})) {
950                         $errormessage = $Lang::tr{'invalid input for hostname'};
951                         goto ROOTCERT_ERROR;
952                 }
953                 if ($cgiparams{'ROOTCERT_EMAIL'} ne '' && (! &General::validemail($cgiparams{'ROOTCERT_EMAIL'}))) {
954                         $errormessage = $Lang::tr{'invalid input for e-mail address'};
955                         goto ROOTCERT_ERROR;
956                 }
957                 if (length($cgiparams{'ROOTCERT_EMAIL'}) > 40) {
958                         $errormessage = $Lang::tr{'e-mail address too long'};
959                         goto ROOTCERT_ERROR;
960                 }
961                 if ($cgiparams{'ROOTCERT_OU'} ne '' && $cgiparams{'ROOTCERT_OU'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
962                         $errormessage = $Lang::tr{'invalid input for department'};
963                         goto ROOTCERT_ERROR;
964                 }
965                 if ($cgiparams{'ROOTCERT_CITY'} ne '' && $cgiparams{'ROOTCERT_CITY'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
966                         $errormessage = $Lang::tr{'invalid input for city'};
967                         goto ROOTCERT_ERROR;
968                 }
969                 if ($cgiparams{'ROOTCERT_STATE'} ne '' && $cgiparams{'ROOTCERT_STATE'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
970                         $errormessage = $Lang::tr{'invalid input for state or province'};
971                         goto ROOTCERT_ERROR;
972                 }
973                 if ($cgiparams{'ROOTCERT_COUNTRY'} !~ /^[A-Z]*$/) {
974                         $errormessage = $Lang::tr{'invalid input for country'};
975                         goto ROOTCERT_ERROR;
976                 }
977                 #the exact syntax is a list comma separated of
978                 #       email:any-validemail
979                 #       URI: a uniform resource indicator
980                 #       DNS: a DNS domain name
981                 #       RID: a registered OBJECT IDENTIFIER
982                 #       IP: an IP address
983                 # example: email:franck@foo.com,IP:10.0.0.10,DNS:franck.foo.com
984
985                 if ($cgiparams{'SUBJECTALTNAME'} ne '' && $cgiparams{'SUBJECTALTNAME'} !~ /^(email|URI|DNS|RID|IP):[a-zA-Z0-9 :\/,\.\-_@]*$/) {
986                         $errormessage = $Lang::tr{'vpn altname syntax'};
987                         goto VPNCONF_ERROR;
988                 }
989
990                 # Copy the cgisettings to vpnsettings and save the configfile
991                 $vpnsettings{'ROOTCERT_ORGANIZATION'}   = $cgiparams{'ROOTCERT_ORGANIZATION'};
992                 $vpnsettings{'ROOTCERT_HOSTNAME'}               = $cgiparams{'ROOTCERT_HOSTNAME'};
993                 $vpnsettings{'ROOTCERT_EMAIL'}                  = $cgiparams{'ROOTCERT_EMAIL'};
994                 $vpnsettings{'ROOTCERT_OU'}                             = $cgiparams{'ROOTCERT_OU'};
995                 $vpnsettings{'ROOTCERT_CITY'}                   = $cgiparams{'ROOTCERT_CITY'};
996                 $vpnsettings{'ROOTCERT_STATE'}                  = $cgiparams{'ROOTCERT_STATE'};
997                 $vpnsettings{'ROOTCERT_COUNTRY'}                = $cgiparams{'ROOTCERT_COUNTRY'};
998                 &General::writehash("${General::swroot}/vpn/settings", \%vpnsettings);
999
1000                 # Replace empty strings with a .
1001                 (my $ou = $cgiparams{'ROOTCERT_OU'}) =~ s/^\s*$/\./;
1002                 (my $city = $cgiparams{'ROOTCERT_CITY'}) =~ s/^\s*$/\./;
1003                 (my $state = $cgiparams{'ROOTCERT_STATE'}) =~ s/^\s*$/\./;
1004
1005                 # Create the CA certificate
1006                 if (!$errormessage) {
1007                         &General::log("ipsec", "Creating cacert...");
1008                         if (open(STDIN, "-|")) {
1009                                 my $opt = " req -x509 -sha256 -nodes";
1010                                 $opt .= " -days 999999";
1011                                 $opt .= " -newkey rsa:4096";
1012                                 $opt .= " -keyout ${General::swroot}/private/cakey.pem";
1013                                 $opt .= " -out ${General::swroot}/ca/cacert.pem";
1014
1015                                 $errormessage = &callssl ($opt);
1016                         } else { #child
1017                                 print "$cgiparams{'ROOTCERT_COUNTRY'}\n";
1018                                 print "$state\n";
1019                                 print "$city\n";
1020                                 print "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
1021                                 print "$ou\n";
1022                                 print "$cgiparams{'ROOTCERT_ORGANIZATION'} CA\n";
1023                                 print "$cgiparams{'ROOTCERT_EMAIL'}\n";
1024                                 exit (0);
1025                         }
1026                 }
1027
1028                 # Create the Host certificate request
1029                 if (!$errormessage) {
1030                         &General::log("ipsec", "Creating host cert...");
1031                         if (open(STDIN, "-|")) {
1032                                 my $opt = " req -sha256 -nodes";
1033                                 $opt .= " -newkey rsa:2048";
1034                                 $opt .= " -keyout ${General::swroot}/certs/hostkey.pem";
1035                                 $opt .= " -out ${General::swroot}/certs/hostreq.pem";
1036                                 $errormessage = &callssl ($opt);
1037                         } else { #child
1038                                 print "$cgiparams{'ROOTCERT_COUNTRY'}\n";
1039                                 print "$state\n";
1040                                 print "$city\n";
1041                                 print "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
1042                                 print "$ou\n";
1043                                 print "$cgiparams{'ROOTCERT_HOSTNAME'}\n";
1044                                 print "$cgiparams{'ROOTCERT_EMAIL'}\n";
1045                                 print ".\n";
1046                                 print ".\n";
1047                                 exit (0);
1048                         }
1049                 }
1050
1051                 # Sign the host certificate request
1052                 if (!$errormessage) {
1053                         &General::log("ipsec", "Self signing host cert...");
1054
1055                         #No easy way for specifying the contain of subjectAltName without writing a config file...
1056                         my ($fh, $v3extname) = tempfile ('/tmp/XXXXXXXX');
1057                         print $fh <<END
1058                         basicConstraints=CA:FALSE
1059                         nsComment="OpenSSL Generated Certificate"
1060                         subjectKeyIdentifier=hash
1061                         authorityKeyIdentifier=keyid,issuer:always
1062                         extendedKeyUsage = serverAuth
1063 END
1064 ;
1065                         print $fh "subjectAltName=$cgiparams{'SUBJECTALTNAME'}" if ($cgiparams{'SUBJECTALTNAME'});
1066                         close ($fh);
1067
1068                         my $opt = " ca -md sha256 -days 999999";
1069                         $opt .= " -batch -notext";
1070                         $opt .= " -in ${General::swroot}/certs/hostreq.pem";
1071                         $opt .= " -out ${General::swroot}/certs/hostcert.pem";
1072                         $opt .= " -extfile $v3extname";
1073                         $errormessage = &callssl ($opt);
1074                         unlink ("${General::swroot}/certs/hostreq.pem"); #no more needed
1075                         unlink ($v3extname);
1076                 }
1077
1078                 # Create an empty CRL
1079                 if (!$errormessage) {
1080                         &General::log("ipsec", "Creating emptycrl...");
1081                         my $opt = " ca -gencrl";
1082                         $opt .= " -out ${General::swroot}/crls/cacrl.pem";
1083                         $errormessage = &callssl ($opt);
1084                 }
1085
1086                 # Successfully build CA / CERT!
1087                 if (!$errormessage) {
1088                         &cleanssldatabase();
1089                         goto ROOTCERT_SUCCESS;
1090                 }
1091
1092                 #Cleanup
1093                 unlink ("${General::swroot}/ca/cacert.pem");
1094                 unlink ("${General::swroot}/certs/hostkey.pem");
1095                 unlink ("${General::swroot}/certs/hostcert.pem");
1096                 unlink ("${General::swroot}/crls/cacrl.pem");
1097                 &cleanssldatabase();
1098         }
1099
1100         ROOTCERT_ERROR:
1101         &Header::showhttpheaders();
1102         &Header::openpage($Lang::tr{'ipsec'}, 1, '');
1103         &Header::openbigbox('100%', 'left', '', $errormessage);
1104         if ($errormessage) {
1105                 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
1106                 print "<class name='base'>$errormessage";
1107                 print "&nbsp;</class>";
1108                 &Header::closebox();
1109         }
1110         &Header::openbox('100%', 'left', "$Lang::tr{'generate root/host certificates'}:");
1111         print <<END
1112         <form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'>
1113                 <table width='100%' border='0' cellspacing='1' cellpadding='0'>
1114         <tr><td width='40%' class='base'>$Lang::tr{'organization name'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
1115                 <td width='60%' class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_ORGANIZATION' value='$cgiparams{'ROOTCERT_ORGANIZATION'}' size='32' /></td></tr>
1116         <tr><td class='base'>$Lang::tr{'ipfires hostname'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
1117                 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_HOSTNAME' value='$cgiparams{'ROOTCERT_HOSTNAME'}' size='32' /></td></tr>
1118         <tr><td class='base'>$Lang::tr{'your e-mail'}:</td>
1119                 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_EMAIL' value='$cgiparams{'ROOTCERT_EMAIL'}' size='32' /></td></tr>
1120         <tr><td class='base'>$Lang::tr{'your department'}:</td>
1121                 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_OU' value='$cgiparams{'ROOTCERT_OU'}' size='32' /></td></tr>
1122         <tr><td class='base'>$Lang::tr{'city'}:</td>
1123                 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_CITY' value='$cgiparams{'ROOTCERT_CITY'}' size='32' /></td></tr>
1124         <tr><td class='base'>$Lang::tr{'state or province'}:</td>
1125                 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_STATE' value='$cgiparams{'ROOTCERT_STATE'}' size='32' /></td></tr>
1126         <tr><td class='base'>$Lang::tr{'country'}:</td>
1127                 <td class='base'><select name='ROOTCERT_COUNTRY'>
1128 END
1129 ;
1130         foreach my $country (sort keys %{Countries::countries}) {
1131                 print "<option value='$Countries::countries{$country}'";
1132                 if ( $Countries::countries{$country} eq $cgiparams{'ROOTCERT_COUNTRY'} ) {
1133                         print " selected='selected'";
1134                 }
1135                 print ">$country</option>";
1136         }
1137         print <<END
1138                 </select></td></tr>
1139         <tr><td class='base'>$Lang::tr{'vpn subjectaltname'} (subjectAltName=email:*,URI:*,DNS:*,RID:*)</td>
1140         <td class='base' nowrap='nowrap'><input type='text' name='SUBJECTALTNAME' value='$cgiparams{'SUBJECTALTNAME'}' size='32' /></td></tr>
1141         <tr><td>&nbsp;</td>
1142                 <td><br /><input type='submit' name='ACTION' value='$Lang::tr{'generate root/host certificates'}' /><br /><br /></td></tr>
1143         <tr><td class='base' colspan='2' align='left'>
1144                 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>:
1145                 $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'}
1146         </td></tr>
1147         <tr><td colspan='2'><hr></td></tr>
1148         <tr><td class='base' nowrap='nowrap'>$Lang::tr{'upload p12 file'}:</td>
1149                 <td nowrap='nowrap'><input type='file' name='FH' size='32' /></td></tr>
1150         <tr><td class='base'>$Lang::tr{'pkcs12 file password'}:</td>
1151                 <td class='base' nowrap='nowrap'><input type='password' name='P12_PASS' value='$cgiparams{'P12_PASS'}' size='32' /></td></tr>
1152         <tr><td>&nbsp;</td>
1153                 <td><input type='submit' name='ACTION' value='$Lang::tr{'upload p12 file'}' /></td></tr>
1154         <tr><td class='base' colspan='2' align='left'>
1155                 <img src='/blob.gif' alt='*' />&nbsp;$Lang::tr{'required field'}</td></tr>
1156         </table></form>
1157 END
1158 ;
1159         &Header::closebox();
1160         &Header::closebigbox();
1161         &Header::closepage();
1162         exit(0);
1163
1164         ROOTCERT_SUCCESS:
1165         if (&vpnenabled) {
1166                 system('/usr/local/bin/ipsecctrl', 'S');
1167                 sleep $sleepDelay;
1168         }
1169         ROOTCERT_SKIP:
1170 ###
1171 ### Export PKCS12 file to browser
1172 ###
1173 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download pkcs12 file'}) {
1174         &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1175         print "Content-Type: application/force-download\n";
1176         print "Content-Disposition: attachment; filename=" . $confighash{$cgiparams{'KEY'}}[1] . ".p12\r\n";
1177         print "Content-Type: application/octet-stream\r\n\r\n";
1178         print `/bin/cat ${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1].p12`;
1179         exit (0);
1180
1181 ###
1182 ### Display certificate
1183 ###
1184 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show certificate'}) {
1185         &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1186
1187         if ( -f "${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem") {
1188                 &Header::showhttpheaders();
1189                 &Header::openpage($Lang::tr{'ipsec'}, 1, '');
1190                 &Header::openbigbox('100%', 'left', '', '');
1191                 &Header::openbox('100%', 'left', "$Lang::tr{'cert'}:");
1192                 my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
1193                 $output = &Header::cleanhtml($output,"y");
1194                 print "<pre>$output</pre>\n";
1195                 &Header::closebox();
1196                 print "<div align='center'><a href='/cgi-bin/vpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1197                 &Header::closebigbox();
1198                 &Header::closepage();
1199                 exit(0);
1200         }
1201
1202 ###
1203 ### Export Certificate to browser
1204 ###
1205 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download certificate'}) {
1206         &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1207
1208         if ( -f "${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem") {
1209                 print "Content-Type: application/force-download\n";
1210                 print "Content-Disposition: attachment; filename=" . $confighash{$cgiparams{'KEY'}}[1] . "cert.pem\n\n";
1211                 print `/bin/cat ${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
1212                 exit (0);
1213         }
1214
1215 ###
1216 ### Enable/Disable connection
1217 ###
1218 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
1219
1220         &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
1221         &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1222
1223         if ($confighash{$cgiparams{'KEY'}}) {
1224                 if ($confighash{$cgiparams{'KEY'}}[0] eq 'off') {
1225                         $confighash{$cgiparams{'KEY'}}[0] = 'on';
1226                         &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
1227                         &writeipsecfiles();
1228                         system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'}) if (&vpnenabled);
1229                 } else {
1230                         system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'}) if (&vpnenabled);
1231                         $confighash{$cgiparams{'KEY'}}[0] = 'off';
1232                         &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
1233                         &writeipsecfiles();
1234                 }
1235                 sleep $sleepDelay;
1236         } else {
1237                 $errormessage = $Lang::tr{'invalid key'};
1238         }
1239
1240 ###
1241 ### Restart connection
1242 ###
1243 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'restart'}) {
1244         &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
1245         &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1246
1247         if ($confighash{$cgiparams{'KEY'}}) {
1248                 if (&vpnenabled) {
1249                         system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'});
1250                         sleep $sleepDelay;
1251                 }
1252         } else {
1253                 $errormessage = $Lang::tr{'invalid key'};
1254         }
1255
1256 ###
1257 ### Remove connection
1258 ###
1259 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove'}) {
1260         &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
1261         &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1262
1263         if ($confighash{$cgiparams{'KEY'}}) {
1264                 system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'}) if (&vpnenabled);
1265                 unlink ("${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem");
1266                 unlink ("${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1].p12");
1267                 delete $confighash{$cgiparams{'KEY'}};
1268                 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
1269                 &writeipsecfiles();
1270         } else {
1271                 $errormessage = $Lang::tr{'invalid key'};
1272         }
1273         &General::firewall_reload();
1274 ###
1275 ### Choose between adding a host-net or net-net connection
1276 ###
1277 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'add'} && $cgiparams{'TYPE'} eq '') {
1278         &Header::showhttpheaders();
1279         &Header::openpage($Lang::tr{'ipsec'}, 1, '');
1280         &Header::openbigbox('100%', 'left', '', '');
1281         &Header::openbox('100%', 'left', $Lang::tr{'connection type'});
1282         print <<END
1283                 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1284                 <b>$Lang::tr{'connection type'}:</b><br />
1285                 <table>
1286                 <tr><td><input type='radio' name='TYPE' value='host' checked='checked' /></td>
1287                 <td class='base'>$Lang::tr{'host to net vpn'}</td>
1288                 </tr><tr>
1289                 <td><input type='radio' name='TYPE' value='net' /></td>
1290                 <td class='base'>$Lang::tr{'net to net vpn'}</td>
1291                 </tr><tr>
1292                 <td align='center' colspan='2'><input type='submit' name='ACTION' value='$Lang::tr{'add'}' /></td>
1293                 </tr>
1294                 </table></form>
1295 END
1296 ;
1297         &Header::closebox();
1298         &Header::closebigbox();
1299         &Header::closepage();
1300         exit (0);
1301 ###
1302 ### Adding/Editing/Saving a connection
1303 ###
1304 } elsif (($cgiparams{'ACTION'} eq $Lang::tr{'add'}) ||
1305         ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) ||
1306         ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'ADVANCED'} eq '')) {
1307
1308         &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
1309         &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
1310         &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1311
1312         if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) {
1313                 if (! $confighash{$cgiparams{'KEY'}}[0]) {
1314                         $errormessage = $Lang::tr{'invalid key'};
1315                         goto VPNCONF_END;
1316                 }
1317                 $cgiparams{'ENABLED'}                   = $confighash{$cgiparams{'KEY'}}[0];
1318                 $cgiparams{'NAME'}                              = $confighash{$cgiparams{'KEY'}}[1];
1319                 $cgiparams{'TYPE'}                              = $confighash{$cgiparams{'KEY'}}[3];
1320                 $cgiparams{'AUTH'}                              = $confighash{$cgiparams{'KEY'}}[4];
1321                 $cgiparams{'PSK'}                               = $confighash{$cgiparams{'KEY'}}[5];
1322                 #$cgiparams{'free'}                             = $confighash{$cgiparams{'KEY'}}[6];
1323                 $cgiparams{'LOCAL_ID'}                  = $confighash{$cgiparams{'KEY'}}[7];
1324                 my @local_subnets = split(",", $confighash{$cgiparams{'KEY'}}[8]);
1325                 $cgiparams{'LOCAL_SUBNET'}              = join(/\|/, @local_subnets);
1326                 $cgiparams{'REMOTE_ID'}                 = $confighash{$cgiparams{'KEY'}}[9];
1327                 $cgiparams{'REMOTE'}                    = $confighash{$cgiparams{'KEY'}}[10];
1328                 my @remote_subnets = split(",", $confighash{$cgiparams{'KEY'}}[11]);
1329                 $cgiparams{'REMOTE_SUBNET'}             = join(/\|/, @remote_subnets);
1330                 $cgiparams{'REMARK'}                    = $confighash{$cgiparams{'KEY'}}[25];
1331                 $cgiparams{'DPD_ACTION'}                = $confighash{$cgiparams{'KEY'}}[27];
1332                 $cgiparams{'IKE_VERSION'}               = $confighash{$cgiparams{'KEY'}}[29];
1333                 $cgiparams{'IKE_ENCRYPTION'}    = $confighash{$cgiparams{'KEY'}}[18];
1334                 $cgiparams{'IKE_INTEGRITY'}             = $confighash{$cgiparams{'KEY'}}[19];
1335                 $cgiparams{'IKE_GROUPTYPE'}             = $confighash{$cgiparams{'KEY'}}[20];
1336                 $cgiparams{'IKE_LIFETIME'}              = $confighash{$cgiparams{'KEY'}}[16];
1337                 $cgiparams{'ESP_ENCRYPTION'}    = $confighash{$cgiparams{'KEY'}}[21];
1338                 $cgiparams{'ESP_INTEGRITY'}             = $confighash{$cgiparams{'KEY'}}[22];
1339                 $cgiparams{'ESP_GROUPTYPE'}             = $confighash{$cgiparams{'KEY'}}[23];
1340                 if ($cgiparams{'ESP_GROUPTYPE'} eq "") {
1341                         $cgiparams{'ESP_GROUPTYPE'}     = $cgiparams{'IKE_GROUPTYPE'};
1342                 }
1343                 $cgiparams{'ESP_KEYLIFE'}               = $confighash{$cgiparams{'KEY'}}[17];
1344                 $cgiparams{'COMPRESSION'}               = $confighash{$cgiparams{'KEY'}}[13];
1345                 $cgiparams{'ONLY_PROPOSED'}             = $confighash{$cgiparams{'KEY'}}[24];
1346                 $cgiparams{'PFS'}                               = $confighash{$cgiparams{'KEY'}}[28];
1347                 $cgiparams{'DPD_TIMEOUT'}               = $confighash{$cgiparams{'KEY'}}[30];
1348                 $cgiparams{'DPD_DELAY'}                 = $confighash{$cgiparams{'KEY'}}[31];
1349                 $cgiparams{'FORCE_MOBIKE'}              = $confighash{$cgiparams{'KEY'}}[32];
1350                 $cgiparams{'INACTIVITY_TIMEOUT'}        = $confighash{$cgiparams{'KEY'}}[34];
1351                 $cgiparams{'MODE'}                      = $confighash{$cgiparams{'KEY'}}[35];
1352                 $cgiparams{'INTERFACE_MODE'}            = $confighash{$cgiparams{'KEY'}}[36];
1353                 $cgiparams{'INTERFACE_ADDRESS'}         = $confighash{$cgiparams{'KEY'}}[37];
1354                 $cgiparams{'INTERFACE_MTU'}             = $confighash{$cgiparams{'KEY'}}[38];
1355
1356                 if (!$cgiparams{'DPD_DELAY'}) {
1357                         $cgiparams{'DPD_DELAY'} = 30;
1358                 }
1359
1360                 if (!$cgiparams{'DPD_TIMEOUT'}) {
1361                         $cgiparams{'DPD_TIMEOUT'} = 120;
1362                 }
1363
1364                 if ($cgiparams{'INACTIVITY_TIMEOUT'} eq "") {
1365                         $cgiparams{'INACTIVITY_TIMEOUT'} = 900;
1366                 }
1367
1368                 if ($cgiparams{'MODE'} eq "") {
1369                         $cgiparams{'MODE'} = "tunnel";
1370                 }
1371
1372         } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
1373                 $cgiparams{'REMARK'} = &Header::cleanhtml($cgiparams{'REMARK'});
1374                 if ($cgiparams{'TYPE'} !~ /^(host|net)$/) {
1375                         $errormessage = $Lang::tr{'connection type is invalid'};
1376                         goto VPNCONF_ERROR;
1377                 }
1378
1379                 if ($cgiparams{'NAME'} !~ /^[a-zA-Z0-9]+$/) {
1380                         $errormessage = $Lang::tr{'name must only contain characters'};
1381                         goto VPNCONF_ERROR;
1382                 }
1383
1384                 if ($cgiparams{'NAME'} =~ /^(host|01|block|private|clear|packetdefault)$/) {
1385                         $errormessage = $Lang::tr{'name is invalid'};
1386                         goto VPNCONF_ERROR;
1387                 }
1388
1389                 if (length($cgiparams{'NAME'}) >60) {
1390                         $errormessage = $Lang::tr{'name too long'};
1391                         goto VPNCONF_ERROR;
1392                 }
1393
1394                 # Check if there is no other entry with this name
1395                 if (! $cgiparams{'KEY'}) { #only for add
1396                         foreach my $key (keys %confighash) {
1397                                 if ($confighash{$key}[1] eq $cgiparams{'NAME'}) {
1398                                         $errormessage = $Lang::tr{'a connection with this name already exists'};
1399                                         goto VPNCONF_ERROR;
1400                                 }
1401                         }
1402                 }
1403
1404                 if (($cgiparams{'TYPE'} eq 'net') && (! $cgiparams{'REMOTE'})) {
1405                         $errormessage = $Lang::tr{'invalid input for remote host/ip'};
1406                         goto VPNCONF_ERROR;
1407                 }
1408
1409                 if ($cgiparams{'REMOTE'}) {
1410                         if (($cgiparams{'REMOTE'} ne '%any') && (! &General::validip($cgiparams{'REMOTE'}))) {
1411                                 if (! &General::validfqdn ($cgiparams{'REMOTE'})) {
1412                                         $errormessage = $Lang::tr{'invalid input for remote host/ip'};
1413                                         goto VPNCONF_ERROR;
1414                                 } else {
1415                                         if (&valid_dns_host($cgiparams{'REMOTE'})) {
1416                                                 $warnmessage = "$Lang::tr{'check vpn lr'} $cgiparams{'REMOTE'}. $Lang::tr{'dns check failed'}";
1417                                         }
1418                                 }
1419                         }
1420                 }
1421
1422                 my @local_subnets = split(",", $cgiparams{'LOCAL_SUBNET'});
1423                 foreach my $subnet (@local_subnets) {
1424                         unless (&Network::check_subnet($subnet)) {
1425                                 $errormessage = $Lang::tr{'local subnet is invalid'};
1426                                 goto VPNCONF_ERROR;
1427                         }
1428                 }
1429
1430                 # Allow only one roadwarrior/psk without remote IP-address
1431                 if ($cgiparams{'REMOTE'} eq '' && $cgiparams{'AUTH'} eq 'psk') {
1432                         foreach my $key (keys %confighash) {
1433                                 if ( ($cgiparams{'KEY'} ne $key) &&
1434                                         ($confighash{$key}[4] eq 'psk') &&
1435                                         ($confighash{$key}[10] eq '') ) {
1436                                         $errormessage = $Lang::tr{'you can only define one roadwarrior connection when using pre-shared key authentication'};
1437                                         goto VPNCONF_ERROR;
1438                                 }
1439                         }
1440                 }
1441
1442                 if ($cgiparams{'TYPE'} eq 'net') {
1443                         my @remote_subnets = split(",", $cgiparams{'REMOTE_SUBNET'});
1444                         foreach my $subnet (@remote_subnets) {
1445                                 unless (&Network::check_subnet($subnet)) {
1446                                         $errormessage = $Lang::tr{'remote subnet is invalid'};
1447                                         goto VPNCONF_ERROR;
1448                                 }
1449                         }
1450                 }
1451
1452                 if ($cgiparams{'ENABLED'} !~ /^(on|off)$/) {
1453                         $errormessage = $Lang::tr{'invalid input'};
1454                         goto VPNCONF_ERROR;
1455                 }
1456                 if ($cgiparams{'EDIT_ADVANCED'} !~ /^(on|off)$/) {
1457                         $errormessage = $Lang::tr{'invalid input'};
1458                         goto VPNCONF_ERROR;
1459                 }
1460
1461                 # Allow nothing or a string (DN,FDQN,) beginning with @
1462                 # with no comma but slashes between RID eg @O=FR/C=Paris/OU=myhome/CN=franck
1463                 if ( ($cgiparams{'LOCAL_ID'} !~ /^(|[\w.-]*@[\w. =*\/-]+|\d+\.\d+\.\d+\.\d+)$/) ||
1464                         ($cgiparams{'REMOTE_ID'} !~ /^(|[\w.-]*@[\w. =*\/-]+|\d+\.\d+\.\d+\.\d+)$/) ||
1465                         (($cgiparams{'REMOTE_ID'} eq $cgiparams{'LOCAL_ID'}) && ($cgiparams{'LOCAL_ID'} ne ''))
1466                 ) {
1467                         $errormessage = $Lang::tr{'invalid local-remote id'} . '<br />' .
1468                         'DER_ASN1_DN: @c=FR/ou=Paris/ou=Home/cn=*<br />' .
1469                         'FQDN: @ipfire.org<br />' .
1470                         'USER_FQDN: info@ipfire.org<br />' .
1471                         'IPV4_ADDR: 123.123.123.123';
1472                         goto VPNCONF_ERROR;
1473                 }
1474                 # If Auth is DN, verify existance of Remote ID.
1475                 if ( $cgiparams{'REMOTE_ID'} eq '' && (
1476                         $cgiparams{'AUTH'} eq 'auth-dn'|| # while creation
1477                         $confighash{$cgiparams{'KEY'}}[2] eq '%auth-dn')){ # while editing
1478                                 $errormessage = $Lang::tr{'vpn missing remote id'};
1479                                 goto VPNCONF_ERROR;
1480                 }
1481
1482                 if ($cgiparams{'TYPE'} eq 'net'){
1483                         $warnmessage=&General::checksubnets('',$cgiparams{'REMOTE_SUBNET'},'ipsec');
1484                         if ($warnmessage ne ''){
1485                                 $warnmessage=$Lang::tr{'remote subnet'}." ($cgiparams{'REMOTE_SUBNET'}) <br>".$warnmessage;
1486                         }
1487                 }
1488
1489                 if ($cgiparams{'AUTH'} eq 'psk') {
1490                         if (! length($cgiparams{'PSK'}) ) {
1491                                 $errormessage = $Lang::tr{'pre-shared key is too short'};
1492                                 goto VPNCONF_ERROR;
1493                         }
1494                         if ($cgiparams{'PSK'} =~ /'/) {
1495                                 $cgiparams{'PSK'} =~ tr/'/ /;
1496                                 $errormessage = $Lang::tr{'invalid characters found in pre-shared key'};
1497                                 goto VPNCONF_ERROR;
1498                         }
1499         } elsif ($cgiparams{'AUTH'} eq 'certreq') {
1500                 if ($cgiparams{'KEY'}) {
1501                         $errormessage = $Lang::tr{'cant change certificates'};
1502                         goto VPNCONF_ERROR;
1503                 }
1504                 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1505                         $errormessage = $Lang::tr{'there was no file upload'};
1506                         goto VPNCONF_ERROR;
1507                 }
1508
1509                 # Move uploaded certificate request to a temporary file
1510                 (my $fh, my $filename) = tempfile( );
1511                 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1512                         $errormessage = $!;
1513                         goto VPNCONF_ERROR;
1514                 }
1515
1516                 # Sign the certificate request
1517                 &General::log("ipsec", "Signing your cert $cgiparams{'NAME'}...");
1518                 my $opt = " ca -md sha256 -days 999999";
1519                 $opt .= " -batch -notext";
1520                 $opt .= " -in $filename";
1521                 $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}cert.pem";
1522
1523                 if ( $errormessage = &callssl ($opt) ) {
1524                         unlink ($filename);
1525                         unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1526                         &cleanssldatabase();
1527                         goto VPNCONF_ERROR;
1528                 } else {
1529                         unlink ($filename);
1530                         &cleanssldatabase();
1531                 }
1532
1533                 $cgiparams{'CERT_NAME'} = getCNfromcert ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1534                 if ($cgiparams{'CERT_NAME'} eq '') {
1535                         $errormessage = $Lang::tr{'could not retrieve common name from certificate'};
1536                         goto VPNCONF_ERROR;
1537                 }
1538         } elsif ($cgiparams{'AUTH'} eq 'pkcs12') {
1539                 &General::log("ipsec", "Importing from p12...");
1540
1541                 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1542                         $errormessage = $Lang::tr{'there was no file upload'};
1543                         goto ROOTCERT_ERROR;
1544                 }
1545
1546                 # Move uploaded certificate request to a temporary file
1547                 (my $fh, my $filename) = tempfile( );
1548                 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1549                         $errormessage = $!;
1550                         goto ROOTCERT_ERROR;
1551                 }
1552
1553                 # Extract the CA certificate from the file
1554                 &General::log("ipsec", "Extracting caroot from p12...");
1555                 if (open(STDIN, "-|")) {
1556                         my $opt = " pkcs12 -cacerts -nokeys";
1557                         $opt .= " -in $filename";
1558                         $opt .= " -out /tmp/newcacert";
1559                         $errormessage = &callssl ($opt);
1560                 } else { #child
1561                         print "$cgiparams{'P12_PASS'}\n";
1562                         exit (0);
1563                 }
1564
1565                 # Extract the Host certificate from the file
1566                 if (!$errormessage) {
1567                         &General::log("ipsec", "Extracting host cert from p12...");
1568                         if (open(STDIN, "-|")) {
1569                                 my $opt = " pkcs12 -clcerts -nokeys";
1570                                 $opt .= " -in $filename";
1571                                 $opt .= " -out /tmp/newhostcert";
1572                                 $errormessage = &callssl ($opt);
1573                         } else { #child
1574                                 print "$cgiparams{'P12_PASS'}\n";
1575                                 exit (0);
1576                         }
1577                 }
1578
1579                 if (!$errormessage) {
1580                         &General::log("ipsec", "Moving cacert...");
1581                         #If CA have new subject, add it to our list of CA
1582                         my $casubject = &Header::cleanhtml(getsubjectfromcert ('/tmp/newcacert'));
1583                         my @names;
1584                         foreach my $x (keys %cahash) {
1585                                 $casubject='' if ($cahash{$x}[1] eq $casubject);
1586                                 unshift (@names,$cahash{$x}[0]);
1587                         }
1588                         if ($casubject) { # a new one!
1589                                 my $temp = `/usr/bin/openssl x509 -text -in /tmp/newcacert`;
1590                                 if ($temp !~ /CA:TRUE/i) {
1591                                         $errormessage = $Lang::tr{'not a valid ca certificate'};
1592                                 } else {
1593                                         #compute a name for it
1594                                         my $idx=0;
1595                                         while (grep(/Imported-$idx/, @names) ) {$idx++};
1596                                         $cgiparams{'CA_NAME'}="Imported-$idx";
1597                                         $cgiparams{'CERT_NAME'}=&Header::cleanhtml(getCNfromcert ('/tmp/newhostcert'));
1598                                         move("/tmp/newcacert", "${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem");
1599                                         $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
1600                                         if (!$errormessage) {
1601                                                 my $key = &General::findhasharraykey (\%cahash);
1602                                                 $cahash{$key}[0] = $cgiparams{'CA_NAME'};
1603                                                 $cahash{$key}[1] = $casubject;
1604                                                 &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
1605                                                 system('/usr/local/bin/ipsecctrl', 'R');
1606                                         }
1607                                 }
1608                         }
1609                 }
1610                 if (!$errormessage) {
1611                         &General::log("ipsec", "Moving host cert...");
1612                         move("/tmp/newhostcert", "${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1613                         $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
1614                 }
1615
1616                 #cleanup temp files
1617                 unlink ($filename);
1618                 unlink ('/tmp/newcacert');
1619                 unlink ('/tmp/newhostcert');
1620                 if ($errormessage) {
1621                         unlink ("${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem");
1622                         unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1623                         goto VPNCONF_ERROR;
1624                 }
1625                 &General::log("ipsec", "p12 import completed!");
1626         } elsif ($cgiparams{'AUTH'} eq 'certfile') {
1627                 if ($cgiparams{'KEY'}) {
1628                         $errormessage = $Lang::tr{'cant change certificates'};
1629                         goto VPNCONF_ERROR;
1630                 }
1631                 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1632                         $errormessage = $Lang::tr{'there was no file upload'};
1633                         goto VPNCONF_ERROR;
1634                 }
1635                 # Move uploaded certificate to a temporary file
1636                 (my $fh, my $filename) = tempfile( );
1637                 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1638                         $errormessage = $!;
1639                         goto VPNCONF_ERROR;
1640                 }
1641
1642                 # Verify the certificate has a valid CA and move it
1643                 &General::log("ipsec", "Validating imported cert against our known CA...");
1644                 my $validca = 1; #assume ok
1645                 my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/cacert.pem $filename`;
1646                 if ($test !~ /: OK/) {
1647                         my $validca = 0;
1648                         foreach my $key (keys %cahash) {
1649                                 $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/$cahash{$key}[0]cert.pem $filename`;
1650                                 if ($test =~ /: OK/) {
1651                                         $validca = 1;
1652                                         last;
1653                                 }
1654                         }
1655                 }
1656                 if (! $validca) {
1657                         $errormessage = $Lang::tr{'certificate does not have a valid ca associated with it'};
1658                         unlink ($filename);
1659                         goto VPNCONF_ERROR;
1660                 } else {
1661                         move($filename, "${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1662                         if ($? ne 0) {
1663                                 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1664                                 unlink ($filename);
1665                                 goto VPNCONF_ERROR;
1666                         }
1667                 }
1668
1669                 $cgiparams{'CERT_NAME'} = getCNfromcert ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1670                 if ($cgiparams{'CERT_NAME'} eq '') {
1671                         unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1672                         $errormessage = $Lang::tr{'could not retrieve common name from certificate'};
1673                         goto VPNCONF_ERROR;
1674                 }
1675         } elsif ($cgiparams{'AUTH'} eq 'certgen') {
1676                 if ($cgiparams{'KEY'}) {
1677                         $errormessage = $Lang::tr{'cant change certificates'};
1678                         goto VPNCONF_ERROR;
1679                 }
1680                 # Validate input since the form was submitted
1681                 if (length($cgiparams{'CERT_NAME'}) >60) {
1682                         $errormessage = $Lang::tr{'name too long'};
1683                         goto VPNCONF_ERROR;
1684                 }
1685                 if ($cgiparams{'CERT_NAME'} !~ /^[a-zA-Z0-9 ,\.\-_]+$/) {
1686                         $errormessage = $Lang::tr{'invalid input for name'};
1687                         goto VPNCONF_ERROR;
1688                 }
1689                 if ($cgiparams{'CERT_EMAIL'} ne '' && (! &General::validemail($cgiparams{'CERT_EMAIL'}))) {
1690                         $errormessage = $Lang::tr{'invalid input for e-mail address'};
1691                         goto VPNCONF_ERROR;
1692                 }
1693                 if (length($cgiparams{'CERT_EMAIL'}) > 40) {
1694                         $errormessage = $Lang::tr{'e-mail address too long'};
1695                         goto VPNCONF_ERROR;
1696                 }
1697                 if ($cgiparams{'CERT_OU'} ne '' && $cgiparams{'CERT_OU'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1698                         $errormessage = $Lang::tr{'invalid input for department'};
1699                         goto VPNCONF_ERROR;
1700                 }
1701                 if (length($cgiparams{'CERT_ORGANIZATION'}) >60) {
1702                         $errormessage = $Lang::tr{'organization too long'};
1703                         goto VPNCONF_ERROR;
1704                 }
1705                 if ($cgiparams{'CERT_ORGANIZATION'} !~ /^[a-zA-Z0-9 ,\.\-_]+$/) {
1706                         $errormessage = $Lang::tr{'invalid input for organization'};
1707                         goto VPNCONF_ERROR;
1708                 }
1709                 if ($cgiparams{'CERT_CITY'} ne '' && $cgiparams{'CERT_CITY'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1710                         $errormessage = $Lang::tr{'invalid input for city'};
1711                         goto VPNCONF_ERROR;
1712                 }
1713                 if ($cgiparams{'CERT_STATE'} ne '' && $cgiparams{'CERT_STATE'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1714                         $errormessage = $Lang::tr{'invalid input for state or province'};
1715                         goto VPNCONF_ERROR;
1716                 }
1717                 if ($cgiparams{'CERT_COUNTRY'} !~ /^[A-Z]*$/) {
1718                         $errormessage = $Lang::tr{'invalid input for country'};
1719                         goto VPNCONF_ERROR;
1720                 }
1721                 #the exact syntax is a list comma separated of
1722                 #       email:any-validemail
1723                 #       URI: a uniform resource indicator
1724                 #       DNS: a DNS domain name
1725                 #       RID: a registered OBJECT IDENTIFIER
1726                 #       IP: an IP address
1727                 # example: email:franck@foo.com,IP:10.0.0.10,DNS:franck.foo.com
1728
1729                 if ($cgiparams{'SUBJECTALTNAME'} ne '' && $cgiparams{'SUBJECTALTNAME'} !~ /^(email|URI|DNS|RID|IP):[a-zA-Z0-9 :\/,\.\-_@]*$/) {
1730                         $errormessage = $Lang::tr{'vpn altname syntax'};
1731                         goto VPNCONF_ERROR;
1732                 }
1733
1734                 if (length($cgiparams{'CERT_PASS1'}) < 5) {
1735                         $errormessage = $Lang::tr{'password too short'};
1736                         goto VPNCONF_ERROR;
1737                 }
1738                 if ($cgiparams{'CERT_PASS1'} ne $cgiparams{'CERT_PASS2'}) {
1739                         $errormessage = $Lang::tr{'passwords do not match'};
1740                         goto VPNCONF_ERROR;
1741                 }
1742
1743                 # Replace empty strings with a .
1744                 (my $ou = $cgiparams{'CERT_OU'}) =~ s/^\s*$/\./;
1745                 (my $city = $cgiparams{'CERT_CITY'}) =~ s/^\s*$/\./;
1746                 (my $state = $cgiparams{'CERT_STATE'}) =~ s/^\s*$/\./;
1747
1748                 # Create the Client certificate request
1749                 &General::log("ipsec", "Creating a cert...");
1750
1751                 if (open(STDIN, "-|")) {
1752                         my $opt = " req -nodes -rand /proc/interrupts:/proc/net/rt_cache";
1753                         $opt .= " -newkey rsa:2048";
1754                         $opt .= " -keyout ${General::swroot}/certs/$cgiparams{'NAME'}key.pem";
1755                         $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}req.pem";
1756
1757                         if ( $errormessage = &callssl ($opt) ) {
1758                                 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}key.pem");
1759                                 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}req.pem");
1760                                 goto VPNCONF_ERROR;
1761                         }
1762                 } else { #child
1763                         print "$cgiparams{'CERT_COUNTRY'}\n";
1764                         print "$state\n";
1765                         print "$city\n";
1766                         print "$cgiparams{'CERT_ORGANIZATION'}\n";
1767                         print "$ou\n";
1768                         print "$cgiparams{'CERT_NAME'}\n";
1769                         print "$cgiparams{'CERT_EMAIL'}\n";
1770                         print ".\n";
1771                         print ".\n";
1772                         exit (0);
1773                 }
1774
1775                 # Sign the client certificate request
1776                 &General::log("ipsec", "Signing the cert $cgiparams{'NAME'}...");
1777
1778                 #No easy way for specifying the contain of subjectAltName without writing a config file...
1779                 my ($fh, $v3extname) = tempfile ('/tmp/XXXXXXXX');
1780                 print $fh <<END
1781                 basicConstraints=CA:FALSE
1782                 nsComment="OpenSSL Generated Certificate"
1783                 subjectKeyIdentifier=hash
1784                 extendedKeyUsage=clientAuth
1785                 authorityKeyIdentifier=keyid,issuer:always
1786 END
1787 ;
1788                 print $fh "subjectAltName=$cgiparams{'SUBJECTALTNAME'}" if ($cgiparams{'SUBJECTALTNAME'});
1789                 close ($fh);
1790
1791                 my $opt = " ca -md sha256 -days 999999 -batch -notext";
1792                 $opt .= " -in ${General::swroot}/certs/$cgiparams{'NAME'}req.pem";
1793                 $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}cert.pem";
1794                 $opt .= " -extfile $v3extname";
1795
1796                 if ( $errormessage = &callssl ($opt) ) {
1797                         unlink ($v3extname);
1798                         unlink ("${General::swroot}/certs/$cgiparams{'NAME'}key.pem");
1799                         unlink ("${General::swroot}/certs/$cgiparams{'NAME'}req.pem");
1800                         unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1801                         &cleanssldatabase();
1802                         goto VPNCONF_ERROR;
1803                 } else {
1804                         unlink ($v3extname);
1805                         unlink ("${General::swroot}/certs/$cgiparams{'NAME'}req.pem");
1806                         &cleanssldatabase();
1807                 }
1808
1809                 # Create the pkcs12 file
1810                 &General::log("ipsec", "Packing a pkcs12 file...");
1811                 $opt = " pkcs12 -export";
1812                 $opt .= " -inkey ${General::swroot}/certs/$cgiparams{'NAME'}key.pem";
1813                 $opt .= " -in ${General::swroot}/certs/$cgiparams{'NAME'}cert.pem";
1814                 $opt .= " -name \"$cgiparams{'NAME'}\"";
1815                 $opt .= " -passout pass:$cgiparams{'CERT_PASS1'}";
1816                 $opt .= " -certfile ${General::swroot}/ca/cacert.pem";
1817                 $opt .= " -caname \"$vpnsettings{'ROOTCERT_ORGANIZATION'} CA\"";
1818                 $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}.p12";
1819
1820                 if ( $errormessage = &callssl ($opt) ) {
1821                         unlink ("${General::swroot}/certs/$cgiparams{'NAME'}key.pem");
1822                         unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1823                         unlink ("${General::swroot}/certs/$cgiparams{'NAME'}.p12");
1824                         goto VPNCONF_ERROR;
1825                 } else {
1826                         unlink ("${General::swroot}/certs/$cgiparams{'NAME'}key.pem");
1827                 }
1828         } elsif ($cgiparams{'AUTH'} eq 'cert') {
1829                 ;# Nothing, just editing
1830         } elsif ($cgiparams{'AUTH'} eq 'auth-dn') {
1831                 $cgiparams{'CERT_NAME'} = '%auth-dn'; # a special value saying 'no cert file'
1832         } else {
1833                 $errormessage = $Lang::tr{'invalid input for authentication method'};
1834                 goto VPNCONF_ERROR;
1835         }
1836
1837         # 1)Error message here is not accurate.
1838         # 2)Test is superfluous, openswan can reference same cert multiple times
1839         # 3)Present since initial version (1.3.2.11), it isn't a bug correction
1840         # Check if there is no other entry with this certificate name
1841         #if ((! $cgiparams{'KEY'}) && ($cgiparams{'AUTH'} ne 'psk') && ($cgiparams{'AUTH'} ne 'auth-dn')) {
1842         #       foreach my $key (keys %confighash) {
1843         #       if ($confighash{$key}[2] eq $cgiparams{'CERT_NAME'}) {
1844         #               $errormessage = $Lang::tr{'a connection with this common name already exists'};
1845         #               goto VPNCONF_ERROR;
1846         #       }
1847         #       }
1848         #}
1849                 # Save the config
1850
1851         my $key = $cgiparams{'KEY'};
1852         if (! $key) {
1853                 $key = &General::findhasharraykey (\%confighash);
1854                 foreach my $i (0 .. 38) { $confighash{$key}[$i] = "";}
1855         }
1856         $confighash{$key}[0] = $cgiparams{'ENABLED'};
1857         $confighash{$key}[1] = $cgiparams{'NAME'};
1858         if ((! $cgiparams{'KEY'}) && $cgiparams{'AUTH'} ne 'psk') {
1859                 $confighash{$key}[2] = $cgiparams{'CERT_NAME'};
1860         }
1861         $confighash{$key}[3] = $cgiparams{'TYPE'};
1862         if ($cgiparams{'AUTH'} eq 'psk') {
1863                 $confighash{$key}[4] = 'psk';
1864                 $confighash{$key}[5] = $cgiparams{'PSK'};
1865         } else {
1866                 $confighash{$key}[4] = 'cert';
1867         }
1868         if ($cgiparams{'TYPE'} eq 'net') {
1869                 my @remote_subnets = split(",", $cgiparams{'REMOTE_SUBNET'});
1870                 $confighash{$key}[11] = join('|', @remote_subnets);
1871         }
1872         $confighash{$key}[7] = $cgiparams{'LOCAL_ID'};
1873         my @local_subnets = split(",", $cgiparams{'LOCAL_SUBNET'});
1874         $confighash{$key}[8] = join('|', @local_subnets);
1875         $confighash{$key}[9] = $cgiparams{'REMOTE_ID'};
1876         $confighash{$key}[10] = $cgiparams{'REMOTE'};
1877         $confighash{$key}[25] = $cgiparams{'REMARK'};
1878         $confighash{$key}[26] = ""; # Formerly INTERFACE
1879         $confighash{$key}[27] = $cgiparams{'DPD_ACTION'};
1880         $confighash{$key}[29] = $cgiparams{'IKE_VERSION'};
1881
1882         # don't forget advanced value
1883         $confighash{$key}[18] = $cgiparams{'IKE_ENCRYPTION'};
1884         $confighash{$key}[19] = $cgiparams{'IKE_INTEGRITY'};
1885         $confighash{$key}[20] = $cgiparams{'IKE_GROUPTYPE'};
1886         $confighash{$key}[16] = $cgiparams{'IKE_LIFETIME'};
1887         $confighash{$key}[21] = $cgiparams{'ESP_ENCRYPTION'};
1888         $confighash{$key}[22] = $cgiparams{'ESP_INTEGRITY'};
1889         $confighash{$key}[23] = $cgiparams{'ESP_GROUPTYPE'};
1890         $confighash{$key}[17] = $cgiparams{'ESP_KEYLIFE'};
1891         $confighash{$key}[12] = 'off'; # $cgiparams{'AGGRMODE'};
1892         $confighash{$key}[13] = $cgiparams{'COMPRESSION'};
1893         $confighash{$key}[24] = $cgiparams{'ONLY_PROPOSED'};
1894         $confighash{$key}[28] = $cgiparams{'PFS'};
1895         $confighash{$key}[30] = $cgiparams{'DPD_TIMEOUT'};
1896         $confighash{$key}[31] = $cgiparams{'DPD_DELAY'};
1897         $confighash{$key}[32] = $cgiparams{'FORCE_MOBIKE'};
1898         $confighash{$key}[34] = $cgiparams{'INACTIVITY_TIMEOUT'};
1899         $confighash{$key}[35] = $cgiparams{'MODE'};
1900         $confighash{$key}[36] = $cgiparams{'INTERFACE_MODE'};
1901         $confighash{$key}[37] = $cgiparams{'INTERFACE_ADDRESS'};
1902         $confighash{$key}[38] = $cgiparams{'INTERFACE_MTU'};
1903
1904         # free unused fields!
1905         $confighash{$key}[6] = 'off';
1906         $confighash{$key}[15] = 'off';
1907
1908         &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
1909         &writeipsecfiles();
1910         if (&vpnenabled) {
1911                 system('/usr/local/bin/ipsecctrl', 'S', $key);
1912                 sleep $sleepDelay;
1913         }
1914         if ($cgiparams{'EDIT_ADVANCED'} eq 'on') {
1915                 $cgiparams{'KEY'} = $key;
1916                 $cgiparams{'ACTION'} = $Lang::tr{'advanced'};
1917         }
1918         goto VPNCONF_END;
1919 } else { # add new connection
1920         $cgiparams{'ENABLED'} = 'on';
1921         if ( ! -f "${General::swroot}/private/cakey.pem" ) {
1922                 $cgiparams{'AUTH'} = 'psk';
1923         } elsif ( ! -f "${General::swroot}/ca/cacert.pem") {
1924                 $cgiparams{'AUTH'} = 'certfile';
1925         } else {
1926                 $cgiparams{'AUTH'} = 'certgen';
1927         }
1928         $cgiparams{'LOCAL_SUBNET'}              = "$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}";
1929         $cgiparams{'CERT_EMAIL'}                = $vpnsettings{'ROOTCERT_EMAIL'};
1930         $cgiparams{'CERT_OU'}                   = $vpnsettings{'ROOTCERT_OU'};
1931         $cgiparams{'CERT_ORGANIZATION'} = $vpnsettings{'ROOTCERT_ORGANIZATION'};
1932         $cgiparams{'CERT_CITY'}                 = $vpnsettings{'ROOTCERT_CITY'};
1933         $cgiparams{'CERT_STATE'}                = $vpnsettings{'ROOTCERT_STATE'};
1934         $cgiparams{'CERT_COUNTRY'}              = $vpnsettings{'ROOTCERT_COUNTRY'};
1935
1936         # choose appropriate dpd action
1937         if ($cgiparams{'TYPE'} eq 'host') {
1938                 $cgiparams{'DPD_ACTION'} = 'clear';
1939         } else {
1940                 $cgiparams{'DPD_ACTION'} = 'restart';
1941         }
1942
1943         if (!$cgiparams{'DPD_DELAY'}) {
1944                 $cgiparams{'DPD_DELAY'} = 30;
1945         }
1946
1947         if (!$cgiparams{'DPD_TIMEOUT'}) {
1948                 $cgiparams{'DPD_TIMEOUT'} = 120;
1949         }
1950
1951         if (!$cgiparams{'FORCE_MOBIKE'}) {
1952                 $cgiparams{'FORCE_MOBIKE'} = 'no';
1953         }
1954
1955         # Default IKE Version to v2
1956         if (!$cgiparams{'IKE_VERSION'}) {
1957                 $cgiparams{'IKE_VERSION'} = 'ikev2';
1958         }
1959
1960         # ID are empty
1961         $cgiparams{'LOCAL_ID'} = '';
1962         $cgiparams{'REMOTE_ID'} = '';
1963
1964         #use default advanced value
1965         $cgiparams{'IKE_ENCRYPTION'}    = 'chacha20poly1305|aes256gcm128|aes256gcm96|aes256gcm64|aes256|aes192gcm128|aes192gcm96|aes192gcm64|aes192|aes128gcm128|aes128gcm96|aes128gcm64|aes128'; #[18];
1966         $cgiparams{'IKE_INTEGRITY'}             = 'sha2_512|sha2_256'; #[19];
1967         $cgiparams{'IKE_GROUPTYPE'}             = 'curve25519|4096|3072|2048'; #[20];
1968         $cgiparams{'IKE_LIFETIME'}              = '3'; #[16];
1969         $cgiparams{'ESP_ENCRYPTION'}    = 'chacha20poly1305|aes256gcm128|aes256gcm96|aes256gcm64|aes256|aes192gcm128|aes192gcm96|aes192gcm64|aes192|aes128gcm128|aes128gcm96|aes128gcm64|aes128'; #[21];
1970         $cgiparams{'ESP_INTEGRITY'}             = 'sha2_512|sha2_256'; #[22];
1971         $cgiparams{'ESP_GROUPTYPE'}             = 'curve25519|4096|3072|2048'; #[23];
1972         $cgiparams{'ESP_KEYLIFE'}               = '1'; #[17];
1973         $cgiparams{'COMPRESSION'}               = 'off'; #[13];
1974         $cgiparams{'ONLY_PROPOSED'}             = 'on'; #[24];
1975         $cgiparams{'PFS'}                               = 'on'; #[28];
1976         $cgiparams{'INACTIVITY_TIMEOUT'}        = 900;
1977         $cgiparams{'MODE'}                      = "tunnel";
1978         $cgiparams{'INTERFACE_MODE'}            = "";
1979         $cgiparams{'INTERFACE_ADDRESS'}         = "";
1980         $cgiparams{'INTERFACE_MTU'}             = 1500;
1981 }
1982
1983 VPNCONF_ERROR:
1984         $checked{'ENABLED'}{'off'} = '';
1985         $checked{'ENABLED'}{'on'} = '';
1986         $checked{'ENABLED'}{$cgiparams{'ENABLED'}} = "checked='checked'";
1987
1988         $checked{'EDIT_ADVANCED'}{'off'} = '';
1989         $checked{'EDIT_ADVANCED'}{'on'} = '';
1990         $checked{'EDIT_ADVANCED'}{$cgiparams{'EDIT_ADVANCED'}} = "checked='checked'";
1991
1992         $checked{'AUTH'}{'psk'} = '';
1993         $checked{'AUTH'}{'certreq'} = '';
1994         $checked{'AUTH'}{'certgen'} = '';
1995         $checked{'AUTH'}{'certfile'} = '';
1996         $checked{'AUTH'}{'pkcs12'} = '';
1997         $checked{'AUTH'}{'auth-dn'} = '';
1998         $checked{'AUTH'}{$cgiparams{'AUTH'}} = "checked='checked'";
1999
2000         &Header::showhttpheaders();
2001         &Header::openpage($Lang::tr{'ipsec'}, 1, '');
2002         &Header::openbigbox('100%', 'left', '', $errormessage);
2003         if ($errormessage) {
2004                 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
2005                 print "<class name='base'>$errormessage";
2006                 print "&nbsp;</class>";
2007                 &Header::closebox();
2008         }
2009
2010         if ($warnmessage) {
2011                 &Header::openbox('100%', 'left', "$Lang::tr{'warning messages'}:");
2012                 print "<class name='base'>$warnmessage";
2013                 print "&nbsp;</class>";
2014                 &Header::closebox();
2015         }
2016
2017         print "<form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'>";
2018         print<<END
2019         <input type='hidden' name='TYPE' value='$cgiparams{'TYPE'}' />
2020         <input type='hidden' name='IKE_VERSION' value='$cgiparams{'IKE_VERSION'}' />
2021         <input type='hidden' name='IKE_ENCRYPTION' value='$cgiparams{'IKE_ENCRYPTION'}' />
2022         <input type='hidden' name='IKE_INTEGRITY' value='$cgiparams{'IKE_INTEGRITY'}' />
2023         <input type='hidden' name='IKE_GROUPTYPE' value='$cgiparams{'IKE_GROUPTYPE'}' />
2024         <input type='hidden' name='IKE_LIFETIME' value='$cgiparams{'IKE_LIFETIME'}' />
2025         <input type='hidden' name='ESP_ENCRYPTION' value='$cgiparams{'ESP_ENCRYPTION'}' />
2026         <input type='hidden' name='ESP_INTEGRITY' value='$cgiparams{'ESP_INTEGRITY'}' />
2027         <input type='hidden' name='ESP_GROUPTYPE' value='$cgiparams{'ESP_GROUPTYPE'}' />
2028         <input type='hidden' name='ESP_KEYLIFE' value='$cgiparams{'ESP_KEYLIFE'}' />
2029         <input type='hidden' name='COMPRESSION' value='$cgiparams{'COMPRESSION'}' />
2030         <input type='hidden' name='ONLY_PROPOSED' value='$cgiparams{'ONLY_PROPOSED'}' />
2031         <input type='hidden' name='PFS' value='$cgiparams{'PFS'}' />
2032         <input type='hidden' name='DPD_ACTION' value='$cgiparams{'DPD_ACTION'}' />
2033         <input type='hidden' name='DPD_DELAY' value='$cgiparams{'DPD_DELAY'}' />
2034         <input type='hidden' name='DPD_TIMEOUT' value='$cgiparams{'DPD_TIMEOUT'}' />
2035         <input type='hidden' name='FORCE_MOBIKE' value='$cgiparams{'FORCE_MOBIKE'}' />
2036         <input type='hidden' name='INACTIVITY_TIMEOUT' value='$cgiparams{'INACTIVITY_TIMEOUT'}' />
2037         <input type='hidden' name='MODE' value='$cgiparams{'MODE'}' />
2038         <input type='hidden' name='INTERFACE_MODE' value='$cgiparams{'INTERFACE_MODE'}' />
2039         <input type='hidden' name='INTERFACE_ADDRESS' value='$cgiparams{'INTERFACE_ADDRESS'}' />
2040         <input type='hidden' name='INTERFACE_MTU' value='$cgiparams{'INTERFACE_MTU'}' />
2041 END
2042 ;
2043         if ($cgiparams{'KEY'}) {
2044                 print "<input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />";
2045                 print "<input type='hidden' name='NAME' value='$cgiparams{'NAME'}' />";
2046                 print "<input type='hidden' name='AUTH' value='$cgiparams{'AUTH'}' />";
2047         }
2048
2049         &Header::openbox('100%', 'left', "$Lang::tr{'connection'}: $cgiparams{'NAME'}");
2050         print "<table width='100%'>";
2051         if (!$cgiparams{'KEY'}) {
2052                 print <<EOF;
2053                         <tr>
2054                                 <td width='20%'>$Lang::tr{'name'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
2055                                 <td width='30%'>
2056                                         <input type='text' name='NAME' value='$cgiparams{'NAME'}' size='25' />
2057                                 </td>
2058                                 <td colspan="2"></td>
2059                         </tr>
2060 EOF
2061         }
2062
2063         my $disabled;
2064         my $blob;
2065         if ($cgiparams{'TYPE'} eq 'host') {
2066                 $disabled = "disabled='disabled'";
2067         } elsif ($cgiparams{'TYPE'} eq 'net') {
2068                 $blob = "<img src='/blob.gif' alt='*' />";
2069         };
2070
2071         my @local_subnets = split(/\|/, $cgiparams{'LOCAL_SUBNET'});
2072         my $local_subnets = join(",", @local_subnets);
2073
2074         my @remote_subnets = split(/\|/, $cgiparams{'REMOTE_SUBNET'});
2075         my $remote_subnets = join(",", @remote_subnets);
2076
2077         print <<END
2078         <tr>
2079                 <td width='20%'>$Lang::tr{'enabled'}</td>
2080                 <td width='30%'>
2081                         <input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} />
2082                 </td>
2083                 <td class='boldbase' nowrap='nowrap' width='20%'>$Lang::tr{'local subnet'}&nbsp;<img src='/blob.gif' alt='*' /></td>
2084                 <td width='30%'>
2085                         <input type='text' name='LOCAL_SUBNET' value='$local_subnets' />
2086                 </td>
2087         </tr>
2088         <tr>
2089                 <td class='boldbase' width='20%'>$Lang::tr{'remote host/ip'}:&nbsp;$blob</td>
2090                 <td width='30%'>
2091                         <input type='text' name='REMOTE' value='$cgiparams{'REMOTE'}' size="25" />
2092                 </td>
2093                 <td class='boldbase' nowrap='nowrap' width='20%'>$Lang::tr{'remote subnet'}&nbsp;$blob</td>
2094                 <td width='30%'>
2095                         <input $disabled type='text' name='REMOTE_SUBNET' value='$remote_subnets' />
2096                 </td>
2097         </tr>
2098         <tr>
2099                 <td class='boldbase' width='20%'>$Lang::tr{'vpn local id'}:</td>
2100                 <td width='30%'>
2101                         <input type='text' name='LOCAL_ID' value='$cgiparams{'LOCAL_ID'}' size="25" />
2102                 </td>
2103                 <td class='boldbase' width='20%'>$Lang::tr{'vpn remote id'}:</td>
2104                 <td width='30%'>
2105                         <input type='text' name='REMOTE_ID' value='$cgiparams{'REMOTE_ID'}' size="25" />
2106                 </td>
2107         </tr>
2108         <tr><td colspan="4"><br /></td></tr>
2109         <tr>
2110                 <td class='boldbase' width='20%'>$Lang::tr{'remark title'}</td>
2111                 <td colspan='3'>
2112                         <input type='text' name='REMARK' value='$cgiparams{'REMARK'}' maxlength='50' size="73" />
2113                 </td>
2114         </tr>
2115 END
2116 ;
2117         if (!$cgiparams{'KEY'}) {
2118                 print "<tr><td colspan='3'><input type='checkbox' name='EDIT_ADVANCED' $checked{'EDIT_ADVANCED'}{'on'} /> $Lang::tr{'edit advanced settings when done'}</td></tr>";
2119         }
2120         print "</table>";
2121         &Header::closebox();
2122
2123         if ($cgiparams{'KEY'} && $cgiparams{'AUTH'} eq 'psk') {
2124                 &Header::openbox('100%', 'left', $Lang::tr{'authentication'});
2125                 print <<END
2126                 <table width='100%' cellpadding='0' cellspacing='5' border='0'>
2127                 <tr><td class='base' width='50%'>$Lang::tr{'use a pre-shared key'}</td>
2128                         <td class='base' width='50%'><input type='password' name='PSK' size='30' value='$cgiparams{'PSK'}' /></td>
2129                 </tr>
2130                 </table>
2131 END
2132 ;
2133                 &Header::closebox();
2134         } elsif (! $cgiparams{'KEY'}) {
2135                 my $cakeydisabled = ( ! -f "${General::swroot}/private/cakey.pem" ) ? "disabled='disabled'" : '';
2136                 $cgiparams{'CERT_NAME'} = $Lang::tr{'vpn no full pki'} if ($cakeydisabled);
2137                 my $cacrtdisabled = ( ! -f "${General::swroot}/ca/cacert.pem" ) ? "disabled='disabled'" : '';
2138
2139                 &Header::openbox('100%', 'left', $Lang::tr{'authentication'});
2140                 print <<END
2141                 <table width='100%' cellpadding='0' cellspacing='5' border='0'>
2142                 <tr><td width='5%'><input type='radio' name='AUTH' value='psk' $checked{'AUTH'}{'psk'} /></td>
2143                         <td class='base' width='55%'>$Lang::tr{'use a pre-shared key'}</td>
2144                         <td class='base' width='40%'><input type='password' name='PSK' size='30' value='$cgiparams{'PSK'}' /></td></tr>
2145                 <tr><td colspan='3' bgcolor='#000000'></td></tr>
2146                 <tr><td><input type='radio' name='AUTH' value='certreq' $checked{'AUTH'}{'certreq'} $cakeydisabled /></td>
2147                         <td class='base'><hr />$Lang::tr{'upload a certificate request'}</td>
2148                         <td class='base' rowspan='3' valign='middle'><input type='file' name='FH' size='30' $cacrtdisabled /></td></tr>
2149                 <tr><td><input type='radio' name='AUTH' value='certfile' $checked{'AUTH'}{'certfile'} $cacrtdisabled /></td>
2150                         <td class='base'>$Lang::tr{'upload a certificate'}</td></tr>
2151                 <tr><td><input type='radio' name='AUTH' value='pkcs12' $cacrtdisabled /></td>
2152                         <td class='base'>$Lang::tr{'upload p12 file'} $Lang::tr{'pkcs12 file password'}:<input type='password' name='P12_PASS'/></td></tr>
2153                 <tr><td><input type='radio' name='AUTH' value='auth-dn' $checked{'AUTH'}{'auth-dn'} $cacrtdisabled /></td>
2154                         <td class='base'><hr />$Lang::tr{'vpn auth-dn'}</td></tr>
2155                 <tr><td colspan='3' bgcolor='#000000'></td></tr>
2156                 <tr><td><input type='radio' name='AUTH' value='certgen' $checked{'AUTH'}{'certgen'} $cakeydisabled /></td>
2157                         <td class='base'><hr />$Lang::tr{'generate a certificate'}</td><td>&nbsp;</td></tr>
2158                 <tr><td>&nbsp;</td>
2159                         <td class='base'>$Lang::tr{'users fullname or system hostname'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
2160                         <td class='base' nowrap='nowrap'><input type='text' name='CERT_NAME' value='$cgiparams{'CERT_NAME'}' size='32' $cakeydisabled /></td></tr>
2161                 <tr><td>&nbsp;</td>
2162                         <td class='base'>$Lang::tr{'users email'}:</td>
2163                         <td class='base' nowrap='nowrap'><input type='text' name='CERT_EMAIL' value='$cgiparams{'CERT_EMAIL'}' size='32' $cakeydisabled /></td></tr>
2164                 <tr><td>&nbsp;</td>
2165                         <td class='base'>$Lang::tr{'users department'}:</td>
2166                         <td class='base' nowrap='nowrap'><input type='text' name='CERT_OU' value='$cgiparams{'CERT_OU'}' size='32' $cakeydisabled /></td></tr>
2167                 <tr><td>&nbsp;</td>
2168                         <td class='base'>$Lang::tr{'organization name'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
2169                         <td class='base' nowrap='nowrap'><input type='text' name='CERT_ORGANIZATION' value='$cgiparams{'CERT_ORGANIZATION'}' size='32' $cakeydisabled /></td></tr>
2170                 <tr><td>&nbsp;</td>
2171                         <td class='base'>$Lang::tr{'city'}:</td>
2172                         <td class='base' nowrap='nowrap'><input type='text' name='CERT_CITY' value='$cgiparams{'CERT_CITY'}' size='32' $cakeydisabled /></td></tr>
2173                 <tr><td>&nbsp;</td>
2174                         <td class='base'>$Lang::tr{'state or province'}:</td>
2175                         <td class='base' nowrap='nowrap'><input type='text' name='CERT_STATE' value='$cgiparams{'CERT_STATE'}' size='32' $cakeydisabled /></td></tr>
2176                 <tr><td>&nbsp;</td>
2177                         <td class='base'>$Lang::tr{'country'}:</td>
2178                         <td class='base'><select name='CERT_COUNTRY' $cakeydisabled>
2179 END
2180 ;
2181                 foreach my $country (sort keys %{Countries::countries}) {
2182                         print "\t\t\t<option value='$Countries::countries{$country}'";
2183                         if ( $Countries::countries{$country} eq $cgiparams{'CERT_COUNTRY'} ) {
2184                                 print " selected='selected'";
2185                         }
2186                         print ">$country</option>\n";
2187                 }
2188                 print <<END
2189                         </select></td></tr>
2190
2191                 <tr><td>&nbsp;</td><td class='base'>$Lang::tr{'vpn subjectaltname'} (subjectAltName=email:*,URI:*,DNS:*,RID:*)</td>
2192                         <td class='base' nowrap='nowrap'><input type='text' name='SUBJECTALTNAME' value='$cgiparams{'SUBJECTALTNAME'}' size='32' $cakeydisabled /></td></tr>
2193                 <tr><td>&nbsp;</td>
2194                         <td class='base'>$Lang::tr{'pkcs12 file password'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
2195                         <td class='base' nowrap='nowrap'><input type='password' name='CERT_PASS1' value='$cgiparams{'CERT_PASS1'}' size='32' $cakeydisabled /></td></tr>
2196                 <tr><td>&nbsp;</td><td class='base'>$Lang::tr{'pkcs12 file password'}&nbsp;($Lang::tr{'confirmation'}):&nbsp;<img src='/blob.gif' alt='*' /></td>
2197                         <td class='base' nowrap='nowrap'><input type='password' name='CERT_PASS2' value='$cgiparams{'CERT_PASS2'}' size='32' $cakeydisabled /></td></tr>
2198                 </table>
2199 END
2200 ;
2201                 &Header::closebox();
2202         }
2203
2204         print "<div align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' />";
2205         if ($cgiparams{'KEY'}) {
2206                 print "<input type='submit' name='ACTION' value='$Lang::tr{'advanced'}' />";
2207         }
2208         print "<input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></div></form>";
2209         &Header::closebigbox();
2210         &Header::closepage();
2211         exit (0);
2212
2213         VPNCONF_END:
2214 }
2215
2216 ###
2217 ### Advanced settings
2218 ###
2219 if(($cgiparams{'ACTION'} eq $Lang::tr{'advanced'}) ||
2220         ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'ADVANCED'} eq 'yes')) {
2221         &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
2222         &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
2223         if (! $confighash{$cgiparams{'KEY'}}) {
2224                 $errormessage = $Lang::tr{'invalid key'};
2225                 goto ADVANCED_END;
2226         }
2227
2228         if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
2229                 my @temp = split('\|', $cgiparams{'IKE_ENCRYPTION'});
2230                 if ($#temp < 0) {
2231                         $errormessage = $Lang::tr{'invalid input'};
2232                         goto ADVANCED_ERROR;
2233                 }
2234                 foreach my $val (@temp) {
2235                         if ($val !~ /^(aes(256|192|128)(gcm(128|96|64))?|3des|chacha20poly1305|camellia(256|192|128))$/) {
2236                                 $errormessage = $Lang::tr{'invalid input'};
2237                                 goto ADVANCED_ERROR;
2238                         }
2239                 }
2240                 @temp = split('\|', $cgiparams{'IKE_INTEGRITY'});
2241                 if ($#temp < 0) {
2242                         $errormessage = $Lang::tr{'invalid input'};
2243                         goto ADVANCED_ERROR;
2244                 }
2245                 foreach my $val (@temp) {
2246                         if ($val !~ /^(sha2_(512|384|256)|sha|md5|aesxcbc)$/) {
2247                                 $errormessage = $Lang::tr{'invalid input'};
2248                                 goto ADVANCED_ERROR;
2249                         }
2250                 }
2251                 @temp = split('\|', $cgiparams{'IKE_GROUPTYPE'});
2252                 if ($#temp < 0) {
2253                         $errormessage = $Lang::tr{'invalid input'};
2254                         goto ADVANCED_ERROR;
2255                 }
2256                 foreach my $val (@temp) {
2257                         if ($val !~ /^(curve25519|e521|e384|e256|e224|e192|e512bp|e384bp|e256bp|e224bp|768|1024|1536|2048|3072|4096|6144|8192)$/) {
2258                                 $errormessage = $Lang::tr{'invalid input'};
2259                                 goto ADVANCED_ERROR;
2260                         }
2261                 }
2262                 if ($cgiparams{'IKE_LIFETIME'} !~ /^\d+$/) {
2263                         $errormessage = $Lang::tr{'invalid input for ike lifetime'};
2264                         goto ADVANCED_ERROR;
2265                 }
2266                 if ($cgiparams{'IKE_LIFETIME'} < 1 || $cgiparams{'IKE_LIFETIME'} > 8) {
2267                         $errormessage = $Lang::tr{'ike lifetime should be between 1 and 8 hours'};
2268                         goto ADVANCED_ERROR;
2269                 }
2270                 @temp = split('\|', $cgiparams{'ESP_ENCRYPTION'});
2271                 if ($#temp < 0) {
2272                         $errormessage = $Lang::tr{'invalid input'};
2273                         goto ADVANCED_ERROR;
2274                 }
2275                 foreach my $val (@temp) {
2276                         if ($val !~ /^(aes(256|192|128)(gcm(128|96|64))?|3des|chacha20poly1305|camellia(256|192|128))$/) {
2277                                 $errormessage = $Lang::tr{'invalid input'};
2278                                 goto ADVANCED_ERROR;
2279                         }
2280                 }
2281                 @temp = split('\|', $cgiparams{'ESP_INTEGRITY'});
2282                 if ($#temp < 0) {
2283                         $errormessage = $Lang::tr{'invalid input'};
2284                         goto ADVANCED_ERROR;
2285                 }
2286                 foreach my $val (@temp) {
2287                         if ($val !~ /^(sha2_(512|384|256)|sha1|md5|aesxcbc)$/) {
2288                                 $errormessage = $Lang::tr{'invalid input'};
2289                                 goto ADVANCED_ERROR;
2290                         }
2291                 }
2292                 @temp = split('\|', $cgiparams{'ESP_GROUPTYPE'});
2293                 if ($#temp < 0) {
2294                         $errormessage = $Lang::tr{'invalid input'};
2295                         goto ADVANCED_ERROR;
2296                 }
2297                 foreach my $val (@temp) {
2298                         if ($val !~ /^(curve25519|e521|e384|e256|e224|e192|e512bp|e384bp|e256bp|e224bp|768|1024|1536|2048|3072|4096|6144|8192|none)$/) {
2299                                 $errormessage = $Lang::tr{'invalid input'};
2300                                 goto ADVANCED_ERROR;
2301                         }
2302                 }
2303                 if ($cgiparams{'ESP_KEYLIFE'} !~ /^\d+$/) {
2304                         $errormessage = $Lang::tr{'invalid input for esp keylife'};
2305                         goto ADVANCED_ERROR;
2306                 }
2307                 if ($cgiparams{'ESP_KEYLIFE'} < 1 || $cgiparams{'ESP_KEYLIFE'} > 24) {
2308                         $errormessage = $Lang::tr{'esp keylife should be between 1 and 24 hours'};
2309                         goto ADVANCED_ERROR;
2310                 }
2311
2312                 if (($cgiparams{'COMPRESSION'} !~ /^(|on|off)$/) ||
2313                         ($cgiparams{'FORCE_MOBIKE'} !~ /^(|on|off)$/) ||
2314                         ($cgiparams{'ONLY_PROPOSED'} !~ /^(|on|off)$/) ||
2315                         ($cgiparams{'PFS'} !~ /^(|on|off)$/)) {
2316                         $errormessage = $Lang::tr{'invalid input'};
2317                         goto ADVANCED_ERROR;
2318                 }
2319
2320                 if ($cgiparams{'DPD_DELAY'} !~ /^\d+$/) {
2321                         $errormessage = $Lang::tr{'invalid input for dpd delay'};
2322                         goto ADVANCED_ERROR;
2323                 }
2324
2325                 if ($cgiparams{'DPD_TIMEOUT'} !~ /^\d+$/) {
2326                         $errormessage = $Lang::tr{'invalid input for dpd timeout'};
2327                         goto ADVANCED_ERROR;
2328                 }
2329
2330                 if ($cgiparams{'INACTIVITY_TIMEOUT'} !~ /^\d+$/) {
2331                         $errormessage = $Lang::tr{'invalid input for inactivity timeout'};
2332                         goto ADVANCED_ERROR;
2333                 }
2334
2335                 if ($cgiparams{'MODE'} !~ /^(tunnel|transport)$/) {
2336                         $errormessage = $Lang::tr{'invalid input for mode'};
2337                         goto ADVANCED_ERROR;
2338                 }
2339
2340                 if ($cgiparams{'INTERFACE_MODE'} !~ /^(|gre|vti)$/) {
2341                         $errormessage = $Lang::tr{'invalid input for interface mode'};
2342                         goto ADVANCED_ERROR;
2343                 }
2344
2345                 if (($cgiparams{'INTERFACE_MODE'} ne "") && !&Network::check_subnet($cgiparams{'INTERFACE_ADDRESS'})) {
2346                         $errormessage = $Lang::tr{'invalid input for interface address'};
2347                         goto ADVANCED_ERROR;
2348                 }
2349
2350                 if ($cgiparams{'INTERFACE_MTU'} !~ /^\d+$/) {
2351                         $errormessage = $Lang::tr{'invalid input for interface mtu'};
2352                         goto ADVANCED_ERROR;
2353                 }
2354
2355                 $confighash{$cgiparams{'KEY'}}[29] = $cgiparams{'IKE_VERSION'};
2356                 $confighash{$cgiparams{'KEY'}}[18] = $cgiparams{'IKE_ENCRYPTION'};
2357                 $confighash{$cgiparams{'KEY'}}[19] = $cgiparams{'IKE_INTEGRITY'};
2358                 $confighash{$cgiparams{'KEY'}}[20] = $cgiparams{'IKE_GROUPTYPE'};
2359                 $confighash{$cgiparams{'KEY'}}[16] = $cgiparams{'IKE_LIFETIME'};
2360                 $confighash{$cgiparams{'KEY'}}[21] = $cgiparams{'ESP_ENCRYPTION'};
2361                 $confighash{$cgiparams{'KEY'}}[22] = $cgiparams{'ESP_INTEGRITY'};
2362                 $confighash{$cgiparams{'KEY'}}[23] = $cgiparams{'ESP_GROUPTYPE'};
2363                 $confighash{$cgiparams{'KEY'}}[17] = $cgiparams{'ESP_KEYLIFE'};
2364                 $confighash{$cgiparams{'KEY'}}[12] = 'off'; #$cgiparams{'AGGRMODE'};
2365                 $confighash{$cgiparams{'KEY'}}[13] = $cgiparams{'COMPRESSION'};
2366                 $confighash{$cgiparams{'KEY'}}[24] = $cgiparams{'ONLY_PROPOSED'};
2367                 $confighash{$cgiparams{'KEY'}}[28] = $cgiparams{'PFS'};
2368                 $confighash{$cgiparams{'KEY'}}[27] = $cgiparams{'DPD_ACTION'};
2369                 $confighash{$cgiparams{'KEY'}}[30] = $cgiparams{'DPD_TIMEOUT'};
2370                 $confighash{$cgiparams{'KEY'}}[31] = $cgiparams{'DPD_DELAY'};
2371                 $confighash{$cgiparams{'KEY'}}[32] = $cgiparams{'FORCE_MOBIKE'};
2372                 $confighash{$cgiparams{'KEY'}}[33] = $cgiparams{'START_ACTION'};
2373                 $confighash{$cgiparams{'KEY'}}[34] = $cgiparams{'INACTIVITY_TIMEOUT'};
2374                 $confighash{$cgiparams{'KEY'}}[35] = $cgiparams{'MODE'};
2375                 $confighash{$cgiparams{'KEY'}}[36] = $cgiparams{'INTERFACE_MODE'};
2376                 $confighash{$cgiparams{'KEY'}}[37] = $cgiparams{'INTERFACE_ADDRESS'};
2377                 $confighash{$cgiparams{'KEY'}}[38] = $cgiparams{'INTERFACE_MTU'};
2378                 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
2379                 &writeipsecfiles();
2380                 if (&vpnenabled) {
2381                         system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'});
2382                         sleep $sleepDelay;
2383                 }
2384                 goto ADVANCED_END;
2385         } else {
2386                 $cgiparams{'IKE_VERSION'}               = $confighash{$cgiparams{'KEY'}}[29];
2387                 $cgiparams{'IKE_ENCRYPTION'}    = $confighash{$cgiparams{'KEY'}}[18];
2388                 $cgiparams{'IKE_INTEGRITY'}             = $confighash{$cgiparams{'KEY'}}[19];
2389                 $cgiparams{'IKE_GROUPTYPE'}             = $confighash{$cgiparams{'KEY'}}[20];
2390                 $cgiparams{'IKE_LIFETIME'}              = $confighash{$cgiparams{'KEY'}}[16];
2391                 $cgiparams{'ESP_ENCRYPTION'}    = $confighash{$cgiparams{'KEY'}}[21];
2392                 $cgiparams{'ESP_INTEGRITY'}             = $confighash{$cgiparams{'KEY'}}[22];
2393                 $cgiparams{'ESP_GROUPTYPE'}             = $confighash{$cgiparams{'KEY'}}[23];
2394                 if ($cgiparams{'ESP_GROUPTYPE'} eq "") {
2395                         $cgiparams{'ESP_GROUPTYPE'}     = $cgiparams{'IKE_GROUPTYPE'};
2396                 }
2397                 $cgiparams{'ESP_KEYLIFE'}               = $confighash{$cgiparams{'KEY'}}[17];
2398                 $cgiparams{'COMPRESSION'}               = $confighash{$cgiparams{'KEY'}}[13];
2399                 $cgiparams{'ONLY_PROPOSED'}             = $confighash{$cgiparams{'KEY'}}[24];
2400                 $cgiparams{'PFS'}                               = $confighash{$cgiparams{'KEY'}}[28];
2401                 $cgiparams{'DPD_ACTION'}                = $confighash{$cgiparams{'KEY'}}[27];
2402                 $cgiparams{'DPD_TIMEOUT'}               = $confighash{$cgiparams{'KEY'}}[30];
2403                 $cgiparams{'DPD_DELAY'}                 = $confighash{$cgiparams{'KEY'}}[31];
2404                 $cgiparams{'FORCE_MOBIKE'}              = $confighash{$cgiparams{'KEY'}}[32];
2405                 $cgiparams{'START_ACTION'}              = $confighash{$cgiparams{'KEY'}}[33];
2406                 $cgiparams{'INACTIVITY_TIMEOUT'}        = $confighash{$cgiparams{'KEY'}}[34];
2407                 $cgiparams{'MODE'}                      = $confighash{$cgiparams{'KEY'}}[35];
2408                 $cgiparams{'INTERFACE_MODE'}            = $confighash{$cgiparams{'KEY'}}[36];
2409                 $cgiparams{'INTERFACE_ADDRESS'}         = $confighash{$cgiparams{'KEY'}}[37];
2410                 $cgiparams{'INTERFACE_MTU'}             = $confighash{$cgiparams{'KEY'}}[38];
2411
2412                 if (!$cgiparams{'DPD_DELAY'}) {
2413                         $cgiparams{'DPD_DELAY'} = 30;
2414                 }
2415
2416                 if (!$cgiparams{'DPD_TIMEOUT'}) {
2417                         $cgiparams{'DPD_TIMEOUT'} = 120;
2418                 }
2419
2420                 if (!$cgiparams{'START_ACTION'}) {
2421                         $cgiparams{'START_ACTION'} = "start";
2422                 }
2423
2424                 if ($cgiparams{'INACTIVITY_TIMEOUT'} eq "") {
2425                         $cgiparams{'INACTIVITY_TIMEOUT'} = 900; # 15 min
2426                 }
2427
2428                 if ($cgiparams{'MODE'} eq "") {
2429                         $cgiparams{'MODE'} = "tunnel";
2430                 }
2431         }
2432
2433         ADVANCED_ERROR:
2434         $checked{'IKE_ENCRYPTION'}{'chacha20poly1305'} = '';
2435         $checked{'IKE_ENCRYPTION'}{'aes256'} = '';
2436         $checked{'IKE_ENCRYPTION'}{'aes192'} = '';
2437         $checked{'IKE_ENCRYPTION'}{'aes128'} = '';
2438         $checked{'IKE_ENCRYPTION'}{'aes256gcm128'} = '';
2439         $checked{'IKE_ENCRYPTION'}{'aes192gcm128'} = '';
2440         $checked{'IKE_ENCRYPTION'}{'aes128gcm128'} = '';
2441         $checked{'IKE_ENCRYPTION'}{'aes256gcm96'} = '';
2442         $checked{'IKE_ENCRYPTION'}{'aes192gcm96'} = '';
2443         $checked{'IKE_ENCRYPTION'}{'aes128gcm96'} = '';
2444         $checked{'IKE_ENCRYPTION'}{'aes256gcm64'} = '';
2445         $checked{'IKE_ENCRYPTION'}{'aes192gcm64'} = '';
2446         $checked{'IKE_ENCRYPTION'}{'aes128gcm64'} = '';
2447         $checked{'IKE_ENCRYPTION'}{'3des'} = '';
2448         $checked{'IKE_ENCRYPTION'}{'camellia256'} = '';
2449         $checked{'IKE_ENCRYPTION'}{'camellia192'} = '';
2450         $checked{'IKE_ENCRYPTION'}{'camellia128'} = '';
2451         my @temp = split('\|', $cgiparams{'IKE_ENCRYPTION'});
2452         foreach my $key (@temp) {$checked{'IKE_ENCRYPTION'}{$key} = "selected='selected'"; }
2453         $checked{'IKE_INTEGRITY'}{'sha2_512'} = '';
2454         $checked{'IKE_INTEGRITY'}{'sha2_384'} = '';
2455         $checked{'IKE_INTEGRITY'}{'sha2_256'} = '';
2456         $checked{'IKE_INTEGRITY'}{'sha'} = '';
2457         $checked{'IKE_INTEGRITY'}{'md5'} = '';
2458         $checked{'IKE_INTEGRITY'}{'aesxcbc'} = '';
2459         @temp = split('\|', $cgiparams{'IKE_INTEGRITY'});
2460         foreach my $key (@temp) {$checked{'IKE_INTEGRITY'}{$key} = "selected='selected'"; }
2461         $checked{'IKE_GROUPTYPE'}{'curve25519'} = '';
2462         $checked{'IKE_GROUPTYPE'}{'768'} = '';
2463         $checked{'IKE_GROUPTYPE'}{'1024'} = '';
2464         $checked{'IKE_GROUPTYPE'}{'1536'} = '';
2465         $checked{'IKE_GROUPTYPE'}{'2048'} = '';
2466         $checked{'IKE_GROUPTYPE'}{'3072'} = '';
2467         $checked{'IKE_GROUPTYPE'}{'4096'} = '';
2468         $checked{'IKE_GROUPTYPE'}{'6144'} = '';
2469         $checked{'IKE_GROUPTYPE'}{'8192'} = '';
2470         @temp = split('\|', $cgiparams{'IKE_GROUPTYPE'});
2471         foreach my $key (@temp) {$checked{'IKE_GROUPTYPE'}{$key} = "selected='selected'"; }
2472
2473         $checked{'ESP_ENCRYPTION'}{'chacha20poly1305'} = '';
2474         $checked{'ESP_ENCRYPTION'}{'aes256'} = '';
2475         $checked{'ESP_ENCRYPTION'}{'aes192'} = '';
2476         $checked{'ESP_ENCRYPTION'}{'aes128'} = '';
2477         $checked{'ESP_ENCRYPTION'}{'aes256gcm128'} = '';
2478         $checked{'ESP_ENCRYPTION'}{'aes192gcm128'} = '';
2479         $checked{'ESP_ENCRYPTION'}{'aes128gcm128'} = '';
2480         $checked{'ESP_ENCRYPTION'}{'aes256gcm96'} = '';
2481         $checked{'ESP_ENCRYPTION'}{'aes192gcm96'} = '';
2482         $checked{'ESP_ENCRYPTION'}{'aes128gcm96'} = '';
2483         $checked{'ESP_ENCRYPTION'}{'aes256gcm64'} = '';
2484         $checked{'ESP_ENCRYPTION'}{'aes192gcm64'} = '';
2485         $checked{'ESP_ENCRYPTION'}{'aes128gcm64'} = '';
2486         $checked{'ESP_ENCRYPTION'}{'3des'} = '';
2487         $checked{'ESP_ENCRYPTION'}{'camellia256'} = '';
2488         $checked{'ESP_ENCRYPTION'}{'camellia192'} = '';
2489         $checked{'ESP_ENCRYPTION'}{'camellia128'} = '';
2490         @temp = split('\|', $cgiparams{'ESP_ENCRYPTION'});
2491         foreach my $key (@temp) {$checked{'ESP_ENCRYPTION'}{$key} = "selected='selected'"; }
2492         $checked{'ESP_INTEGRITY'}{'sha2_512'} = '';
2493         $checked{'ESP_INTEGRITY'}{'sha2_384'} = '';
2494         $checked{'ESP_INTEGRITY'}{'sha2_256'} = '';
2495         $checked{'ESP_INTEGRITY'}{'sha1'} = '';
2496         $checked{'ESP_INTEGRITY'}{'md5'} = '';
2497         $checked{'ESP_INTEGRITY'}{'aesxcbc'} = '';
2498         @temp = split('\|', $cgiparams{'ESP_INTEGRITY'});
2499         foreach my $key (@temp) {$checked{'ESP_INTEGRITY'}{$key} = "selected='selected'"; }
2500         $checked{'ESP_GROUPTYPE'}{'curve25519'} = '';
2501         $checked{'ESP_GROUPTYPE'}{'768'} = '';
2502         $checked{'ESP_GROUPTYPE'}{'1024'} = '';
2503         $checked{'ESP_GROUPTYPE'}{'1536'} = '';
2504         $checked{'ESP_GROUPTYPE'}{'2048'} = '';
2505         $checked{'ESP_GROUPTYPE'}{'3072'} = '';
2506         $checked{'ESP_GROUPTYPE'}{'4096'} = '';
2507         $checked{'ESP_GROUPTYPE'}{'6144'} = '';
2508         $checked{'ESP_GROUPTYPE'}{'8192'} = '';
2509         $checked{'ESP_GROUPTYPE'}{'none'} = '';
2510         @temp = split('\|', $cgiparams{'ESP_GROUPTYPE'});
2511         foreach my $key (@temp) {$checked{'ESP_GROUPTYPE'}{$key} = "selected='selected'"; }
2512
2513         $checked{'COMPRESSION'} = $cgiparams{'COMPRESSION'} eq 'on' ? "checked='checked'" : '' ;
2514         $checked{'FORCE_MOBIKE'} = $cgiparams{'FORCE_MOBIKE'} eq 'on' ? "checked='checked'" : '' ;
2515         $checked{'ONLY_PROPOSED'} = $cgiparams{'ONLY_PROPOSED'} eq 'on' ? "checked='checked'" : '' ;
2516         $checked{'PFS'} = $cgiparams{'PFS'} eq 'on' ? "checked='checked'" : '' ;
2517
2518         $selected{'IKE_VERSION'}{'ikev1'} = '';
2519         $selected{'IKE_VERSION'}{'ikev2'} = '';
2520         $selected{'IKE_VERSION'}{$cgiparams{'IKE_VERSION'}} = "selected='selected'";
2521
2522         $selected{'DPD_ACTION'}{'clear'} = '';
2523         $selected{'DPD_ACTION'}{'hold'} = '';
2524         $selected{'DPD_ACTION'}{'restart'} = '';
2525         $selected{'DPD_ACTION'}{'none'} = '';
2526         $selected{'DPD_ACTION'}{$cgiparams{'DPD_ACTION'}} = "selected='selected'";
2527
2528         $selected{'START_ACTION'}{'add'} = '';
2529         $selected{'START_ACTION'}{'route'} = '';
2530         $selected{'START_ACTION'}{'start'} = '';
2531         $selected{'START_ACTION'}{$cgiparams{'START_ACTION'}} = "selected='selected'";
2532
2533         $selected{'INACTIVITY_TIMEOUT'} = ();
2534         foreach my $timeout (keys %INACTIVITY_TIMEOUTS) {
2535                 $selected{'INACTIVITY_TIMEOUT'}{$timeout} = "";
2536         }
2537         $selected{'INACTIVITY_TIMEOUT'}{$cgiparams{'INACTIVITY_TIMEOUT'}} = "selected";
2538
2539         $selected{'MODE'}{'tunnel'} = '';
2540         $selected{'MODE'}{'transport'} = '';
2541         $selected{'MODE'}{$cgiparams{'MODE'}} = "selected='selected'";
2542
2543         $selected{'INTERFACE_MODE'}{''} = '';
2544         $selected{'INTERFACE_MODE'}{'gre'} = '';
2545         $selected{'INTERFACE_MODE'}{'vti'} = '';
2546         $selected{'INTERFACE_MODE'}{$cgiparams{'INTERFACE_MODE'}} = "selected='selected'";
2547
2548         &Header::showhttpheaders();
2549         &Header::openpage($Lang::tr{'ipsec'}, 1, '');
2550         &Header::openbigbox('100%', 'left', '', $errormessage);
2551
2552         if ($errormessage) {
2553                 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
2554                 print "<class name='base'>$errormessage";
2555                 print "&nbsp;</class>";
2556                 &Header::closebox();
2557         }
2558
2559         if ($warnmessage) {
2560                 &Header::openbox('100%', 'left', $Lang::tr{'warning messages'});
2561                 print "<class name='base'>$warnmessage";
2562                 print "&nbsp;</class>";
2563                 &Header::closebox();
2564         }
2565
2566         &Header::openbox('100%', 'left', "$Lang::tr{'advanced'}:");
2567         print <<EOF;
2568         <form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'>
2569         <input type='hidden' name='ADVANCED' value='yes' />
2570         <input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />
2571
2572         <table width="100%">
2573                 <tbody>
2574                         <tr>
2575                                 <td width="15%">$Lang::tr{'mode'}:</td>
2576                                 <td>
2577                                         <select name='MODE'>
2578                                                 <option value='tunnel' $selected{'MODE'}{'tunnel'}>$Lang::tr{'ipsec mode tunnel'}</option>
2579                                                 <option value='transport' $selected{'MODE'}{'transport'}>$Lang::tr{'ipsec mode transport'}</option>
2580                                         </select>
2581                                 </td>
2582                                 <td></td>
2583                         </tr>
2584
2585                         <tr>
2586                                 <td width="15%">$Lang::tr{'interface mode'}:</td>
2587                                 <td>
2588                                         <label></label>
2589                                         <select name='INTERFACE_MODE'>
2590                                                 <option value='' $selected{'INTERFACE_MODE'}{''}>$Lang::tr{'ipsec interface mode none'}</option>
2591                                                 <option value='gre' $selected{'INTERFACE_MODE'}{'gre'}>$Lang::tr{'ipsec interface mode gre'}</option>
2592                                                 <option value='vti' $selected{'INTERFACE_MODE'}{'vti'}>$Lang::tr{'ipsec interface mode vti'}</option>
2593                                         </select>
2594                                 </td>
2595                                 <td>
2596                                         <label>$Lang::tr{'ip address'}/$Lang::tr{'subnet mask'}</label>
2597                                         <input type="text" name="INTERFACE_ADDRESS" value="$cgiparams{'INTERFACE_ADDRESS'}">
2598                                 </td>
2599                                 <td>
2600                                         <label>$Lang::tr{'mtu'}</label>
2601                                         <input type="number" name="INTERFACE_MTU" value="$cgiparams{'INTERFACE_MTU'}" min="576" max="9000">
2602                                 </td>
2603                         </tr>
2604                 </tbody>
2605         </table>
2606
2607         <br><br>
2608
2609         <h2>$Lang::tr{'cryptographic settings'}</h2>
2610
2611         <table width='100%'>
2612         <thead>
2613                 <tr>
2614                         <th width="15%"></th>
2615                         <th>IKE</th>
2616                         <th>ESP</th>
2617                 </tr>
2618         </thead>
2619         <tbody>
2620                 <tr>
2621                         <td>$Lang::tr{'vpn keyexchange'}:</td>
2622                         <td>
2623                                 <select name='IKE_VERSION'>
2624                                         <option value='ikev2' $selected{'IKE_VERSION'}{'ikev2'}>IKEv2</option>
2625                                         <option value='ikev1' $selected{'IKE_VERSION'}{'ikev1'}>IKEv1</option>
2626                                 </select>
2627                         </td>
2628                         <td></td>
2629                 </tr>
2630                 <tr>
2631                         <td class='boldbase' width="15%">$Lang::tr{'encryption'}</td>
2632                         <td class='boldbase'>
2633                                 <select name='IKE_ENCRYPTION' multiple='multiple' size='6' style='width: 100%'>
2634                                         <option value='chacha20poly1305' $checked{'IKE_ENCRYPTION'}{'chacha20poly1305'}>256 bit ChaCha20-Poly1305/128 bit ICV</option>
2635                                         <option value='aes256gcm128' $checked{'IKE_ENCRYPTION'}{'aes256gcm128'}>256 bit AES-GCM/128 bit ICV</option>
2636                                         <option value='aes256gcm96' $checked{'IKE_ENCRYPTION'}{'aes256gcm96'}>256 bit AES-GCM/96 bit ICV</option>
2637                                         <option value='aes256gcm64' $checked{'IKE_ENCRYPTION'}{'aes256gcm64'}>256 bit AES-GCM/64 bit ICV</option>
2638                                         <option value='aes256' $checked{'IKE_ENCRYPTION'}{'aes256'}>256 bit AES-CBC</option>
2639                                         <option value='camellia256' $checked{'IKE_ENCRYPTION'}{'camellia256'}>256 bit Camellia-CBC</option>
2640                                         <option value='aes192gcm128' $checked{'IKE_ENCRYPTION'}{'aes192gcm128'}>192 bit AES-GCM/128 bit ICV</option>
2641                                         <option value='aes192gcm96' $checked{'IKE_ENCRYPTION'}{'aes192gcm96'}>192 bit AES-GCM/96 bit ICV</option>
2642                                         <option value='aes192gcm64' $checked{'IKE_ENCRYPTION'}{'aes192gcm64'}>192 bit AES-GCM/64 bit ICV</option>
2643                                         <option value='aes192' $checked{'IKE_ENCRYPTION'}{'aes192'}>192 bit AES-CBC</option>
2644                                         <option value='camellia192' $checked{'IKE_ENCRYPTION'}{'camellia192'}>192 bit Camellia-CBC</option>
2645                                         <option value='aes128gcm128' $checked{'IKE_ENCRYPTION'}{'aes128gcm128'}>128 bit AES-GCM/128 bit ICV</option>
2646                                         <option value='aes128gcm96' $checked{'IKE_ENCRYPTION'}{'aes128gcm96'}>128 bit AES-GCM/96 bit ICV</option>
2647                                         <option value='aes128gcm64' $checked{'IKE_ENCRYPTION'}{'aes128gcm64'}>128 bit AES-GCM/64 bit ICV</option>
2648                                         <option value='aes128' $checked{'IKE_ENCRYPTION'}{'aes128'}>128 bit AES-CBC</option>
2649                                         <option value='camellia128' $checked{'IKE_ENCRYPTION'}{'camellia128'}>128 bit Camellia-CBC</option>
2650                                         <option value='3des' $checked{'IKE_ENCRYPTION'}{'3des'}>168 bit 3DES-EDE-CBC ($Lang::tr{'vpn weak'})</option>
2651                                 </select>
2652                         </td>
2653                         <td class='boldbase'>
2654                                 <select name='ESP_ENCRYPTION' multiple='multiple' size='6' style='width: 100%'>
2655                                         <option value='chacha20poly1305' $checked{'ESP_ENCRYPTION'}{'chacha20poly1305'}>256 bit ChaCha20-Poly1305/128 bit ICV</option>
2656                                         <option value='aes256gcm128' $checked{'ESP_ENCRYPTION'}{'aes256gcm128'}>256 bit AES-GCM/128 bit ICV</option>
2657                                         <option value='aes256gcm96' $checked{'ESP_ENCRYPTION'}{'aes256gcm96'}>256 bit AES-GCM/96 bit ICV</option>
2658                                         <option value='aes256gcm64' $checked{'ESP_ENCRYPTION'}{'aes256gcm64'}>256 bit AES-GCM/64 bit ICV</option>
2659                                         <option value='aes256' $checked{'ESP_ENCRYPTION'}{'aes256'}>256 bit AES-CBC</option>
2660                                         <option value='camellia256' $checked{'ESP_ENCRYPTION'}{'camellia256'}>256 bit Camellia-CBC</option>
2661                                         <option value='aes192gcm128' $checked{'ESP_ENCRYPTION'}{'aes192gcm128'}>192 bit AES-GCM/128 bit ICV</option>
2662                                         <option value='aes192gcm96' $checked{'ESP_ENCRYPTION'}{'aes192gcm96'}>192 bit AES-GCM/96 bit ICV</option>
2663                                         <option value='aes192gcm64' $checked{'ESP_ENCRYPTION'}{'aes192gcm64'}>192 bit AES-GCM/64 bit ICV</option>
2664                                         <option value='aes192' $checked{'ESP_ENCRYPTION'}{'aes192'}>192 bit AES-CBC</option>
2665                                         <option value='camellia192' $checked{'ESP_ENCRYPTION'}{'camellia192'}>192 bit Camellia-CBC</option>
2666                                         <option value='aes128gcm128' $checked{'ESP_ENCRYPTION'}{'aes128gcm128'}>128 bit AES-GCM/128 bit ICV</option>
2667                                         <option value='aes128gcm96' $checked{'ESP_ENCRYPTION'}{'aes128gcm96'}>128 bit AES-GCM/96 bit ICV</option>
2668                                         <option value='aes128gcm64' $checked{'ESP_ENCRYPTION'}{'aes128gcm64'}>128 bit AES-GCM/64 bit ICV</option>
2669                                         <option value='aes128' $checked{'ESP_ENCRYPTION'}{'aes128'}>128 bit AES-CBC</option>
2670                                         <option value='camellia128' $checked{'ESP_ENCRYPTION'}{'camellia128'}>128 bit Camellia-CBC</option>
2671                                         <option value='3des' $checked{'ESP_ENCRYPTION'}{'3des'}>168 bit 3DES-EDE-CBC ($Lang::tr{'vpn weak'})</option>
2672                                 </select>
2673                         </td>
2674                 </tr>
2675
2676                 <tr>
2677                         <td class='boldbase' width="15%">$Lang::tr{'integrity'}</td>
2678                         <td class='boldbase'>
2679                                 <select name='IKE_INTEGRITY' multiple='multiple' size='6' style='width: 100%'>
2680                                         <option value='sha2_512' $checked{'IKE_INTEGRITY'}{'sha2_512'}>SHA2 512 bit</option>
2681                                         <option value='sha2_384' $checked{'IKE_INTEGRITY'}{'sha2_384'}>SHA2 384 bit</option>
2682                                         <option value='sha2_256' $checked{'IKE_INTEGRITY'}{'sha2_256'}>SHA2 256 bit</option>
2683                                         <option value='aesxcbc' $checked{'IKE_INTEGRITY'}{'aesxcbc'}>AES XCBC</option>
2684                                         <option value='sha' $checked{'IKE_INTEGRITY'}{'sha'}>SHA1 ($Lang::tr{'vpn weak'})</option>
2685                                         <option value='md5' $checked{'IKE_INTEGRITY'}{'md5'}>MD5 ($Lang::tr{'vpn broken'})</option>
2686                                 </select>
2687                         </td>
2688                         <td class='boldbase'>
2689                                 <select name='ESP_INTEGRITY' multiple='multiple' size='6' style='width: 100%'>
2690                                         <option value='sha2_512' $checked{'ESP_INTEGRITY'}{'sha2_512'}>SHA2 512 bit</option>
2691                                         <option value='sha2_384' $checked{'ESP_INTEGRITY'}{'sha2_384'}>SHA2 384 bit</option>
2692                                         <option value='sha2_256' $checked{'ESP_INTEGRITY'}{'sha2_256'}>SHA2 256 bit</option>
2693                                         <option value='aesxcbc' $checked{'ESP_INTEGRITY'}{'aesxcbc'}>AES XCBC</option>
2694                                         <option value='sha1' $checked{'ESP_INTEGRITY'}{'sha1'}>SHA1 ($Lang::tr{'vpn weak'})</option>
2695                                         <option value='md5' $checked{'ESP_INTEGRITY'}{'md5'}>MD5 ($Lang::tr{'vpn broken'})</option>
2696                                 </select>
2697                         </td>
2698                 </tr>
2699                 <tr>
2700                         <td class='boldbase' width="15%">$Lang::tr{'lifetime'}&nbsp;<img src='/blob.gif' alt='*' /></td>
2701                         <td class='boldbase'>
2702                                 <input type='text' name='IKE_LIFETIME' value='$cgiparams{'IKE_LIFETIME'}' size='5' /> $Lang::tr{'hours'}
2703                         </td>
2704                         <td class='boldbase'>
2705                                 <input type='text' name='ESP_KEYLIFE' value='$cgiparams{'ESP_KEYLIFE'}' size='5' /> $Lang::tr{'hours'}
2706                         </td>
2707                 </tr>
2708                 <tr>
2709                         <td class='boldbase' width="15%">$Lang::tr{'grouptype'}</td>
2710                         <td class='boldbase'>
2711                                 <select name='IKE_GROUPTYPE' multiple='multiple' size='6' style='width: 100%'>
2712                                         <option value='curve25519' $checked{'IKE_GROUPTYPE'}{'curve25519'}>Curve 25519 (256 bit)</option>
2713                                         <option value='e521' $checked{'IKE_GROUPTYPE'}{'e521'}>ECP-521 (NIST)</option>
2714                                         <option value='e512bp' $checked{'IKE_GROUPTYPE'}{'e512bp'}>ECP-512 (Brainpool)</option>
2715                                         <option value='e384' $checked{'IKE_GROUPTYPE'}{'e384'}>ECP-384 (NIST)</option>
2716                                         <option value='e384bp' $checked{'IKE_GROUPTYPE'}{'e384bp'}>ECP-384 (Brainpool)</option>
2717                                         <option value='e256' $checked{'IKE_GROUPTYPE'}{'e256'}>ECP-256 (NIST)</option>
2718                                         <option value='e256bp' $checked{'IKE_GROUPTYPE'}{'e256bp'}>ECP-256 (Brainpool)</option>
2719                                         <option value='e224' $checked{'IKE_GROUPTYPE'}{'e224'}>ECP-224 (NIST)</option>
2720                                         <option value='e224bp' $checked{'IKE_GROUPTYPE'}{'e224bp'}>ECP-224 (Brainpool)</option>
2721                                         <option value='e192' $checked{'IKE_GROUPTYPE'}{'e192'}>ECP-192 (NIST)</option>
2722                                         <option value='8192' $checked{'IKE_GROUPTYPE'}{'8192'}>MODP-8192</option>
2723                                         <option value='6144' $checked{'IKE_GROUPTYPE'}{'6144'}>MODP-6144</option>
2724                                         <option value='4096' $checked{'IKE_GROUPTYPE'}{'4096'}>MODP-4096</option>
2725                                         <option value='3072' $checked{'IKE_GROUPTYPE'}{'3072'}>MODP-3072</option>
2726                                         <option value='2048' $checked{'IKE_GROUPTYPE'}{'2048'}>MODP-2048</option>
2727                                         <option value='1536' $checked{'IKE_GROUPTYPE'}{'1536'}>MODP-1536</option>
2728                                         <option value='1024' $checked{'IKE_GROUPTYPE'}{'1024'}>MODP-1024 ($Lang::tr{'vpn broken'})</option>
2729                                         <option value='768' $checked{'IKE_GROUPTYPE'}{'768'}>MODP-768 ($Lang::tr{'vpn broken'})</option>
2730                                 </select>
2731                         </td>
2732               &nbs