[3/3] vpnmain.cgi: Do not interpret $? as error code of move()

Message ID 20210824155048.13859-3-michael.tremer@ipfire.org
State Accepted
Commit b5b19f370d275aeaef4135bc024a46bfbe5050c8
Headers show
Series [1/3] IPsec: Fix extra whitespace in exported certificates | expand

Commit Message

Michael Tremer Aug. 24, 2021, 3:50 p.m. UTC
From: Stefan Schantl <stefan.schantl@ipfire.org>

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 html/cgi-bin/vpnmain.cgi | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

Comments

Stefan Schantl Sept. 3, 2021, 4:27 p.m. UTC | #1
Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
Tested-by: Stefan Schantl <stefan.schantl@ipfire.org>
> From: Stefan Schantl <stefan.schantl@ipfire.org>
> 
> Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
> ---
>  html/cgi-bin/vpnmain.cgi | 30 ++++++++++++++++++------------
>  1 file changed, 18 insertions(+), 12 deletions(-)
> 
> diff --git a/html/cgi-bin/vpnmain.cgi b/html/cgi-bin/vpnmain.cgi
> index e9064e19c..7bb0d1b35 100644
> --- a/html/cgi-bin/vpnmain.cgi
> +++ b/html/cgi-bin/vpnmain.cgi
> @@ -675,8 +675,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;
> @@ -947,20 +946,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
> @@ -1979,8 +1981,11 @@ END
>                                         while (grep(/Imported-$idx/,
> @names) ) {$idx++};
>                                         $cgiparams{'CA_NAME'}="Import
> ed-$idx";
>                                         $cgiparams{'CERT_NAME'}=&Head
> er::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'};
> @@ -1993,8 +1998,9 @@ END
>                 }
>                 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

Patch

diff --git a/html/cgi-bin/vpnmain.cgi b/html/cgi-bin/vpnmain.cgi
index e9064e19c..7bb0d1b35 100644
--- a/html/cgi-bin/vpnmain.cgi
+++ b/html/cgi-bin/vpnmain.cgi
@@ -675,8 +675,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;
@@ -947,20 +946,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
@@ -1979,8 +1981,11 @@  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'};
@@ -1993,8 +1998,9 @@  END
 		}
 		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