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