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