update-metadata.php 7.28 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
--language <locale>         Language locale, e.g. 'en', 'jp', ...
35
--syslog                    Use syslog for reporting
36
--syslog-id <id>            Process identity for syslog messages
37
--verbose | -v              Verbose mode
Guillaume Rousse's avatar
Guillaume Rousse committed
38
--help | -h                 Print this man page
39 40 41 42


PAGE;

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

require_once($topLevelDir . '/lib/functions.php');
require_once($topLevelDir . '/lib/readMetadata.php');
47 48 49

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

function reportError($message) {
227
	global $syslog, $syslogId;
228 229

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

function reportInfo($message) {
238
	global $syslog, $syslogId;
239 240

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