HinzugefĆ¼gt:
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / ovpnmain.cgi
1 #!/usr/bin/perl
2 # based on SmoothWall and IPCop CGIs
3 #
4 # This code is distributed under the terms of the GPL
5 # Main idea from zeroconcept
6 # ZERNINA-VERSION:0.9.7a7
7 # (c) 2005 Ufuk Altinkaynak
8 #
9 # Ipcop and OpenVPN easy as one two three..
10 #
11
12 use CGI;
13 use CGI qw/:standard/;
14 use Net::DNS;
15 use File::Copy;
16 use File::Temp qw/ tempfile tempdir /;
17 use strict;
18 use Archive::Zip qw(:ERROR_CODES :CONSTANTS);
19 use Net::Ping;
20 require '/var/ipfire/general-functions.pl';
21 require '/home/httpd/cgi-bin/ovpnfunc.pl';
22 require "${General::swroot}/lang.pl";
23 require "${General::swroot}/header.pl";
24 require "${General::swroot}/countries.pl";
25
26 # enable only the following on debugging purpose
27 #use warnings;
28 #use CGI::Carp 'fatalsToBrowser';
29 #workaround to suppress a warning when a variable is used only once
30 my @dummy = ( ${Header::colourgreen} );
31 undef (@dummy);
32
33
34
35 ###
36 ### Initialize variables
37 ###
38 my %netsettings=();
39 my %cgiparams=();
40 my %vpnsettings=();
41 my %checked=();
42 my %confighash=();
43 my %cahash=();
44 my %selected=();
45 my $warnmessage = '';
46 my $errormessage = '';
47 my %settings=();
48 my $zerinaclient = '';
49 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
50 $cgiparams{'ENABLED'} = 'off';
51 $cgiparams{'ENABLED_BLUE'} = 'off';
52 $cgiparams{'ENABLED_ORANGE'} = 'off';
53 $cgiparams{'EDIT_ADVANCED'} = 'off';
54 $cgiparams{'NAT'} = 'off';
55 $cgiparams{'COMPRESSION'} = 'off';
56 $cgiparams{'ONLY_PROPOSED'} = 'off';
57 $cgiparams{'ACTION'} = '';
58 $cgiparams{'CA_NAME'} = '';
59 $cgiparams{'DHCP_DOMAIN'} = '';
60 $cgiparams{'DHCP_DNS'} = '';
61 $cgiparams{'DHCP_WINS'} = '';
62 $cgiparams{'DCOMPLZO'} = 'off';
63 &Header::getcgihash(\%cgiparams, {'wantfile' => 1, 'filevar' => 'FH'});
64
65 # prepare openvpn config file
66 ###
67 ### Useful functions
68 ###
69
70 ###
71 ### OpenVPN Server Control
72 ###
73 if ($cgiparams{'ACTION'} eq $Lang::tr{'start ovpn server'} ||
74 $cgiparams{'ACTION'} eq $Lang::tr{'stop ovpn server'} ||
75 $cgiparams{'ACTION'} eq $Lang::tr{'restart ovpn server'}) {
76 #start openvpn server
77 if ($cgiparams{'ACTION'} eq $Lang::tr{'start ovpn server'}){
78 &Ovpnfunc::emptyserverlog();
79 system('/usr/local/bin/openvpnctrl', '-s');
80 }
81 #stop openvpn server
82 if ($cgiparams{'ACTION'} eq $Lang::tr{'stop ovpn server'}){
83 system('/usr/local/bin/openvpnctrl', '-k');
84 &Ovpnfunc::emptyserverlog();
85 }
86 # #restart openvpn server
87 if ($cgiparams{'ACTION'} eq $Lang::tr{'restart ovpn server'}){
88 #workarund, till SIGHUP also works when running as nobody
89 system('/usr/local/bin/openvpnctrl', '-r');
90 &Ovpnfunc::emptyserverlog();
91 }
92 }
93
94
95
96 ###
97 ### Save Advanced options
98 ###
99
100 if ($cgiparams{'ACTION'} eq $Lang::tr{'save-adv-options'}) {
101 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
102 #DAN do we really need (to to check) this value? Besides if we listen on blue and orange too,
103 #DAN this value has to leave.
104 #new settings for daemon
105 $vpnsettings{'LOG_VERB'} = $cgiparams{'LOG_VERB'};
106 $vpnsettings{'KEEPALIVE_1'} = $cgiparams{'KEEPALIVE_1'};
107 $vpnsettings{'KEEPALIVE_2'} = $cgiparams{'KEEPALIVE_2'};
108 $vpnsettings{'MAX_CLIENTS'} = $cgiparams{'MAX_CLIENTS'};
109 $vpnsettings{'REDIRECT_GW_DEF1'} = $cgiparams{'REDIRECT_GW_DEF1'};
110 $vpnsettings{'CLIENT2CLIENT'} = $cgiparams{'CLIENT2CLIENT'};
111 $vpnsettings{'DHCP_DOMAIN'} = $cgiparams{'DHCP_DOMAIN'};
112 $vpnsettings{'DHCP_DNS'} = $cgiparams{'DHCP_DNS'};
113 $vpnsettings{'DHCP_WINS'} = $cgiparams{'DHCP_WINS'};
114 #additional push route
115 $vpnsettings{'AD_ROUTE1'} = $cgiparams{'AD_ROUTE1'};
116 $vpnsettings{'AD_ROUTE2'} = $cgiparams{'AD_ROUTE2'};
117 $vpnsettings{'AD_ROUTE3'} = $cgiparams{'AD_ROUTE3'};
118 #additional push route
119
120 #################################################################################
121 # Added by Philipp Jenni #
122 # #
123 # Contact: philipp.jenni-at-gmx.ch #
124 # Date: 2006-04-22 #
125 # Description: Add the FAST-IO Parameter from OpenVPN to the Zerina Config #
126 # Add the NICE Parameter from OpenVPN to the Zerina Config #
127 # Add the MTU-DISC Parameter from OpenVPN to the Zerina Config #
128 # Add the MSSFIX Parameter from OpenVPN to the Zerina Config #
129 # Add the FRAMGMENT Parameter from OpenVPN to the Zerina Config #
130 #################################################################################
131 $vpnsettings{'EXTENDED_FASTIO'} = $cgiparams{'EXTENDED_FASTIO'};
132 $vpnsettings{'EXTENDED_NICE'} = $cgiparams{'EXTENDED_NICE'};
133 $vpnsettings{'EXTENDED_MTUDISC'} = $cgiparams{'EXTENDED_MTUDISC'};
134 $vpnsettings{'EXTENDED_MSSFIX'} = $cgiparams{'EXTENDED_MSSFIX'};
135 $vpnsettings{'EXTENDED_FRAGMENT'} = $cgiparams{'EXTENDED_FRAGMENT'};
136 #################################################################################
137 # End of Inserted Data #
138 #################################################################################
139
140
141 if ($cgiparams{'DHCP_DOMAIN'} ne ''){
142 unless (&General::validfqdn($cgiparams{'DHCP_DOMAIN'}) || &General::validip($cgiparams{'DHCP_DOMAIN'})) {
143 $errormessage = $Lang::tr{'invalid input for dhcp domain'};
144 goto ADV_ERROR;
145 }
146 }
147 if ($cgiparams{'DHCP_DNS'} ne ''){
148 unless (&General::validfqdn($cgiparams{'DHCP_DNS'}) || &General::validip($cgiparams{'DHCP_DNS'})) {
149 $errormessage = $Lang::tr{'invalid input for dhcp dns'};
150 goto ADV_ERROR;
151 }
152 }
153 if ($cgiparams{'DHCP_WINS'} ne ''){
154 unless (&General::validfqdn($cgiparams{'DHCP_WINS'}) || &General::validip($cgiparams{'DHCP_WINS'})) {
155 $errormessage = $Lang::tr{'invalid input for dhcp wins'};
156 goto ADV_ERROR;
157 }
158 }
159 if ($cgiparams{'AD_ROUTE1'} ne ''){
160 if (! &General::validipandmask($cgiparams{'AD_ROUTE1'})) {
161 $errormessage = $Lang::tr{'route subnet is invalid'};
162 goto ADV_ERROR;
163 }
164 }
165 if ($cgiparams{'AD_ROUTE2'} ne ''){
166 if (! &General::validipandmask($cgiparams{'AD_ROUTE2'})) {
167 $errormessage = $Lang::tr{'route subnet is invalid'};
168 goto ADV_ERROR;
169 }
170 }
171 if ($cgiparams{'AD_ROUTE3'} ne ''){
172 if (! &General::validipandmask($cgiparams{'AD_ROUTE3'})) {
173 $errormessage = $Lang::tr{'route subnet is invalid'};
174 goto ADV_ERROR;
175 }
176 }
177
178 if ((length($cgiparams{'MAX_CLIENTS'}) == 0) || (($cgiparams{'MAX_CLIENTS'}) < 1 ) || (($cgiparams{'MAX_CLIENTS'}) > 255 )) {
179 $errormessage = $Lang::tr{'invalid input for max clients'};
180 goto ADV_ERROR;
181 }
182 if ($cgiparams{'KEEPALIVE_1'} ne '') {
183 if ($cgiparams{'KEEPALIVE_1'} !~ /^[0-9]+$/) {
184 $errormessage = $Lang::tr{'invalid input for keepalive 1'};
185 goto ADV_ERROR;
186 }
187 }
188 if ($cgiparams{'KEEPALIVE_2'} ne ''){
189 if ($cgiparams{'KEEPALIVE_2'} !~ /^[0-9]+$/) {
190 $errormessage = $Lang::tr{'invalid input for keepalive 2'};
191 goto ADV_ERROR;
192 }
193 }
194 if ($cgiparams{'KEEPALIVE_2'} < ($cgiparams{'KEEPALIVE_1'} * 2)){
195 $errormessage = $Lang::tr{'invalid input for keepalive 1:2'};
196 goto ADV_ERROR;
197 }
198
199 &General::writehash("${General::swroot}/ovpn/settings", \%vpnsettings);
200 &Ovpnfunc::writeserverconf();#hier ok
201 }
202
203 ###
204 ### Save main settings
205 ###
206 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq '' && $cgiparams{'KEY'} eq '') {
207 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
208 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
209 #DAN do we really need (to to check) this value? Besides if we listen on blue and orange too,
210 #DAN this value has to leave.
211 if ($cgiparams{'ENABLED'} eq 'on'){
212 unless (&General::validfqdn($cgiparams{'VPN_IP'}) || &General::validip($cgiparams{'VPN_IP'})) {
213 $errormessage = $Lang::tr{'invalid input for hostname'};
214 goto SETTINGS_ERROR;
215 }
216 }
217 if ($cgiparams{'ENABLED'} eq 'on'){
218 $errormessage = &Ovpnfunc::disallowreserved($cgiparams{'DDEST_PORT'},0,$cgiparams{'DPROTOCOL'},"dest");
219 }
220 if ($errormessage) { goto SETTINGS_ERROR; }
221
222
223 if ($cgiparams{'ENABLED'} eq 'on'){
224 $errormessage = &Ovpnfunc::checkportfw(0,$cgiparams{'DDEST_PORT'},$cgiparams{'DPROTOCOL'},'0.0.0.0');
225 }
226
227 if ($errormessage) { goto SETTINGS_ERROR; }
228
229 if (! &General::validipandmask($cgiparams{'DOVPN_SUBNET'})) {
230 $errormessage = $Lang::tr{'ovpn subnet is invalid'};
231 goto SETTINGS_ERROR;
232 }
233 my @tmpovpnsubnet = split("\/",$cgiparams{'DOVPN_SUBNET'});
234 $tmpovpnsubnet[1] = &Ovpnfunc::cidrormask($tmpovpnsubnet[1]);
235 $cgiparams{'DOVPN_SUBNET'} = "$tmpovpnsubnet[0]/$tmpovpnsubnet[1]";#convert from cidr
236 #plausi1
237 $errormessage = &Ovpnfunc::ovelapplausi($tmpovpnsubnet[0],$tmpovpnsubnet[1]);
238 #plausi1
239 if ($errormessage ne ''){
240 goto SETTINGS_ERROR;
241 }
242 if ($cgiparams{'ENABLED'} !~ /^(on|off)$/) {
243 $errormessage = $Lang::tr{'invalid input'};
244 goto SETTINGS_ERROR;
245 }
246 if ((length($cgiparams{'DMTU'})==0) || (($cgiparams{'DMTU'}) < 1000 )) {
247 $errormessage = $Lang::tr{'invalid mtu input'};
248 goto SETTINGS_ERROR;
249 }
250
251 unless (&General::validport($cgiparams{'DDEST_PORT'})) {
252 $errormessage = $Lang::tr{'invalid port'};
253 goto SETTINGS_ERROR;
254 }
255 #hhh
256 foreach my $dkey (keys %confighash) {#Check if there is no other entry with this name
257 if ($confighash{$dkey}[14] eq $cgiparams{'DPROTOCOL'} && $confighash{$dkey}[15] eq $cgiparams{'DDEST_PORT'}){
258 $errormessage = "Choosed Protocol/Port combination is already used by connection: $confighash{$dkey}[1]";
259 goto SETTINGS_ERROR;
260 }
261 }
262 #hhh
263 $vpnsettings{'ENABLED_BLUE'} = $cgiparams{'ENABLED_BLUE'};
264 $vpnsettings{'ENABLED_ORANGE'} =$cgiparams{'ENABLED_ORANGE'};
265 $vpnsettings{'ENABLED'} = $cgiparams{'ENABLED'};
266 $vpnsettings{'VPN_IP'} = $cgiparams{'VPN_IP'};
267 #new settings for daemon
268 $vpnsettings{'DOVPN_SUBNET'} = $cgiparams{'DOVPN_SUBNET'};
269 $vpnsettings{'DDEVICE'} = $cgiparams{'DDEVICE'};
270 $vpnsettings{'DPROTOCOL'} = $cgiparams{'DPROTOCOL'};
271 $vpnsettings{'DDEST_PORT'} = $cgiparams{'DDEST_PORT'};
272 $vpnsettings{'DMTU'} = $cgiparams{'DMTU'};
273 $vpnsettings{'DCOMPLZO'} = $cgiparams{'DCOMPLZO'};
274 $vpnsettings{'DCIPHER'} = $cgiparams{'DCIPHER'};
275 #new settings for daemon
276 &General::writehash("${General::swroot}/ovpn/settings", \%vpnsettings);
277 &Ovpnfunc::writeserverconf();#hier ok
278 SETTINGS_ERROR:
279 ###
280 ### Reset all step 2
281 ###
282 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'reset'} && $cgiparams{'AREUSURE'} eq 'yes') {
283 my $file = '';
284 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
285
286 foreach my $key (keys %confighash) {
287 if ($confighash{$key}[4] eq 'cert') {
288 delete $confighash{$cgiparams{'$key'}};
289 }
290 }
291 while ($file = glob("${General::swroot}/ovpn/ca/*")) {
292 unlink $file
293 }
294 while ($file = glob("${General::swroot}/ovpn/certs/*")) {
295 unlink $file
296 }
297 while ($file = glob("${General::swroot}/ovpn/crls/*")) {
298 unlink $file
299 }
300 &Ovpnfunc::cleanssldatabase();
301 if (open(FILE, ">${General::swroot}/ovpn/caconfig")) {
302 print FILE "";
303 close FILE;
304 }
305 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
306 ###
307 ### Reset all step 1
308 ###
309 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'reset'}) {
310 &Header::showhttpheaders();
311 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
312 &Header::openbigbox('100%', 'LEFT', '', '');
313 &Header::openbox('100%', 'LEFT', $Lang::tr{'are you sure'});
314 print <<END
315 <table><form method='post'><input type='hidden' name='AREUSURE' value='yes' />
316 <tr><td align='center'>
317 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>:
318 $Lang::tr{'resetting the vpn configuration will remove the root ca, the host certificate and all certificate based connections'}
319 <tr><td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'reset'}' />
320 <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></td></tr>
321 </form></table>
322 END
323 ;
324 &Header::closebox();
325 &Header::closebigbox();
326 &Header::closepage();
327 exit (0);
328
329 ###
330 ### Upload CA Certificate
331 ###
332 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload ca certificate'}) {
333 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
334
335 if ($cgiparams{'CA_NAME'} !~ /^[a-zA-Z0-9]+$/) {
336 $errormessage = $Lang::tr{'name must only contain characters'};
337 goto UPLOADCA_ERROR;
338 }
339
340 if (length($cgiparams{'CA_NAME'}) >60) {
341 $errormessage = $Lang::tr{'name too long'};
342 goto VPNCONF_ERROR;
343 }
344
345 if ($cgiparams{'CA_NAME'} eq 'ca') {
346 $errormessage = $Lang::tr{'name is invalid'};
347 goto UPLOAD_CA_ERROR;
348 }
349
350 # Check if there is no other entry with this name
351 foreach my $key (keys %cahash) {
352 if ($cahash{$key}[0] eq $cgiparams{'CA_NAME'}) {
353 $errormessage = $Lang::tr{'a ca certificate with this name already exists'};
354 goto UPLOADCA_ERROR;
355 }
356 }
357
358 if (ref ($cgiparams{'FH'}) ne 'Fh') {
359 $errormessage = $Lang::tr{'there was no file upload'};
360 goto UPLOADCA_ERROR;
361 }
362 # Move uploaded ca to a temporary file
363 (my $fh, my $filename) = tempfile( );
364 if (copy ($cgiparams{'FH'}, $fh) != 1) {
365 $errormessage = $!;
366 goto UPLOADCA_ERROR;
367 }
368 my $temp = `/usr/bin/openssl x509 -text -in $filename`;
369 if ($temp !~ /CA:TRUE/i) {
370 $errormessage = $Lang::tr{'not a valid ca certificate'};
371 unlink ($filename);
372 goto UPLOADCA_ERROR;
373 } else {
374 move($filename, "${General::swroot}/ovpn/ca/$cgiparams{'CA_NAME'}cert.pem");
375 if ($? ne 0) {
376 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
377 unlink ($filename);
378 goto UPLOADCA_ERROR;
379 }
380 }
381
382 my $casubject = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/ca/$cgiparams{'CA_NAME'}cert.pem`;
383 $casubject =~ /Subject: (.*)[\n]/;
384 $casubject = $1;
385 $casubject =~ s+/Email+, E+;
386 $casubject =~ s/ ST=/ S=/;
387 $casubject = &Header::cleanhtml($casubject);
388
389 my $key = &General::findhasharraykey (\%cahash);
390 $cahash{$key}[0] = $cgiparams{'CA_NAME'};
391 $cahash{$key}[1] = $casubject;
392 &General::writehasharray("${General::swroot}/ovpn/caconfig", \%cahash);
393 UPLOADCA_ERROR:
394
395 ###
396 ### Display ca certificate
397 ###
398 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show ca certificate'}) {
399 &Ovpnfunc::displayca($cgiparams{'KEY'});
400 ###
401 ### Download ca certificate
402 ###
403 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download ca certificate'}) {
404 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
405
406 if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
407 print "Content-Type: application/octet-stream\r\n";
408 print "Content-Disposition: filename=$cahash{$cgiparams{'KEY'}}[0]cert.pem\r\n\r\n";
409 print `/usr/bin/openssl x509 -in ${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
410 exit(0);
411 } else {
412 $errormessage = $Lang::tr{'invalid key'};
413 }
414
415 ###
416 ### Remove ca certificate (step 2)
417 ###
418 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'} && $cgiparams{'AREUSURE'} eq 'yes') {
419 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
420 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
421
422 if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
423 foreach my $key (keys %confighash) {
424 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`;
425 if ($test =~ /: OK/) {
426 unlink ("${General::swroot}/ovpn//certs/$confighash{$key}[1]cert.pem");
427 unlink ("${General::swroot}/ovpn/certs/$confighash{$key}[1].p12");
428 delete $confighash{$key};
429 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
430 }
431 }
432 unlink ("${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
433 delete $cahash{$cgiparams{'KEY'}};
434 &General::writehasharray("${General::swroot}/ovpn/caconfig", \%cahash);
435 } else {
436 $errormessage = $Lang::tr{'invalid key'};
437 }
438 ###
439 ### Remove ca certificate (step 1)
440 ###
441 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove ca certificate'}) {
442 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
443 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
444
445 my $assignedcerts = 0;
446 if ( -f "${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
447 foreach my $key (keys %confighash) {
448 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`;
449 if ($test =~ /: OK/) {
450 $assignedcerts++;
451 }
452 }
453 if ($assignedcerts) {
454 &Header::showhttpheaders();
455 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
456 &Header::openbigbox('100%', 'LEFT', '', $errormessage);
457 &Header::openbox('100%', 'LEFT', $Lang::tr{'are you sure'});
458 print <<END
459 <table><form method='post'><input type='hidden' name='AREUSURE' value='yes' />
460 <input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />
461 <tr><td align='center'>
462 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>: $assignedcerts
463 $Lang::tr{'connections are associated with this ca. deleting the ca will delete these connections as well.'}
464 <tr><td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'remove ca certificate'}' />
465 <input type='submit' name='ACTION' value='$Lang::tr{'cancel'}' /></td></tr>
466 </form></table>
467 END
468 ;
469 &Header::closebox();
470 &Header::closebigbox();
471 &Header::closepage();
472 exit (0);
473 } else {
474 unlink ("${General::swroot}/ovpn/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
475 delete $cahash{$cgiparams{'KEY'}};
476 &General::writehasharray("${General::swroot}/ovpn/caconfig", \%cahash);
477 # system('/usr/local/bin/ipsecctrl', 'R');
478 }
479 } else {
480 $errormessage = $Lang::tr{'invalid key'};
481 }
482
483 ###
484 ### Display root certificate
485 ###
486 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'} || $cgiparams{'ACTION'} eq $Lang::tr{'show host certificate'}) {
487 &Ovpnfunc::displayroothost($cgiparams{'ACTION'});
488 ###
489 ### Download root certificate
490 ###
491 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download root certificate'}) {
492 if ( -f "${General::swroot}/ovpn/ca/cacert.pem" ) {
493 print "Content-Type: application/octet-stream\r\n";
494 print "Content-Disposition: filename=cacert.pem\r\n\r\n";
495 print `/usr/bin/openssl x509 -in ${General::swroot}/ovpn/ca/cacert.pem`;
496 exit(0);
497 }
498
499 ###
500 ### Download host certificate
501 ###
502 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download host certificate'}) {
503 if ( -f "${General::swroot}/ovpn/certs/servercert.pem" ) {
504 print "Content-Type: application/octet-stream\r\n";
505 print "Content-Disposition: filename=servercert.pem\r\n\r\n";
506 print `/usr/bin/openssl x509 -in ${General::swroot}/ovpn/certs/servercert.pem`;
507 exit(0);
508 }
509 ###
510 ### Form for generating a root certificate
511 ###
512 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'generate root/host certificates'} ||
513 $cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
514
515 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
516 if (-f "${General::swroot}/ovpn/ca/cacert.pem") {
517 $errormessage = $Lang::tr{'valid root certificate already exists'};
518 $cgiparams{'ACTION'} = '';
519 goto ROOTCERT_ERROR;
520 }
521
522 if (($cgiparams{'ROOTCERT_HOSTNAME'} eq '') && -e "${General::swroot}/red/active") {
523 if (open(IPADDR, "${General::swroot}/red/local-ipaddress")) {
524 my $ipaddr = <IPADDR>;
525 close IPADDR;
526 chomp ($ipaddr);
527 $cgiparams{'ROOTCERT_HOSTNAME'} = (gethostbyaddr(pack("C4", split(/\./, $ipaddr)), 2))[0];
528 if ($cgiparams{'ROOTCERT_HOSTNAME'} eq '') {
529 $cgiparams{'ROOTCERT_HOSTNAME'} = $ipaddr;
530 }
531 }
532 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
533
534 if (ref ($cgiparams{'FH'}) ne 'Fh') {
535 $errormessage = $Lang::tr{'there was no file upload'};
536 goto ROOTCERT_ERROR;
537 }
538
539 # Move uploaded certificate request to a temporary file
540 (my $fh, my $filename) = tempfile( );
541 if (copy ($cgiparams{'FH'}, $fh) != 1) {
542 $errormessage = $!;
543 goto ROOTCERT_ERROR;
544 }
545
546 # Create a temporary dirctory
547 my $tempdir = tempdir( CLEANUP => 1 );
548
549 # Extract the CA certificate from the file
550 my $pid = open(OPENSSL, "|-");
551 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
552 if ($pid) { # parent
553 if ($cgiparams{'P12_PASS'} ne '') {
554 print OPENSSL "$cgiparams{'P12_PASS'}\n";
555 }
556 close (OPENSSL);
557 if ($?) {
558 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
559 unlink ($filename);
560 goto ROOTCERT_ERROR;
561 }
562 } else { # child
563 unless (exec ('/usr/bin/openssl', 'pkcs12', '-cacerts', '-nokeys',
564 '-in', $filename,
565 '-out', "$tempdir/cacert.pem")) {
566 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
567 unlink ($filename);
568 goto ROOTCERT_ERROR;
569 }
570 }
571
572 # Extract the Host certificate from the file
573 $pid = open(OPENSSL, "|-");
574 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
575 if ($pid) { # parent
576 if ($cgiparams{'P12_PASS'} ne '') {
577 print OPENSSL "$cgiparams{'P12_PASS'}\n";
578 }
579 close (OPENSSL);
580 if ($?) {
581 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
582 unlink ($filename);
583 goto ROOTCERT_ERROR;
584 }
585 } else { # child
586 unless (exec ('/usr/bin/openssl', 'pkcs12', '-clcerts', '-nokeys',
587 '-in', $filename,
588 '-out', "$tempdir/hostcert.pem")) {
589 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
590 unlink ($filename);
591 goto ROOTCERT_ERROR;
592 }
593 }
594
595 # Extract the Host key from the file
596 $pid = open(OPENSSL, "|-");
597 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
598 if ($pid) { # parent
599 if ($cgiparams{'P12_PASS'} ne '') {
600 print OPENSSL "$cgiparams{'P12_PASS'}\n";
601 }
602 close (OPENSSL);
603 if ($?) {
604 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
605 unlink ($filename);
606 goto ROOTCERT_ERROR;
607 }
608 } else { # child
609 unless (exec ('/usr/bin/openssl', 'pkcs12', '-nocerts',
610 '-nodes',
611 '-in', $filename,
612 '-out', "$tempdir/serverkey.pem")) {
613 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
614 unlink ($filename);
615 goto ROOTCERT_ERROR;
616 }
617 }
618
619 move("$tempdir/cacert.pem", "${General::swroot}/ovpn/ca/cacert.pem");
620 if ($? ne 0) {
621 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
622 unlink ($filename);
623 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
624 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
625 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
626 goto ROOTCERT_ERROR;
627 }
628
629 move("$tempdir/hostcert.pem", "${General::swroot}/ovpn/certs/servercert.pem");
630 if ($? ne 0) {
631 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
632 unlink ($filename);
633 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
634 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
635 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
636 goto ROOTCERT_ERROR;
637 }
638
639 move("$tempdir/serverkey.pem", "${General::swroot}/ovpn/certs/serverkey.pem");
640 if ($? ne 0) {
641 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
642 unlink ($filename);
643 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
644 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
645 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
646 goto ROOTCERT_ERROR;
647 }
648
649 goto ROOTCERT_SUCCESS;
650
651 } elsif ($cgiparams{'ROOTCERT_COUNTRY'} ne '') {
652
653 # Validate input since the form was submitted
654 if ($cgiparams{'ROOTCERT_ORGANIZATION'} eq ''){
655 $errormessage = $Lang::tr{'organization cant be empty'};
656 goto ROOTCERT_ERROR;
657 }
658 if (length($cgiparams{'ROOTCERT_ORGANIZATION'}) >60) {
659 $errormessage = $Lang::tr{'organization too long'};
660 goto ROOTCERT_ERROR;
661 }
662 if ($cgiparams{'ROOTCERT_ORGANIZATION'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
663 $errormessage = $Lang::tr{'invalid input for organization'};
664 goto ROOTCERT_ERROR;
665 }
666 if ($cgiparams{'ROOTCERT_HOSTNAME'} eq ''){
667 $errormessage = $Lang::tr{'hostname cant be empty'};
668 goto ROOTCERT_ERROR;
669 }
670 unless (&General::validfqdn($cgiparams{'ROOTCERT_HOSTNAME'}) || &General::validip($cgiparams{'ROOTCERT_HOSTNAME'})) {
671 $errormessage = $Lang::tr{'invalid input for hostname'};
672 goto ROOTCERT_ERROR;
673 }
674 if ($cgiparams{'ROOTCERT_EMAIL'} ne '' && (! &General::validemail($cgiparams{'ROOTCERT_EMAIL'}))) {
675 $errormessage = $Lang::tr{'invalid input for e-mail address'};
676 goto ROOTCERT_ERROR;
677 }
678 if (length($cgiparams{'ROOTCERT_EMAIL'}) > 40) {
679 $errormessage = $Lang::tr{'e-mail address too long'};
680 goto ROOTCERT_ERROR;
681 }
682 if ($cgiparams{'ROOTCERT_OU'} ne '' && $cgiparams{'ROOTCERT_OU'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
683 $errormessage = $Lang::tr{'invalid input for department'};
684 goto ROOTCERT_ERROR;
685 }
686 if ($cgiparams{'ROOTCERT_CITY'} ne '' && $cgiparams{'ROOTCERT_CITY'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
687 $errormessage = $Lang::tr{'invalid input for city'};
688 goto ROOTCERT_ERROR;
689 }
690 if ($cgiparams{'ROOTCERT_STATE'} ne '' && $cgiparams{'ROOTCERT_STATE'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
691 $errormessage = $Lang::tr{'invalid input for state or province'};
692 goto ROOTCERT_ERROR;
693 }
694 if ($cgiparams{'ROOTCERT_COUNTRY'} !~ /^[A-Z]*$/) {
695 $errormessage = $Lang::tr{'invalid input for country'};
696 goto ROOTCERT_ERROR;
697 }
698
699 # Copy the cgisettings to vpnsettings and save the configfile
700 $vpnsettings{'ROOTCERT_ORGANIZATION'} = $cgiparams{'ROOTCERT_ORGANIZATION'};
701 $vpnsettings{'ROOTCERT_HOSTNAME'} = $cgiparams{'ROOTCERT_HOSTNAME'};
702 $vpnsettings{'ROOTCERT_EMAIL'} = $cgiparams{'ROOTCERT_EMAIL'};
703 $vpnsettings{'ROOTCERT_OU'} = $cgiparams{'ROOTCERT_OU'};
704 $vpnsettings{'ROOTCERT_CITY'} = $cgiparams{'ROOTCERT_CITY'};
705 $vpnsettings{'ROOTCERT_STATE'} = $cgiparams{'ROOTCERT_STATE'};
706 $vpnsettings{'ROOTCERT_COUNTRY'} = $cgiparams{'ROOTCERT_COUNTRY'};
707 &General::writehash("${General::swroot}/ovpn/settings", \%vpnsettings);
708
709 # Replace empty strings with a .
710 (my $ou = $cgiparams{'ROOTCERT_OU'}) =~ s/^\s*$/\./;
711 (my $city = $cgiparams{'ROOTCERT_CITY'}) =~ s/^\s*$/\./;
712 (my $state = $cgiparams{'ROOTCERT_STATE'}) =~ s/^\s*$/\./;
713
714 # refresh
715 #system ('/bin/touch', "${General::swroot}/ovpn/gencanow");
716
717 # Create the CA certificate
718 my $pid = open(OPENSSL, "|-");
719 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
720 if ($pid) { # parent
721 print OPENSSL "$cgiparams{'ROOTCERT_COUNTRY'}\n";
722 print OPENSSL "$state\n";
723 print OPENSSL "$city\n";
724 print OPENSSL "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
725 print OPENSSL "$ou\n";
726 print OPENSSL "$cgiparams{'ROOTCERT_ORGANIZATION'} CA\n";
727 print OPENSSL "$cgiparams{'ROOTCERT_EMAIL'}\n";
728 close (OPENSSL);
729 if ($?) {
730 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
731 unlink ("${General::swroot}/ovpn/ca/cakey.pem");
732 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
733 goto ROOTCERT_ERROR;
734 }
735 } else { # child
736 unless (exec ('/usr/bin/openssl', 'req', '-x509', '-nodes', '-rand', '/proc/interrupts:/proc/net/rt_cache',
737 '-days', '999999', '-newkey', 'rsa:2048',
738 '-keyout', "${General::swroot}/ovpn/ca/cakey.pem",
739 '-out', "${General::swroot}/ovpn/ca/cacert.pem",
740 '-config',"${General::swroot}/ovpn/openssl/ovpn.cnf")) {
741 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
742 goto ROOTCERT_ERROR;
743 }
744 }
745
746 # Create the Host certificate request
747 $pid = open(OPENSSL, "|-");
748 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto ROOTCERT_ERROR;};
749 if ($pid) { # parent
750 print OPENSSL "$cgiparams{'ROOTCERT_COUNTRY'}\n";
751 print OPENSSL "$state\n";
752 print OPENSSL "$city\n";
753 print OPENSSL "$cgiparams{'ROOTCERT_ORGANIZATION'}\n";
754 print OPENSSL "$ou\n";
755 print OPENSSL "$cgiparams{'ROOTCERT_HOSTNAME'}\n";
756 print OPENSSL "$cgiparams{'ROOTCERT_EMAIL'}\n";
757 print OPENSSL ".\n";
758 print OPENSSL ".\n";
759 close (OPENSSL);
760 if ($?) {
761 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
762 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
763 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
764 goto ROOTCERT_ERROR;
765 }
766 } else { # child
767 unless (exec ('/usr/bin/openssl', 'req', '-nodes', '-rand', '/proc/interrupts:/proc/net/rt_cache',
768 '-newkey', 'rsa:1024',
769 '-keyout', "${General::swroot}/ovpn/certs/serverkey.pem",
770 '-out', "${General::swroot}/ovpn/certs/serverreq.pem",
771 '-extensions', 'server',
772 '-config', "${General::swroot}/ovpn/openssl/ovpn.cnf" )) {
773 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
774 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
775 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
776 unlink ("${General::swroot}/ovpn/ca/cakey.pem");
777 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
778 goto ROOTCERT_ERROR;
779 }
780 }
781
782 # Sign the host certificate request
783 system('/usr/bin/openssl', 'ca', '-days', '999999',
784 '-batch', '-notext',
785 '-in', "${General::swroot}/ovpn/certs/serverreq.pem",
786 '-out', "${General::swroot}/ovpn/certs/servercert.pem",
787 '-extensions', 'server',
788 '-config', "${General::swroot}/ovpn/openssl/ovpn.cnf");
789 if ($?) {
790 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
791 unlink ("${General::swroot}/ovpn/ca/cakey.pem");
792 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
793 unlink ("${General::swroot}/ovpn/serverkey.pem");
794 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
795 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
796 &Ovpnfunc::newcleanssldatabase();
797 goto ROOTCERT_ERROR;
798 } else {
799 unlink ("${General::swroot}/ovpn/certs/serverreq.pem");
800 &Ovpnfunc::deletebackupcert();
801 }
802
803 # Create an empty CRL
804 system('/usr/bin/openssl', 'ca', '-gencrl',
805 '-out', "${General::swroot}/ovpn/crls/cacrl.pem",
806 '-config', "${General::swroot}/ovpn/openssl/ovpn.cnf" );
807 if ($?) {
808 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
809 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
810 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
811 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
812 unlink ("${General::swroot}/ovpn/crls/cacrl.pem");
813 &Ovpnfunc::cleanssldatabase();
814 goto ROOTCERT_ERROR;
815 }
816 # Create Diffie Hellmann Parameter
817 system('/usr/bin/openssl', 'dhparam', '-rand', '/proc/interrupts:/proc/net/rt_cache',
818 '-out', "${General::swroot}/ovpn/ca/dh1024.pem",
819 '1024' );
820 if ($?) {
821 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
822 unlink ("${General::swroot}/ovpn/certs/serverkey.pem");
823 unlink ("${General::swroot}/ovpn/certs/servercert.pem");
824 unlink ("${General::swroot}/ovpn/ca/cacert.pem");
825 unlink ("${General::swroot}/ovpn/crls/cacrl.pem");
826 unlink ("${General::swroot}/ovpn/ca/dh1024.pem");
827 &Ovpnfunc::cleanssldatabase();
828 goto ROOTCERT_ERROR;
829 }
830 goto ROOTCERT_SUCCESS;
831 }
832 ROOTCERT_ERROR:
833 if ($cgiparams{'ACTION'} ne '') {
834 &Header::showhttpheaders();
835 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
836 &Header::openbigbox('100%', 'LEFT', '', '');
837 if ($errormessage) {
838 &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
839 print "<class name='base'>$errormessage";
840 print "&nbsp;</class>";
841 &Header::closebox();
842 }
843 &Header::openbox('100%', 'LEFT', "$Lang::tr{'generate root/host certificates'}:");
844 print <<END
845 <form method='post' enctype='multipart/form-data'>
846 <table width='100%' border='0' cellspacing='1' cellpadding='0'>
847 <tr><td width='30%' class='base'>$Lang::tr{'organization name'}:</td>
848 <td width='35%' class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_ORGANIZATION' value='$cgiparams{'ROOTCERT_ORGANIZATION'}' size='32' /></td>
849 <td width='35%' colspan='2'>&nbsp;</td></tr>
850 <tr><td class='base'>$Lang::tr{'ipfires hostname'}:</td>
851 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_HOSTNAME' value='$cgiparams{'ROOTCERT_HOSTNAME'}' size='32' /></td>
852 <td colspan='2'>&nbsp;</td></tr>
853 <tr><td class='base'>$Lang::tr{'your e-mail'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
854 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_EMAIL' value='$cgiparams{'ROOTCERT_EMAIL'}' size='32' /></td>
855 <td colspan='2'>&nbsp;</td></tr>
856 <tr><td class='base'>$Lang::tr{'your department'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
857 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_OU' value='$cgiparams{'ROOTCERT_OU'}' size='32' /></td>
858 <td colspan='2'>&nbsp;</td></tr>
859 <tr><td class='base'>$Lang::tr{'city'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
860 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_CITY' value='$cgiparams{'ROOTCERT_CITY'}' size='32' /></td>
861 <td colspan='2'>&nbsp;</td></tr>
862 <tr><td class='base'>$Lang::tr{'state or province'}:&nbsp;<img src='/blob.gif' alt'*' /></td>
863 <td class='base' nowrap='nowrap'><input type='text' name='ROOTCERT_STATE' value='$cgiparams{'ROOTCERT_STATE'}' size='32' /></td>
864 <td colspan='2'>&nbsp;</td></tr>
865 <tr><td class='base'>$Lang::tr{'country'}:</td>
866 <td class='base'><select name='ROOTCERT_COUNTRY'>
867
868 END
869 ;
870 foreach my $country (sort keys %{Countries::countries}) {
871 print "<option value='$Countries::countries{$country}'";
872 if ( $Countries::countries{$country} eq $cgiparams{'ROOTCERT_COUNTRY'} ) {
873 print " selected='selected'";
874 }
875 print ">$country</option>";
876 }
877 print <<END
878 </select></td>
879 <td colspan='2'>&nbsp;</td></tr>
880 <tr><td>&nbsp;</td>
881 <td><input type='submit' name='ACTION' value='$Lang::tr{'generate root/host certificates'}' /></td>
882 <td>&nbsp;</td><td>&nbsp;</td></tr>
883 <tr><td class='base' colspan='4' align='left'>
884 <img src='/blob.gif' valign='top' alt='*' />&nbsp;$Lang::tr{'this field may be blank'}</td></tr>
885 <tr><td class='base' colspan='4' align='left'>
886 <b><font color='${Header::colourred}'>$Lang::tr{'capswarning'}</font></b>:
887 $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'}
888 </td></tr>
889 <tr><td colspan='4' bgcolor='#000000'><img src='/images/null.gif' width='1' height='1' border='0' /></td></tr>
890 <tr><td class='base' nowrap='nowrap'>$Lang::tr{'upload p12 file'}:</td>
891 <td nowrap='nowrap'><input type='file' name='FH' size='32'></td>
892 <td colspan='2'>&nbsp;</td></tr>
893 <tr><td class='base'>$Lang::tr{'pkcs12 file password'}:&nbsp;<img src='/blob.gif' alt='*' ></td>
894 <td class='base' nowrap='nowrap'><input type='password' name='P12_PASS' value='$cgiparams{'P12_PASS'}' size='32' /></td>
895 <td colspan='2'>&nbsp;</td></tr>
896 <tr><td>&nbsp;</td>
897 <td><input type='submit' name='ACTION' value='$Lang::tr{'upload p12 file'}' /></td>
898 <td colspan='2'>&nbsp;</td></tr>
899 <tr><td class='base' colspan='4' align='left'>
900 <img src='/blob.gif' valign='top' al='*' >&nbsp;$Lang::tr{'this field may be blank'}</td></tr>
901 </form></table>
902 END
903 ;
904 &Header::closebox();
905
906 &Header::closebigbox();
907 &Header::closepage();
908 exit(0)
909 }
910
911 ROOTCERT_SUCCESS:
912 system ("chmod 600 ${General::swroot}/ovpn/certs/serverkey.pem");
913
914 ###
915 ### Enable/Disable connection
916 ###
917 }elsif ($cgiparams{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
918 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
919 if ($confighash{$cgiparams{'KEY'}}) {
920 my $n2nactive = `/bin/ps ax|grep $confighash{$cgiparams{'KEY'}}[1].conf|grep -v grep|awk \'{print \$1}\'`;
921 if ($confighash{$cgiparams{'KEY'}}[0] eq 'off') {
922 $confighash{$cgiparams{'KEY'}}[0] = 'on';
923 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
924 if ($n2nactive eq ''){
925 system('/usr/local/bin/openvpnctrl', '-sn2n', $confighash{$cgiparams{'KEY'}}[1]);
926 } else {
927 system('/usr/local/bin/openvpnctrl', '-kn2n', $n2nactive);
928 system('/usr/local/bin/openvpnctrl', '-sn2n', $confighash{$cgiparams{'KEY'}}[1]);
929 }
930 } else {
931 $confighash{$cgiparams{'KEY'}}[0] = 'off';
932 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
933 if ($n2nactive ne ''){
934 system('/usr/local/bin/openvpnctrl', '-kn2n', $n2nactive);
935 }
936 }
937 } else {
938 $errormessage = $Lang::tr{'invalid key'};
939 }
940
941 ###
942 ### Download OpenVPN client package
943 ###
944 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'dl client arch'}) {
945 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
946 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
947 my $file = '';
948 my $clientovpn = '';
949 my @fileholder;
950 my $tempdir = tempdir( CLEANUP => 1 );
951 my $zippath = "$tempdir/";
952 my $zipname = "$confighash{$cgiparams{'KEY'}}[1]-TO-IPFire.zip";
953 my $zippathname = "$zippath$zipname";
954 #anna
955 if ($confighash{$cgiparams{'KEY'}}[3] eq 'net'){
956 $zerinaclient = 'true';
957 &Ovpnfunc::writenet2netconf($cgiparams{'KEY'},$zerinaclient);
958 exit(0);
959 }
960 $clientovpn = "$confighash{$cgiparams{'KEY'}}[1]-TO-IPFire.ovpn";
961 open(CLIENTCONF, ">$tempdir/$clientovpn") or die "Unable to open tempfile: $!";
962 flock CLIENTCONF, 2;
963
964 my $zip = Archive::Zip->new();
965
966 print CLIENTCONF "#OpenVPN Server conf\r\n";
967 print CLIENTCONF "tls-client\r\n";
968 print CLIENTCONF "client\r\n";
969 print CLIENTCONF "dev $vpnsettings{'DDEVICE'}\r\n";
970 print CLIENTCONF "proto $vpnsettings{'DPROTOCOL'}\r\n";
971 print CLIENTCONF "$vpnsettings{'DDEVICE'}-mtu $vpnsettings{'DMTU'}\r\n";
972 if ( $vpnsettings{'ENABLED'} eq 'on'){
973 print CLIENTCONF "remote $vpnsettings{'VPN_IP'} $vpnsettings{'DDEST_PORT'}\r\n";
974 if ( $vpnsettings{'ENABLED_BLUE'} eq 'on' && (&Ovpnfunc::haveBlueNet())){
975 print CLIENTCONF "#Coment the above line and uncoment the next line, if you want to connect on the Blue interface\r\n";
976 print CLIENTCONF ";remote $netsettings{'BLUE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
977 }
978 if ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' && (&Ovpnfunc::haveOrangeNet())){
979 print CLIENTCONF "#Coment the above line and uncoment the next line, if you want to connect on the Orange interface\r\n";
980 print CLIENTCONF ";remote $netsettings{'ORANGE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
981 }
982 } elsif ( $vpnsettings{'ENABLED_BLUE'} eq 'on' && (&Ovpnfunc::haveBlueNet())){
983 print CLIENTCONF "remote $netsettings{'BLUE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
984 if ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' && (&Ovpnfunc::haveOrangeNet())){
985 print CLIENTCONF "#Coment the above line and uncoment the next line, if you want to connect on the Orange interface\r\n";
986 print CLIENTCONF ";remote $netsettings{'ORANGE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
987 }
988 } elsif ( $vpnsettings{'ENABLED_ORANGE'} eq 'on' && (&Ovpnfunc::haveOrangeNet())){
989 print CLIENTCONF "remote $netsettings{'ORANGE_ADDRESS'} $vpnsettings{'DDEST_PORT'}\r\n";
990 }
991
992 if ($confighash{$cgiparams{'KEY'}}[4] eq 'cert' && -f "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12") {
993 print CLIENTCONF "pkcs12 $confighash{$cgiparams{'KEY'}}[1].p12\r\n";
994 $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";
995 } else {
996 print CLIENTCONF "ca cacert.pem\r\n";
997 print CLIENTCONF "cert $confighash{$cgiparams{'KEY'}}[1]cert.pem\r\n";
998 print CLIENTCONF "key $confighash{$cgiparams{'KEY'}}[1].key\r\n";
999 $zip->addFile( "${General::swroot}/ovpn/ca/cacert.pem", "cacert.pem") or die "Can't add file cacert.pem\n";
1000 $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";
1001 }
1002 print CLIENTCONF "cipher $vpnsettings{DCIPHER}\r\n";
1003 if ($vpnsettings{DCOMPLZO} eq 'on') {
1004 print CLIENTCONF "comp-lzo\r\n";
1005 }
1006 print CLIENTCONF "verb 3\r\n";
1007 print CLIENTCONF "ns-cert-type server\r\n";
1008 close(CLIENTCONF);
1009 $zip->addFile( "$tempdir/$clientovpn", $clientovpn) or die "Can't add file $clientovpn\n";
1010 my $status = $zip->writeToFileNamed($zippathname);
1011
1012 open(DLFILE, "<$zippathname") or die "Unable to open $zippathname: $!";
1013 @fileholder = <DLFILE>;
1014 print "Content-Type:application/x-download\n";
1015 print "Content-Disposition:attachment;filename=$zipname\n\n";
1016 print @fileholder;
1017 exit (0);
1018
1019 ###
1020 ### Remove connection
1021 ###
1022 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'remove'}) {
1023 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1024 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1025 if ($confighash{$cgiparams{'KEY'}}) {
1026 if ($confighash{$cgiparams{'KEY'}}[19] eq 'yes') {
1027 &Ovpnfunc::killconnection($cgiparams{'KEY'});
1028 &Ovpnfunc::removenet2netconf($cgiparams{'KEY'});
1029 delete $confighash{$cgiparams{'KEY'}};
1030 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1031 } else {
1032 my $temp = `/usr/bin/openssl ca -revoke ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem -config ${General::swroot}/ovpn/openssl/ovpn.cnf`;
1033 unlink ("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem");
1034 unlink ("${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12");
1035 &Ovpnfunc::killconnection($cgiparams{'KEY'});
1036 &Ovpnfunc::removenet2netconf($cgiparams{'KEY'});
1037 delete $confighash{$cgiparams{'KEY'}};
1038 my $temp2 = `/usr/bin/openssl ca -gencrl -out ${General::swroot}/ovpn/crls/cacrl.pem -config ${General::swroot}/ovpn/openssl/ovpn.cnf`;
1039 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1040 }
1041 } else {
1042 $errormessage = $Lang::tr{'invalid key'};
1043 }
1044 ###
1045 ### Download PKCS12 file
1046 ###
1047 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download pkcs12 file'}) {
1048 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1049
1050 print "Content-Disposition: filename=" . $confighash{$cgiparams{'KEY'}}[1] . ".p12\r\n";
1051 print "Content-Type: application/octet-stream\r\n\r\n";
1052 print `/bin/cat ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1].p12`;
1053 exit (0);
1054
1055 ###
1056 ### Display certificate
1057 ###
1058 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show certificate'}) {
1059 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1060
1061 if ( -f "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem") {
1062 &Header::showhttpheaders();
1063 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1064 &Header::openbigbox('100%', 'LEFT', '', '');
1065 &Header::openbox('100%', 'LEFT', "$Lang::tr{'certificate'}:");
1066 my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
1067 $output = &Header::cleanhtml($output,"y");
1068 print "<pre>$output</pre>\n";
1069 &Header::closebox();
1070 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1071 &Header::closebigbox();
1072 &Header::closepage();
1073 exit(0);
1074 }
1075 ###
1076 ### Display Certificate Revoke List
1077 ###
1078 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show crl'}) {
1079 if ( -f "${General::swroot}/ovpn/crls/cacrl.pem") {
1080 &Header::showhttpheaders();
1081 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1082 &Header::openbigbox('100%', 'LEFT', '', '');
1083 &Header::openbox('100%', 'LEFT', "$Lang::tr{'crl'}:");
1084 my $output = `/usr/bin/openssl crl -text -noout -in ${General::swroot}/ovpn/crls/cacrl.pem`;
1085 $output = &Header::cleanhtml($output,"y");
1086 print "<pre>$output</pre>\n";
1087 &Header::closebox();
1088 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1089 &Header::closebigbox();
1090 &Header::closepage();
1091 exit(0);
1092 }
1093
1094 ###
1095 ### Advanced Server Settings
1096 ###
1097
1098 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'advanced server'}) {
1099 %cgiparams = ();
1100 %cahash = ();
1101 %confighash = ();
1102 &General::readhash("${General::swroot}/ovpn/settings", \%cgiparams);
1103
1104 ADV_ERROR:
1105 if ($cgiparams{'MAX_CLIENTS'} eq '') {
1106 $cgiparams{'MAX_CLIENTS'} = '100';
1107 }
1108
1109 if ($cgiparams{'KEEPALIVE_1'} eq '') {
1110 $cgiparams{'KEEPALIVE_1'} = '10';
1111 }
1112 if ($cgiparams{'KEEPALIVE_2'} eq '') {
1113 $cgiparams{'KEEPALIVE_2'} = '60';
1114 }
1115 if ($cgiparams{'LOG_VERB'} eq '') {
1116 $cgiparams{'LOG_VERB'} = '3';
1117 }
1118 if ($cgiparams{'EXTENDED_NICE'} eq '') {
1119 $cgiparams{'EXTENDED_NICE'} = '0';
1120 }
1121 $checked{'CLIENT2CLIENT'}{'off'} = '';
1122 $checked{'CLIENT2CLIENT'}{'on'} = '';
1123 $checked{'CLIENT2CLIENT'}{$cgiparams{'CLIENT2CLIENT'}} = 'CHECKED';
1124 $checked{'REDIRECT_GW_DEF1'}{'off'} = '';
1125 $checked{'REDIRECT_GW_DEF1'}{'on'} = '';
1126 $checked{'REDIRECT_GW_DEF1'}{$cgiparams{'REDIRECT_GW_DEF1'}} = 'CHECKED';
1127 $selected{'LOG_VERB'}{'1'} = '';
1128 $selected{'LOG_VERB'}{'2'} = '';
1129 $selected{'LOG_VERB'}{'3'} = '';
1130 $selected{'LOG_VERB'}{'4'} = '';
1131 $selected{'LOG_VERB'}{'5'} = '';
1132 $selected{'LOG_VERB'}{'6'} = '';
1133 $selected{'LOG_VERB'}{'7'} = '';
1134 $selected{'LOG_VERB'}{'8'} = '';
1135 $selected{'LOG_VERB'}{'9'} = '';
1136 $selected{'LOG_VERB'}{'10'} = '';
1137 $selected{'LOG_VERB'}{'11'} = '';
1138 $selected{'LOG_VERB'}{'0'} = '';
1139 $selected{'LOG_VERB'}{$cgiparams{'LOG_VERB'}} = 'SELECTED';
1140
1141 #################################################################################
1142 # Added by Philipp Jenni #
1143 # #
1144 # Contact: philipp.jenni-at-gmx.ch #
1145 # Date: 2006-04-22 #
1146 # Description: Definitions to set the FASTIO Checkbox #
1147 # Definitions to set the MTUDISC Checkbox #
1148 # Definitions to set the NICE Selectionbox #
1149 #################################################################################
1150 $checked{'EXTENDED_FASTIO'}{'off'} = '';
1151 $checked{'EXTENDED_FASTIO'}{'on'} = '';
1152 $checked{'EXTENDED_FASTIO'}{$cgiparams{'EXTENDED_FASTIO'}} = 'CHECKED';
1153 $checked{'EXTENDED_MTUDISC'}{'off'} = '';
1154 $checked{'EXTENDED_MTUDISC'}{'on'} = '';
1155 $checked{'EXTENDED_MTUDISC'}{$cgiparams{'EXTENDED_MTUDISC'}} = 'CHECKED';
1156 $selected{'EXTENDED_NICE'}{'-13'} = '';
1157 $selected{'EXTENDED_NICE'}{'-10'} = '';
1158 $selected{'EXTENDED_NICE'}{'-7'} = '';
1159 $selected{'EXTENDED_NICE'}{'-3'} = '';
1160 $selected{'EXTENDED_NICE'}{'0'} = '';
1161 $selected{'EXTENDED_NICE'}{'3'} = '';
1162 $selected{'EXTENDED_NICE'}{'7'} = '';
1163 $selected{'EXTENDED_NICE'}{'10'} = '';
1164 $selected{'EXTENDED_NICE'}{'13'} = '';
1165 $selected{'EXTENDED_NICE'}{$cgiparams{'EXTENDED_NICE'}} = 'SELECTED';
1166 #################################################################################
1167 # End of inserted Data #
1168 #################################################################################
1169
1170 &Header::showhttpheaders();
1171 &Header::openpage($Lang::tr{'status ovpn'}, 1, '');
1172 &Header::openbigbox('100%', 'LEFT', '', $errormessage);
1173 if ($errormessage) {
1174 &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
1175 print "<class name='base'>$errormessage\n";
1176 print "&nbsp;</class>\n";
1177 &Header::closebox();
1178 }
1179 &Header::openbox('100%', 'LEFT', $Lang::tr{'advanced server'});
1180 print <<END
1181 <form method='post' enctype='multipart/form-data'>
1182 <table width='100%'>
1183 <tr>
1184 <td colspan='4'><b>$Lang::tr{'dhcp-options'}</b></td>
1185 </tr>
1186 <tr>
1187 <td width='25%'></td> <td width='20%'> </td><td width='25%'> </td><td width='30%'></td>
1188 </tr>
1189 <tr>
1190 <td class='base'>Domain</td>
1191 <td><input type='TEXT' name='DHCP_DOMAIN' value='$cgiparams{'DHCP_DOMAIN'}' size='30' /></td>
1192 </tr>
1193 <tr>
1194 <td class='base'>DNS</td>
1195 <td><input type='TEXT' name='DHCP_DNS' value='$cgiparams{'DHCP_DNS'}' size='30' /></td>
1196 </tr>
1197 <tr>
1198 <td class='base'>WINS</td>
1199 <td><input type='TEXT' name='DHCP_WINS' value='$cgiparams{'DHCP_WINS'}' size='30' /></td>
1200 </tr>
1201 </table>
1202 <hr size='1'>
1203 <!-- Additional push route START-->
1204 <table width='100%'>
1205 <tr>
1206 <td colspan='4'><b>$Lang::tr{'add-route'}</b></td>
1207 </tr>
1208 <tr>
1209 <td width='25%'></td> <td width='20%'> </td><td width='25%'> </td><td width='30%'></td>
1210 </tr>
1211 <tr>
1212 <td class='base'>$Lang::tr{'subnet'} 1</td>
1213 <td><input type='TEXT' name='AD_ROUTE1' value='$cgiparams{'AD_ROUTE1'}' size='30' /></td>
1214 </tr>
1215 <tr>
1216 <td class='base'>$Lang::tr{'subnet'} 2</td>
1217 <td><input type='TEXT' name='AD_ROUTE2' value='$cgiparams{'AD_ROUTE2'}' size='30' /></td>
1218 </tr>
1219 <tr>
1220 <td class='base'>$Lang::tr{'subnet'} 3</td>
1221 <td><input type='TEXT' name='AD_ROUTE3' value='$cgiparams{'AD_ROUTE3'}' size='30' /></td>
1222 </tr>
1223 </table>
1224 <hr size='1'>
1225 <!-- Additional push route END-->
1226 <table width='100%'>
1227 <tr>
1228 <td class'base'><b>$Lang::tr{'misc-options'}</b></td>
1229 </tr>
1230 <tr>
1231 <td width='25%'></td> <td width='20%'> </td><td width='25%'> </td><td width='30%'></td>
1232 </tr>
1233 <tr>
1234 <td class='base'>Client-To-Client</td>
1235 <td><input type='checkbox' name='CLIENT2CLIENT' $checked{'CLIENT2CLIENT'}{'on'} /></td>
1236 </tr>
1237 <tr>
1238 <td class='base'>Redirect-Gateway def1</td>
1239 <td><input type='checkbox' name='REDIRECT_GW_DEF1' $checked{'REDIRECT_GW_DEF1'}{'on'} /></td>
1240 </tr>
1241 <tr>
1242 <td class='base'>Max-Clients</td>
1243 <td><input type='text' name='MAX_CLIENTS' value='$cgiparams{'MAX_CLIENTS'}' size='30' /></td>
1244 </tr>
1245 <td class='base'>Keppalive (ping/ping-restart)</td>
1246 <td><input type='TEXT' name='KEEPALIVE_1' value='$cgiparams{'KEEPALIVE_1'}' size='30' /></td>
1247 <td><input type='TEXT' name='KEEPALIVE_2' value='$cgiparams{'KEEPALIVE_2'}' size='30' /></td>
1248 </tr>
1249
1250 <!--
1251 #################################################################################
1252 # Added by Philipp Jenni #
1253 # #
1254 # Contact: philipp.jenni-at-gmx.ch #
1255 # Date: 2006-04-22 #
1256 # Description: Add the FAST-IO Checkbox to the HTML Form #
1257 # Add the NICE Selectionbox to the HTML Form #
1258 # Add the MTU-DISC Checkbox to the HTML Form #
1259 # Add the MSSFIX Textbox to the HTML Form #
1260 # Add the FRAMGMENT Textbox to the HTML Form #
1261 # Updates: #
1262 # 2006-04-27 Include Multilanguage-Support #
1263 #################################################################################
1264 -->
1265 </tr>
1266 <tr>
1267 <td class='base'>$Lang::tr{'ovpn_processprio'}</td>
1268 <td>
1269 <select name='EXTENDED_NICE'>
1270 <option value='-13' $selected{'EXTENDED_NICE'}{'-13'}>$Lang::tr{'ovpn_processprioEH'}</option>
1271 <option value='-10' $selected{'EXTENDED_NICE'}{'-10'}>$Lang::tr{'ovpn_processprioVH'}</option>
1272 <option value='-7' $selected{'EXTENDED_NICE'}{'-7'}>$Lang::tr{'ovpn_processprioH'}</option>
1273 <option value='-3' $selected{'EXTENDED_NICE'}{'-3'}>$Lang::tr{'ovpn_processprioEN'}</option>
1274 <option value='0' $selected{'EXTENDED_NICE'}{'0'}>$Lang::tr{'ovpn_processprioN'}</option>
1275 <option value='3' $selected{'EXTENDED_NICE'}{'3'}>$Lang::tr{'ovpn_processprioLN'}</option>
1276 <option value='7' $selected{'EXTENDED_NICE'}{'7'}>$Lang::tr{'ovpn_processprioD'}</option>
1277 <option value='10' $selected{'EXTENDED_NICE'}{'10'}>$Lang::tr{'ovpn_processprioVD'}</option>
1278 <option value='13' $selected{'EXTENDED_NICE'}{'13'}>$Lang::tr{'ovpn_processprioED'}</option>
1279 </select>
1280 </td>
1281 </tr>
1282 <tr>
1283 <td class='base'>$Lang::tr{'ovpn_fastio'}</td>
1284 <td>
1285 <input type='checkbox' name='EXTENDED_FASTIO' $checked{'EXTENDED_FASTIO'}{'on'} />
1286 </td>
1287 </tr>
1288 <tr>
1289 <td class='base'>$Lang::tr{'ovpn_mtudisc'}</td>
1290 <td>
1291 <input type='checkbox' name='EXTENDED_MTUDISC' $checked{'EXTENDED_MTUDISC'}{'on'} />
1292 </td>
1293 </tr>
1294 <tr>
1295 <td class='base'>$Lang::tr{'ovpn_mssfix'}</td>
1296 <td>
1297 <input type='TEXT' name='EXTENDED_MSSFIX' value='$cgiparams{'EXTENDED_MSSFIX'}' size='30'/>
1298 </td>
1299 </tr>
1300 <tr>
1301 <td class='base'>$Lang::tr{'ovpn_fragment'}</td>
1302 <td>
1303 <input type='TEXT' name='EXTENDED_FRAGMENT' value='$cgiparams{'EXTENDED_FRAGMENT'}' size='30'/>
1304 </td>
1305 </tr>
1306
1307 <!--
1308 #################################################################################
1309 # End of Inserted Data #
1310 #################################################################################
1311 -->
1312
1313
1314 </table>
1315 <hr size='1'>
1316 <table width='100%'>
1317 <tr>
1318 <td class'base'><b>$Lang::tr{'log-options'}</b></td>
1319 </tr>
1320 <tr>
1321 <td width='25%'></td> <td width='20%'> </td><td width='25%'> </td><td width='30%'></td>
1322 </tr>
1323
1324 <tr><td class='base'>VERB</td>
1325 <td><select name='LOG_VERB'><option value='1' $selected{'LOG_VERB'}{'1'}>1</option>
1326 <option value='2' $selected{'LOG_VERB'}{'2'}>2</option>
1327 <option value='3' $selected{'LOG_VERB'}{'3'}>3</option>
1328 <option value='4' $selected{'LOG_VERB'}{'4'}>4</option>
1329 <option value='5' $selected{'LOG_VERB'}{'5'}>5</option>
1330 <option value='6' $selected{'LOG_VERB'}{'6'}>6</option>
1331 <option value='7' $selected{'LOG_VERB'}{'7'}>7</option>
1332 <option value='8' $selected{'LOG_VERB'}{'8'}>8</option>
1333 <option value='9' $selected{'LOG_VERB'}{'9'}>9</option>
1334 <option value='10' $selected{'LOG_VERB'}{'10'}>10</option>
1335 <option value='11' $selected{'LOG_VERB'}{'11'}>11</option>
1336 <option value='0' $selected{'LOG_VERB'}{'0'}>0</option></select></td>
1337 <!--
1338 #################################################################################
1339 # Added by Philipp Jenni #
1340 # #
1341 # Contact: philipp.jenni-at-gmx.ch #
1342 # Date: 2006-04-22 #
1343 # Description: Required </TR> Command from this Table #
1344 #################################################################################
1345 -->
1346 </tr>
1347 <!--
1348 #################################################################################
1349 # End of Inserted Data #
1350 #################################################################################
1351 -->
1352
1353 </table>
1354 <hr size='1'>
1355 <table width='100%'>
1356 <tr>
1357 <td>&nbsp;</td>
1358 <td allign='center'><input type='submit' name='ACTION' value='$Lang::tr{'save-adv-options'}' /></td>
1359 <td allign='center'><input type='submit' name='ACTION' value='$Lang::tr{'cancel-adv-options'}' /></td>
1360 <td>&nbsp;</td>
1361 </tr>
1362 </table>
1363 </form>
1364 END
1365 ;
1366
1367 &Header::closebox();
1368 &Header::closebigbox();
1369 &Header::closepage();
1370 exit(0);
1371
1372 ###
1373 ### Openvpn Connections Statistics
1374 ###
1375 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'ovpn con stat'}) {
1376 &Header::showhttpheaders();
1377 &Header::openpage($Lang::tr{'ovpn con stat'}, 1, '');
1378 &Header::openbigbox('100%', 'LEFT', '', '');
1379 &Header::openbox('100%', 'LEFT', $Lang::tr{'ovpn con stat'});
1380
1381 #
1382 # <td><b>$Lang::tr{'protocol'}</b></td>
1383 # protocol temp removed
1384 print <<END
1385 <table width='100%' border='0' cellpadding='2' cellspacing='0'>
1386 <tr>
1387 <td><b>$Lang::tr{'common name'}</b></td>
1388 <td><b>$Lang::tr{'real address'}</b></td>
1389 <td><b>$Lang::tr{'virtual address'}</b></td>
1390 <td><b>$Lang::tr{'loged in at'}</b></td>
1391 <td><b>$Lang::tr{'bytes sent'}</b></td>
1392 <td><b>$Lang::tr{'bytes received'}</b></td>
1393 <td><b>$Lang::tr{'last activity'}</b></td>
1394 </tr>
1395 END
1396 ;
1397 my $filename = "/var/log/ovpnserver.log";
1398 open(FILE, $filename) or die 'Unable to open config file.';
1399 my @current = <FILE>;
1400 close(FILE);
1401 my @users =();
1402 my $status;
1403 my $uid = 0;
1404 my $cn;
1405 my @match = ();
1406 my $proto = "udp";
1407 my $address;
1408 my %userlookup = ();
1409 foreach my $line (@current)
1410 {
1411 chomp($line);
1412 if ( $line =~ /^Updated,(.+)/){
1413 @match = split( /^Updated,(.+)/, $line);
1414 $status = $match[1];
1415 }
1416 if ( $line =~ /^(.+),(\d+\.\d+\.\d+\.\d+\:\d+),(\d+),(\d+),(.+)/) {
1417 @match = split(m/^(.+),(\d+\.\d+\.\d+\.\d+\:\d+),(\d+),(\d+),(.+)/, $line);
1418 if ($match[1] ne "Common Name") {
1419 $cn = $match[1];
1420 $userlookup{$match[2]} = $uid;
1421 $users[$uid]{'CommonName'} = $match[1];
1422 $users[$uid]{'RealAddress'} = $match[2];
1423 $users[$uid]{'BytesReceived'} = &Ovpnfunc::sizeformat($match[3]);
1424 $users[$uid]{'BytesSent'} = &Ovpnfunc::sizeformat($match[4]);
1425 $users[$uid]{'Since'} = $match[5];
1426 $users[$uid]{'Proto'} = $proto;
1427 $uid++;
1428 }
1429 }
1430 if ( $line =~ /^(\d+\.\d+\.\d+\.\d+),(.+),(\d+\.\d+\.\d+\.\d+\:\d+),(.+)/) {
1431 @match = split(m/^(\d+\.\d+\.\d+\.\d+),(.+),(\d+\.\d+\.\d+\.\d+\:\d+),(.+)/, $line);
1432 if ($match[1] ne "Virtual Address") {
1433 $address = $match[3];
1434 #find the uid in the lookup table
1435 $uid = $userlookup{$address};
1436 $users[$uid]{'VirtualAddress'} = $match[1];
1437 $users[$uid]{'LastRef'} = $match[4];
1438 }
1439 }
1440 }
1441 my $user2 = @users;
1442 if ($user2 >= 1){
1443 for (my $idx = 1; $idx <= $user2; $idx++){
1444 if ($idx % 2) {
1445 print "<tr bgcolor='${Header::table1colour}'>\n";
1446 } else {
1447 print "<tr bgcolor='${Header::table2colour}'>\n";
1448 }
1449 print "<td align='left'>$users[$idx-1]{'CommonName'}</td>";
1450 print "<td align='left'>$users[$idx-1]{'RealAddress'}</td>";
1451 print "<td align='left'>$users[$idx-1]{'VirtualAddress'}</td>";
1452 print "<td align='left'>$users[$idx-1]{'Since'}</td>";
1453 print "<td align='left'>$users[$idx-1]{'BytesSent'}</td>";
1454 print "<td align='left'>$users[$idx-1]{'BytesReceived'}</td>";
1455 print "<td align='left'>$users[$idx-1]{'LastRef'}</td>";
1456 # print "<td align='left'>$users[$idx-1]{'Proto'}</td>";
1457 }
1458 }
1459
1460 print "</table>";
1461 print <<END
1462 <table width='100%' border='0' cellpadding='2' cellspacing='0'>
1463 <tr><td></td></tr>
1464 <tr><td></td></tr>
1465 <tr><td></td></tr>
1466 <tr><td></td></tr>
1467 <tr><td align='center' >$Lang::tr{'the statistics were last updated at'} <b>$status</b></td></tr>
1468 </table>
1469 END
1470 ;
1471 &Header::closebox();
1472 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1473 &Header::closebigbox();
1474 &Header::closepage();
1475 exit(0);
1476
1477 ###
1478 ### Download Certificate
1479 ###
1480 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download certificate'}) {
1481 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1482 if ( -f "${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem") {
1483 print "Content-Disposition: filename=" . $confighash{$cgiparams{'KEY'}}[1] . "cert.pem\r\n";
1484 print "Content-Type: application/octet-stream\r\n\r\n";
1485 print `/bin/cat ${General::swroot}/ovpn/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
1486 exit (0);
1487 }
1488
1489 ###
1490 ### Restart connection
1491 ###
1492 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'restart'}) {
1493 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1494 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1495
1496 if ($confighash{$cgiparams{'KEY'}}) {
1497 } else {
1498 $errormessage = $Lang::tr{'invalid key'};
1499 }
1500
1501 ###
1502 ### Choose between adding a host-net or net-net connection
1503 ###
1504 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'add'} && $cgiparams{'TYPE'} eq '') {
1505 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1506 &Header::showhttpheaders();
1507 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
1508 &Header::openbigbox('100%', 'LEFT', '', '');
1509 &Header::openbox('100%', 'LEFT', $Lang::tr{'connection type'});
1510 print <<END
1511 <b>$Lang::tr{'connection type'}:</b><br />
1512 <table><form method='post' enctype='multipart/form-data'>
1513 <tr><td><input type='radio' name='TYPE' value='host' checked /></td>
1514 <td class='base'>$Lang::tr{'host to net vpn'}</td></tr>
1515 <tr><td><input type='radio' name='TYPE' value='net' /></td>
1516 <td class='base'>$Lang::tr{'net to net vpn'}</td></tr>
1517 <tr><td><input type='radio' name='TYPE' value='zerinan2n' /></td>
1518 <td class='base'>upload a ZERINA Net-to-Net package</td>
1519 <td class='base'><input type='file' name='FH' size='30'></td></tr>
1520 <tr><td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'add'}' /></td></tr>
1521 </form></table>
1522 END
1523 ;
1524 &Header::closebox();
1525 &Header::closebigbox();
1526 &Header::closepage();
1527 exit (0);
1528
1529 ###
1530 ### uploading a ZERINA n2n connection package
1531 ###
1532 } elsif (($cgiparams{'ACTION'} eq $Lang::tr{'add'}) && ($cgiparams{'TYPE'} eq 'zerinan2n')){
1533 my @zerinaconf;
1534 my @confdetails;
1535 my $uplconffilename ='';
1536 my $uplp12name = '';
1537 my $complzoactive ='';
1538 my @rem_subnet;
1539 my @rem_subnet2;
1540 my @tmposupnet3;
1541 my $key;
1542 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1543 # Move uploaded ZERINA n2n package to a temporary file
1544 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1545 $errormessage = $Lang::tr{'there was no file upload'};
1546 goto ZERINA_ERROR;
1547 }
1548 # Move uploaded ca to a temporary file
1549 (my $fh, my $filename) = tempfile( );
1550 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1551 $errormessage = $!;
1552 goto ZERINA_ERROR;
1553 }
1554
1555 my $zip = Archive::Zip->new();
1556 my $zipName = $filename;
1557 my $status = $zip->read( $zipName );
1558 if ($status != AZ_OK) {
1559 $errormessage = "Read of $zipName failed\n";
1560 goto ZERINA_ERROR;
1561 }
1562 #my $tempdir = tempdir( CLEANUP => 1 );
1563 my $tempdir = tempdir();
1564 my @files = $zip->memberNames();
1565 for(@files) {
1566 $zip->extractMemberWithoutPaths($_,"$tempdir/$_");
1567 }
1568 my $countfiles = @files;
1569 # see if we have 2 files
1570 if ( $countfiles == 2){
1571 foreach (@files){
1572 if ( $_ =~ /.conf$/){
1573 $uplconffilename = $_;
1574 }
1575 if ( $_ =~ /.p12$/){
1576 $uplp12name = $_;
1577 }
1578 }
1579 if (($uplconffilename eq '') || ($uplp12name eq '')){
1580 $errormessage = "Either no *.conf or no *.p12 file found\n";
1581 goto ZERINA_ERROR;
1582 }
1583 open(FILE, "$tempdir/$uplconffilename") or die 'Unable to open*.conf file';
1584 @zerinaconf = <FILE>;
1585 close (FILE);
1586 chomp(@zerinaconf);
1587 } else {
1588 # only 2 files are allowed
1589 $errormessage = "Filecount does not match only 2 files are allowed\n";
1590 goto ZERINA_ERROR;
1591 }
1592 #prepare imported data not elegant, will be changed later
1593 my $ufuk = (@zerinaconf);
1594 push(@confdetails, substr($zerinaconf[0],4));#dev tun 0
1595 push(@confdetails, substr($zerinaconf[1],8));#mtu value 1
1596 push(@confdetails, substr($zerinaconf[2],6));#protocol 2
1597 push(@confdetails, substr($zerinaconf[3],5));#port 3
1598 push(@confdetails, substr($zerinaconf[4],9));#ovpn subnet 4
1599 push(@confdetails, substr($zerinaconf[5],7));#remote ip 5
1600 push(@confdetails, $zerinaconf[6]); #tls-server/tls-client 6
1601 push(@confdetails, substr($zerinaconf[7],7));#pkcs12 name 7
1602 push(@confdetails, substr($zerinaconf[$ufuk-1],1));#remote subnet 8
1603 push(@confdetails, substr($zerinaconf[9],10));#keepalive 9
1604 push(@confdetails, substr($zerinaconf[10],7));#cipher 10
1605 if ($ufuk == 14) {
1606 push(@confdetails, $zerinaconf[$ufuk-3]);#complzo 11
1607 $complzoactive = "on";
1608 } else {
1609 $complzoactive = "off";
1610 }
1611 push(@confdetails, substr($zerinaconf[$ufuk-2],5));#verb 12
1612 push(@confdetails, substr($zerinaconf[8],6));#localsubnet 13
1613 #push(@confdetails, substr($uplconffilename,0,-5));#connection Name 14
1614 push(@confdetails, substr($uplp12name,0,-4));#connection Name 14
1615 #chomp(@confdetails);
1616 foreach my $dkey (keys %confighash) {#Check if there is no other entry with this name
1617 if ($confighash{$dkey}[1] eq $confdetails[$ufuk]) {
1618 $errormessage = $Lang::tr{'a connection with this name already exists'};
1619 goto ZERINA_ERROR;
1620 }
1621 }
1622 if ($confdetails[$ufuk] eq 'server') {
1623 $errormessage = $Lang::tr{'server reserved'};
1624 goto ZERINA_ERROR;
1625 }
1626 @rem_subnet2 = split(/ /,$confdetails[4]);
1627 @tmposupnet3 = split /\./,$rem_subnet2[0];
1628 $errormessage = &Ovpnfunc::ovelapplausi("$tmposupnet3[0].$tmposupnet3[1].$tmposupnet3[2].0","255.255.255.0");
1629 if ($errormessage ne ''){
1630 goto ZERINA_ERROR;
1631 }
1632
1633 $key = &General::findhasharraykey (\%confighash);
1634 foreach my $i (0 .. 42) { $confighash{$key}[$i] = "";}
1635 $confighash{$key}[0] = 'off';
1636 $confighash{$key}[1] = $confdetails[$ufuk];
1637 #$confighash{$key}[2] = $confdetails[7];
1638 $confighash{$key}[2] = $confdetails[$ufuk];
1639 $confighash{$key}[3] = 'net';
1640 $confighash{$key}[4] = 'cert';
1641 $confighash{$key}[6] = 'client';
1642 $confighash{$key}[8] = $confdetails[8];
1643 @rem_subnet = split(/ /,$confdetails[$ufuk-1]);
1644 $confighash{$key}[11] = "$rem_subnet[0]/$rem_subnet[1]";
1645 $confighash{$key}[10] = $confdetails[5];
1646 $confighash{$key}[25] = 'imported';
1647 $confighash{$key}[12] = 'red';
1648 my @tmposupnet = split(/ /,$confdetails[4]);
1649 my @tmposupnet2 = split /\./,$tmposupnet[0];
1650 $confighash{$key}[13] = "$tmposupnet2[0].$tmposupnet2[1].$tmposupnet2[2].0/255.255.255.0";
1651 $confighash{$key}[14] = $confdetails[2];
1652 $confighash{$key}[15] = $confdetails[3];
1653 $confighash{$key}[16] = $complzoactive;
1654 $confighash{$key}[17] = $confdetails[1];
1655 $confighash{$key}[18] = '';# nn2nvpn_ip
1656 $confighash{$key}[19] = 'yes';# nn2nvpn_ip
1657 $cgiparams{'KEY'} = $key;
1658 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1659 mkdir("${General::swroot}/ovpn/n2nconf/$confdetails[14]", 0770);
1660 move("$tempdir/$uplconffilename", "${General::swroot}/ovpn/n2nconf/$confdetails[14]/$uplconffilename");
1661 if ($? ne 0) {
1662 $errormessage = "*.conf move failed: $!";
1663 unlink ($filename);
1664 goto ZERINA_ERROR;
1665 }
1666 move("$tempdir/$uplp12name", "${General::swroot}/ovpn/n2nconf/$confdetails[14]/$uplp12name");
1667 if ($? ne 0) {
1668 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
1669 unlink ($filename);
1670 goto ZERINA_ERROR;
1671 }
1672 ZERINA_ERROR:
1673
1674 &Header::showhttpheaders();
1675 &Header::openpage('Validate imported configuration', 1, '');
1676 &Header::openbigbox('100%', 'LEFT', '', $errormessage);
1677 if ($errormessage) {
1678 &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
1679 print "<class name='base'>$errormessage";
1680 print "&nbsp;</class>";
1681 &Header::closebox();
1682 } else {
1683 &Header::openbox('100%', 'LEFT', 'Validate imported configuration');
1684 }
1685 if ($errormessage eq ''){
1686 print <<END
1687 <!-- net2net config gui -->
1688 <tr><td width='25%'>&nbsp;</td>
1689 <td width='25%'>&nbsp;</td></tr>
1690 <tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'name'}:</td>
1691 <td><b>$confdetails[$ufuk]</b></td></tr>
1692 <tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'Act as'}</td>
1693 <td><b>$confdetails[6]</b></td>
1694 <td class='boldbase'>$Lang::tr{'remote host/ip'}:</td>
1695 <td><b>$confdetails[5]</b></td></tr>
1696 <tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'local subnet'}</td>
1697 <td><b>$confighash{$key}[8]</b></td>
1698 <td class='boldbase' nowrap='nowrap'>$Lang::tr{'remote subnet'}</td>
1699 <td><b>$confighash{$key}[11]</b></td></tr>
1700 <tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'ovpn subnet'}</td>
1701 <td><b>$confighash{$key}[$ufuk-1]</b></td></tr>
1702 <tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'protocol'}</td>
1703 <td><b>$confdetails[2]</b></td>
1704 <td class='boldbase'>$Lang::tr{'destination port'}:</td>
1705 <td><b>$confdetails[3]</b></td></tr>
1706 <tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'comp-lzo'}</td>
1707 <td><b>$complzoactive</b></td>
1708 <td class='boldbase'>$Lang::tr{'cipher'}</td>
1709 <td><b>$confdetails[10]</b></td></tr>
1710 <tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'MTU'}&nbsp;<img src='/blob.gif' /></td>
1711 <td><b>$confdetails[1]</b></td></tr>
1712 END
1713 ;
1714
1715 &Header::closebox();
1716 }
1717 if ($errormessage) {
1718 print "<div align='center'><a href='/cgi-bin/ovpnmain.cgi'>$Lang::tr{'back'}</a></div>";
1719 } else {
1720 print "<div align='center'><form method='post' enctype='multipart/form-data'><input type='submit' name='ACTION' value='Approved' />";
1721 print "<input type='hidden' name='TYPE' value='zerinan2n' />";
1722 print "<input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />";
1723 print "<input type='submit' name='ACTION' value='Discard' /></div></form>";
1724 }
1725 &Header::closebigbox();
1726 &Header::closepage();
1727 exit(0);
1728
1729 ###
1730 ### Approve Zerina n2n
1731 ###
1732 } elsif (($cgiparams{'ACTION'} eq 'Approved') && ($cgiparams{'TYPE'} eq 'zerinan2n')){
1733 &Ovpnfunc::writenet2netconf($cgiparams{'KEY'},$zerinaclient);
1734 ###
1735 ### Discard Zerina n2n
1736 ###
1737 } elsif (($cgiparams{'ACTION'} eq 'Discard') && ($cgiparams{'TYPE'} eq 'zerinan2n')){
1738 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1739 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1740
1741 if ($confighash{$cgiparams{'KEY'}}) {
1742 &Ovpnfunc::removenet2netconf();
1743 delete $confighash{$cgiparams{'KEY'}};
1744 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1745 } else {
1746 $errormessage = $Lang::tr{'invalid key'};
1747 }
1748 ###
1749 ### Adding a new connection
1750 ###
1751 } elsif (($cgiparams{'ACTION'} eq $Lang::tr{'add'}) ||
1752 ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) ||
1753 ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'ADVANCED'} eq '')) {
1754
1755 &General::readhash("${General::swroot}/ovpn/settings", \%vpnsettings);
1756 &General::readhasharray("${General::swroot}/ovpn/caconfig", \%cahash);
1757 &General::readhasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
1758
1759 if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) {
1760 if (! $confighash{$cgiparams{'KEY'}}[0]) {
1761 $errormessage = $Lang::tr{'invalid key'};
1762 goto VPNCONF_END;
1763 }
1764 $cgiparams{'ENABLED'} = $confighash{$cgiparams{'KEY'}}[0];
1765 $cgiparams{'NAME'} = $confighash{$cgiparams{'KEY'}}[1];
1766 $cgiparams{'TYPE'} = $confighash{$cgiparams{'KEY'}}[3];
1767 $cgiparams{'AUTH'} = $confighash{$cgiparams{'KEY'}}[4];
1768 $cgiparams{'PSK'} = $confighash{$cgiparams{'KEY'}}[5];
1769 $cgiparams{'SIDE'} = $confighash{$cgiparams{'KEY'}}[6];
1770 $cgiparams{'LOCAL_SUBNET'} = $confighash{$cgiparams{'KEY'}}[8];
1771 $cgiparams{'REMOTE'} = $confighash{$cgiparams{'KEY'}}[10];
1772 $cgiparams{'REMOTE_SUBNET'} = $confighash{$cgiparams{'KEY'}}[11];
1773 $cgiparams{'REMARK'} = $confighash{$cgiparams{'KEY'}}[25];
1774 $cgiparams{'INTERFACE'} = $confighash{$cgiparams{'KEY'}}[12];
1775 $cgiparams{'OVPN_SUBNET'} = $confighash{$cgiparams{'KEY'}}[13];#new fields
1776 $cgiparams{'PROTOCOL'} = $confighash{$cgiparams{'KEY'}}[14];
1777 $cgiparams{'DEST_PORT'} = $confighash{$cgiparams{'KEY'}}[15];
1778 $cgiparams{'COMPLZO'} = $confighash{$cgiparams{'KEY'}}[16];
1779 $cgiparams{'MTU'} = $confighash{$cgiparams{'KEY'}}[17];
1780 $cgiparams{'N2NVPN_IP'} = $confighash{$cgiparams{'KEY'}}[18];#new fields
1781 $cgiparams{'ZERINA_CLIENT'} = $confighash{$cgiparams{'KEY'}}[19];#new fields
1782 $cgiparams{'CIPHER'} = $confighash{$cgiparams{'KEY'}}[20];#new fields
1783 if ($cgiparams{'ZERINA_CLIENT'} eq ''){
1784 $cgiparams{'ZERINA_CLIENT'} = 'no';
1785 }
1786 #ab hiere error uebernehmen
1787 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
1788 $cgiparams{'REMARK'} = &Header::cleanhtml($cgiparams{'REMARK'});
1789 # n2n error
1790 if ($cgiparams{'TYPE'} !~ /^(host|net)$/) {
1791 $errormessage = $Lang::tr{'connection type is invalid'};
1792 goto VPNCONF_ERROR;
1793 }
1794 if ($cgiparams{'NAME'} !~ /^[a-zA-Z0-9]+$/) {
1795 $errormessage = $Lang::tr{'name must only contain characters'};
1796 goto VPNCONF_ERROR;
1797 }
1798 if ($cgiparams{'NAME'} =~ /^(host|01|block|private|clear|packetdefault|server)$/) {
1799 $errormessage = $Lang::tr{'name is invalid'};
1800 goto VPNCONF_ERROR;
1801 }
1802 if (length($cgiparams{'NAME'}) >60) {
1803 $errormessage = $Lang::tr{'name too long'};
1804 goto VPNCONF_ERROR;
1805 }
1806 if (! $cgiparams{'KEY'}) {# Check if there is no other entry with this name
1807 foreach my $key (keys %confighash) {
1808 if ($confighash{$key}[1] eq $cgiparams{'NAME'}) {
1809 $errormessage = $Lang::tr{'a connection with this name already exists'};
1810 goto VPNCONF_ERROR;
1811 }
1812 }
1813 }
1814 if (($cgiparams{'TYPE'} eq 'net') && (! $cgiparams{'REMOTE'})) {
1815 $errormessage = $Lang::tr{'invalid input for remote host/ip'};
1816 goto VPNCONF_ERROR;
1817 }
1818 if ($cgiparams{'REMOTE'}) {
1819 if (! &General::validip($cgiparams{'REMOTE'})) {
1820 if (! &General::validfqdn ($cgiparams{'REMOTE'})) {
1821 $errormessage = $Lang::tr{'invalid input for remote host/ip'};
1822 goto VPNCONF_ERROR;
1823 } else {
1824 if (&Ovpnfunc::valid_dns_host($cgiparams{'REMOTE'})) {
1825 $warnmessage = "$Lang::tr{'check vpn lr'} $cgiparams{'REMOTE'}. $Lang::tr{'dns check failed'}";
1826 }
1827 }
1828 }
1829 }
1830 if ($cgiparams{'TYPE'} ne 'host') {
1831 unless (&General::validipandmask($cgiparams{'LOCAL_SUBNET'})) {
1832 $errormessage = $Lang::tr{'local subnet is invalid'};
1833 goto VPNCONF_ERROR;
1834 }
1835 }
1836 #hier1
1837 my @tmpovpnsubnet = split("\/",$cgiparams{'LOCAL_SUBNET'});
1838 $tmpovpnsubnet[1] = &Ovpnfunc::cidrormask($tmpovpnsubnet[1]);
1839 $cgiparams{'LOCAL_SUBNET'} = "$tmpovpnsubnet[0]/$tmpovpnsubnet[1]";#convert from cidr
1840 #hier1
1841 if ($cgiparams{'REMOTE'} eq '') {# Check if there is no other entry without IP-address and PSK
1842 foreach my $key (keys %confighash) {
1843 if(($cgiparams{'KEY'} ne $key) && ($confighash{$key}[4] eq 'psk' || $cgiparams{'AUTH'} eq 'psk') && $confighash{$key}[10] eq '') {
1844 $errormessage = $Lang::tr{'you can only define one roadwarrior connection when using pre-shared key authentication'};
1845 goto VPNCONF_ERROR;
1846 }
1847 }
1848 }
1849 if (($cgiparams{'TYPE'} eq 'net') && (! &General::validipandmask($cgiparams{'REMOTE_SUBNET'}))) {
1850 $errormessage = $Lang::tr{'remote subnet is invalid'};
1851 goto VPNCONF_ERROR;
1852 }
1853 #hier2
1854 my @tmpovpnsubnet = split("\/",$cgiparams{'REMOTE_SUBNET'});
1855 $tmpovpnsubnet[1] = &Ovpnfunc::cidrormask($tmpovpnsubnet[1]);
1856 $cgiparams{'REMOTE_SUBNET'} = "$tmpovpnsubnet[0]/$tmpovpnsubnet[1]";#convert from cidr
1857 #hier2
1858 if ($cgiparams{'ENABLED'} !~ /^(on|off)$/) {
1859 $errormessage = $Lang::tr{'invalid input'};
1860 goto VPNCONF_ERROR;
1861 }
1862 if ($cgiparams{'EDIT_ADVANCED'} !~ /^(on|off)$/) {
1863 $errormessage = $Lang::tr{'invalid input'};
1864 goto VPNCONF_ERROR;
1865 }
1866 if ($cgiparams{'ENABLED'} eq 'on'){
1867 $errormessage = &Ovpnfunc::disallowreserved($cgiparams{'DEST_PORT'},0,$cgiparams{'PROTOCOL'},"dest");
1868 }
1869 if ($errormessage) { goto VPNCONF_ERROR; }
1870
1871 if ($cgiparams{'ENABLED'} eq 'on'){
1872 $errormessage = &Ovpnfunc::checkportfw(0,$cgiparams{'DEST_PORT'},$cgiparams{'PROTOCOL'},'0.0.0.0');
1873 }
1874 if ($errormessage) { goto VPNCONF_ERROR; }
1875 #raul
1876 if ($cgiparams{'TYPE'} eq 'net') {
1877 if (! &General::validipandmask($cgiparams{'OVPN_SUBNET'})) {
1878 $errormessage = $Lang::tr{'ovpn subnet is invalid'};
1879 goto VPNCONF_ERROR;
1880 }
1881 #hier3
1882 my @tmpovpnsubnet = split("\/",$cgiparams{'OVPN_SUBNET'});
1883 $tmpovpnsubnet[1] = &Ovpnfunc::cidrormask($tmpovpnsubnet[1]);
1884 $cgiparams{'OVPN_SUBNET'} = "$tmpovpnsubnet[0]/$tmpovpnsubnet[1]";#convert from cidr
1885 #hier3
1886 #plausi2
1887 $errormessage = &Ovpnfunc::ovelapplausi($tmpovpnsubnet[0],$tmpovpnsubnet[1]);
1888 #plausi2
1889 if ($errormessage ne ''){
1890 goto VPNCONF_ERROR;
1891 }
1892 if ((length($cgiparams{'MTU'})==0) || (($cgiparams{'MTU'}) < 1000 )) {
1893 $errormessage = $Lang::tr{'invalid mtu input'};
1894 goto VPNCONF_ERROR;
1895 }
1896 unless (&General::validport($cgiparams{'DEST_PORT'})) {
1897 $errormessage = $Lang::tr{'invalid port'};
1898 goto VPNCONF_ERROR;
1899 }
1900 # check protcol/port overlap against existing connections gian
1901 foreach my $dkey (keys %confighash) {#Check if there is no other entry with this name
1902 if ($dkey ne $cgiparams{'KEY'}) {
1903 if ($confighash{$dkey}[14] eq $cgiparams{'PROTOCOL'} && $confighash{$dkey}[15] eq $cgiparams{'DEST_PORT'}){
1904 #if ($confighash{$dkey}[14] eq 'on') {
1905 $errormessage = "Choosed Protocol/Port combination is already used by connection: $confighash{$dkey}[1]";
1906 goto VPNCONF_ERROR;
1907 #} else {
1908 # $warnmessage = "Choosed Protcol/Port combination is used by inactive connection: $confighash{$dkey}[1]";
1909 #}
1910 }
1911 }
1912 }
1913 #check protcol/port overlap against RWserver gian
1914 if ($vpnsettings{'ENABLED'} eq 'on') {
1915 if ($vpnsettings{'DPROTOCOL'} eq $cgiparams{'PROTOCOL'} && $vpnsettings{'DDEST_PORT'} eq $cgiparams{'DEST_PORT'}){
1916 $errormessage = "Choosed Protocol/Port combination is already used OpenVPN Roadwarrior Server";
1917 goto VPNCONF_ERROR;
1918 }
1919 }
1920 }
1921 if ($cgiparams{'AUTH'} eq 'psk') {
1922 #removed
1923 } elsif ($cgiparams{'AUTH'} eq 'certreq') {
1924 # {
1925 if ($cgiparams{'KEY'}) {
1926 $errormessage = $Lang::tr{'cant change certificates'};
1927 goto VPNCONF_ERROR;
1928 }
1929 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1930 $errormessage = $Lang::tr{'there was no file upload'};
1931 goto VPNCONF_ERROR;
1932 }
1933 (my $fh, my $filename) = tempfile( );# Move uploaded certificate request to a temporary file
1934 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1935 $errormessage = $!;
1936 goto VPNCONF_ERROR;
1937 }
1938 # Sign the certificate request and move it
1939 # Sign the host certificate request
1940 system('/usr/bin/openssl', 'ca', '-days', '999999',
1941 '-batch', '-notext',
1942 '-in', $filename,
1943 '-out', "${General::swroot}/ovpn/certs/$cgiparams{'NAME'}cert.pem",
1944 '-config',"${General::swroot}/ovpn/openssl/ovpn.cnf");
1945 if ($?) {
1946 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
1947 unlink ($filename);
1948 unlink ("${General::swroot}/ovpn/certs/$cgiparams{'NAME'}cert.pem");
1949 &Ovpnfunc::newcleanssldatabase();
1950 goto VPNCONF_ERROR;
1951 } else {
1952 unlink ($filename);
1953 &Ovpnfunc::deletebackupcert();
1954 }
1955 my $temp = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/certs/$cgiparams{'NAME'}cert.pem`;
1956 $temp =~ /Subject:.*CN=(.*)[\n]/;
1957 $temp = $1;
1958 $temp =~ s+/Email+, E+;
1959 $temp =~ s/ ST=/ S=/;
1960 $cgiparams{'CERT_NAME'} = $temp;
1961 $cgiparams{'CERT_NAME'} =~ s/,//g;
1962 $cgiparams{'CERT_NAME'} =~ s/\'//g;
1963 if ($cgiparams{'CERT_NAME'} eq '') {
1964 $errormessage = $Lang::tr{'could not retrieve common name from certificate'};
1965 goto VPNCONF_ERROR;
1966 }
1967 } elsif ($cgiparams{'AUTH'} eq 'certfile') {
1968 if ($cgiparams{'KEY'}) {
1969 $errormessage = $Lang::tr{'cant change certificates'};
1970 goto VPNCONF_ERROR;
1971 }
1972 if (ref ($cgiparams{'FH'}) ne 'Fh') {
1973 $errormessage = $Lang::tr{'there was no file upload'};
1974 goto VPNCONF_ERROR;
1975 }
1976 (my $fh, my $filename) = tempfile( );# Move uploaded certificate to a temporary file
1977 if (copy ($cgiparams{'FH'}, $fh) != 1) {
1978 $errormessage = $!;
1979 goto VPNCONF_ERROR;
1980 }
1981 my $validca = 0;# Verify the certificate has a valid CA and move it
1982 my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ovpn/ca/cacert.pem $filename`;
1983 if ($test =~ /: OK/) {
1984 $validca = 1;
1985 } else {
1986 foreach my $key (keys %cahash) {
1987 $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ovpn/ca/$cahash{$key}[0]cert.pem $filename`;
1988 if ($test =~ /: OK/) {
1989 $validca = 1;
1990 }
1991 }
1992 }
1993 if (! $validca) {
1994 $errormessage = $Lang::tr{'certificate does not have a valid ca associated with it'};
1995 unlink ($filename);
1996 goto VPNCONF_ERROR;
1997 } else {
1998 move($filename, "${General::swroot}/ovpn/certs/$cgiparams{'NAME'}cert.pem");
1999 if ($? ne 0) {
2000 $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
2001 unlink ($filename);
2002 goto VPNCONF_ERROR;
2003 }
2004 }
2005 my $temp = `/usr/bin/openssl x509 -text -in ${General::swroot}/ovpn/certs/$cgiparams{'NAME'}cert.pem`;
2006 $temp =~ /Subject:.*CN=(.*)[\n]/;
2007 $temp = $1;
2008 $temp =~ s+/Email+, E+;
2009 $temp =~ s/ ST=/ S=/;
2010 $cgiparams{'CERT_NAME'} = $temp;
2011 $cgiparams{'CERT_NAME'} =~ s/,//g;
2012 $cgiparams{'CERT_NAME'} =~ s/\'//g;
2013 if ($cgiparams{'CERT_NAME'} eq '') {
2014 unlink ("${General::swroot}/ovpn/certs/$cgiparams{'NAME'}cert.pem");
2015 $errormessage = $Lang::tr{'could not retrieve common name from certificate'};
2016 goto VPNCONF_ERROR;
2017 }
2018 } elsif ($cgiparams{'AUTH'} eq 'certgen'){
2019 if ($cgiparams{'KEY'}) {
2020 $errormessage = $Lang::tr{'cant change certificates'};
2021 goto VPNCONF_ERROR;
2022 }
2023 if (length($cgiparams{'CERT_NAME'}) >60) {# Validate input since the form was submitted
2024 $errormessage = $Lang::tr{'name too long'};
2025 goto VPNCONF_ERROR;
2026 }
2027 if ($cgiparams{'CERT_NAME'} !~ /^[a-zA-Z0-9 ,\.\-_]+$/) {
2028 $errormessage = $Lang::tr{'invalid input for name'};
2029 goto VPNCONF_ERROR;
2030 }
2031 if ($cgiparams{'CERT_EMAIL'} ne '' && (! &General::validemail($cgiparams{'CERT_EMAIL'}))) {
2032 $errormessage = $Lang::tr{'invalid input for e-mail address'};
2033 goto VPNCONF_ERROR;
2034 }
2035 if (length($cgiparams{'CERT_EMAIL'}) > 40) {
2036 $errormessage = $Lang::tr{'e-mail address too long'};
2037 goto VPNCONF_ERROR;
2038 }
2039 if ($cgiparams{'CERT_OU'} ne '' && $cgiparams{'CERT_OU'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
2040 $errormessage = $Lang::tr{'invalid input for department'};
2041 goto VPNCONF_ERROR;
2042 }
2043 if (length($cgiparams{'CERT_ORGANIZATION'}) >60) {
2044 $errormessage = $Lang::tr{'organization too long'};
2045 goto VPNCONF_ERROR;
2046 }
2047 if ($cgiparams{'CERT_ORGANIZATION'} !~ /^[a-zA-Z0-9 ,\.\-_]+$/) {
2048 $errormessage = $Lang::tr{'invalid input for organization'};
2049 goto VPNCONF_ERROR;
2050 }
2051 if ($cgiparams{'CERT_CITY'} ne '' && $cgiparams{'CERT_CITY'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
2052 $errormessage = $Lang::tr{'invalid input for city'};
2053 goto VPNCONF_ERROR;
2054 }
2055 if ($cgiparams{'CERT_STATE'} ne '' && $cgiparams{'CERT_STATE'} !~ /^[a-zA-Z0-9 ,\.\-_]*$/) {
2056 $errormessage = $Lang::tr{'invalid input for state or province'};
2057 goto VPNCONF_ERROR;
2058 }
2059 if ($cgiparams{'CERT_COUNTRY'} !~ /^[A-Z]*$/) {
2060 $errormessage = $Lang::tr{'invalid input for country'};
2061 goto VPNCONF_ERROR;
2062 }
2063 if ($cgiparams{'CERT_PASS1'} ne '' && $cgiparams{'CERT_PASS2'} ne ''){
2064 if (length($cgiparams{'CERT_PASS1'}) < 5) {
2065 $errormessage = $Lang::tr{'password too short'};
2066 goto VPNCONF_ERROR;
2067 }
2068 }
2069 if ($cgiparams{'CERT_PASS1'} ne $cgiparams{'CERT_PASS2'}) {
2070 $errormessage = $Lang::tr{'passwords do not match'};
2071 goto VPNCONF_ERROR;
2072 }
2073 (my $ou = $cgiparams{'CERT_OU'}) =~ s/^\s*$/\./;# Replace empty strings with a .
2074 (my $city = $cgiparams{'CERT_CITY'}) =~ s/^\s*$/\./;
2075 (my $state = $cgiparams{'CERT_STATE'}) =~ s/^\s*$/\./;
2076 my $pid = open(OPENSSL, "|-");# Create the Host certificate request client
2077 $SIG{ALRM} = sub { $errormessage = $Lang::tr{'broken pipe'}; goto VPNCONF_ERROR;};
2078 if ($pid) { # parent
2079 print OPENSSL "$cgiparams{'CERT_COUNTRY'}\n";
2080 print OPENSSL "$state\n";
2081 print OPENSSL "$city\n";
2082 print OPENSSL "$cgiparams{'CERT_ORGANIZATION'}\n";
2083 print OPENSSL "$ou\n";
2084 print OPENSSL "$cgiparams{'CERT_NAME'}\n";
2085 print OPENSSL "$cgiparams{'CERT_EMAIL'}\n";
2086 print OPENSSL ".\n";
2087 print OPENSSL ".\n";
2088 close (OPENSSL);
2089 if ($?) {
2090 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
2091 unlink ("${General::swroot}ovpn/certs/$cgiparams{'NAME'}key.pem");
2092 unlink ("${General::swroot}ovpn/certs/$cgiparams{'NAME'}req.pem");
2093 goto VPNCONF_ERROR;
2094 }
2095 } else { # child
2096 unless (exec ('/usr/bin/openssl', 'req', '-nodes', '-rand', '/proc/interrupts:/proc/net/rt_cache',
2097 '-newkey', 'rsa:1024',
2098 '-keyout', "${General::swroot}/ovpn/certs/$cgiparams{'NAME'}key.pem",
2099 '-out', "${General::swroot}/ovpn/certs/$cgiparams{'NAME'}req.pem",
2100 '-config',"${General::swroot}/ovpn/openssl/ovpn.cnf")) {
2101 $errormessage = "$Lang::tr{'cant start openssl'}: $!";
2102 unlink ("${General::swroot}/ovpn/certs/$cgiparams{'NAME'}key.pem");
2103 unlink ("${General::swroot}/ovpn/certs/$cgiparams{'NAME'}req.pem");
2104 goto VPNCONF_ERROR;
2105 }
2106 }
2107 # Sign the host certificate request
2108 system('/usr/bin/openssl', 'ca', '-days', '999999',
2109 '-batch', '-notext',
2110 '-in', "${General::swroot}/ovpn/certs/$cgiparams{'NAME'}req.pem",
2111 '-out', "${General::swroot}/ovpn/certs/$cgiparams{'NAME'}cert.pem",
2112 '-config',"${General::swroot}/ovpn/openssl/ovpn.cnf");
2113 if ($?) {
2114 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
2115 unlink ("${General::swroot}/ovpn/certs/$cgiparams{'NAME'}key.pem");
2116 unlink ("${General::swroot}/ovpn/certs/$cgiparams{'NAME'}req.pem");
2117 unlink ("${General::swroot}/ovpn/certs/$cgiparams{'NAME'}cert.pem");
2118 &Ovpnfunc::newcleanssldatabase();
2119 goto VPNCONF_ERROR;
2120 } else {
2121 unlink ("${General::swroot}/ovpn/certs/$cgiparams{'NAME'}req.pem");
2122 &Ovpnfunc::deletebackupcert();
2123 }
2124 # Create the pkcs12 file
2125 system('/usr/bin/openssl', 'pkcs12', '-export',
2126 '-inkey', "${General::swroot}/ovpn/certs/$cgiparams{'NAME'}key.pem",
2127 '-in', "${General::swroot}/ovpn/certs/$cgiparams{'NAME'}cert.pem",
2128 '-name', $cgiparams{'NAME'},
2129 '-passout', "pass:$cgiparams{'CERT_PASS1'}",
2130 '-certfile', "${General::swroot}/ovpn/ca/cacert.pem",
2131 '-caname', "$vpnsettings{'ROOTCERT_ORGANIZATION'} CA",
2132 '-out', "${General::swroot}/ovpn/certs/$cgiparams{'NAME'}.p12");
2133 if ($?) {
2134 $errormessage = "$Lang::tr{'openssl produced an error'}: $?";
2135 unlink ("${General::swroot}/ovpn/certs/$cgiparams{'NAME'}key.pem");
2136 unlink ("${General::swroot}/ovpn/certs/$cgiparams{'NAME'}cert.pem");
2137 unlink ("${General::swroot}/ovpn/certs/$cgiparams{'NAME'}.p12");
2138 goto VPNCONF_ERROR;
2139 } else {
2140 unlink ("${General::swroot}/ovpn/certs/$cgiparams{'NAME'}key.pem");
2141 }
2142 } elsif ($cgiparams{'AUTH'} eq 'cert') {
2143 ;# Nothing, just editing
2144 } else {
2145 $errormessage = $Lang::tr{'invalid input for authentication method'};
2146 goto VPNCONF_ERROR;
2147 }
2148 if ((! $cgiparams{'KEY'}) && ($cgiparams{'AUTH'} ne 'psk')) {# Check if there is no other entry with this common name
2149 foreach my $key (keys %confighash) {
2150 if ($confighash{$key}[2] eq $cgiparams{'CERT_NAME'}) {
2151 $errormessage = $Lang::tr{'a connection with this common name already exists'};
2152 goto VPNCONF_ERROR;
2153 }
2154 }
2155 }
2156
2157 my $key = $cgiparams{'KEY'};# Save the config
2158 if (! $key) {
2159 $key = &General::findhasharraykey (\%confighash);
2160 foreach my $i (0 .. 42) { $confighash{$key}[$i] = "";}
2161 }
2162 $confighash{$key}[0] = $cgiparams{'ENABLED'};
2163 $confighash{$key}[1] = $cgiparams{'NAME'};
2164 if ((! $cgiparams{'KEY'}) && $cgiparams{'AUTH'} ne 'psk') {
2165 $confighash{$key}[2] = $cgiparams{'CERT_NAME'};
2166 }
2167 $confighash{$key}[3] = $cgiparams{'TYPE'};
2168 if ($cgiparams{'AUTH'} eq 'psk') {
2169 $confighash{$key}[4] = 'psk';
2170 $confighash{$key}[5] = $cgiparams{'PSK'};
2171 } else {
2172 $confighash{$key}[4] = 'cert';
2173 }
2174 if ($cgiparams{'TYPE'} eq 'net') {
2175 $confighash{$key}[6] = $cgiparams{'SIDE'};
2176 $confighash{$key}[11] = $cgiparams{'REMOTE_SUBNET'};
2177 if ( $cgiparams{'SIDE'} eq 'client') {
2178 $confighash{$key}[19] = 'yes';
2179 } else{
2180 $confighash{$key}[19] = 'no';
2181 }
2182 }
2183 $confighash{$key}[8] = $cgiparams{'LOCAL_SUBNET'};
2184 $confighash{$key}[10] = $cgiparams{'REMOTE'};
2185 $confighash{$key}[25] = $cgiparams{'REMARK'};
2186 $confighash{$key}[12] = $cgiparams{'INTERFACE'};
2187 $confighash{$key}[13] = $cgiparams{'OVPN_SUBNET'};# new fields
2188 $confighash{$key}[14] = $cgiparams{'PROTOCOL'};
2189 $confighash{$key}[15] = $cgiparams{'DEST_PORT'};
2190 $confighash{$key}[16] = $cgiparams{'COMPLZO'};
2191 $confighash{$key}[17] = $cgiparams{'MTU'};
2192 $confighash{$key}[18] = $cgiparams{'N2NVPN_IP'};# new fileds
2193 $confighash{$key}[19] = $cgiparams{'ZERINA_CLIENT'};# new fileds
2194 $confighash{$key}[20] = $cgiparams{'CIPHER'};
2195
2196 #default n2n advanced
2197 $confighash{$key}[26] = '10';#keepalive ping
2198 $confighash{$key}[27] = '60';#keepalive restart
2199 $confighash{$key}[28] = '0';#nice
2200 $confighash{$key}[42] = '3';#verb
2201 #default n2n advanced
2202 &General::writehasharray("${General::swroot}/ovpn/ovpnconfig", \%confighash);
2203 &Ovpnfunc::writenet2netconf($key,$zerinaclient);
2204 #ppp
2205 my $n2nactive = `/bin/ps ax|grep $cgiparams{'NAME'}.conf|grep -v grep|awk \'{print \$1}\'`;
2206 if ($cgiparams{'ENABLED'}) {
2207 if ($n2nactive eq ''){
2208 system('/usr/local/bin/openvpnctrl', '-sn2n', $cgiparams{'NAME'});
2209 } else {
2210 system('/usr/local/bin/openvpnctrl', '-kn2n', $n2nactive);
2211 system('/usr/local/bin/openvpnctrl', '-sn2n', $cgiparams{'NAME'});
2212 }
2213 } else {
2214 if ($n2nactive ne ''){
2215 system('/usr/local/bin/openvpnctrl', '-kn2n', $cgiparams{'NAME'});
2216 }
2217 }
2218 if ($cgiparams{'EDIT_ADVANCED'} eq 'on') {
2219 $cgiparams{'KEY'} = $key;
2220 $cgiparams{'ACTION'} = $Lang::tr{'advanced'};
2221 }
2222 goto VPNCONF_END;
2223 } else {
2224 $cgiparams{'ENABLED'} = 'on';
2225 if ($cgiparams{'ZERINA_CLIENT'} eq ''){
2226 $cgiparams{'ZERINA_CLIENT'} = 'no';
2227 }
2228 if ( ! -f "${General::swroot}/ovpn/ca/cakey.pem" ) {
2229 $cgiparams{'AUTH'} = 'psk';
2230 } elsif ( ! -f "${General::swroot}/ovpn/ca/cacert.pem") {
2231 $cgiparams{'AUTH'} = 'certfile';
2232 } else {
2233 $cgiparams{'AUTH'} = 'certgen';
2234 }
2235 $cgiparams{'LOCAL_SUBNET'} ="$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}";
2236 $cgiparams{'CERT_ORGANIZATION'} = $vpnsettings{'ROOTCERT_ORGANIZATION'};
2237 $cgiparams{'CERT_CITY'} = $vpnsettings{'ROOTCERT_CITY'};
2238 $cgiparams{'CERT_STATE'} = $vpnsettings{'ROOTCERT_STATE'};
2239 $cgiparams{'CERT_COUNTRY'} = $vpnsettings{'ROOTCERT_COUNTRY'};
2240 }
2241 VPNCONF_ERROR:
2242 # n2n default settings
2243 if ($cgiparams{'CIPHER'} eq '') {
2244 $cgiparams{'CIPHER'} = 'BF-CBC';
2245 }
2246 if ($cgiparams{'MTU'} eq '') {
2247 $cgiparams{'MTU'} = '1400';
2248 }
2249 if ($cgiparams{'OVPN_SUBNET'} eq '') {
2250 $cgiparams{'OVPN_SUBNET'} = '10.' . int(rand(256)) . '.' . int(rand(256)) . '.0/255.255.255.0';
2251 }
2252 #n2n default settings
2253 $checked{'ENABLED'}{'off'} = '';
2254 $checked{'ENABLED'}{'on'} = '';
2255 $checked{'ENABLED'}{$cgiparams{'ENABLED'}} = 'CHECKED';
2256 $checked{'ENABLED_BLUE'}{'off'} = '';
2257 $checked{'ENABLED_BLUE'}{'on'} = '';
2258 $checked{'ENABLED_BLUE'}{$cgiparams{'ENABLED_BLUE'}} = 'CHECKED';
2259 $checked{'ENABLED_ORANGE'}{'off'} = '';
2260 $checked{'ENABLED_ORANGE'}{'on'} = '';
2261 $checked{'ENABLED_ORANGE'}{$cgiparams{'ENABLED_ORANGE'}} = 'CHECKED';
2262 $checked{'EDIT_ADVANCED'}{'off'} = '';
2263 $checked{'EDIT_ADVANCED'}{'on'} = '';
2264 $checked{'EDIT_ADVANCED'}{$cgiparams{'EDIT_ADVANCED'}} = 'CHECKED';
2265 $selected{'SIDE'}{'server'} = '';
2266 $selected{'SIDE'}{'client'} = '';
2267 $selected{'SIDE'}{$cgiparams{'SIDE'}} = 'SELECTED';
2268
2269 # $selected{'DDEVICE'}{'tun'} = '';
2270 # $selected{'DDEVICE'}{'tap'} = '';
2271 # $selected{'DDEVICE'}{$cgiparams{'DDEVICE'}} = 'SELECTED';
2272
2273 $selected{'PROTOCOL'}{'udp'} = '';
2274 $selected{'PROTOCOL'}{'tcp'} = '';
2275 $selected{'PROTOCOL'}{$cgiparams{'PROTOCOL'}} = 'SELECTED';
2276
2277 $checked{'AUTH'}{'psk'} = '';
2278 $checked{'AUTH'}{'certreq'} = '';
2279 $checked{'AUTH'}{'certgen'} = '';
2280 $checked{'AUTH'}{'certfile'} = '';
2281 $checked{'AUTH'}{$cgiparams{'AUTH'}} = 'CHECKED';
2282 $selected{'INTERFACE'}{$cgiparams{'INTERFACE'}} = 'SELECTED';
2283 $checked{'COMPLZO'}{'off'} = '';
2284 $checked{'COMPLZO'}{'on'} = '';
2285 $checked{'COMPLZO'}{$cgiparams{'COMPLZO'}} = 'CHECKED';
2286 $selected{'CIPHER'}{'DES-CBC'} = '';
2287 $selected{'CIPHER'}{'DES-EDE-CBC'} = '';
2288 $selected{'CIPHER'}{'DES-EDE3-CBC'} = '';
2289 $selected{'CIPHER'}{'DESX-CBC'} = '';
2290 $selected{'CIPHER'}{'RC2-CBC'} = '';
2291 $selected{'CIPHER'}{'RC2-40-CBC'} = '';
2292 $selected{'CIPHER'}{'RC2-64-CBC'} = '';
2293 $selected{'CIPHER'}{'BF-CBC'} = '';
2294 $selected{'CIPHER'}{'CAST5-CBC'} = '';
2295 $selected{'CIPHER'}{'AES-128-CBC'} = '';
2296 $selected{'CIPHER'}{'AES-192-CBC'} = '';
2297 $selected{'CIPHER'}{'AES-256-CBC'} = '';
2298 $selected{'CIPHER'}{$cgiparams{'CIPHER'}} = 'SELECTED';
2299
2300 if (1) {
2301 &Header::showhttpheaders();
2302 &Header::openpage($Lang::tr{'vpn configuration main'}, 1, '');
2303 &Header::openbigbox('100%', 'LEFT', '', $errormessage);
2304 if ($errormessage) {
2305 &Header::openbox('100%', 'LEFT', $Lang::tr{'error messages'});
2306 print "<class name='base'>$errormessage";
2307 print "&nbsp;</class>";
2308 &Header::closebox();
2309 }
2310 if ($warnmessage) {
2311 &Header::openbox('100%', 'LEFT', "$Lang::tr{'warning messages'}:");
2312 print "<class name='base'>$warnmessage";
2313 print "&nbsp;</class>";
2314 &Header::closebox();
2315 }
2316 print "<form method='post' enctype='multipart/form-data'>";
2317 print "<input type='hidden' name='TYPE' value='$cgiparams{'TYPE'}' />";
2318 print "<input type='hidden' name='ZERINA_CLIENT' value='$cgiparams{'ZERINA_CLIENT'}' />";
2319 if ($cgiparams{'KEY'}) {
2320 print "<input type='hidden' name='KEY' value='$cgiparams{'KEY'}' />";
2321 print "<input type='hidden' name='AUTH' value='$cgiparams{'AUTH'}' />";
2322 print "<input type='hidden' name='ZERINA_CLIENT' value='$cgiparams{'ZERINA_CLIENT'}' />";
2323 }
2324 &Header::openbox('100%', 'LEFT', "$Lang::tr{'connection'}:");
2325 print "<table width='100%'>\n";
2326 print "<tr><td width='25%' class='boldbase'>$Lang::tr{'name'}:</td>";
2327 if ($cgiparams{'TYPE'} eq 'host') {
2328 if ($cgiparams{'KEY'}) {
2329 print "<td width='35%' class='base'><input type='hidden' name='NAME' value='$cgiparams{'NAME'}' />$cgiparams{'NAME'}</td>\n";
2330 } else {
2331 print "<td width='35%'><input type='text' name='NAME' value='$cgiparams{'NAME'}' maxlength='20' size='30' /></td>";
2332 }
2333 } else {
2334 print "<input type='hidden' name='INTERFACE' value='red' />";
2335 if ($cgiparams{'KEY'}) {
2336 print "<td width='25%' class='base' nowrap='nowrap'><input type='hidden' name='NAME' value='$cgiparams{'NAME'}' />$cgiparams{'NAME'}</td>";
2337 } else {
2338 print "<td width='25%'><input type='text' name='NAME' value='$cgiparams{'NAME'}' maxlength='20' /></td>";
2339 }
2340 print "<!-- net2net config gui -->";
2341 print "<td width='25%'>&nbsp;</td>";
2342 print "<td width='25%'>&nbsp;</td></tr>";
2343 if ((($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) && ($cgiparams{'ZERINA_CLIENT'} eq 'no')) ||
2344 (($cgiparams{'ACTION'} eq $Lang::tr{'save'}) && ($cgiparams{'ZERINA_CLIENT'} eq 'no')) ||
2345 (($cgiparams{'ACTION'} eq $Lang::tr{'add'}) && ($cgiparams{'ZERINA_CLIENT'} eq 'no'))) {
2346 print "<tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'Act as'}</td>";
2347 print "<td><select name='SIDE'><option value='server' $selected{'SIDE'}{'server'}>OpenVPN Server</option>";
2348 print "<option value='client' $selected{'SIDE'}{'client'}>OpenVPN Client</option></select></td>";
2349 print "<tr><td class='base' nowrap='nowrap'>$Lang::tr{'local vpn hostname/ip'}:</td>";
2350 print "<td><input type='text' name='N2NVPN_IP' value='$cgiparams{'N2NVPN_IP'}' size='30' /></td>";
2351 print "<td class='boldbase'>$Lang::tr{'remote host/ip'}:</td>";
2352 } else {
2353 print "<tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'Act as'}</td>";
2354 print "<td>$cgiparams{'SIDE'}</td><input type='hidden' name='SIDE' value='$cgiparams{'SIDE'}' />";
2355 print "<td class='boldbase'>$Lang::tr{'remote host/ip'}:</td>";
2356 }
2357 print "<td><input type='TEXT' name='REMOTE' value='$cgiparams{'REMOTE'}' /></td></tr>";
2358 print "<tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'local subnet'}</td>";
2359 print "<td><input type='TEXT' name='LOCAL_SUBNET' value='$cgiparams{'LOCAL_SUBNET'}' /></td>";
2360 print "<td class='boldbase' nowrap='nowrap'>$Lang::tr{'remote subnet'}</td>";
2361 print "<td><input type='text' name='REMOTE_SUBNET' value='$cgiparams{'REMOTE_SUBNET'}' /></td></tr>";
2362 print "<tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'ovpn subnet'}</td>";
2363 print "<td><input type='TEXT' name='OVPN_SUBNET' value='$cgiparams{'OVPN_SUBNET'}' /></td></tr>";
2364 print "<tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'protocol'}</td>";
2365 print "<td><select name='PROTOCOL'><option value='udp' $selected{'PROTOCOL'}{'udp'}>UDP</option>";
2366 print "<option value='tcp' $selected{'PROTOCOL'}{'tcp'}>TCP</option></select></td>";
2367 print "<td class='boldbase'>$Lang::tr{'destination port'}:</td>";
2368 print "<td><input type='TEXT' name='DEST_PORT' value='$cgiparams{'DEST_PORT'}' size='5' /></td></tr>";
2369 print "<tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'comp-lzo'}</td>";
2370 print "<td><input type='checkbox' name='COMPLZO' $checked{'COMPLZO'}{'on'} /></td>";
2371 print "<td class='boldbase' nowrap='nowrap'>$Lang::tr{'cipher'}</td>";
2372 print "<td><select name='CIPHER'><option value='DES-CBC' $selected{'CIPHER'}{'DES-CBC'}>DES-CBC</option>";
2373 print "<option value='DES-EDE-CBC' $selected{'CIPHER'}{'DES-EDE-CBC'}>DES-EDE-CBC</option>";
2374 print "<option value='DES-EDE3-CBC' $selected{'CIPHER'}{'DES-EDE3-CBC'}>DES-EDE3-CBC</option>";
2375 print "<option value='DESX-CBC' $selected{'CIPHER'}{'DESX-CBC'}>DESX-CBC</option>";
2376 print "<option value='RC2-CBC' $selected{'CIPHER'}{'RC2-CBC'}>RC2-CBC</option>";
2377 print "<option value='RC2-40-CBC' $selected{'CIPHER'}{'RC2-40-CBC'}>RC2-40-CBC</option>";
2378 print "<option value='RC2-64-CBC' $selected{'CIPHER'}{'RC2-64-CBC'}>RC2-64-CBC</option>";
2379 print "<option value='BF-CBC' $selected{'CIPHER'}{'BF-CBC'}>BF-CBC</option>";
2380 print "<option value='CAST5-CBC' $selected{'CIPHER'}{'CAST5-CBC'}>CAST5-CBC</option>";
2381 print "<option value='AES-128-CBC' $selected{'CIPHER'}{'AES-128-CBC'}>AES-128-CBC</option>";
2382 print "<option value='AES-192-CBC' $selected{'CIPHER'}{'AES-192-CBC'}>AES-192-CBC</option>";
2383 print "<option value='AES-256-CBC' $selected{'CIPHER'}{'AES-256-CBC'}>AES-256-CBC</option></select></td>";
2384 print "<tr><td class='boldbase' nowrap='nowrap'>$Lang::tr{'MTU'}&nbsp;</td>";
2385 print "<td> <input type='TEXT' name='MTU' VALUE='$cgiparams{'MTU'}'size='5' /></TD>";
2386 }
2387 print "<tr><td class='boldbase'>$Lang::tr{'remark title'}&nbsp;<img src='/blob.gif' /></td>";
2388 print "<td colspan='3'><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='55' maxlength='50' /></td></tr>";
2389 # if ($cgiparams{'TYPE'} eq 'net') {
2390 print "<tr><td>$Lang::tr{'enabled'} <input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>\n";
2391
2392 if ($cgiparams{'TYPE'} eq 'host') {
2393 print "<td colspan='3'>&nbsp;</td></tr></table>";
2394 } elsif ($cgiparams{'ACTION'} ne $Lang::tr{'edit'}){
2395 print "<td colspan='3'><input type='checkbox' name='EDIT_ADVANCED' $checked{'EDIT_ADVANCED'}{'on'}/> $Lang::tr{'edit advanced settings when done'}</tr></table>";
2396 } else {
2397 print "<td colspan='3'></tr></table>";
2398 }
2399
2400
2401 &Header::closebox();
2402 if ($cgiparams{'KEY'} && $cgiparams{'AUTH'} eq 'psk') {