summaryrefslogtreecommitdiff
path: root/scripts/get-metadata.sh
blob: 2fc0b1367f9c1f771735969d993c817f36af2345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
#
# Fetch Service Provider metadata and save into entityid filename
#

error()
{
	echo "Error: $*" 1>&2
	exit 1
}

metadataurl=$1
if [ -z "$metadataurl" ] ; then
	cat <<EOF
Usage: `basename $0` <metadataurl>
Ex:    `basename $0` https://shibsp.mysite.com/Shibboleth.sso/Metadata
       `basename $0` https://shibidp.mysite.com/idp/profile/Metadata/SAML
       `basename $0` some-downloaded-metadata.xml
       `basename $0` reep:<entityid>
EOF
	exit 1
fi

if echo "$metadataurl" | grep -qE '^http://|^https://' ; then
	metadata=`curl -s -k -f $metadataurl`
elif echo "$metadataurl" | grep -qE '^reep:' ; then
        id=`echo -n $metadataurl | sed 's/^reep://' | sha1sum | awk '{print $1}'`
        metadataurl="http://md.reep.refeds.org/entities/%7Bsha1%7D$id"
        metadata=`curl -s -k -f $metadataurl`
else
	metadata=`cat $metadataurl`
fi
[ -n "$metadata" ] || error "Failed to fetch metadata from $metadataurl"

entityid=`echo "$metadata" | sed -n 's/.*entityID=['\''"]\([^"]*\)['\''"].*/\1/p'`
[ -n "$entityid" ] || error "Failed to find entityID in metadata"
[ "`echo "$entityid" | wc -l`" = "1" ] || error "Multiple entityid:s found: `echo $entityid`"

entityidfn=`echo "$entityid" | sed 's;https*://;;' | tr 'A-Z' 'a-z' | sed 's;/$;;' | sed 's/[^a-z0-9_.-]/-/g' | sed 's/\.xml$//;s/$/.xml/'`
[ -n "$entityidfn" ] || error "Failed to generate filename from entityid $entityid"

if [ -r "$entityidfn" ] ; then
	echo -n "Replace $entityidfn with metadata [Y/n]? "
else
	echo -n "Save metadata into $entityidfn [Y/n]? "
fi
read x
case $x in
	Y|y|"")
		echo "$metadata" > $entityidfn
		echo $entityidfn
		;;
	*)
		echo "Nothing done"
		;;
esac