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