2d27a30f7e5526ab978a61e7f699add4e7c3507e
[shibboleth/cpp-sp.git] / shibsp / attribute / resolver / impl / SimpleAggregationAttributeResolver.cpp
1 /*
2  *  Copyright 2009-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  * SimpleAggregationAttributeResolver.cpp
19  *
20  * AttributeResolver based on SAML queries to third-party AA sources.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "Application.h"
26 #include "ServiceProvider.h"
27 #include "SessionCache.h"
28 #include "attribute/NameIDAttribute.h"
29 #include "attribute/filtering/AttributeFilter.h"
30 #include "attribute/filtering/BasicFilteringContext.h"
31 #include "attribute/resolver/AttributeExtractor.h"
32 #include "attribute/resolver/AttributeResolver.h"
33 #include "attribute/resolver/ResolutionContext.h"
34 #include "binding/SOAPClient.h"
35 #include "metadata/MetadataProviderCriteria.h"
36 #include "security/SecurityPolicy.h"
37 #include "security/SecurityPolicyProvider.h"
38 #include "util/SPConstants.h"
39
40 #include <saml/exceptions.h>
41 #include <saml/SAMLConfig.h>
42 #include <saml/saml2/binding/SAML2SOAPClient.h>
43 #include <saml/saml2/core/Protocols.h>
44 #include <saml/saml2/metadata/Metadata.h>
45 #include <saml/saml2/metadata/MetadataCredentialCriteria.h>
46 #include <saml/saml2/metadata/MetadataProvider.h>
47 #include <xmltooling/XMLToolingConfig.h>
48 #include <xmltooling/security/TrustEngine.h>
49 #include <xmltooling/util/NDC.h>
50 #include <xmltooling/util/XMLHelper.h>
51 #include <xercesc/util/XMLUniDefs.hpp>
52
53 using namespace shibsp;
54 using namespace opensaml::saml2;
55 using namespace opensaml::saml2p;
56 using namespace opensaml::saml2md;
57 using namespace opensaml;
58 using namespace xmltooling;
59 using namespace std;
60
61 namespace shibsp {
62
63     class SHIBSP_DLLLOCAL SimpleAggregationContext : public ResolutionContext
64     {
65     public:
66         SimpleAggregationContext(const Application& application, const Session& session)
67             : m_app(application),
68               m_session(&session),
69               m_nameid(nullptr),
70               m_entityid(nullptr),
71               m_class(XMLString::transcode(session.getAuthnContextClassRef())),
72               m_decl(XMLString::transcode(session.getAuthnContextDeclRef())),
73               m_inputTokens(nullptr),
74               m_inputAttributes(nullptr) {
75         }
76
77         SimpleAggregationContext(
78             const Application& application,
79             const NameID* nameid=nullptr,
80             const XMLCh* entityID=nullptr,
81             const XMLCh* authncontext_class=nullptr,
82             const XMLCh* authncontext_decl=nullptr,
83             const vector<const opensaml::Assertion*>* tokens=nullptr,
84             const vector<shibsp::Attribute*>* attributes=nullptr
85             ) : m_app(application),
86                 m_session(nullptr),
87                 m_nameid(nameid),
88                 m_entityid(entityID ? XMLString::transcode(entityID) : nullptr),
89                 m_class(const_cast<XMLCh*>(authncontext_class)),
90                 m_decl(const_cast<XMLCh*>(authncontext_decl)),
91                 m_inputTokens(tokens),
92                 m_inputAttributes(attributes) {
93         }
94
95         ~SimpleAggregationContext() {
96             for_each(m_attributes.begin(), m_attributes.end(), xmltooling::cleanup<shibsp::Attribute>());
97             for_each(m_assertions.begin(), m_assertions.end(), xmltooling::cleanup<opensaml::Assertion>());
98             if (m_session) {
99                 XMLString::release(&m_class);
100                 XMLString::release(&m_decl);
101             }
102             XMLString::release(&m_entityid);
103         }
104
105         const Application& getApplication() const {
106             return m_app;
107         }
108         const char* getEntityID() const {
109             return m_session ? m_session->getEntityID() : m_entityid;
110         }
111         const NameID* getNameID() const {
112             return m_session ? m_session->getNameID() : m_nameid;
113         }
114         const XMLCh* getClassRef() const {
115             return m_class;
116         }
117         const XMLCh* getDeclRef() const {
118             return m_decl;
119         }
120         const Session* getSession() const {
121             return m_session;
122         }
123         const vector<shibsp::Attribute*>* getInputAttributes() const {
124             return m_inputAttributes;
125         }
126         const vector<const opensaml::Assertion*>* getInputTokens() const {
127             return m_inputTokens;
128         }
129         vector<shibsp::Attribute*>& getResolvedAttributes() {
130             return m_attributes;
131         }
132         vector<opensaml::Assertion*>& getResolvedAssertions() {
133             return m_assertions;
134         }
135
136     private:
137         const Application& m_app;
138         const Session* m_session;
139         const NameID* m_nameid;
140         char* m_entityid;
141         XMLCh* m_class;
142         XMLCh* m_decl;
143         const vector<const opensaml::Assertion*>* m_inputTokens;
144         const vector<shibsp::Attribute*>* m_inputAttributes;
145         vector<shibsp::Attribute*> m_attributes;
146         vector<opensaml::Assertion*> m_assertions;
147     };
148
149     class SHIBSP_DLLLOCAL SimpleAggregationResolver : public AttributeResolver
150     {
151     public:
152         SimpleAggregationResolver(const DOMElement* e);
153         ~SimpleAggregationResolver() {
154             delete m_trust;
155             delete m_metadata;
156             for_each(m_designators.begin(), m_designators.end(), xmltooling::cleanup<saml2::Attribute>());
157         }
158
159         Lockable* lock() {return this;}
160         void unlock() {}
161
162         ResolutionContext* createResolutionContext(
163             const Application& application,
164             const EntityDescriptor* issuer,
165             const XMLCh* protocol,
166             const NameID* nameid=nullptr,
167             const XMLCh* authncontext_class=nullptr,
168             const XMLCh* authncontext_decl=nullptr,
169             const vector<const opensaml::Assertion*>* tokens=nullptr,
170             const vector<shibsp::Attribute*>* attributes=nullptr
171             ) const {
172             return new SimpleAggregationContext(
173                 application, nameid, (issuer ? issuer->getEntityID() : nullptr), authncontext_class, authncontext_decl, tokens, attributes
174                 );
175         }
176
177         ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
178             return new SimpleAggregationContext(application,session);
179         }
180
181         void resolveAttributes(ResolutionContext& ctx) const;
182
183         void getAttributeIds(vector<string>& attributes) const {
184             // Nothing to do, only the extractor would actually generate them.
185         }
186
187     private:
188         bool doQuery(SimpleAggregationContext& ctx, const char* entityID, const NameID* name) const;
189
190         Category& m_log;
191         string m_policyId;
192         bool m_subjectMatch;
193         vector<string> m_attributeIds;
194         xstring m_format;
195         MetadataProvider* m_metadata;
196         TrustEngine* m_trust;
197         vector<saml2::Attribute*> m_designators;
198         vector< pair<string,bool> > m_sources;
199     };
200
201     AttributeResolver* SHIBSP_DLLLOCAL SimpleAggregationResolverFactory(const DOMElement* const & e)
202     {
203         return new SimpleAggregationResolver(e);
204     }
205
206     static const XMLCh attributeId[] =          UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,d);
207     static const XMLCh Entity[] =               UNICODE_LITERAL_6(E,n,t,i,t,y);
208     static const XMLCh EntityReference[] =      UNICODE_LITERAL_15(E,n,t,i,t,y,R,e,f,e,r,e,n,c,e);
209     static const XMLCh format[] =               UNICODE_LITERAL_6(f,o,r,m,a,t);
210     static const XMLCh _MetadataProvider[] =    UNICODE_LITERAL_16(M,e,t,a,d,a,t,a,P,r,o,v,i,d,e,r);
211     static const XMLCh policyId[] =             UNICODE_LITERAL_8(p,o,l,i,c,y,I,d);
212     static const XMLCh subjectMatch[] =         UNICODE_LITERAL_12(s,u,b,j,e,c,t,M,a,t,c,h);
213     static const XMLCh _TrustEngine[] =         UNICODE_LITERAL_11(T,r,u,s,t,E,n,g,i,n,e);
214     static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
215 };
216
217 SimpleAggregationResolver::SimpleAggregationResolver(const DOMElement* e)
218     : m_log(Category::getInstance(SHIBSP_LOGCAT".AttributeResolver.SimpleAggregation")),
219         m_policyId(XMLHelper::getAttrString(e, nullptr, policyId)),
220         m_subjectMatch(XMLHelper::getAttrBool(e, false, subjectMatch)),
221         m_metadata(nullptr), m_trust(nullptr)
222 {
223 #ifdef _DEBUG
224     xmltooling::NDC ndc("SimpleAggregationResolver");
225 #endif
226
227     const XMLCh* aid = e ? e->getAttributeNS(nullptr, attributeId) : nullptr;
228     if (aid && *aid) {
229         char* dup = XMLString::transcode(aid);
230         char* pos;
231         char* start = dup;
232         while (start && *start) {
233             while (*start && isspace(*start))
234                 start++;
235             if (!*start)
236                 break;
237             pos = strchr(start,' ');
238             if (pos)
239                 *pos=0;
240             m_attributeIds.push_back(start);
241             start = pos ? pos+1 : nullptr;
242         }
243         XMLString::release(&dup);
244
245         aid = e->getAttributeNS(nullptr, format);
246         if (aid && *aid)
247             m_format = aid;
248     }
249
250     DOMElement* child = XMLHelper::getFirstChildElement(e, _MetadataProvider);
251     if (child) {
252         string t(XMLHelper::getAttrString(child, nullptr, _type));
253         if (t.empty())
254             throw ConfigurationException("MetadataProvider element missing type attribute.");
255         m_log.info("building MetadataProvider of type %s...", t.c_str());
256         auto_ptr<MetadataProvider> mp(SAMLConfig::getConfig().MetadataProviderManager.newPlugin(t.c_str(), child));
257         mp->init();
258         m_metadata = mp.release();
259     }
260
261     child = XMLHelper::getFirstChildElement(e,  _TrustEngine);
262     if (child) {
263         try {
264             string t(XMLHelper::getAttrString(child, nullptr, _type));
265             if (t.empty())
266                 throw ConfigurationException("TrustEngine element missing type attribute.");
267             m_log.info("building TrustEngine of type %s...", t.c_str());
268             m_trust = XMLToolingConfig::getConfig().TrustEngineManager.newPlugin(t.c_str(), child);
269         }
270         catch (exception&) {
271             delete m_metadata;
272             throw;
273         }
274     }
275
276     child = XMLHelper::getFirstChildElement(e);
277     while (child) {
278         if (child->hasChildNodes() && XMLString::equals(child->getLocalName(), Entity)) {
279             aid = child->getFirstChild()->getNodeValue();
280             if (aid && *aid) {
281                 auto_ptr_char taid(aid);
282                 m_sources.push_back(pair<string,bool>(taid.get(),true));
283             }
284         }
285         else if (child->hasChildNodes() && XMLString::equals(child->getLocalName(), EntityReference)) {
286             aid = child->getFirstChild()->getNodeValue();
287             if (aid && *aid) {
288                 auto_ptr_char taid(aid);
289                 m_sources.push_back(pair<string,bool>(taid.get(),false));
290             }
291         }
292         else if (XMLHelper::isNodeNamed(child, samlconstants::SAML20_NS, saml2::Attribute::LOCAL_NAME)) {
293             try {
294                 auto_ptr<XMLObject> obj(saml2::AttributeBuilder::buildOneFromElement(child));
295                 saml2::Attribute* down = dynamic_cast<saml2::Attribute*>(obj.get());
296                 if (down) {
297                     m_designators.push_back(down);
298                     obj.release();
299                 }
300             }
301             catch (exception& ex) {
302                 m_log.error("exception loading attribute designator: %s", ex.what());
303             }
304         }
305         child = XMLHelper::getNextSiblingElement(child);
306     }
307 }
308
309 bool SimpleAggregationResolver::doQuery(SimpleAggregationContext& ctx, const char* entityID, const NameID* name) const
310 {
311 #ifdef _DEBUG
312     xmltooling::NDC ndc("doQuery");
313 #endif
314     const Application& application = ctx.getApplication();
315     MetadataProviderCriteria mc(application, entityID, &AttributeAuthorityDescriptor::ELEMENT_QNAME, samlconstants::SAML20P_NS);
316     Locker mlocker(m_metadata);
317     const AttributeAuthorityDescriptor* AA=nullptr;
318     pair<const EntityDescriptor*,const RoleDescriptor*> mdresult =
319         (m_metadata ? m_metadata : application.getMetadataProvider())->getEntityDescriptor(mc);
320     if (!mdresult.first) {
321         m_log.warn("unable to locate metadata for provider (%s)", entityID);
322         return false;
323     }
324     else if (!(AA=dynamic_cast<const AttributeAuthorityDescriptor*>(mdresult.second))) {
325         m_log.warn("no SAML 2 AttributeAuthority role found in metadata for (%s)", entityID);
326         return false;
327     }
328
329     const PropertySet* relyingParty = application.getRelyingParty(mdresult.first);
330     pair<bool,bool> signedAssertions = relyingParty->getBool("requireSignedAssertions");
331     pair<bool,const char*> encryption = relyingParty->getString("encryption");
332
333     // Locate policy key.
334     const char* policyId = m_policyId.empty() ? application.getString("policyId").second : m_policyId.c_str();
335
336     // Set up policy and SOAP client.
337     auto_ptr<SecurityPolicy> policy(
338         application.getServiceProvider().getSecurityPolicyProvider()->createSecurityPolicy(application, nullptr, policyId)
339         );
340     if (m_metadata)
341         policy->setMetadataProvider(m_metadata);
342     if (m_trust)
343         policy->setTrustEngine(m_trust);
344     policy->getAudiences().push_back(relyingParty->getXMLString("entityID").second);
345
346     MetadataCredentialCriteria mcc(*AA);
347     shibsp::SOAPClient soaper(*policy.get());
348
349     auto_ptr_XMLCh binding(samlconstants::SAML20_BINDING_SOAP);
350     saml2p::StatusResponseType* srt=nullptr;
351     const vector<AttributeService*>& endpoints=AA->getAttributeServices();
352     for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !srt && ep!=endpoints.end(); ++ep) {
353         if (!XMLString::equals((*ep)->getBinding(),binding.get())  || !(*ep)->getLocation())
354             continue;
355         auto_ptr_char loc((*ep)->getLocation());
356         try {
357             auto_ptr<saml2::Subject> subject(saml2::SubjectBuilder::buildSubject());
358
359             // Encrypt the NameID?
360             if (encryption.first && (!strcmp(encryption.second, "true") || !strcmp(encryption.second, "back"))) {
361                 auto_ptr<EncryptedID> encrypted(EncryptedIDBuilder::buildEncryptedID());
362                 encrypted->encrypt(
363                     *name,
364                     *(policy->getMetadataProvider()),
365                     mcc,
366                     false,
367                     relyingParty->getXMLString("encryptionAlg").second
368                     );
369                 subject->setEncryptedID(encrypted.release());
370             }
371             else {
372                 subject->setNameID(name->cloneNameID());
373             }
374
375             saml2p::AttributeQuery* query = saml2p::AttributeQueryBuilder::buildAttributeQuery();
376             query->setSubject(subject.release());
377             Issuer* iss = IssuerBuilder::buildIssuer();
378             iss->setName(relyingParty->getXMLString("entityID").second);
379             query->setIssuer(iss);
380             for (vector<saml2::Attribute*>::const_iterator ad = m_designators.begin(); ad!=m_designators.end(); ++ad)
381                 query->getAttributes().push_back((*ad)->cloneAttribute());
382
383             SAML2SOAPClient client(soaper, false);
384             client.sendSAML(query, application.getId(), mcc, loc.get());
385             srt = client.receiveSAML();
386         }
387         catch (exception& ex) {
388             m_log.error("exception during SAML query to %s: %s", loc.get(), ex.what());
389             soaper.reset();
390         }
391     }
392
393     if (!srt) {
394         m_log.error("unable to obtain a SAML response from attribute authority (%s)", entityID);
395         return false;
396     }
397
398     auto_ptr<saml2p::StatusResponseType> wrapper(srt);
399
400     saml2p::Response* response = dynamic_cast<saml2p::Response*>(srt);
401     if (!response) {
402         m_log.error("message was not a samlp:Response");
403         return true;
404     }
405     else if (!response->getStatus() || !response->getStatus()->getStatusCode() ||
406             !XMLString::equals(response->getStatus()->getStatusCode()->getValue(), saml2p::StatusCode::SUCCESS)) {
407         m_log.error("attribute authority (%s) returned a SAML error", entityID);
408         return true;
409     }
410
411     saml2::Assertion* newtoken = nullptr;
412     const vector<saml2::Assertion*>& assertions = const_cast<const saml2p::Response*>(response)->getAssertions();
413     if (assertions.empty()) {
414         // Check for encryption.
415         const vector<saml2::EncryptedAssertion*>& encassertions =
416             const_cast<const saml2p::Response*>(response)->getEncryptedAssertions();
417         if (encassertions.empty()) {
418             m_log.warn("response from attribute authority was empty");
419             return true;
420         }
421         else if (encassertions.size() > 1) {
422             m_log.warn("simple resolver only supports one assertion in the query response");
423         }
424
425         CredentialResolver* cr=application.getCredentialResolver();
426         if (!cr) {
427             m_log.warn("found encrypted assertion, but no CredentialResolver was available");
428             return true;
429         }
430
431         // Attempt to decrypt it.
432         try {
433             Locker credlocker(cr);
434             auto_ptr<XMLObject> tokenwrapper(encassertions.front()->decrypt(*cr, relyingParty->getXMLString("entityID").second, &mcc));
435             newtoken = dynamic_cast<saml2::Assertion*>(tokenwrapper.get());
436             if (newtoken) {
437                 tokenwrapper.release();
438                 if (m_log.isDebugEnabled())
439                     m_log.debugStream() << "decrypted Assertion: " << *newtoken << logging::eol;
440             }
441         }
442         catch (exception& ex) {
443             m_log.error(ex.what());
444         }
445         if (newtoken) {
446             // Free the Response now, so we know this is a stand-alone token later.
447             delete wrapper.release();
448         }
449         else {
450             // Nothing decrypted, should already be logged.
451             return true;
452         }
453     }
454     else {
455         if (assertions.size() > 1)
456             m_log.warn("simple resolver only supports one assertion in the query response");
457         newtoken = assertions.front();
458     }
459
460     if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
461         m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
462         if (!wrapper.get())
463             delete newtoken;
464         return true;
465     }
466
467     try {
468         // We're going to insist that the assertion issuer is the same as the peer.
469         // Reset the policy's message bits and extract them from the assertion.
470         policy->reset(true);
471         policy->setMessageID(newtoken->getID());
472         policy->setIssueInstant(newtoken->getIssueInstantEpoch());
473         policy->setIssuer(newtoken->getIssuer());
474         policy->evaluate(*newtoken);
475
476         // Now we can check the security status of the policy.
477         if (!policy->isAuthenticated())
478             throw SecurityPolicyException("Security of SAML 2.0 query result not established.");
479
480         if (m_subjectMatch) {
481             // Check for subject match.
482             bool ownedName = false;
483             NameID* respName = newtoken->getSubject() ? newtoken->getSubject()->getNameID() : nullptr;
484             if (!respName) {
485                 // Check for encryption.
486                 EncryptedID* encname = newtoken->getSubject() ? newtoken->getSubject()->getEncryptedID() : nullptr;
487                 if (encname) {
488                     CredentialResolver* cr=application.getCredentialResolver();
489                     if (!cr)
490                         m_log.warn("found EncryptedID, but no CredentialResolver was available");
491                     else {
492                         Locker credlocker(cr);
493                         auto_ptr<XMLObject> decryptedID(encname->decrypt(*cr, relyingParty->getXMLString("entityID").second, &mcc));
494                         respName = dynamic_cast<NameID*>(decryptedID.get());
495                         if (respName) {
496                             ownedName = true;
497                             decryptedID.release();
498                             if (m_log.isDebugEnabled())
499                                 m_log.debugStream() << "decrypted NameID: " << *respName << logging::eol;
500                         }
501                     }
502                 }
503             }
504
505             auto_ptr<NameID> nameIDwrapper(ownedName ? respName : nullptr);
506
507             if (!respName || !XMLString::equals(respName->getName(), name->getName()) ||
508                 !XMLString::equals(respName->getFormat(), name->getFormat()) ||
509                 !XMLString::equals(respName->getNameQualifier(), name->getNameQualifier()) ||
510                 !XMLString::equals(respName->getSPNameQualifier(), name->getSPNameQualifier())) {
511                 if (respName)
512                     m_log.warnStream() << "ignoring Assertion without strongly matching NameID in Subject: " <<
513                         *respName << logging::eol;
514                 else
515                     m_log.warn("ignoring Assertion without NameID in Subject");
516                 if (!wrapper.get())
517                     delete newtoken;
518                 return true;
519             }
520         }
521     }
522     catch (exception& ex) {
523         m_log.error("assertion failed policy validation: %s", ex.what());
524         if (!wrapper.get())
525             delete newtoken;
526         return true;
527     }
528
529     if (wrapper.get()) {
530         newtoken->detach();
531         wrapper.release();  // detach blows away the Response
532     }
533     ctx.getResolvedAssertions().push_back(newtoken);
534
535     // Finally, extract and filter the result.
536     try {
537         AttributeExtractor* extractor = application.getAttributeExtractor();
538         if (extractor) {
539             Locker extlocker(extractor);
540             extractor->extractAttributes(application, AA, *newtoken, ctx.getResolvedAttributes());
541         }
542
543         AttributeFilter* filter = application.getAttributeFilter();
544         if (filter) {
545             BasicFilteringContext fc(application, ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
546             Locker filtlocker(filter);
547             filter->filterAttributes(fc, ctx.getResolvedAttributes());
548         }
549     }
550     catch (exception& ex) {
551         m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
552         for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), xmltooling::cleanup<shibsp::Attribute>());
553         ctx.getResolvedAttributes().clear();
554     }
555
556     return true;
557 }
558
559 void SimpleAggregationResolver::resolveAttributes(ResolutionContext& ctx) const
560 {
561 #ifdef _DEBUG
562     xmltooling::NDC ndc("resolveAttributes");
563 #endif
564
565     SimpleAggregationContext& qctx = dynamic_cast<SimpleAggregationContext&>(ctx);
566
567     // First we manufacture the appropriate NameID to use.
568     NameID* n=nullptr;
569     for (vector<string>::const_iterator a = m_attributeIds.begin(); !n && a != m_attributeIds.end(); ++a) {
570         const Attribute* attr=nullptr;
571         if (qctx.getSession()) {
572             // Input attributes should be available via multimap.
573             pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> range =
574                 qctx.getSession()->getIndexedAttributes().equal_range(*a);
575             for (; !attr && range.first != range.second; ++range.first) {
576                 if (range.first->second->valueCount() > 0)
577                     attr = range.first->second;
578             }
579         }
580         else if (qctx.getInputAttributes()) {
581             // Have to loop over unindexed set.
582             const vector<Attribute*>* matches = qctx.getInputAttributes();
583             for (vector<Attribute*>::const_iterator match = matches->begin(); !attr && match != matches->end(); ++match) {
584                 if (*a == (*match)->getId() && (*match)->valueCount() > 0)
585                     attr = *match;
586             }
587         }
588
589         if (attr) {
590             m_log.debug("using input attribute (%s) as identifier for queries", attr->getId());
591             n = NameIDBuilder::buildNameID();
592             const NameIDAttribute* down = dynamic_cast<const NameIDAttribute*>(attr);
593             if (down) {
594                 // We can create a NameID directly from the source material.
595                 const NameIDAttribute::Value& v = down->getValues().front();
596                 XMLCh* val = fromUTF8(v.m_Name.c_str());
597                 n->setName(val);
598                 delete[] val;
599                 if (!v.m_Format.empty()) {
600                     val = fromUTF8(v.m_Format.c_str());
601                     n->setFormat(val);
602                     delete[] val;
603                 }
604                 if (!v.m_NameQualifier.empty()) {
605                     val = fromUTF8(v.m_NameQualifier.c_str());
606                     n->setNameQualifier(val);
607                     delete[] val;
608                 }
609                 if (!v.m_SPNameQualifier.empty()) {
610                     val = fromUTF8(v.m_SPNameQualifier.c_str());
611                     n->setSPNameQualifier(val);
612                     delete[] val;
613                 }
614                 if (!v.m_SPProvidedID.empty()) {
615                     val = fromUTF8(v.m_SPProvidedID.c_str());
616                     n->setSPProvidedID(val);
617                     delete[] val;
618                 }
619             }
620             else {
621                 // We have to mock up the NameID.
622                 XMLCh* val = fromUTF8(attr->getSerializedValues().front().c_str());
623                 n->setName(val);
624                 delete[] val;
625                 if (!m_format.empty())
626                     n->setFormat(m_format.c_str());
627             }
628         }
629     }
630
631     if (!n) {
632         if (qctx.getNameID() && m_attributeIds.empty()) {
633             m_log.debug("using authenticated NameID as identifier for queries");
634         }
635         else {
636             m_log.warn("unable to resolve attributes, no suitable query identifier found");
637             return;
638         }
639     }
640
641     auto_ptr<NameID> wrapper(n);
642
643     set<string> history;
644
645     // Put initial IdP into history to prevent extra query.
646     if (qctx.getEntityID())
647         history.insert(qctx.getEntityID());
648
649     // We have a master loop over all the possible sources of material.
650     for (vector< pair<string,bool> >::const_iterator source = m_sources.begin(); source != m_sources.end(); ++source) {
651         if (source->second) {
652             // A literal entityID to query.
653             if (history.count(source->first) == 0) {
654                 m_log.debug("issuing SAML query to (%s)", source->first.c_str());
655                 doQuery(qctx, source->first.c_str(), n ? n : qctx.getNameID());
656                 history.insert(source->first);
657             }
658             else {
659                 m_log.debug("skipping previously queried attribute source (%s)", source->first.c_str());
660             }
661         }
662         else {
663             m_log.debug("using attribute sources referenced in attribute (%s)", source->first.c_str());
664             if (qctx.getSession()) {
665                 // Input attributes should be available via multimap.
666                 pair<multimap<string,const Attribute*>::const_iterator, multimap<string,const Attribute*>::const_iterator> range =
667                     qctx.getSession()->getIndexedAttributes().equal_range(source->first);
668                 for (; range.first != range.second; ++range.first) {
669                     const vector<string>& links = range.first->second->getSerializedValues();
670                     for (vector<string>::const_iterator link = links.begin(); link != links.end(); ++link) {
671                         if (history.count(*link) == 0) {
672                             m_log.debug("issuing SAML query to (%s)", link->c_str());
673                             doQuery(qctx, link->c_str(), n ? n : qctx.getNameID());
674                             history.insert(*link);
675                         }
676                         else {
677                             m_log.debug("skipping previously queried attribute source (%s)", link->c_str());
678                         }
679                     }
680                 }
681             }
682             else if (qctx.getInputAttributes()) {
683                 // Have to loop over unindexed set.
684                 const vector<Attribute*>* matches = qctx.getInputAttributes();
685                 for (vector<Attribute*>::const_iterator match = matches->begin(); match != matches->end(); ++match) {
686                     if (source->first == (*match)->getId()) {
687                         const vector<string>& links = (*match)->getSerializedValues();
688                         for (vector<string>::const_iterator link = links.begin(); link != links.end(); ++link) {
689                             if (history.count(*link) == 0) {
690                                 m_log.debug("issuing SAML query to (%s)", link->c_str());
691                                 doQuery(qctx, link->c_str(), n ? n : qctx.getNameID());
692                                 history.insert(*link);
693                             }
694                             else {
695                                 m_log.debug("skipping previously queried attribute source (%s)", link->c_str());
696                             }
697                         }
698                     }
699                 }
700             }
701         }
702     }
703 }