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