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