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