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