]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/vpnmain.cgi
Some changes for strongswan.
[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');
6652626c
AF
251 #$interfaces .= "ipsec1=$netsettings{'GREEN_DEV'} " if ($interfaces !~ /ipsec1/ && $lconfighash{$key}[26] eq 'GREEN');
252 #$interfaces .= "ipsec2=$netsettings{'BLUE_DEV'} " if ($interfaces !~ /ipsec2/ && $lconfighash{$key}[26] eq 'BLUE');
253 #$interfaces .= "ipsec3=$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
MT
583 &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
584 system('/usr/local/bin/ipsecctrl', 'R');
585 sleep $sleepDelay;
586
587 UPLOADCA_ERROR:
588
589###
590### Display ca certificate
591###
592} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show ca certificate'}) {
593 &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
594
595 if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem") {
596 &Header::showhttpheaders();
597 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
ed84e8b8
MT
598 &Header::openbigbox('100%', 'left', '', '');
599 &Header::openbox('100%', 'left', "$Lang::tr{'ca certificate'}:");
ac1cfefa
MT
600 my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
601 $output = &Header::cleanhtml($output,"y");
602 print "<pre>$output</pre>\n";
603 &Header::closebox();
604 print "<div align='center'><a href='/cgi-bin/vpnmain.cgi'>$Lang::tr{'back'}</a></div>";
605 &Header::closebigbox();
606 &Header::closepage();
607 exit(0);
608 } else {
609 $errormessage = $Lang::tr{'invalid key'};
610 }
611
612###
ed84e8b8 613### Export ca certificate to browser
ac1cfefa
MT
614###
615} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download ca certificate'}) {
616 &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
617
618 if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
ed84e8b8 619 print "Content-Type: application/force-download\n";
ac1cfefa 620 print "Content-Type: application/octet-stream\r\n";
ed84e8b8 621 print "Content-Disposition: attachment; filename=$cahash{$cgiparams{'KEY'}}[0]cert.pem\r\n\r\n";
ac1cfefa
MT
622 print `/usr/bin/openssl x509 -in ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
623 exit(0);
624 } else {
625 $errormessage = $Lang::tr{'invalid key'};
626 }
627
628###
629### Remove ca certificate (step 2)
630###
631} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'} && $cgiparams{'AREUSURE'} eq 'yes') {
632 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
633 &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
634
635 if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
636 foreach my $key (keys %confighash) {
637 my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem ${General::swroot}/certs/$confighash{$key}[1]cert.pem`;
638 if ($test =~ /: OK/) {
639 # Delete connection
ed84e8b8 640 system('/usr/local/bin/ipsecctrl', 'D', $key) if (&vpnenabled);
ac1cfefa
MT
641 unlink ("${General::swroot}/certs/$confighash{$key}[1]cert.pem");
642 unlink ("${General::swroot}/certs/$confighash{$key}[1].p12");
643 delete $confighash{$key};
644 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
645 &writeipsecfiles();
646 }
647 }
648 unlink ("${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
649 delete $cahash{$cgiparams{'KEY'}};
650 &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
651 system('/usr/local/bin/ipsecctrl', 'R');
652 sleep $sleepDelay;
653 } else {
654 $errormessage = $Lang::tr{'invalid key'};
655 }
656###
657### Remove ca certificate (step 1)
658###
659} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'}) {
660 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
661 &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
662
663 my $assignedcerts = 0;
664 if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
665 foreach my $key (keys %confighash) {
666 my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem ${General::swroot}/certs/$confighash{$key}[1]cert.pem`;
667 if ($test =~ /: OK/) {
668 $assignedcerts++;
669 }
670 }
671 if ($assignedcerts) {
672 &Header::showhttpheaders();
673 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
ed84e8b8
MT
674 &Header::openbigbox('100%', 'left', '', '');
675 &Header::openbox('100%', 'left', $Lang::tr{'are you sure'});
ac1cfefa 676 print <<END
ed84e8b8
MT
677 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
678 <table width='100%'>
679 <tr>
680 <td align='center'>
681 <input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />
682 <input type='hidden' name='AREUSURE' value='yes' /></td>
683 </tr><tr>
684 <td align='center'>
685 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>
686 $Lang::tr{'connections are associated with this ca. deleting the ca will delete these connections as well.'}</td>
687 </tr><tr>
688 <td align='center'>
689 <input type='submit' name='ACTION' value='$Lang::tr{'remove ca certificate'}' />
690 <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></td>
691 </tr>
692 </table>
693 </form>
ac1cfefa
MT
694END
695 ;
696 &Header::closebox();
697 &Header::closebigbox();
698 &Header::closepage();
699 exit (0);
700 } else {
701 unlink ("${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
702 delete $cahash{$cgiparams{'KEY'}};
703 &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
704 system('/usr/local/bin/ipsecctrl', 'R');
705 sleep $sleepDelay;
706 }
707 } else {
708 $errormessage = $Lang::tr{'invalid key'};
709 }
710
711###
712### Display root certificate
713###
714} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'} ||
715 $cgiparams{'ACTION'} eq $Lang::tr{'show host certificate'}) {
716 my $output;
717 &Header::showhttpheaders();
718 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
ed84e8b8 719 &Header::openbigbox('100%', 'left', '', '');
ac1cfefa 720 if ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'}) {
ed84e8b8 721 &Header::openbox('100%', 'left', "$Lang::tr{'root certificate'}:");
ac1cfefa
MT
722 $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ca/cacert.pem`;
723 } else {
ed84e8b8 724 &Header::openbox('100%', 'left', "$Lang::tr{'host certificate'}:");
ac1cfefa
MT
725 $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/certs/hostcert.pem`;
726 }
727 $output = &Header::cleanhtml($output,"y");
728 print "<pre>$output</pre>\n";
729 &Header::closebox();
730 print "<div align='center'><a href='/cgi-bin/vpnmain.cgi'>$Lang::tr{'back'}</a></div>";
731 &Header::closebigbox();
732 &Header::closepage();
733 exit(0);
734
735###
ed84e8b8 736### Export root certificate to browser
ac1cfefa
MT
737###
738} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download root certificate'}) {
739 if ( -f "${General::swroot}/ca/cacert.pem" ) {
ed84e8b8
MT
740 print "Content-Type: application/force-download\n";
741 print "Content-Disposition: attachment; filename=cacert.pem\r\n\r\n";
ac1cfefa
MT
742 print `/usr/bin/openssl x509 -in ${General::swroot}/ca/cacert.pem`;
743 exit(0);
744 }
745###
ed84e8b8 746### Export host certificate to browser
ac1cfefa
MT
747###
748} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download host certificate'}) {
749 if ( -f "${General::swroot}/certs/hostcert.pem" ) {
ed84e8b8
MT
750 print "Content-Type: application/force-download\n";
751 print "Content-Disposition: attachment; filename=hostcert.pem\r\n\r\n";
ac1cfefa
MT
752 print `/usr/bin/openssl x509 -in ${General::swroot}/certs/hostcert.pem`;
753 exit(0);
754 }
755###
ed84e8b8 756### Form for generating/importing the caroot+host certificate
ac1cfefa
MT
757###
758} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'generate root/host certificates'} ||
759 $cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
760
ac1cfefa
MT
761 if (-f "${General::swroot}/ca/cacert.pem") {
762 $errormessage = $Lang::tr{'valid root certificate already exists'};
ed84e8b8 763 goto ROOTCERT_SKIP;
ac1cfefa
MT
764 }
765
ed84e8b8
MT
766 &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
767 # fill in initial values
768 if ($cgiparams{'ROOTCERT_HOSTNAME'} eq '') {
769 if (-e "${General::swroot}/red/active" && open(IPADDR, "${General::swroot}/red/local-ipaddress")) {
ac1cfefa
MT
770 my $ipaddr = <IPADDR>;
771 close IPADDR;
772 chomp ($ipaddr);
773 $cgiparams{'ROOTCERT_HOSTNAME'} = (gethostbyaddr(pack("C4", split(/\./, $ipaddr)), 2))[0];
774 if ($cgiparams{'ROOTCERT_HOSTNAME'} eq '') {
775 $cgiparams{'ROOTCERT_HOSTNAME'} = $ipaddr;
776 }
777 }
ed84e8b8 778 $cgiparams{'ROOTCERT_COUNTRY'} = $vpnsettings{'ROOTCERT_COUNTRY'} if (!$cgiparams{'ROOTCERT_COUNTRY'});
ac1cfefa 779 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
ed84e8b8 780 &General::log("ipsec", "Importing from p12...");
ac1cfefa
MT
781
782 if (ref ($cgiparams{'FH'}) ne 'Fh') {
783 $errormessage = $Lang::tr{'there was no file upload'};
784 goto ROOTCERT_ERROR;
785 }
786
787 # Move uploaded certificate request to a temporary file
788 (my $fh, my $filename) = tempfile( );
789 if (copy ($cgiparams{'FH'}, $fh) != 1) {
790 $errormessage = $!;
791 goto ROOTCERT_ERROR;
792 }
793
ac1cfefa 794 # Extract the CA certificate from the file
ed84e8b8
MT
795 &General::log("ipsec", "Extracting caroot from p12...");
796 if (open(STDIN, "-|")) {
797 my $opt = " pkcs12 -cacerts -nokeys";
798 $opt .= " -in $filename";
799 $opt .= " -out /tmp/newcacert";
800 $errormessage = &callssl ($opt);
801 } else { #child
802 print "$cgiparams{'P12_PASS'}\n";
803 exit (0);
ac1cfefa
MT
804 }
805
ed84e8b8
MT
806 # Extract the Host certificate from the file
807 if (!$errormessage) {
808 &General::log("ipsec", "Extracting host cert from p12...");
809 if (open(STDIN, "-|")) {
810 my $opt = " pkcs12 -clcerts -nokeys";
811 $opt .= " -in $filename";
812 $opt .= " -out /tmp/newhostcert";
813 $errormessage = &callssl ($opt);
814 } else { #child
815 print "$cgiparams{'P12_PASS'}\n";
816 exit (0);
ac1cfefa
MT
817 }
818 }
819
820 # Extract the Host key from the file
ed84e8b8
MT
821 if (!$errormessage) {
822 &General::log("ipsec", "Extracting private key from p12...");
823 if (open(STDIN, "-|")) {
824 my $opt = " pkcs12 -nocerts -nodes";
825 $opt .= " -in $filename";
826 $opt .= " -out /tmp/newhostkey";
827 $errormessage = &callssl ($opt);
828 } else { #child
829 print "$cgiparams{'P12_PASS'}\n";
830 exit (0);
831 }
832 }
833
834 if (!$errormessage) {
835 &General::log("ipsec", "Moving cacert...");
836 move("/tmp/newcacert", "${General::swroot}/ca/cacert.pem");
837 $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
838 }
839
840 if (!$errormessage) {
841 &General::log("ipsec", "Moving host cert...");
842 move("/tmp/newhostcert", "${General::swroot}/certs/hostcert.pem");
843 $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
ac1cfefa
MT
844 }
845
ed84e8b8
MT
846 if (!$errormessage) {
847 &General::log("ipsec", "Moving private key...");
848 move("/tmp/newhostkey", "${General::swroot}/certs/hostkey.pem");
849 $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
ac1cfefa 850 }
ed84e8b8
MT
851
852 #cleanup temp files
853 unlink ($filename);
854 unlink ('/tmp/newcacert');
855 unlink ('/tmp/newhostcert');
856 unlink ('/tmp/newhostkey');
857 if ($errormessage) {
ac1cfefa
MT
858 unlink ("${General::swroot}/ca/cacert.pem");
859 unlink ("${General::swroot}/certs/hostcert.pem");
860 unlink ("${General::swroot}/certs/hostkey.pem");
861 goto ROOTCERT_ERROR;
ac1cfefa
MT
862 }
863
ed84e8b8
MT
864 # Create empty CRL cannot be done because we don't have
865 # the private key for this CAROOT
5fd30232 866 # IPFire can only import certificates
ed84e8b8
MT
867
868 &General::log("ipsec", "p12 import completed!");
869 &cleanssldatabase();
ac1cfefa
MT
870 goto ROOTCERT_SUCCESS;
871
872 } elsif ($cgiparams{'ROOTCERT_COUNTRY'} ne '') {
873
874 # Validate input since the form was submitted
875 if ($cgiparams{'ROOTCERT_ORGANIZATION'} eq ''){
876 $errormessage = $Lang::tr{'organization cant be empty'};
877 goto ROOTCERT_ERROR;
878 }
879 if (length($cgiparams{'ROOTCERT_ORGANIZATION'}) >60) {
880 $errormessage = $Lang::tr{'organization too long'};
881 goto ROOTCERT_ERROR;
882 }
883 if ($cgiparams{'ROOTCERT_ORGANIZATION'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
884 $errormessage = $Lang::tr{'invalid input for organization'};
885 goto ROOTCERT_ERROR;
886 }
887 if ($cgiparams{'ROOTCERT_HOSTNAME'} eq ''){
888 $errormessage = $Lang::tr{'hostname cant be empty'};
889 goto ROOTCERT_ERROR;
890 }
891 unless (&General::validfqdn($cgiparams{'ROOTCERT_HOSTNAME'}) || &General::validip($cgiparams{'ROOTCERT_HOSTNAME'})) {
892 $errormessage = $Lang::tr{'invalid input for hostname'};
893 goto ROOTCERT_ERROR;
894 }
895 if ($cgiparams{'ROOTCERT_EMAIL'} ne '' && (! &General::validemail($cgiparams{'ROOTCERT_EMAIL'}))) {
896 $errormessage = $Lang::tr{'invalid input for e-mail address'};
897 goto ROOTCERT_ERROR;
898 }
899 if (length($cgiparams{'ROOTCERT_EMAIL'}) > 40) {
900 $errormessage = $Lang::tr{'e-mail address too long'};
901 goto ROOTCERT_ERROR;
902 }
903 if ($cgiparams{'ROOTCERT_OU'} ne '' && $cgiparams{'ROOTCERT_OU'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
904 $errormessage = $Lang::tr{'invalid input for department'};
905 goto ROOTCERT_ERROR;
906 }
907 if ($cgiparams{'ROOTCERT_CITY'} ne '' && $cgiparams{'ROOTCERT_CITY'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
908 $errormessage = $Lang::tr{'invalid input for city'};
909 goto ROOTCERT_ERROR;
910 }
911 if ($cgiparams{'ROOTCERT_STATE'} ne '' && $cgiparams{'ROOTCERT_STATE'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
912 $errormessage = $Lang::tr{'invalid input for state or province'};
913 goto ROOTCERT_ERROR;
914 }
915 if ($cgiparams{'ROOTCERT_COUNTRY'} !~ /^[A-Z]*$/) {
916 $errormessage = $Lang::tr{'invalid input for country'};
917 goto ROOTCERT_ERROR;
918 }
ed84e8b8
MT
919 #the exact syntax is a list comma separated of
920 # email:any-validemail
921 # URI: a uniform resource indicator
922 # DNS: a DNS domain name
923 # RID: a registered OBJECT IDENTIFIER
924 # IP: an IP address
925 # example: email:franck@foo.com,IP:10.0.0.10,DNS:franck.foo.com
926
927 if ($cgiparams{'SUBJECTALTNAME'} ne '' && $cgiparams{'SUBJECTALTNAME'} !~ /^(email|URI|DNS|RID|IP):[a-zA-Z0-9 :\/,\.\-_@]*$/) {
928 $errormessage = $Lang::tr{'vpn altname syntax'};
929 goto VPNCONF_ERROR;
930 }
ac1cfefa
MT
931
932 # Copy the cgisettings to vpnsettings and save the configfile
933 $vpnsettings{'ROOTCERT_ORGANIZATION'} = $cgiparams{'ROOTCERT_ORGANIZATION'};
934 $vpnsettings{'ROOTCERT_HOSTNAME'} = $cgiparams{'ROOTCERT_HOSTNAME'};
935 $vpnsettings{'ROOTCERT_EMAIL'} = $cgiparams{'ROOTCERT_EMAIL'};
936 $vpnsettings{'ROOTCERT_OU'} = $cgiparams{'ROOTCERT_OU'};
937 $vpnsettings{'ROOTCERT_CITY'} = $cgiparams{'ROOTCERT_CITY'};
938 $vpnsettings{'ROOTCERT_STATE'} = $cgiparams{'ROOTCERT_STATE'};
939 $vpnsettings{'ROOTCERT_COUNTRY'} = $cgiparams{'ROOTCERT_COUNTRY'};
940 &General::writehash("${General::swroot}/vpn/settings", \%vpnsettings);
941
942 # Replace empty strings with a .
943 (my $ou = $cgiparams{'ROOTCERT_OU'}) =~ s/^\s*$/\./;
944 (my $city = $cgiparams{'ROOTCERT_CITY'}) =~ s/^\s*$/\./;
945 (my $state = $cgiparams{'ROOTCERT_STATE'}) =~ s/^\s*$/\./;
946
947 # Create the CA certificate
ed84e8b8
MT
948 if (!$errormessage) {
949 &General::log("ipsec", "Creating cacert...");
950 if (open(STDIN, "-|")) {
951 my $opt = " req -x509 -nodes -rand /proc/interrupts:/proc/net/rt_cache";
952 $opt .= " -days 999999";
953 $opt .= " -newkey rsa:2048";
954 $opt .= " -keyout ${General::swroot}/private/cakey.pem";
955 $opt .= " -out ${General::swroot}/ca/cacert.pem";
956
957 $errormessage = &callssl ($opt);
958 } else { #child
959 print "$cgiparams{'ROOTCERT_COUNTRY'}\n";
960 print "$state\n";
961 print "$city\n";
962 print "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
963 print "$ou\n";
964 print "$cgiparams{'ROOTCERT_ORGANIZATION'} CA\n";
965 print "$cgiparams{'ROOTCERT_EMAIL'}\n";
966 exit (0);
ac1cfefa
MT
967 }
968 }
969
970 # Create the Host certificate request
ed84e8b8
MT
971 if (!$errormessage) {
972 &General::log("ipsec", "Creating host cert...");
973 if (open(STDIN, "-|")) {
974 my $opt = " req -nodes -rand /proc/interrupts:/proc/net/rt_cache";
975 $opt .= " -newkey rsa:1024";
976 $opt .= " -keyout ${General::swroot}/certs/hostkey.pem";
977 $opt .= " -out ${General::swroot}/certs/hostreq.pem";
978 $errormessage = &callssl ($opt);
979 } else { #child
980 print "$cgiparams{'ROOTCERT_COUNTRY'}\n";
981 print "$state\n";
982 print "$city\n";
983 print "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
984 print "$ou\n";
985 print "$cgiparams{'ROOTCERT_HOSTNAME'}\n";
986 print "$cgiparams{'ROOTCERT_EMAIL'}\n";
987 print ".\n";
988 print ".\n";
989 exit (0);
ac1cfefa
MT
990 }
991 }
ed84e8b8 992
ac1cfefa 993 # Sign the host certificate request
ed84e8b8
MT
994 if (!$errormessage) {
995 &General::log("ipsec", "Self signing host cert...");
996
997 #No easy way for specifying the contain of subjectAltName without writing a config file...
998 my ($fh, $v3extname) = tempfile ('/tmp/XXXXXXXX');
999 print $fh <<END
1000 basicConstraints=CA:FALSE
1001 nsComment="OpenSSL Generated Certificate"
1002 subjectKeyIdentifier=hash
1003 authorityKeyIdentifier=keyid,issuer:always
1004END
1005;
1006 print $fh "subjectAltName=$cgiparams{'SUBJECTALTNAME'}" if ($cgiparams{'SUBJECTALTNAME'});
1007 close ($fh);
1008
1009 my $opt = " ca -days 999999";
1010 $opt .= " -batch -notext";
1011 $opt .= " -in ${General::swroot}/certs/hostreq.pem";
1012 $opt .= " -out ${General::swroot}/certs/hostcert.pem";
1013 $opt .= " -extfile $v3extname";
1014 $errormessage = &callssl ($opt);
1015 unlink ("${General::swroot}/certs/hostreq.pem"); #no more needed
1016 unlink ($v3extname);
ac1cfefa
MT
1017 }
1018
1019 # Create an empty CRL
ed84e8b8
MT
1020 if (!$errormessage) {
1021 &General::log("ipsec", "Creating emptycrl...");
1022 my $opt = " ca -gencrl";
1023 $opt .= " -out ${General::swroot}/crls/cacrl.pem";
1024 $errormessage = &callssl ($opt);
1025 }
1026
1027 # Successfully build CA / CERT!
1028 if (!$errormessage) {
ac1cfefa 1029 &cleanssldatabase();
ed84e8b8 1030 goto ROOTCERT_SUCCESS;
ac1cfefa 1031 }
ed84e8b8
MT
1032
1033 #Cleanup
1034 unlink ("${General::swroot}/ca/cacert.pem");
1035 unlink ("${General::swroot}/certs/hostkey.pem");
1036 unlink ("${General::swroot}/certs/hostcert.pem");
1037 unlink ("${General::swroot}/crls/cacrl.pem");
1038 &cleanssldatabase();
ac1cfefa 1039 }
ed84e8b8 1040
ac1cfefa 1041 ROOTCERT_ERROR:
ed84e8b8
MT
1042 &Header::showhttpheaders();
1043 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1044 &Header::openbigbox('100%', 'left', '', $errormessage);
1045 if ($errormessage) {
1046 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
1047 print "<class name='base'>$errormessage";
1048 print "&nbsp;</class>";
1049 &Header::closebox();
1050 }
1051 &Header::openbox('100%', 'left', "$Lang::tr{'generate root/host certificates'}:");
1052 print <<END
1053 <form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'>
1054 <table width='100%' border='0' cellspacing='1' cellpadding='0'>
1055 <tr><td width='40%' class='base'>$Lang::tr{'organization name'}:</td>
1056 <td width='60%' class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_ORGANIZATION' value='$cgiparams{'ROOTCERT_ORGANIZATION'}' size='32' /></td></tr>
15f635cc 1057 <tr><td class='base'>$Lang::tr{'ipfires hostname'}:</td>
ed84e8b8
MT
1058 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_HOSTNAME' value='$cgiparams{'ROOTCERT_HOSTNAME'}' size='32' /></td></tr>
1059 <tr><td class='base'>$Lang::tr{'your e-mail'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
1060 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_EMAIL' value='$cgiparams{'ROOTCERT_EMAIL'}' size='32' /></td></tr>
1061 <tr><td class='base'>$Lang::tr{'your department'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
1062 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_OU' value='$cgiparams{'ROOTCERT_OU'}' size='32' /></td></tr>
1063 <tr><td class='base'>$Lang::tr{'city'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
1064 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_CITY' value='$cgiparams{'ROOTCERT_CITY'}' size='32' /></td></tr>
1065 <tr><td class='base'>$Lang::tr{'state or province'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
1066 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_STATE' value='$cgiparams{'ROOTCERT_STATE'}' size='32' /></td></tr>
1067 <tr><td class='base'>$Lang::tr{'country'}:</td>
1068 <td class='base'><select name='ROOTCERT_COUNTRY'>
ac1cfefa 1069END
ed84e8b8
MT
1070 ;
1071 foreach my $country (sort keys %{Countries::countries}) {
1072 print "<option value='$Countries::countries{$country}'";
1073 if ( $Countries::countries{$country} eq $cgiparams{'ROOTCERT_COUNTRY'} ) {
1074 print " selected='selected'";
1075 }
1076 print ">$country</option>";
ac1cfefa 1077 }
ed84e8b8
MT
1078 print <<END
1079 </select></td></tr>
1080 <tr><td class='base'>$Lang::tr{'vpn subjectaltname'} (subjectAltName=email:*,URI:*,DNS:*,RID:*) <img src='/blob.gif' alt='*' /></td>
1081 <td class='base' nowrap='nowrap'><input type='text' name='SUBJECTALTNAME' value='$cgiparams{'SUBJECTALTNAME'}' size='32' /></td></tr>
1082 <tr><td>&nbsp;</td>
1083 <td><br /><input type='submit' name='ACTION' value='$Lang::tr{'generate root/host certificates'}' /><br /><br /></td></tr>
1084 <tr><td class='base' colspan='2' align='left'>
1085 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>:
1086 $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'}
1087 </td></tr>
1088 <tr><td colspan='2'><hr /></td></tr>
1089 <tr><td class='base' nowrap='nowrap'>$Lang::tr{'upload p12 file'}:</td>
1090 <td nowrap='nowrap'><input type='file' name='FH' size='32' /></td></tr>
1091 <tr><td class='base'>$Lang::tr{'pkcs12 file password'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
1092 <td class='base' nowrap='nowrap'><input type='password' name='P12_PASS' value='$cgiparams{'P12_PASS'}' size='32' /></td></tr>
1093 <tr><td>&nbsp;</td>
1094 <td><input type='submit' name='ACTION' value='$Lang::tr{'upload p12 file'}' /></td></tr>
1095 <tr><td class='base' colspan='2' align='left'>
1096 <img src='/blob.gif' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td></tr>
1097 </table></form>
1098END
1099 ;
1100 &Header::closebox();
1101 &Header::closebigbox();
1102 &Header::closepage();
1103 exit(0);
ac1cfefa
MT
1104
1105 ROOTCERT_SUCCESS:
ed84e8b8 1106 if (&vpnenabled) {
ac1cfefa
MT
1107 system('/usr/local/bin/ipsecctrl', 'S');
1108 sleep $sleepDelay;
1109 }
ed84e8b8 1110 ROOTCERT_SKIP:
ac1cfefa 1111###
ed84e8b8 1112### Export PKCS12 file to browser
ac1cfefa
MT
1113###
1114} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download pkcs12 file'}) {
1115 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
ed84e8b8
MT
1116 print "Content-Type: application/force-download\n";
1117 print "Content-Disposition: attachment; filename=" . $confighash{$cgiparams{'KEY'}}[1] . ".p12\r\n";
ac1cfefa
MT
1118 print "Content-Type: application/octet-stream\r\n\r\n";
1119 print `/bin/cat ${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1].p12`;
1120 exit (0);
1121
1122###
1123### Display certificate
1124###
1125} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show certificate'}) {
1126 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1127
1128 if ( -f "${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem") {
1129 &Header::showhttpheaders();
1130 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
ed84e8b8
MT
1131 &Header::openbigbox('100%', 'left', '', '');
1132 &Header::openbox('100%', 'left', "$Lang::tr{'cert'}:");
ac1cfefa
MT
1133 my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
1134 $output = &Header::cleanhtml($output,"y");
1135 print "<pre>$output</pre>\n";
1136 &Header::closebox();
1137 print "<div align='center'><a href='/cgi-bin/vpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1138 &Header::closebigbox();
1139 &Header::closepage();
1140 exit(0);
1141 }
1142
1143###
ed84e8b8 1144### Export Certificate to browser
ac1cfefa
MT
1145###
1146} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download certificate'}) {
1147 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1148
1149 if ( -f "${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem") {
ed84e8b8
MT
1150 print "Content-Type: application/force-download\n";
1151 print "Content-Disposition: attachment; filename=" . $confighash{$cgiparams{'KEY'}}[1] . "cert.pem\n\n";
ac1cfefa
MT
1152 print `/bin/cat ${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
1153 exit (0);
1154 }
1155
1156###
1157### Enable/Disable connection
1158###
1159} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
1160
1161 &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
1162 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1163
1164 if ($confighash{$cgiparams{'KEY'}}) {
1165 if ($confighash{$cgiparams{'KEY'}}[0] eq 'off') {
1166 $confighash{$cgiparams{'KEY'}}[0] = 'on';
1167 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
1168 &writeipsecfiles();
ed84e8b8 1169 system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'}) if (&vpnenabled);
ac1cfefa 1170 } else {
5fd30232 1171 system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'}) if (&vpnenabled);
ac1cfefa 1172 $confighash{$cgiparams{'KEY'}}[0] = 'off';
ac1cfefa
MT
1173 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
1174 &writeipsecfiles();
1175 }
ed84e8b8 1176 sleep $sleepDelay;
ac1cfefa
MT
1177 } else {
1178 $errormessage = $Lang::tr{'invalid key'};
1179 }
1180
1181###
1182### Restart connection
1183###
1184} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'restart'}) {
1185 &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
1186 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1187
1188 if ($confighash{$cgiparams{'KEY'}}) {
ed84e8b8 1189 if (&vpnenabled) {
ac1cfefa
MT
1190 system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'});
1191 sleep $sleepDelay;
1192 }
1193 } else {
1194 $errormessage = $Lang::tr{'invalid key'};
1195 }
1196
1197###
1198### Remove connection
1199###
1200} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove'}) {
1201 &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
1202 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1203
1204 if ($confighash{$cgiparams{'KEY'}}) {
ed84e8b8 1205 system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'}) if (&vpnenabled);
ac1cfefa
MT
1206 unlink ("${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem");
1207 unlink ("${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1].p12");
1208 delete $confighash{$cgiparams{'KEY'}};
1209 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
1210 &writeipsecfiles();
1211 } else {
1212 $errormessage = $Lang::tr{'invalid key'};
1213 }
1214
1215###
1216### Choose between adding a host-net or net-net connection
1217###
1218} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'add'} && $cgiparams{'TYPE'} eq '') {
ac1cfefa
MT
1219 &Header::showhttpheaders();
1220 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
ed84e8b8
MT
1221 &Header::openbigbox('100%', 'left', '', '');
1222 &Header::openbox('100%', 'left', $Lang::tr{'connection type'});
ac1cfefa 1223 print <<END
ed84e8b8 1224 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
ac1cfefa 1225 <b>$Lang::tr{'connection type'}:</b><br />
ed84e8b8
MT
1226 <table>
1227 <tr><td><input type='radio' name='TYPE' value='host' checked='checked' /></td>
1228 <td class='base'>$Lang::tr{'host to net vpn'}</td>
1229 </tr><tr>
1230 <td><input type='radio' name='TYPE' value='net' /></td>
1231 <td class='base'>$Lang::tr{'net to net vpn'}</td>
1232 </tr><tr>
1233 <td align='center' colspan='2'><input type='submit' name='ACTION' value='$Lang::tr{'add'}' /></td>
1234 </tr>
1235 </table></form>
ac1cfefa
MT
1236END
1237 ;
1238 &Header::closebox();
1239 &Header::closebigbox();
1240 &Header::closepage();
1241 exit (0);
1242###
ed84e8b8 1243### Adding/Editing/Saving a connection
ac1cfefa
MT
1244###
1245} elsif (($cgiparams{'ACTION'} eq $Lang::tr{'add'}) ||
1246 ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) ||
1247 ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'ADVANCED'} eq '')) {
1248
1249 &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
1250 &General::readhasharray("${General::swroot}/vpn/caconfig", \%cahash);
1251 &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
1252
1253 if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) {
1254 if (! $confighash{$cgiparams{'KEY'}}[0]) {
1255 $errormessage = $Lang::tr{'invalid key'};
1256 goto VPNCONF_END;
1257 }
ed84e8b8
MT
1258 $cgiparams{'ENABLED'} = $confighash{$cgiparams{'KEY'}}[0];
1259 $cgiparams{'NAME'} = $confighash{$cgiparams{'KEY'}}[1];
1260 $cgiparams{'TYPE'} = $confighash{$cgiparams{'KEY'}}[3];
1261 $cgiparams{'AUTH'} = $confighash{$cgiparams{'KEY'}}[4];
1262 $cgiparams{'PSK'} = $confighash{$cgiparams{'KEY'}}[5];
5fd30232 1263 #$cgiparams{'free'} = $confighash{$cgiparams{'KEY'}}[6];
ed84e8b8
MT
1264 $cgiparams{'LOCAL_ID'} = $confighash{$cgiparams{'KEY'}}[7];
1265 $cgiparams{'LOCAL_SUBNET'} = $confighash{$cgiparams{'KEY'}}[8];
1266 $cgiparams{'REMOTE_ID'} = $confighash{$cgiparams{'KEY'}}[9];
1267 $cgiparams{'REMOTE'} = $confighash{$cgiparams{'KEY'}}[10];
1268 $cgiparams{'REMOTE_SUBNET'} = $confighash{$cgiparams{'KEY'}}[11];
1269 $cgiparams{'REMARK'} = $confighash{$cgiparams{'KEY'}}[25];
1270 $cgiparams{'INTERFACE'} = $confighash{$cgiparams{'KEY'}}[26];
1271 $cgiparams{'DPD_ACTION'} = $confighash{$cgiparams{'KEY'}}[27];
1272 $cgiparams{'IKE_ENCRYPTION'} = $confighash{$cgiparams{'KEY'}}[18];
1273 $cgiparams{'IKE_INTEGRITY'} = $confighash{$cgiparams{'KEY'}}[19];
1274 $cgiparams{'IKE_GROUPTYPE'} = $confighash{$cgiparams{'KEY'}}[20];
1275 $cgiparams{'IKE_LIFETIME'} = $confighash{$cgiparams{'KEY'}}[16];
1276 $cgiparams{'ESP_ENCRYPTION'} = $confighash{$cgiparams{'KEY'}}[21];
1277 $cgiparams{'ESP_INTEGRITY'} = $confighash{$cgiparams{'KEY'}}[22];
1278 $cgiparams{'ESP_GROUPTYPE'} = $confighash{$cgiparams{'KEY'}}[23];
1279 $cgiparams{'ESP_KEYLIFE'} = $confighash{$cgiparams{'KEY'}}[17];
1280 $cgiparams{'AGGRMODE'} = $confighash{$cgiparams{'KEY'}}[12];
1281 $cgiparams{'COMPRESSION'} = $confighash{$cgiparams{'KEY'}}[13];
1282 $cgiparams{'ONLY_PROPOSED'} = $confighash{$cgiparams{'KEY'}}[24];
1283 $cgiparams{'PFS'} = $confighash{$cgiparams{'KEY'}}[28];
1284 $cgiparams{'VHOST'} = $confighash{$cgiparams{'KEY'}}[14];
ac1cfefa
MT
1285
1286 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
1287 $cgiparams{'REMARK'} = &Header::cleanhtml($cgiparams{'REMARK'});
1288 if ($cgiparams{'TYPE'} !~ /^(host|net)$/) {
1289 $errormessage = $Lang::tr{'connection type is invalid'};
1290 goto VPNCONF_ERROR;
1291 }
1292
1293 if ($cgiparams{'NAME'} !~ /^[a-zA-Z0-9]+$/) {
1294 $errormessage = $Lang::tr{'name must only contain characters'};
1295 goto VPNCONF_ERROR;
1296 }
1297
1298 if ($cgiparams{'NAME'} =~ /^(host|01|block|private|clear|packetdefault)$/) {
1299 $errormessage = $Lang::tr{'name is invalid'};
1300 goto VPNCONF_ERROR;
1301 }
1302
1303 if (length($cgiparams{'NAME'}) >60) {
1304 $errormessage = $Lang::tr{'name too long'};
1305 goto VPNCONF_ERROR;
1306 }
1307
ac1cfefa 1308 # Check if there is no other entry with this name
ed84e8b8 1309 if (! $cgiparams{'KEY'}) { #only for add
ac1cfefa
MT
1310 foreach my $key (keys %confighash) {
1311 if ($confighash{$key}[1] eq $cgiparams{'NAME'}) {
1312 $errormessage = $Lang::tr{'a connection with this name already exists'};
1313 goto VPNCONF_ERROR;
1314 }
1315 }
1316 }
1317
1318 if (($cgiparams{'TYPE'} eq 'net') && (! $cgiparams{'REMOTE'})) {
1319 $errormessage = $Lang::tr{'invalid input for remote host/ip'};
1320 goto VPNCONF_ERROR;
1321 }
1322
1323 if ($cgiparams{'REMOTE'}) {
1324 if (! &General::validip($cgiparams{'REMOTE'})) {
1325 if (! &General::validfqdn ($cgiparams{'REMOTE'})) {
1326 $errormessage = $Lang::tr{'invalid input for remote host/ip'};
1327 goto VPNCONF_ERROR;
1328 } else {
1329 if (&valid_dns_host($cgiparams{'REMOTE'})) {
1330 $warnmessage = "$Lang::tr{'check vpn lr'} $cgiparams{'REMOTE'}. $Lang::tr{'dns check failed'}";
1331 }
1332 }
1333 }
1334 }
1335
1336 unless (&General::validipandmask($cgiparams{'LOCAL_SUBNET'})) {
1337 $errormessage = $Lang::tr{'local subnet is invalid'};
1338 goto VPNCONF_ERROR;
1339 }
1340
ed84e8b8
MT
1341 # Allow only one roadwarrior/psk without remote IP-address
1342 if ($cgiparams{'REMOTE'} eq '' && $cgiparams{'AUTH'} eq 'psk') {
ac1cfefa 1343 foreach my $key (keys %confighash) {
ed84e8b8
MT
1344 if ( ($cgiparams{'KEY'} ne $key) &&
1345 ($confighash{$key}[4] eq 'psk') &&
1346 ($confighash{$key}[10] eq '') ) {
ac1cfefa
MT
1347 $errormessage = $Lang::tr{'you can only define one roadwarrior connection when using pre-shared key authentication'};
1348 goto VPNCONF_ERROR;
1349 }
1350 }
1351 }
1352 if (($cgiparams{'TYPE'} eq 'net') && (! &General::validipandmask($cgiparams{'REMOTE_SUBNET'}))) {
1353 $errormessage = $Lang::tr{'remote subnet is invalid'};
1354 goto VPNCONF_ERROR;
1355 }
1356
1357 if ($cgiparams{'ENABLED'} !~ /^(on|off)$/) {
1358 $errormessage = $Lang::tr{'invalid input'};
1359 goto VPNCONF_ERROR;
1360 }
1361 if ($cgiparams{'EDIT_ADVANCED'} !~ /^(on|off)$/) {
1362 $errormessage = $Lang::tr{'invalid input'};
1363 goto VPNCONF_ERROR;
1364 }
1365
ed84e8b8
MT
1366 # Allow nothing or a string (DN,FDQN,) beginning with @
1367 # with no comma but slashes between RID eg @O=FR/C=Paris/OU=myhome/CN=franck
1368 if ( ($cgiparams{'LOCAL_ID'} !~ /^(|[\w.-]*@[\w. =*\/-]+|\d\.\d\.\d\.\d)$/) ||
1369 ($cgiparams{'REMOTE_ID'} !~ /^(|[\w.-]*@[\w. =*\/-]+|\d\.\d\.\d\.\d)$/) ||
ac1cfefa
MT
1370 (($cgiparams{'REMOTE_ID'} eq $cgiparams{'LOCAL_ID'}) && ($cgiparams{'LOCAL_ID'} ne ''))
1371 ) {
ed84e8b8
MT
1372 $errormessage = $Lang::tr{'invalid local-remote id'} . '<br />' .
1373 'DER_ASN1_DN: @c=FR/ou=Paris/ou=Home/cn=*<br />' .
5fd30232
MT
1374 'FQDN: @ipfire.org<br />' .
1375 'USER_FQDN: info@ipfire.org<br />' .
ed84e8b8 1376 'IPV4_ADDR: @123.123.123.123';
ac1cfefa
MT
1377 goto VPNCONF_ERROR;
1378 }
ed84e8b8
MT
1379 # If Auth is DN, verify existance of Remote ID.
1380 if ( $cgiparams{'REMOTE_ID'} eq '' && (
1381 $cgiparams{'AUTH'} eq 'auth-dn'|| # while creation
1382 $confighash{$cgiparams{'KEY'}}[2] eq '%auth-dn')){ # while editing
1383 $errormessage = $Lang::tr{'vpn missing remote id'};
1384 goto VPNCONF_ERROR;
1385 }
1386
1387 if ($cgiparams{'AUTH'} eq 'psk') {
ac1cfefa
MT
1388 if (! length($cgiparams{'PSK'}) ) {
1389 $errormessage = $Lang::tr{'pre-shared key is too short'};
1390 goto VPNCONF_ERROR;
1391 }
ed84e8b8
MT
1392 if ($cgiparams{'PSK'} =~ /'/) {
1393 $cgiparams{'PSK'} =~ tr/'/ /;
ac1cfefa
MT
1394 $errormessage = $Lang::tr{'invalid characters found in pre-shared key'};
1395 goto VPNCONF_ERROR;
1396 }
1397 } elsif ($cgiparams{'AUTH'} eq 'certreq') {
1398 if ($cgiparams{'KEY'}) {
1399 $errormessage = $Lang::tr{'cant change certificates'};
1400 goto VPNCONF_ERROR;
1401 }
1402 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1403 $errormessage = $Lang::tr{'there was no file upload'};
1404 goto VPNCONF_ERROR;
1405 }
1406
1407 # Move uploaded certificate request to a temporary file
1408 (my $fh, my $filename) = tempfile( );
1409 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1410 $errormessage = $!;
1411 goto VPNCONF_ERROR;
1412 }
1413
ed84e8b8
MT
1414 # Sign the certificate request
1415 &General::log("ipsec", "Signing your cert $cgiparams{'NAME'}...");
1416 my $opt = " ca -days 999999";
1417 $opt .= " -batch -notext";
1418 $opt .= " -in $filename";
1419 $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}cert.pem";
1420
1421 if ( $errormessage = &callssl ($opt) ) {
ac1cfefa
MT
1422 unlink ($filename);
1423 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1424 &cleanssldatabase();
1425 goto VPNCONF_ERROR;
1426 } else {
ed84e8b8
MT
1427 unlink ($filename);
1428 &cleanssldatabase();
ac1cfefa
MT
1429 }
1430
ed84e8b8 1431 $cgiparams{'CERT_NAME'} = getCNfromcert ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
ac1cfefa
MT
1432 if ($cgiparams{'CERT_NAME'} eq '') {
1433 $errormessage = $Lang::tr{'could not retrieve common name from certificate'};
1434 goto VPNCONF_ERROR;
1435 }
ed84e8b8
MT
1436 } elsif ($cgiparams{'AUTH'} eq 'pkcs12') {
1437 &General::log("ipsec", "Importing from p12...");
1438
1439 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1440 $errormessage = $Lang::tr{'there was no file upload'};
1441 goto ROOTCERT_ERROR;
1442 }
1443
1444 # Move uploaded certificate request to a temporary file
1445 (my $fh, my $filename) = tempfile( );
1446 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1447 $errormessage = $!;
1448 goto ROOTCERT_ERROR;
1449 }
1450
1451 # Extract the CA certificate from the file
1452 &General::log("ipsec", "Extracting caroot from p12...");
1453 if (open(STDIN, "-|")) {
1454 my $opt = " pkcs12 -cacerts -nokeys";
1455 $opt .= " -in $filename";
1456 $opt .= " -out /tmp/newcacert";
1457 $errormessage = &callssl ($opt);
1458 } else { #child
1459 print "$cgiparams{'P12_PASS'}\n";
1460 exit (0);
1461 }
1462
1463 # Extract the Host certificate from the file
1464 if (!$errormessage) {
1465 &General::log("ipsec", "Extracting host cert from p12...");
1466 if (open(STDIN, "-|")) {
1467 my $opt = " pkcs12 -clcerts -nokeys";
1468 $opt .= " -in $filename";
1469 $opt .= " -out /tmp/newhostcert";
1470 $errormessage = &callssl ($opt);
1471 } else { #child
1472 print "$cgiparams{'P12_PASS'}\n";
1473 exit (0);
1474 }
1475 }
1476
1477 if (!$errormessage) {
1478 &General::log("ipsec", "Moving cacert...");
1479 #If CA have new subject, add it to our list of CA
1480 my $casubject = &Header::cleanhtml(getsubjectfromcert ('/tmp/newcacert'));
1481 my @names;
1482 foreach my $x (keys %cahash) {
1483 $casubject='' if ($cahash{$x}[1] eq $casubject);
1484 unshift (@names,$cahash{$x}[0]);
1485 }
1486 if ($casubject) { # a new one!
1487 my $temp = `/usr/bin/openssl x509 -text -in /tmp/newcacert`;
1488 if ($temp !~ /CA:TRUE/i) {
1489 $errormessage = $Lang::tr{'not a valid ca certificate'};
1490 } else {
1491 #compute a name for it
1492 my $idx=0;
1493 while (grep(/Imported-$idx/, @names) ) {$idx++};
1494 $cgiparams{'CA_NAME'}="Imported-$idx";
1495 $cgiparams{'CERT_NAME'}=&Header::cleanhtml(getCNfromcert ('/tmp/newhostcert'));
1496 move("/tmp/newcacert", "${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem");
1497 $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
1498 if (!$errormessage) {
1499 my $key = &General::findhasharraykey (\%cahash);
1500 $cahash{$key}[0] = $cgiparams{'CA_NAME'};
1501 $cahash{$key}[1] = $casubject;
1502 &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
1503 system('/usr/local/bin/ipsecctrl', 'R');
1504 }
1505 }
1506 }
1507 }
1508 if (!$errormessage) {
1509 &General::log("ipsec", "Moving host cert...");
1510 move("/tmp/newhostcert", "${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1511 $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
1512 }
1513
1514 #cleanup temp files
1515 unlink ($filename);
1516 unlink ('/tmp/newcacert');
1517 unlink ('/tmp/newhostcert');
1518 if ($errormessage) {
1519 unlink ("${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem");
1520 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1521 goto VPNCONF_ERROR;
1522 }
1523 &General::log("ipsec", "p12 import completed!");
ac1cfefa
MT
1524 } elsif ($cgiparams{'AUTH'} eq 'certfile') {
1525 if ($cgiparams{'KEY'}) {
1526 $errormessage = $Lang::tr{'cant change certificates'};
1527 goto VPNCONF_ERROR;
1528 }
1529 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1530 $errormessage = $Lang::tr{'there was no file upload'};
1531 goto VPNCONF_ERROR;
1532 }
1533 # Move uploaded certificate to a temporary file
1534 (my $fh, my $filename) = tempfile( );
1535 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1536 $errormessage = $!;
1537 goto VPNCONF_ERROR;
1538 }
1539
1540 # Verify the certificate has a valid CA and move it
ed84e8b8
MT
1541 &General::log("ipsec", "Validating imported cert against our known CA...");
1542 my $validca = 1; #assume ok
ac1cfefa 1543 my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/cacert.pem $filename`;
ed84e8b8
MT
1544 if ($test !~ /: OK/) {
1545 my $validca = 0;
ac1cfefa
MT
1546 foreach my $key (keys %cahash) {
1547 $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/$cahash{$key}[0]cert.pem $filename`;
1548 if ($test =~ /: OK/) {
1549 $validca = 1;
ed84e8b8 1550 last;
ac1cfefa
MT
1551 }
1552 }
1553 }
1554 if (! $validca) {
1555 $errormessage = $Lang::tr{'certificate does not have a valid ca associated with it'};
1556 unlink ($filename);
1557 goto VPNCONF_ERROR;
1558 } else {
1559 move($filename, "${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1560 if ($? ne 0) {
1561 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1562 unlink ($filename);
1563 goto VPNCONF_ERROR;
1564 }
1565 }
1566
ed84e8b8 1567 $cgiparams{'CERT_NAME'} = getCNfromcert ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
ac1cfefa
MT
1568 if ($cgiparams{'CERT_NAME'} eq '') {
1569 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1570 $errormessage = $Lang::tr{'could not retrieve common name from certificate'};
1571 goto VPNCONF_ERROR;
1572 }
1573 } elsif ($cgiparams{'AUTH'} eq 'certgen') {
1574 if ($cgiparams{'KEY'}) {
1575 $errormessage = $Lang::tr{'cant change certificates'};
1576 goto VPNCONF_ERROR;
1577 }
1578 # Validate input since the form was submitted
1579 if (length($cgiparams{'CERT_NAME'}) >60) {
1580 $errormessage = $Lang::tr{'name too long'};
1581 goto VPNCONF_ERROR;
1582 }
1583 if ($cgiparams{'CERT_NAME'} !~ /^[a-zA-Z0-9 ,\.\-_]+$/) {
1584 $errormessage = $Lang::tr{'invalid input for name'};
1585 goto VPNCONF_ERROR;
1586 }
1587 if ($cgiparams{'CERT_EMAIL'} ne '' && (! &General::validemail($cgiparams{'CERT_EMAIL'}))) {
1588 $errormessage = $Lang::tr{'invalid input for e-mail address'};
1589 goto VPNCONF_ERROR;
1590 }
1591 if (length($cgiparams{'CERT_EMAIL'}) > 40) {
1592 $errormessage = $Lang::tr{'e-mail address too long'};
1593 goto VPNCONF_ERROR;
1594 }
1595 if ($cgiparams{'CERT_OU'} ne '' && $cgiparams{'CERT_OU'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1596 $errormessage = $Lang::tr{'invalid input for department'};
1597 goto VPNCONF_ERROR;
1598 }
1599 if (length($cgiparams{'CERT_ORGANIZATION'}) >60) {
1600 $errormessage = $Lang::tr{'organization too long'};
1601 goto VPNCONF_ERROR;
1602 }
1603 if ($cgiparams{'CERT_ORGANIZATION'} !~ /^[a-zA-Z0-9 ,\.\-_]+$/) {
1604 $errormessage = $Lang::tr{'invalid input for organization'};
1605 goto VPNCONF_ERROR;
1606 }
1607 if ($cgiparams{'CERT_CITY'} ne '' && $cgiparams{'CERT_CITY'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1608 $errormessage = $Lang::tr{'invalid input for city'};
1609 goto VPNCONF_ERROR;
1610 }
1611 if ($cgiparams{'CERT_STATE'} ne '' && $cgiparams{'CERT_STATE'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1612 $errormessage = $Lang::tr{'invalid input for state or province'};
1613 goto VPNCONF_ERROR;
1614 }
1615 if ($cgiparams{'CERT_COUNTRY'} !~ /^[A-Z]*$/) {
1616 $errormessage = $Lang::tr{'invalid input for country'};
1617 goto VPNCONF_ERROR;
1618 }
ed84e8b8
MT
1619 #the exact syntax is a list comma separated of
1620 # email:any-validemail
1621 # URI: a uniform resource indicator
1622 # DNS: a DNS domain name
1623 # RID: a registered OBJECT IDENTIFIER
1624 # IP: an IP address
1625 # example: email:franck@foo.com,IP:10.0.0.10,DNS:franck.foo.com
1626
1627 if ($cgiparams{'SUBJECTALTNAME'} ne '' && $cgiparams{'SUBJECTALTNAME'} !~ /^(email|URI|DNS|RID|IP):[a-zA-Z0-9 :\/,\.\-_@]*$/) {
1628 $errormessage = $Lang::tr{'vpn altname syntax'};
1629 goto VPNCONF_ERROR;
1630 }
1631
ac1cfefa
MT
1632 if (length($cgiparams{'CERT_PASS1'}) < 5) {
1633 $errormessage = $Lang::tr{'password too short'};
1634 goto VPNCONF_ERROR;
1635 }
1636 if ($cgiparams{'CERT_PASS1'} ne $cgiparams{'CERT_PASS2'}) {
1637 $errormessage = $Lang::tr{'passwords do not match'};
1638 goto VPNCONF_ERROR;
1639 }
1640
1641 # Replace empty strings with a .
1642 (my $ou = $cgiparams{'CERT_OU'}) =~ s/^\s*$/\./;
1643 (my $city = $cgiparams{'CERT_CITY'}) =~ s/^\s*$/\./;
1644 (my $state = $cgiparams{'CERT_STATE'}) =~ s/^\s*$/\./;
1645
1646 # Create the Host certificate request
ed84e8b8
MT
1647 &General::log("ipsec", "Creating a cert...");
1648
1649 if (open(STDIN, "-|")) {
1650 my $opt = " req -nodes -rand /proc/interrupts:/proc/net/rt_cache";
1651 $opt .= " -newkey rsa:1024";
1652 $opt .= " -keyout ${General::swroot}/certs/$cgiparams{'NAME'}key.pem";
1653 $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}req.pem";
1654
1655 if ( $errormessage = &callssl ($opt) ) {
ac1cfefa
MT
1656 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}key.pem");
1657 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}req.pem");
1658 goto VPNCONF_ERROR;
ed84e8b8
MT
1659 }
1660 } else { #child
1661 print "$cgiparams{'CERT_COUNTRY'}\n";
1662 print "$state\n";
1663 print "$city\n";
1664 print "$cgiparams{'CERT_ORGANIZATION'}\n";
1665 print "$ou\n";
1666 print "$cgiparams{'CERT_NAME'}\n";
1667 print "$cgiparams{'CERT_EMAIL'}\n";
1668 print ".\n";
1669 print ".\n";
1670 exit (0);
ac1cfefa 1671 }
ed84e8b8 1672
ac1cfefa 1673 # Sign the host certificate request
ed84e8b8
MT
1674 &General::log("ipsec", "Signing the cert $cgiparams{'NAME'}...");
1675
1676 #No easy way for specifying the contain of subjectAltName without writing a config file...
1677 my ($fh, $v3extname) = tempfile ('/tmp/XXXXXXXX');
1678 print $fh <<END
1679 basicConstraints=CA:FALSE
1680 nsComment="OpenSSL Generated Certificate"
1681 subjectKeyIdentifier=hash
1682 authorityKeyIdentifier=keyid,issuer:always
1683END
1684;
1685 print $fh "subjectAltName=$cgiparams{'SUBJECTALTNAME'}" if ($cgiparams{'SUBJECTALTNAME'});
1686 close ($fh);
1687
1688 my $opt = " ca -days 999999 -batch -notext";
1689 $opt .= " -in ${General::swroot}/certs/$cgiparams{'NAME'}req.pem";
1690 $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}cert.pem";
1691 $opt .= " -extfile $v3extname";
1692
1693 if ( $errormessage = &callssl ($opt) ) {
1694 unlink ($v3extname);
ac1cfefa
MT
1695 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}key.pem");
1696 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}req.pem");
1697 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1698 &cleanssldatabase();
1699 goto VPNCONF_ERROR;
1700 } else {
ed84e8b8 1701 unlink ($v3extname);
ac1cfefa
MT
1702 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}req.pem");
1703 &cleanssldatabase();
1704 }
1705
1706 # Create the pkcs12 file
ed84e8b8
MT
1707 &General::log("ipsec", "Packing a pkcs12 file...");
1708 $opt = " pkcs12 -export";
1709 $opt .= " -inkey ${General::swroot}/certs/$cgiparams{'NAME'}key.pem";
1710 $opt .= " -in ${General::swroot}/certs/$cgiparams{'NAME'}cert.pem";
1711 $opt .= " -name \"$cgiparams{'NAME'}\"";
1712 $opt .= " -passout pass:$cgiparams{'CERT_PASS1'}";
1713 $opt .= " -certfile ${General::swroot}/ca/cacert.pem";
1714 $opt .= " -caname \"$vpnsettings{'ROOTCERT_ORGANIZATION'} CA\"";
1715 $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}.p12";
1716
1717 if ( $errormessage = &callssl ($opt) ) {
ac1cfefa
MT
1718 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}key.pem");
1719 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
1720 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}.p12");
1721 goto VPNCONF_ERROR;
1722 } else {
1723 unlink ("${General::swroot}/certs/$cgiparams{'NAME'}key.pem");
1724 }
1725 } elsif ($cgiparams{'AUTH'} eq 'cert') {
1726 ;# Nothing, just editing
ed84e8b8
MT
1727 } elsif ($cgiparams{'AUTH'} eq 'auth-dn') {
1728 $cgiparams{'CERT_NAME'} = '%auth-dn'; # a special value saying 'no cert file'
ac1cfefa
MT
1729 } else {
1730 $errormessage = $Lang::tr{'invalid input for authentication method'};
1731 goto VPNCONF_ERROR;
1732 }
1733
ed84e8b8
MT
1734 # 1)Error message here is not accurate.
1735 # 2)Test is superfluous, openswan can reference same cert multiple times
1736 # 3)Present since initial version (1.3.2.11), it isn't a bug correction
1737 # Check if there is no other entry with this certificate name
1738 #if ((! $cgiparams{'KEY'}) && ($cgiparams{'AUTH'} ne 'psk') && ($cgiparams{'AUTH'} ne 'auth-dn')) {
1739 # foreach my $key (keys %confighash) {
1740 # if ($confighash{$key}[2] eq $cgiparams{'CERT_NAME'}) {
1741 # $errormessage = $Lang::tr{'a connection with this common name already exists'};
1742 # goto VPNCONF_ERROR;
1743 # }
1744 # }
1745 #}
ac1cfefa 1746 # Save the config
ed84e8b8 1747
ac1cfefa
MT
1748 my $key = $cgiparams{'KEY'};
1749 if (! $key) {
1750 $key = &General::findhasharraykey (\%confighash);
1751 foreach my $i (0 .. 28) { $confighash{$key}[$i] = "";}
1752 }
1753 $confighash{$key}[0] = $cgiparams{'ENABLED'};
1754 $confighash{$key}[1] = $cgiparams{'NAME'};
1755 if ((! $cgiparams{'KEY'}) && $cgiparams{'AUTH'} ne 'psk') {
1756 $confighash{$key}[2] = $cgiparams{'CERT_NAME'};
1757 }
1758 $confighash{$key}[3] = $cgiparams{'TYPE'};
1759 if ($cgiparams{'AUTH'} eq 'psk') {
1760 $confighash{$key}[4] = 'psk';
1761 $confighash{$key}[5] = $cgiparams{'PSK'};
1762 } else {
1763 $confighash{$key}[4] = 'cert';
1764 }
1765 if ($cgiparams{'TYPE'} eq 'net') {
ac1cfefa
MT
1766 $confighash{$key}[11] = $cgiparams{'REMOTE_SUBNET'};
1767 }
1768 $confighash{$key}[7] = $cgiparams{'LOCAL_ID'};
1769 $confighash{$key}[8] = $cgiparams{'LOCAL_SUBNET'};
1770 $confighash{$key}[9] = $cgiparams{'REMOTE_ID'};
1771 $confighash{$key}[10] = $cgiparams{'REMOTE'};
1772 $confighash{$key}[25] = $cgiparams{'REMARK'};
1773 $confighash{$key}[26] = $cgiparams{'INTERFACE'};
1774 $confighash{$key}[27] = $cgiparams{'DPD_ACTION'};
ac1cfefa 1775
ed84e8b8
MT
1776 #dont forget advanced value
1777 $confighash{$key}[18] = $cgiparams{'IKE_ENCRYPTION'};
1778 $confighash{$key}[19] = $cgiparams{'IKE_INTEGRITY'};
1779 $confighash{$key}[20] = $cgiparams{'IKE_GROUPTYPE'};
1780 $confighash{$key}[16] = $cgiparams{'IKE_LIFETIME'};
1781 $confighash{$key}[21] = $cgiparams{'ESP_ENCRYPTION'};
1782 $confighash{$key}[22] = $cgiparams{'ESP_INTEGRITY'};
1783 $confighash{$key}[23] = $cgiparams{'ESP_GROUPTYPE'};
1784 $confighash{$key}[17] = $cgiparams{'ESP_KEYLIFE'};
1785 $confighash{$key}[12] = $cgiparams{'AGGRMODE'};
1786 $confighash{$key}[13] = $cgiparams{'COMPRESSION'};
1787 $confighash{$key}[24] = $cgiparams{'ONLY_PROPOSED'};
1788 $confighash{$key}[28] = $cgiparams{'PFS'};
1789 $confighash{$key}[14] = $cgiparams{'VHOST'};
ac1cfefa
MT
1790
1791 #free unused fields!
5fd30232 1792 $confighash{$key}[6] = 'off';
ed84e8b8 1793 $confighash{$key}[15] = 'off';
ac1cfefa
MT
1794
1795 &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
1796 &writeipsecfiles();
ed84e8b8 1797 if (&vpnenabled) {
ac1cfefa
MT
1798 system('/usr/local/bin/ipsecctrl', 'S', $key);
1799 sleep $sleepDelay;
1800 }
1801 if ($cgiparams{'EDIT_ADVANCED'} eq 'on') {
1802 $cgiparams{'KEY'} = $key;
1803 $cgiparams{'ACTION'} = $Lang::tr{'advanced'};
1804 }
1805 goto VPNCONF_END;
1806 } else { # add new connection
1807 $cgiparams{'ENABLED'} = 'on';
ac1cfefa
MT
1808 if ( ! -f "${General::swroot}/private/cakey.pem" ) {
1809 $cgiparams{'AUTH'} = 'psk';
1810 } elsif ( ! -f "${General::swroot}/ca/cacert.pem") {
1811 $cgiparams{'AUTH'} = 'certfile';
1812 } else {
1813 $cgiparams{'AUTH'} = 'certgen';
1814 }
1815 $cgiparams{'LOCAL_SUBNET'} ="$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}";
ed84e8b8
MT
1816 $cgiparams{'CERT_EMAIL'} = $vpnsettings{'ROOTCERT_EMAIL'};
1817 $cgiparams{'CERT_OU'} = $vpnsettings{'ROOTCERT_OU'};
ac1cfefa
MT
1818 $cgiparams{'CERT_ORGANIZATION'} = $vpnsettings{'ROOTCERT_ORGANIZATION'};
1819 $cgiparams{'CERT_CITY'} = $vpnsettings{'ROOTCERT_CITY'};
1820 $cgiparams{'CERT_STATE'} = $vpnsettings{'ROOTCERT_STATE'};
1821 $cgiparams{'CERT_COUNTRY'} = $vpnsettings{'ROOTCERT_COUNTRY'};
1822
1823 # choose appropriate dpd action
1824 if ($cgiparams{'TYPE'} eq 'host') {
1825 $cgiparams{'DPD_ACTION'} = 'clear';
1826 } else {
ed84e8b8 1827 $cgiparams{'DPD_ACTION'} = 'restart';
ac1cfefa
MT
1828 }
1829
1830 # Default is yes for 'pfs'
ed84e8b8 1831 $cgiparams{'PFS'} = 'on';
ac1cfefa
MT
1832
1833 # ID are empty
1834 $cgiparams{'LOCAL_ID'} = '';
1835 $cgiparams{'REMOTE_ID'} = '';
ed84e8b8
MT
1836
1837 #use default advanced value
1838 $cgiparams{'IKE_ENCRYPTION'} = 'aes128|3des'; #[18];
1839 $cgiparams{'IKE_INTEGRITY'} = 'sha|md5'; #[19];
1840 $cgiparams{'IKE_GROUPTYPE'} = '1536|1024'; #[20];
1841 $cgiparams{'IKE_LIFETIME'} = '1'; #[16];
1842 $cgiparams{'ESP_ENCRYPTION'} = 'aes128|3des'; #[21];
1843 $cgiparams{'ESP_INTEGRITY'} = 'sha1|md5'; #[22];
1844 $cgiparams{'ESP_GROUPTYPE'} = ''; #[23];
1845 $cgiparams{'ESP_KEYLIFE'} = '8'; #[17];
1846 $cgiparams{'AGGRMODE'} = 'off'; #[12];
1847 $cgiparams{'COMPRESSION'} = 'off'; #[13];
1848 $cgiparams{'ONLY_PROPOSED'} = 'off'; #[24];
1849 $cgiparams{'PFS'} = 'on'; #[28];
1850 $cgiparams{'VHOST'} = 'on'; #[14];
ac1cfefa
MT
1851 }
1852
1853 VPNCONF_ERROR:
1854 $checked{'ENABLED'}{'off'} = '';
1855 $checked{'ENABLED'}{'on'} = '';
1856 $checked{'ENABLED'}{$cgiparams{'ENABLED'}} = "checked='checked'";
ac1cfefa
MT
1857
1858 $checked{'EDIT_ADVANCED'}{'off'} = '';
1859 $checked{'EDIT_ADVANCED'}{'on'} = '';
1860 $checked{'EDIT_ADVANCED'}{$cgiparams{'EDIT_ADVANCED'}} = "checked='checked'";
1861
ac1cfefa
MT
1862 $checked{'AUTH'}{'psk'} = '';
1863 $checked{'AUTH'}{'certreq'} = '';
1864 $checked{'AUTH'}{'certgen'} = '';
1865 $checked{'AUTH'}{'certfile'} = '';
ed84e8b8
MT
1866 $checked{'AUTH'}{'pkcs12'} = '';
1867 $checked{'AUTH'}{'auth-dn'} = '';
ac1cfefa
MT
1868 $checked{'AUTH'}{$cgiparams{'AUTH'}} = "checked='checked'";
1869
ed84e8b8
MT
1870 $selected{'INTERFACE'}{'RED'} = '';
1871 $selected{'INTERFACE'}{'ORANGE'} = '';
1872 $selected{'INTERFACE'}{'GREEN'} = '';
1873 $selected{'INTERFACE'}{'BLUE'} = '';
ac1cfefa 1874 $selected{'INTERFACE'}{$cgiparams{'INTERFACE'}} = "selected='selected'";
ed84e8b8
MT
1875
1876 $selected{'DPD_ACTION'}{'clear'} = '';
1877 $selected{'DPD_ACTION'}{'hold'} = '';
1878 $selected{'DPD_ACTION'}{'restart'} = '';
ac1cfefa 1879 $selected{'DPD_ACTION'}{$cgiparams{'DPD_ACTION'}} = "selected='selected'";
ac1cfefa 1880
ed84e8b8
MT
1881 &Header::showhttpheaders();
1882 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1883 &Header::openbigbox('100%', 'left', '', $errormessage);
1884 if ($errormessage) {
1885 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
1886 print "<class name='base'>$errormessage";
1887 print "&nbsp;</class>";
1888 &Header::closebox();
1889 }
ac1cfefa 1890
ed84e8b8
MT
1891 if ($warnmessage) {
1892 &Header::openbox('100%', 'left', "$Lang::tr{'warning messages'}:");
1893 print "<class name='base'>$warnmessage";
1894 print "&nbsp;</class>";
1895 &Header::closebox();
1896 }
ac1cfefa 1897
ed84e8b8
MT
1898 print "<form method='post' enctype='multipart/form-data' action='$ENV{'SCRIPT_NAME'}'>";
1899 print<<END
1900 <input type='hidden' name='TYPE' value='$cgiparams{'TYPE'}' />
1901 <input type='hidden' name='IKE_ENCRYPTION' value='$cgiparams{'IKE_ENCRYPTION'}' />
1902 <input type='hidden' name='IKE_INTEGRITY' value='$cgiparams{'IKE_INTEGRITY'}' />
1903 <input type='hidden' name='IKE_GROUPTYPE' value='$cgiparams{'IKE_GROUPTYPE'}' />
1904 <input type='hidden' name='IKE_LIFETIME' value='$cgiparams{'IKE_LIFETIME'}' />
1905 <input type='hidden' name='ESP_ENCRYPTION' value='$cgiparams{'ESP_ENCRYPTION'}' />
1906 <input type='hidden' name='ESP_INTEGRITY' value='$cgiparams{'ESP_INTEGRITY'}' />
1907 <input type='hidden' name='ESP_GROUPTYPE' value='$cgiparams{'ESP_GROUPTYPE'}' />
1908 <input type='hidden' name='ESP_KEYLIFE' value='$cgiparams{'ESP_KEYLIFE'}' />
1909 <input type='hidden' name='AGGRMODE' value='$cgiparams{'AGGRMODE'}' />
1910 <input type='hidden' name='COMPRESSION' value='$cgiparams{'COMPRESSION'}' />
1911 <input type='hidden' name='ONLY_PROPOSED' value='$cgiparams{'ONLY_PROPOSED'}' />
1912 <input type='hidden' name='PFS' value='$cgiparams{'PFS'}' />
1913 <input type='hidden' name='VHOST' value='$cgiparams{'VHOST'}' />
1914END
1915 ;
1916 if ($cgiparams{'KEY'}) {
1917 print "<input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />";
1918 print "<input type='hidden' name='AUTH' value='$cgiparams{'AUTH'}' />";
1919 }
ac1cfefa 1920
ed84e8b8
MT
1921 &Header::openbox('100%', 'left', "$Lang::tr{'connection'}:");
1922 print "<table width='100%'>";
1923 print "<tr><td width='25%' class='boldbase'>$Lang::tr{'name'}:</td>";
1924 if ($cgiparams{'KEY'}) {
1925 print "<td width='25%' class='base'><input type='hidden' name='NAME' value='$cgiparams{'NAME'}' /><b>$cgiparams{'NAME'}</b></td>";
1926 } else {
1927 print "<td width='25%'><input type='text' name='NAME' value='$cgiparams{'NAME'}' size='30' /></td>";
1928 }
1929 print "<td>$Lang::tr{'enabled'}</td><td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td></tr>";
5fd30232 1930 print '</tr><td><br /></td><tr>';
ac1cfefa 1931
5fd30232
MT
1932 my $disabled;
1933 my $blob;
ed84e8b8 1934 if ($cgiparams{'TYPE'} eq 'host') {
5fd30232
MT
1935 $disabled = "disabled='disabled'";
1936 $blob = "<img src='/blob.gif' alt='*' />";
1937 };
1938
1939 print "<tr><td>$Lang::tr{'host ip'}:</td>";
1940 print "<td><select name='INTERFACE'>";
1941 print "<option value='RED' $selected{'INTERFACE'}{'RED'}>RED ($vpnsettings{'VPN_IP'})</option>";
1942 print "<option value='GREEN' $selected{'INTERFACE'}{'GREEN'}>GREEN ($netsettings{'GREEN_ADDRESS'})</option>";
1943 print "<option value='BLUE' $selected{'INTERFACE'}{'BLUE'}>BLUE ($netsettings{'BLUE_ADDRESS'})</option>" if ($netsettings{'BLUE_DEV'} ne '');
1944 print "<option value='ORANGE' $selected{'INTERFACE'}{'ORANGE'}>ORANGE ($netsettings{'ORANGE_ADDRESS'})</option>" if ($netsettings{'ORANGE_DEV'} ne '');
1945 print "</select></td>";
1946 print <<END
1947 <td class='boldbase'>$Lang::tr{'remote host/ip'}:&nbsp;$blob</td>
ed84e8b8 1948 <td><input type='text' name='REMOTE' value='$cgiparams{'REMOTE'}' size='30' /></td>
ed84e8b8
MT
1949 </tr><tr>
1950 <td class='boldbase' nowrap='nowrap'>$Lang::tr{'local subnet'}</td>
1951 <td><input type='text' name='LOCAL_SUBNET' value='$cgiparams{'LOCAL_SUBNET'}' size='30' /></td>
1952 <td class='boldbase' nowrap='nowrap'>$Lang::tr{'remote subnet'}</td>
5fd30232
MT
1953 <td><input $disabled type='text' name='REMOTE_SUBNET' value='$cgiparams{'REMOTE_SUBNET'}' size='30' /></td>
1954 </tr><tr>
1955 <td class='boldbase'>$Lang::tr{'vpn local id'}:&nbsp;<img src='/blob.gif' alt='*' />
1956 <br />($Lang::tr{'eg'} <tt>&#64;xy.example.com</tt>)</td>
1957 <td><input type='text' name='LOCAL_ID' value='$cgiparams{'LOCAL_ID'}' /></td>
1958 <td class='boldbase'>$Lang::tr{'vpn remote id'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
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();