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