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