Redesigned target around URL->application mapping
[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 #include <log4cpp/Category.hh>
60
61 using namespace shibboleth;
62 using namespace saml;
63 using namespace std;
64
65 OriginMetadata::OriginMetadata(const Iterator<IMetadata*>& metadatas, const XMLCh* site) : m_mapper(NULL), m_site(NULL)
66 {
67     metadatas.reset();
68     while (metadatas.hasNext())
69     {
70         IMetadata* i=metadatas.next();
71         i->lock();
72         if (m_site=dynamic_cast<const IOriginSite*>(i->lookup(site)))
73         {
74             m_mapper=i;
75             break;
76         }
77         i->unlock();
78     }
79 }
80
81 OriginMetadata::~OriginMetadata()
82 {
83     if (m_mapper)
84         m_mapper->unlock();
85 }
86
87 Iterator<XSECCryptoX509*> Trust::getCertificates(const XMLCh* subject)
88 {
89     if (m_mapper)
90     {
91         m_mapper->unlock();
92         m_mapper=NULL;
93     }
94     
95     m_trusts.reset();
96     while (m_trusts.hasNext())
97     {
98         ITrust* i=m_trusts.next();
99         i->lock();
100         Iterator<XSECCryptoX509*> iter=i->getCertificates(subject);
101         if (iter.size())
102         {
103             m_mapper=i;
104             return iter;
105         }
106         i->unlock();
107     }
108     return EMPTY(XSECCryptoX509*);
109 }
110
111 bool Trust::validate(const ISite* site, Iterator<XSECCryptoX509*> certs) const
112 {
113     bool ret=false;
114     m_trusts.reset();
115     while (!ret && m_trusts.hasNext())
116     {
117         ITrust* i=m_trusts.next();
118         i->lock();
119         ret=i->validate(site,certs);
120         i->unlock();
121     }
122     return ret;
123 }
124
125 bool Trust::validate(const ISite* site, Iterator<const XMLCh*> certs) const
126 {
127     bool ret=false;
128     m_trusts.reset();
129     while (!ret && m_trusts.hasNext())
130     {
131         ITrust* i=m_trusts.next();
132         i->lock();
133         ret=i->validate(site,certs);
134         i->unlock();
135     }
136     return ret;
137 }
138
139 bool Trust::attach(const ISite* site, SSL_CTX* ctx) const
140 {
141     bool ret=false;
142     m_trusts.reset();
143     while (!ret && m_trusts.hasNext())
144     {
145         ITrust* i=m_trusts.next();
146         i->lock();
147         ret=i->attach(site,ctx);
148         i->unlock();
149     }
150     return ret;
151 }
152
153 Trust::~Trust()
154 {
155     if (m_mapper)
156         m_mapper->unlock();
157 }
158
159 bool Credentials::attach(const saml::Iterator<ICredentials*>& creds, const XMLCh* subject, const ISite* relyingParty, SSL_CTX* ctx)
160 {
161     bool ret=false;
162     creds.reset();
163     while (!ret && creds.hasNext())
164     {
165         ICredentials* i=creds.next();
166         i->lock();
167         ret=i->attach(subject,relyingParty,ctx);
168         i->unlock();
169         
170     }
171     return ret;
172 }
173
174 AAP::AAP(const saml::Iterator<IAAP*>& aaps, const XMLCh* attrName, const XMLCh* attrNamespace) : m_mapper(NULL), m_rule(NULL)
175 {
176     aaps.reset();
177     while (aaps.hasNext())
178     {
179         IAAP* i=aaps.next();
180         i->lock();
181         if (m_rule=i->lookup(attrName,attrNamespace))
182         {
183             m_mapper=i;
184             break;
185         }
186         i->unlock();
187     }
188 }
189
190 AAP::AAP(const saml::Iterator<IAAP*>& aaps, const char* alias) : m_mapper(NULL), m_rule(NULL)
191 {
192     aaps.reset();
193     while (aaps.hasNext())
194     {
195         IAAP* i=aaps.next();
196         i->lock();
197         if (m_rule=i->lookup(alias))
198         {
199             m_mapper=i;
200             break;
201         }
202         i->unlock();
203     }
204 }
205
206 AAP::~AAP()
207 {
208     if (m_mapper)
209         m_mapper->unlock();
210 }
211
212 void AAP::apply(const saml::Iterator<IAAP*>& aaps, const IOriginSite* originSite, saml::SAMLAssertion& assertion)
213 {
214     saml::NDC("apply");
215     log4cpp::Category& log=log4cpp::Category::getInstance(SHIB_LOGCAT".AAP");
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 }