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