#!/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 < 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: `basename $0` mdq: `basename $0` (tries to pull from standard locations) EOF exit 1 fi script_cwd=`dirname "$0"` if test -d md ; then echo "Moving into md/" cd md echo "$script_cwd" | grep -q ^/ || script_cwd=../$script_cwd fi if echo "$metadataurl" | grep -qE '^http://|^https://' ; then metadata=`curl -L -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 -L -s -k -f "$metadataurl"` elif echo "$metadataurl" | grep -qE '^mdq:' ; then id=`echo -n "$metadataurl" | sed 's/^mdq://' | sha1sum | awk '{print $1}'` metadataurl="$MDQ/%7Bsha1%7D$id" metadata=`curl -L -s -k -f "$metadataurl"` else if [ -s "${metadataurl}" ]; then metadata=`cat "$metadataurl"` else urls="https://${metadataurl}/idp/shibboleth https://${metadataurl}/Shibboleth.sso/Metadata https://${metadataurl}/saml/index/sp-metadata https://${metadataurl}/saml/metadata" for i in ${urls}; do metadata=`curl -L -s -k -f "${i}"` [ -n "${metadata}" ] && break done fi 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" [ -r "$entityidfn" ] && new=false || new=true if $new ; then echo -n "Save metadata into $entityidfn [Y/n]? " else echo -n "Replace $entityidfn with metadata [Y/n]? " fi read x case $x in Y|y|"") echo "$metadata" > $entityidfn tmp=`mktemp` xsltproc $script_cwd/../xslt/clean-entitydescriptor.xsl ${entityidfn} > ${tmp} && mv ${tmp} ${entityidfn} ;; *) echo "Nothing done" ;; esac