Tweak some logging.
[shibboleth/cpp-sp.git] / shibsp / attribute / resolver / impl / QueryAttributeResolver.cpp
1 /*
2  *  Copyright 2001-2007 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  * QueryAttributeResolver.cpp
19  * 
20  * AttributeResolver based on SAML queries.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "ServiceProvider.h"
26 #include "SessionCache.h"
27 #include "attribute/Attribute.h"
28 #include "attribute/filtering/AttributeFilter.h"
29 #include "attribute/filtering/BasicFilteringContext.h"
30 #include "attribute/resolver/AttributeExtractor.h"
31 #include "attribute/resolver/AttributeResolver.h"
32 #include "attribute/resolver/ResolutionContext.h"
33 #include "binding/SOAPClient.h"
34 #include "util/SPConstants.h"
35
36 #include <log4cpp/Category.hh>
37 #include <saml/exceptions.h>
38 #include <saml/binding/SecurityPolicy.h>
39 #include <saml/saml1/binding/SAML1SOAPClient.h>
40 #include <saml/saml1/core/Assertions.h>
41 #include <saml/saml1/core/Protocols.h>
42 #include <saml/saml1/profile/AssertionValidator.h>
43 #include <saml/saml2/binding/SAML2SOAPClient.h>
44 #include <saml/saml2/core/Protocols.h>
45 #include <saml/saml2/metadata/Metadata.h>
46 #include <saml/saml2/metadata/MetadataProvider.h>
47 #include <saml/saml2/profile/AssertionValidator.h>
48 #include <xmltooling/util/NDC.h>
49 #include <xmltooling/util/XMLHelper.h>
50 #include <xercesc/util/XMLUniDefs.hpp>
51
52 using namespace shibsp;
53 using namespace opensaml::saml1;
54 using namespace opensaml::saml1p;
55 using namespace opensaml::saml2;
56 using namespace opensaml::saml2p;
57 using namespace opensaml::saml2md;
58 using namespace opensaml;
59 using namespace xmltooling;
60 using namespace log4cpp;
61 using namespace std;
62
63 namespace shibsp {
64
65     class SHIBSP_DLLLOCAL QueryContext : public ResolutionContext
66     {
67     public:
68         QueryContext(const Application& application, const Session& session)
69             : m_query(true), m_app(application), m_session(&session), m_metadata(NULL), m_entity(NULL), m_nameid(NULL) {
70         }
71         
72         QueryContext(
73             const Application& application,
74             const EntityDescriptor* issuer,
75             const NameID* nameid,
76             const char* authncontext_class=NULL,
77             const char* authncontext_decl=NULL,
78             const vector<const opensaml::Assertion*>* tokens=NULL,
79             const multimap<string,Attribute*>* attributes=NULL
80             ) : m_query(true), m_app(application), m_session(NULL), m_metadata(NULL), m_entity(issuer),
81                 m_nameid(nameid), m_class(authncontext_class), m_decl(authncontext_decl) {
82
83             if (tokens) {
84                 for (vector<const opensaml::Assertion*>::const_iterator t = tokens->begin(); t!=tokens->end(); ++t) {
85                     const saml2::Assertion* token2 = dynamic_cast<const saml2::Assertion*>(*t);
86                     if (token2 && !token2->getAttributeStatements().empty()) {
87                         m_query = false;
88                     }
89                     else {
90                         const saml1::Assertion* token1 = dynamic_cast<const saml1::Assertion*>(*t);
91                         if (token1 && !token1->getAttributeStatements().empty()) {
92                             m_query = false;
93                         }
94                     }
95                 }
96             }
97         }
98         
99         ~QueryContext() {
100             if (m_metadata)
101                 m_metadata->unlock();
102             for_each(m_attributes.begin(), m_attributes.end(), cleanup_pair<string,shibsp::Attribute>());
103             for_each(m_assertions.begin(), m_assertions.end(), xmltooling::cleanup<opensaml::Assertion>());
104         }
105     
106         bool doQuery() const {
107             return m_query;
108         }
109
110         const Application& getApplication() const {
111             return m_app;
112         }
113         const EntityDescriptor* getEntityDescriptor() const {
114             if (m_entity)
115                 return m_entity;
116             if (m_session && m_session->getEntityID()) {
117                 m_metadata = m_app.getMetadataProvider();
118                 if (m_metadata) {
119                     m_metadata->lock();
120                     return m_entity = m_metadata->getEntityDescriptor(m_session->getEntityID());
121                 }
122             }
123             return NULL;
124         }
125         const NameID* getNameID() const {
126             return m_session ? m_session->getNameID() : m_nameid;
127         }
128         const char* getClassRef() const {
129             return m_session ? m_session->getAuthnContextClassRef() :  m_class;
130         }
131         const char* getDeclRef() const {
132             return m_session ? m_session->getAuthnContextDeclRef() : m_decl;
133         }
134         const Session* getSession() const {
135             return m_session;
136         }
137         multimap<string,shibsp::Attribute*>& getResolvedAttributes() {
138             return m_attributes;
139         }
140         vector<opensaml::Assertion*>& getResolvedAssertions() {
141             return m_assertions;
142         }
143
144     private:
145         bool m_query;
146         const Application& m_app;
147         const Session* m_session;
148         mutable MetadataProvider* m_metadata;
149         mutable const EntityDescriptor* m_entity;
150         const NameID* m_nameid;
151         const char* m_class;
152         const char* m_decl;
153         multimap<string,shibsp::Attribute*> m_attributes;
154         vector<opensaml::Assertion*> m_assertions;
155     };
156     
157     class SHIBSP_DLLLOCAL QueryResolver : public AttributeResolver
158     {
159     public:
160         QueryResolver(const DOMElement* e);
161         ~QueryResolver() {
162             for_each(m_SAML1Designators.begin(), m_SAML1Designators.end(), xmltooling::cleanup<AttributeDesignator>());
163             for_each(m_SAML2Designators.begin(), m_SAML2Designators.end(), xmltooling::cleanup<saml2::Attribute>());
164         }
165
166         Lockable* lock() {return this;}
167         void unlock() {}
168         
169         ResolutionContext* createResolutionContext(
170             const Application& application,
171             const EntityDescriptor* issuer,
172             const NameID* nameid,
173             const char* authncontext_class=NULL,
174             const char* authncontext_decl=NULL,
175             const vector<const opensaml::Assertion*>* tokens=NULL,
176             const multimap<string,shibsp::Attribute*>* attributes=NULL
177             ) const {
178             return new QueryContext(application,issuer,nameid,authncontext_class,authncontext_decl,tokens,attributes);
179         }
180
181         ResolutionContext* createResolutionContext(const Application& application, const Session& session) const {
182             return new QueryContext(application,session);
183         }
184
185         void resolveAttributes(ResolutionContext& ctx) const;
186
187         void getAttributeIds(vector<string>& attributes) const {
188             // Nothing to do, only the extractor would actually generate them.
189         }
190
191     private:
192         bool SAML1Query(QueryContext& ctx) const;
193         bool SAML2Query(QueryContext& ctx) const;
194
195         Category& m_log;
196         vector<AttributeDesignator*> m_SAML1Designators;
197         vector<saml2::Attribute*> m_SAML2Designators;
198     };
199
200     AttributeResolver* SHIBSP_DLLLOCAL QueryResolverFactory(const DOMElement* const & e)
201     {
202         return new QueryResolver(e);
203     }
204     
205 };
206
207 void SHIBSP_API shibsp::registerAttributeResolvers()
208 {
209     SPConfig::getConfig().AttributeResolverManager.registerFactory(QUERY_ATTRIBUTE_RESOLVER, QueryResolverFactory);
210 }
211
212 QueryResolver::QueryResolver(const DOMElement* e) : m_log(Category::getInstance(SHIBSP_LOGCAT".AttributeResolver"))
213 {
214 #ifdef _DEBUG
215     xmltooling::NDC ndc("QueryResolver");
216 #endif
217     
218     DOMElement* child = XMLHelper::getFirstChildElement(e);
219     while (child) {
220         try {
221             if (XMLHelper::isNodeNamed(e, samlconstants::SAML20_NS, saml2::Attribute::LOCAL_NAME)) {
222                 auto_ptr<XMLObject> obj(saml2::AttributeBuilder::buildOneFromElement(child));
223                 saml2::Attribute* down = dynamic_cast<saml2::Attribute*>(obj.get());
224                 if (down) {
225                     m_SAML2Designators.push_back(down);
226                     obj.release();
227                 }
228             }
229             else if (XMLHelper::isNodeNamed(e, samlconstants::SAML1P_NS, AttributeDesignator::LOCAL_NAME)) {
230                 auto_ptr<XMLObject> obj(AttributeDesignatorBuilder::buildOneFromElement(child));
231                 AttributeDesignator* down = dynamic_cast<AttributeDesignator*>(obj.get());
232                 if (down) {
233                     m_SAML1Designators.push_back(down);
234                     obj.release();
235                 }
236             }
237         }
238         catch (exception& ex) {
239             m_log.error("exception loading attribute designator: %s", ex.what());
240         }
241         child = XMLHelper::getNextSiblingElement(child);
242     }
243 }
244
245 bool QueryResolver::SAML1Query(QueryContext& ctx) const
246 {
247 #ifdef _DEBUG
248     xmltooling::NDC ndc("query");
249 #endif
250
251     int version = 1;
252     const AttributeAuthorityDescriptor* AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML11_PROTOCOL_ENUM);
253     if (!AA) {
254         AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML10_PROTOCOL_ENUM);
255         version = 0;
256     }
257     if (!AA) {
258         m_log.debug("no SAML 1.x AttributeAuthority role found in metadata");
259         return false;
260     }
261
262     shibsp::SecurityPolicy policy(ctx.getApplication());
263     MetadataCredentialCriteria mcc(*AA);
264     shibsp::SOAPClient soaper(policy);
265     const PropertySet* policySettings =
266         ctx.getApplication().getServiceProvider().getPolicySettings(ctx.getApplication().getString("policyId").second);
267     pair<bool,bool> signedAssertions = policySettings->getBool("signedAssertions");
268
269     auto_ptr_XMLCh binding(samlconstants::SAML1_BINDING_SOAP);
270     saml1p::Response* response=NULL;
271     const vector<AttributeService*>& endpoints=AA->getAttributeServices();
272     for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !response && ep!=endpoints.end(); ++ep) {
273         try {
274             if (!XMLString::equals((*ep)->getBinding(),binding.get()))
275                 continue;
276             auto_ptr_char loc((*ep)->getLocation());
277             auto_ptr_XMLCh issuer(ctx.getApplication().getString("entityID").second);
278             NameIdentifier* nameid = NameIdentifierBuilder::buildNameIdentifier();
279             nameid->setName(ctx.getNameID()->getName());
280             nameid->setFormat(ctx.getNameID()->getFormat());
281             nameid->setNameQualifier(ctx.getNameID()->getNameQualifier());
282             saml1::Subject* subject = saml1::SubjectBuilder::buildSubject();
283             subject->setNameIdentifier(nameid);
284             saml1p::AttributeQuery* query = saml1p::AttributeQueryBuilder::buildAttributeQuery();
285             query->setSubject(subject);
286             query->setResource(issuer.get());
287             for (vector<AttributeDesignator*>::const_iterator ad = m_SAML1Designators.begin(); ad!=m_SAML1Designators.end(); ++ad)
288                 query->getAttributeDesignators().push_back((*ad)->cloneAttributeDesignator());
289             Request* request = RequestBuilder::buildRequest();
290             request->setAttributeQuery(query);
291             request->setMinorVersion(version);
292
293             SAML1SOAPClient client(soaper);
294             client.sendSAML(request, mcc, loc.get());
295             response = client.receiveSAML();
296         }
297         catch (exception& ex) {
298             m_log.error("exception making SAML query: %s", ex.what());
299             soaper.reset();
300         }
301     }
302
303     if (!response) {
304         m_log.error("unable to successfully query for attributes");
305         return false;
306     }
307
308     const vector<saml1::Assertion*>& assertions = const_cast<const saml1p::Response*>(response)->getAssertions();
309     if (assertions.size()>1)
310         m_log.warn("simple resolver only supports one assertion in the query response");
311
312     auto_ptr<saml1p::Response> wrapper(response);
313     saml1::Assertion* newtoken = assertions.front();
314
315     if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
316         m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
317         return true;
318     }
319
320     try {
321         policy.evaluate(*newtoken);
322         if (!policy.isSecure())
323             throw SecurityPolicyException("Security of SAML 1.x query result not established.");
324         saml1::AssertionValidator tokval(ctx.getApplication().getAudiences(), time(NULL));
325         tokval.validateAssertion(*newtoken);
326     }
327     catch (exception& ex) {
328         m_log.error("assertion failed policy/validation: %s", ex.what());
329         return true;
330     }
331
332     newtoken->detach();
333     wrapper.release();
334     ctx.getResolvedAssertions().push_back(newtoken);
335
336     // Finally, extract and filter the result.
337     try {
338         AttributeExtractor* extractor = ctx.getApplication().getAttributeExtractor();
339         if (extractor) {
340             Locker extlocker(extractor);
341             extractor->extractAttributes(ctx.getApplication(), AA, *newtoken, ctx.getResolvedAttributes());
342         }
343
344         AttributeFilter* filter = ctx.getApplication().getAttributeFilter();
345         if (filter) {
346             BasicFilteringContext fc(ctx.getApplication(), ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
347             Locker filtlocker(filter);
348             filter->filterAttributes(fc, ctx.getResolvedAttributes());
349         }
350     }
351     catch (exception& ex) {
352         m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
353         for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), cleanup_pair<string,shibsp::Attribute>());
354         ctx.getResolvedAttributes().clear();
355     }
356
357     return true;
358 }
359
360 bool QueryResolver::SAML2Query(QueryContext& ctx) const
361 {
362 #ifdef _DEBUG
363     xmltooling::NDC ndc("query");
364 #endif
365
366     const AttributeAuthorityDescriptor* AA = ctx.getEntityDescriptor()->getAttributeAuthorityDescriptor(samlconstants::SAML20P_NS);
367     if (!AA) {
368         m_log.debug("no SAML 2 AttributeAuthority role found in metadata");
369         return false;
370     }
371
372     shibsp::SecurityPolicy policy(ctx.getApplication());
373     MetadataCredentialCriteria mcc(*AA);
374     shibsp::SOAPClient soaper(policy);
375     const PropertySet* policySettings =
376         ctx.getApplication().getServiceProvider().getPolicySettings(ctx.getApplication().getString("policyId").second);
377     pair<bool,bool> signedAssertions = policySettings->getBool("signedAssertions");
378
379     auto_ptr_XMLCh binding(samlconstants::SAML20_BINDING_SOAP);
380     saml2p::StatusResponseType* srt=NULL;
381     const vector<AttributeService*>& endpoints=AA->getAttributeServices();
382     for (vector<AttributeService*>::const_iterator ep=endpoints.begin(); !srt && ep!=endpoints.end(); ++ep) {
383         try {
384             if (!XMLString::equals((*ep)->getBinding(),binding.get()))
385                 continue;
386             auto_ptr_char loc((*ep)->getLocation());
387             auto_ptr_XMLCh issuer(ctx.getApplication().getString("entityID").second);
388             saml2::Subject* subject = saml2::SubjectBuilder::buildSubject();
389             subject->setNameID(ctx.getNameID()->cloneNameID());
390             saml2p::AttributeQuery* query = saml2p::AttributeQueryBuilder::buildAttributeQuery();
391             query->setSubject(subject);
392             Issuer* iss = IssuerBuilder::buildIssuer();
393             iss->setName(issuer.get());
394             query->setIssuer(iss);
395             for (vector<saml2::Attribute*>::const_iterator ad = m_SAML2Designators.begin(); ad!=m_SAML2Designators.end(); ++ad)
396                 query->getAttributes().push_back((*ad)->cloneAttribute());
397
398             SAML2SOAPClient client(soaper);
399             client.sendSAML(query, mcc, loc.get());
400             srt = client.receiveSAML();
401         }
402         catch (exception& ex) {
403             m_log.error("exception making SAML query: %s", ex.what());
404             soaper.reset();
405         }
406     }
407
408     if (!srt) {
409         m_log.error("unable to successfully query for attributes");
410         return false;
411     }
412     saml2p::Response* response = dynamic_cast<saml2p::Response*>(srt);
413     if (!response) {
414         delete srt;
415         m_log.error("message was not a samlp:Response");
416         return true;
417     }
418
419     const vector<saml2::Assertion*>& assertions = const_cast<const saml2p::Response*>(response)->getAssertions();
420     if (assertions.size()>1)
421         m_log.warn("simple resolver only supports one assertion in the query response");
422
423     auto_ptr<saml2p::Response> wrapper(response);
424     saml2::Assertion* newtoken = assertions.front();
425
426     if (!newtoken->getSignature() && signedAssertions.first && signedAssertions.second) {
427         m_log.error("assertion unsigned, rejecting it based on signedAssertions policy");
428         return true;
429     }
430
431     try {
432         policy.evaluate(*newtoken);
433         if (!policy.isSecure())
434             throw SecurityPolicyException("Security of SAML 2.0 query result not established.");
435         saml2::AssertionValidator tokval(ctx.getApplication().getAudiences(), time(NULL));
436         tokval.validateAssertion(*newtoken);
437     }
438     catch (exception& ex) {
439         m_log.error("assertion failed policy/validation: %s", ex.what());
440         return true;
441     }
442
443     newtoken->detach();
444     wrapper.release();
445     ctx.getResolvedAssertions().push_back(newtoken);
446
447     // Finally, extract and filter the result.
448     try {
449         AttributeExtractor* extractor = ctx.getApplication().getAttributeExtractor();
450         if (extractor) {
451             Locker extlocker(extractor);
452             extractor->extractAttributes(ctx.getApplication(), AA, *newtoken, ctx.getResolvedAttributes());
453         }
454
455         AttributeFilter* filter = ctx.getApplication().getAttributeFilter();
456         if (filter) {
457             BasicFilteringContext fc(ctx.getApplication(), ctx.getResolvedAttributes(), AA, ctx.getClassRef(), ctx.getDeclRef());
458             Locker filtlocker(filter);
459             filter->filterAttributes(fc, ctx.getResolvedAttributes());
460         }
461     }
462     catch (exception& ex) {
463         m_log.error("caught exception extracting/filtering attributes from query result: %s", ex.what());
464         for_each(ctx.getResolvedAttributes().begin(), ctx.getResolvedAttributes().end(), cleanup_pair<string,shibsp::Attribute>());
465         ctx.getResolvedAttributes().clear();
466     }
467
468     return true;
469 }
470
471 void QueryResolver::resolveAttributes(ResolutionContext& ctx) const
472 {
473 #ifdef _DEBUG
474     xmltooling::NDC ndc("resolveAttributes");
475 #endif
476
477     QueryContext& qctx = dynamic_cast<QueryContext&>(ctx);
478     if (!qctx.doQuery()) {
479         m_log.debug("found AttributeStatement in input to new session, skipping query");
480         return;
481     }
482
483     if (qctx.getNameID() && qctx.getEntityDescriptor()) {
484         m_log.debug("attempting SAML 2.0 attribute query");
485         if (SAML2Query(qctx))
486             return;
487         m_log.debug("attempting SAML 1.x attribute query");
488         SAML1Query(qctx);
489         return;
490     }
491     m_log.warn("can't attempt attribute query, either no NameID or no metadata to use");
492 }