2 * Copyright 2001-2005 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* ShibConfig.cpp - Shibboleth runtime configuration
25 #define SHIB_INSTANTIATE
29 #include <sys/types.h>
31 #include <openssl/err.h>
32 #include <xmltooling/util/Threads.h>
35 using namespace shibboleth;
36 using namespace xmltooling;
37 using namespace log4cpp;
43 vector<Mutex*> g_openssl_locks;
45 map<xmltooling::xstring,const IAttributeFactory*> attrMap;
47 map<XMLCh*,const IAttributeFactory*> attrMap;
51 extern "C" SAMLAttribute* ShibAttributeFactory(DOMElement* e)
53 // First check for an explicit factory.
55 map<xmltooling::xstring,const IAttributeFactory*>::const_iterator i=attrMap.find(e->getAttributeNS(NULL,L(AttributeName)));
57 const XMLCh* aname=e->getAttributeNS(NULL,L(AttributeName));
58 map<XMLCh*,const IAttributeFactory*>::const_iterator i;
59 for (i=attrMap.begin(); i!=attrMap.end(); i++)
60 if (!XMLString::compareString(aname,i->first))
64 return i->second->build(e);
66 // Now check for a Scope attribute to ensure proper value handling whenever possible.
67 DOMElement* n=saml::XML::getFirstChildElement(e,saml::XML::SAML_NS,L(AttributeValue));
68 if (n && n->hasAttributeNS(NULL,ScopedAttribute::Scope))
69 return new ScopedAttribute(e);
71 // Just use the default class.
72 return new SAMLAttribute(e);
75 void ShibConfig::regAttributeMapping(const XMLCh* name, const IAttributeFactory* factory)
77 if (name && factory) {
79 attrMap[name]=factory;
81 attrMap.insert(make_pair(XMLString::replicate(name),factory));
86 void ShibConfig::unregAttributeMapping(const XMLCh* name)
92 for (map<XMLCh*,const IAttributeFactory*>::iterator i=attrMap.begin(); i!=attrMap.end(); i++) {
93 if (!XMLString::compareString(name,i->first)) {
95 XMLString::release(&temp);
104 void ShibConfig::clearAttributeMappings()
106 #ifndef HAVE_GOOD_STL
107 for (map<XMLCh*,const IAttributeFactory*>::iterator i=attrMap.begin(); i!=attrMap.end(); i++) {
108 XMLCh* temp=i->first;
109 XMLString::release(&temp);
115 extern "C" void openssl_locking_callback(int mode,int n,const char *file,int line)
117 if (mode & CRYPTO_LOCK)
118 g_openssl_locks[n]->lock();
120 g_openssl_locks[n]->unlock();
124 extern "C" unsigned long openssl_thread_id(void)
126 return (unsigned long)(pthread_self());
130 bool ShibConfig::init()
132 // Set up OpenSSL locking.
133 for (int i=0; i<CRYPTO_num_locks(); i++)
134 g_openssl_locks.push_back(Mutex::create());
135 CRYPTO_set_locking_callback(openssl_locking_callback);
137 CRYPTO_set_id_callback(openssl_thread_id);
140 SAMLAttribute::setFactory(&ShibAttributeFactory);
144 void ShibConfig::term()
146 SAMLAttribute::setFactory(NULL);
147 clearAttributeMappings();
149 CRYPTO_set_locking_callback(NULL);
150 for (vector<Mutex*>::iterator j=g_openssl_locks.begin(); j!=g_openssl_locks.end(); j++)
152 g_openssl_locks.clear();
155 ShibConfig& ShibConfig::getConfig()