https://issues.shibboleth.net/jira/browse/SSPCPP-468
[shibboleth/cpp-sp.git] / shibsp / attribute / resolver / impl / XMLAttributeExtractor.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * XMLAttributeExtractor.cpp
23  *
24  * AttributeExtractor based on an XML mapping file.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "Application.h"
30 #include "ServiceProvider.h"
31 #include "attribute/Attribute.h"
32 #include "attribute/AttributeDecoder.h"
33 #include "attribute/filtering/AttributeFilter.h"
34 #include "attribute/filtering/BasicFilteringContext.h"
35 #include "attribute/resolver/AttributeExtractor.h"
36 #include "remoting/ddf.h"
37 #include "security/SecurityPolicy.h"
38 #include "util/SPConstants.h"
39
40 #include <boost/bind.hpp>
41 #include <boost/shared_ptr.hpp>
42 #include <boost/algorithm/string.hpp>
43 #include <boost/iterator/indirect_iterator.hpp>
44 #include <boost/ptr_container/ptr_vector.hpp>
45 #include <boost/tuple/tuple.hpp>
46 #include <saml/SAMLConfig.h>
47 #include <saml/saml1/core/Assertions.h>
48 #include <saml/saml2/core/Assertions.h>
49 #include <saml/saml2/metadata/Metadata.h>
50 #include <saml/saml2/metadata/MetadataCredentialCriteria.h>
51 #include <saml/saml2/metadata/ObservableMetadataProvider.h>
52 #include <xmltooling/XMLToolingConfig.h>
53 #include <xmltooling/security/TrustEngine.h>
54 #include <xmltooling/util/NDC.h>
55 #include <xmltooling/util/ReloadableXMLFile.h>
56 #include <xmltooling/util/Threads.h>
57 #include <xmltooling/util/XMLHelper.h>
58 #include <xercesc/util/XMLUniDefs.hpp>
59
60 using namespace shibsp;
61 using namespace opensaml::saml2md;
62 using namespace opensaml;
63 using namespace xmltooling;
64 using namespace boost;
65 using namespace std;
66 using saml1::NameIdentifier;
67 using saml2::NameID;
68 using saml2::EncryptedAttribute;
69
70 namespace shibsp {
71
72 #if defined (_MSC_VER)
73     #pragma warning( push )
74     #pragma warning( disable : 4250 )
75 #endif
76
77     class XMLExtractorImpl : public ObservableMetadataProvider::Observer
78     {
79     public:
80         XMLExtractorImpl(const DOMElement* e, Category& log);
81         ~XMLExtractorImpl() {
82             for (map<const ObservableMetadataProvider*,decoded_t>::iterator i=m_decodedMap.begin(); i!=m_decodedMap.end(); ++i) {
83                 i->first->removeObserver(this);
84                 for (decoded_t::iterator attrs = i->second.begin(); attrs!=i->second.end(); ++attrs)
85                     for_each(attrs->second.begin(), attrs->second.end(), mem_fun_ref<DDF&,DDF>(&DDF::destroy));
86             }
87             if (m_document)
88                 m_document->release();
89         }
90
91         void setDocument(DOMDocument* doc) {
92             m_document = doc;
93         }
94
95         void onEvent(const ObservableMetadataProvider& metadata) const {
96             // Destroy attributes we cached from this provider.
97             m_attrLock->wrlock();
98             SharedLock wrapper(m_attrLock, false);
99             decoded_t& d = m_decodedMap[&metadata];
100             for (decoded_t::iterator a = d.begin(); a!=d.end(); ++a)
101                 for_each(a->second.begin(), a->second.end(), mem_fun_ref<DDF&,DDF>(&DDF::destroy));
102             d.clear();
103         }
104
105         void extractAttributes(const Application&, const char*, const char*, const NameIdentifier&, ptr_vector<Attribute>&) const;
106         void extractAttributes(const Application&, const char*, const char*, const NameID&, ptr_vector<Attribute>&) const;
107         void extractAttributes(const Application&, const GenericRequest*, const char*, const char*, const saml1::Attribute&, ptr_vector<Attribute>&) const;
108         void extractAttributes(const Application&, const GenericRequest*, const char*, const char*, const saml2::Attribute&, ptr_vector<Attribute>&) const;
109         void extractAttributes(const Application&, const GenericRequest*, const char*, const char*, const saml1::AttributeStatement&, ptr_vector<Attribute>&) const;
110         void extractAttributes(const Application&, const GenericRequest*, const char*, const char*, const saml2::AttributeStatement&, ptr_vector<Attribute>&) const;
111         void extractAttributes(
112             const Application&, const GenericRequest*, const ObservableMetadataProvider*, const XMLCh*, const char*, const Extensions&, ptr_vector<Attribute>&
113             ) const;
114
115         void getAttributeIds(vector<string>& attributes) const {
116             attributes.insert(attributes.end(), m_attributeIds.begin(), m_attributeIds.end());
117         }
118
119         void generateMetadata(SPSSODescriptor& role) const;
120
121     private:
122         Category& m_log;
123         DOMDocument* m_document;
124         typedef map< pair<xstring,xstring>,pair< boost::shared_ptr<AttributeDecoder>,vector<string> > > attrmap_t;
125         attrmap_t m_attrMap;
126         vector<string> m_attributeIds;
127         vector< tuple<xstring,xstring,bool> > m_requestedAttrs;
128
129         // settings for embedded assertions in metadata
130         string m_policyId;
131         scoped_ptr<AttributeFilter> m_filter;
132         scoped_ptr<MetadataProvider> m_metadata;
133         scoped_ptr<TrustEngine> m_trust;
134         bool m_entityAssertions,m_metaAttrCaching;
135
136         // manages caching of decoded Attributes
137         scoped_ptr<RWLock> m_attrLock;
138         typedef map< const EntityAttributes*,vector<DDF> > decoded_t;
139         mutable map<const ObservableMetadataProvider*,decoded_t> m_decodedMap;
140     };
141
142     class XMLExtractor : public AttributeExtractor, public ReloadableXMLFile
143     {
144     public:
145         XMLExtractor(const DOMElement* e) : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT".AttributeExtractor.XML")) {
146             background_load();
147         }
148         ~XMLExtractor() {
149             shutdown();
150         }
151
152         // deprecated method
153         void extractAttributes(
154             const Application& application, const RoleDescriptor* issuer, const XMLObject& xmlObject, vector<Attribute*>& attributes
155             ) const {
156             extractAttributes(application, nullptr, issuer, xmlObject, attributes);
157         }
158
159         void extractAttributes(const Application&, const GenericRequest*, const RoleDescriptor*, const XMLObject&, vector<Attribute*>&) const;
160
161         void getAttributeIds(std::vector<std::string>& attributes) const {
162             if (m_impl)
163                 m_impl->getAttributeIds(attributes);
164         }
165
166         void generateMetadata(SPSSODescriptor& role) const {
167             if (m_impl)
168                 m_impl->generateMetadata(role);
169         }
170
171     protected:
172         pair<bool,DOMElement*> background_load();
173
174     private:
175         scoped_ptr<XMLExtractorImpl> m_impl;
176
177         void extractAttributes(const Application&, const GenericRequest*, const RoleDescriptor*, const XMLObject&, ptr_vector<Attribute>&) const;
178     };
179
180 #if defined (_MSC_VER)
181     #pragma warning( pop )
182 #endif
183
184     AttributeExtractor* SHIBSP_DLLLOCAL XMLAttributeExtractorFactory(const DOMElement* const & e)
185     {
186         return new XMLExtractor(e);
187     }
188
189     static const XMLCh _aliases[] =                 UNICODE_LITERAL_7(a,l,i,a,s,e,s);
190     static const XMLCh _AttributeDecoder[] =        UNICODE_LITERAL_16(A,t,t,r,i,b,u,t,e,D,e,c,o,d,e,r);
191     static const XMLCh _AttributeFilter[] =         UNICODE_LITERAL_15(A,t,t,r,i,b,u,t,e,F,i,l,t,e,r);
192     static const XMLCh Attributes[] =               UNICODE_LITERAL_10(A,t,t,r,i,b,u,t,e,s);
193     static const XMLCh _id[] =                      UNICODE_LITERAL_2(i,d);
194     static const XMLCh isRequested[] =              UNICODE_LITERAL_11(i,s,R,e,q,u,e,s,t,e,d);
195     static const XMLCh _MetadataProvider[] =        UNICODE_LITERAL_16(M,e,t,a,d,a,t,a,P,r,o,v,i,d,e,r);
196     static const XMLCh metadataAttributeCaching[] = UNICODE_LITERAL_24(m,e,t,a,d,a,t,a,A,t,t,r,i,b,u,t,e,C,a,c,h,i,n,g);
197     static const XMLCh metadataPolicyId[] =         UNICODE_LITERAL_16(m,e,t,a,d,a,t,a,P,o,l,i,c,y,I,d);
198     static const XMLCh _name[] =                    UNICODE_LITERAL_4(n,a,m,e);
199     static const XMLCh nameFormat[] =               UNICODE_LITERAL_10(n,a,m,e,F,o,r,m,a,t);
200     static const XMLCh _TrustEngine[] =             UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
201     static const XMLCh _type[] =                    UNICODE_LITERAL_4(t,y,p,e);
202 };
203
204 XMLExtractorImpl::XMLExtractorImpl(const DOMElement* e, Category& log)
205     : m_log(log),
206         m_document(nullptr),
207         m_policyId(XMLHelper::getAttrString(e, nullptr, metadataPolicyId)),
208         m_entityAssertions(true),
209         m_metaAttrCaching(XMLHelper::getAttrBool(e, true, metadataAttributeCaching))
210 {
211 #ifdef _DEBUG
212     xmltooling::NDC ndc("XMLExtractorImpl");
213 #endif
214
215     if (!XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, Attributes))
216         throw ConfigurationException("XML AttributeExtractor requires am:Attributes at root of configuration.");
217
218     DOMElement* child = XMLHelper::getFirstChildElement(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, _MetadataProvider);
219     if (child) {
220         try {
221             string t(XMLHelper::getAttrString(child, nullptr, _type));
222             if (t.empty())
223                 throw ConfigurationException("MetadataProvider element missing type attribute.");
224             m_log.info("building MetadataProvider of type %s...", t.c_str());
225             m_metadata.reset(SAMLConfig::getConfig().MetadataProviderManager.newPlugin(t.c_str(), child));
226             m_metadata->init();
227         }
228         catch (std::exception& ex) {
229             m_metadata.reset();
230             m_entityAssertions = false;
231             m_log.crit("error building/initializing dedicated MetadataProvider: %s", ex.what());
232             m_log.crit("disabling support for Assertions in EntityAttributes extension");
233         }
234     }
235
236     if (m_entityAssertions) {
237         child = XMLHelper::getFirstChildElement(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, _TrustEngine);
238         if (child) {
239             try {
240                 string t(XMLHelper::getAttrString(child, nullptr, _type));
241                 if (t.empty())
242                     throw ConfigurationException("TrustEngine element missing type attribute.");
243                 m_log.info("building TrustEngine of type %s...", t.c_str());
244                 m_trust.reset(XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t.c_str(), child));
245             }
246             catch (std::exception& ex) {
247                 m_entityAssertions = false;
248                 m_log.crit("error building/initializing dedicated TrustEngine: %s", ex.what());
249                 m_log.crit("disabling support for Assertions in EntityAttributes extension");
250             }
251         }
252     }
253
254     if (m_entityAssertions) {
255         child = XMLHelper::getFirstChildElement(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, _AttributeFilter);
256         if (child) {
257             try {
258                 string t(XMLHelper::getAttrString(child, nullptr, _type));
259                 if (t.empty())
260                     throw ConfigurationException("AttributeFilter element missing type attribute.");
261                 m_log.info("building AttributeFilter of type %s...", t.c_str());
262                 m_filter.reset(SPConfig::getConfig().AttributeFilterManager.newPlugin(t.c_str(), child));
263             }
264             catch (std::exception& ex) {
265                 m_entityAssertions = false;
266                 m_log.crit("error building/initializing dedicated AttributeFilter: %s", ex.what());
267                 m_log.crit("disabling support for Assertions in EntityAttributes extension");
268             }
269         }
270     }
271
272     child = XMLHelper::getFirstChildElement(e, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
273     while (child) {
274         // Check for missing name or id.
275         const XMLCh* name = child->getAttributeNS(nullptr, _name);
276         if (!name || !*name) {
277             m_log.warn("skipping Attribute with no name");
278             child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
279             continue;
280         }
281
282         auto_ptr_char id(child->getAttributeNS(nullptr, _id));
283         if (!id.get() || !*id.get()) {
284             m_log.warn("skipping Attribute with no id");
285             child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
286             continue;
287         }
288         else if (!strcmp(id.get(), "REMOTE_USER")) {
289             m_log.warn("skipping Attribute, id of REMOTE_USER is a reserved name");
290             child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
291             continue;
292         }
293
294         boost::shared_ptr<AttributeDecoder> decoder;
295         try {
296             DOMElement* dchild = XMLHelper::getFirstChildElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, _AttributeDecoder);
297             if (dchild) {
298                 auto_ptr<xmltooling::QName> q(XMLHelper::getXSIType(dchild));
299                 if (q.get())
300                     decoder.reset(SPConfig::getConfig().AttributeDecoderManager.newPlugin(*q.get(), dchild));
301             }
302             if (!decoder)
303                 decoder.reset(SPConfig::getConfig().AttributeDecoderManager.newPlugin(StringAttributeDecoderType, nullptr));
304         }
305         catch (std::exception& ex) {
306             m_log.error("skipping Attribute (%s), error building AttributeDecoder: %s", id.get(), ex.what());
307         }
308
309         if (!decoder) {
310             child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
311             continue;
312         }
313
314         // Empty NameFormat implies the usual Shib URI naming defaults.
315         const XMLCh* format = child->getAttributeNS(nullptr, nameFormat);
316         if (!format || XMLString::equals(format, shibspconstants::SHIB1_ATTRIBUTE_NAMESPACE_URI) ||
317                 XMLString::equals(format, saml2::Attribute::URI_REFERENCE))
318             format = &chNull;  // ignore default Format/Namespace values
319
320         // Fetch/create the map entry and see if it's a duplicate rule.
321         pair< boost::shared_ptr<AttributeDecoder>,vector<string> >& decl = m_attrMap[pair<xstring,xstring>(name,format)];
322         if (decl.first) {
323             m_log.warn("skipping duplicate Attribute mapping (same name and nameFormat)");
324             child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
325             continue;
326         }
327
328         if (m_log.isInfoEnabled()) {
329             auto_ptr_char n(name);
330             auto_ptr_char f(format);
331             m_log.info("creating mapping for Attribute %s%s%s", n.get(), *f.get() ? ", Format/Namespace:" : "", f.get());
332         }
333
334         decl.first = decoder;
335         decl.second.push_back(id.get());
336         m_attributeIds.push_back(id.get());
337
338         // Check for isRequired/isRequested.
339         bool requested = XMLHelper::getAttrBool(child, false, isRequested);
340         bool required = XMLHelper::getAttrBool(child, false, RequestedAttribute::ISREQUIRED_ATTRIB_NAME);
341         if (required || requested)
342             m_requestedAttrs.push_back(tuple<xstring,xstring,bool>(name,format,required));
343
344         name = child->getAttributeNS(nullptr, _aliases);
345         if (name && *name) {
346             auto_ptr_char aliases(name);
347             string dup(aliases.get());
348             set<string> new_aliases;
349             split(new_aliases, dup, is_space(), algorithm::token_compress_on);
350             set<string>::iterator ru = new_aliases.find("REMOTE_USER");
351             if (ru != new_aliases.end()) {
352                 m_log.warn("skipping alias, REMOTE_USER is a reserved name");
353                 new_aliases.erase(ru);
354             }
355             decl.second.insert(decl.second.end(), new_aliases.begin(), new_aliases.end());
356             m_attributeIds.insert(m_attributeIds.end(), new_aliases.begin(), new_aliases.end());
357         }
358
359         child = XMLHelper::getNextSiblingElement(child, shibspconstants::SHIB2ATTRIBUTEMAP_NS, saml1::Attribute::LOCAL_NAME);
360     }
361
362     if (m_metaAttrCaching)
363         m_attrLock.reset(RWLock::create());
364 }
365
366 void XMLExtractorImpl::generateMetadata(SPSSODescriptor& role) const
367 {
368     if (m_requestedAttrs.empty())
369         return;
370     int index = 1;
371     const vector<AttributeConsumingService*>& svcs = const_cast<const SPSSODescriptor*>(&role)->getAttributeConsumingServices();
372     for (vector<AttributeConsumingService*>::const_iterator s =svcs.begin(); s != svcs.end(); ++s) {
373         pair<bool,int> i = (*s)->getIndex();
374         if (i.first && index == i.second)
375             index = i.second + 1;
376     }
377     AttributeConsumingService* svc = AttributeConsumingServiceBuilder::buildAttributeConsumingService();
378     role.getAttributeConsumingServices().push_back(svc);
379     svc->setIndex(index);
380     ServiceName* sn = ServiceNameBuilder::buildServiceName();
381     svc->getServiceNames().push_back(sn);
382     sn->setName(dynamic_cast<EntityDescriptor*>(role.getParent())->getEntityID());
383     static const XMLCh english[] = UNICODE_LITERAL_2(e,n);
384     sn->setLang(english);
385
386     for (vector< tuple<xstring,xstring,bool> >::const_iterator i = m_requestedAttrs.begin(); i != m_requestedAttrs.end(); ++i) {
387         RequestedAttribute* req = RequestedAttributeBuilder::buildRequestedAttribute();
388         svc->getRequestedAttributes().push_back(req);
389         req->setName(i->get<0>().c_str());
390         if (i->get<1>().empty())
391             req->setNameFormat(saml2::Attribute::URI_REFERENCE);
392         else
393             req->setNameFormat(i->get<1>().c_str());
394         if (i->get<2>())
395             req->isRequired(true);
396     }
397 }
398
399 void XMLExtractorImpl::extractAttributes(
400     const Application& application,
401     const char* assertingParty,
402     const char* relyingParty,
403     const NameIdentifier& nameid,
404     ptr_vector<Attribute>& attributes
405     ) const
406 {
407     const XMLCh* format = nameid.getFormat();
408     if (!format || !*format)
409         format = NameIdentifier::UNSPECIFIED;
410     attrmap_t::const_iterator rule;
411     if ((rule = m_attrMap.find(pair<xstring,xstring>(format,xstring()))) != m_attrMap.end()) {
412         auto_ptr<Attribute> a(rule->second.first->decode(nullptr, rule->second.second, &nameid, assertingParty, relyingParty));
413         if (a.get()) {
414             attributes.push_back(a.get());
415             a.release();
416         }
417     }
418     else if (m_log.isDebugEnabled()) {
419         auto_ptr_char temp(format);
420         m_log.debug("skipping unmapped NameIdentifier with format (%s)", temp.get());
421     }
422 }
423
424 void XMLExtractorImpl::extractAttributes(
425     const Application& application,
426     const char* assertingParty,
427     const char* relyingParty,
428     const NameID& nameid,
429     ptr_vector<Attribute>& attributes
430     ) const
431 {
432     const XMLCh* format = nameid.getFormat();
433     if (!format || !*format)
434         format = NameID::UNSPECIFIED;
435     attrmap_t::const_iterator rule;
436     if ((rule = m_attrMap.find(pair<xstring,xstring>(format,xstring()))) != m_attrMap.end()) {
437         auto_ptr<Attribute> a(rule->second.first->decode(nullptr, rule->second.second, &nameid, assertingParty, relyingParty));
438         if (a.get()) {
439             attributes.push_back(a.get());
440             a.release();
441         }
442     }
443     else if (m_log.isDebugEnabled()) {
444         auto_ptr_char temp(format);
445         m_log.debug("skipping unmapped NameID with format (%s)", temp.get());
446     }
447 }
448
449 void XMLExtractorImpl::extractAttributes(
450     const Application& application,
451     const GenericRequest* request,
452     const char* assertingParty,
453     const char* relyingParty,
454     const saml1::Attribute& attr,
455     ptr_vector<Attribute>& attributes
456     ) const
457 {
458     const XMLCh* name = attr.getAttributeName();
459     const XMLCh* format = attr.getAttributeNamespace();
460     if (!name || !*name)
461         return;
462     if (!format || XMLString::equals(format, shibspconstants::SHIB1_ATTRIBUTE_NAMESPACE_URI))
463         format = &chNull;
464     attrmap_t::const_iterator rule;
465     if ((rule = m_attrMap.find(pair<xstring,xstring>(name,format))) != m_attrMap.end()) {
466         auto_ptr<Attribute> a(rule->second.first->decode(request, rule->second.second, &attr, assertingParty, relyingParty));
467         if (a.get()) {
468             attributes.push_back(a.get());
469             a.release();
470         }
471     }
472     else if (m_log.isInfoEnabled()) {
473         auto_ptr_char temp1(name);
474         auto_ptr_char temp2(format);
475         m_log.info("skipping unmapped SAML 1.x Attribute with Name: %s%s%s", temp1.get(), *temp2.get() ? ", Namespace:" : "", temp2.get());
476     }
477 }
478
479 void XMLExtractorImpl::extractAttributes(
480     const Application& application,
481     const GenericRequest* request,
482     const char* assertingParty,
483     const char* relyingParty,
484     const saml2::Attribute& attr,
485     ptr_vector<Attribute>& attributes
486     ) const
487 {
488     const XMLCh* name = attr.getName();
489     const XMLCh* format = attr.getNameFormat();
490     if (!name || !*name)
491         return;
492     if (!format || !*format)
493         format = saml2::Attribute::UNSPECIFIED;
494     else if (XMLString::equals(format, saml2::Attribute::URI_REFERENCE))
495         format = &chNull;
496     attrmap_t::const_iterator rule;
497     if ((rule = m_attrMap.find(pair<xstring,xstring>(name,format))) != m_attrMap.end()) {
498         auto_ptr<Attribute> a(rule->second.first->decode(request, rule->second.second, &attr, assertingParty, relyingParty));
499         if (a.get()) {
500             attributes.push_back(a.get());
501             a.release();
502             return;
503         }
504     }
505     else if (XMLString::equals(format, saml2::Attribute::UNSPECIFIED)) {
506         // As a fallback, if the format is "unspecified", null out the value and re-map.
507         if ((rule = m_attrMap.find(pair<xstring,xstring>(name,xstring()))) != m_attrMap.end()) {
508             auto_ptr<Attribute> a(rule->second.first->decode(request, rule->second.second, &attr, assertingParty, relyingParty));
509             if (a.get()) {
510                 attributes.push_back(a.get());
511                 a.release();
512                 return;
513             }
514         }
515     }
516
517     if (m_log.isInfoEnabled()) {
518         auto_ptr_char temp1(name);
519         auto_ptr_char temp2(format);
520         m_log.info("skipping unmapped SAML 2.0 Attribute with Name: %s%s%s", temp1.get(), *temp2.get() ? ", Format:" : "", temp2.get());
521     }
522 }
523
524 void XMLExtractorImpl::extractAttributes(
525     const Application& application,
526     const GenericRequest* request,
527     const char* assertingParty,
528     const char* relyingParty,
529     const saml1::AttributeStatement& statement,
530     ptr_vector<Attribute>& attributes
531     ) const
532 {
533     static void (XMLExtractorImpl::* extract)(
534         const Application&, const GenericRequest*, const char*, const char*, const saml1::Attribute&, ptr_vector<Attribute>&
535         ) const = &XMLExtractorImpl::extractAttributes;
536     for_each(
537         make_indirect_iterator(statement.getAttributes().begin()), make_indirect_iterator(statement.getAttributes().end()),
538         boost::bind(extract, this, boost::cref(application), request, assertingParty, relyingParty, _1, boost::ref(attributes))
539         );
540 }
541
542 void XMLExtractorImpl::extractAttributes(
543     const Application& application,
544     const GenericRequest* request,
545     const char* assertingParty,
546     const char* relyingParty,
547     const saml2::AttributeStatement& statement,
548     ptr_vector<Attribute>& attributes
549     ) const
550 {
551     static void (XMLExtractorImpl::* extract)(
552         const Application&, const GenericRequest*, const char*, const char*, const saml2::Attribute&, ptr_vector<Attribute>&
553         ) const = &XMLExtractorImpl::extractAttributes;
554     for_each(
555         make_indirect_iterator(statement.getAttributes().begin()), make_indirect_iterator(statement.getAttributes().end()),
556         boost::bind(extract, this, boost::cref(application), request, assertingParty, relyingParty, _1, boost::ref(attributes))
557         );
558 }
559
560 void XMLExtractorImpl::extractAttributes(
561     const Application& application,
562     const GenericRequest* request,
563     const ObservableMetadataProvider* observable,
564     const XMLCh* entityID,
565     const char* relyingParty,
566     const Extensions& ext,
567     ptr_vector<Attribute>& attributes
568     ) const
569 {
570     const vector<XMLObject*>& exts = ext.getUnknownXMLObjects();
571     for (vector<XMLObject*>::const_iterator i = exts.begin(); i != exts.end(); ++i) {
572         const EntityAttributes* container = dynamic_cast<const EntityAttributes*>(*i);
573         if (!container)
574             continue;
575
576         bool useCache = false;
577         map<const ObservableMetadataProvider*,decoded_t>::iterator cacheEntry;
578
579         // Check for cached result.
580         if (observable && m_metaAttrCaching) {
581             m_attrLock->rdlock();
582             cacheEntry = m_decodedMap.find(observable);
583             if (cacheEntry == m_decodedMap.end()) {
584                 // We need to elevate the lock and retry.
585                 m_attrLock->unlock();
586                 m_attrLock->wrlock();
587                 cacheEntry = m_decodedMap.find(observable);
588                 if (cacheEntry == m_decodedMap.end()) {
589                     SharedLock locker(m_attrLock, false);   // guard in case these throw
590
591                     // It's still brand new, so hook it for cache activation.
592                     observable->addObserver(this);
593
594                     // Prime the map reference with an empty decoded map.
595                     cacheEntry = m_decodedMap.insert(make_pair(observable,decoded_t())).first;
596
597                     // Downgrade the lock.
598                     // We don't have to recheck because we never erase the master map entry entirely, even on changes.
599                     locker.release();   // unguard for lock downgrade
600                     m_attrLock->unlock();
601                     m_attrLock->rdlock();
602                 }
603             }
604             useCache = true;
605         }
606
607         if (useCache) {
608             // We're holding the lock, so check the cache.
609             decoded_t::iterator d = cacheEntry->second.find(container);
610             if (d != cacheEntry->second.end()) {
611                 SharedLock locker(m_attrLock, false);   // pop the lock when we're done
612                 for (vector<DDF>::iterator obj = d->second.begin(); obj != d->second.end(); ++obj) {
613                     auto_ptr<Attribute> wrapper(Attribute::unmarshall(*obj));
614                     m_log.debug("recovered cached metadata attribute (%s)", wrapper->getId());
615                     attributes.push_back(wrapper.get());
616                     wrapper.release();
617                 }
618                 break;
619             }
620         }
621
622         // Add a guard for the lock if we're caching.
623         SharedLock locker(useCache ? m_attrLock.get() : nullptr, false);
624
625         // Use a holding area to support caching.
626         ptr_vector<Attribute> holding;
627
628         // Extract attributes into holding area with no asserting party set.
629         static void (XMLExtractorImpl::* extractV2Attr)(
630             const Application&, const GenericRequest*, const char*, const char*, const saml2::Attribute&, ptr_vector<Attribute>&
631             ) const = &XMLExtractorImpl::extractAttributes;
632         for_each(
633             make_indirect_iterator(container->getAttributes().begin()), make_indirect_iterator(container->getAttributes().end()),
634             boost::bind(extractV2Attr, this, boost::ref(application), request, (const char*)nullptr, relyingParty, _1, boost::ref(holding))
635             );
636
637         if (entityID && m_entityAssertions) {
638             const vector<saml2::Assertion*>& asserts = container->getAssertions();
639             for (indirect_iterator<vector<saml2::Assertion*>::const_iterator> assert = make_indirect_iterator(asserts.begin());
640                     assert != make_indirect_iterator(asserts.end()); ++assert) {
641                 if (!(assert->getSignature())) {
642                     if (m_log.isDebugEnabled()) {
643                         auto_ptr_char eid(entityID);
644                         m_log.debug("skipping unsigned assertion in metadata extension for entity (%s)", eid.get());
645                     }
646                     continue;
647                 }
648                 else if (assert->getAttributeStatements().empty()) {
649                     if (m_log.isDebugEnabled()) {
650                         auto_ptr_char eid(entityID);
651                         m_log.debug("skipping assertion with no AttributeStatement in metadata extension for entity (%s)", eid.get());
652                     }
653                     continue;
654                 }
655                 else {
656                     // Check subject.
657                     const NameID* subject = assert->getSubject() ? assert->getSubject()->getNameID() : nullptr;
658                     if (!subject ||
659                             !XMLString::equals(subject->getFormat(), NameID::ENTITY) ||
660                             !XMLString::equals(subject->getName(), entityID)) {
661                         if (m_log.isDebugEnabled()) {
662                             auto_ptr_char eid(entityID);
663                             m_log.debug("skipping assertion with improper Subject in metadata extension for entity (%s)", eid.get());
664                         }
665                         continue;
666                     }
667                 }
668
669                 try {
670                     // Set up and evaluate a policy for an AA asserting attributes to us.
671                     shibsp::SecurityPolicy policy(application, &AttributeAuthorityDescriptor::ELEMENT_QNAME, false, m_policyId.c_str());
672                     Locker locker(m_metadata.get());
673                     if (m_metadata)
674                         policy.setMetadataProvider(m_metadata.get());
675                     if (m_trust)
676                         policy.setTrustEngine(m_trust.get());
677                     // Populate recipient as audience.
678                     const XMLCh* issuer = assert->getIssuer() ? assert->getIssuer()->getName() : nullptr;
679                     policy.getAudiences().push_back(application.getRelyingParty(issuer)->getXMLString("entityID").second);
680
681                     // Extract assertion information for policy.
682                     policy.setMessageID(assert->getID());
683                     policy.setIssueInstant(assert->getIssueInstantEpoch());
684                     policy.setIssuer(assert->getIssuer());
685
686                     // Look up metadata for issuer.
687                     if (policy.getIssuer() && policy.getMetadataProvider()) {
688                         if (policy.getIssuer()->getFormat() && !XMLString::equals(policy.getIssuer()->getFormat(), saml2::NameIDType::ENTITY)) {
689                             m_log.debug("non-system entity issuer, skipping metadata lookup");
690                         }
691                         else {
692                             m_log.debug("searching metadata for entity assertion issuer...");
693                             pair<const EntityDescriptor*,const RoleDescriptor*> lookup;
694                             MetadataProvider::Criteria& mc = policy.getMetadataProviderCriteria();
695                             mc.entityID_unicode = policy.getIssuer()->getName();
696                             mc.role = &AttributeAuthorityDescriptor::ELEMENT_QNAME;
697                             mc.protocol = samlconstants::SAML20P_NS;
698                             lookup = policy.getMetadataProvider()->getEntityDescriptor(mc);
699                             if (!lookup.first) {
700                                 auto_ptr_char iname(policy.getIssuer()->getName());
701                                 m_log.debug("no metadata found, can't establish identity of issuer (%s)", iname.get());
702                             }
703                             else if (!lookup.second) {
704                                 m_log.debug("unable to find compatible AA role in metadata");
705                             }
706                             else {
707                                 policy.setIssuerMetadata(lookup.second);
708                             }
709                         }
710                     }
711
712                     // Authenticate the assertion. We have to clone and marshall it to establish the signature for verification.
713                     scoped_ptr<saml2::Assertion> tokencopy(assert->cloneAssertion());
714                     tokencopy->marshall();
715                     policy.evaluate(*tokencopy);
716                     if (!policy.isAuthenticated()) {
717                         if (m_log.isDebugEnabled()) {
718                             auto_ptr_char tempid(tokencopy->getID());
719                             auto_ptr_char eid(entityID);
720                             m_log.debug(
721                                 "failed to authenticate assertion (%s) in metadata extension for entity (%s)", tempid.get(), eid.get()
722                                 );
723                         }
724                         continue;
725                     }
726
727                     // Override the asserting/relying party names based on this new issuer.
728                     const EntityDescriptor* inlineEntity =
729                         policy.getIssuerMetadata() ? dynamic_cast<const EntityDescriptor*>(policy.getIssuerMetadata()->getParent()) : nullptr;
730                     auto_ptr_char inlineAssertingParty(inlineEntity ? inlineEntity->getEntityID() : nullptr);
731                     relyingParty = application.getRelyingParty(inlineEntity)->getString("entityID").second;
732
733                     // Use a private holding area for filtering purposes.
734                     ptr_vector<Attribute> holding2;
735                     const vector<saml2::Attribute*>& attrs2 =
736                         const_cast<const saml2::AttributeStatement*>(tokencopy->getAttributeStatements().front())->getAttributes();
737                     for_each(
738                         make_indirect_iterator(attrs2.begin()), make_indirect_iterator(attrs2.end()),
739                         boost::bind(extractV2Attr, this, boost::ref(application), request, inlineAssertingParty.get(), relyingParty, _1, boost::ref(holding2))
740                         );
741
742                     // Now we locally filter the attributes so that the actual issuer can be properly set.
743                     // If we relied on outside filtering, the attributes couldn't be distinguished from the
744                     // ones that come from the user's IdP.
745                     if (m_filter && !holding2.empty()) {
746
747                         // The filter API uses an unsafe container, so we have to transfer everything into one and back.
748                         vector<Attribute*> unsafe_holding2;
749
750                         // Use a local exception context since the container is unsafe.
751                         try {
752                             while (!holding2.empty()) {
753                                 ptr_vector<Attribute>::auto_type ptr = holding2.pop_back();
754                                 unsafe_holding2.push_back(ptr.get());
755                                 ptr.release();
756                             }
757                             BasicFilteringContext fc(application, unsafe_holding2, policy.getIssuerMetadata());
758                             Locker filtlocker(m_filter.get());
759                             m_filter->filterAttributes(fc, unsafe_holding2);
760
761                             // Transfer back to safe container
762                             while (!unsafe_holding2.empty()) {
763                                 auto_ptr<Attribute> ptr(unsafe_holding2.back());
764                                 unsafe_holding2.pop_back();
765                                 holding2.push_back(ptr.get());
766                                 ptr.release();
767                             }
768                         }
769                         catch (std::exception& ex) {
770                             m_log.error("caught exception filtering attributes: %s", ex.what());
771                             m_log.error("dumping extracted attributes due to filtering exception");
772                             for_each(unsafe_holding2.begin(), unsafe_holding2.end(), xmltooling::cleanup<Attribute>());
773                             holding2.clear();   // in case the exception was during transfer between containers
774                         }
775                     }
776
777                     if (!holding2.empty()) {
778                         // Copy them over to the main holding tank, which transfers ownership.
779                         holding.transfer(holding.end(), holding2);
780                     }
781                 }
782                 catch (std::exception& ex) {
783                     // Known exceptions are handled gracefully by skipping the assertion.
784                     if (m_log.isDebugEnabled()) {
785                         auto_ptr_char tempid(assert->getID());
786                         auto_ptr_char eid(entityID);
787                         m_log.debug(
788                             "exception authenticating assertion (%s) in metadata extension for entity (%s): %s",
789                             tempid.get(),
790                             eid.get(),
791                             ex.what()
792                             );
793                     }
794                     continue;
795                 }
796             }
797         }
798
799         if (!holding.empty()) {
800             if (useCache) {
801                 locker.release();   // unguard to upgrade lock
802                 m_attrLock->unlock();
803                 m_attrLock->wrlock();
804                 SharedLock locker2(m_attrLock, false);   // pop the lock when we're done
805                 if (cacheEntry->second.count(container) == 0) {
806                     static void (vector<DDF>::* push_back)(DDF const &) = &vector<DDF>::push_back;
807                     vector<DDF>& marshalled = cacheEntry->second[container];
808                     for_each(
809                         holding.begin(), holding.end(),
810                         boost::bind(push_back, boost::ref(marshalled), boost::bind(&Attribute::marshall, _1))
811                         );
812                 }
813             }
814
815             // Copy them to the output parameter, which transfers ownership.
816             attributes.transfer(attributes.end(), holding);
817         }
818
819         // If the lock is held, it's guarded.
820
821         break;  // only process a single extension element
822     }
823 }
824
825 void XMLExtractor::extractAttributes(
826     const Application& application, const GenericRequest* request, const RoleDescriptor* issuer, const XMLObject& xmlObject, vector<Attribute*>& attributes
827     ) const
828 {
829     if (!m_impl)
830         return;
831
832     ptr_vector<Attribute> holding;
833     extractAttributes(application, request, issuer, xmlObject, holding);
834
835     // Transfer ownership from the ptr_vector to the unsafe vector for API compatibility.
836     // Any throws should leave each container in a consistent state. The holding container
837     // is freed by us, and the result container by the caller.
838     while (!holding.empty()) {
839         ptr_vector<Attribute>::auto_type ptr = holding.pop_back();
840         attributes.push_back(ptr.get());
841         ptr.release();
842     }
843 }
844
845 void XMLExtractor::extractAttributes(
846     const Application& application, const GenericRequest* request, const RoleDescriptor* issuer, const XMLObject& xmlObject, ptr_vector<Attribute>& attributes
847     ) const
848 {
849     static void (XMLExtractor::* extractEncrypted)(
850         const Application&, const GenericRequest*, const RoleDescriptor*, const XMLObject&, ptr_vector<Attribute>&
851         ) const = &XMLExtractor::extractAttributes;
852     static void (XMLExtractorImpl::* extractV1Statement)(
853         const Application&, const GenericRequest*, const char*, const char*, const saml1::AttributeStatement&, ptr_vector<Attribute>&
854         ) const = &XMLExtractorImpl::extractAttributes;
855
856     const EntityDescriptor* entity = issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent()) : nullptr;
857     const char* relyingParty = application.getRelyingParty(entity)->getString("entityID").second;
858
859     // Check for statements.
860     if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::AttributeStatement::LOCAL_NAME)) {
861         const saml2::AttributeStatement* statement2 = dynamic_cast<const saml2::AttributeStatement*>(&xmlObject);
862         if (statement2) {
863             auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
864             m_impl->extractAttributes(application, request, assertingParty.get(), relyingParty, *statement2, attributes);
865             // Handle EncryptedAttributes inline so we have access to the role descriptor.
866             const vector<saml2::EncryptedAttribute*>& encattrs = statement2->getEncryptedAttributes();
867             for_each(
868                 make_indirect_iterator(encattrs.begin()), make_indirect_iterator(encattrs.end()),
869                 boost::bind(extractEncrypted, this, boost::ref(application), request, issuer, _1, boost::ref(attributes))
870                 );
871             return;
872         }
873
874         const saml1::AttributeStatement* statement1 = dynamic_cast<const saml1::AttributeStatement*>(&xmlObject);
875         if (statement1) {
876             auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
877             m_impl->extractAttributes(application, request, assertingParty.get(), relyingParty, *statement1, attributes);
878             return;
879         }
880
881         throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
882     }
883
884     // Check for assertions.
885     if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Assertion::LOCAL_NAME)) {
886         const saml2::Assertion* token2 = dynamic_cast<const saml2::Assertion*>(&xmlObject);
887         if (token2) {
888             auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
889             const vector<saml2::AttributeStatement*>& statements = token2->getAttributeStatements();
890             for (indirect_iterator<vector<saml2::AttributeStatement*>::const_iterator> s = make_indirect_iterator(statements.begin());
891                     s != make_indirect_iterator(statements.end()); ++s) {
892                 m_impl->extractAttributes(application, request, assertingParty.get(), relyingParty, *s, attributes);
893                 // Handle EncryptedAttributes inline so we have access to the role descriptor.
894                 const vector<saml2::EncryptedAttribute*>& encattrs = const_cast<const saml2::AttributeStatement&>(*s).getEncryptedAttributes();
895                 for_each(
896                     make_indirect_iterator(encattrs.begin()), make_indirect_iterator(encattrs.end()),
897                     boost::bind(extractEncrypted, this, boost::ref(application), request, issuer, _1, boost::ref(attributes))
898                     );
899             }
900             return;
901         }
902
903         const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(&xmlObject);
904         if (token1) {
905             auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
906             const vector<saml1::AttributeStatement*>& statements = token1->getAttributeStatements();
907             for_each(make_indirect_iterator(statements.begin()), make_indirect_iterator(statements.end()),
908                 boost::bind(extractV1Statement, m_impl.get(), boost::ref(application), request, assertingParty.get(), relyingParty, _1, boost::ref(attributes))
909                 );
910             return;
911         }
912
913         throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
914     }
915
916     // Check for metadata.
917     if (XMLString::equals(xmlObject.getElementQName().getNamespaceURI(), samlconstants::SAML20MD_NS)) {
918         const RoleDescriptor* roleToExtract = dynamic_cast<const RoleDescriptor*>(&xmlObject);
919         const EntityDescriptor* entityToExtract = roleToExtract ? dynamic_cast<const EntityDescriptor*>(roleToExtract->getParent()) : nullptr;
920         if (!entityToExtract)
921             throw AttributeExtractionException("Unable to extract attributes, unknown metadata object type.");
922         const Extensions* ext = entityToExtract->getExtensions();
923         if (ext) {
924             m_impl->extractAttributes(
925                 application,
926                 request,
927                 dynamic_cast<const ObservableMetadataProvider*>(application.getMetadataProvider(false)),
928                 entityToExtract->getEntityID(),
929                 relyingParty,
930                 *ext,
931                 attributes
932                 );
933         }
934         const EntitiesDescriptor* group = dynamic_cast<const EntitiesDescriptor*>(entityToExtract->getParent());
935         while (group) {
936             ext = group->getExtensions();
937             if (ext) {
938                 m_impl->extractAttributes(
939                     application,
940                     request,
941                     dynamic_cast<const ObservableMetadataProvider*>(application.getMetadataProvider(false)),
942                     nullptr,   // not an entity, so inline assertions won't be processed
943                     relyingParty,
944                     *ext,
945                     attributes
946                     );
947             }
948             group = dynamic_cast<const EntitiesDescriptor*>(group->getParent());
949         }
950         return;
951     }
952
953     // Check for attributes.
954     if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Attribute::LOCAL_NAME)) {
955         auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
956         const saml2::Attribute* attr2 = dynamic_cast<const saml2::Attribute*>(&xmlObject);
957         if (attr2)
958             return m_impl->extractAttributes(application, request, assertingParty.get(), relyingParty, *attr2, attributes);
959
960         const saml1::Attribute* attr1 = dynamic_cast<const saml1::Attribute*>(&xmlObject);
961         if (attr1)
962             return m_impl->extractAttributes(application, request, assertingParty.get(), relyingParty, *attr1, attributes);
963
964         throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
965     }
966
967     if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), EncryptedAttribute::LOCAL_NAME)) {
968         const EncryptedAttribute* encattr = dynamic_cast<const EncryptedAttribute*>(&xmlObject);
969         if (encattr) {
970             const XMLCh* recipient = application.getXMLString("entityID").second;
971             CredentialResolver* cr = application.getCredentialResolver();
972             if (!cr) {
973                 m_log.warn("found encrypted attribute, but no CredentialResolver was available");
974                 return;
975             }
976
977             try {
978                 Locker credlocker(cr);
979                 if (issuer) {
980                     MetadataCredentialCriteria mcc(*issuer);
981                     scoped_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient, &mcc));
982                     if (m_log.isDebugEnabled())
983                         m_log.debugStream() << "decrypted Attribute: " << *decrypted << logging::eol;
984                     return extractAttributes(application, request, issuer, *decrypted, attributes);
985                 }
986                 else {
987                     scoped_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient));
988                     if (m_log.isDebugEnabled())
989                         m_log.debugStream() << "decrypted Attribute: " << *decrypted << logging::eol;
990                     return extractAttributes(application, request, issuer, *decrypted, attributes);
991                 }
992             }
993             catch (std::exception& ex) {
994                 m_log.error("failed to decrypt Attribute: %s", ex.what());
995                 return;
996             }
997         }
998     }
999
1000     // Check for NameIDs.
1001     const NameID* name2 = dynamic_cast<const NameID*>(&xmlObject);
1002     if (name2) {
1003         auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
1004         return m_impl->extractAttributes(application, assertingParty.get(), relyingParty, *name2, attributes);
1005     }
1006
1007     const NameIdentifier* name1 = dynamic_cast<const NameIdentifier*>(&xmlObject);
1008     if (name1) {
1009         auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
1010         return m_impl->extractAttributes(application, assertingParty.get(), relyingParty, *name1, attributes);
1011     }
1012
1013     m_log.debug("unable to extract attributes, unknown XML object type: %s", xmlObject.getElementQName().toString().c_str());
1014 }
1015
1016 pair<bool,DOMElement*> XMLExtractor::background_load()
1017 {
1018     // Load from source using base class.
1019     pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
1020
1021     // If we own it, wrap it.
1022     XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);
1023
1024     scoped_ptr<XMLExtractorImpl> impl(new XMLExtractorImpl(raw.second, m_log));
1025
1026     // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
1027     impl->setDocument(docjanitor.release());
1028
1029     // Perform the swap inside a lock.
1030     if (m_lock)
1031         m_lock->wrlock();
1032     SharedLock locker(m_lock, false);
1033     m_impl.swap(impl);
1034
1035     return make_pair(false,(DOMElement*)nullptr);
1036 }