Commit c3881a5a authored by Guillaume Rousse's avatar Guillaume Rousse

fix error status in case of problem

exit("message") displays a message, but exits with normal error status :(
parent f1f56125
...@@ -67,18 +67,21 @@ if (isset($options['metadata-url'])) { ...@@ -67,18 +67,21 @@ if (isset($options['metadata-url'])) {
} elseif (isset($options['metadata-file'])) { } elseif (isset($options['metadata-file'])) {
$metadataFile = $options['metadata-file']; $metadataFile = $options['metadata-file'];
} else { } else {
exit("Exiting: both --metadata-url and --metadata-file parameters missing\n"); fwrite(STDERR, "Exiting: both --metadata-url and --metadata-file parameters missing\n");
exit(1);
} }
if (!isset($options['metadata-sp-file'])) { if (!isset($options['metadata-sp-file'])) {
exit("Exiting: mandatory --metadata-sp-file parameter missing\n"); fwrite(STDERR, "Exiting: mandatory --metadata-sp-file parameter missing\n");
exit(1);
} else { } else {
$metadataSPFile = $options['metadata-sp-file']; $metadataSPFile = $options['metadata-sp-file'];
$metadataTempSPFile = $metadataSPFile.'.swp'; $metadataTempSPFile = $metadataSPFile.'.swp';
} }
if (!isset($options['metadata-idp-file'])) { if (!isset($options['metadata-idp-file'])) {
exit("Exiting: mandatory --metadata-idp-file parameter missing\n"); fwrite(STDERR, "Exiting: mandatory --metadata-idp-file parameter missing\n");
exit(1);
} else { } else {
$metadataIDPFile = $options['metadata-idp-file']; $metadataIDPFile = $options['metadata-idp-file'];
$metadataTempIDPFile = $metadataIDPFile.'.swp'; $metadataTempIDPFile = $metadataIDPFile.'.swp';
...@@ -86,7 +89,8 @@ if (!isset($options['metadata-idp-file'])) { ...@@ -86,7 +89,8 @@ if (!isset($options['metadata-idp-file'])) {
if (isset($options['min-sp-count'])) { if (isset($options['min-sp-count'])) {
if (!is_numeric($options['min-sp-count'])) { if (!is_numeric($options['min-sp-count'])) {
exit("Exiting: invalid value for --min-sp-count parameter\n"); fwrite(STDERR, "Exiting: invalid value for --min-sp-count parameter\n");
exit(1);
} else { } else {
$minSPCount = $options['min-sp-count']; $minSPCount = $options['min-sp-count'];
} }
...@@ -96,7 +100,8 @@ if (isset($options['min-sp-count'])) { ...@@ -96,7 +100,8 @@ if (isset($options['min-sp-count'])) {
if (isset($options['min-idp-count'])) { if (isset($options['min-idp-count'])) {
if (!is_numeric($options['min-idp-count'])) { if (!is_numeric($options['min-idp-count'])) {
exit("Exiting: invalid value for --min-idp-count parameter\n"); fwrite(STDERR, "Exiting: invalid value for --min-idp-count parameter\n");
exit(1);
} else { } else {
$minIDPCount = $options['min-idp-count']; $minIDPCount = $options['min-idp-count'];
} }
...@@ -112,7 +117,8 @@ $verbose = isset($options['verbose']) || isset($options['v']) ? true : false; ...@@ -112,7 +117,8 @@ $verbose = isset($options['verbose']) || isset($options['v']) ? true : false;
if ($metadataURL) { if ($metadataURL) {
$metadataFile = tempnam(sys_get_temp_dir(), 'metadata'); $metadataFile = tempnam(sys_get_temp_dir(), 'metadata');
if (!ini_get('allow_url_fopen')) { if (!ini_get('allow_url_fopen')) {
exit("Exiting: allow_url_fopen disabled, unabled to download $metadataURL\n"); fwrite(STDERR, "Exiting: allow_url_fopen disabled, unabled to download $metadataURL\n");
exit(1);
} }
if ($verbose) { if ($verbose) {
echo "Downloading metadata from $metadataURL to $metadataFile\n"; echo "Downloading metadata from $metadataURL to $metadataFile\n";
...@@ -121,18 +127,21 @@ if ($metadataURL) { ...@@ -121,18 +127,21 @@ if ($metadataURL) {
if (!$result) { if (!$result) {
$error = error_get_last(); $error = error_get_last();
$message = explode(': ', $error['message'])[2]; $message = explode(': ', $error['message'])[2];
exit("Exiting: could not download $metadataURL: $message"); fwrite(STDERR, "Exiting: could not download $metadataURL: $message");
exit(1);
} }
} else { } else {
if ( if (
!file_exists($metadataFile) !file_exists($metadataFile)
|| filesize($metadataFile) == 0 || filesize($metadataFile) == 0
) { ) {
exit("Exiting: File $metadataFile is empty or does not exist\n"); fwrite(STDERR, "Exiting: file $metadataFile is empty or does not exist\n");
exit(1);
} }
if (!is_readable($metadataFile)){ if (!is_readable($metadataFile)){
exit("Exiting: File $metadataFile is not readable\n"); fwrite(STDERR, "Exiting: file $metadataFile is not readable\n");
exit(1);
} }
} }
...@@ -147,7 +156,8 @@ list($metadataIDProviders, $metadataSProviders) = parseMetadata($metadataFile, $ ...@@ -147,7 +156,8 @@ list($metadataIDProviders, $metadataSProviders) = parseMetadata($metadataFile, $
if (is_array($metadataIDProviders)){ if (is_array($metadataIDProviders)){
$IDPCount = count($metadataIDProviders); $IDPCount = count($metadataIDProviders);
if ($IDPCount < $minIDPCount) { if ($IDPCount < $minIDPCount) {
exit("Exiting: number of Identity Providers found ($IDPCount) lower than expected ($minIDPCount)\n"); fwrite(STDERR, "Exiting: number of Identity Providers found ($IDPCount) lower than expected ($minIDPCount)\n");
exit(1);
} }
if ($verbose) { if ($verbose) {
...@@ -156,7 +166,8 @@ if (is_array($metadataIDProviders)){ ...@@ -156,7 +166,8 @@ if (is_array($metadataIDProviders)){
dumpFile($metadataTempIDPFile, $metadataIDProviders, 'metadataIDProviders'); dumpFile($metadataTempIDPFile, $metadataIDProviders, 'metadataIDProviders');
if(!rename($metadataTempIDPFile, $metadataIDPFile)){ if(!rename($metadataTempIDPFile, $metadataIDPFile)){
exit("Exiting: Could not rename temporary file $metadataTempIDPFile to $metadataIDPFile"); fwrite(STDERR, "Exiting: could not rename temporary file $metadataTempIDPFile to $metadataIDPFile\n");
exit(1);
} }
} }
...@@ -164,7 +175,8 @@ if (is_array($metadataIDProviders)){ ...@@ -164,7 +175,8 @@ if (is_array($metadataIDProviders)){
if (is_array($metadataSProviders)){ if (is_array($metadataSProviders)){
$SPCount = count($metadataSProviders); $SPCount = count($metadataSProviders);
if ($SPCount < $minSPCount) { if ($SPCount < $minSPCount) {
exit("Exiting: number of Service Providers found ($SPCount) lower than expected ($minSPCount)\n"); fwrite(STDERR, "Exiting: number of Service Providers found ($SPCount) lower than expected ($minSPCount)\n");
exit(1);
} }
if ($verbose) { if ($verbose) {
...@@ -173,7 +185,8 @@ if (is_array($metadataSProviders)){ ...@@ -173,7 +185,8 @@ if (is_array($metadataSProviders)){
dumpFile($metadataTempSPFile, $metadataSProviders, 'metadataSProviders'); dumpFile($metadataTempSPFile, $metadataSProviders, 'metadataSProviders');
if(!rename($metadataTempSPFile, $metadataSPFile)){ if(!rename($metadataTempSPFile, $metadataSPFile)){
exit("Exiting: Could not rename temporary file $metadataTempSPFile to $metadataSPFile"); fwrite(STDERR, "Exiting: could not rename temporary file $metadataTempSPFile to $metadataSPFile\n");
exit(1);
} }
} }
...@@ -182,6 +195,8 @@ if ($metadataURL) { ...@@ -182,6 +195,8 @@ if ($metadataURL) {
$result = @unlink($metadataFile); $result = @unlink($metadataFile);
if (!$result) { if (!$result) {
$error = error_get_last(); $error = error_get_last();
exit("Exiting: could not delete temporary file $metadataFile: " . $error['message']); $message = $error['message'];
fwrite(STDERR, "Exiting: could not delete temporary file $metadataFile: $message");
exit(1);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment