update-metadata.php 5.76 KB
Newer Older
1 2 3 4
<?php // Copyright (c) 2018, SWITCH
$MAN=<<<PAGE
Name:        SWITCHwayf
Author:      Lukas Haemmerle, SWITCH
Guillaume Rousse's avatar
Guillaume Rousse committed
5
Description: This script is used to dynamically create the list of
6 7
             IdPs and SP to be displayed for the WAYF/DS service 
             based on the federation metadata.
Guillaume Rousse's avatar
Guillaume Rousse committed
8
             It is intended to be run periodically, e.g. with a cron
9
             entry like:
Guillaume Rousse's avatar
Guillaume Rousse committed
10 11 12 13 14
             5 * * * * /usr/bin/php update-metadata.php \
                 --metadata-file /var/cache/shibboleth/metadata.switchaai.xml \
                 --metadata-idp-file /tmp/IDProvider.metadata.php \
                 --metadata-sp-file /tmp/SProvider.metadata.php \
                 > /dev/null
15
        
Guillaume Rousse's avatar
Guillaume Rousse committed
16 17
Usage
-----
18 19 20
php update-metadata.php -help|-h
php update-metadata.php --metadata-file <file> \
    --metadata-idp-file <file> --metadata-sp-file <file> \
21
    [--verbose | -v] [--min-sp-count <count>] [--min-idp-count <count>]
22 23
php update-metadata.php --metadata-url <url> \
    --metadata-idp-file <file> --metadata-sp-file <file> \
24
    [--verbose | -v] [--min-sp-count <count>] [--min-idp-count <count>]
25

Guillaume Rousse's avatar
Guillaume Rousse committed
26 27
Argument Description
--------------------
28
--metadata-url <url>        SAML2 metadata URL
29 30 31
--metadata-file <file>      SAML2 metadata file
--metadata-idp-file <file>  File containing Service Providers 
--metadata-sp-file <file>   File containing Identity Providers 
32 33
--min-idp-count <count>     Minimum expected number of IdPs in metadata
--min-sp-count <count>      Minimum expected number of SPs in metadata
34 35
--language <locale>         Language locale, e.g. 'en', 'jp', ...
--verbose | -v              Verbose mode
Guillaume Rousse's avatar
Guillaume Rousse committed
36
--help | -h                 Print this man page
37 38 39 40


PAGE;

Guillaume Rousse's avatar
Guillaume Rousse committed
41
$topLevelDir = dirname(__DIR__);
42 43 44

require_once($topLevelDir . '/lib/functions.php');
require_once($topLevelDir . '/lib/readMetadata.php');
45 46 47

// Script options
$longopts = array(
48
    "metadata-url:",
49 50 51
    "metadata-file:",
    "metadata-idp-file:",
    "metadata-sp-file:",
52 53
    "min-idp-count:",
    "min-sp-count:",
54 55 56 57 58 59 60 61 62 63 64
    "language:",
    "verbose",
    "help",
);

$options = getopt('hv', $longopts);

if (isset($options['help']) || isset($options['h'])) {
	exit($MAN);
} 

65 66 67
if (isset($options['metadata-url'])) {
	$metadataURL = $options['metadata-url'];
} elseif (isset($options['metadata-file'])) {
68
	$metadataFile = $options['metadata-file'];
69 70
} else {
	exit("Exiting: both --metadata-url and --metadata-file parameters missing\n");
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
}

if (!isset($options['metadata-sp-file'])) {
	exit("Exiting: mandatory --metadata-sp-file parameter missing\n");
} else {
	$metadataSPFile = $options['metadata-sp-file'];
	$metadataTempSPFile = $metadataSPFile.'.swp';
}

if (!isset($options['metadata-idp-file'])) {
	exit("Exiting: mandatory --metadata-idp-file parameter missing\n");
} else {
	$metadataIDPFile = $options['metadata-idp-file'];
	$metadataTempIDPFile = $metadataIDPFile.'.swp';
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
if (isset($options['min-sp-count'])) {
	if (!is_numeric($options['min-sp-count'])) {
		exit("Exiting: invalid value for --min-sp-count parameter\n");
	} else {
		$minSPCount = $options['min-sp-count'];
	}
} else {
	$minSPCount = 0;
}

if (isset($options['min-idp-count'])) {
	if (!is_numeric($options['min-idp-count'])) {
		exit("Exiting: invalid value for --min-idp-count parameter\n");
	} else {
		$minIDPCount = $options['min-idp-count'];
	}
} else {
	$minIDPCount = 0;
}

107 108 109 110 111
// Set other options
$language = isset($options['language']) ? $options['language'] : 'en';
$verbose  = isset($options['verbose']) || isset($options['v']) ? true : false;

// Input validation
112 113 114 115 116 117 118 119
if ($metadataURL) {
	$metadataFile = tempnam(sys_get_temp_dir(), 'metadata');
	if (!ini_get('allow_url_fopen')) {
		exit("Exiting: allow_url_fopen disabled, unabled to download $metadataURL\n");
	}
	if ($verbose) {
		echo "Downloading metadata from $metadataURL to $metadataFile\n";
	}
120
	$result = @copy($metadataURL, $metadataFile);
121 122
	if (!$result) {
		$error = error_get_last();
123 124
		$message = explode(': ', $error['message'])[2];
		exit("Exiting: could not download $metadataURL: $message");
125 126 127 128 129 130 131 132 133 134 135 136
	}
} else {
	if (
		!file_exists($metadataFile)
		|| filesize($metadataFile) == 0
		) {
		exit("Exiting: File $metadataFile is empty or does not exist\n");
	}

	if (!is_readable($metadataFile)){
		exit("Exiting: File $metadataFile is not readable\n");
	}
137 138 139 140 141 142 143 144 145 146 147
}

if ($verbose) {
	echo "Parsing metadata file $metadataFile\n";
}

// Parse metadata
list($metadataIDProviders, $metadataSProviders) = parseMetadata($metadataFile, $language);

// If $metadataIDProviders is not FALSE, dump results in $metadataIDPFile.
if (is_array($metadataIDProviders)){
148 149 150 151
	$IDPCount = count($metadataIDProviders);
	if ($IDPCount < $minIDPCount) {
		exit("Exiting: number of Identity Providers found ($IDPCount) lower than expected ($minIDPCount)\n");
	}
152 153

	if ($verbose) {
154
		echo "Dumping $IDPCount extracted Identity Providers to file $metadataIDPFile\n";
155 156 157 158 159 160 161 162 163 164
	}
	dumpFile($metadataTempIDPFile, $metadataIDProviders, 'metadataIDProviders');
	
	if(!rename($metadataTempIDPFile, $metadataIDPFile)){
		exit("Exiting: Could not rename temporary file $metadataTempIDPFile to $metadataIDPFile");
	}
}

// If $metadataSProviders is not FALSE, dump results in $metadataSPFile.
if (is_array($metadataSProviders)){
165 166 167 168
	$SPCount = count($metadataSProviders);
	if ($SPCount < $minSPCount) {
		exit("Exiting: number of Service Providers found ($SPCount) lower than expected ($minSPCount)\n");
	}
169 170

	if ($verbose) {
171
		echo "Dumping $SPCount extracted Service Providers to file $metadataSPFile\n";
172 173 174 175 176 177 178
	}
	dumpFile($metadataTempSPFile, $metadataSProviders, 'metadataSProviders');
	
	if(!rename($metadataTempSPFile, $metadataSPFile)){
		exit("Exiting: Could not rename temporary file $metadataTempSPFile to $metadataSPFile");
	}
}
179 180 181

// clean up if needed
if ($metadataURL) {
182
	$result = @unlink($metadataFile);
183 184
	if (!$result) {
		$error = error_get_last();
185
		exit("Exiting: could not delete temporary file $metadataFile: " . $error['message']);
186 187
	}
}