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