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