update-metadata.php 7.35 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 22
    [--verbose | -v] [--min-sp-count <count>] [--min-idp-count <count>] \
    [--language <locale>] [--syslog] [--syslog-id <id>]
23 24
php update-metadata.php --metadata-url <url> \
    --metadata-idp-file <file> --metadata-sp-file <file> \
25 26
    [--verbose | -v] [--min-sp-count <count>] [--min-idp-count <count>] \
    [--language <locale>] [--syslog] [--syslog-id <id>]
27

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


PAGE;

Guillaume Rousse's avatar
Guillaume Rousse committed
45
$topLevelDir = dirname(__DIR__);
46 47 48

require_once($topLevelDir . '/lib/functions.php');
require_once($topLevelDir . '/lib/readMetadata.php');
49 50 51

// Script options
$longopts = array(
52
    "metadata-url:",
53 54 55
    "metadata-file:",
    "metadata-idp-file:",
    "metadata-sp-file:",
56 57
    "min-idp-count:",
    "min-sp-count:",
58 59
    "language:",
    "verbose",
60
    "syslog",
61
    "syslog-id:",
62 63 64 65 66 67 68 69 70
    "help",
);

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

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

71 72 73 74
// simple options
$language = isset($options['language']) ? $options['language'] : 'en';
$verbose  = isset($options['verbose']) || isset($options['v']) ? true : false;
$syslog   = isset($options['syslog']) ? true : false;
75
$syslogId = isset($options['syslog-id']) ? $options['syslog-id'] : 'SWITCHwayf';
76

Guillaume Rousse's avatar
Guillaume Rousse committed
77 78 79 80
if ($syslog) {
	openlog($syslogId, LOG_NDELAY, LOG_USER);
}

81 82 83
if (isset($options['metadata-url'])) {
	$metadataURL = $options['metadata-url'];
} elseif (isset($options['metadata-file'])) {
84
	$metadataFile = $options['metadata-file'];
85
} else {
86
	reportError("Exiting: both --metadata-url and --metadata-file parameters missing\n");
87
	exit(1);
88 89 90
}

if (!isset($options['metadata-sp-file'])) {
91
	reportError("Exiting: mandatory --metadata-sp-file parameter missing\n");
92
	exit(1);
93 94 95 96 97 98
} else {
	$metadataSPFile = $options['metadata-sp-file'];
	$metadataTempSPFile = $metadataSPFile.'.swp';
}

if (!isset($options['metadata-idp-file'])) {
99
	reportError("Exiting: mandatory --metadata-idp-file parameter missing\n");
100
	exit(1);
101 102 103 104 105
} else {
	$metadataIDPFile = $options['metadata-idp-file'];
	$metadataTempIDPFile = $metadataIDPFile.'.swp';
}

106
if (isset($options['min-sp-count'])) {
107
	if (preg_match('/^(\d+)%$/', $options['min-sp-count'], $matches)) {
108 109 110 111 112 113 114
		if (file_exists($metadataSPFile)) {
			require_once($metadataSPFile);
			$SPCount = count($metadataSProviders);
			$minSPCount = floor($SPCount * $matches[1] / 100);
		} else {
			$minSPCount = 0;
		}
115
	} elseif (preg_match('/^\d+$/', $options['min-sp-count'])) {
116
		$minSPCount = $options['min-sp-count'];
117
	} else {
118 119
		reportError("Exiting: invalid value for --min-sp-count parameter\n");
		exit(1);
120 121 122 123 124 125
	}
} else {
	$minSPCount = 0;
}

if (isset($options['min-idp-count'])) {
126
	if (preg_match('/^(\d+)%$/', $options['min-idp-count'], $matches)) {
127 128 129 130 131 132 133
		if (file_exists($metadataIDPFile)) {
			require_once($metadataIDPFile);
			$IDPCount = count($metadataIDProviders);
			$minIDPCount = floor($IDPCount * $matches[1] / 100);
		} else {
			$minIDPCount = 0;
		}
134
	} elseif (preg_match('/^\d+$/', $options['min-idp-count'])) {
135
		$minIDPCount = $options['min-idp-count'];
136
	} else {
137 138
		reportError("Exiting: invalid value for --min-idp-count parameter\n");
		exit(1);
139 140 141 142 143
	}
} else {
	$minIDPCount = 0;
}

144
// Input validation
145 146 147
if ($metadataURL) {
	$metadataFile = tempnam(sys_get_temp_dir(), 'metadata');
	if (!ini_get('allow_url_fopen')) {
148
		reportError("Exiting: allow_url_fopen disabled, unabled to download $metadataURL\n");
149
		exit(1);
150 151
	}
	if ($verbose) {
152
		reportInfo("Downloading metadata from $metadataURL to $metadataFile\n");
153
	}
154
	$result = @copy($metadataURL, $metadataFile);
155 156
	if (!$result) {
		$error = error_get_last();
157
		$message = explode(': ', $error['message'])[2];
158
		reportError("Exiting: could not download $metadataURL: $message");
159
		exit(1);
160 161 162 163 164 165
	}
} else {
	if (
		!file_exists($metadataFile)
		|| filesize($metadataFile) == 0
		) {
166
		reportError("Exiting: file $metadataFile is empty or does not exist\n");
167
		exit(1);
168 169 170
	}

	if (!is_readable($metadataFile)){
171
		reportError("Exiting: file $metadataFile is not readable\n");
172
		exit(1);
173
	}
174 175 176
}

if ($verbose) {
177
	reportInfo("Parsing metadata file $metadataFile\n");
178 179 180 181 182 183 184
}

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

// If $metadataIDProviders is not FALSE, dump results in $metadataIDPFile.
if (is_array($metadataIDProviders)){
185 186
	$IDPCount = count($metadataIDProviders);
	if ($IDPCount < $minIDPCount) {
187
		reportError("Exiting: number of Identity Providers found ($IDPCount) lower than expected ($minIDPCount)\n");
188
		exit(1);
189
	}
190 191

	if ($verbose) {
192
		reportInfo("Dumping $IDPCount extracted Identity Providers to file $metadataIDPFile\n");
193 194 195 196
	}
	dumpFile($metadataTempIDPFile, $metadataIDProviders, 'metadataIDProviders');
	
	if(!rename($metadataTempIDPFile, $metadataIDPFile)){
197
		reportError("Exiting: could not rename temporary file $metadataTempIDPFile to $metadataIDPFile\n");
198
		exit(1);
199 200 201 202 203
	}
}

// If $metadataSProviders is not FALSE, dump results in $metadataSPFile.
if (is_array($metadataSProviders)){
204 205
	$SPCount = count($metadataSProviders);
	if ($SPCount < $minSPCount) {
206
		reportError("Exiting: number of Service Providers found ($SPCount) lower than expected ($minSPCount)\n");
207
		exit(1);
208
	}
209 210

	if ($verbose) {
211
		reportInfo("Dumping $SPCount extracted Service Providers to file $metadataSPFile\n");
212 213 214 215
	}
	dumpFile($metadataTempSPFile, $metadataSProviders, 'metadataSProviders');
	
	if(!rename($metadataTempSPFile, $metadataSPFile)){
216
		reportError("Exiting: could not rename temporary file $metadataTempSPFile to $metadataSPFile\n");
217
		exit(1);
218 219
	}
}
220 221 222

// clean up if needed
if ($metadataURL) {
223
	$result = @unlink($metadataFile);
224 225
	if (!$result) {
		$error = error_get_last();
226
		$message = $error['message'];
227
		reportError("Exiting: could not delete temporary file $metadataFile: $message");
228
		exit(1);
229 230
	}
}
231 232

function reportError($message) {
Guillaume Rousse's avatar
Guillaume Rousse committed
233
	global $syslog;
234 235 236 237 238 239 240 241 242

	if ($syslog) {
		syslog(LOG_ERR, $message);
	} else {
		fwrite(STDERR, $message);
	}
}

function reportInfo($message) {
Guillaume Rousse's avatar
Guillaume Rousse committed
243
	global $syslog;
244 245 246 247 248 249 250

	if ($syslog) {
		syslog(LOG_INFO, $message);
	} else {
		fwrite(STDOUT, $message);
	}
}