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