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