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