OpenVPN: Add CCD configuration GUI.
[people/teissler/ipfire-2.x.git] / html / cgi-bin / ovpnmain.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2011 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21 ###
22 # Based on IPFireCore 55
23 ###
24 use CGI;
25 use CGI qw/:standard/;
26 use Net::DNS;
27 use Net::Ping;
28 use Net::Telnet;
29 use File::Copy;
30 use File::Temp qw/ tempfile tempdir /;
31 use strict;
32 use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
33 require '/var/ipfire/general-functions.pl';
34 require "${General::swroot}/lang.pl";
35 require "${General::swroot}/header.pl";
36 require "${General::swroot}/countries.pl";
37
38 # enable only the following on debugging purpose
39 #use warnings;
40 #use CGI::Carp 'fatalsToBrowser';
41 #workaround to suppress a warning when a variable is used only once
42 my @dummy = ( ${Header::colourgreen}, ${Header::colourblue} );
43 undef (@dummy);
44
45 my %color = ();
46 my %mainsettings = ();
47 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
48 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
49
50 ###
51 ### Initialize variables
52 ###
53 my %netsettings=();
54 my %cgiparams=();
55 my %vpnsettings=();
56 my %checked=();
57 my %confighash=();
58 my %cahash=();
59 my %selected=();
60 my $warnmessage = '';
61 my $errormessage = '';
62 my %settings=();
63 my $routes_push_file = '';
64 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
65 $cgiparams{'ENABLED'} = 'off';
66 $cgiparams{'ENABLED_BLUE'} = 'off';
67 $cgiparams{'ENABLED_ORANGE'} = 'off';
68 $cgiparams{'EDIT_ADVANCED'} = 'off';
69 $cgiparams{'NAT'} = 'off';
70 $cgiparams{'COMPRESSION'} = 'off';
71 $cgiparams{'ONLY_PROPOSED'} = 'off';
72 $cgiparams{'ACTION'} = '';
73 $cgiparams{'CA_NAME'} = '';
74 $cgiparams{'DHCP_DOMAIN'} = '';
75 $cgiparams{'DHCP_DNS'} = '';
76 $cgiparams{'DHCP_WINS'} = '';
77 $cgiparams{'ROUTES_PUSH'} = '';
78 $cgiparams{'DCOMPLZO'} = 'off';
79 $cgiparams{'MSSFIX'} = '';
80 $cgiparams{'number'} = '';
81 $routes_push_file = "${General::swroot}/ovpn/routes_push";
82 unless (-e $routes_push_file) { system("touch $routes_push_file"); }
83 unless (-e "${General::swroot}/ovpn/ccd.conf") { system("touch ${General::swroot}/ovpn/ccd.conf"); }
84 unless (-e "${General::swroot}/ovpn/ccdroute") { system("touch ${General::swroot}/ovpn/ccdroute"); }
85 unless (-e "${General::swroot}/ovpn/ccdroute2") { system("touch ${General::swroot}/ovpn/ccdroute2"); }
86
87 &Header::getcgihash(\%cgiparams, {'wantfile' => 1, 'filevar' => 'FH'});
88
89 # prepare openvpn config file
90 ###
91 ### Useful functions
92 ###
93 sub haveOrangeNet
94 {
95 if ($netsettings{'CONFIG_TYPE'} == 2) {return 1;}
96 if ($netsettings{'CONFIG_TYPE'} == 4) {return 1;}
97 return 0;
98 }
99
100 sub haveBlueNet
101 {
102 if ($netsettings{'CONFIG_TYPE'} == 3) {return 1;}
103 if ($netsettings{'CONFIG_TYPE'} == 4) {return 1;}
104 return 0;
105 }
106
107 sub sizeformat{
108 my $bytesize = shift;
109 my $i = 0;
110
111 while(abs($bytesize) >= 1024){
112 $bytesize=$bytesize/1024;
113 $i++;
114 last if($i==6);
115 }
116
117 my @units = ("Bytes","KB","MB","GB","TB","PB","EB");
118 my $newsize=(int($bytesize*100 +0.5))/100;
119 return("$newsize $units[$i]");
120 }
121
122 sub valid_dns_host {
123 my $hostname = $_[0];
124 unless ($hostname) { return "No hostname"};
125 my $res = new Net::DNS::Resolver;
126 my $query = $res->search("$hostname");
127 if ($query) {
128 foreach my $rr ($query->answer) {
129 ## Potential bug - we are only looking at A records:
130 return 0 if $rr->type eq "A";
131 }
132 } else {
133 return $res->errorstring;
134 }
135 }
136
137 sub cleanssldatabase
138 {
139 if (open(FILE, ">${General::swroot}/ovpn/certs/serial")) {
140 print FILE "01";
141 close FILE;
142 }
143 if (open(FILE, ">${General::swroot}/ovpn/certs/index.txt")) {
144 print FILE "";
145 close FILE;
146 }
147 unlink ("${General::swroot}/ovpn/certs/index.txt.old");
148 unlink ("${General::swroot}/ovpn/certs/serial.old");
149 unlink ("${General::swroot}/ovpn/certs/01.pem");
150 }
151
152 sub newcleanssldatabase
153 {
154 if (! -s "${General::swroot}/ovpn/certs/serial" ) {
155 open(FILE, ">${General::swroot}(ovpn/certs/serial");
156 print FILE "01";
157 close FILE;
158 }
159 if (! -s ">${General::swroot}/ovpn/certs/index.txt") {
160 system ("touch ${General::swroot}/ovpn/certs/index.txt");
161 }
162 unlink ("${General::swroot}/ovpn/certs/index.txt.old");
163 unlink ("${General::swroot}/ovpn/certs/serial.old");
164 }
165
166 sub deletebackupcert
167 {
168 if (open(FILE, "${General::swroot}/ovpn/certs/serial.old")) {
169 my $hexvalue = <FILE>;
170 chomp $hexvalue;
171 close FILE;
172 unlink ("${General::swroot}/ovpn/certs/$hexvalue.pem");
173 }
174 }
175
176 sub checkportfw {
177 my $KEY2 = $_[0]; # key2
178 my $SRC_PORT = $_[1]; # src_port
179 my $PROTOCOL = $_[2]; # protocol
180 my $SRC_IP = $_[3]; # sourceip
181
182 my $pfwfilename = "${General::swroot}/portfw/config";
183 open(FILE, $pfwfilename) or die 'Unable to open config file.';
184 my @pfwcurrent = <FILE>;
185 close(FILE);
186 my $pfwkey1 = 0; # used for finding last sequence number used
187 foreach my $pfwline (@pfwcurrent)
188 {
189 my @pfwtemp = split(/\,/,$pfwline);
190
191 chomp ($pfwtemp[8]);
192 if ($KEY2 eq "0"){ # if key2 is 0 then it is a portfw addition
193 if ( $SRC_PORT eq $pfwtemp[3] &&
194 $PROTOCOL eq $pfwtemp[2] &&
195 $SRC_IP eq $pfwtemp[7])
196 {
197 $errormessage = "$Lang::tr{'source port in use'} $SRC_PORT";
198 }
199 # Check if key2 = 0, if it is then it is a port forward entry and we want the sequence number
200 if ( $pfwtemp[1] eq "0") {
201 $pfwkey1=$pfwtemp[0];
202 }
203 # Darren Critchley - Duplicate or overlapping Port range check
204 if ($pfwtemp[1] eq "0" &&
205 $PROTOCOL eq $pfwtemp[2] &&
206 $SRC_IP eq $pfwtemp[7] &&
207 $errormessage eq '')
208 {
209 &portchecks($SRC_PORT, $pfwtemp[5]);
210 # &portchecks($pfwtemp[3], $pfwtemp[5]);
211 # &portchecks($pfwtemp[3], $SRC_IP);
212 }
213 }
214 }
215 # $errormessage="$KEY2 $SRC_PORT $PROTOCOL $SRC_IP";
216
217 return;
218 }
219
220 sub checkportoverlap
221 {
222 my $portrange1 = $_[0]; # New port range
223 my $portrange2 = $_[1]; # existing port range
224 my @tempr1 = split(/\:/,$portrange1);
225 my @tempr2 = split(/\:/,$portrange2);
226
227 unless (&checkportinc($tempr1[0], $portrange2)){ return 0;}
228 unless (&checkportinc($tempr1[1], $portrange2)){ return 0;}
229
230 unless (&checkportinc($tempr2[0], $portrange1)){ return 0;}
231 unless (&checkportinc($tempr2[1], $portrange1)){ return 0;}
232
233 return 1; # Everything checks out!
234 }
235
236 # Darren Critchley - we want to make sure that a port entry is not within an already existing range
237 sub checkportinc
238 {
239 my $port1 = $_[0]; # Port
240 my $portrange2 = $_[1]; # Port range
241 my @tempr1 = split(/\:/,$portrange2);
242
243 if ($port1 < $tempr1[0] || $port1 > $tempr1[1]) {
244 return 1;
245 } else {
246 return 0;
247 }
248 }
249 # Darren Critchley - Duplicate or overlapping Port range check
250 sub portchecks
251 {
252 my $p1 = $_[0]; # New port range
253 my $p2 = $_[1]; # existing port range
254 # $_ = $_[0];
255 our ($prtrange1, $prtrange2);
256 $prtrange1 = 0;
257 # if (m/:/ && $prtrange1 == 1) { # comparing two port ranges
258 # unless (&checkportoverlap($p1,$p2)) {
259 # $errormessage = "$Lang::tr{'source port overlaps'} $p1";
260 # }
261 # }
262 if (m/:/ && $prtrange1 == 0 && $errormessage eq '') { # compare one port to a range
263 unless (&checkportinc($p2,$p1)) {
264 $errormessage = "$Lang::tr{'srcprt within existing'} $p1";
265 }
266 }
267 $prtrange1 = 1;
268 if (! m/:/ && $prtrange1 == 1 && $errormessage eq '') { # compare one port to a range
269 unless (&checkportinc($p1,$p2)) {
270 $errormessage = "$Lang::tr{'srcprt range overlaps'} $p2";
271 }
272 }
273 return;
274 }
275
276 # Darren Critchley - certain ports are reserved for IPFire
277 # TCP 67,68,81,222,445
278 # UDP 67,68
279 # Params passed in -> port, rangeyn, protocol
280 sub disallowreserved
281 {
282 # port 67 and 68 same for tcp and udp, don't bother putting in an array
283 my $msg = "";
284 my @tcp_reserved = (81,222,445);
285 my $prt = $_[0]; # the port or range
286 my $ryn = $_[1]; # tells us whether or not it is a port range
287 my $prot = $_[2]; # protocol
288 my $srcdst = $_[3]; # source or destination
289 if ($ryn) { # disect port range
290 if ($srcdst eq "src") {
291 $msg = "$Lang::tr{'rsvd src port overlap'}";
292 } else {
293 $msg = "$Lang::tr{'rsvd dst port overlap'}";
294 }
295 my @tmprng = split(/\:/,$prt);
296 unless (67 < $tmprng[0] || 67 > $tmprng[1]) { $errormessage="$msg 67"; return; }
297 unless (68 < $tmprng[0] || 68 > $tmprng[1]) { $errormessage="$msg 68"; return; }
298 if ($prot eq "tcp") {
299 foreach my $prange (@tcp_reserved) {
300 unless ($prange < $tmprng[0] || $prange > $tmprng[1]) { $errormessage="$msg $prange"; return; }
301 }
302 }
303 } else {
304 if ($srcdst eq "src") {
305 $msg = "$Lang::tr{'reserved src port'}";
306 } else {
307 $msg = "$Lang::tr{'reserved dst port'}";
308 }
309 if ($prt == 67) { $errormessage="$msg 67"; return; }
310 if ($prt == 68) { $errormessage="$msg 68"; return; }
311 if ($prot eq "tcp") {
312 foreach my $prange (@tcp_reserved) {
313 if ($prange == $prt) { $errormessage="$msg $prange"; return; }
314 }
315 }
316 }
317 return;
318 }
319
320
321 sub writeserverconf {
322 my %sovpnsettings = ();
323 my @temp = ();
324 &General::readhash("${General::swroot}/ovpn/settings", \%sovpnsettings);
325 &read_routepushfile;
326
327 open(CONF, ">${General::swroot}/ovpn/server.conf") or die "Unable to open ${General::swroot}/ovpn/server.conf: $!";
328 flock CONF, 2;
329 print CONF "#OpenVPN Server conf\n";
330 print CONF "\n";
331 print CONF "daemon openvpnserver\n";
332 print CONF "writepid /var/run/openvpn.pid\n";
333 print CONF "#DAN prepare OpenVPN for listening on blue and orange\n";
334 print CONF ";local $sovpnsettings{'VPN_IP'}\n";
335 print CONF "dev $sovpnsettings{'DDEVICE'}\n";
336 print CONF "$sovpnsettings{'DDEVICE'}-mtu $sovpnsettings{'DMTU'}\n";
337 print CONF "proto $sovpnsettings{'DPROTOCOL'}\n";
338 print CONF "port $sovpnsettings{'DDEST_PORT'}\n";
339 print CONF "script-security 3 system\n";
340 print CONF "ifconfig-pool-persist /var/ipfire/ovpn/ovpn-leases.db 3600\n";
341 print CONF "client-config-dir /var/ipfire/ovpn/ccd\n";
342 print CONF "tls-server\n";
343 print CONF "ca /var/ipfire/ovpn/ca/cacert.pem\n";
344 print CONF "cert /var/ipfire/ovpn/certs/servercert.pem\n";
345 print CONF "key /var/ipfire/ovpn/certs/serverkey.pem\n";
346 print CONF "dh /var/ipfire/ovpn/ca/dh1024.pem\n";
347 my @tempovpnsubnet = split("\/",$sovpnsettings{'DOVPN_SUBNET'});
348 print CONF "server $tempovpnsubnet[0] $tempovpnsubnet[1]\n";
349 #print CONF "push \"route $netsettings{'GREEN_NETADDRESS'} $netsettings{'GREEN_NETMASK'}\"\n";
350
351 if ($vpnsettings{'ROUTES_PUSH'} ne '') {
352 @temp = split(/\n/,$vpnsettings{'ROUTES_PUSH'});
353 foreach (@temp)
354 {
355 @tempovpnsubnet = split("\/",&General::ipcidr2msk($_));
356 print CONF "push \"route " . $tempovpnsubnet[0]. " " . $tempovpnsubnet[1] . "\"\n";
357 }
358 }
359 # a.marx ccd
360 my %ccdconfhash=();
361 &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
362 foreach my $key (keys %ccdconfhash) {
363 my $a=$ccdconfhash{$key}[1];
364 my ($b,$c) = split (/\//, $a);
365 print CONF "route $b ".&General::cidrtosub($c)."\n";
366 }
367 my %ccdroutehash=();
368 &General::readhasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
369 foreach my $key (keys %ccdroutehash) {
370 foreach my $i ( 1 .. $#{$ccdroutehash{$key}}){
371 my ($a,$b)=split (/\//,$ccdroutehash{$key}[$i]);
372 print CONF "route $a $b\n";
373 }
374 }
375 # ccd end
376
377 if ($sovpnsettings{CLIENT2CLIENT} eq 'on') {
378 print CONF "client-to-client\n";
379 }
380 if ($sovpnsettings{MSSFIX} eq 'on') {
381 print CONF "mssfix\n";
382 }
383 if ($sovpnsettings{FRAGMENT} ne '' && $sovpnsettings{'DPROTOCOL'} ne 'tcp') {
384 print CONF "fragment $sovpnsettings{'FRAGMENT'}\n";
385 }
386 if ($sovpnsettings{KEEPALIVE_1} > 0 && $sovpnsettings{KEEPALIVE_2} > 0) {
387 print CONF "keepalive $sovpnsettings{'KEEPALIVE_1'} $sovpnsettings{'KEEPALIVE_2'}\n";
388 }
389 print CONF "status-version 1\n";
390 print CONF "status /var/log/ovpnserver.log 30\n";
391 print CONF "cipher $sovpnsettings{DCIPHER}\n";
392 if ($sovpnsettings{DCOMPLZO} eq 'on') {
393 print CONF "comp-lzo\n";
394 }
395 if ($sovpnsettings{REDIRECT_GW_DEF1} eq 'on') {
396 print CONF "push \"redirect-gateway def1\"\n";
397 }
398 if ($sovpnsettings{DHCP_DOMAIN} ne '') {
399 print CONF "push \"dhcp-option DOMAIN $sovpnsettings{DHCP_DOMAIN}\"\n";
400 }
401
402 if ($sovpnsettings{DHCP_DNS} ne '') {
403 print CONF "push \"dhcp-option DNS $sovpnsettings{DHCP_DNS}\"\n";
404 }
405
406 if ($sovpnsettings{DHCP_WINS} ne '') {
407 print CONF "push \"dhcp-option WINS $sovpnsettings{DHCP_WINS}\"\n";
408 }
409
410 if ($sovpnsettings{DHCP_WINS} eq '') {
411 print CONF "max-clients 100\n";
412 }
413 if ($sovpnsettings{DHCP_WINS} ne '') {
414 print CONF "max-clients $sovpnsettings{MAX_CLIENTS}\n";
415 }
416 print CONF "tls-verify /var/ipfire/ovpn/verify\n";
417 print CONF "crl-verify /var/ipfire/ovpn/crls/cacrl.pem\n";
418 print CONF "user nobody\n";
419 print CONF "group nobody\n";
420 print CONF "persist-key\n";
421 print CONF "persist-tun\n";
422 if ($sovpnsettings{LOG_VERB} ne '') {
423 print CONF "verb $sovpnsettings{LOG_VERB}\n";
424 } else {
425 print CONF "verb 3\n";
426 }
427 print CONF "\n";
428
429 close(CONF);
430 }
431
432 sub emptyserverlog{
433 if (open(FILE, ">/var/log/ovpnserver.log")) {
434 flock FILE, 2;
435 print FILE "";
436 close FILE;
437 }
438
439 }
440
441 sub delccdnet
442 {
443 my %ccdconfhash = ();
444 my %ccdhash = ();
445 my $ccdnetname=$_[0];
446 if (-f "${General::swroot}/ovpn/ovpnconfig"){
447 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%ccdhash);
448 foreach my $key (keys %ccdhash) {
449 if ($ccdhash{$key}[32] eq $ccdnetname) {
450 $errormessage=$Lang::tr{'ccd err hostinnet'};
451 return;
452 }
453 }
454 }
455 &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
456 foreach my $key (keys %ccdconfhash) {
457 if ($ccdconfhash{$key}[0] eq $ccdnetname){
458 delete $ccdconfhash{$key};
459 }
460 }
461 &General::writehasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
462
463 &writeserverconf;
464 return 0;
465 }
466
467 sub addccdnet
468 {
469 my %ccdconfhash=();
470 my @ccdconf=();
471 my $ccdname=$_[0];
472 my $ccdnet=$_[1];
473 my $ovpnsubnet=$_[2];
474 my $subcidr;
475 my @ip2=();
476 my $checkup;
477 my $ccdip;
478 my $baseaddress;
479 if(!&General::validhostname($ccdname)){
480 $errormessage=$Lang::tr{'ccd err invalidname'};
481 return;
482 }
483 #check ip
484 if (&General::validipandmask($ccdnet)){
485 $ccdnet=&General::iporsubtocidr($ccdnet);
486 }else{
487 $errormessage=$Lang::tr{'ccd err invalidnet'};
488 return;
489 }
490 ($ccdip,$subcidr) = split (/\//,$ccdnet);
491 if ($ccdname eq '') {
492 $errormessage=$errormessage.$Lang::tr{'ccd err name'}."<br>";
493 }
494 #check if we try to use same network as ovpn server
495 if (&General::iporsubtocidr($ccdnet) eq &General::iporsubtocidr($ovpnsubnet)) {
496 $errormessage=$errormessage.$Lang::tr{'ccd err isovpnnet'}."<br>";
497 }
498
499 #check if we use a name/subnet that already exists
500 &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
501 foreach my $key (keys %ccdconfhash) {
502 @ccdconf=split(/\//,$ccdconfhash{$key}[1]);
503 if ($ccdname eq $ccdconfhash{$key}[0]) {$errormessage=$errormessage.$Lang::tr{'ccd err nameexist'}."<br>";}
504 my ($newip,$newsub) = split(/\//,$ccdnet);
505 if (&General::IpInSubnet($newip,$ccdconf[0],&General::iporsubtodec($ccdconf[1]))) {$errormessage=$errormessage.$Lang::tr{'ccd err issubnet'}."<br>";}
506
507 }
508 #check if we use one of ipfire's networks (green,orange,blue)
509 my %ownnet=();
510 &General::readhash("${General::swroot}/ethernet/settings", \%ownnet);
511 if (($ownnet{'GREEN_NETADDRESS'} ne '' && $ownnet{'GREEN_NETADDRESS'} ne '0.0.0.0') && &General::IpInSubnet($ownnet{'GREEN_NETADDRESS'},$ccdip,&General::iporsubtodec($subcidr))){ $errormessage=$Lang::tr{'ccd err green'};}
512 if (($ownnet{'ORANGE_NETADDRESS'} ne '' && $ownnet{'ORANGE_NETADDRESS'} ne '0.0.0.0') && &General::IpInSubnet($ownnet{'ORANGE_NETADDRESS'},$ccdip,&General::iporsubtodec($subcidr))){ $errormessage=$Lang::tr{'ccd err orange'};}
513 if (($ownnet{'BLUE_NETADDRESS'} ne '' && $ownnet{'BLUE_NETADDRESS'} ne '0.0.0.0') && &General::IpInSubnet($ownnet{'BLUE_NETADDRESS'},$ccdip,&General::iporsubtodec($subcidr))){ $errormessage=$Lang::tr{'ccd err blue'};}
514 if (($ownnet{'RED_NETADDRESS'} ne '' && $ownnet{'RED_NETADDRESS'} ne '0.0.0.0') && &General::IpInSubnet($ownnet{'RED_NETADDRESS'},$ccdip,&General::iporsubtodec($subcidr))){ $errormessage=$Lang::tr{'ccd err red'};}
515
516
517 if (!$errormessage) {
518 my %ccdconfhash=();
519 $baseaddress=&General::getnetworkip($ccdip,$subcidr);
520 &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
521 my $key = &General::findhasharraykey (\%ccdconfhash);
522 foreach my $i (0 .. 1) { $ccdconfhash{$key}[$i] = "";}
523 $ccdconfhash{$key}[0] = $ccdname;
524 $ccdconfhash{$key}[1] = $baseaddress."/".$subcidr;
525 &General::writehasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
526 &writeserverconf;
527 $cgiparams{'ccdname'}='';
528 $cgiparams{'ccdsubnet'}='';
529 return 1;
530 }
531 }
532
533 sub modccdnet
534 {
535
536 my $newname=$_[0];
537 my $oldname=$_[1];
538 my %ccdconfhash=();
539 my %ccdhash=();
540 &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
541 foreach my $key (keys %ccdconfhash) {
542 if ($ccdconfhash{$key}[0] eq $oldname) {
543 foreach my $key1 (keys %ccdconfhash) {
544 if ($ccdconfhash{$key1}[0] eq $newname){
545 $errormessage=$errormessage.$Lang::tr{'ccd err netadrexist'};
546 return;
547 }else{
548 $ccdconfhash{$key}[0]= $newname;
549 &General::writehasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
550 last;
551 }
552 }
553 }
554 }
555
556 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%ccdhash);
557 foreach my $key (keys %ccdhash) {
558 if ($ccdhash{$key}[32] eq $oldname) {
559 $ccdhash{$key}[32]=$newname;
560 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%ccdhash);
561 last;
562 }
563 }
564
565 return 0;
566 }
567 sub ccdmaxclients
568 {
569 my $ccdnetwork=$_[0];
570 my @octets=();
571 my @subnet=();
572 @octets=split("\/",$ccdnetwork);
573 @subnet= split /\./, &General::cidrtosub($octets[1]);
574 my ($a,$b,$c,$d,$e);
575 $a=256-$subnet[0];
576 $b=256-$subnet[1];
577 $c=256-$subnet[2];
578 $d=256-$subnet[3];
579 $e=($a*$b*$c*$d)/4;
580 return $e-1;
581 }
582
583 sub getccdadresses
584 {
585 my $ipin=$_[0];
586 my ($ip1,$ip2,$ip3,$ip4)=split /\./, $ipin;
587 my $cidr=$_[1];
588 chomp($cidr);
589 my $count=$_[2];
590 my $hasip=$_[3];
591 chomp($hasip);
592 my @iprange=();
593 my %ccdhash=();
594 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%ccdhash);
595 $iprange[0]=$ip1.".".$ip2.".".$ip3.".".2;
596 for (my $i=0;$i<=$count-1;$i++) {
597 my $tmpip=$iprange[$i-1];
598 my $stepper=$i*4;
599 $iprange[$i]= &General::getnextip($tmpip,4);
600 }
601 my $r=0;
602 foreach my $key (keys %ccdhash) {
603 $r=0;
604 foreach my $tmp (@iprange){
605 my ($net,$sub) = split (/\//,$ccdhash{$key}[33]);
606 if ($net eq $tmp) {
607 if ( $hasip ne $ccdhash{$key}[33] ){
608 splice (@iprange,$r,1);
609 }
610 }
611 $r++;
612 }
613 }
614 return @iprange;
615 }
616
617 sub fillselectbox
618 {
619 my $boxname=$_[1];
620 my ($ccdip,$subcidr) = split("/",$_[0]);
621 my $tz=$_[2];
622 my @allccdips=&getccdadresses($ccdip,$subcidr,&ccdmaxclients($ccdip."/".$subcidr),$tz);
623 print"<select name='$boxname' STYLE='font-family : arial; font-size : 9pt; width:130px;' >";
624 foreach (@allccdips) {
625 my $ip=$_."/30";
626 chomp($ip);
627 print "<option value='$ip' ";
628 if ( $ip eq $cgiparams{$boxname} ){
629 print"selected";
630 }
631 print ">$ip</option>";
632 }
633 print "</select>";
634 }
635
636 sub hostsinnet
637 {
638 my $name=$_[0];
639 my %ccdhash=();
640 my $i=0;
641 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%ccdhash);
642 foreach my $key (keys %ccdhash) {
643 if ($ccdhash{$key}[32] eq $name){ $i++;}
644 }
645 return $i;
646 }
647
648 sub check_routes_push
649 {
650 my $val=$_[0];
651 my ($ip,$cidr) = split (/\//, $val);
652 ##check for existing routes in routes_push
653 if (-e "${General::swroot}/ovpn/routes_push") {
654 open(FILE,"${General::swroot}/ovpn/routes_push");
655 while (<FILE>) {
656 $_=~s/\s*$//g;
657
658 my ($ip2,$cidr2) = split (/\//,"$_");
659 my $val2=$ip2."/".&General::iporsubtodec($cidr2);
660
661 if($val eq $val2){
662 return 0;
663 }
664 #subnetcheck
665 if (&General::IpInSubnet ($ip,$ip2,&General::iporsubtodec($cidr2))){
666 return 0;
667 }
668 };
669 close(FILE);
670 }
671 return 1;
672 }
673
674 sub check_ccdroute
675 {
676 my %ccdroutehash=();
677 my $val=$_[0];
678 my ($ip,$cidr) = split (/\//, $val);
679 #check for existing routes in ccdroute
680 &General::readhasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
681 foreach my $key (keys %ccdroutehash) {
682 foreach my $i (1 .. $#{$ccdroutehash{$key}}) {
683 if (&General::iporsubtodec($val) eq $ccdroutehash{$key}[$i] && $ccdroutehash{$key}[0] ne $cgiparams{'NAME'}){
684 return 0;
685 }
686 my ($ip2,$cidr2) = split (/\//,$ccdroutehash{$key}[$i]);
687 #subnetcheck
688 if (&General::IpInSubnet ($ip,$ip2,$cidr2)&& $ccdroutehash{$key}[0] ne $cgiparams{'NAME'} ){
689 return 0;
690 }
691 }
692 }
693 return 1;
694 }
695 sub check_ccdconf
696 {
697 my %ccdconfhash=();
698 my $val=$_[0];
699 my ($ip,$cidr) = split (/\//, $val);
700 #check for existing routes in ccdroute
701 &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
702 foreach my $key (keys %ccdconfhash) {
703 if (&General::iporsubtocidr($val) eq $ccdconfhash{$key}[1]){
704 return 0;
705 }
706 my ($ip2,$cidr2) = split (/\//,$ccdconfhash{$key}[1]);
707 #subnetcheck
708 if (&General::IpInSubnet ($ip,$ip2,&General::cidrtosub($cidr2))){
709 return 0;
710 }
711
712 }
713 return 1;
714 }
715
716 ###
717 # m.a.d net2net
718 ###
719
720 sub validdotmask
721 {
722 my $ipdotmask = $_[0];
723 if (&General::validip($ipdotmask)) { return 0; }
724 if (!($ipdotmask =~ /^(.*?)\/(.*?)$/)) { }
725 my $mask = $2;
726 if (($mask =~ /\./ )) { return 0; }
727 return 1;
728 }
729
730 # -------------------------------------------------------------------
731
732 sub write_routepushfile
733 {
734 open(FILE, ">$routes_push_file");
735 flock(FILE, 2);
736 if ($vpnsettings{'ROUTES_PUSH'} ne '') {
737 print FILE $vpnsettings{'ROUTES_PUSH'};
738 }
739 close(FILE);
740 }
741
742 sub read_routepushfile
743 {
744 if (-e "$routes_push_file") {
745 open(FILE,"$routes_push_file");
746 delete $vpnsettings{'ROUTES_PUSH'};
747 while (<FILE>) { $vpnsettings{'ROUTES_PUSH'} .= $_ };
748 close(FILE);
749 $cgiparams{'ROUTES_PUSH'} = $vpnsettings{'ROUTES_PUSH'};
750
751 }
752 }
753
754
755 #hier die refresh page
756 if ( -e "${General::swroot}/ovpn/gencanow") {
757 my $refresh = '';
758 $refresh = "<meta http-equiv='refresh' content='15;' />";
759 &Header::showhttpheaders();
760 &Header::openpage($Lang::tr{'OVPN'}, 1, $refresh);
761 &Header::openbigbox('100%', 'center');
762 &Header::openbox('100%', 'left', "$Lang::tr{'generate root/host certificates'}:");
763 print "<tr>\n<td align='center'><img src='/images/clock.gif' alt='' /></td>\n";
764 print "<td colspan='2'><font color='red'>Please be patient this realy can take some time on older hardware...</font></td></tr>\n";
765 &Header::closebox();
766 &Header::closebigbox();
767 &Header::closepage();
768 exit (0);
769 }
770 ##hier die refresh page
771
772
773 ###
774 ### OpenVPN Server Control
775 ###
776 if ($cgiparams{'ACTION'} eq $Lang::tr{'start ovpn server'} ||
777 $cgiparams{'ACTION'} eq $Lang::tr{'stop ovpn server'} ||
778 $cgiparams{'ACTION'} eq $Lang::tr{'restart ovpn server'}) {
779 #start openvpn server
780 if ($cgiparams{'ACTION'} eq $Lang::tr{'start ovpn server'}){
781 &emptyserverlog();
782 system('/usr/local/bin/openvpnctrl', '-s');
783 }
784 #stop openvpn server
785 if ($cgiparams{'ACTION'} eq $Lang::tr{'stop ovpn server'}){
786 system('/usr/local/bin/openvpnctrl', '-k');
787 &emptyserverlog();
788 }
789 # #restart openvpn server
790 # if ($cgiparams{'ACTION'} eq $Lang::tr{'restart ovpn server'}){
791 #workarund, till SIGHUP also works when running as nobody
792 # system('/usr/local/bin/openvpnctrl', '-r');
793 # &emptyserverlog();
794 # }
795 }
796
797 ###
798 ### Save Advanced options
799 ###
800
801 if ($cgiparams{'ACTION'} eq $Lang::tr{'save-adv-options'}) {
802 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
803 #DAN do we really need (to to check) this value? Besides if we listen on blue and orange too,
804 #DAN this value has to leave.
805 #new settings for daemon
806 $vpnsettings{'LOG_VERB'} = $cgiparams{'LOG_VERB'};
807 $vpnsettings{'KEEPALIVE_1'} = $cgiparams{'KEEPALIVE_1'};
808 $vpnsettings{'KEEPALIVE_2'} = $cgiparams{'KEEPALIVE_2'};
809 $vpnsettings{'MAX_CLIENTS'} = $cgiparams{'MAX_CLIENTS'};
810 $vpnsettings{'REDIRECT_GW_DEF1'} = $cgiparams{'REDIRECT_GW_DEF1'};
811 $vpnsettings{'CLIENT2CLIENT'} = $cgiparams{'CLIENT2CLIENT'};
812 $vpnsettings{'DHCP_DOMAIN'} = $cgiparams{'DHCP_DOMAIN'};
813 $vpnsettings{'DHCP_DNS'} = $cgiparams{'DHCP_DNS'};
814 $vpnsettings{'DHCP_WINS'} = $cgiparams{'DHCP_WINS'};
815 $vpnsettings{'ROUTES_PUSH'} = $cgiparams{'ROUTES_PUSH'};
816 my @temp=();
817
818 if ($cgiparams{'FRAGMENT'} eq '') {
819 delete $vpnsettings{'FRAGMENT'};
820 } else {
821 if ($cgiparams{'FRAGMENT'} !~ /^[0-9]+$/) {
822 $errormessage = "Incorrect value, please insert only numbers.";
823 goto ADV_ERROR;
824 } else {
825 $vpnsettings{'FRAGMENT'} = $cgiparams{'FRAGMENT'};
826 }
827 }
828 if ($cgiparams{'MSSFIX'} ne 'on') {
829 delete $vpnsettings{'MSSFIX'};
830 } else {
831 $vpnsettings{'MSSFIX'} = $cgiparams{'MSSFIX'};
832 }
833 if ($cgiparams{'DHCP_DOMAIN'} ne ''){
834 unless (&General::validfqdn($cgiparams{'DHCP_DOMAIN'}) || &General::validip($cgiparams{'DHCP_DOMAIN'})) {
835 $errormessage = $Lang::tr{'invalid input for dhcp domain'};
836 goto ADV_ERROR;
837 }
838 }
839 if ($cgiparams{'DHCP_DNS'} ne ''){
840 unless (&General::validfqdn($cgiparams{'DHCP_DNS'}) || &General::validip($cgiparams{'DHCP_DNS'})) {
841 $errormessage = $Lang::tr{'invalid input for dhcp dns'};
842 goto ADV_ERROR;
843 }
844 }
845 if ($cgiparams{'DHCP_WINS'} ne ''){
846 unless (&General::validfqdn($cgiparams{'DHCP_WINS'}) || &General::validip($cgiparams{'DHCP_WINS'})) {
847 $errormessage = $Lang::tr{'invalid input for dhcp wins'};
848 goto ADV_ERROR;
849 }
850 }
851 if ($cgiparams{'ROUTES_PUSH'} ne ''){
852 @temp = split(/\n/,$cgiparams{'ROUTES_PUSH'});
853 undef $vpnsettings{'ROUTES_PUSH'};
854
855 foreach my $tmpip (@temp)
856 {
857 s/^\s+//g; s/\s+$//g;
858
859 if ($tmpip)
860 {
861 $tmpip=~s/\s*$//g;
862 unless (&General::validipandmask($tmpip)) {
863 $errormessage = "$tmpip ".$Lang::tr{'ovpn errmsg invalid ip or mask'};
864 goto ADV_ERROR;
865 }
866 my ($ip, $cidr) = split("\/",&General::ipcidr2msk($tmpip));
867
868 if ($ip eq $netsettings{'GREEN_NETADDRESS'} && $cidr eq $netsettings{'GREEN_NETMASK'}) {
869 $errormessage = $Lang::tr{'ovpn errmsg green already pushed'};
870 goto ADV_ERROR;
871 }
872 # a.marx ccd
873 my %ccdroutehash=();
874 &General::readhasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
875 foreach my $key (keys %ccdroutehash) {
876 foreach my $i (1 .. $#{$ccdroutehash{$key}}) {
877 if ( $ip."/".$cidr eq $ccdroutehash{$key}[$i] ){
878 $errormessage="Route $ip\/$cidr ".$Lang::tr{'ccd err inuse'}." $ccdroutehash{$key}[0]" ;
879 goto ADV_ERROR;
880 }
881 my ($ip2,$cidr2) = split(/\//,$ccdroutehash{$key}[$i]);
882 if (&General::IpInSubnet ($ip,$ip2,$cidr2)){
883 $errormessage="Route $ip\/$cidr ".$Lang::tr{'ccd err inuse'}." $ccdroutehash{$key}[0]" ;
884 goto ADV_ERROR;
885 }
886 }
887 }
888
889 # ccd end
890
891 $vpnsettings{'ROUTES_PUSH'} .= $tmpip."\n";
892 }
893 }
894 &write_routepushfile;
895 undef $vpnsettings{'ROUTES_PUSH'};
896 }
897 else {
898 undef $vpnsettings{'ROUTES_PUSH'};
899 &write_routepushfile;
900 }
901 if ((length($cgiparams{'MAX_CLIENTS'}) == 0) || (($cgiparams{'MAX_CLIENTS'}) < 1 ) || (($cgiparams{'MAX_CLIENTS'}) > 255 )) {
902 $errormessage = $Lang::tr{'invalid input for max clients'};
903 goto ADV_ERROR;
904 }
905 if ($cgiparams{'KEEPALIVE_1'} ne '') {
906 if ($cgiparams{'KEEPALIVE_1'} !~ /^[0-9]+$/) {
907 $errormessage = $Lang::tr{'invalid input for keepalive 1'};
908 goto ADV_ERROR;
909 }
910 }
911 if ($cgiparams{'KEEPALIVE_2'} ne ''){
912 if ($cgiparams{'KEEPALIVE_2'} !~ /^[0-9]+$/) {
913 $errormessage = $Lang::tr{'invalid input for keepalive 2'};
914 goto ADV_ERROR;
915 }
916 }
917 if ($cgiparams{'KEEPALIVE_2'} < ($cgiparams{'KEEPALIVE_1'} * 2)){
918 $errormessage = $Lang::tr{'invalid input for keepalive 1:2'};
919 goto ADV_ERROR;
920 }
921
922 &General::writehash("${General::swroot}/ovpn/settings", \%vpnsettings);
923 &writeserverconf();#hier ok
924 }
925
926 ###
927 # m.a.d net2net
928 ###
929
930 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq 'net' && $cgiparams{'SIDE'} eq 'server')
931 {
932
933 my @remsubnet = split(/\//,$cgiparams{'REMOTE_SUBNET'});
934 my @ovsubnettemp = split(/\./,$cgiparams{'OVPN_SUBNET'});
935 my $ovsubnet = "$ovsubnettemp[0].$ovsubnettemp[1].$ovsubnettemp[2]";
936 my $tunmtu = '';
937
938 unless(-d "${General::swroot}/ovpn/n2nconf/"){mkdir "${General::swroot}/ovpn/n2nconf", 0755 or die "Unable to create dir $!";}
939 unless(-d "${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}"){mkdir "${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}", 0770 or die "Unable to create dir $!";}
940
941 open(SERVERCONF, ">${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}/$cgiparams{'NAME'}.conf") or die "Unable to open ${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}/$cgiparams{'NAME'}.conf: $!";
942
943 flock SERVERCONF, 2;
944 print SERVERCONF "# IPFire n2n Open VPN Server Config by ummeegge und m.a.d\n";
945 print SERVERCONF "\n";
946 print SERVERCONF "# User Security\n";
947 print SERVERCONF "user nobody\n";
948 print SERVERCONF "group nobody\n";
949 print SERVERCONF "persist-tun\n";
950 print SERVERCONF "persist-key\n";
951 print SERVERCONF "script-security 2\n";
952 print SERVERCONF "# IP/DNS for remote Server Gateway\n";
953 print SERVERCONF "remote $cgiparams{'REMOTE'}\n";
954 print SERVERCONF "float\n";
955 print SERVERCONF "# IP adresses of the VPN Subnet\n";
956 print SERVERCONF "ifconfig $ovsubnet.1 $ovsubnet.2\n";
957 print SERVERCONF "# Client Gateway Network\n";
958 print SERVERCONF "route $remsubnet[0] $remsubnet[1]\n";
959 print SERVERCONF "# tun Device\n";
960 print SERVERCONF "dev tun\n";
961 print SERVERCONF "# Port and Protokol\n";
962 print SERVERCONF "port $cgiparams{'DEST_PORT'}\n";
963
964 if ($cgiparams{'PROTOCOL'} eq 'tcp') {
965 print SERVERCONF "proto tcp-server\n";
966 print SERVERCONF "# Packet size\n";
967 if ($cgiparams{'MTU'} eq '') {$tunmtu = '1400'} else {$tunmtu = $cgiparams{'MTU'}};
968 print SERVERCONF "tun-mtu $tunmtu\n";
969 }
970
971 if ($cgiparams{'PROTOCOL'} eq 'udp') {
972 print SERVERCONF "proto udp\n";
973 print SERVERCONF "# Paketsize\n";
974 if ($cgiparams{'MTU'} eq '') {$tunmtu = '1500'} else {$tunmtu = $cgiparams{'MTU'}};
975 print SERVERCONF "tun-mtu $tunmtu\n";
976 if ($cgiparams{'FRAGMENT'} ne '') {print SERVERCONF "fragment $cgiparams{'FRAGMENT'}\n";}
977 if ($cgiparams{'MSSFIX'} eq 'on') {print SERVERCONF "mssfix\n"; };
978 }
979 print SERVERCONF "# Auth. Server\n";
980 print SERVERCONF "tls-server\n";
981 print SERVERCONF "ca ${General::swroot}/ovpn/ca/cacert.pem\n";
982 print SERVERCONF "cert ${General::swroot}/ovpn/certs/servercert.pem\n";
983 print SERVERCONF "key ${General::swroot}/ovpn/certs/serverkey.pem\n";
984 print SERVERCONF "dh ${General::swroot}/ovpn/ca/dh1024.pem\n";
985 print SERVERCONF "# Cipher\n";
986 print SERVERCONF "cipher AES-256-CBC\n";
987 if ($cgiparams{'COMPLZO'} eq 'on') {
988 print SERVERCONF "# Enable Compression\n";
989 print SERVERCONF "comp-lzo\r\n";
990 }
991 print SERVERCONF "# Debug Level\n";
992 print SERVERCONF "verb 3\n";
993 print SERVERCONF "# Tunnel check\n";
994 print SERVERCONF "keepalive 10 60\n";
995 print SERVERCONF "# Start as daemon\n";
996 print SERVERCONF "daemon $cgiparams{'NAME'}n2n\n";
997 print SERVERCONF "writepid /var/run/$cgiparams{'NAME'}n2n.pid\n";
998 print SERVERCONF "# Activate Management Interface and Port\n";
999 if ($cgiparams{'OVPN_MGMT'} eq '') {print SERVERCONF "management localhost $cgiparams{'DEST_PORT'}\n"}
1000 else {print SERVERCONF "management localhost $cgiparams{'OVPN_MGMT'}\n"};
1001 close(SERVERCONF);
1002
1003 }
1004
1005 ###
1006 # m.a.d net2net
1007 ###
1008
1009 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq 'net' && $cgiparams{'SIDE'} eq 'client')
1010 {
1011 my @ovsubnettemp = split(/\./,$cgiparams{'OVPN_SUBNET'});
1012 my $ovsubnet = "$ovsubnettemp[0].$ovsubnettemp[1].$ovsubnettemp[2]";
1013 my @remsubnet = split(/\//,$cgiparams{'REMOTE_SUBNET'});
1014 my $tunmtu = '';
1015
1016 unless(-d "${General::swroot}/ovpn/n2nconf/"){mkdir "${General::swroot}/ovpn/n2nconf", 0755 or die "Unable to create dir $!";}
1017 unless(-d "${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}"){mkdir "${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}", 0770 or die "Unable to create dir $!";}
1018
1019 open(CLIENTCONF, ">${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}/$cgiparams{'NAME'}.conf") or die "Unable to open ${General::swroot}/ovpn/n2nconf/$cgiparams{'NAME'}/$cgiparams{'NAME'}.conf: $!";
1020
1021 flock CLIENTCONF, 2;
1022 print CLIENTCONF "# IPFire rewritten n2n Open VPN Client Config by ummeegge und m.a.d\n";
1023 print CLIENTCONF "#\n";
1024 print CLIENTCONF "# User Security\n";
1025 print CLIENTCONF "user nobody\n";
1026 print CLIENTCONF "group nobody\n";
1027 print CLIENTCONF "persist-tun\n";
1028 print CLIENTCONF "persist-key\n";
1029 print CLIENTCONF "script-security 2\n";
1030 print CLIENTCONF "# IP/DNS for remote Server Gateway\n";
1031 print CLIENTCONF "remote $cgiparams{'REMOTE'}\n";
1032 print CLIENTCONF "float\n";
1033 print CLIENTCONF "# IP adresses of the VPN Subnet\n";
1034 print CLIENTCONF "ifconfig $ovsubnet.2 $ovsubnet.1\n";
1035 print CLIENTCONF "# Server Gateway Network\n";
1036 print CLIENTCONF "route $remsubnet[0] $remsubnet[1]\n";
1037 print CLIENTCONF "# tun Device\n";
1038 print CLIENTCONF "dev tun\n";
1039 print CLIENTCONF "# Port and Protokol\n";
1040 print CLIENTCONF "port $cgiparams{'DEST_PORT'}\n";
1041
1042 if ($cgiparams{'PROTOCOL'} eq 'tcp') {
1043 print CLIENTCONF "proto tcp-client\n";
1044 print CLIENTCONF "# Packet size\n";
1045 if ($cgiparams{'MTU'} eq '') {$tunmtu = '1400'} else {$tunmtu = $cgiparams{'MTU'}};
1046 print CLIENTCONF "tun-mtu $tunmtu\n";
1047 }
1048
1049 if ($cgiparams{'PROTOCOL'} eq 'udp') {
1050 print CLIENTCONF "proto udp\n";
1051 print CLIENTCONF "# Paketsize\n";
1052 if ($cgiparams{'MTU'} eq '') {$tunmtu = '1500'} else {$tunmtu = $cgiparams{'MTU'}};
1053 print CLIENTCONF "tun-mtu $tunmtu\n";
1054 if ($cgiparams{'FRAGMENT'} ne '') {print CLIENTCONF "fragment $cgiparams{'FRAGMENT'}\n";}
1055 if ($cgiparams{'MSSFIX'} eq 'on') {print CLIENTCONF "mssfix\n"; };
1056 }
1057
1058 print CLIENTCONF "ns-cert-type server\n";
1059 print CLIENTCONF "# Auth. Client\n";
1060 print CLIENTCONF "tls-client\n";
1061 print CLIENTCONF "# Cipher\n";
1062 print CLIENTCONF "cipher AES-256-CBC\n";
1063 print CLIENTCONF "pkcs12 ${General::swroot}/ovpn/certs/$cgiparams{'NAME'}.p12\r\n";
1064 if ($cgiparams{'COMPLZO'} eq 'on') {
1065 print CLIENTCONF "# Enable Compression\n";
1066 print CLIENTCONF "comp-lzo\r\n";
1067 }
1068 print CLIENTCONF "# Debug Level\n";
1069 print CLIENTCONF "verb 3\n";
1070 print CLIENTCONF "# Tunnel check\n";
1071 print CLIENTCONF "keepalive 10 60\n";
1072 print CLIENTCONF "# Start as daemon\n";
1073 print CLIENTCONF "daemon $cgiparams{'NAME'}n2n\n";
1074 print CLIENTCONF "writepid /var/run/$cgiparams{'NAME'}n2n.pid\n";
1075 print CLIENTCONF "# Activate Management Interface and Port\n";
1076 if ($cgiparams{'OVPN_MGMT'} eq '') {print CLIENTCONF "management localhost $cgiparams{'DEST_PORT'}\n"}
1077 else {print CLIENTCONF "management localhost $cgiparams{'OVPN_MGMT'}\n"};
1078 close(CLIENTCONF);
1079
1080 }
1081
1082 ###
1083 ### Save main settings
1084 ###
1085
1086
1087 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq '' && $cgiparams{'KEY'} eq '') {
1088 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1089 #DAN do we really need (to to check) this value? Besides if we listen on blue and orange too,
1090 #DAN this value has to leave.
1091 if ($cgiparams{'ENABLED'} eq 'on'){
1092 unless (&General::validfqdn($cgiparams{'VPN_IP'}) || &General::validip($cgiparams{'VPN_IP'})) {
1093 $errormessage = $Lang::tr{'invalid input for hostname'};
1094 goto SETTINGS_ERROR;
1095 }
1096 }
1097 if ($cgiparams{'ENABLED'} eq 'on'){
1098 &disallowreserved($cgiparams{'DDEST_PORT'},0,$cgiparams{'DPROTOCOL'},"dest");
1099 }
1100 if ($errormessage) { goto SETTINGS_ERROR; }
1101
1102
1103 if ($cgiparams{'ENABLED'} eq 'on'){
1104 &checkportfw(0,$cgiparams{'DDEST_PORT'},$cgiparams{'DPROTOCOL'},'0.0.0.0');
1105 }
1106
1107 if ($errormessage) { goto SETTINGS_ERROR; }
1108
1109 if (! &General::validipandmask($cgiparams{'DOVPN_SUBNET'})) {
1110 $errormessage = $Lang::tr{'ovpn subnet is invalid'};
1111 goto SETTINGS_ERROR;
1112 }
1113 my @tmpovpnsubnet = split("\/",$cgiparams{'DOVPN_SUBNET'});
1114
1115 if (&General::IpInSubnet ( $netsettings{'RED_ADDRESS'},
1116 $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1117 $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire RED Network $netsettings{'RED_ADDRESS'}";
1118 goto SETTINGS_ERROR;
1119 }
1120
1121 if (&General::IpInSubnet ( $netsettings{'GREEN_ADDRESS'},
1122 $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1123 $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire Green Network $netsettings{'GREEN_ADDRESS'}";
1124 goto SETTINGS_ERROR;
1125 }
1126
1127 if (&General::IpInSubnet ( $netsettings{'BLUE_ADDRESS'},
1128 $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1129 $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire Blue Network $netsettings{'BLUE_ADDRESS'}";
1130 goto SETTINGS_ERROR;
1131 }
1132
1133 if (&General::IpInSubnet ( $netsettings{'ORANGE_ADDRESS'},
1134 $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1135 $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire Orange Network $netsettings{'ORANGE_ADDRESS'}";
1136 goto SETTINGS_ERROR;
1137 }
1138 open(ALIASES, "${General::swroot}/ethernet/aliases") or die 'Unable to open aliases file.';
1139 while (<ALIASES>)
1140 {
1141 chomp($_);
1142 my @tempalias = split(/\,/,$_);
1143 if ($tempalias[1] eq 'on') {
1144 if (&General::IpInSubnet ($tempalias[0] ,
1145 $tmpovpnsubnet[0], $tmpovpnsubnet[1])) {
1146 $errormessage = "$Lang::tr{'ovpn subnet overlap'} IPFire alias entry $tempalias[0]";
1147 }
1148 }
1149 }
1150 close(ALIASES);
1151 if ($errormessage ne ''){
1152 goto SETTINGS_ERROR;
1153 }
1154 if ($cgiparams{'ENABLED'} !~ /^(on|off)$/) {
1155 $errormessage = $Lang::tr{'invalid input'};
1156 goto SETTINGS_ERROR;
1157 }
1158 if ((length($cgiparams{'DMTU'})==0) || (($cgiparams{'DMTU'}) < 1000 )) {
1159 $errormessage = $Lang::tr{'invalid mtu input'};
1160 goto SETTINGS_ERROR;
1161 }
1162
1163 unless (&General::validport($cgiparams{'DDEST_PORT'})) {
1164 $errormessage = $Lang::tr{'invalid port'};
1165 goto SETTINGS_ERROR;
1166 }
1167 $vpnsettings{'ENABLED_BLUE'} = $cgiparams{'ENABLED_BLUE'};
1168 $vpnsettings{'ENABLED_ORANGE'} =$cgiparams{'ENABLED_ORANGE'};
1169 $vpnsettings{'ENABLED'} = $cgiparams{'ENABLED'};
1170 $vpnsettings{'VPN_IP'} = $cgiparams{'VPN_IP'};
1171 #new settings for daemon
1172 $vpnsettings{'DOVPN_SUBNET'} = $cgiparams{'DOVPN_SUBNET'};
1173 $vpnsettings{'DDEVICE'} = $cgiparams{'DDEVICE'};
1174 $vpnsettings{'DPROTOCOL'} = $cgiparams{'DPROTOCOL'};
1175 $vpnsettings{'DDEST_PORT'} = $cgiparams{'DDEST_PORT'};
1176 $vpnsettings{'DMTU'} = $cgiparams{'DMTU'};
1177 $vpnsettings{'DCOMPLZO'} = $cgiparams{'DCOMPLZO'};
1178 $vpnsettings{'DCIPHER'} = $cgiparams{'DCIPHER'};
1179 #wrtie enable
1180
1181 if ( $vpnsettings{'ENABLED_BLUE'} eq 'on' ) {system("touch ${General::swroot}/ovpn/enable_blue 2>/dev/null");}else{system("unlink ${General::swroot}/ovpn/enable_blue 2>/dev/null");}
1182 if ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' ) {system("touch ${General::swroot}/ovpn/enable_orange 2>/dev/null");}else{system("unlink ${General::swroot}/ovpn/enable_orange 2>/dev/null");}
1183 if ( $vpnsettings{'ENABLED'} eq 'on' ) {system("touch ${General::swroot}/ovpn/enable 2>/dev/null");}else{system("unlink ${General::swroot}/ovpn/enable 2>/dev/null");}
1184 #new settings for daemon
1185 &General::writehash("${General::swroot}/ovpn/settings", \%vpnsettings);
1186 &writeserverconf();#hier ok
1187 SETTINGS_ERROR:
1188 ###
1189 ### Reset all step 2
1190 ###
1191 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'reset'} && $cgiparams{'AREUSURE'} eq 'yes') {
1192 my $file = '';
1193 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1194
1195 foreach my $key (keys %confighash) {
1196 if ($confighash{$key}[4] eq 'cert') {
1197 delete $confighash{$cgiparams{'$key'}};
1198 }
1199 }
1200 while ($file = glob("${General::swroot}/ovpn/ca/*")) {
1201 unlink $file
1202 }
1203 while ($file = glob("${General::swroot}/ovpn/certs/*")) {
1204 unlink $file
1205 }
1206 while ($file = glob("${General::swroot}/ovpn/crls/*")) {
1207 unlink $file
1208 }
1209 &cleanssldatabase();
1210 if (open(FILE, ">${General::swroot}/ovpn/caconfig")) {
1211 print FILE "";
1212 close FILE;
1213 }
1214 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1215 #&writeserverconf();
1216 ###
1217 ### Reset all step 1
1218 ###
1219 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'reset'}) {
1220 &Header::showhttpheaders();
1221 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1222 &Header::openbigbox('100%', 'LEFT', '', '');
1223 &Header::openbox('100%', 'LEFT', $Lang::tr{'are you sure'});
1224 print <<END
1225 <table><form method='post'><input type='hidden' name='AREUSURE' value='yes' />
1226 <tr><td align='center'>
1227 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>:
1228 $Lang::tr{'resetting the vpn configuration will remove the root ca, the host certificate and all certificate based connections'}
1229 <tr><td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reset'}' />
1230 <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></td></tr>
1231 </form></table>
1232 END
1233 ;
1234 &Header::closebox();
1235 &Header::closebigbox();
1236 &Header::closepage();
1237 exit (0);
1238
1239 ###
1240 ### Upload CA Certificate
1241 ###
1242 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload ca certificate'}) {
1243 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1244
1245 if ($cgiparams{'CA_NAME'} !~ /^[a-zA-Z0-9]+$/) {
1246 $errormessage = $Lang::tr{'name must only contain characters'};
1247 goto UPLOADCA_ERROR;
1248 }
1249
1250 if (length($cgiparams{'CA_NAME'}) >60) {
1251 $errormessage = $Lang::tr{'name too long'};
1252 goto VPNCONF_ERROR;
1253 }
1254
1255 if ($cgiparams{'CA_NAME'} eq 'ca') {
1256 $errormessage = $Lang::tr{'name is invalid'};
1257 goto UPLOAD_CA_ERROR;
1258 }
1259
1260 # Check if there is no other entry with this name
1261 foreach my $key (keys %cahash) {
1262 if ($cahash{$key}[0] eq $cgiparams{'CA_NAME'}) {
1263 $errormessage = $Lang::tr{'a ca certificate with this name already exists'};
1264 goto UPLOADCA_ERROR;
1265 }
1266 }
1267
1268 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1269 $errormessage = $Lang::tr{'there was no file upload'};
1270 goto UPLOADCA_ERROR;
1271 }
1272 # Move uploaded ca to a temporary file
1273 (my $fh, my $filename) = tempfile( );
1274 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1275 $errormessage = $!;
1276 goto UPLOADCA_ERROR;
1277 }
1278 my $temp = `/usr/bin/openssl x509 -text -in $filename`;
1279 if ($temp !~ /CA:TRUE/i) {
1280 $errormessage = $Lang::tr{'not a valid ca certificate'};
1281 unlink ($filename);
1282 goto UPLOADCA_ERROR;
1283 } else {
1284 move($filename, "${General::swroot}/ovpn/ca/$cgiparams{'CA_NAME'}cert.pem");
1285 if ($? ne 0) {
1286 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1287 unlink ($filename);
1288 goto UPLOADCA_ERROR;
1289 }
1290 }
1291
1292 my $casubject = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/ca/$cgiparams{'CA_NAME'}cert.pem`;
1293 $casubject =~ /Subject: (.*)[\n]/;
1294 $casubject = $1;
1295 $casubject =~ s+/Email+, E+;
1296 $casubject =~ s/ ST=/ S=/;
1297 $casubject = &Header::cleanhtml($casubject);
1298
1299 my $key = &General::findhasharraykey (\%cahash);
1300 $cahash{$key}[0] = $cgiparams{'CA_NAME'};
1301 $cahash{$key}[1] = $casubject;
1302 &General::writehasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1303 # system('/usr/local/bin/ipsecctrl', 'R');
1304
1305 UPLOADCA_ERROR:
1306
1307 ###
1308 ### Display ca certificate
1309 ###
1310 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show ca certificate'}) {
1311 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1312
1313 if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem") {
1314 &Header::showhttpheaders();
1315 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1316 &Header::openbigbox('100%', 'LEFT', '', $errormessage);
1317 &Header::openbox('100%', 'LEFT', "$Lang::tr{'ca certificate'}:");
1318 my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
1319 $output = &Header::cleanhtml($output,"y");
1320 print "<pre>$output</pre>\n";
1321 &Header::closebox();
1322 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1323 &Header::closebigbox();
1324 &Header::closepage();
1325 exit(0);
1326 } else {
1327 $errormessage = $Lang::tr{'invalid key'};
1328 }
1329
1330 ###
1331 ### Download ca certificate
1332 ###
1333 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download ca certificate'}) {
1334 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1335
1336 if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
1337 print "Content-Type: application/octet-stream\r\n";
1338 print "Content-Disposition: filename=$cahash{$cgiparams{'KEY'}}[0]cert.pem\r\n\r\n";
1339 print `/usr/bin/openssl x509 -in ${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
1340 exit(0);
1341 } else {
1342 $errormessage = $Lang::tr{'invalid key'};
1343 }
1344
1345 ###
1346 ### Remove ca certificate (step 2)
1347 ###
1348 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'} && $cgiparams{'AREUSURE'} eq 'yes') {
1349 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1350 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1351
1352 if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
1353 foreach my $key (keys %confighash) {
1354 my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem ${General::swroot}/ovpn/certs/$confighash{$key}[1]cert.pem`;
1355 if ($test =~ /: OK/) {
1356 # Delete connection
1357 # if ($vpnsettings{'ENABLED'} eq 'on' ||
1358 # $vpnsettings{'ENABLED_BLUE'} eq 'on') {
1359 # system('/usr/local/bin/ipsecctrl', 'D', $key);
1360 # }
1361 unlink ("${General::swroot}/ovpn//certs/$confighash{$key}[1]cert.pem");
1362 unlink ("${General::swroot}/ovpn/certs/$confighash{$key}[1].p12");
1363 delete $confighash{$key};
1364 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1365 # &writeipsecfiles();
1366 }
1367 }
1368 unlink ("${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
1369 delete $cahash{$cgiparams{'KEY'}};
1370 &General::writehasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1371 # system('/usr/local/bin/ipsecctrl', 'R');
1372 } else {
1373 $errormessage = $Lang::tr{'invalid key'};
1374 }
1375 ###
1376 ### Remove ca certificate (step 1)
1377 ###
1378 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'}) {
1379 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1380 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1381
1382 my $assignedcerts = 0;
1383 if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
1384 foreach my $key (keys %confighash) {
1385 my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem ${General::swroot}/ovpn/certs/$confighash{$key}[1]cert.pem`;
1386 if ($test =~ /: OK/) {
1387 $assignedcerts++;
1388 }
1389 }
1390 if ($assignedcerts) {
1391 &Header::showhttpheaders();
1392 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1393 &Header::openbigbox('100%', 'LEFT', '', $errormessage);
1394 &Header::openbox('100%', 'LEFT', $Lang::tr{'are you sure'});
1395 print <<END
1396 <table><form method='post'><input type='hidden' name='AREUSURE' value='yes' />
1397 <input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />
1398 <tr><td align='center'>
1399 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>: $assignedcerts
1400 $Lang::tr{'connections are associated with this ca. deleting the ca will delete these connections as well.'}
1401 <tr><td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'remove ca certificate'}' />
1402 <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></td></tr>
1403 </form></table>
1404 END
1405 ;
1406 &Header::closebox();
1407 &Header::closebigbox();
1408 &Header::closepage();
1409 exit (0);
1410 } else {
1411 unlink ("${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
1412 delete $cahash{$cgiparams{'KEY'}};
1413 &General::writehasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1414 # system('/usr/local/bin/ipsecctrl', 'R');
1415 }
1416 } else {
1417 $errormessage = $Lang::tr{'invalid key'};
1418 }
1419
1420 ###
1421 ### Display root certificate
1422 ###
1423 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'} ||
1424 $cgiparams{'ACTION'} eq $Lang::tr{'show host certificate'}) {
1425 my $output;
1426 &Header::showhttpheaders();
1427 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1428 &Header::openbigbox('100%', 'LEFT', '', '');
1429 if ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'}) {
1430 &Header::openbox('100%', 'LEFT', "$Lang::tr{'root certificate'}:");
1431 $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/ca/cacert.pem`;
1432 } else {
1433 &Header::openbox('100%', 'LEFT', "$Lang::tr{'host certificate'}:");
1434 $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/certs/servercert.pem`;
1435 }
1436 $output = &Header::cleanhtml($output,"y");
1437 print "<pre>$output</pre>\n";
1438 &Header::closebox();
1439 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1440 &Header::closebigbox();
1441 &Header::closepage();
1442 exit(0);
1443
1444 ###
1445 ### Download root certificate
1446 ###
1447 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download root certificate'}) {
1448 if ( -f "${General::swroot}/ovpn/ca/cacert.pem" ) {
1449 print "Content-Type: application/octet-stream\r\n";
1450 print "Content-Disposition: filename=cacert.pem\r\n\r\n";
1451 print `/usr/bin/openssl x509 -in ${General::swroot}/ovpn/ca/cacert.pem`;
1452 exit(0);
1453 }
1454
1455 ###
1456 ### Download host certificate
1457 ###
1458 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download host certificate'}) {
1459 if ( -f "${General::swroot}/ovpn/certs/servercert.pem" ) {
1460 print "Content-Type: application/octet-stream\r\n";
1461 print "Content-Disposition: filename=servercert.pem\r\n\r\n";
1462 print `/usr/bin/openssl x509 -in ${General::swroot}/ovpn/certs/servercert.pem`;
1463 exit(0);
1464 }
1465 ###
1466 ### Form for generating a root certificate
1467 ###
1468 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'generate root/host certificates'} ||
1469 $cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
1470
1471 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1472 if (-f "${General::swroot}/ovpn/ca/cacert.pem") {
1473 $errormessage = $Lang::tr{'valid root certificate already exists'};
1474 $cgiparams{'ACTION'} = '';
1475 goto ROOTCERT_ERROR;
1476 }
1477
1478 if (($cgiparams{'ROOTCERT_HOSTNAME'} eq '') && -e "${General::swroot}/red/active") {
1479 if (open(IPADDR, "${General::swroot}/red/local-ipaddress")) {
1480 my $ipaddr = <IPADDR>;
1481 close IPADDR;
1482 chomp ($ipaddr);
1483 $cgiparams{'ROOTCERT_HOSTNAME'} = (gethostbyaddr(pack("C4", split(/\./, $ipaddr)), 2))[0];
1484 if ($cgiparams{'ROOTCERT_HOSTNAME'} eq '') {
1485 $cgiparams{'ROOTCERT_HOSTNAME'} = $ipaddr;
1486 }
1487 }
1488 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
1489
1490 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1491 $errormessage = $Lang::tr{'there was no file upload'};
1492 goto ROOTCERT_ERROR;
1493 }
1494
1495 # Move uploaded certificate request to a temporary file
1496 (my $fh, my $filename) = tempfile( );
1497 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1498 $errormessage = $!;
1499 goto ROOTCERT_ERROR;
1500 }
1501
1502 # Create a temporary dirctory
1503 my $tempdir = tempdir( CLEANUP => 1 );
1504
1505 # Extract the CA certificate from the file
1506 my $pid = open(OPENSSL, "|-");
1507 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1508 if ($pid) { # parent
1509 if ($cgiparams{'P12_PASS'} ne '') {
1510 print OPENSSL "$cgiparams{'P12_PASS'}\n";
1511 }
1512 close (OPENSSL);
1513 if ($?) {
1514 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1515 unlink ($filename);
1516 goto ROOTCERT_ERROR;
1517 }
1518 } else { # child
1519 unless (exec ('/usr/bin/openssl', 'pkcs12', '-cacerts', '-nokeys',
1520 '-in', $filename,
1521 '-out', "$tempdir/cacert.pem")) {
1522 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1523 unlink ($filename);
1524 goto ROOTCERT_ERROR;
1525 }
1526 }
1527
1528 # Extract the Host certificate from the file
1529 $pid = open(OPENSSL, "|-");
1530 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1531 if ($pid) { # parent
1532 if ($cgiparams{'P12_PASS'} ne '') {
1533 print OPENSSL "$cgiparams{'P12_PASS'}\n";
1534 }
1535 close (OPENSSL);
1536 if ($?) {
1537 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1538 unlink ($filename);
1539 goto ROOTCERT_ERROR;
1540 }
1541 } else { # child
1542 unless (exec ('/usr/bin/openssl', 'pkcs12', '-clcerts', '-nokeys',
1543 '-in', $filename,
1544 '-out', "$tempdir/hostcert.pem")) {
1545 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1546 unlink ($filename);
1547 goto ROOTCERT_ERROR;
1548 }
1549 }
1550
1551 # Extract the Host key from the file
1552 $pid = open(OPENSSL, "|-");
1553 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1554 if ($pid) { # parent
1555 if ($cgiparams{'P12_PASS'} ne '') {
1556 print OPENSSL "$cgiparams{'P12_PASS'}\n";
1557 }
1558 close (OPENSSL);
1559 if ($?) {
1560 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1561 unlink ($filename);
1562 goto ROOTCERT_ERROR;
1563 }
1564 } else { # child
1565 unless (exec ('/usr/bin/openssl', 'pkcs12', '-nocerts',
1566 '-nodes',
1567 '-in', $filename,
1568 '-out', "$tempdir/serverkey.pem")) {
1569 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1570 unlink ($filename);
1571 goto ROOTCERT_ERROR;
1572 }
1573 }
1574
1575 move("$tempdir/cacert.pem", "${General::swroot}/ovpn/ca/cacert.pem");
1576 if ($? ne 0) {
1577 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1578 unlink ($filename);
1579 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1580 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1581 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1582 goto ROOTCERT_ERROR;
1583 }
1584
1585 move("$tempdir/hostcert.pem", "${General::swroot}/ovpn/certs/servercert.pem");
1586 if ($? ne 0) {
1587 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1588 unlink ($filename);
1589 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1590 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1591 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1592 goto ROOTCERT_ERROR;
1593 }
1594
1595 move("$tempdir/serverkey.pem", "${General::swroot}/ovpn/certs/serverkey.pem");
1596 if ($? ne 0) {
1597 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1598 unlink ($filename);
1599 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1600 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1601 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1602 goto ROOTCERT_ERROR;
1603 }
1604
1605 goto ROOTCERT_SUCCESS;
1606
1607 } elsif ($cgiparams{'ROOTCERT_COUNTRY'} ne '') {
1608
1609 # Validate input since the form was submitted
1610 if ($cgiparams{'ROOTCERT_ORGANIZATION'} eq ''){
1611 $errormessage = $Lang::tr{'organization cant be empty'};
1612 goto ROOTCERT_ERROR;
1613 }
1614 if (length($cgiparams{'ROOTCERT_ORGANIZATION'}) >60) {
1615 $errormessage = $Lang::tr{'organization too long'};
1616 goto ROOTCERT_ERROR;
1617 }
1618 if ($cgiparams{'ROOTCERT_ORGANIZATION'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1619 $errormessage = $Lang::tr{'invalid input for organization'};
1620 goto ROOTCERT_ERROR;
1621 }
1622 if ($cgiparams{'ROOTCERT_HOSTNAME'} eq ''){
1623 $errormessage = $Lang::tr{'hostname cant be empty'};
1624 goto ROOTCERT_ERROR;
1625 }
1626 unless (&General::validfqdn($cgiparams{'ROOTCERT_HOSTNAME'}) || &General::validip($cgiparams{'ROOTCERT_HOSTNAME'})) {
1627 $errormessage = $Lang::tr{'invalid input for hostname'};
1628 goto ROOTCERT_ERROR;
1629 }
1630 if ($cgiparams{'ROOTCERT_EMAIL'} ne '' && (! &General::validemail($cgiparams{'ROOTCERT_EMAIL'}))) {
1631 $errormessage = $Lang::tr{'invalid input for e-mail address'};
1632 goto ROOTCERT_ERROR;
1633 }
1634 if (length($cgiparams{'ROOTCERT_EMAIL'}) > 40) {
1635 $errormessage = $Lang::tr{'e-mail address too long'};
1636 goto ROOTCERT_ERROR;
1637 }
1638 if ($cgiparams{'ROOTCERT_OU'} ne '' && $cgiparams{'ROOTCERT_OU'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1639 $errormessage = $Lang::tr{'invalid input for department'};
1640 goto ROOTCERT_ERROR;
1641 }
1642 if ($cgiparams{'ROOTCERT_CITY'} ne '' && $cgiparams{'ROOTCERT_CITY'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1643 $errormessage = $Lang::tr{'invalid input for city'};
1644 goto ROOTCERT_ERROR;
1645 }
1646 if ($cgiparams{'ROOTCERT_STATE'} ne '' && $cgiparams{'ROOTCERT_STATE'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
1647 $errormessage = $Lang::tr{'invalid input for state or province'};
1648 goto ROOTCERT_ERROR;
1649 }
1650 if ($cgiparams{'ROOTCERT_COUNTRY'} !~ /^[A-Z]*$/) {
1651 $errormessage = $Lang::tr{'invalid input for country'};
1652 goto ROOTCERT_ERROR;
1653 }
1654
1655 # Copy the cgisettings to vpnsettings and save the configfile
1656 $vpnsettings{'ROOTCERT_ORGANIZATION'} = $cgiparams{'ROOTCERT_ORGANIZATION'};
1657 $vpnsettings{'ROOTCERT_HOSTNAME'} = $cgiparams{'ROOTCERT_HOSTNAME'};
1658 $vpnsettings{'ROOTCERT_EMAIL'} = $cgiparams{'ROOTCERT_EMAIL'};
1659 $vpnsettings{'ROOTCERT_OU'} = $cgiparams{'ROOTCERT_OU'};
1660 $vpnsettings{'ROOTCERT_CITY'} = $cgiparams{'ROOTCERT_CITY'};
1661 $vpnsettings{'ROOTCERT_STATE'} = $cgiparams{'ROOTCERT_STATE'};
1662 $vpnsettings{'ROOTCERT_COUNTRY'} = $cgiparams{'ROOTCERT_COUNTRY'};
1663 &General::writehash("${General::swroot}/ovpn/settings", \%vpnsettings);
1664
1665 # Replace empty strings with a .
1666 (my $ou = $cgiparams{'ROOTCERT_OU'}) =~ s/^\s*$/\./;
1667 (my $city = $cgiparams{'ROOTCERT_CITY'}) =~ s/^\s*$/\./;
1668 (my $state = $cgiparams{'ROOTCERT_STATE'}) =~ s/^\s*$/\./;
1669
1670 # refresh
1671 #system ('/bin/touch', "${General::swroot}/ovpn/gencanow");
1672
1673 # Create the CA certificate
1674 my $pid = open(OPENSSL, "|-");
1675 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1676 if ($pid) { # parent
1677 print OPENSSL "$cgiparams{'ROOTCERT_COUNTRY'}\n";
1678 print OPENSSL "$state\n";
1679 print OPENSSL "$city\n";
1680 print OPENSSL "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
1681 print OPENSSL "$ou\n";
1682 print OPENSSL "$cgiparams{'ROOTCERT_ORGANIZATION'} CA\n";
1683 print OPENSSL "$cgiparams{'ROOTCERT_EMAIL'}\n";
1684 close (OPENSSL);
1685 if ($?) {
1686 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1687 unlink ("${General::swroot}/ovpn/ca/cakey.pem");
1688 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1689 goto ROOTCERT_ERROR;
1690 }
1691 } else { # child
1692 unless (exec ('/usr/bin/openssl', 'req', '-x509', '-nodes', '-rand', '/proc/interrupts:/proc/net/rt_cache',
1693 '-days', '999999', '-newkey', 'rsa:2048',
1694 '-keyout', "${General::swroot}/ovpn/ca/cakey.pem",
1695 '-out', "${General::swroot}/ovpn/ca/cacert.pem",
1696 '-config',"${General::swroot}/ovpn/openssl/ovpn.cnf")) {
1697 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1698 goto ROOTCERT_ERROR;
1699 }
1700 }
1701
1702 # Create the Host certificate request
1703 $pid = open(OPENSSL, "|-");
1704 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
1705 if ($pid) { # parent
1706 print OPENSSL "$cgiparams{'ROOTCERT_COUNTRY'}\n";
1707 print OPENSSL "$state\n";
1708 print OPENSSL "$city\n";
1709 print OPENSSL "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
1710 print OPENSSL "$ou\n";
1711 print OPENSSL "$cgiparams{'ROOTCERT_HOSTNAME'}\n";
1712 print OPENSSL "$cgiparams{'ROOTCERT_EMAIL'}\n";
1713 print OPENSSL ".\n";
1714 print OPENSSL ".\n";
1715 close (OPENSSL);
1716 if ($?) {
1717 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1718 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1719 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
1720 goto ROOTCERT_ERROR;
1721 }
1722 } else { # child
1723 unless (exec ('/usr/bin/openssl', 'req', '-nodes', '-rand', '/proc/interrupts:/proc/net/rt_cache',
1724 '-newkey', 'rsa:1024',
1725 '-keyout', "${General::swroot}/ovpn/certs/serverkey.pem",
1726 '-out', "${General::swroot}/ovpn/certs/serverreq.pem",
1727 '-extensions', 'server',
1728 '-config', "${General::swroot}/ovpn/openssl/ovpn.cnf" )) {
1729 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
1730 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1731 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
1732 unlink ("${General::swroot}/ovpn/ca/cakey.pem");
1733 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1734 goto ROOTCERT_ERROR;
1735 }
1736 }
1737
1738 # Sign the host certificate request
1739 system('/usr/bin/openssl', 'ca', '-days', '999999',
1740 '-batch', '-notext',
1741 '-in', "${General::swroot}/ovpn/certs/serverreq.pem",
1742 '-out', "${General::swroot}/ovpn/certs/servercert.pem",
1743 '-extensions', 'server',
1744 '-config', "${General::swroot}/ovpn/openssl/ovpn.cnf");
1745 if ($?) {
1746 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1747 unlink ("${General::swroot}/ovpn/ca/cakey.pem");
1748 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1749 unlink ("${General::swroot}/ovpn/serverkey.pem");
1750 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
1751 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1752 &newcleanssldatabase();
1753 goto ROOTCERT_ERROR;
1754 } else {
1755 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
1756 &deletebackupcert();
1757 }
1758
1759 # Create an empty CRL
1760 system('/usr/bin/openssl', 'ca', '-gencrl',
1761 '-out', "${General::swroot}/ovpn/crls/cacrl.pem",
1762 '-config', "${General::swroot}/ovpn/openssl/ovpn.cnf" );
1763 if ($?) {
1764 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1765 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1766 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1767 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1768 unlink ("${General::swroot}/ovpn/crls/cacrl.pem");
1769 &cleanssldatabase();
1770 goto ROOTCERT_ERROR;
1771 # } else {
1772 # &cleanssldatabase();
1773 }
1774 # Create Diffie Hellmann Parameter
1775 system('/usr/bin/openssl', 'dhparam', '-rand', '/proc/interrupts:/proc/net/rt_cache',
1776 '-out', "${General::swroot}/ovpn/ca/dh1024.pem",
1777 '1024' );
1778 if ($?) {
1779 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1780 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
1781 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
1782 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
1783 unlink ("${General::swroot}/ovpn/crls/cacrl.pem");
1784 unlink ("${General::swroot}/ovpn/ca/dh1024.pem");
1785 &cleanssldatabase();
1786 goto ROOTCERT_ERROR;
1787 # } else {
1788 # &cleanssldatabase();
1789 }
1790 goto ROOTCERT_SUCCESS;
1791 }
1792 ROOTCERT_ERROR:
1793 if ($cgiparams{'ACTION'} ne '') {
1794 &Header::showhttpheaders();
1795 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1796 &Header::openbigbox('100%', 'LEFT', '', '');
1797 if ($errormessage) {
1798 &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
1799 print "<class name='base'>$errormessage";
1800 print "&nbsp;</class>";
1801 &Header::closebox();
1802 }
1803 &Header::openbox('100%', 'LEFT', "$Lang::tr{'generate root/host certificates'}:");
1804 print <<END
1805 <form method='post' enctype='multipart/form-data'>
1806 <table width='100%' border='0' cellspacing='1' cellpadding='0'>
1807 <tr><td width='30%' class='base'>$Lang::tr{'organization name'}:</td>
1808 <td width='35%' class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_ORGANIZATION' value='$cgiparams{'ROOTCERT_ORGANIZATION'}' size='32' /></td>
1809 <td width='35%' colspan='2'>&nbsp;</td></tr>
1810 <tr><td class='base'>$Lang::tr{'ipfires hostname'}:</td>
1811 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_HOSTNAME' value='$cgiparams{'ROOTCERT_HOSTNAME'}' size='32' /></td>
1812 <td colspan='2'>&nbsp;</td></tr>
1813 <tr><td class='base'>$Lang::tr{'your e-mail'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
1814 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_EMAIL' value='$cgiparams{'ROOTCERT_EMAIL'}' size='32' /></td>
1815 <td colspan='2'>&nbsp;</td></tr>
1816 <tr><td class='base'>$Lang::tr{'your department'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
1817 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_OU' value='$cgiparams{'ROOTCERT_OU'}' size='32' /></td>
1818 <td colspan='2'>&nbsp;</td></tr>
1819 <tr><td class='base'>$Lang::tr{'city'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
1820 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_CITY' value='$cgiparams{'ROOTCERT_CITY'}' size='32' /></td>
1821 <td colspan='2'>&nbsp;</td></tr>
1822 <tr><td class='base'>$Lang::tr{'state or province'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
1823 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_STATE' value='$cgiparams{'ROOTCERT_STATE'}' size='32' /></td>
1824 <td colspan='2'>&nbsp;</td></tr>
1825 <tr><td class='base'>$Lang::tr{'country'}:</td>
1826 <td class='base'><select name='ROOTCERT_COUNTRY'>
1827
1828 END
1829 ;
1830 foreach my $country (sort keys %{Countries::countries}) {
1831 print "<option value='$Countries::countries{$country}'";
1832 if ( $Countries::countries{$country} eq $cgiparams{'ROOTCERT_COUNTRY'} ) {
1833 print " selected='selected'";
1834 }
1835 print ">$country</option>";
1836 }
1837 print <<END
1838 </select></td>
1839 <td colspan='2'>&nbsp;</td></tr>
1840 <tr><td>&nbsp;</td>
1841 <td><input type='submit' name='ACTION' value='$Lang::tr{'generate root/host certificates'}' /></td>
1842 <td>&nbsp;</td><td>&nbsp;</td></tr>
1843 <tr><td class='base' colspan='4' align='left'>
1844 <img src='/blob.gif' valign='top' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td></tr>
1845 <tr><td class='base' colspan='4' align='left'>
1846 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>:
1847 $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'}
1848 </td></tr>
1849 <tr><td colspan='4' bgcolor='#000000'><img src='/images/null.gif' width='1' height='1' border='0' /></td></tr>
1850 <tr><td class='base' nowrap='nowrap'>$Lang::tr{'upload p12 file'}:</td>
1851 <td nowrap='nowrap'><input type='file' name='FH' size='32'></td>
1852 <td colspan='2'>&nbsp;</td></tr>
1853 <tr><td class='base'>$Lang::tr{'pkcs12 file password'}:&nbsp;<img src='/blob.gif' alt='*' ></td>
1854 <td class='base' nowrap='nowrap'><input type='password' name='P12_PASS' value='$cgiparams{'P12_PASS'}' size='32' /></td>
1855 <td colspan='2'>&nbsp;</td></tr>
1856 <tr><td>&nbsp;</td>
1857 <td><input type='submit' name='ACTION' value='$Lang::tr{'upload p12 file'}' /></td>
1858 <td colspan='2'>&nbsp;</td></tr>
1859 <tr><td class='base' colspan='4' align='left'>
1860 <img src='/blob.gif' valign='top' al='*' >&nbsp;$Lang::tr{'this field may be blank'}</td></tr>
1861 </form></table>
1862 END
1863 ;
1864 &Header::closebox();
1865
1866 &Header::closebigbox();
1867 &Header::closepage();
1868 exit(0)
1869 }
1870
1871 ROOTCERT_SUCCESS:
1872 system ("chmod 600 ${General::swroot}/ovpn/certs/serverkey.pem");
1873 # if ($vpnsettings{'ENABLED'} eq 'on' ||
1874 # $vpnsettings{'ENABLE_BLUE'} eq 'on') {
1875 # system('/usr/local/bin/ipsecctrl', 'S');
1876 # }
1877
1878 ###
1879 ### Enable/Disable connection
1880 ###
1881
1882 ###
1883 # m.a.d net2net
1884 ###
1885
1886 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
1887
1888 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1889 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1890 # my $n2nactive = '';
1891 my $n2nactive = `/bin/ps ax|grep $confighash{$cgiparams{'KEY'}}[1]|grep -v grep|awk \'{print \$1}\'`;
1892
1893 if ($confighash{$cgiparams{'KEY'}}) {
1894 if ($confighash{$cgiparams{'KEY'}}[0] eq 'off') {
1895 $confighash{$cgiparams{'KEY'}}[0] = 'on';
1896 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1897
1898 if ($confighash{$cgiparams{'KEY'}}[3] eq 'net'){
1899 system('/usr/local/bin/openvpnctrl', '-sn2n', $confighash{$cgiparams{'KEY'}}[1]);
1900 }
1901 } else {
1902
1903 $confighash{$cgiparams{'KEY'}}[0] = 'off';
1904 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1905
1906 if ($confighash{$cgiparams{'KEY'}}[3] eq 'net'){
1907 if ($n2nactive ne ''){
1908 system('/usr/local/bin/openvpnctrl', '-kn2n', $confighash{$cgiparams{'KEY'}}[1]);
1909 }
1910
1911 } else {
1912 $errormessage = $Lang::tr{'invalid key'};
1913 }
1914 }
1915 }
1916
1917 ###
1918 ### Download OpenVPN client package
1919 ###
1920
1921
1922 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'dl client arch'}) {
1923 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1924 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1925 my $file = '';
1926 my $clientovpn = '';
1927 my @fileholder;
1928 my $tempdir = tempdir( CLEANUP => 1 );
1929 my $zippath = "$tempdir/";
1930
1931 ###
1932 # m.a.d net2net
1933 ###
1934
1935 if ($confighash{$cgiparams{'KEY'}}[3] eq 'net'){
1936
1937 my $zipname = "$confighash{$cgiparams{'KEY'}}[1]-Client.zip";
1938 my $zippathname = "$zippath$zipname";
1939 $clientovpn = "$confighash{$cgiparams{'KEY'}}[1].conf";
1940 my @ovsubnettemp = split(/\./,$confighash{$cgiparams{'KEY'}}[27]);
1941 my $ovsubnet = "$ovsubnettemp[0].$ovsubnettemp[1].$ovsubnettemp[2]";
1942 my $tunmtu = '';
1943 my @remsubnet = split(/\//,$confighash{$cgiparams{'KEY'}}[8]);
1944 my $n2nfragment = '';
1945
1946 open(CLIENTCONF, ">$tempdir/$clientovpn") or die "Unable to open tempfile: $!";
1947 flock CLIENTCONF, 2;
1948
1949 my $zip = Archive::Zip->new();
1950 print CLIENTCONF "# IPFire n2n Open VPN Client Config by ummeegge und m.a.d\n";
1951 print CLIENTCONF "# \n";
1952 print CLIENTCONF "# User Security\n";
1953 print CLIENTCONF "user nobody\n";
1954 print CLIENTCONF "group nobody\n";
1955 print CLIENTCONF "persist-tun\n";
1956 print CLIENTCONF "persist-key\n";
1957 print CLIENTCONF "script-security 2\n";
1958 print CLIENTCONF "# IP/DNS for remote Server Gateway\n";
1959 print CLIENTCONF "remote $vpnsettings{'VPN_IP'}\n";
1960 print CLIENTCONF "float\n";
1961 print CLIENTCONF "# IP adresses of the VPN Subnet\n";
1962 print CLIENTCONF "ifconfig $ovsubnet.2 $ovsubnet.1\n";
1963 print CLIENTCONF "# Server Gateway Network\n";
1964 print CLIENTCONF "route $remsubnet[0] $remsubnet[1]\n";
1965 print CLIENTCONF "# tun Device\n";
1966 print CLIENTCONF "dev $vpnsettings{'DDEVICE'}\n";
1967 print CLIENTCONF "# Port and Protokoll\n";
1968 print CLIENTCONF "port $confighash{$cgiparams{'KEY'}}[29]\n";
1969
1970 if ($confighash{$cgiparams{'KEY'}}[28] eq 'tcp') {
1971 print CLIENTCONF "proto tcp-client\n";
1972 print CLIENTCONF "# Packet size\n";
1973 if ($confighash{$cgiparams{'KEY'}}[31] eq '') {$tunmtu = '1400'} else {$tunmtu = $confighash{$cgiparams{'KEY'}}[31]};
1974 print CLIENTCONF "tun-mtu $tunmtu\n";
1975 }
1976
1977 if ($confighash{$cgiparams{'KEY'}}[28] eq 'udp') {
1978 print CLIENTCONF "proto udp\n";
1979 print CLIENTCONF "# Paketsize\n";
1980 if ($confighash{$cgiparams{'KEY'}}[31] eq '') {$tunmtu = '1500'} else {$tunmtu = $confighash{$cgiparams{'KEY'}}[31]};
1981 print CLIENTCONF "tun-mtu $tunmtu\n";
1982 if ($confighash{$cgiparams{'KEY'}}[24] ne '') {print CLIENTCONF "fragment $confighash{$cgiparams{'KEY'}}[24]\n";}
1983 if ($confighash{$cgiparams{'KEY'}}[23] eq 'on') {print CLIENTCONF "mssfix\n";}
1984 }
1985 print CLIENTCONF "ns-cert-type server\n";
1986 print CLIENTCONF "# Auth. Client\n";
1987 print CLIENTCONF "tls-client\n";
1988 print CLIENTCONF "# Cipher\n";
1989 print CLIENTCONF "cipher AES-256-CBC\n";
1990 if ($confighash{$cgiparams{'KEY'}}[4] eq 'cert' && -f "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12") {
1991 print CLIENTCONF "pkcs12 ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12\r\n";
1992 $zip->addFile( "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12", "$confighash{$cgiparams{'KEY'}}[1].p12") or die "Can't add file $confighash{$cgiparams{'KEY'}}[1].p12\n";
1993 }
1994 if ($confighash{$cgiparams{'KEY'}}[30] eq 'on') {
1995 print CLIENTCONF "# Enable Compression\n";
1996 print CLIENTCONF "comp-lzo\r\n";
1997 }
1998 print CLIENTCONF "# Debug Level\n";
1999 print CLIENTCONF "verb 3\n";
2000 print CLIENTCONF "# Tunnel check\n";
2001 print CLIENTCONF "keepalive 10 60\n";
2002 print CLIENTCONF "# Start as daemon\n";
2003 print CLIENTCONF "daemon $confighash{$cgiparams{'KEY'}}[1]n2n\n";
2004 print CLIENTCONF "writepid /var/run/$confighash{$cgiparams{'KEY'}}[1]n2n.pid\n";
2005 print CLIENTCONF "# Activate Management Interface and Port\n";
2006 if ($confighash{$cgiparams{'KEY'}}[22] eq '') {print CLIENTCONF "management localhost $confighash{$cgiparams{'KEY'}}[29]\n"}
2007 else {print CLIENTCONF "management localhost $confighash{$cgiparams{'KEY'}}[22]\n"};
2008 print CLIENTCONF "# remsub $confighash{$cgiparams{'KEY'}}[11]\n";
2009
2010
2011 close(CLIENTCONF);
2012
2013 $zip->addFile( "$tempdir/$clientovpn", $clientovpn) or die "Can't add file $clientovpn\n";
2014 my $status = $zip->writeToFileNamed($zippathname);
2015
2016 open(DLFILE, "<$zippathname") or die "Unable to open $zippathname: $!";
2017 @fileholder = <DLFILE>;
2018 print "Content-Type:application/x-download\n";
2019 print "Content-Disposition:attachment;filename=$zipname\n\n";
2020 print @fileholder;
2021 exit (0);
2022 }
2023 else
2024 {
2025 my $zipname = "$confighash{$cgiparams{'KEY'}}[1]-TO-IPFire.zip";
2026 my $zippathname = "$zippath$zipname";
2027 $clientovpn = "$confighash{$cgiparams{'KEY'}}[1]-TO-IPFire.ovpn";
2028
2029 ###
2030 # m.a.d net2net
2031 ###
2032
2033 open(CLIENTCONF, ">$tempdir/$clientovpn") or die "Unable to open tempfile: $!";
2034 flock CLIENTCONF, 2;
2035
2036 my $zip = Archive::Zip->new();
2037
2038 print CLIENTCONF "#OpenVPN Client conf\r\n";
2039 print CLIENTCONF "tls-client\r\n";
2040 print CLIENTCONF "client\r\n";
2041 print CLIENTCONF "nobind\n";
2042 print CLIENTCONF "dev $vpnsettings{'DDEVICE'}\r\n";
2043 print CLIENTCONF "proto $vpnsettings{'DPROTOCOL'}\r\n";
2044 print CLIENTCONF "$vpnsettings{'DDEVICE'}-mtu $vpnsettings{'DMTU'}\r\n";
2045 if ( $vpnsettings{'ENABLED'} eq 'on'){
2046 print CLIENTCONF "remote $vpnsettings{'VPN_IP'} $vpnsettings{'DDEST_PORT'}\r\n";
2047 if ( $vpnsettings{'ENABLED_BLUE'} eq 'on' && (&haveBlueNet())){
2048 print CLIENTCONF "#Coment the above line and uncoment the next line, if you want to connect on the Blue interface\r\n";
2049 print CLIENTCONF ";remote $netsettings{'BLUE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2050 }
2051 if ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' && (&haveOrangeNet())){
2052 print CLIENTCONF "#Coment the above line and uncoment the next line, if you want to connect on the Orange interface\r\n";
2053 print CLIENTCONF ";remote $netsettings{'ORANGE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2054 }
2055 } elsif ( $vpnsettings{'ENABLED_BLUE'} eq 'on' && (&haveBlueNet())){
2056 print CLIENTCONF "remote $netsettings{'BLUE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2057 if ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' && (&haveOrangeNet())){
2058 print CLIENTCONF "#Coment the above line and uncoment the next line, if you want to connect on the Orange interface\r\n";
2059 print CLIENTCONF ";remote $netsettings{'ORANGE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2060 }
2061 } elsif ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' && (&haveOrangeNet())){
2062 print CLIENTCONF "remote $netsettings{'ORANGE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
2063 }
2064
2065 if ($confighash{$cgiparams{'KEY'}}[4] eq 'cert' && -f "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12") {
2066 print CLIENTCONF "pkcs12 $confighash{$cgiparams{'KEY'}}[1].p12\r\n";
2067 $zip->addFile( "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12", "$confighash{$cgiparams{'KEY'}}[1].p12") or die "Can't add file $confighash{$cgiparams{'KEY'}}[1].p12\n";
2068 } else {
2069 print CLIENTCONF "ca cacert.pem\r\n";
2070 print CLIENTCONF "cert $confighash{$cgiparams{'KEY'}}[1]cert.pem\r\n";
2071 print CLIENTCONF "key $confighash{$cgiparams{'KEY'}}[1].key\r\n";
2072 $zip->addFile( "${General::swroot}/ovpn/ca/cacert.pem", "cacert.pem") or die "Can't add file cacert.pem\n";
2073 $zip->addFile( "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem", "$confighash{$cgiparams{'KEY'}}[1]cert.pem") or die "Can't add file $confighash{$cgiparams{'KEY'}}[1]cert.pem\n";
2074 }
2075 print CLIENTCONF "cipher $vpnsettings{DCIPHER}\r\n";
2076 if ($vpnsettings{DCOMPLZO} eq 'on') {
2077 print CLIENTCONF "comp-lzo\r\n";
2078 }
2079 print CLIENTCONF "verb 3\r\n";
2080 print CLIENTCONF "ns-cert-type server\r\n";
2081 print CLIENTCONF "tls-remote $vpnsettings{ROOTCERT_HOSTNAME}\r\n";
2082 if ($vpnsettings{MSSFIX} eq 'on') {
2083 print CLIENTCONF "mssfix\r\n";
2084 }
2085 if ($vpnsettings{FRAGMENT} ne '' && $vpnsettings{DPROTOCOL} ne 'tcp' ) {
2086 print CLIENTCONF "fragment $vpnsettings{'FRAGMENT'}\r\n";
2087 }
2088 close(CLIENTCONF);
2089
2090 $zip->addFile( "$tempdir/$clientovpn", $clientovpn) or die "Can't add file $clientovpn\n";
2091 my $status = $zip->writeToFileNamed($zippathname);
2092
2093 open(DLFILE, "<$zippathname") or die "Unable to open $zippathname: $!";
2094 @fileholder = <DLFILE>;
2095 print "Content-Type:application/x-download\n";
2096 print "Content-Disposition:attachment;filename=$zipname\n\n";
2097 print @fileholder;
2098 exit (0);
2099 }
2100
2101
2102
2103 ###
2104 ### Remove connection
2105 ###
2106
2107
2108 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove'}) {
2109 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
2110 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2111
2112 if ($confighash{$cgiparams{'KEY'}}) {
2113 # if ($vpnsettings{'ENABLED'} eq 'on' ||
2114 # $vpnsettings{'ENABLED_BLUE'} eq 'on') {
2115 # system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'});
2116 # }
2117 #
2118 my $temp = `/usr/bin/openssl ca -revoke ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem -config ${General::swroot}/ovpn/openssl/ovpn.cnf`;
2119
2120 ###
2121 # m.a.d net2net
2122 ###
2123
2124 if ($confighash{$cgiparams{'KEY'}}[3] eq 'net') {
2125
2126 my $conffile = glob("${General::swroot}/ovpn/n2nconf/$confighash{$cgiparams{'KEY'}}[1]/$confighash{$cgiparams{'KEY'}}[1].conf");
2127 my $certfile = glob("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12");
2128 unlink ($certfile) or die "Removing $certfile fail: $!";
2129 unlink ($conffile) or die "Removing $conffile fail: $!";
2130 rmdir ("${General::swroot}/ovpn/n2nconf/$confighash{$cgiparams{'KEY'}}[1]") || die "Kann Verzeichnis nicht loeschen: $!";
2131
2132 }
2133
2134 unlink ("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem");
2135 unlink ("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12");
2136
2137 # A.Marx CCD delete ccd files and routes
2138
2139
2140 if (-f "${General::swroot}/ovpn/ccd/$confighash{$cgiparams{'KEY'}}[2]")
2141 {
2142 unlink "${General::swroot}/ovpn/ccd/$confighash{$cgiparams{'KEY'}}[2]";
2143 }
2144 my %ccdroutehash=();
2145 &General::readhasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
2146 foreach my $key (keys %ccdroutehash) {
2147 if ($ccdroutehash{$key}[0] eq $confighash{$cgiparams{'KEY'}}[1]){
2148 delete $ccdroutehash{$key};
2149 }
2150 }
2151 &General::writehasharray("${General::swroot}/ovpn/ccdroute", \%ccdroutehash);
2152 my %ccdroute2hash=();
2153 &General::readhasharray("${General::swroot}/ovpn/ccdroute2", \%ccdroute2hash);
2154 foreach my $key (keys %ccdroute2hash) {
2155 if ($ccdroute2hash{$key}[0] eq $confighash{$cgiparams{'KEY'}}[1]){
2156 delete $ccdroute2hash{$key};
2157 }
2158 }
2159 &General::writehasharray("${General::swroot}/ovpn/ccdroute2", \%ccdroute2hash);
2160
2161
2162
2163 # CCD end
2164
2165
2166 delete $confighash{$cgiparams{'KEY'}};
2167 my $temp2 = `/usr/bin/openssl ca -gencrl -out ${General::swroot}/ovpn/crls/cacrl.pem -config ${General::swroot}/ovpn/openssl/ovpn.cnf`;
2168 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2169
2170 #&writeserverconf();
2171 } else {
2172 $errormessage = $Lang::tr{'invalid key'};
2173 }
2174
2175
2176 ###
2177 ### Download PKCS12 file
2178 ###
2179 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download pkcs12 file'}) {
2180 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2181
2182 print "Content-Disposition: filename=" . $confighash{$cgiparams{'KEY'}}[1] . ".p12\r\n";
2183 print "Content-Type: application/octet-stream\r\n\r\n";
2184 print `/bin/cat ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12`;
2185 exit (0);
2186
2187 ###
2188 ### Display certificate
2189 ###
2190 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show certificate'}) {
2191 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2192
2193 if ( -f "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem") {
2194 &Header::showhttpheaders();
2195 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
2196 &Header::openbigbox('100%', 'LEFT', '', '');
2197 &Header::openbox('100%', 'LEFT', "$Lang::tr{'certificate'}:");
2198 my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
2199 $output = &Header::cleanhtml($output,"y");
2200 print "<pre>$output</pre>\n";
2201 &Header::closebox();
2202 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2203 &Header::closebigbox();
2204 &Header::closepage();
2205 exit(0);
2206 }
2207 ###
2208 ### Display Certificate Revoke List
2209 ###
2210 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show crl'}) {
2211 # &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2212
2213 if ( -f "${General::swroot}/ovpn/crls/cacrl.pem") {
2214 &Header::showhttpheaders();
2215 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
2216 &Header::openbigbox('100%', 'LEFT', '', '');
2217 &Header::openbox('100%', 'LEFT', "$Lang::tr{'crl'}:");
2218 my $output = `/usr/bin/openssl crl -text -noout -in ${General::swroot}/ovpn/crls/cacrl.pem`;
2219 $output = &Header::cleanhtml($output,"y");
2220 print "<pre>$output</pre>\n";
2221 &Header::closebox();
2222 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2223 &Header::closebigbox();
2224 &Header::closepage();
2225 exit(0);
2226 }
2227
2228 ###
2229 ### Advanced Server Settings
2230 ###
2231
2232 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'advanced server'}) {
2233 %cgiparams = ();
2234 %cahash = ();
2235 %confighash = ();
2236 my $disabled;
2237 &General::readhash("${General::swroot}/ovpn/settings", \%cgiparams);
2238 read_routepushfile;
2239
2240
2241 # if ($cgiparams{'CLIENT2CLIENT'} eq '') {
2242 # $cgiparams{'CLIENT2CLIENT'} = 'on';
2243 # }
2244 ADV_ERROR:
2245 if ($cgiparams{'MAX_CLIENTS'} eq '') {
2246 $cgiparams{'MAX_CLIENTS'} = '100';
2247 }
2248 if ($cgiparams{'KEEPALIVE_1'} eq '') {
2249 $cgiparams{'KEEPALIVE_1'} = '10';
2250 }
2251 if ($cgiparams{'KEEPALIVE_2'} eq '') {
2252 $cgiparams{'KEEPALIVE_2'} = '60';
2253 }
2254 if ($cgiparams{'LOG_VERB'} eq '') {
2255 $cgiparams{'LOG_VERB'} = '3';
2256 }
2257 $checked{'CLIENT2CLIENT'}{'off'} = '';
2258 $checked{'CLIENT2CLIENT'}{'on'} = '';
2259 $checked{'CLIENT2CLIENT'}{$cgiparams{'CLIENT2CLIENT'}} = 'CHECKED';
2260 $checked{'REDIRECT_GW_DEF1'}{'off'} = '';
2261 $checked{'REDIRECT_GW_DEF1'}{'on'} = '';
2262 $checked{'REDIRECT_GW_DEF1'}{$cgiparams{'REDIRECT_GW_DEF1'}} = 'CHECKED';
2263 $selected{'ENGINES'}{$cgiparams{'ENGINES'}} = 'SELECTED';
2264 $checked{'MSSFIX'}{'off'} = '';
2265 $checked{'MSSFIX'}{'on'} = '';
2266 $checked{'MSSFIX'}{$cgiparams{'MSSFIX'}} = 'CHECKED';
2267 $selected{'LOG_VERB'}{'1'} = '';
2268 $selected{'LOG_VERB'}{'2'} = '';
2269 $selected{'LOG_VERB'}{'3'} = '';
2270 $selected{'LOG_VERB'}{'4'} = '';
2271 $selected{'LOG_VERB'}{'5'} = '';
2272 $selected{'LOG_VERB'}{'6'} = '';
2273 $selected{'LOG_VERB'}{'7'} = '';
2274 $selected{'LOG_VERB'}{'8'} = '';
2275 $selected{'LOG_VERB'}{'9'} = '';
2276 $selected{'LOG_VERB'}{'10'} = '';
2277 $selected{'LOG_VERB'}{'11'} = '';
2278 $selected{'LOG_VERB'}{'0'} = '';
2279 $selected{'LOG_VERB'}{$cgiparams{'LOG_VERB'}} = 'SELECTED';
2280
2281 &Header::showhttpheaders();
2282 &Header::openpage($Lang::tr{'status ovpn'}, 1, '');
2283 &Header::openbigbox('100%', 'LEFT', '', $errormessage);
2284 if ($errormessage) {
2285 &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
2286 print "<class name='base'>$errormessage\n";
2287 print "&nbsp;</class>\n";
2288 &Header::closebox();
2289 }
2290 &Header::openbox('100%', 'LEFT', $Lang::tr{'advanced server'});
2291 print <<END
2292 <form method='post' enctype='multipart/form-data' disabled>
2293 <table width='100%' border=0>
2294 <tr>
2295 <td colspan='4'><b>$Lang::tr{'dhcp-options'}</b></td>
2296 </tr>
2297 <tr>
2298 <td width='25%'></td> <td width='20%'> </td><td width='25%'> </td><td width='30%'></td>
2299 </tr>
2300 <tr>
2301 <td class='base'>Domain</td>
2302 <td><input type='TEXT' name='DHCP_DOMAIN' value='$cgiparams{'DHCP_DOMAIN'}' size='30' /></td>
2303 </tr>
2304 <tr>
2305 <td class='base'>DNS</td>
2306 <td><input type='TEXT' name='DHCP_DNS' value='$cgiparams{'DHCP_DNS'}' size='30' /></td>
2307 </tr>
2308 <tr>
2309 <td class='base'>WINS</td>
2310 <td><input type='TEXT' name='DHCP_WINS' value='$cgiparams{'DHCP_WINS'}' size='30' /></td>
2311 </tr>
2312 <tr>
2313 <td colspan='4'><b>$Lang::tr{'ovpn routes push options'}</b></td>
2314 </tr>
2315 <tr>
2316 <td class='base'>$Lang::tr{'ovpn routes push'}</td>
2317 <td colspan='2'>
2318 <textarea name='ROUTES_PUSH' cols='26' rows='6' wrap='off'>
2319 END
2320 ;
2321
2322 if ($cgiparams{'ROUTES_PUSH'} ne '')
2323 {
2324 print $cgiparams{'ROUTES_PUSH'};
2325 }
2326
2327 print <<END;
2328 </textarea></td>
2329 </tr>
2330 </tr>
2331 </table>
2332 <hr size='1'>
2333 <table width='100%'>
2334 <tr>
2335 <td class'base'><b>$Lang::tr{'misc-options'}</b></td>
2336 </tr>
2337 <tr>
2338 <td width='20%'></td> <td width='15%'> </td><td width='15%'> </td><td width='50%'></td>
2339 </tr>
2340 <tr>
2341 <td class='base'>Client-To-Client</td>
2342 <td><input type='checkbox' name='CLIENT2CLIENT' $checked{'CLIENT2CLIENT'}{'on'} /></td>
2343 </tr>
2344 <tr>
2345 <td class='base'>Redirect-Gateway def1</td>
2346 <td><input type='checkbox' name='REDIRECT_GW_DEF1' $checked{'REDIRECT_GW_DEF1'}{'on'} /></td>
2347 </tr>
2348 <tr>
2349 <td class='base'>Max-Clients</td>
2350 <td><input type='text' name='MAX_CLIENTS' value='$cgiparams{'MAX_CLIENTS'}' size='10' /></td>
2351 </tr>
2352 <tr>
2353 <td class='base'>Keppalive <br />
2354 (ping/ping-restart)</td>
2355 <td><input type='TEXT' name='KEEPALIVE_1' value='$cgiparams{'KEEPALIVE_1'}' size='10' /></td>
2356 <td><input type='TEXT' name='KEEPALIVE_2' value='$cgiparams{'KEEPALIVE_2'}' size='10' /></td>
2357 </tr>
2358 <tr>
2359 <td class='base'>fragment <br></td>
2360 <td><input type='TEXT' name='FRAGMENT' value='$cgiparams{'FRAGMENT'}' size='10' /></td>
2361 <td>Default: <span class="base">1300</span></td>
2362 </tr>
2363 <tr>
2364 <td class='base'>mssfix</td>
2365 <td><input type='checkbox' name='MSSFIX' $checked{'MSSFIX'}{'on'} /></td>
2366 <td>Default: on</td>
2367 </tr>
2368 </table>
2369
2370 <!--
2371 <hr size='1'>
2372 <table width='100%'>
2373 <tr>
2374 <td class'base'><b>Crypto-Engines</b></td>
2375 </tr>
2376 <tr>
2377 <td width='15%'></td> <td width='30%'> </td><td width='25%'> </td><td width='30%'></td>
2378 </tr>
2379 <tr><td class='base'>Engines:</td>
2380 <td><select name='ENGINES'><option value="none" $selected{'ENGINES'}{'none'}>none</option>
2381 <option value="cryptodev" $selected{'ENGINES'}{'cryptodev'}>cryptodev</option>
2382 <option value="padlock" $selected{'ENGINES'}{'padlock'}>padlock</option>
2383 </select>
2384 </td>
2385 </table>
2386 -->
2387 <hr size='1'>
2388 <table width='100%'>
2389 <tr>
2390 <td class'base'><b>$Lang::tr{'log-options'}</b></td>
2391 </tr>
2392 <tr>
2393 <td width='15%'></td> <td width='30%'> </td><td width='25%'> </td><td width='30%'></td>
2394 </tr>
2395
2396 <tr><td class='base'>VERB</td>
2397 <td><select name='LOG_VERB'><option value='1' $selected{'LOG_VERB'}{'1'}>1</option>
2398 <option value='2' $selected{'LOG_VERB'}{'2'}>2</option>
2399 <option value='3' $selected{'LOG_VERB'}{'3'}>3</option>
2400 <option value='4' $selected{'LOG_VERB'}{'4'}>4</option>
2401 <option value='5' $selected{'LOG_VERB'}{'5'}>5</option>
2402 <option value='6' $selected{'LOG_VERB'}{'6'}>6</option>
2403 <option value='7' $selected{'LOG_VERB'}{'7'}>7</option>
2404 <option value='8' $selected{'LOG_VERB'}{'8'}>8</option>
2405 <option value='9' $selected{'LOG_VERB'}{'9'}>9</option>
2406 <option value='10' $selected{'LOG_VERB'}{'10'}>10</option>
2407 <option value='11' $selected{'LOG_VERB'}{'11'}>11</option>
2408 <option value='0' $selected{'LOG_VERB'}{'0'}>0</option></select></td>
2409 </table><hr>
2410 END
2411
2412 if ( -e "/var/run/openvpn.pid"){
2413 print" <br><b><font color='#990000'>$Lang::tr{'attention'}:</b></font><br>
2414 $Lang::tr{'server restart'}<br><br>
2415 <hr>";
2416 }
2417
2418 print<<END
2419 <table width='100%'>
2420 <tr>
2421 <td>&nbsp;</td>
2422 <td allign='center'><input type='submit' name='ACTION' value='$Lang::tr{'save-adv-options'}' /></td>
2423 <td allign='center'><input type='submit' name='ACTION' value='$Lang::tr{'cancel-adv-options'}' /></td>
2424 <td>&nbsp;</td>
2425 </tr>
2426 </table>
2427 </form>
2428 END
2429 ;
2430
2431 &Header::closebox();
2432 # print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
2433 &Header::closebigbox();
2434 &Header::closepage();
2435 exit(0);
2436
2437
2438 # A.Marx CCD Add,delete or edit CCD net
2439
2440 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'ccd net'} ||
2441 $cgiparams{'ACTION'} eq $Lang::tr{'ccd add'} ||
2442 $cgiparams{'ACTION'} eq "kill" ||
2443 $cgiparams{'ACTION'} eq "edit" ||
2444 $cgiparams{'ACTION'} eq 'editsave'){
2445 &Header::showhttpheaders();
2446 &Header::openpage($Lang::tr{'ccd net'}, 1, '');
2447 &Header::openbigbox('100%', 'LEFT', '', '');
2448
2449 if ($cgiparams{'ACTION'} eq "kill"){
2450 &delccdnet($cgiparams{'net'});
2451 }
2452
2453 if ($cgiparams{'ACTION'} eq 'editsave'){
2454 my ($a,$b) =split (/\|/,$cgiparams{'ccdname'});
2455 if ( $a ne $b){ &modccdnet($a,$b);}
2456 }
2457
2458 if ($cgiparams{'ACTION'} eq $Lang::tr{'ccd add'}) {
2459 &addccdnet($cgiparams{'ccdname'},$cgiparams{'ccdsubnet'},$cgiparams{'DOVPN_SUBNET'});
2460 }
2461 if ($errormessage) {
2462 &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
2463 print "<class name='base'>$errormessage";
2464 print "&nbsp;</class>";
2465 &Header::closebox();
2466 }
2467 if ($cgiparams{'ACTION'} eq "edit"){
2468
2469 &Header::openbox('100%', 'LEFT', $Lang::tr{'ccd modify'});
2470
2471 print <<END
2472 <table width='100%' border=0>
2473 <tr><form method='post'>
2474 <td width='10%' nowrap='nowrap'>$Lang::tr{'ccd name'}:</td><td><input type='TEXT' name='ccdname' value='$cgiparams{'ccdname'}' /></td>
2475 <td width='8%'>$Lang::tr{'ccd subnet'}:</td><td><input type='TEXT' name='ccdsubnet' value='$cgiparams{'ccdsubnet'}' readonly /></td></tr>
2476 <tr><td colspan='4' align='right'><hr><input type='submit' value='$Lang::tr{'save'}' /><input type='hidden' name='ACTION' value='editsave'/>
2477 <input type='hidden' name='ccdname' value='$cgiparams{'ccdname'}'/><input type='submit' value='$Lang::tr{'cancel'}' />
2478 </td></tr>
2479 </table></form>
2480 END
2481 ;
2482 &Header::closebox();
2483
2484 &Header::openbox('100%', 'LEFT',$Lang::tr{'ccd net'} );
2485 print <<END
2486 <table width='100%' border='0' cellpadding='0' cellspacing='1'>
2487 <tr>
2488 <td class='boldbase' align='center'><b>$Lang::tr{'ccd name'}</td><td class='boldbase' align='center'><b>$Lang::tr{'network'}</td><td class='boldbase' width='15%' align='center'><b>$Lang::tr{'ccd used'}</td><td width='3%'></td><td width='3%'></td></tr>
2489 END
2490 ;
2491 }
2492 else{
2493 if (! -e "/var/run/openvpn.pid"){
2494 &Header::openbox('100%', 'LEFT', $Lang::tr{'ccd add'});
2495 print <<END;
2496 <table width='100%' border='0'>
2497 <tr><form method='post'>
2498 <td colspan='4'>$Lang::tr{'ccd hint'}<br><br></td></tr>
2499 <tr>
2500 <td width='10%' nowrap='nwrap'>$Lang::tr{'ccd name'}:</td><td><input type='TEXT' name='ccdname' value='$cgiparams{'ccdname'}' /></td>
2501 <td width='8%'>$Lang::tr{'ccd subnet'}:</td><td><input type='TEXT' name='ccdsubnet' value='$cgiparams{'ccdsubnet'}' /></td></tr>
2502 <tr><td colspan=4><hr /></td></tr><tr>
2503 <td colspan='4' align='right'><input type='hidden' name='ACTION' value='$Lang::tr{'ccd add'}' /><input type='submit' value='$Lang::tr{'add'}' /><input type='hidden' name='DOVPN_SUBNET' value='$cgiparams{'DOVPN_SUBNET'}'/></td></tr>
2504 </table></form>
2505 END
2506
2507 &Header::closebox();
2508 }
2509 &Header::openbox('100%', 'LEFT',$Lang::tr{'ccd net'} );
2510 print <<END
2511 <table width='100%' border='0' cellpadding='0' cellspacing='1'>
2512 <tr>
2513 <td class='boldbase' align='center' nowrap='nowrap' width='20%'><b>$Lang::tr{'ccd name'}</td><td class='boldbase' align='center' width='8%'><b>$Lang::tr{'network'}</td><td class='boldbase' width='8%' align='center' nowrap='nowrap'><b>$Lang::tr{'ccd used'}</td><td width='1%' align='center'></td><td width='1%' align='center'></td></tr>
2514 END
2515 ;
2516 }
2517 my %ccdconfhash=();
2518 &General::readhasharray("${General::swroot}/ovpn/ccd.conf", \%ccdconfhash);
2519 my @ccdconf=();
2520 my $count=0;
2521 foreach my $key (keys %ccdconfhash) {
2522 @ccdconf=($ccdconfhash{$key}[0],$ccdconfhash{$key}[1]);
2523 $count++;
2524 my $ccdhosts = &hostsinnet($ccdconf[0]);
2525 if ($count % 2){ print" <tr bgcolor='$color{'color22'}'>";}
2526 else{ print" <tr bgcolor='$color{'color20'}'>";}
2527 print"<td>$ccdconf[0]</td><td align='center'>$ccdconf[1]</td><td align='center'>$ccdhosts/".(&ccdmaxclients($ccdconf[1])+1)."</td><td>";
2528 print <<END
2529 <form method='post' />
2530 <input type='image' src='/images/edit.gif' align='middle' alt=$Lang::tr{'edit'} title=$Lang::tr{'edit'} />
2531 <input type='hidden' name='ACTION' value='edit'/>
2532 <input type='hidden' name='ccdname' value='$ccdconf[0]' />
2533 <input type='hidden' name='ccdsubnet' value='$ccdconf[1]' />
2534 </form></td>