32c98865400171737e02b82156e27aaee2330570
[shibboleth/cpp-sp.git] / shibsp / metadata / DynamicMetadataProvider.cpp
1 /*
2  *  Copyright 2001-2010 Internet2
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * DynamicMetadataProvider.cpp
19  *
20  * Advanced implementation of a dynamic caching MetadataProvider.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "Application.h"
26 #include "ServiceProvider.h"
27 #include "metadata/MetadataProviderCriteria.h"
28
29 #include <xercesc/framework/Wrapper4InputSource.hpp>
30 #include <xercesc/util/XMLUniDefs.hpp>
31 #include <xsec/framework/XSECDefs.hpp>
32 #include <saml/version.h>
33 #include <saml/binding/SAMLArtifact.h>
34 #include <saml/saml2/metadata/Metadata.h>
35 #include <saml/saml2/metadata/DynamicMetadataProvider.h>
36 #include <xmltooling/logging.h>
37 #include <xmltooling/XMLToolingConfig.h>
38 #include <xmltooling/security/Credential.h>
39 #include <xmltooling/security/CredentialCriteria.h>
40 #include <xmltooling/security/CredentialResolver.h>
41 #include <xmltooling/security/X509TrustEngine.h>
42 #include <xmltooling/soap/HTTPSOAPTransport.h>
43 #include <xmltooling/util/NDC.h>
44 #include <xmltooling/util/ParserPool.h>
45 #include <xmltooling/util/URLEncoder.h>
46 #include <xmltooling/util/XMLHelper.h>
47
48 using namespace shibsp;
49 using namespace opensaml;
50 using namespace xmltooling::logging;
51 using namespace xmltooling;
52 using namespace std;
53
54 namespace shibsp {
55     class SAML_DLLLOCAL DummyCredentialResolver : public CredentialResolver
56     {
57     public:
58         DummyCredentialResolver() {}
59         ~DummyCredentialResolver() {}
60
61         Lockable* lock() {return this;}
62         void unlock() {}
63
64         const Credential* resolve(const CredentialCriteria* criteria=NULL) const {return NULL;}
65         vector<const Credential*>::size_type resolve(
66             vector<const Credential*>& results, const CredentialCriteria* criteria=NULL
67             ) const {return 0;}
68     };
69
70     class SHIBSP_DLLLOCAL DynamicMetadataProvider : public saml2md::DynamicMetadataProvider
71     {
72     public:
73         DynamicMetadataProvider(const xercesc::DOMElement* e=NULL);
74
75         virtual ~DynamicMetadataProvider() {
76             delete m_trust;
77         }
78
79     protected:
80         saml2md::EntityDescriptor* resolve(const saml2md::MetadataProvider::Criteria& criteria) const;
81
82     private:
83         bool m_verifyHost,m_ignoreTransport,m_encoded;
84         string m_subst, m_match, m_regex;
85         X509TrustEngine* m_trust;
86     };
87
88
89     saml2md::MetadataProvider* SHIBSP_DLLLOCAL DynamicMetadataProviderFactory(const DOMElement* const & e)
90     {
91         return new DynamicMetadataProvider(e);
92     }
93
94     static const XMLCh encoded[] =          UNICODE_LITERAL_7(e,n,c,o,d,e,d);
95     static const XMLCh ignoreTransport[] =  UNICODE_LITERAL_15(i,g,n,o,r,e,T,r,a,n,s,p,o,r,t);
96     static const XMLCh match[] =            UNICODE_LITERAL_5(m,a,t,c,h);
97     static const XMLCh Regex[] =            UNICODE_LITERAL_5(R,e,g,e,x);
98     static const XMLCh Subst[] =            UNICODE_LITERAL_5(S,u,b,s,t);
99     static const XMLCh _TrustEngine[] =     UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
100     static const XMLCh type[] =             UNICODE_LITERAL_4(t,y,p,e);
101     static const XMLCh verifyHost[] =       UNICODE_LITERAL_10(v,e,r,i,f,y,H,o,s,t);
102 };
103
104 DynamicMetadataProvider::DynamicMetadataProvider(const DOMElement* e)
105     : saml2md::DynamicMetadataProvider(e), m_verifyHost(true), m_ignoreTransport(false), m_encoded(true), m_trust(NULL)
106 {
107     const XMLCh* flag = e ? e->getAttributeNS(NULL, verifyHost) : NULL;
108     if (flag && (*flag == chLatin_f || *flag == chDigit_0))
109         m_verifyHost = false;
110     flag = e ? e->getAttributeNS(NULL, ignoreTransport) : NULL;
111     if (flag && (*flag == chLatin_t || *flag == chDigit_1)) {
112         m_ignoreTransport = true;
113         return;
114     }
115
116     const DOMElement* child = e ? XMLHelper::getFirstChildElement(e, Subst) : NULL;
117     if (child && child->hasChildNodes()) {
118         auto_ptr_char s(child->getFirstChild()->getNodeValue());
119         if (s.get() && *s.get()) {
120             m_subst = s.get();
121             flag = child->getAttributeNS(NULL, encoded);
122             if (flag && (*flag == chLatin_f || *flag == chDigit_0))
123                 m_encoded = false;
124         }
125     }
126
127     if (m_subst.empty()) {
128         child = e ? XMLHelper::getFirstChildElement(e, Regex) : NULL;
129         if (child && child->hasChildNodes() && child->hasAttributeNS(NULL, match)) {
130             auto_ptr_char m(child->getAttributeNS(NULL, match));
131             auto_ptr_char repl(child->getFirstChild()->getNodeValue());
132             if (m.get() && *m.get() && repl.get() && *repl.get()) {
133                 m_match = m.get();
134                 m_regex = repl.get();
135             }
136         }
137     }
138
139     child = e ? XMLHelper::getFirstChildElement(e, _TrustEngine) : NULL;
140     auto_ptr_char t2(child ? child->getAttributeNS(NULL,type) : NULL);
141     if (t2.get()) {
142         TrustEngine* trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t2.get(), child);
143         if (!(m_trust = dynamic_cast<X509TrustEngine*>(trust))) {
144             delete trust;
145             throw ConfigurationException("DynamicMetadataProvider requires an X509TrustEngine plugin.");
146         }
147         return;
148     }
149
150     throw ConfigurationException("DynamicMetadataProvider requires an X509TrustEngine plugin unless ignoreTransport is true.");
151 }
152
153 saml2md::EntityDescriptor* DynamicMetadataProvider::resolve(const saml2md::MetadataProvider::Criteria& criteria) const
154 {
155 #ifdef _DEBUG
156     xmltooling::NDC("resolve");
157 #endif
158     Category& log=Category::getInstance(SHIBSP_LOGCAT".MetadataProvider.Dynamic");
159
160     string name;
161     if (criteria.entityID_ascii) {
162         name = criteria.entityID_ascii;
163     }
164     else if (criteria.entityID_unicode) {
165         auto_ptr_char temp(criteria.entityID_unicode);
166         name = temp.get();
167     }
168     else if (criteria.artifact) {
169         throw saml2md::MetadataException("Unable to resolve metadata dynamically from an artifact.");
170     }
171
172     // Possibly transform the input into a different URL to use.
173     if (!m_subst.empty()) {
174         string::size_type pos = m_subst.find("$entityID");
175         if (pos != string::npos) {
176             string name2 = m_subst;
177             name2.replace(pos, 9, m_encoded ? XMLToolingConfig::getConfig().getURLEncoder()->encode(name.c_str()) : name);
178             log.info("transformed location from (%s) to (%s)", name.c_str(), name2.c_str());
179             name = name2;
180         }
181     }
182     else if (!m_match.empty() && !m_regex.empty()) {
183         try {
184             RegularExpression exp(m_match.c_str());
185             XMLCh* temp = exp.replace(name.c_str(), m_regex.c_str());
186             if (temp) {
187                 auto_ptr_char narrow(temp);
188                 XMLString::release(&temp);
189
190                 // For some reason it returns the match string if it doesn't match the expression.
191                 if (name != narrow.get()) {
192                     log.info("transformed location from (%s) to (%s)", name.c_str(), narrow.get());
193                     name = narrow.get();
194                 }
195             }
196         }
197         catch (XMLException& ex) {
198             auto_ptr_char msg(ex.getMessage());
199             log.error("caught error applying regular expression: %s", msg.get());
200         }
201     }
202
203     // Establish networking properties based on calling application.
204     const MetadataProviderCriteria* mpc = dynamic_cast<const MetadataProviderCriteria*>(&criteria);
205     if (!mpc)
206         throw saml2md::MetadataException("Dynamic MetadataProvider requires Shibboleth-aware lookup criteria, check calling code.");
207     const PropertySet* relyingParty;
208     if (criteria.entityID_unicode)
209         relyingParty = mpc->application.getRelyingParty(criteria.entityID_unicode);
210     else {
211         auto_ptr_XMLCh temp2(name.c_str());
212         relyingParty = mpc->application.getRelyingParty(temp2.get());
213     }
214
215     // Prepare a transport object addressed appropriately.
216     SOAPTransport::Address addr(relyingParty->getString("entityID").second, name.c_str(), name.c_str());
217     const char* pch = strchr(addr.m_endpoint,':');
218     if (!pch)
219         throw IOException("location was not a URL.");
220     string scheme(addr.m_endpoint, pch-addr.m_endpoint);
221     SOAPTransport* transport=NULL;
222     try {
223         transport = XMLToolingConfig::getConfig().SOAPTransportManager.newPlugin(scheme.c_str(), addr);
224     }
225     catch (exception& ex) {
226         log.error("exception while building transport object to resolve URL: %s", ex.what());
227         throw IOException("Unable to resolve entityID with a known transport protocol.");
228     }
229     auto_ptr<SOAPTransport> transportwrapper(transport);
230
231     // Apply properties as directed.
232     transport->setVerifyHost(m_verifyHost);
233     DummyCredentialResolver dcr;
234     if (m_trust && !transport->setTrustEngine(m_trust, &dcr))
235         throw IOException("Unable to install X509TrustEngine into metadata resolver.");
236
237     Locker credlocker(NULL, false);
238     CredentialResolver* credResolver = NULL;
239     pair<bool,const char*> authType=relyingParty->getString("authType");
240     if (!authType.first || !strcmp(authType.second,"TLS")) {
241         credResolver = mpc->application.getCredentialResolver();
242         if (credResolver)
243             credlocker.assign(credResolver);
244         if (credResolver) {
245             CredentialCriteria cc;
246             cc.setUsage(Credential::TLS_CREDENTIAL);
247             authType = relyingParty->getString("keyName");
248             if (authType.first)
249                 cc.getKeyNames().insert(authType.second);
250             const Credential* cred = credResolver->resolve(&cc);
251             cc.getKeyNames().clear();
252             if (cred) {
253                 if (!transport->setCredential(cred))
254                     log.error("failed to load Credential into metadata resolver");
255             }
256             else {
257                 log.error("no TLS credential supplied");
258             }
259         }
260         else {
261             log.error("no CredentialResolver available for TLS");
262         }
263     }
264     else {
265         SOAPTransport::transport_auth_t type=SOAPTransport::transport_auth_none;
266         pair<bool,const char*> username=relyingParty->getString("authUsername");
267         pair<bool,const char*> password=relyingParty->getString("authPassword");
268         if (!username.first || !password.first)
269             log.error("transport authType (%s) specified but authUsername or authPassword was missing", authType.second);
270         else if (!strcmp(authType.second,"basic"))
271             type = SOAPTransport::transport_auth_basic;
272         else if (!strcmp(authType.second,"digest"))
273             type = SOAPTransport::transport_auth_digest;
274         else if (!strcmp(authType.second,"ntlm"))
275             type = SOAPTransport::transport_auth_ntlm;
276         else if (!strcmp(authType.second,"gss"))
277             type = SOAPTransport::transport_auth_gss;
278         else if (strcmp(authType.second,"none"))
279             log.error("unknown authType (%s) specified for RelyingParty", authType.second);
280         if (type > SOAPTransport::transport_auth_none) {
281             if (transport->setAuth(type,username.second,password.second))
282                 log.debug("configured for transport authentication (method=%s, username=%s)", authType.second, username.second);
283             else
284                 log.error("failed to configure transport authentication (method=%s)", authType.second);
285         }
286     }
287
288     pair<bool,unsigned int> timeout = relyingParty->getUnsignedInt("connectTimeout");
289     transport->setConnectTimeout(timeout.first ? timeout.second : 10);
290     timeout = relyingParty->getUnsignedInt("timeout");
291     transport->setTimeout(timeout.first ? timeout.second : 20);
292     mpc->application.getServiceProvider().setTransportOptions(*transport);
293
294     HTTPSOAPTransport* http = dynamic_cast<HTTPSOAPTransport*>(transport);
295     if (http) {
296         pair<bool,bool> flag = relyingParty->getBool("chunkedEncoding");
297         http->useChunkedEncoding(flag.first && flag.second);
298         http->setRequestHeader("Xerces-C", XERCES_FULLVERSIONDOT);
299         http->setRequestHeader("XML-Security-C", XSEC_FULLVERSIONDOT);
300         http->setRequestHeader("OpenSAML-C", OPENSAML_FULLVERSIONDOT);
301         http->setRequestHeader("User-Agent", PACKAGE_NAME);
302         http->setRequestHeader(PACKAGE_NAME, PACKAGE_VERSION);
303     }
304
305     try {
306         // Use a NULL stream to trigger a body-less "GET" operation.
307         transport->send();
308         istream& msg = transport->receive();
309
310         DOMDocument* doc=NULL;
311         StreamInputSource src(msg, "DynamicMetadataProvider");
312         Wrapper4InputSource dsrc(&src,false);
313         if (m_validate)
314             doc=XMLToolingConfig::getConfig().getValidatingParser().parse(dsrc);
315         else
316             doc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
317
318         // Wrap the document for now.
319         XercesJanitor<DOMDocument> docjanitor(doc);
320
321         // Unmarshall objects, binding the document.
322         auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
323         docjanitor.release();
324
325         // Make sure it's metadata.
326         saml2md::EntityDescriptor* entity = dynamic_cast<saml2md::EntityDescriptor*>(xmlObject.get());
327         if (!entity) {
328             throw saml2md::MetadataException(
329                 "Root of metadata instance not recognized: $1", params(1,xmlObject->getElementQName().toString().c_str())
330                 );
331         }
332         xmlObject.release();
333         return entity;
334     }
335     catch (XMLException& e) {
336         auto_ptr_char msg(e.getMessage());
337         log.error("Xerces error while resolving location (%s): %s", name.c_str(), msg.get());
338         throw saml2md::MetadataException(msg.get());
339     }
340 }