Add AnyAttribute support for global attrib acceptance with export rules.
[shibboleth/cpp-sp.git] / shib / Metadata.cpp
1 /* 
2  * The Shibboleth License, Version 1. 
3  * Copyright (c) 2002 
4  * University Corporation for Advanced Internet Development, Inc. 
5  * All rights reserved
6  * 
7  * 
8  * Redistribution and use in source and binary forms, with or without 
9  * modification, are permitted provided that the following conditions are met:
10  * 
11  * Redistributions of source code must retain the above copyright notice, this 
12  * list of conditions and the following disclaimer.
13  * 
14  * Redistributions in binary form must reproduce the above copyright notice, 
15  * this list of conditions and the following disclaimer in the documentation 
16  * and/or other materials provided with the distribution, if any, must include 
17  * the following acknowledgment: "This product includes software developed by 
18  * the University Corporation for Advanced Internet Development 
19  * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement 
20  * may appear in the software itself, if and wherever such third-party 
21  * acknowledgments normally appear.
22  * 
23  * Neither the name of Shibboleth nor the names of its contributors, nor 
24  * Internet2, nor the University Corporation for Advanced Internet Development, 
25  * Inc., nor UCAID may be used to endorse or promote products derived from this 
26  * software without specific prior written permission. For written permission, 
27  * please contact shibboleth@shibboleth.org
28  * 
29  * Products derived from this software may not be called Shibboleth, Internet2, 
30  * UCAID, or the University Corporation for Advanced Internet Development, nor 
31  * may Shibboleth appear in their name, without prior written permission of the 
32  * University Corporation for Advanced Internet Development.
33  * 
34  * 
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
36  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
38  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK 
39  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. 
40  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY 
41  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, 
42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49
50 /* Metadata.h - glue classes that interface to metadata providers
51
52    Scott Cantor
53    9/27/02
54
55    $History:$
56 */
57
58 #include "internal.h"
59
60 using namespace shibboleth;
61 using namespace saml;
62 using namespace std;
63
64 const IProvider* Metadata::lookup(const XMLCh* providerId)
65 {
66     if (m_mapper) {
67         m_mapper->unlock();
68         m_mapper=NULL;
69     }
70     const IProvider* ret=NULL;
71     m_metadatas.reset();
72     while (m_metadatas.hasNext()) {
73         IMetadata* i=m_metadatas.next();
74         i->lock();
75         if (ret=i->lookup(providerId)) {
76             m_mapper=i;
77             return ret;
78         }
79         i->unlock();
80     }
81     return NULL;
82 }
83
84 Metadata::~Metadata()
85 {
86     if (m_mapper)
87         m_mapper->unlock();
88 }
89
90 Iterator<void*> Revocation::getRevocationLists(const IProvider* provider, const IProviderRole* role)
91 {
92     if (m_mapper) {
93         m_mapper->unlock();
94         m_mapper=NULL;
95     }
96     m_revocations.reset();
97     while (m_revocations.hasNext()) {
98         IRevocation* i=m_revocations.next();
99         i->lock();
100         Iterator<void*> ret=i->getRevocationLists(provider,role);
101         if (ret.size()) {
102             m_mapper=i;
103             return ret;
104         }
105         i->unlock();
106     }
107     return EMPTY(void*);
108 }
109
110 Revocation::~Revocation()
111 {
112     if (m_mapper)
113         m_mapper->unlock();
114 }
115
116 bool Trust::validate(
117     const Iterator<IRevocation*>& revocations,
118     const IProviderRole* role, const SAMLSignedObject& token,
119     const Iterator<IMetadata*>& metadatas) const
120 {
121     m_trusts.reset();
122     while (m_trusts.hasNext()) {
123         if (m_trusts.next()->validate(revocations,role,token,metadatas))
124             return true;
125     }
126     return false;
127 }
128
129 bool Trust::attach(const Iterator<IRevocation*>& revocations, const IProviderRole* role, void* ctx) const
130 {
131     m_trusts.reset();
132     while (m_trusts.hasNext()) {
133         if (m_trusts.next()->attach(revocations,role,ctx))
134             return true;
135     }
136     return false;
137 }
138
139 const ICredResolver* Credentials::lookup(const char* id)
140 {
141     if (m_mapper) {
142         m_mapper->unlock();
143         m_mapper=NULL;
144     }
145     const ICredResolver* ret=NULL;
146     m_creds.reset();
147     while (m_creds.hasNext()) {
148         ICredentials* i=m_creds.next();
149         i->lock();
150         if (ret=i->lookup(id)) {
151             m_mapper=i;
152             return ret;
153         }
154         i->unlock();
155     }
156     return NULL;
157 }
158
159 Credentials::~Credentials()
160 {
161     if (m_mapper)
162         m_mapper->unlock();
163 }
164
165 AAP::AAP(const saml::Iterator<IAAP*>& aaps, const XMLCh* attrName, const XMLCh* attrNamespace) : m_mapper(NULL), m_rule(NULL)
166 {
167     aaps.reset();
168     while (aaps.hasNext()) {
169         IAAP* i=aaps.next();
170         i->lock();
171         if (m_rule=i->lookup(attrName,attrNamespace)) {
172             m_mapper=i;
173             break;
174         }
175         i->unlock();
176     }
177 }
178
179 AAP::AAP(const saml::Iterator<IAAP*>& aaps, const char* alias) : m_mapper(NULL), m_rule(NULL)
180 {
181     aaps.reset();
182     while (aaps.hasNext()) {
183         IAAP* i=aaps.next();
184         i->lock();
185         if (m_rule=i->lookup(alias)) {
186             m_mapper=i;
187             break;
188         }
189         i->unlock();
190     }
191 }
192
193 AAP::~AAP()
194 {
195     if (m_mapper)
196         m_mapper->unlock();
197 }
198
199 void AAP::apply(const saml::Iterator<IAAP*>& aaps, const IProvider* originSite, saml::SAMLAssertion& assertion)
200 {
201     saml::NDC("apply");
202     log4cpp::Category& log=log4cpp::Category::getInstance(SHIB_LOGCAT".AAP");
203     
204     // First check for no providers or AnyAttribute.
205     if (aaps.size()==0) {
206         log.debug("no filters specified, accepting entire assertion");
207         return;
208     }
209     aaps.reset();
210     while (aaps.hasNext()) {
211         if (aaps.next()->anyAttribute()) {
212             log.debug("any attribute enabled, accepting entire assertion");
213             return;
214         }
215     }
216     
217     // Check each statement.
218     Iterator<SAMLStatement*> statements=assertion.getStatements();
219     for (unsigned int scount=0; scount < statements.size();) {
220         SAMLAttributeStatement* s=dynamic_cast<SAMLAttributeStatement*>(statements[scount]);
221         if (!s)
222             continue;
223         
224         // Check each attribute.
225         Iterator<SAMLAttribute*> attrs=s->getAttributes();
226         for (unsigned int acount=0; acount < attrs.size();) {
227             SAMLAttribute* a=attrs[acount];
228
229             AAP rule(aaps,a->getName(),a->getNamespace());
230             if (rule.fail()) {
231                 if (log.isWarnEnabled()) {
232                     auto_ptr_char temp(a->getName());
233                     log.warn("no rule found for attribute (%s), filtering it out",temp.get());
234                 }
235                 s->removeAttribute(acount);
236                 continue;
237             }
238             
239             try {
240                 rule->apply(originSite,*a);
241                 acount++;
242             }
243             catch (SAMLException&) {
244                 // The attribute is now defunct.
245                 log.info("no values remain, removing attribute");
246                 s->removeAttribute(acount);
247             }
248         }
249
250         try {
251             s->checkValidity();
252             scount++;
253         }
254         catch (SAMLException&) {
255             // The statement is now defunct.
256             log.info("no attributes remain, removing statement");
257             assertion.removeStatement(scount);
258         }
259     }
260     
261     // Now see if we trashed it irrevocably.
262     assertion.checkValidity();
263 }