Switch to shibtarget cleanup functors
[shibboleth/cpp-sp.git] / xmlproviders / XMLAAP.cpp
1 /*
2  *  Copyright 2001-2005 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 /* XMLAAP.cpp - XML AAP implementation
18
19    Scott Cantor
20    12/21/02
21
22    $History:$
23 */
24
25 #include "internal.h"
26 #include <algorithm>
27 #include <log4cpp/Category.hh>
28
29 using namespace saml;
30 using namespace shibboleth;
31 using namespace log4cpp;
32 using namespace std;
33
34 #include <xercesc/util/regx/RegularExpression.hpp>
35
36 namespace {
37
38     class XMLAAPImpl : public ReloadableXMLFileImpl
39     {
40     public:
41         XMLAAPImpl(const char* pathname) : ReloadableXMLFileImpl(pathname), anyAttribute(false) { init(); }
42         XMLAAPImpl(const DOMElement* e) : ReloadableXMLFileImpl(e), anyAttribute(false) { init(); }
43         void init();
44         ~XMLAAPImpl();
45         
46         class AttributeRule : public IAttributeRule
47         {
48         public:
49             AttributeRule(const DOMElement* e);
50             ~AttributeRule() {}
51             
52             const XMLCh* getName() const { return m_name; }
53             const XMLCh* getNamespace() const { return m_namespace; }
54             const char* getAlias() const { return m_alias.get(); }
55             const char* getHeader() const { return m_header.get(); }
56             bool getCaseSensitive() const { return m_caseSensitive; }
57             bool getScoped() const { return m_scoped; }
58             void apply(SAMLAttribute& attribute, const IEntityDescriptor* source=NULL) const;
59     
60             enum value_type { literal, regexp, xpath };
61         private:    
62             const XMLCh* m_name;
63             const XMLCh* m_namespace;
64             auto_ptr_char m_alias;
65             auto_ptr_char m_header;
66             bool m_caseSensitive;
67             bool m_scoped;
68
69             struct SiteRule
70             {
71                 SiteRule() : anyValue(false) {}
72                 bool anyValue;
73                 vector<pair<value_type,const XMLCh*> > valueDenials;
74                 vector<pair<value_type,const XMLCh*> > valueAccepts;
75                 vector<pair<value_type,const XMLCh*> > scopeDenials;
76                 vector<pair<value_type,const XMLCh*> > scopeAccepts;
77             };
78             
79             value_type toValueType(const DOMElement* e);
80             bool scopeCheck(
81                 const DOMElement* e,
82                 const IExtendedEntityDescriptor* source,
83                 const vector<const SiteRule*>& ruleStack
84                 ) const;
85             bool accept(const DOMElement* e, const IExtendedEntityDescriptor* source=NULL) const;
86             
87             SiteRule m_anySiteRule;
88     #ifdef HAVE_GOOD_STL
89             typedef map<xstring,SiteRule> sitemap_t;
90     #else
91             typedef map<string,SiteRule> sitemap_t;
92     #endif
93             sitemap_t m_siteMap;
94         };
95     
96         bool anyAttribute;
97         vector<const IAttributeRule*> m_attrs;
98         map<string,const IAttributeRule*> m_aliasMap;
99     #ifdef HAVE_GOOD_STL
100         typedef map<xstring,AttributeRule*> attrmap_t;
101     #else
102         typedef map<string,AttributeRule*> attrmap_t;
103     #endif
104         attrmap_t m_attrMap;
105     };
106
107     class XMLAAP : public IAAP, public ReloadableXMLFile
108     {
109     public:
110         XMLAAP(const DOMElement* e) : ReloadableXMLFile(e) {}
111         ~XMLAAP() {}
112         
113         bool anyAttribute() const {return static_cast<XMLAAPImpl*>(getImplementation())->anyAttribute;}
114         const IAttributeRule* lookup(const XMLCh* attrName, const XMLCh* attrNamespace=NULL) const;
115         const IAttributeRule* lookup(const char* alias) const;
116         Iterator<const IAttributeRule*> getAttributeRules() const;
117
118     protected:
119         virtual ReloadableXMLFileImpl* newImplementation(const char* pathname, bool first=true) const;
120         virtual ReloadableXMLFileImpl* newImplementation(const DOMElement* e, bool first=true) const;
121     };
122
123 }
124
125 IPlugIn* XMLAAPFactory(const DOMElement* e)
126 {
127     auto_ptr<XMLAAP> aap(new XMLAAP(e));
128     aap->getImplementation();
129     return aap.release();
130 }
131
132 ReloadableXMLFileImpl* XMLAAP::newImplementation(const DOMElement* e, bool first) const
133 {
134     return new XMLAAPImpl(e);
135 }
136
137 ReloadableXMLFileImpl* XMLAAP::newImplementation(const char* pathname, bool first) const
138 {
139     return new XMLAAPImpl(pathname);
140 }
141
142 void XMLAAPImpl::init()
143 {
144 #ifdef _DEBUG
145     saml::NDC ndc("init");
146 #endif
147     Category& log=Category::getInstance(XMLPROVIDERS_LOGCAT".AAP");
148
149     try
150     {
151         if (!saml::XML::isElementNamed(m_root,::XML::SHIB_NS,SHIB_L(AttributeAcceptancePolicy)))
152         {
153             log.error("Construction requires a valid AAP file: (shib:AttributeAcceptancePolicy as root element)");
154             throw MalformedException("Construction requires a valid AAP file: (shib:AttributeAcceptancePolicy as root element)");
155         }
156
157         // Check for AnyAttribute element.
158         DOMElement* anyAttr = saml::XML::getFirstChildElement(m_root,::XML::SHIB_NS,SHIB_L(AnyAttribute));
159         if (anyAttr) {
160             anyAttribute = true;
161             log.warn("<AnyAttribute> found, will short-circuit all attribute value and scope filtering");
162         }
163
164         // Loop over the AttributeRule elements.
165         DOMNodeList* nlist = m_root->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(AttributeRule));
166         for (unsigned int i=0; nlist && i<nlist->getLength(); i++)
167         {
168             AttributeRule* rule=new AttributeRule(static_cast<DOMElement*>(nlist->item(i)));
169 #ifdef HAVE_GOOD_STL
170             xstring key=rule->getName();
171             key=key + chBang + chBang + (rule->getNamespace() ? rule->getNamespace() : Constants::SHIB_ATTRIBUTE_NAMESPACE_URI);
172 #else
173             auto_ptr_char aname(rule->getName());
174             string key(aname.get());
175             key+="!!";
176             if (rule->getNamespace())
177             {
178                 auto_ptr_char ans(rule->getNamespace());
179                 key+=ans.get();
180             }
181             else
182                 key+="urn:mace:shibboleth:1.0:attributeNamespace:uri";
183 #endif
184             m_attrMap[key]=rule;
185             m_attrs.push_back(rule);
186             if (rule->getAlias())
187                 m_aliasMap[rule->getAlias()]=rule;
188         }
189     }
190     catch (SAMLException& e)
191     {
192         log.errorStream() << "Error while parsing AAP: " << e.what() << CategoryStream::ENDLINE;
193         this->~XMLAAPImpl();
194         throw;
195     }
196 #ifndef _DEBUG
197     catch (...)
198     {
199         log.error("Unexpected error while parsing AAP");
200         this->~XMLAAPImpl();
201         throw;
202     }
203 #endif
204 }
205
206 XMLAAPImpl::~XMLAAPImpl()
207 {
208 #ifdef HAVE_GOOD_STL
209     for_each(m_attrMap.begin(),m_attrMap.end(),shibtarget::cleanup_pair<xstring,AttributeRule>());
210 #else
211     for_each(m_attrMap.begin(),m_attrMap.end(),shibtarget::cleanup_pair<string,AttributeRule>());
212 #endif
213 }
214
215 XMLAAPImpl::AttributeRule::AttributeRule(const DOMElement* e) :
216     m_alias(e->hasAttributeNS(NULL,SHIB_L(Alias)) ? e->getAttributeNS(NULL,SHIB_L(Alias)) : NULL),
217     m_header(e->hasAttributeNS(NULL,SHIB_L(Header)) ? e->getAttributeNS(NULL,SHIB_L(Header)) : NULL),
218     m_scoped(false)
219     
220 {
221     m_name=e->getAttributeNS(NULL,SHIB_L(Name));
222     m_namespace=e->getAttributeNS(NULL,SHIB_L(Namespace));
223     if (!m_namespace || !*m_namespace)
224         m_namespace=Constants::SHIB_ATTRIBUTE_NAMESPACE_URI;
225     
226     const XMLCh* caseSensitive=e->getAttributeNS(NULL,SHIB_L(CaseSensitive));
227     m_caseSensitive=(!caseSensitive || !*caseSensitive || *caseSensitive==chDigit_1 || *caseSensitive==chLatin_t);
228     
229     const XMLCh* scoped=e->getAttributeNS(NULL,SHIB_L(Scoped));
230     m_scoped=(scoped && (*scoped==chDigit_1 || *scoped==chLatin_t));
231     
232     // Check for an AnySite rule.
233     DOMElement* anysite = saml::XML::getFirstChildElement(e);
234     if (anysite && saml::XML::isElementNamed(static_cast<DOMElement*>(anysite),::XML::SHIB_NS,SHIB_L(AnySite)))
235     {
236         // Process Scope elements.
237         DOMNodeList* vlist = static_cast<DOMElement*>(anysite)->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(Scope));
238         for (unsigned int i=0; vlist && i<vlist->getLength(); i++)
239         {
240             m_scoped=true;
241             DOMElement* se=static_cast<DOMElement*>(vlist->item(i));
242             DOMNode* valnode=se->getFirstChild();
243             if (valnode && valnode->getNodeType()==DOMNode::TEXT_NODE)
244             {
245                 const XMLCh* accept=se->getAttributeNS(NULL,SHIB_L(Accept));
246                 if (!accept || !*accept || *accept==chDigit_1 || *accept==chLatin_t)
247                     m_anySiteRule.scopeAccepts.push_back(pair<value_type,const XMLCh*>(toValueType(se),valnode->getNodeValue()));
248                 else
249                     m_anySiteRule.scopeDenials.push_back(pair<value_type,const XMLCh*>(toValueType(se),valnode->getNodeValue()));
250             }
251         }
252
253         // Check for an AnyValue rule.
254         vlist = static_cast<DOMElement*>(anysite)->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(AnyValue));
255         if (vlist && vlist->getLength())
256         {
257             m_anySiteRule.anyValue=true;
258         }
259         else
260         {
261             // Process each Value element.
262             vlist = static_cast<DOMElement*>(anysite)->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(Value));
263             for (unsigned int j=0; vlist && j<vlist->getLength(); j++)
264             {
265                 DOMElement* ve=static_cast<DOMElement*>(vlist->item(j));
266                 DOMNode* valnode=ve->getFirstChild();
267                 if (valnode && valnode->getNodeType()==DOMNode::TEXT_NODE) {
268                     const XMLCh* accept=ve->getAttributeNS(NULL,SHIB_L(Accept));
269                     if (!accept || !*accept || *accept==chDigit_1 || *accept==chLatin_t)
270                         m_anySiteRule.valueAccepts.push_back(pair<value_type,const XMLCh*>(toValueType(ve),valnode->getNodeValue()));
271                     else
272                         m_anySiteRule.valueDenials.push_back(pair<value_type,const XMLCh*>(toValueType(ve),valnode->getNodeValue()));
273                 }
274             }
275         }
276     }
277
278     // Loop over the SiteRule elements.
279     DOMNodeList* slist = e->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(SiteRule));
280     for (unsigned int k=0; slist && k<slist->getLength(); k++)
281     {
282         const XMLCh* srulename=static_cast<DOMElement*>(slist->item(k))->getAttributeNS(NULL,SHIB_L(Name));
283 #ifdef HAVE_GOOD_STL
284         m_siteMap[srulename]=SiteRule();
285         SiteRule& srule=m_siteMap[srulename];
286 #else
287         auto_ptr_char srulename2(srulename);
288         m_siteMap[srulename2.get()]=SiteRule();
289         SiteRule& srule=m_siteMap[srulename2.get()];
290 #endif
291
292         // Process Scope elements.
293         DOMNodeList* vlist = static_cast<DOMElement*>(slist->item(k))->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(Scope));
294         for (unsigned int i=0; vlist && i<vlist->getLength(); i++)
295         {
296             m_scoped=true;
297             DOMElement* se=static_cast<DOMElement*>(vlist->item(i));
298             DOMNode* valnode=se->getFirstChild();
299             if (valnode && valnode->getNodeType()==DOMNode::TEXT_NODE)
300             {
301                 const XMLCh* accept=se->getAttributeNS(NULL,SHIB_L(Accept));
302                 if (!accept || !*accept || *accept==chDigit_1 || *accept==chLatin_t)
303                     srule.scopeAccepts.push_back(pair<value_type,const XMLCh*>(toValueType(se),valnode->getNodeValue()));
304                 else
305                     srule.scopeDenials.push_back(pair<value_type,const XMLCh*>(toValueType(se),valnode->getNodeValue()));
306             }
307         }
308
309         // Check for an AnyValue rule.
310         vlist = static_cast<DOMElement*>(slist->item(k))->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(AnyValue));
311         if (vlist && vlist->getLength())
312         {
313             srule.anyValue=true;
314         }
315         else
316         {
317             // Process each Value element.
318             vlist = static_cast<DOMElement*>(slist->item(k))->getElementsByTagNameNS(::XML::SHIB_NS,SHIB_L(Value));
319             for (unsigned int j=0; vlist && j<vlist->getLength(); j++)
320             {
321                 DOMElement* ve=static_cast<DOMElement*>(vlist->item(j));
322                 DOMNode* valnode=ve->getFirstChild();
323                 if (valnode && valnode->getNodeType()==DOMNode::TEXT_NODE) {
324                     const XMLCh* accept=ve->getAttributeNS(NULL,SHIB_L(Accept));
325                     if (!accept || !*accept || *accept==chDigit_1 || *accept==chLatin_t)
326                         srule.valueAccepts.push_back(pair<value_type,const XMLCh*>(toValueType(ve),valnode->getNodeValue()));
327                     else
328                         srule.valueDenials.push_back(pair<value_type,const XMLCh*>(toValueType(ve),valnode->getNodeValue()));
329                 }
330             }
331         }
332     }
333 }
334
335 XMLAAPImpl::AttributeRule::value_type XMLAAPImpl::AttributeRule::toValueType(const DOMElement* e)
336 {
337     if (!XMLString::compareString(SHIB_L(literal),e->getAttributeNS(NULL,SHIB_L(Type))))
338         return literal;
339     else if (!XMLString::compareString(SHIB_L(regexp),e->getAttributeNS(NULL,SHIB_L(Type))))
340         return regexp;
341     else if (!XMLString::compareString(SHIB_L(xpath),e->getAttributeNS(NULL,SHIB_L(Type))))
342         return xpath;
343     throw MalformedException("Found an invalid value or scope rule type.");
344 }
345
346 const IAttributeRule* XMLAAP::lookup(const XMLCh* attrName, const XMLCh* attrNamespace) const
347 {
348 #ifdef HAVE_GOOD_STL
349     xstring key=attrName;
350     key=key + chBang + chBang + (attrNamespace ? attrNamespace : Constants::SHIB_ATTRIBUTE_NAMESPACE_URI);
351 #else
352     auto_ptr_char aname(attrName);
353     string key=aname.get();
354     key+="!!";
355     if (attrNamespace)
356     {
357         auto_ptr_char ans(attrNamespace);
358         key+=ans.get();
359     }
360     else
361         key+="urn:mace:shibboleth:1.0:attributeNamespace:uri";
362 #endif
363     XMLAAPImpl* impl=dynamic_cast<XMLAAPImpl*>(getImplementation());
364     XMLAAPImpl::attrmap_t::const_iterator i=impl->m_attrMap.find(key);
365     return (i==impl->m_attrMap.end()) ? NULL : i->second;
366 }
367
368 const IAttributeRule* XMLAAP::lookup(const char* alias) const
369 {
370     XMLAAPImpl* impl=dynamic_cast<XMLAAPImpl*>(getImplementation());
371     map<string,const IAttributeRule*>::const_iterator i=impl->m_aliasMap.find(alias);
372     return (i==impl->m_aliasMap.end()) ? NULL : i->second;
373 }
374
375 Iterator<const IAttributeRule*> XMLAAP::getAttributeRules() const
376 {
377     return dynamic_cast<XMLAAPImpl*>(getImplementation())->m_attrs;
378 }
379
380 namespace {
381     bool match(const XMLCh* exp, const XMLCh* test)
382     {
383         try
384         {
385             RegularExpression re(exp);
386             if (re.matches(test))
387                 return true;
388         }
389         catch (XMLException& ex)
390         {
391             auto_ptr<char> tmp(XMLString::transcode(ex.getMessage()));
392             Category::getInstance(XMLPROVIDERS_LOGCAT".XMLAAPImpl").errorStream()
393                 << "caught exception while parsing regular expression: " << tmp.get() << CategoryStream::ENDLINE;
394         }
395         return false;
396     }
397 }
398
399 bool XMLAAPImpl::AttributeRule::scopeCheck(
400     const DOMElement* e,
401     const IExtendedEntityDescriptor* source,
402     const vector<const SiteRule*>& ruleStack
403     ) const
404 {
405 #ifdef _DEBUG
406     saml::NDC ndc("scopeCheck");
407 #endif
408     Category& log=Category::getInstance(XMLPROVIDERS_LOGCAT".AAP");
409
410     // Are we scoped?
411     const XMLCh* scope=e->getAttributeNS(NULL,SHIB_L(Scope));
412     if (!scope || !*scope) {
413         // Are we allowed to be unscoped?
414         if (m_scoped && log.isWarnEnabled()) {
415                 auto_ptr_char temp(m_name);
416                 log.warn("attribute (%s) is scoped, no scope supplied, rejecting it",temp.get());
417         }
418         return !m_scoped;
419     }
420
421     // With the new algorithm, we evaluate each matching rule in sequence, separately.
422     for (vector<const SiteRule*>::const_iterator rule=ruleStack.begin(); rule!=ruleStack.end(); rule++) {
423
424         // Now run any denials.
425         vector<pair<value_type,const XMLCh*> >::const_iterator i;
426         for (i=(*rule)->scopeDenials.begin(); i!=(*rule)->scopeDenials.end(); i++) {
427             if ((i->first==literal && !XMLString::compareString(i->second,scope)) ||
428                 (i->first==regexp && match(i->second,scope))) {
429                 if (log.isWarnEnabled()) {
430                     auto_ptr_char temp(m_name);
431                     auto_ptr_char temp2(scope);
432                     log.warn("attribute (%s) scope (%s) denied by site rule, rejecting it",temp.get(),temp2.get());
433                 }
434                 return false;
435             }
436             else if (i->first==xpath)
437                 log.warn("scope checking does not permit XPath rules");
438         }
439
440         // Now run any accepts.
441         for (i=(*rule)->scopeAccepts.begin(); i!=(*rule)->scopeAccepts.end(); i++) {
442             if ((i->first==literal && !XMLString::compareString(i->second,scope)) ||
443                 (i->first==regexp && match(i->second,scope))) {
444                 log.debug("matching site rule, scope match");
445                 return true;
446             }
447             else if (i->first==xpath)
448                 log.warn("scope checking does not permit XPath rules");
449         }
450     }
451
452     // If we still can't decide, defer to metadata.
453     if (source) {
454         Iterator<pair<const XMLCh*,bool> > domains=source->getScopes();
455         while (domains.hasNext()) {
456             const pair<const XMLCh*,bool>& p=domains.next();
457             if ((p.second && match(p.first,scope)) || !XMLString::compareString(p.first,scope)) {
458                 log.debug("scope match via site metadata");
459                 return true;
460             }
461         }
462     }
463     
464     if (log.isWarnEnabled()) {
465         auto_ptr_char temp(m_name);
466         auto_ptr_char temp2(scope);
467         log.warn("attribute (%s) scope (%s) not accepted",temp.get(),temp2.get());
468     }
469     return false;
470 }
471
472 bool XMLAAPImpl::AttributeRule::accept(const DOMElement* e, const IExtendedEntityDescriptor* source) const
473 {
474 #ifdef _DEBUG
475     saml::NDC ndc("accept");
476 #endif
477     Category& log=Category::getInstance(XMLPROVIDERS_LOGCAT".AAP");
478     
479     if (log.isDebugEnabled()) {
480         auto_ptr_char temp(m_name);
481         auto_ptr_char temp2(source ? source->getId() : NULL);
482         log.debug("evaluating value for attribute (%s) from site (%s)",temp.get(),temp2.get() ? temp2.get() : "<unspecified>");
483     }
484     
485     // This is a complete revamp. The "any" cases become a degenerate case, the "least-specific" matching rule.
486     // The first step is to build a list of matching rules, most-specific to least-specific.
487     
488     vector<const SiteRule*> ruleStack;
489     if (source) {
490         // Primary match is against entityID.
491 #ifdef HAVE_GOOD_STL
492         const XMLCh* os=source->getId();
493 #else
494         auto_ptr_char pos(source->getId());
495         const char* os=pos.get();
496 #endif
497         sitemap_t::const_iterator srule=m_siteMap.find(os);
498         if (srule!=m_siteMap.end())
499             ruleStack.push_back(&srule->second);
500         
501         // Secondary matches are on groups.
502         const IEntitiesDescriptor* group=source->getEntitiesDescriptor();
503         while (group) {
504             if (group->getName()) {
505 #ifdef HAVE_GOOD_STL
506                 os=group->getName();
507 #else
508                 auto_ptr_char gname(group->getName());
509                 const char* os=gname.get();
510 #endif
511                 srule=m_siteMap.find(os);
512                 if (srule!=m_siteMap.end())
513                     ruleStack.push_back(&srule->second);
514             }
515             group = group->getEntitiesDescriptor();
516         }
517     }
518     // Tertiary match is the AnySite rule.
519     ruleStack.push_back(&m_anySiteRule);
520
521     // Still don't support complex content models...
522     DOMNode* n=e->getFirstChild();
523     bool bSimple=(n && n->getNodeType()==DOMNode::TEXT_NODE);
524
525     // With the new algorithm, we evaluate each matching rule in sequence, separately.
526     for (vector<const SiteRule*>::const_iterator rule=ruleStack.begin(); rule!=ruleStack.end(); rule++) {
527
528         // Check for shortcut AnyValue blanket rule.
529         if ((*rule)->anyValue) {
530             log.debug("matching site rule, any value match");
531             return scopeCheck(e,source,ruleStack);
532         }
533
534         // Now run any denials.
535         vector<pair<value_type,const XMLCh*> >::const_iterator i;
536         for (i=(*rule)->valueDenials.begin(); bSimple && i!=(*rule)->valueDenials.end(); i++) {
537             switch (i->first) {
538                 case literal:
539                     if ((m_caseSensitive && !XMLString::compareString(i->second,n->getNodeValue())) ||
540                         (!m_caseSensitive && !XMLString::compareIString(i->second,n->getNodeValue()))) {
541                         if (log.isWarnEnabled()) {
542                             auto_ptr_char temp(m_name);
543                             log.warn("attribute (%s) value explicitly denied by site rule, rejecting it",temp.get());
544                         }
545                         return false;
546                     }
547                     break;
548                 
549                 case regexp:
550                     if (match(i->second,n->getNodeValue())) {
551                         if (log.isWarnEnabled()) {
552                             auto_ptr_char temp(m_name);
553                             log.warn("attribute (%s) value explicitly denied by site rule, rejecting it",temp.get());
554                         }
555                         return false;
556                     }
557                     break;
558                 
559                 case xpath:
560                     log.warn("implementation does not support XPath value rules");
561                     break;
562             }
563         }
564
565         // Now run any accepts.
566         for (i=(*rule)->valueAccepts.begin(); bSimple && i!=(*rule)->valueAccepts.end(); i++) {
567             switch (i->first) {
568                 case literal:
569                     if ((m_caseSensitive && !XMLString::compareString(i->second,n->getNodeValue())) ||
570                         (!m_caseSensitive && !XMLString::compareIString(i->second,n->getNodeValue()))) {
571                         log.debug("site rule, value match");
572                         return scopeCheck(e,source,ruleStack);
573                     }
574                     break;
575                 
576                 case regexp:
577                     if (match(i->second,n->getNodeValue())) {
578                         log.debug("site rule, value match");
579                         return scopeCheck(e,source,ruleStack);
580                     }
581                     break;
582                 
583                 case xpath:
584                     log.warn("implementation does not support XPath value rules");
585                     break;
586             }
587         }
588     }
589
590     if (log.isWarnEnabled()) {
591         auto_ptr_char temp(m_name);
592         auto_ptr_char temp2(n->getNodeValue());
593         log.warn("%sattribute (%s) value (%s) could not be validated by policy, rejecting it",
594                  (bSimple ? "" : "complex "),temp.get(),temp2.get());
595     }
596     return false;
597 }
598
599 void XMLAAPImpl::AttributeRule::apply(SAMLAttribute& attribute, const IEntityDescriptor* source) const
600 {
601     // Check each value.
602     DOMNodeList* vals=attribute.getValueElements();
603     int i2=0;
604     for (unsigned int i=0; vals && i < vals->getLength(); i++) {
605         if (!accept(static_cast<DOMElement*>(vals->item(i)),source ? dynamic_cast<const IExtendedEntityDescriptor*>(source) : NULL))
606             attribute.removeValue(i2);
607         else
608             i2++;
609     }
610     
611     // Now see if we trashed it irrevocably.
612     attribute.checkValidity();
613 }