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