update-metadata.php 7.4 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

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

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

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

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

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

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

	if (!is_readable($metadataFile)){
167
		reportError("Exiting: file $metadataFile is not readable\n");
168
		exit(1);
169
	}
170 171 172
}

if ($verbose) {
173
	reportInfo("Parsing metadata file $metadataFile\n");
174 175 176 177 178 179 180
}

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

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

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

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

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

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

function reportError($message) {
229
	global $syslog, $syslogId;
230 231

	if ($syslog) {
232
		openlog($syslogId, LOG_NDELAY, LOG_USER);
233 234 235 236 237 238 239
		syslog(LOG_ERR, $message);
	} else {
		fwrite(STDERR, $message);
	}
}

function reportInfo($message) {
240
	global $syslog, $syslogId;
241 242

	if ($syslog) {
243
		openlog($syslogId, LOG_NDELAY, LOG_USER);
244 245 246 247 248
		syslog(LOG_INFO, $message);
	} else {
		fwrite(STDOUT, $message);
	}
}