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