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