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