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