]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - html/cgi-bin/vpnmain.cgi
suricata: Change midstream policy to "pass-flow"
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / vpnmain.cgi
index 750b69b1dc1a8b871a5be996dcb09d66a4d7c652..9173a85d847f5e17e1b0389144a00fd95c3e7e6c 100644 (file)
@@ -2,7 +2,7 @@
 ###############################################################################
 #                                                                             #
 # IPFire.org - A linux based firewall                                         #
-# Copyright (C) 2007-2019  IPFire Team  info@ipfire.org                       #
+# Copyright (C) 2007-2022  IPFire Team  <info@ipfire.org>                     #
 #                                                                             #
 # This program is free software: you can redistribute it and/or modify        #
 # it under the terms of the GNU General Public License as published by        #
 #                                                                             #
 ###############################################################################
 
+use Data::UUID;
+use MIME::Base64;
 use Net::DNS;
 use File::Copy;
 use File::Temp qw/ tempfile tempdir /;
 use strict;
 use Sort::Naturally;
+use Sys::Hostname;
 # enable only the following on debugging purpose
 #use warnings;
 #use CGI::Carp 'fatalsToBrowser';
@@ -54,7 +57,7 @@ my $errormessage = '';
 my %color = ();
 my %mainsettings = ();
 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
-&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
+&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
 
 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
 
@@ -110,6 +113,7 @@ $cgiparams{'ROOTCERT_EMAIL'} = '';
 $cgiparams{'ROOTCERT_OU'} = '';
 $cgiparams{'ROOTCERT_CITY'} = '';
 $cgiparams{'ROOTCERT_STATE'} = '';
+$cgiparams{'RW_ENDPOINT'} = '';
 $cgiparams{'RW_NET'} = '';
 $cgiparams{'DPD_DELAY'} = '30';
 $cgiparams{'DPD_TIMEOUT'} = '120';
@@ -120,8 +124,38 @@ $cgiparams{'MODE'} = "tunnel";
 $cgiparams{'INTERFACE_MODE'} = "";
 $cgiparams{'INTERFACE_ADDRESS'} = "";
 $cgiparams{'INTERFACE_MTU'} = 1500;
+$cgiparams{'DNS_SERVERS'} = "";
 &Header::getcgihash(\%cgiparams, {'wantfile' => 1, 'filevar' => 'FH'});
 
+my %APPLE_CIPHERS = (
+       "aes256gcm128" => "AES-256-GCM",
+       "aes128gcm128" => "AES-128-GCM",
+       "aes256"       => "AES-256",
+       "aes128"       => "AES-128",
+       "3des"         => "3DES",
+);
+
+my %APPLE_INTEGRITIES = (
+       "sha2_512" => "SHA2-512",
+       "sha2_384" => "SHA2-384",
+       "sha2_256" => "SHA2-256",
+       "sha1"     => "SHA1-160",
+);
+
+my %APPLE_DH_GROUPS = (
+       "768" => 1,
+       "1024" => 2,
+       "1536" => 5,
+       "2048" => 14,
+       "3072" => 15,
+       "4096" => 16,
+       "6144" => 17,
+       "8192" => 18,
+       "e256" => 19,
+       "e384" => 20,
+       "e521" => 21,
+);
+
 ###
 ### Useful functions
 ###
@@ -159,7 +193,7 @@ sub cleanssldatabase {
                close FILE;
        }
        if (open(FILE, ">${General::swroot}/certs/index.txt.attr")) {
-               print FILE "";
+               print FILE "unique_subject = yes";
                close FILE;
        }
        unlink ("${General::swroot}/certs/index.txt.old");
@@ -174,10 +208,13 @@ sub newcleanssldatabase {
                close FILE;
        }
        if (! -s ">${General::swroot}/certs/index.txt") {
-               system ("touch ${General::swroot}/certs/index.txt");
+               open(FILE, ">${General::swroot}/certs/index.txt");
+               close(FILE);
        }
        if (! -s ">${General::swroot}/certs/index.txt.attr") {
-               system ("touch ${General::swroot}/certs/index.txt.attr");
+               open(FILE, ">${General::swroot}/certs/index.txt.attr");
+               print FILE "unique_subject = yes";
+               close(FILE);
        }
        unlink ("${General::swroot}/certs/index.txt.old");
        unlink ("${General::swroot}/certs/index.txt.attr.old");
@@ -192,13 +229,14 @@ sub callssl ($) {
        my $opt = shift;
        my $retssl = `/usr/bin/openssl $opt 2>&1`; #redirect stderr
        my $ret = '';
-       foreach my $line (split (/\n/, $retssl)) {
-               &General::log("ipsec", "$line") if (0); # 1 for verbose logging
-               $ret .= '<br>'.$line if ( $line =~ /error|unknown/ );
-       }
-       if ($ret) {
-               $ret= &Header::cleanhtml($ret);
+
+       if ($?) {
+               foreach my $line (split (/\n/, $retssl)) {
+                       &General::log("ipsec", "$line") if (0); # 1 for verbose logging
+                       $ret .= '<br>' . &Header::escape($line);
+               }
        }
+
        return $ret ? "$Lang::tr{'openssl produced an error'}: $ret" : '' ;
 }
 ###
@@ -207,7 +245,7 @@ sub callssl ($) {
 sub getCNfromcert ($) {
        #&General::log("ipsec", "Extracting name from $_[0]...");
        my $temp = `/usr/bin/openssl x509 -text -in $_[0]`;
-       $temp =~ /Subject:.*CN = (.*)[\n]/;
+       $temp =~ /Subject:.*CN\s*=\s*(.*)[\n]/;
        $temp = $1;
        $temp =~ s+/Email+, E+;
        $temp =~ s/ ST = / S = /;
@@ -316,6 +354,12 @@ sub writeipsecfiles {
 
                print CONF "\tleftfirewall=yes\n";
                print CONF "\tlefthostaccess=yes\n";
+
+               # Always send the host certificate
+               if ($lconfighash{$key}[3] eq 'host') {
+                       print CONF "\tleftsendcert=always\n";
+               }
+
                print CONF "\tright=$lconfighash{$key}[10]\n";
 
                if ($lconfighash{$key}[3] eq 'net') {
@@ -478,6 +522,13 @@ sub writeipsecfiles {
                # Fragmentation
                print CONF "\tfragmentation=yes\n";
 
+               # DNS Servers for RW
+               if ($lconfighash{$key}[3] eq 'host') {
+                       my @servers = split(/\|/, $lconfighash{$key}[39]);
+
+                       print CONF "\trightdns=" . join(",", @servers) . "\n";
+               }
+
                print CONF "\n";
        } #foreach key
 
@@ -505,19 +556,25 @@ if ($ENV{"REMOTE_ADDR"} eq "") {
 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq '' && $cgiparams{'KEY'} eq '') {
        &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
 
+       if ($cgiparams{'RW_ENDPOINT'} ne '' && !&General::validip($cgiparams{'RW_ENDPOINT'}) && !&General::validfqdn($cgiparams{'RW_ENDPOINT'})) {
+               $errormessage = $Lang::tr{'ipsec invalid ip address or fqdn for rw endpoint'};
+               goto SAVE_ERROR;
+       }
+
        if ( $cgiparams{'RW_NET'} ne '' and !&General::validipandmask($cgiparams{'RW_NET'}) ) {
                $errormessage = $Lang::tr{'urlfilter invalid ip or mask error'};
                goto SAVE_ERROR;
        }
 
        $vpnsettings{'ENABLED'} = $cgiparams{'ENABLED'};
+       $vpnsettings{'RW_ENDPOINT'} = $cgiparams{'RW_ENDPOINT'};
        $vpnsettings{'RW_NET'} = $cgiparams{'RW_NET'};
        &General::writehash("${General::swroot}/vpn/settings", \%vpnsettings);
        &writeipsecfiles();
        if (&vpnenabled) {
-               system('/usr/local/bin/ipsecctrl', 'S');
+               &General::system('/usr/local/bin/ipsecctrl', 'S');
        } else {
-               system('/usr/local/bin/ipsecctrl', 'D');
+               &General::system('/usr/local/bin/ipsecctrl', 'D');
        }
        sleep $sleepDelay;
        SAVE_ERROR:
@@ -542,7 +599,7 @@ if ($cgiparams{'ACTION'} eq $Lang::tr{'save'} && $cgiparams{'TYPE'} eq '' && $cg
        }
        &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
        &writeipsecfiles();
-       system('/usr/local/bin/ipsecctrl', 'R');
+       &General::system('/usr/local/bin/ipsecctrl', 'R');
        sleep $sleepDelay;
 
 ###
@@ -604,7 +661,7 @@ END
                }
        }
 
-       if (ref ($cgiparams{'FH'}) ne 'Fh') {
+       unless (ref ($cgiparams{'FH'})) {
                $errormessage = $Lang::tr{'there was no file upload'};
                goto UPLOADCA_ERROR;
        }
@@ -620,8 +677,7 @@ END
                unlink ($filename);
                goto UPLOADCA_ERROR;
        } else {
-               move($filename, "${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem");
-               if ($? ne 0) {
+               unless(move($filename, "${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem")) {
                        $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
                        unlink ($filename);
                        goto UPLOADCA_ERROR;
@@ -633,7 +689,7 @@ END
        $cahash{$key}[1] = &Header::cleanhtml(getsubjectfromcert ("${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem"));
        &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
 
-       system('/usr/local/bin/ipsecctrl', 'R');
+       &General::system('/usr/local/bin/ipsecctrl', 'R');
        sleep $sleepDelay;
 
        UPLOADCA_ERROR:
@@ -649,8 +705,8 @@ END
                &Header::openpage($Lang::tr{'ipsec'}, 1, '');
                &Header::openbigbox('100%', 'left', '', '');
                &Header::openbox('100%', 'left', "$Lang::tr{'ca certificate'}:");
-               my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
-               $output = &Header::cleanhtml($output,"y");
+               my @output = &General::system_output("/usr/bin/openssl", "x509", "-text", "-in", "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
+               my $output = &Header::cleanhtml(join("", @output) ,"y");
                print "<pre>$output</pre>\n";
                &Header::closebox();
                print "<div align='center'><a href='/cgi-bin/vpnmain.cgi'>$Lang::tr{'back'}</a></div>";
@@ -671,7 +727,9 @@ END
                print "Content-Type: application/force-download\n";
                print "Content-Type: application/octet-stream\r\n";
                print "Content-Disposition: attachment; filename=$cahash{$cgiparams{'KEY'}}[0]cert.pem\r\n\r\n";
-               print `/usr/bin/openssl x509 -in ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem`;
+
+               my @cert = &General::system_output("/usr/bin/openssl", "x509", "-in", "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
+               print "@cert";
                exit(0);
        } else {
                $errormessage = $Lang::tr{'invalid key'};
@@ -686,21 +744,21 @@ END
 
        if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
                foreach my $key (keys %confighash) {
-                       my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem ${General::swroot}/certs/$confighash{$key}[1]cert.pem`;
-                       if ($test =~ /: OK/) {
+                       my @test = &General::system_output("/usr/bin/openssl", "verify", "-CAfile", "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem", "${General::swroot}/certs/$confighash{$key}[1]cert.pem");
+                       if (grep(/: OK/, @test)) {
                                # Delete connection
-                               system('/usr/local/bin/ipsecctrl', 'D', $key) if (&vpnenabled);
                                unlink ("${General::swroot}/certs/$confighash{$key}[1]cert.pem");
                                unlink ("${General::swroot}/certs/$confighash{$key}[1].p12");
                                delete $confighash{$key};
                                &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
                                &writeipsecfiles();
+                               &General::system('/usr/local/bin/ipsecctrl', 'D', $key) if (&vpnenabled);
                        }
                }
                unlink ("${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
                delete $cahash{$cgiparams{'KEY'}};
                &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
-               system('/usr/local/bin/ipsecctrl', 'R');
+               &General::system('/usr/local/bin/ipsecctrl', 'R');
                sleep $sleepDelay;
        } else {
                $errormessage = $Lang::tr{'invalid key'};
@@ -715,8 +773,8 @@ END
        my $assignedcerts = 0;
        if ( -f "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem" ) {
                foreach my $key (keys %confighash) {
-                       my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem ${General::swroot}/certs/$confighash{$key}[1]cert.pem`;
-                       if ($test =~ /: OK/) {
+                       my @test = &General::system_output("/usr/bin/openssl", "verify", "-CAfile", "${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem", "${General::swroot}/certs/$confighash{$key}[1]cert.pem");
+                       if (grep(/: OK/, @test)) {
                                $assignedcerts++;
                        }
                }
@@ -752,7 +810,7 @@ END
                        unlink ("${General::swroot}/ca/$cahash{$cgiparams{'KEY'}}[0]cert.pem");
                        delete $cahash{$cgiparams{'KEY'}};
                        &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
-                       system('/usr/local/bin/ipsecctrl', 'R');
+                       &General::system('/usr/local/bin/ipsecctrl', 'R');
                        sleep $sleepDelay;
                }
        } else {
@@ -764,18 +822,18 @@ END
 ###
 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'} ||
        $cgiparams{'ACTION'} eq $Lang::tr{'show host certificate'}) {
-       my $output;
+       my @output;
        &Header::showhttpheaders();
        &Header::openpage($Lang::tr{'ipsec'}, 1, '');
        &Header::openbigbox('100%', 'left', '', '');
        if ($cgiparams{'ACTION'} eq $Lang::tr{'show root certificate'}) {
                &Header::openbox('100%', 'left', "$Lang::tr{'root certificate'}:");
-               $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/ca/cacert.pem`;
+               @output = &General::system_output("/usr/bin/openssl", "x509", "-text", "-in", "${General::swroot}/ca/cacert.pem");
        } else {
                &Header::openbox('100%', 'left', "$Lang::tr{'host certificate'}:");
-               $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/certs/hostcert.pem`;
+               @output = &General::system_output("/usr/bin/openssl", "x509", "-text", "-in", "${General::swroot}/certs/hostcert.pem");
        }
-       $output = &Header::cleanhtml($output,"y");
+       my $output = &Header::cleanhtml(join("", @output) ,"y");
        print "<pre>$output</pre>\n";
        &Header::closebox();
        print "<div align='center'><a href='/cgi-bin/vpnmain.cgi'>$Lang::tr{'back'}</a></div>";
@@ -790,7 +848,9 @@ END
        if ( -f "${General::swroot}/ca/cacert.pem" ) {
                print "Content-Type: application/force-download\n";
                print "Content-Disposition: attachment; filename=cacert.pem\r\n\r\n";
-               print `/usr/bin/openssl x509 -in ${General::swroot}/ca/cacert.pem`;
+
+               my @cert = &General::system_output("/usr/bin/openssl", "x509", "-in", "${General::swroot}/ca/cacert.pem");
+               print join("", @cert);
                exit(0);
        }
 ###
@@ -800,15 +860,25 @@ END
        if ( -f "${General::swroot}/certs/hostcert.pem" ) {
                print "Content-Type: application/force-download\n";
                print "Content-Disposition: attachment; filename=hostcert.pem\r\n\r\n";
-               print `/usr/bin/openssl x509 -in ${General::swroot}/certs/hostcert.pem`;
+
+               my @cert = &General::system_output("/usr/bin/openssl", "x509", "-in", "${General::swroot}/certs/hostcert.pem");
+               print join("", @cert);
                exit(0);
        }
 ###
+### Regenerate the host certificate
+###
+} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'regenerate host certificate'}) {
+       $errormessage = &regenerate_host_certificate();
+
+###
 ### Form for generating/importing the caroot+host certificate
 ###
 } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'generate root/host certificates'} ||
        $cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
 
+       &newcleanssldatabase();
+
        if (-f "${General::swroot}/ca/cacert.pem") {
                $errormessage = $Lang::tr{'valid root certificate already exists'};
                goto ROOTCERT_SKIP;
@@ -822,15 +892,17 @@ END
                        close IPADDR;
                        chomp ($ipaddr);
                        $cgiparams{'ROOTCERT_HOSTNAME'} = (gethostbyaddr(pack("C4", split(/\./, $ipaddr)), 2))[0];
+                       $cgiparams{'SUBJECTALTNAME'} = "DNS:" . $cgiparams{'ROOTCERT_HOSTNAME'};
                        if ($cgiparams{'ROOTCERT_HOSTNAME'} eq '') {
                                $cgiparams{'ROOTCERT_HOSTNAME'} = $ipaddr;
+                               $cgiparams{'SUBJECTALTNAME'} = "IP:" . $cgiparams{'ROOTCERT_HOSTNAME'};
                        }
                }
                $cgiparams{'ROOTCERT_COUNTRY'} = $vpnsettings{'ROOTCERT_COUNTRY'} if (!$cgiparams{'ROOTCERT_COUNTRY'});
        } elsif ($cgiparams{'ACTION'} eq $Lang::tr{'upload p12 file'}) {
                &General::log("ipsec", "Importing from p12...");
 
-               if (ref ($cgiparams{'FH'}) ne 'Fh') {
+               unless (ref ($cgiparams{'FH'})) {
                        $errormessage = $Lang::tr{'there was no file upload'};
                        goto ROOTCERT_ERROR;
                }
@@ -845,7 +917,7 @@ END
                # Extract the CA certificate from the file
                &General::log("ipsec", "Extracting caroot from p12...");
                if (open(STDIN, "-|")) {
-                       my $opt = " pkcs12 -cacerts -nokeys";
+                       my $opt = " pkcs12 -legacy -cacerts -nokeys";
                        $opt .= " -in $filename";
                        $opt .= " -out /tmp/newcacert";
                        $errormessage = &callssl ($opt);
@@ -858,7 +930,7 @@ END
                if (!$errormessage) {
                        &General::log("ipsec", "Extracting host cert from p12...");
                        if (open(STDIN, "-|")) {
-                               my $opt = " pkcs12 -clcerts -nokeys";
+                               my $opt = " pkcs12 -legacy -clcerts -nokeys";
                                $opt .= " -in $filename";
                                $opt .= " -out /tmp/newhostcert";
                                $errormessage = &callssl ($opt);
@@ -872,7 +944,7 @@ END
                if (!$errormessage) {
                        &General::log("ipsec", "Extracting private key from p12...");
                        if (open(STDIN, "-|")) {
-                               my $opt = " pkcs12 -nocerts -nodes";
+                               my $opt = " pkcs12 -legacy -nocerts -nodes";
                                $opt .= " -in $filename";
                                $opt .= " -out /tmp/newhostkey";
                                $errormessage = &callssl ($opt);
@@ -884,20 +956,23 @@ END
 
                if (!$errormessage) {
                        &General::log("ipsec", "Moving cacert...");
-                       move("/tmp/newcacert", "${General::swroot}/ca/cacert.pem");
-                       $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
+                       unless(move("/tmp/newcacert", "${General::swroot}/ca/cacert.pem")) {
+                               $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
+                       }
                }
 
                if (!$errormessage) {
                        &General::log("ipsec", "Moving host cert...");
-                       move("/tmp/newhostcert", "${General::swroot}/certs/hostcert.pem");
-                       $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
+                       unless(move("/tmp/newhostcert", "${General::swroot}/certs/hostcert.pem")) {
+                               $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
+                       }
                }
 
                if (!$errormessage) {
                        &General::log("ipsec", "Moving private key...");
-                       move("/tmp/newhostkey", "${General::swroot}/certs/hostkey.pem");
-                       $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
+                       unless(move("/tmp/newhostkey", "${General::swroot}/certs/hostkey.pem")) {
+                               $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
+                       }
                }
 
                #cleanup temp files
@@ -975,6 +1050,11 @@ END
                #       IP: an IP address
                # example: email:franck@foo.com,IP:10.0.0.10,DNS:franck.foo.com
 
+               if ($cgiparams{'SUBJECTALTNAME'} eq '') {
+                       $errormessage = $Lang::tr{'vpn subjectaltname missing'};
+                       goto ROOTCERT_ERROR;
+               }
+
                if ($cgiparams{'SUBJECTALTNAME'} ne '' && $cgiparams{'SUBJECTALTNAME'} !~ /^(email|URI|DNS|RID|IP):[a-zA-Z0-9 :\/,\.\-_@]*$/) {
                        $errormessage = $Lang::tr{'vpn altname syntax'};
                        goto VPNCONF_ERROR;
@@ -1000,7 +1080,7 @@ END
                        &General::log("ipsec", "Creating cacert...");
                        if (open(STDIN, "-|")) {
                                my $opt = " req -x509 -sha256 -nodes";
-                               $opt .= " -days 999999";
+                               $opt .= " -days 3650";
                                $opt .= " -newkey rsa:4096";
                                $opt .= " -keyout ${General::swroot}/private/cakey.pem";
                                $opt .= " -out ${General::swroot}/ca/cacert.pem";
@@ -1023,7 +1103,7 @@ END
                        &General::log("ipsec", "Creating host cert...");
                        if (open(STDIN, "-|")) {
                                my $opt = " req -sha256 -nodes";
-                               $opt .= " -newkey rsa:2048";
+                               $opt .= " -newkey rsa:4096";
                                $opt .= " -keyout ${General::swroot}/certs/hostkey.pem";
                                $opt .= " -out ${General::swroot}/certs/hostreq.pem";
                                $errormessage = &callssl ($opt);
@@ -1058,7 +1138,7 @@ END
                        print $fh "subjectAltName=$cgiparams{'SUBJECTALTNAME'}" if ($cgiparams{'SUBJECTALTNAME'});
                        close ($fh);
 
-                       my $opt = " ca -md sha256 -days 999999";
+                       my $opt = " ca -md sha256 -days 825";
                        $opt .= " -batch -notext";
                        $opt .= " -in ${General::swroot}/certs/hostreq.pem";
                        $opt .= " -out ${General::swroot}/certs/hostcert.pem";
@@ -1129,7 +1209,7 @@ END
        }
        print <<END
                </select></td></tr>
-       <tr><td class='base'>$Lang::tr{'vpn subjectaltname'} (subjectAltName=email:*,URI:*,DNS:*,RID:*)</td>
+       <tr><td class='base'>$Lang::tr{'vpn subjectaltname'} (subjectAltName=email:*,URI:*,DNS:*,RID:*)&nbsp;<img src='/blob.gif' alt='*' /></td>
        <td class='base' nowrap='nowrap'><input type='text' name='SUBJECTALTNAME' value='$cgiparams{'SUBJECTALTNAME'}' size='32' /></td></tr>
        <tr><td>&nbsp;</td>
                <td><br /><input type='submit' name='ACTION' value='$Lang::tr{'generate root/host certificates'}' /><br /><br /></td></tr>
@@ -1156,7 +1236,7 @@ END
 
        ROOTCERT_SUCCESS:
        if (&vpnenabled) {
-               system('/usr/local/bin/ipsecctrl', 'S');
+               &General::system('/usr/local/bin/ipsecctrl', 'S');
                sleep $sleepDelay;
        }
        ROOTCERT_SKIP:
@@ -1168,9 +1248,279 @@ END
        print "Content-Type: application/force-download\n";
        print "Content-Disposition: attachment; filename=" . $confighash{$cgiparams{'KEY'}}[1] . ".p12\r\n";
        print "Content-Type: application/octet-stream\r\n\r\n";
-       print `/bin/cat ${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1].p12`;
+
+       open(FILE, "${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1].p12");
+       my @p12 = <FILE>;
+       close(FILE);
+       print join("", @p12);
+
        exit (0);
 
+# Export Apple profile to browser
+} elsif ($cgiparams{'ACTION'} eq $Lang::tr{'download apple profile'}) {
+       # Read global configuration
+       &General::readhash("${General::swroot}/vpn/settings", \%vpnsettings);
+
+       # Read connections
+       &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
+       my $key = $cgiparams{'KEY'};
+
+       # Create a UUID generator
+       my $uuid = Data::UUID->new();
+
+       my $uuid1 = $uuid->create_str();
+       my $uuid2 = $uuid->create_str();
+
+       my $ca = "";
+       my $ca_uuid = $uuid->create_str();
+
+       my $cert = "";
+       my $cert_uuid = $uuid->create_str();
+
+       # Read and encode the CA & certificate
+       if ($confighash{$key}[4] eq "cert") {
+               my $ca_path = "${General::swroot}/ca/cacert.pem";
+               my $cert_path = "${General::swroot}/certs/$confighash{$key}[1].p12";
+
+               # Read the CA and encode it into Base64
+               open(CA, "<${ca_path}");
+               local($/) = undef; # slurp
+               $ca = MIME::Base64::encode_base64(<CA>);
+               close(CA);
+
+               # Read certificate and encode it into Base64
+               open(CERT, "<${cert_path}");
+               local($/) = undef; # slurp
+               $cert = MIME::Base64::encode_base64(<CERT>);
+               close(CERT);
+       }
+
+       print "Content-Type: application/octet-stream\n";
+       print "Content-Disposition: attachment; filename=" . $confighash{$key}[1] . ".mobileconfig\n";
+       print "\n"; # end headers
+
+       # Use our own FQDN if nothing else is configured
+       my $endpoint = ($vpnsettings{'RW_ENDPOINT'} ne "") ? $vpnsettings{'RW_ENDPOINT'} : &hostname();
+
+       print "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
+       print "<plist version=\"1.0\">\n";
+       print " <dict>\n";
+        print "                <key>PayloadDisplayName</key>\n";
+       print "         <string>$confighash{$key}[1]</string>\n";
+       print "         <key>PayloadIdentifier</key>\n";
+       print "         <string>$confighash{$key}[1]</string>\n";
+       print "         <key>PayloadUUID</key>\n";
+       print "         <string>${uuid1}</string>\n";
+       print "         <key>PayloadType</key>\n";
+       print "         <string>Configuration</string>\n";
+       print "         <key>PayloadVersion</key>\n";
+       print "         <integer>1</integer>\n";
+       print "         <key>PayloadContent</key>\n";
+       print "         <array>\n";
+       print "                 <dict>\n";
+       print "                         <key>PayloadIdentifier</key>\n";
+       print "                         <string>org.example.vpn1.conf1</string>\n";
+       print "                         <key>PayloadUUID</key>\n";
+       print "                         <string>${uuid2}</string>\n";
+       print "                         <key>PayloadType</key>\n";
+       print "                         <string>com.apple.vpn.managed</string>\n";
+       print "                         <key>PayloadVersion</key>\n";
+       print "                         <integer>1</integer>\n";
+       print "                         <key>UserDefinedName</key>\n";
+       print "                         <string>$confighash{$key}[1]</string>\n";
+       print "                         <key>VPNType</key>\n";
+       print "                         <string>IKEv2</string>\n";
+       print "                         <key>IKEv2</key>\n";
+       print "                         <dict>\n";
+       print "                                 <key>RemoteAddress</key>\n";
+       print "                                 <string>$endpoint</string>\n";
+
+       # PFS
+       my $pfs = $confighash{$key}[28];
+       if ($pfs eq "on") {
+               print "                                 <key>EnablePFS</key>\n";
+               print "                                 <true/>\n";
+       }
+
+       # IKE Cipher Suite
+       print "                                 <key>IKESecurityAssociationParameters</key>\n";
+       print "                                 <dict>\n";
+
+       # Encryption
+       foreach my $cipher (split(/\|/,$confighash{$key}[18])) {
+               # Skip all unsupported ciphers
+               next unless (exists $APPLE_CIPHERS{$cipher});
+
+               print "                                         <key>EncryptionAlgorithm</key>\n";
+               print "                                         <string>$APPLE_CIPHERS{$cipher}</string>\n";
+               last;
+       }
+
+       # Integrity
+       foreach my $integrity (split(/\|/,$confighash{$key}[19])) {
+               # Skip all unsupported algorithms
+               next unless (exists $APPLE_INTEGRITIES{$integrity});
+
+               print "                                         <key>IntegrityAlgorithm</key>\n";
+               print "                                         <string>$APPLE_INTEGRITIES{$integrity}</string>\n";
+               last;
+       }
+
+       # Diffie Hellman Groups
+       foreach my $group (split(/\|/,$confighash{$key}[20])) {
+               # Skip all unsupported algorithms
+               next unless (exists $APPLE_DH_GROUPS{$group});
+
+               print "                                         <key>DiffieHellmanGroup</key>\n";
+               print "                                         <string>$APPLE_DH_GROUPS{$group}</string>\n";
+               last;
+       }
+
+       # Lifetime
+       my $lifetime = $confighash{$key}[16] * 60;
+       print "                                         <key>LifeTimeInMinutes</key>\n";
+       print "                                         <integer>$lifetime</integer>\n";
+       print "                                 </dict>\n";
+
+       # ESP Cipher Suite
+       print "                                 <key>ChildSecurityAssociationParameters</key>\n";
+       print "                                 <dict>\n";
+
+       # Encryption
+       foreach my $cipher (split(/\|/,$confighash{$key}[21])) {
+               # Skip all unsupported ciphers
+               next unless (exists $APPLE_CIPHERS{$cipher});
+
+               print "                                         <key>EncryptionAlgorithm</key>\n";
+               print "                                         <string>$APPLE_CIPHERS{$cipher}</string>\n";
+               last;
+       }
+
+       # Integrity
+       foreach my $integrity (split(/\|/,$confighash{$key}[22])) {
+               # Skip all unsupported algorithms
+               next unless (exists $APPLE_INTEGRITIES{$integrity});
+
+               print "                                         <key>IntegrityAlgorithm</key>\n";
+               print "                                         <string>$APPLE_INTEGRITIES{$integrity}</string>\n";
+               last;
+       }
+
+       # Diffie Hellman Groups
+       foreach my $group (split(/\|/,$confighash{$key}[23])) {
+               # Skip all unsupported algorithms
+               next unless (exists $APPLE_DH_GROUPS{$group});
+
+               print "                                         <key>DiffieHellmanGroup</key>\n";
+               print "                                         <string>$APPLE_DH_GROUPS{$group}</string>\n";
+               last;
+       }
+
+       # Lifetime
+       my $lifetime = $confighash{$key}[17] * 60;
+       print "                                         <key>LifeTimeInMinutes</key>\n";
+       print "                                         <integer>$lifetime</integer>\n";
+       print "                                 </dict>\n";
+
+
+       # Left ID
+       if ($confighash{$key}[9]) {
+               my $leftid = $confighash{$key}[9];
+
+               # Strip leading @ from FQDNs
+               if ($leftid =~ m/^@(.*)$/) {
+                       $leftid = $1;
+               }
+
+               print "                                 <key>LocalIdentifier</key>\n";
+               print "                                 <string>$leftid</string>\n";
+       }
+
+       # Right ID
+       if ($confighash{$key}[7]) {
+               my $rightid = $confighash{$key}[7];
+
+               # Strip leading @ from FQDNs
+               if ($rightid =~ m/^@(.*)$/) {
+                       $rightid = $1;
+               }
+
+               print "                                 <key>RemoteIdentifier</key>\n";
+               print "                                 <string>$rightid</string>\n";
+       }
+
+       if ($confighash{$key}[4] eq "cert") {
+               print "                                 <key>AuthenticationMethod</key>\n";
+               print "                                 <string>Certificate</string>\n";
+
+               print "                                 <key>PayloadCertificateUUID</key>\n";
+               print "                                 <string>${cert_uuid}</string>\n";
+       } else {
+               print "                                 <key>AuthenticationMethod</key>\n";
+               print "                                 <string>SharedSecret</string>\n";
+               print "                                 <key>SharedSecret</key>\n";
+               print "                                 <string>$confighash{$key}[5]</string>\n";
+       }
+
+       print "                                 <key>ExtendedAuthEnabled</key>\n";
+       print "                                 <integer>0</integer>\n";
+
+       # These are not needed, but we provide some default to stop iPhone asking for credentials
+       print "                                 <key>AuthName</key>\n";
+       print "                                 <string>$confighash{$key}[1]</string>\n";
+       print "                                 <key>AuthPassword</key>\n";
+       print "                                 <string></string>\n";
+       print "                         </dict>\n";
+       print "                 </dict>\n";
+
+       if ($confighash{$key}[4] eq "cert") {
+               print "                 <dict>\n";
+               print "                         <key>PayloadIdentifier</key>\n";
+               print "                         <string>org.example.vpn1.client</string>\n";
+               print "                         <key>PayloadDisplayName</key>\n";
+               print "                         <string>$confighash{$key}[1]</string>\n";
+               print "                         <key>PayloadUUID</key>\n";
+               print "                         <string>${cert_uuid}</string>\n";
+               print "                         <key>PayloadType</key>\n";
+               print "                         <string>com.apple.security.pkcs12</string>\n";
+               print "                         <key>PayloadVersion</key>\n";
+               print "                         <integer>1</integer>\n";
+               print "                         <key>PayloadContent</key>\n";
+               print "                         <data>\n";
+
+               foreach (split /\n/,${cert}) {
+                       print "                                 $_\n";
+               }
+
+               print "                         </data>\n";
+               print "                 </dict>\n";
+
+               print "                 <dict>\n";
+               print "                         <key>PayloadIdentifier</key>\n";
+               print "                         <string>org.example.ca</string>\n";
+               print "                         <key>PayloadUUID</key>\n";
+               print "                         <string>${ca_uuid}</string>\n";
+               print "                         <key>PayloadType</key>\n";
+               print "                         <string>com.apple.security.root</string>\n";
+               print "                         <key>PayloadVersion</key>\n";
+               print "                         <integer>1</integer>\n";
+               print "                         <key>PayloadContent</key>\n";
+               print "                         <data>\n";
+
+               foreach (split /\n/,${ca}) {
+                       print "                                 $_\n";
+               }
+
+               print "                         </data>\n";
+               print "                 </dict>\n";
+       }
+
+       print "         </array>\n";
+       print " </dict>\n";
+       print "</plist>\n";
+
+       # Done
+       exit(0);
 ###
 ### Display certificate
 ###
@@ -1182,8 +1532,8 @@ END
                &Header::openpage($Lang::tr{'ipsec'}, 1, '');
                &Header::openbigbox('100%', 'left', '', '');
                &Header::openbox('100%', 'left', "$Lang::tr{'cert'}:");
-               my $output = `/usr/bin/openssl x509 -text -in ${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
-               $output = &Header::cleanhtml($output,"y");
+               my @output = &General::system_output("/usr/bin/openssl", "x509", "-text", "-in", "${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem");
+               my $output = &Header::cleanhtml(join("", @output) ,"y");
                print "<pre>$output</pre>\n";
                &Header::closebox();
                print "<div align='center'><a href='/cgi-bin/vpnmain.cgi'>$Lang::tr{'back'}</a></div>";
@@ -1201,7 +1551,12 @@ END
        if ( -f "${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem") {
                print "Content-Type: application/force-download\n";
                print "Content-Disposition: attachment; filename=" . $confighash{$cgiparams{'KEY'}}[1] . "cert.pem\n\n";
-               print `/bin/cat ${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem`;
+
+               open(FILE, "${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem");
+               my @pem = <FILE>;
+               close(FILE);
+               print "@pem";
+
                exit (0);
        }
 
@@ -1218,12 +1573,12 @@ END
                        $confighash{$cgiparams{'KEY'}}[0] = 'on';
                        &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
                        &writeipsecfiles();
-                       system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'}) if (&vpnenabled);
+                       &General::system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'}) if (&vpnenabled);
                } else {
-                       system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'}) if (&vpnenabled);
                        $confighash{$cgiparams{'KEY'}}[0] = 'off';
                        &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
                        &writeipsecfiles();
+                       &General::system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'}) if (&vpnenabled);
                }
                sleep $sleepDelay;
        } else {
@@ -1239,7 +1594,7 @@ END
 
        if ($confighash{$cgiparams{'KEY'}}) {
                if (&vpnenabled) {
-                       system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'});
+                       &General::system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'});
                        sleep $sleepDelay;
                }
        } else {
@@ -1254,12 +1609,12 @@ END
        &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
 
        if ($confighash{$cgiparams{'KEY'}}) {
-               system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'}) if (&vpnenabled);
                unlink ("${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1]cert.pem");
                unlink ("${General::swroot}/certs/$confighash{$cgiparams{'KEY'}}[1].p12");
                delete $confighash{$cgiparams{'KEY'}};
                &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
                &writeipsecfiles();
+               &General::system('/usr/local/bin/ipsecctrl', 'D', $cgiparams{'KEY'}) if (&vpnenabled);
        } else {
                $errormessage = $Lang::tr{'invalid key'};
        }
@@ -1346,6 +1701,7 @@ END
                $cgiparams{'INTERFACE_MODE'}            = $confighash{$cgiparams{'KEY'}}[36];
                $cgiparams{'INTERFACE_ADDRESS'}         = $confighash{$cgiparams{'KEY'}}[37];
                $cgiparams{'INTERFACE_MTU'}             = $confighash{$cgiparams{'KEY'}}[38];
+               $cgiparams{'DNS_SERVERS'}               = $confighash{$cgiparams{'KEY'}}[39];
 
                if (!$cgiparams{'DPD_DELAY'}) {
                        $cgiparams{'DPD_DELAY'} = 30;
@@ -1479,6 +1835,16 @@ END
                        }
                }
 
+               if ($cgiparams{'TYPE'} eq 'host') {
+                       my @servers = split(",", $cgiparams{'DNS_SERVERS'});
+                       foreach my $server (@servers) {
+                               unless (&Network::check_ip_address($server)) {
+                                       $errormessage = $Lang::tr{'ipsec dns server address is invalid'};
+                                       goto VPNCONF_ERROR;
+                               }
+                       }
+               }
+
                if ($cgiparams{'ENABLED'} !~ /^(on|off)$/) {
                        $errormessage = $Lang::tr{'invalid input'};
                        goto VPNCONF_ERROR;
@@ -1531,7 +1897,7 @@ END
                        $errormessage = $Lang::tr{'cant change certificates'};
                        goto VPNCONF_ERROR;
                }
-               if (ref ($cgiparams{'FH'}) ne 'Fh') {
+               unless (ref ($cgiparams{'FH'})) {
                        $errormessage = $Lang::tr{'there was no file upload'};
                        goto VPNCONF_ERROR;
                }
@@ -1545,7 +1911,7 @@ END
 
                # Sign the certificate request
                &General::log("ipsec", "Signing your cert $cgiparams{'NAME'}...");
-               my $opt = " ca -md sha256 -days 999999";
+               my $opt = " ca -md sha256 -days 825";
                $opt .= " -batch -notext";
                $opt .= " -in $filename";
                $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}cert.pem";
@@ -1568,7 +1934,7 @@ END
        } elsif ($cgiparams{'AUTH'} eq 'pkcs12') {
                &General::log("ipsec", "Importing from p12...");
 
-               if (ref ($cgiparams{'FH'}) ne 'Fh') {
+               unless (ref ($cgiparams{'FH'})) {
                        $errormessage = $Lang::tr{'there was no file upload'};
                        goto ROOTCERT_ERROR;
                }
@@ -1583,7 +1949,7 @@ END
                # Extract the CA certificate from the file
                &General::log("ipsec", "Extracting caroot from p12...");
                if (open(STDIN, "-|")) {
-                       my $opt = " pkcs12 -cacerts -nokeys";
+                       my $opt = " pkcs12 -legacy -cacerts -nokeys";
                        $opt .= " -in $filename";
                        $opt .= " -out /tmp/newcacert";
                        $errormessage = &callssl ($opt);
@@ -1596,7 +1962,7 @@ END
                if (!$errormessage) {
                        &General::log("ipsec", "Extracting host cert from p12...");
                        if (open(STDIN, "-|")) {
-                               my $opt = " pkcs12 -clcerts -nokeys";
+                               my $opt = " pkcs12 -legacy -clcerts -nokeys";
                                $opt .= " -in $filename";
                                $opt .= " -out /tmp/newhostcert";
                                $errormessage = &callssl ($opt);
@@ -1616,8 +1982,8 @@ END
                                unshift (@names,$cahash{$x}[0]);
                        }
                        if ($casubject) { # a new one!
-                               my $temp = `/usr/bin/openssl x509 -text -in /tmp/newcacert`;
-                               if ($temp !~ /CA:TRUE/i) {
+                               my @temp = &General::system_output("/usr/bin/openssl", "x509", "-text", "-in", "/tmp/newcacert");
+                               if (! grep(/CA:TRUE/, @temp)) {
                                        $errormessage = $Lang::tr{'not a valid ca certificate'};
                                } else {
                                        #compute a name for it
@@ -1625,22 +1991,26 @@ END
                                        while (grep(/Imported-$idx/, @names) ) {$idx++};
                                        $cgiparams{'CA_NAME'}="Imported-$idx";
                                        $cgiparams{'CERT_NAME'}=&Header::cleanhtml(getCNfromcert ('/tmp/newhostcert'));
-                                       move("/tmp/newcacert", "${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem");
-                                       $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
+
+                                       unless(move("/tmp/newcacert", "${General::swroot}/ca/$cgiparams{'CA_NAME'}cert.pem")) {
+                                               $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
+                                       }
+
                                        if (!$errormessage) {
                                                my $key = &General::findhasharraykey (\%cahash);
                                                $cahash{$key}[0] = $cgiparams{'CA_NAME'};
                                                $cahash{$key}[1] = $casubject;
                                                &General::writehasharray("${General::swroot}/vpn/caconfig", \%cahash);
-                                               system('/usr/local/bin/ipsecctrl', 'R');
+                                               &General::system('/usr/local/bin/ipsecctrl', 'R');
                                        }
                                }
                        }
                }
                if (!$errormessage) {
                        &General::log("ipsec", "Moving host cert...");
-                       move("/tmp/newhostcert", "${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
-                       $errormessage = "$Lang::tr{'certificate file move failed'}: $!" if ($? ne 0);
+                       unless(move("/tmp/newhostcert", "${General::swroot}/certs/$cgiparams{'NAME'}cert.pem")) {
+                               $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
+                       }
                }
 
                #cleanup temp files
@@ -1658,7 +2028,7 @@ END
                        $errormessage = $Lang::tr{'cant change certificates'};
                        goto VPNCONF_ERROR;
                }
-               if (ref ($cgiparams{'FH'}) ne 'Fh') {
+               unless (ref ($cgiparams{'FH'})) {
                        $errormessage = $Lang::tr{'there was no file upload'};
                        goto VPNCONF_ERROR;
                }
@@ -1672,12 +2042,12 @@ END
                # Verify the certificate has a valid CA and move it
                &General::log("ipsec", "Validating imported cert against our known CA...");
                my $validca = 1; #assume ok
-               my $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/cacert.pem $filename`;
-               if ($test !~ /: OK/) {
+               my @test = &General::system_output("/usr/bin/openssl", "verify", "-CAfile", "${General::swroot}/ca/cacert.pem", "$filename");
+               if (! grep(/: OK/, @test)) {
                        my $validca = 0;
                        foreach my $key (keys %cahash) {
-                               $test = `/usr/bin/openssl verify -CAfile ${General::swroot}/ca/$cahash{$key}[0]cert.pem $filename`;
-                               if ($test =~ /: OK/) {
+                               @test = &General::system_output("/usr/bin/openssl", "verify", "-CAfile", "${General::swroot}/ca/$cahash{$key}[0]cert.pem", "$filename");
+                               if (grep(/: OK/, @test)) {
                                        $validca = 1;
                                        last;
                                }
@@ -1688,9 +2058,8 @@ END
                        unlink ($filename);
                        goto VPNCONF_ERROR;
                } else {
-                       move($filename, "${General::swroot}/certs/$cgiparams{'NAME'}cert.pem");
-                       if ($? ne 0) {
-                               $errormessage = "$Lang::tr{'certificate file move failed'}: $!";
+                       unless (move($filename, "${General::swroot}/certs/$cgiparams{'NAME'}cert.pem")) {
+                               $errormessage = "$Lang::tr{'certificate file move failed'} ($filename): $!";
                                unlink ($filename);
                                goto VPNCONF_ERROR;
                        }
@@ -1779,8 +2148,8 @@ END
                &General::log("ipsec", "Creating a cert...");
 
                if (open(STDIN, "-|")) {
-                       my $opt = " req -nodes -rand /proc/interrupts:/proc/net/rt_cache";
-                       $opt .= " -newkey rsa:2048";
+                       my $opt = " req -nodes";
+                       $opt .= " -newkey rsa:4096";
                        $opt .= " -keyout ${General::swroot}/certs/$cgiparams{'NAME'}key.pem";
                        $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}req.pem";
 
@@ -1818,7 +2187,7 @@ END
                print $fh "subjectAltName=$cgiparams{'SUBJECTALTNAME'}" if ($cgiparams{'SUBJECTALTNAME'});
                close ($fh);
 
-               my $opt = " ca -md sha256 -days 999999 -batch -notext";
+               my $opt = " ca -md sha256 -days 825 -batch -notext";
                $opt .= " -in ${General::swroot}/certs/$cgiparams{'NAME'}req.pem";
                $opt .= " -out ${General::swroot}/certs/$cgiparams{'NAME'}cert.pem";
                $opt .= " -extfile $v3extname";
@@ -1838,7 +2207,7 @@ END
 
                # Create the pkcs12 file
                &General::log("ipsec", "Packing a pkcs12 file...");
-               $opt = " pkcs12 -export";
+               $opt = " pkcs12 -legacy -export";
                $opt .= " -inkey ${General::swroot}/certs/$cgiparams{'NAME'}key.pem";
                $opt .= " -in ${General::swroot}/certs/$cgiparams{'NAME'}cert.pem";
                $opt .= " -name \"$cgiparams{'NAME'}\"";
@@ -1881,7 +2250,7 @@ END
        my $key = $cgiparams{'KEY'};
        if (! $key) {
                $key = &General::findhasharraykey (\%confighash);
-               foreach my $i (0 .. 38) { $confighash{$key}[$i] = "";}
+               foreach my $i (0 .. 39) { $confighash{$key}[$i] = "";}
        }
        $confighash{$key}[0] = $cgiparams{'ENABLED'};
        $confighash{$key}[1] = $cgiparams{'NAME'};
@@ -1932,6 +2301,7 @@ END
        $confighash{$key}[36] = $cgiparams{'INTERFACE_MODE'};
        $confighash{$key}[37] = $cgiparams{'INTERFACE_ADDRESS'};
        $confighash{$key}[38] = $cgiparams{'INTERFACE_MTU'};
+       $confighash{$key}[39] = join("|", split(",", $cgiparams{'DNS_SERVERS'}));
 
        # free unused fields!
        $confighash{$key}[15] = 'off';
@@ -1939,7 +2309,7 @@ END
        &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
        &writeipsecfiles();
        if (&vpnenabled) {
-               system('/usr/local/bin/ipsecctrl', 'S', $key);
+               &General::system('/usr/local/bin/ipsecctrl', 'S', $key);
                sleep $sleepDelay;
        }
        if ($cgiparams{'EDIT_ADVANCED'} eq 'on') {
@@ -2000,11 +2370,11 @@ END
        #use default advanced value
        $cgiparams{'IKE_ENCRYPTION'}    = 'chacha20poly1305|aes256gcm128|aes256gcm96|aes256gcm64|aes256|aes192gcm128|aes192gcm96|aes192gcm64|aes192|aes128gcm128|aes128gcm96|aes128gcm64|aes128'; #[18];
        $cgiparams{'IKE_INTEGRITY'}             = 'sha2_512|sha2_256'; #[19];
-       $cgiparams{'IKE_GROUPTYPE'}             = 'curve25519|4096|3072|2048'; #[20];
+       $cgiparams{'IKE_GROUPTYPE'}             = 'curve448|curve25519|e521|e384|4096|3072'; #[20];
        $cgiparams{'IKE_LIFETIME'}              = '3'; #[16];
        $cgiparams{'ESP_ENCRYPTION'}    = 'chacha20poly1305|aes256gcm128|aes256gcm96|aes256gcm64|aes256|aes192gcm128|aes192gcm96|aes192gcm64|aes192|aes128gcm128|aes128gcm96|aes128gcm64|aes128'; #[21];
        $cgiparams{'ESP_INTEGRITY'}             = 'sha2_512|sha2_256'; #[22];
-       $cgiparams{'ESP_GROUPTYPE'}             = 'curve25519|4096|3072|2048'; #[23];
+       $cgiparams{'ESP_GROUPTYPE'}             = 'curve448|curve25519|e521|e384|4096|3072'; #[23];
        $cgiparams{'ESP_KEYLIFE'}               = '1'; #[17];
        $cgiparams{'COMPRESSION'}               = 'off'; #[13];
        $cgiparams{'ONLY_PROPOSED'}             = 'on'; #[24];
@@ -2014,6 +2384,7 @@ END
        $cgiparams{'INTERFACE_MODE'}            = "";
        $cgiparams{'INTERFACE_ADDRESS'}         = "";
        $cgiparams{'INTERFACE_MTU'}             = 1500;
+       $cgiparams{'DNS_SERVERS'}               = "";
 }
 
 VPNCONF_ERROR:
@@ -2110,11 +2481,8 @@ END
 EOF
        }
 
-       my $disabled;
-       my $blob;
-       if ($cgiparams{'TYPE'} eq 'host') {
-               $disabled = "disabled='disabled'";
-       } elsif ($cgiparams{'TYPE'} eq 'net') {
+       my $blob = "";
+       if ($cgiparams{'TYPE'} eq 'net') {
                $blob = "<img src='/blob.gif' alt='*' />";
        };
 
@@ -2124,6 +2492,9 @@ EOF
        my @remote_subnets = split(/\|/, $cgiparams{'REMOTE_SUBNET'});
        my $remote_subnets = join(",", @remote_subnets);
 
+       my @dns_servers = split(/\|/, $cgiparams{'DNS_SERVERS'});
+       my $dns_servers = join(",", @dns_servers);
+
        print <<END;
        <tr>
                <td width='20%'>$Lang::tr{'enabled'}</td>
@@ -2159,10 +2530,26 @@ END
                <td width='30%'>
                        <input type='text' name='LOCAL_SUBNET' value='$local_subnets' size="25" />
                </td>
-               <td class='boldbase' nowrap='nowrap' width='20%'>$Lang::tr{'remote subnet'}&nbsp;$blob</td>
+END
+
+       if ($cgiparams{'TYPE'} eq "net") {
+               print <<END;
+               <td class='boldbase' nowrap='nowrap' width='20%'>$Lang::tr{'remote subnet'}&nbsp;<img src='/blob.gif' alt='*' /></td>
+               <td width='30%'>
+                       <input type='text' name='REMOTE_SUBNET' value='$remote_subnets' size="25" />
+               </td>
+END
+
+       } elsif ($cgiparams{'TYPE'} eq "host") {
+               print <<END;
+               <td class='boldbase' nowrap='nowrap' width='20%'>$Lang::tr{'dns servers'}:</td>
                <td width='30%'>
-                       <input $disabled type='text' name='REMOTE_SUBNET' value='$remote_subnets' size="25" />
+                       <input type='text' name='DNS_SERVERS' value='$dns_servers' size="25" />
                </td>
+END
+       }
+
+       print <<END;
        </tr>
        <tr>
                <td class='boldbase' width='20%'>$Lang::tr{'vpn local id'}:</td>
@@ -2368,7 +2755,7 @@ if(($cgiparams{'ACTION'} eq $Lang::tr{'advanced'}) ||
                        goto ADVANCED_ERROR;
                }
                foreach my $val (@temp) {
-                       if ($val !~ /^(curve25519|e521|e384|e256|e224|e192|e512bp|e384bp|e256bp|e224bp|768|1024|1536|2048|3072|4096|6144|8192)$/) {
+                       if ($val !~ /^(curve448|curve25519|e521|e384|e256|e224|e192|e512bp|e384bp|e256bp|e224bp|768|1024|1536|2048|3072|4096|6144|8192)$/) {
                                $errormessage = $Lang::tr{'invalid input'};
                                goto ADVANCED_ERROR;
                        }
@@ -2377,8 +2764,8 @@ if(($cgiparams{'ACTION'} eq $Lang::tr{'advanced'}) ||
                        $errormessage = $Lang::tr{'invalid input for ike lifetime'};
                        goto ADVANCED_ERROR;
                }
-               if ($cgiparams{'IKE_LIFETIME'} < 1 || $cgiparams{'IKE_LIFETIME'} > 8) {
-                       $errormessage = $Lang::tr{'ike lifetime should be between 1 and 8 hours'};
+               if ($cgiparams{'IKE_LIFETIME'} < 1 || $cgiparams{'IKE_LIFETIME'} > 24) {
+                       $errormessage = $Lang::tr{'ike lifetime should be between 1 and 24 hours'};
                        goto ADVANCED_ERROR;
                }
                @temp = split('\|', $cgiparams{'ESP_ENCRYPTION'});
@@ -2409,7 +2796,7 @@ if(($cgiparams{'ACTION'} eq $Lang::tr{'advanced'}) ||
                        goto ADVANCED_ERROR;
                }
                foreach my $val (@temp) {
-                       if ($val !~ /^(curve25519|e521|e384|e256|e224|e192|e512bp|e384bp|e256bp|e224bp|768|1024|1536|2048|3072|4096|6144|8192|none)$/) {
+                       if ($val !~ /^(curve448|curve25519|e521|e384|e256|e224|e192|e512bp|e384bp|e256bp|e224bp|768|1024|1536|2048|3072|4096|6144|8192|none)$/) {
                                $errormessage = $Lang::tr{'invalid input'};
                                goto ADVANCED_ERROR;
                        }
@@ -2468,7 +2855,7 @@ if(($cgiparams{'ACTION'} eq $Lang::tr{'advanced'}) ||
                &General::writehasharray("${General::swroot}/vpn/config", \%confighash);
                &writeipsecfiles();
                if (&vpnenabled) {
-                       system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'});
+                       &General::system('/usr/local/bin/ipsecctrl', 'S', $cgiparams{'KEY'});
                        sleep $sleepDelay;
                }
                goto ADVANCED_END;
@@ -2498,6 +2885,7 @@ if(($cgiparams{'ACTION'} eq $Lang::tr{'advanced'}) ||
                $cgiparams{'INTERFACE_MODE'}            = $confighash{$cgiparams{'KEY'}}[36];
                $cgiparams{'INTERFACE_ADDRESS'}         = $confighash{$cgiparams{'KEY'}}[37];
                $cgiparams{'INTERFACE_MTU'}             = $confighash{$cgiparams{'KEY'}}[38];
+               $cgiparams{'DNS_SERVERS'}               = $confighash{$cgiparams{'KEY'}}[39];
 
                if (!$cgiparams{'DPD_DELAY'}) {
                        $cgiparams{'DPD_DELAY'} = 30;
@@ -2548,6 +2936,7 @@ if(($cgiparams{'ACTION'} eq $Lang::tr{'advanced'}) ||
        $checked{'IKE_INTEGRITY'}{'aesxcbc'} = '';
        @temp = split('\|', $cgiparams{'IKE_INTEGRITY'});
        foreach my $key (@temp) {$checked{'IKE_INTEGRITY'}{$key} = "selected='selected'"; }
+       $checked{'IKE_GROUPTYPE'}{'curve448'} = '';
        $checked{'IKE_GROUPTYPE'}{'curve25519'} = '';
        $checked{'IKE_GROUPTYPE'}{'768'} = '';
        $checked{'IKE_GROUPTYPE'}{'1024'} = '';
@@ -2587,6 +2976,7 @@ if(($cgiparams{'ACTION'} eq $Lang::tr{'advanced'}) ||
        $checked{'ESP_INTEGRITY'}{'aesxcbc'} = '';
        @temp = split('\|', $cgiparams{'ESP_INTEGRITY'});
        foreach my $key (@temp) {$checked{'ESP_INTEGRITY'}{$key} = "selected='selected'"; }
+       $checked{'ESP_GROUPTYPE'}{'curve448'} = '';
        $checked{'ESP_GROUPTYPE'}{'curve25519'} = '';
        $checked{'ESP_GROUPTYPE'}{'768'} = '';
        $checked{'ESP_GROUPTYPE'}{'1024'} = '';
@@ -2751,7 +3141,8 @@ if(($cgiparams{'ACTION'} eq $Lang::tr{'advanced'}) ||
                        <td class='boldbase' width="15%">$Lang::tr{'grouptype'}</td>
                        <td class='boldbase'>
                                <select name='IKE_GROUPTYPE' multiple='multiple' size='6' style='width: 100%'>
-                                       <option value='curve25519' $checked{'IKE_GROUPTYPE'}{'curve25519'}>Curve 25519 (256 bit)</option>
+                                       <option value='curve448' $checked{'IKE_GROUPTYPE'}{'curve448'}>Curve 448 (224 bit)</option>
+                                       <option value='curve25519' $checked{'IKE_GROUPTYPE'}{'curve25519'}>Curve 25519 (128 bit)</option>
                                        <option value='e521' $checked{'IKE_GROUPTYPE'}{'e521'}>ECP-521 (NIST)</option>
                                        <option value='e512bp' $checked{'IKE_GROUPTYPE'}{'e512bp'}>ECP-512 (Brainpool)</option>
                                        <option value='e384' $checked{'IKE_GROUPTYPE'}{'e384'}>ECP-384 (NIST)</option>
@@ -2765,15 +3156,16 @@ if(($cgiparams{'ACTION'} eq $Lang::tr{'advanced'}) ||
                                        <option value='6144' $checked{'IKE_GROUPTYPE'}{'6144'}>MODP-6144</option>
                                        <option value='4096' $checked{'IKE_GROUPTYPE'}{'4096'}>MODP-4096</option>
                                        <option value='3072' $checked{'IKE_GROUPTYPE'}{'3072'}>MODP-3072</option>
-                                       <option value='2048' $checked{'IKE_GROUPTYPE'}{'2048'}>MODP-2048</option>
-                                       <option value='1536' $checked{'IKE_GROUPTYPE'}{'1536'}>MODP-1536</option>
+                                       <option value='2048' $checked{'IKE_GROUPTYPE'}{'2048'}>MODP-2048 ($Lang::tr{'vpn weak'})</option>
+                                       <option value='1536' $checked{'IKE_GROUPTYPE'}{'1536'}>MODP-1536 ($Lang::tr{'vpn broken'})</option>
                                        <option value='1024' $checked{'IKE_GROUPTYPE'}{'1024'}>MODP-1024 ($Lang::tr{'vpn broken'})</option>
                                        <option value='768' $checked{'IKE_GROUPTYPE'}{'768'}>MODP-768 ($Lang::tr{'vpn broken'})</option>
                                </select>
                        </td>
                        <td class='boldbase'>
                                <select name='ESP_GROUPTYPE' multiple='multiple' size='6' style='width: 100%'>
-                                       <option value='curve25519' $checked{'ESP_GROUPTYPE'}{'curve25519'}>Curve 25519 (256 bit)</option>
+                                       <option value='curve448' $checked{'ESP_GROUPTYPE'}{'curve448'}>Curve 448 (224 bit)</option>
+                                       <option value='curve25519' $checked{'ESP_GROUPTYPE'}{'curve25519'}>Curve 25519 (128 bit)</option>
                                        <option value='e521' $checked{'ESP_GROUPTYPE'}{'e521'}>ECP-521 (NIST)</option>
                                        <option value='e512bp' $checked{'ESP_GROUPTYPE'}{'e512bp'}>ECP-512 (Brainpool)</option>
                                        <option value='e384' $checked{'ESP_GROUPTYPE'}{'e384'}>ECP-384 (NIST)</option>
@@ -2787,8 +3179,8 @@ if(($cgiparams{'ACTION'} eq $Lang::tr{'advanced'}) ||
                                        <option value='6144' $checked{'ESP_GROUPTYPE'}{'6144'}>MODP-6144</option>
                                        <option value='4096' $checked{'ESP_GROUPTYPE'}{'4096'}>MODP-4096</option>
                                        <option value='3072' $checked{'ESP_GROUPTYPE'}{'3072'}>MODP-3072</option>
-                                       <option value='2048' $checked{'ESP_GROUPTYPE'}{'2048'}>MODP-2048</option>
-                                       <option value='1536' $checked{'ESP_GROUPTYPE'}{'1536'}>MODP-1536</option>
+                                       <option value='2048' $checked{'ESP_GROUPTYPE'}{'2048'}>MODP-2048 ($Lang::tr{'vpn weak'})</option>
+                                       <option value='1536' $checked{'ESP_GROUPTYPE'}{'1536'}>MODP-1536 ($Lang::tr{'vpn broken'})</option>
                                        <option value='1024' $checked{'ESP_GROUPTYPE'}{'1024'}>MODP-1024 ($Lang::tr{'vpn broken'})</option>
                                        <option value='768' $checked{'ESP_GROUPTYPE'}{'768'}>MODP-768 ($Lang::tr{'vpn broken'})</option>
                                        <option value='none' $checked{'ESP_GROUPTYPE'}{'none'}>- $Lang::tr{'none'} -</option>
@@ -2912,7 +3304,7 @@ EOF
        &General::readhasharray("${General::swroot}/vpn/config", \%confighash);
        $cgiparams{'CA_NAME'} = '';
 
-       my @status = `/usr/local/bin/ipsecctrl I 2>/dev/null`;
+       my @status = &General::system_output("/usr/local/bin/ipsecctrl", "I");
 
        $checked{'ENABLED'} = $cgiparams{'ENABLED'} eq 'on' ? "checked='checked'" : '';
 
@@ -2949,6 +3341,10 @@ EOF
                                <input type='checkbox' name='ENABLED' $checked{'ENABLED'} />
                        </td>
                </tr>
+               <tr>
+                       <td class='base' nowrap='nowrap' width="60%">$Lang::tr{'ipsec roadwarrior endpoint'}:</td>
+                       <td width="40%"><input type='text' name='RW_ENDPOINT' value='$cgiparams{'RW_ENDPOINT'}' /></td>
+               </tr>
                <tr>
                        <td class='base' nowrap='nowrap' width="60%">$Lang::tr{'host to net vpn'}:</td>
                        <td width="40%"><input type='text' name='RW_NET' value='$cgiparams{'RW_NET'}' /></td>
@@ -2971,7 +3367,7 @@ END
        <th width='23%' class='boldbase' align='center'><b>$Lang::tr{'common name'}</b></th>
        <th width='30%' class='boldbase' align='center'><b>$Lang::tr{'remark'}</b></th>
        <th width='10%' class='boldbase' align='center'><b>$Lang::tr{'status'}</b></th>
-       <th class='boldbase' align='center' colspan='6'><b>$Lang::tr{'action'}</b></th>
+       <th class='boldbase' align='center' colspan='7'><b>$Lang::tr{'action'}</b></th>
        </tr>
 END
 ;
@@ -3008,6 +3404,7 @@ END
                ($line =~ /$confighash{$key}[1]\{.*INSTALLED/)) {
                        $col1="bgcolor='${Header::colourgreen}'";
                        $active = "<b><font color='#FFFFFF'>$Lang::tr{'capsopen'}</font></b>";
+                       last;
                } elsif ($line =~ /$confighash{$key}[1]\[.*CONNECTING/) {
                        $col1="bgcolor='${Header::colourorange}'";
                        $active = "<b><font color='#FFFFFF'>$Lang::tr{'vpn connecting'}</font></b>";
@@ -3071,6 +3468,22 @@ END
        } else {
                print "<td width='2%' $col>&nbsp;</td>";
        }
+
+       # Apple Profile
+       if ($confighash{$key}[3] eq 'host') {
+               print <<END;
+               <td align='center' $col>
+                       <form method='post' action='$ENV{'SCRIPT_NAME'}'>
+                       <input type='image' name='$Lang::tr{'download apple profile'}' src='/images/apple.png' alt='$Lang::tr{'download apple profile'}' title='$Lang::tr{'download apple profile'}' />
+                       <input type='hidden' name='ACTION' value='$Lang::tr{'download apple profile'}' />
+                       <input type='hidden' name='KEY' value='$key' />
+                       </form>
+               </td>
+END
+       } else {
+               print "<td width='2%' $col>&nbsp;</td>";
+       }
+
        print <<END
        <td align='center' $col>
                <form method='post' action='$ENV{'SCRIPT_NAME'}'>
@@ -3205,7 +3618,12 @@ END
                        <input type='hidden' name='ACTION' value="$Lang::tr{'download host certificate'}" />
                        </form>
                </td>
-               <td width='4%' $col2>&nbsp;</td></tr>
+               <td width='4%' align='center' $col2>
+                       <form method='post' action='$ENV{'SCRIPT_NAME'}'>
+                               <input type='image' name='$Lang::tr{'regenerate host certificate'}' src='/images/reload.gif' alt='$Lang::tr{'regenerate host certificate'}' title='$Lang::tr{'regenerate host certificate'}' />
+                               <input type='hidden' name='ACTION' value='$Lang::tr{'regenerate host certificate'}' />
+                       </form>
+               </td></tr>
 END
 ;
        } else {
@@ -3325,7 +3743,7 @@ sub make_algos($$$$$) {
 
                                        if ($grp =~ m/^e(.*)$/) {
                                                push(@algo, "ecp$1");
-                                       } elsif ($grp =~ m/curve25519/) {
+                                       } elsif ($grp =~ m/curve(448|25519)/) {
                                                push(@algo, "$grp");
                                        } else {
                                                push(@algo, "modp$grp");
@@ -3342,7 +3760,7 @@ sub make_algos($$$$$) {
                                                # noop
                                        } elsif ($grp =~ m/^e(.*)$/) {
                                                push(@algo, "ecp$1");
-                                       } elsif ($grp =~ m/curve25519/) {
+                                       } elsif ($grp =~ m/curve(448|25519)/) {
                                                push(@algo, "$grp");
                                        } else {
                                                push(@algo, "modp$grp");
@@ -3375,3 +3793,44 @@ sub make_subnets($$) {
 
        return join(",", @cidr_nets);
 }
+
+sub regenerate_host_certificate() {
+       my $errormessage = "";
+
+       &General::log("ipsec", "Regenerating host certificate...");
+
+       # Create a CSR based on the existing certificate
+       my $opt = " x509 -x509toreq -copy_extensions copyall";
+       $opt .= " -signkey ${General::swroot}/certs/hostkey.pem";
+       $opt .= " -in ${General::swroot}/certs/hostcert.pem";
+       $opt .= " -out ${General::swroot}/certs/hostreq.pem";
+       $errormessage = &callssl($opt);
+
+       # Revoke the old certificate
+       if (!$errormessage) {
+               &General::log("ipsec", "Revoking the old host cert...");
+
+               my $opt = " ca -revoke ${General::swroot}/certs/hostcert.pem";
+               $errormessage = &callssl($opt);
+       }
+
+       # Sign the host certificate request
+       if (!$errormessage) {
+               &General::log("ipsec", "Self signing host cert...");
+
+               my $opt = " ca -md sha256 -days 825";
+               $opt .= " -batch -notext";
+               $opt .= " -in ${General::swroot}/certs/hostreq.pem";
+               $opt .= " -out ${General::swroot}/certs/hostcert.pem";
+               $errormessage = &callssl ($opt);
+
+               unlink ("${General::swroot}/certs/hostreq.pem"); #no more needed
+       }
+
+       # Reload the new certificate
+       if (!$errormessage) {
+               &General::system('/usr/local/bin/ipsecctrl', 'R');
+       }
+
+       return $errormessage;
+}