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
26 #include <sys/types.h>
29 #define SHIB_INSTANTIATE
33 #include <openssl/err.h>
36 using namespace shibboleth;
37 using namespace xmltooling;
38 using namespace log4cpp;
44 vector<Mutex*> g_openssl_locks;
46 map<xmltooling::xstring,const IAttributeFactory*> attrMap;
48 map<XMLCh*,const IAttributeFactory*> attrMap;
52 extern "C" SAMLAttribute* ShibAttributeFactory(DOMElement* e)
54 // First check for an explicit factory.
56 map<xmltooling::xstring,const IAttributeFactory*>::const_iterator i=attrMap.find(e->getAttributeNS(NULL,L(AttributeName)));
58 const XMLCh* aname=e->getAttributeNS(NULL,L(AttributeName));
59 map<XMLCh*,const IAttributeFactory*>::const_iterator i;
60 for (i=attrMap.begin(); i!=attrMap.end(); i++)
61 if (!XMLString::compareString(aname,i->first))
65 return i->second->build(e);
67 // Now check for a Scope attribute to ensure proper value handling whenever possible.
68 DOMElement* n=saml::XML::getFirstChildElement(e,saml::XML::SAML_NS,L(AttributeValue));
69 if (n && n->hasAttributeNS(NULL,ScopedAttribute::Scope))
70 return new ScopedAttribute(e);
72 // Just use the default class.
73 return new SAMLAttribute(e);
76 void ShibConfig::regAttributeMapping(const XMLCh* name, const IAttributeFactory* factory)
78 if (name && factory) {
80 attrMap[name]=factory;
82 attrMap.insert(make_pair(XMLString::replicate(name),factory));
87 void ShibConfig::unregAttributeMapping(const XMLCh* name)
93 for (map<XMLCh*,const IAttributeFactory*>::iterator i=attrMap.begin(); i!=attrMap.end(); i++) {
94 if (!XMLString::compareString(name,i->first)) {
96 XMLString::release(&temp);
105 void ShibConfig::clearAttributeMappings()
107 #ifndef HAVE_GOOD_STL
108 for (map<XMLCh*,const IAttributeFactory*>::iterator i=attrMap.begin(); i!=attrMap.end(); i++) {
109 XMLCh* temp=i->first;
110 XMLString::release(&temp);
116 extern "C" void openssl_locking_callback(int mode,int n,const char *file,int line)
118 if (mode & CRYPTO_LOCK)
119 g_openssl_locks[n]->lock();
121 g_openssl_locks[n]->unlock();
125 extern "C" unsigned long openssl_thread_id(void)
127 return (unsigned long)(pthread_self());
131 bool ShibConfig::init()
133 // Set up OpenSSL locking.
134 for (int i=0; i<CRYPTO_num_locks(); i++)
135 g_openssl_locks.push_back(Mutex::create());
136 CRYPTO_set_locking_callback(openssl_locking_callback);
138 CRYPTO_set_id_callback(openssl_thread_id);
141 SAMLAttribute::setFactory(&ShibAttributeFactory);
145 void ShibConfig::term()
147 SAMLAttribute::setFactory(NULL);
148 clearAttributeMappings();
150 CRYPTO_set_locking_callback(NULL);
151 for (vector<Mutex*>::iterator j=g_openssl_locks.begin(); j!=g_openssl_locks.end(); j++)
153 g_openssl_locks.clear();
156 ShibConfig& ShibConfig::getConfig()