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