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