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