fd8ebd798c3d9d36c2db045ebe9951f2a59e4695
[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         string 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(nullptr) {
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(nullptr),
231         m_policyId(XMLHelper::getAttrString(e, nullptr, metadataPolicyId)),
232         m_metadata(nullptr),
233         m_trust(nullptr),
234         m_filter(nullptr),
235         m_entityAssertions(true),
236         m_attrLock(nullptr)
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             string t(XMLHelper::getAttrString(child, nullptr, _type));
249             if (t.empty())
250                 throw ConfigurationException("MetadataProvider element missing type attribute.");
251             m_log.info("building MetadataProvider of type %s...", t.c_str());
252             auto_ptr<MetadataProvider> mp(SAMLConfig::getConfig().MetadataProviderManager.newPlugin(t.c_str(), 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                 string t(XMLHelper::getAttrString(child, nullptr, _type));
268                 if (t.empty())
269                     throw ConfigurationException("TrustEngine element missing type attribute.");
270                 m_log.info("building TrustEngine of type %s...", t.c_str());
271                 m_trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t.c_str(), 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                 string t(XMLHelper::getAttrString(child, nullptr, _type));
286                 if (t.empty())
287                     throw ConfigurationException("AttributeFilter element missing type attribute.");
288                 m_log.info("building AttributeFilter of type %s...", t.c_str());
289                 m_filter = SPConfig::getConfig().AttributeFilterManager.newPlugin(t.c_str(), 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(nullptr, _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(nullptr, _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=nullptr;
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, nullptr);
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(nullptr, 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(nullptr, _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 : nullptr;
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
537 #ifdef HAVE_GOOD_STL
538     if ((rule=m_attrMap.find(pair<xstring,xstring>(name,format))) != m_attrMap.end()) {
539 #else
540     auto_ptr_char temp1(name);
541     auto_ptr_char temp2(format);
542     if ((rule=m_attrMap.find(pair<string,string>(temp1.get(),temp2.get()))) != m_attrMap.end()) {
543 #endif
544         Attribute* a = rule->second.first->decode(rule->second.second, &attr, assertingParty, relyingParty);
545         if (a) {
546             attributes.push_back(a);
547             return;
548         }
549     }
550     else if (XMLString::equals(format, saml2::Attribute::UNSPECIFIED)) {
551         // As a fallback, if the format is "unspecified", null out the value and re-map.
552 #ifdef HAVE_GOOD_STL
553         if ((rule=m_attrMap.find(pair<xstring,xstring>(name,xstring()))) != m_attrMap.end()) {
554 #else
555         if ((rule=m_attrMap.find(pair<string,string>(temp1.get(),string()))) != m_attrMap.end()) {
556 #endif
557             Attribute* a = rule->second.first->decode(rule->second.second, &attr, assertingParty, relyingParty);
558             if (a) {
559                 attributes.push_back(a);
560                 return;
561             }
562         }
563     }
564
565     if (m_log.isInfoEnabled()) {
566 #ifdef HAVE_GOOD_STL
567         auto_ptr_char temp1(name);
568         auto_ptr_char temp2(format);
569 #endif
570         m_log.info("skipping unmapped SAML 2.0 Attribute with Name: %s%s%s", temp1.get(), *temp2.get() ? ", Format:" : "", temp2.get());
571     }
572 }
573
574 void XMLExtractorImpl::extractAttributes(
575     const Application& application,
576     const char* assertingParty,
577     const char* relyingParty,
578     const saml1::AttributeStatement& statement,
579     vector<Attribute*>& attributes
580     ) const
581 {
582     const vector<saml1::Attribute*>& attrs = statement.getAttributes();
583     for (vector<saml1::Attribute*>::const_iterator a = attrs.begin(); a!=attrs.end(); ++a)
584         extractAttributes(application, assertingParty, relyingParty, *(*a), attributes);
585 }
586
587 void XMLExtractorImpl::extractAttributes(
588     const Application& application,
589     const char* assertingParty,
590     const char* relyingParty,
591     const saml2::AttributeStatement& statement,
592     vector<Attribute*>& attributes
593     ) const
594 {
595     const vector<saml2::Attribute*>& attrs = statement.getAttributes();
596     for (vector<saml2::Attribute*>::const_iterator a = attrs.begin(); a!=attrs.end(); ++a)
597         extractAttributes(application, assertingParty, relyingParty, *(*a), attributes);
598 }
599
600 void XMLExtractorImpl::extractAttributes(
601     const Application& application,
602     const ObservableMetadataProvider* observable,
603     const XMLCh* entityID,
604     const char* relyingParty,
605     const Extensions& ext,
606     vector<Attribute*>& attributes
607     ) const
608 {
609     const vector<XMLObject*>& exts = ext.getUnknownXMLObjects();
610     for (vector<XMLObject*>::const_iterator i = exts.begin(); i!=exts.end(); ++i) {
611         const EntityAttributes* container = dynamic_cast<const EntityAttributes*>(*i);
612         if (!container)
613             continue;
614
615         bool useCache = false;
616         map<const ObservableMetadataProvider*,decoded_t>::iterator cacheEntry;
617
618         // Check for cached result.
619         if (observable) {
620             m_attrLock->rdlock();
621             cacheEntry = m_decodedMap.find(observable);
622             if (cacheEntry == m_decodedMap.end()) {
623                 // We need to elevate the lock and retry.
624                 m_attrLock->unlock();
625                 m_attrLock->wrlock();
626                 cacheEntry = m_decodedMap.find(observable);
627                 if (cacheEntry==m_decodedMap.end()) {
628
629                     // It's still brand new, so hook it for cache activation.
630                     observable->addObserver(this);
631
632                     // Prime the map reference with an empty decoded map.
633                     cacheEntry = m_decodedMap.insert(make_pair(observable,decoded_t())).first;
634
635                     // Downgrade the lock.
636                     // We don't have to recheck because we never erase the master map entry entirely, even on changes.
637                     m_attrLock->unlock();
638                     m_attrLock->rdlock();
639                 }
640             }
641             useCache = true;
642         }
643
644         if (useCache) {
645             // We're holding a read lock, so check the cache.
646             decoded_t::iterator d = cacheEntry->second.find(container);
647             if (d != cacheEntry->second.end()) {
648                 SharedLock locker(m_attrLock, false);   // pop the lock when we're done
649                 for (vector<DDF>::iterator obj = d->second.begin(); obj != d->second.end(); ++obj) {
650                     auto_ptr<Attribute> wrapper(Attribute::unmarshall(*obj));
651                     m_log.debug("recovered cached metadata attribute (%s)", wrapper->getId());
652                     attributes.push_back(wrapper.release());
653                 }
654                 break;
655             }
656         }
657
658         // Use a holding area to support caching.
659         vector<Attribute*> holding;
660
661         const vector<saml2::Attribute*>& attrs = container->getAttributes();
662         for (vector<saml2::Attribute*>::const_iterator attr = attrs.begin(); attr != attrs.end(); ++attr) {
663             try {
664                 extractAttributes(application, nullptr, relyingParty, *(*attr), holding);
665             }
666             catch (...) {
667                 if (useCache)
668                     m_attrLock->unlock();
669                 for_each(holding.begin(), holding.end(), xmltooling::cleanup<Attribute>());
670                 throw;
671             }
672         }
673
674         if (entityID && m_entityAssertions) {
675             const vector<saml2::Assertion*>& asserts = container->getAssertions();
676             for (vector<saml2::Assertion*>::const_iterator assert = asserts.begin(); assert != asserts.end(); ++assert) {
677                 if (!(*assert)->getSignature()) {
678                     if (m_log.isDebugEnabled()) {
679                         auto_ptr_char eid(entityID);
680                         m_log.debug("skipping unsigned assertion in metadata extension for entity (%s)", eid.get());
681                     }
682                     continue;
683                 }
684                 else if ((*assert)->getAttributeStatements().empty()) {
685                     if (m_log.isDebugEnabled()) {
686                         auto_ptr_char eid(entityID);
687                         m_log.debug("skipping assertion with no AttributeStatement in metadata extension for entity (%s)", eid.get());
688                     }
689                     continue;
690                 }
691                 else {
692                     // Check subject.
693                     const NameID* subject = (*assert)->getSubject() ? (*assert)->getSubject()->getNameID() : nullptr;
694                     if (!subject ||
695                             !XMLString::equals(subject->getFormat(), NameID::ENTITY) ||
696                             !XMLString::equals(subject->getName(), entityID)) {
697                         if (m_log.isDebugEnabled()) {
698                             auto_ptr_char eid(entityID);
699                             m_log.debug("skipping assertion with improper Subject in metadata extension for entity (%s)", eid.get());
700                         }
701                         continue;
702                     }
703                 }
704
705                 // Use a private holding area for filtering purposes.
706                 vector<Attribute*> holding2;
707
708                 try {
709                     // Set up and evaluate a policy for an AA asserting attributes to us.
710                     shibsp::SecurityPolicy policy(application, &AttributeAuthorityDescriptor::ELEMENT_QNAME, false, m_policyId.c_str());
711                     Locker locker(m_metadata);
712                     if (m_metadata)
713                         policy.setMetadataProvider(m_metadata);
714                     if (m_trust)
715                         policy.setTrustEngine(m_trust);
716                     // Populate recipient as audience.
717                     const XMLCh* issuer = (*assert)->getIssuer() ? (*assert)->getIssuer()->getName() : nullptr;
718                     policy.getAudiences().push_back(application.getRelyingParty(issuer)->getXMLString("entityID").second);
719
720                     // Extract assertion information for policy.
721                     policy.setMessageID((*assert)->getID());
722                     policy.setIssueInstant((*assert)->getIssueInstantEpoch());
723                     policy.setIssuer((*assert)->getIssuer());
724
725                     // Look up metadata for issuer.
726                     if (policy.getIssuer() && policy.getMetadataProvider()) {
727                         if (policy.getIssuer()->getFormat() && !XMLString::equals(policy.getIssuer()->getFormat(), saml2::NameIDType::ENTITY)) {
728                             m_log.debug("non-system entity issuer, skipping metadata lookup");
729                         }
730                         else {
731                             m_log.debug("searching metadata for entity assertion issuer...");
732                             pair<const EntityDescriptor*,const RoleDescriptor*> lookup;
733                             MetadataProvider::Criteria& mc = policy.getMetadataProviderCriteria();
734                             mc.entityID_unicode = policy.getIssuer()->getName();
735                             mc.role = &AttributeAuthorityDescriptor::ELEMENT_QNAME;
736                             mc.protocol = samlconstants::SAML20P_NS;
737                             lookup = policy.getMetadataProvider()->getEntityDescriptor(mc);
738                             if (!lookup.first) {
739                                 auto_ptr_char iname(policy.getIssuer()->getName());
740                                 m_log.debug("no metadata found, can't establish identity of issuer (%s)", iname.get());
741                             }
742                             else if (!lookup.second) {
743                                 m_log.debug("unable to find compatible AA role in metadata");
744                             }
745                             else {
746                                 policy.setIssuerMetadata(lookup.second);
747                             }
748                         }
749                     }
750
751                     // Authenticate the assertion. We have to clone and marshall it to establish the signature for verification.
752                     auto_ptr<saml2::Assertion> tokencopy((*assert)->cloneAssertion());
753                     tokencopy->marshall();
754                     policy.evaluate(*tokencopy);
755                     if (!policy.isAuthenticated()) {
756                         if (m_log.isDebugEnabled()) {
757                             auto_ptr_char tempid(tokencopy->getID());
758                             auto_ptr_char eid(entityID);
759                             m_log.debug(
760                                 "failed to authenticate assertion (%s) in metadata extension for entity (%s)", tempid.get(), eid.get()
761                                 );
762                         }
763                         continue;
764                     }
765
766                     // Override the asserting/relying party names based on this new issuer.
767                     const EntityDescriptor* inlineEntity =
768                         policy.getIssuerMetadata() ? dynamic_cast<const EntityDescriptor*>(policy.getIssuerMetadata()->getParent()) : nullptr;
769                     auto_ptr_char inlineAssertingParty(inlineEntity ? inlineEntity->getEntityID() : nullptr);
770                     relyingParty = application.getRelyingParty(inlineEntity)->getString("entityID").second;
771                     const vector<saml2::Attribute*>& attrs2 =
772                         const_cast<const saml2::AttributeStatement*>(tokencopy->getAttributeStatements().front())->getAttributes();
773                     for (vector<saml2::Attribute*>::const_iterator a = attrs2.begin(); a!=attrs2.end(); ++a)
774                         extractAttributes(application, inlineAssertingParty.get(), relyingParty, *(*a), holding2);
775
776                     // Now we locally filter the attributes so that the actual issuer can be properly set.
777                     // If we relied on outside filtering, the attributes couldn't be distinguished from the
778                     // ones that come from the user's IdP.
779                     if (m_filter && !holding2.empty()) {
780                         BasicFilteringContext fc(application, holding2, policy.getIssuerMetadata());
781                         Locker filtlocker(m_filter);
782                         try {
783                             m_filter->filterAttributes(fc, holding2);
784                         }
785                         catch (exception& ex) {
786                             m_log.error("caught exception filtering attributes: %s", ex.what());
787                             m_log.error("dumping extracted attributes due to filtering exception");
788                             for_each(holding2.begin(), holding2.end(), xmltooling::cleanup<Attribute>());
789                             holding2.clear();
790                         }
791                     }
792
793                     if (!holding2.empty()) {
794                         // Copy them over to the main holding tank.
795                         holding.insert(holding.end(), holding2.begin(), holding2.end());
796                     }
797                 }
798                 catch (exception& ex) {
799                     // Known exceptions are handled gracefully by skipping the assertion.
800                     if (m_log.isDebugEnabled()) {
801                         auto_ptr_char tempid((*assert)->getID());
802                         auto_ptr_char eid(entityID);
803                         m_log.debug(
804                             "exception authenticating assertion (%s) in metadata extension for entity (%s): %s",
805                             tempid.get(),
806                             eid.get(),
807                             ex.what()
808                             );
809                     }
810                     for_each(holding2.begin(), holding2.end(), xmltooling::cleanup<Attribute>());
811                     continue;
812                 }
813                 catch (...) {
814                     // Unknown exceptions are fatal.
815                     if (useCache)
816                         m_attrLock->unlock();
817                     for_each(holding.begin(), holding.end(), xmltooling::cleanup<Attribute>());
818                     for_each(holding2.begin(), holding2.end(), xmltooling::cleanup<Attribute>());
819                     throw;
820                 }
821             }
822         }
823
824         if (!holding.empty()) {
825             if (useCache) {
826                 m_attrLock->unlock();
827                 m_attrLock->wrlock();
828                 SharedLock locker(m_attrLock, false);   // pop the lock when we're done
829                 if (cacheEntry->second.count(container) == 0) {
830                     for (vector<Attribute*>::const_iterator held = holding.begin(); held != holding.end(); ++held)
831                         cacheEntry->second[container].push_back((*held)->marshall());
832                 }
833             }
834             attributes.insert(attributes.end(), holding.begin(), holding.end());
835         }
836         else if (useCache) {
837             m_attrLock->unlock();
838         }
839
840         break;  // only process a single extension element
841     }
842 }
843
844 void XMLExtractor::extractAttributes(
845     const Application& application, const RoleDescriptor* issuer, const XMLObject& xmlObject, vector<Attribute*>& attributes
846     ) const
847 {
848     if (!m_impl)
849         return;
850
851     const EntityDescriptor* entity = issuer ? dynamic_cast<const EntityDescriptor*>(issuer->getParent()) : nullptr;
852     const char* relyingParty = application.getRelyingParty(entity)->getString("entityID").second;
853
854     // Check for statements.
855     if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::AttributeStatement::LOCAL_NAME)) {
856         const saml2::AttributeStatement* statement2 = dynamic_cast<const saml2::AttributeStatement*>(&xmlObject);
857         if (statement2) {
858             auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
859             m_impl->extractAttributes(application, assertingParty.get(), relyingParty, *statement2, attributes);
860             // Handle EncryptedAttributes inline so we have access to the role descriptor.
861             const vector<saml2::EncryptedAttribute*>& encattrs = statement2->getEncryptedAttributes();
862             for (vector<saml2::EncryptedAttribute*>::const_iterator ea = encattrs.begin(); ea!=encattrs.end(); ++ea)
863                 extractAttributes(application, issuer, *(*ea), attributes);
864             return;
865         }
866
867         const saml1::AttributeStatement* statement1 = dynamic_cast<const saml1::AttributeStatement*>(&xmlObject);
868         if (statement1) {
869             auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
870             m_impl->extractAttributes(application, assertingParty.get(), relyingParty, *statement1, attributes);
871             return;
872         }
873
874         throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
875     }
876
877     // Check for assertions.
878     if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Assertion::LOCAL_NAME)) {
879         const saml2::Assertion* token2 = dynamic_cast<const saml2::Assertion*>(&xmlObject);
880         if (token2) {
881             auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
882             const vector<saml2::AttributeStatement*>& statements = token2->getAttributeStatements();
883             for (vector<saml2::AttributeStatement*>::const_iterator s = statements.begin(); s!=statements.end(); ++s) {
884                 m_impl->extractAttributes(application, assertingParty.get(), relyingParty, *(*s), attributes);
885                 // Handle EncryptedAttributes inline so we have access to the role descriptor.
886                 const vector<saml2::EncryptedAttribute*>& encattrs = const_cast<const saml2::AttributeStatement*>(*s)->getEncryptedAttributes();
887                 for (vector<saml2::EncryptedAttribute*>::const_iterator ea = encattrs.begin(); ea!=encattrs.end(); ++ea)
888                     extractAttributes(application, issuer, *(*ea), attributes);
889             }
890             return;
891         }
892
893         const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(&xmlObject);
894         if (token1) {
895             auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
896             const vector<saml1::AttributeStatement*>& statements = token1->getAttributeStatements();
897             for (vector<saml1::AttributeStatement*>::const_iterator s = statements.begin(); s!=statements.end(); ++s)
898                 m_impl->extractAttributes(application, assertingParty.get(), relyingParty, *(*s), attributes);
899             return;
900         }
901
902         throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
903     }
904
905     // Check for metadata.
906     if (XMLString::equals(xmlObject.getElementQName().getNamespaceURI(), samlconstants::SAML20MD_NS)) {
907         const RoleDescriptor* roleToExtract = dynamic_cast<const RoleDescriptor*>(&xmlObject);
908         const EntityDescriptor* entityToExtract = roleToExtract ? dynamic_cast<const EntityDescriptor*>(roleToExtract->getParent()) : nullptr;
909         if (!entityToExtract)
910             throw AttributeExtractionException("Unable to extract attributes, unknown metadata object type.");
911         const Extensions* ext = entityToExtract->getExtensions();
912         if (ext) {
913             m_impl->extractAttributes(
914                 application,
915                 dynamic_cast<const ObservableMetadataProvider*>(application.getMetadataProvider(false)),
916                 entityToExtract->getEntityID(),
917                 relyingParty,
918                 *ext,
919                 attributes
920                 );
921         }
922         const EntitiesDescriptor* group = dynamic_cast<const EntitiesDescriptor*>(entityToExtract->getParent());
923         while (group) {
924             ext = group->getExtensions();
925             if (ext) {
926                 m_impl->extractAttributes(
927                     application,
928                     dynamic_cast<const ObservableMetadataProvider*>(application.getMetadataProvider(false)),
929                     nullptr,   // not an entity, so inline assertions won't be processed
930                     relyingParty,
931                     *ext,
932                     attributes
933                     );
934             }
935             group = dynamic_cast<const EntitiesDescriptor*>(group->getParent());
936         }
937         return;
938     }
939
940     // Check for attributes.
941     if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), saml1::Attribute::LOCAL_NAME)) {
942         auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
943         const saml2::Attribute* attr2 = dynamic_cast<const saml2::Attribute*>(&xmlObject);
944         if (attr2)
945             return m_impl->extractAttributes(application, assertingParty.get(), relyingParty, *attr2, attributes);
946
947         const saml1::Attribute* attr1 = dynamic_cast<const saml1::Attribute*>(&xmlObject);
948         if (attr1)
949             return m_impl->extractAttributes(application, assertingParty.get(), relyingParty, *attr1, attributes);
950
951         throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
952     }
953
954     if (XMLString::equals(xmlObject.getElementQName().getLocalPart(), EncryptedAttribute::LOCAL_NAME)) {
955         const EncryptedAttribute* encattr = dynamic_cast<const EncryptedAttribute*>(&xmlObject);
956         if (encattr) {
957             const XMLCh* recipient = application.getXMLString("entityID").second;
958             CredentialResolver* cr = application.getCredentialResolver();
959             if (!cr) {
960                 m_log.warn("found encrypted attribute, but no CredentialResolver was available");
961                 return;
962             }
963
964             try {
965                 Locker credlocker(cr);
966                 if (issuer) {
967                     MetadataCredentialCriteria mcc(*issuer);
968                     auto_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient, &mcc));
969                     if (m_log.isDebugEnabled())
970                         m_log.debugStream() << "decrypted Attribute: " << *(decrypted.get()) << logging::eol;
971                     return extractAttributes(application, issuer, *(decrypted.get()), attributes);
972                 }
973                 else {
974                     auto_ptr<XMLObject> decrypted(encattr->decrypt(*cr, recipient));
975                     if (m_log.isDebugEnabled())
976                         m_log.debugStream() << "decrypted Attribute: " << *(decrypted.get()) << logging::eol;
977                     return extractAttributes(application, issuer, *(decrypted.get()), attributes);
978                 }
979             }
980             catch (exception& ex) {
981                 m_log.error("caught exception decrypting Attribute: %s", ex.what());
982                 return;
983             }
984         }
985     }
986
987     // Check for NameIDs.
988     const NameID* name2 = dynamic_cast<const NameID*>(&xmlObject);
989     if (name2) {
990         auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
991         return m_impl->extractAttributes(application, assertingParty.get(), relyingParty, *name2, attributes);
992     }
993
994     const NameIdentifier* name1 = dynamic_cast<const NameIdentifier*>(&xmlObject);
995     if (name1) {
996         auto_ptr_char assertingParty(entity ? entity->getEntityID() : nullptr);
997         return m_impl->extractAttributes(application, assertingParty.get(), relyingParty, *name1, attributes);
998     }
999
1000     throw AttributeExtractionException("Unable to extract attributes, unknown object type.");
1001 }
1002
1003 pair<bool,DOMElement*> XMLExtractor::background_load()
1004 {
1005     // Load from source using base class.
1006     pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
1007
1008     // If we own it, wrap it.
1009     XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);
1010
1011     XMLExtractorImpl* impl = new XMLExtractorImpl(raw.second, m_log);
1012
1013     // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
1014     impl->setDocument(docjanitor.release());
1015
1016     // Perform the swap inside a lock.
1017     if (m_lock)
1018         m_lock->wrlock();
1019     SharedLock locker(m_lock, false);
1020     delete m_impl;
1021     m_impl = impl;
1022
1023     return make_pair(false,(DOMElement*)nullptr);
1024 }