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