update-metadata.php 6.07 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
} else {
70 71
	fwrite(STDERR, "Exiting: both --metadata-url and --metadata-file parameters missing\n");
	exit(1);
72 73 74
}

if (!isset($options['metadata-sp-file'])) {
75 76
	fwrite(STDERR, "Exiting: mandatory --metadata-sp-file parameter missing\n");
	exit(1);
77 78 79 80 81 82
} else {
	$metadataSPFile = $options['metadata-sp-file'];
	$metadataTempSPFile = $metadataSPFile.'.swp';
}

if (!isset($options['metadata-idp-file'])) {
83 84
	fwrite(STDERR, "Exiting: mandatory --metadata-idp-file parameter missing\n");
	exit(1);
85 86 87 88 89
} else {
	$metadataIDPFile = $options['metadata-idp-file'];
	$metadataTempIDPFile = $metadataIDPFile.'.swp';
}

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

if (isset($options['min-idp-count'])) {
	if (!is_numeric($options['min-idp-count'])) {
103 104
		fwrite(STDERR, "Exiting: invalid value for --min-idp-count parameter\n");
		exit(1);
105 106 107 108 109 110 111
	} else {
		$minIDPCount = $options['min-idp-count'];
	}
} else {
	$minIDPCount = 0;
}

112 113 114 115 116
// Set other options
$language = isset($options['language']) ? $options['language'] : 'en';
$verbose  = isset($options['verbose']) || isset($options['v']) ? true : false;

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

	if (!is_readable($metadataFile)){
143 144
		fwrite(STDERR, "Exiting: file $metadataFile is not readable\n");
		exit(1);
145
	}
146 147 148 149 150 151 152 153 154 155 156
}

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)){
157 158
	$IDPCount = count($metadataIDProviders);
	if ($IDPCount < $minIDPCount) {
159 160
		fwrite(STDERR, "Exiting: number of Identity Providers found ($IDPCount) lower than expected ($minIDPCount)\n");
		exit(1);
161
	}
162 163

	if ($verbose) {
164
		echo "Dumping $IDPCount extracted Identity Providers to file $metadataIDPFile\n";
165 166 167 168
	}
	dumpFile($metadataTempIDPFile, $metadataIDProviders, 'metadataIDProviders');
	
	if(!rename($metadataTempIDPFile, $metadataIDPFile)){
169 170
		fwrite(STDERR, "Exiting: could not rename temporary file $metadataTempIDPFile to $metadataIDPFile\n");
		exit(1);
171 172 173 174 175
	}
}

// If $metadataSProviders is not FALSE, dump results in $metadataSPFile.
if (is_array($metadataSProviders)){
176 177
	$SPCount = count($metadataSProviders);
	if ($SPCount < $minSPCount) {
178 179
		fwrite(STDERR, "Exiting: number of Service Providers found ($SPCount) lower than expected ($minSPCount)\n");
		exit(1);
180
	}
181 182

	if ($verbose) {
183
		echo "Dumping $SPCount extracted Service Providers to file $metadataSPFile\n";
184 185 186 187
	}
	dumpFile($metadataTempSPFile, $metadataSProviders, 'metadataSProviders');
	
	if(!rename($metadataTempSPFile, $metadataSPFile)){
188 189
		fwrite(STDERR, "Exiting: could not rename temporary file $metadataTempSPFile to $metadataSPFile\n");
		exit(1);
190 191
	}
}
192 193 194

// clean up if needed
if ($metadataURL) {
195
	$result = @unlink($metadataFile);
196 197
	if (!$result) {
		$error = error_get_last();
198 199 200
		$message = $error['message'];
		fwrite(STDERR, "Exiting: could not delete temporary file $metadataFile: $message");
		exit(1);
201 202
	}
}