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